]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/breakpoint.c
gdb: re-format Python files using black 21.4b0
[thirdparty/binutils-gdb.git] / gdb / breakpoint.c
CommitLineData
c906108c 1/* Everything about breakpoints, for GDB.
8926118c 2
3666a048 3 Copyright (C) 1986-2021 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"
4de283e4 59#include "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"
d3ce09f5 70
1042e4c0 71/* readline include files */
073bbbb0 72#include "readline/tilde.h"
1042e4c0
SS
73
74/* readline defines this. */
75#undef savestring
76
034dad6f 77#include "mi/mi-common.h"
6dddc817 78#include "extension.h"
325fac50 79#include <algorithm>
5ed8105e 80#include "progspace-and-thread.h"
268a13a5
TT
81#include "gdbsupport/array-view.h"
82#include "gdbsupport/gdb_optional.h"
104c1213 83
4a64f543 84/* Prototypes for local functions. */
c906108c 85
896b6bda 86static void map_breakpoint_numbers (const char *,
48649e1b 87 gdb::function_view<void (breakpoint *)>);
c906108c 88
348d480f
PA
89static void breakpoint_re_set_default (struct breakpoint *);
90
f00aae0f 91static void
626d2320 92 create_sals_from_location_default (struct event_location *location,
f00aae0f
KS
93 struct linespec_result *canonical,
94 enum bptype type_wanted);
983af33b
SDJ
95
96static void create_breakpoints_sal_default (struct gdbarch *,
97 struct linespec_result *,
e1e01040
PA
98 gdb::unique_xmalloc_ptr<char>,
99 gdb::unique_xmalloc_ptr<char>,
100 enum bptype,
983af33b
SDJ
101 enum bpdisp, int, int,
102 int,
103 const struct breakpoint_ops *,
44f238bb 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
a14ed312 113static void mention (struct breakpoint *);
c906108c 114
348d480f
PA
115static struct breakpoint *set_raw_breakpoint_without_location (struct gdbarch *,
116 enum bptype,
c0a91b2b 117 const struct breakpoint_ops *);
3742cc8b
YQ
118static struct bp_location *add_location_to_breakpoint (struct breakpoint *,
119 const struct symtab_and_line *);
120
4a64f543
MS
121/* This function is used in gdbtk sources and thus can not be made
122 static. */
63c252f8 123struct breakpoint *set_raw_breakpoint (struct gdbarch *gdbarch,
348d480f 124 struct symtab_and_line,
c0a91b2b
TT
125 enum bptype,
126 const struct breakpoint_ops *);
c906108c 127
06edf0c0
PA
128static struct breakpoint *
129 momentary_breakpoint_from_master (struct breakpoint *orig,
130 enum bptype type,
a1aa2221
LM
131 const struct breakpoint_ops *ops,
132 int loc_enabled);
06edf0c0 133
76897487
KB
134static void breakpoint_adjustment_warning (CORE_ADDR, CORE_ADDR, int, int);
135
a6d9a66e
UW
136static CORE_ADDR adjust_breakpoint_address (struct gdbarch *gdbarch,
137 CORE_ADDR bpaddr,
dda83cd7 138 enum bptype bptype);
76897487 139
6c95b8df
PA
140static void describe_other_breakpoints (struct gdbarch *,
141 struct program_space *, CORE_ADDR,
5af949e3 142 struct obj_section *, int);
c906108c 143
85d721b8
PA
144static int watchpoint_locations_match (struct bp_location *loc1,
145 struct bp_location *loc2);
146
7f32a4d5
PA
147static int breakpoint_locations_match (struct bp_location *loc1,
148 struct bp_location *loc2,
149 bool sw_hw_bps_match = false);
150
f1310107 151static int breakpoint_location_address_match (struct bp_location *bl,
accd0bcd 152 const struct address_space *aspace,
f1310107
TJB
153 CORE_ADDR addr);
154
d35ae833 155static int breakpoint_location_address_range_overlap (struct bp_location *,
accd0bcd 156 const address_space *,
d35ae833
PA
157 CORE_ADDR, int);
158
834c0d03 159static int remove_breakpoint (struct bp_location *);
b2b6a7da 160static int remove_breakpoint_1 (struct bp_location *, enum remove_bp_reason);
c906108c 161
e514a9d6 162static enum print_stop_action print_bp_stop_message (bpstat bs);
c906108c 163
a14ed312 164static int hw_breakpoint_used_count (void);
c906108c 165
a1398e0c
PA
166static int hw_watchpoint_use_count (struct breakpoint *);
167
168static int hw_watchpoint_used_count_others (struct breakpoint *except,
169 enum bptype type,
170 int *other_type_used);
c906108c 171
816338b5
SS
172static void enable_breakpoint_disp (struct breakpoint *, enum bpdisp,
173 int count);
c906108c 174
f431efe5 175static void decref_bp_location (struct bp_location **loc);
fe3f5fa8 176
39d61571 177static struct bp_location *allocate_bp_location (struct breakpoint *bpt);
a5606eee 178
44702360
PA
179/* update_global_location_list's modes of operation wrt to whether to
180 insert locations now. */
181enum ugll_insert_mode
182{
183 /* Don't insert any breakpoint locations into the inferior, only
184 remove already-inserted locations that no longer should be
185 inserted. Functions that delete a breakpoint or breakpoints
186 should specify this mode, so that deleting a breakpoint doesn't
187 have the side effect of inserting the locations of other
188 breakpoints that are marked not-inserted, but should_be_inserted
189 returns true on them.
190
191 This behavior is useful is situations close to tear-down -- e.g.,
192 after an exec, while the target still has execution, but
193 breakpoint shadows of the previous executable image should *NOT*
194 be restored to the new image; or before detaching, where the
195 target still has execution and wants to delete breakpoints from
196 GDB's lists, and all breakpoints had already been removed from
197 the inferior. */
198 UGLL_DONT_INSERT,
199
a25a5a45
PA
200 /* May insert breakpoints iff breakpoints_should_be_inserted_now
201 claims breakpoints should be inserted now. */
04086b45
PA
202 UGLL_MAY_INSERT,
203
a25a5a45
PA
204 /* Insert locations now, irrespective of
205 breakpoints_should_be_inserted_now. E.g., say all threads are
206 stopped right now, and the user did "continue". We need to
207 insert breakpoints _before_ resuming the target, but
208 UGLL_MAY_INSERT wouldn't insert them, because
209 breakpoints_should_be_inserted_now returns false at that point,
210 as no thread is running yet. */
04086b45 211 UGLL_INSERT
44702360
PA
212};
213
214static void update_global_location_list (enum ugll_insert_mode);
a5606eee 215
44702360 216static void update_global_location_list_nothrow (enum ugll_insert_mode);
74960c60 217
74960c60 218static void insert_breakpoint_locations (void);
a5606eee 219
0b39b52e 220static void trace_pass_command (const char *, int);
1042e4c0 221
558a9d82
YQ
222static void set_tracepoint_count (int num);
223
f2478a7e 224static bool is_masked_watchpoint (const struct breakpoint *b);
9c06b0b4 225
b775012e
LM
226static struct bp_location **get_first_locp_gte_addr (CORE_ADDR address);
227
983af33b
SDJ
228/* Return 1 if B refers to a static tracepoint set by marker ("-m"), zero
229 otherwise. */
230
231static int strace_marker_p (struct breakpoint *b);
0fb4aa4b 232
2060206e
PA
233/* The breakpoint_ops structure to be inherited by all breakpoint_ops
234 that are implemented on top of software or hardware breakpoints
235 (user breakpoints, internal and momentary breakpoints, etc.). */
236static struct breakpoint_ops bkpt_base_breakpoint_ops;
237
238/* Internal breakpoints class type. */
06edf0c0 239static struct breakpoint_ops internal_breakpoint_ops;
2060206e
PA
240
241/* Momentary breakpoints class type. */
06edf0c0
PA
242static struct breakpoint_ops momentary_breakpoint_ops;
243
2060206e
PA
244/* The breakpoint_ops structure to be used in regular user created
245 breakpoints. */
246struct breakpoint_ops bkpt_breakpoint_ops;
247
55aa24fb
SDJ
248/* Breakpoints set on probes. */
249static struct breakpoint_ops bkpt_probe_breakpoint_ops;
250
bac7c5cf
GB
251/* Tracepoints set on probes. */
252static struct breakpoint_ops tracepoint_probe_breakpoint_ops;
253
e7e0cddf 254/* Dynamic printf class type. */
c5867ab6 255struct breakpoint_ops dprintf_breakpoint_ops;
e7e0cddf 256
d3ce09f5
SS
257/* The style in which to perform a dynamic printf. This is a user
258 option because different output options have different tradeoffs;
259 if GDB does the printing, there is better error handling if there
260 is a problem with any of the arguments, but using an inferior
261 function lets you have special-purpose printers and sending of
262 output to the same place as compiled-in print functions. */
263
264static const char dprintf_style_gdb[] = "gdb";
265static const char dprintf_style_call[] = "call";
266static const char dprintf_style_agent[] = "agent";
267static const char *const dprintf_style_enums[] = {
268 dprintf_style_gdb,
269 dprintf_style_call,
270 dprintf_style_agent,
271 NULL
272};
273static const char *dprintf_style = dprintf_style_gdb;
274
275/* The function to use for dynamic printf if the preferred style is to
276 call into the inferior. The value is simply a string that is
277 copied into the command, so it can be anything that GDB can
278 evaluate to a callable address, not necessarily a function name. */
279
bde6261a 280static char *dprintf_function;
d3ce09f5
SS
281
282/* The channel to use for dynamic printf if the preferred style is to
283 call into the inferior; if a nonempty string, it will be passed to
284 the call as the first argument, with the format string as the
285 second. As with the dprintf function, this can be anything that
286 GDB knows how to evaluate, so in addition to common choices like
287 "stderr", this could be an app-specific expression like
288 "mystreams[curlogger]". */
289
bde6261a 290static char *dprintf_channel;
d3ce09f5
SS
291
292/* True if dprintf commands should continue to operate even if GDB
293 has disconnected. */
491144b5 294static bool disconnected_dprintf = true;
d3ce09f5 295
5cea2a26
PA
296struct command_line *
297breakpoint_commands (struct breakpoint *b)
298{
d1b0a7bf 299 return b->commands ? b->commands.get () : NULL;
5cea2a26 300}
3daf8fe5 301
f3b1572e
PA
302/* Flag indicating that a command has proceeded the inferior past the
303 current breakpoint. */
304
491144b5 305static bool breakpoint_proceeded;
f3b1572e 306
956a9fb9 307const char *
2cec12e5
AR
308bpdisp_text (enum bpdisp disp)
309{
4a64f543
MS
310 /* NOTE: the following values are a part of MI protocol and
311 represent values of 'disp' field returned when inferior stops at
312 a breakpoint. */
bc043ef3 313 static const char * const bpdisps[] = {"del", "dstp", "dis", "keep"};
cc59ec59 314
2cec12e5
AR
315 return bpdisps[(int) disp];
316}
c906108c 317
4a64f543 318/* Prototypes for exported functions. */
c906108c 319/* If FALSE, gdb will not use hardware support for watchpoints, even
4a64f543 320 if such is available. */
c906108c
SS
321static int can_use_hw_watchpoints;
322
920d2a44
AC
323static void
324show_can_use_hw_watchpoints (struct ui_file *file, int from_tty,
325 struct cmd_list_element *c,
326 const char *value)
327{
3e43a32a
MS
328 fprintf_filtered (file,
329 _("Debugger's willingness to use "
330 "watchpoint hardware is %s.\n"),
920d2a44
AC
331 value);
332}
333
fa8d40ab
JJ
334/* If AUTO_BOOLEAN_FALSE, gdb will not attempt to create pending breakpoints.
335 If AUTO_BOOLEAN_TRUE, gdb will automatically create pending breakpoints
4a64f543 336 for unrecognized breakpoint locations.
fa8d40ab
JJ
337 If AUTO_BOOLEAN_AUTO, gdb will query when breakpoints are unrecognized. */
338static enum auto_boolean pending_break_support;
920d2a44
AC
339static void
340show_pending_break_support (struct ui_file *file, int from_tty,
341 struct cmd_list_element *c,
342 const char *value)
343{
3e43a32a
MS
344 fprintf_filtered (file,
345 _("Debugger's behavior regarding "
346 "pending breakpoints is %s.\n"),
920d2a44
AC
347 value);
348}
fa8d40ab 349
491144b5 350/* If true, gdb will automatically use hardware breakpoints for breakpoints
4a64f543 351 set with "break" but falling in read-only memory.
491144b5 352 If false, gdb will warn about such breakpoints, but won't automatically
765dc015 353 use hardware breakpoints. */
491144b5 354static bool automatic_hardware_breakpoints;
765dc015
VP
355static void
356show_automatic_hardware_breakpoints (struct ui_file *file, int from_tty,
357 struct cmd_list_element *c,
358 const char *value)
359{
3e43a32a
MS
360 fprintf_filtered (file,
361 _("Automatic usage of hardware breakpoints is %s.\n"),
765dc015
VP
362 value);
363}
364
a25a5a45
PA
365/* If on, GDB keeps breakpoints inserted even if the inferior is
366 stopped, and immediately inserts any new breakpoints as soon as
367 they're created. If off (default), GDB keeps breakpoints off of
368 the target as long as possible. That is, it delays inserting
369 breakpoints until the next resume, and removes them again when the
370 target fully stops. This is a bit safer in case GDB crashes while
371 processing user input. */
491144b5 372static bool always_inserted_mode = false;
72d0e2c5 373
33e5cbd6 374static void
74960c60 375show_always_inserted_mode (struct ui_file *file, int from_tty,
33e5cbd6 376 struct cmd_list_element *c, const char *value)
74960c60 377{
a25a5a45
PA
378 fprintf_filtered (file, _("Always inserted breakpoint mode is %s.\n"),
379 value);
74960c60
VP
380}
381
b57bacec
PA
382/* See breakpoint.h. */
383
33e5cbd6 384int
a25a5a45 385breakpoints_should_be_inserted_now (void)
33e5cbd6 386{
a25a5a45
PA
387 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
388 {
389 /* If breakpoints are global, they should be inserted even if no
390 thread under gdb's control is running, or even if there are
391 no threads under GDB's control yet. */
392 return 1;
393 }
5b6d1e4f 394 else
a25a5a45 395 {
a25a5a45
PA
396 if (always_inserted_mode)
397 {
398 /* The user wants breakpoints inserted even if all threads
399 are stopped. */
400 return 1;
401 }
402
5b6d1e4f
PA
403 for (inferior *inf : all_inferiors ())
404 if (inf->has_execution ()
405 && threads_are_executing (inf->process_target ()))
406 return 1;
372316f1
PA
407
408 /* Don't remove breakpoints yet if, even though all threads are
409 stopped, we still have events to process. */
08036331 410 for (thread_info *tp : all_non_exited_threads ())
372316f1
PA
411 if (tp->resumed
412 && tp->suspend.waitstatus_pending_p)
413 return 1;
a25a5a45
PA
414 }
415 return 0;
33e5cbd6 416}
765dc015 417
b775012e
LM
418static const char condition_evaluation_both[] = "host or target";
419
420/* Modes for breakpoint condition evaluation. */
421static const char condition_evaluation_auto[] = "auto";
422static const char condition_evaluation_host[] = "host";
423static const char condition_evaluation_target[] = "target";
424static const char *const condition_evaluation_enums[] = {
425 condition_evaluation_auto,
426 condition_evaluation_host,
427 condition_evaluation_target,
428 NULL
429};
430
431/* Global that holds the current mode for breakpoint condition evaluation. */
432static const char *condition_evaluation_mode_1 = condition_evaluation_auto;
433
434/* Global that we use to display information to the user (gets its value from
435 condition_evaluation_mode_1. */
436static const char *condition_evaluation_mode = condition_evaluation_auto;
437
438/* Translate a condition evaluation mode MODE into either "host"
439 or "target". This is used mostly to translate from "auto" to the
440 real setting that is being used. It returns the translated
441 evaluation mode. */
442
443static const char *
444translate_condition_evaluation_mode (const char *mode)
445{
446 if (mode == condition_evaluation_auto)
447 {
448 if (target_supports_evaluation_of_breakpoint_conditions ())
449 return condition_evaluation_target;
450 else
451 return condition_evaluation_host;
452 }
453 else
454 return mode;
455}
456
457/* Discovers what condition_evaluation_auto translates to. */
458
459static const char *
460breakpoint_condition_evaluation_mode (void)
461{
462 return translate_condition_evaluation_mode (condition_evaluation_mode);
463}
464
465/* Return true if GDB should evaluate breakpoint conditions or false
466 otherwise. */
467
468static int
469gdb_evaluates_breakpoint_condition_p (void)
470{
471 const char *mode = breakpoint_condition_evaluation_mode ();
472
473 return (mode == condition_evaluation_host);
474}
475
c906108c
SS
476/* Are we executing breakpoint commands? */
477static int executing_breakpoint_commands;
478
c02f5703
MS
479/* Are overlay event breakpoints enabled? */
480static int overlay_events_enabled;
481
e09342b5 482/* See description in breakpoint.h. */
491144b5 483bool target_exact_watchpoints = false;
e09342b5 484
c906108c 485/* Walk the following statement or block through all breakpoints.
e5dd4106 486 ALL_BREAKPOINTS_SAFE does so even if the statement deletes the
4a64f543 487 current breakpoint. */
c906108c 488
5c44784c 489#define ALL_BREAKPOINTS(B) for (B = breakpoint_chain; B; B = B->next)
c906108c 490
5c44784c
JM
491#define ALL_BREAKPOINTS_SAFE(B,TMP) \
492 for (B = breakpoint_chain; \
493 B ? (TMP=B->next, 1): 0; \
494 B = TMP)
c906108c 495
4a64f543
MS
496/* Similar iterator for the low-level breakpoints. SAFE variant is
497 not provided so update_global_location_list must not be called
498 while executing the block of ALL_BP_LOCATIONS. */
7cc221ef 499
876fa593 500#define ALL_BP_LOCATIONS(B,BP_TMP) \
f5336ca5
PA
501 for (BP_TMP = bp_locations; \
502 BP_TMP < bp_locations + bp_locations_count && (B = *BP_TMP);\
876fa593 503 BP_TMP++)
7cc221ef 504
b775012e
LM
505/* Iterates through locations with address ADDRESS for the currently selected
506 program space. BP_LOCP_TMP points to each object. BP_LOCP_START points
507 to where the loop should start from.
508 If BP_LOCP_START is a NULL pointer, the macro automatically seeks the
509 appropriate location to start with. */
510
511#define ALL_BP_LOCATIONS_AT_ADDR(BP_LOCP_TMP, BP_LOCP_START, ADDRESS) \
512 for (BP_LOCP_START = BP_LOCP_START == NULL ? get_first_locp_gte_addr (ADDRESS) : BP_LOCP_START, \
513 BP_LOCP_TMP = BP_LOCP_START; \
514 BP_LOCP_START \
f5336ca5 515 && (BP_LOCP_TMP < bp_locations + bp_locations_count \
b775012e
LM
516 && (*BP_LOCP_TMP)->address == ADDRESS); \
517 BP_LOCP_TMP++)
518
1042e4c0
SS
519/* Iterator for tracepoints only. */
520
521#define ALL_TRACEPOINTS(B) \
522 for (B = breakpoint_chain; B; B = B->next) \
d77f58be 523 if (is_tracepoint (B))
1042e4c0 524
7cc221ef 525/* Chains of all breakpoints defined. */
c906108c 526
81e6b8eb 527static struct breakpoint *breakpoint_chain;
c906108c 528
39ef2f62 529/* Array is sorted by bp_location_is_less_than - primarily by the ADDRESS. */
876fa593 530
f5336ca5 531static struct bp_location **bp_locations;
876fa593 532
f5336ca5 533/* Number of elements of BP_LOCATIONS. */
876fa593 534
f5336ca5 535static unsigned bp_locations_count;
876fa593 536
4a64f543 537/* Maximum alignment offset between bp_target_info.PLACED_ADDRESS and
f5336ca5 538 ADDRESS for the current elements of BP_LOCATIONS which get a valid
4a64f543 539 result from bp_location_has_shadow. You can use it for roughly
f5336ca5 540 limiting the subrange of BP_LOCATIONS to scan for shadow bytes for
4a64f543 541 an address you need to read. */
876fa593 542
f5336ca5 543static CORE_ADDR bp_locations_placed_address_before_address_max;
876fa593 544
4a64f543
MS
545/* Maximum offset plus alignment between bp_target_info.PLACED_ADDRESS
546 + bp_target_info.SHADOW_LEN and ADDRESS for the current elements of
f5336ca5
PA
547 BP_LOCATIONS which get a valid result from bp_location_has_shadow.
548 You can use it for roughly limiting the subrange of BP_LOCATIONS to
4a64f543 549 scan for shadow bytes for an address you need to read. */
876fa593 550
f5336ca5 551static CORE_ADDR bp_locations_shadow_len_after_address_max;
7cc221ef 552
4a64f543 553/* The locations that no longer correspond to any breakpoint, unlinked
f5336ca5
PA
554 from the bp_locations array, but for which a hit may still be
555 reported by a target. */
1123588c 556static std::vector<bp_location *> moribund_locations;
20874c92 557
c906108c
SS
558/* Number of last breakpoint made. */
559
95a42b64
TT
560static int breakpoint_count;
561
86b17b60
PA
562/* The value of `breakpoint_count' before the last command that
563 created breakpoints. If the last (break-like) command created more
564 than one breakpoint, then the difference between BREAKPOINT_COUNT
565 and PREV_BREAKPOINT_COUNT is more than one. */
566static int prev_breakpoint_count;
c906108c 567
1042e4c0
SS
568/* Number of last tracepoint made. */
569
95a42b64 570static int tracepoint_count;
1042e4c0 571
6149aea9
PA
572static struct cmd_list_element *breakpoint_set_cmdlist;
573static struct cmd_list_element *breakpoint_show_cmdlist;
9291a0cd 574struct cmd_list_element *save_cmdlist;
6149aea9 575
badd37ce
SDJ
576/* See declaration at breakpoint.h. */
577
578struct breakpoint *
579breakpoint_find_if (int (*func) (struct breakpoint *b, void *d),
580 void *user_data)
581{
582 struct breakpoint *b = NULL;
583
584 ALL_BREAKPOINTS (b)
585 {
586 if (func (b, user_data) != 0)
587 break;
588 }
589
590 return b;
591}
592
468d015d
JJ
593/* Return whether a breakpoint is an active enabled breakpoint. */
594static int
595breakpoint_enabled (struct breakpoint *b)
596{
0d381245 597 return (b->enable_state == bp_enabled);
468d015d
JJ
598}
599
c906108c
SS
600/* Set breakpoint count to NUM. */
601
95a42b64 602static void
fba45db2 603set_breakpoint_count (int num)
c906108c 604{
86b17b60 605 prev_breakpoint_count = breakpoint_count;
c906108c 606 breakpoint_count = num;
4fa62494 607 set_internalvar_integer (lookup_internalvar ("bpnum"), num);
c906108c
SS
608}
609
86b17b60
PA
610/* Used by `start_rbreak_breakpoints' below, to record the current
611 breakpoint count before "rbreak" creates any breakpoint. */
612static int rbreak_start_breakpoint_count;
613
95a42b64
TT
614/* Called at the start an "rbreak" command to record the first
615 breakpoint made. */
86b17b60 616
c80049d3 617scoped_rbreak_breakpoints::scoped_rbreak_breakpoints ()
95a42b64 618{
86b17b60 619 rbreak_start_breakpoint_count = breakpoint_count;
95a42b64
TT
620}
621
622/* Called at the end of an "rbreak" command to record the last
623 breakpoint made. */
86b17b60 624
c80049d3 625scoped_rbreak_breakpoints::~scoped_rbreak_breakpoints ()
95a42b64 626{
86b17b60 627 prev_breakpoint_count = rbreak_start_breakpoint_count;
95a42b64
TT
628}
629
4a64f543 630/* Used in run_command to zero the hit count when a new run starts. */
c906108c
SS
631
632void
fba45db2 633clear_breakpoint_hit_counts (void)
c906108c
SS
634{
635 struct breakpoint *b;
636
637 ALL_BREAKPOINTS (b)
638 b->hit_count = 0;
639}
640
c906108c 641\f
48cb2d85
VP
642/* Return the breakpoint with the specified number, or NULL
643 if the number does not refer to an existing breakpoint. */
644
645struct breakpoint *
646get_breakpoint (int num)
647{
648 struct breakpoint *b;
649
650 ALL_BREAKPOINTS (b)
651 if (b->number == num)
652 return b;
653
654 return NULL;
655}
5c44784c 656
c906108c 657\f
adc36818 658
b775012e
LM
659/* Mark locations as "conditions have changed" in case the target supports
660 evaluating conditions on its side. */
661
662static void
663mark_breakpoint_modified (struct breakpoint *b)
664{
665 struct bp_location *loc;
666
667 /* This is only meaningful if the target is
668 evaluating conditions and if the user has
669 opted for condition evaluation on the target's
670 side. */
671 if (gdb_evaluates_breakpoint_condition_p ()
672 || !target_supports_evaluation_of_breakpoint_conditions ())
673 return;
674
675 if (!is_breakpoint (b))
676 return;
677
678 for (loc = b->loc; loc; loc = loc->next)
679 loc->condition_changed = condition_modified;
680}
681
682/* Mark location as "conditions have changed" in case the target supports
683 evaluating conditions on its side. */
684
685static void
686mark_breakpoint_location_modified (struct bp_location *loc)
687{
688 /* This is only meaningful if the target is
689 evaluating conditions and if the user has
690 opted for condition evaluation on the target's
691 side. */
692 if (gdb_evaluates_breakpoint_condition_p ()
693 || !target_supports_evaluation_of_breakpoint_conditions ())
694
695 return;
696
697 if (!is_breakpoint (loc->owner))
698 return;
699
700 loc->condition_changed = condition_modified;
701}
702
703/* Sets the condition-evaluation mode using the static global
704 condition_evaluation_mode. */
705
706static void
eb4c3f4a 707set_condition_evaluation_mode (const char *args, int from_tty,
b775012e
LM
708 struct cmd_list_element *c)
709{
b775012e
LM
710 const char *old_mode, *new_mode;
711
712 if ((condition_evaluation_mode_1 == condition_evaluation_target)
713 && !target_supports_evaluation_of_breakpoint_conditions ())
714 {
715 condition_evaluation_mode_1 = condition_evaluation_mode;
716 warning (_("Target does not support breakpoint condition evaluation.\n"
717 "Using host evaluation mode instead."));
718 return;
719 }
720
721 new_mode = translate_condition_evaluation_mode (condition_evaluation_mode_1);
722 old_mode = translate_condition_evaluation_mode (condition_evaluation_mode);
723
abf1152a
JK
724 /* Flip the switch. Flip it even if OLD_MODE == NEW_MODE as one of the
725 settings was "auto". */
726 condition_evaluation_mode = condition_evaluation_mode_1;
727
b775012e
LM
728 /* Only update the mode if the user picked a different one. */
729 if (new_mode != old_mode)
730 {
731 struct bp_location *loc, **loc_tmp;
732 /* If the user switched to a different evaluation mode, we
733 need to synch the changes with the target as follows:
734
735 "host" -> "target": Send all (valid) conditions to the target.
736 "target" -> "host": Remove all the conditions from the target.
737 */
738
b775012e
LM
739 if (new_mode == condition_evaluation_target)
740 {
741 /* Mark everything modified and synch conditions with the
742 target. */
743 ALL_BP_LOCATIONS (loc, loc_tmp)
744 mark_breakpoint_location_modified (loc);
745 }
746 else
747 {
748 /* Manually mark non-duplicate locations to synch conditions
749 with the target. We do this to remove all the conditions the
750 target knows about. */
751 ALL_BP_LOCATIONS (loc, loc_tmp)
752 if (is_breakpoint (loc->owner) && loc->inserted)
753 loc->needs_update = 1;
754 }
755
756 /* Do the update. */
44702360 757 update_global_location_list (UGLL_MAY_INSERT);
b775012e
LM
758 }
759
760 return;
761}
762
763/* Shows the current mode of breakpoint condition evaluation. Explicitly shows
764 what "auto" is translating to. */
765
766static void
767show_condition_evaluation_mode (struct ui_file *file, int from_tty,
768 struct cmd_list_element *c, const char *value)
769{
770 if (condition_evaluation_mode == condition_evaluation_auto)
771 fprintf_filtered (file,
772 _("Breakpoint condition evaluation "
773 "mode is %s (currently %s).\n"),
774 value,
775 breakpoint_condition_evaluation_mode ());
776 else
777 fprintf_filtered (file, _("Breakpoint condition evaluation mode is %s.\n"),
778 value);
779}
780
781/* A comparison function for bp_location AP and BP that is used by
782 bsearch. This comparison function only cares about addresses, unlike
39ef2f62 783 the more general bp_location_is_less_than function. */
b775012e
LM
784
785static int
f5336ca5 786bp_locations_compare_addrs (const void *ap, const void *bp)
b775012e 787{
9a3c8263
SM
788 const struct bp_location *a = *(const struct bp_location **) ap;
789 const struct bp_location *b = *(const struct bp_location **) bp;
b775012e
LM
790
791 if (a->address == b->address)
792 return 0;
793 else
794 return ((a->address > b->address) - (a->address < b->address));
795}
796
797/* Helper function to skip all bp_locations with addresses
798 less than ADDRESS. It returns the first bp_location that
799 is greater than or equal to ADDRESS. If none is found, just
800 return NULL. */
801
802static struct bp_location **
803get_first_locp_gte_addr (CORE_ADDR address)
804{
805 struct bp_location dummy_loc;
806 struct bp_location *dummy_locp = &dummy_loc;
807 struct bp_location **locp_found = NULL;
808
809 /* Initialize the dummy location's address field. */
b775012e
LM
810 dummy_loc.address = address;
811
812 /* Find a close match to the first location at ADDRESS. */
9a3c8263 813 locp_found = ((struct bp_location **)
f5336ca5 814 bsearch (&dummy_locp, bp_locations, bp_locations_count,
9a3c8263 815 sizeof (struct bp_location **),
f5336ca5 816 bp_locations_compare_addrs));
b775012e
LM
817
818 /* Nothing was found, nothing left to do. */
819 if (locp_found == NULL)
820 return NULL;
821
822 /* We may have found a location that is at ADDRESS but is not the first in the
823 location's list. Go backwards (if possible) and locate the first one. */
f5336ca5 824 while ((locp_found - 1) >= bp_locations
b775012e
LM
825 && (*(locp_found - 1))->address == address)
826 locp_found--;
827
828 return locp_found;
829}
830
b5fa468f
TBA
831/* Parse COND_STRING in the context of LOC and set as the condition
832 expression of LOC. BP_NUM is the number of LOC's owner, LOC_NUM is
833 the number of LOC within its owner. In case of parsing error, mark
834 LOC as DISABLED_BY_COND. In case of success, unset DISABLED_BY_COND. */
835
836static void
837set_breakpoint_location_condition (const char *cond_string, bp_location *loc,
838 int bp_num, int loc_num)
839{
840 bool has_junk = false;
841 try
842 {
843 expression_up new_exp = parse_exp_1 (&cond_string, loc->address,
844 block_for_pc (loc->address), 0);
845 if (*cond_string != 0)
846 has_junk = true;
847 else
848 {
849 loc->cond = std::move (new_exp);
850 if (loc->disabled_by_cond && loc->enabled)
851 printf_filtered (_("Breakpoint %d's condition is now valid at "
852 "location %d, enabling.\n"),
853 bp_num, loc_num);
854
855 loc->disabled_by_cond = false;
856 }
857 }
858 catch (const gdb_exception_error &e)
859 {
860 if (loc->enabled)
861 {
862 /* Warn if a user-enabled location is now becoming disabled-by-cond.
863 BP_NUM is 0 if the breakpoint is being defined for the first
864 time using the "break ... if ..." command, and non-zero if
865 already defined. */
866 if (bp_num != 0)
867 warning (_("failed to validate condition at location %d.%d, "
868 "disabling:\n %s"), bp_num, loc_num, e.what ());
869 else
870 warning (_("failed to validate condition at location %d, "
871 "disabling:\n %s"), loc_num, e.what ());
872 }
873
874 loc->disabled_by_cond = true;
875 }
876
877 if (has_junk)
878 error (_("Garbage '%s' follows condition"), cond_string);
879}
880
adc36818 881void
7a26bd4d 882set_breakpoint_condition (struct breakpoint *b, const char *exp,
733d554a 883 int from_tty, bool force)
adc36818 884{
4c55e970 885 if (*exp == 0)
3a5c3e22 886 {
4c55e970
TBA
887 xfree (b->cond_string);
888 b->cond_string = nullptr;
3a5c3e22 889
4c55e970 890 if (is_watchpoint (b))
78319c15 891 static_cast<watchpoint *> (b)->cond_exp.reset ();
4c55e970
TBA
892 else
893 {
b5fa468f 894 int loc_num = 1;
78319c15 895 for (bp_location *loc = b->loc; loc != nullptr; loc = loc->next)
4c55e970
TBA
896 {
897 loc->cond.reset ();
b5fa468f
TBA
898 if (loc->disabled_by_cond && loc->enabled)
899 printf_filtered (_("Breakpoint %d's condition is now valid at "
900 "location %d, enabling.\n"),
901 b->number, loc_num);
902 loc->disabled_by_cond = false;
903 loc_num++;
4c55e970
TBA
904
905 /* No need to free the condition agent expression
906 bytecode (if we have one). We will handle this
907 when we go through update_global_location_list. */
908 }
909 }
1e620590 910
adc36818
PM
911 if (from_tty)
912 printf_filtered (_("Breakpoint %d now unconditional.\n"), b->number);
913 }
914 else
915 {
adc36818
PM
916 if (is_watchpoint (b))
917 {
699bd4cf 918 innermost_block_tracker tracker;
78319c15 919 const char *arg = exp;
4c55e970 920 expression_up new_exp = parse_exp_1 (&arg, 0, 0, 0, &tracker);
78319c15 921 if (*arg != 0)
adc36818 922 error (_("Junk at end of expression"));
78319c15 923 watchpoint *w = static_cast<watchpoint *> (b);
4c55e970 924 w->cond_exp = std::move (new_exp);
699bd4cf 925 w->cond_exp_valid_block = tracker.block ();
adc36818
PM
926 }
927 else
928 {
4c55e970
TBA
929 /* Parse and set condition expressions. We make two passes.
930 In the first, we parse the condition string to see if it
b5fa468f
TBA
931 is valid in at least one location. If so, the condition
932 would be accepted. So we go ahead and set the locations'
933 conditions. In case no valid case is found, we throw
4c55e970
TBA
934 the error and the condition string will be rejected.
935 This two-pass approach is taken to avoid setting the
936 state of locations in case of a reject. */
78319c15 937 for (bp_location *loc = b->loc; loc != nullptr; loc = loc->next)
4c55e970 938 {
b5fa468f
TBA
939 try
940 {
941 const char *arg = exp;
942 parse_exp_1 (&arg, loc->address,
943 block_for_pc (loc->address), 0);
944 if (*arg != 0)
945 error (_("Junk at end of expression"));
946 break;
947 }
948 catch (const gdb_exception_error &e)
949 {
950 /* Condition string is invalid. If this happens to
733d554a
TBA
951 be the last loc, abandon (if not forced) or continue
952 (if forced). */
953 if (loc->next == nullptr && !force)
b5fa468f
TBA
954 throw;
955 }
4c55e970
TBA
956 }
957
b5fa468f 958 /* If we reach here, the condition is valid at some locations. */
dda83cd7
SM
959 int loc_num = 1;
960 for (bp_location *loc = b->loc; loc != nullptr;
961 loc = loc->next, loc_num++)
962 set_breakpoint_location_condition (exp, loc, b->number, loc_num);
adc36818 963 }
1e620590
TBA
964
965 /* We know that the new condition parsed successfully. The
966 condition string of the breakpoint can be safely updated. */
967 xfree (b->cond_string);
968 b->cond_string = xstrdup (exp);
969 b->condition_not_parsed = 0;
adc36818 970 }
b775012e
LM
971 mark_breakpoint_modified (b);
972
76727919 973 gdb::observers::breakpoint_modified.notify (b);
adc36818
PM
974}
975
79aabb73
TBA
976/* See breakpoint.h. */
977
978void
979set_breakpoint_condition (int bpnum, const char *exp, int from_tty,
980 bool force)
981{
982 struct breakpoint *b;
983 ALL_BREAKPOINTS (b)
984 if (b->number == bpnum)
985 {
986 /* Check if this breakpoint has a "stop" method implemented in an
987 extension language. This method and conditions entered into GDB
988 from the CLI are mutually exclusive. */
989 const struct extension_language_defn *extlang
990 = get_breakpoint_cond_ext_lang (b, EXT_LANG_NONE);
991
992 if (extlang != NULL)
993 {
994 error (_("Only one stop condition allowed. There is currently"
995 " a %s stop condition defined for this breakpoint."),
996 ext_lang_capitalized_name (extlang));
997 }
998 set_breakpoint_condition (b, exp, from_tty, force);
999
1000 if (is_breakpoint (b))
1001 update_global_location_list (UGLL_MAY_INSERT);
1002
1003 return;
1004 }
1005
1006 error (_("No breakpoint number %d."), bpnum);
1007}
1008
b1d4d8d1
TBA
1009/* The options for the "condition" command. */
1010
1011struct condition_command_opts
1012{
1013 /* For "-force". */
1014 bool force_condition = false;
1015};
1016
1017static const gdb::option::option_def condition_command_option_defs[] = {
1018
1019 gdb::option::flag_option_def<condition_command_opts> {
1020 "force",
1021 [] (condition_command_opts *opts) { return &opts->force_condition; },
1022 N_("Set the condition even if it is invalid for all current locations."),
1023 },
1024
1025};
1026
1027/* Create an option_def_group for the "condition" options, with
1028 CC_OPTS as context. */
1029
1030static inline gdb::option::option_def_group
1031make_condition_command_options_def_group (condition_command_opts *cc_opts)
1032{
1033 return {{condition_command_option_defs}, cc_opts};
1034}
1035
d55637df
TT
1036/* Completion for the "condition" command. */
1037
eb3ff9a5 1038static void
6f937416 1039condition_completer (struct cmd_list_element *cmd,
eb3ff9a5 1040 completion_tracker &tracker,
b1d4d8d1 1041 const char *text, const char * /*word*/)
d55637df 1042{
b1d4d8d1
TBA
1043 bool has_no_arguments = (*text == '\0');
1044 condition_command_opts cc_opts;
1045 const auto group = make_condition_command_options_def_group (&cc_opts);
1046 if (gdb::option::complete_options
1047 (tracker, &text, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR, group))
1048 return;
d55637df 1049
f1735a53 1050 text = skip_spaces (text);
b1d4d8d1 1051 const char *space = skip_to_space (text);
d55637df
TT
1052 if (*space == '\0')
1053 {
1054 int len;
1055 struct breakpoint *b;
d55637df
TT
1056
1057 if (text[0] == '$')
1058 {
b1d4d8d1 1059 tracker.advance_custom_word_point_by (1);
d55637df 1060 /* We don't support completion of history indices. */
eb3ff9a5
PA
1061 if (!isdigit (text[1]))
1062 complete_internalvar (tracker, &text[1]);
1063 return;
d55637df
TT
1064 }
1065
b1d4d8d1
TBA
1066 /* Suggest the "-force" flag if no arguments are given. If
1067 arguments were passed, they either already include the flag,
1068 or we are beyond the point of suggesting it because it's
1069 positionally the first argument. */
1070 if (has_no_arguments)
1071 gdb::option::complete_on_all_options (tracker, group);
1072
d55637df
TT
1073 /* We're completing the breakpoint number. */
1074 len = strlen (text);
1075
1076 ALL_BREAKPOINTS (b)
58ce7251
SDJ
1077 {
1078 char number[50];
1079
1080 xsnprintf (number, sizeof (number), "%d", b->number);
1081
1082 if (strncmp (number, text, len) == 0)
b02f78f9 1083 tracker.add_completion (make_unique_xstrdup (number));
58ce7251 1084 }
d55637df 1085
eb3ff9a5 1086 return;
d55637df
TT
1087 }
1088
b1d4d8d1
TBA
1089 /* We're completing the expression part. Skip the breakpoint num. */
1090 const char *exp_start = skip_spaces (space);
1091 tracker.advance_custom_word_point_by (exp_start - text);
1092 text = exp_start;
1093 const char *word = advance_to_expression_complete_word_point (tracker, text);
eb3ff9a5 1094 expression_completer (cmd, tracker, text, word);
d55637df
TT
1095}
1096
c906108c
SS
1097/* condition N EXP -- set break condition of breakpoint N to EXP. */
1098
1099static void
0b39b52e 1100condition_command (const char *arg, int from_tty)
c906108c 1101{
0b39b52e 1102 const char *p;
52f0bd74 1103 int bnum;
c906108c
SS
1104
1105 if (arg == 0)
e2e0b3e5 1106 error_no_arg (_("breakpoint number"));
c906108c
SS
1107
1108 p = arg;
733d554a
TBA
1109
1110 /* Check if the "-force" flag was passed. */
b1d4d8d1
TBA
1111 condition_command_opts cc_opts;
1112 const auto group = make_condition_command_options_def_group (&cc_opts);
1113 gdb::option::process_options
1114 (&p, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR, group);
733d554a 1115
c906108c 1116 bnum = get_number (&p);
5c44784c 1117 if (bnum == 0)
8a3fe4f8 1118 error (_("Bad breakpoint argument: '%s'"), arg);
c906108c 1119
79aabb73 1120 set_breakpoint_condition (bnum, p, from_tty, cc_opts.force_condition);
c906108c
SS
1121}
1122
a7bdde9e
VP
1123/* Check that COMMAND do not contain commands that are suitable
1124 only for tracepoints and not suitable for ordinary breakpoints.
4a64f543
MS
1125 Throw if any such commands is found. */
1126
a7bdde9e
VP
1127static void
1128check_no_tracepoint_commands (struct command_line *commands)
1129{
1130 struct command_line *c;
cc59ec59 1131
a7bdde9e
VP
1132 for (c = commands; c; c = c->next)
1133 {
a7bdde9e 1134 if (c->control_type == while_stepping_control)
3e43a32a
MS
1135 error (_("The 'while-stepping' command can "
1136 "only be used for tracepoints"));
a7bdde9e 1137
12973681
TT
1138 check_no_tracepoint_commands (c->body_list_0.get ());
1139 check_no_tracepoint_commands (c->body_list_1.get ());
a7bdde9e
VP
1140
1141 /* Not that command parsing removes leading whitespace and comment
4a64f543 1142 lines and also empty lines. So, we only need to check for
a7bdde9e
VP
1143 command directly. */
1144 if (strstr (c->line, "collect ") == c->line)
1145 error (_("The 'collect' command can only be used for tracepoints"));
1146
51661e93
VP
1147 if (strstr (c->line, "teval ") == c->line)
1148 error (_("The 'teval' command can only be used for tracepoints"));
a7bdde9e
VP
1149 }
1150}
1151
c1fc2657 1152struct longjmp_breakpoint : public breakpoint
3b0871f4 1153{
c1fc2657 1154 ~longjmp_breakpoint () override;
3b0871f4
SM
1155};
1156
d77f58be
SS
1157/* Encapsulate tests for different types of tracepoints. */
1158
3b0871f4
SM
1159static bool
1160is_tracepoint_type (bptype type)
d9b3f62e
PA
1161{
1162 return (type == bp_tracepoint
1163 || type == bp_fast_tracepoint
1164 || type == bp_static_tracepoint);
1165}
1166
3b0871f4
SM
1167static bool
1168is_longjmp_type (bptype type)
1169{
1170 return type == bp_longjmp || type == bp_exception;
1171}
1172
f2478a7e
SM
1173/* See breakpoint.h. */
1174
1175bool
d77f58be 1176is_tracepoint (const struct breakpoint *b)
a7bdde9e 1177{
d9b3f62e 1178 return is_tracepoint_type (b->type);
a7bdde9e 1179}
d9b3f62e 1180
a5e364af
SM
1181/* Factory function to create an appropriate instance of breakpoint given
1182 TYPE. */
1183
1184static std::unique_ptr<breakpoint>
1185new_breakpoint_from_type (bptype type)
1186{
1187 breakpoint *b;
1188
1189 if (is_tracepoint_type (type))
c1fc2657 1190 b = new tracepoint ();
3b0871f4 1191 else if (is_longjmp_type (type))
c1fc2657 1192 b = new longjmp_breakpoint ();
a5e364af
SM
1193 else
1194 b = new breakpoint ();
1195
1196 return std::unique_ptr<breakpoint> (b);
1197}
1198
e5dd4106 1199/* A helper function that validates that COMMANDS are valid for a
95a42b64
TT
1200 breakpoint. This function will throw an exception if a problem is
1201 found. */
48cb2d85 1202
95a42b64
TT
1203static void
1204validate_commands_for_breakpoint (struct breakpoint *b,
1205 struct command_line *commands)
48cb2d85 1206{
d77f58be 1207 if (is_tracepoint (b))
a7bdde9e 1208 {
c9a6ce02 1209 struct tracepoint *t = (struct tracepoint *) b;
a7bdde9e
VP
1210 struct command_line *c;
1211 struct command_line *while_stepping = 0;
c9a6ce02
PA
1212
1213 /* Reset the while-stepping step count. The previous commands
dda83cd7
SM
1214 might have included a while-stepping action, while the new
1215 ones might not. */
c9a6ce02
PA
1216 t->step_count = 0;
1217
1218 /* We need to verify that each top-level element of commands is
1219 valid for tracepoints, that there's at most one
1220 while-stepping element, and that the while-stepping's body
1221 has valid tracing commands excluding nested while-stepping.
1222 We also need to validate the tracepoint action line in the
1223 context of the tracepoint --- validate_actionline actually
1224 has side effects, like setting the tracepoint's
1225 while-stepping STEP_COUNT, in addition to checking if the
1226 collect/teval actions parse and make sense in the
1227 tracepoint's context. */
a7bdde9e
VP
1228 for (c = commands; c; c = c->next)
1229 {
a7bdde9e
VP
1230 if (c->control_type == while_stepping_control)
1231 {
1232 if (b->type == bp_fast_tracepoint)
3e43a32a
MS
1233 error (_("The 'while-stepping' command "
1234 "cannot be used for fast tracepoint"));
0fb4aa4b 1235 else if (b->type == bp_static_tracepoint)
3e43a32a
MS
1236 error (_("The 'while-stepping' command "
1237 "cannot be used for static tracepoint"));
a7bdde9e
VP
1238
1239 if (while_stepping)
3e43a32a
MS
1240 error (_("The 'while-stepping' command "
1241 "can be used only once"));
a7bdde9e
VP
1242 else
1243 while_stepping = c;
1244 }
c9a6ce02
PA
1245
1246 validate_actionline (c->line, b);
a7bdde9e
VP
1247 }
1248 if (while_stepping)
1249 {
1250 struct command_line *c2;
1251
12973681
TT
1252 gdb_assert (while_stepping->body_list_1 == nullptr);
1253 c2 = while_stepping->body_list_0.get ();
a7bdde9e
VP
1254 for (; c2; c2 = c2->next)
1255 {
a7bdde9e
VP
1256 if (c2->control_type == while_stepping_control)
1257 error (_("The 'while-stepping' command cannot be nested"));
1258 }
1259 }
1260 }
1261 else
1262 {
1263 check_no_tracepoint_commands (commands);
1264 }
95a42b64
TT
1265}
1266
0fb4aa4b
PA
1267/* Return a vector of all the static tracepoints set at ADDR. The
1268 caller is responsible for releasing the vector. */
1269
f51e0e20 1270std::vector<breakpoint *>
0fb4aa4b
PA
1271static_tracepoints_here (CORE_ADDR addr)
1272{
1273 struct breakpoint *b;
f51e0e20 1274 std::vector<breakpoint *> found;
0fb4aa4b
PA
1275 struct bp_location *loc;
1276
1277 ALL_BREAKPOINTS (b)
1278 if (b->type == bp_static_tracepoint)
1279 {
1280 for (loc = b->loc; loc; loc = loc->next)
1281 if (loc->address == addr)
f51e0e20 1282 found.push_back (b);
0fb4aa4b
PA
1283 }
1284
1285 return found;
1286}
1287
95a42b64 1288/* Set the command list of B to COMMANDS. If breakpoint is tracepoint,
4a64f543 1289 validate that only allowed commands are included. */
95a42b64
TT
1290
1291void
4a64f543 1292breakpoint_set_commands (struct breakpoint *b,
12973681 1293 counted_command_line &&commands)
95a42b64 1294{
93921405 1295 validate_commands_for_breakpoint (b, commands.get ());
a7bdde9e 1296
d1b0a7bf 1297 b->commands = std::move (commands);
76727919 1298 gdb::observers::breakpoint_modified.notify (b);
48cb2d85
VP
1299}
1300
45a43567
TT
1301/* Set the internal `silent' flag on the breakpoint. Note that this
1302 is not the same as the "silent" that may appear in the breakpoint's
1303 commands. */
1304
1305void
1306breakpoint_set_silent (struct breakpoint *b, int silent)
1307{
1308 int old_silent = b->silent;
1309
1310 b->silent = silent;
1311 if (old_silent != silent)
76727919 1312 gdb::observers::breakpoint_modified.notify (b);
45a43567
TT
1313}
1314
1315/* Set the thread for this breakpoint. If THREAD is -1, make the
1316 breakpoint work for any thread. */
1317
1318void
1319breakpoint_set_thread (struct breakpoint *b, int thread)
1320{
1321 int old_thread = b->thread;
1322
1323 b->thread = thread;
1324 if (old_thread != thread)
76727919 1325 gdb::observers::breakpoint_modified.notify (b);
45a43567
TT
1326}
1327
1328/* Set the task for this breakpoint. If TASK is 0, make the
1329 breakpoint work for any task. */
1330
1331void
1332breakpoint_set_task (struct breakpoint *b, int task)
1333{
1334 int old_task = b->task;
1335
1336 b->task = task;
1337 if (old_task != task)
76727919 1338 gdb::observers::breakpoint_modified.notify (b);
45a43567
TT
1339}
1340
95a42b64 1341static void
896b6bda 1342commands_command_1 (const char *arg, int from_tty,
4a64f543 1343 struct command_line *control)
95a42b64 1344{
d1b0a7bf 1345 counted_command_line cmd;
999700cd
PW
1346 /* cmd_read will be true once we have read cmd. Note that cmd might still be
1347 NULL after the call to read_command_lines if the user provides an empty
1348 list of command by just typing "end". */
1349 bool cmd_read = false;
95a42b64 1350
896b6bda
PA
1351 std::string new_arg;
1352
95a42b64
TT
1353 if (arg == NULL || !*arg)
1354 {
da1df1db 1355 /* Argument not explicitly given. Synthesize it. */
86b17b60 1356 if (breakpoint_count - prev_breakpoint_count > 1)
896b6bda
PA
1357 new_arg = string_printf ("%d-%d", prev_breakpoint_count + 1,
1358 breakpoint_count);
95a42b64 1359 else if (breakpoint_count > 0)
896b6bda 1360 new_arg = string_printf ("%d", breakpoint_count);
48649e1b 1361 }
da1df1db
TBA
1362 else
1363 {
1364 /* Create a copy of ARG. This is needed because the "commands"
1365 command may be coming from a script. In that case, the read
1366 line buffer is going to be overwritten in the lambda of
1367 'map_breakpoint_numbers' below when reading the next line
1368 before we are are done parsing the breakpoint numbers. */
1369 new_arg = arg;
1370 }
1371 arg = new_arg.c_str ();
48649e1b
TT
1372
1373 map_breakpoint_numbers
1374 (arg, [&] (breakpoint *b)
1375 {
999700cd 1376 if (!cmd_read)
48649e1b 1377 {
999700cd 1378 gdb_assert (cmd == NULL);
48649e1b 1379 if (control != NULL)
12973681 1380 cmd = control->body_list_0;
48649e1b
TT
1381 else
1382 {
81b1e71c
TT
1383 std::string str
1384 = string_printf (_("Type commands for breakpoint(s) "
1385 "%s, one per line."),
1386 arg);
48649e1b 1387
60b3cef2
TT
1388 auto do_validate = [=] (const char *line)
1389 {
1390 validate_actionline (line, b);
1391 };
1392 gdb::function_view<void (const char *)> validator;
1393 if (is_tracepoint (b))
1394 validator = do_validate;
1395
1396 cmd = read_command_lines (str.c_str (), from_tty, 1, validator);
48649e1b 1397 }
999700cd 1398 cmd_read = true;
48649e1b
TT
1399 }
1400
1401 /* If a breakpoint was on the list more than once, we don't need to
1402 do anything. */
1403 if (b->commands != cmd)
1404 {
d1b0a7bf 1405 validate_commands_for_breakpoint (b, cmd.get ());
48649e1b 1406 b->commands = cmd;
76727919 1407 gdb::observers::breakpoint_modified.notify (b);
48649e1b
TT
1408 }
1409 });
95a42b64
TT
1410}
1411
1412static void
0b39b52e 1413commands_command (const char *arg, int from_tty)
95a42b64
TT
1414{
1415 commands_command_1 (arg, from_tty, NULL);
c906108c 1416}
40c03ae8
EZ
1417
1418/* Like commands_command, but instead of reading the commands from
1419 input stream, takes them from an already parsed command structure.
1420
1421 This is used by cli-script.c to DTRT with breakpoint commands
1422 that are part of if and while bodies. */
1423enum command_control_type
896b6bda 1424commands_from_control_command (const char *arg, struct command_line *cmd)
40c03ae8 1425{
95a42b64
TT
1426 commands_command_1 (arg, 0, cmd);
1427 return simple_control;
40c03ae8 1428}
876fa593
JK
1429
1430/* Return non-zero if BL->TARGET_INFO contains valid information. */
1431
1432static int
1433bp_location_has_shadow (struct bp_location *bl)
1434{
1435 if (bl->loc_type != bp_loc_software_breakpoint)
1436 return 0;
1437 if (!bl->inserted)
1438 return 0;
1439 if (bl->target_info.shadow_len == 0)
e5dd4106 1440 /* BL isn't valid, or doesn't shadow memory. */
876fa593
JK
1441 return 0;
1442 return 1;
1443}
1444
9d497a19
PA
1445/* Update BUF, which is LEN bytes read from the target address
1446 MEMADDR, by replacing a memory breakpoint with its shadowed
1447 contents.
1448
1449 If READBUF is not NULL, this buffer must not overlap with the of
1450 the breakpoint location's shadow_contents buffer. Otherwise, a
1451 failed assertion internal error will be raised. */
1452
1453static void
1454one_breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1455 const gdb_byte *writebuf_org,
1456 ULONGEST memaddr, LONGEST len,
1457 struct bp_target_info *target_info,
1458 struct gdbarch *gdbarch)
1459{
1460 /* Now do full processing of the found relevant range of elements. */
1461 CORE_ADDR bp_addr = 0;
1462 int bp_size = 0;
1463 int bptoffset = 0;
1464
1465 if (!breakpoint_address_match (target_info->placed_address_space, 0,
1466 current_program_space->aspace, 0))
1467 {
1468 /* The breakpoint is inserted in a different address space. */
1469 return;
1470 }
1471
1472 /* Addresses and length of the part of the breakpoint that
1473 we need to copy. */
1474 bp_addr = target_info->placed_address;
1475 bp_size = target_info->shadow_len;
1476
1477 if (bp_addr + bp_size <= memaddr)
1478 {
1479 /* The breakpoint is entirely before the chunk of memory we are
1480 reading. */
1481 return;
1482 }
1483
1484 if (bp_addr >= memaddr + len)
1485 {
1486 /* The breakpoint is entirely after the chunk of memory we are
1487 reading. */
1488 return;
1489 }
1490
1491 /* Offset within shadow_contents. */
1492 if (bp_addr < memaddr)
1493 {
1494 /* Only copy the second part of the breakpoint. */
1495 bp_size -= memaddr - bp_addr;
1496 bptoffset = memaddr - bp_addr;
1497 bp_addr = memaddr;
1498 }
1499
1500 if (bp_addr + bp_size > memaddr + len)
1501 {
1502 /* Only copy the first part of the breakpoint. */
1503 bp_size -= (bp_addr + bp_size) - (memaddr + len);
1504 }
1505
1506 if (readbuf != NULL)
1507 {
1508 /* Verify that the readbuf buffer does not overlap with the
1509 shadow_contents buffer. */
1510 gdb_assert (target_info->shadow_contents >= readbuf + len
1511 || readbuf >= (target_info->shadow_contents
1512 + target_info->shadow_len));
1513
1514 /* Update the read buffer with this inserted breakpoint's
1515 shadow. */
1516 memcpy (readbuf + bp_addr - memaddr,
1517 target_info->shadow_contents + bptoffset, bp_size);
1518 }
1519 else
1520 {
1521 const unsigned char *bp;
0d5ed153
MR
1522 CORE_ADDR addr = target_info->reqstd_address;
1523 int placed_size;
9d497a19
PA
1524
1525 /* Update the shadow with what we want to write to memory. */
1526 memcpy (target_info->shadow_contents + bptoffset,
1527 writebuf_org + bp_addr - memaddr, bp_size);
1528
1529 /* Determine appropriate breakpoint contents and size for this
1530 address. */
0d5ed153 1531 bp = gdbarch_breakpoint_from_pc (gdbarch, &addr, &placed_size);
9d497a19
PA
1532
1533 /* Update the final write buffer with this inserted
1534 breakpoint's INSN. */
1535 memcpy (writebuf + bp_addr - memaddr, bp + bptoffset, bp_size);
1536 }
1537}
1538
8defab1a 1539/* Update BUF, which is LEN bytes read from the target address MEMADDR,
876fa593
JK
1540 by replacing any memory breakpoints with their shadowed contents.
1541
35c63cd8
JB
1542 If READBUF is not NULL, this buffer must not overlap with any of
1543 the breakpoint location's shadow_contents buffers. Otherwise,
1544 a failed assertion internal error will be raised.
1545
876fa593 1546 The range of shadowed area by each bp_location is:
f5336ca5
PA
1547 bl->address - bp_locations_placed_address_before_address_max
1548 up to bl->address + bp_locations_shadow_len_after_address_max
876fa593
JK
1549 The range we were requested to resolve shadows for is:
1550 memaddr ... memaddr + len
1551 Thus the safe cutoff boundaries for performance optimization are
35df4500 1552 memaddr + len <= (bl->address
f5336ca5 1553 - bp_locations_placed_address_before_address_max)
876fa593 1554 and:
f5336ca5 1555 bl->address + bp_locations_shadow_len_after_address_max <= memaddr */
c906108c 1556
8defab1a 1557void
f0ba3972
PA
1558breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1559 const gdb_byte *writebuf_org,
1560 ULONGEST memaddr, LONGEST len)
c906108c 1561{
4a64f543
MS
1562 /* Left boundary, right boundary and median element of our binary
1563 search. */
876fa593
JK
1564 unsigned bc_l, bc_r, bc;
1565
4a64f543
MS
1566 /* Find BC_L which is a leftmost element which may affect BUF
1567 content. It is safe to report lower value but a failure to
1568 report higher one. */
876fa593
JK
1569
1570 bc_l = 0;
f5336ca5 1571 bc_r = bp_locations_count;
876fa593
JK
1572 while (bc_l + 1 < bc_r)
1573 {
35df4500 1574 struct bp_location *bl;
876fa593
JK
1575
1576 bc = (bc_l + bc_r) / 2;
f5336ca5 1577 bl = bp_locations[bc];
876fa593 1578
4a64f543
MS
1579 /* Check first BL->ADDRESS will not overflow due to the added
1580 constant. Then advance the left boundary only if we are sure
1581 the BC element can in no way affect the BUF content (MEMADDR
1582 to MEMADDR + LEN range).
876fa593 1583
f5336ca5 1584 Use the BP_LOCATIONS_SHADOW_LEN_AFTER_ADDRESS_MAX safety
4a64f543
MS
1585 offset so that we cannot miss a breakpoint with its shadow
1586 range tail still reaching MEMADDR. */
c5aa993b 1587
f5336ca5 1588 if ((bl->address + bp_locations_shadow_len_after_address_max
35df4500 1589 >= bl->address)
f5336ca5 1590 && (bl->address + bp_locations_shadow_len_after_address_max
35df4500 1591 <= memaddr))
876fa593
JK
1592 bc_l = bc;
1593 else
1594 bc_r = bc;
1595 }
1596
128070bb
PA
1597 /* Due to the binary search above, we need to make sure we pick the
1598 first location that's at BC_L's address. E.g., if there are
1599 multiple locations at the same address, BC_L may end up pointing
1600 at a duplicate location, and miss the "master"/"inserted"
1601 location. Say, given locations L1, L2 and L3 at addresses A and
1602 B:
1603
1604 L1@A, L2@A, L3@B, ...
1605
1606 BC_L could end up pointing at location L2, while the "master"
1607 location could be L1. Since the `loc->inserted' flag is only set
1608 on "master" locations, we'd forget to restore the shadow of L1
1609 and L2. */
1610 while (bc_l > 0
f5336ca5 1611 && bp_locations[bc_l]->address == bp_locations[bc_l - 1]->address)
128070bb
PA
1612 bc_l--;
1613
876fa593
JK
1614 /* Now do full processing of the found relevant range of elements. */
1615
f5336ca5 1616 for (bc = bc_l; bc < bp_locations_count; bc++)
c5aa993b 1617 {
f5336ca5 1618 struct bp_location *bl = bp_locations[bc];
876fa593 1619
35df4500
TJB
1620 /* bp_location array has BL->OWNER always non-NULL. */
1621 if (bl->owner->type == bp_none)
8a3fe4f8 1622 warning (_("reading through apparently deleted breakpoint #%d?"),
35df4500 1623 bl->owner->number);
ffce0d52 1624
e5dd4106 1625 /* Performance optimization: any further element can no longer affect BUF
876fa593
JK
1626 content. */
1627
f5336ca5
PA
1628 if (bl->address >= bp_locations_placed_address_before_address_max
1629 && memaddr + len <= (bl->address
1630 - bp_locations_placed_address_before_address_max))
876fa593
JK
1631 break;
1632
35df4500 1633 if (!bp_location_has_shadow (bl))
c5aa993b 1634 continue;
6c95b8df 1635
9d497a19
PA
1636 one_breakpoint_xfer_memory (readbuf, writebuf, writebuf_org,
1637 memaddr, len, &bl->target_info, bl->gdbarch);
1638 }
c906108c 1639}
9d497a19 1640
f2478a7e 1641/* See breakpoint.h. */
b775012e 1642
f2478a7e 1643bool
b775012e
LM
1644is_breakpoint (const struct breakpoint *bpt)
1645{
1646 return (bpt->type == bp_breakpoint
e7e0cddf
SS
1647 || bpt->type == bp_hardware_breakpoint
1648 || bpt->type == bp_dprintf);
b775012e
LM
1649}
1650
60e1c644
PA
1651/* Return true if BPT is of any hardware watchpoint kind. */
1652
f2478a7e 1653static bool
d77f58be 1654is_hardware_watchpoint (const struct breakpoint *bpt)
a5606eee
VP
1655{
1656 return (bpt->type == bp_hardware_watchpoint
1657 || bpt->type == bp_read_watchpoint
1658 || bpt->type == bp_access_watchpoint);
1659}
7270d8f2 1660
f2478a7e 1661/* See breakpoint.h. */
60e1c644 1662
f2478a7e 1663bool
d77f58be 1664is_watchpoint (const struct breakpoint *bpt)
60e1c644
PA
1665{
1666 return (is_hardware_watchpoint (bpt)
1667 || bpt->type == bp_watchpoint);
1668}
1669
3a5c3e22
PA
1670/* Returns true if the current thread and its running state are safe
1671 to evaluate or update watchpoint B. Watchpoints on local
1672 expressions need to be evaluated in the context of the thread that
1673 was current when the watchpoint was created, and, that thread needs
1674 to be stopped to be able to select the correct frame context.
1675 Watchpoints on global expressions can be evaluated on any thread,
1676 and in any state. It is presently left to the target allowing
1677 memory accesses when threads are running. */
f6bc2008
PA
1678
1679static int
3a5c3e22 1680watchpoint_in_thread_scope (struct watchpoint *b)
f6bc2008 1681{
c1fc2657 1682 return (b->pspace == current_program_space
d7e15655
TT
1683 && (b->watchpoint_thread == null_ptid
1684 || (inferior_ptid == b->watchpoint_thread
00431a78 1685 && !inferior_thread ()->executing)));
f6bc2008
PA
1686}
1687
d0fb5eae
JK
1688/* Set watchpoint B to disp_del_at_next_stop, even including its possible
1689 associated bp_watchpoint_scope breakpoint. */
1690
1691static void
3a5c3e22 1692watchpoint_del_at_next_stop (struct watchpoint *w)
d0fb5eae 1693{
c1fc2657 1694 if (w->related_breakpoint != w)
d0fb5eae 1695 {
c1fc2657
SM
1696 gdb_assert (w->related_breakpoint->type == bp_watchpoint_scope);
1697 gdb_assert (w->related_breakpoint->related_breakpoint == w);
1698 w->related_breakpoint->disposition = disp_del_at_next_stop;
1699 w->related_breakpoint->related_breakpoint = w->related_breakpoint;
1700 w->related_breakpoint = w;
d0fb5eae 1701 }
c1fc2657 1702 w->disposition = disp_del_at_next_stop;
d0fb5eae
JK
1703}
1704
bb9d5f81
PP
1705/* Extract a bitfield value from value VAL using the bit parameters contained in
1706 watchpoint W. */
1707
1708static struct value *
1709extract_bitfield_from_watchpoint_value (struct watchpoint *w, struct value *val)
1710{
1711 struct value *bit_val;
1712
1713 if (val == NULL)
1714 return NULL;
1715
1716 bit_val = allocate_value (value_type (val));
1717
1718 unpack_value_bitfield (bit_val,
1719 w->val_bitpos,
1720 w->val_bitsize,
1721 value_contents_for_printing (val),
1722 value_offset (val),
1723 val);
1724
1725 return bit_val;
1726}
1727
c6d81124
PA
1728/* Allocate a dummy location and add it to B, which must be a software
1729 watchpoint. This is required because even if a software watchpoint
1730 is not watching any memory, bpstat_stop_status requires a location
1731 to be able to report stops. */
1732
1733static void
1734software_watchpoint_add_no_memory_location (struct breakpoint *b,
1735 struct program_space *pspace)
1736{
1737 gdb_assert (b->type == bp_watchpoint && b->loc == NULL);
1738
1739 b->loc = allocate_bp_location (b);
1740 b->loc->pspace = pspace;
1741 b->loc->address = -1;
1742 b->loc->length = -1;
1743}
1744
1745/* Returns true if B is a software watchpoint that is not watching any
1746 memory (e.g., "watch $pc"). */
1747
f2478a7e 1748static bool
c6d81124
PA
1749is_no_memory_software_watchpoint (struct breakpoint *b)
1750{
1751 return (b->type == bp_watchpoint
1752 && b->loc != NULL
1753 && b->loc->next == NULL
1754 && b->loc->address == -1
1755 && b->loc->length == -1);
1756}
1757
567e1b4e
JB
1758/* Assuming that B is a watchpoint:
1759 - Reparse watchpoint expression, if REPARSE is non-zero
a5606eee 1760 - Evaluate expression and store the result in B->val
567e1b4e
JB
1761 - Evaluate the condition if there is one, and store the result
1762 in b->loc->cond.
a5606eee
VP
1763 - Update the list of values that must be watched in B->loc.
1764
4a64f543
MS
1765 If the watchpoint disposition is disp_del_at_next_stop, then do
1766 nothing. If this is local watchpoint that is out of scope, delete
1767 it.
1768
1769 Even with `set breakpoint always-inserted on' the watchpoints are
1770 removed + inserted on each stop here. Normal breakpoints must
1771 never be removed because they might be missed by a running thread
1772 when debugging in non-stop mode. On the other hand, hardware
1773 watchpoints (is_hardware_watchpoint; processed here) are specific
1774 to each LWP since they are stored in each LWP's hardware debug
1775 registers. Therefore, such LWP must be stopped first in order to
1776 be able to modify its hardware watchpoints.
1777
1778 Hardware watchpoints must be reset exactly once after being
1779 presented to the user. It cannot be done sooner, because it would
1780 reset the data used to present the watchpoint hit to the user. And
1781 it must not be done later because it could display the same single
1782 watchpoint hit during multiple GDB stops. Note that the latter is
1783 relevant only to the hardware watchpoint types bp_read_watchpoint
1784 and bp_access_watchpoint. False hit by bp_hardware_watchpoint is
1785 not user-visible - its hit is suppressed if the memory content has
1786 not changed.
1787
1788 The following constraints influence the location where we can reset
1789 hardware watchpoints:
1790
1791 * target_stopped_by_watchpoint and target_stopped_data_address are
1792 called several times when GDB stops.
1793
1794 [linux]
1795 * Multiple hardware watchpoints can be hit at the same time,
1796 causing GDB to stop. GDB only presents one hardware watchpoint
1797 hit at a time as the reason for stopping, and all the other hits
1798 are presented later, one after the other, each time the user
1799 requests the execution to be resumed. Execution is not resumed
1800 for the threads still having pending hit event stored in
1801 LWP_INFO->STATUS. While the watchpoint is already removed from
1802 the inferior on the first stop the thread hit event is kept being
1803 reported from its cached value by linux_nat_stopped_data_address
1804 until the real thread resume happens after the watchpoint gets
1805 presented and thus its LWP_INFO->STATUS gets reset.
1806
1807 Therefore the hardware watchpoint hit can get safely reset on the
1808 watchpoint removal from inferior. */
a79d3c27 1809
b40ce68a 1810static void
3a5c3e22 1811update_watchpoint (struct watchpoint *b, int reparse)
7270d8f2 1812{
a5606eee 1813 int within_current_scope;
a5606eee 1814 struct frame_id saved_frame_id;
66076460 1815 int frame_saved;
a5606eee 1816
f6bc2008
PA
1817 /* If this is a local watchpoint, we only want to check if the
1818 watchpoint frame is in scope if the current thread is the thread
1819 that was used to create the watchpoint. */
1820 if (!watchpoint_in_thread_scope (b))
1821 return;
1822
c1fc2657 1823 if (b->disposition == disp_del_at_next_stop)
a5606eee
VP
1824 return;
1825
66076460 1826 frame_saved = 0;
a5606eee
VP
1827
1828 /* Determine if the watchpoint is within scope. */
1829 if (b->exp_valid_block == NULL)
1830 within_current_scope = 1;
1831 else
1832 {
b5db5dfc
UW
1833 struct frame_info *fi = get_current_frame ();
1834 struct gdbarch *frame_arch = get_frame_arch (fi);
1835 CORE_ADDR frame_pc = get_frame_pc (fi);
1836
c9cf6e20
MG
1837 /* If we're at a point where the stack has been destroyed
1838 (e.g. in a function epilogue), unwinding may not work
1839 properly. Do not attempt to recreate locations at this
b5db5dfc 1840 point. See similar comments in watchpoint_check. */
c9cf6e20 1841 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
b5db5dfc 1842 return;
66076460
DJ
1843
1844 /* Save the current frame's ID so we can restore it after
dda83cd7 1845 evaluating the watchpoint expression on its own frame. */
66076460 1846 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
dda83cd7
SM
1847 took a frame parameter, so that we didn't have to change the
1848 selected frame. */
66076460
DJ
1849 frame_saved = 1;
1850 saved_frame_id = get_frame_id (get_selected_frame (NULL));
1851
a5606eee
VP
1852 fi = frame_find_by_id (b->watchpoint_frame);
1853 within_current_scope = (fi != NULL);
1854 if (within_current_scope)
1855 select_frame (fi);
1856 }
1857
b5db5dfc
UW
1858 /* We don't free locations. They are stored in the bp_location array
1859 and update_global_location_list will eventually delete them and
1860 remove breakpoints if needed. */
c1fc2657 1861 b->loc = NULL;
b5db5dfc 1862
a5606eee
VP
1863 if (within_current_scope && reparse)
1864 {
bbc13ae3 1865 const char *s;
d63d0675 1866
4d01a485 1867 b->exp.reset ();
d63d0675 1868 s = b->exp_string_reparse ? b->exp_string_reparse : b->exp_string;
1bb9788d 1869 b->exp = parse_exp_1 (&s, 0, b->exp_valid_block, 0);
a5606eee
VP
1870 /* If the meaning of expression itself changed, the old value is
1871 no longer relevant. We don't want to report a watchpoint hit
1872 to the user when the old value and the new value may actually
1873 be completely different objects. */
fa4727a6 1874 b->val = NULL;
4c1d86d9 1875 b->val_valid = false;
60e1c644
PA
1876
1877 /* Note that unlike with breakpoints, the watchpoint's condition
1878 expression is stored in the breakpoint object, not in the
1879 locations (re)created below. */
c1fc2657 1880 if (b->cond_string != NULL)
60e1c644 1881 {
4d01a485 1882 b->cond_exp.reset ();
60e1c644 1883
c1fc2657 1884 s = b->cond_string;
1bb9788d 1885 b->cond_exp = parse_exp_1 (&s, 0, b->cond_exp_valid_block, 0);
60e1c644 1886 }
a5606eee 1887 }
a5606eee
VP
1888
1889 /* If we failed to parse the expression, for example because
1890 it refers to a global variable in a not-yet-loaded shared library,
1891 don't try to insert watchpoint. We don't automatically delete
1892 such watchpoint, though, since failure to parse expression
1893 is different from out-of-scope watchpoint. */
55f6301a 1894 if (!target_has_execution ())
2d134ed3
PA
1895 {
1896 /* Without execution, memory can't change. No use to try and
1897 set watchpoint locations. The watchpoint will be reset when
1898 the target gains execution, through breakpoint_re_set. */
e8369a73
AB
1899 if (!can_use_hw_watchpoints)
1900 {
c1fc2657
SM
1901 if (b->ops->works_in_software_mode (b))
1902 b->type = bp_watchpoint;
e8369a73 1903 else
638aa5a1
AB
1904 error (_("Can't set read/access watchpoint when "
1905 "hardware watchpoints are disabled."));
e8369a73 1906 }
2d134ed3
PA
1907 }
1908 else if (within_current_scope && b->exp)
a5606eee 1909 {
a6535de1 1910 std::vector<value_ref_ptr> val_chain;
8d49165d 1911 struct value *v, *result;
2d134ed3 1912 struct program_space *frame_pspace;
a5606eee 1913
1eaebe02 1914 fetch_subexp_value (b->exp.get (), b->exp->op.get (), &v, &result,
413403fc 1915 &val_chain, false);
a5606eee 1916
a5606eee
VP
1917 /* Avoid setting b->val if it's already set. The meaning of
1918 b->val is 'the last value' user saw, and we should update
1919 it only if we reported that last value to user. As it
9c06b0b4
TJB
1920 happens, the code that reports it updates b->val directly.
1921 We don't keep track of the memory value for masked
1922 watchpoints. */
c1fc2657 1923 if (!b->val_valid && !is_masked_watchpoint (b))
fa4727a6 1924 {
bb9d5f81 1925 if (b->val_bitsize != 0)
850645cf
TT
1926 v = extract_bitfield_from_watchpoint_value (b, v);
1927 b->val = release_value (v);
4c1d86d9 1928 b->val_valid = true;
fa4727a6 1929 }
a5606eee 1930
2d134ed3
PA
1931 frame_pspace = get_frame_program_space (get_selected_frame (NULL));
1932
a5606eee 1933 /* Look at each value on the value chain. */
a6535de1
TT
1934 gdb_assert (!val_chain.empty ());
1935 for (const value_ref_ptr &iter : val_chain)
a5606eee 1936 {
a6535de1
TT
1937 v = iter.get ();
1938
a5606eee
VP
1939 /* If it's a memory location, and GDB actually needed
1940 its contents to evaluate the expression, then we
fa4727a6
DJ
1941 must watch it. If the first value returned is
1942 still lazy, that means an error occurred reading it;
1943 watch it anyway in case it becomes readable. */
a5606eee 1944 if (VALUE_LVAL (v) == lval_memory
a6535de1 1945 && (v == val_chain[0] || ! value_lazy (v)))
a5606eee
VP
1946 {
1947 struct type *vtype = check_typedef (value_type (v));
7270d8f2 1948
a5606eee
VP
1949 /* We only watch structs and arrays if user asked
1950 for it explicitly, never if they just happen to
1951 appear in the middle of some value chain. */
fa4727a6 1952 if (v == result
78134374
SM
1953 || (vtype->code () != TYPE_CODE_STRUCT
1954 && vtype->code () != TYPE_CODE_ARRAY))
a5606eee
VP
1955 {
1956 CORE_ADDR addr;
f486487f 1957 enum target_hw_bp_type type;
a5606eee 1958 struct bp_location *loc, **tmp;
bb9d5f81
PP
1959 int bitpos = 0, bitsize = 0;
1960
1961 if (value_bitsize (v) != 0)
1962 {
1963 /* Extract the bit parameters out from the bitfield
1964 sub-expression. */
1965 bitpos = value_bitpos (v);
1966 bitsize = value_bitsize (v);
1967 }
1968 else if (v == result && b->val_bitsize != 0)
1969 {
1970 /* If VAL_BITSIZE != 0 then RESULT is actually a bitfield
1971 lvalue whose bit parameters are saved in the fields
1972 VAL_BITPOS and VAL_BITSIZE. */
1973 bitpos = b->val_bitpos;
1974 bitsize = b->val_bitsize;
1975 }
a5606eee 1976
42ae5230 1977 addr = value_address (v);
bb9d5f81
PP
1978 if (bitsize != 0)
1979 {
1980 /* Skip the bytes that don't contain the bitfield. */
1981 addr += bitpos / 8;
1982 }
1983
a5606eee 1984 type = hw_write;
c1fc2657 1985 if (b->type == bp_read_watchpoint)
a5606eee 1986 type = hw_read;
c1fc2657 1987 else if (b->type == bp_access_watchpoint)
a5606eee 1988 type = hw_access;
3a5c3e22 1989
c1fc2657
SM
1990 loc = allocate_bp_location (b);
1991 for (tmp = &(b->loc); *tmp != NULL; tmp = &((*tmp)->next))
a5606eee
VP
1992 ;
1993 *tmp = loc;
8ee511af 1994 loc->gdbarch = value_type (v)->arch ();
6c95b8df
PA
1995
1996 loc->pspace = frame_pspace;
f17d9474 1997 loc->address = address_significant (loc->gdbarch, addr);
bb9d5f81
PP
1998
1999 if (bitsize != 0)
2000 {
2001 /* Just cover the bytes that make up the bitfield. */
2002 loc->length = ((bitpos % 8) + bitsize + 7) / 8;
2003 }
2004 else
2005 loc->length = TYPE_LENGTH (value_type (v));
2006
a5606eee
VP
2007 loc->watchpoint_type = type;
2008 }
2009 }
9fa40276
TJB
2010 }
2011
2012 /* Change the type of breakpoint between hardware assisted or
2013 an ordinary watchpoint depending on the hardware support
2014 and free hardware slots. REPARSE is set when the inferior
2015 is started. */
a9634178 2016 if (reparse)
9fa40276 2017 {
e09342b5 2018 int reg_cnt;
9fa40276
TJB
2019 enum bp_loc_type loc_type;
2020 struct bp_location *bl;
a5606eee 2021
a9634178 2022 reg_cnt = can_use_hardware_watchpoint (val_chain);
e09342b5
TJB
2023
2024 if (reg_cnt)
9fa40276
TJB
2025 {
2026 int i, target_resources_ok, other_type_used;
a1398e0c 2027 enum bptype type;
9fa40276 2028
a9634178
TJB
2029 /* Use an exact watchpoint when there's only one memory region to be
2030 watched, and only one debug register is needed to watch it. */
2031 b->exact = target_exact_watchpoints && reg_cnt == 1;
2032
9fa40276 2033 /* We need to determine how many resources are already
e09342b5
TJB
2034 used for all other hardware watchpoints plus this one
2035 to see if we still have enough resources to also fit
a1398e0c
PA
2036 this watchpoint in as well. */
2037
2038 /* If this is a software watchpoint, we try to turn it
2039 to a hardware one -- count resources as if B was of
2040 hardware watchpoint type. */
c1fc2657 2041 type = b->type;
a1398e0c
PA
2042 if (type == bp_watchpoint)
2043 type = bp_hardware_watchpoint;
2044
2045 /* This watchpoint may or may not have been placed on
2046 the list yet at this point (it won't be in the list
2047 if we're trying to create it for the first time,
2048 through watch_command), so always account for it
2049 manually. */
2050
2051 /* Count resources used by all watchpoints except B. */
c1fc2657 2052 i = hw_watchpoint_used_count_others (b, type, &other_type_used);
a1398e0c
PA
2053
2054 /* Add in the resources needed for B. */
c1fc2657 2055 i += hw_watchpoint_use_count (b);
a1398e0c
PA
2056
2057 target_resources_ok
2058 = target_can_use_hardware_watchpoint (type, i, other_type_used);
e09342b5 2059 if (target_resources_ok <= 0)
a9634178 2060 {
c1fc2657 2061 int sw_mode = b->ops->works_in_software_mode (b);
9c06b0b4
TJB
2062
2063 if (target_resources_ok == 0 && !sw_mode)
a9634178
TJB
2064 error (_("Target does not support this type of "
2065 "hardware watchpoint."));
9c06b0b4
TJB
2066 else if (target_resources_ok < 0 && !sw_mode)
2067 error (_("There are not enough available hardware "
2068 "resources for this watchpoint."));
a1398e0c
PA
2069
2070 /* Downgrade to software watchpoint. */
c1fc2657 2071 b->type = bp_watchpoint;
a1398e0c
PA
2072 }
2073 else
2074 {
2075 /* If this was a software watchpoint, we've just
2076 found we have enough resources to turn it to a
2077 hardware watchpoint. Otherwise, this is a
2078 nop. */
c1fc2657 2079 b->type = type;
a9634178 2080 }
9fa40276 2081 }
c1fc2657 2082 else if (!b->ops->works_in_software_mode (b))
638aa5a1
AB
2083 {
2084 if (!can_use_hw_watchpoints)
2085 error (_("Can't set read/access watchpoint when "
2086 "hardware watchpoints are disabled."));
2087 else
2088 error (_("Expression cannot be implemented with "
2089 "read/access watchpoint."));
2090 }
9fa40276 2091 else
c1fc2657 2092 b->type = bp_watchpoint;
9fa40276 2093
c1fc2657 2094 loc_type = (b->type == bp_watchpoint? bp_loc_other
9fa40276 2095 : bp_loc_hardware_watchpoint);
c1fc2657 2096 for (bl = b->loc; bl; bl = bl->next)
9fa40276
TJB
2097 bl->loc_type = loc_type;
2098 }
2099
c7437ca6
PA
2100 /* If a software watchpoint is not watching any memory, then the
2101 above left it without any location set up. But,
2102 bpstat_stop_status requires a location to be able to report
2103 stops, so make sure there's at least a dummy one. */
c1fc2657
SM
2104 if (b->type == bp_watchpoint && b->loc == NULL)
2105 software_watchpoint_add_no_memory_location (b, frame_pspace);
a5606eee
VP
2106 }
2107 else if (!within_current_scope)
7270d8f2 2108 {
ac74f770
MS
2109 printf_filtered (_("\
2110Watchpoint %d deleted because the program has left the block\n\
2111in which its expression is valid.\n"),
c1fc2657 2112 b->number);
d0fb5eae 2113 watchpoint_del_at_next_stop (b);
7270d8f2 2114 }
a5606eee
VP
2115
2116 /* Restore the selected frame. */
66076460
DJ
2117 if (frame_saved)
2118 select_frame (frame_find_by_id (saved_frame_id));
7270d8f2
OF
2119}
2120
a5606eee 2121
74960c60 2122/* Returns 1 iff breakpoint location should be
1e4d1764
YQ
2123 inserted in the inferior. We don't differentiate the type of BL's owner
2124 (breakpoint vs. tracepoint), although insert_location in tracepoint's
2125 breakpoint_ops is not defined, because in insert_bp_location,
2126 tracepoint's insert_location will not be called. */
74960c60 2127static int
35df4500 2128should_be_inserted (struct bp_location *bl)
74960c60 2129{
35df4500 2130 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
74960c60
VP
2131 return 0;
2132
35df4500 2133 if (bl->owner->disposition == disp_del_at_next_stop)
74960c60
VP
2134 return 0;
2135
b5fa468f
TBA
2136 if (!bl->enabled || bl->disabled_by_cond
2137 || bl->shlib_disabled || bl->duplicate)
74960c60
VP
2138 return 0;
2139
f8eba3c6
TT
2140 if (user_breakpoint_p (bl->owner) && bl->pspace->executing_startup)
2141 return 0;
2142
56710373
PA
2143 /* This is set for example, when we're attached to the parent of a
2144 vfork, and have detached from the child. The child is running
2145 free, and we expect it to do an exec or exit, at which point the
2146 OS makes the parent schedulable again (and the target reports
2147 that the vfork is done). Until the child is done with the shared
2148 memory region, do not insert breakpoints in the parent, otherwise
2149 the child could still trip on the parent's breakpoints. Since
2150 the parent is blocked anyway, it won't miss any breakpoint. */
35df4500 2151 if (bl->pspace->breakpoints_not_allowed)
56710373
PA
2152 return 0;
2153
31e77af2 2154 /* Don't insert a breakpoint if we're trying to step past its
21edc42f
YQ
2155 location, except if the breakpoint is a single-step breakpoint,
2156 and the breakpoint's thread is the thread which is stepping past
2157 a breakpoint. */
31e77af2
PA
2158 if ((bl->loc_type == bp_loc_software_breakpoint
2159 || bl->loc_type == bp_loc_hardware_breakpoint)
2160 && stepping_past_instruction_at (bl->pspace->aspace,
21edc42f
YQ
2161 bl->address)
2162 /* The single-step breakpoint may be inserted at the location
2163 we're trying to step if the instruction branches to itself.
2164 However, the instruction won't be executed at all and it may
2165 break the semantics of the instruction, for example, the
2166 instruction is a conditional branch or updates some flags.
2167 We can't fix it unless GDB is able to emulate the instruction
2168 or switch to displaced stepping. */
2169 && !(bl->owner->type == bp_single_step
2170 && thread_is_stepping_over_breakpoint (bl->owner->thread)))
e558d7c1 2171 {
1eb8556f
SM
2172 infrun_debug_printf ("skipping breakpoint: stepping past insn at: %s",
2173 paddress (bl->gdbarch, bl->address));
e558d7c1
PA
2174 return 0;
2175 }
31e77af2 2176
963f9c80
PA
2177 /* Don't insert watchpoints if we're trying to step past the
2178 instruction that triggered one. */
2179 if ((bl->loc_type == bp_loc_hardware_watchpoint)
2180 && stepping_past_nonsteppable_watchpoint ())
2181 {
1eb8556f 2182 infrun_debug_printf ("stepping past non-steppable watchpoint. "
ae1f4d2d 2183 "skipping watchpoint at %s:%d",
1eb8556f 2184 paddress (bl->gdbarch, bl->address), bl->length);
963f9c80
PA
2185 return 0;
2186 }
2187
74960c60
VP
2188 return 1;
2189}
2190
934709f0
PW
2191/* Same as should_be_inserted but does the check assuming
2192 that the location is not duplicated. */
2193
2194static int
2195unduplicated_should_be_inserted (struct bp_location *bl)
2196{
2197 int result;
2198 const int save_duplicate = bl->duplicate;
2199
2200 bl->duplicate = 0;
2201 result = should_be_inserted (bl);
2202 bl->duplicate = save_duplicate;
2203 return result;
2204}
2205
b775012e
LM
2206/* Parses a conditional described by an expression COND into an
2207 agent expression bytecode suitable for evaluation
2208 by the bytecode interpreter. Return NULL if there was
2209 any error during parsing. */
2210
833177a4 2211static agent_expr_up
b775012e
LM
2212parse_cond_to_aexpr (CORE_ADDR scope, struct expression *cond)
2213{
833177a4 2214 if (cond == NULL)
b775012e
LM
2215 return NULL;
2216
833177a4
PA
2217 agent_expr_up aexpr;
2218
b775012e
LM
2219 /* We don't want to stop processing, so catch any errors
2220 that may show up. */
a70b8144 2221 try
b775012e 2222 {
036e657b 2223 aexpr = gen_eval_for_expr (scope, cond);
b775012e
LM
2224 }
2225
230d2906 2226 catch (const gdb_exception_error &ex)
b775012e
LM
2227 {
2228 /* If we got here, it means the condition could not be parsed to a valid
2229 bytecode expression and thus can't be evaluated on the target's side.
2230 It's no use iterating through the conditions. */
b775012e
LM
2231 }
2232
2233 /* We have a valid agent expression. */
2234 return aexpr;
2235}
2236
2237/* Based on location BL, create a list of breakpoint conditions to be
2238 passed on to the target. If we have duplicated locations with different
2239 conditions, we will add such conditions to the list. The idea is that the
2240 target will evaluate the list of conditions and will only notify GDB when
2241 one of them is true. */
2242
2243static void
2244build_target_condition_list (struct bp_location *bl)
2245{
2246 struct bp_location **locp = NULL, **loc2p;
2247 int null_condition_or_parse_error = 0;
2248 int modified = bl->needs_update;
2249 struct bp_location *loc;
2250
8b4f3082 2251 /* Release conditions left over from a previous insert. */
3cde5c42 2252 bl->target_info.conditions.clear ();
8b4f3082 2253
b775012e
LM
2254 /* This is only meaningful if the target is
2255 evaluating conditions and if the user has
2256 opted for condition evaluation on the target's
2257 side. */
2258 if (gdb_evaluates_breakpoint_condition_p ()
2259 || !target_supports_evaluation_of_breakpoint_conditions ())
2260 return;
2261
2262 /* Do a first pass to check for locations with no assigned
7f32a4d5
PA
2263 conditions or conditions that fail to parse to a valid agent
2264 expression bytecode. If any of these happen, then it's no use to
2265 send conditions to the target since this location will always
2266 trigger and generate a response back to GDB. Note we consider
2267 all locations at the same address irrespective of type, i.e.,
2268 even if the locations aren't considered duplicates (e.g.,
2269 software breakpoint and hardware breakpoint at the same
2270 address). */
b775012e
LM
2271 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2272 {
2273 loc = (*loc2p);
2274 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2275 {
2276 if (modified)
2277 {
b775012e
LM
2278 /* Re-parse the conditions since something changed. In that
2279 case we already freed the condition bytecodes (see
2280 force_breakpoint_reinsertion). We just
2281 need to parse the condition to bytecodes again. */
833177a4
PA
2282 loc->cond_bytecode = parse_cond_to_aexpr (bl->address,
2283 loc->cond.get ());
b775012e
LM
2284 }
2285
2286 /* If we have a NULL bytecode expression, it means something
2287 went wrong or we have a null condition expression. */
2288 if (!loc->cond_bytecode)
2289 {
2290 null_condition_or_parse_error = 1;
2291 break;
2292 }
2293 }
2294 }
2295
2296 /* If any of these happened, it means we will have to evaluate the conditions
2297 for the location's address on gdb's side. It is no use keeping bytecodes
2298 for all the other duplicate locations, thus we free all of them here.
2299
2300 This is so we have a finer control over which locations' conditions are
2301 being evaluated by GDB or the remote stub. */
2302 if (null_condition_or_parse_error)
2303 {
2304 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2305 {
2306 loc = (*loc2p);
2307 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2308 {
2309 /* Only go as far as the first NULL bytecode is
2310 located. */
2311 if (!loc->cond_bytecode)
2312 return;
2313
833177a4 2314 loc->cond_bytecode.reset ();
b775012e
LM
2315 }
2316 }
2317 }
2318
7f32a4d5
PA
2319 /* No NULL conditions or failed bytecode generation. Build a
2320 condition list for this location's address. If we have software
2321 and hardware locations at the same address, they aren't
2322 considered duplicates, but we still marge all the conditions
2323 anyway, as it's simpler, and doesn't really make a practical
2324 difference. */
b775012e
LM
2325 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2326 {
2327 loc = (*loc2p);
2328 if (loc->cond
2329 && is_breakpoint (loc->owner)
2330 && loc->pspace->num == bl->pspace->num
2331 && loc->owner->enable_state == bp_enabled
b5fa468f
TBA
2332 && loc->enabled
2333 && !loc->disabled_by_cond)
3cde5c42
PA
2334 {
2335 /* Add the condition to the vector. This will be used later
2336 to send the conditions to the target. */
2337 bl->target_info.conditions.push_back (loc->cond_bytecode.get ());
2338 }
b775012e
LM
2339 }
2340
2341 return;
2342}
2343
d3ce09f5
SS
2344/* Parses a command described by string CMD into an agent expression
2345 bytecode suitable for evaluation by the bytecode interpreter.
2346 Return NULL if there was any error during parsing. */
2347
833177a4 2348static agent_expr_up
d3ce09f5
SS
2349parse_cmd_to_aexpr (CORE_ADDR scope, char *cmd)
2350{
bbc13ae3
KS
2351 const char *cmdrest;
2352 const char *format_start, *format_end;
d3ce09f5
SS
2353 struct gdbarch *gdbarch = get_current_arch ();
2354
833177a4 2355 if (cmd == NULL)
d3ce09f5
SS
2356 return NULL;
2357
2358 cmdrest = cmd;
2359
2360 if (*cmdrest == ',')
2361 ++cmdrest;
f1735a53 2362 cmdrest = skip_spaces (cmdrest);
d3ce09f5
SS
2363
2364 if (*cmdrest++ != '"')
2365 error (_("No format string following the location"));
2366
2367 format_start = cmdrest;
2368
8e481c3b 2369 format_pieces fpieces (&cmdrest);
d3ce09f5
SS
2370
2371 format_end = cmdrest;
2372
2373 if (*cmdrest++ != '"')
2374 error (_("Bad format string, non-terminated '\"'."));
2375
f1735a53 2376 cmdrest = skip_spaces (cmdrest);
d3ce09f5
SS
2377
2378 if (!(*cmdrest == ',' || *cmdrest == '\0'))
2379 error (_("Invalid argument syntax"));
2380
2381 if (*cmdrest == ',')
2382 cmdrest++;
f1735a53 2383 cmdrest = skip_spaces (cmdrest);
d3ce09f5
SS
2384
2385 /* For each argument, make an expression. */
2386
8e481c3b 2387 std::vector<struct expression *> argvec;
d3ce09f5
SS
2388 while (*cmdrest != '\0')
2389 {
bbc13ae3 2390 const char *cmd1;
d3ce09f5
SS
2391
2392 cmd1 = cmdrest;
4d01a485 2393 expression_up expr = parse_exp_1 (&cmd1, scope, block_for_pc (scope), 1);
8e481c3b 2394 argvec.push_back (expr.release ());
d3ce09f5
SS
2395 cmdrest = cmd1;
2396 if (*cmdrest == ',')
2397 ++cmdrest;
2398 }
2399
833177a4
PA
2400 agent_expr_up aexpr;
2401
d3ce09f5
SS
2402 /* We don't want to stop processing, so catch any errors
2403 that may show up. */
a70b8144 2404 try
d3ce09f5 2405 {
036e657b
JB
2406 aexpr = gen_printf (scope, gdbarch, 0, 0,
2407 format_start, format_end - format_start,
8e481c3b 2408 argvec.size (), argvec.data ());
d3ce09f5 2409 }
230d2906 2410 catch (const gdb_exception_error &ex)
d3ce09f5
SS
2411 {
2412 /* If we got here, it means the command could not be parsed to a valid
2413 bytecode expression and thus can't be evaluated on the target's side.
2414 It's no use iterating through the other commands. */
d3ce09f5 2415 }
492d29ea 2416
d3ce09f5
SS
2417 /* We have a valid agent expression, return it. */
2418 return aexpr;
2419}
2420
2421/* Based on location BL, create a list of breakpoint commands to be
2422 passed on to the target. If we have duplicated locations with
2423 different commands, we will add any such to the list. */
2424
2425static void
2426build_target_command_list (struct bp_location *bl)
2427{
2428 struct bp_location **locp = NULL, **loc2p;
2429 int null_command_or_parse_error = 0;
2430 int modified = bl->needs_update;
2431 struct bp_location *loc;
2432
3cde5c42
PA
2433 /* Clear commands left over from a previous insert. */
2434 bl->target_info.tcommands.clear ();
8b4f3082 2435
41fac0cf 2436 if (!target_can_run_breakpoint_commands ())
d3ce09f5
SS
2437 return;
2438
41fac0cf
PA
2439 /* For now, limit to agent-style dprintf breakpoints. */
2440 if (dprintf_style != dprintf_style_agent)
d3ce09f5
SS
2441 return;
2442
7f32a4d5
PA
2443 /* For now, if we have any location at the same address that isn't a
2444 dprintf, don't install the target-side commands, as that would
2445 make the breakpoint not be reported to the core, and we'd lose
41fac0cf
PA
2446 control. */
2447 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2448 {
2449 loc = (*loc2p);
2450 if (is_breakpoint (loc->owner)
2451 && loc->pspace->num == bl->pspace->num
2452 && loc->owner->type != bp_dprintf)
2453 return;
2454 }
2455
d3ce09f5
SS
2456 /* Do a first pass to check for locations with no assigned
2457 conditions or conditions that fail to parse to a valid agent expression
2458 bytecode. If any of these happen, then it's no use to send conditions
2459 to the target since this location will always trigger and generate a
2460 response back to GDB. */
2461 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2462 {
2463 loc = (*loc2p);
2464 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2465 {
2466 if (modified)
2467 {
d3ce09f5
SS
2468 /* Re-parse the commands since something changed. In that
2469 case we already freed the command bytecodes (see
2470 force_breakpoint_reinsertion). We just
2471 need to parse the command to bytecodes again. */
833177a4
PA
2472 loc->cmd_bytecode
2473 = parse_cmd_to_aexpr (bl->address,
2474 loc->owner->extra_string);
d3ce09f5
SS
2475 }
2476
2477 /* If we have a NULL bytecode expression, it means something
2478 went wrong or we have a null command expression. */
2479 if (!loc->cmd_bytecode)
2480 {
2481 null_command_or_parse_error = 1;
2482 break;
2483 }
2484 }
2485 }
2486
2487 /* If anything failed, then we're not doing target-side commands,
2488 and so clean up. */
2489 if (null_command_or_parse_error)
2490 {
2491 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2492 {
2493 loc = (*loc2p);
2494 if (is_breakpoint (loc->owner)
2495 && loc->pspace->num == bl->pspace->num)
2496 {
2497 /* Only go as far as the first NULL bytecode is
2498 located. */
40fb6c5e 2499 if (loc->cmd_bytecode == NULL)
d3ce09f5
SS
2500 return;
2501
833177a4 2502 loc->cmd_bytecode.reset ();
d3ce09f5
SS
2503 }
2504 }
2505 }
2506
7f32a4d5
PA
2507 /* No NULL commands or failed bytecode generation. Build a command
2508 list for all duplicate locations at this location's address.
2509 Note that here we must care for whether the breakpoint location
2510 types are considered duplicates, otherwise, say, if we have a
2511 software and hardware location at the same address, the target
2512 could end up running the commands twice. For the moment, we only
2513 support targets-side commands with dprintf, but it doesn't hurt
2514 to be pedantically correct in case that changes. */
d3ce09f5
SS
2515 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2516 {
2517 loc = (*loc2p);
7f32a4d5
PA
2518 if (breakpoint_locations_match (bl, loc)
2519 && loc->owner->extra_string
d3ce09f5
SS
2520 && is_breakpoint (loc->owner)
2521 && loc->pspace->num == bl->pspace->num
2522 && loc->owner->enable_state == bp_enabled
b5fa468f
TBA
2523 && loc->enabled
2524 && !loc->disabled_by_cond)
3cde5c42
PA
2525 {
2526 /* Add the command to the vector. This will be used later
2527 to send the commands to the target. */
2528 bl->target_info.tcommands.push_back (loc->cmd_bytecode.get ());
2529 }
d3ce09f5
SS
2530 }
2531
2532 bl->target_info.persist = 0;
2533 /* Maybe flag this location as persistent. */
2534 if (bl->owner->type == bp_dprintf && disconnected_dprintf)
2535 bl->target_info.persist = 1;
2536}
2537
833b7ab5
YQ
2538/* Return the kind of breakpoint on address *ADDR. Get the kind
2539 of breakpoint according to ADDR except single-step breakpoint.
2540 Get the kind of single-step breakpoint according to the current
2541 registers state. */
cd6c3b4f
YQ
2542
2543static int
2544breakpoint_kind (struct bp_location *bl, CORE_ADDR *addr)
2545{
833b7ab5
YQ
2546 if (bl->owner->type == bp_single_step)
2547 {
2548 struct thread_info *thr = find_thread_global_id (bl->owner->thread);
2549 struct regcache *regcache;
2550
00431a78 2551 regcache = get_thread_regcache (thr);
833b7ab5
YQ
2552
2553 return gdbarch_breakpoint_kind_from_current_state (bl->gdbarch,
2554 regcache, addr);
2555 }
2556 else
2557 return gdbarch_breakpoint_kind_from_pc (bl->gdbarch, addr);
cd6c3b4f
YQ
2558}
2559
35df4500
TJB
2560/* Insert a low-level "breakpoint" of some type. BL is the breakpoint
2561 location. Any error messages are printed to TMP_ERROR_STREAM; and
3fbb6ffa 2562 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
c30eee59
TJB
2563 Returns 0 for success, 1 if the bp_location type is not supported or
2564 -1 for failure.
879bfdc2 2565
4a64f543
MS
2566 NOTE drow/2003-09-09: This routine could be broken down to an
2567 object-style method for each breakpoint or catchpoint type. */
26bb91f3 2568static int
35df4500 2569insert_bp_location (struct bp_location *bl,
26bb91f3 2570 struct ui_file *tmp_error_stream,
3fbb6ffa 2571 int *disabled_breaks,
dd61ec5c
MW
2572 int *hw_breakpoint_error,
2573 int *hw_bp_error_explained_already)
879bfdc2 2574{
cc06b668 2575 gdb_exception bp_excpt;
879bfdc2 2576
b775012e 2577 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
879bfdc2
DJ
2578 return 0;
2579
35c63cd8
JB
2580 /* Note we don't initialize bl->target_info, as that wipes out
2581 the breakpoint location's shadow_contents if the breakpoint
2582 is still inserted at that location. This in turn breaks
2583 target_read_memory which depends on these buffers when
2584 a memory read is requested at the breakpoint location:
2585 Once the target_info has been wiped, we fail to see that
2586 we have a breakpoint inserted at that address and thus
2587 read the breakpoint instead of returning the data saved in
2588 the breakpoint location's shadow contents. */
0d5ed153 2589 bl->target_info.reqstd_address = bl->address;
35df4500 2590 bl->target_info.placed_address_space = bl->pspace->aspace;
f1310107 2591 bl->target_info.length = bl->length;
8181d85f 2592
b775012e
LM
2593 /* When working with target-side conditions, we must pass all the conditions
2594 for the same breakpoint address down to the target since GDB will not
2595 insert those locations. With a list of breakpoint conditions, the target
2596 can decide when to stop and notify GDB. */
2597
2598 if (is_breakpoint (bl->owner))
2599 {
2600 build_target_condition_list (bl);
d3ce09f5
SS
2601 build_target_command_list (bl);
2602 /* Reset the modification marker. */
b775012e
LM
2603 bl->needs_update = 0;
2604 }
2605
7f32a4d5
PA
2606 /* If "set breakpoint auto-hw" is "on" and a software breakpoint was
2607 set at a read-only address, then a breakpoint location will have
2608 been changed to hardware breakpoint before we get here. If it is
2609 "off" however, error out before actually trying to insert the
2610 breakpoint, with a nicer error message. */
35df4500 2611 if (bl->loc_type == bp_loc_software_breakpoint
7f32a4d5 2612 && !automatic_hardware_breakpoints)
879bfdc2 2613 {
7f32a4d5 2614 mem_region *mr = lookup_mem_region (bl->address);
cc59ec59 2615
7f32a4d5
PA
2616 if (mr != nullptr && mr->attrib.mode != MEM_RW)
2617 {
2618 fprintf_unfiltered (tmp_error_stream,
2619 _("Cannot insert breakpoint %d.\n"
2620 "Cannot set software breakpoint "
2621 "at read-only address %s\n"),
2622 bl->owner->number,
2623 paddress (bl->gdbarch, bl->address));
2624 return 1;
765dc015 2625 }
7f32a4d5
PA
2626 }
2627
2628 if (bl->loc_type == bp_loc_software_breakpoint
2629 || bl->loc_type == bp_loc_hardware_breakpoint)
2630 {
879bfdc2
DJ
2631 /* First check to see if we have to handle an overlay. */
2632 if (overlay_debugging == ovly_off
35df4500
TJB
2633 || bl->section == NULL
2634 || !(section_is_overlay (bl->section)))
879bfdc2
DJ
2635 {
2636 /* No overlay handling: just set the breakpoint. */
a70b8144 2637 try
dd61ec5c 2638 {
0000e5cc
PA
2639 int val;
2640
dd61ec5c 2641 val = bl->owner->ops->insert_location (bl);
0000e5cc 2642 if (val)
688fca4f 2643 bp_excpt = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
dd61ec5c 2644 }
94aeb44b 2645 catch (gdb_exception &e)
dd61ec5c 2646 {
94aeb44b 2647 bp_excpt = std::move (e);
dd61ec5c 2648 }
879bfdc2
DJ
2649 }
2650 else
2651 {
4a64f543 2652 /* This breakpoint is in an overlay section.
879bfdc2
DJ
2653 Shall we set a breakpoint at the LMA? */
2654 if (!overlay_events_enabled)
2655 {
2656 /* Yes -- overlay event support is not active,
2657 so we must try to set a breakpoint at the LMA.
2658 This will not work for a hardware breakpoint. */
35df4500 2659 if (bl->loc_type == bp_loc_hardware_breakpoint)
8a3fe4f8 2660 warning (_("hardware breakpoint %d not supported in overlay!"),
35df4500 2661 bl->owner->number);
879bfdc2
DJ
2662 else
2663 {
35df4500
TJB
2664 CORE_ADDR addr = overlay_unmapped_address (bl->address,
2665 bl->section);
879bfdc2 2666 /* Set a software (trap) breakpoint at the LMA. */
35df4500 2667 bl->overlay_target_info = bl->target_info;
0d5ed153 2668 bl->overlay_target_info.reqstd_address = addr;
0000e5cc
PA
2669
2670 /* No overlay handling: just set the breakpoint. */
a70b8144 2671 try
0000e5cc
PA
2672 {
2673 int val;
2674
579c6ad9 2675 bl->overlay_target_info.kind
cd6c3b4f
YQ
2676 = breakpoint_kind (bl, &addr);
2677 bl->overlay_target_info.placed_address = addr;
0000e5cc
PA
2678 val = target_insert_breakpoint (bl->gdbarch,
2679 &bl->overlay_target_info);
2680 if (val)
688fca4f
PA
2681 bp_excpt
2682 = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
0000e5cc 2683 }
94aeb44b 2684 catch (gdb_exception &e)
0000e5cc 2685 {
94aeb44b 2686 bp_excpt = std::move (e);
0000e5cc
PA
2687 }
2688
688fca4f 2689 if (bp_excpt.reason != 0)
99361f52 2690 fprintf_unfiltered (tmp_error_stream,
3e43a32a
MS
2691 "Overlay breakpoint %d "
2692 "failed: in ROM?\n",
35df4500 2693 bl->owner->number);
879bfdc2
DJ
2694 }
2695 }
2696 /* Shall we set a breakpoint at the VMA? */
35df4500 2697 if (section_is_mapped (bl->section))
879bfdc2
DJ
2698 {
2699 /* Yes. This overlay section is mapped into memory. */
a70b8144 2700 try
dda83cd7 2701 {
0000e5cc
PA
2702 int val;
2703
dda83cd7 2704 val = bl->owner->ops->insert_location (bl);
0000e5cc 2705 if (val)
688fca4f 2706 bp_excpt = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
dda83cd7 2707 }
94aeb44b 2708 catch (gdb_exception &e)
dda83cd7 2709 {
94aeb44b 2710 bp_excpt = std::move (e);
dda83cd7 2711 }
879bfdc2
DJ
2712 }
2713 else
2714 {
2715 /* No. This breakpoint will not be inserted.
2716 No error, but do not mark the bp as 'inserted'. */
2717 return 0;
2718 }
2719 }
2720
688fca4f 2721 if (bp_excpt.reason != 0)
879bfdc2
DJ
2722 {
2723 /* Can't set the breakpoint. */
0000e5cc
PA
2724
2725 /* In some cases, we might not be able to insert a
2726 breakpoint in a shared library that has already been
2727 removed, but we have not yet processed the shlib unload
2728 event. Unfortunately, some targets that implement
076855f9
PA
2729 breakpoint insertion themselves can't tell why the
2730 breakpoint insertion failed (e.g., the remote target
2731 doesn't define error codes), so we must treat generic
2732 errors as memory errors. */
688fca4f
PA
2733 if (bp_excpt.reason == RETURN_ERROR
2734 && (bp_excpt.error == GENERIC_ERROR
2735 || bp_excpt.error == MEMORY_ERROR)
076855f9 2736 && bl->loc_type == bp_loc_software_breakpoint
08351840 2737 && (solib_name_from_address (bl->pspace, bl->address)
d03de421
PA
2738 || shared_objfile_contains_address_p (bl->pspace,
2739 bl->address)))
879bfdc2 2740 {
4a64f543 2741 /* See also: disable_breakpoints_in_shlibs. */
35df4500 2742 bl->shlib_disabled = 1;
76727919 2743 gdb::observers::breakpoint_modified.notify (bl->owner);
3fbb6ffa
TJB
2744 if (!*disabled_breaks)
2745 {
2746 fprintf_unfiltered (tmp_error_stream,
2747 "Cannot insert breakpoint %d.\n",
2748 bl->owner->number);
2749 fprintf_unfiltered (tmp_error_stream,
2750 "Temporarily disabling shared "
2751 "library breakpoints:\n");
2752 }
2753 *disabled_breaks = 1;
879bfdc2 2754 fprintf_unfiltered (tmp_error_stream,
35df4500 2755 "breakpoint #%d\n", bl->owner->number);
0000e5cc 2756 return 0;
879bfdc2
DJ
2757 }
2758 else
879bfdc2 2759 {
35df4500 2760 if (bl->loc_type == bp_loc_hardware_breakpoint)
879bfdc2 2761 {
0000e5cc 2762 *hw_breakpoint_error = 1;
688fca4f 2763 *hw_bp_error_explained_already = bp_excpt.message != NULL;
dda83cd7
SM
2764 fprintf_unfiltered (tmp_error_stream,
2765 "Cannot insert hardware breakpoint %d%s",
2766 bl->owner->number,
688fca4f 2767 bp_excpt.message ? ":" : ".\n");
dda83cd7
SM
2768 if (bp_excpt.message != NULL)
2769 fprintf_unfiltered (tmp_error_stream, "%s.\n",
3d6e9d23 2770 bp_excpt.what ());
879bfdc2
DJ
2771 }
2772 else
2773 {
688fca4f 2774 if (bp_excpt.message == NULL)
0000e5cc 2775 {
1ccbe998 2776 std::string message
0000e5cc
PA
2777 = memory_error_message (TARGET_XFER_E_IO,
2778 bl->gdbarch, bl->address);
0000e5cc
PA
2779
2780 fprintf_unfiltered (tmp_error_stream,
2781 "Cannot insert breakpoint %d.\n"
2782 "%s\n",
1ccbe998 2783 bl->owner->number, message.c_str ());
0000e5cc
PA
2784 }
2785 else
2786 {
2787 fprintf_unfiltered (tmp_error_stream,
2788 "Cannot insert breakpoint %d: %s\n",
2789 bl->owner->number,
3d6e9d23 2790 bp_excpt.what ());
0000e5cc 2791 }
879bfdc2 2792 }
0000e5cc 2793 return 1;
879bfdc2
DJ
2794
2795 }
2796 }
2797 else
35df4500 2798 bl->inserted = 1;
879bfdc2 2799
0000e5cc 2800 return 0;
879bfdc2
DJ
2801 }
2802
35df4500 2803 else if (bl->loc_type == bp_loc_hardware_watchpoint
879bfdc2 2804 /* NOTE drow/2003-09-08: This state only exists for removing
4a64f543 2805 watchpoints. It's not clear that it's necessary... */
35df4500 2806 && bl->owner->disposition != disp_del_at_next_stop)
879bfdc2 2807 {
0000e5cc
PA
2808 int val;
2809
77b06cd7
TJB
2810 gdb_assert (bl->owner->ops != NULL
2811 && bl->owner->ops->insert_location != NULL);
2812
2813 val = bl->owner->ops->insert_location (bl);
85d721b8
PA
2814
2815 /* If trying to set a read-watchpoint, and it turns out it's not
2816 supported, try emulating one with an access watchpoint. */
35df4500 2817 if (val == 1 && bl->watchpoint_type == hw_read)
85d721b8
PA
2818 {
2819 struct bp_location *loc, **loc_temp;
2820
2821 /* But don't try to insert it, if there's already another
2822 hw_access location that would be considered a duplicate
2823 of this one. */
2824 ALL_BP_LOCATIONS (loc, loc_temp)
35df4500 2825 if (loc != bl
85d721b8 2826 && loc->watchpoint_type == hw_access
35df4500 2827 && watchpoint_locations_match (bl, loc))
85d721b8 2828 {
35df4500
TJB
2829 bl->duplicate = 1;
2830 bl->inserted = 1;
2831 bl->target_info = loc->target_info;
2832 bl->watchpoint_type = hw_access;
85d721b8
PA
2833 val = 0;
2834 break;
2835 }
2836
2837 if (val == 1)
2838 {
77b06cd7
TJB
2839 bl->watchpoint_type = hw_access;
2840 val = bl->owner->ops->insert_location (bl);
2841
2842 if (val)
2843 /* Back to the original value. */
2844 bl->watchpoint_type = hw_read;
85d721b8
PA
2845 }
2846 }
2847
35df4500 2848 bl->inserted = (val == 0);
879bfdc2
DJ
2849 }
2850
35df4500 2851 else if (bl->owner->type == bp_catchpoint)
879bfdc2 2852 {
0000e5cc
PA
2853 int val;
2854
77b06cd7
TJB
2855 gdb_assert (bl->owner->ops != NULL
2856 && bl->owner->ops->insert_location != NULL);
2857
2858 val = bl->owner->ops->insert_location (bl);
2859 if (val)
2860 {
2861 bl->owner->enable_state = bp_disabled;
2862
2863 if (val == 1)
2864 warning (_("\
2865Error inserting catchpoint %d: Your system does not support this type\n\
2866of catchpoint."), bl->owner->number);
2867 else
2868 warning (_("Error inserting catchpoint %d."), bl->owner->number);
2869 }
2870
2871 bl->inserted = (val == 0);
1640b821
DJ
2872
2873 /* We've already printed an error message if there was a problem
2874 inserting this catchpoint, and we've disabled the catchpoint,
2875 so just return success. */
2876 return 0;
879bfdc2
DJ
2877 }
2878
2879 return 0;
2880}
2881
6c95b8df
PA
2882/* This function is called when program space PSPACE is about to be
2883 deleted. It takes care of updating breakpoints to not reference
2884 PSPACE anymore. */
2885
2886void
2887breakpoint_program_space_exit (struct program_space *pspace)
2888{
2889 struct breakpoint *b, *b_temp;
876fa593 2890 struct bp_location *loc, **loc_temp;
6c95b8df
PA
2891
2892 /* Remove any breakpoint that was set through this program space. */
2893 ALL_BREAKPOINTS_SAFE (b, b_temp)
2894 {
2895 if (b->pspace == pspace)
2896 delete_breakpoint (b);
2897 }
2898
2899 /* Breakpoints set through other program spaces could have locations
2900 bound to PSPACE as well. Remove those. */
876fa593 2901 ALL_BP_LOCATIONS (loc, loc_temp)
6c95b8df
PA
2902 {
2903 struct bp_location *tmp;
2904
2905 if (loc->pspace == pspace)
2906 {
2bdf28a0 2907 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
6c95b8df
PA
2908 if (loc->owner->loc == loc)
2909 loc->owner->loc = loc->next;
2910 else
2911 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
2912 if (tmp->next == loc)
2913 {
2914 tmp->next = loc->next;
2915 break;
2916 }
2917 }
2918 }
2919
2920 /* Now update the global location list to permanently delete the
2921 removed locations above. */
44702360 2922 update_global_location_list (UGLL_DONT_INSERT);
6c95b8df
PA
2923}
2924
74960c60
VP
2925/* Make sure all breakpoints are inserted in inferior.
2926 Throws exception on any error.
2927 A breakpoint that is already inserted won't be inserted
2928 again, so calling this function twice is safe. */
2929void
2930insert_breakpoints (void)
2931{
2932 struct breakpoint *bpt;
2933
2934 ALL_BREAKPOINTS (bpt)
2935 if (is_hardware_watchpoint (bpt))
3a5c3e22
PA
2936 {
2937 struct watchpoint *w = (struct watchpoint *) bpt;
2938
2939 update_watchpoint (w, 0 /* don't reparse. */);
2940 }
74960c60 2941
04086b45
PA
2942 /* Updating watchpoints creates new locations, so update the global
2943 location list. Explicitly tell ugll to insert locations and
7f32a4d5
PA
2944 ignore breakpoints_always_inserted_mode. Also,
2945 update_global_location_list tries to "upgrade" software
2946 breakpoints to hardware breakpoints to handle "set breakpoint
2947 auto-hw", so we need to call it even if we don't have new
2948 locations. */
04086b45 2949 update_global_location_list (UGLL_INSERT);
74960c60
VP
2950}
2951
20388dd6
YQ
2952/* Invoke CALLBACK for each of bp_location. */
2953
2954void
0406545d 2955iterate_over_bp_locations (gdb::function_view<void (bp_location *)> callback)
20388dd6
YQ
2956{
2957 struct bp_location *loc, **loc_tmp;
2958
2959 ALL_BP_LOCATIONS (loc, loc_tmp)
2960 {
0406545d 2961 callback (loc);
20388dd6
YQ
2962 }
2963}
2964
b775012e
LM
2965/* This is used when we need to synch breakpoint conditions between GDB and the
2966 target. It is the case with deleting and disabling of breakpoints when using
2967 always-inserted mode. */
2968
2969static void
2970update_inserted_breakpoint_locations (void)
2971{
2972 struct bp_location *bl, **blp_tmp;
2973 int error_flag = 0;
2974 int val = 0;
2975 int disabled_breaks = 0;
2976 int hw_breakpoint_error = 0;
dd61ec5c 2977 int hw_bp_details_reported = 0;
b775012e 2978
d7e74731 2979 string_file tmp_error_stream;
b775012e
LM
2980
2981 /* Explicitly mark the warning -- this will only be printed if
2982 there was an error. */
d7e74731 2983 tmp_error_stream.puts ("Warning:\n");
b775012e 2984
5ed8105e 2985 scoped_restore_current_pspace_and_thread restore_pspace_thread;
b775012e
LM
2986
2987 ALL_BP_LOCATIONS (bl, blp_tmp)
2988 {
2989 /* We only want to update software breakpoints and hardware
2990 breakpoints. */
2991 if (!is_breakpoint (bl->owner))
2992 continue;
2993
2994 /* We only want to update locations that are already inserted
2995 and need updating. This is to avoid unwanted insertion during
2996 deletion of breakpoints. */
4daf1902 2997 if (!bl->inserted || !bl->needs_update)
b775012e
LM
2998 continue;
2999
3000 switch_to_program_space_and_thread (bl->pspace);
3001
3002 /* For targets that support global breakpoints, there's no need
3003 to select an inferior to insert breakpoint to. In fact, even
3004 if we aren't attached to any process yet, we should still
3005 insert breakpoints. */
f5656ead 3006 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
55f6301a 3007 && (inferior_ptid == null_ptid || !target_has_execution ()))
b775012e
LM
3008 continue;
3009
d7e74731 3010 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
dd61ec5c 3011 &hw_breakpoint_error, &hw_bp_details_reported);
b775012e
LM
3012 if (val)
3013 error_flag = val;
3014 }
3015
3016 if (error_flag)
3017 {
223ffa71 3018 target_terminal::ours_for_output ();
b775012e
LM
3019 error_stream (tmp_error_stream);
3020 }
b775012e
LM
3021}
3022
c30eee59 3023/* Used when starting or continuing the program. */
c906108c 3024
74960c60
VP
3025static void
3026insert_breakpoint_locations (void)
c906108c 3027{
a5606eee 3028 struct breakpoint *bpt;
35df4500 3029 struct bp_location *bl, **blp_tmp;
eacd795a 3030 int error_flag = 0;
c906108c 3031 int val = 0;
3fbb6ffa 3032 int disabled_breaks = 0;
81d0cc19 3033 int hw_breakpoint_error = 0;
dd61ec5c 3034 int hw_bp_error_explained_already = 0;
c906108c 3035
d7e74731
PA
3036 string_file tmp_error_stream;
3037
81d0cc19
GS
3038 /* Explicitly mark the warning -- this will only be printed if
3039 there was an error. */
d7e74731 3040 tmp_error_stream.puts ("Warning:\n");
6c95b8df 3041
5ed8105e 3042 scoped_restore_current_pspace_and_thread restore_pspace_thread;
6c95b8df 3043
35df4500 3044 ALL_BP_LOCATIONS (bl, blp_tmp)
879bfdc2 3045 {
b775012e 3046 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
879bfdc2
DJ
3047 continue;
3048
4a64f543
MS
3049 /* There is no point inserting thread-specific breakpoints if
3050 the thread no longer exists. ALL_BP_LOCATIONS bp_location
3051 has BL->OWNER always non-NULL. */
35df4500 3052 if (bl->owner->thread != -1
5d5658a1 3053 && !valid_global_thread_id (bl->owner->thread))
f365de73
AS
3054 continue;
3055
35df4500 3056 switch_to_program_space_and_thread (bl->pspace);
6c95b8df
PA
3057
3058 /* For targets that support global breakpoints, there's no need
3059 to select an inferior to insert breakpoint to. In fact, even
3060 if we aren't attached to any process yet, we should still
3061 insert breakpoints. */
f5656ead 3062 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
55f6301a 3063 && (inferior_ptid == null_ptid || !target_has_execution ()))
6c95b8df
PA
3064 continue;
3065
d7e74731 3066 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
dd61ec5c 3067 &hw_breakpoint_error, &hw_bp_error_explained_already);
879bfdc2 3068 if (val)
eacd795a 3069 error_flag = val;
879bfdc2 3070 }
c906108c 3071
4a64f543
MS
3072 /* If we failed to insert all locations of a watchpoint, remove
3073 them, as half-inserted watchpoint is of limited use. */
a5606eee
VP
3074 ALL_BREAKPOINTS (bpt)
3075 {
3076 int some_failed = 0;
3077 struct bp_location *loc;
3078
3079 if (!is_hardware_watchpoint (bpt))
3080 continue;
3081
d6b74ac4 3082 if (!breakpoint_enabled (bpt))
a5606eee 3083 continue;
74960c60
VP
3084
3085 if (bpt->disposition == disp_del_at_next_stop)
3086 continue;
a5606eee
VP
3087
3088 for (loc = bpt->loc; loc; loc = loc->next)
56710373 3089 if (!loc->inserted && should_be_inserted (loc))
a5606eee
VP
3090 {
3091 some_failed = 1;
3092 break;
3093 }
3094 if (some_failed)
3095 {
3096 for (loc = bpt->loc; loc; loc = loc->next)
3097 if (loc->inserted)
834c0d03 3098 remove_breakpoint (loc);
a5606eee
VP
3099
3100 hw_breakpoint_error = 1;
d7e74731
PA
3101 tmp_error_stream.printf ("Could not insert "
3102 "hardware watchpoint %d.\n",
3103 bpt->number);
eacd795a 3104 error_flag = -1;
a5606eee
VP
3105 }
3106 }
3107
eacd795a 3108 if (error_flag)
81d0cc19
GS
3109 {
3110 /* If a hardware breakpoint or watchpoint was inserted, add a
dda83cd7 3111 message about possibly exhausted resources. */
dd61ec5c 3112 if (hw_breakpoint_error && !hw_bp_error_explained_already)
81d0cc19 3113 {
d7e74731 3114 tmp_error_stream.printf ("Could not insert hardware breakpoints:\n\
c6510018 3115You may have requested too many hardware breakpoints/watchpoints.\n");
81d0cc19 3116 }
223ffa71 3117 target_terminal::ours_for_output ();
81d0cc19
GS
3118 error_stream (tmp_error_stream);
3119 }
c906108c
SS
3120}
3121
c30eee59
TJB
3122/* Used when the program stops.
3123 Returns zero if successful, or non-zero if there was a problem
3124 removing a breakpoint location. */
3125
c906108c 3126int
fba45db2 3127remove_breakpoints (void)
c906108c 3128{
35df4500 3129 struct bp_location *bl, **blp_tmp;
3a1bae8e 3130 int val = 0;
c906108c 3131
35df4500 3132 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3133 {
1e4d1764 3134 if (bl->inserted && !is_tracepoint (bl->owner))
834c0d03 3135 val |= remove_breakpoint (bl);
c5aa993b 3136 }
3a1bae8e 3137 return val;
c906108c
SS
3138}
3139
49fa26b0
PA
3140/* When a thread exits, remove breakpoints that are related to
3141 that thread. */
3142
3143static void
3144remove_threaded_breakpoints (struct thread_info *tp, int silent)
3145{
3146 struct breakpoint *b, *b_tmp;
3147
3148 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3149 {
5d5658a1 3150 if (b->thread == tp->global_num && user_breakpoint_p (b))
49fa26b0
PA
3151 {
3152 b->disposition = disp_del_at_next_stop;
3153
3154 printf_filtered (_("\
43792cf0
PA
3155Thread-specific breakpoint %d deleted - thread %s no longer in the thread list.\n"),
3156 b->number, print_thread_id (tp));
49fa26b0
PA
3157
3158 /* Hide it from the user. */
3159 b->number = 0;
3160 }
3161 }
3162}
3163
f3869b1a 3164/* See breakpoint.h. */
6c95b8df 3165
f3869b1a 3166void
00431a78 3167remove_breakpoints_inf (inferior *inf)
6c95b8df 3168{
35df4500 3169 struct bp_location *bl, **blp_tmp;
6c95b8df 3170 int val;
6c95b8df 3171
35df4500 3172 ALL_BP_LOCATIONS (bl, blp_tmp)
6c95b8df 3173 {
35df4500 3174 if (bl->pspace != inf->pspace)
6c95b8df
PA
3175 continue;
3176
fc126975 3177 if (bl->inserted && !bl->target_info.persist)
6c95b8df 3178 {
834c0d03 3179 val = remove_breakpoint (bl);
6c95b8df 3180 if (val != 0)
f3869b1a 3181 return;
6c95b8df
PA
3182 }
3183 }
6c95b8df
PA
3184}
3185
e58b0e63
PA
3186static int internal_breakpoint_number = -1;
3187
84f4c1fe
PM
3188/* Set the breakpoint number of B, depending on the value of INTERNAL.
3189 If INTERNAL is non-zero, the breakpoint number will be populated
3190 from internal_breakpoint_number and that variable decremented.
e5dd4106 3191 Otherwise the breakpoint number will be populated from
84f4c1fe
PM
3192 breakpoint_count and that value incremented. Internal breakpoints
3193 do not set the internal var bpnum. */
3194static void
3195set_breakpoint_number (int internal, struct breakpoint *b)
3196{
3197 if (internal)
3198 b->number = internal_breakpoint_number--;
3199 else
3200 {
3201 set_breakpoint_count (breakpoint_count + 1);
3202 b->number = breakpoint_count;
3203 }
3204}
3205
e62c965a 3206static struct breakpoint *
a6d9a66e 3207create_internal_breakpoint (struct gdbarch *gdbarch,
06edf0c0 3208 CORE_ADDR address, enum bptype type,
c0a91b2b 3209 const struct breakpoint_ops *ops)
e62c965a 3210{
51abb421 3211 symtab_and_line sal;
e62c965a
PP
3212 sal.pc = address;
3213 sal.section = find_pc_overlay (sal.pc);
6c95b8df 3214 sal.pspace = current_program_space;
e62c965a 3215
51abb421 3216 breakpoint *b = set_raw_breakpoint (gdbarch, sal, type, ops);
e62c965a
PP
3217 b->number = internal_breakpoint_number--;
3218 b->disposition = disp_donttouch;
3219
3220 return b;
3221}
3222
17450429
PP
3223static const char *const longjmp_names[] =
3224 {
3225 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
3226 };
3227#define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
3228
3229/* Per-objfile data private to breakpoint.c. */
3230struct breakpoint_objfile_data
3231{
3232 /* Minimal symbol for "_ovly_debug_event" (if any). */
43dce439 3233 struct bound_minimal_symbol overlay_msym {};
17450429
PP
3234
3235 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
43dce439 3236 struct bound_minimal_symbol longjmp_msym[NUM_LONGJMP_NAMES] {};
17450429 3237
28106bc2 3238 /* True if we have looked for longjmp probes. */
43dce439 3239 int longjmp_searched = 0;
28106bc2 3240
45461e0d
SM
3241 /* SystemTap probe points for longjmp (if any). These are non-owning
3242 references. */
3243 std::vector<probe *> longjmp_probes;
28106bc2 3244
17450429 3245 /* Minimal symbol for "std::terminate()" (if any). */
43dce439 3246 struct bound_minimal_symbol terminate_msym {};
17450429
PP
3247
3248 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
43dce439 3249 struct bound_minimal_symbol exception_msym {};
28106bc2
SDJ
3250
3251 /* True if we have looked for exception probes. */
43dce439 3252 int exception_searched = 0;
28106bc2 3253
45461e0d
SM
3254 /* SystemTap probe points for unwinding (if any). These are non-owning
3255 references. */
3256 std::vector<probe *> exception_probes;
17450429
PP
3257};
3258
51d3063a
TT
3259static const struct objfile_key<breakpoint_objfile_data>
3260 breakpoint_objfile_key;
17450429
PP
3261
3262/* Minimal symbol not found sentinel. */
3263static struct minimal_symbol msym_not_found;
3264
3265/* Returns TRUE if MSYM point to the "not found" sentinel. */
3266
3267static int
3268msym_not_found_p (const struct minimal_symbol *msym)
3269{
3270 return msym == &msym_not_found;
3271}
3272
3273/* Return per-objfile data needed by breakpoint.c.
3274 Allocate the data if necessary. */
3275
3276static struct breakpoint_objfile_data *
3277get_breakpoint_objfile_data (struct objfile *objfile)
3278{
3279 struct breakpoint_objfile_data *bp_objfile_data;
3280
51d3063a 3281 bp_objfile_data = breakpoint_objfile_key.get (objfile);
17450429 3282 if (bp_objfile_data == NULL)
51d3063a 3283 bp_objfile_data = breakpoint_objfile_key.emplace (objfile);
17450429
PP
3284 return bp_objfile_data;
3285}
3286
e62c965a 3287static void
af02033e 3288create_overlay_event_breakpoint (void)
e62c965a 3289{
af02033e 3290 const char *const func_name = "_ovly_debug_event";
e62c965a 3291
2030c079 3292 for (objfile *objfile : current_program_space->objfiles ())
69de3c6a
PP
3293 {
3294 struct breakpoint *b;
17450429
PP
3295 struct breakpoint_objfile_data *bp_objfile_data;
3296 CORE_ADDR addr;
67994074 3297 struct explicit_location explicit_loc;
69de3c6a 3298
17450429
PP
3299 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3300
3b7344d5 3301 if (msym_not_found_p (bp_objfile_data->overlay_msym.minsym))
17450429
PP
3302 continue;
3303
3b7344d5 3304 if (bp_objfile_data->overlay_msym.minsym == NULL)
17450429 3305 {
3b7344d5 3306 struct bound_minimal_symbol m;
17450429
PP
3307
3308 m = lookup_minimal_symbol_text (func_name, objfile);
3b7344d5 3309 if (m.minsym == NULL)
17450429
PP
3310 {
3311 /* Avoid future lookups in this objfile. */
3b7344d5 3312 bp_objfile_data->overlay_msym.minsym = &msym_not_found;
17450429
PP
3313 continue;
3314 }
3315 bp_objfile_data->overlay_msym = m;
3316 }
e62c965a 3317
77e371c0 3318 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->overlay_msym);
08feed99 3319 b = create_internal_breakpoint (objfile->arch (), addr,
dda83cd7 3320 bp_overlay_event,
06edf0c0 3321 &internal_breakpoint_ops);
67994074
KS
3322 initialize_explicit_location (&explicit_loc);
3323 explicit_loc.function_name = ASTRDUP (func_name);
d28cd78a 3324 b->location = new_explicit_location (&explicit_loc);
e62c965a 3325
69de3c6a 3326 if (overlay_debugging == ovly_auto)
dda83cd7
SM
3327 {
3328 b->enable_state = bp_enabled;
3329 overlay_events_enabled = 1;
3330 }
69de3c6a
PP
3331 else
3332 {
dda83cd7
SM
3333 b->enable_state = bp_disabled;
3334 overlay_events_enabled = 0;
69de3c6a 3335 }
e62c965a 3336 }
e62c965a
PP
3337}
3338
2a7f6487
TV
3339/* Install a master longjmp breakpoint for OBJFILE using a probe. Return
3340 true if a breakpoint was installed. */
3341
3342static bool
3343create_longjmp_master_breakpoint_probe (objfile *objfile)
0fd8e87f 3344{
2a7f6487
TV
3345 struct gdbarch *gdbarch = objfile->arch ();
3346 struct breakpoint_objfile_data *bp_objfile_data
3347 = get_breakpoint_objfile_data (objfile);
0fd8e87f 3348
2a7f6487 3349 if (!bp_objfile_data->longjmp_searched)
94c93c35 3350 {
2a7f6487
TV
3351 std::vector<probe *> ret
3352 = find_probes_in_objfile (objfile, "libc", "longjmp");
af02033e 3353
2a7f6487 3354 if (!ret.empty ())
94c93c35 3355 {
2a7f6487
TV
3356 /* We are only interested in checking one element. */
3357 probe *p = ret[0];
0fd8e87f 3358
2a7f6487
TV
3359 if (!p->can_evaluate_arguments ())
3360 {
3361 /* We cannot use the probe interface here,
3362 because it does not know how to evaluate
3363 arguments. */
3364 ret.clear ();
3365 }
3366 }
3367 bp_objfile_data->longjmp_probes = ret;
3368 bp_objfile_data->longjmp_searched = 1;
3369 }
0fd8e87f 3370
2a7f6487
TV
3371 if (bp_objfile_data->longjmp_probes.empty ())
3372 return false;
17450429 3373
2a7f6487
TV
3374 for (probe *p : bp_objfile_data->longjmp_probes)
3375 {
3376 struct breakpoint *b;
25f9533e 3377
2a7f6487
TV
3378 b = create_internal_breakpoint (gdbarch,
3379 p->get_relocated_address (objfile),
3380 bp_longjmp_master,
3381 &internal_breakpoint_ops);
3382 b->location = new_probe_location ("-probe-stap libc:longjmp");
3383 b->enable_state = bp_disabled;
3384 }
aed57c53 3385
2a7f6487
TV
3386 return true;
3387}
25f9533e 3388
2a7f6487
TV
3389/* Install master longjmp breakpoints for OBJFILE using longjmp_names.
3390 Return true if at least one breakpoint was installed. */
3391
3392static bool
3393create_longjmp_master_breakpoint_names (objfile *objfile)
3394{
3395 struct gdbarch *gdbarch = objfile->arch ();
3396 if (!gdbarch_get_longjmp_target_p (gdbarch))
3397 return false;
3398
3399 struct breakpoint_objfile_data *bp_objfile_data
3400 = get_breakpoint_objfile_data (objfile);
3401 unsigned int installed_bp = 0;
3402
3403 for (int i = 0; i < NUM_LONGJMP_NAMES; i++)
3404 {
3405 struct breakpoint *b;
3406 const char *func_name;
3407 CORE_ADDR addr;
3408 struct explicit_location explicit_loc;
3409
3410 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i].minsym))
3411 continue;
28106bc2 3412
2a7f6487
TV
3413 func_name = longjmp_names[i];
3414 if (bp_objfile_data->longjmp_msym[i].minsym == NULL)
3415 {
3416 struct bound_minimal_symbol m;
3417
3418 m = lookup_minimal_symbol_text (func_name, objfile);
3419 if (m.minsym == NULL)
3420 {
3421 /* Prevent future lookups in this objfile. */
3422 bp_objfile_data->longjmp_msym[i].minsym = &msym_not_found;
94c93c35
TT
3423 continue;
3424 }
2a7f6487
TV
3425 bp_objfile_data->longjmp_msym[i] = m;
3426 }
28106bc2 3427
2a7f6487
TV
3428 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->longjmp_msym[i]);
3429 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master,
3430 &internal_breakpoint_ops);
3431 initialize_explicit_location (&explicit_loc);
3432 explicit_loc.function_name = ASTRDUP (func_name);
3433 b->location = new_explicit_location (&explicit_loc);
3434 b->enable_state = bp_disabled;
3435 installed_bp++;
3436 }
28106bc2 3437
2a7f6487
TV
3438 return installed_bp > 0;
3439}
0fd8e87f 3440
2a7f6487 3441/* Create a master longjmp breakpoint. */
17450429 3442
2a7f6487
TV
3443static void
3444create_longjmp_master_breakpoint (void)
3445{
3446 scoped_restore_current_program_space restore_pspace;
aed57c53 3447
2a7f6487
TV
3448 for (struct program_space *pspace : program_spaces)
3449 {
3450 set_current_program_space (pspace);
17450429 3451
2a7f6487
TV
3452 for (objfile *obj : current_program_space->objfiles ())
3453 {
3454 /* Skip separate debug object, it's handled in the loop below. */
3455 if (obj->separate_debug_objfile_backlink != nullptr)
3456 continue;
3457
3458 /* Try a probe kind breakpoint on main objfile. */
3459 if (create_longjmp_master_breakpoint_probe (obj))
3460 continue;
3461
3462 /* Try longjmp_names kind breakpoints on main and separate_debug
3463 objfiles. */
3464 for (objfile *debug_objfile : obj->separate_debug_objfiles ())
3465 if (create_longjmp_master_breakpoint_names (debug_objfile))
3466 break;
94c93c35
TT
3467 }
3468 }
0fd8e87f
UW
3469}
3470
af02033e 3471/* Create a master std::terminate breakpoint. */
aa7d318d 3472static void
af02033e 3473create_std_terminate_master_breakpoint (void)
aa7d318d 3474{
af02033e 3475 const char *const func_name = "std::terminate()";
aa7d318d 3476
5ed8105e 3477 scoped_restore_current_program_space restore_pspace;
aa7d318d 3478
94c93c35
TT
3479 for (struct program_space *pspace : program_spaces)
3480 {
3481 CORE_ADDR addr;
17450429 3482
94c93c35 3483 set_current_program_space (pspace);
17450429 3484
94c93c35
TT
3485 for (objfile *objfile : current_program_space->objfiles ())
3486 {
3487 struct breakpoint *b;
3488 struct breakpoint_objfile_data *bp_objfile_data;
3489 struct explicit_location explicit_loc;
aa7d318d 3490
94c93c35 3491 bp_objfile_data = get_breakpoint_objfile_data (objfile);
aa7d318d 3492
94c93c35
TT
3493 if (msym_not_found_p (bp_objfile_data->terminate_msym.minsym))
3494 continue;
17450429 3495
94c93c35
TT
3496 if (bp_objfile_data->terminate_msym.minsym == NULL)
3497 {
3498 struct bound_minimal_symbol m;
17450429 3499
94c93c35
TT
3500 m = lookup_minimal_symbol (func_name, NULL, objfile);
3501 if (m.minsym == NULL || (MSYMBOL_TYPE (m.minsym) != mst_text
3502 && MSYMBOL_TYPE (m.minsym) != mst_file_text))
3503 {
3504 /* Prevent future lookups in this objfile. */
3505 bp_objfile_data->terminate_msym.minsym = &msym_not_found;
3506 continue;
3507 }
3508 bp_objfile_data->terminate_msym = m;
3509 }
aa7d318d 3510
94c93c35
TT
3511 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->terminate_msym);
3512 b = create_internal_breakpoint (objfile->arch (), addr,
3513 bp_std_terminate_master,
3514 &internal_breakpoint_ops);
3515 initialize_explicit_location (&explicit_loc);
3516 explicit_loc.function_name = ASTRDUP (func_name);
3517 b->location = new_explicit_location (&explicit_loc);
3518 b->enable_state = bp_disabled;
3519 }
3520 }
aa7d318d
TT
3521}
3522
1940319c
TV
3523/* Install a master breakpoint on the unwinder's debug hook for OBJFILE using a
3524 probe. Return true if a breakpoint was installed. */
186c406b 3525
1940319c
TV
3526static bool
3527create_exception_master_breakpoint_probe (objfile *objfile)
186c406b 3528{
1940319c
TV
3529 struct breakpoint *b;
3530 struct gdbarch *gdbarch;
3531 struct breakpoint_objfile_data *bp_objfile_data;
186c406b 3532
1940319c 3533 bp_objfile_data = get_breakpoint_objfile_data (objfile);
17450429 3534
1940319c
TV
3535 /* We prefer the SystemTap probe point if it exists. */
3536 if (!bp_objfile_data->exception_searched)
3537 {
3538 std::vector<probe *> ret
3539 = find_probes_in_objfile (objfile, "libgcc", "unwind");
17450429 3540
1940319c 3541 if (!ret.empty ())
28106bc2 3542 {
1940319c
TV
3543 /* We are only interested in checking one element. */
3544 probe *p = ret[0];
25f9533e 3545
1940319c 3546 if (!p->can_evaluate_arguments ())
25f9533e 3547 {
1940319c
TV
3548 /* We cannot use the probe interface here, because it does
3549 not know how to evaluate arguments. */
3550 ret.clear ();
25f9533e 3551 }
28106bc2 3552 }
1940319c
TV
3553 bp_objfile_data->exception_probes = ret;
3554 bp_objfile_data->exception_searched = 1;
3555 }
28106bc2 3556
1940319c
TV
3557 if (bp_objfile_data->exception_probes.empty ())
3558 return false;
45461e0d 3559
1940319c 3560 gdbarch = objfile->arch ();
28106bc2 3561
1940319c
TV
3562 for (probe *p : bp_objfile_data->exception_probes)
3563 {
3564 b = create_internal_breakpoint (gdbarch,
3565 p->get_relocated_address (objfile),
3566 bp_exception_master,
3567 &internal_breakpoint_ops);
3568 b->location = new_probe_location ("-probe-stap libgcc:unwind");
3569 b->enable_state = bp_disabled;
3570 }
28106bc2 3571
1940319c
TV
3572 return true;
3573}
28106bc2 3574
1940319c
TV
3575/* Install a master breakpoint on the unwinder's debug hook for OBJFILE using
3576 _Unwind_DebugHook. Return true if a breakpoint was installed. */
17450429 3577
1940319c
TV
3578static bool
3579create_exception_master_breakpoint_hook (objfile *objfile)
3580{
3581 const char *const func_name = "_Unwind_DebugHook";
3582 struct breakpoint *b;
3583 struct gdbarch *gdbarch;
3584 struct breakpoint_objfile_data *bp_objfile_data;
3585 CORE_ADDR addr;
3586 struct explicit_location explicit_loc;
186c406b 3587
1940319c 3588 bp_objfile_data = get_breakpoint_objfile_data (objfile);
186c406b 3589
1940319c
TV
3590 if (msym_not_found_p (bp_objfile_data->exception_msym.minsym))
3591 return false;
17450429 3592
1940319c
TV
3593 gdbarch = objfile->arch ();
3594
3595 if (bp_objfile_data->exception_msym.minsym == NULL)
3596 {
3597 struct bound_minimal_symbol debug_hook;
3598
3599 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
3600 if (debug_hook.minsym == NULL)
3601 {
3602 bp_objfile_data->exception_msym.minsym = &msym_not_found;
3603 return false;
186c406b 3604 }
17450429 3605
1940319c
TV
3606 bp_objfile_data->exception_msym = debug_hook;
3607 }
3608
3609 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->exception_msym);
328d42d8
SM
3610 addr = gdbarch_convert_from_func_ptr_addr
3611 (gdbarch, addr, current_inferior ()->top_target ());
1940319c
TV
3612 b = create_internal_breakpoint (gdbarch, addr, bp_exception_master,
3613 &internal_breakpoint_ops);
3614 initialize_explicit_location (&explicit_loc);
3615 explicit_loc.function_name = ASTRDUP (func_name);
3616 b->location = new_explicit_location (&explicit_loc);
3617 b->enable_state = bp_disabled;
3618
3619 return true;
3620}
3621
3622/* Install a master breakpoint on the unwinder's debug hook. */
3623
3624static void
3625create_exception_master_breakpoint (void)
3626{
3627 for (objfile *obj : current_program_space->objfiles ())
3628 {
3629 /* Skip separate debug object. */
3630 if (obj->separate_debug_objfile_backlink)
3631 continue;
3632
3633 /* Try a probe kind breakpoint. */
3634 if (create_exception_master_breakpoint_probe (obj))
3635 continue;
3636
7c6944ab
TV
3637 /* Iterate over main and separate debug objects and try an
3638 _Unwind_DebugHook kind breakpoint. */
3639 for (objfile *debug_objfile : obj->separate_debug_objfiles ())
3640 if (create_exception_master_breakpoint_hook (debug_objfile))
1940319c 3641 break;
186c406b 3642 }
186c406b
TT
3643}
3644
9ef9e6a6
KS
3645/* Does B have a location spec? */
3646
3647static int
3648breakpoint_event_location_empty_p (const struct breakpoint *b)
3649{
d28cd78a 3650 return b->location != NULL && event_location_empty_p (b->location.get ());
9ef9e6a6
KS
3651}
3652
c906108c 3653void
fba45db2 3654update_breakpoints_after_exec (void)
c906108c 3655{
35df4500 3656 struct breakpoint *b, *b_tmp;
876fa593 3657 struct bp_location *bploc, **bplocp_tmp;
c906108c 3658
25b22b0a
PA
3659 /* We're about to delete breakpoints from GDB's lists. If the
3660 INSERTED flag is true, GDB will try to lift the breakpoints by
3661 writing the breakpoints' "shadow contents" back into memory. The
3662 "shadow contents" are NOT valid after an exec, so GDB should not
3663 do that. Instead, the target is responsible from marking
3664 breakpoints out as soon as it detects an exec. We don't do that
3665 here instead, because there may be other attempts to delete
3666 breakpoints after detecting an exec and before reaching here. */
876fa593 3667 ALL_BP_LOCATIONS (bploc, bplocp_tmp)
6c95b8df
PA
3668 if (bploc->pspace == current_program_space)
3669 gdb_assert (!bploc->inserted);
c906108c 3670
35df4500 3671 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 3672 {
6c95b8df
PA
3673 if (b->pspace != current_program_space)
3674 continue;
3675
4a64f543 3676 /* Solib breakpoints must be explicitly reset after an exec(). */
c5aa993b
JM
3677 if (b->type == bp_shlib_event)
3678 {
3679 delete_breakpoint (b);
3680 continue;
3681 }
c906108c 3682
4a64f543 3683 /* JIT breakpoints must be explicitly reset after an exec(). */
4efc6507
DE
3684 if (b->type == bp_jit_event)
3685 {
3686 delete_breakpoint (b);
3687 continue;
3688 }
3689
1900040c 3690 /* Thread event breakpoints must be set anew after an exec(),
0fd8e87f
UW
3691 as must overlay event and longjmp master breakpoints. */
3692 if (b->type == bp_thread_event || b->type == bp_overlay_event
186c406b
TT
3693 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
3694 || b->type == bp_exception_master)
c4093a6a
JM
3695 {
3696 delete_breakpoint (b);
3697 continue;
3698 }
3699
4a64f543 3700 /* Step-resume breakpoints are meaningless after an exec(). */
2c03e5be 3701 if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
c5aa993b
JM
3702 {
3703 delete_breakpoint (b);
3704 continue;
3705 }
3706
7c16b83e
PA
3707 /* Just like single-step breakpoints. */
3708 if (b->type == bp_single_step)
3709 {
3710 delete_breakpoint (b);
3711 continue;
3712 }
3713
611c83ae
PA
3714 /* Longjmp and longjmp-resume breakpoints are also meaningless
3715 after an exec. */
186c406b 3716 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
e2e4d78b 3717 || b->type == bp_longjmp_call_dummy
186c406b 3718 || b->type == bp_exception || b->type == bp_exception_resume)
611c83ae
PA
3719 {
3720 delete_breakpoint (b);
3721 continue;
3722 }
3723
ce78b96d
JB
3724 if (b->type == bp_catchpoint)
3725 {
dda83cd7
SM
3726 /* For now, none of the bp_catchpoint breakpoints need to
3727 do anything at this point. In the future, if some of
3728 the catchpoints need to something, we will need to add
3729 a new method, and call this method from here. */
3730 continue;
ce78b96d
JB
3731 }
3732
c5aa993b
JM
3733 /* bp_finish is a special case. The only way we ought to be able
3734 to see one of these when an exec() has happened, is if the user
3735 caught a vfork, and then said "finish". Ordinarily a finish just
3736 carries them to the call-site of the current callee, by setting
3737 a temporary bp there and resuming. But in this case, the finish
3738 will carry them entirely through the vfork & exec.
3739
3740 We don't want to allow a bp_finish to remain inserted now. But
3741 we can't safely delete it, 'cause finish_command has a handle to
3742 the bp on a bpstat, and will later want to delete it. There's a
3743 chance (and I've seen it happen) that if we delete the bp_finish
3744 here, that its storage will get reused by the time finish_command
3745 gets 'round to deleting the "use to be a bp_finish" breakpoint.
3746 We really must allow finish_command to delete a bp_finish.
3747
e5dd4106 3748 In the absence of a general solution for the "how do we know
53a5351d
JM
3749 it's safe to delete something others may have handles to?"
3750 problem, what we'll do here is just uninsert the bp_finish, and
3751 let finish_command delete it.
3752
3753 (We know the bp_finish is "doomed" in the sense that it's
3754 momentary, and will be deleted as soon as finish_command sees
3755 the inferior stopped. So it doesn't matter that the bp's
3756 address is probably bogus in the new a.out, unlike e.g., the
3757 solib breakpoints.) */
c5aa993b 3758
c5aa993b
JM
3759 if (b->type == bp_finish)
3760 {
3761 continue;
3762 }
3763
3764 /* Without a symbolic address, we have little hope of the
3765 pre-exec() address meaning the same thing in the post-exec()
4a64f543 3766 a.out. */
9ef9e6a6 3767 if (breakpoint_event_location_empty_p (b))
c5aa993b
JM
3768 {
3769 delete_breakpoint (b);
3770 continue;
3771 }
c5aa993b 3772 }
c906108c
SS
3773}
3774
3775int
d80ee84f 3776detach_breakpoints (ptid_t ptid)
c906108c 3777{
35df4500 3778 struct bp_location *bl, **blp_tmp;
3a1bae8e 3779 int val = 0;
2989a365 3780 scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid);
6c95b8df 3781 struct inferior *inf = current_inferior ();
c5aa993b 3782
e99b03dc 3783 if (ptid.pid () == inferior_ptid.pid ())
8a3fe4f8 3784 error (_("Cannot detach breakpoints of inferior_ptid"));
c5aa993b 3785
6c95b8df 3786 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
d80ee84f 3787 inferior_ptid = ptid;
35df4500 3788 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3789 {
35df4500 3790 if (bl->pspace != inf->pspace)
6c95b8df
PA
3791 continue;
3792
bd9673a4
PW
3793 /* This function must physically remove breakpoints locations
3794 from the specified ptid, without modifying the breakpoint
3795 package's state. Locations of type bp_loc_other are only
3796 maintained at GDB side. So, there is no need to remove
3797 these bp_loc_other locations. Moreover, removing these
3798 would modify the breakpoint package's state. */
3799 if (bl->loc_type == bp_loc_other)
3800 continue;
3801
35df4500 3802 if (bl->inserted)
b2b6a7da 3803 val |= remove_breakpoint_1 (bl, DETACH_BREAKPOINT);
c5aa993b 3804 }
d03285ec 3805
3a1bae8e 3806 return val;
c906108c
SS
3807}
3808
35df4500 3809/* Remove the breakpoint location BL from the current address space.
6c95b8df
PA
3810 Note that this is used to detach breakpoints from a child fork.
3811 When we get here, the child isn't in the inferior list, and neither
3812 do we have objects to represent its address space --- we should
35df4500 3813 *not* look at bl->pspace->aspace here. */
6c95b8df 3814
c906108c 3815static int
b2b6a7da 3816remove_breakpoint_1 (struct bp_location *bl, enum remove_bp_reason reason)
c906108c
SS
3817{
3818 int val;
c5aa993b 3819
35df4500
TJB
3820 /* BL is never in moribund_locations by our callers. */
3821 gdb_assert (bl->owner != NULL);
2bdf28a0 3822
74960c60
VP
3823 /* The type of none suggests that owner is actually deleted.
3824 This should not ever happen. */
35df4500 3825 gdb_assert (bl->owner->type != bp_none);
0bde7532 3826
35df4500
TJB
3827 if (bl->loc_type == bp_loc_software_breakpoint
3828 || bl->loc_type == bp_loc_hardware_breakpoint)
c906108c 3829 {
c02f5703
MS
3830 /* "Normal" instruction breakpoint: either the standard
3831 trap-instruction bp (bp_breakpoint), or a
3832 bp_hardware_breakpoint. */
3833
3834 /* First check to see if we have to handle an overlay. */
3835 if (overlay_debugging == ovly_off
35df4500
TJB
3836 || bl->section == NULL
3837 || !(section_is_overlay (bl->section)))
c02f5703
MS
3838 {
3839 /* No overlay handling: just remove the breakpoint. */
08351840
PA
3840
3841 /* If we're trying to uninsert a memory breakpoint that we
3842 know is set in a dynamic object that is marked
3843 shlib_disabled, then either the dynamic object was
3844 removed with "remove-symbol-file" or with
3845 "nosharedlibrary". In the former case, we don't know
3846 whether another dynamic object might have loaded over the
3847 breakpoint's address -- the user might well let us know
3848 about it next with add-symbol-file (the whole point of
d03de421 3849 add-symbol-file is letting the user manually maintain a
08351840
PA
3850 list of dynamically loaded objects). If we have the
3851 breakpoint's shadow memory, that is, this is a software
3852 breakpoint managed by GDB, check whether the breakpoint
3853 is still inserted in memory, to avoid overwriting wrong
3854 code with stale saved shadow contents. Note that HW
3855 breakpoints don't have shadow memory, as they're
3856 implemented using a mechanism that is not dependent on
3857 being able to modify the target's memory, and as such
3858 they should always be removed. */
3859 if (bl->shlib_disabled
3860 && bl->target_info.shadow_len != 0
3861 && !memory_validate_breakpoint (bl->gdbarch, &bl->target_info))
3862 val = 0;
3863 else
73971819 3864 val = bl->owner->ops->remove_location (bl, reason);
c02f5703 3865 }
c906108c
SS
3866 else
3867 {
4a64f543 3868 /* This breakpoint is in an overlay section.
c02f5703
MS
3869 Did we set a breakpoint at the LMA? */
3870 if (!overlay_events_enabled)
3871 {
3872 /* Yes -- overlay event support is not active, so we
3873 should have set a breakpoint at the LMA. Remove it.
3874 */
c02f5703
MS
3875 /* Ignore any failures: if the LMA is in ROM, we will
3876 have already warned when we failed to insert it. */
35df4500
TJB
3877 if (bl->loc_type == bp_loc_hardware_breakpoint)
3878 target_remove_hw_breakpoint (bl->gdbarch,
3879 &bl->overlay_target_info);
c02f5703 3880 else
35df4500 3881 target_remove_breakpoint (bl->gdbarch,
73971819
PA
3882 &bl->overlay_target_info,
3883 reason);
c02f5703
MS
3884 }
3885 /* Did we set a breakpoint at the VMA?
3886 If so, we will have marked the breakpoint 'inserted'. */
35df4500 3887 if (bl->inserted)
c906108c 3888 {
c02f5703
MS
3889 /* Yes -- remove it. Previously we did not bother to
3890 remove the breakpoint if the section had been
3891 unmapped, but let's not rely on that being safe. We
3892 don't know what the overlay manager might do. */
aa67235e
UW
3893
3894 /* However, we should remove *software* breakpoints only
3895 if the section is still mapped, or else we overwrite
3896 wrong code with the saved shadow contents. */
348d480f
PA
3897 if (bl->loc_type == bp_loc_hardware_breakpoint
3898 || section_is_mapped (bl->section))
73971819 3899 val = bl->owner->ops->remove_location (bl, reason);
aa67235e
UW
3900 else
3901 val = 0;
c906108c 3902 }
c02f5703
MS
3903 else
3904 {
3905 /* No -- not inserted, so no need to remove. No error. */
3906 val = 0;
3907 }
c906108c 3908 }
879d1e6b 3909
08351840
PA
3910 /* In some cases, we might not be able to remove a breakpoint in
3911 a shared library that has already been removed, but we have
3912 not yet processed the shlib unload event. Similarly for an
3913 unloaded add-symbol-file object - the user might not yet have
3914 had the chance to remove-symbol-file it. shlib_disabled will
3915 be set if the library/object has already been removed, but
3916 the breakpoint hasn't been uninserted yet, e.g., after
3917 "nosharedlibrary" or "remove-symbol-file" with breakpoints
3918 always-inserted mode. */
076855f9 3919 if (val
08351840
PA
3920 && (bl->loc_type == bp_loc_software_breakpoint
3921 && (bl->shlib_disabled
3922 || solib_name_from_address (bl->pspace, bl->address)
d03de421
PA
3923 || shared_objfile_contains_address_p (bl->pspace,
3924 bl->address))))
879d1e6b
UW
3925 val = 0;
3926
c906108c
SS
3927 if (val)
3928 return val;
b2b6a7da 3929 bl->inserted = (reason == DETACH_BREAKPOINT);
c906108c 3930 }
35df4500 3931 else if (bl->loc_type == bp_loc_hardware_watchpoint)
c906108c 3932 {
77b06cd7
TJB
3933 gdb_assert (bl->owner->ops != NULL
3934 && bl->owner->ops->remove_location != NULL);
3935
b2b6a7da 3936 bl->inserted = (reason == DETACH_BREAKPOINT);
73971819 3937 bl->owner->ops->remove_location (bl, reason);
2e70b7b9 3938
c906108c 3939 /* Failure to remove any of the hardware watchpoints comes here. */
b2b6a7da 3940 if (reason == REMOVE_BREAKPOINT && bl->inserted)
8a3fe4f8 3941 warning (_("Could not remove hardware watchpoint %d."),
35df4500 3942 bl->owner->number);
c906108c 3943 }
35df4500 3944 else if (bl->owner->type == bp_catchpoint
dda83cd7
SM
3945 && breakpoint_enabled (bl->owner)
3946 && !bl->duplicate)
ce78b96d 3947 {
77b06cd7
TJB
3948 gdb_assert (bl->owner->ops != NULL
3949 && bl->owner->ops->remove_location != NULL);
ce78b96d 3950
73971819 3951 val = bl->owner->ops->remove_location (bl, reason);
ce78b96d
JB
3952 if (val)
3953 return val;
77b06cd7 3954
b2b6a7da 3955 bl->inserted = (reason == DETACH_BREAKPOINT);
ce78b96d 3956 }
c906108c
SS
3957
3958 return 0;
3959}
3960
6c95b8df 3961static int
834c0d03 3962remove_breakpoint (struct bp_location *bl)
6c95b8df 3963{
35df4500
TJB
3964 /* BL is never in moribund_locations by our callers. */
3965 gdb_assert (bl->owner != NULL);
2bdf28a0 3966
6c95b8df
PA
3967 /* The type of none suggests that owner is actually deleted.
3968 This should not ever happen. */
35df4500 3969 gdb_assert (bl->owner->type != bp_none);
6c95b8df 3970
5ed8105e 3971 scoped_restore_current_pspace_and_thread restore_pspace_thread;
6c95b8df 3972
35df4500 3973 switch_to_program_space_and_thread (bl->pspace);
6c95b8df 3974
5ed8105e 3975 return remove_breakpoint_1 (bl, REMOVE_BREAKPOINT);
6c95b8df
PA
3976}
3977
c906108c
SS
3978/* Clear the "inserted" flag in all breakpoints. */
3979
25b22b0a 3980void
fba45db2 3981mark_breakpoints_out (void)
c906108c 3982{
35df4500 3983 struct bp_location *bl, **blp_tmp;
c906108c 3984
35df4500 3985 ALL_BP_LOCATIONS (bl, blp_tmp)
66c4b3e8 3986 if (bl->pspace == current_program_space)
35df4500 3987 bl->inserted = 0;
c906108c
SS
3988}
3989
53a5351d
JM
3990/* Clear the "inserted" flag in all breakpoints and delete any
3991 breakpoints which should go away between runs of the program.
c906108c
SS
3992
3993 Plus other such housekeeping that has to be done for breakpoints
3994 between runs.
3995
53a5351d
JM
3996 Note: this function gets called at the end of a run (by
3997 generic_mourn_inferior) and when a run begins (by
4a64f543 3998 init_wait_for_inferior). */
c906108c
SS
3999
4000
4001
4002void
fba45db2 4003breakpoint_init_inferior (enum inf_context context)
c906108c 4004{
35df4500 4005 struct breakpoint *b, *b_tmp;
6c95b8df 4006 struct program_space *pspace = current_program_space;
c906108c 4007
50c71eaf
PA
4008 /* If breakpoint locations are shared across processes, then there's
4009 nothing to do. */
f5656ead 4010 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
50c71eaf
PA
4011 return;
4012
1a853c52 4013 mark_breakpoints_out ();
075f6582 4014
35df4500 4015 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 4016 {
6c95b8df
PA
4017 if (b->loc && b->loc->pspace != pspace)
4018 continue;
4019
c5aa993b
JM
4020 switch (b->type)
4021 {
4022 case bp_call_dummy:
e2e4d78b 4023 case bp_longjmp_call_dummy:
c906108c 4024
c5aa993b 4025 /* If the call dummy breakpoint is at the entry point it will
ab92d69b
PA
4026 cause problems when the inferior is rerun, so we better get
4027 rid of it. */
4028
4029 case bp_watchpoint_scope:
4030
4031 /* Also get rid of scope breakpoints. */
4032
4033 case bp_shlib_event:
4034
4035 /* Also remove solib event breakpoints. Their addresses may
4036 have changed since the last time we ran the program.
4037 Actually we may now be debugging against different target;
4038 and so the solib backend that installed this breakpoint may
4039 not be used in by the target. E.g.,
4040
4041 (gdb) file prog-linux
4042 (gdb) run # native linux target
4043 ...
4044 (gdb) kill
4045 (gdb) file prog-win.exe
4046 (gdb) tar rem :9999 # remote Windows gdbserver.
4047 */
c906108c 4048
f59f708a
PA
4049 case bp_step_resume:
4050
4051 /* Also remove step-resume breakpoints. */
4052
7c16b83e
PA
4053 case bp_single_step:
4054
4055 /* Also remove single-step breakpoints. */
4056
c5aa993b
JM
4057 delete_breakpoint (b);
4058 break;
c906108c 4059
c5aa993b
JM
4060 case bp_watchpoint:
4061 case bp_hardware_watchpoint:
4062 case bp_read_watchpoint:
4063 case bp_access_watchpoint:
3a5c3e22
PA
4064 {
4065 struct watchpoint *w = (struct watchpoint *) b;
c906108c 4066
3a5c3e22
PA
4067 /* Likewise for watchpoints on local expressions. */
4068 if (w->exp_valid_block != NULL)
4069 delete_breakpoint (b);
63000888 4070 else
3a5c3e22 4071 {
63000888
PA
4072 /* Get rid of existing locations, which are no longer
4073 valid. New ones will be created in
4074 update_watchpoint, when the inferior is restarted.
4075 The next update_global_location_list call will
4076 garbage collect them. */
4077 b->loc = NULL;
4078
4079 if (context == inf_starting)
4080 {
4081 /* Reset val field to force reread of starting value in
4082 insert_breakpoints. */
850645cf 4083 w->val.reset (nullptr);
4c1d86d9 4084 w->val_valid = false;
63000888
PA
4085 }
4086 }
3a5c3e22 4087 }
c5aa993b
JM
4088 break;
4089 default:
c5aa993b
JM
4090 break;
4091 }
4092 }
1c5cfe86
PA
4093
4094 /* Get rid of the moribund locations. */
1123588c 4095 for (bp_location *bl : moribund_locations)
35df4500 4096 decref_bp_location (&bl);
1123588c 4097 moribund_locations.clear ();
c906108c
SS
4098}
4099
6c95b8df
PA
4100/* These functions concern about actual breakpoints inserted in the
4101 target --- to e.g. check if we need to do decr_pc adjustment or if
4102 we need to hop over the bkpt --- so we check for address space
4103 match, not program space. */
4104
c2c6d25f
JM
4105/* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
4106 exists at PC. It returns ordinary_breakpoint_here if it's an
4107 ordinary breakpoint, or permanent_breakpoint_here if it's a
4108 permanent breakpoint.
4109 - When continuing from a location with an ordinary breakpoint, we
4110 actually single step once before calling insert_breakpoints.
e5dd4106 4111 - When continuing from a location with a permanent breakpoint, we
c2c6d25f
JM
4112 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
4113 the target, to advance the PC past the breakpoint. */
c906108c 4114
c2c6d25f 4115enum breakpoint_here
accd0bcd 4116breakpoint_here_p (const address_space *aspace, CORE_ADDR pc)
c906108c 4117{
35df4500 4118 struct bp_location *bl, **blp_tmp;
c2c6d25f 4119 int any_breakpoint_here = 0;
c906108c 4120
35df4500 4121 ALL_BP_LOCATIONS (bl, blp_tmp)
075f6582 4122 {
35df4500
TJB
4123 if (bl->loc_type != bp_loc_software_breakpoint
4124 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
4125 continue;
4126
f1310107 4127 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
35df4500 4128 if ((breakpoint_enabled (bl->owner)
1a853c52 4129 || bl->permanent)
f1310107 4130 && breakpoint_location_address_match (bl, aspace, pc))
075f6582
DJ
4131 {
4132 if (overlay_debugging
35df4500
TJB
4133 && section_is_overlay (bl->section)
4134 && !section_is_mapped (bl->section))
075f6582 4135 continue; /* unmapped overlay -- can't be a match */
1a853c52 4136 else if (bl->permanent)
075f6582
DJ
4137 return permanent_breakpoint_here;
4138 else
4139 any_breakpoint_here = 1;
4140 }
4141 }
c906108c 4142
f486487f 4143 return any_breakpoint_here ? ordinary_breakpoint_here : no_breakpoint_here;
c906108c
SS
4144}
4145
d35ae833
PA
4146/* See breakpoint.h. */
4147
4148int
accd0bcd 4149breakpoint_in_range_p (const address_space *aspace,
d35ae833
PA
4150 CORE_ADDR addr, ULONGEST len)
4151{
4152 struct bp_location *bl, **blp_tmp;
4153
4154 ALL_BP_LOCATIONS (bl, blp_tmp)
4155 {
4156 if (bl->loc_type != bp_loc_software_breakpoint
4157 && bl->loc_type != bp_loc_hardware_breakpoint)
4158 continue;
4159
4160 if ((breakpoint_enabled (bl->owner)
4161 || bl->permanent)
4162 && breakpoint_location_address_range_overlap (bl, aspace,
4163 addr, len))
4164 {
4165 if (overlay_debugging
4166 && section_is_overlay (bl->section)
4167 && !section_is_mapped (bl->section))
4168 {
4169 /* Unmapped overlay -- can't be a match. */
4170 continue;
4171 }
4172
4173 return 1;
4174 }
4175 }
4176
4177 return 0;
4178}
4179
1c5cfe86
PA
4180/* Return true if there's a moribund breakpoint at PC. */
4181
4182int
accd0bcd 4183moribund_breakpoint_here_p (const address_space *aspace, CORE_ADDR pc)
1c5cfe86 4184{
1123588c 4185 for (bp_location *loc : moribund_locations)
f1310107 4186 if (breakpoint_location_address_match (loc, aspace, pc))
1c5cfe86
PA
4187 return 1;
4188
4189 return 0;
4190}
c2c6d25f 4191
f7ce857f
PA
4192/* Returns non-zero iff BL is inserted at PC, in address space
4193 ASPACE. */
4194
4195static int
4196bp_location_inserted_here_p (struct bp_location *bl,
accd0bcd 4197 const address_space *aspace, CORE_ADDR pc)
f7ce857f
PA
4198{
4199 if (bl->inserted
4200 && breakpoint_address_match (bl->pspace->aspace, bl->address,
4201 aspace, pc))
4202 {
4203 if (overlay_debugging
4204 && section_is_overlay (bl->section)
4205 && !section_is_mapped (bl->section))
4206 return 0; /* unmapped overlay -- can't be a match */
4207 else
4208 return 1;
4209 }
4210 return 0;
4211}
4212
a1fd2fa5 4213/* Returns non-zero iff there's a breakpoint inserted at PC. */
c906108c
SS
4214
4215int
accd0bcd 4216breakpoint_inserted_here_p (const address_space *aspace, CORE_ADDR pc)
c906108c 4217{
f7ce857f 4218 struct bp_location **blp, **blp_tmp = NULL;
c906108c 4219
f7ce857f 4220 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
c5aa993b 4221 {
f7ce857f
PA
4222 struct bp_location *bl = *blp;
4223
35df4500
TJB
4224 if (bl->loc_type != bp_loc_software_breakpoint
4225 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
4226 continue;
4227
f7ce857f
PA
4228 if (bp_location_inserted_here_p (bl, aspace, pc))
4229 return 1;
c5aa993b 4230 }
c36b740a
VP
4231 return 0;
4232}
4233
a1fd2fa5
PA
4234/* This function returns non-zero iff there is a software breakpoint
4235 inserted at PC. */
c36b740a
VP
4236
4237int
accd0bcd 4238software_breakpoint_inserted_here_p (const address_space *aspace,
a1fd2fa5 4239 CORE_ADDR pc)
4fa8626c 4240{
f7ce857f 4241 struct bp_location **blp, **blp_tmp = NULL;
4fa8626c 4242
f7ce857f 4243 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4fa8626c 4244 {
f7ce857f
PA
4245 struct bp_location *bl = *blp;
4246
35df4500 4247 if (bl->loc_type != bp_loc_software_breakpoint)
4fa8626c
DJ
4248 continue;
4249
f7ce857f
PA
4250 if (bp_location_inserted_here_p (bl, aspace, pc))
4251 return 1;
4fa8626c
DJ
4252 }
4253
4254 return 0;
9c02b525
PA
4255}
4256
4257/* See breakpoint.h. */
4258
4259int
accd0bcd 4260hardware_breakpoint_inserted_here_p (const address_space *aspace,
9c02b525
PA
4261 CORE_ADDR pc)
4262{
4263 struct bp_location **blp, **blp_tmp = NULL;
9c02b525
PA
4264
4265 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4266 {
4267 struct bp_location *bl = *blp;
4268
4269 if (bl->loc_type != bp_loc_hardware_breakpoint)
4270 continue;
4271
4272 if (bp_location_inserted_here_p (bl, aspace, pc))
4273 return 1;
4274 }
4275
4276 return 0;
4fa8626c
DJ
4277}
4278
9093389c 4279int
accd0bcd 4280hardware_watchpoint_inserted_in_range (const address_space *aspace,
9093389c
PA
4281 CORE_ADDR addr, ULONGEST len)
4282{
4283 struct breakpoint *bpt;
4284
4285 ALL_BREAKPOINTS (bpt)
4286 {
4287 struct bp_location *loc;
4288
4289 if (bpt->type != bp_hardware_watchpoint
4290 && bpt->type != bp_access_watchpoint)
4291 continue;
4292
4293 if (!breakpoint_enabled (bpt))
4294 continue;
4295
4296 for (loc = bpt->loc; loc; loc = loc->next)
4297 if (loc->pspace->aspace == aspace && loc->inserted)
4298 {
4299 CORE_ADDR l, h;
4300
4301 /* Check for intersection. */
768adc05
PA
4302 l = std::max<CORE_ADDR> (loc->address, addr);
4303 h = std::min<CORE_ADDR> (loc->address + loc->length, addr + len);
9093389c
PA
4304 if (l < h)
4305 return 1;
4306 }
4307 }
4308 return 0;
4309}
c5aa993b 4310
f2478a7e 4311/* See breakpoint.h. */
c906108c 4312
f2478a7e
SM
4313bool
4314is_catchpoint (struct breakpoint *b)
c906108c 4315{
f2478a7e 4316 return (b->type == bp_catchpoint);
c906108c
SS
4317}
4318
c906108c
SS
4319/* Clear a bpstat so that it says we are not at any breakpoint.
4320 Also free any storage that is part of a bpstat. */
4321
4322void
fba45db2 4323bpstat_clear (bpstat *bsp)
c906108c
SS
4324{
4325 bpstat p;
4326 bpstat q;
4327
4328 if (bsp == 0)
4329 return;
4330 p = *bsp;
4331 while (p != NULL)
4332 {
4333 q = p->next;
04afa70c 4334 delete p;
c906108c
SS
4335 p = q;
4336 }
4337 *bsp = NULL;
4338}
4339
04afa70c
TT
4340bpstats::bpstats (const bpstats &other)
4341 : next (NULL),
4342 bp_location_at (other.bp_location_at),
4343 breakpoint_at (other.breakpoint_at),
4344 commands (other.commands),
04afa70c
TT
4345 print (other.print),
4346 stop (other.stop),
4347 print_it (other.print_it)
4348{
850645cf
TT
4349 if (other.old_val != NULL)
4350 old_val = release_value (value_copy (other.old_val.get ()));
04afa70c
TT
4351}
4352
c906108c
SS
4353/* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
4354 is part of the bpstat is copied as well. */
4355
4356bpstat
fba45db2 4357bpstat_copy (bpstat bs)
c906108c
SS
4358{
4359 bpstat p = NULL;
4360 bpstat tmp;
4361 bpstat retval = NULL;
4362
4363 if (bs == NULL)
4364 return bs;
4365
4366 for (; bs != NULL; bs = bs->next)
4367 {
04afa70c 4368 tmp = new bpstats (*bs);
31cc81e9 4369
c906108c
SS
4370 if (p == NULL)
4371 /* This is the first thing in the chain. */
4372 retval = tmp;
4373 else
4374 p->next = tmp;
4375 p = tmp;
4376 }
4377 p->next = NULL;
4378 return retval;
4379}
4380
4a64f543 4381/* Find the bpstat associated with this breakpoint. */
c906108c
SS
4382
4383bpstat
fba45db2 4384bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
c906108c 4385{
c5aa993b
JM
4386 if (bsp == NULL)
4387 return NULL;
c906108c 4388
c5aa993b
JM
4389 for (; bsp != NULL; bsp = bsp->next)
4390 {
f431efe5 4391 if (bsp->breakpoint_at == breakpoint)
c5aa993b
JM
4392 return bsp;
4393 }
c906108c
SS
4394 return NULL;
4395}
4396
ab04a2af
TT
4397/* See breakpoint.h. */
4398
4c462cb0 4399bool
427cd150 4400bpstat_explains_signal (bpstat bsp, enum gdb_signal sig)
ab04a2af 4401{
ab04a2af
TT
4402 for (; bsp != NULL; bsp = bsp->next)
4403 {
427cd150
TT
4404 if (bsp->breakpoint_at == NULL)
4405 {
4406 /* A moribund location can never explain a signal other than
4407 GDB_SIGNAL_TRAP. */
4408 if (sig == GDB_SIGNAL_TRAP)
4c462cb0 4409 return true;
427cd150
TT
4410 }
4411 else
47591c29
PA
4412 {
4413 if (bsp->breakpoint_at->ops->explains_signal (bsp->breakpoint_at,
4414 sig))
4c462cb0 4415 return true;
47591c29 4416 }
ab04a2af
TT
4417 }
4418
4c462cb0 4419 return false;
ab04a2af
TT
4420}
4421
4a64f543
MS
4422/* Put in *NUM the breakpoint number of the first breakpoint we are
4423 stopped at. *BSP upon return is a bpstat which points to the
4424 remaining breakpoints stopped at (but which is not guaranteed to be
4425 good for anything but further calls to bpstat_num).
4426
8671a17b
PA
4427 Return 0 if passed a bpstat which does not indicate any breakpoints.
4428 Return -1 if stopped at a breakpoint that has been deleted since
4429 we set it.
4430 Return 1 otherwise. */
c906108c
SS
4431
4432int
8671a17b 4433bpstat_num (bpstat *bsp, int *num)
c906108c
SS
4434{
4435 struct breakpoint *b;
4436
4437 if ((*bsp) == NULL)
4438 return 0; /* No more breakpoint values */
8671a17b 4439
4a64f543
MS
4440 /* We assume we'll never have several bpstats that correspond to a
4441 single breakpoint -- otherwise, this function might return the
4442 same number more than once and this will look ugly. */
f431efe5 4443 b = (*bsp)->breakpoint_at;
8671a17b
PA
4444 *bsp = (*bsp)->next;
4445 if (b == NULL)
4446 return -1; /* breakpoint that's been deleted since */
4447
4448 *num = b->number; /* We have its number */
4449 return 1;
c906108c
SS
4450}
4451
e93ca019 4452/* See breakpoint.h. */
c906108c
SS
4453
4454void
e93ca019 4455bpstat_clear_actions (void)
c906108c 4456{
e93ca019
JK
4457 bpstat bs;
4458
00431a78 4459 if (inferior_ptid == null_ptid)
e93ca019
JK
4460 return;
4461
00431a78 4462 thread_info *tp = inferior_thread ();
e93ca019 4463 for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
c906108c 4464 {
d1b0a7bf 4465 bs->commands = NULL;
850645cf 4466 bs->old_val.reset (nullptr);
c906108c
SS
4467 }
4468}
4469
f3b1572e
PA
4470/* Called when a command is about to proceed the inferior. */
4471
4472static void
4473breakpoint_about_to_proceed (void)
4474{
d7e15655 4475 if (inferior_ptid != null_ptid)
f3b1572e
PA
4476 {
4477 struct thread_info *tp = inferior_thread ();
4478
4479 /* Allow inferior function calls in breakpoint commands to not
4480 interrupt the command list. When the call finishes
4481 successfully, the inferior will be standing at the same
4482 breakpoint as if nothing happened. */
16c381f0 4483 if (tp->control.in_infcall)
f3b1572e
PA
4484 return;
4485 }
4486
4487 breakpoint_proceeded = 1;
4488}
4489
abf85f46
JK
4490/* Return non-zero iff CMD as the first line of a command sequence is `silent'
4491 or its equivalent. */
4492
4493static int
4494command_line_is_silent (struct command_line *cmd)
4495{
4f45d445 4496 return cmd && (strcmp ("silent", cmd->line) == 0);
abf85f46
JK
4497}
4498
4a64f543
MS
4499/* Execute all the commands associated with all the breakpoints at
4500 this location. Any of these commands could cause the process to
4501 proceed beyond this point, etc. We look out for such changes by
4502 checking the global "breakpoint_proceeded" after each command.
c906108c 4503
347bddb7
PA
4504 Returns true if a breakpoint command resumed the inferior. In that
4505 case, it is the caller's responsibility to recall it again with the
4506 bpstat of the current thread. */
4507
4508static int
4509bpstat_do_actions_1 (bpstat *bsp)
c906108c
SS
4510{
4511 bpstat bs;
347bddb7 4512 int again = 0;
c906108c
SS
4513
4514 /* Avoid endless recursion if a `source' command is contained
4515 in bs->commands. */
4516 if (executing_breakpoint_commands)
347bddb7 4517 return 0;
c906108c 4518
81b1e71c
TT
4519 scoped_restore save_executing
4520 = make_scoped_restore (&executing_breakpoint_commands, 1);
c906108c 4521
1ac32117 4522 scoped_restore preventer = prevent_dont_repeat ();
cf6c5ffb 4523
4a64f543 4524 /* This pointer will iterate over the list of bpstat's. */
c906108c
SS
4525 bs = *bsp;
4526
4527 breakpoint_proceeded = 0;
4528 for (; bs != NULL; bs = bs->next)
4529 {
d1b0a7bf 4530 struct command_line *cmd = NULL;
6c50ab1c
JB
4531
4532 /* Take ownership of the BSP's command tree, if it has one.
4533
dda83cd7
SM
4534 The command tree could legitimately contain commands like
4535 'step' and 'next', which call clear_proceed_status, which
4536 frees stop_bpstat's command tree. To make sure this doesn't
4537 free the tree we're executing out from under us, we need to
4538 take ownership of the tree ourselves. Since a given bpstat's
4539 commands are only executed once, we don't need to copy it; we
4540 can clear the pointer in the bpstat, and make sure we free
4541 the tree when we're done. */
d1b0a7bf 4542 counted_command_line ccmd = bs->commands;
9add0f1b 4543 bs->commands = NULL;
d1b0a7bf
TT
4544 if (ccmd != NULL)
4545 cmd = ccmd.get ();
abf85f46
JK
4546 if (command_line_is_silent (cmd))
4547 {
4548 /* The action has been already done by bpstat_stop_status. */
4549 cmd = cmd->next;
4550 }
6c50ab1c 4551
c906108c
SS
4552 while (cmd != NULL)
4553 {
4554 execute_control_command (cmd);
4555
4556 if (breakpoint_proceeded)
4557 break;
4558 else
4559 cmd = cmd->next;
4560 }
6c50ab1c 4561
c906108c 4562 if (breakpoint_proceeded)
32c1e744 4563 {
cb814510 4564 if (current_ui->async)
347bddb7
PA
4565 /* If we are in async mode, then the target might be still
4566 running, not stopped at any breakpoint, so nothing for
4567 us to do here -- just return to the event loop. */
4568 ;
32c1e744
VP
4569 else
4570 /* In sync mode, when execute_control_command returns
4571 we're already standing on the next breakpoint.
347bddb7
PA
4572 Breakpoint commands for that stop were not run, since
4573 execute_command does not run breakpoint commands --
4574 only command_line_handler does, but that one is not
4575 involved in execution of breakpoint commands. So, we
4576 can now execute breakpoint commands. It should be
4577 noted that making execute_command do bpstat actions is
4578 not an option -- in this case we'll have recursive
4579 invocation of bpstat for each breakpoint with a
4580 command, and can easily blow up GDB stack. Instead, we
4581 return true, which will trigger the caller to recall us
4582 with the new stop_bpstat. */
4583 again = 1;
4584 break;
32c1e744 4585 }
c906108c 4586 }
347bddb7
PA
4587 return again;
4588}
4589
00431a78
PA
4590/* Helper for bpstat_do_actions. Get the current thread, if there's
4591 one, is alive and has execution. Return NULL otherwise. */
4592
4593static thread_info *
4594get_bpstat_thread ()
4595{
55f6301a 4596 if (inferior_ptid == null_ptid || !target_has_execution ())
00431a78
PA
4597 return NULL;
4598
4599 thread_info *tp = inferior_thread ();
4600 if (tp->state == THREAD_EXITED || tp->executing)
4601 return NULL;
4602 return tp;
4603}
4604
347bddb7
PA
4605void
4606bpstat_do_actions (void)
4607{
694c6bf5 4608 auto cleanup_if_error = make_scope_exit (bpstat_clear_actions);
00431a78 4609 thread_info *tp;
353d1d73 4610
347bddb7 4611 /* Do any commands attached to breakpoint we are stopped at. */
00431a78
PA
4612 while ((tp = get_bpstat_thread ()) != NULL)
4613 {
4614 /* Since in sync mode, bpstat_do_actions may resume the
4615 inferior, and only return when it is stopped at the next
4616 breakpoint, we keep doing breakpoint actions until it returns
4617 false to indicate the inferior was not resumed. */
4618 if (!bpstat_do_actions_1 (&tp->control.stop_bpstat))
4619 break;
4620 }
353d1d73 4621
694c6bf5 4622 cleanup_if_error.release ();
c906108c
SS
4623}
4624
fa4727a6
DJ
4625/* Print out the (old or new) value associated with a watchpoint. */
4626
4627static void
4628watchpoint_value_print (struct value *val, struct ui_file *stream)
4629{
4630 if (val == NULL)
7f6aba03 4631 fprintf_styled (stream, metadata_style.style (), _("<unreadable>"));
fa4727a6 4632 else
79a45b7d
TT
4633 {
4634 struct value_print_options opts;
4635 get_user_print_options (&opts);
4636 value_print (val, stream, &opts);
4637 }
fa4727a6
DJ
4638}
4639
f303dbd6
PA
4640/* Print the "Thread ID hit" part of "Thread ID hit Breakpoint N" if
4641 debugging multiple threads. */
4642
4643void
4644maybe_print_thread_hit_breakpoint (struct ui_out *uiout)
4645{
112e8700 4646 if (uiout->is_mi_like_p ())
f303dbd6
PA
4647 return;
4648
112e8700 4649 uiout->text ("\n");
f303dbd6
PA
4650
4651 if (show_thread_that_caused_stop ())
4652 {
4653 const char *name;
4654 struct thread_info *thr = inferior_thread ();
4655
112e8700 4656 uiout->text ("Thread ");
33eca680 4657 uiout->field_string ("thread-id", print_thread_id (thr));
f303dbd6
PA
4658
4659 name = thr->name != NULL ? thr->name : target_thread_name (thr);
4660 if (name != NULL)
4661 {
112e8700 4662 uiout->text (" \"");
33eca680 4663 uiout->field_string ("name", name);
112e8700 4664 uiout->text ("\"");
f303dbd6
PA
4665 }
4666
112e8700 4667 uiout->text (" hit ");
f303dbd6
PA
4668 }
4669}
4670
e514a9d6 4671/* Generic routine for printing messages indicating why we
4a64f543 4672 stopped. The behavior of this function depends on the value
e514a9d6
JM
4673 'print_it' in the bpstat structure. Under some circumstances we
4674 may decide not to print anything here and delegate the task to
4a64f543 4675 normal_stop(). */
e514a9d6
JM
4676
4677static enum print_stop_action
4678print_bp_stop_message (bpstat bs)
4679{
4680 switch (bs->print_it)
4681 {
4682 case print_it_noop:
4a64f543 4683 /* Nothing should be printed for this bpstat entry. */
e514a9d6
JM
4684 return PRINT_UNKNOWN;
4685 break;
4686
4687 case print_it_done:
4688 /* We still want to print the frame, but we already printed the
dda83cd7 4689 relevant messages. */
e514a9d6
JM
4690 return PRINT_SRC_AND_LOC;
4691 break;
4692
4693 case print_it_normal:
4f8d1dc6 4694 {
f431efe5
PA
4695 struct breakpoint *b = bs->breakpoint_at;
4696
1a6a67de
TJB
4697 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
4698 which has since been deleted. */
4699 if (b == NULL)
4700 return PRINT_UNKNOWN;
4701
348d480f
PA
4702 /* Normal case. Call the breakpoint's print_it method. */
4703 return b->ops->print_it (bs);
4f8d1dc6 4704 }
348d480f 4705 break;
3086aeae 4706
e514a9d6 4707 default:
8e65ff28 4708 internal_error (__FILE__, __LINE__,
e2e0b3e5 4709 _("print_bp_stop_message: unrecognized enum value"));
e514a9d6 4710 break;
c906108c 4711 }
c906108c
SS
4712}
4713
edcc5120
TT
4714/* A helper function that prints a shared library stopped event. */
4715
4716static void
4717print_solib_event (int is_catchpoint)
4718{
6fb16ce6 4719 bool any_deleted = !current_program_space->deleted_solibs.empty ();
bcb430e4 4720 bool any_added = !current_program_space->added_solibs.empty ();
edcc5120
TT
4721
4722 if (!is_catchpoint)
4723 {
4724 if (any_added || any_deleted)
112e8700 4725 current_uiout->text (_("Stopped due to shared library event:\n"));
edcc5120 4726 else
112e8700
SM
4727 current_uiout->text (_("Stopped due to shared library event (no "
4728 "libraries added or removed)\n"));
edcc5120
TT
4729 }
4730
112e8700
SM
4731 if (current_uiout->is_mi_like_p ())
4732 current_uiout->field_string ("reason",
4733 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
edcc5120
TT
4734
4735 if (any_deleted)
4736 {
112e8700 4737 current_uiout->text (_(" Inferior unloaded "));
10f489e5 4738 ui_out_emit_list list_emitter (current_uiout, "removed");
6fb16ce6 4739 for (int ix = 0; ix < current_program_space->deleted_solibs.size (); ix++)
edcc5120 4740 {
6fb16ce6
SM
4741 const std::string &name = current_program_space->deleted_solibs[ix];
4742
edcc5120 4743 if (ix > 0)
112e8700
SM
4744 current_uiout->text (" ");
4745 current_uiout->field_string ("library", name);
4746 current_uiout->text ("\n");
edcc5120 4747 }
edcc5120
TT
4748 }
4749
4750 if (any_added)
4751 {
112e8700 4752 current_uiout->text (_(" Inferior loaded "));
10f489e5 4753 ui_out_emit_list list_emitter (current_uiout, "added");
bcb430e4 4754 bool first = true;
52941706 4755 for (so_list *iter : current_program_space->added_solibs)
edcc5120 4756 {
bcb430e4 4757 if (!first)
112e8700 4758 current_uiout->text (" ");
bcb430e4 4759 first = false;
112e8700
SM
4760 current_uiout->field_string ("library", iter->so_name);
4761 current_uiout->text ("\n");
edcc5120 4762 }
edcc5120
TT
4763 }
4764}
4765
e514a9d6
JM
4766/* Print a message indicating what happened. This is called from
4767 normal_stop(). The input to this routine is the head of the bpstat
36dfb11c
TT
4768 list - a list of the eventpoints that caused this stop. KIND is
4769 the target_waitkind for the stopping event. This
e514a9d6
JM
4770 routine calls the generic print routine for printing a message
4771 about reasons for stopping. This will print (for example) the
4772 "Breakpoint n," part of the output. The return value of this
4773 routine is one of:
c906108c 4774
4a64f543 4775 PRINT_UNKNOWN: Means we printed nothing.
917317f4 4776 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4a64f543 4777 code to print the location. An example is
c5aa993b
JM
4778 "Breakpoint 1, " which should be followed by
4779 the location.
917317f4 4780 PRINT_SRC_ONLY: Means we printed something, but there is no need
c5aa993b
JM
4781 to also print the location part of the message.
4782 An example is the catch/throw messages, which
4a64f543 4783 don't require a location appended to the end.
917317f4 4784 PRINT_NOTHING: We have done some printing and we don't need any
4a64f543 4785 further info to be printed. */
c906108c 4786
917317f4 4787enum print_stop_action
36dfb11c 4788bpstat_print (bpstat bs, int kind)
c906108c 4789{
f486487f 4790 enum print_stop_action val;
c5aa993b 4791
c906108c 4792 /* Maybe another breakpoint in the chain caused us to stop.
53a5351d
JM
4793 (Currently all watchpoints go on the bpstat whether hit or not.
4794 That probably could (should) be changed, provided care is taken
c906108c 4795 with respect to bpstat_explains_signal). */
e514a9d6
JM
4796 for (; bs; bs = bs->next)
4797 {
4798 val = print_bp_stop_message (bs);
4799 if (val == PRINT_SRC_ONLY
4800 || val == PRINT_SRC_AND_LOC
4801 || val == PRINT_NOTHING)
4802 return val;
4803 }
c906108c 4804
36dfb11c
TT
4805 /* If we had hit a shared library event breakpoint,
4806 print_bp_stop_message would print out this message. If we hit an
4807 OS-level shared library event, do the same thing. */
4808 if (kind == TARGET_WAITKIND_LOADED)
4809 {
edcc5120 4810 print_solib_event (0);
36dfb11c
TT
4811 return PRINT_NOTHING;
4812 }
4813
e514a9d6 4814 /* We reached the end of the chain, or we got a null BS to start
4a64f543 4815 with and nothing was printed. */
917317f4 4816 return PRINT_UNKNOWN;
c906108c
SS
4817}
4818
bf469271 4819/* Evaluate the boolean expression EXP and return the result. */
c906108c 4820
bf469271
PA
4821static bool
4822breakpoint_cond_eval (expression *exp)
c906108c 4823{
278cd55f 4824 struct value *mark = value_mark ();
bf469271 4825 bool res = value_true (evaluate_expression (exp));
cc59ec59 4826
c906108c 4827 value_free_to_mark (mark);
bf469271 4828 return res;
c906108c
SS
4829}
4830
5760d0ab 4831/* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
c906108c 4832
04afa70c
TT
4833bpstats::bpstats (struct bp_location *bl, bpstat **bs_link_pointer)
4834 : next (NULL),
b6433ede 4835 bp_location_at (bp_location_ref_ptr::new_reference (bl)),
04afa70c
TT
4836 breakpoint_at (bl->owner),
4837 commands (NULL),
04afa70c
TT
4838 print (0),
4839 stop (0),
4840 print_it (print_it_normal)
c906108c 4841{
04afa70c
TT
4842 **bs_link_pointer = this;
4843 *bs_link_pointer = &next;
4844}
4845
4846bpstats::bpstats ()
4847 : next (NULL),
04afa70c
TT
4848 breakpoint_at (NULL),
4849 commands (NULL),
04afa70c
TT
4850 print (0),
4851 stop (0),
4852 print_it (print_it_normal)
4853{
c906108c
SS
4854}
4855\f
d983da9c
DJ
4856/* The target has stopped with waitstatus WS. Check if any hardware
4857 watchpoints have triggered, according to the target. */
4858
4859int
4860watchpoints_triggered (struct target_waitstatus *ws)
4861{
57810aa7 4862 bool stopped_by_watchpoint = target_stopped_by_watchpoint ();
d983da9c
DJ
4863 CORE_ADDR addr;
4864 struct breakpoint *b;
4865
4866 if (!stopped_by_watchpoint)
4867 {
4868 /* We were not stopped by a watchpoint. Mark all watchpoints
4869 as not triggered. */
4870 ALL_BREAKPOINTS (b)
cc60f2e3 4871 if (is_hardware_watchpoint (b))
3a5c3e22
PA
4872 {
4873 struct watchpoint *w = (struct watchpoint *) b;
4874
4875 w->watchpoint_triggered = watch_triggered_no;
4876 }
d983da9c
DJ
4877
4878 return 0;
4879 }
4880
328d42d8 4881 if (!target_stopped_data_address (current_inferior ()->top_target (), &addr))
d983da9c
DJ
4882 {
4883 /* We were stopped by a watchpoint, but we don't know where.
4884 Mark all watchpoints as unknown. */
4885 ALL_BREAKPOINTS (b)
cc60f2e3 4886 if (is_hardware_watchpoint (b))
3a5c3e22
PA
4887 {
4888 struct watchpoint *w = (struct watchpoint *) b;
4889
4890 w->watchpoint_triggered = watch_triggered_unknown;
4891 }
d983da9c 4892
3c4797ba 4893 return 1;
d983da9c
DJ
4894 }
4895
4896 /* The target could report the data address. Mark watchpoints
4897 affected by this data address as triggered, and all others as not
4898 triggered. */
4899
4900 ALL_BREAKPOINTS (b)
cc60f2e3 4901 if (is_hardware_watchpoint (b))
d983da9c 4902 {
3a5c3e22 4903 struct watchpoint *w = (struct watchpoint *) b;
a5606eee 4904 struct bp_location *loc;
d983da9c 4905
3a5c3e22 4906 w->watchpoint_triggered = watch_triggered_no;
a5606eee 4907 for (loc = b->loc; loc; loc = loc->next)
9c06b0b4 4908 {
3a5c3e22 4909 if (is_masked_watchpoint (b))
9c06b0b4 4910 {
3a5c3e22
PA
4911 CORE_ADDR newaddr = addr & w->hw_wp_mask;
4912 CORE_ADDR start = loc->address & w->hw_wp_mask;
9c06b0b4
TJB
4913
4914 if (newaddr == start)
4915 {
3a5c3e22 4916 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
4917 break;
4918 }
4919 }
4920 /* Exact match not required. Within range is sufficient. */
328d42d8
SM
4921 else if (target_watchpoint_addr_within_range
4922 (current_inferior ()->top_target (), addr, loc->address,
4923 loc->length))
9c06b0b4 4924 {
3a5c3e22 4925 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
4926 break;
4927 }
4928 }
d983da9c
DJ
4929 }
4930
4931 return 1;
4932}
4933
bf469271
PA
4934/* Possible return values for watchpoint_check. */
4935enum wp_check_result
4936 {
4937 /* The watchpoint has been deleted. */
4938 WP_DELETED = 1,
4939
4940 /* The value has changed. */
4941 WP_VALUE_CHANGED = 2,
4942
4943 /* The value has not changed. */
4944 WP_VALUE_NOT_CHANGED = 3,
4945
4946 /* Ignore this watchpoint, no matter if the value changed or not. */
4947 WP_IGNORE = 4,
4948 };
c906108c
SS
4949
4950#define BP_TEMPFLAG 1
4951#define BP_HARDWAREFLAG 2
4952
4a64f543 4953/* Evaluate watchpoint condition expression and check if its value
bf469271 4954 changed. */
553e4c11 4955
bf469271
PA
4956static wp_check_result
4957watchpoint_check (bpstat bs)
c906108c 4958{
3a5c3e22 4959 struct watchpoint *b;
c906108c
SS
4960 struct frame_info *fr;
4961 int within_current_scope;
4962
f431efe5 4963 /* BS is built from an existing struct breakpoint. */
2bdf28a0 4964 gdb_assert (bs->breakpoint_at != NULL);
3a5c3e22 4965 b = (struct watchpoint *) bs->breakpoint_at;
d0fb5eae 4966
f6bc2008
PA
4967 /* If this is a local watchpoint, we only want to check if the
4968 watchpoint frame is in scope if the current thread is the thread
4969 that was used to create the watchpoint. */
4970 if (!watchpoint_in_thread_scope (b))
60e1c644 4971 return WP_IGNORE;
f6bc2008 4972
c906108c
SS
4973 if (b->exp_valid_block == NULL)
4974 within_current_scope = 1;
4975 else
4976 {
edb3359d
DJ
4977 struct frame_info *frame = get_current_frame ();
4978 struct gdbarch *frame_arch = get_frame_arch (frame);
4979 CORE_ADDR frame_pc = get_frame_pc (frame);
4980
c9cf6e20 4981 /* stack_frame_destroyed_p() returns a non-zero value if we're
4a64f543
MS
4982 still in the function but the stack frame has already been
4983 invalidated. Since we can't rely on the values of local
4984 variables after the stack has been destroyed, we are treating
4985 the watchpoint in that state as `not changed' without further
4986 checking. Don't mark watchpoints as changed if the current
4987 frame is in an epilogue - even if they are in some other
4988 frame, our view of the stack is likely to be wrong and
4989 frame_find_by_id could error out. */
c9cf6e20 4990 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
60e1c644 4991 return WP_IGNORE;
a0f49112 4992
101dcfbe 4993 fr = frame_find_by_id (b->watchpoint_frame);
c906108c 4994 within_current_scope = (fr != NULL);
69fbadd5
DJ
4995
4996 /* If we've gotten confused in the unwinder, we might have
4997 returned a frame that can't describe this variable. */
edb3359d
DJ
4998 if (within_current_scope)
4999 {
5000 struct symbol *function;
5001
5002 function = get_frame_function (fr);
5003 if (function == NULL
5004 || !contained_in (b->exp_valid_block,
5005 SYMBOL_BLOCK_VALUE (function)))
5006 within_current_scope = 0;
5007 }
69fbadd5 5008
edb3359d 5009 if (within_current_scope)
c906108c
SS
5010 /* If we end up stopping, the current frame will get selected
5011 in normal_stop. So this call to select_frame won't affect
5012 the user. */
0f7d239c 5013 select_frame (fr);
c906108c 5014 }
c5aa993b 5015
c906108c
SS
5016 if (within_current_scope)
5017 {
4a64f543 5018 /* We use value_{,free_to_}mark because it could be a *long*
dda83cd7
SM
5019 time before we return to the command level and call
5020 free_all_values. We can't call free_all_values because we
5021 might be in the middle of evaluating a function call. */
c906108c 5022
9c06b0b4 5023 struct value *mark;
fa4727a6
DJ
5024 struct value *new_val;
5025
c1fc2657 5026 if (is_masked_watchpoint (b))
9c06b0b4
TJB
5027 /* Since we don't know the exact trigger address (from
5028 stopped_data_address), just tell the user we've triggered
5029 a mask watchpoint. */
5030 return WP_VALUE_CHANGED;
5031
5032 mark = value_mark ();
1eaebe02 5033 fetch_subexp_value (b->exp.get (), b->exp->op.get (), &new_val,
413403fc 5034 NULL, NULL, false);
218d2fc6 5035
bb9d5f81
PP
5036 if (b->val_bitsize != 0)
5037 new_val = extract_bitfield_from_watchpoint_value (b, new_val);
5038
4a64f543
MS
5039 /* We use value_equal_contents instead of value_equal because
5040 the latter coerces an array to a pointer, thus comparing just
5041 the address of the array instead of its contents. This is
5042 not what we want. */
fa4727a6 5043 if ((b->val != NULL) != (new_val != NULL)
850645cf
TT
5044 || (b->val != NULL && !value_equal_contents (b->val.get (),
5045 new_val)))
c906108c 5046 {
c906108c 5047 bs->old_val = b->val;
850645cf 5048 b->val = release_value (new_val);
4c1d86d9 5049 b->val_valid = true;
850645cf
TT
5050 if (new_val != NULL)
5051 value_free_to_mark (mark);
c906108c
SS
5052 return WP_VALUE_CHANGED;
5053 }
5054 else
5055 {
60e1c644 5056 /* Nothing changed. */
c906108c 5057 value_free_to_mark (mark);
c906108c
SS
5058 return WP_VALUE_NOT_CHANGED;
5059 }
5060 }
5061 else
5062 {
5063 /* This seems like the only logical thing to do because
dda83cd7
SM
5064 if we temporarily ignored the watchpoint, then when
5065 we reenter the block in which it is valid it contains
5066 garbage (in the case of a function, it may have two
5067 garbage values, one before and one after the prologue).
5068 So we can't even detect the first assignment to it and
5069 watch after that (since the garbage may or may not equal
5070 the first value assigned). */
348d480f
PA
5071 /* We print all the stop information in
5072 breakpoint_ops->print_it, but in this case, by the time we
5073 call breakpoint_ops->print_it this bp will be deleted
5074 already. So we have no choice but print the information
5075 here. */
468afe6c 5076
0e454242 5077 SWITCH_THRU_ALL_UIS ()
dda83cd7 5078 {
468afe6c
PA
5079 struct ui_out *uiout = current_uiout;
5080
112e8700
SM
5081 if (uiout->is_mi_like_p ())
5082 uiout->field_string
5083 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
6a831f06
PA
5084 uiout->message ("\nWatchpoint %pF deleted because the program has "
5085 "left the block in\n"
5086 "which its expression is valid.\n",
5087 signed_field ("wpnum", b->number));
468afe6c 5088 }
4ce44c66 5089
cdac0397 5090 /* Make sure the watchpoint's commands aren't executed. */
d1b0a7bf 5091 b->commands = NULL;
d0fb5eae 5092 watchpoint_del_at_next_stop (b);
c906108c
SS
5093
5094 return WP_DELETED;
5095 }
5096}
5097
18a18393 5098/* Return true if it looks like target has stopped due to hitting
348d480f
PA
5099 breakpoint location BL. This function does not check if we should
5100 stop, only if BL explains the stop. */
5101
18a18393 5102static int
6c95b8df 5103bpstat_check_location (const struct bp_location *bl,
accd0bcd 5104 const address_space *aspace, CORE_ADDR bp_addr,
09ac7c10 5105 const struct target_waitstatus *ws)
18a18393
VP
5106{
5107 struct breakpoint *b = bl->owner;
5108
348d480f 5109 /* BL is from an existing breakpoint. */
2bdf28a0
JK
5110 gdb_assert (b != NULL);
5111
bd522513 5112 return b->ops->breakpoint_hit (bl, aspace, bp_addr, ws);
18a18393
VP
5113}
5114
3a5c3e22
PA
5115/* Determine if the watched values have actually changed, and we
5116 should stop. If not, set BS->stop to 0. */
5117
18a18393
VP
5118static void
5119bpstat_check_watchpoint (bpstat bs)
5120{
2bdf28a0 5121 const struct bp_location *bl;
3a5c3e22 5122 struct watchpoint *b;
2bdf28a0
JK
5123
5124 /* BS is built for existing struct breakpoint. */
b6433ede 5125 bl = bs->bp_location_at.get ();
2bdf28a0 5126 gdb_assert (bl != NULL);
3a5c3e22 5127 b = (struct watchpoint *) bs->breakpoint_at;
2bdf28a0 5128 gdb_assert (b != NULL);
18a18393 5129
18a18393 5130 {
18a18393
VP
5131 int must_check_value = 0;
5132
c1fc2657 5133 if (b->type == bp_watchpoint)
18a18393
VP
5134 /* For a software watchpoint, we must always check the
5135 watched value. */
5136 must_check_value = 1;
5137 else if (b->watchpoint_triggered == watch_triggered_yes)
5138 /* We have a hardware watchpoint (read, write, or access)
5139 and the target earlier reported an address watched by
5140 this watchpoint. */
5141 must_check_value = 1;
5142 else if (b->watchpoint_triggered == watch_triggered_unknown
c1fc2657 5143 && b->type == bp_hardware_watchpoint)
18a18393
VP
5144 /* We were stopped by a hardware watchpoint, but the target could
5145 not report the data address. We must check the watchpoint's
5146 value. Access and read watchpoints are out of luck; without
5147 a data address, we can't figure it out. */
5148 must_check_value = 1;
3a5c3e22 5149
18a18393
VP
5150 if (must_check_value)
5151 {
bf469271
PA
5152 wp_check_result e;
5153
a70b8144 5154 try
bf469271
PA
5155 {
5156 e = watchpoint_check (bs);
5157 }
230d2906 5158 catch (const gdb_exception &ex)
bf469271
PA
5159 {
5160 exception_fprintf (gdb_stderr, ex,
5161 "Error evaluating expression "
5162 "for watchpoint %d\n",
5163 b->number);
5164
5165 SWITCH_THRU_ALL_UIS ()
5166 {
5167 printf_filtered (_("Watchpoint %d deleted.\n"),
5168 b->number);
5169 }
5170 watchpoint_del_at_next_stop (b);
5171 e = WP_DELETED;
5172 }
bf469271 5173
18a18393
VP
5174 switch (e)
5175 {
5176 case WP_DELETED:
5177 /* We've already printed what needs to be printed. */
5178 bs->print_it = print_it_done;
5179 /* Stop. */
5180 break;
60e1c644
PA
5181 case WP_IGNORE:
5182 bs->print_it = print_it_noop;
5183 bs->stop = 0;
5184 break;
18a18393 5185 case WP_VALUE_CHANGED:
c1fc2657 5186 if (b->type == bp_read_watchpoint)
18a18393 5187 {
85d721b8
PA
5188 /* There are two cases to consider here:
5189
4a64f543 5190 1. We're watching the triggered memory for reads.
85d721b8
PA
5191 In that case, trust the target, and always report
5192 the watchpoint hit to the user. Even though
5193 reads don't cause value changes, the value may
5194 have changed since the last time it was read, and
5195 since we're not trapping writes, we will not see
5196 those, and as such we should ignore our notion of
5197 old value.
5198
4a64f543 5199 2. We're watching the triggered memory for both
85d721b8
PA
5200 reads and writes. There are two ways this may
5201 happen:
5202
4a64f543 5203 2.1. This is a target that can't break on data
85d721b8
PA
5204 reads only, but can break on accesses (reads or
5205 writes), such as e.g., x86. We detect this case
5206 at the time we try to insert read watchpoints.
5207
4a64f543 5208 2.2. Otherwise, the target supports read
85d721b8
PA
5209 watchpoints, but, the user set an access or write
5210 watchpoint watching the same memory as this read
5211 watchpoint.
5212
5213 If we're watching memory writes as well as reads,
5214 ignore watchpoint hits when we find that the
5215 value hasn't changed, as reads don't cause
5216 changes. This still gives false positives when
5217 the program writes the same value to memory as
5218 what there was already in memory (we will confuse
5219 it for a read), but it's much better than
5220 nothing. */
5221
5222 int other_write_watchpoint = 0;
5223
5224 if (bl->watchpoint_type == hw_read)
5225 {
5226 struct breakpoint *other_b;
5227
5228 ALL_BREAKPOINTS (other_b)
3a5c3e22
PA
5229 if (other_b->type == bp_hardware_watchpoint
5230 || other_b->type == bp_access_watchpoint)
85d721b8 5231 {
3a5c3e22
PA
5232 struct watchpoint *other_w =
5233 (struct watchpoint *) other_b;
5234
5235 if (other_w->watchpoint_triggered
5236 == watch_triggered_yes)
5237 {
5238 other_write_watchpoint = 1;
5239 break;
5240 }
85d721b8
PA
5241 }
5242 }
5243
5244 if (other_write_watchpoint
5245 || bl->watchpoint_type == hw_access)
5246 {
5247 /* We're watching the same memory for writes,
5248 and the value changed since the last time we
5249 updated it, so this trap must be for a write.
5250 Ignore it. */
5251 bs->print_it = print_it_noop;
5252 bs->stop = 0;
5253 }
18a18393
VP
5254 }
5255 break;
5256 case WP_VALUE_NOT_CHANGED:
c1fc2657
SM
5257 if (b->type == bp_hardware_watchpoint
5258 || b->type == bp_watchpoint)
18a18393
VP
5259 {
5260 /* Don't stop: write watchpoints shouldn't fire if
5261 the value hasn't changed. */
5262 bs->print_it = print_it_noop;
5263 bs->stop = 0;
5264 }
5265 /* Stop. */
5266 break;
5267 default:
5268 /* Can't happen. */
18a18393
VP
5269 break;
5270 }
5271 }
5272 else /* must_check_value == 0 */
5273 {
5274 /* This is a case where some watchpoint(s) triggered, but
5275 not at the address of this watchpoint, or else no
5276 watchpoint triggered after all. So don't print
5277 anything for this watchpoint. */
5278 bs->print_it = print_it_noop;
5279 bs->stop = 0;
5280 }
5281 }
5282}
5283
7d4df6a4
DE
5284/* For breakpoints that are currently marked as telling gdb to stop,
5285 check conditions (condition proper, frame, thread and ignore count)
18a18393
VP
5286 of breakpoint referred to by BS. If we should not stop for this
5287 breakpoint, set BS->stop to 0. */
f431efe5 5288
18a18393 5289static void
00431a78 5290bpstat_check_breakpoint_conditions (bpstat bs, thread_info *thread)
18a18393 5291{
2bdf28a0
JK
5292 const struct bp_location *bl;
5293 struct breakpoint *b;
bf469271
PA
5294 /* Assume stop. */
5295 bool condition_result = true;
7d4df6a4
DE
5296 struct expression *cond;
5297
5298 gdb_assert (bs->stop);
2bdf28a0
JK
5299
5300 /* BS is built for existing struct breakpoint. */
b6433ede 5301 bl = bs->bp_location_at.get ();
2bdf28a0 5302 gdb_assert (bl != NULL);
f431efe5 5303 b = bs->breakpoint_at;
2bdf28a0 5304 gdb_assert (b != NULL);
18a18393 5305
b775012e
LM
5306 /* Even if the target evaluated the condition on its end and notified GDB, we
5307 need to do so again since GDB does not know if we stopped due to a
5308 breakpoint or a single step breakpoint. */
5309
18a18393 5310 if (frame_id_p (b->frame_id)
edb3359d 5311 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
18a18393 5312 {
7d4df6a4
DE
5313 bs->stop = 0;
5314 return;
5315 }
60e1c644 5316
12ab52e9
PA
5317 /* If this is a thread/task-specific breakpoint, don't waste cpu
5318 evaluating the condition if this isn't the specified
5319 thread/task. */
00431a78
PA
5320 if ((b->thread != -1 && b->thread != thread->global_num)
5321 || (b->task != 0 && b->task != ada_get_task_number (thread)))
6c1b0f7b
DE
5322 {
5323 bs->stop = 0;
5324 return;
5325 }
5326
6dddc817
DE
5327 /* Evaluate extension language breakpoints that have a "stop" method
5328 implemented. */
5329 bs->stop = breakpoint_ext_lang_cond_says_stop (b);
7371cf6d 5330
7d4df6a4
DE
5331 if (is_watchpoint (b))
5332 {
5333 struct watchpoint *w = (struct watchpoint *) b;
3a5c3e22 5334
4d01a485 5335 cond = w->cond_exp.get ();
7d4df6a4
DE
5336 }
5337 else
4d01a485 5338 cond = bl->cond.get ();
60e1c644 5339
7d4df6a4
DE
5340 if (cond && b->disposition != disp_del_at_next_stop)
5341 {
5342 int within_current_scope = 1;
5343 struct watchpoint * w;
60e1c644 5344
7d4df6a4
DE
5345 /* We use value_mark and value_free_to_mark because it could
5346 be a long time before we return to the command level and
5347 call free_all_values. We can't call free_all_values
5348 because we might be in the middle of evaluating a
5349 function call. */
5350 struct value *mark = value_mark ();
5351
5352 if (is_watchpoint (b))
5353 w = (struct watchpoint *) b;
5354 else
5355 w = NULL;
5356
5357 /* Need to select the frame, with all that implies so that
5358 the conditions will have the right context. Because we
5359 use the frame, we will not see an inlined function's
5360 variables when we arrive at a breakpoint at the start
5361 of the inlined function; the current frame will be the
5362 call site. */
5363 if (w == NULL || w->cond_exp_valid_block == NULL)
5364 select_frame (get_current_frame ());
5365 else
18a18393 5366 {
7d4df6a4
DE
5367 struct frame_info *frame;
5368
5369 /* For local watchpoint expressions, which particular
5370 instance of a local is being watched matters, so we
5371 keep track of the frame to evaluate the expression
5372 in. To evaluate the condition however, it doesn't
5373 really matter which instantiation of the function
5374 where the condition makes sense triggers the
5375 watchpoint. This allows an expression like "watch
5376 global if q > 10" set in `func', catch writes to
5377 global on all threads that call `func', or catch
5378 writes on all recursive calls of `func' by a single
5379 thread. We simply always evaluate the condition in
5380 the innermost frame that's executing where it makes
5381 sense to evaluate the condition. It seems
5382 intuitive. */
5383 frame = block_innermost_frame (w->cond_exp_valid_block);
5384 if (frame != NULL)
5385 select_frame (frame);
5386 else
5387 within_current_scope = 0;
18a18393 5388 }
7d4df6a4 5389 if (within_current_scope)
bf469271 5390 {
a70b8144 5391 try
bf469271
PA
5392 {
5393 condition_result = breakpoint_cond_eval (cond);
5394 }
230d2906 5395 catch (const gdb_exception &ex)
bf469271
PA
5396 {
5397 exception_fprintf (gdb_stderr, ex,
5398 "Error in testing breakpoint condition:\n");
5399 }
bf469271 5400 }
7d4df6a4 5401 else
18a18393 5402 {
7d4df6a4
DE
5403 warning (_("Watchpoint condition cannot be tested "
5404 "in the current scope"));
5405 /* If we failed to set the right context for this
5406 watchpoint, unconditionally report it. */
18a18393 5407 }
7d4df6a4
DE
5408 /* FIXME-someday, should give breakpoint #. */
5409 value_free_to_mark (mark);
18a18393 5410 }
7d4df6a4 5411
bf469271 5412 if (cond && !condition_result)
7d4df6a4
DE
5413 {
5414 bs->stop = 0;
5415 }
7d4df6a4
DE
5416 else if (b->ignore_count > 0)
5417 {
5418 b->ignore_count--;
5419 bs->stop = 0;
5420 /* Increase the hit count even though we don't stop. */
5421 ++(b->hit_count);
76727919 5422 gdb::observers::breakpoint_modified.notify (b);
7d4df6a4 5423 }
18a18393
VP
5424}
5425
1cf4d951
PA
5426/* Returns true if we need to track moribund locations of LOC's type
5427 on the current target. */
5428
5429static int
5430need_moribund_for_location_type (struct bp_location *loc)
5431{
5432 return ((loc->loc_type == bp_loc_software_breakpoint
5433 && !target_supports_stopped_by_sw_breakpoint ())
5434 || (loc->loc_type == bp_loc_hardware_breakpoint
5435 && !target_supports_stopped_by_hw_breakpoint ()));
5436}
5437
ddfe970e 5438/* See breakpoint.h. */
c906108c
SS
5439
5440bpstat
ddfe970e 5441build_bpstat_chain (const address_space *aspace, CORE_ADDR bp_addr,
09ac7c10 5442 const struct target_waitstatus *ws)
c906108c 5443{
ddfe970e 5444 struct breakpoint *b;
5760d0ab 5445 bpstat bs_head = NULL, *bs_link = &bs_head;
c5aa993b 5446
429374b8
JK
5447 ALL_BREAKPOINTS (b)
5448 {
1a853c52 5449 if (!breakpoint_enabled (b))
429374b8 5450 continue;
a5606eee 5451
ddfe970e 5452 for (bp_location *bl = b->loc; bl != NULL; bl = bl->next)
429374b8 5453 {
4a64f543
MS
5454 /* For hardware watchpoints, we look only at the first
5455 location. The watchpoint_check function will work on the
5456 entire expression, not the individual locations. For
5457 read watchpoints, the watchpoints_triggered function has
5458 checked all locations already. */
429374b8
JK
5459 if (b->type == bp_hardware_watchpoint && bl != b->loc)
5460 break;
18a18393 5461
b5fa468f 5462 if (!bl->enabled || bl->disabled_by_cond || bl->shlib_disabled)
429374b8 5463 continue;
c5aa993b 5464
09ac7c10 5465 if (!bpstat_check_location (bl, aspace, bp_addr, ws))
429374b8 5466 continue;
c5aa993b 5467
4a64f543
MS
5468 /* Come here if it's a watchpoint, or if the break address
5469 matches. */
c5aa993b 5470
ddfe970e
KS
5471 bpstat bs = new bpstats (bl, &bs_link); /* Alloc a bpstat to
5472 explain stop. */
c5aa993b 5473
f431efe5
PA
5474 /* Assume we stop. Should we find a watchpoint that is not
5475 actually triggered, or if the condition of the breakpoint
5476 evaluates as false, we'll reset 'stop' to 0. */
429374b8
JK
5477 bs->stop = 1;
5478 bs->print = 1;
d983da9c 5479
f431efe5
PA
5480 /* If this is a scope breakpoint, mark the associated
5481 watchpoint as triggered so that we will handle the
5482 out-of-scope event. We'll get to the watchpoint next
5483 iteration. */
d0fb5eae 5484 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
3a5c3e22
PA
5485 {
5486 struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
5487
5488 w->watchpoint_triggered = watch_triggered_yes;
5489 }
f431efe5
PA
5490 }
5491 }
5492
7c16b83e 5493 /* Check if a moribund breakpoint explains the stop. */
1cf4d951
PA
5494 if (!target_supports_stopped_by_sw_breakpoint ()
5495 || !target_supports_stopped_by_hw_breakpoint ())
f431efe5 5496 {
1123588c 5497 for (bp_location *loc : moribund_locations)
f431efe5 5498 {
1cf4d951
PA
5499 if (breakpoint_location_address_match (loc, aspace, bp_addr)
5500 && need_moribund_for_location_type (loc))
5501 {
ddfe970e 5502 bpstat bs = new bpstats (loc, &bs_link);
1cf4d951
PA
5503 /* For hits of moribund locations, we should just proceed. */
5504 bs->stop = 0;
5505 bs->print = 0;
5506 bs->print_it = print_it_noop;
5507 }
f431efe5
PA
5508 }
5509 }
5510
ddfe970e
KS
5511 return bs_head;
5512}
5513
5514/* See breakpoint.h. */
5515
5516bpstat
5517bpstat_stop_status (const address_space *aspace,
00431a78 5518 CORE_ADDR bp_addr, thread_info *thread,
ddfe970e
KS
5519 const struct target_waitstatus *ws,
5520 bpstat stop_chain)
5521{
5522 struct breakpoint *b = NULL;
5523 /* First item of allocated bpstat's. */
5524 bpstat bs_head = stop_chain;
5525 bpstat bs;
5526 int need_remove_insert;
5527 int removed_any;
5528
5529 /* First, build the bpstat chain with locations that explain a
5530 target stop, while being careful to not set the target running,
5531 as that may invalidate locations (in particular watchpoint
5532 locations are recreated). Resuming will happen here with
5533 breakpoint conditions or watchpoint expressions that include
5534 inferior function calls. */
5535 if (bs_head == NULL)
5536 bs_head = build_bpstat_chain (aspace, bp_addr, ws);
5537
edcc5120
TT
5538 /* A bit of special processing for shlib breakpoints. We need to
5539 process solib loading here, so that the lists of loaded and
5540 unloaded libraries are correct before we handle "catch load" and
5541 "catch unload". */
5542 for (bs = bs_head; bs != NULL; bs = bs->next)
5543 {
5d268276 5544 if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
edcc5120
TT
5545 {
5546 handle_solib_event ();
5547 break;
5548 }
5549 }
5550
f431efe5
PA
5551 /* Now go through the locations that caused the target to stop, and
5552 check whether we're interested in reporting this stop to higher
5553 layers, or whether we should resume the target transparently. */
5554
5555 removed_any = 0;
5556
5760d0ab 5557 for (bs = bs_head; bs != NULL; bs = bs->next)
f431efe5
PA
5558 {
5559 if (!bs->stop)
5560 continue;
5561
f431efe5 5562 b = bs->breakpoint_at;
348d480f
PA
5563 b->ops->check_status (bs);
5564 if (bs->stop)
28010a5d 5565 {
00431a78 5566 bpstat_check_breakpoint_conditions (bs, thread);
f431efe5 5567
429374b8
JK
5568 if (bs->stop)
5569 {
5570 ++(b->hit_count);
76727919 5571 gdb::observers::breakpoint_modified.notify (b);
c906108c 5572
4a64f543 5573 /* We will stop here. */
429374b8
JK
5574 if (b->disposition == disp_disable)
5575 {
816338b5 5576 --(b->enable_count);
1a853c52 5577 if (b->enable_count <= 0)
429374b8 5578 b->enable_state = bp_disabled;
f431efe5 5579 removed_any = 1;
429374b8
JK
5580 }
5581 if (b->silent)
5582 bs->print = 0;
5583 bs->commands = b->commands;
abf85f46 5584 if (command_line_is_silent (bs->commands
d1b0a7bf 5585 ? bs->commands.get () : NULL))
abf85f46 5586 bs->print = 0;
9d6e6e84
HZ
5587
5588 b->ops->after_condition_true (bs);
429374b8
JK
5589 }
5590
348d480f 5591 }
a9b3a50f
PA
5592
5593 /* Print nothing for this entry if we don't stop or don't
5594 print. */
5595 if (!bs->stop || !bs->print)
5596 bs->print_it = print_it_noop;
429374b8 5597 }
876fa593 5598
d983da9c
DJ
5599 /* If we aren't stopping, the value of some hardware watchpoint may
5600 not have changed, but the intermediate memory locations we are
5601 watching may have. Don't bother if we're stopping; this will get
5602 done later. */
d832cb68 5603 need_remove_insert = 0;
5760d0ab
JK
5604 if (! bpstat_causes_stop (bs_head))
5605 for (bs = bs_head; bs != NULL; bs = bs->next)
d983da9c 5606 if (!bs->stop
f431efe5
PA
5607 && bs->breakpoint_at
5608 && is_hardware_watchpoint (bs->breakpoint_at))
d983da9c 5609 {
3a5c3e22
PA
5610 struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
5611
5612 update_watchpoint (w, 0 /* don't reparse. */);
d832cb68 5613 need_remove_insert = 1;
d983da9c
DJ
5614 }
5615
d832cb68 5616 if (need_remove_insert)
44702360 5617 update_global_location_list (UGLL_MAY_INSERT);
f431efe5 5618 else if (removed_any)
44702360 5619 update_global_location_list (UGLL_DONT_INSERT);
d832cb68 5620
5760d0ab 5621 return bs_head;
c906108c 5622}
628fe4e4
JK
5623
5624static void
61c26be8 5625handle_jit_event (CORE_ADDR address)
628fe4e4 5626{
628fe4e4
JK
5627 struct gdbarch *gdbarch;
5628
1eb8556f 5629 infrun_debug_printf ("handling bp_jit_event");
243a9253 5630
628fe4e4
JK
5631 /* Switch terminal for any messages produced by
5632 breakpoint_re_set. */
223ffa71 5633 target_terminal::ours_for_output ();
628fe4e4 5634
61c26be8
MS
5635 gdbarch = get_frame_arch (get_current_frame ());
5636 /* This event is caused by a breakpoint set in `jit_breakpoint_re_set`,
5637 thus it is expected that its objectfile can be found through
5638 minimal symbol lookup. If it doesn't work (and assert fails), it
5639 most likely means that `jit_breakpoint_re_set` was changes and this
5640 function needs to be updated too. */
5641 bound_minimal_symbol jit_bp_sym = lookup_minimal_symbol_by_pc (address);
5642 gdb_assert (jit_bp_sym.objfile != nullptr);
5643 jit_event_handler (gdbarch, jit_bp_sym.objfile);
628fe4e4 5644
223ffa71 5645 target_terminal::inferior ();
628fe4e4
JK
5646}
5647
5648/* Prepare WHAT final decision for infrun. */
5649
5650/* Decide what infrun needs to do with this bpstat. */
5651
c906108c 5652struct bpstat_what
0e30163f 5653bpstat_what (bpstat bs_head)
c906108c 5654{
c906108c 5655 struct bpstat_what retval;
0e30163f 5656 bpstat bs;
c906108c 5657
628fe4e4 5658 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
aa7d318d 5659 retval.call_dummy = STOP_NONE;
e2d0f980 5660 retval.is_longjmp = false;
628fe4e4 5661
0e30163f 5662 for (bs = bs_head; bs != NULL; bs = bs->next)
c906108c 5663 {
628fe4e4
JK
5664 /* Extract this BS's action. After processing each BS, we check
5665 if its action overrides all we've seem so far. */
5666 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
5667 enum bptype bptype;
5668
c906108c 5669 if (bs->breakpoint_at == NULL)
628fe4e4
JK
5670 {
5671 /* I suspect this can happen if it was a momentary
5672 breakpoint which has since been deleted. */
5673 bptype = bp_none;
5674 }
20874c92 5675 else
f431efe5 5676 bptype = bs->breakpoint_at->type;
628fe4e4
JK
5677
5678 switch (bptype)
c906108c
SS
5679 {
5680 case bp_none:
628fe4e4 5681 break;
c906108c
SS
5682 case bp_breakpoint:
5683 case bp_hardware_breakpoint:
7c16b83e 5684 case bp_single_step:
c906108c
SS
5685 case bp_until:
5686 case bp_finish:
a9b3a50f 5687 case bp_shlib_event:
c906108c
SS
5688 if (bs->stop)
5689 {
5690 if (bs->print)
628fe4e4 5691 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 5692 else
628fe4e4 5693 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
5694 }
5695 else
628fe4e4 5696 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5697 break;
5698 case bp_watchpoint:
5699 case bp_hardware_watchpoint:
5700 case bp_read_watchpoint:
5701 case bp_access_watchpoint:
5702 if (bs->stop)
5703 {
5704 if (bs->print)
628fe4e4 5705 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 5706 else
628fe4e4 5707 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
5708 }
5709 else
628fe4e4
JK
5710 {
5711 /* There was a watchpoint, but we're not stopping.
5712 This requires no further action. */
5713 }
c906108c
SS
5714 break;
5715 case bp_longjmp:
e2e4d78b 5716 case bp_longjmp_call_dummy:
186c406b 5717 case bp_exception:
0a39bb32
PA
5718 if (bs->stop)
5719 {
5720 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
5721 retval.is_longjmp = bptype != bp_exception;
5722 }
5723 else
5724 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5725 break;
5726 case bp_longjmp_resume:
186c406b 5727 case bp_exception_resume:
0a39bb32
PA
5728 if (bs->stop)
5729 {
5730 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
5731 retval.is_longjmp = bptype == bp_longjmp_resume;
5732 }
5733 else
5734 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5735 break;
5736 case bp_step_resume:
5737 if (bs->stop)
628fe4e4
JK
5738 this_action = BPSTAT_WHAT_STEP_RESUME;
5739 else
c906108c 5740 {
628fe4e4
JK
5741 /* It is for the wrong frame. */
5742 this_action = BPSTAT_WHAT_SINGLE;
c906108c 5743 }
c906108c 5744 break;
2c03e5be
PA
5745 case bp_hp_step_resume:
5746 if (bs->stop)
5747 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
5748 else
5749 {
5750 /* It is for the wrong frame. */
5751 this_action = BPSTAT_WHAT_SINGLE;
5752 }
5753 break;
c906108c 5754 case bp_watchpoint_scope:
c4093a6a 5755 case bp_thread_event:
1900040c 5756 case bp_overlay_event:
0fd8e87f 5757 case bp_longjmp_master:
aa7d318d 5758 case bp_std_terminate_master:
186c406b 5759 case bp_exception_master:
628fe4e4 5760 this_action = BPSTAT_WHAT_SINGLE;
c4093a6a 5761 break;
ce78b96d 5762 case bp_catchpoint:
c5aa993b
JM
5763 if (bs->stop)
5764 {
5765 if (bs->print)
628fe4e4 5766 this_action = BPSTAT_WHAT_STOP_NOISY;
c5aa993b 5767 else
628fe4e4 5768 this_action = BPSTAT_WHAT_STOP_SILENT;
c5aa993b
JM
5769 }
5770 else
628fe4e4 5771 {
cb1e4e32
PA
5772 /* Some catchpoints are implemented with breakpoints.
5773 For those, we need to step over the breakpoint. */
5774 if (bs->bp_location_at->loc_type != bp_loc_other)
5775 this_action = BPSTAT_WHAT_SINGLE;
628fe4e4
JK
5776 }
5777 break;
628fe4e4 5778 case bp_jit_event:
628fe4e4 5779 this_action = BPSTAT_WHAT_SINGLE;
c5aa993b 5780 break;
c906108c 5781 case bp_call_dummy:
53a5351d
JM
5782 /* Make sure the action is stop (silent or noisy),
5783 so infrun.c pops the dummy frame. */
aa7d318d 5784 retval.call_dummy = STOP_STACK_DUMMY;
628fe4e4 5785 this_action = BPSTAT_WHAT_STOP_SILENT;
aa7d318d
TT
5786 break;
5787 case bp_std_terminate:
5788 /* Make sure the action is stop (silent or noisy),
5789 so infrun.c pops the dummy frame. */
aa7d318d 5790 retval.call_dummy = STOP_STD_TERMINATE;
628fe4e4 5791 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c 5792 break;
1042e4c0 5793 case bp_tracepoint:
7a697b8d 5794 case bp_fast_tracepoint:
0fb4aa4b 5795 case bp_static_tracepoint:
1042e4c0
SS
5796 /* Tracepoint hits should not be reported back to GDB, and
5797 if one got through somehow, it should have been filtered
5798 out already. */
5799 internal_error (__FILE__, __LINE__,
7a697b8d 5800 _("bpstat_what: tracepoint encountered"));
0e30163f
JK
5801 break;
5802 case bp_gnu_ifunc_resolver:
5803 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
5804 this_action = BPSTAT_WHAT_SINGLE;
5805 break;
5806 case bp_gnu_ifunc_resolver_return:
5807 /* The breakpoint will be removed, execution will restart from the
5808 PC of the former breakpoint. */
5809 this_action = BPSTAT_WHAT_KEEP_CHECKING;
5810 break;
e7e0cddf
SS
5811
5812 case bp_dprintf:
a11cfd87
HZ
5813 if (bs->stop)
5814 this_action = BPSTAT_WHAT_STOP_SILENT;
5815 else
5816 this_action = BPSTAT_WHAT_SINGLE;
e7e0cddf
SS
5817 break;
5818
628fe4e4
JK
5819 default:
5820 internal_error (__FILE__, __LINE__,
5821 _("bpstat_what: unhandled bptype %d"), (int) bptype);
c906108c 5822 }
628fe4e4 5823
325fac50 5824 retval.main_action = std::max (retval.main_action, this_action);
c906108c 5825 }
628fe4e4 5826
243a9253
PA
5827 return retval;
5828}
628fe4e4 5829
243a9253
PA
5830void
5831bpstat_run_callbacks (bpstat bs_head)
5832{
5833 bpstat bs;
628fe4e4 5834
0e30163f
JK
5835 for (bs = bs_head; bs != NULL; bs = bs->next)
5836 {
5837 struct breakpoint *b = bs->breakpoint_at;
5838
5839 if (b == NULL)
5840 continue;
5841 switch (b->type)
5842 {
243a9253 5843 case bp_jit_event:
61c26be8 5844 handle_jit_event (bs->bp_location_at->address);
243a9253 5845 break;
0e30163f
JK
5846 case bp_gnu_ifunc_resolver:
5847 gnu_ifunc_resolver_stop (b);
5848 break;
5849 case bp_gnu_ifunc_resolver_return:
5850 gnu_ifunc_resolver_return_stop (b);
5851 break;
5852 }
5853 }
c906108c
SS
5854}
5855
4c462cb0 5856/* See breakpoint.h. */
c906108c 5857
4c462cb0
SM
5858bool
5859bpstat_should_step ()
c906108c
SS
5860{
5861 struct breakpoint *b;
cc59ec59 5862
c906108c 5863 ALL_BREAKPOINTS (b)
717a8278 5864 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
4c462cb0
SM
5865 return true;
5866 return false;
c906108c
SS
5867}
5868
4c462cb0
SM
5869/* See breakpoint.h. */
5870
5871bool
67822962
PA
5872bpstat_causes_stop (bpstat bs)
5873{
5874 for (; bs != NULL; bs = bs->next)
5875 if (bs->stop)
4c462cb0 5876 return true;
67822962 5877
4c462cb0 5878 return false;
67822962
PA
5879}
5880
c906108c 5881\f
c5aa993b 5882
170b53b2
UW
5883/* Compute a string of spaces suitable to indent the next line
5884 so it starts at the position corresponding to the table column
5885 named COL_NAME in the currently active table of UIOUT. */
5886
5887static char *
5888wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
5889{
5890 static char wrap_indent[80];
5891 int i, total_width, width, align;
c5209615 5892 const char *text;
170b53b2
UW
5893
5894 total_width = 0;
112e8700 5895 for (i = 1; uiout->query_table_field (i, &width, &align, &text); i++)
170b53b2
UW
5896 {
5897 if (strcmp (text, col_name) == 0)
5898 {
5899 gdb_assert (total_width < sizeof wrap_indent);
5900 memset (wrap_indent, ' ', total_width);
5901 wrap_indent[total_width] = 0;
5902
5903 return wrap_indent;
5904 }
5905
5906 total_width += width + 1;
5907 }
5908
5909 return NULL;
5910}
5911
b775012e
LM
5912/* Determine if the locations of this breakpoint will have their conditions
5913 evaluated by the target, host or a mix of both. Returns the following:
5914
5915 "host": Host evals condition.
5916 "host or target": Host or Target evals condition.
5917 "target": Target evals condition.
5918*/
5919
5920static const char *
5921bp_condition_evaluator (struct breakpoint *b)
5922{
5923 struct bp_location *bl;
5924 char host_evals = 0;
5925 char target_evals = 0;
5926
5927 if (!b)
5928 return NULL;
5929
5930 if (!is_breakpoint (b))
5931 return NULL;
5932
5933 if (gdb_evaluates_breakpoint_condition_p ()
5934 || !target_supports_evaluation_of_breakpoint_conditions ())
5935 return condition_evaluation_host;
5936
5937 for (bl = b->loc; bl; bl = bl->next)
5938 {
5939 if (bl->cond_bytecode)
5940 target_evals++;
5941 else
5942 host_evals++;
5943 }
5944
5945 if (host_evals && target_evals)
5946 return condition_evaluation_both;
5947 else if (target_evals)
5948 return condition_evaluation_target;
5949 else
5950 return condition_evaluation_host;
5951}
5952
5953/* Determine the breakpoint location's condition evaluator. This is
5954 similar to bp_condition_evaluator, but for locations. */
5955
5956static const char *
5957bp_location_condition_evaluator (struct bp_location *bl)
5958{
5959 if (bl && !is_breakpoint (bl->owner))
5960 return NULL;
5961
5962 if (gdb_evaluates_breakpoint_condition_p ()
5963 || !target_supports_evaluation_of_breakpoint_conditions ())
5964 return condition_evaluation_host;
5965
5966 if (bl && bl->cond_bytecode)
5967 return condition_evaluation_target;
5968 else
5969 return condition_evaluation_host;
5970}
5971
859825b8
JK
5972/* Print the LOC location out of the list of B->LOC locations. */
5973
170b53b2
UW
5974static void
5975print_breakpoint_location (struct breakpoint *b,
5976 struct bp_location *loc)
0d381245 5977{
79a45e25 5978 struct ui_out *uiout = current_uiout;
5ed8105e
PA
5979
5980 scoped_restore_current_program_space restore_pspace;
6c95b8df 5981
859825b8
JK
5982 if (loc != NULL && loc->shlib_disabled)
5983 loc = NULL;
5984
6c95b8df
PA
5985 if (loc != NULL)
5986 set_current_program_space (loc->pspace);
5987
56435ebe 5988 if (b->display_canonical)
d28cd78a 5989 uiout->field_string ("what", event_location_to_string (b->location.get ()));
2f202fde 5990 else if (loc && loc->symtab)
0d381245 5991 {
4a27f119
KS
5992 const struct symbol *sym = loc->symbol;
5993
0d381245
VP
5994 if (sym)
5995 {
112e8700 5996 uiout->text ("in ");
987012b8 5997 uiout->field_string ("func", sym->print_name (),
e43b10e1 5998 function_name_style.style ());
112e8700
SM
5999 uiout->text (" ");
6000 uiout->wrap_hint (wrap_indent_at_field (uiout, "what"));
6001 uiout->text ("at ");
0d381245 6002 }
112e8700 6003 uiout->field_string ("file",
cbe56571 6004 symtab_to_filename_for_display (loc->symtab),
e43b10e1 6005 file_name_style.style ());
112e8700 6006 uiout->text (":");
05cba821 6007
112e8700
SM
6008 if (uiout->is_mi_like_p ())
6009 uiout->field_string ("fullname", symtab_to_fullname (loc->symtab));
0d381245 6010
381befee 6011 uiout->field_signed ("line", loc->line_number);
0d381245 6012 }
859825b8 6013 else if (loc)
0d381245 6014 {
d7e74731 6015 string_file stb;
170b53b2 6016
d7e74731 6017 print_address_symbolic (loc->gdbarch, loc->address, &stb,
22e722e1 6018 demangle, "");
112e8700 6019 uiout->field_stream ("at", stb);
0d381245 6020 }
859825b8 6021 else
f00aae0f 6022 {
d28cd78a
TT
6023 uiout->field_string ("pending",
6024 event_location_to_string (b->location.get ()));
f00aae0f
KS
6025 /* If extra_string is available, it could be holding a condition
6026 or dprintf arguments. In either case, make sure it is printed,
6027 too, but only for non-MI streams. */
112e8700 6028 if (!uiout->is_mi_like_p () && b->extra_string != NULL)
f00aae0f
KS
6029 {
6030 if (b->type == bp_dprintf)
112e8700 6031 uiout->text (",");
f00aae0f 6032 else
112e8700
SM
6033 uiout->text (" ");
6034 uiout->text (b->extra_string);
f00aae0f
KS
6035 }
6036 }
6c95b8df 6037
b775012e
LM
6038 if (loc && is_breakpoint (b)
6039 && breakpoint_condition_evaluation_mode () == condition_evaluation_target
6040 && bp_condition_evaluator (b) == condition_evaluation_both)
6041 {
112e8700
SM
6042 uiout->text (" (");
6043 uiout->field_string ("evaluated-by",
b775012e 6044 bp_location_condition_evaluator (loc));
112e8700 6045 uiout->text (")");
b775012e 6046 }
0d381245
VP
6047}
6048
269b11a2
PA
6049static const char *
6050bptype_string (enum bptype type)
c906108c 6051{
c4093a6a
JM
6052 struct ep_type_description
6053 {
6054 enum bptype type;
a121b7c1 6055 const char *description;
c4093a6a
JM
6056 };
6057 static struct ep_type_description bptypes[] =
c906108c 6058 {
c5aa993b
JM
6059 {bp_none, "?deleted?"},
6060 {bp_breakpoint, "breakpoint"},
c906108c 6061 {bp_hardware_breakpoint, "hw breakpoint"},
7c16b83e 6062 {bp_single_step, "sw single-step"},
c5aa993b
JM
6063 {bp_until, "until"},
6064 {bp_finish, "finish"},
6065 {bp_watchpoint, "watchpoint"},
c906108c 6066 {bp_hardware_watchpoint, "hw watchpoint"},
c5aa993b
JM
6067 {bp_read_watchpoint, "read watchpoint"},
6068 {bp_access_watchpoint, "acc watchpoint"},
6069 {bp_longjmp, "longjmp"},
6070 {bp_longjmp_resume, "longjmp resume"},
e2e4d78b 6071 {bp_longjmp_call_dummy, "longjmp for call dummy"},
186c406b
TT
6072 {bp_exception, "exception"},
6073 {bp_exception_resume, "exception resume"},
c5aa993b 6074 {bp_step_resume, "step resume"},
2c03e5be 6075 {bp_hp_step_resume, "high-priority step resume"},
c5aa993b
JM
6076 {bp_watchpoint_scope, "watchpoint scope"},
6077 {bp_call_dummy, "call dummy"},
aa7d318d 6078 {bp_std_terminate, "std::terminate"},
c5aa993b 6079 {bp_shlib_event, "shlib events"},
c4093a6a 6080 {bp_thread_event, "thread events"},
1900040c 6081 {bp_overlay_event, "overlay events"},
0fd8e87f 6082 {bp_longjmp_master, "longjmp master"},
aa7d318d 6083 {bp_std_terminate_master, "std::terminate master"},
186c406b 6084 {bp_exception_master, "exception master"},
ce78b96d 6085 {bp_catchpoint, "catchpoint"},
1042e4c0 6086 {bp_tracepoint, "tracepoint"},
7a697b8d 6087 {bp_fast_tracepoint, "fast tracepoint"},
0fb4aa4b 6088 {bp_static_tracepoint, "static tracepoint"},
e7e0cddf 6089 {bp_dprintf, "dprintf"},
4efc6507 6090 {bp_jit_event, "jit events"},
0e30163f
JK
6091 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
6092 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
c5aa993b 6093 };
269b11a2
PA
6094
6095 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
6096 || ((int) type != bptypes[(int) type].type))
6097 internal_error (__FILE__, __LINE__,
6098 _("bptypes table does not describe type #%d."),
6099 (int) type);
6100
6101 return bptypes[(int) type].description;
6102}
6103
998580f1
MK
6104/* For MI, output a field named 'thread-groups' with a list as the value.
6105 For CLI, prefix the list with the string 'inf'. */
6106
6107static void
6108output_thread_groups (struct ui_out *uiout,
6109 const char *field_name,
5c632425 6110 const std::vector<int> &inf_nums,
998580f1
MK
6111 int mi_only)
6112{
112e8700 6113 int is_mi = uiout->is_mi_like_p ();
998580f1
MK
6114
6115 /* For backward compatibility, don't display inferiors in CLI unless
6116 there are several. Always display them for MI. */
6117 if (!is_mi && mi_only)
6118 return;
6119
10f489e5 6120 ui_out_emit_list list_emitter (uiout, field_name);
752eb8b4 6121
5c632425 6122 for (size_t i = 0; i < inf_nums.size (); i++)
998580f1
MK
6123 {
6124 if (is_mi)
6125 {
6126 char mi_group[10];
6127
5c632425 6128 xsnprintf (mi_group, sizeof (mi_group), "i%d", inf_nums[i]);
112e8700 6129 uiout->field_string (NULL, mi_group);
998580f1
MK
6130 }
6131 else
6132 {
6133 if (i == 0)
112e8700 6134 uiout->text (" inf ");
998580f1 6135 else
112e8700 6136 uiout->text (", ");
998580f1 6137
5c632425 6138 uiout->text (plongest (inf_nums[i]));
998580f1
MK
6139 }
6140 }
998580f1
MK
6141}
6142
a38118e5
PA
6143/* Print B to gdb_stdout. If RAW_LOC, print raw breakpoint locations
6144 instead of going via breakpoint_ops::print_one. This makes "maint
6145 info breakpoints" show the software breakpoint locations of
6146 catchpoints, which are considered internal implementation
6147 detail. */
269b11a2
PA
6148
6149static void
6150print_one_breakpoint_location (struct breakpoint *b,
6151 struct bp_location *loc,
6152 int loc_number,
6153 struct bp_location **last_loc,
a38118e5 6154 int allflag, bool raw_loc)
269b11a2
PA
6155{
6156 struct command_line *l;
c2c6d25f 6157 static char bpenables[] = "nynny";
c906108c 6158
79a45e25 6159 struct ui_out *uiout = current_uiout;
0d381245
VP
6160 int header_of_multiple = 0;
6161 int part_of_multiple = (loc != NULL);
79a45b7d
TT
6162 struct value_print_options opts;
6163
6164 get_user_print_options (&opts);
0d381245
VP
6165
6166 gdb_assert (!loc || loc_number != 0);
4a64f543
MS
6167 /* See comment in print_one_breakpoint concerning treatment of
6168 breakpoints with single disabled location. */
0d381245
VP
6169 if (loc == NULL
6170 && (b->loc != NULL
b5fa468f
TBA
6171 && (b->loc->next != NULL
6172 || !b->loc->enabled || b->loc->disabled_by_cond)))
0d381245
VP
6173 header_of_multiple = 1;
6174 if (loc == NULL)
6175 loc = b->loc;
6176
c4093a6a
JM
6177 annotate_record ();
6178
6179 /* 1 */
6180 annotate_field (0);
0d381245 6181 if (part_of_multiple)
528e1572 6182 uiout->field_fmt ("number", "%d.%d", b->number, loc_number);
0d381245 6183 else
381befee 6184 uiout->field_signed ("number", b->number);
c4093a6a
JM
6185
6186 /* 2 */
6187 annotate_field (1);
0d381245 6188 if (part_of_multiple)
112e8700 6189 uiout->field_skip ("type");
269b11a2 6190 else
112e8700 6191 uiout->field_string ("type", bptype_string (b->type));
c4093a6a
JM
6192
6193 /* 3 */
6194 annotate_field (2);
0d381245 6195 if (part_of_multiple)
112e8700 6196 uiout->field_skip ("disp");
0d381245 6197 else
112e8700 6198 uiout->field_string ("disp", bpdisp_text (b->disposition));
0d381245 6199
c4093a6a
JM
6200 /* 4 */
6201 annotate_field (3);
85c88e2a
TBA
6202 /* For locations that are disabled because of an invalid condition,
6203 display "N*" on CLI, where "*" refers to a footnote below the
6204 table. For MI, simply display a "N" without a footnote. */
6205 const char *N = (uiout->is_mi_like_p ()) ? "N" : "N*";
0d381245 6206 if (part_of_multiple)
85c88e2a 6207 uiout->field_string ("enabled", (loc->disabled_by_cond ? N
b5fa468f 6208 : (loc->enabled ? "y" : "n")));
0d381245 6209 else
112e8700 6210 uiout->field_fmt ("enabled", "%c", bpenables[(int) b->enable_state]);
0d381245 6211
c4093a6a 6212 /* 5 and 6 */
a38118e5 6213 if (!raw_loc && b->ops != NULL && b->ops->print_one != NULL)
b58a68fe 6214 b->ops->print_one (b, last_loc);
3086aeae 6215 else
a38118e5
PA
6216 {
6217 if (is_watchpoint (b))
3a5c3e22
PA
6218 {
6219 struct watchpoint *w = (struct watchpoint *) b;
6220
6221 /* Field 4, the address, is omitted (which makes the columns
6222 not line up too nicely with the headers, but the effect
6223 is relatively readable). */
6224 if (opts.addressprint)
112e8700 6225 uiout->field_skip ("addr");
3a5c3e22 6226 annotate_field (5);
112e8700 6227 uiout->field_string ("what", w->exp_string);
3a5c3e22 6228 }
f06f1252
TT
6229 else if (!is_catchpoint (b) || is_exception_catchpoint (b)
6230 || is_ada_exception_catchpoint (b))
a38118e5
PA
6231 {
6232 if (opts.addressprint)
6233 {
6234 annotate_field (4);
6235 if (header_of_multiple)
7f6aba03
TT
6236 uiout->field_string ("addr", "<MULTIPLE>",
6237 metadata_style.style ());
a38118e5 6238 else if (b->loc == NULL || loc->shlib_disabled)
7f6aba03
TT
6239 uiout->field_string ("addr", "<PENDING>",
6240 metadata_style.style ());
a38118e5
PA
6241 else
6242 uiout->field_core_addr ("addr",
6243 loc->gdbarch, loc->address);
6244 }
6245 annotate_field (5);
6246 if (!header_of_multiple)
6247 print_breakpoint_location (b, loc);
6248 if (b->loc)
6249 *last_loc = b->loc;
6250 }
6251 }
6c95b8df 6252
998580f1 6253 if (loc != NULL && !header_of_multiple)
6c95b8df 6254 {
5c632425 6255 std::vector<int> inf_nums;
998580f1 6256 int mi_only = 1;
6c95b8df 6257
08036331 6258 for (inferior *inf : all_inferiors ())
6c95b8df
PA
6259 {
6260 if (inf->pspace == loc->pspace)
5c632425 6261 inf_nums.push_back (inf->num);
6c95b8df 6262 }
998580f1 6263
dda83cd7 6264 /* For backward compatibility, don't display inferiors in CLI unless
998580f1
MK
6265 there are several. Always display for MI. */
6266 if (allflag
6267 || (!gdbarch_has_global_breakpoints (target_gdbarch ())
94c93c35 6268 && (program_spaces.size () > 1
998580f1
MK
6269 || number_of_inferiors () > 1)
6270 /* LOC is for existing B, it cannot be in
6271 moribund_locations and thus having NULL OWNER. */
6272 && loc->owner->type != bp_catchpoint))
6273 mi_only = 0;
5c632425 6274 output_thread_groups (uiout, "thread-groups", inf_nums, mi_only);
6c95b8df
PA
6275 }
6276
4a306c9a 6277 if (!part_of_multiple)
c4093a6a 6278 {
4a306c9a
JB
6279 if (b->thread != -1)
6280 {
6281 /* FIXME: This seems to be redundant and lost here; see the
4a64f543 6282 "stop only in" line a little further down. */
112e8700 6283 uiout->text (" thread ");
381befee 6284 uiout->field_signed ("thread", b->thread);
4a306c9a
JB
6285 }
6286 else if (b->task != 0)
6287 {
112e8700 6288 uiout->text (" task ");
381befee 6289 uiout->field_signed ("task", b->task);
4a306c9a 6290 }
c4093a6a 6291 }
f1310107 6292
112e8700 6293 uiout->text ("\n");
f1310107 6294
348d480f 6295 if (!part_of_multiple)
f1310107
TJB
6296 b->ops->print_one_detail (b, uiout);
6297
0d381245 6298 if (part_of_multiple && frame_id_p (b->frame_id))
c4093a6a
JM
6299 {
6300 annotate_field (6);
112e8700 6301 uiout->text ("\tstop only in stack frame at ");
e5dd4106 6302 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
dda83cd7 6303 the frame ID. */
112e8700 6304 uiout->field_core_addr ("frame",
5af949e3 6305 b->gdbarch, b->frame_id.stack_addr);
112e8700 6306 uiout->text ("\n");
c4093a6a
JM
6307 }
6308
28010a5d 6309 if (!part_of_multiple && b->cond_string)
c4093a6a
JM
6310 {
6311 annotate_field (7);
d77f58be 6312 if (is_tracepoint (b))
112e8700 6313 uiout->text ("\ttrace only if ");
1042e4c0 6314 else
112e8700
SM
6315 uiout->text ("\tstop only if ");
6316 uiout->field_string ("cond", b->cond_string);
b775012e
LM
6317
6318 /* Print whether the target is doing the breakpoint's condition
6319 evaluation. If GDB is doing the evaluation, don't print anything. */
6320 if (is_breakpoint (b)
6321 && breakpoint_condition_evaluation_mode ()
6322 == condition_evaluation_target)
6323 {
6a831f06
PA
6324 uiout->message (" (%pF evals)",
6325 string_field ("evaluated-by",
6326 bp_condition_evaluator (b)));
b775012e 6327 }
112e8700 6328 uiout->text ("\n");
0101ce28
JJ
6329 }
6330
0d381245 6331 if (!part_of_multiple && b->thread != -1)
c4093a6a 6332 {
4a64f543 6333 /* FIXME should make an annotation for this. */
112e8700
SM
6334 uiout->text ("\tstop only in thread ");
6335 if (uiout->is_mi_like_p ())
381befee 6336 uiout->field_signed ("thread", b->thread);
5d5658a1
PA
6337 else
6338 {
6339 struct thread_info *thr = find_thread_global_id (b->thread);
6340
112e8700 6341 uiout->field_string ("thread", print_thread_id (thr));
5d5658a1 6342 }
112e8700 6343 uiout->text ("\n");
c4093a6a
JM
6344 }
6345
556ec64d
YQ
6346 if (!part_of_multiple)
6347 {
6348 if (b->hit_count)
31f56a27
YQ
6349 {
6350 /* FIXME should make an annotation for this. */
6351 if (is_catchpoint (b))
112e8700 6352 uiout->text ("\tcatchpoint");
31f56a27 6353 else if (is_tracepoint (b))
112e8700 6354 uiout->text ("\ttracepoint");
31f56a27 6355 else
112e8700
SM
6356 uiout->text ("\tbreakpoint");
6357 uiout->text (" already hit ");
381befee 6358 uiout->field_signed ("times", b->hit_count);
31f56a27 6359 if (b->hit_count == 1)
112e8700 6360 uiout->text (" time\n");
31f56a27 6361 else
112e8700 6362 uiout->text (" times\n");
31f56a27 6363 }
556ec64d
YQ
6364 else
6365 {
31f56a27 6366 /* Output the count also if it is zero, but only if this is mi. */
112e8700 6367 if (uiout->is_mi_like_p ())
381befee 6368 uiout->field_signed ("times", b->hit_count);
556ec64d
YQ
6369 }
6370 }
8b93c638 6371
0d381245 6372 if (!part_of_multiple && b->ignore_count)
c4093a6a
JM
6373 {
6374 annotate_field (8);
6a831f06
PA
6375 uiout->message ("\tignore next %pF hits\n",
6376 signed_field ("ignore", b->ignore_count));
c4093a6a 6377 }
059fb39f 6378
816338b5
SS
6379 /* Note that an enable count of 1 corresponds to "enable once"
6380 behavior, which is reported by the combination of enablement and
6381 disposition, so we don't need to mention it here. */
6382 if (!part_of_multiple && b->enable_count > 1)
6383 {
6384 annotate_field (8);
112e8700 6385 uiout->text ("\tdisable after ");
816338b5
SS
6386 /* Tweak the wording to clarify that ignore and enable counts
6387 are distinct, and have additive effect. */
6388 if (b->ignore_count)
112e8700 6389 uiout->text ("additional ");
816338b5 6390 else
112e8700 6391 uiout->text ("next ");
381befee 6392 uiout->field_signed ("enable", b->enable_count);
112e8700 6393 uiout->text (" hits\n");
816338b5
SS
6394 }
6395
f196051f
SS
6396 if (!part_of_multiple && is_tracepoint (b))
6397 {
6398 struct tracepoint *tp = (struct tracepoint *) b;
6399
6400 if (tp->traceframe_usage)
6401 {
112e8700 6402 uiout->text ("\ttrace buffer usage ");
381befee 6403 uiout->field_signed ("traceframe-usage", tp->traceframe_usage);
112e8700 6404 uiout->text (" bytes\n");
f196051f
SS
6405 }
6406 }
d3ce09f5 6407
d1b0a7bf 6408 l = b->commands ? b->commands.get () : NULL;
059fb39f 6409 if (!part_of_multiple && l)
c4093a6a
JM
6410 {
6411 annotate_field (9);
2e783024 6412 ui_out_emit_tuple tuple_emitter (uiout, "script");
8b93c638 6413 print_command_lines (uiout, l, 4);
c4093a6a 6414 }
d24317b4 6415
d9b3f62e 6416 if (is_tracepoint (b))
1042e4c0 6417 {
d9b3f62e
PA
6418 struct tracepoint *t = (struct tracepoint *) b;
6419
6420 if (!part_of_multiple && t->pass_count)
6421 {
6422 annotate_field (10);
112e8700 6423 uiout->text ("\tpass count ");
381befee 6424 uiout->field_signed ("pass", t->pass_count);
112e8700 6425 uiout->text (" \n");
d9b3f62e 6426 }
f2a8bc8a
YQ
6427
6428 /* Don't display it when tracepoint or tracepoint location is
6429 pending. */
6430 if (!header_of_multiple && loc != NULL && !loc->shlib_disabled)
6431 {
6432 annotate_field (11);
6433
112e8700
SM
6434 if (uiout->is_mi_like_p ())
6435 uiout->field_string ("installed",
f2a8bc8a
YQ
6436 loc->inserted ? "y" : "n");
6437 else
6438 {
6439 if (loc->inserted)
112e8700 6440 uiout->text ("\t");
f2a8bc8a 6441 else
112e8700
SM
6442 uiout->text ("\tnot ");
6443 uiout->text ("installed on target\n");
f2a8bc8a
YQ
6444 }
6445 }
1042e4c0
SS
6446 }
6447
112e8700 6448 if (uiout->is_mi_like_p () && !part_of_multiple)
d24317b4 6449 {
3a5c3e22
PA
6450 if (is_watchpoint (b))
6451 {
6452 struct watchpoint *w = (struct watchpoint *) b;
6453
112e8700 6454 uiout->field_string ("original-location", w->exp_string);
3a5c3e22 6455 }
f00aae0f 6456 else if (b->location != NULL
d28cd78a 6457 && event_location_to_string (b->location.get ()) != NULL)
112e8700 6458 uiout->field_string ("original-location",
d28cd78a 6459 event_location_to_string (b->location.get ()));
d24317b4 6460 }
c4093a6a 6461}
c5aa993b 6462
13674803
SM
6463/* See breakpoint.h. */
6464
6465bool fix_multi_location_breakpoint_output_globally = false;
6466
0d381245
VP
6467static void
6468print_one_breakpoint (struct breakpoint *b,
4a64f543 6469 struct bp_location **last_loc,
6c95b8df 6470 int allflag)
0d381245 6471{
79a45e25 6472 struct ui_out *uiout = current_uiout;
13674803
SM
6473 bool use_fixed_output
6474 = (uiout->test_flags (fix_multi_location_breakpoint_output)
6475 || fix_multi_location_breakpoint_output_globally);
8d3788bd 6476
b4be1b06 6477 gdb::optional<ui_out_emit_tuple> bkpt_tuple_emitter (gdb::in_place, uiout, "bkpt");
a38118e5 6478 print_one_breakpoint_location (b, NULL, 0, last_loc, allflag, false);
8d3788bd 6479
b4be1b06
SM
6480 /* The mi2 broken format: the main breakpoint tuple ends here, the locations
6481 are outside. */
6482 if (!use_fixed_output)
6483 bkpt_tuple_emitter.reset ();
0d381245
VP
6484
6485 /* If this breakpoint has custom print function,
6486 it's already printed. Otherwise, print individual
6487 locations, if any. */
a38118e5
PA
6488 if (b->ops == NULL
6489 || b->ops->print_one == NULL
6490 || allflag)
0d381245 6491 {
4a64f543
MS
6492 /* If breakpoint has a single location that is disabled, we
6493 print it as if it had several locations, since otherwise it's
6494 hard to represent "breakpoint enabled, location disabled"
6495 situation.
6496
6497 Note that while hardware watchpoints have several locations
a38118e5
PA
6498 internally, that's not a property exposed to users.
6499
6500 Likewise, while catchpoints may be implemented with
6501 breakpoints (e.g., catch throw), that's not a property
6502 exposed to users. We do however display the internal
6503 breakpoint locations with "maint info breakpoints". */
6504 if (!is_hardware_watchpoint (b)
f06f1252
TT
6505 && (!is_catchpoint (b) || is_exception_catchpoint (b)
6506 || is_ada_exception_catchpoint (b))
a38118e5 6507 && (allflag
b5fa468f
TBA
6508 || (b->loc && (b->loc->next
6509 || !b->loc->enabled
6510 || b->loc->disabled_by_cond))))
0d381245 6511 {
b4be1b06
SM
6512 gdb::optional<ui_out_emit_list> locations_list;
6513
6514 /* For MI version <= 2, keep the behavior where GDB outputs an invalid
6515 MI record. For later versions, place breakpoint locations in a
6516 list. */
6517 if (uiout->is_mi_like_p () && use_fixed_output)
6518 locations_list.emplace (uiout, "locations");
8d3788bd 6519
b4be1b06
SM
6520 int n = 1;
6521 for (bp_location *loc = b->loc; loc != NULL; loc = loc->next, ++n)
8d3788bd 6522 {
b4be1b06 6523 ui_out_emit_tuple loc_tuple_emitter (uiout, NULL);
a38118e5
PA
6524 print_one_breakpoint_location (b, loc, n, last_loc,
6525 allflag, allflag);
8d3788bd 6526 }
0d381245
VP
6527 }
6528 }
6529}
6530
a6d9a66e
UW
6531static int
6532breakpoint_address_bits (struct breakpoint *b)
6533{
6534 int print_address_bits = 0;
6535 struct bp_location *loc;
6536
c6d81124
PA
6537 /* Software watchpoints that aren't watching memory don't have an
6538 address to print. */
6539 if (is_no_memory_software_watchpoint (b))
6540 return 0;
6541
a6d9a66e
UW
6542 for (loc = b->loc; loc; loc = loc->next)
6543 {
c7437ca6
PA
6544 int addr_bit;
6545
c7437ca6 6546 addr_bit = gdbarch_addr_bit (loc->gdbarch);
a6d9a66e
UW
6547 if (addr_bit > print_address_bits)
6548 print_address_bits = addr_bit;
6549 }
6550
6551 return print_address_bits;
6552}
0d381245 6553
65630365 6554/* See breakpoint.h. */
c5aa993b 6555
65630365
PA
6556void
6557print_breakpoint (breakpoint *b)
c4093a6a 6558{
a6d9a66e 6559 struct bp_location *dummy_loc = NULL;
65630365 6560 print_one_breakpoint (b, &dummy_loc, 0);
c4093a6a 6561}
c5aa993b 6562
09d682a4
TT
6563/* Return true if this breakpoint was set by the user, false if it is
6564 internal or momentary. */
6565
6566int
6567user_breakpoint_p (struct breakpoint *b)
6568{
46c6471b 6569 return b->number > 0;
09d682a4
TT
6570}
6571
93daf339
TT
6572/* See breakpoint.h. */
6573
6574int
6575pending_breakpoint_p (struct breakpoint *b)
6576{
6577 return b->loc == NULL;
6578}
6579
5c458ae8
SM
6580/* Print information on breakpoints (including watchpoints and tracepoints).
6581
6582 If non-NULL, BP_NUM_LIST is a list of numbers and number ranges as
6583 understood by number_or_range_parser. Only breakpoints included in this
6584 list are then printed.
6585
6586 If SHOW_INTERNAL is true, print internal breakpoints.
6587
6588 If FILTER is non-NULL, call it on each breakpoint and only include the
6589 ones for which it returns true.
6590
6591 Return the total number of breakpoints listed. */
c906108c 6592
d77f58be 6593static int
5c458ae8 6594breakpoint_1 (const char *bp_num_list, bool show_internal,
f2478a7e 6595 bool (*filter) (const struct breakpoint *))
c4093a6a 6596{
52f0bd74 6597 struct breakpoint *b;
a6d9a66e 6598 struct bp_location *last_loc = NULL;
7f3b0473 6599 int nr_printable_breakpoints;
79a45b7d 6600 struct value_print_options opts;
a6d9a66e 6601 int print_address_bits = 0;
269b11a2 6602 int print_type_col_width = 14;
79a45e25 6603 struct ui_out *uiout = current_uiout;
b5fa468f 6604 bool has_disabled_by_cond_location = false;
269b11a2 6605
79a45b7d
TT
6606 get_user_print_options (&opts);
6607
4a64f543
MS
6608 /* Compute the number of rows in the table, as well as the size
6609 required for address fields. */
7f3b0473
AC
6610 nr_printable_breakpoints = 0;
6611 ALL_BREAKPOINTS (b)
e5a67952
MS
6612 {
6613 /* If we have a filter, only list the breakpoints it accepts. */
6614 if (filter && !filter (b))
6615 continue;
6616
5c458ae8 6617 /* If we have a BP_NUM_LIST string, it is a list of breakpoints to
e5a67952 6618 accept. Skip the others. */
5c458ae8 6619 if (bp_num_list != NULL && *bp_num_list != '\0')
e5a67952 6620 {
5c458ae8 6621 if (show_internal && parse_and_eval_long (bp_num_list) != b->number)
e5a67952 6622 continue;
5c458ae8 6623 if (!show_internal && !number_is_in_list (bp_num_list, b->number))
e5a67952
MS
6624 continue;
6625 }
269b11a2 6626
5c458ae8 6627 if (show_internal || user_breakpoint_p (b))
e5a67952
MS
6628 {
6629 int addr_bit, type_len;
a6d9a66e 6630
e5a67952
MS
6631 addr_bit = breakpoint_address_bits (b);
6632 if (addr_bit > print_address_bits)
6633 print_address_bits = addr_bit;
269b11a2 6634
e5a67952
MS
6635 type_len = strlen (bptype_string (b->type));
6636 if (type_len > print_type_col_width)
6637 print_type_col_width = type_len;
6638
6639 nr_printable_breakpoints++;
6640 }
6641 }
7f3b0473 6642
4a2b031d
TT
6643 {
6644 ui_out_emit_table table_emitter (uiout,
6645 opts.addressprint ? 6 : 5,
6646 nr_printable_breakpoints,
6647 "BreakpointTable");
6648
6649 if (nr_printable_breakpoints > 0)
6650 annotate_breakpoints_headers ();
6651 if (nr_printable_breakpoints > 0)
6652 annotate_field (0);
6653 uiout->table_header (7, ui_left, "number", "Num"); /* 1 */
6654 if (nr_printable_breakpoints > 0)
6655 annotate_field (1);
6656 uiout->table_header (print_type_col_width, ui_left, "type", "Type"); /* 2 */
6657 if (nr_printable_breakpoints > 0)
6658 annotate_field (2);
6659 uiout->table_header (4, ui_left, "disp", "Disp"); /* 3 */
6660 if (nr_printable_breakpoints > 0)
6661 annotate_field (3);
6662 uiout->table_header (3, ui_left, "enabled", "Enb"); /* 4 */
6663 if (opts.addressprint)
6664 {
6665 if (nr_printable_breakpoints > 0)
6666 annotate_field (4);
6667 if (print_address_bits <= 32)
6668 uiout->table_header (10, ui_left, "addr", "Address"); /* 5 */
6669 else
6670 uiout->table_header (18, ui_left, "addr", "Address"); /* 5 */
6671 }
6672 if (nr_printable_breakpoints > 0)
6673 annotate_field (5);
6674 uiout->table_header (40, ui_noalign, "what", "What"); /* 6 */
6675 uiout->table_body ();
6676 if (nr_printable_breakpoints > 0)
6677 annotate_breakpoints_table ();
6678
6679 ALL_BREAKPOINTS (b)
6680 {
6681 QUIT;
6682 /* If we have a filter, only list the breakpoints it accepts. */
6683 if (filter && !filter (b))
6684 continue;
e5a67952 6685
5c458ae8 6686 /* If we have a BP_NUM_LIST string, it is a list of breakpoints to
4a2b031d 6687 accept. Skip the others. */
e5a67952 6688
5c458ae8 6689 if (bp_num_list != NULL && *bp_num_list != '\0')
4a2b031d 6690 {
5c458ae8 6691 if (show_internal) /* maintenance info breakpoint */
4a2b031d 6692 {
5c458ae8 6693 if (parse_and_eval_long (bp_num_list) != b->number)
4a2b031d
TT
6694 continue;
6695 }
6696 else /* all others */
6697 {
5c458ae8 6698 if (!number_is_in_list (bp_num_list, b->number))
4a2b031d
TT
6699 continue;
6700 }
6701 }
6702 /* We only print out user settable breakpoints unless the
5c458ae8
SM
6703 show_internal is set. */
6704 if (show_internal || user_breakpoint_p (b))
b5fa468f
TBA
6705 {
6706 print_one_breakpoint (b, &last_loc, show_internal);
6707 for (bp_location *loc = b->loc; loc != NULL; loc = loc->next)
6708 if (loc->disabled_by_cond)
6709 has_disabled_by_cond_location = true;
6710 }
4a2b031d
TT
6711 }
6712 }
698384cd 6713
7f3b0473 6714 if (nr_printable_breakpoints == 0)
c906108c 6715 {
4a64f543
MS
6716 /* If there's a filter, let the caller decide how to report
6717 empty list. */
d77f58be
SS
6718 if (!filter)
6719 {
5c458ae8 6720 if (bp_num_list == NULL || *bp_num_list == '\0')
112e8700 6721 uiout->message ("No breakpoints or watchpoints.\n");
d77f58be 6722 else
112e8700 6723 uiout->message ("No breakpoint or watchpoint matching '%s'.\n",
5c458ae8 6724 bp_num_list);
d77f58be 6725 }
c906108c
SS
6726 }
6727 else
c4093a6a 6728 {
a6d9a66e
UW
6729 if (last_loc && !server_command)
6730 set_next_address (last_loc->gdbarch, last_loc->address);
b5fa468f 6731
85c88e2a 6732 if (has_disabled_by_cond_location && !uiout->is_mi_like_p ())
b5fa468f
TBA
6733 uiout->message (_("(*): Breakpoint condition is invalid at this "
6734 "location.\n"));
c4093a6a 6735 }
c906108c 6736
4a64f543 6737 /* FIXME? Should this be moved up so that it is only called when
c4093a6a 6738 there have been breakpoints? */
c906108c 6739 annotate_breakpoints_table_end ();
d77f58be
SS
6740
6741 return nr_printable_breakpoints;
c906108c
SS
6742}
6743
ad443146
SS
6744/* Display the value of default-collect in a way that is generally
6745 compatible with the breakpoint list. */
6746
6747static void
6748default_collect_info (void)
6749{
79a45e25
PA
6750 struct ui_out *uiout = current_uiout;
6751
ad443146
SS
6752 /* If it has no value (which is frequently the case), say nothing; a
6753 message like "No default-collect." gets in user's face when it's
6754 not wanted. */
6755 if (!*default_collect)
6756 return;
6757
6758 /* The following phrase lines up nicely with per-tracepoint collect
6759 actions. */
112e8700
SM
6760 uiout->text ("default collect ");
6761 uiout->field_string ("default-collect", default_collect);
6762 uiout->text (" \n");
ad443146
SS
6763}
6764
c906108c 6765static void
0b39b52e 6766info_breakpoints_command (const char *args, int from_tty)
c906108c 6767{
5c458ae8 6768 breakpoint_1 (args, false, NULL);
ad443146
SS
6769
6770 default_collect_info ();
d77f58be
SS
6771}
6772
6773static void
1d12d88f 6774info_watchpoints_command (const char *args, int from_tty)
d77f58be 6775{
5c458ae8 6776 int num_printed = breakpoint_1 (args, false, is_watchpoint);
79a45e25 6777 struct ui_out *uiout = current_uiout;
d77f58be
SS
6778
6779 if (num_printed == 0)
6780 {
e5a67952 6781 if (args == NULL || *args == '\0')
112e8700 6782 uiout->message ("No watchpoints.\n");
d77f58be 6783 else
112e8700 6784 uiout->message ("No watchpoint matching '%s'.\n", args);
d77f58be 6785 }
c906108c
SS
6786}
6787
7a292a7a 6788static void
4495129a 6789maintenance_info_breakpoints (const char *args, int from_tty)
c906108c 6790{
5c458ae8 6791 breakpoint_1 (args, true, NULL);
ad443146
SS
6792
6793 default_collect_info ();
c906108c
SS
6794}
6795
0d381245 6796static int
714835d5 6797breakpoint_has_pc (struct breakpoint *b,
6c95b8df 6798 struct program_space *pspace,
714835d5 6799 CORE_ADDR pc, struct obj_section *section)
0d381245
VP
6800{
6801 struct bp_location *bl = b->loc;
cc59ec59 6802
0d381245
VP
6803 for (; bl; bl = bl->next)
6804 {
6c95b8df
PA
6805 if (bl->pspace == pspace
6806 && bl->address == pc
0d381245
VP
6807 && (!overlay_debugging || bl->section == section))
6808 return 1;
6809 }
6810 return 0;
6811}
6812
672f9b60 6813/* Print a message describing any user-breakpoints set at PC. This
6c95b8df
PA
6814 concerns with logical breakpoints, so we match program spaces, not
6815 address spaces. */
c906108c
SS
6816
6817static void
6c95b8df
PA
6818describe_other_breakpoints (struct gdbarch *gdbarch,
6819 struct program_space *pspace, CORE_ADDR pc,
5af949e3 6820 struct obj_section *section, int thread)
c906108c 6821{
52f0bd74
AC
6822 int others = 0;
6823 struct breakpoint *b;
c906108c
SS
6824
6825 ALL_BREAKPOINTS (b)
672f9b60 6826 others += (user_breakpoint_p (b)
dda83cd7 6827 && breakpoint_has_pc (b, pspace, pc, section));
c906108c
SS
6828 if (others > 0)
6829 {
a3f17187
AC
6830 if (others == 1)
6831 printf_filtered (_("Note: breakpoint "));
6832 else /* if (others == ???) */
6833 printf_filtered (_("Note: breakpoints "));
c906108c 6834 ALL_BREAKPOINTS (b)
672f9b60 6835 if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
0d381245
VP
6836 {
6837 others--;
6838 printf_filtered ("%d", b->number);
6839 if (b->thread == -1 && thread != -1)
6840 printf_filtered (" (all threads)");
6841 else if (b->thread != -1)
6842 printf_filtered (" (thread %d)", b->thread);
6843 printf_filtered ("%s%s ",
059fb39f 6844 ((b->enable_state == bp_disabled
f8eba3c6 6845 || b->enable_state == bp_call_disabled)
0d381245 6846 ? " (disabled)"
0d381245
VP
6847 : ""),
6848 (others > 1) ? ","
6849 : ((others == 1) ? " and" : ""));
6850 }
6a831f06
PA
6851 current_uiout->message (_("also set at pc %ps.\n"),
6852 styled_string (address_style.style (),
6853 paddress (gdbarch, pc)));
c906108c
SS
6854 }
6855}
6856\f
c906108c 6857
cb1e4e32
PA
6858/* Return true iff it is meaningful to use the address member of LOC.
6859 For some breakpoint types, the locations' address members are
6860 irrelevant and it makes no sense to attempt to compare them to
6861 other addresses (or use them for any other purpose either).
2d134ed3 6862
cb1e4e32
PA
6863 More specifically, software watchpoints and catchpoints that are
6864 not backed by breakpoints always have a zero valued location
6865 address and we don't want to mark breakpoints of any of these types
6866 to be a duplicate of an actual breakpoint location at address
6867 zero. */
e4f237da 6868
cb1e4e32
PA
6869static bool
6870bl_address_is_meaningful (bp_location *loc)
e4f237da 6871{
cb1e4e32 6872 return loc->loc_type != bp_loc_other;
2d134ed3
PA
6873}
6874
6875/* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
6876 true if LOC1 and LOC2 represent the same watchpoint location. */
6877
6878static int
4a64f543
MS
6879watchpoint_locations_match (struct bp_location *loc1,
6880 struct bp_location *loc2)
2d134ed3 6881{
3a5c3e22
PA
6882 struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
6883 struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
6884
6885 /* Both of them must exist. */
6886 gdb_assert (w1 != NULL);
6887 gdb_assert (w2 != NULL);
2bdf28a0 6888
4a64f543
MS
6889 /* If the target can evaluate the condition expression in hardware,
6890 then we we need to insert both watchpoints even if they are at
6891 the same place. Otherwise the watchpoint will only trigger when
6892 the condition of whichever watchpoint was inserted evaluates to
6893 true, not giving a chance for GDB to check the condition of the
6894 other watchpoint. */
3a5c3e22 6895 if ((w1->cond_exp
4a64f543
MS
6896 && target_can_accel_watchpoint_condition (loc1->address,
6897 loc1->length,
0cf6dd15 6898 loc1->watchpoint_type,
4d01a485 6899 w1->cond_exp.get ()))
3a5c3e22 6900 || (w2->cond_exp
4a64f543
MS
6901 && target_can_accel_watchpoint_condition (loc2->address,
6902 loc2->length,
0cf6dd15 6903 loc2->watchpoint_type,
4d01a485 6904 w2->cond_exp.get ())))
0cf6dd15
TJB
6905 return 0;
6906
85d721b8
PA
6907 /* Note that this checks the owner's type, not the location's. In
6908 case the target does not support read watchpoints, but does
6909 support access watchpoints, we'll have bp_read_watchpoint
6910 watchpoints with hw_access locations. Those should be considered
6911 duplicates of hw_read locations. The hw_read locations will
6912 become hw_access locations later. */
2d134ed3
PA
6913 return (loc1->owner->type == loc2->owner->type
6914 && loc1->pspace->aspace == loc2->pspace->aspace
6915 && loc1->address == loc2->address
6916 && loc1->length == loc2->length);
e4f237da
KB
6917}
6918
31e77af2 6919/* See breakpoint.h. */
6c95b8df 6920
31e77af2 6921int
accd0bcd
YQ
6922breakpoint_address_match (const address_space *aspace1, CORE_ADDR addr1,
6923 const address_space *aspace2, CORE_ADDR addr2)
6c95b8df 6924{
f5656ead 6925 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
6c95b8df
PA
6926 || aspace1 == aspace2)
6927 && addr1 == addr2);
6928}
6929
f1310107
TJB
6930/* Returns true if {ASPACE2,ADDR2} falls within the range determined by
6931 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
6932 matches ASPACE2. On targets that have global breakpoints, the address
6933 space doesn't really matter. */
6934
6935static int
accd0bcd
YQ
6936breakpoint_address_match_range (const address_space *aspace1,
6937 CORE_ADDR addr1,
6938 int len1, const address_space *aspace2,
f1310107
TJB
6939 CORE_ADDR addr2)
6940{
f5656ead 6941 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
f1310107
TJB
6942 || aspace1 == aspace2)
6943 && addr2 >= addr1 && addr2 < addr1 + len1);
6944}
6945
6946/* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
6947 a ranged breakpoint. In most targets, a match happens only if ASPACE
6948 matches the breakpoint's address space. On targets that have global
6949 breakpoints, the address space doesn't really matter. */
6950
6951static int
6952breakpoint_location_address_match (struct bp_location *bl,
accd0bcd 6953 const address_space *aspace,
f1310107
TJB
6954 CORE_ADDR addr)
6955{
6956 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
6957 aspace, addr)
6958 || (bl->length
6959 && breakpoint_address_match_range (bl->pspace->aspace,
6960 bl->address, bl->length,
6961 aspace, addr)));
6962}
6963
d35ae833
PA
6964/* Returns true if the [ADDR,ADDR+LEN) range in ASPACE overlaps
6965 breakpoint BL. BL may be a ranged breakpoint. In most targets, a
6966 match happens only if ASPACE matches the breakpoint's address
6967 space. On targets that have global breakpoints, the address space
6968 doesn't really matter. */
6969
6970static int
6971breakpoint_location_address_range_overlap (struct bp_location *bl,
accd0bcd 6972 const address_space *aspace,
d35ae833
PA
6973 CORE_ADDR addr, int len)
6974{
6975 if (gdbarch_has_global_breakpoints (target_gdbarch ())
6976 || bl->pspace->aspace == aspace)
6977 {
6978 int bl_len = bl->length != 0 ? bl->length : 1;
6979
6980 if (mem_ranges_overlap (addr, len, bl->address, bl_len))
6981 return 1;
6982 }
6983 return 0;
6984}
6985
1e4d1764
YQ
6986/* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
6987 Then, if LOC1 and LOC2 represent the same tracepoint location, returns
6988 true, otherwise returns false. */
6989
6990static int
6991tracepoint_locations_match (struct bp_location *loc1,
6992 struct bp_location *loc2)
6993{
6994 if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
6995 /* Since tracepoint locations are never duplicated with others', tracepoint
6996 locations at the same address of different tracepoints are regarded as
6997 different locations. */
6998 return (loc1->address == loc2->address && loc1->owner == loc2->owner);
6999 else
7000 return 0;
7001}
7002
2d134ed3 7003/* Assuming LOC1 and LOC2's types' have meaningful target addresses
cb1e4e32 7004 (bl_address_is_meaningful), returns true if LOC1 and LOC2 represent
7f32a4d5
PA
7005 the same location. If SW_HW_BPS_MATCH is true, then software
7006 breakpoint locations and hardware breakpoint locations match,
7007 otherwise they don't. */
2d134ed3
PA
7008
7009static int
7f32a4d5
PA
7010breakpoint_locations_match (struct bp_location *loc1,
7011 struct bp_location *loc2,
7012 bool sw_hw_bps_match)
2d134ed3 7013{
2bdf28a0
JK
7014 int hw_point1, hw_point2;
7015
7016 /* Both of them must not be in moribund_locations. */
7017 gdb_assert (loc1->owner != NULL);
7018 gdb_assert (loc2->owner != NULL);
7019
7020 hw_point1 = is_hardware_watchpoint (loc1->owner);
7021 hw_point2 = is_hardware_watchpoint (loc2->owner);
2d134ed3
PA
7022
7023 if (hw_point1 != hw_point2)
7024 return 0;
7025 else if (hw_point1)
7026 return watchpoint_locations_match (loc1, loc2);
1e4d1764
YQ
7027 else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
7028 return tracepoint_locations_match (loc1, loc2);
2d134ed3 7029 else
7f32a4d5
PA
7030 /* We compare bp_location.length in order to cover ranged
7031 breakpoints. Keep this in sync with
7032 bp_location_is_less_than. */
f1310107
TJB
7033 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
7034 loc2->pspace->aspace, loc2->address)
7f32a4d5 7035 && (loc1->loc_type == loc2->loc_type || sw_hw_bps_match)
f1310107 7036 && loc1->length == loc2->length);
2d134ed3
PA
7037}
7038
76897487
KB
7039static void
7040breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
dda83cd7 7041 int bnum, int have_bnum)
76897487 7042{
f63fbe86
MS
7043 /* The longest string possibly returned by hex_string_custom
7044 is 50 chars. These must be at least that big for safety. */
7045 char astr1[64];
7046 char astr2[64];
76897487 7047
bb599908
PH
7048 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
7049 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
76897487 7050 if (have_bnum)
8a3fe4f8 7051 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
dda83cd7 7052 bnum, astr1, astr2);
76897487 7053 else
8a3fe4f8 7054 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
76897487
KB
7055}
7056
4a64f543
MS
7057/* Adjust a breakpoint's address to account for architectural
7058 constraints on breakpoint placement. Return the adjusted address.
7059 Note: Very few targets require this kind of adjustment. For most
7060 targets, this function is simply the identity function. */
76897487
KB
7061
7062static CORE_ADDR
a6d9a66e
UW
7063adjust_breakpoint_address (struct gdbarch *gdbarch,
7064 CORE_ADDR bpaddr, enum bptype bptype)
76897487 7065{
a0de8c21
YQ
7066 if (bptype == bp_watchpoint
7067 || bptype == bp_hardware_watchpoint
7068 || bptype == bp_read_watchpoint
7069 || bptype == bp_access_watchpoint
7070 || bptype == bp_catchpoint)
88f7da05
KB
7071 {
7072 /* Watchpoints and the various bp_catch_* eventpoints should not
dda83cd7 7073 have their addresses modified. */
88f7da05
KB
7074 return bpaddr;
7075 }
7c16b83e
PA
7076 else if (bptype == bp_single_step)
7077 {
7078 /* Single-step breakpoints should not have their addresses
7079 modified. If there's any architectural constrain that
7080 applies to this address, then it should have already been
7081 taken into account when the breakpoint was created in the
7082 first place. If we didn't do this, stepping through e.g.,
7083 Thumb-2 IT blocks would break. */
7084 return bpaddr;
7085 }
76897487
KB
7086 else
7087 {
a0de8c21
YQ
7088 CORE_ADDR adjusted_bpaddr = bpaddr;
7089
7090 if (gdbarch_adjust_breakpoint_address_p (gdbarch))
7091 {
7092 /* Some targets have architectural constraints on the placement
7093 of breakpoint instructions. Obtain the adjusted address. */
7094 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
7095 }
76897487 7096
a0de8c21 7097 adjusted_bpaddr = address_significant (gdbarch, adjusted_bpaddr);
76897487
KB
7098
7099 /* An adjusted breakpoint address can significantly alter
dda83cd7 7100 a user's expectations. Print a warning if an adjustment
76897487
KB
7101 is required. */
7102 if (adjusted_bpaddr != bpaddr)
7103 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
7104
7105 return adjusted_bpaddr;
7106 }
7107}
7108
cb1e4e32
PA
7109static bp_loc_type
7110bp_location_from_bp_type (bptype type)
7cc221ef 7111{
cb1e4e32 7112 switch (type)
e049a4b5
DJ
7113 {
7114 case bp_breakpoint:
7c16b83e 7115 case bp_single_step:
e049a4b5
DJ
7116 case bp_until:
7117 case bp_finish:
7118 case bp_longjmp:
7119 case bp_longjmp_resume:
e2e4d78b 7120 case bp_longjmp_call_dummy:
186c406b
TT
7121 case bp_exception:
7122 case bp_exception_resume:
e049a4b5 7123 case bp_step_resume:
2c03e5be 7124 case bp_hp_step_resume:
e049a4b5
DJ
7125 case bp_watchpoint_scope:
7126 case bp_call_dummy:
aa7d318d 7127 case bp_std_terminate:
e049a4b5
DJ
7128 case bp_shlib_event:
7129 case bp_thread_event:
7130 case bp_overlay_event:
4efc6507 7131 case bp_jit_event:
0fd8e87f 7132 case bp_longjmp_master:
aa7d318d 7133 case bp_std_terminate_master:
186c406b 7134 case bp_exception_master:
0e30163f
JK
7135 case bp_gnu_ifunc_resolver:
7136 case bp_gnu_ifunc_resolver_return:
e7e0cddf 7137 case bp_dprintf:
cb1e4e32 7138 return bp_loc_software_breakpoint;
e049a4b5 7139 case bp_hardware_breakpoint:
cb1e4e32 7140 return bp_loc_hardware_breakpoint;
e049a4b5
DJ
7141 case bp_hardware_watchpoint:
7142 case bp_read_watchpoint:
7143 case bp_access_watchpoint:
cb1e4e32 7144 return bp_loc_hardware_watchpoint;
e049a4b5 7145 case bp_watchpoint:
ce78b96d 7146 case bp_catchpoint:
15c3d785
PA
7147 case bp_tracepoint:
7148 case bp_fast_tracepoint:
0fb4aa4b 7149 case bp_static_tracepoint:
cb1e4e32 7150 return bp_loc_other;
e049a4b5 7151 default:
e2e0b3e5 7152 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
e049a4b5 7153 }
cb1e4e32
PA
7154}
7155
7156bp_location::bp_location (breakpoint *owner, bp_loc_type type)
7157{
7158 this->owner = owner;
7159 this->cond_bytecode = NULL;
7160 this->shlib_disabled = 0;
7161 this->enabled = 1;
b5fa468f 7162 this->disabled_by_cond = false;
cb1e4e32
PA
7163
7164 this->loc_type = type;
e049a4b5 7165
cb1e4e32
PA
7166 if (this->loc_type == bp_loc_software_breakpoint
7167 || this->loc_type == bp_loc_hardware_breakpoint)
7168 mark_breakpoint_location_modified (this);
7169
b6433ede 7170 incref ();
cb1e4e32
PA
7171}
7172
7173bp_location::bp_location (breakpoint *owner)
7174 : bp_location::bp_location (owner,
7175 bp_location_from_bp_type (owner->type))
7176{
28010a5d
PA
7177}
7178
7179/* Allocate a struct bp_location. */
7180
7181static struct bp_location *
7182allocate_bp_location (struct breakpoint *bpt)
7183{
348d480f
PA
7184 return bpt->ops->allocate_location (bpt);
7185}
7cc221ef 7186
f431efe5
PA
7187/* Decrement reference count. If the reference count reaches 0,
7188 destroy the bp_location. Sets *BLP to NULL. */
7189
7190static void
7191decref_bp_location (struct bp_location **blp)
7192{
b6433ede 7193 bp_location_ref_policy::decref (*blp);
f431efe5
PA
7194 *blp = NULL;
7195}
7196
346774a9 7197/* Add breakpoint B at the end of the global breakpoint chain. */
c906108c 7198
b270e6f9
TT
7199static breakpoint *
7200add_to_breakpoint_chain (std::unique_ptr<breakpoint> &&b)
c906108c 7201{
346774a9 7202 struct breakpoint *b1;
b270e6f9 7203 struct breakpoint *result = b.get ();
c906108c 7204
346774a9
PA
7205 /* Add this breakpoint to the end of the chain so that a list of
7206 breakpoints will come out in order of increasing numbers. */
7207
7208 b1 = breakpoint_chain;
7209 if (b1 == 0)
b270e6f9 7210 breakpoint_chain = b.release ();
346774a9
PA
7211 else
7212 {
7213 while (b1->next)
7214 b1 = b1->next;
b270e6f9 7215 b1->next = b.release ();
346774a9 7216 }
b270e6f9
TT
7217
7218 return result;
346774a9
PA
7219}
7220
7221/* Initializes breakpoint B with type BPTYPE and no locations yet. */
7222
7223static void
7224init_raw_breakpoint_without_location (struct breakpoint *b,
7225 struct gdbarch *gdbarch,
28010a5d 7226 enum bptype bptype,
c0a91b2b 7227 const struct breakpoint_ops *ops)
346774a9 7228{
348d480f
PA
7229 gdb_assert (ops != NULL);
7230
28010a5d 7231 b->ops = ops;
4d28f7a8 7232 b->type = bptype;
a6d9a66e 7233 b->gdbarch = gdbarch;
c906108c
SS
7234 b->language = current_language->la_language;
7235 b->input_radix = input_radix;
d0fb5eae 7236 b->related_breakpoint = b;
346774a9
PA
7237}
7238
7239/* Helper to set_raw_breakpoint below. Creates a breakpoint
7240 that has type BPTYPE and has no locations as yet. */
346774a9
PA
7241
7242static struct breakpoint *
7243set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
348d480f 7244 enum bptype bptype,
c0a91b2b 7245 const struct breakpoint_ops *ops)
346774a9 7246{
3b0871f4 7247 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (bptype);
346774a9 7248
3b0871f4 7249 init_raw_breakpoint_without_location (b.get (), gdbarch, bptype, ops);
b270e6f9 7250 return add_to_breakpoint_chain (std::move (b));
0d381245
VP
7251}
7252
0ba852ab 7253/* Initialize loc->function_name. */
0e30163f 7254
0d381245 7255static void
0ba852ab 7256set_breakpoint_location_function (struct bp_location *loc)
0d381245 7257{
2bdf28a0
JK
7258 gdb_assert (loc->owner != NULL);
7259
0d381245 7260 if (loc->owner->type == bp_breakpoint
1042e4c0 7261 || loc->owner->type == bp_hardware_breakpoint
d77f58be 7262 || is_tracepoint (loc->owner))
0d381245 7263 {
2c02bd72 7264 const char *function_name;
0e30163f 7265
3467ec66 7266 if (loc->msymbol != NULL
f50776aa 7267 && (MSYMBOL_TYPE (loc->msymbol) == mst_text_gnu_ifunc
0ba852ab 7268 || MSYMBOL_TYPE (loc->msymbol) == mst_data_gnu_ifunc))
0e30163f
JK
7269 {
7270 struct breakpoint *b = loc->owner;
7271
c9d95fa3 7272 function_name = loc->msymbol->linkage_name ();
3467ec66
PA
7273
7274 if (b->type == bp_breakpoint && b->loc == loc
7275 && loc->next == NULL && b->related_breakpoint == b)
0e30163f
JK
7276 {
7277 /* Create only the whole new breakpoint of this type but do not
7278 mess more complicated breakpoints with multiple locations. */
7279 b->type = bp_gnu_ifunc_resolver;
6a3a010b 7280 /* Remember the resolver's address for use by the return
dda83cd7 7281 breakpoint. */
3467ec66 7282 loc->related_address = loc->address;
0e30163f
JK
7283 }
7284 }
3467ec66
PA
7285 else
7286 find_pc_partial_function (loc->address, &function_name, NULL, NULL);
0e30163f 7287
2c02bd72
DE
7288 if (function_name)
7289 loc->function_name = xstrdup (function_name);
0d381245
VP
7290 }
7291}
7292
a6d9a66e 7293/* Attempt to determine architecture of location identified by SAL. */
1bfeeb0f 7294struct gdbarch *
a6d9a66e
UW
7295get_sal_arch (struct symtab_and_line sal)
7296{
7297 if (sal.section)
08feed99 7298 return sal.section->objfile->arch ();
a6d9a66e 7299 if (sal.symtab)
08feed99 7300 return SYMTAB_OBJFILE (sal.symtab)->arch ();
a6d9a66e
UW
7301
7302 return NULL;
7303}
7304
346774a9
PA
7305/* Low level routine for partially initializing a breakpoint of type
7306 BPTYPE. The newly created breakpoint's address, section, source
c56053d2 7307 file name, and line number are provided by SAL.
0d381245
VP
7308
7309 It is expected that the caller will complete the initialization of
7310 the newly created breakpoint struct as well as output any status
c56053d2 7311 information regarding the creation of a new breakpoint. */
0d381245 7312
346774a9
PA
7313static void
7314init_raw_breakpoint (struct breakpoint *b, struct gdbarch *gdbarch,
28010a5d 7315 struct symtab_and_line sal, enum bptype bptype,
c0a91b2b 7316 const struct breakpoint_ops *ops)
0d381245 7317{
28010a5d 7318 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
346774a9 7319
3742cc8b 7320 add_location_to_breakpoint (b, &sal);
0d381245 7321
6c95b8df
PA
7322 if (bptype != bp_catchpoint)
7323 gdb_assert (sal.pspace != NULL);
7324
f8eba3c6
TT
7325 /* Store the program space that was used to set the breakpoint,
7326 except for ordinary breakpoints, which are independent of the
7327 program space. */
7328 if (bptype != bp_breakpoint && bptype != bp_hardware_breakpoint)
7329 b->pspace = sal.pspace;
346774a9 7330}
c906108c 7331
346774a9
PA
7332/* set_raw_breakpoint is a low level routine for allocating and
7333 partially initializing a breakpoint of type BPTYPE. The newly
7334 created breakpoint's address, section, source file name, and line
7335 number are provided by SAL. The newly created and partially
7336 initialized breakpoint is added to the breakpoint chain and
7337 is also returned as the value of this function.
7338
7339 It is expected that the caller will complete the initialization of
7340 the newly created breakpoint struct as well as output any status
7341 information regarding the creation of a new breakpoint. In
7342 particular, set_raw_breakpoint does NOT set the breakpoint
7343 number! Care should be taken to not allow an error to occur
7344 prior to completing the initialization of the breakpoint. If this
7345 should happen, a bogus breakpoint will be left on the chain. */
7346
7347struct breakpoint *
7348set_raw_breakpoint (struct gdbarch *gdbarch,
348d480f 7349 struct symtab_and_line sal, enum bptype bptype,
c0a91b2b 7350 const struct breakpoint_ops *ops)
346774a9 7351{
3b0871f4 7352 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (bptype);
346774a9 7353
3b0871f4 7354 init_raw_breakpoint (b.get (), gdbarch, sal, bptype, ops);
b270e6f9 7355 return add_to_breakpoint_chain (std::move (b));
c906108c
SS
7356}
7357
53a5351d 7358/* Call this routine when stepping and nexting to enable a breakpoint
186c406b
TT
7359 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
7360 initiated the operation. */
c906108c
SS
7361
7362void
186c406b 7363set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
c906108c 7364{
35df4500 7365 struct breakpoint *b, *b_tmp;
5d5658a1 7366 int thread = tp->global_num;
0fd8e87f
UW
7367
7368 /* To avoid having to rescan all objfile symbols at every step,
7369 we maintain a list of continually-inserted but always disabled
7370 longjmp "master" breakpoints. Here, we simply create momentary
7371 clones of those and enable them for the requested thread. */
35df4500 7372 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df 7373 if (b->pspace == current_program_space
186c406b
TT
7374 && (b->type == bp_longjmp_master
7375 || b->type == bp_exception_master))
0fd8e87f 7376 {
06edf0c0
PA
7377 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
7378 struct breakpoint *clone;
cc59ec59 7379
e2e4d78b
JK
7380 /* longjmp_breakpoint_ops ensures INITIATING_FRAME is cleared again
7381 after their removal. */
06edf0c0 7382 clone = momentary_breakpoint_from_master (b, type,
c1fc2657 7383 &momentary_breakpoint_ops, 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{
35df4500 7394 struct breakpoint *b, *b_tmp;
c906108c 7395
35df4500 7396 ALL_BREAKPOINTS_SAFE (b, b_tmp)
186c406b 7397 if (b->type == bp_longjmp || b->type == bp_exception)
611c83ae
PA
7398 {
7399 if (b->thread == thread)
7400 delete_breakpoint (b);
7401 }
c906108c
SS
7402}
7403
f59f708a
PA
7404void
7405delete_longjmp_breakpoint_at_next_stop (int thread)
7406{
7407 struct breakpoint *b, *b_tmp;
7408
7409 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7410 if (b->type == bp_longjmp || b->type == bp_exception)
7411 {
7412 if (b->thread == thread)
7413 b->disposition = disp_del_at_next_stop;
7414 }
7415}
7416
e2e4d78b
JK
7417/* Place breakpoints of type bp_longjmp_call_dummy to catch longjmp for
7418 INFERIOR_PTID thread. Chain them all by RELATED_BREAKPOINT and return
7419 pointer to any of them. Return NULL if this system cannot place longjmp
7420 breakpoints. */
7421
7422struct breakpoint *
7423set_longjmp_breakpoint_for_call_dummy (void)
7424{
7425 struct breakpoint *b, *retval = NULL;
7426
7427 ALL_BREAKPOINTS (b)
7428 if (b->pspace == current_program_space && b->type == bp_longjmp_master)
7429 {
7430 struct breakpoint *new_b;
7431
7432 new_b = momentary_breakpoint_from_master (b, bp_longjmp_call_dummy,
a1aa2221
LM
7433 &momentary_breakpoint_ops,
7434 1);
00431a78 7435 new_b->thread = inferior_thread ()->global_num;
e2e4d78b
JK
7436
7437 /* Link NEW_B into the chain of RETVAL breakpoints. */
7438
7439 gdb_assert (new_b->related_breakpoint == new_b);
7440 if (retval == NULL)
7441 retval = new_b;
7442 new_b->related_breakpoint = retval;
7443 while (retval->related_breakpoint != new_b->related_breakpoint)
7444 retval = retval->related_breakpoint;
7445 retval->related_breakpoint = new_b;
7446 }
7447
7448 return retval;
7449}
7450
7451/* Verify all existing dummy frames and their associated breakpoints for
b67a2c6f 7452 TP. Remove those which can no longer be found in the current frame
e2e4d78b
JK
7453 stack.
7454
7455 You should call this function only at places where it is safe to currently
7456 unwind the whole stack. Failed stack unwind would discard live dummy
7457 frames. */
7458
7459void
b67a2c6f 7460check_longjmp_breakpoint_for_call_dummy (struct thread_info *tp)
e2e4d78b
JK
7461{
7462 struct breakpoint *b, *b_tmp;
7463
7464 ALL_BREAKPOINTS_SAFE (b, b_tmp)
5d5658a1 7465 if (b->type == bp_longjmp_call_dummy && b->thread == tp->global_num)
e2e4d78b
JK
7466 {
7467 struct breakpoint *dummy_b = b->related_breakpoint;
7468
7469 while (dummy_b != b && dummy_b->type != bp_call_dummy)
7470 dummy_b = dummy_b->related_breakpoint;
7471 if (dummy_b->type != bp_call_dummy
7472 || frame_find_by_id (dummy_b->frame_id) != NULL)
7473 continue;
7474
00431a78 7475 dummy_frame_discard (dummy_b->frame_id, tp);
e2e4d78b
JK
7476
7477 while (b->related_breakpoint != b)
7478 {
7479 if (b_tmp == b->related_breakpoint)
7480 b_tmp = b->related_breakpoint->next;
7481 delete_breakpoint (b->related_breakpoint);
7482 }
7483 delete_breakpoint (b);
7484 }
7485}
7486
1900040c
MS
7487void
7488enable_overlay_breakpoints (void)
7489{
52f0bd74 7490 struct breakpoint *b;
1900040c
MS
7491
7492 ALL_BREAKPOINTS (b)
7493 if (b->type == bp_overlay_event)
7494 {
7495 b->enable_state = bp_enabled;
44702360 7496 update_global_location_list (UGLL_MAY_INSERT);
c02f5703 7497 overlay_events_enabled = 1;
1900040c
MS
7498 }
7499}
7500
7501void
7502disable_overlay_breakpoints (void)
7503{
52f0bd74 7504 struct breakpoint *b;
1900040c
MS
7505
7506 ALL_BREAKPOINTS (b)
7507 if (b->type == bp_overlay_event)
7508 {
7509 b->enable_state = bp_disabled;
44702360 7510 update_global_location_list (UGLL_DONT_INSERT);
c02f5703 7511 overlay_events_enabled = 0;
1900040c
MS
7512 }
7513}
7514
aa7d318d
TT
7515/* Set an active std::terminate breakpoint for each std::terminate
7516 master breakpoint. */
7517void
7518set_std_terminate_breakpoint (void)
7519{
35df4500 7520 struct breakpoint *b, *b_tmp;
aa7d318d 7521
35df4500 7522 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
7523 if (b->pspace == current_program_space
7524 && b->type == bp_std_terminate_master)
7525 {
06edf0c0 7526 momentary_breakpoint_from_master (b, bp_std_terminate,
a1aa2221 7527 &momentary_breakpoint_ops, 1);
aa7d318d
TT
7528 }
7529}
7530
7531/* Delete all the std::terminate breakpoints. */
7532void
7533delete_std_terminate_breakpoint (void)
7534{
35df4500 7535 struct breakpoint *b, *b_tmp;
aa7d318d 7536
35df4500 7537 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
7538 if (b->type == bp_std_terminate)
7539 delete_breakpoint (b);
7540}
7541
c4093a6a 7542struct breakpoint *
a6d9a66e 7543create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
c4093a6a
JM
7544{
7545 struct breakpoint *b;
c4093a6a 7546
06edf0c0
PA
7547 b = create_internal_breakpoint (gdbarch, address, bp_thread_event,
7548 &internal_breakpoint_ops);
7549
b5de0fa7 7550 b->enable_state = bp_enabled;
f00aae0f 7551 /* location has to be used or breakpoint_re_set will delete me. */
d28cd78a 7552 b->location = new_address_location (b->loc->address, NULL, 0);
c4093a6a 7553
44702360 7554 update_global_location_list_nothrow (UGLL_MAY_INSERT);
74960c60 7555
c4093a6a
JM
7556 return b;
7557}
7558
0101ce28
JJ
7559struct lang_and_radix
7560 {
7561 enum language lang;
7562 int radix;
7563 };
7564
4efc6507
DE
7565/* Create a breakpoint for JIT code registration and unregistration. */
7566
7567struct breakpoint *
7568create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7569{
2a7f3dff
PA
7570 return create_internal_breakpoint (gdbarch, address, bp_jit_event,
7571 &internal_breakpoint_ops);
4efc6507 7572}
0101ce28 7573
03673fc7
PP
7574/* Remove JIT code registration and unregistration breakpoint(s). */
7575
7576void
7577remove_jit_event_breakpoints (void)
7578{
7579 struct breakpoint *b, *b_tmp;
7580
7581 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7582 if (b->type == bp_jit_event
7583 && b->loc->pspace == current_program_space)
7584 delete_breakpoint (b);
7585}
7586
cae688ec
JJ
7587void
7588remove_solib_event_breakpoints (void)
7589{
35df4500 7590 struct breakpoint *b, *b_tmp;
cae688ec 7591
35df4500 7592 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df
PA
7593 if (b->type == bp_shlib_event
7594 && b->loc->pspace == current_program_space)
cae688ec
JJ
7595 delete_breakpoint (b);
7596}
7597
f37f681c
PA
7598/* See breakpoint.h. */
7599
7600void
7601remove_solib_event_breakpoints_at_next_stop (void)
7602{
7603 struct breakpoint *b, *b_tmp;
7604
7605 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7606 if (b->type == bp_shlib_event
7607 && b->loc->pspace == current_program_space)
7608 b->disposition = disp_del_at_next_stop;
7609}
7610
04086b45
PA
7611/* Helper for create_solib_event_breakpoint /
7612 create_and_insert_solib_event_breakpoint. Allows specifying which
7613 INSERT_MODE to pass through to update_global_location_list. */
7614
7615static struct breakpoint *
7616create_solib_event_breakpoint_1 (struct gdbarch *gdbarch, CORE_ADDR address,
7617 enum ugll_insert_mode insert_mode)
cae688ec
JJ
7618{
7619 struct breakpoint *b;
7620
06edf0c0
PA
7621 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event,
7622 &internal_breakpoint_ops);
04086b45 7623 update_global_location_list_nothrow (insert_mode);
cae688ec
JJ
7624 return b;
7625}
7626
04086b45
PA
7627struct breakpoint *
7628create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7629{
7630 return create_solib_event_breakpoint_1 (gdbarch, address, UGLL_MAY_INSERT);
7631}
7632
f37f681c
PA
7633/* See breakpoint.h. */
7634
7635struct breakpoint *
7636create_and_insert_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7637{
7638 struct breakpoint *b;
7639
04086b45
PA
7640 /* Explicitly tell update_global_location_list to insert
7641 locations. */
7642 b = create_solib_event_breakpoint_1 (gdbarch, address, UGLL_INSERT);
f37f681c
PA
7643 if (!b->loc->inserted)
7644 {
7645 delete_breakpoint (b);
7646 return NULL;
7647 }
7648 return b;
7649}
7650
cae688ec
JJ
7651/* Disable any breakpoints that are on code in shared libraries. Only
7652 apply to enabled breakpoints, disabled ones can just stay disabled. */
7653
7654void
cb851954 7655disable_breakpoints_in_shlibs (void)
cae688ec 7656{
876fa593 7657 struct bp_location *loc, **locp_tmp;
cae688ec 7658
876fa593 7659 ALL_BP_LOCATIONS (loc, locp_tmp)
cae688ec 7660 {
2bdf28a0 7661 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 7662 struct breakpoint *b = loc->owner;
2bdf28a0 7663
4a64f543
MS
7664 /* We apply the check to all breakpoints, including disabled for
7665 those with loc->duplicate set. This is so that when breakpoint
7666 becomes enabled, or the duplicate is removed, gdb will try to
7667 insert all breakpoints. If we don't set shlib_disabled here,
7668 we'll try to insert those breakpoints and fail. */
1042e4c0 7669 if (((b->type == bp_breakpoint)
508ccb1f 7670 || (b->type == bp_jit_event)
1042e4c0 7671 || (b->type == bp_hardware_breakpoint)
d77f58be 7672 || (is_tracepoint (b)))
6c95b8df 7673 && loc->pspace == current_program_space
0d381245 7674 && !loc->shlib_disabled
6c95b8df 7675 && solib_name_from_address (loc->pspace, loc->address)
a77053c2 7676 )
0d381245
VP
7677 {
7678 loc->shlib_disabled = 1;
7679 }
cae688ec
JJ
7680 }
7681}
7682
63644780
NB
7683/* Disable any breakpoints and tracepoints that are in SOLIB upon
7684 notification of unloaded_shlib. Only apply to enabled breakpoints,
7685 disabled ones can just stay disabled. */
84acb35a 7686
75149521 7687static void
84acb35a
JJ
7688disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
7689{
876fa593 7690 struct bp_location *loc, **locp_tmp;
84acb35a
JJ
7691 int disabled_shlib_breaks = 0;
7692
876fa593 7693 ALL_BP_LOCATIONS (loc, locp_tmp)
84acb35a 7694 {
2bdf28a0 7695 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 7696 struct breakpoint *b = loc->owner;
cc59ec59 7697
1e4d1764 7698 if (solib->pspace == loc->pspace
e2dd7057 7699 && !loc->shlib_disabled
1e4d1764
YQ
7700 && (((b->type == bp_breakpoint
7701 || b->type == bp_jit_event
7702 || b->type == bp_hardware_breakpoint)
7703 && (loc->loc_type == bp_loc_hardware_breakpoint
7704 || loc->loc_type == bp_loc_software_breakpoint))
7705 || is_tracepoint (b))
e2dd7057 7706 && solib_contains_address_p (solib, loc->address))
84acb35a 7707 {
e2dd7057
PP
7708 loc->shlib_disabled = 1;
7709 /* At this point, we cannot rely on remove_breakpoint
7710 succeeding so we must mark the breakpoint as not inserted
7711 to prevent future errors occurring in remove_breakpoints. */
7712 loc->inserted = 0;
8d3788bd
VP
7713
7714 /* This may cause duplicate notifications for the same breakpoint. */
76727919 7715 gdb::observers::breakpoint_modified.notify (b);
8d3788bd 7716
e2dd7057
PP
7717 if (!disabled_shlib_breaks)
7718 {
223ffa71 7719 target_terminal::ours_for_output ();
3e43a32a
MS
7720 warning (_("Temporarily disabling breakpoints "
7721 "for unloaded shared library \"%s\""),
e2dd7057 7722 solib->so_name);
84acb35a 7723 }
e2dd7057 7724 disabled_shlib_breaks = 1;
84acb35a
JJ
7725 }
7726 }
84acb35a
JJ
7727}
7728
63644780
NB
7729/* Disable any breakpoints and tracepoints in OBJFILE upon
7730 notification of free_objfile. Only apply to enabled breakpoints,
7731 disabled ones can just stay disabled. */
7732
7733static void
7734disable_breakpoints_in_freed_objfile (struct objfile *objfile)
7735{
7736 struct breakpoint *b;
7737
7738 if (objfile == NULL)
7739 return;
7740
d03de421
PA
7741 /* OBJF_SHARED|OBJF_USERLOADED objfiles are dynamic modules manually
7742 managed by the user with add-symbol-file/remove-symbol-file.
7743 Similarly to how breakpoints in shared libraries are handled in
7744 response to "nosharedlibrary", mark breakpoints in such modules
08351840
PA
7745 shlib_disabled so they end up uninserted on the next global
7746 location list update. Shared libraries not loaded by the user
7747 aren't handled here -- they're already handled in
7748 disable_breakpoints_in_unloaded_shlib, called by solib.c's
7749 solib_unloaded observer. We skip objfiles that are not
d03de421
PA
7750 OBJF_SHARED as those aren't considered dynamic objects (e.g. the
7751 main objfile). */
7752 if ((objfile->flags & OBJF_SHARED) == 0
7753 || (objfile->flags & OBJF_USERLOADED) == 0)
63644780
NB
7754 return;
7755
7756 ALL_BREAKPOINTS (b)
7757 {
7758 struct bp_location *loc;
7759 int bp_modified = 0;
7760
7761 if (!is_breakpoint (b) && !is_tracepoint (b))
7762 continue;
7763
7764 for (loc = b->loc; loc != NULL; loc = loc->next)
7765 {
7766 CORE_ADDR loc_addr = loc->address;
7767
7768 if (loc->loc_type != bp_loc_hardware_breakpoint
7769 && loc->loc_type != bp_loc_software_breakpoint)
7770 continue;
7771
7772 if (loc->shlib_disabled != 0)
7773 continue;
7774
7775 if (objfile->pspace != loc->pspace)
7776 continue;
7777
7778 if (loc->loc_type != bp_loc_hardware_breakpoint
7779 && loc->loc_type != bp_loc_software_breakpoint)
7780 continue;
7781
7782 if (is_addr_in_objfile (loc_addr, objfile))
7783 {
7784 loc->shlib_disabled = 1;
08351840
PA
7785 /* At this point, we don't know whether the object was
7786 unmapped from the inferior or not, so leave the
7787 inserted flag alone. We'll handle failure to
7788 uninsert quietly, in case the object was indeed
7789 unmapped. */
63644780
NB
7790
7791 mark_breakpoint_location_modified (loc);
7792
7793 bp_modified = 1;
7794 }
7795 }
7796
7797 if (bp_modified)
76727919 7798 gdb::observers::breakpoint_modified.notify (b);
63644780
NB
7799 }
7800}
7801
ce78b96d
JB
7802/* FORK & VFORK catchpoints. */
7803
e29a4733 7804/* An instance of this type is used to represent a fork or vfork
c1fc2657
SM
7805 catchpoint. A breakpoint is really of this type iff its ops pointer points
7806 to CATCH_FORK_BREAKPOINT_OPS. */
e29a4733 7807
c1fc2657 7808struct fork_catchpoint : public breakpoint
e29a4733 7809{
e29a4733
PA
7810 /* Process id of a child process whose forking triggered this
7811 catchpoint. This field is only valid immediately after this
7812 catchpoint has triggered. */
7813 ptid_t forked_inferior_pid;
7814};
7815
4a64f543
MS
7816/* Implement the "insert" breakpoint_ops method for fork
7817 catchpoints. */
ce78b96d 7818
77b06cd7
TJB
7819static int
7820insert_catch_fork (struct bp_location *bl)
ce78b96d 7821{
e99b03dc 7822 return target_insert_fork_catchpoint (inferior_ptid.pid ());
ce78b96d
JB
7823}
7824
4a64f543
MS
7825/* Implement the "remove" breakpoint_ops method for fork
7826 catchpoints. */
ce78b96d
JB
7827
7828static int
73971819 7829remove_catch_fork (struct bp_location *bl, enum remove_bp_reason reason)
ce78b96d 7830{
e99b03dc 7831 return target_remove_fork_catchpoint (inferior_ptid.pid ());
ce78b96d
JB
7832}
7833
7834/* Implement the "breakpoint_hit" breakpoint_ops method for fork
7835 catchpoints. */
7836
7837static int
f1310107 7838breakpoint_hit_catch_fork (const struct bp_location *bl,
bd522513 7839 const address_space *aspace, CORE_ADDR bp_addr,
09ac7c10 7840 const struct target_waitstatus *ws)
ce78b96d 7841{
e29a4733
PA
7842 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7843
f90263c1
TT
7844 if (ws->kind != TARGET_WAITKIND_FORKED)
7845 return 0;
7846
7847 c->forked_inferior_pid = ws->value.related_pid;
7848 return 1;
ce78b96d
JB
7849}
7850
4a64f543
MS
7851/* Implement the "print_it" breakpoint_ops method for fork
7852 catchpoints. */
ce78b96d
JB
7853
7854static enum print_stop_action
348d480f 7855print_it_catch_fork (bpstat bs)
ce78b96d 7856{
36dfb11c 7857 struct ui_out *uiout = current_uiout;
348d480f
PA
7858 struct breakpoint *b = bs->breakpoint_at;
7859 struct fork_catchpoint *c = (struct fork_catchpoint *) bs->breakpoint_at;
e29a4733 7860
ce78b96d 7861 annotate_catchpoint (b->number);
f303dbd6 7862 maybe_print_thread_hit_breakpoint (uiout);
36dfb11c 7863 if (b->disposition == disp_del)
112e8700 7864 uiout->text ("Temporary catchpoint ");
36dfb11c 7865 else
112e8700
SM
7866 uiout->text ("Catchpoint ");
7867 if (uiout->is_mi_like_p ())
36dfb11c 7868 {
112e8700
SM
7869 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_FORK));
7870 uiout->field_string ("disp", bpdisp_text (b->disposition));
36dfb11c 7871 }
381befee 7872 uiout->field_signed ("bkptno", b->number);
112e8700 7873 uiout->text (" (forked process ");
381befee 7874 uiout->field_signed ("newpid", c->forked_inferior_pid.pid ());
112e8700 7875 uiout->text ("), ");
ce78b96d
JB
7876 return PRINT_SRC_AND_LOC;
7877}
7878
4a64f543
MS
7879/* Implement the "print_one" breakpoint_ops method for fork
7880 catchpoints. */
ce78b96d
JB
7881
7882static void
a6d9a66e 7883print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 7884{
e29a4733 7885 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
79a45b7d 7886 struct value_print_options opts;
79a45e25 7887 struct ui_out *uiout = current_uiout;
79a45b7d
TT
7888
7889 get_user_print_options (&opts);
7890
4a64f543
MS
7891 /* Field 4, the address, is omitted (which makes the columns not
7892 line up too nicely with the headers, but the effect is relatively
7893 readable). */
79a45b7d 7894 if (opts.addressprint)
112e8700 7895 uiout->field_skip ("addr");
ce78b96d 7896 annotate_field (5);
112e8700 7897 uiout->text ("fork");
d7e15655 7898 if (c->forked_inferior_pid != null_ptid)
ce78b96d 7899 {
112e8700 7900 uiout->text (", process ");
381befee 7901 uiout->field_signed ("what", c->forked_inferior_pid.pid ());
112e8700 7902 uiout->spaces (1);
ce78b96d 7903 }
8ac3646f 7904
112e8700
SM
7905 if (uiout->is_mi_like_p ())
7906 uiout->field_string ("catch-type", "fork");
ce78b96d
JB
7907}
7908
7909/* Implement the "print_mention" breakpoint_ops method for fork
7910 catchpoints. */
7911
7912static void
7913print_mention_catch_fork (struct breakpoint *b)
7914{
7915 printf_filtered (_("Catchpoint %d (fork)"), b->number);
7916}
7917
6149aea9
PA
7918/* Implement the "print_recreate" breakpoint_ops method for fork
7919 catchpoints. */
7920
7921static void
7922print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
7923{
7924 fprintf_unfiltered (fp, "catch fork");
d9b3f62e 7925 print_recreate_thread (b, fp);
6149aea9
PA
7926}
7927
ce78b96d
JB
7928/* The breakpoint_ops structure to be used in fork catchpoints. */
7929
2060206e 7930static struct breakpoint_ops catch_fork_breakpoint_ops;
ce78b96d 7931
4a64f543
MS
7932/* Implement the "insert" breakpoint_ops method for vfork
7933 catchpoints. */
ce78b96d 7934
77b06cd7
TJB
7935static int
7936insert_catch_vfork (struct bp_location *bl)
ce78b96d 7937{
e99b03dc 7938 return target_insert_vfork_catchpoint (inferior_ptid.pid ());
ce78b96d
JB
7939}
7940
4a64f543
MS
7941/* Implement the "remove" breakpoint_ops method for vfork
7942 catchpoints. */
ce78b96d
JB
7943
7944static int
73971819 7945remove_catch_vfork (struct bp_location *bl, enum remove_bp_reason reason)
ce78b96d 7946{
e99b03dc 7947 return target_remove_vfork_catchpoint (inferior_ptid.pid ());
ce78b96d
JB
7948}
7949
7950/* Implement the "breakpoint_hit" breakpoint_ops method for vfork
7951 catchpoints. */
7952
7953static int
f1310107 7954breakpoint_hit_catch_vfork (const struct bp_location *bl,
bd522513 7955 const address_space *aspace, CORE_ADDR bp_addr,
09ac7c10 7956 const struct target_waitstatus *ws)
ce78b96d 7957{
e29a4733
PA
7958 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7959
f90263c1
TT
7960 if (ws->kind != TARGET_WAITKIND_VFORKED)
7961 return 0;
7962
7963 c->forked_inferior_pid = ws->value.related_pid;
7964 return 1;
ce78b96d
JB
7965}
7966
4a64f543
MS
7967/* Implement the "print_it" breakpoint_ops method for vfork
7968 catchpoints. */
ce78b96d
JB
7969
7970static enum print_stop_action
348d480f 7971print_it_catch_vfork (bpstat bs)
ce78b96d 7972{
36dfb11c 7973 struct ui_out *uiout = current_uiout;
348d480f 7974 struct breakpoint *b = bs->breakpoint_at;
e29a4733
PA
7975 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
7976
ce78b96d 7977 annotate_catchpoint (b->number);
f303dbd6 7978 maybe_print_thread_hit_breakpoint (uiout);
36dfb11c 7979 if (b->disposition == disp_del)
112e8700 7980 uiout->text ("Temporary catchpoint ");
36dfb11c 7981 else
112e8700
SM
7982 uiout->text ("Catchpoint ");
7983 if (uiout->is_mi_like_p ())
36dfb11c 7984 {
112e8700
SM
7985 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_VFORK));
7986 uiout->field_string ("disp", bpdisp_text (b->disposition));
36dfb11c 7987 }
381befee 7988 uiout->field_signed ("bkptno", b->number);
112e8700 7989 uiout->text (" (vforked process ");
381befee 7990 uiout->field_signed ("newpid", c->forked_inferior_pid.pid ());
112e8700 7991 uiout->text ("), ");
ce78b96d
JB
7992 return PRINT_SRC_AND_LOC;
7993}
7994
4a64f543
MS
7995/* Implement the "print_one" breakpoint_ops method for vfork
7996 catchpoints. */
ce78b96d
JB
7997
7998static void
a6d9a66e 7999print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 8000{
e29a4733 8001 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
79a45b7d 8002 struct value_print_options opts;
79a45e25 8003 struct ui_out *uiout = current_uiout;
79a45b7d
TT
8004
8005 get_user_print_options (&opts);
4a64f543
MS
8006 /* Field 4, the address, is omitted (which makes the columns not
8007 line up too nicely with the headers, but the effect is relatively
8008 readable). */
79a45b7d 8009 if (opts.addressprint)
112e8700 8010 uiout->field_skip ("addr");
ce78b96d 8011 annotate_field (5);
112e8700 8012 uiout->text ("vfork");
d7e15655 8013 if (c->forked_inferior_pid != null_ptid)
ce78b96d 8014 {
112e8700 8015 uiout->text (", process ");
381befee 8016 uiout->field_signed ("what", c->forked_inferior_pid.pid ());
112e8700 8017 uiout->spaces (1);
ce78b96d 8018 }
8ac3646f 8019
112e8700
SM
8020 if (uiout->is_mi_like_p ())
8021 uiout->field_string ("catch-type", "vfork");
ce78b96d
JB
8022}
8023
8024/* Implement the "print_mention" breakpoint_ops method for vfork
8025 catchpoints. */
8026
8027static void
8028print_mention_catch_vfork (struct breakpoint *b)
8029{
8030 printf_filtered (_("Catchpoint %d (vfork)"), b->number);
8031}
8032
6149aea9
PA
8033/* Implement the "print_recreate" breakpoint_ops method for vfork
8034 catchpoints. */
8035
8036static void
8037print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
8038{
8039 fprintf_unfiltered (fp, "catch vfork");
d9b3f62e 8040 print_recreate_thread (b, fp);
6149aea9
PA
8041}
8042
ce78b96d
JB
8043/* The breakpoint_ops structure to be used in vfork catchpoints. */
8044
2060206e 8045static struct breakpoint_ops catch_vfork_breakpoint_ops;
ce78b96d 8046
edcc5120 8047/* An instance of this type is used to represent an solib catchpoint.
c1fc2657 8048 A breakpoint is really of this type iff its ops pointer points to
edcc5120
TT
8049 CATCH_SOLIB_BREAKPOINT_OPS. */
8050
c1fc2657 8051struct solib_catchpoint : public breakpoint
edcc5120 8052{
c1fc2657 8053 ~solib_catchpoint () override;
edcc5120
TT
8054
8055 /* True for "catch load", false for "catch unload". */
b650a282 8056 bool is_load;
edcc5120
TT
8057
8058 /* Regular expression to match, if any. COMPILED is only valid when
8059 REGEX is non-NULL. */
8060 char *regex;
2d7cc5c7 8061 std::unique_ptr<compiled_regex> compiled;
edcc5120
TT
8062};
8063
c1fc2657 8064solib_catchpoint::~solib_catchpoint ()
edcc5120 8065{
c1fc2657 8066 xfree (this->regex);
edcc5120
TT
8067}
8068
8069static int
8070insert_catch_solib (struct bp_location *ignore)
8071{
8072 return 0;
8073}
8074
8075static int
73971819 8076remove_catch_solib (struct bp_location *ignore, enum remove_bp_reason reason)
edcc5120
TT
8077{
8078 return 0;
8079}
8080
8081static int
8082breakpoint_hit_catch_solib (const struct bp_location *bl,
bd522513 8083 const address_space *aspace,
edcc5120
TT
8084 CORE_ADDR bp_addr,
8085 const struct target_waitstatus *ws)
8086{
8087 struct solib_catchpoint *self = (struct solib_catchpoint *) bl->owner;
8088 struct breakpoint *other;
8089
8090 if (ws->kind == TARGET_WAITKIND_LOADED)
8091 return 1;
8092
8093 ALL_BREAKPOINTS (other)
8094 {
8095 struct bp_location *other_bl;
8096
8097 if (other == bl->owner)
8098 continue;
8099
8100 if (other->type != bp_shlib_event)
8101 continue;
8102
c1fc2657 8103 if (self->pspace != NULL && other->pspace != self->pspace)
edcc5120
TT
8104 continue;
8105
8106 for (other_bl = other->loc; other_bl != NULL; other_bl = other_bl->next)
8107 {
8108 if (other->ops->breakpoint_hit (other_bl, aspace, bp_addr, ws))
8109 return 1;
8110 }
8111 }
8112
8113 return 0;
8114}
8115
8116static void
8117check_status_catch_solib (struct bpstats *bs)
8118{
8119 struct solib_catchpoint *self
8120 = (struct solib_catchpoint *) bs->breakpoint_at;
edcc5120
TT
8121
8122 if (self->is_load)
8123 {
52941706 8124 for (so_list *iter : current_program_space->added_solibs)
edcc5120
TT
8125 {
8126 if (!self->regex
2d7cc5c7 8127 || self->compiled->exec (iter->so_name, 0, NULL, 0) == 0)
edcc5120
TT
8128 return;
8129 }
8130 }
8131 else
8132 {
6fb16ce6 8133 for (const std::string &iter : current_program_space->deleted_solibs)
edcc5120
TT
8134 {
8135 if (!self->regex
6fb16ce6 8136 || self->compiled->exec (iter.c_str (), 0, NULL, 0) == 0)
edcc5120
TT
8137 return;
8138 }
8139 }
8140
8141 bs->stop = 0;
8142 bs->print_it = print_it_noop;
8143}
8144
8145static enum print_stop_action
8146print_it_catch_solib (bpstat bs)
8147{
8148 struct breakpoint *b = bs->breakpoint_at;
8149 struct ui_out *uiout = current_uiout;
8150
8151 annotate_catchpoint (b->number);
f303dbd6 8152 maybe_print_thread_hit_breakpoint (uiout);
edcc5120 8153 if (b->disposition == disp_del)
112e8700 8154 uiout->text ("Temporary catchpoint ");
edcc5120 8155 else
112e8700 8156 uiout->text ("Catchpoint ");
381befee 8157 uiout->field_signed ("bkptno", b->number);
112e8700
SM
8158 uiout->text ("\n");
8159 if (uiout->is_mi_like_p ())
8160 uiout->field_string ("disp", bpdisp_text (b->disposition));
edcc5120
TT
8161 print_solib_event (1);
8162 return PRINT_SRC_AND_LOC;
8163}
8164
8165static void
8166print_one_catch_solib (struct breakpoint *b, struct bp_location **locs)
8167{
8168 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8169 struct value_print_options opts;
8170 struct ui_out *uiout = current_uiout;
edcc5120
TT
8171
8172 get_user_print_options (&opts);
8173 /* Field 4, the address, is omitted (which makes the columns not
8174 line up too nicely with the headers, but the effect is relatively
8175 readable). */
8176 if (opts.addressprint)
8177 {
8178 annotate_field (4);
112e8700 8179 uiout->field_skip ("addr");
edcc5120
TT
8180 }
8181
528e1572 8182 std::string msg;
edcc5120
TT
8183 annotate_field (5);
8184 if (self->is_load)
8185 {
8186 if (self->regex)
528e1572 8187 msg = string_printf (_("load of library matching %s"), self->regex);
edcc5120 8188 else
528e1572 8189 msg = _("load of library");
edcc5120
TT
8190 }
8191 else
8192 {
8193 if (self->regex)
528e1572 8194 msg = string_printf (_("unload of library matching %s"), self->regex);
edcc5120 8195 else
528e1572 8196 msg = _("unload of library");
edcc5120 8197 }
112e8700 8198 uiout->field_string ("what", msg);
8ac3646f 8199
112e8700
SM
8200 if (uiout->is_mi_like_p ())
8201 uiout->field_string ("catch-type", self->is_load ? "load" : "unload");
edcc5120
TT
8202}
8203
8204static void
8205print_mention_catch_solib (struct breakpoint *b)
8206{
8207 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8208
8209 printf_filtered (_("Catchpoint %d (%s)"), b->number,
8210 self->is_load ? "load" : "unload");
8211}
8212
8213static void
8214print_recreate_catch_solib (struct breakpoint *b, struct ui_file *fp)
8215{
8216 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8217
8218 fprintf_unfiltered (fp, "%s %s",
8219 b->disposition == disp_del ? "tcatch" : "catch",
8220 self->is_load ? "load" : "unload");
8221 if (self->regex)
8222 fprintf_unfiltered (fp, " %s", self->regex);
8223 fprintf_unfiltered (fp, "\n");
8224}
8225
8226static struct breakpoint_ops catch_solib_breakpoint_ops;
8227
b650a282 8228/* See breakpoint.h. */
edcc5120 8229
91985142 8230void
b650a282 8231add_solib_catchpoint (const char *arg, bool is_load, bool is_temp, bool enabled)
edcc5120 8232{
edcc5120 8233 struct gdbarch *gdbarch = get_current_arch ();
edcc5120 8234
edcc5120
TT
8235 if (!arg)
8236 arg = "";
f1735a53 8237 arg = skip_spaces (arg);
edcc5120 8238
36bd8eaa 8239 std::unique_ptr<solib_catchpoint> c (new solib_catchpoint ());
edcc5120
TT
8240
8241 if (*arg != '\0')
8242 {
2d7cc5c7
PA
8243 c->compiled.reset (new compiled_regex (arg, REG_NOSUB,
8244 _("Invalid regexp")));
edcc5120
TT
8245 c->regex = xstrdup (arg);
8246 }
8247
8248 c->is_load = is_load;
36bd8eaa 8249 init_catchpoint (c.get (), gdbarch, is_temp, NULL,
edcc5120
TT
8250 &catch_solib_breakpoint_ops);
8251
c1fc2657 8252 c->enable_state = enabled ? bp_enabled : bp_disabled;
91985142 8253
b270e6f9 8254 install_breakpoint (0, std::move (c), 1);
edcc5120
TT
8255}
8256
91985142
MG
8257/* A helper function that does all the work for "catch load" and
8258 "catch unload". */
8259
8260static void
eb4c3f4a 8261catch_load_or_unload (const char *arg, int from_tty, int is_load,
91985142
MG
8262 struct cmd_list_element *command)
8263{
91985142 8264 const int enabled = 1;
b650a282 8265 bool temp = get_cmd_context (command) == CATCH_TEMPORARY;
91985142 8266
b650a282 8267 add_solib_catchpoint (arg, is_load, temp, enabled);
91985142
MG
8268}
8269
edcc5120 8270static void
eb4c3f4a 8271catch_load_command_1 (const char *arg, int from_tty,
edcc5120
TT
8272 struct cmd_list_element *command)
8273{
8274 catch_load_or_unload (arg, from_tty, 1, command);
8275}
8276
8277static void
eb4c3f4a 8278catch_unload_command_1 (const char *arg, int from_tty,
edcc5120
TT
8279 struct cmd_list_element *command)
8280{
8281 catch_load_or_unload (arg, from_tty, 0, command);
8282}
8283
b650a282 8284/* See breakpoint.h. */
ce78b96d 8285
ab04a2af 8286void
346774a9 8287init_catchpoint (struct breakpoint *b,
b650a282 8288 struct gdbarch *gdbarch, bool temp,
63160a43 8289 const char *cond_string,
c0a91b2b 8290 const struct breakpoint_ops *ops)
c906108c 8291{
51abb421 8292 symtab_and_line sal;
6c95b8df 8293 sal.pspace = current_program_space;
c5aa993b 8294
28010a5d 8295 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
ce78b96d 8296
1b36a34b 8297 b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
b650a282 8298 b->disposition = temp ? disp_del : disp_donttouch;
346774a9
PA
8299}
8300
28010a5d 8301void
b270e6f9 8302install_breakpoint (int internal, std::unique_ptr<breakpoint> &&arg, int update_gll)
c56053d2 8303{
b270e6f9 8304 breakpoint *b = add_to_breakpoint_chain (std::move (arg));
3a5c3e22 8305 set_breakpoint_number (internal, b);
558a9d82
YQ
8306 if (is_tracepoint (b))
8307 set_tracepoint_count (breakpoint_count);
3a5c3e22
PA
8308 if (!internal)
8309 mention (b);
76727919 8310 gdb::observers::breakpoint_created.notify (b);
3ea46bff
YQ
8311
8312 if (update_gll)
44702360 8313 update_global_location_list (UGLL_MAY_INSERT);
c56053d2
PA
8314}
8315
9b70b993 8316static void
a6d9a66e 8317create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
b650a282 8318 bool temp, const char *cond_string,
dda83cd7 8319 const struct breakpoint_ops *ops)
c906108c 8320{
b270e6f9 8321 std::unique_ptr<fork_catchpoint> c (new fork_catchpoint ());
ce78b96d 8322
b650a282 8323 init_catchpoint (c.get (), gdbarch, temp, cond_string, ops);
e29a4733
PA
8324
8325 c->forked_inferior_pid = null_ptid;
8326
b270e6f9 8327 install_breakpoint (0, std::move (c), 1);
c906108c
SS
8328}
8329
fe798b75
JB
8330/* Exec catchpoints. */
8331
b4d90040 8332/* An instance of this type is used to represent an exec catchpoint.
c1fc2657 8333 A breakpoint is really of this type iff its ops pointer points to
b4d90040
PA
8334 CATCH_EXEC_BREAKPOINT_OPS. */
8335
c1fc2657 8336struct exec_catchpoint : public breakpoint
b4d90040 8337{
c1fc2657 8338 ~exec_catchpoint () override;
b4d90040
PA
8339
8340 /* Filename of a program whose exec triggered this catchpoint.
8341 This field is only valid immediately after this catchpoint has
8342 triggered. */
8343 char *exec_pathname;
8344};
8345
c1fc2657 8346/* Exec catchpoint destructor. */
b4d90040 8347
c1fc2657 8348exec_catchpoint::~exec_catchpoint ()
b4d90040 8349{
c1fc2657 8350 xfree (this->exec_pathname);
b4d90040
PA
8351}
8352
77b06cd7
TJB
8353static int
8354insert_catch_exec (struct bp_location *bl)
c906108c 8355{
e99b03dc 8356 return target_insert_exec_catchpoint (inferior_ptid.pid ());
fe798b75 8357}
c906108c 8358
fe798b75 8359static int
73971819 8360remove_catch_exec (struct bp_location *bl, enum remove_bp_reason reason)
fe798b75 8361{
e99b03dc 8362 return target_remove_exec_catchpoint (inferior_ptid.pid ());
fe798b75 8363}
c906108c 8364
fe798b75 8365static int
f1310107 8366breakpoint_hit_catch_exec (const struct bp_location *bl,
bd522513 8367 const address_space *aspace, CORE_ADDR bp_addr,
09ac7c10 8368 const struct target_waitstatus *ws)
fe798b75 8369{
b4d90040
PA
8370 struct exec_catchpoint *c = (struct exec_catchpoint *) bl->owner;
8371
f90263c1
TT
8372 if (ws->kind != TARGET_WAITKIND_EXECD)
8373 return 0;
8374
8375 c->exec_pathname = xstrdup (ws->value.execd_pathname);
8376 return 1;
fe798b75 8377}
c906108c 8378
fe798b75 8379static enum print_stop_action
348d480f 8380print_it_catch_exec (bpstat bs)
fe798b75 8381{
36dfb11c 8382 struct ui_out *uiout = current_uiout;
348d480f 8383 struct breakpoint *b = bs->breakpoint_at;
b4d90040
PA
8384 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8385
fe798b75 8386 annotate_catchpoint (b->number);
f303dbd6 8387 maybe_print_thread_hit_breakpoint (uiout);
36dfb11c 8388 if (b->disposition == disp_del)
112e8700 8389 uiout->text ("Temporary catchpoint ");
36dfb11c 8390 else
112e8700
SM
8391 uiout->text ("Catchpoint ");
8392 if (uiout->is_mi_like_p ())
36dfb11c 8393 {
112e8700
SM
8394 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_EXEC));
8395 uiout->field_string ("disp", bpdisp_text (b->disposition));
36dfb11c 8396 }
381befee 8397 uiout->field_signed ("bkptno", b->number);
112e8700
SM
8398 uiout->text (" (exec'd ");
8399 uiout->field_string ("new-exec", c->exec_pathname);
8400 uiout->text ("), ");
36dfb11c 8401
fe798b75 8402 return PRINT_SRC_AND_LOC;
c906108c
SS
8403}
8404
fe798b75 8405static void
a6d9a66e 8406print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
fe798b75 8407{
b4d90040 8408 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
fe798b75 8409 struct value_print_options opts;
79a45e25 8410 struct ui_out *uiout = current_uiout;
fe798b75
JB
8411
8412 get_user_print_options (&opts);
8413
8414 /* Field 4, the address, is omitted (which makes the columns
8415 not line up too nicely with the headers, but the effect
8416 is relatively readable). */
8417 if (opts.addressprint)
112e8700 8418 uiout->field_skip ("addr");
fe798b75 8419 annotate_field (5);
112e8700 8420 uiout->text ("exec");
b4d90040 8421 if (c->exec_pathname != NULL)
fe798b75 8422 {
112e8700
SM
8423 uiout->text (", program \"");
8424 uiout->field_string ("what", c->exec_pathname);
8425 uiout->text ("\" ");
fe798b75 8426 }
8ac3646f 8427
112e8700
SM
8428 if (uiout->is_mi_like_p ())
8429 uiout->field_string ("catch-type", "exec");
fe798b75
JB
8430}
8431
8432static void
8433print_mention_catch_exec (struct breakpoint *b)
8434{
8435 printf_filtered (_("Catchpoint %d (exec)"), b->number);
8436}
8437
6149aea9
PA
8438/* Implement the "print_recreate" breakpoint_ops method for exec
8439 catchpoints. */
8440
8441static void
8442print_recreate_catch_exec (struct breakpoint *b, struct ui_file *fp)
8443{
8444 fprintf_unfiltered (fp, "catch exec");
d9b3f62e 8445 print_recreate_thread (b, fp);
6149aea9
PA
8446}
8447
2060206e 8448static struct breakpoint_ops catch_exec_breakpoint_ops;
fe798b75 8449
c906108c 8450static int
fba45db2 8451hw_breakpoint_used_count (void)
c906108c 8452{
c906108c 8453 int i = 0;
f1310107
TJB
8454 struct breakpoint *b;
8455 struct bp_location *bl;
c906108c
SS
8456
8457 ALL_BREAKPOINTS (b)
c5aa993b 8458 {
d6b74ac4 8459 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
f1310107
TJB
8460 for (bl = b->loc; bl; bl = bl->next)
8461 {
8462 /* Special types of hardware breakpoints may use more than
8463 one register. */
348d480f 8464 i += b->ops->resources_needed (bl);
f1310107 8465 }
c5aa993b 8466 }
c906108c
SS
8467
8468 return i;
8469}
8470
a1398e0c
PA
8471/* Returns the resources B would use if it were a hardware
8472 watchpoint. */
8473
c906108c 8474static int
a1398e0c 8475hw_watchpoint_use_count (struct breakpoint *b)
c906108c 8476{
c906108c 8477 int i = 0;
e09342b5 8478 struct bp_location *bl;
c906108c 8479
a1398e0c
PA
8480 if (!breakpoint_enabled (b))
8481 return 0;
8482
8483 for (bl = b->loc; bl; bl = bl->next)
8484 {
8485 /* Special types of hardware watchpoints may use more than
8486 one register. */
8487 i += b->ops->resources_needed (bl);
8488 }
8489
8490 return i;
8491}
8492
8493/* Returns the sum the used resources of all hardware watchpoints of
8494 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
8495 the sum of the used resources of all hardware watchpoints of other
8496 types _not_ TYPE. */
8497
8498static int
8499hw_watchpoint_used_count_others (struct breakpoint *except,
8500 enum bptype type, int *other_type_used)
8501{
8502 int i = 0;
8503 struct breakpoint *b;
8504
c906108c
SS
8505 *other_type_used = 0;
8506 ALL_BREAKPOINTS (b)
e09342b5 8507 {
a1398e0c
PA
8508 if (b == except)
8509 continue;
e09342b5
TJB
8510 if (!breakpoint_enabled (b))
8511 continue;
8512
a1398e0c
PA
8513 if (b->type == type)
8514 i += hw_watchpoint_use_count (b);
8515 else if (is_hardware_watchpoint (b))
8516 *other_type_used = 1;
e09342b5
TJB
8517 }
8518
c906108c
SS
8519 return i;
8520}
8521
c906108c 8522void
fba45db2 8523disable_watchpoints_before_interactive_call_start (void)
c906108c 8524{
c5aa993b 8525 struct breakpoint *b;
c906108c
SS
8526
8527 ALL_BREAKPOINTS (b)
c5aa993b 8528 {
cc60f2e3 8529 if (is_watchpoint (b) && breakpoint_enabled (b))
c5aa993b 8530 {
b5de0fa7 8531 b->enable_state = bp_call_disabled;
44702360 8532 update_global_location_list (UGLL_DONT_INSERT);
c5aa993b
JM
8533 }
8534 }
c906108c
SS
8535}
8536
8537void
fba45db2 8538enable_watchpoints_after_interactive_call_stop (void)
c906108c 8539{
c5aa993b 8540 struct breakpoint *b;
c906108c
SS
8541
8542 ALL_BREAKPOINTS (b)
c5aa993b 8543 {
cc60f2e3 8544 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
c5aa993b 8545 {
b5de0fa7 8546 b->enable_state = bp_enabled;
44702360 8547 update_global_location_list (UGLL_MAY_INSERT);
c5aa993b
JM
8548 }
8549 }
c906108c
SS
8550}
8551
8bea4e01
UW
8552void
8553disable_breakpoints_before_startup (void)
8554{
6c95b8df 8555 current_program_space->executing_startup = 1;
44702360 8556 update_global_location_list (UGLL_DONT_INSERT);
8bea4e01
UW
8557}
8558
8559void
8560enable_breakpoints_after_startup (void)
8561{
6c95b8df 8562 current_program_space->executing_startup = 0;
f8eba3c6 8563 breakpoint_re_set ();
8bea4e01
UW
8564}
8565
7c16b83e
PA
8566/* Create a new single-step breakpoint for thread THREAD, with no
8567 locations. */
c906108c 8568
7c16b83e
PA
8569static struct breakpoint *
8570new_single_step_breakpoint (int thread, struct gdbarch *gdbarch)
8571{
b270e6f9 8572 std::unique_ptr<breakpoint> b (new breakpoint ());
7c16b83e 8573
b270e6f9 8574 init_raw_breakpoint_without_location (b.get (), gdbarch, bp_single_step,
7c16b83e
PA
8575 &momentary_breakpoint_ops);
8576
8577 b->disposition = disp_donttouch;
8578 b->frame_id = null_frame_id;
8579
8580 b->thread = thread;
8581 gdb_assert (b->thread != 0);
8582
b270e6f9 8583 return add_to_breakpoint_chain (std::move (b));
7c16b83e
PA
8584}
8585
8586/* Set a momentary breakpoint of type TYPE at address specified by
8587 SAL. If FRAME_ID is valid, the breakpoint is restricted to that
8588 frame. */
c906108c 8589
454dafbd 8590breakpoint_up
a6d9a66e
UW
8591set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
8592 struct frame_id frame_id, enum bptype type)
c906108c 8593{
52f0bd74 8594 struct breakpoint *b;
edb3359d 8595
193facb3
JK
8596 /* If FRAME_ID is valid, it should be a real frame, not an inlined or
8597 tail-called one. */
8598 gdb_assert (!frame_id_artificial_p (frame_id));
edb3359d 8599
06edf0c0 8600 b = set_raw_breakpoint (gdbarch, sal, type, &momentary_breakpoint_ops);
b5de0fa7
EZ
8601 b->enable_state = bp_enabled;
8602 b->disposition = disp_donttouch;
818dd999 8603 b->frame_id = frame_id;
c906108c 8604
00431a78 8605 b->thread = inferior_thread ()->global_num;
c906108c 8606
44702360 8607 update_global_location_list_nothrow (UGLL_MAY_INSERT);
74960c60 8608
454dafbd 8609 return breakpoint_up (b);
c906108c 8610}
611c83ae 8611
06edf0c0 8612/* Make a momentary breakpoint based on the master breakpoint ORIG.
a1aa2221
LM
8613 The new breakpoint will have type TYPE, use OPS as its
8614 breakpoint_ops, and will set enabled to LOC_ENABLED. */
e58b0e63 8615
06edf0c0
PA
8616static struct breakpoint *
8617momentary_breakpoint_from_master (struct breakpoint *orig,
8618 enum bptype type,
a1aa2221
LM
8619 const struct breakpoint_ops *ops,
8620 int loc_enabled)
e58b0e63
PA
8621{
8622 struct breakpoint *copy;
8623
06edf0c0 8624 copy = set_raw_breakpoint_without_location (orig->gdbarch, type, ops);
e58b0e63 8625 copy->loc = allocate_bp_location (copy);
0ba852ab 8626 set_breakpoint_location_function (copy->loc);
e58b0e63 8627
a6d9a66e 8628 copy->loc->gdbarch = orig->loc->gdbarch;
e58b0e63
PA
8629 copy->loc->requested_address = orig->loc->requested_address;
8630 copy->loc->address = orig->loc->address;
8631 copy->loc->section = orig->loc->section;
6c95b8df 8632 copy->loc->pspace = orig->loc->pspace;
55aa24fb 8633 copy->loc->probe = orig->loc->probe;
f8eba3c6 8634 copy->loc->line_number = orig->loc->line_number;
2f202fde 8635 copy->loc->symtab = orig->loc->symtab;
a1aa2221 8636 copy->loc->enabled = loc_enabled;
e58b0e63
PA
8637 copy->frame_id = orig->frame_id;
8638 copy->thread = orig->thread;
6c95b8df 8639 copy->pspace = orig->pspace;
e58b0e63
PA
8640
8641 copy->enable_state = bp_enabled;
8642 copy->disposition = disp_donttouch;
8643 copy->number = internal_breakpoint_number--;
8644
44702360 8645 update_global_location_list_nothrow (UGLL_DONT_INSERT);
e58b0e63
PA
8646 return copy;
8647}
8648
06edf0c0
PA
8649/* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
8650 ORIG is NULL. */
8651
8652struct breakpoint *
8653clone_momentary_breakpoint (struct breakpoint *orig)
8654{
8655 /* If there's nothing to clone, then return nothing. */
8656 if (orig == NULL)
8657 return NULL;
8658
a1aa2221 8659 return momentary_breakpoint_from_master (orig, orig->type, orig->ops, 0);
06edf0c0
PA
8660}
8661
454dafbd 8662breakpoint_up
a6d9a66e
UW
8663set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
8664 enum bptype type)
611c83ae
PA
8665{
8666 struct symtab_and_line sal;
8667
8668 sal = find_pc_line (pc, 0);
8669 sal.pc = pc;
8670 sal.section = find_pc_overlay (pc);
8671 sal.explicit_pc = 1;
8672
a6d9a66e 8673 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
611c83ae 8674}
c906108c 8675\f
c5aa993b 8676
c906108c
SS
8677/* Tell the user we have just set a breakpoint B. */
8678
8679static void
fba45db2 8680mention (struct breakpoint *b)
c906108c 8681{
348d480f 8682 b->ops->print_mention (b);
2d33446d 8683 current_uiout->text ("\n");
c906108c 8684}
c906108c 8685\f
c5aa993b 8686
5133a315 8687static bool bp_loc_is_permanent (struct bp_location *loc);
1a853c52 8688
7f32a4d5
PA
8689/* Handle "set breakpoint auto-hw on".
8690
8691 If the explicitly specified breakpoint type is not hardware
8692 breakpoint, check the memory map to see whether the breakpoint
8693 address is in read-only memory.
8694
8695 - location type is not hardware breakpoint, memory is read-only.
8696 We change the type of the location to hardware breakpoint.
8697
8698 - location type is hardware breakpoint, memory is read-write. This
8699 means we've previously made the location hardware one, but then the
8700 memory map changed, so we undo.
8701*/
8702
8703static void
8704handle_automatic_hardware_breakpoints (bp_location *bl)
8705{
8706 if (automatic_hardware_breakpoints
8707 && bl->owner->type != bp_hardware_breakpoint
8708 && (bl->loc_type == bp_loc_software_breakpoint
8709 || bl->loc_type == bp_loc_hardware_breakpoint))
8710 {
8711 /* When breakpoints are removed, remove_breakpoints will use
8712 location types we've just set here, the only possible problem
8713 is that memory map has changed during running program, but
8714 it's not going to work anyway with current gdb. */
8715 mem_region *mr = lookup_mem_region (bl->address);
8716
8717 if (mr != nullptr)
8718 {
8719 enum bp_loc_type new_type;
8720
8721 if (mr->attrib.mode != MEM_RW)
8722 new_type = bp_loc_hardware_breakpoint;
8723 else
8724 new_type = bp_loc_software_breakpoint;
8725
8726 if (new_type != bl->loc_type)
8727 {
8728 static bool said = false;
8729
8730 bl->loc_type = new_type;
8731 if (!said)
8732 {
8733 fprintf_filtered (gdb_stdout,
8734 _("Note: automatically using "
8735 "hardware breakpoints for "
8736 "read-only addresses.\n"));
8737 said = true;
8738 }
8739 }
8740 }
8741 }
8742}
8743
0d381245 8744static struct bp_location *
39d61571 8745add_location_to_breakpoint (struct breakpoint *b,
0d381245
VP
8746 const struct symtab_and_line *sal)
8747{
8748 struct bp_location *loc, **tmp;
3742cc8b
YQ
8749 CORE_ADDR adjusted_address;
8750 struct gdbarch *loc_gdbarch = get_sal_arch (*sal);
8751
8752 if (loc_gdbarch == NULL)
8753 loc_gdbarch = b->gdbarch;
8754
8755 /* Adjust the breakpoint's address prior to allocating a location.
8756 Once we call allocate_bp_location(), that mostly uninitialized
8757 location will be placed on the location chain. Adjustment of the
8758 breakpoint may cause target_read_memory() to be called and we do
8759 not want its scan of the location chain to find a breakpoint and
8760 location that's only been partially initialized. */
8761 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
8762 sal->pc, b->type);
0d381245 8763
d30113d4 8764 /* Sort the locations by their ADDRESS. */
39d61571 8765 loc = allocate_bp_location (b);
d30113d4
JK
8766 for (tmp = &(b->loc); *tmp != NULL && (*tmp)->address <= adjusted_address;
8767 tmp = &((*tmp)->next))
0d381245 8768 ;
d30113d4 8769 loc->next = *tmp;
0d381245 8770 *tmp = loc;
3742cc8b 8771
0d381245 8772 loc->requested_address = sal->pc;
3742cc8b 8773 loc->address = adjusted_address;
6c95b8df 8774 loc->pspace = sal->pspace;
935676c9 8775 loc->probe.prob = sal->prob;
729662a5 8776 loc->probe.objfile = sal->objfile;
6c95b8df 8777 gdb_assert (loc->pspace != NULL);
0d381245 8778 loc->section = sal->section;
3742cc8b 8779 loc->gdbarch = loc_gdbarch;
f8eba3c6 8780 loc->line_number = sal->line;
2f202fde 8781 loc->symtab = sal->symtab;
4a27f119 8782 loc->symbol = sal->symbol;
3467ec66
PA
8783 loc->msymbol = sal->msymbol;
8784 loc->objfile = sal->objfile;
f8eba3c6 8785
0ba852ab 8786 set_breakpoint_location_function (loc);
1a853c52 8787
6ae88661
LM
8788 /* While by definition, permanent breakpoints are already present in the
8789 code, we don't mark the location as inserted. Normally one would expect
8790 that GDB could rely on that breakpoint instruction to stop the program,
8791 thus removing the need to insert its own breakpoint, except that executing
8792 the breakpoint instruction can kill the target instead of reporting a
8793 SIGTRAP. E.g., on SPARC, when interrupts are disabled, executing the
8794 instruction resets the CPU, so QEMU 2.0.0 for SPARC correspondingly dies
8795 with "Trap 0x02 while interrupts disabled, Error state". Letting the
8796 breakpoint be inserted normally results in QEMU knowing about the GDB
8797 breakpoint, and thus trap before the breakpoint instruction is executed.
8798 (If GDB later needs to continue execution past the permanent breakpoint,
8799 it manually increments the PC, thus avoiding executing the breakpoint
8800 instruction.) */
1a853c52 8801 if (bp_loc_is_permanent (loc))
6ae88661 8802 loc->permanent = 1;
1a853c52 8803
0d381245
VP
8804 return loc;
8805}
514f746b
AR
8806\f
8807
5133a315
LM
8808/* Return true if LOC is pointing to a permanent breakpoint,
8809 return false otherwise. */
1cf4d951 8810
5133a315 8811static bool
1cf4d951
PA
8812bp_loc_is_permanent (struct bp_location *loc)
8813{
514f746b
AR
8814 gdb_assert (loc != NULL);
8815
cb1e4e32
PA
8816 /* If we have a non-breakpoint-backed catchpoint or a software
8817 watchpoint, just return 0. We should not attempt to read from
8818 the addresses the locations of these breakpoint types point to.
5133a315 8819 gdbarch_program_breakpoint_here_p, below, will attempt to read
244558af 8820 memory. */
cb1e4e32 8821 if (!bl_address_is_meaningful (loc))
5133a315 8822 return false;
244558af 8823
5ed8105e 8824 scoped_restore_current_pspace_and_thread restore_pspace_thread;
6c95b8df 8825 switch_to_program_space_and_thread (loc->pspace);
5133a315 8826 return gdbarch_program_breakpoint_here_p (loc->gdbarch, loc->address);
514f746b
AR
8827}
8828
e7e0cddf
SS
8829/* Build a command list for the dprintf corresponding to the current
8830 settings of the dprintf style options. */
8831
8832static void
8833update_dprintf_command_list (struct breakpoint *b)
8834{
8835 char *dprintf_args = b->extra_string;
8836 char *printf_line = NULL;
8837
8838 if (!dprintf_args)
8839 return;
8840
8841 dprintf_args = skip_spaces (dprintf_args);
8842
8843 /* Allow a comma, as it may have terminated a location, but don't
8844 insist on it. */
8845 if (*dprintf_args == ',')
8846 ++dprintf_args;
8847 dprintf_args = skip_spaces (dprintf_args);
8848
8849 if (*dprintf_args != '"')
8850 error (_("Bad format string, missing '\"'."));
8851
d3ce09f5 8852 if (strcmp (dprintf_style, dprintf_style_gdb) == 0)
e7e0cddf 8853 printf_line = xstrprintf ("printf %s", dprintf_args);
d3ce09f5 8854 else if (strcmp (dprintf_style, dprintf_style_call) == 0)
e7e0cddf
SS
8855 {
8856 if (!dprintf_function)
8857 error (_("No function supplied for dprintf call"));
8858
8859 if (dprintf_channel && strlen (dprintf_channel) > 0)
8860 printf_line = xstrprintf ("call (void) %s (%s,%s)",
8861 dprintf_function,
8862 dprintf_channel,
8863 dprintf_args);
8864 else
8865 printf_line = xstrprintf ("call (void) %s (%s)",
8866 dprintf_function,
8867 dprintf_args);
8868 }
d3ce09f5
SS
8869 else if (strcmp (dprintf_style, dprintf_style_agent) == 0)
8870 {
8871 if (target_can_run_breakpoint_commands ())
8872 printf_line = xstrprintf ("agent-printf %s", dprintf_args);
8873 else
8874 {
8875 warning (_("Target cannot run dprintf commands, falling back to GDB printf"));
8876 printf_line = xstrprintf ("printf %s", dprintf_args);
8877 }
8878 }
e7e0cddf
SS
8879 else
8880 internal_error (__FILE__, __LINE__,
8881 _("Invalid dprintf style."));
8882
f28045c2 8883 gdb_assert (printf_line != NULL);
e7e0cddf 8884
12973681
TT
8885 /* Manufacture a printf sequence. */
8886 struct command_line *printf_cmd_line
8887 = new struct command_line (simple_control, printf_line);
8888 breakpoint_set_commands (b, counted_command_line (printf_cmd_line,
8889 command_lines_deleter ()));
e7e0cddf
SS
8890}
8891
8892/* Update all dprintf commands, making their command lists reflect
8893 current style settings. */
8894
8895static void
eb4c3f4a 8896update_dprintf_commands (const char *args, int from_tty,
e7e0cddf
SS
8897 struct cmd_list_element *c)
8898{
8899 struct breakpoint *b;
8900
8901 ALL_BREAKPOINTS (b)
8902 {
8903 if (b->type == bp_dprintf)
8904 update_dprintf_command_list (b);
8905 }
8906}
c3f6f71d 8907
f00aae0f
KS
8908/* Create a breakpoint with SAL as location. Use LOCATION
8909 as a description of the location, and COND_STRING
b35a8b2f
DE
8910 as condition expression. If LOCATION is NULL then create an
8911 "address location" from the address in the SAL. */
018d34a4
VP
8912
8913static void
d9b3f62e 8914init_breakpoint_sal (struct breakpoint *b, struct gdbarch *gdbarch,
6c5b2ebe 8915 gdb::array_view<const symtab_and_line> sals,
ffc2605c 8916 event_location_up &&location,
e1e01040
PA
8917 gdb::unique_xmalloc_ptr<char> filter,
8918 gdb::unique_xmalloc_ptr<char> cond_string,
8919 gdb::unique_xmalloc_ptr<char> extra_string,
d9b3f62e
PA
8920 enum bptype type, enum bpdisp disposition,
8921 int thread, int task, int ignore_count,
c0a91b2b 8922 const struct breakpoint_ops *ops, int from_tty,
44f238bb
PA
8923 int enabled, int internal, unsigned flags,
8924 int display_canonical)
018d34a4 8925{
0d381245 8926 int i;
018d34a4
VP
8927
8928 if (type == bp_hardware_breakpoint)
8929 {
fbbd034e
AS
8930 int target_resources_ok;
8931
8932 i = hw_breakpoint_used_count ();
8933 target_resources_ok =
8934 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
018d34a4
VP
8935 i + 1, 0);
8936 if (target_resources_ok == 0)
8937 error (_("No hardware breakpoint support in the target."));
8938 else if (target_resources_ok < 0)
8939 error (_("Hardware breakpoints used exceeds limit."));
8940 }
8941
6c5b2ebe 8942 gdb_assert (!sals.empty ());
6c95b8df 8943
6c5b2ebe 8944 for (const auto &sal : sals)
0d381245 8945 {
0d381245
VP
8946 struct bp_location *loc;
8947
8948 if (from_tty)
5af949e3
UW
8949 {
8950 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
8951 if (!loc_gdbarch)
8952 loc_gdbarch = gdbarch;
8953
8954 describe_other_breakpoints (loc_gdbarch,
6c95b8df 8955 sal.pspace, sal.pc, sal.section, thread);
5af949e3 8956 }
0d381245 8957
6c5b2ebe 8958 if (&sal == &sals[0])
0d381245 8959 {
d9b3f62e 8960 init_raw_breakpoint (b, gdbarch, sal, type, ops);
0d381245 8961 b->thread = thread;
4a306c9a 8962 b->task = task;
855a6e68 8963
e1e01040
PA
8964 b->cond_string = cond_string.release ();
8965 b->extra_string = extra_string.release ();
0d381245 8966 b->ignore_count = ignore_count;
41447f92 8967 b->enable_state = enabled ? bp_enabled : bp_disabled;
0d381245 8968 b->disposition = disposition;
6c95b8df 8969
44f238bb
PA
8970 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
8971 b->loc->inserted = 1;
8972
0fb4aa4b
PA
8973 if (type == bp_static_tracepoint)
8974 {
d9b3f62e 8975 struct tracepoint *t = (struct tracepoint *) b;
0fb4aa4b
PA
8976 struct static_tracepoint_marker marker;
8977
983af33b 8978 if (strace_marker_p (b))
0fb4aa4b
PA
8979 {
8980 /* We already know the marker exists, otherwise, we
8981 wouldn't see a sal for it. */
d28cd78a
TT
8982 const char *p
8983 = &event_location_to_string (b->location.get ())[3];
f00aae0f 8984 const char *endp;
0fb4aa4b 8985
f1735a53 8986 p = skip_spaces (p);
0fb4aa4b 8987
f1735a53 8988 endp = skip_to_space (p);
0fb4aa4b 8989
5d9310c4 8990 t->static_trace_marker_id.assign (p, endp - p);
0fb4aa4b 8991
3e43a32a
MS
8992 printf_filtered (_("Probed static tracepoint "
8993 "marker \"%s\"\n"),
5d9310c4 8994 t->static_trace_marker_id.c_str ());
0fb4aa4b
PA
8995 }
8996 else if (target_static_tracepoint_marker_at (sal.pc, &marker))
8997 {
5d9310c4 8998 t->static_trace_marker_id = std::move (marker.str_id);
0fb4aa4b 8999
3e43a32a
MS
9000 printf_filtered (_("Probed static tracepoint "
9001 "marker \"%s\"\n"),
5d9310c4 9002 t->static_trace_marker_id.c_str ());
0fb4aa4b
PA
9003 }
9004 else
3e43a32a
MS
9005 warning (_("Couldn't determine the static "
9006 "tracepoint marker to probe"));
0fb4aa4b
PA
9007 }
9008
0d381245
VP
9009 loc = b->loc;
9010 }
9011 else
018d34a4 9012 {
39d61571 9013 loc = add_location_to_breakpoint (b, &sal);
44f238bb
PA
9014 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9015 loc->inserted = 1;
0d381245
VP
9016 }
9017
b5fa468f
TBA
9018 /* Do not set breakpoint locations conditions yet. As locations
9019 are inserted, they get sorted based on their addresses. Let
9020 the list stabilize to have reliable location numbers. */
e7e0cddf
SS
9021
9022 /* Dynamic printf requires and uses additional arguments on the
9023 command line, otherwise it's an error. */
9024 if (type == bp_dprintf)
9025 {
9026 if (b->extra_string)
9027 update_dprintf_command_list (b);
9028 else
9029 error (_("Format string required"));
9030 }
9031 else if (b->extra_string)
588ae58c 9032 error (_("Garbage '%s' at end of command"), b->extra_string);
855a6e68 9033 }
018d34a4 9034
b5fa468f
TBA
9035
9036 /* The order of the locations is now stable. Set the location
9037 condition using the location's number. */
9038 int loc_num = 1;
9039 for (bp_location *loc = b->loc; loc != nullptr; loc = loc->next)
9040 {
9041 if (b->cond_string != nullptr)
9042 set_breakpoint_location_condition (b->cond_string, loc, b->number,
9043 loc_num);
9044
9045 ++loc_num;
9046 }
9047
56435ebe 9048 b->display_canonical = display_canonical;
f00aae0f 9049 if (location != NULL)
d28cd78a 9050 b->location = std::move (location);
018d34a4 9051 else
d28cd78a 9052 b->location = new_address_location (b->loc->address, NULL, 0);
c0e8dcd8 9053 b->filter = std::move (filter);
d9b3f62e 9054}
018d34a4 9055
d9b3f62e
PA
9056static void
9057create_breakpoint_sal (struct gdbarch *gdbarch,
6c5b2ebe 9058 gdb::array_view<const symtab_and_line> sals,
ffc2605c 9059 event_location_up &&location,
e1e01040
PA
9060 gdb::unique_xmalloc_ptr<char> filter,
9061 gdb::unique_xmalloc_ptr<char> cond_string,
9062 gdb::unique_xmalloc_ptr<char> extra_string,
d9b3f62e
PA
9063 enum bptype type, enum bpdisp disposition,
9064 int thread, int task, int ignore_count,
c0a91b2b 9065 const struct breakpoint_ops *ops, int from_tty,
44f238bb
PA
9066 int enabled, int internal, unsigned flags,
9067 int display_canonical)
d9b3f62e 9068{
a5e364af 9069 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (type);
d9b3f62e 9070
a5e364af 9071 init_breakpoint_sal (b.get (), gdbarch,
ffc2605c 9072 sals, std::move (location),
e1e01040
PA
9073 std::move (filter),
9074 std::move (cond_string),
9075 std::move (extra_string),
d9b3f62e
PA
9076 type, disposition,
9077 thread, task, ignore_count,
9078 ops, from_tty,
44f238bb
PA
9079 enabled, internal, flags,
9080 display_canonical);
d9b3f62e 9081
b270e6f9 9082 install_breakpoint (internal, std::move (b), 0);
018d34a4
VP
9083}
9084
9085/* Add SALS.nelts breakpoints to the breakpoint table. For each
9086 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
9087 value. COND_STRING, if not NULL, specified the condition to be
9088 used for all breakpoints. Essentially the only case where
9089 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
9090 function. In that case, it's still not possible to specify
9091 separate conditions for different overloaded functions, so
9092 we take just a single condition string.
9093
c3f6f71d 9094 NOTE: If the function succeeds, the caller is expected to cleanup
018d34a4 9095 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
c3f6f71d
JM
9096 array contents). If the function fails (error() is called), the
9097 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
4a64f543 9098 COND and SALS arrays and each of those arrays contents. */
c906108c
SS
9099
9100static void
8cdf0e15 9101create_breakpoints_sal (struct gdbarch *gdbarch,
7efd8fc2 9102 struct linespec_result *canonical,
e1e01040
PA
9103 gdb::unique_xmalloc_ptr<char> cond_string,
9104 gdb::unique_xmalloc_ptr<char> extra_string,
8cdf0e15
VP
9105 enum bptype type, enum bpdisp disposition,
9106 int thread, int task, int ignore_count,
c0a91b2b 9107 const struct breakpoint_ops *ops, int from_tty,
44f238bb 9108 int enabled, int internal, unsigned flags)
c906108c 9109{
f8eba3c6 9110 if (canonical->pre_expanded)
6c5b2ebe 9111 gdb_assert (canonical->lsals.size () == 1);
f8eba3c6 9112
6c5b2ebe 9113 for (const auto &lsal : canonical->lsals)
c3f6f71d 9114 {
f00aae0f 9115 /* Note that 'location' can be NULL in the case of a plain
f8eba3c6 9116 'break', without arguments. */
ffc2605c 9117 event_location_up location
f00aae0f 9118 = (canonical->location != NULL
8e9e35b1 9119 ? copy_event_location (canonical->location.get ()) : NULL);
e1e01040 9120 gdb::unique_xmalloc_ptr<char> filter_string
6c5b2ebe 9121 (lsal.canonical != NULL ? xstrdup (lsal.canonical) : NULL);
0d381245 9122
6c5b2ebe 9123 create_breakpoint_sal (gdbarch, lsal.sals,
ffc2605c 9124 std::move (location),
e1e01040
PA
9125 std::move (filter_string),
9126 std::move (cond_string),
9127 std::move (extra_string),
e7e0cddf 9128 type, disposition,
84f4c1fe 9129 thread, task, ignore_count, ops,
44f238bb 9130 from_tty, enabled, internal, flags,
56435ebe 9131 canonical->special_display);
c3f6f71d 9132 }
c3f6f71d 9133}
c906108c 9134
f00aae0f 9135/* Parse LOCATION which is assumed to be a SAL specification possibly
c3f6f71d 9136 followed by conditionals. On return, SALS contains an array of SAL
f00aae0f
KS
9137 addresses found. LOCATION points to the end of the SAL (for
9138 linespec locations).
9998af43
TJB
9139
9140 The array and the line spec strings are allocated on the heap, it is
9141 the caller's responsibility to free them. */
c906108c 9142
b9362cc7 9143static void
626d2320 9144parse_breakpoint_sals (struct event_location *location,
58438ac1 9145 struct linespec_result *canonical)
c3f6f71d 9146{
f00aae0f
KS
9147 struct symtab_and_line cursal;
9148
9149 if (event_location_type (location) == LINESPEC_LOCATION)
9150 {
a20714ff 9151 const char *spec = get_linespec_location (location)->spec_string;
f00aae0f 9152
a20714ff 9153 if (spec == NULL)
f00aae0f
KS
9154 {
9155 /* The last displayed codepoint, if it's valid, is our default
9156 breakpoint address. */
9157 if (last_displayed_sal_is_valid ())
9158 {
f00aae0f
KS
9159 /* Set sal's pspace, pc, symtab, and line to the values
9160 corresponding to the last call to print_frame_info.
9161 Be sure to reinitialize LINE with NOTCURRENT == 0
9162 as the breakpoint line number is inappropriate otherwise.
9163 find_pc_line would adjust PC, re-set it back. */
51abb421
PA
9164 symtab_and_line sal = get_last_displayed_sal ();
9165 CORE_ADDR pc = sal.pc;
9166
f00aae0f
KS
9167 sal = find_pc_line (pc, 0);
9168
9169 /* "break" without arguments is equivalent to "break *PC"
9170 where PC is the last displayed codepoint's address. So
9171 make sure to set sal.explicit_pc to prevent GDB from
9172 trying to expand the list of sals to include all other
9173 instances with the same symtab and line. */
9174 sal.pc = pc;
9175 sal.explicit_pc = 1;
9176
6c5b2ebe
PA
9177 struct linespec_sals lsal;
9178 lsal.sals = {sal};
f00aae0f
KS
9179 lsal.canonical = NULL;
9180
6c5b2ebe 9181 canonical->lsals.push_back (std::move (lsal));
f00aae0f
KS
9182 return;
9183 }
9184 else
9185 error (_("No default breakpoint address now."));
c906108c 9186 }
c906108c 9187 }
f00aae0f
KS
9188
9189 /* Force almost all breakpoints to be in terms of the
9190 current_source_symtab (which is decode_line_1's default).
9191 This should produce the results we want almost all of the
9192 time while leaving default_breakpoint_* alone.
9193
9194 ObjC: However, don't match an Objective-C method name which
9195 may have a '+' or '-' succeeded by a '['. */
9196 cursal = get_current_source_symtab_and_line ();
9197 if (last_displayed_sal_is_valid ())
c906108c 9198 {
a20714ff 9199 const char *spec = NULL;
cc80f267 9200
f00aae0f 9201 if (event_location_type (location) == LINESPEC_LOCATION)
a20714ff 9202 spec = get_linespec_location (location)->spec_string;
cc80f267 9203
f00aae0f 9204 if (!cursal.symtab
a20714ff
PA
9205 || (spec != NULL
9206 && strchr ("+-", spec[0]) != NULL
9207 && spec[1] != '['))
f00aae0f 9208 {
c2f4122d 9209 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
f00aae0f
KS
9210 get_last_displayed_symtab (),
9211 get_last_displayed_line (),
9212 canonical, NULL, NULL);
9213 return;
9214 }
c906108c 9215 }
f00aae0f 9216
c2f4122d 9217 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
f00aae0f 9218 cursal.symtab, cursal.line, canonical, NULL, NULL);
c3f6f71d 9219}
c906108c 9220
c906108c 9221
c3f6f71d 9222/* Convert each SAL into a real PC. Verify that the PC can be
4a64f543 9223 inserted as a breakpoint. If it can't throw an error. */
c906108c 9224
b9362cc7 9225static void
6c5b2ebe 9226breakpoint_sals_to_pc (std::vector<symtab_and_line> &sals)
c3f6f71d 9227{
6c5b2ebe
PA
9228 for (auto &sal : sals)
9229 resolve_sal_pc (&sal);
c3f6f71d
JM
9230}
9231
7a697b8d
SS
9232/* Fast tracepoints may have restrictions on valid locations. For
9233 instance, a fast tracepoint using a jump instead of a trap will
9234 likely have to overwrite more bytes than a trap would, and so can
9235 only be placed where the instruction is longer than the jump, or a
9236 multi-instruction sequence does not have a jump into the middle of
9237 it, etc. */
9238
9239static void
9240check_fast_tracepoint_sals (struct gdbarch *gdbarch,
6c5b2ebe 9241 gdb::array_view<const symtab_and_line> sals)
7a697b8d 9242{
6c5b2ebe 9243 for (const auto &sal : sals)
7a697b8d 9244 {
f8eba3c6
TT
9245 struct gdbarch *sarch;
9246
6c5b2ebe 9247 sarch = get_sal_arch (sal);
f8eba3c6
TT
9248 /* We fall back to GDBARCH if there is no architecture
9249 associated with SAL. */
9250 if (sarch == NULL)
9251 sarch = gdbarch;
281d762b
TT
9252 std::string msg;
9253 if (!gdbarch_fast_tracepoint_valid_at (sarch, sal.pc, &msg))
53c3572a 9254 error (_("May not have a fast tracepoint at %s%s"),
281d762b 9255 paddress (sarch, sal.pc), msg.c_str ());
7a697b8d
SS
9256 }
9257}
9258
018d34a4
VP
9259/* Given TOK, a string specification of condition and thread, as
9260 accepted by the 'break' command, extract the condition
9261 string and thread number and set *COND_STRING and *THREAD.
4a64f543 9262 PC identifies the context at which the condition should be parsed.
018d34a4
VP
9263 If no condition is found, *COND_STRING is set to NULL.
9264 If no thread is found, *THREAD is set to -1. */
d634f2de
JB
9265
9266static void
bbc13ae3 9267find_condition_and_thread (const char *tok, CORE_ADDR pc,
e7e0cddf
SS
9268 char **cond_string, int *thread, int *task,
9269 char **rest)
018d34a4
VP
9270{
9271 *cond_string = NULL;
9272 *thread = -1;
ed1d1739
KS
9273 *task = 0;
9274 *rest = NULL;
733d554a 9275 bool force = false;
ed1d1739 9276
018d34a4
VP
9277 while (tok && *tok)
9278 {
bbc13ae3 9279 const char *end_tok;
018d34a4 9280 int toklen;
bbc13ae3
KS
9281 const char *cond_start = NULL;
9282 const char *cond_end = NULL;
cc59ec59 9283
f1735a53 9284 tok = skip_spaces (tok);
e7e0cddf
SS
9285
9286 if ((*tok == '"' || *tok == ',') && rest)
9287 {
9288 *rest = savestring (tok, strlen (tok));
9289 return;
9290 }
9291
f1735a53 9292 end_tok = skip_to_space (tok);
d634f2de 9293
018d34a4 9294 toklen = end_tok - tok;
d634f2de 9295
018d34a4
VP
9296 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
9297 {
9298 tok = cond_start = end_tok + 1;
733d554a
TBA
9299 try
9300 {
9301 parse_exp_1 (&tok, pc, block_for_pc (pc), 0);
9302 }
9303 catch (const gdb_exception_error &)
9304 {
9305 if (!force)
9306 throw;
9307 else
9308 tok = tok + strlen (tok);
9309 }
018d34a4 9310 cond_end = tok;
d634f2de 9311 *cond_string = savestring (cond_start, cond_end - cond_start);
018d34a4 9312 }
733d554a
TBA
9313 else if (toklen >= 1 && strncmp (tok, "-force-condition", toklen) == 0)
9314 {
5759831a 9315 tok = tok + toklen;
733d554a
TBA
9316 force = true;
9317 }
018d34a4
VP
9318 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
9319 {
5d5658a1
PA
9320 const char *tmptok;
9321 struct thread_info *thr;
d634f2de 9322
018d34a4 9323 tok = end_tok + 1;
5d5658a1 9324 thr = parse_thread_id (tok, &tmptok);
018d34a4
VP
9325 if (tok == tmptok)
9326 error (_("Junk after thread keyword."));
5d5658a1 9327 *thread = thr->global_num;
bbc13ae3 9328 tok = tmptok;
018d34a4 9329 }
4a306c9a
JB
9330 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
9331 {
9332 char *tmptok;
9333
9334 tok = end_tok + 1;
bbc13ae3 9335 *task = strtol (tok, &tmptok, 0);
4a306c9a
JB
9336 if (tok == tmptok)
9337 error (_("Junk after task keyword."));
9338 if (!valid_task_id (*task))
b6199126 9339 error (_("Unknown task %d."), *task);
bbc13ae3 9340 tok = tmptok;
4a306c9a 9341 }
e7e0cddf
SS
9342 else if (rest)
9343 {
9344 *rest = savestring (tok, strlen (tok));
ccab2054 9345 return;
e7e0cddf 9346 }
018d34a4
VP
9347 else
9348 error (_("Junk at end of arguments."));
9349 }
9350}
9351
b5fa468f
TBA
9352/* Call 'find_condition_and_thread' for each sal in SALS until a parse
9353 succeeds. The parsed values are written to COND_STRING, THREAD,
9354 TASK, and REST. See the comment of 'find_condition_and_thread'
9355 for the description of these parameters and INPUT. */
9356
9357static void
9358find_condition_and_thread_for_sals (const std::vector<symtab_and_line> &sals,
9359 const char *input, char **cond_string,
9360 int *thread, int *task, char **rest)
9361{
9362 int num_failures = 0;
9363 for (auto &sal : sals)
9364 {
9365 char *cond = nullptr;
9366 int thread_id = 0;
9367 int task_id = 0;
9368 char *remaining = nullptr;
9369
9370 /* Here we want to parse 'arg' to separate condition from thread
9371 number. But because parsing happens in a context and the
9372 contexts of sals might be different, try each until there is
9373 success. Finding one successful parse is sufficient for our
9374 goal. When setting the breakpoint we'll re-parse the
9375 condition in the context of each sal. */
9376 try
9377 {
9378 find_condition_and_thread (input, sal.pc, &cond, &thread_id,
9379 &task_id, &remaining);
9380 *cond_string = cond;
9381 *thread = thread_id;
9382 *task = task_id;
9383 *rest = remaining;
9384 break;
9385 }
9386 catch (const gdb_exception_error &e)
9387 {
9388 num_failures++;
9389 /* If no sal remains, do not continue. */
9390 if (num_failures == sals.size ())
9391 throw;
9392 }
9393 }
9394}
9395
0fb4aa4b
PA
9396/* Decode a static tracepoint marker spec. */
9397
6c5b2ebe 9398static std::vector<symtab_and_line>
f00aae0f 9399decode_static_tracepoint_spec (const char **arg_p)
0fb4aa4b 9400{
f00aae0f
KS
9401 const char *p = &(*arg_p)[3];
9402 const char *endp;
0fb4aa4b 9403
f1735a53 9404 p = skip_spaces (p);
0fb4aa4b 9405
f1735a53 9406 endp = skip_to_space (p);
0fb4aa4b 9407
81b1e71c 9408 std::string marker_str (p, endp - p);
0fb4aa4b 9409
5d9310c4
SM
9410 std::vector<static_tracepoint_marker> markers
9411 = target_static_tracepoint_markers_by_strid (marker_str.c_str ());
9412 if (markers.empty ())
81b1e71c
TT
9413 error (_("No known static tracepoint marker named %s"),
9414 marker_str.c_str ());
0fb4aa4b 9415
6c5b2ebe 9416 std::vector<symtab_and_line> sals;
5d9310c4 9417 sals.reserve (markers.size ());
0fb4aa4b 9418
5d9310c4 9419 for (const static_tracepoint_marker &marker : markers)
0fb4aa4b 9420 {
5d9310c4
SM
9421 symtab_and_line sal = find_pc_line (marker.address, 0);
9422 sal.pc = marker.address;
6c5b2ebe 9423 sals.push_back (sal);
5d9310c4 9424 }
0fb4aa4b 9425
0fb4aa4b
PA
9426 *arg_p = endp;
9427 return sals;
9428}
9429
bac7c5cf
GB
9430/* Returns the breakpoint ops appropriate for use with with LOCATION_TYPE and
9431 according to IS_TRACEPOINT. */
9432
9433static const struct breakpoint_ops *
9434breakpoint_ops_for_event_location_type (enum event_location_type location_type,
9435 bool is_tracepoint)
9436{
9437 if (is_tracepoint)
9438 {
9439 if (location_type == PROBE_LOCATION)
9440 return &tracepoint_probe_breakpoint_ops;
9441 else
9442 return &tracepoint_breakpoint_ops;
9443 }
9444 else
9445 {
9446 if (location_type == PROBE_LOCATION)
9447 return &bkpt_probe_breakpoint_ops;
9448 else
9449 return &bkpt_breakpoint_ops;
9450 }
9451}
9452
9453/* See breakpoint.h. */
9454
9455const struct breakpoint_ops *
9456breakpoint_ops_for_event_location (const struct event_location *location,
9457 bool is_tracepoint)
9458{
9459 if (location != nullptr)
9460 return breakpoint_ops_for_event_location_type
9461 (event_location_type (location), is_tracepoint);
9462 return is_tracepoint ? &tracepoint_breakpoint_ops : &bkpt_breakpoint_ops;
9463}
9464
f00aae0f 9465/* See breakpoint.h. */
0101ce28 9466
8cdf0e15
VP
9467int
9468create_breakpoint (struct gdbarch *gdbarch,
626d2320 9469 struct event_location *location,
e1e01040
PA
9470 const char *cond_string,
9471 int thread, const char *extra_string,
10a636cc 9472 bool force_condition, int parse_extra,
0fb4aa4b 9473 int tempflag, enum bptype type_wanted,
8cdf0e15
VP
9474 int ignore_count,
9475 enum auto_boolean pending_break_support,
c0a91b2b 9476 const struct breakpoint_ops *ops,
44f238bb
PA
9477 int from_tty, int enabled, int internal,
9478 unsigned flags)
c3f6f71d 9479{
7efd8fc2 9480 struct linespec_result canonical;
0101ce28 9481 int pending = 0;
4a306c9a 9482 int task = 0;
86b17b60 9483 int prev_bkpt_count = breakpoint_count;
c3f6f71d 9484
348d480f
PA
9485 gdb_assert (ops != NULL);
9486
f00aae0f
KS
9487 /* If extra_string isn't useful, set it to NULL. */
9488 if (extra_string != NULL && *extra_string == '\0')
9489 extra_string = NULL;
9490
a70b8144 9491 try
b78a6381 9492 {
f00aae0f 9493 ops->create_sals_from_location (location, &canonical, type_wanted);
b78a6381 9494 }
230d2906 9495 catch (const gdb_exception_error &e)
0101ce28 9496 {
492d29ea
PA
9497 /* If caller is interested in rc value from parse, set
9498 value. */
9499 if (e.error == NOT_FOUND_ERROR)
0101ce28 9500 {
05ff989b
AC
9501 /* If pending breakpoint support is turned off, throw
9502 error. */
fa8d40ab
JJ
9503
9504 if (pending_break_support == AUTO_BOOLEAN_FALSE)
eedc3f4f 9505 throw;
723a2275
VP
9506
9507 exception_print (gdb_stderr, e);
fa8d40ab 9508
dda83cd7 9509 /* If pending breakpoint support is auto query and the user
05ff989b 9510 selects no, then simply return the error code. */
059fb39f 9511 if (pending_break_support == AUTO_BOOLEAN_AUTO
bfccc43c
YQ
9512 && !nquery (_("Make %s pending on future shared library load? "),
9513 bptype_string (type_wanted)))
fd9b8c24 9514 return 0;
fa8d40ab 9515
05ff989b
AC
9516 /* At this point, either the user was queried about setting
9517 a pending breakpoint and selected yes, or pending
9518 breakpoint behavior is on and thus a pending breakpoint
9519 is defaulted on behalf of the user. */
f00aae0f 9520 pending = 1;
0101ce28 9521 }
492d29ea 9522 else
eedc3f4f 9523 throw;
0101ce28 9524 }
492d29ea 9525
6c5b2ebe 9526 if (!pending && canonical.lsals.empty ())
492d29ea 9527 return 0;
c3f6f71d 9528
c3f6f71d
JM
9529 /* Resolve all line numbers to PC's and verify that the addresses
9530 are ok for the target. */
0101ce28 9531 if (!pending)
f8eba3c6 9532 {
6c5b2ebe
PA
9533 for (auto &lsal : canonical.lsals)
9534 breakpoint_sals_to_pc (lsal.sals);
f8eba3c6 9535 }
c3f6f71d 9536
7a697b8d 9537 /* Fast tracepoints may have additional restrictions on location. */
bfccc43c 9538 if (!pending && type_wanted == bp_fast_tracepoint)
f8eba3c6 9539 {
6c5b2ebe
PA
9540 for (const auto &lsal : canonical.lsals)
9541 check_fast_tracepoint_sals (gdbarch, lsal.sals);
f8eba3c6 9542 }
7a697b8d 9543
c3f6f71d
JM
9544 /* Verify that condition can be parsed, before setting any
9545 breakpoints. Allocate a separate condition expression for each
4a64f543 9546 breakpoint. */
0101ce28 9547 if (!pending)
c3f6f71d 9548 {
e1e01040
PA
9549 gdb::unique_xmalloc_ptr<char> cond_string_copy;
9550 gdb::unique_xmalloc_ptr<char> extra_string_copy;
9551
f00aae0f 9552 if (parse_extra)
dda83cd7 9553 {
0878d0fa 9554 char *rest;
e1e01040 9555 char *cond;
52d361e1 9556
6c5b2ebe 9557 const linespec_sals &lsal = canonical.lsals[0];
52d361e1 9558
b5fa468f
TBA
9559 find_condition_and_thread_for_sals (lsal.sals, extra_string,
9560 &cond, &thread, &task, &rest);
e1e01040
PA
9561 cond_string_copy.reset (cond);
9562 extra_string_copy.reset (rest);
dda83cd7 9563 }
2f069f6f 9564 else
dda83cd7 9565 {
f00aae0f
KS
9566 if (type_wanted != bp_dprintf
9567 && extra_string != NULL && *extra_string != '\0')
9568 error (_("Garbage '%s' at end of location"), extra_string);
0878d0fa 9569
10a636cc
TBA
9570 /* Check the validity of the condition. We should error out
9571 if the condition is invalid at all of the locations and
9572 if it is not forced. In the PARSE_EXTRA case above, this
9573 check is done when parsing the EXTRA_STRING. */
9574 if (cond_string != nullptr && !force_condition)
9575 {
9576 int num_failures = 0;
9577 const linespec_sals &lsal = canonical.lsals[0];
9578 for (const auto &sal : lsal.sals)
9579 {
9580 const char *cond = cond_string;
9581 try
9582 {
9583 parse_exp_1 (&cond, sal.pc, block_for_pc (sal.pc), 0);
9584 /* One success is sufficient to keep going. */
9585 break;
9586 }
9587 catch (const gdb_exception_error &)
9588 {
9589 num_failures++;
9590 /* If this is the last sal, error out. */
9591 if (num_failures == lsal.sals.size ())
9592 throw;
9593 }
9594 }
9595 }
9596
0878d0fa
YQ
9597 /* Create a private copy of condition string. */
9598 if (cond_string)
e1e01040 9599 cond_string_copy.reset (xstrdup (cond_string));
0878d0fa
YQ
9600 /* Create a private copy of any extra string. */
9601 if (extra_string)
e1e01040 9602 extra_string_copy.reset (xstrdup (extra_string));
dda83cd7 9603 }
0fb4aa4b 9604
52d361e1 9605 ops->create_breakpoints_sal (gdbarch, &canonical,
e1e01040
PA
9606 std::move (cond_string_copy),
9607 std::move (extra_string_copy),
9608 type_wanted,
d9b3f62e
PA
9609 tempflag ? disp_del : disp_donttouch,
9610 thread, task, ignore_count, ops,
44f238bb 9611 from_tty, enabled, internal, flags);
c906108c 9612 }
0101ce28
JJ
9613 else
9614 {
a5e364af 9615 std::unique_ptr <breakpoint> b = new_breakpoint_from_type (type_wanted);
bfccc43c 9616
a5e364af 9617 init_raw_breakpoint_without_location (b.get (), gdbarch, type_wanted, ops);
d28cd78a 9618 b->location = copy_event_location (location);
bfccc43c 9619
f00aae0f
KS
9620 if (parse_extra)
9621 b->cond_string = NULL;
e12c7713
MK
9622 else
9623 {
9624 /* Create a private copy of condition string. */
e1e01040 9625 b->cond_string = cond_string != NULL ? xstrdup (cond_string) : NULL;
15630549 9626 b->thread = thread;
e12c7713 9627 }
f00aae0f
KS
9628
9629 /* Create a private copy of any extra string. */
e1e01040 9630 b->extra_string = extra_string != NULL ? xstrdup (extra_string) : NULL;
0101ce28 9631 b->ignore_count = ignore_count;
0101ce28 9632 b->disposition = tempflag ? disp_del : disp_donttouch;
0d381245 9633 b->condition_not_parsed = 1;
41447f92 9634 b->enable_state = enabled ? bp_enabled : bp_disabled;
cc72b2a2 9635 if ((type_wanted != bp_breakpoint
dda83cd7 9636 && type_wanted != bp_hardware_breakpoint) || thread != -1)
f8eba3c6 9637 b->pspace = current_program_space;
8bea4e01 9638
b270e6f9 9639 install_breakpoint (internal, std::move (b), 0);
0101ce28
JJ
9640 }
9641
6c5b2ebe 9642 if (canonical.lsals.size () > 1)
95a42b64 9643 {
3e43a32a
MS
9644 warning (_("Multiple breakpoints were set.\nUse the "
9645 "\"delete\" command to delete unwanted breakpoints."));
86b17b60 9646 prev_breakpoint_count = prev_bkpt_count;
95a42b64
TT
9647 }
9648
44702360 9649 update_global_location_list (UGLL_MAY_INSERT);
fd9b8c24
PA
9650
9651 return 1;
c3f6f71d 9652}
c906108c 9653
348d480f 9654/* Set a breakpoint.
72b2ff0e
VP
9655 ARG is a string describing breakpoint address,
9656 condition, and thread.
9657 FLAG specifies if a breakpoint is hardware on,
9658 and if breakpoint is temporary, using BP_HARDWARE_FLAG
9659 and BP_TEMPFLAG. */
348d480f 9660
98deb0da 9661static void
f2fc3015 9662break_command_1 (const char *arg, int flag, int from_tty)
c3f6f71d 9663{
72b2ff0e 9664 int tempflag = flag & BP_TEMPFLAG;
0fb4aa4b
PA
9665 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
9666 ? bp_hardware_breakpoint
9667 : bp_breakpoint);
f00aae0f 9668
ffc2605c 9669 event_location_up location = string_to_event_location (&arg, current_language);
bac7c5cf
GB
9670 const struct breakpoint_ops *ops = breakpoint_ops_for_event_location
9671 (location.get (), false /* is_tracepoint */);
c3f6f71d 9672
8cdf0e15 9673 create_breakpoint (get_current_arch (),
ffc2605c 9674 location.get (),
10a636cc 9675 NULL, 0, arg, false, 1 /* parse arg */,
0fb4aa4b 9676 tempflag, type_wanted,
8cdf0e15
VP
9677 0 /* Ignore count */,
9678 pending_break_support,
55aa24fb 9679 ops,
8cdf0e15 9680 from_tty,
84f4c1fe 9681 1 /* enabled */,
44f238bb
PA
9682 0 /* internal */,
9683 0);
c906108c
SS
9684}
9685
c906108c
SS
9686/* Helper function for break_command_1 and disassemble_command. */
9687
9688void
fba45db2 9689resolve_sal_pc (struct symtab_and_line *sal)
c906108c
SS
9690{
9691 CORE_ADDR pc;
9692
9693 if (sal->pc == 0 && sal->symtab != NULL)
9694 {
9695 if (!find_line_pc (sal->symtab, sal->line, &pc))
8a3fe4f8 9696 error (_("No line %d in file \"%s\"."),
05cba821 9697 sal->line, symtab_to_filename_for_display (sal->symtab));
c906108c 9698 sal->pc = pc;
6a048695 9699
4a64f543 9700 /* If this SAL corresponds to a breakpoint inserted using a line
dda83cd7 9701 number, then skip the function prologue if necessary. */
6a048695 9702 if (sal->explicit_line)
059acae7 9703 skip_prologue_sal (sal);
c906108c
SS
9704 }
9705
9706 if (sal->section == 0 && sal->symtab != NULL)
9707 {
346d1dfe 9708 const struct blockvector *bv;
3977b71f 9709 const struct block *b;
c5aa993b 9710 struct symbol *sym;
c906108c 9711
43f3e411
DE
9712 bv = blockvector_for_pc_sect (sal->pc, 0, &b,
9713 SYMTAB_COMPUNIT (sal->symtab));
c906108c
SS
9714 if (bv != NULL)
9715 {
7f0df278 9716 sym = block_linkage_function (b);
c906108c
SS
9717 if (sym != NULL)
9718 {
eb822aa6 9719 fixup_symbol_section (sym, SYMTAB_OBJFILE (sal->symtab));
ebbc3a7d 9720 sal->section = sym->obj_section (SYMTAB_OBJFILE (sal->symtab));
c906108c
SS
9721 }
9722 else
9723 {
4a64f543 9724 /* It really is worthwhile to have the section, so we'll
dda83cd7
SM
9725 just have to look harder. This case can be executed
9726 if we have line numbers but no functions (as can
9727 happen in assembly source). */
c906108c 9728
5ed8105e 9729 scoped_restore_current_pspace_and_thread restore_pspace_thread;
6c95b8df 9730 switch_to_program_space_and_thread (sal->pspace);
c906108c 9731
5ed8105e 9732 bound_minimal_symbol msym = lookup_minimal_symbol_by_pc (sal->pc);
7cbd4a93 9733 if (msym.minsym)
1db66e34 9734 sal->section = msym.obj_section ();
c906108c
SS
9735 }
9736 }
9737 }
9738}
9739
9740void
0b39b52e 9741break_command (const char *arg, int from_tty)
c906108c 9742{
db107f19 9743 break_command_1 (arg, 0, from_tty);
c906108c
SS
9744}
9745
c906108c 9746void
0b39b52e 9747tbreak_command (const char *arg, int from_tty)
c906108c 9748{
db107f19 9749 break_command_1 (arg, BP_TEMPFLAG, from_tty);
c906108c
SS
9750}
9751
c906108c 9752static void
0b39b52e 9753hbreak_command (const char *arg, int from_tty)
c906108c 9754{
db107f19 9755 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
c906108c
SS
9756}
9757
9758static void
0b39b52e 9759thbreak_command (const char *arg, int from_tty)
c906108c 9760{
db107f19 9761 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
c906108c
SS
9762}
9763
9764static void
ee7ddd71 9765stop_command (const char *arg, int from_tty)
c906108c 9766{
a3f17187 9767 printf_filtered (_("Specify the type of breakpoint to set.\n\
c906108c 9768Usage: stop in <function | address>\n\
a3f17187 9769 stop at <line>\n"));
c906108c
SS
9770}
9771
9772static void
4495129a 9773stopin_command (const char *arg, int from_tty)
c906108c
SS
9774{
9775 int badInput = 0;
9776
cafb3438 9777 if (arg == NULL)
c906108c
SS
9778 badInput = 1;
9779 else if (*arg != '*')
9780 {
4495129a 9781 const char *argptr = arg;
c906108c
SS
9782 int hasColon = 0;
9783
4a64f543 9784 /* Look for a ':'. If this is a line number specification, then
dda83cd7
SM
9785 say it is bad, otherwise, it should be an address or
9786 function/method name. */
c906108c 9787 while (*argptr && !hasColon)
c5aa993b
JM
9788 {
9789 hasColon = (*argptr == ':');
9790 argptr++;
9791 }
c906108c
SS
9792
9793 if (hasColon)
c5aa993b 9794 badInput = (*argptr != ':'); /* Not a class::method */
c906108c 9795 else
c5aa993b 9796 badInput = isdigit (*arg); /* a simple line number */
c906108c
SS
9797 }
9798
9799 if (badInput)
a3f17187 9800 printf_filtered (_("Usage: stop in <function | address>\n"));
c906108c 9801 else
db107f19 9802 break_command_1 (arg, 0, from_tty);
c906108c
SS
9803}
9804
9805static void
4495129a 9806stopat_command (const char *arg, int from_tty)
c906108c
SS
9807{
9808 int badInput = 0;
9809
cafb3438 9810 if (arg == NULL || *arg == '*') /* no line number */
c906108c
SS
9811 badInput = 1;
9812 else
9813 {
4495129a 9814 const char *argptr = arg;
c906108c
SS
9815 int hasColon = 0;
9816
4a64f543 9817 /* Look for a ':'. If there is a '::' then get out, otherwise
dda83cd7 9818 it is probably a line number. */
c906108c 9819 while (*argptr && !hasColon)
c5aa993b
JM
9820 {
9821 hasColon = (*argptr == ':');
9822 argptr++;
9823 }
c906108c
SS
9824
9825 if (hasColon)
c5aa993b 9826 badInput = (*argptr == ':'); /* we have class::method */
c906108c 9827 else
c5aa993b 9828 badInput = !isdigit (*arg); /* not a line number */
c906108c
SS
9829 }
9830
9831 if (badInput)
65e65158 9832 printf_filtered (_("Usage: stop at LINE\n"));
c906108c 9833 else
db107f19 9834 break_command_1 (arg, 0, from_tty);
c906108c
SS
9835}
9836
e7e0cddf
SS
9837/* The dynamic printf command is mostly like a regular breakpoint, but
9838 with a prewired command list consisting of a single output command,
9839 built from extra arguments supplied on the dprintf command
9840 line. */
9841
da821c7b 9842static void
0b39b52e 9843dprintf_command (const char *arg, int from_tty)
e7e0cddf 9844{
ffc2605c 9845 event_location_up location = string_to_event_location (&arg, current_language);
f00aae0f
KS
9846
9847 /* If non-NULL, ARG should have been advanced past the location;
9848 the next character must be ','. */
9849 if (arg != NULL)
9850 {
9851 if (arg[0] != ',' || arg[1] == '\0')
9852 error (_("Format string required"));
9853 else
9854 {
9855 /* Skip the comma. */
9856 ++arg;
9857 }
9858 }
9859
e7e0cddf 9860 create_breakpoint (get_current_arch (),
ffc2605c 9861 location.get (),
10a636cc 9862 NULL, 0, arg, false, 1 /* parse arg */,
e7e0cddf
SS
9863 0, bp_dprintf,
9864 0 /* Ignore count */,
9865 pending_break_support,
9866 &dprintf_breakpoint_ops,
9867 from_tty,
9868 1 /* enabled */,
9869 0 /* internal */,
9870 0);
9871}
9872
d3ce09f5 9873static void
0b39b52e 9874agent_printf_command (const char *arg, int from_tty)
d3ce09f5
SS
9875{
9876 error (_("May only run agent-printf on the target"));
9877}
9878
f1310107
TJB
9879/* Implement the "breakpoint_hit" breakpoint_ops method for
9880 ranged breakpoints. */
9881
9882static int
9883breakpoint_hit_ranged_breakpoint (const struct bp_location *bl,
bd522513 9884 const address_space *aspace,
09ac7c10
TT
9885 CORE_ADDR bp_addr,
9886 const struct target_waitstatus *ws)
f1310107 9887{
09ac7c10 9888 if (ws->kind != TARGET_WAITKIND_STOPPED
a493e3e2 9889 || ws->value.sig != GDB_SIGNAL_TRAP)
09ac7c10
TT
9890 return 0;
9891
f1310107
TJB
9892 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
9893 bl->length, aspace, bp_addr);
9894}
9895
9896/* Implement the "resources_needed" breakpoint_ops method for
9897 ranged breakpoints. */
9898
9899static int
9900resources_needed_ranged_breakpoint (const struct bp_location *bl)
9901{
9902 return target_ranged_break_num_registers ();
9903}
9904
9905/* Implement the "print_it" breakpoint_ops method for
9906 ranged breakpoints. */
9907
9908static enum print_stop_action
348d480f 9909print_it_ranged_breakpoint (bpstat bs)
f1310107 9910{
348d480f 9911 struct breakpoint *b = bs->breakpoint_at;
f1310107 9912 struct bp_location *bl = b->loc;
79a45e25 9913 struct ui_out *uiout = current_uiout;
f1310107
TJB
9914
9915 gdb_assert (b->type == bp_hardware_breakpoint);
9916
9917 /* Ranged breakpoints have only one location. */
9918 gdb_assert (bl && bl->next == NULL);
9919
9920 annotate_breakpoint (b->number);
f303dbd6
PA
9921
9922 maybe_print_thread_hit_breakpoint (uiout);
9923
f1310107 9924 if (b->disposition == disp_del)
112e8700 9925 uiout->text ("Temporary ranged breakpoint ");
f1310107 9926 else
112e8700
SM
9927 uiout->text ("Ranged breakpoint ");
9928 if (uiout->is_mi_like_p ())
f1310107 9929 {
112e8700 9930 uiout->field_string ("reason",
f1310107 9931 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
112e8700 9932 uiout->field_string ("disp", bpdisp_text (b->disposition));
f1310107 9933 }
381befee 9934 uiout->field_signed ("bkptno", b->number);
112e8700 9935 uiout->text (", ");
f1310107
TJB
9936
9937 return PRINT_SRC_AND_LOC;
9938}
9939
9940/* Implement the "print_one" breakpoint_ops method for
9941 ranged breakpoints. */
9942
9943static void
9944print_one_ranged_breakpoint (struct breakpoint *b,
9945 struct bp_location **last_loc)
9946{
9947 struct bp_location *bl = b->loc;
9948 struct value_print_options opts;
79a45e25 9949 struct ui_out *uiout = current_uiout;
f1310107
TJB
9950
9951 /* Ranged breakpoints have only one location. */
9952 gdb_assert (bl && bl->next == NULL);
9953
9954 get_user_print_options (&opts);
9955
9956 if (opts.addressprint)
9957 /* We don't print the address range here, it will be printed later
9958 by print_one_detail_ranged_breakpoint. */
112e8700 9959 uiout->field_skip ("addr");
f1310107
TJB
9960 annotate_field (5);
9961 print_breakpoint_location (b, bl);
9962 *last_loc = bl;
9963}
9964
9965/* Implement the "print_one_detail" breakpoint_ops method for
9966 ranged breakpoints. */
9967
9968static void
9969print_one_detail_ranged_breakpoint (const struct breakpoint *b,
9970 struct ui_out *uiout)
9971{
9972 CORE_ADDR address_start, address_end;
9973 struct bp_location *bl = b->loc;
d7e74731 9974 string_file stb;
f1310107
TJB
9975
9976 gdb_assert (bl);
9977
9978 address_start = bl->address;
9979 address_end = address_start + bl->length - 1;
9980
112e8700 9981 uiout->text ("\taddress range: ");
d7e74731
PA
9982 stb.printf ("[%s, %s]",
9983 print_core_address (bl->gdbarch, address_start),
9984 print_core_address (bl->gdbarch, address_end));
112e8700
SM
9985 uiout->field_stream ("addr", stb);
9986 uiout->text ("\n");
f1310107
TJB
9987}
9988
9989/* Implement the "print_mention" breakpoint_ops method for
9990 ranged breakpoints. */
9991
9992static void
9993print_mention_ranged_breakpoint (struct breakpoint *b)
9994{
9995 struct bp_location *bl = b->loc;
79a45e25 9996 struct ui_out *uiout = current_uiout;
f1310107
TJB
9997
9998 gdb_assert (bl);
9999 gdb_assert (b->type == bp_hardware_breakpoint);
10000
2d33446d
TT
10001 uiout->message (_("Hardware assisted ranged breakpoint %d from %s to %s."),
10002 b->number, paddress (bl->gdbarch, bl->address),
10003 paddress (bl->gdbarch, bl->address + bl->length - 1));
f1310107
TJB
10004}
10005
10006/* Implement the "print_recreate" breakpoint_ops method for
10007 ranged breakpoints. */
10008
10009static void
10010print_recreate_ranged_breakpoint (struct breakpoint *b, struct ui_file *fp)
10011{
f00aae0f 10012 fprintf_unfiltered (fp, "break-range %s, %s",
d28cd78a
TT
10013 event_location_to_string (b->location.get ()),
10014 event_location_to_string (b->location_range_end.get ()));
d9b3f62e 10015 print_recreate_thread (b, fp);
f1310107
TJB
10016}
10017
10018/* The breakpoint_ops structure to be used in ranged breakpoints. */
10019
2060206e 10020static struct breakpoint_ops ranged_breakpoint_ops;
f1310107
TJB
10021
10022/* Find the address where the end of the breakpoint range should be
10023 placed, given the SAL of the end of the range. This is so that if
10024 the user provides a line number, the end of the range is set to the
10025 last instruction of the given line. */
10026
10027static CORE_ADDR
10028find_breakpoint_range_end (struct symtab_and_line sal)
10029{
10030 CORE_ADDR end;
10031
10032 /* If the user provided a PC value, use it. Otherwise,
10033 find the address of the end of the given location. */
10034 if (sal.explicit_pc)
10035 end = sal.pc;
10036 else
10037 {
10038 int ret;
10039 CORE_ADDR start;
10040
10041 ret = find_line_pc_range (sal, &start, &end);
10042 if (!ret)
10043 error (_("Could not find location of the end of the range."));
10044
10045 /* find_line_pc_range returns the start of the next line. */
10046 end--;
10047 }
10048
10049 return end;
10050}
10051
10052/* Implement the "break-range" CLI command. */
10053
10054static void
0b39b52e 10055break_range_command (const char *arg, int from_tty)
f1310107 10056{
f2fc3015 10057 const char *arg_start;
f1310107
TJB
10058 struct linespec_result canonical_start, canonical_end;
10059 int bp_count, can_use_bp, length;
10060 CORE_ADDR end;
10061 struct breakpoint *b;
f1310107
TJB
10062
10063 /* We don't support software ranged breakpoints. */
10064 if (target_ranged_break_num_registers () < 0)
10065 error (_("This target does not support hardware ranged breakpoints."));
10066
10067 bp_count = hw_breakpoint_used_count ();
10068 bp_count += target_ranged_break_num_registers ();
10069 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
10070 bp_count, 0);
10071 if (can_use_bp < 0)
10072 error (_("Hardware breakpoints used exceeds limit."));
10073
f8eba3c6 10074 arg = skip_spaces (arg);
f1310107
TJB
10075 if (arg == NULL || arg[0] == '\0')
10076 error(_("No address range specified."));
10077
f8eba3c6 10078 arg_start = arg;
ffc2605c
TT
10079 event_location_up start_location = string_to_event_location (&arg,
10080 current_language);
10081 parse_breakpoint_sals (start_location.get (), &canonical_start);
f1310107
TJB
10082
10083 if (arg[0] != ',')
10084 error (_("Too few arguments."));
6c5b2ebe 10085 else if (canonical_start.lsals.empty ())
f1310107 10086 error (_("Could not find location of the beginning of the range."));
f8eba3c6 10087
6c5b2ebe 10088 const linespec_sals &lsal_start = canonical_start.lsals[0];
f8eba3c6 10089
6c5b2ebe
PA
10090 if (canonical_start.lsals.size () > 1
10091 || lsal_start.sals.size () != 1)
f1310107
TJB
10092 error (_("Cannot create a ranged breakpoint with multiple locations."));
10093
6c5b2ebe 10094 const symtab_and_line &sal_start = lsal_start.sals[0];
81b1e71c 10095 std::string addr_string_start (arg_start, arg - arg_start);
f1310107
TJB
10096
10097 arg++; /* Skip the comma. */
f8eba3c6 10098 arg = skip_spaces (arg);
f1310107
TJB
10099
10100 /* Parse the end location. */
10101
f1310107
TJB
10102 arg_start = arg;
10103
f8eba3c6 10104 /* We call decode_line_full directly here instead of using
f1310107
TJB
10105 parse_breakpoint_sals because we need to specify the start location's
10106 symtab and line as the default symtab and line for the end of the
10107 range. This makes it possible to have ranges like "foo.c:27, +14",
10108 where +14 means 14 lines from the start location. */
ffc2605c
TT
10109 event_location_up end_location = string_to_event_location (&arg,
10110 current_language);
10111 decode_line_full (end_location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
f8eba3c6
TT
10112 sal_start.symtab, sal_start.line,
10113 &canonical_end, NULL, NULL);
10114
6c5b2ebe 10115 if (canonical_end.lsals.empty ())
f1310107 10116 error (_("Could not find location of the end of the range."));
f8eba3c6 10117
6c5b2ebe
PA
10118 const linespec_sals &lsal_end = canonical_end.lsals[0];
10119 if (canonical_end.lsals.size () > 1
10120 || lsal_end.sals.size () != 1)
f1310107
TJB
10121 error (_("Cannot create a ranged breakpoint with multiple locations."));
10122
6c5b2ebe 10123 const symtab_and_line &sal_end = lsal_end.sals[0];
f1310107
TJB
10124
10125 end = find_breakpoint_range_end (sal_end);
10126 if (sal_start.pc > end)
177b42fe 10127 error (_("Invalid address range, end precedes start."));
f1310107
TJB
10128
10129 length = end - sal_start.pc + 1;
10130 if (length < 0)
10131 /* Length overflowed. */
10132 error (_("Address range too large."));
10133 else if (length == 1)
10134 {
10135 /* This range is simple enough to be handled by
10136 the `hbreak' command. */
81b1e71c 10137 hbreak_command (&addr_string_start[0], 1);
f1310107
TJB
10138
10139 return;
10140 }
10141
10142 /* Now set up the breakpoint. */
10143 b = set_raw_breakpoint (get_current_arch (), sal_start,
348d480f 10144 bp_hardware_breakpoint, &ranged_breakpoint_ops);
f1310107
TJB
10145 set_breakpoint_count (breakpoint_count + 1);
10146 b->number = breakpoint_count;
10147 b->disposition = disp_donttouch;
d28cd78a
TT
10148 b->location = std::move (start_location);
10149 b->location_range_end = std::move (end_location);
f1310107
TJB
10150 b->loc->length = length;
10151
f1310107 10152 mention (b);
76727919 10153 gdb::observers::breakpoint_created.notify (b);
44702360 10154 update_global_location_list (UGLL_MAY_INSERT);
f1310107
TJB
10155}
10156
4a64f543
MS
10157/* Return non-zero if EXP is verified as constant. Returned zero
10158 means EXP is variable. Also the constant detection may fail for
10159 some constant expressions and in such case still falsely return
10160 zero. */
2e6e3d9c 10161
dab72643 10162static bool
65d79d4b
SDJ
10163watchpoint_exp_is_const (const struct expression *exp)
10164{
1eaebe02 10165 return exp->op->constant_p ();
65d79d4b
SDJ
10166}
10167
c1fc2657 10168/* Watchpoint destructor. */
3a5c3e22 10169
c1fc2657 10170watchpoint::~watchpoint ()
3a5c3e22 10171{
c1fc2657
SM
10172 xfree (this->exp_string);
10173 xfree (this->exp_string_reparse);
3a5c3e22
PA
10174}
10175
348d480f
PA
10176/* Implement the "re_set" breakpoint_ops method for watchpoints. */
10177
10178static void
10179re_set_watchpoint (struct breakpoint *b)
10180{
3a5c3e22
PA
10181 struct watchpoint *w = (struct watchpoint *) b;
10182
348d480f
PA
10183 /* Watchpoint can be either on expression using entirely global
10184 variables, or it can be on local variables.
10185
10186 Watchpoints of the first kind are never auto-deleted, and even
10187 persist across program restarts. Since they can use variables
10188 from shared libraries, we need to reparse expression as libraries
10189 are loaded and unloaded.
10190
10191 Watchpoints on local variables can also change meaning as result
10192 of solib event. For example, if a watchpoint uses both a local
10193 and a global variables in expression, it's a local watchpoint,
10194 but unloading of a shared library will make the expression
10195 invalid. This is not a very common use case, but we still
10196 re-evaluate expression, to avoid surprises to the user.
10197
10198 Note that for local watchpoints, we re-evaluate it only if
10199 watchpoints frame id is still valid. If it's not, it means the
10200 watchpoint is out of scope and will be deleted soon. In fact,
10201 I'm not sure we'll ever be called in this case.
10202
10203 If a local watchpoint's frame id is still valid, then
3a5c3e22 10204 w->exp_valid_block is likewise valid, and we can safely use it.
348d480f 10205
3a5c3e22
PA
10206 Don't do anything about disabled watchpoints, since they will be
10207 reevaluated again when enabled. */
10208 update_watchpoint (w, 1 /* reparse */);
348d480f
PA
10209}
10210
77b06cd7
TJB
10211/* Implement the "insert" breakpoint_ops method for hardware watchpoints. */
10212
10213static int
10214insert_watchpoint (struct bp_location *bl)
10215{
3a5c3e22
PA
10216 struct watchpoint *w = (struct watchpoint *) bl->owner;
10217 int length = w->exact ? 1 : bl->length;
e09342b5
TJB
10218
10219 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
4d01a485 10220 w->cond_exp.get ());
77b06cd7
TJB
10221}
10222
10223/* Implement the "remove" breakpoint_ops method for hardware watchpoints. */
10224
10225static int
73971819 10226remove_watchpoint (struct bp_location *bl, enum remove_bp_reason reason)
77b06cd7 10227{
3a5c3e22
PA
10228 struct watchpoint *w = (struct watchpoint *) bl->owner;
10229 int length = w->exact ? 1 : bl->length;
e09342b5
TJB
10230
10231 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
4d01a485 10232 w->cond_exp.get ());
e09342b5
TJB
10233}
10234
e09342b5 10235static int
348d480f 10236breakpoint_hit_watchpoint (const struct bp_location *bl,
bd522513 10237 const address_space *aspace, CORE_ADDR bp_addr,
09ac7c10 10238 const struct target_waitstatus *ws)
e09342b5 10239{
348d480f 10240 struct breakpoint *b = bl->owner;
3a5c3e22 10241 struct watchpoint *w = (struct watchpoint *) b;
77b06cd7 10242
348d480f
PA
10243 /* Continuable hardware watchpoints are treated as non-existent if the
10244 reason we stopped wasn't a hardware watchpoint (we didn't stop on
10245 some data address). Otherwise gdb won't stop on a break instruction
10246 in the code (not from a breakpoint) when a hardware watchpoint has
10247 been defined. Also skip watchpoints which we know did not trigger
10248 (did not match the data address). */
10249 if (is_hardware_watchpoint (b)
3a5c3e22 10250 && w->watchpoint_triggered == watch_triggered_no)
348d480f 10251 return 0;
9c06b0b4 10252
348d480f 10253 return 1;
9c06b0b4
TJB
10254}
10255
348d480f
PA
10256static void
10257check_status_watchpoint (bpstat bs)
9c06b0b4 10258{
348d480f 10259 gdb_assert (is_watchpoint (bs->breakpoint_at));
9c06b0b4 10260
348d480f 10261 bpstat_check_watchpoint (bs);
9c06b0b4
TJB
10262}
10263
10264/* Implement the "resources_needed" breakpoint_ops method for
348d480f 10265 hardware watchpoints. */
9c06b0b4
TJB
10266
10267static int
348d480f 10268resources_needed_watchpoint (const struct bp_location *bl)
9c06b0b4 10269{
3a5c3e22
PA
10270 struct watchpoint *w = (struct watchpoint *) bl->owner;
10271 int length = w->exact? 1 : bl->length;
348d480f
PA
10272
10273 return target_region_ok_for_hw_watchpoint (bl->address, length);
9c06b0b4
TJB
10274}
10275
10276/* Implement the "works_in_software_mode" breakpoint_ops method for
348d480f 10277 hardware watchpoints. */
9c06b0b4
TJB
10278
10279static int
348d480f 10280works_in_software_mode_watchpoint (const struct breakpoint *b)
9c06b0b4 10281{
efa80663
PA
10282 /* Read and access watchpoints only work with hardware support. */
10283 return b->type == bp_watchpoint || b->type == bp_hardware_watchpoint;
9c06b0b4
TJB
10284}
10285
9c06b0b4 10286static enum print_stop_action
348d480f 10287print_it_watchpoint (bpstat bs)
9c06b0b4 10288{
348d480f 10289 struct breakpoint *b;
348d480f 10290 enum print_stop_action result;
3a5c3e22 10291 struct watchpoint *w;
79a45e25 10292 struct ui_out *uiout = current_uiout;
348d480f
PA
10293
10294 gdb_assert (bs->bp_location_at != NULL);
10295
348d480f 10296 b = bs->breakpoint_at;
3a5c3e22 10297 w = (struct watchpoint *) b;
348d480f 10298
f303dbd6
PA
10299 annotate_watchpoint (b->number);
10300 maybe_print_thread_hit_breakpoint (uiout);
10301
d7e74731
PA
10302 string_file stb;
10303
76f9c9cf 10304 gdb::optional<ui_out_emit_tuple> tuple_emitter;
9c06b0b4
TJB
10305 switch (b->type)
10306 {
348d480f 10307 case bp_watchpoint:
9c06b0b4 10308 case bp_hardware_watchpoint:
112e8700
SM
10309 if (uiout->is_mi_like_p ())
10310 uiout->field_string
10311 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
348d480f 10312 mention (b);
76f9c9cf 10313 tuple_emitter.emplace (uiout, "value");
112e8700 10314 uiout->text ("\nOld value = ");
850645cf 10315 watchpoint_value_print (bs->old_val.get (), &stb);
112e8700
SM
10316 uiout->field_stream ("old", stb);
10317 uiout->text ("\nNew value = ");
850645cf 10318 watchpoint_value_print (w->val.get (), &stb);
112e8700
SM
10319 uiout->field_stream ("new", stb);
10320 uiout->text ("\n");
348d480f
PA
10321 /* More than one watchpoint may have been triggered. */
10322 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10323 break;
10324
10325 case bp_read_watchpoint:
112e8700
SM
10326 if (uiout->is_mi_like_p ())
10327 uiout->field_string
10328 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
348d480f 10329 mention (b);
76f9c9cf 10330 tuple_emitter.emplace (uiout, "value");
112e8700 10331 uiout->text ("\nValue = ");
850645cf 10332 watchpoint_value_print (w->val.get (), &stb);
112e8700
SM
10333 uiout->field_stream ("value", stb);
10334 uiout->text ("\n");
348d480f 10335 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10336 break;
10337
10338 case bp_access_watchpoint:
348d480f
PA
10339 if (bs->old_val != NULL)
10340 {
112e8700
SM
10341 if (uiout->is_mi_like_p ())
10342 uiout->field_string
10343 ("reason",
348d480f
PA
10344 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10345 mention (b);
76f9c9cf 10346 tuple_emitter.emplace (uiout, "value");
112e8700 10347 uiout->text ("\nOld value = ");
850645cf 10348 watchpoint_value_print (bs->old_val.get (), &stb);
112e8700
SM
10349 uiout->field_stream ("old", stb);
10350 uiout->text ("\nNew value = ");
348d480f
PA
10351 }
10352 else
10353 {
10354 mention (b);
112e8700
SM
10355 if (uiout->is_mi_like_p ())
10356 uiout->field_string
10357 ("reason",
348d480f 10358 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
76f9c9cf 10359 tuple_emitter.emplace (uiout, "value");
112e8700 10360 uiout->text ("\nValue = ");
348d480f 10361 }
850645cf 10362 watchpoint_value_print (w->val.get (), &stb);
112e8700
SM
10363 uiout->field_stream ("new", stb);
10364 uiout->text ("\n");
348d480f 10365 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10366 break;
10367 default:
348d480f 10368 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10369 }
10370
348d480f
PA
10371 return result;
10372}
10373
10374/* Implement the "print_mention" breakpoint_ops method for hardware
10375 watchpoints. */
10376
10377static void
10378print_mention_watchpoint (struct breakpoint *b)
10379{
3a5c3e22 10380 struct watchpoint *w = (struct watchpoint *) b;
79a45e25 10381 struct ui_out *uiout = current_uiout;
46b9c129 10382 const char *tuple_name;
348d480f
PA
10383
10384 switch (b->type)
10385 {
10386 case bp_watchpoint:
112e8700 10387 uiout->text ("Watchpoint ");
46b9c129 10388 tuple_name = "wpt";
348d480f
PA
10389 break;
10390 case bp_hardware_watchpoint:
112e8700 10391 uiout->text ("Hardware watchpoint ");
46b9c129 10392 tuple_name = "wpt";
348d480f
PA
10393 break;
10394 case bp_read_watchpoint:
112e8700 10395 uiout->text ("Hardware read watchpoint ");
46b9c129 10396 tuple_name = "hw-rwpt";
348d480f
PA
10397 break;
10398 case bp_access_watchpoint:
112e8700 10399 uiout->text ("Hardware access (read/write) watchpoint ");
46b9c129 10400 tuple_name = "hw-awpt";
348d480f
PA
10401 break;
10402 default:
10403 internal_error (__FILE__, __LINE__,
10404 _("Invalid hardware watchpoint type."));
10405 }
10406
46b9c129 10407 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
381befee 10408 uiout->field_signed ("number", b->number);
112e8700
SM
10409 uiout->text (": ");
10410 uiout->field_string ("exp", w->exp_string);
348d480f
PA
10411}
10412
10413/* Implement the "print_recreate" breakpoint_ops method for
10414 watchpoints. */
10415
10416static void
10417print_recreate_watchpoint (struct breakpoint *b, struct ui_file *fp)
10418{
3a5c3e22
PA
10419 struct watchpoint *w = (struct watchpoint *) b;
10420
348d480f
PA
10421 switch (b->type)
10422 {
10423 case bp_watchpoint:
10424 case bp_hardware_watchpoint:
10425 fprintf_unfiltered (fp, "watch");
10426 break;
10427 case bp_read_watchpoint:
10428 fprintf_unfiltered (fp, "rwatch");
10429 break;
10430 case bp_access_watchpoint:
10431 fprintf_unfiltered (fp, "awatch");
10432 break;
10433 default:
10434 internal_error (__FILE__, __LINE__,
10435 _("Invalid watchpoint type."));
10436 }
10437
3a5c3e22 10438 fprintf_unfiltered (fp, " %s", w->exp_string);
d9b3f62e 10439 print_recreate_thread (b, fp);
348d480f
PA
10440}
10441
427cd150
TT
10442/* Implement the "explains_signal" breakpoint_ops method for
10443 watchpoints. */
10444
47591c29 10445static int
427cd150
TT
10446explains_signal_watchpoint (struct breakpoint *b, enum gdb_signal sig)
10447{
10448 /* A software watchpoint cannot cause a signal other than
10449 GDB_SIGNAL_TRAP. */
10450 if (b->type == bp_watchpoint && sig != GDB_SIGNAL_TRAP)
47591c29 10451 return 0;
427cd150 10452
47591c29 10453 return 1;
427cd150
TT
10454}
10455
348d480f
PA
10456/* The breakpoint_ops structure to be used in hardware watchpoints. */
10457
2060206e 10458static struct breakpoint_ops watchpoint_breakpoint_ops;
348d480f
PA
10459
10460/* Implement the "insert" breakpoint_ops method for
10461 masked hardware watchpoints. */
10462
10463static int
10464insert_masked_watchpoint (struct bp_location *bl)
10465{
3a5c3e22
PA
10466 struct watchpoint *w = (struct watchpoint *) bl->owner;
10467
10468 return target_insert_mask_watchpoint (bl->address, w->hw_wp_mask,
348d480f
PA
10469 bl->watchpoint_type);
10470}
10471
10472/* Implement the "remove" breakpoint_ops method for
10473 masked hardware watchpoints. */
10474
10475static int
73971819 10476remove_masked_watchpoint (struct bp_location *bl, enum remove_bp_reason reason)
348d480f 10477{
3a5c3e22
PA
10478 struct watchpoint *w = (struct watchpoint *) bl->owner;
10479
10480 return target_remove_mask_watchpoint (bl->address, w->hw_wp_mask,
dda83cd7 10481 bl->watchpoint_type);
348d480f
PA
10482}
10483
10484/* Implement the "resources_needed" breakpoint_ops method for
10485 masked hardware watchpoints. */
10486
10487static int
10488resources_needed_masked_watchpoint (const struct bp_location *bl)
10489{
3a5c3e22
PA
10490 struct watchpoint *w = (struct watchpoint *) bl->owner;
10491
10492 return target_masked_watch_num_registers (bl->address, w->hw_wp_mask);
348d480f
PA
10493}
10494
10495/* Implement the "works_in_software_mode" breakpoint_ops method for
10496 masked hardware watchpoints. */
10497
10498static int
10499works_in_software_mode_masked_watchpoint (const struct breakpoint *b)
10500{
10501 return 0;
10502}
10503
10504/* Implement the "print_it" breakpoint_ops method for
10505 masked hardware watchpoints. */
10506
10507static enum print_stop_action
10508print_it_masked_watchpoint (bpstat bs)
10509{
10510 struct breakpoint *b = bs->breakpoint_at;
79a45e25 10511 struct ui_out *uiout = current_uiout;
348d480f
PA
10512
10513 /* Masked watchpoints have only one location. */
10514 gdb_assert (b->loc && b->loc->next == NULL);
10515
f303dbd6
PA
10516 annotate_watchpoint (b->number);
10517 maybe_print_thread_hit_breakpoint (uiout);
10518
348d480f
PA
10519 switch (b->type)
10520 {
10521 case bp_hardware_watchpoint:
112e8700
SM
10522 if (uiout->is_mi_like_p ())
10523 uiout->field_string
10524 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
348d480f
PA
10525 break;
10526
10527 case bp_read_watchpoint:
112e8700
SM
10528 if (uiout->is_mi_like_p ())
10529 uiout->field_string
10530 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
348d480f
PA
10531 break;
10532
10533 case bp_access_watchpoint:
112e8700
SM
10534 if (uiout->is_mi_like_p ())
10535 uiout->field_string
10536 ("reason",
348d480f
PA
10537 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10538 break;
10539 default:
10540 internal_error (__FILE__, __LINE__,
10541 _("Invalid hardware watchpoint type."));
10542 }
10543
10544 mention (b);
112e8700 10545 uiout->text (_("\n\
9c06b0b4
TJB
10546Check the underlying instruction at PC for the memory\n\
10547address and value which triggered this watchpoint.\n"));
112e8700 10548 uiout->text ("\n");
9c06b0b4
TJB
10549
10550 /* More than one watchpoint may have been triggered. */
10551 return PRINT_UNKNOWN;
10552}
10553
10554/* Implement the "print_one_detail" breakpoint_ops method for
10555 masked hardware watchpoints. */
10556
10557static void
10558print_one_detail_masked_watchpoint (const struct breakpoint *b,
10559 struct ui_out *uiout)
10560{
3a5c3e22
PA
10561 struct watchpoint *w = (struct watchpoint *) b;
10562
9c06b0b4
TJB
10563 /* Masked watchpoints have only one location. */
10564 gdb_assert (b->loc && b->loc->next == NULL);
10565
112e8700
SM
10566 uiout->text ("\tmask ");
10567 uiout->field_core_addr ("mask", b->loc->gdbarch, w->hw_wp_mask);
10568 uiout->text ("\n");
9c06b0b4
TJB
10569}
10570
10571/* Implement the "print_mention" breakpoint_ops method for
10572 masked hardware watchpoints. */
10573
10574static void
10575print_mention_masked_watchpoint (struct breakpoint *b)
10576{
3a5c3e22 10577 struct watchpoint *w = (struct watchpoint *) b;
79a45e25 10578 struct ui_out *uiout = current_uiout;
46b9c129 10579 const char *tuple_name;
9c06b0b4
TJB
10580
10581 switch (b->type)
10582 {
10583 case bp_hardware_watchpoint:
112e8700 10584 uiout->text ("Masked hardware watchpoint ");
46b9c129 10585 tuple_name = "wpt";
9c06b0b4
TJB
10586 break;
10587 case bp_read_watchpoint:
112e8700 10588 uiout->text ("Masked hardware read watchpoint ");
46b9c129 10589 tuple_name = "hw-rwpt";
9c06b0b4
TJB
10590 break;
10591 case bp_access_watchpoint:
112e8700 10592 uiout->text ("Masked hardware access (read/write) watchpoint ");
46b9c129 10593 tuple_name = "hw-awpt";
9c06b0b4
TJB
10594 break;
10595 default:
10596 internal_error (__FILE__, __LINE__,
10597 _("Invalid hardware watchpoint type."));
10598 }
10599
46b9c129 10600 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
381befee 10601 uiout->field_signed ("number", b->number);
112e8700
SM
10602 uiout->text (": ");
10603 uiout->field_string ("exp", w->exp_string);
9c06b0b4
TJB
10604}
10605
10606/* Implement the "print_recreate" breakpoint_ops method for
10607 masked hardware watchpoints. */
10608
10609static void
10610print_recreate_masked_watchpoint (struct breakpoint *b, struct ui_file *fp)
10611{
3a5c3e22 10612 struct watchpoint *w = (struct watchpoint *) b;
9c06b0b4
TJB
10613
10614 switch (b->type)
10615 {
10616 case bp_hardware_watchpoint:
10617 fprintf_unfiltered (fp, "watch");
10618 break;
10619 case bp_read_watchpoint:
10620 fprintf_unfiltered (fp, "rwatch");
10621 break;
10622 case bp_access_watchpoint:
10623 fprintf_unfiltered (fp, "awatch");
10624 break;
10625 default:
10626 internal_error (__FILE__, __LINE__,
10627 _("Invalid hardware watchpoint type."));
10628 }
10629
53807e9f
TT
10630 fprintf_unfiltered (fp, " %s mask 0x%s", w->exp_string,
10631 phex (w->hw_wp_mask, sizeof (CORE_ADDR)));
d9b3f62e 10632 print_recreate_thread (b, fp);
9c06b0b4
TJB
10633}
10634
10635/* The breakpoint_ops structure to be used in masked hardware watchpoints. */
10636
2060206e 10637static struct breakpoint_ops masked_watchpoint_breakpoint_ops;
9c06b0b4
TJB
10638
10639/* Tell whether the given watchpoint is a masked hardware watchpoint. */
10640
f2478a7e 10641static bool
9c06b0b4
TJB
10642is_masked_watchpoint (const struct breakpoint *b)
10643{
10644 return b->ops == &masked_watchpoint_breakpoint_ops;
10645}
10646
53a5351d 10647/* accessflag: hw_write: watch write,
dda83cd7 10648 hw_read: watch read,
53a5351d 10649 hw_access: watch access (read or write) */
c906108c 10650static void
bbc13ae3 10651watch_command_1 (const char *arg, int accessflag, int from_tty,
2e362716 10652 bool just_location, bool internal)
c906108c 10653{
c1fc2657 10654 struct breakpoint *scope_breakpoint = NULL;
270140bd 10655 const struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
b926417a 10656 struct value *result;
bb9d5f81 10657 int saved_bitpos = 0, saved_bitsize = 0;
bbc13ae3
KS
10658 const char *exp_start = NULL;
10659 const char *exp_end = NULL;
10660 const char *tok, *end_tok;
9c06b0b4 10661 int toklen = -1;
bbc13ae3
KS
10662 const char *cond_start = NULL;
10663 const char *cond_end = NULL;
c906108c 10664 enum bptype bp_type;
37e4754d 10665 int thread = -1;
9c06b0b4
TJB
10666 /* Flag to indicate whether we are going to use masks for
10667 the hardware watchpoint. */
2e362716 10668 bool use_mask = false;
9c06b0b4 10669 CORE_ADDR mask = 0;
c906108c 10670
37e4754d
LM
10671 /* Make sure that we actually have parameters to parse. */
10672 if (arg != NULL && arg[0] != '\0')
10673 {
bbc13ae3
KS
10674 const char *value_start;
10675
10676 exp_end = arg + strlen (arg);
37e4754d 10677
9c06b0b4
TJB
10678 /* Look for "parameter value" pairs at the end
10679 of the arguments string. */
bbc13ae3 10680 for (tok = exp_end - 1; tok > arg; tok--)
9c06b0b4
TJB
10681 {
10682 /* Skip whitespace at the end of the argument list. */
10683 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10684 tok--;
10685
10686 /* Find the beginning of the last token.
10687 This is the value of the parameter. */
10688 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10689 tok--;
10690 value_start = tok + 1;
10691
10692 /* Skip whitespace. */
10693 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10694 tok--;
10695
10696 end_tok = tok;
10697
10698 /* Find the beginning of the second to last token.
10699 This is the parameter itself. */
10700 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10701 tok--;
10702 tok++;
10703 toklen = end_tok - tok + 1;
10704
61012eef 10705 if (toklen == 6 && startswith (tok, "thread"))
9c06b0b4 10706 {
5d5658a1 10707 struct thread_info *thr;
9c06b0b4
TJB
10708 /* At this point we've found a "thread" token, which means
10709 the user is trying to set a watchpoint that triggers
10710 only in a specific thread. */
5d5658a1 10711 const char *endp;
37e4754d 10712
9c06b0b4
TJB
10713 if (thread != -1)
10714 error(_("You can specify only one thread."));
37e4754d 10715
9c06b0b4 10716 /* Extract the thread ID from the next token. */
5d5658a1 10717 thr = parse_thread_id (value_start, &endp);
37e4754d 10718
5d5658a1 10719 /* Check if the user provided a valid thread ID. */
9c06b0b4 10720 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
5d5658a1 10721 invalid_thread_id_error (value_start);
9c06b0b4 10722
5d5658a1 10723 thread = thr->global_num;
9c06b0b4 10724 }
61012eef 10725 else if (toklen == 4 && startswith (tok, "mask"))
9c06b0b4
TJB
10726 {
10727 /* We've found a "mask" token, which means the user wants to
10728 create a hardware watchpoint that is going to have the mask
10729 facility. */
10730 struct value *mask_value, *mark;
37e4754d 10731
9c06b0b4
TJB
10732 if (use_mask)
10733 error(_("You can specify only one mask."));
37e4754d 10734
2e362716 10735 use_mask = just_location = true;
37e4754d 10736
9c06b0b4
TJB
10737 mark = value_mark ();
10738 mask_value = parse_to_comma_and_eval (&value_start);
10739 mask = value_as_address (mask_value);
10740 value_free_to_mark (mark);
10741 }
10742 else
10743 /* We didn't recognize what we found. We should stop here. */
10744 break;
37e4754d 10745
9c06b0b4
TJB
10746 /* Truncate the string and get rid of the "parameter value" pair before
10747 the arguments string is parsed by the parse_exp_1 function. */
bbc13ae3 10748 exp_end = tok;
9c06b0b4 10749 }
37e4754d 10750 }
bbc13ae3
KS
10751 else
10752 exp_end = arg;
37e4754d 10753
bbc13ae3
KS
10754 /* Parse the rest of the arguments. From here on out, everything
10755 is in terms of a newly allocated string instead of the original
10756 ARG. */
81b1e71c
TT
10757 std::string expression (arg, exp_end - arg);
10758 exp_start = arg = expression.c_str ();
699bd4cf
TT
10759 innermost_block_tracker tracker;
10760 expression_up exp = parse_exp_1 (&arg, 0, 0, 0, &tracker);
c906108c 10761 exp_end = arg;
fa8a61dc
TT
10762 /* Remove trailing whitespace from the expression before saving it.
10763 This makes the eventual display of the expression string a bit
10764 prettier. */
10765 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
10766 --exp_end;
10767
65d79d4b 10768 /* Checking if the expression is not constant. */
4d01a485 10769 if (watchpoint_exp_is_const (exp.get ()))
65d79d4b
SDJ
10770 {
10771 int len;
10772
10773 len = exp_end - exp_start;
10774 while (len > 0 && isspace (exp_start[len - 1]))
10775 len--;
10776 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
10777 }
10778
699bd4cf 10779 exp_valid_block = tracker.block ();
b926417a 10780 struct value *mark = value_mark ();
850645cf 10781 struct value *val_as_value = nullptr;
1eaebe02
TT
10782 fetch_subexp_value (exp.get (), exp->op.get (), &val_as_value, &result, NULL,
10783 just_location);
06a64a0b 10784
850645cf 10785 if (val_as_value != NULL && just_location)
bb9d5f81 10786 {
850645cf
TT
10787 saved_bitpos = value_bitpos (val_as_value);
10788 saved_bitsize = value_bitsize (val_as_value);
bb9d5f81
PP
10789 }
10790
850645cf 10791 value_ref_ptr val;
06a64a0b
TT
10792 if (just_location)
10793 {
9c06b0b4
TJB
10794 int ret;
10795
06a64a0b 10796 exp_valid_block = NULL;
850645cf 10797 val = release_value (value_addr (result));
06a64a0b 10798 value_free_to_mark (mark);
9c06b0b4
TJB
10799
10800 if (use_mask)
10801 {
850645cf 10802 ret = target_masked_watch_num_registers (value_as_address (val.get ()),
9c06b0b4
TJB
10803 mask);
10804 if (ret == -1)
10805 error (_("This target does not support masked watchpoints."));
10806 else if (ret == -2)
10807 error (_("Invalid mask or memory region."));
10808 }
06a64a0b 10809 }
850645cf
TT
10810 else if (val_as_value != NULL)
10811 val = release_value (val_as_value);
c906108c 10812
f1735a53
TT
10813 tok = skip_spaces (arg);
10814 end_tok = skip_to_space (tok);
c906108c
SS
10815
10816 toklen = end_tok - tok;
10817 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
10818 {
10819 tok = cond_start = end_tok + 1;
699bd4cf
TT
10820 innermost_block_tracker if_tracker;
10821 parse_exp_1 (&tok, 0, 0, 0, &if_tracker);
60e1c644
PA
10822
10823 /* The watchpoint expression may not be local, but the condition
10824 may still be. E.g.: `watch global if local > 0'. */
699bd4cf 10825 cond_exp_valid_block = if_tracker.block ();
60e1c644 10826
c906108c
SS
10827 cond_end = tok;
10828 }
10829 if (*tok)
8a3fe4f8 10830 error (_("Junk at end of command."));
c906108c 10831
441d7c93
PA
10832 frame_info *wp_frame = block_innermost_frame (exp_valid_block);
10833
10834 /* Save this because create_internal_breakpoint below invalidates
10835 'wp_frame'. */
10836 frame_id watchpoint_frame = get_frame_id (wp_frame);
d983da9c
DJ
10837
10838 /* If the expression is "local", then set up a "watchpoint scope"
10839 breakpoint at the point where we've left the scope of the watchpoint
10840 expression. Create the scope breakpoint before the watchpoint, so
10841 that we will encounter it first in bpstat_stop_status. */
441d7c93 10842 if (exp_valid_block != NULL && wp_frame != NULL)
d983da9c 10843 {
441d7c93
PA
10844 frame_id caller_frame_id = frame_unwind_caller_id (wp_frame);
10845
10846 if (frame_id_p (caller_frame_id))
edb3359d 10847 {
441d7c93
PA
10848 gdbarch *caller_arch = frame_unwind_caller_arch (wp_frame);
10849 CORE_ADDR caller_pc = frame_unwind_caller_pc (wp_frame);
10850
edb3359d 10851 scope_breakpoint
441d7c93 10852 = create_internal_breakpoint (caller_arch, caller_pc,
06edf0c0
PA
10853 bp_watchpoint_scope,
10854 &momentary_breakpoint_ops);
d983da9c 10855
441d7c93
PA
10856 /* create_internal_breakpoint could invalidate WP_FRAME. */
10857 wp_frame = NULL;
10858
edb3359d 10859 scope_breakpoint->enable_state = bp_enabled;
d983da9c 10860
edb3359d
DJ
10861 /* Automatically delete the breakpoint when it hits. */
10862 scope_breakpoint->disposition = disp_del;
d983da9c 10863
edb3359d 10864 /* Only break in the proper frame (help with recursion). */
441d7c93 10865 scope_breakpoint->frame_id = caller_frame_id;
d983da9c 10866
edb3359d 10867 /* Set the address at which we will stop. */
441d7c93
PA
10868 scope_breakpoint->loc->gdbarch = caller_arch;
10869 scope_breakpoint->loc->requested_address = caller_pc;
edb3359d 10870 scope_breakpoint->loc->address
a6d9a66e
UW
10871 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
10872 scope_breakpoint->loc->requested_address,
edb3359d
DJ
10873 scope_breakpoint->type);
10874 }
d983da9c
DJ
10875 }
10876
e8369a73
AB
10877 /* Now set up the breakpoint. We create all watchpoints as hardware
10878 watchpoints here even if hardware watchpoints are turned off, a call
10879 to update_watchpoint later in this function will cause the type to
10880 drop back to bp_watchpoint (software watchpoint) if required. */
10881
10882 if (accessflag == hw_read)
10883 bp_type = bp_read_watchpoint;
10884 else if (accessflag == hw_access)
10885 bp_type = bp_access_watchpoint;
10886 else
10887 bp_type = bp_hardware_watchpoint;
3a5c3e22 10888
b270e6f9 10889 std::unique_ptr<watchpoint> w (new watchpoint ());
c1fc2657 10890
348d480f 10891 if (use_mask)
b270e6f9 10892 init_raw_breakpoint_without_location (w.get (), NULL, bp_type,
3a5c3e22 10893 &masked_watchpoint_breakpoint_ops);
348d480f 10894 else
b270e6f9 10895 init_raw_breakpoint_without_location (w.get (), NULL, bp_type,
3a5c3e22 10896 &watchpoint_breakpoint_ops);
c1fc2657
SM
10897 w->thread = thread;
10898 w->disposition = disp_donttouch;
10899 w->pspace = current_program_space;
b22e99fd 10900 w->exp = std::move (exp);
3a5c3e22
PA
10901 w->exp_valid_block = exp_valid_block;
10902 w->cond_exp_valid_block = cond_exp_valid_block;
06a64a0b
TT
10903 if (just_location)
10904 {
850645cf
TT
10905 struct type *t = value_type (val.get ());
10906 CORE_ADDR addr = value_as_address (val.get ());
06a64a0b 10907
43cc5389 10908 w->exp_string_reparse
f16a9f57 10909 = current_language->watch_location_expression (t, addr).release ();
06a64a0b 10910
3a5c3e22 10911 w->exp_string = xstrprintf ("-location %.*s",
d63d0675 10912 (int) (exp_end - exp_start), exp_start);
06a64a0b
TT
10913 }
10914 else
3a5c3e22 10915 w->exp_string = savestring (exp_start, exp_end - exp_start);
9c06b0b4
TJB
10916
10917 if (use_mask)
10918 {
3a5c3e22 10919 w->hw_wp_mask = mask;
9c06b0b4
TJB
10920 }
10921 else
10922 {
3a5c3e22 10923 w->val = val;
bb9d5f81
PP
10924 w->val_bitpos = saved_bitpos;
10925 w->val_bitsize = saved_bitsize;
4c1d86d9 10926 w->val_valid = true;
9c06b0b4 10927 }
77b06cd7 10928
c906108c 10929 if (cond_start)
c1fc2657 10930 w->cond_string = savestring (cond_start, cond_end - cond_start);
c906108c 10931 else
c1fc2657 10932 w->cond_string = 0;
c5aa993b 10933
441d7c93 10934 if (frame_id_p (watchpoint_frame))
f6bc2008 10935 {
441d7c93 10936 w->watchpoint_frame = watchpoint_frame;
3a5c3e22 10937 w->watchpoint_thread = inferior_ptid;
f6bc2008 10938 }
c906108c 10939 else
f6bc2008 10940 {
3a5c3e22
PA
10941 w->watchpoint_frame = null_frame_id;
10942 w->watchpoint_thread = null_ptid;
f6bc2008 10943 }
c906108c 10944
d983da9c 10945 if (scope_breakpoint != NULL)
c906108c 10946 {
d983da9c
DJ
10947 /* The scope breakpoint is related to the watchpoint. We will
10948 need to act on them together. */
c1fc2657 10949 w->related_breakpoint = scope_breakpoint;
b270e6f9 10950 scope_breakpoint->related_breakpoint = w.get ();
c906108c 10951 }
d983da9c 10952
06a64a0b
TT
10953 if (!just_location)
10954 value_free_to_mark (mark);
2d134ed3 10955
b270e6f9
TT
10956 /* Finally update the new watchpoint. This creates the locations
10957 that should be inserted. */
10958 update_watchpoint (w.get (), 1);
a9634178 10959
b270e6f9 10960 install_breakpoint (internal, std::move (w), 1);
c906108c
SS
10961}
10962
e09342b5 10963/* Return count of debug registers needed to watch the given expression.
e09342b5 10964 If the watchpoint cannot be handled in hardware return zero. */
c906108c 10965
c906108c 10966static int
a6535de1 10967can_use_hardware_watchpoint (const std::vector<value_ref_ptr> &vals)
c906108c
SS
10968{
10969 int found_memory_cnt = 0;
10970
10971 /* Did the user specifically forbid us to use hardware watchpoints? */
c5aa993b 10972 if (!can_use_hw_watchpoints)
c906108c 10973 return 0;
c5aa993b 10974
a6535de1
TT
10975 gdb_assert (!vals.empty ());
10976 struct value *head = vals[0].get ();
10977
5c44784c
JM
10978 /* Make sure that the value of the expression depends only upon
10979 memory contents, and values computed from them within GDB. If we
10980 find any register references or function calls, we can't use a
10981 hardware watchpoint.
10982
10983 The idea here is that evaluating an expression generates a series
10984 of values, one holding the value of every subexpression. (The
10985 expression a*b+c has five subexpressions: a, b, a*b, c, and
10986 a*b+c.) GDB's values hold almost enough information to establish
10987 the criteria given above --- they identify memory lvalues,
10988 register lvalues, computed values, etcetera. So we can evaluate
10989 the expression, and then scan the chain of values that leaves
10990 behind to decide whether we can detect any possible change to the
10991 expression's final value using only hardware watchpoints.
10992
10993 However, I don't think that the values returned by inferior
10994 function calls are special in any way. So this function may not
10995 notice that an expression involving an inferior function call
10996 can't be watched with hardware watchpoints. FIXME. */
a6535de1 10997 for (const value_ref_ptr &iter : vals)
c906108c 10998 {
a6535de1
TT
10999 struct value *v = iter.get ();
11000
5c44784c 11001 if (VALUE_LVAL (v) == lval_memory)
c906108c 11002 {
8464be76
DJ
11003 if (v != head && value_lazy (v))
11004 /* A lazy memory lvalue in the chain is one that GDB never
11005 needed to fetch; we either just used its address (e.g.,
11006 `a' in `a.b') or we never needed it at all (e.g., `a'
11007 in `a,b'). This doesn't apply to HEAD; if that is
11008 lazy then it was not readable, but watch it anyway. */
5c44784c 11009 ;
53a5351d 11010 else
5c44784c
JM
11011 {
11012 /* Ahh, memory we actually used! Check if we can cover
dda83cd7 11013 it with hardware watchpoints. */
df407dfe 11014 struct type *vtype = check_typedef (value_type (v));
2e70b7b9
MS
11015
11016 /* We only watch structs and arrays if user asked for it
11017 explicitly, never if they just happen to appear in a
11018 middle of some value chain. */
11019 if (v == head
78134374
SM
11020 || (vtype->code () != TYPE_CODE_STRUCT
11021 && vtype->code () != TYPE_CODE_ARRAY))
2e70b7b9 11022 {
42ae5230 11023 CORE_ADDR vaddr = value_address (v);
e09342b5
TJB
11024 int len;
11025 int num_regs;
11026
a9634178 11027 len = (target_exact_watchpoints
e09342b5
TJB
11028 && is_scalar_type_recursive (vtype))?
11029 1 : TYPE_LENGTH (value_type (v));
2e70b7b9 11030
e09342b5
TJB
11031 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
11032 if (!num_regs)
2e70b7b9
MS
11033 return 0;
11034 else
e09342b5 11035 found_memory_cnt += num_regs;
2e70b7b9 11036 }
5c44784c 11037 }
c5aa993b 11038 }
5086187c
AC
11039 else if (VALUE_LVAL (v) != not_lval
11040 && deprecated_value_modifiable (v) == 0)
38b6c3b3 11041 return 0; /* These are values from the history (e.g., $1). */
5086187c 11042 else if (VALUE_LVAL (v) == lval_register)
38b6c3b3 11043 return 0; /* Cannot watch a register with a HW watchpoint. */
c906108c
SS
11044 }
11045
11046 /* The expression itself looks suitable for using a hardware
11047 watchpoint, but give the target machine a chance to reject it. */
11048 return found_memory_cnt;
11049}
11050
8b93c638 11051void
2e362716 11052watch_command_wrapper (const char *arg, int from_tty, bool internal)
8b93c638 11053{
84f4c1fe 11054 watch_command_1 (arg, hw_write, from_tty, 0, internal);
06a64a0b
TT
11055}
11056
a15a5258
AB
11057/* Options for the watch, awatch, and rwatch commands. */
11058
11059struct watch_options
11060{
11061 /* For -location. */
11062 bool location = false;
11063};
11064
11065/* Definitions of options for the "watch", "awatch", and "rwatch" commands.
11066
11067 Historically GDB always accepted both '-location' and '-l' flags for
11068 these commands (both flags being synonyms). When converting to the
11069 newer option scheme only '-location' is added here. That's fine (for
11070 backward compatibility) as any non-ambiguous prefix of a flag will be
11071 accepted, so '-l', '-loc', are now all accepted.
11072
11073 What this means is that, if in the future, we add any new flag here
11074 that starts with '-l' then this will break backward compatibility, so
11075 please, don't do that! */
11076
11077static const gdb::option::option_def watch_option_defs[] = {
11078 gdb::option::flag_option_def<watch_options> {
11079 "location",
11080 [] (watch_options *opt) { return &opt->location; },
11081 N_("\
11082This evaluates EXPRESSION and watches the memory to which is refers.\n\
11083-l can be used as a short form of -location."),
11084 },
11085};
11086
11087/* Returns the option group used by 'watch', 'awatch', and 'rwatch'
11088 commands. */
11089
11090static gdb::option::option_def_group
11091make_watch_options_def_group (watch_options *opts)
11092{
11093 return {{watch_option_defs}, opts};
11094}
11095
06a64a0b
TT
11096/* A helper function that looks for the "-location" argument and then
11097 calls watch_command_1. */
11098
11099static void
0b39b52e 11100watch_maybe_just_location (const char *arg, int accessflag, int from_tty)
06a64a0b 11101{
a15a5258
AB
11102 watch_options opts;
11103 auto grp = make_watch_options_def_group (&opts);
11104 gdb::option::process_options
11105 (&arg, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND, grp);
11106 if (arg != nullptr && *arg == '\0')
11107 arg = nullptr;
11108
11109 watch_command_1 (arg, accessflag, from_tty, opts.location, false);
11110}
06a64a0b 11111
a15a5258
AB
11112/* Command completion for 'watch', 'awatch', and 'rwatch' commands. */
11113static void
11114watch_command_completer (struct cmd_list_element *ignore,
11115 completion_tracker &tracker,
11116 const char *text, const char * /*word*/)
11117{
11118 const auto group = make_watch_options_def_group (nullptr);
11119 if (gdb::option::complete_options
11120 (tracker, &text, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND, group))
11121 return;
06a64a0b 11122
a15a5258
AB
11123 const char *word = advance_to_expression_complete_word_point (tracker, text);
11124 expression_completer (ignore, tracker, text, word);
8b93c638 11125}
8926118c 11126
c5aa993b 11127static void
0b39b52e 11128watch_command (const char *arg, int from_tty)
c906108c 11129{
06a64a0b 11130 watch_maybe_just_location (arg, hw_write, from_tty);
c906108c
SS
11131}
11132
8b93c638 11133void
2e362716 11134rwatch_command_wrapper (const char *arg, int from_tty, bool internal)
8b93c638 11135{
84f4c1fe 11136 watch_command_1 (arg, hw_read, from_tty, 0, internal);
8b93c638 11137}
8926118c 11138
c5aa993b 11139static void
0b39b52e 11140rwatch_command (const char *arg, int from_tty)
c906108c 11141{
06a64a0b 11142 watch_maybe_just_location (arg, hw_read, from_tty);
c906108c
SS
11143}
11144
8b93c638 11145void
2e362716 11146awatch_command_wrapper (const char *arg, int from_tty, bool internal)
8b93c638 11147{
84f4c1fe 11148 watch_command_1 (arg, hw_access, from_tty, 0, internal);
8b93c638 11149}
8926118c 11150
c5aa993b 11151static void
0b39b52e 11152awatch_command (const char *arg, int from_tty)
c906108c 11153{
06a64a0b 11154 watch_maybe_just_location (arg, hw_access, from_tty);
c906108c 11155}
c906108c 11156\f
c5aa993b 11157
cfc31633
PA
11158/* Data for the FSM that manages the until(location)/advance commands
11159 in infcmd.c. Here because it uses the mechanisms of
11160 breakpoints. */
c906108c 11161
46e3ed7f 11162struct until_break_fsm : public thread_fsm
bfec99b2 11163{
46e3ed7f 11164 /* The thread that was current when the command was executed. */
cfc31633
PA
11165 int thread;
11166
df631783
PA
11167 /* The breakpoint set at the return address in the caller frame,
11168 plus breakpoints at all the destination locations. */
11169 std::vector<breakpoint_up> breakpoints;
cfc31633 11170
46e3ed7f 11171 until_break_fsm (struct interp *cmd_interp, int thread,
df631783 11172 std::vector<breakpoint_up> &&breakpoints)
46e3ed7f
TT
11173 : thread_fsm (cmd_interp),
11174 thread (thread),
df631783 11175 breakpoints (std::move (breakpoints))
46e3ed7f
TT
11176 {
11177 }
cfc31633 11178
46e3ed7f
TT
11179 void clean_up (struct thread_info *thread) override;
11180 bool should_stop (struct thread_info *thread) override;
11181 enum async_reply_reason do_async_reply_reason () override;
cfc31633
PA
11182};
11183
cfc31633
PA
11184/* Implementation of the 'should_stop' FSM method for the
11185 until(location)/advance commands. */
11186
46e3ed7f
TT
11187bool
11188until_break_fsm::should_stop (struct thread_info *tp)
cfc31633 11189{
df631783
PA
11190 for (const breakpoint_up &bp : breakpoints)
11191 if (bpstat_find_breakpoint (tp->control.stop_bpstat,
11192 bp.get ()) != NULL)
11193 {
11194 set_finished ();
11195 break;
11196 }
cfc31633 11197
46e3ed7f 11198 return true;
cfc31633
PA
11199}
11200
11201/* Implementation of the 'clean_up' FSM method for the
11202 until(location)/advance commands. */
11203
46e3ed7f
TT
11204void
11205until_break_fsm::clean_up (struct thread_info *)
43ff13b4 11206{
cfc31633 11207 /* Clean up our temporary breakpoints. */
df631783 11208 breakpoints.clear ();
46e3ed7f 11209 delete_longjmp_breakpoint (thread);
cfc31633
PA
11210}
11211
11212/* Implementation of the 'async_reply_reason' FSM method for the
11213 until(location)/advance commands. */
11214
46e3ed7f
TT
11215enum async_reply_reason
11216until_break_fsm::do_async_reply_reason ()
cfc31633
PA
11217{
11218 return EXEC_ASYNC_LOCATION_REACHED;
43ff13b4
JM
11219}
11220
c906108c 11221void
f2fc3015 11222until_break_command (const char *arg, int from_tty, int anywhere)
c906108c 11223{
8556afb4
PA
11224 struct frame_info *frame;
11225 struct gdbarch *frame_gdbarch;
11226 struct frame_id stack_frame_id;
11227 struct frame_id caller_frame_id;
186c406b
TT
11228 int thread;
11229 struct thread_info *tp;
c906108c 11230
70509625 11231 clear_proceed_status (0);
c906108c
SS
11232
11233 /* Set a breakpoint where the user wants it and at return from
4a64f543 11234 this function. */
c5aa993b 11235
ffc2605c 11236 event_location_up location = string_to_event_location (&arg, current_language);
f00aae0f 11237
6c5b2ebe
PA
11238 std::vector<symtab_and_line> sals
11239 = (last_displayed_sal_is_valid ()
11240 ? decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
11241 get_last_displayed_symtab (),
11242 get_last_displayed_line ())
11243 : decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE,
cafb3438 11244 NULL, NULL, 0));
c5aa993b 11245
df631783 11246 if (sals.empty ())
8a3fe4f8 11247 error (_("Couldn't get information on specified line."));
c5aa993b 11248
c906108c 11249 if (*arg)
8a3fe4f8 11250 error (_("Junk at end of arguments."));
c5aa993b 11251
186c406b 11252 tp = inferior_thread ();
5d5658a1 11253 thread = tp->global_num;
186c406b 11254
8556afb4
PA
11255 /* Note linespec handling above invalidates the frame chain.
11256 Installing a breakpoint also invalidates the frame chain (as it
11257 may need to switch threads), so do any frame handling before
11258 that. */
11259
11260 frame = get_selected_frame (NULL);
11261 frame_gdbarch = get_frame_arch (frame);
11262 stack_frame_id = get_stack_frame_id (frame);
11263 caller_frame_id = frame_unwind_caller_id (frame);
883bc8d1 11264
ae66c1fc
EZ
11265 /* Keep within the current frame, or in frames called by the current
11266 one. */
edb3359d 11267
df631783 11268 std::vector<breakpoint_up> breakpoints;
5419bdae
TT
11269
11270 gdb::optional<delete_longjmp_breakpoint_cleanup> lj_deleter;
11271
883bc8d1 11272 if (frame_id_p (caller_frame_id))
c906108c 11273 {
883bc8d1 11274 struct symtab_and_line sal2;
cfc31633 11275 struct gdbarch *caller_gdbarch;
883bc8d1
PA
11276
11277 sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
11278 sal2.pc = frame_unwind_caller_pc (frame);
cfc31633 11279 caller_gdbarch = frame_unwind_caller_arch (frame);
df631783
PA
11280
11281 breakpoint_up caller_breakpoint
11282 = set_momentary_breakpoint (caller_gdbarch, sal2,
11283 caller_frame_id, bp_until);
11284 breakpoints.emplace_back (std::move (caller_breakpoint));
186c406b 11285
883bc8d1 11286 set_longjmp_breakpoint (tp, caller_frame_id);
5419bdae 11287 lj_deleter.emplace (thread);
c906108c 11288 }
c5aa993b 11289
c70a6932
JK
11290 /* set_momentary_breakpoint could invalidate FRAME. */
11291 frame = NULL;
11292
df631783
PA
11293 /* If the user told us to continue until a specified location, we
11294 don't specify a frame at which we need to stop. Otherwise,
11295 specify the selected frame, because we want to stop only at the
11296 very same frame. */
11297 frame_id stop_frame_id = anywhere ? null_frame_id : stack_frame_id;
11298
11299 for (symtab_and_line &sal : sals)
11300 {
11301 resolve_sal_pc (&sal);
11302
11303 breakpoint_up location_breakpoint
11304 = set_momentary_breakpoint (frame_gdbarch, sal,
11305 stop_frame_id, bp_until);
11306 breakpoints.emplace_back (std::move (location_breakpoint));
11307 }
883bc8d1 11308
46e3ed7f 11309 tp->thread_fsm = new until_break_fsm (command_interp (), tp->global_num,
df631783 11310 std::move (breakpoints));
f107f563 11311
5419bdae
TT
11312 if (lj_deleter)
11313 lj_deleter->release ();
f107f563 11314
cfc31633 11315 proceed (-1, GDB_SIGNAL_DEFAULT);
c906108c 11316}
ae66c1fc 11317
c906108c
SS
11318/* This function attempts to parse an optional "if <cond>" clause
11319 from the arg string. If one is not found, it returns NULL.
c5aa993b 11320
c906108c
SS
11321 Else, it returns a pointer to the condition string. (It does not
11322 attempt to evaluate the string against a particular block.) And,
11323 it updates arg to point to the first character following the parsed
4a64f543 11324 if clause in the arg string. */
53a5351d 11325
63160a43
PA
11326const char *
11327ep_parse_optional_if_clause (const char **arg)
c906108c 11328{
63160a43 11329 const char *cond_string;
c5aa993b
JM
11330
11331 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
c906108c 11332 return NULL;
c5aa993b 11333
4a64f543 11334 /* Skip the "if" keyword. */
c906108c 11335 (*arg) += 2;
c5aa993b 11336
c906108c 11337 /* Skip any extra leading whitespace, and record the start of the
4a64f543 11338 condition string. */
f1735a53 11339 *arg = skip_spaces (*arg);
c906108c 11340 cond_string = *arg;
c5aa993b 11341
4a64f543
MS
11342 /* Assume that the condition occupies the remainder of the arg
11343 string. */
c906108c 11344 (*arg) += strlen (cond_string);
c5aa993b 11345
c906108c
SS
11346 return cond_string;
11347}
c5aa993b 11348
c906108c
SS
11349/* Commands to deal with catching events, such as signals, exceptions,
11350 process start/exit, etc. */
c5aa993b
JM
11351
11352typedef enum
11353{
44feb3ce
TT
11354 catch_fork_temporary, catch_vfork_temporary,
11355 catch_fork_permanent, catch_vfork_permanent
c5aa993b
JM
11356}
11357catch_fork_kind;
11358
c906108c 11359static void
eb4c3f4a 11360catch_fork_command_1 (const char *arg, int from_tty,
cc59ec59 11361 struct cmd_list_element *command)
c906108c 11362{
a6d9a66e 11363 struct gdbarch *gdbarch = get_current_arch ();
63160a43 11364 const char *cond_string = NULL;
44feb3ce 11365 catch_fork_kind fork_kind;
44feb3ce
TT
11366
11367 fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
b650a282
SM
11368 bool temp = (fork_kind == catch_fork_temporary
11369 || fork_kind == catch_vfork_temporary);
c5aa993b 11370
44feb3ce
TT
11371 if (!arg)
11372 arg = "";
f1735a53 11373 arg = skip_spaces (arg);
c5aa993b 11374
c906108c 11375 /* The allowed syntax is:
c5aa993b
JM
11376 catch [v]fork
11377 catch [v]fork if <cond>
11378
4a64f543 11379 First, check if there's an if clause. */
c906108c 11380 cond_string = ep_parse_optional_if_clause (&arg);
c5aa993b 11381
c906108c 11382 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 11383 error (_("Junk at end of arguments."));
c5aa993b 11384
c906108c 11385 /* If this target supports it, create a fork or vfork catchpoint
4a64f543 11386 and enable reporting of such events. */
c5aa993b
JM
11387 switch (fork_kind)
11388 {
44feb3ce
TT
11389 case catch_fork_temporary:
11390 case catch_fork_permanent:
b650a282 11391 create_fork_vfork_event_catchpoint (gdbarch, temp, cond_string,
dda83cd7 11392 &catch_fork_breakpoint_ops);
c906108c 11393 break;
44feb3ce
TT
11394 case catch_vfork_temporary:
11395 case catch_vfork_permanent:
b650a282 11396 create_fork_vfork_event_catchpoint (gdbarch, temp, cond_string,
dda83cd7 11397 &catch_vfork_breakpoint_ops);
c906108c 11398 break;
c5aa993b 11399 default:
8a3fe4f8 11400 error (_("unsupported or unknown fork kind; cannot catch it"));
c906108c 11401 break;
c5aa993b 11402 }
c906108c
SS
11403}
11404
11405static void
eb4c3f4a 11406catch_exec_command_1 (const char *arg, int from_tty,
cc59ec59 11407 struct cmd_list_element *command)
c906108c 11408{
a6d9a66e 11409 struct gdbarch *gdbarch = get_current_arch ();
63160a43 11410 const char *cond_string = NULL;
b650a282 11411 bool temp = get_cmd_context (command) == CATCH_TEMPORARY;
44feb3ce
TT
11412
11413 if (!arg)
11414 arg = "";
f1735a53 11415 arg = skip_spaces (arg);
c906108c
SS
11416
11417 /* The allowed syntax is:
c5aa993b
JM
11418 catch exec
11419 catch exec if <cond>
c906108c 11420
4a64f543 11421 First, check if there's an if clause. */
c906108c
SS
11422 cond_string = ep_parse_optional_if_clause (&arg);
11423
11424 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 11425 error (_("Junk at end of arguments."));
c906108c 11426
b270e6f9 11427 std::unique_ptr<exec_catchpoint> c (new exec_catchpoint ());
b650a282 11428 init_catchpoint (c.get (), gdbarch, temp, cond_string,
b4d90040
PA
11429 &catch_exec_breakpoint_ops);
11430 c->exec_pathname = NULL;
11431
b270e6f9 11432 install_breakpoint (0, std::move (c), 1);
c906108c 11433}
c5aa993b 11434
9ac4176b 11435void
28010a5d
PA
11436init_ada_exception_breakpoint (struct breakpoint *b,
11437 struct gdbarch *gdbarch,
11438 struct symtab_and_line sal,
f2fc3015 11439 const char *addr_string,
c0a91b2b 11440 const struct breakpoint_ops *ops,
28010a5d 11441 int tempflag,
349774ef 11442 int enabled,
28010a5d 11443 int from_tty)
f7f9143b 11444{
f7f9143b
JB
11445 if (from_tty)
11446 {
5af949e3
UW
11447 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
11448 if (!loc_gdbarch)
11449 loc_gdbarch = gdbarch;
11450
6c95b8df
PA
11451 describe_other_breakpoints (loc_gdbarch,
11452 sal.pspace, sal.pc, sal.section, -1);
f7f9143b 11453 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
dda83cd7
SM
11454 version for exception catchpoints, because two catchpoints
11455 used for different exception names will use the same address.
11456 In this case, a "breakpoint ... also set at..." warning is
11457 unproductive. Besides, the warning phrasing is also a bit
11458 inappropriate, we should use the word catchpoint, and tell
11459 the user what type of catchpoint it is. The above is good
11460 enough for now, though. */
f7f9143b
JB
11461 }
11462
f06f1252 11463 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
f7f9143b 11464
349774ef 11465 b->enable_state = enabled ? bp_enabled : bp_disabled;
f7f9143b 11466 b->disposition = tempflag ? disp_del : disp_donttouch;
d28cd78a
TT
11467 b->location = string_to_event_location (&addr_string,
11468 language_def (language_ada));
f7f9143b 11469 b->language = language_ada;
f7f9143b
JB
11470}
11471
c906108c
SS
11472\f
11473
81b1e71c 11474/* Compare two breakpoints and return a strcmp-like result. */
8a2c437b
TT
11475
11476static int
81b1e71c 11477compare_breakpoints (const breakpoint *a, const breakpoint *b)
8a2c437b 11478{
81b1e71c
TT
11479 uintptr_t ua = (uintptr_t) a;
11480 uintptr_t ub = (uintptr_t) b;
8a2c437b 11481
81b1e71c 11482 if (a->number < b->number)
8a2c437b 11483 return -1;
81b1e71c 11484 else if (a->number > b->number)
8a2c437b
TT
11485 return 1;
11486
11487 /* Now sort by address, in case we see, e..g, two breakpoints with
11488 the number 0. */
11489 if (ua < ub)
11490 return -1;
94b0e70d 11491 return ua > ub ? 1 : 0;
8a2c437b
TT
11492}
11493
80f8a6eb 11494/* Delete breakpoints by address or line. */
c906108c
SS
11495
11496static void
0b39b52e 11497clear_command (const char *arg, int from_tty)
c906108c 11498{
81b1e71c 11499 struct breakpoint *b;
c906108c 11500 int default_match;
c906108c 11501
6c5b2ebe
PA
11502 std::vector<symtab_and_line> decoded_sals;
11503 symtab_and_line last_sal;
11504 gdb::array_view<symtab_and_line> sals;
c906108c
SS
11505 if (arg)
11506 {
6c5b2ebe
PA
11507 decoded_sals
11508 = decode_line_with_current_source (arg,
11509 (DECODE_LINE_FUNFIRSTLINE
11510 | DECODE_LINE_LIST_MODE));
c906108c 11511 default_match = 0;
6c5b2ebe 11512 sals = decoded_sals;
c906108c
SS
11513 }
11514 else
11515 {
1bfeeb0f
JL
11516 /* Set sal's line, symtab, pc, and pspace to the values
11517 corresponding to the last call to print_frame_info. If the
11518 codepoint is not valid, this will set all the fields to 0. */
51abb421 11519 last_sal = get_last_displayed_sal ();
6c5b2ebe 11520 if (last_sal.symtab == 0)
8a3fe4f8 11521 error (_("No source file specified."));
c906108c 11522
c906108c 11523 default_match = 1;
6c5b2ebe 11524 sals = last_sal;
c906108c
SS
11525 }
11526
4a64f543
MS
11527 /* We don't call resolve_sal_pc here. That's not as bad as it
11528 seems, because all existing breakpoints typically have both
11529 file/line and pc set. So, if clear is given file/line, we can
11530 match this to existing breakpoint without obtaining pc at all.
ed0616c6
VP
11531
11532 We only support clearing given the address explicitly
11533 present in breakpoint table. Say, we've set breakpoint
4a64f543 11534 at file:line. There were several PC values for that file:line,
ed0616c6 11535 due to optimization, all in one block.
4a64f543
MS
11536
11537 We've picked one PC value. If "clear" is issued with another
ed0616c6
VP
11538 PC corresponding to the same file:line, the breakpoint won't
11539 be cleared. We probably can still clear the breakpoint, but
11540 since the other PC value is never presented to user, user
11541 can only find it by guessing, and it does not seem important
11542 to support that. */
11543
4a64f543
MS
11544 /* For each line spec given, delete bps which correspond to it. Do
11545 it in two passes, solely to preserve the current behavior that
11546 from_tty is forced true if we delete more than one
11547 breakpoint. */
c906108c 11548
81b1e71c 11549 std::vector<struct breakpoint *> found;
6c5b2ebe 11550 for (const auto &sal : sals)
c906108c 11551 {
05cba821
JK
11552 const char *sal_fullname;
11553
c906108c 11554 /* If exact pc given, clear bpts at that pc.
dda83cd7
SM
11555 If line given (pc == 0), clear all bpts on specified line.
11556 If defaulting, clear all bpts on default line
11557 or at default pc.
c5aa993b 11558
dda83cd7 11559 defaulting sal.pc != 0 tests to do
c5aa993b 11560
dda83cd7
SM
11561 0 1 pc
11562 1 1 pc _and_ line
11563 0 0 line
11564 1 0 <can't happen> */
c906108c 11565
05cba821
JK
11566 sal_fullname = (sal.symtab == NULL
11567 ? NULL : symtab_to_fullname (sal.symtab));
c906108c 11568
4a64f543 11569 /* Find all matching breakpoints and add them to 'found'. */
d6e956e5 11570 ALL_BREAKPOINTS (b)
c5aa993b 11571 {
0d381245 11572 int match = 0;
4a64f543 11573 /* Are we going to delete b? */
cc60f2e3 11574 if (b->type != bp_none && !is_watchpoint (b))
0d381245
VP
11575 {
11576 struct bp_location *loc = b->loc;
11577 for (; loc; loc = loc->next)
11578 {
f8eba3c6
TT
11579 /* If the user specified file:line, don't allow a PC
11580 match. This matches historical gdb behavior. */
11581 int pc_match = (!sal.explicit_line
11582 && sal.pc
11583 && (loc->pspace == sal.pspace)
11584 && (loc->address == sal.pc)
11585 && (!section_is_overlay (loc->section)
11586 || loc->section == sal.section));
4aac40c8
TT
11587 int line_match = 0;
11588
11589 if ((default_match || sal.explicit_line)
2f202fde 11590 && loc->symtab != NULL
05cba821 11591 && sal_fullname != NULL
4aac40c8 11592 && sal.pspace == loc->pspace
05cba821
JK
11593 && loc->line_number == sal.line
11594 && filename_cmp (symtab_to_fullname (loc->symtab),
11595 sal_fullname) == 0)
11596 line_match = 1;
4aac40c8 11597
0d381245
VP
11598 if (pc_match || line_match)
11599 {
11600 match = 1;
11601 break;
11602 }
11603 }
11604 }
11605
11606 if (match)
81b1e71c 11607 found.push_back (b);
c906108c 11608 }
80f8a6eb 11609 }
8a2c437b 11610
80f8a6eb 11611 /* Now go thru the 'found' chain and delete them. */
81b1e71c 11612 if (found.empty ())
80f8a6eb
MS
11613 {
11614 if (arg)
8a3fe4f8 11615 error (_("No breakpoint at %s."), arg);
80f8a6eb 11616 else
8a3fe4f8 11617 error (_("No breakpoint at this line."));
80f8a6eb 11618 }
c906108c 11619
8a2c437b 11620 /* Remove duplicates from the vec. */
81b1e71c 11621 std::sort (found.begin (), found.end (),
b926417a 11622 [] (const breakpoint *bp_a, const breakpoint *bp_b)
81b1e71c 11623 {
b926417a 11624 return compare_breakpoints (bp_a, bp_b) < 0;
81b1e71c
TT
11625 });
11626 found.erase (std::unique (found.begin (), found.end (),
b926417a 11627 [] (const breakpoint *bp_a, const breakpoint *bp_b)
81b1e71c 11628 {
b926417a 11629 return compare_breakpoints (bp_a, bp_b) == 0;
81b1e71c
TT
11630 }),
11631 found.end ());
8a2c437b 11632
81b1e71c 11633 if (found.size () > 1)
4a64f543 11634 from_tty = 1; /* Always report if deleted more than one. */
80f8a6eb 11635 if (from_tty)
a3f17187 11636 {
81b1e71c 11637 if (found.size () == 1)
a3f17187
AC
11638 printf_unfiltered (_("Deleted breakpoint "));
11639 else
11640 printf_unfiltered (_("Deleted breakpoints "));
11641 }
d6e956e5 11642
81b1e71c 11643 for (breakpoint *iter : found)
80f8a6eb 11644 {
c5aa993b 11645 if (from_tty)
81b1e71c
TT
11646 printf_unfiltered ("%d ", iter->number);
11647 delete_breakpoint (iter);
c906108c 11648 }
80f8a6eb
MS
11649 if (from_tty)
11650 putchar_unfiltered ('\n');
c906108c
SS
11651}
11652\f
11653/* Delete breakpoint in BS if they are `delete' breakpoints and
11654 all breakpoints that are marked for deletion, whether hit or not.
11655 This is called after any breakpoint is hit, or after errors. */
11656
11657void
fba45db2 11658breakpoint_auto_delete (bpstat bs)
c906108c 11659{
35df4500 11660 struct breakpoint *b, *b_tmp;
c906108c
SS
11661
11662 for (; bs; bs = bs->next)
f431efe5
PA
11663 if (bs->breakpoint_at
11664 && bs->breakpoint_at->disposition == disp_del
c906108c 11665 && bs->stop)
f431efe5 11666 delete_breakpoint (bs->breakpoint_at);
c906108c 11667
35df4500 11668 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 11669 {
b5de0fa7 11670 if (b->disposition == disp_del_at_next_stop)
c5aa993b
JM
11671 delete_breakpoint (b);
11672 }
c906108c
SS
11673}
11674
4a64f543 11675/* A comparison function for bp_location AP and BP being interfaced to
39ef2f62 11676 std::sort. Sort elements primarily by their ADDRESS (no matter what
cb1e4e32
PA
11677 bl_address_is_meaningful says), secondarily by ordering first
11678 permanent elements and terciarily just ensuring the array is sorted
39ef2f62 11679 stable way despite std::sort being an unstable algorithm. */
876fa593
JK
11680
11681static int
39ef2f62 11682bp_location_is_less_than (const bp_location *a, const bp_location *b)
876fa593 11683{
876fa593 11684 if (a->address != b->address)
39ef2f62 11685 return a->address < b->address;
876fa593 11686
dea2aa5f
LM
11687 /* Sort locations at the same address by their pspace number, keeping
11688 locations of the same inferior (in a multi-inferior environment)
11689 grouped. */
11690
11691 if (a->pspace->num != b->pspace->num)
39ef2f62 11692 return a->pspace->num < b->pspace->num;
dea2aa5f 11693
876fa593 11694 /* Sort permanent breakpoints first. */
1a853c52 11695 if (a->permanent != b->permanent)
39ef2f62 11696 return a->permanent > b->permanent;
876fa593 11697
7f32a4d5
PA
11698 /* Sort by type in order to make duplicate determination easier.
11699 See update_global_location_list. This is kept in sync with
11700 breakpoint_locations_match. */
11701 if (a->loc_type < b->loc_type)
11702 return true;
11703
11704 /* Likewise, for range-breakpoints, sort by length. */
11705 if (a->loc_type == bp_loc_hardware_breakpoint
11706 && b->loc_type == bp_loc_hardware_breakpoint
11707 && a->length < b->length)
11708 return true;
11709
c56a97f9
JK
11710 /* Make the internal GDB representation stable across GDB runs
11711 where A and B memory inside GDB can differ. Breakpoint locations of
11712 the same type at the same address can be sorted in arbitrary order. */
876fa593
JK
11713
11714 if (a->owner->number != b->owner->number)
39ef2f62 11715 return a->owner->number < b->owner->number;
876fa593 11716
39ef2f62 11717 return a < b;
876fa593
JK
11718}
11719
f5336ca5
PA
11720/* Set bp_locations_placed_address_before_address_max and
11721 bp_locations_shadow_len_after_address_max according to the current
11722 content of the bp_locations array. */
f7545552
TT
11723
11724static void
f5336ca5 11725bp_locations_target_extensions_update (void)
f7545552 11726{
876fa593
JK
11727 struct bp_location *bl, **blp_tmp;
11728
f5336ca5
PA
11729 bp_locations_placed_address_before_address_max = 0;
11730 bp_locations_shadow_len_after_address_max = 0;
876fa593
JK
11731
11732 ALL_BP_LOCATIONS (bl, blp_tmp)
11733 {
11734 CORE_ADDR start, end, addr;
11735
11736 if (!bp_location_has_shadow (bl))
11737 continue;
11738
11739 start = bl->target_info.placed_address;
11740 end = start + bl->target_info.shadow_len;
11741
11742 gdb_assert (bl->address >= start);
11743 addr = bl->address - start;
f5336ca5
PA
11744 if (addr > bp_locations_placed_address_before_address_max)
11745 bp_locations_placed_address_before_address_max = addr;
876fa593
JK
11746
11747 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
11748
11749 gdb_assert (bl->address < end);
11750 addr = end - bl->address;
f5336ca5
PA
11751 if (addr > bp_locations_shadow_len_after_address_max)
11752 bp_locations_shadow_len_after_address_max = addr;
876fa593 11753 }
f7545552
TT
11754}
11755
1e4d1764
YQ
11756/* Download tracepoint locations if they haven't been. */
11757
11758static void
11759download_tracepoint_locations (void)
11760{
7ed2c994 11761 struct breakpoint *b;
dd2e65cc 11762 enum tribool can_download_tracepoint = TRIBOOL_UNKNOWN;
1e4d1764 11763
5ed8105e 11764 scoped_restore_current_pspace_and_thread restore_pspace_thread;
1e4d1764 11765
7ed2c994 11766 ALL_TRACEPOINTS (b)
1e4d1764 11767 {
7ed2c994 11768 struct bp_location *bl;
1e4d1764 11769 struct tracepoint *t;
f2a8bc8a 11770 int bp_location_downloaded = 0;
1e4d1764 11771
7ed2c994 11772 if ((b->type == bp_fast_tracepoint
1e4d1764
YQ
11773 ? !may_insert_fast_tracepoints
11774 : !may_insert_tracepoints))
11775 continue;
11776
dd2e65cc
YQ
11777 if (can_download_tracepoint == TRIBOOL_UNKNOWN)
11778 {
11779 if (target_can_download_tracepoint ())
11780 can_download_tracepoint = TRIBOOL_TRUE;
11781 else
11782 can_download_tracepoint = TRIBOOL_FALSE;
11783 }
11784
11785 if (can_download_tracepoint == TRIBOOL_FALSE)
11786 break;
11787
7ed2c994
YQ
11788 for (bl = b->loc; bl; bl = bl->next)
11789 {
11790 /* In tracepoint, locations are _never_ duplicated, so
11791 should_be_inserted is equivalent to
11792 unduplicated_should_be_inserted. */
11793 if (!should_be_inserted (bl) || bl->inserted)
11794 continue;
1e4d1764 11795
7ed2c994 11796 switch_to_program_space_and_thread (bl->pspace);
1e4d1764 11797
7ed2c994 11798 target_download_tracepoint (bl);
1e4d1764 11799
7ed2c994 11800 bl->inserted = 1;
f2a8bc8a 11801 bp_location_downloaded = 1;
7ed2c994
YQ
11802 }
11803 t = (struct tracepoint *) b;
11804 t->number_on_target = b->number;
f2a8bc8a 11805 if (bp_location_downloaded)
76727919 11806 gdb::observers::breakpoint_modified.notify (b);
1e4d1764 11807 }
1e4d1764
YQ
11808}
11809
934709f0
PW
11810/* Swap the insertion/duplication state between two locations. */
11811
11812static void
11813swap_insertion (struct bp_location *left, struct bp_location *right)
11814{
11815 const int left_inserted = left->inserted;
11816 const int left_duplicate = left->duplicate;
b775012e 11817 const int left_needs_update = left->needs_update;
934709f0
PW
11818 const struct bp_target_info left_target_info = left->target_info;
11819
1e4d1764
YQ
11820 /* Locations of tracepoints can never be duplicated. */
11821 if (is_tracepoint (left->owner))
11822 gdb_assert (!left->duplicate);
11823 if (is_tracepoint (right->owner))
11824 gdb_assert (!right->duplicate);
11825
934709f0
PW
11826 left->inserted = right->inserted;
11827 left->duplicate = right->duplicate;
b775012e 11828 left->needs_update = right->needs_update;
934709f0
PW
11829 left->target_info = right->target_info;
11830 right->inserted = left_inserted;
11831 right->duplicate = left_duplicate;
b775012e 11832 right->needs_update = left_needs_update;
934709f0
PW
11833 right->target_info = left_target_info;
11834}
11835
b775012e
LM
11836/* Force the re-insertion of the locations at ADDRESS. This is called
11837 once a new/deleted/modified duplicate location is found and we are evaluating
11838 conditions on the target's side. Such conditions need to be updated on
11839 the target. */
11840
11841static void
11842force_breakpoint_reinsertion (struct bp_location *bl)
11843{
11844 struct bp_location **locp = NULL, **loc2p;
11845 struct bp_location *loc;
11846 CORE_ADDR address = 0;
11847 int pspace_num;
11848
11849 address = bl->address;
11850 pspace_num = bl->pspace->num;
11851
11852 /* This is only meaningful if the target is
11853 evaluating conditions and if the user has
11854 opted for condition evaluation on the target's
11855 side. */
11856 if (gdb_evaluates_breakpoint_condition_p ()
11857 || !target_supports_evaluation_of_breakpoint_conditions ())
11858 return;
11859
11860 /* Flag all breakpoint locations with this address and
11861 the same program space as the location
11862 as "its condition has changed". We need to
11863 update the conditions on the target's side. */
11864 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, address)
11865 {
11866 loc = *loc2p;
11867
11868 if (!is_breakpoint (loc->owner)
11869 || pspace_num != loc->pspace->num)
11870 continue;
11871
11872 /* Flag the location appropriately. We use a different state to
11873 let everyone know that we already updated the set of locations
11874 with addr bl->address and program space bl->pspace. This is so
11875 we don't have to keep calling these functions just to mark locations
11876 that have already been marked. */
11877 loc->condition_changed = condition_updated;
11878
11879 /* Free the agent expression bytecode as well. We will compute
11880 it later on. */
833177a4 11881 loc->cond_bytecode.reset ();
b775012e
LM
11882 }
11883}
7f32a4d5 11884
44702360
PA
11885/* Called whether new breakpoints are created, or existing breakpoints
11886 deleted, to update the global location list and recompute which
11887 locations are duplicate of which.
b775012e 11888
04086b45
PA
11889 The INSERT_MODE flag determines whether locations may not, may, or
11890 shall be inserted now. See 'enum ugll_insert_mode' for more
11891 info. */
b60e7edf 11892
0d381245 11893static void
44702360 11894update_global_location_list (enum ugll_insert_mode insert_mode)
0d381245 11895{
74960c60 11896 struct breakpoint *b;
876fa593 11897 struct bp_location **locp, *loc;
b775012e
LM
11898 /* Last breakpoint location address that was marked for update. */
11899 CORE_ADDR last_addr = 0;
11900 /* Last breakpoint location program space that was marked for update. */
11901 int last_pspace_num = -1;
f7545552 11902
2d134ed3
PA
11903 /* Used in the duplicates detection below. When iterating over all
11904 bp_locations, points to the first bp_location of a given address.
11905 Breakpoints and watchpoints of different types are never
11906 duplicates of each other. Keep one pointer for each type of
11907 breakpoint/watchpoint, so we only need to loop over all locations
11908 once. */
11909 struct bp_location *bp_loc_first; /* breakpoint */
11910 struct bp_location *wp_loc_first; /* hardware watchpoint */
11911 struct bp_location *awp_loc_first; /* access watchpoint */
11912 struct bp_location *rwp_loc_first; /* read watchpoint */
876fa593 11913
f5336ca5
PA
11914 /* Saved former bp_locations array which we compare against the newly
11915 built bp_locations from the current state of ALL_BREAKPOINTS. */
81b1e71c 11916 struct bp_location **old_locp;
f5336ca5 11917 unsigned old_locations_count;
81b1e71c 11918 gdb::unique_xmalloc_ptr<struct bp_location *> old_locations (bp_locations);
876fa593 11919
f5336ca5
PA
11920 old_locations_count = bp_locations_count;
11921 bp_locations = NULL;
11922 bp_locations_count = 0;
0d381245 11923
74960c60 11924 ALL_BREAKPOINTS (b)
876fa593 11925 for (loc = b->loc; loc; loc = loc->next)
f5336ca5 11926 bp_locations_count++;
876fa593 11927
f5336ca5
PA
11928 bp_locations = XNEWVEC (struct bp_location *, bp_locations_count);
11929 locp = bp_locations;
876fa593
JK
11930 ALL_BREAKPOINTS (b)
11931 for (loc = b->loc; loc; loc = loc->next)
11932 *locp++ = loc;
7f32a4d5
PA
11933
11934 /* See if we need to "upgrade" a software breakpoint to a hardware
11935 breakpoint. Do this before deciding whether locations are
11936 duplicates. Also do this before sorting because sorting order
11937 depends on location type. */
11938 for (locp = bp_locations;
11939 locp < bp_locations + bp_locations_count;
11940 locp++)
11941 {
11942 loc = *locp;
11943 if (!loc->inserted && should_be_inserted (loc))
11944 handle_automatic_hardware_breakpoints (loc);
11945 }
11946
39ef2f62
CB
11947 std::sort (bp_locations, bp_locations + bp_locations_count,
11948 bp_location_is_less_than);
876fa593 11949
f5336ca5 11950 bp_locations_target_extensions_update ();
74960c60 11951
4a64f543
MS
11952 /* Identify bp_location instances that are no longer present in the
11953 new list, and therefore should be freed. Note that it's not
11954 necessary that those locations should be removed from inferior --
11955 if there's another location at the same address (previously
11956 marked as duplicate), we don't need to remove/insert the
11957 location.
876fa593 11958
4a64f543
MS
11959 LOCP is kept in sync with OLD_LOCP, each pointing to the current
11960 and former bp_location array state respectively. */
876fa593 11961
f5336ca5 11962 locp = bp_locations;
81b1e71c
TT
11963 for (old_locp = old_locations.get ();
11964 old_locp < old_locations.get () + old_locations_count;
876fa593 11965 old_locp++)
74960c60 11966 {
876fa593 11967 struct bp_location *old_loc = *old_locp;
c7d46a38 11968 struct bp_location **loc2p;
876fa593 11969
e5dd4106 11970 /* Tells if 'old_loc' is found among the new locations. If
4a64f543 11971 not, we have to free it. */
c7d46a38 11972 int found_object = 0;
20874c92
VP
11973 /* Tells if the location should remain inserted in the target. */
11974 int keep_in_target = 0;
11975 int removed = 0;
876fa593 11976
4a64f543
MS
11977 /* Skip LOCP entries which will definitely never be needed.
11978 Stop either at or being the one matching OLD_LOC. */
f5336ca5 11979 while (locp < bp_locations + bp_locations_count
c7d46a38 11980 && (*locp)->address < old_loc->address)
876fa593 11981 locp++;
c7d46a38
PA
11982
11983 for (loc2p = locp;
f5336ca5 11984 (loc2p < bp_locations + bp_locations_count
c7d46a38
PA
11985 && (*loc2p)->address == old_loc->address);
11986 loc2p++)
11987 {
b775012e
LM
11988 /* Check if this is a new/duplicated location or a duplicated
11989 location that had its condition modified. If so, we want to send
11990 its condition to the target if evaluation of conditions is taking
11991 place there. */
11992 if ((*loc2p)->condition_changed == condition_modified
11993 && (last_addr != old_loc->address
11994 || last_pspace_num != old_loc->pspace->num))
c7d46a38 11995 {
b775012e
LM
11996 force_breakpoint_reinsertion (*loc2p);
11997 last_pspace_num = old_loc->pspace->num;
c7d46a38 11998 }
b775012e
LM
11999
12000 if (*loc2p == old_loc)
12001 found_object = 1;
c7d46a38 12002 }
74960c60 12003
b775012e
LM
12004 /* We have already handled this address, update it so that we don't
12005 have to go through updates again. */
12006 last_addr = old_loc->address;
12007
12008 /* Target-side condition evaluation: Handle deleted locations. */
12009 if (!found_object)
12010 force_breakpoint_reinsertion (old_loc);
12011
4a64f543
MS
12012 /* If this location is no longer present, and inserted, look if
12013 there's maybe a new location at the same address. If so,
12014 mark that one inserted, and don't remove this one. This is
12015 needed so that we don't have a time window where a breakpoint
12016 at certain location is not inserted. */
74960c60 12017
876fa593 12018 if (old_loc->inserted)
0d381245 12019 {
4a64f543
MS
12020 /* If the location is inserted now, we might have to remove
12021 it. */
74960c60 12022
876fa593 12023 if (found_object && should_be_inserted (old_loc))
74960c60 12024 {
4a64f543
MS
12025 /* The location is still present in the location list,
12026 and still should be inserted. Don't do anything. */
20874c92 12027 keep_in_target = 1;
74960c60
VP
12028 }
12029 else
12030 {
b775012e
LM
12031 /* This location still exists, but it won't be kept in the
12032 target since it may have been disabled. We proceed to
12033 remove its target-side condition. */
12034
4a64f543
MS
12035 /* The location is either no longer present, or got
12036 disabled. See if there's another location at the
12037 same address, in which case we don't need to remove
12038 this one from the target. */
876fa593 12039
2bdf28a0 12040 /* OLD_LOC comes from existing struct breakpoint. */
cb1e4e32 12041 if (bl_address_is_meaningful (old_loc))
876fa593 12042 {
876fa593 12043 for (loc2p = locp;
f5336ca5 12044 (loc2p < bp_locations + bp_locations_count
c7d46a38 12045 && (*loc2p)->address == old_loc->address);
876fa593
JK
12046 loc2p++)
12047 {
12048 struct bp_location *loc2 = *loc2p;
12049
7f32a4d5
PA
12050 if (loc2 == old_loc)
12051 continue;
12052
2d134ed3 12053 if (breakpoint_locations_match (loc2, old_loc))
c7d46a38 12054 {
85d721b8
PA
12055 /* Read watchpoint locations are switched to
12056 access watchpoints, if the former are not
12057 supported, but the latter are. */
12058 if (is_hardware_watchpoint (old_loc->owner))
12059 {
12060 gdb_assert (is_hardware_watchpoint (loc2->owner));
12061 loc2->watchpoint_type = old_loc->watchpoint_type;
12062 }
12063
934709f0
PW
12064 /* loc2 is a duplicated location. We need to check
12065 if it should be inserted in case it will be
12066 unduplicated. */
7f32a4d5 12067 if (unduplicated_should_be_inserted (loc2))
c7d46a38 12068 {
934709f0 12069 swap_insertion (old_loc, loc2);
c7d46a38
PA
12070 keep_in_target = 1;
12071 break;
12072 }
876fa593
JK
12073 }
12074 }
12075 }
74960c60
VP
12076 }
12077
20874c92
VP
12078 if (!keep_in_target)
12079 {
834c0d03 12080 if (remove_breakpoint (old_loc))
20874c92 12081 {
4a64f543
MS
12082 /* This is just about all we can do. We could keep
12083 this location on the global list, and try to
12084 remove it next time, but there's no particular
12085 reason why we will succeed next time.
20874c92 12086
4a64f543
MS
12087 Note that at this point, old_loc->owner is still
12088 valid, as delete_breakpoint frees the breakpoint
12089 only after calling us. */
3e43a32a
MS
12090 printf_filtered (_("warning: Error removing "
12091 "breakpoint %d\n"),
876fa593 12092 old_loc->owner->number);
20874c92
VP
12093 }
12094 removed = 1;
12095 }
0d381245 12096 }
74960c60
VP
12097
12098 if (!found_object)
1c5cfe86 12099 {
fbea99ea 12100 if (removed && target_is_non_stop_p ()
1cf4d951 12101 && need_moribund_for_location_type (old_loc))
20874c92 12102 {
db82e815
PA
12103 /* This location was removed from the target. In
12104 non-stop mode, a race condition is possible where
12105 we've removed a breakpoint, but stop events for that
12106 breakpoint are already queued and will arrive later.
12107 We apply an heuristic to be able to distinguish such
12108 SIGTRAPs from other random SIGTRAPs: we keep this
12109 breakpoint location for a bit, and will retire it
12110 after we see some number of events. The theory here
12111 is that reporting of events should, "on the average",
12112 be fair, so after a while we'll see events from all
12113 threads that have anything of interest, and no longer
12114 need to keep this breakpoint location around. We
12115 don't hold locations forever so to reduce chances of
12116 mistaking a non-breakpoint SIGTRAP for a breakpoint
12117 SIGTRAP.
12118
12119 The heuristic failing can be disastrous on
12120 decr_pc_after_break targets.
12121
12122 On decr_pc_after_break targets, like e.g., x86-linux,
12123 if we fail to recognize a late breakpoint SIGTRAP,
12124 because events_till_retirement has reached 0 too
12125 soon, we'll fail to do the PC adjustment, and report
12126 a random SIGTRAP to the user. When the user resumes
12127 the inferior, it will most likely immediately crash
2dec564e 12128 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
db82e815
PA
12129 corrupted, because of being resumed e.g., in the
12130 middle of a multi-byte instruction, or skipped a
12131 one-byte instruction. This was actually seen happen
12132 on native x86-linux, and should be less rare on
12133 targets that do not support new thread events, like
12134 remote, due to the heuristic depending on
12135 thread_count.
12136
12137 Mistaking a random SIGTRAP for a breakpoint trap
12138 causes similar symptoms (PC adjustment applied when
12139 it shouldn't), but then again, playing with SIGTRAPs
12140 behind the debugger's back is asking for trouble.
12141
12142 Since hardware watchpoint traps are always
12143 distinguishable from other traps, so we don't need to
12144 apply keep hardware watchpoint moribund locations
12145 around. We simply always ignore hardware watchpoint
12146 traps we can no longer explain. */
12147
5b6d1e4f
PA
12148 process_stratum_target *proc_target = nullptr;
12149 for (inferior *inf : all_inferiors ())
12150 if (inf->pspace == old_loc->pspace)
12151 {
12152 proc_target = inf->process_target ();
12153 break;
12154 }
12155 if (proc_target != nullptr)
12156 old_loc->events_till_retirement
12157 = 3 * (thread_count (proc_target) + 1);
12158 else
12159 old_loc->events_till_retirement = 1;
876fa593 12160 old_loc->owner = NULL;
20874c92 12161
1123588c 12162 moribund_locations.push_back (old_loc);
1c5cfe86
PA
12163 }
12164 else
f431efe5
PA
12165 {
12166 old_loc->owner = NULL;
12167 decref_bp_location (&old_loc);
12168 }
20874c92 12169 }
74960c60 12170 }
1c5cfe86 12171
348d480f
PA
12172 /* Rescan breakpoints at the same address and section, marking the
12173 first one as "first" and any others as "duplicates". This is so
12174 that the bpt instruction is only inserted once. If we have a
12175 permanent breakpoint at the same place as BPT, make that one the
12176 official one, and the rest as duplicates. Permanent breakpoints
12177 are sorted first for the same address.
12178
12179 Do the same for hardware watchpoints, but also considering the
12180 watchpoint's type (regular/access/read) and length. */
12181
12182 bp_loc_first = NULL;
12183 wp_loc_first = NULL;
12184 awp_loc_first = NULL;
12185 rwp_loc_first = NULL;
12186 ALL_BP_LOCATIONS (loc, locp)
12187 {
12188 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
12189 non-NULL. */
348d480f 12190 struct bp_location **loc_first_p;
d3fbdd86 12191 b = loc->owner;
348d480f 12192
6f380991 12193 if (!unduplicated_should_be_inserted (loc)
cb1e4e32 12194 || !bl_address_is_meaningful (loc)
1e4d1764
YQ
12195 /* Don't detect duplicate for tracepoint locations because they are
12196 never duplicated. See the comments in field `duplicate' of
12197 `struct bp_location'. */
348d480f 12198 || is_tracepoint (b))
b775012e
LM
12199 {
12200 /* Clear the condition modification flag. */
12201 loc->condition_changed = condition_unchanged;
12202 continue;
12203 }
348d480f 12204
348d480f
PA
12205 if (b->type == bp_hardware_watchpoint)
12206 loc_first_p = &wp_loc_first;
12207 else if (b->type == bp_read_watchpoint)
12208 loc_first_p = &rwp_loc_first;
12209 else if (b->type == bp_access_watchpoint)
12210 loc_first_p = &awp_loc_first;
12211 else
12212 loc_first_p = &bp_loc_first;
12213
12214 if (*loc_first_p == NULL
12215 || (overlay_debugging && loc->section != (*loc_first_p)->section)
12216 || !breakpoint_locations_match (loc, *loc_first_p))
12217 {
12218 *loc_first_p = loc;
12219 loc->duplicate = 0;
b775012e
LM
12220
12221 if (is_breakpoint (loc->owner) && loc->condition_changed)
12222 {
12223 loc->needs_update = 1;
12224 /* Clear the condition modification flag. */
12225 loc->condition_changed = condition_unchanged;
12226 }
348d480f
PA
12227 continue;
12228 }
12229
934709f0
PW
12230
12231 /* This and the above ensure the invariant that the first location
12232 is not duplicated, and is the inserted one.
12233 All following are marked as duplicated, and are not inserted. */
12234 if (loc->inserted)
12235 swap_insertion (loc, *loc_first_p);
348d480f
PA
12236 loc->duplicate = 1;
12237
b775012e
LM
12238 /* Clear the condition modification flag. */
12239 loc->condition_changed = condition_unchanged;
348d480f
PA
12240 }
12241
a25a5a45 12242 if (insert_mode == UGLL_INSERT || breakpoints_should_be_inserted_now ())
b775012e 12243 {
04086b45 12244 if (insert_mode != UGLL_DONT_INSERT)
b775012e
LM
12245 insert_breakpoint_locations ();
12246 else
12247 {
44702360
PA
12248 /* Even though the caller told us to not insert new
12249 locations, we may still need to update conditions on the
12250 target's side of breakpoints that were already inserted
12251 if the target is evaluating breakpoint conditions. We
b775012e
LM
12252 only update conditions for locations that are marked
12253 "needs_update". */
12254 update_inserted_breakpoint_locations ();
12255 }
12256 }
348d480f 12257
04086b45 12258 if (insert_mode != UGLL_DONT_INSERT)
1e4d1764 12259 download_tracepoint_locations ();
348d480f
PA
12260}
12261
12262void
12263breakpoint_retire_moribund (void)
12264{
1123588c
TT
12265 for (int ix = 0; ix < moribund_locations.size (); ++ix)
12266 {
12267 struct bp_location *loc = moribund_locations[ix];
12268 if (--(loc->events_till_retirement) == 0)
12269 {
12270 decref_bp_location (&loc);
12271 unordered_remove (moribund_locations, ix);
12272 --ix;
12273 }
12274 }
348d480f
PA
12275}
12276
12277static void
44702360 12278update_global_location_list_nothrow (enum ugll_insert_mode insert_mode)
348d480f 12279{
348d480f 12280
a70b8144 12281 try
492d29ea
PA
12282 {
12283 update_global_location_list (insert_mode);
12284 }
230d2906 12285 catch (const gdb_exception_error &e)
492d29ea
PA
12286 {
12287 }
348d480f
PA
12288}
12289
12290/* Clear BKP from a BPS. */
12291
12292static void
12293bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
12294{
12295 bpstat bs;
12296
12297 for (bs = bps; bs; bs = bs->next)
12298 if (bs->breakpoint_at == bpt)
12299 {
12300 bs->breakpoint_at = NULL;
12301 bs->old_val = NULL;
12302 /* bs->commands will be freed later. */
12303 }
12304}
12305
12306/* Callback for iterate_over_threads. */
12307static int
12308bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
12309{
9a3c8263 12310 struct breakpoint *bpt = (struct breakpoint *) data;
348d480f
PA
12311
12312 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
12313 return 0;
12314}
12315
12316/* Helper for breakpoint and tracepoint breakpoint_ops->mention
12317 callbacks. */
12318
12319static void
12320say_where (struct breakpoint *b)
12321{
12322 struct value_print_options opts;
12323
12324 get_user_print_options (&opts);
12325
12326 /* i18n: cagney/2005-02-11: Below needs to be merged into a
12327 single string. */
12328 if (b->loc == NULL)
12329 {
f00aae0f
KS
12330 /* For pending locations, the output differs slightly based
12331 on b->extra_string. If this is non-NULL, it contains either
12332 a condition or dprintf arguments. */
12333 if (b->extra_string == NULL)
12334 {
12335 printf_filtered (_(" (%s) pending."),
d28cd78a 12336 event_location_to_string (b->location.get ()));
f00aae0f
KS
12337 }
12338 else if (b->type == bp_dprintf)
12339 {
12340 printf_filtered (_(" (%s,%s) pending."),
d28cd78a 12341 event_location_to_string (b->location.get ()),
f00aae0f
KS
12342 b->extra_string);
12343 }
12344 else
12345 {
12346 printf_filtered (_(" (%s %s) pending."),
d28cd78a 12347 event_location_to_string (b->location.get ()),
f00aae0f
KS
12348 b->extra_string);
12349 }
348d480f
PA
12350 }
12351 else
12352 {
2f202fde 12353 if (opts.addressprint || b->loc->symtab == NULL)
6a831f06
PA
12354 printf_filtered (" at %ps",
12355 styled_string (address_style.style (),
12356 paddress (b->loc->gdbarch,
12357 b->loc->address)));
2f202fde 12358 if (b->loc->symtab != NULL)
f8eba3c6
TT
12359 {
12360 /* If there is a single location, we can print the location
12361 more nicely. */
12362 if (b->loc->next == NULL)
0bb296cb 12363 {
6a831f06
PA
12364 const char *filename
12365 = symtab_to_filename_for_display (b->loc->symtab);
12366 printf_filtered (": file %ps, line %d.",
12367 styled_string (file_name_style.style (),
12368 filename),
0bb296cb
TT
12369 b->loc->line_number);
12370 }
f8eba3c6
TT
12371 else
12372 /* This is not ideal, but each location may have a
12373 different file name, and this at least reflects the
12374 real situation somewhat. */
f00aae0f 12375 printf_filtered (": %s.",
d28cd78a 12376 event_location_to_string (b->location.get ()));
f8eba3c6 12377 }
348d480f
PA
12378
12379 if (b->loc->next)
12380 {
12381 struct bp_location *loc = b->loc;
12382 int n = 0;
12383 for (; loc; loc = loc->next)
12384 ++n;
12385 printf_filtered (" (%d locations)", n);
12386 }
12387 }
12388}
12389
5f486660 12390bp_location::~bp_location ()
348d480f 12391{
5f486660 12392 xfree (function_name);
348d480f
PA
12393}
12394
c1fc2657 12395/* Destructor for the breakpoint base class. */
348d480f 12396
c1fc2657 12397breakpoint::~breakpoint ()
348d480f 12398{
c1fc2657
SM
12399 xfree (this->cond_string);
12400 xfree (this->extra_string);
348d480f
PA
12401}
12402
2060206e
PA
12403static struct bp_location *
12404base_breakpoint_allocate_location (struct breakpoint *self)
348d480f 12405{
5f486660 12406 return new bp_location (self);
348d480f
PA
12407}
12408
2060206e
PA
12409static void
12410base_breakpoint_re_set (struct breakpoint *b)
12411{
12412 /* Nothing to re-set. */
12413}
12414
12415#define internal_error_pure_virtual_called() \
12416 gdb_assert_not_reached ("pure virtual function called")
12417
12418static int
12419base_breakpoint_insert_location (struct bp_location *bl)
12420{
12421 internal_error_pure_virtual_called ();
12422}
12423
12424static int
73971819
PA
12425base_breakpoint_remove_location (struct bp_location *bl,
12426 enum remove_bp_reason reason)
2060206e
PA
12427{
12428 internal_error_pure_virtual_called ();
12429}
12430
12431static int
12432base_breakpoint_breakpoint_hit (const struct bp_location *bl,
bd522513 12433 const address_space *aspace,
09ac7c10
TT
12434 CORE_ADDR bp_addr,
12435 const struct target_waitstatus *ws)
2060206e
PA
12436{
12437 internal_error_pure_virtual_called ();
12438}
12439
12440static void
12441base_breakpoint_check_status (bpstat bs)
12442{
12443 /* Always stop. */
12444}
12445
12446/* A "works_in_software_mode" breakpoint_ops method that just internal
12447 errors. */
12448
12449static int
12450base_breakpoint_works_in_software_mode (const struct breakpoint *b)
12451{
12452 internal_error_pure_virtual_called ();
12453}
12454
12455/* A "resources_needed" breakpoint_ops method that just internal
12456 errors. */
12457
12458static int
12459base_breakpoint_resources_needed (const struct bp_location *bl)
12460{
12461 internal_error_pure_virtual_called ();
12462}
12463
12464static enum print_stop_action
12465base_breakpoint_print_it (bpstat bs)
12466{
12467 internal_error_pure_virtual_called ();
12468}
12469
12470static void
12471base_breakpoint_print_one_detail (const struct breakpoint *self,
12472 struct ui_out *uiout)
12473{
12474 /* nothing */
12475}
12476
12477static void
12478base_breakpoint_print_mention (struct breakpoint *b)
12479{
12480 internal_error_pure_virtual_called ();
12481}
12482
12483static void
12484base_breakpoint_print_recreate (struct breakpoint *b, struct ui_file *fp)
12485{
12486 internal_error_pure_virtual_called ();
12487}
12488
983af33b 12489static void
f00aae0f 12490base_breakpoint_create_sals_from_location
626d2320 12491 (struct event_location *location,
f00aae0f
KS
12492 struct linespec_result *canonical,
12493 enum bptype type_wanted)
983af33b
SDJ
12494{
12495 internal_error_pure_virtual_called ();
12496}
12497
12498static void
12499base_breakpoint_create_breakpoints_sal (struct gdbarch *gdbarch,
12500 struct linespec_result *c,
e1e01040
PA
12501 gdb::unique_xmalloc_ptr<char> cond_string,
12502 gdb::unique_xmalloc_ptr<char> extra_string,
983af33b
SDJ
12503 enum bptype type_wanted,
12504 enum bpdisp disposition,
12505 int thread,
12506 int task, int ignore_count,
12507 const struct breakpoint_ops *o,
12508 int from_tty, int enabled,
44f238bb 12509 int internal, unsigned flags)
983af33b
SDJ
12510{
12511 internal_error_pure_virtual_called ();
12512}
12513
6c5b2ebe 12514static std::vector<symtab_and_line>
f00aae0f 12515base_breakpoint_decode_location (struct breakpoint *b,
626d2320 12516 struct event_location *location,
6c5b2ebe 12517 struct program_space *search_pspace)
983af33b
SDJ
12518{
12519 internal_error_pure_virtual_called ();
12520}
12521
ab04a2af
TT
12522/* The default 'explains_signal' method. */
12523
47591c29 12524static int
427cd150 12525base_breakpoint_explains_signal (struct breakpoint *b, enum gdb_signal sig)
ab04a2af 12526{
47591c29 12527 return 1;
ab04a2af
TT
12528}
12529
9d6e6e84
HZ
12530/* The default "after_condition_true" method. */
12531
12532static void
12533base_breakpoint_after_condition_true (struct bpstats *bs)
12534{
12535 /* Nothing to do. */
12536}
12537
ab04a2af 12538struct breakpoint_ops base_breakpoint_ops =
2060206e 12539{
2060206e
PA
12540 base_breakpoint_allocate_location,
12541 base_breakpoint_re_set,
12542 base_breakpoint_insert_location,
12543 base_breakpoint_remove_location,
12544 base_breakpoint_breakpoint_hit,
12545 base_breakpoint_check_status,
12546 base_breakpoint_resources_needed,
12547 base_breakpoint_works_in_software_mode,
12548 base_breakpoint_print_it,
12549 NULL,
12550 base_breakpoint_print_one_detail,
12551 base_breakpoint_print_mention,
983af33b 12552 base_breakpoint_print_recreate,
5f700d83 12553 base_breakpoint_create_sals_from_location,
983af33b 12554 base_breakpoint_create_breakpoints_sal,
5f700d83 12555 base_breakpoint_decode_location,
9d6e6e84
HZ
12556 base_breakpoint_explains_signal,
12557 base_breakpoint_after_condition_true,
2060206e
PA
12558};
12559
12560/* Default breakpoint_ops methods. */
12561
12562static void
348d480f
PA
12563bkpt_re_set (struct breakpoint *b)
12564{
06edf0c0 12565 /* FIXME: is this still reachable? */
9ef9e6a6 12566 if (breakpoint_event_location_empty_p (b))
06edf0c0 12567 {
f00aae0f 12568 /* Anything without a location can't be re-set. */
348d480f 12569 delete_breakpoint (b);
06edf0c0 12570 return;
348d480f 12571 }
06edf0c0
PA
12572
12573 breakpoint_re_set_default (b);
348d480f
PA
12574}
12575
2060206e 12576static int
348d480f
PA
12577bkpt_insert_location (struct bp_location *bl)
12578{
cd6c3b4f
YQ
12579 CORE_ADDR addr = bl->target_info.reqstd_address;
12580
579c6ad9 12581 bl->target_info.kind = breakpoint_kind (bl, &addr);
cd6c3b4f
YQ
12582 bl->target_info.placed_address = addr;
12583
348d480f 12584 if (bl->loc_type == bp_loc_hardware_breakpoint)
7c16b83e 12585 return target_insert_hw_breakpoint (bl->gdbarch, &bl->target_info);
348d480f 12586 else
7c16b83e 12587 return target_insert_breakpoint (bl->gdbarch, &bl->target_info);
348d480f
PA
12588}
12589
2060206e 12590static int
73971819 12591bkpt_remove_location (struct bp_location *bl, enum remove_bp_reason reason)
348d480f
PA
12592{
12593 if (bl->loc_type == bp_loc_hardware_breakpoint)
12594 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
12595 else
73971819 12596 return target_remove_breakpoint (bl->gdbarch, &bl->target_info, reason);
348d480f
PA
12597}
12598
2060206e 12599static int
348d480f 12600bkpt_breakpoint_hit (const struct bp_location *bl,
bd522513 12601 const address_space *aspace, CORE_ADDR bp_addr,
09ac7c10 12602 const struct target_waitstatus *ws)
348d480f 12603{
09ac7c10 12604 if (ws->kind != TARGET_WAITKIND_STOPPED
a493e3e2 12605 || ws->value.sig != GDB_SIGNAL_TRAP)
09ac7c10
TT
12606 return 0;
12607
348d480f
PA
12608 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
12609 aspace, bp_addr))
12610 return 0;
12611
12612 if (overlay_debugging /* unmapped overlay section */
12613 && section_is_overlay (bl->section)
12614 && !section_is_mapped (bl->section))
12615 return 0;
12616
12617 return 1;
12618}
12619
cd1608cc
PA
12620static int
12621dprintf_breakpoint_hit (const struct bp_location *bl,
bd522513 12622 const address_space *aspace, CORE_ADDR bp_addr,
cd1608cc
PA
12623 const struct target_waitstatus *ws)
12624{
12625 if (dprintf_style == dprintf_style_agent
12626 && target_can_run_breakpoint_commands ())
12627 {
12628 /* An agent-style dprintf never causes a stop. If we see a trap
12629 for this address it must be for a breakpoint that happens to
12630 be set at the same address. */
12631 return 0;
12632 }
12633
12634 return bkpt_breakpoint_hit (bl, aspace, bp_addr, ws);
12635}
12636
2060206e 12637static int
348d480f
PA
12638bkpt_resources_needed (const struct bp_location *bl)
12639{
12640 gdb_assert (bl->owner->type == bp_hardware_breakpoint);
12641
12642 return 1;
12643}
12644
2060206e 12645static enum print_stop_action
348d480f
PA
12646bkpt_print_it (bpstat bs)
12647{
348d480f
PA
12648 struct breakpoint *b;
12649 const struct bp_location *bl;
001c8c33 12650 int bp_temp;
79a45e25 12651 struct ui_out *uiout = current_uiout;
348d480f
PA
12652
12653 gdb_assert (bs->bp_location_at != NULL);
12654
b6433ede 12655 bl = bs->bp_location_at.get ();
348d480f
PA
12656 b = bs->breakpoint_at;
12657
001c8c33
PA
12658 bp_temp = b->disposition == disp_del;
12659 if (bl->address != bl->requested_address)
12660 breakpoint_adjustment_warning (bl->requested_address,
12661 bl->address,
12662 b->number, 1);
12663 annotate_breakpoint (b->number);
f303dbd6
PA
12664 maybe_print_thread_hit_breakpoint (uiout);
12665
112e8700 12666 if (uiout->is_mi_like_p ())
348d480f 12667 {
112e8700 12668 uiout->field_string ("reason",
001c8c33 12669 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
112e8700 12670 uiout->field_string ("disp", bpdisp_text (b->disposition));
06edf0c0 12671 }
6a831f06
PA
12672 if (bp_temp)
12673 uiout->message ("Temporary breakpoint %pF, ",
12674 signed_field ("bkptno", b->number));
12675 else
12676 uiout->message ("Breakpoint %pF, ",
12677 signed_field ("bkptno", b->number));
06edf0c0 12678
001c8c33 12679 return PRINT_SRC_AND_LOC;
06edf0c0
PA
12680}
12681
2060206e 12682static void
06edf0c0
PA
12683bkpt_print_mention (struct breakpoint *b)
12684{
112e8700 12685 if (current_uiout->is_mi_like_p ())
06edf0c0
PA
12686 return;
12687
12688 switch (b->type)
12689 {
12690 case bp_breakpoint:
12691 case bp_gnu_ifunc_resolver:
12692 if (b->disposition == disp_del)
12693 printf_filtered (_("Temporary breakpoint"));
12694 else
12695 printf_filtered (_("Breakpoint"));
12696 printf_filtered (_(" %d"), b->number);
12697 if (b->type == bp_gnu_ifunc_resolver)
12698 printf_filtered (_(" at gnu-indirect-function resolver"));
12699 break;
12700 case bp_hardware_breakpoint:
12701 printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
12702 break;
e7e0cddf
SS
12703 case bp_dprintf:
12704 printf_filtered (_("Dprintf %d"), b->number);
12705 break;
06edf0c0
PA
12706 }
12707
12708 say_where (b);
12709}
12710
2060206e 12711static void
06edf0c0
PA
12712bkpt_print_recreate (struct breakpoint *tp, struct ui_file *fp)
12713{
12714 if (tp->type == bp_breakpoint && tp->disposition == disp_del)
12715 fprintf_unfiltered (fp, "tbreak");
12716 else if (tp->type == bp_breakpoint)
12717 fprintf_unfiltered (fp, "break");
12718 else if (tp->type == bp_hardware_breakpoint
12719 && tp->disposition == disp_del)
12720 fprintf_unfiltered (fp, "thbreak");
12721 else if (tp->type == bp_hardware_breakpoint)
12722 fprintf_unfiltered (fp, "hbreak");
12723 else
12724 internal_error (__FILE__, __LINE__,
12725 _("unhandled breakpoint type %d"), (int) tp->type);
12726
f00aae0f 12727 fprintf_unfiltered (fp, " %s",
d28cd78a 12728 event_location_to_string (tp->location.get ()));
f00aae0f
KS
12729
12730 /* Print out extra_string if this breakpoint is pending. It might
12731 contain, for example, conditions that were set by the user. */
12732 if (tp->loc == NULL && tp->extra_string != NULL)
12733 fprintf_unfiltered (fp, " %s", tp->extra_string);
12734
dd11a36c 12735 print_recreate_thread (tp, fp);
06edf0c0
PA
12736}
12737
983af33b 12738static void
626d2320 12739bkpt_create_sals_from_location (struct event_location *location,
f00aae0f
KS
12740 struct linespec_result *canonical,
12741 enum bptype type_wanted)
983af33b 12742{
f00aae0f 12743 create_sals_from_location_default (location, canonical, type_wanted);
983af33b
SDJ
12744}
12745
12746static void
12747bkpt_create_breakpoints_sal (struct gdbarch *gdbarch,
12748 struct linespec_result *canonical,
e1e01040
PA
12749 gdb::unique_xmalloc_ptr<char> cond_string,
12750 gdb::unique_xmalloc_ptr<char> extra_string,
983af33b
SDJ
12751 enum bptype type_wanted,
12752 enum bpdisp disposition,
12753 int thread,
12754 int task, int ignore_count,
12755 const struct breakpoint_ops *ops,
12756 int from_tty, int enabled,
44f238bb 12757 int internal, unsigned flags)
983af33b 12758{
023fa29b 12759 create_breakpoints_sal_default (gdbarch, canonical,
e1e01040
PA
12760 std::move (cond_string),
12761 std::move (extra_string),
e7e0cddf 12762 type_wanted,
983af33b
SDJ
12763 disposition, thread, task,
12764 ignore_count, ops, from_tty,
44f238bb 12765 enabled, internal, flags);
983af33b
SDJ
12766}
12767
6c5b2ebe 12768static std::vector<symtab_and_line>
f00aae0f 12769bkpt_decode_location (struct breakpoint *b,
626d2320 12770 struct event_location *location,
6c5b2ebe 12771 struct program_space *search_pspace)
983af33b 12772{
6c5b2ebe 12773 return decode_location_default (b, location, search_pspace);
983af33b
SDJ
12774}
12775
06edf0c0
PA
12776/* Virtual table for internal breakpoints. */
12777
12778static void
12779internal_bkpt_re_set (struct breakpoint *b)
12780{
12781 switch (b->type)
12782 {
12783 /* Delete overlay event and longjmp master breakpoints; they
12784 will be reset later by breakpoint_re_set. */
12785 case bp_overlay_event:
12786 case bp_longjmp_master:
12787 case bp_std_terminate_master:
12788 case bp_exception_master:
12789 delete_breakpoint (b);
12790 break;
12791
12792 /* This breakpoint is special, it's set up when the inferior
dda83cd7 12793 starts and we really don't want to touch it. */
06edf0c0
PA
12794 case bp_shlib_event:
12795
12796 /* Like bp_shlib_event, this breakpoint type is special. Once
12797 it is set up, we do not want to touch it. */
12798 case bp_thread_event:
12799 break;
12800 }
12801}
12802
12803static void
12804internal_bkpt_check_status (bpstat bs)
12805{
a9b3a50f
PA
12806 if (bs->breakpoint_at->type == bp_shlib_event)
12807 {
12808 /* If requested, stop when the dynamic linker notifies GDB of
12809 events. This allows the user to get control and place
12810 breakpoints in initializer routines for dynamically loaded
12811 objects (among other things). */
12812 bs->stop = stop_on_solib_events;
12813 bs->print = stop_on_solib_events;
12814 }
12815 else
12816 bs->stop = 0;
06edf0c0
PA
12817}
12818
12819static enum print_stop_action
12820internal_bkpt_print_it (bpstat bs)
12821{
06edf0c0 12822 struct breakpoint *b;
06edf0c0 12823
06edf0c0
PA
12824 b = bs->breakpoint_at;
12825
06edf0c0
PA
12826 switch (b->type)
12827 {
348d480f
PA
12828 case bp_shlib_event:
12829 /* Did we stop because the user set the stop_on_solib_events
12830 variable? (If so, we report this as a generic, "Stopped due
12831 to shlib event" message.) */
edcc5120 12832 print_solib_event (0);
348d480f
PA
12833 break;
12834
12835 case bp_thread_event:
12836 /* Not sure how we will get here.
12837 GDB should not stop for these breakpoints. */
12838 printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
12839 break;
12840
12841 case bp_overlay_event:
12842 /* By analogy with the thread event, GDB should not stop for these. */
12843 printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
12844 break;
12845
12846 case bp_longjmp_master:
12847 /* These should never be enabled. */
12848 printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
348d480f
PA
12849 break;
12850
12851 case bp_std_terminate_master:
12852 /* These should never be enabled. */
12853 printf_filtered (_("std::terminate Master Breakpoint: "
12854 "gdb should not stop!\n"));
348d480f
PA
12855 break;
12856
12857 case bp_exception_master:
12858 /* These should never be enabled. */
12859 printf_filtered (_("Exception Master Breakpoint: "
12860 "gdb should not stop!\n"));
06edf0c0
PA
12861 break;
12862 }
12863
001c8c33 12864 return PRINT_NOTHING;
06edf0c0
PA
12865}
12866
12867static void
12868internal_bkpt_print_mention (struct breakpoint *b)
12869{
12870 /* Nothing to mention. These breakpoints are internal. */
12871}
12872
06edf0c0
PA
12873/* Virtual table for momentary breakpoints */
12874
12875static void
12876momentary_bkpt_re_set (struct breakpoint *b)
12877{
12878 /* Keep temporary breakpoints, which can be encountered when we step
4d1eb6b4 12879 over a dlopen call and solib_add is resetting the breakpoints.
06edf0c0
PA
12880 Otherwise these should have been blown away via the cleanup chain
12881 or by breakpoint_init_inferior when we rerun the executable. */
12882}
12883
12884static void
12885momentary_bkpt_check_status (bpstat bs)
12886{
12887 /* Nothing. The point of these breakpoints is causing a stop. */
12888}
12889
12890static enum print_stop_action
12891momentary_bkpt_print_it (bpstat bs)
12892{
001c8c33 12893 return PRINT_UNKNOWN;
348d480f
PA
12894}
12895
06edf0c0
PA
12896static void
12897momentary_bkpt_print_mention (struct breakpoint *b)
348d480f 12898{
06edf0c0 12899 /* Nothing to mention. These breakpoints are internal. */
348d480f
PA
12900}
12901
e2e4d78b
JK
12902/* Ensure INITIATING_FRAME is cleared when no such breakpoint exists.
12903
12904 It gets cleared already on the removal of the first one of such placed
12905 breakpoints. This is OK as they get all removed altogether. */
12906
c1fc2657 12907longjmp_breakpoint::~longjmp_breakpoint ()
e2e4d78b 12908{
c1fc2657 12909 thread_info *tp = find_thread_global_id (this->thread);
e2e4d78b 12910
c1fc2657 12911 if (tp != NULL)
e2e4d78b 12912 tp->initiating_frame = null_frame_id;
e2e4d78b
JK
12913}
12914
55aa24fb
SDJ
12915/* Specific methods for probe breakpoints. */
12916
12917static int
12918bkpt_probe_insert_location (struct bp_location *bl)
12919{
12920 int v = bkpt_insert_location (bl);
12921
12922 if (v == 0)
12923 {
12924 /* The insertion was successful, now let's set the probe's semaphore
12925 if needed. */
935676c9 12926 bl->probe.prob->set_semaphore (bl->probe.objfile, bl->gdbarch);
55aa24fb
SDJ
12927 }
12928
12929 return v;
12930}
12931
12932static int
73971819
PA
12933bkpt_probe_remove_location (struct bp_location *bl,
12934 enum remove_bp_reason reason)
55aa24fb
SDJ
12935{
12936 /* Let's clear the semaphore before removing the location. */
935676c9 12937 bl->probe.prob->clear_semaphore (bl->probe.objfile, bl->gdbarch);
55aa24fb 12938
73971819 12939 return bkpt_remove_location (bl, reason);
55aa24fb
SDJ
12940}
12941
12942static void
626d2320 12943bkpt_probe_create_sals_from_location (struct event_location *location,
5f700d83 12944 struct linespec_result *canonical,
f00aae0f 12945 enum bptype type_wanted)
55aa24fb
SDJ
12946{
12947 struct linespec_sals lsal;
12948
c2f4122d 12949 lsal.sals = parse_probes (location, NULL, canonical);
8e9e35b1
TT
12950 lsal.canonical
12951 = xstrdup (event_location_to_string (canonical->location.get ()));
6c5b2ebe 12952 canonical->lsals.push_back (std::move (lsal));
55aa24fb
SDJ
12953}
12954
6c5b2ebe 12955static std::vector<symtab_and_line>
f00aae0f 12956bkpt_probe_decode_location (struct breakpoint *b,
626d2320 12957 struct event_location *location,
6c5b2ebe 12958 struct program_space *search_pspace)
55aa24fb 12959{
6c5b2ebe
PA
12960 std::vector<symtab_and_line> sals = parse_probes (location, search_pspace, NULL);
12961 if (sals.empty ())
55aa24fb 12962 error (_("probe not found"));
6c5b2ebe 12963 return sals;
55aa24fb
SDJ
12964}
12965
348d480f 12966/* The breakpoint_ops structure to be used in tracepoints. */
876fa593 12967
348d480f
PA
12968static void
12969tracepoint_re_set (struct breakpoint *b)
12970{
12971 breakpoint_re_set_default (b);
12972}
876fa593 12973
348d480f
PA
12974static int
12975tracepoint_breakpoint_hit (const struct bp_location *bl,
bd522513 12976 const address_space *aspace, CORE_ADDR bp_addr,
09ac7c10 12977 const struct target_waitstatus *ws)
348d480f
PA
12978{
12979 /* By definition, the inferior does not report stops at
12980 tracepoints. */
12981 return 0;
74960c60
VP
12982}
12983
12984static void
348d480f
PA
12985tracepoint_print_one_detail (const struct breakpoint *self,
12986 struct ui_out *uiout)
74960c60 12987{
d9b3f62e 12988 struct tracepoint *tp = (struct tracepoint *) self;
5d9310c4 12989 if (!tp->static_trace_marker_id.empty ())
348d480f
PA
12990 {
12991 gdb_assert (self->type == bp_static_tracepoint);
cc59ec59 12992
6a831f06
PA
12993 uiout->message ("\tmarker id is %pF\n",
12994 string_field ("static-tracepoint-marker-string-id",
12995 tp->static_trace_marker_id.c_str ()));
348d480f 12996 }
0d381245
VP
12997}
12998
a474d7c2 12999static void
348d480f 13000tracepoint_print_mention (struct breakpoint *b)
a474d7c2 13001{
112e8700 13002 if (current_uiout->is_mi_like_p ())
348d480f 13003 return;
cc59ec59 13004
348d480f
PA
13005 switch (b->type)
13006 {
13007 case bp_tracepoint:
13008 printf_filtered (_("Tracepoint"));
13009 printf_filtered (_(" %d"), b->number);
13010 break;
13011 case bp_fast_tracepoint:
13012 printf_filtered (_("Fast tracepoint"));
13013 printf_filtered (_(" %d"), b->number);
13014 break;
13015 case bp_static_tracepoint:
13016 printf_filtered (_("Static tracepoint"));
13017 printf_filtered (_(" %d"), b->number);
13018 break;
13019 default:
13020 internal_error (__FILE__, __LINE__,
13021 _("unhandled tracepoint type %d"), (int) b->type);
13022 }
13023
13024 say_where (b);
a474d7c2
PA
13025}
13026
348d480f 13027static void
d9b3f62e 13028tracepoint_print_recreate (struct breakpoint *self, struct ui_file *fp)
a474d7c2 13029{
d9b3f62e
PA
13030 struct tracepoint *tp = (struct tracepoint *) self;
13031
13032 if (self->type == bp_fast_tracepoint)
348d480f 13033 fprintf_unfiltered (fp, "ftrace");
c93e8391 13034 else if (self->type == bp_static_tracepoint)
348d480f 13035 fprintf_unfiltered (fp, "strace");
d9b3f62e 13036 else if (self->type == bp_tracepoint)
348d480f
PA
13037 fprintf_unfiltered (fp, "trace");
13038 else
13039 internal_error (__FILE__, __LINE__,
d9b3f62e 13040 _("unhandled tracepoint type %d"), (int) self->type);
cc59ec59 13041
f00aae0f 13042 fprintf_unfiltered (fp, " %s",
d28cd78a 13043 event_location_to_string (self->location.get ()));
d9b3f62e
PA
13044 print_recreate_thread (self, fp);
13045
13046 if (tp->pass_count)
13047 fprintf_unfiltered (fp, " passcount %d\n", tp->pass_count);
a474d7c2
PA
13048}
13049
983af33b 13050static void
626d2320 13051tracepoint_create_sals_from_location (struct event_location *location,
f00aae0f
KS
13052 struct linespec_result *canonical,
13053 enum bptype type_wanted)
983af33b 13054{
f00aae0f 13055 create_sals_from_location_default (location, canonical, type_wanted);
983af33b
SDJ
13056}
13057
13058static void
13059tracepoint_create_breakpoints_sal (struct gdbarch *gdbarch,
13060 struct linespec_result *canonical,
e1e01040
PA
13061 gdb::unique_xmalloc_ptr<char> cond_string,
13062 gdb::unique_xmalloc_ptr<char> extra_string,
983af33b
SDJ
13063 enum bptype type_wanted,
13064 enum bpdisp disposition,
13065 int thread,
13066 int task, int ignore_count,
13067 const struct breakpoint_ops *ops,
13068 int from_tty, int enabled,
44f238bb 13069 int internal, unsigned flags)
983af33b 13070{
023fa29b 13071 create_breakpoints_sal_default (gdbarch, canonical,
e1e01040
PA
13072 std::move (cond_string),
13073 std::move (extra_string),
e7e0cddf 13074 type_wanted,
983af33b
SDJ
13075 disposition, thread, task,
13076 ignore_count, ops, from_tty,
44f238bb 13077 enabled, internal, flags);
983af33b
SDJ
13078}
13079
6c5b2ebe 13080static std::vector<symtab_and_line>
f00aae0f 13081tracepoint_decode_location (struct breakpoint *b,
626d2320 13082 struct event_location *location,
6c5b2ebe 13083 struct program_space *search_pspace)
983af33b 13084{
6c5b2ebe 13085 return decode_location_default (b, location, search_pspace);
983af33b
SDJ
13086}
13087
2060206e 13088struct breakpoint_ops tracepoint_breakpoint_ops;
348d480f 13089
bac7c5cf 13090/* Virtual table for tracepoints on static probes. */
55aa24fb
SDJ
13091
13092static void
f00aae0f 13093tracepoint_probe_create_sals_from_location
626d2320 13094 (struct event_location *location,
f00aae0f
KS
13095 struct linespec_result *canonical,
13096 enum bptype type_wanted)
55aa24fb
SDJ
13097{
13098 /* We use the same method for breakpoint on probes. */
f00aae0f 13099 bkpt_probe_create_sals_from_location (location, canonical, type_wanted);
55aa24fb
SDJ
13100}
13101
6c5b2ebe 13102static std::vector<symtab_and_line>
f00aae0f 13103tracepoint_probe_decode_location (struct breakpoint *b,
626d2320 13104 struct event_location *location,
6c5b2ebe 13105 struct program_space *search_pspace)
55aa24fb
SDJ
13106{
13107 /* We use the same method for breakpoint on probes. */
6c5b2ebe 13108 return bkpt_probe_decode_location (b, location, search_pspace);
55aa24fb
SDJ
13109}
13110
5c2b4418
HZ
13111/* Dprintf breakpoint_ops methods. */
13112
13113static void
13114dprintf_re_set (struct breakpoint *b)
13115{
13116 breakpoint_re_set_default (b);
13117
f00aae0f
KS
13118 /* extra_string should never be non-NULL for dprintf. */
13119 gdb_assert (b->extra_string != NULL);
5c2b4418
HZ
13120
13121 /* 1 - connect to target 1, that can run breakpoint commands.
13122 2 - create a dprintf, which resolves fine.
13123 3 - disconnect from target 1
13124 4 - connect to target 2, that can NOT run breakpoint commands.
13125
13126 After steps #3/#4, you'll want the dprintf command list to
13127 be updated, because target 1 and 2 may well return different
13128 answers for target_can_run_breakpoint_commands().
13129 Given absence of finer grained resetting, we get to do
13130 it all the time. */
13131 if (b->extra_string != NULL)
13132 update_dprintf_command_list (b);
13133}
13134
2d9442cc
HZ
13135/* Implement the "print_recreate" breakpoint_ops method for dprintf. */
13136
13137static void
13138dprintf_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13139{
f00aae0f 13140 fprintf_unfiltered (fp, "dprintf %s,%s",
d28cd78a 13141 event_location_to_string (tp->location.get ()),
2d9442cc
HZ
13142 tp->extra_string);
13143 print_recreate_thread (tp, fp);
13144}
13145
9d6e6e84
HZ
13146/* Implement the "after_condition_true" breakpoint_ops method for
13147 dprintf.
13148
13149 dprintf's are implemented with regular commands in their command
13150 list, but we run the commands here instead of before presenting the
13151 stop to the user, as dprintf's don't actually cause a stop. This
13152 also makes it so that the commands of multiple dprintfs at the same
13153 address are all handled. */
13154
13155static void
13156dprintf_after_condition_true (struct bpstats *bs)
13157{
04afa70c 13158 struct bpstats tmp_bs;
9d6e6e84
HZ
13159 struct bpstats *tmp_bs_p = &tmp_bs;
13160
13161 /* dprintf's never cause a stop. This wasn't set in the
13162 check_status hook instead because that would make the dprintf's
13163 condition not be evaluated. */
13164 bs->stop = 0;
13165
13166 /* Run the command list here. Take ownership of it instead of
13167 copying. We never want these commands to run later in
13168 bpstat_do_actions, if a breakpoint that causes a stop happens to
13169 be set at same address as this dprintf, or even if running the
13170 commands here throws. */
13171 tmp_bs.commands = bs->commands;
13172 bs->commands = NULL;
9d6e6e84
HZ
13173
13174 bpstat_do_actions_1 (&tmp_bs_p);
13175
13176 /* 'tmp_bs.commands' will usually be NULL by now, but
13177 bpstat_do_actions_1 may return early without processing the whole
13178 list. */
9d6e6e84
HZ
13179}
13180
983af33b
SDJ
13181/* The breakpoint_ops structure to be used on static tracepoints with
13182 markers (`-m'). */
13183
13184static void
626d2320 13185strace_marker_create_sals_from_location (struct event_location *location,
5f700d83 13186 struct linespec_result *canonical,
f00aae0f 13187 enum bptype type_wanted)
983af33b
SDJ
13188{
13189 struct linespec_sals lsal;
f00aae0f 13190 const char *arg_start, *arg;
983af33b 13191
a20714ff 13192 arg = arg_start = get_linespec_location (location)->spec_string;
f00aae0f 13193 lsal.sals = decode_static_tracepoint_spec (&arg);
983af33b 13194
f2fc3015
TT
13195 std::string str (arg_start, arg - arg_start);
13196 const char *ptr = str.c_str ();
a20714ff
PA
13197 canonical->location
13198 = new_linespec_location (&ptr, symbol_name_match_type::FULL);
983af33b 13199
8e9e35b1
TT
13200 lsal.canonical
13201 = xstrdup (event_location_to_string (canonical->location.get ()));
6c5b2ebe 13202 canonical->lsals.push_back (std::move (lsal));
983af33b
SDJ
13203}
13204
13205static void
13206strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
13207 struct linespec_result *canonical,
e1e01040
PA
13208 gdb::unique_xmalloc_ptr<char> cond_string,
13209 gdb::unique_xmalloc_ptr<char> extra_string,
983af33b
SDJ
13210 enum bptype type_wanted,
13211 enum bpdisp disposition,
13212 int thread,
13213 int task, int ignore_count,
13214 const struct breakpoint_ops *ops,
13215 int from_tty, int enabled,
44f238bb 13216 int internal, unsigned flags)
983af33b 13217{
6c5b2ebe 13218 const linespec_sals &lsal = canonical->lsals[0];
983af33b
SDJ
13219
13220 /* If the user is creating a static tracepoint by marker id
13221 (strace -m MARKER_ID), then store the sals index, so that
13222 breakpoint_re_set can try to match up which of the newly
13223 found markers corresponds to this one, and, don't try to
13224 expand multiple locations for each sal, given than SALS
13225 already should contain all sals for MARKER_ID. */
13226
6c5b2ebe 13227 for (size_t i = 0; i < lsal.sals.size (); i++)
983af33b 13228 {
6c5b2ebe
PA
13229 event_location_up location
13230 = copy_event_location (canonical->location.get ());
983af33b 13231
b270e6f9 13232 std::unique_ptr<tracepoint> tp (new tracepoint ());
6c5b2ebe 13233 init_breakpoint_sal (tp.get (), gdbarch, lsal.sals[i],
ffc2605c 13234 std::move (location), NULL,
e1e01040
PA
13235 std::move (cond_string),
13236 std::move (extra_string),
e7e0cddf 13237 type_wanted, disposition,
983af33b 13238 thread, task, ignore_count, ops,
44f238bb 13239 from_tty, enabled, internal, flags,
983af33b
SDJ
13240 canonical->special_display);
13241 /* Given that its possible to have multiple markers with
13242 the same string id, if the user is creating a static
13243 tracepoint by marker id ("strace -m MARKER_ID"), then
13244 store the sals index, so that breakpoint_re_set can
13245 try to match up which of the newly found markers
13246 corresponds to this one */
13247 tp->static_trace_marker_id_idx = i;
13248
b270e6f9 13249 install_breakpoint (internal, std::move (tp), 0);
983af33b
SDJ
13250 }
13251}
13252
6c5b2ebe 13253static std::vector<symtab_and_line>
f00aae0f 13254strace_marker_decode_location (struct breakpoint *b,
626d2320 13255 struct event_location *location,
6c5b2ebe 13256 struct program_space *search_pspace)
983af33b
SDJ
13257{
13258 struct tracepoint *tp = (struct tracepoint *) b;
a20714ff 13259 const char *s = get_linespec_location (location)->spec_string;
983af33b 13260
6c5b2ebe
PA
13261 std::vector<symtab_and_line> sals = decode_static_tracepoint_spec (&s);
13262 if (sals.size () > tp->static_trace_marker_id_idx)
983af33b 13263 {
6c5b2ebe
PA
13264 sals[0] = sals[tp->static_trace_marker_id_idx];
13265 sals.resize (1);
13266 return sals;
983af33b
SDJ
13267 }
13268 else
5d9310c4 13269 error (_("marker %s not found"), tp->static_trace_marker_id.c_str ());
983af33b
SDJ
13270}
13271
13272static struct breakpoint_ops strace_marker_breakpoint_ops;
13273
13274static int
13275strace_marker_p (struct breakpoint *b)
13276{
13277 return b->ops == &strace_marker_breakpoint_ops;
13278}
13279
53a5351d 13280/* Delete a breakpoint and clean up all traces of it in the data
f431efe5 13281 structures. */
c906108c
SS
13282
13283void
fba45db2 13284delete_breakpoint (struct breakpoint *bpt)
c906108c 13285{
52f0bd74 13286 struct breakpoint *b;
c906108c 13287
8a3fe4f8 13288 gdb_assert (bpt != NULL);
c906108c 13289
4a64f543
MS
13290 /* Has this bp already been deleted? This can happen because
13291 multiple lists can hold pointers to bp's. bpstat lists are
13292 especial culprits.
13293
13294 One example of this happening is a watchpoint's scope bp. When
13295 the scope bp triggers, we notice that the watchpoint is out of
13296 scope, and delete it. We also delete its scope bp. But the
13297 scope bp is marked "auto-deleting", and is already on a bpstat.
13298 That bpstat is then checked for auto-deleting bp's, which are
13299 deleted.
13300
13301 A real solution to this problem might involve reference counts in
13302 bp's, and/or giving them pointers back to their referencing
13303 bpstat's, and teaching delete_breakpoint to only free a bp's
13304 storage when no more references were extent. A cheaper bandaid
13305 was chosen. */
c906108c
SS
13306 if (bpt->type == bp_none)
13307 return;
13308
4a64f543
MS
13309 /* At least avoid this stale reference until the reference counting
13310 of breakpoints gets resolved. */
d0fb5eae 13311 if (bpt->related_breakpoint != bpt)
e5a0a904 13312 {
d0fb5eae 13313 struct breakpoint *related;
3a5c3e22 13314 struct watchpoint *w;
d0fb5eae
JK
13315
13316 if (bpt->type == bp_watchpoint_scope)
3a5c3e22 13317 w = (struct watchpoint *) bpt->related_breakpoint;
d0fb5eae 13318 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
3a5c3e22
PA
13319 w = (struct watchpoint *) bpt;
13320 else
13321 w = NULL;
13322 if (w != NULL)
13323 watchpoint_del_at_next_stop (w);
d0fb5eae
JK
13324
13325 /* Unlink bpt from the bpt->related_breakpoint ring. */
13326 for (related = bpt; related->related_breakpoint != bpt;
13327 related = related->related_breakpoint);
13328 related->related_breakpoint = bpt->related_breakpoint;
13329 bpt->related_breakpoint = bpt;
e5a0a904
JK
13330 }
13331
a9634178
TJB
13332 /* watch_command_1 creates a watchpoint but only sets its number if
13333 update_watchpoint succeeds in creating its bp_locations. If there's
13334 a problem in that process, we'll be asked to delete the half-created
13335 watchpoint. In that case, don't announce the deletion. */
13336 if (bpt->number)
76727919 13337 gdb::observers::breakpoint_deleted.notify (bpt);
c906108c 13338
c906108c
SS
13339 if (breakpoint_chain == bpt)
13340 breakpoint_chain = bpt->next;
13341
c906108c
SS
13342 ALL_BREAKPOINTS (b)
13343 if (b->next == bpt)
c5aa993b
JM
13344 {
13345 b->next = bpt->next;
13346 break;
13347 }
c906108c 13348
f431efe5
PA
13349 /* Be sure no bpstat's are pointing at the breakpoint after it's
13350 been freed. */
13351 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
e5dd4106 13352 in all threads for now. Note that we cannot just remove bpstats
f431efe5
PA
13353 pointing at bpt from the stop_bpstat list entirely, as breakpoint
13354 commands are associated with the bpstat; if we remove it here,
13355 then the later call to bpstat_do_actions (&stop_bpstat); in
13356 event-top.c won't do anything, and temporary breakpoints with
13357 commands won't work. */
13358
13359 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
13360
4a64f543
MS
13361 /* Now that breakpoint is removed from breakpoint list, update the
13362 global location list. This will remove locations that used to
13363 belong to this breakpoint. Do this before freeing the breakpoint
13364 itself, since remove_breakpoint looks at location's owner. It
13365 might be better design to have location completely
13366 self-contained, but it's not the case now. */
44702360 13367 update_global_location_list (UGLL_DONT_INSERT);
74960c60 13368
4a64f543
MS
13369 /* On the chance that someone will soon try again to delete this
13370 same bp, we mark it as deleted before freeing its storage. */
c906108c 13371 bpt->type = bp_none;
4d01a485 13372 delete bpt;
c906108c
SS
13373}
13374
51be5b68
PA
13375/* Iterator function to call a user-provided callback function once
13376 for each of B and its related breakpoints. */
13377
13378static void
13379iterate_over_related_breakpoints (struct breakpoint *b,
48649e1b 13380 gdb::function_view<void (breakpoint *)> function)
51be5b68
PA
13381{
13382 struct breakpoint *related;
13383
13384 related = b;
13385 do
13386 {
13387 struct breakpoint *next;
13388
13389 /* FUNCTION may delete RELATED. */
13390 next = related->related_breakpoint;
13391
13392 if (next == related)
13393 {
13394 /* RELATED is the last ring entry. */
48649e1b 13395 function (related);
51be5b68
PA
13396
13397 /* FUNCTION may have deleted it, so we'd never reach back to
13398 B. There's nothing left to do anyway, so just break
13399 out. */
13400 break;
13401 }
13402 else
48649e1b 13403 function (related);
51be5b68
PA
13404
13405 related = next;
13406 }
13407 while (related != b);
13408}
95a42b64 13409
4495129a 13410static void
981a3fb3 13411delete_command (const char *arg, int from_tty)
c906108c 13412{
35df4500 13413 struct breakpoint *b, *b_tmp;
c906108c 13414
ea9365bb
TT
13415 dont_repeat ();
13416
c906108c
SS
13417 if (arg == 0)
13418 {
13419 int breaks_to_delete = 0;
13420
46c6471b 13421 /* Delete all breakpoints if no argument. Do not delete
dda83cd7
SM
13422 internal breakpoints, these have to be deleted with an
13423 explicit breakpoint number argument. */
c5aa993b 13424 ALL_BREAKPOINTS (b)
46c6471b 13425 if (user_breakpoint_p (b))
973d738b
DJ
13426 {
13427 breaks_to_delete = 1;
13428 break;
13429 }
c906108c
SS
13430
13431 /* Ask user only if there are some breakpoints to delete. */
13432 if (!from_tty
e2e0b3e5 13433 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
c906108c 13434 {
35df4500 13435 ALL_BREAKPOINTS_SAFE (b, b_tmp)
46c6471b 13436 if (user_breakpoint_p (b))
c5aa993b 13437 delete_breakpoint (b);
c906108c
SS
13438 }
13439 }
13440 else
48649e1b 13441 map_breakpoint_numbers
b926417a 13442 (arg, [&] (breakpoint *br)
48649e1b 13443 {
b926417a 13444 iterate_over_related_breakpoints (br, delete_breakpoint);
48649e1b 13445 });
c906108c
SS
13446}
13447
c2f4122d
PA
13448/* Return true if all locations of B bound to PSPACE are pending. If
13449 PSPACE is NULL, all locations of all program spaces are
13450 considered. */
13451
0d381245 13452static int
c2f4122d 13453all_locations_are_pending (struct breakpoint *b, struct program_space *pspace)
fe3f5fa8 13454{
c2f4122d
PA
13455 struct bp_location *loc;
13456
13457 for (loc = b->loc; loc != NULL; loc = loc->next)
13458 if ((pspace == NULL
13459 || loc->pspace == pspace)
13460 && !loc->shlib_disabled
8645ff69 13461 && !loc->pspace->executing_startup)
0d381245
VP
13462 return 0;
13463 return 1;
fe3f5fa8
VP
13464}
13465
776592bf
DE
13466/* Subroutine of update_breakpoint_locations to simplify it.
13467 Return non-zero if multiple fns in list LOC have the same name.
13468 Null names are ignored. */
13469
13470static int
13471ambiguous_names_p (struct bp_location *loc)
13472{
13473 struct bp_location *l;
c1fb9836
TT
13474 htab_up htab (htab_create_alloc (13, htab_hash_string, streq_hash, NULL,
13475 xcalloc, xfree));
776592bf
DE
13476
13477 for (l = loc; l != NULL; l = l->next)
13478 {
13479 const char **slot;
13480 const char *name = l->function_name;
13481
13482 /* Allow for some names to be NULL, ignore them. */
13483 if (name == NULL)
13484 continue;
13485
c1fb9836 13486 slot = (const char **) htab_find_slot (htab.get (), (const void *) name,
776592bf 13487 INSERT);
4a64f543
MS
13488 /* NOTE: We can assume slot != NULL here because xcalloc never
13489 returns NULL. */
776592bf 13490 if (*slot != NULL)
c1fb9836 13491 return 1;
776592bf
DE
13492 *slot = name;
13493 }
13494
776592bf
DE
13495 return 0;
13496}
13497
0fb4aa4b
PA
13498/* When symbols change, it probably means the sources changed as well,
13499 and it might mean the static tracepoint markers are no longer at
13500 the same address or line numbers they used to be at last we
13501 checked. Losing your static tracepoints whenever you rebuild is
13502 undesirable. This function tries to resync/rematch gdb static
13503 tracepoints with the markers on the target, for static tracepoints
13504 that have not been set by marker id. Static tracepoint that have
13505 been set by marker id are reset by marker id in breakpoint_re_set.
13506 The heuristic is:
13507
13508 1) For a tracepoint set at a specific address, look for a marker at
13509 the old PC. If one is found there, assume to be the same marker.
13510 If the name / string id of the marker found is different from the
13511 previous known name, assume that means the user renamed the marker
13512 in the sources, and output a warning.
13513
13514 2) For a tracepoint set at a given line number, look for a marker
13515 at the new address of the old line number. If one is found there,
13516 assume to be the same marker. If the name / string id of the
13517 marker found is different from the previous known name, assume that
13518 means the user renamed the marker in the sources, and output a
13519 warning.
13520
13521 3) If a marker is no longer found at the same address or line, it
13522 may mean the marker no longer exists. But it may also just mean
13523 the code changed a bit. Maybe the user added a few lines of code
13524 that made the marker move up or down (in line number terms). Ask
13525 the target for info about the marker with the string id as we knew
13526 it. If found, update line number and address in the matching
13527 static tracepoint. This will get confused if there's more than one
13528 marker with the same ID (possible in UST, although unadvised
13529 precisely because it confuses tools). */
13530
13531static struct symtab_and_line
13532update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
13533{
d9b3f62e 13534 struct tracepoint *tp = (struct tracepoint *) b;
0fb4aa4b
PA
13535 struct static_tracepoint_marker marker;
13536 CORE_ADDR pc;
0fb4aa4b
PA
13537
13538 pc = sal.pc;
13539 if (sal.line)
13540 find_line_pc (sal.symtab, sal.line, &pc);
13541
13542 if (target_static_tracepoint_marker_at (pc, &marker))
13543 {
5d9310c4 13544 if (tp->static_trace_marker_id != marker.str_id)
0fb4aa4b 13545 warning (_("static tracepoint %d changed probed marker from %s to %s"),
5d9310c4
SM
13546 b->number, tp->static_trace_marker_id.c_str (),
13547 marker.str_id.c_str ());
0fb4aa4b 13548
5d9310c4 13549 tp->static_trace_marker_id = std::move (marker.str_id);
0fb4aa4b
PA
13550
13551 return sal;
13552 }
13553
13554 /* Old marker wasn't found on target at lineno. Try looking it up
13555 by string ID. */
13556 if (!sal.explicit_pc
13557 && sal.line != 0
13558 && sal.symtab != NULL
5d9310c4 13559 && !tp->static_trace_marker_id.empty ())
0fb4aa4b 13560 {
5d9310c4
SM
13561 std::vector<static_tracepoint_marker> markers
13562 = target_static_tracepoint_markers_by_strid
13563 (tp->static_trace_marker_id.c_str ());
0fb4aa4b 13564
5d9310c4 13565 if (!markers.empty ())
0fb4aa4b 13566 {
0fb4aa4b 13567 struct symbol *sym;
80e1d417 13568 struct static_tracepoint_marker *tpmarker;
79a45e25 13569 struct ui_out *uiout = current_uiout;
67994074 13570 struct explicit_location explicit_loc;
0fb4aa4b 13571
5d9310c4 13572 tpmarker = &markers[0];
0fb4aa4b 13573
5d9310c4 13574 tp->static_trace_marker_id = std::move (tpmarker->str_id);
0fb4aa4b
PA
13575
13576 warning (_("marker for static tracepoint %d (%s) not "
13577 "found at previous line number"),
5d9310c4 13578 b->number, tp->static_trace_marker_id.c_str ());
0fb4aa4b 13579
51abb421 13580 symtab_and_line sal2 = find_pc_line (tpmarker->address, 0);
80e1d417 13581 sym = find_pc_sect_function (tpmarker->address, NULL);
112e8700 13582 uiout->text ("Now in ");
0fb4aa4b
PA
13583 if (sym)
13584 {
987012b8 13585 uiout->field_string ("func", sym->print_name (),
e43b10e1 13586 function_name_style.style ());
112e8700 13587 uiout->text (" at ");
0fb4aa4b 13588 }
112e8700 13589 uiout->field_string ("file",
cbe56571 13590 symtab_to_filename_for_display (sal2.symtab),
e43b10e1 13591 file_name_style.style ());
112e8700 13592 uiout->text (":");
0fb4aa4b 13593
112e8700 13594 if (uiout->is_mi_like_p ())
0fb4aa4b 13595 {
0b0865da 13596 const char *fullname = symtab_to_fullname (sal2.symtab);
0fb4aa4b 13597
112e8700 13598 uiout->field_string ("fullname", fullname);
0fb4aa4b
PA
13599 }
13600
381befee 13601 uiout->field_signed ("line", sal2.line);
112e8700 13602 uiout->text ("\n");
0fb4aa4b 13603
80e1d417 13604 b->loc->line_number = sal2.line;
2f202fde 13605 b->loc->symtab = sym != NULL ? sal2.symtab : NULL;
0fb4aa4b 13606
d28cd78a 13607 b->location.reset (NULL);
67994074
KS
13608 initialize_explicit_location (&explicit_loc);
13609 explicit_loc.source_filename
00e52e53 13610 = ASTRDUP (symtab_to_filename_for_display (sal2.symtab));
67994074
KS
13611 explicit_loc.line_offset.offset = b->loc->line_number;
13612 explicit_loc.line_offset.sign = LINE_OFFSET_NONE;
d28cd78a 13613 b->location = new_explicit_location (&explicit_loc);
0fb4aa4b
PA
13614
13615 /* Might be nice to check if function changed, and warn if
13616 so. */
0fb4aa4b
PA
13617 }
13618 }
13619 return sal;
13620}
13621
8d3788bd
VP
13622/* Returns 1 iff locations A and B are sufficiently same that
13623 we don't need to report breakpoint as changed. */
13624
13625static int
13626locations_are_equal (struct bp_location *a, struct bp_location *b)
13627{
13628 while (a && b)
13629 {
13630 if (a->address != b->address)
13631 return 0;
13632
13633 if (a->shlib_disabled != b->shlib_disabled)
13634 return 0;
13635
13636 if (a->enabled != b->enabled)
13637 return 0;
13638
b5fa468f
TBA
13639 if (a->disabled_by_cond != b->disabled_by_cond)
13640 return 0;
13641
8d3788bd
VP
13642 a = a->next;
13643 b = b->next;
13644 }
13645
13646 if ((a == NULL) != (b == NULL))
13647 return 0;
13648
13649 return 1;
13650}
13651
c2f4122d
PA
13652/* Split all locations of B that are bound to PSPACE out of B's
13653 location list to a separate list and return that list's head. If
13654 PSPACE is NULL, hoist out all locations of B. */
13655
13656static struct bp_location *
13657hoist_existing_locations (struct breakpoint *b, struct program_space *pspace)
13658{
13659 struct bp_location head;
13660 struct bp_location *i = b->loc;
13661 struct bp_location **i_link = &b->loc;
13662 struct bp_location *hoisted = &head;
13663
13664 if (pspace == NULL)
13665 {
13666 i = b->loc;
13667 b->loc = NULL;
13668 return i;
13669 }
13670
13671 head.next = NULL;
13672
13673 while (i != NULL)
13674 {
13675 if (i->pspace == pspace)
13676 {
13677 *i_link = i->next;
13678 i->next = NULL;
13679 hoisted->next = i;
13680 hoisted = i;
13681 }
13682 else
13683 i_link = &i->next;
13684 i = *i_link;
13685 }
13686
13687 return head.next;
13688}
13689
13690/* Create new breakpoint locations for B (a hardware or software
13691 breakpoint) based on SALS and SALS_END. If SALS_END.NELTS is not
13692 zero, then B is a ranged breakpoint. Only recreates locations for
13693 FILTER_PSPACE. Locations of other program spaces are left
13694 untouched. */
f1310107 13695
0e30163f 13696void
0d381245 13697update_breakpoint_locations (struct breakpoint *b,
c2f4122d 13698 struct program_space *filter_pspace,
6c5b2ebe
PA
13699 gdb::array_view<const symtab_and_line> sals,
13700 gdb::array_view<const symtab_and_line> sals_end)
fe3f5fa8 13701{
c2f4122d 13702 struct bp_location *existing_locations;
0d381245 13703
6c5b2ebe 13704 if (!sals_end.empty () && (sals.size () != 1 || sals_end.size () != 1))
f8eba3c6
TT
13705 {
13706 /* Ranged breakpoints have only one start location and one end
13707 location. */
13708 b->enable_state = bp_disabled;
f8eba3c6
TT
13709 printf_unfiltered (_("Could not reset ranged breakpoint %d: "
13710 "multiple locations found\n"),
13711 b->number);
13712 return;
13713 }
f1310107 13714
4a64f543
MS
13715 /* If there's no new locations, and all existing locations are
13716 pending, don't do anything. This optimizes the common case where
13717 all locations are in the same shared library, that was unloaded.
13718 We'd like to retain the location, so that when the library is
13719 loaded again, we don't loose the enabled/disabled status of the
13720 individual locations. */
6c5b2ebe 13721 if (all_locations_are_pending (b, filter_pspace) && sals.empty ())
fe3f5fa8
VP
13722 return;
13723
c2f4122d 13724 existing_locations = hoist_existing_locations (b, filter_pspace);
fe3f5fa8 13725
6c5b2ebe 13726 for (const auto &sal : sals)
fe3f5fa8 13727 {
f8eba3c6
TT
13728 struct bp_location *new_loc;
13729
6c5b2ebe 13730 switch_to_program_space_and_thread (sal.pspace);
f8eba3c6 13731
6c5b2ebe 13732 new_loc = add_location_to_breakpoint (b, &sal);
fe3f5fa8 13733
0d381245
VP
13734 /* Reparse conditions, they might contain references to the
13735 old symtab. */
13736 if (b->cond_string != NULL)
13737 {
bbc13ae3 13738 const char *s;
fe3f5fa8 13739
0d381245 13740 s = b->cond_string;
a70b8144 13741 try
0d381245 13742 {
6c5b2ebe
PA
13743 new_loc->cond = parse_exp_1 (&s, sal.pc,
13744 block_for_pc (sal.pc),
0d381245
VP
13745 0);
13746 }
230d2906 13747 catch (const gdb_exception_error &e)
0d381245 13748 {
b5fa468f 13749 new_loc->disabled_by_cond = true;
0d381245
VP
13750 }
13751 }
fe3f5fa8 13752
6c5b2ebe 13753 if (!sals_end.empty ())
f1310107 13754 {
6c5b2ebe 13755 CORE_ADDR end = find_breakpoint_range_end (sals_end[0]);
f1310107 13756
6c5b2ebe 13757 new_loc->length = end - sals[0].pc + 1;
f1310107 13758 }
0d381245 13759 }
fe3f5fa8 13760
4a64f543
MS
13761 /* If possible, carry over 'disable' status from existing
13762 breakpoints. */
0d381245
VP
13763 {
13764 struct bp_location *e = existing_locations;
776592bf
DE
13765 /* If there are multiple breakpoints with the same function name,
13766 e.g. for inline functions, comparing function names won't work.
13767 Instead compare pc addresses; this is just a heuristic as things
13768 may have moved, but in practice it gives the correct answer
13769 often enough until a better solution is found. */
13770 int have_ambiguous_names = ambiguous_names_p (b->loc);
13771
0d381245
VP
13772 for (; e; e = e->next)
13773 {
b5fa468f 13774 if ((!e->enabled || e->disabled_by_cond) && e->function_name)
0d381245
VP
13775 {
13776 struct bp_location *l = b->loc;
776592bf
DE
13777 if (have_ambiguous_names)
13778 {
13779 for (; l; l = l->next)
7f32a4d5
PA
13780 {
13781 /* Ignore software vs hardware location type at
13782 this point, because with "set breakpoint
13783 auto-hw", after a re-set, locations that were
13784 hardware can end up as software, or vice versa.
13785 As mentioned above, this is an heuristic and in
13786 practice should give the correct answer often
13787 enough. */
13788 if (breakpoint_locations_match (e, l, true))
13789 {
b5fa468f
TBA
13790 l->enabled = e->enabled;
13791 l->disabled_by_cond = e->disabled_by_cond;
7f32a4d5
PA
13792 break;
13793 }
13794 }
776592bf
DE
13795 }
13796 else
13797 {
13798 for (; l; l = l->next)
13799 if (l->function_name
13800 && strcmp (e->function_name, l->function_name) == 0)
13801 {
b5fa468f
TBA
13802 l->enabled = e->enabled;
13803 l->disabled_by_cond = e->disabled_by_cond;
776592bf
DE
13804 break;
13805 }
13806 }
0d381245
VP
13807 }
13808 }
13809 }
fe3f5fa8 13810
8d3788bd 13811 if (!locations_are_equal (existing_locations, b->loc))
76727919 13812 gdb::observers::breakpoint_modified.notify (b);
fe3f5fa8
VP
13813}
13814
f00aae0f 13815/* Find the SaL locations corresponding to the given LOCATION.
ef23e705
TJB
13816 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
13817
6c5b2ebe 13818static std::vector<symtab_and_line>
f00aae0f 13819location_to_sals (struct breakpoint *b, struct event_location *location,
c2f4122d 13820 struct program_space *search_pspace, int *found)
ef23e705 13821{
cc06b668 13822 struct gdb_exception exception;
ef23e705 13823
983af33b 13824 gdb_assert (b->ops != NULL);
ef23e705 13825
6c5b2ebe
PA
13826 std::vector<symtab_and_line> sals;
13827
a70b8144 13828 try
ef23e705 13829 {
6c5b2ebe 13830 sals = b->ops->decode_location (b, location, search_pspace);
ef23e705 13831 }
94aeb44b 13832 catch (gdb_exception_error &e)
ef23e705
TJB
13833 {
13834 int not_found_and_ok = 0;
492d29ea 13835
ef23e705
TJB
13836 /* For pending breakpoints, it's expected that parsing will
13837 fail until the right shared library is loaded. User has
13838 already told to create pending breakpoints and don't need
13839 extra messages. If breakpoint is in bp_shlib_disabled
13840 state, then user already saw the message about that
13841 breakpoint being disabled, and don't want to see more
13842 errors. */
58438ac1 13843 if (e.error == NOT_FOUND_ERROR
c2f4122d
PA
13844 && (b->condition_not_parsed
13845 || (b->loc != NULL
13846 && search_pspace != NULL
13847 && b->loc->pspace != search_pspace)
ef23e705 13848 || (b->loc && b->loc->shlib_disabled)
f8eba3c6 13849 || (b->loc && b->loc->pspace->executing_startup)
ef23e705
TJB
13850 || b->enable_state == bp_disabled))
13851 not_found_and_ok = 1;
13852
13853 if (!not_found_and_ok)
13854 {
13855 /* We surely don't want to warn about the same breakpoint
13856 10 times. One solution, implemented here, is disable
13857 the breakpoint on error. Another solution would be to
13858 have separate 'warning emitted' flag. Since this
13859 happens only when a binary has changed, I don't know
13860 which approach is better. */
13861 b->enable_state = bp_disabled;
eedc3f4f 13862 throw;
ef23e705 13863 }
94aeb44b
TT
13864
13865 exception = std::move (e);
ef23e705
TJB
13866 }
13867
492d29ea 13868 if (exception.reason == 0 || exception.error != NOT_FOUND_ERROR)
ef23e705 13869 {
6c5b2ebe
PA
13870 for (auto &sal : sals)
13871 resolve_sal_pc (&sal);
f00aae0f 13872 if (b->condition_not_parsed && b->extra_string != NULL)
ef23e705 13873 {
ed1d1739
KS
13874 char *cond_string, *extra_string;
13875 int thread, task;
ef23e705 13876
b5fa468f
TBA
13877 find_condition_and_thread_for_sals (sals, b->extra_string,
13878 &cond_string, &thread,
13879 &task, &extra_string);
f00aae0f 13880 gdb_assert (b->cond_string == NULL);
ef23e705
TJB
13881 if (cond_string)
13882 b->cond_string = cond_string;
13883 b->thread = thread;
13884 b->task = task;
e7e0cddf 13885 if (extra_string)
f00aae0f
KS
13886 {
13887 xfree (b->extra_string);
13888 b->extra_string = extra_string;
13889 }
ef23e705
TJB
13890 b->condition_not_parsed = 0;
13891 }
13892
983af33b 13893 if (b->type == bp_static_tracepoint && !strace_marker_p (b))
6c5b2ebe 13894 sals[0] = update_static_tracepoint (b, sals[0]);
ef23e705 13895
58438ac1
TT
13896 *found = 1;
13897 }
13898 else
13899 *found = 0;
ef23e705
TJB
13900
13901 return sals;
13902}
13903
348d480f
PA
13904/* The default re_set method, for typical hardware or software
13905 breakpoints. Reevaluate the breakpoint and recreate its
13906 locations. */
13907
13908static void
28010a5d 13909breakpoint_re_set_default (struct breakpoint *b)
ef23e705 13910{
c2f4122d 13911 struct program_space *filter_pspace = current_program_space;
6c5b2ebe 13912 std::vector<symtab_and_line> expanded, expanded_end;
ef23e705 13913
6c5b2ebe
PA
13914 int found;
13915 std::vector<symtab_and_line> sals = location_to_sals (b, b->location.get (),
13916 filter_pspace, &found);
ef23e705 13917 if (found)
6c5b2ebe 13918 expanded = std::move (sals);
ef23e705 13919
f00aae0f 13920 if (b->location_range_end != NULL)
f1310107 13921 {
6c5b2ebe
PA
13922 std::vector<symtab_and_line> sals_end
13923 = location_to_sals (b, b->location_range_end.get (),
13924 filter_pspace, &found);
f1310107 13925 if (found)
6c5b2ebe 13926 expanded_end = std::move (sals_end);
f1310107
TJB
13927 }
13928
c2f4122d 13929 update_breakpoint_locations (b, filter_pspace, expanded, expanded_end);
28010a5d
PA
13930}
13931
983af33b
SDJ
13932/* Default method for creating SALs from an address string. It basically
13933 calls parse_breakpoint_sals. Return 1 for success, zero for failure. */
13934
13935static void
626d2320 13936create_sals_from_location_default (struct event_location *location,
f00aae0f
KS
13937 struct linespec_result *canonical,
13938 enum bptype type_wanted)
983af33b 13939{
f00aae0f 13940 parse_breakpoint_sals (location, canonical);
983af33b
SDJ
13941}
13942
13943/* Call create_breakpoints_sal for the given arguments. This is the default
13944 function for the `create_breakpoints_sal' method of
13945 breakpoint_ops. */
13946
13947static void
13948create_breakpoints_sal_default (struct gdbarch *gdbarch,
13949 struct linespec_result *canonical,
e1e01040
PA
13950 gdb::unique_xmalloc_ptr<char> cond_string,
13951 gdb::unique_xmalloc_ptr<char> extra_string,
983af33b
SDJ
13952 enum bptype type_wanted,
13953 enum bpdisp disposition,
13954 int thread,
13955 int task, int ignore_count,
13956 const struct breakpoint_ops *ops,
13957 int from_tty, int enabled,
44f238bb 13958 int internal, unsigned flags)
983af33b 13959{
e1e01040
PA
13960 create_breakpoints_sal (gdbarch, canonical,
13961 std::move (cond_string),
13962 std::move (extra_string),
983af33b
SDJ
13963 type_wanted, disposition,
13964 thread, task, ignore_count, ops, from_tty,
44f238bb 13965 enabled, internal, flags);
983af33b
SDJ
13966}
13967
13968/* Decode the line represented by S by calling decode_line_full. This is the
5f700d83 13969 default function for the `decode_location' method of breakpoint_ops. */
983af33b 13970
6c5b2ebe 13971static std::vector<symtab_and_line>
f00aae0f 13972decode_location_default (struct breakpoint *b,
626d2320 13973 struct event_location *location,
6c5b2ebe 13974 struct program_space *search_pspace)
983af33b
SDJ
13975{
13976 struct linespec_result canonical;
13977
c2f4122d 13978 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, search_pspace,
cafb3438 13979 NULL, 0, &canonical, multiple_symbols_all,
c0e8dcd8 13980 b->filter.get ());
983af33b
SDJ
13981
13982 /* We should get 0 or 1 resulting SALs. */
6c5b2ebe 13983 gdb_assert (canonical.lsals.size () < 2);
983af33b 13984
6c5b2ebe 13985 if (!canonical.lsals.empty ())
983af33b 13986 {
6c5b2ebe
PA
13987 const linespec_sals &lsal = canonical.lsals[0];
13988 return std::move (lsal.sals);
983af33b 13989 }
6c5b2ebe 13990 return {};
983af33b
SDJ
13991}
13992
bf469271 13993/* Reset a breakpoint. */
c906108c 13994
bf469271
PA
13995static void
13996breakpoint_re_set_one (breakpoint *b)
c906108c 13997{
fdf44873
TT
13998 input_radix = b->input_radix;
13999 set_language (b->language);
c906108c 14000
348d480f 14001 b->ops->re_set (b);
c906108c
SS
14002}
14003
c2f4122d
PA
14004/* Re-set breakpoint locations for the current program space.
14005 Locations bound to other program spaces are left untouched. */
14006
c906108c 14007void
69de3c6a 14008breakpoint_re_set (void)
c906108c 14009{
35df4500 14010 struct breakpoint *b, *b_tmp;
2a7f3dff 14011
c5aa993b 14012 {
fdf44873
TT
14013 scoped_restore_current_language save_language;
14014 scoped_restore save_input_radix = make_scoped_restore (&input_radix);
5ed8105e 14015 scoped_restore_current_pspace_and_thread restore_pspace_thread;
e62c965a 14016
8e817061
JB
14017 /* breakpoint_re_set_one sets the current_language to the language
14018 of the breakpoint it is resetting (see prepare_re_set_context)
14019 before re-evaluating the breakpoint's location. This change can
14020 unfortunately get undone by accident if the language_mode is set
14021 to auto, and we either switch frames, or more likely in this context,
14022 we select the current frame.
14023
14024 We prevent this by temporarily turning the language_mode to
14025 language_mode_manual. We restore it once all breakpoints
14026 have been reset. */
14027 scoped_restore save_language_mode = make_scoped_restore (&language_mode);
14028 language_mode = language_mode_manual;
14029
5ed8105e
PA
14030 /* Note: we must not try to insert locations until after all
14031 breakpoints have been re-set. Otherwise, e.g., when re-setting
14032 breakpoint 1, we'd insert the locations of breakpoint 2, which
14033 hadn't been re-set yet, and thus may have stale locations. */
4efc6507 14034
5ed8105e
PA
14035 ALL_BREAKPOINTS_SAFE (b, b_tmp)
14036 {
a70b8144 14037 try
bf469271
PA
14038 {
14039 breakpoint_re_set_one (b);
14040 }
230d2906 14041 catch (const gdb_exception &ex)
bf469271
PA
14042 {
14043 exception_fprintf (gdb_stderr, ex,
14044 "Error in re-setting breakpoint %d: ",
14045 b->number);
14046 }
5ed8105e 14047 }
5ed8105e
PA
14048
14049 jit_breakpoint_re_set ();
14050 }
6c95b8df 14051
af02033e
PP
14052 create_overlay_event_breakpoint ();
14053 create_longjmp_master_breakpoint ();
14054 create_std_terminate_master_breakpoint ();
186c406b 14055 create_exception_master_breakpoint ();
2a7f3dff
PA
14056
14057 /* Now we can insert. */
14058 update_global_location_list (UGLL_MAY_INSERT);
c906108c
SS
14059}
14060\f
c906108c
SS
14061/* Reset the thread number of this breakpoint:
14062
14063 - If the breakpoint is for all threads, leave it as-is.
4a64f543 14064 - Else, reset it to the current thread for inferior_ptid. */
c906108c 14065void
fba45db2 14066breakpoint_re_set_thread (struct breakpoint *b)
c906108c
SS
14067{
14068 if (b->thread != -1)
14069 {
00431a78 14070 b->thread = inferior_thread ()->global_num;
6c95b8df
PA
14071
14072 /* We're being called after following a fork. The new fork is
14073 selected as current, and unless this was a vfork will have a
14074 different program space from the original thread. Reset that
14075 as well. */
14076 b->loc->pspace = current_program_space;
c906108c
SS
14077 }
14078}
14079
03ac34d5
MS
14080/* Set ignore-count of breakpoint number BPTNUM to COUNT.
14081 If from_tty is nonzero, it prints a message to that effect,
14082 which ends with a period (no newline). */
14083
c906108c 14084void
fba45db2 14085set_ignore_count (int bptnum, int count, int from_tty)
c906108c 14086{
52f0bd74 14087 struct breakpoint *b;
c906108c
SS
14088
14089 if (count < 0)
14090 count = 0;
14091
14092 ALL_BREAKPOINTS (b)
14093 if (b->number == bptnum)
c5aa993b 14094 {
d77f58be
SS
14095 if (is_tracepoint (b))
14096 {
14097 if (from_tty && count != 0)
14098 printf_filtered (_("Ignore count ignored for tracepoint %d."),
14099 bptnum);
14100 return;
14101 }
14102
c5aa993b 14103 b->ignore_count = count;
221ea385
KS
14104 if (from_tty)
14105 {
14106 if (count == 0)
3e43a32a
MS
14107 printf_filtered (_("Will stop next time "
14108 "breakpoint %d is reached."),
221ea385
KS
14109 bptnum);
14110 else if (count == 1)
a3f17187 14111 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
221ea385
KS
14112 bptnum);
14113 else
3e43a32a
MS
14114 printf_filtered (_("Will ignore next %d "
14115 "crossings of breakpoint %d."),
221ea385
KS
14116 count, bptnum);
14117 }
76727919 14118 gdb::observers::breakpoint_modified.notify (b);
c5aa993b
JM
14119 return;
14120 }
c906108c 14121
8a3fe4f8 14122 error (_("No breakpoint number %d."), bptnum);
c906108c
SS
14123}
14124
c906108c
SS
14125/* Command to set ignore-count of breakpoint N to COUNT. */
14126
14127static void
0b39b52e 14128ignore_command (const char *args, int from_tty)
c906108c 14129{
0b39b52e 14130 const char *p = args;
52f0bd74 14131 int num;
c906108c
SS
14132
14133 if (p == 0)
e2e0b3e5 14134 error_no_arg (_("a breakpoint number"));
c5aa993b 14135
c906108c 14136 num = get_number (&p);
5c44784c 14137 if (num == 0)
8a3fe4f8 14138 error (_("bad breakpoint number: '%s'"), args);
c906108c 14139 if (*p == 0)
8a3fe4f8 14140 error (_("Second argument (specified ignore-count) is missing."));
c906108c
SS
14141
14142 set_ignore_count (num,
14143 longest_to_int (value_as_long (parse_and_eval (p))),
14144 from_tty);
221ea385
KS
14145 if (from_tty)
14146 printf_filtered ("\n");
c906108c
SS
14147}
14148\f
d0fe4701
XR
14149
14150/* Call FUNCTION on each of the breakpoints with numbers in the range
14151 defined by BP_NUM_RANGE (an inclusive range). */
c906108c
SS
14152
14153static void
d0fe4701
XR
14154map_breakpoint_number_range (std::pair<int, int> bp_num_range,
14155 gdb::function_view<void (breakpoint *)> function)
c906108c 14156{
d0fe4701
XR
14157 if (bp_num_range.first == 0)
14158 {
14159 warning (_("bad breakpoint number at or near '%d'"),
14160 bp_num_range.first);
14161 }
14162 else
c906108c 14163 {
d0fe4701 14164 struct breakpoint *b, *tmp;
197f0a60 14165
d0fe4701 14166 for (int i = bp_num_range.first; i <= bp_num_range.second; i++)
5c44784c 14167 {
d0fe4701
XR
14168 bool match = false;
14169
5c44784c 14170 ALL_BREAKPOINTS_SAFE (b, tmp)
d0fe4701 14171 if (b->number == i)
5c44784c 14172 {
bfd28288 14173 match = true;
48649e1b 14174 function (b);
11cf8741 14175 break;
5c44784c 14176 }
bfd28288 14177 if (!match)
d0fe4701 14178 printf_unfiltered (_("No breakpoint number %d.\n"), i);
c5aa993b 14179 }
c906108c
SS
14180 }
14181}
14182
d0fe4701
XR
14183/* Call FUNCTION on each of the breakpoints whose numbers are given in
14184 ARGS. */
14185
14186static void
14187map_breakpoint_numbers (const char *args,
14188 gdb::function_view<void (breakpoint *)> function)
14189{
14190 if (args == NULL || *args == '\0')
14191 error_no_arg (_("one or more breakpoint numbers"));
14192
14193 number_or_range_parser parser (args);
14194
14195 while (!parser.finished ())
14196 {
14197 int num = parser.get_number ();
14198 map_breakpoint_number_range (std::make_pair (num, num), function);
14199 }
14200}
14201
14202/* Return the breakpoint location structure corresponding to the
14203 BP_NUM and LOC_NUM values. */
14204
0d381245 14205static struct bp_location *
d0fe4701 14206find_location_by_number (int bp_num, int loc_num)
0d381245 14207{
0d381245 14208 struct breakpoint *b;
0d381245
VP
14209
14210 ALL_BREAKPOINTS (b)
14211 if (b->number == bp_num)
14212 {
14213 break;
14214 }
14215
14216 if (!b || b->number != bp_num)
d0fe4701 14217 error (_("Bad breakpoint number '%d'"), bp_num);
0d381245 14218
0d381245 14219 if (loc_num == 0)
d0fe4701 14220 error (_("Bad breakpoint location number '%d'"), loc_num);
0d381245 14221
d0fe4701
XR
14222 int n = 0;
14223 for (bp_location *loc = b->loc; loc != NULL; loc = loc->next)
14224 if (++n == loc_num)
14225 return loc;
14226
14227 error (_("Bad breakpoint location number '%d'"), loc_num);
0d381245
VP
14228}
14229
95e95a6d
PA
14230/* Modes of operation for extract_bp_num. */
14231enum class extract_bp_kind
14232{
14233 /* Extracting a breakpoint number. */
14234 bp,
14235
14236 /* Extracting a location number. */
14237 loc,
14238};
14239
14240/* Extract a breakpoint or location number (as determined by KIND)
14241 from the string starting at START. TRAILER is a character which
14242 can be found after the number. If you don't want a trailer, use
14243 '\0'. If END_OUT is not NULL, it is set to point after the parsed
14244 string. This always returns a positive integer. */
14245
14246static int
14247extract_bp_num (extract_bp_kind kind, const char *start,
14248 int trailer, const char **end_out = NULL)
14249{
14250 const char *end = start;
14251 int num = get_number_trailer (&end, trailer);
14252 if (num < 0)
14253 error (kind == extract_bp_kind::bp
14254 ? _("Negative breakpoint number '%.*s'")
14255 : _("Negative breakpoint location number '%.*s'"),
14256 int (end - start), start);
14257 if (num == 0)
14258 error (kind == extract_bp_kind::bp
14259 ? _("Bad breakpoint number '%.*s'")
14260 : _("Bad breakpoint location number '%.*s'"),
14261 int (end - start), start);
14262
14263 if (end_out != NULL)
14264 *end_out = end;
14265 return num;
14266}
14267
14268/* Extract a breakpoint or location range (as determined by KIND) in
14269 the form NUM1-NUM2 stored at &ARG[arg_offset]. Returns a std::pair
14270 representing the (inclusive) range. The returned pair's elements
14271 are always positive integers. */
14272
14273static std::pair<int, int>
14274extract_bp_or_bp_range (extract_bp_kind kind,
14275 const std::string &arg,
14276 std::string::size_type arg_offset)
14277{
14278 std::pair<int, int> range;
14279 const char *bp_loc = &arg[arg_offset];
14280 std::string::size_type dash = arg.find ('-', arg_offset);
14281 if (dash != std::string::npos)
14282 {
14283 /* bp_loc is a range (x-z). */
14284 if (arg.length () == dash + 1)
14285 error (kind == extract_bp_kind::bp
14286 ? _("Bad breakpoint number at or near: '%s'")
14287 : _("Bad breakpoint location number at or near: '%s'"),
14288 bp_loc);
14289
14290 const char *end;
14291 const char *start_first = bp_loc;
14292 const char *start_second = &arg[dash + 1];
14293 range.first = extract_bp_num (kind, start_first, '-');
14294 range.second = extract_bp_num (kind, start_second, '\0', &end);
14295
14296 if (range.first > range.second)
14297 error (kind == extract_bp_kind::bp
14298 ? _("Inverted breakpoint range at '%.*s'")
14299 : _("Inverted breakpoint location range at '%.*s'"),
14300 int (end - start_first), start_first);
14301 }
14302 else
14303 {
14304 /* bp_loc is a single value. */
14305 range.first = extract_bp_num (kind, bp_loc, '\0');
14306 range.second = range.first;
14307 }
14308 return range;
14309}
14310
d0fe4701
XR
14311/* Extract the breakpoint/location range specified by ARG. Returns
14312 the breakpoint range in BP_NUM_RANGE, and the location range in
14313 BP_LOC_RANGE.
14314
14315 ARG may be in any of the following forms:
14316
14317 x where 'x' is a breakpoint number.
14318 x-y where 'x' and 'y' specify a breakpoint numbers range.
14319 x.y where 'x' is a breakpoint number and 'y' a location number.
14320 x.y-z where 'x' is a breakpoint number and 'y' and 'z' specify a
14321 location number range.
14322*/
14323
cc638e86 14324static void
d0fe4701
XR
14325extract_bp_number_and_location (const std::string &arg,
14326 std::pair<int, int> &bp_num_range,
14327 std::pair<int, int> &bp_loc_range)
14328{
14329 std::string::size_type dot = arg.find ('.');
14330
14331 if (dot != std::string::npos)
14332 {
14333 /* Handle 'x.y' and 'x.y-z' cases. */
14334
14335 if (arg.length () == dot + 1 || dot == 0)
95e95a6d 14336 error (_("Bad breakpoint number at or near: '%s'"), arg.c_str ());
d0fe4701 14337
95e95a6d
PA
14338 bp_num_range.first
14339 = extract_bp_num (extract_bp_kind::bp, arg.c_str (), '.');
14340 bp_num_range.second = bp_num_range.first;
d0fe4701 14341
95e95a6d
PA
14342 bp_loc_range = extract_bp_or_bp_range (extract_bp_kind::loc,
14343 arg, dot + 1);
d0fe4701
XR
14344 }
14345 else
14346 {
14347 /* Handle x and x-y cases. */
d0fe4701 14348
95e95a6d 14349 bp_num_range = extract_bp_or_bp_range (extract_bp_kind::bp, arg, 0);
d0fe4701
XR
14350 bp_loc_range.first = 0;
14351 bp_loc_range.second = 0;
14352 }
d0fe4701
XR
14353}
14354
14355/* Enable or disable a breakpoint location BP_NUM.LOC_NUM. ENABLE
14356 specifies whether to enable or disable. */
14357
14358static void
14359enable_disable_bp_num_loc (int bp_num, int loc_num, bool enable)
14360{
14361 struct bp_location *loc = find_location_by_number (bp_num, loc_num);
14362 if (loc != NULL)
14363 {
b5fa468f
TBA
14364 if (loc->disabled_by_cond && enable)
14365 error (_("Breakpoint %d's condition is invalid at location %d, "
14366 "cannot enable."), bp_num, loc_num);
14367
d0fe4701
XR
14368 if (loc->enabled != enable)
14369 {
14370 loc->enabled = enable;
14371 mark_breakpoint_location_modified (loc);
14372 }
14373 if (target_supports_enable_disable_tracepoint ()
14374 && current_trace_status ()->running && loc->owner
14375 && is_tracepoint (loc->owner))
14376 target_disable_tracepoint (loc);
14377 }
14378 update_global_location_list (UGLL_DONT_INSERT);
d7154a8d
JV
14379
14380 gdb::observers::breakpoint_modified.notify (loc->owner);
d0fe4701
XR
14381}
14382
14383/* Enable or disable a range of breakpoint locations. BP_NUM is the
14384 number of the breakpoint, and BP_LOC_RANGE specifies the
14385 (inclusive) range of location numbers of that breakpoint to
14386 enable/disable. ENABLE specifies whether to enable or disable the
14387 location. */
14388
14389static void
14390enable_disable_breakpoint_location_range (int bp_num,
14391 std::pair<int, int> &bp_loc_range,
14392 bool enable)
14393{
14394 for (int i = bp_loc_range.first; i <= bp_loc_range.second; i++)
14395 enable_disable_bp_num_loc (bp_num, i, enable);
14396}
0d381245 14397
1900040c
MS
14398/* Set ignore-count of breakpoint number BPTNUM to COUNT.
14399 If from_tty is nonzero, it prints a message to that effect,
14400 which ends with a period (no newline). */
14401
c906108c 14402void
fba45db2 14403disable_breakpoint (struct breakpoint *bpt)
c906108c
SS
14404{
14405 /* Never disable a watchpoint scope breakpoint; we want to
14406 hit them when we leave scope so we can delete both the
14407 watchpoint and its scope breakpoint at that time. */
14408 if (bpt->type == bp_watchpoint_scope)
14409 return;
14410
b5de0fa7 14411 bpt->enable_state = bp_disabled;
c906108c 14412
b775012e
LM
14413 /* Mark breakpoint locations modified. */
14414 mark_breakpoint_modified (bpt);
14415
d248b706
KY
14416 if (target_supports_enable_disable_tracepoint ()
14417 && current_trace_status ()->running && is_tracepoint (bpt))
14418 {
14419 struct bp_location *location;
14420
14421 for (location = bpt->loc; location; location = location->next)
14422 target_disable_tracepoint (location);
14423 }
14424
44702360 14425 update_global_location_list (UGLL_DONT_INSERT);
c906108c 14426
76727919 14427 gdb::observers::breakpoint_modified.notify (bpt);
c906108c
SS
14428}
14429
d0fe4701
XR
14430/* Enable or disable the breakpoint(s) or breakpoint location(s)
14431 specified in ARGS. ARGS may be in any of the formats handled by
14432 extract_bp_number_and_location. ENABLE specifies whether to enable
14433 or disable the breakpoints/locations. */
14434
c906108c 14435static void
d0fe4701 14436enable_disable_command (const char *args, int from_tty, bool enable)
c906108c 14437{
c906108c 14438 if (args == 0)
46c6471b
PA
14439 {
14440 struct breakpoint *bpt;
14441
14442 ALL_BREAKPOINTS (bpt)
14443 if (user_breakpoint_p (bpt))
d0fe4701
XR
14444 {
14445 if (enable)
14446 enable_breakpoint (bpt);
14447 else
14448 disable_breakpoint (bpt);
14449 }
46c6471b 14450 }
9eaabc75 14451 else
0d381245 14452 {
cb791d59 14453 std::string num = extract_arg (&args);
9eaabc75 14454
cb791d59 14455 while (!num.empty ())
d248b706 14456 {
d0fe4701 14457 std::pair<int, int> bp_num_range, bp_loc_range;
9eaabc75 14458
cc638e86
PA
14459 extract_bp_number_and_location (num, bp_num_range, bp_loc_range);
14460
14461 if (bp_loc_range.first == bp_loc_range.second
14462 && bp_loc_range.first == 0)
d0fe4701 14463 {
cc638e86
PA
14464 /* Handle breakpoint ids with formats 'x' or 'x-z'. */
14465 map_breakpoint_number_range (bp_num_range,
14466 enable
14467 ? enable_breakpoint
14468 : disable_breakpoint);
14469 }
14470 else
14471 {
14472 /* Handle breakpoint ids with formats 'x.y' or
14473 'x.y-z'. */
14474 enable_disable_breakpoint_location_range
14475 (bp_num_range.first, bp_loc_range, enable);
b775012e 14476 }
9eaabc75 14477 num = extract_arg (&args);
d248b706 14478 }
0d381245 14479 }
c906108c
SS
14480}
14481
d0fe4701
XR
14482/* The disable command disables the specified breakpoints/locations
14483 (or all defined breakpoints) so they're no longer effective in
14484 stopping the inferior. ARGS may be in any of the forms defined in
14485 extract_bp_number_and_location. */
14486
14487static void
14488disable_command (const char *args, int from_tty)
14489{
14490 enable_disable_command (args, from_tty, false);
14491}
14492
c906108c 14493static void
816338b5
SS
14494enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
14495 int count)
c906108c 14496{
afe38095 14497 int target_resources_ok;
c906108c
SS
14498
14499 if (bpt->type == bp_hardware_breakpoint)
14500 {
14501 int i;
c5aa993b 14502 i = hw_breakpoint_used_count ();
53a5351d 14503 target_resources_ok =
d92524f1 14504 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
53a5351d 14505 i + 1, 0);
c906108c 14506 if (target_resources_ok == 0)
8a3fe4f8 14507 error (_("No hardware breakpoint support in the target."));
c906108c 14508 else if (target_resources_ok < 0)
8a3fe4f8 14509 error (_("Hardware breakpoints used exceeds limit."));
c906108c
SS
14510 }
14511
cc60f2e3 14512 if (is_watchpoint (bpt))
c906108c 14513 {
d07205c2 14514 /* Initialize it just to avoid a GCC false warning. */
f486487f 14515 enum enable_state orig_enable_state = bp_disabled;
dde02812 14516
a70b8144 14517 try
c906108c 14518 {
3a5c3e22
PA
14519 struct watchpoint *w = (struct watchpoint *) bpt;
14520
1e718ff1
TJB
14521 orig_enable_state = bpt->enable_state;
14522 bpt->enable_state = bp_enabled;
3a5c3e22 14523 update_watchpoint (w, 1 /* reparse */);
c906108c 14524 }
230d2906 14525 catch (const gdb_exception &e)
c5aa993b 14526 {
1e718ff1 14527 bpt->enable_state = orig_enable_state;
dde02812
ES
14528 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
14529 bpt->number);
14530 return;
c5aa993b 14531 }
c906108c 14532 }
0101ce28 14533
b775012e
LM
14534 bpt->enable_state = bp_enabled;
14535
14536 /* Mark breakpoint locations modified. */
14537 mark_breakpoint_modified (bpt);
14538
d248b706
KY
14539 if (target_supports_enable_disable_tracepoint ()
14540 && current_trace_status ()->running && is_tracepoint (bpt))
14541 {
14542 struct bp_location *location;
14543
14544 for (location = bpt->loc; location; location = location->next)
14545 target_enable_tracepoint (location);
14546 }
14547
b4c291bb 14548 bpt->disposition = disposition;
816338b5 14549 bpt->enable_count = count;
44702360 14550 update_global_location_list (UGLL_MAY_INSERT);
9c97429f 14551
76727919 14552 gdb::observers::breakpoint_modified.notify (bpt);
c906108c
SS
14553}
14554
fe3f5fa8 14555
c906108c 14556void
fba45db2 14557enable_breakpoint (struct breakpoint *bpt)
c906108c 14558{
816338b5 14559 enable_breakpoint_disp (bpt, bpt->disposition, 0);
51be5b68
PA
14560}
14561
d0fe4701
XR
14562/* The enable command enables the specified breakpoints/locations (or
14563 all defined breakpoints) so they once again become (or continue to
14564 be) effective in stopping the inferior. ARGS may be in any of the
14565 forms defined in extract_bp_number_and_location. */
c906108c 14566
c906108c 14567static void
981a3fb3 14568enable_command (const char *args, int from_tty)
c906108c 14569{
d0fe4701 14570 enable_disable_command (args, from_tty, true);
c906108c
SS
14571}
14572
c906108c 14573static void
4495129a 14574enable_once_command (const char *args, int from_tty)
c906108c 14575{
48649e1b
TT
14576 map_breakpoint_numbers
14577 (args, [&] (breakpoint *b)
14578 {
14579 iterate_over_related_breakpoints
14580 (b, [&] (breakpoint *bpt)
14581 {
14582 enable_breakpoint_disp (bpt, disp_disable, 1);
14583 });
14584 });
816338b5
SS
14585}
14586
14587static void
4495129a 14588enable_count_command (const char *args, int from_tty)
816338b5 14589{
b9d61307
SM
14590 int count;
14591
14592 if (args == NULL)
14593 error_no_arg (_("hit count"));
14594
14595 count = get_number (&args);
816338b5 14596
48649e1b
TT
14597 map_breakpoint_numbers
14598 (args, [&] (breakpoint *b)
14599 {
14600 iterate_over_related_breakpoints
14601 (b, [&] (breakpoint *bpt)
14602 {
14603 enable_breakpoint_disp (bpt, disp_disable, count);
14604 });
14605 });
c906108c
SS
14606}
14607
c906108c 14608static void
4495129a 14609enable_delete_command (const char *args, int from_tty)
c906108c 14610{
48649e1b
TT
14611 map_breakpoint_numbers
14612 (args, [&] (breakpoint *b)
14613 {
14614 iterate_over_related_breakpoints
14615 (b, [&] (breakpoint *bpt)
14616 {
14617 enable_breakpoint_disp (bpt, disp_del, 1);
14618 });
14619 });
c906108c
SS
14620}
14621\f
1f3b5d1b
PP
14622/* Invalidate last known value of any hardware watchpoint if
14623 the memory which that value represents has been written to by
14624 GDB itself. */
14625
14626static void
8de0566d
YQ
14627invalidate_bp_value_on_memory_change (struct inferior *inferior,
14628 CORE_ADDR addr, ssize_t len,
1f3b5d1b
PP
14629 const bfd_byte *data)
14630{
14631 struct breakpoint *bp;
14632
14633 ALL_BREAKPOINTS (bp)
14634 if (bp->enable_state == bp_enabled
3a5c3e22 14635 && bp->type == bp_hardware_watchpoint)
1f3b5d1b 14636 {
3a5c3e22 14637 struct watchpoint *wp = (struct watchpoint *) bp;
1f3b5d1b 14638
850645cf 14639 if (wp->val_valid && wp->val != nullptr)
3a5c3e22
PA
14640 {
14641 struct bp_location *loc;
14642
14643 for (loc = bp->loc; loc != NULL; loc = loc->next)
14644 if (loc->loc_type == bp_loc_hardware_watchpoint
14645 && loc->address + loc->length > addr
14646 && addr + len > loc->address)
14647 {
3a5c3e22 14648 wp->val = NULL;
4c1d86d9 14649 wp->val_valid = false;
3a5c3e22
PA
14650 }
14651 }
1f3b5d1b
PP
14652 }
14653}
14654
8181d85f
DJ
14655/* Create and insert a breakpoint for software single step. */
14656
14657void
6c95b8df 14658insert_single_step_breakpoint (struct gdbarch *gdbarch,
accd0bcd 14659 const address_space *aspace,
4a64f543 14660 CORE_ADDR next_pc)
8181d85f 14661{
7c16b83e
PA
14662 struct thread_info *tp = inferior_thread ();
14663 struct symtab_and_line sal;
14664 CORE_ADDR pc = next_pc;
8181d85f 14665
34b7e8a6
PA
14666 if (tp->control.single_step_breakpoints == NULL)
14667 {
14668 tp->control.single_step_breakpoints
5d5658a1 14669 = new_single_step_breakpoint (tp->global_num, gdbarch);
34b7e8a6 14670 }
8181d85f 14671
7c16b83e
PA
14672 sal = find_pc_line (pc, 0);
14673 sal.pc = pc;
14674 sal.section = find_pc_overlay (pc);
14675 sal.explicit_pc = 1;
34b7e8a6 14676 add_location_to_breakpoint (tp->control.single_step_breakpoints, &sal);
8181d85f 14677
7c16b83e 14678 update_global_location_list (UGLL_INSERT);
8181d85f
DJ
14679}
14680
93f9a11f
YQ
14681/* Insert single step breakpoints according to the current state. */
14682
14683int
14684insert_single_step_breakpoints (struct gdbarch *gdbarch)
14685{
f5ea389a 14686 struct regcache *regcache = get_current_regcache ();
a0ff9e1a 14687 std::vector<CORE_ADDR> next_pcs;
93f9a11f 14688
f5ea389a 14689 next_pcs = gdbarch_software_single_step (gdbarch, regcache);
93f9a11f 14690
a0ff9e1a 14691 if (!next_pcs.empty ())
93f9a11f 14692 {
f5ea389a 14693 struct frame_info *frame = get_current_frame ();
8b86c959 14694 const address_space *aspace = get_frame_address_space (frame);
93f9a11f 14695
a0ff9e1a 14696 for (CORE_ADDR pc : next_pcs)
93f9a11f
YQ
14697 insert_single_step_breakpoint (gdbarch, aspace, pc);
14698
93f9a11f
YQ
14699 return 1;
14700 }
14701 else
14702 return 0;
14703}
14704
34b7e8a6 14705/* See breakpoint.h. */
f02253f1
HZ
14706
14707int
7c16b83e 14708breakpoint_has_location_inserted_here (struct breakpoint *bp,
accd0bcd 14709 const address_space *aspace,
7c16b83e 14710 CORE_ADDR pc)
1aafd4da 14711{
7c16b83e 14712 struct bp_location *loc;
1aafd4da 14713
7c16b83e
PA
14714 for (loc = bp->loc; loc != NULL; loc = loc->next)
14715 if (loc->inserted
14716 && breakpoint_location_address_match (loc, aspace, pc))
14717 return 1;
1aafd4da 14718
7c16b83e 14719 return 0;
ef370185
JB
14720}
14721
14722/* Check whether a software single-step breakpoint is inserted at
14723 PC. */
14724
14725int
accd0bcd 14726single_step_breakpoint_inserted_here_p (const address_space *aspace,
ef370185
JB
14727 CORE_ADDR pc)
14728{
34b7e8a6
PA
14729 struct breakpoint *bpt;
14730
14731 ALL_BREAKPOINTS (bpt)
14732 {
14733 if (bpt->type == bp_single_step
14734 && breakpoint_has_location_inserted_here (bpt, aspace, pc))
14735 return 1;
14736 }
14737 return 0;
1aafd4da
UW
14738}
14739
1042e4c0
SS
14740/* Tracepoint-specific operations. */
14741
14742/* Set tracepoint count to NUM. */
14743static void
14744set_tracepoint_count (int num)
14745{
14746 tracepoint_count = num;
4fa62494 14747 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
1042e4c0
SS
14748}
14749
70221824 14750static void
0b39b52e 14751trace_command (const char *arg, int from_tty)
1042e4c0 14752{
ffc2605c
TT
14753 event_location_up location = string_to_event_location (&arg,
14754 current_language);
bac7c5cf
GB
14755 const struct breakpoint_ops *ops = breakpoint_ops_for_event_location
14756 (location.get (), true /* is_tracepoint */);
55aa24fb 14757
558a9d82 14758 create_breakpoint (get_current_arch (),
ffc2605c 14759 location.get (),
10a636cc 14760 NULL, 0, arg, false, 1 /* parse arg */,
558a9d82
YQ
14761 0 /* tempflag */,
14762 bp_tracepoint /* type_wanted */,
14763 0 /* Ignore count */,
14764 pending_break_support,
14765 ops,
14766 from_tty,
14767 1 /* enabled */,
14768 0 /* internal */, 0);
1042e4c0
SS
14769}
14770
70221824 14771static void
0b39b52e 14772ftrace_command (const char *arg, int from_tty)
7a697b8d 14773{
ffc2605c
TT
14774 event_location_up location = string_to_event_location (&arg,
14775 current_language);
558a9d82 14776 create_breakpoint (get_current_arch (),
ffc2605c 14777 location.get (),
10a636cc 14778 NULL, 0, arg, false, 1 /* parse arg */,
558a9d82
YQ
14779 0 /* tempflag */,
14780 bp_fast_tracepoint /* type_wanted */,
14781 0 /* Ignore count */,
14782 pending_break_support,
14783 &tracepoint_breakpoint_ops,
14784 from_tty,
14785 1 /* enabled */,
14786 0 /* internal */, 0);
0fb4aa4b
PA
14787}
14788
14789/* strace command implementation. Creates a static tracepoint. */
14790
70221824 14791static void
0b39b52e 14792strace_command (const char *arg, int from_tty)
0fb4aa4b 14793{
983af33b 14794 struct breakpoint_ops *ops;
ffc2605c 14795 event_location_up location;
983af33b
SDJ
14796
14797 /* Decide if we are dealing with a static tracepoint marker (`-m'),
14798 or with a normal static tracepoint. */
61012eef 14799 if (arg && startswith (arg, "-m") && isspace (arg[2]))
f00aae0f
KS
14800 {
14801 ops = &strace_marker_breakpoint_ops;
a20714ff 14802 location = new_linespec_location (&arg, symbol_name_match_type::FULL);
f00aae0f 14803 }
983af33b 14804 else
f00aae0f
KS
14805 {
14806 ops = &tracepoint_breakpoint_ops;
14807 location = string_to_event_location (&arg, current_language);
14808 }
983af33b 14809
558a9d82 14810 create_breakpoint (get_current_arch (),
ffc2605c 14811 location.get (),
10a636cc 14812 NULL, 0, arg, false, 1 /* parse arg */,
558a9d82
YQ
14813 0 /* tempflag */,
14814 bp_static_tracepoint /* type_wanted */,
14815 0 /* Ignore count */,
14816 pending_break_support,
14817 ops,
14818 from_tty,
14819 1 /* enabled */,
14820 0 /* internal */, 0);
7a697b8d
SS
14821}
14822
409873ef
SS
14823/* Set up a fake reader function that gets command lines from a linked
14824 list that was acquired during tracepoint uploading. */
14825
14826static struct uploaded_tp *this_utp;
3149d8c1 14827static int next_cmd;
409873ef
SS
14828
14829static char *
14830read_uploaded_action (void)
14831{
a18ba4e4 14832 char *rslt = nullptr;
409873ef 14833
a18ba4e4
SM
14834 if (next_cmd < this_utp->cmd_strings.size ())
14835 {
67aa1f3c 14836 rslt = this_utp->cmd_strings[next_cmd].get ();
a18ba4e4
SM
14837 next_cmd++;
14838 }
409873ef
SS
14839
14840 return rslt;
14841}
14842
00bf0b85
SS
14843/* Given information about a tracepoint as recorded on a target (which
14844 can be either a live system or a trace file), attempt to create an
14845 equivalent GDB tracepoint. This is not a reliable process, since
14846 the target does not necessarily have all the information used when
14847 the tracepoint was originally defined. */
14848
d9b3f62e 14849struct tracepoint *
00bf0b85 14850create_tracepoint_from_upload (struct uploaded_tp *utp)
d5551862 14851{
f2fc3015
TT
14852 const char *addr_str;
14853 char small_buf[100];
d9b3f62e 14854 struct tracepoint *tp;
fd9b8c24 14855
409873ef 14856 if (utp->at_string)
67aa1f3c 14857 addr_str = utp->at_string.get ();
409873ef
SS
14858 else
14859 {
14860 /* In the absence of a source location, fall back to raw
14861 address. Since there is no way to confirm that the address
14862 means the same thing as when the trace was started, warn the
14863 user. */
3e43a32a
MS
14864 warning (_("Uploaded tracepoint %d has no "
14865 "source location, using raw address"),
409873ef 14866 utp->number);
8c042590 14867 xsnprintf (small_buf, sizeof (small_buf), "*%s", hex_string (utp->addr));
409873ef
SS
14868 addr_str = small_buf;
14869 }
14870
14871 /* There's not much we can do with a sequence of bytecodes. */
14872 if (utp->cond && !utp->cond_string)
3e43a32a
MS
14873 warning (_("Uploaded tracepoint %d condition "
14874 "has no source form, ignoring it"),
409873ef 14875 utp->number);
d5551862 14876
ffc2605c
TT
14877 event_location_up location = string_to_event_location (&addr_str,
14878 current_language);
8cdf0e15 14879 if (!create_breakpoint (get_current_arch (),
ffc2605c 14880 location.get (),
67aa1f3c 14881 utp->cond_string.get (), -1, addr_str,
10a636cc 14882 false /* force_condition */,
e7e0cddf 14883 0 /* parse cond/thread */,
8cdf0e15 14884 0 /* tempflag */,
0fb4aa4b 14885 utp->type /* type_wanted */,
8cdf0e15
VP
14886 0 /* Ignore count */,
14887 pending_break_support,
348d480f 14888 &tracepoint_breakpoint_ops,
8cdf0e15 14889 0 /* from_tty */,
84f4c1fe 14890 utp->enabled /* enabled */,
44f238bb
PA
14891 0 /* internal */,
14892 CREATE_BREAKPOINT_FLAGS_INSERTED))
ffc2605c 14893 return NULL;
fd9b8c24 14894
409873ef 14895 /* Get the tracepoint we just created. */
fd9b8c24
PA
14896 tp = get_tracepoint (tracepoint_count);
14897 gdb_assert (tp != NULL);
d5551862 14898
00bf0b85
SS
14899 if (utp->pass > 0)
14900 {
8c042590 14901 xsnprintf (small_buf, sizeof (small_buf), "%d %d", utp->pass,
c1fc2657 14902 tp->number);
00bf0b85 14903
409873ef 14904 trace_pass_command (small_buf, 0);
00bf0b85
SS
14905 }
14906
409873ef
SS
14907 /* If we have uploaded versions of the original commands, set up a
14908 special-purpose "reader" function and call the usual command line
14909 reader, then pass the result to the breakpoint command-setting
14910 function. */
a18ba4e4 14911 if (!utp->cmd_strings.empty ())
00bf0b85 14912 {
12973681 14913 counted_command_line cmd_list;
00bf0b85 14914
409873ef 14915 this_utp = utp;
3149d8c1 14916 next_cmd = 0;
d5551862 14917
60b3cef2 14918 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL);
409873ef 14919
c1fc2657 14920 breakpoint_set_commands (tp, std::move (cmd_list));
00bf0b85 14921 }
a18ba4e4
SM
14922 else if (!utp->actions.empty ()
14923 || !utp->step_actions.empty ())
3e43a32a
MS
14924 warning (_("Uploaded tracepoint %d actions "
14925 "have no source form, ignoring them"),
409873ef 14926 utp->number);
00bf0b85 14927
f196051f 14928 /* Copy any status information that might be available. */
c1fc2657 14929 tp->hit_count = utp->hit_count;
f196051f
SS
14930 tp->traceframe_usage = utp->traceframe_usage;
14931
00bf0b85 14932 return tp;
d9b3f62e 14933}
00bf0b85 14934
1042e4c0
SS
14935/* Print information on tracepoint number TPNUM_EXP, or all if
14936 omitted. */
14937
14938static void
1d12d88f 14939info_tracepoints_command (const char *args, int from_tty)
1042e4c0 14940{
79a45e25 14941 struct ui_out *uiout = current_uiout;
e5a67952 14942 int num_printed;
1042e4c0 14943
5c458ae8 14944 num_printed = breakpoint_1 (args, false, is_tracepoint);
d77f58be
SS
14945
14946 if (num_printed == 0)
1042e4c0 14947 {
e5a67952 14948 if (args == NULL || *args == '\0')
112e8700 14949 uiout->message ("No tracepoints.\n");
d77f58be 14950 else
112e8700 14951 uiout->message ("No tracepoint matching '%s'.\n", args);
1042e4c0 14952 }
ad443146
SS
14953
14954 default_collect_info ();
1042e4c0
SS
14955}
14956
4a64f543 14957/* The 'enable trace' command enables tracepoints.
1042e4c0
SS
14958 Not supported by all targets. */
14959static void
5fed81ff 14960enable_trace_command (const char *args, int from_tty)
1042e4c0
SS
14961{
14962 enable_command (args, from_tty);
14963}
14964
4a64f543 14965/* The 'disable trace' command disables tracepoints.
1042e4c0
SS
14966 Not supported by all targets. */
14967static void
5fed81ff 14968disable_trace_command (const char *args, int from_tty)
1042e4c0
SS
14969{
14970 disable_command (args, from_tty);
14971}
14972
4a64f543 14973/* Remove a tracepoint (or all if no argument). */
1042e4c0 14974static void
4495129a 14975delete_trace_command (const char *arg, int from_tty)
1042e4c0 14976{
35df4500 14977 struct breakpoint *b, *b_tmp;
1042e4c0
SS
14978
14979 dont_repeat ();
14980
14981 if (arg == 0)
14982 {
14983 int breaks_to_delete = 0;
14984
14985 /* Delete all breakpoints if no argument.
dda83cd7
SM
14986 Do not delete internal or call-dummy breakpoints, these
14987 have to be deleted with an explicit breakpoint number
4a64f543 14988 argument. */
1042e4c0 14989 ALL_TRACEPOINTS (b)
46c6471b 14990 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0
SS
14991 {
14992 breaks_to_delete = 1;
14993 break;
14994 }
1042e4c0
SS
14995
14996 /* Ask user only if there are some breakpoints to delete. */
14997 if (!from_tty
14998 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
14999 {
35df4500 15000 ALL_BREAKPOINTS_SAFE (b, b_tmp)
46c6471b 15001 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0 15002 delete_breakpoint (b);
1042e4c0
SS
15003 }
15004 }
15005 else
48649e1b 15006 map_breakpoint_numbers
b926417a 15007 (arg, [&] (breakpoint *br)
48649e1b 15008 {
b926417a 15009 iterate_over_related_breakpoints (br, delete_breakpoint);
48649e1b 15010 });
1042e4c0
SS
15011}
15012
197f0a60
TT
15013/* Helper function for trace_pass_command. */
15014
15015static void
d9b3f62e 15016trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
197f0a60 15017{
d9b3f62e 15018 tp->pass_count = count;
76727919 15019 gdb::observers::breakpoint_modified.notify (tp);
197f0a60
TT
15020 if (from_tty)
15021 printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
c1fc2657 15022 tp->number, count);
197f0a60
TT
15023}
15024
1042e4c0
SS
15025/* Set passcount for tracepoint.
15026
15027 First command argument is passcount, second is tracepoint number.
15028 If tracepoint number omitted, apply to most recently defined.
15029 Also accepts special argument "all". */
15030
15031static void
0b39b52e 15032trace_pass_command (const char *args, int from_tty)
1042e4c0 15033{
d9b3f62e 15034 struct tracepoint *t1;
0b39b52e 15035 ULONGEST count;
1042e4c0
SS
15036
15037 if (args == 0 || *args == 0)
3e43a32a
MS
15038 error (_("passcount command requires an "
15039 "argument (count + optional TP num)"));
1042e4c0 15040
0b39b52e 15041 count = strtoulst (args, &args, 10); /* Count comes first, then TP num. */
1042e4c0 15042
529480d0 15043 args = skip_spaces (args);
1042e4c0
SS
15044 if (*args && strncasecmp (args, "all", 3) == 0)
15045 {
d9b3f62e
PA
15046 struct breakpoint *b;
15047
1042e4c0 15048 args += 3; /* Skip special argument "all". */
1042e4c0
SS
15049 if (*args)
15050 error (_("Junk at end of arguments."));
1042e4c0 15051
d9b3f62e 15052 ALL_TRACEPOINTS (b)
197f0a60 15053 {
d9b3f62e 15054 t1 = (struct tracepoint *) b;
197f0a60
TT
15055 trace_pass_set_count (t1, count, from_tty);
15056 }
15057 }
15058 else if (*args == '\0')
1042e4c0 15059 {
5fa1d40e 15060 t1 = get_tracepoint_by_number (&args, NULL);
1042e4c0 15061 if (t1)
197f0a60
TT
15062 trace_pass_set_count (t1, count, from_tty);
15063 }
15064 else
15065 {
bfd28288
PA
15066 number_or_range_parser parser (args);
15067 while (!parser.finished ())
1042e4c0 15068 {
bfd28288 15069 t1 = get_tracepoint_by_number (&args, &parser);
197f0a60
TT
15070 if (t1)
15071 trace_pass_set_count (t1, count, from_tty);
1042e4c0
SS
15072 }
15073 }
1042e4c0
SS
15074}
15075
d9b3f62e 15076struct tracepoint *
1042e4c0
SS
15077get_tracepoint (int num)
15078{
15079 struct breakpoint *t;
15080
15081 ALL_TRACEPOINTS (t)
15082 if (t->number == num)
d9b3f62e 15083 return (struct tracepoint *) t;
1042e4c0
SS
15084
15085 return NULL;
15086}
15087
d5551862
SS
15088/* Find the tracepoint with the given target-side number (which may be
15089 different from the tracepoint number after disconnecting and
15090 reconnecting). */
15091
d9b3f62e 15092struct tracepoint *
d5551862
SS
15093get_tracepoint_by_number_on_target (int num)
15094{
d9b3f62e 15095 struct breakpoint *b;
d5551862 15096
d9b3f62e
PA
15097 ALL_TRACEPOINTS (b)
15098 {
15099 struct tracepoint *t = (struct tracepoint *) b;
15100
15101 if (t->number_on_target == num)
15102 return t;
15103 }
d5551862
SS
15104
15105 return NULL;
15106}
15107
1042e4c0 15108/* Utility: parse a tracepoint number and look it up in the list.
197f0a60 15109 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
5fa1d40e
YQ
15110 If the argument is missing, the most recent tracepoint
15111 (tracepoint_count) is returned. */
15112
d9b3f62e 15113struct tracepoint *
0b39b52e 15114get_tracepoint_by_number (const char **arg,
bfd28288 15115 number_or_range_parser *parser)
1042e4c0 15116{
1042e4c0
SS
15117 struct breakpoint *t;
15118 int tpnum;
0b39b52e 15119 const char *instring = arg == NULL ? NULL : *arg;
1042e4c0 15120
bfd28288 15121 if (parser != NULL)
197f0a60 15122 {
bfd28288
PA
15123 gdb_assert (!parser->finished ());
15124 tpnum = parser->get_number ();
197f0a60
TT
15125 }
15126 else if (arg == NULL || *arg == NULL || ! **arg)
5fa1d40e 15127 tpnum = tracepoint_count;
1042e4c0 15128 else
197f0a60 15129 tpnum = get_number (arg);
1042e4c0
SS
15130
15131 if (tpnum <= 0)
15132 {
15133 if (instring && *instring)
15134 printf_filtered (_("bad tracepoint number at or near '%s'\n"),
15135 instring);
15136 else
5fa1d40e 15137 printf_filtered (_("No previous tracepoint\n"));
1042e4c0
SS
15138 return NULL;
15139 }
15140
15141 ALL_TRACEPOINTS (t)
15142 if (t->number == tpnum)
15143 {
d9b3f62e 15144 return (struct tracepoint *) t;
1042e4c0
SS
15145 }
15146
1042e4c0
SS
15147 printf_unfiltered ("No tracepoint number %d.\n", tpnum);
15148 return NULL;
15149}
15150
d9b3f62e
PA
15151void
15152print_recreate_thread (struct breakpoint *b, struct ui_file *fp)
15153{
15154 if (b->thread != -1)
15155 fprintf_unfiltered (fp, " thread %d", b->thread);
15156
15157 if (b->task != 0)
15158 fprintf_unfiltered (fp, " task %d", b->task);
15159
15160 fprintf_unfiltered (fp, "\n");
15161}
15162
6149aea9
PA
15163/* Save information on user settable breakpoints (watchpoints, etc) to
15164 a new script file named FILENAME. If FILTER is non-NULL, call it
15165 on each breakpoint and only include the ones for which it returns
f2478a7e 15166 true. */
6149aea9 15167
1042e4c0 15168static void
4495129a 15169save_breakpoints (const char *filename, int from_tty,
f2478a7e 15170 bool (*filter) (const struct breakpoint *))
1042e4c0
SS
15171{
15172 struct breakpoint *tp;
6149aea9 15173 int any = 0;
6149aea9 15174 int extra_trace_bits = 0;
1042e4c0 15175
6149aea9
PA
15176 if (filename == 0 || *filename == 0)
15177 error (_("Argument required (file name in which to save)"));
1042e4c0
SS
15178
15179 /* See if we have anything to save. */
6149aea9 15180 ALL_BREAKPOINTS (tp)
1042e4c0 15181 {
6149aea9 15182 /* Skip internal and momentary breakpoints. */
09d682a4 15183 if (!user_breakpoint_p (tp))
6149aea9
PA
15184 continue;
15185
15186 /* If we have a filter, only save the breakpoints it accepts. */
15187 if (filter && !filter (tp))
15188 continue;
15189
15190 any = 1;
15191
15192 if (is_tracepoint (tp))
15193 {
15194 extra_trace_bits = 1;
15195
15196 /* We can stop searching. */
15197 break;
15198 }
1042e4c0 15199 }
6149aea9
PA
15200
15201 if (!any)
1042e4c0 15202 {
6149aea9 15203 warning (_("Nothing to save."));
1042e4c0
SS
15204 return;
15205 }
15206
ee0c3293 15207 gdb::unique_xmalloc_ptr<char> expanded_filename (tilde_expand (filename));
d7e74731
PA
15208
15209 stdio_file fp;
15210
ee0c3293 15211 if (!fp.open (expanded_filename.get (), "w"))
6149aea9 15212 error (_("Unable to open file '%s' for saving (%s)"),
ee0c3293 15213 expanded_filename.get (), safe_strerror (errno));
8bf6485c 15214
6149aea9 15215 if (extra_trace_bits)
d7e74731 15216 save_trace_state_variables (&fp);
8bf6485c 15217
6149aea9 15218 ALL_BREAKPOINTS (tp)
1042e4c0 15219 {
6149aea9 15220 /* Skip internal and momentary breakpoints. */
09d682a4 15221 if (!user_breakpoint_p (tp))
6149aea9 15222 continue;
8bf6485c 15223
6149aea9
PA
15224 /* If we have a filter, only save the breakpoints it accepts. */
15225 if (filter && !filter (tp))
15226 continue;
15227
d7e74731 15228 tp->ops->print_recreate (tp, &fp);
1042e4c0 15229
6149aea9
PA
15230 /* Note, we can't rely on tp->number for anything, as we can't
15231 assume the recreated breakpoint numbers will match. Use $bpnum
15232 instead. */
15233
15234 if (tp->cond_string)
d7e74731 15235 fp.printf (" condition $bpnum %s\n", tp->cond_string);
6149aea9
PA
15236
15237 if (tp->ignore_count)
d7e74731 15238 fp.printf (" ignore $bpnum %d\n", tp->ignore_count);
6149aea9 15239
2d9442cc 15240 if (tp->type != bp_dprintf && tp->commands)
1042e4c0 15241 {
d7e74731 15242 fp.puts (" commands\n");
a7bdde9e 15243
d7e74731 15244 current_uiout->redirect (&fp);
a70b8144 15245 try
1042e4c0 15246 {
d1b0a7bf 15247 print_command_lines (current_uiout, tp->commands.get (), 2);
a7bdde9e 15248 }
230d2906 15249 catch (const gdb_exception &ex)
492d29ea 15250 {
112e8700 15251 current_uiout->redirect (NULL);
eedc3f4f 15252 throw;
492d29ea 15253 }
1042e4c0 15254
112e8700 15255 current_uiout->redirect (NULL);
d7e74731 15256 fp.puts (" end\n");
1042e4c0 15257 }
6149aea9
PA
15258
15259 if (tp->enable_state == bp_disabled)
d7e74731 15260 fp.puts ("disable $bpnum\n");
6149aea9
PA
15261
15262 /* If this is a multi-location breakpoint, check if the locations
15263 should be individually disabled. Watchpoint locations are
15264 special, and not user visible. */
15265 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
15266 {
15267 struct bp_location *loc;
15268 int n = 1;
15269
15270 for (loc = tp->loc; loc != NULL; loc = loc->next, n++)
15271 if (!loc->enabled)
d7e74731 15272 fp.printf ("disable $bpnum.%d\n", n);
6149aea9 15273 }
1042e4c0 15274 }
8bf6485c 15275
6149aea9 15276 if (extra_trace_bits && *default_collect)
d7e74731 15277 fp.printf ("set default-collect %s\n", default_collect);
8bf6485c 15278
1042e4c0 15279 if (from_tty)
ee0c3293 15280 printf_filtered (_("Saved to file '%s'.\n"), expanded_filename.get ());
6149aea9
PA
15281}
15282
15283/* The `save breakpoints' command. */
15284
15285static void
4495129a 15286save_breakpoints_command (const char *args, int from_tty)
6149aea9
PA
15287{
15288 save_breakpoints (args, from_tty, NULL);
15289}
15290
15291/* The `save tracepoints' command. */
15292
15293static void
4495129a 15294save_tracepoints_command (const char *args, int from_tty)
6149aea9
PA
15295{
15296 save_breakpoints (args, from_tty, is_tracepoint);
1042e4c0
SS
15297}
15298
15299/* Create a vector of all tracepoints. */
15300
f51e0e20 15301std::vector<breakpoint *>
eeae04df 15302all_tracepoints (void)
1042e4c0 15303{
f51e0e20 15304 std::vector<breakpoint *> tp_vec;
1042e4c0
SS
15305 struct breakpoint *tp;
15306
15307 ALL_TRACEPOINTS (tp)
15308 {
f51e0e20 15309 tp_vec.push_back (tp);
1042e4c0
SS
15310 }
15311
15312 return tp_vec;
15313}
15314
c906108c 15315\f
629500fa
KS
15316/* This help string is used to consolidate all the help string for specifying
15317 locations used by several commands. */
15318
15319#define LOCATION_HELP_STRING \
15320"Linespecs are colon-separated lists of location parameters, such as\n\
15321source filename, function name, label name, and line number.\n\
15322Example: To specify the start of a label named \"the_top\" in the\n\
15323function \"fact\" in the file \"factorial.c\", use\n\
15324\"factorial.c:fact:the_top\".\n\
15325\n\
15326Address locations begin with \"*\" and specify an exact address in the\n\
15327program. Example: To specify the fourth byte past the start function\n\
15328\"main\", use \"*main + 4\".\n\
15329\n\
15330Explicit locations are similar to linespecs but use an option/argument\n\
15331syntax to specify location parameters.\n\
15332Example: To specify the start of the label named \"the_top\" in the\n\
15333function \"fact\" in the file \"factorial.c\", use \"-source factorial.c\n\
a20714ff
PA
15334-function fact -label the_top\".\n\
15335\n\
15336By default, a specified function is matched against the program's\n\
15337functions in all scopes. For C++, this means in all namespaces and\n\
15338classes. For Ada, this means in all packages. E.g., in C++,\n\
15339\"func()\" matches \"A::func()\", \"A::B::func()\", etc. The\n\
15340\"-qualified\" flag overrides this behavior, making GDB interpret the\n\
89549d7f 15341specified name as a complete fully-qualified name instead."
629500fa 15342
4a64f543
MS
15343/* This help string is used for the break, hbreak, tbreak and thbreak
15344 commands. It is defined as a macro to prevent duplication.
15345 COMMAND should be a string constant containing the name of the
15346 command. */
629500fa 15347
31e2b00f 15348#define BREAK_ARGS_HELP(command) \
733d554a
TBA
15349command" [PROBE_MODIFIER] [LOCATION] [thread THREADNUM]\n\
15350\t[-force-condition] [if CONDITION]\n\
fb7b5af4
SDJ
15351PROBE_MODIFIER shall be present if the command is to be placed in a\n\
15352probe point. Accepted values are `-probe' (for a generic, automatically\n\
d4777acb
JM
15353guessed probe type), `-probe-stap' (for a SystemTap probe) or \n\
15354`-probe-dtrace' (for a DTrace probe).\n\
629500fa
KS
15355LOCATION may be a linespec, address, or explicit location as described\n\
15356below.\n\
15357\n\
dc10affe
PA
15358With no LOCATION, uses current execution address of the selected\n\
15359stack frame. This is useful for breaking on return to a stack frame.\n\
31e2b00f
AS
15360\n\
15361THREADNUM is the number from \"info threads\".\n\
15362CONDITION is a boolean expression.\n\
733d554a
TBA
15363\n\
15364With the \"-force-condition\" flag, the condition is defined even when\n\
15365it is invalid for all current locations.\n\
89549d7f 15366\n" LOCATION_HELP_STRING "\n\n\
d41c0fc8
PA
15367Multiple breakpoints at one place are permitted, and useful if their\n\
15368conditions are different.\n\
31e2b00f
AS
15369\n\
15370Do \"help breakpoints\" for info on other commands dealing with breakpoints."
15371
44feb3ce
TT
15372/* List of subcommands for "catch". */
15373static struct cmd_list_element *catch_cmdlist;
15374
15375/* List of subcommands for "tcatch". */
15376static struct cmd_list_element *tcatch_cmdlist;
15377
9ac4176b 15378void
a121b7c1 15379add_catch_command (const char *name, const char *docstring,
eb4c3f4a 15380 cmd_const_sfunc_ftype *sfunc,
625e8578 15381 completer_ftype *completer,
44feb3ce
TT
15382 void *user_data_catch,
15383 void *user_data_tcatch)
15384{
15385 struct cmd_list_element *command;
15386
0450cc4c 15387 command = add_cmd (name, class_breakpoint, docstring,
44feb3ce
TT
15388 &catch_cmdlist);
15389 set_cmd_sfunc (command, sfunc);
15390 set_cmd_context (command, user_data_catch);
a96d9b2e 15391 set_cmd_completer (command, completer);
44feb3ce 15392
0450cc4c 15393 command = add_cmd (name, class_breakpoint, docstring,
44feb3ce
TT
15394 &tcatch_cmdlist);
15395 set_cmd_sfunc (command, sfunc);
15396 set_cmd_context (command, user_data_tcatch);
a96d9b2e 15397 set_cmd_completer (command, completer);
44feb3ce
TT
15398}
15399
84f4c1fe 15400struct breakpoint *
95da600f 15401iterate_over_breakpoints (gdb::function_view<bool (breakpoint *)> callback)
84f4c1fe 15402{
35df4500 15403 struct breakpoint *b, *b_tmp;
84f4c1fe 15404
35df4500 15405 ALL_BREAKPOINTS_SAFE (b, b_tmp)
84f4c1fe 15406 {
95da600f 15407 if (callback (b))
84f4c1fe
PM
15408 return b;
15409 }
15410
15411 return NULL;
15412}
15413
0574c78f
GB
15414/* Zero if any of the breakpoint's locations could be a location where
15415 functions have been inlined, nonzero otherwise. */
15416
15417static int
15418is_non_inline_function (struct breakpoint *b)
15419{
15420 /* The shared library event breakpoint is set on the address of a
15421 non-inline function. */
15422 if (b->type == bp_shlib_event)
15423 return 1;
15424
15425 return 0;
15426}
15427
15428/* Nonzero if the specified PC cannot be a location where functions
15429 have been inlined. */
15430
15431int
accd0bcd 15432pc_at_non_inline_function (const address_space *aspace, CORE_ADDR pc,
09ac7c10 15433 const struct target_waitstatus *ws)
0574c78f
GB
15434{
15435 struct breakpoint *b;
15436 struct bp_location *bl;
15437
15438 ALL_BREAKPOINTS (b)
15439 {
15440 if (!is_non_inline_function (b))
15441 continue;
15442
15443 for (bl = b->loc; bl != NULL; bl = bl->next)
15444 {
15445 if (!bl->shlib_disabled
09ac7c10 15446 && bpstat_check_location (bl, aspace, pc, ws))
0574c78f
GB
15447 return 1;
15448 }
15449 }
15450
15451 return 0;
15452}
15453
2f202fde
JK
15454/* Remove any references to OBJFILE which is going to be freed. */
15455
15456void
15457breakpoint_free_objfile (struct objfile *objfile)
15458{
15459 struct bp_location **locp, *loc;
15460
15461 ALL_BP_LOCATIONS (loc, locp)
eb822aa6 15462 if (loc->symtab != NULL && SYMTAB_OBJFILE (loc->symtab) == objfile)
2f202fde
JK
15463 loc->symtab = NULL;
15464}
15465
2060206e
PA
15466void
15467initialize_breakpoint_ops (void)
15468{
15469 static int initialized = 0;
15470
15471 struct breakpoint_ops *ops;
15472
15473 if (initialized)
15474 return;
15475 initialized = 1;
15476
15477 /* The breakpoint_ops structure to be inherit by all kinds of
15478 breakpoints (real breakpoints, i.e., user "break" breakpoints,
15479 internal and momentary breakpoints, etc.). */
15480 ops = &bkpt_base_breakpoint_ops;
15481 *ops = base_breakpoint_ops;
15482 ops->re_set = bkpt_re_set;
15483 ops->insert_location = bkpt_insert_location;
15484 ops->remove_location = bkpt_remove_location;
15485 ops->breakpoint_hit = bkpt_breakpoint_hit;
5f700d83 15486 ops->create_sals_from_location = bkpt_create_sals_from_location;
983af33b 15487 ops->create_breakpoints_sal = bkpt_create_breakpoints_sal;
5f700d83 15488 ops->decode_location = bkpt_decode_location;
2060206e
PA
15489
15490 /* The breakpoint_ops structure to be used in regular breakpoints. */
15491 ops = &bkpt_breakpoint_ops;
15492 *ops = bkpt_base_breakpoint_ops;
15493 ops->re_set = bkpt_re_set;
15494 ops->resources_needed = bkpt_resources_needed;
15495 ops->print_it = bkpt_print_it;
15496 ops->print_mention = bkpt_print_mention;
15497 ops->print_recreate = bkpt_print_recreate;
15498
15499 /* Ranged breakpoints. */
15500 ops = &ranged_breakpoint_ops;
15501 *ops = bkpt_breakpoint_ops;
15502 ops->breakpoint_hit = breakpoint_hit_ranged_breakpoint;
15503 ops->resources_needed = resources_needed_ranged_breakpoint;
15504 ops->print_it = print_it_ranged_breakpoint;
15505 ops->print_one = print_one_ranged_breakpoint;
15506 ops->print_one_detail = print_one_detail_ranged_breakpoint;
15507 ops->print_mention = print_mention_ranged_breakpoint;
15508 ops->print_recreate = print_recreate_ranged_breakpoint;
15509
15510 /* Internal breakpoints. */
15511 ops = &internal_breakpoint_ops;
15512 *ops = bkpt_base_breakpoint_ops;
15513 ops->re_set = internal_bkpt_re_set;
15514 ops->check_status = internal_bkpt_check_status;
15515 ops->print_it = internal_bkpt_print_it;
15516 ops->print_mention = internal_bkpt_print_mention;
15517
15518 /* Momentary breakpoints. */
15519 ops = &momentary_breakpoint_ops;
15520 *ops = bkpt_base_breakpoint_ops;
15521 ops->re_set = momentary_bkpt_re_set;
15522 ops->check_status = momentary_bkpt_check_status;
15523 ops->print_it = momentary_bkpt_print_it;
15524 ops->print_mention = momentary_bkpt_print_mention;
15525
55aa24fb
SDJ
15526 /* Probe breakpoints. */
15527 ops = &bkpt_probe_breakpoint_ops;
15528 *ops = bkpt_breakpoint_ops;
15529 ops->insert_location = bkpt_probe_insert_location;
15530 ops->remove_location = bkpt_probe_remove_location;
5f700d83
KS
15531 ops->create_sals_from_location = bkpt_probe_create_sals_from_location;
15532 ops->decode_location = bkpt_probe_decode_location;
55aa24fb 15533
2060206e
PA
15534 /* Watchpoints. */
15535 ops = &watchpoint_breakpoint_ops;
15536 *ops = base_breakpoint_ops;
15537 ops->re_set = re_set_watchpoint;
15538 ops->insert_location = insert_watchpoint;
15539 ops->remove_location = remove_watchpoint;
15540 ops->breakpoint_hit = breakpoint_hit_watchpoint;
15541 ops->check_status = check_status_watchpoint;
15542 ops->resources_needed = resources_needed_watchpoint;
15543 ops->works_in_software_mode = works_in_software_mode_watchpoint;
15544 ops->print_it = print_it_watchpoint;
15545 ops->print_mention = print_mention_watchpoint;
15546 ops->print_recreate = print_recreate_watchpoint;
427cd150 15547 ops->explains_signal = explains_signal_watchpoint;
2060206e
PA
15548
15549 /* Masked watchpoints. */
15550 ops = &masked_watchpoint_breakpoint_ops;
15551 *ops = watchpoint_breakpoint_ops;
15552 ops->insert_location = insert_masked_watchpoint;
15553 ops->remove_location = remove_masked_watchpoint;
15554 ops->resources_needed = resources_needed_masked_watchpoint;
15555 ops->works_in_software_mode = works_in_software_mode_masked_watchpoint;
15556 ops->print_it = print_it_masked_watchpoint;
15557 ops->print_one_detail = print_one_detail_masked_watchpoint;
15558 ops->print_mention = print_mention_masked_watchpoint;
15559 ops->print_recreate = print_recreate_masked_watchpoint;
15560
15561 /* Tracepoints. */
15562 ops = &tracepoint_breakpoint_ops;
15563 *ops = base_breakpoint_ops;
15564 ops->re_set = tracepoint_re_set;
15565 ops->breakpoint_hit = tracepoint_breakpoint_hit;
15566 ops->print_one_detail = tracepoint_print_one_detail;
15567 ops->print_mention = tracepoint_print_mention;
15568 ops->print_recreate = tracepoint_print_recreate;
5f700d83 15569 ops->create_sals_from_location = tracepoint_create_sals_from_location;
983af33b 15570 ops->create_breakpoints_sal = tracepoint_create_breakpoints_sal;
5f700d83 15571 ops->decode_location = tracepoint_decode_location;
983af33b 15572
55aa24fb
SDJ
15573 /* Probe tracepoints. */
15574 ops = &tracepoint_probe_breakpoint_ops;
15575 *ops = tracepoint_breakpoint_ops;
5f700d83
KS
15576 ops->create_sals_from_location = tracepoint_probe_create_sals_from_location;
15577 ops->decode_location = tracepoint_probe_decode_location;
55aa24fb 15578
983af33b
SDJ
15579 /* Static tracepoints with marker (`-m'). */
15580 ops = &strace_marker_breakpoint_ops;
15581 *ops = tracepoint_breakpoint_ops;
5f700d83 15582 ops->create_sals_from_location = strace_marker_create_sals_from_location;
983af33b 15583 ops->create_breakpoints_sal = strace_marker_create_breakpoints_sal;
5f700d83 15584 ops->decode_location = strace_marker_decode_location;
2060206e
PA
15585
15586 /* Fork catchpoints. */
15587 ops = &catch_fork_breakpoint_ops;
15588 *ops = base_breakpoint_ops;
15589 ops->insert_location = insert_catch_fork;
15590 ops->remove_location = remove_catch_fork;
15591 ops->breakpoint_hit = breakpoint_hit_catch_fork;
15592 ops->print_it = print_it_catch_fork;
15593 ops->print_one = print_one_catch_fork;
15594 ops->print_mention = print_mention_catch_fork;
15595 ops->print_recreate = print_recreate_catch_fork;
15596
15597 /* Vfork catchpoints. */
15598 ops = &catch_vfork_breakpoint_ops;
15599 *ops = base_breakpoint_ops;
15600 ops->insert_location = insert_catch_vfork;
15601 ops->remove_location = remove_catch_vfork;
15602 ops->breakpoint_hit = breakpoint_hit_catch_vfork;
15603 ops->print_it = print_it_catch_vfork;
15604 ops->print_one = print_one_catch_vfork;
15605 ops->print_mention = print_mention_catch_vfork;
15606 ops->print_recreate = print_recreate_catch_vfork;
15607
15608 /* Exec catchpoints. */
15609 ops = &catch_exec_breakpoint_ops;
15610 *ops = base_breakpoint_ops;
2060206e
PA
15611 ops->insert_location = insert_catch_exec;
15612 ops->remove_location = remove_catch_exec;
15613 ops->breakpoint_hit = breakpoint_hit_catch_exec;
15614 ops->print_it = print_it_catch_exec;
15615 ops->print_one = print_one_catch_exec;
15616 ops->print_mention = print_mention_catch_exec;
15617 ops->print_recreate = print_recreate_catch_exec;
15618
edcc5120
TT
15619 /* Solib-related catchpoints. */
15620 ops = &catch_solib_breakpoint_ops;
15621 *ops = base_breakpoint_ops;
edcc5120
TT
15622 ops->insert_location = insert_catch_solib;
15623 ops->remove_location = remove_catch_solib;
15624 ops->breakpoint_hit = breakpoint_hit_catch_solib;
15625 ops->check_status = check_status_catch_solib;
15626 ops->print_it = print_it_catch_solib;
15627 ops->print_one = print_one_catch_solib;
15628 ops->print_mention = print_mention_catch_solib;
15629 ops->print_recreate = print_recreate_catch_solib;
e7e0cddf
SS
15630
15631 ops = &dprintf_breakpoint_ops;
15632 *ops = bkpt_base_breakpoint_ops;
5c2b4418 15633 ops->re_set = dprintf_re_set;
e7e0cddf
SS
15634 ops->resources_needed = bkpt_resources_needed;
15635 ops->print_it = bkpt_print_it;
15636 ops->print_mention = bkpt_print_mention;
2d9442cc 15637 ops->print_recreate = dprintf_print_recreate;
9d6e6e84 15638 ops->after_condition_true = dprintf_after_condition_true;
cd1608cc 15639 ops->breakpoint_hit = dprintf_breakpoint_hit;
2060206e
PA
15640}
15641
8bfd80db
YQ
15642/* Chain containing all defined "enable breakpoint" subcommands. */
15643
15644static struct cmd_list_element *enablebreaklist = NULL;
15645
8588b356
SM
15646/* See breakpoint.h. */
15647
15648cmd_list_element *commands_cmd_element = nullptr;
15649
6c265988 15650void _initialize_breakpoint ();
c906108c 15651void
6c265988 15652_initialize_breakpoint ()
c906108c
SS
15653{
15654 struct cmd_list_element *c;
15655
2060206e
PA
15656 initialize_breakpoint_ops ();
15657
c90e7d63
SM
15658 gdb::observers::solib_unloaded.attach (disable_breakpoints_in_unloaded_shlib,
15659 "breakpoint");
15660 gdb::observers::free_objfile.attach (disable_breakpoints_in_freed_objfile,
15661 "breakpoint");
15662 gdb::observers::memory_changed.attach (invalidate_bp_value_on_memory_change,
15663 "breakpoint");
84acb35a 15664
c906108c
SS
15665 breakpoint_chain = 0;
15666 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
15667 before a breakpoint is set. */
15668 breakpoint_count = 0;
15669
1042e4c0
SS
15670 tracepoint_count = 0;
15671
1bedd215
AC
15672 add_com ("ignore", class_breakpoint, ignore_command, _("\
15673Set ignore-count of breakpoint number N to COUNT.\n\
15674Usage is `ignore N COUNT'."));
c906108c 15675
8588b356
SM
15676 commands_cmd_element = add_com ("commands", class_breakpoint,
15677 commands_command, _("\
18da0c51
MG
15678Set commands to be executed when the given breakpoints are hit.\n\
15679Give a space-separated breakpoint list as argument after \"commands\".\n\
15680A list element can be a breakpoint number (e.g. `5') or a range of numbers\n\
15681(e.g. `5-7').\n\
c906108c
SS
15682With no argument, the targeted breakpoint is the last one set.\n\
15683The commands themselves follow starting on the next line.\n\
15684Type a line containing \"end\" to indicate the end of them.\n\
15685Give \"silent\" as the first line to make the breakpoint silent;\n\
1bedd215 15686then no output is printed when it is hit, except what the commands print."));
c906108c 15687
b1d4d8d1
TBA
15688 const auto cc_opts = make_condition_command_options_def_group (nullptr);
15689 static std::string condition_command_help
15690 = gdb::option::build_help (_("\
1bedd215 15691Specify breakpoint number N to break only if COND is true.\n\
b1d4d8d1 15692Usage is `condition [OPTION] N COND', where N is an integer and COND\n\
733d554a 15693is an expression to be evaluated whenever breakpoint N is reached.\n\
b1d4d8d1
TBA
15694\n\
15695Options:\n\
15696%OPTIONS%"), cc_opts);
15697
15698 c = add_com ("condition", class_breakpoint, condition_command,
15699 condition_command_help.c_str ());
15700 set_cmd_completer_handle_brkchars (c, condition_completer);
c906108c 15701
1bedd215 15702 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
31e2b00f 15703Set a temporary breakpoint.\n\
c906108c
SS
15704Like \"break\" except the breakpoint is only temporary,\n\
15705so it will be deleted when hit. Equivalent to \"break\" followed\n\
31e2b00f
AS
15706by using \"enable delete\" on the breakpoint number.\n\
15707\n"
15708BREAK_ARGS_HELP ("tbreak")));
5ba2abeb 15709 set_cmd_completer (c, location_completer);
c94fdfd0 15710
1bedd215 15711 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
a3be7890 15712Set a hardware assisted breakpoint.\n\
c906108c 15713Like \"break\" except the breakpoint requires hardware support,\n\
31e2b00f
AS
15714some target hardware may not have this support.\n\
15715\n"
15716BREAK_ARGS_HELP ("hbreak")));
5ba2abeb 15717 set_cmd_completer (c, location_completer);
c906108c 15718
1bedd215 15719 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
31e2b00f 15720Set a temporary hardware assisted breakpoint.\n\
c906108c 15721Like \"hbreak\" except the breakpoint is only temporary,\n\
31e2b00f
AS
15722so it will be deleted when hit.\n\
15723\n"
15724BREAK_ARGS_HELP ("thbreak")));
5ba2abeb 15725 set_cmd_completer (c, location_completer);
c906108c 15726
1bedd215 15727 add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
590042fc
PW
15728Enable all or some breakpoints.\n\
15729Usage: enable [BREAKPOINTNUM]...\n\
c906108c
SS
15730Give breakpoint numbers (separated by spaces) as arguments.\n\
15731With no subcommand, breakpoints are enabled until you command otherwise.\n\
15732This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 15733With a subcommand you can enable temporarily."),
c906108c 15734 &enablelist, "enable ", 1, &cmdlist);
c906108c
SS
15735
15736 add_com_alias ("en", "enable", class_breakpoint, 1);
15737
84951ab5 15738 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
590042fc
PW
15739Enable all or some breakpoints.\n\
15740Usage: enable breakpoints [BREAKPOINTNUM]...\n\
c906108c
SS
15741Give breakpoint numbers (separated by spaces) as arguments.\n\
15742This is used to cancel the effect of the \"disable\" command.\n\
89549d7f 15743May be abbreviated to simply \"enable\"."),
c5aa993b 15744 &enablebreaklist, "enable breakpoints ", 1, &enablelist);
c906108c 15745
1a966eab 15746 add_cmd ("once", no_class, enable_once_command, _("\
590042fc
PW
15747Enable some breakpoints for one hit.\n\
15748Usage: enable breakpoints once BREAKPOINTNUM...\n\
1a966eab 15749If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
c906108c
SS
15750 &enablebreaklist);
15751
1a966eab 15752 add_cmd ("delete", no_class, enable_delete_command, _("\
590042fc
PW
15753Enable some breakpoints and delete when hit.\n\
15754Usage: enable breakpoints delete BREAKPOINTNUM...\n\
1a966eab 15755If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
15756 &enablebreaklist);
15757
816338b5 15758 add_cmd ("count", no_class, enable_count_command, _("\
590042fc
PW
15759Enable some breakpoints for COUNT hits.\n\
15760Usage: enable breakpoints count COUNT BREAKPOINTNUM...\n\
816338b5
SS
15761If a breakpoint is hit while enabled in this fashion,\n\
15762the count is decremented; when it reaches zero, the breakpoint is disabled."),
15763 &enablebreaklist);
15764
1a966eab 15765 add_cmd ("delete", no_class, enable_delete_command, _("\
590042fc
PW
15766Enable some breakpoints and delete when hit.\n\
15767Usage: enable delete BREAKPOINTNUM...\n\
1a966eab 15768If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
15769 &enablelist);
15770
1a966eab 15771 add_cmd ("once", no_class, enable_once_command, _("\
590042fc
PW
15772Enable some breakpoints for one hit.\n\
15773Usage: enable once BREAKPOINTNUM...\n\
1a966eab 15774If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
816338b5
SS
15775 &enablelist);
15776
15777 add_cmd ("count", no_class, enable_count_command, _("\
590042fc
PW
15778Enable some breakpoints for COUNT hits.\n\
15779Usage: enable count COUNT BREAKPOINTNUM...\n\
816338b5
SS
15780If a breakpoint is hit while enabled in this fashion,\n\
15781the count is decremented; when it reaches zero, the breakpoint is disabled."),
c906108c
SS
15782 &enablelist);
15783
1bedd215 15784 add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
590042fc
PW
15785Disable all or some breakpoints.\n\
15786Usage: disable [BREAKPOINTNUM]...\n\
c906108c
SS
15787Arguments are breakpoint numbers with spaces in between.\n\
15788To disable all breakpoints, give no argument.\n\
64b9b334 15789A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
c906108c
SS
15790 &disablelist, "disable ", 1, &cmdlist);
15791 add_com_alias ("dis", "disable", class_breakpoint, 1);
15792 add_com_alias ("disa", "disable", class_breakpoint, 1);
c906108c 15793
57b4f16e 15794 add_cmd ("breakpoints", class_breakpoint, disable_command, _("\
590042fc
PW
15795Disable all or some breakpoints.\n\
15796Usage: disable breakpoints [BREAKPOINTNUM]...\n\
c906108c
SS
15797Arguments are breakpoint numbers with spaces in between.\n\
15798To disable all breakpoints, give no argument.\n\
64b9b334 15799A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
1a966eab 15800This command may be abbreviated \"disable\"."),
c906108c
SS
15801 &disablelist);
15802
1bedd215 15803 add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
590042fc
PW
15804Delete all or some breakpoints.\n\
15805Usage: delete [BREAKPOINTNUM]...\n\
c906108c
SS
15806Arguments are breakpoint numbers with spaces in between.\n\
15807To delete all breakpoints, give no argument.\n\
15808\n\
590042fc 15809Also a prefix command for deletion of other GDB objects."),
c906108c
SS
15810 &deletelist, "delete ", 1, &cmdlist);
15811 add_com_alias ("d", "delete", class_breakpoint, 1);
7f198e01 15812 add_com_alias ("del", "delete", class_breakpoint, 1);
c906108c 15813
57b4f16e 15814 add_cmd ("breakpoints", class_breakpoint, delete_command, _("\
590042fc
PW
15815Delete all or some breakpoints or auto-display expressions.\n\
15816Usage: delete breakpoints [BREAKPOINTNUM]...\n\
c906108c
SS
15817Arguments are breakpoint numbers with spaces in between.\n\
15818To delete all breakpoints, give no argument.\n\
1a966eab 15819This command may be abbreviated \"delete\"."),
c906108c
SS
15820 &deletelist);
15821
1bedd215 15822 add_com ("clear", class_breakpoint, clear_command, _("\
629500fa
KS
15823Clear breakpoint at specified location.\n\
15824Argument may be a linespec, explicit, or address location as described below.\n\
1bedd215
AC
15825\n\
15826With no argument, clears all breakpoints in the line that the selected frame\n\
629500fa 15827is executing in.\n"
89549d7f 15828"\n" LOCATION_HELP_STRING "\n\n\
1bedd215 15829See also the \"delete\" command which clears breakpoints by number."));
a6cc4789 15830 add_com_alias ("cl", "clear", class_breakpoint, 1);
c906108c 15831
1bedd215 15832 c = add_com ("break", class_breakpoint, break_command, _("\
629500fa 15833Set breakpoint at specified location.\n"
31e2b00f 15834BREAK_ARGS_HELP ("break")));
5ba2abeb 15835 set_cmd_completer (c, location_completer);
c94fdfd0 15836
c906108c
SS
15837 add_com_alias ("b", "break", class_run, 1);
15838 add_com_alias ("br", "break", class_run, 1);
15839 add_com_alias ("bre", "break", class_run, 1);
15840 add_com_alias ("brea", "break", class_run, 1);
15841
c906108c
SS
15842 if (dbx_commands)
15843 {
1bedd215
AC
15844 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
15845Break in function/address or break at a line in the current file."),
c5aa993b
JM
15846 &stoplist, "stop ", 1, &cmdlist);
15847 add_cmd ("in", class_breakpoint, stopin_command,
1a966eab 15848 _("Break in function or address."), &stoplist);
c5aa993b 15849 add_cmd ("at", class_breakpoint, stopat_command,
1a966eab 15850 _("Break at a line in the current file."), &stoplist);
11db9430 15851 add_com ("status", class_info, info_breakpoints_command, _("\
1bedd215 15852Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
15853The \"Type\" column indicates one of:\n\
15854\tbreakpoint - normal breakpoint\n\
15855\twatchpoint - watchpoint\n\
15856The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15857the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15858breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
15859address and file/line number respectively.\n\
15860\n\
15861Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
15862are set to the address of the last breakpoint listed unless the command\n\
15863is prefixed with \"server \".\n\n\
c906108c 15864Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 15865breakpoint set."));
c906108c
SS
15866 }
15867
11db9430 15868 add_info ("breakpoints", info_breakpoints_command, _("\
e5a67952 15869Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
c906108c
SS
15870The \"Type\" column indicates one of:\n\
15871\tbreakpoint - normal breakpoint\n\
15872\twatchpoint - watchpoint\n\
15873The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15874the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15875breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
15876address and file/line number respectively.\n\
15877\n\
15878Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
15879are set to the address of the last breakpoint listed unless the command\n\
15880is prefixed with \"server \".\n\n\
c906108c 15881Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 15882breakpoint set."));
c906108c 15883
6b04bdb7
MS
15884 add_info_alias ("b", "breakpoints", 1);
15885
1a966eab
AC
15886 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
15887Status of all breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
15888The \"Type\" column indicates one of:\n\
15889\tbreakpoint - normal breakpoint\n\
15890\twatchpoint - watchpoint\n\
15891\tlongjmp - internal breakpoint used to step through longjmp()\n\
15892\tlongjmp resume - internal breakpoint at the target of longjmp()\n\
15893\tuntil - internal breakpoint used by the \"until\" command\n\
1a966eab
AC
15894\tfinish - internal breakpoint used by the \"finish\" command\n\
15895The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
c906108c
SS
15896the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15897breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1a966eab
AC
15898address and file/line number respectively.\n\
15899\n\
15900Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
15901are set to the address of the last breakpoint listed unless the command\n\
15902is prefixed with \"server \".\n\n\
c906108c 15903Convenience variable \"$bpnum\" contains the number of the last\n\
1a966eab 15904breakpoint set."),
c906108c
SS
15905 &maintenanceinfolist);
15906
3b6acaee 15907 add_basic_prefix_cmd ("catch", class_breakpoint, _("\
44feb3ce 15908Set catchpoints to catch events."),
3b6acaee
TT
15909 &catch_cmdlist, "catch ",
15910 0/*allow-unknown*/, &cmdlist);
44feb3ce 15911
3b6acaee 15912 add_basic_prefix_cmd ("tcatch", class_breakpoint, _("\
44feb3ce 15913Set temporary catchpoints to catch events."),
3b6acaee
TT
15914 &tcatch_cmdlist, "tcatch ",
15915 0/*allow-unknown*/, &cmdlist);
44feb3ce 15916
44feb3ce
TT
15917 add_catch_command ("fork", _("Catch calls to fork."),
15918 catch_fork_command_1,
dda83cd7 15919 NULL,
44feb3ce
TT
15920 (void *) (uintptr_t) catch_fork_permanent,
15921 (void *) (uintptr_t) catch_fork_temporary);
15922 add_catch_command ("vfork", _("Catch calls to vfork."),
15923 catch_fork_command_1,
dda83cd7 15924 NULL,
44feb3ce
TT
15925 (void *) (uintptr_t) catch_vfork_permanent,
15926 (void *) (uintptr_t) catch_vfork_temporary);
15927 add_catch_command ("exec", _("Catch calls to exec."),
15928 catch_exec_command_1,
dda83cd7 15929 NULL,
a96d9b2e
SDJ
15930 CATCH_PERMANENT,
15931 CATCH_TEMPORARY);
edcc5120
TT
15932 add_catch_command ("load", _("Catch loads of shared libraries.\n\
15933Usage: catch load [REGEX]\n\
15934If REGEX is given, only stop for libraries matching the regular expression."),
15935 catch_load_command_1,
15936 NULL,
15937 CATCH_PERMANENT,
15938 CATCH_TEMPORARY);
15939 add_catch_command ("unload", _("Catch unloads of shared libraries.\n\
15940Usage: catch unload [REGEX]\n\
15941If REGEX is given, only stop for libraries matching the regular expression."),
15942 catch_unload_command_1,
15943 NULL,
15944 CATCH_PERMANENT,
15945 CATCH_TEMPORARY);
c5aa993b 15946
a15a5258
AB
15947 const auto opts = make_watch_options_def_group (nullptr);
15948
15949 static const std::string watch_help = gdb::option::build_help (_("\
15950Set a watchpoint for EXPRESSION.\n\
15951Usage: watch [-location] EXPRESSION\n\
15952\n\
15953Options:\n\
15954%OPTIONS%\n\
15955\n\
c906108c 15956A watchpoint stops execution of your program whenever the value of\n\
a15a5258
AB
15957an expression changes."), opts);
15958 c = add_com ("watch", class_breakpoint, watch_command,
15959 watch_help.c_str ());
15960 set_cmd_completer_handle_brkchars (c, watch_command_completer);
15961
15962 static const std::string rwatch_help = gdb::option::build_help (_("\
15963Set a read watchpoint for EXPRESSION.\n\
15964Usage: rwatch [-location] EXPRESSION\n\
15965\n\
15966Options:\n\
15967%OPTIONS%\n\
15968\n\
15969A read watchpoint stops execution of your program whenever the value of\n\
15970an expression is read."), opts);
15971 c = add_com ("rwatch", class_breakpoint, rwatch_command,
15972 rwatch_help.c_str ());
15973 set_cmd_completer_handle_brkchars (c, watch_command_completer);
15974
15975 static const std::string awatch_help = gdb::option::build_help (_("\
15976Set an access watchpoint for EXPRESSION.\n\
15977Usage: awatch [-location] EXPRESSION\n\
15978\n\
15979Options:\n\
15980%OPTIONS%\n\
15981\n\
15982An access watchpoint stops execution of your program whenever the value\n\
15983of an expression is either read or written."), opts);
15984 c = add_com ("awatch", class_breakpoint, awatch_command,
15985 awatch_help.c_str ());
15986 set_cmd_completer_handle_brkchars (c, watch_command_completer);
c906108c 15987
11db9430 15988 add_info ("watchpoints", info_watchpoints_command, _("\
e5a67952 15989Status of specified watchpoints (all watchpoints if no argument)."));
c906108c 15990
920d2a44
AC
15991 /* XXX: cagney/2005-02-23: This should be a boolean, and should
15992 respond to changes - contrary to the description. */
85c07804
AC
15993 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
15994 &can_use_hw_watchpoints, _("\
15995Set debugger's willingness to use watchpoint hardware."), _("\
15996Show debugger's willingness to use watchpoint hardware."), _("\
c906108c
SS
15997If zero, gdb will not use hardware for new watchpoints, even if\n\
15998such is available. (However, any hardware watchpoints that were\n\
15999created before setting this to nonzero, will continue to use watchpoint\n\
85c07804
AC
16000hardware.)"),
16001 NULL,
920d2a44 16002 show_can_use_hw_watchpoints,
85c07804 16003 &setlist, &showlist);
c906108c
SS
16004
16005 can_use_hw_watchpoints = 1;
fa8d40ab 16006
1042e4c0
SS
16007 /* Tracepoint manipulation commands. */
16008
16009 c = add_com ("trace", class_breakpoint, trace_command, _("\
629500fa 16010Set a tracepoint at specified location.\n\
1042e4c0
SS
16011\n"
16012BREAK_ARGS_HELP ("trace") "\n\
16013Do \"help tracepoints\" for info on other tracepoint commands."));
16014 set_cmd_completer (c, location_completer);
16015
57b4f16e
PW
16016 add_com_alias ("tp", "trace", class_breakpoint, 0);
16017 add_com_alias ("tr", "trace", class_breakpoint, 1);
16018 add_com_alias ("tra", "trace", class_breakpoint, 1);
16019 add_com_alias ("trac", "trace", class_breakpoint, 1);
1042e4c0 16020
7a697b8d 16021 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
629500fa 16022Set a fast tracepoint at specified location.\n\
7a697b8d
SS
16023\n"
16024BREAK_ARGS_HELP ("ftrace") "\n\
16025Do \"help tracepoints\" for info on other tracepoint commands."));
16026 set_cmd_completer (c, location_completer);
16027
0fb4aa4b 16028 c = add_com ("strace", class_breakpoint, strace_command, _("\
629500fa 16029Set a static tracepoint at location or marker.\n\
0fb4aa4b
PA
16030\n\
16031strace [LOCATION] [if CONDITION]\n\
629500fa
KS
16032LOCATION may be a linespec, explicit, or address location (described below) \n\
16033or -m MARKER_ID.\n\n\
16034If a marker id is specified, probe the marker with that name. With\n\
16035no LOCATION, uses current execution address of the selected stack frame.\n\
0fb4aa4b
PA
16036Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
16037This collects arbitrary user data passed in the probe point call to the\n\
16038tracing library. You can inspect it when analyzing the trace buffer,\n\
16039by printing the $_sdata variable like any other convenience variable.\n\
16040\n\
16041CONDITION is a boolean expression.\n\
89549d7f 16042\n" LOCATION_HELP_STRING "\n\n\
d41c0fc8
PA
16043Multiple tracepoints at one place are permitted, and useful if their\n\
16044conditions are different.\n\
0fb4aa4b
PA
16045\n\
16046Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
16047Do \"help tracepoints\" for info on other tracepoint commands."));
16048 set_cmd_completer (c, location_completer);
16049
11db9430 16050 add_info ("tracepoints", info_tracepoints_command, _("\
e5a67952 16051Status of specified tracepoints (all tracepoints if no argument).\n\
1042e4c0
SS
16052Convenience variable \"$tpnum\" contains the number of the\n\
16053last tracepoint set."));
16054
16055 add_info_alias ("tp", "tracepoints", 1);
16056
16057 add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
16058Delete specified tracepoints.\n\
16059Arguments are tracepoint numbers, separated by spaces.\n\
16060No argument means delete all tracepoints."),
16061 &deletelist);
7e20dfcd 16062 add_alias_cmd ("tr", "tracepoints", class_trace, 1, &deletelist);
1042e4c0
SS
16063
16064 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
16065Disable specified tracepoints.\n\
16066Arguments are tracepoint numbers, separated by spaces.\n\
16067No argument means disable all tracepoints."),
16068 &disablelist);
16069 deprecate_cmd (c, "disable");
16070
16071 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
16072Enable specified tracepoints.\n\
16073Arguments are tracepoint numbers, separated by spaces.\n\
16074No argument means enable all tracepoints."),
16075 &enablelist);
16076 deprecate_cmd (c, "enable");
16077
16078 add_com ("passcount", class_trace, trace_pass_command, _("\
16079Set the passcount for a tracepoint.\n\
16080The trace will end when the tracepoint has been passed 'count' times.\n\
16081Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
16082if TPNUM is omitted, passcount refers to the last tracepoint defined."));
16083
0743fc83
TT
16084 add_basic_prefix_cmd ("save", class_breakpoint,
16085 _("Save breakpoint definitions as a script."),
16086 &save_cmdlist, "save ",
16087 0/*allow-unknown*/, &cmdlist);
6149aea9
PA
16088
16089 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
16090Save current breakpoint definitions as a script.\n\
cce7e648 16091This includes all types of breakpoints (breakpoints, watchpoints,\n\
6149aea9
PA
16092catchpoints, tracepoints). Use the 'source' command in another debug\n\
16093session to restore them."),
16094 &save_cmdlist);
16095 set_cmd_completer (c, filename_completer);
16096
16097 c = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
1042e4c0 16098Save current tracepoint definitions as a script.\n\
6149aea9
PA
16099Use the 'source' command in another debug session to restore them."),
16100 &save_cmdlist);
1042e4c0
SS
16101 set_cmd_completer (c, filename_completer);
16102
6149aea9
PA
16103 c = add_com_alias ("save-tracepoints", "save tracepoints", class_trace, 0);
16104 deprecate_cmd (c, "save tracepoints");
16105
3b6acaee 16106 add_basic_prefix_cmd ("breakpoint", class_maintenance, _("\
590042fc 16107Breakpoint specific settings.\n\
fa8d40ab 16108Configure various breakpoint-specific variables such as\n\
590042fc 16109pending breakpoint behavior."),
3b6acaee
TT
16110 &breakpoint_set_cmdlist, "set breakpoint ",
16111 0/*allow-unknown*/, &setlist);
16112 add_show_prefix_cmd ("breakpoint", class_maintenance, _("\
590042fc 16113Breakpoint specific settings.\n\
fa8d40ab 16114Configure various breakpoint-specific variables such as\n\
590042fc 16115pending breakpoint behavior."),
3b6acaee
TT
16116 &breakpoint_show_cmdlist, "show breakpoint ",
16117 0/*allow-unknown*/, &showlist);
fa8d40ab 16118
7915a72c
AC
16119 add_setshow_auto_boolean_cmd ("pending", no_class,
16120 &pending_break_support, _("\
16121Set debugger's behavior regarding pending breakpoints."), _("\
16122Show debugger's behavior regarding pending breakpoints."), _("\
6e1d7d6c
AC
16123If on, an unrecognized breakpoint location will cause gdb to create a\n\
16124pending breakpoint. If off, an unrecognized breakpoint location results in\n\
16125an error. If auto, an unrecognized breakpoint location results in a\n\
7915a72c 16126user-query to see if a pending breakpoint should be created."),
2c5b56ce 16127 NULL,
920d2a44 16128 show_pending_break_support,
6e1d7d6c
AC
16129 &breakpoint_set_cmdlist,
16130 &breakpoint_show_cmdlist);
fa8d40ab
JJ
16131
16132 pending_break_support = AUTO_BOOLEAN_AUTO;
765dc015
VP
16133
16134 add_setshow_boolean_cmd ("auto-hw", no_class,
16135 &automatic_hardware_breakpoints, _("\
16136Set automatic usage of hardware breakpoints."), _("\
16137Show automatic usage of hardware breakpoints."), _("\
16138If set, the debugger will automatically use hardware breakpoints for\n\
16139breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
16140a warning will be emitted for such breakpoints."),
16141 NULL,
16142 show_automatic_hardware_breakpoints,
16143 &breakpoint_set_cmdlist,
16144 &breakpoint_show_cmdlist);
74960c60 16145
a25a5a45
PA
16146 add_setshow_boolean_cmd ("always-inserted", class_support,
16147 &always_inserted_mode, _("\
74960c60
VP
16148Set mode for inserting breakpoints."), _("\
16149Show mode for inserting breakpoints."), _("\
a25a5a45
PA
16150When this mode is on, breakpoints are inserted immediately as soon as\n\
16151they're created, kept inserted even when execution stops, and removed\n\
16152only when the user deletes them. When this mode is off (the default),\n\
16153breakpoints are inserted only when execution continues, and removed\n\
16154when execution stops."),
72d0e2c5
YQ
16155 NULL,
16156 &show_always_inserted_mode,
16157 &breakpoint_set_cmdlist,
16158 &breakpoint_show_cmdlist);
f1310107 16159
b775012e
LM
16160 add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
16161 condition_evaluation_enums,
16162 &condition_evaluation_mode_1, _("\
16163Set mode of breakpoint condition evaluation."), _("\
16164Show mode of breakpoint condition evaluation."), _("\
d1cda5d9 16165When this is set to \"host\", breakpoint conditions will be\n\
b775012e
LM
16166evaluated on the host's side by GDB. When it is set to \"target\",\n\
16167breakpoint conditions will be downloaded to the target (if the target\n\
16168supports such feature) and conditions will be evaluated on the target's side.\n\
16169If this is set to \"auto\" (default), this will be automatically set to\n\
16170\"target\" if it supports condition evaluation, otherwise it will\n\
7a4e8e7d 16171be set to \"host\"."),
b775012e
LM
16172 &set_condition_evaluation_mode,
16173 &show_condition_evaluation_mode,
16174 &breakpoint_set_cmdlist,
16175 &breakpoint_show_cmdlist);
16176
f1310107
TJB
16177 add_com ("break-range", class_breakpoint, break_range_command, _("\
16178Set a breakpoint for an address range.\n\
16179break-range START-LOCATION, END-LOCATION\n\
16180where START-LOCATION and END-LOCATION can be one of the following:\n\
16181 LINENUM, for that line in the current file,\n\
16182 FILE:LINENUM, for that line in that file,\n\
16183 +OFFSET, for that number of lines after the current line\n\
dda83cd7 16184 or the start of the range\n\
f1310107
TJB
16185 FUNCTION, for the first line in that function,\n\
16186 FILE:FUNCTION, to distinguish among like-named static functions.\n\
16187 *ADDRESS, for the instruction at that address.\n\
16188\n\
16189The breakpoint will stop execution of the inferior whenever it executes\n\
16190an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
16191range (including START-LOCATION and END-LOCATION)."));
16192
e7e0cddf 16193 c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
629500fa 16194Set a dynamic printf at specified location.\n\
e7e0cddf 16195dprintf location,format string,arg1,arg2,...\n\
629500fa
KS
16196location may be a linespec, explicit, or address location.\n"
16197"\n" LOCATION_HELP_STRING));
e7e0cddf
SS
16198 set_cmd_completer (c, location_completer);
16199
16200 add_setshow_enum_cmd ("dprintf-style", class_support,
16201 dprintf_style_enums, &dprintf_style, _("\
16202Set the style of usage for dynamic printf."), _("\
16203Show the style of usage for dynamic printf."), _("\
16204This setting chooses how GDB will do a dynamic printf.\n\
16205If the value is \"gdb\", then the printing is done by GDB to its own\n\
16206console, as with the \"printf\" command.\n\
16207If the value is \"call\", the print is done by calling a function in your\n\
16208program; by default printf(), but you can choose a different function or\n\
16209output stream by setting dprintf-function and dprintf-channel."),
16210 update_dprintf_commands, NULL,
16211 &setlist, &showlist);
16212
16213 dprintf_function = xstrdup ("printf");
16214 add_setshow_string_cmd ("dprintf-function", class_support,
16215 &dprintf_function, _("\
590042fc
PW
16216Set the function to use for dynamic printf."), _("\
16217Show the function to use for dynamic printf."), NULL,
e7e0cddf
SS
16218 update_dprintf_commands, NULL,
16219 &setlist, &showlist);
16220
16221 dprintf_channel = xstrdup ("");
16222 add_setshow_string_cmd ("dprintf-channel", class_support,
16223 &dprintf_channel, _("\
590042fc
PW
16224Set the channel to use for dynamic printf."), _("\
16225Show the channel to use for dynamic printf."), NULL,
e7e0cddf
SS
16226 update_dprintf_commands, NULL,
16227 &setlist, &showlist);
16228
d3ce09f5
SS
16229 add_setshow_boolean_cmd ("disconnected-dprintf", no_class,
16230 &disconnected_dprintf, _("\
16231Set whether dprintf continues after GDB disconnects."), _("\
16232Show whether dprintf continues after GDB disconnects."), _("\
16233Use this to let dprintf commands continue to hit and produce output\n\
16234even if GDB disconnects or detaches from the target."),
16235 NULL,
16236 NULL,
16237 &setlist, &showlist);
16238
16239 add_com ("agent-printf", class_vars, agent_printf_command, _("\
590042fc
PW
16240Target agent only formatted printing, like the C \"printf\" function.\n\
16241Usage: agent-printf \"format string\", ARG1, ARG2, ARG3, ..., ARGN\n\
16242This supports most C printf format specifications, like %s, %d, etc.\n\
16243This is useful for formatted output in user-defined commands."));
d3ce09f5 16244
491144b5 16245 automatic_hardware_breakpoints = true;
f3b1572e 16246
c90e7d63
SM
16247 gdb::observers::about_to_proceed.attach (breakpoint_about_to_proceed,
16248 "breakpoint");
16249 gdb::observers::thread_exit.attach (remove_threaded_breakpoints,
16250 "breakpoint");
c906108c 16251}