]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/breakpoint.c
Make breakpoint_address_bits look at the location kind
[thirdparty/binutils-gdb.git] / gdb / breakpoint.c
CommitLineData
c906108c 1/* Everything about breakpoints, for GDB.
8926118c 2
4a94e368 3 Copyright (C) 1986-2022 Free Software Foundation, Inc.
c906108c 4
c5aa993b 5 This file is part of GDB.
c906108c 6
c5aa993b
JM
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
a9762ec7 9 the Free Software Foundation; either version 3 of the License, or
c5aa993b 10 (at your option) any later version.
c906108c 11
c5aa993b
JM
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
c906108c 16
c5aa993b 17 You should have received a copy of the GNU General Public License
a9762ec7 18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c
SS
19
20#include "defs.h"
d55e5aa6 21#include "arch-utils.h"
4de283e4
TT
22#include <ctype.h>
23#include "hashtab.h"
24#include "symtab.h"
25#include "frame.h"
c906108c 26#include "breakpoint.h"
4de283e4
TT
27#include "tracepoint.h"
28#include "gdbtypes.h"
c906108c 29#include "expression.h"
d55e5aa6 30#include "gdbcore.h"
4de283e4
TT
31#include "gdbcmd.h"
32#include "value.h"
33#include "command.h"
c906108c 34#include "inferior.h"
45741a9c 35#include "infrun.h"
4de283e4
TT
36#include "gdbthread.h"
37#include "target.h"
c906108c 38#include "language.h"
4de283e4
TT
39#include "gdb-demangle.h"
40#include "filenames.h"
41#include "annotate.h"
42#include "symfile.h"
d55e5aa6 43#include "objfiles.h"
4de283e4
TT
44#include "source.h"
45#include "linespec.h"
46#include "completer.h"
47#include "ui-out.h"
48#include "cli/cli-script.h"
49#include "block.h"
50#include "solib.h"
51#include "solist.h"
d55e5aa6 52#include "observable.h"
4de283e4
TT
53#include "memattr.h"
54#include "ada-lang.h"
55#include "top.h"
56#include "valprint.h"
57#include "jit.h"
65d79d4b 58#include "parser-defs.h"
d322d6d6 59#include "gdbsupport/gdb_regex.h"
55aa24fb 60#include "probe.h"
4de283e4 61#include "cli/cli-utils.h"
d55e5aa6 62#include "stack.h"
4de283e4
TT
63#include "ax-gdb.h"
64#include "dummy-frame.h"
65#include "interps.h"
268a13a5 66#include "gdbsupport/format.h"
cfc31633 67#include "thread-fsm.h"
5d5658a1 68#include "tid-parse.h"
4de283e4 69#include "cli/cli-style.h"
0f8e2034 70#include "cli/cli-decode.h"
d3ce09f5 71
1042e4c0 72/* readline include files */
073bbbb0 73#include "readline/tilde.h"
1042e4c0
SS
74
75/* readline defines this. */
76#undef savestring
77
034dad6f 78#include "mi/mi-common.h"
6dddc817 79#include "extension.h"
325fac50 80#include <algorithm>
5ed8105e 81#include "progspace-and-thread.h"
268a13a5
TT
82#include "gdbsupport/array-view.h"
83#include "gdbsupport/gdb_optional.h"
104c1213 84
4a64f543 85/* Prototypes for local functions. */
c906108c 86
896b6bda 87static void map_breakpoint_numbers (const char *,
48649e1b 88 gdb::function_view<void (breakpoint *)>);
c906108c 89
348d480f
PA
90static void breakpoint_re_set_default (struct breakpoint *);
91
f00aae0f 92static void
626d2320 93 create_sals_from_location_default (struct event_location *location,
03ada39e 94 struct linespec_result *canonical);
983af33b 95
eee031e2
TT
96static void create_breakpoints_sal (struct gdbarch *,
97 struct linespec_result *,
98 gdb::unique_xmalloc_ptr<char>,
99 gdb::unique_xmalloc_ptr<char>,
100 enum bptype,
101 enum bpdisp, int, int,
102 int,
eee031e2 103 int, int, int, unsigned);
983af33b 104
6c5b2ebe 105static std::vector<symtab_and_line> decode_location_default
626d2320 106 (struct breakpoint *b, struct event_location *location,
6c5b2ebe 107 struct program_space *search_pspace);
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,
a1aa2221 121 int loc_enabled);
06edf0c0 122
76897487
KB
123static void breakpoint_adjustment_warning (CORE_ADDR, CORE_ADDR, int, int);
124
a6d9a66e
UW
125static CORE_ADDR adjust_breakpoint_address (struct gdbarch *gdbarch,
126 CORE_ADDR bpaddr,
dda83cd7 127 enum bptype bptype);
76897487 128
85d721b8
PA
129static int watchpoint_locations_match (struct bp_location *loc1,
130 struct bp_location *loc2);
131
7f32a4d5
PA
132static int breakpoint_locations_match (struct bp_location *loc1,
133 struct bp_location *loc2,
134 bool sw_hw_bps_match = false);
135
f1310107 136static int breakpoint_location_address_match (struct bp_location *bl,
accd0bcd 137 const struct address_space *aspace,
f1310107
TJB
138 CORE_ADDR addr);
139
d35ae833 140static int breakpoint_location_address_range_overlap (struct bp_location *,
accd0bcd 141 const address_space *,
d35ae833
PA
142 CORE_ADDR, int);
143
834c0d03 144static int remove_breakpoint (struct bp_location *);
b2b6a7da 145static int remove_breakpoint_1 (struct bp_location *, enum remove_bp_reason);
c906108c 146
313f3b21 147static enum print_stop_action print_bp_stop_message (bpstat *bs);
c906108c 148
a14ed312 149static int hw_breakpoint_used_count (void);
c906108c 150
a1398e0c
PA
151static int hw_watchpoint_use_count (struct breakpoint *);
152
153static int hw_watchpoint_used_count_others (struct breakpoint *except,
154 enum bptype type,
155 int *other_type_used);
c906108c 156
816338b5
SS
157static void enable_breakpoint_disp (struct breakpoint *, enum bpdisp,
158 int count);
c906108c 159
f431efe5 160static void decref_bp_location (struct bp_location **loc);
fe3f5fa8 161
5d926615
TT
162static std::vector<symtab_and_line> bkpt_probe_decode_location
163 (struct breakpoint *b,
164 struct event_location *location,
165 struct program_space *search_pspace);
166
6e14e441
PA
167static bool bl_address_is_meaningful (bp_location *loc);
168
44702360
PA
169/* update_global_location_list's modes of operation wrt to whether to
170 insert locations now. */
171enum ugll_insert_mode
172{
173 /* Don't insert any breakpoint locations into the inferior, only
174 remove already-inserted locations that no longer should be
175 inserted. Functions that delete a breakpoint or breakpoints
176 should specify this mode, so that deleting a breakpoint doesn't
177 have the side effect of inserting the locations of other
178 breakpoints that are marked not-inserted, but should_be_inserted
179 returns true on them.
180
181 This behavior is useful is situations close to tear-down -- e.g.,
182 after an exec, while the target still has execution, but
183 breakpoint shadows of the previous executable image should *NOT*
184 be restored to the new image; or before detaching, where the
185 target still has execution and wants to delete breakpoints from
186 GDB's lists, and all breakpoints had already been removed from
187 the inferior. */
188 UGLL_DONT_INSERT,
189
a25a5a45
PA
190 /* May insert breakpoints iff breakpoints_should_be_inserted_now
191 claims breakpoints should be inserted now. */
04086b45
PA
192 UGLL_MAY_INSERT,
193
a25a5a45
PA
194 /* Insert locations now, irrespective of
195 breakpoints_should_be_inserted_now. E.g., say all threads are
196 stopped right now, and the user did "continue". We need to
197 insert breakpoints _before_ resuming the target, but
198 UGLL_MAY_INSERT wouldn't insert them, because
199 breakpoints_should_be_inserted_now returns false at that point,
200 as no thread is running yet. */
04086b45 201 UGLL_INSERT
44702360
PA
202};
203
204static void update_global_location_list (enum ugll_insert_mode);
a5606eee 205
44702360 206static void update_global_location_list_nothrow (enum ugll_insert_mode);
74960c60 207
74960c60 208static void insert_breakpoint_locations (void);
a5606eee 209
0b39b52e 210static void trace_pass_command (const char *, int);
1042e4c0 211
558a9d82
YQ
212static void set_tracepoint_count (int num);
213
f2478a7e 214static bool is_masked_watchpoint (const struct breakpoint *b);
9c06b0b4 215
983af33b
SDJ
216/* Return 1 if B refers to a static tracepoint set by marker ("-m"), zero
217 otherwise. */
218
219static int strace_marker_p (struct breakpoint *b);
0fb4aa4b 220
8613a476
TT
221static void bkpt_probe_create_sals_from_location
222 (struct event_location *location,
03ada39e 223 struct linespec_result *canonical);
8613a476
TT
224static void tracepoint_probe_create_sals_from_location
225 (struct event_location *location,
03ada39e 226 struct linespec_result *canonical);
8613a476 227
a678887d 228const struct breakpoint_ops base_breakpoint_ops =
8613a476
TT
229{
230 create_sals_from_location_default,
eee031e2 231 create_breakpoints_sal,
8613a476
TT
232};
233
55aa24fb 234/* Breakpoints set on probes. */
a678887d 235static const struct breakpoint_ops bkpt_probe_breakpoint_ops =
8613a476
TT
236{
237 bkpt_probe_create_sals_from_location,
eee031e2 238 create_breakpoints_sal,
8613a476 239};
55aa24fb 240
bac7c5cf 241/* Tracepoints set on probes. */
a678887d 242static const struct breakpoint_ops tracepoint_probe_breakpoint_ops =
8613a476
TT
243{
244 tracepoint_probe_create_sals_from_location,
eee031e2 245 create_breakpoints_sal,
8613a476 246};
bac7c5cf 247
1c2cbcf1
PA
248/* Implementation of abstract dtors. These must exist to satisfy the
249 linker. */
250
251breakpoint::~breakpoint ()
252{
253}
254
255base_breakpoint::~base_breakpoint ()
256{
257}
258
259catchpoint::~catchpoint ()
260{
261}
262
098f1272
TT
263/* The structure to be used in regular breakpoints. */
264struct ordinary_breakpoint : public base_breakpoint
265{
73063f51
TT
266 using base_breakpoint::base_breakpoint;
267
7dd8e7ae 268 int resources_needed (const struct bp_location *) override;
7bd86313 269 enum print_stop_action print_it (const bpstat *bs) const override;
b713485d 270 void print_mention () const override;
4d1ae558 271 void print_recreate (struct ui_file *fp) const override;
098f1272
TT
272};
273
274/* Internal breakpoints. */
275struct internal_breakpoint : public base_breakpoint
276{
752a2291
PA
277 internal_breakpoint (struct gdbarch *gdbarch,
278 enum bptype type, CORE_ADDR address)
279 : base_breakpoint (gdbarch, type)
280 {
281 symtab_and_line sal;
282 sal.pc = address;
283 sal.section = find_pc_overlay (sal.pc);
284 sal.pspace = current_program_space;
285 add_location (sal);
286
287 pspace = current_program_space;
288 disposition = disp_donttouch;
289 }
73063f51 290
c359fff5
TT
291 void re_set () override;
292 void check_status (struct bpstat *bs) override;
7bd86313 293 enum print_stop_action print_it (const bpstat *bs) const override;
b713485d 294 void print_mention () const override;
098f1272
TT
295};
296
297/* Momentary breakpoints. */
298struct momentary_breakpoint : public base_breakpoint
299{
73063f51
TT
300 using base_breakpoint::base_breakpoint;
301
1fd30a47
TT
302 void re_set () override;
303 void check_status (struct bpstat *bs) override;
7bd86313 304 enum print_stop_action print_it (const bpstat *bs) const override;
b713485d 305 void print_mention () const override;
098f1272
TT
306};
307
308/* DPrintf breakpoints. */
5ad71d67 309struct dprintf_breakpoint : public ordinary_breakpoint
098f1272 310{
73063f51
TT
311 using ordinary_breakpoint::ordinary_breakpoint;
312
f293a0b5
TT
313 void re_set () override;
314 int breakpoint_hit (const struct bp_location *bl,
315 const address_space *aspace,
316 CORE_ADDR bp_addr,
317 const target_waitstatus &ws) override;
4d1ae558 318 void print_recreate (struct ui_file *fp) const override;
f293a0b5 319 void after_condition_true (struct bpstat *bs) override;
098f1272
TT
320};
321
ec45bb67
TT
322/* Ranged breakpoints. */
323struct ranged_breakpoint : public ordinary_breakpoint
324{
b925bf21
PA
325 explicit ranged_breakpoint (struct gdbarch *gdbarch,
326 const symtab_and_line &sal_start,
327 int length,
328 event_location_up start_location,
329 event_location_up end_location)
73063f51
TT
330 : ordinary_breakpoint (gdbarch, bp_hardware_breakpoint)
331 {
b925bf21
PA
332 bp_location *bl = add_location (sal_start);
333 bl->length = length;
334
335 disposition = disp_donttouch;
336
337 location = std::move (start_location);
338 location_range_end = std::move (end_location);
73063f51
TT
339 }
340
ec45bb67
TT
341 int breakpoint_hit (const struct bp_location *bl,
342 const address_space *aspace,
343 CORE_ADDR bp_addr,
344 const target_waitstatus &ws) override;
345 int resources_needed (const struct bp_location *) override;
7bd86313 346 enum print_stop_action print_it (const bpstat *bs) const override;
a67bcaba 347 bool print_one (bp_location **) const override;
ec45bb67 348 void print_one_detail (struct ui_out *) const override;
b713485d 349 void print_mention () const override;
4d1ae558 350 void print_recreate (struct ui_file *fp) const override;
ec45bb67
TT
351};
352
6d7a8c56
TT
353/* Static tracepoints with marker (`-m'). */
354struct static_marker_tracepoint : public tracepoint
355{
73063f51
TT
356 using tracepoint::tracepoint;
357
6d7a8c56
TT
358 std::vector<symtab_and_line> decode_location
359 (struct event_location *location,
360 struct program_space *search_pspace) override;
361};
362
d3ce09f5
SS
363/* The style in which to perform a dynamic printf. This is a user
364 option because different output options have different tradeoffs;
365 if GDB does the printing, there is better error handling if there
366 is a problem with any of the arguments, but using an inferior
367 function lets you have special-purpose printers and sending of
368 output to the same place as compiled-in print functions. */
369
370static const char dprintf_style_gdb[] = "gdb";
371static const char dprintf_style_call[] = "call";
372static const char dprintf_style_agent[] = "agent";
373static const char *const dprintf_style_enums[] = {
374 dprintf_style_gdb,
375 dprintf_style_call,
376 dprintf_style_agent,
377 NULL
378};
379static const char *dprintf_style = dprintf_style_gdb;
380
381/* The function to use for dynamic printf if the preferred style is to
382 call into the inferior. The value is simply a string that is
383 copied into the command, so it can be anything that GDB can
384 evaluate to a callable address, not necessarily a function name. */
385
e0700ba4 386static std::string dprintf_function = "printf";
d3ce09f5
SS
387
388/* The channel to use for dynamic printf if the preferred style is to
389 call into the inferior; if a nonempty string, it will be passed to
390 the call as the first argument, with the format string as the
391 second. As with the dprintf function, this can be anything that
392 GDB knows how to evaluate, so in addition to common choices like
393 "stderr", this could be an app-specific expression like
394 "mystreams[curlogger]". */
395
e0700ba4 396static std::string dprintf_channel;
d3ce09f5
SS
397
398/* True if dprintf commands should continue to operate even if GDB
399 has disconnected. */
491144b5 400static bool disconnected_dprintf = true;
d3ce09f5 401
5cea2a26
PA
402struct command_line *
403breakpoint_commands (struct breakpoint *b)
404{
d1b0a7bf 405 return b->commands ? b->commands.get () : NULL;
5cea2a26 406}
3daf8fe5 407
f3b1572e
PA
408/* Flag indicating that a command has proceeded the inferior past the
409 current breakpoint. */
410
491144b5 411static bool breakpoint_proceeded;
f3b1572e 412
956a9fb9 413const char *
2cec12e5
AR
414bpdisp_text (enum bpdisp disp)
415{
4a64f543
MS
416 /* NOTE: the following values are a part of MI protocol and
417 represent values of 'disp' field returned when inferior stops at
418 a breakpoint. */
bc043ef3 419 static const char * const bpdisps[] = {"del", "dstp", "dis", "keep"};
cc59ec59 420
2cec12e5
AR
421 return bpdisps[(int) disp];
422}
c906108c 423
4a64f543 424/* Prototypes for exported functions. */
c906108c 425/* If FALSE, gdb will not use hardware support for watchpoints, even
4a64f543 426 if such is available. */
c906108c
SS
427static int can_use_hw_watchpoints;
428
920d2a44
AC
429static void
430show_can_use_hw_watchpoints (struct ui_file *file, int from_tty,
431 struct cmd_list_element *c,
432 const char *value)
433{
6cb06a8c
TT
434 gdb_printf (file,
435 _("Debugger's willingness to use "
436 "watchpoint hardware is %s.\n"),
437 value);
920d2a44
AC
438}
439
fa8d40ab
JJ
440/* If AUTO_BOOLEAN_FALSE, gdb will not attempt to create pending breakpoints.
441 If AUTO_BOOLEAN_TRUE, gdb will automatically create pending breakpoints
4a64f543 442 for unrecognized breakpoint locations.
fa8d40ab
JJ
443 If AUTO_BOOLEAN_AUTO, gdb will query when breakpoints are unrecognized. */
444static enum auto_boolean pending_break_support;
920d2a44
AC
445static void
446show_pending_break_support (struct ui_file *file, int from_tty,
447 struct cmd_list_element *c,
448 const char *value)
449{
6cb06a8c
TT
450 gdb_printf (file,
451 _("Debugger's behavior regarding "
452 "pending breakpoints is %s.\n"),
453 value);
920d2a44 454}
fa8d40ab 455
491144b5 456/* If true, gdb will automatically use hardware breakpoints for breakpoints
4a64f543 457 set with "break" but falling in read-only memory.
491144b5 458 If false, gdb will warn about such breakpoints, but won't automatically
765dc015 459 use hardware breakpoints. */
491144b5 460static bool automatic_hardware_breakpoints;
765dc015
VP
461static void
462show_automatic_hardware_breakpoints (struct ui_file *file, int from_tty,
463 struct cmd_list_element *c,
464 const char *value)
465{
6cb06a8c
TT
466 gdb_printf (file,
467 _("Automatic usage of hardware breakpoints is %s.\n"),
468 value);
765dc015
VP
469}
470
a25a5a45
PA
471/* If on, GDB keeps breakpoints inserted even if the inferior is
472 stopped, and immediately inserts any new breakpoints as soon as
473 they're created. If off (default), GDB keeps breakpoints off of
474 the target as long as possible. That is, it delays inserting
475 breakpoints until the next resume, and removes them again when the
476 target fully stops. This is a bit safer in case GDB crashes while
477 processing user input. */
491144b5 478static bool always_inserted_mode = false;
72d0e2c5 479
33e5cbd6 480static void
74960c60 481show_always_inserted_mode (struct ui_file *file, int from_tty,
33e5cbd6 482 struct cmd_list_element *c, const char *value)
74960c60 483{
6cb06a8c
TT
484 gdb_printf (file, _("Always inserted breakpoint mode is %s.\n"),
485 value);
74960c60
VP
486}
487
b57bacec
PA
488/* See breakpoint.h. */
489
33e5cbd6 490int
a25a5a45 491breakpoints_should_be_inserted_now (void)
33e5cbd6 492{
a25a5a45
PA
493 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
494 {
495 /* If breakpoints are global, they should be inserted even if no
496 thread under gdb's control is running, or even if there are
497 no threads under GDB's control yet. */
498 return 1;
499 }
5b6d1e4f 500 else
a25a5a45 501 {
a25a5a45
PA
502 if (always_inserted_mode)
503 {
504 /* The user wants breakpoints inserted even if all threads
505 are stopped. */
506 return 1;
507 }
508
5b6d1e4f
PA
509 for (inferior *inf : all_inferiors ())
510 if (inf->has_execution ()
511 && threads_are_executing (inf->process_target ()))
512 return 1;
372316f1
PA
513
514 /* Don't remove breakpoints yet if, even though all threads are
515 stopped, we still have events to process. */
08036331 516 for (thread_info *tp : all_non_exited_threads ())
1edb66d8 517 if (tp->resumed () && tp->has_pending_waitstatus ())
372316f1 518 return 1;
a25a5a45
PA
519 }
520 return 0;
33e5cbd6 521}
765dc015 522
b775012e
LM
523static const char condition_evaluation_both[] = "host or target";
524
525/* Modes for breakpoint condition evaluation. */
526static const char condition_evaluation_auto[] = "auto";
527static const char condition_evaluation_host[] = "host";
528static const char condition_evaluation_target[] = "target";
529static const char *const condition_evaluation_enums[] = {
530 condition_evaluation_auto,
531 condition_evaluation_host,
532 condition_evaluation_target,
533 NULL
534};
535
536/* Global that holds the current mode for breakpoint condition evaluation. */
537static const char *condition_evaluation_mode_1 = condition_evaluation_auto;
538
539/* Global that we use to display information to the user (gets its value from
540 condition_evaluation_mode_1. */
541static const char *condition_evaluation_mode = condition_evaluation_auto;
542
543/* Translate a condition evaluation mode MODE into either "host"
544 or "target". This is used mostly to translate from "auto" to the
545 real setting that is being used. It returns the translated
546 evaluation mode. */
547
548static const char *
549translate_condition_evaluation_mode (const char *mode)
550{
551 if (mode == condition_evaluation_auto)
552 {
553 if (target_supports_evaluation_of_breakpoint_conditions ())
554 return condition_evaluation_target;
555 else
556 return condition_evaluation_host;
557 }
558 else
559 return mode;
560}
561
562/* Discovers what condition_evaluation_auto translates to. */
563
564static const char *
565breakpoint_condition_evaluation_mode (void)
566{
567 return translate_condition_evaluation_mode (condition_evaluation_mode);
568}
569
570/* Return true if GDB should evaluate breakpoint conditions or false
571 otherwise. */
572
573static int
574gdb_evaluates_breakpoint_condition_p (void)
575{
576 const char *mode = breakpoint_condition_evaluation_mode ();
577
578 return (mode == condition_evaluation_host);
579}
580
c906108c
SS
581/* Are we executing breakpoint commands? */
582static int executing_breakpoint_commands;
583
c02f5703
MS
584/* Are overlay event breakpoints enabled? */
585static int overlay_events_enabled;
586
e09342b5 587/* See description in breakpoint.h. */
491144b5 588bool target_exact_watchpoints = false;
e09342b5 589
c906108c 590/* Walk the following statement or block through all breakpoints.
e5dd4106 591 ALL_BREAKPOINTS_SAFE does so even if the statement deletes the
4a64f543 592 current breakpoint. */
c906108c 593
5c44784c
JM
594#define ALL_BREAKPOINTS_SAFE(B,TMP) \
595 for (B = breakpoint_chain; \
596 B ? (TMP=B->next, 1): 0; \
597 B = TMP)
c906108c 598
7cc221ef 599/* Chains of all breakpoints defined. */
c906108c 600
81e6b8eb 601static struct breakpoint *breakpoint_chain;
c906108c 602
240edef6 603/* See breakpoint.h. */
43892fdf 604
240edef6 605breakpoint_range
43892fdf
SM
606all_breakpoints ()
607{
608 return breakpoint_range (breakpoint_chain);
609}
610
240edef6 611/* See breakpoint.h. */
1428b37a 612
240edef6 613breakpoint_safe_range
1428b37a
SM
614all_breakpoints_safe ()
615{
616 return breakpoint_safe_range (all_breakpoints ());
617}
618
f6d17b2b
SM
619/* See breakpoint.h. */
620
621tracepoint_range
622all_tracepoints ()
623{
624 return tracepoint_range (breakpoint_chain);
625}
626
39ef2f62 627/* Array is sorted by bp_location_is_less_than - primarily by the ADDRESS. */
876fa593 628
5d51cd5d 629static std::vector<bp_location *> bp_locations;
876fa593 630
055c879f
SM
631/* See breakpoint.h. */
632
633const std::vector<bp_location *> &
48d7020b
SM
634all_bp_locations ()
635{
636 return bp_locations;
637}
638
e0d9a270
SM
639/* Range to iterate over breakpoint locations at a given address. */
640
641struct bp_locations_at_addr_range
642{
643 using iterator = std::vector<bp_location *>::iterator;
644
645 bp_locations_at_addr_range (CORE_ADDR addr)
646 {
647 struct compare
648 {
649 bool operator() (const bp_location *loc, CORE_ADDR addr_) const
650 { return loc->address < addr_; }
651
652 bool operator() (CORE_ADDR addr_, const bp_location *loc) const
653 { return addr_ < loc->address; }
654 };
655
656 auto it_pair = std::equal_range (bp_locations.begin (), bp_locations.end (),
657 addr, compare ());
658
659 m_begin = it_pair.first;
660 m_end = it_pair.second;
661 }
662
663 iterator begin () const
664 { return m_begin; }
665
666 iterator end () const
667 { return m_end; }
668
669private:
670 iterator m_begin;
671 iterator m_end;
672};
673
674/* Return a range to iterate over all breakpoint locations exactly at address
675 ADDR.
676
677 If it's needed to iterate multiple times on the same range, it's possible
678 to save the range in a local variable and use it multiple times:
679
680 auto range = all_bp_locations_at_addr (addr);
681
682 for (bp_location *loc : range)
683 // use loc
684
685 for (bp_location *loc : range)
686 // use loc
687
688 This saves a bit of time, as it avoids re-doing the binary searches to find
689 the range's boundaries. Just remember not to change the bp_locations vector
690 in the mean time, as it could make the range's iterators stale. */
691
692static bp_locations_at_addr_range
693all_bp_locations_at_addr (CORE_ADDR addr)
694{
695 return bp_locations_at_addr_range (addr);
696}
697
4a64f543 698/* Maximum alignment offset between bp_target_info.PLACED_ADDRESS and
f5336ca5 699 ADDRESS for the current elements of BP_LOCATIONS which get a valid
4a64f543 700 result from bp_location_has_shadow. You can use it for roughly
f5336ca5 701 limiting the subrange of BP_LOCATIONS to scan for shadow bytes for
4a64f543 702 an address you need to read. */
876fa593 703
f5336ca5 704static CORE_ADDR bp_locations_placed_address_before_address_max;
876fa593 705
4a64f543
MS
706/* Maximum offset plus alignment between bp_target_info.PLACED_ADDRESS
707 + bp_target_info.SHADOW_LEN and ADDRESS for the current elements of
f5336ca5
PA
708 BP_LOCATIONS which get a valid result from bp_location_has_shadow.
709 You can use it for roughly limiting the subrange of BP_LOCATIONS to
4a64f543 710 scan for shadow bytes for an address you need to read. */
876fa593 711
f5336ca5 712static CORE_ADDR bp_locations_shadow_len_after_address_max;
7cc221ef 713
4a64f543 714/* The locations that no longer correspond to any breakpoint, unlinked
f5336ca5
PA
715 from the bp_locations array, but for which a hit may still be
716 reported by a target. */
1123588c 717static std::vector<bp_location *> moribund_locations;
20874c92 718
c906108c
SS
719/* Number of last breakpoint made. */
720
95a42b64
TT
721static int breakpoint_count;
722
86b17b60
PA
723/* The value of `breakpoint_count' before the last command that
724 created breakpoints. If the last (break-like) command created more
725 than one breakpoint, then the difference between BREAKPOINT_COUNT
726 and PREV_BREAKPOINT_COUNT is more than one. */
727static int prev_breakpoint_count;
c906108c 728
1042e4c0
SS
729/* Number of last tracepoint made. */
730
95a42b64 731static int tracepoint_count;
1042e4c0 732
6149aea9
PA
733static struct cmd_list_element *breakpoint_set_cmdlist;
734static struct cmd_list_element *breakpoint_show_cmdlist;
9291a0cd 735struct cmd_list_element *save_cmdlist;
6149aea9 736
468d015d
JJ
737/* Return whether a breakpoint is an active enabled breakpoint. */
738static int
739breakpoint_enabled (struct breakpoint *b)
740{
0d381245 741 return (b->enable_state == bp_enabled);
468d015d
JJ
742}
743
c906108c
SS
744/* Set breakpoint count to NUM. */
745
95a42b64 746static void
fba45db2 747set_breakpoint_count (int num)
c906108c 748{
86b17b60 749 prev_breakpoint_count = breakpoint_count;
c906108c 750 breakpoint_count = num;
4fa62494 751 set_internalvar_integer (lookup_internalvar ("bpnum"), num);
c906108c
SS
752}
753
86b17b60
PA
754/* Used by `start_rbreak_breakpoints' below, to record the current
755 breakpoint count before "rbreak" creates any breakpoint. */
756static int rbreak_start_breakpoint_count;
757
95a42b64
TT
758/* Called at the start an "rbreak" command to record the first
759 breakpoint made. */
86b17b60 760
c80049d3 761scoped_rbreak_breakpoints::scoped_rbreak_breakpoints ()
95a42b64 762{
86b17b60 763 rbreak_start_breakpoint_count = breakpoint_count;
95a42b64
TT
764}
765
766/* Called at the end of an "rbreak" command to record the last
767 breakpoint made. */
86b17b60 768
c80049d3 769scoped_rbreak_breakpoints::~scoped_rbreak_breakpoints ()
95a42b64 770{
86b17b60 771 prev_breakpoint_count = rbreak_start_breakpoint_count;
95a42b64
TT
772}
773
4a64f543 774/* Used in run_command to zero the hit count when a new run starts. */
c906108c
SS
775
776void
fba45db2 777clear_breakpoint_hit_counts (void)
c906108c 778{
43892fdf 779 for (breakpoint *b : all_breakpoints ())
c906108c
SS
780 b->hit_count = 0;
781}
782
c906108c 783\f
48cb2d85
VP
784/* Return the breakpoint with the specified number, or NULL
785 if the number does not refer to an existing breakpoint. */
786
787struct breakpoint *
788get_breakpoint (int num)
789{
43892fdf 790 for (breakpoint *b : all_breakpoints ())
48cb2d85
VP
791 if (b->number == num)
792 return b;
793
43892fdf 794 return nullptr;
48cb2d85 795}
5c44784c 796
c906108c 797\f
adc36818 798
b775012e
LM
799/* Mark locations as "conditions have changed" in case the target supports
800 evaluating conditions on its side. */
801
802static void
803mark_breakpoint_modified (struct breakpoint *b)
804{
b775012e
LM
805 /* This is only meaningful if the target is
806 evaluating conditions and if the user has
807 opted for condition evaluation on the target's
808 side. */
809 if (gdb_evaluates_breakpoint_condition_p ()
810 || !target_supports_evaluation_of_breakpoint_conditions ())
811 return;
812
813 if (!is_breakpoint (b))
814 return;
815
40cb8ca5 816 for (bp_location *loc : b->locations ())
b775012e
LM
817 loc->condition_changed = condition_modified;
818}
819
820/* Mark location as "conditions have changed" in case the target supports
821 evaluating conditions on its side. */
822
823static void
824mark_breakpoint_location_modified (struct bp_location *loc)
825{
826 /* This is only meaningful if the target is
827 evaluating conditions and if the user has
828 opted for condition evaluation on the target's
829 side. */
830 if (gdb_evaluates_breakpoint_condition_p ()
831 || !target_supports_evaluation_of_breakpoint_conditions ())
832
833 return;
834
835 if (!is_breakpoint (loc->owner))
836 return;
837
838 loc->condition_changed = condition_modified;
839}
840
841/* Sets the condition-evaluation mode using the static global
842 condition_evaluation_mode. */
843
844static void
eb4c3f4a 845set_condition_evaluation_mode (const char *args, int from_tty,
b775012e
LM
846 struct cmd_list_element *c)
847{
b775012e
LM
848 const char *old_mode, *new_mode;
849
850 if ((condition_evaluation_mode_1 == condition_evaluation_target)
851 && !target_supports_evaluation_of_breakpoint_conditions ())
852 {
853 condition_evaluation_mode_1 = condition_evaluation_mode;
854 warning (_("Target does not support breakpoint condition evaluation.\n"
855 "Using host evaluation mode instead."));
856 return;
857 }
858
859 new_mode = translate_condition_evaluation_mode (condition_evaluation_mode_1);
860 old_mode = translate_condition_evaluation_mode (condition_evaluation_mode);
861
abf1152a
JK
862 /* Flip the switch. Flip it even if OLD_MODE == NEW_MODE as one of the
863 settings was "auto". */
864 condition_evaluation_mode = condition_evaluation_mode_1;
865
b775012e
LM
866 /* Only update the mode if the user picked a different one. */
867 if (new_mode != old_mode)
868 {
b775012e
LM
869 /* If the user switched to a different evaluation mode, we
870 need to synch the changes with the target as follows:
871
872 "host" -> "target": Send all (valid) conditions to the target.
873 "target" -> "host": Remove all the conditions from the target.
874 */
875
b775012e
LM
876 if (new_mode == condition_evaluation_target)
877 {
878 /* Mark everything modified and synch conditions with the
879 target. */
48d7020b 880 for (bp_location *loc : all_bp_locations ())
b775012e 881 mark_breakpoint_location_modified (loc);
24b21115 882 }
b775012e
LM
883 else
884 {
885 /* Manually mark non-duplicate locations to synch conditions
886 with the target. We do this to remove all the conditions the
887 target knows about. */
48d7020b 888 for (bp_location *loc : all_bp_locations ())
b775012e
LM
889 if (is_breakpoint (loc->owner) && loc->inserted)
890 loc->needs_update = 1;
891 }
892
893 /* Do the update. */
44702360 894 update_global_location_list (UGLL_MAY_INSERT);
b775012e
LM
895 }
896
897 return;
898}
899
900/* Shows the current mode of breakpoint condition evaluation. Explicitly shows
901 what "auto" is translating to. */
902
903static void
904show_condition_evaluation_mode (struct ui_file *file, int from_tty,
905 struct cmd_list_element *c, const char *value)
906{
907 if (condition_evaluation_mode == condition_evaluation_auto)
6cb06a8c
TT
908 gdb_printf (file,
909 _("Breakpoint condition evaluation "
910 "mode is %s (currently %s).\n"),
911 value,
912 breakpoint_condition_evaluation_mode ());
b775012e 913 else
6cb06a8c
TT
914 gdb_printf (file, _("Breakpoint condition evaluation mode is %s.\n"),
915 value);
b775012e
LM
916}
917
b5fa468f
TBA
918/* Parse COND_STRING in the context of LOC and set as the condition
919 expression of LOC. BP_NUM is the number of LOC's owner, LOC_NUM is
920 the number of LOC within its owner. In case of parsing error, mark
921 LOC as DISABLED_BY_COND. In case of success, unset DISABLED_BY_COND. */
922
923static void
924set_breakpoint_location_condition (const char *cond_string, bp_location *loc,
925 int bp_num, int loc_num)
926{
927 bool has_junk = false;
928 try
929 {
930 expression_up new_exp = parse_exp_1 (&cond_string, loc->address,
931 block_for_pc (loc->address), 0);
932 if (*cond_string != 0)
933 has_junk = true;
934 else
935 {
936 loc->cond = std::move (new_exp);
937 if (loc->disabled_by_cond && loc->enabled)
6cb06a8c
TT
938 gdb_printf (_("Breakpoint %d's condition is now valid at "
939 "location %d, enabling.\n"),
940 bp_num, loc_num);
b5fa468f
TBA
941
942 loc->disabled_by_cond = false;
943 }
944 }
945 catch (const gdb_exception_error &e)
946 {
947 if (loc->enabled)
948 {
949 /* Warn if a user-enabled location is now becoming disabled-by-cond.
950 BP_NUM is 0 if the breakpoint is being defined for the first
951 time using the "break ... if ..." command, and non-zero if
952 already defined. */
953 if (bp_num != 0)
954 warning (_("failed to validate condition at location %d.%d, "
955 "disabling:\n %s"), bp_num, loc_num, e.what ());
956 else
957 warning (_("failed to validate condition at location %d, "
958 "disabling:\n %s"), loc_num, e.what ());
959 }
960
961 loc->disabled_by_cond = true;
962 }
963
964 if (has_junk)
965 error (_("Garbage '%s' follows condition"), cond_string);
966}
967
adc36818 968void
7a26bd4d 969set_breakpoint_condition (struct breakpoint *b, const char *exp,
733d554a 970 int from_tty, bool force)
adc36818 971{
4c55e970 972 if (*exp == 0)
3a5c3e22 973 {
6f781ee3 974 b->cond_string.reset ();
3a5c3e22 975
4c55e970 976 if (is_watchpoint (b))
78319c15 977 static_cast<watchpoint *> (b)->cond_exp.reset ();
4c55e970
TBA
978 else
979 {
b5fa468f 980 int loc_num = 1;
40cb8ca5 981 for (bp_location *loc : b->locations ())
4c55e970
TBA
982 {
983 loc->cond.reset ();
b5fa468f 984 if (loc->disabled_by_cond && loc->enabled)
6cb06a8c
TT
985 gdb_printf (_("Breakpoint %d's condition is now valid at "
986 "location %d, enabling.\n"),
987 b->number, loc_num);
b5fa468f
TBA
988 loc->disabled_by_cond = false;
989 loc_num++;
4c55e970
TBA
990
991 /* No need to free the condition agent expression
992 bytecode (if we have one). We will handle this
993 when we go through update_global_location_list. */
994 }
995 }
1e620590 996
adc36818 997 if (from_tty)
6cb06a8c 998 gdb_printf (_("Breakpoint %d now unconditional.\n"), b->number);
adc36818
PM
999 }
1000 else
1001 {
adc36818
PM
1002 if (is_watchpoint (b))
1003 {
699bd4cf 1004 innermost_block_tracker tracker;
78319c15 1005 const char *arg = exp;
4c55e970 1006 expression_up new_exp = parse_exp_1 (&arg, 0, 0, 0, &tracker);
78319c15 1007 if (*arg != 0)
adc36818 1008 error (_("Junk at end of expression"));
78319c15 1009 watchpoint *w = static_cast<watchpoint *> (b);
4c55e970 1010 w->cond_exp = std::move (new_exp);
699bd4cf 1011 w->cond_exp_valid_block = tracker.block ();
adc36818
PM
1012 }
1013 else
1014 {
4c55e970
TBA
1015 /* Parse and set condition expressions. We make two passes.
1016 In the first, we parse the condition string to see if it
b5fa468f
TBA
1017 is valid in at least one location. If so, the condition
1018 would be accepted. So we go ahead and set the locations'
1019 conditions. In case no valid case is found, we throw
4c55e970
TBA
1020 the error and the condition string will be rejected.
1021 This two-pass approach is taken to avoid setting the
1022 state of locations in case of a reject. */
40cb8ca5 1023 for (bp_location *loc : b->locations ())
4c55e970 1024 {
b5fa468f
TBA
1025 try
1026 {
1027 const char *arg = exp;
1028 parse_exp_1 (&arg, loc->address,
1029 block_for_pc (loc->address), 0);
1030 if (*arg != 0)
1031 error (_("Junk at end of expression"));
1032 break;
1033 }
1034 catch (const gdb_exception_error &e)
1035 {
1036 /* Condition string is invalid. If this happens to
733d554a
TBA
1037 be the last loc, abandon (if not forced) or continue
1038 (if forced). */
1039 if (loc->next == nullptr && !force)
b5fa468f
TBA
1040 throw;
1041 }
4c55e970
TBA
1042 }
1043
b5fa468f 1044 /* If we reach here, the condition is valid at some locations. */
dda83cd7 1045 int loc_num = 1;
40cb8ca5
SM
1046 for (bp_location *loc : b->locations ())
1047 {
1048 set_breakpoint_location_condition (exp, loc, b->number, loc_num);
1049 loc_num++;
1050 }
adc36818 1051 }
1e620590
TBA
1052
1053 /* We know that the new condition parsed successfully. The
1054 condition string of the breakpoint can be safely updated. */
6f781ee3 1055 b->cond_string = make_unique_xstrdup (exp);
1e620590 1056 b->condition_not_parsed = 0;
adc36818 1057 }
b775012e
LM
1058 mark_breakpoint_modified (b);
1059
76727919 1060 gdb::observers::breakpoint_modified.notify (b);
adc36818
PM
1061}
1062
79aabb73
TBA
1063/* See breakpoint.h. */
1064
1065void
1066set_breakpoint_condition (int bpnum, const char *exp, int from_tty,
1067 bool force)
1068{
43892fdf 1069 for (breakpoint *b : all_breakpoints ())
79aabb73
TBA
1070 if (b->number == bpnum)
1071 {
1072 /* Check if this breakpoint has a "stop" method implemented in an
1073 extension language. This method and conditions entered into GDB
1074 from the CLI are mutually exclusive. */
1075 const struct extension_language_defn *extlang
1076 = get_breakpoint_cond_ext_lang (b, EXT_LANG_NONE);
1077
1078 if (extlang != NULL)
1079 {
1080 error (_("Only one stop condition allowed. There is currently"
1081 " a %s stop condition defined for this breakpoint."),
1082 ext_lang_capitalized_name (extlang));
1083 }
1084 set_breakpoint_condition (b, exp, from_tty, force);
1085
1086 if (is_breakpoint (b))
1087 update_global_location_list (UGLL_MAY_INSERT);
1088
1089 return;
1090 }
1091
1092 error (_("No breakpoint number %d."), bpnum);
1093}
1094
b1d4d8d1
TBA
1095/* The options for the "condition" command. */
1096
1097struct condition_command_opts
1098{
1099 /* For "-force". */
1100 bool force_condition = false;
1101};
1102
1103static const gdb::option::option_def condition_command_option_defs[] = {
1104
1105 gdb::option::flag_option_def<condition_command_opts> {
1106 "force",
1107 [] (condition_command_opts *opts) { return &opts->force_condition; },
1108 N_("Set the condition even if it is invalid for all current locations."),
1109 },
1110
1111};
1112
1113/* Create an option_def_group for the "condition" options, with
1114 CC_OPTS as context. */
1115
1116static inline gdb::option::option_def_group
1117make_condition_command_options_def_group (condition_command_opts *cc_opts)
1118{
1119 return {{condition_command_option_defs}, cc_opts};
1120}
1121
d55637df
TT
1122/* Completion for the "condition" command. */
1123
eb3ff9a5 1124static void
6f937416 1125condition_completer (struct cmd_list_element *cmd,
eb3ff9a5 1126 completion_tracker &tracker,
b1d4d8d1 1127 const char *text, const char * /*word*/)
d55637df 1128{
b1d4d8d1
TBA
1129 bool has_no_arguments = (*text == '\0');
1130 condition_command_opts cc_opts;
1131 const auto group = make_condition_command_options_def_group (&cc_opts);
1132 if (gdb::option::complete_options
1133 (tracker, &text, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR, group))
1134 return;
d55637df 1135
f1735a53 1136 text = skip_spaces (text);
b1d4d8d1 1137 const char *space = skip_to_space (text);
d55637df
TT
1138 if (*space == '\0')
1139 {
1140 int len;
d55637df
TT
1141
1142 if (text[0] == '$')
1143 {
b1d4d8d1 1144 tracker.advance_custom_word_point_by (1);
d55637df 1145 /* We don't support completion of history indices. */
eb3ff9a5
PA
1146 if (!isdigit (text[1]))
1147 complete_internalvar (tracker, &text[1]);
1148 return;
d55637df
TT
1149 }
1150
b1d4d8d1
TBA
1151 /* Suggest the "-force" flag if no arguments are given. If
1152 arguments were passed, they either already include the flag,
1153 or we are beyond the point of suggesting it because it's
1154 positionally the first argument. */
1155 if (has_no_arguments)
1156 gdb::option::complete_on_all_options (tracker, group);
1157
d55637df
TT
1158 /* We're completing the breakpoint number. */
1159 len = strlen (text);
1160
43892fdf 1161 for (breakpoint *b : all_breakpoints ())
58ce7251
SDJ
1162 {
1163 char number[50];
1164
1165 xsnprintf (number, sizeof (number), "%d", b->number);
1166
1167 if (strncmp (number, text, len) == 0)
b02f78f9 1168 tracker.add_completion (make_unique_xstrdup (number));
58ce7251 1169 }
d55637df 1170
eb3ff9a5 1171 return;
d55637df
TT
1172 }
1173
b1d4d8d1
TBA
1174 /* We're completing the expression part. Skip the breakpoint num. */
1175 const char *exp_start = skip_spaces (space);
1176 tracker.advance_custom_word_point_by (exp_start - text);
1177 text = exp_start;
1178 const char *word = advance_to_expression_complete_word_point (tracker, text);
eb3ff9a5 1179 expression_completer (cmd, tracker, text, word);
d55637df
TT
1180}
1181
c906108c
SS
1182/* condition N EXP -- set break condition of breakpoint N to EXP. */
1183
1184static void
0b39b52e 1185condition_command (const char *arg, int from_tty)
c906108c 1186{
0b39b52e 1187 const char *p;
52f0bd74 1188 int bnum;
c906108c
SS
1189
1190 if (arg == 0)
e2e0b3e5 1191 error_no_arg (_("breakpoint number"));
c906108c
SS
1192
1193 p = arg;
733d554a
TBA
1194
1195 /* Check if the "-force" flag was passed. */
b1d4d8d1
TBA
1196 condition_command_opts cc_opts;
1197 const auto group = make_condition_command_options_def_group (&cc_opts);
1198 gdb::option::process_options
1199 (&p, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR, group);
733d554a 1200
c906108c 1201 bnum = get_number (&p);
5c44784c 1202 if (bnum == 0)
8a3fe4f8 1203 error (_("Bad breakpoint argument: '%s'"), arg);
c906108c 1204
79aabb73 1205 set_breakpoint_condition (bnum, p, from_tty, cc_opts.force_condition);
c906108c
SS
1206}
1207
a7bdde9e
VP
1208/* Check that COMMAND do not contain commands that are suitable
1209 only for tracepoints and not suitable for ordinary breakpoints.
4a64f543
MS
1210 Throw if any such commands is found. */
1211
a7bdde9e
VP
1212static void
1213check_no_tracepoint_commands (struct command_line *commands)
1214{
1215 struct command_line *c;
cc59ec59 1216
a7bdde9e
VP
1217 for (c = commands; c; c = c->next)
1218 {
a7bdde9e 1219 if (c->control_type == while_stepping_control)
3e43a32a
MS
1220 error (_("The 'while-stepping' command can "
1221 "only be used for tracepoints"));
a7bdde9e 1222
12973681
TT
1223 check_no_tracepoint_commands (c->body_list_0.get ());
1224 check_no_tracepoint_commands (c->body_list_1.get ());
a7bdde9e
VP
1225
1226 /* Not that command parsing removes leading whitespace and comment
4a64f543 1227 lines and also empty lines. So, we only need to check for
a7bdde9e
VP
1228 command directly. */
1229 if (strstr (c->line, "collect ") == c->line)
1230 error (_("The 'collect' command can only be used for tracepoints"));
1231
51661e93
VP
1232 if (strstr (c->line, "teval ") == c->line)
1233 error (_("The 'teval' command can only be used for tracepoints"));
a7bdde9e
VP
1234 }
1235}
1236
098f1272 1237struct longjmp_breakpoint : public momentary_breakpoint
3b0871f4 1238{
73063f51
TT
1239 using momentary_breakpoint::momentary_breakpoint;
1240
c1fc2657 1241 ~longjmp_breakpoint () override;
3b0871f4
SM
1242};
1243
d77f58be
SS
1244/* Encapsulate tests for different types of tracepoints. */
1245
3b0871f4
SM
1246static bool
1247is_tracepoint_type (bptype type)
d9b3f62e
PA
1248{
1249 return (type == bp_tracepoint
1250 || type == bp_fast_tracepoint
7b572efb
TT
1251 || type == bp_static_tracepoint
1252 || type == bp_static_marker_tracepoint);
d9b3f62e
PA
1253}
1254
f2478a7e
SM
1255/* See breakpoint.h. */
1256
1257bool
d77f58be 1258is_tracepoint (const struct breakpoint *b)
a7bdde9e 1259{
d9b3f62e 1260 return is_tracepoint_type (b->type);
a7bdde9e 1261}
d9b3f62e 1262
a5e364af
SM
1263/* Factory function to create an appropriate instance of breakpoint given
1264 TYPE. */
1265
3b003a61 1266template<typename... Arg>
ff733ec2 1267static std::unique_ptr<base_breakpoint>
3b003a61
PA
1268new_breakpoint_from_type (struct gdbarch *gdbarch, bptype type,
1269 Arg&&... args)
a5e364af 1270{
ff733ec2 1271 base_breakpoint *b;
a5e364af 1272
098f1272
TT
1273 switch (type)
1274 {
1275 case bp_breakpoint:
1276 case bp_hardware_breakpoint:
3b003a61
PA
1277 b = new ordinary_breakpoint (gdbarch, type,
1278 std::forward<Arg> (args)...);
098f1272
TT
1279 break;
1280
1281 case bp_fast_tracepoint:
1282 case bp_static_tracepoint:
1283 case bp_tracepoint:
3b003a61
PA
1284 b = new tracepoint (gdbarch, type,
1285 std::forward<Arg> (args)...);
098f1272
TT
1286 break;
1287
6d7a8c56 1288 case bp_static_marker_tracepoint:
3b003a61
PA
1289 b = new static_marker_tracepoint (gdbarch, type,
1290 std::forward<Arg> (args)...);
6d7a8c56
TT
1291 break;
1292
098f1272 1293 case bp_dprintf:
3b003a61
PA
1294 b = new dprintf_breakpoint (gdbarch, type,
1295 std::forward<Arg> (args)...);
098f1272
TT
1296 break;
1297
098f1272
TT
1298 default:
1299 gdb_assert_not_reached ("invalid type");
1300 }
a5e364af 1301
ff733ec2 1302 return std::unique_ptr<base_breakpoint> (b);
a5e364af
SM
1303}
1304
e5dd4106 1305/* A helper function that validates that COMMANDS are valid for a
95a42b64
TT
1306 breakpoint. This function will throw an exception if a problem is
1307 found. */
48cb2d85 1308
95a42b64
TT
1309static void
1310validate_commands_for_breakpoint (struct breakpoint *b,
1311 struct command_line *commands)
48cb2d85 1312{
d77f58be 1313 if (is_tracepoint (b))
a7bdde9e 1314 {
c9a6ce02 1315 struct tracepoint *t = (struct tracepoint *) b;
a7bdde9e
VP
1316 struct command_line *c;
1317 struct command_line *while_stepping = 0;
c9a6ce02
PA
1318
1319 /* Reset the while-stepping step count. The previous commands
dda83cd7
SM
1320 might have included a while-stepping action, while the new
1321 ones might not. */
c9a6ce02
PA
1322 t->step_count = 0;
1323
1324 /* We need to verify that each top-level element of commands is
1325 valid for tracepoints, that there's at most one
1326 while-stepping element, and that the while-stepping's body
1327 has valid tracing commands excluding nested while-stepping.
1328 We also need to validate the tracepoint action line in the
1329 context of the tracepoint --- validate_actionline actually
1330 has side effects, like setting the tracepoint's
1331 while-stepping STEP_COUNT, in addition to checking if the
1332 collect/teval actions parse and make sense in the
1333 tracepoint's context. */
a7bdde9e
VP
1334 for (c = commands; c; c = c->next)
1335 {
a7bdde9e
VP
1336 if (c->control_type == while_stepping_control)
1337 {
1338 if (b->type == bp_fast_tracepoint)
3e43a32a
MS
1339 error (_("The 'while-stepping' command "
1340 "cannot be used for fast tracepoint"));
7b572efb
TT
1341 else if (b->type == bp_static_tracepoint
1342 || b->type == bp_static_marker_tracepoint)
3e43a32a
MS
1343 error (_("The 'while-stepping' command "
1344 "cannot be used for static tracepoint"));
a7bdde9e
VP
1345
1346 if (while_stepping)
3e43a32a
MS
1347 error (_("The 'while-stepping' command "
1348 "can be used only once"));
a7bdde9e
VP
1349 else
1350 while_stepping = c;
1351 }
c9a6ce02
PA
1352
1353 validate_actionline (c->line, b);
a7bdde9e
VP
1354 }
1355 if (while_stepping)
1356 {
1357 struct command_line *c2;
1358
12973681
TT
1359 gdb_assert (while_stepping->body_list_1 == nullptr);
1360 c2 = while_stepping->body_list_0.get ();
a7bdde9e
VP
1361 for (; c2; c2 = c2->next)
1362 {
a7bdde9e
VP
1363 if (c2->control_type == while_stepping_control)
1364 error (_("The 'while-stepping' command cannot be nested"));
1365 }
1366 }
1367 }
1368 else
1369 {
1370 check_no_tracepoint_commands (commands);
1371 }
95a42b64
TT
1372}
1373
0fb4aa4b
PA
1374/* Return a vector of all the static tracepoints set at ADDR. The
1375 caller is responsible for releasing the vector. */
1376
f51e0e20 1377std::vector<breakpoint *>
0fb4aa4b
PA
1378static_tracepoints_here (CORE_ADDR addr)
1379{
f51e0e20 1380 std::vector<breakpoint *> found;
0fb4aa4b 1381
43892fdf 1382 for (breakpoint *b : all_breakpoints ())
7b572efb
TT
1383 if (b->type == bp_static_tracepoint
1384 || b->type == bp_static_marker_tracepoint)
0fb4aa4b 1385 {
40cb8ca5 1386 for (bp_location *loc : b->locations ())
0fb4aa4b 1387 if (loc->address == addr)
f51e0e20 1388 found.push_back (b);
0fb4aa4b
PA
1389 }
1390
1391 return found;
1392}
1393
95a42b64 1394/* Set the command list of B to COMMANDS. If breakpoint is tracepoint,
4a64f543 1395 validate that only allowed commands are included. */
95a42b64
TT
1396
1397void
4a64f543 1398breakpoint_set_commands (struct breakpoint *b,
12973681 1399 counted_command_line &&commands)
95a42b64 1400{
93921405 1401 validate_commands_for_breakpoint (b, commands.get ());
a7bdde9e 1402
d1b0a7bf 1403 b->commands = std::move (commands);
76727919 1404 gdb::observers::breakpoint_modified.notify (b);
48cb2d85
VP
1405}
1406
45a43567
TT
1407/* Set the internal `silent' flag on the breakpoint. Note that this
1408 is not the same as the "silent" that may appear in the breakpoint's
1409 commands. */
1410
1411void
1412breakpoint_set_silent (struct breakpoint *b, int silent)
1413{
1414 int old_silent = b->silent;
1415
1416 b->silent = silent;
1417 if (old_silent != silent)
76727919 1418 gdb::observers::breakpoint_modified.notify (b);
45a43567
TT
1419}
1420
1421/* Set the thread for this breakpoint. If THREAD is -1, make the
1422 breakpoint work for any thread. */
1423
1424void
1425breakpoint_set_thread (struct breakpoint *b, int thread)
1426{
1427 int old_thread = b->thread;
1428
1429 b->thread = thread;
1430 if (old_thread != thread)
76727919 1431 gdb::observers::breakpoint_modified.notify (b);
45a43567
TT
1432}
1433
1434/* Set the task for this breakpoint. If TASK is 0, make the
1435 breakpoint work for any task. */
1436
1437void
1438breakpoint_set_task (struct breakpoint *b, int task)
1439{
1440 int old_task = b->task;
1441
1442 b->task = task;
1443 if (old_task != task)
76727919 1444 gdb::observers::breakpoint_modified.notify (b);
45a43567
TT
1445}
1446
95a42b64 1447static void
896b6bda 1448commands_command_1 (const char *arg, int from_tty,
4a64f543 1449 struct command_line *control)
95a42b64 1450{
d1b0a7bf 1451 counted_command_line cmd;
999700cd
PW
1452 /* cmd_read will be true once we have read cmd. Note that cmd might still be
1453 NULL after the call to read_command_lines if the user provides an empty
1454 list of command by just typing "end". */
1455 bool cmd_read = false;
95a42b64 1456
896b6bda
PA
1457 std::string new_arg;
1458
95a42b64
TT
1459 if (arg == NULL || !*arg)
1460 {
da1df1db 1461 /* Argument not explicitly given. Synthesize it. */
86b17b60 1462 if (breakpoint_count - prev_breakpoint_count > 1)
896b6bda
PA
1463 new_arg = string_printf ("%d-%d", prev_breakpoint_count + 1,
1464 breakpoint_count);
95a42b64 1465 else if (breakpoint_count > 0)
896b6bda 1466 new_arg = string_printf ("%d", breakpoint_count);
48649e1b 1467 }
da1df1db
TBA
1468 else
1469 {
1470 /* Create a copy of ARG. This is needed because the "commands"
1471 command may be coming from a script. In that case, the read
1472 line buffer is going to be overwritten in the lambda of
1473 'map_breakpoint_numbers' below when reading the next line
1474 before we are are done parsing the breakpoint numbers. */
1475 new_arg = arg;
1476 }
1477 arg = new_arg.c_str ();
48649e1b
TT
1478
1479 map_breakpoint_numbers
1480 (arg, [&] (breakpoint *b)
1481 {
999700cd 1482 if (!cmd_read)
48649e1b 1483 {
999700cd 1484 gdb_assert (cmd == NULL);
48649e1b 1485 if (control != NULL)
12973681 1486 cmd = control->body_list_0;
48649e1b
TT
1487 else
1488 {
81b1e71c
TT
1489 std::string str
1490 = string_printf (_("Type commands for breakpoint(s) "
1491 "%s, one per line."),
1492 arg);
48649e1b 1493
60b3cef2
TT
1494 auto do_validate = [=] (const char *line)
1495 {
1496 validate_actionline (line, b);
1497 };
1498 gdb::function_view<void (const char *)> validator;
1499 if (is_tracepoint (b))
1500 validator = do_validate;
1501
1502 cmd = read_command_lines (str.c_str (), from_tty, 1, validator);
48649e1b 1503 }
999700cd 1504 cmd_read = true;
48649e1b
TT
1505 }
1506
1507 /* If a breakpoint was on the list more than once, we don't need to
1508 do anything. */
1509 if (b->commands != cmd)
1510 {
d1b0a7bf 1511 validate_commands_for_breakpoint (b, cmd.get ());
48649e1b 1512 b->commands = cmd;
76727919 1513 gdb::observers::breakpoint_modified.notify (b);
48649e1b
TT
1514 }
1515 });
95a42b64
TT
1516}
1517
1518static void
0b39b52e 1519commands_command (const char *arg, int from_tty)
95a42b64
TT
1520{
1521 commands_command_1 (arg, from_tty, NULL);
c906108c 1522}
40c03ae8
EZ
1523
1524/* Like commands_command, but instead of reading the commands from
1525 input stream, takes them from an already parsed command structure.
1526
1527 This is used by cli-script.c to DTRT with breakpoint commands
1528 that are part of if and while bodies. */
1529enum command_control_type
896b6bda 1530commands_from_control_command (const char *arg, struct command_line *cmd)
40c03ae8 1531{
95a42b64
TT
1532 commands_command_1 (arg, 0, cmd);
1533 return simple_control;
40c03ae8 1534}
876fa593
JK
1535
1536/* Return non-zero if BL->TARGET_INFO contains valid information. */
1537
1538static int
1539bp_location_has_shadow (struct bp_location *bl)
1540{
1541 if (bl->loc_type != bp_loc_software_breakpoint)
1542 return 0;
1543 if (!bl->inserted)
1544 return 0;
1545 if (bl->target_info.shadow_len == 0)
e5dd4106 1546 /* BL isn't valid, or doesn't shadow memory. */
876fa593
JK
1547 return 0;
1548 return 1;
1549}
1550
9d497a19
PA
1551/* Update BUF, which is LEN bytes read from the target address
1552 MEMADDR, by replacing a memory breakpoint with its shadowed
1553 contents.
1554
1555 If READBUF is not NULL, this buffer must not overlap with the of
1556 the breakpoint location's shadow_contents buffer. Otherwise, a
1557 failed assertion internal error will be raised. */
1558
1559static void
1560one_breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1561 const gdb_byte *writebuf_org,
1562 ULONGEST memaddr, LONGEST len,
1563 struct bp_target_info *target_info,
1564 struct gdbarch *gdbarch)
1565{
1566 /* Now do full processing of the found relevant range of elements. */
1567 CORE_ADDR bp_addr = 0;
1568 int bp_size = 0;
1569 int bptoffset = 0;
1570
1571 if (!breakpoint_address_match (target_info->placed_address_space, 0,
1572 current_program_space->aspace, 0))
1573 {
1574 /* The breakpoint is inserted in a different address space. */
1575 return;
1576 }
1577
1578 /* Addresses and length of the part of the breakpoint that
1579 we need to copy. */
1580 bp_addr = target_info->placed_address;
1581 bp_size = target_info->shadow_len;
1582
1583 if (bp_addr + bp_size <= memaddr)
1584 {
1585 /* The breakpoint is entirely before the chunk of memory we are
1586 reading. */
1587 return;
1588 }
1589
1590 if (bp_addr >= memaddr + len)
1591 {
1592 /* The breakpoint is entirely after the chunk of memory we are
1593 reading. */
1594 return;
1595 }
1596
1597 /* Offset within shadow_contents. */
1598 if (bp_addr < memaddr)
1599 {
1600 /* Only copy the second part of the breakpoint. */
1601 bp_size -= memaddr - bp_addr;
1602 bptoffset = memaddr - bp_addr;
1603 bp_addr = memaddr;
1604 }
1605
1606 if (bp_addr + bp_size > memaddr + len)
1607 {
1608 /* Only copy the first part of the breakpoint. */
1609 bp_size -= (bp_addr + bp_size) - (memaddr + len);
1610 }
1611
1612 if (readbuf != NULL)
1613 {
1614 /* Verify that the readbuf buffer does not overlap with the
1615 shadow_contents buffer. */
1616 gdb_assert (target_info->shadow_contents >= readbuf + len
1617 || readbuf >= (target_info->shadow_contents
1618 + target_info->shadow_len));
1619
1620 /* Update the read buffer with this inserted breakpoint's
1621 shadow. */
1622 memcpy (readbuf + bp_addr - memaddr,
1623 target_info->shadow_contents + bptoffset, bp_size);
1624 }
1625 else
1626 {
1627 const unsigned char *bp;
0d5ed153
MR
1628 CORE_ADDR addr = target_info->reqstd_address;
1629 int placed_size;
9d497a19
PA
1630
1631 /* Update the shadow with what we want to write to memory. */
1632 memcpy (target_info->shadow_contents + bptoffset,
1633 writebuf_org + bp_addr - memaddr, bp_size);
1634
1635 /* Determine appropriate breakpoint contents and size for this
1636 address. */
0d5ed153 1637 bp = gdbarch_breakpoint_from_pc (gdbarch, &addr, &placed_size);
9d497a19
PA
1638
1639 /* Update the final write buffer with this inserted
1640 breakpoint's INSN. */
1641 memcpy (writebuf + bp_addr - memaddr, bp + bptoffset, bp_size);
1642 }
1643}
1644
8defab1a 1645/* Update BUF, which is LEN bytes read from the target address MEMADDR,
876fa593
JK
1646 by replacing any memory breakpoints with their shadowed contents.
1647
35c63cd8
JB
1648 If READBUF is not NULL, this buffer must not overlap with any of
1649 the breakpoint location's shadow_contents buffers. Otherwise,
1650 a failed assertion internal error will be raised.
1651
876fa593 1652 The range of shadowed area by each bp_location is:
f5336ca5
PA
1653 bl->address - bp_locations_placed_address_before_address_max
1654 up to bl->address + bp_locations_shadow_len_after_address_max
876fa593
JK
1655 The range we were requested to resolve shadows for is:
1656 memaddr ... memaddr + len
1657 Thus the safe cutoff boundaries for performance optimization are
35df4500 1658 memaddr + len <= (bl->address
f5336ca5 1659 - bp_locations_placed_address_before_address_max)
876fa593 1660 and:
f5336ca5 1661 bl->address + bp_locations_shadow_len_after_address_max <= memaddr */
c906108c 1662
8defab1a 1663void
f0ba3972
PA
1664breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1665 const gdb_byte *writebuf_org,
1666 ULONGEST memaddr, LONGEST len)
c906108c 1667{
4a64f543
MS
1668 /* Left boundary, right boundary and median element of our binary
1669 search. */
876fa593
JK
1670 unsigned bc_l, bc_r, bc;
1671
4a64f543
MS
1672 /* Find BC_L which is a leftmost element which may affect BUF
1673 content. It is safe to report lower value but a failure to
1674 report higher one. */
876fa593
JK
1675
1676 bc_l = 0;
5d51cd5d 1677 bc_r = bp_locations.size ();
876fa593
JK
1678 while (bc_l + 1 < bc_r)
1679 {
35df4500 1680 struct bp_location *bl;
876fa593
JK
1681
1682 bc = (bc_l + bc_r) / 2;
f5336ca5 1683 bl = bp_locations[bc];
876fa593 1684
4a64f543
MS
1685 /* Check first BL->ADDRESS will not overflow due to the added
1686 constant. Then advance the left boundary only if we are sure
1687 the BC element can in no way affect the BUF content (MEMADDR
1688 to MEMADDR + LEN range).
876fa593 1689
f5336ca5 1690 Use the BP_LOCATIONS_SHADOW_LEN_AFTER_ADDRESS_MAX safety
4a64f543
MS
1691 offset so that we cannot miss a breakpoint with its shadow
1692 range tail still reaching MEMADDR. */
c5aa993b 1693
f5336ca5 1694 if ((bl->address + bp_locations_shadow_len_after_address_max
35df4500 1695 >= bl->address)
f5336ca5 1696 && (bl->address + bp_locations_shadow_len_after_address_max
35df4500 1697 <= memaddr))
876fa593
JK
1698 bc_l = bc;
1699 else
1700 bc_r = bc;
1701 }
1702
128070bb
PA
1703 /* Due to the binary search above, we need to make sure we pick the
1704 first location that's at BC_L's address. E.g., if there are
1705 multiple locations at the same address, BC_L may end up pointing
1706 at a duplicate location, and miss the "master"/"inserted"
1707 location. Say, given locations L1, L2 and L3 at addresses A and
1708 B:
1709
1710 L1@A, L2@A, L3@B, ...
1711
1712 BC_L could end up pointing at location L2, while the "master"
1713 location could be L1. Since the `loc->inserted' flag is only set
1714 on "master" locations, we'd forget to restore the shadow of L1
1715 and L2. */
1716 while (bc_l > 0
f5336ca5 1717 && bp_locations[bc_l]->address == bp_locations[bc_l - 1]->address)
128070bb
PA
1718 bc_l--;
1719
876fa593
JK
1720 /* Now do full processing of the found relevant range of elements. */
1721
5d51cd5d 1722 for (bc = bc_l; bc < bp_locations.size (); bc++)
01add95b
SM
1723 {
1724 struct bp_location *bl = bp_locations[bc];
876fa593 1725
01add95b
SM
1726 /* bp_location array has BL->OWNER always non-NULL. */
1727 if (bl->owner->type == bp_none)
1728 warning (_("reading through apparently deleted breakpoint #%d?"),
1729 bl->owner->number);
ffce0d52 1730
01add95b
SM
1731 /* Performance optimization: any further element can no longer affect BUF
1732 content. */
876fa593 1733
01add95b
SM
1734 if (bl->address >= bp_locations_placed_address_before_address_max
1735 && (memaddr + len
1736 <= (bl->address
1737 - bp_locations_placed_address_before_address_max)))
1738 break;
876fa593 1739
01add95b
SM
1740 if (!bp_location_has_shadow (bl))
1741 continue;
6c95b8df 1742
01add95b
SM
1743 one_breakpoint_xfer_memory (readbuf, writebuf, writebuf_org,
1744 memaddr, len, &bl->target_info, bl->gdbarch);
1745 }
c906108c 1746}
9d497a19 1747
f2478a7e 1748/* See breakpoint.h. */
b775012e 1749
f2478a7e 1750bool
b775012e
LM
1751is_breakpoint (const struct breakpoint *bpt)
1752{
1753 return (bpt->type == bp_breakpoint
e7e0cddf
SS
1754 || bpt->type == bp_hardware_breakpoint
1755 || bpt->type == bp_dprintf);
b775012e
LM
1756}
1757
60e1c644
PA
1758/* Return true if BPT is of any hardware watchpoint kind. */
1759
f2478a7e 1760static bool
d77f58be 1761is_hardware_watchpoint (const struct breakpoint *bpt)
a5606eee
VP
1762{
1763 return (bpt->type == bp_hardware_watchpoint
1764 || bpt->type == bp_read_watchpoint
1765 || bpt->type == bp_access_watchpoint);
1766}
7270d8f2 1767
f2478a7e 1768/* See breakpoint.h. */
60e1c644 1769
f2478a7e 1770bool
d77f58be 1771is_watchpoint (const struct breakpoint *bpt)
60e1c644
PA
1772{
1773 return (is_hardware_watchpoint (bpt)
1774 || bpt->type == bp_watchpoint);
1775}
1776
3a5c3e22
PA
1777/* Returns true if the current thread and its running state are safe
1778 to evaluate or update watchpoint B. Watchpoints on local
1779 expressions need to be evaluated in the context of the thread that
1780 was current when the watchpoint was created, and, that thread needs
1781 to be stopped to be able to select the correct frame context.
1782 Watchpoints on global expressions can be evaluated on any thread,
1783 and in any state. It is presently left to the target allowing
1784 memory accesses when threads are running. */
f6bc2008
PA
1785
1786static int
3a5c3e22 1787watchpoint_in_thread_scope (struct watchpoint *b)
f6bc2008 1788{
c1fc2657 1789 return (b->pspace == current_program_space
d7e15655
TT
1790 && (b->watchpoint_thread == null_ptid
1791 || (inferior_ptid == b->watchpoint_thread
611841bb 1792 && !inferior_thread ()->executing ())));
f6bc2008
PA
1793}
1794
d0fb5eae
JK
1795/* Set watchpoint B to disp_del_at_next_stop, even including its possible
1796 associated bp_watchpoint_scope breakpoint. */
1797
1798static void
3a5c3e22 1799watchpoint_del_at_next_stop (struct watchpoint *w)
d0fb5eae 1800{
c1fc2657 1801 if (w->related_breakpoint != w)
d0fb5eae 1802 {
c1fc2657
SM
1803 gdb_assert (w->related_breakpoint->type == bp_watchpoint_scope);
1804 gdb_assert (w->related_breakpoint->related_breakpoint == w);
1805 w->related_breakpoint->disposition = disp_del_at_next_stop;
1806 w->related_breakpoint->related_breakpoint = w->related_breakpoint;
1807 w->related_breakpoint = w;
d0fb5eae 1808 }
c1fc2657 1809 w->disposition = disp_del_at_next_stop;
d0fb5eae
JK
1810}
1811
bb9d5f81
PP
1812/* Extract a bitfield value from value VAL using the bit parameters contained in
1813 watchpoint W. */
1814
1815static struct value *
1816extract_bitfield_from_watchpoint_value (struct watchpoint *w, struct value *val)
1817{
1818 struct value *bit_val;
1819
1820 if (val == NULL)
1821 return NULL;
1822
1823 bit_val = allocate_value (value_type (val));
1824
1825 unpack_value_bitfield (bit_val,
1826 w->val_bitpos,
1827 w->val_bitsize,
50888e42 1828 value_contents_for_printing (val).data (),
bb9d5f81
PP
1829 value_offset (val),
1830 val);
1831
1832 return bit_val;
1833}
1834
6e14e441
PA
1835/* Allocate a dummy location and add it to B. This is required
1836 because bpstat_stop_status requires a location to be able to report
1837 stops. */
c6d81124
PA
1838
1839static void
6e14e441
PA
1840add_dummy_location (struct breakpoint *b,
1841 struct program_space *pspace)
c6d81124 1842{
6e14e441 1843 gdb_assert (b->loc == NULL);
c6d81124 1844
6e14e441 1845 b->loc = new bp_location (b, bp_loc_other);
c6d81124 1846 b->loc->pspace = pspace;
c6d81124
PA
1847}
1848
567e1b4e
JB
1849/* Assuming that B is a watchpoint:
1850 - Reparse watchpoint expression, if REPARSE is non-zero
a5606eee 1851 - Evaluate expression and store the result in B->val
567e1b4e
JB
1852 - Evaluate the condition if there is one, and store the result
1853 in b->loc->cond.
a5606eee
VP
1854 - Update the list of values that must be watched in B->loc.
1855
4a64f543
MS
1856 If the watchpoint disposition is disp_del_at_next_stop, then do
1857 nothing. If this is local watchpoint that is out of scope, delete
1858 it.
1859
1860 Even with `set breakpoint always-inserted on' the watchpoints are
1861 removed + inserted on each stop here. Normal breakpoints must
1862 never be removed because they might be missed by a running thread
1863 when debugging in non-stop mode. On the other hand, hardware
1864 watchpoints (is_hardware_watchpoint; processed here) are specific
1865 to each LWP since they are stored in each LWP's hardware debug
1866 registers. Therefore, such LWP must be stopped first in order to
1867 be able to modify its hardware watchpoints.
1868
1869 Hardware watchpoints must be reset exactly once after being
1870 presented to the user. It cannot be done sooner, because it would
1871 reset the data used to present the watchpoint hit to the user. And
1872 it must not be done later because it could display the same single
1873 watchpoint hit during multiple GDB stops. Note that the latter is
1874 relevant only to the hardware watchpoint types bp_read_watchpoint
1875 and bp_access_watchpoint. False hit by bp_hardware_watchpoint is
1876 not user-visible - its hit is suppressed if the memory content has
1877 not changed.
1878
1879 The following constraints influence the location where we can reset
1880 hardware watchpoints:
1881
1882 * target_stopped_by_watchpoint and target_stopped_data_address are
1883 called several times when GDB stops.
1884
1885 [linux]
1886 * Multiple hardware watchpoints can be hit at the same time,
1887 causing GDB to stop. GDB only presents one hardware watchpoint
1888 hit at a time as the reason for stopping, and all the other hits
1889 are presented later, one after the other, each time the user
1890 requests the execution to be resumed. Execution is not resumed
1891 for the threads still having pending hit event stored in
1892 LWP_INFO->STATUS. While the watchpoint is already removed from
1893 the inferior on the first stop the thread hit event is kept being
1894 reported from its cached value by linux_nat_stopped_data_address
1895 until the real thread resume happens after the watchpoint gets
1896 presented and thus its LWP_INFO->STATUS gets reset.
1897
1898 Therefore the hardware watchpoint hit can get safely reset on the
1899 watchpoint removal from inferior. */
a79d3c27 1900
b40ce68a 1901static void
3a5c3e22 1902update_watchpoint (struct watchpoint *b, int reparse)
7270d8f2 1903{
a5606eee 1904 int within_current_scope;
a5606eee 1905 struct frame_id saved_frame_id;
66076460 1906 int frame_saved;
a5606eee 1907
f6bc2008
PA
1908 /* If this is a local watchpoint, we only want to check if the
1909 watchpoint frame is in scope if the current thread is the thread
1910 that was used to create the watchpoint. */
1911 if (!watchpoint_in_thread_scope (b))
1912 return;
1913
c1fc2657 1914 if (b->disposition == disp_del_at_next_stop)
a5606eee
VP
1915 return;
1916
66076460 1917 frame_saved = 0;
a5606eee
VP
1918
1919 /* Determine if the watchpoint is within scope. */
1920 if (b->exp_valid_block == NULL)
1921 within_current_scope = 1;
1922 else
1923 {
b5db5dfc
UW
1924 struct frame_info *fi = get_current_frame ();
1925 struct gdbarch *frame_arch = get_frame_arch (fi);
1926 CORE_ADDR frame_pc = get_frame_pc (fi);
1927
c9cf6e20
MG
1928 /* If we're at a point where the stack has been destroyed
1929 (e.g. in a function epilogue), unwinding may not work
1930 properly. Do not attempt to recreate locations at this
b5db5dfc 1931 point. See similar comments in watchpoint_check. */
c9cf6e20 1932 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
b5db5dfc 1933 return;
66076460
DJ
1934
1935 /* Save the current frame's ID so we can restore it after
dda83cd7 1936 evaluating the watchpoint expression on its own frame. */
66076460 1937 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
dda83cd7
SM
1938 took a frame parameter, so that we didn't have to change the
1939 selected frame. */
66076460
DJ
1940 frame_saved = 1;
1941 saved_frame_id = get_frame_id (get_selected_frame (NULL));
1942
a5606eee
VP
1943 fi = frame_find_by_id (b->watchpoint_frame);
1944 within_current_scope = (fi != NULL);
1945 if (within_current_scope)
1946 select_frame (fi);
1947 }
1948
b5db5dfc
UW
1949 /* We don't free locations. They are stored in the bp_location array
1950 and update_global_location_list will eventually delete them and
1951 remove breakpoints if needed. */
c1fc2657 1952 b->loc = NULL;
b5db5dfc 1953
a5606eee
VP
1954 if (within_current_scope && reparse)
1955 {
bbc13ae3 1956 const char *s;
d63d0675 1957
4d01a485 1958 b->exp.reset ();
a4c50be3
TT
1959 s = (b->exp_string_reparse
1960 ? b->exp_string_reparse.get ()
1961 : b->exp_string.get ());
1bb9788d 1962 b->exp = parse_exp_1 (&s, 0, b->exp_valid_block, 0);
a5606eee
VP
1963 /* If the meaning of expression itself changed, the old value is
1964 no longer relevant. We don't want to report a watchpoint hit
1965 to the user when the old value and the new value may actually
1966 be completely different objects. */
fa4727a6 1967 b->val = NULL;
4c1d86d9 1968 b->val_valid = false;
60e1c644
PA
1969
1970 /* Note that unlike with breakpoints, the watchpoint's condition
1971 expression is stored in the breakpoint object, not in the
1972 locations (re)created below. */
c1fc2657 1973 if (b->cond_string != NULL)
60e1c644 1974 {
4d01a485 1975 b->cond_exp.reset ();
60e1c644 1976
6f781ee3 1977 s = b->cond_string.get ();
1bb9788d 1978 b->cond_exp = parse_exp_1 (&s, 0, b->cond_exp_valid_block, 0);
60e1c644 1979 }
a5606eee 1980 }
a5606eee
VP
1981
1982 /* If we failed to parse the expression, for example because
1983 it refers to a global variable in a not-yet-loaded shared library,
1984 don't try to insert watchpoint. We don't automatically delete
1985 such watchpoint, though, since failure to parse expression
1986 is different from out-of-scope watchpoint. */
55f6301a 1987 if (!target_has_execution ())
2d134ed3
PA
1988 {
1989 /* Without execution, memory can't change. No use to try and
1990 set watchpoint locations. The watchpoint will be reset when
1991 the target gains execution, through breakpoint_re_set. */
e8369a73
AB
1992 if (!can_use_hw_watchpoints)
1993 {
c47614fe 1994 if (b->works_in_software_mode ())
c1fc2657 1995 b->type = bp_watchpoint;
e8369a73 1996 else
638aa5a1
AB
1997 error (_("Can't set read/access watchpoint when "
1998 "hardware watchpoints are disabled."));
e8369a73 1999 }
2d134ed3
PA
2000 }
2001 else if (within_current_scope && b->exp)
a5606eee 2002 {
a6535de1 2003 std::vector<value_ref_ptr> val_chain;
8d49165d 2004 struct value *v, *result;
2d134ed3 2005 struct program_space *frame_pspace;
a5606eee 2006
1eaebe02 2007 fetch_subexp_value (b->exp.get (), b->exp->op.get (), &v, &result,
413403fc 2008 &val_chain, false);
a5606eee 2009
a5606eee
VP
2010 /* Avoid setting b->val if it's already set. The meaning of
2011 b->val is 'the last value' user saw, and we should update
2012 it only if we reported that last value to user. As it
9c06b0b4
TJB
2013 happens, the code that reports it updates b->val directly.
2014 We don't keep track of the memory value for masked
2015 watchpoints. */
c1fc2657 2016 if (!b->val_valid && !is_masked_watchpoint (b))
fa4727a6 2017 {
bb9d5f81 2018 if (b->val_bitsize != 0)
850645cf
TT
2019 v = extract_bitfield_from_watchpoint_value (b, v);
2020 b->val = release_value (v);
4c1d86d9 2021 b->val_valid = true;
fa4727a6 2022 }
a5606eee 2023
2d134ed3
PA
2024 frame_pspace = get_frame_program_space (get_selected_frame (NULL));
2025
a5606eee 2026 /* Look at each value on the value chain. */
a6535de1
TT
2027 gdb_assert (!val_chain.empty ());
2028 for (const value_ref_ptr &iter : val_chain)
a5606eee 2029 {
a6535de1
TT
2030 v = iter.get ();
2031
a5606eee
VP
2032 /* If it's a memory location, and GDB actually needed
2033 its contents to evaluate the expression, then we
fa4727a6
DJ
2034 must watch it. If the first value returned is
2035 still lazy, that means an error occurred reading it;
2036 watch it anyway in case it becomes readable. */
a5606eee 2037 if (VALUE_LVAL (v) == lval_memory
a6535de1 2038 && (v == val_chain[0] || ! value_lazy (v)))
a5606eee
VP
2039 {
2040 struct type *vtype = check_typedef (value_type (v));
7270d8f2 2041
a5606eee
VP
2042 /* We only watch structs and arrays if user asked
2043 for it explicitly, never if they just happen to
2044 appear in the middle of some value chain. */
fa4727a6 2045 if (v == result
78134374
SM
2046 || (vtype->code () != TYPE_CODE_STRUCT
2047 && vtype->code () != TYPE_CODE_ARRAY))
a5606eee
VP
2048 {
2049 CORE_ADDR addr;
f486487f 2050 enum target_hw_bp_type type;
a5606eee 2051 struct bp_location *loc, **tmp;
bb9d5f81
PP
2052 int bitpos = 0, bitsize = 0;
2053
2054 if (value_bitsize (v) != 0)
2055 {
2056 /* Extract the bit parameters out from the bitfield
2057 sub-expression. */
2058 bitpos = value_bitpos (v);
2059 bitsize = value_bitsize (v);
2060 }
2061 else if (v == result && b->val_bitsize != 0)
2062 {
2063 /* If VAL_BITSIZE != 0 then RESULT is actually a bitfield
2064 lvalue whose bit parameters are saved in the fields
2065 VAL_BITPOS and VAL_BITSIZE. */
2066 bitpos = b->val_bitpos;
2067 bitsize = b->val_bitsize;
2068 }
a5606eee 2069
42ae5230 2070 addr = value_address (v);
bb9d5f81
PP
2071 if (bitsize != 0)
2072 {
2073 /* Skip the bytes that don't contain the bitfield. */
2074 addr += bitpos / 8;
2075 }
2076
a5606eee 2077 type = hw_write;
c1fc2657 2078 if (b->type == bp_read_watchpoint)
a5606eee 2079 type = hw_read;
c1fc2657 2080 else if (b->type == bp_access_watchpoint)
a5606eee 2081 type = hw_access;
3a5c3e22 2082
ee89d0a4 2083 loc = b->allocate_location ();
c1fc2657 2084 for (tmp = &(b->loc); *tmp != NULL; tmp = &((*tmp)->next))
a5606eee
VP
2085 ;
2086 *tmp = loc;
8ee511af 2087 loc->gdbarch = value_type (v)->arch ();
6c95b8df
PA
2088
2089 loc->pspace = frame_pspace;
f17d9474 2090 loc->address = address_significant (loc->gdbarch, addr);
bb9d5f81
PP
2091
2092 if (bitsize != 0)
2093 {
2094 /* Just cover the bytes that make up the bitfield. */
2095 loc->length = ((bitpos % 8) + bitsize + 7) / 8;
2096 }
2097 else
2098 loc->length = TYPE_LENGTH (value_type (v));
2099
a5606eee
VP
2100 loc->watchpoint_type = type;
2101 }
2102 }
9fa40276
TJB
2103 }
2104
2105 /* Change the type of breakpoint between hardware assisted or
2106 an ordinary watchpoint depending on the hardware support
2107 and free hardware slots. REPARSE is set when the inferior
2108 is started. */
a9634178 2109 if (reparse)
9fa40276 2110 {
e09342b5 2111 int reg_cnt;
9fa40276 2112 enum bp_loc_type loc_type;
a5606eee 2113
a9634178 2114 reg_cnt = can_use_hardware_watchpoint (val_chain);
e09342b5
TJB
2115
2116 if (reg_cnt)
9fa40276
TJB
2117 {
2118 int i, target_resources_ok, other_type_used;
a1398e0c 2119 enum bptype type;
9fa40276 2120
a9634178
TJB
2121 /* Use an exact watchpoint when there's only one memory region to be
2122 watched, and only one debug register is needed to watch it. */
2123 b->exact = target_exact_watchpoints && reg_cnt == 1;
2124
9fa40276 2125 /* We need to determine how many resources are already
e09342b5
TJB
2126 used for all other hardware watchpoints plus this one
2127 to see if we still have enough resources to also fit
a1398e0c
PA
2128 this watchpoint in as well. */
2129
2130 /* If this is a software watchpoint, we try to turn it
2131 to a hardware one -- count resources as if B was of
2132 hardware watchpoint type. */
c1fc2657 2133 type = b->type;
a1398e0c
PA
2134 if (type == bp_watchpoint)
2135 type = bp_hardware_watchpoint;
2136
2137 /* This watchpoint may or may not have been placed on
2138 the list yet at this point (it won't be in the list
2139 if we're trying to create it for the first time,
2140 through watch_command), so always account for it
2141 manually. */
2142
2143 /* Count resources used by all watchpoints except B. */
c1fc2657 2144 i = hw_watchpoint_used_count_others (b, type, &other_type_used);
a1398e0c
PA
2145
2146 /* Add in the resources needed for B. */
c1fc2657 2147 i += hw_watchpoint_use_count (b);
a1398e0c
PA
2148
2149 target_resources_ok
2150 = target_can_use_hardware_watchpoint (type, i, other_type_used);
e09342b5 2151 if (target_resources_ok <= 0)
a9634178 2152 {
5a61e176 2153 bool sw_mode = b->works_in_software_mode ();
9c06b0b4
TJB
2154
2155 if (target_resources_ok == 0 && !sw_mode)
a9634178
TJB
2156 error (_("Target does not support this type of "
2157 "hardware watchpoint."));
9c06b0b4
TJB
2158 else if (target_resources_ok < 0 && !sw_mode)
2159 error (_("There are not enough available hardware "
2160 "resources for this watchpoint."));
a1398e0c
PA
2161
2162 /* Downgrade to software watchpoint. */
c1fc2657 2163 b->type = bp_watchpoint;
a1398e0c
PA
2164 }
2165 else
2166 {
2167 /* If this was a software watchpoint, we've just
2168 found we have enough resources to turn it to a
2169 hardware watchpoint. Otherwise, this is a
2170 nop. */
c1fc2657 2171 b->type = type;
a9634178 2172 }
9fa40276 2173 }
c47614fe 2174 else if (!b->works_in_software_mode ())
638aa5a1
AB
2175 {
2176 if (!can_use_hw_watchpoints)
2177 error (_("Can't set read/access watchpoint when "
2178 "hardware watchpoints are disabled."));
2179 else
2180 error (_("Expression cannot be implemented with "
2181 "read/access watchpoint."));
2182 }
9fa40276 2183 else
c1fc2657 2184 b->type = bp_watchpoint;
9fa40276 2185
6e14e441 2186 loc_type = (b->type == bp_watchpoint? bp_loc_software_watchpoint
9fa40276 2187 : bp_loc_hardware_watchpoint);
40cb8ca5 2188 for (bp_location *bl : b->locations ())
9fa40276
TJB
2189 bl->loc_type = loc_type;
2190 }
2191
c7437ca6
PA
2192 /* If a software watchpoint is not watching any memory, then the
2193 above left it without any location set up. But,
2194 bpstat_stop_status requires a location to be able to report
2195 stops, so make sure there's at least a dummy one. */
c1fc2657 2196 if (b->type == bp_watchpoint && b->loc == NULL)
6e14e441 2197 add_dummy_location (b, frame_pspace);
a5606eee
VP
2198 }
2199 else if (!within_current_scope)
7270d8f2 2200 {
6cb06a8c 2201 gdb_printf (_("\
ac74f770
MS
2202Watchpoint %d deleted because the program has left the block\n\
2203in which its expression is valid.\n"),
6cb06a8c 2204 b->number);
d0fb5eae 2205 watchpoint_del_at_next_stop (b);
7270d8f2 2206 }
a5606eee
VP
2207
2208 /* Restore the selected frame. */
66076460
DJ
2209 if (frame_saved)
2210 select_frame (frame_find_by_id (saved_frame_id));
7270d8f2
OF
2211}
2212
a5606eee 2213
74960c60 2214/* Returns 1 iff breakpoint location should be
1e4d1764
YQ
2215 inserted in the inferior. We don't differentiate the type of BL's owner
2216 (breakpoint vs. tracepoint), although insert_location in tracepoint's
2217 breakpoint_ops is not defined, because in insert_bp_location,
2218 tracepoint's insert_location will not be called. */
74960c60 2219static int
35df4500 2220should_be_inserted (struct bp_location *bl)
74960c60 2221{
35df4500 2222 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
74960c60
VP
2223 return 0;
2224
35df4500 2225 if (bl->owner->disposition == disp_del_at_next_stop)
74960c60
VP
2226 return 0;
2227
b5fa468f
TBA
2228 if (!bl->enabled || bl->disabled_by_cond
2229 || bl->shlib_disabled || bl->duplicate)
74960c60
VP
2230 return 0;
2231
f8eba3c6
TT
2232 if (user_breakpoint_p (bl->owner) && bl->pspace->executing_startup)
2233 return 0;
2234
56710373
PA
2235 /* This is set for example, when we're attached to the parent of a
2236 vfork, and have detached from the child. The child is running
2237 free, and we expect it to do an exec or exit, at which point the
2238 OS makes the parent schedulable again (and the target reports
2239 that the vfork is done). Until the child is done with the shared
2240 memory region, do not insert breakpoints in the parent, otherwise
2241 the child could still trip on the parent's breakpoints. Since
2242 the parent is blocked anyway, it won't miss any breakpoint. */
35df4500 2243 if (bl->pspace->breakpoints_not_allowed)
56710373
PA
2244 return 0;
2245
31e77af2 2246 /* Don't insert a breakpoint if we're trying to step past its
21edc42f
YQ
2247 location, except if the breakpoint is a single-step breakpoint,
2248 and the breakpoint's thread is the thread which is stepping past
2249 a breakpoint. */
31e77af2
PA
2250 if ((bl->loc_type == bp_loc_software_breakpoint
2251 || bl->loc_type == bp_loc_hardware_breakpoint)
2252 && stepping_past_instruction_at (bl->pspace->aspace,
21edc42f
YQ
2253 bl->address)
2254 /* The single-step breakpoint may be inserted at the location
2255 we're trying to step if the instruction branches to itself.
2256 However, the instruction won't be executed at all and it may
2257 break the semantics of the instruction, for example, the
2258 instruction is a conditional branch or updates some flags.
2259 We can't fix it unless GDB is able to emulate the instruction
2260 or switch to displaced stepping. */
2261 && !(bl->owner->type == bp_single_step
2262 && thread_is_stepping_over_breakpoint (bl->owner->thread)))
e558d7c1 2263 {
1eb8556f
SM
2264 infrun_debug_printf ("skipping breakpoint: stepping past insn at: %s",
2265 paddress (bl->gdbarch, bl->address));
e558d7c1
PA
2266 return 0;
2267 }
31e77af2 2268
963f9c80
PA
2269 /* Don't insert watchpoints if we're trying to step past the
2270 instruction that triggered one. */
2271 if ((bl->loc_type == bp_loc_hardware_watchpoint)
2272 && stepping_past_nonsteppable_watchpoint ())
2273 {
1eb8556f 2274 infrun_debug_printf ("stepping past non-steppable watchpoint. "
ae1f4d2d 2275 "skipping watchpoint at %s:%d",
1eb8556f 2276 paddress (bl->gdbarch, bl->address), bl->length);
963f9c80
PA
2277 return 0;
2278 }
2279
74960c60
VP
2280 return 1;
2281}
2282
934709f0
PW
2283/* Same as should_be_inserted but does the check assuming
2284 that the location is not duplicated. */
2285
2286static int
2287unduplicated_should_be_inserted (struct bp_location *bl)
2288{
2289 int result;
2290 const int save_duplicate = bl->duplicate;
2291
2292 bl->duplicate = 0;
2293 result = should_be_inserted (bl);
2294 bl->duplicate = save_duplicate;
2295 return result;
2296}
2297
b775012e
LM
2298/* Parses a conditional described by an expression COND into an
2299 agent expression bytecode suitable for evaluation
2300 by the bytecode interpreter. Return NULL if there was
2301 any error during parsing. */
2302
833177a4 2303static agent_expr_up
b775012e
LM
2304parse_cond_to_aexpr (CORE_ADDR scope, struct expression *cond)
2305{
833177a4 2306 if (cond == NULL)
b775012e
LM
2307 return NULL;
2308
833177a4
PA
2309 agent_expr_up aexpr;
2310
b775012e
LM
2311 /* We don't want to stop processing, so catch any errors
2312 that may show up. */
a70b8144 2313 try
b775012e 2314 {
036e657b 2315 aexpr = gen_eval_for_expr (scope, cond);
b775012e
LM
2316 }
2317
230d2906 2318 catch (const gdb_exception_error &ex)
b775012e
LM
2319 {
2320 /* If we got here, it means the condition could not be parsed to a valid
2321 bytecode expression and thus can't be evaluated on the target's side.
2322 It's no use iterating through the conditions. */
b775012e
LM
2323 }
2324
2325 /* We have a valid agent expression. */
2326 return aexpr;
2327}
2328
2329/* Based on location BL, create a list of breakpoint conditions to be
2330 passed on to the target. If we have duplicated locations with different
2331 conditions, we will add such conditions to the list. The idea is that the
2332 target will evaluate the list of conditions and will only notify GDB when
2333 one of them is true. */
2334
2335static void
2336build_target_condition_list (struct bp_location *bl)
2337{
b775012e
LM
2338 int null_condition_or_parse_error = 0;
2339 int modified = bl->needs_update;
b775012e 2340
8b4f3082 2341 /* Release conditions left over from a previous insert. */
3cde5c42 2342 bl->target_info.conditions.clear ();
8b4f3082 2343
b775012e
LM
2344 /* This is only meaningful if the target is
2345 evaluating conditions and if the user has
2346 opted for condition evaluation on the target's
2347 side. */
2348 if (gdb_evaluates_breakpoint_condition_p ()
2349 || !target_supports_evaluation_of_breakpoint_conditions ())
2350 return;
2351
e0d9a270
SM
2352 auto loc_range = all_bp_locations_at_addr (bl->address);
2353
b775012e 2354 /* Do a first pass to check for locations with no assigned
7f32a4d5
PA
2355 conditions or conditions that fail to parse to a valid agent
2356 expression bytecode. If any of these happen, then it's no use to
2357 send conditions to the target since this location will always
2358 trigger and generate a response back to GDB. Note we consider
2359 all locations at the same address irrespective of type, i.e.,
2360 even if the locations aren't considered duplicates (e.g.,
2361 software breakpoint and hardware breakpoint at the same
2362 address). */
e0d9a270 2363 for (bp_location *loc : loc_range)
b775012e 2364 {
b775012e
LM
2365 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2366 {
2367 if (modified)
2368 {
b775012e
LM
2369 /* Re-parse the conditions since something changed. In that
2370 case we already freed the condition bytecodes (see
2371 force_breakpoint_reinsertion). We just
2372 need to parse the condition to bytecodes again. */
833177a4
PA
2373 loc->cond_bytecode = parse_cond_to_aexpr (bl->address,
2374 loc->cond.get ());
b775012e
LM
2375 }
2376
2377 /* If we have a NULL bytecode expression, it means something
2378 went wrong or we have a null condition expression. */
2379 if (!loc->cond_bytecode)
2380 {
2381 null_condition_or_parse_error = 1;
2382 break;
2383 }
2384 }
2385 }
2386
2387 /* If any of these happened, it means we will have to evaluate the conditions
2388 for the location's address on gdb's side. It is no use keeping bytecodes
2389 for all the other duplicate locations, thus we free all of them here.
2390
2391 This is so we have a finer control over which locations' conditions are
2392 being evaluated by GDB or the remote stub. */
2393 if (null_condition_or_parse_error)
2394 {
e0d9a270 2395 for (bp_location *loc : loc_range)
b775012e 2396 {
b775012e
LM
2397 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2398 {
2399 /* Only go as far as the first NULL bytecode is
2400 located. */
2401 if (!loc->cond_bytecode)
2402 return;
2403
833177a4 2404 loc->cond_bytecode.reset ();
b775012e
LM
2405 }
2406 }
2407 }
2408
7f32a4d5
PA
2409 /* No NULL conditions or failed bytecode generation. Build a
2410 condition list for this location's address. If we have software
2411 and hardware locations at the same address, they aren't
2412 considered duplicates, but we still marge all the conditions
2413 anyway, as it's simpler, and doesn't really make a practical
2414 difference. */
e0d9a270
SM
2415 for (bp_location *loc : loc_range)
2416 if (loc->cond
2417 && is_breakpoint (loc->owner)
2418 && loc->pspace->num == bl->pspace->num
2419 && loc->owner->enable_state == bp_enabled
2420 && loc->enabled
2421 && !loc->disabled_by_cond)
2422 {
2423 /* Add the condition to the vector. This will be used later
2424 to send the conditions to the target. */
2425 bl->target_info.conditions.push_back (loc->cond_bytecode.get ());
2426 }
b775012e
LM
2427
2428 return;
2429}
2430
d3ce09f5
SS
2431/* Parses a command described by string CMD into an agent expression
2432 bytecode suitable for evaluation by the bytecode interpreter.
2433 Return NULL if there was any error during parsing. */
2434
833177a4 2435static agent_expr_up
d3ce09f5
SS
2436parse_cmd_to_aexpr (CORE_ADDR scope, char *cmd)
2437{
bbc13ae3
KS
2438 const char *cmdrest;
2439 const char *format_start, *format_end;
d3ce09f5
SS
2440 struct gdbarch *gdbarch = get_current_arch ();
2441
833177a4 2442 if (cmd == NULL)
d3ce09f5
SS
2443 return NULL;
2444
2445 cmdrest = cmd;
2446
2447 if (*cmdrest == ',')
2448 ++cmdrest;
f1735a53 2449 cmdrest = skip_spaces (cmdrest);
d3ce09f5
SS
2450
2451 if (*cmdrest++ != '"')
2452 error (_("No format string following the location"));
2453
2454 format_start = cmdrest;
2455
8e481c3b 2456 format_pieces fpieces (&cmdrest);
d3ce09f5
SS
2457
2458 format_end = cmdrest;
2459
2460 if (*cmdrest++ != '"')
2461 error (_("Bad format string, non-terminated '\"'."));
2462
f1735a53 2463 cmdrest = skip_spaces (cmdrest);
d3ce09f5
SS
2464
2465 if (!(*cmdrest == ',' || *cmdrest == '\0'))
2466 error (_("Invalid argument syntax"));
2467
2468 if (*cmdrest == ',')
2469 cmdrest++;
f1735a53 2470 cmdrest = skip_spaces (cmdrest);
d3ce09f5
SS
2471
2472 /* For each argument, make an expression. */
2473
8e481c3b 2474 std::vector<struct expression *> argvec;
d3ce09f5
SS
2475 while (*cmdrest != '\0')
2476 {
bbc13ae3 2477 const char *cmd1;
d3ce09f5
SS
2478
2479 cmd1 = cmdrest;
4d01a485 2480 expression_up expr = parse_exp_1 (&cmd1, scope, block_for_pc (scope), 1);
8e481c3b 2481 argvec.push_back (expr.release ());
d3ce09f5
SS
2482 cmdrest = cmd1;
2483 if (*cmdrest == ',')
2484 ++cmdrest;
2485 }
2486
833177a4
PA
2487 agent_expr_up aexpr;
2488
d3ce09f5
SS
2489 /* We don't want to stop processing, so catch any errors
2490 that may show up. */
a70b8144 2491 try
d3ce09f5 2492 {
036e657b
JB
2493 aexpr = gen_printf (scope, gdbarch, 0, 0,
2494 format_start, format_end - format_start,
8e481c3b 2495 argvec.size (), argvec.data ());
d3ce09f5 2496 }
230d2906 2497 catch (const gdb_exception_error &ex)
d3ce09f5
SS
2498 {
2499 /* If we got here, it means the command could not be parsed to a valid
2500 bytecode expression and thus can't be evaluated on the target's side.
2501 It's no use iterating through the other commands. */
d3ce09f5 2502 }
492d29ea 2503
d3ce09f5
SS
2504 /* We have a valid agent expression, return it. */
2505 return aexpr;
2506}
2507
2508/* Based on location BL, create a list of breakpoint commands to be
2509 passed on to the target. If we have duplicated locations with
2510 different commands, we will add any such to the list. */
2511
2512static void
2513build_target_command_list (struct bp_location *bl)
2514{
d3ce09f5
SS
2515 int null_command_or_parse_error = 0;
2516 int modified = bl->needs_update;
d3ce09f5 2517
3cde5c42
PA
2518 /* Clear commands left over from a previous insert. */
2519 bl->target_info.tcommands.clear ();
8b4f3082 2520
41fac0cf 2521 if (!target_can_run_breakpoint_commands ())
d3ce09f5
SS
2522 return;
2523
41fac0cf
PA
2524 /* For now, limit to agent-style dprintf breakpoints. */
2525 if (dprintf_style != dprintf_style_agent)
d3ce09f5
SS
2526 return;
2527
e0d9a270
SM
2528 auto loc_range = all_bp_locations_at_addr (bl->address);
2529
7f32a4d5
PA
2530 /* For now, if we have any location at the same address that isn't a
2531 dprintf, don't install the target-side commands, as that would
2532 make the breakpoint not be reported to the core, and we'd lose
41fac0cf 2533 control. */
e0d9a270
SM
2534 for (bp_location *loc : loc_range)
2535 if (is_breakpoint (loc->owner)
2536 && loc->pspace->num == bl->pspace->num
2537 && loc->owner->type != bp_dprintf)
2538 return;
41fac0cf 2539
d3ce09f5
SS
2540 /* Do a first pass to check for locations with no assigned
2541 conditions or conditions that fail to parse to a valid agent expression
2542 bytecode. If any of these happen, then it's no use to send conditions
2543 to the target since this location will always trigger and generate a
2544 response back to GDB. */
e0d9a270 2545 for (bp_location *loc : loc_range)
d3ce09f5 2546 {
d3ce09f5
SS
2547 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2548 {
2549 if (modified)
2550 {
d3ce09f5
SS
2551 /* Re-parse the commands since something changed. In that
2552 case we already freed the command bytecodes (see
2553 force_breakpoint_reinsertion). We just
2554 need to parse the command to bytecodes again. */
833177a4
PA
2555 loc->cmd_bytecode
2556 = parse_cmd_to_aexpr (bl->address,
6f781ee3 2557 loc->owner->extra_string.get ());
d3ce09f5
SS
2558 }
2559
2560 /* If we have a NULL bytecode expression, it means something
2561 went wrong or we have a null command expression. */
2562 if (!loc->cmd_bytecode)
2563 {
2564 null_command_or_parse_error = 1;
2565 break;
2566 }
2567 }
2568 }
2569
2570 /* If anything failed, then we're not doing target-side commands,
2571 and so clean up. */
2572 if (null_command_or_parse_error)
2573 {
e0d9a270
SM
2574 for (bp_location *loc : loc_range)
2575 if (is_breakpoint (loc->owner)
2576 && loc->pspace->num == bl->pspace->num)
2577 {
2578 /* Only go as far as the first NULL bytecode is
2579 located. */
2580 if (loc->cmd_bytecode == NULL)
2581 return;
d3ce09f5 2582
e0d9a270
SM
2583 loc->cmd_bytecode.reset ();
2584 }
d3ce09f5
SS
2585 }
2586
7f32a4d5
PA
2587 /* No NULL commands or failed bytecode generation. Build a command
2588 list for all duplicate locations at this location's address.
2589 Note that here we must care for whether the breakpoint location
2590 types are considered duplicates, otherwise, say, if we have a
2591 software and hardware location at the same address, the target
2592 could end up running the commands twice. For the moment, we only
2593 support targets-side commands with dprintf, but it doesn't hurt
2594 to be pedantically correct in case that changes. */
e0d9a270
SM
2595 for (bp_location *loc : loc_range)
2596 if (breakpoint_locations_match (bl, loc)
2597 && loc->owner->extra_string
2598 && is_breakpoint (loc->owner)
2599 && loc->pspace->num == bl->pspace->num
2600 && loc->owner->enable_state == bp_enabled
2601 && loc->enabled
2602 && !loc->disabled_by_cond)
2603 {
2604 /* Add the command to the vector. This will be used later
2605 to send the commands to the target. */
2606 bl->target_info.tcommands.push_back (loc->cmd_bytecode.get ());
2607 }
d3ce09f5
SS
2608
2609 bl->target_info.persist = 0;
2610 /* Maybe flag this location as persistent. */
2611 if (bl->owner->type == bp_dprintf && disconnected_dprintf)
2612 bl->target_info.persist = 1;
2613}
2614
833b7ab5
YQ
2615/* Return the kind of breakpoint on address *ADDR. Get the kind
2616 of breakpoint according to ADDR except single-step breakpoint.
2617 Get the kind of single-step breakpoint according to the current
2618 registers state. */
cd6c3b4f
YQ
2619
2620static int
2621breakpoint_kind (struct bp_location *bl, CORE_ADDR *addr)
2622{
833b7ab5
YQ
2623 if (bl->owner->type == bp_single_step)
2624 {
2625 struct thread_info *thr = find_thread_global_id (bl->owner->thread);
2626 struct regcache *regcache;
2627
00431a78 2628 regcache = get_thread_regcache (thr);
833b7ab5
YQ
2629
2630 return gdbarch_breakpoint_kind_from_current_state (bl->gdbarch,
2631 regcache, addr);
2632 }
2633 else
2634 return gdbarch_breakpoint_kind_from_pc (bl->gdbarch, addr);
cd6c3b4f
YQ
2635}
2636
35df4500
TJB
2637/* Insert a low-level "breakpoint" of some type. BL is the breakpoint
2638 location. Any error messages are printed to TMP_ERROR_STREAM; and
3fbb6ffa 2639 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
c30eee59
TJB
2640 Returns 0 for success, 1 if the bp_location type is not supported or
2641 -1 for failure.
879bfdc2 2642
4a64f543
MS
2643 NOTE drow/2003-09-09: This routine could be broken down to an
2644 object-style method for each breakpoint or catchpoint type. */
26bb91f3 2645static int
35df4500 2646insert_bp_location (struct bp_location *bl,
26bb91f3 2647 struct ui_file *tmp_error_stream,
3fbb6ffa 2648 int *disabled_breaks,
dd61ec5c
MW
2649 int *hw_breakpoint_error,
2650 int *hw_bp_error_explained_already)
879bfdc2 2651{
cc06b668 2652 gdb_exception bp_excpt;
879bfdc2 2653
b775012e 2654 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
879bfdc2
DJ
2655 return 0;
2656
35c63cd8
JB
2657 /* Note we don't initialize bl->target_info, as that wipes out
2658 the breakpoint location's shadow_contents if the breakpoint
2659 is still inserted at that location. This in turn breaks
2660 target_read_memory which depends on these buffers when
2661 a memory read is requested at the breakpoint location:
2662 Once the target_info has been wiped, we fail to see that
2663 we have a breakpoint inserted at that address and thus
2664 read the breakpoint instead of returning the data saved in
2665 the breakpoint location's shadow contents. */
0d5ed153 2666 bl->target_info.reqstd_address = bl->address;
35df4500 2667 bl->target_info.placed_address_space = bl->pspace->aspace;
f1310107 2668 bl->target_info.length = bl->length;
8181d85f 2669
b775012e
LM
2670 /* When working with target-side conditions, we must pass all the conditions
2671 for the same breakpoint address down to the target since GDB will not
2672 insert those locations. With a list of breakpoint conditions, the target
2673 can decide when to stop and notify GDB. */
2674
2675 if (is_breakpoint (bl->owner))
2676 {
2677 build_target_condition_list (bl);
d3ce09f5
SS
2678 build_target_command_list (bl);
2679 /* Reset the modification marker. */
b775012e
LM
2680 bl->needs_update = 0;
2681 }
2682
7f32a4d5
PA
2683 /* If "set breakpoint auto-hw" is "on" and a software breakpoint was
2684 set at a read-only address, then a breakpoint location will have
2685 been changed to hardware breakpoint before we get here. If it is
2686 "off" however, error out before actually trying to insert the
2687 breakpoint, with a nicer error message. */
35df4500 2688 if (bl->loc_type == bp_loc_software_breakpoint
7f32a4d5 2689 && !automatic_hardware_breakpoints)
879bfdc2 2690 {
7f32a4d5 2691 mem_region *mr = lookup_mem_region (bl->address);
cc59ec59 2692
7f32a4d5
PA
2693 if (mr != nullptr && mr->attrib.mode != MEM_RW)
2694 {
6cb06a8c
TT
2695 gdb_printf (tmp_error_stream,
2696 _("Cannot insert breakpoint %d.\n"
2697 "Cannot set software breakpoint "
2698 "at read-only address %s\n"),
2699 bl->owner->number,
2700 paddress (bl->gdbarch, bl->address));
7f32a4d5 2701 return 1;
765dc015 2702 }
7f32a4d5
PA
2703 }
2704
2705 if (bl->loc_type == bp_loc_software_breakpoint
2706 || bl->loc_type == bp_loc_hardware_breakpoint)
2707 {
879bfdc2
DJ
2708 /* First check to see if we have to handle an overlay. */
2709 if (overlay_debugging == ovly_off
35df4500
TJB
2710 || bl->section == NULL
2711 || !(section_is_overlay (bl->section)))
879bfdc2
DJ
2712 {
2713 /* No overlay handling: just set the breakpoint. */
a70b8144 2714 try
dd61ec5c 2715 {
0000e5cc
PA
2716 int val;
2717
c47614fe 2718 val = bl->owner->insert_location (bl);
0000e5cc 2719 if (val)
688fca4f 2720 bp_excpt = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
dd61ec5c 2721 }
94aeb44b 2722 catch (gdb_exception &e)
dd61ec5c 2723 {
94aeb44b 2724 bp_excpt = std::move (e);
dd61ec5c 2725 }
879bfdc2
DJ
2726 }
2727 else
2728 {
4a64f543 2729 /* This breakpoint is in an overlay section.
879bfdc2
DJ
2730 Shall we set a breakpoint at the LMA? */
2731 if (!overlay_events_enabled)
2732 {
2733 /* Yes -- overlay event support is not active,
2734 so we must try to set a breakpoint at the LMA.
2735 This will not work for a hardware breakpoint. */
35df4500 2736 if (bl->loc_type == bp_loc_hardware_breakpoint)
8a3fe4f8 2737 warning (_("hardware breakpoint %d not supported in overlay!"),
35df4500 2738 bl->owner->number);
879bfdc2
DJ
2739 else
2740 {
35df4500
TJB
2741 CORE_ADDR addr = overlay_unmapped_address (bl->address,
2742 bl->section);
879bfdc2 2743 /* Set a software (trap) breakpoint at the LMA. */
35df4500 2744 bl->overlay_target_info = bl->target_info;
0d5ed153 2745 bl->overlay_target_info.reqstd_address = addr;
0000e5cc
PA
2746
2747 /* No overlay handling: just set the breakpoint. */
a70b8144 2748 try
0000e5cc
PA
2749 {
2750 int val;
2751
579c6ad9 2752 bl->overlay_target_info.kind
cd6c3b4f
YQ
2753 = breakpoint_kind (bl, &addr);
2754 bl->overlay_target_info.placed_address = addr;
0000e5cc
PA
2755 val = target_insert_breakpoint (bl->gdbarch,
2756 &bl->overlay_target_info);
2757 if (val)
688fca4f
PA
2758 bp_excpt
2759 = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
0000e5cc 2760 }
94aeb44b 2761 catch (gdb_exception &e)
0000e5cc 2762 {
94aeb44b 2763 bp_excpt = std::move (e);
0000e5cc
PA
2764 }
2765
688fca4f 2766 if (bp_excpt.reason != 0)
6cb06a8c
TT
2767 gdb_printf (tmp_error_stream,
2768 "Overlay breakpoint %d "
2769 "failed: in ROM?\n",
2770 bl->owner->number);
879bfdc2
DJ
2771 }
2772 }
2773 /* Shall we set a breakpoint at the VMA? */
35df4500 2774 if (section_is_mapped (bl->section))
879bfdc2
DJ
2775 {
2776 /* Yes. This overlay section is mapped into memory. */
a70b8144 2777 try
dda83cd7 2778 {
0000e5cc
PA
2779 int val;
2780
c47614fe 2781 val = bl->owner->insert_location (bl);
0000e5cc 2782 if (val)
688fca4f 2783 bp_excpt = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
dda83cd7 2784 }
94aeb44b 2785 catch (gdb_exception &e)
dda83cd7 2786 {
94aeb44b 2787 bp_excpt = std::move (e);
dda83cd7 2788 }
879bfdc2
DJ
2789 }
2790 else
2791 {
2792 /* No. This breakpoint will not be inserted.
2793 No error, but do not mark the bp as 'inserted'. */
2794 return 0;
2795 }
2796 }
2797
688fca4f 2798 if (bp_excpt.reason != 0)
879bfdc2
DJ
2799 {
2800 /* Can't set the breakpoint. */
0000e5cc 2801
80dc83fd
AB
2802 /* If the target has closed then it will have deleted any
2803 breakpoints inserted within the target inferior, as a result
2804 any further attempts to interact with the breakpoint objects
2805 is not possible. Just rethrow the error. */
2806 if (bp_excpt.error == TARGET_CLOSE_ERROR)
2807 throw bp_excpt;
2808 gdb_assert (bl->owner != nullptr);
2809
0000e5cc
PA
2810 /* In some cases, we might not be able to insert a
2811 breakpoint in a shared library that has already been
2812 removed, but we have not yet processed the shlib unload
2813 event. Unfortunately, some targets that implement
076855f9
PA
2814 breakpoint insertion themselves can't tell why the
2815 breakpoint insertion failed (e.g., the remote target
2816 doesn't define error codes), so we must treat generic
2817 errors as memory errors. */
688fca4f
PA
2818 if (bp_excpt.reason == RETURN_ERROR
2819 && (bp_excpt.error == GENERIC_ERROR
2820 || bp_excpt.error == MEMORY_ERROR)
076855f9 2821 && bl->loc_type == bp_loc_software_breakpoint
08351840 2822 && (solib_name_from_address (bl->pspace, bl->address)
d03de421
PA
2823 || shared_objfile_contains_address_p (bl->pspace,
2824 bl->address)))
879bfdc2 2825 {
4a64f543 2826 /* See also: disable_breakpoints_in_shlibs. */
35df4500 2827 bl->shlib_disabled = 1;
76727919 2828 gdb::observers::breakpoint_modified.notify (bl->owner);
3fbb6ffa
TJB
2829 if (!*disabled_breaks)
2830 {
6cb06a8c
TT
2831 gdb_printf (tmp_error_stream,
2832 "Cannot insert breakpoint %d.\n",
2833 bl->owner->number);
2834 gdb_printf (tmp_error_stream,
2835 "Temporarily disabling shared "
2836 "library breakpoints:\n");
3fbb6ffa
TJB
2837 }
2838 *disabled_breaks = 1;
6cb06a8c
TT
2839 gdb_printf (tmp_error_stream,
2840 "breakpoint #%d\n", bl->owner->number);
0000e5cc 2841 return 0;
879bfdc2
DJ
2842 }
2843 else
879bfdc2 2844 {
35df4500 2845 if (bl->loc_type == bp_loc_hardware_breakpoint)
879bfdc2 2846 {
0000e5cc 2847 *hw_breakpoint_error = 1;
688fca4f 2848 *hw_bp_error_explained_already = bp_excpt.message != NULL;
6cb06a8c
TT
2849 gdb_printf (tmp_error_stream,
2850 "Cannot insert hardware breakpoint %d%s",
2851 bl->owner->number,
2852 bp_excpt.message ? ":" : ".\n");
dda83cd7 2853 if (bp_excpt.message != NULL)
6cb06a8c
TT
2854 gdb_printf (tmp_error_stream, "%s.\n",
2855 bp_excpt.what ());
879bfdc2
DJ
2856 }
2857 else
2858 {
688fca4f 2859 if (bp_excpt.message == NULL)
0000e5cc 2860 {
1ccbe998 2861 std::string message
0000e5cc
PA
2862 = memory_error_message (TARGET_XFER_E_IO,
2863 bl->gdbarch, bl->address);
0000e5cc 2864
6cb06a8c
TT
2865 gdb_printf (tmp_error_stream,
2866 "Cannot insert breakpoint %d.\n"
2867 "%s\n",
2868 bl->owner->number, message.c_str ());
0000e5cc
PA
2869 }
2870 else
2871 {
6cb06a8c
TT
2872 gdb_printf (tmp_error_stream,
2873 "Cannot insert breakpoint %d: %s\n",
2874 bl->owner->number,
2875 bp_excpt.what ());
0000e5cc 2876 }
879bfdc2 2877 }
0000e5cc 2878 return 1;
879bfdc2
DJ
2879
2880 }
2881 }
2882 else
35df4500 2883 bl->inserted = 1;
879bfdc2 2884
0000e5cc 2885 return 0;
879bfdc2
DJ
2886 }
2887
35df4500 2888 else if (bl->loc_type == bp_loc_hardware_watchpoint
879bfdc2 2889 /* NOTE drow/2003-09-08: This state only exists for removing
4a64f543 2890 watchpoints. It's not clear that it's necessary... */
35df4500 2891 && bl->owner->disposition != disp_del_at_next_stop)
879bfdc2 2892 {
0000e5cc
PA
2893 int val;
2894
c47614fe 2895 val = bl->owner->insert_location (bl);
85d721b8
PA
2896
2897 /* If trying to set a read-watchpoint, and it turns out it's not
2898 supported, try emulating one with an access watchpoint. */
35df4500 2899 if (val == 1 && bl->watchpoint_type == hw_read)
85d721b8 2900 {
85d721b8
PA
2901 /* But don't try to insert it, if there's already another
2902 hw_access location that would be considered a duplicate
2903 of this one. */
48d7020b 2904 for (bp_location *loc : all_bp_locations ())
35df4500 2905 if (loc != bl
85d721b8 2906 && loc->watchpoint_type == hw_access
35df4500 2907 && watchpoint_locations_match (bl, loc))
85d721b8 2908 {
35df4500
TJB
2909 bl->duplicate = 1;
2910 bl->inserted = 1;
2911 bl->target_info = loc->target_info;
2912 bl->watchpoint_type = hw_access;
85d721b8
PA
2913 val = 0;
2914 break;
2915 }
2916
2917 if (val == 1)
2918 {
77b06cd7 2919 bl->watchpoint_type = hw_access;
c47614fe 2920 val = bl->owner->insert_location (bl);
77b06cd7
TJB
2921
2922 if (val)
2923 /* Back to the original value. */
2924 bl->watchpoint_type = hw_read;
85d721b8
PA
2925 }
2926 }
2927
35df4500 2928 bl->inserted = (val == 0);
879bfdc2
DJ
2929 }
2930
35df4500 2931 else if (bl->owner->type == bp_catchpoint)
879bfdc2 2932 {
0000e5cc
PA
2933 int val;
2934
c47614fe 2935 val = bl->owner->insert_location (bl);
77b06cd7
TJB
2936 if (val)
2937 {
2938 bl->owner->enable_state = bp_disabled;
2939
2940 if (val == 1)
2941 warning (_("\
2942Error inserting catchpoint %d: Your system does not support this type\n\
2943of catchpoint."), bl->owner->number);
2944 else
2945 warning (_("Error inserting catchpoint %d."), bl->owner->number);
2946 }
2947
2948 bl->inserted = (val == 0);
1640b821
DJ
2949
2950 /* We've already printed an error message if there was a problem
2951 inserting this catchpoint, and we've disabled the catchpoint,
2952 so just return success. */
2953 return 0;
879bfdc2
DJ
2954 }
2955
2956 return 0;
2957}
2958
6c95b8df
PA
2959/* This function is called when program space PSPACE is about to be
2960 deleted. It takes care of updating breakpoints to not reference
2961 PSPACE anymore. */
2962
2963void
2964breakpoint_program_space_exit (struct program_space *pspace)
2965{
6c95b8df 2966 /* Remove any breakpoint that was set through this program space. */
1428b37a
SM
2967 for (breakpoint *b : all_breakpoints_safe ())
2968 if (b->pspace == pspace)
2969 delete_breakpoint (b);
6c95b8df
PA
2970
2971 /* Breakpoints set through other program spaces could have locations
2972 bound to PSPACE as well. Remove those. */
48d7020b 2973 for (bp_location *loc : all_bp_locations ())
6c95b8df
PA
2974 {
2975 struct bp_location *tmp;
2976
2977 if (loc->pspace == pspace)
2978 {
2bdf28a0 2979 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
6c95b8df
PA
2980 if (loc->owner->loc == loc)
2981 loc->owner->loc = loc->next;
2982 else
2983 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
2984 if (tmp->next == loc)
2985 {
2986 tmp->next = loc->next;
2987 break;
2988 }
2989 }
2990 }
2991
2992 /* Now update the global location list to permanently delete the
2993 removed locations above. */
44702360 2994 update_global_location_list (UGLL_DONT_INSERT);
6c95b8df
PA
2995}
2996
74960c60
VP
2997/* Make sure all breakpoints are inserted in inferior.
2998 Throws exception on any error.
2999 A breakpoint that is already inserted won't be inserted
3000 again, so calling this function twice is safe. */
3001void
3002insert_breakpoints (void)
3003{
43892fdf 3004 for (breakpoint *bpt : all_breakpoints ())
74960c60 3005 if (is_hardware_watchpoint (bpt))
3a5c3e22
PA
3006 {
3007 struct watchpoint *w = (struct watchpoint *) bpt;
3008
3009 update_watchpoint (w, 0 /* don't reparse. */);
3010 }
74960c60 3011
04086b45
PA
3012 /* Updating watchpoints creates new locations, so update the global
3013 location list. Explicitly tell ugll to insert locations and
7f32a4d5
PA
3014 ignore breakpoints_always_inserted_mode. Also,
3015 update_global_location_list tries to "upgrade" software
3016 breakpoints to hardware breakpoints to handle "set breakpoint
3017 auto-hw", so we need to call it even if we don't have new
3018 locations. */
04086b45 3019 update_global_location_list (UGLL_INSERT);
74960c60
VP
3020}
3021
b775012e
LM
3022/* This is used when we need to synch breakpoint conditions between GDB and the
3023 target. It is the case with deleting and disabling of breakpoints when using
3024 always-inserted mode. */
3025
3026static void
3027update_inserted_breakpoint_locations (void)
3028{
b775012e
LM
3029 int error_flag = 0;
3030 int val = 0;
3031 int disabled_breaks = 0;
3032 int hw_breakpoint_error = 0;
dd61ec5c 3033 int hw_bp_details_reported = 0;
b775012e 3034
d7e74731 3035 string_file tmp_error_stream;
b775012e
LM
3036
3037 /* Explicitly mark the warning -- this will only be printed if
3038 there was an error. */
d7e74731 3039 tmp_error_stream.puts ("Warning:\n");
b775012e 3040
5ed8105e 3041 scoped_restore_current_pspace_and_thread restore_pspace_thread;
b775012e 3042
48d7020b 3043 for (bp_location *bl : all_bp_locations ())
b775012e
LM
3044 {
3045 /* We only want to update software breakpoints and hardware
3046 breakpoints. */
3047 if (!is_breakpoint (bl->owner))
3048 continue;
3049
3050 /* We only want to update locations that are already inserted
3051 and need updating. This is to avoid unwanted insertion during
3052 deletion of breakpoints. */
4daf1902 3053 if (!bl->inserted || !bl->needs_update)
b775012e
LM
3054 continue;
3055
3056 switch_to_program_space_and_thread (bl->pspace);
3057
3058 /* For targets that support global breakpoints, there's no need
3059 to select an inferior to insert breakpoint to. In fact, even
3060 if we aren't attached to any process yet, we should still
3061 insert breakpoints. */
f5656ead 3062 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
55f6301a 3063 && (inferior_ptid == null_ptid || !target_has_execution ()))
b775012e
LM
3064 continue;
3065
d7e74731 3066 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
dd61ec5c 3067 &hw_breakpoint_error, &hw_bp_details_reported);
b775012e
LM
3068 if (val)
3069 error_flag = val;
3070 }
3071
3072 if (error_flag)
3073 {
223ffa71 3074 target_terminal::ours_for_output ();
b775012e
LM
3075 error_stream (tmp_error_stream);
3076 }
b775012e
LM
3077}
3078
c30eee59 3079/* Used when starting or continuing the program. */
c906108c 3080
74960c60
VP
3081static void
3082insert_breakpoint_locations (void)
c906108c 3083{
eacd795a 3084 int error_flag = 0;
c906108c 3085 int val = 0;
3fbb6ffa 3086 int disabled_breaks = 0;
81d0cc19 3087 int hw_breakpoint_error = 0;
dd61ec5c 3088 int hw_bp_error_explained_already = 0;
c906108c 3089
d7e74731
PA
3090 string_file tmp_error_stream;
3091
81d0cc19
GS
3092 /* Explicitly mark the warning -- this will only be printed if
3093 there was an error. */
d7e74731 3094 tmp_error_stream.puts ("Warning:\n");
6c95b8df 3095
5ed8105e 3096 scoped_restore_current_pspace_and_thread restore_pspace_thread;
6c95b8df 3097
48d7020b 3098 for (bp_location *bl : all_bp_locations ())
879bfdc2 3099 {
b775012e 3100 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
879bfdc2
DJ
3101 continue;
3102
4a64f543
MS
3103 /* There is no point inserting thread-specific breakpoints if
3104 the thread no longer exists. ALL_BP_LOCATIONS bp_location
3105 has BL->OWNER always non-NULL. */
35df4500 3106 if (bl->owner->thread != -1
5d5658a1 3107 && !valid_global_thread_id (bl->owner->thread))
f365de73
AS
3108 continue;
3109
35df4500 3110 switch_to_program_space_and_thread (bl->pspace);
6c95b8df
PA
3111
3112 /* For targets that support global breakpoints, there's no need
3113 to select an inferior to insert breakpoint to. In fact, even
3114 if we aren't attached to any process yet, we should still
3115 insert breakpoints. */
f5656ead 3116 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
55f6301a 3117 && (inferior_ptid == null_ptid || !target_has_execution ()))
6c95b8df
PA
3118 continue;
3119
d7e74731 3120 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
dd61ec5c 3121 &hw_breakpoint_error, &hw_bp_error_explained_already);
879bfdc2 3122 if (val)
eacd795a 3123 error_flag = val;
879bfdc2 3124 }
c906108c 3125
4a64f543
MS
3126 /* If we failed to insert all locations of a watchpoint, remove
3127 them, as half-inserted watchpoint is of limited use. */
43892fdf 3128 for (breakpoint *bpt : all_breakpoints ())
a5606eee
VP
3129 {
3130 int some_failed = 0;
a5606eee
VP
3131
3132 if (!is_hardware_watchpoint (bpt))
3133 continue;
3134
d6b74ac4 3135 if (!breakpoint_enabled (bpt))
a5606eee 3136 continue;
74960c60
VP
3137
3138 if (bpt->disposition == disp_del_at_next_stop)
3139 continue;
a5606eee 3140
40cb8ca5 3141 for (bp_location *loc : bpt->locations ())
56710373 3142 if (!loc->inserted && should_be_inserted (loc))
a5606eee
VP
3143 {
3144 some_failed = 1;
3145 break;
3146 }
40cb8ca5 3147
a5606eee
VP
3148 if (some_failed)
3149 {
40cb8ca5 3150 for (bp_location *loc : bpt->locations ())
a5606eee 3151 if (loc->inserted)
834c0d03 3152 remove_breakpoint (loc);
a5606eee
VP
3153
3154 hw_breakpoint_error = 1;
d7e74731
PA
3155 tmp_error_stream.printf ("Could not insert "
3156 "hardware watchpoint %d.\n",
3157 bpt->number);
eacd795a 3158 error_flag = -1;
a5606eee
VP
3159 }
3160 }
3161
eacd795a 3162 if (error_flag)
81d0cc19
GS
3163 {
3164 /* If a hardware breakpoint or watchpoint was inserted, add a
dda83cd7 3165 message about possibly exhausted resources. */
dd61ec5c 3166 if (hw_breakpoint_error && !hw_bp_error_explained_already)
81d0cc19 3167 {
d7e74731 3168 tmp_error_stream.printf ("Could not insert hardware breakpoints:\n\
c6510018 3169You may have requested too many hardware breakpoints/watchpoints.\n");
81d0cc19 3170 }
223ffa71 3171 target_terminal::ours_for_output ();
81d0cc19
GS
3172 error_stream (tmp_error_stream);
3173 }
c906108c
SS
3174}
3175
c30eee59
TJB
3176/* Used when the program stops.
3177 Returns zero if successful, or non-zero if there was a problem
3178 removing a breakpoint location. */
3179
c906108c 3180int
fba45db2 3181remove_breakpoints (void)
c906108c 3182{
3a1bae8e 3183 int val = 0;
c906108c 3184
48d7020b 3185 for (bp_location *bl : all_bp_locations ())
1e4d1764 3186 if (bl->inserted && !is_tracepoint (bl->owner))
834c0d03 3187 val |= remove_breakpoint (bl);
01add95b 3188
3a1bae8e 3189 return val;
c906108c
SS
3190}
3191
49fa26b0
PA
3192/* When a thread exits, remove breakpoints that are related to
3193 that thread. */
3194
3195static void
3196remove_threaded_breakpoints (struct thread_info *tp, int silent)
3197{
1428b37a 3198 for (breakpoint *b : all_breakpoints_safe ())
49fa26b0 3199 {
5d5658a1 3200 if (b->thread == tp->global_num && user_breakpoint_p (b))
49fa26b0
PA
3201 {
3202 b->disposition = disp_del_at_next_stop;
3203
6cb06a8c 3204 gdb_printf (_("\
43792cf0 3205Thread-specific breakpoint %d deleted - thread %s no longer in the thread list.\n"),
6cb06a8c 3206 b->number, print_thread_id (tp));
49fa26b0
PA
3207
3208 /* Hide it from the user. */
3209 b->number = 0;
3210 }
3211 }
3212}
3213
f3869b1a 3214/* See breakpoint.h. */
6c95b8df 3215
f3869b1a 3216void
00431a78 3217remove_breakpoints_inf (inferior *inf)
6c95b8df 3218{
6c95b8df 3219 int val;
6c95b8df 3220
48d7020b 3221 for (bp_location *bl : all_bp_locations ())
01add95b
SM
3222 {
3223 if (bl->pspace != inf->pspace)
3224 continue;
6c95b8df 3225
01add95b
SM
3226 if (bl->inserted && !bl->target_info.persist)
3227 {
3228 val = remove_breakpoint (bl);
3229 if (val != 0)
3230 return;
3231 }
3232 }
6c95b8df
PA
3233}
3234
e58b0e63
PA
3235static int internal_breakpoint_number = -1;
3236
84f4c1fe
PM
3237/* Set the breakpoint number of B, depending on the value of INTERNAL.
3238 If INTERNAL is non-zero, the breakpoint number will be populated
3239 from internal_breakpoint_number and that variable decremented.
e5dd4106 3240 Otherwise the breakpoint number will be populated from
84f4c1fe
PM
3241 breakpoint_count and that value incremented. Internal breakpoints
3242 do not set the internal var bpnum. */
3243static void
3244set_breakpoint_number (int internal, struct breakpoint *b)
3245{
3246 if (internal)
3247 b->number = internal_breakpoint_number--;
3248 else
3249 {
3250 set_breakpoint_count (breakpoint_count + 1);
3251 b->number = breakpoint_count;
3252 }
3253}
3254
e62c965a 3255static struct breakpoint *
a6d9a66e 3256create_internal_breakpoint (struct gdbarch *gdbarch,
9efa3c7f 3257 CORE_ADDR address, enum bptype type)
e62c965a 3258{
752a2291
PA
3259 std::unique_ptr<internal_breakpoint> b
3260 (new internal_breakpoint (gdbarch, type, address));
e62c965a 3261
e62c965a 3262 b->number = internal_breakpoint_number--;
e62c965a 3263
752a2291 3264 return add_to_breakpoint_chain (std::move (b));
e62c965a
PP
3265}
3266
17450429
PP
3267static const char *const longjmp_names[] =
3268 {
3269 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
3270 };
3271#define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
3272
3273/* Per-objfile data private to breakpoint.c. */
3274struct breakpoint_objfile_data
3275{
3276 /* Minimal symbol for "_ovly_debug_event" (if any). */
f6b3ad54 3277 struct bound_minimal_symbol overlay_msym;
17450429
PP
3278
3279 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
f6b3ad54 3280 struct bound_minimal_symbol longjmp_msym[NUM_LONGJMP_NAMES];
17450429 3281
28106bc2 3282 /* True if we have looked for longjmp probes. */
43dce439 3283 int longjmp_searched = 0;
28106bc2 3284
45461e0d
SM
3285 /* SystemTap probe points for longjmp (if any). These are non-owning
3286 references. */
3287 std::vector<probe *> longjmp_probes;
28106bc2 3288
17450429 3289 /* Minimal symbol for "std::terminate()" (if any). */
f6b3ad54 3290 struct bound_minimal_symbol terminate_msym;
17450429
PP
3291
3292 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
f6b3ad54 3293 struct bound_minimal_symbol exception_msym;
28106bc2
SDJ
3294
3295 /* True if we have looked for exception probes. */
43dce439 3296 int exception_searched = 0;
28106bc2 3297
45461e0d
SM
3298 /* SystemTap probe points for unwinding (if any). These are non-owning
3299 references. */
3300 std::vector<probe *> exception_probes;
17450429
PP
3301};
3302
51d3063a
TT
3303static const struct objfile_key<breakpoint_objfile_data>
3304 breakpoint_objfile_key;
17450429
PP
3305
3306/* Minimal symbol not found sentinel. */
3307static struct minimal_symbol msym_not_found;
3308
3309/* Returns TRUE if MSYM point to the "not found" sentinel. */
3310
3311static int
3312msym_not_found_p (const struct minimal_symbol *msym)
3313{
3314 return msym == &msym_not_found;
3315}
3316
3317/* Return per-objfile data needed by breakpoint.c.
3318 Allocate the data if necessary. */
3319
3320static struct breakpoint_objfile_data *
3321get_breakpoint_objfile_data (struct objfile *objfile)
3322{
3323 struct breakpoint_objfile_data *bp_objfile_data;
3324
51d3063a 3325 bp_objfile_data = breakpoint_objfile_key.get (objfile);
17450429 3326 if (bp_objfile_data == NULL)
51d3063a 3327 bp_objfile_data = breakpoint_objfile_key.emplace (objfile);
17450429
PP
3328 return bp_objfile_data;
3329}
3330
e62c965a 3331static void
af02033e 3332create_overlay_event_breakpoint (void)
e62c965a 3333{
af02033e 3334 const char *const func_name = "_ovly_debug_event";
e62c965a 3335
2030c079 3336 for (objfile *objfile : current_program_space->objfiles ())
69de3c6a
PP
3337 {
3338 struct breakpoint *b;
17450429
PP
3339 struct breakpoint_objfile_data *bp_objfile_data;
3340 CORE_ADDR addr;
67994074 3341 struct explicit_location explicit_loc;
69de3c6a 3342
17450429
PP
3343 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3344
3b7344d5 3345 if (msym_not_found_p (bp_objfile_data->overlay_msym.minsym))
17450429
PP
3346 continue;
3347
3b7344d5 3348 if (bp_objfile_data->overlay_msym.minsym == NULL)
17450429 3349 {
3b7344d5 3350 struct bound_minimal_symbol m;
17450429
PP
3351
3352 m = lookup_minimal_symbol_text (func_name, objfile);
3b7344d5 3353 if (m.minsym == NULL)
17450429
PP
3354 {
3355 /* Avoid future lookups in this objfile. */
3b7344d5 3356 bp_objfile_data->overlay_msym.minsym = &msym_not_found;
17450429
PP
3357 continue;
3358 }
3359 bp_objfile_data->overlay_msym = m;
3360 }
e62c965a 3361
4aeddc50 3362 addr = bp_objfile_data->overlay_msym.value_address ();
08feed99 3363 b = create_internal_breakpoint (objfile->arch (), addr,
9efa3c7f 3364 bp_overlay_event);
67994074
KS
3365 initialize_explicit_location (&explicit_loc);
3366 explicit_loc.function_name = ASTRDUP (func_name);
d28cd78a 3367 b->location = new_explicit_location (&explicit_loc);
e62c965a 3368
69de3c6a 3369 if (overlay_debugging == ovly_auto)
dda83cd7
SM
3370 {
3371 b->enable_state = bp_enabled;
3372 overlay_events_enabled = 1;
3373 }
69de3c6a
PP
3374 else
3375 {
dda83cd7
SM
3376 b->enable_state = bp_disabled;
3377 overlay_events_enabled = 0;
69de3c6a 3378 }
e62c965a 3379 }
e62c965a
PP
3380}
3381
2a7f6487
TV
3382/* Install a master longjmp breakpoint for OBJFILE using a probe. Return
3383 true if a breakpoint was installed. */
3384
3385static bool
3386create_longjmp_master_breakpoint_probe (objfile *objfile)
0fd8e87f 3387{
2a7f6487
TV
3388 struct gdbarch *gdbarch = objfile->arch ();
3389 struct breakpoint_objfile_data *bp_objfile_data
3390 = get_breakpoint_objfile_data (objfile);
0fd8e87f 3391
2a7f6487 3392 if (!bp_objfile_data->longjmp_searched)
94c93c35 3393 {
2a7f6487
TV
3394 std::vector<probe *> ret
3395 = find_probes_in_objfile (objfile, "libc", "longjmp");
af02033e 3396
2a7f6487 3397 if (!ret.empty ())
94c93c35 3398 {
2a7f6487
TV
3399 /* We are only interested in checking one element. */
3400 probe *p = ret[0];
0fd8e87f 3401
2a7f6487
TV
3402 if (!p->can_evaluate_arguments ())
3403 {
3404 /* We cannot use the probe interface here,
3405 because it does not know how to evaluate
3406 arguments. */
3407 ret.clear ();
3408 }
3409 }
3410 bp_objfile_data->longjmp_probes = ret;
3411 bp_objfile_data->longjmp_searched = 1;
3412 }
0fd8e87f 3413
2a7f6487
TV
3414 if (bp_objfile_data->longjmp_probes.empty ())
3415 return false;
17450429 3416
2a7f6487
TV
3417 for (probe *p : bp_objfile_data->longjmp_probes)
3418 {
3419 struct breakpoint *b;
25f9533e 3420
2a7f6487
TV
3421 b = create_internal_breakpoint (gdbarch,
3422 p->get_relocated_address (objfile),
9efa3c7f 3423 bp_longjmp_master);
2a7f6487
TV
3424 b->location = new_probe_location ("-probe-stap libc:longjmp");
3425 b->enable_state = bp_disabled;
3426 }
aed57c53 3427
2a7f6487
TV
3428 return true;
3429}
25f9533e 3430
2a7f6487
TV
3431/* Install master longjmp breakpoints for OBJFILE using longjmp_names.
3432 Return true if at least one breakpoint was installed. */
3433
3434static bool
3435create_longjmp_master_breakpoint_names (objfile *objfile)
3436{
3437 struct gdbarch *gdbarch = objfile->arch ();
3438 if (!gdbarch_get_longjmp_target_p (gdbarch))
3439 return false;
3440
3441 struct breakpoint_objfile_data *bp_objfile_data
3442 = get_breakpoint_objfile_data (objfile);
3443 unsigned int installed_bp = 0;
3444
3445 for (int i = 0; i < NUM_LONGJMP_NAMES; i++)
3446 {
3447 struct breakpoint *b;
3448 const char *func_name;
3449 CORE_ADDR addr;
3450 struct explicit_location explicit_loc;
3451
3452 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i].minsym))
3453 continue;
28106bc2 3454
2a7f6487
TV
3455 func_name = longjmp_names[i];
3456 if (bp_objfile_data->longjmp_msym[i].minsym == NULL)
3457 {
3458 struct bound_minimal_symbol m;
3459
3460 m = lookup_minimal_symbol_text (func_name, objfile);
3461 if (m.minsym == NULL)
3462 {
3463 /* Prevent future lookups in this objfile. */
3464 bp_objfile_data->longjmp_msym[i].minsym = &msym_not_found;
94c93c35
TT
3465 continue;
3466 }
2a7f6487
TV
3467 bp_objfile_data->longjmp_msym[i] = m;
3468 }
28106bc2 3469
4aeddc50 3470 addr = bp_objfile_data->longjmp_msym[i].value_address ();
9efa3c7f 3471 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master);
2a7f6487
TV
3472 initialize_explicit_location (&explicit_loc);
3473 explicit_loc.function_name = ASTRDUP (func_name);
3474 b->location = new_explicit_location (&explicit_loc);
3475 b->enable_state = bp_disabled;
3476 installed_bp++;
3477 }
28106bc2 3478
2a7f6487
TV
3479 return installed_bp > 0;
3480}
0fd8e87f 3481
2a7f6487 3482/* Create a master longjmp breakpoint. */
17450429 3483
2a7f6487
TV
3484static void
3485create_longjmp_master_breakpoint (void)
3486{
3487 scoped_restore_current_program_space restore_pspace;
aed57c53 3488
2a7f6487
TV
3489 for (struct program_space *pspace : program_spaces)
3490 {
3491 set_current_program_space (pspace);
17450429 3492
2a7f6487
TV
3493 for (objfile *obj : current_program_space->objfiles ())
3494 {
3495 /* Skip separate debug object, it's handled in the loop below. */
3496 if (obj->separate_debug_objfile_backlink != nullptr)
3497 continue;
3498
3499 /* Try a probe kind breakpoint on main objfile. */
3500 if (create_longjmp_master_breakpoint_probe (obj))
3501 continue;
3502
3503 /* Try longjmp_names kind breakpoints on main and separate_debug
3504 objfiles. */
3505 for (objfile *debug_objfile : obj->separate_debug_objfiles ())
3506 if (create_longjmp_master_breakpoint_names (debug_objfile))
3507 break;
94c93c35
TT
3508 }
3509 }
0fd8e87f
UW
3510}
3511
af02033e 3512/* Create a master std::terminate breakpoint. */
aa7d318d 3513static void
af02033e 3514create_std_terminate_master_breakpoint (void)
aa7d318d 3515{
af02033e 3516 const char *const func_name = "std::terminate()";
aa7d318d 3517
5ed8105e 3518 scoped_restore_current_program_space restore_pspace;
aa7d318d 3519
94c93c35
TT
3520 for (struct program_space *pspace : program_spaces)
3521 {
3522 CORE_ADDR addr;
17450429 3523
94c93c35 3524 set_current_program_space (pspace);
17450429 3525
94c93c35
TT
3526 for (objfile *objfile : current_program_space->objfiles ())
3527 {
3528 struct breakpoint *b;
3529 struct breakpoint_objfile_data *bp_objfile_data;
3530 struct explicit_location explicit_loc;
aa7d318d 3531
94c93c35 3532 bp_objfile_data = get_breakpoint_objfile_data (objfile);
aa7d318d 3533
94c93c35
TT
3534 if (msym_not_found_p (bp_objfile_data->terminate_msym.minsym))
3535 continue;
17450429 3536
94c93c35
TT
3537 if (bp_objfile_data->terminate_msym.minsym == NULL)
3538 {
3539 struct bound_minimal_symbol m;
17450429 3540
94c93c35 3541 m = lookup_minimal_symbol (func_name, NULL, objfile);
60f62e2b
SM
3542 if (m.minsym == NULL || (m.minsym->type () != mst_text
3543 && m.minsym->type () != mst_file_text))
94c93c35
TT
3544 {
3545 /* Prevent future lookups in this objfile. */
3546 bp_objfile_data->terminate_msym.minsym = &msym_not_found;
3547 continue;
3548 }
3549 bp_objfile_data->terminate_msym = m;
3550 }
aa7d318d 3551
4aeddc50 3552 addr = bp_objfile_data->terminate_msym.value_address ();
94c93c35 3553 b = create_internal_breakpoint (objfile->arch (), addr,
9efa3c7f 3554 bp_std_terminate_master);
94c93c35
TT
3555 initialize_explicit_location (&explicit_loc);
3556 explicit_loc.function_name = ASTRDUP (func_name);
3557 b->location = new_explicit_location (&explicit_loc);
3558 b->enable_state = bp_disabled;
3559 }
3560 }
aa7d318d
TT
3561}
3562
1940319c
TV
3563/* Install a master breakpoint on the unwinder's debug hook for OBJFILE using a
3564 probe. Return true if a breakpoint was installed. */
186c406b 3565
1940319c
TV
3566static bool
3567create_exception_master_breakpoint_probe (objfile *objfile)
186c406b 3568{
1940319c
TV
3569 struct breakpoint *b;
3570 struct gdbarch *gdbarch;
3571 struct breakpoint_objfile_data *bp_objfile_data;
186c406b 3572
1940319c 3573 bp_objfile_data = get_breakpoint_objfile_data (objfile);
17450429 3574
1940319c
TV
3575 /* We prefer the SystemTap probe point if it exists. */
3576 if (!bp_objfile_data->exception_searched)
3577 {
3578 std::vector<probe *> ret
3579 = find_probes_in_objfile (objfile, "libgcc", "unwind");
17450429 3580
1940319c 3581 if (!ret.empty ())
28106bc2 3582 {
1940319c
TV
3583 /* We are only interested in checking one element. */
3584 probe *p = ret[0];
25f9533e 3585
1940319c 3586 if (!p->can_evaluate_arguments ())
25f9533e 3587 {
1940319c
TV
3588 /* We cannot use the probe interface here, because it does
3589 not know how to evaluate arguments. */
3590 ret.clear ();
25f9533e 3591 }
28106bc2 3592 }
1940319c
TV
3593 bp_objfile_data->exception_probes = ret;
3594 bp_objfile_data->exception_searched = 1;
3595 }
28106bc2 3596
1940319c
TV
3597 if (bp_objfile_data->exception_probes.empty ())
3598 return false;
45461e0d 3599
1940319c 3600 gdbarch = objfile->arch ();
28106bc2 3601
1940319c
TV
3602 for (probe *p : bp_objfile_data->exception_probes)
3603 {
3604 b = create_internal_breakpoint (gdbarch,
3605 p->get_relocated_address (objfile),
9efa3c7f 3606 bp_exception_master);
1940319c
TV
3607 b->location = new_probe_location ("-probe-stap libgcc:unwind");
3608 b->enable_state = bp_disabled;
3609 }
28106bc2 3610
1940319c
TV
3611 return true;
3612}
28106bc2 3613
1940319c
TV
3614/* Install a master breakpoint on the unwinder's debug hook for OBJFILE using
3615 _Unwind_DebugHook. Return true if a breakpoint was installed. */
17450429 3616
1940319c
TV
3617static bool
3618create_exception_master_breakpoint_hook (objfile *objfile)
3619{
3620 const char *const func_name = "_Unwind_DebugHook";
3621 struct breakpoint *b;
3622 struct gdbarch *gdbarch;
3623 struct breakpoint_objfile_data *bp_objfile_data;
3624 CORE_ADDR addr;
3625 struct explicit_location explicit_loc;
186c406b 3626
1940319c 3627 bp_objfile_data = get_breakpoint_objfile_data (objfile);
186c406b 3628
1940319c
TV
3629 if (msym_not_found_p (bp_objfile_data->exception_msym.minsym))
3630 return false;
17450429 3631
1940319c
TV
3632 gdbarch = objfile->arch ();
3633
3634 if (bp_objfile_data->exception_msym.minsym == NULL)
3635 {
3636 struct bound_minimal_symbol debug_hook;
3637
3638 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
3639 if (debug_hook.minsym == NULL)
3640 {
3641 bp_objfile_data->exception_msym.minsym = &msym_not_found;
3642 return false;
186c406b 3643 }
17450429 3644
1940319c
TV
3645 bp_objfile_data->exception_msym = debug_hook;
3646 }
3647
4aeddc50 3648 addr = bp_objfile_data->exception_msym.value_address ();
328d42d8
SM
3649 addr = gdbarch_convert_from_func_ptr_addr
3650 (gdbarch, addr, current_inferior ()->top_target ());
9efa3c7f 3651 b = create_internal_breakpoint (gdbarch, addr, bp_exception_master);
1940319c
TV
3652 initialize_explicit_location (&explicit_loc);
3653 explicit_loc.function_name = ASTRDUP (func_name);
3654 b->location = new_explicit_location (&explicit_loc);
3655 b->enable_state = bp_disabled;
3656
3657 return true;
3658}
3659
3660/* Install a master breakpoint on the unwinder's debug hook. */
3661
3662static void
3663create_exception_master_breakpoint (void)
3664{
3665 for (objfile *obj : current_program_space->objfiles ())
3666 {
3667 /* Skip separate debug object. */
3668 if (obj->separate_debug_objfile_backlink)
3669 continue;
3670
3671 /* Try a probe kind breakpoint. */
3672 if (create_exception_master_breakpoint_probe (obj))
3673 continue;
3674
7c6944ab
TV
3675 /* Iterate over main and separate debug objects and try an
3676 _Unwind_DebugHook kind breakpoint. */
3677 for (objfile *debug_objfile : obj->separate_debug_objfiles ())
3678 if (create_exception_master_breakpoint_hook (debug_objfile))
1940319c 3679 break;
186c406b 3680 }
186c406b
TT
3681}
3682
9ef9e6a6
KS
3683/* Does B have a location spec? */
3684
3685static int
3686breakpoint_event_location_empty_p (const struct breakpoint *b)
3687{
d28cd78a 3688 return b->location != NULL && event_location_empty_p (b->location.get ());
9ef9e6a6
KS
3689}
3690
c906108c 3691void
fba45db2 3692update_breakpoints_after_exec (void)
c906108c 3693{
25b22b0a
PA
3694 /* We're about to delete breakpoints from GDB's lists. If the
3695 INSERTED flag is true, GDB will try to lift the breakpoints by
3696 writing the breakpoints' "shadow contents" back into memory. The
3697 "shadow contents" are NOT valid after an exec, so GDB should not
3698 do that. Instead, the target is responsible from marking
3699 breakpoints out as soon as it detects an exec. We don't do that
3700 here instead, because there may be other attempts to delete
3701 breakpoints after detecting an exec and before reaching here. */
48d7020b 3702 for (bp_location *bploc : all_bp_locations ())
6c95b8df
PA
3703 if (bploc->pspace == current_program_space)
3704 gdb_assert (!bploc->inserted);
c906108c 3705
1428b37a 3706 for (breakpoint *b : all_breakpoints_safe ())
01add95b
SM
3707 {
3708 if (b->pspace != current_program_space)
c5aa993b 3709 continue;
c906108c 3710
01add95b
SM
3711 /* Solib breakpoints must be explicitly reset after an exec(). */
3712 if (b->type == bp_shlib_event)
3713 {
3714 delete_breakpoint (b);
3715 continue;
3716 }
4efc6507 3717
01add95b
SM
3718 /* JIT breakpoints must be explicitly reset after an exec(). */
3719 if (b->type == bp_jit_event)
3720 {
3721 delete_breakpoint (b);
3722 continue;
3723 }
c4093a6a 3724
01add95b
SM
3725 /* Thread event breakpoints must be set anew after an exec(),
3726 as must overlay event and longjmp master breakpoints. */
3727 if (b->type == bp_thread_event || b->type == bp_overlay_event
3728 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
3729 || b->type == bp_exception_master)
3730 {
3731 delete_breakpoint (b);
3732 continue;
3733 }
c5aa993b 3734
01add95b
SM
3735 /* Step-resume breakpoints are meaningless after an exec(). */
3736 if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
3737 {
3738 delete_breakpoint (b);
3739 continue;
3740 }
7c16b83e 3741
01add95b
SM
3742 /* Just like single-step breakpoints. */
3743 if (b->type == bp_single_step)
3744 {
3745 delete_breakpoint (b);
3746 continue;
3747 }
611c83ae 3748
01add95b
SM
3749 /* Longjmp and longjmp-resume breakpoints are also meaningless
3750 after an exec. */
3751 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
3752 || b->type == bp_longjmp_call_dummy
3753 || b->type == bp_exception || b->type == bp_exception_resume)
3754 {
3755 delete_breakpoint (b);
3756 continue;
3757 }
ce78b96d 3758
01add95b
SM
3759 if (b->type == bp_catchpoint)
3760 {
3761 /* For now, none of the bp_catchpoint breakpoints need to
3762 do anything at this point. In the future, if some of
3763 the catchpoints need to something, we will need to add
3764 a new method, and call this method from here. */
3765 continue;
3766 }
c5aa993b 3767
01add95b
SM
3768 /* bp_finish is a special case. The only way we ought to be able
3769 to see one of these when an exec() has happened, is if the user
3770 caught a vfork, and then said "finish". Ordinarily a finish just
3771 carries them to the call-site of the current callee, by setting
3772 a temporary bp there and resuming. But in this case, the finish
3773 will carry them entirely through the vfork & exec.
3774
3775 We don't want to allow a bp_finish to remain inserted now. But
3776 we can't safely delete it, 'cause finish_command has a handle to
3777 the bp on a bpstat, and will later want to delete it. There's a
3778 chance (and I've seen it happen) that if we delete the bp_finish
3779 here, that its storage will get reused by the time finish_command
3780 gets 'round to deleting the "use to be a bp_finish" breakpoint.
3781 We really must allow finish_command to delete a bp_finish.
3782
3783 In the absence of a general solution for the "how do we know
3784 it's safe to delete something others may have handles to?"
3785 problem, what we'll do here is just uninsert the bp_finish, and
3786 let finish_command delete it.
3787
3788 (We know the bp_finish is "doomed" in the sense that it's
3789 momentary, and will be deleted as soon as finish_command sees
3790 the inferior stopped. So it doesn't matter that the bp's
3791 address is probably bogus in the new a.out, unlike e.g., the
3792 solib breakpoints.) */
3793
3794 if (b->type == bp_finish)
3795 {
3796 continue;
3797 }
3798
3799 /* Without a symbolic address, we have little hope of the
3800 pre-exec() address meaning the same thing in the post-exec()
3801 a.out. */
3802 if (breakpoint_event_location_empty_p (b))
3803 {
3804 delete_breakpoint (b);
3805 continue;
3806 }
3807 }
c906108c
SS
3808}
3809
3810int
d80ee84f 3811detach_breakpoints (ptid_t ptid)
c906108c 3812{
3a1bae8e 3813 int val = 0;
2989a365 3814 scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid);
6c95b8df 3815 struct inferior *inf = current_inferior ();
c5aa993b 3816
e99b03dc 3817 if (ptid.pid () == inferior_ptid.pid ())
8a3fe4f8 3818 error (_("Cannot detach breakpoints of inferior_ptid"));
c5aa993b 3819
6c95b8df 3820 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
d80ee84f 3821 inferior_ptid = ptid;
48d7020b 3822 for (bp_location *bl : all_bp_locations ())
01add95b
SM
3823 {
3824 if (bl->pspace != inf->pspace)
3825 continue;
3826
3827 /* This function must physically remove breakpoints locations
3828 from the specified ptid, without modifying the breakpoint
6e14e441
PA
3829 package's state. Locations of type bp_loc_other and
3830 bp_loc_software_watchpoint are only maintained at GDB side,
3831 so there is no need to remove them. Moreover, removing these
01add95b 3832 would modify the breakpoint package's state. */
6e14e441
PA
3833 if (bl->loc_type == bp_loc_other
3834 || bl->loc_type == bp_loc_software_watchpoint)
01add95b
SM
3835 continue;
3836
3837 if (bl->inserted)
3838 val |= remove_breakpoint_1 (bl, DETACH_BREAKPOINT);
3839 }
d03285ec 3840
3a1bae8e 3841 return val;
c906108c
SS
3842}
3843
35df4500 3844/* Remove the breakpoint location BL from the current address space.
6c95b8df
PA
3845 Note that this is used to detach breakpoints from a child fork.
3846 When we get here, the child isn't in the inferior list, and neither
3847 do we have objects to represent its address space --- we should
35df4500 3848 *not* look at bl->pspace->aspace here. */
6c95b8df 3849
c906108c 3850static int
b2b6a7da 3851remove_breakpoint_1 (struct bp_location *bl, enum remove_bp_reason reason)
c906108c
SS
3852{
3853 int val;
c5aa993b 3854
35df4500
TJB
3855 /* BL is never in moribund_locations by our callers. */
3856 gdb_assert (bl->owner != NULL);
2bdf28a0 3857
74960c60
VP
3858 /* The type of none suggests that owner is actually deleted.
3859 This should not ever happen. */
35df4500 3860 gdb_assert (bl->owner->type != bp_none);
0bde7532 3861
35df4500
TJB
3862 if (bl->loc_type == bp_loc_software_breakpoint
3863 || bl->loc_type == bp_loc_hardware_breakpoint)
c906108c 3864 {
c02f5703
MS
3865 /* "Normal" instruction breakpoint: either the standard
3866 trap-instruction bp (bp_breakpoint), or a
3867 bp_hardware_breakpoint. */
3868
3869 /* First check to see if we have to handle an overlay. */
3870 if (overlay_debugging == ovly_off
35df4500
TJB
3871 || bl->section == NULL
3872 || !(section_is_overlay (bl->section)))
c02f5703
MS
3873 {
3874 /* No overlay handling: just remove the breakpoint. */
08351840
PA
3875
3876 /* If we're trying to uninsert a memory breakpoint that we
3877 know is set in a dynamic object that is marked
3878 shlib_disabled, then either the dynamic object was
3879 removed with "remove-symbol-file" or with
3880 "nosharedlibrary". In the former case, we don't know
3881 whether another dynamic object might have loaded over the
3882 breakpoint's address -- the user might well let us know
3883 about it next with add-symbol-file (the whole point of
d03de421 3884 add-symbol-file is letting the user manually maintain a
08351840
PA
3885 list of dynamically loaded objects). If we have the
3886 breakpoint's shadow memory, that is, this is a software
3887 breakpoint managed by GDB, check whether the breakpoint
3888 is still inserted in memory, to avoid overwriting wrong
3889 code with stale saved shadow contents. Note that HW
3890 breakpoints don't have shadow memory, as they're
3891 implemented using a mechanism that is not dependent on
3892 being able to modify the target's memory, and as such
3893 they should always be removed. */
3894 if (bl->shlib_disabled
3895 && bl->target_info.shadow_len != 0
3896 && !memory_validate_breakpoint (bl->gdbarch, &bl->target_info))
3897 val = 0;
3898 else
c47614fe 3899 val = bl->owner->remove_location (bl, reason);
c02f5703 3900 }
c906108c
SS
3901 else
3902 {
4a64f543 3903 /* This breakpoint is in an overlay section.
c02f5703
MS
3904 Did we set a breakpoint at the LMA? */
3905 if (!overlay_events_enabled)
3906 {
3907 /* Yes -- overlay event support is not active, so we
3908 should have set a breakpoint at the LMA. Remove it.
3909 */
c02f5703
MS
3910 /* Ignore any failures: if the LMA is in ROM, we will
3911 have already warned when we failed to insert it. */
35df4500
TJB
3912 if (bl->loc_type == bp_loc_hardware_breakpoint)
3913 target_remove_hw_breakpoint (bl->gdbarch,
3914 &bl->overlay_target_info);
c02f5703 3915 else
35df4500 3916 target_remove_breakpoint (bl->gdbarch,
73971819
PA
3917 &bl->overlay_target_info,
3918 reason);
c02f5703
MS
3919 }
3920 /* Did we set a breakpoint at the VMA?
3921 If so, we will have marked the breakpoint 'inserted'. */
35df4500 3922 if (bl->inserted)
c906108c 3923 {
c02f5703
MS
3924 /* Yes -- remove it. Previously we did not bother to
3925 remove the breakpoint if the section had been
3926 unmapped, but let's not rely on that being safe. We
3927 don't know what the overlay manager might do. */
aa67235e
UW
3928
3929 /* However, we should remove *software* breakpoints only
3930 if the section is still mapped, or else we overwrite
3931 wrong code with the saved shadow contents. */
348d480f
PA
3932 if (bl->loc_type == bp_loc_hardware_breakpoint
3933 || section_is_mapped (bl->section))
c47614fe 3934 val = bl->owner->remove_location (bl, reason);
aa67235e
UW
3935 else
3936 val = 0;
c906108c 3937 }
c02f5703
MS
3938 else
3939 {
3940 /* No -- not inserted, so no need to remove. No error. */
3941 val = 0;
3942 }
c906108c 3943 }
879d1e6b 3944
08351840
PA
3945 /* In some cases, we might not be able to remove a breakpoint in
3946 a shared library that has already been removed, but we have
3947 not yet processed the shlib unload event. Similarly for an
3948 unloaded add-symbol-file object - the user might not yet have
3949 had the chance to remove-symbol-file it. shlib_disabled will
3950 be set if the library/object has already been removed, but
3951 the breakpoint hasn't been uninserted yet, e.g., after
3952 "nosharedlibrary" or "remove-symbol-file" with breakpoints
3953 always-inserted mode. */
076855f9 3954 if (val
08351840
PA
3955 && (bl->loc_type == bp_loc_software_breakpoint
3956 && (bl->shlib_disabled
3957 || solib_name_from_address (bl->pspace, bl->address)
d03de421
PA
3958 || shared_objfile_contains_address_p (bl->pspace,
3959 bl->address))))
879d1e6b
UW
3960 val = 0;
3961
c906108c
SS
3962 if (val)
3963 return val;
b2b6a7da 3964 bl->inserted = (reason == DETACH_BREAKPOINT);
c906108c 3965 }
35df4500 3966 else if (bl->loc_type == bp_loc_hardware_watchpoint)
c906108c 3967 {
b2b6a7da 3968 bl->inserted = (reason == DETACH_BREAKPOINT);
c47614fe 3969 bl->owner->remove_location (bl, reason);
2e70b7b9 3970
c906108c 3971 /* Failure to remove any of the hardware watchpoints comes here. */
b2b6a7da 3972 if (reason == REMOVE_BREAKPOINT && bl->inserted)
8a3fe4f8 3973 warning (_("Could not remove hardware watchpoint %d."),
35df4500 3974 bl->owner->number);
c906108c 3975 }
35df4500 3976 else if (bl->owner->type == bp_catchpoint
dda83cd7
SM
3977 && breakpoint_enabled (bl->owner)
3978 && !bl->duplicate)
ce78b96d 3979 {
c47614fe 3980 val = bl->owner->remove_location (bl, reason);
ce78b96d
JB
3981 if (val)
3982 return val;
77b06cd7 3983
b2b6a7da 3984 bl->inserted = (reason == DETACH_BREAKPOINT);
ce78b96d 3985 }
c906108c
SS
3986
3987 return 0;
3988}
3989
6c95b8df 3990static int
834c0d03 3991remove_breakpoint (struct bp_location *bl)
6c95b8df 3992{
35df4500
TJB
3993 /* BL is never in moribund_locations by our callers. */
3994 gdb_assert (bl->owner != NULL);
2bdf28a0 3995
6c95b8df
PA
3996 /* The type of none suggests that owner is actually deleted.
3997 This should not ever happen. */
35df4500 3998 gdb_assert (bl->owner->type != bp_none);
6c95b8df 3999
5ed8105e 4000 scoped_restore_current_pspace_and_thread restore_pspace_thread;
6c95b8df 4001
35df4500 4002 switch_to_program_space_and_thread (bl->pspace);
6c95b8df 4003
5ed8105e 4004 return remove_breakpoint_1 (bl, REMOVE_BREAKPOINT);
6c95b8df
PA
4005}
4006
c906108c
SS
4007/* Clear the "inserted" flag in all breakpoints. */
4008
25b22b0a 4009void
fba45db2 4010mark_breakpoints_out (void)
c906108c 4011{
48d7020b 4012 for (bp_location *bl : all_bp_locations ())
66c4b3e8 4013 if (bl->pspace == current_program_space)
35df4500 4014 bl->inserted = 0;
c906108c
SS
4015}
4016
53a5351d
JM
4017/* Clear the "inserted" flag in all breakpoints and delete any
4018 breakpoints which should go away between runs of the program.
c906108c
SS
4019
4020 Plus other such housekeeping that has to be done for breakpoints
4021 between runs.
4022
53a5351d
JM
4023 Note: this function gets called at the end of a run (by
4024 generic_mourn_inferior) and when a run begins (by
4a64f543 4025 init_wait_for_inferior). */
c906108c
SS
4026
4027
4028
4029void
fba45db2 4030breakpoint_init_inferior (enum inf_context context)
c906108c 4031{
6c95b8df 4032 struct program_space *pspace = current_program_space;
c906108c 4033
50c71eaf
PA
4034 /* If breakpoint locations are shared across processes, then there's
4035 nothing to do. */
f5656ead 4036 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
50c71eaf
PA
4037 return;
4038
1a853c52 4039 mark_breakpoints_out ();
075f6582 4040
1428b37a 4041 for (breakpoint *b : all_breakpoints_safe ())
01add95b
SM
4042 {
4043 if (b->loc && b->loc->pspace != pspace)
4044 continue;
6c95b8df 4045
01add95b
SM
4046 switch (b->type)
4047 {
4048 case bp_call_dummy:
4049 case bp_longjmp_call_dummy:
c906108c 4050
01add95b
SM
4051 /* If the call dummy breakpoint is at the entry point it will
4052 cause problems when the inferior is rerun, so we better get
4053 rid of it. */
ab92d69b 4054
01add95b 4055 case bp_watchpoint_scope:
ab92d69b 4056
01add95b 4057 /* Also get rid of scope breakpoints. */
ab92d69b 4058
01add95b 4059 case bp_shlib_event:
ab92d69b 4060
01add95b
SM
4061 /* Also remove solib event breakpoints. Their addresses may
4062 have changed since the last time we ran the program.
4063 Actually we may now be debugging against different target;
4064 and so the solib backend that installed this breakpoint may
4065 not be used in by the target. E.g.,
ab92d69b 4066
01add95b
SM
4067 (gdb) file prog-linux
4068 (gdb) run # native linux target
4069 ...
4070 (gdb) kill
4071 (gdb) file prog-win.exe
4072 (gdb) tar rem :9999 # remote Windows gdbserver.
4073 */
c906108c 4074
01add95b 4075 case bp_step_resume:
f59f708a 4076
01add95b 4077 /* Also remove step-resume breakpoints. */
f59f708a 4078
01add95b 4079 case bp_single_step:
7c16b83e 4080
01add95b 4081 /* Also remove single-step breakpoints. */
7c16b83e 4082
01add95b
SM
4083 delete_breakpoint (b);
4084 break;
c906108c 4085
01add95b
SM
4086 case bp_watchpoint:
4087 case bp_hardware_watchpoint:
4088 case bp_read_watchpoint:
4089 case bp_access_watchpoint:
4090 {
4091 struct watchpoint *w = (struct watchpoint *) b;
c906108c 4092
01add95b
SM
4093 /* Likewise for watchpoints on local expressions. */
4094 if (w->exp_valid_block != NULL)
4095 delete_breakpoint (b);
4096 else
4097 {
4098 /* Get rid of existing locations, which are no longer
4099 valid. New ones will be created in
4100 update_watchpoint, when the inferior is restarted.
4101 The next update_global_location_list call will
4102 garbage collect them. */
4103 b->loc = NULL;
4104
4105 if (context == inf_starting)
4106 {
4107 /* Reset val field to force reread of starting value in
4108 insert_breakpoints. */
4109 w->val.reset (nullptr);
4110 w->val_valid = false;
4111 }
4112 }
4113 }
4114 break;
4115 default:
4116 break;
3a5c3e22 4117 }
01add95b 4118 }
1c5cfe86
PA
4119
4120 /* Get rid of the moribund locations. */
1123588c 4121 for (bp_location *bl : moribund_locations)
35df4500 4122 decref_bp_location (&bl);
1123588c 4123 moribund_locations.clear ();
c906108c
SS
4124}
4125
6c95b8df
PA
4126/* These functions concern about actual breakpoints inserted in the
4127 target --- to e.g. check if we need to do decr_pc adjustment or if
4128 we need to hop over the bkpt --- so we check for address space
4129 match, not program space. */
4130
c2c6d25f
JM
4131/* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
4132 exists at PC. It returns ordinary_breakpoint_here if it's an
4133 ordinary breakpoint, or permanent_breakpoint_here if it's a
4134 permanent breakpoint.
4135 - When continuing from a location with an ordinary breakpoint, we
4136 actually single step once before calling insert_breakpoints.
e5dd4106 4137 - When continuing from a location with a permanent breakpoint, we
c2c6d25f
JM
4138 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
4139 the target, to advance the PC past the breakpoint. */
c906108c 4140
c2c6d25f 4141enum breakpoint_here
accd0bcd 4142breakpoint_here_p (const address_space *aspace, CORE_ADDR pc)
c906108c 4143{
c2c6d25f 4144 int any_breakpoint_here = 0;
c906108c 4145
48d7020b 4146 for (bp_location *bl : all_bp_locations ())
075f6582 4147 {
35df4500
TJB
4148 if (bl->loc_type != bp_loc_software_breakpoint
4149 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
4150 continue;
4151
f1310107 4152 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
35df4500 4153 if ((breakpoint_enabled (bl->owner)
1a853c52 4154 || bl->permanent)
f1310107 4155 && breakpoint_location_address_match (bl, aspace, pc))
075f6582
DJ
4156 {
4157 if (overlay_debugging
35df4500
TJB
4158 && section_is_overlay (bl->section)
4159 && !section_is_mapped (bl->section))
075f6582 4160 continue; /* unmapped overlay -- can't be a match */
1a853c52 4161 else if (bl->permanent)
075f6582
DJ
4162 return permanent_breakpoint_here;
4163 else
4164 any_breakpoint_here = 1;
4165 }
4166 }
c906108c 4167
f486487f 4168 return any_breakpoint_here ? ordinary_breakpoint_here : no_breakpoint_here;
c906108c
SS
4169}
4170
d35ae833
PA
4171/* See breakpoint.h. */
4172
4173int
accd0bcd 4174breakpoint_in_range_p (const address_space *aspace,
d35ae833
PA
4175 CORE_ADDR addr, ULONGEST len)
4176{
48d7020b 4177 for (bp_location *bl : all_bp_locations ())
d35ae833
PA
4178 {
4179 if (bl->loc_type != bp_loc_software_breakpoint
4180 && bl->loc_type != bp_loc_hardware_breakpoint)
4181 continue;
4182
4183 if ((breakpoint_enabled (bl->owner)
4184 || bl->permanent)
4185 && breakpoint_location_address_range_overlap (bl, aspace,
4186 addr, len))
4187 {
4188 if (overlay_debugging
4189 && section_is_overlay (bl->section)
4190 && !section_is_mapped (bl->section))
4191 {
4192 /* Unmapped overlay -- can't be a match. */
4193 continue;
4194 }
4195
4196 return 1;
4197 }
4198 }
4199
4200 return 0;
4201}
4202
1c5cfe86
PA
4203/* Return true if there's a moribund breakpoint at PC. */
4204
4205int
accd0bcd 4206moribund_breakpoint_here_p (const address_space *aspace, CORE_ADDR pc)
1c5cfe86 4207{
1123588c 4208 for (bp_location *loc : moribund_locations)
f1310107 4209 if (breakpoint_location_address_match (loc, aspace, pc))
1c5cfe86
PA
4210 return 1;
4211
4212 return 0;
4213}
c2c6d25f 4214
f7ce857f
PA
4215/* Returns non-zero iff BL is inserted at PC, in address space
4216 ASPACE. */
4217
4218static int
4219bp_location_inserted_here_p (struct bp_location *bl,
accd0bcd 4220 const address_space *aspace, CORE_ADDR pc)
f7ce857f
PA
4221{
4222 if (bl->inserted
4223 && breakpoint_address_match (bl->pspace->aspace, bl->address,
4224 aspace, pc))
4225 {
4226 if (overlay_debugging
4227 && section_is_overlay (bl->section)
4228 && !section_is_mapped (bl->section))
4229 return 0; /* unmapped overlay -- can't be a match */
4230 else
4231 return 1;
4232 }
4233 return 0;
4234}
4235
a1fd2fa5 4236/* Returns non-zero iff there's a breakpoint inserted at PC. */
c906108c
SS
4237
4238int
accd0bcd 4239breakpoint_inserted_here_p (const address_space *aspace, CORE_ADDR pc)
c906108c 4240{
e0d9a270 4241 for (bp_location *bl : all_bp_locations_at_addr (pc))
c5aa993b 4242 {
35df4500
TJB
4243 if (bl->loc_type != bp_loc_software_breakpoint
4244 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
4245 continue;
4246
f7ce857f
PA
4247 if (bp_location_inserted_here_p (bl, aspace, pc))
4248 return 1;
c5aa993b 4249 }
c36b740a
VP
4250 return 0;
4251}
4252
a1fd2fa5
PA
4253/* This function returns non-zero iff there is a software breakpoint
4254 inserted at PC. */
c36b740a
VP
4255
4256int
accd0bcd 4257software_breakpoint_inserted_here_p (const address_space *aspace,
a1fd2fa5 4258 CORE_ADDR pc)
4fa8626c 4259{
e0d9a270 4260 for (bp_location *bl : all_bp_locations_at_addr (pc))
4fa8626c 4261 {
35df4500 4262 if (bl->loc_type != bp_loc_software_breakpoint)
4fa8626c
DJ
4263 continue;
4264
f7ce857f
PA
4265 if (bp_location_inserted_here_p (bl, aspace, pc))
4266 return 1;
4fa8626c
DJ
4267 }
4268
4269 return 0;
9c02b525
PA
4270}
4271
4272/* See breakpoint.h. */
4273
4274int
accd0bcd 4275hardware_breakpoint_inserted_here_p (const address_space *aspace,
9c02b525
PA
4276 CORE_ADDR pc)
4277{
e0d9a270 4278 for (bp_location *bl : all_bp_locations_at_addr (pc))
9c02b525 4279 {
9c02b525
PA
4280 if (bl->loc_type != bp_loc_hardware_breakpoint)
4281 continue;
4282
4283 if (bp_location_inserted_here_p (bl, aspace, pc))
4284 return 1;
4285 }
4286
4287 return 0;
4fa8626c
DJ
4288}
4289
9093389c 4290int
accd0bcd 4291hardware_watchpoint_inserted_in_range (const address_space *aspace,
9093389c
PA
4292 CORE_ADDR addr, ULONGEST len)
4293{
43892fdf 4294 for (breakpoint *bpt : all_breakpoints ())
9093389c 4295 {
9093389c
PA
4296 if (bpt->type != bp_hardware_watchpoint
4297 && bpt->type != bp_access_watchpoint)
4298 continue;
4299
4300 if (!breakpoint_enabled (bpt))
4301 continue;
4302
40cb8ca5 4303 for (bp_location *loc : bpt->locations ())
9093389c
PA
4304 if (loc->pspace->aspace == aspace && loc->inserted)
4305 {
4306 CORE_ADDR l, h;
4307
4308 /* Check for intersection. */
768adc05
PA
4309 l = std::max<CORE_ADDR> (loc->address, addr);
4310 h = std::min<CORE_ADDR> (loc->address + loc->length, addr + len);
9093389c
PA
4311 if (l < h)
4312 return 1;
4313 }
4314 }
4315 return 0;
4316}
c5aa993b 4317
f2478a7e 4318/* See breakpoint.h. */
c906108c 4319
f2478a7e
SM
4320bool
4321is_catchpoint (struct breakpoint *b)
c906108c 4322{
f2478a7e 4323 return (b->type == bp_catchpoint);
c906108c
SS
4324}
4325
c906108c
SS
4326/* Clear a bpstat so that it says we are not at any breakpoint.
4327 Also free any storage that is part of a bpstat. */
4328
4329void
313f3b21 4330bpstat_clear (bpstat **bsp)
c906108c 4331{
313f3b21
SM
4332 bpstat *p;
4333 bpstat *q;
c906108c
SS
4334
4335 if (bsp == 0)
4336 return;
4337 p = *bsp;
4338 while (p != NULL)
4339 {
4340 q = p->next;
04afa70c 4341 delete p;
c906108c
SS
4342 p = q;
4343 }
4344 *bsp = NULL;
4345}
4346
313f3b21 4347bpstat::bpstat (const bpstat &other)
04afa70c
TT
4348 : next (NULL),
4349 bp_location_at (other.bp_location_at),
4350 breakpoint_at (other.breakpoint_at),
4351 commands (other.commands),
04afa70c
TT
4352 print (other.print),
4353 stop (other.stop),
4354 print_it (other.print_it)
4355{
850645cf
TT
4356 if (other.old_val != NULL)
4357 old_val = release_value (value_copy (other.old_val.get ()));
04afa70c
TT
4358}
4359
c906108c
SS
4360/* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
4361 is part of the bpstat is copied as well. */
4362
313f3b21
SM
4363bpstat *
4364bpstat_copy (bpstat *bs)
c906108c 4365{
313f3b21
SM
4366 bpstat *p = nullptr;
4367 bpstat *tmp;
4368 bpstat *retval = nullptr;
c906108c
SS
4369
4370 if (bs == NULL)
4371 return bs;
4372
4373 for (; bs != NULL; bs = bs->next)
4374 {
313f3b21 4375 tmp = new bpstat (*bs);
31cc81e9 4376
c906108c
SS
4377 if (p == NULL)
4378 /* This is the first thing in the chain. */
4379 retval = tmp;
4380 else
4381 p->next = tmp;
4382 p = tmp;
4383 }
4384 p->next = NULL;
4385 return retval;
4386}
4387
4a64f543 4388/* Find the bpstat associated with this breakpoint. */
c906108c 4389
313f3b21
SM
4390bpstat *
4391bpstat_find_breakpoint (bpstat *bsp, struct breakpoint *breakpoint)
c906108c 4392{
c5aa993b
JM
4393 if (bsp == NULL)
4394 return NULL;
c906108c 4395
c5aa993b
JM
4396 for (; bsp != NULL; bsp = bsp->next)
4397 {
f431efe5 4398 if (bsp->breakpoint_at == breakpoint)
c5aa993b
JM
4399 return bsp;
4400 }
c906108c
SS
4401 return NULL;
4402}
4403
ab04a2af
TT
4404/* See breakpoint.h. */
4405
4c462cb0 4406bool
313f3b21 4407bpstat_explains_signal (bpstat *bsp, enum gdb_signal sig)
ab04a2af 4408{
ab04a2af
TT
4409 for (; bsp != NULL; bsp = bsp->next)
4410 {
427cd150
TT
4411 if (bsp->breakpoint_at == NULL)
4412 {
4413 /* A moribund location can never explain a signal other than
4414 GDB_SIGNAL_TRAP. */
4415 if (sig == GDB_SIGNAL_TRAP)
4c462cb0 4416 return true;
427cd150
TT
4417 }
4418 else
47591c29 4419 {
c47614fe 4420 if (bsp->breakpoint_at->explains_signal (sig))
4c462cb0 4421 return true;
47591c29 4422 }
ab04a2af
TT
4423 }
4424
4c462cb0 4425 return false;
ab04a2af
TT
4426}
4427
4a64f543
MS
4428/* Put in *NUM the breakpoint number of the first breakpoint we are
4429 stopped at. *BSP upon return is a bpstat which points to the
4430 remaining breakpoints stopped at (but which is not guaranteed to be
4431 good for anything but further calls to bpstat_num).
4432
8671a17b
PA
4433 Return 0 if passed a bpstat which does not indicate any breakpoints.
4434 Return -1 if stopped at a breakpoint that has been deleted since
4435 we set it.
4436 Return 1 otherwise. */
c906108c
SS
4437
4438int
313f3b21 4439bpstat_num (bpstat **bsp, int *num)
c906108c
SS
4440{
4441 struct breakpoint *b;
4442
4443 if ((*bsp) == NULL)
4444 return 0; /* No more breakpoint values */
8671a17b 4445
4a64f543
MS
4446 /* We assume we'll never have several bpstats that correspond to a
4447 single breakpoint -- otherwise, this function might return the
4448 same number more than once and this will look ugly. */
f431efe5 4449 b = (*bsp)->breakpoint_at;
8671a17b
PA
4450 *bsp = (*bsp)->next;
4451 if (b == NULL)
4452 return -1; /* breakpoint that's been deleted since */
4453
4454 *num = b->number; /* We have its number */
4455 return 1;
c906108c
SS
4456}
4457
e93ca019 4458/* See breakpoint.h. */
c906108c
SS
4459
4460void
e93ca019 4461bpstat_clear_actions (void)
c906108c 4462{
313f3b21 4463 bpstat *bs;
e93ca019 4464
00431a78 4465 if (inferior_ptid == null_ptid)
e93ca019
JK
4466 return;
4467
00431a78 4468 thread_info *tp = inferior_thread ();
e93ca019 4469 for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
c906108c 4470 {
d1b0a7bf 4471 bs->commands = NULL;
850645cf 4472 bs->old_val.reset (nullptr);
c906108c
SS
4473 }
4474}
4475
f3b1572e
PA
4476/* Called when a command is about to proceed the inferior. */
4477
4478static void
4479breakpoint_about_to_proceed (void)
4480{
d7e15655 4481 if (inferior_ptid != null_ptid)
f3b1572e
PA
4482 {
4483 struct thread_info *tp = inferior_thread ();
4484
4485 /* Allow inferior function calls in breakpoint commands to not
4486 interrupt the command list. When the call finishes
4487 successfully, the inferior will be standing at the same
4488 breakpoint as if nothing happened. */
16c381f0 4489 if (tp->control.in_infcall)
f3b1572e
PA
4490 return;
4491 }
4492
4493 breakpoint_proceeded = 1;
4494}
4495
abf85f46
JK
4496/* Return non-zero iff CMD as the first line of a command sequence is `silent'
4497 or its equivalent. */
4498
4499static int
4500command_line_is_silent (struct command_line *cmd)
4501{
4f45d445 4502 return cmd && (strcmp ("silent", cmd->line) == 0);
abf85f46
JK
4503}
4504
4a64f543
MS
4505/* Execute all the commands associated with all the breakpoints at
4506 this location. Any of these commands could cause the process to
4507 proceed beyond this point, etc. We look out for such changes by
4508 checking the global "breakpoint_proceeded" after each command.
c906108c 4509
347bddb7
PA
4510 Returns true if a breakpoint command resumed the inferior. In that
4511 case, it is the caller's responsibility to recall it again with the
4512 bpstat of the current thread. */
4513
4514static int
313f3b21 4515bpstat_do_actions_1 (bpstat **bsp)
c906108c 4516{
313f3b21 4517 bpstat *bs;
347bddb7 4518 int again = 0;
c906108c
SS
4519
4520 /* Avoid endless recursion if a `source' command is contained
4521 in bs->commands. */
4522 if (executing_breakpoint_commands)
347bddb7 4523 return 0;
c906108c 4524
81b1e71c
TT
4525 scoped_restore save_executing
4526 = make_scoped_restore (&executing_breakpoint_commands, 1);
c906108c 4527
1ac32117 4528 scoped_restore preventer = prevent_dont_repeat ();
cf6c5ffb 4529
4a64f543 4530 /* This pointer will iterate over the list of bpstat's. */
c906108c
SS
4531 bs = *bsp;
4532
4533 breakpoint_proceeded = 0;
4534 for (; bs != NULL; bs = bs->next)
4535 {
d1b0a7bf 4536 struct command_line *cmd = NULL;
6c50ab1c
JB
4537
4538 /* Take ownership of the BSP's command tree, if it has one.
4539
dda83cd7
SM
4540 The command tree could legitimately contain commands like
4541 'step' and 'next', which call clear_proceed_status, which
4542 frees stop_bpstat's command tree. To make sure this doesn't
4543 free the tree we're executing out from under us, we need to
4544 take ownership of the tree ourselves. Since a given bpstat's
4545 commands are only executed once, we don't need to copy it; we
4546 can clear the pointer in the bpstat, and make sure we free
4547 the tree when we're done. */
d1b0a7bf 4548 counted_command_line ccmd = bs->commands;
9add0f1b 4549 bs->commands = NULL;
d1b0a7bf
TT
4550 if (ccmd != NULL)
4551 cmd = ccmd.get ();
abf85f46
JK
4552 if (command_line_is_silent (cmd))
4553 {
4554 /* The action has been already done by bpstat_stop_status. */
4555 cmd = cmd->next;
4556 }
6c50ab1c 4557
c906108c
SS
4558 while (cmd != NULL)
4559 {
4560 execute_control_command (cmd);
4561
4562 if (breakpoint_proceeded)
4563 break;
4564 else
4565 cmd = cmd->next;
4566 }
6c50ab1c 4567
c906108c 4568 if (breakpoint_proceeded)
32c1e744 4569 {
cb814510 4570 if (current_ui->async)
347bddb7
PA
4571 /* If we are in async mode, then the target might be still
4572 running, not stopped at any breakpoint, so nothing for
4573 us to do here -- just return to the event loop. */
4574 ;
32c1e744
VP
4575 else
4576 /* In sync mode, when execute_control_command returns
4577 we're already standing on the next breakpoint.
347bddb7
PA
4578 Breakpoint commands for that stop were not run, since
4579 execute_command does not run breakpoint commands --
4580 only command_line_handler does, but that one is not
4581 involved in execution of breakpoint commands. So, we
4582 can now execute breakpoint commands. It should be
4583 noted that making execute_command do bpstat actions is
4584 not an option -- in this case we'll have recursive
4585 invocation of bpstat for each breakpoint with a
4586 command, and can easily blow up GDB stack. Instead, we
4587 return true, which will trigger the caller to recall us
4588 with the new stop_bpstat. */
4589 again = 1;
4590 break;
32c1e744 4591 }
c906108c 4592 }
347bddb7
PA
4593 return again;
4594}
4595
00431a78
PA
4596/* Helper for bpstat_do_actions. Get the current thread, if there's
4597 one, is alive and has execution. Return NULL otherwise. */
4598
4599static thread_info *
4600get_bpstat_thread ()
4601{
55f6301a 4602 if (inferior_ptid == null_ptid || !target_has_execution ())
00431a78
PA
4603 return NULL;
4604
4605 thread_info *tp = inferior_thread ();
611841bb 4606 if (tp->state == THREAD_EXITED || tp->executing ())
00431a78
PA
4607 return NULL;
4608 return tp;
4609}
4610
347bddb7
PA
4611void
4612bpstat_do_actions (void)
4613{
694c6bf5 4614 auto cleanup_if_error = make_scope_exit (bpstat_clear_actions);
00431a78 4615 thread_info *tp;
353d1d73 4616
347bddb7 4617 /* Do any commands attached to breakpoint we are stopped at. */
00431a78
PA
4618 while ((tp = get_bpstat_thread ()) != NULL)
4619 {
4620 /* Since in sync mode, bpstat_do_actions may resume the
4621 inferior, and only return when it is stopped at the next
4622 breakpoint, we keep doing breakpoint actions until it returns
4623 false to indicate the inferior was not resumed. */
4624 if (!bpstat_do_actions_1 (&tp->control.stop_bpstat))
4625 break;
4626 }
353d1d73 4627
694c6bf5 4628 cleanup_if_error.release ();
c906108c
SS
4629}
4630
fa4727a6
DJ
4631/* Print out the (old or new) value associated with a watchpoint. */
4632
4633static void
4634watchpoint_value_print (struct value *val, struct ui_file *stream)
4635{
4636 if (val == NULL)
7f6aba03 4637 fprintf_styled (stream, metadata_style.style (), _("<unreadable>"));
fa4727a6 4638 else
79a45b7d
TT
4639 {
4640 struct value_print_options opts;
4641 get_user_print_options (&opts);
4642 value_print (val, stream, &opts);
4643 }
fa4727a6
DJ
4644}
4645
f303dbd6
PA
4646/* Print the "Thread ID hit" part of "Thread ID hit Breakpoint N" if
4647 debugging multiple threads. */
4648
4649void
4650maybe_print_thread_hit_breakpoint (struct ui_out *uiout)
4651{
112e8700 4652 if (uiout->is_mi_like_p ())
f303dbd6
PA
4653 return;
4654
112e8700 4655 uiout->text ("\n");
f303dbd6
PA
4656
4657 if (show_thread_that_caused_stop ())
4658 {
f303dbd6
PA
4659 struct thread_info *thr = inferior_thread ();
4660
112e8700 4661 uiout->text ("Thread ");
33eca680 4662 uiout->field_string ("thread-id", print_thread_id (thr));
f303dbd6 4663
25558938 4664 const char *name = thread_name (thr);
f303dbd6
PA
4665 if (name != NULL)
4666 {
112e8700 4667 uiout->text (" \"");
33eca680 4668 uiout->field_string ("name", name);
112e8700 4669 uiout->text ("\"");
f303dbd6
PA
4670 }
4671
112e8700 4672 uiout->text (" hit ");
f303dbd6
PA
4673 }
4674}
4675
e514a9d6 4676/* Generic routine for printing messages indicating why we
4a64f543 4677 stopped. The behavior of this function depends on the value
e514a9d6
JM
4678 'print_it' in the bpstat structure. Under some circumstances we
4679 may decide not to print anything here and delegate the task to
4a64f543 4680 normal_stop(). */
e514a9d6
JM
4681
4682static enum print_stop_action
313f3b21 4683print_bp_stop_message (bpstat *bs)
e514a9d6
JM
4684{
4685 switch (bs->print_it)
4686 {
4687 case print_it_noop:
4a64f543 4688 /* Nothing should be printed for this bpstat entry. */
e514a9d6
JM
4689 return PRINT_UNKNOWN;
4690 break;
4691
4692 case print_it_done:
4693 /* We still want to print the frame, but we already printed the
dda83cd7 4694 relevant messages. */
e514a9d6
JM
4695 return PRINT_SRC_AND_LOC;
4696 break;
4697
4698 case print_it_normal:
4f8d1dc6 4699 {
f431efe5
PA
4700 struct breakpoint *b = bs->breakpoint_at;
4701
1a6a67de
TJB
4702 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
4703 which has since been deleted. */
4704 if (b == NULL)
4705 return PRINT_UNKNOWN;
4706
348d480f 4707 /* Normal case. Call the breakpoint's print_it method. */
c47614fe 4708 return b->print_it (bs);
4f8d1dc6 4709 }
348d480f 4710 break;
3086aeae 4711
e514a9d6 4712 default:
8e65ff28 4713 internal_error (__FILE__, __LINE__,
e2e0b3e5 4714 _("print_bp_stop_message: unrecognized enum value"));
e514a9d6 4715 break;
c906108c 4716 }
c906108c
SS
4717}
4718
2f9ee862 4719/* See breakpoint.h. */
edcc5120 4720
2f9ee862 4721void
4e9e993a 4722print_solib_event (bool is_catchpoint)
edcc5120 4723{
6fb16ce6 4724 bool any_deleted = !current_program_space->deleted_solibs.empty ();
bcb430e4 4725 bool any_added = !current_program_space->added_solibs.empty ();
edcc5120
TT
4726
4727 if (!is_catchpoint)
4728 {
4729 if (any_added || any_deleted)
112e8700 4730 current_uiout->text (_("Stopped due to shared library event:\n"));
edcc5120 4731 else
112e8700
SM
4732 current_uiout->text (_("Stopped due to shared library event (no "
4733 "libraries added or removed)\n"));
edcc5120
TT
4734 }
4735
112e8700
SM
4736 if (current_uiout->is_mi_like_p ())
4737 current_uiout->field_string ("reason",
4738 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
edcc5120
TT
4739
4740 if (any_deleted)
4741 {
112e8700 4742 current_uiout->text (_(" Inferior unloaded "));
10f489e5 4743 ui_out_emit_list list_emitter (current_uiout, "removed");
6fb16ce6 4744 for (int ix = 0; ix < current_program_space->deleted_solibs.size (); ix++)
edcc5120 4745 {
6fb16ce6
SM
4746 const std::string &name = current_program_space->deleted_solibs[ix];
4747
edcc5120 4748 if (ix > 0)
112e8700
SM
4749 current_uiout->text (" ");
4750 current_uiout->field_string ("library", name);
4751 current_uiout->text ("\n");
edcc5120 4752 }
edcc5120
TT
4753 }
4754
4755 if (any_added)
4756 {
112e8700 4757 current_uiout->text (_(" Inferior loaded "));
10f489e5 4758 ui_out_emit_list list_emitter (current_uiout, "added");
bcb430e4 4759 bool first = true;
52941706 4760 for (so_list *iter : current_program_space->added_solibs)
edcc5120 4761 {
bcb430e4 4762 if (!first)
112e8700 4763 current_uiout->text (" ");
bcb430e4 4764 first = false;
112e8700
SM
4765 current_uiout->field_string ("library", iter->so_name);
4766 current_uiout->text ("\n");
edcc5120 4767 }
edcc5120
TT
4768 }
4769}
4770
e514a9d6
JM
4771/* Print a message indicating what happened. This is called from
4772 normal_stop(). The input to this routine is the head of the bpstat
36dfb11c
TT
4773 list - a list of the eventpoints that caused this stop. KIND is
4774 the target_waitkind for the stopping event. This
e514a9d6
JM
4775 routine calls the generic print routine for printing a message
4776 about reasons for stopping. This will print (for example) the
4777 "Breakpoint n," part of the output. The return value of this
4778 routine is one of:
c906108c 4779
4a64f543 4780 PRINT_UNKNOWN: Means we printed nothing.
917317f4 4781 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4a64f543 4782 code to print the location. An example is
c5aa993b
JM
4783 "Breakpoint 1, " which should be followed by
4784 the location.
917317f4 4785 PRINT_SRC_ONLY: Means we printed something, but there is no need
c5aa993b
JM
4786 to also print the location part of the message.
4787 An example is the catch/throw messages, which
4a64f543 4788 don't require a location appended to the end.
917317f4 4789 PRINT_NOTHING: We have done some printing and we don't need any
4a64f543 4790 further info to be printed. */
c906108c 4791
917317f4 4792enum print_stop_action
313f3b21 4793bpstat_print (bpstat *bs, int kind)
c906108c 4794{
f486487f 4795 enum print_stop_action val;
c5aa993b 4796
c906108c 4797 /* Maybe another breakpoint in the chain caused us to stop.
53a5351d
JM
4798 (Currently all watchpoints go on the bpstat whether hit or not.
4799 That probably could (should) be changed, provided care is taken
c906108c 4800 with respect to bpstat_explains_signal). */
e514a9d6
JM
4801 for (; bs; bs = bs->next)
4802 {
4803 val = print_bp_stop_message (bs);
4804 if (val == PRINT_SRC_ONLY
4805 || val == PRINT_SRC_AND_LOC
4806 || val == PRINT_NOTHING)
4807 return val;
4808 }
c906108c 4809
36dfb11c
TT
4810 /* If we had hit a shared library event breakpoint,
4811 print_bp_stop_message would print out this message. If we hit an
4812 OS-level shared library event, do the same thing. */
4813 if (kind == TARGET_WAITKIND_LOADED)
4814 {
4e9e993a 4815 print_solib_event (false);
36dfb11c
TT
4816 return PRINT_NOTHING;
4817 }
4818
e514a9d6 4819 /* We reached the end of the chain, or we got a null BS to start
4a64f543 4820 with and nothing was printed. */
917317f4 4821 return PRINT_UNKNOWN;
c906108c
SS
4822}
4823
bf469271 4824/* Evaluate the boolean expression EXP and return the result. */
c906108c 4825
bf469271
PA
4826static bool
4827breakpoint_cond_eval (expression *exp)
c906108c 4828{
278cd55f 4829 struct value *mark = value_mark ();
bf469271 4830 bool res = value_true (evaluate_expression (exp));
cc59ec59 4831
c906108c 4832 value_free_to_mark (mark);
bf469271 4833 return res;
c906108c
SS
4834}
4835
5760d0ab 4836/* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
c906108c 4837
313f3b21 4838bpstat::bpstat (struct bp_location *bl, bpstat ***bs_link_pointer)
04afa70c 4839 : next (NULL),
b6433ede 4840 bp_location_at (bp_location_ref_ptr::new_reference (bl)),
04afa70c
TT
4841 breakpoint_at (bl->owner),
4842 commands (NULL),
04afa70c
TT
4843 print (0),
4844 stop (0),
4845 print_it (print_it_normal)
c906108c 4846{
04afa70c
TT
4847 **bs_link_pointer = this;
4848 *bs_link_pointer = &next;
4849}
4850
313f3b21 4851bpstat::bpstat ()
04afa70c 4852 : next (NULL),
04afa70c
TT
4853 breakpoint_at (NULL),
4854 commands (NULL),
04afa70c
TT
4855 print (0),
4856 stop (0),
4857 print_it (print_it_normal)
4858{
c906108c
SS
4859}
4860\f
d983da9c
DJ
4861/* The target has stopped with waitstatus WS. Check if any hardware
4862 watchpoints have triggered, according to the target. */
4863
4864int
c272a98c 4865watchpoints_triggered (const target_waitstatus &ws)
d983da9c 4866{
57810aa7 4867 bool stopped_by_watchpoint = target_stopped_by_watchpoint ();
d983da9c 4868 CORE_ADDR addr;
d983da9c
DJ
4869
4870 if (!stopped_by_watchpoint)
4871 {
4872 /* We were not stopped by a watchpoint. Mark all watchpoints
4873 as not triggered. */
43892fdf 4874 for (breakpoint *b : all_breakpoints ())
cc60f2e3 4875 if (is_hardware_watchpoint (b))
3a5c3e22
PA
4876 {
4877 struct watchpoint *w = (struct watchpoint *) b;
4878
4879 w->watchpoint_triggered = watch_triggered_no;
4880 }
d983da9c
DJ
4881
4882 return 0;
4883 }
4884
328d42d8 4885 if (!target_stopped_data_address (current_inferior ()->top_target (), &addr))
d983da9c
DJ
4886 {
4887 /* We were stopped by a watchpoint, but we don't know where.
4888 Mark all watchpoints as unknown. */
43892fdf 4889 for (breakpoint *b : all_breakpoints ())
cc60f2e3 4890 if (is_hardware_watchpoint (b))
3a5c3e22
PA
4891 {
4892 struct watchpoint *w = (struct watchpoint *) b;
4893
4894 w->watchpoint_triggered = watch_triggered_unknown;
4895 }
d983da9c 4896
3c4797ba 4897 return 1;
d983da9c
DJ
4898 }
4899
4900 /* The target could report the data address. Mark watchpoints
4901 affected by this data address as triggered, and all others as not
4902 triggered. */
4903
43892fdf 4904 for (breakpoint *b : all_breakpoints ())
cc60f2e3 4905 if (is_hardware_watchpoint (b))
d983da9c 4906 {
3a5c3e22 4907 struct watchpoint *w = (struct watchpoint *) b;
d983da9c 4908
3a5c3e22 4909 w->watchpoint_triggered = watch_triggered_no;
40cb8ca5 4910 for (bp_location *loc : b->locations ())
9c06b0b4 4911 {
3a5c3e22 4912 if (is_masked_watchpoint (b))
9c06b0b4 4913 {
3a5c3e22
PA
4914 CORE_ADDR newaddr = addr & w->hw_wp_mask;
4915 CORE_ADDR start = loc->address & w->hw_wp_mask;
9c06b0b4
TJB
4916
4917 if (newaddr == start)
4918 {
3a5c3e22 4919 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
4920 break;
4921 }
4922 }
4923 /* Exact match not required. Within range is sufficient. */
328d42d8
SM
4924 else if (target_watchpoint_addr_within_range
4925 (current_inferior ()->top_target (), addr, loc->address,
4926 loc->length))
9c06b0b4 4927 {
3a5c3e22 4928 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
4929 break;
4930 }
4931 }
d983da9c
DJ
4932 }
4933
4934 return 1;
4935}
4936
bf469271
PA
4937/* Possible return values for watchpoint_check. */
4938enum wp_check_result
4939 {
4940 /* The watchpoint has been deleted. */
4941 WP_DELETED = 1,
4942
4943 /* The value has changed. */
4944 WP_VALUE_CHANGED = 2,
4945
4946 /* The value has not changed. */
4947 WP_VALUE_NOT_CHANGED = 3,
4948
4949 /* Ignore this watchpoint, no matter if the value changed or not. */
4950 WP_IGNORE = 4,
4951 };
c906108c
SS
4952
4953#define BP_TEMPFLAG 1
4954#define BP_HARDWAREFLAG 2
4955
4a64f543 4956/* Evaluate watchpoint condition expression and check if its value
bf469271 4957 changed. */
553e4c11 4958
bf469271 4959static wp_check_result
313f3b21 4960watchpoint_check (bpstat *bs)
c906108c 4961{
3a5c3e22 4962 struct watchpoint *b;
c906108c
SS
4963 struct frame_info *fr;
4964 int within_current_scope;
4965
f431efe5 4966 /* BS is built from an existing struct breakpoint. */
2bdf28a0 4967 gdb_assert (bs->breakpoint_at != NULL);
3a5c3e22 4968 b = (struct watchpoint *) bs->breakpoint_at;
d0fb5eae 4969
f6bc2008
PA
4970 /* If this is a local watchpoint, we only want to check if the
4971 watchpoint frame is in scope if the current thread is the thread
4972 that was used to create the watchpoint. */
4973 if (!watchpoint_in_thread_scope (b))
60e1c644 4974 return WP_IGNORE;
f6bc2008 4975
c906108c
SS
4976 if (b->exp_valid_block == NULL)
4977 within_current_scope = 1;
4978 else
4979 {
edb3359d
DJ
4980 struct frame_info *frame = get_current_frame ();
4981 struct gdbarch *frame_arch = get_frame_arch (frame);
4982 CORE_ADDR frame_pc = get_frame_pc (frame);
4983
c9cf6e20 4984 /* stack_frame_destroyed_p() returns a non-zero value if we're
4a64f543
MS
4985 still in the function but the stack frame has already been
4986 invalidated. Since we can't rely on the values of local
4987 variables after the stack has been destroyed, we are treating
4988 the watchpoint in that state as `not changed' without further
4989 checking. Don't mark watchpoints as changed if the current
4990 frame is in an epilogue - even if they are in some other
4991 frame, our view of the stack is likely to be wrong and
4992 frame_find_by_id could error out. */
c9cf6e20 4993 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
60e1c644 4994 return WP_IGNORE;
a0f49112 4995
101dcfbe 4996 fr = frame_find_by_id (b->watchpoint_frame);
c906108c 4997 within_current_scope = (fr != NULL);
69fbadd5
DJ
4998
4999 /* If we've gotten confused in the unwinder, we might have
5000 returned a frame that can't describe this variable. */
edb3359d
DJ
5001 if (within_current_scope)
5002 {
5003 struct symbol *function;
5004
5005 function = get_frame_function (fr);
5006 if (function == NULL
4aeddc50 5007 || !contained_in (b->exp_valid_block, function->value_block ()))
edb3359d
DJ
5008 within_current_scope = 0;
5009 }
69fbadd5 5010
edb3359d 5011 if (within_current_scope)
c906108c
SS
5012 /* If we end up stopping, the current frame will get selected
5013 in normal_stop. So this call to select_frame won't affect
5014 the user. */
0f7d239c 5015 select_frame (fr);
c906108c 5016 }
c5aa993b 5017
c906108c
SS
5018 if (within_current_scope)
5019 {
4a64f543 5020 /* We use value_{,free_to_}mark because it could be a *long*
dda83cd7
SM
5021 time before we return to the command level and call
5022 free_all_values. We can't call free_all_values because we
5023 might be in the middle of evaluating a function call. */
c906108c 5024
9c06b0b4 5025 struct value *mark;
fa4727a6
DJ
5026 struct value *new_val;
5027
c1fc2657 5028 if (is_masked_watchpoint (b))
9c06b0b4
TJB
5029 /* Since we don't know the exact trigger address (from
5030 stopped_data_address), just tell the user we've triggered
5031 a mask watchpoint. */
5032 return WP_VALUE_CHANGED;
5033
5034 mark = value_mark ();
1eaebe02 5035 fetch_subexp_value (b->exp.get (), b->exp->op.get (), &new_val,
413403fc 5036 NULL, NULL, false);
218d2fc6 5037
bb9d5f81
PP
5038 if (b->val_bitsize != 0)
5039 new_val = extract_bitfield_from_watchpoint_value (b, new_val);
5040
4a64f543
MS
5041 /* We use value_equal_contents instead of value_equal because
5042 the latter coerces an array to a pointer, thus comparing just
5043 the address of the array instead of its contents. This is
5044 not what we want. */
fa4727a6 5045 if ((b->val != NULL) != (new_val != NULL)
850645cf
TT
5046 || (b->val != NULL && !value_equal_contents (b->val.get (),
5047 new_val)))
c906108c 5048 {
c906108c 5049 bs->old_val = b->val;
850645cf 5050 b->val = release_value (new_val);
4c1d86d9 5051 b->val_valid = true;
850645cf
TT
5052 if (new_val != NULL)
5053 value_free_to_mark (mark);
c906108c
SS
5054 return WP_VALUE_CHANGED;
5055 }
5056 else
5057 {
60e1c644 5058 /* Nothing changed. */
c906108c 5059 value_free_to_mark (mark);
c906108c
SS
5060 return WP_VALUE_NOT_CHANGED;
5061 }
5062 }
5063 else
5064 {
5065 /* This seems like the only logical thing to do because
dda83cd7
SM
5066 if we temporarily ignored the watchpoint, then when
5067 we reenter the block in which it is valid it contains
5068 garbage (in the case of a function, it may have two
5069 garbage values, one before and one after the prologue).
5070 So we can't even detect the first assignment to it and
5071 watch after that (since the garbage may or may not equal
5072 the first value assigned). */
348d480f 5073 /* We print all the stop information in
c47614fe
TT
5074 breakpointprint_it, but in this case, by the time we
5075 call breakpoint->print_it this bp will be deleted
348d480f
PA
5076 already. So we have no choice but print the information
5077 here. */
468afe6c 5078
0e454242 5079 SWITCH_THRU_ALL_UIS ()
dda83cd7 5080 {
468afe6c
PA
5081 struct ui_out *uiout = current_uiout;
5082
112e8700
SM
5083 if (uiout->is_mi_like_p ())
5084 uiout->field_string
5085 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
6a831f06
PA
5086 uiout->message ("\nWatchpoint %pF deleted because the program has "
5087 "left the block in\n"
5088 "which its expression is valid.\n",
5089 signed_field ("wpnum", b->number));
468afe6c 5090 }
4ce44c66 5091
cdac0397 5092 /* Make sure the watchpoint's commands aren't executed. */
d1b0a7bf 5093 b->commands = NULL;
d0fb5eae 5094 watchpoint_del_at_next_stop (b);
c906108c
SS
5095
5096 return WP_DELETED;
5097 }
5098}
5099
18a18393 5100/* Return true if it looks like target has stopped due to hitting
348d480f
PA
5101 breakpoint location BL. This function does not check if we should
5102 stop, only if BL explains the stop. */
5103
18a18393 5104static int
6c95b8df 5105bpstat_check_location (const struct bp_location *bl,
accd0bcd 5106 const address_space *aspace, CORE_ADDR bp_addr,
c272a98c 5107 const target_waitstatus &ws)
18a18393
VP
5108{
5109 struct breakpoint *b = bl->owner;
5110
348d480f 5111 /* BL is from an existing breakpoint. */
2bdf28a0
JK
5112 gdb_assert (b != NULL);
5113
c47614fe 5114 return b->breakpoint_hit (bl, aspace, bp_addr, ws);
18a18393
VP
5115}
5116
3a5c3e22
PA
5117/* Determine if the watched values have actually changed, and we
5118 should stop. If not, set BS->stop to 0. */
5119
18a18393 5120static void
313f3b21 5121bpstat_check_watchpoint (bpstat *bs)
18a18393 5122{
2bdf28a0 5123 const struct bp_location *bl;
3a5c3e22 5124 struct watchpoint *b;
2bdf28a0
JK
5125
5126 /* BS is built for existing struct breakpoint. */
b6433ede 5127 bl = bs->bp_location_at.get ();
2bdf28a0 5128 gdb_assert (bl != NULL);
3a5c3e22 5129 b = (struct watchpoint *) bs->breakpoint_at;
2bdf28a0 5130 gdb_assert (b != NULL);
18a18393 5131
18a18393 5132 {
18a18393
VP
5133 int must_check_value = 0;
5134
c1fc2657 5135 if (b->type == bp_watchpoint)
18a18393
VP
5136 /* For a software watchpoint, we must always check the
5137 watched value. */
5138 must_check_value = 1;
5139 else if (b->watchpoint_triggered == watch_triggered_yes)
5140 /* We have a hardware watchpoint (read, write, or access)
5141 and the target earlier reported an address watched by
5142 this watchpoint. */
5143 must_check_value = 1;
5144 else if (b->watchpoint_triggered == watch_triggered_unknown
c1fc2657 5145 && b->type == bp_hardware_watchpoint)
18a18393
VP
5146 /* We were stopped by a hardware watchpoint, but the target could
5147 not report the data address. We must check the watchpoint's
5148 value. Access and read watchpoints are out of luck; without
5149 a data address, we can't figure it out. */
5150 must_check_value = 1;
3a5c3e22 5151
18a18393
VP
5152 if (must_check_value)
5153 {
bf469271
PA
5154 wp_check_result e;
5155
a70b8144 5156 try
bf469271
PA
5157 {
5158 e = watchpoint_check (bs);
5159 }
230d2906 5160 catch (const gdb_exception &ex)
bf469271
PA
5161 {
5162 exception_fprintf (gdb_stderr, ex,
5163 "Error evaluating expression "
5164 "for watchpoint %d\n",
5165 b->number);
5166
5167 SWITCH_THRU_ALL_UIS ()
5168 {
6cb06a8c
TT
5169 gdb_printf (_("Watchpoint %d deleted.\n"),
5170 b->number);
bf469271
PA
5171 }
5172 watchpoint_del_at_next_stop (b);
5173 e = WP_DELETED;
5174 }
bf469271 5175
18a18393
VP
5176 switch (e)
5177 {
5178 case WP_DELETED:
5179 /* We've already printed what needs to be printed. */
5180 bs->print_it = print_it_done;
5181 /* Stop. */
5182 break;
60e1c644
PA
5183 case WP_IGNORE:
5184 bs->print_it = print_it_noop;
5185 bs->stop = 0;
5186 break;
18a18393 5187 case WP_VALUE_CHANGED:
c1fc2657 5188 if (b->type == bp_read_watchpoint)
18a18393 5189 {
85d721b8
PA
5190 /* There are two cases to consider here:
5191
4a64f543 5192 1. We're watching the triggered memory for reads.
85d721b8
PA
5193 In that case, trust the target, and always report
5194 the watchpoint hit to the user. Even though
5195 reads don't cause value changes, the value may
5196 have changed since the last time it was read, and
5197 since we're not trapping writes, we will not see
5198 those, and as such we should ignore our notion of
5199 old value.
5200
4a64f543 5201 2. We're watching the triggered memory for both
85d721b8
PA
5202 reads and writes. There are two ways this may
5203 happen:
5204
4a64f543 5205 2.1. This is a target that can't break on data
85d721b8
PA
5206 reads only, but can break on accesses (reads or
5207 writes), such as e.g., x86. We detect this case
5208 at the time we try to insert read watchpoints.
5209
4a64f543 5210 2.2. Otherwise, the target supports read
85d721b8
PA
5211 watchpoints, but, the user set an access or write
5212 watchpoint watching the same memory as this read
5213 watchpoint.
5214
5215 If we're watching memory writes as well as reads,
5216 ignore watchpoint hits when we find that the
5217 value hasn't changed, as reads don't cause
5218 changes. This still gives false positives when
5219 the program writes the same value to memory as
5220 what there was already in memory (we will confuse
5221 it for a read), but it's much better than
5222 nothing. */
5223
5224 int other_write_watchpoint = 0;
5225
5226 if (bl->watchpoint_type == hw_read)
5227 {
43892fdf 5228 for (breakpoint *other_b : all_breakpoints ())
3a5c3e22
PA
5229 if (other_b->type == bp_hardware_watchpoint
5230 || other_b->type == bp_access_watchpoint)
85d721b8 5231 {
3a5c3e22
PA
5232 struct watchpoint *other_w =
5233 (struct watchpoint *) other_b;
5234
5235 if (other_w->watchpoint_triggered
5236 == watch_triggered_yes)
5237 {
5238 other_write_watchpoint = 1;
5239 break;
5240 }
85d721b8
PA
5241 }
5242 }
5243
5244 if (other_write_watchpoint
5245 || bl->watchpoint_type == hw_access)
5246 {
5247 /* We're watching the same memory for writes,
5248 and the value changed since the last time we
5249 updated it, so this trap must be for a write.
5250 Ignore it. */
5251 bs->print_it = print_it_noop;
5252 bs->stop = 0;
5253 }
18a18393
VP
5254 }
5255 break;
5256 case WP_VALUE_NOT_CHANGED:
c1fc2657
SM
5257 if (b->type == bp_hardware_watchpoint
5258 || b->type == bp_watchpoint)
18a18393
VP
5259 {
5260 /* Don't stop: write watchpoints shouldn't fire if
5261 the value hasn't changed. */
5262 bs->print_it = print_it_noop;
5263 bs->stop = 0;
5264 }
5265 /* Stop. */
5266 break;
5267 default:
5268 /* Can't happen. */
18a18393
VP
5269 break;
5270 }
5271 }
5272 else /* must_check_value == 0 */
5273 {
5274 /* This is a case where some watchpoint(s) triggered, but
5275 not at the address of this watchpoint, or else no
5276 watchpoint triggered after all. So don't print
5277 anything for this watchpoint. */
5278 bs->print_it = print_it_noop;
5279 bs->stop = 0;
5280 }
5281 }
5282}
5283
7d4df6a4
DE
5284/* For breakpoints that are currently marked as telling gdb to stop,
5285 check conditions (condition proper, frame, thread and ignore count)
18a18393
VP
5286 of breakpoint referred to by BS. If we should not stop for this
5287 breakpoint, set BS->stop to 0. */
f431efe5 5288
18a18393 5289static void
313f3b21 5290bpstat_check_breakpoint_conditions (bpstat *bs, thread_info *thread)
18a18393 5291{
2bdf28a0
JK
5292 const struct bp_location *bl;
5293 struct breakpoint *b;
bf469271
PA
5294 /* Assume stop. */
5295 bool condition_result = true;
7d4df6a4
DE
5296 struct expression *cond;
5297
5298 gdb_assert (bs->stop);
2bdf28a0
JK
5299
5300 /* BS is built for existing struct breakpoint. */
b6433ede 5301 bl = bs->bp_location_at.get ();
2bdf28a0 5302 gdb_assert (bl != NULL);
f431efe5 5303 b = bs->breakpoint_at;
2bdf28a0 5304 gdb_assert (b != NULL);
18a18393 5305
b775012e
LM
5306 /* Even if the target evaluated the condition on its end and notified GDB, we
5307 need to do so again since GDB does not know if we stopped due to a
5308 breakpoint or a single step breakpoint. */
5309
18a18393 5310 if (frame_id_p (b->frame_id)
edb3359d 5311 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
18a18393 5312 {
7d4df6a4
DE
5313 bs->stop = 0;
5314 return;
5315 }
60e1c644 5316
12ab52e9
PA
5317 /* If this is a thread/task-specific breakpoint, don't waste cpu
5318 evaluating the condition if this isn't the specified
5319 thread/task. */
00431a78
PA
5320 if ((b->thread != -1 && b->thread != thread->global_num)
5321 || (b->task != 0 && b->task != ada_get_task_number (thread)))
6c1b0f7b
DE
5322 {
5323 bs->stop = 0;
5324 return;
5325 }
5326
6dddc817
DE
5327 /* Evaluate extension language breakpoints that have a "stop" method
5328 implemented. */
5329 bs->stop = breakpoint_ext_lang_cond_says_stop (b);
7371cf6d 5330
7d4df6a4
DE
5331 if (is_watchpoint (b))
5332 {
5333 struct watchpoint *w = (struct watchpoint *) b;
3a5c3e22 5334
4d01a485 5335 cond = w->cond_exp.get ();
7d4df6a4
DE
5336 }
5337 else
4d01a485 5338 cond = bl->cond.get ();
60e1c644 5339
7d4df6a4
DE
5340 if (cond && b->disposition != disp_del_at_next_stop)
5341 {
5342 int within_current_scope = 1;
5343 struct watchpoint * w;
60e1c644 5344
7d4df6a4
DE
5345 /* We use value_mark and value_free_to_mark because it could
5346 be a long time before we return to the command level and
5347 call free_all_values. We can't call free_all_values
5348 because we might be in the middle of evaluating a
5349 function call. */
5350 struct value *mark = value_mark ();
5351
5352 if (is_watchpoint (b))
5353 w = (struct watchpoint *) b;
5354 else
5355 w = NULL;
5356
5357 /* Need to select the frame, with all that implies so that
5358 the conditions will have the right context. Because we
5359 use the frame, we will not see an inlined function's
5360 variables when we arrive at a breakpoint at the start
5361 of the inlined function; the current frame will be the
5362 call site. */
5363 if (w == NULL || w->cond_exp_valid_block == NULL)
5364 select_frame (get_current_frame ());
5365 else
18a18393 5366 {
7d4df6a4
DE
5367 struct frame_info *frame;
5368
5369 /* For local watchpoint expressions, which particular
5370 instance of a local is being watched matters, so we
5371 keep track of the frame to evaluate the expression
5372 in. To evaluate the condition however, it doesn't
5373 really matter which instantiation of the function
5374 where the condition makes sense triggers the
5375 watchpoint. This allows an expression like "watch
5376 global if q > 10" set in `func', catch writes to
5377 global on all threads that call `func', or catch
5378 writes on all recursive calls of `func' by a single
5379 thread. We simply always evaluate the condition in
5380 the innermost frame that's executing where it makes
5381 sense to evaluate the condition. It seems
5382 intuitive. */
5383 frame = block_innermost_frame (w->cond_exp_valid_block);
5384 if (frame != NULL)
5385 select_frame (frame);
5386 else
5387 within_current_scope = 0;
18a18393 5388 }
7d4df6a4 5389 if (within_current_scope)
bf469271 5390 {
a70b8144 5391 try
bf469271
PA
5392 {
5393 condition_result = breakpoint_cond_eval (cond);
5394 }
230d2906 5395 catch (const gdb_exception &ex)
bf469271
PA
5396 {
5397 exception_fprintf (gdb_stderr, ex,
5398 "Error in testing breakpoint condition:\n");
5399 }
bf469271 5400 }
7d4df6a4 5401 else
18a18393 5402 {
7d4df6a4
DE
5403 warning (_("Watchpoint condition cannot be tested "
5404 "in the current scope"));
5405 /* If we failed to set the right context for this
5406 watchpoint, unconditionally report it. */
18a18393 5407 }
7d4df6a4
DE
5408 /* FIXME-someday, should give breakpoint #. */
5409 value_free_to_mark (mark);
18a18393 5410 }
7d4df6a4 5411
bf469271 5412 if (cond && !condition_result)
7d4df6a4
DE
5413 {
5414 bs->stop = 0;
5415 }
7d4df6a4
DE
5416 else if (b->ignore_count > 0)
5417 {
5418 b->ignore_count--;
5419 bs->stop = 0;
5420 /* Increase the hit count even though we don't stop. */
5421 ++(b->hit_count);
76727919 5422 gdb::observers::breakpoint_modified.notify (b);
7d4df6a4 5423 }
18a18393
VP
5424}
5425
1cf4d951
PA
5426/* Returns true if we need to track moribund locations of LOC's type
5427 on the current target. */
5428
5429static int
5430need_moribund_for_location_type (struct bp_location *loc)
5431{
5432 return ((loc->loc_type == bp_loc_software_breakpoint
5433 && !target_supports_stopped_by_sw_breakpoint ())
5434 || (loc->loc_type == bp_loc_hardware_breakpoint
5435 && !target_supports_stopped_by_hw_breakpoint ()));
5436}
5437
ddfe970e 5438/* See breakpoint.h. */
c906108c 5439
313f3b21 5440bpstat *
ddfe970e 5441build_bpstat_chain (const address_space *aspace, CORE_ADDR bp_addr,
c272a98c 5442 const target_waitstatus &ws)
c906108c 5443{
313f3b21 5444 bpstat *bs_head = nullptr, **bs_link = &bs_head;
c5aa993b 5445
43892fdf 5446 for (breakpoint *b : all_breakpoints ())
429374b8 5447 {
1a853c52 5448 if (!breakpoint_enabled (b))
429374b8 5449 continue;
a5606eee 5450
40cb8ca5 5451 for (bp_location *bl : b->locations ())
429374b8 5452 {
4a64f543
MS
5453 /* For hardware watchpoints, we look only at the first
5454 location. The watchpoint_check function will work on the
5455 entire expression, not the individual locations. For
5456 read watchpoints, the watchpoints_triggered function has
5457 checked all locations already. */
429374b8
JK
5458 if (b->type == bp_hardware_watchpoint && bl != b->loc)
5459 break;
18a18393 5460
b5fa468f 5461 if (!bl->enabled || bl->disabled_by_cond || bl->shlib_disabled)
429374b8 5462 continue;
c5aa993b 5463
09ac7c10 5464 if (!bpstat_check_location (bl, aspace, bp_addr, ws))
429374b8 5465 continue;
c5aa993b 5466
4a64f543
MS
5467 /* Come here if it's a watchpoint, or if the break address
5468 matches. */
c5aa993b 5469
313f3b21 5470 bpstat *bs = new bpstat (bl, &bs_link); /* Alloc a bpstat to
ddfe970e 5471 explain stop. */
c5aa993b 5472
f431efe5
PA
5473 /* Assume we stop. Should we find a watchpoint that is not
5474 actually triggered, or if the condition of the breakpoint
5475 evaluates as false, we'll reset 'stop' to 0. */
429374b8
JK
5476 bs->stop = 1;
5477 bs->print = 1;
d983da9c 5478
f431efe5
PA
5479 /* If this is a scope breakpoint, mark the associated
5480 watchpoint as triggered so that we will handle the
5481 out-of-scope event. We'll get to the watchpoint next
5482 iteration. */
d0fb5eae 5483 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
3a5c3e22
PA
5484 {
5485 struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
5486
5487 w->watchpoint_triggered = watch_triggered_yes;
5488 }
f431efe5
PA
5489 }
5490 }
5491
7c16b83e 5492 /* Check if a moribund breakpoint explains the stop. */
1cf4d951
PA
5493 if (!target_supports_stopped_by_sw_breakpoint ()
5494 || !target_supports_stopped_by_hw_breakpoint ())
f431efe5 5495 {
1123588c 5496 for (bp_location *loc : moribund_locations)
f431efe5 5497 {
1cf4d951
PA
5498 if (breakpoint_location_address_match (loc, aspace, bp_addr)
5499 && need_moribund_for_location_type (loc))
5500 {
313f3b21 5501 bpstat *bs = new bpstat (loc, &bs_link);
1cf4d951
PA
5502 /* For hits of moribund locations, we should just proceed. */
5503 bs->stop = 0;
5504 bs->print = 0;
5505 bs->print_it = print_it_noop;
5506 }
f431efe5
PA
5507 }
5508 }
5509
ddfe970e
KS
5510 return bs_head;
5511}
5512
5513/* See breakpoint.h. */
5514
313f3b21 5515bpstat *
ddfe970e 5516bpstat_stop_status (const address_space *aspace,
00431a78 5517 CORE_ADDR bp_addr, thread_info *thread,
c272a98c 5518 const target_waitstatus &ws,
313f3b21 5519 bpstat *stop_chain)
ddfe970e
KS
5520{
5521 struct breakpoint *b = NULL;
5522 /* First item of allocated bpstat's. */
313f3b21
SM
5523 bpstat *bs_head = stop_chain;
5524 bpstat *bs;
ddfe970e
KS
5525 int need_remove_insert;
5526 int removed_any;
5527
5528 /* First, build the bpstat chain with locations that explain a
5529 target stop, while being careful to not set the target running,
5530 as that may invalidate locations (in particular watchpoint
5531 locations are recreated). Resuming will happen here with
5532 breakpoint conditions or watchpoint expressions that include
5533 inferior function calls. */
5534 if (bs_head == NULL)
5535 bs_head = build_bpstat_chain (aspace, bp_addr, ws);
5536
edcc5120
TT
5537 /* A bit of special processing for shlib breakpoints. We need to
5538 process solib loading here, so that the lists of loaded and
5539 unloaded libraries are correct before we handle "catch load" and
5540 "catch unload". */
5541 for (bs = bs_head; bs != NULL; bs = bs->next)
5542 {
5d268276 5543 if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
edcc5120
TT
5544 {
5545 handle_solib_event ();
5546 break;
5547 }
5548 }
5549
f431efe5
PA
5550 /* Now go through the locations that caused the target to stop, and
5551 check whether we're interested in reporting this stop to higher
5552 layers, or whether we should resume the target transparently. */
5553
5554 removed_any = 0;
5555
5760d0ab 5556 for (bs = bs_head; bs != NULL; bs = bs->next)
f431efe5
PA
5557 {
5558 if (!bs->stop)
5559 continue;
5560
f431efe5 5561 b = bs->breakpoint_at;
c47614fe 5562 b->check_status (bs);
348d480f 5563 if (bs->stop)
28010a5d 5564 {
00431a78 5565 bpstat_check_breakpoint_conditions (bs, thread);
f431efe5 5566
429374b8
JK
5567 if (bs->stop)
5568 {
5569 ++(b->hit_count);
c906108c 5570
4a64f543 5571 /* We will stop here. */
429374b8
JK
5572 if (b->disposition == disp_disable)
5573 {
816338b5 5574 --(b->enable_count);
1a853c52 5575 if (b->enable_count <= 0)
429374b8 5576 b->enable_state = bp_disabled;
f431efe5 5577 removed_any = 1;
429374b8 5578 }
bd7ccaa9 5579 gdb::observers::breakpoint_modified.notify (b);
429374b8
JK
5580 if (b->silent)
5581 bs->print = 0;
5582 bs->commands = b->commands;
abf85f46 5583 if (command_line_is_silent (bs->commands
d1b0a7bf 5584 ? bs->commands.get () : NULL))
abf85f46 5585 bs->print = 0;
9d6e6e84 5586
c47614fe 5587 b->after_condition_true (bs);
429374b8
JK
5588 }
5589
348d480f 5590 }
a9b3a50f
PA
5591
5592 /* Print nothing for this entry if we don't stop or don't
5593 print. */
5594 if (!bs->stop || !bs->print)
5595 bs->print_it = print_it_noop;
429374b8 5596 }
876fa593 5597
d983da9c
DJ
5598 /* If we aren't stopping, the value of some hardware watchpoint may
5599 not have changed, but the intermediate memory locations we are
5600 watching may have. Don't bother if we're stopping; this will get
5601 done later. */
d832cb68 5602 need_remove_insert = 0;
5760d0ab
JK
5603 if (! bpstat_causes_stop (bs_head))
5604 for (bs = bs_head; bs != NULL; bs = bs->next)
d983da9c 5605 if (!bs->stop
f431efe5
PA
5606 && bs->breakpoint_at
5607 && is_hardware_watchpoint (bs->breakpoint_at))
d983da9c 5608 {
3a5c3e22
PA
5609 struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
5610
5611 update_watchpoint (w, 0 /* don't reparse. */);
d832cb68 5612 need_remove_insert = 1;
d983da9c
DJ
5613 }
5614
d832cb68 5615 if (need_remove_insert)
44702360 5616 update_global_location_list (UGLL_MAY_INSERT);
f431efe5 5617 else if (removed_any)
44702360 5618 update_global_location_list (UGLL_DONT_INSERT);
d832cb68 5619
5760d0ab 5620 return bs_head;
c906108c 5621}
628fe4e4 5622
d37e0847
PA
5623/* See breakpoint.h. */
5624
5625bpstat *
5626bpstat_stop_status_nowatch (const address_space *aspace, CORE_ADDR bp_addr,
5627 thread_info *thread, const target_waitstatus &ws)
5628{
5629 gdb_assert (!target_stopped_by_watchpoint ());
5630
5631 /* Clear all watchpoints' 'watchpoint_triggered' value from a
5632 previous stop to avoid confusing bpstat_stop_status. */
5633 watchpoints_triggered (ws);
5634
5635 return bpstat_stop_status (aspace, bp_addr, thread, ws);
5636}
5637
628fe4e4 5638static void
61c26be8 5639handle_jit_event (CORE_ADDR address)
628fe4e4 5640{
628fe4e4
JK
5641 struct gdbarch *gdbarch;
5642
1eb8556f 5643 infrun_debug_printf ("handling bp_jit_event");
243a9253 5644
628fe4e4
JK
5645 /* Switch terminal for any messages produced by
5646 breakpoint_re_set. */
223ffa71 5647 target_terminal::ours_for_output ();
628fe4e4 5648
61c26be8
MS
5649 gdbarch = get_frame_arch (get_current_frame ());
5650 /* This event is caused by a breakpoint set in `jit_breakpoint_re_set`,
5651 thus it is expected that its objectfile can be found through
5652 minimal symbol lookup. If it doesn't work (and assert fails), it
5653 most likely means that `jit_breakpoint_re_set` was changes and this
5654 function needs to be updated too. */
5655 bound_minimal_symbol jit_bp_sym = lookup_minimal_symbol_by_pc (address);
5656 gdb_assert (jit_bp_sym.objfile != nullptr);
5657 jit_event_handler (gdbarch, jit_bp_sym.objfile);
628fe4e4 5658
223ffa71 5659 target_terminal::inferior ();
628fe4e4
JK
5660}
5661
5662/* Prepare WHAT final decision for infrun. */
5663
5664/* Decide what infrun needs to do with this bpstat. */
5665
c906108c 5666struct bpstat_what
313f3b21 5667bpstat_what (bpstat *bs_head)
c906108c 5668{
c906108c 5669 struct bpstat_what retval;
313f3b21 5670 bpstat *bs;
c906108c 5671
628fe4e4 5672 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
aa7d318d 5673 retval.call_dummy = STOP_NONE;
e2d0f980 5674 retval.is_longjmp = false;
628fe4e4 5675
0e30163f 5676 for (bs = bs_head; bs != NULL; bs = bs->next)
c906108c 5677 {
628fe4e4
JK
5678 /* Extract this BS's action. After processing each BS, we check
5679 if its action overrides all we've seem so far. */
5680 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
5681 enum bptype bptype;
5682
c906108c 5683 if (bs->breakpoint_at == NULL)
628fe4e4
JK
5684 {
5685 /* I suspect this can happen if it was a momentary
5686 breakpoint which has since been deleted. */
5687 bptype = bp_none;
5688 }
20874c92 5689 else
f431efe5 5690 bptype = bs->breakpoint_at->type;
628fe4e4
JK
5691
5692 switch (bptype)
c906108c
SS
5693 {
5694 case bp_none:
628fe4e4 5695 break;
c906108c
SS
5696 case bp_breakpoint:
5697 case bp_hardware_breakpoint:
7c16b83e 5698 case bp_single_step:
c906108c
SS
5699 case bp_until:
5700 case bp_finish:
a9b3a50f 5701 case bp_shlib_event:
c906108c
SS
5702 if (bs->stop)
5703 {
5704 if (bs->print)
628fe4e4 5705 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 5706 else
628fe4e4 5707 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
5708 }
5709 else
628fe4e4 5710 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5711 break;
5712 case bp_watchpoint:
5713 case bp_hardware_watchpoint:
5714 case bp_read_watchpoint:
5715 case bp_access_watchpoint:
5716 if (bs->stop)
5717 {
5718 if (bs->print)
628fe4e4 5719 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 5720 else
628fe4e4 5721 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
5722 }
5723 else
628fe4e4
JK
5724 {
5725 /* There was a watchpoint, but we're not stopping.
5726 This requires no further action. */
5727 }
c906108c
SS
5728 break;
5729 case bp_longjmp:
e2e4d78b 5730 case bp_longjmp_call_dummy:
186c406b 5731 case bp_exception:
0a39bb32
PA
5732 if (bs->stop)
5733 {
5734 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
5735 retval.is_longjmp = bptype != bp_exception;
5736 }
5737 else
5738 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5739 break;
5740 case bp_longjmp_resume:
186c406b 5741 case bp_exception_resume:
0a39bb32
PA
5742 if (bs->stop)
5743 {
5744 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
5745 retval.is_longjmp = bptype == bp_longjmp_resume;
5746 }
5747 else
5748 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5749 break;
5750 case bp_step_resume:
5751 if (bs->stop)
628fe4e4
JK
5752 this_action = BPSTAT_WHAT_STEP_RESUME;
5753 else
c906108c 5754 {
628fe4e4
JK
5755 /* It is for the wrong frame. */
5756 this_action = BPSTAT_WHAT_SINGLE;
c906108c 5757 }
c906108c 5758 break;
2c03e5be
PA
5759 case bp_hp_step_resume:
5760 if (bs->stop)
5761 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
5762 else
5763 {
5764 /* It is for the wrong frame. */
5765 this_action = BPSTAT_WHAT_SINGLE;
5766 }
5767 break;
c906108c 5768 case bp_watchpoint_scope:
c4093a6a 5769 case bp_thread_event:
1900040c 5770 case bp_overlay_event:
0fd8e87f 5771 case bp_longjmp_master:
aa7d318d 5772 case bp_std_terminate_master:
186c406b 5773 case bp_exception_master:
628fe4e4 5774 this_action = BPSTAT_WHAT_SINGLE;
c4093a6a 5775 break;
ce78b96d 5776 case bp_catchpoint:
c5aa993b
JM
5777 if (bs->stop)
5778 {
5779 if (bs->print)
628fe4e4 5780 this_action = BPSTAT_WHAT_STOP_NOISY;
c5aa993b 5781 else
628fe4e4 5782 this_action = BPSTAT_WHAT_STOP_SILENT;
c5aa993b
JM
5783 }
5784 else
628fe4e4 5785 {
cb1e4e32
PA
5786 /* Some catchpoints are implemented with breakpoints.
5787 For those, we need to step over the breakpoint. */
6e14e441
PA
5788 if (bs->bp_location_at->loc_type == bp_loc_software_breakpoint
5789 || bs->bp_location_at->loc_type == bp_loc_hardware_breakpoint)
cb1e4e32 5790 this_action = BPSTAT_WHAT_SINGLE;
628fe4e4
JK
5791 }
5792 break;
628fe4e4 5793 case bp_jit_event:
628fe4e4 5794 this_action = BPSTAT_WHAT_SINGLE;
c5aa993b 5795 break;
c906108c 5796 case bp_call_dummy:
53a5351d
JM
5797 /* Make sure the action is stop (silent or noisy),
5798 so infrun.c pops the dummy frame. */
aa7d318d 5799 retval.call_dummy = STOP_STACK_DUMMY;
628fe4e4 5800 this_action = BPSTAT_WHAT_STOP_SILENT;
aa7d318d
TT
5801 break;
5802 case bp_std_terminate:
5803 /* Make sure the action is stop (silent or noisy),
5804 so infrun.c pops the dummy frame. */
aa7d318d 5805 retval.call_dummy = STOP_STD_TERMINATE;
628fe4e4 5806 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c 5807 break;
1042e4c0 5808 case bp_tracepoint:
7a697b8d 5809 case bp_fast_tracepoint:
0fb4aa4b 5810 case bp_static_tracepoint:
7b572efb 5811 case bp_static_marker_tracepoint:
1042e4c0
SS
5812 /* Tracepoint hits should not be reported back to GDB, and
5813 if one got through somehow, it should have been filtered
5814 out already. */
5815 internal_error (__FILE__, __LINE__,
7a697b8d 5816 _("bpstat_what: tracepoint encountered"));
0e30163f
JK
5817 break;
5818 case bp_gnu_ifunc_resolver:
5819 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
5820 this_action = BPSTAT_WHAT_SINGLE;
5821 break;
5822 case bp_gnu_ifunc_resolver_return:
5823 /* The breakpoint will be removed, execution will restart from the
5824 PC of the former breakpoint. */
5825 this_action = BPSTAT_WHAT_KEEP_CHECKING;
5826 break;
e7e0cddf
SS
5827
5828 case bp_dprintf:
a11cfd87
HZ
5829 if (bs->stop)
5830 this_action = BPSTAT_WHAT_STOP_SILENT;
5831 else
5832 this_action = BPSTAT_WHAT_SINGLE;
e7e0cddf
SS
5833 break;
5834
628fe4e4
JK
5835 default:
5836 internal_error (__FILE__, __LINE__,
5837 _("bpstat_what: unhandled bptype %d"), (int) bptype);
c906108c 5838 }
628fe4e4 5839
325fac50 5840 retval.main_action = std::max (retval.main_action, this_action);
c906108c 5841 }
628fe4e4 5842
243a9253
PA
5843 return retval;
5844}
628fe4e4 5845
243a9253 5846void
313f3b21 5847bpstat_run_callbacks (bpstat *bs_head)
243a9253 5848{
313f3b21 5849 bpstat *bs;
628fe4e4 5850
0e30163f
JK
5851 for (bs = bs_head; bs != NULL; bs = bs->next)
5852 {
5853 struct breakpoint *b = bs->breakpoint_at;
5854
5855 if (b == NULL)
5856 continue;
5857 switch (b->type)
5858 {
243a9253 5859 case bp_jit_event:
61c26be8 5860 handle_jit_event (bs->bp_location_at->address);
243a9253 5861 break;
0e30163f
JK
5862 case bp_gnu_ifunc_resolver:
5863 gnu_ifunc_resolver_stop (b);
5864 break;
5865 case bp_gnu_ifunc_resolver_return:
5866 gnu_ifunc_resolver_return_stop (b);
5867 break;
5868 }
5869 }
c906108c
SS
5870}
5871
4c462cb0 5872/* See breakpoint.h. */
c906108c 5873
4c462cb0
SM
5874bool
5875bpstat_should_step ()
c906108c 5876{
43892fdf 5877 for (breakpoint *b : all_breakpoints ())
717a8278 5878 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
4c462cb0 5879 return true;
43892fdf 5880
4c462cb0 5881 return false;
c906108c
SS
5882}
5883
4c462cb0
SM
5884/* See breakpoint.h. */
5885
5886bool
313f3b21 5887bpstat_causes_stop (bpstat *bs)
67822962
PA
5888{
5889 for (; bs != NULL; bs = bs->next)
5890 if (bs->stop)
4c462cb0 5891 return true;
67822962 5892
4c462cb0 5893 return false;
67822962
PA
5894}
5895
c906108c 5896\f
c5aa993b 5897
6c92c339 5898/* Compute a number of spaces suitable to indent the next line
170b53b2
UW
5899 so it starts at the position corresponding to the table column
5900 named COL_NAME in the currently active table of UIOUT. */
5901
6c92c339 5902static int
170b53b2
UW
5903wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
5904{
170b53b2 5905 int i, total_width, width, align;
c5209615 5906 const char *text;
170b53b2
UW
5907
5908 total_width = 0;
112e8700 5909 for (i = 1; uiout->query_table_field (i, &width, &align, &text); i++)
170b53b2
UW
5910 {
5911 if (strcmp (text, col_name) == 0)
6c92c339 5912 return total_width;
170b53b2
UW
5913
5914 total_width += width + 1;
5915 }
5916
6c92c339 5917 return 0;
170b53b2
UW
5918}
5919
b775012e
LM
5920/* Determine if the locations of this breakpoint will have their conditions
5921 evaluated by the target, host or a mix of both. Returns the following:
5922
5923 "host": Host evals condition.
5924 "host or target": Host or Target evals condition.
5925 "target": Target evals condition.
5926*/
5927
5928static const char *
a67bcaba 5929bp_condition_evaluator (const breakpoint *b)
b775012e 5930{
b775012e
LM
5931 char host_evals = 0;
5932 char target_evals = 0;
5933
5934 if (!b)
5935 return NULL;
5936
5937 if (!is_breakpoint (b))
5938 return NULL;
5939
5940 if (gdb_evaluates_breakpoint_condition_p ()
5941 || !target_supports_evaluation_of_breakpoint_conditions ())
5942 return condition_evaluation_host;
5943
40cb8ca5 5944 for (bp_location *bl : b->locations ())
b775012e
LM
5945 {
5946 if (bl->cond_bytecode)
5947 target_evals++;
5948 else
5949 host_evals++;
5950 }
5951
5952 if (host_evals && target_evals)
5953 return condition_evaluation_both;
5954 else if (target_evals)
5955 return condition_evaluation_target;
5956 else
5957 return condition_evaluation_host;
5958}
5959
5960/* Determine the breakpoint location's condition evaluator. This is
5961 similar to bp_condition_evaluator, but for locations. */
5962
5963static const char *
5964bp_location_condition_evaluator (struct bp_location *bl)
5965{
5966 if (bl && !is_breakpoint (bl->owner))
5967 return NULL;
5968
5969 if (gdb_evaluates_breakpoint_condition_p ()
5970 || !target_supports_evaluation_of_breakpoint_conditions ())
5971 return condition_evaluation_host;
5972
5973 if (bl && bl->cond_bytecode)
5974 return condition_evaluation_target;
5975 else
5976 return condition_evaluation_host;
5977}
5978
859825b8
JK
5979/* Print the LOC location out of the list of B->LOC locations. */
5980
170b53b2 5981static void
a67bcaba 5982print_breakpoint_location (const breakpoint *b,
170b53b2 5983 struct bp_location *loc)
0d381245 5984{
79a45e25 5985 struct ui_out *uiout = current_uiout;
5ed8105e
PA
5986
5987 scoped_restore_current_program_space restore_pspace;
6c95b8df 5988
859825b8
JK
5989 if (loc != NULL && loc->shlib_disabled)
5990 loc = NULL;
5991
6c95b8df
PA
5992 if (loc != NULL)
5993 set_current_program_space (loc->pspace);
5994
56435ebe 5995 if (b->display_canonical)
d28cd78a 5996 uiout->field_string ("what", event_location_to_string (b->location.get ()));
2f202fde 5997 else if (loc && loc->symtab)
0d381245 5998 {
4a27f119
KS
5999 const struct symbol *sym = loc->symbol;
6000
0d381245
VP
6001 if (sym)
6002 {
112e8700 6003 uiout->text ("in ");
987012b8 6004 uiout->field_string ("func", sym->print_name (),
e43b10e1 6005 function_name_style.style ());
112e8700
SM
6006 uiout->text (" ");
6007 uiout->wrap_hint (wrap_indent_at_field (uiout, "what"));
6008 uiout->text ("at ");
0d381245 6009 }
112e8700 6010 uiout->field_string ("file",
cbe56571 6011 symtab_to_filename_for_display (loc->symtab),
e43b10e1 6012 file_name_style.style ());
112e8700 6013 uiout->text (":");
05cba821 6014
112e8700
SM
6015 if (uiout->is_mi_like_p ())
6016 uiout->field_string ("fullname", symtab_to_fullname (loc->symtab));
0d381245 6017
381befee 6018 uiout->field_signed ("line", loc->line_number);
0d381245 6019 }
859825b8 6020 else if (loc)
0d381245 6021 {
d7e74731 6022 string_file stb;
170b53b2 6023
d7e74731 6024 print_address_symbolic (loc->gdbarch, loc->address, &stb,
22e722e1 6025 demangle, "");
112e8700 6026 uiout->field_stream ("at", stb);
0d381245 6027 }
859825b8 6028 else
f00aae0f 6029 {
d28cd78a
TT
6030 uiout->field_string ("pending",
6031 event_location_to_string (b->location.get ()));
f00aae0f
KS
6032 /* If extra_string is available, it could be holding a condition
6033 or dprintf arguments. In either case, make sure it is printed,
6034 too, but only for non-MI streams. */
112e8700 6035 if (!uiout->is_mi_like_p () && b->extra_string != NULL)
f00aae0f
KS
6036 {
6037 if (b->type == bp_dprintf)
112e8700 6038 uiout->text (",");
f00aae0f 6039 else
112e8700 6040 uiout->text (" ");
6f781ee3 6041 uiout->text (b->extra_string.get ());
f00aae0f
KS
6042 }
6043 }
6c95b8df 6044
b775012e
LM
6045 if (loc && is_breakpoint (b)
6046 && breakpoint_condition_evaluation_mode () == condition_evaluation_target
6047 && bp_condition_evaluator (b) == condition_evaluation_both)
6048 {
112e8700
SM
6049 uiout->text (" (");
6050 uiout->field_string ("evaluated-by",
b775012e 6051 bp_location_condition_evaluator (loc));
112e8700 6052 uiout->text (")");
b775012e 6053 }
0d381245
VP
6054}
6055
269b11a2
PA
6056static const char *
6057bptype_string (enum bptype type)
c906108c 6058{
c4093a6a
JM
6059 struct ep_type_description
6060 {
6061 enum bptype type;
a121b7c1 6062 const char *description;
c4093a6a
JM
6063 };
6064 static struct ep_type_description bptypes[] =
c906108c 6065 {
c5aa993b
JM
6066 {bp_none, "?deleted?"},
6067 {bp_breakpoint, "breakpoint"},
c906108c 6068 {bp_hardware_breakpoint, "hw breakpoint"},
7c16b83e 6069 {bp_single_step, "sw single-step"},
c5aa993b
JM
6070 {bp_until, "until"},
6071 {bp_finish, "finish"},
6072 {bp_watchpoint, "watchpoint"},
c906108c 6073 {bp_hardware_watchpoint, "hw watchpoint"},
c5aa993b
JM
6074 {bp_read_watchpoint, "read watchpoint"},
6075 {bp_access_watchpoint, "acc watchpoint"},
6076 {bp_longjmp, "longjmp"},
6077 {bp_longjmp_resume, "longjmp resume"},
e2e4d78b 6078 {bp_longjmp_call_dummy, "longjmp for call dummy"},
186c406b
TT
6079 {bp_exception, "exception"},
6080 {bp_exception_resume, "exception resume"},
c5aa993b 6081 {bp_step_resume, "step resume"},
2c03e5be 6082 {bp_hp_step_resume, "high-priority step resume"},
c5aa993b
JM
6083 {bp_watchpoint_scope, "watchpoint scope"},
6084 {bp_call_dummy, "call dummy"},
aa7d318d 6085 {bp_std_terminate, "std::terminate"},
c5aa993b 6086 {bp_shlib_event, "shlib events"},
c4093a6a 6087 {bp_thread_event, "thread events"},
1900040c 6088 {bp_overlay_event, "overlay events"},
0fd8e87f 6089 {bp_longjmp_master, "longjmp master"},
aa7d318d 6090 {bp_std_terminate_master, "std::terminate master"},
186c406b 6091 {bp_exception_master, "exception master"},
ce78b96d 6092 {bp_catchpoint, "catchpoint"},
1042e4c0 6093 {bp_tracepoint, "tracepoint"},
7a697b8d 6094 {bp_fast_tracepoint, "fast tracepoint"},
0fb4aa4b 6095 {bp_static_tracepoint, "static tracepoint"},
7b572efb 6096 {bp_static_marker_tracepoint, "static marker tracepoint"},
e7e0cddf 6097 {bp_dprintf, "dprintf"},
4efc6507 6098 {bp_jit_event, "jit events"},
0e30163f
JK
6099 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
6100 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
c5aa993b 6101 };
269b11a2
PA
6102
6103 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
6104 || ((int) type != bptypes[(int) type].type))
6105 internal_error (__FILE__, __LINE__,
6106 _("bptypes table does not describe type #%d."),
6107 (int) type);
6108
6109 return bptypes[(int) type].description;
6110}
6111
998580f1
MK
6112/* For MI, output a field named 'thread-groups' with a list as the value.
6113 For CLI, prefix the list with the string 'inf'. */
6114
6115static void
6116output_thread_groups (struct ui_out *uiout,
6117 const char *field_name,
5c632425 6118 const std::vector<int> &inf_nums,
998580f1
MK
6119 int mi_only)
6120{
112e8700 6121 int is_mi = uiout->is_mi_like_p ();
998580f1
MK
6122
6123 /* For backward compatibility, don't display inferiors in CLI unless
6124 there are several. Always display them for MI. */
6125 if (!is_mi && mi_only)
6126 return;
6127
10f489e5 6128 ui_out_emit_list list_emitter (uiout, field_name);
752eb8b4 6129
5c632425 6130 for (size_t i = 0; i < inf_nums.size (); i++)
998580f1
MK
6131 {
6132 if (is_mi)
6133 {
6134 char mi_group[10];
6135
5c632425 6136 xsnprintf (mi_group, sizeof (mi_group), "i%d", inf_nums[i]);
112e8700 6137 uiout->field_string (NULL, mi_group);
998580f1
MK
6138 }
6139 else
6140 {
6141 if (i == 0)
112e8700 6142 uiout->text (" inf ");
998580f1 6143 else
112e8700 6144 uiout->text (", ");
998580f1 6145
5c632425 6146 uiout->text (plongest (inf_nums[i]));
998580f1
MK
6147 }
6148 }
998580f1
MK
6149}
6150
a38118e5
PA
6151/* Print B to gdb_stdout. If RAW_LOC, print raw breakpoint locations
6152 instead of going via breakpoint_ops::print_one. This makes "maint
6153 info breakpoints" show the software breakpoint locations of
6154 catchpoints, which are considered internal implementation
c01e038b
TT
6155 detail. Returns true if RAW_LOC is false and if the breakpoint's
6156 print_one method did something; false otherwise. */
269b11a2 6157
c01e038b 6158static bool
269b11a2
PA
6159print_one_breakpoint_location (struct breakpoint *b,
6160 struct bp_location *loc,
6161 int loc_number,
6162 struct bp_location **last_loc,
a38118e5 6163 int allflag, bool raw_loc)
269b11a2
PA
6164{
6165 struct command_line *l;
c2c6d25f 6166 static char bpenables[] = "nynny";
c906108c 6167
79a45e25 6168 struct ui_out *uiout = current_uiout;
0d381245
VP
6169 int header_of_multiple = 0;
6170 int part_of_multiple = (loc != NULL);
79a45b7d
TT
6171 struct value_print_options opts;
6172
6173 get_user_print_options (&opts);
0d381245
VP
6174
6175 gdb_assert (!loc || loc_number != 0);
4a64f543
MS
6176 /* See comment in print_one_breakpoint concerning treatment of
6177 breakpoints with single disabled location. */
0d381245
VP
6178 if (loc == NULL
6179 && (b->loc != NULL
b5fa468f
TBA
6180 && (b->loc->next != NULL
6181 || !b->loc->enabled || b->loc->disabled_by_cond)))
0d381245
VP
6182 header_of_multiple = 1;
6183 if (loc == NULL)
6184 loc = b->loc;
6185
c4093a6a
JM
6186 annotate_record ();
6187
6188 /* 1 */
6189 annotate_field (0);
0d381245 6190 if (part_of_multiple)
528e1572 6191 uiout->field_fmt ("number", "%d.%d", b->number, loc_number);
0d381245 6192 else
381befee 6193 uiout->field_signed ("number", b->number);
c4093a6a
JM
6194
6195 /* 2 */
6196 annotate_field (1);
0d381245 6197 if (part_of_multiple)
112e8700 6198 uiout->field_skip ("type");
269b11a2 6199 else
112e8700 6200 uiout->field_string ("type", bptype_string (b->type));
c4093a6a
JM
6201
6202 /* 3 */
6203 annotate_field (2);
0d381245 6204 if (part_of_multiple)
112e8700 6205 uiout->field_skip ("disp");
0d381245 6206 else
112e8700 6207 uiout->field_string ("disp", bpdisp_text (b->disposition));
0d381245 6208
c4093a6a
JM
6209 /* 4 */
6210 annotate_field (3);
85c88e2a
TBA
6211 /* For locations that are disabled because of an invalid condition,
6212 display "N*" on CLI, where "*" refers to a footnote below the
6213 table. For MI, simply display a "N" without a footnote. */
6214 const char *N = (uiout->is_mi_like_p ()) ? "N" : "N*";
0d381245 6215 if (part_of_multiple)
85c88e2a 6216 uiout->field_string ("enabled", (loc->disabled_by_cond ? N
b5fa468f 6217 : (loc->enabled ? "y" : "n")));
0d381245 6218 else
112e8700 6219 uiout->field_fmt ("enabled", "%c", bpenables[(int) b->enable_state]);
0d381245 6220
c4093a6a 6221 /* 5 and 6 */
c01e038b 6222 bool result = false;
c47614fe 6223 if (!raw_loc && b->print_one (last_loc))
c01e038b 6224 result = true;
3086aeae 6225 else
a38118e5
PA
6226 {
6227 if (is_watchpoint (b))
3a5c3e22
PA
6228 {
6229 struct watchpoint *w = (struct watchpoint *) b;
6230
6231 /* Field 4, the address, is omitted (which makes the columns
6232 not line up too nicely with the headers, but the effect
6233 is relatively readable). */
6234 if (opts.addressprint)
112e8700 6235 uiout->field_skip ("addr");
3a5c3e22 6236 annotate_field (5);
a4c50be3 6237 uiout->field_string ("what", w->exp_string.get ());
3a5c3e22 6238 }
f06f1252
TT
6239 else if (!is_catchpoint (b) || is_exception_catchpoint (b)
6240 || is_ada_exception_catchpoint (b))
a38118e5
PA
6241 {
6242 if (opts.addressprint)
6243 {
6244 annotate_field (4);
6245 if (header_of_multiple)
7f6aba03
TT
6246 uiout->field_string ("addr", "<MULTIPLE>",
6247 metadata_style.style ());
a38118e5 6248 else if (b->loc == NULL || loc->shlib_disabled)
7f6aba03
TT
6249 uiout->field_string ("addr", "<PENDING>",
6250 metadata_style.style ());
a38118e5
PA
6251 else
6252 uiout->field_core_addr ("addr",
6253 loc->gdbarch, loc->address);
6254 }
6255 annotate_field (5);
6256 if (!header_of_multiple)
6257 print_breakpoint_location (b, loc);
6258 if (b->loc)
6259 *last_loc = b->loc;
6260 }
6261 }
6c95b8df 6262
998580f1 6263 if (loc != NULL && !header_of_multiple)
6c95b8df 6264 {
5c632425 6265 std::vector<int> inf_nums;
998580f1 6266 int mi_only = 1;
6c95b8df 6267
08036331 6268 for (inferior *inf : all_inferiors ())
6c95b8df
PA
6269 {
6270 if (inf->pspace == loc->pspace)
5c632425 6271 inf_nums.push_back (inf->num);
6c95b8df 6272 }
998580f1 6273
dda83cd7 6274 /* For backward compatibility, don't display inferiors in CLI unless
998580f1
MK
6275 there are several. Always display for MI. */
6276 if (allflag
6277 || (!gdbarch_has_global_breakpoints (target_gdbarch ())
94c93c35 6278 && (program_spaces.size () > 1
998580f1
MK
6279 || number_of_inferiors () > 1)
6280 /* LOC is for existing B, it cannot be in
6281 moribund_locations and thus having NULL OWNER. */
6282 && loc->owner->type != bp_catchpoint))
6283 mi_only = 0;
5c632425 6284 output_thread_groups (uiout, "thread-groups", inf_nums, mi_only);
6c95b8df
PA
6285 }
6286
4a306c9a 6287 if (!part_of_multiple)
c4093a6a 6288 {
4a306c9a
JB
6289 if (b->thread != -1)
6290 {
6291 /* FIXME: This seems to be redundant and lost here; see the
4a64f543 6292 "stop only in" line a little further down. */
112e8700 6293 uiout->text (" thread ");
381befee 6294 uiout->field_signed ("thread", b->thread);
4a306c9a
JB
6295 }
6296 else if (b->task != 0)
6297 {
112e8700 6298 uiout->text (" task ");
381befee 6299 uiout->field_signed ("task", b->task);
4a306c9a 6300 }
c4093a6a 6301 }
f1310107 6302
112e8700 6303 uiout->text ("\n");
f1310107 6304
348d480f 6305 if (!part_of_multiple)
c47614fe 6306 b->print_one_detail (uiout);
f1310107 6307
0d381245 6308 if (part_of_multiple && frame_id_p (b->frame_id))
c4093a6a
JM
6309 {
6310 annotate_field (6);
112e8700 6311 uiout->text ("\tstop only in stack frame at ");
e5dd4106 6312 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
dda83cd7 6313 the frame ID. */
112e8700 6314 uiout->field_core_addr ("frame",
5af949e3 6315 b->gdbarch, b->frame_id.stack_addr);
112e8700 6316 uiout->text ("\n");
c4093a6a
JM
6317 }
6318
28010a5d 6319 if (!part_of_multiple && b->cond_string)
c4093a6a
JM
6320 {
6321 annotate_field (7);
d77f58be 6322 if (is_tracepoint (b))
112e8700 6323 uiout->text ("\ttrace only if ");
1042e4c0 6324 else
112e8700 6325 uiout->text ("\tstop only if ");
6f781ee3 6326 uiout->field_string ("cond", b->cond_string.get ());
b775012e
LM
6327
6328 /* Print whether the target is doing the breakpoint's condition
6329 evaluation. If GDB is doing the evaluation, don't print anything. */
6330 if (is_breakpoint (b)
6331 && breakpoint_condition_evaluation_mode ()
6332 == condition_evaluation_target)
6333 {
6a831f06
PA
6334 uiout->message (" (%pF evals)",
6335 string_field ("evaluated-by",
6336 bp_condition_evaluator (b)));
b775012e 6337 }
112e8700 6338 uiout->text ("\n");
0101ce28
JJ
6339 }
6340
0d381245 6341 if (!part_of_multiple && b->thread != -1)
c4093a6a 6342 {
4a64f543 6343 /* FIXME should make an annotation for this. */
112e8700
SM
6344 uiout->text ("\tstop only in thread ");
6345 if (uiout->is_mi_like_p ())
381befee 6346 uiout->field_signed ("thread", b->thread);
5d5658a1
PA
6347 else
6348 {
6349 struct thread_info *thr = find_thread_global_id (b->thread);
6350
112e8700 6351 uiout->field_string ("thread", print_thread_id (thr));
5d5658a1 6352 }
112e8700 6353 uiout->text ("\n");
c4093a6a
JM
6354 }
6355
556ec64d
YQ
6356 if (!part_of_multiple)
6357 {
6358 if (b->hit_count)
31f56a27
YQ
6359 {
6360 /* FIXME should make an annotation for this. */
6361 if (is_catchpoint (b))
112e8700 6362 uiout->text ("\tcatchpoint");
31f56a27 6363 else if (is_tracepoint (b))
112e8700 6364 uiout->text ("\ttracepoint");
31f56a27 6365 else
112e8700
SM
6366 uiout->text ("\tbreakpoint");
6367 uiout->text (" already hit ");
381befee 6368 uiout->field_signed ("times", b->hit_count);
31f56a27 6369 if (b->hit_count == 1)
112e8700 6370 uiout->text (" time\n");
31f56a27 6371 else
112e8700 6372 uiout->text (" times\n");
31f56a27 6373 }
556ec64d
YQ
6374 else
6375 {
31f56a27 6376 /* Output the count also if it is zero, but only if this is mi. */
112e8700 6377 if (uiout->is_mi_like_p ())
381befee 6378 uiout->field_signed ("times", b->hit_count);
556ec64d
YQ
6379 }
6380 }
8b93c638 6381
0d381245 6382 if (!part_of_multiple && b->ignore_count)
c4093a6a
JM
6383 {
6384 annotate_field (8);
6a831f06
PA
6385 uiout->message ("\tignore next %pF hits\n",
6386 signed_field ("ignore", b->ignore_count));
c4093a6a 6387 }
059fb39f 6388
816338b5
SS
6389 /* Note that an enable count of 1 corresponds to "enable once"
6390 behavior, which is reported by the combination of enablement and
6391 disposition, so we don't need to mention it here. */
6392 if (!part_of_multiple && b->enable_count > 1)
6393 {
6394 annotate_field (8);
112e8700 6395 uiout->text ("\tdisable after ");
816338b5
SS
6396 /* Tweak the wording to clarify that ignore and enable counts
6397 are distinct, and have additive effect. */
6398 if (b->ignore_count)
112e8700 6399 uiout->text ("additional ");
816338b5 6400 else
112e8700 6401 uiout->text ("next ");
381befee 6402 uiout->field_signed ("enable", b->enable_count);
112e8700 6403 uiout->text (" hits\n");
816338b5
SS
6404 }
6405
f196051f
SS
6406 if (!part_of_multiple && is_tracepoint (b))
6407 {
6408 struct tracepoint *tp = (struct tracepoint *) b;
6409
6410 if (tp->traceframe_usage)
6411 {
112e8700 6412 uiout->text ("\ttrace buffer usage ");
381befee 6413 uiout->field_signed ("traceframe-usage", tp->traceframe_usage);
112e8700 6414 uiout->text (" bytes\n");
f196051f
SS
6415 }
6416 }
d3ce09f5 6417
d1b0a7bf 6418 l = b->commands ? b->commands.get () : NULL;
059fb39f 6419 if (!part_of_multiple && l)
c4093a6a
JM
6420 {
6421 annotate_field (9);
2e783024 6422 ui_out_emit_tuple tuple_emitter (uiout, "script");
8b93c638 6423 print_command_lines (uiout, l, 4);
c4093a6a 6424 }
d24317b4 6425
d9b3f62e 6426 if (is_tracepoint (b))
1042e4c0 6427 {
d9b3f62e
PA
6428 struct tracepoint *t = (struct tracepoint *) b;
6429
6430 if (!part_of_multiple && t->pass_count)
6431 {
6432 annotate_field (10);
112e8700 6433 uiout->text ("\tpass count ");
381befee 6434 uiout->field_signed ("pass", t->pass_count);
112e8700 6435 uiout->text (" \n");
d9b3f62e 6436 }
f2a8bc8a
YQ
6437
6438 /* Don't display it when tracepoint or tracepoint location is
6439 pending. */
6440 if (!header_of_multiple && loc != NULL && !loc->shlib_disabled)
6441 {
6442 annotate_field (11);
6443
112e8700
SM
6444 if (uiout->is_mi_like_p ())
6445 uiout->field_string ("installed",
f2a8bc8a
YQ
6446 loc->inserted ? "y" : "n");
6447 else
6448 {
6449 if (loc->inserted)
112e8700 6450 uiout->text ("\t");
f2a8bc8a 6451 else
112e8700
SM
6452 uiout->text ("\tnot ");
6453 uiout->text ("installed on target\n");
f2a8bc8a
YQ
6454 }
6455 }
1042e4c0
SS
6456 }
6457
112e8700 6458 if (uiout->is_mi_like_p () && !part_of_multiple)
d24317b4 6459 {
3a5c3e22
PA
6460 if (is_watchpoint (b))
6461 {
6462 struct watchpoint *w = (struct watchpoint *) b;
6463
a4c50be3 6464 uiout->field_string ("original-location", w->exp_string.get ());
3a5c3e22 6465 }
f00aae0f 6466 else if (b->location != NULL
d28cd78a 6467 && event_location_to_string (b->location.get ()) != NULL)
112e8700 6468 uiout->field_string ("original-location",
d28cd78a 6469 event_location_to_string (b->location.get ()));
d24317b4 6470 }
c01e038b
TT
6471
6472 return result;
c4093a6a 6473}
c5aa993b 6474
13674803
SM
6475/* See breakpoint.h. */
6476
6477bool fix_multi_location_breakpoint_output_globally = false;
6478
0d381245
VP
6479static void
6480print_one_breakpoint (struct breakpoint *b,
4a64f543 6481 struct bp_location **last_loc,
6c95b8df 6482 int allflag)
0d381245 6483{
79a45e25 6484 struct ui_out *uiout = current_uiout;
13674803
SM
6485 bool use_fixed_output
6486 = (uiout->test_flags (fix_multi_location_breakpoint_output)
6487 || fix_multi_location_breakpoint_output_globally);
8d3788bd 6488
b4be1b06 6489 gdb::optional<ui_out_emit_tuple> bkpt_tuple_emitter (gdb::in_place, uiout, "bkpt");
c01e038b
TT
6490 bool printed = print_one_breakpoint_location (b, NULL, 0, last_loc,
6491 allflag, false);
8d3788bd 6492
b4be1b06
SM
6493 /* The mi2 broken format: the main breakpoint tuple ends here, the locations
6494 are outside. */
6495 if (!use_fixed_output)
6496 bkpt_tuple_emitter.reset ();
0d381245
VP
6497
6498 /* If this breakpoint has custom print function,
6499 it's already printed. Otherwise, print individual
6500 locations, if any. */
c01e038b 6501 if (!printed || allflag)
0d381245 6502 {
4a64f543
MS
6503 /* If breakpoint has a single location that is disabled, we
6504 print it as if it had several locations, since otherwise it's
6505 hard to represent "breakpoint enabled, location disabled"
6506 situation.
6507
6508 Note that while hardware watchpoints have several locations
a38118e5
PA
6509 internally, that's not a property exposed to users.
6510
6511 Likewise, while catchpoints may be implemented with
6512 breakpoints (e.g., catch throw), that's not a property
6513 exposed to users. We do however display the internal
6514 breakpoint locations with "maint info breakpoints". */
6515 if (!is_hardware_watchpoint (b)
f06f1252
TT
6516 && (!is_catchpoint (b) || is_exception_catchpoint (b)
6517 || is_ada_exception_catchpoint (b))
a38118e5 6518 && (allflag
b5fa468f
TBA
6519 || (b->loc && (b->loc->next
6520 || !b->loc->enabled
6521 || b->loc->disabled_by_cond))))
0d381245 6522 {
b4be1b06
SM
6523 gdb::optional<ui_out_emit_list> locations_list;
6524
6525 /* For MI version <= 2, keep the behavior where GDB outputs an invalid
6526 MI record. For later versions, place breakpoint locations in a
6527 list. */
6528 if (uiout->is_mi_like_p () && use_fixed_output)
6529 locations_list.emplace (uiout, "locations");
8d3788bd 6530
b4be1b06 6531 int n = 1;
40cb8ca5 6532 for (bp_location *loc : b->locations ())
8d3788bd 6533 {
b4be1b06 6534 ui_out_emit_tuple loc_tuple_emitter (uiout, NULL);
a38118e5
PA
6535 print_one_breakpoint_location (b, loc, n, last_loc,
6536 allflag, allflag);
40cb8ca5 6537 n++;
8d3788bd 6538 }
0d381245
VP
6539 }
6540 }
6541}
6542
a6d9a66e
UW
6543static int
6544breakpoint_address_bits (struct breakpoint *b)
6545{
6546 int print_address_bits = 0;
a6d9a66e 6547
40cb8ca5 6548 for (bp_location *loc : b->locations ())
a6d9a66e 6549 {
6e14e441
PA
6550 if (!bl_address_is_meaningful (loc))
6551 continue;
c7437ca6 6552
6e14e441 6553 int addr_bit = gdbarch_addr_bit (loc->gdbarch);
a6d9a66e
UW
6554 if (addr_bit > print_address_bits)
6555 print_address_bits = addr_bit;
6556 }
6557
6558 return print_address_bits;
6559}
0d381245 6560
65630365 6561/* See breakpoint.h. */
c5aa993b 6562
65630365
PA
6563void
6564print_breakpoint (breakpoint *b)
c4093a6a 6565{
a6d9a66e 6566 struct bp_location *dummy_loc = NULL;
65630365 6567 print_one_breakpoint (b, &dummy_loc, 0);
c4093a6a 6568}
c5aa993b 6569
09d682a4
TT
6570/* Return true if this breakpoint was set by the user, false if it is
6571 internal or momentary. */
6572
6573int
6574user_breakpoint_p (struct breakpoint *b)
6575{
46c6471b 6576 return b->number > 0;
09d682a4
TT
6577}
6578
93daf339
TT
6579/* See breakpoint.h. */
6580
6581int
6582pending_breakpoint_p (struct breakpoint *b)
6583{
6584 return b->loc == NULL;
6585}
6586
5c458ae8
SM
6587/* Print information on breakpoints (including watchpoints and tracepoints).
6588
6589 If non-NULL, BP_NUM_LIST is a list of numbers and number ranges as
6590 understood by number_or_range_parser. Only breakpoints included in this
6591 list are then printed.
6592
6593 If SHOW_INTERNAL is true, print internal breakpoints.
6594
6595 If FILTER is non-NULL, call it on each breakpoint and only include the
6596 ones for which it returns true.
6597
6598 Return the total number of breakpoints listed. */
c906108c 6599
d77f58be 6600static int
5c458ae8 6601breakpoint_1 (const char *bp_num_list, bool show_internal,
f2478a7e 6602 bool (*filter) (const struct breakpoint *))
c4093a6a 6603{
a6d9a66e 6604 struct bp_location *last_loc = NULL;
7f3b0473 6605 int nr_printable_breakpoints;
79a45b7d 6606 struct value_print_options opts;
a6d9a66e 6607 int print_address_bits = 0;
269b11a2 6608 int print_type_col_width = 14;
79a45e25 6609 struct ui_out *uiout = current_uiout;
b5fa468f 6610 bool has_disabled_by_cond_location = false;
269b11a2 6611
79a45b7d
TT
6612 get_user_print_options (&opts);
6613
4a64f543
MS
6614 /* Compute the number of rows in the table, as well as the size
6615 required for address fields. */
7f3b0473 6616 nr_printable_breakpoints = 0;
43892fdf 6617 for (breakpoint *b : all_breakpoints ())
e5a67952
MS
6618 {
6619 /* If we have a filter, only list the breakpoints it accepts. */
6620 if (filter && !filter (b))
6621 continue;
6622
5c458ae8 6623 /* If we have a BP_NUM_LIST string, it is a list of breakpoints to
e5a67952 6624 accept. Skip the others. */
5c458ae8 6625 if (bp_num_list != NULL && *bp_num_list != '\0')
e5a67952 6626 {
5c458ae8 6627 if (show_internal && parse_and_eval_long (bp_num_list) != b->number)
e5a67952 6628 continue;
5c458ae8 6629 if (!show_internal && !number_is_in_list (bp_num_list, b->number))
e5a67952
MS
6630 continue;
6631 }
269b11a2 6632
5c458ae8 6633 if (show_internal || user_breakpoint_p (b))
e5a67952
MS
6634 {
6635 int addr_bit, type_len;
a6d9a66e 6636
e5a67952
MS
6637 addr_bit = breakpoint_address_bits (b);
6638 if (addr_bit > print_address_bits)
6639 print_address_bits = addr_bit;
269b11a2 6640
e5a67952
MS
6641 type_len = strlen (bptype_string (b->type));
6642 if (type_len > print_type_col_width)
6643 print_type_col_width = type_len;
6644
6645 nr_printable_breakpoints++;
6646 }
6647 }
7f3b0473 6648
4a2b031d
TT
6649 {
6650 ui_out_emit_table table_emitter (uiout,
6651 opts.addressprint ? 6 : 5,
6652 nr_printable_breakpoints,
6653 "BreakpointTable");
6654
6655 if (nr_printable_breakpoints > 0)
6656 annotate_breakpoints_headers ();
6657 if (nr_printable_breakpoints > 0)
6658 annotate_field (0);
6659 uiout->table_header (7, ui_left, "number", "Num"); /* 1 */
6660 if (nr_printable_breakpoints > 0)
6661 annotate_field (1);
6662 uiout->table_header (print_type_col_width, ui_left, "type", "Type"); /* 2 */
6663 if (nr_printable_breakpoints > 0)
6664 annotate_field (2);
6665 uiout->table_header (4, ui_left, "disp", "Disp"); /* 3 */
6666 if (nr_printable_breakpoints > 0)
6667 annotate_field (3);
6668 uiout->table_header (3, ui_left, "enabled", "Enb"); /* 4 */
6669 if (opts.addressprint)
6670 {
6671 if (nr_printable_breakpoints > 0)
6672 annotate_field (4);
6673 if (print_address_bits <= 32)
6674 uiout->table_header (10, ui_left, "addr", "Address"); /* 5 */
6675 else
6676 uiout->table_header (18, ui_left, "addr", "Address"); /* 5 */
6677 }
6678 if (nr_printable_breakpoints > 0)
6679 annotate_field (5);
6680 uiout->table_header (40, ui_noalign, "what", "What"); /* 6 */
6681 uiout->table_body ();
6682 if (nr_printable_breakpoints > 0)
6683 annotate_breakpoints_table ();
6684
43892fdf 6685 for (breakpoint *b : all_breakpoints ())
4a2b031d
TT
6686 {
6687 QUIT;
6688 /* If we have a filter, only list the breakpoints it accepts. */
6689 if (filter && !filter (b))
6690 continue;
e5a67952 6691
5c458ae8 6692 /* If we have a BP_NUM_LIST string, it is a list of breakpoints to
4a2b031d 6693 accept. Skip the others. */
e5a67952 6694
5c458ae8 6695 if (bp_num_list != NULL && *bp_num_list != '\0')
4a2b031d 6696 {
5c458ae8 6697 if (show_internal) /* maintenance info breakpoint */
4a2b031d 6698 {
5c458ae8 6699 if (parse_and_eval_long (bp_num_list) != b->number)
4a2b031d
TT
6700 continue;
6701 }
6702 else /* all others */
6703 {
5c458ae8 6704 if (!number_is_in_list (bp_num_list, b->number))
4a2b031d
TT
6705 continue;
6706 }
6707 }
6708 /* We only print out user settable breakpoints unless the
5c458ae8
SM
6709 show_internal is set. */
6710 if (show_internal || user_breakpoint_p (b))
b5fa468f
TBA
6711 {
6712 print_one_breakpoint (b, &last_loc, show_internal);
40cb8ca5 6713 for (bp_location *loc : b->locations ())
b5fa468f
TBA
6714 if (loc->disabled_by_cond)
6715 has_disabled_by_cond_location = true;
6716 }
4a2b031d
TT
6717 }
6718 }
698384cd 6719
7f3b0473 6720 if (nr_printable_breakpoints == 0)
c906108c 6721 {
4a64f543
MS
6722 /* If there's a filter, let the caller decide how to report
6723 empty list. */
d77f58be
SS
6724 if (!filter)
6725 {
5c458ae8 6726 if (bp_num_list == NULL || *bp_num_list == '\0')
112e8700 6727 uiout->message ("No breakpoints or watchpoints.\n");
d77f58be 6728 else
112e8700 6729 uiout->message ("No breakpoint or watchpoint matching '%s'.\n",
5c458ae8 6730 bp_num_list);
d77f58be 6731 }
c906108c
SS
6732 }
6733 else
c4093a6a 6734 {
a6d9a66e
UW
6735 if (last_loc && !server_command)
6736 set_next_address (last_loc->gdbarch, last_loc->address);
b5fa468f 6737
85c88e2a 6738 if (has_disabled_by_cond_location && !uiout->is_mi_like_p ())
b5fa468f
TBA
6739 uiout->message (_("(*): Breakpoint condition is invalid at this "
6740 "location.\n"));
c4093a6a 6741 }
c906108c 6742
4a64f543 6743 /* FIXME? Should this be moved up so that it is only called when
c4093a6a 6744 there have been breakpoints? */
c906108c 6745 annotate_breakpoints_table_end ();
d77f58be
SS
6746
6747 return nr_printable_breakpoints;
c906108c
SS
6748}
6749
ad443146
SS
6750/* Display the value of default-collect in a way that is generally
6751 compatible with the breakpoint list. */
6752
6753static void
6754default_collect_info (void)
6755{
79a45e25
PA
6756 struct ui_out *uiout = current_uiout;
6757
ad443146
SS
6758 /* If it has no value (which is frequently the case), say nothing; a
6759 message like "No default-collect." gets in user's face when it's
6760 not wanted. */
e0700ba4 6761 if (default_collect.empty ())
ad443146
SS
6762 return;
6763
6764 /* The following phrase lines up nicely with per-tracepoint collect
6765 actions. */
112e8700
SM
6766 uiout->text ("default collect ");
6767 uiout->field_string ("default-collect", default_collect);
6768 uiout->text (" \n");
ad443146
SS
6769}
6770
c906108c 6771static void
0b39b52e 6772info_breakpoints_command (const char *args, int from_tty)
c906108c 6773{
5c458ae8 6774 breakpoint_1 (args, false, NULL);
ad443146
SS
6775
6776 default_collect_info ();
d77f58be
SS
6777}
6778
6779static void
1d12d88f 6780info_watchpoints_command (const char *args, int from_tty)
d77f58be 6781{
5c458ae8 6782 int num_printed = breakpoint_1 (args, false, is_watchpoint);
79a45e25 6783 struct ui_out *uiout = current_uiout;
d77f58be
SS
6784
6785 if (num_printed == 0)
6786 {
e5a67952 6787 if (args == NULL || *args == '\0')
112e8700 6788 uiout->message ("No watchpoints.\n");
d77f58be 6789 else
112e8700 6790 uiout->message ("No watchpoint matching '%s'.\n", args);
d77f58be 6791 }
c906108c
SS
6792}
6793
7a292a7a 6794static void
4495129a 6795maintenance_info_breakpoints (const char *args, int from_tty)
c906108c 6796{
5c458ae8 6797 breakpoint_1 (args, true, NULL);
ad443146
SS
6798
6799 default_collect_info ();
c906108c
SS
6800}
6801
0d381245 6802static int
714835d5 6803breakpoint_has_pc (struct breakpoint *b,
6c95b8df 6804 struct program_space *pspace,
714835d5 6805 CORE_ADDR pc, struct obj_section *section)
0d381245 6806{
40cb8ca5 6807 for (bp_location *bl : b->locations ())
0d381245 6808 {
6c95b8df
PA
6809 if (bl->pspace == pspace
6810 && bl->address == pc
0d381245
VP
6811 && (!overlay_debugging || bl->section == section))
6812 return 1;
6813 }
6814 return 0;
6815}
6816
bd21b6c9 6817/* See breakpoint.h. */
c906108c 6818
bd21b6c9 6819void
6c95b8df
PA
6820describe_other_breakpoints (struct gdbarch *gdbarch,
6821 struct program_space *pspace, CORE_ADDR pc,
5af949e3 6822 struct obj_section *section, int thread)
c906108c 6823{
52f0bd74 6824 int others = 0;
c906108c 6825
43892fdf 6826 for (breakpoint *b : all_breakpoints ())
672f9b60 6827 others += (user_breakpoint_p (b)
dda83cd7 6828 && breakpoint_has_pc (b, pspace, pc, section));
43892fdf 6829
c906108c
SS
6830 if (others > 0)
6831 {
a3f17187 6832 if (others == 1)
6cb06a8c 6833 gdb_printf (_("Note: breakpoint "));
a3f17187 6834 else /* if (others == ???) */
6cb06a8c 6835 gdb_printf (_("Note: breakpoints "));
43892fdf 6836 for (breakpoint *b : all_breakpoints ())
672f9b60 6837 if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
0d381245
VP
6838 {
6839 others--;
6cb06a8c 6840 gdb_printf ("%d", b->number);
0d381245 6841 if (b->thread == -1 && thread != -1)
6cb06a8c 6842 gdb_printf (" (all threads)");
0d381245 6843 else if (b->thread != -1)
6cb06a8c
TT
6844 gdb_printf (" (thread %d)", b->thread);
6845 gdb_printf ("%s%s ",
6846 ((b->enable_state == bp_disabled
6847 || b->enable_state == bp_call_disabled)
6848 ? " (disabled)"
6849 : ""),
6850 (others > 1) ? ","
6851 : ((others == 1) ? " and" : ""));
0d381245 6852 }
6a831f06
PA
6853 current_uiout->message (_("also set at pc %ps.\n"),
6854 styled_string (address_style.style (),
6855 paddress (gdbarch, pc)));
c906108c
SS
6856 }
6857}
6858\f
c906108c 6859
cb1e4e32
PA
6860/* Return true iff it is meaningful to use the address member of LOC.
6861 For some breakpoint types, the locations' address members are
6862 irrelevant and it makes no sense to attempt to compare them to
6863 other addresses (or use them for any other purpose either).
2d134ed3 6864
cb1e4e32
PA
6865 More specifically, software watchpoints and catchpoints that are
6866 not backed by breakpoints always have a zero valued location
6867 address and we don't want to mark breakpoints of any of these types
6868 to be a duplicate of an actual breakpoint location at address
6869 zero. */
e4f237da 6870
cb1e4e32
PA
6871static bool
6872bl_address_is_meaningful (bp_location *loc)
e4f237da 6873{
cb1e4e32 6874 return loc->loc_type != bp_loc_other;
2d134ed3
PA
6875}
6876
6877/* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
6878 true if LOC1 and LOC2 represent the same watchpoint location. */
6879
6880static int
4a64f543
MS
6881watchpoint_locations_match (struct bp_location *loc1,
6882 struct bp_location *loc2)
2d134ed3 6883{
3a5c3e22
PA
6884 struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
6885 struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
6886
6887 /* Both of them must exist. */
6888 gdb_assert (w1 != NULL);
6889 gdb_assert (w2 != NULL);
2bdf28a0 6890
4a64f543
MS
6891 /* If the target can evaluate the condition expression in hardware,
6892 then we we need to insert both watchpoints even if they are at
6893 the same place. Otherwise the watchpoint will only trigger when
6894 the condition of whichever watchpoint was inserted evaluates to
6895 true, not giving a chance for GDB to check the condition of the
6896 other watchpoint. */
3a5c3e22 6897 if ((w1->cond_exp
4a64f543
MS
6898 && target_can_accel_watchpoint_condition (loc1->address,
6899 loc1->length,
0cf6dd15 6900 loc1->watchpoint_type,
4d01a485 6901 w1->cond_exp.get ()))
3a5c3e22 6902 || (w2->cond_exp
4a64f543
MS
6903 && target_can_accel_watchpoint_condition (loc2->address,
6904 loc2->length,
0cf6dd15 6905 loc2->watchpoint_type,
4d01a485 6906 w2->cond_exp.get ())))
0cf6dd15
TJB
6907 return 0;
6908
85d721b8
PA
6909 /* Note that this checks the owner's type, not the location's. In
6910 case the target does not support read watchpoints, but does
6911 support access watchpoints, we'll have bp_read_watchpoint
6912 watchpoints with hw_access locations. Those should be considered
6913 duplicates of hw_read locations. The hw_read locations will
6914 become hw_access locations later. */
2d134ed3
PA
6915 return (loc1->owner->type == loc2->owner->type
6916 && loc1->pspace->aspace == loc2->pspace->aspace
6917 && loc1->address == loc2->address
6918 && loc1->length == loc2->length);
e4f237da
KB
6919}
6920
31e77af2 6921/* See breakpoint.h. */
6c95b8df 6922
31e77af2 6923int
accd0bcd
YQ
6924breakpoint_address_match (const address_space *aspace1, CORE_ADDR addr1,
6925 const address_space *aspace2, CORE_ADDR addr2)
6c95b8df 6926{
f5656ead 6927 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
6c95b8df
PA
6928 || aspace1 == aspace2)
6929 && addr1 == addr2);
6930}
6931
f1310107
TJB
6932/* Returns true if {ASPACE2,ADDR2} falls within the range determined by
6933 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
6934 matches ASPACE2. On targets that have global breakpoints, the address
6935 space doesn't really matter. */
6936
6937static int
accd0bcd
YQ
6938breakpoint_address_match_range (const address_space *aspace1,
6939 CORE_ADDR addr1,
6940 int len1, const address_space *aspace2,
f1310107
TJB
6941 CORE_ADDR addr2)
6942{
f5656ead 6943 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
f1310107
TJB
6944 || aspace1 == aspace2)
6945 && addr2 >= addr1 && addr2 < addr1 + len1);
6946}
6947
6948/* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
6949 a ranged breakpoint. In most targets, a match happens only if ASPACE
6950 matches the breakpoint's address space. On targets that have global
6951 breakpoints, the address space doesn't really matter. */
6952
6953static int
6954breakpoint_location_address_match (struct bp_location *bl,
accd0bcd 6955 const address_space *aspace,
f1310107
TJB
6956 CORE_ADDR addr)
6957{
6958 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
6959 aspace, addr)
6960 || (bl->length
6961 && breakpoint_address_match_range (bl->pspace->aspace,
6962 bl->address, bl->length,
6963 aspace, addr)));
6964}
6965
d35ae833
PA
6966/* Returns true if the [ADDR,ADDR+LEN) range in ASPACE overlaps
6967 breakpoint BL. BL may be a ranged breakpoint. In most targets, a
6968 match happens only if ASPACE matches the breakpoint's address
6969 space. On targets that have global breakpoints, the address space
6970 doesn't really matter. */
6971
6972static int
6973breakpoint_location_address_range_overlap (struct bp_location *bl,
accd0bcd 6974 const address_space *aspace,
d35ae833
PA
6975 CORE_ADDR addr, int len)
6976{
6977 if (gdbarch_has_global_breakpoints (target_gdbarch ())
6978 || bl->pspace->aspace == aspace)
6979 {
6980 int bl_len = bl->length != 0 ? bl->length : 1;
6981
6982 if (mem_ranges_overlap (addr, len, bl->address, bl_len))
6983 return 1;
6984 }
6985 return 0;
6986}
6987
1e4d1764
YQ
6988/* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
6989 Then, if LOC1 and LOC2 represent the same tracepoint location, returns
6990 true, otherwise returns false. */
6991
6992static int
6993tracepoint_locations_match (struct bp_location *loc1,
6994 struct bp_location *loc2)
6995{
6996 if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
6997 /* Since tracepoint locations are never duplicated with others', tracepoint
6998 locations at the same address of different tracepoints are regarded as
6999 different locations. */
7000 return (loc1->address == loc2->address && loc1->owner == loc2->owner);
7001 else
7002 return 0;
7003}
7004
2d134ed3 7005/* Assuming LOC1 and LOC2's types' have meaningful target addresses
cb1e4e32 7006 (bl_address_is_meaningful), returns true if LOC1 and LOC2 represent
7f32a4d5
PA
7007 the same location. If SW_HW_BPS_MATCH is true, then software
7008 breakpoint locations and hardware breakpoint locations match,
7009 otherwise they don't. */
2d134ed3
PA
7010
7011static int
7f32a4d5
PA
7012breakpoint_locations_match (struct bp_location *loc1,
7013 struct bp_location *loc2,
7014 bool sw_hw_bps_match)
2d134ed3 7015{
2bdf28a0
JK
7016 int hw_point1, hw_point2;
7017
7018 /* Both of them must not be in moribund_locations. */
7019 gdb_assert (loc1->owner != NULL);
7020 gdb_assert (loc2->owner != NULL);
7021
7022 hw_point1 = is_hardware_watchpoint (loc1->owner);
7023 hw_point2 = is_hardware_watchpoint (loc2->owner);
2d134ed3
PA
7024
7025 if (hw_point1 != hw_point2)
7026 return 0;
7027 else if (hw_point1)
7028 return watchpoint_locations_match (loc1, loc2);
1e4d1764
YQ
7029 else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
7030 return tracepoint_locations_match (loc1, loc2);
2d134ed3 7031 else
7f32a4d5
PA
7032 /* We compare bp_location.length in order to cover ranged
7033 breakpoints. Keep this in sync with
7034 bp_location_is_less_than. */
f1310107
TJB
7035 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
7036 loc2->pspace->aspace, loc2->address)
7f32a4d5 7037 && (loc1->loc_type == loc2->loc_type || sw_hw_bps_match)
f1310107 7038 && loc1->length == loc2->length);
2d134ed3
PA
7039}
7040
76897487
KB
7041static void
7042breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
dda83cd7 7043 int bnum, int have_bnum)
76897487 7044{
f63fbe86
MS
7045 /* The longest string possibly returned by hex_string_custom
7046 is 50 chars. These must be at least that big for safety. */
7047 char astr1[64];
7048 char astr2[64];
76897487 7049
bb599908
PH
7050 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
7051 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
76897487 7052 if (have_bnum)
8a3fe4f8 7053 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
dda83cd7 7054 bnum, astr1, astr2);
76897487 7055 else
8a3fe4f8 7056 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
76897487
KB
7057}
7058
4a64f543
MS
7059/* Adjust a breakpoint's address to account for architectural
7060 constraints on breakpoint placement. Return the adjusted address.
7061 Note: Very few targets require this kind of adjustment. For most
7062 targets, this function is simply the identity function. */
76897487
KB
7063
7064static CORE_ADDR
a6d9a66e
UW
7065adjust_breakpoint_address (struct gdbarch *gdbarch,
7066 CORE_ADDR bpaddr, enum bptype bptype)
76897487 7067{
a0de8c21
YQ
7068 if (bptype == bp_watchpoint
7069 || bptype == bp_hardware_watchpoint
7070 || bptype == bp_read_watchpoint
7071 || bptype == bp_access_watchpoint
7072 || bptype == bp_catchpoint)
88f7da05
KB
7073 {
7074 /* Watchpoints and the various bp_catch_* eventpoints should not
dda83cd7 7075 have their addresses modified. */
88f7da05
KB
7076 return bpaddr;
7077 }
7c16b83e
PA
7078 else if (bptype == bp_single_step)
7079 {
7080 /* Single-step breakpoints should not have their addresses
7081 modified. If there's any architectural constrain that
7082 applies to this address, then it should have already been
7083 taken into account when the breakpoint was created in the
7084 first place. If we didn't do this, stepping through e.g.,
7085 Thumb-2 IT blocks would break. */
7086 return bpaddr;
7087 }
76897487
KB
7088 else
7089 {
a0de8c21
YQ
7090 CORE_ADDR adjusted_bpaddr = bpaddr;
7091
7092 if (gdbarch_adjust_breakpoint_address_p (gdbarch))
7093 {
7094 /* Some targets have architectural constraints on the placement
7095 of breakpoint instructions. Obtain the adjusted address. */
7096 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
7097 }
76897487 7098
a0de8c21 7099 adjusted_bpaddr = address_significant (gdbarch, adjusted_bpaddr);
76897487
KB
7100
7101 /* An adjusted breakpoint address can significantly alter
dda83cd7 7102 a user's expectations. Print a warning if an adjustment
76897487
KB
7103 is required. */
7104 if (adjusted_bpaddr != bpaddr)
7105 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
7106
7107 return adjusted_bpaddr;
7108 }
7109}
7110
cb1e4e32
PA
7111static bp_loc_type
7112bp_location_from_bp_type (bptype type)
7cc221ef 7113{
cb1e4e32 7114 switch (type)
e049a4b5
DJ
7115 {
7116 case bp_breakpoint:
7c16b83e 7117 case bp_single_step:
e049a4b5
DJ
7118 case bp_until:
7119 case bp_finish:
7120 case bp_longjmp:
7121 case bp_longjmp_resume:
e2e4d78b 7122 case bp_longjmp_call_dummy:
186c406b
TT
7123 case bp_exception:
7124 case bp_exception_resume:
e049a4b5 7125 case bp_step_resume:
2c03e5be 7126 case bp_hp_step_resume:
e049a4b5
DJ
7127 case bp_watchpoint_scope:
7128 case bp_call_dummy:
aa7d318d 7129 case bp_std_terminate:
e049a4b5
DJ
7130 case bp_shlib_event:
7131 case bp_thread_event:
7132 case bp_overlay_event:
4efc6507 7133 case bp_jit_event:
0fd8e87f 7134 case bp_longjmp_master:
aa7d318d 7135 case bp_std_terminate_master:
186c406b 7136 case bp_exception_master:
0e30163f
JK
7137 case bp_gnu_ifunc_resolver:
7138 case bp_gnu_ifunc_resolver_return:
e7e0cddf 7139 case bp_dprintf:
cb1e4e32 7140 return bp_loc_software_breakpoint;
e049a4b5 7141 case bp_hardware_breakpoint:
cb1e4e32 7142 return bp_loc_hardware_breakpoint;
e049a4b5
DJ
7143 case bp_hardware_watchpoint:
7144 case bp_read_watchpoint:
7145 case bp_access_watchpoint:
cb1e4e32 7146 return bp_loc_hardware_watchpoint;
e049a4b5 7147 case bp_watchpoint:
6e14e441 7148 return bp_loc_software_watchpoint;
ce78b96d 7149 case bp_catchpoint:
15c3d785
PA
7150 case bp_tracepoint:
7151 case bp_fast_tracepoint:
0fb4aa4b 7152 case bp_static_tracepoint:
7b572efb 7153 case bp_static_marker_tracepoint:
cb1e4e32 7154 return bp_loc_other;
e049a4b5 7155 default:
e2e0b3e5 7156 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
e049a4b5 7157 }
cb1e4e32
PA
7158}
7159
7160bp_location::bp_location (breakpoint *owner, bp_loc_type type)
7161{
7162 this->owner = owner;
7163 this->cond_bytecode = NULL;
7164 this->shlib_disabled = 0;
7165 this->enabled = 1;
b5fa468f 7166 this->disabled_by_cond = false;
cb1e4e32
PA
7167
7168 this->loc_type = type;
e049a4b5 7169
cb1e4e32
PA
7170 if (this->loc_type == bp_loc_software_breakpoint
7171 || this->loc_type == bp_loc_hardware_breakpoint)
7172 mark_breakpoint_location_modified (this);
7173
b6433ede 7174 incref ();
cb1e4e32
PA
7175}
7176
7177bp_location::bp_location (breakpoint *owner)
7178 : bp_location::bp_location (owner,
7179 bp_location_from_bp_type (owner->type))
7180{
28010a5d
PA
7181}
7182
f431efe5
PA
7183/* Decrement reference count. If the reference count reaches 0,
7184 destroy the bp_location. Sets *BLP to NULL. */
7185
7186static void
7187decref_bp_location (struct bp_location **blp)
7188{
b6433ede 7189 bp_location_ref_policy::decref (*blp);
f431efe5
PA
7190 *blp = NULL;
7191}
7192
346774a9 7193/* Add breakpoint B at the end of the global breakpoint chain. */
c906108c 7194
b270e6f9
TT
7195static breakpoint *
7196add_to_breakpoint_chain (std::unique_ptr<breakpoint> &&b)
c906108c 7197{
346774a9 7198 struct breakpoint *b1;
b270e6f9 7199 struct breakpoint *result = b.get ();
c906108c 7200
346774a9
PA
7201 /* Add this breakpoint to the end of the chain so that a list of
7202 breakpoints will come out in order of increasing numbers. */
7203
7204 b1 = breakpoint_chain;
7205 if (b1 == 0)
b270e6f9 7206 breakpoint_chain = b.release ();
346774a9
PA
7207 else
7208 {
7209 while (b1->next)
7210 b1 = b1->next;
b270e6f9 7211 b1->next = b.release ();
346774a9 7212 }
b270e6f9
TT
7213
7214 return result;
346774a9
PA
7215}
7216
0ba852ab 7217/* Initialize loc->function_name. */
0e30163f 7218
0d381245 7219static void
0ba852ab 7220set_breakpoint_location_function (struct bp_location *loc)
0d381245 7221{
2bdf28a0
JK
7222 gdb_assert (loc->owner != NULL);
7223
0d381245 7224 if (loc->owner->type == bp_breakpoint
1042e4c0 7225 || loc->owner->type == bp_hardware_breakpoint
d77f58be 7226 || is_tracepoint (loc->owner))
0d381245 7227 {
2c02bd72 7228 const char *function_name;
0e30163f 7229
3467ec66 7230 if (loc->msymbol != NULL
60f62e2b
SM
7231 && (loc->msymbol->type () == mst_text_gnu_ifunc
7232 || loc->msymbol->type () == mst_data_gnu_ifunc))
0e30163f
JK
7233 {
7234 struct breakpoint *b = loc->owner;
7235
c9d95fa3 7236 function_name = loc->msymbol->linkage_name ();
3467ec66
PA
7237
7238 if (b->type == bp_breakpoint && b->loc == loc
7239 && loc->next == NULL && b->related_breakpoint == b)
0e30163f
JK
7240 {
7241 /* Create only the whole new breakpoint of this type but do not
7242 mess more complicated breakpoints with multiple locations. */
7243 b->type = bp_gnu_ifunc_resolver;
6a3a010b 7244 /* Remember the resolver's address for use by the return
dda83cd7 7245 breakpoint. */
3467ec66 7246 loc->related_address = loc->address;
0e30163f
JK
7247 }
7248 }
3467ec66
PA
7249 else
7250 find_pc_partial_function (loc->address, &function_name, NULL, NULL);
0e30163f 7251
2c02bd72 7252 if (function_name)
23d6ee64 7253 loc->function_name = make_unique_xstrdup (function_name);
0d381245
VP
7254 }
7255}
7256
a6d9a66e 7257/* Attempt to determine architecture of location identified by SAL. */
1bfeeb0f 7258struct gdbarch *
a6d9a66e
UW
7259get_sal_arch (struct symtab_and_line sal)
7260{
7261 if (sal.section)
08feed99 7262 return sal.section->objfile->arch ();
a6d9a66e 7263 if (sal.symtab)
3c86fae3 7264 return sal.symtab->compunit ()->objfile ()->arch ();
a6d9a66e
UW
7265
7266 return NULL;
7267}
7268
346774a9
PA
7269/* Low level routine for partially initializing a breakpoint of type
7270 BPTYPE. The newly created breakpoint's address, section, source
c56053d2 7271 file name, and line number are provided by SAL.
0d381245
VP
7272
7273 It is expected that the caller will complete the initialization of
7274 the newly created breakpoint struct as well as output any status
c56053d2 7275 information regarding the creation of a new breakpoint. */
0d381245 7276
346774a9 7277static void
73063f51
TT
7278init_raw_breakpoint (struct breakpoint *b, struct symtab_and_line sal,
7279 enum bptype bptype)
0d381245 7280{
06615628 7281 b->add_location (sal);
0d381245 7282
6c95b8df
PA
7283 if (bptype != bp_catchpoint)
7284 gdb_assert (sal.pspace != NULL);
7285
f8eba3c6
TT
7286 /* Store the program space that was used to set the breakpoint,
7287 except for ordinary breakpoints, which are independent of the
7288 program space. */
7289 if (bptype != bp_breakpoint && bptype != bp_hardware_breakpoint)
7290 b->pspace = sal.pspace;
346774a9 7291}
c906108c 7292
53a5351d 7293/* Call this routine when stepping and nexting to enable a breakpoint
186c406b
TT
7294 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
7295 initiated the operation. */
c906108c
SS
7296
7297void
186c406b 7298set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
c906108c 7299{
5d5658a1 7300 int thread = tp->global_num;
0fd8e87f
UW
7301
7302 /* To avoid having to rescan all objfile symbols at every step,
7303 we maintain a list of continually-inserted but always disabled
7304 longjmp "master" breakpoints. Here, we simply create momentary
7305 clones of those and enable them for the requested thread. */
1428b37a 7306 for (breakpoint *b : all_breakpoints_safe ())
6c95b8df 7307 if (b->pspace == current_program_space
186c406b
TT
7308 && (b->type == bp_longjmp_master
7309 || b->type == bp_exception_master))
0fd8e87f 7310 {
06edf0c0
PA
7311 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
7312 struct breakpoint *clone;
cc59ec59 7313
e2e4d78b
JK
7314 /* longjmp_breakpoint_ops ensures INITIATING_FRAME is cleared again
7315 after their removal. */
9efa3c7f 7316 clone = momentary_breakpoint_from_master (b, type, 1);
0fd8e87f
UW
7317 clone->thread = thread;
7318 }
186c406b
TT
7319
7320 tp->initiating_frame = frame;
c906108c
SS
7321}
7322
611c83ae 7323/* Delete all longjmp breakpoints from THREAD. */
c906108c 7324void
611c83ae 7325delete_longjmp_breakpoint (int thread)
c906108c 7326{
1428b37a 7327 for (breakpoint *b : all_breakpoints_safe ())
186c406b 7328 if (b->type == bp_longjmp || b->type == bp_exception)
611c83ae
PA
7329 {
7330 if (b->thread == thread)
7331 delete_breakpoint (b);
7332 }
c906108c
SS
7333}
7334
f59f708a
PA
7335void
7336delete_longjmp_breakpoint_at_next_stop (int thread)
7337{
1428b37a 7338 for (breakpoint *b : all_breakpoints_safe ())
f59f708a
PA
7339 if (b->type == bp_longjmp || b->type == bp_exception)
7340 {
7341 if (b->thread == thread)
7342 b->disposition = disp_del_at_next_stop;
7343 }
7344}
7345
e2e4d78b
JK
7346/* Place breakpoints of type bp_longjmp_call_dummy to catch longjmp for
7347 INFERIOR_PTID thread. Chain them all by RELATED_BREAKPOINT and return
7348 pointer to any of them. Return NULL if this system cannot place longjmp
7349 breakpoints. */
7350
7351struct breakpoint *
7352set_longjmp_breakpoint_for_call_dummy (void)
7353{
43892fdf 7354 breakpoint *retval = nullptr;
e2e4d78b 7355
43892fdf 7356 for (breakpoint *b : all_breakpoints ())
e2e4d78b
JK
7357 if (b->pspace == current_program_space && b->type == bp_longjmp_master)
7358 {
7359 struct breakpoint *new_b;
7360
7361 new_b = momentary_breakpoint_from_master (b, bp_longjmp_call_dummy,
a1aa2221 7362 1);
00431a78 7363 new_b->thread = inferior_thread ()->global_num;
e2e4d78b
JK
7364
7365 /* Link NEW_B into the chain of RETVAL breakpoints. */
7366
7367 gdb_assert (new_b->related_breakpoint == new_b);
7368 if (retval == NULL)
7369 retval = new_b;
7370 new_b->related_breakpoint = retval;
7371 while (retval->related_breakpoint != new_b->related_breakpoint)
7372 retval = retval->related_breakpoint;
7373 retval->related_breakpoint = new_b;
7374 }
7375
7376 return retval;
7377}
7378
7379/* Verify all existing dummy frames and their associated breakpoints for
b67a2c6f 7380 TP. Remove those which can no longer be found in the current frame
e2e4d78b
JK
7381 stack.
7382
b4b3e2de
AB
7383 If the unwind fails then there is not sufficient information to discard
7384 dummy frames. In this case, elide the clean up and the dummy frames will
7385 be cleaned up next time this function is called from a location where
7386 unwinding is possible. */
e2e4d78b
JK
7387
7388void
b67a2c6f 7389check_longjmp_breakpoint_for_call_dummy (struct thread_info *tp)
e2e4d78b
JK
7390{
7391 struct breakpoint *b, *b_tmp;
7392
7393 ALL_BREAKPOINTS_SAFE (b, b_tmp)
5d5658a1 7394 if (b->type == bp_longjmp_call_dummy && b->thread == tp->global_num)
e2e4d78b
JK
7395 {
7396 struct breakpoint *dummy_b = b->related_breakpoint;
7397
b4b3e2de
AB
7398 /* Find the bp_call_dummy breakpoint in the list of breakpoints
7399 chained off b->related_breakpoint. */
e2e4d78b
JK
7400 while (dummy_b != b && dummy_b->type != bp_call_dummy)
7401 dummy_b = dummy_b->related_breakpoint;
b4b3e2de
AB
7402
7403 /* If there was no bp_call_dummy breakpoint then there's nothing
7404 more to do. Or, if the dummy frame associated with the
7405 bp_call_dummy is still on the stack then we need to leave this
7406 bp_call_dummy in place. */
e2e4d78b
JK
7407 if (dummy_b->type != bp_call_dummy
7408 || frame_find_by_id (dummy_b->frame_id) != NULL)
7409 continue;
b4b3e2de
AB
7410
7411 /* We didn't find the dummy frame on the stack, this could be
7412 because we have longjmp'd to a stack frame that is previous to
7413 the dummy frame, or it could be because the stack unwind is
7414 broken at some point between the longjmp frame and the dummy
7415 frame.
7416
7417 Next we figure out why the stack unwind stopped. If it looks
7418 like the unwind is complete then we assume the dummy frame has
7419 been jumped over, however, if the unwind stopped for an
7420 unexpected reason then we assume the stack unwind is currently
7421 broken, and that we will (eventually) return to the dummy
7422 frame.
7423
7424 It might be tempting to consider using frame_id_inner here, but
7425 that is not safe. There is no guarantee that the stack frames
7426 we are looking at here are even on the same stack as the
7427 original dummy frame, hence frame_id_inner can't be used. See
7428 the comments on frame_id_inner for more details. */
7429 bool unwind_finished_unexpectedly = false;
7430 for (struct frame_info *fi = get_current_frame (); fi != nullptr; )
7431 {
7432 struct frame_info *prev = get_prev_frame (fi);
7433 if (prev == nullptr)
7434 {
7435 /* FI is the last stack frame. Why did this frame not
7436 unwind further? */
7437 auto stop_reason = get_frame_unwind_stop_reason (fi);
7438 if (stop_reason != UNWIND_NO_REASON
7439 && stop_reason != UNWIND_OUTERMOST)
7440 unwind_finished_unexpectedly = true;
7441 }
7442 fi = prev;
7443 }
7444 if (unwind_finished_unexpectedly)
7445 continue;
7446
00431a78 7447 dummy_frame_discard (dummy_b->frame_id, tp);
e2e4d78b
JK
7448
7449 while (b->related_breakpoint != b)
7450 {
7451 if (b_tmp == b->related_breakpoint)
7452 b_tmp = b->related_breakpoint->next;
7453 delete_breakpoint (b->related_breakpoint);
7454 }
7455 delete_breakpoint (b);
7456 }
7457}
7458
1900040c
MS
7459void
7460enable_overlay_breakpoints (void)
7461{
43892fdf 7462 for (breakpoint *b : all_breakpoints ())
1900040c 7463 if (b->type == bp_overlay_event)
01add95b
SM
7464 {
7465 b->enable_state = bp_enabled;
7466 update_global_location_list (UGLL_MAY_INSERT);
7467 overlay_events_enabled = 1;
7468 }
1900040c
MS
7469}
7470
7471void
7472disable_overlay_breakpoints (void)
7473{
43892fdf 7474 for (breakpoint *b : all_breakpoints ())
1900040c 7475 if (b->type == bp_overlay_event)
01add95b
SM
7476 {
7477 b->enable_state = bp_disabled;
7478 update_global_location_list (UGLL_DONT_INSERT);
7479 overlay_events_enabled = 0;
7480 }
1900040c
MS
7481}
7482
aa7d318d
TT
7483/* Set an active std::terminate breakpoint for each std::terminate
7484 master breakpoint. */
7485void
7486set_std_terminate_breakpoint (void)
7487{
1428b37a 7488 for (breakpoint *b : all_breakpoints_safe ())
aa7d318d
TT
7489 if (b->pspace == current_program_space
7490 && b->type == bp_std_terminate_master)
7491 {
9efa3c7f 7492 momentary_breakpoint_from_master (b, bp_std_terminate, 1);
aa7d318d
TT
7493 }
7494}
7495
7496/* Delete all the std::terminate breakpoints. */
7497void
7498delete_std_terminate_breakpoint (void)
7499{
1428b37a 7500 for (breakpoint *b : all_breakpoints_safe ())
aa7d318d
TT
7501 if (b->type == bp_std_terminate)
7502 delete_breakpoint (b);
7503}
7504
c4093a6a 7505struct breakpoint *
a6d9a66e 7506create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
c4093a6a
JM
7507{
7508 struct breakpoint *b;
c4093a6a 7509
9efa3c7f 7510 b = create_internal_breakpoint (gdbarch, address, bp_thread_event);
06edf0c0 7511
b5de0fa7 7512 b->enable_state = bp_enabled;
f00aae0f 7513 /* location has to be used or breakpoint_re_set will delete me. */
d28cd78a 7514 b->location = new_address_location (b->loc->address, NULL, 0);
c4093a6a 7515
44702360 7516 update_global_location_list_nothrow (UGLL_MAY_INSERT);
74960c60 7517
c4093a6a
JM
7518 return b;
7519}
7520
0101ce28
JJ
7521struct lang_and_radix
7522 {
7523 enum language lang;
7524 int radix;
7525 };
7526
4efc6507
DE
7527/* Create a breakpoint for JIT code registration and unregistration. */
7528
7529struct breakpoint *
7530create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7531{
9efa3c7f 7532 return create_internal_breakpoint (gdbarch, address, bp_jit_event);
4efc6507 7533}
0101ce28 7534
03673fc7
PP
7535/* Remove JIT code registration and unregistration breakpoint(s). */
7536
7537void
7538remove_jit_event_breakpoints (void)
7539{
1428b37a 7540 for (breakpoint *b : all_breakpoints_safe ())
03673fc7
PP
7541 if (b->type == bp_jit_event
7542 && b->loc->pspace == current_program_space)
7543 delete_breakpoint (b);
7544}
7545
cae688ec
JJ
7546void
7547remove_solib_event_breakpoints (void)
7548{
1428b37a 7549 for (breakpoint *b : all_breakpoints_safe ())
6c95b8df
PA
7550 if (b->type == bp_shlib_event
7551 && b->loc->pspace == current_program_space)
cae688ec
JJ
7552 delete_breakpoint (b);
7553}
7554
f37f681c
PA
7555/* See breakpoint.h. */
7556
7557void
7558remove_solib_event_breakpoints_at_next_stop (void)
7559{
1428b37a 7560 for (breakpoint *b : all_breakpoints_safe ())
f37f681c
PA
7561 if (b->type == bp_shlib_event
7562 && b->loc->pspace == current_program_space)
7563 b->disposition = disp_del_at_next_stop;
7564}
7565
04086b45
PA
7566/* Helper for create_solib_event_breakpoint /
7567 create_and_insert_solib_event_breakpoint. Allows specifying which
7568 INSERT_MODE to pass through to update_global_location_list. */
7569
7570static struct breakpoint *
7571create_solib_event_breakpoint_1 (struct gdbarch *gdbarch, CORE_ADDR address,
7572 enum ugll_insert_mode insert_mode)
cae688ec
JJ
7573{
7574 struct breakpoint *b;
7575
9efa3c7f 7576 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event);
04086b45 7577 update_global_location_list_nothrow (insert_mode);
cae688ec
JJ
7578 return b;
7579}
7580
04086b45
PA
7581struct breakpoint *
7582create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7583{
7584 return create_solib_event_breakpoint_1 (gdbarch, address, UGLL_MAY_INSERT);
7585}
7586
f37f681c
PA
7587/* See breakpoint.h. */
7588
7589struct breakpoint *
7590create_and_insert_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7591{
7592 struct breakpoint *b;
7593
04086b45
PA
7594 /* Explicitly tell update_global_location_list to insert
7595 locations. */
7596 b = create_solib_event_breakpoint_1 (gdbarch, address, UGLL_INSERT);
f37f681c
PA
7597 if (!b->loc->inserted)
7598 {
7599 delete_breakpoint (b);
7600 return NULL;
7601 }
7602 return b;
7603}
7604
cae688ec
JJ
7605/* Disable any breakpoints that are on code in shared libraries. Only
7606 apply to enabled breakpoints, disabled ones can just stay disabled. */
7607
7608void
cb851954 7609disable_breakpoints_in_shlibs (void)
cae688ec 7610{
48d7020b 7611 for (bp_location *loc : all_bp_locations ())
01add95b
SM
7612 {
7613 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7614 struct breakpoint *b = loc->owner;
7615
7616 /* We apply the check to all breakpoints, including disabled for
7617 those with loc->duplicate set. This is so that when breakpoint
7618 becomes enabled, or the duplicate is removed, gdb will try to
7619 insert all breakpoints. If we don't set shlib_disabled here,
7620 we'll try to insert those breakpoints and fail. */
7621 if (((b->type == bp_breakpoint)
7622 || (b->type == bp_jit_event)
7623 || (b->type == bp_hardware_breakpoint)
7624 || (is_tracepoint (b)))
7625 && loc->pspace == current_program_space
7626 && !loc->shlib_disabled
7627 && solib_name_from_address (loc->pspace, loc->address)
7628 )
7629 {
7630 loc->shlib_disabled = 1;
7631 }
7632 }
cae688ec
JJ
7633}
7634
63644780
NB
7635/* Disable any breakpoints and tracepoints that are in SOLIB upon
7636 notification of unloaded_shlib. Only apply to enabled breakpoints,
7637 disabled ones can just stay disabled. */
84acb35a 7638
75149521 7639static void
84acb35a
JJ
7640disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
7641{
84acb35a
JJ
7642 int disabled_shlib_breaks = 0;
7643
48d7020b 7644 for (bp_location *loc : all_bp_locations ())
01add95b
SM
7645 {
7646 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7647 struct breakpoint *b = loc->owner;
7648
7649 if (solib->pspace == loc->pspace
7650 && !loc->shlib_disabled
7651 && (((b->type == bp_breakpoint
7652 || b->type == bp_jit_event
7653 || b->type == bp_hardware_breakpoint)
7654 && (loc->loc_type == bp_loc_hardware_breakpoint
7655 || loc->loc_type == bp_loc_software_breakpoint))
7656 || is_tracepoint (b))
7657 && solib_contains_address_p (solib, loc->address))
7658 {
7659 loc->shlib_disabled = 1;
7660 /* At this point, we cannot rely on remove_breakpoint
7661 succeeding so we must mark the breakpoint as not inserted
7662 to prevent future errors occurring in remove_breakpoints. */
7663 loc->inserted = 0;
7664
7665 /* This may cause duplicate notifications for the same breakpoint. */
7666 gdb::observers::breakpoint_modified.notify (b);
7667
7668 if (!disabled_shlib_breaks)
7669 {
7670 target_terminal::ours_for_output ();
7671 warning (_("Temporarily disabling breakpoints "
7672 "for unloaded shared library \"%s\""),
7673 solib->so_name);
7674 }
7675 disabled_shlib_breaks = 1;
7676 }
7677 }
84acb35a
JJ
7678}
7679
63644780
NB
7680/* Disable any breakpoints and tracepoints in OBJFILE upon
7681 notification of free_objfile. Only apply to enabled breakpoints,
7682 disabled ones can just stay disabled. */
7683
7684static void
7685disable_breakpoints_in_freed_objfile (struct objfile *objfile)
7686{
63644780
NB
7687 if (objfile == NULL)
7688 return;
7689
d03de421
PA
7690 /* OBJF_SHARED|OBJF_USERLOADED objfiles are dynamic modules manually
7691 managed by the user with add-symbol-file/remove-symbol-file.
7692 Similarly to how breakpoints in shared libraries are handled in
7693 response to "nosharedlibrary", mark breakpoints in such modules
08351840
PA
7694 shlib_disabled so they end up uninserted on the next global
7695 location list update. Shared libraries not loaded by the user
7696 aren't handled here -- they're already handled in
7697 disable_breakpoints_in_unloaded_shlib, called by solib.c's
7698 solib_unloaded observer. We skip objfiles that are not
d03de421
PA
7699 OBJF_SHARED as those aren't considered dynamic objects (e.g. the
7700 main objfile). */
7701 if ((objfile->flags & OBJF_SHARED) == 0
7702 || (objfile->flags & OBJF_USERLOADED) == 0)
63644780
NB
7703 return;
7704
43892fdf 7705 for (breakpoint *b : all_breakpoints ())
63644780 7706 {
63644780
NB
7707 int bp_modified = 0;
7708
7709 if (!is_breakpoint (b) && !is_tracepoint (b))
7710 continue;
7711
40cb8ca5 7712 for (bp_location *loc : b->locations ())
63644780
NB
7713 {
7714 CORE_ADDR loc_addr = loc->address;
7715
7716 if (loc->loc_type != bp_loc_hardware_breakpoint
7717 && loc->loc_type != bp_loc_software_breakpoint)
7718 continue;
7719
7720 if (loc->shlib_disabled != 0)
7721 continue;
7722
7723 if (objfile->pspace != loc->pspace)
7724 continue;
7725
7726 if (loc->loc_type != bp_loc_hardware_breakpoint
7727 && loc->loc_type != bp_loc_software_breakpoint)
7728 continue;
7729
7730 if (is_addr_in_objfile (loc_addr, objfile))
7731 {
7732 loc->shlib_disabled = 1;
08351840
PA
7733 /* At this point, we don't know whether the object was
7734 unmapped from the inferior or not, so leave the
7735 inserted flag alone. We'll handle failure to
7736 uninsert quietly, in case the object was indeed
7737 unmapped. */
63644780
NB
7738
7739 mark_breakpoint_location_modified (loc);
7740
7741 bp_modified = 1;
7742 }
7743 }
7744
7745 if (bp_modified)
76727919 7746 gdb::observers::breakpoint_modified.notify (b);
63644780
NB
7747 }
7748}
7749
b650a282 7750/* See breakpoint.h. */
ce78b96d 7751
fed1c982
TT
7752catchpoint::catchpoint (struct gdbarch *gdbarch, bool temp,
7753 const char *cond_string_)
7754 : base_breakpoint (gdbarch, bp_catchpoint)
c906108c 7755{
51abb421 7756 symtab_and_line sal;
6c95b8df 7757 sal.pspace = current_program_space;
c5aa993b 7758
fed1c982 7759 init_raw_breakpoint (this, sal, bp_catchpoint);
ce78b96d 7760
fed1c982
TT
7761 if (cond_string_ != nullptr)
7762 cond_string = make_unique_xstrdup (cond_string_);
7763 disposition = temp ? disp_del : disp_donttouch;
346774a9
PA
7764}
7765
28010a5d 7766void
b270e6f9 7767install_breakpoint (int internal, std::unique_ptr<breakpoint> &&arg, int update_gll)
c56053d2 7768{
b270e6f9 7769 breakpoint *b = add_to_breakpoint_chain (std::move (arg));
3a5c3e22 7770 set_breakpoint_number (internal, b);
558a9d82
YQ
7771 if (is_tracepoint (b))
7772 set_tracepoint_count (breakpoint_count);
3a5c3e22
PA
7773 if (!internal)
7774 mention (b);
76727919 7775 gdb::observers::breakpoint_created.notify (b);
3ea46bff
YQ
7776
7777 if (update_gll)
44702360 7778 update_global_location_list (UGLL_MAY_INSERT);
c56053d2
PA
7779}
7780
c906108c 7781static int
fba45db2 7782hw_breakpoint_used_count (void)
c906108c 7783{
c906108c
SS
7784 int i = 0;
7785
43892fdf 7786 for (breakpoint *b : all_breakpoints ())
d6b74ac4 7787 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
40cb8ca5 7788 for (bp_location *bl : b->locations ())
f1310107
TJB
7789 {
7790 /* Special types of hardware breakpoints may use more than
7791 one register. */
c47614fe 7792 i += b->resources_needed (bl);
f1310107 7793 }
c906108c
SS
7794
7795 return i;
7796}
7797
a1398e0c
PA
7798/* Returns the resources B would use if it were a hardware
7799 watchpoint. */
7800
c906108c 7801static int
a1398e0c 7802hw_watchpoint_use_count (struct breakpoint *b)
c906108c 7803{
c906108c
SS
7804 int i = 0;
7805
a1398e0c
PA
7806 if (!breakpoint_enabled (b))
7807 return 0;
7808
40cb8ca5 7809 for (bp_location *bl : b->locations ())
a1398e0c
PA
7810 {
7811 /* Special types of hardware watchpoints may use more than
7812 one register. */
c47614fe 7813 i += b->resources_needed (bl);
a1398e0c
PA
7814 }
7815
7816 return i;
7817}
7818
7819/* Returns the sum the used resources of all hardware watchpoints of
7820 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
7821 the sum of the used resources of all hardware watchpoints of other
7822 types _not_ TYPE. */
7823
7824static int
7825hw_watchpoint_used_count_others (struct breakpoint *except,
7826 enum bptype type, int *other_type_used)
7827{
7828 int i = 0;
a1398e0c 7829
c906108c 7830 *other_type_used = 0;
43892fdf 7831 for (breakpoint *b : all_breakpoints ())
e09342b5 7832 {
a1398e0c
PA
7833 if (b == except)
7834 continue;
e09342b5
TJB
7835 if (!breakpoint_enabled (b))
7836 continue;
7837
a1398e0c
PA
7838 if (b->type == type)
7839 i += hw_watchpoint_use_count (b);
7840 else if (is_hardware_watchpoint (b))
7841 *other_type_used = 1;
e09342b5
TJB
7842 }
7843
c906108c
SS
7844 return i;
7845}
7846
c906108c 7847void
fba45db2 7848disable_watchpoints_before_interactive_call_start (void)
c906108c 7849{
43892fdf 7850 for (breakpoint *b : all_breakpoints ())
cc60f2e3 7851 if (is_watchpoint (b) && breakpoint_enabled (b))
c5aa993b 7852 {
b5de0fa7 7853 b->enable_state = bp_call_disabled;
44702360 7854 update_global_location_list (UGLL_DONT_INSERT);
c5aa993b 7855 }
c906108c
SS
7856}
7857
7858void
fba45db2 7859enable_watchpoints_after_interactive_call_stop (void)
c906108c 7860{
43892fdf 7861 for (breakpoint *b : all_breakpoints ())
cc60f2e3 7862 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
c5aa993b 7863 {
b5de0fa7 7864 b->enable_state = bp_enabled;
44702360 7865 update_global_location_list (UGLL_MAY_INSERT);
c5aa993b 7866 }
c906108c
SS
7867}
7868
8bea4e01
UW
7869void
7870disable_breakpoints_before_startup (void)
7871{
6c95b8df 7872 current_program_space->executing_startup = 1;
44702360 7873 update_global_location_list (UGLL_DONT_INSERT);
8bea4e01
UW
7874}
7875
7876void
7877enable_breakpoints_after_startup (void)
7878{
6c95b8df 7879 current_program_space->executing_startup = 0;
f8eba3c6 7880 breakpoint_re_set ();
8bea4e01
UW
7881}
7882
7c16b83e
PA
7883/* Create a new single-step breakpoint for thread THREAD, with no
7884 locations. */
c906108c 7885
7c16b83e
PA
7886static struct breakpoint *
7887new_single_step_breakpoint (int thread, struct gdbarch *gdbarch)
7888{
73063f51
TT
7889 std::unique_ptr<breakpoint> b (new momentary_breakpoint (gdbarch,
7890 bp_single_step));
7c16b83e
PA
7891
7892 b->disposition = disp_donttouch;
7893 b->frame_id = null_frame_id;
7894
7895 b->thread = thread;
7896 gdb_assert (b->thread != 0);
7897
b270e6f9 7898 return add_to_breakpoint_chain (std::move (b));
7c16b83e
PA
7899}
7900
7a3e3265
PA
7901/* Allocate a new momentary breakpoint. */
7902
7903static momentary_breakpoint *
7904new_momentary_breakpoint (struct gdbarch *gdbarch, enum bptype type)
7905{
7906 if (type == bp_longjmp || type == bp_exception)
7907 return new longjmp_breakpoint (gdbarch, type);
7908 else
7909 return new momentary_breakpoint (gdbarch, type);
7910}
7911
7c16b83e
PA
7912/* Set a momentary breakpoint of type TYPE at address specified by
7913 SAL. If FRAME_ID is valid, the breakpoint is restricted to that
7914 frame. */
c906108c 7915
454dafbd 7916breakpoint_up
a6d9a66e
UW
7917set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
7918 struct frame_id frame_id, enum bptype type)
c906108c 7919{
193facb3
JK
7920 /* If FRAME_ID is valid, it should be a real frame, not an inlined or
7921 tail-called one. */
7922 gdb_assert (!frame_id_artificial_p (frame_id));
edb3359d 7923
7a3e3265
PA
7924 std::unique_ptr<momentary_breakpoint> b
7925 (new_momentary_breakpoint (gdbarch, type));
7926
7927 b->add_location (sal);
7928 b->pspace = sal.pspace;
b5de0fa7
EZ
7929 b->enable_state = bp_enabled;
7930 b->disposition = disp_donttouch;
818dd999 7931 b->frame_id = frame_id;
c906108c 7932
00431a78 7933 b->thread = inferior_thread ()->global_num;
c906108c 7934
7a3e3265
PA
7935 breakpoint_up bp (add_to_breakpoint_chain (std::move (b)));
7936
44702360 7937 update_global_location_list_nothrow (UGLL_MAY_INSERT);
74960c60 7938
7a3e3265 7939 return bp;
c906108c 7940}
611c83ae 7941
06edf0c0 7942/* Make a momentary breakpoint based on the master breakpoint ORIG.
a1aa2221
LM
7943 The new breakpoint will have type TYPE, use OPS as its
7944 breakpoint_ops, and will set enabled to LOC_ENABLED. */
e58b0e63 7945
06edf0c0
PA
7946static struct breakpoint *
7947momentary_breakpoint_from_master (struct breakpoint *orig,
7948 enum bptype type,
a1aa2221 7949 int loc_enabled)
e58b0e63 7950{
7a3e3265
PA
7951 std::unique_ptr<breakpoint> copy
7952 (new_momentary_breakpoint (orig->gdbarch, type));
ee89d0a4 7953 copy->loc = copy->allocate_location ();
0ba852ab 7954 set_breakpoint_location_function (copy->loc);
e58b0e63 7955
a6d9a66e 7956 copy->loc->gdbarch = orig->loc->gdbarch;
e58b0e63
PA
7957 copy->loc->requested_address = orig->loc->requested_address;
7958 copy->loc->address = orig->loc->address;
7959 copy->loc->section = orig->loc->section;
6c95b8df 7960 copy->loc->pspace = orig->loc->pspace;
55aa24fb 7961 copy->loc->probe = orig->loc->probe;
f8eba3c6 7962 copy->loc->line_number = orig->loc->line_number;
2f202fde 7963 copy->loc->symtab = orig->loc->symtab;
a1aa2221 7964 copy->loc->enabled = loc_enabled;
e58b0e63
PA
7965 copy->frame_id = orig->frame_id;
7966 copy->thread = orig->thread;
6c95b8df 7967 copy->pspace = orig->pspace;
e58b0e63
PA
7968
7969 copy->enable_state = bp_enabled;
7970 copy->disposition = disp_donttouch;
7971 copy->number = internal_breakpoint_number--;
7972
7a3e3265 7973 breakpoint *b = add_to_breakpoint_chain (std::move (copy));
44702360 7974 update_global_location_list_nothrow (UGLL_DONT_INSERT);
7a3e3265 7975 return b;
e58b0e63
PA
7976}
7977
06edf0c0
PA
7978/* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
7979 ORIG is NULL. */
7980
7981struct breakpoint *
7982clone_momentary_breakpoint (struct breakpoint *orig)
7983{
7984 /* If there's nothing to clone, then return nothing. */
7985 if (orig == NULL)
7986 return NULL;
7987
9efa3c7f 7988 return momentary_breakpoint_from_master (orig, orig->type, 0);
06edf0c0
PA
7989}
7990
454dafbd 7991breakpoint_up
a6d9a66e
UW
7992set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
7993 enum bptype type)
611c83ae
PA
7994{
7995 struct symtab_and_line sal;
7996
7997 sal = find_pc_line (pc, 0);
7998 sal.pc = pc;
7999 sal.section = find_pc_overlay (pc);
8000 sal.explicit_pc = 1;
8001
a6d9a66e 8002 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
611c83ae 8003}
c906108c 8004\f
c5aa993b 8005
c906108c
SS
8006/* Tell the user we have just set a breakpoint B. */
8007
8008static void
b713485d 8009mention (const breakpoint *b)
c906108c 8010{
c47614fe 8011 b->print_mention ();
2d33446d 8012 current_uiout->text ("\n");
c906108c 8013}
c906108c 8014\f
c5aa993b 8015
5133a315 8016static bool bp_loc_is_permanent (struct bp_location *loc);
1a853c52 8017
7f32a4d5
PA
8018/* Handle "set breakpoint auto-hw on".
8019
8020 If the explicitly specified breakpoint type is not hardware
8021 breakpoint, check the memory map to see whether the breakpoint
8022 address is in read-only memory.
8023
8024 - location type is not hardware breakpoint, memory is read-only.
8025 We change the type of the location to hardware breakpoint.
8026
8027 - location type is hardware breakpoint, memory is read-write. This
8028 means we've previously made the location hardware one, but then the
8029 memory map changed, so we undo.
8030*/
8031
8032static void
8033handle_automatic_hardware_breakpoints (bp_location *bl)
8034{
8035 if (automatic_hardware_breakpoints
8036 && bl->owner->type != bp_hardware_breakpoint
8037 && (bl->loc_type == bp_loc_software_breakpoint
8038 || bl->loc_type == bp_loc_hardware_breakpoint))
8039 {
8040 /* When breakpoints are removed, remove_breakpoints will use
8041 location types we've just set here, the only possible problem
8042 is that memory map has changed during running program, but
8043 it's not going to work anyway with current gdb. */
8044 mem_region *mr = lookup_mem_region (bl->address);
8045
8046 if (mr != nullptr)
8047 {
8048 enum bp_loc_type new_type;
8049
8050 if (mr->attrib.mode != MEM_RW)
8051 new_type = bp_loc_hardware_breakpoint;
8052 else
8053 new_type = bp_loc_software_breakpoint;
8054
8055 if (new_type != bl->loc_type)
8056 {
8057 static bool said = false;
8058
8059 bl->loc_type = new_type;
8060 if (!said)
8061 {
6cb06a8c
TT
8062 gdb_printf (_("Note: automatically using "
8063 "hardware breakpoints for "
8064 "read-only addresses.\n"));
7f32a4d5
PA
8065 said = true;
8066 }
8067 }
8068 }
8069 }
8070}
8071
06615628
PA
8072bp_location *
8073breakpoint::add_location (const symtab_and_line &sal)
0d381245 8074{
06615628 8075 struct bp_location *new_loc, **tmp;
3742cc8b 8076 CORE_ADDR adjusted_address;
06615628 8077 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
3742cc8b
YQ
8078
8079 if (loc_gdbarch == NULL)
06615628 8080 loc_gdbarch = gdbarch;
3742cc8b
YQ
8081
8082 /* Adjust the breakpoint's address prior to allocating a location.
ee89d0a4 8083 Once we call allocate_location(), that mostly uninitialized
3742cc8b
YQ
8084 location will be placed on the location chain. Adjustment of the
8085 breakpoint may cause target_read_memory() to be called and we do
8086 not want its scan of the location chain to find a breakpoint and
8087 location that's only been partially initialized. */
8088 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
06615628 8089 sal.pc, type);
0d381245 8090
d30113d4 8091 /* Sort the locations by their ADDRESS. */
06615628
PA
8092 new_loc = allocate_location ();
8093 for (tmp = &(loc); *tmp != NULL && (*tmp)->address <= adjusted_address;
d30113d4 8094 tmp = &((*tmp)->next))
0d381245 8095 ;
06615628
PA
8096 new_loc->next = *tmp;
8097 *tmp = new_loc;
8098
8099 new_loc->requested_address = sal.pc;
8100 new_loc->address = adjusted_address;
8101 new_loc->pspace = sal.pspace;
8102 new_loc->probe.prob = sal.prob;
8103 new_loc->probe.objfile = sal.objfile;
8104 gdb_assert (new_loc->pspace != NULL);
8105 new_loc->section = sal.section;
8106 new_loc->gdbarch = loc_gdbarch;
8107 new_loc->line_number = sal.line;
8108 new_loc->symtab = sal.symtab;
8109 new_loc->symbol = sal.symbol;
8110 new_loc->msymbol = sal.msymbol;
8111 new_loc->objfile = sal.objfile;
8112
8113 set_breakpoint_location_function (new_loc);
1a853c52 8114
6ae88661
LM
8115 /* While by definition, permanent breakpoints are already present in the
8116 code, we don't mark the location as inserted. Normally one would expect
8117 that GDB could rely on that breakpoint instruction to stop the program,
8118 thus removing the need to insert its own breakpoint, except that executing
8119 the breakpoint instruction can kill the target instead of reporting a
8120 SIGTRAP. E.g., on SPARC, when interrupts are disabled, executing the
8121 instruction resets the CPU, so QEMU 2.0.0 for SPARC correspondingly dies
8122 with "Trap 0x02 while interrupts disabled, Error state". Letting the
8123 breakpoint be inserted normally results in QEMU knowing about the GDB
8124 breakpoint, and thus trap before the breakpoint instruction is executed.
8125 (If GDB later needs to continue execution past the permanent breakpoint,
8126 it manually increments the PC, thus avoiding executing the breakpoint
8127 instruction.) */
06615628
PA
8128 if (bp_loc_is_permanent (new_loc))
8129 new_loc->permanent = 1;
1a853c52 8130
06615628 8131 return new_loc;
0d381245 8132}
514f746b
AR
8133\f
8134
5133a315
LM
8135/* Return true if LOC is pointing to a permanent breakpoint,
8136 return false otherwise. */
1cf4d951 8137
5133a315 8138static bool
1cf4d951
PA
8139bp_loc_is_permanent (struct bp_location *loc)
8140{
514f746b
AR
8141 gdb_assert (loc != NULL);
8142
cb1e4e32
PA
8143 /* If we have a non-breakpoint-backed catchpoint or a software
8144 watchpoint, just return 0. We should not attempt to read from
8145 the addresses the locations of these breakpoint types point to.
5133a315 8146 gdbarch_program_breakpoint_here_p, below, will attempt to read
244558af 8147 memory. */
cb1e4e32 8148 if (!bl_address_is_meaningful (loc))
5133a315 8149 return false;
244558af 8150
5ed8105e 8151 scoped_restore_current_pspace_and_thread restore_pspace_thread;
6c95b8df 8152 switch_to_program_space_and_thread (loc->pspace);
5133a315 8153 return gdbarch_program_breakpoint_here_p (loc->gdbarch, loc->address);
514f746b
AR
8154}
8155
e7e0cddf
SS
8156/* Build a command list for the dprintf corresponding to the current
8157 settings of the dprintf style options. */
8158
8159static void
8160update_dprintf_command_list (struct breakpoint *b)
8161{
6f781ee3 8162 const char *dprintf_args = b->extra_string.get ();
8579fd13 8163 gdb::unique_xmalloc_ptr<char> printf_line = nullptr;
e7e0cddf
SS
8164
8165 if (!dprintf_args)
8166 return;
8167
8168 dprintf_args = skip_spaces (dprintf_args);
8169
8170 /* Allow a comma, as it may have terminated a location, but don't
8171 insist on it. */
8172 if (*dprintf_args == ',')
8173 ++dprintf_args;
8174 dprintf_args = skip_spaces (dprintf_args);
8175
8176 if (*dprintf_args != '"')
8177 error (_("Bad format string, missing '\"'."));
8178
d3ce09f5 8179 if (strcmp (dprintf_style, dprintf_style_gdb) == 0)
e7e0cddf 8180 printf_line = xstrprintf ("printf %s", dprintf_args);
d3ce09f5 8181 else if (strcmp (dprintf_style, dprintf_style_call) == 0)
e7e0cddf 8182 {
e0700ba4 8183 if (dprintf_function.empty ())
e7e0cddf
SS
8184 error (_("No function supplied for dprintf call"));
8185
e0700ba4 8186 if (!dprintf_channel.empty ())
e7e0cddf 8187 printf_line = xstrprintf ("call (void) %s (%s,%s)",
e0700ba4
SM
8188 dprintf_function.c_str (),
8189 dprintf_channel.c_str (),
e7e0cddf
SS
8190 dprintf_args);
8191 else
8192 printf_line = xstrprintf ("call (void) %s (%s)",
e0700ba4 8193 dprintf_function.c_str (),
e7e0cddf
SS
8194 dprintf_args);
8195 }
d3ce09f5
SS
8196 else if (strcmp (dprintf_style, dprintf_style_agent) == 0)
8197 {
8198 if (target_can_run_breakpoint_commands ())
8199 printf_line = xstrprintf ("agent-printf %s", dprintf_args);
8200 else
8201 {
8202 warning (_("Target cannot run dprintf commands, falling back to GDB printf"));
8203 printf_line = xstrprintf ("printf %s", dprintf_args);
8204 }
8205 }
e7e0cddf
SS
8206 else
8207 internal_error (__FILE__, __LINE__,
8208 _("Invalid dprintf style."));
8209
f28045c2 8210 gdb_assert (printf_line != NULL);
e7e0cddf 8211
12973681
TT
8212 /* Manufacture a printf sequence. */
8213 struct command_line *printf_cmd_line
8579fd13 8214 = new struct command_line (simple_control, printf_line.release ());
12973681
TT
8215 breakpoint_set_commands (b, counted_command_line (printf_cmd_line,
8216 command_lines_deleter ()));
e7e0cddf
SS
8217}
8218
8219/* Update all dprintf commands, making their command lists reflect
8220 current style settings. */
8221
8222static void
eb4c3f4a 8223update_dprintf_commands (const char *args, int from_tty,
e7e0cddf
SS
8224 struct cmd_list_element *c)
8225{
43892fdf
SM
8226 for (breakpoint *b : all_breakpoints ())
8227 if (b->type == bp_dprintf)
e7e0cddf 8228 update_dprintf_command_list (b);
e7e0cddf 8229}
c3f6f71d 8230
3b003a61
PA
8231base_breakpoint::base_breakpoint (struct gdbarch *gdbarch_,
8232 enum bptype type_,
8233 gdb::array_view<const symtab_and_line> sals,
8234 event_location_up &&location_,
8235 gdb::unique_xmalloc_ptr<char> filter_,
8236 gdb::unique_xmalloc_ptr<char> cond_string_,
8237 gdb::unique_xmalloc_ptr<char> extra_string_,
8238 enum bpdisp disposition_,
8239 int thread_, int task_, int ignore_count_,
8240 int from_tty,
8241 int enabled_, unsigned flags,
8242 int display_canonical_)
8243 : breakpoint (gdbarch_, type_)
018d34a4 8244{
0d381245 8245 int i;
018d34a4
VP
8246
8247 if (type == bp_hardware_breakpoint)
8248 {
fbbd034e
AS
8249 int target_resources_ok;
8250
8251 i = hw_breakpoint_used_count ();
8252 target_resources_ok =
8253 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
018d34a4
VP
8254 i + 1, 0);
8255 if (target_resources_ok == 0)
8256 error (_("No hardware breakpoint support in the target."));
8257 else if (target_resources_ok < 0)
8258 error (_("Hardware breakpoints used exceeds limit."));
8259 }
8260
6c5b2ebe 8261 gdb_assert (!sals.empty ());
6c95b8df 8262
3b003a61
PA
8263 thread = thread_;
8264 task = task_;
0d381245 8265
3b003a61
PA
8266 cond_string = std::move (cond_string_);
8267 extra_string = std::move (extra_string_);
8268 ignore_count = ignore_count_;
8269 enable_state = enabled_ ? bp_enabled : bp_disabled;
8270 disposition = disposition_;
5af949e3 8271
3b003a61
PA
8272 if (type == bp_static_tracepoint
8273 || type == bp_static_marker_tracepoint)
8274 {
8275 auto *t = static_cast<struct tracepoint *> (this);
8276 struct static_tracepoint_marker marker;
0d381245 8277
3b003a61 8278 if (strace_marker_p (this))
0d381245 8279 {
3b003a61
PA
8280 /* We already know the marker exists, otherwise, we wouldn't
8281 see a sal for it. */
8282 const char *p = &event_location_to_string (location_.get ())[3];
8283 const char *endp;
0fb4aa4b 8284
3b003a61 8285 p = skip_spaces (p);
0fb4aa4b 8286
3b003a61 8287 endp = skip_to_space (p);
0fb4aa4b 8288
3b003a61 8289 t->static_trace_marker_id.assign (p, endp - p);
0fb4aa4b 8290
3b003a61
PA
8291 gdb_printf (_("Probed static tracepoint marker \"%s\"\n"),
8292 t->static_trace_marker_id.c_str ());
8293 }
8294 else if (target_static_tracepoint_marker_at (sals[0].pc, &marker))
8295 {
8296 t->static_trace_marker_id = std::move (marker.str_id);
0fb4aa4b 8297
3b003a61
PA
8298 gdb_printf (_("Probed static tracepoint marker \"%s\"\n"),
8299 t->static_trace_marker_id.c_str ());
0d381245
VP
8300 }
8301 else
3b003a61
PA
8302 warning (_("Couldn't determine the static tracepoint marker to probe"));
8303 }
8304
8305 for (const auto &sal : sals)
8306 {
8307 if (from_tty)
018d34a4 8308 {
3b003a61
PA
8309 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
8310 if (loc_gdbarch == nullptr)
8311 loc_gdbarch = gdbarch;
8312
8313 describe_other_breakpoints (loc_gdbarch,
8314 sal.pspace, sal.pc, sal.section, thread);
0d381245
VP
8315 }
8316
3b003a61
PA
8317 bp_location *new_loc = add_location (sal);
8318 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
8319 new_loc->inserted = 1;
8320
b5fa468f
TBA
8321 /* Do not set breakpoint locations conditions yet. As locations
8322 are inserted, they get sorted based on their addresses. Let
8323 the list stabilize to have reliable location numbers. */
e7e0cddf
SS
8324
8325 /* Dynamic printf requires and uses additional arguments on the
8326 command line, otherwise it's an error. */
8327 if (type == bp_dprintf)
8328 {
3b003a61
PA
8329 if (extra_string != nullptr)
8330 update_dprintf_command_list (this);
e7e0cddf
SS
8331 else
8332 error (_("Format string required"));
8333 }
3b003a61
PA
8334 else if (extra_string != nullptr)
8335 error (_("Garbage '%s' at end of command"), extra_string.get ());
855a6e68 8336 }
018d34a4 8337
b5fa468f
TBA
8338 /* The order of the locations is now stable. Set the location
8339 condition using the location's number. */
8340 int loc_num = 1;
3b003a61 8341 for (bp_location *bl : locations ())
b5fa468f 8342 {
3b003a61
PA
8343 if (cond_string != nullptr)
8344 set_breakpoint_location_condition (cond_string.get (), bl,
8345 number, loc_num);
b5fa468f
TBA
8346
8347 ++loc_num;
8348 }
8349
3b003a61
PA
8350 display_canonical = display_canonical_;
8351 if (location_ != nullptr)
8352 location = std::move (location_);
018d34a4 8353 else
3b003a61
PA
8354 location = new_address_location (this->loc->address, NULL, 0);
8355 filter = std::move (filter_);
d9b3f62e 8356}
018d34a4 8357
d9b3f62e
PA
8358static void
8359create_breakpoint_sal (struct gdbarch *gdbarch,
6c5b2ebe 8360 gdb::array_view<const symtab_and_line> sals,
ffc2605c 8361 event_location_up &&location,
e1e01040
PA
8362 gdb::unique_xmalloc_ptr<char> filter,
8363 gdb::unique_xmalloc_ptr<char> cond_string,
8364 gdb::unique_xmalloc_ptr<char> extra_string,
d9b3f62e
PA
8365 enum bptype type, enum bpdisp disposition,
8366 int thread, int task, int ignore_count,
ef4848c7 8367 int from_tty,
44f238bb
PA
8368 int enabled, int internal, unsigned flags,
8369 int display_canonical)
d9b3f62e 8370{
3b003a61
PA
8371 std::unique_ptr<base_breakpoint> b
8372 = new_breakpoint_from_type (gdbarch,
8373 type,
8374 sals,
8375 std::move (location),
8376 std::move (filter),
8377 std::move (cond_string),
8378 std::move (extra_string),
8379 disposition,
8380 thread, task, ignore_count,
8381 from_tty,
8382 enabled, flags,
8383 display_canonical);
d9b3f62e 8384
b270e6f9 8385 install_breakpoint (internal, std::move (b), 0);
018d34a4
VP
8386}
8387
8388/* Add SALS.nelts breakpoints to the breakpoint table. For each
8389 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
8390 value. COND_STRING, if not NULL, specified the condition to be
8391 used for all breakpoints. Essentially the only case where
8392 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
8393 function. In that case, it's still not possible to specify
8394 separate conditions for different overloaded functions, so
8395 we take just a single condition string.
8396
c3f6f71d 8397 NOTE: If the function succeeds, the caller is expected to cleanup
018d34a4 8398 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
c3f6f71d
JM
8399 array contents). If the function fails (error() is called), the
8400 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
4a64f543 8401 COND and SALS arrays and each of those arrays contents. */
c906108c
SS
8402
8403static void
8cdf0e15 8404create_breakpoints_sal (struct gdbarch *gdbarch,
7efd8fc2 8405 struct linespec_result *canonical,
e1e01040
PA
8406 gdb::unique_xmalloc_ptr<char> cond_string,
8407 gdb::unique_xmalloc_ptr<char> extra_string,
8cdf0e15
VP
8408 enum bptype type, enum bpdisp disposition,
8409 int thread, int task, int ignore_count,
ef4848c7 8410 int from_tty,
44f238bb 8411 int enabled, int internal, unsigned flags)
c906108c 8412{
f8eba3c6 8413 if (canonical->pre_expanded)
6c5b2ebe 8414 gdb_assert (canonical->lsals.size () == 1);
f8eba3c6 8415
6c5b2ebe 8416 for (const auto &lsal : canonical->lsals)
c3f6f71d 8417 {
f00aae0f 8418 /* Note that 'location' can be NULL in the case of a plain
f8eba3c6 8419 'break', without arguments. */
ffc2605c 8420 event_location_up location
f00aae0f 8421 = (canonical->location != NULL
8e9e35b1 8422 ? copy_event_location (canonical->location.get ()) : NULL);
e1e01040 8423 gdb::unique_xmalloc_ptr<char> filter_string
6c5b2ebe 8424 (lsal.canonical != NULL ? xstrdup (lsal.canonical) : NULL);
0d381245 8425
6c5b2ebe 8426 create_breakpoint_sal (gdbarch, lsal.sals,
ffc2605c 8427 std::move (location),
e1e01040
PA
8428 std::move (filter_string),
8429 std::move (cond_string),
8430 std::move (extra_string),
e7e0cddf 8431 type, disposition,
ef4848c7 8432 thread, task, ignore_count,
44f238bb 8433 from_tty, enabled, internal, flags,
56435ebe 8434 canonical->special_display);
c3f6f71d 8435 }
c3f6f71d 8436}
c906108c 8437
f00aae0f 8438/* Parse LOCATION which is assumed to be a SAL specification possibly
c3f6f71d 8439 followed by conditionals. On return, SALS contains an array of SAL
f00aae0f
KS
8440 addresses found. LOCATION points to the end of the SAL (for
8441 linespec locations).
9998af43
TJB
8442
8443 The array and the line spec strings are allocated on the heap, it is
8444 the caller's responsibility to free them. */
c906108c 8445
b9362cc7 8446static void
626d2320 8447parse_breakpoint_sals (struct event_location *location,
58438ac1 8448 struct linespec_result *canonical)
c3f6f71d 8449{
f00aae0f
KS
8450 struct symtab_and_line cursal;
8451
8452 if (event_location_type (location) == LINESPEC_LOCATION)
8453 {
a20714ff 8454 const char *spec = get_linespec_location (location)->spec_string;
f00aae0f 8455
a20714ff 8456 if (spec == NULL)
f00aae0f
KS
8457 {
8458 /* The last displayed codepoint, if it's valid, is our default
8459 breakpoint address. */
8460 if (last_displayed_sal_is_valid ())
8461 {
f00aae0f
KS
8462 /* Set sal's pspace, pc, symtab, and line to the values
8463 corresponding to the last call to print_frame_info.
8464 Be sure to reinitialize LINE with NOTCURRENT == 0
8465 as the breakpoint line number is inappropriate otherwise.
8466 find_pc_line would adjust PC, re-set it back. */
51abb421
PA
8467 symtab_and_line sal = get_last_displayed_sal ();
8468 CORE_ADDR pc = sal.pc;
8469
f00aae0f
KS
8470 sal = find_pc_line (pc, 0);
8471
8472 /* "break" without arguments is equivalent to "break *PC"
8473 where PC is the last displayed codepoint's address. So
8474 make sure to set sal.explicit_pc to prevent GDB from
8475 trying to expand the list of sals to include all other
8476 instances with the same symtab and line. */
8477 sal.pc = pc;
8478 sal.explicit_pc = 1;
8479
6c5b2ebe
PA
8480 struct linespec_sals lsal;
8481 lsal.sals = {sal};
f00aae0f
KS
8482 lsal.canonical = NULL;
8483
6c5b2ebe 8484 canonical->lsals.push_back (std::move (lsal));
f00aae0f
KS
8485 return;
8486 }
8487 else
8488 error (_("No default breakpoint address now."));
c906108c 8489 }
c906108c 8490 }
f00aae0f
KS
8491
8492 /* Force almost all breakpoints to be in terms of the
8493 current_source_symtab (which is decode_line_1's default).
8494 This should produce the results we want almost all of the
8495 time while leaving default_breakpoint_* alone.
8496
8497 ObjC: However, don't match an Objective-C method name which
8498 may have a '+' or '-' succeeded by a '['. */
8499 cursal = get_current_source_symtab_and_line ();
8500 if (last_displayed_sal_is_valid ())
c906108c 8501 {
a20714ff 8502 const char *spec = NULL;
cc80f267 8503
f00aae0f 8504 if (event_location_type (location) == LINESPEC_LOCATION)
a20714ff 8505 spec = get_linespec_location (location)->spec_string;
cc80f267 8506
f00aae0f 8507 if (!cursal.symtab
a20714ff
PA
8508 || (spec != NULL
8509 && strchr ("+-", spec[0]) != NULL
8510 && spec[1] != '['))
f00aae0f 8511 {
c2f4122d 8512 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
f00aae0f
KS
8513 get_last_displayed_symtab (),
8514 get_last_displayed_line (),
8515 canonical, NULL, NULL);
8516 return;
8517 }
c906108c 8518 }
f00aae0f 8519
c2f4122d 8520 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
f00aae0f 8521 cursal.symtab, cursal.line, canonical, NULL, NULL);
c3f6f71d 8522}
c906108c 8523
c906108c 8524
c3f6f71d 8525/* Convert each SAL into a real PC. Verify that the PC can be
4a64f543 8526 inserted as a breakpoint. If it can't throw an error. */
c906108c 8527
b9362cc7 8528static void
6c5b2ebe 8529breakpoint_sals_to_pc (std::vector<symtab_and_line> &sals)
c3f6f71d 8530{
6c5b2ebe
PA
8531 for (auto &sal : sals)
8532 resolve_sal_pc (&sal);
c3f6f71d
JM
8533}
8534
7a697b8d
SS
8535/* Fast tracepoints may have restrictions on valid locations. For
8536 instance, a fast tracepoint using a jump instead of a trap will
8537 likely have to overwrite more bytes than a trap would, and so can
8538 only be placed where the instruction is longer than the jump, or a
8539 multi-instruction sequence does not have a jump into the middle of
8540 it, etc. */
8541
8542static void
8543check_fast_tracepoint_sals (struct gdbarch *gdbarch,
6c5b2ebe 8544 gdb::array_view<const symtab_and_line> sals)
7a697b8d 8545{
6c5b2ebe 8546 for (const auto &sal : sals)
7a697b8d 8547 {
f8eba3c6
TT
8548 struct gdbarch *sarch;
8549
6c5b2ebe 8550 sarch = get_sal_arch (sal);
f8eba3c6
TT
8551 /* We fall back to GDBARCH if there is no architecture
8552 associated with SAL. */
8553 if (sarch == NULL)
8554 sarch = gdbarch;
281d762b
TT
8555 std::string msg;
8556 if (!gdbarch_fast_tracepoint_valid_at (sarch, sal.pc, &msg))
53c3572a 8557 error (_("May not have a fast tracepoint at %s%s"),
281d762b 8558 paddress (sarch, sal.pc), msg.c_str ());
7a697b8d
SS
8559 }
8560}
8561
018d34a4
VP
8562/* Given TOK, a string specification of condition and thread, as
8563 accepted by the 'break' command, extract the condition
8564 string and thread number and set *COND_STRING and *THREAD.
4a64f543 8565 PC identifies the context at which the condition should be parsed.
018d34a4
VP
8566 If no condition is found, *COND_STRING is set to NULL.
8567 If no thread is found, *THREAD is set to -1. */
d634f2de
JB
8568
8569static void
bbc13ae3 8570find_condition_and_thread (const char *tok, CORE_ADDR pc,
6f781ee3
TT
8571 gdb::unique_xmalloc_ptr<char> *cond_string,
8572 int *thread, int *task,
8573 gdb::unique_xmalloc_ptr<char> *rest)
018d34a4 8574{
6f781ee3 8575 cond_string->reset ();
018d34a4 8576 *thread = -1;
ed1d1739 8577 *task = 0;
6f781ee3 8578 rest->reset ();
733d554a 8579 bool force = false;
ed1d1739 8580
018d34a4
VP
8581 while (tok && *tok)
8582 {
bbc13ae3 8583 const char *end_tok;
018d34a4 8584 int toklen;
bbc13ae3
KS
8585 const char *cond_start = NULL;
8586 const char *cond_end = NULL;
cc59ec59 8587
f1735a53 8588 tok = skip_spaces (tok);
e7e0cddf
SS
8589
8590 if ((*tok == '"' || *tok == ',') && rest)
8591 {
6f781ee3 8592 rest->reset (savestring (tok, strlen (tok)));
e7e0cddf
SS
8593 return;
8594 }
8595
f1735a53 8596 end_tok = skip_to_space (tok);
d634f2de 8597
018d34a4 8598 toklen = end_tok - tok;
d634f2de 8599
018d34a4
VP
8600 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
8601 {
8602 tok = cond_start = end_tok + 1;
733d554a
TBA
8603 try
8604 {
8605 parse_exp_1 (&tok, pc, block_for_pc (pc), 0);
8606 }
8607 catch (const gdb_exception_error &)
8608 {
8609 if (!force)
8610 throw;
8611 else
8612 tok = tok + strlen (tok);
8613 }
018d34a4 8614 cond_end = tok;
6f781ee3 8615 cond_string->reset (savestring (cond_start, cond_end - cond_start));
018d34a4 8616 }
733d554a
TBA
8617 else if (toklen >= 1 && strncmp (tok, "-force-condition", toklen) == 0)
8618 {
5759831a 8619 tok = tok + toklen;
733d554a
TBA
8620 force = true;
8621 }
018d34a4
VP
8622 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
8623 {
5d5658a1
PA
8624 const char *tmptok;
8625 struct thread_info *thr;
d634f2de 8626
018d34a4 8627 tok = end_tok + 1;
5d5658a1 8628 thr = parse_thread_id (tok, &tmptok);
018d34a4
VP
8629 if (tok == tmptok)
8630 error (_("Junk after thread keyword."));
5d5658a1 8631 *thread = thr->global_num;
bbc13ae3 8632 tok = tmptok;
018d34a4 8633 }
4a306c9a
JB
8634 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
8635 {
8636 char *tmptok;
8637
8638 tok = end_tok + 1;
bbc13ae3 8639 *task = strtol (tok, &tmptok, 0);
4a306c9a
JB
8640 if (tok == tmptok)
8641 error (_("Junk after task keyword."));
8642 if (!valid_task_id (*task))
b6199126 8643 error (_("Unknown task %d."), *task);
bbc13ae3 8644 tok = tmptok;
4a306c9a 8645 }
e7e0cddf
SS
8646 else if (rest)
8647 {
6f781ee3 8648 rest->reset (savestring (tok, strlen (tok)));
ccab2054 8649 return;
e7e0cddf 8650 }
018d34a4
VP
8651 else
8652 error (_("Junk at end of arguments."));
8653 }
8654}
8655
b5fa468f
TBA
8656/* Call 'find_condition_and_thread' for each sal in SALS until a parse
8657 succeeds. The parsed values are written to COND_STRING, THREAD,
8658 TASK, and REST. See the comment of 'find_condition_and_thread'
8659 for the description of these parameters and INPUT. */
8660
8661static void
8662find_condition_and_thread_for_sals (const std::vector<symtab_and_line> &sals,
6f781ee3
TT
8663 const char *input,
8664 gdb::unique_xmalloc_ptr<char> *cond_string,
8665 int *thread, int *task,
8666 gdb::unique_xmalloc_ptr<char> *rest)
b5fa468f
TBA
8667{
8668 int num_failures = 0;
8669 for (auto &sal : sals)
8670 {
6f781ee3 8671 gdb::unique_xmalloc_ptr<char> cond;
b5fa468f
TBA
8672 int thread_id = 0;
8673 int task_id = 0;
6f781ee3 8674 gdb::unique_xmalloc_ptr<char> remaining;
b5fa468f
TBA
8675
8676 /* Here we want to parse 'arg' to separate condition from thread
8677 number. But because parsing happens in a context and the
8678 contexts of sals might be different, try each until there is
8679 success. Finding one successful parse is sufficient for our
8680 goal. When setting the breakpoint we'll re-parse the
8681 condition in the context of each sal. */
8682 try
8683 {
8684 find_condition_and_thread (input, sal.pc, &cond, &thread_id,
8685 &task_id, &remaining);
6f781ee3 8686 *cond_string = std::move (cond);
b5fa468f
TBA
8687 *thread = thread_id;
8688 *task = task_id;
6f781ee3 8689 *rest = std::move (remaining);
b5fa468f
TBA
8690 break;
8691 }
8692 catch (const gdb_exception_error &e)
8693 {
8694 num_failures++;
8695 /* If no sal remains, do not continue. */
8696 if (num_failures == sals.size ())
8697 throw;
8698 }
8699 }
8700}
8701
0fb4aa4b
PA
8702/* Decode a static tracepoint marker spec. */
8703
6c5b2ebe 8704static std::vector<symtab_and_line>
f00aae0f 8705decode_static_tracepoint_spec (const char **arg_p)
0fb4aa4b 8706{
f00aae0f
KS
8707 const char *p = &(*arg_p)[3];
8708 const char *endp;
0fb4aa4b 8709
f1735a53 8710 p = skip_spaces (p);
0fb4aa4b 8711
f1735a53 8712 endp = skip_to_space (p);
0fb4aa4b 8713
81b1e71c 8714 std::string marker_str (p, endp - p);
0fb4aa4b 8715
5d9310c4
SM
8716 std::vector<static_tracepoint_marker> markers
8717 = target_static_tracepoint_markers_by_strid (marker_str.c_str ());
8718 if (markers.empty ())
81b1e71c
TT
8719 error (_("No known static tracepoint marker named %s"),
8720 marker_str.c_str ());
0fb4aa4b 8721
6c5b2ebe 8722 std::vector<symtab_and_line> sals;
5d9310c4 8723 sals.reserve (markers.size ());
0fb4aa4b 8724
5d9310c4 8725 for (const static_tracepoint_marker &marker : markers)
0fb4aa4b 8726 {
5d9310c4
SM
8727 symtab_and_line sal = find_pc_line (marker.address, 0);
8728 sal.pc = marker.address;
6c5b2ebe 8729 sals.push_back (sal);
5d9310c4 8730 }
0fb4aa4b 8731
0fb4aa4b
PA
8732 *arg_p = endp;
8733 return sals;
8734}
8735
bac7c5cf
GB
8736/* Returns the breakpoint ops appropriate for use with with LOCATION_TYPE and
8737 according to IS_TRACEPOINT. */
8738
8739static const struct breakpoint_ops *
8740breakpoint_ops_for_event_location_type (enum event_location_type location_type,
8741 bool is_tracepoint)
8742{
8743 if (is_tracepoint)
8744 {
8745 if (location_type == PROBE_LOCATION)
8746 return &tracepoint_probe_breakpoint_ops;
8747 else
a0cd5f08 8748 return &base_breakpoint_ops;
bac7c5cf
GB
8749 }
8750 else
8751 {
8752 if (location_type == PROBE_LOCATION)
8753 return &bkpt_probe_breakpoint_ops;
8754 else
a0cd5f08 8755 return &base_breakpoint_ops;
bac7c5cf
GB
8756 }
8757}
8758
8759/* See breakpoint.h. */
8760
8761const struct breakpoint_ops *
8762breakpoint_ops_for_event_location (const struct event_location *location,
8763 bool is_tracepoint)
8764{
8765 if (location != nullptr)
8766 return breakpoint_ops_for_event_location_type
8767 (event_location_type (location), is_tracepoint);
a0cd5f08 8768 return &base_breakpoint_ops;
bac7c5cf
GB
8769}
8770
f00aae0f 8771/* See breakpoint.h. */
0101ce28 8772
8cdf0e15
VP
8773int
8774create_breakpoint (struct gdbarch *gdbarch,
626d2320 8775 struct event_location *location,
e1e01040
PA
8776 const char *cond_string,
8777 int thread, const char *extra_string,
10a636cc 8778 bool force_condition, int parse_extra,
0fb4aa4b 8779 int tempflag, enum bptype type_wanted,
8cdf0e15
VP
8780 int ignore_count,
8781 enum auto_boolean pending_break_support,
c0a91b2b 8782 const struct breakpoint_ops *ops,
44f238bb
PA
8783 int from_tty, int enabled, int internal,
8784 unsigned flags)
c3f6f71d 8785{
7efd8fc2 8786 struct linespec_result canonical;
0101ce28 8787 int pending = 0;
4a306c9a 8788 int task = 0;
86b17b60 8789 int prev_bkpt_count = breakpoint_count;
c3f6f71d 8790
348d480f
PA
8791 gdb_assert (ops != NULL);
8792
f00aae0f
KS
8793 /* If extra_string isn't useful, set it to NULL. */
8794 if (extra_string != NULL && *extra_string == '\0')
8795 extra_string = NULL;
8796
a70b8144 8797 try
b78a6381 8798 {
03ada39e 8799 ops->create_sals_from_location (location, &canonical);
b78a6381 8800 }
230d2906 8801 catch (const gdb_exception_error &e)
0101ce28 8802 {
492d29ea
PA
8803 /* If caller is interested in rc value from parse, set
8804 value. */
8805 if (e.error == NOT_FOUND_ERROR)
0101ce28 8806 {
05ff989b
AC
8807 /* If pending breakpoint support is turned off, throw
8808 error. */
fa8d40ab
JJ
8809
8810 if (pending_break_support == AUTO_BOOLEAN_FALSE)
eedc3f4f 8811 throw;
723a2275
VP
8812
8813 exception_print (gdb_stderr, e);
fa8d40ab 8814
dda83cd7 8815 /* If pending breakpoint support is auto query and the user
05ff989b 8816 selects no, then simply return the error code. */
059fb39f 8817 if (pending_break_support == AUTO_BOOLEAN_AUTO
bfccc43c
YQ
8818 && !nquery (_("Make %s pending on future shared library load? "),
8819 bptype_string (type_wanted)))
fd9b8c24 8820 return 0;
fa8d40ab 8821
05ff989b
AC
8822 /* At this point, either the user was queried about setting
8823 a pending breakpoint and selected yes, or pending
8824 breakpoint behavior is on and thus a pending breakpoint
8825 is defaulted on behalf of the user. */
f00aae0f 8826 pending = 1;
0101ce28 8827 }
492d29ea 8828 else
eedc3f4f 8829 throw;
0101ce28 8830 }
492d29ea 8831
6c5b2ebe 8832 if (!pending && canonical.lsals.empty ())
492d29ea 8833 return 0;
c3f6f71d 8834
c3f6f71d
JM
8835 /* Resolve all line numbers to PC's and verify that the addresses
8836 are ok for the target. */
0101ce28 8837 if (!pending)
f8eba3c6 8838 {
6c5b2ebe
PA
8839 for (auto &lsal : canonical.lsals)
8840 breakpoint_sals_to_pc (lsal.sals);
f8eba3c6 8841 }
c3f6f71d 8842
7a697b8d 8843 /* Fast tracepoints may have additional restrictions on location. */
bfccc43c 8844 if (!pending && type_wanted == bp_fast_tracepoint)
f8eba3c6 8845 {
6c5b2ebe
PA
8846 for (const auto &lsal : canonical.lsals)
8847 check_fast_tracepoint_sals (gdbarch, lsal.sals);
f8eba3c6 8848 }
7a697b8d 8849
c3f6f71d
JM
8850 /* Verify that condition can be parsed, before setting any
8851 breakpoints. Allocate a separate condition expression for each
4a64f543 8852 breakpoint. */
0101ce28 8853 if (!pending)
c3f6f71d 8854 {
e1e01040
PA
8855 gdb::unique_xmalloc_ptr<char> cond_string_copy;
8856 gdb::unique_xmalloc_ptr<char> extra_string_copy;
8857
f00aae0f 8858 if (parse_extra)
dda83cd7 8859 {
6f781ee3
TT
8860 gdb::unique_xmalloc_ptr<char> rest;
8861 gdb::unique_xmalloc_ptr<char> cond;
52d361e1 8862
6c5b2ebe 8863 const linespec_sals &lsal = canonical.lsals[0];
52d361e1 8864
b5fa468f
TBA
8865 find_condition_and_thread_for_sals (lsal.sals, extra_string,
8866 &cond, &thread, &task, &rest);
6f781ee3
TT
8867 cond_string_copy = std::move (cond);
8868 extra_string_copy = std::move (rest);
dda83cd7 8869 }
2f069f6f 8870 else
dda83cd7 8871 {
f00aae0f
KS
8872 if (type_wanted != bp_dprintf
8873 && extra_string != NULL && *extra_string != '\0')
8874 error (_("Garbage '%s' at end of location"), extra_string);
0878d0fa 8875
10a636cc
TBA
8876 /* Check the validity of the condition. We should error out
8877 if the condition is invalid at all of the locations and
8878 if it is not forced. In the PARSE_EXTRA case above, this
8879 check is done when parsing the EXTRA_STRING. */
8880 if (cond_string != nullptr && !force_condition)
8881 {
8882 int num_failures = 0;
8883 const linespec_sals &lsal = canonical.lsals[0];
8884 for (const auto &sal : lsal.sals)
8885 {
8886 const char *cond = cond_string;
8887 try
8888 {
8889 parse_exp_1 (&cond, sal.pc, block_for_pc (sal.pc), 0);
8890 /* One success is sufficient to keep going. */
8891 break;
8892 }
8893 catch (const gdb_exception_error &)
8894 {
8895 num_failures++;
8896 /* If this is the last sal, error out. */
8897 if (num_failures == lsal.sals.size ())
8898 throw;
8899 }
8900 }
8901 }
8902
0878d0fa
YQ
8903 /* Create a private copy of condition string. */
8904 if (cond_string)
e1e01040 8905 cond_string_copy.reset (xstrdup (cond_string));
0878d0fa
YQ
8906 /* Create a private copy of any extra string. */
8907 if (extra_string)
e1e01040 8908 extra_string_copy.reset (xstrdup (extra_string));
dda83cd7 8909 }
0fb4aa4b 8910
52d361e1 8911 ops->create_breakpoints_sal (gdbarch, &canonical,
e1e01040
PA
8912 std::move (cond_string_copy),
8913 std::move (extra_string_copy),
8914 type_wanted,
d9b3f62e 8915 tempflag ? disp_del : disp_donttouch,
ef4848c7 8916 thread, task, ignore_count,
44f238bb 8917 from_tty, enabled, internal, flags);
c906108c 8918 }
0101ce28
JJ
8919 else
8920 {
73063f51
TT
8921 std::unique_ptr <breakpoint> b = new_breakpoint_from_type (gdbarch,
8922 type_wanted);
d28cd78a 8923 b->location = copy_event_location (location);
bfccc43c 8924
f00aae0f
KS
8925 if (parse_extra)
8926 b->cond_string = NULL;
e12c7713
MK
8927 else
8928 {
8929 /* Create a private copy of condition string. */
6f781ee3
TT
8930 b->cond_string.reset (cond_string != NULL
8931 ? xstrdup (cond_string)
8932 : NULL);
15630549 8933 b->thread = thread;
e12c7713 8934 }
f00aae0f
KS
8935
8936 /* Create a private copy of any extra string. */
6f781ee3
TT
8937 b->extra_string.reset (extra_string != NULL
8938 ? xstrdup (extra_string)
8939 : NULL);
0101ce28 8940 b->ignore_count = ignore_count;
0101ce28 8941 b->disposition = tempflag ? disp_del : disp_donttouch;
0d381245 8942 b->condition_not_parsed = 1;
41447f92 8943 b->enable_state = enabled ? bp_enabled : bp_disabled;
cc72b2a2 8944 if ((type_wanted != bp_breakpoint
dda83cd7 8945 && type_wanted != bp_hardware_breakpoint) || thread != -1)
f8eba3c6 8946 b->pspace = current_program_space;
8bea4e01 8947
b270e6f9 8948 install_breakpoint (internal, std::move (b), 0);
0101ce28
JJ
8949 }
8950
6c5b2ebe 8951 if (canonical.lsals.size () > 1)
95a42b64 8952 {
3e43a32a
MS
8953 warning (_("Multiple breakpoints were set.\nUse the "
8954 "\"delete\" command to delete unwanted breakpoints."));
86b17b60 8955 prev_breakpoint_count = prev_bkpt_count;
95a42b64
TT
8956 }
8957
44702360 8958 update_global_location_list (UGLL_MAY_INSERT);
fd9b8c24
PA
8959
8960 return 1;
c3f6f71d 8961}
c906108c 8962
348d480f 8963/* Set a breakpoint.
72b2ff0e
VP
8964 ARG is a string describing breakpoint address,
8965 condition, and thread.
8966 FLAG specifies if a breakpoint is hardware on,
8967 and if breakpoint is temporary, using BP_HARDWARE_FLAG
8968 and BP_TEMPFLAG. */
348d480f 8969
98deb0da 8970static void
f2fc3015 8971break_command_1 (const char *arg, int flag, int from_tty)
c3f6f71d 8972{
72b2ff0e 8973 int tempflag = flag & BP_TEMPFLAG;
0fb4aa4b
PA
8974 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
8975 ? bp_hardware_breakpoint
8976 : bp_breakpoint);
f00aae0f 8977
ffc2605c 8978 event_location_up location = string_to_event_location (&arg, current_language);
bac7c5cf
GB
8979 const struct breakpoint_ops *ops = breakpoint_ops_for_event_location
8980 (location.get (), false /* is_tracepoint */);
c3f6f71d 8981
8cdf0e15 8982 create_breakpoint (get_current_arch (),
ffc2605c 8983 location.get (),
10a636cc 8984 NULL, 0, arg, false, 1 /* parse arg */,
0fb4aa4b 8985 tempflag, type_wanted,
8cdf0e15
VP
8986 0 /* Ignore count */,
8987 pending_break_support,
55aa24fb 8988 ops,
8cdf0e15 8989 from_tty,
84f4c1fe 8990 1 /* enabled */,
44f238bb
PA
8991 0 /* internal */,
8992 0);
c906108c
SS
8993}
8994
c906108c
SS
8995/* Helper function for break_command_1 and disassemble_command. */
8996
8997void
fba45db2 8998resolve_sal_pc (struct symtab_and_line *sal)
c906108c
SS
8999{
9000 CORE_ADDR pc;
9001
9002 if (sal->pc == 0 && sal->symtab != NULL)
9003 {
9004 if (!find_line_pc (sal->symtab, sal->line, &pc))
8a3fe4f8 9005 error (_("No line %d in file \"%s\"."),
05cba821 9006 sal->line, symtab_to_filename_for_display (sal->symtab));
c906108c 9007 sal->pc = pc;
6a048695 9008
4a64f543 9009 /* If this SAL corresponds to a breakpoint inserted using a line
dda83cd7 9010 number, then skip the function prologue if necessary. */
6a048695 9011 if (sal->explicit_line)
059acae7 9012 skip_prologue_sal (sal);
c906108c
SS
9013 }
9014
9015 if (sal->section == 0 && sal->symtab != NULL)
9016 {
346d1dfe 9017 const struct blockvector *bv;
3977b71f 9018 const struct block *b;
c5aa993b 9019 struct symbol *sym;
c906108c 9020
43f3e411 9021 bv = blockvector_for_pc_sect (sal->pc, 0, &b,
c6159652 9022 sal->symtab->compunit ());
c906108c
SS
9023 if (bv != NULL)
9024 {
7f0df278 9025 sym = block_linkage_function (b);
c906108c
SS
9026 if (sym != NULL)
9027 {
3c86fae3
SM
9028 fixup_symbol_section (sym, sal->symtab->compunit ()->objfile ());
9029 sal->section
9030 = sym->obj_section (sal->symtab->compunit ()->objfile ());
c906108c
SS
9031 }
9032 else
9033 {
4a64f543 9034 /* It really is worthwhile to have the section, so we'll
dda83cd7
SM
9035 just have to look harder. This case can be executed
9036 if we have line numbers but no functions (as can
9037 happen in assembly source). */
c906108c 9038
5ed8105e 9039 scoped_restore_current_pspace_and_thread restore_pspace_thread;
6c95b8df 9040 switch_to_program_space_and_thread (sal->pspace);
c906108c 9041
5ed8105e 9042 bound_minimal_symbol msym = lookup_minimal_symbol_by_pc (sal->pc);
7cbd4a93 9043 if (msym.minsym)
1db66e34 9044 sal->section = msym.obj_section ();
c906108c
SS
9045 }
9046 }
9047 }
9048}
9049
9050void
0b39b52e 9051break_command (const char *arg, int from_tty)
c906108c 9052{
db107f19 9053 break_command_1 (arg, 0, from_tty);
c906108c
SS
9054}
9055
c906108c 9056void
0b39b52e 9057tbreak_command (const char *arg, int from_tty)
c906108c 9058{
db107f19 9059 break_command_1 (arg, BP_TEMPFLAG, from_tty);
c906108c
SS
9060}
9061
c906108c 9062static void
0b39b52e 9063hbreak_command (const char *arg, int from_tty)
c906108c 9064{
db107f19 9065 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
c906108c
SS
9066}
9067
9068static void
0b39b52e 9069thbreak_command (const char *arg, int from_tty)
c906108c 9070{
db107f19 9071 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
c906108c
SS
9072}
9073
e7e0cddf
SS
9074/* The dynamic printf command is mostly like a regular breakpoint, but
9075 with a prewired command list consisting of a single output command,
9076 built from extra arguments supplied on the dprintf command
9077 line. */
9078
da821c7b 9079static void
0b39b52e 9080dprintf_command (const char *arg, int from_tty)
e7e0cddf 9081{
ffc2605c 9082 event_location_up location = string_to_event_location (&arg, current_language);
f00aae0f
KS
9083
9084 /* If non-NULL, ARG should have been advanced past the location;
9085 the next character must be ','. */
9086 if (arg != NULL)
9087 {
9088 if (arg[0] != ',' || arg[1] == '\0')
9089 error (_("Format string required"));
9090 else
9091 {
9092 /* Skip the comma. */
9093 ++arg;
9094 }
9095 }
9096
e7e0cddf 9097 create_breakpoint (get_current_arch (),
ffc2605c 9098 location.get (),
10a636cc 9099 NULL, 0, arg, false, 1 /* parse arg */,
e7e0cddf
SS
9100 0, bp_dprintf,
9101 0 /* Ignore count */,
9102 pending_break_support,
a0cd5f08 9103 &base_breakpoint_ops,
e7e0cddf
SS
9104 from_tty,
9105 1 /* enabled */,
9106 0 /* internal */,
9107 0);
9108}
9109
d3ce09f5 9110static void
0b39b52e 9111agent_printf_command (const char *arg, int from_tty)
d3ce09f5
SS
9112{
9113 error (_("May only run agent-printf on the target"));
9114}
9115
ec45bb67 9116/* Implement the "breakpoint_hit" method for ranged breakpoints. */
f1310107 9117
ec45bb67
TT
9118int
9119ranged_breakpoint::breakpoint_hit (const struct bp_location *bl,
9120 const address_space *aspace,
9121 CORE_ADDR bp_addr,
9122 const target_waitstatus &ws)
f1310107 9123{
c272a98c
SM
9124 if (ws.kind () != TARGET_WAITKIND_STOPPED
9125 || ws.sig () != GDB_SIGNAL_TRAP)
09ac7c10
TT
9126 return 0;
9127
f1310107
TJB
9128 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
9129 bl->length, aspace, bp_addr);
9130}
9131
ec45bb67 9132/* Implement the "resources_needed" method for ranged breakpoints. */
f1310107 9133
ec45bb67
TT
9134int
9135ranged_breakpoint::resources_needed (const struct bp_location *bl)
f1310107
TJB
9136{
9137 return target_ranged_break_num_registers ();
9138}
9139
ec45bb67 9140/* Implement the "print_it" method for ranged breakpoints. */
f1310107 9141
ec45bb67 9142enum print_stop_action
7bd86313 9143ranged_breakpoint::print_it (const bpstat *bs) const
f1310107 9144{
ec45bb67 9145 struct bp_location *bl = loc;
79a45e25 9146 struct ui_out *uiout = current_uiout;
f1310107 9147
ec45bb67 9148 gdb_assert (type == bp_hardware_breakpoint);
f1310107
TJB
9149
9150 /* Ranged breakpoints have only one location. */
9151 gdb_assert (bl && bl->next == NULL);
9152
ec45bb67 9153 annotate_breakpoint (number);
f303dbd6
PA
9154
9155 maybe_print_thread_hit_breakpoint (uiout);
9156
ec45bb67 9157 if (disposition == disp_del)
112e8700 9158 uiout->text ("Temporary ranged breakpoint ");
f1310107 9159 else
112e8700
SM
9160 uiout->text ("Ranged breakpoint ");
9161 if (uiout->is_mi_like_p ())
f1310107 9162 {
112e8700 9163 uiout->field_string ("reason",
f1310107 9164 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
ec45bb67 9165 uiout->field_string ("disp", bpdisp_text (disposition));
f1310107 9166 }
ec45bb67 9167 uiout->field_signed ("bkptno", number);
112e8700 9168 uiout->text (", ");
f1310107
TJB
9169
9170 return PRINT_SRC_AND_LOC;
9171}
9172
ec45bb67 9173/* Implement the "print_one" method for ranged breakpoints. */
f1310107 9174
ec45bb67 9175bool
a67bcaba 9176ranged_breakpoint::print_one (bp_location **last_loc) const
f1310107 9177{
ec45bb67 9178 struct bp_location *bl = loc;
f1310107 9179 struct value_print_options opts;
79a45e25 9180 struct ui_out *uiout = current_uiout;
f1310107
TJB
9181
9182 /* Ranged breakpoints have only one location. */
9183 gdb_assert (bl && bl->next == NULL);
9184
9185 get_user_print_options (&opts);
9186
9187 if (opts.addressprint)
9188 /* We don't print the address range here, it will be printed later
9189 by print_one_detail_ranged_breakpoint. */
112e8700 9190 uiout->field_skip ("addr");
f1310107 9191 annotate_field (5);
ec45bb67 9192 print_breakpoint_location (this, bl);
f1310107 9193 *last_loc = bl;
c01e038b
TT
9194
9195 return true;
f1310107
TJB
9196}
9197
ec45bb67 9198/* Implement the "print_one_detail" method for ranged breakpoints. */
f1310107 9199
ec45bb67
TT
9200void
9201ranged_breakpoint::print_one_detail (struct ui_out *uiout) const
f1310107
TJB
9202{
9203 CORE_ADDR address_start, address_end;
ec45bb67 9204 struct bp_location *bl = loc;
d7e74731 9205 string_file stb;
f1310107
TJB
9206
9207 gdb_assert (bl);
9208
9209 address_start = bl->address;
9210 address_end = address_start + bl->length - 1;
9211
112e8700 9212 uiout->text ("\taddress range: ");
d7e74731
PA
9213 stb.printf ("[%s, %s]",
9214 print_core_address (bl->gdbarch, address_start),
9215 print_core_address (bl->gdbarch, address_end));
112e8700
SM
9216 uiout->field_stream ("addr", stb);
9217 uiout->text ("\n");
f1310107
TJB
9218}
9219
ec45bb67 9220/* Implement the "print_mention" method for ranged breakpoints. */
f1310107 9221
ec45bb67 9222void
b713485d 9223ranged_breakpoint::print_mention () const
f1310107 9224{
ec45bb67 9225 struct bp_location *bl = loc;
79a45e25 9226 struct ui_out *uiout = current_uiout;
f1310107
TJB
9227
9228 gdb_assert (bl);
ec45bb67 9229 gdb_assert (type == bp_hardware_breakpoint);
f1310107 9230
2d33446d 9231 uiout->message (_("Hardware assisted ranged breakpoint %d from %s to %s."),
ec45bb67 9232 number, paddress (bl->gdbarch, bl->address),
2d33446d 9233 paddress (bl->gdbarch, bl->address + bl->length - 1));
f1310107
TJB
9234}
9235
ec45bb67 9236/* Implement the "print_recreate" method for ranged breakpoints. */
f1310107 9237
ec45bb67 9238void
4d1ae558 9239ranged_breakpoint::print_recreate (struct ui_file *fp) const
f1310107 9240{
6cb06a8c 9241 gdb_printf (fp, "break-range %s, %s",
ec45bb67
TT
9242 event_location_to_string (location.get ()),
9243 event_location_to_string (location_range_end.get ()));
04d0163c 9244 print_recreate_thread (fp);
f1310107
TJB
9245}
9246
f1310107
TJB
9247/* Find the address where the end of the breakpoint range should be
9248 placed, given the SAL of the end of the range. This is so that if
9249 the user provides a line number, the end of the range is set to the
9250 last instruction of the given line. */
9251
9252static CORE_ADDR
9253find_breakpoint_range_end (struct symtab_and_line sal)
9254{
9255 CORE_ADDR end;
9256
9257 /* If the user provided a PC value, use it. Otherwise,
9258 find the address of the end of the given location. */
9259 if (sal.explicit_pc)
9260 end = sal.pc;
9261 else
9262 {
9263 int ret;
9264 CORE_ADDR start;
9265
9266 ret = find_line_pc_range (sal, &start, &end);
9267 if (!ret)
9268 error (_("Could not find location of the end of the range."));
9269
9270 /* find_line_pc_range returns the start of the next line. */
9271 end--;
9272 }
9273
9274 return end;
9275}
9276
9277/* Implement the "break-range" CLI command. */
9278
9279static void
0b39b52e 9280break_range_command (const char *arg, int from_tty)
f1310107 9281{
f2fc3015 9282 const char *arg_start;
f1310107
TJB
9283 struct linespec_result canonical_start, canonical_end;
9284 int bp_count, can_use_bp, length;
9285 CORE_ADDR end;
f1310107
TJB
9286
9287 /* We don't support software ranged breakpoints. */
9288 if (target_ranged_break_num_registers () < 0)
9289 error (_("This target does not support hardware ranged breakpoints."));
9290
9291 bp_count = hw_breakpoint_used_count ();
9292 bp_count += target_ranged_break_num_registers ();
9293 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
9294 bp_count, 0);
9295 if (can_use_bp < 0)
9296 error (_("Hardware breakpoints used exceeds limit."));
9297
f8eba3c6 9298 arg = skip_spaces (arg);
f1310107
TJB
9299 if (arg == NULL || arg[0] == '\0')
9300 error(_("No address range specified."));
9301
f8eba3c6 9302 arg_start = arg;
ffc2605c
TT
9303 event_location_up start_location = string_to_event_location (&arg,
9304 current_language);
9305 parse_breakpoint_sals (start_location.get (), &canonical_start);
f1310107
TJB
9306
9307 if (arg[0] != ',')
9308 error (_("Too few arguments."));
6c5b2ebe 9309 else if (canonical_start.lsals.empty ())
f1310107 9310 error (_("Could not find location of the beginning of the range."));
f8eba3c6 9311
6c5b2ebe 9312 const linespec_sals &lsal_start = canonical_start.lsals[0];
f8eba3c6 9313
6c5b2ebe
PA
9314 if (canonical_start.lsals.size () > 1
9315 || lsal_start.sals.size () != 1)
f1310107
TJB
9316 error (_("Cannot create a ranged breakpoint with multiple locations."));
9317
6c5b2ebe 9318 const symtab_and_line &sal_start = lsal_start.sals[0];
81b1e71c 9319 std::string addr_string_start (arg_start, arg - arg_start);
f1310107
TJB
9320
9321 arg++; /* Skip the comma. */
f8eba3c6 9322 arg = skip_spaces (arg);
f1310107
TJB
9323
9324 /* Parse the end location. */
9325
f1310107
TJB
9326 arg_start = arg;
9327
f8eba3c6 9328 /* We call decode_line_full directly here instead of using
f1310107
TJB
9329 parse_breakpoint_sals because we need to specify the start location's
9330 symtab and line as the default symtab and line for the end of the
9331 range. This makes it possible to have ranges like "foo.c:27, +14",
9332 where +14 means 14 lines from the start location. */
ffc2605c
TT
9333 event_location_up end_location = string_to_event_location (&arg,
9334 current_language);
9335 decode_line_full (end_location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
f8eba3c6
TT
9336 sal_start.symtab, sal_start.line,
9337 &canonical_end, NULL, NULL);
9338
6c5b2ebe 9339 if (canonical_end.lsals.empty ())
f1310107 9340 error (_("Could not find location of the end of the range."));
f8eba3c6 9341
6c5b2ebe
PA
9342 const linespec_sals &lsal_end = canonical_end.lsals[0];
9343 if (canonical_end.lsals.size () > 1
9344 || lsal_end.sals.size () != 1)
f1310107
TJB
9345 error (_("Cannot create a ranged breakpoint with multiple locations."));
9346
6c5b2ebe 9347 const symtab_and_line &sal_end = lsal_end.sals[0];
f1310107
TJB
9348
9349 end = find_breakpoint_range_end (sal_end);
9350 if (sal_start.pc > end)
177b42fe 9351 error (_("Invalid address range, end precedes start."));
f1310107
TJB
9352
9353 length = end - sal_start.pc + 1;
9354 if (length < 0)
9355 /* Length overflowed. */
9356 error (_("Address range too large."));
9357 else if (length == 1)
9358 {
9359 /* This range is simple enough to be handled by
9360 the `hbreak' command. */
81b1e71c 9361 hbreak_command (&addr_string_start[0], 1);
f1310107
TJB
9362
9363 return;
9364 }
9365
b925bf21
PA
9366 /* Now set up the breakpoint and install it. */
9367
9368 std::unique_ptr<breakpoint> br
9369 (new ranged_breakpoint (get_current_arch (),
9370 sal_start, length,
9371 std::move (start_location),
9372 std::move (end_location)));
f1310107 9373
27a62b43 9374 install_breakpoint (false, std::move (br), true);
f1310107
TJB
9375}
9376
4a64f543
MS
9377/* Return non-zero if EXP is verified as constant. Returned zero
9378 means EXP is variable. Also the constant detection may fail for
9379 some constant expressions and in such case still falsely return
9380 zero. */
2e6e3d9c 9381
dab72643 9382static bool
65d79d4b
SDJ
9383watchpoint_exp_is_const (const struct expression *exp)
9384{
1eaebe02 9385 return exp->op->constant_p ();
65d79d4b
SDJ
9386}
9387
3a292923 9388/* Implement the "re_set" method for watchpoints. */
348d480f 9389
3a292923
TT
9390void
9391watchpoint::re_set ()
348d480f
PA
9392{
9393 /* Watchpoint can be either on expression using entirely global
9394 variables, or it can be on local variables.
9395
9396 Watchpoints of the first kind are never auto-deleted, and even
9397 persist across program restarts. Since they can use variables
9398 from shared libraries, we need to reparse expression as libraries
9399 are loaded and unloaded.
9400
9401 Watchpoints on local variables can also change meaning as result
9402 of solib event. For example, if a watchpoint uses both a local
9403 and a global variables in expression, it's a local watchpoint,
9404 but unloading of a shared library will make the expression
9405 invalid. This is not a very common use case, but we still
9406 re-evaluate expression, to avoid surprises to the user.
9407
9408 Note that for local watchpoints, we re-evaluate it only if
9409 watchpoints frame id is still valid. If it's not, it means the
9410 watchpoint is out of scope and will be deleted soon. In fact,
9411 I'm not sure we'll ever be called in this case.
9412
9413 If a local watchpoint's frame id is still valid, then
3a292923 9414 exp_valid_block is likewise valid, and we can safely use it.
348d480f 9415
3a5c3e22
PA
9416 Don't do anything about disabled watchpoints, since they will be
9417 reevaluated again when enabled. */
3a292923 9418 update_watchpoint (this, 1 /* reparse */);
348d480f
PA
9419}
9420
3a292923 9421/* Implement the "insert" method for hardware watchpoints. */
77b06cd7 9422
3a292923
TT
9423int
9424watchpoint::insert_location (struct bp_location *bl)
77b06cd7 9425{
3a292923 9426 int length = exact ? 1 : bl->length;
e09342b5
TJB
9427
9428 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
3a292923 9429 cond_exp.get ());
77b06cd7
TJB
9430}
9431
3a292923 9432/* Implement the "remove" method for hardware watchpoints. */
77b06cd7 9433
3a292923
TT
9434int
9435watchpoint::remove_location (struct bp_location *bl,
9436 enum remove_bp_reason reason)
77b06cd7 9437{
3a292923 9438 int length = exact ? 1 : bl->length;
e09342b5
TJB
9439
9440 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
3a292923 9441 cond_exp.get ());
e09342b5
TJB
9442}
9443
3a292923
TT
9444int
9445watchpoint::breakpoint_hit (const struct bp_location *bl,
9446 const address_space *aspace, CORE_ADDR bp_addr,
9447 const target_waitstatus &ws)
e09342b5 9448{
348d480f 9449 struct breakpoint *b = bl->owner;
77b06cd7 9450
348d480f
PA
9451 /* Continuable hardware watchpoints are treated as non-existent if the
9452 reason we stopped wasn't a hardware watchpoint (we didn't stop on
9453 some data address). Otherwise gdb won't stop on a break instruction
9454 in the code (not from a breakpoint) when a hardware watchpoint has
9455 been defined. Also skip watchpoints which we know did not trigger
9456 (did not match the data address). */
9457 if (is_hardware_watchpoint (b)
3a292923 9458 && watchpoint_triggered == watch_triggered_no)
348d480f 9459 return 0;
9c06b0b4 9460
348d480f 9461 return 1;
9c06b0b4
TJB
9462}
9463
3a292923
TT
9464void
9465watchpoint::check_status (bpstat *bs)
9c06b0b4 9466{
348d480f 9467 gdb_assert (is_watchpoint (bs->breakpoint_at));
9c06b0b4 9468
348d480f 9469 bpstat_check_watchpoint (bs);
9c06b0b4
TJB
9470}
9471
3a292923
TT
9472/* Implement the "resources_needed" method for hardware
9473 watchpoints. */
9c06b0b4 9474
3a292923
TT
9475int
9476watchpoint::resources_needed (const struct bp_location *bl)
9c06b0b4 9477{
3a292923 9478 int length = exact? 1 : bl->length;
348d480f
PA
9479
9480 return target_region_ok_for_hw_watchpoint (bl->address, length);
9c06b0b4
TJB
9481}
9482
3a292923
TT
9483/* Implement the "works_in_software_mode" method for hardware
9484 watchpoints. */
9c06b0b4 9485
5a61e176 9486bool
3a292923 9487watchpoint::works_in_software_mode () const
9c06b0b4 9488{
efa80663 9489 /* Read and access watchpoints only work with hardware support. */
3a292923 9490 return type == bp_watchpoint || type == bp_hardware_watchpoint;
9c06b0b4
TJB
9491}
9492
3a292923 9493enum print_stop_action
7bd86313 9494watchpoint::print_it (const bpstat *bs) const
9c06b0b4 9495{
348d480f 9496 struct breakpoint *b;
348d480f 9497 enum print_stop_action result;
79a45e25 9498 struct ui_out *uiout = current_uiout;
348d480f
PA
9499
9500 gdb_assert (bs->bp_location_at != NULL);
9501
348d480f
PA
9502 b = bs->breakpoint_at;
9503
f303dbd6
PA
9504 annotate_watchpoint (b->number);
9505 maybe_print_thread_hit_breakpoint (uiout);
9506
d7e74731
PA
9507 string_file stb;
9508
76f9c9cf 9509 gdb::optional<ui_out_emit_tuple> tuple_emitter;
9c06b0b4
TJB
9510 switch (b->type)
9511 {
348d480f 9512 case bp_watchpoint:
9c06b0b4 9513 case bp_hardware_watchpoint:
112e8700
SM
9514 if (uiout->is_mi_like_p ())
9515 uiout->field_string
9516 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
348d480f 9517 mention (b);
76f9c9cf 9518 tuple_emitter.emplace (uiout, "value");
112e8700 9519 uiout->text ("\nOld value = ");
850645cf 9520 watchpoint_value_print (bs->old_val.get (), &stb);
112e8700
SM
9521 uiout->field_stream ("old", stb);
9522 uiout->text ("\nNew value = ");
3a292923 9523 watchpoint_value_print (val.get (), &stb);
112e8700
SM
9524 uiout->field_stream ("new", stb);
9525 uiout->text ("\n");
348d480f
PA
9526 /* More than one watchpoint may have been triggered. */
9527 result = PRINT_UNKNOWN;
9c06b0b4
TJB
9528 break;
9529
9530 case bp_read_watchpoint:
112e8700
SM
9531 if (uiout->is_mi_like_p ())
9532 uiout->field_string
9533 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
348d480f 9534 mention (b);
76f9c9cf 9535 tuple_emitter.emplace (uiout, "value");
112e8700 9536 uiout->text ("\nValue = ");
3a292923 9537 watchpoint_value_print (val.get (), &stb);
112e8700
SM
9538 uiout->field_stream ("value", stb);
9539 uiout->text ("\n");
348d480f 9540 result = PRINT_UNKNOWN;
9c06b0b4
TJB
9541 break;
9542
9543 case bp_access_watchpoint:
348d480f
PA
9544 if (bs->old_val != NULL)
9545 {
112e8700
SM
9546 if (uiout->is_mi_like_p ())
9547 uiout->field_string
9548 ("reason",
348d480f
PA
9549 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
9550 mention (b);
76f9c9cf 9551 tuple_emitter.emplace (uiout, "value");
112e8700 9552 uiout->text ("\nOld value = ");
850645cf 9553 watchpoint_value_print (bs->old_val.get (), &stb);
112e8700
SM
9554 uiout->field_stream ("old", stb);
9555 uiout->text ("\nNew value = ");
348d480f
PA
9556 }
9557 else
9558 {
9559 mention (b);
112e8700
SM
9560 if (uiout->is_mi_like_p ())
9561 uiout->field_string
9562 ("reason",
348d480f 9563 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
76f9c9cf 9564 tuple_emitter.emplace (uiout, "value");
112e8700 9565 uiout->text ("\nValue = ");
348d480f 9566 }
3a292923 9567 watchpoint_value_print (val.get (), &stb);
112e8700
SM
9568 uiout->field_stream ("new", stb);
9569 uiout->text ("\n");
348d480f 9570 result = PRINT_UNKNOWN;
9c06b0b4
TJB
9571 break;
9572 default:
348d480f 9573 result = PRINT_UNKNOWN;
9c06b0b4
TJB
9574 }
9575
348d480f
PA
9576 return result;
9577}
9578
3a292923 9579/* Implement the "print_mention" method for hardware watchpoints. */
348d480f 9580
3a292923 9581void
b713485d 9582watchpoint::print_mention () const
348d480f 9583{
79a45e25 9584 struct ui_out *uiout = current_uiout;
46b9c129 9585 const char *tuple_name;
348d480f 9586
3a292923 9587 switch (type)
348d480f
PA
9588 {
9589 case bp_watchpoint:
112e8700 9590 uiout->text ("Watchpoint ");
46b9c129 9591 tuple_name = "wpt";
348d480f
PA
9592 break;
9593 case bp_hardware_watchpoint:
112e8700 9594 uiout->text ("Hardware watchpoint ");
46b9c129 9595 tuple_name = "wpt";
348d480f
PA
9596 break;
9597 case bp_read_watchpoint:
112e8700 9598 uiout->text ("Hardware read watchpoint ");
46b9c129 9599 tuple_name = "hw-rwpt";
348d480f
PA
9600 break;
9601 case bp_access_watchpoint:
112e8700 9602 uiout->text ("Hardware access (read/write) watchpoint ");
46b9c129 9603 tuple_name = "hw-awpt";
348d480f
PA
9604 break;
9605 default:
9606 internal_error (__FILE__, __LINE__,
9607 _("Invalid hardware watchpoint type."));
9608 }
9609
46b9c129 9610 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
3a292923 9611 uiout->field_signed ("number", number);
112e8700 9612 uiout->text (": ");
3a292923 9613 uiout->field_string ("exp", exp_string.get ());
348d480f
PA
9614}
9615
3a292923 9616/* Implement the "print_recreate" method for watchpoints. */
348d480f 9617
3a292923 9618void
4d1ae558 9619watchpoint::print_recreate (struct ui_file *fp) const
348d480f 9620{
3a292923 9621 switch (type)
348d480f
PA
9622 {
9623 case bp_watchpoint:
9624 case bp_hardware_watchpoint:
6cb06a8c 9625 gdb_printf (fp, "watch");
348d480f
PA
9626 break;
9627 case bp_read_watchpoint:
6cb06a8c 9628 gdb_printf (fp, "rwatch");
348d480f
PA
9629 break;
9630 case bp_access_watchpoint:
6cb06a8c 9631 gdb_printf (fp, "awatch");
348d480f
PA
9632 break;
9633 default:
9634 internal_error (__FILE__, __LINE__,
9635 _("Invalid watchpoint type."));
9636 }
9637
3a292923 9638 gdb_printf (fp, " %s", exp_string.get ());
04d0163c 9639 print_recreate_thread (fp);
348d480f
PA
9640}
9641
3a292923 9642/* Implement the "explains_signal" method for watchpoints. */
427cd150 9643
a6860f3a 9644bool
3a292923 9645watchpoint::explains_signal (enum gdb_signal sig)
427cd150
TT
9646{
9647 /* A software watchpoint cannot cause a signal other than
9648 GDB_SIGNAL_TRAP. */
3a292923 9649 if (type == bp_watchpoint && sig != GDB_SIGNAL_TRAP)
a6860f3a 9650 return false;
427cd150 9651
a6860f3a 9652 return true;
427cd150
TT
9653}
9654
3a292923
TT
9655struct masked_watchpoint : public watchpoint
9656{
73063f51
TT
9657 using watchpoint::watchpoint;
9658
3a292923
TT
9659 int insert_location (struct bp_location *) override;
9660 int remove_location (struct bp_location *,
9661 enum remove_bp_reason reason) override;
9662 int resources_needed (const struct bp_location *) override;
5a61e176 9663 bool works_in_software_mode () const override;
7bd86313 9664 enum print_stop_action print_it (const bpstat *bs) const override;
3a292923 9665 void print_one_detail (struct ui_out *) const override;
b713485d 9666 void print_mention () const override;
4d1ae558 9667 void print_recreate (struct ui_file *fp) const override;
3a292923 9668};
348d480f 9669
3a292923 9670/* Implement the "insert" method for masked hardware watchpoints. */
348d480f 9671
3a292923
TT
9672int
9673masked_watchpoint::insert_location (struct bp_location *bl)
348d480f 9674{
3a292923 9675 return target_insert_mask_watchpoint (bl->address, hw_wp_mask,
348d480f
PA
9676 bl->watchpoint_type);
9677}
9678
3a292923 9679/* Implement the "remove" method for masked hardware watchpoints. */
348d480f 9680
3a292923
TT
9681int
9682masked_watchpoint::remove_location (struct bp_location *bl,
9683 enum remove_bp_reason reason)
348d480f 9684{
3a292923 9685 return target_remove_mask_watchpoint (bl->address, hw_wp_mask,
dda83cd7 9686 bl->watchpoint_type);
348d480f
PA
9687}
9688
3a292923
TT
9689/* Implement the "resources_needed" method for masked hardware
9690 watchpoints. */
348d480f 9691
3a292923
TT
9692int
9693masked_watchpoint::resources_needed (const struct bp_location *bl)
348d480f 9694{
3a292923 9695 return target_masked_watch_num_registers (bl->address, hw_wp_mask);
348d480f
PA
9696}
9697
3a292923
TT
9698/* Implement the "works_in_software_mode" method for masked hardware
9699 watchpoints. */
348d480f 9700
5a61e176 9701bool
3a292923 9702masked_watchpoint::works_in_software_mode () const
348d480f 9703{
5a61e176 9704 return false;
348d480f
PA
9705}
9706
3a292923
TT
9707/* Implement the "print_it" method for masked hardware
9708 watchpoints. */
348d480f 9709
3a292923 9710enum print_stop_action
7bd86313 9711masked_watchpoint::print_it (const bpstat *bs) const
348d480f
PA
9712{
9713 struct breakpoint *b = bs->breakpoint_at;
79a45e25 9714 struct ui_out *uiout = current_uiout;
348d480f
PA
9715
9716 /* Masked watchpoints have only one location. */
9717 gdb_assert (b->loc && b->loc->next == NULL);
9718
f303dbd6
PA
9719 annotate_watchpoint (b->number);
9720 maybe_print_thread_hit_breakpoint (uiout);
9721
348d480f
PA
9722 switch (b->type)
9723 {
9724 case bp_hardware_watchpoint:
112e8700
SM
9725 if (uiout->is_mi_like_p ())
9726 uiout->field_string
9727 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
348d480f
PA
9728 break;
9729
9730 case bp_read_watchpoint:
112e8700
SM
9731 if (uiout->is_mi_like_p ())
9732 uiout->field_string
9733 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
348d480f
PA
9734 break;
9735
9736 case bp_access_watchpoint:
112e8700
SM
9737 if (uiout->is_mi_like_p ())
9738 uiout->field_string
9739 ("reason",
348d480f
PA
9740 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
9741 break;
9742 default:
9743 internal_error (__FILE__, __LINE__,
9744 _("Invalid hardware watchpoint type."));
9745 }
9746
9747 mention (b);
112e8700 9748 uiout->text (_("\n\
9c06b0b4
TJB
9749Check the underlying instruction at PC for the memory\n\
9750address and value which triggered this watchpoint.\n"));
112e8700 9751 uiout->text ("\n");
9c06b0b4
TJB
9752
9753 /* More than one watchpoint may have been triggered. */
9754 return PRINT_UNKNOWN;
9755}
9756
3a292923
TT
9757/* Implement the "print_one_detail" method for masked hardware
9758 watchpoints. */
9c06b0b4 9759
3a292923
TT
9760void
9761masked_watchpoint::print_one_detail (struct ui_out *uiout) const
9c06b0b4
TJB
9762{
9763 /* Masked watchpoints have only one location. */
3a292923 9764 gdb_assert (loc && loc->next == NULL);
9c06b0b4 9765
112e8700 9766 uiout->text ("\tmask ");
3a292923 9767 uiout->field_core_addr ("mask", loc->gdbarch, hw_wp_mask);
112e8700 9768 uiout->text ("\n");
9c06b0b4
TJB
9769}
9770
3a292923
TT
9771/* Implement the "print_mention" method for masked hardware
9772 watchpoints. */
9c06b0b4 9773
3a292923 9774void
b713485d 9775masked_watchpoint::print_mention () const
9c06b0b4 9776{
79a45e25 9777 struct ui_out *uiout = current_uiout;
46b9c129 9778 const char *tuple_name;
9c06b0b4 9779
3a292923 9780 switch (type)
9c06b0b4
TJB
9781 {
9782 case bp_hardware_watchpoint:
112e8700 9783 uiout->text ("Masked hardware watchpoint ");
46b9c129 9784 tuple_name = "wpt";
9c06b0b4
TJB
9785 break;
9786 case bp_read_watchpoint:
112e8700 9787 uiout->text ("Masked hardware read watchpoint ");
46b9c129 9788 tuple_name = "hw-rwpt";
9c06b0b4
TJB
9789 break;
9790 case bp_access_watchpoint:
112e8700 9791 uiout->text ("Masked hardware access (read/write) watchpoint ");
46b9c129 9792 tuple_name = "hw-awpt";
9c06b0b4
TJB
9793 break;
9794 default:
9795 internal_error (__FILE__, __LINE__,
9796 _("Invalid hardware watchpoint type."));
9797 }
9798
46b9c129 9799 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
3a292923 9800 uiout->field_signed ("number", number);
112e8700 9801 uiout->text (": ");
3a292923 9802 uiout->field_string ("exp", exp_string.get ());
9c06b0b4
TJB
9803}
9804
3a292923
TT
9805/* Implement the "print_recreate" method for masked hardware
9806 watchpoints. */
9c06b0b4 9807
3a292923 9808void
4d1ae558 9809masked_watchpoint::print_recreate (struct ui_file *fp) const
9c06b0b4 9810{
3a292923 9811 switch (type)
9c06b0b4
TJB
9812 {
9813 case bp_hardware_watchpoint:
6cb06a8c 9814 gdb_printf (fp, "watch");
9c06b0b4
TJB
9815 break;
9816 case bp_read_watchpoint:
6cb06a8c 9817 gdb_printf (fp, "rwatch");
9c06b0b4
TJB
9818 break;
9819 case bp_access_watchpoint:
6cb06a8c 9820 gdb_printf (fp, "awatch");
9c06b0b4
TJB
9821 break;
9822 default:
9823 internal_error (__FILE__, __LINE__,
9824 _("Invalid hardware watchpoint type."));
9825 }
9826
3a292923
TT
9827 gdb_printf (fp, " %s mask 0x%s", exp_string.get (),
9828 phex (hw_wp_mask, sizeof (CORE_ADDR)));
04d0163c 9829 print_recreate_thread (fp);
9c06b0b4
TJB
9830}
9831
9c06b0b4
TJB
9832/* Tell whether the given watchpoint is a masked hardware watchpoint. */
9833
f2478a7e 9834static bool
9c06b0b4
TJB
9835is_masked_watchpoint (const struct breakpoint *b)
9836{
3a292923 9837 return dynamic_cast<const masked_watchpoint *> (b) != nullptr;
9c06b0b4
TJB
9838}
9839
53a5351d 9840/* accessflag: hw_write: watch write,
dda83cd7 9841 hw_read: watch read,
53a5351d 9842 hw_access: watch access (read or write) */
c906108c 9843static void
bbc13ae3 9844watch_command_1 (const char *arg, int accessflag, int from_tty,
2e362716 9845 bool just_location, bool internal)
c906108c 9846{
c1fc2657 9847 struct breakpoint *scope_breakpoint = NULL;
270140bd 9848 const struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
b926417a 9849 struct value *result;
bb9d5f81 9850 int saved_bitpos = 0, saved_bitsize = 0;
bbc13ae3
KS
9851 const char *exp_start = NULL;
9852 const char *exp_end = NULL;
9853 const char *tok, *end_tok;
9c06b0b4 9854 int toklen = -1;
bbc13ae3
KS
9855 const char *cond_start = NULL;
9856 const char *cond_end = NULL;
c906108c 9857 enum bptype bp_type;
37e4754d 9858 int thread = -1;
9c06b0b4
TJB
9859 /* Flag to indicate whether we are going to use masks for
9860 the hardware watchpoint. */
2e362716 9861 bool use_mask = false;
9c06b0b4 9862 CORE_ADDR mask = 0;
8a18382f 9863 int task = 0;
c906108c 9864
37e4754d
LM
9865 /* Make sure that we actually have parameters to parse. */
9866 if (arg != NULL && arg[0] != '\0')
9867 {
bbc13ae3
KS
9868 const char *value_start;
9869
9870 exp_end = arg + strlen (arg);
37e4754d 9871
9c06b0b4
TJB
9872 /* Look for "parameter value" pairs at the end
9873 of the arguments string. */
bbc13ae3 9874 for (tok = exp_end - 1; tok > arg; tok--)
9c06b0b4
TJB
9875 {
9876 /* Skip whitespace at the end of the argument list. */
9877 while (tok > arg && (*tok == ' ' || *tok == '\t'))
9878 tok--;
9879
9880 /* Find the beginning of the last token.
9881 This is the value of the parameter. */
9882 while (tok > arg && (*tok != ' ' && *tok != '\t'))
9883 tok--;
9884 value_start = tok + 1;
9885
9886 /* Skip whitespace. */
9887 while (tok > arg && (*tok == ' ' || *tok == '\t'))
9888 tok--;
9889
9890 end_tok = tok;
9891
9892 /* Find the beginning of the second to last token.
9893 This is the parameter itself. */
9894 while (tok > arg && (*tok != ' ' && *tok != '\t'))
9895 tok--;
9896 tok++;
9897 toklen = end_tok - tok + 1;
9898
61012eef 9899 if (toklen == 6 && startswith (tok, "thread"))
9c06b0b4 9900 {
5d5658a1 9901 struct thread_info *thr;
9c06b0b4
TJB
9902 /* At this point we've found a "thread" token, which means
9903 the user is trying to set a watchpoint that triggers
9904 only in a specific thread. */
5d5658a1 9905 const char *endp;
37e4754d 9906
9c06b0b4
TJB
9907 if (thread != -1)
9908 error(_("You can specify only one thread."));
37e4754d 9909
9c06b0b4 9910 /* Extract the thread ID from the next token. */
5d5658a1 9911 thr = parse_thread_id (value_start, &endp);
37e4754d 9912
5d5658a1 9913 /* Check if the user provided a valid thread ID. */
9c06b0b4 9914 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
5d5658a1 9915 invalid_thread_id_error (value_start);
9c06b0b4 9916
5d5658a1 9917 thread = thr->global_num;
9c06b0b4 9918 }
8a18382f
TT
9919 else if (toklen == 4 && startswith (tok, "task"))
9920 {
9921 char *tmp;
9922
9923 task = strtol (value_start, &tmp, 0);
9924 if (tmp == value_start)
9925 error (_("Junk after task keyword."));
9926 if (!valid_task_id (task))
9927 error (_("Unknown task %d."), task);
9928 }
61012eef 9929 else if (toklen == 4 && startswith (tok, "mask"))
9c06b0b4
TJB
9930 {
9931 /* We've found a "mask" token, which means the user wants to
9932 create a hardware watchpoint that is going to have the mask
9933 facility. */
9934 struct value *mask_value, *mark;
37e4754d 9935
9c06b0b4
TJB
9936 if (use_mask)
9937 error(_("You can specify only one mask."));
37e4754d 9938
2e362716 9939 use_mask = just_location = true;
37e4754d 9940
9c06b0b4
TJB
9941 mark = value_mark ();
9942 mask_value = parse_to_comma_and_eval (&value_start);
9943 mask = value_as_address (mask_value);
9944 value_free_to_mark (mark);
9945 }
9946 else
9947 /* We didn't recognize what we found. We should stop here. */
9948 break;
37e4754d 9949
9c06b0b4
TJB
9950 /* Truncate the string and get rid of the "parameter value" pair before
9951 the arguments string is parsed by the parse_exp_1 function. */
bbc13ae3 9952 exp_end = tok;
9c06b0b4 9953 }
37e4754d 9954 }
bbc13ae3
KS
9955 else
9956 exp_end = arg;
37e4754d 9957
bbc13ae3
KS
9958 /* Parse the rest of the arguments. From here on out, everything
9959 is in terms of a newly allocated string instead of the original
9960 ARG. */
81b1e71c
TT
9961 std::string expression (arg, exp_end - arg);
9962 exp_start = arg = expression.c_str ();
699bd4cf
TT
9963 innermost_block_tracker tracker;
9964 expression_up exp = parse_exp_1 (&arg, 0, 0, 0, &tracker);
c906108c 9965 exp_end = arg;
fa8a61dc
TT
9966 /* Remove trailing whitespace from the expression before saving it.
9967 This makes the eventual display of the expression string a bit
9968 prettier. */
9969 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
9970 --exp_end;
9971
65d79d4b 9972 /* Checking if the expression is not constant. */
4d01a485 9973 if (watchpoint_exp_is_const (exp.get ()))
65d79d4b
SDJ
9974 {
9975 int len;
9976
9977 len = exp_end - exp_start;
9978 while (len > 0 && isspace (exp_start[len - 1]))
9979 len--;
9980 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
9981 }
9982
699bd4cf 9983 exp_valid_block = tracker.block ();
b926417a 9984 struct value *mark = value_mark ();
850645cf 9985 struct value *val_as_value = nullptr;
1eaebe02
TT
9986 fetch_subexp_value (exp.get (), exp->op.get (), &val_as_value, &result, NULL,
9987 just_location);
06a64a0b 9988
850645cf 9989 if (val_as_value != NULL && just_location)
bb9d5f81 9990 {
850645cf
TT
9991 saved_bitpos = value_bitpos (val_as_value);
9992 saved_bitsize = value_bitsize (val_as_value);
bb9d5f81
PP
9993 }
9994
850645cf 9995 value_ref_ptr val;
06a64a0b
TT
9996 if (just_location)
9997 {
9c06b0b4
TJB
9998 int ret;
9999
06a64a0b 10000 exp_valid_block = NULL;
850645cf 10001 val = release_value (value_addr (result));
06a64a0b 10002 value_free_to_mark (mark);
9c06b0b4
TJB
10003
10004 if (use_mask)
10005 {
850645cf 10006 ret = target_masked_watch_num_registers (value_as_address (val.get ()),
9c06b0b4
TJB
10007 mask);
10008 if (ret == -1)
10009 error (_("This target does not support masked watchpoints."));
10010 else if (ret == -2)
10011 error (_("Invalid mask or memory region."));
10012 }
06a64a0b 10013 }
850645cf
TT
10014 else if (val_as_value != NULL)
10015 val = release_value (val_as_value);
c906108c 10016
f1735a53
TT
10017 tok = skip_spaces (arg);
10018 end_tok = skip_to_space (tok);
c906108c
SS
10019
10020 toklen = end_tok - tok;
10021 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
10022 {
10023 tok = cond_start = end_tok + 1;
699bd4cf
TT
10024 innermost_block_tracker if_tracker;
10025 parse_exp_1 (&tok, 0, 0, 0, &if_tracker);
60e1c644
PA
10026
10027 /* The watchpoint expression may not be local, but the condition
10028 may still be. E.g.: `watch global if local > 0'. */
699bd4cf 10029 cond_exp_valid_block = if_tracker.block ();
60e1c644 10030
c906108c
SS
10031 cond_end = tok;
10032 }
10033 if (*tok)
8a3fe4f8 10034 error (_("Junk at end of command."));
c906108c 10035
441d7c93
PA
10036 frame_info *wp_frame = block_innermost_frame (exp_valid_block);
10037
10038 /* Save this because create_internal_breakpoint below invalidates
10039 'wp_frame'. */
10040 frame_id watchpoint_frame = get_frame_id (wp_frame);
d983da9c
DJ
10041
10042 /* If the expression is "local", then set up a "watchpoint scope"
10043 breakpoint at the point where we've left the scope of the watchpoint
10044 expression. Create the scope breakpoint before the watchpoint, so
10045 that we will encounter it first in bpstat_stop_status. */
441d7c93 10046 if (exp_valid_block != NULL && wp_frame != NULL)
d983da9c 10047 {
441d7c93
PA
10048 frame_id caller_frame_id = frame_unwind_caller_id (wp_frame);
10049
10050 if (frame_id_p (caller_frame_id))
edb3359d 10051 {
441d7c93
PA
10052 gdbarch *caller_arch = frame_unwind_caller_arch (wp_frame);
10053 CORE_ADDR caller_pc = frame_unwind_caller_pc (wp_frame);
10054
24b21115 10055 scope_breakpoint
441d7c93 10056 = create_internal_breakpoint (caller_arch, caller_pc,
9efa3c7f 10057 bp_watchpoint_scope);
d983da9c 10058
441d7c93
PA
10059 /* create_internal_breakpoint could invalidate WP_FRAME. */
10060 wp_frame = NULL;
10061
edb3359d 10062 scope_breakpoint->enable_state = bp_enabled;
d983da9c 10063
edb3359d
DJ
10064 /* Automatically delete the breakpoint when it hits. */
10065 scope_breakpoint->disposition = disp_del;
d983da9c 10066
edb3359d 10067 /* Only break in the proper frame (help with recursion). */
441d7c93 10068 scope_breakpoint->frame_id = caller_frame_id;
d983da9c 10069
edb3359d 10070 /* Set the address at which we will stop. */
441d7c93
PA
10071 scope_breakpoint->loc->gdbarch = caller_arch;
10072 scope_breakpoint->loc->requested_address = caller_pc;
edb3359d 10073 scope_breakpoint->loc->address
a6d9a66e
UW
10074 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
10075 scope_breakpoint->loc->requested_address,
edb3359d
DJ
10076 scope_breakpoint->type);
10077 }
d983da9c
DJ
10078 }
10079
e8369a73
AB
10080 /* Now set up the breakpoint. We create all watchpoints as hardware
10081 watchpoints here even if hardware watchpoints are turned off, a call
10082 to update_watchpoint later in this function will cause the type to
10083 drop back to bp_watchpoint (software watchpoint) if required. */
10084
10085 if (accessflag == hw_read)
10086 bp_type = bp_read_watchpoint;
10087 else if (accessflag == hw_access)
10088 bp_type = bp_access_watchpoint;
10089 else
10090 bp_type = bp_hardware_watchpoint;
3a5c3e22 10091
3a292923 10092 std::unique_ptr<watchpoint> w;
348d480f 10093 if (use_mask)
73063f51 10094 w.reset (new masked_watchpoint (nullptr, bp_type));
348d480f 10095 else
73063f51 10096 w.reset (new watchpoint (nullptr, bp_type));
3a292923 10097
c1fc2657 10098 w->thread = thread;
8a18382f 10099 w->task = task;
c1fc2657
SM
10100 w->disposition = disp_donttouch;
10101 w->pspace = current_program_space;
b22e99fd 10102 w->exp = std::move (exp);
3a5c3e22
PA
10103 w->exp_valid_block = exp_valid_block;
10104 w->cond_exp_valid_block = cond_exp_valid_block;
06a64a0b
TT
10105 if (just_location)
10106 {
850645cf
TT
10107 struct type *t = value_type (val.get ());
10108 CORE_ADDR addr = value_as_address (val.get ());
06a64a0b 10109
43cc5389 10110 w->exp_string_reparse
a4c50be3 10111 = current_language->watch_location_expression (t, addr);
06a64a0b 10112
8579fd13
AB
10113 w->exp_string = xstrprintf ("-location %.*s",
10114 (int) (exp_end - exp_start), exp_start);
06a64a0b
TT
10115 }
10116 else
a4c50be3 10117 w->exp_string.reset (savestring (exp_start, exp_end - exp_start));
9c06b0b4
TJB
10118
10119 if (use_mask)
10120 {
3a5c3e22 10121 w->hw_wp_mask = mask;
9c06b0b4
TJB
10122 }
10123 else
10124 {
3a5c3e22 10125 w->val = val;
bb9d5f81
PP
10126 w->val_bitpos = saved_bitpos;
10127 w->val_bitsize = saved_bitsize;
4c1d86d9 10128 w->val_valid = true;
9c06b0b4 10129 }
77b06cd7 10130
c906108c 10131 if (cond_start)
6f781ee3 10132 w->cond_string.reset (savestring (cond_start, cond_end - cond_start));
c906108c 10133 else
c1fc2657 10134 w->cond_string = 0;
c5aa993b 10135
441d7c93 10136 if (frame_id_p (watchpoint_frame))
f6bc2008 10137 {
441d7c93 10138 w->watchpoint_frame = watchpoint_frame;
3a5c3e22 10139 w->watchpoint_thread = inferior_ptid;
f6bc2008 10140 }
c906108c 10141 else
f6bc2008 10142 {
3a5c3e22
PA
10143 w->watchpoint_frame = null_frame_id;
10144 w->watchpoint_thread = null_ptid;
f6bc2008 10145 }
c906108c 10146
d983da9c 10147 if (scope_breakpoint != NULL)
c906108c 10148 {
d983da9c
DJ
10149 /* The scope breakpoint is related to the watchpoint. We will
10150 need to act on them together. */
c1fc2657 10151 w->related_breakpoint = scope_breakpoint;
b270e6f9 10152 scope_breakpoint->related_breakpoint = w.get ();
c906108c 10153 }
d983da9c 10154
06a64a0b
TT
10155 if (!just_location)
10156 value_free_to_mark (mark);
2d134ed3 10157
b270e6f9
TT
10158 /* Finally update the new watchpoint. This creates the locations
10159 that should be inserted. */
10160 update_watchpoint (w.get (), 1);
a9634178 10161
b270e6f9 10162 install_breakpoint (internal, std::move (w), 1);
c906108c
SS
10163}
10164
e09342b5 10165/* Return count of debug registers needed to watch the given expression.
e09342b5 10166 If the watchpoint cannot be handled in hardware return zero. */
c906108c 10167
c906108c 10168static int
a6535de1 10169can_use_hardware_watchpoint (const std::vector<value_ref_ptr> &vals)
c906108c
SS
10170{
10171 int found_memory_cnt = 0;
10172
10173 /* Did the user specifically forbid us to use hardware watchpoints? */
c5aa993b 10174 if (!can_use_hw_watchpoints)
c906108c 10175 return 0;
c5aa993b 10176
a6535de1
TT
10177 gdb_assert (!vals.empty ());
10178 struct value *head = vals[0].get ();
10179
5c44784c
JM
10180 /* Make sure that the value of the expression depends only upon
10181 memory contents, and values computed from them within GDB. If we
10182 find any register references or function calls, we can't use a
10183 hardware watchpoint.
10184
10185 The idea here is that evaluating an expression generates a series
10186 of values, one holding the value of every subexpression. (The
10187 expression a*b+c has five subexpressions: a, b, a*b, c, and
10188 a*b+c.) GDB's values hold almost enough information to establish
10189 the criteria given above --- they identify memory lvalues,
10190 register lvalues, computed values, etcetera. So we can evaluate
10191 the expression, and then scan the chain of values that leaves
10192 behind to decide whether we can detect any possible change to the
10193 expression's final value using only hardware watchpoints.
10194
10195 However, I don't think that the values returned by inferior
10196 function calls are special in any way. So this function may not
10197 notice that an expression involving an inferior function call
10198 can't be watched with hardware watchpoints. FIXME. */
a6535de1 10199 for (const value_ref_ptr &iter : vals)
c906108c 10200 {
a6535de1
TT
10201 struct value *v = iter.get ();
10202
5c44784c 10203 if (VALUE_LVAL (v) == lval_memory)
c906108c 10204 {
8464be76
DJ
10205 if (v != head && value_lazy (v))
10206 /* A lazy memory lvalue in the chain is one that GDB never
10207 needed to fetch; we either just used its address (e.g.,
10208 `a' in `a.b') or we never needed it at all (e.g., `a'
10209 in `a,b'). This doesn't apply to HEAD; if that is
10210 lazy then it was not readable, but watch it anyway. */
5c44784c 10211 ;
53a5351d 10212 else
5c44784c
JM
10213 {
10214 /* Ahh, memory we actually used! Check if we can cover
dda83cd7 10215 it with hardware watchpoints. */
df407dfe 10216 struct type *vtype = check_typedef (value_type (v));
2e70b7b9
MS
10217
10218 /* We only watch structs and arrays if user asked for it
10219 explicitly, never if they just happen to appear in a
10220 middle of some value chain. */
10221 if (v == head
78134374
SM
10222 || (vtype->code () != TYPE_CODE_STRUCT
10223 && vtype->code () != TYPE_CODE_ARRAY))
2e70b7b9 10224 {
42ae5230 10225 CORE_ADDR vaddr = value_address (v);
e09342b5
TJB
10226 int len;
10227 int num_regs;
10228
a9634178 10229 len = (target_exact_watchpoints
e09342b5
TJB
10230 && is_scalar_type_recursive (vtype))?
10231 1 : TYPE_LENGTH (value_type (v));
2e70b7b9 10232
e09342b5
TJB
10233 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
10234 if (!num_regs)
2e70b7b9
MS
10235 return 0;
10236 else
e09342b5 10237 found_memory_cnt += num_regs;
2e70b7b9 10238 }
5c44784c 10239 }
c5aa993b 10240 }
5086187c
AC
10241 else if (VALUE_LVAL (v) != not_lval
10242 && deprecated_value_modifiable (v) == 0)
38b6c3b3 10243 return 0; /* These are values from the history (e.g., $1). */
5086187c 10244 else if (VALUE_LVAL (v) == lval_register)
38b6c3b3 10245 return 0; /* Cannot watch a register with a HW watchpoint. */
c906108c
SS
10246 }
10247
10248 /* The expression itself looks suitable for using a hardware
10249 watchpoint, but give the target machine a chance to reject it. */
10250 return found_memory_cnt;
10251}
10252
8b93c638 10253void
2e362716 10254watch_command_wrapper (const char *arg, int from_tty, bool internal)
8b93c638 10255{
84f4c1fe 10256 watch_command_1 (arg, hw_write, from_tty, 0, internal);
06a64a0b
TT
10257}
10258
a15a5258
AB
10259/* Options for the watch, awatch, and rwatch commands. */
10260
10261struct watch_options
10262{
10263 /* For -location. */
10264 bool location = false;
10265};
10266
10267/* Definitions of options for the "watch", "awatch", and "rwatch" commands.
10268
10269 Historically GDB always accepted both '-location' and '-l' flags for
10270 these commands (both flags being synonyms). When converting to the
10271 newer option scheme only '-location' is added here. That's fine (for
10272 backward compatibility) as any non-ambiguous prefix of a flag will be
10273 accepted, so '-l', '-loc', are now all accepted.
10274
10275 What this means is that, if in the future, we add any new flag here
10276 that starts with '-l' then this will break backward compatibility, so
10277 please, don't do that! */
10278
10279static const gdb::option::option_def watch_option_defs[] = {
10280 gdb::option::flag_option_def<watch_options> {
10281 "location",
10282 [] (watch_options *opt) { return &opt->location; },
10283 N_("\
10284This evaluates EXPRESSION and watches the memory to which is refers.\n\
10285-l can be used as a short form of -location."),
10286 },
10287};
10288
10289/* Returns the option group used by 'watch', 'awatch', and 'rwatch'
10290 commands. */
10291
10292static gdb::option::option_def_group
10293make_watch_options_def_group (watch_options *opts)
10294{
10295 return {{watch_option_defs}, opts};
10296}
10297
06a64a0b
TT
10298/* A helper function that looks for the "-location" argument and then
10299 calls watch_command_1. */
10300
10301static void
0b39b52e 10302watch_maybe_just_location (const char *arg, int accessflag, int from_tty)
06a64a0b 10303{
a15a5258
AB
10304 watch_options opts;
10305 auto grp = make_watch_options_def_group (&opts);
10306 gdb::option::process_options
10307 (&arg, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND, grp);
10308 if (arg != nullptr && *arg == '\0')
10309 arg = nullptr;
10310
10311 watch_command_1 (arg, accessflag, from_tty, opts.location, false);
10312}
06a64a0b 10313
a15a5258
AB
10314/* Command completion for 'watch', 'awatch', and 'rwatch' commands. */
10315static void
10316watch_command_completer (struct cmd_list_element *ignore,
10317 completion_tracker &tracker,
10318 const char *text, const char * /*word*/)
10319{
10320 const auto group = make_watch_options_def_group (nullptr);
10321 if (gdb::option::complete_options
10322 (tracker, &text, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND, group))
10323 return;
06a64a0b 10324
a15a5258
AB
10325 const char *word = advance_to_expression_complete_word_point (tracker, text);
10326 expression_completer (ignore, tracker, text, word);
8b93c638 10327}
8926118c 10328
c5aa993b 10329static void
0b39b52e 10330watch_command (const char *arg, int from_tty)
c906108c 10331{
06a64a0b 10332 watch_maybe_just_location (arg, hw_write, from_tty);
c906108c
SS
10333}
10334
8b93c638 10335void
2e362716 10336rwatch_command_wrapper (const char *arg, int from_tty, bool internal)
8b93c638 10337{
84f4c1fe 10338 watch_command_1 (arg, hw_read, from_tty, 0, internal);
8b93c638 10339}
8926118c 10340
c5aa993b 10341static void
0b39b52e 10342rwatch_command (const char *arg, int from_tty)
c906108c 10343{
06a64a0b 10344 watch_maybe_just_location (arg, hw_read, from_tty);
c906108c
SS
10345}
10346
8b93c638 10347void
2e362716 10348awatch_command_wrapper (const char *arg, int from_tty, bool internal)
8b93c638 10349{
84f4c1fe 10350 watch_command_1 (arg, hw_access, from_tty, 0, internal);
8b93c638 10351}
8926118c 10352
c5aa993b 10353static void
0b39b52e 10354awatch_command (const char *arg, int from_tty)
c906108c 10355{
06a64a0b 10356 watch_maybe_just_location (arg, hw_access, from_tty);
c906108c 10357}
c906108c 10358\f
c5aa993b 10359
cfc31633
PA
10360/* Data for the FSM that manages the until(location)/advance commands
10361 in infcmd.c. Here because it uses the mechanisms of
10362 breakpoints. */
c906108c 10363
46e3ed7f 10364struct until_break_fsm : public thread_fsm
bfec99b2 10365{
46e3ed7f 10366 /* The thread that was current when the command was executed. */
cfc31633
PA
10367 int thread;
10368
df631783
PA
10369 /* The breakpoint set at the return address in the caller frame,
10370 plus breakpoints at all the destination locations. */
10371 std::vector<breakpoint_up> breakpoints;
cfc31633 10372
46e3ed7f 10373 until_break_fsm (struct interp *cmd_interp, int thread,
df631783 10374 std::vector<breakpoint_up> &&breakpoints)
46e3ed7f
TT
10375 : thread_fsm (cmd_interp),
10376 thread (thread),
df631783 10377 breakpoints (std::move (breakpoints))
46e3ed7f
TT
10378 {
10379 }
cfc31633 10380
46e3ed7f
TT
10381 void clean_up (struct thread_info *thread) override;
10382 bool should_stop (struct thread_info *thread) override;
10383 enum async_reply_reason do_async_reply_reason () override;
cfc31633
PA
10384};
10385
cfc31633
PA
10386/* Implementation of the 'should_stop' FSM method for the
10387 until(location)/advance commands. */
10388
46e3ed7f
TT
10389bool
10390until_break_fsm::should_stop (struct thread_info *tp)
cfc31633 10391{
df631783
PA
10392 for (const breakpoint_up &bp : breakpoints)
10393 if (bpstat_find_breakpoint (tp->control.stop_bpstat,
10394 bp.get ()) != NULL)
10395 {
10396 set_finished ();
10397 break;
10398 }
cfc31633 10399
46e3ed7f 10400 return true;
cfc31633
PA
10401}
10402
10403/* Implementation of the 'clean_up' FSM method for the
10404 until(location)/advance commands. */
10405
46e3ed7f
TT
10406void
10407until_break_fsm::clean_up (struct thread_info *)
43ff13b4 10408{
cfc31633 10409 /* Clean up our temporary breakpoints. */
df631783 10410 breakpoints.clear ();
46e3ed7f 10411 delete_longjmp_breakpoint (thread);
cfc31633
PA
10412}
10413
10414/* Implementation of the 'async_reply_reason' FSM method for the
10415 until(location)/advance commands. */
10416
46e3ed7f
TT
10417enum async_reply_reason
10418until_break_fsm::do_async_reply_reason ()
cfc31633
PA
10419{
10420 return EXEC_ASYNC_LOCATION_REACHED;
43ff13b4
JM
10421}
10422
c906108c 10423void
f2fc3015 10424until_break_command (const char *arg, int from_tty, int anywhere)
c906108c 10425{
8556afb4
PA
10426 struct frame_info *frame;
10427 struct gdbarch *frame_gdbarch;
10428 struct frame_id stack_frame_id;
10429 struct frame_id caller_frame_id;
186c406b
TT
10430 int thread;
10431 struct thread_info *tp;
c906108c 10432
70509625 10433 clear_proceed_status (0);
c906108c
SS
10434
10435 /* Set a breakpoint where the user wants it and at return from
4a64f543 10436 this function. */
c5aa993b 10437
ffc2605c 10438 event_location_up location = string_to_event_location (&arg, current_language);
f00aae0f 10439
6c5b2ebe
PA
10440 std::vector<symtab_and_line> sals
10441 = (last_displayed_sal_is_valid ()
10442 ? decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
10443 get_last_displayed_symtab (),
10444 get_last_displayed_line ())
10445 : decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE,
cafb3438 10446 NULL, NULL, 0));
c5aa993b 10447
df631783 10448 if (sals.empty ())
8a3fe4f8 10449 error (_("Couldn't get information on specified line."));
c5aa993b 10450
c906108c 10451 if (*arg)
8a3fe4f8 10452 error (_("Junk at end of arguments."));
c5aa993b 10453
186c406b 10454 tp = inferior_thread ();
5d5658a1 10455 thread = tp->global_num;
186c406b 10456
8556afb4
PA
10457 /* Note linespec handling above invalidates the frame chain.
10458 Installing a breakpoint also invalidates the frame chain (as it
10459 may need to switch threads), so do any frame handling before
10460 that. */
10461
10462 frame = get_selected_frame (NULL);
10463 frame_gdbarch = get_frame_arch (frame);
10464 stack_frame_id = get_stack_frame_id (frame);
10465 caller_frame_id = frame_unwind_caller_id (frame);
883bc8d1 10466
ae66c1fc
EZ
10467 /* Keep within the current frame, or in frames called by the current
10468 one. */
edb3359d 10469
df631783 10470 std::vector<breakpoint_up> breakpoints;
5419bdae
TT
10471
10472 gdb::optional<delete_longjmp_breakpoint_cleanup> lj_deleter;
10473
883bc8d1 10474 if (frame_id_p (caller_frame_id))
c906108c 10475 {
883bc8d1 10476 struct symtab_and_line sal2;
cfc31633 10477 struct gdbarch *caller_gdbarch;
883bc8d1
PA
10478
10479 sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
10480 sal2.pc = frame_unwind_caller_pc (frame);
cfc31633 10481 caller_gdbarch = frame_unwind_caller_arch (frame);
df631783
PA
10482
10483 breakpoint_up caller_breakpoint
10484 = set_momentary_breakpoint (caller_gdbarch, sal2,
10485 caller_frame_id, bp_until);
10486 breakpoints.emplace_back (std::move (caller_breakpoint));
186c406b 10487
883bc8d1 10488 set_longjmp_breakpoint (tp, caller_frame_id);
5419bdae 10489 lj_deleter.emplace (thread);
c906108c 10490 }
c5aa993b 10491
c70a6932
JK
10492 /* set_momentary_breakpoint could invalidate FRAME. */
10493 frame = NULL;
10494
df631783
PA
10495 /* If the user told us to continue until a specified location, we
10496 don't specify a frame at which we need to stop. Otherwise,
10497 specify the selected frame, because we want to stop only at the
10498 very same frame. */
10499 frame_id stop_frame_id = anywhere ? null_frame_id : stack_frame_id;
10500
10501 for (symtab_and_line &sal : sals)
10502 {
10503 resolve_sal_pc (&sal);
10504
10505 breakpoint_up location_breakpoint
10506 = set_momentary_breakpoint (frame_gdbarch, sal,
10507 stop_frame_id, bp_until);
10508 breakpoints.emplace_back (std::move (location_breakpoint));
10509 }
883bc8d1 10510
573269a8
LS
10511 tp->set_thread_fsm
10512 (std::unique_ptr<thread_fsm>
10513 (new until_break_fsm (command_interp (), tp->global_num,
10514 std::move (breakpoints))));
f107f563 10515
5419bdae
TT
10516 if (lj_deleter)
10517 lj_deleter->release ();
f107f563 10518
cfc31633 10519 proceed (-1, GDB_SIGNAL_DEFAULT);
c906108c 10520}
ae66c1fc 10521
c906108c
SS
10522\f
10523
81b1e71c 10524/* Compare two breakpoints and return a strcmp-like result. */
8a2c437b
TT
10525
10526static int
81b1e71c 10527compare_breakpoints (const breakpoint *a, const breakpoint *b)
8a2c437b 10528{
81b1e71c
TT
10529 uintptr_t ua = (uintptr_t) a;
10530 uintptr_t ub = (uintptr_t) b;
8a2c437b 10531
81b1e71c 10532 if (a->number < b->number)
8a2c437b 10533 return -1;
81b1e71c 10534 else if (a->number > b->number)
8a2c437b
TT
10535 return 1;
10536
10537 /* Now sort by address, in case we see, e..g, two breakpoints with
10538 the number 0. */
10539 if (ua < ub)
10540 return -1;
94b0e70d 10541 return ua > ub ? 1 : 0;
8a2c437b
TT
10542}
10543
80f8a6eb 10544/* Delete breakpoints by address or line. */
c906108c
SS
10545
10546static void
0b39b52e 10547clear_command (const char *arg, int from_tty)
c906108c 10548{
c906108c 10549 int default_match;
c906108c 10550
6c5b2ebe
PA
10551 std::vector<symtab_and_line> decoded_sals;
10552 symtab_and_line last_sal;
10553 gdb::array_view<symtab_and_line> sals;
c906108c
SS
10554 if (arg)
10555 {
6c5b2ebe
PA
10556 decoded_sals
10557 = decode_line_with_current_source (arg,
10558 (DECODE_LINE_FUNFIRSTLINE
10559 | DECODE_LINE_LIST_MODE));
c906108c 10560 default_match = 0;
6c5b2ebe 10561 sals = decoded_sals;
c906108c
SS
10562 }
10563 else
10564 {
1bfeeb0f
JL
10565 /* Set sal's line, symtab, pc, and pspace to the values
10566 corresponding to the last call to print_frame_info. If the
10567 codepoint is not valid, this will set all the fields to 0. */
51abb421 10568 last_sal = get_last_displayed_sal ();
6c5b2ebe 10569 if (last_sal.symtab == 0)
8a3fe4f8 10570 error (_("No source file specified."));
c906108c 10571
c906108c 10572 default_match = 1;
6c5b2ebe 10573 sals = last_sal;
c906108c
SS
10574 }
10575
4a64f543
MS
10576 /* We don't call resolve_sal_pc here. That's not as bad as it
10577 seems, because all existing breakpoints typically have both
10578 file/line and pc set. So, if clear is given file/line, we can
10579 match this to existing breakpoint without obtaining pc at all.
ed0616c6
VP
10580
10581 We only support clearing given the address explicitly
10582 present in breakpoint table. Say, we've set breakpoint
4a64f543 10583 at file:line. There were several PC values for that file:line,
ed0616c6 10584 due to optimization, all in one block.
4a64f543
MS
10585
10586 We've picked one PC value. If "clear" is issued with another
ed0616c6
VP
10587 PC corresponding to the same file:line, the breakpoint won't
10588 be cleared. We probably can still clear the breakpoint, but
10589 since the other PC value is never presented to user, user
10590 can only find it by guessing, and it does not seem important
10591 to support that. */
10592
4a64f543
MS
10593 /* For each line spec given, delete bps which correspond to it. Do
10594 it in two passes, solely to preserve the current behavior that
10595 from_tty is forced true if we delete more than one
10596 breakpoint. */
c906108c 10597
81b1e71c 10598 std::vector<struct breakpoint *> found;
6c5b2ebe 10599 for (const auto &sal : sals)
c906108c 10600 {
05cba821
JK
10601 const char *sal_fullname;
10602
c906108c 10603 /* If exact pc given, clear bpts at that pc.
dda83cd7
SM
10604 If line given (pc == 0), clear all bpts on specified line.
10605 If defaulting, clear all bpts on default line
10606 or at default pc.
c5aa993b 10607
dda83cd7 10608 defaulting sal.pc != 0 tests to do
c5aa993b 10609
dda83cd7
SM
10610 0 1 pc
10611 1 1 pc _and_ line
10612 0 0 line
10613 1 0 <can't happen> */
c906108c 10614
05cba821
JK
10615 sal_fullname = (sal.symtab == NULL
10616 ? NULL : symtab_to_fullname (sal.symtab));
c906108c 10617
4a64f543 10618 /* Find all matching breakpoints and add them to 'found'. */
43892fdf 10619 for (breakpoint *b : all_breakpoints ())
c5aa993b 10620 {
0d381245 10621 int match = 0;
4a64f543 10622 /* Are we going to delete b? */
a5c69b1e
EL
10623 if (b->type != bp_none && !is_watchpoint (b)
10624 && user_breakpoint_p (b))
0d381245 10625 {
40cb8ca5 10626 for (bp_location *loc : b->locations ())
0d381245 10627 {
f8eba3c6
TT
10628 /* If the user specified file:line, don't allow a PC
10629 match. This matches historical gdb behavior. */
10630 int pc_match = (!sal.explicit_line
10631 && sal.pc
10632 && (loc->pspace == sal.pspace)
10633 && (loc->address == sal.pc)
10634 && (!section_is_overlay (loc->section)
10635 || loc->section == sal.section));
4aac40c8
TT
10636 int line_match = 0;
10637
10638 if ((default_match || sal.explicit_line)
2f202fde 10639 && loc->symtab != NULL
05cba821 10640 && sal_fullname != NULL
4aac40c8 10641 && sal.pspace == loc->pspace
05cba821
JK
10642 && loc->line_number == sal.line
10643 && filename_cmp (symtab_to_fullname (loc->symtab),
10644 sal_fullname) == 0)
10645 line_match = 1;
4aac40c8 10646
0d381245
VP
10647 if (pc_match || line_match)
10648 {
10649 match = 1;
10650 break;
10651 }
10652 }
10653 }
10654
10655 if (match)
81b1e71c 10656 found.push_back (b);
c906108c 10657 }
80f8a6eb 10658 }
8a2c437b 10659
80f8a6eb 10660 /* Now go thru the 'found' chain and delete them. */
81b1e71c 10661 if (found.empty ())
80f8a6eb
MS
10662 {
10663 if (arg)
8a3fe4f8 10664 error (_("No breakpoint at %s."), arg);
80f8a6eb 10665 else
8a3fe4f8 10666 error (_("No breakpoint at this line."));
80f8a6eb 10667 }
c906108c 10668
8a2c437b 10669 /* Remove duplicates from the vec. */
81b1e71c 10670 std::sort (found.begin (), found.end (),
b926417a 10671 [] (const breakpoint *bp_a, const breakpoint *bp_b)
81b1e71c 10672 {
b926417a 10673 return compare_breakpoints (bp_a, bp_b) < 0;
81b1e71c
TT
10674 });
10675 found.erase (std::unique (found.begin (), found.end (),
b926417a 10676 [] (const breakpoint *bp_a, const breakpoint *bp_b)
81b1e71c 10677 {
b926417a 10678 return compare_breakpoints (bp_a, bp_b) == 0;
81b1e71c
TT
10679 }),
10680 found.end ());
8a2c437b 10681
81b1e71c 10682 if (found.size () > 1)
4a64f543 10683 from_tty = 1; /* Always report if deleted more than one. */
80f8a6eb 10684 if (from_tty)
a3f17187 10685 {
81b1e71c 10686 if (found.size () == 1)
6cb06a8c 10687 gdb_printf (_("Deleted breakpoint "));
a3f17187 10688 else
6cb06a8c 10689 gdb_printf (_("Deleted breakpoints "));
a3f17187 10690 }
d6e956e5 10691
81b1e71c 10692 for (breakpoint *iter : found)
80f8a6eb 10693 {
c5aa993b 10694 if (from_tty)
6cb06a8c 10695 gdb_printf ("%d ", iter->number);
81b1e71c 10696 delete_breakpoint (iter);
c906108c 10697 }
80f8a6eb 10698 if (from_tty)
a11ac3b3 10699 gdb_putc ('\n');
c906108c
SS
10700}
10701\f
10702/* Delete breakpoint in BS if they are `delete' breakpoints and
10703 all breakpoints that are marked for deletion, whether hit or not.
10704 This is called after any breakpoint is hit, or after errors. */
10705
10706void
313f3b21 10707breakpoint_auto_delete (bpstat *bs)
c906108c 10708{
c906108c 10709 for (; bs; bs = bs->next)
f431efe5
PA
10710 if (bs->breakpoint_at
10711 && bs->breakpoint_at->disposition == disp_del
c906108c 10712 && bs->stop)
f431efe5 10713 delete_breakpoint (bs->breakpoint_at);
c906108c 10714
1428b37a 10715 for (breakpoint *b : all_breakpoints_safe ())
b5de0fa7 10716 if (b->disposition == disp_del_at_next_stop)
c5aa993b 10717 delete_breakpoint (b);
c906108c
SS
10718}
10719
4a64f543 10720/* A comparison function for bp_location AP and BP being interfaced to
39ef2f62 10721 std::sort. Sort elements primarily by their ADDRESS (no matter what
cb1e4e32
PA
10722 bl_address_is_meaningful says), secondarily by ordering first
10723 permanent elements and terciarily just ensuring the array is sorted
39ef2f62 10724 stable way despite std::sort being an unstable algorithm. */
876fa593
JK
10725
10726static int
39ef2f62 10727bp_location_is_less_than (const bp_location *a, const bp_location *b)
876fa593 10728{
876fa593 10729 if (a->address != b->address)
39ef2f62 10730 return a->address < b->address;
876fa593 10731
dea2aa5f
LM
10732 /* Sort locations at the same address by their pspace number, keeping
10733 locations of the same inferior (in a multi-inferior environment)
10734 grouped. */
10735
10736 if (a->pspace->num != b->pspace->num)
39ef2f62 10737 return a->pspace->num < b->pspace->num;
dea2aa5f 10738
876fa593 10739 /* Sort permanent breakpoints first. */
1a853c52 10740 if (a->permanent != b->permanent)
39ef2f62 10741 return a->permanent > b->permanent;
876fa593 10742
7f32a4d5
PA
10743 /* Sort by type in order to make duplicate determination easier.
10744 See update_global_location_list. This is kept in sync with
10745 breakpoint_locations_match. */
10746 if (a->loc_type < b->loc_type)
10747 return true;
10748
10749 /* Likewise, for range-breakpoints, sort by length. */
10750 if (a->loc_type == bp_loc_hardware_breakpoint
10751 && b->loc_type == bp_loc_hardware_breakpoint
10752 && a->length < b->length)
10753 return true;
10754
c56a97f9
JK
10755 /* Make the internal GDB representation stable across GDB runs
10756 where A and B memory inside GDB can differ. Breakpoint locations of
10757 the same type at the same address can be sorted in arbitrary order. */
876fa593
JK
10758
10759 if (a->owner->number != b->owner->number)
39ef2f62 10760 return a->owner->number < b->owner->number;
876fa593 10761
39ef2f62 10762 return a < b;
876fa593
JK
10763}
10764
f5336ca5
PA
10765/* Set bp_locations_placed_address_before_address_max and
10766 bp_locations_shadow_len_after_address_max according to the current
10767 content of the bp_locations array. */
f7545552
TT
10768
10769static void
f5336ca5 10770bp_locations_target_extensions_update (void)
f7545552 10771{
f5336ca5
PA
10772 bp_locations_placed_address_before_address_max = 0;
10773 bp_locations_shadow_len_after_address_max = 0;
876fa593 10774
48d7020b 10775 for (bp_location *bl : all_bp_locations ())
876fa593
JK
10776 {
10777 CORE_ADDR start, end, addr;
10778
10779 if (!bp_location_has_shadow (bl))
10780 continue;
10781
10782 start = bl->target_info.placed_address;
10783 end = start + bl->target_info.shadow_len;
10784
10785 gdb_assert (bl->address >= start);
10786 addr = bl->address - start;
f5336ca5
PA
10787 if (addr > bp_locations_placed_address_before_address_max)
10788 bp_locations_placed_address_before_address_max = addr;
876fa593
JK
10789
10790 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
10791
10792 gdb_assert (bl->address < end);
10793 addr = end - bl->address;
f5336ca5
PA
10794 if (addr > bp_locations_shadow_len_after_address_max)
10795 bp_locations_shadow_len_after_address_max = addr;
876fa593 10796 }
f7545552
TT
10797}
10798
1e4d1764
YQ
10799/* Download tracepoint locations if they haven't been. */
10800
10801static void
10802download_tracepoint_locations (void)
10803{
dd2e65cc 10804 enum tribool can_download_tracepoint = TRIBOOL_UNKNOWN;
1e4d1764 10805
5ed8105e 10806 scoped_restore_current_pspace_and_thread restore_pspace_thread;
1e4d1764 10807
f6d17b2b 10808 for (breakpoint *b : all_tracepoints ())
1e4d1764
YQ
10809 {
10810 struct tracepoint *t;
f2a8bc8a 10811 int bp_location_downloaded = 0;
1e4d1764 10812
7ed2c994 10813 if ((b->type == bp_fast_tracepoint
1e4d1764
YQ
10814 ? !may_insert_fast_tracepoints
10815 : !may_insert_tracepoints))
10816 continue;
10817
dd2e65cc
YQ
10818 if (can_download_tracepoint == TRIBOOL_UNKNOWN)
10819 {
10820 if (target_can_download_tracepoint ())
10821 can_download_tracepoint = TRIBOOL_TRUE;
10822 else
10823 can_download_tracepoint = TRIBOOL_FALSE;
10824 }
10825
10826 if (can_download_tracepoint == TRIBOOL_FALSE)
10827 break;
10828
40cb8ca5 10829 for (bp_location *bl : b->locations ())
7ed2c994
YQ
10830 {
10831 /* In tracepoint, locations are _never_ duplicated, so
10832 should_be_inserted is equivalent to
10833 unduplicated_should_be_inserted. */
10834 if (!should_be_inserted (bl) || bl->inserted)
10835 continue;
1e4d1764 10836
7ed2c994 10837 switch_to_program_space_and_thread (bl->pspace);
1e4d1764 10838
7ed2c994 10839 target_download_tracepoint (bl);
1e4d1764 10840
7ed2c994 10841 bl->inserted = 1;
f2a8bc8a 10842 bp_location_downloaded = 1;
7ed2c994
YQ
10843 }
10844 t = (struct tracepoint *) b;
10845 t->number_on_target = b->number;
f2a8bc8a 10846 if (bp_location_downloaded)
76727919 10847 gdb::observers::breakpoint_modified.notify (b);
1e4d1764 10848 }
1e4d1764
YQ
10849}
10850
934709f0
PW
10851/* Swap the insertion/duplication state between two locations. */
10852
10853static void
10854swap_insertion (struct bp_location *left, struct bp_location *right)
10855{
10856 const int left_inserted = left->inserted;
10857 const int left_duplicate = left->duplicate;
b775012e 10858 const int left_needs_update = left->needs_update;
934709f0
PW
10859 const struct bp_target_info left_target_info = left->target_info;
10860
1e4d1764
YQ
10861 /* Locations of tracepoints can never be duplicated. */
10862 if (is_tracepoint (left->owner))
10863 gdb_assert (!left->duplicate);
10864 if (is_tracepoint (right->owner))
10865 gdb_assert (!right->duplicate);
10866
934709f0
PW
10867 left->inserted = right->inserted;
10868 left->duplicate = right->duplicate;
b775012e 10869 left->needs_update = right->needs_update;
934709f0
PW
10870 left->target_info = right->target_info;
10871 right->inserted = left_inserted;
10872 right->duplicate = left_duplicate;
b775012e 10873 right->needs_update = left_needs_update;
934709f0
PW
10874 right->target_info = left_target_info;
10875}
10876
b775012e
LM
10877/* Force the re-insertion of the locations at ADDRESS. This is called
10878 once a new/deleted/modified duplicate location is found and we are evaluating
10879 conditions on the target's side. Such conditions need to be updated on
10880 the target. */
10881
10882static void
10883force_breakpoint_reinsertion (struct bp_location *bl)
10884{
b775012e
LM
10885 CORE_ADDR address = 0;
10886 int pspace_num;
10887
10888 address = bl->address;
10889 pspace_num = bl->pspace->num;
10890
10891 /* This is only meaningful if the target is
10892 evaluating conditions and if the user has
10893 opted for condition evaluation on the target's
10894 side. */
10895 if (gdb_evaluates_breakpoint_condition_p ()
10896 || !target_supports_evaluation_of_breakpoint_conditions ())
10897 return;
10898
10899 /* Flag all breakpoint locations with this address and
10900 the same program space as the location
10901 as "its condition has changed". We need to
10902 update the conditions on the target's side. */
e0d9a270 10903 for (bp_location *loc : all_bp_locations_at_addr (address))
b775012e 10904 {
b775012e
LM
10905 if (!is_breakpoint (loc->owner)
10906 || pspace_num != loc->pspace->num)
10907 continue;
10908
10909 /* Flag the location appropriately. We use a different state to
10910 let everyone know that we already updated the set of locations
10911 with addr bl->address and program space bl->pspace. This is so
10912 we don't have to keep calling these functions just to mark locations
10913 that have already been marked. */
10914 loc->condition_changed = condition_updated;
10915
10916 /* Free the agent expression bytecode as well. We will compute
10917 it later on. */
833177a4 10918 loc->cond_bytecode.reset ();
b775012e
LM
10919 }
10920}
7f32a4d5 10921
44702360
PA
10922/* Called whether new breakpoints are created, or existing breakpoints
10923 deleted, to update the global location list and recompute which
10924 locations are duplicate of which.
b775012e 10925
04086b45
PA
10926 The INSERT_MODE flag determines whether locations may not, may, or
10927 shall be inserted now. See 'enum ugll_insert_mode' for more
10928 info. */
b60e7edf 10929
0d381245 10930static void
44702360 10931update_global_location_list (enum ugll_insert_mode insert_mode)
0d381245 10932{
b775012e
LM
10933 /* Last breakpoint location address that was marked for update. */
10934 CORE_ADDR last_addr = 0;
10935 /* Last breakpoint location program space that was marked for update. */
10936 int last_pspace_num = -1;
f7545552 10937
2d134ed3
PA
10938 /* Used in the duplicates detection below. When iterating over all
10939 bp_locations, points to the first bp_location of a given address.
10940 Breakpoints and watchpoints of different types are never
10941 duplicates of each other. Keep one pointer for each type of
10942 breakpoint/watchpoint, so we only need to loop over all locations
10943 once. */
10944 struct bp_location *bp_loc_first; /* breakpoint */
10945 struct bp_location *wp_loc_first; /* hardware watchpoint */
10946 struct bp_location *awp_loc_first; /* access watchpoint */
10947 struct bp_location *rwp_loc_first; /* read watchpoint */
876fa593 10948
f5336ca5
PA
10949 /* Saved former bp_locations array which we compare against the newly
10950 built bp_locations from the current state of ALL_BREAKPOINTS. */
5d51cd5d
SM
10951 std::vector<bp_location *> old_locations = std::move (bp_locations);
10952 bp_locations.clear ();
876fa593 10953
43892fdf 10954 for (breakpoint *b : all_breakpoints ())
40cb8ca5 10955 for (bp_location *loc : b->locations ())
5d51cd5d 10956 bp_locations.push_back (loc);
7f32a4d5
PA
10957
10958 /* See if we need to "upgrade" a software breakpoint to a hardware
10959 breakpoint. Do this before deciding whether locations are
10960 duplicates. Also do this before sorting because sorting order
10961 depends on location type. */
5d51cd5d
SM
10962 for (bp_location *loc : bp_locations)
10963 if (!loc->inserted && should_be_inserted (loc))
7f32a4d5 10964 handle_automatic_hardware_breakpoints (loc);
7f32a4d5 10965
5d51cd5d 10966 std::sort (bp_locations.begin (), bp_locations.end (),
39ef2f62 10967 bp_location_is_less_than);
876fa593 10968
f5336ca5 10969 bp_locations_target_extensions_update ();
74960c60 10970
4a64f543
MS
10971 /* Identify bp_location instances that are no longer present in the
10972 new list, and therefore should be freed. Note that it's not
10973 necessary that those locations should be removed from inferior --
10974 if there's another location at the same address (previously
10975 marked as duplicate), we don't need to remove/insert the
10976 location.
876fa593 10977
4a64f543
MS
10978 LOCP is kept in sync with OLD_LOCP, each pointing to the current
10979 and former bp_location array state respectively. */
876fa593 10980
5d51cd5d
SM
10981 size_t loc_i = 0;
10982 for (bp_location *old_loc : old_locations)
74960c60 10983 {
e5dd4106 10984 /* Tells if 'old_loc' is found among the new locations. If
4a64f543 10985 not, we have to free it. */
c7d46a38 10986 int found_object = 0;
20874c92
VP
10987 /* Tells if the location should remain inserted in the target. */
10988 int keep_in_target = 0;
10989 int removed = 0;
876fa593 10990
4a64f543
MS
10991 /* Skip LOCP entries which will definitely never be needed.
10992 Stop either at or being the one matching OLD_LOC. */
5d51cd5d
SM
10993 while (loc_i < bp_locations.size ()
10994 && bp_locations[loc_i]->address < old_loc->address)
10995 loc_i++;
c7d46a38 10996
5d51cd5d
SM
10997 for (size_t loc2_i = loc_i;
10998 (loc2_i < bp_locations.size ()
10999 && bp_locations[loc2_i]->address == old_loc->address);
11000 loc2_i++)
c7d46a38 11001 {
b775012e
LM
11002 /* Check if this is a new/duplicated location or a duplicated
11003 location that had its condition modified. If so, we want to send
11004 its condition to the target if evaluation of conditions is taking
11005 place there. */
5d51cd5d 11006 if (bp_locations[loc2_i]->condition_changed == condition_modified
b775012e
LM
11007 && (last_addr != old_loc->address
11008 || last_pspace_num != old_loc->pspace->num))
c7d46a38 11009 {
5d51cd5d 11010 force_breakpoint_reinsertion (bp_locations[loc2_i]);
b775012e 11011 last_pspace_num = old_loc->pspace->num;
c7d46a38 11012 }
b775012e 11013
5d51cd5d 11014 if (bp_locations[loc2_i] == old_loc)
b775012e 11015 found_object = 1;
c7d46a38 11016 }
74960c60 11017
b775012e
LM
11018 /* We have already handled this address, update it so that we don't
11019 have to go through updates again. */
11020 last_addr = old_loc->address;
11021
11022 /* Target-side condition evaluation: Handle deleted locations. */
11023 if (!found_object)
11024 force_breakpoint_reinsertion (old_loc);
11025
4a64f543
MS
11026 /* If this location is no longer present, and inserted, look if
11027 there's maybe a new location at the same address. If so,
11028 mark that one inserted, and don't remove this one. This is
11029 needed so that we don't have a time window where a breakpoint
11030 at certain location is not inserted. */
74960c60 11031
876fa593 11032 if (old_loc->inserted)
0d381245 11033 {
4a64f543
MS
11034 /* If the location is inserted now, we might have to remove
11035 it. */
74960c60 11036
876fa593 11037 if (found_object && should_be_inserted (old_loc))
74960c60 11038 {
4a64f543
MS
11039 /* The location is still present in the location list,
11040 and still should be inserted. Don't do anything. */
20874c92 11041 keep_in_target = 1;
74960c60
VP
11042 }
11043 else
11044 {
b775012e
LM
11045 /* This location still exists, but it won't be kept in the
11046 target since it may have been disabled. We proceed to
11047 remove its target-side condition. */
11048
4a64f543
MS
11049 /* The location is either no longer present, or got
11050 disabled. See if there's another location at the
11051 same address, in which case we don't need to remove
11052 this one from the target. */
876fa593 11053
2bdf28a0 11054 /* OLD_LOC comes from existing struct breakpoint. */
cb1e4e32 11055 if (bl_address_is_meaningful (old_loc))
876fa593 11056 {
5d51cd5d
SM
11057 for (size_t loc2_i = loc_i;
11058 (loc2_i < bp_locations.size ()
11059 && bp_locations[loc2_i]->address == old_loc->address);
11060 loc2_i++)
876fa593 11061 {
5d51cd5d 11062 bp_location *loc2 = bp_locations[loc2_i];
876fa593 11063
7f32a4d5
PA
11064 if (loc2 == old_loc)
11065 continue;
11066
2d134ed3 11067 if (breakpoint_locations_match (loc2, old_loc))
c7d46a38 11068 {
85d721b8
PA
11069 /* Read watchpoint locations are switched to
11070 access watchpoints, if the former are not
11071 supported, but the latter are. */
11072 if (is_hardware_watchpoint (old_loc->owner))
11073 {
11074 gdb_assert (is_hardware_watchpoint (loc2->owner));
11075 loc2->watchpoint_type = old_loc->watchpoint_type;
11076 }
11077
934709f0
PW
11078 /* loc2 is a duplicated location. We need to check
11079 if it should be inserted in case it will be
11080 unduplicated. */
7f32a4d5 11081 if (unduplicated_should_be_inserted (loc2))
c7d46a38 11082 {
934709f0 11083 swap_insertion (old_loc, loc2);
c7d46a38
PA
11084 keep_in_target = 1;
11085 break;
11086 }
876fa593
JK
11087 }
11088 }
11089 }
74960c60
VP
11090 }
11091
20874c92
VP
11092 if (!keep_in_target)
11093 {
834c0d03 11094 if (remove_breakpoint (old_loc))
20874c92 11095 {
4a64f543
MS
11096 /* This is just about all we can do. We could keep
11097 this location on the global list, and try to
11098 remove it next time, but there's no particular
11099 reason why we will succeed next time.
20874c92 11100
4a64f543
MS
11101 Note that at this point, old_loc->owner is still
11102 valid, as delete_breakpoint frees the breakpoint
11103 only after calling us. */
6cb06a8c
TT
11104 gdb_printf (_("warning: Error removing "
11105 "breakpoint %d\n"),
11106 old_loc->owner->number);
20874c92
VP
11107 }
11108 removed = 1;
11109 }
0d381245 11110 }
74960c60
VP
11111
11112 if (!found_object)
1c5cfe86 11113 {
fbea99ea 11114 if (removed && target_is_non_stop_p ()
1cf4d951 11115 && need_moribund_for_location_type (old_loc))
20874c92 11116 {
db82e815
PA
11117 /* This location was removed from the target. In
11118 non-stop mode, a race condition is possible where
11119 we've removed a breakpoint, but stop events for that
11120 breakpoint are already queued and will arrive later.
11121 We apply an heuristic to be able to distinguish such
11122 SIGTRAPs from other random SIGTRAPs: we keep this
11123 breakpoint location for a bit, and will retire it
11124 after we see some number of events. The theory here
11125 is that reporting of events should, "on the average",
11126 be fair, so after a while we'll see events from all
11127 threads that have anything of interest, and no longer
11128 need to keep this breakpoint location around. We
11129 don't hold locations forever so to reduce chances of
11130 mistaking a non-breakpoint SIGTRAP for a breakpoint
11131 SIGTRAP.
11132
11133 The heuristic failing can be disastrous on
11134 decr_pc_after_break targets.
11135
11136 On decr_pc_after_break targets, like e.g., x86-linux,
11137 if we fail to recognize a late breakpoint SIGTRAP,
11138 because events_till_retirement has reached 0 too
11139 soon, we'll fail to do the PC adjustment, and report
11140 a random SIGTRAP to the user. When the user resumes
11141 the inferior, it will most likely immediately crash
2dec564e 11142 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
db82e815
PA
11143 corrupted, because of being resumed e.g., in the
11144 middle of a multi-byte instruction, or skipped a
11145 one-byte instruction. This was actually seen happen
11146 on native x86-linux, and should be less rare on
11147 targets that do not support new thread events, like
11148 remote, due to the heuristic depending on
11149 thread_count.
11150
11151 Mistaking a random SIGTRAP for a breakpoint trap
11152 causes similar symptoms (PC adjustment applied when
11153 it shouldn't), but then again, playing with SIGTRAPs
11154 behind the debugger's back is asking for trouble.
11155
11156 Since hardware watchpoint traps are always
11157 distinguishable from other traps, so we don't need to
11158 apply keep hardware watchpoint moribund locations
11159 around. We simply always ignore hardware watchpoint
11160 traps we can no longer explain. */
11161
5b6d1e4f
PA
11162 process_stratum_target *proc_target = nullptr;
11163 for (inferior *inf : all_inferiors ())
11164 if (inf->pspace == old_loc->pspace)
11165 {
11166 proc_target = inf->process_target ();
11167 break;
11168 }
11169 if (proc_target != nullptr)
11170 old_loc->events_till_retirement
11171 = 3 * (thread_count (proc_target) + 1);
11172 else
11173 old_loc->events_till_retirement = 1;
876fa593 11174 old_loc->owner = NULL;
20874c92 11175
1123588c 11176 moribund_locations.push_back (old_loc);
1c5cfe86
PA
11177 }
11178 else
f431efe5
PA
11179 {
11180 old_loc->owner = NULL;
11181 decref_bp_location (&old_loc);
11182 }
20874c92 11183 }
74960c60 11184 }
1c5cfe86 11185
348d480f
PA
11186 /* Rescan breakpoints at the same address and section, marking the
11187 first one as "first" and any others as "duplicates". This is so
11188 that the bpt instruction is only inserted once. If we have a
11189 permanent breakpoint at the same place as BPT, make that one the
11190 official one, and the rest as duplicates. Permanent breakpoints
11191 are sorted first for the same address.
11192
11193 Do the same for hardware watchpoints, but also considering the
11194 watchpoint's type (regular/access/read) and length. */
11195
11196 bp_loc_first = NULL;
11197 wp_loc_first = NULL;
11198 awp_loc_first = NULL;
11199 rwp_loc_first = NULL;
40cb8ca5 11200
48d7020b 11201 for (bp_location *loc : all_bp_locations ())
348d480f
PA
11202 {
11203 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
11204 non-NULL. */
348d480f 11205 struct bp_location **loc_first_p;
43892fdf 11206 breakpoint *b = loc->owner;
348d480f 11207
6f380991 11208 if (!unduplicated_should_be_inserted (loc)
cb1e4e32 11209 || !bl_address_is_meaningful (loc)
1e4d1764
YQ
11210 /* Don't detect duplicate for tracepoint locations because they are
11211 never duplicated. See the comments in field `duplicate' of
11212 `struct bp_location'. */
348d480f 11213 || is_tracepoint (b))
b775012e
LM
11214 {
11215 /* Clear the condition modification flag. */
11216 loc->condition_changed = condition_unchanged;
11217 continue;
11218 }
348d480f 11219
348d480f
PA
11220 if (b->type == bp_hardware_watchpoint)
11221 loc_first_p = &wp_loc_first;
11222 else if (b->type == bp_read_watchpoint)
11223 loc_first_p = &rwp_loc_first;
11224 else if (b->type == bp_access_watchpoint)
11225 loc_first_p = &awp_loc_first;
11226 else
11227 loc_first_p = &bp_loc_first;
11228
11229 if (*loc_first_p == NULL
11230 || (overlay_debugging && loc->section != (*loc_first_p)->section)
11231 || !breakpoint_locations_match (loc, *loc_first_p))
11232 {
11233 *loc_first_p = loc;
11234 loc->duplicate = 0;
b775012e
LM
11235
11236 if (is_breakpoint (loc->owner) && loc->condition_changed)
11237 {
11238 loc->needs_update = 1;
11239 /* Clear the condition modification flag. */
11240 loc->condition_changed = condition_unchanged;
11241 }
348d480f
PA
11242 continue;
11243 }
11244
934709f0
PW
11245
11246 /* This and the above ensure the invariant that the first location
11247 is not duplicated, and is the inserted one.
11248 All following are marked as duplicated, and are not inserted. */
11249 if (loc->inserted)
11250 swap_insertion (loc, *loc_first_p);
348d480f
PA
11251 loc->duplicate = 1;
11252
b775012e
LM
11253 /* Clear the condition modification flag. */
11254 loc->condition_changed = condition_unchanged;
348d480f
PA
11255 }
11256
a25a5a45 11257 if (insert_mode == UGLL_INSERT || breakpoints_should_be_inserted_now ())
b775012e 11258 {
04086b45 11259 if (insert_mode != UGLL_DONT_INSERT)
b775012e
LM
11260 insert_breakpoint_locations ();
11261 else
11262 {
44702360
PA
11263 /* Even though the caller told us to not insert new
11264 locations, we may still need to update conditions on the
11265 target's side of breakpoints that were already inserted
11266 if the target is evaluating breakpoint conditions. We
b775012e
LM
11267 only update conditions for locations that are marked
11268 "needs_update". */
11269 update_inserted_breakpoint_locations ();
11270 }
11271 }
348d480f 11272
04086b45 11273 if (insert_mode != UGLL_DONT_INSERT)
1e4d1764 11274 download_tracepoint_locations ();
348d480f
PA
11275}
11276
11277void
11278breakpoint_retire_moribund (void)
11279{
1123588c
TT
11280 for (int ix = 0; ix < moribund_locations.size (); ++ix)
11281 {
11282 struct bp_location *loc = moribund_locations[ix];
11283 if (--(loc->events_till_retirement) == 0)
11284 {
11285 decref_bp_location (&loc);
11286 unordered_remove (moribund_locations, ix);
11287 --ix;
11288 }
11289 }
348d480f
PA
11290}
11291
11292static void
44702360 11293update_global_location_list_nothrow (enum ugll_insert_mode insert_mode)
348d480f 11294{
348d480f 11295
a70b8144 11296 try
492d29ea
PA
11297 {
11298 update_global_location_list (insert_mode);
11299 }
230d2906 11300 catch (const gdb_exception_error &e)
492d29ea
PA
11301 {
11302 }
348d480f
PA
11303}
11304
11305/* Clear BKP from a BPS. */
11306
11307static void
313f3b21 11308bpstat_remove_bp_location (bpstat *bps, struct breakpoint *bpt)
348d480f 11309{
313f3b21 11310 bpstat *bs;
348d480f
PA
11311
11312 for (bs = bps; bs; bs = bs->next)
11313 if (bs->breakpoint_at == bpt)
11314 {
11315 bs->breakpoint_at = NULL;
11316 bs->old_val = NULL;
11317 /* bs->commands will be freed later. */
11318 }
11319}
11320
11321/* Callback for iterate_over_threads. */
11322static int
11323bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
11324{
9a3c8263 11325 struct breakpoint *bpt = (struct breakpoint *) data;
348d480f
PA
11326
11327 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
11328 return 0;
11329}
11330
c47614fe 11331/* Helper for breakpoint and tracepoint breakpoint->mention
348d480f
PA
11332 callbacks. */
11333
11334static void
b713485d 11335say_where (const breakpoint *b)
348d480f
PA
11336{
11337 struct value_print_options opts;
11338
11339 get_user_print_options (&opts);
11340
11341 /* i18n: cagney/2005-02-11: Below needs to be merged into a
11342 single string. */
11343 if (b->loc == NULL)
11344 {
f00aae0f
KS
11345 /* For pending locations, the output differs slightly based
11346 on b->extra_string. If this is non-NULL, it contains either
11347 a condition or dprintf arguments. */
11348 if (b->extra_string == NULL)
11349 {
6cb06a8c
TT
11350 gdb_printf (_(" (%s) pending."),
11351 event_location_to_string (b->location.get ()));
f00aae0f
KS
11352 }
11353 else if (b->type == bp_dprintf)
11354 {
6cb06a8c
TT
11355 gdb_printf (_(" (%s,%s) pending."),
11356 event_location_to_string (b->location.get ()),
11357 b->extra_string.get ());
f00aae0f
KS
11358 }
11359 else
11360 {
6cb06a8c
TT
11361 gdb_printf (_(" (%s %s) pending."),
11362 event_location_to_string (b->location.get ()),
11363 b->extra_string.get ());
f00aae0f 11364 }
348d480f
PA
11365 }
11366 else
11367 {
2f202fde 11368 if (opts.addressprint || b->loc->symtab == NULL)
6cb06a8c
TT
11369 gdb_printf (" at %ps",
11370 styled_string (address_style.style (),
11371 paddress (b->loc->gdbarch,
11372 b->loc->address)));
2f202fde 11373 if (b->loc->symtab != NULL)
f8eba3c6
TT
11374 {
11375 /* If there is a single location, we can print the location
11376 more nicely. */
11377 if (b->loc->next == NULL)
0bb296cb 11378 {
6a831f06
PA
11379 const char *filename
11380 = symtab_to_filename_for_display (b->loc->symtab);
6cb06a8c
TT
11381 gdb_printf (": file %ps, line %d.",
11382 styled_string (file_name_style.style (),
11383 filename),
11384 b->loc->line_number);
0bb296cb 11385 }
f8eba3c6
TT
11386 else
11387 /* This is not ideal, but each location may have a
11388 different file name, and this at least reflects the
11389 real situation somewhat. */
6cb06a8c
TT
11390 gdb_printf (": %s.",
11391 event_location_to_string (b->location.get ()));
f8eba3c6 11392 }
348d480f
PA
11393
11394 if (b->loc->next)
11395 {
11396 struct bp_location *loc = b->loc;
11397 int n = 0;
11398 for (; loc; loc = loc->next)
11399 ++n;
6cb06a8c 11400 gdb_printf (" (%d locations)", n);
348d480f
PA
11401 }
11402 }
11403}
11404
40cb8ca5
SM
11405/* See breakpoint.h. */
11406
a67bcaba 11407bp_location_range breakpoint::locations () const
40cb8ca5 11408{
9be25986 11409 return bp_location_range (this->loc);
40cb8ca5
SM
11410}
11411
4c6a92b1
TT
11412struct bp_location *
11413breakpoint::allocate_location ()
11414{
11415 return new bp_location (this);
11416}
11417
2060206e
PA
11418#define internal_error_pure_virtual_called() \
11419 gdb_assert_not_reached ("pure virtual function called")
11420
4c6a92b1
TT
11421int
11422breakpoint::insert_location (struct bp_location *bl)
11423{
11424 internal_error_pure_virtual_called ();
11425}
11426
4c6a92b1
TT
11427int
11428breakpoint::remove_location (struct bp_location *bl,
11429 enum remove_bp_reason reason)
11430{
11431 internal_error_pure_virtual_called ();
11432}
11433
4c6a92b1
TT
11434int
11435breakpoint::breakpoint_hit (const struct bp_location *bl,
11436 const address_space *aspace,
11437 CORE_ADDR bp_addr,
11438 const target_waitstatus &ws)
11439{
11440 internal_error_pure_virtual_called ();
11441}
11442
4c6a92b1
TT
11443int
11444breakpoint::resources_needed (const struct bp_location *bl)
11445{
11446 internal_error_pure_virtual_called ();
11447}
11448
4c6a92b1 11449enum print_stop_action
7bd86313 11450breakpoint::print_it (const bpstat *bs) const
4c6a92b1
TT
11451{
11452 internal_error_pure_virtual_called ();
11453}
11454
4c6a92b1 11455void
b713485d 11456breakpoint::print_mention () const
4c6a92b1
TT
11457{
11458 internal_error_pure_virtual_called ();
11459}
11460
4c6a92b1 11461void
4d1ae558 11462breakpoint::print_recreate (struct ui_file *fp) const
4c6a92b1
TT
11463{
11464 internal_error_pure_virtual_called ();
11465}
11466
4c6a92b1
TT
11467std::vector<symtab_and_line>
11468breakpoint::decode_location (struct event_location *location,
11469 struct program_space *search_pspace)
11470{
11471 internal_error_pure_virtual_called ();
11472}
11473
2060206e
PA
11474/* Default breakpoint_ops methods. */
11475
2b5ab5b8
TT
11476void
11477base_breakpoint::re_set ()
348d480f 11478{
06edf0c0 11479 /* FIXME: is this still reachable? */
2b5ab5b8 11480 if (breakpoint_event_location_empty_p (this))
06edf0c0 11481 {
f00aae0f 11482 /* Anything without a location can't be re-set. */
2b5ab5b8 11483 delete_breakpoint (this);
06edf0c0 11484 return;
348d480f 11485 }
06edf0c0 11486
2b5ab5b8 11487 breakpoint_re_set_default (this);
348d480f
PA
11488}
11489
2b5ab5b8
TT
11490int
11491base_breakpoint::insert_location (struct bp_location *bl)
348d480f 11492{
cd6c3b4f
YQ
11493 CORE_ADDR addr = bl->target_info.reqstd_address;
11494
579c6ad9 11495 bl->target_info.kind = breakpoint_kind (bl, &addr);
cd6c3b4f
YQ
11496 bl->target_info.placed_address = addr;
11497
5d926615 11498 int result;
348d480f 11499 if (bl->loc_type == bp_loc_hardware_breakpoint)
5d926615 11500 result = target_insert_hw_breakpoint (bl->gdbarch, &bl->target_info);
348d480f 11501 else
5d926615
TT
11502 result = target_insert_breakpoint (bl->gdbarch, &bl->target_info);
11503
11504 if (result == 0 && bl->probe.prob != nullptr)
11505 {
11506 /* The insertion was successful, now let's set the probe's semaphore
11507 if needed. */
11508 bl->probe.prob->set_semaphore (bl->probe.objfile, bl->gdbarch);
11509 }
11510
11511 return result;
348d480f
PA
11512}
11513
2b5ab5b8
TT
11514int
11515base_breakpoint::remove_location (struct bp_location *bl,
11516 enum remove_bp_reason reason)
348d480f 11517{
5d926615
TT
11518 if (bl->probe.prob != nullptr)
11519 {
11520 /* Let's clear the semaphore before removing the location. */
11521 bl->probe.prob->clear_semaphore (bl->probe.objfile, bl->gdbarch);
11522 }
11523
348d480f
PA
11524 if (bl->loc_type == bp_loc_hardware_breakpoint)
11525 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
11526 else
73971819 11527 return target_remove_breakpoint (bl->gdbarch, &bl->target_info, reason);
348d480f
PA
11528}
11529
2b5ab5b8
TT
11530int
11531base_breakpoint::breakpoint_hit (const struct bp_location *bl,
11532 const address_space *aspace,
11533 CORE_ADDR bp_addr,
11534 const target_waitstatus &ws)
348d480f 11535{
c272a98c
SM
11536 if (ws.kind () != TARGET_WAITKIND_STOPPED
11537 || ws.sig () != GDB_SIGNAL_TRAP)
09ac7c10
TT
11538 return 0;
11539
348d480f
PA
11540 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
11541 aspace, bp_addr))
11542 return 0;
11543
11544 if (overlay_debugging /* unmapped overlay section */
11545 && section_is_overlay (bl->section)
11546 && !section_is_mapped (bl->section))
11547 return 0;
11548
11549 return 1;
11550}
11551
f293a0b5
TT
11552int
11553dprintf_breakpoint::breakpoint_hit (const struct bp_location *bl,
11554 const address_space *aspace,
11555 CORE_ADDR bp_addr,
11556 const target_waitstatus &ws)
cd1608cc
PA
11557{
11558 if (dprintf_style == dprintf_style_agent
11559 && target_can_run_breakpoint_commands ())
11560 {
11561 /* An agent-style dprintf never causes a stop. If we see a trap
11562 for this address it must be for a breakpoint that happens to
11563 be set at the same address. */
11564 return 0;
11565 }
11566
f293a0b5 11567 return this->ordinary_breakpoint::breakpoint_hit (bl, aspace, bp_addr, ws);
cd1608cc
PA
11568}
11569
7dd8e7ae
TT
11570int
11571ordinary_breakpoint::resources_needed (const struct bp_location *bl)
348d480f 11572{
7dd8e7ae 11573 gdb_assert (type == bp_hardware_breakpoint);
348d480f
PA
11574
11575 return 1;
11576}
11577
7dd8e7ae 11578enum print_stop_action
7bd86313 11579ordinary_breakpoint::print_it (const bpstat *bs) const
348d480f 11580{
348d480f 11581 const struct bp_location *bl;
001c8c33 11582 int bp_temp;
79a45e25 11583 struct ui_out *uiout = current_uiout;
348d480f 11584
b6433ede 11585 bl = bs->bp_location_at.get ();
348d480f 11586
7dd8e7ae 11587 bp_temp = disposition == disp_del;
001c8c33
PA
11588 if (bl->address != bl->requested_address)
11589 breakpoint_adjustment_warning (bl->requested_address,
11590 bl->address,
7dd8e7ae
TT
11591 number, 1);
11592 annotate_breakpoint (number);
f303dbd6
PA
11593 maybe_print_thread_hit_breakpoint (uiout);
11594
112e8700 11595 if (uiout->is_mi_like_p ())
348d480f 11596 {
112e8700 11597 uiout->field_string ("reason",
001c8c33 11598 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
7dd8e7ae 11599 uiout->field_string ("disp", bpdisp_text (disposition));
06edf0c0 11600 }
6a831f06
PA
11601 if (bp_temp)
11602 uiout->message ("Temporary breakpoint %pF, ",
7dd8e7ae 11603 signed_field ("bkptno", number));
6a831f06
PA
11604 else
11605 uiout->message ("Breakpoint %pF, ",
7dd8e7ae 11606 signed_field ("bkptno", number));
06edf0c0 11607
001c8c33 11608 return PRINT_SRC_AND_LOC;
06edf0c0
PA
11609}
11610
7dd8e7ae 11611void
b713485d 11612ordinary_breakpoint::print_mention () const
06edf0c0 11613{
112e8700 11614 if (current_uiout->is_mi_like_p ())
06edf0c0
PA
11615 return;
11616
7dd8e7ae 11617 switch (type)
06edf0c0
PA
11618 {
11619 case bp_breakpoint:
11620 case bp_gnu_ifunc_resolver:
7dd8e7ae 11621 if (disposition == disp_del)
6cb06a8c 11622 gdb_printf (_("Temporary breakpoint"));
06edf0c0 11623 else
6cb06a8c 11624 gdb_printf (_("Breakpoint"));
7dd8e7ae
TT
11625 gdb_printf (_(" %d"), number);
11626 if (type == bp_gnu_ifunc_resolver)
6cb06a8c 11627 gdb_printf (_(" at gnu-indirect-function resolver"));
06edf0c0
PA
11628 break;
11629 case bp_hardware_breakpoint:
7dd8e7ae 11630 gdb_printf (_("Hardware assisted breakpoint %d"), number);
06edf0c0 11631 break;
e7e0cddf 11632 case bp_dprintf:
7dd8e7ae 11633 gdb_printf (_("Dprintf %d"), number);
e7e0cddf 11634 break;
06edf0c0
PA
11635 }
11636
7dd8e7ae 11637 say_where (this);
06edf0c0
PA
11638}
11639
7dd8e7ae 11640void
4d1ae558 11641ordinary_breakpoint::print_recreate (struct ui_file *fp) const
06edf0c0 11642{
7dd8e7ae 11643 if (type == bp_breakpoint && disposition == disp_del)
6cb06a8c 11644 gdb_printf (fp, "tbreak");
7dd8e7ae 11645 else if (type == bp_breakpoint)
6cb06a8c 11646 gdb_printf (fp, "break");
7dd8e7ae
TT
11647 else if (type == bp_hardware_breakpoint
11648 && disposition == disp_del)
6cb06a8c 11649 gdb_printf (fp, "thbreak");
7dd8e7ae 11650 else if (type == bp_hardware_breakpoint)
6cb06a8c 11651 gdb_printf (fp, "hbreak");
06edf0c0
PA
11652 else
11653 internal_error (__FILE__, __LINE__,
7dd8e7ae 11654 _("unhandled breakpoint type %d"), (int) type);
06edf0c0 11655
7dd8e7ae 11656 gdb_printf (fp, " %s", event_location_to_string (location.get ()));
f00aae0f
KS
11657
11658 /* Print out extra_string if this breakpoint is pending. It might
11659 contain, for example, conditions that were set by the user. */
7dd8e7ae
TT
11660 if (loc == NULL && extra_string != NULL)
11661 gdb_printf (fp, " %s", extra_string.get ());
f00aae0f 11662
04d0163c 11663 print_recreate_thread (fp);
06edf0c0
PA
11664}
11665
2b5ab5b8
TT
11666std::vector<symtab_and_line>
11667base_breakpoint::decode_location (struct event_location *location,
11668 struct program_space *search_pspace)
983af33b 11669{
5d926615
TT
11670 if (event_location_type (location) == PROBE_LOCATION)
11671 return bkpt_probe_decode_location (this, location, search_pspace);
11672
2b5ab5b8 11673 return decode_location_default (this, location, search_pspace);
983af33b
SDJ
11674}
11675
06edf0c0
PA
11676/* Virtual table for internal breakpoints. */
11677
c359fff5
TT
11678void
11679internal_breakpoint::re_set ()
06edf0c0 11680{
c359fff5 11681 switch (type)
06edf0c0
PA
11682 {
11683 /* Delete overlay event and longjmp master breakpoints; they
11684 will be reset later by breakpoint_re_set. */
11685 case bp_overlay_event:
11686 case bp_longjmp_master:
11687 case bp_std_terminate_master:
11688 case bp_exception_master:
c359fff5 11689 delete_breakpoint (this);
06edf0c0
PA
11690 break;
11691
11692 /* This breakpoint is special, it's set up when the inferior
dda83cd7 11693 starts and we really don't want to touch it. */
06edf0c0
PA
11694 case bp_shlib_event:
11695
11696 /* Like bp_shlib_event, this breakpoint type is special. Once
11697 it is set up, we do not want to touch it. */
11698 case bp_thread_event:
11699 break;
11700 }
11701}
11702
c359fff5
TT
11703void
11704internal_breakpoint::check_status (bpstat *bs)
06edf0c0 11705{
c359fff5 11706 if (type == bp_shlib_event)
a9b3a50f
PA
11707 {
11708 /* If requested, stop when the dynamic linker notifies GDB of
11709 events. This allows the user to get control and place
11710 breakpoints in initializer routines for dynamically loaded
11711 objects (among other things). */
11712 bs->stop = stop_on_solib_events;
11713 bs->print = stop_on_solib_events;
11714 }
11715 else
11716 bs->stop = 0;
06edf0c0
PA
11717}
11718
c359fff5 11719enum print_stop_action
7bd86313 11720internal_breakpoint::print_it (const bpstat *bs) const
06edf0c0 11721{
c359fff5 11722 switch (type)
06edf0c0 11723 {
348d480f
PA
11724 case bp_shlib_event:
11725 /* Did we stop because the user set the stop_on_solib_events
11726 variable? (If so, we report this as a generic, "Stopped due
11727 to shlib event" message.) */
4e9e993a 11728 print_solib_event (false);
348d480f
PA
11729 break;
11730
11731 case bp_thread_event:
11732 /* Not sure how we will get here.
11733 GDB should not stop for these breakpoints. */
6cb06a8c 11734 gdb_printf (_("Thread Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
11735 break;
11736
11737 case bp_overlay_event:
11738 /* By analogy with the thread event, GDB should not stop for these. */
6cb06a8c 11739 gdb_printf (_("Overlay Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
11740 break;
11741
11742 case bp_longjmp_master:
11743 /* These should never be enabled. */
6cb06a8c 11744 gdb_printf (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
348d480f
PA
11745 break;
11746
11747 case bp_std_terminate_master:
11748 /* These should never be enabled. */
6cb06a8c
TT
11749 gdb_printf (_("std::terminate Master Breakpoint: "
11750 "gdb should not stop!\n"));
348d480f
PA
11751 break;
11752
11753 case bp_exception_master:
11754 /* These should never be enabled. */
6cb06a8c
TT
11755 gdb_printf (_("Exception Master Breakpoint: "
11756 "gdb should not stop!\n"));
06edf0c0
PA
11757 break;
11758 }
11759
001c8c33 11760 return PRINT_NOTHING;
06edf0c0
PA
11761}
11762
c359fff5 11763void
b713485d 11764internal_breakpoint::print_mention () const
06edf0c0
PA
11765{
11766 /* Nothing to mention. These breakpoints are internal. */
11767}
11768
06edf0c0
PA
11769/* Virtual table for momentary breakpoints */
11770
1fd30a47
TT
11771void
11772momentary_breakpoint::re_set ()
06edf0c0
PA
11773{
11774 /* Keep temporary breakpoints, which can be encountered when we step
4d1eb6b4 11775 over a dlopen call and solib_add is resetting the breakpoints.
06edf0c0
PA
11776 Otherwise these should have been blown away via the cleanup chain
11777 or by breakpoint_init_inferior when we rerun the executable. */
11778}
11779
1fd30a47
TT
11780void
11781momentary_breakpoint::check_status (bpstat *bs)
06edf0c0
PA
11782{
11783 /* Nothing. The point of these breakpoints is causing a stop. */
11784}
11785
1fd30a47 11786enum print_stop_action
7bd86313 11787momentary_breakpoint::print_it (const bpstat *bs) const
06edf0c0 11788{
001c8c33 11789 return PRINT_UNKNOWN;
348d480f
PA
11790}
11791
1fd30a47 11792void
b713485d 11793momentary_breakpoint::print_mention () const
348d480f 11794{
06edf0c0 11795 /* Nothing to mention. These breakpoints are internal. */
348d480f
PA
11796}
11797
e2e4d78b
JK
11798/* Ensure INITIATING_FRAME is cleared when no such breakpoint exists.
11799
11800 It gets cleared already on the removal of the first one of such placed
11801 breakpoints. This is OK as they get all removed altogether. */
11802
c1fc2657 11803longjmp_breakpoint::~longjmp_breakpoint ()
e2e4d78b 11804{
c1fc2657 11805 thread_info *tp = find_thread_global_id (this->thread);
e2e4d78b 11806
c1fc2657 11807 if (tp != NULL)
e2e4d78b 11808 tp->initiating_frame = null_frame_id;
e2e4d78b
JK
11809}
11810
55aa24fb 11811static void
626d2320 11812bkpt_probe_create_sals_from_location (struct event_location *location,
03ada39e
SM
11813 struct linespec_result *canonical)
11814
55aa24fb
SDJ
11815{
11816 struct linespec_sals lsal;
11817
c2f4122d 11818 lsal.sals = parse_probes (location, NULL, canonical);
8e9e35b1
TT
11819 lsal.canonical
11820 = xstrdup (event_location_to_string (canonical->location.get ()));
6c5b2ebe 11821 canonical->lsals.push_back (std::move (lsal));
55aa24fb
SDJ
11822}
11823
6c5b2ebe 11824static std::vector<symtab_and_line>
f00aae0f 11825bkpt_probe_decode_location (struct breakpoint *b,
626d2320 11826 struct event_location *location,
6c5b2ebe 11827 struct program_space *search_pspace)
55aa24fb 11828{
6c5b2ebe
PA
11829 std::vector<symtab_and_line> sals = parse_probes (location, search_pspace, NULL);
11830 if (sals.empty ())
55aa24fb 11831 error (_("probe not found"));
6c5b2ebe 11832 return sals;
55aa24fb
SDJ
11833}
11834
779dcceb
TT
11835int
11836tracepoint::breakpoint_hit (const struct bp_location *bl,
11837 const address_space *aspace, CORE_ADDR bp_addr,
11838 const target_waitstatus &ws)
348d480f
PA
11839{
11840 /* By definition, the inferior does not report stops at
11841 tracepoints. */
11842 return 0;
74960c60
VP
11843}
11844
779dcceb
TT
11845void
11846tracepoint::print_one_detail (struct ui_out *uiout) const
74960c60 11847{
779dcceb 11848 if (!static_trace_marker_id.empty ())
348d480f 11849 {
7b572efb
TT
11850 gdb_assert (type == bp_static_tracepoint
11851 || type == bp_static_marker_tracepoint);
cc59ec59 11852
6a831f06
PA
11853 uiout->message ("\tmarker id is %pF\n",
11854 string_field ("static-tracepoint-marker-string-id",
779dcceb 11855 static_trace_marker_id.c_str ()));
348d480f 11856 }
0d381245
VP
11857}
11858
779dcceb 11859void
b713485d 11860tracepoint::print_mention () const
a474d7c2 11861{
112e8700 11862 if (current_uiout->is_mi_like_p ())
348d480f 11863 return;
cc59ec59 11864
779dcceb 11865 switch (type)
348d480f
PA
11866 {
11867 case bp_tracepoint:
6cb06a8c 11868 gdb_printf (_("Tracepoint"));
779dcceb 11869 gdb_printf (_(" %d"), number);
348d480f
PA
11870 break;
11871 case bp_fast_tracepoint:
6cb06a8c 11872 gdb_printf (_("Fast tracepoint"));
779dcceb 11873 gdb_printf (_(" %d"), number);
348d480f
PA
11874 break;
11875 case bp_static_tracepoint:
7b572efb 11876 case bp_static_marker_tracepoint:
6cb06a8c 11877 gdb_printf (_("Static tracepoint"));
779dcceb 11878 gdb_printf (_(" %d"), number);
348d480f
PA
11879 break;
11880 default:
11881 internal_error (__FILE__, __LINE__,
779dcceb 11882 _("unhandled tracepoint type %d"), (int) type);
348d480f
PA
11883 }
11884
779dcceb 11885 say_where (this);
a474d7c2
PA
11886}
11887
779dcceb 11888void
4d1ae558 11889tracepoint::print_recreate (struct ui_file *fp) const
a474d7c2 11890{
779dcceb 11891 if (type == bp_fast_tracepoint)
6cb06a8c 11892 gdb_printf (fp, "ftrace");
7b572efb
TT
11893 else if (type == bp_static_tracepoint
11894 || type == bp_static_marker_tracepoint)
6cb06a8c 11895 gdb_printf (fp, "strace");
779dcceb 11896 else if (type == bp_tracepoint)
6cb06a8c 11897 gdb_printf (fp, "trace");
348d480f
PA
11898 else
11899 internal_error (__FILE__, __LINE__,
779dcceb 11900 _("unhandled tracepoint type %d"), (int) type);
cc59ec59 11901
779dcceb 11902 gdb_printf (fp, " %s", event_location_to_string (location.get ()));
04d0163c 11903 print_recreate_thread (fp);
d9b3f62e 11904
779dcceb
TT
11905 if (pass_count)
11906 gdb_printf (fp, " passcount %d\n", pass_count);
a474d7c2
PA
11907}
11908
bac7c5cf 11909/* Virtual table for tracepoints on static probes. */
55aa24fb
SDJ
11910
11911static void
f00aae0f 11912tracepoint_probe_create_sals_from_location
626d2320 11913 (struct event_location *location,
03ada39e 11914 struct linespec_result *canonical)
55aa24fb
SDJ
11915{
11916 /* We use the same method for breakpoint on probes. */
03ada39e 11917 bkpt_probe_create_sals_from_location (location, canonical);
55aa24fb
SDJ
11918}
11919
f293a0b5
TT
11920void
11921dprintf_breakpoint::re_set ()
5c2b4418 11922{
f293a0b5 11923 breakpoint_re_set_default (this);
5c2b4418 11924
f00aae0f 11925 /* extra_string should never be non-NULL for dprintf. */
f293a0b5 11926 gdb_assert (extra_string != NULL);
5c2b4418
HZ
11927
11928 /* 1 - connect to target 1, that can run breakpoint commands.
11929 2 - create a dprintf, which resolves fine.
11930 3 - disconnect from target 1
11931 4 - connect to target 2, that can NOT run breakpoint commands.
11932
11933 After steps #3/#4, you'll want the dprintf command list to
11934 be updated, because target 1 and 2 may well return different
11935 answers for target_can_run_breakpoint_commands().
11936 Given absence of finer grained resetting, we get to do
11937 it all the time. */
f293a0b5
TT
11938 if (extra_string != NULL)
11939 update_dprintf_command_list (this);
5c2b4418
HZ
11940}
11941
f293a0b5 11942/* Implement the "print_recreate" method for dprintf. */
2d9442cc 11943
f293a0b5 11944void
4d1ae558 11945dprintf_breakpoint::print_recreate (struct ui_file *fp) const
2d9442cc 11946{
6cb06a8c 11947 gdb_printf (fp, "dprintf %s,%s",
f293a0b5
TT
11948 event_location_to_string (location.get ()),
11949 extra_string.get ());
04d0163c 11950 print_recreate_thread (fp);
2d9442cc
HZ
11951}
11952
f293a0b5 11953/* Implement the "after_condition_true" method for dprintf.
9d6e6e84
HZ
11954
11955 dprintf's are implemented with regular commands in their command
11956 list, but we run the commands here instead of before presenting the
11957 stop to the user, as dprintf's don't actually cause a stop. This
11958 also makes it so that the commands of multiple dprintfs at the same
11959 address are all handled. */
11960
f293a0b5
TT
11961void
11962dprintf_breakpoint::after_condition_true (struct bpstat *bs)
9d6e6e84 11963{
9d6e6e84
HZ
11964 /* dprintf's never cause a stop. This wasn't set in the
11965 check_status hook instead because that would make the dprintf's
11966 condition not be evaluated. */
11967 bs->stop = 0;
11968
11969 /* Run the command list here. Take ownership of it instead of
11970 copying. We never want these commands to run later in
11971 bpstat_do_actions, if a breakpoint that causes a stop happens to
11972 be set at same address as this dprintf, or even if running the
11973 commands here throws. */
9c95aea1
KB
11974 counted_command_line cmds = std::move (bs->commands);
11975 gdb_assert (cmds != nullptr);
11976 execute_control_commands (cmds.get (), 0);
9d6e6e84
HZ
11977}
11978
983af33b
SDJ
11979/* The breakpoint_ops structure to be used on static tracepoints with
11980 markers (`-m'). */
11981
11982static void
626d2320 11983strace_marker_create_sals_from_location (struct event_location *location,
03ada39e 11984 struct linespec_result *canonical)
983af33b
SDJ
11985{
11986 struct linespec_sals lsal;
f00aae0f 11987 const char *arg_start, *arg;
983af33b 11988
a20714ff 11989 arg = arg_start = get_linespec_location (location)->spec_string;
f00aae0f 11990 lsal.sals = decode_static_tracepoint_spec (&arg);
983af33b 11991
f2fc3015
TT
11992 std::string str (arg_start, arg - arg_start);
11993 const char *ptr = str.c_str ();
a20714ff
PA
11994 canonical->location
11995 = new_linespec_location (&ptr, symbol_name_match_type::FULL);
983af33b 11996
8e9e35b1
TT
11997 lsal.canonical
11998 = xstrdup (event_location_to_string (canonical->location.get ()));
6c5b2ebe 11999 canonical->lsals.push_back (std::move (lsal));
983af33b
SDJ
12000}
12001
12002static void
12003strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
12004 struct linespec_result *canonical,
e1e01040
PA
12005 gdb::unique_xmalloc_ptr<char> cond_string,
12006 gdb::unique_xmalloc_ptr<char> extra_string,
983af33b
SDJ
12007 enum bptype type_wanted,
12008 enum bpdisp disposition,
12009 int thread,
12010 int task, int ignore_count,
983af33b 12011 int from_tty, int enabled,
44f238bb 12012 int internal, unsigned flags)
983af33b 12013{
6c5b2ebe 12014 const linespec_sals &lsal = canonical->lsals[0];
983af33b
SDJ
12015
12016 /* If the user is creating a static tracepoint by marker id
12017 (strace -m MARKER_ID), then store the sals index, so that
12018 breakpoint_re_set can try to match up which of the newly
12019 found markers corresponds to this one, and, don't try to
12020 expand multiple locations for each sal, given than SALS
12021 already should contain all sals for MARKER_ID. */
12022
6c5b2ebe 12023 for (size_t i = 0; i < lsal.sals.size (); i++)
983af33b 12024 {
6c5b2ebe
PA
12025 event_location_up location
12026 = copy_event_location (canonical->location.get ());
983af33b 12027
3b003a61
PA
12028 std::unique_ptr<tracepoint> tp
12029 (new tracepoint (gdbarch,
12030 type_wanted,
12031 lsal.sals[i],
12032 std::move (location),
12033 NULL,
12034 std::move (cond_string),
12035 std::move (extra_string),
12036 disposition,
12037 thread, task, ignore_count,
12038 from_tty, enabled, flags,
12039 canonical->special_display));
12040
983af33b
SDJ
12041 /* Given that its possible to have multiple markers with
12042 the same string id, if the user is creating a static
12043 tracepoint by marker id ("strace -m MARKER_ID"), then
12044 store the sals index, so that breakpoint_re_set can
12045 try to match up which of the newly found markers
12046 corresponds to this one */
12047 tp->static_trace_marker_id_idx = i;
12048
b270e6f9 12049 install_breakpoint (internal, std::move (tp), 0);
983af33b
SDJ
12050 }
12051}
12052
6d7a8c56
TT
12053std::vector<symtab_and_line>
12054static_marker_tracepoint::decode_location (struct event_location *location,
12055 struct program_space *search_pspace)
983af33b 12056{
a20714ff 12057 const char *s = get_linespec_location (location)->spec_string;
983af33b 12058
6c5b2ebe 12059 std::vector<symtab_and_line> sals = decode_static_tracepoint_spec (&s);
6d7a8c56 12060 if (sals.size () > static_trace_marker_id_idx)
983af33b 12061 {
6d7a8c56 12062 sals[0] = sals[static_trace_marker_id_idx];
6c5b2ebe
PA
12063 sals.resize (1);
12064 return sals;
983af33b
SDJ
12065 }
12066 else
6d7a8c56 12067 error (_("marker %s not found"), static_trace_marker_id.c_str ());
983af33b
SDJ
12068}
12069
8613a476
TT
12070/* Static tracepoints with marker (`-m'). */
12071static struct breakpoint_ops strace_marker_breakpoint_ops =
12072{
12073 strace_marker_create_sals_from_location,
12074 strace_marker_create_breakpoints_sal,
12075};
983af33b
SDJ
12076
12077static int
12078strace_marker_p (struct breakpoint *b)
12079{
7b572efb 12080 return b->type == bp_static_marker_tracepoint;
983af33b
SDJ
12081}
12082
53a5351d 12083/* Delete a breakpoint and clean up all traces of it in the data
f431efe5 12084 structures. */
c906108c
SS
12085
12086void
fba45db2 12087delete_breakpoint (struct breakpoint *bpt)
c906108c 12088{
8a3fe4f8 12089 gdb_assert (bpt != NULL);
c906108c 12090
4a64f543
MS
12091 /* Has this bp already been deleted? This can happen because
12092 multiple lists can hold pointers to bp's. bpstat lists are
12093 especial culprits.
12094
12095 One example of this happening is a watchpoint's scope bp. When
12096 the scope bp triggers, we notice that the watchpoint is out of
12097 scope, and delete it. We also delete its scope bp. But the
12098 scope bp is marked "auto-deleting", and is already on a bpstat.
12099 That bpstat is then checked for auto-deleting bp's, which are
12100 deleted.
12101
12102 A real solution to this problem might involve reference counts in
12103 bp's, and/or giving them pointers back to their referencing
12104 bpstat's, and teaching delete_breakpoint to only free a bp's
12105 storage when no more references were extent. A cheaper bandaid
12106 was chosen. */
c906108c
SS
12107 if (bpt->type == bp_none)
12108 return;
12109
4a64f543
MS
12110 /* At least avoid this stale reference until the reference counting
12111 of breakpoints gets resolved. */
d0fb5eae 12112 if (bpt->related_breakpoint != bpt)
e5a0a904 12113 {
d0fb5eae 12114 struct breakpoint *related;
3a5c3e22 12115 struct watchpoint *w;
d0fb5eae
JK
12116
12117 if (bpt->type == bp_watchpoint_scope)
3a5c3e22 12118 w = (struct watchpoint *) bpt->related_breakpoint;
d0fb5eae 12119 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
3a5c3e22
PA
12120 w = (struct watchpoint *) bpt;
12121 else
12122 w = NULL;
12123 if (w != NULL)
12124 watchpoint_del_at_next_stop (w);
d0fb5eae
JK
12125
12126 /* Unlink bpt from the bpt->related_breakpoint ring. */
12127 for (related = bpt; related->related_breakpoint != bpt;
12128 related = related->related_breakpoint);
12129 related->related_breakpoint = bpt->related_breakpoint;
12130 bpt->related_breakpoint = bpt;
e5a0a904
JK
12131 }
12132
a9634178
TJB
12133 /* watch_command_1 creates a watchpoint but only sets its number if
12134 update_watchpoint succeeds in creating its bp_locations. If there's
12135 a problem in that process, we'll be asked to delete the half-created
12136 watchpoint. In that case, don't announce the deletion. */
12137 if (bpt->number)
76727919 12138 gdb::observers::breakpoint_deleted.notify (bpt);
c906108c 12139
c906108c
SS
12140 if (breakpoint_chain == bpt)
12141 breakpoint_chain = bpt->next;
12142
43892fdf 12143 for (breakpoint *b : all_breakpoints ())
c906108c 12144 if (b->next == bpt)
01add95b
SM
12145 {
12146 b->next = bpt->next;
12147 break;
12148 }
c906108c 12149
f431efe5
PA
12150 /* Be sure no bpstat's are pointing at the breakpoint after it's
12151 been freed. */
12152 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
e5dd4106 12153 in all threads for now. Note that we cannot just remove bpstats
f431efe5
PA
12154 pointing at bpt from the stop_bpstat list entirely, as breakpoint
12155 commands are associated with the bpstat; if we remove it here,
12156 then the later call to bpstat_do_actions (&stop_bpstat); in
12157 event-top.c won't do anything, and temporary breakpoints with
12158 commands won't work. */
12159
12160 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
12161
4a64f543
MS
12162 /* Now that breakpoint is removed from breakpoint list, update the
12163 global location list. This will remove locations that used to
12164 belong to this breakpoint. Do this before freeing the breakpoint
12165 itself, since remove_breakpoint looks at location's owner. It
12166 might be better design to have location completely
12167 self-contained, but it's not the case now. */
44702360 12168 update_global_location_list (UGLL_DONT_INSERT);
74960c60 12169
4a64f543
MS
12170 /* On the chance that someone will soon try again to delete this
12171 same bp, we mark it as deleted before freeing its storage. */
c906108c 12172 bpt->type = bp_none;
4d01a485 12173 delete bpt;
c906108c
SS
12174}
12175
51be5b68
PA
12176/* Iterator function to call a user-provided callback function once
12177 for each of B and its related breakpoints. */
12178
12179static void
12180iterate_over_related_breakpoints (struct breakpoint *b,
48649e1b 12181 gdb::function_view<void (breakpoint *)> function)
51be5b68
PA
12182{
12183 struct breakpoint *related;
12184
12185 related = b;
12186 do
12187 {
12188 struct breakpoint *next;
12189
12190 /* FUNCTION may delete RELATED. */
12191 next = related->related_breakpoint;
12192
12193 if (next == related)
12194 {
12195 /* RELATED is the last ring entry. */
48649e1b 12196 function (related);
51be5b68
PA
12197
12198 /* FUNCTION may have deleted it, so we'd never reach back to
12199 B. There's nothing left to do anyway, so just break
12200 out. */
12201 break;
12202 }
12203 else
48649e1b 12204 function (related);
51be5b68
PA
12205
12206 related = next;
12207 }
12208 while (related != b);
12209}
95a42b64 12210
4495129a 12211static void
981a3fb3 12212delete_command (const char *arg, int from_tty)
c906108c 12213{
ea9365bb
TT
12214 dont_repeat ();
12215
c906108c
SS
12216 if (arg == 0)
12217 {
12218 int breaks_to_delete = 0;
12219
46c6471b 12220 /* Delete all breakpoints if no argument. Do not delete
dda83cd7
SM
12221 internal breakpoints, these have to be deleted with an
12222 explicit breakpoint number argument. */
43892fdf 12223 for (breakpoint *b : all_breakpoints ())
46c6471b 12224 if (user_breakpoint_p (b))
973d738b
DJ
12225 {
12226 breaks_to_delete = 1;
12227 break;
12228 }
c906108c
SS
12229
12230 /* Ask user only if there are some breakpoints to delete. */
12231 if (!from_tty
e2e0b3e5 12232 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
1428b37a
SM
12233 for (breakpoint *b : all_breakpoints_safe ())
12234 if (user_breakpoint_p (b))
12235 delete_breakpoint (b);
c906108c
SS
12236 }
12237 else
48649e1b 12238 map_breakpoint_numbers
b926417a 12239 (arg, [&] (breakpoint *br)
48649e1b 12240 {
b926417a 12241 iterate_over_related_breakpoints (br, delete_breakpoint);
48649e1b 12242 });
c906108c
SS
12243}
12244
c2f4122d
PA
12245/* Return true if all locations of B bound to PSPACE are pending. If
12246 PSPACE is NULL, all locations of all program spaces are
12247 considered. */
12248
0d381245 12249static int
c2f4122d 12250all_locations_are_pending (struct breakpoint *b, struct program_space *pspace)
fe3f5fa8 12251{
40cb8ca5 12252 for (bp_location *loc : b->locations ())
c2f4122d
PA
12253 if ((pspace == NULL
12254 || loc->pspace == pspace)
12255 && !loc->shlib_disabled
8645ff69 12256 && !loc->pspace->executing_startup)
0d381245
VP
12257 return 0;
12258 return 1;
fe3f5fa8
VP
12259}
12260
776592bf
DE
12261/* Subroutine of update_breakpoint_locations to simplify it.
12262 Return non-zero if multiple fns in list LOC have the same name.
12263 Null names are ignored. */
12264
12265static int
12266ambiguous_names_p (struct bp_location *loc)
12267{
12268 struct bp_location *l;
2698f5ea 12269 htab_up htab (htab_create_alloc (13, htab_hash_string, htab_eq_string, NULL,
c1fb9836 12270 xcalloc, xfree));
776592bf
DE
12271
12272 for (l = loc; l != NULL; l = l->next)
12273 {
12274 const char **slot;
23d6ee64 12275 const char *name = l->function_name.get ();
776592bf
DE
12276
12277 /* Allow for some names to be NULL, ignore them. */
12278 if (name == NULL)
12279 continue;
12280
c1fb9836 12281 slot = (const char **) htab_find_slot (htab.get (), (const void *) name,
776592bf 12282 INSERT);
4a64f543
MS
12283 /* NOTE: We can assume slot != NULL here because xcalloc never
12284 returns NULL. */
776592bf 12285 if (*slot != NULL)
c1fb9836 12286 return 1;
776592bf
DE
12287 *slot = name;
12288 }
12289
776592bf
DE
12290 return 0;
12291}
12292
0fb4aa4b
PA
12293/* When symbols change, it probably means the sources changed as well,
12294 and it might mean the static tracepoint markers are no longer at
12295 the same address or line numbers they used to be at last we
12296 checked. Losing your static tracepoints whenever you rebuild is
12297 undesirable. This function tries to resync/rematch gdb static
12298 tracepoints with the markers on the target, for static tracepoints
12299 that have not been set by marker id. Static tracepoint that have
12300 been set by marker id are reset by marker id in breakpoint_re_set.
12301 The heuristic is:
12302
12303 1) For a tracepoint set at a specific address, look for a marker at
12304 the old PC. If one is found there, assume to be the same marker.
12305 If the name / string id of the marker found is different from the
12306 previous known name, assume that means the user renamed the marker
12307 in the sources, and output a warning.
12308
12309 2) For a tracepoint set at a given line number, look for a marker
12310 at the new address of the old line number. If one is found there,
12311 assume to be the same marker. If the name / string id of the
12312 marker found is different from the previous known name, assume that
12313 means the user renamed the marker in the sources, and output a
12314 warning.
12315
12316 3) If a marker is no longer found at the same address or line, it
12317 may mean the marker no longer exists. But it may also just mean
12318 the code changed a bit. Maybe the user added a few lines of code
12319 that made the marker move up or down (in line number terms). Ask
12320 the target for info about the marker with the string id as we knew
12321 it. If found, update line number and address in the matching
12322 static tracepoint. This will get confused if there's more than one
12323 marker with the same ID (possible in UST, although unadvised
12324 precisely because it confuses tools). */
12325
12326static struct symtab_and_line
12327update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
12328{
d9b3f62e 12329 struct tracepoint *tp = (struct tracepoint *) b;
0fb4aa4b
PA
12330 struct static_tracepoint_marker marker;
12331 CORE_ADDR pc;
0fb4aa4b
PA
12332
12333 pc = sal.pc;
12334 if (sal.line)
12335 find_line_pc (sal.symtab, sal.line, &pc);
12336
12337 if (target_static_tracepoint_marker_at (pc, &marker))
12338 {
5d9310c4 12339 if (tp->static_trace_marker_id != marker.str_id)
0fb4aa4b 12340 warning (_("static tracepoint %d changed probed marker from %s to %s"),
5d9310c4
SM
12341 b->number, tp->static_trace_marker_id.c_str (),
12342 marker.str_id.c_str ());
0fb4aa4b 12343
5d9310c4 12344 tp->static_trace_marker_id = std::move (marker.str_id);
0fb4aa4b
PA
12345
12346 return sal;
12347 }
12348
12349 /* Old marker wasn't found on target at lineno. Try looking it up
12350 by string ID. */
12351 if (!sal.explicit_pc
12352 && sal.line != 0
12353 && sal.symtab != NULL
5d9310c4 12354 && !tp->static_trace_marker_id.empty ())
0fb4aa4b 12355 {
5d9310c4
SM
12356 std::vector<static_tracepoint_marker> markers
12357 = target_static_tracepoint_markers_by_strid
12358 (tp->static_trace_marker_id.c_str ());
0fb4aa4b 12359
5d9310c4 12360 if (!markers.empty ())
0fb4aa4b 12361 {
0fb4aa4b 12362 struct symbol *sym;
80e1d417 12363 struct static_tracepoint_marker *tpmarker;
79a45e25 12364 struct ui_out *uiout = current_uiout;
67994074 12365 struct explicit_location explicit_loc;
0fb4aa4b 12366
5d9310c4 12367 tpmarker = &markers[0];
0fb4aa4b 12368
5d9310c4 12369 tp->static_trace_marker_id = std::move (tpmarker->str_id);
0fb4aa4b
PA
12370
12371 warning (_("marker for static tracepoint %d (%s) not "
12372 "found at previous line number"),
5d9310c4 12373 b->number, tp->static_trace_marker_id.c_str ());
0fb4aa4b 12374
51abb421 12375 symtab_and_line sal2 = find_pc_line (tpmarker->address, 0);
80e1d417 12376 sym = find_pc_sect_function (tpmarker->address, NULL);
112e8700 12377 uiout->text ("Now in ");
0fb4aa4b
PA
12378 if (sym)
12379 {
987012b8 12380 uiout->field_string ("func", sym->print_name (),
e43b10e1 12381 function_name_style.style ());
112e8700 12382 uiout->text (" at ");
0fb4aa4b 12383 }
112e8700 12384 uiout->field_string ("file",
cbe56571 12385 symtab_to_filename_for_display (sal2.symtab),
e43b10e1 12386 file_name_style.style ());
112e8700 12387 uiout->text (":");
0fb4aa4b 12388
112e8700 12389 if (uiout->is_mi_like_p ())
0fb4aa4b 12390 {
0b0865da 12391 const char *fullname = symtab_to_fullname (sal2.symtab);
0fb4aa4b 12392
112e8700 12393 uiout->field_string ("fullname", fullname);
0fb4aa4b
PA
12394 }
12395
381befee 12396 uiout->field_signed ("line", sal2.line);
112e8700 12397 uiout->text ("\n");
0fb4aa4b 12398
80e1d417 12399 b->loc->line_number = sal2.line;
2f202fde 12400 b->loc->symtab = sym != NULL ? sal2.symtab : NULL;
0fb4aa4b 12401
d28cd78a 12402 b->location.reset (NULL);
67994074
KS
12403 initialize_explicit_location (&explicit_loc);
12404 explicit_loc.source_filename
00e52e53 12405 = ASTRDUP (symtab_to_filename_for_display (sal2.symtab));
67994074
KS
12406 explicit_loc.line_offset.offset = b->loc->line_number;
12407 explicit_loc.line_offset.sign = LINE_OFFSET_NONE;
d28cd78a 12408 b->location = new_explicit_location (&explicit_loc);
0fb4aa4b
PA
12409
12410 /* Might be nice to check if function changed, and warn if
12411 so. */
0fb4aa4b
PA
12412 }
12413 }
12414 return sal;
12415}
12416
8d3788bd
VP
12417/* Returns 1 iff locations A and B are sufficiently same that
12418 we don't need to report breakpoint as changed. */
12419
12420static int
12421locations_are_equal (struct bp_location *a, struct bp_location *b)
12422{
12423 while (a && b)
12424 {
12425 if (a->address != b->address)
12426 return 0;
12427
12428 if (a->shlib_disabled != b->shlib_disabled)
12429 return 0;
12430
12431 if (a->enabled != b->enabled)
12432 return 0;
12433
b5fa468f
TBA
12434 if (a->disabled_by_cond != b->disabled_by_cond)
12435 return 0;
12436
8d3788bd
VP
12437 a = a->next;
12438 b = b->next;
12439 }
12440
12441 if ((a == NULL) != (b == NULL))
12442 return 0;
12443
12444 return 1;
12445}
12446
c2f4122d
PA
12447/* Split all locations of B that are bound to PSPACE out of B's
12448 location list to a separate list and return that list's head. If
12449 PSPACE is NULL, hoist out all locations of B. */
12450
12451static struct bp_location *
12452hoist_existing_locations (struct breakpoint *b, struct program_space *pspace)
12453{
12454 struct bp_location head;
12455 struct bp_location *i = b->loc;
12456 struct bp_location **i_link = &b->loc;
12457 struct bp_location *hoisted = &head;
12458
12459 if (pspace == NULL)
12460 {
12461 i = b->loc;
12462 b->loc = NULL;
12463 return i;
12464 }
12465
12466 head.next = NULL;
12467
12468 while (i != NULL)
12469 {
12470 if (i->pspace == pspace)
12471 {
12472 *i_link = i->next;
12473 i->next = NULL;
12474 hoisted->next = i;
12475 hoisted = i;
12476 }
12477 else
12478 i_link = &i->next;
12479 i = *i_link;
12480 }
12481
12482 return head.next;
12483}
12484
12485/* Create new breakpoint locations for B (a hardware or software
12486 breakpoint) based on SALS and SALS_END. If SALS_END.NELTS is not
12487 zero, then B is a ranged breakpoint. Only recreates locations for
12488 FILTER_PSPACE. Locations of other program spaces are left
12489 untouched. */
f1310107 12490
0e30163f 12491void
0d381245 12492update_breakpoint_locations (struct breakpoint *b,
c2f4122d 12493 struct program_space *filter_pspace,
6c5b2ebe
PA
12494 gdb::array_view<const symtab_and_line> sals,
12495 gdb::array_view<const symtab_and_line> sals_end)
fe3f5fa8 12496{
c2f4122d 12497 struct bp_location *existing_locations;
0d381245 12498
6c5b2ebe 12499 if (!sals_end.empty () && (sals.size () != 1 || sals_end.size () != 1))
f8eba3c6
TT
12500 {
12501 /* Ranged breakpoints have only one start location and one end
12502 location. */
12503 b->enable_state = bp_disabled;
6cb06a8c
TT
12504 gdb_printf (gdb_stderr,
12505 _("Could not reset ranged breakpoint %d: "
12506 "multiple locations found\n"),
12507 b->number);
f8eba3c6
TT
12508 return;
12509 }
f1310107 12510
4a64f543
MS
12511 /* If there's no new locations, and all existing locations are
12512 pending, don't do anything. This optimizes the common case where
12513 all locations are in the same shared library, that was unloaded.
12514 We'd like to retain the location, so that when the library is
12515 loaded again, we don't loose the enabled/disabled status of the
12516 individual locations. */
6c5b2ebe 12517 if (all_locations_are_pending (b, filter_pspace) && sals.empty ())
fe3f5fa8
VP
12518 return;
12519
c2f4122d 12520 existing_locations = hoist_existing_locations (b, filter_pspace);
fe3f5fa8 12521
6c5b2ebe 12522 for (const auto &sal : sals)
fe3f5fa8 12523 {
f8eba3c6
TT
12524 struct bp_location *new_loc;
12525
6c5b2ebe 12526 switch_to_program_space_and_thread (sal.pspace);
f8eba3c6 12527
06615628 12528 new_loc = b->add_location (sal);
fe3f5fa8 12529
0d381245
VP
12530 /* Reparse conditions, they might contain references to the
12531 old symtab. */
12532 if (b->cond_string != NULL)
12533 {
bbc13ae3 12534 const char *s;
fe3f5fa8 12535
6f781ee3 12536 s = b->cond_string.get ();
a70b8144 12537 try
0d381245 12538 {
6c5b2ebe
PA
12539 new_loc->cond = parse_exp_1 (&s, sal.pc,
12540 block_for_pc (sal.pc),
0d381245
VP
12541 0);
12542 }
230d2906 12543 catch (const gdb_exception_error &e)
0d381245 12544 {
b5fa468f 12545 new_loc->disabled_by_cond = true;
0d381245
VP
12546 }
12547 }
fe3f5fa8 12548
6c5b2ebe 12549 if (!sals_end.empty ())
f1310107 12550 {
6c5b2ebe 12551 CORE_ADDR end = find_breakpoint_range_end (sals_end[0]);
f1310107 12552
6c5b2ebe 12553 new_loc->length = end - sals[0].pc + 1;
f1310107 12554 }
0d381245 12555 }
fe3f5fa8 12556
4a64f543
MS
12557 /* If possible, carry over 'disable' status from existing
12558 breakpoints. */
0d381245
VP
12559 {
12560 struct bp_location *e = existing_locations;
776592bf
DE
12561 /* If there are multiple breakpoints with the same function name,
12562 e.g. for inline functions, comparing function names won't work.
12563 Instead compare pc addresses; this is just a heuristic as things
12564 may have moved, but in practice it gives the correct answer
12565 often enough until a better solution is found. */
12566 int have_ambiguous_names = ambiguous_names_p (b->loc);
12567
0d381245
VP
12568 for (; e; e = e->next)
12569 {
b5fa468f 12570 if ((!e->enabled || e->disabled_by_cond) && e->function_name)
0d381245 12571 {
776592bf
DE
12572 if (have_ambiguous_names)
12573 {
40cb8ca5 12574 for (bp_location *l : b->locations ())
7f32a4d5
PA
12575 {
12576 /* Ignore software vs hardware location type at
12577 this point, because with "set breakpoint
12578 auto-hw", after a re-set, locations that were
12579 hardware can end up as software, or vice versa.
12580 As mentioned above, this is an heuristic and in
12581 practice should give the correct answer often
12582 enough. */
12583 if (breakpoint_locations_match (e, l, true))
12584 {
b5fa468f
TBA
12585 l->enabled = e->enabled;
12586 l->disabled_by_cond = e->disabled_by_cond;
7f32a4d5
PA
12587 break;
12588 }
12589 }
776592bf
DE
12590 }
12591 else
12592 {
40cb8ca5 12593 for (bp_location *l : b->locations ())
776592bf 12594 if (l->function_name
23d6ee64
TT
12595 && strcmp (e->function_name.get (),
12596 l->function_name.get ()) == 0)
776592bf 12597 {
b5fa468f
TBA
12598 l->enabled = e->enabled;
12599 l->disabled_by_cond = e->disabled_by_cond;
776592bf
DE
12600 break;
12601 }
12602 }
0d381245
VP
12603 }
12604 }
12605 }
fe3f5fa8 12606
8d3788bd 12607 if (!locations_are_equal (existing_locations, b->loc))
76727919 12608 gdb::observers::breakpoint_modified.notify (b);
fe3f5fa8
VP
12609}
12610
f00aae0f 12611/* Find the SaL locations corresponding to the given LOCATION.
ef23e705
TJB
12612 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
12613
6c5b2ebe 12614static std::vector<symtab_and_line>
f00aae0f 12615location_to_sals (struct breakpoint *b, struct event_location *location,
c2f4122d 12616 struct program_space *search_pspace, int *found)
ef23e705 12617{
cc06b668 12618 struct gdb_exception exception;
ef23e705 12619
6c5b2ebe
PA
12620 std::vector<symtab_and_line> sals;
12621
a70b8144 12622 try
ef23e705 12623 {
c47614fe 12624 sals = b->decode_location (location, search_pspace);
ef23e705 12625 }
94aeb44b 12626 catch (gdb_exception_error &e)
ef23e705
TJB
12627 {
12628 int not_found_and_ok = 0;
492d29ea 12629
ef23e705
TJB
12630 /* For pending breakpoints, it's expected that parsing will
12631 fail until the right shared library is loaded. User has
12632 already told to create pending breakpoints and don't need
12633 extra messages. If breakpoint is in bp_shlib_disabled
12634 state, then user already saw the message about that
12635 breakpoint being disabled, and don't want to see more
12636 errors. */
58438ac1 12637 if (e.error == NOT_FOUND_ERROR
c2f4122d
PA
12638 && (b->condition_not_parsed
12639 || (b->loc != NULL
12640 && search_pspace != NULL
12641 && b->loc->pspace != search_pspace)
ef23e705 12642 || (b->loc && b->loc->shlib_disabled)
f8eba3c6 12643 || (b->loc && b->loc->pspace->executing_startup)
ef23e705
TJB
12644 || b->enable_state == bp_disabled))
12645 not_found_and_ok = 1;
12646
12647 if (!not_found_and_ok)
12648 {
12649 /* We surely don't want to warn about the same breakpoint
12650 10 times. One solution, implemented here, is disable
12651 the breakpoint on error. Another solution would be to
12652 have separate 'warning emitted' flag. Since this
12653 happens only when a binary has changed, I don't know
12654 which approach is better. */
12655 b->enable_state = bp_disabled;
eedc3f4f 12656 throw;
ef23e705 12657 }
94aeb44b
TT
12658
12659 exception = std::move (e);
ef23e705
TJB
12660 }
12661
492d29ea 12662 if (exception.reason == 0 || exception.error != NOT_FOUND_ERROR)
ef23e705 12663 {
6c5b2ebe
PA
12664 for (auto &sal : sals)
12665 resolve_sal_pc (&sal);
f00aae0f 12666 if (b->condition_not_parsed && b->extra_string != NULL)
ef23e705 12667 {
6f781ee3 12668 gdb::unique_xmalloc_ptr<char> cond_string, extra_string;
ed1d1739 12669 int thread, task;
ef23e705 12670
6f781ee3 12671 find_condition_and_thread_for_sals (sals, b->extra_string.get (),
b5fa468f
TBA
12672 &cond_string, &thread,
12673 &task, &extra_string);
f00aae0f 12674 gdb_assert (b->cond_string == NULL);
ef23e705 12675 if (cond_string)
6f781ee3 12676 b->cond_string = std::move (cond_string);
ef23e705
TJB
12677 b->thread = thread;
12678 b->task = task;
e7e0cddf 12679 if (extra_string)
6f781ee3 12680 b->extra_string = std::move (extra_string);
ef23e705
TJB
12681 b->condition_not_parsed = 0;
12682 }
12683
7b572efb 12684 if (b->type == bp_static_tracepoint)
6c5b2ebe 12685 sals[0] = update_static_tracepoint (b, sals[0]);
ef23e705 12686
58438ac1
TT
12687 *found = 1;
12688 }
12689 else
12690 *found = 0;
ef23e705
TJB
12691
12692 return sals;
12693}
12694
348d480f
PA
12695/* The default re_set method, for typical hardware or software
12696 breakpoints. Reevaluate the breakpoint and recreate its
12697 locations. */
12698
12699static void
28010a5d 12700breakpoint_re_set_default (struct breakpoint *b)
ef23e705 12701{
c2f4122d 12702 struct program_space *filter_pspace = current_program_space;
6c5b2ebe 12703 std::vector<symtab_and_line> expanded, expanded_end;
ef23e705 12704
6c5b2ebe
PA
12705 int found;
12706 std::vector<symtab_and_line> sals = location_to_sals (b, b->location.get (),
12707 filter_pspace, &found);
ef23e705 12708 if (found)
6c5b2ebe 12709 expanded = std::move (sals);
ef23e705 12710
f00aae0f 12711 if (b->location_range_end != NULL)
f1310107 12712 {
6c5b2ebe
PA
12713 std::vector<symtab_and_line> sals_end
12714 = location_to_sals (b, b->location_range_end.get (),
12715 filter_pspace, &found);
f1310107 12716 if (found)
6c5b2ebe 12717 expanded_end = std::move (sals_end);
f1310107
TJB
12718 }
12719
c2f4122d 12720 update_breakpoint_locations (b, filter_pspace, expanded, expanded_end);
28010a5d
PA
12721}
12722
983af33b
SDJ
12723/* Default method for creating SALs from an address string. It basically
12724 calls parse_breakpoint_sals. Return 1 for success, zero for failure. */
12725
12726static void
626d2320 12727create_sals_from_location_default (struct event_location *location,
03ada39e 12728 struct linespec_result *canonical)
983af33b 12729{
f00aae0f 12730 parse_breakpoint_sals (location, canonical);
983af33b
SDJ
12731}
12732
983af33b 12733/* Decode the line represented by S by calling decode_line_full. This is the
5f700d83 12734 default function for the `decode_location' method of breakpoint_ops. */
983af33b 12735
6c5b2ebe 12736static std::vector<symtab_and_line>
f00aae0f 12737decode_location_default (struct breakpoint *b,
626d2320 12738 struct event_location *location,
6c5b2ebe 12739 struct program_space *search_pspace)
983af33b
SDJ
12740{
12741 struct linespec_result canonical;
12742
c2f4122d 12743 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, search_pspace,
cafb3438 12744 NULL, 0, &canonical, multiple_symbols_all,
c0e8dcd8 12745 b->filter.get ());
983af33b
SDJ
12746
12747 /* We should get 0 or 1 resulting SALs. */
6c5b2ebe 12748 gdb_assert (canonical.lsals.size () < 2);
983af33b 12749
6c5b2ebe 12750 if (!canonical.lsals.empty ())
983af33b 12751 {
6c5b2ebe
PA
12752 const linespec_sals &lsal = canonical.lsals[0];
12753 return std::move (lsal.sals);
983af33b 12754 }
6c5b2ebe 12755 return {};
983af33b
SDJ
12756}
12757
bf469271 12758/* Reset a breakpoint. */
c906108c 12759
bf469271
PA
12760static void
12761breakpoint_re_set_one (breakpoint *b)
c906108c 12762{
fdf44873
TT
12763 input_radix = b->input_radix;
12764 set_language (b->language);
c906108c 12765
c47614fe 12766 b->re_set ();
c906108c
SS
12767}
12768
c2f4122d
PA
12769/* Re-set breakpoint locations for the current program space.
12770 Locations bound to other program spaces are left untouched. */
12771
c906108c 12772void
69de3c6a 12773breakpoint_re_set (void)
c906108c 12774{
c5aa993b 12775 {
fdf44873
TT
12776 scoped_restore_current_language save_language;
12777 scoped_restore save_input_radix = make_scoped_restore (&input_radix);
5ed8105e 12778 scoped_restore_current_pspace_and_thread restore_pspace_thread;
e62c965a 12779
8e817061
JB
12780 /* breakpoint_re_set_one sets the current_language to the language
12781 of the breakpoint it is resetting (see prepare_re_set_context)
12782 before re-evaluating the breakpoint's location. This change can
12783 unfortunately get undone by accident if the language_mode is set
12784 to auto, and we either switch frames, or more likely in this context,
12785 we select the current frame.
12786
12787 We prevent this by temporarily turning the language_mode to
12788 language_mode_manual. We restore it once all breakpoints
12789 have been reset. */
12790 scoped_restore save_language_mode = make_scoped_restore (&language_mode);
12791 language_mode = language_mode_manual;
12792
5ed8105e
PA
12793 /* Note: we must not try to insert locations until after all
12794 breakpoints have been re-set. Otherwise, e.g., when re-setting
12795 breakpoint 1, we'd insert the locations of breakpoint 2, which
12796 hadn't been re-set yet, and thus may have stale locations. */
4efc6507 12797
1428b37a 12798 for (breakpoint *b : all_breakpoints_safe ())
5ed8105e 12799 {
a70b8144 12800 try
bf469271
PA
12801 {
12802 breakpoint_re_set_one (b);
12803 }
230d2906 12804 catch (const gdb_exception &ex)
bf469271
PA
12805 {
12806 exception_fprintf (gdb_stderr, ex,
12807 "Error in re-setting breakpoint %d: ",
12808 b->number);
12809 }
5ed8105e 12810 }
5ed8105e
PA
12811
12812 jit_breakpoint_re_set ();
12813 }
6c95b8df 12814
af02033e
PP
12815 create_overlay_event_breakpoint ();
12816 create_longjmp_master_breakpoint ();
12817 create_std_terminate_master_breakpoint ();
186c406b 12818 create_exception_master_breakpoint ();
2a7f3dff
PA
12819
12820 /* Now we can insert. */
12821 update_global_location_list (UGLL_MAY_INSERT);
c906108c
SS
12822}
12823\f
c906108c
SS
12824/* Reset the thread number of this breakpoint:
12825
12826 - If the breakpoint is for all threads, leave it as-is.
4a64f543 12827 - Else, reset it to the current thread for inferior_ptid. */
c906108c 12828void
fba45db2 12829breakpoint_re_set_thread (struct breakpoint *b)
c906108c
SS
12830{
12831 if (b->thread != -1)
12832 {
00431a78 12833 b->thread = inferior_thread ()->global_num;
6c95b8df
PA
12834
12835 /* We're being called after following a fork. The new fork is
12836 selected as current, and unless this was a vfork will have a
12837 different program space from the original thread. Reset that
12838 as well. */
12839 b->loc->pspace = current_program_space;
c906108c
SS
12840 }
12841}
12842
03ac34d5
MS
12843/* Set ignore-count of breakpoint number BPTNUM to COUNT.
12844 If from_tty is nonzero, it prints a message to that effect,
12845 which ends with a period (no newline). */
12846
c906108c 12847void
fba45db2 12848set_ignore_count (int bptnum, int count, int from_tty)
c906108c 12849{
c906108c
SS
12850 if (count < 0)
12851 count = 0;
12852
43892fdf 12853 for (breakpoint *b : all_breakpoints ())
c906108c 12854 if (b->number == bptnum)
01add95b
SM
12855 {
12856 if (is_tracepoint (b))
12857 {
12858 if (from_tty && count != 0)
6cb06a8c
TT
12859 gdb_printf (_("Ignore count ignored for tracepoint %d."),
12860 bptnum);
01add95b
SM
12861 return;
12862 }
12863
12864 b->ignore_count = count;
12865 if (from_tty)
12866 {
12867 if (count == 0)
6cb06a8c
TT
12868 gdb_printf (_("Will stop next time "
12869 "breakpoint %d is reached."),
12870 bptnum);
01add95b 12871 else if (count == 1)
6cb06a8c
TT
12872 gdb_printf (_("Will ignore next crossing of breakpoint %d."),
12873 bptnum);
01add95b 12874 else
6cb06a8c
TT
12875 gdb_printf (_("Will ignore next %d "
12876 "crossings of breakpoint %d."),
12877 count, bptnum);
01add95b
SM
12878 }
12879 gdb::observers::breakpoint_modified.notify (b);
12880 return;
12881 }
c906108c 12882
8a3fe4f8 12883 error (_("No breakpoint number %d."), bptnum);
c906108c
SS
12884}
12885
c906108c
SS
12886/* Command to set ignore-count of breakpoint N to COUNT. */
12887
12888static void
0b39b52e 12889ignore_command (const char *args, int from_tty)
c906108c 12890{
0b39b52e 12891 const char *p = args;
52f0bd74 12892 int num;
c906108c
SS
12893
12894 if (p == 0)
e2e0b3e5 12895 error_no_arg (_("a breakpoint number"));
c5aa993b 12896
c906108c 12897 num = get_number (&p);
5c44784c 12898 if (num == 0)
8a3fe4f8 12899 error (_("bad breakpoint number: '%s'"), args);
c906108c 12900 if (*p == 0)
8a3fe4f8 12901 error (_("Second argument (specified ignore-count) is missing."));
c906108c
SS
12902
12903 set_ignore_count (num,
12904 longest_to_int (value_as_long (parse_and_eval (p))),
12905 from_tty);
221ea385 12906 if (from_tty)
6cb06a8c 12907 gdb_printf ("\n");
c906108c
SS
12908}
12909\f
d0fe4701
XR
12910
12911/* Call FUNCTION on each of the breakpoints with numbers in the range
12912 defined by BP_NUM_RANGE (an inclusive range). */
c906108c
SS
12913
12914static void
d0fe4701
XR
12915map_breakpoint_number_range (std::pair<int, int> bp_num_range,
12916 gdb::function_view<void (breakpoint *)> function)
c906108c 12917{
d0fe4701
XR
12918 if (bp_num_range.first == 0)
12919 {
12920 warning (_("bad breakpoint number at or near '%d'"),
12921 bp_num_range.first);
12922 }
12923 else
c906108c 12924 {
d0fe4701 12925 for (int i = bp_num_range.first; i <= bp_num_range.second; i++)
5c44784c 12926 {
d0fe4701
XR
12927 bool match = false;
12928
1428b37a 12929 for (breakpoint *b : all_breakpoints_safe ())
d0fe4701 12930 if (b->number == i)
5c44784c 12931 {
bfd28288 12932 match = true;
48649e1b 12933 function (b);
11cf8741 12934 break;
5c44784c 12935 }
bfd28288 12936 if (!match)
6cb06a8c 12937 gdb_printf (_("No breakpoint number %d.\n"), i);
c5aa993b 12938 }
c906108c
SS
12939 }
12940}
12941
d0fe4701
XR
12942/* Call FUNCTION on each of the breakpoints whose numbers are given in
12943 ARGS. */
12944
12945static void
12946map_breakpoint_numbers (const char *args,
12947 gdb::function_view<void (breakpoint *)> function)
12948{
12949 if (args == NULL || *args == '\0')
12950 error_no_arg (_("one or more breakpoint numbers"));
12951
12952 number_or_range_parser parser (args);
12953
12954 while (!parser.finished ())
12955 {
12956 int num = parser.get_number ();
12957 map_breakpoint_number_range (std::make_pair (num, num), function);
12958 }
12959}
12960
12961/* Return the breakpoint location structure corresponding to the
12962 BP_NUM and LOC_NUM values. */
12963
0d381245 12964static struct bp_location *
d0fe4701 12965find_location_by_number (int bp_num, int loc_num)
0d381245 12966{
43892fdf 12967 breakpoint *b = get_breakpoint (bp_num);
0d381245
VP
12968
12969 if (!b || b->number != bp_num)
d0fe4701 12970 error (_("Bad breakpoint number '%d'"), bp_num);
0d381245 12971
0d381245 12972 if (loc_num == 0)
d0fe4701 12973 error (_("Bad breakpoint location number '%d'"), loc_num);
0d381245 12974
d0fe4701 12975 int n = 0;
40cb8ca5 12976 for (bp_location *loc : b->locations ())
d0fe4701
XR
12977 if (++n == loc_num)
12978 return loc;
12979
12980 error (_("Bad breakpoint location number '%d'"), loc_num);
0d381245
VP
12981}
12982
95e95a6d
PA
12983/* Modes of operation for extract_bp_num. */
12984enum class extract_bp_kind
12985{
12986 /* Extracting a breakpoint number. */
12987 bp,
12988
12989 /* Extracting a location number. */
12990 loc,
12991};
12992
12993/* Extract a breakpoint or location number (as determined by KIND)
12994 from the string starting at START. TRAILER is a character which
12995 can be found after the number. If you don't want a trailer, use
12996 '\0'. If END_OUT is not NULL, it is set to point after the parsed
12997 string. This always returns a positive integer. */
12998
12999static int
13000extract_bp_num (extract_bp_kind kind, const char *start,
13001 int trailer, const char **end_out = NULL)
13002{
13003 const char *end = start;
13004 int num = get_number_trailer (&end, trailer);
13005 if (num < 0)
13006 error (kind == extract_bp_kind::bp
13007 ? _("Negative breakpoint number '%.*s'")
13008 : _("Negative breakpoint location number '%.*s'"),
13009 int (end - start), start);
13010 if (num == 0)
13011 error (kind == extract_bp_kind::bp
13012 ? _("Bad breakpoint number '%.*s'")
13013 : _("Bad breakpoint location number '%.*s'"),
13014 int (end - start), start);
13015
13016 if (end_out != NULL)
13017 *end_out = end;
13018 return num;
13019}
13020
13021/* Extract a breakpoint or location range (as determined by KIND) in
13022 the form NUM1-NUM2 stored at &ARG[arg_offset]. Returns a std::pair
13023 representing the (inclusive) range. The returned pair's elements
13024 are always positive integers. */
13025
13026static std::pair<int, int>
13027extract_bp_or_bp_range (extract_bp_kind kind,
13028 const std::string &arg,
13029 std::string::size_type arg_offset)
13030{
13031 std::pair<int, int> range;
13032 const char *bp_loc = &arg[arg_offset];
13033 std::string::size_type dash = arg.find ('-', arg_offset);
13034 if (dash != std::string::npos)
13035 {
13036 /* bp_loc is a range (x-z). */
13037 if (arg.length () == dash + 1)
13038 error (kind == extract_bp_kind::bp
13039 ? _("Bad breakpoint number at or near: '%s'")
13040 : _("Bad breakpoint location number at or near: '%s'"),
13041 bp_loc);
13042
13043 const char *end;
13044 const char *start_first = bp_loc;
13045 const char *start_second = &arg[dash + 1];
13046 range.first = extract_bp_num (kind, start_first, '-');
13047 range.second = extract_bp_num (kind, start_second, '\0', &end);
13048
13049 if (range.first > range.second)
13050 error (kind == extract_bp_kind::bp
13051 ? _("Inverted breakpoint range at '%.*s'")
13052 : _("Inverted breakpoint location range at '%.*s'"),
13053 int (end - start_first), start_first);
13054 }
13055 else
13056 {
13057 /* bp_loc is a single value. */
13058 range.first = extract_bp_num (kind, bp_loc, '\0');
13059 range.second = range.first;
13060 }
13061 return range;
13062}
13063
d0fe4701
XR
13064/* Extract the breakpoint/location range specified by ARG. Returns
13065 the breakpoint range in BP_NUM_RANGE, and the location range in
13066 BP_LOC_RANGE.
13067
13068 ARG may be in any of the following forms:
13069
13070 x where 'x' is a breakpoint number.
13071 x-y where 'x' and 'y' specify a breakpoint numbers range.
13072 x.y where 'x' is a breakpoint number and 'y' a location number.
13073 x.y-z where 'x' is a breakpoint number and 'y' and 'z' specify a
13074 location number range.
13075*/
13076
cc638e86 13077static void
d0fe4701
XR
13078extract_bp_number_and_location (const std::string &arg,
13079 std::pair<int, int> &bp_num_range,
13080 std::pair<int, int> &bp_loc_range)
13081{
13082 std::string::size_type dot = arg.find ('.');
13083
13084 if (dot != std::string::npos)
13085 {
13086 /* Handle 'x.y' and 'x.y-z' cases. */
13087
13088 if (arg.length () == dot + 1 || dot == 0)
95e95a6d 13089 error (_("Bad breakpoint number at or near: '%s'"), arg.c_str ());
d0fe4701 13090
95e95a6d
PA
13091 bp_num_range.first
13092 = extract_bp_num (extract_bp_kind::bp, arg.c_str (), '.');
13093 bp_num_range.second = bp_num_range.first;
d0fe4701 13094
95e95a6d
PA
13095 bp_loc_range = extract_bp_or_bp_range (extract_bp_kind::loc,
13096 arg, dot + 1);
d0fe4701
XR
13097 }
13098 else
13099 {
13100 /* Handle x and x-y cases. */
d0fe4701 13101
95e95a6d 13102 bp_num_range = extract_bp_or_bp_range (extract_bp_kind::bp, arg, 0);
d0fe4701
XR
13103 bp_loc_range.first = 0;
13104 bp_loc_range.second = 0;
13105 }
d0fe4701
XR
13106}
13107
13108/* Enable or disable a breakpoint location BP_NUM.LOC_NUM. ENABLE
13109 specifies whether to enable or disable. */
13110
13111static void
13112enable_disable_bp_num_loc (int bp_num, int loc_num, bool enable)
13113{
13114 struct bp_location *loc = find_location_by_number (bp_num, loc_num);
13115 if (loc != NULL)
13116 {
b5fa468f
TBA
13117 if (loc->disabled_by_cond && enable)
13118 error (_("Breakpoint %d's condition is invalid at location %d, "
13119 "cannot enable."), bp_num, loc_num);
13120
d0fe4701
XR
13121 if (loc->enabled != enable)
13122 {
13123 loc->enabled = enable;
13124 mark_breakpoint_location_modified (loc);
13125 }
13126 if (target_supports_enable_disable_tracepoint ()
13127 && current_trace_status ()->running && loc->owner
13128 && is_tracepoint (loc->owner))
13129 target_disable_tracepoint (loc);
13130 }
13131 update_global_location_list (UGLL_DONT_INSERT);
d7154a8d
JV
13132
13133 gdb::observers::breakpoint_modified.notify (loc->owner);
d0fe4701
XR
13134}
13135
13136/* Enable or disable a range of breakpoint locations. BP_NUM is the
13137 number of the breakpoint, and BP_LOC_RANGE specifies the
13138 (inclusive) range of location numbers of that breakpoint to
13139 enable/disable. ENABLE specifies whether to enable or disable the
13140 location. */
13141
13142static void
13143enable_disable_breakpoint_location_range (int bp_num,
13144 std::pair<int, int> &bp_loc_range,
13145 bool enable)
13146{
13147 for (int i = bp_loc_range.first; i <= bp_loc_range.second; i++)
13148 enable_disable_bp_num_loc (bp_num, i, enable);
13149}
0d381245 13150
1900040c
MS
13151/* Set ignore-count of breakpoint number BPTNUM to COUNT.
13152 If from_tty is nonzero, it prints a message to that effect,
13153 which ends with a period (no newline). */
13154
c906108c 13155void
fba45db2 13156disable_breakpoint (struct breakpoint *bpt)
c906108c
SS
13157{
13158 /* Never disable a watchpoint scope breakpoint; we want to
13159 hit them when we leave scope so we can delete both the
13160 watchpoint and its scope breakpoint at that time. */
13161 if (bpt->type == bp_watchpoint_scope)
13162 return;
13163
b5de0fa7 13164 bpt->enable_state = bp_disabled;
c906108c 13165
b775012e
LM
13166 /* Mark breakpoint locations modified. */
13167 mark_breakpoint_modified (bpt);
13168
d248b706
KY
13169 if (target_supports_enable_disable_tracepoint ()
13170 && current_trace_status ()->running && is_tracepoint (bpt))
13171 {
40cb8ca5 13172 for (bp_location *location : bpt->locations ())
d248b706
KY
13173 target_disable_tracepoint (location);
13174 }
13175
44702360 13176 update_global_location_list (UGLL_DONT_INSERT);
c906108c 13177
76727919 13178 gdb::observers::breakpoint_modified.notify (bpt);
c906108c
SS
13179}
13180
d0fe4701
XR
13181/* Enable or disable the breakpoint(s) or breakpoint location(s)
13182 specified in ARGS. ARGS may be in any of the formats handled by
13183 extract_bp_number_and_location. ENABLE specifies whether to enable
13184 or disable the breakpoints/locations. */
13185
c906108c 13186static void
d0fe4701 13187enable_disable_command (const char *args, int from_tty, bool enable)
c906108c 13188{
c906108c 13189 if (args == 0)
46c6471b 13190 {
43892fdf 13191 for (breakpoint *bpt : all_breakpoints ())
46c6471b 13192 if (user_breakpoint_p (bpt))
d0fe4701
XR
13193 {
13194 if (enable)
13195 enable_breakpoint (bpt);
13196 else
13197 disable_breakpoint (bpt);
13198 }
46c6471b 13199 }
9eaabc75 13200 else
0d381245 13201 {
cb791d59 13202 std::string num = extract_arg (&args);
9eaabc75 13203
cb791d59 13204 while (!num.empty ())
d248b706 13205 {
d0fe4701 13206 std::pair<int, int> bp_num_range, bp_loc_range;
9eaabc75 13207
cc638e86
PA
13208 extract_bp_number_and_location (num, bp_num_range, bp_loc_range);
13209
13210 if (bp_loc_range.first == bp_loc_range.second
13211 && bp_loc_range.first == 0)
d0fe4701 13212 {
cc638e86
PA
13213 /* Handle breakpoint ids with formats 'x' or 'x-z'. */
13214 map_breakpoint_number_range (bp_num_range,
13215 enable
13216 ? enable_breakpoint
13217 : disable_breakpoint);
13218 }
13219 else
13220 {
13221 /* Handle breakpoint ids with formats 'x.y' or
13222 'x.y-z'. */
13223 enable_disable_breakpoint_location_range
13224 (bp_num_range.first, bp_loc_range, enable);
b775012e 13225 }
9eaabc75 13226 num = extract_arg (&args);
d248b706 13227 }
0d381245 13228 }
c906108c
SS
13229}
13230
d0fe4701
XR
13231/* The disable command disables the specified breakpoints/locations
13232 (or all defined breakpoints) so they're no longer effective in
13233 stopping the inferior. ARGS may be in any of the forms defined in
13234 extract_bp_number_and_location. */
13235
13236static void
13237disable_command (const char *args, int from_tty)
13238{
13239 enable_disable_command (args, from_tty, false);
13240}
13241
c906108c 13242static void
816338b5
SS
13243enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
13244 int count)
c906108c 13245{
afe38095 13246 int target_resources_ok;
c906108c
SS
13247
13248 if (bpt->type == bp_hardware_breakpoint)
13249 {
13250 int i;
c5aa993b 13251 i = hw_breakpoint_used_count ();
53a5351d 13252 target_resources_ok =
d92524f1 13253 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
53a5351d 13254 i + 1, 0);
c906108c 13255 if (target_resources_ok == 0)
8a3fe4f8 13256 error (_("No hardware breakpoint support in the target."));
c906108c 13257 else if (target_resources_ok < 0)
8a3fe4f8 13258 error (_("Hardware breakpoints used exceeds limit."));
c906108c
SS
13259 }
13260
cc60f2e3 13261 if (is_watchpoint (bpt))
c906108c 13262 {
d07205c2 13263 /* Initialize it just to avoid a GCC false warning. */
f486487f 13264 enum enable_state orig_enable_state = bp_disabled;
dde02812 13265
a70b8144 13266 try
c906108c 13267 {
3a5c3e22
PA
13268 struct watchpoint *w = (struct watchpoint *) bpt;
13269
1e718ff1
TJB
13270 orig_enable_state = bpt->enable_state;
13271 bpt->enable_state = bp_enabled;
3a5c3e22 13272 update_watchpoint (w, 1 /* reparse */);
c906108c 13273 }
230d2906 13274 catch (const gdb_exception &e)
c5aa993b 13275 {
1e718ff1 13276 bpt->enable_state = orig_enable_state;
dde02812
ES
13277 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
13278 bpt->number);
13279 return;
c5aa993b 13280 }
c906108c 13281 }
0101ce28 13282
b775012e
LM
13283 bpt->enable_state = bp_enabled;
13284
13285 /* Mark breakpoint locations modified. */
13286 mark_breakpoint_modified (bpt);
13287
d248b706
KY
13288 if (target_supports_enable_disable_tracepoint ()
13289 && current_trace_status ()->running && is_tracepoint (bpt))
13290 {
40cb8ca5 13291 for (bp_location *location : bpt->locations ())
d248b706
KY
13292 target_enable_tracepoint (location);
13293 }
13294
b4c291bb 13295 bpt->disposition = disposition;
816338b5 13296 bpt->enable_count = count;
44702360 13297 update_global_location_list (UGLL_MAY_INSERT);
9c97429f 13298
76727919 13299 gdb::observers::breakpoint_modified.notify (bpt);
c906108c
SS
13300}
13301
fe3f5fa8 13302
c906108c 13303void
fba45db2 13304enable_breakpoint (struct breakpoint *bpt)
c906108c 13305{
816338b5 13306 enable_breakpoint_disp (bpt, bpt->disposition, 0);
51be5b68
PA
13307}
13308
d0fe4701
XR
13309/* The enable command enables the specified breakpoints/locations (or
13310 all defined breakpoints) so they once again become (or continue to
13311 be) effective in stopping the inferior. ARGS may be in any of the
13312 forms defined in extract_bp_number_and_location. */
c906108c 13313
c906108c 13314static void
981a3fb3 13315enable_command (const char *args, int from_tty)
c906108c 13316{
d0fe4701 13317 enable_disable_command (args, from_tty, true);
c906108c
SS
13318}
13319
c906108c 13320static void
4495129a 13321enable_once_command (const char *args, int from_tty)
c906108c 13322{
48649e1b
TT
13323 map_breakpoint_numbers
13324 (args, [&] (breakpoint *b)
13325 {
13326 iterate_over_related_breakpoints
13327 (b, [&] (breakpoint *bpt)
13328 {
13329 enable_breakpoint_disp (bpt, disp_disable, 1);
13330 });
13331 });
816338b5
SS
13332}
13333
13334static void
4495129a 13335enable_count_command (const char *args, int from_tty)
816338b5 13336{
b9d61307
SM
13337 int count;
13338
13339 if (args == NULL)
13340 error_no_arg (_("hit count"));
13341
13342 count = get_number (&args);
816338b5 13343
48649e1b
TT
13344 map_breakpoint_numbers
13345 (args, [&] (breakpoint *b)
13346 {
13347 iterate_over_related_breakpoints
13348 (b, [&] (breakpoint *bpt)
13349 {
13350 enable_breakpoint_disp (bpt, disp_disable, count);
13351 });
13352 });
c906108c
SS
13353}
13354
c906108c 13355static void
4495129a 13356enable_delete_command (const char *args, int from_tty)
c906108c 13357{
48649e1b
TT
13358 map_breakpoint_numbers
13359 (args, [&] (breakpoint *b)
13360 {
13361 iterate_over_related_breakpoints
13362 (b, [&] (breakpoint *bpt)
13363 {
13364 enable_breakpoint_disp (bpt, disp_del, 1);
13365 });
13366 });
c906108c
SS
13367}
13368\f
1f3b5d1b
PP
13369/* Invalidate last known value of any hardware watchpoint if
13370 the memory which that value represents has been written to by
13371 GDB itself. */
13372
13373static void
8de0566d
YQ
13374invalidate_bp_value_on_memory_change (struct inferior *inferior,
13375 CORE_ADDR addr, ssize_t len,
1f3b5d1b
PP
13376 const bfd_byte *data)
13377{
43892fdf 13378 for (breakpoint *bp : all_breakpoints ())
1f3b5d1b 13379 if (bp->enable_state == bp_enabled
3a5c3e22 13380 && bp->type == bp_hardware_watchpoint)
1f3b5d1b 13381 {
3a5c3e22 13382 struct watchpoint *wp = (struct watchpoint *) bp;
1f3b5d1b 13383
850645cf 13384 if (wp->val_valid && wp->val != nullptr)
3a5c3e22 13385 {
40cb8ca5 13386 for (bp_location *loc : bp->locations ())
3a5c3e22
PA
13387 if (loc->loc_type == bp_loc_hardware_watchpoint
13388 && loc->address + loc->length > addr
13389 && addr + len > loc->address)
13390 {
3a5c3e22 13391 wp->val = NULL;
4c1d86d9 13392 wp->val_valid = false;
3a5c3e22
PA
13393 }
13394 }
1f3b5d1b
PP
13395 }
13396}
13397
8181d85f
DJ
13398/* Create and insert a breakpoint for software single step. */
13399
13400void
6c95b8df 13401insert_single_step_breakpoint (struct gdbarch *gdbarch,
accd0bcd 13402 const address_space *aspace,
4a64f543 13403 CORE_ADDR next_pc)
8181d85f 13404{
7c16b83e
PA
13405 struct thread_info *tp = inferior_thread ();
13406 struct symtab_and_line sal;
13407 CORE_ADDR pc = next_pc;
8181d85f 13408
34b7e8a6
PA
13409 if (tp->control.single_step_breakpoints == NULL)
13410 {
13411 tp->control.single_step_breakpoints
5d5658a1 13412 = new_single_step_breakpoint (tp->global_num, gdbarch);
34b7e8a6 13413 }
8181d85f 13414
7c16b83e
PA
13415 sal = find_pc_line (pc, 0);
13416 sal.pc = pc;
13417 sal.section = find_pc_overlay (pc);
13418 sal.explicit_pc = 1;
06615628 13419 tp->control.single_step_breakpoints->add_location (sal);
8181d85f 13420
7c16b83e 13421 update_global_location_list (UGLL_INSERT);
8181d85f
DJ
13422}
13423
93f9a11f
YQ
13424/* Insert single step breakpoints according to the current state. */
13425
13426int
13427insert_single_step_breakpoints (struct gdbarch *gdbarch)
13428{
f5ea389a 13429 struct regcache *regcache = get_current_regcache ();
a0ff9e1a 13430 std::vector<CORE_ADDR> next_pcs;
93f9a11f 13431
f5ea389a 13432 next_pcs = gdbarch_software_single_step (gdbarch, regcache);
93f9a11f 13433
a0ff9e1a 13434 if (!next_pcs.empty ())
93f9a11f 13435 {
f5ea389a 13436 struct frame_info *frame = get_current_frame ();
8b86c959 13437 const address_space *aspace = get_frame_address_space (frame);
93f9a11f 13438
a0ff9e1a 13439 for (CORE_ADDR pc : next_pcs)
93f9a11f
YQ
13440 insert_single_step_breakpoint (gdbarch, aspace, pc);
13441
93f9a11f
YQ
13442 return 1;
13443 }
13444 else
13445 return 0;
13446}
13447
34b7e8a6 13448/* See breakpoint.h. */
f02253f1
HZ
13449
13450int
7c16b83e 13451breakpoint_has_location_inserted_here (struct breakpoint *bp,
accd0bcd 13452 const address_space *aspace,
7c16b83e 13453 CORE_ADDR pc)
1aafd4da 13454{
40cb8ca5 13455 for (bp_location *loc : bp->locations ())
7c16b83e
PA
13456 if (loc->inserted
13457 && breakpoint_location_address_match (loc, aspace, pc))
13458 return 1;
1aafd4da 13459
7c16b83e 13460 return 0;
ef370185
JB
13461}
13462
13463/* Check whether a software single-step breakpoint is inserted at
13464 PC. */
13465
13466int
accd0bcd 13467single_step_breakpoint_inserted_here_p (const address_space *aspace,
ef370185
JB
13468 CORE_ADDR pc)
13469{
43892fdf 13470 for (breakpoint *bpt : all_breakpoints ())
34b7e8a6
PA
13471 {
13472 if (bpt->type == bp_single_step
13473 && breakpoint_has_location_inserted_here (bpt, aspace, pc))
13474 return 1;
13475 }
13476 return 0;
1aafd4da
UW
13477}
13478
1042e4c0
SS
13479/* Tracepoint-specific operations. */
13480
13481/* Set tracepoint count to NUM. */
13482static void
13483set_tracepoint_count (int num)
13484{
13485 tracepoint_count = num;
4fa62494 13486 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
1042e4c0
SS
13487}
13488
70221824 13489static void
0b39b52e 13490trace_command (const char *arg, int from_tty)
1042e4c0 13491{
ffc2605c
TT
13492 event_location_up location = string_to_event_location (&arg,
13493 current_language);
bac7c5cf
GB
13494 const struct breakpoint_ops *ops = breakpoint_ops_for_event_location
13495 (location.get (), true /* is_tracepoint */);
55aa24fb 13496
558a9d82 13497 create_breakpoint (get_current_arch (),
ffc2605c 13498 location.get (),
10a636cc 13499 NULL, 0, arg, false, 1 /* parse arg */,
558a9d82
YQ
13500 0 /* tempflag */,
13501 bp_tracepoint /* type_wanted */,
13502 0 /* Ignore count */,
13503 pending_break_support,
13504 ops,
13505 from_tty,
13506 1 /* enabled */,
13507 0 /* internal */, 0);
1042e4c0
SS
13508}
13509
70221824 13510static void
0b39b52e 13511ftrace_command (const char *arg, int from_tty)
7a697b8d 13512{
ffc2605c
TT
13513 event_location_up location = string_to_event_location (&arg,
13514 current_language);
558a9d82 13515 create_breakpoint (get_current_arch (),
ffc2605c 13516 location.get (),
10a636cc 13517 NULL, 0, arg, false, 1 /* parse arg */,
558a9d82
YQ
13518 0 /* tempflag */,
13519 bp_fast_tracepoint /* type_wanted */,
13520 0 /* Ignore count */,
13521 pending_break_support,
a0cd5f08 13522 &base_breakpoint_ops,
558a9d82
YQ
13523 from_tty,
13524 1 /* enabled */,
13525 0 /* internal */, 0);
0fb4aa4b
PA
13526}
13527
13528/* strace command implementation. Creates a static tracepoint. */
13529
70221824 13530static void
0b39b52e 13531strace_command (const char *arg, int from_tty)
0fb4aa4b 13532{
a678887d 13533 const struct breakpoint_ops *ops;
ffc2605c 13534 event_location_up location;
7b572efb 13535 enum bptype type;
983af33b
SDJ
13536
13537 /* Decide if we are dealing with a static tracepoint marker (`-m'),
13538 or with a normal static tracepoint. */
61012eef 13539 if (arg && startswith (arg, "-m") && isspace (arg[2]))
f00aae0f
KS
13540 {
13541 ops = &strace_marker_breakpoint_ops;
a20714ff 13542 location = new_linespec_location (&arg, symbol_name_match_type::FULL);
7b572efb 13543 type = bp_static_marker_tracepoint;
f00aae0f 13544 }
983af33b 13545 else
f00aae0f 13546 {
a0cd5f08 13547 ops = &base_breakpoint_ops;
f00aae0f 13548 location = string_to_event_location (&arg, current_language);
7b572efb 13549 type = bp_static_tracepoint;
f00aae0f 13550 }
983af33b 13551
558a9d82 13552 create_breakpoint (get_current_arch (),
ffc2605c 13553 location.get (),
10a636cc 13554 NULL, 0, arg, false, 1 /* parse arg */,
558a9d82 13555 0 /* tempflag */,
7b572efb 13556 type /* type_wanted */,
558a9d82
YQ
13557 0 /* Ignore count */,
13558 pending_break_support,
13559 ops,
13560 from_tty,
13561 1 /* enabled */,
13562 0 /* internal */, 0);
7a697b8d
SS
13563}
13564
409873ef
SS
13565/* Set up a fake reader function that gets command lines from a linked
13566 list that was acquired during tracepoint uploading. */
13567
13568static struct uploaded_tp *this_utp;
3149d8c1 13569static int next_cmd;
409873ef
SS
13570
13571static char *
13572read_uploaded_action (void)
13573{
a18ba4e4 13574 char *rslt = nullptr;
409873ef 13575
a18ba4e4
SM
13576 if (next_cmd < this_utp->cmd_strings.size ())
13577 {
67aa1f3c 13578 rslt = this_utp->cmd_strings[next_cmd].get ();
a18ba4e4
SM
13579 next_cmd++;
13580 }
409873ef
SS
13581
13582 return rslt;
13583}
13584
00bf0b85
SS
13585/* Given information about a tracepoint as recorded on a target (which
13586 can be either a live system or a trace file), attempt to create an
13587 equivalent GDB tracepoint. This is not a reliable process, since
13588 the target does not necessarily have all the information used when
13589 the tracepoint was originally defined. */
13590
d9b3f62e 13591struct tracepoint *
00bf0b85 13592create_tracepoint_from_upload (struct uploaded_tp *utp)
d5551862 13593{
f2fc3015
TT
13594 const char *addr_str;
13595 char small_buf[100];
d9b3f62e 13596 struct tracepoint *tp;
fd9b8c24 13597
409873ef 13598 if (utp->at_string)
67aa1f3c 13599 addr_str = utp->at_string.get ();
409873ef
SS
13600 else
13601 {
13602 /* In the absence of a source location, fall back to raw
13603 address. Since there is no way to confirm that the address
13604 means the same thing as when the trace was started, warn the
13605 user. */
3e43a32a
MS
13606 warning (_("Uploaded tracepoint %d has no "
13607 "source location, using raw address"),
409873ef 13608 utp->number);
8c042590 13609 xsnprintf (small_buf, sizeof (small_buf), "*%s", hex_string (utp->addr));
409873ef
SS
13610 addr_str = small_buf;
13611 }
13612
13613 /* There's not much we can do with a sequence of bytecodes. */
13614 if (utp->cond && !utp->cond_string)
3e43a32a
MS
13615 warning (_("Uploaded tracepoint %d condition "
13616 "has no source form, ignoring it"),
409873ef 13617 utp->number);
d5551862 13618
ffc2605c
TT
13619 event_location_up location = string_to_event_location (&addr_str,
13620 current_language);
8cdf0e15 13621 if (!create_breakpoint (get_current_arch (),
ffc2605c 13622 location.get (),
67aa1f3c 13623 utp->cond_string.get (), -1, addr_str,
10a636cc 13624 false /* force_condition */,
e7e0cddf 13625 0 /* parse cond/thread */,
8cdf0e15 13626 0 /* tempflag */,
0fb4aa4b 13627 utp->type /* type_wanted */,
8cdf0e15
VP
13628 0 /* Ignore count */,
13629 pending_break_support,
a0cd5f08 13630 &base_breakpoint_ops,
8cdf0e15 13631 0 /* from_tty */,
84f4c1fe 13632 utp->enabled /* enabled */,
44f238bb
PA
13633 0 /* internal */,
13634 CREATE_BREAKPOINT_FLAGS_INSERTED))
ffc2605c 13635 return NULL;
fd9b8c24 13636
409873ef 13637 /* Get the tracepoint we just created. */
fd9b8c24
PA
13638 tp = get_tracepoint (tracepoint_count);
13639 gdb_assert (tp != NULL);
d5551862 13640
00bf0b85
SS
13641 if (utp->pass > 0)
13642 {
8c042590 13643 xsnprintf (small_buf, sizeof (small_buf), "%d %d", utp->pass,
c1fc2657 13644 tp->number);
00bf0b85 13645
409873ef 13646 trace_pass_command (small_buf, 0);
00bf0b85
SS
13647 }
13648
409873ef
SS
13649 /* If we have uploaded versions of the original commands, set up a
13650 special-purpose "reader" function and call the usual command line
13651 reader, then pass the result to the breakpoint command-setting
13652 function. */
a18ba4e4 13653 if (!utp->cmd_strings.empty ())
00bf0b85 13654 {
12973681 13655 counted_command_line cmd_list;
00bf0b85 13656
409873ef 13657 this_utp = utp;
3149d8c1 13658 next_cmd = 0;
d5551862 13659
60b3cef2 13660 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL);
409873ef 13661
c1fc2657 13662 breakpoint_set_commands (tp, std::move (cmd_list));
00bf0b85 13663 }
a18ba4e4
SM
13664 else if (!utp->actions.empty ()
13665 || !utp->step_actions.empty ())
3e43a32a
MS
13666 warning (_("Uploaded tracepoint %d actions "
13667 "have no source form, ignoring them"),
409873ef 13668 utp->number);
00bf0b85 13669
f196051f 13670 /* Copy any status information that might be available. */
c1fc2657 13671 tp->hit_count = utp->hit_count;
f196051f
SS
13672 tp->traceframe_usage = utp->traceframe_usage;
13673
00bf0b85 13674 return tp;
d9b3f62e 13675}
00bf0b85 13676
1042e4c0
SS
13677/* Print information on tracepoint number TPNUM_EXP, or all if
13678 omitted. */
13679
13680static void
1d12d88f 13681info_tracepoints_command (const char *args, int from_tty)
1042e4c0 13682{
79a45e25 13683 struct ui_out *uiout = current_uiout;
e5a67952 13684 int num_printed;
1042e4c0 13685
5c458ae8 13686 num_printed = breakpoint_1 (args, false, is_tracepoint);
d77f58be
SS
13687
13688 if (num_printed == 0)
1042e4c0 13689 {
e5a67952 13690 if (args == NULL || *args == '\0')
112e8700 13691 uiout->message ("No tracepoints.\n");
d77f58be 13692 else
112e8700 13693 uiout->message ("No tracepoint matching '%s'.\n", args);
1042e4c0 13694 }
ad443146
SS
13695
13696 default_collect_info ();
1042e4c0
SS
13697}
13698
4a64f543 13699/* The 'enable trace' command enables tracepoints.
1042e4c0
SS
13700 Not supported by all targets. */
13701static void
5fed81ff 13702enable_trace_command (const char *args, int from_tty)
1042e4c0
SS
13703{
13704 enable_command (args, from_tty);
13705}
13706
4a64f543 13707/* The 'disable trace' command disables tracepoints.
1042e4c0
SS
13708 Not supported by all targets. */
13709static void
5fed81ff 13710disable_trace_command (const char *args, int from_tty)
1042e4c0
SS
13711{
13712 disable_command (args, from_tty);
13713}
13714
4a64f543 13715/* Remove a tracepoint (or all if no argument). */
1042e4c0 13716static void
4495129a 13717delete_trace_command (const char *arg, int from_tty)
1042e4c0 13718{
1042e4c0
SS
13719 dont_repeat ();
13720
13721 if (arg == 0)
13722 {
13723 int breaks_to_delete = 0;
13724
13725 /* Delete all breakpoints if no argument.
dda83cd7
SM
13726 Do not delete internal or call-dummy breakpoints, these
13727 have to be deleted with an explicit breakpoint number
4a64f543 13728 argument. */
f6d17b2b 13729 for (breakpoint *tp : all_tracepoints ())
1428b37a 13730 if (is_tracepoint (tp) && user_breakpoint_p (tp))
1042e4c0
SS
13731 {
13732 breaks_to_delete = 1;
13733 break;
13734 }
1042e4c0
SS
13735
13736 /* Ask user only if there are some breakpoints to delete. */
13737 if (!from_tty
13738 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
13739 {
1428b37a 13740 for (breakpoint *b : all_breakpoints_safe ())
46c6471b 13741 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0 13742 delete_breakpoint (b);
1042e4c0
SS
13743 }
13744 }
13745 else
48649e1b 13746 map_breakpoint_numbers
b926417a 13747 (arg, [&] (breakpoint *br)
48649e1b 13748 {
b926417a 13749 iterate_over_related_breakpoints (br, delete_breakpoint);
48649e1b 13750 });
1042e4c0
SS
13751}
13752
197f0a60
TT
13753/* Helper function for trace_pass_command. */
13754
13755static void
d9b3f62e 13756trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
197f0a60 13757{
d9b3f62e 13758 tp->pass_count = count;
76727919 13759 gdb::observers::breakpoint_modified.notify (tp);
197f0a60 13760 if (from_tty)
6cb06a8c
TT
13761 gdb_printf (_("Setting tracepoint %d's passcount to %d\n"),
13762 tp->number, count);
197f0a60
TT
13763}
13764
1042e4c0
SS
13765/* Set passcount for tracepoint.
13766
13767 First command argument is passcount, second is tracepoint number.
13768 If tracepoint number omitted, apply to most recently defined.
13769 Also accepts special argument "all". */
13770
13771static void
0b39b52e 13772trace_pass_command (const char *args, int from_tty)
1042e4c0 13773{
d9b3f62e 13774 struct tracepoint *t1;
0b39b52e 13775 ULONGEST count;
1042e4c0
SS
13776
13777 if (args == 0 || *args == 0)
3e43a32a
MS
13778 error (_("passcount command requires an "
13779 "argument (count + optional TP num)"));
1042e4c0 13780
0b39b52e 13781 count = strtoulst (args, &args, 10); /* Count comes first, then TP num. */
1042e4c0 13782
529480d0 13783 args = skip_spaces (args);
1042e4c0
SS
13784 if (*args && strncasecmp (args, "all", 3) == 0)
13785 {
13786 args += 3; /* Skip special argument "all". */
1042e4c0
SS
13787 if (*args)
13788 error (_("Junk at end of arguments."));
1042e4c0 13789
f6d17b2b 13790 for (breakpoint *b : all_tracepoints ())
01add95b
SM
13791 {
13792 t1 = (struct tracepoint *) b;
13793 trace_pass_set_count (t1, count, from_tty);
13794 }
197f0a60
TT
13795 }
13796 else if (*args == '\0')
1042e4c0 13797 {
5fa1d40e 13798 t1 = get_tracepoint_by_number (&args, NULL);
1042e4c0 13799 if (t1)
197f0a60
TT
13800 trace_pass_set_count (t1, count, from_tty);
13801 }
13802 else
13803 {
bfd28288
PA
13804 number_or_range_parser parser (args);
13805 while (!parser.finished ())
1042e4c0 13806 {
bfd28288 13807 t1 = get_tracepoint_by_number (&args, &parser);
197f0a60
TT
13808 if (t1)
13809 trace_pass_set_count (t1, count, from_tty);
1042e4c0
SS
13810 }
13811 }
1042e4c0
SS
13812}
13813
d9b3f62e 13814struct tracepoint *
1042e4c0
SS
13815get_tracepoint (int num)
13816{
f6d17b2b 13817 for (breakpoint *t : all_tracepoints ())
1042e4c0 13818 if (t->number == num)
d9b3f62e 13819 return (struct tracepoint *) t;
1042e4c0
SS
13820
13821 return NULL;
13822}
13823
d5551862
SS
13824/* Find the tracepoint with the given target-side number (which may be
13825 different from the tracepoint number after disconnecting and
13826 reconnecting). */
13827
d9b3f62e 13828struct tracepoint *
d5551862
SS
13829get_tracepoint_by_number_on_target (int num)
13830{
f6d17b2b 13831 for (breakpoint *b : all_tracepoints ())
d9b3f62e
PA
13832 {
13833 struct tracepoint *t = (struct tracepoint *) b;
13834
13835 if (t->number_on_target == num)
13836 return t;
13837 }
d5551862
SS
13838
13839 return NULL;
13840}
13841
1042e4c0 13842/* Utility: parse a tracepoint number and look it up in the list.
197f0a60 13843 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
5fa1d40e
YQ
13844 If the argument is missing, the most recent tracepoint
13845 (tracepoint_count) is returned. */
13846
d9b3f62e 13847struct tracepoint *
0b39b52e 13848get_tracepoint_by_number (const char **arg,
bfd28288 13849 number_or_range_parser *parser)
1042e4c0 13850{
1042e4c0 13851 int tpnum;
0b39b52e 13852 const char *instring = arg == NULL ? NULL : *arg;
1042e4c0 13853
bfd28288 13854 if (parser != NULL)
197f0a60 13855 {
bfd28288
PA
13856 gdb_assert (!parser->finished ());
13857 tpnum = parser->get_number ();
197f0a60
TT
13858 }
13859 else if (arg == NULL || *arg == NULL || ! **arg)
5fa1d40e 13860 tpnum = tracepoint_count;
1042e4c0 13861 else
197f0a60 13862 tpnum = get_number (arg);
1042e4c0
SS
13863
13864 if (tpnum <= 0)
13865 {
13866 if (instring && *instring)
6cb06a8c
TT
13867 gdb_printf (_("bad tracepoint number at or near '%s'\n"),
13868 instring);
1042e4c0 13869 else
6cb06a8c 13870 gdb_printf (_("No previous tracepoint\n"));
1042e4c0
SS
13871 return NULL;
13872 }
13873
f6d17b2b 13874 for (breakpoint *t : all_tracepoints ())
1042e4c0 13875 if (t->number == tpnum)
d9b3f62e 13876 return (struct tracepoint *) t;
1042e4c0 13877
6cb06a8c 13878 gdb_printf ("No tracepoint number %d.\n", tpnum);
1042e4c0
SS
13879 return NULL;
13880}
13881
d9b3f62e 13882void
04d0163c 13883breakpoint::print_recreate_thread (struct ui_file *fp) const
d9b3f62e 13884{
04d0163c
TT
13885 if (thread != -1)
13886 gdb_printf (fp, " thread %d", thread);
d9b3f62e 13887
04d0163c
TT
13888 if (task != 0)
13889 gdb_printf (fp, " task %d", task);
d9b3f62e 13890
6cb06a8c 13891 gdb_printf (fp, "\n");
d9b3f62e
PA
13892}
13893
6149aea9
PA
13894/* Save information on user settable breakpoints (watchpoints, etc) to
13895 a new script file named FILENAME. If FILTER is non-NULL, call it
13896 on each breakpoint and only include the ones for which it returns
f2478a7e 13897 true. */
6149aea9 13898
1042e4c0 13899static void
4495129a 13900save_breakpoints (const char *filename, int from_tty,
f2478a7e 13901 bool (*filter) (const struct breakpoint *))
1042e4c0 13902{
6149aea9 13903 int any = 0;
6149aea9 13904 int extra_trace_bits = 0;
1042e4c0 13905
6149aea9
PA
13906 if (filename == 0 || *filename == 0)
13907 error (_("Argument required (file name in which to save)"));
1042e4c0
SS
13908
13909 /* See if we have anything to save. */
43892fdf 13910 for (breakpoint *tp : all_breakpoints ())
01add95b
SM
13911 {
13912 /* Skip internal and momentary breakpoints. */
13913 if (!user_breakpoint_p (tp))
13914 continue;
6149aea9 13915
01add95b
SM
13916 /* If we have a filter, only save the breakpoints it accepts. */
13917 if (filter && !filter (tp))
13918 continue;
6149aea9 13919
01add95b 13920 any = 1;
6149aea9 13921
01add95b
SM
13922 if (is_tracepoint (tp))
13923 {
13924 extra_trace_bits = 1;
6149aea9 13925
01add95b
SM
13926 /* We can stop searching. */
13927 break;
13928 }
13929 }
6149aea9
PA
13930
13931 if (!any)
1042e4c0 13932 {
6149aea9 13933 warning (_("Nothing to save."));
1042e4c0
SS
13934 return;
13935 }
13936
ee0c3293 13937 gdb::unique_xmalloc_ptr<char> expanded_filename (tilde_expand (filename));
d7e74731
PA
13938
13939 stdio_file fp;
13940
ee0c3293 13941 if (!fp.open (expanded_filename.get (), "w"))
6149aea9 13942 error (_("Unable to open file '%s' for saving (%s)"),
ee0c3293 13943 expanded_filename.get (), safe_strerror (errno));
8bf6485c 13944
6149aea9 13945 if (extra_trace_bits)
d7e74731 13946 save_trace_state_variables (&fp);
8bf6485c 13947
43892fdf 13948 for (breakpoint *tp : all_breakpoints ())
01add95b
SM
13949 {
13950 /* Skip internal and momentary breakpoints. */
13951 if (!user_breakpoint_p (tp))
13952 continue;
8bf6485c 13953
01add95b
SM
13954 /* If we have a filter, only save the breakpoints it accepts. */
13955 if (filter && !filter (tp))
13956 continue;
6149aea9 13957
c47614fe 13958 tp->print_recreate (&fp);
1042e4c0 13959
01add95b
SM
13960 /* Note, we can't rely on tp->number for anything, as we can't
13961 assume the recreated breakpoint numbers will match. Use $bpnum
13962 instead. */
6149aea9 13963
01add95b 13964 if (tp->cond_string)
6f781ee3 13965 fp.printf (" condition $bpnum %s\n", tp->cond_string.get ());
6149aea9 13966
01add95b
SM
13967 if (tp->ignore_count)
13968 fp.printf (" ignore $bpnum %d\n", tp->ignore_count);
6149aea9 13969
01add95b
SM
13970 if (tp->type != bp_dprintf && tp->commands)
13971 {
13972 fp.puts (" commands\n");
1042e4c0 13973
01add95b
SM
13974 current_uiout->redirect (&fp);
13975 try
13976 {
13977 print_command_lines (current_uiout, tp->commands.get (), 2);
13978 }
13979 catch (const gdb_exception &ex)
13980 {
13981 current_uiout->redirect (NULL);
13982 throw;
13983 }
6149aea9 13984
01add95b
SM
13985 current_uiout->redirect (NULL);
13986 fp.puts (" end\n");
13987 }
6149aea9 13988
01add95b
SM
13989 if (tp->enable_state == bp_disabled)
13990 fp.puts ("disable $bpnum\n");
6149aea9 13991
01add95b
SM
13992 /* If this is a multi-location breakpoint, check if the locations
13993 should be individually disabled. Watchpoint locations are
13994 special, and not user visible. */
13995 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
13996 {
13997 int n = 1;
40cb8ca5 13998
01add95b
SM
13999 for (bp_location *loc : tp->locations ())
14000 {
14001 if (!loc->enabled)
14002 fp.printf ("disable $bpnum.%d\n", n);
14003
14004 n++;
14005 }
14006 }
14007 }
8bf6485c 14008
e0700ba4
SM
14009 if (extra_trace_bits && !default_collect.empty ())
14010 fp.printf ("set default-collect %s\n", default_collect.c_str ());
8bf6485c 14011
1042e4c0 14012 if (from_tty)
6cb06a8c 14013 gdb_printf (_("Saved to file '%s'.\n"), expanded_filename.get ());
6149aea9
PA
14014}
14015
14016/* The `save breakpoints' command. */
14017
14018static void
4495129a 14019save_breakpoints_command (const char *args, int from_tty)
6149aea9
PA
14020{
14021 save_breakpoints (args, from_tty, NULL);
14022}
14023
14024/* The `save tracepoints' command. */
14025
14026static void
4495129a 14027save_tracepoints_command (const char *args, int from_tty)
6149aea9
PA
14028{
14029 save_breakpoints (args, from_tty, is_tracepoint);
1042e4c0
SS
14030}
14031
c906108c 14032\f
629500fa
KS
14033/* This help string is used to consolidate all the help string for specifying
14034 locations used by several commands. */
14035
14036#define LOCATION_HELP_STRING \
14037"Linespecs are colon-separated lists of location parameters, such as\n\
14038source filename, function name, label name, and line number.\n\
14039Example: To specify the start of a label named \"the_top\" in the\n\
14040function \"fact\" in the file \"factorial.c\", use\n\
14041\"factorial.c:fact:the_top\".\n\
14042\n\
14043Address locations begin with \"*\" and specify an exact address in the\n\
14044program. Example: To specify the fourth byte past the start function\n\
14045\"main\", use \"*main + 4\".\n\
14046\n\
14047Explicit locations are similar to linespecs but use an option/argument\n\
14048syntax to specify location parameters.\n\
14049Example: To specify the start of the label named \"the_top\" in the\n\
14050function \"fact\" in the file \"factorial.c\", use \"-source factorial.c\n\
a20714ff
PA
14051-function fact -label the_top\".\n\
14052\n\
14053By default, a specified function is matched against the program's\n\
14054functions in all scopes. For C++, this means in all namespaces and\n\
14055classes. For Ada, this means in all packages. E.g., in C++,\n\
14056\"func()\" matches \"A::func()\", \"A::B::func()\", etc. The\n\
14057\"-qualified\" flag overrides this behavior, making GDB interpret the\n\
89549d7f 14058specified name as a complete fully-qualified name instead."
629500fa 14059
4a64f543
MS
14060/* This help string is used for the break, hbreak, tbreak and thbreak
14061 commands. It is defined as a macro to prevent duplication.
14062 COMMAND should be a string constant containing the name of the
14063 command. */
629500fa 14064
31e2b00f 14065#define BREAK_ARGS_HELP(command) \
733d554a
TBA
14066command" [PROBE_MODIFIER] [LOCATION] [thread THREADNUM]\n\
14067\t[-force-condition] [if CONDITION]\n\
fb7b5af4
SDJ
14068PROBE_MODIFIER shall be present if the command is to be placed in a\n\
14069probe point. Accepted values are `-probe' (for a generic, automatically\n\
d4777acb
JM
14070guessed probe type), `-probe-stap' (for a SystemTap probe) or \n\
14071`-probe-dtrace' (for a DTrace probe).\n\
629500fa
KS
14072LOCATION may be a linespec, address, or explicit location as described\n\
14073below.\n\
14074\n\
dc10affe
PA
14075With no LOCATION, uses current execution address of the selected\n\
14076stack frame. This is useful for breaking on return to a stack frame.\n\
31e2b00f
AS
14077\n\
14078THREADNUM is the number from \"info threads\".\n\
14079CONDITION is a boolean expression.\n\
733d554a
TBA
14080\n\
14081With the \"-force-condition\" flag, the condition is defined even when\n\
14082it is invalid for all current locations.\n\
89549d7f 14083\n" LOCATION_HELP_STRING "\n\n\
d41c0fc8
PA
14084Multiple breakpoints at one place are permitted, and useful if their\n\
14085conditions are different.\n\
31e2b00f
AS
14086\n\
14087Do \"help breakpoints\" for info on other commands dealing with breakpoints."
14088
44feb3ce
TT
14089/* List of subcommands for "catch". */
14090static struct cmd_list_element *catch_cmdlist;
14091
14092/* List of subcommands for "tcatch". */
14093static struct cmd_list_element *tcatch_cmdlist;
14094
9ac4176b 14095void
a121b7c1 14096add_catch_command (const char *name, const char *docstring,
5538b03c 14097 cmd_func_ftype *func,
625e8578 14098 completer_ftype *completer,
44feb3ce
TT
14099 void *user_data_catch,
14100 void *user_data_tcatch)
14101{
14102 struct cmd_list_element *command;
14103
0450cc4c 14104 command = add_cmd (name, class_breakpoint, docstring,
44feb3ce 14105 &catch_cmdlist);
5538b03c 14106 command->func = func;
0f8e2034 14107 command->set_context (user_data_catch);
a96d9b2e 14108 set_cmd_completer (command, completer);
44feb3ce 14109
0450cc4c 14110 command = add_cmd (name, class_breakpoint, docstring,
44feb3ce 14111 &tcatch_cmdlist);
5538b03c 14112 command->func = func;
0f8e2034 14113 command->set_context (user_data_tcatch);
a96d9b2e 14114 set_cmd_completer (command, completer);
44feb3ce
TT
14115}
14116
0574c78f
GB
14117/* Zero if any of the breakpoint's locations could be a location where
14118 functions have been inlined, nonzero otherwise. */
14119
14120static int
14121is_non_inline_function (struct breakpoint *b)
14122{
14123 /* The shared library event breakpoint is set on the address of a
14124 non-inline function. */
14125 if (b->type == bp_shlib_event)
14126 return 1;
14127
14128 return 0;
14129}
14130
14131/* Nonzero if the specified PC cannot be a location where functions
14132 have been inlined. */
14133
14134int
accd0bcd 14135pc_at_non_inline_function (const address_space *aspace, CORE_ADDR pc,
c272a98c 14136 const target_waitstatus &ws)
0574c78f 14137{
43892fdf 14138 for (breakpoint *b : all_breakpoints ())
0574c78f
GB
14139 {
14140 if (!is_non_inline_function (b))
14141 continue;
14142
40cb8ca5 14143 for (bp_location *bl : b->locations ())
0574c78f
GB
14144 {
14145 if (!bl->shlib_disabled
09ac7c10 14146 && bpstat_check_location (bl, aspace, pc, ws))
0574c78f
GB
14147 return 1;
14148 }
14149 }
14150
14151 return 0;
14152}
14153
2f202fde
JK
14154/* Remove any references to OBJFILE which is going to be freed. */
14155
14156void
14157breakpoint_free_objfile (struct objfile *objfile)
14158{
48d7020b 14159 for (bp_location *loc : all_bp_locations ())
3c86fae3 14160 if (loc->symtab != NULL && loc->symtab->compunit ()->objfile () == objfile)
2f202fde
JK
14161 loc->symtab = NULL;
14162}
14163
8bfd80db
YQ
14164/* Chain containing all defined "enable breakpoint" subcommands. */
14165
14166static struct cmd_list_element *enablebreaklist = NULL;
14167
8588b356
SM
14168/* See breakpoint.h. */
14169
14170cmd_list_element *commands_cmd_element = nullptr;
14171
6c265988 14172void _initialize_breakpoint ();
c906108c 14173void
6c265988 14174_initialize_breakpoint ()
c906108c
SS
14175{
14176 struct cmd_list_element *c;
14177
c90e7d63
SM
14178 gdb::observers::solib_unloaded.attach (disable_breakpoints_in_unloaded_shlib,
14179 "breakpoint");
14180 gdb::observers::free_objfile.attach (disable_breakpoints_in_freed_objfile,
14181 "breakpoint");
14182 gdb::observers::memory_changed.attach (invalidate_bp_value_on_memory_change,
14183 "breakpoint");
84acb35a 14184
c906108c
SS
14185 breakpoint_chain = 0;
14186 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
14187 before a breakpoint is set. */
14188 breakpoint_count = 0;
14189
1042e4c0
SS
14190 tracepoint_count = 0;
14191
1bedd215
AC
14192 add_com ("ignore", class_breakpoint, ignore_command, _("\
14193Set ignore-count of breakpoint number N to COUNT.\n\
14194Usage is `ignore N COUNT'."));
c906108c 14195
8588b356
SM
14196 commands_cmd_element = add_com ("commands", class_breakpoint,
14197 commands_command, _("\
18da0c51
MG
14198Set commands to be executed when the given breakpoints are hit.\n\
14199Give a space-separated breakpoint list as argument after \"commands\".\n\
14200A list element can be a breakpoint number (e.g. `5') or a range of numbers\n\
14201(e.g. `5-7').\n\
c906108c
SS
14202With no argument, the targeted breakpoint is the last one set.\n\
14203The commands themselves follow starting on the next line.\n\
14204Type a line containing \"end\" to indicate the end of them.\n\
14205Give \"silent\" as the first line to make the breakpoint silent;\n\
1bedd215 14206then no output is printed when it is hit, except what the commands print."));
c906108c 14207
b1d4d8d1
TBA
14208 const auto cc_opts = make_condition_command_options_def_group (nullptr);
14209 static std::string condition_command_help
14210 = gdb::option::build_help (_("\
1bedd215 14211Specify breakpoint number N to break only if COND is true.\n\
b1d4d8d1 14212Usage is `condition [OPTION] N COND', where N is an integer and COND\n\
733d554a 14213is an expression to be evaluated whenever breakpoint N is reached.\n\
b1d4d8d1
TBA
14214\n\
14215Options:\n\
14216%OPTIONS%"), cc_opts);
14217
14218 c = add_com ("condition", class_breakpoint, condition_command,
14219 condition_command_help.c_str ());
14220 set_cmd_completer_handle_brkchars (c, condition_completer);
c906108c 14221
1bedd215 14222 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
31e2b00f 14223Set a temporary breakpoint.\n\
c906108c
SS
14224Like \"break\" except the breakpoint is only temporary,\n\
14225so it will be deleted when hit. Equivalent to \"break\" followed\n\
31e2b00f
AS
14226by using \"enable delete\" on the breakpoint number.\n\
14227\n"
14228BREAK_ARGS_HELP ("tbreak")));
5ba2abeb 14229 set_cmd_completer (c, location_completer);
c94fdfd0 14230
1bedd215 14231 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
a3be7890 14232Set a hardware assisted breakpoint.\n\
c906108c 14233Like \"break\" except the breakpoint requires hardware support,\n\
31e2b00f
AS
14234some target hardware may not have this support.\n\
14235\n"
14236BREAK_ARGS_HELP ("hbreak")));
5ba2abeb 14237 set_cmd_completer (c, location_completer);
c906108c 14238
1bedd215 14239 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
31e2b00f 14240Set a temporary hardware assisted breakpoint.\n\
c906108c 14241Like \"hbreak\" except the breakpoint is only temporary,\n\
31e2b00f
AS
14242so it will be deleted when hit.\n\
14243\n"
14244BREAK_ARGS_HELP ("thbreak")));
5ba2abeb 14245 set_cmd_completer (c, location_completer);
c906108c 14246
3947f654
SM
14247 cmd_list_element *enable_cmd
14248 = add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
590042fc
PW
14249Enable all or some breakpoints.\n\
14250Usage: enable [BREAKPOINTNUM]...\n\
c906108c
SS
14251Give breakpoint numbers (separated by spaces) as arguments.\n\
14252With no subcommand, breakpoints are enabled until you command otherwise.\n\
14253This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 14254With a subcommand you can enable temporarily."),
3947f654 14255 &enablelist, 1, &cmdlist);
c906108c 14256
3947f654 14257 add_com_alias ("en", enable_cmd, class_breakpoint, 1);
c906108c 14258
84951ab5 14259 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
590042fc
PW
14260Enable all or some breakpoints.\n\
14261Usage: enable breakpoints [BREAKPOINTNUM]...\n\
c906108c
SS
14262Give breakpoint numbers (separated by spaces) as arguments.\n\
14263This is used to cancel the effect of the \"disable\" command.\n\
89549d7f 14264May be abbreviated to simply \"enable\"."),
2f822da5 14265 &enablebreaklist, 1, &enablelist);
c906108c 14266
1a966eab 14267 add_cmd ("once", no_class, enable_once_command, _("\
590042fc
PW
14268Enable some breakpoints for one hit.\n\
14269Usage: enable breakpoints once BREAKPOINTNUM...\n\
1a966eab 14270If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
c906108c
SS
14271 &enablebreaklist);
14272
1a966eab 14273 add_cmd ("delete", no_class, enable_delete_command, _("\
590042fc
PW
14274Enable some breakpoints and delete when hit.\n\
14275Usage: enable breakpoints delete BREAKPOINTNUM...\n\
1a966eab 14276If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
14277 &enablebreaklist);
14278
816338b5 14279 add_cmd ("count", no_class, enable_count_command, _("\
590042fc
PW
14280Enable some breakpoints for COUNT hits.\n\
14281Usage: enable breakpoints count COUNT BREAKPOINTNUM...\n\
816338b5
SS
14282If a breakpoint is hit while enabled in this fashion,\n\
14283the count is decremented; when it reaches zero, the breakpoint is disabled."),
14284 &enablebreaklist);
14285
1a966eab 14286 add_cmd ("delete", no_class, enable_delete_command, _("\
590042fc
PW
14287Enable some breakpoints and delete when hit.\n\
14288Usage: enable delete BREAKPOINTNUM...\n\
1a966eab 14289If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
14290 &enablelist);
14291
1a966eab 14292 add_cmd ("once", no_class, enable_once_command, _("\
590042fc
PW
14293Enable some breakpoints for one hit.\n\
14294Usage: enable once BREAKPOINTNUM...\n\
1a966eab 14295If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
816338b5
SS
14296 &enablelist);
14297
14298 add_cmd ("count", no_class, enable_count_command, _("\
590042fc
PW
14299Enable some breakpoints for COUNT hits.\n\
14300Usage: enable count COUNT BREAKPOINTNUM...\n\
816338b5
SS
14301If a breakpoint is hit while enabled in this fashion,\n\
14302the count is decremented; when it reaches zero, the breakpoint is disabled."),
c906108c
SS
14303 &enablelist);
14304
3947f654
SM
14305 cmd_list_element *disable_cmd
14306 = add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
590042fc
PW
14307Disable all or some breakpoints.\n\
14308Usage: disable [BREAKPOINTNUM]...\n\
c906108c
SS
14309Arguments are breakpoint numbers with spaces in between.\n\
14310To disable all breakpoints, give no argument.\n\
64b9b334 14311A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
3947f654
SM
14312 &disablelist, 1, &cmdlist);
14313 add_com_alias ("dis", disable_cmd, class_breakpoint, 1);
14314 add_com_alias ("disa", disable_cmd, class_breakpoint, 1);
c906108c 14315
57b4f16e 14316 add_cmd ("breakpoints", class_breakpoint, disable_command, _("\
590042fc
PW
14317Disable all or some breakpoints.\n\
14318Usage: disable breakpoints [BREAKPOINTNUM]...\n\
c906108c
SS
14319Arguments are breakpoint numbers with spaces in between.\n\
14320To disable all breakpoints, give no argument.\n\
64b9b334 14321A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
1a966eab 14322This command may be abbreviated \"disable\"."),
c906108c
SS
14323 &disablelist);
14324
3947f654
SM
14325 cmd_list_element *delete_cmd
14326 = add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
590042fc
PW
14327Delete all or some breakpoints.\n\
14328Usage: delete [BREAKPOINTNUM]...\n\
c906108c
SS
14329Arguments are breakpoint numbers with spaces in between.\n\
14330To delete all breakpoints, give no argument.\n\
14331\n\
590042fc 14332Also a prefix command for deletion of other GDB objects."),
3947f654
SM
14333 &deletelist, 1, &cmdlist);
14334 add_com_alias ("d", delete_cmd, class_breakpoint, 1);
14335 add_com_alias ("del", delete_cmd, class_breakpoint, 1);
c906108c 14336
57b4f16e 14337 add_cmd ("breakpoints", class_breakpoint, delete_command, _("\
590042fc
PW
14338Delete all or some breakpoints or auto-display expressions.\n\
14339Usage: delete breakpoints [BREAKPOINTNUM]...\n\
c906108c
SS
14340Arguments are breakpoint numbers with spaces in between.\n\
14341To delete all breakpoints, give no argument.\n\
1a966eab 14342This command may be abbreviated \"delete\"."),
c906108c
SS
14343 &deletelist);
14344
3947f654
SM
14345 cmd_list_element *clear_cmd
14346 = add_com ("clear", class_breakpoint, clear_command, _("\
629500fa
KS
14347Clear breakpoint at specified location.\n\
14348Argument may be a linespec, explicit, or address location as described below.\n\
1bedd215
AC
14349\n\
14350With no argument, clears all breakpoints in the line that the selected frame\n\
629500fa 14351is executing in.\n"
89549d7f 14352"\n" LOCATION_HELP_STRING "\n\n\
1bedd215 14353See also the \"delete\" command which clears breakpoints by number."));
3947f654 14354 add_com_alias ("cl", clear_cmd, class_breakpoint, 1);
c906108c 14355
3947f654
SM
14356 cmd_list_element *break_cmd
14357 = add_com ("break", class_breakpoint, break_command, _("\
629500fa 14358Set breakpoint at specified location.\n"
31e2b00f 14359BREAK_ARGS_HELP ("break")));
3947f654 14360 set_cmd_completer (break_cmd, location_completer);
c94fdfd0 14361
3947f654
SM
14362 add_com_alias ("b", break_cmd, class_run, 1);
14363 add_com_alias ("br", break_cmd, class_run, 1);
14364 add_com_alias ("bre", break_cmd, class_run, 1);
14365 add_com_alias ("brea", break_cmd, class_run, 1);
c906108c 14366
e0f25bd9
SM
14367 cmd_list_element *info_breakpoints_cmd
14368 = add_info ("breakpoints", info_breakpoints_command, _("\
e5a67952 14369Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
c906108c
SS
14370The \"Type\" column indicates one of:\n\
14371\tbreakpoint - normal breakpoint\n\
14372\twatchpoint - watchpoint\n\
14373The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
14374the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
14375breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
14376address and file/line number respectively.\n\
14377\n\
14378Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
14379are set to the address of the last breakpoint listed unless the command\n\
14380is prefixed with \"server \".\n\n\
c906108c 14381Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 14382breakpoint set."));
c906108c 14383
e0f25bd9 14384 add_info_alias ("b", info_breakpoints_cmd, 1);
6b04bdb7 14385
1a966eab
AC
14386 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
14387Status of all breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
14388The \"Type\" column indicates one of:\n\
14389\tbreakpoint - normal breakpoint\n\
14390\twatchpoint - watchpoint\n\
14391\tlongjmp - internal breakpoint used to step through longjmp()\n\
14392\tlongjmp resume - internal breakpoint at the target of longjmp()\n\
14393\tuntil - internal breakpoint used by the \"until\" command\n\
1a966eab
AC
14394\tfinish - internal breakpoint used by the \"finish\" command\n\
14395The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
c906108c
SS
14396the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
14397breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1a966eab
AC
14398address and file/line number respectively.\n\
14399\n\
14400Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
14401are set to the address of the last breakpoint listed unless the command\n\
14402is prefixed with \"server \".\n\n\
c906108c 14403Convenience variable \"$bpnum\" contains the number of the last\n\
1a966eab 14404breakpoint set."),
c906108c
SS
14405 &maintenanceinfolist);
14406
3b6acaee 14407 add_basic_prefix_cmd ("catch", class_breakpoint, _("\
44feb3ce 14408Set catchpoints to catch events."),
2f822da5 14409 &catch_cmdlist,
3b6acaee 14410 0/*allow-unknown*/, &cmdlist);
44feb3ce 14411
3b6acaee 14412 add_basic_prefix_cmd ("tcatch", class_breakpoint, _("\
44feb3ce 14413Set temporary catchpoints to catch events."),
2f822da5 14414 &tcatch_cmdlist,
3b6acaee 14415 0/*allow-unknown*/, &cmdlist);
44feb3ce 14416
a15a5258
AB
14417 const auto opts = make_watch_options_def_group (nullptr);
14418
14419 static const std::string watch_help = gdb::option::build_help (_("\
14420Set a watchpoint for EXPRESSION.\n\
14421Usage: watch [-location] EXPRESSION\n\
14422\n\
14423Options:\n\
14424%OPTIONS%\n\
14425\n\
c906108c 14426A watchpoint stops execution of your program whenever the value of\n\
a15a5258
AB
14427an expression changes."), opts);
14428 c = add_com ("watch", class_breakpoint, watch_command,
14429 watch_help.c_str ());
14430 set_cmd_completer_handle_brkchars (c, watch_command_completer);
14431
14432 static const std::string rwatch_help = gdb::option::build_help (_("\
14433Set a read watchpoint for EXPRESSION.\n\
14434Usage: rwatch [-location] EXPRESSION\n\
14435\n\
14436Options:\n\
14437%OPTIONS%\n\
14438\n\
14439A read watchpoint stops execution of your program whenever the value of\n\
14440an expression is read."), opts);
14441 c = add_com ("rwatch", class_breakpoint, rwatch_command,
14442 rwatch_help.c_str ());
14443 set_cmd_completer_handle_brkchars (c, watch_command_completer);
14444
14445 static const std::string awatch_help = gdb::option::build_help (_("\
14446Set an access watchpoint for EXPRESSION.\n\
14447Usage: awatch [-location] EXPRESSION\n\
14448\n\
14449Options:\n\
14450%OPTIONS%\n\
14451\n\
14452An access watchpoint stops execution of your program whenever the value\n\
14453of an expression is either read or written."), opts);
14454 c = add_com ("awatch", class_breakpoint, awatch_command,
14455 awatch_help.c_str ());
14456 set_cmd_completer_handle_brkchars (c, watch_command_completer);
c906108c 14457
11db9430 14458 add_info ("watchpoints", info_watchpoints_command, _("\
e5a67952 14459Status of specified watchpoints (all watchpoints if no argument)."));
c906108c 14460
920d2a44
AC
14461 /* XXX: cagney/2005-02-23: This should be a boolean, and should
14462 respond to changes - contrary to the description. */
85c07804
AC
14463 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
14464 &can_use_hw_watchpoints, _("\
14465Set debugger's willingness to use watchpoint hardware."), _("\
14466Show debugger's willingness to use watchpoint hardware."), _("\
c906108c
SS
14467If zero, gdb will not use hardware for new watchpoints, even if\n\
14468such is available. (However, any hardware watchpoints that were\n\
14469created before setting this to nonzero, will continue to use watchpoint\n\
85c07804
AC
14470hardware.)"),
14471 NULL,
920d2a44 14472 show_can_use_hw_watchpoints,
85c07804 14473 &setlist, &showlist);
c906108c
SS
14474
14475 can_use_hw_watchpoints = 1;
fa8d40ab 14476
1042e4c0
SS
14477 /* Tracepoint manipulation commands. */
14478
3947f654
SM
14479 cmd_list_element *trace_cmd
14480 = add_com ("trace", class_breakpoint, trace_command, _("\
629500fa 14481Set a tracepoint at specified location.\n\
1042e4c0
SS
14482\n"
14483BREAK_ARGS_HELP ("trace") "\n\
14484Do \"help tracepoints\" for info on other tracepoint commands."));
3947f654 14485 set_cmd_completer (trace_cmd, location_completer);
1042e4c0 14486
3947f654
SM
14487 add_com_alias ("tp", trace_cmd, class_breakpoint, 0);
14488 add_com_alias ("tr", trace_cmd, class_breakpoint, 1);
14489 add_com_alias ("tra", trace_cmd, class_breakpoint, 1);
14490 add_com_alias ("trac", trace_cmd, class_breakpoint, 1);
1042e4c0 14491
7a697b8d 14492 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
629500fa 14493Set a fast tracepoint at specified location.\n\
7a697b8d
SS
14494\n"
14495BREAK_ARGS_HELP ("ftrace") "\n\
14496Do \"help tracepoints\" for info on other tracepoint commands."));
14497 set_cmd_completer (c, location_completer);
14498
0fb4aa4b 14499 c = add_com ("strace", class_breakpoint, strace_command, _("\
629500fa 14500Set a static tracepoint at location or marker.\n\
0fb4aa4b
PA
14501\n\
14502strace [LOCATION] [if CONDITION]\n\
629500fa
KS
14503LOCATION may be a linespec, explicit, or address location (described below) \n\
14504or -m MARKER_ID.\n\n\
14505If a marker id is specified, probe the marker with that name. With\n\
14506no LOCATION, uses current execution address of the selected stack frame.\n\
0fb4aa4b
PA
14507Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
14508This collects arbitrary user data passed in the probe point call to the\n\
14509tracing library. You can inspect it when analyzing the trace buffer,\n\
14510by printing the $_sdata variable like any other convenience variable.\n\
14511\n\
14512CONDITION is a boolean expression.\n\
89549d7f 14513\n" LOCATION_HELP_STRING "\n\n\
d41c0fc8
PA
14514Multiple tracepoints at one place are permitted, and useful if their\n\
14515conditions are different.\n\
0fb4aa4b
PA
14516\n\
14517Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
14518Do \"help tracepoints\" for info on other tracepoint commands."));
14519 set_cmd_completer (c, location_completer);
14520
e0f25bd9
SM
14521 cmd_list_element *info_tracepoints_cmd
14522 = add_info ("tracepoints", info_tracepoints_command, _("\
e5a67952 14523Status of specified tracepoints (all tracepoints if no argument).\n\
1042e4c0
SS
14524Convenience variable \"$tpnum\" contains the number of the\n\
14525last tracepoint set."));
14526
e0f25bd9 14527 add_info_alias ("tp", info_tracepoints_cmd, 1);
1042e4c0 14528
5e84b7ee
SM
14529 cmd_list_element *delete_tracepoints_cmd
14530 = add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
1042e4c0
SS
14531Delete specified tracepoints.\n\
14532Arguments are tracepoint numbers, separated by spaces.\n\
14533No argument means delete all tracepoints."),
14534 &deletelist);
5e84b7ee 14535 add_alias_cmd ("tr", delete_tracepoints_cmd, class_trace, 1, &deletelist);
1042e4c0
SS
14536
14537 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
14538Disable specified tracepoints.\n\
14539Arguments are tracepoint numbers, separated by spaces.\n\
14540No argument means disable all tracepoints."),
14541 &disablelist);
14542 deprecate_cmd (c, "disable");
14543
14544 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
14545Enable specified tracepoints.\n\
14546Arguments are tracepoint numbers, separated by spaces.\n\
14547No argument means enable all tracepoints."),
14548 &enablelist);
14549 deprecate_cmd (c, "enable");
14550
14551 add_com ("passcount", class_trace, trace_pass_command, _("\
14552Set the passcount for a tracepoint.\n\
14553The trace will end when the tracepoint has been passed 'count' times.\n\
14554Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
14555if TPNUM is omitted, passcount refers to the last tracepoint defined."));
14556
0743fc83
TT
14557 add_basic_prefix_cmd ("save", class_breakpoint,
14558 _("Save breakpoint definitions as a script."),
2f822da5 14559 &save_cmdlist,
0743fc83 14560 0/*allow-unknown*/, &cmdlist);
6149aea9
PA
14561
14562 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
14563Save current breakpoint definitions as a script.\n\
cce7e648 14564This includes all types of breakpoints (breakpoints, watchpoints,\n\
6149aea9
PA
14565catchpoints, tracepoints). Use the 'source' command in another debug\n\
14566session to restore them."),
14567 &save_cmdlist);
14568 set_cmd_completer (c, filename_completer);
14569
3947f654
SM
14570 cmd_list_element *save_tracepoints_cmd
14571 = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
1042e4c0 14572Save current tracepoint definitions as a script.\n\
6149aea9
PA
14573Use the 'source' command in another debug session to restore them."),
14574 &save_cmdlist);
3947f654 14575 set_cmd_completer (save_tracepoints_cmd, filename_completer);
1042e4c0 14576
3947f654 14577 c = add_com_alias ("save-tracepoints", save_tracepoints_cmd, class_trace, 0);
6149aea9
PA
14578 deprecate_cmd (c, "save tracepoints");
14579
f54bdb6d
SM
14580 add_setshow_prefix_cmd ("breakpoint", class_maintenance,
14581 _("\
590042fc 14582Breakpoint specific settings.\n\
fa8d40ab 14583Configure various breakpoint-specific variables such as\n\
590042fc 14584pending breakpoint behavior."),
f54bdb6d 14585 _("\
590042fc 14586Breakpoint specific settings.\n\
fa8d40ab 14587Configure various breakpoint-specific variables such as\n\
590042fc 14588pending breakpoint behavior."),
f54bdb6d
SM
14589 &breakpoint_set_cmdlist, &breakpoint_show_cmdlist,
14590 &setlist, &showlist);
fa8d40ab 14591
7915a72c
AC
14592 add_setshow_auto_boolean_cmd ("pending", no_class,
14593 &pending_break_support, _("\
14594Set debugger's behavior regarding pending breakpoints."), _("\
14595Show debugger's behavior regarding pending breakpoints."), _("\
6e1d7d6c
AC
14596If on, an unrecognized breakpoint location will cause gdb to create a\n\
14597pending breakpoint. If off, an unrecognized breakpoint location results in\n\
14598an error. If auto, an unrecognized breakpoint location results in a\n\
7915a72c 14599user-query to see if a pending breakpoint should be created."),
2c5b56ce 14600 NULL,
920d2a44 14601 show_pending_break_support,
6e1d7d6c
AC
14602 &breakpoint_set_cmdlist,
14603 &breakpoint_show_cmdlist);
fa8d40ab
JJ
14604
14605 pending_break_support = AUTO_BOOLEAN_AUTO;
765dc015
VP
14606
14607 add_setshow_boolean_cmd ("auto-hw", no_class,
14608 &automatic_hardware_breakpoints, _("\
14609Set automatic usage of hardware breakpoints."), _("\
14610Show automatic usage of hardware breakpoints."), _("\
14611If set, the debugger will automatically use hardware breakpoints for\n\
14612breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
14613a warning will be emitted for such breakpoints."),
14614 NULL,
14615 show_automatic_hardware_breakpoints,
14616 &breakpoint_set_cmdlist,
14617 &breakpoint_show_cmdlist);
74960c60 14618
a25a5a45
PA
14619 add_setshow_boolean_cmd ("always-inserted", class_support,
14620 &always_inserted_mode, _("\
74960c60
VP
14621Set mode for inserting breakpoints."), _("\
14622Show mode for inserting breakpoints."), _("\
a25a5a45
PA
14623When this mode is on, breakpoints are inserted immediately as soon as\n\
14624they're created, kept inserted even when execution stops, and removed\n\
14625only when the user deletes them. When this mode is off (the default),\n\
14626breakpoints are inserted only when execution continues, and removed\n\
14627when execution stops."),
72d0e2c5
YQ
14628 NULL,
14629 &show_always_inserted_mode,
14630 &breakpoint_set_cmdlist,
14631 &breakpoint_show_cmdlist);
f1310107 14632
b775012e
LM
14633 add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
14634 condition_evaluation_enums,
14635 &condition_evaluation_mode_1, _("\
14636Set mode of breakpoint condition evaluation."), _("\
14637Show mode of breakpoint condition evaluation."), _("\
d1cda5d9 14638When this is set to \"host\", breakpoint conditions will be\n\
b775012e
LM
14639evaluated on the host's side by GDB. When it is set to \"target\",\n\
14640breakpoint conditions will be downloaded to the target (if the target\n\
14641supports such feature) and conditions will be evaluated on the target's side.\n\
14642If this is set to \"auto\" (default), this will be automatically set to\n\
14643\"target\" if it supports condition evaluation, otherwise it will\n\
7a4e8e7d 14644be set to \"host\"."),
b775012e
LM
14645 &set_condition_evaluation_mode,
14646 &show_condition_evaluation_mode,
14647 &breakpoint_set_cmdlist,
14648 &breakpoint_show_cmdlist);
14649
f1310107
TJB
14650 add_com ("break-range", class_breakpoint, break_range_command, _("\
14651Set a breakpoint for an address range.\n\
14652break-range START-LOCATION, END-LOCATION\n\
14653where START-LOCATION and END-LOCATION can be one of the following:\n\
14654 LINENUM, for that line in the current file,\n\
14655 FILE:LINENUM, for that line in that file,\n\
14656 +OFFSET, for that number of lines after the current line\n\
dda83cd7 14657 or the start of the range\n\
f1310107
TJB
14658 FUNCTION, for the first line in that function,\n\
14659 FILE:FUNCTION, to distinguish among like-named static functions.\n\
14660 *ADDRESS, for the instruction at that address.\n\
14661\n\
14662The breakpoint will stop execution of the inferior whenever it executes\n\
14663an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
14664range (including START-LOCATION and END-LOCATION)."));
14665
e7e0cddf 14666 c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
629500fa 14667Set a dynamic printf at specified location.\n\
e7e0cddf 14668dprintf location,format string,arg1,arg2,...\n\
629500fa
KS
14669location may be a linespec, explicit, or address location.\n"
14670"\n" LOCATION_HELP_STRING));
e7e0cddf
SS
14671 set_cmd_completer (c, location_completer);
14672
14673 add_setshow_enum_cmd ("dprintf-style", class_support,
14674 dprintf_style_enums, &dprintf_style, _("\
14675Set the style of usage for dynamic printf."), _("\
14676Show the style of usage for dynamic printf."), _("\
14677This setting chooses how GDB will do a dynamic printf.\n\
14678If the value is \"gdb\", then the printing is done by GDB to its own\n\
14679console, as with the \"printf\" command.\n\
14680If the value is \"call\", the print is done by calling a function in your\n\
14681program; by default printf(), but you can choose a different function or\n\
14682output stream by setting dprintf-function and dprintf-channel."),
14683 update_dprintf_commands, NULL,
14684 &setlist, &showlist);
14685
e7e0cddf
SS
14686 add_setshow_string_cmd ("dprintf-function", class_support,
14687 &dprintf_function, _("\
590042fc
PW
14688Set the function to use for dynamic printf."), _("\
14689Show the function to use for dynamic printf."), NULL,
e7e0cddf
SS
14690 update_dprintf_commands, NULL,
14691 &setlist, &showlist);
14692
e7e0cddf
SS
14693 add_setshow_string_cmd ("dprintf-channel", class_support,
14694 &dprintf_channel, _("\
590042fc
PW
14695Set the channel to use for dynamic printf."), _("\
14696Show the channel to use for dynamic printf."), NULL,
e7e0cddf
SS
14697 update_dprintf_commands, NULL,
14698 &setlist, &showlist);
14699
d3ce09f5
SS
14700 add_setshow_boolean_cmd ("disconnected-dprintf", no_class,
14701 &disconnected_dprintf, _("\
14702Set whether dprintf continues after GDB disconnects."), _("\
14703Show whether dprintf continues after GDB disconnects."), _("\
14704Use this to let dprintf commands continue to hit and produce output\n\
14705even if GDB disconnects or detaches from the target."),
14706 NULL,
14707 NULL,
14708 &setlist, &showlist);
14709
14710 add_com ("agent-printf", class_vars, agent_printf_command, _("\
590042fc
PW
14711Target agent only formatted printing, like the C \"printf\" function.\n\
14712Usage: agent-printf \"format string\", ARG1, ARG2, ARG3, ..., ARGN\n\
14713This supports most C printf format specifications, like %s, %d, etc.\n\
14714This is useful for formatted output in user-defined commands."));
d3ce09f5 14715
491144b5 14716 automatic_hardware_breakpoints = true;
f3b1572e 14717
c90e7d63
SM
14718 gdb::observers::about_to_proceed.attach (breakpoint_about_to_proceed,
14719 "breakpoint");
14720 gdb::observers::thread_exit.attach (remove_threaded_breakpoints,
14721 "breakpoint");
c906108c 14722}