]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/breakpoint.c
ranged_breakpoint: don't use init_raw_breakpoint
[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,
103 const struct breakpoint_ops *,
104 int, int, int, unsigned);
983af33b 105
6c5b2ebe 106static std::vector<symtab_and_line> decode_location_default
626d2320 107 (struct breakpoint *b, struct event_location *location,
6c5b2ebe 108 struct program_space *search_pspace);
983af33b 109
a6535de1
TT
110static int can_use_hardware_watchpoint
111 (const std::vector<value_ref_ptr> &vals);
c906108c 112
b713485d 113static void mention (const breakpoint *);
c906108c 114
348d480f 115static struct breakpoint *set_raw_breakpoint_without_location (struct gdbarch *,
9efa3c7f 116 enum bptype);
4a64f543
MS
117/* This function is used in gdbtk sources and thus can not be made
118 static. */
cd0f67f3
SM
119static struct breakpoint *set_raw_breakpoint (struct gdbarch *gdbarch,
120 struct symtab_and_line,
9efa3c7f 121 enum bptype);
c906108c 122
06edf0c0
PA
123static struct breakpoint *
124 momentary_breakpoint_from_master (struct breakpoint *orig,
125 enum bptype type,
a1aa2221 126 int loc_enabled);
06edf0c0 127
76897487
KB
128static void breakpoint_adjustment_warning (CORE_ADDR, CORE_ADDR, int, int);
129
a6d9a66e
UW
130static CORE_ADDR adjust_breakpoint_address (struct gdbarch *gdbarch,
131 CORE_ADDR bpaddr,
dda83cd7 132 enum bptype bptype);
76897487 133
6c95b8df
PA
134static void describe_other_breakpoints (struct gdbarch *,
135 struct program_space *, CORE_ADDR,
5af949e3 136 struct obj_section *, int);
c906108c 137
85d721b8
PA
138static int watchpoint_locations_match (struct bp_location *loc1,
139 struct bp_location *loc2);
140
7f32a4d5
PA
141static int breakpoint_locations_match (struct bp_location *loc1,
142 struct bp_location *loc2,
143 bool sw_hw_bps_match = false);
144
f1310107 145static int breakpoint_location_address_match (struct bp_location *bl,
accd0bcd 146 const struct address_space *aspace,
f1310107
TJB
147 CORE_ADDR addr);
148
d35ae833 149static int breakpoint_location_address_range_overlap (struct bp_location *,
accd0bcd 150 const address_space *,
d35ae833
PA
151 CORE_ADDR, int);
152
834c0d03 153static int remove_breakpoint (struct bp_location *);
b2b6a7da 154static int remove_breakpoint_1 (struct bp_location *, enum remove_bp_reason);
c906108c 155
313f3b21 156static enum print_stop_action print_bp_stop_message (bpstat *bs);
c906108c 157
a14ed312 158static int hw_breakpoint_used_count (void);
c906108c 159
a1398e0c
PA
160static int hw_watchpoint_use_count (struct breakpoint *);
161
162static int hw_watchpoint_used_count_others (struct breakpoint *except,
163 enum bptype type,
164 int *other_type_used);
c906108c 165
816338b5
SS
166static void enable_breakpoint_disp (struct breakpoint *, enum bpdisp,
167 int count);
c906108c 168
f431efe5 169static void decref_bp_location (struct bp_location **loc);
fe3f5fa8 170
5d926615
TT
171static std::vector<symtab_and_line> bkpt_probe_decode_location
172 (struct breakpoint *b,
173 struct event_location *location,
174 struct program_space *search_pspace);
175
44702360
PA
176/* update_global_location_list's modes of operation wrt to whether to
177 insert locations now. */
178enum ugll_insert_mode
179{
180 /* Don't insert any breakpoint locations into the inferior, only
181 remove already-inserted locations that no longer should be
182 inserted. Functions that delete a breakpoint or breakpoints
183 should specify this mode, so that deleting a breakpoint doesn't
184 have the side effect of inserting the locations of other
185 breakpoints that are marked not-inserted, but should_be_inserted
186 returns true on them.
187
188 This behavior is useful is situations close to tear-down -- e.g.,
189 after an exec, while the target still has execution, but
190 breakpoint shadows of the previous executable image should *NOT*
191 be restored to the new image; or before detaching, where the
192 target still has execution and wants to delete breakpoints from
193 GDB's lists, and all breakpoints had already been removed from
194 the inferior. */
195 UGLL_DONT_INSERT,
196
a25a5a45
PA
197 /* May insert breakpoints iff breakpoints_should_be_inserted_now
198 claims breakpoints should be inserted now. */
04086b45
PA
199 UGLL_MAY_INSERT,
200
a25a5a45
PA
201 /* Insert locations now, irrespective of
202 breakpoints_should_be_inserted_now. E.g., say all threads are
203 stopped right now, and the user did "continue". We need to
204 insert breakpoints _before_ resuming the target, but
205 UGLL_MAY_INSERT wouldn't insert them, because
206 breakpoints_should_be_inserted_now returns false at that point,
207 as no thread is running yet. */
04086b45 208 UGLL_INSERT
44702360
PA
209};
210
211static void update_global_location_list (enum ugll_insert_mode);
a5606eee 212
44702360 213static void update_global_location_list_nothrow (enum ugll_insert_mode);
74960c60 214
74960c60 215static void insert_breakpoint_locations (void);
a5606eee 216
0b39b52e 217static void trace_pass_command (const char *, int);
1042e4c0 218
558a9d82
YQ
219static void set_tracepoint_count (int num);
220
f2478a7e 221static bool is_masked_watchpoint (const struct breakpoint *b);
9c06b0b4 222
983af33b
SDJ
223/* Return 1 if B refers to a static tracepoint set by marker ("-m"), zero
224 otherwise. */
225
226static int strace_marker_p (struct breakpoint *b);
0fb4aa4b 227
8613a476
TT
228static void bkpt_probe_create_sals_from_location
229 (struct event_location *location,
03ada39e 230 struct linespec_result *canonical);
8613a476
TT
231static void tracepoint_probe_create_sals_from_location
232 (struct event_location *location,
03ada39e 233 struct linespec_result *canonical);
8613a476 234
a678887d 235const struct breakpoint_ops base_breakpoint_ops =
8613a476
TT
236{
237 create_sals_from_location_default,
eee031e2 238 create_breakpoints_sal,
8613a476
TT
239};
240
55aa24fb 241/* Breakpoints set on probes. */
a678887d 242static const struct breakpoint_ops bkpt_probe_breakpoint_ops =
8613a476
TT
243{
244 bkpt_probe_create_sals_from_location,
eee031e2 245 create_breakpoints_sal,
8613a476 246};
55aa24fb 247
bac7c5cf 248/* Tracepoints set on probes. */
a678887d 249static const struct breakpoint_ops tracepoint_probe_breakpoint_ops =
8613a476
TT
250{
251 tracepoint_probe_create_sals_from_location,
eee031e2 252 create_breakpoints_sal,
8613a476 253};
bac7c5cf 254
1c2cbcf1
PA
255/* Implementation of abstract dtors. These must exist to satisfy the
256 linker. */
257
258breakpoint::~breakpoint ()
259{
260}
261
262base_breakpoint::~base_breakpoint ()
263{
264}
265
266catchpoint::~catchpoint ()
267{
268}
269
098f1272
TT
270/* The structure to be used in regular breakpoints. */
271struct ordinary_breakpoint : public base_breakpoint
272{
73063f51
TT
273 using base_breakpoint::base_breakpoint;
274
7dd8e7ae 275 int resources_needed (const struct bp_location *) override;
7bd86313 276 enum print_stop_action print_it (const bpstat *bs) const override;
b713485d 277 void print_mention () const override;
4d1ae558 278 void print_recreate (struct ui_file *fp) const override;
098f1272
TT
279};
280
281/* Internal breakpoints. */
282struct internal_breakpoint : public base_breakpoint
283{
73063f51
TT
284 using base_breakpoint::base_breakpoint;
285
c359fff5
TT
286 void re_set () override;
287 void check_status (struct bpstat *bs) override;
7bd86313 288 enum print_stop_action print_it (const bpstat *bs) const override;
b713485d 289 void print_mention () const override;
098f1272
TT
290};
291
292/* Momentary breakpoints. */
293struct momentary_breakpoint : public base_breakpoint
294{
73063f51
TT
295 using base_breakpoint::base_breakpoint;
296
1fd30a47
TT
297 void re_set () override;
298 void check_status (struct bpstat *bs) override;
7bd86313 299 enum print_stop_action print_it (const bpstat *bs) const override;
b713485d 300 void print_mention () const override;
098f1272
TT
301};
302
303/* DPrintf breakpoints. */
5ad71d67 304struct dprintf_breakpoint : public ordinary_breakpoint
098f1272 305{
73063f51
TT
306 using ordinary_breakpoint::ordinary_breakpoint;
307
f293a0b5
TT
308 void re_set () override;
309 int breakpoint_hit (const struct bp_location *bl,
310 const address_space *aspace,
311 CORE_ADDR bp_addr,
312 const target_waitstatus &ws) override;
4d1ae558 313 void print_recreate (struct ui_file *fp) const override;
f293a0b5 314 void after_condition_true (struct bpstat *bs) override;
098f1272
TT
315};
316
ec45bb67
TT
317/* Ranged breakpoints. */
318struct ranged_breakpoint : public ordinary_breakpoint
319{
73063f51
TT
320 explicit ranged_breakpoint (struct gdbarch *gdbarch)
321 : ordinary_breakpoint (gdbarch, bp_hardware_breakpoint)
322 {
323 }
324
ec45bb67
TT
325 int breakpoint_hit (const struct bp_location *bl,
326 const address_space *aspace,
327 CORE_ADDR bp_addr,
328 const target_waitstatus &ws) override;
329 int resources_needed (const struct bp_location *) override;
7bd86313 330 enum print_stop_action print_it (const bpstat *bs) const override;
a67bcaba 331 bool print_one (bp_location **) const override;
ec45bb67 332 void print_one_detail (struct ui_out *) const override;
b713485d 333 void print_mention () const override;
4d1ae558 334 void print_recreate (struct ui_file *fp) const override;
ec45bb67
TT
335};
336
6d7a8c56
TT
337/* Static tracepoints with marker (`-m'). */
338struct static_marker_tracepoint : public tracepoint
339{
73063f51
TT
340 using tracepoint::tracepoint;
341
6d7a8c56
TT
342 std::vector<symtab_and_line> decode_location
343 (struct event_location *location,
344 struct program_space *search_pspace) override;
345};
346
d3ce09f5
SS
347/* The style in which to perform a dynamic printf. This is a user
348 option because different output options have different tradeoffs;
349 if GDB does the printing, there is better error handling if there
350 is a problem with any of the arguments, but using an inferior
351 function lets you have special-purpose printers and sending of
352 output to the same place as compiled-in print functions. */
353
354static const char dprintf_style_gdb[] = "gdb";
355static const char dprintf_style_call[] = "call";
356static const char dprintf_style_agent[] = "agent";
357static const char *const dprintf_style_enums[] = {
358 dprintf_style_gdb,
359 dprintf_style_call,
360 dprintf_style_agent,
361 NULL
362};
363static const char *dprintf_style = dprintf_style_gdb;
364
365/* The function to use for dynamic printf if the preferred style is to
366 call into the inferior. The value is simply a string that is
367 copied into the command, so it can be anything that GDB can
368 evaluate to a callable address, not necessarily a function name. */
369
e0700ba4 370static std::string dprintf_function = "printf";
d3ce09f5
SS
371
372/* The channel to use for dynamic printf if the preferred style is to
373 call into the inferior; if a nonempty string, it will be passed to
374 the call as the first argument, with the format string as the
375 second. As with the dprintf function, this can be anything that
376 GDB knows how to evaluate, so in addition to common choices like
377 "stderr", this could be an app-specific expression like
378 "mystreams[curlogger]". */
379
e0700ba4 380static std::string dprintf_channel;
d3ce09f5
SS
381
382/* True if dprintf commands should continue to operate even if GDB
383 has disconnected. */
491144b5 384static bool disconnected_dprintf = true;
d3ce09f5 385
5cea2a26
PA
386struct command_line *
387breakpoint_commands (struct breakpoint *b)
388{
d1b0a7bf 389 return b->commands ? b->commands.get () : NULL;
5cea2a26 390}
3daf8fe5 391
f3b1572e
PA
392/* Flag indicating that a command has proceeded the inferior past the
393 current breakpoint. */
394
491144b5 395static bool breakpoint_proceeded;
f3b1572e 396
956a9fb9 397const char *
2cec12e5
AR
398bpdisp_text (enum bpdisp disp)
399{
4a64f543
MS
400 /* NOTE: the following values are a part of MI protocol and
401 represent values of 'disp' field returned when inferior stops at
402 a breakpoint. */
bc043ef3 403 static const char * const bpdisps[] = {"del", "dstp", "dis", "keep"};
cc59ec59 404
2cec12e5
AR
405 return bpdisps[(int) disp];
406}
c906108c 407
4a64f543 408/* Prototypes for exported functions. */
c906108c 409/* If FALSE, gdb will not use hardware support for watchpoints, even
4a64f543 410 if such is available. */
c906108c
SS
411static int can_use_hw_watchpoints;
412
920d2a44
AC
413static void
414show_can_use_hw_watchpoints (struct ui_file *file, int from_tty,
415 struct cmd_list_element *c,
416 const char *value)
417{
6cb06a8c
TT
418 gdb_printf (file,
419 _("Debugger's willingness to use "
420 "watchpoint hardware is %s.\n"),
421 value);
920d2a44
AC
422}
423
fa8d40ab
JJ
424/* If AUTO_BOOLEAN_FALSE, gdb will not attempt to create pending breakpoints.
425 If AUTO_BOOLEAN_TRUE, gdb will automatically create pending breakpoints
4a64f543 426 for unrecognized breakpoint locations.
fa8d40ab
JJ
427 If AUTO_BOOLEAN_AUTO, gdb will query when breakpoints are unrecognized. */
428static enum auto_boolean pending_break_support;
920d2a44
AC
429static void
430show_pending_break_support (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 behavior regarding "
436 "pending breakpoints is %s.\n"),
437 value);
920d2a44 438}
fa8d40ab 439
491144b5 440/* If true, gdb will automatically use hardware breakpoints for breakpoints
4a64f543 441 set with "break" but falling in read-only memory.
491144b5 442 If false, gdb will warn about such breakpoints, but won't automatically
765dc015 443 use hardware breakpoints. */
491144b5 444static bool automatic_hardware_breakpoints;
765dc015
VP
445static void
446show_automatic_hardware_breakpoints (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 _("Automatic usage of hardware breakpoints is %s.\n"),
452 value);
765dc015
VP
453}
454
a25a5a45
PA
455/* If on, GDB keeps breakpoints inserted even if the inferior is
456 stopped, and immediately inserts any new breakpoints as soon as
457 they're created. If off (default), GDB keeps breakpoints off of
458 the target as long as possible. That is, it delays inserting
459 breakpoints until the next resume, and removes them again when the
460 target fully stops. This is a bit safer in case GDB crashes while
461 processing user input. */
491144b5 462static bool always_inserted_mode = false;
72d0e2c5 463
33e5cbd6 464static void
74960c60 465show_always_inserted_mode (struct ui_file *file, int from_tty,
33e5cbd6 466 struct cmd_list_element *c, const char *value)
74960c60 467{
6cb06a8c
TT
468 gdb_printf (file, _("Always inserted breakpoint mode is %s.\n"),
469 value);
74960c60
VP
470}
471
b57bacec
PA
472/* See breakpoint.h. */
473
33e5cbd6 474int
a25a5a45 475breakpoints_should_be_inserted_now (void)
33e5cbd6 476{
a25a5a45
PA
477 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
478 {
479 /* If breakpoints are global, they should be inserted even if no
480 thread under gdb's control is running, or even if there are
481 no threads under GDB's control yet. */
482 return 1;
483 }
5b6d1e4f 484 else
a25a5a45 485 {
a25a5a45
PA
486 if (always_inserted_mode)
487 {
488 /* The user wants breakpoints inserted even if all threads
489 are stopped. */
490 return 1;
491 }
492
5b6d1e4f
PA
493 for (inferior *inf : all_inferiors ())
494 if (inf->has_execution ()
495 && threads_are_executing (inf->process_target ()))
496 return 1;
372316f1
PA
497
498 /* Don't remove breakpoints yet if, even though all threads are
499 stopped, we still have events to process. */
08036331 500 for (thread_info *tp : all_non_exited_threads ())
1edb66d8 501 if (tp->resumed () && tp->has_pending_waitstatus ())
372316f1 502 return 1;
a25a5a45
PA
503 }
504 return 0;
33e5cbd6 505}
765dc015 506
b775012e
LM
507static const char condition_evaluation_both[] = "host or target";
508
509/* Modes for breakpoint condition evaluation. */
510static const char condition_evaluation_auto[] = "auto";
511static const char condition_evaluation_host[] = "host";
512static const char condition_evaluation_target[] = "target";
513static const char *const condition_evaluation_enums[] = {
514 condition_evaluation_auto,
515 condition_evaluation_host,
516 condition_evaluation_target,
517 NULL
518};
519
520/* Global that holds the current mode for breakpoint condition evaluation. */
521static const char *condition_evaluation_mode_1 = condition_evaluation_auto;
522
523/* Global that we use to display information to the user (gets its value from
524 condition_evaluation_mode_1. */
525static const char *condition_evaluation_mode = condition_evaluation_auto;
526
527/* Translate a condition evaluation mode MODE into either "host"
528 or "target". This is used mostly to translate from "auto" to the
529 real setting that is being used. It returns the translated
530 evaluation mode. */
531
532static const char *
533translate_condition_evaluation_mode (const char *mode)
534{
535 if (mode == condition_evaluation_auto)
536 {
537 if (target_supports_evaluation_of_breakpoint_conditions ())
538 return condition_evaluation_target;
539 else
540 return condition_evaluation_host;
541 }
542 else
543 return mode;
544}
545
546/* Discovers what condition_evaluation_auto translates to. */
547
548static const char *
549breakpoint_condition_evaluation_mode (void)
550{
551 return translate_condition_evaluation_mode (condition_evaluation_mode);
552}
553
554/* Return true if GDB should evaluate breakpoint conditions or false
555 otherwise. */
556
557static int
558gdb_evaluates_breakpoint_condition_p (void)
559{
560 const char *mode = breakpoint_condition_evaluation_mode ();
561
562 return (mode == condition_evaluation_host);
563}
564
c906108c
SS
565/* Are we executing breakpoint commands? */
566static int executing_breakpoint_commands;
567
c02f5703
MS
568/* Are overlay event breakpoints enabled? */
569static int overlay_events_enabled;
570
e09342b5 571/* See description in breakpoint.h. */
491144b5 572bool target_exact_watchpoints = false;
e09342b5 573
c906108c 574/* Walk the following statement or block through all breakpoints.
e5dd4106 575 ALL_BREAKPOINTS_SAFE does so even if the statement deletes the
4a64f543 576 current breakpoint. */
c906108c 577
5c44784c
JM
578#define ALL_BREAKPOINTS_SAFE(B,TMP) \
579 for (B = breakpoint_chain; \
580 B ? (TMP=B->next, 1): 0; \
581 B = TMP)
c906108c 582
7cc221ef 583/* Chains of all breakpoints defined. */
c906108c 584
81e6b8eb 585static struct breakpoint *breakpoint_chain;
c906108c 586
240edef6 587/* See breakpoint.h. */
43892fdf 588
240edef6 589breakpoint_range
43892fdf
SM
590all_breakpoints ()
591{
592 return breakpoint_range (breakpoint_chain);
593}
594
240edef6 595/* See breakpoint.h. */
1428b37a 596
240edef6 597breakpoint_safe_range
1428b37a
SM
598all_breakpoints_safe ()
599{
600 return breakpoint_safe_range (all_breakpoints ());
601}
602
f6d17b2b
SM
603/* See breakpoint.h. */
604
605tracepoint_range
606all_tracepoints ()
607{
608 return tracepoint_range (breakpoint_chain);
609}
610
39ef2f62 611/* Array is sorted by bp_location_is_less_than - primarily by the ADDRESS. */
876fa593 612
5d51cd5d 613static std::vector<bp_location *> bp_locations;
876fa593 614
055c879f
SM
615/* See breakpoint.h. */
616
617const std::vector<bp_location *> &
48d7020b
SM
618all_bp_locations ()
619{
620 return bp_locations;
621}
622
e0d9a270
SM
623/* Range to iterate over breakpoint locations at a given address. */
624
625struct bp_locations_at_addr_range
626{
627 using iterator = std::vector<bp_location *>::iterator;
628
629 bp_locations_at_addr_range (CORE_ADDR addr)
630 {
631 struct compare
632 {
633 bool operator() (const bp_location *loc, CORE_ADDR addr_) const
634 { return loc->address < addr_; }
635
636 bool operator() (CORE_ADDR addr_, const bp_location *loc) const
637 { return addr_ < loc->address; }
638 };
639
640 auto it_pair = std::equal_range (bp_locations.begin (), bp_locations.end (),
641 addr, compare ());
642
643 m_begin = it_pair.first;
644 m_end = it_pair.second;
645 }
646
647 iterator begin () const
648 { return m_begin; }
649
650 iterator end () const
651 { return m_end; }
652
653private:
654 iterator m_begin;
655 iterator m_end;
656};
657
658/* Return a range to iterate over all breakpoint locations exactly at address
659 ADDR.
660
661 If it's needed to iterate multiple times on the same range, it's possible
662 to save the range in a local variable and use it multiple times:
663
664 auto range = all_bp_locations_at_addr (addr);
665
666 for (bp_location *loc : range)
667 // use loc
668
669 for (bp_location *loc : range)
670 // use loc
671
672 This saves a bit of time, as it avoids re-doing the binary searches to find
673 the range's boundaries. Just remember not to change the bp_locations vector
674 in the mean time, as it could make the range's iterators stale. */
675
676static bp_locations_at_addr_range
677all_bp_locations_at_addr (CORE_ADDR addr)
678{
679 return bp_locations_at_addr_range (addr);
680}
681
4a64f543 682/* Maximum alignment offset between bp_target_info.PLACED_ADDRESS and
f5336ca5 683 ADDRESS for the current elements of BP_LOCATIONS which get a valid
4a64f543 684 result from bp_location_has_shadow. You can use it for roughly
f5336ca5 685 limiting the subrange of BP_LOCATIONS to scan for shadow bytes for
4a64f543 686 an address you need to read. */
876fa593 687
f5336ca5 688static CORE_ADDR bp_locations_placed_address_before_address_max;
876fa593 689
4a64f543
MS
690/* Maximum offset plus alignment between bp_target_info.PLACED_ADDRESS
691 + bp_target_info.SHADOW_LEN and ADDRESS for the current elements of
f5336ca5
PA
692 BP_LOCATIONS which get a valid result from bp_location_has_shadow.
693 You can use it for roughly limiting the subrange of BP_LOCATIONS to
4a64f543 694 scan for shadow bytes for an address you need to read. */
876fa593 695
f5336ca5 696static CORE_ADDR bp_locations_shadow_len_after_address_max;
7cc221ef 697
4a64f543 698/* The locations that no longer correspond to any breakpoint, unlinked
f5336ca5
PA
699 from the bp_locations array, but for which a hit may still be
700 reported by a target. */
1123588c 701static std::vector<bp_location *> moribund_locations;
20874c92 702
c906108c
SS
703/* Number of last breakpoint made. */
704
95a42b64
TT
705static int breakpoint_count;
706
86b17b60
PA
707/* The value of `breakpoint_count' before the last command that
708 created breakpoints. If the last (break-like) command created more
709 than one breakpoint, then the difference between BREAKPOINT_COUNT
710 and PREV_BREAKPOINT_COUNT is more than one. */
711static int prev_breakpoint_count;
c906108c 712
1042e4c0
SS
713/* Number of last tracepoint made. */
714
95a42b64 715static int tracepoint_count;
1042e4c0 716
6149aea9
PA
717static struct cmd_list_element *breakpoint_set_cmdlist;
718static struct cmd_list_element *breakpoint_show_cmdlist;
9291a0cd 719struct cmd_list_element *save_cmdlist;
6149aea9 720
468d015d
JJ
721/* Return whether a breakpoint is an active enabled breakpoint. */
722static int
723breakpoint_enabled (struct breakpoint *b)
724{
0d381245 725 return (b->enable_state == bp_enabled);
468d015d
JJ
726}
727
c906108c
SS
728/* Set breakpoint count to NUM. */
729
95a42b64 730static void
fba45db2 731set_breakpoint_count (int num)
c906108c 732{
86b17b60 733 prev_breakpoint_count = breakpoint_count;
c906108c 734 breakpoint_count = num;
4fa62494 735 set_internalvar_integer (lookup_internalvar ("bpnum"), num);
c906108c
SS
736}
737
86b17b60
PA
738/* Used by `start_rbreak_breakpoints' below, to record the current
739 breakpoint count before "rbreak" creates any breakpoint. */
740static int rbreak_start_breakpoint_count;
741
95a42b64
TT
742/* Called at the start an "rbreak" command to record the first
743 breakpoint made. */
86b17b60 744
c80049d3 745scoped_rbreak_breakpoints::scoped_rbreak_breakpoints ()
95a42b64 746{
86b17b60 747 rbreak_start_breakpoint_count = breakpoint_count;
95a42b64
TT
748}
749
750/* Called at the end of an "rbreak" command to record the last
751 breakpoint made. */
86b17b60 752
c80049d3 753scoped_rbreak_breakpoints::~scoped_rbreak_breakpoints ()
95a42b64 754{
86b17b60 755 prev_breakpoint_count = rbreak_start_breakpoint_count;
95a42b64
TT
756}
757
4a64f543 758/* Used in run_command to zero the hit count when a new run starts. */
c906108c
SS
759
760void
fba45db2 761clear_breakpoint_hit_counts (void)
c906108c 762{
43892fdf 763 for (breakpoint *b : all_breakpoints ())
c906108c
SS
764 b->hit_count = 0;
765}
766
c906108c 767\f
48cb2d85
VP
768/* Return the breakpoint with the specified number, or NULL
769 if the number does not refer to an existing breakpoint. */
770
771struct breakpoint *
772get_breakpoint (int num)
773{
43892fdf 774 for (breakpoint *b : all_breakpoints ())
48cb2d85
VP
775 if (b->number == num)
776 return b;
777
43892fdf 778 return nullptr;
48cb2d85 779}
5c44784c 780
c906108c 781\f
adc36818 782
b775012e
LM
783/* Mark locations as "conditions have changed" in case the target supports
784 evaluating conditions on its side. */
785
786static void
787mark_breakpoint_modified (struct breakpoint *b)
788{
b775012e
LM
789 /* This is only meaningful if the target is
790 evaluating conditions and if the user has
791 opted for condition evaluation on the target's
792 side. */
793 if (gdb_evaluates_breakpoint_condition_p ()
794 || !target_supports_evaluation_of_breakpoint_conditions ())
795 return;
796
797 if (!is_breakpoint (b))
798 return;
799
40cb8ca5 800 for (bp_location *loc : b->locations ())
b775012e
LM
801 loc->condition_changed = condition_modified;
802}
803
804/* Mark location as "conditions have changed" in case the target supports
805 evaluating conditions on its side. */
806
807static void
808mark_breakpoint_location_modified (struct bp_location *loc)
809{
810 /* This is only meaningful if the target is
811 evaluating conditions and if the user has
812 opted for condition evaluation on the target's
813 side. */
814 if (gdb_evaluates_breakpoint_condition_p ()
815 || !target_supports_evaluation_of_breakpoint_conditions ())
816
817 return;
818
819 if (!is_breakpoint (loc->owner))
820 return;
821
822 loc->condition_changed = condition_modified;
823}
824
825/* Sets the condition-evaluation mode using the static global
826 condition_evaluation_mode. */
827
828static void
eb4c3f4a 829set_condition_evaluation_mode (const char *args, int from_tty,
b775012e
LM
830 struct cmd_list_element *c)
831{
b775012e
LM
832 const char *old_mode, *new_mode;
833
834 if ((condition_evaluation_mode_1 == condition_evaluation_target)
835 && !target_supports_evaluation_of_breakpoint_conditions ())
836 {
837 condition_evaluation_mode_1 = condition_evaluation_mode;
838 warning (_("Target does not support breakpoint condition evaluation.\n"
839 "Using host evaluation mode instead."));
840 return;
841 }
842
843 new_mode = translate_condition_evaluation_mode (condition_evaluation_mode_1);
844 old_mode = translate_condition_evaluation_mode (condition_evaluation_mode);
845
abf1152a
JK
846 /* Flip the switch. Flip it even if OLD_MODE == NEW_MODE as one of the
847 settings was "auto". */
848 condition_evaluation_mode = condition_evaluation_mode_1;
849
b775012e
LM
850 /* Only update the mode if the user picked a different one. */
851 if (new_mode != old_mode)
852 {
b775012e
LM
853 /* If the user switched to a different evaluation mode, we
854 need to synch the changes with the target as follows:
855
856 "host" -> "target": Send all (valid) conditions to the target.
857 "target" -> "host": Remove all the conditions from the target.
858 */
859
b775012e
LM
860 if (new_mode == condition_evaluation_target)
861 {
862 /* Mark everything modified and synch conditions with the
863 target. */
48d7020b 864 for (bp_location *loc : all_bp_locations ())
b775012e 865 mark_breakpoint_location_modified (loc);
24b21115 866 }
b775012e
LM
867 else
868 {
869 /* Manually mark non-duplicate locations to synch conditions
870 with the target. We do this to remove all the conditions the
871 target knows about. */
48d7020b 872 for (bp_location *loc : all_bp_locations ())
b775012e
LM
873 if (is_breakpoint (loc->owner) && loc->inserted)
874 loc->needs_update = 1;
875 }
876
877 /* Do the update. */
44702360 878 update_global_location_list (UGLL_MAY_INSERT);
b775012e
LM
879 }
880
881 return;
882}
883
884/* Shows the current mode of breakpoint condition evaluation. Explicitly shows
885 what "auto" is translating to. */
886
887static void
888show_condition_evaluation_mode (struct ui_file *file, int from_tty,
889 struct cmd_list_element *c, const char *value)
890{
891 if (condition_evaluation_mode == condition_evaluation_auto)
6cb06a8c
TT
892 gdb_printf (file,
893 _("Breakpoint condition evaluation "
894 "mode is %s (currently %s).\n"),
895 value,
896 breakpoint_condition_evaluation_mode ());
b775012e 897 else
6cb06a8c
TT
898 gdb_printf (file, _("Breakpoint condition evaluation mode is %s.\n"),
899 value);
b775012e
LM
900}
901
b5fa468f
TBA
902/* Parse COND_STRING in the context of LOC and set as the condition
903 expression of LOC. BP_NUM is the number of LOC's owner, LOC_NUM is
904 the number of LOC within its owner. In case of parsing error, mark
905 LOC as DISABLED_BY_COND. In case of success, unset DISABLED_BY_COND. */
906
907static void
908set_breakpoint_location_condition (const char *cond_string, bp_location *loc,
909 int bp_num, int loc_num)
910{
911 bool has_junk = false;
912 try
913 {
914 expression_up new_exp = parse_exp_1 (&cond_string, loc->address,
915 block_for_pc (loc->address), 0);
916 if (*cond_string != 0)
917 has_junk = true;
918 else
919 {
920 loc->cond = std::move (new_exp);
921 if (loc->disabled_by_cond && loc->enabled)
6cb06a8c
TT
922 gdb_printf (_("Breakpoint %d's condition is now valid at "
923 "location %d, enabling.\n"),
924 bp_num, loc_num);
b5fa468f
TBA
925
926 loc->disabled_by_cond = false;
927 }
928 }
929 catch (const gdb_exception_error &e)
930 {
931 if (loc->enabled)
932 {
933 /* Warn if a user-enabled location is now becoming disabled-by-cond.
934 BP_NUM is 0 if the breakpoint is being defined for the first
935 time using the "break ... if ..." command, and non-zero if
936 already defined. */
937 if (bp_num != 0)
938 warning (_("failed to validate condition at location %d.%d, "
939 "disabling:\n %s"), bp_num, loc_num, e.what ());
940 else
941 warning (_("failed to validate condition at location %d, "
942 "disabling:\n %s"), loc_num, e.what ());
943 }
944
945 loc->disabled_by_cond = true;
946 }
947
948 if (has_junk)
949 error (_("Garbage '%s' follows condition"), cond_string);
950}
951
adc36818 952void
7a26bd4d 953set_breakpoint_condition (struct breakpoint *b, const char *exp,
733d554a 954 int from_tty, bool force)
adc36818 955{
4c55e970 956 if (*exp == 0)
3a5c3e22 957 {
6f781ee3 958 b->cond_string.reset ();
3a5c3e22 959
4c55e970 960 if (is_watchpoint (b))
78319c15 961 static_cast<watchpoint *> (b)->cond_exp.reset ();
4c55e970
TBA
962 else
963 {
b5fa468f 964 int loc_num = 1;
40cb8ca5 965 for (bp_location *loc : b->locations ())
4c55e970
TBA
966 {
967 loc->cond.reset ();
b5fa468f 968 if (loc->disabled_by_cond && loc->enabled)
6cb06a8c
TT
969 gdb_printf (_("Breakpoint %d's condition is now valid at "
970 "location %d, enabling.\n"),
971 b->number, loc_num);
b5fa468f
TBA
972 loc->disabled_by_cond = false;
973 loc_num++;
4c55e970
TBA
974
975 /* No need to free the condition agent expression
976 bytecode (if we have one). We will handle this
977 when we go through update_global_location_list. */
978 }
979 }
1e620590 980
adc36818 981 if (from_tty)
6cb06a8c 982 gdb_printf (_("Breakpoint %d now unconditional.\n"), b->number);
adc36818
PM
983 }
984 else
985 {
adc36818
PM
986 if (is_watchpoint (b))
987 {
699bd4cf 988 innermost_block_tracker tracker;
78319c15 989 const char *arg = exp;
4c55e970 990 expression_up new_exp = parse_exp_1 (&arg, 0, 0, 0, &tracker);
78319c15 991 if (*arg != 0)
adc36818 992 error (_("Junk at end of expression"));
78319c15 993 watchpoint *w = static_cast<watchpoint *> (b);
4c55e970 994 w->cond_exp = std::move (new_exp);
699bd4cf 995 w->cond_exp_valid_block = tracker.block ();
adc36818
PM
996 }
997 else
998 {
4c55e970
TBA
999 /* Parse and set condition expressions. We make two passes.
1000 In the first, we parse the condition string to see if it
b5fa468f
TBA
1001 is valid in at least one location. If so, the condition
1002 would be accepted. So we go ahead and set the locations'
1003 conditions. In case no valid case is found, we throw
4c55e970
TBA
1004 the error and the condition string will be rejected.
1005 This two-pass approach is taken to avoid setting the
1006 state of locations in case of a reject. */
40cb8ca5 1007 for (bp_location *loc : b->locations ())
4c55e970 1008 {
b5fa468f
TBA
1009 try
1010 {
1011 const char *arg = exp;
1012 parse_exp_1 (&arg, loc->address,
1013 block_for_pc (loc->address), 0);
1014 if (*arg != 0)
1015 error (_("Junk at end of expression"));
1016 break;
1017 }
1018 catch (const gdb_exception_error &e)
1019 {
1020 /* Condition string is invalid. If this happens to
733d554a
TBA
1021 be the last loc, abandon (if not forced) or continue
1022 (if forced). */
1023 if (loc->next == nullptr && !force)
b5fa468f
TBA
1024 throw;
1025 }
4c55e970
TBA
1026 }
1027
b5fa468f 1028 /* If we reach here, the condition is valid at some locations. */
dda83cd7 1029 int loc_num = 1;
40cb8ca5
SM
1030 for (bp_location *loc : b->locations ())
1031 {
1032 set_breakpoint_location_condition (exp, loc, b->number, loc_num);
1033 loc_num++;
1034 }
adc36818 1035 }
1e620590
TBA
1036
1037 /* We know that the new condition parsed successfully. The
1038 condition string of the breakpoint can be safely updated. */
6f781ee3 1039 b->cond_string = make_unique_xstrdup (exp);
1e620590 1040 b->condition_not_parsed = 0;
adc36818 1041 }
b775012e
LM
1042 mark_breakpoint_modified (b);
1043
76727919 1044 gdb::observers::breakpoint_modified.notify (b);
adc36818
PM
1045}
1046
79aabb73
TBA
1047/* See breakpoint.h. */
1048
1049void
1050set_breakpoint_condition (int bpnum, const char *exp, int from_tty,
1051 bool force)
1052{
43892fdf 1053 for (breakpoint *b : all_breakpoints ())
79aabb73
TBA
1054 if (b->number == bpnum)
1055 {
1056 /* Check if this breakpoint has a "stop" method implemented in an
1057 extension language. This method and conditions entered into GDB
1058 from the CLI are mutually exclusive. */
1059 const struct extension_language_defn *extlang
1060 = get_breakpoint_cond_ext_lang (b, EXT_LANG_NONE);
1061
1062 if (extlang != NULL)
1063 {
1064 error (_("Only one stop condition allowed. There is currently"
1065 " a %s stop condition defined for this breakpoint."),
1066 ext_lang_capitalized_name (extlang));
1067 }
1068 set_breakpoint_condition (b, exp, from_tty, force);
1069
1070 if (is_breakpoint (b))
1071 update_global_location_list (UGLL_MAY_INSERT);
1072
1073 return;
1074 }
1075
1076 error (_("No breakpoint number %d."), bpnum);
1077}
1078
b1d4d8d1
TBA
1079/* The options for the "condition" command. */
1080
1081struct condition_command_opts
1082{
1083 /* For "-force". */
1084 bool force_condition = false;
1085};
1086
1087static const gdb::option::option_def condition_command_option_defs[] = {
1088
1089 gdb::option::flag_option_def<condition_command_opts> {
1090 "force",
1091 [] (condition_command_opts *opts) { return &opts->force_condition; },
1092 N_("Set the condition even if it is invalid for all current locations."),
1093 },
1094
1095};
1096
1097/* Create an option_def_group for the "condition" options, with
1098 CC_OPTS as context. */
1099
1100static inline gdb::option::option_def_group
1101make_condition_command_options_def_group (condition_command_opts *cc_opts)
1102{
1103 return {{condition_command_option_defs}, cc_opts};
1104}
1105
d55637df
TT
1106/* Completion for the "condition" command. */
1107
eb3ff9a5 1108static void
6f937416 1109condition_completer (struct cmd_list_element *cmd,
eb3ff9a5 1110 completion_tracker &tracker,
b1d4d8d1 1111 const char *text, const char * /*word*/)
d55637df 1112{
b1d4d8d1
TBA
1113 bool has_no_arguments = (*text == '\0');
1114 condition_command_opts cc_opts;
1115 const auto group = make_condition_command_options_def_group (&cc_opts);
1116 if (gdb::option::complete_options
1117 (tracker, &text, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR, group))
1118 return;
d55637df 1119
f1735a53 1120 text = skip_spaces (text);
b1d4d8d1 1121 const char *space = skip_to_space (text);
d55637df
TT
1122 if (*space == '\0')
1123 {
1124 int len;
d55637df
TT
1125
1126 if (text[0] == '$')
1127 {
b1d4d8d1 1128 tracker.advance_custom_word_point_by (1);
d55637df 1129 /* We don't support completion of history indices. */
eb3ff9a5
PA
1130 if (!isdigit (text[1]))
1131 complete_internalvar (tracker, &text[1]);
1132 return;
d55637df
TT
1133 }
1134
b1d4d8d1
TBA
1135 /* Suggest the "-force" flag if no arguments are given. If
1136 arguments were passed, they either already include the flag,
1137 or we are beyond the point of suggesting it because it's
1138 positionally the first argument. */
1139 if (has_no_arguments)
1140 gdb::option::complete_on_all_options (tracker, group);
1141
d55637df
TT
1142 /* We're completing the breakpoint number. */
1143 len = strlen (text);
1144
43892fdf 1145 for (breakpoint *b : all_breakpoints ())
58ce7251
SDJ
1146 {
1147 char number[50];
1148
1149 xsnprintf (number, sizeof (number), "%d", b->number);
1150
1151 if (strncmp (number, text, len) == 0)
b02f78f9 1152 tracker.add_completion (make_unique_xstrdup (number));
58ce7251 1153 }
d55637df 1154
eb3ff9a5 1155 return;
d55637df
TT
1156 }
1157
b1d4d8d1
TBA
1158 /* We're completing the expression part. Skip the breakpoint num. */
1159 const char *exp_start = skip_spaces (space);
1160 tracker.advance_custom_word_point_by (exp_start - text);
1161 text = exp_start;
1162 const char *word = advance_to_expression_complete_word_point (tracker, text);
eb3ff9a5 1163 expression_completer (cmd, tracker, text, word);
d55637df
TT
1164}
1165
c906108c
SS
1166/* condition N EXP -- set break condition of breakpoint N to EXP. */
1167
1168static void
0b39b52e 1169condition_command (const char *arg, int from_tty)
c906108c 1170{
0b39b52e 1171 const char *p;
52f0bd74 1172 int bnum;
c906108c
SS
1173
1174 if (arg == 0)
e2e0b3e5 1175 error_no_arg (_("breakpoint number"));
c906108c
SS
1176
1177 p = arg;
733d554a
TBA
1178
1179 /* Check if the "-force" flag was passed. */
b1d4d8d1
TBA
1180 condition_command_opts cc_opts;
1181 const auto group = make_condition_command_options_def_group (&cc_opts);
1182 gdb::option::process_options
1183 (&p, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR, group);
733d554a 1184
c906108c 1185 bnum = get_number (&p);
5c44784c 1186 if (bnum == 0)
8a3fe4f8 1187 error (_("Bad breakpoint argument: '%s'"), arg);
c906108c 1188
79aabb73 1189 set_breakpoint_condition (bnum, p, from_tty, cc_opts.force_condition);
c906108c
SS
1190}
1191
a7bdde9e
VP
1192/* Check that COMMAND do not contain commands that are suitable
1193 only for tracepoints and not suitable for ordinary breakpoints.
4a64f543
MS
1194 Throw if any such commands is found. */
1195
a7bdde9e
VP
1196static void
1197check_no_tracepoint_commands (struct command_line *commands)
1198{
1199 struct command_line *c;
cc59ec59 1200
a7bdde9e
VP
1201 for (c = commands; c; c = c->next)
1202 {
a7bdde9e 1203 if (c->control_type == while_stepping_control)
3e43a32a
MS
1204 error (_("The 'while-stepping' command can "
1205 "only be used for tracepoints"));
a7bdde9e 1206
12973681
TT
1207 check_no_tracepoint_commands (c->body_list_0.get ());
1208 check_no_tracepoint_commands (c->body_list_1.get ());
a7bdde9e
VP
1209
1210 /* Not that command parsing removes leading whitespace and comment
4a64f543 1211 lines and also empty lines. So, we only need to check for
a7bdde9e
VP
1212 command directly. */
1213 if (strstr (c->line, "collect ") == c->line)
1214 error (_("The 'collect' command can only be used for tracepoints"));
1215
51661e93
VP
1216 if (strstr (c->line, "teval ") == c->line)
1217 error (_("The 'teval' command can only be used for tracepoints"));
a7bdde9e
VP
1218 }
1219}
1220
098f1272 1221struct longjmp_breakpoint : public momentary_breakpoint
3b0871f4 1222{
73063f51
TT
1223 using momentary_breakpoint::momentary_breakpoint;
1224
c1fc2657 1225 ~longjmp_breakpoint () override;
3b0871f4
SM
1226};
1227
d77f58be
SS
1228/* Encapsulate tests for different types of tracepoints. */
1229
3b0871f4
SM
1230static bool
1231is_tracepoint_type (bptype type)
d9b3f62e
PA
1232{
1233 return (type == bp_tracepoint
1234 || type == bp_fast_tracepoint
7b572efb
TT
1235 || type == bp_static_tracepoint
1236 || type == bp_static_marker_tracepoint);
d9b3f62e
PA
1237}
1238
f2478a7e
SM
1239/* See breakpoint.h. */
1240
1241bool
d77f58be 1242is_tracepoint (const struct breakpoint *b)
a7bdde9e 1243{
d9b3f62e 1244 return is_tracepoint_type (b->type);
a7bdde9e 1245}
d9b3f62e 1246
a5e364af
SM
1247/* Factory function to create an appropriate instance of breakpoint given
1248 TYPE. */
1249
1250static std::unique_ptr<breakpoint>
73063f51 1251new_breakpoint_from_type (struct gdbarch *gdbarch, bptype type)
a5e364af
SM
1252{
1253 breakpoint *b;
1254
098f1272
TT
1255 switch (type)
1256 {
1257 case bp_breakpoint:
1258 case bp_hardware_breakpoint:
73063f51 1259 b = new ordinary_breakpoint (gdbarch, type);
098f1272
TT
1260 break;
1261
1262 case bp_fast_tracepoint:
1263 case bp_static_tracepoint:
1264 case bp_tracepoint:
73063f51 1265 b = new tracepoint (gdbarch, type);
098f1272
TT
1266 break;
1267
6d7a8c56 1268 case bp_static_marker_tracepoint:
73063f51 1269 b = new static_marker_tracepoint (gdbarch, type);
6d7a8c56
TT
1270 break;
1271
098f1272 1272 case bp_dprintf:
73063f51 1273 b = new dprintf_breakpoint (gdbarch, type);
098f1272
TT
1274 break;
1275
1276 case bp_overlay_event:
1277 case bp_longjmp_master:
1278 case bp_std_terminate_master:
1279 case bp_exception_master:
1280 case bp_thread_event:
1281 case bp_jit_event:
1282 case bp_shlib_event:
73063f51 1283 b = new internal_breakpoint (gdbarch, type);
098f1272
TT
1284 break;
1285
1286 case bp_longjmp:
1287 case bp_exception:
73063f51 1288 b = new longjmp_breakpoint (gdbarch, type);
098f1272
TT
1289 break;
1290
1291 case bp_watchpoint_scope:
1292 case bp_finish:
1293 case bp_gnu_ifunc_resolver_return:
1294 case bp_step_resume:
1295 case bp_hp_step_resume:
1296 case bp_longjmp_resume:
1297 case bp_longjmp_call_dummy:
1298 case bp_exception_resume:
1299 case bp_call_dummy:
1300 case bp_until:
1301 case bp_std_terminate:
73063f51 1302 b = new momentary_breakpoint (gdbarch, type);
098f1272
TT
1303 break;
1304
1305 default:
1306 gdb_assert_not_reached ("invalid type");
1307 }
a5e364af
SM
1308
1309 return std::unique_ptr<breakpoint> (b);
1310}
1311
e5dd4106 1312/* A helper function that validates that COMMANDS are valid for a
95a42b64
TT
1313 breakpoint. This function will throw an exception if a problem is
1314 found. */
48cb2d85 1315
95a42b64
TT
1316static void
1317validate_commands_for_breakpoint (struct breakpoint *b,
1318 struct command_line *commands)
48cb2d85 1319{
d77f58be 1320 if (is_tracepoint (b))
a7bdde9e 1321 {
c9a6ce02 1322 struct tracepoint *t = (struct tracepoint *) b;
a7bdde9e
VP
1323 struct command_line *c;
1324 struct command_line *while_stepping = 0;
c9a6ce02
PA
1325
1326 /* Reset the while-stepping step count. The previous commands
dda83cd7
SM
1327 might have included a while-stepping action, while the new
1328 ones might not. */
c9a6ce02
PA
1329 t->step_count = 0;
1330
1331 /* We need to verify that each top-level element of commands is
1332 valid for tracepoints, that there's at most one
1333 while-stepping element, and that the while-stepping's body
1334 has valid tracing commands excluding nested while-stepping.
1335 We also need to validate the tracepoint action line in the
1336 context of the tracepoint --- validate_actionline actually
1337 has side effects, like setting the tracepoint's
1338 while-stepping STEP_COUNT, in addition to checking if the
1339 collect/teval actions parse and make sense in the
1340 tracepoint's context. */
a7bdde9e
VP
1341 for (c = commands; c; c = c->next)
1342 {
a7bdde9e
VP
1343 if (c->control_type == while_stepping_control)
1344 {
1345 if (b->type == bp_fast_tracepoint)
3e43a32a
MS
1346 error (_("The 'while-stepping' command "
1347 "cannot be used for fast tracepoint"));
7b572efb
TT
1348 else if (b->type == bp_static_tracepoint
1349 || b->type == bp_static_marker_tracepoint)
3e43a32a
MS
1350 error (_("The 'while-stepping' command "
1351 "cannot be used for static tracepoint"));
a7bdde9e
VP
1352
1353 if (while_stepping)
3e43a32a
MS
1354 error (_("The 'while-stepping' command "
1355 "can be used only once"));
a7bdde9e
VP
1356 else
1357 while_stepping = c;
1358 }
c9a6ce02
PA
1359
1360 validate_actionline (c->line, b);
a7bdde9e
VP
1361 }
1362 if (while_stepping)
1363 {
1364 struct command_line *c2;
1365
12973681
TT
1366 gdb_assert (while_stepping->body_list_1 == nullptr);
1367 c2 = while_stepping->body_list_0.get ();
a7bdde9e
VP
1368 for (; c2; c2 = c2->next)
1369 {
a7bdde9e
VP
1370 if (c2->control_type == while_stepping_control)
1371 error (_("The 'while-stepping' command cannot be nested"));
1372 }
1373 }
1374 }
1375 else
1376 {
1377 check_no_tracepoint_commands (commands);
1378 }
95a42b64
TT
1379}
1380
0fb4aa4b
PA
1381/* Return a vector of all the static tracepoints set at ADDR. The
1382 caller is responsible for releasing the vector. */
1383
f51e0e20 1384std::vector<breakpoint *>
0fb4aa4b
PA
1385static_tracepoints_here (CORE_ADDR addr)
1386{
f51e0e20 1387 std::vector<breakpoint *> found;
0fb4aa4b 1388
43892fdf 1389 for (breakpoint *b : all_breakpoints ())
7b572efb
TT
1390 if (b->type == bp_static_tracepoint
1391 || b->type == bp_static_marker_tracepoint)
0fb4aa4b 1392 {
40cb8ca5 1393 for (bp_location *loc : b->locations ())
0fb4aa4b 1394 if (loc->address == addr)
f51e0e20 1395 found.push_back (b);
0fb4aa4b
PA
1396 }
1397
1398 return found;
1399}
1400
95a42b64 1401/* Set the command list of B to COMMANDS. If breakpoint is tracepoint,
4a64f543 1402 validate that only allowed commands are included. */
95a42b64
TT
1403
1404void
4a64f543 1405breakpoint_set_commands (struct breakpoint *b,
12973681 1406 counted_command_line &&commands)
95a42b64 1407{
93921405 1408 validate_commands_for_breakpoint (b, commands.get ());
a7bdde9e 1409
d1b0a7bf 1410 b->commands = std::move (commands);
76727919 1411 gdb::observers::breakpoint_modified.notify (b);
48cb2d85
VP
1412}
1413
45a43567
TT
1414/* Set the internal `silent' flag on the breakpoint. Note that this
1415 is not the same as the "silent" that may appear in the breakpoint's
1416 commands. */
1417
1418void
1419breakpoint_set_silent (struct breakpoint *b, int silent)
1420{
1421 int old_silent = b->silent;
1422
1423 b->silent = silent;
1424 if (old_silent != silent)
76727919 1425 gdb::observers::breakpoint_modified.notify (b);
45a43567
TT
1426}
1427
1428/* Set the thread for this breakpoint. If THREAD is -1, make the
1429 breakpoint work for any thread. */
1430
1431void
1432breakpoint_set_thread (struct breakpoint *b, int thread)
1433{
1434 int old_thread = b->thread;
1435
1436 b->thread = thread;
1437 if (old_thread != thread)
76727919 1438 gdb::observers::breakpoint_modified.notify (b);
45a43567
TT
1439}
1440
1441/* Set the task for this breakpoint. If TASK is 0, make the
1442 breakpoint work for any task. */
1443
1444void
1445breakpoint_set_task (struct breakpoint *b, int task)
1446{
1447 int old_task = b->task;
1448
1449 b->task = task;
1450 if (old_task != task)
76727919 1451 gdb::observers::breakpoint_modified.notify (b);
45a43567
TT
1452}
1453
95a42b64 1454static void
896b6bda 1455commands_command_1 (const char *arg, int from_tty,
4a64f543 1456 struct command_line *control)
95a42b64 1457{
d1b0a7bf 1458 counted_command_line cmd;
999700cd
PW
1459 /* cmd_read will be true once we have read cmd. Note that cmd might still be
1460 NULL after the call to read_command_lines if the user provides an empty
1461 list of command by just typing "end". */
1462 bool cmd_read = false;
95a42b64 1463
896b6bda
PA
1464 std::string new_arg;
1465
95a42b64
TT
1466 if (arg == NULL || !*arg)
1467 {
da1df1db 1468 /* Argument not explicitly given. Synthesize it. */
86b17b60 1469 if (breakpoint_count - prev_breakpoint_count > 1)
896b6bda
PA
1470 new_arg = string_printf ("%d-%d", prev_breakpoint_count + 1,
1471 breakpoint_count);
95a42b64 1472 else if (breakpoint_count > 0)
896b6bda 1473 new_arg = string_printf ("%d", breakpoint_count);
48649e1b 1474 }
da1df1db
TBA
1475 else
1476 {
1477 /* Create a copy of ARG. This is needed because the "commands"
1478 command may be coming from a script. In that case, the read
1479 line buffer is going to be overwritten in the lambda of
1480 'map_breakpoint_numbers' below when reading the next line
1481 before we are are done parsing the breakpoint numbers. */
1482 new_arg = arg;
1483 }
1484 arg = new_arg.c_str ();
48649e1b
TT
1485
1486 map_breakpoint_numbers
1487 (arg, [&] (breakpoint *b)
1488 {
999700cd 1489 if (!cmd_read)
48649e1b 1490 {
999700cd 1491 gdb_assert (cmd == NULL);
48649e1b 1492 if (control != NULL)
12973681 1493 cmd = control->body_list_0;
48649e1b
TT
1494 else
1495 {
81b1e71c
TT
1496 std::string str
1497 = string_printf (_("Type commands for breakpoint(s) "
1498 "%s, one per line."),
1499 arg);
48649e1b 1500
60b3cef2
TT
1501 auto do_validate = [=] (const char *line)
1502 {
1503 validate_actionline (line, b);
1504 };
1505 gdb::function_view<void (const char *)> validator;
1506 if (is_tracepoint (b))
1507 validator = do_validate;
1508
1509 cmd = read_command_lines (str.c_str (), from_tty, 1, validator);
48649e1b 1510 }
999700cd 1511 cmd_read = true;
48649e1b
TT
1512 }
1513
1514 /* If a breakpoint was on the list more than once, we don't need to
1515 do anything. */
1516 if (b->commands != cmd)
1517 {
d1b0a7bf 1518 validate_commands_for_breakpoint (b, cmd.get ());
48649e1b 1519 b->commands = cmd;
76727919 1520 gdb::observers::breakpoint_modified.notify (b);
48649e1b
TT
1521 }
1522 });
95a42b64
TT
1523}
1524
1525static void
0b39b52e 1526commands_command (const char *arg, int from_tty)
95a42b64
TT
1527{
1528 commands_command_1 (arg, from_tty, NULL);
c906108c 1529}
40c03ae8
EZ
1530
1531/* Like commands_command, but instead of reading the commands from
1532 input stream, takes them from an already parsed command structure.
1533
1534 This is used by cli-script.c to DTRT with breakpoint commands
1535 that are part of if and while bodies. */
1536enum command_control_type
896b6bda 1537commands_from_control_command (const char *arg, struct command_line *cmd)
40c03ae8 1538{
95a42b64
TT
1539 commands_command_1 (arg, 0, cmd);
1540 return simple_control;
40c03ae8 1541}
876fa593
JK
1542
1543/* Return non-zero if BL->TARGET_INFO contains valid information. */
1544
1545static int
1546bp_location_has_shadow (struct bp_location *bl)
1547{
1548 if (bl->loc_type != bp_loc_software_breakpoint)
1549 return 0;
1550 if (!bl->inserted)
1551 return 0;
1552 if (bl->target_info.shadow_len == 0)
e5dd4106 1553 /* BL isn't valid, or doesn't shadow memory. */
876fa593
JK
1554 return 0;
1555 return 1;
1556}
1557
9d497a19
PA
1558/* Update BUF, which is LEN bytes read from the target address
1559 MEMADDR, by replacing a memory breakpoint with its shadowed
1560 contents.
1561
1562 If READBUF is not NULL, this buffer must not overlap with the of
1563 the breakpoint location's shadow_contents buffer. Otherwise, a
1564 failed assertion internal error will be raised. */
1565
1566static void
1567one_breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1568 const gdb_byte *writebuf_org,
1569 ULONGEST memaddr, LONGEST len,
1570 struct bp_target_info *target_info,
1571 struct gdbarch *gdbarch)
1572{
1573 /* Now do full processing of the found relevant range of elements. */
1574 CORE_ADDR bp_addr = 0;
1575 int bp_size = 0;
1576 int bptoffset = 0;
1577
1578 if (!breakpoint_address_match (target_info->placed_address_space, 0,
1579 current_program_space->aspace, 0))
1580 {
1581 /* The breakpoint is inserted in a different address space. */
1582 return;
1583 }
1584
1585 /* Addresses and length of the part of the breakpoint that
1586 we need to copy. */
1587 bp_addr = target_info->placed_address;
1588 bp_size = target_info->shadow_len;
1589
1590 if (bp_addr + bp_size <= memaddr)
1591 {
1592 /* The breakpoint is entirely before the chunk of memory we are
1593 reading. */
1594 return;
1595 }
1596
1597 if (bp_addr >= memaddr + len)
1598 {
1599 /* The breakpoint is entirely after the chunk of memory we are
1600 reading. */
1601 return;
1602 }
1603
1604 /* Offset within shadow_contents. */
1605 if (bp_addr < memaddr)
1606 {
1607 /* Only copy the second part of the breakpoint. */
1608 bp_size -= memaddr - bp_addr;
1609 bptoffset = memaddr - bp_addr;
1610 bp_addr = memaddr;
1611 }
1612
1613 if (bp_addr + bp_size > memaddr + len)
1614 {
1615 /* Only copy the first part of the breakpoint. */
1616 bp_size -= (bp_addr + bp_size) - (memaddr + len);
1617 }
1618
1619 if (readbuf != NULL)
1620 {
1621 /* Verify that the readbuf buffer does not overlap with the
1622 shadow_contents buffer. */
1623 gdb_assert (target_info->shadow_contents >= readbuf + len
1624 || readbuf >= (target_info->shadow_contents
1625 + target_info->shadow_len));
1626
1627 /* Update the read buffer with this inserted breakpoint's
1628 shadow. */
1629 memcpy (readbuf + bp_addr - memaddr,
1630 target_info->shadow_contents + bptoffset, bp_size);
1631 }
1632 else
1633 {
1634 const unsigned char *bp;
0d5ed153
MR
1635 CORE_ADDR addr = target_info->reqstd_address;
1636 int placed_size;
9d497a19
PA
1637
1638 /* Update the shadow with what we want to write to memory. */
1639 memcpy (target_info->shadow_contents + bptoffset,
1640 writebuf_org + bp_addr - memaddr, bp_size);
1641
1642 /* Determine appropriate breakpoint contents and size for this
1643 address. */
0d5ed153 1644 bp = gdbarch_breakpoint_from_pc (gdbarch, &addr, &placed_size);
9d497a19
PA
1645
1646 /* Update the final write buffer with this inserted
1647 breakpoint's INSN. */
1648 memcpy (writebuf + bp_addr - memaddr, bp + bptoffset, bp_size);
1649 }
1650}
1651
8defab1a 1652/* Update BUF, which is LEN bytes read from the target address MEMADDR,
876fa593
JK
1653 by replacing any memory breakpoints with their shadowed contents.
1654
35c63cd8
JB
1655 If READBUF is not NULL, this buffer must not overlap with any of
1656 the breakpoint location's shadow_contents buffers. Otherwise,
1657 a failed assertion internal error will be raised.
1658
876fa593 1659 The range of shadowed area by each bp_location is:
f5336ca5
PA
1660 bl->address - bp_locations_placed_address_before_address_max
1661 up to bl->address + bp_locations_shadow_len_after_address_max
876fa593
JK
1662 The range we were requested to resolve shadows for is:
1663 memaddr ... memaddr + len
1664 Thus the safe cutoff boundaries for performance optimization are
35df4500 1665 memaddr + len <= (bl->address
f5336ca5 1666 - bp_locations_placed_address_before_address_max)
876fa593 1667 and:
f5336ca5 1668 bl->address + bp_locations_shadow_len_after_address_max <= memaddr */
c906108c 1669
8defab1a 1670void
f0ba3972
PA
1671breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1672 const gdb_byte *writebuf_org,
1673 ULONGEST memaddr, LONGEST len)
c906108c 1674{
4a64f543
MS
1675 /* Left boundary, right boundary and median element of our binary
1676 search. */
876fa593
JK
1677 unsigned bc_l, bc_r, bc;
1678
4a64f543
MS
1679 /* Find BC_L which is a leftmost element which may affect BUF
1680 content. It is safe to report lower value but a failure to
1681 report higher one. */
876fa593
JK
1682
1683 bc_l = 0;
5d51cd5d 1684 bc_r = bp_locations.size ();
876fa593
JK
1685 while (bc_l + 1 < bc_r)
1686 {
35df4500 1687 struct bp_location *bl;
876fa593
JK
1688
1689 bc = (bc_l + bc_r) / 2;
f5336ca5 1690 bl = bp_locations[bc];
876fa593 1691
4a64f543
MS
1692 /* Check first BL->ADDRESS will not overflow due to the added
1693 constant. Then advance the left boundary only if we are sure
1694 the BC element can in no way affect the BUF content (MEMADDR
1695 to MEMADDR + LEN range).
876fa593 1696
f5336ca5 1697 Use the BP_LOCATIONS_SHADOW_LEN_AFTER_ADDRESS_MAX safety
4a64f543
MS
1698 offset so that we cannot miss a breakpoint with its shadow
1699 range tail still reaching MEMADDR. */
c5aa993b 1700
f5336ca5 1701 if ((bl->address + bp_locations_shadow_len_after_address_max
35df4500 1702 >= bl->address)
f5336ca5 1703 && (bl->address + bp_locations_shadow_len_after_address_max
35df4500 1704 <= memaddr))
876fa593
JK
1705 bc_l = bc;
1706 else
1707 bc_r = bc;
1708 }
1709
128070bb
PA
1710 /* Due to the binary search above, we need to make sure we pick the
1711 first location that's at BC_L's address. E.g., if there are
1712 multiple locations at the same address, BC_L may end up pointing
1713 at a duplicate location, and miss the "master"/"inserted"
1714 location. Say, given locations L1, L2 and L3 at addresses A and
1715 B:
1716
1717 L1@A, L2@A, L3@B, ...
1718
1719 BC_L could end up pointing at location L2, while the "master"
1720 location could be L1. Since the `loc->inserted' flag is only set
1721 on "master" locations, we'd forget to restore the shadow of L1
1722 and L2. */
1723 while (bc_l > 0
f5336ca5 1724 && bp_locations[bc_l]->address == bp_locations[bc_l - 1]->address)
128070bb
PA
1725 bc_l--;
1726
876fa593
JK
1727 /* Now do full processing of the found relevant range of elements. */
1728
5d51cd5d 1729 for (bc = bc_l; bc < bp_locations.size (); bc++)
01add95b
SM
1730 {
1731 struct bp_location *bl = bp_locations[bc];
876fa593 1732
01add95b
SM
1733 /* bp_location array has BL->OWNER always non-NULL. */
1734 if (bl->owner->type == bp_none)
1735 warning (_("reading through apparently deleted breakpoint #%d?"),
1736 bl->owner->number);
ffce0d52 1737
01add95b
SM
1738 /* Performance optimization: any further element can no longer affect BUF
1739 content. */
876fa593 1740
01add95b
SM
1741 if (bl->address >= bp_locations_placed_address_before_address_max
1742 && (memaddr + len
1743 <= (bl->address
1744 - bp_locations_placed_address_before_address_max)))
1745 break;
876fa593 1746
01add95b
SM
1747 if (!bp_location_has_shadow (bl))
1748 continue;
6c95b8df 1749
01add95b
SM
1750 one_breakpoint_xfer_memory (readbuf, writebuf, writebuf_org,
1751 memaddr, len, &bl->target_info, bl->gdbarch);
1752 }
c906108c 1753}
9d497a19 1754
f2478a7e 1755/* See breakpoint.h. */
b775012e 1756
f2478a7e 1757bool
b775012e
LM
1758is_breakpoint (const struct breakpoint *bpt)
1759{
1760 return (bpt->type == bp_breakpoint
e7e0cddf
SS
1761 || bpt->type == bp_hardware_breakpoint
1762 || bpt->type == bp_dprintf);
b775012e
LM
1763}
1764
60e1c644
PA
1765/* Return true if BPT is of any hardware watchpoint kind. */
1766
f2478a7e 1767static bool
d77f58be 1768is_hardware_watchpoint (const struct breakpoint *bpt)
a5606eee
VP
1769{
1770 return (bpt->type == bp_hardware_watchpoint
1771 || bpt->type == bp_read_watchpoint
1772 || bpt->type == bp_access_watchpoint);
1773}
7270d8f2 1774
f2478a7e 1775/* See breakpoint.h. */
60e1c644 1776
f2478a7e 1777bool
d77f58be 1778is_watchpoint (const struct breakpoint *bpt)
60e1c644
PA
1779{
1780 return (is_hardware_watchpoint (bpt)
1781 || bpt->type == bp_watchpoint);
1782}
1783
3a5c3e22
PA
1784/* Returns true if the current thread and its running state are safe
1785 to evaluate or update watchpoint B. Watchpoints on local
1786 expressions need to be evaluated in the context of the thread that
1787 was current when the watchpoint was created, and, that thread needs
1788 to be stopped to be able to select the correct frame context.
1789 Watchpoints on global expressions can be evaluated on any thread,
1790 and in any state. It is presently left to the target allowing
1791 memory accesses when threads are running. */
f6bc2008
PA
1792
1793static int
3a5c3e22 1794watchpoint_in_thread_scope (struct watchpoint *b)
f6bc2008 1795{
c1fc2657 1796 return (b->pspace == current_program_space
d7e15655
TT
1797 && (b->watchpoint_thread == null_ptid
1798 || (inferior_ptid == b->watchpoint_thread
611841bb 1799 && !inferior_thread ()->executing ())));
f6bc2008
PA
1800}
1801
d0fb5eae
JK
1802/* Set watchpoint B to disp_del_at_next_stop, even including its possible
1803 associated bp_watchpoint_scope breakpoint. */
1804
1805static void
3a5c3e22 1806watchpoint_del_at_next_stop (struct watchpoint *w)
d0fb5eae 1807{
c1fc2657 1808 if (w->related_breakpoint != w)
d0fb5eae 1809 {
c1fc2657
SM
1810 gdb_assert (w->related_breakpoint->type == bp_watchpoint_scope);
1811 gdb_assert (w->related_breakpoint->related_breakpoint == w);
1812 w->related_breakpoint->disposition = disp_del_at_next_stop;
1813 w->related_breakpoint->related_breakpoint = w->related_breakpoint;
1814 w->related_breakpoint = w;
d0fb5eae 1815 }
c1fc2657 1816 w->disposition = disp_del_at_next_stop;
d0fb5eae
JK
1817}
1818
bb9d5f81
PP
1819/* Extract a bitfield value from value VAL using the bit parameters contained in
1820 watchpoint W. */
1821
1822static struct value *
1823extract_bitfield_from_watchpoint_value (struct watchpoint *w, struct value *val)
1824{
1825 struct value *bit_val;
1826
1827 if (val == NULL)
1828 return NULL;
1829
1830 bit_val = allocate_value (value_type (val));
1831
1832 unpack_value_bitfield (bit_val,
1833 w->val_bitpos,
1834 w->val_bitsize,
50888e42 1835 value_contents_for_printing (val).data (),
bb9d5f81
PP
1836 value_offset (val),
1837 val);
1838
1839 return bit_val;
1840}
1841
c6d81124
PA
1842/* Allocate a dummy location and add it to B, which must be a software
1843 watchpoint. This is required because even if a software watchpoint
1844 is not watching any memory, bpstat_stop_status requires a location
1845 to be able to report stops. */
1846
1847static void
1848software_watchpoint_add_no_memory_location (struct breakpoint *b,
1849 struct program_space *pspace)
1850{
1851 gdb_assert (b->type == bp_watchpoint && b->loc == NULL);
1852
ee89d0a4 1853 b->loc = b->allocate_location ();
c6d81124
PA
1854 b->loc->pspace = pspace;
1855 b->loc->address = -1;
1856 b->loc->length = -1;
1857}
1858
1859/* Returns true if B is a software watchpoint that is not watching any
1860 memory (e.g., "watch $pc"). */
1861
f2478a7e 1862static bool
c6d81124
PA
1863is_no_memory_software_watchpoint (struct breakpoint *b)
1864{
1865 return (b->type == bp_watchpoint
1866 && b->loc != NULL
1867 && b->loc->next == NULL
1868 && b->loc->address == -1
1869 && b->loc->length == -1);
1870}
1871
567e1b4e
JB
1872/* Assuming that B is a watchpoint:
1873 - Reparse watchpoint expression, if REPARSE is non-zero
a5606eee 1874 - Evaluate expression and store the result in B->val
567e1b4e
JB
1875 - Evaluate the condition if there is one, and store the result
1876 in b->loc->cond.
a5606eee
VP
1877 - Update the list of values that must be watched in B->loc.
1878
4a64f543
MS
1879 If the watchpoint disposition is disp_del_at_next_stop, then do
1880 nothing. If this is local watchpoint that is out of scope, delete
1881 it.
1882
1883 Even with `set breakpoint always-inserted on' the watchpoints are
1884 removed + inserted on each stop here. Normal breakpoints must
1885 never be removed because they might be missed by a running thread
1886 when debugging in non-stop mode. On the other hand, hardware
1887 watchpoints (is_hardware_watchpoint; processed here) are specific
1888 to each LWP since they are stored in each LWP's hardware debug
1889 registers. Therefore, such LWP must be stopped first in order to
1890 be able to modify its hardware watchpoints.
1891
1892 Hardware watchpoints must be reset exactly once after being
1893 presented to the user. It cannot be done sooner, because it would
1894 reset the data used to present the watchpoint hit to the user. And
1895 it must not be done later because it could display the same single
1896 watchpoint hit during multiple GDB stops. Note that the latter is
1897 relevant only to the hardware watchpoint types bp_read_watchpoint
1898 and bp_access_watchpoint. False hit by bp_hardware_watchpoint is
1899 not user-visible - its hit is suppressed if the memory content has
1900 not changed.
1901
1902 The following constraints influence the location where we can reset
1903 hardware watchpoints:
1904
1905 * target_stopped_by_watchpoint and target_stopped_data_address are
1906 called several times when GDB stops.
1907
1908 [linux]
1909 * Multiple hardware watchpoints can be hit at the same time,
1910 causing GDB to stop. GDB only presents one hardware watchpoint
1911 hit at a time as the reason for stopping, and all the other hits
1912 are presented later, one after the other, each time the user
1913 requests the execution to be resumed. Execution is not resumed
1914 for the threads still having pending hit event stored in
1915 LWP_INFO->STATUS. While the watchpoint is already removed from
1916 the inferior on the first stop the thread hit event is kept being
1917 reported from its cached value by linux_nat_stopped_data_address
1918 until the real thread resume happens after the watchpoint gets
1919 presented and thus its LWP_INFO->STATUS gets reset.
1920
1921 Therefore the hardware watchpoint hit can get safely reset on the
1922 watchpoint removal from inferior. */
a79d3c27 1923
b40ce68a 1924static void
3a5c3e22 1925update_watchpoint (struct watchpoint *b, int reparse)
7270d8f2 1926{
a5606eee 1927 int within_current_scope;
a5606eee 1928 struct frame_id saved_frame_id;
66076460 1929 int frame_saved;
a5606eee 1930
f6bc2008
PA
1931 /* If this is a local watchpoint, we only want to check if the
1932 watchpoint frame is in scope if the current thread is the thread
1933 that was used to create the watchpoint. */
1934 if (!watchpoint_in_thread_scope (b))
1935 return;
1936
c1fc2657 1937 if (b->disposition == disp_del_at_next_stop)
a5606eee
VP
1938 return;
1939
66076460 1940 frame_saved = 0;
a5606eee
VP
1941
1942 /* Determine if the watchpoint is within scope. */
1943 if (b->exp_valid_block == NULL)
1944 within_current_scope = 1;
1945 else
1946 {
b5db5dfc
UW
1947 struct frame_info *fi = get_current_frame ();
1948 struct gdbarch *frame_arch = get_frame_arch (fi);
1949 CORE_ADDR frame_pc = get_frame_pc (fi);
1950
c9cf6e20
MG
1951 /* If we're at a point where the stack has been destroyed
1952 (e.g. in a function epilogue), unwinding may not work
1953 properly. Do not attempt to recreate locations at this
b5db5dfc 1954 point. See similar comments in watchpoint_check. */
c9cf6e20 1955 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
b5db5dfc 1956 return;
66076460
DJ
1957
1958 /* Save the current frame's ID so we can restore it after
dda83cd7 1959 evaluating the watchpoint expression on its own frame. */
66076460 1960 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
dda83cd7
SM
1961 took a frame parameter, so that we didn't have to change the
1962 selected frame. */
66076460
DJ
1963 frame_saved = 1;
1964 saved_frame_id = get_frame_id (get_selected_frame (NULL));
1965
a5606eee
VP
1966 fi = frame_find_by_id (b->watchpoint_frame);
1967 within_current_scope = (fi != NULL);
1968 if (within_current_scope)
1969 select_frame (fi);
1970 }
1971
b5db5dfc
UW
1972 /* We don't free locations. They are stored in the bp_location array
1973 and update_global_location_list will eventually delete them and
1974 remove breakpoints if needed. */
c1fc2657 1975 b->loc = NULL;
b5db5dfc 1976
a5606eee
VP
1977 if (within_current_scope && reparse)
1978 {
bbc13ae3 1979 const char *s;
d63d0675 1980
4d01a485 1981 b->exp.reset ();
a4c50be3
TT
1982 s = (b->exp_string_reparse
1983 ? b->exp_string_reparse.get ()
1984 : b->exp_string.get ());
1bb9788d 1985 b->exp = parse_exp_1 (&s, 0, b->exp_valid_block, 0);
a5606eee
VP
1986 /* If the meaning of expression itself changed, the old value is
1987 no longer relevant. We don't want to report a watchpoint hit
1988 to the user when the old value and the new value may actually
1989 be completely different objects. */
fa4727a6 1990 b->val = NULL;
4c1d86d9 1991 b->val_valid = false;
60e1c644
PA
1992
1993 /* Note that unlike with breakpoints, the watchpoint's condition
1994 expression is stored in the breakpoint object, not in the
1995 locations (re)created below. */
c1fc2657 1996 if (b->cond_string != NULL)
60e1c644 1997 {
4d01a485 1998 b->cond_exp.reset ();
60e1c644 1999
6f781ee3 2000 s = b->cond_string.get ();
1bb9788d 2001 b->cond_exp = parse_exp_1 (&s, 0, b->cond_exp_valid_block, 0);
60e1c644 2002 }
a5606eee 2003 }
a5606eee
VP
2004
2005 /* If we failed to parse the expression, for example because
2006 it refers to a global variable in a not-yet-loaded shared library,
2007 don't try to insert watchpoint. We don't automatically delete
2008 such watchpoint, though, since failure to parse expression
2009 is different from out-of-scope watchpoint. */
55f6301a 2010 if (!target_has_execution ())
2d134ed3
PA
2011 {
2012 /* Without execution, memory can't change. No use to try and
2013 set watchpoint locations. The watchpoint will be reset when
2014 the target gains execution, through breakpoint_re_set. */
e8369a73
AB
2015 if (!can_use_hw_watchpoints)
2016 {
c47614fe 2017 if (b->works_in_software_mode ())
c1fc2657 2018 b->type = bp_watchpoint;
e8369a73 2019 else
638aa5a1
AB
2020 error (_("Can't set read/access watchpoint when "
2021 "hardware watchpoints are disabled."));
e8369a73 2022 }
2d134ed3
PA
2023 }
2024 else if (within_current_scope && b->exp)
a5606eee 2025 {
a6535de1 2026 std::vector<value_ref_ptr> val_chain;
8d49165d 2027 struct value *v, *result;
2d134ed3 2028 struct program_space *frame_pspace;
a5606eee 2029
1eaebe02 2030 fetch_subexp_value (b->exp.get (), b->exp->op.get (), &v, &result,
413403fc 2031 &val_chain, false);
a5606eee 2032
a5606eee
VP
2033 /* Avoid setting b->val if it's already set. The meaning of
2034 b->val is 'the last value' user saw, and we should update
2035 it only if we reported that last value to user. As it
9c06b0b4
TJB
2036 happens, the code that reports it updates b->val directly.
2037 We don't keep track of the memory value for masked
2038 watchpoints. */
c1fc2657 2039 if (!b->val_valid && !is_masked_watchpoint (b))
fa4727a6 2040 {
bb9d5f81 2041 if (b->val_bitsize != 0)
850645cf
TT
2042 v = extract_bitfield_from_watchpoint_value (b, v);
2043 b->val = release_value (v);
4c1d86d9 2044 b->val_valid = true;
fa4727a6 2045 }
a5606eee 2046
2d134ed3
PA
2047 frame_pspace = get_frame_program_space (get_selected_frame (NULL));
2048
a5606eee 2049 /* Look at each value on the value chain. */
a6535de1
TT
2050 gdb_assert (!val_chain.empty ());
2051 for (const value_ref_ptr &iter : val_chain)
a5606eee 2052 {
a6535de1
TT
2053 v = iter.get ();
2054
a5606eee
VP
2055 /* If it's a memory location, and GDB actually needed
2056 its contents to evaluate the expression, then we
fa4727a6
DJ
2057 must watch it. If the first value returned is
2058 still lazy, that means an error occurred reading it;
2059 watch it anyway in case it becomes readable. */
a5606eee 2060 if (VALUE_LVAL (v) == lval_memory
a6535de1 2061 && (v == val_chain[0] || ! value_lazy (v)))
a5606eee
VP
2062 {
2063 struct type *vtype = check_typedef (value_type (v));
7270d8f2 2064
a5606eee
VP
2065 /* We only watch structs and arrays if user asked
2066 for it explicitly, never if they just happen to
2067 appear in the middle of some value chain. */
fa4727a6 2068 if (v == result
78134374
SM
2069 || (vtype->code () != TYPE_CODE_STRUCT
2070 && vtype->code () != TYPE_CODE_ARRAY))
a5606eee
VP
2071 {
2072 CORE_ADDR addr;
f486487f 2073 enum target_hw_bp_type type;
a5606eee 2074 struct bp_location *loc, **tmp;
bb9d5f81
PP
2075 int bitpos = 0, bitsize = 0;
2076
2077 if (value_bitsize (v) != 0)
2078 {
2079 /* Extract the bit parameters out from the bitfield
2080 sub-expression. */
2081 bitpos = value_bitpos (v);
2082 bitsize = value_bitsize (v);
2083 }
2084 else if (v == result && b->val_bitsize != 0)
2085 {
2086 /* If VAL_BITSIZE != 0 then RESULT is actually a bitfield
2087 lvalue whose bit parameters are saved in the fields
2088 VAL_BITPOS and VAL_BITSIZE. */
2089 bitpos = b->val_bitpos;
2090 bitsize = b->val_bitsize;
2091 }
a5606eee 2092
42ae5230 2093 addr = value_address (v);
bb9d5f81
PP
2094 if (bitsize != 0)
2095 {
2096 /* Skip the bytes that don't contain the bitfield. */
2097 addr += bitpos / 8;
2098 }
2099
a5606eee 2100 type = hw_write;
c1fc2657 2101 if (b->type == bp_read_watchpoint)
a5606eee 2102 type = hw_read;
c1fc2657 2103 else if (b->type == bp_access_watchpoint)
a5606eee 2104 type = hw_access;
3a5c3e22 2105
ee89d0a4 2106 loc = b->allocate_location ();
c1fc2657 2107 for (tmp = &(b->loc); *tmp != NULL; tmp = &((*tmp)->next))
a5606eee
VP
2108 ;
2109 *tmp = loc;
8ee511af 2110 loc->gdbarch = value_type (v)->arch ();
6c95b8df
PA
2111
2112 loc->pspace = frame_pspace;
f17d9474 2113 loc->address = address_significant (loc->gdbarch, addr);
bb9d5f81
PP
2114
2115 if (bitsize != 0)
2116 {
2117 /* Just cover the bytes that make up the bitfield. */
2118 loc->length = ((bitpos % 8) + bitsize + 7) / 8;
2119 }
2120 else
2121 loc->length = TYPE_LENGTH (value_type (v));
2122
a5606eee
VP
2123 loc->watchpoint_type = type;
2124 }
2125 }
9fa40276
TJB
2126 }
2127
2128 /* Change the type of breakpoint between hardware assisted or
2129 an ordinary watchpoint depending on the hardware support
2130 and free hardware slots. REPARSE is set when the inferior
2131 is started. */
a9634178 2132 if (reparse)
9fa40276 2133 {
e09342b5 2134 int reg_cnt;
9fa40276 2135 enum bp_loc_type loc_type;
a5606eee 2136
a9634178 2137 reg_cnt = can_use_hardware_watchpoint (val_chain);
e09342b5
TJB
2138
2139 if (reg_cnt)
9fa40276
TJB
2140 {
2141 int i, target_resources_ok, other_type_used;
a1398e0c 2142 enum bptype type;
9fa40276 2143
a9634178
TJB
2144 /* Use an exact watchpoint when there's only one memory region to be
2145 watched, and only one debug register is needed to watch it. */
2146 b->exact = target_exact_watchpoints && reg_cnt == 1;
2147
9fa40276 2148 /* We need to determine how many resources are already
e09342b5
TJB
2149 used for all other hardware watchpoints plus this one
2150 to see if we still have enough resources to also fit
a1398e0c
PA
2151 this watchpoint in as well. */
2152
2153 /* If this is a software watchpoint, we try to turn it
2154 to a hardware one -- count resources as if B was of
2155 hardware watchpoint type. */
c1fc2657 2156 type = b->type;
a1398e0c
PA
2157 if (type == bp_watchpoint)
2158 type = bp_hardware_watchpoint;
2159
2160 /* This watchpoint may or may not have been placed on
2161 the list yet at this point (it won't be in the list
2162 if we're trying to create it for the first time,
2163 through watch_command), so always account for it
2164 manually. */
2165
2166 /* Count resources used by all watchpoints except B. */
c1fc2657 2167 i = hw_watchpoint_used_count_others (b, type, &other_type_used);
a1398e0c
PA
2168
2169 /* Add in the resources needed for B. */
c1fc2657 2170 i += hw_watchpoint_use_count (b);
a1398e0c
PA
2171
2172 target_resources_ok
2173 = target_can_use_hardware_watchpoint (type, i, other_type_used);
e09342b5 2174 if (target_resources_ok <= 0)
a9634178 2175 {
5a61e176 2176 bool sw_mode = b->works_in_software_mode ();
9c06b0b4
TJB
2177
2178 if (target_resources_ok == 0 && !sw_mode)
a9634178
TJB
2179 error (_("Target does not support this type of "
2180 "hardware watchpoint."));
9c06b0b4
TJB
2181 else if (target_resources_ok < 0 && !sw_mode)
2182 error (_("There are not enough available hardware "
2183 "resources for this watchpoint."));
a1398e0c
PA
2184
2185 /* Downgrade to software watchpoint. */
c1fc2657 2186 b->type = bp_watchpoint;
a1398e0c
PA
2187 }
2188 else
2189 {
2190 /* If this was a software watchpoint, we've just
2191 found we have enough resources to turn it to a
2192 hardware watchpoint. Otherwise, this is a
2193 nop. */
c1fc2657 2194 b->type = type;
a9634178 2195 }
9fa40276 2196 }
c47614fe 2197 else if (!b->works_in_software_mode ())
638aa5a1
AB
2198 {
2199 if (!can_use_hw_watchpoints)
2200 error (_("Can't set read/access watchpoint when "
2201 "hardware watchpoints are disabled."));
2202 else
2203 error (_("Expression cannot be implemented with "
2204 "read/access watchpoint."));
2205 }
9fa40276 2206 else
c1fc2657 2207 b->type = bp_watchpoint;
9fa40276 2208
c1fc2657 2209 loc_type = (b->type == bp_watchpoint? bp_loc_other
9fa40276 2210 : bp_loc_hardware_watchpoint);
40cb8ca5 2211 for (bp_location *bl : b->locations ())
9fa40276
TJB
2212 bl->loc_type = loc_type;
2213 }
2214
c7437ca6
PA
2215 /* If a software watchpoint is not watching any memory, then the
2216 above left it without any location set up. But,
2217 bpstat_stop_status requires a location to be able to report
2218 stops, so make sure there's at least a dummy one. */
c1fc2657
SM
2219 if (b->type == bp_watchpoint && b->loc == NULL)
2220 software_watchpoint_add_no_memory_location (b, frame_pspace);
a5606eee
VP
2221 }
2222 else if (!within_current_scope)
7270d8f2 2223 {
6cb06a8c 2224 gdb_printf (_("\
ac74f770
MS
2225Watchpoint %d deleted because the program has left the block\n\
2226in which its expression is valid.\n"),
6cb06a8c 2227 b->number);
d0fb5eae 2228 watchpoint_del_at_next_stop (b);
7270d8f2 2229 }
a5606eee
VP
2230
2231 /* Restore the selected frame. */
66076460
DJ
2232 if (frame_saved)
2233 select_frame (frame_find_by_id (saved_frame_id));
7270d8f2
OF
2234}
2235
a5606eee 2236
74960c60 2237/* Returns 1 iff breakpoint location should be
1e4d1764
YQ
2238 inserted in the inferior. We don't differentiate the type of BL's owner
2239 (breakpoint vs. tracepoint), although insert_location in tracepoint's
2240 breakpoint_ops is not defined, because in insert_bp_location,
2241 tracepoint's insert_location will not be called. */
74960c60 2242static int
35df4500 2243should_be_inserted (struct bp_location *bl)
74960c60 2244{
35df4500 2245 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
74960c60
VP
2246 return 0;
2247
35df4500 2248 if (bl->owner->disposition == disp_del_at_next_stop)
74960c60
VP
2249 return 0;
2250
b5fa468f
TBA
2251 if (!bl->enabled || bl->disabled_by_cond
2252 || bl->shlib_disabled || bl->duplicate)
74960c60
VP
2253 return 0;
2254
f8eba3c6
TT
2255 if (user_breakpoint_p (bl->owner) && bl->pspace->executing_startup)
2256 return 0;
2257
56710373
PA
2258 /* This is set for example, when we're attached to the parent of a
2259 vfork, and have detached from the child. The child is running
2260 free, and we expect it to do an exec or exit, at which point the
2261 OS makes the parent schedulable again (and the target reports
2262 that the vfork is done). Until the child is done with the shared
2263 memory region, do not insert breakpoints in the parent, otherwise
2264 the child could still trip on the parent's breakpoints. Since
2265 the parent is blocked anyway, it won't miss any breakpoint. */
35df4500 2266 if (bl->pspace->breakpoints_not_allowed)
56710373
PA
2267 return 0;
2268
31e77af2 2269 /* Don't insert a breakpoint if we're trying to step past its
21edc42f
YQ
2270 location, except if the breakpoint is a single-step breakpoint,
2271 and the breakpoint's thread is the thread which is stepping past
2272 a breakpoint. */
31e77af2
PA
2273 if ((bl->loc_type == bp_loc_software_breakpoint
2274 || bl->loc_type == bp_loc_hardware_breakpoint)
2275 && stepping_past_instruction_at (bl->pspace->aspace,
21edc42f
YQ
2276 bl->address)
2277 /* The single-step breakpoint may be inserted at the location
2278 we're trying to step if the instruction branches to itself.
2279 However, the instruction won't be executed at all and it may
2280 break the semantics of the instruction, for example, the
2281 instruction is a conditional branch or updates some flags.
2282 We can't fix it unless GDB is able to emulate the instruction
2283 or switch to displaced stepping. */
2284 && !(bl->owner->type == bp_single_step
2285 && thread_is_stepping_over_breakpoint (bl->owner->thread)))
e558d7c1 2286 {
1eb8556f
SM
2287 infrun_debug_printf ("skipping breakpoint: stepping past insn at: %s",
2288 paddress (bl->gdbarch, bl->address));
e558d7c1
PA
2289 return 0;
2290 }
31e77af2 2291
963f9c80
PA
2292 /* Don't insert watchpoints if we're trying to step past the
2293 instruction that triggered one. */
2294 if ((bl->loc_type == bp_loc_hardware_watchpoint)
2295 && stepping_past_nonsteppable_watchpoint ())
2296 {
1eb8556f 2297 infrun_debug_printf ("stepping past non-steppable watchpoint. "
ae1f4d2d 2298 "skipping watchpoint at %s:%d",
1eb8556f 2299 paddress (bl->gdbarch, bl->address), bl->length);
963f9c80
PA
2300 return 0;
2301 }
2302
74960c60
VP
2303 return 1;
2304}
2305
934709f0
PW
2306/* Same as should_be_inserted but does the check assuming
2307 that the location is not duplicated. */
2308
2309static int
2310unduplicated_should_be_inserted (struct bp_location *bl)
2311{
2312 int result;
2313 const int save_duplicate = bl->duplicate;
2314
2315 bl->duplicate = 0;
2316 result = should_be_inserted (bl);
2317 bl->duplicate = save_duplicate;
2318 return result;
2319}
2320
b775012e
LM
2321/* Parses a conditional described by an expression COND into an
2322 agent expression bytecode suitable for evaluation
2323 by the bytecode interpreter. Return NULL if there was
2324 any error during parsing. */
2325
833177a4 2326static agent_expr_up
b775012e
LM
2327parse_cond_to_aexpr (CORE_ADDR scope, struct expression *cond)
2328{
833177a4 2329 if (cond == NULL)
b775012e
LM
2330 return NULL;
2331
833177a4
PA
2332 agent_expr_up aexpr;
2333
b775012e
LM
2334 /* We don't want to stop processing, so catch any errors
2335 that may show up. */
a70b8144 2336 try
b775012e 2337 {
036e657b 2338 aexpr = gen_eval_for_expr (scope, cond);
b775012e
LM
2339 }
2340
230d2906 2341 catch (const gdb_exception_error &ex)
b775012e
LM
2342 {
2343 /* If we got here, it means the condition could not be parsed to a valid
2344 bytecode expression and thus can't be evaluated on the target's side.
2345 It's no use iterating through the conditions. */
b775012e
LM
2346 }
2347
2348 /* We have a valid agent expression. */
2349 return aexpr;
2350}
2351
2352/* Based on location BL, create a list of breakpoint conditions to be
2353 passed on to the target. If we have duplicated locations with different
2354 conditions, we will add such conditions to the list. The idea is that the
2355 target will evaluate the list of conditions and will only notify GDB when
2356 one of them is true. */
2357
2358static void
2359build_target_condition_list (struct bp_location *bl)
2360{
b775012e
LM
2361 int null_condition_or_parse_error = 0;
2362 int modified = bl->needs_update;
b775012e 2363
8b4f3082 2364 /* Release conditions left over from a previous insert. */
3cde5c42 2365 bl->target_info.conditions.clear ();
8b4f3082 2366
b775012e
LM
2367 /* This is only meaningful if the target is
2368 evaluating conditions and if the user has
2369 opted for condition evaluation on the target's
2370 side. */
2371 if (gdb_evaluates_breakpoint_condition_p ()
2372 || !target_supports_evaluation_of_breakpoint_conditions ())
2373 return;
2374
e0d9a270
SM
2375 auto loc_range = all_bp_locations_at_addr (bl->address);
2376
b775012e 2377 /* Do a first pass to check for locations with no assigned
7f32a4d5
PA
2378 conditions or conditions that fail to parse to a valid agent
2379 expression bytecode. If any of these happen, then it's no use to
2380 send conditions to the target since this location will always
2381 trigger and generate a response back to GDB. Note we consider
2382 all locations at the same address irrespective of type, i.e.,
2383 even if the locations aren't considered duplicates (e.g.,
2384 software breakpoint and hardware breakpoint at the same
2385 address). */
e0d9a270 2386 for (bp_location *loc : loc_range)
b775012e 2387 {
b775012e
LM
2388 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2389 {
2390 if (modified)
2391 {
b775012e
LM
2392 /* Re-parse the conditions since something changed. In that
2393 case we already freed the condition bytecodes (see
2394 force_breakpoint_reinsertion). We just
2395 need to parse the condition to bytecodes again. */
833177a4
PA
2396 loc->cond_bytecode = parse_cond_to_aexpr (bl->address,
2397 loc->cond.get ());
b775012e
LM
2398 }
2399
2400 /* If we have a NULL bytecode expression, it means something
2401 went wrong or we have a null condition expression. */
2402 if (!loc->cond_bytecode)
2403 {
2404 null_condition_or_parse_error = 1;
2405 break;
2406 }
2407 }
2408 }
2409
2410 /* If any of these happened, it means we will have to evaluate the conditions
2411 for the location's address on gdb's side. It is no use keeping bytecodes
2412 for all the other duplicate locations, thus we free all of them here.
2413
2414 This is so we have a finer control over which locations' conditions are
2415 being evaluated by GDB or the remote stub. */
2416 if (null_condition_or_parse_error)
2417 {
e0d9a270 2418 for (bp_location *loc : loc_range)
b775012e 2419 {
b775012e
LM
2420 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2421 {
2422 /* Only go as far as the first NULL bytecode is
2423 located. */
2424 if (!loc->cond_bytecode)
2425 return;
2426
833177a4 2427 loc->cond_bytecode.reset ();
b775012e
LM
2428 }
2429 }
2430 }
2431
7f32a4d5
PA
2432 /* No NULL conditions or failed bytecode generation. Build a
2433 condition list for this location's address. If we have software
2434 and hardware locations at the same address, they aren't
2435 considered duplicates, but we still marge all the conditions
2436 anyway, as it's simpler, and doesn't really make a practical
2437 difference. */
e0d9a270
SM
2438 for (bp_location *loc : loc_range)
2439 if (loc->cond
2440 && is_breakpoint (loc->owner)
2441 && loc->pspace->num == bl->pspace->num
2442 && loc->owner->enable_state == bp_enabled
2443 && loc->enabled
2444 && !loc->disabled_by_cond)
2445 {
2446 /* Add the condition to the vector. This will be used later
2447 to send the conditions to the target. */
2448 bl->target_info.conditions.push_back (loc->cond_bytecode.get ());
2449 }
b775012e
LM
2450
2451 return;
2452}
2453
d3ce09f5
SS
2454/* Parses a command described by string CMD into an agent expression
2455 bytecode suitable for evaluation by the bytecode interpreter.
2456 Return NULL if there was any error during parsing. */
2457
833177a4 2458static agent_expr_up
d3ce09f5
SS
2459parse_cmd_to_aexpr (CORE_ADDR scope, char *cmd)
2460{
bbc13ae3
KS
2461 const char *cmdrest;
2462 const char *format_start, *format_end;
d3ce09f5
SS
2463 struct gdbarch *gdbarch = get_current_arch ();
2464
833177a4 2465 if (cmd == NULL)
d3ce09f5
SS
2466 return NULL;
2467
2468 cmdrest = cmd;
2469
2470 if (*cmdrest == ',')
2471 ++cmdrest;
f1735a53 2472 cmdrest = skip_spaces (cmdrest);
d3ce09f5
SS
2473
2474 if (*cmdrest++ != '"')
2475 error (_("No format string following the location"));
2476
2477 format_start = cmdrest;
2478
8e481c3b 2479 format_pieces fpieces (&cmdrest);
d3ce09f5
SS
2480
2481 format_end = cmdrest;
2482
2483 if (*cmdrest++ != '"')
2484 error (_("Bad format string, non-terminated '\"'."));
2485
f1735a53 2486 cmdrest = skip_spaces (cmdrest);
d3ce09f5
SS
2487
2488 if (!(*cmdrest == ',' || *cmdrest == '\0'))
2489 error (_("Invalid argument syntax"));
2490
2491 if (*cmdrest == ',')
2492 cmdrest++;
f1735a53 2493 cmdrest = skip_spaces (cmdrest);
d3ce09f5
SS
2494
2495 /* For each argument, make an expression. */
2496
8e481c3b 2497 std::vector<struct expression *> argvec;
d3ce09f5
SS
2498 while (*cmdrest != '\0')
2499 {
bbc13ae3 2500 const char *cmd1;
d3ce09f5
SS
2501
2502 cmd1 = cmdrest;
4d01a485 2503 expression_up expr = parse_exp_1 (&cmd1, scope, block_for_pc (scope), 1);
8e481c3b 2504 argvec.push_back (expr.release ());
d3ce09f5
SS
2505 cmdrest = cmd1;
2506 if (*cmdrest == ',')
2507 ++cmdrest;
2508 }
2509
833177a4
PA
2510 agent_expr_up aexpr;
2511
d3ce09f5
SS
2512 /* We don't want to stop processing, so catch any errors
2513 that may show up. */
a70b8144 2514 try
d3ce09f5 2515 {
036e657b
JB
2516 aexpr = gen_printf (scope, gdbarch, 0, 0,
2517 format_start, format_end - format_start,
8e481c3b 2518 argvec.size (), argvec.data ());
d3ce09f5 2519 }
230d2906 2520 catch (const gdb_exception_error &ex)
d3ce09f5
SS
2521 {
2522 /* If we got here, it means the command could not be parsed to a valid
2523 bytecode expression and thus can't be evaluated on the target's side.
2524 It's no use iterating through the other commands. */
d3ce09f5 2525 }
492d29ea 2526
d3ce09f5
SS
2527 /* We have a valid agent expression, return it. */
2528 return aexpr;
2529}
2530
2531/* Based on location BL, create a list of breakpoint commands to be
2532 passed on to the target. If we have duplicated locations with
2533 different commands, we will add any such to the list. */
2534
2535static void
2536build_target_command_list (struct bp_location *bl)
2537{
d3ce09f5
SS
2538 int null_command_or_parse_error = 0;
2539 int modified = bl->needs_update;
d3ce09f5 2540
3cde5c42
PA
2541 /* Clear commands left over from a previous insert. */
2542 bl->target_info.tcommands.clear ();
8b4f3082 2543
41fac0cf 2544 if (!target_can_run_breakpoint_commands ())
d3ce09f5
SS
2545 return;
2546
41fac0cf
PA
2547 /* For now, limit to agent-style dprintf breakpoints. */
2548 if (dprintf_style != dprintf_style_agent)
d3ce09f5
SS
2549 return;
2550
e0d9a270
SM
2551 auto loc_range = all_bp_locations_at_addr (bl->address);
2552
7f32a4d5
PA
2553 /* For now, if we have any location at the same address that isn't a
2554 dprintf, don't install the target-side commands, as that would
2555 make the breakpoint not be reported to the core, and we'd lose
41fac0cf 2556 control. */
e0d9a270
SM
2557 for (bp_location *loc : loc_range)
2558 if (is_breakpoint (loc->owner)
2559 && loc->pspace->num == bl->pspace->num
2560 && loc->owner->type != bp_dprintf)
2561 return;
41fac0cf 2562
d3ce09f5
SS
2563 /* Do a first pass to check for locations with no assigned
2564 conditions or conditions that fail to parse to a valid agent expression
2565 bytecode. If any of these happen, then it's no use to send conditions
2566 to the target since this location will always trigger and generate a
2567 response back to GDB. */
e0d9a270 2568 for (bp_location *loc : loc_range)
d3ce09f5 2569 {
d3ce09f5
SS
2570 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2571 {
2572 if (modified)
2573 {
d3ce09f5
SS
2574 /* Re-parse the commands since something changed. In that
2575 case we already freed the command bytecodes (see
2576 force_breakpoint_reinsertion). We just
2577 need to parse the command to bytecodes again. */
833177a4
PA
2578 loc->cmd_bytecode
2579 = parse_cmd_to_aexpr (bl->address,
6f781ee3 2580 loc->owner->extra_string.get ());
d3ce09f5
SS
2581 }
2582
2583 /* If we have a NULL bytecode expression, it means something
2584 went wrong or we have a null command expression. */
2585 if (!loc->cmd_bytecode)
2586 {
2587 null_command_or_parse_error = 1;
2588 break;
2589 }
2590 }
2591 }
2592
2593 /* If anything failed, then we're not doing target-side commands,
2594 and so clean up. */
2595 if (null_command_or_parse_error)
2596 {
e0d9a270
SM
2597 for (bp_location *loc : loc_range)
2598 if (is_breakpoint (loc->owner)
2599 && loc->pspace->num == bl->pspace->num)
2600 {
2601 /* Only go as far as the first NULL bytecode is
2602 located. */
2603 if (loc->cmd_bytecode == NULL)
2604 return;
d3ce09f5 2605
e0d9a270
SM
2606 loc->cmd_bytecode.reset ();
2607 }
d3ce09f5
SS
2608 }
2609
7f32a4d5
PA
2610 /* No NULL commands or failed bytecode generation. Build a command
2611 list for all duplicate locations at this location's address.
2612 Note that here we must care for whether the breakpoint location
2613 types are considered duplicates, otherwise, say, if we have a
2614 software and hardware location at the same address, the target
2615 could end up running the commands twice. For the moment, we only
2616 support targets-side commands with dprintf, but it doesn't hurt
2617 to be pedantically correct in case that changes. */
e0d9a270
SM
2618 for (bp_location *loc : loc_range)
2619 if (breakpoint_locations_match (bl, loc)
2620 && loc->owner->extra_string
2621 && is_breakpoint (loc->owner)
2622 && loc->pspace->num == bl->pspace->num
2623 && loc->owner->enable_state == bp_enabled
2624 && loc->enabled
2625 && !loc->disabled_by_cond)
2626 {
2627 /* Add the command to the vector. This will be used later
2628 to send the commands to the target. */
2629 bl->target_info.tcommands.push_back (loc->cmd_bytecode.get ());
2630 }
d3ce09f5
SS
2631
2632 bl->target_info.persist = 0;
2633 /* Maybe flag this location as persistent. */
2634 if (bl->owner->type == bp_dprintf && disconnected_dprintf)
2635 bl->target_info.persist = 1;
2636}
2637
833b7ab5
YQ
2638/* Return the kind of breakpoint on address *ADDR. Get the kind
2639 of breakpoint according to ADDR except single-step breakpoint.
2640 Get the kind of single-step breakpoint according to the current
2641 registers state. */
cd6c3b4f
YQ
2642
2643static int
2644breakpoint_kind (struct bp_location *bl, CORE_ADDR *addr)
2645{
833b7ab5
YQ
2646 if (bl->owner->type == bp_single_step)
2647 {
2648 struct thread_info *thr = find_thread_global_id (bl->owner->thread);
2649 struct regcache *regcache;
2650
00431a78 2651 regcache = get_thread_regcache (thr);
833b7ab5
YQ
2652
2653 return gdbarch_breakpoint_kind_from_current_state (bl->gdbarch,
2654 regcache, addr);
2655 }
2656 else
2657 return gdbarch_breakpoint_kind_from_pc (bl->gdbarch, addr);
cd6c3b4f
YQ
2658}
2659
35df4500
TJB
2660/* Insert a low-level "breakpoint" of some type. BL is the breakpoint
2661 location. Any error messages are printed to TMP_ERROR_STREAM; and
3fbb6ffa 2662 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
c30eee59
TJB
2663 Returns 0 for success, 1 if the bp_location type is not supported or
2664 -1 for failure.
879bfdc2 2665
4a64f543
MS
2666 NOTE drow/2003-09-09: This routine could be broken down to an
2667 object-style method for each breakpoint or catchpoint type. */
26bb91f3 2668static int
35df4500 2669insert_bp_location (struct bp_location *bl,
26bb91f3 2670 struct ui_file *tmp_error_stream,
3fbb6ffa 2671 int *disabled_breaks,
dd61ec5c
MW
2672 int *hw_breakpoint_error,
2673 int *hw_bp_error_explained_already)
879bfdc2 2674{
cc06b668 2675 gdb_exception bp_excpt;
879bfdc2 2676
b775012e 2677 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
879bfdc2
DJ
2678 return 0;
2679
35c63cd8
JB
2680 /* Note we don't initialize bl->target_info, as that wipes out
2681 the breakpoint location's shadow_contents if the breakpoint
2682 is still inserted at that location. This in turn breaks
2683 target_read_memory which depends on these buffers when
2684 a memory read is requested at the breakpoint location:
2685 Once the target_info has been wiped, we fail to see that
2686 we have a breakpoint inserted at that address and thus
2687 read the breakpoint instead of returning the data saved in
2688 the breakpoint location's shadow contents. */
0d5ed153 2689 bl->target_info.reqstd_address = bl->address;
35df4500 2690 bl->target_info.placed_address_space = bl->pspace->aspace;
f1310107 2691 bl->target_info.length = bl->length;
8181d85f 2692
b775012e
LM
2693 /* When working with target-side conditions, we must pass all the conditions
2694 for the same breakpoint address down to the target since GDB will not
2695 insert those locations. With a list of breakpoint conditions, the target
2696 can decide when to stop and notify GDB. */
2697
2698 if (is_breakpoint (bl->owner))
2699 {
2700 build_target_condition_list (bl);
d3ce09f5
SS
2701 build_target_command_list (bl);
2702 /* Reset the modification marker. */
b775012e
LM
2703 bl->needs_update = 0;
2704 }
2705
7f32a4d5
PA
2706 /* If "set breakpoint auto-hw" is "on" and a software breakpoint was
2707 set at a read-only address, then a breakpoint location will have
2708 been changed to hardware breakpoint before we get here. If it is
2709 "off" however, error out before actually trying to insert the
2710 breakpoint, with a nicer error message. */
35df4500 2711 if (bl->loc_type == bp_loc_software_breakpoint
7f32a4d5 2712 && !automatic_hardware_breakpoints)
879bfdc2 2713 {
7f32a4d5 2714 mem_region *mr = lookup_mem_region (bl->address);
cc59ec59 2715
7f32a4d5
PA
2716 if (mr != nullptr && mr->attrib.mode != MEM_RW)
2717 {
6cb06a8c
TT
2718 gdb_printf (tmp_error_stream,
2719 _("Cannot insert breakpoint %d.\n"
2720 "Cannot set software breakpoint "
2721 "at read-only address %s\n"),
2722 bl->owner->number,
2723 paddress (bl->gdbarch, bl->address));
7f32a4d5 2724 return 1;
765dc015 2725 }
7f32a4d5
PA
2726 }
2727
2728 if (bl->loc_type == bp_loc_software_breakpoint
2729 || bl->loc_type == bp_loc_hardware_breakpoint)
2730 {
879bfdc2
DJ
2731 /* First check to see if we have to handle an overlay. */
2732 if (overlay_debugging == ovly_off
35df4500
TJB
2733 || bl->section == NULL
2734 || !(section_is_overlay (bl->section)))
879bfdc2
DJ
2735 {
2736 /* No overlay handling: just set the breakpoint. */
a70b8144 2737 try
dd61ec5c 2738 {
0000e5cc
PA
2739 int val;
2740
c47614fe 2741 val = bl->owner->insert_location (bl);
0000e5cc 2742 if (val)
688fca4f 2743 bp_excpt = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
dd61ec5c 2744 }
94aeb44b 2745 catch (gdb_exception &e)
dd61ec5c 2746 {
94aeb44b 2747 bp_excpt = std::move (e);
dd61ec5c 2748 }
879bfdc2
DJ
2749 }
2750 else
2751 {
4a64f543 2752 /* This breakpoint is in an overlay section.
879bfdc2
DJ
2753 Shall we set a breakpoint at the LMA? */
2754 if (!overlay_events_enabled)
2755 {
2756 /* Yes -- overlay event support is not active,
2757 so we must try to set a breakpoint at the LMA.
2758 This will not work for a hardware breakpoint. */
35df4500 2759 if (bl->loc_type == bp_loc_hardware_breakpoint)
8a3fe4f8 2760 warning (_("hardware breakpoint %d not supported in overlay!"),
35df4500 2761 bl->owner->number);
879bfdc2
DJ
2762 else
2763 {
35df4500
TJB
2764 CORE_ADDR addr = overlay_unmapped_address (bl->address,
2765 bl->section);
879bfdc2 2766 /* Set a software (trap) breakpoint at the LMA. */
35df4500 2767 bl->overlay_target_info = bl->target_info;
0d5ed153 2768 bl->overlay_target_info.reqstd_address = addr;
0000e5cc
PA
2769
2770 /* No overlay handling: just set the breakpoint. */
a70b8144 2771 try
0000e5cc
PA
2772 {
2773 int val;
2774
579c6ad9 2775 bl->overlay_target_info.kind
cd6c3b4f
YQ
2776 = breakpoint_kind (bl, &addr);
2777 bl->overlay_target_info.placed_address = addr;
0000e5cc
PA
2778 val = target_insert_breakpoint (bl->gdbarch,
2779 &bl->overlay_target_info);
2780 if (val)
688fca4f
PA
2781 bp_excpt
2782 = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
0000e5cc 2783 }
94aeb44b 2784 catch (gdb_exception &e)
0000e5cc 2785 {
94aeb44b 2786 bp_excpt = std::move (e);
0000e5cc
PA
2787 }
2788
688fca4f 2789 if (bp_excpt.reason != 0)
6cb06a8c
TT
2790 gdb_printf (tmp_error_stream,
2791 "Overlay breakpoint %d "
2792 "failed: in ROM?\n",
2793 bl->owner->number);
879bfdc2
DJ
2794 }
2795 }
2796 /* Shall we set a breakpoint at the VMA? */
35df4500 2797 if (section_is_mapped (bl->section))
879bfdc2
DJ
2798 {
2799 /* Yes. This overlay section is mapped into memory. */
a70b8144 2800 try
dda83cd7 2801 {
0000e5cc
PA
2802 int val;
2803
c47614fe 2804 val = bl->owner->insert_location (bl);
0000e5cc 2805 if (val)
688fca4f 2806 bp_excpt = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
dda83cd7 2807 }
94aeb44b 2808 catch (gdb_exception &e)
dda83cd7 2809 {
94aeb44b 2810 bp_excpt = std::move (e);
dda83cd7 2811 }
879bfdc2
DJ
2812 }
2813 else
2814 {
2815 /* No. This breakpoint will not be inserted.
2816 No error, but do not mark the bp as 'inserted'. */
2817 return 0;
2818 }
2819 }
2820
688fca4f 2821 if (bp_excpt.reason != 0)
879bfdc2
DJ
2822 {
2823 /* Can't set the breakpoint. */
0000e5cc 2824
80dc83fd
AB
2825 /* If the target has closed then it will have deleted any
2826 breakpoints inserted within the target inferior, as a result
2827 any further attempts to interact with the breakpoint objects
2828 is not possible. Just rethrow the error. */
2829 if (bp_excpt.error == TARGET_CLOSE_ERROR)
2830 throw bp_excpt;
2831 gdb_assert (bl->owner != nullptr);
2832
0000e5cc
PA
2833 /* In some cases, we might not be able to insert a
2834 breakpoint in a shared library that has already been
2835 removed, but we have not yet processed the shlib unload
2836 event. Unfortunately, some targets that implement
076855f9
PA
2837 breakpoint insertion themselves can't tell why the
2838 breakpoint insertion failed (e.g., the remote target
2839 doesn't define error codes), so we must treat generic
2840 errors as memory errors. */
688fca4f
PA
2841 if (bp_excpt.reason == RETURN_ERROR
2842 && (bp_excpt.error == GENERIC_ERROR
2843 || bp_excpt.error == MEMORY_ERROR)
076855f9 2844 && bl->loc_type == bp_loc_software_breakpoint
08351840 2845 && (solib_name_from_address (bl->pspace, bl->address)
d03de421
PA
2846 || shared_objfile_contains_address_p (bl->pspace,
2847 bl->address)))
879bfdc2 2848 {
4a64f543 2849 /* See also: disable_breakpoints_in_shlibs. */
35df4500 2850 bl->shlib_disabled = 1;
76727919 2851 gdb::observers::breakpoint_modified.notify (bl->owner);
3fbb6ffa
TJB
2852 if (!*disabled_breaks)
2853 {
6cb06a8c
TT
2854 gdb_printf (tmp_error_stream,
2855 "Cannot insert breakpoint %d.\n",
2856 bl->owner->number);
2857 gdb_printf (tmp_error_stream,
2858 "Temporarily disabling shared "
2859 "library breakpoints:\n");
3fbb6ffa
TJB
2860 }
2861 *disabled_breaks = 1;
6cb06a8c
TT
2862 gdb_printf (tmp_error_stream,
2863 "breakpoint #%d\n", bl->owner->number);
0000e5cc 2864 return 0;
879bfdc2
DJ
2865 }
2866 else
879bfdc2 2867 {
35df4500 2868 if (bl->loc_type == bp_loc_hardware_breakpoint)
879bfdc2 2869 {
0000e5cc 2870 *hw_breakpoint_error = 1;
688fca4f 2871 *hw_bp_error_explained_already = bp_excpt.message != NULL;
6cb06a8c
TT
2872 gdb_printf (tmp_error_stream,
2873 "Cannot insert hardware breakpoint %d%s",
2874 bl->owner->number,
2875 bp_excpt.message ? ":" : ".\n");
dda83cd7 2876 if (bp_excpt.message != NULL)
6cb06a8c
TT
2877 gdb_printf (tmp_error_stream, "%s.\n",
2878 bp_excpt.what ());
879bfdc2
DJ
2879 }
2880 else
2881 {
688fca4f 2882 if (bp_excpt.message == NULL)
0000e5cc 2883 {
1ccbe998 2884 std::string message
0000e5cc
PA
2885 = memory_error_message (TARGET_XFER_E_IO,
2886 bl->gdbarch, bl->address);
0000e5cc 2887
6cb06a8c
TT
2888 gdb_printf (tmp_error_stream,
2889 "Cannot insert breakpoint %d.\n"
2890 "%s\n",
2891 bl->owner->number, message.c_str ());
0000e5cc
PA
2892 }
2893 else
2894 {
6cb06a8c
TT
2895 gdb_printf (tmp_error_stream,
2896 "Cannot insert breakpoint %d: %s\n",
2897 bl->owner->number,
2898 bp_excpt.what ());
0000e5cc 2899 }
879bfdc2 2900 }
0000e5cc 2901 return 1;
879bfdc2
DJ
2902
2903 }
2904 }
2905 else
35df4500 2906 bl->inserted = 1;
879bfdc2 2907
0000e5cc 2908 return 0;
879bfdc2
DJ
2909 }
2910
35df4500 2911 else if (bl->loc_type == bp_loc_hardware_watchpoint
879bfdc2 2912 /* NOTE drow/2003-09-08: This state only exists for removing
4a64f543 2913 watchpoints. It's not clear that it's necessary... */
35df4500 2914 && bl->owner->disposition != disp_del_at_next_stop)
879bfdc2 2915 {
0000e5cc
PA
2916 int val;
2917
c47614fe 2918 val = bl->owner->insert_location (bl);
85d721b8
PA
2919
2920 /* If trying to set a read-watchpoint, and it turns out it's not
2921 supported, try emulating one with an access watchpoint. */
35df4500 2922 if (val == 1 && bl->watchpoint_type == hw_read)
85d721b8 2923 {
85d721b8
PA
2924 /* But don't try to insert it, if there's already another
2925 hw_access location that would be considered a duplicate
2926 of this one. */
48d7020b 2927 for (bp_location *loc : all_bp_locations ())
35df4500 2928 if (loc != bl
85d721b8 2929 && loc->watchpoint_type == hw_access
35df4500 2930 && watchpoint_locations_match (bl, loc))
85d721b8 2931 {
35df4500
TJB
2932 bl->duplicate = 1;
2933 bl->inserted = 1;
2934 bl->target_info = loc->target_info;
2935 bl->watchpoint_type = hw_access;
85d721b8
PA
2936 val = 0;
2937 break;
2938 }
2939
2940 if (val == 1)
2941 {
77b06cd7 2942 bl->watchpoint_type = hw_access;
c47614fe 2943 val = bl->owner->insert_location (bl);
77b06cd7
TJB
2944
2945 if (val)
2946 /* Back to the original value. */
2947 bl->watchpoint_type = hw_read;
85d721b8
PA
2948 }
2949 }
2950
35df4500 2951 bl->inserted = (val == 0);
879bfdc2
DJ
2952 }
2953
35df4500 2954 else if (bl->owner->type == bp_catchpoint)
879bfdc2 2955 {
0000e5cc
PA
2956 int val;
2957
c47614fe 2958 val = bl->owner->insert_location (bl);
77b06cd7
TJB
2959 if (val)
2960 {
2961 bl->owner->enable_state = bp_disabled;
2962
2963 if (val == 1)
2964 warning (_("\
2965Error inserting catchpoint %d: Your system does not support this type\n\
2966of catchpoint."), bl->owner->number);
2967 else
2968 warning (_("Error inserting catchpoint %d."), bl->owner->number);
2969 }
2970
2971 bl->inserted = (val == 0);
1640b821
DJ
2972
2973 /* We've already printed an error message if there was a problem
2974 inserting this catchpoint, and we've disabled the catchpoint,
2975 so just return success. */
2976 return 0;
879bfdc2
DJ
2977 }
2978
2979 return 0;
2980}
2981
6c95b8df
PA
2982/* This function is called when program space PSPACE is about to be
2983 deleted. It takes care of updating breakpoints to not reference
2984 PSPACE anymore. */
2985
2986void
2987breakpoint_program_space_exit (struct program_space *pspace)
2988{
6c95b8df 2989 /* Remove any breakpoint that was set through this program space. */
1428b37a
SM
2990 for (breakpoint *b : all_breakpoints_safe ())
2991 if (b->pspace == pspace)
2992 delete_breakpoint (b);
6c95b8df
PA
2993
2994 /* Breakpoints set through other program spaces could have locations
2995 bound to PSPACE as well. Remove those. */
48d7020b 2996 for (bp_location *loc : all_bp_locations ())
6c95b8df
PA
2997 {
2998 struct bp_location *tmp;
2999
3000 if (loc->pspace == pspace)
3001 {
2bdf28a0 3002 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
6c95b8df
PA
3003 if (loc->owner->loc == loc)
3004 loc->owner->loc = loc->next;
3005 else
3006 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
3007 if (tmp->next == loc)
3008 {
3009 tmp->next = loc->next;
3010 break;
3011 }
3012 }
3013 }
3014
3015 /* Now update the global location list to permanently delete the
3016 removed locations above. */
44702360 3017 update_global_location_list (UGLL_DONT_INSERT);
6c95b8df
PA
3018}
3019
74960c60
VP
3020/* Make sure all breakpoints are inserted in inferior.
3021 Throws exception on any error.
3022 A breakpoint that is already inserted won't be inserted
3023 again, so calling this function twice is safe. */
3024void
3025insert_breakpoints (void)
3026{
43892fdf 3027 for (breakpoint *bpt : all_breakpoints ())
74960c60 3028 if (is_hardware_watchpoint (bpt))
3a5c3e22
PA
3029 {
3030 struct watchpoint *w = (struct watchpoint *) bpt;
3031
3032 update_watchpoint (w, 0 /* don't reparse. */);
3033 }
74960c60 3034
04086b45
PA
3035 /* Updating watchpoints creates new locations, so update the global
3036 location list. Explicitly tell ugll to insert locations and
7f32a4d5
PA
3037 ignore breakpoints_always_inserted_mode. Also,
3038 update_global_location_list tries to "upgrade" software
3039 breakpoints to hardware breakpoints to handle "set breakpoint
3040 auto-hw", so we need to call it even if we don't have new
3041 locations. */
04086b45 3042 update_global_location_list (UGLL_INSERT);
74960c60
VP
3043}
3044
b775012e
LM
3045/* This is used when we need to synch breakpoint conditions between GDB and the
3046 target. It is the case with deleting and disabling of breakpoints when using
3047 always-inserted mode. */
3048
3049static void
3050update_inserted_breakpoint_locations (void)
3051{
b775012e
LM
3052 int error_flag = 0;
3053 int val = 0;
3054 int disabled_breaks = 0;
3055 int hw_breakpoint_error = 0;
dd61ec5c 3056 int hw_bp_details_reported = 0;
b775012e 3057
d7e74731 3058 string_file tmp_error_stream;
b775012e
LM
3059
3060 /* Explicitly mark the warning -- this will only be printed if
3061 there was an error. */
d7e74731 3062 tmp_error_stream.puts ("Warning:\n");
b775012e 3063
5ed8105e 3064 scoped_restore_current_pspace_and_thread restore_pspace_thread;
b775012e 3065
48d7020b 3066 for (bp_location *bl : all_bp_locations ())
b775012e
LM
3067 {
3068 /* We only want to update software breakpoints and hardware
3069 breakpoints. */
3070 if (!is_breakpoint (bl->owner))
3071 continue;
3072
3073 /* We only want to update locations that are already inserted
3074 and need updating. This is to avoid unwanted insertion during
3075 deletion of breakpoints. */
4daf1902 3076 if (!bl->inserted || !bl->needs_update)
b775012e
LM
3077 continue;
3078
3079 switch_to_program_space_and_thread (bl->pspace);
3080
3081 /* For targets that support global breakpoints, there's no need
3082 to select an inferior to insert breakpoint to. In fact, even
3083 if we aren't attached to any process yet, we should still
3084 insert breakpoints. */
f5656ead 3085 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
55f6301a 3086 && (inferior_ptid == null_ptid || !target_has_execution ()))
b775012e
LM
3087 continue;
3088
d7e74731 3089 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
dd61ec5c 3090 &hw_breakpoint_error, &hw_bp_details_reported);
b775012e
LM
3091 if (val)
3092 error_flag = val;
3093 }
3094
3095 if (error_flag)
3096 {
223ffa71 3097 target_terminal::ours_for_output ();
b775012e
LM
3098 error_stream (tmp_error_stream);
3099 }
b775012e
LM
3100}
3101
c30eee59 3102/* Used when starting or continuing the program. */
c906108c 3103
74960c60
VP
3104static void
3105insert_breakpoint_locations (void)
c906108c 3106{
eacd795a 3107 int error_flag = 0;
c906108c 3108 int val = 0;
3fbb6ffa 3109 int disabled_breaks = 0;
81d0cc19 3110 int hw_breakpoint_error = 0;
dd61ec5c 3111 int hw_bp_error_explained_already = 0;
c906108c 3112
d7e74731
PA
3113 string_file tmp_error_stream;
3114
81d0cc19
GS
3115 /* Explicitly mark the warning -- this will only be printed if
3116 there was an error. */
d7e74731 3117 tmp_error_stream.puts ("Warning:\n");
6c95b8df 3118
5ed8105e 3119 scoped_restore_current_pspace_and_thread restore_pspace_thread;
6c95b8df 3120
48d7020b 3121 for (bp_location *bl : all_bp_locations ())
879bfdc2 3122 {
b775012e 3123 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
879bfdc2
DJ
3124 continue;
3125
4a64f543
MS
3126 /* There is no point inserting thread-specific breakpoints if
3127 the thread no longer exists. ALL_BP_LOCATIONS bp_location
3128 has BL->OWNER always non-NULL. */
35df4500 3129 if (bl->owner->thread != -1
5d5658a1 3130 && !valid_global_thread_id (bl->owner->thread))
f365de73
AS
3131 continue;
3132
35df4500 3133 switch_to_program_space_and_thread (bl->pspace);
6c95b8df
PA
3134
3135 /* For targets that support global breakpoints, there's no need
3136 to select an inferior to insert breakpoint to. In fact, even
3137 if we aren't attached to any process yet, we should still
3138 insert breakpoints. */
f5656ead 3139 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
55f6301a 3140 && (inferior_ptid == null_ptid || !target_has_execution ()))
6c95b8df
PA
3141 continue;
3142
d7e74731 3143 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
dd61ec5c 3144 &hw_breakpoint_error, &hw_bp_error_explained_already);
879bfdc2 3145 if (val)
eacd795a 3146 error_flag = val;
879bfdc2 3147 }
c906108c 3148
4a64f543
MS
3149 /* If we failed to insert all locations of a watchpoint, remove
3150 them, as half-inserted watchpoint is of limited use. */
43892fdf 3151 for (breakpoint *bpt : all_breakpoints ())
a5606eee
VP
3152 {
3153 int some_failed = 0;
a5606eee
VP
3154
3155 if (!is_hardware_watchpoint (bpt))
3156 continue;
3157
d6b74ac4 3158 if (!breakpoint_enabled (bpt))
a5606eee 3159 continue;
74960c60
VP
3160
3161 if (bpt->disposition == disp_del_at_next_stop)
3162 continue;
a5606eee 3163
40cb8ca5 3164 for (bp_location *loc : bpt->locations ())
56710373 3165 if (!loc->inserted && should_be_inserted (loc))
a5606eee
VP
3166 {
3167 some_failed = 1;
3168 break;
3169 }
40cb8ca5 3170
a5606eee
VP
3171 if (some_failed)
3172 {
40cb8ca5 3173 for (bp_location *loc : bpt->locations ())
a5606eee 3174 if (loc->inserted)
834c0d03 3175 remove_breakpoint (loc);
a5606eee
VP
3176
3177 hw_breakpoint_error = 1;
d7e74731
PA
3178 tmp_error_stream.printf ("Could not insert "
3179 "hardware watchpoint %d.\n",
3180 bpt->number);
eacd795a 3181 error_flag = -1;
a5606eee
VP
3182 }
3183 }
3184
eacd795a 3185 if (error_flag)
81d0cc19
GS
3186 {
3187 /* If a hardware breakpoint or watchpoint was inserted, add a
dda83cd7 3188 message about possibly exhausted resources. */
dd61ec5c 3189 if (hw_breakpoint_error && !hw_bp_error_explained_already)
81d0cc19 3190 {
d7e74731 3191 tmp_error_stream.printf ("Could not insert hardware breakpoints:\n\
c6510018 3192You may have requested too many hardware breakpoints/watchpoints.\n");
81d0cc19 3193 }
223ffa71 3194 target_terminal::ours_for_output ();
81d0cc19
GS
3195 error_stream (tmp_error_stream);
3196 }
c906108c
SS
3197}
3198
c30eee59
TJB
3199/* Used when the program stops.
3200 Returns zero if successful, or non-zero if there was a problem
3201 removing a breakpoint location. */
3202
c906108c 3203int
fba45db2 3204remove_breakpoints (void)
c906108c 3205{
3a1bae8e 3206 int val = 0;
c906108c 3207
48d7020b 3208 for (bp_location *bl : all_bp_locations ())
1e4d1764 3209 if (bl->inserted && !is_tracepoint (bl->owner))
834c0d03 3210 val |= remove_breakpoint (bl);
01add95b 3211
3a1bae8e 3212 return val;
c906108c
SS
3213}
3214
49fa26b0
PA
3215/* When a thread exits, remove breakpoints that are related to
3216 that thread. */
3217
3218static void
3219remove_threaded_breakpoints (struct thread_info *tp, int silent)
3220{
1428b37a 3221 for (breakpoint *b : all_breakpoints_safe ())
49fa26b0 3222 {
5d5658a1 3223 if (b->thread == tp->global_num && user_breakpoint_p (b))
49fa26b0
PA
3224 {
3225 b->disposition = disp_del_at_next_stop;
3226
6cb06a8c 3227 gdb_printf (_("\
43792cf0 3228Thread-specific breakpoint %d deleted - thread %s no longer in the thread list.\n"),
6cb06a8c 3229 b->number, print_thread_id (tp));
49fa26b0
PA
3230
3231 /* Hide it from the user. */
3232 b->number = 0;
3233 }
3234 }
3235}
3236
f3869b1a 3237/* See breakpoint.h. */
6c95b8df 3238
f3869b1a 3239void
00431a78 3240remove_breakpoints_inf (inferior *inf)
6c95b8df 3241{
6c95b8df 3242 int val;
6c95b8df 3243
48d7020b 3244 for (bp_location *bl : all_bp_locations ())
01add95b
SM
3245 {
3246 if (bl->pspace != inf->pspace)
3247 continue;
6c95b8df 3248
01add95b
SM
3249 if (bl->inserted && !bl->target_info.persist)
3250 {
3251 val = remove_breakpoint (bl);
3252 if (val != 0)
3253 return;
3254 }
3255 }
6c95b8df
PA
3256}
3257
e58b0e63
PA
3258static int internal_breakpoint_number = -1;
3259
84f4c1fe
PM
3260/* Set the breakpoint number of B, depending on the value of INTERNAL.
3261 If INTERNAL is non-zero, the breakpoint number will be populated
3262 from internal_breakpoint_number and that variable decremented.
e5dd4106 3263 Otherwise the breakpoint number will be populated from
84f4c1fe
PM
3264 breakpoint_count and that value incremented. Internal breakpoints
3265 do not set the internal var bpnum. */
3266static void
3267set_breakpoint_number (int internal, struct breakpoint *b)
3268{
3269 if (internal)
3270 b->number = internal_breakpoint_number--;
3271 else
3272 {
3273 set_breakpoint_count (breakpoint_count + 1);
3274 b->number = breakpoint_count;
3275 }
3276}
3277
e62c965a 3278static struct breakpoint *
a6d9a66e 3279create_internal_breakpoint (struct gdbarch *gdbarch,
9efa3c7f 3280 CORE_ADDR address, enum bptype type)
e62c965a 3281{
51abb421 3282 symtab_and_line sal;
e62c965a
PP
3283 sal.pc = address;
3284 sal.section = find_pc_overlay (sal.pc);
6c95b8df 3285 sal.pspace = current_program_space;
e62c965a 3286
9efa3c7f 3287 breakpoint *b = set_raw_breakpoint (gdbarch, sal, type);
e62c965a
PP
3288 b->number = internal_breakpoint_number--;
3289 b->disposition = disp_donttouch;
3290
3291 return b;
3292}
3293
17450429
PP
3294static const char *const longjmp_names[] =
3295 {
3296 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
3297 };
3298#define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
3299
3300/* Per-objfile data private to breakpoint.c. */
3301struct breakpoint_objfile_data
3302{
3303 /* Minimal symbol for "_ovly_debug_event" (if any). */
f6b3ad54 3304 struct bound_minimal_symbol overlay_msym;
17450429
PP
3305
3306 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
f6b3ad54 3307 struct bound_minimal_symbol longjmp_msym[NUM_LONGJMP_NAMES];
17450429 3308
28106bc2 3309 /* True if we have looked for longjmp probes. */
43dce439 3310 int longjmp_searched = 0;
28106bc2 3311
45461e0d
SM
3312 /* SystemTap probe points for longjmp (if any). These are non-owning
3313 references. */
3314 std::vector<probe *> longjmp_probes;
28106bc2 3315
17450429 3316 /* Minimal symbol for "std::terminate()" (if any). */
f6b3ad54 3317 struct bound_minimal_symbol terminate_msym;
17450429
PP
3318
3319 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
f6b3ad54 3320 struct bound_minimal_symbol exception_msym;
28106bc2
SDJ
3321
3322 /* True if we have looked for exception probes. */
43dce439 3323 int exception_searched = 0;
28106bc2 3324
45461e0d
SM
3325 /* SystemTap probe points for unwinding (if any). These are non-owning
3326 references. */
3327 std::vector<probe *> exception_probes;
17450429
PP
3328};
3329
51d3063a
TT
3330static const struct objfile_key<breakpoint_objfile_data>
3331 breakpoint_objfile_key;
17450429
PP
3332
3333/* Minimal symbol not found sentinel. */
3334static struct minimal_symbol msym_not_found;
3335
3336/* Returns TRUE if MSYM point to the "not found" sentinel. */
3337
3338static int
3339msym_not_found_p (const struct minimal_symbol *msym)
3340{
3341 return msym == &msym_not_found;
3342}
3343
3344/* Return per-objfile data needed by breakpoint.c.
3345 Allocate the data if necessary. */
3346
3347static struct breakpoint_objfile_data *
3348get_breakpoint_objfile_data (struct objfile *objfile)
3349{
3350 struct breakpoint_objfile_data *bp_objfile_data;
3351
51d3063a 3352 bp_objfile_data = breakpoint_objfile_key.get (objfile);
17450429 3353 if (bp_objfile_data == NULL)
51d3063a 3354 bp_objfile_data = breakpoint_objfile_key.emplace (objfile);
17450429
PP
3355 return bp_objfile_data;
3356}
3357
e62c965a 3358static void
af02033e 3359create_overlay_event_breakpoint (void)
e62c965a 3360{
af02033e 3361 const char *const func_name = "_ovly_debug_event";
e62c965a 3362
2030c079 3363 for (objfile *objfile : current_program_space->objfiles ())
69de3c6a
PP
3364 {
3365 struct breakpoint *b;
17450429
PP
3366 struct breakpoint_objfile_data *bp_objfile_data;
3367 CORE_ADDR addr;
67994074 3368 struct explicit_location explicit_loc;
69de3c6a 3369
17450429
PP
3370 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3371
3b7344d5 3372 if (msym_not_found_p (bp_objfile_data->overlay_msym.minsym))
17450429
PP
3373 continue;
3374
3b7344d5 3375 if (bp_objfile_data->overlay_msym.minsym == NULL)
17450429 3376 {
3b7344d5 3377 struct bound_minimal_symbol m;
17450429
PP
3378
3379 m = lookup_minimal_symbol_text (func_name, objfile);
3b7344d5 3380 if (m.minsym == NULL)
17450429
PP
3381 {
3382 /* Avoid future lookups in this objfile. */
3b7344d5 3383 bp_objfile_data->overlay_msym.minsym = &msym_not_found;
17450429
PP
3384 continue;
3385 }
3386 bp_objfile_data->overlay_msym = m;
3387 }
e62c965a 3388
4aeddc50 3389 addr = bp_objfile_data->overlay_msym.value_address ();
08feed99 3390 b = create_internal_breakpoint (objfile->arch (), addr,
9efa3c7f 3391 bp_overlay_event);
67994074
KS
3392 initialize_explicit_location (&explicit_loc);
3393 explicit_loc.function_name = ASTRDUP (func_name);
d28cd78a 3394 b->location = new_explicit_location (&explicit_loc);
e62c965a 3395
69de3c6a 3396 if (overlay_debugging == ovly_auto)
dda83cd7
SM
3397 {
3398 b->enable_state = bp_enabled;
3399 overlay_events_enabled = 1;
3400 }
69de3c6a
PP
3401 else
3402 {
dda83cd7
SM
3403 b->enable_state = bp_disabled;
3404 overlay_events_enabled = 0;
69de3c6a 3405 }
e62c965a 3406 }
e62c965a
PP
3407}
3408
2a7f6487
TV
3409/* Install a master longjmp breakpoint for OBJFILE using a probe. Return
3410 true if a breakpoint was installed. */
3411
3412static bool
3413create_longjmp_master_breakpoint_probe (objfile *objfile)
0fd8e87f 3414{
2a7f6487
TV
3415 struct gdbarch *gdbarch = objfile->arch ();
3416 struct breakpoint_objfile_data *bp_objfile_data
3417 = get_breakpoint_objfile_data (objfile);
0fd8e87f 3418
2a7f6487 3419 if (!bp_objfile_data->longjmp_searched)
94c93c35 3420 {
2a7f6487
TV
3421 std::vector<probe *> ret
3422 = find_probes_in_objfile (objfile, "libc", "longjmp");
af02033e 3423
2a7f6487 3424 if (!ret.empty ())
94c93c35 3425 {
2a7f6487
TV
3426 /* We are only interested in checking one element. */
3427 probe *p = ret[0];
0fd8e87f 3428
2a7f6487
TV
3429 if (!p->can_evaluate_arguments ())
3430 {
3431 /* We cannot use the probe interface here,
3432 because it does not know how to evaluate
3433 arguments. */
3434 ret.clear ();
3435 }
3436 }
3437 bp_objfile_data->longjmp_probes = ret;
3438 bp_objfile_data->longjmp_searched = 1;
3439 }
0fd8e87f 3440
2a7f6487
TV
3441 if (bp_objfile_data->longjmp_probes.empty ())
3442 return false;
17450429 3443
2a7f6487
TV
3444 for (probe *p : bp_objfile_data->longjmp_probes)
3445 {
3446 struct breakpoint *b;
25f9533e 3447
2a7f6487
TV
3448 b = create_internal_breakpoint (gdbarch,
3449 p->get_relocated_address (objfile),
9efa3c7f 3450 bp_longjmp_master);
2a7f6487
TV
3451 b->location = new_probe_location ("-probe-stap libc:longjmp");
3452 b->enable_state = bp_disabled;
3453 }
aed57c53 3454
2a7f6487
TV
3455 return true;
3456}
25f9533e 3457
2a7f6487
TV
3458/* Install master longjmp breakpoints for OBJFILE using longjmp_names.
3459 Return true if at least one breakpoint was installed. */
3460
3461static bool
3462create_longjmp_master_breakpoint_names (objfile *objfile)
3463{
3464 struct gdbarch *gdbarch = objfile->arch ();
3465 if (!gdbarch_get_longjmp_target_p (gdbarch))
3466 return false;
3467
3468 struct breakpoint_objfile_data *bp_objfile_data
3469 = get_breakpoint_objfile_data (objfile);
3470 unsigned int installed_bp = 0;
3471
3472 for (int i = 0; i < NUM_LONGJMP_NAMES; i++)
3473 {
3474 struct breakpoint *b;
3475 const char *func_name;
3476 CORE_ADDR addr;
3477 struct explicit_location explicit_loc;
3478
3479 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i].minsym))
3480 continue;
28106bc2 3481
2a7f6487
TV
3482 func_name = longjmp_names[i];
3483 if (bp_objfile_data->longjmp_msym[i].minsym == NULL)
3484 {
3485 struct bound_minimal_symbol m;
3486
3487 m = lookup_minimal_symbol_text (func_name, objfile);
3488 if (m.minsym == NULL)
3489 {
3490 /* Prevent future lookups in this objfile. */
3491 bp_objfile_data->longjmp_msym[i].minsym = &msym_not_found;
94c93c35
TT
3492 continue;
3493 }
2a7f6487
TV
3494 bp_objfile_data->longjmp_msym[i] = m;
3495 }
28106bc2 3496
4aeddc50 3497 addr = bp_objfile_data->longjmp_msym[i].value_address ();
9efa3c7f 3498 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master);
2a7f6487
TV
3499 initialize_explicit_location (&explicit_loc);
3500 explicit_loc.function_name = ASTRDUP (func_name);
3501 b->location = new_explicit_location (&explicit_loc);
3502 b->enable_state = bp_disabled;
3503 installed_bp++;
3504 }
28106bc2 3505
2a7f6487
TV
3506 return installed_bp > 0;
3507}
0fd8e87f 3508
2a7f6487 3509/* Create a master longjmp breakpoint. */
17450429 3510
2a7f6487
TV
3511static void
3512create_longjmp_master_breakpoint (void)
3513{
3514 scoped_restore_current_program_space restore_pspace;
aed57c53 3515
2a7f6487
TV
3516 for (struct program_space *pspace : program_spaces)
3517 {
3518 set_current_program_space (pspace);
17450429 3519
2a7f6487
TV
3520 for (objfile *obj : current_program_space->objfiles ())
3521 {
3522 /* Skip separate debug object, it's handled in the loop below. */
3523 if (obj->separate_debug_objfile_backlink != nullptr)
3524 continue;
3525
3526 /* Try a probe kind breakpoint on main objfile. */
3527 if (create_longjmp_master_breakpoint_probe (obj))
3528 continue;
3529
3530 /* Try longjmp_names kind breakpoints on main and separate_debug
3531 objfiles. */
3532 for (objfile *debug_objfile : obj->separate_debug_objfiles ())
3533 if (create_longjmp_master_breakpoint_names (debug_objfile))
3534 break;
94c93c35
TT
3535 }
3536 }
0fd8e87f
UW
3537}
3538
af02033e 3539/* Create a master std::terminate breakpoint. */
aa7d318d 3540static void
af02033e 3541create_std_terminate_master_breakpoint (void)
aa7d318d 3542{
af02033e 3543 const char *const func_name = "std::terminate()";
aa7d318d 3544
5ed8105e 3545 scoped_restore_current_program_space restore_pspace;
aa7d318d 3546
94c93c35
TT
3547 for (struct program_space *pspace : program_spaces)
3548 {
3549 CORE_ADDR addr;
17450429 3550
94c93c35 3551 set_current_program_space (pspace);
17450429 3552
94c93c35
TT
3553 for (objfile *objfile : current_program_space->objfiles ())
3554 {
3555 struct breakpoint *b;
3556 struct breakpoint_objfile_data *bp_objfile_data;
3557 struct explicit_location explicit_loc;
aa7d318d 3558
94c93c35 3559 bp_objfile_data = get_breakpoint_objfile_data (objfile);
aa7d318d 3560
94c93c35
TT
3561 if (msym_not_found_p (bp_objfile_data->terminate_msym.minsym))
3562 continue;
17450429 3563
94c93c35
TT
3564 if (bp_objfile_data->terminate_msym.minsym == NULL)
3565 {
3566 struct bound_minimal_symbol m;
17450429 3567
94c93c35 3568 m = lookup_minimal_symbol (func_name, NULL, objfile);
60f62e2b
SM
3569 if (m.minsym == NULL || (m.minsym->type () != mst_text
3570 && m.minsym->type () != mst_file_text))
94c93c35
TT
3571 {
3572 /* Prevent future lookups in this objfile. */
3573 bp_objfile_data->terminate_msym.minsym = &msym_not_found;
3574 continue;
3575 }
3576 bp_objfile_data->terminate_msym = m;
3577 }
aa7d318d 3578
4aeddc50 3579 addr = bp_objfile_data->terminate_msym.value_address ();
94c93c35 3580 b = create_internal_breakpoint (objfile->arch (), addr,
9efa3c7f 3581 bp_std_terminate_master);
94c93c35
TT
3582 initialize_explicit_location (&explicit_loc);
3583 explicit_loc.function_name = ASTRDUP (func_name);
3584 b->location = new_explicit_location (&explicit_loc);
3585 b->enable_state = bp_disabled;
3586 }
3587 }
aa7d318d
TT
3588}
3589
1940319c
TV
3590/* Install a master breakpoint on the unwinder's debug hook for OBJFILE using a
3591 probe. Return true if a breakpoint was installed. */
186c406b 3592
1940319c
TV
3593static bool
3594create_exception_master_breakpoint_probe (objfile *objfile)
186c406b 3595{
1940319c
TV
3596 struct breakpoint *b;
3597 struct gdbarch *gdbarch;
3598 struct breakpoint_objfile_data *bp_objfile_data;
186c406b 3599
1940319c 3600 bp_objfile_data = get_breakpoint_objfile_data (objfile);
17450429 3601
1940319c
TV
3602 /* We prefer the SystemTap probe point if it exists. */
3603 if (!bp_objfile_data->exception_searched)
3604 {
3605 std::vector<probe *> ret
3606 = find_probes_in_objfile (objfile, "libgcc", "unwind");
17450429 3607
1940319c 3608 if (!ret.empty ())
28106bc2 3609 {
1940319c
TV
3610 /* We are only interested in checking one element. */
3611 probe *p = ret[0];
25f9533e 3612
1940319c 3613 if (!p->can_evaluate_arguments ())
25f9533e 3614 {
1940319c
TV
3615 /* We cannot use the probe interface here, because it does
3616 not know how to evaluate arguments. */
3617 ret.clear ();
25f9533e 3618 }
28106bc2 3619 }
1940319c
TV
3620 bp_objfile_data->exception_probes = ret;
3621 bp_objfile_data->exception_searched = 1;
3622 }
28106bc2 3623
1940319c
TV
3624 if (bp_objfile_data->exception_probes.empty ())
3625 return false;
45461e0d 3626
1940319c 3627 gdbarch = objfile->arch ();
28106bc2 3628
1940319c
TV
3629 for (probe *p : bp_objfile_data->exception_probes)
3630 {
3631 b = create_internal_breakpoint (gdbarch,
3632 p->get_relocated_address (objfile),
9efa3c7f 3633 bp_exception_master);
1940319c
TV
3634 b->location = new_probe_location ("-probe-stap libgcc:unwind");
3635 b->enable_state = bp_disabled;
3636 }
28106bc2 3637
1940319c
TV
3638 return true;
3639}
28106bc2 3640
1940319c
TV
3641/* Install a master breakpoint on the unwinder's debug hook for OBJFILE using
3642 _Unwind_DebugHook. Return true if a breakpoint was installed. */
17450429 3643
1940319c
TV
3644static bool
3645create_exception_master_breakpoint_hook (objfile *objfile)
3646{
3647 const char *const func_name = "_Unwind_DebugHook";
3648 struct breakpoint *b;
3649 struct gdbarch *gdbarch;
3650 struct breakpoint_objfile_data *bp_objfile_data;
3651 CORE_ADDR addr;
3652 struct explicit_location explicit_loc;
186c406b 3653
1940319c 3654 bp_objfile_data = get_breakpoint_objfile_data (objfile);
186c406b 3655
1940319c
TV
3656 if (msym_not_found_p (bp_objfile_data->exception_msym.minsym))
3657 return false;
17450429 3658
1940319c
TV
3659 gdbarch = objfile->arch ();
3660
3661 if (bp_objfile_data->exception_msym.minsym == NULL)
3662 {
3663 struct bound_minimal_symbol debug_hook;
3664
3665 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
3666 if (debug_hook.minsym == NULL)
3667 {
3668 bp_objfile_data->exception_msym.minsym = &msym_not_found;
3669 return false;
186c406b 3670 }
17450429 3671
1940319c
TV
3672 bp_objfile_data->exception_msym = debug_hook;
3673 }
3674
4aeddc50 3675 addr = bp_objfile_data->exception_msym.value_address ();
328d42d8
SM
3676 addr = gdbarch_convert_from_func_ptr_addr
3677 (gdbarch, addr, current_inferior ()->top_target ());
9efa3c7f 3678 b = create_internal_breakpoint (gdbarch, addr, bp_exception_master);
1940319c
TV
3679 initialize_explicit_location (&explicit_loc);
3680 explicit_loc.function_name = ASTRDUP (func_name);
3681 b->location = new_explicit_location (&explicit_loc);
3682 b->enable_state = bp_disabled;
3683
3684 return true;
3685}
3686
3687/* Install a master breakpoint on the unwinder's debug hook. */
3688
3689static void
3690create_exception_master_breakpoint (void)
3691{
3692 for (objfile *obj : current_program_space->objfiles ())
3693 {
3694 /* Skip separate debug object. */
3695 if (obj->separate_debug_objfile_backlink)
3696 continue;
3697
3698 /* Try a probe kind breakpoint. */
3699 if (create_exception_master_breakpoint_probe (obj))
3700 continue;
3701
7c6944ab
TV
3702 /* Iterate over main and separate debug objects and try an
3703 _Unwind_DebugHook kind breakpoint. */
3704 for (objfile *debug_objfile : obj->separate_debug_objfiles ())
3705 if (create_exception_master_breakpoint_hook (debug_objfile))
1940319c 3706 break;
186c406b 3707 }
186c406b
TT
3708}
3709
9ef9e6a6
KS
3710/* Does B have a location spec? */
3711
3712static int
3713breakpoint_event_location_empty_p (const struct breakpoint *b)
3714{
d28cd78a 3715 return b->location != NULL && event_location_empty_p (b->location.get ());
9ef9e6a6
KS
3716}
3717
c906108c 3718void
fba45db2 3719update_breakpoints_after_exec (void)
c906108c 3720{
25b22b0a
PA
3721 /* We're about to delete breakpoints from GDB's lists. If the
3722 INSERTED flag is true, GDB will try to lift the breakpoints by
3723 writing the breakpoints' "shadow contents" back into memory. The
3724 "shadow contents" are NOT valid after an exec, so GDB should not
3725 do that. Instead, the target is responsible from marking
3726 breakpoints out as soon as it detects an exec. We don't do that
3727 here instead, because there may be other attempts to delete
3728 breakpoints after detecting an exec and before reaching here. */
48d7020b 3729 for (bp_location *bploc : all_bp_locations ())
6c95b8df
PA
3730 if (bploc->pspace == current_program_space)
3731 gdb_assert (!bploc->inserted);
c906108c 3732
1428b37a 3733 for (breakpoint *b : all_breakpoints_safe ())
01add95b
SM
3734 {
3735 if (b->pspace != current_program_space)
c5aa993b 3736 continue;
c906108c 3737
01add95b
SM
3738 /* Solib breakpoints must be explicitly reset after an exec(). */
3739 if (b->type == bp_shlib_event)
3740 {
3741 delete_breakpoint (b);
3742 continue;
3743 }
4efc6507 3744
01add95b
SM
3745 /* JIT breakpoints must be explicitly reset after an exec(). */
3746 if (b->type == bp_jit_event)
3747 {
3748 delete_breakpoint (b);
3749 continue;
3750 }
c4093a6a 3751
01add95b
SM
3752 /* Thread event breakpoints must be set anew after an exec(),
3753 as must overlay event and longjmp master breakpoints. */
3754 if (b->type == bp_thread_event || b->type == bp_overlay_event
3755 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
3756 || b->type == bp_exception_master)
3757 {
3758 delete_breakpoint (b);
3759 continue;
3760 }
c5aa993b 3761
01add95b
SM
3762 /* Step-resume breakpoints are meaningless after an exec(). */
3763 if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
3764 {
3765 delete_breakpoint (b);
3766 continue;
3767 }
7c16b83e 3768
01add95b
SM
3769 /* Just like single-step breakpoints. */
3770 if (b->type == bp_single_step)
3771 {
3772 delete_breakpoint (b);
3773 continue;
3774 }
611c83ae 3775
01add95b
SM
3776 /* Longjmp and longjmp-resume breakpoints are also meaningless
3777 after an exec. */
3778 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
3779 || b->type == bp_longjmp_call_dummy
3780 || b->type == bp_exception || b->type == bp_exception_resume)
3781 {
3782 delete_breakpoint (b);
3783 continue;
3784 }
ce78b96d 3785
01add95b
SM
3786 if (b->type == bp_catchpoint)
3787 {
3788 /* For now, none of the bp_catchpoint breakpoints need to
3789 do anything at this point. In the future, if some of
3790 the catchpoints need to something, we will need to add
3791 a new method, and call this method from here. */
3792 continue;
3793 }
c5aa993b 3794
01add95b
SM
3795 /* bp_finish is a special case. The only way we ought to be able
3796 to see one of these when an exec() has happened, is if the user
3797 caught a vfork, and then said "finish". Ordinarily a finish just
3798 carries them to the call-site of the current callee, by setting
3799 a temporary bp there and resuming. But in this case, the finish
3800 will carry them entirely through the vfork & exec.
3801
3802 We don't want to allow a bp_finish to remain inserted now. But
3803 we can't safely delete it, 'cause finish_command has a handle to
3804 the bp on a bpstat, and will later want to delete it. There's a
3805 chance (and I've seen it happen) that if we delete the bp_finish
3806 here, that its storage will get reused by the time finish_command
3807 gets 'round to deleting the "use to be a bp_finish" breakpoint.
3808 We really must allow finish_command to delete a bp_finish.
3809
3810 In the absence of a general solution for the "how do we know
3811 it's safe to delete something others may have handles to?"
3812 problem, what we'll do here is just uninsert the bp_finish, and
3813 let finish_command delete it.
3814
3815 (We know the bp_finish is "doomed" in the sense that it's
3816 momentary, and will be deleted as soon as finish_command sees
3817 the inferior stopped. So it doesn't matter that the bp's
3818 address is probably bogus in the new a.out, unlike e.g., the
3819 solib breakpoints.) */
3820
3821 if (b->type == bp_finish)
3822 {
3823 continue;
3824 }
3825
3826 /* Without a symbolic address, we have little hope of the
3827 pre-exec() address meaning the same thing in the post-exec()
3828 a.out. */
3829 if (breakpoint_event_location_empty_p (b))
3830 {
3831 delete_breakpoint (b);
3832 continue;
3833 }
3834 }
c906108c
SS
3835}
3836
3837int
d80ee84f 3838detach_breakpoints (ptid_t ptid)
c906108c 3839{
3a1bae8e 3840 int val = 0;
2989a365 3841 scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid);
6c95b8df 3842 struct inferior *inf = current_inferior ();
c5aa993b 3843
e99b03dc 3844 if (ptid.pid () == inferior_ptid.pid ())
8a3fe4f8 3845 error (_("Cannot detach breakpoints of inferior_ptid"));
c5aa993b 3846
6c95b8df 3847 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
d80ee84f 3848 inferior_ptid = ptid;
48d7020b 3849 for (bp_location *bl : all_bp_locations ())
01add95b
SM
3850 {
3851 if (bl->pspace != inf->pspace)
3852 continue;
3853
3854 /* This function must physically remove breakpoints locations
3855 from the specified ptid, without modifying the breakpoint
3856 package's state. Locations of type bp_loc_other are only
3857 maintained at GDB side. So, there is no need to remove
3858 these bp_loc_other locations. Moreover, removing these
3859 would modify the breakpoint package's state. */
3860 if (bl->loc_type == bp_loc_other)
3861 continue;
3862
3863 if (bl->inserted)
3864 val |= remove_breakpoint_1 (bl, DETACH_BREAKPOINT);
3865 }
d03285ec 3866
3a1bae8e 3867 return val;
c906108c
SS
3868}
3869
35df4500 3870/* Remove the breakpoint location BL from the current address space.
6c95b8df
PA
3871 Note that this is used to detach breakpoints from a child fork.
3872 When we get here, the child isn't in the inferior list, and neither
3873 do we have objects to represent its address space --- we should
35df4500 3874 *not* look at bl->pspace->aspace here. */
6c95b8df 3875
c906108c 3876static int
b2b6a7da 3877remove_breakpoint_1 (struct bp_location *bl, enum remove_bp_reason reason)
c906108c
SS
3878{
3879 int val;
c5aa993b 3880
35df4500
TJB
3881 /* BL is never in moribund_locations by our callers. */
3882 gdb_assert (bl->owner != NULL);
2bdf28a0 3883
74960c60
VP
3884 /* The type of none suggests that owner is actually deleted.
3885 This should not ever happen. */
35df4500 3886 gdb_assert (bl->owner->type != bp_none);
0bde7532 3887
35df4500
TJB
3888 if (bl->loc_type == bp_loc_software_breakpoint
3889 || bl->loc_type == bp_loc_hardware_breakpoint)
c906108c 3890 {
c02f5703
MS
3891 /* "Normal" instruction breakpoint: either the standard
3892 trap-instruction bp (bp_breakpoint), or a
3893 bp_hardware_breakpoint. */
3894
3895 /* First check to see if we have to handle an overlay. */
3896 if (overlay_debugging == ovly_off
35df4500
TJB
3897 || bl->section == NULL
3898 || !(section_is_overlay (bl->section)))
c02f5703
MS
3899 {
3900 /* No overlay handling: just remove the breakpoint. */
08351840
PA
3901
3902 /* If we're trying to uninsert a memory breakpoint that we
3903 know is set in a dynamic object that is marked
3904 shlib_disabled, then either the dynamic object was
3905 removed with "remove-symbol-file" or with
3906 "nosharedlibrary". In the former case, we don't know
3907 whether another dynamic object might have loaded over the
3908 breakpoint's address -- the user might well let us know
3909 about it next with add-symbol-file (the whole point of
d03de421 3910 add-symbol-file is letting the user manually maintain a
08351840
PA
3911 list of dynamically loaded objects). If we have the
3912 breakpoint's shadow memory, that is, this is a software
3913 breakpoint managed by GDB, check whether the breakpoint
3914 is still inserted in memory, to avoid overwriting wrong
3915 code with stale saved shadow contents. Note that HW
3916 breakpoints don't have shadow memory, as they're
3917 implemented using a mechanism that is not dependent on
3918 being able to modify the target's memory, and as such
3919 they should always be removed. */
3920 if (bl->shlib_disabled
3921 && bl->target_info.shadow_len != 0
3922 && !memory_validate_breakpoint (bl->gdbarch, &bl->target_info))
3923 val = 0;
3924 else
c47614fe 3925 val = bl->owner->remove_location (bl, reason);
c02f5703 3926 }
c906108c
SS
3927 else
3928 {
4a64f543 3929 /* This breakpoint is in an overlay section.
c02f5703
MS
3930 Did we set a breakpoint at the LMA? */
3931 if (!overlay_events_enabled)
3932 {
3933 /* Yes -- overlay event support is not active, so we
3934 should have set a breakpoint at the LMA. Remove it.
3935 */
c02f5703
MS
3936 /* Ignore any failures: if the LMA is in ROM, we will
3937 have already warned when we failed to insert it. */
35df4500
TJB
3938 if (bl->loc_type == bp_loc_hardware_breakpoint)
3939 target_remove_hw_breakpoint (bl->gdbarch,
3940 &bl->overlay_target_info);
c02f5703 3941 else
35df4500 3942 target_remove_breakpoint (bl->gdbarch,
73971819
PA
3943 &bl->overlay_target_info,
3944 reason);
c02f5703
MS
3945 }
3946 /* Did we set a breakpoint at the VMA?
3947 If so, we will have marked the breakpoint 'inserted'. */
35df4500 3948 if (bl->inserted)
c906108c 3949 {
c02f5703
MS
3950 /* Yes -- remove it. Previously we did not bother to
3951 remove the breakpoint if the section had been
3952 unmapped, but let's not rely on that being safe. We
3953 don't know what the overlay manager might do. */
aa67235e
UW
3954
3955 /* However, we should remove *software* breakpoints only
3956 if the section is still mapped, or else we overwrite
3957 wrong code with the saved shadow contents. */
348d480f
PA
3958 if (bl->loc_type == bp_loc_hardware_breakpoint
3959 || section_is_mapped (bl->section))
c47614fe 3960 val = bl->owner->remove_location (bl, reason);
aa67235e
UW
3961 else
3962 val = 0;
c906108c 3963 }
c02f5703
MS
3964 else
3965 {
3966 /* No -- not inserted, so no need to remove. No error. */
3967 val = 0;
3968 }
c906108c 3969 }
879d1e6b 3970
08351840
PA
3971 /* In some cases, we might not be able to remove a breakpoint in
3972 a shared library that has already been removed, but we have
3973 not yet processed the shlib unload event. Similarly for an
3974 unloaded add-symbol-file object - the user might not yet have
3975 had the chance to remove-symbol-file it. shlib_disabled will
3976 be set if the library/object has already been removed, but
3977 the breakpoint hasn't been uninserted yet, e.g., after
3978 "nosharedlibrary" or "remove-symbol-file" with breakpoints
3979 always-inserted mode. */
076855f9 3980 if (val
08351840
PA
3981 && (bl->loc_type == bp_loc_software_breakpoint
3982 && (bl->shlib_disabled
3983 || solib_name_from_address (bl->pspace, bl->address)
d03de421
PA
3984 || shared_objfile_contains_address_p (bl->pspace,
3985 bl->address))))
879d1e6b
UW
3986 val = 0;
3987
c906108c
SS
3988 if (val)
3989 return val;
b2b6a7da 3990 bl->inserted = (reason == DETACH_BREAKPOINT);
c906108c 3991 }
35df4500 3992 else if (bl->loc_type == bp_loc_hardware_watchpoint)
c906108c 3993 {
b2b6a7da 3994 bl->inserted = (reason == DETACH_BREAKPOINT);
c47614fe 3995 bl->owner->remove_location (bl, reason);
2e70b7b9 3996
c906108c 3997 /* Failure to remove any of the hardware watchpoints comes here. */
b2b6a7da 3998 if (reason == REMOVE_BREAKPOINT && bl->inserted)
8a3fe4f8 3999 warning (_("Could not remove hardware watchpoint %d."),
35df4500 4000 bl->owner->number);
c906108c 4001 }
35df4500 4002 else if (bl->owner->type == bp_catchpoint
dda83cd7
SM
4003 && breakpoint_enabled (bl->owner)
4004 && !bl->duplicate)
ce78b96d 4005 {
c47614fe 4006 val = bl->owner->remove_location (bl, reason);
ce78b96d
JB
4007 if (val)
4008 return val;
77b06cd7 4009
b2b6a7da 4010 bl->inserted = (reason == DETACH_BREAKPOINT);
ce78b96d 4011 }
c906108c
SS
4012
4013 return 0;
4014}
4015
6c95b8df 4016static int
834c0d03 4017remove_breakpoint (struct bp_location *bl)
6c95b8df 4018{
35df4500
TJB
4019 /* BL is never in moribund_locations by our callers. */
4020 gdb_assert (bl->owner != NULL);
2bdf28a0 4021
6c95b8df
PA
4022 /* The type of none suggests that owner is actually deleted.
4023 This should not ever happen. */
35df4500 4024 gdb_assert (bl->owner->type != bp_none);
6c95b8df 4025
5ed8105e 4026 scoped_restore_current_pspace_and_thread restore_pspace_thread;
6c95b8df 4027
35df4500 4028 switch_to_program_space_and_thread (bl->pspace);
6c95b8df 4029
5ed8105e 4030 return remove_breakpoint_1 (bl, REMOVE_BREAKPOINT);
6c95b8df
PA
4031}
4032
c906108c
SS
4033/* Clear the "inserted" flag in all breakpoints. */
4034
25b22b0a 4035void
fba45db2 4036mark_breakpoints_out (void)
c906108c 4037{
48d7020b 4038 for (bp_location *bl : all_bp_locations ())
66c4b3e8 4039 if (bl->pspace == current_program_space)
35df4500 4040 bl->inserted = 0;
c906108c
SS
4041}
4042
53a5351d
JM
4043/* Clear the "inserted" flag in all breakpoints and delete any
4044 breakpoints which should go away between runs of the program.
c906108c
SS
4045
4046 Plus other such housekeeping that has to be done for breakpoints
4047 between runs.
4048
53a5351d
JM
4049 Note: this function gets called at the end of a run (by
4050 generic_mourn_inferior) and when a run begins (by
4a64f543 4051 init_wait_for_inferior). */
c906108c
SS
4052
4053
4054
4055void
fba45db2 4056breakpoint_init_inferior (enum inf_context context)
c906108c 4057{
6c95b8df 4058 struct program_space *pspace = current_program_space;
c906108c 4059
50c71eaf
PA
4060 /* If breakpoint locations are shared across processes, then there's
4061 nothing to do. */
f5656ead 4062 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
50c71eaf
PA
4063 return;
4064
1a853c52 4065 mark_breakpoints_out ();
075f6582 4066
1428b37a 4067 for (breakpoint *b : all_breakpoints_safe ())
01add95b
SM
4068 {
4069 if (b->loc && b->loc->pspace != pspace)
4070 continue;
6c95b8df 4071
01add95b
SM
4072 switch (b->type)
4073 {
4074 case bp_call_dummy:
4075 case bp_longjmp_call_dummy:
c906108c 4076
01add95b
SM
4077 /* If the call dummy breakpoint is at the entry point it will
4078 cause problems when the inferior is rerun, so we better get
4079 rid of it. */
ab92d69b 4080
01add95b 4081 case bp_watchpoint_scope:
ab92d69b 4082
01add95b 4083 /* Also get rid of scope breakpoints. */
ab92d69b 4084
01add95b 4085 case bp_shlib_event:
ab92d69b 4086
01add95b
SM
4087 /* Also remove solib event breakpoints. Their addresses may
4088 have changed since the last time we ran the program.
4089 Actually we may now be debugging against different target;
4090 and so the solib backend that installed this breakpoint may
4091 not be used in by the target. E.g.,
ab92d69b 4092
01add95b
SM
4093 (gdb) file prog-linux
4094 (gdb) run # native linux target
4095 ...
4096 (gdb) kill
4097 (gdb) file prog-win.exe
4098 (gdb) tar rem :9999 # remote Windows gdbserver.
4099 */
c906108c 4100
01add95b 4101 case bp_step_resume:
f59f708a 4102
01add95b 4103 /* Also remove step-resume breakpoints. */
f59f708a 4104
01add95b 4105 case bp_single_step:
7c16b83e 4106
01add95b 4107 /* Also remove single-step breakpoints. */
7c16b83e 4108
01add95b
SM
4109 delete_breakpoint (b);
4110 break;
c906108c 4111
01add95b
SM
4112 case bp_watchpoint:
4113 case bp_hardware_watchpoint:
4114 case bp_read_watchpoint:
4115 case bp_access_watchpoint:
4116 {
4117 struct watchpoint *w = (struct watchpoint *) b;
c906108c 4118
01add95b
SM
4119 /* Likewise for watchpoints on local expressions. */
4120 if (w->exp_valid_block != NULL)
4121 delete_breakpoint (b);
4122 else
4123 {
4124 /* Get rid of existing locations, which are no longer
4125 valid. New ones will be created in
4126 update_watchpoint, when the inferior is restarted.
4127 The next update_global_location_list call will
4128 garbage collect them. */
4129 b->loc = NULL;
4130
4131 if (context == inf_starting)
4132 {
4133 /* Reset val field to force reread of starting value in
4134 insert_breakpoints. */
4135 w->val.reset (nullptr);
4136 w->val_valid = false;
4137 }
4138 }
4139 }
4140 break;
4141 default:
4142 break;
3a5c3e22 4143 }
01add95b 4144 }
1c5cfe86
PA
4145
4146 /* Get rid of the moribund locations. */
1123588c 4147 for (bp_location *bl : moribund_locations)
35df4500 4148 decref_bp_location (&bl);
1123588c 4149 moribund_locations.clear ();
c906108c
SS
4150}
4151
6c95b8df
PA
4152/* These functions concern about actual breakpoints inserted in the
4153 target --- to e.g. check if we need to do decr_pc adjustment or if
4154 we need to hop over the bkpt --- so we check for address space
4155 match, not program space. */
4156
c2c6d25f
JM
4157/* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
4158 exists at PC. It returns ordinary_breakpoint_here if it's an
4159 ordinary breakpoint, or permanent_breakpoint_here if it's a
4160 permanent breakpoint.
4161 - When continuing from a location with an ordinary breakpoint, we
4162 actually single step once before calling insert_breakpoints.
e5dd4106 4163 - When continuing from a location with a permanent breakpoint, we
c2c6d25f
JM
4164 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
4165 the target, to advance the PC past the breakpoint. */
c906108c 4166
c2c6d25f 4167enum breakpoint_here
accd0bcd 4168breakpoint_here_p (const address_space *aspace, CORE_ADDR pc)
c906108c 4169{
c2c6d25f 4170 int any_breakpoint_here = 0;
c906108c 4171
48d7020b 4172 for (bp_location *bl : all_bp_locations ())
075f6582 4173 {
35df4500
TJB
4174 if (bl->loc_type != bp_loc_software_breakpoint
4175 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
4176 continue;
4177
f1310107 4178 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
35df4500 4179 if ((breakpoint_enabled (bl->owner)
1a853c52 4180 || bl->permanent)
f1310107 4181 && breakpoint_location_address_match (bl, aspace, pc))
075f6582
DJ
4182 {
4183 if (overlay_debugging
35df4500
TJB
4184 && section_is_overlay (bl->section)
4185 && !section_is_mapped (bl->section))
075f6582 4186 continue; /* unmapped overlay -- can't be a match */
1a853c52 4187 else if (bl->permanent)
075f6582
DJ
4188 return permanent_breakpoint_here;
4189 else
4190 any_breakpoint_here = 1;
4191 }
4192 }
c906108c 4193
f486487f 4194 return any_breakpoint_here ? ordinary_breakpoint_here : no_breakpoint_here;
c906108c
SS
4195}
4196
d35ae833
PA
4197/* See breakpoint.h. */
4198
4199int
accd0bcd 4200breakpoint_in_range_p (const address_space *aspace,
d35ae833
PA
4201 CORE_ADDR addr, ULONGEST len)
4202{
48d7020b 4203 for (bp_location *bl : all_bp_locations ())
d35ae833
PA
4204 {
4205 if (bl->loc_type != bp_loc_software_breakpoint
4206 && bl->loc_type != bp_loc_hardware_breakpoint)
4207 continue;
4208
4209 if ((breakpoint_enabled (bl->owner)
4210 || bl->permanent)
4211 && breakpoint_location_address_range_overlap (bl, aspace,
4212 addr, len))
4213 {
4214 if (overlay_debugging
4215 && section_is_overlay (bl->section)
4216 && !section_is_mapped (bl->section))
4217 {
4218 /* Unmapped overlay -- can't be a match. */
4219 continue;
4220 }
4221
4222 return 1;
4223 }
4224 }
4225
4226 return 0;
4227}
4228
1c5cfe86
PA
4229/* Return true if there's a moribund breakpoint at PC. */
4230
4231int
accd0bcd 4232moribund_breakpoint_here_p (const address_space *aspace, CORE_ADDR pc)
1c5cfe86 4233{
1123588c 4234 for (bp_location *loc : moribund_locations)
f1310107 4235 if (breakpoint_location_address_match (loc, aspace, pc))
1c5cfe86
PA
4236 return 1;
4237
4238 return 0;
4239}
c2c6d25f 4240
f7ce857f
PA
4241/* Returns non-zero iff BL is inserted at PC, in address space
4242 ASPACE. */
4243
4244static int
4245bp_location_inserted_here_p (struct bp_location *bl,
accd0bcd 4246 const address_space *aspace, CORE_ADDR pc)
f7ce857f
PA
4247{
4248 if (bl->inserted
4249 && breakpoint_address_match (bl->pspace->aspace, bl->address,
4250 aspace, pc))
4251 {
4252 if (overlay_debugging
4253 && section_is_overlay (bl->section)
4254 && !section_is_mapped (bl->section))
4255 return 0; /* unmapped overlay -- can't be a match */
4256 else
4257 return 1;
4258 }
4259 return 0;
4260}
4261
a1fd2fa5 4262/* Returns non-zero iff there's a breakpoint inserted at PC. */
c906108c
SS
4263
4264int
accd0bcd 4265breakpoint_inserted_here_p (const address_space *aspace, CORE_ADDR pc)
c906108c 4266{
e0d9a270 4267 for (bp_location *bl : all_bp_locations_at_addr (pc))
c5aa993b 4268 {
35df4500
TJB
4269 if (bl->loc_type != bp_loc_software_breakpoint
4270 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
4271 continue;
4272
f7ce857f
PA
4273 if (bp_location_inserted_here_p (bl, aspace, pc))
4274 return 1;
c5aa993b 4275 }
c36b740a
VP
4276 return 0;
4277}
4278
a1fd2fa5
PA
4279/* This function returns non-zero iff there is a software breakpoint
4280 inserted at PC. */
c36b740a
VP
4281
4282int
accd0bcd 4283software_breakpoint_inserted_here_p (const address_space *aspace,
a1fd2fa5 4284 CORE_ADDR pc)
4fa8626c 4285{
e0d9a270 4286 for (bp_location *bl : all_bp_locations_at_addr (pc))
4fa8626c 4287 {
35df4500 4288 if (bl->loc_type != bp_loc_software_breakpoint)
4fa8626c
DJ
4289 continue;
4290
f7ce857f
PA
4291 if (bp_location_inserted_here_p (bl, aspace, pc))
4292 return 1;
4fa8626c
DJ
4293 }
4294
4295 return 0;
9c02b525
PA
4296}
4297
4298/* See breakpoint.h. */
4299
4300int
accd0bcd 4301hardware_breakpoint_inserted_here_p (const address_space *aspace,
9c02b525
PA
4302 CORE_ADDR pc)
4303{
e0d9a270 4304 for (bp_location *bl : all_bp_locations_at_addr (pc))
9c02b525 4305 {
9c02b525
PA
4306 if (bl->loc_type != bp_loc_hardware_breakpoint)
4307 continue;
4308
4309 if (bp_location_inserted_here_p (bl, aspace, pc))
4310 return 1;
4311 }
4312
4313 return 0;
4fa8626c
DJ
4314}
4315
9093389c 4316int
accd0bcd 4317hardware_watchpoint_inserted_in_range (const address_space *aspace,
9093389c
PA
4318 CORE_ADDR addr, ULONGEST len)
4319{
43892fdf 4320 for (breakpoint *bpt : all_breakpoints ())
9093389c 4321 {
9093389c
PA
4322 if (bpt->type != bp_hardware_watchpoint
4323 && bpt->type != bp_access_watchpoint)
4324 continue;
4325
4326 if (!breakpoint_enabled (bpt))
4327 continue;
4328
40cb8ca5 4329 for (bp_location *loc : bpt->locations ())
9093389c
PA
4330 if (loc->pspace->aspace == aspace && loc->inserted)
4331 {
4332 CORE_ADDR l, h;
4333
4334 /* Check for intersection. */
768adc05
PA
4335 l = std::max<CORE_ADDR> (loc->address, addr);
4336 h = std::min<CORE_ADDR> (loc->address + loc->length, addr + len);
9093389c
PA
4337 if (l < h)
4338 return 1;
4339 }
4340 }
4341 return 0;
4342}
c5aa993b 4343
f2478a7e 4344/* See breakpoint.h. */
c906108c 4345
f2478a7e
SM
4346bool
4347is_catchpoint (struct breakpoint *b)
c906108c 4348{
f2478a7e 4349 return (b->type == bp_catchpoint);
c906108c
SS
4350}
4351
c906108c
SS
4352/* Clear a bpstat so that it says we are not at any breakpoint.
4353 Also free any storage that is part of a bpstat. */
4354
4355void
313f3b21 4356bpstat_clear (bpstat **bsp)
c906108c 4357{
313f3b21
SM
4358 bpstat *p;
4359 bpstat *q;
c906108c
SS
4360
4361 if (bsp == 0)
4362 return;
4363 p = *bsp;
4364 while (p != NULL)
4365 {
4366 q = p->next;
04afa70c 4367 delete p;
c906108c
SS
4368 p = q;
4369 }
4370 *bsp = NULL;
4371}
4372
313f3b21 4373bpstat::bpstat (const bpstat &other)
04afa70c
TT
4374 : next (NULL),
4375 bp_location_at (other.bp_location_at),
4376 breakpoint_at (other.breakpoint_at),
4377 commands (other.commands),
04afa70c
TT
4378 print (other.print),
4379 stop (other.stop),
4380 print_it (other.print_it)
4381{
850645cf
TT
4382 if (other.old_val != NULL)
4383 old_val = release_value (value_copy (other.old_val.get ()));
04afa70c
TT
4384}
4385
c906108c
SS
4386/* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
4387 is part of the bpstat is copied as well. */
4388
313f3b21
SM
4389bpstat *
4390bpstat_copy (bpstat *bs)
c906108c 4391{
313f3b21
SM
4392 bpstat *p = nullptr;
4393 bpstat *tmp;
4394 bpstat *retval = nullptr;
c906108c
SS
4395
4396 if (bs == NULL)
4397 return bs;
4398
4399 for (; bs != NULL; bs = bs->next)
4400 {
313f3b21 4401 tmp = new bpstat (*bs);
31cc81e9 4402
c906108c
SS
4403 if (p == NULL)
4404 /* This is the first thing in the chain. */
4405 retval = tmp;
4406 else
4407 p->next = tmp;
4408 p = tmp;
4409 }
4410 p->next = NULL;
4411 return retval;
4412}
4413
4a64f543 4414/* Find the bpstat associated with this breakpoint. */
c906108c 4415
313f3b21
SM
4416bpstat *
4417bpstat_find_breakpoint (bpstat *bsp, struct breakpoint *breakpoint)
c906108c 4418{
c5aa993b
JM
4419 if (bsp == NULL)
4420 return NULL;
c906108c 4421
c5aa993b
JM
4422 for (; bsp != NULL; bsp = bsp->next)
4423 {
f431efe5 4424 if (bsp->breakpoint_at == breakpoint)
c5aa993b
JM
4425 return bsp;
4426 }
c906108c
SS
4427 return NULL;
4428}
4429
ab04a2af
TT
4430/* See breakpoint.h. */
4431
4c462cb0 4432bool
313f3b21 4433bpstat_explains_signal (bpstat *bsp, enum gdb_signal sig)
ab04a2af 4434{
ab04a2af
TT
4435 for (; bsp != NULL; bsp = bsp->next)
4436 {
427cd150
TT
4437 if (bsp->breakpoint_at == NULL)
4438 {
4439 /* A moribund location can never explain a signal other than
4440 GDB_SIGNAL_TRAP. */
4441 if (sig == GDB_SIGNAL_TRAP)
4c462cb0 4442 return true;
427cd150
TT
4443 }
4444 else
47591c29 4445 {
c47614fe 4446 if (bsp->breakpoint_at->explains_signal (sig))
4c462cb0 4447 return true;
47591c29 4448 }
ab04a2af
TT
4449 }
4450
4c462cb0 4451 return false;
ab04a2af
TT
4452}
4453
4a64f543
MS
4454/* Put in *NUM the breakpoint number of the first breakpoint we are
4455 stopped at. *BSP upon return is a bpstat which points to the
4456 remaining breakpoints stopped at (but which is not guaranteed to be
4457 good for anything but further calls to bpstat_num).
4458
8671a17b
PA
4459 Return 0 if passed a bpstat which does not indicate any breakpoints.
4460 Return -1 if stopped at a breakpoint that has been deleted since
4461 we set it.
4462 Return 1 otherwise. */
c906108c
SS
4463
4464int
313f3b21 4465bpstat_num (bpstat **bsp, int *num)
c906108c
SS
4466{
4467 struct breakpoint *b;
4468
4469 if ((*bsp) == NULL)
4470 return 0; /* No more breakpoint values */
8671a17b 4471
4a64f543
MS
4472 /* We assume we'll never have several bpstats that correspond to a
4473 single breakpoint -- otherwise, this function might return the
4474 same number more than once and this will look ugly. */
f431efe5 4475 b = (*bsp)->breakpoint_at;
8671a17b
PA
4476 *bsp = (*bsp)->next;
4477 if (b == NULL)
4478 return -1; /* breakpoint that's been deleted since */
4479
4480 *num = b->number; /* We have its number */
4481 return 1;
c906108c
SS
4482}
4483
e93ca019 4484/* See breakpoint.h. */
c906108c
SS
4485
4486void
e93ca019 4487bpstat_clear_actions (void)
c906108c 4488{
313f3b21 4489 bpstat *bs;
e93ca019 4490
00431a78 4491 if (inferior_ptid == null_ptid)
e93ca019
JK
4492 return;
4493
00431a78 4494 thread_info *tp = inferior_thread ();
e93ca019 4495 for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
c906108c 4496 {
d1b0a7bf 4497 bs->commands = NULL;
850645cf 4498 bs->old_val.reset (nullptr);
c906108c
SS
4499 }
4500}
4501
f3b1572e
PA
4502/* Called when a command is about to proceed the inferior. */
4503
4504static void
4505breakpoint_about_to_proceed (void)
4506{
d7e15655 4507 if (inferior_ptid != null_ptid)
f3b1572e
PA
4508 {
4509 struct thread_info *tp = inferior_thread ();
4510
4511 /* Allow inferior function calls in breakpoint commands to not
4512 interrupt the command list. When the call finishes
4513 successfully, the inferior will be standing at the same
4514 breakpoint as if nothing happened. */
16c381f0 4515 if (tp->control.in_infcall)
f3b1572e
PA
4516 return;
4517 }
4518
4519 breakpoint_proceeded = 1;
4520}
4521
abf85f46
JK
4522/* Return non-zero iff CMD as the first line of a command sequence is `silent'
4523 or its equivalent. */
4524
4525static int
4526command_line_is_silent (struct command_line *cmd)
4527{
4f45d445 4528 return cmd && (strcmp ("silent", cmd->line) == 0);
abf85f46
JK
4529}
4530
4a64f543
MS
4531/* Execute all the commands associated with all the breakpoints at
4532 this location. Any of these commands could cause the process to
4533 proceed beyond this point, etc. We look out for such changes by
4534 checking the global "breakpoint_proceeded" after each command.
c906108c 4535
347bddb7
PA
4536 Returns true if a breakpoint command resumed the inferior. In that
4537 case, it is the caller's responsibility to recall it again with the
4538 bpstat of the current thread. */
4539
4540static int
313f3b21 4541bpstat_do_actions_1 (bpstat **bsp)
c906108c 4542{
313f3b21 4543 bpstat *bs;
347bddb7 4544 int again = 0;
c906108c
SS
4545
4546 /* Avoid endless recursion if a `source' command is contained
4547 in bs->commands. */
4548 if (executing_breakpoint_commands)
347bddb7 4549 return 0;
c906108c 4550
81b1e71c
TT
4551 scoped_restore save_executing
4552 = make_scoped_restore (&executing_breakpoint_commands, 1);
c906108c 4553
1ac32117 4554 scoped_restore preventer = prevent_dont_repeat ();
cf6c5ffb 4555
4a64f543 4556 /* This pointer will iterate over the list of bpstat's. */
c906108c
SS
4557 bs = *bsp;
4558
4559 breakpoint_proceeded = 0;
4560 for (; bs != NULL; bs = bs->next)
4561 {
d1b0a7bf 4562 struct command_line *cmd = NULL;
6c50ab1c
JB
4563
4564 /* Take ownership of the BSP's command tree, if it has one.
4565
dda83cd7
SM
4566 The command tree could legitimately contain commands like
4567 'step' and 'next', which call clear_proceed_status, which
4568 frees stop_bpstat's command tree. To make sure this doesn't
4569 free the tree we're executing out from under us, we need to
4570 take ownership of the tree ourselves. Since a given bpstat's
4571 commands are only executed once, we don't need to copy it; we
4572 can clear the pointer in the bpstat, and make sure we free
4573 the tree when we're done. */
d1b0a7bf 4574 counted_command_line ccmd = bs->commands;
9add0f1b 4575 bs->commands = NULL;
d1b0a7bf
TT
4576 if (ccmd != NULL)
4577 cmd = ccmd.get ();
abf85f46
JK
4578 if (command_line_is_silent (cmd))
4579 {
4580 /* The action has been already done by bpstat_stop_status. */
4581 cmd = cmd->next;
4582 }
6c50ab1c 4583
c906108c
SS
4584 while (cmd != NULL)
4585 {
4586 execute_control_command (cmd);
4587
4588 if (breakpoint_proceeded)
4589 break;
4590 else
4591 cmd = cmd->next;
4592 }
6c50ab1c 4593
c906108c 4594 if (breakpoint_proceeded)
32c1e744 4595 {
cb814510 4596 if (current_ui->async)
347bddb7
PA
4597 /* If we are in async mode, then the target might be still
4598 running, not stopped at any breakpoint, so nothing for
4599 us to do here -- just return to the event loop. */
4600 ;
32c1e744
VP
4601 else
4602 /* In sync mode, when execute_control_command returns
4603 we're already standing on the next breakpoint.
347bddb7
PA
4604 Breakpoint commands for that stop were not run, since
4605 execute_command does not run breakpoint commands --
4606 only command_line_handler does, but that one is not
4607 involved in execution of breakpoint commands. So, we
4608 can now execute breakpoint commands. It should be
4609 noted that making execute_command do bpstat actions is
4610 not an option -- in this case we'll have recursive
4611 invocation of bpstat for each breakpoint with a
4612 command, and can easily blow up GDB stack. Instead, we
4613 return true, which will trigger the caller to recall us
4614 with the new stop_bpstat. */
4615 again = 1;
4616 break;
32c1e744 4617 }
c906108c 4618 }
347bddb7
PA
4619 return again;
4620}
4621
00431a78
PA
4622/* Helper for bpstat_do_actions. Get the current thread, if there's
4623 one, is alive and has execution. Return NULL otherwise. */
4624
4625static thread_info *
4626get_bpstat_thread ()
4627{
55f6301a 4628 if (inferior_ptid == null_ptid || !target_has_execution ())
00431a78
PA
4629 return NULL;
4630
4631 thread_info *tp = inferior_thread ();
611841bb 4632 if (tp->state == THREAD_EXITED || tp->executing ())
00431a78
PA
4633 return NULL;
4634 return tp;
4635}
4636
347bddb7
PA
4637void
4638bpstat_do_actions (void)
4639{
694c6bf5 4640 auto cleanup_if_error = make_scope_exit (bpstat_clear_actions);
00431a78 4641 thread_info *tp;
353d1d73 4642
347bddb7 4643 /* Do any commands attached to breakpoint we are stopped at. */
00431a78
PA
4644 while ((tp = get_bpstat_thread ()) != NULL)
4645 {
4646 /* Since in sync mode, bpstat_do_actions may resume the
4647 inferior, and only return when it is stopped at the next
4648 breakpoint, we keep doing breakpoint actions until it returns
4649 false to indicate the inferior was not resumed. */
4650 if (!bpstat_do_actions_1 (&tp->control.stop_bpstat))
4651 break;
4652 }
353d1d73 4653
694c6bf5 4654 cleanup_if_error.release ();
c906108c
SS
4655}
4656
fa4727a6
DJ
4657/* Print out the (old or new) value associated with a watchpoint. */
4658
4659static void
4660watchpoint_value_print (struct value *val, struct ui_file *stream)
4661{
4662 if (val == NULL)
7f6aba03 4663 fprintf_styled (stream, metadata_style.style (), _("<unreadable>"));
fa4727a6 4664 else
79a45b7d
TT
4665 {
4666 struct value_print_options opts;
4667 get_user_print_options (&opts);
4668 value_print (val, stream, &opts);
4669 }
fa4727a6
DJ
4670}
4671
f303dbd6
PA
4672/* Print the "Thread ID hit" part of "Thread ID hit Breakpoint N" if
4673 debugging multiple threads. */
4674
4675void
4676maybe_print_thread_hit_breakpoint (struct ui_out *uiout)
4677{
112e8700 4678 if (uiout->is_mi_like_p ())
f303dbd6
PA
4679 return;
4680
112e8700 4681 uiout->text ("\n");
f303dbd6
PA
4682
4683 if (show_thread_that_caused_stop ())
4684 {
f303dbd6
PA
4685 struct thread_info *thr = inferior_thread ();
4686
112e8700 4687 uiout->text ("Thread ");
33eca680 4688 uiout->field_string ("thread-id", print_thread_id (thr));
f303dbd6 4689
25558938 4690 const char *name = thread_name (thr);
f303dbd6
PA
4691 if (name != NULL)
4692 {
112e8700 4693 uiout->text (" \"");
33eca680 4694 uiout->field_string ("name", name);
112e8700 4695 uiout->text ("\"");
f303dbd6
PA
4696 }
4697
112e8700 4698 uiout->text (" hit ");
f303dbd6
PA
4699 }
4700}
4701
e514a9d6 4702/* Generic routine for printing messages indicating why we
4a64f543 4703 stopped. The behavior of this function depends on the value
e514a9d6
JM
4704 'print_it' in the bpstat structure. Under some circumstances we
4705 may decide not to print anything here and delegate the task to
4a64f543 4706 normal_stop(). */
e514a9d6
JM
4707
4708static enum print_stop_action
313f3b21 4709print_bp_stop_message (bpstat *bs)
e514a9d6
JM
4710{
4711 switch (bs->print_it)
4712 {
4713 case print_it_noop:
4a64f543 4714 /* Nothing should be printed for this bpstat entry. */
e514a9d6
JM
4715 return PRINT_UNKNOWN;
4716 break;
4717
4718 case print_it_done:
4719 /* We still want to print the frame, but we already printed the
dda83cd7 4720 relevant messages. */
e514a9d6
JM
4721 return PRINT_SRC_AND_LOC;
4722 break;
4723
4724 case print_it_normal:
4f8d1dc6 4725 {
f431efe5
PA
4726 struct breakpoint *b = bs->breakpoint_at;
4727
1a6a67de
TJB
4728 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
4729 which has since been deleted. */
4730 if (b == NULL)
4731 return PRINT_UNKNOWN;
4732
348d480f 4733 /* Normal case. Call the breakpoint's print_it method. */
c47614fe 4734 return b->print_it (bs);
4f8d1dc6 4735 }
348d480f 4736 break;
3086aeae 4737
e514a9d6 4738 default:
8e65ff28 4739 internal_error (__FILE__, __LINE__,
e2e0b3e5 4740 _("print_bp_stop_message: unrecognized enum value"));
e514a9d6 4741 break;
c906108c 4742 }
c906108c
SS
4743}
4744
2f9ee862 4745/* See breakpoint.h. */
edcc5120 4746
2f9ee862 4747void
4e9e993a 4748print_solib_event (bool is_catchpoint)
edcc5120 4749{
6fb16ce6 4750 bool any_deleted = !current_program_space->deleted_solibs.empty ();
bcb430e4 4751 bool any_added = !current_program_space->added_solibs.empty ();
edcc5120
TT
4752
4753 if (!is_catchpoint)
4754 {
4755 if (any_added || any_deleted)
112e8700 4756 current_uiout->text (_("Stopped due to shared library event:\n"));
edcc5120 4757 else
112e8700
SM
4758 current_uiout->text (_("Stopped due to shared library event (no "
4759 "libraries added or removed)\n"));
edcc5120
TT
4760 }
4761
112e8700
SM
4762 if (current_uiout->is_mi_like_p ())
4763 current_uiout->field_string ("reason",
4764 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
edcc5120
TT
4765
4766 if (any_deleted)
4767 {
112e8700 4768 current_uiout->text (_(" Inferior unloaded "));
10f489e5 4769 ui_out_emit_list list_emitter (current_uiout, "removed");
6fb16ce6 4770 for (int ix = 0; ix < current_program_space->deleted_solibs.size (); ix++)
edcc5120 4771 {
6fb16ce6
SM
4772 const std::string &name = current_program_space->deleted_solibs[ix];
4773
edcc5120 4774 if (ix > 0)
112e8700
SM
4775 current_uiout->text (" ");
4776 current_uiout->field_string ("library", name);
4777 current_uiout->text ("\n");
edcc5120 4778 }
edcc5120
TT
4779 }
4780
4781 if (any_added)
4782 {
112e8700 4783 current_uiout->text (_(" Inferior loaded "));
10f489e5 4784 ui_out_emit_list list_emitter (current_uiout, "added");
bcb430e4 4785 bool first = true;
52941706 4786 for (so_list *iter : current_program_space->added_solibs)
edcc5120 4787 {
bcb430e4 4788 if (!first)
112e8700 4789 current_uiout->text (" ");
bcb430e4 4790 first = false;
112e8700
SM
4791 current_uiout->field_string ("library", iter->so_name);
4792 current_uiout->text ("\n");
edcc5120 4793 }
edcc5120
TT
4794 }
4795}
4796
e514a9d6
JM
4797/* Print a message indicating what happened. This is called from
4798 normal_stop(). The input to this routine is the head of the bpstat
36dfb11c
TT
4799 list - a list of the eventpoints that caused this stop. KIND is
4800 the target_waitkind for the stopping event. This
e514a9d6
JM
4801 routine calls the generic print routine for printing a message
4802 about reasons for stopping. This will print (for example) the
4803 "Breakpoint n," part of the output. The return value of this
4804 routine is one of:
c906108c 4805
4a64f543 4806 PRINT_UNKNOWN: Means we printed nothing.
917317f4 4807 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4a64f543 4808 code to print the location. An example is
c5aa993b
JM
4809 "Breakpoint 1, " which should be followed by
4810 the location.
917317f4 4811 PRINT_SRC_ONLY: Means we printed something, but there is no need
c5aa993b
JM
4812 to also print the location part of the message.
4813 An example is the catch/throw messages, which
4a64f543 4814 don't require a location appended to the end.
917317f4 4815 PRINT_NOTHING: We have done some printing and we don't need any
4a64f543 4816 further info to be printed. */
c906108c 4817
917317f4 4818enum print_stop_action
313f3b21 4819bpstat_print (bpstat *bs, int kind)
c906108c 4820{
f486487f 4821 enum print_stop_action val;
c5aa993b 4822
c906108c 4823 /* Maybe another breakpoint in the chain caused us to stop.
53a5351d
JM
4824 (Currently all watchpoints go on the bpstat whether hit or not.
4825 That probably could (should) be changed, provided care is taken
c906108c 4826 with respect to bpstat_explains_signal). */
e514a9d6
JM
4827 for (; bs; bs = bs->next)
4828 {
4829 val = print_bp_stop_message (bs);
4830 if (val == PRINT_SRC_ONLY
4831 || val == PRINT_SRC_AND_LOC
4832 || val == PRINT_NOTHING)
4833 return val;
4834 }
c906108c 4835
36dfb11c
TT
4836 /* If we had hit a shared library event breakpoint,
4837 print_bp_stop_message would print out this message. If we hit an
4838 OS-level shared library event, do the same thing. */
4839 if (kind == TARGET_WAITKIND_LOADED)
4840 {
4e9e993a 4841 print_solib_event (false);
36dfb11c
TT
4842 return PRINT_NOTHING;
4843 }
4844
e514a9d6 4845 /* We reached the end of the chain, or we got a null BS to start
4a64f543 4846 with and nothing was printed. */
917317f4 4847 return PRINT_UNKNOWN;
c906108c
SS
4848}
4849
bf469271 4850/* Evaluate the boolean expression EXP and return the result. */
c906108c 4851
bf469271
PA
4852static bool
4853breakpoint_cond_eval (expression *exp)
c906108c 4854{
278cd55f 4855 struct value *mark = value_mark ();
bf469271 4856 bool res = value_true (evaluate_expression (exp));
cc59ec59 4857
c906108c 4858 value_free_to_mark (mark);
bf469271 4859 return res;
c906108c
SS
4860}
4861
5760d0ab 4862/* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
c906108c 4863
313f3b21 4864bpstat::bpstat (struct bp_location *bl, bpstat ***bs_link_pointer)
04afa70c 4865 : next (NULL),
b6433ede 4866 bp_location_at (bp_location_ref_ptr::new_reference (bl)),
04afa70c
TT
4867 breakpoint_at (bl->owner),
4868 commands (NULL),
04afa70c
TT
4869 print (0),
4870 stop (0),
4871 print_it (print_it_normal)
c906108c 4872{
04afa70c
TT
4873 **bs_link_pointer = this;
4874 *bs_link_pointer = &next;
4875}
4876
313f3b21 4877bpstat::bpstat ()
04afa70c 4878 : next (NULL),
04afa70c
TT
4879 breakpoint_at (NULL),
4880 commands (NULL),
04afa70c
TT
4881 print (0),
4882 stop (0),
4883 print_it (print_it_normal)
4884{
c906108c
SS
4885}
4886\f
d983da9c
DJ
4887/* The target has stopped with waitstatus WS. Check if any hardware
4888 watchpoints have triggered, according to the target. */
4889
4890int
c272a98c 4891watchpoints_triggered (const target_waitstatus &ws)
d983da9c 4892{
57810aa7 4893 bool stopped_by_watchpoint = target_stopped_by_watchpoint ();
d983da9c 4894 CORE_ADDR addr;
d983da9c
DJ
4895
4896 if (!stopped_by_watchpoint)
4897 {
4898 /* We were not stopped by a watchpoint. Mark all watchpoints
4899 as not triggered. */
43892fdf 4900 for (breakpoint *b : all_breakpoints ())
cc60f2e3 4901 if (is_hardware_watchpoint (b))
3a5c3e22
PA
4902 {
4903 struct watchpoint *w = (struct watchpoint *) b;
4904
4905 w->watchpoint_triggered = watch_triggered_no;
4906 }
d983da9c
DJ
4907
4908 return 0;
4909 }
4910
328d42d8 4911 if (!target_stopped_data_address (current_inferior ()->top_target (), &addr))
d983da9c
DJ
4912 {
4913 /* We were stopped by a watchpoint, but we don't know where.
4914 Mark all watchpoints as unknown. */
43892fdf 4915 for (breakpoint *b : all_breakpoints ())
cc60f2e3 4916 if (is_hardware_watchpoint (b))
3a5c3e22
PA
4917 {
4918 struct watchpoint *w = (struct watchpoint *) b;
4919
4920 w->watchpoint_triggered = watch_triggered_unknown;
4921 }
d983da9c 4922
3c4797ba 4923 return 1;
d983da9c
DJ
4924 }
4925
4926 /* The target could report the data address. Mark watchpoints
4927 affected by this data address as triggered, and all others as not
4928 triggered. */
4929
43892fdf 4930 for (breakpoint *b : all_breakpoints ())
cc60f2e3 4931 if (is_hardware_watchpoint (b))
d983da9c 4932 {
3a5c3e22 4933 struct watchpoint *w = (struct watchpoint *) b;
d983da9c 4934
3a5c3e22 4935 w->watchpoint_triggered = watch_triggered_no;
40cb8ca5 4936 for (bp_location *loc : b->locations ())
9c06b0b4 4937 {
3a5c3e22 4938 if (is_masked_watchpoint (b))
9c06b0b4 4939 {
3a5c3e22
PA
4940 CORE_ADDR newaddr = addr & w->hw_wp_mask;
4941 CORE_ADDR start = loc->address & w->hw_wp_mask;
9c06b0b4
TJB
4942
4943 if (newaddr == start)
4944 {
3a5c3e22 4945 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
4946 break;
4947 }
4948 }
4949 /* Exact match not required. Within range is sufficient. */
328d42d8
SM
4950 else if (target_watchpoint_addr_within_range
4951 (current_inferior ()->top_target (), addr, loc->address,
4952 loc->length))
9c06b0b4 4953 {
3a5c3e22 4954 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
4955 break;
4956 }
4957 }
d983da9c
DJ
4958 }
4959
4960 return 1;
4961}
4962
bf469271
PA
4963/* Possible return values for watchpoint_check. */
4964enum wp_check_result
4965 {
4966 /* The watchpoint has been deleted. */
4967 WP_DELETED = 1,
4968
4969 /* The value has changed. */
4970 WP_VALUE_CHANGED = 2,
4971
4972 /* The value has not changed. */
4973 WP_VALUE_NOT_CHANGED = 3,
4974
4975 /* Ignore this watchpoint, no matter if the value changed or not. */
4976 WP_IGNORE = 4,
4977 };
c906108c
SS
4978
4979#define BP_TEMPFLAG 1
4980#define BP_HARDWAREFLAG 2
4981
4a64f543 4982/* Evaluate watchpoint condition expression and check if its value
bf469271 4983 changed. */
553e4c11 4984
bf469271 4985static wp_check_result
313f3b21 4986watchpoint_check (bpstat *bs)
c906108c 4987{
3a5c3e22 4988 struct watchpoint *b;
c906108c
SS
4989 struct frame_info *fr;
4990 int within_current_scope;
4991
f431efe5 4992 /* BS is built from an existing struct breakpoint. */
2bdf28a0 4993 gdb_assert (bs->breakpoint_at != NULL);
3a5c3e22 4994 b = (struct watchpoint *) bs->breakpoint_at;
d0fb5eae 4995
f6bc2008
PA
4996 /* If this is a local watchpoint, we only want to check if the
4997 watchpoint frame is in scope if the current thread is the thread
4998 that was used to create the watchpoint. */
4999 if (!watchpoint_in_thread_scope (b))
60e1c644 5000 return WP_IGNORE;
f6bc2008 5001
c906108c
SS
5002 if (b->exp_valid_block == NULL)
5003 within_current_scope = 1;
5004 else
5005 {
edb3359d
DJ
5006 struct frame_info *frame = get_current_frame ();
5007 struct gdbarch *frame_arch = get_frame_arch (frame);
5008 CORE_ADDR frame_pc = get_frame_pc (frame);
5009
c9cf6e20 5010 /* stack_frame_destroyed_p() returns a non-zero value if we're
4a64f543
MS
5011 still in the function but the stack frame has already been
5012 invalidated. Since we can't rely on the values of local
5013 variables after the stack has been destroyed, we are treating
5014 the watchpoint in that state as `not changed' without further
5015 checking. Don't mark watchpoints as changed if the current
5016 frame is in an epilogue - even if they are in some other
5017 frame, our view of the stack is likely to be wrong and
5018 frame_find_by_id could error out. */
c9cf6e20 5019 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
60e1c644 5020 return WP_IGNORE;
a0f49112 5021
101dcfbe 5022 fr = frame_find_by_id (b->watchpoint_frame);
c906108c 5023 within_current_scope = (fr != NULL);
69fbadd5
DJ
5024
5025 /* If we've gotten confused in the unwinder, we might have
5026 returned a frame that can't describe this variable. */
edb3359d
DJ
5027 if (within_current_scope)
5028 {
5029 struct symbol *function;
5030
5031 function = get_frame_function (fr);
5032 if (function == NULL
4aeddc50 5033 || !contained_in (b->exp_valid_block, function->value_block ()))
edb3359d
DJ
5034 within_current_scope = 0;
5035 }
69fbadd5 5036
edb3359d 5037 if (within_current_scope)
c906108c
SS
5038 /* If we end up stopping, the current frame will get selected
5039 in normal_stop. So this call to select_frame won't affect
5040 the user. */
0f7d239c 5041 select_frame (fr);
c906108c 5042 }
c5aa993b 5043
c906108c
SS
5044 if (within_current_scope)
5045 {
4a64f543 5046 /* We use value_{,free_to_}mark because it could be a *long*
dda83cd7
SM
5047 time before we return to the command level and call
5048 free_all_values. We can't call free_all_values because we
5049 might be in the middle of evaluating a function call. */
c906108c 5050
9c06b0b4 5051 struct value *mark;
fa4727a6
DJ
5052 struct value *new_val;
5053
c1fc2657 5054 if (is_masked_watchpoint (b))
9c06b0b4
TJB
5055 /* Since we don't know the exact trigger address (from
5056 stopped_data_address), just tell the user we've triggered
5057 a mask watchpoint. */
5058 return WP_VALUE_CHANGED;
5059
5060 mark = value_mark ();
1eaebe02 5061 fetch_subexp_value (b->exp.get (), b->exp->op.get (), &new_val,
413403fc 5062 NULL, NULL, false);
218d2fc6 5063
bb9d5f81
PP
5064 if (b->val_bitsize != 0)
5065 new_val = extract_bitfield_from_watchpoint_value (b, new_val);
5066
4a64f543
MS
5067 /* We use value_equal_contents instead of value_equal because
5068 the latter coerces an array to a pointer, thus comparing just
5069 the address of the array instead of its contents. This is
5070 not what we want. */
fa4727a6 5071 if ((b->val != NULL) != (new_val != NULL)
850645cf
TT
5072 || (b->val != NULL && !value_equal_contents (b->val.get (),
5073 new_val)))
c906108c 5074 {
c906108c 5075 bs->old_val = b->val;
850645cf 5076 b->val = release_value (new_val);
4c1d86d9 5077 b->val_valid = true;
850645cf
TT
5078 if (new_val != NULL)
5079 value_free_to_mark (mark);
c906108c
SS
5080 return WP_VALUE_CHANGED;
5081 }
5082 else
5083 {
60e1c644 5084 /* Nothing changed. */
c906108c 5085 value_free_to_mark (mark);
c906108c
SS
5086 return WP_VALUE_NOT_CHANGED;
5087 }
5088 }
5089 else
5090 {
5091 /* This seems like the only logical thing to do because
dda83cd7
SM
5092 if we temporarily ignored the watchpoint, then when
5093 we reenter the block in which it is valid it contains
5094 garbage (in the case of a function, it may have two
5095 garbage values, one before and one after the prologue).
5096 So we can't even detect the first assignment to it and
5097 watch after that (since the garbage may or may not equal
5098 the first value assigned). */
348d480f 5099 /* We print all the stop information in
c47614fe
TT
5100 breakpointprint_it, but in this case, by the time we
5101 call breakpoint->print_it this bp will be deleted
348d480f
PA
5102 already. So we have no choice but print the information
5103 here. */
468afe6c 5104
0e454242 5105 SWITCH_THRU_ALL_UIS ()
dda83cd7 5106 {
468afe6c
PA
5107 struct ui_out *uiout = current_uiout;
5108
112e8700
SM
5109 if (uiout->is_mi_like_p ())
5110 uiout->field_string
5111 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
6a831f06
PA
5112 uiout->message ("\nWatchpoint %pF deleted because the program has "
5113 "left the block in\n"
5114 "which its expression is valid.\n",
5115 signed_field ("wpnum", b->number));
468afe6c 5116 }
4ce44c66 5117
cdac0397 5118 /* Make sure the watchpoint's commands aren't executed. */
d1b0a7bf 5119 b->commands = NULL;
d0fb5eae 5120 watchpoint_del_at_next_stop (b);
c906108c
SS
5121
5122 return WP_DELETED;
5123 }
5124}
5125
18a18393 5126/* Return true if it looks like target has stopped due to hitting
348d480f
PA
5127 breakpoint location BL. This function does not check if we should
5128 stop, only if BL explains the stop. */
5129
18a18393 5130static int
6c95b8df 5131bpstat_check_location (const struct bp_location *bl,
accd0bcd 5132 const address_space *aspace, CORE_ADDR bp_addr,
c272a98c 5133 const target_waitstatus &ws)
18a18393
VP
5134{
5135 struct breakpoint *b = bl->owner;
5136
348d480f 5137 /* BL is from an existing breakpoint. */
2bdf28a0
JK
5138 gdb_assert (b != NULL);
5139
c47614fe 5140 return b->breakpoint_hit (bl, aspace, bp_addr, ws);
18a18393
VP
5141}
5142
3a5c3e22
PA
5143/* Determine if the watched values have actually changed, and we
5144 should stop. If not, set BS->stop to 0. */
5145
18a18393 5146static void
313f3b21 5147bpstat_check_watchpoint (bpstat *bs)
18a18393 5148{
2bdf28a0 5149 const struct bp_location *bl;
3a5c3e22 5150 struct watchpoint *b;
2bdf28a0
JK
5151
5152 /* BS is built for existing struct breakpoint. */
b6433ede 5153 bl = bs->bp_location_at.get ();
2bdf28a0 5154 gdb_assert (bl != NULL);
3a5c3e22 5155 b = (struct watchpoint *) bs->breakpoint_at;
2bdf28a0 5156 gdb_assert (b != NULL);
18a18393 5157
18a18393 5158 {
18a18393
VP
5159 int must_check_value = 0;
5160
c1fc2657 5161 if (b->type == bp_watchpoint)
18a18393
VP
5162 /* For a software watchpoint, we must always check the
5163 watched value. */
5164 must_check_value = 1;
5165 else if (b->watchpoint_triggered == watch_triggered_yes)
5166 /* We have a hardware watchpoint (read, write, or access)
5167 and the target earlier reported an address watched by
5168 this watchpoint. */
5169 must_check_value = 1;
5170 else if (b->watchpoint_triggered == watch_triggered_unknown
c1fc2657 5171 && b->type == bp_hardware_watchpoint)
18a18393
VP
5172 /* We were stopped by a hardware watchpoint, but the target could
5173 not report the data address. We must check the watchpoint's
5174 value. Access and read watchpoints are out of luck; without
5175 a data address, we can't figure it out. */
5176 must_check_value = 1;
3a5c3e22 5177
18a18393
VP
5178 if (must_check_value)
5179 {
bf469271
PA
5180 wp_check_result e;
5181
a70b8144 5182 try
bf469271
PA
5183 {
5184 e = watchpoint_check (bs);
5185 }
230d2906 5186 catch (const gdb_exception &ex)
bf469271
PA
5187 {
5188 exception_fprintf (gdb_stderr, ex,
5189 "Error evaluating expression "
5190 "for watchpoint %d\n",
5191 b->number);
5192
5193 SWITCH_THRU_ALL_UIS ()
5194 {
6cb06a8c
TT
5195 gdb_printf (_("Watchpoint %d deleted.\n"),
5196 b->number);
bf469271
PA
5197 }
5198 watchpoint_del_at_next_stop (b);
5199 e = WP_DELETED;
5200 }
bf469271 5201
18a18393
VP
5202 switch (e)
5203 {
5204 case WP_DELETED:
5205 /* We've already printed what needs to be printed. */
5206 bs->print_it = print_it_done;
5207 /* Stop. */
5208 break;
60e1c644
PA
5209 case WP_IGNORE:
5210 bs->print_it = print_it_noop;
5211 bs->stop = 0;
5212 break;
18a18393 5213 case WP_VALUE_CHANGED:
c1fc2657 5214 if (b->type == bp_read_watchpoint)
18a18393 5215 {
85d721b8
PA
5216 /* There are two cases to consider here:
5217
4a64f543 5218 1. We're watching the triggered memory for reads.
85d721b8
PA
5219 In that case, trust the target, and always report
5220 the watchpoint hit to the user. Even though
5221 reads don't cause value changes, the value may
5222 have changed since the last time it was read, and
5223 since we're not trapping writes, we will not see
5224 those, and as such we should ignore our notion of
5225 old value.
5226
4a64f543 5227 2. We're watching the triggered memory for both
85d721b8
PA
5228 reads and writes. There are two ways this may
5229 happen:
5230
4a64f543 5231 2.1. This is a target that can't break on data
85d721b8
PA
5232 reads only, but can break on accesses (reads or
5233 writes), such as e.g., x86. We detect this case
5234 at the time we try to insert read watchpoints.
5235
4a64f543 5236 2.2. Otherwise, the target supports read
85d721b8
PA
5237 watchpoints, but, the user set an access or write
5238 watchpoint watching the same memory as this read
5239 watchpoint.
5240
5241 If we're watching memory writes as well as reads,
5242 ignore watchpoint hits when we find that the
5243 value hasn't changed, as reads don't cause
5244 changes. This still gives false positives when
5245 the program writes the same value to memory as
5246 what there was already in memory (we will confuse
5247 it for a read), but it's much better than
5248 nothing. */
5249
5250 int other_write_watchpoint = 0;
5251
5252 if (bl->watchpoint_type == hw_read)
5253 {
43892fdf 5254 for (breakpoint *other_b : all_breakpoints ())
3a5c3e22
PA
5255 if (other_b->type == bp_hardware_watchpoint
5256 || other_b->type == bp_access_watchpoint)
85d721b8 5257 {
3a5c3e22
PA
5258 struct watchpoint *other_w =
5259 (struct watchpoint *) other_b;
5260
5261 if (other_w->watchpoint_triggered
5262 == watch_triggered_yes)
5263 {
5264 other_write_watchpoint = 1;
5265 break;
5266 }
85d721b8
PA
5267 }
5268 }
5269
5270 if (other_write_watchpoint
5271 || bl->watchpoint_type == hw_access)
5272 {
5273 /* We're watching the same memory for writes,
5274 and the value changed since the last time we
5275 updated it, so this trap must be for a write.
5276 Ignore it. */
5277 bs->print_it = print_it_noop;
5278 bs->stop = 0;
5279 }
18a18393
VP
5280 }
5281 break;
5282 case WP_VALUE_NOT_CHANGED:
c1fc2657
SM
5283 if (b->type == bp_hardware_watchpoint
5284 || b->type == bp_watchpoint)
18a18393
VP
5285 {
5286 /* Don't stop: write watchpoints shouldn't fire if
5287 the value hasn't changed. */
5288 bs->print_it = print_it_noop;
5289 bs->stop = 0;
5290 }
5291 /* Stop. */
5292 break;
5293 default:
5294 /* Can't happen. */
18a18393
VP
5295 break;
5296 }
5297 }
5298 else /* must_check_value == 0 */
5299 {
5300 /* This is a case where some watchpoint(s) triggered, but
5301 not at the address of this watchpoint, or else no
5302 watchpoint triggered after all. So don't print
5303 anything for this watchpoint. */
5304 bs->print_it = print_it_noop;
5305 bs->stop = 0;
5306 }
5307 }
5308}
5309
7d4df6a4
DE
5310/* For breakpoints that are currently marked as telling gdb to stop,
5311 check conditions (condition proper, frame, thread and ignore count)
18a18393
VP
5312 of breakpoint referred to by BS. If we should not stop for this
5313 breakpoint, set BS->stop to 0. */
f431efe5 5314
18a18393 5315static void
313f3b21 5316bpstat_check_breakpoint_conditions (bpstat *bs, thread_info *thread)
18a18393 5317{
2bdf28a0
JK
5318 const struct bp_location *bl;
5319 struct breakpoint *b;
bf469271
PA
5320 /* Assume stop. */
5321 bool condition_result = true;
7d4df6a4
DE
5322 struct expression *cond;
5323
5324 gdb_assert (bs->stop);
2bdf28a0
JK
5325
5326 /* BS is built for existing struct breakpoint. */
b6433ede 5327 bl = bs->bp_location_at.get ();
2bdf28a0 5328 gdb_assert (bl != NULL);
f431efe5 5329 b = bs->breakpoint_at;
2bdf28a0 5330 gdb_assert (b != NULL);
18a18393 5331
b775012e
LM
5332 /* Even if the target evaluated the condition on its end and notified GDB, we
5333 need to do so again since GDB does not know if we stopped due to a
5334 breakpoint or a single step breakpoint. */
5335
18a18393 5336 if (frame_id_p (b->frame_id)
edb3359d 5337 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
18a18393 5338 {
7d4df6a4
DE
5339 bs->stop = 0;
5340 return;
5341 }
60e1c644 5342
12ab52e9
PA
5343 /* If this is a thread/task-specific breakpoint, don't waste cpu
5344 evaluating the condition if this isn't the specified
5345 thread/task. */
00431a78
PA
5346 if ((b->thread != -1 && b->thread != thread->global_num)
5347 || (b->task != 0 && b->task != ada_get_task_number (thread)))
6c1b0f7b
DE
5348 {
5349 bs->stop = 0;
5350 return;
5351 }
5352
6dddc817
DE
5353 /* Evaluate extension language breakpoints that have a "stop" method
5354 implemented. */
5355 bs->stop = breakpoint_ext_lang_cond_says_stop (b);
7371cf6d 5356
7d4df6a4
DE
5357 if (is_watchpoint (b))
5358 {
5359 struct watchpoint *w = (struct watchpoint *) b;
3a5c3e22 5360
4d01a485 5361 cond = w->cond_exp.get ();
7d4df6a4
DE
5362 }
5363 else
4d01a485 5364 cond = bl->cond.get ();
60e1c644 5365
7d4df6a4
DE
5366 if (cond && b->disposition != disp_del_at_next_stop)
5367 {
5368 int within_current_scope = 1;
5369 struct watchpoint * w;
60e1c644 5370
7d4df6a4
DE
5371 /* We use value_mark and value_free_to_mark because it could
5372 be a long time before we return to the command level and
5373 call free_all_values. We can't call free_all_values
5374 because we might be in the middle of evaluating a
5375 function call. */
5376 struct value *mark = value_mark ();
5377
5378 if (is_watchpoint (b))
5379 w = (struct watchpoint *) b;
5380 else
5381 w = NULL;
5382
5383 /* Need to select the frame, with all that implies so that
5384 the conditions will have the right context. Because we
5385 use the frame, we will not see an inlined function's
5386 variables when we arrive at a breakpoint at the start
5387 of the inlined function; the current frame will be the
5388 call site. */
5389 if (w == NULL || w->cond_exp_valid_block == NULL)
5390 select_frame (get_current_frame ());
5391 else
18a18393 5392 {
7d4df6a4
DE
5393 struct frame_info *frame;
5394
5395 /* For local watchpoint expressions, which particular
5396 instance of a local is being watched matters, so we
5397 keep track of the frame to evaluate the expression
5398 in. To evaluate the condition however, it doesn't
5399 really matter which instantiation of the function
5400 where the condition makes sense triggers the
5401 watchpoint. This allows an expression like "watch
5402 global if q > 10" set in `func', catch writes to
5403 global on all threads that call `func', or catch
5404 writes on all recursive calls of `func' by a single
5405 thread. We simply always evaluate the condition in
5406 the innermost frame that's executing where it makes
5407 sense to evaluate the condition. It seems
5408 intuitive. */
5409 frame = block_innermost_frame (w->cond_exp_valid_block);
5410 if (frame != NULL)
5411 select_frame (frame);
5412 else
5413 within_current_scope = 0;
18a18393 5414 }
7d4df6a4 5415 if (within_current_scope)
bf469271 5416 {
a70b8144 5417 try
bf469271
PA
5418 {
5419 condition_result = breakpoint_cond_eval (cond);
5420 }
230d2906 5421 catch (const gdb_exception &ex)
bf469271
PA
5422 {
5423 exception_fprintf (gdb_stderr, ex,
5424 "Error in testing breakpoint condition:\n");
5425 }
bf469271 5426 }
7d4df6a4 5427 else
18a18393 5428 {
7d4df6a4
DE
5429 warning (_("Watchpoint condition cannot be tested "
5430 "in the current scope"));
5431 /* If we failed to set the right context for this
5432 watchpoint, unconditionally report it. */
18a18393 5433 }
7d4df6a4
DE
5434 /* FIXME-someday, should give breakpoint #. */
5435 value_free_to_mark (mark);
18a18393 5436 }
7d4df6a4 5437
bf469271 5438 if (cond && !condition_result)
7d4df6a4
DE
5439 {
5440 bs->stop = 0;
5441 }
7d4df6a4
DE
5442 else if (b->ignore_count > 0)
5443 {
5444 b->ignore_count--;
5445 bs->stop = 0;
5446 /* Increase the hit count even though we don't stop. */
5447 ++(b->hit_count);
76727919 5448 gdb::observers::breakpoint_modified.notify (b);
7d4df6a4 5449 }
18a18393
VP
5450}
5451
1cf4d951
PA
5452/* Returns true if we need to track moribund locations of LOC's type
5453 on the current target. */
5454
5455static int
5456need_moribund_for_location_type (struct bp_location *loc)
5457{
5458 return ((loc->loc_type == bp_loc_software_breakpoint
5459 && !target_supports_stopped_by_sw_breakpoint ())
5460 || (loc->loc_type == bp_loc_hardware_breakpoint
5461 && !target_supports_stopped_by_hw_breakpoint ()));
5462}
5463
ddfe970e 5464/* See breakpoint.h. */
c906108c 5465
313f3b21 5466bpstat *
ddfe970e 5467build_bpstat_chain (const address_space *aspace, CORE_ADDR bp_addr,
c272a98c 5468 const target_waitstatus &ws)
c906108c 5469{
313f3b21 5470 bpstat *bs_head = nullptr, **bs_link = &bs_head;
c5aa993b 5471
43892fdf 5472 for (breakpoint *b : all_breakpoints ())
429374b8 5473 {
1a853c52 5474 if (!breakpoint_enabled (b))
429374b8 5475 continue;
a5606eee 5476
40cb8ca5 5477 for (bp_location *bl : b->locations ())
429374b8 5478 {
4a64f543
MS
5479 /* For hardware watchpoints, we look only at the first
5480 location. The watchpoint_check function will work on the
5481 entire expression, not the individual locations. For
5482 read watchpoints, the watchpoints_triggered function has
5483 checked all locations already. */
429374b8
JK
5484 if (b->type == bp_hardware_watchpoint && bl != b->loc)
5485 break;
18a18393 5486
b5fa468f 5487 if (!bl->enabled || bl->disabled_by_cond || bl->shlib_disabled)
429374b8 5488 continue;
c5aa993b 5489
09ac7c10 5490 if (!bpstat_check_location (bl, aspace, bp_addr, ws))
429374b8 5491 continue;
c5aa993b 5492
4a64f543
MS
5493 /* Come here if it's a watchpoint, or if the break address
5494 matches. */
c5aa993b 5495
313f3b21 5496 bpstat *bs = new bpstat (bl, &bs_link); /* Alloc a bpstat to
ddfe970e 5497 explain stop. */
c5aa993b 5498
f431efe5
PA
5499 /* Assume we stop. Should we find a watchpoint that is not
5500 actually triggered, or if the condition of the breakpoint
5501 evaluates as false, we'll reset 'stop' to 0. */
429374b8
JK
5502 bs->stop = 1;
5503 bs->print = 1;
d983da9c 5504
f431efe5
PA
5505 /* If this is a scope breakpoint, mark the associated
5506 watchpoint as triggered so that we will handle the
5507 out-of-scope event. We'll get to the watchpoint next
5508 iteration. */
d0fb5eae 5509 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
3a5c3e22
PA
5510 {
5511 struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
5512
5513 w->watchpoint_triggered = watch_triggered_yes;
5514 }
f431efe5
PA
5515 }
5516 }
5517
7c16b83e 5518 /* Check if a moribund breakpoint explains the stop. */
1cf4d951
PA
5519 if (!target_supports_stopped_by_sw_breakpoint ()
5520 || !target_supports_stopped_by_hw_breakpoint ())
f431efe5 5521 {
1123588c 5522 for (bp_location *loc : moribund_locations)
f431efe5 5523 {
1cf4d951
PA
5524 if (breakpoint_location_address_match (loc, aspace, bp_addr)
5525 && need_moribund_for_location_type (loc))
5526 {
313f3b21 5527 bpstat *bs = new bpstat (loc, &bs_link);
1cf4d951
PA
5528 /* For hits of moribund locations, we should just proceed. */
5529 bs->stop = 0;
5530 bs->print = 0;
5531 bs->print_it = print_it_noop;
5532 }
f431efe5
PA
5533 }
5534 }
5535
ddfe970e
KS
5536 return bs_head;
5537}
5538
5539/* See breakpoint.h. */
5540
313f3b21 5541bpstat *
ddfe970e 5542bpstat_stop_status (const address_space *aspace,
00431a78 5543 CORE_ADDR bp_addr, thread_info *thread,
c272a98c 5544 const target_waitstatus &ws,
313f3b21 5545 bpstat *stop_chain)
ddfe970e
KS
5546{
5547 struct breakpoint *b = NULL;
5548 /* First item of allocated bpstat's. */
313f3b21
SM
5549 bpstat *bs_head = stop_chain;
5550 bpstat *bs;
ddfe970e
KS
5551 int need_remove_insert;
5552 int removed_any;
5553
5554 /* First, build the bpstat chain with locations that explain a
5555 target stop, while being careful to not set the target running,
5556 as that may invalidate locations (in particular watchpoint
5557 locations are recreated). Resuming will happen here with
5558 breakpoint conditions or watchpoint expressions that include
5559 inferior function calls. */
5560 if (bs_head == NULL)
5561 bs_head = build_bpstat_chain (aspace, bp_addr, ws);
5562
edcc5120
TT
5563 /* A bit of special processing for shlib breakpoints. We need to
5564 process solib loading here, so that the lists of loaded and
5565 unloaded libraries are correct before we handle "catch load" and
5566 "catch unload". */
5567 for (bs = bs_head; bs != NULL; bs = bs->next)
5568 {
5d268276 5569 if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
edcc5120
TT
5570 {
5571 handle_solib_event ();
5572 break;
5573 }
5574 }
5575
f431efe5
PA
5576 /* Now go through the locations that caused the target to stop, and
5577 check whether we're interested in reporting this stop to higher
5578 layers, or whether we should resume the target transparently. */
5579
5580 removed_any = 0;
5581
5760d0ab 5582 for (bs = bs_head; bs != NULL; bs = bs->next)
f431efe5
PA
5583 {
5584 if (!bs->stop)
5585 continue;
5586
f431efe5 5587 b = bs->breakpoint_at;
c47614fe 5588 b->check_status (bs);
348d480f 5589 if (bs->stop)
28010a5d 5590 {
00431a78 5591 bpstat_check_breakpoint_conditions (bs, thread);
f431efe5 5592
429374b8
JK
5593 if (bs->stop)
5594 {
5595 ++(b->hit_count);
c906108c 5596
4a64f543 5597 /* We will stop here. */
429374b8
JK
5598 if (b->disposition == disp_disable)
5599 {
816338b5 5600 --(b->enable_count);
1a853c52 5601 if (b->enable_count <= 0)
429374b8 5602 b->enable_state = bp_disabled;
f431efe5 5603 removed_any = 1;
429374b8 5604 }
bd7ccaa9 5605 gdb::observers::breakpoint_modified.notify (b);
429374b8
JK
5606 if (b->silent)
5607 bs->print = 0;
5608 bs->commands = b->commands;
abf85f46 5609 if (command_line_is_silent (bs->commands
d1b0a7bf 5610 ? bs->commands.get () : NULL))
abf85f46 5611 bs->print = 0;
9d6e6e84 5612
c47614fe 5613 b->after_condition_true (bs);
429374b8
JK
5614 }
5615
348d480f 5616 }
a9b3a50f
PA
5617
5618 /* Print nothing for this entry if we don't stop or don't
5619 print. */
5620 if (!bs->stop || !bs->print)
5621 bs->print_it = print_it_noop;
429374b8 5622 }
876fa593 5623
d983da9c
DJ
5624 /* If we aren't stopping, the value of some hardware watchpoint may
5625 not have changed, but the intermediate memory locations we are
5626 watching may have. Don't bother if we're stopping; this will get
5627 done later. */
d832cb68 5628 need_remove_insert = 0;
5760d0ab
JK
5629 if (! bpstat_causes_stop (bs_head))
5630 for (bs = bs_head; bs != NULL; bs = bs->next)
d983da9c 5631 if (!bs->stop
f431efe5
PA
5632 && bs->breakpoint_at
5633 && is_hardware_watchpoint (bs->breakpoint_at))
d983da9c 5634 {
3a5c3e22
PA
5635 struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
5636
5637 update_watchpoint (w, 0 /* don't reparse. */);
d832cb68 5638 need_remove_insert = 1;
d983da9c
DJ
5639 }
5640
d832cb68 5641 if (need_remove_insert)
44702360 5642 update_global_location_list (UGLL_MAY_INSERT);
f431efe5 5643 else if (removed_any)
44702360 5644 update_global_location_list (UGLL_DONT_INSERT);
d832cb68 5645
5760d0ab 5646 return bs_head;
c906108c 5647}
628fe4e4 5648
d37e0847
PA
5649/* See breakpoint.h. */
5650
5651bpstat *
5652bpstat_stop_status_nowatch (const address_space *aspace, CORE_ADDR bp_addr,
5653 thread_info *thread, const target_waitstatus &ws)
5654{
5655 gdb_assert (!target_stopped_by_watchpoint ());
5656
5657 /* Clear all watchpoints' 'watchpoint_triggered' value from a
5658 previous stop to avoid confusing bpstat_stop_status. */
5659 watchpoints_triggered (ws);
5660
5661 return bpstat_stop_status (aspace, bp_addr, thread, ws);
5662}
5663
628fe4e4 5664static void
61c26be8 5665handle_jit_event (CORE_ADDR address)
628fe4e4 5666{
628fe4e4
JK
5667 struct gdbarch *gdbarch;
5668
1eb8556f 5669 infrun_debug_printf ("handling bp_jit_event");
243a9253 5670
628fe4e4
JK
5671 /* Switch terminal for any messages produced by
5672 breakpoint_re_set. */
223ffa71 5673 target_terminal::ours_for_output ();
628fe4e4 5674
61c26be8
MS
5675 gdbarch = get_frame_arch (get_current_frame ());
5676 /* This event is caused by a breakpoint set in `jit_breakpoint_re_set`,
5677 thus it is expected that its objectfile can be found through
5678 minimal symbol lookup. If it doesn't work (and assert fails), it
5679 most likely means that `jit_breakpoint_re_set` was changes and this
5680 function needs to be updated too. */
5681 bound_minimal_symbol jit_bp_sym = lookup_minimal_symbol_by_pc (address);
5682 gdb_assert (jit_bp_sym.objfile != nullptr);
5683 jit_event_handler (gdbarch, jit_bp_sym.objfile);
628fe4e4 5684
223ffa71 5685 target_terminal::inferior ();
628fe4e4
JK
5686}
5687
5688/* Prepare WHAT final decision for infrun. */
5689
5690/* Decide what infrun needs to do with this bpstat. */
5691
c906108c 5692struct bpstat_what
313f3b21 5693bpstat_what (bpstat *bs_head)
c906108c 5694{
c906108c 5695 struct bpstat_what retval;
313f3b21 5696 bpstat *bs;
c906108c 5697
628fe4e4 5698 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
aa7d318d 5699 retval.call_dummy = STOP_NONE;
e2d0f980 5700 retval.is_longjmp = false;
628fe4e4 5701
0e30163f 5702 for (bs = bs_head; bs != NULL; bs = bs->next)
c906108c 5703 {
628fe4e4
JK
5704 /* Extract this BS's action. After processing each BS, we check
5705 if its action overrides all we've seem so far. */
5706 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
5707 enum bptype bptype;
5708
c906108c 5709 if (bs->breakpoint_at == NULL)
628fe4e4
JK
5710 {
5711 /* I suspect this can happen if it was a momentary
5712 breakpoint which has since been deleted. */
5713 bptype = bp_none;
5714 }
20874c92 5715 else
f431efe5 5716 bptype = bs->breakpoint_at->type;
628fe4e4
JK
5717
5718 switch (bptype)
c906108c
SS
5719 {
5720 case bp_none:
628fe4e4 5721 break;
c906108c
SS
5722 case bp_breakpoint:
5723 case bp_hardware_breakpoint:
7c16b83e 5724 case bp_single_step:
c906108c
SS
5725 case bp_until:
5726 case bp_finish:
a9b3a50f 5727 case bp_shlib_event:
c906108c
SS
5728 if (bs->stop)
5729 {
5730 if (bs->print)
628fe4e4 5731 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 5732 else
628fe4e4 5733 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
5734 }
5735 else
628fe4e4 5736 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5737 break;
5738 case bp_watchpoint:
5739 case bp_hardware_watchpoint:
5740 case bp_read_watchpoint:
5741 case bp_access_watchpoint:
5742 if (bs->stop)
5743 {
5744 if (bs->print)
628fe4e4 5745 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 5746 else
628fe4e4 5747 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
5748 }
5749 else
628fe4e4
JK
5750 {
5751 /* There was a watchpoint, but we're not stopping.
5752 This requires no further action. */
5753 }
c906108c
SS
5754 break;
5755 case bp_longjmp:
e2e4d78b 5756 case bp_longjmp_call_dummy:
186c406b 5757 case bp_exception:
0a39bb32
PA
5758 if (bs->stop)
5759 {
5760 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
5761 retval.is_longjmp = bptype != bp_exception;
5762 }
5763 else
5764 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5765 break;
5766 case bp_longjmp_resume:
186c406b 5767 case bp_exception_resume:
0a39bb32
PA
5768 if (bs->stop)
5769 {
5770 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
5771 retval.is_longjmp = bptype == bp_longjmp_resume;
5772 }
5773 else
5774 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5775 break;
5776 case bp_step_resume:
5777 if (bs->stop)
628fe4e4
JK
5778 this_action = BPSTAT_WHAT_STEP_RESUME;
5779 else
c906108c 5780 {
628fe4e4
JK
5781 /* It is for the wrong frame. */
5782 this_action = BPSTAT_WHAT_SINGLE;
c906108c 5783 }
c906108c 5784 break;
2c03e5be
PA
5785 case bp_hp_step_resume:
5786 if (bs->stop)
5787 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
5788 else
5789 {
5790 /* It is for the wrong frame. */
5791 this_action = BPSTAT_WHAT_SINGLE;
5792 }
5793 break;
c906108c 5794 case bp_watchpoint_scope:
c4093a6a 5795 case bp_thread_event:
1900040c 5796 case bp_overlay_event:
0fd8e87f 5797 case bp_longjmp_master:
aa7d318d 5798 case bp_std_terminate_master:
186c406b 5799 case bp_exception_master:
628fe4e4 5800 this_action = BPSTAT_WHAT_SINGLE;
c4093a6a 5801 break;
ce78b96d 5802 case bp_catchpoint:
c5aa993b
JM
5803 if (bs->stop)
5804 {
5805 if (bs->print)
628fe4e4 5806 this_action = BPSTAT_WHAT_STOP_NOISY;
c5aa993b 5807 else
628fe4e4 5808 this_action = BPSTAT_WHAT_STOP_SILENT;
c5aa993b
JM
5809 }
5810 else
628fe4e4 5811 {
cb1e4e32
PA
5812 /* Some catchpoints are implemented with breakpoints.
5813 For those, we need to step over the breakpoint. */
5814 if (bs->bp_location_at->loc_type != bp_loc_other)
5815 this_action = BPSTAT_WHAT_SINGLE;
628fe4e4
JK
5816 }
5817 break;
628fe4e4 5818 case bp_jit_event:
628fe4e4 5819 this_action = BPSTAT_WHAT_SINGLE;
c5aa993b 5820 break;
c906108c 5821 case bp_call_dummy:
53a5351d
JM
5822 /* Make sure the action is stop (silent or noisy),
5823 so infrun.c pops the dummy frame. */
aa7d318d 5824 retval.call_dummy = STOP_STACK_DUMMY;
628fe4e4 5825 this_action = BPSTAT_WHAT_STOP_SILENT;
aa7d318d
TT
5826 break;
5827 case bp_std_terminate:
5828 /* Make sure the action is stop (silent or noisy),
5829 so infrun.c pops the dummy frame. */
aa7d318d 5830 retval.call_dummy = STOP_STD_TERMINATE;
628fe4e4 5831 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c 5832 break;
1042e4c0 5833 case bp_tracepoint:
7a697b8d 5834 case bp_fast_tracepoint:
0fb4aa4b 5835 case bp_static_tracepoint:
7b572efb 5836 case bp_static_marker_tracepoint:
1042e4c0
SS
5837 /* Tracepoint hits should not be reported back to GDB, and
5838 if one got through somehow, it should have been filtered
5839 out already. */
5840 internal_error (__FILE__, __LINE__,
7a697b8d 5841 _("bpstat_what: tracepoint encountered"));
0e30163f
JK
5842 break;
5843 case bp_gnu_ifunc_resolver:
5844 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
5845 this_action = BPSTAT_WHAT_SINGLE;
5846 break;
5847 case bp_gnu_ifunc_resolver_return:
5848 /* The breakpoint will be removed, execution will restart from the
5849 PC of the former breakpoint. */
5850 this_action = BPSTAT_WHAT_KEEP_CHECKING;
5851 break;
e7e0cddf
SS
5852
5853 case bp_dprintf:
a11cfd87
HZ
5854 if (bs->stop)
5855 this_action = BPSTAT_WHAT_STOP_SILENT;
5856 else
5857 this_action = BPSTAT_WHAT_SINGLE;
e7e0cddf
SS
5858 break;
5859
628fe4e4
JK
5860 default:
5861 internal_error (__FILE__, __LINE__,
5862 _("bpstat_what: unhandled bptype %d"), (int) bptype);
c906108c 5863 }
628fe4e4 5864
325fac50 5865 retval.main_action = std::max (retval.main_action, this_action);
c906108c 5866 }
628fe4e4 5867
243a9253
PA
5868 return retval;
5869}
628fe4e4 5870
243a9253 5871void
313f3b21 5872bpstat_run_callbacks (bpstat *bs_head)
243a9253 5873{
313f3b21 5874 bpstat *bs;
628fe4e4 5875
0e30163f
JK
5876 for (bs = bs_head; bs != NULL; bs = bs->next)
5877 {
5878 struct breakpoint *b = bs->breakpoint_at;
5879
5880 if (b == NULL)
5881 continue;
5882 switch (b->type)
5883 {
243a9253 5884 case bp_jit_event:
61c26be8 5885 handle_jit_event (bs->bp_location_at->address);
243a9253 5886 break;
0e30163f
JK
5887 case bp_gnu_ifunc_resolver:
5888 gnu_ifunc_resolver_stop (b);
5889 break;
5890 case bp_gnu_ifunc_resolver_return:
5891 gnu_ifunc_resolver_return_stop (b);
5892 break;
5893 }
5894 }
c906108c
SS
5895}
5896
4c462cb0 5897/* See breakpoint.h. */
c906108c 5898
4c462cb0
SM
5899bool
5900bpstat_should_step ()
c906108c 5901{
43892fdf 5902 for (breakpoint *b : all_breakpoints ())
717a8278 5903 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
4c462cb0 5904 return true;
43892fdf 5905
4c462cb0 5906 return false;
c906108c
SS
5907}
5908
4c462cb0
SM
5909/* See breakpoint.h. */
5910
5911bool
313f3b21 5912bpstat_causes_stop (bpstat *bs)
67822962
PA
5913{
5914 for (; bs != NULL; bs = bs->next)
5915 if (bs->stop)
4c462cb0 5916 return true;
67822962 5917
4c462cb0 5918 return false;
67822962
PA
5919}
5920
c906108c 5921\f
c5aa993b 5922
6c92c339 5923/* Compute a number of spaces suitable to indent the next line
170b53b2
UW
5924 so it starts at the position corresponding to the table column
5925 named COL_NAME in the currently active table of UIOUT. */
5926
6c92c339 5927static int
170b53b2
UW
5928wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
5929{
170b53b2 5930 int i, total_width, width, align;
c5209615 5931 const char *text;
170b53b2
UW
5932
5933 total_width = 0;
112e8700 5934 for (i = 1; uiout->query_table_field (i, &width, &align, &text); i++)
170b53b2
UW
5935 {
5936 if (strcmp (text, col_name) == 0)
6c92c339 5937 return total_width;
170b53b2
UW
5938
5939 total_width += width + 1;
5940 }
5941
6c92c339 5942 return 0;
170b53b2
UW
5943}
5944
b775012e
LM
5945/* Determine if the locations of this breakpoint will have their conditions
5946 evaluated by the target, host or a mix of both. Returns the following:
5947
5948 "host": Host evals condition.
5949 "host or target": Host or Target evals condition.
5950 "target": Target evals condition.
5951*/
5952
5953static const char *
a67bcaba 5954bp_condition_evaluator (const breakpoint *b)
b775012e 5955{
b775012e
LM
5956 char host_evals = 0;
5957 char target_evals = 0;
5958
5959 if (!b)
5960 return NULL;
5961
5962 if (!is_breakpoint (b))
5963 return NULL;
5964
5965 if (gdb_evaluates_breakpoint_condition_p ()
5966 || !target_supports_evaluation_of_breakpoint_conditions ())
5967 return condition_evaluation_host;
5968
40cb8ca5 5969 for (bp_location *bl : b->locations ())
b775012e
LM
5970 {
5971 if (bl->cond_bytecode)
5972 target_evals++;
5973 else
5974 host_evals++;
5975 }
5976
5977 if (host_evals && target_evals)
5978 return condition_evaluation_both;
5979 else if (target_evals)
5980 return condition_evaluation_target;
5981 else
5982 return condition_evaluation_host;
5983}
5984
5985/* Determine the breakpoint location's condition evaluator. This is
5986 similar to bp_condition_evaluator, but for locations. */
5987
5988static const char *
5989bp_location_condition_evaluator (struct bp_location *bl)
5990{
5991 if (bl && !is_breakpoint (bl->owner))
5992 return NULL;
5993
5994 if (gdb_evaluates_breakpoint_condition_p ()
5995 || !target_supports_evaluation_of_breakpoint_conditions ())
5996 return condition_evaluation_host;
5997
5998 if (bl && bl->cond_bytecode)
5999 return condition_evaluation_target;
6000 else
6001 return condition_evaluation_host;
6002}
6003
859825b8
JK
6004/* Print the LOC location out of the list of B->LOC locations. */
6005
170b53b2 6006static void
a67bcaba 6007print_breakpoint_location (const breakpoint *b,
170b53b2 6008 struct bp_location *loc)
0d381245 6009{
79a45e25 6010 struct ui_out *uiout = current_uiout;
5ed8105e
PA
6011
6012 scoped_restore_current_program_space restore_pspace;
6c95b8df 6013
859825b8
JK
6014 if (loc != NULL && loc->shlib_disabled)
6015 loc = NULL;
6016
6c95b8df
PA
6017 if (loc != NULL)
6018 set_current_program_space (loc->pspace);
6019
56435ebe 6020 if (b->display_canonical)
d28cd78a 6021 uiout->field_string ("what", event_location_to_string (b->location.get ()));
2f202fde 6022 else if (loc && loc->symtab)
0d381245 6023 {
4a27f119
KS
6024 const struct symbol *sym = loc->symbol;
6025
0d381245
VP
6026 if (sym)
6027 {
112e8700 6028 uiout->text ("in ");
987012b8 6029 uiout->field_string ("func", sym->print_name (),
e43b10e1 6030 function_name_style.style ());
112e8700
SM
6031 uiout->text (" ");
6032 uiout->wrap_hint (wrap_indent_at_field (uiout, "what"));
6033 uiout->text ("at ");
0d381245 6034 }
112e8700 6035 uiout->field_string ("file",
cbe56571 6036 symtab_to_filename_for_display (loc->symtab),
e43b10e1 6037 file_name_style.style ());
112e8700 6038 uiout->text (":");
05cba821 6039
112e8700
SM
6040 if (uiout->is_mi_like_p ())
6041 uiout->field_string ("fullname", symtab_to_fullname (loc->symtab));
0d381245 6042
381befee 6043 uiout->field_signed ("line", loc->line_number);
0d381245 6044 }
859825b8 6045 else if (loc)
0d381245 6046 {
d7e74731 6047 string_file stb;
170b53b2 6048
d7e74731 6049 print_address_symbolic (loc->gdbarch, loc->address, &stb,
22e722e1 6050 demangle, "");
112e8700 6051 uiout->field_stream ("at", stb);
0d381245 6052 }
859825b8 6053 else
f00aae0f 6054 {
d28cd78a
TT
6055 uiout->field_string ("pending",
6056 event_location_to_string (b->location.get ()));
f00aae0f
KS
6057 /* If extra_string is available, it could be holding a condition
6058 or dprintf arguments. In either case, make sure it is printed,
6059 too, but only for non-MI streams. */
112e8700 6060 if (!uiout->is_mi_like_p () && b->extra_string != NULL)
f00aae0f
KS
6061 {
6062 if (b->type == bp_dprintf)
112e8700 6063 uiout->text (",");
f00aae0f 6064 else
112e8700 6065 uiout->text (" ");
6f781ee3 6066 uiout->text (b->extra_string.get ());
f00aae0f
KS
6067 }
6068 }
6c95b8df 6069
b775012e
LM
6070 if (loc && is_breakpoint (b)
6071 && breakpoint_condition_evaluation_mode () == condition_evaluation_target
6072 && bp_condition_evaluator (b) == condition_evaluation_both)
6073 {
112e8700
SM
6074 uiout->text (" (");
6075 uiout->field_string ("evaluated-by",
b775012e 6076 bp_location_condition_evaluator (loc));
112e8700 6077 uiout->text (")");
b775012e 6078 }
0d381245
VP
6079}
6080
269b11a2
PA
6081static const char *
6082bptype_string (enum bptype type)
c906108c 6083{
c4093a6a
JM
6084 struct ep_type_description
6085 {
6086 enum bptype type;
a121b7c1 6087 const char *description;
c4093a6a
JM
6088 };
6089 static struct ep_type_description bptypes[] =
c906108c 6090 {
c5aa993b
JM
6091 {bp_none, "?deleted?"},
6092 {bp_breakpoint, "breakpoint"},
c906108c 6093 {bp_hardware_breakpoint, "hw breakpoint"},
7c16b83e 6094 {bp_single_step, "sw single-step"},
c5aa993b
JM
6095 {bp_until, "until"},
6096 {bp_finish, "finish"},
6097 {bp_watchpoint, "watchpoint"},
c906108c 6098 {bp_hardware_watchpoint, "hw watchpoint"},
c5aa993b
JM
6099 {bp_read_watchpoint, "read watchpoint"},
6100 {bp_access_watchpoint, "acc watchpoint"},
6101 {bp_longjmp, "longjmp"},
6102 {bp_longjmp_resume, "longjmp resume"},
e2e4d78b 6103 {bp_longjmp_call_dummy, "longjmp for call dummy"},
186c406b
TT
6104 {bp_exception, "exception"},
6105 {bp_exception_resume, "exception resume"},
c5aa993b 6106 {bp_step_resume, "step resume"},
2c03e5be 6107 {bp_hp_step_resume, "high-priority step resume"},
c5aa993b
JM
6108 {bp_watchpoint_scope, "watchpoint scope"},
6109 {bp_call_dummy, "call dummy"},
aa7d318d 6110 {bp_std_terminate, "std::terminate"},
c5aa993b 6111 {bp_shlib_event, "shlib events"},
c4093a6a 6112 {bp_thread_event, "thread events"},
1900040c 6113 {bp_overlay_event, "overlay events"},
0fd8e87f 6114 {bp_longjmp_master, "longjmp master"},
aa7d318d 6115 {bp_std_terminate_master, "std::terminate master"},
186c406b 6116 {bp_exception_master, "exception master"},
ce78b96d 6117 {bp_catchpoint, "catchpoint"},
1042e4c0 6118 {bp_tracepoint, "tracepoint"},
7a697b8d 6119 {bp_fast_tracepoint, "fast tracepoint"},
0fb4aa4b 6120 {bp_static_tracepoint, "static tracepoint"},
7b572efb 6121 {bp_static_marker_tracepoint, "static marker tracepoint"},
e7e0cddf 6122 {bp_dprintf, "dprintf"},
4efc6507 6123 {bp_jit_event, "jit events"},
0e30163f
JK
6124 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
6125 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
c5aa993b 6126 };
269b11a2
PA
6127
6128 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
6129 || ((int) type != bptypes[(int) type].type))
6130 internal_error (__FILE__, __LINE__,
6131 _("bptypes table does not describe type #%d."),
6132 (int) type);
6133
6134 return bptypes[(int) type].description;
6135}
6136
998580f1
MK
6137/* For MI, output a field named 'thread-groups' with a list as the value.
6138 For CLI, prefix the list with the string 'inf'. */
6139
6140static void
6141output_thread_groups (struct ui_out *uiout,
6142 const char *field_name,
5c632425 6143 const std::vector<int> &inf_nums,
998580f1
MK
6144 int mi_only)
6145{
112e8700 6146 int is_mi = uiout->is_mi_like_p ();
998580f1
MK
6147
6148 /* For backward compatibility, don't display inferiors in CLI unless
6149 there are several. Always display them for MI. */
6150 if (!is_mi && mi_only)
6151 return;
6152
10f489e5 6153 ui_out_emit_list list_emitter (uiout, field_name);
752eb8b4 6154
5c632425 6155 for (size_t i = 0; i < inf_nums.size (); i++)
998580f1
MK
6156 {
6157 if (is_mi)
6158 {
6159 char mi_group[10];
6160
5c632425 6161 xsnprintf (mi_group, sizeof (mi_group), "i%d", inf_nums[i]);
112e8700 6162 uiout->field_string (NULL, mi_group);
998580f1
MK
6163 }
6164 else
6165 {
6166 if (i == 0)
112e8700 6167 uiout->text (" inf ");
998580f1 6168 else
112e8700 6169 uiout->text (", ");
998580f1 6170
5c632425 6171 uiout->text (plongest (inf_nums[i]));
998580f1
MK
6172 }
6173 }
998580f1
MK
6174}
6175
a38118e5
PA
6176/* Print B to gdb_stdout. If RAW_LOC, print raw breakpoint locations
6177 instead of going via breakpoint_ops::print_one. This makes "maint
6178 info breakpoints" show the software breakpoint locations of
6179 catchpoints, which are considered internal implementation
c01e038b
TT
6180 detail. Returns true if RAW_LOC is false and if the breakpoint's
6181 print_one method did something; false otherwise. */
269b11a2 6182
c01e038b 6183static bool
269b11a2
PA
6184print_one_breakpoint_location (struct breakpoint *b,
6185 struct bp_location *loc,
6186 int loc_number,
6187 struct bp_location **last_loc,
a38118e5 6188 int allflag, bool raw_loc)
269b11a2
PA
6189{
6190 struct command_line *l;
c2c6d25f 6191 static char bpenables[] = "nynny";
c906108c 6192
79a45e25 6193 struct ui_out *uiout = current_uiout;
0d381245
VP
6194 int header_of_multiple = 0;
6195 int part_of_multiple = (loc != NULL);
79a45b7d
TT
6196 struct value_print_options opts;
6197
6198 get_user_print_options (&opts);
0d381245
VP
6199
6200 gdb_assert (!loc || loc_number != 0);
4a64f543
MS
6201 /* See comment in print_one_breakpoint concerning treatment of
6202 breakpoints with single disabled location. */
0d381245
VP
6203 if (loc == NULL
6204 && (b->loc != NULL
b5fa468f
TBA
6205 && (b->loc->next != NULL
6206 || !b->loc->enabled || b->loc->disabled_by_cond)))
0d381245
VP
6207 header_of_multiple = 1;
6208 if (loc == NULL)
6209 loc = b->loc;
6210
c4093a6a
JM
6211 annotate_record ();
6212
6213 /* 1 */
6214 annotate_field (0);
0d381245 6215 if (part_of_multiple)
528e1572 6216 uiout->field_fmt ("number", "%d.%d", b->number, loc_number);
0d381245 6217 else
381befee 6218 uiout->field_signed ("number", b->number);
c4093a6a
JM
6219
6220 /* 2 */
6221 annotate_field (1);
0d381245 6222 if (part_of_multiple)
112e8700 6223 uiout->field_skip ("type");
269b11a2 6224 else
112e8700 6225 uiout->field_string ("type", bptype_string (b->type));
c4093a6a
JM
6226
6227 /* 3 */
6228 annotate_field (2);
0d381245 6229 if (part_of_multiple)
112e8700 6230 uiout->field_skip ("disp");
0d381245 6231 else
112e8700 6232 uiout->field_string ("disp", bpdisp_text (b->disposition));
0d381245 6233
c4093a6a
JM
6234 /* 4 */
6235 annotate_field (3);
85c88e2a
TBA
6236 /* For locations that are disabled because of an invalid condition,
6237 display "N*" on CLI, where "*" refers to a footnote below the
6238 table. For MI, simply display a "N" without a footnote. */
6239 const char *N = (uiout->is_mi_like_p ()) ? "N" : "N*";
0d381245 6240 if (part_of_multiple)
85c88e2a 6241 uiout->field_string ("enabled", (loc->disabled_by_cond ? N
b5fa468f 6242 : (loc->enabled ? "y" : "n")));
0d381245 6243 else
112e8700 6244 uiout->field_fmt ("enabled", "%c", bpenables[(int) b->enable_state]);
0d381245 6245
c4093a6a 6246 /* 5 and 6 */
c01e038b 6247 bool result = false;
c47614fe 6248 if (!raw_loc && b->print_one (last_loc))
c01e038b 6249 result = true;
3086aeae 6250 else
a38118e5
PA
6251 {
6252 if (is_watchpoint (b))
3a5c3e22
PA
6253 {
6254 struct watchpoint *w = (struct watchpoint *) b;
6255
6256 /* Field 4, the address, is omitted (which makes the columns
6257 not line up too nicely with the headers, but the effect
6258 is relatively readable). */
6259 if (opts.addressprint)
112e8700 6260 uiout->field_skip ("addr");
3a5c3e22 6261 annotate_field (5);
a4c50be3 6262 uiout->field_string ("what", w->exp_string.get ());
3a5c3e22 6263 }
f06f1252
TT
6264 else if (!is_catchpoint (b) || is_exception_catchpoint (b)
6265 || is_ada_exception_catchpoint (b))
a38118e5
PA
6266 {
6267 if (opts.addressprint)
6268 {
6269 annotate_field (4);
6270 if (header_of_multiple)
7f6aba03
TT
6271 uiout->field_string ("addr", "<MULTIPLE>",
6272 metadata_style.style ());
a38118e5 6273 else if (b->loc == NULL || loc->shlib_disabled)
7f6aba03
TT
6274 uiout->field_string ("addr", "<PENDING>",
6275 metadata_style.style ());
a38118e5
PA
6276 else
6277 uiout->field_core_addr ("addr",
6278 loc->gdbarch, loc->address);
6279 }
6280 annotate_field (5);
6281 if (!header_of_multiple)
6282 print_breakpoint_location (b, loc);
6283 if (b->loc)
6284 *last_loc = b->loc;
6285 }
6286 }
6c95b8df 6287
998580f1 6288 if (loc != NULL && !header_of_multiple)
6c95b8df 6289 {
5c632425 6290 std::vector<int> inf_nums;
998580f1 6291 int mi_only = 1;
6c95b8df 6292
08036331 6293 for (inferior *inf : all_inferiors ())
6c95b8df
PA
6294 {
6295 if (inf->pspace == loc->pspace)
5c632425 6296 inf_nums.push_back (inf->num);
6c95b8df 6297 }
998580f1 6298
dda83cd7 6299 /* For backward compatibility, don't display inferiors in CLI unless
998580f1
MK
6300 there are several. Always display for MI. */
6301 if (allflag
6302 || (!gdbarch_has_global_breakpoints (target_gdbarch ())
94c93c35 6303 && (program_spaces.size () > 1
998580f1
MK
6304 || number_of_inferiors () > 1)
6305 /* LOC is for existing B, it cannot be in
6306 moribund_locations and thus having NULL OWNER. */
6307 && loc->owner->type != bp_catchpoint))
6308 mi_only = 0;
5c632425 6309 output_thread_groups (uiout, "thread-groups", inf_nums, mi_only);
6c95b8df
PA
6310 }
6311
4a306c9a 6312 if (!part_of_multiple)
c4093a6a 6313 {
4a306c9a
JB
6314 if (b->thread != -1)
6315 {
6316 /* FIXME: This seems to be redundant and lost here; see the
4a64f543 6317 "stop only in" line a little further down. */
112e8700 6318 uiout->text (" thread ");
381befee 6319 uiout->field_signed ("thread", b->thread);
4a306c9a
JB
6320 }
6321 else if (b->task != 0)
6322 {
112e8700 6323 uiout->text (" task ");
381befee 6324 uiout->field_signed ("task", b->task);
4a306c9a 6325 }
c4093a6a 6326 }
f1310107 6327
112e8700 6328 uiout->text ("\n");
f1310107 6329
348d480f 6330 if (!part_of_multiple)
c47614fe 6331 b->print_one_detail (uiout);
f1310107 6332
0d381245 6333 if (part_of_multiple && frame_id_p (b->frame_id))
c4093a6a
JM
6334 {
6335 annotate_field (6);
112e8700 6336 uiout->text ("\tstop only in stack frame at ");
e5dd4106 6337 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
dda83cd7 6338 the frame ID. */
112e8700 6339 uiout->field_core_addr ("frame",
5af949e3 6340 b->gdbarch, b->frame_id.stack_addr);
112e8700 6341 uiout->text ("\n");
c4093a6a
JM
6342 }
6343
28010a5d 6344 if (!part_of_multiple && b->cond_string)
c4093a6a
JM
6345 {
6346 annotate_field (7);
d77f58be 6347 if (is_tracepoint (b))
112e8700 6348 uiout->text ("\ttrace only if ");
1042e4c0 6349 else
112e8700 6350 uiout->text ("\tstop only if ");
6f781ee3 6351 uiout->field_string ("cond", b->cond_string.get ());
b775012e
LM
6352
6353 /* Print whether the target is doing the breakpoint's condition
6354 evaluation. If GDB is doing the evaluation, don't print anything. */
6355 if (is_breakpoint (b)
6356 && breakpoint_condition_evaluation_mode ()
6357 == condition_evaluation_target)
6358 {
6a831f06
PA
6359 uiout->message (" (%pF evals)",
6360 string_field ("evaluated-by",
6361 bp_condition_evaluator (b)));
b775012e 6362 }
112e8700 6363 uiout->text ("\n");
0101ce28
JJ
6364 }
6365
0d381245 6366 if (!part_of_multiple && b->thread != -1)
c4093a6a 6367 {
4a64f543 6368 /* FIXME should make an annotation for this. */
112e8700
SM
6369 uiout->text ("\tstop only in thread ");
6370 if (uiout->is_mi_like_p ())
381befee 6371 uiout->field_signed ("thread", b->thread);
5d5658a1
PA
6372 else
6373 {
6374 struct thread_info *thr = find_thread_global_id (b->thread);
6375
112e8700 6376 uiout->field_string ("thread", print_thread_id (thr));
5d5658a1 6377 }
112e8700 6378 uiout->text ("\n");
c4093a6a
JM
6379 }
6380
556ec64d
YQ
6381 if (!part_of_multiple)
6382 {
6383 if (b->hit_count)
31f56a27
YQ
6384 {
6385 /* FIXME should make an annotation for this. */
6386 if (is_catchpoint (b))
112e8700 6387 uiout->text ("\tcatchpoint");
31f56a27 6388 else if (is_tracepoint (b))
112e8700 6389 uiout->text ("\ttracepoint");
31f56a27 6390 else
112e8700
SM
6391 uiout->text ("\tbreakpoint");
6392 uiout->text (" already hit ");
381befee 6393 uiout->field_signed ("times", b->hit_count);
31f56a27 6394 if (b->hit_count == 1)
112e8700 6395 uiout->text (" time\n");
31f56a27 6396 else
112e8700 6397 uiout->text (" times\n");
31f56a27 6398 }
556ec64d
YQ
6399 else
6400 {
31f56a27 6401 /* Output the count also if it is zero, but only if this is mi. */
112e8700 6402 if (uiout->is_mi_like_p ())
381befee 6403 uiout->field_signed ("times", b->hit_count);
556ec64d
YQ
6404 }
6405 }
8b93c638 6406
0d381245 6407 if (!part_of_multiple && b->ignore_count)
c4093a6a
JM
6408 {
6409 annotate_field (8);
6a831f06
PA
6410 uiout->message ("\tignore next %pF hits\n",
6411 signed_field ("ignore", b->ignore_count));
c4093a6a 6412 }
059fb39f 6413
816338b5
SS
6414 /* Note that an enable count of 1 corresponds to "enable once"
6415 behavior, which is reported by the combination of enablement and
6416 disposition, so we don't need to mention it here. */
6417 if (!part_of_multiple && b->enable_count > 1)
6418 {
6419 annotate_field (8);
112e8700 6420 uiout->text ("\tdisable after ");
816338b5
SS
6421 /* Tweak the wording to clarify that ignore and enable counts
6422 are distinct, and have additive effect. */
6423 if (b->ignore_count)
112e8700 6424 uiout->text ("additional ");
816338b5 6425 else
112e8700 6426 uiout->text ("next ");
381befee 6427 uiout->field_signed ("enable", b->enable_count);
112e8700 6428 uiout->text (" hits\n");
816338b5
SS
6429 }
6430
f196051f
SS
6431 if (!part_of_multiple && is_tracepoint (b))
6432 {
6433 struct tracepoint *tp = (struct tracepoint *) b;
6434
6435 if (tp->traceframe_usage)
6436 {
112e8700 6437 uiout->text ("\ttrace buffer usage ");
381befee 6438 uiout->field_signed ("traceframe-usage", tp->traceframe_usage);
112e8700 6439 uiout->text (" bytes\n");
f196051f
SS
6440 }
6441 }
d3ce09f5 6442
d1b0a7bf 6443 l = b->commands ? b->commands.get () : NULL;
059fb39f 6444 if (!part_of_multiple && l)
c4093a6a
JM
6445 {
6446 annotate_field (9);
2e783024 6447 ui_out_emit_tuple tuple_emitter (uiout, "script");
8b93c638 6448 print_command_lines (uiout, l, 4);
c4093a6a 6449 }
d24317b4 6450
d9b3f62e 6451 if (is_tracepoint (b))
1042e4c0 6452 {
d9b3f62e
PA
6453 struct tracepoint *t = (struct tracepoint *) b;
6454
6455 if (!part_of_multiple && t->pass_count)
6456 {
6457 annotate_field (10);
112e8700 6458 uiout->text ("\tpass count ");
381befee 6459 uiout->field_signed ("pass", t->pass_count);
112e8700 6460 uiout->text (" \n");
d9b3f62e 6461 }
f2a8bc8a
YQ
6462
6463 /* Don't display it when tracepoint or tracepoint location is
6464 pending. */
6465 if (!header_of_multiple && loc != NULL && !loc->shlib_disabled)
6466 {
6467 annotate_field (11);
6468
112e8700
SM
6469 if (uiout->is_mi_like_p ())
6470 uiout->field_string ("installed",
f2a8bc8a
YQ
6471 loc->inserted ? "y" : "n");
6472 else
6473 {
6474 if (loc->inserted)
112e8700 6475 uiout->text ("\t");
f2a8bc8a 6476 else
112e8700
SM
6477 uiout->text ("\tnot ");
6478 uiout->text ("installed on target\n");
f2a8bc8a
YQ
6479 }
6480 }
1042e4c0
SS
6481 }
6482
112e8700 6483 if (uiout->is_mi_like_p () && !part_of_multiple)
d24317b4 6484 {
3a5c3e22
PA
6485 if (is_watchpoint (b))
6486 {
6487 struct watchpoint *w = (struct watchpoint *) b;
6488
a4c50be3 6489 uiout->field_string ("original-location", w->exp_string.get ());
3a5c3e22 6490 }
f00aae0f 6491 else if (b->location != NULL
d28cd78a 6492 && event_location_to_string (b->location.get ()) != NULL)
112e8700 6493 uiout->field_string ("original-location",
d28cd78a 6494 event_location_to_string (b->location.get ()));
d24317b4 6495 }
c01e038b
TT
6496
6497 return result;
c4093a6a 6498}
c5aa993b 6499
13674803
SM
6500/* See breakpoint.h. */
6501
6502bool fix_multi_location_breakpoint_output_globally = false;
6503
0d381245
VP
6504static void
6505print_one_breakpoint (struct breakpoint *b,
4a64f543 6506 struct bp_location **last_loc,
6c95b8df 6507 int allflag)
0d381245 6508{
79a45e25 6509 struct ui_out *uiout = current_uiout;
13674803
SM
6510 bool use_fixed_output
6511 = (uiout->test_flags (fix_multi_location_breakpoint_output)
6512 || fix_multi_location_breakpoint_output_globally);
8d3788bd 6513
b4be1b06 6514 gdb::optional<ui_out_emit_tuple> bkpt_tuple_emitter (gdb::in_place, uiout, "bkpt");
c01e038b
TT
6515 bool printed = print_one_breakpoint_location (b, NULL, 0, last_loc,
6516 allflag, false);
8d3788bd 6517
b4be1b06
SM
6518 /* The mi2 broken format: the main breakpoint tuple ends here, the locations
6519 are outside. */
6520 if (!use_fixed_output)
6521 bkpt_tuple_emitter.reset ();
0d381245
VP
6522
6523 /* If this breakpoint has custom print function,
6524 it's already printed. Otherwise, print individual
6525 locations, if any. */
c01e038b 6526 if (!printed || allflag)
0d381245 6527 {
4a64f543
MS
6528 /* If breakpoint has a single location that is disabled, we
6529 print it as if it had several locations, since otherwise it's
6530 hard to represent "breakpoint enabled, location disabled"
6531 situation.
6532
6533 Note that while hardware watchpoints have several locations
a38118e5
PA
6534 internally, that's not a property exposed to users.
6535
6536 Likewise, while catchpoints may be implemented with
6537 breakpoints (e.g., catch throw), that's not a property
6538 exposed to users. We do however display the internal
6539 breakpoint locations with "maint info breakpoints". */
6540 if (!is_hardware_watchpoint (b)
f06f1252
TT
6541 && (!is_catchpoint (b) || is_exception_catchpoint (b)
6542 || is_ada_exception_catchpoint (b))
a38118e5 6543 && (allflag
b5fa468f
TBA
6544 || (b->loc && (b->loc->next
6545 || !b->loc->enabled
6546 || b->loc->disabled_by_cond))))
0d381245 6547 {
b4be1b06
SM
6548 gdb::optional<ui_out_emit_list> locations_list;
6549
6550 /* For MI version <= 2, keep the behavior where GDB outputs an invalid
6551 MI record. For later versions, place breakpoint locations in a
6552 list. */
6553 if (uiout->is_mi_like_p () && use_fixed_output)
6554 locations_list.emplace (uiout, "locations");
8d3788bd 6555
b4be1b06 6556 int n = 1;
40cb8ca5 6557 for (bp_location *loc : b->locations ())
8d3788bd 6558 {
b4be1b06 6559 ui_out_emit_tuple loc_tuple_emitter (uiout, NULL);
a38118e5
PA
6560 print_one_breakpoint_location (b, loc, n, last_loc,
6561 allflag, allflag);
40cb8ca5 6562 n++;
8d3788bd 6563 }
0d381245
VP
6564 }
6565 }
6566}
6567
a6d9a66e
UW
6568static int
6569breakpoint_address_bits (struct breakpoint *b)
6570{
6571 int print_address_bits = 0;
a6d9a66e 6572
c6d81124
PA
6573 /* Software watchpoints that aren't watching memory don't have an
6574 address to print. */
6575 if (is_no_memory_software_watchpoint (b))
6576 return 0;
6577
40cb8ca5 6578 for (bp_location *loc : b->locations ())
a6d9a66e 6579 {
c7437ca6
PA
6580 int addr_bit;
6581
c7437ca6 6582 addr_bit = gdbarch_addr_bit (loc->gdbarch);
a6d9a66e
UW
6583 if (addr_bit > print_address_bits)
6584 print_address_bits = addr_bit;
6585 }
6586
6587 return print_address_bits;
6588}
0d381245 6589
65630365 6590/* See breakpoint.h. */
c5aa993b 6591
65630365
PA
6592void
6593print_breakpoint (breakpoint *b)
c4093a6a 6594{
a6d9a66e 6595 struct bp_location *dummy_loc = NULL;
65630365 6596 print_one_breakpoint (b, &dummy_loc, 0);
c4093a6a 6597}
c5aa993b 6598
09d682a4
TT
6599/* Return true if this breakpoint was set by the user, false if it is
6600 internal or momentary. */
6601
6602int
6603user_breakpoint_p (struct breakpoint *b)
6604{
46c6471b 6605 return b->number > 0;
09d682a4
TT
6606}
6607
93daf339
TT
6608/* See breakpoint.h. */
6609
6610int
6611pending_breakpoint_p (struct breakpoint *b)
6612{
6613 return b->loc == NULL;
6614}
6615
5c458ae8
SM
6616/* Print information on breakpoints (including watchpoints and tracepoints).
6617
6618 If non-NULL, BP_NUM_LIST is a list of numbers and number ranges as
6619 understood by number_or_range_parser. Only breakpoints included in this
6620 list are then printed.
6621
6622 If SHOW_INTERNAL is true, print internal breakpoints.
6623
6624 If FILTER is non-NULL, call it on each breakpoint and only include the
6625 ones for which it returns true.
6626
6627 Return the total number of breakpoints listed. */
c906108c 6628
d77f58be 6629static int
5c458ae8 6630breakpoint_1 (const char *bp_num_list, bool show_internal,
f2478a7e 6631 bool (*filter) (const struct breakpoint *))
c4093a6a 6632{
a6d9a66e 6633 struct bp_location *last_loc = NULL;
7f3b0473 6634 int nr_printable_breakpoints;
79a45b7d 6635 struct value_print_options opts;
a6d9a66e 6636 int print_address_bits = 0;
269b11a2 6637 int print_type_col_width = 14;
79a45e25 6638 struct ui_out *uiout = current_uiout;
b5fa468f 6639 bool has_disabled_by_cond_location = false;
269b11a2 6640
79a45b7d
TT
6641 get_user_print_options (&opts);
6642
4a64f543
MS
6643 /* Compute the number of rows in the table, as well as the size
6644 required for address fields. */
7f3b0473 6645 nr_printable_breakpoints = 0;
43892fdf 6646 for (breakpoint *b : all_breakpoints ())
e5a67952
MS
6647 {
6648 /* If we have a filter, only list the breakpoints it accepts. */
6649 if (filter && !filter (b))
6650 continue;
6651
5c458ae8 6652 /* If we have a BP_NUM_LIST string, it is a list of breakpoints to
e5a67952 6653 accept. Skip the others. */
5c458ae8 6654 if (bp_num_list != NULL && *bp_num_list != '\0')
e5a67952 6655 {
5c458ae8 6656 if (show_internal && parse_and_eval_long (bp_num_list) != b->number)
e5a67952 6657 continue;
5c458ae8 6658 if (!show_internal && !number_is_in_list (bp_num_list, b->number))
e5a67952
MS
6659 continue;
6660 }
269b11a2 6661
5c458ae8 6662 if (show_internal || user_breakpoint_p (b))
e5a67952
MS
6663 {
6664 int addr_bit, type_len;
a6d9a66e 6665
e5a67952
MS
6666 addr_bit = breakpoint_address_bits (b);
6667 if (addr_bit > print_address_bits)
6668 print_address_bits = addr_bit;
269b11a2 6669
e5a67952
MS
6670 type_len = strlen (bptype_string (b->type));
6671 if (type_len > print_type_col_width)
6672 print_type_col_width = type_len;
6673
6674 nr_printable_breakpoints++;
6675 }
6676 }
7f3b0473 6677
4a2b031d
TT
6678 {
6679 ui_out_emit_table table_emitter (uiout,
6680 opts.addressprint ? 6 : 5,
6681 nr_printable_breakpoints,
6682 "BreakpointTable");
6683
6684 if (nr_printable_breakpoints > 0)
6685 annotate_breakpoints_headers ();
6686 if (nr_printable_breakpoints > 0)
6687 annotate_field (0);
6688 uiout->table_header (7, ui_left, "number", "Num"); /* 1 */
6689 if (nr_printable_breakpoints > 0)
6690 annotate_field (1);
6691 uiout->table_header (print_type_col_width, ui_left, "type", "Type"); /* 2 */
6692 if (nr_printable_breakpoints > 0)
6693 annotate_field (2);
6694 uiout->table_header (4, ui_left, "disp", "Disp"); /* 3 */
6695 if (nr_printable_breakpoints > 0)
6696 annotate_field (3);
6697 uiout->table_header (3, ui_left, "enabled", "Enb"); /* 4 */
6698 if (opts.addressprint)
6699 {
6700 if (nr_printable_breakpoints > 0)
6701 annotate_field (4);
6702 if (print_address_bits <= 32)
6703 uiout->table_header (10, ui_left, "addr", "Address"); /* 5 */
6704 else
6705 uiout->table_header (18, ui_left, "addr", "Address"); /* 5 */
6706 }
6707 if (nr_printable_breakpoints > 0)
6708 annotate_field (5);
6709 uiout->table_header (40, ui_noalign, "what", "What"); /* 6 */
6710 uiout->table_body ();
6711 if (nr_printable_breakpoints > 0)
6712 annotate_breakpoints_table ();
6713
43892fdf 6714 for (breakpoint *b : all_breakpoints ())
4a2b031d
TT
6715 {
6716 QUIT;
6717 /* If we have a filter, only list the breakpoints it accepts. */
6718 if (filter && !filter (b))
6719 continue;
e5a67952 6720
5c458ae8 6721 /* If we have a BP_NUM_LIST string, it is a list of breakpoints to
4a2b031d 6722 accept. Skip the others. */
e5a67952 6723
5c458ae8 6724 if (bp_num_list != NULL && *bp_num_list != '\0')
4a2b031d 6725 {
5c458ae8 6726 if (show_internal) /* maintenance info breakpoint */
4a2b031d 6727 {
5c458ae8 6728 if (parse_and_eval_long (bp_num_list) != b->number)
4a2b031d
TT
6729 continue;
6730 }
6731 else /* all others */
6732 {
5c458ae8 6733 if (!number_is_in_list (bp_num_list, b->number))
4a2b031d
TT
6734 continue;
6735 }
6736 }
6737 /* We only print out user settable breakpoints unless the
5c458ae8
SM
6738 show_internal is set. */
6739 if (show_internal || user_breakpoint_p (b))
b5fa468f
TBA
6740 {
6741 print_one_breakpoint (b, &last_loc, show_internal);
40cb8ca5 6742 for (bp_location *loc : b->locations ())
b5fa468f
TBA
6743 if (loc->disabled_by_cond)
6744 has_disabled_by_cond_location = true;
6745 }
4a2b031d
TT
6746 }
6747 }
698384cd 6748
7f3b0473 6749 if (nr_printable_breakpoints == 0)
c906108c 6750 {
4a64f543
MS
6751 /* If there's a filter, let the caller decide how to report
6752 empty list. */
d77f58be
SS
6753 if (!filter)
6754 {
5c458ae8 6755 if (bp_num_list == NULL || *bp_num_list == '\0')
112e8700 6756 uiout->message ("No breakpoints or watchpoints.\n");
d77f58be 6757 else
112e8700 6758 uiout->message ("No breakpoint or watchpoint matching '%s'.\n",
5c458ae8 6759 bp_num_list);
d77f58be 6760 }
c906108c
SS
6761 }
6762 else
c4093a6a 6763 {
a6d9a66e
UW
6764 if (last_loc && !server_command)
6765 set_next_address (last_loc->gdbarch, last_loc->address);
b5fa468f 6766
85c88e2a 6767 if (has_disabled_by_cond_location && !uiout->is_mi_like_p ())
b5fa468f
TBA
6768 uiout->message (_("(*): Breakpoint condition is invalid at this "
6769 "location.\n"));
c4093a6a 6770 }
c906108c 6771
4a64f543 6772 /* FIXME? Should this be moved up so that it is only called when
c4093a6a 6773 there have been breakpoints? */
c906108c 6774 annotate_breakpoints_table_end ();
d77f58be
SS
6775
6776 return nr_printable_breakpoints;
c906108c
SS
6777}
6778
ad443146
SS
6779/* Display the value of default-collect in a way that is generally
6780 compatible with the breakpoint list. */
6781
6782static void
6783default_collect_info (void)
6784{
79a45e25
PA
6785 struct ui_out *uiout = current_uiout;
6786
ad443146
SS
6787 /* If it has no value (which is frequently the case), say nothing; a
6788 message like "No default-collect." gets in user's face when it's
6789 not wanted. */
e0700ba4 6790 if (default_collect.empty ())
ad443146
SS
6791 return;
6792
6793 /* The following phrase lines up nicely with per-tracepoint collect
6794 actions. */
112e8700
SM
6795 uiout->text ("default collect ");
6796 uiout->field_string ("default-collect", default_collect);
6797 uiout->text (" \n");
ad443146
SS
6798}
6799
c906108c 6800static void
0b39b52e 6801info_breakpoints_command (const char *args, int from_tty)
c906108c 6802{
5c458ae8 6803 breakpoint_1 (args, false, NULL);
ad443146
SS
6804
6805 default_collect_info ();
d77f58be
SS
6806}
6807
6808static void
1d12d88f 6809info_watchpoints_command (const char *args, int from_tty)
d77f58be 6810{
5c458ae8 6811 int num_printed = breakpoint_1 (args, false, is_watchpoint);
79a45e25 6812 struct ui_out *uiout = current_uiout;
d77f58be
SS
6813
6814 if (num_printed == 0)
6815 {
e5a67952 6816 if (args == NULL || *args == '\0')
112e8700 6817 uiout->message ("No watchpoints.\n");
d77f58be 6818 else
112e8700 6819 uiout->message ("No watchpoint matching '%s'.\n", args);
d77f58be 6820 }
c906108c
SS
6821}
6822
7a292a7a 6823static void
4495129a 6824maintenance_info_breakpoints (const char *args, int from_tty)
c906108c 6825{
5c458ae8 6826 breakpoint_1 (args, true, NULL);
ad443146
SS
6827
6828 default_collect_info ();
c906108c
SS
6829}
6830
0d381245 6831static int
714835d5 6832breakpoint_has_pc (struct breakpoint *b,
6c95b8df 6833 struct program_space *pspace,
714835d5 6834 CORE_ADDR pc, struct obj_section *section)
0d381245 6835{
40cb8ca5 6836 for (bp_location *bl : b->locations ())
0d381245 6837 {
6c95b8df
PA
6838 if (bl->pspace == pspace
6839 && bl->address == pc
0d381245
VP
6840 && (!overlay_debugging || bl->section == section))
6841 return 1;
6842 }
6843 return 0;
6844}
6845
672f9b60 6846/* Print a message describing any user-breakpoints set at PC. This
6c95b8df
PA
6847 concerns with logical breakpoints, so we match program spaces, not
6848 address spaces. */
c906108c
SS
6849
6850static void
6c95b8df
PA
6851describe_other_breakpoints (struct gdbarch *gdbarch,
6852 struct program_space *pspace, CORE_ADDR pc,
5af949e3 6853 struct obj_section *section, int thread)
c906108c 6854{
52f0bd74 6855 int others = 0;
c906108c 6856
43892fdf 6857 for (breakpoint *b : all_breakpoints ())
672f9b60 6858 others += (user_breakpoint_p (b)
dda83cd7 6859 && breakpoint_has_pc (b, pspace, pc, section));
43892fdf 6860
c906108c
SS
6861 if (others > 0)
6862 {
a3f17187 6863 if (others == 1)
6cb06a8c 6864 gdb_printf (_("Note: breakpoint "));
a3f17187 6865 else /* if (others == ???) */
6cb06a8c 6866 gdb_printf (_("Note: breakpoints "));
43892fdf 6867 for (breakpoint *b : all_breakpoints ())
672f9b60 6868 if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
0d381245
VP
6869 {
6870 others--;
6cb06a8c 6871 gdb_printf ("%d", b->number);
0d381245 6872 if (b->thread == -1 && thread != -1)
6cb06a8c 6873 gdb_printf (" (all threads)");
0d381245 6874 else if (b->thread != -1)
6cb06a8c
TT
6875 gdb_printf (" (thread %d)", b->thread);
6876 gdb_printf ("%s%s ",
6877 ((b->enable_state == bp_disabled
6878 || b->enable_state == bp_call_disabled)
6879 ? " (disabled)"
6880 : ""),
6881 (others > 1) ? ","
6882 : ((others == 1) ? " and" : ""));
0d381245 6883 }
6a831f06
PA
6884 current_uiout->message (_("also set at pc %ps.\n"),
6885 styled_string (address_style.style (),
6886 paddress (gdbarch, pc)));
c906108c
SS
6887 }
6888}
6889\f
c906108c 6890
cb1e4e32
PA
6891/* Return true iff it is meaningful to use the address member of LOC.
6892 For some breakpoint types, the locations' address members are
6893 irrelevant and it makes no sense to attempt to compare them to
6894 other addresses (or use them for any other purpose either).
2d134ed3 6895
cb1e4e32
PA
6896 More specifically, software watchpoints and catchpoints that are
6897 not backed by breakpoints always have a zero valued location
6898 address and we don't want to mark breakpoints of any of these types
6899 to be a duplicate of an actual breakpoint location at address
6900 zero. */
e4f237da 6901
cb1e4e32
PA
6902static bool
6903bl_address_is_meaningful (bp_location *loc)
e4f237da 6904{
cb1e4e32 6905 return loc->loc_type != bp_loc_other;
2d134ed3
PA
6906}
6907
6908/* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
6909 true if LOC1 and LOC2 represent the same watchpoint location. */
6910
6911static int
4a64f543
MS
6912watchpoint_locations_match (struct bp_location *loc1,
6913 struct bp_location *loc2)
2d134ed3 6914{
3a5c3e22
PA
6915 struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
6916 struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
6917
6918 /* Both of them must exist. */
6919 gdb_assert (w1 != NULL);
6920 gdb_assert (w2 != NULL);
2bdf28a0 6921
4a64f543
MS
6922 /* If the target can evaluate the condition expression in hardware,
6923 then we we need to insert both watchpoints even if they are at
6924 the same place. Otherwise the watchpoint will only trigger when
6925 the condition of whichever watchpoint was inserted evaluates to
6926 true, not giving a chance for GDB to check the condition of the
6927 other watchpoint. */
3a5c3e22 6928 if ((w1->cond_exp
4a64f543
MS
6929 && target_can_accel_watchpoint_condition (loc1->address,
6930 loc1->length,
0cf6dd15 6931 loc1->watchpoint_type,
4d01a485 6932 w1->cond_exp.get ()))
3a5c3e22 6933 || (w2->cond_exp
4a64f543
MS
6934 && target_can_accel_watchpoint_condition (loc2->address,
6935 loc2->length,
0cf6dd15 6936 loc2->watchpoint_type,
4d01a485 6937 w2->cond_exp.get ())))
0cf6dd15
TJB
6938 return 0;
6939
85d721b8
PA
6940 /* Note that this checks the owner's type, not the location's. In
6941 case the target does not support read watchpoints, but does
6942 support access watchpoints, we'll have bp_read_watchpoint
6943 watchpoints with hw_access locations. Those should be considered
6944 duplicates of hw_read locations. The hw_read locations will
6945 become hw_access locations later. */
2d134ed3
PA
6946 return (loc1->owner->type == loc2->owner->type
6947 && loc1->pspace->aspace == loc2->pspace->aspace
6948 && loc1->address == loc2->address
6949 && loc1->length == loc2->length);
e4f237da
KB
6950}
6951
31e77af2 6952/* See breakpoint.h. */
6c95b8df 6953
31e77af2 6954int
accd0bcd
YQ
6955breakpoint_address_match (const address_space *aspace1, CORE_ADDR addr1,
6956 const address_space *aspace2, CORE_ADDR addr2)
6c95b8df 6957{
f5656ead 6958 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
6c95b8df
PA
6959 || aspace1 == aspace2)
6960 && addr1 == addr2);
6961}
6962
f1310107
TJB
6963/* Returns true if {ASPACE2,ADDR2} falls within the range determined by
6964 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
6965 matches ASPACE2. On targets that have global breakpoints, the address
6966 space doesn't really matter. */
6967
6968static int
accd0bcd
YQ
6969breakpoint_address_match_range (const address_space *aspace1,
6970 CORE_ADDR addr1,
6971 int len1, const address_space *aspace2,
f1310107
TJB
6972 CORE_ADDR addr2)
6973{
f5656ead 6974 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
f1310107
TJB
6975 || aspace1 == aspace2)
6976 && addr2 >= addr1 && addr2 < addr1 + len1);
6977}
6978
6979/* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
6980 a ranged breakpoint. In most targets, a match happens only if ASPACE
6981 matches the breakpoint's address space. On targets that have global
6982 breakpoints, the address space doesn't really matter. */
6983
6984static int
6985breakpoint_location_address_match (struct bp_location *bl,
accd0bcd 6986 const address_space *aspace,
f1310107
TJB
6987 CORE_ADDR addr)
6988{
6989 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
6990 aspace, addr)
6991 || (bl->length
6992 && breakpoint_address_match_range (bl->pspace->aspace,
6993 bl->address, bl->length,
6994 aspace, addr)));
6995}
6996
d35ae833
PA
6997/* Returns true if the [ADDR,ADDR+LEN) range in ASPACE overlaps
6998 breakpoint BL. BL may be a ranged breakpoint. In most targets, a
6999 match happens only if ASPACE matches the breakpoint's address
7000 space. On targets that have global breakpoints, the address space
7001 doesn't really matter. */
7002
7003static int
7004breakpoint_location_address_range_overlap (struct bp_location *bl,
accd0bcd 7005 const address_space *aspace,
d35ae833
PA
7006 CORE_ADDR addr, int len)
7007{
7008 if (gdbarch_has_global_breakpoints (target_gdbarch ())
7009 || bl->pspace->aspace == aspace)
7010 {
7011 int bl_len = bl->length != 0 ? bl->length : 1;
7012
7013 if (mem_ranges_overlap (addr, len, bl->address, bl_len))
7014 return 1;
7015 }
7016 return 0;
7017}
7018
1e4d1764
YQ
7019/* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
7020 Then, if LOC1 and LOC2 represent the same tracepoint location, returns
7021 true, otherwise returns false. */
7022
7023static int
7024tracepoint_locations_match (struct bp_location *loc1,
7025 struct bp_location *loc2)
7026{
7027 if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
7028 /* Since tracepoint locations are never duplicated with others', tracepoint
7029 locations at the same address of different tracepoints are regarded as
7030 different locations. */
7031 return (loc1->address == loc2->address && loc1->owner == loc2->owner);
7032 else
7033 return 0;
7034}
7035
2d134ed3 7036/* Assuming LOC1 and LOC2's types' have meaningful target addresses
cb1e4e32 7037 (bl_address_is_meaningful), returns true if LOC1 and LOC2 represent
7f32a4d5
PA
7038 the same location. If SW_HW_BPS_MATCH is true, then software
7039 breakpoint locations and hardware breakpoint locations match,
7040 otherwise they don't. */
2d134ed3
PA
7041
7042static int
7f32a4d5
PA
7043breakpoint_locations_match (struct bp_location *loc1,
7044 struct bp_location *loc2,
7045 bool sw_hw_bps_match)
2d134ed3 7046{
2bdf28a0
JK
7047 int hw_point1, hw_point2;
7048
7049 /* Both of them must not be in moribund_locations. */
7050 gdb_assert (loc1->owner != NULL);
7051 gdb_assert (loc2->owner != NULL);
7052
7053 hw_point1 = is_hardware_watchpoint (loc1->owner);
7054 hw_point2 = is_hardware_watchpoint (loc2->owner);
2d134ed3
PA
7055
7056 if (hw_point1 != hw_point2)
7057 return 0;
7058 else if (hw_point1)
7059 return watchpoint_locations_match (loc1, loc2);
1e4d1764
YQ
7060 else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
7061 return tracepoint_locations_match (loc1, loc2);
2d134ed3 7062 else
7f32a4d5
PA
7063 /* We compare bp_location.length in order to cover ranged
7064 breakpoints. Keep this in sync with
7065 bp_location_is_less_than. */
f1310107
TJB
7066 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
7067 loc2->pspace->aspace, loc2->address)
7f32a4d5 7068 && (loc1->loc_type == loc2->loc_type || sw_hw_bps_match)
f1310107 7069 && loc1->length == loc2->length);
2d134ed3
PA
7070}
7071
76897487
KB
7072static void
7073breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
dda83cd7 7074 int bnum, int have_bnum)
76897487 7075{
f63fbe86
MS
7076 /* The longest string possibly returned by hex_string_custom
7077 is 50 chars. These must be at least that big for safety. */
7078 char astr1[64];
7079 char astr2[64];
76897487 7080
bb599908
PH
7081 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
7082 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
76897487 7083 if (have_bnum)
8a3fe4f8 7084 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
dda83cd7 7085 bnum, astr1, astr2);
76897487 7086 else
8a3fe4f8 7087 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
76897487
KB
7088}
7089
4a64f543
MS
7090/* Adjust a breakpoint's address to account for architectural
7091 constraints on breakpoint placement. Return the adjusted address.
7092 Note: Very few targets require this kind of adjustment. For most
7093 targets, this function is simply the identity function. */
76897487
KB
7094
7095static CORE_ADDR
a6d9a66e
UW
7096adjust_breakpoint_address (struct gdbarch *gdbarch,
7097 CORE_ADDR bpaddr, enum bptype bptype)
76897487 7098{
a0de8c21
YQ
7099 if (bptype == bp_watchpoint
7100 || bptype == bp_hardware_watchpoint
7101 || bptype == bp_read_watchpoint
7102 || bptype == bp_access_watchpoint
7103 || bptype == bp_catchpoint)
88f7da05
KB
7104 {
7105 /* Watchpoints and the various bp_catch_* eventpoints should not
dda83cd7 7106 have their addresses modified. */
88f7da05
KB
7107 return bpaddr;
7108 }
7c16b83e
PA
7109 else if (bptype == bp_single_step)
7110 {
7111 /* Single-step breakpoints should not have their addresses
7112 modified. If there's any architectural constrain that
7113 applies to this address, then it should have already been
7114 taken into account when the breakpoint was created in the
7115 first place. If we didn't do this, stepping through e.g.,
7116 Thumb-2 IT blocks would break. */
7117 return bpaddr;
7118 }
76897487
KB
7119 else
7120 {
a0de8c21
YQ
7121 CORE_ADDR adjusted_bpaddr = bpaddr;
7122
7123 if (gdbarch_adjust_breakpoint_address_p (gdbarch))
7124 {
7125 /* Some targets have architectural constraints on the placement
7126 of breakpoint instructions. Obtain the adjusted address. */
7127 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
7128 }
76897487 7129
a0de8c21 7130 adjusted_bpaddr = address_significant (gdbarch, adjusted_bpaddr);
76897487
KB
7131
7132 /* An adjusted breakpoint address can significantly alter
dda83cd7 7133 a user's expectations. Print a warning if an adjustment
76897487
KB
7134 is required. */
7135 if (adjusted_bpaddr != bpaddr)
7136 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
7137
7138 return adjusted_bpaddr;
7139 }
7140}
7141
cb1e4e32
PA
7142static bp_loc_type
7143bp_location_from_bp_type (bptype type)
7cc221ef 7144{
cb1e4e32 7145 switch (type)
e049a4b5
DJ
7146 {
7147 case bp_breakpoint:
7c16b83e 7148 case bp_single_step:
e049a4b5
DJ
7149 case bp_until:
7150 case bp_finish:
7151 case bp_longjmp:
7152 case bp_longjmp_resume:
e2e4d78b 7153 case bp_longjmp_call_dummy:
186c406b
TT
7154 case bp_exception:
7155 case bp_exception_resume:
e049a4b5 7156 case bp_step_resume:
2c03e5be 7157 case bp_hp_step_resume:
e049a4b5
DJ
7158 case bp_watchpoint_scope:
7159 case bp_call_dummy:
aa7d318d 7160 case bp_std_terminate:
e049a4b5
DJ
7161 case bp_shlib_event:
7162 case bp_thread_event:
7163 case bp_overlay_event:
4efc6507 7164 case bp_jit_event:
0fd8e87f 7165 case bp_longjmp_master:
aa7d318d 7166 case bp_std_terminate_master:
186c406b 7167 case bp_exception_master:
0e30163f
JK
7168 case bp_gnu_ifunc_resolver:
7169 case bp_gnu_ifunc_resolver_return:
e7e0cddf 7170 case bp_dprintf:
cb1e4e32 7171 return bp_loc_software_breakpoint;
e049a4b5 7172 case bp_hardware_breakpoint:
cb1e4e32 7173 return bp_loc_hardware_breakpoint;
e049a4b5
DJ
7174 case bp_hardware_watchpoint:
7175 case bp_read_watchpoint:
7176 case bp_access_watchpoint:
cb1e4e32 7177 return bp_loc_hardware_watchpoint;
e049a4b5 7178 case bp_watchpoint:
ce78b96d 7179 case bp_catchpoint:
15c3d785
PA
7180 case bp_tracepoint:
7181 case bp_fast_tracepoint:
0fb4aa4b 7182 case bp_static_tracepoint:
7b572efb 7183 case bp_static_marker_tracepoint:
cb1e4e32 7184 return bp_loc_other;
e049a4b5 7185 default:
e2e0b3e5 7186 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
e049a4b5 7187 }
cb1e4e32
PA
7188}
7189
7190bp_location::bp_location (breakpoint *owner, bp_loc_type type)
7191{
7192 this->owner = owner;
7193 this->cond_bytecode = NULL;
7194 this->shlib_disabled = 0;
7195 this->enabled = 1;
b5fa468f 7196 this->disabled_by_cond = false;
cb1e4e32
PA
7197
7198 this->loc_type = type;
e049a4b5 7199
cb1e4e32
PA
7200 if (this->loc_type == bp_loc_software_breakpoint
7201 || this->loc_type == bp_loc_hardware_breakpoint)
7202 mark_breakpoint_location_modified (this);
7203
b6433ede 7204 incref ();
cb1e4e32
PA
7205}
7206
7207bp_location::bp_location (breakpoint *owner)
7208 : bp_location::bp_location (owner,
7209 bp_location_from_bp_type (owner->type))
7210{
28010a5d
PA
7211}
7212
f431efe5
PA
7213/* Decrement reference count. If the reference count reaches 0,
7214 destroy the bp_location. Sets *BLP to NULL. */
7215
7216static void
7217decref_bp_location (struct bp_location **blp)
7218{
b6433ede 7219 bp_location_ref_policy::decref (*blp);
f431efe5
PA
7220 *blp = NULL;
7221}
7222
346774a9 7223/* Add breakpoint B at the end of the global breakpoint chain. */
c906108c 7224
b270e6f9
TT
7225static breakpoint *
7226add_to_breakpoint_chain (std::unique_ptr<breakpoint> &&b)
c906108c 7227{
346774a9 7228 struct breakpoint *b1;
b270e6f9 7229 struct breakpoint *result = b.get ();
c906108c 7230
346774a9
PA
7231 /* Add this breakpoint to the end of the chain so that a list of
7232 breakpoints will come out in order of increasing numbers. */
7233
7234 b1 = breakpoint_chain;
7235 if (b1 == 0)
b270e6f9 7236 breakpoint_chain = b.release ();
346774a9
PA
7237 else
7238 {
7239 while (b1->next)
7240 b1 = b1->next;
b270e6f9 7241 b1->next = b.release ();
346774a9 7242 }
b270e6f9
TT
7243
7244 return result;
346774a9
PA
7245}
7246
346774a9
PA
7247/* Helper to set_raw_breakpoint below. Creates a breakpoint
7248 that has type BPTYPE and has no locations as yet. */
346774a9
PA
7249
7250static struct breakpoint *
7251set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
9efa3c7f 7252 enum bptype bptype)
346774a9 7253{
73063f51 7254 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (gdbarch, bptype);
346774a9 7255
b270e6f9 7256 return add_to_breakpoint_chain (std::move (b));
0d381245
VP
7257}
7258
0ba852ab 7259/* Initialize loc->function_name. */
0e30163f 7260
0d381245 7261static void
0ba852ab 7262set_breakpoint_location_function (struct bp_location *loc)
0d381245 7263{
2bdf28a0
JK
7264 gdb_assert (loc->owner != NULL);
7265
0d381245 7266 if (loc->owner->type == bp_breakpoint
1042e4c0 7267 || loc->owner->type == bp_hardware_breakpoint
d77f58be 7268 || is_tracepoint (loc->owner))
0d381245 7269 {
2c02bd72 7270 const char *function_name;
0e30163f 7271
3467ec66 7272 if (loc->msymbol != NULL
60f62e2b
SM
7273 && (loc->msymbol->type () == mst_text_gnu_ifunc
7274 || loc->msymbol->type () == mst_data_gnu_ifunc))
0e30163f
JK
7275 {
7276 struct breakpoint *b = loc->owner;
7277
c9d95fa3 7278 function_name = loc->msymbol->linkage_name ();
3467ec66
PA
7279
7280 if (b->type == bp_breakpoint && b->loc == loc
7281 && loc->next == NULL && b->related_breakpoint == b)
0e30163f
JK
7282 {
7283 /* Create only the whole new breakpoint of this type but do not
7284 mess more complicated breakpoints with multiple locations. */
7285 b->type = bp_gnu_ifunc_resolver;
6a3a010b 7286 /* Remember the resolver's address for use by the return
dda83cd7 7287 breakpoint. */
3467ec66 7288 loc->related_address = loc->address;
0e30163f
JK
7289 }
7290 }
3467ec66
PA
7291 else
7292 find_pc_partial_function (loc->address, &function_name, NULL, NULL);
0e30163f 7293
2c02bd72 7294 if (function_name)
23d6ee64 7295 loc->function_name = make_unique_xstrdup (function_name);
0d381245
VP
7296 }
7297}
7298
a6d9a66e 7299/* Attempt to determine architecture of location identified by SAL. */
1bfeeb0f 7300struct gdbarch *
a6d9a66e
UW
7301get_sal_arch (struct symtab_and_line sal)
7302{
7303 if (sal.section)
08feed99 7304 return sal.section->objfile->arch ();
a6d9a66e 7305 if (sal.symtab)
3c86fae3 7306 return sal.symtab->compunit ()->objfile ()->arch ();
a6d9a66e
UW
7307
7308 return NULL;
7309}
7310
346774a9
PA
7311/* Low level routine for partially initializing a breakpoint of type
7312 BPTYPE. The newly created breakpoint's address, section, source
c56053d2 7313 file name, and line number are provided by SAL.
0d381245
VP
7314
7315 It is expected that the caller will complete the initialization of
7316 the newly created breakpoint struct as well as output any status
c56053d2 7317 information regarding the creation of a new breakpoint. */
0d381245 7318
346774a9 7319static void
73063f51
TT
7320init_raw_breakpoint (struct breakpoint *b, struct symtab_and_line sal,
7321 enum bptype bptype)
0d381245 7322{
06615628 7323 b->add_location (sal);
0d381245 7324
6c95b8df
PA
7325 if (bptype != bp_catchpoint)
7326 gdb_assert (sal.pspace != NULL);
7327
f8eba3c6
TT
7328 /* Store the program space that was used to set the breakpoint,
7329 except for ordinary breakpoints, which are independent of the
7330 program space. */
7331 if (bptype != bp_breakpoint && bptype != bp_hardware_breakpoint)
7332 b->pspace = sal.pspace;
346774a9 7333}
c906108c 7334
346774a9
PA
7335/* set_raw_breakpoint is a low level routine for allocating and
7336 partially initializing a breakpoint of type BPTYPE. The newly
7337 created breakpoint's address, section, source file name, and line
7338 number are provided by SAL. The newly created and partially
7339 initialized breakpoint is added to the breakpoint chain and
7340 is also returned as the value of this function.
7341
7342 It is expected that the caller will complete the initialization of
7343 the newly created breakpoint struct as well as output any status
7344 information regarding the creation of a new breakpoint. In
7345 particular, set_raw_breakpoint does NOT set the breakpoint
7346 number! Care should be taken to not allow an error to occur
7347 prior to completing the initialization of the breakpoint. If this
7348 should happen, a bogus breakpoint will be left on the chain. */
7349
cd0f67f3 7350static struct breakpoint *
346774a9 7351set_raw_breakpoint (struct gdbarch *gdbarch,
9efa3c7f 7352 struct symtab_and_line sal, enum bptype bptype)
346774a9 7353{
73063f51 7354 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (gdbarch, bptype);
346774a9 7355
73063f51 7356 init_raw_breakpoint (b.get (), sal, bptype);
b270e6f9 7357 return add_to_breakpoint_chain (std::move (b));
c906108c
SS
7358}
7359
53a5351d 7360/* Call this routine when stepping and nexting to enable a breakpoint
186c406b
TT
7361 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
7362 initiated the operation. */
c906108c
SS
7363
7364void
186c406b 7365set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
c906108c 7366{
5d5658a1 7367 int thread = tp->global_num;
0fd8e87f
UW
7368
7369 /* To avoid having to rescan all objfile symbols at every step,
7370 we maintain a list of continually-inserted but always disabled
7371 longjmp "master" breakpoints. Here, we simply create momentary
7372 clones of those and enable them for the requested thread. */
1428b37a 7373 for (breakpoint *b : all_breakpoints_safe ())
6c95b8df 7374 if (b->pspace == current_program_space
186c406b
TT
7375 && (b->type == bp_longjmp_master
7376 || b->type == bp_exception_master))
0fd8e87f 7377 {
06edf0c0
PA
7378 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
7379 struct breakpoint *clone;
cc59ec59 7380
e2e4d78b
JK
7381 /* longjmp_breakpoint_ops ensures INITIATING_FRAME is cleared again
7382 after their removal. */
9efa3c7f 7383 clone = momentary_breakpoint_from_master (b, type, 1);
0fd8e87f
UW
7384 clone->thread = thread;
7385 }
186c406b
TT
7386
7387 tp->initiating_frame = frame;
c906108c
SS
7388}
7389
611c83ae 7390/* Delete all longjmp breakpoints from THREAD. */
c906108c 7391void
611c83ae 7392delete_longjmp_breakpoint (int thread)
c906108c 7393{
1428b37a 7394 for (breakpoint *b : all_breakpoints_safe ())
186c406b 7395 if (b->type == bp_longjmp || b->type == bp_exception)
611c83ae
PA
7396 {
7397 if (b->thread == thread)
7398 delete_breakpoint (b);
7399 }
c906108c
SS
7400}
7401
f59f708a
PA
7402void
7403delete_longjmp_breakpoint_at_next_stop (int thread)
7404{
1428b37a 7405 for (breakpoint *b : all_breakpoints_safe ())
f59f708a
PA
7406 if (b->type == bp_longjmp || b->type == bp_exception)
7407 {
7408 if (b->thread == thread)
7409 b->disposition = disp_del_at_next_stop;
7410 }
7411}
7412
e2e4d78b
JK
7413/* Place breakpoints of type bp_longjmp_call_dummy to catch longjmp for
7414 INFERIOR_PTID thread. Chain them all by RELATED_BREAKPOINT and return
7415 pointer to any of them. Return NULL if this system cannot place longjmp
7416 breakpoints. */
7417
7418struct breakpoint *
7419set_longjmp_breakpoint_for_call_dummy (void)
7420{
43892fdf 7421 breakpoint *retval = nullptr;
e2e4d78b 7422
43892fdf 7423 for (breakpoint *b : all_breakpoints ())
e2e4d78b
JK
7424 if (b->pspace == current_program_space && b->type == bp_longjmp_master)
7425 {
7426 struct breakpoint *new_b;
7427
7428 new_b = momentary_breakpoint_from_master (b, bp_longjmp_call_dummy,
a1aa2221 7429 1);
00431a78 7430 new_b->thread = inferior_thread ()->global_num;
e2e4d78b
JK
7431
7432 /* Link NEW_B into the chain of RETVAL breakpoints. */
7433
7434 gdb_assert (new_b->related_breakpoint == new_b);
7435 if (retval == NULL)
7436 retval = new_b;
7437 new_b->related_breakpoint = retval;
7438 while (retval->related_breakpoint != new_b->related_breakpoint)
7439 retval = retval->related_breakpoint;
7440 retval->related_breakpoint = new_b;
7441 }
7442
7443 return retval;
7444}
7445
7446/* Verify all existing dummy frames and their associated breakpoints for
b67a2c6f 7447 TP. Remove those which can no longer be found in the current frame
e2e4d78b
JK
7448 stack.
7449
b4b3e2de
AB
7450 If the unwind fails then there is not sufficient information to discard
7451 dummy frames. In this case, elide the clean up and the dummy frames will
7452 be cleaned up next time this function is called from a location where
7453 unwinding is possible. */
e2e4d78b
JK
7454
7455void
b67a2c6f 7456check_longjmp_breakpoint_for_call_dummy (struct thread_info *tp)
e2e4d78b
JK
7457{
7458 struct breakpoint *b, *b_tmp;
7459
7460 ALL_BREAKPOINTS_SAFE (b, b_tmp)
5d5658a1 7461 if (b->type == bp_longjmp_call_dummy && b->thread == tp->global_num)
e2e4d78b
JK
7462 {
7463 struct breakpoint *dummy_b = b->related_breakpoint;
7464
b4b3e2de
AB
7465 /* Find the bp_call_dummy breakpoint in the list of breakpoints
7466 chained off b->related_breakpoint. */
e2e4d78b
JK
7467 while (dummy_b != b && dummy_b->type != bp_call_dummy)
7468 dummy_b = dummy_b->related_breakpoint;
b4b3e2de
AB
7469
7470 /* If there was no bp_call_dummy breakpoint then there's nothing
7471 more to do. Or, if the dummy frame associated with the
7472 bp_call_dummy is still on the stack then we need to leave this
7473 bp_call_dummy in place. */
e2e4d78b
JK
7474 if (dummy_b->type != bp_call_dummy
7475 || frame_find_by_id (dummy_b->frame_id) != NULL)
7476 continue;
b4b3e2de
AB
7477
7478 /* We didn't find the dummy frame on the stack, this could be
7479 because we have longjmp'd to a stack frame that is previous to
7480 the dummy frame, or it could be because the stack unwind is
7481 broken at some point between the longjmp frame and the dummy
7482 frame.
7483
7484 Next we figure out why the stack unwind stopped. If it looks
7485 like the unwind is complete then we assume the dummy frame has
7486 been jumped over, however, if the unwind stopped for an
7487 unexpected reason then we assume the stack unwind is currently
7488 broken, and that we will (eventually) return to the dummy
7489 frame.
7490
7491 It might be tempting to consider using frame_id_inner here, but
7492 that is not safe. There is no guarantee that the stack frames
7493 we are looking at here are even on the same stack as the
7494 original dummy frame, hence frame_id_inner can't be used. See
7495 the comments on frame_id_inner for more details. */
7496 bool unwind_finished_unexpectedly = false;
7497 for (struct frame_info *fi = get_current_frame (); fi != nullptr; )
7498 {
7499 struct frame_info *prev = get_prev_frame (fi);
7500 if (prev == nullptr)
7501 {
7502 /* FI is the last stack frame. Why did this frame not
7503 unwind further? */
7504 auto stop_reason = get_frame_unwind_stop_reason (fi);
7505 if (stop_reason != UNWIND_NO_REASON
7506 && stop_reason != UNWIND_OUTERMOST)
7507 unwind_finished_unexpectedly = true;
7508 }
7509 fi = prev;
7510 }
7511 if (unwind_finished_unexpectedly)
7512 continue;
7513
00431a78 7514 dummy_frame_discard (dummy_b->frame_id, tp);
e2e4d78b
JK
7515
7516 while (b->related_breakpoint != b)
7517 {
7518 if (b_tmp == b->related_breakpoint)
7519 b_tmp = b->related_breakpoint->next;
7520 delete_breakpoint (b->related_breakpoint);
7521 }
7522 delete_breakpoint (b);
7523 }
7524}
7525
1900040c
MS
7526void
7527enable_overlay_breakpoints (void)
7528{
43892fdf 7529 for (breakpoint *b : all_breakpoints ())
1900040c 7530 if (b->type == bp_overlay_event)
01add95b
SM
7531 {
7532 b->enable_state = bp_enabled;
7533 update_global_location_list (UGLL_MAY_INSERT);
7534 overlay_events_enabled = 1;
7535 }
1900040c
MS
7536}
7537
7538void
7539disable_overlay_breakpoints (void)
7540{
43892fdf 7541 for (breakpoint *b : all_breakpoints ())
1900040c 7542 if (b->type == bp_overlay_event)
01add95b
SM
7543 {
7544 b->enable_state = bp_disabled;
7545 update_global_location_list (UGLL_DONT_INSERT);
7546 overlay_events_enabled = 0;
7547 }
1900040c
MS
7548}
7549
aa7d318d
TT
7550/* Set an active std::terminate breakpoint for each std::terminate
7551 master breakpoint. */
7552void
7553set_std_terminate_breakpoint (void)
7554{
1428b37a 7555 for (breakpoint *b : all_breakpoints_safe ())
aa7d318d
TT
7556 if (b->pspace == current_program_space
7557 && b->type == bp_std_terminate_master)
7558 {
9efa3c7f 7559 momentary_breakpoint_from_master (b, bp_std_terminate, 1);
aa7d318d
TT
7560 }
7561}
7562
7563/* Delete all the std::terminate breakpoints. */
7564void
7565delete_std_terminate_breakpoint (void)
7566{
1428b37a 7567 for (breakpoint *b : all_breakpoints_safe ())
aa7d318d
TT
7568 if (b->type == bp_std_terminate)
7569 delete_breakpoint (b);
7570}
7571
c4093a6a 7572struct breakpoint *
a6d9a66e 7573create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
c4093a6a
JM
7574{
7575 struct breakpoint *b;
c4093a6a 7576
9efa3c7f 7577 b = create_internal_breakpoint (gdbarch, address, bp_thread_event);
06edf0c0 7578
b5de0fa7 7579 b->enable_state = bp_enabled;
f00aae0f 7580 /* location has to be used or breakpoint_re_set will delete me. */
d28cd78a 7581 b->location = new_address_location (b->loc->address, NULL, 0);
c4093a6a 7582
44702360 7583 update_global_location_list_nothrow (UGLL_MAY_INSERT);
74960c60 7584
c4093a6a
JM
7585 return b;
7586}
7587
0101ce28
JJ
7588struct lang_and_radix
7589 {
7590 enum language lang;
7591 int radix;
7592 };
7593
4efc6507
DE
7594/* Create a breakpoint for JIT code registration and unregistration. */
7595
7596struct breakpoint *
7597create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7598{
9efa3c7f 7599 return create_internal_breakpoint (gdbarch, address, bp_jit_event);
4efc6507 7600}
0101ce28 7601
03673fc7
PP
7602/* Remove JIT code registration and unregistration breakpoint(s). */
7603
7604void
7605remove_jit_event_breakpoints (void)
7606{
1428b37a 7607 for (breakpoint *b : all_breakpoints_safe ())
03673fc7
PP
7608 if (b->type == bp_jit_event
7609 && b->loc->pspace == current_program_space)
7610 delete_breakpoint (b);
7611}
7612
cae688ec
JJ
7613void
7614remove_solib_event_breakpoints (void)
7615{
1428b37a 7616 for (breakpoint *b : all_breakpoints_safe ())
6c95b8df
PA
7617 if (b->type == bp_shlib_event
7618 && b->loc->pspace == current_program_space)
cae688ec
JJ
7619 delete_breakpoint (b);
7620}
7621
f37f681c
PA
7622/* See breakpoint.h. */
7623
7624void
7625remove_solib_event_breakpoints_at_next_stop (void)
7626{
1428b37a 7627 for (breakpoint *b : all_breakpoints_safe ())
f37f681c
PA
7628 if (b->type == bp_shlib_event
7629 && b->loc->pspace == current_program_space)
7630 b->disposition = disp_del_at_next_stop;
7631}
7632
04086b45
PA
7633/* Helper for create_solib_event_breakpoint /
7634 create_and_insert_solib_event_breakpoint. Allows specifying which
7635 INSERT_MODE to pass through to update_global_location_list. */
7636
7637static struct breakpoint *
7638create_solib_event_breakpoint_1 (struct gdbarch *gdbarch, CORE_ADDR address,
7639 enum ugll_insert_mode insert_mode)
cae688ec
JJ
7640{
7641 struct breakpoint *b;
7642
9efa3c7f 7643 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event);
04086b45 7644 update_global_location_list_nothrow (insert_mode);
cae688ec
JJ
7645 return b;
7646}
7647
04086b45
PA
7648struct breakpoint *
7649create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7650{
7651 return create_solib_event_breakpoint_1 (gdbarch, address, UGLL_MAY_INSERT);
7652}
7653
f37f681c
PA
7654/* See breakpoint.h. */
7655
7656struct breakpoint *
7657create_and_insert_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7658{
7659 struct breakpoint *b;
7660
04086b45
PA
7661 /* Explicitly tell update_global_location_list to insert
7662 locations. */
7663 b = create_solib_event_breakpoint_1 (gdbarch, address, UGLL_INSERT);
f37f681c
PA
7664 if (!b->loc->inserted)
7665 {
7666 delete_breakpoint (b);
7667 return NULL;
7668 }
7669 return b;
7670}
7671
cae688ec
JJ
7672/* Disable any breakpoints that are on code in shared libraries. Only
7673 apply to enabled breakpoints, disabled ones can just stay disabled. */
7674
7675void
cb851954 7676disable_breakpoints_in_shlibs (void)
cae688ec 7677{
48d7020b 7678 for (bp_location *loc : all_bp_locations ())
01add95b
SM
7679 {
7680 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7681 struct breakpoint *b = loc->owner;
7682
7683 /* We apply the check to all breakpoints, including disabled for
7684 those with loc->duplicate set. This is so that when breakpoint
7685 becomes enabled, or the duplicate is removed, gdb will try to
7686 insert all breakpoints. If we don't set shlib_disabled here,
7687 we'll try to insert those breakpoints and fail. */
7688 if (((b->type == bp_breakpoint)
7689 || (b->type == bp_jit_event)
7690 || (b->type == bp_hardware_breakpoint)
7691 || (is_tracepoint (b)))
7692 && loc->pspace == current_program_space
7693 && !loc->shlib_disabled
7694 && solib_name_from_address (loc->pspace, loc->address)
7695 )
7696 {
7697 loc->shlib_disabled = 1;
7698 }
7699 }
cae688ec
JJ
7700}
7701
63644780
NB
7702/* Disable any breakpoints and tracepoints that are in SOLIB upon
7703 notification of unloaded_shlib. Only apply to enabled breakpoints,
7704 disabled ones can just stay disabled. */
84acb35a 7705
75149521 7706static void
84acb35a
JJ
7707disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
7708{
84acb35a
JJ
7709 int disabled_shlib_breaks = 0;
7710
48d7020b 7711 for (bp_location *loc : all_bp_locations ())
01add95b
SM
7712 {
7713 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7714 struct breakpoint *b = loc->owner;
7715
7716 if (solib->pspace == loc->pspace
7717 && !loc->shlib_disabled
7718 && (((b->type == bp_breakpoint
7719 || b->type == bp_jit_event
7720 || b->type == bp_hardware_breakpoint)
7721 && (loc->loc_type == bp_loc_hardware_breakpoint
7722 || loc->loc_type == bp_loc_software_breakpoint))
7723 || is_tracepoint (b))
7724 && solib_contains_address_p (solib, loc->address))
7725 {
7726 loc->shlib_disabled = 1;
7727 /* At this point, we cannot rely on remove_breakpoint
7728 succeeding so we must mark the breakpoint as not inserted
7729 to prevent future errors occurring in remove_breakpoints. */
7730 loc->inserted = 0;
7731
7732 /* This may cause duplicate notifications for the same breakpoint. */
7733 gdb::observers::breakpoint_modified.notify (b);
7734
7735 if (!disabled_shlib_breaks)
7736 {
7737 target_terminal::ours_for_output ();
7738 warning (_("Temporarily disabling breakpoints "
7739 "for unloaded shared library \"%s\""),
7740 solib->so_name);
7741 }
7742 disabled_shlib_breaks = 1;
7743 }
7744 }
84acb35a
JJ
7745}
7746
63644780
NB
7747/* Disable any breakpoints and tracepoints in OBJFILE upon
7748 notification of free_objfile. Only apply to enabled breakpoints,
7749 disabled ones can just stay disabled. */
7750
7751static void
7752disable_breakpoints_in_freed_objfile (struct objfile *objfile)
7753{
63644780
NB
7754 if (objfile == NULL)
7755 return;
7756
d03de421
PA
7757 /* OBJF_SHARED|OBJF_USERLOADED objfiles are dynamic modules manually
7758 managed by the user with add-symbol-file/remove-symbol-file.
7759 Similarly to how breakpoints in shared libraries are handled in
7760 response to "nosharedlibrary", mark breakpoints in such modules
08351840
PA
7761 shlib_disabled so they end up uninserted on the next global
7762 location list update. Shared libraries not loaded by the user
7763 aren't handled here -- they're already handled in
7764 disable_breakpoints_in_unloaded_shlib, called by solib.c's
7765 solib_unloaded observer. We skip objfiles that are not
d03de421
PA
7766 OBJF_SHARED as those aren't considered dynamic objects (e.g. the
7767 main objfile). */
7768 if ((objfile->flags & OBJF_SHARED) == 0
7769 || (objfile->flags & OBJF_USERLOADED) == 0)
63644780
NB
7770 return;
7771
43892fdf 7772 for (breakpoint *b : all_breakpoints ())
63644780 7773 {
63644780
NB
7774 int bp_modified = 0;
7775
7776 if (!is_breakpoint (b) && !is_tracepoint (b))
7777 continue;
7778
40cb8ca5 7779 for (bp_location *loc : b->locations ())
63644780
NB
7780 {
7781 CORE_ADDR loc_addr = loc->address;
7782
7783 if (loc->loc_type != bp_loc_hardware_breakpoint
7784 && loc->loc_type != bp_loc_software_breakpoint)
7785 continue;
7786
7787 if (loc->shlib_disabled != 0)
7788 continue;
7789
7790 if (objfile->pspace != loc->pspace)
7791 continue;
7792
7793 if (loc->loc_type != bp_loc_hardware_breakpoint
7794 && loc->loc_type != bp_loc_software_breakpoint)
7795 continue;
7796
7797 if (is_addr_in_objfile (loc_addr, objfile))
7798 {
7799 loc->shlib_disabled = 1;
08351840
PA
7800 /* At this point, we don't know whether the object was
7801 unmapped from the inferior or not, so leave the
7802 inserted flag alone. We'll handle failure to
7803 uninsert quietly, in case the object was indeed
7804 unmapped. */
63644780
NB
7805
7806 mark_breakpoint_location_modified (loc);
7807
7808 bp_modified = 1;
7809 }
7810 }
7811
7812 if (bp_modified)
76727919 7813 gdb::observers::breakpoint_modified.notify (b);
63644780
NB
7814 }
7815}
7816
b650a282 7817/* See breakpoint.h. */
ce78b96d 7818
fed1c982
TT
7819catchpoint::catchpoint (struct gdbarch *gdbarch, bool temp,
7820 const char *cond_string_)
7821 : base_breakpoint (gdbarch, bp_catchpoint)
c906108c 7822{
51abb421 7823 symtab_and_line sal;
6c95b8df 7824 sal.pspace = current_program_space;
c5aa993b 7825
fed1c982 7826 init_raw_breakpoint (this, sal, bp_catchpoint);
ce78b96d 7827
fed1c982
TT
7828 if (cond_string_ != nullptr)
7829 cond_string = make_unique_xstrdup (cond_string_);
7830 disposition = temp ? disp_del : disp_donttouch;
346774a9
PA
7831}
7832
28010a5d 7833void
b270e6f9 7834install_breakpoint (int internal, std::unique_ptr<breakpoint> &&arg, int update_gll)
c56053d2 7835{
b270e6f9 7836 breakpoint *b = add_to_breakpoint_chain (std::move (arg));
3a5c3e22 7837 set_breakpoint_number (internal, b);
558a9d82
YQ
7838 if (is_tracepoint (b))
7839 set_tracepoint_count (breakpoint_count);
3a5c3e22
PA
7840 if (!internal)
7841 mention (b);
76727919 7842 gdb::observers::breakpoint_created.notify (b);
3ea46bff
YQ
7843
7844 if (update_gll)
44702360 7845 update_global_location_list (UGLL_MAY_INSERT);
c56053d2
PA
7846}
7847
c906108c 7848static int
fba45db2 7849hw_breakpoint_used_count (void)
c906108c 7850{
c906108c
SS
7851 int i = 0;
7852
43892fdf 7853 for (breakpoint *b : all_breakpoints ())
d6b74ac4 7854 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
40cb8ca5 7855 for (bp_location *bl : b->locations ())
f1310107
TJB
7856 {
7857 /* Special types of hardware breakpoints may use more than
7858 one register. */
c47614fe 7859 i += b->resources_needed (bl);
f1310107 7860 }
c906108c
SS
7861
7862 return i;
7863}
7864
a1398e0c
PA
7865/* Returns the resources B would use if it were a hardware
7866 watchpoint. */
7867
c906108c 7868static int
a1398e0c 7869hw_watchpoint_use_count (struct breakpoint *b)
c906108c 7870{
c906108c
SS
7871 int i = 0;
7872
a1398e0c
PA
7873 if (!breakpoint_enabled (b))
7874 return 0;
7875
40cb8ca5 7876 for (bp_location *bl : b->locations ())
a1398e0c
PA
7877 {
7878 /* Special types of hardware watchpoints may use more than
7879 one register. */
c47614fe 7880 i += b->resources_needed (bl);
a1398e0c
PA
7881 }
7882
7883 return i;
7884}
7885
7886/* Returns the sum the used resources of all hardware watchpoints of
7887 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
7888 the sum of the used resources of all hardware watchpoints of other
7889 types _not_ TYPE. */
7890
7891static int
7892hw_watchpoint_used_count_others (struct breakpoint *except,
7893 enum bptype type, int *other_type_used)
7894{
7895 int i = 0;
a1398e0c 7896
c906108c 7897 *other_type_used = 0;
43892fdf 7898 for (breakpoint *b : all_breakpoints ())
e09342b5 7899 {
a1398e0c
PA
7900 if (b == except)
7901 continue;
e09342b5
TJB
7902 if (!breakpoint_enabled (b))
7903 continue;
7904
a1398e0c
PA
7905 if (b->type == type)
7906 i += hw_watchpoint_use_count (b);
7907 else if (is_hardware_watchpoint (b))
7908 *other_type_used = 1;
e09342b5
TJB
7909 }
7910
c906108c
SS
7911 return i;
7912}
7913
c906108c 7914void
fba45db2 7915disable_watchpoints_before_interactive_call_start (void)
c906108c 7916{
43892fdf 7917 for (breakpoint *b : all_breakpoints ())
cc60f2e3 7918 if (is_watchpoint (b) && breakpoint_enabled (b))
c5aa993b 7919 {
b5de0fa7 7920 b->enable_state = bp_call_disabled;
44702360 7921 update_global_location_list (UGLL_DONT_INSERT);
c5aa993b 7922 }
c906108c
SS
7923}
7924
7925void
fba45db2 7926enable_watchpoints_after_interactive_call_stop (void)
c906108c 7927{
43892fdf 7928 for (breakpoint *b : all_breakpoints ())
cc60f2e3 7929 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
c5aa993b 7930 {
b5de0fa7 7931 b->enable_state = bp_enabled;
44702360 7932 update_global_location_list (UGLL_MAY_INSERT);
c5aa993b 7933 }
c906108c
SS
7934}
7935
8bea4e01
UW
7936void
7937disable_breakpoints_before_startup (void)
7938{
6c95b8df 7939 current_program_space->executing_startup = 1;
44702360 7940 update_global_location_list (UGLL_DONT_INSERT);
8bea4e01
UW
7941}
7942
7943void
7944enable_breakpoints_after_startup (void)
7945{
6c95b8df 7946 current_program_space->executing_startup = 0;
f8eba3c6 7947 breakpoint_re_set ();
8bea4e01
UW
7948}
7949
7c16b83e
PA
7950/* Create a new single-step breakpoint for thread THREAD, with no
7951 locations. */
c906108c 7952
7c16b83e
PA
7953static struct breakpoint *
7954new_single_step_breakpoint (int thread, struct gdbarch *gdbarch)
7955{
73063f51
TT
7956 std::unique_ptr<breakpoint> b (new momentary_breakpoint (gdbarch,
7957 bp_single_step));
7c16b83e
PA
7958
7959 b->disposition = disp_donttouch;
7960 b->frame_id = null_frame_id;
7961
7962 b->thread = thread;
7963 gdb_assert (b->thread != 0);
7964
b270e6f9 7965 return add_to_breakpoint_chain (std::move (b));
7c16b83e
PA
7966}
7967
7968/* Set a momentary breakpoint of type TYPE at address specified by
7969 SAL. If FRAME_ID is valid, the breakpoint is restricted to that
7970 frame. */
c906108c 7971
454dafbd 7972breakpoint_up
a6d9a66e
UW
7973set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
7974 struct frame_id frame_id, enum bptype type)
c906108c 7975{
52f0bd74 7976 struct breakpoint *b;
edb3359d 7977
193facb3
JK
7978 /* If FRAME_ID is valid, it should be a real frame, not an inlined or
7979 tail-called one. */
7980 gdb_assert (!frame_id_artificial_p (frame_id));
edb3359d 7981
9efa3c7f 7982 b = set_raw_breakpoint (gdbarch, sal, type);
b5de0fa7
EZ
7983 b->enable_state = bp_enabled;
7984 b->disposition = disp_donttouch;
818dd999 7985 b->frame_id = frame_id;
c906108c 7986
00431a78 7987 b->thread = inferior_thread ()->global_num;
c906108c 7988
44702360 7989 update_global_location_list_nothrow (UGLL_MAY_INSERT);
74960c60 7990
454dafbd 7991 return breakpoint_up (b);
c906108c 7992}
611c83ae 7993
06edf0c0 7994/* Make a momentary breakpoint based on the master breakpoint ORIG.
a1aa2221
LM
7995 The new breakpoint will have type TYPE, use OPS as its
7996 breakpoint_ops, and will set enabled to LOC_ENABLED. */
e58b0e63 7997
06edf0c0
PA
7998static struct breakpoint *
7999momentary_breakpoint_from_master (struct breakpoint *orig,
8000 enum bptype type,
a1aa2221 8001 int loc_enabled)
e58b0e63
PA
8002{
8003 struct breakpoint *copy;
8004
9efa3c7f 8005 copy = set_raw_breakpoint_without_location (orig->gdbarch, type);
ee89d0a4 8006 copy->loc = copy->allocate_location ();
0ba852ab 8007 set_breakpoint_location_function (copy->loc);
e58b0e63 8008
a6d9a66e 8009 copy->loc->gdbarch = orig->loc->gdbarch;
e58b0e63
PA
8010 copy->loc->requested_address = orig->loc->requested_address;
8011 copy->loc->address = orig->loc->address;
8012 copy->loc->section = orig->loc->section;
6c95b8df 8013 copy->loc->pspace = orig->loc->pspace;
55aa24fb 8014 copy->loc->probe = orig->loc->probe;
f8eba3c6 8015 copy->loc->line_number = orig->loc->line_number;
2f202fde 8016 copy->loc->symtab = orig->loc->symtab;
a1aa2221 8017 copy->loc->enabled = loc_enabled;
e58b0e63
PA
8018 copy->frame_id = orig->frame_id;
8019 copy->thread = orig->thread;
6c95b8df 8020 copy->pspace = orig->pspace;
e58b0e63
PA
8021
8022 copy->enable_state = bp_enabled;
8023 copy->disposition = disp_donttouch;
8024 copy->number = internal_breakpoint_number--;
8025
44702360 8026 update_global_location_list_nothrow (UGLL_DONT_INSERT);
e58b0e63
PA
8027 return copy;
8028}
8029
06edf0c0
PA
8030/* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
8031 ORIG is NULL. */
8032
8033struct breakpoint *
8034clone_momentary_breakpoint (struct breakpoint *orig)
8035{
8036 /* If there's nothing to clone, then return nothing. */
8037 if (orig == NULL)
8038 return NULL;
8039
9efa3c7f 8040 return momentary_breakpoint_from_master (orig, orig->type, 0);
06edf0c0
PA
8041}
8042
454dafbd 8043breakpoint_up
a6d9a66e
UW
8044set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
8045 enum bptype type)
611c83ae
PA
8046{
8047 struct symtab_and_line sal;
8048
8049 sal = find_pc_line (pc, 0);
8050 sal.pc = pc;
8051 sal.section = find_pc_overlay (pc);
8052 sal.explicit_pc = 1;
8053
a6d9a66e 8054 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
611c83ae 8055}
c906108c 8056\f
c5aa993b 8057
c906108c
SS
8058/* Tell the user we have just set a breakpoint B. */
8059
8060static void
b713485d 8061mention (const breakpoint *b)
c906108c 8062{
c47614fe 8063 b->print_mention ();
2d33446d 8064 current_uiout->text ("\n");
c906108c 8065}
c906108c 8066\f
c5aa993b 8067
5133a315 8068static bool bp_loc_is_permanent (struct bp_location *loc);
1a853c52 8069
7f32a4d5
PA
8070/* Handle "set breakpoint auto-hw on".
8071
8072 If the explicitly specified breakpoint type is not hardware
8073 breakpoint, check the memory map to see whether the breakpoint
8074 address is in read-only memory.
8075
8076 - location type is not hardware breakpoint, memory is read-only.
8077 We change the type of the location to hardware breakpoint.
8078
8079 - location type is hardware breakpoint, memory is read-write. This
8080 means we've previously made the location hardware one, but then the
8081 memory map changed, so we undo.
8082*/
8083
8084static void
8085handle_automatic_hardware_breakpoints (bp_location *bl)
8086{
8087 if (automatic_hardware_breakpoints
8088 && bl->owner->type != bp_hardware_breakpoint
8089 && (bl->loc_type == bp_loc_software_breakpoint
8090 || bl->loc_type == bp_loc_hardware_breakpoint))
8091 {
8092 /* When breakpoints are removed, remove_breakpoints will use
8093 location types we've just set here, the only possible problem
8094 is that memory map has changed during running program, but
8095 it's not going to work anyway with current gdb. */
8096 mem_region *mr = lookup_mem_region (bl->address);
8097
8098 if (mr != nullptr)
8099 {
8100 enum bp_loc_type new_type;
8101
8102 if (mr->attrib.mode != MEM_RW)
8103 new_type = bp_loc_hardware_breakpoint;
8104 else
8105 new_type = bp_loc_software_breakpoint;
8106
8107 if (new_type != bl->loc_type)
8108 {
8109 static bool said = false;
8110
8111 bl->loc_type = new_type;
8112 if (!said)
8113 {
6cb06a8c
TT
8114 gdb_printf (_("Note: automatically using "
8115 "hardware breakpoints for "
8116 "read-only addresses.\n"));
7f32a4d5
PA
8117 said = true;
8118 }
8119 }
8120 }
8121 }
8122}
8123
06615628
PA
8124bp_location *
8125breakpoint::add_location (const symtab_and_line &sal)
0d381245 8126{
06615628 8127 struct bp_location *new_loc, **tmp;
3742cc8b 8128 CORE_ADDR adjusted_address;
06615628 8129 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
3742cc8b
YQ
8130
8131 if (loc_gdbarch == NULL)
06615628 8132 loc_gdbarch = gdbarch;
3742cc8b
YQ
8133
8134 /* Adjust the breakpoint's address prior to allocating a location.
ee89d0a4 8135 Once we call allocate_location(), that mostly uninitialized
3742cc8b
YQ
8136 location will be placed on the location chain. Adjustment of the
8137 breakpoint may cause target_read_memory() to be called and we do
8138 not want its scan of the location chain to find a breakpoint and
8139 location that's only been partially initialized. */
8140 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
06615628 8141 sal.pc, type);
0d381245 8142
d30113d4 8143 /* Sort the locations by their ADDRESS. */
06615628
PA
8144 new_loc = allocate_location ();
8145 for (tmp = &(loc); *tmp != NULL && (*tmp)->address <= adjusted_address;
d30113d4 8146 tmp = &((*tmp)->next))
0d381245 8147 ;
06615628
PA
8148 new_loc->next = *tmp;
8149 *tmp = new_loc;
8150
8151 new_loc->requested_address = sal.pc;
8152 new_loc->address = adjusted_address;
8153 new_loc->pspace = sal.pspace;
8154 new_loc->probe.prob = sal.prob;
8155 new_loc->probe.objfile = sal.objfile;
8156 gdb_assert (new_loc->pspace != NULL);
8157 new_loc->section = sal.section;
8158 new_loc->gdbarch = loc_gdbarch;
8159 new_loc->line_number = sal.line;
8160 new_loc->symtab = sal.symtab;
8161 new_loc->symbol = sal.symbol;
8162 new_loc->msymbol = sal.msymbol;
8163 new_loc->objfile = sal.objfile;
8164
8165 set_breakpoint_location_function (new_loc);
1a853c52 8166
6ae88661
LM
8167 /* While by definition, permanent breakpoints are already present in the
8168 code, we don't mark the location as inserted. Normally one would expect
8169 that GDB could rely on that breakpoint instruction to stop the program,
8170 thus removing the need to insert its own breakpoint, except that executing
8171 the breakpoint instruction can kill the target instead of reporting a
8172 SIGTRAP. E.g., on SPARC, when interrupts are disabled, executing the
8173 instruction resets the CPU, so QEMU 2.0.0 for SPARC correspondingly dies
8174 with "Trap 0x02 while interrupts disabled, Error state". Letting the
8175 breakpoint be inserted normally results in QEMU knowing about the GDB
8176 breakpoint, and thus trap before the breakpoint instruction is executed.
8177 (If GDB later needs to continue execution past the permanent breakpoint,
8178 it manually increments the PC, thus avoiding executing the breakpoint
8179 instruction.) */
06615628
PA
8180 if (bp_loc_is_permanent (new_loc))
8181 new_loc->permanent = 1;
1a853c52 8182
06615628 8183 return new_loc;
0d381245 8184}
514f746b
AR
8185\f
8186
5133a315
LM
8187/* Return true if LOC is pointing to a permanent breakpoint,
8188 return false otherwise. */
1cf4d951 8189
5133a315 8190static bool
1cf4d951
PA
8191bp_loc_is_permanent (struct bp_location *loc)
8192{
514f746b
AR
8193 gdb_assert (loc != NULL);
8194
cb1e4e32
PA
8195 /* If we have a non-breakpoint-backed catchpoint or a software
8196 watchpoint, just return 0. We should not attempt to read from
8197 the addresses the locations of these breakpoint types point to.
5133a315 8198 gdbarch_program_breakpoint_here_p, below, will attempt to read
244558af 8199 memory. */
cb1e4e32 8200 if (!bl_address_is_meaningful (loc))
5133a315 8201 return false;
244558af 8202
5ed8105e 8203 scoped_restore_current_pspace_and_thread restore_pspace_thread;
6c95b8df 8204 switch_to_program_space_and_thread (loc->pspace);
5133a315 8205 return gdbarch_program_breakpoint_here_p (loc->gdbarch, loc->address);
514f746b
AR
8206}
8207
e7e0cddf
SS
8208/* Build a command list for the dprintf corresponding to the current
8209 settings of the dprintf style options. */
8210
8211static void
8212update_dprintf_command_list (struct breakpoint *b)
8213{
6f781ee3 8214 const char *dprintf_args = b->extra_string.get ();
8579fd13 8215 gdb::unique_xmalloc_ptr<char> printf_line = nullptr;
e7e0cddf
SS
8216
8217 if (!dprintf_args)
8218 return;
8219
8220 dprintf_args = skip_spaces (dprintf_args);
8221
8222 /* Allow a comma, as it may have terminated a location, but don't
8223 insist on it. */
8224 if (*dprintf_args == ',')
8225 ++dprintf_args;
8226 dprintf_args = skip_spaces (dprintf_args);
8227
8228 if (*dprintf_args != '"')
8229 error (_("Bad format string, missing '\"'."));
8230
d3ce09f5 8231 if (strcmp (dprintf_style, dprintf_style_gdb) == 0)
e7e0cddf 8232 printf_line = xstrprintf ("printf %s", dprintf_args);
d3ce09f5 8233 else if (strcmp (dprintf_style, dprintf_style_call) == 0)
e7e0cddf 8234 {
e0700ba4 8235 if (dprintf_function.empty ())
e7e0cddf
SS
8236 error (_("No function supplied for dprintf call"));
8237
e0700ba4 8238 if (!dprintf_channel.empty ())
e7e0cddf 8239 printf_line = xstrprintf ("call (void) %s (%s,%s)",
e0700ba4
SM
8240 dprintf_function.c_str (),
8241 dprintf_channel.c_str (),
e7e0cddf
SS
8242 dprintf_args);
8243 else
8244 printf_line = xstrprintf ("call (void) %s (%s)",
e0700ba4 8245 dprintf_function.c_str (),
e7e0cddf
SS
8246 dprintf_args);
8247 }
d3ce09f5
SS
8248 else if (strcmp (dprintf_style, dprintf_style_agent) == 0)
8249 {
8250 if (target_can_run_breakpoint_commands ())
8251 printf_line = xstrprintf ("agent-printf %s", dprintf_args);
8252 else
8253 {
8254 warning (_("Target cannot run dprintf commands, falling back to GDB printf"));
8255 printf_line = xstrprintf ("printf %s", dprintf_args);
8256 }
8257 }
e7e0cddf
SS
8258 else
8259 internal_error (__FILE__, __LINE__,
8260 _("Invalid dprintf style."));
8261
f28045c2 8262 gdb_assert (printf_line != NULL);
e7e0cddf 8263
12973681
TT
8264 /* Manufacture a printf sequence. */
8265 struct command_line *printf_cmd_line
8579fd13 8266 = new struct command_line (simple_control, printf_line.release ());
12973681
TT
8267 breakpoint_set_commands (b, counted_command_line (printf_cmd_line,
8268 command_lines_deleter ()));
e7e0cddf
SS
8269}
8270
8271/* Update all dprintf commands, making their command lists reflect
8272 current style settings. */
8273
8274static void
eb4c3f4a 8275update_dprintf_commands (const char *args, int from_tty,
e7e0cddf
SS
8276 struct cmd_list_element *c)
8277{
43892fdf
SM
8278 for (breakpoint *b : all_breakpoints ())
8279 if (b->type == bp_dprintf)
e7e0cddf 8280 update_dprintf_command_list (b);
e7e0cddf 8281}
c3f6f71d 8282
f00aae0f
KS
8283/* Create a breakpoint with SAL as location. Use LOCATION
8284 as a description of the location, and COND_STRING
b35a8b2f
DE
8285 as condition expression. If LOCATION is NULL then create an
8286 "address location" from the address in the SAL. */
018d34a4
VP
8287
8288static void
d9b3f62e 8289init_breakpoint_sal (struct breakpoint *b, struct gdbarch *gdbarch,
6c5b2ebe 8290 gdb::array_view<const symtab_and_line> sals,
ffc2605c 8291 event_location_up &&location,
e1e01040
PA
8292 gdb::unique_xmalloc_ptr<char> filter,
8293 gdb::unique_xmalloc_ptr<char> cond_string,
8294 gdb::unique_xmalloc_ptr<char> extra_string,
d9b3f62e
PA
8295 enum bptype type, enum bpdisp disposition,
8296 int thread, int task, int ignore_count,
c0a91b2b 8297 const struct breakpoint_ops *ops, int from_tty,
44f238bb
PA
8298 int enabled, int internal, unsigned flags,
8299 int display_canonical)
018d34a4 8300{
0d381245 8301 int i;
018d34a4
VP
8302
8303 if (type == bp_hardware_breakpoint)
8304 {
fbbd034e
AS
8305 int target_resources_ok;
8306
8307 i = hw_breakpoint_used_count ();
8308 target_resources_ok =
8309 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
018d34a4
VP
8310 i + 1, 0);
8311 if (target_resources_ok == 0)
8312 error (_("No hardware breakpoint support in the target."));
8313 else if (target_resources_ok < 0)
8314 error (_("Hardware breakpoints used exceeds limit."));
8315 }
8316
6c5b2ebe 8317 gdb_assert (!sals.empty ());
6c95b8df 8318
6c5b2ebe 8319 for (const auto &sal : sals)
0d381245 8320 {
0d381245
VP
8321 struct bp_location *loc;
8322
8323 if (from_tty)
5af949e3
UW
8324 {
8325 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
8326 if (!loc_gdbarch)
8327 loc_gdbarch = gdbarch;
8328
8329 describe_other_breakpoints (loc_gdbarch,
6c95b8df 8330 sal.pspace, sal.pc, sal.section, thread);
5af949e3 8331 }
0d381245 8332
6c5b2ebe 8333 if (&sal == &sals[0])
0d381245 8334 {
73063f51 8335 init_raw_breakpoint (b, sal, type);
0d381245 8336 b->thread = thread;
4a306c9a 8337 b->task = task;
855a6e68 8338
6f781ee3
TT
8339 b->cond_string = std::move (cond_string);
8340 b->extra_string = std::move (extra_string);
0d381245 8341 b->ignore_count = ignore_count;
41447f92 8342 b->enable_state = enabled ? bp_enabled : bp_disabled;
0d381245 8343 b->disposition = disposition;
6c95b8df 8344
44f238bb
PA
8345 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
8346 b->loc->inserted = 1;
8347
7b572efb
TT
8348 if (type == bp_static_tracepoint
8349 || type == bp_static_marker_tracepoint)
0fb4aa4b 8350 {
d9b3f62e 8351 struct tracepoint *t = (struct tracepoint *) b;
0fb4aa4b
PA
8352 struct static_tracepoint_marker marker;
8353
983af33b 8354 if (strace_marker_p (b))
0fb4aa4b
PA
8355 {
8356 /* We already know the marker exists, otherwise, we
8357 wouldn't see a sal for it. */
d28cd78a
TT
8358 const char *p
8359 = &event_location_to_string (b->location.get ())[3];
f00aae0f 8360 const char *endp;
0fb4aa4b 8361
f1735a53 8362 p = skip_spaces (p);
0fb4aa4b 8363
f1735a53 8364 endp = skip_to_space (p);
0fb4aa4b 8365
5d9310c4 8366 t->static_trace_marker_id.assign (p, endp - p);
0fb4aa4b 8367
6cb06a8c
TT
8368 gdb_printf (_("Probed static tracepoint "
8369 "marker \"%s\"\n"),
8370 t->static_trace_marker_id.c_str ());
0fb4aa4b
PA
8371 }
8372 else if (target_static_tracepoint_marker_at (sal.pc, &marker))
8373 {
5d9310c4 8374 t->static_trace_marker_id = std::move (marker.str_id);
0fb4aa4b 8375
6cb06a8c
TT
8376 gdb_printf (_("Probed static tracepoint "
8377 "marker \"%s\"\n"),
8378 t->static_trace_marker_id.c_str ());
0fb4aa4b
PA
8379 }
8380 else
3e43a32a
MS
8381 warning (_("Couldn't determine the static "
8382 "tracepoint marker to probe"));
0fb4aa4b
PA
8383 }
8384
0d381245
VP
8385 loc = b->loc;
8386 }
8387 else
018d34a4 8388 {
06615628 8389 loc = b->add_location (sal);
44f238bb
PA
8390 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
8391 loc->inserted = 1;
0d381245
VP
8392 }
8393
b5fa468f
TBA
8394 /* Do not set breakpoint locations conditions yet. As locations
8395 are inserted, they get sorted based on their addresses. Let
8396 the list stabilize to have reliable location numbers. */
e7e0cddf
SS
8397
8398 /* Dynamic printf requires and uses additional arguments on the
8399 command line, otherwise it's an error. */
8400 if (type == bp_dprintf)
8401 {
8402 if (b->extra_string)
8403 update_dprintf_command_list (b);
8404 else
8405 error (_("Format string required"));
8406 }
8407 else if (b->extra_string)
6f781ee3 8408 error (_("Garbage '%s' at end of command"), b->extra_string.get ());
855a6e68 8409 }
018d34a4 8410
b5fa468f
TBA
8411
8412 /* The order of the locations is now stable. Set the location
8413 condition using the location's number. */
8414 int loc_num = 1;
40cb8ca5 8415 for (bp_location *loc : b->locations ())
b5fa468f
TBA
8416 {
8417 if (b->cond_string != nullptr)
6f781ee3
TT
8418 set_breakpoint_location_condition (b->cond_string.get (), loc,
8419 b->number, loc_num);
b5fa468f
TBA
8420
8421 ++loc_num;
8422 }
8423
56435ebe 8424 b->display_canonical = display_canonical;
f00aae0f 8425 if (location != NULL)
d28cd78a 8426 b->location = std::move (location);
018d34a4 8427 else
d28cd78a 8428 b->location = new_address_location (b->loc->address, NULL, 0);
c0e8dcd8 8429 b->filter = std::move (filter);
d9b3f62e 8430}
018d34a4 8431
d9b3f62e
PA
8432static void
8433create_breakpoint_sal (struct gdbarch *gdbarch,
6c5b2ebe 8434 gdb::array_view<const symtab_and_line> sals,
ffc2605c 8435 event_location_up &&location,
e1e01040
PA
8436 gdb::unique_xmalloc_ptr<char> filter,
8437 gdb::unique_xmalloc_ptr<char> cond_string,
8438 gdb::unique_xmalloc_ptr<char> extra_string,
d9b3f62e
PA
8439 enum bptype type, enum bpdisp disposition,
8440 int thread, int task, int ignore_count,
c0a91b2b 8441 const struct breakpoint_ops *ops, int from_tty,
44f238bb
PA
8442 int enabled, int internal, unsigned flags,
8443 int display_canonical)
d9b3f62e 8444{
73063f51 8445 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (gdbarch, type);
d9b3f62e 8446
a5e364af 8447 init_breakpoint_sal (b.get (), gdbarch,
ffc2605c 8448 sals, std::move (location),
e1e01040
PA
8449 std::move (filter),
8450 std::move (cond_string),
8451 std::move (extra_string),
d9b3f62e
PA
8452 type, disposition,
8453 thread, task, ignore_count,
8454 ops, from_tty,
44f238bb
PA
8455 enabled, internal, flags,
8456 display_canonical);
d9b3f62e 8457
b270e6f9 8458 install_breakpoint (internal, std::move (b), 0);
018d34a4
VP
8459}
8460
8461/* Add SALS.nelts breakpoints to the breakpoint table. For each
8462 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
8463 value. COND_STRING, if not NULL, specified the condition to be
8464 used for all breakpoints. Essentially the only case where
8465 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
8466 function. In that case, it's still not possible to specify
8467 separate conditions for different overloaded functions, so
8468 we take just a single condition string.
8469
c3f6f71d 8470 NOTE: If the function succeeds, the caller is expected to cleanup
018d34a4 8471 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
c3f6f71d
JM
8472 array contents). If the function fails (error() is called), the
8473 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
4a64f543 8474 COND and SALS arrays and each of those arrays contents. */
c906108c
SS
8475
8476static void
8cdf0e15 8477create_breakpoints_sal (struct gdbarch *gdbarch,
7efd8fc2 8478 struct linespec_result *canonical,
e1e01040
PA
8479 gdb::unique_xmalloc_ptr<char> cond_string,
8480 gdb::unique_xmalloc_ptr<char> extra_string,
8cdf0e15
VP
8481 enum bptype type, enum bpdisp disposition,
8482 int thread, int task, int ignore_count,
c0a91b2b 8483 const struct breakpoint_ops *ops, int from_tty,
44f238bb 8484 int enabled, int internal, unsigned flags)
c906108c 8485{
f8eba3c6 8486 if (canonical->pre_expanded)
6c5b2ebe 8487 gdb_assert (canonical->lsals.size () == 1);
f8eba3c6 8488
6c5b2ebe 8489 for (const auto &lsal : canonical->lsals)
c3f6f71d 8490 {
f00aae0f 8491 /* Note that 'location' can be NULL in the case of a plain
f8eba3c6 8492 'break', without arguments. */
ffc2605c 8493 event_location_up location
f00aae0f 8494 = (canonical->location != NULL
8e9e35b1 8495 ? copy_event_location (canonical->location.get ()) : NULL);
e1e01040 8496 gdb::unique_xmalloc_ptr<char> filter_string
6c5b2ebe 8497 (lsal.canonical != NULL ? xstrdup (lsal.canonical) : NULL);
0d381245 8498
6c5b2ebe 8499 create_breakpoint_sal (gdbarch, lsal.sals,
ffc2605c 8500 std::move (location),
e1e01040
PA
8501 std::move (filter_string),
8502 std::move (cond_string),
8503 std::move (extra_string),
e7e0cddf 8504 type, disposition,
84f4c1fe 8505 thread, task, ignore_count, ops,
44f238bb 8506 from_tty, enabled, internal, flags,
56435ebe 8507 canonical->special_display);
c3f6f71d 8508 }
c3f6f71d 8509}
c906108c 8510
f00aae0f 8511/* Parse LOCATION which is assumed to be a SAL specification possibly
c3f6f71d 8512 followed by conditionals. On return, SALS contains an array of SAL
f00aae0f
KS
8513 addresses found. LOCATION points to the end of the SAL (for
8514 linespec locations).
9998af43
TJB
8515
8516 The array and the line spec strings are allocated on the heap, it is
8517 the caller's responsibility to free them. */
c906108c 8518
b9362cc7 8519static void
626d2320 8520parse_breakpoint_sals (struct event_location *location,
58438ac1 8521 struct linespec_result *canonical)
c3f6f71d 8522{
f00aae0f
KS
8523 struct symtab_and_line cursal;
8524
8525 if (event_location_type (location) == LINESPEC_LOCATION)
8526 {
a20714ff 8527 const char *spec = get_linespec_location (location)->spec_string;
f00aae0f 8528
a20714ff 8529 if (spec == NULL)
f00aae0f
KS
8530 {
8531 /* The last displayed codepoint, if it's valid, is our default
8532 breakpoint address. */
8533 if (last_displayed_sal_is_valid ())
8534 {
f00aae0f
KS
8535 /* Set sal's pspace, pc, symtab, and line to the values
8536 corresponding to the last call to print_frame_info.
8537 Be sure to reinitialize LINE with NOTCURRENT == 0
8538 as the breakpoint line number is inappropriate otherwise.
8539 find_pc_line would adjust PC, re-set it back. */
51abb421
PA
8540 symtab_and_line sal = get_last_displayed_sal ();
8541 CORE_ADDR pc = sal.pc;
8542
f00aae0f
KS
8543 sal = find_pc_line (pc, 0);
8544
8545 /* "break" without arguments is equivalent to "break *PC"
8546 where PC is the last displayed codepoint's address. So
8547 make sure to set sal.explicit_pc to prevent GDB from
8548 trying to expand the list of sals to include all other
8549 instances with the same symtab and line. */
8550 sal.pc = pc;
8551 sal.explicit_pc = 1;
8552
6c5b2ebe
PA
8553 struct linespec_sals lsal;
8554 lsal.sals = {sal};
f00aae0f
KS
8555 lsal.canonical = NULL;
8556
6c5b2ebe 8557 canonical->lsals.push_back (std::move (lsal));
f00aae0f
KS
8558 return;
8559 }
8560 else
8561 error (_("No default breakpoint address now."));
c906108c 8562 }
c906108c 8563 }
f00aae0f
KS
8564
8565 /* Force almost all breakpoints to be in terms of the
8566 current_source_symtab (which is decode_line_1's default).
8567 This should produce the results we want almost all of the
8568 time while leaving default_breakpoint_* alone.
8569
8570 ObjC: However, don't match an Objective-C method name which
8571 may have a '+' or '-' succeeded by a '['. */
8572 cursal = get_current_source_symtab_and_line ();
8573 if (last_displayed_sal_is_valid ())
c906108c 8574 {
a20714ff 8575 const char *spec = NULL;
cc80f267 8576
f00aae0f 8577 if (event_location_type (location) == LINESPEC_LOCATION)
a20714ff 8578 spec = get_linespec_location (location)->spec_string;
cc80f267 8579
f00aae0f 8580 if (!cursal.symtab
a20714ff
PA
8581 || (spec != NULL
8582 && strchr ("+-", spec[0]) != NULL
8583 && spec[1] != '['))
f00aae0f 8584 {
c2f4122d 8585 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
f00aae0f
KS
8586 get_last_displayed_symtab (),
8587 get_last_displayed_line (),
8588 canonical, NULL, NULL);
8589 return;
8590 }
c906108c 8591 }
f00aae0f 8592
c2f4122d 8593 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
f00aae0f 8594 cursal.symtab, cursal.line, canonical, NULL, NULL);
c3f6f71d 8595}
c906108c 8596
c906108c 8597
c3f6f71d 8598/* Convert each SAL into a real PC. Verify that the PC can be
4a64f543 8599 inserted as a breakpoint. If it can't throw an error. */
c906108c 8600
b9362cc7 8601static void
6c5b2ebe 8602breakpoint_sals_to_pc (std::vector<symtab_and_line> &sals)
c3f6f71d 8603{
6c5b2ebe
PA
8604 for (auto &sal : sals)
8605 resolve_sal_pc (&sal);
c3f6f71d
JM
8606}
8607
7a697b8d
SS
8608/* Fast tracepoints may have restrictions on valid locations. For
8609 instance, a fast tracepoint using a jump instead of a trap will
8610 likely have to overwrite more bytes than a trap would, and so can
8611 only be placed where the instruction is longer than the jump, or a
8612 multi-instruction sequence does not have a jump into the middle of
8613 it, etc. */
8614
8615static void
8616check_fast_tracepoint_sals (struct gdbarch *gdbarch,
6c5b2ebe 8617 gdb::array_view<const symtab_and_line> sals)
7a697b8d 8618{
6c5b2ebe 8619 for (const auto &sal : sals)
7a697b8d 8620 {
f8eba3c6
TT
8621 struct gdbarch *sarch;
8622
6c5b2ebe 8623 sarch = get_sal_arch (sal);
f8eba3c6
TT
8624 /* We fall back to GDBARCH if there is no architecture
8625 associated with SAL. */
8626 if (sarch == NULL)
8627 sarch = gdbarch;
281d762b
TT
8628 std::string msg;
8629 if (!gdbarch_fast_tracepoint_valid_at (sarch, sal.pc, &msg))
53c3572a 8630 error (_("May not have a fast tracepoint at %s%s"),
281d762b 8631 paddress (sarch, sal.pc), msg.c_str ());
7a697b8d
SS
8632 }
8633}
8634
018d34a4
VP
8635/* Given TOK, a string specification of condition and thread, as
8636 accepted by the 'break' command, extract the condition
8637 string and thread number and set *COND_STRING and *THREAD.
4a64f543 8638 PC identifies the context at which the condition should be parsed.
018d34a4
VP
8639 If no condition is found, *COND_STRING is set to NULL.
8640 If no thread is found, *THREAD is set to -1. */
d634f2de
JB
8641
8642static void
bbc13ae3 8643find_condition_and_thread (const char *tok, CORE_ADDR pc,
6f781ee3
TT
8644 gdb::unique_xmalloc_ptr<char> *cond_string,
8645 int *thread, int *task,
8646 gdb::unique_xmalloc_ptr<char> *rest)
018d34a4 8647{
6f781ee3 8648 cond_string->reset ();
018d34a4 8649 *thread = -1;
ed1d1739 8650 *task = 0;
6f781ee3 8651 rest->reset ();
733d554a 8652 bool force = false;
ed1d1739 8653
018d34a4
VP
8654 while (tok && *tok)
8655 {
bbc13ae3 8656 const char *end_tok;
018d34a4 8657 int toklen;
bbc13ae3
KS
8658 const char *cond_start = NULL;
8659 const char *cond_end = NULL;
cc59ec59 8660
f1735a53 8661 tok = skip_spaces (tok);
e7e0cddf
SS
8662
8663 if ((*tok == '"' || *tok == ',') && rest)
8664 {
6f781ee3 8665 rest->reset (savestring (tok, strlen (tok)));
e7e0cddf
SS
8666 return;
8667 }
8668
f1735a53 8669 end_tok = skip_to_space (tok);
d634f2de 8670
018d34a4 8671 toklen = end_tok - tok;
d634f2de 8672
018d34a4
VP
8673 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
8674 {
8675 tok = cond_start = end_tok + 1;
733d554a
TBA
8676 try
8677 {
8678 parse_exp_1 (&tok, pc, block_for_pc (pc), 0);
8679 }
8680 catch (const gdb_exception_error &)
8681 {
8682 if (!force)
8683 throw;
8684 else
8685 tok = tok + strlen (tok);
8686 }
018d34a4 8687 cond_end = tok;
6f781ee3 8688 cond_string->reset (savestring (cond_start, cond_end - cond_start));
018d34a4 8689 }
733d554a
TBA
8690 else if (toklen >= 1 && strncmp (tok, "-force-condition", toklen) == 0)
8691 {
5759831a 8692 tok = tok + toklen;
733d554a
TBA
8693 force = true;
8694 }
018d34a4
VP
8695 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
8696 {
5d5658a1
PA
8697 const char *tmptok;
8698 struct thread_info *thr;
d634f2de 8699
018d34a4 8700 tok = end_tok + 1;
5d5658a1 8701 thr = parse_thread_id (tok, &tmptok);
018d34a4
VP
8702 if (tok == tmptok)
8703 error (_("Junk after thread keyword."));
5d5658a1 8704 *thread = thr->global_num;
bbc13ae3 8705 tok = tmptok;
018d34a4 8706 }
4a306c9a
JB
8707 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
8708 {
8709 char *tmptok;
8710
8711 tok = end_tok + 1;
bbc13ae3 8712 *task = strtol (tok, &tmptok, 0);
4a306c9a
JB
8713 if (tok == tmptok)
8714 error (_("Junk after task keyword."));
8715 if (!valid_task_id (*task))
b6199126 8716 error (_("Unknown task %d."), *task);
bbc13ae3 8717 tok = tmptok;
4a306c9a 8718 }
e7e0cddf
SS
8719 else if (rest)
8720 {
6f781ee3 8721 rest->reset (savestring (tok, strlen (tok)));
ccab2054 8722 return;
e7e0cddf 8723 }
018d34a4
VP
8724 else
8725 error (_("Junk at end of arguments."));
8726 }
8727}
8728
b5fa468f
TBA
8729/* Call 'find_condition_and_thread' for each sal in SALS until a parse
8730 succeeds. The parsed values are written to COND_STRING, THREAD,
8731 TASK, and REST. See the comment of 'find_condition_and_thread'
8732 for the description of these parameters and INPUT. */
8733
8734static void
8735find_condition_and_thread_for_sals (const std::vector<symtab_and_line> &sals,
6f781ee3
TT
8736 const char *input,
8737 gdb::unique_xmalloc_ptr<char> *cond_string,
8738 int *thread, int *task,
8739 gdb::unique_xmalloc_ptr<char> *rest)
b5fa468f
TBA
8740{
8741 int num_failures = 0;
8742 for (auto &sal : sals)
8743 {
6f781ee3 8744 gdb::unique_xmalloc_ptr<char> cond;
b5fa468f
TBA
8745 int thread_id = 0;
8746 int task_id = 0;
6f781ee3 8747 gdb::unique_xmalloc_ptr<char> remaining;
b5fa468f
TBA
8748
8749 /* Here we want to parse 'arg' to separate condition from thread
8750 number. But because parsing happens in a context and the
8751 contexts of sals might be different, try each until there is
8752 success. Finding one successful parse is sufficient for our
8753 goal. When setting the breakpoint we'll re-parse the
8754 condition in the context of each sal. */
8755 try
8756 {
8757 find_condition_and_thread (input, sal.pc, &cond, &thread_id,
8758 &task_id, &remaining);
6f781ee3 8759 *cond_string = std::move (cond);
b5fa468f
TBA
8760 *thread = thread_id;
8761 *task = task_id;
6f781ee3 8762 *rest = std::move (remaining);
b5fa468f
TBA
8763 break;
8764 }
8765 catch (const gdb_exception_error &e)
8766 {
8767 num_failures++;
8768 /* If no sal remains, do not continue. */
8769 if (num_failures == sals.size ())
8770 throw;
8771 }
8772 }
8773}
8774
0fb4aa4b
PA
8775/* Decode a static tracepoint marker spec. */
8776
6c5b2ebe 8777static std::vector<symtab_and_line>
f00aae0f 8778decode_static_tracepoint_spec (const char **arg_p)
0fb4aa4b 8779{
f00aae0f
KS
8780 const char *p = &(*arg_p)[3];
8781 const char *endp;
0fb4aa4b 8782
f1735a53 8783 p = skip_spaces (p);
0fb4aa4b 8784
f1735a53 8785 endp = skip_to_space (p);
0fb4aa4b 8786
81b1e71c 8787 std::string marker_str (p, endp - p);
0fb4aa4b 8788
5d9310c4
SM
8789 std::vector<static_tracepoint_marker> markers
8790 = target_static_tracepoint_markers_by_strid (marker_str.c_str ());
8791 if (markers.empty ())
81b1e71c
TT
8792 error (_("No known static tracepoint marker named %s"),
8793 marker_str.c_str ());
0fb4aa4b 8794
6c5b2ebe 8795 std::vector<symtab_and_line> sals;
5d9310c4 8796 sals.reserve (markers.size ());
0fb4aa4b 8797
5d9310c4 8798 for (const static_tracepoint_marker &marker : markers)
0fb4aa4b 8799 {
5d9310c4
SM
8800 symtab_and_line sal = find_pc_line (marker.address, 0);
8801 sal.pc = marker.address;
6c5b2ebe 8802 sals.push_back (sal);
5d9310c4 8803 }
0fb4aa4b 8804
0fb4aa4b
PA
8805 *arg_p = endp;
8806 return sals;
8807}
8808
bac7c5cf
GB
8809/* Returns the breakpoint ops appropriate for use with with LOCATION_TYPE and
8810 according to IS_TRACEPOINT. */
8811
8812static const struct breakpoint_ops *
8813breakpoint_ops_for_event_location_type (enum event_location_type location_type,
8814 bool is_tracepoint)
8815{
8816 if (is_tracepoint)
8817 {
8818 if (location_type == PROBE_LOCATION)
8819 return &tracepoint_probe_breakpoint_ops;
8820 else
a0cd5f08 8821 return &base_breakpoint_ops;
bac7c5cf
GB
8822 }
8823 else
8824 {
8825 if (location_type == PROBE_LOCATION)
8826 return &bkpt_probe_breakpoint_ops;
8827 else
a0cd5f08 8828 return &base_breakpoint_ops;
bac7c5cf
GB
8829 }
8830}
8831
8832/* See breakpoint.h. */
8833
8834const struct breakpoint_ops *
8835breakpoint_ops_for_event_location (const struct event_location *location,
8836 bool is_tracepoint)
8837{
8838 if (location != nullptr)
8839 return breakpoint_ops_for_event_location_type
8840 (event_location_type (location), is_tracepoint);
a0cd5f08 8841 return &base_breakpoint_ops;
bac7c5cf
GB
8842}
8843
f00aae0f 8844/* See breakpoint.h. */
0101ce28 8845
8cdf0e15
VP
8846int
8847create_breakpoint (struct gdbarch *gdbarch,
626d2320 8848 struct event_location *location,
e1e01040
PA
8849 const char *cond_string,
8850 int thread, const char *extra_string,
10a636cc 8851 bool force_condition, int parse_extra,
0fb4aa4b 8852 int tempflag, enum bptype type_wanted,
8cdf0e15
VP
8853 int ignore_count,
8854 enum auto_boolean pending_break_support,
c0a91b2b 8855 const struct breakpoint_ops *ops,
44f238bb
PA
8856 int from_tty, int enabled, int internal,
8857 unsigned flags)
c3f6f71d 8858{
7efd8fc2 8859 struct linespec_result canonical;
0101ce28 8860 int pending = 0;
4a306c9a 8861 int task = 0;
86b17b60 8862 int prev_bkpt_count = breakpoint_count;
c3f6f71d 8863
348d480f
PA
8864 gdb_assert (ops != NULL);
8865
f00aae0f
KS
8866 /* If extra_string isn't useful, set it to NULL. */
8867 if (extra_string != NULL && *extra_string == '\0')
8868 extra_string = NULL;
8869
a70b8144 8870 try
b78a6381 8871 {
03ada39e 8872 ops->create_sals_from_location (location, &canonical);
b78a6381 8873 }
230d2906 8874 catch (const gdb_exception_error &e)
0101ce28 8875 {
492d29ea
PA
8876 /* If caller is interested in rc value from parse, set
8877 value. */
8878 if (e.error == NOT_FOUND_ERROR)
0101ce28 8879 {
05ff989b
AC
8880 /* If pending breakpoint support is turned off, throw
8881 error. */
fa8d40ab
JJ
8882
8883 if (pending_break_support == AUTO_BOOLEAN_FALSE)
eedc3f4f 8884 throw;
723a2275
VP
8885
8886 exception_print (gdb_stderr, e);
fa8d40ab 8887
dda83cd7 8888 /* If pending breakpoint support is auto query and the user
05ff989b 8889 selects no, then simply return the error code. */
059fb39f 8890 if (pending_break_support == AUTO_BOOLEAN_AUTO
bfccc43c
YQ
8891 && !nquery (_("Make %s pending on future shared library load? "),
8892 bptype_string (type_wanted)))
fd9b8c24 8893 return 0;
fa8d40ab 8894
05ff989b
AC
8895 /* At this point, either the user was queried about setting
8896 a pending breakpoint and selected yes, or pending
8897 breakpoint behavior is on and thus a pending breakpoint
8898 is defaulted on behalf of the user. */
f00aae0f 8899 pending = 1;
0101ce28 8900 }
492d29ea 8901 else
eedc3f4f 8902 throw;
0101ce28 8903 }
492d29ea 8904
6c5b2ebe 8905 if (!pending && canonical.lsals.empty ())
492d29ea 8906 return 0;
c3f6f71d 8907
c3f6f71d
JM
8908 /* Resolve all line numbers to PC's and verify that the addresses
8909 are ok for the target. */
0101ce28 8910 if (!pending)
f8eba3c6 8911 {
6c5b2ebe
PA
8912 for (auto &lsal : canonical.lsals)
8913 breakpoint_sals_to_pc (lsal.sals);
f8eba3c6 8914 }
c3f6f71d 8915
7a697b8d 8916 /* Fast tracepoints may have additional restrictions on location. */
bfccc43c 8917 if (!pending && type_wanted == bp_fast_tracepoint)
f8eba3c6 8918 {
6c5b2ebe
PA
8919 for (const auto &lsal : canonical.lsals)
8920 check_fast_tracepoint_sals (gdbarch, lsal.sals);
f8eba3c6 8921 }
7a697b8d 8922
c3f6f71d
JM
8923 /* Verify that condition can be parsed, before setting any
8924 breakpoints. Allocate a separate condition expression for each
4a64f543 8925 breakpoint. */
0101ce28 8926 if (!pending)
c3f6f71d 8927 {
e1e01040
PA
8928 gdb::unique_xmalloc_ptr<char> cond_string_copy;
8929 gdb::unique_xmalloc_ptr<char> extra_string_copy;
8930
f00aae0f 8931 if (parse_extra)
dda83cd7 8932 {
6f781ee3
TT
8933 gdb::unique_xmalloc_ptr<char> rest;
8934 gdb::unique_xmalloc_ptr<char> cond;
52d361e1 8935
6c5b2ebe 8936 const linespec_sals &lsal = canonical.lsals[0];
52d361e1 8937
b5fa468f
TBA
8938 find_condition_and_thread_for_sals (lsal.sals, extra_string,
8939 &cond, &thread, &task, &rest);
6f781ee3
TT
8940 cond_string_copy = std::move (cond);
8941 extra_string_copy = std::move (rest);
dda83cd7 8942 }
2f069f6f 8943 else
dda83cd7 8944 {
f00aae0f
KS
8945 if (type_wanted != bp_dprintf
8946 && extra_string != NULL && *extra_string != '\0')
8947 error (_("Garbage '%s' at end of location"), extra_string);
0878d0fa 8948
10a636cc
TBA
8949 /* Check the validity of the condition. We should error out
8950 if the condition is invalid at all of the locations and
8951 if it is not forced. In the PARSE_EXTRA case above, this
8952 check is done when parsing the EXTRA_STRING. */
8953 if (cond_string != nullptr && !force_condition)
8954 {
8955 int num_failures = 0;
8956 const linespec_sals &lsal = canonical.lsals[0];
8957 for (const auto &sal : lsal.sals)
8958 {
8959 const char *cond = cond_string;
8960 try
8961 {
8962 parse_exp_1 (&cond, sal.pc, block_for_pc (sal.pc), 0);
8963 /* One success is sufficient to keep going. */
8964 break;
8965 }
8966 catch (const gdb_exception_error &)
8967 {
8968 num_failures++;
8969 /* If this is the last sal, error out. */
8970 if (num_failures == lsal.sals.size ())
8971 throw;
8972 }
8973 }
8974 }
8975
0878d0fa
YQ
8976 /* Create a private copy of condition string. */
8977 if (cond_string)
e1e01040 8978 cond_string_copy.reset (xstrdup (cond_string));
0878d0fa
YQ
8979 /* Create a private copy of any extra string. */
8980 if (extra_string)
e1e01040 8981 extra_string_copy.reset (xstrdup (extra_string));
dda83cd7 8982 }
0fb4aa4b 8983
52d361e1 8984 ops->create_breakpoints_sal (gdbarch, &canonical,
e1e01040
PA
8985 std::move (cond_string_copy),
8986 std::move (extra_string_copy),
8987 type_wanted,
d9b3f62e
PA
8988 tempflag ? disp_del : disp_donttouch,
8989 thread, task, ignore_count, ops,
44f238bb 8990 from_tty, enabled, internal, flags);
c906108c 8991 }
0101ce28
JJ
8992 else
8993 {
73063f51
TT
8994 std::unique_ptr <breakpoint> b = new_breakpoint_from_type (gdbarch,
8995 type_wanted);
d28cd78a 8996 b->location = copy_event_location (location);
bfccc43c 8997
f00aae0f
KS
8998 if (parse_extra)
8999 b->cond_string = NULL;
e12c7713
MK
9000 else
9001 {
9002 /* Create a private copy of condition string. */
6f781ee3
TT
9003 b->cond_string.reset (cond_string != NULL
9004 ? xstrdup (cond_string)
9005 : NULL);
15630549 9006 b->thread = thread;
e12c7713 9007 }
f00aae0f
KS
9008
9009 /* Create a private copy of any extra string. */
6f781ee3
TT
9010 b->extra_string.reset (extra_string != NULL
9011 ? xstrdup (extra_string)
9012 : NULL);
0101ce28 9013 b->ignore_count = ignore_count;
0101ce28 9014 b->disposition = tempflag ? disp_del : disp_donttouch;
0d381245 9015 b->condition_not_parsed = 1;
41447f92 9016 b->enable_state = enabled ? bp_enabled : bp_disabled;
cc72b2a2 9017 if ((type_wanted != bp_breakpoint
dda83cd7 9018 && type_wanted != bp_hardware_breakpoint) || thread != -1)
f8eba3c6 9019 b->pspace = current_program_space;
8bea4e01 9020
b270e6f9 9021 install_breakpoint (internal, std::move (b), 0);
0101ce28
JJ
9022 }
9023
6c5b2ebe 9024 if (canonical.lsals.size () > 1)
95a42b64 9025 {
3e43a32a
MS
9026 warning (_("Multiple breakpoints were set.\nUse the "
9027 "\"delete\" command to delete unwanted breakpoints."));
86b17b60 9028 prev_breakpoint_count = prev_bkpt_count;
95a42b64
TT
9029 }
9030
44702360 9031 update_global_location_list (UGLL_MAY_INSERT);
fd9b8c24
PA
9032
9033 return 1;
c3f6f71d 9034}
c906108c 9035
348d480f 9036/* Set a breakpoint.
72b2ff0e
VP
9037 ARG is a string describing breakpoint address,
9038 condition, and thread.
9039 FLAG specifies if a breakpoint is hardware on,
9040 and if breakpoint is temporary, using BP_HARDWARE_FLAG
9041 and BP_TEMPFLAG. */
348d480f 9042
98deb0da 9043static void
f2fc3015 9044break_command_1 (const char *arg, int flag, int from_tty)
c3f6f71d 9045{
72b2ff0e 9046 int tempflag = flag & BP_TEMPFLAG;
0fb4aa4b
PA
9047 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
9048 ? bp_hardware_breakpoint
9049 : bp_breakpoint);
f00aae0f 9050
ffc2605c 9051 event_location_up location = string_to_event_location (&arg, current_language);
bac7c5cf
GB
9052 const struct breakpoint_ops *ops = breakpoint_ops_for_event_location
9053 (location.get (), false /* is_tracepoint */);
c3f6f71d 9054
8cdf0e15 9055 create_breakpoint (get_current_arch (),
ffc2605c 9056 location.get (),
10a636cc 9057 NULL, 0, arg, false, 1 /* parse arg */,
0fb4aa4b 9058 tempflag, type_wanted,
8cdf0e15
VP
9059 0 /* Ignore count */,
9060 pending_break_support,
55aa24fb 9061 ops,
8cdf0e15 9062 from_tty,
84f4c1fe 9063 1 /* enabled */,
44f238bb
PA
9064 0 /* internal */,
9065 0);
c906108c
SS
9066}
9067
c906108c
SS
9068/* Helper function for break_command_1 and disassemble_command. */
9069
9070void
fba45db2 9071resolve_sal_pc (struct symtab_and_line *sal)
c906108c
SS
9072{
9073 CORE_ADDR pc;
9074
9075 if (sal->pc == 0 && sal->symtab != NULL)
9076 {
9077 if (!find_line_pc (sal->symtab, sal->line, &pc))
8a3fe4f8 9078 error (_("No line %d in file \"%s\"."),
05cba821 9079 sal->line, symtab_to_filename_for_display (sal->symtab));
c906108c 9080 sal->pc = pc;
6a048695 9081
4a64f543 9082 /* If this SAL corresponds to a breakpoint inserted using a line
dda83cd7 9083 number, then skip the function prologue if necessary. */
6a048695 9084 if (sal->explicit_line)
059acae7 9085 skip_prologue_sal (sal);
c906108c
SS
9086 }
9087
9088 if (sal->section == 0 && sal->symtab != NULL)
9089 {
346d1dfe 9090 const struct blockvector *bv;
3977b71f 9091 const struct block *b;
c5aa993b 9092 struct symbol *sym;
c906108c 9093
43f3e411 9094 bv = blockvector_for_pc_sect (sal->pc, 0, &b,
c6159652 9095 sal->symtab->compunit ());
c906108c
SS
9096 if (bv != NULL)
9097 {
7f0df278 9098 sym = block_linkage_function (b);
c906108c
SS
9099 if (sym != NULL)
9100 {
3c86fae3
SM
9101 fixup_symbol_section (sym, sal->symtab->compunit ()->objfile ());
9102 sal->section
9103 = sym->obj_section (sal->symtab->compunit ()->objfile ());
c906108c
SS
9104 }
9105 else
9106 {
4a64f543 9107 /* It really is worthwhile to have the section, so we'll
dda83cd7
SM
9108 just have to look harder. This case can be executed
9109 if we have line numbers but no functions (as can
9110 happen in assembly source). */
c906108c 9111
5ed8105e 9112 scoped_restore_current_pspace_and_thread restore_pspace_thread;
6c95b8df 9113 switch_to_program_space_and_thread (sal->pspace);
c906108c 9114
5ed8105e 9115 bound_minimal_symbol msym = lookup_minimal_symbol_by_pc (sal->pc);
7cbd4a93 9116 if (msym.minsym)
1db66e34 9117 sal->section = msym.obj_section ();
c906108c
SS
9118 }
9119 }
9120 }
9121}
9122
9123void
0b39b52e 9124break_command (const char *arg, int from_tty)
c906108c 9125{
db107f19 9126 break_command_1 (arg, 0, from_tty);
c906108c
SS
9127}
9128
c906108c 9129void
0b39b52e 9130tbreak_command (const char *arg, int from_tty)
c906108c 9131{
db107f19 9132 break_command_1 (arg, BP_TEMPFLAG, from_tty);
c906108c
SS
9133}
9134
c906108c 9135static void
0b39b52e 9136hbreak_command (const char *arg, int from_tty)
c906108c 9137{
db107f19 9138 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
c906108c
SS
9139}
9140
9141static void
0b39b52e 9142thbreak_command (const char *arg, int from_tty)
c906108c 9143{
db107f19 9144 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
c906108c
SS
9145}
9146
e7e0cddf
SS
9147/* The dynamic printf command is mostly like a regular breakpoint, but
9148 with a prewired command list consisting of a single output command,
9149 built from extra arguments supplied on the dprintf command
9150 line. */
9151
da821c7b 9152static void
0b39b52e 9153dprintf_command (const char *arg, int from_tty)
e7e0cddf 9154{
ffc2605c 9155 event_location_up location = string_to_event_location (&arg, current_language);
f00aae0f
KS
9156
9157 /* If non-NULL, ARG should have been advanced past the location;
9158 the next character must be ','. */
9159 if (arg != NULL)
9160 {
9161 if (arg[0] != ',' || arg[1] == '\0')
9162 error (_("Format string required"));
9163 else
9164 {
9165 /* Skip the comma. */
9166 ++arg;
9167 }
9168 }
9169
e7e0cddf 9170 create_breakpoint (get_current_arch (),
ffc2605c 9171 location.get (),
10a636cc 9172 NULL, 0, arg, false, 1 /* parse arg */,
e7e0cddf
SS
9173 0, bp_dprintf,
9174 0 /* Ignore count */,
9175 pending_break_support,
a0cd5f08 9176 &base_breakpoint_ops,
e7e0cddf
SS
9177 from_tty,
9178 1 /* enabled */,
9179 0 /* internal */,
9180 0);
9181}
9182
d3ce09f5 9183static void
0b39b52e 9184agent_printf_command (const char *arg, int from_tty)
d3ce09f5
SS
9185{
9186 error (_("May only run agent-printf on the target"));
9187}
9188
ec45bb67 9189/* Implement the "breakpoint_hit" method for ranged breakpoints. */
f1310107 9190
ec45bb67
TT
9191int
9192ranged_breakpoint::breakpoint_hit (const struct bp_location *bl,
9193 const address_space *aspace,
9194 CORE_ADDR bp_addr,
9195 const target_waitstatus &ws)
f1310107 9196{
c272a98c
SM
9197 if (ws.kind () != TARGET_WAITKIND_STOPPED
9198 || ws.sig () != GDB_SIGNAL_TRAP)
09ac7c10
TT
9199 return 0;
9200
f1310107
TJB
9201 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
9202 bl->length, aspace, bp_addr);
9203}
9204
ec45bb67 9205/* Implement the "resources_needed" method for ranged breakpoints. */
f1310107 9206
ec45bb67
TT
9207int
9208ranged_breakpoint::resources_needed (const struct bp_location *bl)
f1310107
TJB
9209{
9210 return target_ranged_break_num_registers ();
9211}
9212
ec45bb67 9213/* Implement the "print_it" method for ranged breakpoints. */
f1310107 9214
ec45bb67 9215enum print_stop_action
7bd86313 9216ranged_breakpoint::print_it (const bpstat *bs) const
f1310107 9217{
ec45bb67 9218 struct bp_location *bl = loc;
79a45e25 9219 struct ui_out *uiout = current_uiout;
f1310107 9220
ec45bb67 9221 gdb_assert (type == bp_hardware_breakpoint);
f1310107
TJB
9222
9223 /* Ranged breakpoints have only one location. */
9224 gdb_assert (bl && bl->next == NULL);
9225
ec45bb67 9226 annotate_breakpoint (number);
f303dbd6
PA
9227
9228 maybe_print_thread_hit_breakpoint (uiout);
9229
ec45bb67 9230 if (disposition == disp_del)
112e8700 9231 uiout->text ("Temporary ranged breakpoint ");
f1310107 9232 else
112e8700
SM
9233 uiout->text ("Ranged breakpoint ");
9234 if (uiout->is_mi_like_p ())
f1310107 9235 {
112e8700 9236 uiout->field_string ("reason",
f1310107 9237 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
ec45bb67 9238 uiout->field_string ("disp", bpdisp_text (disposition));
f1310107 9239 }
ec45bb67 9240 uiout->field_signed ("bkptno", number);
112e8700 9241 uiout->text (", ");
f1310107
TJB
9242
9243 return PRINT_SRC_AND_LOC;
9244}
9245
ec45bb67 9246/* Implement the "print_one" method for ranged breakpoints. */
f1310107 9247
ec45bb67 9248bool
a67bcaba 9249ranged_breakpoint::print_one (bp_location **last_loc) const
f1310107 9250{
ec45bb67 9251 struct bp_location *bl = loc;
f1310107 9252 struct value_print_options opts;
79a45e25 9253 struct ui_out *uiout = current_uiout;
f1310107
TJB
9254
9255 /* Ranged breakpoints have only one location. */
9256 gdb_assert (bl && bl->next == NULL);
9257
9258 get_user_print_options (&opts);
9259
9260 if (opts.addressprint)
9261 /* We don't print the address range here, it will be printed later
9262 by print_one_detail_ranged_breakpoint. */
112e8700 9263 uiout->field_skip ("addr");
f1310107 9264 annotate_field (5);
ec45bb67 9265 print_breakpoint_location (this, bl);
f1310107 9266 *last_loc = bl;
c01e038b
TT
9267
9268 return true;
f1310107
TJB
9269}
9270
ec45bb67 9271/* Implement the "print_one_detail" method for ranged breakpoints. */
f1310107 9272
ec45bb67
TT
9273void
9274ranged_breakpoint::print_one_detail (struct ui_out *uiout) const
f1310107
TJB
9275{
9276 CORE_ADDR address_start, address_end;
ec45bb67 9277 struct bp_location *bl = loc;
d7e74731 9278 string_file stb;
f1310107
TJB
9279
9280 gdb_assert (bl);
9281
9282 address_start = bl->address;
9283 address_end = address_start + bl->length - 1;
9284
112e8700 9285 uiout->text ("\taddress range: ");
d7e74731
PA
9286 stb.printf ("[%s, %s]",
9287 print_core_address (bl->gdbarch, address_start),
9288 print_core_address (bl->gdbarch, address_end));
112e8700
SM
9289 uiout->field_stream ("addr", stb);
9290 uiout->text ("\n");
f1310107
TJB
9291}
9292
ec45bb67 9293/* Implement the "print_mention" method for ranged breakpoints. */
f1310107 9294
ec45bb67 9295void
b713485d 9296ranged_breakpoint::print_mention () const
f1310107 9297{
ec45bb67 9298 struct bp_location *bl = loc;
79a45e25 9299 struct ui_out *uiout = current_uiout;
f1310107
TJB
9300
9301 gdb_assert (bl);
ec45bb67 9302 gdb_assert (type == bp_hardware_breakpoint);
f1310107 9303
2d33446d 9304 uiout->message (_("Hardware assisted ranged breakpoint %d from %s to %s."),
ec45bb67 9305 number, paddress (bl->gdbarch, bl->address),
2d33446d 9306 paddress (bl->gdbarch, bl->address + bl->length - 1));
f1310107
TJB
9307}
9308
ec45bb67 9309/* Implement the "print_recreate" method for ranged breakpoints. */
f1310107 9310
ec45bb67 9311void
4d1ae558 9312ranged_breakpoint::print_recreate (struct ui_file *fp) const
f1310107 9313{
6cb06a8c 9314 gdb_printf (fp, "break-range %s, %s",
ec45bb67
TT
9315 event_location_to_string (location.get ()),
9316 event_location_to_string (location_range_end.get ()));
04d0163c 9317 print_recreate_thread (fp);
f1310107
TJB
9318}
9319
f1310107
TJB
9320/* Find the address where the end of the breakpoint range should be
9321 placed, given the SAL of the end of the range. This is so that if
9322 the user provides a line number, the end of the range is set to the
9323 last instruction of the given line. */
9324
9325static CORE_ADDR
9326find_breakpoint_range_end (struct symtab_and_line sal)
9327{
9328 CORE_ADDR end;
9329
9330 /* If the user provided a PC value, use it. Otherwise,
9331 find the address of the end of the given location. */
9332 if (sal.explicit_pc)
9333 end = sal.pc;
9334 else
9335 {
9336 int ret;
9337 CORE_ADDR start;
9338
9339 ret = find_line_pc_range (sal, &start, &end);
9340 if (!ret)
9341 error (_("Could not find location of the end of the range."));
9342
9343 /* find_line_pc_range returns the start of the next line. */
9344 end--;
9345 }
9346
9347 return end;
9348}
9349
9350/* Implement the "break-range" CLI command. */
9351
9352static void
0b39b52e 9353break_range_command (const char *arg, int from_tty)
f1310107 9354{
f2fc3015 9355 const char *arg_start;
f1310107
TJB
9356 struct linespec_result canonical_start, canonical_end;
9357 int bp_count, can_use_bp, length;
9358 CORE_ADDR end;
9359 struct breakpoint *b;
f1310107
TJB
9360
9361 /* We don't support software ranged breakpoints. */
9362 if (target_ranged_break_num_registers () < 0)
9363 error (_("This target does not support hardware ranged breakpoints."));
9364
9365 bp_count = hw_breakpoint_used_count ();
9366 bp_count += target_ranged_break_num_registers ();
9367 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
9368 bp_count, 0);
9369 if (can_use_bp < 0)
9370 error (_("Hardware breakpoints used exceeds limit."));
9371
f8eba3c6 9372 arg = skip_spaces (arg);
f1310107
TJB
9373 if (arg == NULL || arg[0] == '\0')
9374 error(_("No address range specified."));
9375
f8eba3c6 9376 arg_start = arg;
ffc2605c
TT
9377 event_location_up start_location = string_to_event_location (&arg,
9378 current_language);
9379 parse_breakpoint_sals (start_location.get (), &canonical_start);
f1310107
TJB
9380
9381 if (arg[0] != ',')
9382 error (_("Too few arguments."));
6c5b2ebe 9383 else if (canonical_start.lsals.empty ())
f1310107 9384 error (_("Could not find location of the beginning of the range."));
f8eba3c6 9385
6c5b2ebe 9386 const linespec_sals &lsal_start = canonical_start.lsals[0];
f8eba3c6 9387
6c5b2ebe
PA
9388 if (canonical_start.lsals.size () > 1
9389 || lsal_start.sals.size () != 1)
f1310107
TJB
9390 error (_("Cannot create a ranged breakpoint with multiple locations."));
9391
6c5b2ebe 9392 const symtab_and_line &sal_start = lsal_start.sals[0];
81b1e71c 9393 std::string addr_string_start (arg_start, arg - arg_start);
f1310107
TJB
9394
9395 arg++; /* Skip the comma. */
f8eba3c6 9396 arg = skip_spaces (arg);
f1310107
TJB
9397
9398 /* Parse the end location. */
9399
f1310107
TJB
9400 arg_start = arg;
9401
f8eba3c6 9402 /* We call decode_line_full directly here instead of using
f1310107
TJB
9403 parse_breakpoint_sals because we need to specify the start location's
9404 symtab and line as the default symtab and line for the end of the
9405 range. This makes it possible to have ranges like "foo.c:27, +14",
9406 where +14 means 14 lines from the start location. */
ffc2605c
TT
9407 event_location_up end_location = string_to_event_location (&arg,
9408 current_language);
9409 decode_line_full (end_location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
f8eba3c6
TT
9410 sal_start.symtab, sal_start.line,
9411 &canonical_end, NULL, NULL);
9412
6c5b2ebe 9413 if (canonical_end.lsals.empty ())
f1310107 9414 error (_("Could not find location of the end of the range."));
f8eba3c6 9415
6c5b2ebe
PA
9416 const linespec_sals &lsal_end = canonical_end.lsals[0];
9417 if (canonical_end.lsals.size () > 1
9418 || lsal_end.sals.size () != 1)
f1310107
TJB
9419 error (_("Cannot create a ranged breakpoint with multiple locations."));
9420
6c5b2ebe 9421 const symtab_and_line &sal_end = lsal_end.sals[0];
f1310107
TJB
9422
9423 end = find_breakpoint_range_end (sal_end);
9424 if (sal_start.pc > end)
177b42fe 9425 error (_("Invalid address range, end precedes start."));
f1310107
TJB
9426
9427 length = end - sal_start.pc + 1;
9428 if (length < 0)
9429 /* Length overflowed. */
9430 error (_("Address range too large."));
9431 else if (length == 1)
9432 {
9433 /* This range is simple enough to be handled by
9434 the `hbreak' command. */
81b1e71c 9435 hbreak_command (&addr_string_start[0], 1);
f1310107
TJB
9436
9437 return;
9438 }
9439
9440 /* Now set up the breakpoint. */
73063f51 9441 std::unique_ptr<breakpoint> br (new ranged_breakpoint (get_current_arch ()));
f317d1eb 9442 br->add_location (sal_start);
ec45bb67
TT
9443 b = add_to_breakpoint_chain (std::move (br));
9444
f1310107
TJB
9445 set_breakpoint_count (breakpoint_count + 1);
9446 b->number = breakpoint_count;
9447 b->disposition = disp_donttouch;
d28cd78a
TT
9448 b->location = std::move (start_location);
9449 b->location_range_end = std::move (end_location);
f1310107
TJB
9450 b->loc->length = length;
9451
f1310107 9452 mention (b);
76727919 9453 gdb::observers::breakpoint_created.notify (b);
44702360 9454 update_global_location_list (UGLL_MAY_INSERT);
f1310107
TJB
9455}
9456
4a64f543
MS
9457/* Return non-zero if EXP is verified as constant. Returned zero
9458 means EXP is variable. Also the constant detection may fail for
9459 some constant expressions and in such case still falsely return
9460 zero. */
2e6e3d9c 9461
dab72643 9462static bool
65d79d4b
SDJ
9463watchpoint_exp_is_const (const struct expression *exp)
9464{
1eaebe02 9465 return exp->op->constant_p ();
65d79d4b
SDJ
9466}
9467
3a292923 9468/* Implement the "re_set" method for watchpoints. */
348d480f 9469
3a292923
TT
9470void
9471watchpoint::re_set ()
348d480f
PA
9472{
9473 /* Watchpoint can be either on expression using entirely global
9474 variables, or it can be on local variables.
9475
9476 Watchpoints of the first kind are never auto-deleted, and even
9477 persist across program restarts. Since they can use variables
9478 from shared libraries, we need to reparse expression as libraries
9479 are loaded and unloaded.
9480
9481 Watchpoints on local variables can also change meaning as result
9482 of solib event. For example, if a watchpoint uses both a local
9483 and a global variables in expression, it's a local watchpoint,
9484 but unloading of a shared library will make the expression
9485 invalid. This is not a very common use case, but we still
9486 re-evaluate expression, to avoid surprises to the user.
9487
9488 Note that for local watchpoints, we re-evaluate it only if
9489 watchpoints frame id is still valid. If it's not, it means the
9490 watchpoint is out of scope and will be deleted soon. In fact,
9491 I'm not sure we'll ever be called in this case.
9492
9493 If a local watchpoint's frame id is still valid, then
3a292923 9494 exp_valid_block is likewise valid, and we can safely use it.
348d480f 9495
3a5c3e22
PA
9496 Don't do anything about disabled watchpoints, since they will be
9497 reevaluated again when enabled. */
3a292923 9498 update_watchpoint (this, 1 /* reparse */);
348d480f
PA
9499}
9500
3a292923 9501/* Implement the "insert" method for hardware watchpoints. */
77b06cd7 9502
3a292923
TT
9503int
9504watchpoint::insert_location (struct bp_location *bl)
77b06cd7 9505{
3a292923 9506 int length = exact ? 1 : bl->length;
e09342b5
TJB
9507
9508 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
3a292923 9509 cond_exp.get ());
77b06cd7
TJB
9510}
9511
3a292923 9512/* Implement the "remove" method for hardware watchpoints. */
77b06cd7 9513
3a292923
TT
9514int
9515watchpoint::remove_location (struct bp_location *bl,
9516 enum remove_bp_reason reason)
77b06cd7 9517{
3a292923 9518 int length = exact ? 1 : bl->length;
e09342b5
TJB
9519
9520 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
3a292923 9521 cond_exp.get ());
e09342b5
TJB
9522}
9523
3a292923
TT
9524int
9525watchpoint::breakpoint_hit (const struct bp_location *bl,
9526 const address_space *aspace, CORE_ADDR bp_addr,
9527 const target_waitstatus &ws)
e09342b5 9528{
348d480f 9529 struct breakpoint *b = bl->owner;
77b06cd7 9530
348d480f
PA
9531 /* Continuable hardware watchpoints are treated as non-existent if the
9532 reason we stopped wasn't a hardware watchpoint (we didn't stop on
9533 some data address). Otherwise gdb won't stop on a break instruction
9534 in the code (not from a breakpoint) when a hardware watchpoint has
9535 been defined. Also skip watchpoints which we know did not trigger
9536 (did not match the data address). */
9537 if (is_hardware_watchpoint (b)
3a292923 9538 && watchpoint_triggered == watch_triggered_no)
348d480f 9539 return 0;
9c06b0b4 9540
348d480f 9541 return 1;
9c06b0b4
TJB
9542}
9543
3a292923
TT
9544void
9545watchpoint::check_status (bpstat *bs)
9c06b0b4 9546{
348d480f 9547 gdb_assert (is_watchpoint (bs->breakpoint_at));
9c06b0b4 9548
348d480f 9549 bpstat_check_watchpoint (bs);
9c06b0b4
TJB
9550}
9551
3a292923
TT
9552/* Implement the "resources_needed" method for hardware
9553 watchpoints. */
9c06b0b4 9554
3a292923
TT
9555int
9556watchpoint::resources_needed (const struct bp_location *bl)
9c06b0b4 9557{
3a292923 9558 int length = exact? 1 : bl->length;
348d480f
PA
9559
9560 return target_region_ok_for_hw_watchpoint (bl->address, length);
9c06b0b4
TJB
9561}
9562
3a292923
TT
9563/* Implement the "works_in_software_mode" method for hardware
9564 watchpoints. */
9c06b0b4 9565
5a61e176 9566bool
3a292923 9567watchpoint::works_in_software_mode () const
9c06b0b4 9568{
efa80663 9569 /* Read and access watchpoints only work with hardware support. */
3a292923 9570 return type == bp_watchpoint || type == bp_hardware_watchpoint;
9c06b0b4
TJB
9571}
9572
3a292923 9573enum print_stop_action
7bd86313 9574watchpoint::print_it (const bpstat *bs) const
9c06b0b4 9575{
348d480f 9576 struct breakpoint *b;
348d480f 9577 enum print_stop_action result;
79a45e25 9578 struct ui_out *uiout = current_uiout;
348d480f
PA
9579
9580 gdb_assert (bs->bp_location_at != NULL);
9581
348d480f
PA
9582 b = bs->breakpoint_at;
9583
f303dbd6
PA
9584 annotate_watchpoint (b->number);
9585 maybe_print_thread_hit_breakpoint (uiout);
9586
d7e74731
PA
9587 string_file stb;
9588
76f9c9cf 9589 gdb::optional<ui_out_emit_tuple> tuple_emitter;
9c06b0b4
TJB
9590 switch (b->type)
9591 {
348d480f 9592 case bp_watchpoint:
9c06b0b4 9593 case bp_hardware_watchpoint:
112e8700
SM
9594 if (uiout->is_mi_like_p ())
9595 uiout->field_string
9596 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
348d480f 9597 mention (b);
76f9c9cf 9598 tuple_emitter.emplace (uiout, "value");
112e8700 9599 uiout->text ("\nOld value = ");
850645cf 9600 watchpoint_value_print (bs->old_val.get (), &stb);
112e8700
SM
9601 uiout->field_stream ("old", stb);
9602 uiout->text ("\nNew value = ");
3a292923 9603 watchpoint_value_print (val.get (), &stb);
112e8700
SM
9604 uiout->field_stream ("new", stb);
9605 uiout->text ("\n");
348d480f
PA
9606 /* More than one watchpoint may have been triggered. */
9607 result = PRINT_UNKNOWN;
9c06b0b4
TJB
9608 break;
9609
9610 case bp_read_watchpoint:
112e8700
SM
9611 if (uiout->is_mi_like_p ())
9612 uiout->field_string
9613 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
348d480f 9614 mention (b);
76f9c9cf 9615 tuple_emitter.emplace (uiout, "value");
112e8700 9616 uiout->text ("\nValue = ");
3a292923 9617 watchpoint_value_print (val.get (), &stb);
112e8700
SM
9618 uiout->field_stream ("value", stb);
9619 uiout->text ("\n");
348d480f 9620 result = PRINT_UNKNOWN;
9c06b0b4
TJB
9621 break;
9622
9623 case bp_access_watchpoint:
348d480f
PA
9624 if (bs->old_val != NULL)
9625 {
112e8700
SM
9626 if (uiout->is_mi_like_p ())
9627 uiout->field_string
9628 ("reason",
348d480f
PA
9629 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
9630 mention (b);
76f9c9cf 9631 tuple_emitter.emplace (uiout, "value");
112e8700 9632 uiout->text ("\nOld value = ");
850645cf 9633 watchpoint_value_print (bs->old_val.get (), &stb);
112e8700
SM
9634 uiout->field_stream ("old", stb);
9635 uiout->text ("\nNew value = ");
348d480f
PA
9636 }
9637 else
9638 {
9639 mention (b);
112e8700
SM
9640 if (uiout->is_mi_like_p ())
9641 uiout->field_string
9642 ("reason",
348d480f 9643 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
76f9c9cf 9644 tuple_emitter.emplace (uiout, "value");
112e8700 9645 uiout->text ("\nValue = ");
348d480f 9646 }
3a292923 9647 watchpoint_value_print (val.get (), &stb);
112e8700
SM
9648 uiout->field_stream ("new", stb);
9649 uiout->text ("\n");
348d480f 9650 result = PRINT_UNKNOWN;
9c06b0b4
TJB
9651 break;
9652 default:
348d480f 9653 result = PRINT_UNKNOWN;
9c06b0b4
TJB
9654 }
9655
348d480f
PA
9656 return result;
9657}
9658
3a292923 9659/* Implement the "print_mention" method for hardware watchpoints. */
348d480f 9660
3a292923 9661void
b713485d 9662watchpoint::print_mention () const
348d480f 9663{
79a45e25 9664 struct ui_out *uiout = current_uiout;
46b9c129 9665 const char *tuple_name;
348d480f 9666
3a292923 9667 switch (type)
348d480f
PA
9668 {
9669 case bp_watchpoint:
112e8700 9670 uiout->text ("Watchpoint ");
46b9c129 9671 tuple_name = "wpt";
348d480f
PA
9672 break;
9673 case bp_hardware_watchpoint:
112e8700 9674 uiout->text ("Hardware watchpoint ");
46b9c129 9675 tuple_name = "wpt";
348d480f
PA
9676 break;
9677 case bp_read_watchpoint:
112e8700 9678 uiout->text ("Hardware read watchpoint ");
46b9c129 9679 tuple_name = "hw-rwpt";
348d480f
PA
9680 break;
9681 case bp_access_watchpoint:
112e8700 9682 uiout->text ("Hardware access (read/write) watchpoint ");
46b9c129 9683 tuple_name = "hw-awpt";
348d480f
PA
9684 break;
9685 default:
9686 internal_error (__FILE__, __LINE__,
9687 _("Invalid hardware watchpoint type."));
9688 }
9689
46b9c129 9690 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
3a292923 9691 uiout->field_signed ("number", number);
112e8700 9692 uiout->text (": ");
3a292923 9693 uiout->field_string ("exp", exp_string.get ());
348d480f
PA
9694}
9695
3a292923 9696/* Implement the "print_recreate" method for watchpoints. */
348d480f 9697
3a292923 9698void
4d1ae558 9699watchpoint::print_recreate (struct ui_file *fp) const
348d480f 9700{
3a292923 9701 switch (type)
348d480f
PA
9702 {
9703 case bp_watchpoint:
9704 case bp_hardware_watchpoint:
6cb06a8c 9705 gdb_printf (fp, "watch");
348d480f
PA
9706 break;
9707 case bp_read_watchpoint:
6cb06a8c 9708 gdb_printf (fp, "rwatch");
348d480f
PA
9709 break;
9710 case bp_access_watchpoint:
6cb06a8c 9711 gdb_printf (fp, "awatch");
348d480f
PA
9712 break;
9713 default:
9714 internal_error (__FILE__, __LINE__,
9715 _("Invalid watchpoint type."));
9716 }
9717
3a292923 9718 gdb_printf (fp, " %s", exp_string.get ());
04d0163c 9719 print_recreate_thread (fp);
348d480f
PA
9720}
9721
3a292923 9722/* Implement the "explains_signal" method for watchpoints. */
427cd150 9723
a6860f3a 9724bool
3a292923 9725watchpoint::explains_signal (enum gdb_signal sig)
427cd150
TT
9726{
9727 /* A software watchpoint cannot cause a signal other than
9728 GDB_SIGNAL_TRAP. */
3a292923 9729 if (type == bp_watchpoint && sig != GDB_SIGNAL_TRAP)
a6860f3a 9730 return false;
427cd150 9731
a6860f3a 9732 return true;
427cd150
TT
9733}
9734
3a292923
TT
9735struct masked_watchpoint : public watchpoint
9736{
73063f51
TT
9737 using watchpoint::watchpoint;
9738
3a292923
TT
9739 int insert_location (struct bp_location *) override;
9740 int remove_location (struct bp_location *,
9741 enum remove_bp_reason reason) override;
9742 int resources_needed (const struct bp_location *) override;
5a61e176 9743 bool works_in_software_mode () const override;
7bd86313 9744 enum print_stop_action print_it (const bpstat *bs) const override;
3a292923 9745 void print_one_detail (struct ui_out *) const override;
b713485d 9746 void print_mention () const override;
4d1ae558 9747 void print_recreate (struct ui_file *fp) const override;
3a292923 9748};
348d480f 9749
3a292923 9750/* Implement the "insert" method for masked hardware watchpoints. */
348d480f 9751
3a292923
TT
9752int
9753masked_watchpoint::insert_location (struct bp_location *bl)
348d480f 9754{
3a292923 9755 return target_insert_mask_watchpoint (bl->address, hw_wp_mask,
348d480f
PA
9756 bl->watchpoint_type);
9757}
9758
3a292923 9759/* Implement the "remove" method for masked hardware watchpoints. */
348d480f 9760
3a292923
TT
9761int
9762masked_watchpoint::remove_location (struct bp_location *bl,
9763 enum remove_bp_reason reason)
348d480f 9764{
3a292923 9765 return target_remove_mask_watchpoint (bl->address, hw_wp_mask,
dda83cd7 9766 bl->watchpoint_type);
348d480f
PA
9767}
9768
3a292923
TT
9769/* Implement the "resources_needed" method for masked hardware
9770 watchpoints. */
348d480f 9771
3a292923
TT
9772int
9773masked_watchpoint::resources_needed (const struct bp_location *bl)
348d480f 9774{
3a292923 9775 return target_masked_watch_num_registers (bl->address, hw_wp_mask);
348d480f
PA
9776}
9777
3a292923
TT
9778/* Implement the "works_in_software_mode" method for masked hardware
9779 watchpoints. */
348d480f 9780
5a61e176 9781bool
3a292923 9782masked_watchpoint::works_in_software_mode () const
348d480f 9783{
5a61e176 9784 return false;
348d480f
PA
9785}
9786
3a292923
TT
9787/* Implement the "print_it" method for masked hardware
9788 watchpoints. */
348d480f 9789
3a292923 9790enum print_stop_action
7bd86313 9791masked_watchpoint::print_it (const bpstat *bs) const
348d480f
PA
9792{
9793 struct breakpoint *b = bs->breakpoint_at;
79a45e25 9794 struct ui_out *uiout = current_uiout;
348d480f
PA
9795
9796 /* Masked watchpoints have only one location. */
9797 gdb_assert (b->loc && b->loc->next == NULL);
9798
f303dbd6
PA
9799 annotate_watchpoint (b->number);
9800 maybe_print_thread_hit_breakpoint (uiout);
9801
348d480f
PA
9802 switch (b->type)
9803 {
9804 case bp_hardware_watchpoint:
112e8700
SM
9805 if (uiout->is_mi_like_p ())
9806 uiout->field_string
9807 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
348d480f
PA
9808 break;
9809
9810 case bp_read_watchpoint:
112e8700
SM
9811 if (uiout->is_mi_like_p ())
9812 uiout->field_string
9813 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
348d480f
PA
9814 break;
9815
9816 case bp_access_watchpoint:
112e8700
SM
9817 if (uiout->is_mi_like_p ())
9818 uiout->field_string
9819 ("reason",
348d480f
PA
9820 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
9821 break;
9822 default:
9823 internal_error (__FILE__, __LINE__,
9824 _("Invalid hardware watchpoint type."));
9825 }
9826
9827 mention (b);
112e8700 9828 uiout->text (_("\n\
9c06b0b4
TJB
9829Check the underlying instruction at PC for the memory\n\
9830address and value which triggered this watchpoint.\n"));
112e8700 9831 uiout->text ("\n");
9c06b0b4
TJB
9832
9833 /* More than one watchpoint may have been triggered. */
9834 return PRINT_UNKNOWN;
9835}
9836
3a292923
TT
9837/* Implement the "print_one_detail" method for masked hardware
9838 watchpoints. */
9c06b0b4 9839
3a292923
TT
9840void
9841masked_watchpoint::print_one_detail (struct ui_out *uiout) const
9c06b0b4
TJB
9842{
9843 /* Masked watchpoints have only one location. */
3a292923 9844 gdb_assert (loc && loc->next == NULL);
9c06b0b4 9845
112e8700 9846 uiout->text ("\tmask ");
3a292923 9847 uiout->field_core_addr ("mask", loc->gdbarch, hw_wp_mask);
112e8700 9848 uiout->text ("\n");
9c06b0b4
TJB
9849}
9850
3a292923
TT
9851/* Implement the "print_mention" method for masked hardware
9852 watchpoints. */
9c06b0b4 9853
3a292923 9854void
b713485d 9855masked_watchpoint::print_mention () const
9c06b0b4 9856{
79a45e25 9857 struct ui_out *uiout = current_uiout;
46b9c129 9858 const char *tuple_name;
9c06b0b4 9859
3a292923 9860 switch (type)
9c06b0b4
TJB
9861 {
9862 case bp_hardware_watchpoint:
112e8700 9863 uiout->text ("Masked hardware watchpoint ");
46b9c129 9864 tuple_name = "wpt";
9c06b0b4
TJB
9865 break;
9866 case bp_read_watchpoint:
112e8700 9867 uiout->text ("Masked hardware read watchpoint ");
46b9c129 9868 tuple_name = "hw-rwpt";
9c06b0b4
TJB
9869 break;
9870 case bp_access_watchpoint:
112e8700 9871 uiout->text ("Masked hardware access (read/write) watchpoint ");
46b9c129 9872 tuple_name = "hw-awpt";
9c06b0b4
TJB
9873 break;
9874 default:
9875 internal_error (__FILE__, __LINE__,
9876 _("Invalid hardware watchpoint type."));
9877 }
9878
46b9c129 9879 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
3a292923 9880 uiout->field_signed ("number", number);
112e8700 9881 uiout->text (": ");
3a292923 9882 uiout->field_string ("exp", exp_string.get ());
9c06b0b4
TJB
9883}
9884
3a292923
TT
9885/* Implement the "print_recreate" method for masked hardware
9886 watchpoints. */
9c06b0b4 9887
3a292923 9888void
4d1ae558 9889masked_watchpoint::print_recreate (struct ui_file *fp) const
9c06b0b4 9890{
3a292923 9891 switch (type)
9c06b0b4
TJB
9892 {
9893 case bp_hardware_watchpoint:
6cb06a8c 9894 gdb_printf (fp, "watch");
9c06b0b4
TJB
9895 break;
9896 case bp_read_watchpoint:
6cb06a8c 9897 gdb_printf (fp, "rwatch");
9c06b0b4
TJB
9898 break;
9899 case bp_access_watchpoint:
6cb06a8c 9900 gdb_printf (fp, "awatch");
9c06b0b4
TJB
9901 break;
9902 default:
9903 internal_error (__FILE__, __LINE__,
9904 _("Invalid hardware watchpoint type."));
9905 }
9906
3a292923
TT
9907 gdb_printf (fp, " %s mask 0x%s", exp_string.get (),
9908 phex (hw_wp_mask, sizeof (CORE_ADDR)));
04d0163c 9909 print_recreate_thread (fp);
9c06b0b4
TJB
9910}
9911
9c06b0b4
TJB
9912/* Tell whether the given watchpoint is a masked hardware watchpoint. */
9913
f2478a7e 9914static bool
9c06b0b4
TJB
9915is_masked_watchpoint (const struct breakpoint *b)
9916{
3a292923 9917 return dynamic_cast<const masked_watchpoint *> (b) != nullptr;
9c06b0b4
TJB
9918}
9919
53a5351d 9920/* accessflag: hw_write: watch write,
dda83cd7 9921 hw_read: watch read,
53a5351d 9922 hw_access: watch access (read or write) */
c906108c 9923static void
bbc13ae3 9924watch_command_1 (const char *arg, int accessflag, int from_tty,
2e362716 9925 bool just_location, bool internal)
c906108c 9926{
c1fc2657 9927 struct breakpoint *scope_breakpoint = NULL;
270140bd 9928 const struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
b926417a 9929 struct value *result;
bb9d5f81 9930 int saved_bitpos = 0, saved_bitsize = 0;
bbc13ae3
KS
9931 const char *exp_start = NULL;
9932 const char *exp_end = NULL;
9933 const char *tok, *end_tok;
9c06b0b4 9934 int toklen = -1;
bbc13ae3
KS
9935 const char *cond_start = NULL;
9936 const char *cond_end = NULL;
c906108c 9937 enum bptype bp_type;
37e4754d 9938 int thread = -1;
9c06b0b4
TJB
9939 /* Flag to indicate whether we are going to use masks for
9940 the hardware watchpoint. */
2e362716 9941 bool use_mask = false;
9c06b0b4 9942 CORE_ADDR mask = 0;
8a18382f 9943 int task = 0;
c906108c 9944
37e4754d
LM
9945 /* Make sure that we actually have parameters to parse. */
9946 if (arg != NULL && arg[0] != '\0')
9947 {
bbc13ae3
KS
9948 const char *value_start;
9949
9950 exp_end = arg + strlen (arg);
37e4754d 9951
9c06b0b4
TJB
9952 /* Look for "parameter value" pairs at the end
9953 of the arguments string. */
bbc13ae3 9954 for (tok = exp_end - 1; tok > arg; tok--)
9c06b0b4
TJB
9955 {
9956 /* Skip whitespace at the end of the argument list. */
9957 while (tok > arg && (*tok == ' ' || *tok == '\t'))
9958 tok--;
9959
9960 /* Find the beginning of the last token.
9961 This is the value of the parameter. */
9962 while (tok > arg && (*tok != ' ' && *tok != '\t'))
9963 tok--;
9964 value_start = tok + 1;
9965
9966 /* Skip whitespace. */
9967 while (tok > arg && (*tok == ' ' || *tok == '\t'))
9968 tok--;
9969
9970 end_tok = tok;
9971
9972 /* Find the beginning of the second to last token.
9973 This is the parameter itself. */
9974 while (tok > arg && (*tok != ' ' && *tok != '\t'))
9975 tok--;
9976 tok++;
9977 toklen = end_tok - tok + 1;
9978
61012eef 9979 if (toklen == 6 && startswith (tok, "thread"))
9c06b0b4 9980 {
5d5658a1 9981 struct thread_info *thr;
9c06b0b4
TJB
9982 /* At this point we've found a "thread" token, which means
9983 the user is trying to set a watchpoint that triggers
9984 only in a specific thread. */
5d5658a1 9985 const char *endp;
37e4754d 9986
9c06b0b4
TJB
9987 if (thread != -1)
9988 error(_("You can specify only one thread."));
37e4754d 9989
9c06b0b4 9990 /* Extract the thread ID from the next token. */
5d5658a1 9991 thr = parse_thread_id (value_start, &endp);
37e4754d 9992
5d5658a1 9993 /* Check if the user provided a valid thread ID. */
9c06b0b4 9994 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
5d5658a1 9995 invalid_thread_id_error (value_start);
9c06b0b4 9996
5d5658a1 9997 thread = thr->global_num;
9c06b0b4 9998 }
8a18382f
TT
9999 else if (toklen == 4 && startswith (tok, "task"))
10000 {
10001 char *tmp;
10002
10003 task = strtol (value_start, &tmp, 0);
10004 if (tmp == value_start)
10005 error (_("Junk after task keyword."));
10006 if (!valid_task_id (task))
10007 error (_("Unknown task %d."), task);
10008 }
61012eef 10009 else if (toklen == 4 && startswith (tok, "mask"))
9c06b0b4
TJB
10010 {
10011 /* We've found a "mask" token, which means the user wants to
10012 create a hardware watchpoint that is going to have the mask
10013 facility. */
10014 struct value *mask_value, *mark;
37e4754d 10015
9c06b0b4
TJB
10016 if (use_mask)
10017 error(_("You can specify only one mask."));
37e4754d 10018
2e362716 10019 use_mask = just_location = true;
37e4754d 10020
9c06b0b4
TJB
10021 mark = value_mark ();
10022 mask_value = parse_to_comma_and_eval (&value_start);
10023 mask = value_as_address (mask_value);
10024 value_free_to_mark (mark);
10025 }
10026 else
10027 /* We didn't recognize what we found. We should stop here. */
10028 break;
37e4754d 10029
9c06b0b4
TJB
10030 /* Truncate the string and get rid of the "parameter value" pair before
10031 the arguments string is parsed by the parse_exp_1 function. */
bbc13ae3 10032 exp_end = tok;
9c06b0b4 10033 }
37e4754d 10034 }
bbc13ae3
KS
10035 else
10036 exp_end = arg;
37e4754d 10037
bbc13ae3
KS
10038 /* Parse the rest of the arguments. From here on out, everything
10039 is in terms of a newly allocated string instead of the original
10040 ARG. */
81b1e71c
TT
10041 std::string expression (arg, exp_end - arg);
10042 exp_start = arg = expression.c_str ();
699bd4cf
TT
10043 innermost_block_tracker tracker;
10044 expression_up exp = parse_exp_1 (&arg, 0, 0, 0, &tracker);
c906108c 10045 exp_end = arg;
fa8a61dc
TT
10046 /* Remove trailing whitespace from the expression before saving it.
10047 This makes the eventual display of the expression string a bit
10048 prettier. */
10049 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
10050 --exp_end;
10051
65d79d4b 10052 /* Checking if the expression is not constant. */
4d01a485 10053 if (watchpoint_exp_is_const (exp.get ()))
65d79d4b
SDJ
10054 {
10055 int len;
10056
10057 len = exp_end - exp_start;
10058 while (len > 0 && isspace (exp_start[len - 1]))
10059 len--;
10060 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
10061 }
10062
699bd4cf 10063 exp_valid_block = tracker.block ();
b926417a 10064 struct value *mark = value_mark ();
850645cf 10065 struct value *val_as_value = nullptr;
1eaebe02
TT
10066 fetch_subexp_value (exp.get (), exp->op.get (), &val_as_value, &result, NULL,
10067 just_location);
06a64a0b 10068
850645cf 10069 if (val_as_value != NULL && just_location)
bb9d5f81 10070 {
850645cf
TT
10071 saved_bitpos = value_bitpos (val_as_value);
10072 saved_bitsize = value_bitsize (val_as_value);
bb9d5f81
PP
10073 }
10074
850645cf 10075 value_ref_ptr val;
06a64a0b
TT
10076 if (just_location)
10077 {
9c06b0b4
TJB
10078 int ret;
10079
06a64a0b 10080 exp_valid_block = NULL;
850645cf 10081 val = release_value (value_addr (result));
06a64a0b 10082 value_free_to_mark (mark);
9c06b0b4
TJB
10083
10084 if (use_mask)
10085 {
850645cf 10086 ret = target_masked_watch_num_registers (value_as_address (val.get ()),
9c06b0b4
TJB
10087 mask);
10088 if (ret == -1)
10089 error (_("This target does not support masked watchpoints."));
10090 else if (ret == -2)
10091 error (_("Invalid mask or memory region."));
10092 }
06a64a0b 10093 }
850645cf
TT
10094 else if (val_as_value != NULL)
10095 val = release_value (val_as_value);
c906108c 10096
f1735a53
TT
10097 tok = skip_spaces (arg);
10098 end_tok = skip_to_space (tok);
c906108c
SS
10099
10100 toklen = end_tok - tok;
10101 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
10102 {
10103 tok = cond_start = end_tok + 1;
699bd4cf
TT
10104 innermost_block_tracker if_tracker;
10105 parse_exp_1 (&tok, 0, 0, 0, &if_tracker);
60e1c644
PA
10106
10107 /* The watchpoint expression may not be local, but the condition
10108 may still be. E.g.: `watch global if local > 0'. */
699bd4cf 10109 cond_exp_valid_block = if_tracker.block ();
60e1c644 10110
c906108c
SS
10111 cond_end = tok;
10112 }
10113 if (*tok)
8a3fe4f8 10114 error (_("Junk at end of command."));
c906108c 10115
441d7c93
PA
10116 frame_info *wp_frame = block_innermost_frame (exp_valid_block);
10117
10118 /* Save this because create_internal_breakpoint below invalidates
10119 'wp_frame'. */
10120 frame_id watchpoint_frame = get_frame_id (wp_frame);
d983da9c
DJ
10121
10122 /* If the expression is "local", then set up a "watchpoint scope"
10123 breakpoint at the point where we've left the scope of the watchpoint
10124 expression. Create the scope breakpoint before the watchpoint, so
10125 that we will encounter it first in bpstat_stop_status. */
441d7c93 10126 if (exp_valid_block != NULL && wp_frame != NULL)
d983da9c 10127 {
441d7c93
PA
10128 frame_id caller_frame_id = frame_unwind_caller_id (wp_frame);
10129
10130 if (frame_id_p (caller_frame_id))
edb3359d 10131 {
441d7c93
PA
10132 gdbarch *caller_arch = frame_unwind_caller_arch (wp_frame);
10133 CORE_ADDR caller_pc = frame_unwind_caller_pc (wp_frame);
10134
24b21115 10135 scope_breakpoint
441d7c93 10136 = create_internal_breakpoint (caller_arch, caller_pc,
9efa3c7f 10137 bp_watchpoint_scope);
d983da9c 10138
441d7c93
PA
10139 /* create_internal_breakpoint could invalidate WP_FRAME. */
10140 wp_frame = NULL;
10141
edb3359d 10142 scope_breakpoint->enable_state = bp_enabled;
d983da9c 10143
edb3359d
DJ
10144 /* Automatically delete the breakpoint when it hits. */
10145 scope_breakpoint->disposition = disp_del;
d983da9c 10146
edb3359d 10147 /* Only break in the proper frame (help with recursion). */
441d7c93 10148 scope_breakpoint->frame_id = caller_frame_id;
d983da9c 10149
edb3359d 10150 /* Set the address at which we will stop. */
441d7c93
PA
10151 scope_breakpoint->loc->gdbarch = caller_arch;
10152 scope_breakpoint->loc->requested_address = caller_pc;
edb3359d 10153 scope_breakpoint->loc->address
a6d9a66e
UW
10154 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
10155 scope_breakpoint->loc->requested_address,
edb3359d
DJ
10156 scope_breakpoint->type);
10157 }
d983da9c
DJ
10158 }
10159
e8369a73
AB
10160 /* Now set up the breakpoint. We create all watchpoints as hardware
10161 watchpoints here even if hardware watchpoints are turned off, a call
10162 to update_watchpoint later in this function will cause the type to
10163 drop back to bp_watchpoint (software watchpoint) if required. */
10164
10165 if (accessflag == hw_read)
10166 bp_type = bp_read_watchpoint;
10167 else if (accessflag == hw_access)
10168 bp_type = bp_access_watchpoint;
10169 else
10170 bp_type = bp_hardware_watchpoint;
3a5c3e22 10171
3a292923 10172 std::unique_ptr<watchpoint> w;
348d480f 10173 if (use_mask)
73063f51 10174 w.reset (new masked_watchpoint (nullptr, bp_type));
348d480f 10175 else
73063f51 10176 w.reset (new watchpoint (nullptr, bp_type));
3a292923 10177
c1fc2657 10178 w->thread = thread;
8a18382f 10179 w->task = task;
c1fc2657
SM
10180 w->disposition = disp_donttouch;
10181 w->pspace = current_program_space;
b22e99fd 10182 w->exp = std::move (exp);
3a5c3e22
PA
10183 w->exp_valid_block = exp_valid_block;
10184 w->cond_exp_valid_block = cond_exp_valid_block;
06a64a0b
TT
10185 if (just_location)
10186 {
850645cf
TT
10187 struct type *t = value_type (val.get ());
10188 CORE_ADDR addr = value_as_address (val.get ());
06a64a0b 10189
43cc5389 10190 w->exp_string_reparse
a4c50be3 10191 = current_language->watch_location_expression (t, addr);
06a64a0b 10192
8579fd13
AB
10193 w->exp_string = xstrprintf ("-location %.*s",
10194 (int) (exp_end - exp_start), exp_start);
06a64a0b
TT
10195 }
10196 else
a4c50be3 10197 w->exp_string.reset (savestring (exp_start, exp_end - exp_start));
9c06b0b4
TJB
10198
10199 if (use_mask)
10200 {
3a5c3e22 10201 w->hw_wp_mask = mask;
9c06b0b4
TJB
10202 }
10203 else
10204 {
3a5c3e22 10205 w->val = val;
bb9d5f81
PP
10206 w->val_bitpos = saved_bitpos;
10207 w->val_bitsize = saved_bitsize;
4c1d86d9 10208 w->val_valid = true;
9c06b0b4 10209 }
77b06cd7 10210
c906108c 10211 if (cond_start)
6f781ee3 10212 w->cond_string.reset (savestring (cond_start, cond_end - cond_start));
c906108c 10213 else
c1fc2657 10214 w->cond_string = 0;
c5aa993b 10215
441d7c93 10216 if (frame_id_p (watchpoint_frame))
f6bc2008 10217 {
441d7c93 10218 w->watchpoint_frame = watchpoint_frame;
3a5c3e22 10219 w->watchpoint_thread = inferior_ptid;
f6bc2008 10220 }
c906108c 10221 else
f6bc2008 10222 {
3a5c3e22
PA
10223 w->watchpoint_frame = null_frame_id;
10224 w->watchpoint_thread = null_ptid;
f6bc2008 10225 }
c906108c 10226
d983da9c 10227 if (scope_breakpoint != NULL)
c906108c 10228 {
d983da9c
DJ
10229 /* The scope breakpoint is related to the watchpoint. We will
10230 need to act on them together. */
c1fc2657 10231 w->related_breakpoint = scope_breakpoint;
b270e6f9 10232 scope_breakpoint->related_breakpoint = w.get ();
c906108c 10233 }
d983da9c 10234
06a64a0b
TT
10235 if (!just_location)
10236 value_free_to_mark (mark);
2d134ed3 10237
b270e6f9
TT
10238 /* Finally update the new watchpoint. This creates the locations
10239 that should be inserted. */
10240 update_watchpoint (w.get (), 1);
a9634178 10241
b270e6f9 10242 install_breakpoint (internal, std::move (w), 1);
c906108c
SS
10243}
10244
e09342b5 10245/* Return count of debug registers needed to watch the given expression.
e09342b5 10246 If the watchpoint cannot be handled in hardware return zero. */
c906108c 10247
c906108c 10248static int
a6535de1 10249can_use_hardware_watchpoint (const std::vector<value_ref_ptr> &vals)
c906108c
SS
10250{
10251 int found_memory_cnt = 0;
10252
10253 /* Did the user specifically forbid us to use hardware watchpoints? */
c5aa993b 10254 if (!can_use_hw_watchpoints)
c906108c 10255 return 0;
c5aa993b 10256
a6535de1
TT
10257 gdb_assert (!vals.empty ());
10258 struct value *head = vals[0].get ();
10259
5c44784c
JM
10260 /* Make sure that the value of the expression depends only upon
10261 memory contents, and values computed from them within GDB. If we
10262 find any register references or function calls, we can't use a
10263 hardware watchpoint.
10264
10265 The idea here is that evaluating an expression generates a series
10266 of values, one holding the value of every subexpression. (The
10267 expression a*b+c has five subexpressions: a, b, a*b, c, and
10268 a*b+c.) GDB's values hold almost enough information to establish
10269 the criteria given above --- they identify memory lvalues,
10270 register lvalues, computed values, etcetera. So we can evaluate
10271 the expression, and then scan the chain of values that leaves
10272 behind to decide whether we can detect any possible change to the
10273 expression's final value using only hardware watchpoints.
10274
10275 However, I don't think that the values returned by inferior
10276 function calls are special in any way. So this function may not
10277 notice that an expression involving an inferior function call
10278 can't be watched with hardware watchpoints. FIXME. */
a6535de1 10279 for (const value_ref_ptr &iter : vals)
c906108c 10280 {
a6535de1
TT
10281 struct value *v = iter.get ();
10282
5c44784c 10283 if (VALUE_LVAL (v) == lval_memory)
c906108c 10284 {
8464be76
DJ
10285 if (v != head && value_lazy (v))
10286 /* A lazy memory lvalue in the chain is one that GDB never
10287 needed to fetch; we either just used its address (e.g.,
10288 `a' in `a.b') or we never needed it at all (e.g., `a'
10289 in `a,b'). This doesn't apply to HEAD; if that is
10290 lazy then it was not readable, but watch it anyway. */
5c44784c 10291 ;
53a5351d 10292 else
5c44784c
JM
10293 {
10294 /* Ahh, memory we actually used! Check if we can cover
dda83cd7 10295 it with hardware watchpoints. */
df407dfe 10296 struct type *vtype = check_typedef (value_type (v));
2e70b7b9
MS
10297
10298 /* We only watch structs and arrays if user asked for it
10299 explicitly, never if they just happen to appear in a
10300 middle of some value chain. */
10301 if (v == head
78134374
SM
10302 || (vtype->code () != TYPE_CODE_STRUCT
10303 && vtype->code () != TYPE_CODE_ARRAY))
2e70b7b9 10304 {
42ae5230 10305 CORE_ADDR vaddr = value_address (v);
e09342b5
TJB
10306 int len;
10307 int num_regs;
10308
a9634178 10309 len = (target_exact_watchpoints
e09342b5
TJB
10310 && is_scalar_type_recursive (vtype))?
10311 1 : TYPE_LENGTH (value_type (v));
2e70b7b9 10312
e09342b5
TJB
10313 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
10314 if (!num_regs)
2e70b7b9
MS
10315 return 0;
10316 else
e09342b5 10317 found_memory_cnt += num_regs;
2e70b7b9 10318 }
5c44784c 10319 }
c5aa993b 10320 }
5086187c
AC
10321 else if (VALUE_LVAL (v) != not_lval
10322 && deprecated_value_modifiable (v) == 0)
38b6c3b3 10323 return 0; /* These are values from the history (e.g., $1). */
5086187c 10324 else if (VALUE_LVAL (v) == lval_register)
38b6c3b3 10325 return 0; /* Cannot watch a register with a HW watchpoint. */
c906108c
SS
10326 }
10327
10328 /* The expression itself looks suitable for using a hardware
10329 watchpoint, but give the target machine a chance to reject it. */
10330 return found_memory_cnt;
10331}
10332
8b93c638 10333void
2e362716 10334watch_command_wrapper (const char *arg, int from_tty, bool internal)
8b93c638 10335{
84f4c1fe 10336 watch_command_1 (arg, hw_write, from_tty, 0, internal);
06a64a0b
TT
10337}
10338
a15a5258
AB
10339/* Options for the watch, awatch, and rwatch commands. */
10340
10341struct watch_options
10342{
10343 /* For -location. */
10344 bool location = false;
10345};
10346
10347/* Definitions of options for the "watch", "awatch", and "rwatch" commands.
10348
10349 Historically GDB always accepted both '-location' and '-l' flags for
10350 these commands (both flags being synonyms). When converting to the
10351 newer option scheme only '-location' is added here. That's fine (for
10352 backward compatibility) as any non-ambiguous prefix of a flag will be
10353 accepted, so '-l', '-loc', are now all accepted.
10354
10355 What this means is that, if in the future, we add any new flag here
10356 that starts with '-l' then this will break backward compatibility, so
10357 please, don't do that! */
10358
10359static const gdb::option::option_def watch_option_defs[] = {
10360 gdb::option::flag_option_def<watch_options> {
10361 "location",
10362 [] (watch_options *opt) { return &opt->location; },
10363 N_("\
10364This evaluates EXPRESSION and watches the memory to which is refers.\n\
10365-l can be used as a short form of -location."),
10366 },
10367};
10368
10369/* Returns the option group used by 'watch', 'awatch', and 'rwatch'
10370 commands. */
10371
10372static gdb::option::option_def_group
10373make_watch_options_def_group (watch_options *opts)
10374{
10375 return {{watch_option_defs}, opts};
10376}
10377
06a64a0b
TT
10378/* A helper function that looks for the "-location" argument and then
10379 calls watch_command_1. */
10380
10381static void
0b39b52e 10382watch_maybe_just_location (const char *arg, int accessflag, int from_tty)
06a64a0b 10383{
a15a5258
AB
10384 watch_options opts;
10385 auto grp = make_watch_options_def_group (&opts);
10386 gdb::option::process_options
10387 (&arg, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND, grp);
10388 if (arg != nullptr && *arg == '\0')
10389 arg = nullptr;
10390
10391 watch_command_1 (arg, accessflag, from_tty, opts.location, false);
10392}
06a64a0b 10393
a15a5258
AB
10394/* Command completion for 'watch', 'awatch', and 'rwatch' commands. */
10395static void
10396watch_command_completer (struct cmd_list_element *ignore,
10397 completion_tracker &tracker,
10398 const char *text, const char * /*word*/)
10399{
10400 const auto group = make_watch_options_def_group (nullptr);
10401 if (gdb::option::complete_options
10402 (tracker, &text, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND, group))
10403 return;
06a64a0b 10404
a15a5258
AB
10405 const char *word = advance_to_expression_complete_word_point (tracker, text);
10406 expression_completer (ignore, tracker, text, word);
8b93c638 10407}
8926118c 10408
c5aa993b 10409static void
0b39b52e 10410watch_command (const char *arg, int from_tty)
c906108c 10411{
06a64a0b 10412 watch_maybe_just_location (arg, hw_write, from_tty);
c906108c
SS
10413}
10414
8b93c638 10415void
2e362716 10416rwatch_command_wrapper (const char *arg, int from_tty, bool internal)
8b93c638 10417{
84f4c1fe 10418 watch_command_1 (arg, hw_read, from_tty, 0, internal);
8b93c638 10419}
8926118c 10420
c5aa993b 10421static void
0b39b52e 10422rwatch_command (const char *arg, int from_tty)
c906108c 10423{
06a64a0b 10424 watch_maybe_just_location (arg, hw_read, from_tty);
c906108c
SS
10425}
10426
8b93c638 10427void
2e362716 10428awatch_command_wrapper (const char *arg, int from_tty, bool internal)
8b93c638 10429{
84f4c1fe 10430 watch_command_1 (arg, hw_access, from_tty, 0, internal);
8b93c638 10431}
8926118c 10432
c5aa993b 10433static void
0b39b52e 10434awatch_command (const char *arg, int from_tty)
c906108c 10435{
06a64a0b 10436 watch_maybe_just_location (arg, hw_access, from_tty);
c906108c 10437}
c906108c 10438\f
c5aa993b 10439
cfc31633
PA
10440/* Data for the FSM that manages the until(location)/advance commands
10441 in infcmd.c. Here because it uses the mechanisms of
10442 breakpoints. */
c906108c 10443
46e3ed7f 10444struct until_break_fsm : public thread_fsm
bfec99b2 10445{
46e3ed7f 10446 /* The thread that was current when the command was executed. */
cfc31633
PA
10447 int thread;
10448
df631783
PA
10449 /* The breakpoint set at the return address in the caller frame,
10450 plus breakpoints at all the destination locations. */
10451 std::vector<breakpoint_up> breakpoints;
cfc31633 10452
46e3ed7f 10453 until_break_fsm (struct interp *cmd_interp, int thread,
df631783 10454 std::vector<breakpoint_up> &&breakpoints)
46e3ed7f
TT
10455 : thread_fsm (cmd_interp),
10456 thread (thread),
df631783 10457 breakpoints (std::move (breakpoints))
46e3ed7f
TT
10458 {
10459 }
cfc31633 10460
46e3ed7f
TT
10461 void clean_up (struct thread_info *thread) override;
10462 bool should_stop (struct thread_info *thread) override;
10463 enum async_reply_reason do_async_reply_reason () override;
cfc31633
PA
10464};
10465
cfc31633
PA
10466/* Implementation of the 'should_stop' FSM method for the
10467 until(location)/advance commands. */
10468
46e3ed7f
TT
10469bool
10470until_break_fsm::should_stop (struct thread_info *tp)
cfc31633 10471{
df631783
PA
10472 for (const breakpoint_up &bp : breakpoints)
10473 if (bpstat_find_breakpoint (tp->control.stop_bpstat,
10474 bp.get ()) != NULL)
10475 {
10476 set_finished ();
10477 break;
10478 }
cfc31633 10479
46e3ed7f 10480 return true;
cfc31633
PA
10481}
10482
10483/* Implementation of the 'clean_up' FSM method for the
10484 until(location)/advance commands. */
10485
46e3ed7f
TT
10486void
10487until_break_fsm::clean_up (struct thread_info *)
43ff13b4 10488{
cfc31633 10489 /* Clean up our temporary breakpoints. */
df631783 10490 breakpoints.clear ();
46e3ed7f 10491 delete_longjmp_breakpoint (thread);
cfc31633
PA
10492}
10493
10494/* Implementation of the 'async_reply_reason' FSM method for the
10495 until(location)/advance commands. */
10496
46e3ed7f
TT
10497enum async_reply_reason
10498until_break_fsm::do_async_reply_reason ()
cfc31633
PA
10499{
10500 return EXEC_ASYNC_LOCATION_REACHED;
43ff13b4
JM
10501}
10502
c906108c 10503void
f2fc3015 10504until_break_command (const char *arg, int from_tty, int anywhere)
c906108c 10505{
8556afb4
PA
10506 struct frame_info *frame;
10507 struct gdbarch *frame_gdbarch;
10508 struct frame_id stack_frame_id;
10509 struct frame_id caller_frame_id;
186c406b
TT
10510 int thread;
10511 struct thread_info *tp;
c906108c 10512
70509625 10513 clear_proceed_status (0);
c906108c
SS
10514
10515 /* Set a breakpoint where the user wants it and at return from
4a64f543 10516 this function. */
c5aa993b 10517
ffc2605c 10518 event_location_up location = string_to_event_location (&arg, current_language);
f00aae0f 10519
6c5b2ebe
PA
10520 std::vector<symtab_and_line> sals
10521 = (last_displayed_sal_is_valid ()
10522 ? decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
10523 get_last_displayed_symtab (),
10524 get_last_displayed_line ())
10525 : decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE,
cafb3438 10526 NULL, NULL, 0));
c5aa993b 10527
df631783 10528 if (sals.empty ())
8a3fe4f8 10529 error (_("Couldn't get information on specified line."));
c5aa993b 10530
c906108c 10531 if (*arg)
8a3fe4f8 10532 error (_("Junk at end of arguments."));
c5aa993b 10533
186c406b 10534 tp = inferior_thread ();
5d5658a1 10535 thread = tp->global_num;
186c406b 10536
8556afb4
PA
10537 /* Note linespec handling above invalidates the frame chain.
10538 Installing a breakpoint also invalidates the frame chain (as it
10539 may need to switch threads), so do any frame handling before
10540 that. */
10541
10542 frame = get_selected_frame (NULL);
10543 frame_gdbarch = get_frame_arch (frame);
10544 stack_frame_id = get_stack_frame_id (frame);
10545 caller_frame_id = frame_unwind_caller_id (frame);
883bc8d1 10546
ae66c1fc
EZ
10547 /* Keep within the current frame, or in frames called by the current
10548 one. */
edb3359d 10549
df631783 10550 std::vector<breakpoint_up> breakpoints;
5419bdae
TT
10551
10552 gdb::optional<delete_longjmp_breakpoint_cleanup> lj_deleter;
10553
883bc8d1 10554 if (frame_id_p (caller_frame_id))
c906108c 10555 {
883bc8d1 10556 struct symtab_and_line sal2;
cfc31633 10557 struct gdbarch *caller_gdbarch;
883bc8d1
PA
10558
10559 sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
10560 sal2.pc = frame_unwind_caller_pc (frame);
cfc31633 10561 caller_gdbarch = frame_unwind_caller_arch (frame);
df631783
PA
10562
10563 breakpoint_up caller_breakpoint
10564 = set_momentary_breakpoint (caller_gdbarch, sal2,
10565 caller_frame_id, bp_until);
10566 breakpoints.emplace_back (std::move (caller_breakpoint));
186c406b 10567
883bc8d1 10568 set_longjmp_breakpoint (tp, caller_frame_id);
5419bdae 10569 lj_deleter.emplace (thread);
c906108c 10570 }
c5aa993b 10571
c70a6932
JK
10572 /* set_momentary_breakpoint could invalidate FRAME. */
10573 frame = NULL;
10574
df631783
PA
10575 /* If the user told us to continue until a specified location, we
10576 don't specify a frame at which we need to stop. Otherwise,
10577 specify the selected frame, because we want to stop only at the
10578 very same frame. */
10579 frame_id stop_frame_id = anywhere ? null_frame_id : stack_frame_id;
10580
10581 for (symtab_and_line &sal : sals)
10582 {
10583 resolve_sal_pc (&sal);
10584
10585 breakpoint_up location_breakpoint
10586 = set_momentary_breakpoint (frame_gdbarch, sal,
10587 stop_frame_id, bp_until);
10588 breakpoints.emplace_back (std::move (location_breakpoint));
10589 }
883bc8d1 10590
573269a8
LS
10591 tp->set_thread_fsm
10592 (std::unique_ptr<thread_fsm>
10593 (new until_break_fsm (command_interp (), tp->global_num,
10594 std::move (breakpoints))));
f107f563 10595
5419bdae
TT
10596 if (lj_deleter)
10597 lj_deleter->release ();
f107f563 10598
cfc31633 10599 proceed (-1, GDB_SIGNAL_DEFAULT);
c906108c 10600}
ae66c1fc 10601
9ac4176b 10602void
28010a5d
PA
10603init_ada_exception_breakpoint (struct breakpoint *b,
10604 struct gdbarch *gdbarch,
10605 struct symtab_and_line sal,
f2fc3015 10606 const char *addr_string,
28010a5d 10607 int tempflag,
349774ef 10608 int enabled,
28010a5d 10609 int from_tty)
f7f9143b 10610{
f7f9143b
JB
10611 if (from_tty)
10612 {
5af949e3
UW
10613 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
10614 if (!loc_gdbarch)
10615 loc_gdbarch = gdbarch;
10616
6c95b8df
PA
10617 describe_other_breakpoints (loc_gdbarch,
10618 sal.pspace, sal.pc, sal.section, -1);
f7f9143b 10619 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
dda83cd7
SM
10620 version for exception catchpoints, because two catchpoints
10621 used for different exception names will use the same address.
10622 In this case, a "breakpoint ... also set at..." warning is
10623 unproductive. Besides, the warning phrasing is also a bit
10624 inappropriate, we should use the word catchpoint, and tell
10625 the user what type of catchpoint it is. The above is good
10626 enough for now, though. */
f7f9143b
JB
10627 }
10628
73063f51 10629 init_raw_breakpoint (b, sal, bp_catchpoint);
f7f9143b 10630
349774ef 10631 b->enable_state = enabled ? bp_enabled : bp_disabled;
f7f9143b 10632 b->disposition = tempflag ? disp_del : disp_donttouch;
d28cd78a
TT
10633 b->location = string_to_event_location (&addr_string,
10634 language_def (language_ada));
f7f9143b 10635 b->language = language_ada;
f7f9143b
JB
10636}
10637
c906108c
SS
10638\f
10639
81b1e71c 10640/* Compare two breakpoints and return a strcmp-like result. */
8a2c437b
TT
10641
10642static int
81b1e71c 10643compare_breakpoints (const breakpoint *a, const breakpoint *b)
8a2c437b 10644{
81b1e71c
TT
10645 uintptr_t ua = (uintptr_t) a;
10646 uintptr_t ub = (uintptr_t) b;
8a2c437b 10647
81b1e71c 10648 if (a->number < b->number)
8a2c437b 10649 return -1;
81b1e71c 10650 else if (a->number > b->number)
8a2c437b
TT
10651 return 1;
10652
10653 /* Now sort by address, in case we see, e..g, two breakpoints with
10654 the number 0. */
10655 if (ua < ub)
10656 return -1;
94b0e70d 10657 return ua > ub ? 1 : 0;
8a2c437b
TT
10658}
10659
80f8a6eb 10660/* Delete breakpoints by address or line. */
c906108c
SS
10661
10662static void
0b39b52e 10663clear_command (const char *arg, int from_tty)
c906108c 10664{
c906108c 10665 int default_match;
c906108c 10666
6c5b2ebe
PA
10667 std::vector<symtab_and_line> decoded_sals;
10668 symtab_and_line last_sal;
10669 gdb::array_view<symtab_and_line> sals;
c906108c
SS
10670 if (arg)
10671 {
6c5b2ebe
PA
10672 decoded_sals
10673 = decode_line_with_current_source (arg,
10674 (DECODE_LINE_FUNFIRSTLINE
10675 | DECODE_LINE_LIST_MODE));
c906108c 10676 default_match = 0;
6c5b2ebe 10677 sals = decoded_sals;
c906108c
SS
10678 }
10679 else
10680 {
1bfeeb0f
JL
10681 /* Set sal's line, symtab, pc, and pspace to the values
10682 corresponding to the last call to print_frame_info. If the
10683 codepoint is not valid, this will set all the fields to 0. */
51abb421 10684 last_sal = get_last_displayed_sal ();
6c5b2ebe 10685 if (last_sal.symtab == 0)
8a3fe4f8 10686 error (_("No source file specified."));
c906108c 10687
c906108c 10688 default_match = 1;
6c5b2ebe 10689 sals = last_sal;
c906108c
SS
10690 }
10691
4a64f543
MS
10692 /* We don't call resolve_sal_pc here. That's not as bad as it
10693 seems, because all existing breakpoints typically have both
10694 file/line and pc set. So, if clear is given file/line, we can
10695 match this to existing breakpoint without obtaining pc at all.
ed0616c6
VP
10696
10697 We only support clearing given the address explicitly
10698 present in breakpoint table. Say, we've set breakpoint
4a64f543 10699 at file:line. There were several PC values for that file:line,
ed0616c6 10700 due to optimization, all in one block.
4a64f543
MS
10701
10702 We've picked one PC value. If "clear" is issued with another
ed0616c6
VP
10703 PC corresponding to the same file:line, the breakpoint won't
10704 be cleared. We probably can still clear the breakpoint, but
10705 since the other PC value is never presented to user, user
10706 can only find it by guessing, and it does not seem important
10707 to support that. */
10708
4a64f543
MS
10709 /* For each line spec given, delete bps which correspond to it. Do
10710 it in two passes, solely to preserve the current behavior that
10711 from_tty is forced true if we delete more than one
10712 breakpoint. */
c906108c 10713
81b1e71c 10714 std::vector<struct breakpoint *> found;
6c5b2ebe 10715 for (const auto &sal : sals)
c906108c 10716 {
05cba821
JK
10717 const char *sal_fullname;
10718
c906108c 10719 /* If exact pc given, clear bpts at that pc.
dda83cd7
SM
10720 If line given (pc == 0), clear all bpts on specified line.
10721 If defaulting, clear all bpts on default line
10722 or at default pc.
c5aa993b 10723
dda83cd7 10724 defaulting sal.pc != 0 tests to do
c5aa993b 10725
dda83cd7
SM
10726 0 1 pc
10727 1 1 pc _and_ line
10728 0 0 line
10729 1 0 <can't happen> */
c906108c 10730
05cba821
JK
10731 sal_fullname = (sal.symtab == NULL
10732 ? NULL : symtab_to_fullname (sal.symtab));
c906108c 10733
4a64f543 10734 /* Find all matching breakpoints and add them to 'found'. */
43892fdf 10735 for (breakpoint *b : all_breakpoints ())
c5aa993b 10736 {
0d381245 10737 int match = 0;
4a64f543 10738 /* Are we going to delete b? */
a5c69b1e
EL
10739 if (b->type != bp_none && !is_watchpoint (b)
10740 && user_breakpoint_p (b))
0d381245 10741 {
40cb8ca5 10742 for (bp_location *loc : b->locations ())
0d381245 10743 {
f8eba3c6
TT
10744 /* If the user specified file:line, don't allow a PC
10745 match. This matches historical gdb behavior. */
10746 int pc_match = (!sal.explicit_line
10747 && sal.pc
10748 && (loc->pspace == sal.pspace)
10749 && (loc->address == sal.pc)
10750 && (!section_is_overlay (loc->section)
10751 || loc->section == sal.section));
4aac40c8
TT
10752 int line_match = 0;
10753
10754 if ((default_match || sal.explicit_line)
2f202fde 10755 && loc->symtab != NULL
05cba821 10756 && sal_fullname != NULL
4aac40c8 10757 && sal.pspace == loc->pspace
05cba821
JK
10758 && loc->line_number == sal.line
10759 && filename_cmp (symtab_to_fullname (loc->symtab),
10760 sal_fullname) == 0)
10761 line_match = 1;
4aac40c8 10762
0d381245
VP
10763 if (pc_match || line_match)
10764 {
10765 match = 1;
10766 break;
10767 }
10768 }
10769 }
10770
10771 if (match)
81b1e71c 10772 found.push_back (b);
c906108c 10773 }
80f8a6eb 10774 }
8a2c437b 10775
80f8a6eb 10776 /* Now go thru the 'found' chain and delete them. */
81b1e71c 10777 if (found.empty ())
80f8a6eb
MS
10778 {
10779 if (arg)
8a3fe4f8 10780 error (_("No breakpoint at %s."), arg);
80f8a6eb 10781 else
8a3fe4f8 10782 error (_("No breakpoint at this line."));
80f8a6eb 10783 }
c906108c 10784
8a2c437b 10785 /* Remove duplicates from the vec. */
81b1e71c 10786 std::sort (found.begin (), found.end (),
b926417a 10787 [] (const breakpoint *bp_a, const breakpoint *bp_b)
81b1e71c 10788 {
b926417a 10789 return compare_breakpoints (bp_a, bp_b) < 0;
81b1e71c
TT
10790 });
10791 found.erase (std::unique (found.begin (), found.end (),
b926417a 10792 [] (const breakpoint *bp_a, const breakpoint *bp_b)
81b1e71c 10793 {
b926417a 10794 return compare_breakpoints (bp_a, bp_b) == 0;
81b1e71c
TT
10795 }),
10796 found.end ());
8a2c437b 10797
81b1e71c 10798 if (found.size () > 1)
4a64f543 10799 from_tty = 1; /* Always report if deleted more than one. */
80f8a6eb 10800 if (from_tty)
a3f17187 10801 {
81b1e71c 10802 if (found.size () == 1)
6cb06a8c 10803 gdb_printf (_("Deleted breakpoint "));
a3f17187 10804 else
6cb06a8c 10805 gdb_printf (_("Deleted breakpoints "));
a3f17187 10806 }
d6e956e5 10807
81b1e71c 10808 for (breakpoint *iter : found)
80f8a6eb 10809 {
c5aa993b 10810 if (from_tty)
6cb06a8c 10811 gdb_printf ("%d ", iter->number);
81b1e71c 10812 delete_breakpoint (iter);
c906108c 10813 }
80f8a6eb 10814 if (from_tty)
a11ac3b3 10815 gdb_putc ('\n');
c906108c
SS
10816}
10817\f
10818/* Delete breakpoint in BS if they are `delete' breakpoints and
10819 all breakpoints that are marked for deletion, whether hit or not.
10820 This is called after any breakpoint is hit, or after errors. */
10821
10822void
313f3b21 10823breakpoint_auto_delete (bpstat *bs)
c906108c 10824{
c906108c 10825 for (; bs; bs = bs->next)
f431efe5
PA
10826 if (bs->breakpoint_at
10827 && bs->breakpoint_at->disposition == disp_del
c906108c 10828 && bs->stop)
f431efe5 10829 delete_breakpoint (bs->breakpoint_at);
c906108c 10830
1428b37a 10831 for (breakpoint *b : all_breakpoints_safe ())
b5de0fa7 10832 if (b->disposition == disp_del_at_next_stop)
c5aa993b 10833 delete_breakpoint (b);
c906108c
SS
10834}
10835
4a64f543 10836/* A comparison function for bp_location AP and BP being interfaced to
39ef2f62 10837 std::sort. Sort elements primarily by their ADDRESS (no matter what
cb1e4e32
PA
10838 bl_address_is_meaningful says), secondarily by ordering first
10839 permanent elements and terciarily just ensuring the array is sorted
39ef2f62 10840 stable way despite std::sort being an unstable algorithm. */
876fa593
JK
10841
10842static int
39ef2f62 10843bp_location_is_less_than (const bp_location *a, const bp_location *b)
876fa593 10844{
876fa593 10845 if (a->address != b->address)
39ef2f62 10846 return a->address < b->address;
876fa593 10847
dea2aa5f
LM
10848 /* Sort locations at the same address by their pspace number, keeping
10849 locations of the same inferior (in a multi-inferior environment)
10850 grouped. */
10851
10852 if (a->pspace->num != b->pspace->num)
39ef2f62 10853 return a->pspace->num < b->pspace->num;
dea2aa5f 10854
876fa593 10855 /* Sort permanent breakpoints first. */
1a853c52 10856 if (a->permanent != b->permanent)
39ef2f62 10857 return a->permanent > b->permanent;
876fa593 10858
7f32a4d5
PA
10859 /* Sort by type in order to make duplicate determination easier.
10860 See update_global_location_list. This is kept in sync with
10861 breakpoint_locations_match. */
10862 if (a->loc_type < b->loc_type)
10863 return true;
10864
10865 /* Likewise, for range-breakpoints, sort by length. */
10866 if (a->loc_type == bp_loc_hardware_breakpoint
10867 && b->loc_type == bp_loc_hardware_breakpoint
10868 && a->length < b->length)
10869 return true;
10870
c56a97f9
JK
10871 /* Make the internal GDB representation stable across GDB runs
10872 where A and B memory inside GDB can differ. Breakpoint locations of
10873 the same type at the same address can be sorted in arbitrary order. */
876fa593
JK
10874
10875 if (a->owner->number != b->owner->number)
39ef2f62 10876 return a->owner->number < b->owner->number;
876fa593 10877
39ef2f62 10878 return a < b;
876fa593
JK
10879}
10880
f5336ca5
PA
10881/* Set bp_locations_placed_address_before_address_max and
10882 bp_locations_shadow_len_after_address_max according to the current
10883 content of the bp_locations array. */
f7545552
TT
10884
10885static void
f5336ca5 10886bp_locations_target_extensions_update (void)
f7545552 10887{
f5336ca5
PA
10888 bp_locations_placed_address_before_address_max = 0;
10889 bp_locations_shadow_len_after_address_max = 0;
876fa593 10890
48d7020b 10891 for (bp_location *bl : all_bp_locations ())
876fa593
JK
10892 {
10893 CORE_ADDR start, end, addr;
10894
10895 if (!bp_location_has_shadow (bl))
10896 continue;
10897
10898 start = bl->target_info.placed_address;
10899 end = start + bl->target_info.shadow_len;
10900
10901 gdb_assert (bl->address >= start);
10902 addr = bl->address - start;
f5336ca5
PA
10903 if (addr > bp_locations_placed_address_before_address_max)
10904 bp_locations_placed_address_before_address_max = addr;
876fa593
JK
10905
10906 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
10907
10908 gdb_assert (bl->address < end);
10909 addr = end - bl->address;
f5336ca5
PA
10910 if (addr > bp_locations_shadow_len_after_address_max)
10911 bp_locations_shadow_len_after_address_max = addr;
876fa593 10912 }
f7545552
TT
10913}
10914
1e4d1764
YQ
10915/* Download tracepoint locations if they haven't been. */
10916
10917static void
10918download_tracepoint_locations (void)
10919{
dd2e65cc 10920 enum tribool can_download_tracepoint = TRIBOOL_UNKNOWN;
1e4d1764 10921
5ed8105e 10922 scoped_restore_current_pspace_and_thread restore_pspace_thread;
1e4d1764 10923
f6d17b2b 10924 for (breakpoint *b : all_tracepoints ())
1e4d1764
YQ
10925 {
10926 struct tracepoint *t;
f2a8bc8a 10927 int bp_location_downloaded = 0;
1e4d1764 10928
7ed2c994 10929 if ((b->type == bp_fast_tracepoint
1e4d1764
YQ
10930 ? !may_insert_fast_tracepoints
10931 : !may_insert_tracepoints))
10932 continue;
10933
dd2e65cc
YQ
10934 if (can_download_tracepoint == TRIBOOL_UNKNOWN)
10935 {
10936 if (target_can_download_tracepoint ())
10937 can_download_tracepoint = TRIBOOL_TRUE;
10938 else
10939 can_download_tracepoint = TRIBOOL_FALSE;
10940 }
10941
10942 if (can_download_tracepoint == TRIBOOL_FALSE)
10943 break;
10944
40cb8ca5 10945 for (bp_location *bl : b->locations ())
7ed2c994
YQ
10946 {
10947 /* In tracepoint, locations are _never_ duplicated, so
10948 should_be_inserted is equivalent to
10949 unduplicated_should_be_inserted. */
10950 if (!should_be_inserted (bl) || bl->inserted)
10951 continue;
1e4d1764 10952
7ed2c994 10953 switch_to_program_space_and_thread (bl->pspace);
1e4d1764 10954
7ed2c994 10955 target_download_tracepoint (bl);
1e4d1764 10956
7ed2c994 10957 bl->inserted = 1;
f2a8bc8a 10958 bp_location_downloaded = 1;
7ed2c994
YQ
10959 }
10960 t = (struct tracepoint *) b;
10961 t->number_on_target = b->number;
f2a8bc8a 10962 if (bp_location_downloaded)
76727919 10963 gdb::observers::breakpoint_modified.notify (b);
1e4d1764 10964 }
1e4d1764
YQ
10965}
10966
934709f0
PW
10967/* Swap the insertion/duplication state between two locations. */
10968
10969static void
10970swap_insertion (struct bp_location *left, struct bp_location *right)
10971{
10972 const int left_inserted = left->inserted;
10973 const int left_duplicate = left->duplicate;
b775012e 10974 const int left_needs_update = left->needs_update;
934709f0
PW
10975 const struct bp_target_info left_target_info = left->target_info;
10976
1e4d1764
YQ
10977 /* Locations of tracepoints can never be duplicated. */
10978 if (is_tracepoint (left->owner))
10979 gdb_assert (!left->duplicate);
10980 if (is_tracepoint (right->owner))
10981 gdb_assert (!right->duplicate);
10982
934709f0
PW
10983 left->inserted = right->inserted;
10984 left->duplicate = right->duplicate;
b775012e 10985 left->needs_update = right->needs_update;
934709f0
PW
10986 left->target_info = right->target_info;
10987 right->inserted = left_inserted;
10988 right->duplicate = left_duplicate;
b775012e 10989 right->needs_update = left_needs_update;
934709f0
PW
10990 right->target_info = left_target_info;
10991}
10992
b775012e
LM
10993/* Force the re-insertion of the locations at ADDRESS. This is called
10994 once a new/deleted/modified duplicate location is found and we are evaluating
10995 conditions on the target's side. Such conditions need to be updated on
10996 the target. */
10997
10998static void
10999force_breakpoint_reinsertion (struct bp_location *bl)
11000{
b775012e
LM
11001 CORE_ADDR address = 0;
11002 int pspace_num;
11003
11004 address = bl->address;
11005 pspace_num = bl->pspace->num;
11006
11007 /* This is only meaningful if the target is
11008 evaluating conditions and if the user has
11009 opted for condition evaluation on the target's
11010 side. */
11011 if (gdb_evaluates_breakpoint_condition_p ()
11012 || !target_supports_evaluation_of_breakpoint_conditions ())
11013 return;
11014
11015 /* Flag all breakpoint locations with this address and
11016 the same program space as the location
11017 as "its condition has changed". We need to
11018 update the conditions on the target's side. */
e0d9a270 11019 for (bp_location *loc : all_bp_locations_at_addr (address))
b775012e 11020 {
b775012e
LM
11021 if (!is_breakpoint (loc->owner)
11022 || pspace_num != loc->pspace->num)
11023 continue;
11024
11025 /* Flag the location appropriately. We use a different state to
11026 let everyone know that we already updated the set of locations
11027 with addr bl->address and program space bl->pspace. This is so
11028 we don't have to keep calling these functions just to mark locations
11029 that have already been marked. */
11030 loc->condition_changed = condition_updated;
11031
11032 /* Free the agent expression bytecode as well. We will compute
11033 it later on. */
833177a4 11034 loc->cond_bytecode.reset ();
b775012e
LM
11035 }
11036}
7f32a4d5 11037
44702360
PA
11038/* Called whether new breakpoints are created, or existing breakpoints
11039 deleted, to update the global location list and recompute which
11040 locations are duplicate of which.
b775012e 11041
04086b45
PA
11042 The INSERT_MODE flag determines whether locations may not, may, or
11043 shall be inserted now. See 'enum ugll_insert_mode' for more
11044 info. */
b60e7edf 11045
0d381245 11046static void
44702360 11047update_global_location_list (enum ugll_insert_mode insert_mode)
0d381245 11048{
b775012e
LM
11049 /* Last breakpoint location address that was marked for update. */
11050 CORE_ADDR last_addr = 0;
11051 /* Last breakpoint location program space that was marked for update. */
11052 int last_pspace_num = -1;
f7545552 11053
2d134ed3
PA
11054 /* Used in the duplicates detection below. When iterating over all
11055 bp_locations, points to the first bp_location of a given address.
11056 Breakpoints and watchpoints of different types are never
11057 duplicates of each other. Keep one pointer for each type of
11058 breakpoint/watchpoint, so we only need to loop over all locations
11059 once. */
11060 struct bp_location *bp_loc_first; /* breakpoint */
11061 struct bp_location *wp_loc_first; /* hardware watchpoint */
11062 struct bp_location *awp_loc_first; /* access watchpoint */
11063 struct bp_location *rwp_loc_first; /* read watchpoint */
876fa593 11064
f5336ca5
PA
11065 /* Saved former bp_locations array which we compare against the newly
11066 built bp_locations from the current state of ALL_BREAKPOINTS. */
5d51cd5d
SM
11067 std::vector<bp_location *> old_locations = std::move (bp_locations);
11068 bp_locations.clear ();
876fa593 11069
43892fdf 11070 for (breakpoint *b : all_breakpoints ())
40cb8ca5 11071 for (bp_location *loc : b->locations ())
5d51cd5d 11072 bp_locations.push_back (loc);
7f32a4d5
PA
11073
11074 /* See if we need to "upgrade" a software breakpoint to a hardware
11075 breakpoint. Do this before deciding whether locations are
11076 duplicates. Also do this before sorting because sorting order
11077 depends on location type. */
5d51cd5d
SM
11078 for (bp_location *loc : bp_locations)
11079 if (!loc->inserted && should_be_inserted (loc))
7f32a4d5 11080 handle_automatic_hardware_breakpoints (loc);
7f32a4d5 11081
5d51cd5d 11082 std::sort (bp_locations.begin (), bp_locations.end (),
39ef2f62 11083 bp_location_is_less_than);
876fa593 11084
f5336ca5 11085 bp_locations_target_extensions_update ();
74960c60 11086
4a64f543
MS
11087 /* Identify bp_location instances that are no longer present in the
11088 new list, and therefore should be freed. Note that it's not
11089 necessary that those locations should be removed from inferior --
11090 if there's another location at the same address (previously
11091 marked as duplicate), we don't need to remove/insert the
11092 location.
876fa593 11093
4a64f543
MS
11094 LOCP is kept in sync with OLD_LOCP, each pointing to the current
11095 and former bp_location array state respectively. */
876fa593 11096
5d51cd5d
SM
11097 size_t loc_i = 0;
11098 for (bp_location *old_loc : old_locations)
74960c60 11099 {
e5dd4106 11100 /* Tells if 'old_loc' is found among the new locations. If
4a64f543 11101 not, we have to free it. */
c7d46a38 11102 int found_object = 0;
20874c92
VP
11103 /* Tells if the location should remain inserted in the target. */
11104 int keep_in_target = 0;
11105 int removed = 0;
876fa593 11106
4a64f543
MS
11107 /* Skip LOCP entries which will definitely never be needed.
11108 Stop either at or being the one matching OLD_LOC. */
5d51cd5d
SM
11109 while (loc_i < bp_locations.size ()
11110 && bp_locations[loc_i]->address < old_loc->address)
11111 loc_i++;
c7d46a38 11112
5d51cd5d
SM
11113 for (size_t loc2_i = loc_i;
11114 (loc2_i < bp_locations.size ()
11115 && bp_locations[loc2_i]->address == old_loc->address);
11116 loc2_i++)
c7d46a38 11117 {
b775012e
LM
11118 /* Check if this is a new/duplicated location or a duplicated
11119 location that had its condition modified. If so, we want to send
11120 its condition to the target if evaluation of conditions is taking
11121 place there. */
5d51cd5d 11122 if (bp_locations[loc2_i]->condition_changed == condition_modified
b775012e
LM
11123 && (last_addr != old_loc->address
11124 || last_pspace_num != old_loc->pspace->num))
c7d46a38 11125 {
5d51cd5d 11126 force_breakpoint_reinsertion (bp_locations[loc2_i]);
b775012e 11127 last_pspace_num = old_loc->pspace->num;
c7d46a38 11128 }
b775012e 11129
5d51cd5d 11130 if (bp_locations[loc2_i] == old_loc)
b775012e 11131 found_object = 1;
c7d46a38 11132 }
74960c60 11133
b775012e
LM
11134 /* We have already handled this address, update it so that we don't
11135 have to go through updates again. */
11136 last_addr = old_loc->address;
11137
11138 /* Target-side condition evaluation: Handle deleted locations. */
11139 if (!found_object)
11140 force_breakpoint_reinsertion (old_loc);
11141
4a64f543
MS
11142 /* If this location is no longer present, and inserted, look if
11143 there's maybe a new location at the same address. If so,
11144 mark that one inserted, and don't remove this one. This is
11145 needed so that we don't have a time window where a breakpoint
11146 at certain location is not inserted. */
74960c60 11147
876fa593 11148 if (old_loc->inserted)
0d381245 11149 {
4a64f543
MS
11150 /* If the location is inserted now, we might have to remove
11151 it. */
74960c60 11152
876fa593 11153 if (found_object && should_be_inserted (old_loc))
74960c60 11154 {
4a64f543
MS
11155 /* The location is still present in the location list,
11156 and still should be inserted. Don't do anything. */
20874c92 11157 keep_in_target = 1;
74960c60
VP
11158 }
11159 else
11160 {
b775012e
LM
11161 /* This location still exists, but it won't be kept in the
11162 target since it may have been disabled. We proceed to
11163 remove its target-side condition. */
11164
4a64f543
MS
11165 /* The location is either no longer present, or got
11166 disabled. See if there's another location at the
11167 same address, in which case we don't need to remove
11168 this one from the target. */
876fa593 11169
2bdf28a0 11170 /* OLD_LOC comes from existing struct breakpoint. */
cb1e4e32 11171 if (bl_address_is_meaningful (old_loc))
876fa593 11172 {
5d51cd5d
SM
11173 for (size_t loc2_i = loc_i;
11174 (loc2_i < bp_locations.size ()
11175 && bp_locations[loc2_i]->address == old_loc->address);
11176 loc2_i++)
876fa593 11177 {
5d51cd5d 11178 bp_location *loc2 = bp_locations[loc2_i];
876fa593 11179
7f32a4d5
PA
11180 if (loc2 == old_loc)
11181 continue;
11182
2d134ed3 11183 if (breakpoint_locations_match (loc2, old_loc))
c7d46a38 11184 {
85d721b8
PA
11185 /* Read watchpoint locations are switched to
11186 access watchpoints, if the former are not
11187 supported, but the latter are. */
11188 if (is_hardware_watchpoint (old_loc->owner))
11189 {
11190 gdb_assert (is_hardware_watchpoint (loc2->owner));
11191 loc2->watchpoint_type = old_loc->watchpoint_type;
11192 }
11193
934709f0
PW
11194 /* loc2 is a duplicated location. We need to check
11195 if it should be inserted in case it will be
11196 unduplicated. */
7f32a4d5 11197 if (unduplicated_should_be_inserted (loc2))
c7d46a38 11198 {
934709f0 11199 swap_insertion (old_loc, loc2);
c7d46a38
PA
11200 keep_in_target = 1;
11201 break;
11202 }
876fa593
JK
11203 }
11204 }
11205 }
74960c60
VP
11206 }
11207
20874c92
VP
11208 if (!keep_in_target)
11209 {
834c0d03 11210 if (remove_breakpoint (old_loc))
20874c92 11211 {
4a64f543
MS
11212 /* This is just about all we can do. We could keep
11213 this location on the global list, and try to
11214 remove it next time, but there's no particular
11215 reason why we will succeed next time.
20874c92 11216
4a64f543
MS
11217 Note that at this point, old_loc->owner is still
11218 valid, as delete_breakpoint frees the breakpoint
11219 only after calling us. */
6cb06a8c
TT
11220 gdb_printf (_("warning: Error removing "
11221 "breakpoint %d\n"),
11222 old_loc->owner->number);
20874c92
VP
11223 }
11224 removed = 1;
11225 }
0d381245 11226 }
74960c60
VP
11227
11228 if (!found_object)
1c5cfe86 11229 {
fbea99ea 11230 if (removed && target_is_non_stop_p ()
1cf4d951 11231 && need_moribund_for_location_type (old_loc))
20874c92 11232 {
db82e815
PA
11233 /* This location was removed from the target. In
11234 non-stop mode, a race condition is possible where
11235 we've removed a breakpoint, but stop events for that
11236 breakpoint are already queued and will arrive later.
11237 We apply an heuristic to be able to distinguish such
11238 SIGTRAPs from other random SIGTRAPs: we keep this
11239 breakpoint location for a bit, and will retire it
11240 after we see some number of events. The theory here
11241 is that reporting of events should, "on the average",
11242 be fair, so after a while we'll see events from all
11243 threads that have anything of interest, and no longer
11244 need to keep this breakpoint location around. We
11245 don't hold locations forever so to reduce chances of
11246 mistaking a non-breakpoint SIGTRAP for a breakpoint
11247 SIGTRAP.
11248
11249 The heuristic failing can be disastrous on
11250 decr_pc_after_break targets.
11251
11252 On decr_pc_after_break targets, like e.g., x86-linux,
11253 if we fail to recognize a late breakpoint SIGTRAP,
11254 because events_till_retirement has reached 0 too
11255 soon, we'll fail to do the PC adjustment, and report
11256 a random SIGTRAP to the user. When the user resumes
11257 the inferior, it will most likely immediately crash
2dec564e 11258 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
db82e815
PA
11259 corrupted, because of being resumed e.g., in the
11260 middle of a multi-byte instruction, or skipped a
11261 one-byte instruction. This was actually seen happen
11262 on native x86-linux, and should be less rare on
11263 targets that do not support new thread events, like
11264 remote, due to the heuristic depending on
11265 thread_count.
11266
11267 Mistaking a random SIGTRAP for a breakpoint trap
11268 causes similar symptoms (PC adjustment applied when
11269 it shouldn't), but then again, playing with SIGTRAPs
11270 behind the debugger's back is asking for trouble.
11271
11272 Since hardware watchpoint traps are always
11273 distinguishable from other traps, so we don't need to
11274 apply keep hardware watchpoint moribund locations
11275 around. We simply always ignore hardware watchpoint
11276 traps we can no longer explain. */
11277
5b6d1e4f
PA
11278 process_stratum_target *proc_target = nullptr;
11279 for (inferior *inf : all_inferiors ())
11280 if (inf->pspace == old_loc->pspace)
11281 {
11282 proc_target = inf->process_target ();
11283 break;
11284 }
11285 if (proc_target != nullptr)
11286 old_loc->events_till_retirement
11287 = 3 * (thread_count (proc_target) + 1);
11288 else
11289 old_loc->events_till_retirement = 1;
876fa593 11290 old_loc->owner = NULL;
20874c92 11291
1123588c 11292 moribund_locations.push_back (old_loc);
1c5cfe86
PA
11293 }
11294 else
f431efe5
PA
11295 {
11296 old_loc->owner = NULL;
11297 decref_bp_location (&old_loc);
11298 }
20874c92 11299 }
74960c60 11300 }
1c5cfe86 11301
348d480f
PA
11302 /* Rescan breakpoints at the same address and section, marking the
11303 first one as "first" and any others as "duplicates". This is so
11304 that the bpt instruction is only inserted once. If we have a
11305 permanent breakpoint at the same place as BPT, make that one the
11306 official one, and the rest as duplicates. Permanent breakpoints
11307 are sorted first for the same address.
11308
11309 Do the same for hardware watchpoints, but also considering the
11310 watchpoint's type (regular/access/read) and length. */
11311
11312 bp_loc_first = NULL;
11313 wp_loc_first = NULL;
11314 awp_loc_first = NULL;
11315 rwp_loc_first = NULL;
40cb8ca5 11316
48d7020b 11317 for (bp_location *loc : all_bp_locations ())
348d480f
PA
11318 {
11319 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
11320 non-NULL. */
348d480f 11321 struct bp_location **loc_first_p;
43892fdf 11322 breakpoint *b = loc->owner;
348d480f 11323
6f380991 11324 if (!unduplicated_should_be_inserted (loc)
cb1e4e32 11325 || !bl_address_is_meaningful (loc)
1e4d1764
YQ
11326 /* Don't detect duplicate for tracepoint locations because they are
11327 never duplicated. See the comments in field `duplicate' of
11328 `struct bp_location'. */
348d480f 11329 || is_tracepoint (b))
b775012e
LM
11330 {
11331 /* Clear the condition modification flag. */
11332 loc->condition_changed = condition_unchanged;
11333 continue;
11334 }
348d480f 11335
348d480f
PA
11336 if (b->type == bp_hardware_watchpoint)
11337 loc_first_p = &wp_loc_first;
11338 else if (b->type == bp_read_watchpoint)
11339 loc_first_p = &rwp_loc_first;
11340 else if (b->type == bp_access_watchpoint)
11341 loc_first_p = &awp_loc_first;
11342 else
11343 loc_first_p = &bp_loc_first;
11344
11345 if (*loc_first_p == NULL
11346 || (overlay_debugging && loc->section != (*loc_first_p)->section)
11347 || !breakpoint_locations_match (loc, *loc_first_p))
11348 {
11349 *loc_first_p = loc;
11350 loc->duplicate = 0;
b775012e
LM
11351
11352 if (is_breakpoint (loc->owner) && loc->condition_changed)
11353 {
11354 loc->needs_update = 1;
11355 /* Clear the condition modification flag. */
11356 loc->condition_changed = condition_unchanged;
11357 }
348d480f
PA
11358 continue;
11359 }
11360
934709f0
PW
11361
11362 /* This and the above ensure the invariant that the first location
11363 is not duplicated, and is the inserted one.
11364 All following are marked as duplicated, and are not inserted. */
11365 if (loc->inserted)
11366 swap_insertion (loc, *loc_first_p);
348d480f
PA
11367 loc->duplicate = 1;
11368
b775012e
LM
11369 /* Clear the condition modification flag. */
11370 loc->condition_changed = condition_unchanged;
348d480f
PA
11371 }
11372
a25a5a45 11373 if (insert_mode == UGLL_INSERT || breakpoints_should_be_inserted_now ())
b775012e 11374 {
04086b45 11375 if (insert_mode != UGLL_DONT_INSERT)
b775012e
LM
11376 insert_breakpoint_locations ();
11377 else
11378 {
44702360
PA
11379 /* Even though the caller told us to not insert new
11380 locations, we may still need to update conditions on the
11381 target's side of breakpoints that were already inserted
11382 if the target is evaluating breakpoint conditions. We
b775012e
LM
11383 only update conditions for locations that are marked
11384 "needs_update". */
11385 update_inserted_breakpoint_locations ();
11386 }
11387 }
348d480f 11388
04086b45 11389 if (insert_mode != UGLL_DONT_INSERT)
1e4d1764 11390 download_tracepoint_locations ();
348d480f
PA
11391}
11392
11393void
11394breakpoint_retire_moribund (void)
11395{
1123588c
TT
11396 for (int ix = 0; ix < moribund_locations.size (); ++ix)
11397 {
11398 struct bp_location *loc = moribund_locations[ix];
11399 if (--(loc->events_till_retirement) == 0)
11400 {
11401 decref_bp_location (&loc);
11402 unordered_remove (moribund_locations, ix);
11403 --ix;
11404 }
11405 }
348d480f
PA
11406}
11407
11408static void
44702360 11409update_global_location_list_nothrow (enum ugll_insert_mode insert_mode)
348d480f 11410{
348d480f 11411
a70b8144 11412 try
492d29ea
PA
11413 {
11414 update_global_location_list (insert_mode);
11415 }
230d2906 11416 catch (const gdb_exception_error &e)
492d29ea
PA
11417 {
11418 }
348d480f
PA
11419}
11420
11421/* Clear BKP from a BPS. */
11422
11423static void
313f3b21 11424bpstat_remove_bp_location (bpstat *bps, struct breakpoint *bpt)
348d480f 11425{
313f3b21 11426 bpstat *bs;
348d480f
PA
11427
11428 for (bs = bps; bs; bs = bs->next)
11429 if (bs->breakpoint_at == bpt)
11430 {
11431 bs->breakpoint_at = NULL;
11432 bs->old_val = NULL;
11433 /* bs->commands will be freed later. */
11434 }
11435}
11436
11437/* Callback for iterate_over_threads. */
11438static int
11439bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
11440{
9a3c8263 11441 struct breakpoint *bpt = (struct breakpoint *) data;
348d480f
PA
11442
11443 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
11444 return 0;
11445}
11446
c47614fe 11447/* Helper for breakpoint and tracepoint breakpoint->mention
348d480f
PA
11448 callbacks. */
11449
11450static void
b713485d 11451say_where (const breakpoint *b)
348d480f
PA
11452{
11453 struct value_print_options opts;
11454
11455 get_user_print_options (&opts);
11456
11457 /* i18n: cagney/2005-02-11: Below needs to be merged into a
11458 single string. */
11459 if (b->loc == NULL)
11460 {
f00aae0f
KS
11461 /* For pending locations, the output differs slightly based
11462 on b->extra_string. If this is non-NULL, it contains either
11463 a condition or dprintf arguments. */
11464 if (b->extra_string == NULL)
11465 {
6cb06a8c
TT
11466 gdb_printf (_(" (%s) pending."),
11467 event_location_to_string (b->location.get ()));
f00aae0f
KS
11468 }
11469 else if (b->type == bp_dprintf)
11470 {
6cb06a8c
TT
11471 gdb_printf (_(" (%s,%s) pending."),
11472 event_location_to_string (b->location.get ()),
11473 b->extra_string.get ());
f00aae0f
KS
11474 }
11475 else
11476 {
6cb06a8c
TT
11477 gdb_printf (_(" (%s %s) pending."),
11478 event_location_to_string (b->location.get ()),
11479 b->extra_string.get ());
f00aae0f 11480 }
348d480f
PA
11481 }
11482 else
11483 {
2f202fde 11484 if (opts.addressprint || b->loc->symtab == NULL)
6cb06a8c
TT
11485 gdb_printf (" at %ps",
11486 styled_string (address_style.style (),
11487 paddress (b->loc->gdbarch,
11488 b->loc->address)));
2f202fde 11489 if (b->loc->symtab != NULL)
f8eba3c6
TT
11490 {
11491 /* If there is a single location, we can print the location
11492 more nicely. */
11493 if (b->loc->next == NULL)
0bb296cb 11494 {
6a831f06
PA
11495 const char *filename
11496 = symtab_to_filename_for_display (b->loc->symtab);
6cb06a8c
TT
11497 gdb_printf (": file %ps, line %d.",
11498 styled_string (file_name_style.style (),
11499 filename),
11500 b->loc->line_number);
0bb296cb 11501 }
f8eba3c6
TT
11502 else
11503 /* This is not ideal, but each location may have a
11504 different file name, and this at least reflects the
11505 real situation somewhat. */
6cb06a8c
TT
11506 gdb_printf (": %s.",
11507 event_location_to_string (b->location.get ()));
f8eba3c6 11508 }
348d480f
PA
11509
11510 if (b->loc->next)
11511 {
11512 struct bp_location *loc = b->loc;
11513 int n = 0;
11514 for (; loc; loc = loc->next)
11515 ++n;
6cb06a8c 11516 gdb_printf (" (%d locations)", n);
348d480f
PA
11517 }
11518 }
11519}
11520
40cb8ca5
SM
11521/* See breakpoint.h. */
11522
a67bcaba 11523bp_location_range breakpoint::locations () const
40cb8ca5 11524{
9be25986 11525 return bp_location_range (this->loc);
40cb8ca5
SM
11526}
11527
4c6a92b1
TT
11528struct bp_location *
11529breakpoint::allocate_location ()
11530{
11531 return new bp_location (this);
11532}
11533
2060206e
PA
11534#define internal_error_pure_virtual_called() \
11535 gdb_assert_not_reached ("pure virtual function called")
11536
4c6a92b1
TT
11537int
11538breakpoint::insert_location (struct bp_location *bl)
11539{
11540 internal_error_pure_virtual_called ();
11541}
11542
4c6a92b1
TT
11543int
11544breakpoint::remove_location (struct bp_location *bl,
11545 enum remove_bp_reason reason)
11546{
11547 internal_error_pure_virtual_called ();
11548}
11549
4c6a92b1
TT
11550int
11551breakpoint::breakpoint_hit (const struct bp_location *bl,
11552 const address_space *aspace,
11553 CORE_ADDR bp_addr,
11554 const target_waitstatus &ws)
11555{
11556 internal_error_pure_virtual_called ();
11557}
11558
4c6a92b1
TT
11559int
11560breakpoint::resources_needed (const struct bp_location *bl)
11561{
11562 internal_error_pure_virtual_called ();
11563}
11564
4c6a92b1 11565enum print_stop_action
7bd86313 11566breakpoint::print_it (const bpstat *bs) const
4c6a92b1
TT
11567{
11568 internal_error_pure_virtual_called ();
11569}
11570
4c6a92b1 11571void
b713485d 11572breakpoint::print_mention () const
4c6a92b1
TT
11573{
11574 internal_error_pure_virtual_called ();
11575}
11576
4c6a92b1 11577void
4d1ae558 11578breakpoint::print_recreate (struct ui_file *fp) const
4c6a92b1
TT
11579{
11580 internal_error_pure_virtual_called ();
11581}
11582
4c6a92b1
TT
11583std::vector<symtab_and_line>
11584breakpoint::decode_location (struct event_location *location,
11585 struct program_space *search_pspace)
11586{
11587 internal_error_pure_virtual_called ();
11588}
11589
2060206e
PA
11590/* Default breakpoint_ops methods. */
11591
2b5ab5b8
TT
11592void
11593base_breakpoint::re_set ()
348d480f 11594{
06edf0c0 11595 /* FIXME: is this still reachable? */
2b5ab5b8 11596 if (breakpoint_event_location_empty_p (this))
06edf0c0 11597 {
f00aae0f 11598 /* Anything without a location can't be re-set. */
2b5ab5b8 11599 delete_breakpoint (this);
06edf0c0 11600 return;
348d480f 11601 }
06edf0c0 11602
2b5ab5b8 11603 breakpoint_re_set_default (this);
348d480f
PA
11604}
11605
2b5ab5b8
TT
11606int
11607base_breakpoint::insert_location (struct bp_location *bl)
348d480f 11608{
cd6c3b4f
YQ
11609 CORE_ADDR addr = bl->target_info.reqstd_address;
11610
579c6ad9 11611 bl->target_info.kind = breakpoint_kind (bl, &addr);
cd6c3b4f
YQ
11612 bl->target_info.placed_address = addr;
11613
5d926615 11614 int result;
348d480f 11615 if (bl->loc_type == bp_loc_hardware_breakpoint)
5d926615 11616 result = target_insert_hw_breakpoint (bl->gdbarch, &bl->target_info);
348d480f 11617 else
5d926615
TT
11618 result = target_insert_breakpoint (bl->gdbarch, &bl->target_info);
11619
11620 if (result == 0 && bl->probe.prob != nullptr)
11621 {
11622 /* The insertion was successful, now let's set the probe's semaphore
11623 if needed. */
11624 bl->probe.prob->set_semaphore (bl->probe.objfile, bl->gdbarch);
11625 }
11626
11627 return result;
348d480f
PA
11628}
11629
2b5ab5b8
TT
11630int
11631base_breakpoint::remove_location (struct bp_location *bl,
11632 enum remove_bp_reason reason)
348d480f 11633{
5d926615
TT
11634 if (bl->probe.prob != nullptr)
11635 {
11636 /* Let's clear the semaphore before removing the location. */
11637 bl->probe.prob->clear_semaphore (bl->probe.objfile, bl->gdbarch);
11638 }
11639
348d480f
PA
11640 if (bl->loc_type == bp_loc_hardware_breakpoint)
11641 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
11642 else
73971819 11643 return target_remove_breakpoint (bl->gdbarch, &bl->target_info, reason);
348d480f
PA
11644}
11645
2b5ab5b8
TT
11646int
11647base_breakpoint::breakpoint_hit (const struct bp_location *bl,
11648 const address_space *aspace,
11649 CORE_ADDR bp_addr,
11650 const target_waitstatus &ws)
348d480f 11651{
c272a98c
SM
11652 if (ws.kind () != TARGET_WAITKIND_STOPPED
11653 || ws.sig () != GDB_SIGNAL_TRAP)
09ac7c10
TT
11654 return 0;
11655
348d480f
PA
11656 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
11657 aspace, bp_addr))
11658 return 0;
11659
11660 if (overlay_debugging /* unmapped overlay section */
11661 && section_is_overlay (bl->section)
11662 && !section_is_mapped (bl->section))
11663 return 0;
11664
11665 return 1;
11666}
11667
f293a0b5
TT
11668int
11669dprintf_breakpoint::breakpoint_hit (const struct bp_location *bl,
11670 const address_space *aspace,
11671 CORE_ADDR bp_addr,
11672 const target_waitstatus &ws)
cd1608cc
PA
11673{
11674 if (dprintf_style == dprintf_style_agent
11675 && target_can_run_breakpoint_commands ())
11676 {
11677 /* An agent-style dprintf never causes a stop. If we see a trap
11678 for this address it must be for a breakpoint that happens to
11679 be set at the same address. */
11680 return 0;
11681 }
11682
f293a0b5 11683 return this->ordinary_breakpoint::breakpoint_hit (bl, aspace, bp_addr, ws);
cd1608cc
PA
11684}
11685
7dd8e7ae
TT
11686int
11687ordinary_breakpoint::resources_needed (const struct bp_location *bl)
348d480f 11688{
7dd8e7ae 11689 gdb_assert (type == bp_hardware_breakpoint);
348d480f
PA
11690
11691 return 1;
11692}
11693
7dd8e7ae 11694enum print_stop_action
7bd86313 11695ordinary_breakpoint::print_it (const bpstat *bs) const
348d480f 11696{
348d480f 11697 const struct bp_location *bl;
001c8c33 11698 int bp_temp;
79a45e25 11699 struct ui_out *uiout = current_uiout;
348d480f 11700
b6433ede 11701 bl = bs->bp_location_at.get ();
348d480f 11702
7dd8e7ae 11703 bp_temp = disposition == disp_del;
001c8c33
PA
11704 if (bl->address != bl->requested_address)
11705 breakpoint_adjustment_warning (bl->requested_address,
11706 bl->address,
7dd8e7ae
TT
11707 number, 1);
11708 annotate_breakpoint (number);
f303dbd6
PA
11709 maybe_print_thread_hit_breakpoint (uiout);
11710
112e8700 11711 if (uiout->is_mi_like_p ())
348d480f 11712 {
112e8700 11713 uiout->field_string ("reason",
001c8c33 11714 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
7dd8e7ae 11715 uiout->field_string ("disp", bpdisp_text (disposition));
06edf0c0 11716 }
6a831f06
PA
11717 if (bp_temp)
11718 uiout->message ("Temporary breakpoint %pF, ",
7dd8e7ae 11719 signed_field ("bkptno", number));
6a831f06
PA
11720 else
11721 uiout->message ("Breakpoint %pF, ",
7dd8e7ae 11722 signed_field ("bkptno", number));
06edf0c0 11723
001c8c33 11724 return PRINT_SRC_AND_LOC;
06edf0c0
PA
11725}
11726
7dd8e7ae 11727void
b713485d 11728ordinary_breakpoint::print_mention () const
06edf0c0 11729{
112e8700 11730 if (current_uiout->is_mi_like_p ())
06edf0c0
PA
11731 return;
11732
7dd8e7ae 11733 switch (type)
06edf0c0
PA
11734 {
11735 case bp_breakpoint:
11736 case bp_gnu_ifunc_resolver:
7dd8e7ae 11737 if (disposition == disp_del)
6cb06a8c 11738 gdb_printf (_("Temporary breakpoint"));
06edf0c0 11739 else
6cb06a8c 11740 gdb_printf (_("Breakpoint"));
7dd8e7ae
TT
11741 gdb_printf (_(" %d"), number);
11742 if (type == bp_gnu_ifunc_resolver)
6cb06a8c 11743 gdb_printf (_(" at gnu-indirect-function resolver"));
06edf0c0
PA
11744 break;
11745 case bp_hardware_breakpoint:
7dd8e7ae 11746 gdb_printf (_("Hardware assisted breakpoint %d"), number);
06edf0c0 11747 break;
e7e0cddf 11748 case bp_dprintf:
7dd8e7ae 11749 gdb_printf (_("Dprintf %d"), number);
e7e0cddf 11750 break;
06edf0c0
PA
11751 }
11752
7dd8e7ae 11753 say_where (this);
06edf0c0
PA
11754}
11755
7dd8e7ae 11756void
4d1ae558 11757ordinary_breakpoint::print_recreate (struct ui_file *fp) const
06edf0c0 11758{
7dd8e7ae 11759 if (type == bp_breakpoint && disposition == disp_del)
6cb06a8c 11760 gdb_printf (fp, "tbreak");
7dd8e7ae 11761 else if (type == bp_breakpoint)
6cb06a8c 11762 gdb_printf (fp, "break");
7dd8e7ae
TT
11763 else if (type == bp_hardware_breakpoint
11764 && disposition == disp_del)
6cb06a8c 11765 gdb_printf (fp, "thbreak");
7dd8e7ae 11766 else if (type == bp_hardware_breakpoint)
6cb06a8c 11767 gdb_printf (fp, "hbreak");
06edf0c0
PA
11768 else
11769 internal_error (__FILE__, __LINE__,
7dd8e7ae 11770 _("unhandled breakpoint type %d"), (int) type);
06edf0c0 11771
7dd8e7ae 11772 gdb_printf (fp, " %s", event_location_to_string (location.get ()));
f00aae0f
KS
11773
11774 /* Print out extra_string if this breakpoint is pending. It might
11775 contain, for example, conditions that were set by the user. */
7dd8e7ae
TT
11776 if (loc == NULL && extra_string != NULL)
11777 gdb_printf (fp, " %s", extra_string.get ());
f00aae0f 11778
04d0163c 11779 print_recreate_thread (fp);
06edf0c0
PA
11780}
11781
2b5ab5b8
TT
11782std::vector<symtab_and_line>
11783base_breakpoint::decode_location (struct event_location *location,
11784 struct program_space *search_pspace)
983af33b 11785{
5d926615
TT
11786 if (event_location_type (location) == PROBE_LOCATION)
11787 return bkpt_probe_decode_location (this, location, search_pspace);
11788
2b5ab5b8 11789 return decode_location_default (this, location, search_pspace);
983af33b
SDJ
11790}
11791
06edf0c0
PA
11792/* Virtual table for internal breakpoints. */
11793
c359fff5
TT
11794void
11795internal_breakpoint::re_set ()
06edf0c0 11796{
c359fff5 11797 switch (type)
06edf0c0
PA
11798 {
11799 /* Delete overlay event and longjmp master breakpoints; they
11800 will be reset later by breakpoint_re_set. */
11801 case bp_overlay_event:
11802 case bp_longjmp_master:
11803 case bp_std_terminate_master:
11804 case bp_exception_master:
c359fff5 11805 delete_breakpoint (this);
06edf0c0
PA
11806 break;
11807
11808 /* This breakpoint is special, it's set up when the inferior
dda83cd7 11809 starts and we really don't want to touch it. */
06edf0c0
PA
11810 case bp_shlib_event:
11811
11812 /* Like bp_shlib_event, this breakpoint type is special. Once
11813 it is set up, we do not want to touch it. */
11814 case bp_thread_event:
11815 break;
11816 }
11817}
11818
c359fff5
TT
11819void
11820internal_breakpoint::check_status (bpstat *bs)
06edf0c0 11821{
c359fff5 11822 if (type == bp_shlib_event)
a9b3a50f
PA
11823 {
11824 /* If requested, stop when the dynamic linker notifies GDB of
11825 events. This allows the user to get control and place
11826 breakpoints in initializer routines for dynamically loaded
11827 objects (among other things). */
11828 bs->stop = stop_on_solib_events;
11829 bs->print = stop_on_solib_events;
11830 }
11831 else
11832 bs->stop = 0;
06edf0c0
PA
11833}
11834
c359fff5 11835enum print_stop_action
7bd86313 11836internal_breakpoint::print_it (const bpstat *bs) const
06edf0c0 11837{
c359fff5 11838 switch (type)
06edf0c0 11839 {
348d480f
PA
11840 case bp_shlib_event:
11841 /* Did we stop because the user set the stop_on_solib_events
11842 variable? (If so, we report this as a generic, "Stopped due
11843 to shlib event" message.) */
4e9e993a 11844 print_solib_event (false);
348d480f
PA
11845 break;
11846
11847 case bp_thread_event:
11848 /* Not sure how we will get here.
11849 GDB should not stop for these breakpoints. */
6cb06a8c 11850 gdb_printf (_("Thread Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
11851 break;
11852
11853 case bp_overlay_event:
11854 /* By analogy with the thread event, GDB should not stop for these. */
6cb06a8c 11855 gdb_printf (_("Overlay Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
11856 break;
11857
11858 case bp_longjmp_master:
11859 /* These should never be enabled. */
6cb06a8c 11860 gdb_printf (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
348d480f
PA
11861 break;
11862
11863 case bp_std_terminate_master:
11864 /* These should never be enabled. */
6cb06a8c
TT
11865 gdb_printf (_("std::terminate Master Breakpoint: "
11866 "gdb should not stop!\n"));
348d480f
PA
11867 break;
11868
11869 case bp_exception_master:
11870 /* These should never be enabled. */
6cb06a8c
TT
11871 gdb_printf (_("Exception Master Breakpoint: "
11872 "gdb should not stop!\n"));
06edf0c0
PA
11873 break;
11874 }
11875
001c8c33 11876 return PRINT_NOTHING;
06edf0c0
PA
11877}
11878
c359fff5 11879void
b713485d 11880internal_breakpoint::print_mention () const
06edf0c0
PA
11881{
11882 /* Nothing to mention. These breakpoints are internal. */
11883}
11884
06edf0c0
PA
11885/* Virtual table for momentary breakpoints */
11886
1fd30a47
TT
11887void
11888momentary_breakpoint::re_set ()
06edf0c0
PA
11889{
11890 /* Keep temporary breakpoints, which can be encountered when we step
4d1eb6b4 11891 over a dlopen call and solib_add is resetting the breakpoints.
06edf0c0
PA
11892 Otherwise these should have been blown away via the cleanup chain
11893 or by breakpoint_init_inferior when we rerun the executable. */
11894}
11895
1fd30a47
TT
11896void
11897momentary_breakpoint::check_status (bpstat *bs)
06edf0c0
PA
11898{
11899 /* Nothing. The point of these breakpoints is causing a stop. */
11900}
11901
1fd30a47 11902enum print_stop_action
7bd86313 11903momentary_breakpoint::print_it (const bpstat *bs) const
06edf0c0 11904{
001c8c33 11905 return PRINT_UNKNOWN;
348d480f
PA
11906}
11907
1fd30a47 11908void
b713485d 11909momentary_breakpoint::print_mention () const
348d480f 11910{
06edf0c0 11911 /* Nothing to mention. These breakpoints are internal. */
348d480f
PA
11912}
11913
e2e4d78b
JK
11914/* Ensure INITIATING_FRAME is cleared when no such breakpoint exists.
11915
11916 It gets cleared already on the removal of the first one of such placed
11917 breakpoints. This is OK as they get all removed altogether. */
11918
c1fc2657 11919longjmp_breakpoint::~longjmp_breakpoint ()
e2e4d78b 11920{
c1fc2657 11921 thread_info *tp = find_thread_global_id (this->thread);
e2e4d78b 11922
c1fc2657 11923 if (tp != NULL)
e2e4d78b 11924 tp->initiating_frame = null_frame_id;
e2e4d78b
JK
11925}
11926
55aa24fb 11927static void
626d2320 11928bkpt_probe_create_sals_from_location (struct event_location *location,
03ada39e
SM
11929 struct linespec_result *canonical)
11930
55aa24fb
SDJ
11931{
11932 struct linespec_sals lsal;
11933
c2f4122d 11934 lsal.sals = parse_probes (location, NULL, canonical);
8e9e35b1
TT
11935 lsal.canonical
11936 = xstrdup (event_location_to_string (canonical->location.get ()));
6c5b2ebe 11937 canonical->lsals.push_back (std::move (lsal));
55aa24fb
SDJ
11938}
11939
6c5b2ebe 11940static std::vector<symtab_and_line>
f00aae0f 11941bkpt_probe_decode_location (struct breakpoint *b,
626d2320 11942 struct event_location *location,
6c5b2ebe 11943 struct program_space *search_pspace)
55aa24fb 11944{
6c5b2ebe
PA
11945 std::vector<symtab_and_line> sals = parse_probes (location, search_pspace, NULL);
11946 if (sals.empty ())
55aa24fb 11947 error (_("probe not found"));
6c5b2ebe 11948 return sals;
55aa24fb
SDJ
11949}
11950
348d480f 11951/* The breakpoint_ops structure to be used in tracepoints. */
876fa593 11952
779dcceb
TT
11953void
11954tracepoint::re_set ()
348d480f 11955{
779dcceb 11956 breakpoint_re_set_default (this);
348d480f 11957}
876fa593 11958
779dcceb
TT
11959int
11960tracepoint::breakpoint_hit (const struct bp_location *bl,
11961 const address_space *aspace, CORE_ADDR bp_addr,
11962 const target_waitstatus &ws)
348d480f
PA
11963{
11964 /* By definition, the inferior does not report stops at
11965 tracepoints. */
11966 return 0;
74960c60
VP
11967}
11968
779dcceb
TT
11969void
11970tracepoint::print_one_detail (struct ui_out *uiout) const
74960c60 11971{
779dcceb 11972 if (!static_trace_marker_id.empty ())
348d480f 11973 {
7b572efb
TT
11974 gdb_assert (type == bp_static_tracepoint
11975 || type == bp_static_marker_tracepoint);
cc59ec59 11976
6a831f06
PA
11977 uiout->message ("\tmarker id is %pF\n",
11978 string_field ("static-tracepoint-marker-string-id",
779dcceb 11979 static_trace_marker_id.c_str ()));
348d480f 11980 }
0d381245
VP
11981}
11982
779dcceb 11983void
b713485d 11984tracepoint::print_mention () const
a474d7c2 11985{
112e8700 11986 if (current_uiout->is_mi_like_p ())
348d480f 11987 return;
cc59ec59 11988
779dcceb 11989 switch (type)
348d480f
PA
11990 {
11991 case bp_tracepoint:
6cb06a8c 11992 gdb_printf (_("Tracepoint"));
779dcceb 11993 gdb_printf (_(" %d"), number);
348d480f
PA
11994 break;
11995 case bp_fast_tracepoint:
6cb06a8c 11996 gdb_printf (_("Fast tracepoint"));
779dcceb 11997 gdb_printf (_(" %d"), number);
348d480f
PA
11998 break;
11999 case bp_static_tracepoint:
7b572efb 12000 case bp_static_marker_tracepoint:
6cb06a8c 12001 gdb_printf (_("Static tracepoint"));
779dcceb 12002 gdb_printf (_(" %d"), number);
348d480f
PA
12003 break;
12004 default:
12005 internal_error (__FILE__, __LINE__,
779dcceb 12006 _("unhandled tracepoint type %d"), (int) type);
348d480f
PA
12007 }
12008
779dcceb 12009 say_where (this);
a474d7c2
PA
12010}
12011
779dcceb 12012void
4d1ae558 12013tracepoint::print_recreate (struct ui_file *fp) const
a474d7c2 12014{
779dcceb 12015 if (type == bp_fast_tracepoint)
6cb06a8c 12016 gdb_printf (fp, "ftrace");
7b572efb
TT
12017 else if (type == bp_static_tracepoint
12018 || type == bp_static_marker_tracepoint)
6cb06a8c 12019 gdb_printf (fp, "strace");
779dcceb 12020 else if (type == bp_tracepoint)
6cb06a8c 12021 gdb_printf (fp, "trace");
348d480f
PA
12022 else
12023 internal_error (__FILE__, __LINE__,
779dcceb 12024 _("unhandled tracepoint type %d"), (int) type);
cc59ec59 12025
779dcceb 12026 gdb_printf (fp, " %s", event_location_to_string (location.get ()));
04d0163c 12027 print_recreate_thread (fp);
d9b3f62e 12028
779dcceb
TT
12029 if (pass_count)
12030 gdb_printf (fp, " passcount %d\n", pass_count);
a474d7c2
PA
12031}
12032
779dcceb
TT
12033std::vector<symtab_and_line>
12034tracepoint::decode_location (struct event_location *location,
12035 struct program_space *search_pspace)
983af33b 12036{
8d89123d
TT
12037 if (event_location_type (location) == PROBE_LOCATION)
12038 return bkpt_probe_decode_location (this, location, search_pspace);
12039
779dcceb 12040 return decode_location_default (this, location, search_pspace);
983af33b
SDJ
12041}
12042
bac7c5cf 12043/* Virtual table for tracepoints on static probes. */
55aa24fb
SDJ
12044
12045static void
f00aae0f 12046tracepoint_probe_create_sals_from_location
626d2320 12047 (struct event_location *location,
03ada39e 12048 struct linespec_result *canonical)
55aa24fb
SDJ
12049{
12050 /* We use the same method for breakpoint on probes. */
03ada39e 12051 bkpt_probe_create_sals_from_location (location, canonical);
55aa24fb
SDJ
12052}
12053
f293a0b5
TT
12054void
12055dprintf_breakpoint::re_set ()
5c2b4418 12056{
f293a0b5 12057 breakpoint_re_set_default (this);
5c2b4418 12058
f00aae0f 12059 /* extra_string should never be non-NULL for dprintf. */
f293a0b5 12060 gdb_assert (extra_string != NULL);
5c2b4418
HZ
12061
12062 /* 1 - connect to target 1, that can run breakpoint commands.
12063 2 - create a dprintf, which resolves fine.
12064 3 - disconnect from target 1
12065 4 - connect to target 2, that can NOT run breakpoint commands.
12066
12067 After steps #3/#4, you'll want the dprintf command list to
12068 be updated, because target 1 and 2 may well return different
12069 answers for target_can_run_breakpoint_commands().
12070 Given absence of finer grained resetting, we get to do
12071 it all the time. */
f293a0b5
TT
12072 if (extra_string != NULL)
12073 update_dprintf_command_list (this);
5c2b4418
HZ
12074}
12075
f293a0b5 12076/* Implement the "print_recreate" method for dprintf. */
2d9442cc 12077
f293a0b5 12078void
4d1ae558 12079dprintf_breakpoint::print_recreate (struct ui_file *fp) const
2d9442cc 12080{
6cb06a8c 12081 gdb_printf (fp, "dprintf %s,%s",
f293a0b5
TT
12082 event_location_to_string (location.get ()),
12083 extra_string.get ());
04d0163c 12084 print_recreate_thread (fp);
2d9442cc
HZ
12085}
12086
f293a0b5 12087/* Implement the "after_condition_true" method for dprintf.
9d6e6e84
HZ
12088
12089 dprintf's are implemented with regular commands in their command
12090 list, but we run the commands here instead of before presenting the
12091 stop to the user, as dprintf's don't actually cause a stop. This
12092 also makes it so that the commands of multiple dprintfs at the same
12093 address are all handled. */
12094
f293a0b5
TT
12095void
12096dprintf_breakpoint::after_condition_true (struct bpstat *bs)
9d6e6e84 12097{
9d6e6e84
HZ
12098 /* dprintf's never cause a stop. This wasn't set in the
12099 check_status hook instead because that would make the dprintf's
12100 condition not be evaluated. */
12101 bs->stop = 0;
12102
12103 /* Run the command list here. Take ownership of it instead of
12104 copying. We never want these commands to run later in
12105 bpstat_do_actions, if a breakpoint that causes a stop happens to
12106 be set at same address as this dprintf, or even if running the
12107 commands here throws. */
9c95aea1
KB
12108 counted_command_line cmds = std::move (bs->commands);
12109 gdb_assert (cmds != nullptr);
12110 execute_control_commands (cmds.get (), 0);
9d6e6e84
HZ
12111}
12112
983af33b
SDJ
12113/* The breakpoint_ops structure to be used on static tracepoints with
12114 markers (`-m'). */
12115
12116static void
626d2320 12117strace_marker_create_sals_from_location (struct event_location *location,
03ada39e 12118 struct linespec_result *canonical)
983af33b
SDJ
12119{
12120 struct linespec_sals lsal;
f00aae0f 12121 const char *arg_start, *arg;
983af33b 12122
a20714ff 12123 arg = arg_start = get_linespec_location (location)->spec_string;
f00aae0f 12124 lsal.sals = decode_static_tracepoint_spec (&arg);
983af33b 12125
f2fc3015
TT
12126 std::string str (arg_start, arg - arg_start);
12127 const char *ptr = str.c_str ();
a20714ff
PA
12128 canonical->location
12129 = new_linespec_location (&ptr, symbol_name_match_type::FULL);
983af33b 12130
8e9e35b1
TT
12131 lsal.canonical
12132 = xstrdup (event_location_to_string (canonical->location.get ()));
6c5b2ebe 12133 canonical->lsals.push_back (std::move (lsal));
983af33b
SDJ
12134}
12135
12136static void
12137strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
12138 struct linespec_result *canonical,
e1e01040
PA
12139 gdb::unique_xmalloc_ptr<char> cond_string,
12140 gdb::unique_xmalloc_ptr<char> extra_string,
983af33b
SDJ
12141 enum bptype type_wanted,
12142 enum bpdisp disposition,
12143 int thread,
12144 int task, int ignore_count,
12145 const struct breakpoint_ops *ops,
12146 int from_tty, int enabled,
44f238bb 12147 int internal, unsigned flags)
983af33b 12148{
6c5b2ebe 12149 const linespec_sals &lsal = canonical->lsals[0];
983af33b
SDJ
12150
12151 /* If the user is creating a static tracepoint by marker id
12152 (strace -m MARKER_ID), then store the sals index, so that
12153 breakpoint_re_set can try to match up which of the newly
12154 found markers corresponds to this one, and, don't try to
12155 expand multiple locations for each sal, given than SALS
12156 already should contain all sals for MARKER_ID. */
12157
6c5b2ebe 12158 for (size_t i = 0; i < lsal.sals.size (); i++)
983af33b 12159 {
6c5b2ebe
PA
12160 event_location_up location
12161 = copy_event_location (canonical->location.get ());
983af33b 12162
73063f51
TT
12163 std::unique_ptr<tracepoint> tp (new tracepoint (gdbarch,
12164 type_wanted));
6c5b2ebe 12165 init_breakpoint_sal (tp.get (), gdbarch, lsal.sals[i],
ffc2605c 12166 std::move (location), NULL,
e1e01040
PA
12167 std::move (cond_string),
12168 std::move (extra_string),
e7e0cddf 12169 type_wanted, disposition,
983af33b 12170 thread, task, ignore_count, ops,
44f238bb 12171 from_tty, enabled, internal, flags,
983af33b
SDJ
12172 canonical->special_display);
12173 /* Given that its possible to have multiple markers with
12174 the same string id, if the user is creating a static
12175 tracepoint by marker id ("strace -m MARKER_ID"), then
12176 store the sals index, so that breakpoint_re_set can
12177 try to match up which of the newly found markers
12178 corresponds to this one */
12179 tp->static_trace_marker_id_idx = i;
12180
b270e6f9 12181 install_breakpoint (internal, std::move (tp), 0);
983af33b
SDJ
12182 }
12183}
12184
6d7a8c56
TT
12185std::vector<symtab_and_line>
12186static_marker_tracepoint::decode_location (struct event_location *location,
12187 struct program_space *search_pspace)
983af33b 12188{
a20714ff 12189 const char *s = get_linespec_location (location)->spec_string;
983af33b 12190
6c5b2ebe 12191 std::vector<symtab_and_line> sals = decode_static_tracepoint_spec (&s);
6d7a8c56 12192 if (sals.size () > static_trace_marker_id_idx)
983af33b 12193 {
6d7a8c56 12194 sals[0] = sals[static_trace_marker_id_idx];
6c5b2ebe
PA
12195 sals.resize (1);
12196 return sals;
983af33b
SDJ
12197 }
12198 else
6d7a8c56 12199 error (_("marker %s not found"), static_trace_marker_id.c_str ());
983af33b
SDJ
12200}
12201
8613a476
TT
12202/* Static tracepoints with marker (`-m'). */
12203static struct breakpoint_ops strace_marker_breakpoint_ops =
12204{
12205 strace_marker_create_sals_from_location,
12206 strace_marker_create_breakpoints_sal,
12207};
983af33b
SDJ
12208
12209static int
12210strace_marker_p (struct breakpoint *b)
12211{
7b572efb 12212 return b->type == bp_static_marker_tracepoint;
983af33b
SDJ
12213}
12214
53a5351d 12215/* Delete a breakpoint and clean up all traces of it in the data
f431efe5 12216 structures. */
c906108c
SS
12217
12218void
fba45db2 12219delete_breakpoint (struct breakpoint *bpt)
c906108c 12220{
8a3fe4f8 12221 gdb_assert (bpt != NULL);
c906108c 12222
4a64f543
MS
12223 /* Has this bp already been deleted? This can happen because
12224 multiple lists can hold pointers to bp's. bpstat lists are
12225 especial culprits.
12226
12227 One example of this happening is a watchpoint's scope bp. When
12228 the scope bp triggers, we notice that the watchpoint is out of
12229 scope, and delete it. We also delete its scope bp. But the
12230 scope bp is marked "auto-deleting", and is already on a bpstat.
12231 That bpstat is then checked for auto-deleting bp's, which are
12232 deleted.
12233
12234 A real solution to this problem might involve reference counts in
12235 bp's, and/or giving them pointers back to their referencing
12236 bpstat's, and teaching delete_breakpoint to only free a bp's
12237 storage when no more references were extent. A cheaper bandaid
12238 was chosen. */
c906108c
SS
12239 if (bpt->type == bp_none)
12240 return;
12241
4a64f543
MS
12242 /* At least avoid this stale reference until the reference counting
12243 of breakpoints gets resolved. */
d0fb5eae 12244 if (bpt->related_breakpoint != bpt)
e5a0a904 12245 {
d0fb5eae 12246 struct breakpoint *related;
3a5c3e22 12247 struct watchpoint *w;
d0fb5eae
JK
12248
12249 if (bpt->type == bp_watchpoint_scope)
3a5c3e22 12250 w = (struct watchpoint *) bpt->related_breakpoint;
d0fb5eae 12251 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
3a5c3e22
PA
12252 w = (struct watchpoint *) bpt;
12253 else
12254 w = NULL;
12255 if (w != NULL)
12256 watchpoint_del_at_next_stop (w);
d0fb5eae
JK
12257
12258 /* Unlink bpt from the bpt->related_breakpoint ring. */
12259 for (related = bpt; related->related_breakpoint != bpt;
12260 related = related->related_breakpoint);
12261 related->related_breakpoint = bpt->related_breakpoint;
12262 bpt->related_breakpoint = bpt;
e5a0a904
JK
12263 }
12264
a9634178
TJB
12265 /* watch_command_1 creates a watchpoint but only sets its number if
12266 update_watchpoint succeeds in creating its bp_locations. If there's
12267 a problem in that process, we'll be asked to delete the half-created
12268 watchpoint. In that case, don't announce the deletion. */
12269 if (bpt->number)
76727919 12270 gdb::observers::breakpoint_deleted.notify (bpt);
c906108c 12271
c906108c
SS
12272 if (breakpoint_chain == bpt)
12273 breakpoint_chain = bpt->next;
12274
43892fdf 12275 for (breakpoint *b : all_breakpoints ())
c906108c 12276 if (b->next == bpt)
01add95b
SM
12277 {
12278 b->next = bpt->next;
12279 break;
12280 }
c906108c 12281
f431efe5
PA
12282 /* Be sure no bpstat's are pointing at the breakpoint after it's
12283 been freed. */
12284 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
e5dd4106 12285 in all threads for now. Note that we cannot just remove bpstats
f431efe5
PA
12286 pointing at bpt from the stop_bpstat list entirely, as breakpoint
12287 commands are associated with the bpstat; if we remove it here,
12288 then the later call to bpstat_do_actions (&stop_bpstat); in
12289 event-top.c won't do anything, and temporary breakpoints with
12290 commands won't work. */
12291
12292 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
12293
4a64f543
MS
12294 /* Now that breakpoint is removed from breakpoint list, update the
12295 global location list. This will remove locations that used to
12296 belong to this breakpoint. Do this before freeing the breakpoint
12297 itself, since remove_breakpoint looks at location's owner. It
12298 might be better design to have location completely
12299 self-contained, but it's not the case now. */
44702360 12300 update_global_location_list (UGLL_DONT_INSERT);
74960c60 12301
4a64f543
MS
12302 /* On the chance that someone will soon try again to delete this
12303 same bp, we mark it as deleted before freeing its storage. */
c906108c 12304 bpt->type = bp_none;
4d01a485 12305 delete bpt;
c906108c
SS
12306}
12307
51be5b68
PA
12308/* Iterator function to call a user-provided callback function once
12309 for each of B and its related breakpoints. */
12310
12311static void
12312iterate_over_related_breakpoints (struct breakpoint *b,
48649e1b 12313 gdb::function_view<void (breakpoint *)> function)
51be5b68
PA
12314{
12315 struct breakpoint *related;
12316
12317 related = b;
12318 do
12319 {
12320 struct breakpoint *next;
12321
12322 /* FUNCTION may delete RELATED. */
12323 next = related->related_breakpoint;
12324
12325 if (next == related)
12326 {
12327 /* RELATED is the last ring entry. */
48649e1b 12328 function (related);
51be5b68
PA
12329
12330 /* FUNCTION may have deleted it, so we'd never reach back to
12331 B. There's nothing left to do anyway, so just break
12332 out. */
12333 break;
12334 }
12335 else
48649e1b 12336 function (related);
51be5b68
PA
12337
12338 related = next;
12339 }
12340 while (related != b);
12341}
95a42b64 12342
4495129a 12343static void
981a3fb3 12344delete_command (const char *arg, int from_tty)
c906108c 12345{
ea9365bb
TT
12346 dont_repeat ();
12347
c906108c
SS
12348 if (arg == 0)
12349 {
12350 int breaks_to_delete = 0;
12351
46c6471b 12352 /* Delete all breakpoints if no argument. Do not delete
dda83cd7
SM
12353 internal breakpoints, these have to be deleted with an
12354 explicit breakpoint number argument. */
43892fdf 12355 for (breakpoint *b : all_breakpoints ())
46c6471b 12356 if (user_breakpoint_p (b))
973d738b
DJ
12357 {
12358 breaks_to_delete = 1;
12359 break;
12360 }
c906108c
SS
12361
12362 /* Ask user only if there are some breakpoints to delete. */
12363 if (!from_tty
e2e0b3e5 12364 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
1428b37a
SM
12365 for (breakpoint *b : all_breakpoints_safe ())
12366 if (user_breakpoint_p (b))
12367 delete_breakpoint (b);
c906108c
SS
12368 }
12369 else
48649e1b 12370 map_breakpoint_numbers
b926417a 12371 (arg, [&] (breakpoint *br)
48649e1b 12372 {
b926417a 12373 iterate_over_related_breakpoints (br, delete_breakpoint);
48649e1b 12374 });
c906108c
SS
12375}
12376
c2f4122d
PA
12377/* Return true if all locations of B bound to PSPACE are pending. If
12378 PSPACE is NULL, all locations of all program spaces are
12379 considered. */
12380
0d381245 12381static int
c2f4122d 12382all_locations_are_pending (struct breakpoint *b, struct program_space *pspace)
fe3f5fa8 12383{
40cb8ca5 12384 for (bp_location *loc : b->locations ())
c2f4122d
PA
12385 if ((pspace == NULL
12386 || loc->pspace == pspace)
12387 && !loc->shlib_disabled
8645ff69 12388 && !loc->pspace->executing_startup)
0d381245
VP
12389 return 0;
12390 return 1;
fe3f5fa8
VP
12391}
12392
776592bf
DE
12393/* Subroutine of update_breakpoint_locations to simplify it.
12394 Return non-zero if multiple fns in list LOC have the same name.
12395 Null names are ignored. */
12396
12397static int
12398ambiguous_names_p (struct bp_location *loc)
12399{
12400 struct bp_location *l;
2698f5ea 12401 htab_up htab (htab_create_alloc (13, htab_hash_string, htab_eq_string, NULL,
c1fb9836 12402 xcalloc, xfree));
776592bf
DE
12403
12404 for (l = loc; l != NULL; l = l->next)
12405 {
12406 const char **slot;
23d6ee64 12407 const char *name = l->function_name.get ();
776592bf
DE
12408
12409 /* Allow for some names to be NULL, ignore them. */
12410 if (name == NULL)
12411 continue;
12412
c1fb9836 12413 slot = (const char **) htab_find_slot (htab.get (), (const void *) name,
776592bf 12414 INSERT);
4a64f543
MS
12415 /* NOTE: We can assume slot != NULL here because xcalloc never
12416 returns NULL. */
776592bf 12417 if (*slot != NULL)
c1fb9836 12418 return 1;
776592bf
DE
12419 *slot = name;
12420 }
12421
776592bf
DE
12422 return 0;
12423}
12424
0fb4aa4b
PA
12425/* When symbols change, it probably means the sources changed as well,
12426 and it might mean the static tracepoint markers are no longer at
12427 the same address or line numbers they used to be at last we
12428 checked. Losing your static tracepoints whenever you rebuild is
12429 undesirable. This function tries to resync/rematch gdb static
12430 tracepoints with the markers on the target, for static tracepoints
12431 that have not been set by marker id. Static tracepoint that have
12432 been set by marker id are reset by marker id in breakpoint_re_set.
12433 The heuristic is:
12434
12435 1) For a tracepoint set at a specific address, look for a marker at
12436 the old PC. If one is found there, assume to be the same marker.
12437 If the name / string id of the marker found is different from the
12438 previous known name, assume that means the user renamed the marker
12439 in the sources, and output a warning.
12440
12441 2) For a tracepoint set at a given line number, look for a marker
12442 at the new address of the old line number. If one is found there,
12443 assume to be the same marker. If the name / string id of the
12444 marker found is different from the previous known name, assume that
12445 means the user renamed the marker in the sources, and output a
12446 warning.
12447
12448 3) If a marker is no longer found at the same address or line, it
12449 may mean the marker no longer exists. But it may also just mean
12450 the code changed a bit. Maybe the user added a few lines of code
12451 that made the marker move up or down (in line number terms). Ask
12452 the target for info about the marker with the string id as we knew
12453 it. If found, update line number and address in the matching
12454 static tracepoint. This will get confused if there's more than one
12455 marker with the same ID (possible in UST, although unadvised
12456 precisely because it confuses tools). */
12457
12458static struct symtab_and_line
12459update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
12460{
d9b3f62e 12461 struct tracepoint *tp = (struct tracepoint *) b;
0fb4aa4b
PA
12462 struct static_tracepoint_marker marker;
12463 CORE_ADDR pc;
0fb4aa4b
PA
12464
12465 pc = sal.pc;
12466 if (sal.line)
12467 find_line_pc (sal.symtab, sal.line, &pc);
12468
12469 if (target_static_tracepoint_marker_at (pc, &marker))
12470 {
5d9310c4 12471 if (tp->static_trace_marker_id != marker.str_id)
0fb4aa4b 12472 warning (_("static tracepoint %d changed probed marker from %s to %s"),
5d9310c4
SM
12473 b->number, tp->static_trace_marker_id.c_str (),
12474 marker.str_id.c_str ());
0fb4aa4b 12475
5d9310c4 12476 tp->static_trace_marker_id = std::move (marker.str_id);
0fb4aa4b
PA
12477
12478 return sal;
12479 }
12480
12481 /* Old marker wasn't found on target at lineno. Try looking it up
12482 by string ID. */
12483 if (!sal.explicit_pc
12484 && sal.line != 0
12485 && sal.symtab != NULL
5d9310c4 12486 && !tp->static_trace_marker_id.empty ())
0fb4aa4b 12487 {
5d9310c4
SM
12488 std::vector<static_tracepoint_marker> markers
12489 = target_static_tracepoint_markers_by_strid
12490 (tp->static_trace_marker_id.c_str ());
0fb4aa4b 12491
5d9310c4 12492 if (!markers.empty ())
0fb4aa4b 12493 {
0fb4aa4b 12494 struct symbol *sym;
80e1d417 12495 struct static_tracepoint_marker *tpmarker;
79a45e25 12496 struct ui_out *uiout = current_uiout;
67994074 12497 struct explicit_location explicit_loc;
0fb4aa4b 12498
5d9310c4 12499 tpmarker = &markers[0];
0fb4aa4b 12500
5d9310c4 12501 tp->static_trace_marker_id = std::move (tpmarker->str_id);
0fb4aa4b
PA
12502
12503 warning (_("marker for static tracepoint %d (%s) not "
12504 "found at previous line number"),
5d9310c4 12505 b->number, tp->static_trace_marker_id.c_str ());
0fb4aa4b 12506
51abb421 12507 symtab_and_line sal2 = find_pc_line (tpmarker->address, 0);
80e1d417 12508 sym = find_pc_sect_function (tpmarker->address, NULL);
112e8700 12509 uiout->text ("Now in ");
0fb4aa4b
PA
12510 if (sym)
12511 {
987012b8 12512 uiout->field_string ("func", sym->print_name (),
e43b10e1 12513 function_name_style.style ());
112e8700 12514 uiout->text (" at ");
0fb4aa4b 12515 }
112e8700 12516 uiout->field_string ("file",
cbe56571 12517 symtab_to_filename_for_display (sal2.symtab),
e43b10e1 12518 file_name_style.style ());
112e8700 12519 uiout->text (":");
0fb4aa4b 12520
112e8700 12521 if (uiout->is_mi_like_p ())
0fb4aa4b 12522 {
0b0865da 12523 const char *fullname = symtab_to_fullname (sal2.symtab);
0fb4aa4b 12524
112e8700 12525 uiout->field_string ("fullname", fullname);
0fb4aa4b
PA
12526 }
12527
381befee 12528 uiout->field_signed ("line", sal2.line);
112e8700 12529 uiout->text ("\n");
0fb4aa4b 12530
80e1d417 12531 b->loc->line_number = sal2.line;
2f202fde 12532 b->loc->symtab = sym != NULL ? sal2.symtab : NULL;
0fb4aa4b 12533
d28cd78a 12534 b->location.reset (NULL);
67994074
KS
12535 initialize_explicit_location (&explicit_loc);
12536 explicit_loc.source_filename
00e52e53 12537 = ASTRDUP (symtab_to_filename_for_display (sal2.symtab));
67994074
KS
12538 explicit_loc.line_offset.offset = b->loc->line_number;
12539 explicit_loc.line_offset.sign = LINE_OFFSET_NONE;
d28cd78a 12540 b->location = new_explicit_location (&explicit_loc);
0fb4aa4b
PA
12541
12542 /* Might be nice to check if function changed, and warn if
12543 so. */
0fb4aa4b
PA
12544 }
12545 }
12546 return sal;
12547}
12548
8d3788bd
VP
12549/* Returns 1 iff locations A and B are sufficiently same that
12550 we don't need to report breakpoint as changed. */
12551
12552static int
12553locations_are_equal (struct bp_location *a, struct bp_location *b)
12554{
12555 while (a && b)
12556 {
12557 if (a->address != b->address)
12558 return 0;
12559
12560 if (a->shlib_disabled != b->shlib_disabled)
12561 return 0;
12562
12563 if (a->enabled != b->enabled)
12564 return 0;
12565
b5fa468f
TBA
12566 if (a->disabled_by_cond != b->disabled_by_cond)
12567 return 0;
12568
8d3788bd
VP
12569 a = a->next;
12570 b = b->next;
12571 }
12572
12573 if ((a == NULL) != (b == NULL))
12574 return 0;
12575
12576 return 1;
12577}
12578
c2f4122d
PA
12579/* Split all locations of B that are bound to PSPACE out of B's
12580 location list to a separate list and return that list's head. If
12581 PSPACE is NULL, hoist out all locations of B. */
12582
12583static struct bp_location *
12584hoist_existing_locations (struct breakpoint *b, struct program_space *pspace)
12585{
12586 struct bp_location head;
12587 struct bp_location *i = b->loc;
12588 struct bp_location **i_link = &b->loc;
12589 struct bp_location *hoisted = &head;
12590
12591 if (pspace == NULL)
12592 {
12593 i = b->loc;
12594 b->loc = NULL;
12595 return i;
12596 }
12597
12598 head.next = NULL;
12599
12600 while (i != NULL)
12601 {
12602 if (i->pspace == pspace)
12603 {
12604 *i_link = i->next;
12605 i->next = NULL;
12606 hoisted->next = i;
12607 hoisted = i;
12608 }
12609 else
12610 i_link = &i->next;
12611 i = *i_link;
12612 }
12613
12614 return head.next;
12615}
12616
12617/* Create new breakpoint locations for B (a hardware or software
12618 breakpoint) based on SALS and SALS_END. If SALS_END.NELTS is not
12619 zero, then B is a ranged breakpoint. Only recreates locations for
12620 FILTER_PSPACE. Locations of other program spaces are left
12621 untouched. */
f1310107 12622
0e30163f 12623void
0d381245 12624update_breakpoint_locations (struct breakpoint *b,
c2f4122d 12625 struct program_space *filter_pspace,
6c5b2ebe
PA
12626 gdb::array_view<const symtab_and_line> sals,
12627 gdb::array_view<const symtab_and_line> sals_end)
fe3f5fa8 12628{
c2f4122d 12629 struct bp_location *existing_locations;
0d381245 12630
6c5b2ebe 12631 if (!sals_end.empty () && (sals.size () != 1 || sals_end.size () != 1))
f8eba3c6
TT
12632 {
12633 /* Ranged breakpoints have only one start location and one end
12634 location. */
12635 b->enable_state = bp_disabled;
6cb06a8c
TT
12636 gdb_printf (gdb_stderr,
12637 _("Could not reset ranged breakpoint %d: "
12638 "multiple locations found\n"),
12639 b->number);
f8eba3c6
TT
12640 return;
12641 }
f1310107 12642
4a64f543
MS
12643 /* If there's no new locations, and all existing locations are
12644 pending, don't do anything. This optimizes the common case where
12645 all locations are in the same shared library, that was unloaded.
12646 We'd like to retain the location, so that when the library is
12647 loaded again, we don't loose the enabled/disabled status of the
12648 individual locations. */
6c5b2ebe 12649 if (all_locations_are_pending (b, filter_pspace) && sals.empty ())
fe3f5fa8
VP
12650 return;
12651
c2f4122d 12652 existing_locations = hoist_existing_locations (b, filter_pspace);
fe3f5fa8 12653
6c5b2ebe 12654 for (const auto &sal : sals)
fe3f5fa8 12655 {
f8eba3c6
TT
12656 struct bp_location *new_loc;
12657
6c5b2ebe 12658 switch_to_program_space_and_thread (sal.pspace);
f8eba3c6 12659
06615628 12660 new_loc = b->add_location (sal);
fe3f5fa8 12661
0d381245
VP
12662 /* Reparse conditions, they might contain references to the
12663 old symtab. */
12664 if (b->cond_string != NULL)
12665 {
bbc13ae3 12666 const char *s;
fe3f5fa8 12667
6f781ee3 12668 s = b->cond_string.get ();
a70b8144 12669 try
0d381245 12670 {
6c5b2ebe
PA
12671 new_loc->cond = parse_exp_1 (&s, sal.pc,
12672 block_for_pc (sal.pc),
0d381245
VP
12673 0);
12674 }
230d2906 12675 catch (const gdb_exception_error &e)
0d381245 12676 {
b5fa468f 12677 new_loc->disabled_by_cond = true;
0d381245
VP
12678 }
12679 }
fe3f5fa8 12680
6c5b2ebe 12681 if (!sals_end.empty ())
f1310107 12682 {
6c5b2ebe 12683 CORE_ADDR end = find_breakpoint_range_end (sals_end[0]);
f1310107 12684
6c5b2ebe 12685 new_loc->length = end - sals[0].pc + 1;
f1310107 12686 }
0d381245 12687 }
fe3f5fa8 12688
4a64f543
MS
12689 /* If possible, carry over 'disable' status from existing
12690 breakpoints. */
0d381245
VP
12691 {
12692 struct bp_location *e = existing_locations;
776592bf
DE
12693 /* If there are multiple breakpoints with the same function name,
12694 e.g. for inline functions, comparing function names won't work.
12695 Instead compare pc addresses; this is just a heuristic as things
12696 may have moved, but in practice it gives the correct answer
12697 often enough until a better solution is found. */
12698 int have_ambiguous_names = ambiguous_names_p (b->loc);
12699
0d381245
VP
12700 for (; e; e = e->next)
12701 {
b5fa468f 12702 if ((!e->enabled || e->disabled_by_cond) && e->function_name)
0d381245 12703 {
776592bf
DE
12704 if (have_ambiguous_names)
12705 {
40cb8ca5 12706 for (bp_location *l : b->locations ())
7f32a4d5
PA
12707 {
12708 /* Ignore software vs hardware location type at
12709 this point, because with "set breakpoint
12710 auto-hw", after a re-set, locations that were
12711 hardware can end up as software, or vice versa.
12712 As mentioned above, this is an heuristic and in
12713 practice should give the correct answer often
12714 enough. */
12715 if (breakpoint_locations_match (e, l, true))
12716 {
b5fa468f
TBA
12717 l->enabled = e->enabled;
12718 l->disabled_by_cond = e->disabled_by_cond;
7f32a4d5
PA
12719 break;
12720 }
12721 }
776592bf
DE
12722 }
12723 else
12724 {
40cb8ca5 12725 for (bp_location *l : b->locations ())
776592bf 12726 if (l->function_name
23d6ee64
TT
12727 && strcmp (e->function_name.get (),
12728 l->function_name.get ()) == 0)
776592bf 12729 {
b5fa468f
TBA
12730 l->enabled = e->enabled;
12731 l->disabled_by_cond = e->disabled_by_cond;
776592bf
DE
12732 break;
12733 }
12734 }
0d381245
VP
12735 }
12736 }
12737 }
fe3f5fa8 12738
8d3788bd 12739 if (!locations_are_equal (existing_locations, b->loc))
76727919 12740 gdb::observers::breakpoint_modified.notify (b);
fe3f5fa8
VP
12741}
12742
f00aae0f 12743/* Find the SaL locations corresponding to the given LOCATION.
ef23e705
TJB
12744 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
12745
6c5b2ebe 12746static std::vector<symtab_and_line>
f00aae0f 12747location_to_sals (struct breakpoint *b, struct event_location *location,
c2f4122d 12748 struct program_space *search_pspace, int *found)
ef23e705 12749{
cc06b668 12750 struct gdb_exception exception;
ef23e705 12751
6c5b2ebe
PA
12752 std::vector<symtab_and_line> sals;
12753
a70b8144 12754 try
ef23e705 12755 {
c47614fe 12756 sals = b->decode_location (location, search_pspace);
ef23e705 12757 }
94aeb44b 12758 catch (gdb_exception_error &e)
ef23e705
TJB
12759 {
12760 int not_found_and_ok = 0;
492d29ea 12761
ef23e705
TJB
12762 /* For pending breakpoints, it's expected that parsing will
12763 fail until the right shared library is loaded. User has
12764 already told to create pending breakpoints and don't need
12765 extra messages. If breakpoint is in bp_shlib_disabled
12766 state, then user already saw the message about that
12767 breakpoint being disabled, and don't want to see more
12768 errors. */
58438ac1 12769 if (e.error == NOT_FOUND_ERROR
c2f4122d
PA
12770 && (b->condition_not_parsed
12771 || (b->loc != NULL
12772 && search_pspace != NULL
12773 && b->loc->pspace != search_pspace)
ef23e705 12774 || (b->loc && b->loc->shlib_disabled)
f8eba3c6 12775 || (b->loc && b->loc->pspace->executing_startup)
ef23e705
TJB
12776 || b->enable_state == bp_disabled))
12777 not_found_and_ok = 1;
12778
12779 if (!not_found_and_ok)
12780 {
12781 /* We surely don't want to warn about the same breakpoint
12782 10 times. One solution, implemented here, is disable
12783 the breakpoint on error. Another solution would be to
12784 have separate 'warning emitted' flag. Since this
12785 happens only when a binary has changed, I don't know
12786 which approach is better. */
12787 b->enable_state = bp_disabled;
eedc3f4f 12788 throw;
ef23e705 12789 }
94aeb44b
TT
12790
12791 exception = std::move (e);
ef23e705
TJB
12792 }
12793
492d29ea 12794 if (exception.reason == 0 || exception.error != NOT_FOUND_ERROR)
ef23e705 12795 {
6c5b2ebe
PA
12796 for (auto &sal : sals)
12797 resolve_sal_pc (&sal);
f00aae0f 12798 if (b->condition_not_parsed && b->extra_string != NULL)
ef23e705 12799 {
6f781ee3 12800 gdb::unique_xmalloc_ptr<char> cond_string, extra_string;
ed1d1739 12801 int thread, task;
ef23e705 12802
6f781ee3 12803 find_condition_and_thread_for_sals (sals, b->extra_string.get (),
b5fa468f
TBA
12804 &cond_string, &thread,
12805 &task, &extra_string);
f00aae0f 12806 gdb_assert (b->cond_string == NULL);
ef23e705 12807 if (cond_string)
6f781ee3 12808 b->cond_string = std::move (cond_string);
ef23e705
TJB
12809 b->thread = thread;
12810 b->task = task;
e7e0cddf 12811 if (extra_string)
6f781ee3 12812 b->extra_string = std::move (extra_string);
ef23e705
TJB
12813 b->condition_not_parsed = 0;
12814 }
12815
7b572efb 12816 if (b->type == bp_static_tracepoint)
6c5b2ebe 12817 sals[0] = update_static_tracepoint (b, sals[0]);
ef23e705 12818
58438ac1
TT
12819 *found = 1;
12820 }
12821 else
12822 *found = 0;
ef23e705
TJB
12823
12824 return sals;
12825}
12826
348d480f
PA
12827/* The default re_set method, for typical hardware or software
12828 breakpoints. Reevaluate the breakpoint and recreate its
12829 locations. */
12830
12831static void
28010a5d 12832breakpoint_re_set_default (struct breakpoint *b)
ef23e705 12833{
c2f4122d 12834 struct program_space *filter_pspace = current_program_space;
6c5b2ebe 12835 std::vector<symtab_and_line> expanded, expanded_end;
ef23e705 12836
6c5b2ebe
PA
12837 int found;
12838 std::vector<symtab_and_line> sals = location_to_sals (b, b->location.get (),
12839 filter_pspace, &found);
ef23e705 12840 if (found)
6c5b2ebe 12841 expanded = std::move (sals);
ef23e705 12842
f00aae0f 12843 if (b->location_range_end != NULL)
f1310107 12844 {
6c5b2ebe
PA
12845 std::vector<symtab_and_line> sals_end
12846 = location_to_sals (b, b->location_range_end.get (),
12847 filter_pspace, &found);
f1310107 12848 if (found)
6c5b2ebe 12849 expanded_end = std::move (sals_end);
f1310107
TJB
12850 }
12851
c2f4122d 12852 update_breakpoint_locations (b, filter_pspace, expanded, expanded_end);
28010a5d
PA
12853}
12854
983af33b
SDJ
12855/* Default method for creating SALs from an address string. It basically
12856 calls parse_breakpoint_sals. Return 1 for success, zero for failure. */
12857
12858static void
626d2320 12859create_sals_from_location_default (struct event_location *location,
03ada39e 12860 struct linespec_result *canonical)
983af33b 12861{
f00aae0f 12862 parse_breakpoint_sals (location, canonical);
983af33b
SDJ
12863}
12864
983af33b 12865/* Decode the line represented by S by calling decode_line_full. This is the
5f700d83 12866 default function for the `decode_location' method of breakpoint_ops. */
983af33b 12867
6c5b2ebe 12868static std::vector<symtab_and_line>
f00aae0f 12869decode_location_default (struct breakpoint *b,
626d2320 12870 struct event_location *location,
6c5b2ebe 12871 struct program_space *search_pspace)
983af33b
SDJ
12872{
12873 struct linespec_result canonical;
12874
c2f4122d 12875 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, search_pspace,
cafb3438 12876 NULL, 0, &canonical, multiple_symbols_all,
c0e8dcd8 12877 b->filter.get ());
983af33b
SDJ
12878
12879 /* We should get 0 or 1 resulting SALs. */
6c5b2ebe 12880 gdb_assert (canonical.lsals.size () < 2);
983af33b 12881
6c5b2ebe 12882 if (!canonical.lsals.empty ())
983af33b 12883 {
6c5b2ebe
PA
12884 const linespec_sals &lsal = canonical.lsals[0];
12885 return std::move (lsal.sals);
983af33b 12886 }
6c5b2ebe 12887 return {};
983af33b
SDJ
12888}
12889
bf469271 12890/* Reset a breakpoint. */
c906108c 12891
bf469271
PA
12892static void
12893breakpoint_re_set_one (breakpoint *b)
c906108c 12894{
fdf44873
TT
12895 input_radix = b->input_radix;
12896 set_language (b->language);
c906108c 12897
c47614fe 12898 b->re_set ();
c906108c
SS
12899}
12900
c2f4122d
PA
12901/* Re-set breakpoint locations for the current program space.
12902 Locations bound to other program spaces are left untouched. */
12903
c906108c 12904void
69de3c6a 12905breakpoint_re_set (void)
c906108c 12906{
c5aa993b 12907 {
fdf44873
TT
12908 scoped_restore_current_language save_language;
12909 scoped_restore save_input_radix = make_scoped_restore (&input_radix);
5ed8105e 12910 scoped_restore_current_pspace_and_thread restore_pspace_thread;
e62c965a 12911
8e817061
JB
12912 /* breakpoint_re_set_one sets the current_language to the language
12913 of the breakpoint it is resetting (see prepare_re_set_context)
12914 before re-evaluating the breakpoint's location. This change can
12915 unfortunately get undone by accident if the language_mode is set
12916 to auto, and we either switch frames, or more likely in this context,
12917 we select the current frame.
12918
12919 We prevent this by temporarily turning the language_mode to
12920 language_mode_manual. We restore it once all breakpoints
12921 have been reset. */
12922 scoped_restore save_language_mode = make_scoped_restore (&language_mode);
12923 language_mode = language_mode_manual;
12924
5ed8105e
PA
12925 /* Note: we must not try to insert locations until after all
12926 breakpoints have been re-set. Otherwise, e.g., when re-setting
12927 breakpoint 1, we'd insert the locations of breakpoint 2, which
12928 hadn't been re-set yet, and thus may have stale locations. */
4efc6507 12929
1428b37a 12930 for (breakpoint *b : all_breakpoints_safe ())
5ed8105e 12931 {
a70b8144 12932 try
bf469271
PA
12933 {
12934 breakpoint_re_set_one (b);
12935 }
230d2906 12936 catch (const gdb_exception &ex)
bf469271
PA
12937 {
12938 exception_fprintf (gdb_stderr, ex,
12939 "Error in re-setting breakpoint %d: ",
12940 b->number);
12941 }
5ed8105e 12942 }
5ed8105e
PA
12943
12944 jit_breakpoint_re_set ();
12945 }
6c95b8df 12946
af02033e
PP
12947 create_overlay_event_breakpoint ();
12948 create_longjmp_master_breakpoint ();
12949 create_std_terminate_master_breakpoint ();
186c406b 12950 create_exception_master_breakpoint ();
2a7f3dff
PA
12951
12952 /* Now we can insert. */
12953 update_global_location_list (UGLL_MAY_INSERT);
c906108c
SS
12954}
12955\f
c906108c
SS
12956/* Reset the thread number of this breakpoint:
12957
12958 - If the breakpoint is for all threads, leave it as-is.
4a64f543 12959 - Else, reset it to the current thread for inferior_ptid. */
c906108c 12960void
fba45db2 12961breakpoint_re_set_thread (struct breakpoint *b)
c906108c
SS
12962{
12963 if (b->thread != -1)
12964 {
00431a78 12965 b->thread = inferior_thread ()->global_num;
6c95b8df
PA
12966
12967 /* We're being called after following a fork. The new fork is
12968 selected as current, and unless this was a vfork will have a
12969 different program space from the original thread. Reset that
12970 as well. */
12971 b->loc->pspace = current_program_space;
c906108c
SS
12972 }
12973}
12974
03ac34d5
MS
12975/* Set ignore-count of breakpoint number BPTNUM to COUNT.
12976 If from_tty is nonzero, it prints a message to that effect,
12977 which ends with a period (no newline). */
12978
c906108c 12979void
fba45db2 12980set_ignore_count (int bptnum, int count, int from_tty)
c906108c 12981{
c906108c
SS
12982 if (count < 0)
12983 count = 0;
12984
43892fdf 12985 for (breakpoint *b : all_breakpoints ())
c906108c 12986 if (b->number == bptnum)
01add95b
SM
12987 {
12988 if (is_tracepoint (b))
12989 {
12990 if (from_tty && count != 0)
6cb06a8c
TT
12991 gdb_printf (_("Ignore count ignored for tracepoint %d."),
12992 bptnum);
01add95b
SM
12993 return;
12994 }
12995
12996 b->ignore_count = count;
12997 if (from_tty)
12998 {
12999 if (count == 0)
6cb06a8c
TT
13000 gdb_printf (_("Will stop next time "
13001 "breakpoint %d is reached."),
13002 bptnum);
01add95b 13003 else if (count == 1)
6cb06a8c
TT
13004 gdb_printf (_("Will ignore next crossing of breakpoint %d."),
13005 bptnum);
01add95b 13006 else
6cb06a8c
TT
13007 gdb_printf (_("Will ignore next %d "
13008 "crossings of breakpoint %d."),
13009 count, bptnum);
01add95b
SM
13010 }
13011 gdb::observers::breakpoint_modified.notify (b);
13012 return;
13013 }
c906108c 13014
8a3fe4f8 13015 error (_("No breakpoint number %d."), bptnum);
c906108c
SS
13016}
13017
c906108c
SS
13018/* Command to set ignore-count of breakpoint N to COUNT. */
13019
13020static void
0b39b52e 13021ignore_command (const char *args, int from_tty)
c906108c 13022{
0b39b52e 13023 const char *p = args;
52f0bd74 13024 int num;
c906108c
SS
13025
13026 if (p == 0)
e2e0b3e5 13027 error_no_arg (_("a breakpoint number"));
c5aa993b 13028
c906108c 13029 num = get_number (&p);
5c44784c 13030 if (num == 0)
8a3fe4f8 13031 error (_("bad breakpoint number: '%s'"), args);
c906108c 13032 if (*p == 0)
8a3fe4f8 13033 error (_("Second argument (specified ignore-count) is missing."));
c906108c
SS
13034
13035 set_ignore_count (num,
13036 longest_to_int (value_as_long (parse_and_eval (p))),
13037 from_tty);
221ea385 13038 if (from_tty)
6cb06a8c 13039 gdb_printf ("\n");
c906108c
SS
13040}
13041\f
d0fe4701
XR
13042
13043/* Call FUNCTION on each of the breakpoints with numbers in the range
13044 defined by BP_NUM_RANGE (an inclusive range). */
c906108c
SS
13045
13046static void
d0fe4701
XR
13047map_breakpoint_number_range (std::pair<int, int> bp_num_range,
13048 gdb::function_view<void (breakpoint *)> function)
c906108c 13049{
d0fe4701
XR
13050 if (bp_num_range.first == 0)
13051 {
13052 warning (_("bad breakpoint number at or near '%d'"),
13053 bp_num_range.first);
13054 }
13055 else
c906108c 13056 {
d0fe4701 13057 for (int i = bp_num_range.first; i <= bp_num_range.second; i++)
5c44784c 13058 {
d0fe4701
XR
13059 bool match = false;
13060
1428b37a 13061 for (breakpoint *b : all_breakpoints_safe ())
d0fe4701 13062 if (b->number == i)
5c44784c 13063 {
bfd28288 13064 match = true;
48649e1b 13065 function (b);
11cf8741 13066 break;
5c44784c 13067 }
bfd28288 13068 if (!match)
6cb06a8c 13069 gdb_printf (_("No breakpoint number %d.\n"), i);
c5aa993b 13070 }
c906108c
SS
13071 }
13072}
13073
d0fe4701
XR
13074/* Call FUNCTION on each of the breakpoints whose numbers are given in
13075 ARGS. */
13076
13077static void
13078map_breakpoint_numbers (const char *args,
13079 gdb::function_view<void (breakpoint *)> function)
13080{
13081 if (args == NULL || *args == '\0')
13082 error_no_arg (_("one or more breakpoint numbers"));
13083
13084 number_or_range_parser parser (args);
13085
13086 while (!parser.finished ())
13087 {
13088 int num = parser.get_number ();
13089 map_breakpoint_number_range (std::make_pair (num, num), function);
13090 }
13091}
13092
13093/* Return the breakpoint location structure corresponding to the
13094 BP_NUM and LOC_NUM values. */
13095
0d381245 13096static struct bp_location *
d0fe4701 13097find_location_by_number (int bp_num, int loc_num)
0d381245 13098{
43892fdf 13099 breakpoint *b = get_breakpoint (bp_num);
0d381245
VP
13100
13101 if (!b || b->number != bp_num)
d0fe4701 13102 error (_("Bad breakpoint number '%d'"), bp_num);
0d381245 13103
0d381245 13104 if (loc_num == 0)
d0fe4701 13105 error (_("Bad breakpoint location number '%d'"), loc_num);
0d381245 13106
d0fe4701 13107 int n = 0;
40cb8ca5 13108 for (bp_location *loc : b->locations ())
d0fe4701
XR
13109 if (++n == loc_num)
13110 return loc;
13111
13112 error (_("Bad breakpoint location number '%d'"), loc_num);
0d381245
VP
13113}
13114
95e95a6d
PA
13115/* Modes of operation for extract_bp_num. */
13116enum class extract_bp_kind
13117{
13118 /* Extracting a breakpoint number. */
13119 bp,
13120
13121 /* Extracting a location number. */
13122 loc,
13123};
13124
13125/* Extract a breakpoint or location number (as determined by KIND)
13126 from the string starting at START. TRAILER is a character which
13127 can be found after the number. If you don't want a trailer, use
13128 '\0'. If END_OUT is not NULL, it is set to point after the parsed
13129 string. This always returns a positive integer. */
13130
13131static int
13132extract_bp_num (extract_bp_kind kind, const char *start,
13133 int trailer, const char **end_out = NULL)
13134{
13135 const char *end = start;
13136 int num = get_number_trailer (&end, trailer);
13137 if (num < 0)
13138 error (kind == extract_bp_kind::bp
13139 ? _("Negative breakpoint number '%.*s'")
13140 : _("Negative breakpoint location number '%.*s'"),
13141 int (end - start), start);
13142 if (num == 0)
13143 error (kind == extract_bp_kind::bp
13144 ? _("Bad breakpoint number '%.*s'")
13145 : _("Bad breakpoint location number '%.*s'"),
13146 int (end - start), start);
13147
13148 if (end_out != NULL)
13149 *end_out = end;
13150 return num;
13151}
13152
13153/* Extract a breakpoint or location range (as determined by KIND) in
13154 the form NUM1-NUM2 stored at &ARG[arg_offset]. Returns a std::pair
13155 representing the (inclusive) range. The returned pair's elements
13156 are always positive integers. */
13157
13158static std::pair<int, int>
13159extract_bp_or_bp_range (extract_bp_kind kind,
13160 const std::string &arg,
13161 std::string::size_type arg_offset)
13162{
13163 std::pair<int, int> range;
13164 const char *bp_loc = &arg[arg_offset];
13165 std::string::size_type dash = arg.find ('-', arg_offset);
13166 if (dash != std::string::npos)
13167 {
13168 /* bp_loc is a range (x-z). */
13169 if (arg.length () == dash + 1)
13170 error (kind == extract_bp_kind::bp
13171 ? _("Bad breakpoint number at or near: '%s'")
13172 : _("Bad breakpoint location number at or near: '%s'"),
13173 bp_loc);
13174
13175 const char *end;
13176 const char *start_first = bp_loc;
13177 const char *start_second = &arg[dash + 1];
13178 range.first = extract_bp_num (kind, start_first, '-');
13179 range.second = extract_bp_num (kind, start_second, '\0', &end);
13180
13181 if (range.first > range.second)
13182 error (kind == extract_bp_kind::bp
13183 ? _("Inverted breakpoint range at '%.*s'")
13184 : _("Inverted breakpoint location range at '%.*s'"),
13185 int (end - start_first), start_first);
13186 }
13187 else
13188 {
13189 /* bp_loc is a single value. */
13190 range.first = extract_bp_num (kind, bp_loc, '\0');
13191 range.second = range.first;
13192 }
13193 return range;
13194}
13195
d0fe4701
XR
13196/* Extract the breakpoint/location range specified by ARG. Returns
13197 the breakpoint range in BP_NUM_RANGE, and the location range in
13198 BP_LOC_RANGE.
13199
13200 ARG may be in any of the following forms:
13201
13202 x where 'x' is a breakpoint number.
13203 x-y where 'x' and 'y' specify a breakpoint numbers range.
13204 x.y where 'x' is a breakpoint number and 'y' a location number.
13205 x.y-z where 'x' is a breakpoint number and 'y' and 'z' specify a
13206 location number range.
13207*/
13208
cc638e86 13209static void
d0fe4701
XR
13210extract_bp_number_and_location (const std::string &arg,
13211 std::pair<int, int> &bp_num_range,
13212 std::pair<int, int> &bp_loc_range)
13213{
13214 std::string::size_type dot = arg.find ('.');
13215
13216 if (dot != std::string::npos)
13217 {
13218 /* Handle 'x.y' and 'x.y-z' cases. */
13219
13220 if (arg.length () == dot + 1 || dot == 0)
95e95a6d 13221 error (_("Bad breakpoint number at or near: '%s'"), arg.c_str ());
d0fe4701 13222
95e95a6d
PA
13223 bp_num_range.first
13224 = extract_bp_num (extract_bp_kind::bp, arg.c_str (), '.');
13225 bp_num_range.second = bp_num_range.first;
d0fe4701 13226
95e95a6d
PA
13227 bp_loc_range = extract_bp_or_bp_range (extract_bp_kind::loc,
13228 arg, dot + 1);
d0fe4701
XR
13229 }
13230 else
13231 {
13232 /* Handle x and x-y cases. */
d0fe4701 13233
95e95a6d 13234 bp_num_range = extract_bp_or_bp_range (extract_bp_kind::bp, arg, 0);
d0fe4701
XR
13235 bp_loc_range.first = 0;
13236 bp_loc_range.second = 0;
13237 }
d0fe4701
XR
13238}
13239
13240/* Enable or disable a breakpoint location BP_NUM.LOC_NUM. ENABLE
13241 specifies whether to enable or disable. */
13242
13243static void
13244enable_disable_bp_num_loc (int bp_num, int loc_num, bool enable)
13245{
13246 struct bp_location *loc = find_location_by_number (bp_num, loc_num);
13247 if (loc != NULL)
13248 {
b5fa468f
TBA
13249 if (loc->disabled_by_cond && enable)
13250 error (_("Breakpoint %d's condition is invalid at location %d, "
13251 "cannot enable."), bp_num, loc_num);
13252
d0fe4701
XR
13253 if (loc->enabled != enable)
13254 {
13255 loc->enabled = enable;
13256 mark_breakpoint_location_modified (loc);
13257 }
13258 if (target_supports_enable_disable_tracepoint ()
13259 && current_trace_status ()->running && loc->owner
13260 && is_tracepoint (loc->owner))
13261 target_disable_tracepoint (loc);
13262 }
13263 update_global_location_list (UGLL_DONT_INSERT);
d7154a8d
JV
13264
13265 gdb::observers::breakpoint_modified.notify (loc->owner);
d0fe4701
XR
13266}
13267
13268/* Enable or disable a range of breakpoint locations. BP_NUM is the
13269 number of the breakpoint, and BP_LOC_RANGE specifies the
13270 (inclusive) range of location numbers of that breakpoint to
13271 enable/disable. ENABLE specifies whether to enable or disable the
13272 location. */
13273
13274static void
13275enable_disable_breakpoint_location_range (int bp_num,
13276 std::pair<int, int> &bp_loc_range,
13277 bool enable)
13278{
13279 for (int i = bp_loc_range.first; i <= bp_loc_range.second; i++)
13280 enable_disable_bp_num_loc (bp_num, i, enable);
13281}
0d381245 13282
1900040c
MS
13283/* Set ignore-count of breakpoint number BPTNUM to COUNT.
13284 If from_tty is nonzero, it prints a message to that effect,
13285 which ends with a period (no newline). */
13286
c906108c 13287void
fba45db2 13288disable_breakpoint (struct breakpoint *bpt)
c906108c
SS
13289{
13290 /* Never disable a watchpoint scope breakpoint; we want to
13291 hit them when we leave scope so we can delete both the
13292 watchpoint and its scope breakpoint at that time. */
13293 if (bpt->type == bp_watchpoint_scope)
13294 return;
13295
b5de0fa7 13296 bpt->enable_state = bp_disabled;
c906108c 13297
b775012e
LM
13298 /* Mark breakpoint locations modified. */
13299 mark_breakpoint_modified (bpt);
13300
d248b706
KY
13301 if (target_supports_enable_disable_tracepoint ()
13302 && current_trace_status ()->running && is_tracepoint (bpt))
13303 {
40cb8ca5 13304 for (bp_location *location : bpt->locations ())
d248b706
KY
13305 target_disable_tracepoint (location);
13306 }
13307
44702360 13308 update_global_location_list (UGLL_DONT_INSERT);
c906108c 13309
76727919 13310 gdb::observers::breakpoint_modified.notify (bpt);
c906108c
SS
13311}
13312
d0fe4701
XR
13313/* Enable or disable the breakpoint(s) or breakpoint location(s)
13314 specified in ARGS. ARGS may be in any of the formats handled by
13315 extract_bp_number_and_location. ENABLE specifies whether to enable
13316 or disable the breakpoints/locations. */
13317
c906108c 13318static void
d0fe4701 13319enable_disable_command (const char *args, int from_tty, bool enable)
c906108c 13320{
c906108c 13321 if (args == 0)
46c6471b 13322 {
43892fdf 13323 for (breakpoint *bpt : all_breakpoints ())
46c6471b 13324 if (user_breakpoint_p (bpt))
d0fe4701
XR
13325 {
13326 if (enable)
13327 enable_breakpoint (bpt);
13328 else
13329 disable_breakpoint (bpt);
13330 }
46c6471b 13331 }
9eaabc75 13332 else
0d381245 13333 {
cb791d59 13334 std::string num = extract_arg (&args);
9eaabc75 13335
cb791d59 13336 while (!num.empty ())
d248b706 13337 {
d0fe4701 13338 std::pair<int, int> bp_num_range, bp_loc_range;
9eaabc75 13339
cc638e86
PA
13340 extract_bp_number_and_location (num, bp_num_range, bp_loc_range);
13341
13342 if (bp_loc_range.first == bp_loc_range.second
13343 && bp_loc_range.first == 0)
d0fe4701 13344 {
cc638e86
PA
13345 /* Handle breakpoint ids with formats 'x' or 'x-z'. */
13346 map_breakpoint_number_range (bp_num_range,
13347 enable
13348 ? enable_breakpoint
13349 : disable_breakpoint);
13350 }
13351 else
13352 {
13353 /* Handle breakpoint ids with formats 'x.y' or
13354 'x.y-z'. */
13355 enable_disable_breakpoint_location_range
13356 (bp_num_range.first, bp_loc_range, enable);
b775012e 13357 }
9eaabc75 13358 num = extract_arg (&args);
d248b706 13359 }
0d381245 13360 }
c906108c
SS
13361}
13362
d0fe4701
XR
13363/* The disable command disables the specified breakpoints/locations
13364 (or all defined breakpoints) so they're no longer effective in
13365 stopping the inferior. ARGS may be in any of the forms defined in
13366 extract_bp_number_and_location. */
13367
13368static void
13369disable_command (const char *args, int from_tty)
13370{
13371 enable_disable_command (args, from_tty, false);
13372}
13373
c906108c 13374static void
816338b5
SS
13375enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
13376 int count)
c906108c 13377{
afe38095 13378 int target_resources_ok;
c906108c
SS
13379
13380 if (bpt->type == bp_hardware_breakpoint)
13381 {
13382 int i;
c5aa993b 13383 i = hw_breakpoint_used_count ();
53a5351d 13384 target_resources_ok =
d92524f1 13385 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
53a5351d 13386 i + 1, 0);
c906108c 13387 if (target_resources_ok == 0)
8a3fe4f8 13388 error (_("No hardware breakpoint support in the target."));
c906108c 13389 else if (target_resources_ok < 0)
8a3fe4f8 13390 error (_("Hardware breakpoints used exceeds limit."));
c906108c
SS
13391 }
13392
cc60f2e3 13393 if (is_watchpoint (bpt))
c906108c 13394 {
d07205c2 13395 /* Initialize it just to avoid a GCC false warning. */
f486487f 13396 enum enable_state orig_enable_state = bp_disabled;
dde02812 13397
a70b8144 13398 try
c906108c 13399 {
3a5c3e22
PA
13400 struct watchpoint *w = (struct watchpoint *) bpt;
13401
1e718ff1
TJB
13402 orig_enable_state = bpt->enable_state;
13403 bpt->enable_state = bp_enabled;
3a5c3e22 13404 update_watchpoint (w, 1 /* reparse */);
c906108c 13405 }
230d2906 13406 catch (const gdb_exception &e)
c5aa993b 13407 {
1e718ff1 13408 bpt->enable_state = orig_enable_state;
dde02812
ES
13409 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
13410 bpt->number);
13411 return;
c5aa993b 13412 }
c906108c 13413 }
0101ce28 13414
b775012e
LM
13415 bpt->enable_state = bp_enabled;
13416
13417 /* Mark breakpoint locations modified. */
13418 mark_breakpoint_modified (bpt);
13419
d248b706
KY
13420 if (target_supports_enable_disable_tracepoint ()
13421 && current_trace_status ()->running && is_tracepoint (bpt))
13422 {
40cb8ca5 13423 for (bp_location *location : bpt->locations ())
d248b706
KY
13424 target_enable_tracepoint (location);
13425 }
13426
b4c291bb 13427 bpt->disposition = disposition;
816338b5 13428 bpt->enable_count = count;
44702360 13429 update_global_location_list (UGLL_MAY_INSERT);
9c97429f 13430
76727919 13431 gdb::observers::breakpoint_modified.notify (bpt);
c906108c
SS
13432}
13433
fe3f5fa8 13434
c906108c 13435void
fba45db2 13436enable_breakpoint (struct breakpoint *bpt)
c906108c 13437{
816338b5 13438 enable_breakpoint_disp (bpt, bpt->disposition, 0);
51be5b68
PA
13439}
13440
d0fe4701
XR
13441/* The enable command enables the specified breakpoints/locations (or
13442 all defined breakpoints) so they once again become (or continue to
13443 be) effective in stopping the inferior. ARGS may be in any of the
13444 forms defined in extract_bp_number_and_location. */
c906108c 13445
c906108c 13446static void
981a3fb3 13447enable_command (const char *args, int from_tty)
c906108c 13448{
d0fe4701 13449 enable_disable_command (args, from_tty, true);
c906108c
SS
13450}
13451
c906108c 13452static void
4495129a 13453enable_once_command (const char *args, int from_tty)
c906108c 13454{
48649e1b
TT
13455 map_breakpoint_numbers
13456 (args, [&] (breakpoint *b)
13457 {
13458 iterate_over_related_breakpoints
13459 (b, [&] (breakpoint *bpt)
13460 {
13461 enable_breakpoint_disp (bpt, disp_disable, 1);
13462 });
13463 });
816338b5
SS
13464}
13465
13466static void
4495129a 13467enable_count_command (const char *args, int from_tty)
816338b5 13468{
b9d61307
SM
13469 int count;
13470
13471 if (args == NULL)
13472 error_no_arg (_("hit count"));
13473
13474 count = get_number (&args);
816338b5 13475
48649e1b
TT
13476 map_breakpoint_numbers
13477 (args, [&] (breakpoint *b)
13478 {
13479 iterate_over_related_breakpoints
13480 (b, [&] (breakpoint *bpt)
13481 {
13482 enable_breakpoint_disp (bpt, disp_disable, count);
13483 });
13484 });
c906108c
SS
13485}
13486
c906108c 13487static void
4495129a 13488enable_delete_command (const char *args, int from_tty)
c906108c 13489{
48649e1b
TT
13490 map_breakpoint_numbers
13491 (args, [&] (breakpoint *b)
13492 {
13493 iterate_over_related_breakpoints
13494 (b, [&] (breakpoint *bpt)
13495 {
13496 enable_breakpoint_disp (bpt, disp_del, 1);
13497 });
13498 });
c906108c
SS
13499}
13500\f
1f3b5d1b
PP
13501/* Invalidate last known value of any hardware watchpoint if
13502 the memory which that value represents has been written to by
13503 GDB itself. */
13504
13505static void
8de0566d
YQ
13506invalidate_bp_value_on_memory_change (struct inferior *inferior,
13507 CORE_ADDR addr, ssize_t len,
1f3b5d1b
PP
13508 const bfd_byte *data)
13509{
43892fdf 13510 for (breakpoint *bp : all_breakpoints ())
1f3b5d1b 13511 if (bp->enable_state == bp_enabled
3a5c3e22 13512 && bp->type == bp_hardware_watchpoint)
1f3b5d1b 13513 {
3a5c3e22 13514 struct watchpoint *wp = (struct watchpoint *) bp;
1f3b5d1b 13515
850645cf 13516 if (wp->val_valid && wp->val != nullptr)
3a5c3e22 13517 {
40cb8ca5 13518 for (bp_location *loc : bp->locations ())
3a5c3e22
PA
13519 if (loc->loc_type == bp_loc_hardware_watchpoint
13520 && loc->address + loc->length > addr
13521 && addr + len > loc->address)
13522 {
3a5c3e22 13523 wp->val = NULL;
4c1d86d9 13524 wp->val_valid = false;
3a5c3e22
PA
13525 }
13526 }
1f3b5d1b
PP
13527 }
13528}
13529
8181d85f
DJ
13530/* Create and insert a breakpoint for software single step. */
13531
13532void
6c95b8df 13533insert_single_step_breakpoint (struct gdbarch *gdbarch,
accd0bcd 13534 const address_space *aspace,
4a64f543 13535 CORE_ADDR next_pc)
8181d85f 13536{
7c16b83e
PA
13537 struct thread_info *tp = inferior_thread ();
13538 struct symtab_and_line sal;
13539 CORE_ADDR pc = next_pc;
8181d85f 13540
34b7e8a6
PA
13541 if (tp->control.single_step_breakpoints == NULL)
13542 {
13543 tp->control.single_step_breakpoints
5d5658a1 13544 = new_single_step_breakpoint (tp->global_num, gdbarch);
34b7e8a6 13545 }
8181d85f 13546
7c16b83e
PA
13547 sal = find_pc_line (pc, 0);
13548 sal.pc = pc;
13549 sal.section = find_pc_overlay (pc);
13550 sal.explicit_pc = 1;
06615628 13551 tp->control.single_step_breakpoints->add_location (sal);
8181d85f 13552
7c16b83e 13553 update_global_location_list (UGLL_INSERT);
8181d85f
DJ
13554}
13555
93f9a11f
YQ
13556/* Insert single step breakpoints according to the current state. */
13557
13558int
13559insert_single_step_breakpoints (struct gdbarch *gdbarch)
13560{
f5ea389a 13561 struct regcache *regcache = get_current_regcache ();
a0ff9e1a 13562 std::vector<CORE_ADDR> next_pcs;
93f9a11f 13563
f5ea389a 13564 next_pcs = gdbarch_software_single_step (gdbarch, regcache);
93f9a11f 13565
a0ff9e1a 13566 if (!next_pcs.empty ())
93f9a11f 13567 {
f5ea389a 13568 struct frame_info *frame = get_current_frame ();
8b86c959 13569 const address_space *aspace = get_frame_address_space (frame);
93f9a11f 13570
a0ff9e1a 13571 for (CORE_ADDR pc : next_pcs)
93f9a11f
YQ
13572 insert_single_step_breakpoint (gdbarch, aspace, pc);
13573
93f9a11f
YQ
13574 return 1;
13575 }
13576 else
13577 return 0;
13578}
13579
34b7e8a6 13580/* See breakpoint.h. */
f02253f1
HZ
13581
13582int
7c16b83e 13583breakpoint_has_location_inserted_here (struct breakpoint *bp,
accd0bcd 13584 const address_space *aspace,
7c16b83e 13585 CORE_ADDR pc)
1aafd4da 13586{
40cb8ca5 13587 for (bp_location *loc : bp->locations ())
7c16b83e
PA
13588 if (loc->inserted
13589 && breakpoint_location_address_match (loc, aspace, pc))
13590 return 1;
1aafd4da 13591
7c16b83e 13592 return 0;
ef370185
JB
13593}
13594
13595/* Check whether a software single-step breakpoint is inserted at
13596 PC. */
13597
13598int
accd0bcd 13599single_step_breakpoint_inserted_here_p (const address_space *aspace,
ef370185
JB
13600 CORE_ADDR pc)
13601{
43892fdf 13602 for (breakpoint *bpt : all_breakpoints ())
34b7e8a6
PA
13603 {
13604 if (bpt->type == bp_single_step
13605 && breakpoint_has_location_inserted_here (bpt, aspace, pc))
13606 return 1;
13607 }
13608 return 0;
1aafd4da
UW
13609}
13610
1042e4c0
SS
13611/* Tracepoint-specific operations. */
13612
13613/* Set tracepoint count to NUM. */
13614static void
13615set_tracepoint_count (int num)
13616{
13617 tracepoint_count = num;
4fa62494 13618 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
1042e4c0
SS
13619}
13620
70221824 13621static void
0b39b52e 13622trace_command (const char *arg, int from_tty)
1042e4c0 13623{
ffc2605c
TT
13624 event_location_up location = string_to_event_location (&arg,
13625 current_language);
bac7c5cf
GB
13626 const struct breakpoint_ops *ops = breakpoint_ops_for_event_location
13627 (location.get (), true /* is_tracepoint */);
55aa24fb 13628
558a9d82 13629 create_breakpoint (get_current_arch (),
ffc2605c 13630 location.get (),
10a636cc 13631 NULL, 0, arg, false, 1 /* parse arg */,
558a9d82
YQ
13632 0 /* tempflag */,
13633 bp_tracepoint /* type_wanted */,
13634 0 /* Ignore count */,
13635 pending_break_support,
13636 ops,
13637 from_tty,
13638 1 /* enabled */,
13639 0 /* internal */, 0);
1042e4c0
SS
13640}
13641
70221824 13642static void
0b39b52e 13643ftrace_command (const char *arg, int from_tty)
7a697b8d 13644{
ffc2605c
TT
13645 event_location_up location = string_to_event_location (&arg,
13646 current_language);
558a9d82 13647 create_breakpoint (get_current_arch (),
ffc2605c 13648 location.get (),
10a636cc 13649 NULL, 0, arg, false, 1 /* parse arg */,
558a9d82
YQ
13650 0 /* tempflag */,
13651 bp_fast_tracepoint /* type_wanted */,
13652 0 /* Ignore count */,
13653 pending_break_support,
a0cd5f08 13654 &base_breakpoint_ops,
558a9d82
YQ
13655 from_tty,
13656 1 /* enabled */,
13657 0 /* internal */, 0);
0fb4aa4b
PA
13658}
13659
13660/* strace command implementation. Creates a static tracepoint. */
13661
70221824 13662static void
0b39b52e 13663strace_command (const char *arg, int from_tty)
0fb4aa4b 13664{
a678887d 13665 const struct breakpoint_ops *ops;
ffc2605c 13666 event_location_up location;
7b572efb 13667 enum bptype type;
983af33b
SDJ
13668
13669 /* Decide if we are dealing with a static tracepoint marker (`-m'),
13670 or with a normal static tracepoint. */
61012eef 13671 if (arg && startswith (arg, "-m") && isspace (arg[2]))
f00aae0f
KS
13672 {
13673 ops = &strace_marker_breakpoint_ops;
a20714ff 13674 location = new_linespec_location (&arg, symbol_name_match_type::FULL);
7b572efb 13675 type = bp_static_marker_tracepoint;
f00aae0f 13676 }
983af33b 13677 else
f00aae0f 13678 {
a0cd5f08 13679 ops = &base_breakpoint_ops;
f00aae0f 13680 location = string_to_event_location (&arg, current_language);
7b572efb 13681 type = bp_static_tracepoint;
f00aae0f 13682 }
983af33b 13683
558a9d82 13684 create_breakpoint (get_current_arch (),
ffc2605c 13685 location.get (),
10a636cc 13686 NULL, 0, arg, false, 1 /* parse arg */,
558a9d82 13687 0 /* tempflag */,
7b572efb 13688 type /* type_wanted */,
558a9d82
YQ
13689 0 /* Ignore count */,
13690 pending_break_support,
13691 ops,
13692 from_tty,
13693 1 /* enabled */,
13694 0 /* internal */, 0);
7a697b8d
SS
13695}
13696
409873ef
SS
13697/* Set up a fake reader function that gets command lines from a linked
13698 list that was acquired during tracepoint uploading. */
13699
13700static struct uploaded_tp *this_utp;
3149d8c1 13701static int next_cmd;
409873ef
SS
13702
13703static char *
13704read_uploaded_action (void)
13705{
a18ba4e4 13706 char *rslt = nullptr;
409873ef 13707
a18ba4e4
SM
13708 if (next_cmd < this_utp->cmd_strings.size ())
13709 {
67aa1f3c 13710 rslt = this_utp->cmd_strings[next_cmd].get ();
a18ba4e4
SM
13711 next_cmd++;
13712 }
409873ef
SS
13713
13714 return rslt;
13715}
13716
00bf0b85
SS
13717/* Given information about a tracepoint as recorded on a target (which
13718 can be either a live system or a trace file), attempt to create an
13719 equivalent GDB tracepoint. This is not a reliable process, since
13720 the target does not necessarily have all the information used when
13721 the tracepoint was originally defined. */
13722
d9b3f62e 13723struct tracepoint *
00bf0b85 13724create_tracepoint_from_upload (struct uploaded_tp *utp)
d5551862 13725{
f2fc3015
TT
13726 const char *addr_str;
13727 char small_buf[100];
d9b3f62e 13728 struct tracepoint *tp;
fd9b8c24 13729
409873ef 13730 if (utp->at_string)
67aa1f3c 13731 addr_str = utp->at_string.get ();
409873ef
SS
13732 else
13733 {
13734 /* In the absence of a source location, fall back to raw
13735 address. Since there is no way to confirm that the address
13736 means the same thing as when the trace was started, warn the
13737 user. */
3e43a32a
MS
13738 warning (_("Uploaded tracepoint %d has no "
13739 "source location, using raw address"),
409873ef 13740 utp->number);
8c042590 13741 xsnprintf (small_buf, sizeof (small_buf), "*%s", hex_string (utp->addr));
409873ef
SS
13742 addr_str = small_buf;
13743 }
13744
13745 /* There's not much we can do with a sequence of bytecodes. */
13746 if (utp->cond && !utp->cond_string)
3e43a32a
MS
13747 warning (_("Uploaded tracepoint %d condition "
13748 "has no source form, ignoring it"),
409873ef 13749 utp->number);
d5551862 13750
ffc2605c
TT
13751 event_location_up location = string_to_event_location (&addr_str,
13752 current_language);
8cdf0e15 13753 if (!create_breakpoint (get_current_arch (),
ffc2605c 13754 location.get (),
67aa1f3c 13755 utp->cond_string.get (), -1, addr_str,
10a636cc 13756 false /* force_condition */,
e7e0cddf 13757 0 /* parse cond/thread */,
8cdf0e15 13758 0 /* tempflag */,
0fb4aa4b 13759 utp->type /* type_wanted */,
8cdf0e15
VP
13760 0 /* Ignore count */,
13761 pending_break_support,
a0cd5f08 13762 &base_breakpoint_ops,
8cdf0e15 13763 0 /* from_tty */,
84f4c1fe 13764 utp->enabled /* enabled */,
44f238bb
PA
13765 0 /* internal */,
13766 CREATE_BREAKPOINT_FLAGS_INSERTED))
ffc2605c 13767 return NULL;
fd9b8c24 13768
409873ef 13769 /* Get the tracepoint we just created. */
fd9b8c24
PA
13770 tp = get_tracepoint (tracepoint_count);
13771 gdb_assert (tp != NULL);
d5551862 13772
00bf0b85
SS
13773 if (utp->pass > 0)
13774 {
8c042590 13775 xsnprintf (small_buf, sizeof (small_buf), "%d %d", utp->pass,
c1fc2657 13776 tp->number);
00bf0b85 13777
409873ef 13778 trace_pass_command (small_buf, 0);
00bf0b85
SS
13779 }
13780
409873ef
SS
13781 /* If we have uploaded versions of the original commands, set up a
13782 special-purpose "reader" function and call the usual command line
13783 reader, then pass the result to the breakpoint command-setting
13784 function. */
a18ba4e4 13785 if (!utp->cmd_strings.empty ())
00bf0b85 13786 {
12973681 13787 counted_command_line cmd_list;
00bf0b85 13788
409873ef 13789 this_utp = utp;
3149d8c1 13790 next_cmd = 0;
d5551862 13791
60b3cef2 13792 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL);
409873ef 13793
c1fc2657 13794 breakpoint_set_commands (tp, std::move (cmd_list));
00bf0b85 13795 }
a18ba4e4
SM
13796 else if (!utp->actions.empty ()
13797 || !utp->step_actions.empty ())
3e43a32a
MS
13798 warning (_("Uploaded tracepoint %d actions "
13799 "have no source form, ignoring them"),
409873ef 13800 utp->number);
00bf0b85 13801
f196051f 13802 /* Copy any status information that might be available. */
c1fc2657 13803 tp->hit_count = utp->hit_count;
f196051f
SS
13804 tp->traceframe_usage = utp->traceframe_usage;
13805
00bf0b85 13806 return tp;
d9b3f62e 13807}
00bf0b85 13808
1042e4c0
SS
13809/* Print information on tracepoint number TPNUM_EXP, or all if
13810 omitted. */
13811
13812static void
1d12d88f 13813info_tracepoints_command (const char *args, int from_tty)
1042e4c0 13814{
79a45e25 13815 struct ui_out *uiout = current_uiout;
e5a67952 13816 int num_printed;
1042e4c0 13817
5c458ae8 13818 num_printed = breakpoint_1 (args, false, is_tracepoint);
d77f58be
SS
13819
13820 if (num_printed == 0)
1042e4c0 13821 {
e5a67952 13822 if (args == NULL || *args == '\0')
112e8700 13823 uiout->message ("No tracepoints.\n");
d77f58be 13824 else
112e8700 13825 uiout->message ("No tracepoint matching '%s'.\n", args);
1042e4c0 13826 }
ad443146
SS
13827
13828 default_collect_info ();
1042e4c0
SS
13829}
13830
4a64f543 13831/* The 'enable trace' command enables tracepoints.
1042e4c0
SS
13832 Not supported by all targets. */
13833static void
5fed81ff 13834enable_trace_command (const char *args, int from_tty)
1042e4c0
SS
13835{
13836 enable_command (args, from_tty);
13837}
13838
4a64f543 13839/* The 'disable trace' command disables tracepoints.
1042e4c0
SS
13840 Not supported by all targets. */
13841static void
5fed81ff 13842disable_trace_command (const char *args, int from_tty)
1042e4c0
SS
13843{
13844 disable_command (args, from_tty);
13845}
13846
4a64f543 13847/* Remove a tracepoint (or all if no argument). */
1042e4c0 13848static void
4495129a 13849delete_trace_command (const char *arg, int from_tty)
1042e4c0 13850{
1042e4c0
SS
13851 dont_repeat ();
13852
13853 if (arg == 0)
13854 {
13855 int breaks_to_delete = 0;
13856
13857 /* Delete all breakpoints if no argument.
dda83cd7
SM
13858 Do not delete internal or call-dummy breakpoints, these
13859 have to be deleted with an explicit breakpoint number
4a64f543 13860 argument. */
f6d17b2b 13861 for (breakpoint *tp : all_tracepoints ())
1428b37a 13862 if (is_tracepoint (tp) && user_breakpoint_p (tp))
1042e4c0
SS
13863 {
13864 breaks_to_delete = 1;
13865 break;
13866 }
1042e4c0
SS
13867
13868 /* Ask user only if there are some breakpoints to delete. */
13869 if (!from_tty
13870 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
13871 {
1428b37a 13872 for (breakpoint *b : all_breakpoints_safe ())
46c6471b 13873 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0 13874 delete_breakpoint (b);
1042e4c0
SS
13875 }
13876 }
13877 else
48649e1b 13878 map_breakpoint_numbers
b926417a 13879 (arg, [&] (breakpoint *br)
48649e1b 13880 {
b926417a 13881 iterate_over_related_breakpoints (br, delete_breakpoint);
48649e1b 13882 });
1042e4c0
SS
13883}
13884
197f0a60
TT
13885/* Helper function for trace_pass_command. */
13886
13887static void
d9b3f62e 13888trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
197f0a60 13889{
d9b3f62e 13890 tp->pass_count = count;
76727919 13891 gdb::observers::breakpoint_modified.notify (tp);
197f0a60 13892 if (from_tty)
6cb06a8c
TT
13893 gdb_printf (_("Setting tracepoint %d's passcount to %d\n"),
13894 tp->number, count);
197f0a60
TT
13895}
13896
1042e4c0
SS
13897/* Set passcount for tracepoint.
13898
13899 First command argument is passcount, second is tracepoint number.
13900 If tracepoint number omitted, apply to most recently defined.
13901 Also accepts special argument "all". */
13902
13903static void
0b39b52e 13904trace_pass_command (const char *args, int from_tty)
1042e4c0 13905{
d9b3f62e 13906 struct tracepoint *t1;
0b39b52e 13907 ULONGEST count;
1042e4c0
SS
13908
13909 if (args == 0 || *args == 0)
3e43a32a
MS
13910 error (_("passcount command requires an "
13911 "argument (count + optional TP num)"));
1042e4c0 13912
0b39b52e 13913 count = strtoulst (args, &args, 10); /* Count comes first, then TP num. */
1042e4c0 13914
529480d0 13915 args = skip_spaces (args);
1042e4c0
SS
13916 if (*args && strncasecmp (args, "all", 3) == 0)
13917 {
13918 args += 3; /* Skip special argument "all". */
1042e4c0
SS
13919 if (*args)
13920 error (_("Junk at end of arguments."));
1042e4c0 13921
f6d17b2b 13922 for (breakpoint *b : all_tracepoints ())
01add95b
SM
13923 {
13924 t1 = (struct tracepoint *) b;
13925 trace_pass_set_count (t1, count, from_tty);
13926 }
197f0a60
TT
13927 }
13928 else if (*args == '\0')
1042e4c0 13929 {
5fa1d40e 13930 t1 = get_tracepoint_by_number (&args, NULL);
1042e4c0 13931 if (t1)
197f0a60
TT
13932 trace_pass_set_count (t1, count, from_tty);
13933 }
13934 else
13935 {
bfd28288
PA
13936 number_or_range_parser parser (args);
13937 while (!parser.finished ())
1042e4c0 13938 {
bfd28288 13939 t1 = get_tracepoint_by_number (&args, &parser);
197f0a60
TT
13940 if (t1)
13941 trace_pass_set_count (t1, count, from_tty);
1042e4c0
SS
13942 }
13943 }
1042e4c0
SS
13944}
13945
d9b3f62e 13946struct tracepoint *
1042e4c0
SS
13947get_tracepoint (int num)
13948{
f6d17b2b 13949 for (breakpoint *t : all_tracepoints ())
1042e4c0 13950 if (t->number == num)
d9b3f62e 13951 return (struct tracepoint *) t;
1042e4c0
SS
13952
13953 return NULL;
13954}
13955
d5551862
SS
13956/* Find the tracepoint with the given target-side number (which may be
13957 different from the tracepoint number after disconnecting and
13958 reconnecting). */
13959
d9b3f62e 13960struct tracepoint *
d5551862
SS
13961get_tracepoint_by_number_on_target (int num)
13962{
f6d17b2b 13963 for (breakpoint *b : all_tracepoints ())
d9b3f62e
PA
13964 {
13965 struct tracepoint *t = (struct tracepoint *) b;
13966
13967 if (t->number_on_target == num)
13968 return t;
13969 }
d5551862
SS
13970
13971 return NULL;
13972}
13973
1042e4c0 13974/* Utility: parse a tracepoint number and look it up in the list.
197f0a60 13975 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
5fa1d40e
YQ
13976 If the argument is missing, the most recent tracepoint
13977 (tracepoint_count) is returned. */
13978
d9b3f62e 13979struct tracepoint *
0b39b52e 13980get_tracepoint_by_number (const char **arg,
bfd28288 13981 number_or_range_parser *parser)
1042e4c0 13982{
1042e4c0 13983 int tpnum;
0b39b52e 13984 const char *instring = arg == NULL ? NULL : *arg;
1042e4c0 13985
bfd28288 13986 if (parser != NULL)
197f0a60 13987 {
bfd28288
PA
13988 gdb_assert (!parser->finished ());
13989 tpnum = parser->get_number ();
197f0a60
TT
13990 }
13991 else if (arg == NULL || *arg == NULL || ! **arg)
5fa1d40e 13992 tpnum = tracepoint_count;
1042e4c0 13993 else
197f0a60 13994 tpnum = get_number (arg);
1042e4c0
SS
13995
13996 if (tpnum <= 0)
13997 {
13998 if (instring && *instring)
6cb06a8c
TT
13999 gdb_printf (_("bad tracepoint number at or near '%s'\n"),
14000 instring);
1042e4c0 14001 else
6cb06a8c 14002 gdb_printf (_("No previous tracepoint\n"));
1042e4c0
SS
14003 return NULL;
14004 }
14005
f6d17b2b 14006 for (breakpoint *t : all_tracepoints ())
1042e4c0 14007 if (t->number == tpnum)
d9b3f62e 14008 return (struct tracepoint *) t;
1042e4c0 14009
6cb06a8c 14010 gdb_printf ("No tracepoint number %d.\n", tpnum);
1042e4c0
SS
14011 return NULL;
14012}
14013
d9b3f62e 14014void
04d0163c 14015breakpoint::print_recreate_thread (struct ui_file *fp) const
d9b3f62e 14016{
04d0163c
TT
14017 if (thread != -1)
14018 gdb_printf (fp, " thread %d", thread);
d9b3f62e 14019
04d0163c
TT
14020 if (task != 0)
14021 gdb_printf (fp, " task %d", task);
d9b3f62e 14022
6cb06a8c 14023 gdb_printf (fp, "\n");
d9b3f62e
PA
14024}
14025
6149aea9
PA
14026/* Save information on user settable breakpoints (watchpoints, etc) to
14027 a new script file named FILENAME. If FILTER is non-NULL, call it
14028 on each breakpoint and only include the ones for which it returns
f2478a7e 14029 true. */
6149aea9 14030
1042e4c0 14031static void
4495129a 14032save_breakpoints (const char *filename, int from_tty,
f2478a7e 14033 bool (*filter) (const struct breakpoint *))
1042e4c0 14034{
6149aea9 14035 int any = 0;
6149aea9 14036 int extra_trace_bits = 0;
1042e4c0 14037
6149aea9
PA
14038 if (filename == 0 || *filename == 0)
14039 error (_("Argument required (file name in which to save)"));
1042e4c0
SS
14040
14041 /* See if we have anything to save. */
43892fdf 14042 for (breakpoint *tp : all_breakpoints ())
01add95b
SM
14043 {
14044 /* Skip internal and momentary breakpoints. */
14045 if (!user_breakpoint_p (tp))
14046 continue;
6149aea9 14047
01add95b
SM
14048 /* If we have a filter, only save the breakpoints it accepts. */
14049 if (filter && !filter (tp))
14050 continue;
6149aea9 14051
01add95b 14052 any = 1;
6149aea9 14053
01add95b
SM
14054 if (is_tracepoint (tp))
14055 {
14056 extra_trace_bits = 1;
6149aea9 14057
01add95b
SM
14058 /* We can stop searching. */
14059 break;
14060 }
14061 }
6149aea9
PA
14062
14063 if (!any)
1042e4c0 14064 {
6149aea9 14065 warning (_("Nothing to save."));
1042e4c0
SS
14066 return;
14067 }
14068
ee0c3293 14069 gdb::unique_xmalloc_ptr<char> expanded_filename (tilde_expand (filename));
d7e74731
PA
14070
14071 stdio_file fp;
14072
ee0c3293 14073 if (!fp.open (expanded_filename.get (), "w"))
6149aea9 14074 error (_("Unable to open file '%s' for saving (%s)"),
ee0c3293 14075 expanded_filename.get (), safe_strerror (errno));
8bf6485c 14076
6149aea9 14077 if (extra_trace_bits)
d7e74731 14078 save_trace_state_variables (&fp);
8bf6485c 14079
43892fdf 14080 for (breakpoint *tp : all_breakpoints ())
01add95b
SM
14081 {
14082 /* Skip internal and momentary breakpoints. */
14083 if (!user_breakpoint_p (tp))
14084 continue;
8bf6485c 14085
01add95b
SM
14086 /* If we have a filter, only save the breakpoints it accepts. */
14087 if (filter && !filter (tp))
14088 continue;
6149aea9 14089
c47614fe 14090 tp->print_recreate (&fp);
1042e4c0 14091
01add95b
SM
14092 /* Note, we can't rely on tp->number for anything, as we can't
14093 assume the recreated breakpoint numbers will match. Use $bpnum
14094 instead. */
6149aea9 14095
01add95b 14096 if (tp->cond_string)
6f781ee3 14097 fp.printf (" condition $bpnum %s\n", tp->cond_string.get ());
6149aea9 14098
01add95b
SM
14099 if (tp->ignore_count)
14100 fp.printf (" ignore $bpnum %d\n", tp->ignore_count);
6149aea9 14101
01add95b
SM
14102 if (tp->type != bp_dprintf && tp->commands)
14103 {
14104 fp.puts (" commands\n");
1042e4c0 14105
01add95b
SM
14106 current_uiout->redirect (&fp);
14107 try
14108 {
14109 print_command_lines (current_uiout, tp->commands.get (), 2);
14110 }
14111 catch (const gdb_exception &ex)
14112 {
14113 current_uiout->redirect (NULL);
14114 throw;
14115 }
6149aea9 14116
01add95b
SM
14117 current_uiout->redirect (NULL);
14118 fp.puts (" end\n");
14119 }
6149aea9 14120
01add95b
SM
14121 if (tp->enable_state == bp_disabled)
14122 fp.puts ("disable $bpnum\n");
6149aea9 14123
01add95b
SM
14124 /* If this is a multi-location breakpoint, check if the locations
14125 should be individually disabled. Watchpoint locations are
14126 special, and not user visible. */
14127 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
14128 {
14129 int n = 1;
40cb8ca5 14130
01add95b
SM
14131 for (bp_location *loc : tp->locations ())
14132 {
14133 if (!loc->enabled)
14134 fp.printf ("disable $bpnum.%d\n", n);
14135
14136 n++;
14137 }
14138 }
14139 }
8bf6485c 14140
e0700ba4
SM
14141 if (extra_trace_bits && !default_collect.empty ())
14142 fp.printf ("set default-collect %s\n", default_collect.c_str ());
8bf6485c 14143
1042e4c0 14144 if (from_tty)
6cb06a8c 14145 gdb_printf (_("Saved to file '%s'.\n"), expanded_filename.get ());
6149aea9
PA
14146}
14147
14148/* The `save breakpoints' command. */
14149
14150static void
4495129a 14151save_breakpoints_command (const char *args, int from_tty)
6149aea9
PA
14152{
14153 save_breakpoints (args, from_tty, NULL);
14154}
14155
14156/* The `save tracepoints' command. */
14157
14158static void
4495129a 14159save_tracepoints_command (const char *args, int from_tty)
6149aea9
PA
14160{
14161 save_breakpoints (args, from_tty, is_tracepoint);
1042e4c0
SS
14162}
14163
c906108c 14164\f
629500fa
KS
14165/* This help string is used to consolidate all the help string for specifying
14166 locations used by several commands. */
14167
14168#define LOCATION_HELP_STRING \
14169"Linespecs are colon-separated lists of location parameters, such as\n\
14170source filename, function name, label name, and line number.\n\
14171Example: To specify the start of a label named \"the_top\" in the\n\
14172function \"fact\" in the file \"factorial.c\", use\n\
14173\"factorial.c:fact:the_top\".\n\
14174\n\
14175Address locations begin with \"*\" and specify an exact address in the\n\
14176program. Example: To specify the fourth byte past the start function\n\
14177\"main\", use \"*main + 4\".\n\
14178\n\
14179Explicit locations are similar to linespecs but use an option/argument\n\
14180syntax to specify location parameters.\n\
14181Example: To specify the start of the label named \"the_top\" in the\n\
14182function \"fact\" in the file \"factorial.c\", use \"-source factorial.c\n\
a20714ff
PA
14183-function fact -label the_top\".\n\
14184\n\
14185By default, a specified function is matched against the program's\n\
14186functions in all scopes. For C++, this means in all namespaces and\n\
14187classes. For Ada, this means in all packages. E.g., in C++,\n\
14188\"func()\" matches \"A::func()\", \"A::B::func()\", etc. The\n\
14189\"-qualified\" flag overrides this behavior, making GDB interpret the\n\
89549d7f 14190specified name as a complete fully-qualified name instead."
629500fa 14191
4a64f543
MS
14192/* This help string is used for the break, hbreak, tbreak and thbreak
14193 commands. It is defined as a macro to prevent duplication.
14194 COMMAND should be a string constant containing the name of the
14195 command. */
629500fa 14196
31e2b00f 14197#define BREAK_ARGS_HELP(command) \
733d554a
TBA
14198command" [PROBE_MODIFIER] [LOCATION] [thread THREADNUM]\n\
14199\t[-force-condition] [if CONDITION]\n\
fb7b5af4
SDJ
14200PROBE_MODIFIER shall be present if the command is to be placed in a\n\
14201probe point. Accepted values are `-probe' (for a generic, automatically\n\
d4777acb
JM
14202guessed probe type), `-probe-stap' (for a SystemTap probe) or \n\
14203`-probe-dtrace' (for a DTrace probe).\n\
629500fa
KS
14204LOCATION may be a linespec, address, or explicit location as described\n\
14205below.\n\
14206\n\
dc10affe
PA
14207With no LOCATION, uses current execution address of the selected\n\
14208stack frame. This is useful for breaking on return to a stack frame.\n\
31e2b00f
AS
14209\n\
14210THREADNUM is the number from \"info threads\".\n\
14211CONDITION is a boolean expression.\n\
733d554a
TBA
14212\n\
14213With the \"-force-condition\" flag, the condition is defined even when\n\
14214it is invalid for all current locations.\n\
89549d7f 14215\n" LOCATION_HELP_STRING "\n\n\
d41c0fc8
PA
14216Multiple breakpoints at one place are permitted, and useful if their\n\
14217conditions are different.\n\
31e2b00f
AS
14218\n\
14219Do \"help breakpoints\" for info on other commands dealing with breakpoints."
14220
44feb3ce
TT
14221/* List of subcommands for "catch". */
14222static struct cmd_list_element *catch_cmdlist;
14223
14224/* List of subcommands for "tcatch". */
14225static struct cmd_list_element *tcatch_cmdlist;
14226
9ac4176b 14227void
a121b7c1 14228add_catch_command (const char *name, const char *docstring,
5538b03c 14229 cmd_func_ftype *func,
625e8578 14230 completer_ftype *completer,
44feb3ce
TT
14231 void *user_data_catch,
14232 void *user_data_tcatch)
14233{
14234 struct cmd_list_element *command;
14235
0450cc4c 14236 command = add_cmd (name, class_breakpoint, docstring,
44feb3ce 14237 &catch_cmdlist);
5538b03c 14238 command->func = func;
0f8e2034 14239 command->set_context (user_data_catch);
a96d9b2e 14240 set_cmd_completer (command, completer);
44feb3ce 14241
0450cc4c 14242 command = add_cmd (name, class_breakpoint, docstring,
44feb3ce 14243 &tcatch_cmdlist);
5538b03c 14244 command->func = func;
0f8e2034 14245 command->set_context (user_data_tcatch);
a96d9b2e 14246 set_cmd_completer (command, completer);
44feb3ce
TT
14247}
14248
0574c78f
GB
14249/* Zero if any of the breakpoint's locations could be a location where
14250 functions have been inlined, nonzero otherwise. */
14251
14252static int
14253is_non_inline_function (struct breakpoint *b)
14254{
14255 /* The shared library event breakpoint is set on the address of a
14256 non-inline function. */
14257 if (b->type == bp_shlib_event)
14258 return 1;
14259
14260 return 0;
14261}
14262
14263/* Nonzero if the specified PC cannot be a location where functions
14264 have been inlined. */
14265
14266int
accd0bcd 14267pc_at_non_inline_function (const address_space *aspace, CORE_ADDR pc,
c272a98c 14268 const target_waitstatus &ws)
0574c78f 14269{
43892fdf 14270 for (breakpoint *b : all_breakpoints ())
0574c78f
GB
14271 {
14272 if (!is_non_inline_function (b))
14273 continue;
14274
40cb8ca5 14275 for (bp_location *bl : b->locations ())
0574c78f
GB
14276 {
14277 if (!bl->shlib_disabled
09ac7c10 14278 && bpstat_check_location (bl, aspace, pc, ws))
0574c78f
GB
14279 return 1;
14280 }
14281 }
14282
14283 return 0;
14284}
14285
2f202fde
JK
14286/* Remove any references to OBJFILE which is going to be freed. */
14287
14288void
14289breakpoint_free_objfile (struct objfile *objfile)
14290{
48d7020b 14291 for (bp_location *loc : all_bp_locations ())
3c86fae3 14292 if (loc->symtab != NULL && loc->symtab->compunit ()->objfile () == objfile)
2f202fde
JK
14293 loc->symtab = NULL;
14294}
14295
8bfd80db
YQ
14296/* Chain containing all defined "enable breakpoint" subcommands. */
14297
14298static struct cmd_list_element *enablebreaklist = NULL;
14299
8588b356
SM
14300/* See breakpoint.h. */
14301
14302cmd_list_element *commands_cmd_element = nullptr;
14303
6c265988 14304void _initialize_breakpoint ();
c906108c 14305void
6c265988 14306_initialize_breakpoint ()
c906108c
SS
14307{
14308 struct cmd_list_element *c;
14309
c90e7d63
SM
14310 gdb::observers::solib_unloaded.attach (disable_breakpoints_in_unloaded_shlib,
14311 "breakpoint");
14312 gdb::observers::free_objfile.attach (disable_breakpoints_in_freed_objfile,
14313 "breakpoint");
14314 gdb::observers::memory_changed.attach (invalidate_bp_value_on_memory_change,
14315 "breakpoint");
84acb35a 14316
c906108c
SS
14317 breakpoint_chain = 0;
14318 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
14319 before a breakpoint is set. */
14320 breakpoint_count = 0;
14321
1042e4c0
SS
14322 tracepoint_count = 0;
14323
1bedd215
AC
14324 add_com ("ignore", class_breakpoint, ignore_command, _("\
14325Set ignore-count of breakpoint number N to COUNT.\n\
14326Usage is `ignore N COUNT'."));
c906108c 14327
8588b356
SM
14328 commands_cmd_element = add_com ("commands", class_breakpoint,
14329 commands_command, _("\
18da0c51
MG
14330Set commands to be executed when the given breakpoints are hit.\n\
14331Give a space-separated breakpoint list as argument after \"commands\".\n\
14332A list element can be a breakpoint number (e.g. `5') or a range of numbers\n\
14333(e.g. `5-7').\n\
c906108c
SS
14334With no argument, the targeted breakpoint is the last one set.\n\
14335The commands themselves follow starting on the next line.\n\
14336Type a line containing \"end\" to indicate the end of them.\n\
14337Give \"silent\" as the first line to make the breakpoint silent;\n\
1bedd215 14338then no output is printed when it is hit, except what the commands print."));
c906108c 14339
b1d4d8d1
TBA
14340 const auto cc_opts = make_condition_command_options_def_group (nullptr);
14341 static std::string condition_command_help
14342 = gdb::option::build_help (_("\
1bedd215 14343Specify breakpoint number N to break only if COND is true.\n\
b1d4d8d1 14344Usage is `condition [OPTION] N COND', where N is an integer and COND\n\
733d554a 14345is an expression to be evaluated whenever breakpoint N is reached.\n\
b1d4d8d1
TBA
14346\n\
14347Options:\n\
14348%OPTIONS%"), cc_opts);
14349
14350 c = add_com ("condition", class_breakpoint, condition_command,
14351 condition_command_help.c_str ());
14352 set_cmd_completer_handle_brkchars (c, condition_completer);
c906108c 14353
1bedd215 14354 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
31e2b00f 14355Set a temporary breakpoint.\n\
c906108c
SS
14356Like \"break\" except the breakpoint is only temporary,\n\
14357so it will be deleted when hit. Equivalent to \"break\" followed\n\
31e2b00f
AS
14358by using \"enable delete\" on the breakpoint number.\n\
14359\n"
14360BREAK_ARGS_HELP ("tbreak")));
5ba2abeb 14361 set_cmd_completer (c, location_completer);
c94fdfd0 14362
1bedd215 14363 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
a3be7890 14364Set a hardware assisted breakpoint.\n\
c906108c 14365Like \"break\" except the breakpoint requires hardware support,\n\
31e2b00f
AS
14366some target hardware may not have this support.\n\
14367\n"
14368BREAK_ARGS_HELP ("hbreak")));
5ba2abeb 14369 set_cmd_completer (c, location_completer);
c906108c 14370
1bedd215 14371 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
31e2b00f 14372Set a temporary hardware assisted breakpoint.\n\
c906108c 14373Like \"hbreak\" except the breakpoint is only temporary,\n\
31e2b00f
AS
14374so it will be deleted when hit.\n\
14375\n"
14376BREAK_ARGS_HELP ("thbreak")));
5ba2abeb 14377 set_cmd_completer (c, location_completer);
c906108c 14378
3947f654
SM
14379 cmd_list_element *enable_cmd
14380 = add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
590042fc
PW
14381Enable all or some breakpoints.\n\
14382Usage: enable [BREAKPOINTNUM]...\n\
c906108c
SS
14383Give breakpoint numbers (separated by spaces) as arguments.\n\
14384With no subcommand, breakpoints are enabled until you command otherwise.\n\
14385This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 14386With a subcommand you can enable temporarily."),
3947f654 14387 &enablelist, 1, &cmdlist);
c906108c 14388
3947f654 14389 add_com_alias ("en", enable_cmd, class_breakpoint, 1);
c906108c 14390
84951ab5 14391 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
590042fc
PW
14392Enable all or some breakpoints.\n\
14393Usage: enable breakpoints [BREAKPOINTNUM]...\n\
c906108c
SS
14394Give breakpoint numbers (separated by spaces) as arguments.\n\
14395This is used to cancel the effect of the \"disable\" command.\n\
89549d7f 14396May be abbreviated to simply \"enable\"."),
2f822da5 14397 &enablebreaklist, 1, &enablelist);
c906108c 14398
1a966eab 14399 add_cmd ("once", no_class, enable_once_command, _("\
590042fc
PW
14400Enable some breakpoints for one hit.\n\
14401Usage: enable breakpoints once BREAKPOINTNUM...\n\
1a966eab 14402If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
c906108c
SS
14403 &enablebreaklist);
14404
1a966eab 14405 add_cmd ("delete", no_class, enable_delete_command, _("\
590042fc
PW
14406Enable some breakpoints and delete when hit.\n\
14407Usage: enable breakpoints delete BREAKPOINTNUM...\n\
1a966eab 14408If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
14409 &enablebreaklist);
14410
816338b5 14411 add_cmd ("count", no_class, enable_count_command, _("\
590042fc
PW
14412Enable some breakpoints for COUNT hits.\n\
14413Usage: enable breakpoints count COUNT BREAKPOINTNUM...\n\
816338b5
SS
14414If a breakpoint is hit while enabled in this fashion,\n\
14415the count is decremented; when it reaches zero, the breakpoint is disabled."),
14416 &enablebreaklist);
14417
1a966eab 14418 add_cmd ("delete", no_class, enable_delete_command, _("\
590042fc
PW
14419Enable some breakpoints and delete when hit.\n\
14420Usage: enable delete BREAKPOINTNUM...\n\
1a966eab 14421If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
14422 &enablelist);
14423
1a966eab 14424 add_cmd ("once", no_class, enable_once_command, _("\
590042fc
PW
14425Enable some breakpoints for one hit.\n\
14426Usage: enable once BREAKPOINTNUM...\n\
1a966eab 14427If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
816338b5
SS
14428 &enablelist);
14429
14430 add_cmd ("count", no_class, enable_count_command, _("\
590042fc
PW
14431Enable some breakpoints for COUNT hits.\n\
14432Usage: enable count COUNT BREAKPOINTNUM...\n\
816338b5
SS
14433If a breakpoint is hit while enabled in this fashion,\n\
14434the count is decremented; when it reaches zero, the breakpoint is disabled."),
c906108c
SS
14435 &enablelist);
14436
3947f654
SM
14437 cmd_list_element *disable_cmd
14438 = add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
590042fc
PW
14439Disable all or some breakpoints.\n\
14440Usage: disable [BREAKPOINTNUM]...\n\
c906108c
SS
14441Arguments are breakpoint numbers with spaces in between.\n\
14442To disable all breakpoints, give no argument.\n\
64b9b334 14443A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
3947f654
SM
14444 &disablelist, 1, &cmdlist);
14445 add_com_alias ("dis", disable_cmd, class_breakpoint, 1);
14446 add_com_alias ("disa", disable_cmd, class_breakpoint, 1);
c906108c 14447
57b4f16e 14448 add_cmd ("breakpoints", class_breakpoint, disable_command, _("\
590042fc
PW
14449Disable all or some breakpoints.\n\
14450Usage: disable breakpoints [BREAKPOINTNUM]...\n\
c906108c
SS
14451Arguments are breakpoint numbers with spaces in between.\n\
14452To disable all breakpoints, give no argument.\n\
64b9b334 14453A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
1a966eab 14454This command may be abbreviated \"disable\"."),
c906108c
SS
14455 &disablelist);
14456
3947f654
SM
14457 cmd_list_element *delete_cmd
14458 = add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
590042fc
PW
14459Delete all or some breakpoints.\n\
14460Usage: delete [BREAKPOINTNUM]...\n\
c906108c
SS
14461Arguments are breakpoint numbers with spaces in between.\n\
14462To delete all breakpoints, give no argument.\n\
14463\n\
590042fc 14464Also a prefix command for deletion of other GDB objects."),
3947f654
SM
14465 &deletelist, 1, &cmdlist);
14466 add_com_alias ("d", delete_cmd, class_breakpoint, 1);
14467 add_com_alias ("del", delete_cmd, class_breakpoint, 1);
c906108c 14468
57b4f16e 14469 add_cmd ("breakpoints", class_breakpoint, delete_command, _("\
590042fc
PW
14470Delete all or some breakpoints or auto-display expressions.\n\
14471Usage: delete breakpoints [BREAKPOINTNUM]...\n\
c906108c
SS
14472Arguments are breakpoint numbers with spaces in between.\n\
14473To delete all breakpoints, give no argument.\n\
1a966eab 14474This command may be abbreviated \"delete\"."),
c906108c
SS
14475 &deletelist);
14476
3947f654
SM
14477 cmd_list_element *clear_cmd
14478 = add_com ("clear", class_breakpoint, clear_command, _("\
629500fa
KS
14479Clear breakpoint at specified location.\n\
14480Argument may be a linespec, explicit, or address location as described below.\n\
1bedd215
AC
14481\n\
14482With no argument, clears all breakpoints in the line that the selected frame\n\
629500fa 14483is executing in.\n"
89549d7f 14484"\n" LOCATION_HELP_STRING "\n\n\
1bedd215 14485See also the \"delete\" command which clears breakpoints by number."));
3947f654 14486 add_com_alias ("cl", clear_cmd, class_breakpoint, 1);
c906108c 14487
3947f654
SM
14488 cmd_list_element *break_cmd
14489 = add_com ("break", class_breakpoint, break_command, _("\
629500fa 14490Set breakpoint at specified location.\n"
31e2b00f 14491BREAK_ARGS_HELP ("break")));
3947f654 14492 set_cmd_completer (break_cmd, location_completer);
c94fdfd0 14493
3947f654
SM
14494 add_com_alias ("b", break_cmd, class_run, 1);
14495 add_com_alias ("br", break_cmd, class_run, 1);
14496 add_com_alias ("bre", break_cmd, class_run, 1);
14497 add_com_alias ("brea", break_cmd, class_run, 1);
c906108c 14498
e0f25bd9
SM
14499 cmd_list_element *info_breakpoints_cmd
14500 = add_info ("breakpoints", info_breakpoints_command, _("\
e5a67952 14501Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
c906108c
SS
14502The \"Type\" column indicates one of:\n\
14503\tbreakpoint - normal breakpoint\n\
14504\twatchpoint - watchpoint\n\
14505The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
14506the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
14507breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
14508address and file/line number respectively.\n\
14509\n\
14510Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
14511are set to the address of the last breakpoint listed unless the command\n\
14512is prefixed with \"server \".\n\n\
c906108c 14513Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 14514breakpoint set."));
c906108c 14515
e0f25bd9 14516 add_info_alias ("b", info_breakpoints_cmd, 1);
6b04bdb7 14517
1a966eab
AC
14518 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
14519Status of all breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
14520The \"Type\" column indicates one of:\n\
14521\tbreakpoint - normal breakpoint\n\
14522\twatchpoint - watchpoint\n\
14523\tlongjmp - internal breakpoint used to step through longjmp()\n\
14524\tlongjmp resume - internal breakpoint at the target of longjmp()\n\
14525\tuntil - internal breakpoint used by the \"until\" command\n\
1a966eab
AC
14526\tfinish - internal breakpoint used by the \"finish\" command\n\
14527The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
c906108c
SS
14528the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
14529breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1a966eab
AC
14530address and file/line number respectively.\n\
14531\n\
14532Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
14533are set to the address of the last breakpoint listed unless the command\n\
14534is prefixed with \"server \".\n\n\
c906108c 14535Convenience variable \"$bpnum\" contains the number of the last\n\
1a966eab 14536breakpoint set."),
c906108c
SS
14537 &maintenanceinfolist);
14538
3b6acaee 14539 add_basic_prefix_cmd ("catch", class_breakpoint, _("\
44feb3ce 14540Set catchpoints to catch events."),
2f822da5 14541 &catch_cmdlist,
3b6acaee 14542 0/*allow-unknown*/, &cmdlist);
44feb3ce 14543
3b6acaee 14544 add_basic_prefix_cmd ("tcatch", class_breakpoint, _("\
44feb3ce 14545Set temporary catchpoints to catch events."),
2f822da5 14546 &tcatch_cmdlist,
3b6acaee 14547 0/*allow-unknown*/, &cmdlist);
44feb3ce 14548
a15a5258
AB
14549 const auto opts = make_watch_options_def_group (nullptr);
14550
14551 static const std::string watch_help = gdb::option::build_help (_("\
14552Set a watchpoint for EXPRESSION.\n\
14553Usage: watch [-location] EXPRESSION\n\
14554\n\
14555Options:\n\
14556%OPTIONS%\n\
14557\n\
c906108c 14558A watchpoint stops execution of your program whenever the value of\n\
a15a5258
AB
14559an expression changes."), opts);
14560 c = add_com ("watch", class_breakpoint, watch_command,
14561 watch_help.c_str ());
14562 set_cmd_completer_handle_brkchars (c, watch_command_completer);
14563
14564 static const std::string rwatch_help = gdb::option::build_help (_("\
14565Set a read watchpoint for EXPRESSION.\n\
14566Usage: rwatch [-location] EXPRESSION\n\
14567\n\
14568Options:\n\
14569%OPTIONS%\n\
14570\n\
14571A read watchpoint stops execution of your program whenever the value of\n\
14572an expression is read."), opts);
14573 c = add_com ("rwatch", class_breakpoint, rwatch_command,
14574 rwatch_help.c_str ());
14575 set_cmd_completer_handle_brkchars (c, watch_command_completer);
14576
14577 static const std::string awatch_help = gdb::option::build_help (_("\
14578Set an access watchpoint for EXPRESSION.\n\
14579Usage: awatch [-location] EXPRESSION\n\
14580\n\
14581Options:\n\
14582%OPTIONS%\n\
14583\n\
14584An access watchpoint stops execution of your program whenever the value\n\
14585of an expression is either read or written."), opts);
14586 c = add_com ("awatch", class_breakpoint, awatch_command,
14587 awatch_help.c_str ());
14588 set_cmd_completer_handle_brkchars (c, watch_command_completer);
c906108c 14589
11db9430 14590 add_info ("watchpoints", info_watchpoints_command, _("\
e5a67952 14591Status of specified watchpoints (all watchpoints if no argument)."));
c906108c 14592
920d2a44
AC
14593 /* XXX: cagney/2005-02-23: This should be a boolean, and should
14594 respond to changes - contrary to the description. */
85c07804
AC
14595 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
14596 &can_use_hw_watchpoints, _("\
14597Set debugger's willingness to use watchpoint hardware."), _("\
14598Show debugger's willingness to use watchpoint hardware."), _("\
c906108c
SS
14599If zero, gdb will not use hardware for new watchpoints, even if\n\
14600such is available. (However, any hardware watchpoints that were\n\
14601created before setting this to nonzero, will continue to use watchpoint\n\
85c07804
AC
14602hardware.)"),
14603 NULL,
920d2a44 14604 show_can_use_hw_watchpoints,
85c07804 14605 &setlist, &showlist);
c906108c
SS
14606
14607 can_use_hw_watchpoints = 1;
fa8d40ab 14608
1042e4c0
SS
14609 /* Tracepoint manipulation commands. */
14610
3947f654
SM
14611 cmd_list_element *trace_cmd
14612 = add_com ("trace", class_breakpoint, trace_command, _("\
629500fa 14613Set a tracepoint at specified location.\n\
1042e4c0
SS
14614\n"
14615BREAK_ARGS_HELP ("trace") "\n\
14616Do \"help tracepoints\" for info on other tracepoint commands."));
3947f654 14617 set_cmd_completer (trace_cmd, location_completer);
1042e4c0 14618
3947f654
SM
14619 add_com_alias ("tp", trace_cmd, class_breakpoint, 0);
14620 add_com_alias ("tr", trace_cmd, class_breakpoint, 1);
14621 add_com_alias ("tra", trace_cmd, class_breakpoint, 1);
14622 add_com_alias ("trac", trace_cmd, class_breakpoint, 1);
1042e4c0 14623
7a697b8d 14624 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
629500fa 14625Set a fast tracepoint at specified location.\n\
7a697b8d
SS
14626\n"
14627BREAK_ARGS_HELP ("ftrace") "\n\
14628Do \"help tracepoints\" for info on other tracepoint commands."));
14629 set_cmd_completer (c, location_completer);
14630
0fb4aa4b 14631 c = add_com ("strace", class_breakpoint, strace_command, _("\
629500fa 14632Set a static tracepoint at location or marker.\n\
0fb4aa4b
PA
14633\n\
14634strace [LOCATION] [if CONDITION]\n\
629500fa
KS
14635LOCATION may be a linespec, explicit, or address location (described below) \n\
14636or -m MARKER_ID.\n\n\
14637If a marker id is specified, probe the marker with that name. With\n\
14638no LOCATION, uses current execution address of the selected stack frame.\n\
0fb4aa4b
PA
14639Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
14640This collects arbitrary user data passed in the probe point call to the\n\
14641tracing library. You can inspect it when analyzing the trace buffer,\n\
14642by printing the $_sdata variable like any other convenience variable.\n\
14643\n\
14644CONDITION is a boolean expression.\n\
89549d7f 14645\n" LOCATION_HELP_STRING "\n\n\
d41c0fc8
PA
14646Multiple tracepoints at one place are permitted, and useful if their\n\
14647conditions are different.\n\
0fb4aa4b
PA
14648\n\
14649Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
14650Do \"help tracepoints\" for info on other tracepoint commands."));
14651 set_cmd_completer (c, location_completer);
14652
e0f25bd9
SM
14653 cmd_list_element *info_tracepoints_cmd
14654 = add_info ("tracepoints", info_tracepoints_command, _("\
e5a67952 14655Status of specified tracepoints (all tracepoints if no argument).\n\
1042e4c0
SS
14656Convenience variable \"$tpnum\" contains the number of the\n\
14657last tracepoint set."));
14658
e0f25bd9 14659 add_info_alias ("tp", info_tracepoints_cmd, 1);
1042e4c0 14660
5e84b7ee
SM
14661 cmd_list_element *delete_tracepoints_cmd
14662 = add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
1042e4c0
SS
14663Delete specified tracepoints.\n\
14664Arguments are tracepoint numbers, separated by spaces.\n\
14665No argument means delete all tracepoints."),
14666 &deletelist);
5e84b7ee 14667 add_alias_cmd ("tr", delete_tracepoints_cmd, class_trace, 1, &deletelist);
1042e4c0
SS
14668
14669 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
14670Disable specified tracepoints.\n\
14671Arguments are tracepoint numbers, separated by spaces.\n\
14672No argument means disable all tracepoints."),
14673 &disablelist);
14674 deprecate_cmd (c, "disable");
14675
14676 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
14677Enable specified tracepoints.\n\
14678Arguments are tracepoint numbers, separated by spaces.\n\
14679No argument means enable all tracepoints."),
14680 &enablelist);
14681 deprecate_cmd (c, "enable");
14682
14683 add_com ("passcount", class_trace, trace_pass_command, _("\
14684Set the passcount for a tracepoint.\n\
14685The trace will end when the tracepoint has been passed 'count' times.\n\
14686Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
14687if TPNUM is omitted, passcount refers to the last tracepoint defined."));
14688
0743fc83
TT
14689 add_basic_prefix_cmd ("save", class_breakpoint,
14690 _("Save breakpoint definitions as a script."),
2f822da5 14691 &save_cmdlist,
0743fc83 14692 0/*allow-unknown*/, &cmdlist);
6149aea9
PA
14693
14694 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
14695Save current breakpoint definitions as a script.\n\
cce7e648 14696This includes all types of breakpoints (breakpoints, watchpoints,\n\
6149aea9
PA
14697catchpoints, tracepoints). Use the 'source' command in another debug\n\
14698session to restore them."),
14699 &save_cmdlist);
14700 set_cmd_completer (c, filename_completer);
14701
3947f654
SM
14702 cmd_list_element *save_tracepoints_cmd
14703 = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
1042e4c0 14704Save current tracepoint definitions as a script.\n\
6149aea9
PA
14705Use the 'source' command in another debug session to restore them."),
14706 &save_cmdlist);
3947f654 14707 set_cmd_completer (save_tracepoints_cmd, filename_completer);
1042e4c0 14708
3947f654 14709 c = add_com_alias ("save-tracepoints", save_tracepoints_cmd, class_trace, 0);
6149aea9
PA
14710 deprecate_cmd (c, "save tracepoints");
14711
f54bdb6d
SM
14712 add_setshow_prefix_cmd ("breakpoint", class_maintenance,
14713 _("\
590042fc 14714Breakpoint specific settings.\n\
fa8d40ab 14715Configure various breakpoint-specific variables such as\n\
590042fc 14716pending breakpoint behavior."),
f54bdb6d 14717 _("\
590042fc 14718Breakpoint specific settings.\n\
fa8d40ab 14719Configure various breakpoint-specific variables such as\n\
590042fc 14720pending breakpoint behavior."),
f54bdb6d
SM
14721 &breakpoint_set_cmdlist, &breakpoint_show_cmdlist,
14722 &setlist, &showlist);
fa8d40ab 14723
7915a72c
AC
14724 add_setshow_auto_boolean_cmd ("pending", no_class,
14725 &pending_break_support, _("\
14726Set debugger's behavior regarding pending breakpoints."), _("\
14727Show debugger's behavior regarding pending breakpoints."), _("\
6e1d7d6c
AC
14728If on, an unrecognized breakpoint location will cause gdb to create a\n\
14729pending breakpoint. If off, an unrecognized breakpoint location results in\n\
14730an error. If auto, an unrecognized breakpoint location results in a\n\
7915a72c 14731user-query to see if a pending breakpoint should be created."),
2c5b56ce 14732 NULL,
920d2a44 14733 show_pending_break_support,
6e1d7d6c
AC
14734 &breakpoint_set_cmdlist,
14735 &breakpoint_show_cmdlist);
fa8d40ab
JJ
14736
14737 pending_break_support = AUTO_BOOLEAN_AUTO;
765dc015
VP
14738
14739 add_setshow_boolean_cmd ("auto-hw", no_class,
14740 &automatic_hardware_breakpoints, _("\
14741Set automatic usage of hardware breakpoints."), _("\
14742Show automatic usage of hardware breakpoints."), _("\
14743If set, the debugger will automatically use hardware breakpoints for\n\
14744breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
14745a warning will be emitted for such breakpoints."),
14746 NULL,
14747 show_automatic_hardware_breakpoints,
14748 &breakpoint_set_cmdlist,
14749 &breakpoint_show_cmdlist);
74960c60 14750
a25a5a45
PA
14751 add_setshow_boolean_cmd ("always-inserted", class_support,
14752 &always_inserted_mode, _("\
74960c60
VP
14753Set mode for inserting breakpoints."), _("\
14754Show mode for inserting breakpoints."), _("\
a25a5a45
PA
14755When this mode is on, breakpoints are inserted immediately as soon as\n\
14756they're created, kept inserted even when execution stops, and removed\n\
14757only when the user deletes them. When this mode is off (the default),\n\
14758breakpoints are inserted only when execution continues, and removed\n\
14759when execution stops."),
72d0e2c5
YQ
14760 NULL,
14761 &show_always_inserted_mode,
14762 &breakpoint_set_cmdlist,
14763 &breakpoint_show_cmdlist);
f1310107 14764
b775012e
LM
14765 add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
14766 condition_evaluation_enums,
14767 &condition_evaluation_mode_1, _("\
14768Set mode of breakpoint condition evaluation."), _("\
14769Show mode of breakpoint condition evaluation."), _("\
d1cda5d9 14770When this is set to \"host\", breakpoint conditions will be\n\
b775012e
LM
14771evaluated on the host's side by GDB. When it is set to \"target\",\n\
14772breakpoint conditions will be downloaded to the target (if the target\n\
14773supports such feature) and conditions will be evaluated on the target's side.\n\
14774If this is set to \"auto\" (default), this will be automatically set to\n\
14775\"target\" if it supports condition evaluation, otherwise it will\n\
7a4e8e7d 14776be set to \"host\"."),
b775012e
LM
14777 &set_condition_evaluation_mode,
14778 &show_condition_evaluation_mode,
14779 &breakpoint_set_cmdlist,
14780 &breakpoint_show_cmdlist);
14781
f1310107
TJB
14782 add_com ("break-range", class_breakpoint, break_range_command, _("\
14783Set a breakpoint for an address range.\n\
14784break-range START-LOCATION, END-LOCATION\n\
14785where START-LOCATION and END-LOCATION can be one of the following:\n\
14786 LINENUM, for that line in the current file,\n\
14787 FILE:LINENUM, for that line in that file,\n\
14788 +OFFSET, for that number of lines after the current line\n\
dda83cd7 14789 or the start of the range\n\
f1310107
TJB
14790 FUNCTION, for the first line in that function,\n\
14791 FILE:FUNCTION, to distinguish among like-named static functions.\n\
14792 *ADDRESS, for the instruction at that address.\n\
14793\n\
14794The breakpoint will stop execution of the inferior whenever it executes\n\
14795an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
14796range (including START-LOCATION and END-LOCATION)."));
14797
e7e0cddf 14798 c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
629500fa 14799Set a dynamic printf at specified location.\n\
e7e0cddf 14800dprintf location,format string,arg1,arg2,...\n\
629500fa
KS
14801location may be a linespec, explicit, or address location.\n"
14802"\n" LOCATION_HELP_STRING));
e7e0cddf
SS
14803 set_cmd_completer (c, location_completer);
14804
14805 add_setshow_enum_cmd ("dprintf-style", class_support,
14806 dprintf_style_enums, &dprintf_style, _("\
14807Set the style of usage for dynamic printf."), _("\
14808Show the style of usage for dynamic printf."), _("\
14809This setting chooses how GDB will do a dynamic printf.\n\
14810If the value is \"gdb\", then the printing is done by GDB to its own\n\
14811console, as with the \"printf\" command.\n\
14812If the value is \"call\", the print is done by calling a function in your\n\
14813program; by default printf(), but you can choose a different function or\n\
14814output stream by setting dprintf-function and dprintf-channel."),
14815 update_dprintf_commands, NULL,
14816 &setlist, &showlist);
14817
e7e0cddf
SS
14818 add_setshow_string_cmd ("dprintf-function", class_support,
14819 &dprintf_function, _("\
590042fc
PW
14820Set the function to use for dynamic printf."), _("\
14821Show the function to use for dynamic printf."), NULL,
e7e0cddf
SS
14822 update_dprintf_commands, NULL,
14823 &setlist, &showlist);
14824
e7e0cddf
SS
14825 add_setshow_string_cmd ("dprintf-channel", class_support,
14826 &dprintf_channel, _("\
590042fc
PW
14827Set the channel to use for dynamic printf."), _("\
14828Show the channel to use for dynamic printf."), NULL,
e7e0cddf
SS
14829 update_dprintf_commands, NULL,
14830 &setlist, &showlist);
14831
d3ce09f5
SS
14832 add_setshow_boolean_cmd ("disconnected-dprintf", no_class,
14833 &disconnected_dprintf, _("\
14834Set whether dprintf continues after GDB disconnects."), _("\
14835Show whether dprintf continues after GDB disconnects."), _("\
14836Use this to let dprintf commands continue to hit and produce output\n\
14837even if GDB disconnects or detaches from the target."),
14838 NULL,
14839 NULL,
14840 &setlist, &showlist);
14841
14842 add_com ("agent-printf", class_vars, agent_printf_command, _("\
590042fc
PW
14843Target agent only formatted printing, like the C \"printf\" function.\n\
14844Usage: agent-printf \"format string\", ARG1, ARG2, ARG3, ..., ARGN\n\
14845This supports most C printf format specifications, like %s, %d, etc.\n\
14846This is useful for formatted output in user-defined commands."));
d3ce09f5 14847
491144b5 14848 automatic_hardware_breakpoints = true;
f3b1572e 14849
c90e7d63
SM
14850 gdb::observers::about_to_proceed.attach (breakpoint_about_to_proceed,
14851 "breakpoint");
14852 gdb::observers::thread_exit.attach (remove_threaded_breakpoints,
14853 "breakpoint");
c906108c 14854}