Fix a bug in the ARM emulator which would not allow 4 byte alignment for double word...
[deliverable/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
b1d4d8d1
TBA
976/* The options for the "condition" command. */
977
978struct condition_command_opts
979{
980 /* For "-force". */
981 bool force_condition = false;
982};
983
984static const gdb::option::option_def condition_command_option_defs[] = {
985
986 gdb::option::flag_option_def<condition_command_opts> {
987 "force",
988 [] (condition_command_opts *opts) { return &opts->force_condition; },
989 N_("Set the condition even if it is invalid for all current locations."),
990 },
991
992};
993
994/* Create an option_def_group for the "condition" options, with
995 CC_OPTS as context. */
996
997static inline gdb::option::option_def_group
998make_condition_command_options_def_group (condition_command_opts *cc_opts)
999{
1000 return {{condition_command_option_defs}, cc_opts};
1001}
1002
d55637df
TT
1003/* Completion for the "condition" command. */
1004
eb3ff9a5 1005static void
6f937416 1006condition_completer (struct cmd_list_element *cmd,
eb3ff9a5 1007 completion_tracker &tracker,
b1d4d8d1 1008 const char *text, const char * /*word*/)
d55637df 1009{
b1d4d8d1
TBA
1010 bool has_no_arguments = (*text == '\0');
1011 condition_command_opts cc_opts;
1012 const auto group = make_condition_command_options_def_group (&cc_opts);
1013 if (gdb::option::complete_options
1014 (tracker, &text, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR, group))
1015 return;
d55637df 1016
f1735a53 1017 text = skip_spaces (text);
b1d4d8d1 1018 const char *space = skip_to_space (text);
d55637df
TT
1019 if (*space == '\0')
1020 {
1021 int len;
1022 struct breakpoint *b;
d55637df
TT
1023
1024 if (text[0] == '$')
1025 {
b1d4d8d1 1026 tracker.advance_custom_word_point_by (1);
d55637df 1027 /* We don't support completion of history indices. */
eb3ff9a5
PA
1028 if (!isdigit (text[1]))
1029 complete_internalvar (tracker, &text[1]);
1030 return;
d55637df
TT
1031 }
1032
b1d4d8d1
TBA
1033 /* Suggest the "-force" flag if no arguments are given. If
1034 arguments were passed, they either already include the flag,
1035 or we are beyond the point of suggesting it because it's
1036 positionally the first argument. */
1037 if (has_no_arguments)
1038 gdb::option::complete_on_all_options (tracker, group);
1039
d55637df
TT
1040 /* We're completing the breakpoint number. */
1041 len = strlen (text);
1042
1043 ALL_BREAKPOINTS (b)
58ce7251
SDJ
1044 {
1045 char number[50];
1046
1047 xsnprintf (number, sizeof (number), "%d", b->number);
1048
1049 if (strncmp (number, text, len) == 0)
b02f78f9 1050 tracker.add_completion (make_unique_xstrdup (number));
58ce7251 1051 }
d55637df 1052
eb3ff9a5 1053 return;
d55637df
TT
1054 }
1055
b1d4d8d1
TBA
1056 /* We're completing the expression part. Skip the breakpoint num. */
1057 const char *exp_start = skip_spaces (space);
1058 tracker.advance_custom_word_point_by (exp_start - text);
1059 text = exp_start;
1060 const char *word = advance_to_expression_complete_word_point (tracker, text);
eb3ff9a5 1061 expression_completer (cmd, tracker, text, word);
d55637df
TT
1062}
1063
c906108c
SS
1064/* condition N EXP -- set break condition of breakpoint N to EXP. */
1065
1066static void
0b39b52e 1067condition_command (const char *arg, int from_tty)
c906108c 1068{
52f0bd74 1069 struct breakpoint *b;
0b39b52e 1070 const char *p;
52f0bd74 1071 int bnum;
c906108c
SS
1072
1073 if (arg == 0)
e2e0b3e5 1074 error_no_arg (_("breakpoint number"));
c906108c
SS
1075
1076 p = arg;
733d554a
TBA
1077
1078 /* Check if the "-force" flag was passed. */
b1d4d8d1
TBA
1079 condition_command_opts cc_opts;
1080 const auto group = make_condition_command_options_def_group (&cc_opts);
1081 gdb::option::process_options
1082 (&p, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR, group);
733d554a 1083
c906108c 1084 bnum = get_number (&p);
5c44784c 1085 if (bnum == 0)
8a3fe4f8 1086 error (_("Bad breakpoint argument: '%s'"), arg);
c906108c
SS
1087
1088 ALL_BREAKPOINTS (b)
1089 if (b->number == bnum)
2f069f6f 1090 {
6dddc817
DE
1091 /* Check if this breakpoint has a "stop" method implemented in an
1092 extension language. This method and conditions entered into GDB
1093 from the CLI are mutually exclusive. */
1094 const struct extension_language_defn *extlang
1095 = get_breakpoint_cond_ext_lang (b, EXT_LANG_NONE);
1096
1097 if (extlang != NULL)
1098 {
1099 error (_("Only one stop condition allowed. There is currently"
1100 " a %s stop condition defined for this breakpoint."),
1101 ext_lang_capitalized_name (extlang));
1102 }
b1d4d8d1 1103 set_breakpoint_condition (b, p, from_tty, cc_opts.force_condition);
b775012e
LM
1104
1105 if (is_breakpoint (b))
44702360 1106 update_global_location_list (UGLL_MAY_INSERT);
b775012e 1107
2f069f6f
JB
1108 return;
1109 }
c906108c 1110
8a3fe4f8 1111 error (_("No breakpoint number %d."), bnum);
c906108c
SS
1112}
1113
a7bdde9e
VP
1114/* Check that COMMAND do not contain commands that are suitable
1115 only for tracepoints and not suitable for ordinary breakpoints.
4a64f543
MS
1116 Throw if any such commands is found. */
1117
a7bdde9e
VP
1118static void
1119check_no_tracepoint_commands (struct command_line *commands)
1120{
1121 struct command_line *c;
cc59ec59 1122
a7bdde9e
VP
1123 for (c = commands; c; c = c->next)
1124 {
a7bdde9e 1125 if (c->control_type == while_stepping_control)
3e43a32a
MS
1126 error (_("The 'while-stepping' command can "
1127 "only be used for tracepoints"));
a7bdde9e 1128
12973681
TT
1129 check_no_tracepoint_commands (c->body_list_0.get ());
1130 check_no_tracepoint_commands (c->body_list_1.get ());
a7bdde9e
VP
1131
1132 /* Not that command parsing removes leading whitespace and comment
4a64f543 1133 lines and also empty lines. So, we only need to check for
a7bdde9e
VP
1134 command directly. */
1135 if (strstr (c->line, "collect ") == c->line)
1136 error (_("The 'collect' command can only be used for tracepoints"));
1137
51661e93
VP
1138 if (strstr (c->line, "teval ") == c->line)
1139 error (_("The 'teval' command can only be used for tracepoints"));
a7bdde9e
VP
1140 }
1141}
1142
c1fc2657 1143struct longjmp_breakpoint : public breakpoint
3b0871f4 1144{
c1fc2657 1145 ~longjmp_breakpoint () override;
3b0871f4
SM
1146};
1147
d77f58be
SS
1148/* Encapsulate tests for different types of tracepoints. */
1149
3b0871f4
SM
1150static bool
1151is_tracepoint_type (bptype type)
d9b3f62e
PA
1152{
1153 return (type == bp_tracepoint
1154 || type == bp_fast_tracepoint
1155 || type == bp_static_tracepoint);
1156}
1157
3b0871f4
SM
1158static bool
1159is_longjmp_type (bptype type)
1160{
1161 return type == bp_longjmp || type == bp_exception;
1162}
1163
f2478a7e
SM
1164/* See breakpoint.h. */
1165
1166bool
d77f58be 1167is_tracepoint (const struct breakpoint *b)
a7bdde9e 1168{
d9b3f62e 1169 return is_tracepoint_type (b->type);
a7bdde9e 1170}
d9b3f62e 1171
a5e364af
SM
1172/* Factory function to create an appropriate instance of breakpoint given
1173 TYPE. */
1174
1175static std::unique_ptr<breakpoint>
1176new_breakpoint_from_type (bptype type)
1177{
1178 breakpoint *b;
1179
1180 if (is_tracepoint_type (type))
c1fc2657 1181 b = new tracepoint ();
3b0871f4 1182 else if (is_longjmp_type (type))
c1fc2657 1183 b = new longjmp_breakpoint ();
a5e364af
SM
1184 else
1185 b = new breakpoint ();
1186
1187 return std::unique_ptr<breakpoint> (b);
1188}
1189
e5dd4106 1190/* A helper function that validates that COMMANDS are valid for a
95a42b64
TT
1191 breakpoint. This function will throw an exception if a problem is
1192 found. */
48cb2d85 1193
95a42b64
TT
1194static void
1195validate_commands_for_breakpoint (struct breakpoint *b,
1196 struct command_line *commands)
48cb2d85 1197{
d77f58be 1198 if (is_tracepoint (b))
a7bdde9e 1199 {
c9a6ce02 1200 struct tracepoint *t = (struct tracepoint *) b;
a7bdde9e
VP
1201 struct command_line *c;
1202 struct command_line *while_stepping = 0;
c9a6ce02
PA
1203
1204 /* Reset the while-stepping step count. The previous commands
dda83cd7
SM
1205 might have included a while-stepping action, while the new
1206 ones might not. */
c9a6ce02
PA
1207 t->step_count = 0;
1208
1209 /* We need to verify that each top-level element of commands is
1210 valid for tracepoints, that there's at most one
1211 while-stepping element, and that the while-stepping's body
1212 has valid tracing commands excluding nested while-stepping.
1213 We also need to validate the tracepoint action line in the
1214 context of the tracepoint --- validate_actionline actually
1215 has side effects, like setting the tracepoint's
1216 while-stepping STEP_COUNT, in addition to checking if the
1217 collect/teval actions parse and make sense in the
1218 tracepoint's context. */
a7bdde9e
VP
1219 for (c = commands; c; c = c->next)
1220 {
a7bdde9e
VP
1221 if (c->control_type == while_stepping_control)
1222 {
1223 if (b->type == bp_fast_tracepoint)
3e43a32a
MS
1224 error (_("The 'while-stepping' command "
1225 "cannot be used for fast tracepoint"));
0fb4aa4b 1226 else if (b->type == bp_static_tracepoint)
3e43a32a
MS
1227 error (_("The 'while-stepping' command "
1228 "cannot be used for static tracepoint"));
a7bdde9e
VP
1229
1230 if (while_stepping)
3e43a32a
MS
1231 error (_("The 'while-stepping' command "
1232 "can be used only once"));
a7bdde9e
VP
1233 else
1234 while_stepping = c;
1235 }
c9a6ce02
PA
1236
1237 validate_actionline (c->line, b);
a7bdde9e
VP
1238 }
1239 if (while_stepping)
1240 {
1241 struct command_line *c2;
1242
12973681
TT
1243 gdb_assert (while_stepping->body_list_1 == nullptr);
1244 c2 = while_stepping->body_list_0.get ();
a7bdde9e
VP
1245 for (; c2; c2 = c2->next)
1246 {
a7bdde9e
VP
1247 if (c2->control_type == while_stepping_control)
1248 error (_("The 'while-stepping' command cannot be nested"));
1249 }
1250 }
1251 }
1252 else
1253 {
1254 check_no_tracepoint_commands (commands);
1255 }
95a42b64
TT
1256}
1257
0fb4aa4b
PA
1258/* Return a vector of all the static tracepoints set at ADDR. The
1259 caller is responsible for releasing the vector. */
1260
f51e0e20 1261std::vector<breakpoint *>
0fb4aa4b
PA
1262static_tracepoints_here (CORE_ADDR addr)
1263{
1264 struct breakpoint *b;
f51e0e20 1265 std::vector<breakpoint *> found;
0fb4aa4b
PA
1266 struct bp_location *loc;
1267
1268 ALL_BREAKPOINTS (b)
1269 if (b->type == bp_static_tracepoint)
1270 {
1271 for (loc = b->loc; loc; loc = loc->next)
1272 if (loc->address == addr)
f51e0e20 1273 found.push_back (b);
0fb4aa4b
PA
1274 }
1275
1276 return found;
1277}
1278
95a42b64 1279/* Set the command list of B to COMMANDS. If breakpoint is tracepoint,
4a64f543 1280 validate that only allowed commands are included. */
95a42b64
TT
1281
1282void
4a64f543 1283breakpoint_set_commands (struct breakpoint *b,
12973681 1284 counted_command_line &&commands)
95a42b64 1285{
93921405 1286 validate_commands_for_breakpoint (b, commands.get ());
a7bdde9e 1287
d1b0a7bf 1288 b->commands = std::move (commands);
76727919 1289 gdb::observers::breakpoint_modified.notify (b);
48cb2d85
VP
1290}
1291
45a43567
TT
1292/* Set the internal `silent' flag on the breakpoint. Note that this
1293 is not the same as the "silent" that may appear in the breakpoint's
1294 commands. */
1295
1296void
1297breakpoint_set_silent (struct breakpoint *b, int silent)
1298{
1299 int old_silent = b->silent;
1300
1301 b->silent = silent;
1302 if (old_silent != silent)
76727919 1303 gdb::observers::breakpoint_modified.notify (b);
45a43567
TT
1304}
1305
1306/* Set the thread for this breakpoint. If THREAD is -1, make the
1307 breakpoint work for any thread. */
1308
1309void
1310breakpoint_set_thread (struct breakpoint *b, int thread)
1311{
1312 int old_thread = b->thread;
1313
1314 b->thread = thread;
1315 if (old_thread != thread)
76727919 1316 gdb::observers::breakpoint_modified.notify (b);
45a43567
TT
1317}
1318
1319/* Set the task for this breakpoint. If TASK is 0, make the
1320 breakpoint work for any task. */
1321
1322void
1323breakpoint_set_task (struct breakpoint *b, int task)
1324{
1325 int old_task = b->task;
1326
1327 b->task = task;
1328 if (old_task != task)
76727919 1329 gdb::observers::breakpoint_modified.notify (b);
45a43567
TT
1330}
1331
95a42b64 1332static void
896b6bda 1333commands_command_1 (const char *arg, int from_tty,
4a64f543 1334 struct command_line *control)
95a42b64 1335{
d1b0a7bf 1336 counted_command_line cmd;
999700cd
PW
1337 /* cmd_read will be true once we have read cmd. Note that cmd might still be
1338 NULL after the call to read_command_lines if the user provides an empty
1339 list of command by just typing "end". */
1340 bool cmd_read = false;
95a42b64 1341
896b6bda
PA
1342 std::string new_arg;
1343
95a42b64
TT
1344 if (arg == NULL || !*arg)
1345 {
da1df1db 1346 /* Argument not explicitly given. Synthesize it. */
86b17b60 1347 if (breakpoint_count - prev_breakpoint_count > 1)
896b6bda
PA
1348 new_arg = string_printf ("%d-%d", prev_breakpoint_count + 1,
1349 breakpoint_count);
95a42b64 1350 else if (breakpoint_count > 0)
896b6bda 1351 new_arg = string_printf ("%d", breakpoint_count);
48649e1b 1352 }
da1df1db
TBA
1353 else
1354 {
1355 /* Create a copy of ARG. This is needed because the "commands"
1356 command may be coming from a script. In that case, the read
1357 line buffer is going to be overwritten in the lambda of
1358 'map_breakpoint_numbers' below when reading the next line
1359 before we are are done parsing the breakpoint numbers. */
1360 new_arg = arg;
1361 }
1362 arg = new_arg.c_str ();
48649e1b
TT
1363
1364 map_breakpoint_numbers
1365 (arg, [&] (breakpoint *b)
1366 {
999700cd 1367 if (!cmd_read)
48649e1b 1368 {
999700cd 1369 gdb_assert (cmd == NULL);
48649e1b 1370 if (control != NULL)
12973681 1371 cmd = control->body_list_0;
48649e1b
TT
1372 else
1373 {
81b1e71c
TT
1374 std::string str
1375 = string_printf (_("Type commands for breakpoint(s) "
1376 "%s, one per line."),
1377 arg);
48649e1b 1378
60b3cef2
TT
1379 auto do_validate = [=] (const char *line)
1380 {
1381 validate_actionline (line, b);
1382 };
1383 gdb::function_view<void (const char *)> validator;
1384 if (is_tracepoint (b))
1385 validator = do_validate;
1386
1387 cmd = read_command_lines (str.c_str (), from_tty, 1, validator);
48649e1b 1388 }
999700cd 1389 cmd_read = true;
48649e1b
TT
1390 }
1391
1392 /* If a breakpoint was on the list more than once, we don't need to
1393 do anything. */
1394 if (b->commands != cmd)
1395 {
d1b0a7bf 1396 validate_commands_for_breakpoint (b, cmd.get ());
48649e1b 1397 b->commands = cmd;
76727919 1398 gdb::observers::breakpoint_modified.notify (b);
48649e1b
TT
1399 }
1400 });
95a42b64
TT
1401}
1402
1403static void
0b39b52e 1404commands_command (const char *arg, int from_tty)
95a42b64
TT
1405{
1406 commands_command_1 (arg, from_tty, NULL);
c906108c 1407}
40c03ae8
EZ
1408
1409/* Like commands_command, but instead of reading the commands from
1410 input stream, takes them from an already parsed command structure.
1411
1412 This is used by cli-script.c to DTRT with breakpoint commands
1413 that are part of if and while bodies. */
1414enum command_control_type
896b6bda 1415commands_from_control_command (const char *arg, struct command_line *cmd)
40c03ae8 1416{
95a42b64
TT
1417 commands_command_1 (arg, 0, cmd);
1418 return simple_control;
40c03ae8 1419}
876fa593
JK
1420
1421/* Return non-zero if BL->TARGET_INFO contains valid information. */
1422
1423static int
1424bp_location_has_shadow (struct bp_location *bl)
1425{
1426 if (bl->loc_type != bp_loc_software_breakpoint)
1427 return 0;
1428 if (!bl->inserted)
1429 return 0;
1430 if (bl->target_info.shadow_len == 0)
e5dd4106 1431 /* BL isn't valid, or doesn't shadow memory. */
876fa593
JK
1432 return 0;
1433 return 1;
1434}
1435
9d497a19
PA
1436/* Update BUF, which is LEN bytes read from the target address
1437 MEMADDR, by replacing a memory breakpoint with its shadowed
1438 contents.
1439
1440 If READBUF is not NULL, this buffer must not overlap with the of
1441 the breakpoint location's shadow_contents buffer. Otherwise, a
1442 failed assertion internal error will be raised. */
1443
1444static void
1445one_breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1446 const gdb_byte *writebuf_org,
1447 ULONGEST memaddr, LONGEST len,
1448 struct bp_target_info *target_info,
1449 struct gdbarch *gdbarch)
1450{
1451 /* Now do full processing of the found relevant range of elements. */
1452 CORE_ADDR bp_addr = 0;
1453 int bp_size = 0;
1454 int bptoffset = 0;
1455
1456 if (!breakpoint_address_match (target_info->placed_address_space, 0,
1457 current_program_space->aspace, 0))
1458 {
1459 /* The breakpoint is inserted in a different address space. */
1460 return;
1461 }
1462
1463 /* Addresses and length of the part of the breakpoint that
1464 we need to copy. */
1465 bp_addr = target_info->placed_address;
1466 bp_size = target_info->shadow_len;
1467
1468 if (bp_addr + bp_size <= memaddr)
1469 {
1470 /* The breakpoint is entirely before the chunk of memory we are
1471 reading. */
1472 return;
1473 }
1474
1475 if (bp_addr >= memaddr + len)
1476 {
1477 /* The breakpoint is entirely after the chunk of memory we are
1478 reading. */
1479 return;
1480 }
1481
1482 /* Offset within shadow_contents. */
1483 if (bp_addr < memaddr)
1484 {
1485 /* Only copy the second part of the breakpoint. */
1486 bp_size -= memaddr - bp_addr;
1487 bptoffset = memaddr - bp_addr;
1488 bp_addr = memaddr;
1489 }
1490
1491 if (bp_addr + bp_size > memaddr + len)
1492 {
1493 /* Only copy the first part of the breakpoint. */
1494 bp_size -= (bp_addr + bp_size) - (memaddr + len);
1495 }
1496
1497 if (readbuf != NULL)
1498 {
1499 /* Verify that the readbuf buffer does not overlap with the
1500 shadow_contents buffer. */
1501 gdb_assert (target_info->shadow_contents >= readbuf + len
1502 || readbuf >= (target_info->shadow_contents
1503 + target_info->shadow_len));
1504
1505 /* Update the read buffer with this inserted breakpoint's
1506 shadow. */
1507 memcpy (readbuf + bp_addr - memaddr,
1508 target_info->shadow_contents + bptoffset, bp_size);
1509 }
1510 else
1511 {
1512 const unsigned char *bp;
0d5ed153
MR
1513 CORE_ADDR addr = target_info->reqstd_address;
1514 int placed_size;
9d497a19
PA
1515
1516 /* Update the shadow with what we want to write to memory. */
1517 memcpy (target_info->shadow_contents + bptoffset,
1518 writebuf_org + bp_addr - memaddr, bp_size);
1519
1520 /* Determine appropriate breakpoint contents and size for this
1521 address. */
0d5ed153 1522 bp = gdbarch_breakpoint_from_pc (gdbarch, &addr, &placed_size);
9d497a19
PA
1523
1524 /* Update the final write buffer with this inserted
1525 breakpoint's INSN. */
1526 memcpy (writebuf + bp_addr - memaddr, bp + bptoffset, bp_size);
1527 }
1528}
1529
8defab1a 1530/* Update BUF, which is LEN bytes read from the target address MEMADDR,
876fa593
JK
1531 by replacing any memory breakpoints with their shadowed contents.
1532
35c63cd8
JB
1533 If READBUF is not NULL, this buffer must not overlap with any of
1534 the breakpoint location's shadow_contents buffers. Otherwise,
1535 a failed assertion internal error will be raised.
1536
876fa593 1537 The range of shadowed area by each bp_location is:
f5336ca5
PA
1538 bl->address - bp_locations_placed_address_before_address_max
1539 up to bl->address + bp_locations_shadow_len_after_address_max
876fa593
JK
1540 The range we were requested to resolve shadows for is:
1541 memaddr ... memaddr + len
1542 Thus the safe cutoff boundaries for performance optimization are
35df4500 1543 memaddr + len <= (bl->address
f5336ca5 1544 - bp_locations_placed_address_before_address_max)
876fa593 1545 and:
f5336ca5 1546 bl->address + bp_locations_shadow_len_after_address_max <= memaddr */
c906108c 1547
8defab1a 1548void
f0ba3972
PA
1549breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1550 const gdb_byte *writebuf_org,
1551 ULONGEST memaddr, LONGEST len)
c906108c 1552{
4a64f543
MS
1553 /* Left boundary, right boundary and median element of our binary
1554 search. */
876fa593
JK
1555 unsigned bc_l, bc_r, bc;
1556
4a64f543
MS
1557 /* Find BC_L which is a leftmost element which may affect BUF
1558 content. It is safe to report lower value but a failure to
1559 report higher one. */
876fa593
JK
1560
1561 bc_l = 0;
f5336ca5 1562 bc_r = bp_locations_count;
876fa593
JK
1563 while (bc_l + 1 < bc_r)
1564 {
35df4500 1565 struct bp_location *bl;
876fa593
JK
1566
1567 bc = (bc_l + bc_r) / 2;
f5336ca5 1568 bl = bp_locations[bc];
876fa593 1569
4a64f543
MS
1570 /* Check first BL->ADDRESS will not overflow due to the added
1571 constant. Then advance the left boundary only if we are sure
1572 the BC element can in no way affect the BUF content (MEMADDR
1573 to MEMADDR + LEN range).
876fa593 1574
f5336ca5 1575 Use the BP_LOCATIONS_SHADOW_LEN_AFTER_ADDRESS_MAX safety
4a64f543
MS
1576 offset so that we cannot miss a breakpoint with its shadow
1577 range tail still reaching MEMADDR. */
c5aa993b 1578
f5336ca5 1579 if ((bl->address + bp_locations_shadow_len_after_address_max
35df4500 1580 >= bl->address)
f5336ca5 1581 && (bl->address + bp_locations_shadow_len_after_address_max
35df4500 1582 <= memaddr))
876fa593
JK
1583 bc_l = bc;
1584 else
1585 bc_r = bc;
1586 }
1587
128070bb
PA
1588 /* Due to the binary search above, we need to make sure we pick the
1589 first location that's at BC_L's address. E.g., if there are
1590 multiple locations at the same address, BC_L may end up pointing
1591 at a duplicate location, and miss the "master"/"inserted"
1592 location. Say, given locations L1, L2 and L3 at addresses A and
1593 B:
1594
1595 L1@A, L2@A, L3@B, ...
1596
1597 BC_L could end up pointing at location L2, while the "master"
1598 location could be L1. Since the `loc->inserted' flag is only set
1599 on "master" locations, we'd forget to restore the shadow of L1
1600 and L2. */
1601 while (bc_l > 0
f5336ca5 1602 && bp_locations[bc_l]->address == bp_locations[bc_l - 1]->address)
128070bb
PA
1603 bc_l--;
1604
876fa593
JK
1605 /* Now do full processing of the found relevant range of elements. */
1606
f5336ca5 1607 for (bc = bc_l; bc < bp_locations_count; bc++)
c5aa993b 1608 {
f5336ca5 1609 struct bp_location *bl = bp_locations[bc];
876fa593 1610
35df4500
TJB
1611 /* bp_location array has BL->OWNER always non-NULL. */
1612 if (bl->owner->type == bp_none)
8a3fe4f8 1613 warning (_("reading through apparently deleted breakpoint #%d?"),
35df4500 1614 bl->owner->number);
ffce0d52 1615
e5dd4106 1616 /* Performance optimization: any further element can no longer affect BUF
876fa593
JK
1617 content. */
1618
f5336ca5
PA
1619 if (bl->address >= bp_locations_placed_address_before_address_max
1620 && memaddr + len <= (bl->address
1621 - bp_locations_placed_address_before_address_max))
876fa593
JK
1622 break;
1623
35df4500 1624 if (!bp_location_has_shadow (bl))
c5aa993b 1625 continue;
6c95b8df 1626
9d497a19
PA
1627 one_breakpoint_xfer_memory (readbuf, writebuf, writebuf_org,
1628 memaddr, len, &bl->target_info, bl->gdbarch);
1629 }
c906108c 1630}
9d497a19 1631
f2478a7e 1632/* See breakpoint.h. */
b775012e 1633
f2478a7e 1634bool
b775012e
LM
1635is_breakpoint (const struct breakpoint *bpt)
1636{
1637 return (bpt->type == bp_breakpoint
e7e0cddf
SS
1638 || bpt->type == bp_hardware_breakpoint
1639 || bpt->type == bp_dprintf);
b775012e
LM
1640}
1641
60e1c644
PA
1642/* Return true if BPT is of any hardware watchpoint kind. */
1643
f2478a7e 1644static bool
d77f58be 1645is_hardware_watchpoint (const struct breakpoint *bpt)
a5606eee
VP
1646{
1647 return (bpt->type == bp_hardware_watchpoint
1648 || bpt->type == bp_read_watchpoint
1649 || bpt->type == bp_access_watchpoint);
1650}
7270d8f2 1651
f2478a7e 1652/* See breakpoint.h. */
60e1c644 1653
f2478a7e 1654bool
d77f58be 1655is_watchpoint (const struct breakpoint *bpt)
60e1c644
PA
1656{
1657 return (is_hardware_watchpoint (bpt)
1658 || bpt->type == bp_watchpoint);
1659}
1660
3a5c3e22
PA
1661/* Returns true if the current thread and its running state are safe
1662 to evaluate or update watchpoint B. Watchpoints on local
1663 expressions need to be evaluated in the context of the thread that
1664 was current when the watchpoint was created, and, that thread needs
1665 to be stopped to be able to select the correct frame context.
1666 Watchpoints on global expressions can be evaluated on any thread,
1667 and in any state. It is presently left to the target allowing
1668 memory accesses when threads are running. */
f6bc2008
PA
1669
1670static int
3a5c3e22 1671watchpoint_in_thread_scope (struct watchpoint *b)
f6bc2008 1672{
c1fc2657 1673 return (b->pspace == current_program_space
d7e15655
TT
1674 && (b->watchpoint_thread == null_ptid
1675 || (inferior_ptid == b->watchpoint_thread
00431a78 1676 && !inferior_thread ()->executing)));
f6bc2008
PA
1677}
1678
d0fb5eae
JK
1679/* Set watchpoint B to disp_del_at_next_stop, even including its possible
1680 associated bp_watchpoint_scope breakpoint. */
1681
1682static void
3a5c3e22 1683watchpoint_del_at_next_stop (struct watchpoint *w)
d0fb5eae 1684{
c1fc2657 1685 if (w->related_breakpoint != w)
d0fb5eae 1686 {
c1fc2657
SM
1687 gdb_assert (w->related_breakpoint->type == bp_watchpoint_scope);
1688 gdb_assert (w->related_breakpoint->related_breakpoint == w);
1689 w->related_breakpoint->disposition = disp_del_at_next_stop;
1690 w->related_breakpoint->related_breakpoint = w->related_breakpoint;
1691 w->related_breakpoint = w;
d0fb5eae 1692 }
c1fc2657 1693 w->disposition = disp_del_at_next_stop;
d0fb5eae
JK
1694}
1695
bb9d5f81
PP
1696/* Extract a bitfield value from value VAL using the bit parameters contained in
1697 watchpoint W. */
1698
1699static struct value *
1700extract_bitfield_from_watchpoint_value (struct watchpoint *w, struct value *val)
1701{
1702 struct value *bit_val;
1703
1704 if (val == NULL)
1705 return NULL;
1706
1707 bit_val = allocate_value (value_type (val));
1708
1709 unpack_value_bitfield (bit_val,
1710 w->val_bitpos,
1711 w->val_bitsize,
1712 value_contents_for_printing (val),
1713 value_offset (val),
1714 val);
1715
1716 return bit_val;
1717}
1718
c6d81124
PA
1719/* Allocate a dummy location and add it to B, which must be a software
1720 watchpoint. This is required because even if a software watchpoint
1721 is not watching any memory, bpstat_stop_status requires a location
1722 to be able to report stops. */
1723
1724static void
1725software_watchpoint_add_no_memory_location (struct breakpoint *b,
1726 struct program_space *pspace)
1727{
1728 gdb_assert (b->type == bp_watchpoint && b->loc == NULL);
1729
1730 b->loc = allocate_bp_location (b);
1731 b->loc->pspace = pspace;
1732 b->loc->address = -1;
1733 b->loc->length = -1;
1734}
1735
1736/* Returns true if B is a software watchpoint that is not watching any
1737 memory (e.g., "watch $pc"). */
1738
f2478a7e 1739static bool
c6d81124
PA
1740is_no_memory_software_watchpoint (struct breakpoint *b)
1741{
1742 return (b->type == bp_watchpoint
1743 && b->loc != NULL
1744 && b->loc->next == NULL
1745 && b->loc->address == -1
1746 && b->loc->length == -1);
1747}
1748
567e1b4e
JB
1749/* Assuming that B is a watchpoint:
1750 - Reparse watchpoint expression, if REPARSE is non-zero
a5606eee 1751 - Evaluate expression and store the result in B->val
567e1b4e
JB
1752 - Evaluate the condition if there is one, and store the result
1753 in b->loc->cond.
a5606eee
VP
1754 - Update the list of values that must be watched in B->loc.
1755
4a64f543
MS
1756 If the watchpoint disposition is disp_del_at_next_stop, then do
1757 nothing. If this is local watchpoint that is out of scope, delete
1758 it.
1759
1760 Even with `set breakpoint always-inserted on' the watchpoints are
1761 removed + inserted on each stop here. Normal breakpoints must
1762 never be removed because they might be missed by a running thread
1763 when debugging in non-stop mode. On the other hand, hardware
1764 watchpoints (is_hardware_watchpoint; processed here) are specific
1765 to each LWP since they are stored in each LWP's hardware debug
1766 registers. Therefore, such LWP must be stopped first in order to
1767 be able to modify its hardware watchpoints.
1768
1769 Hardware watchpoints must be reset exactly once after being
1770 presented to the user. It cannot be done sooner, because it would
1771 reset the data used to present the watchpoint hit to the user. And
1772 it must not be done later because it could display the same single
1773 watchpoint hit during multiple GDB stops. Note that the latter is
1774 relevant only to the hardware watchpoint types bp_read_watchpoint
1775 and bp_access_watchpoint. False hit by bp_hardware_watchpoint is
1776 not user-visible - its hit is suppressed if the memory content has
1777 not changed.
1778
1779 The following constraints influence the location where we can reset
1780 hardware watchpoints:
1781
1782 * target_stopped_by_watchpoint and target_stopped_data_address are
1783 called several times when GDB stops.
1784
1785 [linux]
1786 * Multiple hardware watchpoints can be hit at the same time,
1787 causing GDB to stop. GDB only presents one hardware watchpoint
1788 hit at a time as the reason for stopping, and all the other hits
1789 are presented later, one after the other, each time the user
1790 requests the execution to be resumed. Execution is not resumed
1791 for the threads still having pending hit event stored in
1792 LWP_INFO->STATUS. While the watchpoint is already removed from
1793 the inferior on the first stop the thread hit event is kept being
1794 reported from its cached value by linux_nat_stopped_data_address
1795 until the real thread resume happens after the watchpoint gets
1796 presented and thus its LWP_INFO->STATUS gets reset.
1797
1798 Therefore the hardware watchpoint hit can get safely reset on the
1799 watchpoint removal from inferior. */
a79d3c27 1800
b40ce68a 1801static void
3a5c3e22 1802update_watchpoint (struct watchpoint *b, int reparse)
7270d8f2 1803{
a5606eee 1804 int within_current_scope;
a5606eee 1805 struct frame_id saved_frame_id;
66076460 1806 int frame_saved;
a5606eee 1807
f6bc2008
PA
1808 /* If this is a local watchpoint, we only want to check if the
1809 watchpoint frame is in scope if the current thread is the thread
1810 that was used to create the watchpoint. */
1811 if (!watchpoint_in_thread_scope (b))
1812 return;
1813
c1fc2657 1814 if (b->disposition == disp_del_at_next_stop)
a5606eee
VP
1815 return;
1816
66076460 1817 frame_saved = 0;
a5606eee
VP
1818
1819 /* Determine if the watchpoint is within scope. */
1820 if (b->exp_valid_block == NULL)
1821 within_current_scope = 1;
1822 else
1823 {
b5db5dfc
UW
1824 struct frame_info *fi = get_current_frame ();
1825 struct gdbarch *frame_arch = get_frame_arch (fi);
1826 CORE_ADDR frame_pc = get_frame_pc (fi);
1827
c9cf6e20
MG
1828 /* If we're at a point where the stack has been destroyed
1829 (e.g. in a function epilogue), unwinding may not work
1830 properly. Do not attempt to recreate locations at this
b5db5dfc 1831 point. See similar comments in watchpoint_check. */
c9cf6e20 1832 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
b5db5dfc 1833 return;
66076460
DJ
1834
1835 /* Save the current frame's ID so we can restore it after
dda83cd7 1836 evaluating the watchpoint expression on its own frame. */
66076460 1837 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
dda83cd7
SM
1838 took a frame parameter, so that we didn't have to change the
1839 selected frame. */
66076460
DJ
1840 frame_saved = 1;
1841 saved_frame_id = get_frame_id (get_selected_frame (NULL));
1842
a5606eee
VP
1843 fi = frame_find_by_id (b->watchpoint_frame);
1844 within_current_scope = (fi != NULL);
1845 if (within_current_scope)
1846 select_frame (fi);
1847 }
1848
b5db5dfc
UW
1849 /* We don't free locations. They are stored in the bp_location array
1850 and update_global_location_list will eventually delete them and
1851 remove breakpoints if needed. */
c1fc2657 1852 b->loc = NULL;
b5db5dfc 1853
a5606eee
VP
1854 if (within_current_scope && reparse)
1855 {
bbc13ae3 1856 const char *s;
d63d0675 1857
4d01a485 1858 b->exp.reset ();
d63d0675 1859 s = b->exp_string_reparse ? b->exp_string_reparse : b->exp_string;
1bb9788d 1860 b->exp = parse_exp_1 (&s, 0, b->exp_valid_block, 0);
a5606eee
VP
1861 /* If the meaning of expression itself changed, the old value is
1862 no longer relevant. We don't want to report a watchpoint hit
1863 to the user when the old value and the new value may actually
1864 be completely different objects. */
fa4727a6 1865 b->val = NULL;
4c1d86d9 1866 b->val_valid = false;
60e1c644
PA
1867
1868 /* Note that unlike with breakpoints, the watchpoint's condition
1869 expression is stored in the breakpoint object, not in the
1870 locations (re)created below. */
c1fc2657 1871 if (b->cond_string != NULL)
60e1c644 1872 {
4d01a485 1873 b->cond_exp.reset ();
60e1c644 1874
c1fc2657 1875 s = b->cond_string;
1bb9788d 1876 b->cond_exp = parse_exp_1 (&s, 0, b->cond_exp_valid_block, 0);
60e1c644 1877 }
a5606eee 1878 }
a5606eee
VP
1879
1880 /* If we failed to parse the expression, for example because
1881 it refers to a global variable in a not-yet-loaded shared library,
1882 don't try to insert watchpoint. We don't automatically delete
1883 such watchpoint, though, since failure to parse expression
1884 is different from out-of-scope watchpoint. */
55f6301a 1885 if (!target_has_execution ())
2d134ed3
PA
1886 {
1887 /* Without execution, memory can't change. No use to try and
1888 set watchpoint locations. The watchpoint will be reset when
1889 the target gains execution, through breakpoint_re_set. */
e8369a73
AB
1890 if (!can_use_hw_watchpoints)
1891 {
c1fc2657
SM
1892 if (b->ops->works_in_software_mode (b))
1893 b->type = bp_watchpoint;
e8369a73 1894 else
638aa5a1
AB
1895 error (_("Can't set read/access watchpoint when "
1896 "hardware watchpoints are disabled."));
e8369a73 1897 }
2d134ed3
PA
1898 }
1899 else if (within_current_scope && b->exp)
a5606eee 1900 {
a6535de1 1901 std::vector<value_ref_ptr> val_chain;
8d49165d 1902 struct value *v, *result;
2d134ed3 1903 struct program_space *frame_pspace;
a5606eee 1904
1eaebe02 1905 fetch_subexp_value (b->exp.get (), b->exp->op.get (), &v, &result,
413403fc 1906 &val_chain, false);
a5606eee 1907
a5606eee
VP
1908 /* Avoid setting b->val if it's already set. The meaning of
1909 b->val is 'the last value' user saw, and we should update
1910 it only if we reported that last value to user. As it
9c06b0b4
TJB
1911 happens, the code that reports it updates b->val directly.
1912 We don't keep track of the memory value for masked
1913 watchpoints. */
c1fc2657 1914 if (!b->val_valid && !is_masked_watchpoint (b))
fa4727a6 1915 {
bb9d5f81 1916 if (b->val_bitsize != 0)
850645cf
TT
1917 v = extract_bitfield_from_watchpoint_value (b, v);
1918 b->val = release_value (v);
4c1d86d9 1919 b->val_valid = true;
fa4727a6 1920 }
a5606eee 1921
2d134ed3
PA
1922 frame_pspace = get_frame_program_space (get_selected_frame (NULL));
1923
a5606eee 1924 /* Look at each value on the value chain. */
a6535de1
TT
1925 gdb_assert (!val_chain.empty ());
1926 for (const value_ref_ptr &iter : val_chain)
a5606eee 1927 {
a6535de1
TT
1928 v = iter.get ();
1929
a5606eee
VP
1930 /* If it's a memory location, and GDB actually needed
1931 its contents to evaluate the expression, then we
fa4727a6
DJ
1932 must watch it. If the first value returned is
1933 still lazy, that means an error occurred reading it;
1934 watch it anyway in case it becomes readable. */
a5606eee 1935 if (VALUE_LVAL (v) == lval_memory
a6535de1 1936 && (v == val_chain[0] || ! value_lazy (v)))
a5606eee
VP
1937 {
1938 struct type *vtype = check_typedef (value_type (v));
7270d8f2 1939
a5606eee
VP
1940 /* We only watch structs and arrays if user asked
1941 for it explicitly, never if they just happen to
1942 appear in the middle of some value chain. */
fa4727a6 1943 if (v == result
78134374
SM
1944 || (vtype->code () != TYPE_CODE_STRUCT
1945 && vtype->code () != TYPE_CODE_ARRAY))
a5606eee
VP
1946 {
1947 CORE_ADDR addr;
f486487f 1948 enum target_hw_bp_type type;
a5606eee 1949 struct bp_location *loc, **tmp;
bb9d5f81
PP
1950 int bitpos = 0, bitsize = 0;
1951
1952 if (value_bitsize (v) != 0)
1953 {
1954 /* Extract the bit parameters out from the bitfield
1955 sub-expression. */
1956 bitpos = value_bitpos (v);
1957 bitsize = value_bitsize (v);
1958 }
1959 else if (v == result && b->val_bitsize != 0)
1960 {
1961 /* If VAL_BITSIZE != 0 then RESULT is actually a bitfield
1962 lvalue whose bit parameters are saved in the fields
1963 VAL_BITPOS and VAL_BITSIZE. */
1964 bitpos = b->val_bitpos;
1965 bitsize = b->val_bitsize;
1966 }
a5606eee 1967
42ae5230 1968 addr = value_address (v);
bb9d5f81
PP
1969 if (bitsize != 0)
1970 {
1971 /* Skip the bytes that don't contain the bitfield. */
1972 addr += bitpos / 8;
1973 }
1974
a5606eee 1975 type = hw_write;
c1fc2657 1976 if (b->type == bp_read_watchpoint)
a5606eee 1977 type = hw_read;
c1fc2657 1978 else if (b->type == bp_access_watchpoint)
a5606eee 1979 type = hw_access;
3a5c3e22 1980
c1fc2657
SM
1981 loc = allocate_bp_location (b);
1982 for (tmp = &(b->loc); *tmp != NULL; tmp = &((*tmp)->next))
a5606eee
VP
1983 ;
1984 *tmp = loc;
8ee511af 1985 loc->gdbarch = value_type (v)->arch ();
6c95b8df
PA
1986
1987 loc->pspace = frame_pspace;
f17d9474 1988 loc->address = address_significant (loc->gdbarch, addr);
bb9d5f81
PP
1989
1990 if (bitsize != 0)
1991 {
1992 /* Just cover the bytes that make up the bitfield. */
1993 loc->length = ((bitpos % 8) + bitsize + 7) / 8;
1994 }
1995 else
1996 loc->length = TYPE_LENGTH (value_type (v));
1997
a5606eee
VP
1998 loc->watchpoint_type = type;
1999 }
2000 }
9fa40276
TJB
2001 }
2002
2003 /* Change the type of breakpoint between hardware assisted or
2004 an ordinary watchpoint depending on the hardware support
2005 and free hardware slots. REPARSE is set when the inferior
2006 is started. */
a9634178 2007 if (reparse)
9fa40276 2008 {
e09342b5 2009 int reg_cnt;
9fa40276
TJB
2010 enum bp_loc_type loc_type;
2011 struct bp_location *bl;
a5606eee 2012
a9634178 2013 reg_cnt = can_use_hardware_watchpoint (val_chain);
e09342b5
TJB
2014
2015 if (reg_cnt)
9fa40276
TJB
2016 {
2017 int i, target_resources_ok, other_type_used;
a1398e0c 2018 enum bptype type;
9fa40276 2019
a9634178
TJB
2020 /* Use an exact watchpoint when there's only one memory region to be
2021 watched, and only one debug register is needed to watch it. */
2022 b->exact = target_exact_watchpoints && reg_cnt == 1;
2023
9fa40276 2024 /* We need to determine how many resources are already
e09342b5
TJB
2025 used for all other hardware watchpoints plus this one
2026 to see if we still have enough resources to also fit
a1398e0c
PA
2027 this watchpoint in as well. */
2028
2029 /* If this is a software watchpoint, we try to turn it
2030 to a hardware one -- count resources as if B was of
2031 hardware watchpoint type. */
c1fc2657 2032 type = b->type;
a1398e0c
PA
2033 if (type == bp_watchpoint)
2034 type = bp_hardware_watchpoint;
2035
2036 /* This watchpoint may or may not have been placed on
2037 the list yet at this point (it won't be in the list
2038 if we're trying to create it for the first time,
2039 through watch_command), so always account for it
2040 manually. */
2041
2042 /* Count resources used by all watchpoints except B. */
c1fc2657 2043 i = hw_watchpoint_used_count_others (b, type, &other_type_used);
a1398e0c
PA
2044
2045 /* Add in the resources needed for B. */
c1fc2657 2046 i += hw_watchpoint_use_count (b);
a1398e0c
PA
2047
2048 target_resources_ok
2049 = target_can_use_hardware_watchpoint (type, i, other_type_used);
e09342b5 2050 if (target_resources_ok <= 0)
a9634178 2051 {
c1fc2657 2052 int sw_mode = b->ops->works_in_software_mode (b);
9c06b0b4
TJB
2053
2054 if (target_resources_ok == 0 && !sw_mode)
a9634178
TJB
2055 error (_("Target does not support this type of "
2056 "hardware watchpoint."));
9c06b0b4
TJB
2057 else if (target_resources_ok < 0 && !sw_mode)
2058 error (_("There are not enough available hardware "
2059 "resources for this watchpoint."));
a1398e0c
PA
2060
2061 /* Downgrade to software watchpoint. */
c1fc2657 2062 b->type = bp_watchpoint;
a1398e0c
PA
2063 }
2064 else
2065 {
2066 /* If this was a software watchpoint, we've just
2067 found we have enough resources to turn it to a
2068 hardware watchpoint. Otherwise, this is a
2069 nop. */
c1fc2657 2070 b->type = type;
a9634178 2071 }
9fa40276 2072 }
c1fc2657 2073 else if (!b->ops->works_in_software_mode (b))
638aa5a1
AB
2074 {
2075 if (!can_use_hw_watchpoints)
2076 error (_("Can't set read/access watchpoint when "
2077 "hardware watchpoints are disabled."));
2078 else
2079 error (_("Expression cannot be implemented with "
2080 "read/access watchpoint."));
2081 }
9fa40276 2082 else
c1fc2657 2083 b->type = bp_watchpoint;
9fa40276 2084
c1fc2657 2085 loc_type = (b->type == bp_watchpoint? bp_loc_other
9fa40276 2086 : bp_loc_hardware_watchpoint);
c1fc2657 2087 for (bl = b->loc; bl; bl = bl->next)
9fa40276
TJB
2088 bl->loc_type = loc_type;
2089 }
2090
c7437ca6
PA
2091 /* If a software watchpoint is not watching any memory, then the
2092 above left it without any location set up. But,
2093 bpstat_stop_status requires a location to be able to report
2094 stops, so make sure there's at least a dummy one. */
c1fc2657
SM
2095 if (b->type == bp_watchpoint && b->loc == NULL)
2096 software_watchpoint_add_no_memory_location (b, frame_pspace);
a5606eee
VP
2097 }
2098 else if (!within_current_scope)
7270d8f2 2099 {
ac74f770
MS
2100 printf_filtered (_("\
2101Watchpoint %d deleted because the program has left the block\n\
2102in which its expression is valid.\n"),
c1fc2657 2103 b->number);
d0fb5eae 2104 watchpoint_del_at_next_stop (b);
7270d8f2 2105 }
a5606eee
VP
2106
2107 /* Restore the selected frame. */
66076460
DJ
2108 if (frame_saved)
2109 select_frame (frame_find_by_id (saved_frame_id));
7270d8f2
OF
2110}
2111
a5606eee 2112
74960c60 2113/* Returns 1 iff breakpoint location should be
1e4d1764
YQ
2114 inserted in the inferior. We don't differentiate the type of BL's owner
2115 (breakpoint vs. tracepoint), although insert_location in tracepoint's
2116 breakpoint_ops is not defined, because in insert_bp_location,
2117 tracepoint's insert_location will not be called. */
74960c60 2118static int
35df4500 2119should_be_inserted (struct bp_location *bl)
74960c60 2120{
35df4500 2121 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
74960c60
VP
2122 return 0;
2123
35df4500 2124 if (bl->owner->disposition == disp_del_at_next_stop)
74960c60
VP
2125 return 0;
2126
b5fa468f
TBA
2127 if (!bl->enabled || bl->disabled_by_cond
2128 || bl->shlib_disabled || bl->duplicate)
74960c60
VP
2129 return 0;
2130
f8eba3c6
TT
2131 if (user_breakpoint_p (bl->owner) && bl->pspace->executing_startup)
2132 return 0;
2133
56710373
PA
2134 /* This is set for example, when we're attached to the parent of a
2135 vfork, and have detached from the child. The child is running
2136 free, and we expect it to do an exec or exit, at which point the
2137 OS makes the parent schedulable again (and the target reports
2138 that the vfork is done). Until the child is done with the shared
2139 memory region, do not insert breakpoints in the parent, otherwise
2140 the child could still trip on the parent's breakpoints. Since
2141 the parent is blocked anyway, it won't miss any breakpoint. */
35df4500 2142 if (bl->pspace->breakpoints_not_allowed)
56710373
PA
2143 return 0;
2144
31e77af2 2145 /* Don't insert a breakpoint if we're trying to step past its
21edc42f
YQ
2146 location, except if the breakpoint is a single-step breakpoint,
2147 and the breakpoint's thread is the thread which is stepping past
2148 a breakpoint. */
31e77af2
PA
2149 if ((bl->loc_type == bp_loc_software_breakpoint
2150 || bl->loc_type == bp_loc_hardware_breakpoint)
2151 && stepping_past_instruction_at (bl->pspace->aspace,
21edc42f
YQ
2152 bl->address)
2153 /* The single-step breakpoint may be inserted at the location
2154 we're trying to step if the instruction branches to itself.
2155 However, the instruction won't be executed at all and it may
2156 break the semantics of the instruction, for example, the
2157 instruction is a conditional branch or updates some flags.
2158 We can't fix it unless GDB is able to emulate the instruction
2159 or switch to displaced stepping. */
2160 && !(bl->owner->type == bp_single_step
2161 && thread_is_stepping_over_breakpoint (bl->owner->thread)))
e558d7c1 2162 {
1eb8556f
SM
2163 infrun_debug_printf ("skipping breakpoint: stepping past insn at: %s",
2164 paddress (bl->gdbarch, bl->address));
e558d7c1
PA
2165 return 0;
2166 }
31e77af2 2167
963f9c80
PA
2168 /* Don't insert watchpoints if we're trying to step past the
2169 instruction that triggered one. */
2170 if ((bl->loc_type == bp_loc_hardware_watchpoint)
2171 && stepping_past_nonsteppable_watchpoint ())
2172 {
1eb8556f 2173 infrun_debug_printf ("stepping past non-steppable watchpoint. "
ae1f4d2d 2174 "skipping watchpoint at %s:%d",
1eb8556f 2175 paddress (bl->gdbarch, bl->address), bl->length);
963f9c80
PA
2176 return 0;
2177 }
2178
74960c60
VP
2179 return 1;
2180}
2181
934709f0
PW
2182/* Same as should_be_inserted but does the check assuming
2183 that the location is not duplicated. */
2184
2185static int
2186unduplicated_should_be_inserted (struct bp_location *bl)
2187{
2188 int result;
2189 const int save_duplicate = bl->duplicate;
2190
2191 bl->duplicate = 0;
2192 result = should_be_inserted (bl);
2193 bl->duplicate = save_duplicate;
2194 return result;
2195}
2196
b775012e
LM
2197/* Parses a conditional described by an expression COND into an
2198 agent expression bytecode suitable for evaluation
2199 by the bytecode interpreter. Return NULL if there was
2200 any error during parsing. */
2201
833177a4 2202static agent_expr_up
b775012e
LM
2203parse_cond_to_aexpr (CORE_ADDR scope, struct expression *cond)
2204{
833177a4 2205 if (cond == NULL)
b775012e
LM
2206 return NULL;
2207
833177a4
PA
2208 agent_expr_up aexpr;
2209
b775012e
LM
2210 /* We don't want to stop processing, so catch any errors
2211 that may show up. */
a70b8144 2212 try
b775012e 2213 {
036e657b 2214 aexpr = gen_eval_for_expr (scope, cond);
b775012e
LM
2215 }
2216
230d2906 2217 catch (const gdb_exception_error &ex)
b775012e
LM
2218 {
2219 /* If we got here, it means the condition could not be parsed to a valid
2220 bytecode expression and thus can't be evaluated on the target's side.
2221 It's no use iterating through the conditions. */
b775012e
LM
2222 }
2223
2224 /* We have a valid agent expression. */
2225 return aexpr;
2226}
2227
2228/* Based on location BL, create a list of breakpoint conditions to be
2229 passed on to the target. If we have duplicated locations with different
2230 conditions, we will add such conditions to the list. The idea is that the
2231 target will evaluate the list of conditions and will only notify GDB when
2232 one of them is true. */
2233
2234static void
2235build_target_condition_list (struct bp_location *bl)
2236{
2237 struct bp_location **locp = NULL, **loc2p;
2238 int null_condition_or_parse_error = 0;
2239 int modified = bl->needs_update;
2240 struct bp_location *loc;
2241
8b4f3082 2242 /* Release conditions left over from a previous insert. */
3cde5c42 2243 bl->target_info.conditions.clear ();
8b4f3082 2244
b775012e
LM
2245 /* This is only meaningful if the target is
2246 evaluating conditions and if the user has
2247 opted for condition evaluation on the target's
2248 side. */
2249 if (gdb_evaluates_breakpoint_condition_p ()
2250 || !target_supports_evaluation_of_breakpoint_conditions ())
2251 return;
2252
2253 /* Do a first pass to check for locations with no assigned
7f32a4d5
PA
2254 conditions or conditions that fail to parse to a valid agent
2255 expression bytecode. If any of these happen, then it's no use to
2256 send conditions to the target since this location will always
2257 trigger and generate a response back to GDB. Note we consider
2258 all locations at the same address irrespective of type, i.e.,
2259 even if the locations aren't considered duplicates (e.g.,
2260 software breakpoint and hardware breakpoint at the same
2261 address). */
b775012e
LM
2262 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2263 {
2264 loc = (*loc2p);
2265 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2266 {
2267 if (modified)
2268 {
b775012e
LM
2269 /* Re-parse the conditions since something changed. In that
2270 case we already freed the condition bytecodes (see
2271 force_breakpoint_reinsertion). We just
2272 need to parse the condition to bytecodes again. */
833177a4
PA
2273 loc->cond_bytecode = parse_cond_to_aexpr (bl->address,
2274 loc->cond.get ());
b775012e
LM
2275 }
2276
2277 /* If we have a NULL bytecode expression, it means something
2278 went wrong or we have a null condition expression. */
2279 if (!loc->cond_bytecode)
2280 {
2281 null_condition_or_parse_error = 1;
2282 break;
2283 }
2284 }
2285 }
2286
2287 /* If any of these happened, it means we will have to evaluate the conditions
2288 for the location's address on gdb's side. It is no use keeping bytecodes
2289 for all the other duplicate locations, thus we free all of them here.
2290
2291 This is so we have a finer control over which locations' conditions are
2292 being evaluated by GDB or the remote stub. */
2293 if (null_condition_or_parse_error)
2294 {
2295 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2296 {
2297 loc = (*loc2p);
2298 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2299 {
2300 /* Only go as far as the first NULL bytecode is
2301 located. */
2302 if (!loc->cond_bytecode)
2303 return;
2304
833177a4 2305 loc->cond_bytecode.reset ();
b775012e
LM
2306 }
2307 }
2308 }
2309
7f32a4d5
PA
2310 /* No NULL conditions or failed bytecode generation. Build a
2311 condition list for this location's address. If we have software
2312 and hardware locations at the same address, they aren't
2313 considered duplicates, but we still marge all the conditions
2314 anyway, as it's simpler, and doesn't really make a practical
2315 difference. */
b775012e
LM
2316 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2317 {
2318 loc = (*loc2p);
2319 if (loc->cond
2320 && is_breakpoint (loc->owner)
2321 && loc->pspace->num == bl->pspace->num
2322 && loc->owner->enable_state == bp_enabled
b5fa468f
TBA
2323 && loc->enabled
2324 && !loc->disabled_by_cond)
3cde5c42
PA
2325 {
2326 /* Add the condition to the vector. This will be used later
2327 to send the conditions to the target. */
2328 bl->target_info.conditions.push_back (loc->cond_bytecode.get ());
2329 }
b775012e
LM
2330 }
2331
2332 return;
2333}
2334
d3ce09f5
SS
2335/* Parses a command described by string CMD into an agent expression
2336 bytecode suitable for evaluation by the bytecode interpreter.
2337 Return NULL if there was any error during parsing. */
2338
833177a4 2339static agent_expr_up
d3ce09f5
SS
2340parse_cmd_to_aexpr (CORE_ADDR scope, char *cmd)
2341{
bbc13ae3
KS
2342 const char *cmdrest;
2343 const char *format_start, *format_end;
d3ce09f5
SS
2344 struct gdbarch *gdbarch = get_current_arch ();
2345
833177a4 2346 if (cmd == NULL)
d3ce09f5
SS
2347 return NULL;
2348
2349 cmdrest = cmd;
2350
2351 if (*cmdrest == ',')
2352 ++cmdrest;
f1735a53 2353 cmdrest = skip_spaces (cmdrest);
d3ce09f5
SS
2354
2355 if (*cmdrest++ != '"')
2356 error (_("No format string following the location"));
2357
2358 format_start = cmdrest;
2359
8e481c3b 2360 format_pieces fpieces (&cmdrest);
d3ce09f5
SS
2361
2362 format_end = cmdrest;
2363
2364 if (*cmdrest++ != '"')
2365 error (_("Bad format string, non-terminated '\"'."));
2366
f1735a53 2367 cmdrest = skip_spaces (cmdrest);
d3ce09f5
SS
2368
2369 if (!(*cmdrest == ',' || *cmdrest == '\0'))
2370 error (_("Invalid argument syntax"));
2371
2372 if (*cmdrest == ',')
2373 cmdrest++;
f1735a53 2374 cmdrest = skip_spaces (cmdrest);
d3ce09f5
SS
2375
2376 /* For each argument, make an expression. */
2377
8e481c3b 2378 std::vector<struct expression *> argvec;
d3ce09f5
SS
2379 while (*cmdrest != '\0')
2380 {
bbc13ae3 2381 const char *cmd1;
d3ce09f5
SS
2382
2383 cmd1 = cmdrest;
4d01a485 2384 expression_up expr = parse_exp_1 (&cmd1, scope, block_for_pc (scope), 1);
8e481c3b 2385 argvec.push_back (expr.release ());
d3ce09f5
SS
2386 cmdrest = cmd1;
2387 if (*cmdrest == ',')
2388 ++cmdrest;
2389 }
2390
833177a4
PA
2391 agent_expr_up aexpr;
2392
d3ce09f5
SS
2393 /* We don't want to stop processing, so catch any errors
2394 that may show up. */
a70b8144 2395 try
d3ce09f5 2396 {
036e657b
JB
2397 aexpr = gen_printf (scope, gdbarch, 0, 0,
2398 format_start, format_end - format_start,
8e481c3b 2399 argvec.size (), argvec.data ());
d3ce09f5 2400 }
230d2906 2401 catch (const gdb_exception_error &ex)
d3ce09f5
SS
2402 {
2403 /* If we got here, it means the command could not be parsed to a valid
2404 bytecode expression and thus can't be evaluated on the target's side.
2405 It's no use iterating through the other commands. */
d3ce09f5 2406 }
492d29ea 2407
d3ce09f5
SS
2408 /* We have a valid agent expression, return it. */
2409 return aexpr;
2410}
2411
2412/* Based on location BL, create a list of breakpoint commands to be
2413 passed on to the target. If we have duplicated locations with
2414 different commands, we will add any such to the list. */
2415
2416static void
2417build_target_command_list (struct bp_location *bl)
2418{
2419 struct bp_location **locp = NULL, **loc2p;
2420 int null_command_or_parse_error = 0;
2421 int modified = bl->needs_update;
2422 struct bp_location *loc;
2423
3cde5c42
PA
2424 /* Clear commands left over from a previous insert. */
2425 bl->target_info.tcommands.clear ();
8b4f3082 2426
41fac0cf 2427 if (!target_can_run_breakpoint_commands ())
d3ce09f5
SS
2428 return;
2429
41fac0cf
PA
2430 /* For now, limit to agent-style dprintf breakpoints. */
2431 if (dprintf_style != dprintf_style_agent)
d3ce09f5
SS
2432 return;
2433
7f32a4d5
PA
2434 /* For now, if we have any location at the same address that isn't a
2435 dprintf, don't install the target-side commands, as that would
2436 make the breakpoint not be reported to the core, and we'd lose
41fac0cf
PA
2437 control. */
2438 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2439 {
2440 loc = (*loc2p);
2441 if (is_breakpoint (loc->owner)
2442 && loc->pspace->num == bl->pspace->num
2443 && loc->owner->type != bp_dprintf)
2444 return;
2445 }
2446
d3ce09f5
SS
2447 /* Do a first pass to check for locations with no assigned
2448 conditions or conditions that fail to parse to a valid agent expression
2449 bytecode. If any of these happen, then it's no use to send conditions
2450 to the target since this location will always trigger and generate a
2451 response back to GDB. */
2452 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2453 {
2454 loc = (*loc2p);
2455 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2456 {
2457 if (modified)
2458 {
d3ce09f5
SS
2459 /* Re-parse the commands since something changed. In that
2460 case we already freed the command bytecodes (see
2461 force_breakpoint_reinsertion). We just
2462 need to parse the command to bytecodes again. */
833177a4
PA
2463 loc->cmd_bytecode
2464 = parse_cmd_to_aexpr (bl->address,
2465 loc->owner->extra_string);
d3ce09f5
SS
2466 }
2467
2468 /* If we have a NULL bytecode expression, it means something
2469 went wrong or we have a null command expression. */
2470 if (!loc->cmd_bytecode)
2471 {
2472 null_command_or_parse_error = 1;
2473 break;
2474 }
2475 }
2476 }
2477
2478 /* If anything failed, then we're not doing target-side commands,
2479 and so clean up. */
2480 if (null_command_or_parse_error)
2481 {
2482 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2483 {
2484 loc = (*loc2p);
2485 if (is_breakpoint (loc->owner)
2486 && loc->pspace->num == bl->pspace->num)
2487 {
2488 /* Only go as far as the first NULL bytecode is
2489 located. */
40fb6c5e 2490 if (loc->cmd_bytecode == NULL)
d3ce09f5
SS
2491 return;
2492
833177a4 2493 loc->cmd_bytecode.reset ();
d3ce09f5
SS
2494 }
2495 }
2496 }
2497
7f32a4d5
PA
2498 /* No NULL commands or failed bytecode generation. Build a command
2499 list for all duplicate locations at this location's address.
2500 Note that here we must care for whether the breakpoint location
2501 types are considered duplicates, otherwise, say, if we have a
2502 software and hardware location at the same address, the target
2503 could end up running the commands twice. For the moment, we only
2504 support targets-side commands with dprintf, but it doesn't hurt
2505 to be pedantically correct in case that changes. */
d3ce09f5
SS
2506 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2507 {
2508 loc = (*loc2p);
7f32a4d5
PA
2509 if (breakpoint_locations_match (bl, loc)
2510 && loc->owner->extra_string
d3ce09f5
SS
2511 && is_breakpoint (loc->owner)
2512 && loc->pspace->num == bl->pspace->num
2513 && loc->owner->enable_state == bp_enabled
b5fa468f
TBA
2514 && loc->enabled
2515 && !loc->disabled_by_cond)
3cde5c42
PA
2516 {
2517 /* Add the command to the vector. This will be used later
2518 to send the commands to the target. */
2519 bl->target_info.tcommands.push_back (loc->cmd_bytecode.get ());
2520 }
d3ce09f5
SS
2521 }
2522
2523 bl->target_info.persist = 0;
2524 /* Maybe flag this location as persistent. */
2525 if (bl->owner->type == bp_dprintf && disconnected_dprintf)
2526 bl->target_info.persist = 1;
2527}
2528
833b7ab5
YQ
2529/* Return the kind of breakpoint on address *ADDR. Get the kind
2530 of breakpoint according to ADDR except single-step breakpoint.
2531 Get the kind of single-step breakpoint according to the current
2532 registers state. */
cd6c3b4f
YQ
2533
2534static int
2535breakpoint_kind (struct bp_location *bl, CORE_ADDR *addr)
2536{
833b7ab5
YQ
2537 if (bl->owner->type == bp_single_step)
2538 {
2539 struct thread_info *thr = find_thread_global_id (bl->owner->thread);
2540 struct regcache *regcache;
2541
00431a78 2542 regcache = get_thread_regcache (thr);
833b7ab5
YQ
2543
2544 return gdbarch_breakpoint_kind_from_current_state (bl->gdbarch,
2545 regcache, addr);
2546 }
2547 else
2548 return gdbarch_breakpoint_kind_from_pc (bl->gdbarch, addr);
cd6c3b4f
YQ
2549}
2550
35df4500
TJB
2551/* Insert a low-level "breakpoint" of some type. BL is the breakpoint
2552 location. Any error messages are printed to TMP_ERROR_STREAM; and
3fbb6ffa 2553 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
c30eee59
TJB
2554 Returns 0 for success, 1 if the bp_location type is not supported or
2555 -1 for failure.
879bfdc2 2556
4a64f543
MS
2557 NOTE drow/2003-09-09: This routine could be broken down to an
2558 object-style method for each breakpoint or catchpoint type. */
26bb91f3 2559static int
35df4500 2560insert_bp_location (struct bp_location *bl,
26bb91f3 2561 struct ui_file *tmp_error_stream,
3fbb6ffa 2562 int *disabled_breaks,
dd61ec5c
MW
2563 int *hw_breakpoint_error,
2564 int *hw_bp_error_explained_already)
879bfdc2 2565{
cc06b668 2566 gdb_exception bp_excpt;
879bfdc2 2567
b775012e 2568 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
879bfdc2
DJ
2569 return 0;
2570
35c63cd8
JB
2571 /* Note we don't initialize bl->target_info, as that wipes out
2572 the breakpoint location's shadow_contents if the breakpoint
2573 is still inserted at that location. This in turn breaks
2574 target_read_memory which depends on these buffers when
2575 a memory read is requested at the breakpoint location:
2576 Once the target_info has been wiped, we fail to see that
2577 we have a breakpoint inserted at that address and thus
2578 read the breakpoint instead of returning the data saved in
2579 the breakpoint location's shadow contents. */
0d5ed153 2580 bl->target_info.reqstd_address = bl->address;
35df4500 2581 bl->target_info.placed_address_space = bl->pspace->aspace;
f1310107 2582 bl->target_info.length = bl->length;
8181d85f 2583
b775012e
LM
2584 /* When working with target-side conditions, we must pass all the conditions
2585 for the same breakpoint address down to the target since GDB will not
2586 insert those locations. With a list of breakpoint conditions, the target
2587 can decide when to stop and notify GDB. */
2588
2589 if (is_breakpoint (bl->owner))
2590 {
2591 build_target_condition_list (bl);
d3ce09f5
SS
2592 build_target_command_list (bl);
2593 /* Reset the modification marker. */
b775012e
LM
2594 bl->needs_update = 0;
2595 }
2596
7f32a4d5
PA
2597 /* If "set breakpoint auto-hw" is "on" and a software breakpoint was
2598 set at a read-only address, then a breakpoint location will have
2599 been changed to hardware breakpoint before we get here. If it is
2600 "off" however, error out before actually trying to insert the
2601 breakpoint, with a nicer error message. */
35df4500 2602 if (bl->loc_type == bp_loc_software_breakpoint
7f32a4d5 2603 && !automatic_hardware_breakpoints)
879bfdc2 2604 {
7f32a4d5 2605 mem_region *mr = lookup_mem_region (bl->address);
cc59ec59 2606
7f32a4d5
PA
2607 if (mr != nullptr && mr->attrib.mode != MEM_RW)
2608 {
2609 fprintf_unfiltered (tmp_error_stream,
2610 _("Cannot insert breakpoint %d.\n"
2611 "Cannot set software breakpoint "
2612 "at read-only address %s\n"),
2613 bl->owner->number,
2614 paddress (bl->gdbarch, bl->address));
2615 return 1;
765dc015 2616 }
7f32a4d5
PA
2617 }
2618
2619 if (bl->loc_type == bp_loc_software_breakpoint
2620 || bl->loc_type == bp_loc_hardware_breakpoint)
2621 {
879bfdc2
DJ
2622 /* First check to see if we have to handle an overlay. */
2623 if (overlay_debugging == ovly_off
35df4500
TJB
2624 || bl->section == NULL
2625 || !(section_is_overlay (bl->section)))
879bfdc2
DJ
2626 {
2627 /* No overlay handling: just set the breakpoint. */
a70b8144 2628 try
dd61ec5c 2629 {
0000e5cc
PA
2630 int val;
2631
dd61ec5c 2632 val = bl->owner->ops->insert_location (bl);
0000e5cc 2633 if (val)
688fca4f 2634 bp_excpt = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
dd61ec5c 2635 }
94aeb44b 2636 catch (gdb_exception &e)
dd61ec5c 2637 {
94aeb44b 2638 bp_excpt = std::move (e);
dd61ec5c 2639 }
879bfdc2
DJ
2640 }
2641 else
2642 {
4a64f543 2643 /* This breakpoint is in an overlay section.
879bfdc2
DJ
2644 Shall we set a breakpoint at the LMA? */
2645 if (!overlay_events_enabled)
2646 {
2647 /* Yes -- overlay event support is not active,
2648 so we must try to set a breakpoint at the LMA.
2649 This will not work for a hardware breakpoint. */
35df4500 2650 if (bl->loc_type == bp_loc_hardware_breakpoint)
8a3fe4f8 2651 warning (_("hardware breakpoint %d not supported in overlay!"),
35df4500 2652 bl->owner->number);
879bfdc2
DJ
2653 else
2654 {
35df4500
TJB
2655 CORE_ADDR addr = overlay_unmapped_address (bl->address,
2656 bl->section);
879bfdc2 2657 /* Set a software (trap) breakpoint at the LMA. */
35df4500 2658 bl->overlay_target_info = bl->target_info;
0d5ed153 2659 bl->overlay_target_info.reqstd_address = addr;
0000e5cc
PA
2660
2661 /* No overlay handling: just set the breakpoint. */
a70b8144 2662 try
0000e5cc
PA
2663 {
2664 int val;
2665
579c6ad9 2666 bl->overlay_target_info.kind
cd6c3b4f
YQ
2667 = breakpoint_kind (bl, &addr);
2668 bl->overlay_target_info.placed_address = addr;
0000e5cc
PA
2669 val = target_insert_breakpoint (bl->gdbarch,
2670 &bl->overlay_target_info);
2671 if (val)
688fca4f
PA
2672 bp_excpt
2673 = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
0000e5cc 2674 }
94aeb44b 2675 catch (gdb_exception &e)
0000e5cc 2676 {
94aeb44b 2677 bp_excpt = std::move (e);
0000e5cc
PA
2678 }
2679
688fca4f 2680 if (bp_excpt.reason != 0)
99361f52 2681 fprintf_unfiltered (tmp_error_stream,
3e43a32a
MS
2682 "Overlay breakpoint %d "
2683 "failed: in ROM?\n",
35df4500 2684 bl->owner->number);
879bfdc2
DJ
2685 }
2686 }
2687 /* Shall we set a breakpoint at the VMA? */
35df4500 2688 if (section_is_mapped (bl->section))
879bfdc2
DJ
2689 {
2690 /* Yes. This overlay section is mapped into memory. */
a70b8144 2691 try
dda83cd7 2692 {
0000e5cc
PA
2693 int val;
2694
dda83cd7 2695 val = bl->owner->ops->insert_location (bl);
0000e5cc 2696 if (val)
688fca4f 2697 bp_excpt = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
dda83cd7 2698 }
94aeb44b 2699 catch (gdb_exception &e)
dda83cd7 2700 {
94aeb44b 2701 bp_excpt = std::move (e);
dda83cd7 2702 }
879bfdc2
DJ
2703 }
2704 else
2705 {
2706 /* No. This breakpoint will not be inserted.
2707 No error, but do not mark the bp as 'inserted'. */
2708 return 0;
2709 }
2710 }
2711
688fca4f 2712 if (bp_excpt.reason != 0)
879bfdc2
DJ
2713 {
2714 /* Can't set the breakpoint. */
0000e5cc
PA
2715
2716 /* In some cases, we might not be able to insert a
2717 breakpoint in a shared library that has already been
2718 removed, but we have not yet processed the shlib unload
2719 event. Unfortunately, some targets that implement
076855f9
PA
2720 breakpoint insertion themselves can't tell why the
2721 breakpoint insertion failed (e.g., the remote target
2722 doesn't define error codes), so we must treat generic
2723 errors as memory errors. */
688fca4f
PA
2724 if (bp_excpt.reason == RETURN_ERROR
2725 && (bp_excpt.error == GENERIC_ERROR
2726 || bp_excpt.error == MEMORY_ERROR)
076855f9 2727 && bl->loc_type == bp_loc_software_breakpoint
08351840 2728 && (solib_name_from_address (bl->pspace, bl->address)
d03de421
PA
2729 || shared_objfile_contains_address_p (bl->pspace,
2730 bl->address)))
879bfdc2 2731 {
4a64f543 2732 /* See also: disable_breakpoints_in_shlibs. */
35df4500 2733 bl->shlib_disabled = 1;
76727919 2734 gdb::observers::breakpoint_modified.notify (bl->owner);
3fbb6ffa
TJB
2735 if (!*disabled_breaks)
2736 {
2737 fprintf_unfiltered (tmp_error_stream,
2738 "Cannot insert breakpoint %d.\n",
2739 bl->owner->number);
2740 fprintf_unfiltered (tmp_error_stream,
2741 "Temporarily disabling shared "
2742 "library breakpoints:\n");
2743 }
2744 *disabled_breaks = 1;
879bfdc2 2745 fprintf_unfiltered (tmp_error_stream,
35df4500 2746 "breakpoint #%d\n", bl->owner->number);
0000e5cc 2747 return 0;
879bfdc2
DJ
2748 }
2749 else
879bfdc2 2750 {
35df4500 2751 if (bl->loc_type == bp_loc_hardware_breakpoint)
879bfdc2 2752 {
0000e5cc 2753 *hw_breakpoint_error = 1;
688fca4f 2754 *hw_bp_error_explained_already = bp_excpt.message != NULL;
dda83cd7
SM
2755 fprintf_unfiltered (tmp_error_stream,
2756 "Cannot insert hardware breakpoint %d%s",
2757 bl->owner->number,
688fca4f 2758 bp_excpt.message ? ":" : ".\n");
dda83cd7
SM
2759 if (bp_excpt.message != NULL)
2760 fprintf_unfiltered (tmp_error_stream, "%s.\n",
3d6e9d23 2761 bp_excpt.what ());
879bfdc2
DJ
2762 }
2763 else
2764 {
688fca4f 2765 if (bp_excpt.message == NULL)
0000e5cc 2766 {
1ccbe998 2767 std::string message
0000e5cc
PA
2768 = memory_error_message (TARGET_XFER_E_IO,
2769 bl->gdbarch, bl->address);
0000e5cc
PA
2770
2771 fprintf_unfiltered (tmp_error_stream,
2772 "Cannot insert breakpoint %d.\n"
2773 "%s\n",
1ccbe998 2774 bl->owner->number, message.c_str ());
0000e5cc
PA
2775 }
2776 else
2777 {
2778 fprintf_unfiltered (tmp_error_stream,
2779 "Cannot insert breakpoint %d: %s\n",
2780 bl->owner->number,
3d6e9d23 2781 bp_excpt.what ());
0000e5cc 2782 }
879bfdc2 2783 }
0000e5cc 2784 return 1;
879bfdc2
DJ
2785
2786 }
2787 }
2788 else
35df4500 2789 bl->inserted = 1;
879bfdc2 2790
0000e5cc 2791 return 0;
879bfdc2
DJ
2792 }
2793
35df4500 2794 else if (bl->loc_type == bp_loc_hardware_watchpoint
879bfdc2 2795 /* NOTE drow/2003-09-08: This state only exists for removing
4a64f543 2796 watchpoints. It's not clear that it's necessary... */
35df4500 2797 && bl->owner->disposition != disp_del_at_next_stop)
879bfdc2 2798 {
0000e5cc
PA
2799 int val;
2800
77b06cd7
TJB
2801 gdb_assert (bl->owner->ops != NULL
2802 && bl->owner->ops->insert_location != NULL);
2803
2804 val = bl->owner->ops->insert_location (bl);
85d721b8
PA
2805
2806 /* If trying to set a read-watchpoint, and it turns out it's not
2807 supported, try emulating one with an access watchpoint. */
35df4500 2808 if (val == 1 && bl->watchpoint_type == hw_read)
85d721b8
PA
2809 {
2810 struct bp_location *loc, **loc_temp;
2811
2812 /* But don't try to insert it, if there's already another
2813 hw_access location that would be considered a duplicate
2814 of this one. */
2815 ALL_BP_LOCATIONS (loc, loc_temp)
35df4500 2816 if (loc != bl
85d721b8 2817 && loc->watchpoint_type == hw_access
35df4500 2818 && watchpoint_locations_match (bl, loc))
85d721b8 2819 {
35df4500
TJB
2820 bl->duplicate = 1;
2821 bl->inserted = 1;
2822 bl->target_info = loc->target_info;
2823 bl->watchpoint_type = hw_access;
85d721b8
PA
2824 val = 0;
2825 break;
2826 }
2827
2828 if (val == 1)
2829 {
77b06cd7
TJB
2830 bl->watchpoint_type = hw_access;
2831 val = bl->owner->ops->insert_location (bl);
2832
2833 if (val)
2834 /* Back to the original value. */
2835 bl->watchpoint_type = hw_read;
85d721b8
PA
2836 }
2837 }
2838
35df4500 2839 bl->inserted = (val == 0);
879bfdc2
DJ
2840 }
2841
35df4500 2842 else if (bl->owner->type == bp_catchpoint)
879bfdc2 2843 {
0000e5cc
PA
2844 int val;
2845
77b06cd7
TJB
2846 gdb_assert (bl->owner->ops != NULL
2847 && bl->owner->ops->insert_location != NULL);
2848
2849 val = bl->owner->ops->insert_location (bl);
2850 if (val)
2851 {
2852 bl->owner->enable_state = bp_disabled;
2853
2854 if (val == 1)
2855 warning (_("\
2856Error inserting catchpoint %d: Your system does not support this type\n\
2857of catchpoint."), bl->owner->number);
2858 else
2859 warning (_("Error inserting catchpoint %d."), bl->owner->number);
2860 }
2861
2862 bl->inserted = (val == 0);
1640b821
DJ
2863
2864 /* We've already printed an error message if there was a problem
2865 inserting this catchpoint, and we've disabled the catchpoint,
2866 so just return success. */
2867 return 0;
879bfdc2
DJ
2868 }
2869
2870 return 0;
2871}
2872
6c95b8df
PA
2873/* This function is called when program space PSPACE is about to be
2874 deleted. It takes care of updating breakpoints to not reference
2875 PSPACE anymore. */
2876
2877void
2878breakpoint_program_space_exit (struct program_space *pspace)
2879{
2880 struct breakpoint *b, *b_temp;
876fa593 2881 struct bp_location *loc, **loc_temp;
6c95b8df
PA
2882
2883 /* Remove any breakpoint that was set through this program space. */
2884 ALL_BREAKPOINTS_SAFE (b, b_temp)
2885 {
2886 if (b->pspace == pspace)
2887 delete_breakpoint (b);
2888 }
2889
2890 /* Breakpoints set through other program spaces could have locations
2891 bound to PSPACE as well. Remove those. */
876fa593 2892 ALL_BP_LOCATIONS (loc, loc_temp)
6c95b8df
PA
2893 {
2894 struct bp_location *tmp;
2895
2896 if (loc->pspace == pspace)
2897 {
2bdf28a0 2898 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
6c95b8df
PA
2899 if (loc->owner->loc == loc)
2900 loc->owner->loc = loc->next;
2901 else
2902 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
2903 if (tmp->next == loc)
2904 {
2905 tmp->next = loc->next;
2906 break;
2907 }
2908 }
2909 }
2910
2911 /* Now update the global location list to permanently delete the
2912 removed locations above. */
44702360 2913 update_global_location_list (UGLL_DONT_INSERT);
6c95b8df
PA
2914}
2915
74960c60
VP
2916/* Make sure all breakpoints are inserted in inferior.
2917 Throws exception on any error.
2918 A breakpoint that is already inserted won't be inserted
2919 again, so calling this function twice is safe. */
2920void
2921insert_breakpoints (void)
2922{
2923 struct breakpoint *bpt;
2924
2925 ALL_BREAKPOINTS (bpt)
2926 if (is_hardware_watchpoint (bpt))
3a5c3e22
PA
2927 {
2928 struct watchpoint *w = (struct watchpoint *) bpt;
2929
2930 update_watchpoint (w, 0 /* don't reparse. */);
2931 }
74960c60 2932
04086b45
PA
2933 /* Updating watchpoints creates new locations, so update the global
2934 location list. Explicitly tell ugll to insert locations and
7f32a4d5
PA
2935 ignore breakpoints_always_inserted_mode. Also,
2936 update_global_location_list tries to "upgrade" software
2937 breakpoints to hardware breakpoints to handle "set breakpoint
2938 auto-hw", so we need to call it even if we don't have new
2939 locations. */
04086b45 2940 update_global_location_list (UGLL_INSERT);
74960c60
VP
2941}
2942
20388dd6
YQ
2943/* Invoke CALLBACK for each of bp_location. */
2944
2945void
0406545d 2946iterate_over_bp_locations (gdb::function_view<void (bp_location *)> callback)
20388dd6
YQ
2947{
2948 struct bp_location *loc, **loc_tmp;
2949
2950 ALL_BP_LOCATIONS (loc, loc_tmp)
2951 {
0406545d 2952 callback (loc);
20388dd6
YQ
2953 }
2954}
2955
b775012e
LM
2956/* This is used when we need to synch breakpoint conditions between GDB and the
2957 target. It is the case with deleting and disabling of breakpoints when using
2958 always-inserted mode. */
2959
2960static void
2961update_inserted_breakpoint_locations (void)
2962{
2963 struct bp_location *bl, **blp_tmp;
2964 int error_flag = 0;
2965 int val = 0;
2966 int disabled_breaks = 0;
2967 int hw_breakpoint_error = 0;
dd61ec5c 2968 int hw_bp_details_reported = 0;
b775012e 2969
d7e74731 2970 string_file tmp_error_stream;
b775012e
LM
2971
2972 /* Explicitly mark the warning -- this will only be printed if
2973 there was an error. */
d7e74731 2974 tmp_error_stream.puts ("Warning:\n");
b775012e 2975
5ed8105e 2976 scoped_restore_current_pspace_and_thread restore_pspace_thread;
b775012e
LM
2977
2978 ALL_BP_LOCATIONS (bl, blp_tmp)
2979 {
2980 /* We only want to update software breakpoints and hardware
2981 breakpoints. */
2982 if (!is_breakpoint (bl->owner))
2983 continue;
2984
2985 /* We only want to update locations that are already inserted
2986 and need updating. This is to avoid unwanted insertion during
2987 deletion of breakpoints. */
4daf1902 2988 if (!bl->inserted || !bl->needs_update)
b775012e
LM
2989 continue;
2990
2991 switch_to_program_space_and_thread (bl->pspace);
2992
2993 /* For targets that support global breakpoints, there's no need
2994 to select an inferior to insert breakpoint to. In fact, even
2995 if we aren't attached to any process yet, we should still
2996 insert breakpoints. */
f5656ead 2997 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
55f6301a 2998 && (inferior_ptid == null_ptid || !target_has_execution ()))
b775012e
LM
2999 continue;
3000
d7e74731 3001 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
dd61ec5c 3002 &hw_breakpoint_error, &hw_bp_details_reported);
b775012e
LM
3003 if (val)
3004 error_flag = val;
3005 }
3006
3007 if (error_flag)
3008 {
223ffa71 3009 target_terminal::ours_for_output ();
b775012e
LM
3010 error_stream (tmp_error_stream);
3011 }
b775012e
LM
3012}
3013
c30eee59 3014/* Used when starting or continuing the program. */
c906108c 3015
74960c60
VP
3016static void
3017insert_breakpoint_locations (void)
c906108c 3018{
a5606eee 3019 struct breakpoint *bpt;
35df4500 3020 struct bp_location *bl, **blp_tmp;
eacd795a 3021 int error_flag = 0;
c906108c 3022 int val = 0;
3fbb6ffa 3023 int disabled_breaks = 0;
81d0cc19 3024 int hw_breakpoint_error = 0;
dd61ec5c 3025 int hw_bp_error_explained_already = 0;
c906108c 3026
d7e74731
PA
3027 string_file tmp_error_stream;
3028
81d0cc19
GS
3029 /* Explicitly mark the warning -- this will only be printed if
3030 there was an error. */
d7e74731 3031 tmp_error_stream.puts ("Warning:\n");
6c95b8df 3032
5ed8105e 3033 scoped_restore_current_pspace_and_thread restore_pspace_thread;
6c95b8df 3034
35df4500 3035 ALL_BP_LOCATIONS (bl, blp_tmp)
879bfdc2 3036 {
b775012e 3037 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
879bfdc2
DJ
3038 continue;
3039
4a64f543
MS
3040 /* There is no point inserting thread-specific breakpoints if
3041 the thread no longer exists. ALL_BP_LOCATIONS bp_location
3042 has BL->OWNER always non-NULL. */
35df4500 3043 if (bl->owner->thread != -1
5d5658a1 3044 && !valid_global_thread_id (bl->owner->thread))
f365de73
AS
3045 continue;
3046
35df4500 3047 switch_to_program_space_and_thread (bl->pspace);
6c95b8df
PA
3048
3049 /* For targets that support global breakpoints, there's no need
3050 to select an inferior to insert breakpoint to. In fact, even
3051 if we aren't attached to any process yet, we should still
3052 insert breakpoints. */
f5656ead 3053 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
55f6301a 3054 && (inferior_ptid == null_ptid || !target_has_execution ()))
6c95b8df
PA
3055 continue;
3056
d7e74731 3057 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
dd61ec5c 3058 &hw_breakpoint_error, &hw_bp_error_explained_already);
879bfdc2 3059 if (val)
eacd795a 3060 error_flag = val;
879bfdc2 3061 }
c906108c 3062
4a64f543
MS
3063 /* If we failed to insert all locations of a watchpoint, remove
3064 them, as half-inserted watchpoint is of limited use. */
a5606eee
VP
3065 ALL_BREAKPOINTS (bpt)
3066 {
3067 int some_failed = 0;
3068 struct bp_location *loc;
3069
3070 if (!is_hardware_watchpoint (bpt))
3071 continue;
3072
d6b74ac4 3073 if (!breakpoint_enabled (bpt))
a5606eee 3074 continue;
74960c60
VP
3075
3076 if (bpt->disposition == disp_del_at_next_stop)
3077 continue;
a5606eee
VP
3078
3079 for (loc = bpt->loc; loc; loc = loc->next)
56710373 3080 if (!loc->inserted && should_be_inserted (loc))
a5606eee
VP
3081 {
3082 some_failed = 1;
3083 break;
3084 }
3085 if (some_failed)
3086 {
3087 for (loc = bpt->loc; loc; loc = loc->next)
3088 if (loc->inserted)
834c0d03 3089 remove_breakpoint (loc);
a5606eee
VP
3090
3091 hw_breakpoint_error = 1;
d7e74731
PA
3092 tmp_error_stream.printf ("Could not insert "
3093 "hardware watchpoint %d.\n",
3094 bpt->number);
eacd795a 3095 error_flag = -1;
a5606eee
VP
3096 }
3097 }
3098
eacd795a 3099 if (error_flag)
81d0cc19
GS
3100 {
3101 /* If a hardware breakpoint or watchpoint was inserted, add a
dda83cd7 3102 message about possibly exhausted resources. */
dd61ec5c 3103 if (hw_breakpoint_error && !hw_bp_error_explained_already)
81d0cc19 3104 {
d7e74731 3105 tmp_error_stream.printf ("Could not insert hardware breakpoints:\n\
c6510018 3106You may have requested too many hardware breakpoints/watchpoints.\n");
81d0cc19 3107 }
223ffa71 3108 target_terminal::ours_for_output ();
81d0cc19
GS
3109 error_stream (tmp_error_stream);
3110 }
c906108c
SS
3111}
3112
c30eee59
TJB
3113/* Used when the program stops.
3114 Returns zero if successful, or non-zero if there was a problem
3115 removing a breakpoint location. */
3116
c906108c 3117int
fba45db2 3118remove_breakpoints (void)
c906108c 3119{
35df4500 3120 struct bp_location *bl, **blp_tmp;
3a1bae8e 3121 int val = 0;
c906108c 3122
35df4500 3123 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3124 {
1e4d1764 3125 if (bl->inserted && !is_tracepoint (bl->owner))
834c0d03 3126 val |= remove_breakpoint (bl);
c5aa993b 3127 }
3a1bae8e 3128 return val;
c906108c
SS
3129}
3130
49fa26b0
PA
3131/* When a thread exits, remove breakpoints that are related to
3132 that thread. */
3133
3134static void
3135remove_threaded_breakpoints (struct thread_info *tp, int silent)
3136{
3137 struct breakpoint *b, *b_tmp;
3138
3139 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3140 {
5d5658a1 3141 if (b->thread == tp->global_num && user_breakpoint_p (b))
49fa26b0
PA
3142 {
3143 b->disposition = disp_del_at_next_stop;
3144
3145 printf_filtered (_("\
43792cf0
PA
3146Thread-specific breakpoint %d deleted - thread %s no longer in the thread list.\n"),
3147 b->number, print_thread_id (tp));
49fa26b0
PA
3148
3149 /* Hide it from the user. */
3150 b->number = 0;
3151 }
3152 }
3153}
3154
f3869b1a 3155/* See breakpoint.h. */
6c95b8df 3156
f3869b1a 3157void
00431a78 3158remove_breakpoints_inf (inferior *inf)
6c95b8df 3159{
35df4500 3160 struct bp_location *bl, **blp_tmp;
6c95b8df 3161 int val;
6c95b8df 3162
35df4500 3163 ALL_BP_LOCATIONS (bl, blp_tmp)
6c95b8df 3164 {
35df4500 3165 if (bl->pspace != inf->pspace)
6c95b8df
PA
3166 continue;
3167
fc126975 3168 if (bl->inserted && !bl->target_info.persist)
6c95b8df 3169 {
834c0d03 3170 val = remove_breakpoint (bl);
6c95b8df 3171 if (val != 0)
f3869b1a 3172 return;
6c95b8df
PA
3173 }
3174 }
6c95b8df
PA
3175}
3176
e58b0e63
PA
3177static int internal_breakpoint_number = -1;
3178
84f4c1fe
PM
3179/* Set the breakpoint number of B, depending on the value of INTERNAL.
3180 If INTERNAL is non-zero, the breakpoint number will be populated
3181 from internal_breakpoint_number and that variable decremented.
e5dd4106 3182 Otherwise the breakpoint number will be populated from
84f4c1fe
PM
3183 breakpoint_count and that value incremented. Internal breakpoints
3184 do not set the internal var bpnum. */
3185static void
3186set_breakpoint_number (int internal, struct breakpoint *b)
3187{
3188 if (internal)
3189 b->number = internal_breakpoint_number--;
3190 else
3191 {
3192 set_breakpoint_count (breakpoint_count + 1);
3193 b->number = breakpoint_count;
3194 }
3195}
3196
e62c965a 3197static struct breakpoint *
a6d9a66e 3198create_internal_breakpoint (struct gdbarch *gdbarch,
06edf0c0 3199 CORE_ADDR address, enum bptype type,
c0a91b2b 3200 const struct breakpoint_ops *ops)
e62c965a 3201{
51abb421 3202 symtab_and_line sal;
e62c965a
PP
3203 sal.pc = address;
3204 sal.section = find_pc_overlay (sal.pc);
6c95b8df 3205 sal.pspace = current_program_space;
e62c965a 3206
51abb421 3207 breakpoint *b = set_raw_breakpoint (gdbarch, sal, type, ops);
e62c965a
PP
3208 b->number = internal_breakpoint_number--;
3209 b->disposition = disp_donttouch;
3210
3211 return b;
3212}
3213
17450429
PP
3214static const char *const longjmp_names[] =
3215 {
3216 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
3217 };
3218#define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
3219
3220/* Per-objfile data private to breakpoint.c. */
3221struct breakpoint_objfile_data
3222{
3223 /* Minimal symbol for "_ovly_debug_event" (if any). */
43dce439 3224 struct bound_minimal_symbol overlay_msym {};
17450429
PP
3225
3226 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
43dce439 3227 struct bound_minimal_symbol longjmp_msym[NUM_LONGJMP_NAMES] {};
17450429 3228
28106bc2 3229 /* True if we have looked for longjmp probes. */
43dce439 3230 int longjmp_searched = 0;
28106bc2 3231
45461e0d
SM
3232 /* SystemTap probe points for longjmp (if any). These are non-owning
3233 references. */
3234 std::vector<probe *> longjmp_probes;
28106bc2 3235
17450429 3236 /* Minimal symbol for "std::terminate()" (if any). */
43dce439 3237 struct bound_minimal_symbol terminate_msym {};
17450429
PP
3238
3239 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
43dce439 3240 struct bound_minimal_symbol exception_msym {};
28106bc2
SDJ
3241
3242 /* True if we have looked for exception probes. */
43dce439 3243 int exception_searched = 0;
28106bc2 3244
45461e0d
SM
3245 /* SystemTap probe points for unwinding (if any). These are non-owning
3246 references. */
3247 std::vector<probe *> exception_probes;
17450429
PP
3248};
3249
51d3063a
TT
3250static const struct objfile_key<breakpoint_objfile_data>
3251 breakpoint_objfile_key;
17450429
PP
3252
3253/* Minimal symbol not found sentinel. */
3254static struct minimal_symbol msym_not_found;
3255
3256/* Returns TRUE if MSYM point to the "not found" sentinel. */
3257
3258static int
3259msym_not_found_p (const struct minimal_symbol *msym)
3260{
3261 return msym == &msym_not_found;
3262}
3263
3264/* Return per-objfile data needed by breakpoint.c.
3265 Allocate the data if necessary. */
3266
3267static struct breakpoint_objfile_data *
3268get_breakpoint_objfile_data (struct objfile *objfile)
3269{
3270 struct breakpoint_objfile_data *bp_objfile_data;
3271
51d3063a 3272 bp_objfile_data = breakpoint_objfile_key.get (objfile);
17450429 3273 if (bp_objfile_data == NULL)
51d3063a 3274 bp_objfile_data = breakpoint_objfile_key.emplace (objfile);
17450429
PP
3275 return bp_objfile_data;
3276}
3277
e62c965a 3278static void
af02033e 3279create_overlay_event_breakpoint (void)
e62c965a 3280{
af02033e 3281 const char *const func_name = "_ovly_debug_event";
e62c965a 3282
2030c079 3283 for (objfile *objfile : current_program_space->objfiles ())
69de3c6a
PP
3284 {
3285 struct breakpoint *b;
17450429
PP
3286 struct breakpoint_objfile_data *bp_objfile_data;
3287 CORE_ADDR addr;
67994074 3288 struct explicit_location explicit_loc;
69de3c6a 3289
17450429
PP
3290 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3291
3b7344d5 3292 if (msym_not_found_p (bp_objfile_data->overlay_msym.minsym))
17450429
PP
3293 continue;
3294
3b7344d5 3295 if (bp_objfile_data->overlay_msym.minsym == NULL)
17450429 3296 {
3b7344d5 3297 struct bound_minimal_symbol m;
17450429
PP
3298
3299 m = lookup_minimal_symbol_text (func_name, objfile);
3b7344d5 3300 if (m.minsym == NULL)
17450429
PP
3301 {
3302 /* Avoid future lookups in this objfile. */
3b7344d5 3303 bp_objfile_data->overlay_msym.minsym = &msym_not_found;
17450429
PP
3304 continue;
3305 }
3306 bp_objfile_data->overlay_msym = m;
3307 }
e62c965a 3308
77e371c0 3309 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->overlay_msym);
08feed99 3310 b = create_internal_breakpoint (objfile->arch (), addr,
dda83cd7 3311 bp_overlay_event,
06edf0c0 3312 &internal_breakpoint_ops);
67994074
KS
3313 initialize_explicit_location (&explicit_loc);
3314 explicit_loc.function_name = ASTRDUP (func_name);
d28cd78a 3315 b->location = new_explicit_location (&explicit_loc);
e62c965a 3316
69de3c6a 3317 if (overlay_debugging == ovly_auto)
dda83cd7
SM
3318 {
3319 b->enable_state = bp_enabled;
3320 overlay_events_enabled = 1;
3321 }
69de3c6a
PP
3322 else
3323 {
dda83cd7
SM
3324 b->enable_state = bp_disabled;
3325 overlay_events_enabled = 0;
69de3c6a 3326 }
e62c965a 3327 }
e62c965a
PP
3328}
3329
2a7f6487
TV
3330/* Install a master longjmp breakpoint for OBJFILE using a probe. Return
3331 true if a breakpoint was installed. */
3332
3333static bool
3334create_longjmp_master_breakpoint_probe (objfile *objfile)
0fd8e87f 3335{
2a7f6487
TV
3336 struct gdbarch *gdbarch = objfile->arch ();
3337 struct breakpoint_objfile_data *bp_objfile_data
3338 = get_breakpoint_objfile_data (objfile);
0fd8e87f 3339
2a7f6487 3340 if (!bp_objfile_data->longjmp_searched)
94c93c35 3341 {
2a7f6487
TV
3342 std::vector<probe *> ret
3343 = find_probes_in_objfile (objfile, "libc", "longjmp");
af02033e 3344
2a7f6487 3345 if (!ret.empty ())
94c93c35 3346 {
2a7f6487
TV
3347 /* We are only interested in checking one element. */
3348 probe *p = ret[0];
0fd8e87f 3349
2a7f6487
TV
3350 if (!p->can_evaluate_arguments ())
3351 {
3352 /* We cannot use the probe interface here,
3353 because it does not know how to evaluate
3354 arguments. */
3355 ret.clear ();
3356 }
3357 }
3358 bp_objfile_data->longjmp_probes = ret;
3359 bp_objfile_data->longjmp_searched = 1;
3360 }
0fd8e87f 3361
2a7f6487
TV
3362 if (bp_objfile_data->longjmp_probes.empty ())
3363 return false;
17450429 3364
2a7f6487
TV
3365 for (probe *p : bp_objfile_data->longjmp_probes)
3366 {
3367 struct breakpoint *b;
25f9533e 3368
2a7f6487
TV
3369 b = create_internal_breakpoint (gdbarch,
3370 p->get_relocated_address (objfile),
3371 bp_longjmp_master,
3372 &internal_breakpoint_ops);
3373 b->location = new_probe_location ("-probe-stap libc:longjmp");
3374 b->enable_state = bp_disabled;
3375 }
aed57c53 3376
2a7f6487
TV
3377 return true;
3378}
25f9533e 3379
2a7f6487
TV
3380/* Install master longjmp breakpoints for OBJFILE using longjmp_names.
3381 Return true if at least one breakpoint was installed. */
3382
3383static bool
3384create_longjmp_master_breakpoint_names (objfile *objfile)
3385{
3386 struct gdbarch *gdbarch = objfile->arch ();
3387 if (!gdbarch_get_longjmp_target_p (gdbarch))
3388 return false;
3389
3390 struct breakpoint_objfile_data *bp_objfile_data
3391 = get_breakpoint_objfile_data (objfile);
3392 unsigned int installed_bp = 0;
3393
3394 for (int i = 0; i < NUM_LONGJMP_NAMES; i++)
3395 {
3396 struct breakpoint *b;
3397 const char *func_name;
3398 CORE_ADDR addr;
3399 struct explicit_location explicit_loc;
3400
3401 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i].minsym))
3402 continue;
28106bc2 3403
2a7f6487
TV
3404 func_name = longjmp_names[i];
3405 if (bp_objfile_data->longjmp_msym[i].minsym == NULL)
3406 {
3407 struct bound_minimal_symbol m;
3408
3409 m = lookup_minimal_symbol_text (func_name, objfile);
3410 if (m.minsym == NULL)
3411 {
3412 /* Prevent future lookups in this objfile. */
3413 bp_objfile_data->longjmp_msym[i].minsym = &msym_not_found;
94c93c35
TT
3414 continue;
3415 }
2a7f6487
TV
3416 bp_objfile_data->longjmp_msym[i] = m;
3417 }
28106bc2 3418
2a7f6487
TV
3419 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->longjmp_msym[i]);
3420 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master,
3421 &internal_breakpoint_ops);
3422 initialize_explicit_location (&explicit_loc);
3423 explicit_loc.function_name = ASTRDUP (func_name);
3424 b->location = new_explicit_location (&explicit_loc);
3425 b->enable_state = bp_disabled;
3426 installed_bp++;
3427 }
28106bc2 3428
2a7f6487
TV
3429 return installed_bp > 0;
3430}
0fd8e87f 3431
2a7f6487 3432/* Create a master longjmp breakpoint. */
17450429 3433
2a7f6487
TV
3434static void
3435create_longjmp_master_breakpoint (void)
3436{
3437 scoped_restore_current_program_space restore_pspace;
aed57c53 3438
2a7f6487
TV
3439 for (struct program_space *pspace : program_spaces)
3440 {
3441 set_current_program_space (pspace);
17450429 3442
2a7f6487
TV
3443 for (objfile *obj : current_program_space->objfiles ())
3444 {
3445 /* Skip separate debug object, it's handled in the loop below. */
3446 if (obj->separate_debug_objfile_backlink != nullptr)
3447 continue;
3448
3449 /* Try a probe kind breakpoint on main objfile. */
3450 if (create_longjmp_master_breakpoint_probe (obj))
3451 continue;
3452
3453 /* Try longjmp_names kind breakpoints on main and separate_debug
3454 objfiles. */
3455 for (objfile *debug_objfile : obj->separate_debug_objfiles ())
3456 if (create_longjmp_master_breakpoint_names (debug_objfile))
3457 break;
94c93c35
TT
3458 }
3459 }
0fd8e87f
UW
3460}
3461
af02033e 3462/* Create a master std::terminate breakpoint. */
aa7d318d 3463static void
af02033e 3464create_std_terminate_master_breakpoint (void)
aa7d318d 3465{
af02033e 3466 const char *const func_name = "std::terminate()";
aa7d318d 3467
5ed8105e 3468 scoped_restore_current_program_space restore_pspace;
aa7d318d 3469
94c93c35
TT
3470 for (struct program_space *pspace : program_spaces)
3471 {
3472 CORE_ADDR addr;
17450429 3473
94c93c35 3474 set_current_program_space (pspace);
17450429 3475
94c93c35
TT
3476 for (objfile *objfile : current_program_space->objfiles ())
3477 {
3478 struct breakpoint *b;
3479 struct breakpoint_objfile_data *bp_objfile_data;
3480 struct explicit_location explicit_loc;
aa7d318d 3481
94c93c35 3482 bp_objfile_data = get_breakpoint_objfile_data (objfile);
aa7d318d 3483
94c93c35
TT
3484 if (msym_not_found_p (bp_objfile_data->terminate_msym.minsym))
3485 continue;
17450429 3486
94c93c35
TT
3487 if (bp_objfile_data->terminate_msym.minsym == NULL)
3488 {
3489 struct bound_minimal_symbol m;
17450429 3490
94c93c35
TT
3491 m = lookup_minimal_symbol (func_name, NULL, objfile);
3492 if (m.minsym == NULL || (MSYMBOL_TYPE (m.minsym) != mst_text
3493 && MSYMBOL_TYPE (m.minsym) != mst_file_text))
3494 {
3495 /* Prevent future lookups in this objfile. */
3496 bp_objfile_data->terminate_msym.minsym = &msym_not_found;
3497 continue;
3498 }
3499 bp_objfile_data->terminate_msym = m;
3500 }
aa7d318d 3501
94c93c35
TT
3502 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->terminate_msym);
3503 b = create_internal_breakpoint (objfile->arch (), addr,
3504 bp_std_terminate_master,
3505 &internal_breakpoint_ops);
3506 initialize_explicit_location (&explicit_loc);
3507 explicit_loc.function_name = ASTRDUP (func_name);
3508 b->location = new_explicit_location (&explicit_loc);
3509 b->enable_state = bp_disabled;
3510 }
3511 }
aa7d318d
TT
3512}
3513
1940319c
TV
3514/* Install a master breakpoint on the unwinder's debug hook for OBJFILE using a
3515 probe. Return true if a breakpoint was installed. */
186c406b 3516
1940319c
TV
3517static bool
3518create_exception_master_breakpoint_probe (objfile *objfile)
186c406b 3519{
1940319c
TV
3520 struct breakpoint *b;
3521 struct gdbarch *gdbarch;
3522 struct breakpoint_objfile_data *bp_objfile_data;
186c406b 3523
1940319c 3524 bp_objfile_data = get_breakpoint_objfile_data (objfile);
17450429 3525
1940319c
TV
3526 /* We prefer the SystemTap probe point if it exists. */
3527 if (!bp_objfile_data->exception_searched)
3528 {
3529 std::vector<probe *> ret
3530 = find_probes_in_objfile (objfile, "libgcc", "unwind");
17450429 3531
1940319c 3532 if (!ret.empty ())
28106bc2 3533 {
1940319c
TV
3534 /* We are only interested in checking one element. */
3535 probe *p = ret[0];
25f9533e 3536
1940319c 3537 if (!p->can_evaluate_arguments ())
25f9533e 3538 {
1940319c
TV
3539 /* We cannot use the probe interface here, because it does
3540 not know how to evaluate arguments. */
3541 ret.clear ();
25f9533e 3542 }
28106bc2 3543 }
1940319c
TV
3544 bp_objfile_data->exception_probes = ret;
3545 bp_objfile_data->exception_searched = 1;
3546 }
28106bc2 3547
1940319c
TV
3548 if (bp_objfile_data->exception_probes.empty ())
3549 return false;
45461e0d 3550
1940319c 3551 gdbarch = objfile->arch ();
28106bc2 3552
1940319c
TV
3553 for (probe *p : bp_objfile_data->exception_probes)
3554 {
3555 b = create_internal_breakpoint (gdbarch,
3556 p->get_relocated_address (objfile),
3557 bp_exception_master,
3558 &internal_breakpoint_ops);
3559 b->location = new_probe_location ("-probe-stap libgcc:unwind");
3560 b->enable_state = bp_disabled;
3561 }
28106bc2 3562
1940319c
TV
3563 return true;
3564}
28106bc2 3565
1940319c
TV
3566/* Install a master breakpoint on the unwinder's debug hook for OBJFILE using
3567 _Unwind_DebugHook. Return true if a breakpoint was installed. */
17450429 3568
1940319c
TV
3569static bool
3570create_exception_master_breakpoint_hook (objfile *objfile)
3571{
3572 const char *const func_name = "_Unwind_DebugHook";
3573 struct breakpoint *b;
3574 struct gdbarch *gdbarch;
3575 struct breakpoint_objfile_data *bp_objfile_data;
3576 CORE_ADDR addr;
3577 struct explicit_location explicit_loc;
186c406b 3578
1940319c 3579 bp_objfile_data = get_breakpoint_objfile_data (objfile);
186c406b 3580
1940319c
TV
3581 if (msym_not_found_p (bp_objfile_data->exception_msym.minsym))
3582 return false;
17450429 3583
1940319c
TV
3584 gdbarch = objfile->arch ();
3585
3586 if (bp_objfile_data->exception_msym.minsym == NULL)
3587 {
3588 struct bound_minimal_symbol debug_hook;
3589
3590 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
3591 if (debug_hook.minsym == NULL)
3592 {
3593 bp_objfile_data->exception_msym.minsym = &msym_not_found;
3594 return false;
186c406b 3595 }
17450429 3596
1940319c
TV
3597 bp_objfile_data->exception_msym = debug_hook;
3598 }
3599
3600 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->exception_msym);
328d42d8
SM
3601 addr = gdbarch_convert_from_func_ptr_addr
3602 (gdbarch, addr, current_inferior ()->top_target ());
1940319c
TV
3603 b = create_internal_breakpoint (gdbarch, addr, bp_exception_master,
3604 &internal_breakpoint_ops);
3605 initialize_explicit_location (&explicit_loc);
3606 explicit_loc.function_name = ASTRDUP (func_name);
3607 b->location = new_explicit_location (&explicit_loc);
3608 b->enable_state = bp_disabled;
3609
3610 return true;
3611}
3612
3613/* Install a master breakpoint on the unwinder's debug hook. */
3614
3615static void
3616create_exception_master_breakpoint (void)
3617{
3618 for (objfile *obj : current_program_space->objfiles ())
3619 {
3620 /* Skip separate debug object. */
3621 if (obj->separate_debug_objfile_backlink)
3622 continue;
3623
3624 /* Try a probe kind breakpoint. */
3625 if (create_exception_master_breakpoint_probe (obj))
3626 continue;
3627
7c6944ab
TV
3628 /* Iterate over main and separate debug objects and try an
3629 _Unwind_DebugHook kind breakpoint. */
3630 for (objfile *debug_objfile : obj->separate_debug_objfiles ())
3631 if (create_exception_master_breakpoint_hook (debug_objfile))
1940319c 3632 break;
186c406b 3633 }
186c406b
TT
3634}
3635
9ef9e6a6
KS
3636/* Does B have a location spec? */
3637
3638static int
3639breakpoint_event_location_empty_p (const struct breakpoint *b)
3640{
d28cd78a 3641 return b->location != NULL && event_location_empty_p (b->location.get ());
9ef9e6a6
KS
3642}
3643
c906108c 3644void
fba45db2 3645update_breakpoints_after_exec (void)
c906108c 3646{
35df4500 3647 struct breakpoint *b, *b_tmp;
876fa593 3648 struct bp_location *bploc, **bplocp_tmp;
c906108c 3649
25b22b0a
PA
3650 /* We're about to delete breakpoints from GDB's lists. If the
3651 INSERTED flag is true, GDB will try to lift the breakpoints by
3652 writing the breakpoints' "shadow contents" back into memory. The
3653 "shadow contents" are NOT valid after an exec, so GDB should not
3654 do that. Instead, the target is responsible from marking
3655 breakpoints out as soon as it detects an exec. We don't do that
3656 here instead, because there may be other attempts to delete
3657 breakpoints after detecting an exec and before reaching here. */
876fa593 3658 ALL_BP_LOCATIONS (bploc, bplocp_tmp)
6c95b8df
PA
3659 if (bploc->pspace == current_program_space)
3660 gdb_assert (!bploc->inserted);
c906108c 3661
35df4500 3662 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 3663 {
6c95b8df
PA
3664 if (b->pspace != current_program_space)
3665 continue;
3666
4a64f543 3667 /* Solib breakpoints must be explicitly reset after an exec(). */
c5aa993b
JM
3668 if (b->type == bp_shlib_event)
3669 {
3670 delete_breakpoint (b);
3671 continue;
3672 }
c906108c 3673
4a64f543 3674 /* JIT breakpoints must be explicitly reset after an exec(). */
4efc6507
DE
3675 if (b->type == bp_jit_event)
3676 {
3677 delete_breakpoint (b);
3678 continue;
3679 }
3680
1900040c 3681 /* Thread event breakpoints must be set anew after an exec(),
0fd8e87f
UW
3682 as must overlay event and longjmp master breakpoints. */
3683 if (b->type == bp_thread_event || b->type == bp_overlay_event
186c406b
TT
3684 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
3685 || b->type == bp_exception_master)
c4093a6a
JM
3686 {
3687 delete_breakpoint (b);
3688 continue;
3689 }
3690
4a64f543 3691 /* Step-resume breakpoints are meaningless after an exec(). */
2c03e5be 3692 if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
c5aa993b
JM
3693 {
3694 delete_breakpoint (b);
3695 continue;
3696 }
3697
7c16b83e
PA
3698 /* Just like single-step breakpoints. */
3699 if (b->type == bp_single_step)
3700 {
3701 delete_breakpoint (b);
3702 continue;
3703 }
3704
611c83ae
PA
3705 /* Longjmp and longjmp-resume breakpoints are also meaningless
3706 after an exec. */
186c406b 3707 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
e2e4d78b 3708 || b->type == bp_longjmp_call_dummy
186c406b 3709 || b->type == bp_exception || b->type == bp_exception_resume)
611c83ae
PA
3710 {
3711 delete_breakpoint (b);
3712 continue;
3713 }
3714
ce78b96d
JB
3715 if (b->type == bp_catchpoint)
3716 {
dda83cd7
SM
3717 /* For now, none of the bp_catchpoint breakpoints need to
3718 do anything at this point. In the future, if some of
3719 the catchpoints need to something, we will need to add
3720 a new method, and call this method from here. */
3721 continue;
ce78b96d
JB
3722 }
3723
c5aa993b
JM
3724 /* bp_finish is a special case. The only way we ought to be able
3725 to see one of these when an exec() has happened, is if the user
3726 caught a vfork, and then said "finish". Ordinarily a finish just
3727 carries them to the call-site of the current callee, by setting
3728 a temporary bp there and resuming. But in this case, the finish
3729 will carry them entirely through the vfork & exec.
3730
3731 We don't want to allow a bp_finish to remain inserted now. But
3732 we can't safely delete it, 'cause finish_command has a handle to
3733 the bp on a bpstat, and will later want to delete it. There's a
3734 chance (and I've seen it happen) that if we delete the bp_finish
3735 here, that its storage will get reused by the time finish_command
3736 gets 'round to deleting the "use to be a bp_finish" breakpoint.
3737 We really must allow finish_command to delete a bp_finish.
3738
e5dd4106 3739 In the absence of a general solution for the "how do we know
53a5351d
JM
3740 it's safe to delete something others may have handles to?"
3741 problem, what we'll do here is just uninsert the bp_finish, and
3742 let finish_command delete it.
3743
3744 (We know the bp_finish is "doomed" in the sense that it's
3745 momentary, and will be deleted as soon as finish_command sees
3746 the inferior stopped. So it doesn't matter that the bp's
3747 address is probably bogus in the new a.out, unlike e.g., the
3748 solib breakpoints.) */
c5aa993b 3749
c5aa993b
JM
3750 if (b->type == bp_finish)
3751 {
3752 continue;
3753 }
3754
3755 /* Without a symbolic address, we have little hope of the
3756 pre-exec() address meaning the same thing in the post-exec()
4a64f543 3757 a.out. */
9ef9e6a6 3758 if (breakpoint_event_location_empty_p (b))
c5aa993b
JM
3759 {
3760 delete_breakpoint (b);
3761 continue;
3762 }
c5aa993b 3763 }
c906108c
SS
3764}
3765
3766int
d80ee84f 3767detach_breakpoints (ptid_t ptid)
c906108c 3768{
35df4500 3769 struct bp_location *bl, **blp_tmp;
3a1bae8e 3770 int val = 0;
2989a365 3771 scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid);
6c95b8df 3772 struct inferior *inf = current_inferior ();
c5aa993b 3773
e99b03dc 3774 if (ptid.pid () == inferior_ptid.pid ())
8a3fe4f8 3775 error (_("Cannot detach breakpoints of inferior_ptid"));
c5aa993b 3776
6c95b8df 3777 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
d80ee84f 3778 inferior_ptid = ptid;
35df4500 3779 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3780 {
35df4500 3781 if (bl->pspace != inf->pspace)
6c95b8df
PA
3782 continue;
3783
bd9673a4
PW
3784 /* This function must physically remove breakpoints locations
3785 from the specified ptid, without modifying the breakpoint
3786 package's state. Locations of type bp_loc_other are only
3787 maintained at GDB side. So, there is no need to remove
3788 these bp_loc_other locations. Moreover, removing these
3789 would modify the breakpoint package's state. */
3790 if (bl->loc_type == bp_loc_other)
3791 continue;
3792
35df4500 3793 if (bl->inserted)
b2b6a7da 3794 val |= remove_breakpoint_1 (bl, DETACH_BREAKPOINT);
c5aa993b 3795 }
d03285ec 3796
3a1bae8e 3797 return val;
c906108c
SS
3798}
3799
35df4500 3800/* Remove the breakpoint location BL from the current address space.
6c95b8df
PA
3801 Note that this is used to detach breakpoints from a child fork.
3802 When we get here, the child isn't in the inferior list, and neither
3803 do we have objects to represent its address space --- we should
35df4500 3804 *not* look at bl->pspace->aspace here. */
6c95b8df 3805
c906108c 3806static int
b2b6a7da 3807remove_breakpoint_1 (struct bp_location *bl, enum remove_bp_reason reason)
c906108c
SS
3808{
3809 int val;
c5aa993b 3810
35df4500
TJB
3811 /* BL is never in moribund_locations by our callers. */
3812 gdb_assert (bl->owner != NULL);
2bdf28a0 3813
74960c60
VP
3814 /* The type of none suggests that owner is actually deleted.
3815 This should not ever happen. */
35df4500 3816 gdb_assert (bl->owner->type != bp_none);
0bde7532 3817
35df4500
TJB
3818 if (bl->loc_type == bp_loc_software_breakpoint
3819 || bl->loc_type == bp_loc_hardware_breakpoint)
c906108c 3820 {
c02f5703
MS
3821 /* "Normal" instruction breakpoint: either the standard
3822 trap-instruction bp (bp_breakpoint), or a
3823 bp_hardware_breakpoint. */
3824
3825 /* First check to see if we have to handle an overlay. */
3826 if (overlay_debugging == ovly_off
35df4500
TJB
3827 || bl->section == NULL
3828 || !(section_is_overlay (bl->section)))
c02f5703
MS
3829 {
3830 /* No overlay handling: just remove the breakpoint. */
08351840
PA
3831
3832 /* If we're trying to uninsert a memory breakpoint that we
3833 know is set in a dynamic object that is marked
3834 shlib_disabled, then either the dynamic object was
3835 removed with "remove-symbol-file" or with
3836 "nosharedlibrary". In the former case, we don't know
3837 whether another dynamic object might have loaded over the
3838 breakpoint's address -- the user might well let us know
3839 about it next with add-symbol-file (the whole point of
d03de421 3840 add-symbol-file is letting the user manually maintain a
08351840
PA
3841 list of dynamically loaded objects). If we have the
3842 breakpoint's shadow memory, that is, this is a software
3843 breakpoint managed by GDB, check whether the breakpoint
3844 is still inserted in memory, to avoid overwriting wrong
3845 code with stale saved shadow contents. Note that HW
3846 breakpoints don't have shadow memory, as they're
3847 implemented using a mechanism that is not dependent on
3848 being able to modify the target's memory, and as such
3849 they should always be removed. */
3850 if (bl->shlib_disabled
3851 && bl->target_info.shadow_len != 0
3852 && !memory_validate_breakpoint (bl->gdbarch, &bl->target_info))
3853 val = 0;
3854 else
73971819 3855 val = bl->owner->ops->remove_location (bl, reason);
c02f5703 3856 }
c906108c
SS
3857 else
3858 {
4a64f543 3859 /* This breakpoint is in an overlay section.
c02f5703
MS
3860 Did we set a breakpoint at the LMA? */
3861 if (!overlay_events_enabled)
3862 {
3863 /* Yes -- overlay event support is not active, so we
3864 should have set a breakpoint at the LMA. Remove it.
3865 */
c02f5703
MS
3866 /* Ignore any failures: if the LMA is in ROM, we will
3867 have already warned when we failed to insert it. */
35df4500
TJB
3868 if (bl->loc_type == bp_loc_hardware_breakpoint)
3869 target_remove_hw_breakpoint (bl->gdbarch,
3870 &bl->overlay_target_info);
c02f5703 3871 else
35df4500 3872 target_remove_breakpoint (bl->gdbarch,
73971819
PA
3873 &bl->overlay_target_info,
3874 reason);
c02f5703
MS
3875 }
3876 /* Did we set a breakpoint at the VMA?
3877 If so, we will have marked the breakpoint 'inserted'. */
35df4500 3878 if (bl->inserted)
c906108c 3879 {
c02f5703
MS
3880 /* Yes -- remove it. Previously we did not bother to
3881 remove the breakpoint if the section had been
3882 unmapped, but let's not rely on that being safe. We
3883 don't know what the overlay manager might do. */
aa67235e
UW
3884
3885 /* However, we should remove *software* breakpoints only
3886 if the section is still mapped, or else we overwrite
3887 wrong code with the saved shadow contents. */
348d480f
PA
3888 if (bl->loc_type == bp_loc_hardware_breakpoint
3889 || section_is_mapped (bl->section))
73971819 3890 val = bl->owner->ops->remove_location (bl, reason);
aa67235e
UW
3891 else
3892 val = 0;
c906108c 3893 }
c02f5703
MS
3894 else
3895 {
3896 /* No -- not inserted, so no need to remove. No error. */
3897 val = 0;
3898 }
c906108c 3899 }
879d1e6b 3900
08351840
PA
3901 /* In some cases, we might not be able to remove a breakpoint in
3902 a shared library that has already been removed, but we have
3903 not yet processed the shlib unload event. Similarly for an
3904 unloaded add-symbol-file object - the user might not yet have
3905 had the chance to remove-symbol-file it. shlib_disabled will
3906 be set if the library/object has already been removed, but
3907 the breakpoint hasn't been uninserted yet, e.g., after
3908 "nosharedlibrary" or "remove-symbol-file" with breakpoints
3909 always-inserted mode. */
076855f9 3910 if (val
08351840
PA
3911 && (bl->loc_type == bp_loc_software_breakpoint
3912 && (bl->shlib_disabled
3913 || solib_name_from_address (bl->pspace, bl->address)
d03de421
PA
3914 || shared_objfile_contains_address_p (bl->pspace,
3915 bl->address))))
879d1e6b
UW
3916 val = 0;
3917
c906108c
SS
3918 if (val)
3919 return val;
b2b6a7da 3920 bl->inserted = (reason == DETACH_BREAKPOINT);
c906108c 3921 }
35df4500 3922 else if (bl->loc_type == bp_loc_hardware_watchpoint)
c906108c 3923 {
77b06cd7
TJB
3924 gdb_assert (bl->owner->ops != NULL
3925 && bl->owner->ops->remove_location != NULL);
3926
b2b6a7da 3927 bl->inserted = (reason == DETACH_BREAKPOINT);
73971819 3928 bl->owner->ops->remove_location (bl, reason);
2e70b7b9 3929
c906108c 3930 /* Failure to remove any of the hardware watchpoints comes here. */
b2b6a7da 3931 if (reason == REMOVE_BREAKPOINT && bl->inserted)
8a3fe4f8 3932 warning (_("Could not remove hardware watchpoint %d."),
35df4500 3933 bl->owner->number);
c906108c 3934 }
35df4500 3935 else if (bl->owner->type == bp_catchpoint
dda83cd7
SM
3936 && breakpoint_enabled (bl->owner)
3937 && !bl->duplicate)
ce78b96d 3938 {
77b06cd7
TJB
3939 gdb_assert (bl->owner->ops != NULL
3940 && bl->owner->ops->remove_location != NULL);
ce78b96d 3941
73971819 3942 val = bl->owner->ops->remove_location (bl, reason);
ce78b96d
JB
3943 if (val)
3944 return val;
77b06cd7 3945
b2b6a7da 3946 bl->inserted = (reason == DETACH_BREAKPOINT);
ce78b96d 3947 }
c906108c
SS
3948
3949 return 0;
3950}
3951
6c95b8df 3952static int
834c0d03 3953remove_breakpoint (struct bp_location *bl)
6c95b8df 3954{
35df4500
TJB
3955 /* BL is never in moribund_locations by our callers. */
3956 gdb_assert (bl->owner != NULL);
2bdf28a0 3957
6c95b8df
PA
3958 /* The type of none suggests that owner is actually deleted.
3959 This should not ever happen. */
35df4500 3960 gdb_assert (bl->owner->type != bp_none);
6c95b8df 3961
5ed8105e 3962 scoped_restore_current_pspace_and_thread restore_pspace_thread;
6c95b8df 3963
35df4500 3964 switch_to_program_space_and_thread (bl->pspace);
6c95b8df 3965
5ed8105e 3966 return remove_breakpoint_1 (bl, REMOVE_BREAKPOINT);
6c95b8df
PA
3967}
3968
c906108c
SS
3969/* Clear the "inserted" flag in all breakpoints. */
3970
25b22b0a 3971void
fba45db2 3972mark_breakpoints_out (void)
c906108c 3973{
35df4500 3974 struct bp_location *bl, **blp_tmp;
c906108c 3975
35df4500 3976 ALL_BP_LOCATIONS (bl, blp_tmp)
66c4b3e8 3977 if (bl->pspace == current_program_space)
35df4500 3978 bl->inserted = 0;
c906108c
SS
3979}
3980
53a5351d
JM
3981/* Clear the "inserted" flag in all breakpoints and delete any
3982 breakpoints which should go away between runs of the program.
c906108c
SS
3983
3984 Plus other such housekeeping that has to be done for breakpoints
3985 between runs.
3986
53a5351d
JM
3987 Note: this function gets called at the end of a run (by
3988 generic_mourn_inferior) and when a run begins (by
4a64f543 3989 init_wait_for_inferior). */
c906108c
SS
3990
3991
3992
3993void
fba45db2 3994breakpoint_init_inferior (enum inf_context context)
c906108c 3995{
35df4500 3996 struct breakpoint *b, *b_tmp;
6c95b8df 3997 struct program_space *pspace = current_program_space;
c906108c 3998
50c71eaf
PA
3999 /* If breakpoint locations are shared across processes, then there's
4000 nothing to do. */
f5656ead 4001 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
50c71eaf
PA
4002 return;
4003
1a853c52 4004 mark_breakpoints_out ();
075f6582 4005
35df4500 4006 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 4007 {
6c95b8df
PA
4008 if (b->loc && b->loc->pspace != pspace)
4009 continue;
4010
c5aa993b
JM
4011 switch (b->type)
4012 {
4013 case bp_call_dummy:
e2e4d78b 4014 case bp_longjmp_call_dummy:
c906108c 4015
c5aa993b 4016 /* If the call dummy breakpoint is at the entry point it will
ab92d69b
PA
4017 cause problems when the inferior is rerun, so we better get
4018 rid of it. */
4019
4020 case bp_watchpoint_scope:
4021
4022 /* Also get rid of scope breakpoints. */
4023
4024 case bp_shlib_event:
4025
4026 /* Also remove solib event breakpoints. Their addresses may
4027 have changed since the last time we ran the program.
4028 Actually we may now be debugging against different target;
4029 and so the solib backend that installed this breakpoint may
4030 not be used in by the target. E.g.,
4031
4032 (gdb) file prog-linux
4033 (gdb) run # native linux target
4034 ...
4035 (gdb) kill
4036 (gdb) file prog-win.exe
4037 (gdb) tar rem :9999 # remote Windows gdbserver.
4038 */
c906108c 4039
f59f708a
PA
4040 case bp_step_resume:
4041
4042 /* Also remove step-resume breakpoints. */
4043
7c16b83e
PA
4044 case bp_single_step:
4045
4046 /* Also remove single-step breakpoints. */
4047
c5aa993b
JM
4048 delete_breakpoint (b);
4049 break;
c906108c 4050
c5aa993b
JM
4051 case bp_watchpoint:
4052 case bp_hardware_watchpoint:
4053 case bp_read_watchpoint:
4054 case bp_access_watchpoint:
3a5c3e22
PA
4055 {
4056 struct watchpoint *w = (struct watchpoint *) b;
c906108c 4057
3a5c3e22
PA
4058 /* Likewise for watchpoints on local expressions. */
4059 if (w->exp_valid_block != NULL)
4060 delete_breakpoint (b);
63000888 4061 else
3a5c3e22 4062 {
63000888
PA
4063 /* Get rid of existing locations, which are no longer
4064 valid. New ones will be created in
4065 update_watchpoint, when the inferior is restarted.
4066 The next update_global_location_list call will
4067 garbage collect them. */
4068 b->loc = NULL;
4069
4070 if (context == inf_starting)
4071 {
4072 /* Reset val field to force reread of starting value in
4073 insert_breakpoints. */
850645cf 4074 w->val.reset (nullptr);
4c1d86d9 4075 w->val_valid = false;
63000888
PA
4076 }
4077 }
3a5c3e22 4078 }
c5aa993b
JM
4079 break;
4080 default:
c5aa993b
JM
4081 break;
4082 }
4083 }
1c5cfe86
PA
4084
4085 /* Get rid of the moribund locations. */
1123588c 4086 for (bp_location *bl : moribund_locations)
35df4500 4087 decref_bp_location (&bl);
1123588c 4088 moribund_locations.clear ();
c906108c
SS
4089}
4090
6c95b8df
PA
4091/* These functions concern about actual breakpoints inserted in the
4092 target --- to e.g. check if we need to do decr_pc adjustment or if
4093 we need to hop over the bkpt --- so we check for address space
4094 match, not program space. */
4095
c2c6d25f
JM
4096/* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
4097 exists at PC. It returns ordinary_breakpoint_here if it's an
4098 ordinary breakpoint, or permanent_breakpoint_here if it's a
4099 permanent breakpoint.
4100 - When continuing from a location with an ordinary breakpoint, we
4101 actually single step once before calling insert_breakpoints.
e5dd4106 4102 - When continuing from a location with a permanent breakpoint, we
c2c6d25f
JM
4103 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
4104 the target, to advance the PC past the breakpoint. */
c906108c 4105
c2c6d25f 4106enum breakpoint_here
accd0bcd 4107breakpoint_here_p (const address_space *aspace, CORE_ADDR pc)
c906108c 4108{
35df4500 4109 struct bp_location *bl, **blp_tmp;
c2c6d25f 4110 int any_breakpoint_here = 0;
c906108c 4111
35df4500 4112 ALL_BP_LOCATIONS (bl, blp_tmp)
075f6582 4113 {
35df4500
TJB
4114 if (bl->loc_type != bp_loc_software_breakpoint
4115 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
4116 continue;
4117
f1310107 4118 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
35df4500 4119 if ((breakpoint_enabled (bl->owner)
1a853c52 4120 || bl->permanent)
f1310107 4121 && breakpoint_location_address_match (bl, aspace, pc))
075f6582
DJ
4122 {
4123 if (overlay_debugging
35df4500
TJB
4124 && section_is_overlay (bl->section)
4125 && !section_is_mapped (bl->section))
075f6582 4126 continue; /* unmapped overlay -- can't be a match */
1a853c52 4127 else if (bl->permanent)
075f6582
DJ
4128 return permanent_breakpoint_here;
4129 else
4130 any_breakpoint_here = 1;
4131 }
4132 }
c906108c 4133
f486487f 4134 return any_breakpoint_here ? ordinary_breakpoint_here : no_breakpoint_here;
c906108c
SS
4135}
4136
d35ae833
PA
4137/* See breakpoint.h. */
4138
4139int
accd0bcd 4140breakpoint_in_range_p (const address_space *aspace,
d35ae833
PA
4141 CORE_ADDR addr, ULONGEST len)
4142{
4143 struct bp_location *bl, **blp_tmp;
4144
4145 ALL_BP_LOCATIONS (bl, blp_tmp)
4146 {
4147 if (bl->loc_type != bp_loc_software_breakpoint
4148 && bl->loc_type != bp_loc_hardware_breakpoint)
4149 continue;
4150
4151 if ((breakpoint_enabled (bl->owner)
4152 || bl->permanent)
4153 && breakpoint_location_address_range_overlap (bl, aspace,
4154 addr, len))
4155 {
4156 if (overlay_debugging
4157 && section_is_overlay (bl->section)
4158 && !section_is_mapped (bl->section))
4159 {
4160 /* Unmapped overlay -- can't be a match. */
4161 continue;
4162 }
4163
4164 return 1;
4165 }
4166 }
4167
4168 return 0;
4169}
4170
1c5cfe86
PA
4171/* Return true if there's a moribund breakpoint at PC. */
4172
4173int
accd0bcd 4174moribund_breakpoint_here_p (const address_space *aspace, CORE_ADDR pc)
1c5cfe86 4175{
1123588c 4176 for (bp_location *loc : moribund_locations)
f1310107 4177 if (breakpoint_location_address_match (loc, aspace, pc))
1c5cfe86
PA
4178 return 1;
4179
4180 return 0;
4181}
c2c6d25f 4182
f7ce857f
PA
4183/* Returns non-zero iff BL is inserted at PC, in address space
4184 ASPACE. */
4185
4186static int
4187bp_location_inserted_here_p (struct bp_location *bl,
accd0bcd 4188 const address_space *aspace, CORE_ADDR pc)
f7ce857f
PA
4189{
4190 if (bl->inserted
4191 && breakpoint_address_match (bl->pspace->aspace, bl->address,
4192 aspace, pc))
4193 {
4194 if (overlay_debugging
4195 && section_is_overlay (bl->section)
4196 && !section_is_mapped (bl->section))
4197 return 0; /* unmapped overlay -- can't be a match */
4198 else
4199 return 1;
4200 }
4201 return 0;
4202}
4203
a1fd2fa5 4204/* Returns non-zero iff there's a breakpoint inserted at PC. */
c906108c
SS
4205
4206int
accd0bcd 4207breakpoint_inserted_here_p (const address_space *aspace, CORE_ADDR pc)
c906108c 4208{
f7ce857f 4209 struct bp_location **blp, **blp_tmp = NULL;
c906108c 4210
f7ce857f 4211 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
c5aa993b 4212 {
f7ce857f
PA
4213 struct bp_location *bl = *blp;
4214
35df4500
TJB
4215 if (bl->loc_type != bp_loc_software_breakpoint
4216 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
4217 continue;
4218
f7ce857f
PA
4219 if (bp_location_inserted_here_p (bl, aspace, pc))
4220 return 1;
c5aa993b 4221 }
c36b740a
VP
4222 return 0;
4223}
4224
a1fd2fa5
PA
4225/* This function returns non-zero iff there is a software breakpoint
4226 inserted at PC. */
c36b740a
VP
4227
4228int
accd0bcd 4229software_breakpoint_inserted_here_p (const address_space *aspace,
a1fd2fa5 4230 CORE_ADDR pc)
4fa8626c 4231{
f7ce857f 4232 struct bp_location **blp, **blp_tmp = NULL;
4fa8626c 4233
f7ce857f 4234 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4fa8626c 4235 {
f7ce857f
PA
4236 struct bp_location *bl = *blp;
4237
35df4500 4238 if (bl->loc_type != bp_loc_software_breakpoint)
4fa8626c
DJ
4239 continue;
4240
f7ce857f
PA
4241 if (bp_location_inserted_here_p (bl, aspace, pc))
4242 return 1;
4fa8626c
DJ
4243 }
4244
4245 return 0;
9c02b525
PA
4246}
4247
4248/* See breakpoint.h. */
4249
4250int
accd0bcd 4251hardware_breakpoint_inserted_here_p (const address_space *aspace,
9c02b525
PA
4252 CORE_ADDR pc)
4253{
4254 struct bp_location **blp, **blp_tmp = NULL;
9c02b525
PA
4255
4256 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4257 {
4258 struct bp_location *bl = *blp;
4259
4260 if (bl->loc_type != bp_loc_hardware_breakpoint)
4261 continue;
4262
4263 if (bp_location_inserted_here_p (bl, aspace, pc))
4264 return 1;
4265 }
4266
4267 return 0;
4fa8626c
DJ
4268}
4269
9093389c 4270int
accd0bcd 4271hardware_watchpoint_inserted_in_range (const address_space *aspace,
9093389c
PA
4272 CORE_ADDR addr, ULONGEST len)
4273{
4274 struct breakpoint *bpt;
4275
4276 ALL_BREAKPOINTS (bpt)
4277 {
4278 struct bp_location *loc;
4279
4280 if (bpt->type != bp_hardware_watchpoint
4281 && bpt->type != bp_access_watchpoint)
4282 continue;
4283
4284 if (!breakpoint_enabled (bpt))
4285 continue;
4286
4287 for (loc = bpt->loc; loc; loc = loc->next)
4288 if (loc->pspace->aspace == aspace && loc->inserted)
4289 {
4290 CORE_ADDR l, h;
4291
4292 /* Check for intersection. */
768adc05
PA
4293 l = std::max<CORE_ADDR> (loc->address, addr);
4294 h = std::min<CORE_ADDR> (loc->address + loc->length, addr + len);
9093389c
PA
4295 if (l < h)
4296 return 1;
4297 }
4298 }
4299 return 0;
4300}
c5aa993b 4301
f2478a7e 4302/* See breakpoint.h. */
c906108c 4303
f2478a7e
SM
4304bool
4305is_catchpoint (struct breakpoint *b)
c906108c 4306{
f2478a7e 4307 return (b->type == bp_catchpoint);
c906108c
SS
4308}
4309
c906108c
SS
4310/* Clear a bpstat so that it says we are not at any breakpoint.
4311 Also free any storage that is part of a bpstat. */
4312
4313void
fba45db2 4314bpstat_clear (bpstat *bsp)
c906108c
SS
4315{
4316 bpstat p;
4317 bpstat q;
4318
4319 if (bsp == 0)
4320 return;
4321 p = *bsp;
4322 while (p != NULL)
4323 {
4324 q = p->next;
04afa70c 4325 delete p;
c906108c
SS
4326 p = q;
4327 }
4328 *bsp = NULL;
4329}
4330
04afa70c
TT
4331bpstats::bpstats (const bpstats &other)
4332 : next (NULL),
4333 bp_location_at (other.bp_location_at),
4334 breakpoint_at (other.breakpoint_at),
4335 commands (other.commands),
04afa70c
TT
4336 print (other.print),
4337 stop (other.stop),
4338 print_it (other.print_it)
4339{
850645cf
TT
4340 if (other.old_val != NULL)
4341 old_val = release_value (value_copy (other.old_val.get ()));
04afa70c
TT
4342}
4343
c906108c
SS
4344/* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
4345 is part of the bpstat is copied as well. */
4346
4347bpstat
fba45db2 4348bpstat_copy (bpstat bs)
c906108c
SS
4349{
4350 bpstat p = NULL;
4351 bpstat tmp;
4352 bpstat retval = NULL;
4353
4354 if (bs == NULL)
4355 return bs;
4356
4357 for (; bs != NULL; bs = bs->next)
4358 {
04afa70c 4359 tmp = new bpstats (*bs);
31cc81e9 4360
c906108c
SS
4361 if (p == NULL)
4362 /* This is the first thing in the chain. */
4363 retval = tmp;
4364 else
4365 p->next = tmp;
4366 p = tmp;
4367 }
4368 p->next = NULL;
4369 return retval;
4370}
4371
4a64f543 4372/* Find the bpstat associated with this breakpoint. */
c906108c
SS
4373
4374bpstat
fba45db2 4375bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
c906108c 4376{
c5aa993b
JM
4377 if (bsp == NULL)
4378 return NULL;
c906108c 4379
c5aa993b
JM
4380 for (; bsp != NULL; bsp = bsp->next)
4381 {
f431efe5 4382 if (bsp->breakpoint_at == breakpoint)
c5aa993b
JM
4383 return bsp;
4384 }
c906108c
SS
4385 return NULL;
4386}
4387
ab04a2af
TT
4388/* See breakpoint.h. */
4389
4c462cb0 4390bool
427cd150 4391bpstat_explains_signal (bpstat bsp, enum gdb_signal sig)
ab04a2af 4392{
ab04a2af
TT
4393 for (; bsp != NULL; bsp = bsp->next)
4394 {
427cd150
TT
4395 if (bsp->breakpoint_at == NULL)
4396 {
4397 /* A moribund location can never explain a signal other than
4398 GDB_SIGNAL_TRAP. */
4399 if (sig == GDB_SIGNAL_TRAP)
4c462cb0 4400 return true;
427cd150
TT
4401 }
4402 else
47591c29
PA
4403 {
4404 if (bsp->breakpoint_at->ops->explains_signal (bsp->breakpoint_at,
4405 sig))
4c462cb0 4406 return true;
47591c29 4407 }
ab04a2af
TT
4408 }
4409
4c462cb0 4410 return false;
ab04a2af
TT
4411}
4412
4a64f543
MS
4413/* Put in *NUM the breakpoint number of the first breakpoint we are
4414 stopped at. *BSP upon return is a bpstat which points to the
4415 remaining breakpoints stopped at (but which is not guaranteed to be
4416 good for anything but further calls to bpstat_num).
4417
8671a17b
PA
4418 Return 0 if passed a bpstat which does not indicate any breakpoints.
4419 Return -1 if stopped at a breakpoint that has been deleted since
4420 we set it.
4421 Return 1 otherwise. */
c906108c
SS
4422
4423int
8671a17b 4424bpstat_num (bpstat *bsp, int *num)
c906108c
SS
4425{
4426 struct breakpoint *b;
4427
4428 if ((*bsp) == NULL)
4429 return 0; /* No more breakpoint values */
8671a17b 4430
4a64f543
MS
4431 /* We assume we'll never have several bpstats that correspond to a
4432 single breakpoint -- otherwise, this function might return the
4433 same number more than once and this will look ugly. */
f431efe5 4434 b = (*bsp)->breakpoint_at;
8671a17b
PA
4435 *bsp = (*bsp)->next;
4436 if (b == NULL)
4437 return -1; /* breakpoint that's been deleted since */
4438
4439 *num = b->number; /* We have its number */
4440 return 1;
c906108c
SS
4441}
4442
e93ca019 4443/* See breakpoint.h. */
c906108c
SS
4444
4445void
e93ca019 4446bpstat_clear_actions (void)
c906108c 4447{
e93ca019
JK
4448 bpstat bs;
4449
00431a78 4450 if (inferior_ptid == null_ptid)
e93ca019
JK
4451 return;
4452
00431a78 4453 thread_info *tp = inferior_thread ();
e93ca019 4454 for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
c906108c 4455 {
d1b0a7bf 4456 bs->commands = NULL;
850645cf 4457 bs->old_val.reset (nullptr);
c906108c
SS
4458 }
4459}
4460
f3b1572e
PA
4461/* Called when a command is about to proceed the inferior. */
4462
4463static void
4464breakpoint_about_to_proceed (void)
4465{
d7e15655 4466 if (inferior_ptid != null_ptid)
f3b1572e
PA
4467 {
4468 struct thread_info *tp = inferior_thread ();
4469
4470 /* Allow inferior function calls in breakpoint commands to not
4471 interrupt the command list. When the call finishes
4472 successfully, the inferior will be standing at the same
4473 breakpoint as if nothing happened. */
16c381f0 4474 if (tp->control.in_infcall)
f3b1572e
PA
4475 return;
4476 }
4477
4478 breakpoint_proceeded = 1;
4479}
4480
abf85f46
JK
4481/* Return non-zero iff CMD as the first line of a command sequence is `silent'
4482 or its equivalent. */
4483
4484static int
4485command_line_is_silent (struct command_line *cmd)
4486{
4f45d445 4487 return cmd && (strcmp ("silent", cmd->line) == 0);
abf85f46
JK
4488}
4489
4a64f543
MS
4490/* Execute all the commands associated with all the breakpoints at
4491 this location. Any of these commands could cause the process to
4492 proceed beyond this point, etc. We look out for such changes by
4493 checking the global "breakpoint_proceeded" after each command.
c906108c 4494
347bddb7
PA
4495 Returns true if a breakpoint command resumed the inferior. In that
4496 case, it is the caller's responsibility to recall it again with the
4497 bpstat of the current thread. */
4498
4499static int
4500bpstat_do_actions_1 (bpstat *bsp)
c906108c
SS
4501{
4502 bpstat bs;
347bddb7 4503 int again = 0;
c906108c
SS
4504
4505 /* Avoid endless recursion if a `source' command is contained
4506 in bs->commands. */
4507 if (executing_breakpoint_commands)
347bddb7 4508 return 0;
c906108c 4509
81b1e71c
TT
4510 scoped_restore save_executing
4511 = make_scoped_restore (&executing_breakpoint_commands, 1);
c906108c 4512
1ac32117 4513 scoped_restore preventer = prevent_dont_repeat ();
cf6c5ffb 4514
4a64f543 4515 /* This pointer will iterate over the list of bpstat's. */
c906108c
SS
4516 bs = *bsp;
4517
4518 breakpoint_proceeded = 0;
4519 for (; bs != NULL; bs = bs->next)
4520 {
d1b0a7bf 4521 struct command_line *cmd = NULL;
6c50ab1c
JB
4522
4523 /* Take ownership of the BSP's command tree, if it has one.
4524
dda83cd7
SM
4525 The command tree could legitimately contain commands like
4526 'step' and 'next', which call clear_proceed_status, which
4527 frees stop_bpstat's command tree. To make sure this doesn't
4528 free the tree we're executing out from under us, we need to
4529 take ownership of the tree ourselves. Since a given bpstat's
4530 commands are only executed once, we don't need to copy it; we
4531 can clear the pointer in the bpstat, and make sure we free
4532 the tree when we're done. */
d1b0a7bf 4533 counted_command_line ccmd = bs->commands;
9add0f1b 4534 bs->commands = NULL;
d1b0a7bf
TT
4535 if (ccmd != NULL)
4536 cmd = ccmd.get ();
abf85f46
JK
4537 if (command_line_is_silent (cmd))
4538 {
4539 /* The action has been already done by bpstat_stop_status. */
4540 cmd = cmd->next;
4541 }
6c50ab1c 4542
c906108c
SS
4543 while (cmd != NULL)
4544 {
4545 execute_control_command (cmd);
4546
4547 if (breakpoint_proceeded)
4548 break;
4549 else
4550 cmd = cmd->next;
4551 }
6c50ab1c 4552
c906108c 4553 if (breakpoint_proceeded)
32c1e744 4554 {
cb814510 4555 if (current_ui->async)
347bddb7
PA
4556 /* If we are in async mode, then the target might be still
4557 running, not stopped at any breakpoint, so nothing for
4558 us to do here -- just return to the event loop. */
4559 ;
32c1e744
VP
4560 else
4561 /* In sync mode, when execute_control_command returns
4562 we're already standing on the next breakpoint.
347bddb7
PA
4563 Breakpoint commands for that stop were not run, since
4564 execute_command does not run breakpoint commands --
4565 only command_line_handler does, but that one is not
4566 involved in execution of breakpoint commands. So, we
4567 can now execute breakpoint commands. It should be
4568 noted that making execute_command do bpstat actions is
4569 not an option -- in this case we'll have recursive
4570 invocation of bpstat for each breakpoint with a
4571 command, and can easily blow up GDB stack. Instead, we
4572 return true, which will trigger the caller to recall us
4573 with the new stop_bpstat. */
4574 again = 1;
4575 break;
32c1e744 4576 }
c906108c 4577 }
347bddb7
PA
4578 return again;
4579}
4580
00431a78
PA
4581/* Helper for bpstat_do_actions. Get the current thread, if there's
4582 one, is alive and has execution. Return NULL otherwise. */
4583
4584static thread_info *
4585get_bpstat_thread ()
4586{
55f6301a 4587 if (inferior_ptid == null_ptid || !target_has_execution ())
00431a78
PA
4588 return NULL;
4589
4590 thread_info *tp = inferior_thread ();
4591 if (tp->state == THREAD_EXITED || tp->executing)
4592 return NULL;
4593 return tp;
4594}
4595
347bddb7
PA
4596void
4597bpstat_do_actions (void)
4598{
694c6bf5 4599 auto cleanup_if_error = make_scope_exit (bpstat_clear_actions);
00431a78 4600 thread_info *tp;
353d1d73 4601
347bddb7 4602 /* Do any commands attached to breakpoint we are stopped at. */
00431a78
PA
4603 while ((tp = get_bpstat_thread ()) != NULL)
4604 {
4605 /* Since in sync mode, bpstat_do_actions may resume the
4606 inferior, and only return when it is stopped at the next
4607 breakpoint, we keep doing breakpoint actions until it returns
4608 false to indicate the inferior was not resumed. */
4609 if (!bpstat_do_actions_1 (&tp->control.stop_bpstat))
4610 break;
4611 }
353d1d73 4612
694c6bf5 4613 cleanup_if_error.release ();
c906108c
SS
4614}
4615
fa4727a6
DJ
4616/* Print out the (old or new) value associated with a watchpoint. */
4617
4618static void
4619watchpoint_value_print (struct value *val, struct ui_file *stream)
4620{
4621 if (val == NULL)
7f6aba03 4622 fprintf_styled (stream, metadata_style.style (), _("<unreadable>"));
fa4727a6 4623 else
79a45b7d
TT
4624 {
4625 struct value_print_options opts;
4626 get_user_print_options (&opts);
4627 value_print (val, stream, &opts);
4628 }
fa4727a6
DJ
4629}
4630
f303dbd6
PA
4631/* Print the "Thread ID hit" part of "Thread ID hit Breakpoint N" if
4632 debugging multiple threads. */
4633
4634void
4635maybe_print_thread_hit_breakpoint (struct ui_out *uiout)
4636{
112e8700 4637 if (uiout->is_mi_like_p ())
f303dbd6
PA
4638 return;
4639
112e8700 4640 uiout->text ("\n");
f303dbd6
PA
4641
4642 if (show_thread_that_caused_stop ())
4643 {
4644 const char *name;
4645 struct thread_info *thr = inferior_thread ();
4646
112e8700 4647 uiout->text ("Thread ");
33eca680 4648 uiout->field_string ("thread-id", print_thread_id (thr));
f303dbd6
PA
4649
4650 name = thr->name != NULL ? thr->name : target_thread_name (thr);
4651 if (name != NULL)
4652 {
112e8700 4653 uiout->text (" \"");
33eca680 4654 uiout->field_string ("name", name);
112e8700 4655 uiout->text ("\"");
f303dbd6
PA
4656 }
4657
112e8700 4658 uiout->text (" hit ");
f303dbd6
PA
4659 }
4660}
4661
e514a9d6 4662/* Generic routine for printing messages indicating why we
4a64f543 4663 stopped. The behavior of this function depends on the value
e514a9d6
JM
4664 'print_it' in the bpstat structure. Under some circumstances we
4665 may decide not to print anything here and delegate the task to
4a64f543 4666 normal_stop(). */
e514a9d6
JM
4667
4668static enum print_stop_action
4669print_bp_stop_message (bpstat bs)
4670{
4671 switch (bs->print_it)
4672 {
4673 case print_it_noop:
4a64f543 4674 /* Nothing should be printed for this bpstat entry. */
e514a9d6
JM
4675 return PRINT_UNKNOWN;
4676 break;
4677
4678 case print_it_done:
4679 /* We still want to print the frame, but we already printed the
dda83cd7 4680 relevant messages. */
e514a9d6
JM
4681 return PRINT_SRC_AND_LOC;
4682 break;
4683
4684 case print_it_normal:
4f8d1dc6 4685 {
f431efe5
PA
4686 struct breakpoint *b = bs->breakpoint_at;
4687
1a6a67de
TJB
4688 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
4689 which has since been deleted. */
4690 if (b == NULL)
4691 return PRINT_UNKNOWN;
4692
348d480f
PA
4693 /* Normal case. Call the breakpoint's print_it method. */
4694 return b->ops->print_it (bs);
4f8d1dc6 4695 }
348d480f 4696 break;
3086aeae 4697
e514a9d6 4698 default:
8e65ff28 4699 internal_error (__FILE__, __LINE__,
e2e0b3e5 4700 _("print_bp_stop_message: unrecognized enum value"));
e514a9d6 4701 break;
c906108c 4702 }
c906108c
SS
4703}
4704
edcc5120
TT
4705/* A helper function that prints a shared library stopped event. */
4706
4707static void
4708print_solib_event (int is_catchpoint)
4709{
6fb16ce6 4710 bool any_deleted = !current_program_space->deleted_solibs.empty ();
bcb430e4 4711 bool any_added = !current_program_space->added_solibs.empty ();
edcc5120
TT
4712
4713 if (!is_catchpoint)
4714 {
4715 if (any_added || any_deleted)
112e8700 4716 current_uiout->text (_("Stopped due to shared library event:\n"));
edcc5120 4717 else
112e8700
SM
4718 current_uiout->text (_("Stopped due to shared library event (no "
4719 "libraries added or removed)\n"));
edcc5120
TT
4720 }
4721
112e8700
SM
4722 if (current_uiout->is_mi_like_p ())
4723 current_uiout->field_string ("reason",
4724 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
edcc5120
TT
4725
4726 if (any_deleted)
4727 {
112e8700 4728 current_uiout->text (_(" Inferior unloaded "));
10f489e5 4729 ui_out_emit_list list_emitter (current_uiout, "removed");
6fb16ce6 4730 for (int ix = 0; ix < current_program_space->deleted_solibs.size (); ix++)
edcc5120 4731 {
6fb16ce6
SM
4732 const std::string &name = current_program_space->deleted_solibs[ix];
4733
edcc5120 4734 if (ix > 0)
112e8700
SM
4735 current_uiout->text (" ");
4736 current_uiout->field_string ("library", name);
4737 current_uiout->text ("\n");
edcc5120 4738 }
edcc5120
TT
4739 }
4740
4741 if (any_added)
4742 {
112e8700 4743 current_uiout->text (_(" Inferior loaded "));
10f489e5 4744 ui_out_emit_list list_emitter (current_uiout, "added");
bcb430e4 4745 bool first = true;
52941706 4746 for (so_list *iter : current_program_space->added_solibs)
edcc5120 4747 {
bcb430e4 4748 if (!first)
112e8700 4749 current_uiout->text (" ");
bcb430e4 4750 first = false;
112e8700
SM
4751 current_uiout->field_string ("library", iter->so_name);
4752 current_uiout->text ("\n");
edcc5120 4753 }
edcc5120
TT
4754 }
4755}
4756
e514a9d6
JM
4757/* Print a message indicating what happened. This is called from
4758 normal_stop(). The input to this routine is the head of the bpstat
36dfb11c
TT
4759 list - a list of the eventpoints that caused this stop. KIND is
4760 the target_waitkind for the stopping event. This
e514a9d6
JM
4761 routine calls the generic print routine for printing a message
4762 about reasons for stopping. This will print (for example) the
4763 "Breakpoint n," part of the output. The return value of this
4764 routine is one of:
c906108c 4765
4a64f543 4766 PRINT_UNKNOWN: Means we printed nothing.
917317f4 4767 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4a64f543 4768 code to print the location. An example is
c5aa993b
JM
4769 "Breakpoint 1, " which should be followed by
4770 the location.
917317f4 4771 PRINT_SRC_ONLY: Means we printed something, but there is no need
c5aa993b
JM
4772 to also print the location part of the message.
4773 An example is the catch/throw messages, which
4a64f543 4774 don't require a location appended to the end.
917317f4 4775 PRINT_NOTHING: We have done some printing and we don't need any
4a64f543 4776 further info to be printed. */
c906108c 4777
917317f4 4778enum print_stop_action
36dfb11c 4779bpstat_print (bpstat bs, int kind)
c906108c 4780{
f486487f 4781 enum print_stop_action val;
c5aa993b 4782
c906108c 4783 /* Maybe another breakpoint in the chain caused us to stop.
53a5351d
JM
4784 (Currently all watchpoints go on the bpstat whether hit or not.
4785 That probably could (should) be changed, provided care is taken
c906108c 4786 with respect to bpstat_explains_signal). */
e514a9d6
JM
4787 for (; bs; bs = bs->next)
4788 {
4789 val = print_bp_stop_message (bs);
4790 if (val == PRINT_SRC_ONLY
4791 || val == PRINT_SRC_AND_LOC
4792 || val == PRINT_NOTHING)
4793 return val;
4794 }
c906108c 4795
36dfb11c
TT
4796 /* If we had hit a shared library event breakpoint,
4797 print_bp_stop_message would print out this message. If we hit an
4798 OS-level shared library event, do the same thing. */
4799 if (kind == TARGET_WAITKIND_LOADED)
4800 {
edcc5120 4801 print_solib_event (0);
36dfb11c
TT
4802 return PRINT_NOTHING;
4803 }
4804
e514a9d6 4805 /* We reached the end of the chain, or we got a null BS to start
4a64f543 4806 with and nothing was printed. */
917317f4 4807 return PRINT_UNKNOWN;
c906108c
SS
4808}
4809
bf469271 4810/* Evaluate the boolean expression EXP and return the result. */
c906108c 4811
bf469271
PA
4812static bool
4813breakpoint_cond_eval (expression *exp)
c906108c 4814{
278cd55f 4815 struct value *mark = value_mark ();
bf469271 4816 bool res = value_true (evaluate_expression (exp));
cc59ec59 4817
c906108c 4818 value_free_to_mark (mark);
bf469271 4819 return res;
c906108c
SS
4820}
4821
5760d0ab 4822/* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
c906108c 4823
04afa70c
TT
4824bpstats::bpstats (struct bp_location *bl, bpstat **bs_link_pointer)
4825 : next (NULL),
b6433ede 4826 bp_location_at (bp_location_ref_ptr::new_reference (bl)),
04afa70c
TT
4827 breakpoint_at (bl->owner),
4828 commands (NULL),
04afa70c
TT
4829 print (0),
4830 stop (0),
4831 print_it (print_it_normal)
c906108c 4832{
04afa70c
TT
4833 **bs_link_pointer = this;
4834 *bs_link_pointer = &next;
4835}
4836
4837bpstats::bpstats ()
4838 : next (NULL),
04afa70c
TT
4839 breakpoint_at (NULL),
4840 commands (NULL),
04afa70c
TT
4841 print (0),
4842 stop (0),
4843 print_it (print_it_normal)
4844{
c906108c
SS
4845}
4846\f
d983da9c
DJ
4847/* The target has stopped with waitstatus WS. Check if any hardware
4848 watchpoints have triggered, according to the target. */
4849
4850int
4851watchpoints_triggered (struct target_waitstatus *ws)
4852{
57810aa7 4853 bool stopped_by_watchpoint = target_stopped_by_watchpoint ();
d983da9c
DJ
4854 CORE_ADDR addr;
4855 struct breakpoint *b;
4856
4857 if (!stopped_by_watchpoint)
4858 {
4859 /* We were not stopped by a watchpoint. Mark all watchpoints
4860 as not triggered. */
4861 ALL_BREAKPOINTS (b)
cc60f2e3 4862 if (is_hardware_watchpoint (b))
3a5c3e22
PA
4863 {
4864 struct watchpoint *w = (struct watchpoint *) b;
4865
4866 w->watchpoint_triggered = watch_triggered_no;
4867 }
d983da9c
DJ
4868
4869 return 0;
4870 }
4871
328d42d8 4872 if (!target_stopped_data_address (current_inferior ()->top_target (), &addr))
d983da9c
DJ
4873 {
4874 /* We were stopped by a watchpoint, but we don't know where.
4875 Mark all watchpoints as unknown. */
4876 ALL_BREAKPOINTS (b)
cc60f2e3 4877 if (is_hardware_watchpoint (b))
3a5c3e22
PA
4878 {
4879 struct watchpoint *w = (struct watchpoint *) b;
4880
4881 w->watchpoint_triggered = watch_triggered_unknown;
4882 }
d983da9c 4883
3c4797ba 4884 return 1;
d983da9c
DJ
4885 }
4886
4887 /* The target could report the data address. Mark watchpoints
4888 affected by this data address as triggered, and all others as not
4889 triggered. */
4890
4891 ALL_BREAKPOINTS (b)
cc60f2e3 4892 if (is_hardware_watchpoint (b))
d983da9c 4893 {
3a5c3e22 4894 struct watchpoint *w = (struct watchpoint *) b;
a5606eee 4895 struct bp_location *loc;
d983da9c 4896
3a5c3e22 4897 w->watchpoint_triggered = watch_triggered_no;
a5606eee 4898 for (loc = b->loc; loc; loc = loc->next)
9c06b0b4 4899 {
3a5c3e22 4900 if (is_masked_watchpoint (b))
9c06b0b4 4901 {
3a5c3e22
PA
4902 CORE_ADDR newaddr = addr & w->hw_wp_mask;
4903 CORE_ADDR start = loc->address & w->hw_wp_mask;
9c06b0b4
TJB
4904
4905 if (newaddr == start)
4906 {
3a5c3e22 4907 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
4908 break;
4909 }
4910 }
4911 /* Exact match not required. Within range is sufficient. */
328d42d8
SM
4912 else if (target_watchpoint_addr_within_range
4913 (current_inferior ()->top_target (), addr, loc->address,
4914 loc->length))
9c06b0b4 4915 {
3a5c3e22 4916 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
4917 break;
4918 }
4919 }
d983da9c
DJ
4920 }
4921
4922 return 1;
4923}
4924
bf469271
PA
4925/* Possible return values for watchpoint_check. */
4926enum wp_check_result
4927 {
4928 /* The watchpoint has been deleted. */
4929 WP_DELETED = 1,
4930
4931 /* The value has changed. */
4932 WP_VALUE_CHANGED = 2,
4933
4934 /* The value has not changed. */
4935 WP_VALUE_NOT_CHANGED = 3,
4936
4937 /* Ignore this watchpoint, no matter if the value changed or not. */
4938 WP_IGNORE = 4,
4939 };
c906108c
SS
4940
4941#define BP_TEMPFLAG 1
4942#define BP_HARDWAREFLAG 2
4943
4a64f543 4944/* Evaluate watchpoint condition expression and check if its value
bf469271 4945 changed. */
553e4c11 4946
bf469271
PA
4947static wp_check_result
4948watchpoint_check (bpstat bs)
c906108c 4949{
3a5c3e22 4950 struct watchpoint *b;
c906108c
SS
4951 struct frame_info *fr;
4952 int within_current_scope;
4953
f431efe5 4954 /* BS is built from an existing struct breakpoint. */
2bdf28a0 4955 gdb_assert (bs->breakpoint_at != NULL);
3a5c3e22 4956 b = (struct watchpoint *) bs->breakpoint_at;
d0fb5eae 4957
f6bc2008
PA
4958 /* If this is a local watchpoint, we only want to check if the
4959 watchpoint frame is in scope if the current thread is the thread
4960 that was used to create the watchpoint. */
4961 if (!watchpoint_in_thread_scope (b))
60e1c644 4962 return WP_IGNORE;
f6bc2008 4963
c906108c
SS
4964 if (b->exp_valid_block == NULL)
4965 within_current_scope = 1;
4966 else
4967 {
edb3359d
DJ
4968 struct frame_info *frame = get_current_frame ();
4969 struct gdbarch *frame_arch = get_frame_arch (frame);
4970 CORE_ADDR frame_pc = get_frame_pc (frame);
4971
c9cf6e20 4972 /* stack_frame_destroyed_p() returns a non-zero value if we're
4a64f543
MS
4973 still in the function but the stack frame has already been
4974 invalidated. Since we can't rely on the values of local
4975 variables after the stack has been destroyed, we are treating
4976 the watchpoint in that state as `not changed' without further
4977 checking. Don't mark watchpoints as changed if the current
4978 frame is in an epilogue - even if they are in some other
4979 frame, our view of the stack is likely to be wrong and
4980 frame_find_by_id could error out. */
c9cf6e20 4981 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
60e1c644 4982 return WP_IGNORE;
a0f49112 4983
101dcfbe 4984 fr = frame_find_by_id (b->watchpoint_frame);
c906108c 4985 within_current_scope = (fr != NULL);
69fbadd5
DJ
4986
4987 /* If we've gotten confused in the unwinder, we might have
4988 returned a frame that can't describe this variable. */
edb3359d
DJ
4989 if (within_current_scope)
4990 {
4991 struct symbol *function;
4992
4993 function = get_frame_function (fr);
4994 if (function == NULL
4995 || !contained_in (b->exp_valid_block,
4996 SYMBOL_BLOCK_VALUE (function)))
4997 within_current_scope = 0;
4998 }
69fbadd5 4999
edb3359d 5000 if (within_current_scope)
c906108c
SS
5001 /* If we end up stopping, the current frame will get selected
5002 in normal_stop. So this call to select_frame won't affect
5003 the user. */
0f7d239c 5004 select_frame (fr);
c906108c 5005 }
c5aa993b 5006
c906108c
SS
5007 if (within_current_scope)
5008 {
4a64f543 5009 /* We use value_{,free_to_}mark because it could be a *long*
dda83cd7
SM
5010 time before we return to the command level and call
5011 free_all_values. We can't call free_all_values because we
5012 might be in the middle of evaluating a function call. */
c906108c 5013
9c06b0b4 5014 struct value *mark;
fa4727a6
DJ
5015 struct value *new_val;
5016
c1fc2657 5017 if (is_masked_watchpoint (b))
9c06b0b4
TJB
5018 /* Since we don't know the exact trigger address (from
5019 stopped_data_address), just tell the user we've triggered
5020 a mask watchpoint. */
5021 return WP_VALUE_CHANGED;
5022
5023 mark = value_mark ();
1eaebe02 5024 fetch_subexp_value (b->exp.get (), b->exp->op.get (), &new_val,
413403fc 5025 NULL, NULL, false);
218d2fc6 5026
bb9d5f81
PP
5027 if (b->val_bitsize != 0)
5028 new_val = extract_bitfield_from_watchpoint_value (b, new_val);
5029
4a64f543
MS
5030 /* We use value_equal_contents instead of value_equal because
5031 the latter coerces an array to a pointer, thus comparing just
5032 the address of the array instead of its contents. This is
5033 not what we want. */
fa4727a6 5034 if ((b->val != NULL) != (new_val != NULL)
850645cf
TT
5035 || (b->val != NULL && !value_equal_contents (b->val.get (),
5036 new_val)))
c906108c 5037 {
c906108c 5038 bs->old_val = b->val;
850645cf 5039 b->val = release_value (new_val);
4c1d86d9 5040 b->val_valid = true;
850645cf
TT
5041 if (new_val != NULL)
5042 value_free_to_mark (mark);
c906108c
SS
5043 return WP_VALUE_CHANGED;
5044 }
5045 else
5046 {
60e1c644 5047 /* Nothing changed. */
c906108c 5048 value_free_to_mark (mark);
c906108c
SS
5049 return WP_VALUE_NOT_CHANGED;
5050 }
5051 }
5052 else
5053 {
5054 /* This seems like the only logical thing to do because
dda83cd7
SM
5055 if we temporarily ignored the watchpoint, then when
5056 we reenter the block in which it is valid it contains
5057 garbage (in the case of a function, it may have two
5058 garbage values, one before and one after the prologue).
5059 So we can't even detect the first assignment to it and
5060 watch after that (since the garbage may or may not equal
5061 the first value assigned). */
348d480f
PA
5062 /* We print all the stop information in
5063 breakpoint_ops->print_it, but in this case, by the time we
5064 call breakpoint_ops->print_it this bp will be deleted
5065 already. So we have no choice but print the information
5066 here. */
468afe6c 5067
0e454242 5068 SWITCH_THRU_ALL_UIS ()
dda83cd7 5069 {
468afe6c
PA
5070 struct ui_out *uiout = current_uiout;
5071
112e8700
SM
5072 if (uiout->is_mi_like_p ())
5073 uiout->field_string
5074 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
6a831f06
PA
5075 uiout->message ("\nWatchpoint %pF deleted because the program has "
5076 "left the block in\n"
5077 "which its expression is valid.\n",
5078 signed_field ("wpnum", b->number));
468afe6c 5079 }
4ce44c66 5080
cdac0397 5081 /* Make sure the watchpoint's commands aren't executed. */
d1b0a7bf 5082 b->commands = NULL;
d0fb5eae 5083 watchpoint_del_at_next_stop (b);
c906108c
SS
5084
5085 return WP_DELETED;
5086 }
5087}
5088
18a18393 5089/* Return true if it looks like target has stopped due to hitting
348d480f
PA
5090 breakpoint location BL. This function does not check if we should
5091 stop, only if BL explains the stop. */
5092
18a18393 5093static int
6c95b8df 5094bpstat_check_location (const struct bp_location *bl,
accd0bcd 5095 const address_space *aspace, CORE_ADDR bp_addr,
09ac7c10 5096 const struct target_waitstatus *ws)
18a18393
VP
5097{
5098 struct breakpoint *b = bl->owner;
5099
348d480f 5100 /* BL is from an existing breakpoint. */
2bdf28a0
JK
5101 gdb_assert (b != NULL);
5102
bd522513 5103 return b->ops->breakpoint_hit (bl, aspace, bp_addr, ws);
18a18393
VP
5104}
5105
3a5c3e22
PA
5106/* Determine if the watched values have actually changed, and we
5107 should stop. If not, set BS->stop to 0. */
5108
18a18393
VP
5109static void
5110bpstat_check_watchpoint (bpstat bs)
5111{
2bdf28a0 5112 const struct bp_location *bl;
3a5c3e22 5113 struct watchpoint *b;
2bdf28a0
JK
5114
5115 /* BS is built for existing struct breakpoint. */
b6433ede 5116 bl = bs->bp_location_at.get ();
2bdf28a0 5117 gdb_assert (bl != NULL);
3a5c3e22 5118 b = (struct watchpoint *) bs->breakpoint_at;
2bdf28a0 5119 gdb_assert (b != NULL);
18a18393 5120
18a18393 5121 {
18a18393
VP
5122 int must_check_value = 0;
5123
c1fc2657 5124 if (b->type == bp_watchpoint)
18a18393
VP
5125 /* For a software watchpoint, we must always check the
5126 watched value. */
5127 must_check_value = 1;
5128 else if (b->watchpoint_triggered == watch_triggered_yes)
5129 /* We have a hardware watchpoint (read, write, or access)
5130 and the target earlier reported an address watched by
5131 this watchpoint. */
5132 must_check_value = 1;
5133 else if (b->watchpoint_triggered == watch_triggered_unknown
c1fc2657 5134 && b->type == bp_hardware_watchpoint)
18a18393
VP
5135 /* We were stopped by a hardware watchpoint, but the target could
5136 not report the data address. We must check the watchpoint's
5137 value. Access and read watchpoints are out of luck; without
5138 a data address, we can't figure it out. */
5139 must_check_value = 1;
3a5c3e22 5140
18a18393
VP
5141 if (must_check_value)
5142 {
bf469271
PA
5143 wp_check_result e;
5144
a70b8144 5145 try
bf469271
PA
5146 {
5147 e = watchpoint_check (bs);
5148 }
230d2906 5149 catch (const gdb_exception &ex)
bf469271
PA
5150 {
5151 exception_fprintf (gdb_stderr, ex,
5152 "Error evaluating expression "
5153 "for watchpoint %d\n",
5154 b->number);
5155
5156 SWITCH_THRU_ALL_UIS ()
5157 {
5158 printf_filtered (_("Watchpoint %d deleted.\n"),
5159 b->number);
5160 }
5161 watchpoint_del_at_next_stop (b);
5162 e = WP_DELETED;
5163 }
bf469271 5164
18a18393
VP
5165 switch (e)
5166 {
5167 case WP_DELETED:
5168 /* We've already printed what needs to be printed. */
5169 bs->print_it = print_it_done;
5170 /* Stop. */
5171 break;
60e1c644
PA
5172 case WP_IGNORE:
5173 bs->print_it = print_it_noop;
5174 bs->stop = 0;
5175 break;
18a18393 5176 case WP_VALUE_CHANGED:
c1fc2657 5177 if (b->type == bp_read_watchpoint)
18a18393 5178 {
85d721b8
PA
5179 /* There are two cases to consider here:
5180
4a64f543 5181 1. We're watching the triggered memory for reads.
85d721b8
PA
5182 In that case, trust the target, and always report
5183 the watchpoint hit to the user. Even though
5184 reads don't cause value changes, the value may
5185 have changed since the last time it was read, and
5186 since we're not trapping writes, we will not see
5187 those, and as such we should ignore our notion of
5188 old value.
5189
4a64f543 5190 2. We're watching the triggered memory for both
85d721b8
PA
5191 reads and writes. There are two ways this may
5192 happen:
5193
4a64f543 5194 2.1. This is a target that can't break on data
85d721b8
PA
5195 reads only, but can break on accesses (reads or
5196 writes), such as e.g., x86. We detect this case
5197 at the time we try to insert read watchpoints.
5198
4a64f543 5199 2.2. Otherwise, the target supports read
85d721b8
PA
5200 watchpoints, but, the user set an access or write
5201 watchpoint watching the same memory as this read
5202 watchpoint.
5203
5204 If we're watching memory writes as well as reads,
5205 ignore watchpoint hits when we find that the
5206 value hasn't changed, as reads don't cause
5207 changes. This still gives false positives when
5208 the program writes the same value to memory as
5209 what there was already in memory (we will confuse
5210 it for a read), but it's much better than
5211 nothing. */
5212
5213 int other_write_watchpoint = 0;
5214
5215 if (bl->watchpoint_type == hw_read)
5216 {
5217 struct breakpoint *other_b;
5218
5219 ALL_BREAKPOINTS (other_b)
3a5c3e22
PA
5220 if (other_b->type == bp_hardware_watchpoint
5221 || other_b->type == bp_access_watchpoint)
85d721b8 5222 {
3a5c3e22
PA
5223 struct watchpoint *other_w =
5224 (struct watchpoint *) other_b;
5225
5226 if (other_w->watchpoint_triggered
5227 == watch_triggered_yes)
5228 {
5229 other_write_watchpoint = 1;
5230 break;
5231 }
85d721b8
PA
5232 }
5233 }
5234
5235 if (other_write_watchpoint
5236 || bl->watchpoint_type == hw_access)
5237 {
5238 /* We're watching the same memory for writes,
5239 and the value changed since the last time we
5240 updated it, so this trap must be for a write.
5241 Ignore it. */
5242 bs->print_it = print_it_noop;
5243 bs->stop = 0;
5244 }
18a18393
VP
5245 }
5246 break;
5247 case WP_VALUE_NOT_CHANGED:
c1fc2657
SM
5248 if (b->type == bp_hardware_watchpoint
5249 || b->type == bp_watchpoint)
18a18393
VP
5250 {
5251 /* Don't stop: write watchpoints shouldn't fire if
5252 the value hasn't changed. */
5253 bs->print_it = print_it_noop;
5254 bs->stop = 0;
5255 }
5256 /* Stop. */
5257 break;
5258 default:
5259 /* Can't happen. */
18a18393
VP
5260 break;
5261 }
5262 }
5263 else /* must_check_value == 0 */
5264 {
5265 /* This is a case where some watchpoint(s) triggered, but
5266 not at the address of this watchpoint, or else no
5267 watchpoint triggered after all. So don't print
5268 anything for this watchpoint. */
5269 bs->print_it = print_it_noop;
5270 bs->stop = 0;
5271 }
5272 }
5273}
5274
7d4df6a4
DE
5275/* For breakpoints that are currently marked as telling gdb to stop,
5276 check conditions (condition proper, frame, thread and ignore count)
18a18393
VP
5277 of breakpoint referred to by BS. If we should not stop for this
5278 breakpoint, set BS->stop to 0. */
f431efe5 5279
18a18393 5280static void
00431a78 5281bpstat_check_breakpoint_conditions (bpstat bs, thread_info *thread)
18a18393 5282{
2bdf28a0
JK
5283 const struct bp_location *bl;
5284 struct breakpoint *b;
bf469271
PA
5285 /* Assume stop. */
5286 bool condition_result = true;
7d4df6a4
DE
5287 struct expression *cond;
5288
5289 gdb_assert (bs->stop);
2bdf28a0
JK
5290
5291 /* BS is built for existing struct breakpoint. */
b6433ede 5292 bl = bs->bp_location_at.get ();
2bdf28a0 5293 gdb_assert (bl != NULL);
f431efe5 5294 b = bs->breakpoint_at;
2bdf28a0 5295 gdb_assert (b != NULL);
18a18393 5296
b775012e
LM
5297 /* Even if the target evaluated the condition on its end and notified GDB, we
5298 need to do so again since GDB does not know if we stopped due to a
5299 breakpoint or a single step breakpoint. */
5300
18a18393 5301 if (frame_id_p (b->frame_id)
edb3359d 5302 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
18a18393 5303 {
7d4df6a4
DE
5304 bs->stop = 0;
5305 return;
5306 }
60e1c644 5307
12ab52e9
PA
5308 /* If this is a thread/task-specific breakpoint, don't waste cpu
5309 evaluating the condition if this isn't the specified
5310 thread/task. */
00431a78
PA
5311 if ((b->thread != -1 && b->thread != thread->global_num)
5312 || (b->task != 0 && b->task != ada_get_task_number (thread)))
6c1b0f7b
DE
5313 {
5314 bs->stop = 0;
5315 return;
5316 }
5317
6dddc817
DE
5318 /* Evaluate extension language breakpoints that have a "stop" method
5319 implemented. */
5320 bs->stop = breakpoint_ext_lang_cond_says_stop (b);
7371cf6d 5321
7d4df6a4
DE
5322 if (is_watchpoint (b))
5323 {
5324 struct watchpoint *w = (struct watchpoint *) b;
3a5c3e22 5325
4d01a485 5326 cond = w->cond_exp.get ();
7d4df6a4
DE
5327 }
5328 else
4d01a485 5329 cond = bl->cond.get ();
60e1c644 5330
7d4df6a4
DE
5331 if (cond && b->disposition != disp_del_at_next_stop)
5332 {
5333 int within_current_scope = 1;
5334 struct watchpoint * w;
60e1c644 5335
7d4df6a4
DE
5336 /* We use value_mark and value_free_to_mark because it could
5337 be a long time before we return to the command level and
5338 call free_all_values. We can't call free_all_values
5339 because we might be in the middle of evaluating a
5340 function call. */
5341 struct value *mark = value_mark ();
5342
5343 if (is_watchpoint (b))
5344 w = (struct watchpoint *) b;
5345 else
5346 w = NULL;
5347
5348 /* Need to select the frame, with all that implies so that
5349 the conditions will have the right context. Because we
5350 use the frame, we will not see an inlined function's
5351 variables when we arrive at a breakpoint at the start
5352 of the inlined function; the current frame will be the
5353 call site. */
5354 if (w == NULL || w->cond_exp_valid_block == NULL)
5355 select_frame (get_current_frame ());
5356 else
18a18393 5357 {
7d4df6a4
DE
5358 struct frame_info *frame;
5359
5360 /* For local watchpoint expressions, which particular
5361 instance of a local is being watched matters, so we
5362 keep track of the frame to evaluate the expression
5363 in. To evaluate the condition however, it doesn't
5364 really matter which instantiation of the function
5365 where the condition makes sense triggers the
5366 watchpoint. This allows an expression like "watch
5367 global if q > 10" set in `func', catch writes to
5368 global on all threads that call `func', or catch
5369 writes on all recursive calls of `func' by a single
5370 thread. We simply always evaluate the condition in
5371 the innermost frame that's executing where it makes
5372 sense to evaluate the condition. It seems
5373 intuitive. */
5374 frame = block_innermost_frame (w->cond_exp_valid_block);
5375 if (frame != NULL)
5376 select_frame (frame);
5377 else
5378 within_current_scope = 0;
18a18393 5379 }
7d4df6a4 5380 if (within_current_scope)
bf469271 5381 {
a70b8144 5382 try
bf469271
PA
5383 {
5384 condition_result = breakpoint_cond_eval (cond);
5385 }
230d2906 5386 catch (const gdb_exception &ex)
bf469271
PA
5387 {
5388 exception_fprintf (gdb_stderr, ex,
5389 "Error in testing breakpoint condition:\n");
5390 }
bf469271 5391 }
7d4df6a4 5392 else
18a18393 5393 {
7d4df6a4
DE
5394 warning (_("Watchpoint condition cannot be tested "
5395 "in the current scope"));
5396 /* If we failed to set the right context for this
5397 watchpoint, unconditionally report it. */
18a18393 5398 }
7d4df6a4
DE
5399 /* FIXME-someday, should give breakpoint #. */
5400 value_free_to_mark (mark);
18a18393 5401 }
7d4df6a4 5402
bf469271 5403 if (cond && !condition_result)
7d4df6a4
DE
5404 {
5405 bs->stop = 0;
5406 }
7d4df6a4
DE
5407 else if (b->ignore_count > 0)
5408 {
5409 b->ignore_count--;
5410 bs->stop = 0;
5411 /* Increase the hit count even though we don't stop. */
5412 ++(b->hit_count);
76727919 5413 gdb::observers::breakpoint_modified.notify (b);
7d4df6a4 5414 }
18a18393
VP
5415}
5416
1cf4d951
PA
5417/* Returns true if we need to track moribund locations of LOC's type
5418 on the current target. */
5419
5420static int
5421need_moribund_for_location_type (struct bp_location *loc)
5422{
5423 return ((loc->loc_type == bp_loc_software_breakpoint
5424 && !target_supports_stopped_by_sw_breakpoint ())
5425 || (loc->loc_type == bp_loc_hardware_breakpoint
5426 && !target_supports_stopped_by_hw_breakpoint ()));
5427}
5428
ddfe970e 5429/* See breakpoint.h. */
c906108c
SS
5430
5431bpstat
ddfe970e 5432build_bpstat_chain (const address_space *aspace, CORE_ADDR bp_addr,
09ac7c10 5433 const struct target_waitstatus *ws)
c906108c 5434{
ddfe970e 5435 struct breakpoint *b;
5760d0ab 5436 bpstat bs_head = NULL, *bs_link = &bs_head;
c5aa993b 5437
429374b8
JK
5438 ALL_BREAKPOINTS (b)
5439 {
1a853c52 5440 if (!breakpoint_enabled (b))
429374b8 5441 continue;
a5606eee 5442
ddfe970e 5443 for (bp_location *bl = b->loc; bl != NULL; bl = bl->next)
429374b8 5444 {
4a64f543
MS
5445 /* For hardware watchpoints, we look only at the first
5446 location. The watchpoint_check function will work on the
5447 entire expression, not the individual locations. For
5448 read watchpoints, the watchpoints_triggered function has
5449 checked all locations already. */
429374b8
JK
5450 if (b->type == bp_hardware_watchpoint && bl != b->loc)
5451 break;
18a18393 5452
b5fa468f 5453 if (!bl->enabled || bl->disabled_by_cond || bl->shlib_disabled)
429374b8 5454 continue;
c5aa993b 5455
09ac7c10 5456 if (!bpstat_check_location (bl, aspace, bp_addr, ws))
429374b8 5457 continue;
c5aa993b 5458
4a64f543
MS
5459 /* Come here if it's a watchpoint, or if the break address
5460 matches. */
c5aa993b 5461
ddfe970e
KS
5462 bpstat bs = new bpstats (bl, &bs_link); /* Alloc a bpstat to
5463 explain stop. */
c5aa993b 5464
f431efe5
PA
5465 /* Assume we stop. Should we find a watchpoint that is not
5466 actually triggered, or if the condition of the breakpoint
5467 evaluates as false, we'll reset 'stop' to 0. */
429374b8
JK
5468 bs->stop = 1;
5469 bs->print = 1;
d983da9c 5470
f431efe5
PA
5471 /* If this is a scope breakpoint, mark the associated
5472 watchpoint as triggered so that we will handle the
5473 out-of-scope event. We'll get to the watchpoint next
5474 iteration. */
d0fb5eae 5475 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
3a5c3e22
PA
5476 {
5477 struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
5478
5479 w->watchpoint_triggered = watch_triggered_yes;
5480 }
f431efe5
PA
5481 }
5482 }
5483
7c16b83e 5484 /* Check if a moribund breakpoint explains the stop. */
1cf4d951
PA
5485 if (!target_supports_stopped_by_sw_breakpoint ()
5486 || !target_supports_stopped_by_hw_breakpoint ())
f431efe5 5487 {
1123588c 5488 for (bp_location *loc : moribund_locations)
f431efe5 5489 {
1cf4d951
PA
5490 if (breakpoint_location_address_match (loc, aspace, bp_addr)
5491 && need_moribund_for_location_type (loc))
5492 {
ddfe970e 5493 bpstat bs = new bpstats (loc, &bs_link);
1cf4d951
PA
5494 /* For hits of moribund locations, we should just proceed. */
5495 bs->stop = 0;
5496 bs->print = 0;
5497 bs->print_it = print_it_noop;
5498 }
f431efe5
PA
5499 }
5500 }
5501
ddfe970e
KS
5502 return bs_head;
5503}
5504
5505/* See breakpoint.h. */
5506
5507bpstat
5508bpstat_stop_status (const address_space *aspace,
00431a78 5509 CORE_ADDR bp_addr, thread_info *thread,
ddfe970e
KS
5510 const struct target_waitstatus *ws,
5511 bpstat stop_chain)
5512{
5513 struct breakpoint *b = NULL;
5514 /* First item of allocated bpstat's. */
5515 bpstat bs_head = stop_chain;
5516 bpstat bs;
5517 int need_remove_insert;
5518 int removed_any;
5519
5520 /* First, build the bpstat chain with locations that explain a
5521 target stop, while being careful to not set the target running,
5522 as that may invalidate locations (in particular watchpoint
5523 locations are recreated). Resuming will happen here with
5524 breakpoint conditions or watchpoint expressions that include
5525 inferior function calls. */
5526 if (bs_head == NULL)
5527 bs_head = build_bpstat_chain (aspace, bp_addr, ws);
5528
edcc5120
TT
5529 /* A bit of special processing for shlib breakpoints. We need to
5530 process solib loading here, so that the lists of loaded and
5531 unloaded libraries are correct before we handle "catch load" and
5532 "catch unload". */
5533 for (bs = bs_head; bs != NULL; bs = bs->next)
5534 {
5d268276 5535 if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
edcc5120
TT
5536 {
5537 handle_solib_event ();
5538 break;
5539 }
5540 }
5541
f431efe5
PA
5542 /* Now go through the locations that caused the target to stop, and
5543 check whether we're interested in reporting this stop to higher
5544 layers, or whether we should resume the target transparently. */
5545
5546 removed_any = 0;
5547
5760d0ab 5548 for (bs = bs_head; bs != NULL; bs = bs->next)
f431efe5
PA
5549 {
5550 if (!bs->stop)
5551 continue;
5552
f431efe5 5553 b = bs->breakpoint_at;
348d480f
PA
5554 b->ops->check_status (bs);
5555 if (bs->stop)
28010a5d 5556 {
00431a78 5557 bpstat_check_breakpoint_conditions (bs, thread);
f431efe5 5558
429374b8
JK
5559 if (bs->stop)
5560 {
5561 ++(b->hit_count);
76727919 5562 gdb::observers::breakpoint_modified.notify (b);
c906108c 5563
4a64f543 5564 /* We will stop here. */
429374b8
JK
5565 if (b->disposition == disp_disable)
5566 {
816338b5 5567 --(b->enable_count);
1a853c52 5568 if (b->enable_count <= 0)
429374b8 5569 b->enable_state = bp_disabled;
f431efe5 5570 removed_any = 1;
429374b8
JK
5571 }
5572 if (b->silent)
5573 bs->print = 0;
5574 bs->commands = b->commands;
abf85f46 5575 if (command_line_is_silent (bs->commands
d1b0a7bf 5576 ? bs->commands.get () : NULL))
abf85f46 5577 bs->print = 0;
9d6e6e84
HZ
5578
5579 b->ops->after_condition_true (bs);
429374b8
JK
5580 }
5581
348d480f 5582 }
a9b3a50f
PA
5583
5584 /* Print nothing for this entry if we don't stop or don't
5585 print. */
5586 if (!bs->stop || !bs->print)
5587 bs->print_it = print_it_noop;
429374b8 5588 }
876fa593 5589
d983da9c
DJ
5590 /* If we aren't stopping, the value of some hardware watchpoint may
5591 not have changed, but the intermediate memory locations we are
5592 watching may have. Don't bother if we're stopping; this will get
5593 done later. */
d832cb68 5594 need_remove_insert = 0;
5760d0ab
JK
5595 if (! bpstat_causes_stop (bs_head))
5596 for (bs = bs_head; bs != NULL; bs = bs->next)
d983da9c 5597 if (!bs->stop
f431efe5
PA
5598 && bs->breakpoint_at
5599 && is_hardware_watchpoint (bs->breakpoint_at))
d983da9c 5600 {
3a5c3e22
PA
5601 struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
5602
5603 update_watchpoint (w, 0 /* don't reparse. */);
d832cb68 5604 need_remove_insert = 1;
d983da9c
DJ
5605 }
5606
d832cb68 5607 if (need_remove_insert)
44702360 5608 update_global_location_list (UGLL_MAY_INSERT);
f431efe5 5609 else if (removed_any)
44702360 5610 update_global_location_list (UGLL_DONT_INSERT);
d832cb68 5611
5760d0ab 5612 return bs_head;
c906108c 5613}
628fe4e4
JK
5614
5615static void
61c26be8 5616handle_jit_event (CORE_ADDR address)
628fe4e4 5617{
628fe4e4
JK
5618 struct gdbarch *gdbarch;
5619
1eb8556f 5620 infrun_debug_printf ("handling bp_jit_event");
243a9253 5621
628fe4e4
JK
5622 /* Switch terminal for any messages produced by
5623 breakpoint_re_set. */
223ffa71 5624 target_terminal::ours_for_output ();
628fe4e4 5625
61c26be8
MS
5626 gdbarch = get_frame_arch (get_current_frame ());
5627 /* This event is caused by a breakpoint set in `jit_breakpoint_re_set`,
5628 thus it is expected that its objectfile can be found through
5629 minimal symbol lookup. If it doesn't work (and assert fails), it
5630 most likely means that `jit_breakpoint_re_set` was changes and this
5631 function needs to be updated too. */
5632 bound_minimal_symbol jit_bp_sym = lookup_minimal_symbol_by_pc (address);
5633 gdb_assert (jit_bp_sym.objfile != nullptr);
5634 jit_event_handler (gdbarch, jit_bp_sym.objfile);
628fe4e4 5635
223ffa71 5636 target_terminal::inferior ();
628fe4e4
JK
5637}
5638
5639/* Prepare WHAT final decision for infrun. */
5640
5641/* Decide what infrun needs to do with this bpstat. */
5642
c906108c 5643struct bpstat_what
0e30163f 5644bpstat_what (bpstat bs_head)
c906108c 5645{
c906108c 5646 struct bpstat_what retval;
0e30163f 5647 bpstat bs;
c906108c 5648
628fe4e4 5649 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
aa7d318d 5650 retval.call_dummy = STOP_NONE;
e2d0f980 5651 retval.is_longjmp = false;
628fe4e4 5652
0e30163f 5653 for (bs = bs_head; bs != NULL; bs = bs->next)
c906108c 5654 {
628fe4e4
JK
5655 /* Extract this BS's action. After processing each BS, we check
5656 if its action overrides all we've seem so far. */
5657 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
5658 enum bptype bptype;
5659
c906108c 5660 if (bs->breakpoint_at == NULL)
628fe4e4
JK
5661 {
5662 /* I suspect this can happen if it was a momentary
5663 breakpoint which has since been deleted. */
5664 bptype = bp_none;
5665 }
20874c92 5666 else
f431efe5 5667 bptype = bs->breakpoint_at->type;
628fe4e4
JK
5668
5669 switch (bptype)
c906108c
SS
5670 {
5671 case bp_none:
628fe4e4 5672 break;
c906108c
SS
5673 case bp_breakpoint:
5674 case bp_hardware_breakpoint:
7c16b83e 5675 case bp_single_step:
c906108c
SS
5676 case bp_until:
5677 case bp_finish:
a9b3a50f 5678 case bp_shlib_event:
c906108c
SS
5679 if (bs->stop)
5680 {
5681 if (bs->print)
628fe4e4 5682 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 5683 else
628fe4e4 5684 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
5685 }
5686 else
628fe4e4 5687 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5688 break;
5689 case bp_watchpoint:
5690 case bp_hardware_watchpoint:
5691 case bp_read_watchpoint:
5692 case bp_access_watchpoint:
5693 if (bs->stop)
5694 {
5695 if (bs->print)
628fe4e4 5696 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 5697 else
628fe4e4 5698 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
5699 }
5700 else
628fe4e4
JK
5701 {
5702 /* There was a watchpoint, but we're not stopping.
5703 This requires no further action. */
5704 }
c906108c
SS
5705 break;
5706 case bp_longjmp:
e2e4d78b 5707 case bp_longjmp_call_dummy:
186c406b 5708 case bp_exception:
0a39bb32
PA
5709 if (bs->stop)
5710 {
5711 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
5712 retval.is_longjmp = bptype != bp_exception;
5713 }
5714 else
5715 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5716 break;
5717 case bp_longjmp_resume:
186c406b 5718 case bp_exception_resume:
0a39bb32
PA
5719 if (bs->stop)
5720 {
5721 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
5722 retval.is_longjmp = bptype == bp_longjmp_resume;
5723 }
5724 else
5725 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5726 break;
5727 case bp_step_resume:
5728 if (bs->stop)
628fe4e4
JK
5729 this_action = BPSTAT_WHAT_STEP_RESUME;
5730 else
c906108c 5731 {
628fe4e4
JK
5732 /* It is for the wrong frame. */
5733 this_action = BPSTAT_WHAT_SINGLE;
c906108c 5734 }
c906108c 5735 break;
2c03e5be
PA
5736 case bp_hp_step_resume:
5737 if (bs->stop)
5738 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
5739 else
5740 {
5741 /* It is for the wrong frame. */
5742 this_action = BPSTAT_WHAT_SINGLE;
5743 }
5744 break;
c906108c 5745 case bp_watchpoint_scope:
c4093a6a 5746 case bp_thread_event:
1900040c 5747 case bp_overlay_event:
0fd8e87f 5748 case bp_longjmp_master:
aa7d318d 5749 case bp_std_terminate_master:
186c406b 5750 case bp_exception_master:
628fe4e4 5751 this_action = BPSTAT_WHAT_SINGLE;
c4093a6a 5752 break;
ce78b96d 5753 case bp_catchpoint:
c5aa993b
JM
5754 if (bs->stop)
5755 {
5756 if (bs->print)
628fe4e4 5757 this_action = BPSTAT_WHAT_STOP_NOISY;
c5aa993b 5758 else
628fe4e4 5759 this_action = BPSTAT_WHAT_STOP_SILENT;
c5aa993b
JM
5760 }
5761 else
628fe4e4 5762 {
cb1e4e32
PA
5763 /* Some catchpoints are implemented with breakpoints.
5764 For those, we need to step over the breakpoint. */
5765 if (bs->bp_location_at->loc_type != bp_loc_other)
5766 this_action = BPSTAT_WHAT_SINGLE;
628fe4e4
JK
5767 }
5768 break;
628fe4e4 5769 case bp_jit_event:
628fe4e4 5770 this_action = BPSTAT_WHAT_SINGLE;
c5aa993b 5771 break;
c906108c 5772 case bp_call_dummy:
53a5351d
JM
5773 /* Make sure the action is stop (silent or noisy),
5774 so infrun.c pops the dummy frame. */
aa7d318d 5775 retval.call_dummy = STOP_STACK_DUMMY;
628fe4e4 5776 this_action = BPSTAT_WHAT_STOP_SILENT;
aa7d318d
TT
5777 break;
5778 case bp_std_terminate:
5779 /* Make sure the action is stop (silent or noisy),
5780 so infrun.c pops the dummy frame. */
aa7d318d 5781 retval.call_dummy = STOP_STD_TERMINATE;
628fe4e4 5782 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c 5783 break;
1042e4c0 5784 case bp_tracepoint:
7a697b8d 5785 case bp_fast_tracepoint:
0fb4aa4b 5786 case bp_static_tracepoint:
1042e4c0
SS
5787 /* Tracepoint hits should not be reported back to GDB, and
5788 if one got through somehow, it should have been filtered
5789 out already. */
5790 internal_error (__FILE__, __LINE__,
7a697b8d 5791 _("bpstat_what: tracepoint encountered"));
0e30163f
JK
5792 break;
5793 case bp_gnu_ifunc_resolver:
5794 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
5795 this_action = BPSTAT_WHAT_SINGLE;
5796 break;
5797 case bp_gnu_ifunc_resolver_return:
5798 /* The breakpoint will be removed, execution will restart from the
5799 PC of the former breakpoint. */
5800 this_action = BPSTAT_WHAT_KEEP_CHECKING;
5801 break;
e7e0cddf
SS
5802
5803 case bp_dprintf:
a11cfd87
HZ
5804 if (bs->stop)
5805 this_action = BPSTAT_WHAT_STOP_SILENT;
5806 else
5807 this_action = BPSTAT_WHAT_SINGLE;
e7e0cddf
SS
5808 break;
5809
628fe4e4
JK
5810 default:
5811 internal_error (__FILE__, __LINE__,
5812 _("bpstat_what: unhandled bptype %d"), (int) bptype);
c906108c 5813 }
628fe4e4 5814
325fac50 5815 retval.main_action = std::max (retval.main_action, this_action);
c906108c 5816 }
628fe4e4 5817
243a9253
PA
5818 return retval;
5819}
628fe4e4 5820
243a9253
PA
5821void
5822bpstat_run_callbacks (bpstat bs_head)
5823{
5824 bpstat bs;
628fe4e4 5825
0e30163f
JK
5826 for (bs = bs_head; bs != NULL; bs = bs->next)
5827 {
5828 struct breakpoint *b = bs->breakpoint_at;
5829
5830 if (b == NULL)
5831 continue;
5832 switch (b->type)
5833 {
243a9253 5834 case bp_jit_event:
61c26be8 5835 handle_jit_event (bs->bp_location_at->address);
243a9253 5836 break;
0e30163f
JK
5837 case bp_gnu_ifunc_resolver:
5838 gnu_ifunc_resolver_stop (b);
5839 break;
5840 case bp_gnu_ifunc_resolver_return:
5841 gnu_ifunc_resolver_return_stop (b);
5842 break;
5843 }
5844 }
c906108c
SS
5845}
5846
4c462cb0 5847/* See breakpoint.h. */
c906108c 5848
4c462cb0
SM
5849bool
5850bpstat_should_step ()
c906108c
SS
5851{
5852 struct breakpoint *b;
cc59ec59 5853
c906108c 5854 ALL_BREAKPOINTS (b)
717a8278 5855 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
4c462cb0
SM
5856 return true;
5857 return false;
c906108c
SS
5858}
5859
4c462cb0
SM
5860/* See breakpoint.h. */
5861
5862bool
67822962
PA
5863bpstat_causes_stop (bpstat bs)
5864{
5865 for (; bs != NULL; bs = bs->next)
5866 if (bs->stop)
4c462cb0 5867 return true;
67822962 5868
4c462cb0 5869 return false;
67822962
PA
5870}
5871
c906108c 5872\f
c5aa993b 5873
170b53b2
UW
5874/* Compute a string of spaces suitable to indent the next line
5875 so it starts at the position corresponding to the table column
5876 named COL_NAME in the currently active table of UIOUT. */
5877
5878static char *
5879wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
5880{
5881 static char wrap_indent[80];
5882 int i, total_width, width, align;
c5209615 5883 const char *text;
170b53b2
UW
5884
5885 total_width = 0;
112e8700 5886 for (i = 1; uiout->query_table_field (i, &width, &align, &text); i++)
170b53b2
UW
5887 {
5888 if (strcmp (text, col_name) == 0)
5889 {
5890 gdb_assert (total_width < sizeof wrap_indent);
5891 memset (wrap_indent, ' ', total_width);
5892 wrap_indent[total_width] = 0;
5893
5894 return wrap_indent;
5895 }
5896
5897 total_width += width + 1;
5898 }
5899
5900 return NULL;
5901}
5902
b775012e
LM
5903/* Determine if the locations of this breakpoint will have their conditions
5904 evaluated by the target, host or a mix of both. Returns the following:
5905
5906 "host": Host evals condition.
5907 "host or target": Host or Target evals condition.
5908 "target": Target evals condition.
5909*/
5910
5911static const char *
5912bp_condition_evaluator (struct breakpoint *b)
5913{
5914 struct bp_location *bl;
5915 char host_evals = 0;
5916 char target_evals = 0;
5917
5918 if (!b)
5919 return NULL;
5920
5921 if (!is_breakpoint (b))
5922 return NULL;
5923
5924 if (gdb_evaluates_breakpoint_condition_p ()
5925 || !target_supports_evaluation_of_breakpoint_conditions ())
5926 return condition_evaluation_host;
5927
5928 for (bl = b->loc; bl; bl = bl->next)
5929 {
5930 if (bl->cond_bytecode)
5931 target_evals++;
5932 else
5933 host_evals++;
5934 }
5935
5936 if (host_evals && target_evals)
5937 return condition_evaluation_both;
5938 else if (target_evals)
5939 return condition_evaluation_target;
5940 else
5941 return condition_evaluation_host;
5942}
5943
5944/* Determine the breakpoint location's condition evaluator. This is
5945 similar to bp_condition_evaluator, but for locations. */
5946
5947static const char *
5948bp_location_condition_evaluator (struct bp_location *bl)
5949{
5950 if (bl && !is_breakpoint (bl->owner))
5951 return NULL;
5952
5953 if (gdb_evaluates_breakpoint_condition_p ()
5954 || !target_supports_evaluation_of_breakpoint_conditions ())
5955 return condition_evaluation_host;
5956
5957 if (bl && bl->cond_bytecode)
5958 return condition_evaluation_target;
5959 else
5960 return condition_evaluation_host;
5961}
5962
859825b8
JK
5963/* Print the LOC location out of the list of B->LOC locations. */
5964
170b53b2
UW
5965static void
5966print_breakpoint_location (struct breakpoint *b,
5967 struct bp_location *loc)
0d381245 5968{
79a45e25 5969 struct ui_out *uiout = current_uiout;
5ed8105e
PA
5970
5971 scoped_restore_current_program_space restore_pspace;
6c95b8df 5972
859825b8
JK
5973 if (loc != NULL && loc->shlib_disabled)
5974 loc = NULL;
5975
6c95b8df
PA
5976 if (loc != NULL)
5977 set_current_program_space (loc->pspace);
5978
56435ebe 5979 if (b->display_canonical)
d28cd78a 5980 uiout->field_string ("what", event_location_to_string (b->location.get ()));
2f202fde 5981 else if (loc && loc->symtab)
0d381245 5982 {
4a27f119
KS
5983 const struct symbol *sym = loc->symbol;
5984
0d381245
VP
5985 if (sym)
5986 {
112e8700 5987 uiout->text ("in ");
987012b8 5988 uiout->field_string ("func", sym->print_name (),
e43b10e1 5989 function_name_style.style ());
112e8700
SM
5990 uiout->text (" ");
5991 uiout->wrap_hint (wrap_indent_at_field (uiout, "what"));
5992 uiout->text ("at ");
0d381245 5993 }
112e8700 5994 uiout->field_string ("file",
cbe56571 5995 symtab_to_filename_for_display (loc->symtab),
e43b10e1 5996 file_name_style.style ());
112e8700 5997 uiout->text (":");
05cba821 5998
112e8700
SM
5999 if (uiout->is_mi_like_p ())
6000 uiout->field_string ("fullname", symtab_to_fullname (loc->symtab));
0d381245 6001
381befee 6002 uiout->field_signed ("line", loc->line_number);
0d381245 6003 }
859825b8 6004 else if (loc)
0d381245 6005 {
d7e74731 6006 string_file stb;
170b53b2 6007
d7e74731 6008 print_address_symbolic (loc->gdbarch, loc->address, &stb,
22e722e1 6009 demangle, "");
112e8700 6010 uiout->field_stream ("at", stb);
0d381245 6011 }
859825b8 6012 else
f00aae0f 6013 {
d28cd78a
TT
6014 uiout->field_string ("pending",
6015 event_location_to_string (b->location.get ()));
f00aae0f
KS
6016 /* If extra_string is available, it could be holding a condition
6017 or dprintf arguments. In either case, make sure it is printed,
6018 too, but only for non-MI streams. */
112e8700 6019 if (!uiout->is_mi_like_p () && b->extra_string != NULL)
f00aae0f
KS
6020 {
6021 if (b->type == bp_dprintf)
112e8700 6022 uiout->text (",");
f00aae0f 6023 else
112e8700
SM
6024 uiout->text (" ");
6025 uiout->text (b->extra_string);
f00aae0f
KS
6026 }
6027 }
6c95b8df 6028
b775012e
LM
6029 if (loc && is_breakpoint (b)
6030 && breakpoint_condition_evaluation_mode () == condition_evaluation_target
6031 && bp_condition_evaluator (b) == condition_evaluation_both)
6032 {
112e8700
SM
6033 uiout->text (" (");
6034 uiout->field_string ("evaluated-by",
b775012e 6035 bp_location_condition_evaluator (loc));
112e8700 6036 uiout->text (")");
b775012e 6037 }
0d381245
VP
6038}
6039
269b11a2
PA
6040static const char *
6041bptype_string (enum bptype type)
c906108c 6042{
c4093a6a
JM
6043 struct ep_type_description
6044 {
6045 enum bptype type;
a121b7c1 6046 const char *description;
c4093a6a
JM
6047 };
6048 static struct ep_type_description bptypes[] =
c906108c 6049 {
c5aa993b
JM
6050 {bp_none, "?deleted?"},
6051 {bp_breakpoint, "breakpoint"},
c906108c 6052 {bp_hardware_breakpoint, "hw breakpoint"},
7c16b83e 6053 {bp_single_step, "sw single-step"},
c5aa993b
JM
6054 {bp_until, "until"},
6055 {bp_finish, "finish"},
6056 {bp_watchpoint, "watchpoint"},
c906108c 6057 {bp_hardware_watchpoint, "hw watchpoint"},
c5aa993b
JM
6058 {bp_read_watchpoint, "read watchpoint"},
6059 {bp_access_watchpoint, "acc watchpoint"},
6060 {bp_longjmp, "longjmp"},
6061 {bp_longjmp_resume, "longjmp resume"},
e2e4d78b 6062 {bp_longjmp_call_dummy, "longjmp for call dummy"},
186c406b
TT
6063 {bp_exception, "exception"},
6064 {bp_exception_resume, "exception resume"},
c5aa993b 6065 {bp_step_resume, "step resume"},
2c03e5be 6066 {bp_hp_step_resume, "high-priority step resume"},
c5aa993b
JM
6067 {bp_watchpoint_scope, "watchpoint scope"},
6068 {bp_call_dummy, "call dummy"},
aa7d318d 6069 {bp_std_terminate, "std::terminate"},
c5aa993b 6070 {bp_shlib_event, "shlib events"},
c4093a6a 6071 {bp_thread_event, "thread events"},
1900040c 6072 {bp_overlay_event, "overlay events"},
0fd8e87f 6073 {bp_longjmp_master, "longjmp master"},
aa7d318d 6074 {bp_std_terminate_master, "std::terminate master"},
186c406b 6075 {bp_exception_master, "exception master"},
ce78b96d 6076 {bp_catchpoint, "catchpoint"},
1042e4c0 6077 {bp_tracepoint, "tracepoint"},
7a697b8d 6078 {bp_fast_tracepoint, "fast tracepoint"},
0fb4aa4b 6079 {bp_static_tracepoint, "static tracepoint"},
e7e0cddf 6080 {bp_dprintf, "dprintf"},
4efc6507 6081 {bp_jit_event, "jit events"},
0e30163f
JK
6082 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
6083 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
c5aa993b 6084 };
269b11a2
PA
6085
6086 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
6087 || ((int) type != bptypes[(int) type].type))
6088 internal_error (__FILE__, __LINE__,
6089 _("bptypes table does not describe type #%d."),
6090 (int) type);
6091
6092 return bptypes[(int) type].description;
6093}
6094
998580f1
MK
6095/* For MI, output a field named 'thread-groups' with a list as the value.
6096 For CLI, prefix the list with the string 'inf'. */
6097
6098static void
6099output_thread_groups (struct ui_out *uiout,
6100 const char *field_name,
5c632425 6101 const std::vector<int> &inf_nums,
998580f1
MK
6102 int mi_only)
6103{
112e8700 6104 int is_mi = uiout->is_mi_like_p ();
998580f1
MK
6105
6106 /* For backward compatibility, don't display inferiors in CLI unless
6107 there are several. Always display them for MI. */
6108 if (!is_mi && mi_only)
6109 return;
6110
10f489e5 6111 ui_out_emit_list list_emitter (uiout, field_name);
752eb8b4 6112
5c632425 6113 for (size_t i = 0; i < inf_nums.size (); i++)
998580f1
MK
6114 {
6115 if (is_mi)
6116 {
6117 char mi_group[10];
6118
5c632425 6119 xsnprintf (mi_group, sizeof (mi_group), "i%d", inf_nums[i]);
112e8700 6120 uiout->field_string (NULL, mi_group);
998580f1
MK
6121 }
6122 else
6123 {
6124 if (i == 0)
112e8700 6125 uiout->text (" inf ");
998580f1 6126 else
112e8700 6127 uiout->text (", ");
998580f1 6128
5c632425 6129 uiout->text (plongest (inf_nums[i]));
998580f1
MK
6130 }
6131 }
998580f1
MK
6132}
6133
a38118e5
PA
6134/* Print B to gdb_stdout. If RAW_LOC, print raw breakpoint locations
6135 instead of going via breakpoint_ops::print_one. This makes "maint
6136 info breakpoints" show the software breakpoint locations of
6137 catchpoints, which are considered internal implementation
6138 detail. */
269b11a2
PA
6139
6140static void
6141print_one_breakpoint_location (struct breakpoint *b,
6142 struct bp_location *loc,
6143 int loc_number,
6144 struct bp_location **last_loc,
a38118e5 6145 int allflag, bool raw_loc)
269b11a2
PA
6146{
6147 struct command_line *l;
c2c6d25f 6148 static char bpenables[] = "nynny";
c906108c 6149
79a45e25 6150 struct ui_out *uiout = current_uiout;
0d381245
VP
6151 int header_of_multiple = 0;
6152 int part_of_multiple = (loc != NULL);
79a45b7d
TT
6153 struct value_print_options opts;
6154
6155 get_user_print_options (&opts);
0d381245
VP
6156
6157 gdb_assert (!loc || loc_number != 0);
4a64f543
MS
6158 /* See comment in print_one_breakpoint concerning treatment of
6159 breakpoints with single disabled location. */
0d381245
VP
6160 if (loc == NULL
6161 && (b->loc != NULL
b5fa468f
TBA
6162 && (b->loc->next != NULL
6163 || !b->loc->enabled || b->loc->disabled_by_cond)))
0d381245
VP
6164 header_of_multiple = 1;
6165 if (loc == NULL)
6166 loc = b->loc;
6167
c4093a6a
JM
6168 annotate_record ();
6169
6170 /* 1 */
6171 annotate_field (0);
0d381245 6172 if (part_of_multiple)
528e1572 6173 uiout->field_fmt ("number", "%d.%d", b->number, loc_number);
0d381245 6174 else
381befee 6175 uiout->field_signed ("number", b->number);
c4093a6a
JM
6176
6177 /* 2 */
6178 annotate_field (1);
0d381245 6179 if (part_of_multiple)
112e8700 6180 uiout->field_skip ("type");
269b11a2 6181 else
112e8700 6182 uiout->field_string ("type", bptype_string (b->type));
c4093a6a
JM
6183
6184 /* 3 */
6185 annotate_field (2);
0d381245 6186 if (part_of_multiple)
112e8700 6187 uiout->field_skip ("disp");
0d381245 6188 else
112e8700 6189 uiout->field_string ("disp", bpdisp_text (b->disposition));
0d381245 6190
c4093a6a
JM
6191 /* 4 */
6192 annotate_field (3);
85c88e2a
TBA
6193 /* For locations that are disabled because of an invalid condition,
6194 display "N*" on CLI, where "*" refers to a footnote below the
6195 table. For MI, simply display a "N" without a footnote. */
6196 const char *N = (uiout->is_mi_like_p ()) ? "N" : "N*";
0d381245 6197 if (part_of_multiple)
85c88e2a 6198 uiout->field_string ("enabled", (loc->disabled_by_cond ? N
b5fa468f 6199 : (loc->enabled ? "y" : "n")));
0d381245 6200 else
112e8700 6201 uiout->field_fmt ("enabled", "%c", bpenables[(int) b->enable_state]);
0d381245 6202
c4093a6a 6203 /* 5 and 6 */
a38118e5 6204 if (!raw_loc && b->ops != NULL && b->ops->print_one != NULL)
b58a68fe 6205 b->ops->print_one (b, last_loc);
3086aeae 6206 else
a38118e5
PA
6207 {
6208 if (is_watchpoint (b))
3a5c3e22
PA
6209 {
6210 struct watchpoint *w = (struct watchpoint *) b;
6211
6212 /* Field 4, the address, is omitted (which makes the columns
6213 not line up too nicely with the headers, but the effect
6214 is relatively readable). */
6215 if (opts.addressprint)
112e8700 6216 uiout->field_skip ("addr");
3a5c3e22 6217 annotate_field (5);
112e8700 6218 uiout->field_string ("what", w->exp_string);
3a5c3e22 6219 }
f06f1252
TT
6220 else if (!is_catchpoint (b) || is_exception_catchpoint (b)
6221 || is_ada_exception_catchpoint (b))
a38118e5
PA
6222 {
6223 if (opts.addressprint)
6224 {
6225 annotate_field (4);
6226 if (header_of_multiple)
7f6aba03
TT
6227 uiout->field_string ("addr", "<MULTIPLE>",
6228 metadata_style.style ());
a38118e5 6229 else if (b->loc == NULL || loc->shlib_disabled)
7f6aba03
TT
6230 uiout->field_string ("addr", "<PENDING>",
6231 metadata_style.style ());
a38118e5
PA
6232 else
6233 uiout->field_core_addr ("addr",
6234 loc->gdbarch, loc->address);
6235 }
6236 annotate_field (5);
6237 if (!header_of_multiple)
6238 print_breakpoint_location (b, loc);
6239 if (b->loc)
6240 *last_loc = b->loc;
6241 }
6242 }
6c95b8df 6243
998580f1 6244 if (loc != NULL && !header_of_multiple)
6c95b8df 6245 {
5c632425 6246 std::vector<int> inf_nums;
998580f1 6247 int mi_only = 1;
6c95b8df 6248
08036331 6249 for (inferior *inf : all_inferiors ())
6c95b8df
PA
6250 {
6251 if (inf->pspace == loc->pspace)
5c632425 6252 inf_nums.push_back (inf->num);
6c95b8df 6253 }
998580f1 6254
dda83cd7 6255 /* For backward compatibility, don't display inferiors in CLI unless
998580f1
MK
6256 there are several. Always display for MI. */
6257 if (allflag
6258 || (!gdbarch_has_global_breakpoints (target_gdbarch ())
94c93c35 6259 && (program_spaces.size () > 1
998580f1
MK
6260 || number_of_inferiors () > 1)
6261 /* LOC is for existing B, it cannot be in
6262 moribund_locations and thus having NULL OWNER. */
6263 && loc->owner->type != bp_catchpoint))
6264 mi_only = 0;
5c632425 6265 output_thread_groups (uiout, "thread-groups", inf_nums, mi_only);
6c95b8df
PA
6266 }
6267
4a306c9a 6268 if (!part_of_multiple)
c4093a6a 6269 {
4a306c9a
JB
6270 if (b->thread != -1)
6271 {
6272 /* FIXME: This seems to be redundant and lost here; see the
4a64f543 6273 "stop only in" line a little further down. */
112e8700 6274 uiout->text (" thread ");
381befee 6275 uiout->field_signed ("thread", b->thread);
4a306c9a
JB
6276 }
6277 else if (b->task != 0)
6278 {
112e8700 6279 uiout->text (" task ");
381befee 6280 uiout->field_signed ("task", b->task);
4a306c9a 6281 }
c4093a6a 6282 }
f1310107 6283
112e8700 6284 uiout->text ("\n");
f1310107 6285
348d480f 6286 if (!part_of_multiple)
f1310107
TJB
6287 b->ops->print_one_detail (b, uiout);
6288
0d381245 6289 if (part_of_multiple && frame_id_p (b->frame_id))
c4093a6a
JM
6290 {
6291 annotate_field (6);
112e8700 6292 uiout->text ("\tstop only in stack frame at ");
e5dd4106 6293 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
dda83cd7 6294 the frame ID. */
112e8700 6295 uiout->field_core_addr ("frame",
5af949e3 6296 b->gdbarch, b->frame_id.stack_addr);
112e8700 6297 uiout->text ("\n");
c4093a6a
JM
6298 }
6299
28010a5d 6300 if (!part_of_multiple && b->cond_string)
c4093a6a
JM
6301 {
6302 annotate_field (7);
d77f58be 6303 if (is_tracepoint (b))
112e8700 6304 uiout->text ("\ttrace only if ");
1042e4c0 6305 else
112e8700
SM
6306 uiout->text ("\tstop only if ");
6307 uiout->field_string ("cond", b->cond_string);
b775012e
LM
6308
6309 /* Print whether the target is doing the breakpoint's condition
6310 evaluation. If GDB is doing the evaluation, don't print anything. */
6311 if (is_breakpoint (b)
6312 && breakpoint_condition_evaluation_mode ()
6313 == condition_evaluation_target)
6314 {
6a831f06
PA
6315 uiout->message (" (%pF evals)",
6316 string_field ("evaluated-by",
6317 bp_condition_evaluator (b)));
b775012e 6318 }
112e8700 6319 uiout->text ("\n");
0101ce28
JJ
6320 }
6321
0d381245 6322 if (!part_of_multiple && b->thread != -1)
c4093a6a 6323 {
4a64f543 6324 /* FIXME should make an annotation for this. */
112e8700
SM
6325 uiout->text ("\tstop only in thread ");
6326 if (uiout->is_mi_like_p ())
381befee 6327 uiout->field_signed ("thread", b->thread);
5d5658a1
PA
6328 else
6329 {
6330 struct thread_info *thr = find_thread_global_id (b->thread);
6331
112e8700 6332 uiout->field_string ("thread", print_thread_id (thr));
5d5658a1 6333 }
112e8700 6334 uiout->text ("\n");
c4093a6a
JM
6335 }
6336
556ec64d
YQ
6337 if (!part_of_multiple)
6338 {
6339 if (b->hit_count)
31f56a27
YQ
6340 {
6341 /* FIXME should make an annotation for this. */
6342 if (is_catchpoint (b))
112e8700 6343 uiout->text ("\tcatchpoint");
31f56a27 6344 else if (is_tracepoint (b))
112e8700 6345 uiout->text ("\ttracepoint");
31f56a27 6346 else
112e8700
SM
6347 uiout->text ("\tbreakpoint");
6348 uiout->text (" already hit ");
381befee 6349 uiout->field_signed ("times", b->hit_count);
31f56a27 6350 if (b->hit_count == 1)
112e8700 6351 uiout->text (" time\n");
31f56a27 6352 else
112e8700 6353 uiout->text (" times\n");
31f56a27 6354 }
556ec64d
YQ
6355 else
6356 {
31f56a27 6357 /* Output the count also if it is zero, but only if this is mi. */
112e8700 6358 if (uiout->is_mi_like_p ())
381befee 6359 uiout->field_signed ("times", b->hit_count);
556ec64d
YQ
6360 }
6361 }
8b93c638 6362
0d381245 6363 if (!part_of_multiple && b->ignore_count)
c4093a6a
JM
6364 {
6365 annotate_field (8);
6a831f06
PA
6366 uiout->message ("\tignore next %pF hits\n",
6367 signed_field ("ignore", b->ignore_count));
c4093a6a 6368 }
059fb39f 6369
816338b5
SS
6370 /* Note that an enable count of 1 corresponds to "enable once"
6371 behavior, which is reported by the combination of enablement and
6372 disposition, so we don't need to mention it here. */
6373 if (!part_of_multiple && b->enable_count > 1)
6374 {
6375 annotate_field (8);
112e8700 6376 uiout->text ("\tdisable after ");
816338b5
SS
6377 /* Tweak the wording to clarify that ignore and enable counts
6378 are distinct, and have additive effect. */
6379 if (b->ignore_count)
112e8700 6380 uiout->text ("additional ");
816338b5 6381 else
112e8700 6382 uiout->text ("next ");
381befee 6383 uiout->field_signed ("enable", b->enable_count);
112e8700 6384 uiout->text (" hits\n");
816338b5
SS
6385 }
6386
f196051f
SS
6387 if (!part_of_multiple && is_tracepoint (b))
6388 {
6389 struct tracepoint *tp = (struct tracepoint *) b;
6390
6391 if (tp->traceframe_usage)
6392 {
112e8700 6393 uiout->text ("\ttrace buffer usage ");
381befee 6394 uiout->field_signed ("traceframe-usage", tp->traceframe_usage);
112e8700 6395 uiout->text (" bytes\n");
f196051f
SS
6396 }
6397 }
d3ce09f5 6398
d1b0a7bf 6399 l = b->commands ? b->commands.get () : NULL;
059fb39f 6400 if (!part_of_multiple && l)
c4093a6a
JM
6401 {
6402 annotate_field (9);
2e783024 6403 ui_out_emit_tuple tuple_emitter (uiout, "script");
8b93c638 6404 print_command_lines (uiout, l, 4);
c4093a6a 6405 }
d24317b4 6406
d9b3f62e 6407 if (is_tracepoint (b))
1042e4c0 6408 {
d9b3f62e
PA
6409 struct tracepoint *t = (struct tracepoint *) b;
6410
6411 if (!part_of_multiple && t->pass_count)
6412 {
6413 annotate_field (10);
112e8700 6414 uiout->text ("\tpass count ");
381befee 6415 uiout->field_signed ("pass", t->pass_count);
112e8700 6416 uiout->text (" \n");
d9b3f62e 6417 }
f2a8bc8a
YQ
6418
6419 /* Don't display it when tracepoint or tracepoint location is
6420 pending. */
6421 if (!header_of_multiple && loc != NULL && !loc->shlib_disabled)
6422 {
6423 annotate_field (11);
6424
112e8700
SM
6425 if (uiout->is_mi_like_p ())
6426 uiout->field_string ("installed",
f2a8bc8a
YQ
6427 loc->inserted ? "y" : "n");
6428 else
6429 {
6430 if (loc->inserted)
112e8700 6431 uiout->text ("\t");
f2a8bc8a 6432 else
112e8700
SM
6433 uiout->text ("\tnot ");
6434 uiout->text ("installed on target\n");
f2a8bc8a
YQ
6435 }
6436 }
1042e4c0
SS
6437 }
6438
112e8700 6439 if (uiout->is_mi_like_p () && !part_of_multiple)
d24317b4 6440 {
3a5c3e22
PA
6441 if (is_watchpoint (b))
6442 {
6443 struct watchpoint *w = (struct watchpoint *) b;
6444
112e8700 6445 uiout->field_string ("original-location", w->exp_string);
3a5c3e22 6446 }
f00aae0f 6447 else if (b->location != NULL
d28cd78a 6448 && event_location_to_string (b->location.get ()) != NULL)
112e8700 6449 uiout->field_string ("original-location",
d28cd78a 6450 event_location_to_string (b->location.get ()));
d24317b4 6451 }
c4093a6a 6452}
c5aa993b 6453
13674803
SM
6454/* See breakpoint.h. */
6455
6456bool fix_multi_location_breakpoint_output_globally = false;
6457
0d381245
VP
6458static void
6459print_one_breakpoint (struct breakpoint *b,
4a64f543 6460 struct bp_location **last_loc,
6c95b8df 6461 int allflag)
0d381245 6462{
79a45e25 6463 struct ui_out *uiout = current_uiout;
13674803
SM
6464 bool use_fixed_output
6465 = (uiout->test_flags (fix_multi_location_breakpoint_output)
6466 || fix_multi_location_breakpoint_output_globally);
8d3788bd 6467
b4be1b06 6468 gdb::optional<ui_out_emit_tuple> bkpt_tuple_emitter (gdb::in_place, uiout, "bkpt");
a38118e5 6469 print_one_breakpoint_location (b, NULL, 0, last_loc, allflag, false);
8d3788bd 6470
b4be1b06
SM
6471 /* The mi2 broken format: the main breakpoint tuple ends here, the locations
6472 are outside. */
6473 if (!use_fixed_output)
6474 bkpt_tuple_emitter.reset ();
0d381245
VP
6475
6476 /* If this breakpoint has custom print function,
6477 it's already printed. Otherwise, print individual
6478 locations, if any. */
a38118e5
PA
6479 if (b->ops == NULL
6480 || b->ops->print_one == NULL
6481 || allflag)
0d381245 6482 {
4a64f543
MS
6483 /* If breakpoint has a single location that is disabled, we
6484 print it as if it had several locations, since otherwise it's
6485 hard to represent "breakpoint enabled, location disabled"
6486 situation.
6487
6488 Note that while hardware watchpoints have several locations
a38118e5
PA
6489 internally, that's not a property exposed to users.
6490
6491 Likewise, while catchpoints may be implemented with
6492 breakpoints (e.g., catch throw), that's not a property
6493 exposed to users. We do however display the internal
6494 breakpoint locations with "maint info breakpoints". */
6495 if (!is_hardware_watchpoint (b)
f06f1252
TT
6496 && (!is_catchpoint (b) || is_exception_catchpoint (b)
6497 || is_ada_exception_catchpoint (b))
a38118e5 6498 && (allflag
b5fa468f
TBA
6499 || (b->loc && (b->loc->next
6500 || !b->loc->enabled
6501 || b->loc->disabled_by_cond))))
0d381245 6502 {
b4be1b06
SM
6503 gdb::optional<ui_out_emit_list> locations_list;
6504
6505 /* For MI version <= 2, keep the behavior where GDB outputs an invalid
6506 MI record. For later versions, place breakpoint locations in a
6507 list. */
6508 if (uiout->is_mi_like_p () && use_fixed_output)
6509 locations_list.emplace (uiout, "locations");
8d3788bd 6510
b4be1b06
SM
6511 int n = 1;
6512 for (bp_location *loc = b->loc; loc != NULL; loc = loc->next, ++n)
8d3788bd 6513 {
b4be1b06 6514 ui_out_emit_tuple loc_tuple_emitter (uiout, NULL);
a38118e5
PA
6515 print_one_breakpoint_location (b, loc, n, last_loc,
6516 allflag, allflag);
8d3788bd 6517 }
0d381245
VP
6518 }
6519 }
6520}
6521
a6d9a66e
UW
6522static int
6523breakpoint_address_bits (struct breakpoint *b)
6524{
6525 int print_address_bits = 0;
6526 struct bp_location *loc;
6527
c6d81124
PA
6528 /* Software watchpoints that aren't watching memory don't have an
6529 address to print. */
6530 if (is_no_memory_software_watchpoint (b))
6531 return 0;
6532
a6d9a66e
UW
6533 for (loc = b->loc; loc; loc = loc->next)
6534 {
c7437ca6
PA
6535 int addr_bit;
6536
c7437ca6 6537 addr_bit = gdbarch_addr_bit (loc->gdbarch);
a6d9a66e
UW
6538 if (addr_bit > print_address_bits)
6539 print_address_bits = addr_bit;
6540 }
6541
6542 return print_address_bits;
6543}
0d381245 6544
65630365 6545/* See breakpoint.h. */
c5aa993b 6546
65630365
PA
6547void
6548print_breakpoint (breakpoint *b)
c4093a6a 6549{
a6d9a66e 6550 struct bp_location *dummy_loc = NULL;
65630365 6551 print_one_breakpoint (b, &dummy_loc, 0);
c4093a6a 6552}
c5aa993b 6553
09d682a4
TT
6554/* Return true if this breakpoint was set by the user, false if it is
6555 internal or momentary. */
6556
6557int
6558user_breakpoint_p (struct breakpoint *b)
6559{
46c6471b 6560 return b->number > 0;
09d682a4
TT
6561}
6562
93daf339
TT
6563/* See breakpoint.h. */
6564
6565int
6566pending_breakpoint_p (struct breakpoint *b)
6567{
6568 return b->loc == NULL;
6569}
6570
5c458ae8
SM
6571/* Print information on breakpoints (including watchpoints and tracepoints).
6572
6573 If non-NULL, BP_NUM_LIST is a list of numbers and number ranges as
6574 understood by number_or_range_parser. Only breakpoints included in this
6575 list are then printed.
6576
6577 If SHOW_INTERNAL is true, print internal breakpoints.
6578
6579 If FILTER is non-NULL, call it on each breakpoint and only include the
6580 ones for which it returns true.
6581
6582 Return the total number of breakpoints listed. */
c906108c 6583
d77f58be 6584static int
5c458ae8 6585breakpoint_1 (const char *bp_num_list, bool show_internal,
f2478a7e 6586 bool (*filter) (const struct breakpoint *))
c4093a6a 6587{
52f0bd74 6588 struct breakpoint *b;
a6d9a66e 6589 struct bp_location *last_loc = NULL;
7f3b0473 6590 int nr_printable_breakpoints;
79a45b7d 6591 struct value_print_options opts;
a6d9a66e 6592 int print_address_bits = 0;
269b11a2 6593 int print_type_col_width = 14;
79a45e25 6594 struct ui_out *uiout = current_uiout;
b5fa468f 6595 bool has_disabled_by_cond_location = false;
269b11a2 6596
79a45b7d
TT
6597 get_user_print_options (&opts);
6598
4a64f543
MS
6599 /* Compute the number of rows in the table, as well as the size
6600 required for address fields. */
7f3b0473
AC
6601 nr_printable_breakpoints = 0;
6602 ALL_BREAKPOINTS (b)
e5a67952
MS
6603 {
6604 /* If we have a filter, only list the breakpoints it accepts. */
6605 if (filter && !filter (b))
6606 continue;
6607
5c458ae8 6608 /* If we have a BP_NUM_LIST string, it is a list of breakpoints to
e5a67952 6609 accept. Skip the others. */
5c458ae8 6610 if (bp_num_list != NULL && *bp_num_list != '\0')
e5a67952 6611 {
5c458ae8 6612 if (show_internal && parse_and_eval_long (bp_num_list) != b->number)
e5a67952 6613 continue;
5c458ae8 6614 if (!show_internal && !number_is_in_list (bp_num_list, b->number))
e5a67952
MS
6615 continue;
6616 }
269b11a2 6617
5c458ae8 6618 if (show_internal || user_breakpoint_p (b))
e5a67952
MS
6619 {
6620 int addr_bit, type_len;
a6d9a66e 6621
e5a67952
MS
6622 addr_bit = breakpoint_address_bits (b);
6623 if (addr_bit > print_address_bits)
6624 print_address_bits = addr_bit;
269b11a2 6625
e5a67952
MS
6626 type_len = strlen (bptype_string (b->type));
6627 if (type_len > print_type_col_width)
6628 print_type_col_width = type_len;
6629
6630 nr_printable_breakpoints++;
6631 }
6632 }
7f3b0473 6633
4a2b031d
TT
6634 {
6635 ui_out_emit_table table_emitter (uiout,
6636 opts.addressprint ? 6 : 5,
6637 nr_printable_breakpoints,
6638 "BreakpointTable");
6639
6640 if (nr_printable_breakpoints > 0)
6641 annotate_breakpoints_headers ();
6642 if (nr_printable_breakpoints > 0)
6643 annotate_field (0);
6644 uiout->table_header (7, ui_left, "number", "Num"); /* 1 */
6645 if (nr_printable_breakpoints > 0)
6646 annotate_field (1);
6647 uiout->table_header (print_type_col_width, ui_left, "type", "Type"); /* 2 */
6648 if (nr_printable_breakpoints > 0)
6649 annotate_field (2);
6650 uiout->table_header (4, ui_left, "disp", "Disp"); /* 3 */
6651 if (nr_printable_breakpoints > 0)
6652 annotate_field (3);
6653 uiout->table_header (3, ui_left, "enabled", "Enb"); /* 4 */
6654 if (opts.addressprint)
6655 {
6656 if (nr_printable_breakpoints > 0)
6657 annotate_field (4);
6658 if (print_address_bits <= 32)
6659 uiout->table_header (10, ui_left, "addr", "Address"); /* 5 */
6660 else
6661 uiout->table_header (18, ui_left, "addr", "Address"); /* 5 */
6662 }
6663 if (nr_printable_breakpoints > 0)
6664 annotate_field (5);
6665 uiout->table_header (40, ui_noalign, "what", "What"); /* 6 */
6666 uiout->table_body ();
6667 if (nr_printable_breakpoints > 0)
6668 annotate_breakpoints_table ();
6669
6670 ALL_BREAKPOINTS (b)
6671 {
6672 QUIT;
6673 /* If we have a filter, only list the breakpoints it accepts. */
6674 if (filter && !filter (b))
6675 continue;
e5a67952 6676
5c458ae8 6677 /* If we have a BP_NUM_LIST string, it is a list of breakpoints to
4a2b031d 6678 accept. Skip the others. */
e5a67952 6679
5c458ae8 6680 if (bp_num_list != NULL && *bp_num_list != '\0')
4a2b031d 6681 {
5c458ae8 6682 if (show_internal) /* maintenance info breakpoint */
4a2b031d 6683 {
5c458ae8 6684 if (parse_and_eval_long (bp_num_list) != b->number)
4a2b031d
TT
6685 continue;
6686 }
6687 else /* all others */
6688 {
5c458ae8 6689 if (!number_is_in_list (bp_num_list, b->number))
4a2b031d
TT
6690 continue;
6691 }
6692 }
6693 /* We only print out user settable breakpoints unless the
5c458ae8
SM
6694 show_internal is set. */
6695 if (show_internal || user_breakpoint_p (b))
b5fa468f
TBA
6696 {
6697 print_one_breakpoint (b, &last_loc, show_internal);
6698 for (bp_location *loc = b->loc; loc != NULL; loc = loc->next)
6699 if (loc->disabled_by_cond)
6700 has_disabled_by_cond_location = true;
6701 }
4a2b031d
TT
6702 }
6703 }
698384cd 6704
7f3b0473 6705 if (nr_printable_breakpoints == 0)
c906108c 6706 {
4a64f543
MS
6707 /* If there's a filter, let the caller decide how to report
6708 empty list. */
d77f58be
SS
6709 if (!filter)
6710 {
5c458ae8 6711 if (bp_num_list == NULL || *bp_num_list == '\0')
112e8700 6712 uiout->message ("No breakpoints or watchpoints.\n");
d77f58be 6713 else
112e8700 6714 uiout->message ("No breakpoint or watchpoint matching '%s'.\n",
5c458ae8 6715 bp_num_list);
d77f58be 6716 }
c906108c
SS
6717 }
6718 else
c4093a6a 6719 {
a6d9a66e
UW
6720 if (last_loc && !server_command)
6721 set_next_address (last_loc->gdbarch, last_loc->address);
b5fa468f 6722
85c88e2a 6723 if (has_disabled_by_cond_location && !uiout->is_mi_like_p ())
b5fa468f
TBA
6724 uiout->message (_("(*): Breakpoint condition is invalid at this "
6725 "location.\n"));
c4093a6a 6726 }
c906108c 6727
4a64f543 6728 /* FIXME? Should this be moved up so that it is only called when
c4093a6a 6729 there have been breakpoints? */
c906108c 6730 annotate_breakpoints_table_end ();
d77f58be
SS
6731
6732 return nr_printable_breakpoints;
c906108c
SS
6733}
6734
ad443146
SS
6735/* Display the value of default-collect in a way that is generally
6736 compatible with the breakpoint list. */
6737
6738static void
6739default_collect_info (void)
6740{
79a45e25
PA
6741 struct ui_out *uiout = current_uiout;
6742
ad443146
SS
6743 /* If it has no value (which is frequently the case), say nothing; a
6744 message like "No default-collect." gets in user's face when it's
6745 not wanted. */
6746 if (!*default_collect)
6747 return;
6748
6749 /* The following phrase lines up nicely with per-tracepoint collect
6750 actions. */
112e8700
SM
6751 uiout->text ("default collect ");
6752 uiout->field_string ("default-collect", default_collect);
6753 uiout->text (" \n");
ad443146
SS
6754}
6755
c906108c 6756static void
0b39b52e 6757info_breakpoints_command (const char *args, int from_tty)
c906108c 6758{
5c458ae8 6759 breakpoint_1 (args, false, NULL);
ad443146
SS
6760
6761 default_collect_info ();
d77f58be
SS
6762}
6763
6764static void
1d12d88f 6765info_watchpoints_command (const char *args, int from_tty)
d77f58be 6766{
5c458ae8 6767 int num_printed = breakpoint_1 (args, false, is_watchpoint);
79a45e25 6768 struct ui_out *uiout = current_uiout;
d77f58be
SS
6769
6770 if (num_printed == 0)
6771 {
e5a67952 6772 if (args == NULL || *args == '\0')
112e8700 6773 uiout->message ("No watchpoints.\n");
d77f58be 6774 else
112e8700 6775 uiout->message ("No watchpoint matching '%s'.\n", args);
d77f58be 6776 }
c906108c
SS
6777}
6778
7a292a7a 6779static void
4495129a 6780maintenance_info_breakpoints (const char *args, int from_tty)
c906108c 6781{
5c458ae8 6782 breakpoint_1 (args, true, NULL);
ad443146
SS
6783
6784 default_collect_info ();
c906108c
SS
6785}
6786
0d381245 6787static int
714835d5 6788breakpoint_has_pc (struct breakpoint *b,
6c95b8df 6789 struct program_space *pspace,
714835d5 6790 CORE_ADDR pc, struct obj_section *section)
0d381245
VP
6791{
6792 struct bp_location *bl = b->loc;
cc59ec59 6793
0d381245
VP
6794 for (; bl; bl = bl->next)
6795 {
6c95b8df
PA
6796 if (bl->pspace == pspace
6797 && bl->address == pc
0d381245
VP
6798 && (!overlay_debugging || bl->section == section))
6799 return 1;
6800 }
6801 return 0;
6802}
6803
672f9b60 6804/* Print a message describing any user-breakpoints set at PC. This
6c95b8df
PA
6805 concerns with logical breakpoints, so we match program spaces, not
6806 address spaces. */
c906108c
SS
6807
6808static void
6c95b8df
PA
6809describe_other_breakpoints (struct gdbarch *gdbarch,
6810 struct program_space *pspace, CORE_ADDR pc,
5af949e3 6811 struct obj_section *section, int thread)
c906108c 6812{
52f0bd74
AC
6813 int others = 0;
6814 struct breakpoint *b;
c906108c
SS
6815
6816 ALL_BREAKPOINTS (b)
672f9b60 6817 others += (user_breakpoint_p (b)
dda83cd7 6818 && breakpoint_has_pc (b, pspace, pc, section));
c906108c
SS
6819 if (others > 0)
6820 {
a3f17187
AC
6821 if (others == 1)
6822 printf_filtered (_("Note: breakpoint "));
6823 else /* if (others == ???) */
6824 printf_filtered (_("Note: breakpoints "));
c906108c 6825 ALL_BREAKPOINTS (b)
672f9b60 6826 if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
0d381245
VP
6827 {
6828 others--;
6829 printf_filtered ("%d", b->number);
6830 if (b->thread == -1 && thread != -1)
6831 printf_filtered (" (all threads)");
6832 else if (b->thread != -1)
6833 printf_filtered (" (thread %d)", b->thread);
6834 printf_filtered ("%s%s ",
059fb39f 6835 ((b->enable_state == bp_disabled
f8eba3c6 6836 || b->enable_state == bp_call_disabled)
0d381245 6837 ? " (disabled)"
0d381245
VP
6838 : ""),
6839 (others > 1) ? ","
6840 : ((others == 1) ? " and" : ""));
6841 }
6a831f06
PA
6842 current_uiout->message (_("also set at pc %ps.\n"),
6843 styled_string (address_style.style (),
6844 paddress (gdbarch, pc)));
c906108c
SS
6845 }
6846}
6847\f
c906108c 6848
cb1e4e32
PA
6849/* Return true iff it is meaningful to use the address member of LOC.
6850 For some breakpoint types, the locations' address members are
6851 irrelevant and it makes no sense to attempt to compare them to
6852 other addresses (or use them for any other purpose either).
2d134ed3 6853
cb1e4e32
PA
6854 More specifically, software watchpoints and catchpoints that are
6855 not backed by breakpoints always have a zero valued location
6856 address and we don't want to mark breakpoints of any of these types
6857 to be a duplicate of an actual breakpoint location at address
6858 zero. */
e4f237da 6859
cb1e4e32
PA
6860static bool
6861bl_address_is_meaningful (bp_location *loc)
e4f237da 6862{
cb1e4e32 6863 return loc->loc_type != bp_loc_other;
2d134ed3
PA
6864}
6865
6866/* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
6867 true if LOC1 and LOC2 represent the same watchpoint location. */
6868
6869static int
4a64f543
MS
6870watchpoint_locations_match (struct bp_location *loc1,
6871 struct bp_location *loc2)
2d134ed3 6872{
3a5c3e22
PA
6873 struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
6874 struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
6875
6876 /* Both of them must exist. */
6877 gdb_assert (w1 != NULL);
6878 gdb_assert (w2 != NULL);
2bdf28a0 6879
4a64f543
MS
6880 /* If the target can evaluate the condition expression in hardware,
6881 then we we need to insert both watchpoints even if they are at
6882 the same place. Otherwise the watchpoint will only trigger when
6883 the condition of whichever watchpoint was inserted evaluates to
6884 true, not giving a chance for GDB to check the condition of the
6885 other watchpoint. */
3a5c3e22 6886 if ((w1->cond_exp
4a64f543
MS
6887 && target_can_accel_watchpoint_condition (loc1->address,
6888 loc1->length,
0cf6dd15 6889 loc1->watchpoint_type,
4d01a485 6890 w1->cond_exp.get ()))
3a5c3e22 6891 || (w2->cond_exp
4a64f543
MS
6892 && target_can_accel_watchpoint_condition (loc2->address,
6893 loc2->length,
0cf6dd15 6894 loc2->watchpoint_type,
4d01a485 6895 w2->cond_exp.get ())))
0cf6dd15
TJB
6896 return 0;
6897
85d721b8
PA
6898 /* Note that this checks the owner's type, not the location's. In
6899 case the target does not support read watchpoints, but does
6900 support access watchpoints, we'll have bp_read_watchpoint
6901 watchpoints with hw_access locations. Those should be considered
6902 duplicates of hw_read locations. The hw_read locations will
6903 become hw_access locations later. */
2d134ed3
PA
6904 return (loc1->owner->type == loc2->owner->type
6905 && loc1->pspace->aspace == loc2->pspace->aspace
6906 && loc1->address == loc2->address
6907 && loc1->length == loc2->length);
e4f237da
KB
6908}
6909
31e77af2 6910/* See breakpoint.h. */
6c95b8df 6911
31e77af2 6912int
accd0bcd
YQ
6913breakpoint_address_match (const address_space *aspace1, CORE_ADDR addr1,
6914 const address_space *aspace2, CORE_ADDR addr2)
6c95b8df 6915{
f5656ead 6916 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
6c95b8df
PA
6917 || aspace1 == aspace2)
6918 && addr1 == addr2);
6919}
6920
f1310107
TJB
6921/* Returns true if {ASPACE2,ADDR2} falls within the range determined by
6922 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
6923 matches ASPACE2. On targets that have global breakpoints, the address
6924 space doesn't really matter. */
6925
6926static int
accd0bcd
YQ
6927breakpoint_address_match_range (const address_space *aspace1,
6928 CORE_ADDR addr1,
6929 int len1, const address_space *aspace2,
f1310107
TJB
6930 CORE_ADDR addr2)
6931{
f5656ead 6932 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
f1310107
TJB
6933 || aspace1 == aspace2)
6934 && addr2 >= addr1 && addr2 < addr1 + len1);
6935}
6936
6937/* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
6938 a ranged breakpoint. In most targets, a match happens only if ASPACE
6939 matches the breakpoint's address space. On targets that have global
6940 breakpoints, the address space doesn't really matter. */
6941
6942static int
6943breakpoint_location_address_match (struct bp_location *bl,
accd0bcd 6944 const address_space *aspace,
f1310107
TJB
6945 CORE_ADDR addr)
6946{
6947 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
6948 aspace, addr)
6949 || (bl->length
6950 && breakpoint_address_match_range (bl->pspace->aspace,
6951 bl->address, bl->length,
6952 aspace, addr)));
6953}
6954
d35ae833
PA
6955/* Returns true if the [ADDR,ADDR+LEN) range in ASPACE overlaps
6956 breakpoint BL. BL may be a ranged breakpoint. In most targets, a
6957 match happens only if ASPACE matches the breakpoint's address
6958 space. On targets that have global breakpoints, the address space
6959 doesn't really matter. */
6960
6961static int
6962breakpoint_location_address_range_overlap (struct bp_location *bl,
accd0bcd 6963 const address_space *aspace,
d35ae833
PA
6964 CORE_ADDR addr, int len)
6965{
6966 if (gdbarch_has_global_breakpoints (target_gdbarch ())
6967 || bl->pspace->aspace == aspace)
6968 {
6969 int bl_len = bl->length != 0 ? bl->length : 1;
6970
6971 if (mem_ranges_overlap (addr, len, bl->address, bl_len))
6972 return 1;
6973 }
6974 return 0;
6975}
6976
1e4d1764
YQ
6977/* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
6978 Then, if LOC1 and LOC2 represent the same tracepoint location, returns
6979 true, otherwise returns false. */
6980
6981static int
6982tracepoint_locations_match (struct bp_location *loc1,
6983 struct bp_location *loc2)
6984{
6985 if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
6986 /* Since tracepoint locations are never duplicated with others', tracepoint
6987 locations at the same address of different tracepoints are regarded as
6988 different locations. */
6989 return (loc1->address == loc2->address && loc1->owner == loc2->owner);
6990 else
6991 return 0;
6992}
6993
2d134ed3 6994/* Assuming LOC1 and LOC2's types' have meaningful target addresses
cb1e4e32 6995 (bl_address_is_meaningful), returns true if LOC1 and LOC2 represent
7f32a4d5
PA
6996 the same location. If SW_HW_BPS_MATCH is true, then software
6997 breakpoint locations and hardware breakpoint locations match,
6998 otherwise they don't. */
2d134ed3
PA
6999
7000static int
7f32a4d5
PA
7001breakpoint_locations_match (struct bp_location *loc1,
7002 struct bp_location *loc2,
7003 bool sw_hw_bps_match)
2d134ed3 7004{
2bdf28a0
JK
7005 int hw_point1, hw_point2;
7006
7007 /* Both of them must not be in moribund_locations. */
7008 gdb_assert (loc1->owner != NULL);
7009 gdb_assert (loc2->owner != NULL);
7010
7011 hw_point1 = is_hardware_watchpoint (loc1->owner);
7012 hw_point2 = is_hardware_watchpoint (loc2->owner);
2d134ed3
PA
7013
7014 if (hw_point1 != hw_point2)
7015 return 0;
7016 else if (hw_point1)
7017 return watchpoint_locations_match (loc1, loc2);
1e4d1764
YQ
7018 else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
7019 return tracepoint_locations_match (loc1, loc2);
2d134ed3 7020 else
7f32a4d5
PA
7021 /* We compare bp_location.length in order to cover ranged
7022 breakpoints. Keep this in sync with
7023 bp_location_is_less_than. */
f1310107
TJB
7024 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
7025 loc2->pspace->aspace, loc2->address)
7f32a4d5 7026 && (loc1->loc_type == loc2->loc_type || sw_hw_bps_match)
f1310107 7027 && loc1->length == loc2->length);
2d134ed3
PA
7028}
7029
76897487
KB
7030static void
7031breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
dda83cd7 7032 int bnum, int have_bnum)
76897487 7033{
f63fbe86
MS
7034 /* The longest string possibly returned by hex_string_custom
7035 is 50 chars. These must be at least that big for safety. */
7036 char astr1[64];
7037 char astr2[64];
76897487 7038
bb599908
PH
7039 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
7040 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
76897487 7041 if (have_bnum)
8a3fe4f8 7042 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
dda83cd7 7043 bnum, astr1, astr2);
76897487 7044 else
8a3fe4f8 7045 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
76897487
KB
7046}
7047
4a64f543
MS
7048/* Adjust a breakpoint's address to account for architectural
7049 constraints on breakpoint placement. Return the adjusted address.
7050 Note: Very few targets require this kind of adjustment. For most
7051 targets, this function is simply the identity function. */
76897487
KB
7052
7053static CORE_ADDR
a6d9a66e
UW
7054adjust_breakpoint_address (struct gdbarch *gdbarch,
7055 CORE_ADDR bpaddr, enum bptype bptype)
76897487 7056{
a0de8c21
YQ
7057 if (bptype == bp_watchpoint
7058 || bptype == bp_hardware_watchpoint
7059 || bptype == bp_read_watchpoint
7060 || bptype == bp_access_watchpoint
7061 || bptype == bp_catchpoint)
88f7da05
KB
7062 {
7063 /* Watchpoints and the various bp_catch_* eventpoints should not
dda83cd7 7064 have their addresses modified. */
88f7da05
KB
7065 return bpaddr;
7066 }
7c16b83e
PA
7067 else if (bptype == bp_single_step)
7068 {
7069 /* Single-step breakpoints should not have their addresses
7070 modified. If there's any architectural constrain that
7071 applies to this address, then it should have already been
7072 taken into account when the breakpoint was created in the
7073 first place. If we didn't do this, stepping through e.g.,
7074 Thumb-2 IT blocks would break. */
7075 return bpaddr;
7076 }
76897487
KB
7077 else
7078 {
a0de8c21
YQ
7079 CORE_ADDR adjusted_bpaddr = bpaddr;
7080
7081 if (gdbarch_adjust_breakpoint_address_p (gdbarch))
7082 {
7083 /* Some targets have architectural constraints on the placement
7084 of breakpoint instructions. Obtain the adjusted address. */
7085 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
7086 }
76897487 7087
a0de8c21 7088 adjusted_bpaddr = address_significant (gdbarch, adjusted_bpaddr);
76897487
KB
7089
7090 /* An adjusted breakpoint address can significantly alter
dda83cd7 7091 a user's expectations. Print a warning if an adjustment
76897487
KB
7092 is required. */
7093 if (adjusted_bpaddr != bpaddr)
7094 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
7095
7096 return adjusted_bpaddr;
7097 }
7098}
7099
cb1e4e32
PA
7100static bp_loc_type
7101bp_location_from_bp_type (bptype type)
7cc221ef 7102{
cb1e4e32 7103 switch (type)
e049a4b5
DJ
7104 {
7105 case bp_breakpoint:
7c16b83e 7106 case bp_single_step:
e049a4b5
DJ
7107 case bp_until:
7108 case bp_finish:
7109 case bp_longjmp:
7110 case bp_longjmp_resume:
e2e4d78b 7111 case bp_longjmp_call_dummy:
186c406b
TT
7112 case bp_exception:
7113 case bp_exception_resume:
e049a4b5 7114 case bp_step_resume:
2c03e5be 7115 case bp_hp_step_resume:
e049a4b5
DJ
7116 case bp_watchpoint_scope:
7117 case bp_call_dummy:
aa7d318d 7118 case bp_std_terminate:
e049a4b5
DJ
7119 case bp_shlib_event:
7120 case bp_thread_event:
7121 case bp_overlay_event:
4efc6507 7122 case bp_jit_event:
0fd8e87f 7123 case bp_longjmp_master:
aa7d318d 7124 case bp_std_terminate_master:
186c406b 7125 case bp_exception_master:
0e30163f
JK
7126 case bp_gnu_ifunc_resolver:
7127 case bp_gnu_ifunc_resolver_return:
e7e0cddf 7128 case bp_dprintf:
cb1e4e32 7129 return bp_loc_software_breakpoint;
e049a4b5 7130 case bp_hardware_breakpoint:
cb1e4e32 7131 return bp_loc_hardware_breakpoint;
e049a4b5
DJ
7132 case bp_hardware_watchpoint:
7133 case bp_read_watchpoint:
7134 case bp_access_watchpoint:
cb1e4e32 7135 return bp_loc_hardware_watchpoint;
e049a4b5 7136 case bp_watchpoint:
ce78b96d 7137 case bp_catchpoint:
15c3d785
PA
7138 case bp_tracepoint:
7139 case bp_fast_tracepoint:
0fb4aa4b 7140 case bp_static_tracepoint:
cb1e4e32 7141 return bp_loc_other;
e049a4b5 7142 default:
e2e0b3e5 7143 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
e049a4b5 7144 }
cb1e4e32
PA
7145}
7146
7147bp_location::bp_location (breakpoint *owner, bp_loc_type type)
7148{
7149 this->owner = owner;
7150 this->cond_bytecode = NULL;
7151 this->shlib_disabled = 0;
7152 this->enabled = 1;
b5fa468f 7153 this->disabled_by_cond = false;
cb1e4e32
PA
7154
7155 this->loc_type = type;
e049a4b5 7156
cb1e4e32
PA
7157 if (this->loc_type == bp_loc_software_breakpoint
7158 || this->loc_type == bp_loc_hardware_breakpoint)
7159 mark_breakpoint_location_modified (this);
7160
b6433ede 7161 incref ();
cb1e4e32
PA
7162}
7163
7164bp_location::bp_location (breakpoint *owner)
7165 : bp_location::bp_location (owner,
7166 bp_location_from_bp_type (owner->type))
7167{
28010a5d
PA
7168}
7169
7170/* Allocate a struct bp_location. */
7171
7172static struct bp_location *
7173allocate_bp_location (struct breakpoint *bpt)
7174{
348d480f
PA
7175 return bpt->ops->allocate_location (bpt);
7176}
7cc221ef 7177
f431efe5
PA
7178/* Decrement reference count. If the reference count reaches 0,
7179 destroy the bp_location. Sets *BLP to NULL. */
7180
7181static void
7182decref_bp_location (struct bp_location **blp)
7183{
b6433ede 7184 bp_location_ref_policy::decref (*blp);
f431efe5
PA
7185 *blp = NULL;
7186}
7187
346774a9 7188/* Add breakpoint B at the end of the global breakpoint chain. */
c906108c 7189
b270e6f9
TT
7190static breakpoint *
7191add_to_breakpoint_chain (std::unique_ptr<breakpoint> &&b)
c906108c 7192{
346774a9 7193 struct breakpoint *b1;
b270e6f9 7194 struct breakpoint *result = b.get ();
c906108c 7195
346774a9
PA
7196 /* Add this breakpoint to the end of the chain so that a list of
7197 breakpoints will come out in order of increasing numbers. */
7198
7199 b1 = breakpoint_chain;
7200 if (b1 == 0)
b270e6f9 7201 breakpoint_chain = b.release ();
346774a9
PA
7202 else
7203 {
7204 while (b1->next)
7205 b1 = b1->next;
b270e6f9 7206 b1->next = b.release ();
346774a9 7207 }
b270e6f9
TT
7208
7209 return result;
346774a9
PA
7210}
7211
7212/* Initializes breakpoint B with type BPTYPE and no locations yet. */
7213
7214static void
7215init_raw_breakpoint_without_location (struct breakpoint *b,
7216 struct gdbarch *gdbarch,
28010a5d 7217 enum bptype bptype,
c0a91b2b 7218 const struct breakpoint_ops *ops)
346774a9 7219{
348d480f
PA
7220 gdb_assert (ops != NULL);
7221
28010a5d 7222 b->ops = ops;
4d28f7a8 7223 b->type = bptype;
a6d9a66e 7224 b->gdbarch = gdbarch;
c906108c
SS
7225 b->language = current_language->la_language;
7226 b->input_radix = input_radix;
d0fb5eae 7227 b->related_breakpoint = b;
346774a9
PA
7228}
7229
7230/* Helper to set_raw_breakpoint below. Creates a breakpoint
7231 that has type BPTYPE and has no locations as yet. */
346774a9
PA
7232
7233static struct breakpoint *
7234set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
348d480f 7235 enum bptype bptype,
c0a91b2b 7236 const struct breakpoint_ops *ops)
346774a9 7237{
3b0871f4 7238 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (bptype);
346774a9 7239
3b0871f4 7240 init_raw_breakpoint_without_location (b.get (), gdbarch, bptype, ops);
b270e6f9 7241 return add_to_breakpoint_chain (std::move (b));
0d381245
VP
7242}
7243
0ba852ab 7244/* Initialize loc->function_name. */
0e30163f 7245
0d381245 7246static void
0ba852ab 7247set_breakpoint_location_function (struct bp_location *loc)
0d381245 7248{
2bdf28a0
JK
7249 gdb_assert (loc->owner != NULL);
7250
0d381245 7251 if (loc->owner->type == bp_breakpoint
1042e4c0 7252 || loc->owner->type == bp_hardware_breakpoint
d77f58be 7253 || is_tracepoint (loc->owner))
0d381245 7254 {
2c02bd72 7255 const char *function_name;
0e30163f 7256
3467ec66 7257 if (loc->msymbol != NULL
f50776aa 7258 && (MSYMBOL_TYPE (loc->msymbol) == mst_text_gnu_ifunc
0ba852ab 7259 || MSYMBOL_TYPE (loc->msymbol) == mst_data_gnu_ifunc))
0e30163f
JK
7260 {
7261 struct breakpoint *b = loc->owner;
7262
c9d95fa3 7263 function_name = loc->msymbol->linkage_name ();
3467ec66
PA
7264
7265 if (b->type == bp_breakpoint && b->loc == loc
7266 && loc->next == NULL && b->related_breakpoint == b)
0e30163f
JK
7267 {
7268 /* Create only the whole new breakpoint of this type but do not
7269 mess more complicated breakpoints with multiple locations. */
7270 b->type = bp_gnu_ifunc_resolver;
6a3a010b 7271 /* Remember the resolver's address for use by the return
dda83cd7 7272 breakpoint. */
3467ec66 7273 loc->related_address = loc->address;
0e30163f
JK
7274 }
7275 }
3467ec66
PA
7276 else
7277 find_pc_partial_function (loc->address, &function_name, NULL, NULL);
0e30163f 7278
2c02bd72
DE
7279 if (function_name)
7280 loc->function_name = xstrdup (function_name);
0d381245
VP
7281 }
7282}
7283
a6d9a66e 7284/* Attempt to determine architecture of location identified by SAL. */
1bfeeb0f 7285struct gdbarch *
a6d9a66e
UW
7286get_sal_arch (struct symtab_and_line sal)
7287{
7288 if (sal.section)
08feed99 7289 return sal.section->objfile->arch ();
a6d9a66e 7290 if (sal.symtab)
08feed99 7291 return SYMTAB_OBJFILE (sal.symtab)->arch ();
a6d9a66e
UW
7292
7293 return NULL;
7294}
7295
346774a9
PA
7296/* Low level routine for partially initializing a breakpoint of type
7297 BPTYPE. The newly created breakpoint's address, section, source
c56053d2 7298 file name, and line number are provided by SAL.
0d381245
VP
7299
7300 It is expected that the caller will complete the initialization of
7301 the newly created breakpoint struct as well as output any status
c56053d2 7302 information regarding the creation of a new breakpoint. */
0d381245 7303
346774a9
PA
7304static void
7305init_raw_breakpoint (struct breakpoint *b, struct gdbarch *gdbarch,
28010a5d 7306 struct symtab_and_line sal, enum bptype bptype,
c0a91b2b 7307 const struct breakpoint_ops *ops)
0d381245 7308{
28010a5d 7309 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
346774a9 7310
3742cc8b 7311 add_location_to_breakpoint (b, &sal);
0d381245 7312
6c95b8df
PA
7313 if (bptype != bp_catchpoint)
7314 gdb_assert (sal.pspace != NULL);
7315
f8eba3c6
TT
7316 /* Store the program space that was used to set the breakpoint,
7317 except for ordinary breakpoints, which are independent of the
7318 program space. */
7319 if (bptype != bp_breakpoint && bptype != bp_hardware_breakpoint)
7320 b->pspace = sal.pspace;
346774a9 7321}
c906108c 7322
346774a9
PA
7323/* set_raw_breakpoint is a low level routine for allocating and
7324 partially initializing a breakpoint of type BPTYPE. The newly
7325 created breakpoint's address, section, source file name, and line
7326 number are provided by SAL. The newly created and partially
7327 initialized breakpoint is added to the breakpoint chain and
7328 is also returned as the value of this function.
7329
7330 It is expected that the caller will complete the initialization of
7331 the newly created breakpoint struct as well as output any status
7332 information regarding the creation of a new breakpoint. In
7333 particular, set_raw_breakpoint does NOT set the breakpoint
7334 number! Care should be taken to not allow an error to occur
7335 prior to completing the initialization of the breakpoint. If this
7336 should happen, a bogus breakpoint will be left on the chain. */
7337
7338struct breakpoint *
7339set_raw_breakpoint (struct gdbarch *gdbarch,
348d480f 7340 struct symtab_and_line sal, enum bptype bptype,
c0a91b2b 7341 const struct breakpoint_ops *ops)
346774a9 7342{
3b0871f4 7343 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (bptype);
346774a9 7344
3b0871f4 7345 init_raw_breakpoint (b.get (), gdbarch, sal, bptype, ops);
b270e6f9 7346 return add_to_breakpoint_chain (std::move (b));
c906108c
SS
7347}
7348
53a5351d 7349/* Call this routine when stepping and nexting to enable a breakpoint
186c406b
TT
7350 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
7351 initiated the operation. */
c906108c
SS
7352
7353void
186c406b 7354set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
c906108c 7355{
35df4500 7356 struct breakpoint *b, *b_tmp;
5d5658a1 7357 int thread = tp->global_num;
0fd8e87f
UW
7358
7359 /* To avoid having to rescan all objfile symbols at every step,
7360 we maintain a list of continually-inserted but always disabled
7361 longjmp "master" breakpoints. Here, we simply create momentary
7362 clones of those and enable them for the requested thread. */
35df4500 7363 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df 7364 if (b->pspace == current_program_space
186c406b
TT
7365 && (b->type == bp_longjmp_master
7366 || b->type == bp_exception_master))
0fd8e87f 7367 {
06edf0c0
PA
7368 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
7369 struct breakpoint *clone;
cc59ec59 7370
e2e4d78b
JK
7371 /* longjmp_breakpoint_ops ensures INITIATING_FRAME is cleared again
7372 after their removal. */
06edf0c0 7373 clone = momentary_breakpoint_from_master (b, type,
c1fc2657 7374 &momentary_breakpoint_ops, 1);
0fd8e87f
UW
7375 clone->thread = thread;
7376 }
186c406b
TT
7377
7378 tp->initiating_frame = frame;
c906108c
SS
7379}
7380
611c83ae 7381/* Delete all longjmp breakpoints from THREAD. */
c906108c 7382void
611c83ae 7383delete_longjmp_breakpoint (int thread)
c906108c 7384{
35df4500 7385 struct breakpoint *b, *b_tmp;
c906108c 7386
35df4500 7387 ALL_BREAKPOINTS_SAFE (b, b_tmp)
186c406b 7388 if (b->type == bp_longjmp || b->type == bp_exception)
611c83ae
PA
7389 {
7390 if (b->thread == thread)
7391 delete_breakpoint (b);
7392 }
c906108c
SS
7393}
7394
f59f708a
PA
7395void
7396delete_longjmp_breakpoint_at_next_stop (int thread)
7397{
7398 struct breakpoint *b, *b_tmp;
7399
7400 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7401 if (b->type == bp_longjmp || b->type == bp_exception)
7402 {
7403 if (b->thread == thread)
7404 b->disposition = disp_del_at_next_stop;
7405 }
7406}
7407
e2e4d78b
JK
7408/* Place breakpoints of type bp_longjmp_call_dummy to catch longjmp for
7409 INFERIOR_PTID thread. Chain them all by RELATED_BREAKPOINT and return
7410 pointer to any of them. Return NULL if this system cannot place longjmp
7411 breakpoints. */
7412
7413struct breakpoint *
7414set_longjmp_breakpoint_for_call_dummy (void)
7415{
7416 struct breakpoint *b, *retval = NULL;
7417
7418 ALL_BREAKPOINTS (b)
7419 if (b->pspace == current_program_space && b->type == bp_longjmp_master)
7420 {
7421 struct breakpoint *new_b;
7422
7423 new_b = momentary_breakpoint_from_master (b, bp_longjmp_call_dummy,
a1aa2221
LM
7424 &momentary_breakpoint_ops,
7425 1);
00431a78 7426 new_b->thread = inferior_thread ()->global_num;
e2e4d78b
JK
7427
7428 /* Link NEW_B into the chain of RETVAL breakpoints. */
7429
7430 gdb_assert (new_b->related_breakpoint == new_b);
7431 if (retval == NULL)
7432 retval = new_b;
7433 new_b->related_breakpoint = retval;
7434 while (retval->related_breakpoint != new_b->related_breakpoint)
7435 retval = retval->related_breakpoint;
7436 retval->related_breakpoint = new_b;
7437 }
7438
7439 return retval;
7440}
7441
7442/* Verify all existing dummy frames and their associated breakpoints for
b67a2c6f 7443 TP. Remove those which can no longer be found in the current frame
e2e4d78b
JK
7444 stack.
7445
7446 You should call this function only at places where it is safe to currently
7447 unwind the whole stack. Failed stack unwind would discard live dummy
7448 frames. */
7449
7450void
b67a2c6f 7451check_longjmp_breakpoint_for_call_dummy (struct thread_info *tp)
e2e4d78b
JK
7452{
7453 struct breakpoint *b, *b_tmp;
7454
7455 ALL_BREAKPOINTS_SAFE (b, b_tmp)
5d5658a1 7456 if (b->type == bp_longjmp_call_dummy && b->thread == tp->global_num)
e2e4d78b
JK
7457 {
7458 struct breakpoint *dummy_b = b->related_breakpoint;
7459
7460 while (dummy_b != b && dummy_b->type != bp_call_dummy)
7461 dummy_b = dummy_b->related_breakpoint;
7462 if (dummy_b->type != bp_call_dummy
7463 || frame_find_by_id (dummy_b->frame_id) != NULL)
7464 continue;
7465
00431a78 7466 dummy_frame_discard (dummy_b->frame_id, tp);
e2e4d78b
JK
7467
7468 while (b->related_breakpoint != b)
7469 {
7470 if (b_tmp == b->related_breakpoint)
7471 b_tmp = b->related_breakpoint->next;
7472 delete_breakpoint (b->related_breakpoint);
7473 }
7474 delete_breakpoint (b);
7475 }
7476}
7477
1900040c
MS
7478void
7479enable_overlay_breakpoints (void)
7480{
52f0bd74 7481 struct breakpoint *b;
1900040c
MS
7482
7483 ALL_BREAKPOINTS (b)
7484 if (b->type == bp_overlay_event)
7485 {
7486 b->enable_state = bp_enabled;
44702360 7487 update_global_location_list (UGLL_MAY_INSERT);
c02f5703 7488 overlay_events_enabled = 1;
1900040c
MS
7489 }
7490}
7491
7492void
7493disable_overlay_breakpoints (void)
7494{
52f0bd74 7495 struct breakpoint *b;
1900040c
MS
7496
7497 ALL_BREAKPOINTS (b)
7498 if (b->type == bp_overlay_event)
7499 {
7500 b->enable_state = bp_disabled;
44702360 7501 update_global_location_list (UGLL_DONT_INSERT);
c02f5703 7502 overlay_events_enabled = 0;
1900040c
MS
7503 }
7504}
7505
aa7d318d
TT
7506/* Set an active std::terminate breakpoint for each std::terminate
7507 master breakpoint. */
7508void
7509set_std_terminate_breakpoint (void)
7510{
35df4500 7511 struct breakpoint *b, *b_tmp;
aa7d318d 7512
35df4500 7513 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
7514 if (b->pspace == current_program_space
7515 && b->type == bp_std_terminate_master)
7516 {
06edf0c0 7517 momentary_breakpoint_from_master (b, bp_std_terminate,
a1aa2221 7518 &momentary_breakpoint_ops, 1);
aa7d318d
TT
7519 }
7520}
7521
7522/* Delete all the std::terminate breakpoints. */
7523void
7524delete_std_terminate_breakpoint (void)
7525{
35df4500 7526 struct breakpoint *b, *b_tmp;
aa7d318d 7527
35df4500 7528 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
7529 if (b->type == bp_std_terminate)
7530 delete_breakpoint (b);
7531}
7532
c4093a6a 7533struct breakpoint *
a6d9a66e 7534create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
c4093a6a
JM
7535{
7536 struct breakpoint *b;
c4093a6a 7537
06edf0c0
PA
7538 b = create_internal_breakpoint (gdbarch, address, bp_thread_event,
7539 &internal_breakpoint_ops);
7540
b5de0fa7 7541 b->enable_state = bp_enabled;
f00aae0f 7542 /* location has to be used or breakpoint_re_set will delete me. */
d28cd78a 7543 b->location = new_address_location (b->loc->address, NULL, 0);
c4093a6a 7544
44702360 7545 update_global_location_list_nothrow (UGLL_MAY_INSERT);
74960c60 7546
c4093a6a
JM
7547 return b;
7548}
7549
0101ce28
JJ
7550struct lang_and_radix
7551 {
7552 enum language lang;
7553 int radix;
7554 };
7555
4efc6507
DE
7556/* Create a breakpoint for JIT code registration and unregistration. */
7557
7558struct breakpoint *
7559create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7560{
2a7f3dff
PA
7561 return create_internal_breakpoint (gdbarch, address, bp_jit_event,
7562 &internal_breakpoint_ops);
4efc6507 7563}
0101ce28 7564
03673fc7
PP
7565/* Remove JIT code registration and unregistration breakpoint(s). */
7566
7567void
7568remove_jit_event_breakpoints (void)
7569{
7570 struct breakpoint *b, *b_tmp;
7571
7572 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7573 if (b->type == bp_jit_event
7574 && b->loc->pspace == current_program_space)
7575 delete_breakpoint (b);
7576}
7577
cae688ec
JJ
7578void
7579remove_solib_event_breakpoints (void)
7580{
35df4500 7581 struct breakpoint *b, *b_tmp;
cae688ec 7582
35df4500 7583 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df
PA
7584 if (b->type == bp_shlib_event
7585 && b->loc->pspace == current_program_space)
cae688ec
JJ
7586 delete_breakpoint (b);
7587}
7588
f37f681c
PA
7589/* See breakpoint.h. */
7590
7591void
7592remove_solib_event_breakpoints_at_next_stop (void)
7593{
7594 struct breakpoint *b, *b_tmp;
7595
7596 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7597 if (b->type == bp_shlib_event
7598 && b->loc->pspace == current_program_space)
7599 b->disposition = disp_del_at_next_stop;
7600}
7601
04086b45
PA
7602/* Helper for create_solib_event_breakpoint /
7603 create_and_insert_solib_event_breakpoint. Allows specifying which
7604 INSERT_MODE to pass through to update_global_location_list. */
7605
7606static struct breakpoint *
7607create_solib_event_breakpoint_1 (struct gdbarch *gdbarch, CORE_ADDR address,
7608 enum ugll_insert_mode insert_mode)
cae688ec
JJ
7609{
7610 struct breakpoint *b;
7611
06edf0c0
PA
7612 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event,
7613 &internal_breakpoint_ops);
04086b45 7614 update_global_location_list_nothrow (insert_mode);
cae688ec
JJ
7615 return b;
7616}
7617
04086b45
PA
7618struct breakpoint *
7619create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7620{
7621 return create_solib_event_breakpoint_1 (gdbarch, address, UGLL_MAY_INSERT);
7622}
7623
f37f681c
PA
7624/* See breakpoint.h. */
7625
7626struct breakpoint *
7627create_and_insert_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7628{
7629 struct breakpoint *b;
7630
04086b45
PA
7631 /* Explicitly tell update_global_location_list to insert
7632 locations. */
7633 b = create_solib_event_breakpoint_1 (gdbarch, address, UGLL_INSERT);
f37f681c
PA
7634 if (!b->loc->inserted)
7635 {
7636 delete_breakpoint (b);
7637 return NULL;
7638 }
7639 return b;
7640}
7641
cae688ec
JJ
7642/* Disable any breakpoints that are on code in shared libraries. Only
7643 apply to enabled breakpoints, disabled ones can just stay disabled. */
7644
7645void
cb851954 7646disable_breakpoints_in_shlibs (void)
cae688ec 7647{
876fa593 7648 struct bp_location *loc, **locp_tmp;
cae688ec 7649
876fa593 7650 ALL_BP_LOCATIONS (loc, locp_tmp)
cae688ec 7651 {
2bdf28a0 7652 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 7653 struct breakpoint *b = loc->owner;
2bdf28a0 7654
4a64f543
MS
7655 /* We apply the check to all breakpoints, including disabled for
7656 those with loc->duplicate set. This is so that when breakpoint
7657 becomes enabled, or the duplicate is removed, gdb will try to
7658 insert all breakpoints. If we don't set shlib_disabled here,
7659 we'll try to insert those breakpoints and fail. */
1042e4c0 7660 if (((b->type == bp_breakpoint)
508ccb1f 7661 || (b->type == bp_jit_event)
1042e4c0 7662 || (b->type == bp_hardware_breakpoint)
d77f58be 7663 || (is_tracepoint (b)))
6c95b8df 7664 && loc->pspace == current_program_space
0d381245 7665 && !loc->shlib_disabled
6c95b8df 7666 && solib_name_from_address (loc->pspace, loc->address)
a77053c2 7667 )
0d381245
VP
7668 {
7669 loc->shlib_disabled = 1;
7670 }
cae688ec
JJ
7671 }
7672}
7673
63644780
NB
7674/* Disable any breakpoints and tracepoints that are in SOLIB upon
7675 notification of unloaded_shlib. Only apply to enabled breakpoints,
7676 disabled ones can just stay disabled. */
84acb35a 7677
75149521 7678static void
84acb35a
JJ
7679disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
7680{
876fa593 7681 struct bp_location *loc, **locp_tmp;
84acb35a
JJ
7682 int disabled_shlib_breaks = 0;
7683
876fa593 7684 ALL_BP_LOCATIONS (loc, locp_tmp)
84acb35a 7685 {
2bdf28a0 7686 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 7687 struct breakpoint *b = loc->owner;
cc59ec59 7688
1e4d1764 7689 if (solib->pspace == loc->pspace
e2dd7057 7690 && !loc->shlib_disabled
1e4d1764
YQ
7691 && (((b->type == bp_breakpoint
7692 || b->type == bp_jit_event
7693 || b->type == bp_hardware_breakpoint)
7694 && (loc->loc_type == bp_loc_hardware_breakpoint
7695 || loc->loc_type == bp_loc_software_breakpoint))
7696 || is_tracepoint (b))
e2dd7057 7697 && solib_contains_address_p (solib, loc->address))
84acb35a 7698 {
e2dd7057
PP
7699 loc->shlib_disabled = 1;
7700 /* At this point, we cannot rely on remove_breakpoint
7701 succeeding so we must mark the breakpoint as not inserted
7702 to prevent future errors occurring in remove_breakpoints. */
7703 loc->inserted = 0;
8d3788bd
VP
7704
7705 /* This may cause duplicate notifications for the same breakpoint. */
76727919 7706 gdb::observers::breakpoint_modified.notify (b);
8d3788bd 7707
e2dd7057
PP
7708 if (!disabled_shlib_breaks)
7709 {
223ffa71 7710 target_terminal::ours_for_output ();
3e43a32a
MS
7711 warning (_("Temporarily disabling breakpoints "
7712 "for unloaded shared library \"%s\""),
e2dd7057 7713 solib->so_name);
84acb35a 7714 }
e2dd7057 7715 disabled_shlib_breaks = 1;
84acb35a
JJ
7716 }
7717 }
84acb35a
JJ
7718}
7719
63644780
NB
7720/* Disable any breakpoints and tracepoints in OBJFILE upon
7721 notification of free_objfile. Only apply to enabled breakpoints,
7722 disabled ones can just stay disabled. */
7723
7724static void
7725disable_breakpoints_in_freed_objfile (struct objfile *objfile)
7726{
7727 struct breakpoint *b;
7728
7729 if (objfile == NULL)
7730 return;
7731
d03de421
PA
7732 /* OBJF_SHARED|OBJF_USERLOADED objfiles are dynamic modules manually
7733 managed by the user with add-symbol-file/remove-symbol-file.
7734 Similarly to how breakpoints in shared libraries are handled in
7735 response to "nosharedlibrary", mark breakpoints in such modules
08351840
PA
7736 shlib_disabled so they end up uninserted on the next global
7737 location list update. Shared libraries not loaded by the user
7738 aren't handled here -- they're already handled in
7739 disable_breakpoints_in_unloaded_shlib, called by solib.c's
7740 solib_unloaded observer. We skip objfiles that are not
d03de421
PA
7741 OBJF_SHARED as those aren't considered dynamic objects (e.g. the
7742 main objfile). */
7743 if ((objfile->flags & OBJF_SHARED) == 0
7744 || (objfile->flags & OBJF_USERLOADED) == 0)
63644780
NB
7745 return;
7746
7747 ALL_BREAKPOINTS (b)
7748 {
7749 struct bp_location *loc;
7750 int bp_modified = 0;
7751
7752 if (!is_breakpoint (b) && !is_tracepoint (b))
7753 continue;
7754
7755 for (loc = b->loc; loc != NULL; loc = loc->next)
7756 {
7757 CORE_ADDR loc_addr = loc->address;
7758
7759 if (loc->loc_type != bp_loc_hardware_breakpoint
7760 && loc->loc_type != bp_loc_software_breakpoint)
7761 continue;
7762
7763 if (loc->shlib_disabled != 0)
7764 continue;
7765
7766 if (objfile->pspace != loc->pspace)
7767 continue;
7768
7769 if (loc->loc_type != bp_loc_hardware_breakpoint
7770 && loc->loc_type != bp_loc_software_breakpoint)
7771 continue;
7772
7773 if (is_addr_in_objfile (loc_addr, objfile))
7774 {
7775 loc->shlib_disabled = 1;
08351840
PA
7776 /* At this point, we don't know whether the object was
7777 unmapped from the inferior or not, so leave the
7778 inserted flag alone. We'll handle failure to
7779 uninsert quietly, in case the object was indeed
7780 unmapped. */
63644780
NB
7781
7782 mark_breakpoint_location_modified (loc);
7783
7784 bp_modified = 1;
7785 }
7786 }
7787
7788 if (bp_modified)
76727919 7789 gdb::observers::breakpoint_modified.notify (b);
63644780
NB
7790 }
7791}
7792
ce78b96d
JB
7793/* FORK & VFORK catchpoints. */
7794
e29a4733 7795/* An instance of this type is used to represent a fork or vfork
c1fc2657
SM
7796 catchpoint. A breakpoint is really of this type iff its ops pointer points
7797 to CATCH_FORK_BREAKPOINT_OPS. */
e29a4733 7798
c1fc2657 7799struct fork_catchpoint : public breakpoint
e29a4733 7800{
e29a4733
PA
7801 /* Process id of a child process whose forking triggered this
7802 catchpoint. This field is only valid immediately after this
7803 catchpoint has triggered. */
7804 ptid_t forked_inferior_pid;
7805};
7806
4a64f543
MS
7807/* Implement the "insert" breakpoint_ops method for fork
7808 catchpoints. */
ce78b96d 7809
77b06cd7
TJB
7810static int
7811insert_catch_fork (struct bp_location *bl)
ce78b96d 7812{
e99b03dc 7813 return target_insert_fork_catchpoint (inferior_ptid.pid ());
ce78b96d
JB
7814}
7815
4a64f543
MS
7816/* Implement the "remove" breakpoint_ops method for fork
7817 catchpoints. */
ce78b96d
JB
7818
7819static int
73971819 7820remove_catch_fork (struct bp_location *bl, enum remove_bp_reason reason)
ce78b96d 7821{
e99b03dc 7822 return target_remove_fork_catchpoint (inferior_ptid.pid ());
ce78b96d
JB
7823}
7824
7825/* Implement the "breakpoint_hit" breakpoint_ops method for fork
7826 catchpoints. */
7827
7828static int
f1310107 7829breakpoint_hit_catch_fork (const struct bp_location *bl,
bd522513 7830 const address_space *aspace, CORE_ADDR bp_addr,
09ac7c10 7831 const struct target_waitstatus *ws)
ce78b96d 7832{
e29a4733
PA
7833 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7834
f90263c1
TT
7835 if (ws->kind != TARGET_WAITKIND_FORKED)
7836 return 0;
7837
7838 c->forked_inferior_pid = ws->value.related_pid;
7839 return 1;
ce78b96d
JB
7840}
7841
4a64f543
MS
7842/* Implement the "print_it" breakpoint_ops method for fork
7843 catchpoints. */
ce78b96d
JB
7844
7845static enum print_stop_action
348d480f 7846print_it_catch_fork (bpstat bs)
ce78b96d 7847{
36dfb11c 7848 struct ui_out *uiout = current_uiout;
348d480f
PA
7849 struct breakpoint *b = bs->breakpoint_at;
7850 struct fork_catchpoint *c = (struct fork_catchpoint *) bs->breakpoint_at;
e29a4733 7851
ce78b96d 7852 annotate_catchpoint (b->number);
f303dbd6 7853 maybe_print_thread_hit_breakpoint (uiout);
36dfb11c 7854 if (b->disposition == disp_del)
112e8700 7855 uiout->text ("Temporary catchpoint ");
36dfb11c 7856 else
112e8700
SM
7857 uiout->text ("Catchpoint ");
7858 if (uiout->is_mi_like_p ())
36dfb11c 7859 {
112e8700
SM
7860 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_FORK));
7861 uiout->field_string ("disp", bpdisp_text (b->disposition));
36dfb11c 7862 }
381befee 7863 uiout->field_signed ("bkptno", b->number);
112e8700 7864 uiout->text (" (forked process ");
381befee 7865 uiout->field_signed ("newpid", c->forked_inferior_pid.pid ());
112e8700 7866 uiout->text ("), ");
ce78b96d
JB
7867 return PRINT_SRC_AND_LOC;
7868}
7869
4a64f543
MS
7870/* Implement the "print_one" breakpoint_ops method for fork
7871 catchpoints. */
ce78b96d
JB
7872
7873static void
a6d9a66e 7874print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 7875{
e29a4733 7876 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
79a45b7d 7877 struct value_print_options opts;
79a45e25 7878 struct ui_out *uiout = current_uiout;
79a45b7d
TT
7879
7880 get_user_print_options (&opts);
7881
4a64f543
MS
7882 /* Field 4, the address, is omitted (which makes the columns not
7883 line up too nicely with the headers, but the effect is relatively
7884 readable). */
79a45b7d 7885 if (opts.addressprint)
112e8700 7886 uiout->field_skip ("addr");
ce78b96d 7887 annotate_field (5);
112e8700 7888 uiout->text ("fork");
d7e15655 7889 if (c->forked_inferior_pid != null_ptid)
ce78b96d 7890 {
112e8700 7891 uiout->text (", process ");
381befee 7892 uiout->field_signed ("what", c->forked_inferior_pid.pid ());
112e8700 7893 uiout->spaces (1);
ce78b96d 7894 }
8ac3646f 7895
112e8700
SM
7896 if (uiout->is_mi_like_p ())
7897 uiout->field_string ("catch-type", "fork");
ce78b96d
JB
7898}
7899
7900/* Implement the "print_mention" breakpoint_ops method for fork
7901 catchpoints. */
7902
7903static void
7904print_mention_catch_fork (struct breakpoint *b)
7905{
7906 printf_filtered (_("Catchpoint %d (fork)"), b->number);
7907}
7908
6149aea9
PA
7909/* Implement the "print_recreate" breakpoint_ops method for fork
7910 catchpoints. */
7911
7912static void
7913print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
7914{
7915 fprintf_unfiltered (fp, "catch fork");
d9b3f62e 7916 print_recreate_thread (b, fp);
6149aea9
PA
7917}
7918
ce78b96d
JB
7919/* The breakpoint_ops structure to be used in fork catchpoints. */
7920
2060206e 7921static struct breakpoint_ops catch_fork_breakpoint_ops;
ce78b96d 7922
4a64f543
MS
7923/* Implement the "insert" breakpoint_ops method for vfork
7924 catchpoints. */
ce78b96d 7925
77b06cd7
TJB
7926static int
7927insert_catch_vfork (struct bp_location *bl)
ce78b96d 7928{
e99b03dc 7929 return target_insert_vfork_catchpoint (inferior_ptid.pid ());
ce78b96d
JB
7930}
7931
4a64f543
MS
7932/* Implement the "remove" breakpoint_ops method for vfork
7933 catchpoints. */
ce78b96d
JB
7934
7935static int
73971819 7936remove_catch_vfork (struct bp_location *bl, enum remove_bp_reason reason)
ce78b96d 7937{
e99b03dc 7938 return target_remove_vfork_catchpoint (inferior_ptid.pid ());
ce78b96d
JB
7939}
7940
7941/* Implement the "breakpoint_hit" breakpoint_ops method for vfork
7942 catchpoints. */
7943
7944static int
f1310107 7945breakpoint_hit_catch_vfork (const struct bp_location *bl,
bd522513 7946 const address_space *aspace, CORE_ADDR bp_addr,
09ac7c10 7947 const struct target_waitstatus *ws)
ce78b96d 7948{
e29a4733
PA
7949 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7950
f90263c1
TT
7951 if (ws->kind != TARGET_WAITKIND_VFORKED)
7952 return 0;
7953
7954 c->forked_inferior_pid = ws->value.related_pid;
7955 return 1;
ce78b96d
JB
7956}
7957
4a64f543
MS
7958/* Implement the "print_it" breakpoint_ops method for vfork
7959 catchpoints. */
ce78b96d
JB
7960
7961static enum print_stop_action
348d480f 7962print_it_catch_vfork (bpstat bs)
ce78b96d 7963{
36dfb11c 7964 struct ui_out *uiout = current_uiout;
348d480f 7965 struct breakpoint *b = bs->breakpoint_at;
e29a4733
PA
7966 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
7967
ce78b96d 7968 annotate_catchpoint (b->number);
f303dbd6 7969 maybe_print_thread_hit_breakpoint (uiout);
36dfb11c 7970 if (b->disposition == disp_del)
112e8700 7971 uiout->text ("Temporary catchpoint ");
36dfb11c 7972 else
112e8700
SM
7973 uiout->text ("Catchpoint ");
7974 if (uiout->is_mi_like_p ())
36dfb11c 7975 {
112e8700
SM
7976 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_VFORK));
7977 uiout->field_string ("disp", bpdisp_text (b->disposition));
36dfb11c 7978 }
381befee 7979 uiout->field_signed ("bkptno", b->number);
112e8700 7980 uiout->text (" (vforked process ");
381befee 7981 uiout->field_signed ("newpid", c->forked_inferior_pid.pid ());
112e8700 7982 uiout->text ("), ");
ce78b96d
JB
7983 return PRINT_SRC_AND_LOC;
7984}
7985
4a64f543
MS
7986/* Implement the "print_one" breakpoint_ops method for vfork
7987 catchpoints. */
ce78b96d
JB
7988
7989static void
a6d9a66e 7990print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 7991{
e29a4733 7992 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
79a45b7d 7993 struct value_print_options opts;
79a45e25 7994 struct ui_out *uiout = current_uiout;
79a45b7d
TT
7995
7996 get_user_print_options (&opts);
4a64f543
MS
7997 /* Field 4, the address, is omitted (which makes the columns not
7998 line up too nicely with the headers, but the effect is relatively
7999 readable). */
79a45b7d 8000 if (opts.addressprint)
112e8700 8001 uiout->field_skip ("addr");
ce78b96d 8002 annotate_field (5);
112e8700 8003 uiout->text ("vfork");
d7e15655 8004 if (c->forked_inferior_pid != null_ptid)
ce78b96d 8005 {
112e8700 8006 uiout->text (", process ");
381befee 8007 uiout->field_signed ("what", c->forked_inferior_pid.pid ());
112e8700 8008 uiout->spaces (1);
ce78b96d 8009 }
8ac3646f 8010
112e8700
SM
8011 if (uiout->is_mi_like_p ())
8012 uiout->field_string ("catch-type", "vfork");
ce78b96d
JB
8013}
8014
8015/* Implement the "print_mention" breakpoint_ops method for vfork
8016 catchpoints. */
8017
8018static void
8019print_mention_catch_vfork (struct breakpoint *b)
8020{
8021 printf_filtered (_("Catchpoint %d (vfork)"), b->number);
8022}
8023
6149aea9
PA
8024/* Implement the "print_recreate" breakpoint_ops method for vfork
8025 catchpoints. */
8026
8027static void
8028print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
8029{
8030 fprintf_unfiltered (fp, "catch vfork");
d9b3f62e 8031 print_recreate_thread (b, fp);
6149aea9
PA
8032}
8033
ce78b96d
JB
8034/* The breakpoint_ops structure to be used in vfork catchpoints. */
8035
2060206e 8036static struct breakpoint_ops catch_vfork_breakpoint_ops;
ce78b96d 8037
edcc5120 8038/* An instance of this type is used to represent an solib catchpoint.
c1fc2657 8039 A breakpoint is really of this type iff its ops pointer points to
edcc5120
TT
8040 CATCH_SOLIB_BREAKPOINT_OPS. */
8041
c1fc2657 8042struct solib_catchpoint : public breakpoint
edcc5120 8043{
c1fc2657 8044 ~solib_catchpoint () override;
edcc5120
TT
8045
8046 /* True for "catch load", false for "catch unload". */
b650a282 8047 bool is_load;
edcc5120
TT
8048
8049 /* Regular expression to match, if any. COMPILED is only valid when
8050 REGEX is non-NULL. */
8051 char *regex;
2d7cc5c7 8052 std::unique_ptr<compiled_regex> compiled;
edcc5120
TT
8053};
8054
c1fc2657 8055solib_catchpoint::~solib_catchpoint ()
edcc5120 8056{
c1fc2657 8057 xfree (this->regex);
edcc5120
TT
8058}
8059
8060static int
8061insert_catch_solib (struct bp_location *ignore)
8062{
8063 return 0;
8064}
8065
8066static int
73971819 8067remove_catch_solib (struct bp_location *ignore, enum remove_bp_reason reason)
edcc5120
TT
8068{
8069 return 0;
8070}
8071
8072static int
8073breakpoint_hit_catch_solib (const struct bp_location *bl,
bd522513 8074 const address_space *aspace,
edcc5120
TT
8075 CORE_ADDR bp_addr,
8076 const struct target_waitstatus *ws)
8077{
8078 struct solib_catchpoint *self = (struct solib_catchpoint *) bl->owner;
8079 struct breakpoint *other;
8080
8081 if (ws->kind == TARGET_WAITKIND_LOADED)
8082 return 1;
8083
8084 ALL_BREAKPOINTS (other)
8085 {
8086 struct bp_location *other_bl;
8087
8088 if (other == bl->owner)
8089 continue;
8090
8091 if (other->type != bp_shlib_event)
8092 continue;
8093
c1fc2657 8094 if (self->pspace != NULL && other->pspace != self->pspace)
edcc5120
TT
8095 continue;
8096
8097 for (other_bl = other->loc; other_bl != NULL; other_bl = other_bl->next)
8098 {
8099 if (other->ops->breakpoint_hit (other_bl, aspace, bp_addr, ws))
8100 return 1;
8101 }
8102 }
8103
8104 return 0;
8105}
8106
8107static void
8108check_status_catch_solib (struct bpstats *bs)
8109{
8110 struct solib_catchpoint *self
8111 = (struct solib_catchpoint *) bs->breakpoint_at;
edcc5120
TT
8112
8113 if (self->is_load)
8114 {
52941706 8115 for (so_list *iter : current_program_space->added_solibs)
edcc5120
TT
8116 {
8117 if (!self->regex
2d7cc5c7 8118 || self->compiled->exec (iter->so_name, 0, NULL, 0) == 0)
edcc5120
TT
8119 return;
8120 }
8121 }
8122 else
8123 {
6fb16ce6 8124 for (const std::string &iter : current_program_space->deleted_solibs)
edcc5120
TT
8125 {
8126 if (!self->regex
6fb16ce6 8127 || self->compiled->exec (iter.c_str (), 0, NULL, 0) == 0)
edcc5120
TT
8128 return;
8129 }
8130 }
8131
8132 bs->stop = 0;
8133 bs->print_it = print_it_noop;
8134}
8135
8136static enum print_stop_action
8137print_it_catch_solib (bpstat bs)
8138{
8139 struct breakpoint *b = bs->breakpoint_at;
8140 struct ui_out *uiout = current_uiout;
8141
8142 annotate_catchpoint (b->number);
f303dbd6 8143 maybe_print_thread_hit_breakpoint (uiout);
edcc5120 8144 if (b->disposition == disp_del)
112e8700 8145 uiout->text ("Temporary catchpoint ");
edcc5120 8146 else
112e8700 8147 uiout->text ("Catchpoint ");
381befee 8148 uiout->field_signed ("bkptno", b->number);
112e8700
SM
8149 uiout->text ("\n");
8150 if (uiout->is_mi_like_p ())
8151 uiout->field_string ("disp", bpdisp_text (b->disposition));
edcc5120
TT
8152 print_solib_event (1);
8153 return PRINT_SRC_AND_LOC;
8154}
8155
8156static void
8157print_one_catch_solib (struct breakpoint *b, struct bp_location **locs)
8158{
8159 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8160 struct value_print_options opts;
8161 struct ui_out *uiout = current_uiout;
edcc5120
TT
8162
8163 get_user_print_options (&opts);
8164 /* Field 4, the address, is omitted (which makes the columns not
8165 line up too nicely with the headers, but the effect is relatively
8166 readable). */
8167 if (opts.addressprint)
8168 {
8169 annotate_field (4);
112e8700 8170 uiout->field_skip ("addr");
edcc5120
TT
8171 }
8172
528e1572 8173 std::string msg;
edcc5120
TT
8174 annotate_field (5);
8175 if (self->is_load)
8176 {
8177 if (self->regex)
528e1572 8178 msg = string_printf (_("load of library matching %s"), self->regex);
edcc5120 8179 else
528e1572 8180 msg = _("load of library");
edcc5120
TT
8181 }
8182 else
8183 {
8184 if (self->regex)
528e1572 8185 msg = string_printf (_("unload of library matching %s"), self->regex);
edcc5120 8186 else
528e1572 8187 msg = _("unload of library");
edcc5120 8188 }
112e8700 8189 uiout->field_string ("what", msg);
8ac3646f 8190
112e8700
SM
8191 if (uiout->is_mi_like_p ())
8192 uiout->field_string ("catch-type", self->is_load ? "load" : "unload");
edcc5120
TT
8193}
8194
8195static void
8196print_mention_catch_solib (struct breakpoint *b)
8197{
8198 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8199
8200 printf_filtered (_("Catchpoint %d (%s)"), b->number,
8201 self->is_load ? "load" : "unload");
8202}
8203
8204static void
8205print_recreate_catch_solib (struct breakpoint *b, struct ui_file *fp)
8206{
8207 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8208
8209 fprintf_unfiltered (fp, "%s %s",
8210 b->disposition == disp_del ? "tcatch" : "catch",
8211 self->is_load ? "load" : "unload");
8212 if (self->regex)
8213 fprintf_unfiltered (fp, " %s", self->regex);
8214 fprintf_unfiltered (fp, "\n");
8215}
8216
8217static struct breakpoint_ops catch_solib_breakpoint_ops;
8218
b650a282 8219/* See breakpoint.h. */
edcc5120 8220
91985142 8221void
b650a282 8222add_solib_catchpoint (const char *arg, bool is_load, bool is_temp, bool enabled)
edcc5120 8223{
edcc5120 8224 struct gdbarch *gdbarch = get_current_arch ();
edcc5120 8225
edcc5120
TT
8226 if (!arg)
8227 arg = "";
f1735a53 8228 arg = skip_spaces (arg);
edcc5120 8229
36bd8eaa 8230 std::unique_ptr<solib_catchpoint> c (new solib_catchpoint ());
edcc5120
TT
8231
8232 if (*arg != '\0')
8233 {
2d7cc5c7
PA
8234 c->compiled.reset (new compiled_regex (arg, REG_NOSUB,
8235 _("Invalid regexp")));
edcc5120
TT
8236 c->regex = xstrdup (arg);
8237 }
8238
8239 c->is_load = is_load;
36bd8eaa 8240 init_catchpoint (c.get (), gdbarch, is_temp, NULL,
edcc5120
TT
8241 &catch_solib_breakpoint_ops);
8242
c1fc2657 8243 c->enable_state = enabled ? bp_enabled : bp_disabled;
91985142 8244
b270e6f9 8245 install_breakpoint (0, std::move (c), 1);
edcc5120
TT
8246}
8247
91985142
MG
8248/* A helper function that does all the work for "catch load" and
8249 "catch unload". */
8250
8251static void
eb4c3f4a 8252catch_load_or_unload (const char *arg, int from_tty, int is_load,
91985142
MG
8253 struct cmd_list_element *command)
8254{
91985142 8255 const int enabled = 1;
b650a282 8256 bool temp = get_cmd_context (command) == CATCH_TEMPORARY;
91985142 8257
b650a282 8258 add_solib_catchpoint (arg, is_load, temp, enabled);
91985142
MG
8259}
8260
edcc5120 8261static void
eb4c3f4a 8262catch_load_command_1 (const char *arg, int from_tty,
edcc5120
TT
8263 struct cmd_list_element *command)
8264{
8265 catch_load_or_unload (arg, from_tty, 1, command);
8266}
8267
8268static void
eb4c3f4a 8269catch_unload_command_1 (const char *arg, int from_tty,
edcc5120
TT
8270 struct cmd_list_element *command)
8271{
8272 catch_load_or_unload (arg, from_tty, 0, command);
8273}
8274
b650a282 8275/* See breakpoint.h. */
ce78b96d 8276
ab04a2af 8277void
346774a9 8278init_catchpoint (struct breakpoint *b,
b650a282 8279 struct gdbarch *gdbarch, bool temp,
63160a43 8280 const char *cond_string,
c0a91b2b 8281 const struct breakpoint_ops *ops)
c906108c 8282{
51abb421 8283 symtab_and_line sal;
6c95b8df 8284 sal.pspace = current_program_space;
c5aa993b 8285
28010a5d 8286 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
ce78b96d 8287
1b36a34b 8288 b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
b650a282 8289 b->disposition = temp ? disp_del : disp_donttouch;
346774a9
PA
8290}
8291
28010a5d 8292void
b270e6f9 8293install_breakpoint (int internal, std::unique_ptr<breakpoint> &&arg, int update_gll)
c56053d2 8294{
b270e6f9 8295 breakpoint *b = add_to_breakpoint_chain (std::move (arg));
3a5c3e22 8296 set_breakpoint_number (internal, b);
558a9d82
YQ
8297 if (is_tracepoint (b))
8298 set_tracepoint_count (breakpoint_count);
3a5c3e22
PA
8299 if (!internal)
8300 mention (b);
76727919 8301 gdb::observers::breakpoint_created.notify (b);
3ea46bff
YQ
8302
8303 if (update_gll)
44702360 8304 update_global_location_list (UGLL_MAY_INSERT);
c56053d2
PA
8305}
8306
9b70b993 8307static void
a6d9a66e 8308create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
b650a282 8309 bool temp, const char *cond_string,
dda83cd7 8310 const struct breakpoint_ops *ops)
c906108c 8311{
b270e6f9 8312 std::unique_ptr<fork_catchpoint> c (new fork_catchpoint ());
ce78b96d 8313
b650a282 8314 init_catchpoint (c.get (), gdbarch, temp, cond_string, ops);
e29a4733
PA
8315
8316 c->forked_inferior_pid = null_ptid;
8317
b270e6f9 8318 install_breakpoint (0, std::move (c), 1);
c906108c
SS
8319}
8320
fe798b75
JB
8321/* Exec catchpoints. */
8322
b4d90040 8323/* An instance of this type is used to represent an exec catchpoint.
c1fc2657 8324 A breakpoint is really of this type iff its ops pointer points to
b4d90040
PA
8325 CATCH_EXEC_BREAKPOINT_OPS. */
8326
c1fc2657 8327struct exec_catchpoint : public breakpoint
b4d90040 8328{
c1fc2657 8329 ~exec_catchpoint () override;
b4d90040
PA
8330
8331 /* Filename of a program whose exec triggered this catchpoint.
8332 This field is only valid immediately after this catchpoint has
8333 triggered. */
8334 char *exec_pathname;
8335};
8336
c1fc2657 8337/* Exec catchpoint destructor. */
b4d90040 8338
c1fc2657 8339exec_catchpoint::~exec_catchpoint ()
b4d90040 8340{
c1fc2657 8341 xfree (this->exec_pathname);
b4d90040
PA
8342}
8343
77b06cd7
TJB
8344static int
8345insert_catch_exec (struct bp_location *bl)
c906108c 8346{
e99b03dc 8347 return target_insert_exec_catchpoint (inferior_ptid.pid ());
fe798b75 8348}
c906108c 8349
fe798b75 8350static int
73971819 8351remove_catch_exec (struct bp_location *bl, enum remove_bp_reason reason)
fe798b75 8352{
e99b03dc 8353 return target_remove_exec_catchpoint (inferior_ptid.pid ());
fe798b75 8354}
c906108c 8355
fe798b75 8356static int
f1310107 8357breakpoint_hit_catch_exec (const struct bp_location *bl,
bd522513 8358 const address_space *aspace, CORE_ADDR bp_addr,
09ac7c10 8359 const struct target_waitstatus *ws)
fe798b75 8360{
b4d90040
PA
8361 struct exec_catchpoint *c = (struct exec_catchpoint *) bl->owner;
8362
f90263c1
TT
8363 if (ws->kind != TARGET_WAITKIND_EXECD)
8364 return 0;
8365
8366 c->exec_pathname = xstrdup (ws->value.execd_pathname);
8367 return 1;
fe798b75 8368}
c906108c 8369
fe798b75 8370static enum print_stop_action
348d480f 8371print_it_catch_exec (bpstat bs)
fe798b75 8372{
36dfb11c 8373 struct ui_out *uiout = current_uiout;
348d480f 8374 struct breakpoint *b = bs->breakpoint_at;
b4d90040
PA
8375 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8376
fe798b75 8377 annotate_catchpoint (b->number);
f303dbd6 8378 maybe_print_thread_hit_breakpoint (uiout);
36dfb11c 8379 if (b->disposition == disp_del)
112e8700 8380 uiout->text ("Temporary catchpoint ");
36dfb11c 8381 else
112e8700
SM
8382 uiout->text ("Catchpoint ");
8383 if (uiout->is_mi_like_p ())
36dfb11c 8384 {
112e8700
SM
8385 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_EXEC));
8386 uiout->field_string ("disp", bpdisp_text (b->disposition));
36dfb11c 8387 }
381befee 8388 uiout->field_signed ("bkptno", b->number);
112e8700
SM
8389 uiout->text (" (exec'd ");
8390 uiout->field_string ("new-exec", c->exec_pathname);
8391 uiout->text ("), ");
36dfb11c 8392
fe798b75 8393 return PRINT_SRC_AND_LOC;
c906108c
SS
8394}
8395
fe798b75 8396static void
a6d9a66e 8397print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
fe798b75 8398{
b4d90040 8399 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
fe798b75 8400 struct value_print_options opts;
79a45e25 8401 struct ui_out *uiout = current_uiout;
fe798b75
JB
8402
8403 get_user_print_options (&opts);
8404
8405 /* Field 4, the address, is omitted (which makes the columns
8406 not line up too nicely with the headers, but the effect
8407 is relatively readable). */
8408 if (opts.addressprint)
112e8700 8409 uiout->field_skip ("addr");
fe798b75 8410 annotate_field (5);
112e8700 8411 uiout->text ("exec");
b4d90040 8412 if (c->exec_pathname != NULL)
fe798b75 8413 {
112e8700
SM
8414 uiout->text (", program \"");
8415 uiout->field_string ("what", c->exec_pathname);
8416 uiout->text ("\" ");
fe798b75 8417 }
8ac3646f 8418
112e8700
SM
8419 if (uiout->is_mi_like_p ())
8420 uiout->field_string ("catch-type", "exec");
fe798b75
JB
8421}
8422
8423static void
8424print_mention_catch_exec (struct breakpoint *b)
8425{
8426 printf_filtered (_("Catchpoint %d (exec)"), b->number);
8427}
8428
6149aea9
PA
8429/* Implement the "print_recreate" breakpoint_ops method for exec
8430 catchpoints. */
8431
8432static void
8433print_recreate_catch_exec (struct breakpoint *b, struct ui_file *fp)
8434{
8435 fprintf_unfiltered (fp, "catch exec");
d9b3f62e 8436 print_recreate_thread (b, fp);
6149aea9
PA
8437}
8438
2060206e 8439static struct breakpoint_ops catch_exec_breakpoint_ops;
fe798b75 8440
c906108c 8441static int
fba45db2 8442hw_breakpoint_used_count (void)
c906108c 8443{
c906108c 8444 int i = 0;
f1310107
TJB
8445 struct breakpoint *b;
8446 struct bp_location *bl;
c906108c
SS
8447
8448 ALL_BREAKPOINTS (b)
c5aa993b 8449 {
d6b74ac4 8450 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
f1310107
TJB
8451 for (bl = b->loc; bl; bl = bl->next)
8452 {
8453 /* Special types of hardware breakpoints may use more than
8454 one register. */
348d480f 8455 i += b->ops->resources_needed (bl);
f1310107 8456 }
c5aa993b 8457 }
c906108c
SS
8458
8459 return i;
8460}
8461
a1398e0c
PA
8462/* Returns the resources B would use if it were a hardware
8463 watchpoint. */
8464
c906108c 8465static int
a1398e0c 8466hw_watchpoint_use_count (struct breakpoint *b)
c906108c 8467{
c906108c 8468 int i = 0;
e09342b5 8469 struct bp_location *bl;
c906108c 8470
a1398e0c
PA
8471 if (!breakpoint_enabled (b))
8472 return 0;
8473
8474 for (bl = b->loc; bl; bl = bl->next)
8475 {
8476 /* Special types of hardware watchpoints may use more than
8477 one register. */
8478 i += b->ops->resources_needed (bl);
8479 }
8480
8481 return i;
8482}
8483
8484/* Returns the sum the used resources of all hardware watchpoints of
8485 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
8486 the sum of the used resources of all hardware watchpoints of other
8487 types _not_ TYPE. */
8488
8489static int
8490hw_watchpoint_used_count_others (struct breakpoint *except,
8491 enum bptype type, int *other_type_used)
8492{
8493 int i = 0;
8494 struct breakpoint *b;
8495
c906108c
SS
8496 *other_type_used = 0;
8497 ALL_BREAKPOINTS (b)
e09342b5 8498 {
a1398e0c
PA
8499 if (b == except)
8500 continue;
e09342b5
TJB
8501 if (!breakpoint_enabled (b))
8502 continue;
8503
a1398e0c
PA
8504 if (b->type == type)
8505 i += hw_watchpoint_use_count (b);
8506 else if (is_hardware_watchpoint (b))
8507 *other_type_used = 1;
e09342b5
TJB
8508 }
8509
c906108c
SS
8510 return i;
8511}
8512
c906108c 8513void
fba45db2 8514disable_watchpoints_before_interactive_call_start (void)
c906108c 8515{
c5aa993b 8516 struct breakpoint *b;
c906108c
SS
8517
8518 ALL_BREAKPOINTS (b)
c5aa993b 8519 {
cc60f2e3 8520 if (is_watchpoint (b) && breakpoint_enabled (b))
c5aa993b 8521 {
b5de0fa7 8522 b->enable_state = bp_call_disabled;
44702360 8523 update_global_location_list (UGLL_DONT_INSERT);
c5aa993b
JM
8524 }
8525 }
c906108c
SS
8526}
8527
8528void
fba45db2 8529enable_watchpoints_after_interactive_call_stop (void)
c906108c 8530{
c5aa993b 8531 struct breakpoint *b;
c906108c
SS
8532
8533 ALL_BREAKPOINTS (b)
c5aa993b 8534 {
cc60f2e3 8535 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
c5aa993b 8536 {
b5de0fa7 8537 b->enable_state = bp_enabled;
44702360 8538 update_global_location_list (UGLL_MAY_INSERT);
c5aa993b
JM
8539 }
8540 }
c906108c
SS
8541}
8542
8bea4e01
UW
8543void
8544disable_breakpoints_before_startup (void)
8545{
6c95b8df 8546 current_program_space->executing_startup = 1;
44702360 8547 update_global_location_list (UGLL_DONT_INSERT);
8bea4e01
UW
8548}
8549
8550void
8551enable_breakpoints_after_startup (void)
8552{
6c95b8df 8553 current_program_space->executing_startup = 0;
f8eba3c6 8554 breakpoint_re_set ();
8bea4e01
UW
8555}
8556
7c16b83e
PA
8557/* Create a new single-step breakpoint for thread THREAD, with no
8558 locations. */
c906108c 8559
7c16b83e
PA
8560static struct breakpoint *
8561new_single_step_breakpoint (int thread, struct gdbarch *gdbarch)
8562{
b270e6f9 8563 std::unique_ptr<breakpoint> b (new breakpoint ());
7c16b83e 8564
b270e6f9 8565 init_raw_breakpoint_without_location (b.get (), gdbarch, bp_single_step,
7c16b83e
PA
8566 &momentary_breakpoint_ops);
8567
8568 b->disposition = disp_donttouch;
8569 b->frame_id = null_frame_id;
8570
8571 b->thread = thread;
8572 gdb_assert (b->thread != 0);
8573
b270e6f9 8574 return add_to_breakpoint_chain (std::move (b));
7c16b83e
PA
8575}
8576
8577/* Set a momentary breakpoint of type TYPE at address specified by
8578 SAL. If FRAME_ID is valid, the breakpoint is restricted to that
8579 frame. */
c906108c 8580
454dafbd 8581breakpoint_up
a6d9a66e
UW
8582set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
8583 struct frame_id frame_id, enum bptype type)
c906108c 8584{
52f0bd74 8585 struct breakpoint *b;
edb3359d 8586
193facb3
JK
8587 /* If FRAME_ID is valid, it should be a real frame, not an inlined or
8588 tail-called one. */
8589 gdb_assert (!frame_id_artificial_p (frame_id));
edb3359d 8590
06edf0c0 8591 b = set_raw_breakpoint (gdbarch, sal, type, &momentary_breakpoint_ops);
b5de0fa7
EZ
8592 b->enable_state = bp_enabled;
8593 b->disposition = disp_donttouch;
818dd999 8594 b->frame_id = frame_id;
c906108c 8595
00431a78 8596 b->thread = inferior_thread ()->global_num;
c906108c 8597
44702360 8598 update_global_location_list_nothrow (UGLL_MAY_INSERT);
74960c60 8599
454dafbd 8600 return breakpoint_up (b);
c906108c 8601}
611c83ae 8602
06edf0c0 8603/* Make a momentary breakpoint based on the master breakpoint ORIG.
a1aa2221
LM
8604 The new breakpoint will have type TYPE, use OPS as its
8605 breakpoint_ops, and will set enabled to LOC_ENABLED. */
e58b0e63 8606
06edf0c0
PA
8607static struct breakpoint *
8608momentary_breakpoint_from_master (struct breakpoint *orig,
8609 enum bptype type,
a1aa2221
LM
8610 const struct breakpoint_ops *ops,
8611 int loc_enabled)
e58b0e63
PA
8612{
8613 struct breakpoint *copy;
8614
06edf0c0 8615 copy = set_raw_breakpoint_without_location (orig->gdbarch, type, ops);
e58b0e63 8616 copy->loc = allocate_bp_location (copy);
0ba852ab 8617 set_breakpoint_location_function (copy->loc);
e58b0e63 8618
a6d9a66e 8619 copy->loc->gdbarch = orig->loc->gdbarch;
e58b0e63
PA
8620 copy->loc->requested_address = orig->loc->requested_address;
8621 copy->loc->address = orig->loc->address;
8622 copy->loc->section = orig->loc->section;
6c95b8df 8623 copy->loc->pspace = orig->loc->pspace;
55aa24fb 8624 copy->loc->probe = orig->loc->probe;
f8eba3c6 8625 copy->loc->line_number = orig->loc->line_number;
2f202fde 8626 copy->loc->symtab = orig->loc->symtab;
a1aa2221 8627 copy->loc->enabled = loc_enabled;
e58b0e63
PA
8628 copy->frame_id = orig->frame_id;
8629 copy->thread = orig->thread;
6c95b8df 8630 copy->pspace = orig->pspace;
e58b0e63
PA
8631
8632 copy->enable_state = bp_enabled;
8633 copy->disposition = disp_donttouch;
8634 copy->number = internal_breakpoint_number--;
8635
44702360 8636 update_global_location_list_nothrow (UGLL_DONT_INSERT);
e58b0e63
PA
8637 return copy;
8638}
8639
06edf0c0
PA
8640/* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
8641 ORIG is NULL. */
8642
8643struct breakpoint *
8644clone_momentary_breakpoint (struct breakpoint *orig)
8645{
8646 /* If there's nothing to clone, then return nothing. */
8647 if (orig == NULL)
8648 return NULL;
8649
a1aa2221 8650 return momentary_breakpoint_from_master (orig, orig->type, orig->ops, 0);
06edf0c0
PA
8651}
8652
454dafbd 8653breakpoint_up
a6d9a66e
UW
8654set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
8655 enum bptype type)
611c83ae
PA
8656{
8657 struct symtab_and_line sal;
8658
8659 sal = find_pc_line (pc, 0);
8660 sal.pc = pc;
8661 sal.section = find_pc_overlay (pc);
8662 sal.explicit_pc = 1;
8663
a6d9a66e 8664 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
611c83ae 8665}
c906108c 8666\f
c5aa993b 8667
c906108c
SS
8668/* Tell the user we have just set a breakpoint B. */
8669
8670static void
fba45db2 8671mention (struct breakpoint *b)
c906108c 8672{
348d480f 8673 b->ops->print_mention (b);
2d33446d 8674 current_uiout->text ("\n");
c906108c 8675}
c906108c 8676\f
c5aa993b 8677
5133a315 8678static bool bp_loc_is_permanent (struct bp_location *loc);
1a853c52 8679
7f32a4d5
PA
8680/* Handle "set breakpoint auto-hw on".
8681
8682 If the explicitly specified breakpoint type is not hardware
8683 breakpoint, check the memory map to see whether the breakpoint
8684 address is in read-only memory.
8685
8686 - location type is not hardware breakpoint, memory is read-only.
8687 We change the type of the location to hardware breakpoint.
8688
8689 - location type is hardware breakpoint, memory is read-write. This
8690 means we've previously made the location hardware one, but then the
8691 memory map changed, so we undo.
8692*/
8693
8694static void
8695handle_automatic_hardware_breakpoints (bp_location *bl)
8696{
8697 if (automatic_hardware_breakpoints
8698 && bl->owner->type != bp_hardware_breakpoint
8699 && (bl->loc_type == bp_loc_software_breakpoint
8700 || bl->loc_type == bp_loc_hardware_breakpoint))
8701 {
8702 /* When breakpoints are removed, remove_breakpoints will use
8703 location types we've just set here, the only possible problem
8704 is that memory map has changed during running program, but
8705 it's not going to work anyway with current gdb. */
8706 mem_region *mr = lookup_mem_region (bl->address);
8707
8708 if (mr != nullptr)
8709 {
8710 enum bp_loc_type new_type;
8711
8712 if (mr->attrib.mode != MEM_RW)
8713 new_type = bp_loc_hardware_breakpoint;
8714 else
8715 new_type = bp_loc_software_breakpoint;
8716
8717 if (new_type != bl->loc_type)
8718 {
8719 static bool said = false;
8720
8721 bl->loc_type = new_type;
8722 if (!said)
8723 {
8724 fprintf_filtered (gdb_stdout,
8725 _("Note: automatically using "
8726 "hardware breakpoints for "
8727 "read-only addresses.\n"));
8728 said = true;
8729 }
8730 }
8731 }
8732 }
8733}
8734
0d381245 8735static struct bp_location *
39d61571 8736add_location_to_breakpoint (struct breakpoint *b,
0d381245
VP
8737 const struct symtab_and_line *sal)
8738{
8739 struct bp_location *loc, **tmp;
3742cc8b
YQ
8740 CORE_ADDR adjusted_address;
8741 struct gdbarch *loc_gdbarch = get_sal_arch (*sal);
8742
8743 if (loc_gdbarch == NULL)
8744 loc_gdbarch = b->gdbarch;
8745
8746 /* Adjust the breakpoint's address prior to allocating a location.
8747 Once we call allocate_bp_location(), that mostly uninitialized
8748 location will be placed on the location chain. Adjustment of the
8749 breakpoint may cause target_read_memory() to be called and we do
8750 not want its scan of the location chain to find a breakpoint and
8751 location that's only been partially initialized. */
8752 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
8753 sal->pc, b->type);
0d381245 8754
d30113d4 8755 /* Sort the locations by their ADDRESS. */
39d61571 8756 loc = allocate_bp_location (b);
d30113d4
JK
8757 for (tmp = &(b->loc); *tmp != NULL && (*tmp)->address <= adjusted_address;
8758 tmp = &((*tmp)->next))
0d381245 8759 ;
d30113d4 8760 loc->next = *tmp;
0d381245 8761 *tmp = loc;
3742cc8b 8762
0d381245 8763 loc->requested_address = sal->pc;
3742cc8b 8764 loc->address = adjusted_address;
6c95b8df 8765 loc->pspace = sal->pspace;
935676c9 8766 loc->probe.prob = sal->prob;
729662a5 8767 loc->probe.objfile = sal->objfile;
6c95b8df 8768 gdb_assert (loc->pspace != NULL);
0d381245 8769 loc->section = sal->section;
3742cc8b 8770 loc->gdbarch = loc_gdbarch;
f8eba3c6 8771 loc->line_number = sal->line;
2f202fde 8772 loc->symtab = sal->symtab;
4a27f119 8773 loc->symbol = sal->symbol;
3467ec66
PA
8774 loc->msymbol = sal->msymbol;
8775 loc->objfile = sal->objfile;
f8eba3c6 8776
0ba852ab 8777 set_breakpoint_location_function (loc);
1a853c52 8778
6ae88661
LM
8779 /* While by definition, permanent breakpoints are already present in the
8780 code, we don't mark the location as inserted. Normally one would expect
8781 that GDB could rely on that breakpoint instruction to stop the program,
8782 thus removing the need to insert its own breakpoint, except that executing
8783 the breakpoint instruction can kill the target instead of reporting a
8784 SIGTRAP. E.g., on SPARC, when interrupts are disabled, executing the
8785 instruction resets the CPU, so QEMU 2.0.0 for SPARC correspondingly dies
8786 with "Trap 0x02 while interrupts disabled, Error state". Letting the
8787 breakpoint be inserted normally results in QEMU knowing about the GDB
8788 breakpoint, and thus trap before the breakpoint instruction is executed.
8789 (If GDB later needs to continue execution past the permanent breakpoint,
8790 it manually increments the PC, thus avoiding executing the breakpoint
8791 instruction.) */
1a853c52 8792 if (bp_loc_is_permanent (loc))
6ae88661 8793 loc->permanent = 1;
1a853c52 8794
0d381245
VP
8795 return loc;
8796}
514f746b
AR
8797\f
8798
5133a315
LM
8799/* Return true if LOC is pointing to a permanent breakpoint,
8800 return false otherwise. */
1cf4d951 8801
5133a315 8802static bool
1cf4d951
PA
8803bp_loc_is_permanent (struct bp_location *loc)
8804{
514f746b
AR
8805 gdb_assert (loc != NULL);
8806
cb1e4e32
PA
8807 /* If we have a non-breakpoint-backed catchpoint or a software
8808 watchpoint, just return 0. We should not attempt to read from
8809 the addresses the locations of these breakpoint types point to.
5133a315 8810 gdbarch_program_breakpoint_here_p, below, will attempt to read
244558af 8811 memory. */
cb1e4e32 8812 if (!bl_address_is_meaningful (loc))
5133a315 8813 return false;
244558af 8814
5ed8105e 8815 scoped_restore_current_pspace_and_thread restore_pspace_thread;
6c95b8df 8816 switch_to_program_space_and_thread (loc->pspace);
5133a315 8817 return gdbarch_program_breakpoint_here_p (loc->gdbarch, loc->address);
514f746b
AR
8818}
8819
e7e0cddf
SS
8820/* Build a command list for the dprintf corresponding to the current
8821 settings of the dprintf style options. */
8822
8823static void
8824update_dprintf_command_list (struct breakpoint *b)
8825{
8826 char *dprintf_args = b->extra_string;
8827 char *printf_line = NULL;
8828
8829 if (!dprintf_args)
8830 return;
8831
8832 dprintf_args = skip_spaces (dprintf_args);
8833
8834 /* Allow a comma, as it may have terminated a location, but don't
8835 insist on it. */
8836 if (*dprintf_args == ',')
8837 ++dprintf_args;
8838 dprintf_args = skip_spaces (dprintf_args);
8839
8840 if (*dprintf_args != '"')
8841 error (_("Bad format string, missing '\"'."));
8842
d3ce09f5 8843 if (strcmp (dprintf_style, dprintf_style_gdb) == 0)
e7e0cddf 8844 printf_line = xstrprintf ("printf %s", dprintf_args);
d3ce09f5 8845 else if (strcmp (dprintf_style, dprintf_style_call) == 0)
e7e0cddf
SS
8846 {
8847 if (!dprintf_function)
8848 error (_("No function supplied for dprintf call"));
8849
8850 if (dprintf_channel && strlen (dprintf_channel) > 0)
8851 printf_line = xstrprintf ("call (void) %s (%s,%s)",
8852 dprintf_function,
8853 dprintf_channel,
8854 dprintf_args);
8855 else
8856 printf_line = xstrprintf ("call (void) %s (%s)",
8857 dprintf_function,
8858 dprintf_args);
8859 }
d3ce09f5
SS
8860 else if (strcmp (dprintf_style, dprintf_style_agent) == 0)
8861 {
8862 if (target_can_run_breakpoint_commands ())
8863 printf_line = xstrprintf ("agent-printf %s", dprintf_args);
8864 else
8865 {
8866 warning (_("Target cannot run dprintf commands, falling back to GDB printf"));
8867 printf_line = xstrprintf ("printf %s", dprintf_args);
8868 }
8869 }
e7e0cddf
SS
8870 else
8871 internal_error (__FILE__, __LINE__,
8872 _("Invalid dprintf style."));
8873
f28045c2 8874 gdb_assert (printf_line != NULL);
e7e0cddf 8875
12973681
TT
8876 /* Manufacture a printf sequence. */
8877 struct command_line *printf_cmd_line
8878 = new struct command_line (simple_control, printf_line);
8879 breakpoint_set_commands (b, counted_command_line (printf_cmd_line,
8880 command_lines_deleter ()));
e7e0cddf
SS
8881}
8882
8883/* Update all dprintf commands, making their command lists reflect
8884 current style settings. */
8885
8886static void
eb4c3f4a 8887update_dprintf_commands (const char *args, int from_tty,
e7e0cddf
SS
8888 struct cmd_list_element *c)
8889{
8890 struct breakpoint *b;
8891
8892 ALL_BREAKPOINTS (b)
8893 {
8894 if (b->type == bp_dprintf)
8895 update_dprintf_command_list (b);
8896 }
8897}
c3f6f71d 8898
f00aae0f
KS
8899/* Create a breakpoint with SAL as location. Use LOCATION
8900 as a description of the location, and COND_STRING
b35a8b2f
DE
8901 as condition expression. If LOCATION is NULL then create an
8902 "address location" from the address in the SAL. */
018d34a4
VP
8903
8904static void
d9b3f62e 8905init_breakpoint_sal (struct breakpoint *b, struct gdbarch *gdbarch,
6c5b2ebe 8906 gdb::array_view<const symtab_and_line> sals,
ffc2605c 8907 event_location_up &&location,
e1e01040
PA
8908 gdb::unique_xmalloc_ptr<char> filter,
8909 gdb::unique_xmalloc_ptr<char> cond_string,
8910 gdb::unique_xmalloc_ptr<char> extra_string,
d9b3f62e
PA
8911 enum bptype type, enum bpdisp disposition,
8912 int thread, int task, int ignore_count,
c0a91b2b 8913 const struct breakpoint_ops *ops, int from_tty,
44f238bb
PA
8914 int enabled, int internal, unsigned flags,
8915 int display_canonical)
018d34a4 8916{
0d381245 8917 int i;
018d34a4
VP
8918
8919 if (type == bp_hardware_breakpoint)
8920 {
fbbd034e
AS
8921 int target_resources_ok;
8922
8923 i = hw_breakpoint_used_count ();
8924 target_resources_ok =
8925 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
018d34a4
VP
8926 i + 1, 0);
8927 if (target_resources_ok == 0)
8928 error (_("No hardware breakpoint support in the target."));
8929 else if (target_resources_ok < 0)
8930 error (_("Hardware breakpoints used exceeds limit."));
8931 }
8932
6c5b2ebe 8933 gdb_assert (!sals.empty ());
6c95b8df 8934
6c5b2ebe 8935 for (const auto &sal : sals)
0d381245 8936 {
0d381245
VP
8937 struct bp_location *loc;
8938
8939 if (from_tty)
5af949e3
UW
8940 {
8941 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
8942 if (!loc_gdbarch)
8943 loc_gdbarch = gdbarch;
8944
8945 describe_other_breakpoints (loc_gdbarch,
6c95b8df 8946 sal.pspace, sal.pc, sal.section, thread);
5af949e3 8947 }
0d381245 8948
6c5b2ebe 8949 if (&sal == &sals[0])
0d381245 8950 {
d9b3f62e 8951 init_raw_breakpoint (b, gdbarch, sal, type, ops);
0d381245 8952 b->thread = thread;
4a306c9a 8953 b->task = task;
855a6e68 8954
e1e01040
PA
8955 b->cond_string = cond_string.release ();
8956 b->extra_string = extra_string.release ();
0d381245 8957 b->ignore_count = ignore_count;
41447f92 8958 b->enable_state = enabled ? bp_enabled : bp_disabled;
0d381245 8959 b->disposition = disposition;
6c95b8df 8960
44f238bb
PA
8961 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
8962 b->loc->inserted = 1;
8963
0fb4aa4b
PA
8964 if (type == bp_static_tracepoint)
8965 {
d9b3f62e 8966 struct tracepoint *t = (struct tracepoint *) b;
0fb4aa4b
PA
8967 struct static_tracepoint_marker marker;
8968
983af33b 8969 if (strace_marker_p (b))
0fb4aa4b
PA
8970 {
8971 /* We already know the marker exists, otherwise, we
8972 wouldn't see a sal for it. */
d28cd78a
TT
8973 const char *p
8974 = &event_location_to_string (b->location.get ())[3];
f00aae0f 8975 const char *endp;
0fb4aa4b 8976
f1735a53 8977 p = skip_spaces (p);
0fb4aa4b 8978
f1735a53 8979 endp = skip_to_space (p);
0fb4aa4b 8980
5d9310c4 8981 t->static_trace_marker_id.assign (p, endp - p);
0fb4aa4b 8982
3e43a32a
MS
8983 printf_filtered (_("Probed static tracepoint "
8984 "marker \"%s\"\n"),
5d9310c4 8985 t->static_trace_marker_id.c_str ());
0fb4aa4b
PA
8986 }
8987 else if (target_static_tracepoint_marker_at (sal.pc, &marker))
8988 {
5d9310c4 8989 t->static_trace_marker_id = std::move (marker.str_id);
0fb4aa4b 8990
3e43a32a
MS
8991 printf_filtered (_("Probed static tracepoint "
8992 "marker \"%s\"\n"),
5d9310c4 8993 t->static_trace_marker_id.c_str ());
0fb4aa4b
PA
8994 }
8995 else
3e43a32a
MS
8996 warning (_("Couldn't determine the static "
8997 "tracepoint marker to probe"));
0fb4aa4b
PA
8998 }
8999
0d381245
VP
9000 loc = b->loc;
9001 }
9002 else
018d34a4 9003 {
39d61571 9004 loc = add_location_to_breakpoint (b, &sal);
44f238bb
PA
9005 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9006 loc->inserted = 1;
0d381245
VP
9007 }
9008
b5fa468f
TBA
9009 /* Do not set breakpoint locations conditions yet. As locations
9010 are inserted, they get sorted based on their addresses. Let
9011 the list stabilize to have reliable location numbers. */
e7e0cddf
SS
9012
9013 /* Dynamic printf requires and uses additional arguments on the
9014 command line, otherwise it's an error. */
9015 if (type == bp_dprintf)
9016 {
9017 if (b->extra_string)
9018 update_dprintf_command_list (b);
9019 else
9020 error (_("Format string required"));
9021 }
9022 else if (b->extra_string)
588ae58c 9023 error (_("Garbage '%s' at end of command"), b->extra_string);
855a6e68 9024 }
018d34a4 9025
b5fa468f
TBA
9026
9027 /* The order of the locations is now stable. Set the location
9028 condition using the location's number. */
9029 int loc_num = 1;
9030 for (bp_location *loc = b->loc; loc != nullptr; loc = loc->next)
9031 {
9032 if (b->cond_string != nullptr)
9033 set_breakpoint_location_condition (b->cond_string, loc, b->number,
9034 loc_num);
9035
9036 ++loc_num;
9037 }
9038
56435ebe 9039 b->display_canonical = display_canonical;
f00aae0f 9040 if (location != NULL)
d28cd78a 9041 b->location = std::move (location);
018d34a4 9042 else
d28cd78a 9043 b->location = new_address_location (b->loc->address, NULL, 0);
c0e8dcd8 9044 b->filter = std::move (filter);
d9b3f62e 9045}
018d34a4 9046
d9b3f62e
PA
9047static void
9048create_breakpoint_sal (struct gdbarch *gdbarch,
6c5b2ebe 9049 gdb::array_view<const symtab_and_line> sals,
ffc2605c 9050 event_location_up &&location,
e1e01040
PA
9051 gdb::unique_xmalloc_ptr<char> filter,
9052 gdb::unique_xmalloc_ptr<char> cond_string,
9053 gdb::unique_xmalloc_ptr<char> extra_string,
d9b3f62e
PA
9054 enum bptype type, enum bpdisp disposition,
9055 int thread, int task, int ignore_count,
c0a91b2b 9056 const struct breakpoint_ops *ops, int from_tty,
44f238bb
PA
9057 int enabled, int internal, unsigned flags,
9058 int display_canonical)
d9b3f62e 9059{
a5e364af 9060 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (type);
d9b3f62e 9061
a5e364af 9062 init_breakpoint_sal (b.get (), gdbarch,
ffc2605c 9063 sals, std::move (location),
e1e01040
PA
9064 std::move (filter),
9065 std::move (cond_string),
9066 std::move (extra_string),
d9b3f62e
PA
9067 type, disposition,
9068 thread, task, ignore_count,
9069 ops, from_tty,
44f238bb
PA
9070 enabled, internal, flags,
9071 display_canonical);
d9b3f62e 9072
b270e6f9 9073 install_breakpoint (internal, std::move (b), 0);
018d34a4
VP
9074}
9075
9076/* Add SALS.nelts breakpoints to the breakpoint table. For each
9077 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
9078 value. COND_STRING, if not NULL, specified the condition to be
9079 used for all breakpoints. Essentially the only case where
9080 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
9081 function. In that case, it's still not possible to specify
9082 separate conditions for different overloaded functions, so
9083 we take just a single condition string.
9084
c3f6f71d 9085 NOTE: If the function succeeds, the caller is expected to cleanup
018d34a4 9086 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
c3f6f71d
JM
9087 array contents). If the function fails (error() is called), the
9088 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
4a64f543 9089 COND and SALS arrays and each of those arrays contents. */
c906108c
SS
9090
9091static void
8cdf0e15 9092create_breakpoints_sal (struct gdbarch *gdbarch,
7efd8fc2 9093 struct linespec_result *canonical,
e1e01040
PA
9094 gdb::unique_xmalloc_ptr<char> cond_string,
9095 gdb::unique_xmalloc_ptr<char> extra_string,
8cdf0e15
VP
9096 enum bptype type, enum bpdisp disposition,
9097 int thread, int task, int ignore_count,
c0a91b2b 9098 const struct breakpoint_ops *ops, int from_tty,
44f238bb 9099 int enabled, int internal, unsigned flags)
c906108c 9100{
f8eba3c6 9101 if (canonical->pre_expanded)
6c5b2ebe 9102 gdb_assert (canonical->lsals.size () == 1);
f8eba3c6 9103
6c5b2ebe 9104 for (const auto &lsal : canonical->lsals)
c3f6f71d 9105 {
f00aae0f 9106 /* Note that 'location' can be NULL in the case of a plain
f8eba3c6 9107 'break', without arguments. */
ffc2605c 9108 event_location_up location
f00aae0f 9109 = (canonical->location != NULL
8e9e35b1 9110 ? copy_event_location (canonical->location.get ()) : NULL);
e1e01040 9111 gdb::unique_xmalloc_ptr<char> filter_string
6c5b2ebe 9112 (lsal.canonical != NULL ? xstrdup (lsal.canonical) : NULL);
0d381245 9113
6c5b2ebe 9114 create_breakpoint_sal (gdbarch, lsal.sals,
ffc2605c 9115 std::move (location),
e1e01040
PA
9116 std::move (filter_string),
9117 std::move (cond_string),
9118 std::move (extra_string),
e7e0cddf 9119 type, disposition,
84f4c1fe 9120 thread, task, ignore_count, ops,
44f238bb 9121 from_tty, enabled, internal, flags,
56435ebe 9122 canonical->special_display);
c3f6f71d 9123 }
c3f6f71d 9124}
c906108c 9125
f00aae0f 9126/* Parse LOCATION which is assumed to be a SAL specification possibly
c3f6f71d 9127 followed by conditionals. On return, SALS contains an array of SAL
f00aae0f
KS
9128 addresses found. LOCATION points to the end of the SAL (for
9129 linespec locations).
9998af43
TJB
9130
9131 The array and the line spec strings are allocated on the heap, it is
9132 the caller's responsibility to free them. */
c906108c 9133
b9362cc7 9134static void
626d2320 9135parse_breakpoint_sals (struct event_location *location,
58438ac1 9136 struct linespec_result *canonical)
c3f6f71d 9137{
f00aae0f
KS
9138 struct symtab_and_line cursal;
9139
9140 if (event_location_type (location) == LINESPEC_LOCATION)
9141 {
a20714ff 9142 const char *spec = get_linespec_location (location)->spec_string;
f00aae0f 9143
a20714ff 9144 if (spec == NULL)
f00aae0f
KS
9145 {
9146 /* The last displayed codepoint, if it's valid, is our default
9147 breakpoint address. */
9148 if (last_displayed_sal_is_valid ())
9149 {
f00aae0f
KS
9150 /* Set sal's pspace, pc, symtab, and line to the values
9151 corresponding to the last call to print_frame_info.
9152 Be sure to reinitialize LINE with NOTCURRENT == 0
9153 as the breakpoint line number is inappropriate otherwise.
9154 find_pc_line would adjust PC, re-set it back. */
51abb421
PA
9155 symtab_and_line sal = get_last_displayed_sal ();
9156 CORE_ADDR pc = sal.pc;
9157
f00aae0f
KS
9158 sal = find_pc_line (pc, 0);
9159
9160 /* "break" without arguments is equivalent to "break *PC"
9161 where PC is the last displayed codepoint's address. So
9162 make sure to set sal.explicit_pc to prevent GDB from
9163 trying to expand the list of sals to include all other
9164 instances with the same symtab and line. */
9165 sal.pc = pc;
9166 sal.explicit_pc = 1;
9167
6c5b2ebe
PA
9168 struct linespec_sals lsal;
9169 lsal.sals = {sal};
f00aae0f
KS
9170 lsal.canonical = NULL;
9171
6c5b2ebe 9172 canonical->lsals.push_back (std::move (lsal));
f00aae0f
KS
9173 return;
9174 }
9175 else
9176 error (_("No default breakpoint address now."));
c906108c 9177 }
c906108c 9178 }
f00aae0f
KS
9179
9180 /* Force almost all breakpoints to be in terms of the
9181 current_source_symtab (which is decode_line_1's default).
9182 This should produce the results we want almost all of the
9183 time while leaving default_breakpoint_* alone.
9184
9185 ObjC: However, don't match an Objective-C method name which
9186 may have a '+' or '-' succeeded by a '['. */
9187 cursal = get_current_source_symtab_and_line ();
9188 if (last_displayed_sal_is_valid ())
c906108c 9189 {
a20714ff 9190 const char *spec = NULL;
cc80f267 9191
f00aae0f 9192 if (event_location_type (location) == LINESPEC_LOCATION)
a20714ff 9193 spec = get_linespec_location (location)->spec_string;
cc80f267 9194
f00aae0f 9195 if (!cursal.symtab
a20714ff
PA
9196 || (spec != NULL
9197 && strchr ("+-", spec[0]) != NULL
9198 && spec[1] != '['))
f00aae0f 9199 {
c2f4122d 9200 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
f00aae0f
KS
9201 get_last_displayed_symtab (),
9202 get_last_displayed_line (),
9203 canonical, NULL, NULL);
9204 return;
9205 }
c906108c 9206 }
f00aae0f 9207
c2f4122d 9208 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
f00aae0f 9209 cursal.symtab, cursal.line, canonical, NULL, NULL);
c3f6f71d 9210}
c906108c 9211
c906108c 9212
c3f6f71d 9213/* Convert each SAL into a real PC. Verify that the PC can be
4a64f543 9214 inserted as a breakpoint. If it can't throw an error. */
c906108c 9215
b9362cc7 9216static void
6c5b2ebe 9217breakpoint_sals_to_pc (std::vector<symtab_and_line> &sals)
c3f6f71d 9218{
6c5b2ebe
PA
9219 for (auto &sal : sals)
9220 resolve_sal_pc (&sal);
c3f6f71d
JM
9221}
9222
7a697b8d
SS
9223/* Fast tracepoints may have restrictions on valid locations. For
9224 instance, a fast tracepoint using a jump instead of a trap will
9225 likely have to overwrite more bytes than a trap would, and so can
9226 only be placed where the instruction is longer than the jump, or a
9227 multi-instruction sequence does not have a jump into the middle of
9228 it, etc. */
9229
9230static void
9231check_fast_tracepoint_sals (struct gdbarch *gdbarch,
6c5b2ebe 9232 gdb::array_view<const symtab_and_line> sals)
7a697b8d 9233{
6c5b2ebe 9234 for (const auto &sal : sals)
7a697b8d 9235 {
f8eba3c6
TT
9236 struct gdbarch *sarch;
9237
6c5b2ebe 9238 sarch = get_sal_arch (sal);
f8eba3c6
TT
9239 /* We fall back to GDBARCH if there is no architecture
9240 associated with SAL. */
9241 if (sarch == NULL)
9242 sarch = gdbarch;
281d762b
TT
9243 std::string msg;
9244 if (!gdbarch_fast_tracepoint_valid_at (sarch, sal.pc, &msg))
53c3572a 9245 error (_("May not have a fast tracepoint at %s%s"),
281d762b 9246 paddress (sarch, sal.pc), msg.c_str ());
7a697b8d
SS
9247 }
9248}
9249
018d34a4
VP
9250/* Given TOK, a string specification of condition and thread, as
9251 accepted by the 'break' command, extract the condition
9252 string and thread number and set *COND_STRING and *THREAD.
4a64f543 9253 PC identifies the context at which the condition should be parsed.
018d34a4
VP
9254 If no condition is found, *COND_STRING is set to NULL.
9255 If no thread is found, *THREAD is set to -1. */
d634f2de
JB
9256
9257static void
bbc13ae3 9258find_condition_and_thread (const char *tok, CORE_ADDR pc,
e7e0cddf
SS
9259 char **cond_string, int *thread, int *task,
9260 char **rest)
018d34a4
VP
9261{
9262 *cond_string = NULL;
9263 *thread = -1;
ed1d1739
KS
9264 *task = 0;
9265 *rest = NULL;
733d554a 9266 bool force = false;
ed1d1739 9267
018d34a4
VP
9268 while (tok && *tok)
9269 {
bbc13ae3 9270 const char *end_tok;
018d34a4 9271 int toklen;
bbc13ae3
KS
9272 const char *cond_start = NULL;
9273 const char *cond_end = NULL;
cc59ec59 9274
f1735a53 9275 tok = skip_spaces (tok);
e7e0cddf
SS
9276
9277 if ((*tok == '"' || *tok == ',') && rest)
9278 {
9279 *rest = savestring (tok, strlen (tok));
9280 return;
9281 }
9282
f1735a53 9283 end_tok = skip_to_space (tok);
d634f2de 9284
018d34a4 9285 toklen = end_tok - tok;
d634f2de 9286
018d34a4
VP
9287 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
9288 {
9289 tok = cond_start = end_tok + 1;
733d554a
TBA
9290 try
9291 {
9292 parse_exp_1 (&tok, pc, block_for_pc (pc), 0);
9293 }
9294 catch (const gdb_exception_error &)
9295 {
9296 if (!force)
9297 throw;
9298 else
9299 tok = tok + strlen (tok);
9300 }
018d34a4 9301 cond_end = tok;
d634f2de 9302 *cond_string = savestring (cond_start, cond_end - cond_start);
018d34a4 9303 }
733d554a
TBA
9304 else if (toklen >= 1 && strncmp (tok, "-force-condition", toklen) == 0)
9305 {
5759831a 9306 tok = tok + toklen;
733d554a
TBA
9307 force = true;
9308 }
018d34a4
VP
9309 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
9310 {
5d5658a1
PA
9311 const char *tmptok;
9312 struct thread_info *thr;
d634f2de 9313
018d34a4 9314 tok = end_tok + 1;
5d5658a1 9315 thr = parse_thread_id (tok, &tmptok);
018d34a4
VP
9316 if (tok == tmptok)
9317 error (_("Junk after thread keyword."));
5d5658a1 9318 *thread = thr->global_num;
bbc13ae3 9319 tok = tmptok;
018d34a4 9320 }
4a306c9a
JB
9321 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
9322 {
9323 char *tmptok;
9324
9325 tok = end_tok + 1;
bbc13ae3 9326 *task = strtol (tok, &tmptok, 0);
4a306c9a
JB
9327 if (tok == tmptok)
9328 error (_("Junk after task keyword."));
9329 if (!valid_task_id (*task))
b6199126 9330 error (_("Unknown task %d."), *task);
bbc13ae3 9331 tok = tmptok;
4a306c9a 9332 }
e7e0cddf
SS
9333 else if (rest)
9334 {
9335 *rest = savestring (tok, strlen (tok));
ccab2054 9336 return;
e7e0cddf 9337 }
018d34a4
VP
9338 else
9339 error (_("Junk at end of arguments."));
9340 }
9341}
9342
b5fa468f
TBA
9343/* Call 'find_condition_and_thread' for each sal in SALS until a parse
9344 succeeds. The parsed values are written to COND_STRING, THREAD,
9345 TASK, and REST. See the comment of 'find_condition_and_thread'
9346 for the description of these parameters and INPUT. */
9347
9348static void
9349find_condition_and_thread_for_sals (const std::vector<symtab_and_line> &sals,
9350 const char *input, char **cond_string,
9351 int *thread, int *task, char **rest)
9352{
9353 int num_failures = 0;
9354 for (auto &sal : sals)
9355 {
9356 char *cond = nullptr;
9357 int thread_id = 0;
9358 int task_id = 0;
9359 char *remaining = nullptr;
9360
9361 /* Here we want to parse 'arg' to separate condition from thread
9362 number. But because parsing happens in a context and the
9363 contexts of sals might be different, try each until there is
9364 success. Finding one successful parse is sufficient for our
9365 goal. When setting the breakpoint we'll re-parse the
9366 condition in the context of each sal. */
9367 try
9368 {
9369 find_condition_and_thread (input, sal.pc, &cond, &thread_id,
9370 &task_id, &remaining);
9371 *cond_string = cond;
9372 *thread = thread_id;
9373 *task = task_id;
9374 *rest = remaining;
9375 break;
9376 }
9377 catch (const gdb_exception_error &e)
9378 {
9379 num_failures++;
9380 /* If no sal remains, do not continue. */
9381 if (num_failures == sals.size ())
9382 throw;
9383 }
9384 }
9385}
9386
0fb4aa4b
PA
9387/* Decode a static tracepoint marker spec. */
9388
6c5b2ebe 9389static std::vector<symtab_and_line>
f00aae0f 9390decode_static_tracepoint_spec (const char **arg_p)
0fb4aa4b 9391{
f00aae0f
KS
9392 const char *p = &(*arg_p)[3];
9393 const char *endp;
0fb4aa4b 9394
f1735a53 9395 p = skip_spaces (p);
0fb4aa4b 9396
f1735a53 9397 endp = skip_to_space (p);
0fb4aa4b 9398
81b1e71c 9399 std::string marker_str (p, endp - p);
0fb4aa4b 9400
5d9310c4
SM
9401 std::vector<static_tracepoint_marker> markers
9402 = target_static_tracepoint_markers_by_strid (marker_str.c_str ());
9403 if (markers.empty ())
81b1e71c
TT
9404 error (_("No known static tracepoint marker named %s"),
9405 marker_str.c_str ());
0fb4aa4b 9406
6c5b2ebe 9407 std::vector<symtab_and_line> sals;
5d9310c4 9408 sals.reserve (markers.size ());
0fb4aa4b 9409
5d9310c4 9410 for (const static_tracepoint_marker &marker : markers)
0fb4aa4b 9411 {
5d9310c4
SM
9412 symtab_and_line sal = find_pc_line (marker.address, 0);
9413 sal.pc = marker.address;
6c5b2ebe 9414 sals.push_back (sal);
5d9310c4 9415 }
0fb4aa4b 9416
0fb4aa4b
PA
9417 *arg_p = endp;
9418 return sals;
9419}
9420
bac7c5cf
GB
9421/* Returns the breakpoint ops appropriate for use with with LOCATION_TYPE and
9422 according to IS_TRACEPOINT. */
9423
9424static const struct breakpoint_ops *
9425breakpoint_ops_for_event_location_type (enum event_location_type location_type,
9426 bool is_tracepoint)
9427{
9428 if (is_tracepoint)
9429 {
9430 if (location_type == PROBE_LOCATION)
9431 return &tracepoint_probe_breakpoint_ops;
9432 else
9433 return &tracepoint_breakpoint_ops;
9434 }
9435 else
9436 {
9437 if (location_type == PROBE_LOCATION)
9438 return &bkpt_probe_breakpoint_ops;
9439 else
9440 return &bkpt_breakpoint_ops;
9441 }
9442}
9443
9444/* See breakpoint.h. */
9445
9446const struct breakpoint_ops *
9447breakpoint_ops_for_event_location (const struct event_location *location,
9448 bool is_tracepoint)
9449{
9450 if (location != nullptr)
9451 return breakpoint_ops_for_event_location_type
9452 (event_location_type (location), is_tracepoint);
9453 return is_tracepoint ? &tracepoint_breakpoint_ops : &bkpt_breakpoint_ops;
9454}
9455
f00aae0f 9456/* See breakpoint.h. */
0101ce28 9457
8cdf0e15
VP
9458int
9459create_breakpoint (struct gdbarch *gdbarch,
626d2320 9460 struct event_location *location,
e1e01040
PA
9461 const char *cond_string,
9462 int thread, const char *extra_string,
10a636cc 9463 bool force_condition, int parse_extra,
0fb4aa4b 9464 int tempflag, enum bptype type_wanted,
8cdf0e15
VP
9465 int ignore_count,
9466 enum auto_boolean pending_break_support,
c0a91b2b 9467 const struct breakpoint_ops *ops,
44f238bb
PA
9468 int from_tty, int enabled, int internal,
9469 unsigned flags)
c3f6f71d 9470{
7efd8fc2 9471 struct linespec_result canonical;
0101ce28 9472 int pending = 0;
4a306c9a 9473 int task = 0;
86b17b60 9474 int prev_bkpt_count = breakpoint_count;
c3f6f71d 9475
348d480f
PA
9476 gdb_assert (ops != NULL);
9477
f00aae0f
KS
9478 /* If extra_string isn't useful, set it to NULL. */
9479 if (extra_string != NULL && *extra_string == '\0')
9480 extra_string = NULL;
9481
a70b8144 9482 try
b78a6381 9483 {
f00aae0f 9484 ops->create_sals_from_location (location, &canonical, type_wanted);
b78a6381 9485 }
230d2906 9486 catch (const gdb_exception_error &e)
0101ce28 9487 {
492d29ea
PA
9488 /* If caller is interested in rc value from parse, set
9489 value. */
9490 if (e.error == NOT_FOUND_ERROR)
0101ce28 9491 {
05ff989b
AC
9492 /* If pending breakpoint support is turned off, throw
9493 error. */
fa8d40ab
JJ
9494
9495 if (pending_break_support == AUTO_BOOLEAN_FALSE)
eedc3f4f 9496 throw;
723a2275
VP
9497
9498 exception_print (gdb_stderr, e);
fa8d40ab 9499
dda83cd7 9500 /* If pending breakpoint support is auto query and the user
05ff989b 9501 selects no, then simply return the error code. */
059fb39f 9502 if (pending_break_support == AUTO_BOOLEAN_AUTO
bfccc43c
YQ
9503 && !nquery (_("Make %s pending on future shared library load? "),
9504 bptype_string (type_wanted)))
fd9b8c24 9505 return 0;
fa8d40ab 9506
05ff989b
AC
9507 /* At this point, either the user was queried about setting
9508 a pending breakpoint and selected yes, or pending
9509 breakpoint behavior is on and thus a pending breakpoint
9510 is defaulted on behalf of the user. */
f00aae0f 9511 pending = 1;
0101ce28 9512 }
492d29ea 9513 else
eedc3f4f 9514 throw;
0101ce28 9515 }
492d29ea 9516
6c5b2ebe 9517 if (!pending && canonical.lsals.empty ())
492d29ea 9518 return 0;
c3f6f71d 9519
c3f6f71d
JM
9520 /* Resolve all line numbers to PC's and verify that the addresses
9521 are ok for the target. */
0101ce28 9522 if (!pending)
f8eba3c6 9523 {
6c5b2ebe
PA
9524 for (auto &lsal : canonical.lsals)
9525 breakpoint_sals_to_pc (lsal.sals);
f8eba3c6 9526 }
c3f6f71d 9527
7a697b8d 9528 /* Fast tracepoints may have additional restrictions on location. */
bfccc43c 9529 if (!pending && type_wanted == bp_fast_tracepoint)
f8eba3c6 9530 {
6c5b2ebe
PA
9531 for (const auto &lsal : canonical.lsals)
9532 check_fast_tracepoint_sals (gdbarch, lsal.sals);
f8eba3c6 9533 }
7a697b8d 9534
c3f6f71d
JM
9535 /* Verify that condition can be parsed, before setting any
9536 breakpoints. Allocate a separate condition expression for each
4a64f543 9537 breakpoint. */
0101ce28 9538 if (!pending)
c3f6f71d 9539 {
e1e01040
PA
9540 gdb::unique_xmalloc_ptr<char> cond_string_copy;
9541 gdb::unique_xmalloc_ptr<char> extra_string_copy;
9542
f00aae0f 9543 if (parse_extra)
dda83cd7 9544 {
0878d0fa 9545 char *rest;
e1e01040 9546 char *cond;
52d361e1 9547
6c5b2ebe 9548 const linespec_sals &lsal = canonical.lsals[0];
52d361e1 9549
b5fa468f
TBA
9550 find_condition_and_thread_for_sals (lsal.sals, extra_string,
9551 &cond, &thread, &task, &rest);
e1e01040
PA
9552 cond_string_copy.reset (cond);
9553 extra_string_copy.reset (rest);
dda83cd7 9554 }
2f069f6f 9555 else
dda83cd7 9556 {
f00aae0f
KS
9557 if (type_wanted != bp_dprintf
9558 && extra_string != NULL && *extra_string != '\0')
9559 error (_("Garbage '%s' at end of location"), extra_string);
0878d0fa 9560
10a636cc
TBA
9561 /* Check the validity of the condition. We should error out
9562 if the condition is invalid at all of the locations and
9563 if it is not forced. In the PARSE_EXTRA case above, this
9564 check is done when parsing the EXTRA_STRING. */
9565 if (cond_string != nullptr && !force_condition)
9566 {
9567 int num_failures = 0;
9568 const linespec_sals &lsal = canonical.lsals[0];
9569 for (const auto &sal : lsal.sals)
9570 {
9571 const char *cond = cond_string;
9572 try
9573 {
9574 parse_exp_1 (&cond, sal.pc, block_for_pc (sal.pc), 0);
9575 /* One success is sufficient to keep going. */
9576 break;
9577 }
9578 catch (const gdb_exception_error &)
9579 {
9580 num_failures++;
9581 /* If this is the last sal, error out. */
9582 if (num_failures == lsal.sals.size ())
9583 throw;
9584 }
9585 }
9586 }
9587
0878d0fa
YQ
9588 /* Create a private copy of condition string. */
9589 if (cond_string)
e1e01040 9590 cond_string_copy.reset (xstrdup (cond_string));
0878d0fa
YQ
9591 /* Create a private copy of any extra string. */
9592 if (extra_string)
e1e01040 9593 extra_string_copy.reset (xstrdup (extra_string));
dda83cd7 9594 }
0fb4aa4b 9595
52d361e1 9596 ops->create_breakpoints_sal (gdbarch, &canonical,
e1e01040
PA
9597 std::move (cond_string_copy),
9598 std::move (extra_string_copy),
9599 type_wanted,
d9b3f62e
PA
9600 tempflag ? disp_del : disp_donttouch,
9601 thread, task, ignore_count, ops,
44f238bb 9602 from_tty, enabled, internal, flags);
c906108c 9603 }
0101ce28
JJ
9604 else
9605 {
a5e364af 9606 std::unique_ptr <breakpoint> b = new_breakpoint_from_type (type_wanted);
bfccc43c 9607
a5e364af 9608 init_raw_breakpoint_without_location (b.get (), gdbarch, type_wanted, ops);
d28cd78a 9609 b->location = copy_event_location (location);
bfccc43c 9610
f00aae0f
KS
9611 if (parse_extra)
9612 b->cond_string = NULL;
e12c7713
MK
9613 else
9614 {
9615 /* Create a private copy of condition string. */
e1e01040 9616 b->cond_string = cond_string != NULL ? xstrdup (cond_string) : NULL;
15630549 9617 b->thread = thread;
e12c7713 9618 }
f00aae0f
KS
9619
9620 /* Create a private copy of any extra string. */
e1e01040 9621 b->extra_string = extra_string != NULL ? xstrdup (extra_string) : NULL;
0101ce28 9622 b->ignore_count = ignore_count;
0101ce28 9623 b->disposition = tempflag ? disp_del : disp_donttouch;
0d381245 9624 b->condition_not_parsed = 1;
41447f92 9625 b->enable_state = enabled ? bp_enabled : bp_disabled;
cc72b2a2 9626 if ((type_wanted != bp_breakpoint
dda83cd7 9627 && type_wanted != bp_hardware_breakpoint) || thread != -1)
f8eba3c6 9628 b->pspace = current_program_space;
8bea4e01 9629
b270e6f9 9630 install_breakpoint (internal, std::move (b), 0);
0101ce28
JJ
9631 }
9632
6c5b2ebe 9633 if (canonical.lsals.size () > 1)
95a42b64 9634 {
3e43a32a
MS
9635 warning (_("Multiple breakpoints were set.\nUse the "
9636 "\"delete\" command to delete unwanted breakpoints."));
86b17b60 9637 prev_breakpoint_count = prev_bkpt_count;
95a42b64
TT
9638 }
9639
44702360 9640 update_global_location_list (UGLL_MAY_INSERT);
fd9b8c24
PA
9641
9642 return 1;
c3f6f71d 9643}
c906108c 9644
348d480f 9645/* Set a breakpoint.
72b2ff0e
VP
9646 ARG is a string describing breakpoint address,
9647 condition, and thread.
9648 FLAG specifies if a breakpoint is hardware on,
9649 and if breakpoint is temporary, using BP_HARDWARE_FLAG
9650 and BP_TEMPFLAG. */
348d480f 9651
98deb0da 9652static void
f2fc3015 9653break_command_1 (const char *arg, int flag, int from_tty)
c3f6f71d 9654{
72b2ff0e 9655 int tempflag = flag & BP_TEMPFLAG;
0fb4aa4b
PA
9656 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
9657 ? bp_hardware_breakpoint
9658 : bp_breakpoint);
f00aae0f 9659
ffc2605c 9660 event_location_up location = string_to_event_location (&arg, current_language);
bac7c5cf
GB
9661 const struct breakpoint_ops *ops = breakpoint_ops_for_event_location
9662 (location.get (), false /* is_tracepoint */);
c3f6f71d 9663
8cdf0e15 9664 create_breakpoint (get_current_arch (),
ffc2605c 9665 location.get (),
10a636cc 9666 NULL, 0, arg, false, 1 /* parse arg */,
0fb4aa4b 9667 tempflag, type_wanted,
8cdf0e15
VP
9668 0 /* Ignore count */,
9669 pending_break_support,
55aa24fb 9670 ops,
8cdf0e15 9671 from_tty,
84f4c1fe 9672 1 /* enabled */,
44f238bb
PA
9673 0 /* internal */,
9674 0);
c906108c
SS
9675}
9676
c906108c
SS
9677/* Helper function for break_command_1 and disassemble_command. */
9678
9679void
fba45db2 9680resolve_sal_pc (struct symtab_and_line *sal)
c906108c
SS
9681{
9682 CORE_ADDR pc;
9683
9684 if (sal->pc == 0 && sal->symtab != NULL)
9685 {
9686 if (!find_line_pc (sal->symtab, sal->line, &pc))
8a3fe4f8 9687 error (_("No line %d in file \"%s\"."),
05cba821 9688 sal->line, symtab_to_filename_for_display (sal->symtab));
c906108c 9689 sal->pc = pc;
6a048695 9690
4a64f543 9691 /* If this SAL corresponds to a breakpoint inserted using a line
dda83cd7 9692 number, then skip the function prologue if necessary. */
6a048695 9693 if (sal->explicit_line)
059acae7 9694 skip_prologue_sal (sal);
c906108c
SS
9695 }
9696
9697 if (sal->section == 0 && sal->symtab != NULL)
9698 {
346d1dfe 9699 const struct blockvector *bv;
3977b71f 9700 const struct block *b;
c5aa993b 9701 struct symbol *sym;
c906108c 9702
43f3e411
DE
9703 bv = blockvector_for_pc_sect (sal->pc, 0, &b,
9704 SYMTAB_COMPUNIT (sal->symtab));
c906108c
SS
9705 if (bv != NULL)
9706 {
7f0df278 9707 sym = block_linkage_function (b);
c906108c
SS
9708 if (sym != NULL)
9709 {
eb822aa6 9710 fixup_symbol_section (sym, SYMTAB_OBJFILE (sal->symtab));
ebbc3a7d 9711 sal->section = sym->obj_section (SYMTAB_OBJFILE (sal->symtab));
c906108c
SS
9712 }
9713 else
9714 {
4a64f543 9715 /* It really is worthwhile to have the section, so we'll
dda83cd7
SM
9716 just have to look harder. This case can be executed
9717 if we have line numbers but no functions (as can
9718 happen in assembly source). */
c906108c 9719
5ed8105e 9720 scoped_restore_current_pspace_and_thread restore_pspace_thread;
6c95b8df 9721 switch_to_program_space_and_thread (sal->pspace);
c906108c 9722
5ed8105e 9723 bound_minimal_symbol msym = lookup_minimal_symbol_by_pc (sal->pc);
7cbd4a93 9724 if (msym.minsym)
1db66e34 9725 sal->section = msym.obj_section ();
c906108c
SS
9726 }
9727 }
9728 }
9729}
9730
9731void
0b39b52e 9732break_command (const char *arg, int from_tty)
c906108c 9733{
db107f19 9734 break_command_1 (arg, 0, from_tty);
c906108c
SS
9735}
9736
c906108c 9737void
0b39b52e 9738tbreak_command (const char *arg, int from_tty)
c906108c 9739{
db107f19 9740 break_command_1 (arg, BP_TEMPFLAG, from_tty);
c906108c
SS
9741}
9742
c906108c 9743static void
0b39b52e 9744hbreak_command (const char *arg, int from_tty)
c906108c 9745{
db107f19 9746 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
c906108c
SS
9747}
9748
9749static void
0b39b52e 9750thbreak_command (const char *arg, int from_tty)
c906108c 9751{
db107f19 9752 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
c906108c
SS
9753}
9754
9755static void
ee7ddd71 9756stop_command (const char *arg, int from_tty)
c906108c 9757{
a3f17187 9758 printf_filtered (_("Specify the type of breakpoint to set.\n\
c906108c 9759Usage: stop in <function | address>\n\
a3f17187 9760 stop at <line>\n"));
c906108c
SS
9761}
9762
9763static void
4495129a 9764stopin_command (const char *arg, int from_tty)
c906108c
SS
9765{
9766 int badInput = 0;
9767
cafb3438 9768 if (arg == NULL)
c906108c
SS
9769 badInput = 1;
9770 else if (*arg != '*')
9771 {
4495129a 9772 const char *argptr = arg;
c906108c
SS
9773 int hasColon = 0;
9774
4a64f543 9775 /* Look for a ':'. If this is a line number specification, then
dda83cd7
SM
9776 say it is bad, otherwise, it should be an address or
9777 function/method name. */
c906108c 9778 while (*argptr && !hasColon)
c5aa993b
JM
9779 {
9780 hasColon = (*argptr == ':');
9781 argptr++;
9782 }
c906108c
SS
9783
9784 if (hasColon)
c5aa993b 9785 badInput = (*argptr != ':'); /* Not a class::method */
c906108c 9786 else
c5aa993b 9787 badInput = isdigit (*arg); /* a simple line number */
c906108c
SS
9788 }
9789
9790 if (badInput)
a3f17187 9791 printf_filtered (_("Usage: stop in <function | address>\n"));
c906108c 9792 else
db107f19 9793 break_command_1 (arg, 0, from_tty);
c906108c
SS
9794}
9795
9796static void
4495129a 9797stopat_command (const char *arg, int from_tty)
c906108c
SS
9798{
9799 int badInput = 0;
9800
cafb3438 9801 if (arg == NULL || *arg == '*') /* no line number */
c906108c
SS
9802 badInput = 1;
9803 else
9804 {
4495129a 9805 const char *argptr = arg;
c906108c
SS
9806 int hasColon = 0;
9807
4a64f543 9808 /* Look for a ':'. If there is a '::' then get out, otherwise
dda83cd7 9809 it is probably a line number. */
c906108c 9810 while (*argptr && !hasColon)
c5aa993b
JM
9811 {
9812 hasColon = (*argptr == ':');
9813 argptr++;
9814 }
c906108c
SS
9815
9816 if (hasColon)
c5aa993b 9817 badInput = (*argptr == ':'); /* we have class::method */
c906108c 9818 else
c5aa993b 9819 badInput = !isdigit (*arg); /* not a line number */
c906108c
SS
9820 }
9821
9822 if (badInput)
65e65158 9823 printf_filtered (_("Usage: stop at LINE\n"));
c906108c 9824 else
db107f19 9825 break_command_1 (arg, 0, from_tty);
c906108c
SS
9826}
9827
e7e0cddf
SS
9828/* The dynamic printf command is mostly like a regular breakpoint, but
9829 with a prewired command list consisting of a single output command,
9830 built from extra arguments supplied on the dprintf command
9831 line. */
9832
da821c7b 9833static void
0b39b52e 9834dprintf_command (const char *arg, int from_tty)
e7e0cddf 9835{
ffc2605c 9836 event_location_up location = string_to_event_location (&arg, current_language);
f00aae0f
KS
9837
9838 /* If non-NULL, ARG should have been advanced past the location;
9839 the next character must be ','. */
9840 if (arg != NULL)
9841 {
9842 if (arg[0] != ',' || arg[1] == '\0')
9843 error (_("Format string required"));
9844 else
9845 {
9846 /* Skip the comma. */
9847 ++arg;
9848 }
9849 }
9850
e7e0cddf 9851 create_breakpoint (get_current_arch (),
ffc2605c 9852 location.get (),
10a636cc 9853 NULL, 0, arg, false, 1 /* parse arg */,
e7e0cddf
SS
9854 0, bp_dprintf,
9855 0 /* Ignore count */,
9856 pending_break_support,
9857 &dprintf_breakpoint_ops,
9858 from_tty,
9859 1 /* enabled */,
9860 0 /* internal */,
9861 0);
9862}
9863
d3ce09f5 9864static void
0b39b52e 9865agent_printf_command (const char *arg, int from_tty)
d3ce09f5
SS
9866{
9867 error (_("May only run agent-printf on the target"));
9868}
9869
f1310107
TJB
9870/* Implement the "breakpoint_hit" breakpoint_ops method for
9871 ranged breakpoints. */
9872
9873static int
9874breakpoint_hit_ranged_breakpoint (const struct bp_location *bl,
bd522513 9875 const address_space *aspace,
09ac7c10
TT
9876 CORE_ADDR bp_addr,
9877 const struct target_waitstatus *ws)
f1310107 9878{
09ac7c10 9879 if (ws->kind != TARGET_WAITKIND_STOPPED
a493e3e2 9880 || ws->value.sig != GDB_SIGNAL_TRAP)
09ac7c10
TT
9881 return 0;
9882
f1310107
TJB
9883 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
9884 bl->length, aspace, bp_addr);
9885}
9886
9887/* Implement the "resources_needed" breakpoint_ops method for
9888 ranged breakpoints. */
9889
9890static int
9891resources_needed_ranged_breakpoint (const struct bp_location *bl)
9892{
9893 return target_ranged_break_num_registers ();
9894}
9895
9896/* Implement the "print_it" breakpoint_ops method for
9897 ranged breakpoints. */
9898
9899static enum print_stop_action
348d480f 9900print_it_ranged_breakpoint (bpstat bs)
f1310107 9901{
348d480f 9902 struct breakpoint *b = bs->breakpoint_at;
f1310107 9903 struct bp_location *bl = b->loc;
79a45e25 9904 struct ui_out *uiout = current_uiout;
f1310107
TJB
9905
9906 gdb_assert (b->type == bp_hardware_breakpoint);
9907
9908 /* Ranged breakpoints have only one location. */
9909 gdb_assert (bl && bl->next == NULL);
9910
9911 annotate_breakpoint (b->number);
f303dbd6
PA
9912
9913 maybe_print_thread_hit_breakpoint (uiout);
9914
f1310107 9915 if (b->disposition == disp_del)
112e8700 9916 uiout->text ("Temporary ranged breakpoint ");
f1310107 9917 else
112e8700
SM
9918 uiout->text ("Ranged breakpoint ");
9919 if (uiout->is_mi_like_p ())
f1310107 9920 {
112e8700 9921 uiout->field_string ("reason",
f1310107 9922 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
112e8700 9923 uiout->field_string ("disp", bpdisp_text (b->disposition));
f1310107 9924 }
381befee 9925 uiout->field_signed ("bkptno", b->number);
112e8700 9926 uiout->text (", ");
f1310107
TJB
9927
9928 return PRINT_SRC_AND_LOC;
9929}
9930
9931/* Implement the "print_one" breakpoint_ops method for
9932 ranged breakpoints. */
9933
9934static void
9935print_one_ranged_breakpoint (struct breakpoint *b,
9936 struct bp_location **last_loc)
9937{
9938 struct bp_location *bl = b->loc;
9939 struct value_print_options opts;
79a45e25 9940 struct ui_out *uiout = current_uiout;
f1310107
TJB
9941
9942 /* Ranged breakpoints have only one location. */
9943 gdb_assert (bl && bl->next == NULL);
9944
9945 get_user_print_options (&opts);
9946
9947 if (opts.addressprint)
9948 /* We don't print the address range here, it will be printed later
9949 by print_one_detail_ranged_breakpoint. */
112e8700 9950 uiout->field_skip ("addr");
f1310107
TJB
9951 annotate_field (5);
9952 print_breakpoint_location (b, bl);
9953 *last_loc = bl;
9954}
9955
9956/* Implement the "print_one_detail" breakpoint_ops method for
9957 ranged breakpoints. */
9958
9959static void
9960print_one_detail_ranged_breakpoint (const struct breakpoint *b,
9961 struct ui_out *uiout)
9962{
9963 CORE_ADDR address_start, address_end;
9964 struct bp_location *bl = b->loc;
d7e74731 9965 string_file stb;
f1310107
TJB
9966
9967 gdb_assert (bl);
9968
9969 address_start = bl->address;
9970 address_end = address_start + bl->length - 1;
9971
112e8700 9972 uiout->text ("\taddress range: ");
d7e74731
PA
9973 stb.printf ("[%s, %s]",
9974 print_core_address (bl->gdbarch, address_start),
9975 print_core_address (bl->gdbarch, address_end));
112e8700
SM
9976 uiout->field_stream ("addr", stb);
9977 uiout->text ("\n");
f1310107
TJB
9978}
9979
9980/* Implement the "print_mention" breakpoint_ops method for
9981 ranged breakpoints. */
9982
9983static void
9984print_mention_ranged_breakpoint (struct breakpoint *b)
9985{
9986 struct bp_location *bl = b->loc;
79a45e25 9987 struct ui_out *uiout = current_uiout;
f1310107
TJB
9988
9989 gdb_assert (bl);
9990 gdb_assert (b->type == bp_hardware_breakpoint);
9991
2d33446d
TT
9992 uiout->message (_("Hardware assisted ranged breakpoint %d from %s to %s."),
9993 b->number, paddress (bl->gdbarch, bl->address),
9994 paddress (bl->gdbarch, bl->address + bl->length - 1));
f1310107
TJB
9995}
9996
9997/* Implement the "print_recreate" breakpoint_ops method for
9998 ranged breakpoints. */
9999
10000static void
10001print_recreate_ranged_breakpoint (struct breakpoint *b, struct ui_file *fp)
10002{
f00aae0f 10003 fprintf_unfiltered (fp, "break-range %s, %s",
d28cd78a
TT
10004 event_location_to_string (b->location.get ()),
10005 event_location_to_string (b->location_range_end.get ()));
d9b3f62e 10006 print_recreate_thread (b, fp);
f1310107
TJB
10007}
10008
10009/* The breakpoint_ops structure to be used in ranged breakpoints. */
10010
2060206e 10011static struct breakpoint_ops ranged_breakpoint_ops;
f1310107
TJB
10012
10013/* Find the address where the end of the breakpoint range should be
10014 placed, given the SAL of the end of the range. This is so that if
10015 the user provides a line number, the end of the range is set to the
10016 last instruction of the given line. */
10017
10018static CORE_ADDR
10019find_breakpoint_range_end (struct symtab_and_line sal)
10020{
10021 CORE_ADDR end;
10022
10023 /* If the user provided a PC value, use it. Otherwise,
10024 find the address of the end of the given location. */
10025 if (sal.explicit_pc)
10026 end = sal.pc;
10027 else
10028 {
10029 int ret;
10030 CORE_ADDR start;
10031
10032 ret = find_line_pc_range (sal, &start, &end);
10033 if (!ret)
10034 error (_("Could not find location of the end of the range."));
10035
10036 /* find_line_pc_range returns the start of the next line. */
10037 end--;
10038 }
10039
10040 return end;
10041}
10042
10043/* Implement the "break-range" CLI command. */
10044
10045static void
0b39b52e 10046break_range_command (const char *arg, int from_tty)
f1310107 10047{
f2fc3015 10048 const char *arg_start;
f1310107
TJB
10049 struct linespec_result canonical_start, canonical_end;
10050 int bp_count, can_use_bp, length;
10051 CORE_ADDR end;
10052 struct breakpoint *b;
f1310107
TJB
10053
10054 /* We don't support software ranged breakpoints. */
10055 if (target_ranged_break_num_registers () < 0)
10056 error (_("This target does not support hardware ranged breakpoints."));
10057
10058 bp_count = hw_breakpoint_used_count ();
10059 bp_count += target_ranged_break_num_registers ();
10060 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
10061 bp_count, 0);
10062 if (can_use_bp < 0)
10063 error (_("Hardware breakpoints used exceeds limit."));
10064
f8eba3c6 10065 arg = skip_spaces (arg);
f1310107
TJB
10066 if (arg == NULL || arg[0] == '\0')
10067 error(_("No address range specified."));
10068
f8eba3c6 10069 arg_start = arg;
ffc2605c
TT
10070 event_location_up start_location = string_to_event_location (&arg,
10071 current_language);
10072 parse_breakpoint_sals (start_location.get (), &canonical_start);
f1310107
TJB
10073
10074 if (arg[0] != ',')
10075 error (_("Too few arguments."));
6c5b2ebe 10076 else if (canonical_start.lsals.empty ())
f1310107 10077 error (_("Could not find location of the beginning of the range."));
f8eba3c6 10078
6c5b2ebe 10079 const linespec_sals &lsal_start = canonical_start.lsals[0];
f8eba3c6 10080
6c5b2ebe
PA
10081 if (canonical_start.lsals.size () > 1
10082 || lsal_start.sals.size () != 1)
f1310107
TJB
10083 error (_("Cannot create a ranged breakpoint with multiple locations."));
10084
6c5b2ebe 10085 const symtab_and_line &sal_start = lsal_start.sals[0];
81b1e71c 10086 std::string addr_string_start (arg_start, arg - arg_start);
f1310107
TJB
10087
10088 arg++; /* Skip the comma. */
f8eba3c6 10089 arg = skip_spaces (arg);
f1310107
TJB
10090
10091 /* Parse the end location. */
10092
f1310107
TJB
10093 arg_start = arg;
10094
f8eba3c6 10095 /* We call decode_line_full directly here instead of using
f1310107
TJB
10096 parse_breakpoint_sals because we need to specify the start location's
10097 symtab and line as the default symtab and line for the end of the
10098 range. This makes it possible to have ranges like "foo.c:27, +14",
10099 where +14 means 14 lines from the start location. */
ffc2605c
TT
10100 event_location_up end_location = string_to_event_location (&arg,
10101 current_language);
10102 decode_line_full (end_location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
f8eba3c6
TT
10103 sal_start.symtab, sal_start.line,
10104 &canonical_end, NULL, NULL);
10105
6c5b2ebe 10106 if (canonical_end.lsals.empty ())
f1310107 10107 error (_("Could not find location of the end of the range."));
f8eba3c6 10108
6c5b2ebe
PA
10109 const linespec_sals &lsal_end = canonical_end.lsals[0];
10110 if (canonical_end.lsals.size () > 1
10111 || lsal_end.sals.size () != 1)
f1310107
TJB
10112 error (_("Cannot create a ranged breakpoint with multiple locations."));
10113
6c5b2ebe 10114 const symtab_and_line &sal_end = lsal_end.sals[0];
f1310107
TJB
10115
10116 end = find_breakpoint_range_end (sal_end);
10117 if (sal_start.pc > end)
177b42fe 10118 error (_("Invalid address range, end precedes start."));
f1310107
TJB
10119
10120 length = end - sal_start.pc + 1;
10121 if (length < 0)
10122 /* Length overflowed. */
10123 error (_("Address range too large."));
10124 else if (length == 1)
10125 {
10126 /* This range is simple enough to be handled by
10127 the `hbreak' command. */
81b1e71c 10128 hbreak_command (&addr_string_start[0], 1);
f1310107
TJB
10129
10130 return;
10131 }
10132
10133 /* Now set up the breakpoint. */
10134 b = set_raw_breakpoint (get_current_arch (), sal_start,
348d480f 10135 bp_hardware_breakpoint, &ranged_breakpoint_ops);
f1310107
TJB
10136 set_breakpoint_count (breakpoint_count + 1);
10137 b->number = breakpoint_count;
10138 b->disposition = disp_donttouch;
d28cd78a
TT
10139 b->location = std::move (start_location);
10140 b->location_range_end = std::move (end_location);
f1310107
TJB
10141 b->loc->length = length;
10142
f1310107 10143 mention (b);
76727919 10144 gdb::observers::breakpoint_created.notify (b);
44702360 10145 update_global_location_list (UGLL_MAY_INSERT);
f1310107
TJB
10146}
10147
4a64f543
MS
10148/* Return non-zero if EXP is verified as constant. Returned zero
10149 means EXP is variable. Also the constant detection may fail for
10150 some constant expressions and in such case still falsely return
10151 zero. */
2e6e3d9c 10152
dab72643 10153static bool
65d79d4b
SDJ
10154watchpoint_exp_is_const (const struct expression *exp)
10155{
1eaebe02 10156 return exp->op->constant_p ();
65d79d4b
SDJ
10157}
10158
c1fc2657 10159/* Watchpoint destructor. */
3a5c3e22 10160
c1fc2657 10161watchpoint::~watchpoint ()
3a5c3e22 10162{
c1fc2657
SM
10163 xfree (this->exp_string);
10164 xfree (this->exp_string_reparse);
3a5c3e22
PA
10165}
10166
348d480f
PA
10167/* Implement the "re_set" breakpoint_ops method for watchpoints. */
10168
10169static void
10170re_set_watchpoint (struct breakpoint *b)
10171{
3a5c3e22
PA
10172 struct watchpoint *w = (struct watchpoint *) b;
10173
348d480f
PA
10174 /* Watchpoint can be either on expression using entirely global
10175 variables, or it can be on local variables.
10176
10177 Watchpoints of the first kind are never auto-deleted, and even
10178 persist across program restarts. Since they can use variables
10179 from shared libraries, we need to reparse expression as libraries
10180 are loaded and unloaded.
10181
10182 Watchpoints on local variables can also change meaning as result
10183 of solib event. For example, if a watchpoint uses both a local
10184 and a global variables in expression, it's a local watchpoint,
10185 but unloading of a shared library will make the expression
10186 invalid. This is not a very common use case, but we still
10187 re-evaluate expression, to avoid surprises to the user.
10188
10189 Note that for local watchpoints, we re-evaluate it only if
10190 watchpoints frame id is still valid. If it's not, it means the
10191 watchpoint is out of scope and will be deleted soon. In fact,
10192 I'm not sure we'll ever be called in this case.
10193
10194 If a local watchpoint's frame id is still valid, then
3a5c3e22 10195 w->exp_valid_block is likewise valid, and we can safely use it.
348d480f 10196
3a5c3e22
PA
10197 Don't do anything about disabled watchpoints, since they will be
10198 reevaluated again when enabled. */
10199 update_watchpoint (w, 1 /* reparse */);
348d480f
PA
10200}
10201
77b06cd7
TJB
10202/* Implement the "insert" breakpoint_ops method for hardware watchpoints. */
10203
10204static int
10205insert_watchpoint (struct bp_location *bl)
10206{
3a5c3e22
PA
10207 struct watchpoint *w = (struct watchpoint *) bl->owner;
10208 int length = w->exact ? 1 : bl->length;
e09342b5
TJB
10209
10210 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
4d01a485 10211 w->cond_exp.get ());
77b06cd7
TJB
10212}
10213
10214/* Implement the "remove" breakpoint_ops method for hardware watchpoints. */
10215
10216static int
73971819 10217remove_watchpoint (struct bp_location *bl, enum remove_bp_reason reason)
77b06cd7 10218{
3a5c3e22
PA
10219 struct watchpoint *w = (struct watchpoint *) bl->owner;
10220 int length = w->exact ? 1 : bl->length;
e09342b5
TJB
10221
10222 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
4d01a485 10223 w->cond_exp.get ());
e09342b5
TJB
10224}
10225
e09342b5 10226static int
348d480f 10227breakpoint_hit_watchpoint (const struct bp_location *bl,
bd522513 10228 const address_space *aspace, CORE_ADDR bp_addr,
09ac7c10 10229 const struct target_waitstatus *ws)
e09342b5 10230{
348d480f 10231 struct breakpoint *b = bl->owner;
3a5c3e22 10232 struct watchpoint *w = (struct watchpoint *) b;
77b06cd7 10233
348d480f
PA
10234 /* Continuable hardware watchpoints are treated as non-existent if the
10235 reason we stopped wasn't a hardware watchpoint (we didn't stop on
10236 some data address). Otherwise gdb won't stop on a break instruction
10237 in the code (not from a breakpoint) when a hardware watchpoint has
10238 been defined. Also skip watchpoints which we know did not trigger
10239 (did not match the data address). */
10240 if (is_hardware_watchpoint (b)
3a5c3e22 10241 && w->watchpoint_triggered == watch_triggered_no)
348d480f 10242 return 0;
9c06b0b4 10243
348d480f 10244 return 1;
9c06b0b4
TJB
10245}
10246
348d480f
PA
10247static void
10248check_status_watchpoint (bpstat bs)
9c06b0b4 10249{
348d480f 10250 gdb_assert (is_watchpoint (bs->breakpoint_at));
9c06b0b4 10251
348d480f 10252 bpstat_check_watchpoint (bs);
9c06b0b4
TJB
10253}
10254
10255/* Implement the "resources_needed" breakpoint_ops method for
348d480f 10256 hardware watchpoints. */
9c06b0b4
TJB
10257
10258static int
348d480f 10259resources_needed_watchpoint (const struct bp_location *bl)
9c06b0b4 10260{
3a5c3e22
PA
10261 struct watchpoint *w = (struct watchpoint *) bl->owner;
10262 int length = w->exact? 1 : bl->length;
348d480f
PA
10263
10264 return target_region_ok_for_hw_watchpoint (bl->address, length);
9c06b0b4
TJB
10265}
10266
10267/* Implement the "works_in_software_mode" breakpoint_ops method for
348d480f 10268 hardware watchpoints. */
9c06b0b4
TJB
10269
10270static int
348d480f 10271works_in_software_mode_watchpoint (const struct breakpoint *b)
9c06b0b4 10272{
efa80663
PA
10273 /* Read and access watchpoints only work with hardware support. */
10274 return b->type == bp_watchpoint || b->type == bp_hardware_watchpoint;
9c06b0b4
TJB
10275}
10276
9c06b0b4 10277static enum print_stop_action
348d480f 10278print_it_watchpoint (bpstat bs)
9c06b0b4 10279{
348d480f 10280 struct breakpoint *b;
348d480f 10281 enum print_stop_action result;
3a5c3e22 10282 struct watchpoint *w;
79a45e25 10283 struct ui_out *uiout = current_uiout;
348d480f
PA
10284
10285 gdb_assert (bs->bp_location_at != NULL);
10286
348d480f 10287 b = bs->breakpoint_at;
3a5c3e22 10288 w = (struct watchpoint *) b;
348d480f 10289
f303dbd6
PA
10290 annotate_watchpoint (b->number);
10291 maybe_print_thread_hit_breakpoint (uiout);
10292
d7e74731
PA
10293 string_file stb;
10294
76f9c9cf 10295 gdb::optional<ui_out_emit_tuple> tuple_emitter;
9c06b0b4
TJB
10296 switch (b->type)
10297 {
348d480f 10298 case bp_watchpoint:
9c06b0b4 10299 case bp_hardware_watchpoint:
112e8700
SM
10300 if (uiout->is_mi_like_p ())
10301 uiout->field_string
10302 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
348d480f 10303 mention (b);
76f9c9cf 10304 tuple_emitter.emplace (uiout, "value");
112e8700 10305 uiout->text ("\nOld value = ");
850645cf 10306 watchpoint_value_print (bs->old_val.get (), &stb);
112e8700
SM
10307 uiout->field_stream ("old", stb);
10308 uiout->text ("\nNew value = ");
850645cf 10309 watchpoint_value_print (w->val.get (), &stb);
112e8700
SM
10310 uiout->field_stream ("new", stb);
10311 uiout->text ("\n");
348d480f
PA
10312 /* More than one watchpoint may have been triggered. */
10313 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10314 break;
10315
10316 case bp_read_watchpoint:
112e8700
SM
10317 if (uiout->is_mi_like_p ())
10318 uiout->field_string
10319 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
348d480f 10320 mention (b);
76f9c9cf 10321 tuple_emitter.emplace (uiout, "value");
112e8700 10322 uiout->text ("\nValue = ");
850645cf 10323 watchpoint_value_print (w->val.get (), &stb);
112e8700
SM
10324 uiout->field_stream ("value", stb);
10325 uiout->text ("\n");
348d480f 10326 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10327 break;
10328
10329 case bp_access_watchpoint:
348d480f
PA
10330 if (bs->old_val != NULL)
10331 {
112e8700
SM
10332 if (uiout->is_mi_like_p ())
10333 uiout->field_string
10334 ("reason",
348d480f
PA
10335 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10336 mention (b);
76f9c9cf 10337 tuple_emitter.emplace (uiout, "value");
112e8700 10338 uiout->text ("\nOld value = ");
850645cf 10339 watchpoint_value_print (bs->old_val.get (), &stb);
112e8700
SM
10340 uiout->field_stream ("old", stb);
10341 uiout->text ("\nNew value = ");
348d480f
PA
10342 }
10343 else
10344 {
10345 mention (b);
112e8700
SM
10346 if (uiout->is_mi_like_p ())
10347 uiout->field_string
10348 ("reason",
348d480f 10349 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
76f9c9cf 10350 tuple_emitter.emplace (uiout, "value");
112e8700 10351 uiout->text ("\nValue = ");
348d480f 10352 }
850645cf 10353 watchpoint_value_print (w->val.get (), &stb);
112e8700
SM
10354 uiout->field_stream ("new", stb);
10355 uiout->text ("\n");
348d480f 10356 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10357 break;
10358 default:
348d480f 10359 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10360 }
10361
348d480f
PA
10362 return result;
10363}
10364
10365/* Implement the "print_mention" breakpoint_ops method for hardware
10366 watchpoints. */
10367
10368static void
10369print_mention_watchpoint (struct breakpoint *b)
10370{
3a5c3e22 10371 struct watchpoint *w = (struct watchpoint *) b;
79a45e25 10372 struct ui_out *uiout = current_uiout;
46b9c129 10373 const char *tuple_name;
348d480f
PA
10374
10375 switch (b->type)
10376 {
10377 case bp_watchpoint:
112e8700 10378 uiout->text ("Watchpoint ");
46b9c129 10379 tuple_name = "wpt";
348d480f
PA
10380 break;
10381 case bp_hardware_watchpoint:
112e8700 10382 uiout->text ("Hardware watchpoint ");
46b9c129 10383 tuple_name = "wpt";
348d480f
PA
10384 break;
10385 case bp_read_watchpoint:
112e8700 10386 uiout->text ("Hardware read watchpoint ");
46b9c129 10387 tuple_name = "hw-rwpt";
348d480f
PA
10388 break;
10389 case bp_access_watchpoint:
112e8700 10390 uiout->text ("Hardware access (read/write) watchpoint ");
46b9c129 10391 tuple_name = "hw-awpt";
348d480f
PA
10392 break;
10393 default:
10394 internal_error (__FILE__, __LINE__,
10395 _("Invalid hardware watchpoint type."));
10396 }
10397
46b9c129 10398 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
381befee 10399 uiout->field_signed ("number", b->number);
112e8700
SM
10400 uiout->text (": ");
10401 uiout->field_string ("exp", w->exp_string);
348d480f
PA
10402}
10403
10404/* Implement the "print_recreate" breakpoint_ops method for
10405 watchpoints. */
10406
10407static void
10408print_recreate_watchpoint (struct breakpoint *b, struct ui_file *fp)
10409{
3a5c3e22
PA
10410 struct watchpoint *w = (struct watchpoint *) b;
10411
348d480f
PA
10412 switch (b->type)
10413 {
10414 case bp_watchpoint:
10415 case bp_hardware_watchpoint:
10416 fprintf_unfiltered (fp, "watch");
10417 break;
10418 case bp_read_watchpoint:
10419 fprintf_unfiltered (fp, "rwatch");
10420 break;
10421 case bp_access_watchpoint:
10422 fprintf_unfiltered (fp, "awatch");
10423 break;
10424 default:
10425 internal_error (__FILE__, __LINE__,
10426 _("Invalid watchpoint type."));
10427 }
10428
3a5c3e22 10429 fprintf_unfiltered (fp, " %s", w->exp_string);
d9b3f62e 10430 print_recreate_thread (b, fp);
348d480f
PA
10431}
10432
427cd150
TT
10433/* Implement the "explains_signal" breakpoint_ops method for
10434 watchpoints. */
10435
47591c29 10436static int
427cd150
TT
10437explains_signal_watchpoint (struct breakpoint *b, enum gdb_signal sig)
10438{
10439 /* A software watchpoint cannot cause a signal other than
10440 GDB_SIGNAL_TRAP. */
10441 if (b->type == bp_watchpoint && sig != GDB_SIGNAL_TRAP)
47591c29 10442 return 0;
427cd150 10443
47591c29 10444 return 1;
427cd150
TT
10445}
10446
348d480f
PA
10447/* The breakpoint_ops structure to be used in hardware watchpoints. */
10448
2060206e 10449static struct breakpoint_ops watchpoint_breakpoint_ops;
348d480f
PA
10450
10451/* Implement the "insert" breakpoint_ops method for
10452 masked hardware watchpoints. */
10453
10454static int
10455insert_masked_watchpoint (struct bp_location *bl)
10456{
3a5c3e22
PA
10457 struct watchpoint *w = (struct watchpoint *) bl->owner;
10458
10459 return target_insert_mask_watchpoint (bl->address, w->hw_wp_mask,
348d480f
PA
10460 bl->watchpoint_type);
10461}
10462
10463/* Implement the "remove" breakpoint_ops method for
10464 masked hardware watchpoints. */
10465
10466static int
73971819 10467remove_masked_watchpoint (struct bp_location *bl, enum remove_bp_reason reason)
348d480f 10468{
3a5c3e22
PA
10469 struct watchpoint *w = (struct watchpoint *) bl->owner;
10470
10471 return target_remove_mask_watchpoint (bl->address, w->hw_wp_mask,
dda83cd7 10472 bl->watchpoint_type);
348d480f
PA
10473}
10474
10475/* Implement the "resources_needed" breakpoint_ops method for
10476 masked hardware watchpoints. */
10477
10478static int
10479resources_needed_masked_watchpoint (const struct bp_location *bl)
10480{
3a5c3e22
PA
10481 struct watchpoint *w = (struct watchpoint *) bl->owner;
10482
10483 return target_masked_watch_num_registers (bl->address, w->hw_wp_mask);
348d480f
PA
10484}
10485
10486/* Implement the "works_in_software_mode" breakpoint_ops method for
10487 masked hardware watchpoints. */
10488
10489static int
10490works_in_software_mode_masked_watchpoint (const struct breakpoint *b)
10491{
10492 return 0;
10493}
10494
10495/* Implement the "print_it" breakpoint_ops method for
10496 masked hardware watchpoints. */
10497
10498static enum print_stop_action
10499print_it_masked_watchpoint (bpstat bs)
10500{
10501 struct breakpoint *b = bs->breakpoint_at;
79a45e25 10502 struct ui_out *uiout = current_uiout;
348d480f
PA
10503
10504 /* Masked watchpoints have only one location. */
10505 gdb_assert (b->loc && b->loc->next == NULL);
10506
f303dbd6
PA
10507 annotate_watchpoint (b->number);
10508 maybe_print_thread_hit_breakpoint (uiout);
10509
348d480f
PA
10510 switch (b->type)
10511 {
10512 case bp_hardware_watchpoint:
112e8700
SM
10513 if (uiout->is_mi_like_p ())
10514 uiout->field_string
10515 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
348d480f
PA
10516 break;
10517
10518 case bp_read_watchpoint:
112e8700
SM
10519 if (uiout->is_mi_like_p ())
10520 uiout->field_string
10521 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
348d480f
PA
10522 break;
10523
10524 case bp_access_watchpoint:
112e8700
SM
10525 if (uiout->is_mi_like_p ())
10526 uiout->field_string
10527 ("reason",
348d480f
PA
10528 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10529 break;
10530 default:
10531 internal_error (__FILE__, __LINE__,
10532 _("Invalid hardware watchpoint type."));
10533 }
10534
10535 mention (b);
112e8700 10536 uiout->text (_("\n\
9c06b0b4
TJB
10537Check the underlying instruction at PC for the memory\n\
10538address and value which triggered this watchpoint.\n"));
112e8700 10539 uiout->text ("\n");
9c06b0b4
TJB
10540
10541 /* More than one watchpoint may have been triggered. */
10542 return PRINT_UNKNOWN;
10543}
10544
10545/* Implement the "print_one_detail" breakpoint_ops method for
10546 masked hardware watchpoints. */
10547
10548static void
10549print_one_detail_masked_watchpoint (const struct breakpoint *b,
10550 struct ui_out *uiout)
10551{
3a5c3e22
PA
10552 struct watchpoint *w = (struct watchpoint *) b;
10553
9c06b0b4
TJB
10554 /* Masked watchpoints have only one location. */
10555 gdb_assert (b->loc && b->loc->next == NULL);
10556
112e8700
SM
10557 uiout->text ("\tmask ");
10558 uiout->field_core_addr ("mask", b->loc->gdbarch, w->hw_wp_mask);
10559 uiout->text ("\n");
9c06b0b4
TJB
10560}
10561
10562/* Implement the "print_mention" breakpoint_ops method for
10563 masked hardware watchpoints. */
10564
10565static void
10566print_mention_masked_watchpoint (struct breakpoint *b)
10567{
3a5c3e22 10568 struct watchpoint *w = (struct watchpoint *) b;
79a45e25 10569 struct ui_out *uiout = current_uiout;
46b9c129 10570 const char *tuple_name;
9c06b0b4
TJB
10571
10572 switch (b->type)
10573 {
10574 case bp_hardware_watchpoint:
112e8700 10575 uiout->text ("Masked hardware watchpoint ");
46b9c129 10576 tuple_name = "wpt";
9c06b0b4
TJB
10577 break;
10578 case bp_read_watchpoint:
112e8700 10579 uiout->text ("Masked hardware read watchpoint ");
46b9c129 10580 tuple_name = "hw-rwpt";
9c06b0b4
TJB
10581 break;
10582 case bp_access_watchpoint:
112e8700 10583 uiout->text ("Masked hardware access (read/write) watchpoint ");
46b9c129 10584 tuple_name = "hw-awpt";
9c06b0b4
TJB
10585 break;
10586 default:
10587 internal_error (__FILE__, __LINE__,
10588 _("Invalid hardware watchpoint type."));
10589 }
10590
46b9c129 10591 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
381befee 10592 uiout->field_signed ("number", b->number);
112e8700
SM
10593 uiout->text (": ");
10594 uiout->field_string ("exp", w->exp_string);
9c06b0b4
TJB
10595}
10596
10597/* Implement the "print_recreate" breakpoint_ops method for
10598 masked hardware watchpoints. */
10599
10600static void
10601print_recreate_masked_watchpoint (struct breakpoint *b, struct ui_file *fp)
10602{
3a5c3e22 10603 struct watchpoint *w = (struct watchpoint *) b;
9c06b0b4
TJB
10604
10605 switch (b->type)
10606 {
10607 case bp_hardware_watchpoint:
10608 fprintf_unfiltered (fp, "watch");
10609 break;
10610 case bp_read_watchpoint:
10611 fprintf_unfiltered (fp, "rwatch");
10612 break;
10613 case bp_access_watchpoint:
10614 fprintf_unfiltered (fp, "awatch");
10615 break;
10616 default:
10617 internal_error (__FILE__, __LINE__,
10618 _("Invalid hardware watchpoint type."));
10619 }
10620
53807e9f
TT
10621 fprintf_unfiltered (fp, " %s mask 0x%s", w->exp_string,
10622 phex (w->hw_wp_mask, sizeof (CORE_ADDR)));
d9b3f62e 10623 print_recreate_thread (b, fp);
9c06b0b4
TJB
10624}
10625
10626/* The breakpoint_ops structure to be used in masked hardware watchpoints. */
10627
2060206e 10628static struct breakpoint_ops masked_watchpoint_breakpoint_ops;
9c06b0b4
TJB
10629
10630/* Tell whether the given watchpoint is a masked hardware watchpoint. */
10631
f2478a7e 10632static bool
9c06b0b4
TJB
10633is_masked_watchpoint (const struct breakpoint *b)
10634{
10635 return b->ops == &masked_watchpoint_breakpoint_ops;
10636}
10637
53a5351d 10638/* accessflag: hw_write: watch write,
dda83cd7 10639 hw_read: watch read,
53a5351d 10640 hw_access: watch access (read or write) */
c906108c 10641static void
bbc13ae3 10642watch_command_1 (const char *arg, int accessflag, int from_tty,
2e362716 10643 bool just_location, bool internal)
c906108c 10644{
c1fc2657 10645 struct breakpoint *scope_breakpoint = NULL;
270140bd 10646 const struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
b926417a 10647 struct value *result;
bb9d5f81 10648 int saved_bitpos = 0, saved_bitsize = 0;
bbc13ae3
KS
10649 const char *exp_start = NULL;
10650 const char *exp_end = NULL;
10651 const char *tok, *end_tok;
9c06b0b4 10652 int toklen = -1;
bbc13ae3
KS
10653 const char *cond_start = NULL;
10654 const char *cond_end = NULL;
c906108c 10655 enum bptype bp_type;
37e4754d 10656 int thread = -1;
9c06b0b4
TJB
10657 /* Flag to indicate whether we are going to use masks for
10658 the hardware watchpoint. */
2e362716 10659 bool use_mask = false;
9c06b0b4 10660 CORE_ADDR mask = 0;
c906108c 10661
37e4754d
LM
10662 /* Make sure that we actually have parameters to parse. */
10663 if (arg != NULL && arg[0] != '\0')
10664 {
bbc13ae3
KS
10665 const char *value_start;
10666
10667 exp_end = arg + strlen (arg);
37e4754d 10668
9c06b0b4
TJB
10669 /* Look for "parameter value" pairs at the end
10670 of the arguments string. */
bbc13ae3 10671 for (tok = exp_end - 1; tok > arg; tok--)
9c06b0b4
TJB
10672 {
10673 /* Skip whitespace at the end of the argument list. */
10674 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10675 tok--;
10676
10677 /* Find the beginning of the last token.
10678 This is the value of the parameter. */
10679 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10680 tok--;
10681 value_start = tok + 1;
10682
10683 /* Skip whitespace. */
10684 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10685 tok--;
10686
10687 end_tok = tok;
10688
10689 /* Find the beginning of the second to last token.
10690 This is the parameter itself. */
10691 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10692 tok--;
10693 tok++;
10694 toklen = end_tok - tok + 1;
10695
61012eef 10696 if (toklen == 6 && startswith (tok, "thread"))
9c06b0b4 10697 {
5d5658a1 10698 struct thread_info *thr;
9c06b0b4
TJB
10699 /* At this point we've found a "thread" token, which means
10700 the user is trying to set a watchpoint that triggers
10701 only in a specific thread. */
5d5658a1 10702 const char *endp;
37e4754d 10703
9c06b0b4
TJB
10704 if (thread != -1)
10705 error(_("You can specify only one thread."));
37e4754d 10706
9c06b0b4 10707 /* Extract the thread ID from the next token. */
5d5658a1 10708 thr = parse_thread_id (value_start, &endp);
37e4754d 10709
5d5658a1 10710 /* Check if the user provided a valid thread ID. */
9c06b0b4 10711 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
5d5658a1 10712 invalid_thread_id_error (value_start);
9c06b0b4 10713
5d5658a1 10714 thread = thr->global_num;
9c06b0b4 10715 }
61012eef 10716 else if (toklen == 4 && startswith (tok, "mask"))
9c06b0b4
TJB
10717 {
10718 /* We've found a "mask" token, which means the user wants to
10719 create a hardware watchpoint that is going to have the mask
10720 facility. */
10721 struct value *mask_value, *mark;
37e4754d 10722
9c06b0b4
TJB
10723 if (use_mask)
10724 error(_("You can specify only one mask."));
37e4754d 10725
2e362716 10726 use_mask = just_location = true;
37e4754d 10727
9c06b0b4
TJB
10728 mark = value_mark ();
10729 mask_value = parse_to_comma_and_eval (&value_start);
10730 mask = value_as_address (mask_value);
10731 value_free_to_mark (mark);
10732 }
10733 else
10734 /* We didn't recognize what we found. We should stop here. */
10735 break;
37e4754d 10736
9c06b0b4
TJB
10737 /* Truncate the string and get rid of the "parameter value" pair before
10738 the arguments string is parsed by the parse_exp_1 function. */
bbc13ae3 10739 exp_end = tok;
9c06b0b4 10740 }
37e4754d 10741 }
bbc13ae3
KS
10742 else
10743 exp_end = arg;
37e4754d 10744
bbc13ae3
KS
10745 /* Parse the rest of the arguments. From here on out, everything
10746 is in terms of a newly allocated string instead of the original
10747 ARG. */
81b1e71c
TT
10748 std::string expression (arg, exp_end - arg);
10749 exp_start = arg = expression.c_str ();
699bd4cf
TT
10750 innermost_block_tracker tracker;
10751 expression_up exp = parse_exp_1 (&arg, 0, 0, 0, &tracker);
c906108c 10752 exp_end = arg;
fa8a61dc
TT
10753 /* Remove trailing whitespace from the expression before saving it.
10754 This makes the eventual display of the expression string a bit
10755 prettier. */
10756 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
10757 --exp_end;
10758
65d79d4b 10759 /* Checking if the expression is not constant. */
4d01a485 10760 if (watchpoint_exp_is_const (exp.get ()))
65d79d4b
SDJ
10761 {
10762 int len;
10763
10764 len = exp_end - exp_start;
10765 while (len > 0 && isspace (exp_start[len - 1]))
10766 len--;
10767 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
10768 }
10769
699bd4cf 10770 exp_valid_block = tracker.block ();
b926417a 10771 struct value *mark = value_mark ();
850645cf 10772 struct value *val_as_value = nullptr;
1eaebe02
TT
10773 fetch_subexp_value (exp.get (), exp->op.get (), &val_as_value, &result, NULL,
10774 just_location);
06a64a0b 10775
850645cf 10776 if (val_as_value != NULL && just_location)
bb9d5f81 10777 {
850645cf
TT
10778 saved_bitpos = value_bitpos (val_as_value);
10779 saved_bitsize = value_bitsize (val_as_value);
bb9d5f81
PP
10780 }
10781
850645cf 10782 value_ref_ptr val;
06a64a0b
TT
10783 if (just_location)
10784 {
9c06b0b4
TJB
10785 int ret;
10786
06a64a0b 10787 exp_valid_block = NULL;
850645cf 10788 val = release_value (value_addr (result));
06a64a0b 10789 value_free_to_mark (mark);
9c06b0b4
TJB
10790
10791 if (use_mask)
10792 {
850645cf 10793 ret = target_masked_watch_num_registers (value_as_address (val.get ()),
9c06b0b4
TJB
10794 mask);
10795 if (ret == -1)
10796 error (_("This target does not support masked watchpoints."));
10797 else if (ret == -2)
10798 error (_("Invalid mask or memory region."));
10799 }
06a64a0b 10800 }
850645cf
TT
10801 else if (val_as_value != NULL)
10802 val = release_value (val_as_value);
c906108c 10803
f1735a53
TT
10804 tok = skip_spaces (arg);
10805 end_tok = skip_to_space (tok);
c906108c
SS
10806
10807 toklen = end_tok - tok;
10808 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
10809 {
10810 tok = cond_start = end_tok + 1;
699bd4cf
TT
10811 innermost_block_tracker if_tracker;
10812 parse_exp_1 (&tok, 0, 0, 0, &if_tracker);
60e1c644
PA
10813
10814 /* The watchpoint expression may not be local, but the condition
10815 may still be. E.g.: `watch global if local > 0'. */
699bd4cf 10816 cond_exp_valid_block = if_tracker.block ();
60e1c644 10817
c906108c
SS
10818 cond_end = tok;
10819 }
10820 if (*tok)
8a3fe4f8 10821 error (_("Junk at end of command."));
c906108c 10822
441d7c93
PA
10823 frame_info *wp_frame = block_innermost_frame (exp_valid_block);
10824
10825 /* Save this because create_internal_breakpoint below invalidates
10826 'wp_frame'. */
10827 frame_id watchpoint_frame = get_frame_id (wp_frame);
d983da9c
DJ
10828
10829 /* If the expression is "local", then set up a "watchpoint scope"
10830 breakpoint at the point where we've left the scope of the watchpoint
10831 expression. Create the scope breakpoint before the watchpoint, so
10832 that we will encounter it first in bpstat_stop_status. */
441d7c93 10833 if (exp_valid_block != NULL && wp_frame != NULL)
d983da9c 10834 {
441d7c93
PA
10835 frame_id caller_frame_id = frame_unwind_caller_id (wp_frame);
10836
10837 if (frame_id_p (caller_frame_id))
edb3359d 10838 {
441d7c93
PA
10839 gdbarch *caller_arch = frame_unwind_caller_arch (wp_frame);
10840 CORE_ADDR caller_pc = frame_unwind_caller_pc (wp_frame);
10841
edb3359d 10842 scope_breakpoint
441d7c93 10843 = create_internal_breakpoint (caller_arch, caller_pc,
06edf0c0
PA
10844 bp_watchpoint_scope,
10845 &momentary_breakpoint_ops);
d983da9c 10846
441d7c93
PA
10847 /* create_internal_breakpoint could invalidate WP_FRAME. */
10848 wp_frame = NULL;
10849
edb3359d 10850 scope_breakpoint->enable_state = bp_enabled;
d983da9c 10851
edb3359d
DJ
10852 /* Automatically delete the breakpoint when it hits. */
10853 scope_breakpoint->disposition = disp_del;
d983da9c 10854
edb3359d 10855 /* Only break in the proper frame (help with recursion). */
441d7c93 10856 scope_breakpoint->frame_id = caller_frame_id;
d983da9c 10857
edb3359d 10858 /* Set the address at which we will stop. */
441d7c93
PA
10859 scope_breakpoint->loc->gdbarch = caller_arch;
10860 scope_breakpoint->loc->requested_address = caller_pc;
edb3359d 10861 scope_breakpoint->loc->address
a6d9a66e
UW
10862 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
10863 scope_breakpoint->loc->requested_address,
edb3359d
DJ
10864 scope_breakpoint->type);
10865 }
d983da9c
DJ
10866 }
10867
e8369a73
AB
10868 /* Now set up the breakpoint. We create all watchpoints as hardware
10869 watchpoints here even if hardware watchpoints are turned off, a call
10870 to update_watchpoint later in this function will cause the type to
10871 drop back to bp_watchpoint (software watchpoint) if required. */
10872
10873 if (accessflag == hw_read)
10874 bp_type = bp_read_watchpoint;
10875 else if (accessflag == hw_access)
10876 bp_type = bp_access_watchpoint;
10877 else
10878 bp_type = bp_hardware_watchpoint;
3a5c3e22 10879
b270e6f9 10880 std::unique_ptr<watchpoint> w (new watchpoint ());
c1fc2657 10881
348d480f 10882 if (use_mask)
b270e6f9 10883 init_raw_breakpoint_without_location (w.get (), NULL, bp_type,
3a5c3e22 10884 &masked_watchpoint_breakpoint_ops);
348d480f 10885 else
b270e6f9 10886 init_raw_breakpoint_without_location (w.get (), NULL, bp_type,
3a5c3e22 10887 &watchpoint_breakpoint_ops);
c1fc2657
SM
10888 w->thread = thread;
10889 w->disposition = disp_donttouch;
10890 w->pspace = current_program_space;
b22e99fd 10891 w->exp = std::move (exp);
3a5c3e22
PA
10892 w->exp_valid_block = exp_valid_block;
10893 w->cond_exp_valid_block = cond_exp_valid_block;
06a64a0b
TT
10894 if (just_location)
10895 {
850645cf
TT
10896 struct type *t = value_type (val.get ());
10897 CORE_ADDR addr = value_as_address (val.get ());
06a64a0b 10898
43cc5389 10899 w->exp_string_reparse
f16a9f57 10900 = current_language->watch_location_expression (t, addr).release ();
06a64a0b 10901
3a5c3e22 10902 w->exp_string = xstrprintf ("-location %.*s",
d63d0675 10903 (int) (exp_end - exp_start), exp_start);
06a64a0b
TT
10904 }
10905 else
3a5c3e22 10906 w->exp_string = savestring (exp_start, exp_end - exp_start);
9c06b0b4
TJB
10907
10908 if (use_mask)
10909 {
3a5c3e22 10910 w->hw_wp_mask = mask;
9c06b0b4
TJB
10911 }
10912 else
10913 {
3a5c3e22 10914 w->val = val;
bb9d5f81
PP
10915 w->val_bitpos = saved_bitpos;
10916 w->val_bitsize = saved_bitsize;
4c1d86d9 10917 w->val_valid = true;
9c06b0b4 10918 }
77b06cd7 10919
c906108c 10920 if (cond_start)
c1fc2657 10921 w->cond_string = savestring (cond_start, cond_end - cond_start);
c906108c 10922 else
c1fc2657 10923 w->cond_string = 0;
c5aa993b 10924
441d7c93 10925 if (frame_id_p (watchpoint_frame))
f6bc2008 10926 {
441d7c93 10927 w->watchpoint_frame = watchpoint_frame;
3a5c3e22 10928 w->watchpoint_thread = inferior_ptid;
f6bc2008 10929 }
c906108c 10930 else
f6bc2008 10931 {
3a5c3e22
PA
10932 w->watchpoint_frame = null_frame_id;
10933 w->watchpoint_thread = null_ptid;
f6bc2008 10934 }
c906108c 10935
d983da9c 10936 if (scope_breakpoint != NULL)
c906108c 10937 {
d983da9c
DJ
10938 /* The scope breakpoint is related to the watchpoint. We will
10939 need to act on them together. */
c1fc2657 10940 w->related_breakpoint = scope_breakpoint;
b270e6f9 10941 scope_breakpoint->related_breakpoint = w.get ();
c906108c 10942 }
d983da9c 10943
06a64a0b
TT
10944 if (!just_location)
10945 value_free_to_mark (mark);
2d134ed3 10946
b270e6f9
TT
10947 /* Finally update the new watchpoint. This creates the locations
10948 that should be inserted. */
10949 update_watchpoint (w.get (), 1);
a9634178 10950
b270e6f9 10951 install_breakpoint (internal, std::move (w), 1);
c906108c
SS
10952}
10953
e09342b5 10954/* Return count of debug registers needed to watch the given expression.
e09342b5 10955 If the watchpoint cannot be handled in hardware return zero. */
c906108c 10956
c906108c 10957static int
a6535de1 10958can_use_hardware_watchpoint (const std::vector<value_ref_ptr> &vals)
c906108c
SS
10959{
10960 int found_memory_cnt = 0;
10961
10962 /* Did the user specifically forbid us to use hardware watchpoints? */
c5aa993b 10963 if (!can_use_hw_watchpoints)
c906108c 10964 return 0;
c5aa993b 10965
a6535de1
TT
10966 gdb_assert (!vals.empty ());
10967 struct value *head = vals[0].get ();
10968
5c44784c
JM
10969 /* Make sure that the value of the expression depends only upon
10970 memory contents, and values computed from them within GDB. If we
10971 find any register references or function calls, we can't use a
10972 hardware watchpoint.
10973
10974 The idea here is that evaluating an expression generates a series
10975 of values, one holding the value of every subexpression. (The
10976 expression a*b+c has five subexpressions: a, b, a*b, c, and
10977 a*b+c.) GDB's values hold almost enough information to establish
10978 the criteria given above --- they identify memory lvalues,
10979 register lvalues, computed values, etcetera. So we can evaluate
10980 the expression, and then scan the chain of values that leaves
10981 behind to decide whether we can detect any possible change to the
10982 expression's final value using only hardware watchpoints.
10983
10984 However, I don't think that the values returned by inferior
10985 function calls are special in any way. So this function may not
10986 notice that an expression involving an inferior function call
10987 can't be watched with hardware watchpoints. FIXME. */
a6535de1 10988 for (const value_ref_ptr &iter : vals)
c906108c 10989 {
a6535de1
TT
10990 struct value *v = iter.get ();
10991
5c44784c 10992 if (VALUE_LVAL (v) == lval_memory)
c906108c 10993 {
8464be76
DJ
10994 if (v != head && value_lazy (v))
10995 /* A lazy memory lvalue in the chain is one that GDB never
10996 needed to fetch; we either just used its address (e.g.,
10997 `a' in `a.b') or we never needed it at all (e.g., `a'
10998 in `a,b'). This doesn't apply to HEAD; if that is
10999 lazy then it was not readable, but watch it anyway. */
5c44784c 11000 ;
53a5351d 11001 else
5c44784c
JM
11002 {
11003 /* Ahh, memory we actually used! Check if we can cover
dda83cd7 11004 it with hardware watchpoints. */
df407dfe 11005 struct type *vtype = check_typedef (value_type (v));
2e70b7b9
MS
11006
11007 /* We only watch structs and arrays if user asked for it
11008 explicitly, never if they just happen to appear in a
11009 middle of some value chain. */
11010 if (v == head
78134374
SM
11011 || (vtype->code () != TYPE_CODE_STRUCT
11012 && vtype->code () != TYPE_CODE_ARRAY))
2e70b7b9 11013 {
42ae5230 11014 CORE_ADDR vaddr = value_address (v);
e09342b5
TJB
11015 int len;
11016 int num_regs;
11017
a9634178 11018 len = (target_exact_watchpoints
e09342b5
TJB
11019 && is_scalar_type_recursive (vtype))?
11020 1 : TYPE_LENGTH (value_type (v));
2e70b7b9 11021
e09342b5
TJB
11022 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
11023 if (!num_regs)
2e70b7b9
MS
11024 return 0;
11025 else
e09342b5 11026 found_memory_cnt += num_regs;
2e70b7b9 11027 }
5c44784c 11028 }
c5aa993b 11029 }
5086187c
AC
11030 else if (VALUE_LVAL (v) != not_lval
11031 && deprecated_value_modifiable (v) == 0)
38b6c3b3 11032 return 0; /* These are values from the history (e.g., $1). */
5086187c 11033 else if (VALUE_LVAL (v) == lval_register)
38b6c3b3 11034 return 0; /* Cannot watch a register with a HW watchpoint. */
c906108c
SS
11035 }
11036
11037 /* The expression itself looks suitable for using a hardware
11038 watchpoint, but give the target machine a chance to reject it. */
11039 return found_memory_cnt;
11040}
11041
8b93c638 11042void
2e362716 11043watch_command_wrapper (const char *arg, int from_tty, bool internal)
8b93c638 11044{
84f4c1fe 11045 watch_command_1 (arg, hw_write, from_tty, 0, internal);
06a64a0b
TT
11046}
11047
a15a5258
AB
11048/* Options for the watch, awatch, and rwatch commands. */
11049
11050struct watch_options
11051{
11052 /* For -location. */
11053 bool location = false;
11054};
11055
11056/* Definitions of options for the "watch", "awatch", and "rwatch" commands.
11057
11058 Historically GDB always accepted both '-location' and '-l' flags for
11059 these commands (both flags being synonyms). When converting to the
11060 newer option scheme only '-location' is added here. That's fine (for
11061 backward compatibility) as any non-ambiguous prefix of a flag will be
11062 accepted, so '-l', '-loc', are now all accepted.
11063
11064 What this means is that, if in the future, we add any new flag here
11065 that starts with '-l' then this will break backward compatibility, so
11066 please, don't do that! */
11067
11068static const gdb::option::option_def watch_option_defs[] = {
11069 gdb::option::flag_option_def<watch_options> {
11070 "location",
11071 [] (watch_options *opt) { return &opt->location; },
11072 N_("\
11073This evaluates EXPRESSION and watches the memory to which is refers.\n\
11074-l can be used as a short form of -location."),
11075 },
11076};
11077
11078/* Returns the option group used by 'watch', 'awatch', and 'rwatch'
11079 commands. */
11080
11081static gdb::option::option_def_group
11082make_watch_options_def_group (watch_options *opts)
11083{
11084 return {{watch_option_defs}, opts};
11085}
11086
06a64a0b
TT
11087/* A helper function that looks for the "-location" argument and then
11088 calls watch_command_1. */
11089
11090static void
0b39b52e 11091watch_maybe_just_location (const char *arg, int accessflag, int from_tty)
06a64a0b 11092{
a15a5258
AB
11093 watch_options opts;
11094 auto grp = make_watch_options_def_group (&opts);
11095 gdb::option::process_options
11096 (&arg, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND, grp);
11097 if (arg != nullptr && *arg == '\0')
11098 arg = nullptr;
11099
11100 watch_command_1 (arg, accessflag, from_tty, opts.location, false);
11101}
06a64a0b 11102
a15a5258
AB
11103/* Command completion for 'watch', 'awatch', and 'rwatch' commands. */
11104static void
11105watch_command_completer (struct cmd_list_element *ignore,
11106 completion_tracker &tracker,
11107 const char *text, const char * /*word*/)
11108{
11109 const auto group = make_watch_options_def_group (nullptr);
11110 if (gdb::option::complete_options
11111 (tracker, &text, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND, group))
11112 return;
06a64a0b 11113
a15a5258
AB
11114 const char *word = advance_to_expression_complete_word_point (tracker, text);
11115 expression_completer (ignore, tracker, text, word);
8b93c638 11116}
8926118c 11117
c5aa993b 11118static void
0b39b52e 11119watch_command (const char *arg, int from_tty)
c906108c 11120{
06a64a0b 11121 watch_maybe_just_location (arg, hw_write, from_tty);
c906108c
SS
11122}
11123
8b93c638 11124void
2e362716 11125rwatch_command_wrapper (const char *arg, int from_tty, bool internal)
8b93c638 11126{
84f4c1fe 11127 watch_command_1 (arg, hw_read, from_tty, 0, internal);
8b93c638 11128}
8926118c 11129
c5aa993b 11130static void
0b39b52e 11131rwatch_command (const char *arg, int from_tty)
c906108c 11132{
06a64a0b 11133 watch_maybe_just_location (arg, hw_read, from_tty);
c906108c
SS
11134}
11135
8b93c638 11136void
2e362716 11137awatch_command_wrapper (const char *arg, int from_tty, bool internal)
8b93c638 11138{
84f4c1fe 11139 watch_command_1 (arg, hw_access, from_tty, 0, internal);
8b93c638 11140}
8926118c 11141
c5aa993b 11142static void
0b39b52e 11143awatch_command (const char *arg, int from_tty)
c906108c 11144{
06a64a0b 11145 watch_maybe_just_location (arg, hw_access, from_tty);
c906108c 11146}
c906108c 11147\f
c5aa993b 11148
cfc31633
PA
11149/* Data for the FSM that manages the until(location)/advance commands
11150 in infcmd.c. Here because it uses the mechanisms of
11151 breakpoints. */
c906108c 11152
46e3ed7f 11153struct until_break_fsm : public thread_fsm
bfec99b2 11154{
46e3ed7f 11155 /* The thread that was current when the command was executed. */
cfc31633
PA
11156 int thread;
11157
df631783
PA
11158 /* The breakpoint set at the return address in the caller frame,
11159 plus breakpoints at all the destination locations. */
11160 std::vector<breakpoint_up> breakpoints;
cfc31633 11161
46e3ed7f 11162 until_break_fsm (struct interp *cmd_interp, int thread,
df631783 11163 std::vector<breakpoint_up> &&breakpoints)
46e3ed7f
TT
11164 : thread_fsm (cmd_interp),
11165 thread (thread),
df631783 11166 breakpoints (std::move (breakpoints))
46e3ed7f
TT
11167 {
11168 }
cfc31633 11169
46e3ed7f
TT
11170 void clean_up (struct thread_info *thread) override;
11171 bool should_stop (struct thread_info *thread) override;
11172 enum async_reply_reason do_async_reply_reason () override;
cfc31633
PA
11173};
11174
cfc31633
PA
11175/* Implementation of the 'should_stop' FSM method for the
11176 until(location)/advance commands. */
11177
46e3ed7f
TT
11178bool
11179until_break_fsm::should_stop (struct thread_info *tp)
cfc31633 11180{
df631783
PA
11181 for (const breakpoint_up &bp : breakpoints)
11182 if (bpstat_find_breakpoint (tp->control.stop_bpstat,
11183 bp.get ()) != NULL)
11184 {
11185 set_finished ();
11186 break;
11187 }
cfc31633 11188
46e3ed7f 11189 return true;
cfc31633
PA
11190}
11191
11192/* Implementation of the 'clean_up' FSM method for the
11193 until(location)/advance commands. */
11194
46e3ed7f
TT
11195void
11196until_break_fsm::clean_up (struct thread_info *)
43ff13b4 11197{
cfc31633 11198 /* Clean up our temporary breakpoints. */
df631783 11199 breakpoints.clear ();
46e3ed7f 11200 delete_longjmp_breakpoint (thread);
cfc31633
PA
11201}
11202
11203/* Implementation of the 'async_reply_reason' FSM method for the
11204 until(location)/advance commands. */
11205
46e3ed7f
TT
11206enum async_reply_reason
11207until_break_fsm::do_async_reply_reason ()
cfc31633
PA
11208{
11209 return EXEC_ASYNC_LOCATION_REACHED;
43ff13b4
JM
11210}
11211
c906108c 11212void
f2fc3015 11213until_break_command (const char *arg, int from_tty, int anywhere)
c906108c 11214{
8556afb4
PA
11215 struct frame_info *frame;
11216 struct gdbarch *frame_gdbarch;
11217 struct frame_id stack_frame_id;
11218 struct frame_id caller_frame_id;
186c406b
TT
11219 int thread;
11220 struct thread_info *tp;
c906108c 11221
70509625 11222 clear_proceed_status (0);
c906108c
SS
11223
11224 /* Set a breakpoint where the user wants it and at return from
4a64f543 11225 this function. */
c5aa993b 11226
ffc2605c 11227 event_location_up location = string_to_event_location (&arg, current_language);
f00aae0f 11228
6c5b2ebe
PA
11229 std::vector<symtab_and_line> sals
11230 = (last_displayed_sal_is_valid ()
11231 ? decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
11232 get_last_displayed_symtab (),
11233 get_last_displayed_line ())
11234 : decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE,
cafb3438 11235 NULL, NULL, 0));
c5aa993b 11236
df631783 11237 if (sals.empty ())
8a3fe4f8 11238 error (_("Couldn't get information on specified line."));
c5aa993b 11239
c906108c 11240 if (*arg)
8a3fe4f8 11241 error (_("Junk at end of arguments."));
c5aa993b 11242
186c406b 11243 tp = inferior_thread ();
5d5658a1 11244 thread = tp->global_num;
186c406b 11245
8556afb4
PA
11246 /* Note linespec handling above invalidates the frame chain.
11247 Installing a breakpoint also invalidates the frame chain (as it
11248 may need to switch threads), so do any frame handling before
11249 that. */
11250
11251 frame = get_selected_frame (NULL);
11252 frame_gdbarch = get_frame_arch (frame);
11253 stack_frame_id = get_stack_frame_id (frame);
11254 caller_frame_id = frame_unwind_caller_id (frame);
883bc8d1 11255
ae66c1fc
EZ
11256 /* Keep within the current frame, or in frames called by the current
11257 one. */
edb3359d 11258
df631783 11259 std::vector<breakpoint_up> breakpoints;
5419bdae
TT
11260
11261 gdb::optional<delete_longjmp_breakpoint_cleanup> lj_deleter;
11262
883bc8d1 11263 if (frame_id_p (caller_frame_id))
c906108c 11264 {
883bc8d1 11265 struct symtab_and_line sal2;
cfc31633 11266 struct gdbarch *caller_gdbarch;
883bc8d1
PA
11267
11268 sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
11269 sal2.pc = frame_unwind_caller_pc (frame);
cfc31633 11270 caller_gdbarch = frame_unwind_caller_arch (frame);
df631783
PA
11271
11272 breakpoint_up caller_breakpoint
11273 = set_momentary_breakpoint (caller_gdbarch, sal2,
11274 caller_frame_id, bp_until);
11275 breakpoints.emplace_back (std::move (caller_breakpoint));
186c406b 11276
883bc8d1 11277 set_longjmp_breakpoint (tp, caller_frame_id);
5419bdae 11278 lj_deleter.emplace (thread);
c906108c 11279 }
c5aa993b 11280
c70a6932
JK
11281 /* set_momentary_breakpoint could invalidate FRAME. */
11282 frame = NULL;
11283
df631783
PA
11284 /* If the user told us to continue until a specified location, we
11285 don't specify a frame at which we need to stop. Otherwise,
11286 specify the selected frame, because we want to stop only at the
11287 very same frame. */
11288 frame_id stop_frame_id = anywhere ? null_frame_id : stack_frame_id;
11289
11290 for (symtab_and_line &sal : sals)
11291 {
11292 resolve_sal_pc (&sal);
11293
11294 breakpoint_up location_breakpoint
11295 = set_momentary_breakpoint (frame_gdbarch, sal,
11296 stop_frame_id, bp_until);
11297 breakpoints.emplace_back (std::move (location_breakpoint));
11298 }
883bc8d1 11299
46e3ed7f 11300 tp->thread_fsm = new until_break_fsm (command_interp (), tp->global_num,
df631783 11301 std::move (breakpoints));
f107f563 11302
5419bdae
TT
11303 if (lj_deleter)
11304 lj_deleter->release ();
f107f563 11305
cfc31633 11306 proceed (-1, GDB_SIGNAL_DEFAULT);
c906108c 11307}
ae66c1fc 11308
c906108c
SS
11309/* This function attempts to parse an optional "if <cond>" clause
11310 from the arg string. If one is not found, it returns NULL.
c5aa993b 11311
c906108c
SS
11312 Else, it returns a pointer to the condition string. (It does not
11313 attempt to evaluate the string against a particular block.) And,
11314 it updates arg to point to the first character following the parsed
4a64f543 11315 if clause in the arg string. */
53a5351d 11316
63160a43
PA
11317const char *
11318ep_parse_optional_if_clause (const char **arg)
c906108c 11319{
63160a43 11320 const char *cond_string;
c5aa993b
JM
11321
11322 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
c906108c 11323 return NULL;
c5aa993b 11324
4a64f543 11325 /* Skip the "if" keyword. */
c906108c 11326 (*arg) += 2;
c5aa993b 11327
c906108c 11328 /* Skip any extra leading whitespace, and record the start of the
4a64f543 11329 condition string. */
f1735a53 11330 *arg = skip_spaces (*arg);
c906108c 11331 cond_string = *arg;
c5aa993b 11332
4a64f543
MS
11333 /* Assume that the condition occupies the remainder of the arg
11334 string. */
c906108c 11335 (*arg) += strlen (cond_string);
c5aa993b 11336
c906108c
SS
11337 return cond_string;
11338}
c5aa993b 11339
c906108c
SS
11340/* Commands to deal with catching events, such as signals, exceptions,
11341 process start/exit, etc. */
c5aa993b
JM
11342
11343typedef enum
11344{
44feb3ce
TT
11345 catch_fork_temporary, catch_vfork_temporary,
11346 catch_fork_permanent, catch_vfork_permanent
c5aa993b
JM
11347}
11348catch_fork_kind;
11349
c906108c 11350static void
eb4c3f4a 11351catch_fork_command_1 (const char *arg, int from_tty,
cc59ec59 11352 struct cmd_list_element *command)
c906108c 11353{
a6d9a66e 11354 struct gdbarch *gdbarch = get_current_arch ();
63160a43 11355 const char *cond_string = NULL;
44feb3ce 11356 catch_fork_kind fork_kind;
44feb3ce
TT
11357
11358 fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
b650a282
SM
11359 bool temp = (fork_kind == catch_fork_temporary
11360 || fork_kind == catch_vfork_temporary);
c5aa993b 11361
44feb3ce
TT
11362 if (!arg)
11363 arg = "";
f1735a53 11364 arg = skip_spaces (arg);
c5aa993b 11365
c906108c 11366 /* The allowed syntax is:
c5aa993b
JM
11367 catch [v]fork
11368 catch [v]fork if <cond>
11369
4a64f543 11370 First, check if there's an if clause. */
c906108c 11371 cond_string = ep_parse_optional_if_clause (&arg);
c5aa993b 11372
c906108c 11373 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 11374 error (_("Junk at end of arguments."));
c5aa993b 11375
c906108c 11376 /* If this target supports it, create a fork or vfork catchpoint
4a64f543 11377 and enable reporting of such events. */
c5aa993b
JM
11378 switch (fork_kind)
11379 {
44feb3ce
TT
11380 case catch_fork_temporary:
11381 case catch_fork_permanent:
b650a282 11382 create_fork_vfork_event_catchpoint (gdbarch, temp, cond_string,
dda83cd7 11383 &catch_fork_breakpoint_ops);
c906108c 11384 break;
44feb3ce
TT
11385 case catch_vfork_temporary:
11386 case catch_vfork_permanent:
b650a282 11387 create_fork_vfork_event_catchpoint (gdbarch, temp, cond_string,
dda83cd7 11388 &catch_vfork_breakpoint_ops);
c906108c 11389 break;
c5aa993b 11390 default:
8a3fe4f8 11391 error (_("unsupported or unknown fork kind; cannot catch it"));
c906108c 11392 break;
c5aa993b 11393 }
c906108c
SS
11394}
11395
11396static void
eb4c3f4a 11397catch_exec_command_1 (const char *arg, int from_tty,
cc59ec59 11398 struct cmd_list_element *command)
c906108c 11399{
a6d9a66e 11400 struct gdbarch *gdbarch = get_current_arch ();
63160a43 11401 const char *cond_string = NULL;
b650a282 11402 bool temp = get_cmd_context (command) == CATCH_TEMPORARY;
44feb3ce
TT
11403
11404 if (!arg)
11405 arg = "";
f1735a53 11406 arg = skip_spaces (arg);
c906108c
SS
11407
11408 /* The allowed syntax is:
c5aa993b
JM
11409 catch exec
11410 catch exec if <cond>
c906108c 11411
4a64f543 11412 First, check if there's an if clause. */
c906108c
SS
11413 cond_string = ep_parse_optional_if_clause (&arg);
11414
11415 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 11416 error (_("Junk at end of arguments."));
c906108c 11417
b270e6f9 11418 std::unique_ptr<exec_catchpoint> c (new exec_catchpoint ());
b650a282 11419 init_catchpoint (c.get (), gdbarch, temp, cond_string,
b4d90040
PA
11420 &catch_exec_breakpoint_ops);
11421 c->exec_pathname = NULL;
11422
b270e6f9 11423 install_breakpoint (0, std::move (c), 1);
c906108c 11424}
c5aa993b 11425
9ac4176b 11426void
28010a5d
PA
11427init_ada_exception_breakpoint (struct breakpoint *b,
11428 struct gdbarch *gdbarch,
11429 struct symtab_and_line sal,
f2fc3015 11430 const char *addr_string,
c0a91b2b 11431 const struct breakpoint_ops *ops,
28010a5d 11432 int tempflag,
349774ef 11433 int enabled,
28010a5d 11434 int from_tty)
f7f9143b 11435{
f7f9143b
JB
11436 if (from_tty)
11437 {
5af949e3
UW
11438 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
11439 if (!loc_gdbarch)
11440 loc_gdbarch = gdbarch;
11441
6c95b8df
PA
11442 describe_other_breakpoints (loc_gdbarch,
11443 sal.pspace, sal.pc, sal.section, -1);
f7f9143b 11444 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
dda83cd7
SM
11445 version for exception catchpoints, because two catchpoints
11446 used for different exception names will use the same address.
11447 In this case, a "breakpoint ... also set at..." warning is
11448 unproductive. Besides, the warning phrasing is also a bit
11449 inappropriate, we should use the word catchpoint, and tell
11450 the user what type of catchpoint it is. The above is good
11451 enough for now, though. */
f7f9143b
JB
11452 }
11453
f06f1252 11454 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
f7f9143b 11455
349774ef 11456 b->enable_state = enabled ? bp_enabled : bp_disabled;
f7f9143b 11457 b->disposition = tempflag ? disp_del : disp_donttouch;
d28cd78a
TT
11458 b->location = string_to_event_location (&addr_string,
11459 language_def (language_ada));
f7f9143b 11460 b->language = language_ada;
f7f9143b
JB
11461}
11462
c906108c
SS
11463\f
11464
81b1e71c 11465/* Compare two breakpoints and return a strcmp-like result. */
8a2c437b
TT
11466
11467static int
81b1e71c 11468compare_breakpoints (const breakpoint *a, const breakpoint *b)
8a2c437b 11469{
81b1e71c
TT
11470 uintptr_t ua = (uintptr_t) a;
11471 uintptr_t ub = (uintptr_t) b;
8a2c437b 11472
81b1e71c 11473 if (a->number < b->number)
8a2c437b 11474 return -1;
81b1e71c 11475 else if (a->number > b->number)
8a2c437b
TT
11476 return 1;
11477
11478 /* Now sort by address, in case we see, e..g, two breakpoints with
11479 the number 0. */
11480 if (ua < ub)
11481 return -1;
94b0e70d 11482 return ua > ub ? 1 : 0;
8a2c437b
TT
11483}
11484
80f8a6eb 11485/* Delete breakpoints by address or line. */
c906108c
SS
11486
11487static void
0b39b52e 11488clear_command (const char *arg, int from_tty)
c906108c 11489{
81b1e71c 11490 struct breakpoint *b;
c906108c 11491 int default_match;
c906108c 11492
6c5b2ebe
PA
11493 std::vector<symtab_and_line> decoded_sals;
11494 symtab_and_line last_sal;
11495 gdb::array_view<symtab_and_line> sals;
c906108c
SS
11496 if (arg)
11497 {
6c5b2ebe
PA
11498 decoded_sals
11499 = decode_line_with_current_source (arg,
11500 (DECODE_LINE_FUNFIRSTLINE
11501 | DECODE_LINE_LIST_MODE));
c906108c 11502 default_match = 0;
6c5b2ebe 11503 sals = decoded_sals;
c906108c
SS
11504 }
11505 else
11506 {
1bfeeb0f
JL
11507 /* Set sal's line, symtab, pc, and pspace to the values
11508 corresponding to the last call to print_frame_info. If the
11509 codepoint is not valid, this will set all the fields to 0. */
51abb421 11510 last_sal = get_last_displayed_sal ();
6c5b2ebe 11511 if (last_sal.symtab == 0)
8a3fe4f8 11512 error (_("No source file specified."));
c906108c 11513
c906108c 11514 default_match = 1;
6c5b2ebe 11515 sals = last_sal;
c906108c
SS
11516 }
11517
4a64f543
MS
11518 /* We don't call resolve_sal_pc here. That's not as bad as it
11519 seems, because all existing breakpoints typically have both
11520 file/line and pc set. So, if clear is given file/line, we can
11521 match this to existing breakpoint without obtaining pc at all.
ed0616c6
VP
11522
11523 We only support clearing given the address explicitly
11524 present in breakpoint table. Say, we've set breakpoint
4a64f543 11525 at file:line. There were several PC values for that file:line,
ed0616c6 11526 due to optimization, all in one block.
4a64f543
MS
11527
11528 We've picked one PC value. If "clear" is issued with another
ed0616c6
VP
11529 PC corresponding to the same file:line, the breakpoint won't
11530 be cleared. We probably can still clear the breakpoint, but
11531 since the other PC value is never presented to user, user
11532 can only find it by guessing, and it does not seem important
11533 to support that. */
11534
4a64f543
MS
11535 /* For each line spec given, delete bps which correspond to it. Do
11536 it in two passes, solely to preserve the current behavior that
11537 from_tty is forced true if we delete more than one
11538 breakpoint. */
c906108c 11539
81b1e71c 11540 std::vector<struct breakpoint *> found;
6c5b2ebe 11541 for (const auto &sal : sals)
c906108c 11542 {
05cba821
JK
11543 const char *sal_fullname;
11544
c906108c 11545 /* If exact pc given, clear bpts at that pc.
dda83cd7
SM
11546 If line given (pc == 0), clear all bpts on specified line.
11547 If defaulting, clear all bpts on default line
11548 or at default pc.
c5aa993b 11549
dda83cd7 11550 defaulting sal.pc != 0 tests to do
c5aa993b 11551
dda83cd7
SM
11552 0 1 pc
11553 1 1 pc _and_ line
11554 0 0 line
11555 1 0 <can't happen> */
c906108c 11556
05cba821
JK
11557 sal_fullname = (sal.symtab == NULL
11558 ? NULL : symtab_to_fullname (sal.symtab));
c906108c 11559
4a64f543 11560 /* Find all matching breakpoints and add them to 'found'. */
d6e956e5 11561 ALL_BREAKPOINTS (b)
c5aa993b 11562 {
0d381245 11563 int match = 0;
4a64f543 11564 /* Are we going to delete b? */
cc60f2e3 11565 if (b->type != bp_none && !is_watchpoint (b))
0d381245
VP
11566 {
11567 struct bp_location *loc = b->loc;
11568 for (; loc; loc = loc->next)
11569 {
f8eba3c6
TT
11570 /* If the user specified file:line, don't allow a PC
11571 match. This matches historical gdb behavior. */
11572 int pc_match = (!sal.explicit_line
11573 && sal.pc
11574 && (loc->pspace == sal.pspace)
11575 && (loc->address == sal.pc)
11576 && (!section_is_overlay (loc->section)
11577 || loc->section == sal.section));
4aac40c8
TT
11578 int line_match = 0;
11579
11580 if ((default_match || sal.explicit_line)
2f202fde 11581 && loc->symtab != NULL
05cba821 11582 && sal_fullname != NULL
4aac40c8 11583 && sal.pspace == loc->pspace
05cba821
JK
11584 && loc->line_number == sal.line
11585 && filename_cmp (symtab_to_fullname (loc->symtab),
11586 sal_fullname) == 0)
11587 line_match = 1;
4aac40c8 11588
0d381245
VP
11589 if (pc_match || line_match)
11590 {
11591 match = 1;
11592 break;
11593 }
11594 }
11595 }
11596
11597 if (match)
81b1e71c 11598 found.push_back (b);
c906108c 11599 }
80f8a6eb 11600 }
8a2c437b 11601
80f8a6eb 11602 /* Now go thru the 'found' chain and delete them. */
81b1e71c 11603 if (found.empty ())
80f8a6eb
MS
11604 {
11605 if (arg)
8a3fe4f8 11606 error (_("No breakpoint at %s."), arg);
80f8a6eb 11607 else
8a3fe4f8 11608 error (_("No breakpoint at this line."));
80f8a6eb 11609 }
c906108c 11610
8a2c437b 11611 /* Remove duplicates from the vec. */
81b1e71c 11612 std::sort (found.begin (), found.end (),
b926417a 11613 [] (const breakpoint *bp_a, const breakpoint *bp_b)
81b1e71c 11614 {
b926417a 11615 return compare_breakpoints (bp_a, bp_b) < 0;
81b1e71c
TT
11616 });
11617 found.erase (std::unique (found.begin (), found.end (),
b926417a 11618 [] (const breakpoint *bp_a, const breakpoint *bp_b)
81b1e71c 11619 {
b926417a 11620 return compare_breakpoints (bp_a, bp_b) == 0;
81b1e71c
TT
11621 }),
11622 found.end ());
8a2c437b 11623
81b1e71c 11624 if (found.size () > 1)
4a64f543 11625 from_tty = 1; /* Always report if deleted more than one. */
80f8a6eb 11626 if (from_tty)
a3f17187 11627 {
81b1e71c 11628 if (found.size () == 1)
a3f17187
AC
11629 printf_unfiltered (_("Deleted breakpoint "));
11630 else
11631 printf_unfiltered (_("Deleted breakpoints "));
11632 }
d6e956e5 11633
81b1e71c 11634 for (breakpoint *iter : found)
80f8a6eb 11635 {
c5aa993b 11636 if (from_tty)
81b1e71c
TT
11637 printf_unfiltered ("%d ", iter->number);
11638 delete_breakpoint (iter);
c906108c 11639 }
80f8a6eb
MS
11640 if (from_tty)
11641 putchar_unfiltered ('\n');
c906108c
SS
11642}
11643\f
11644/* Delete breakpoint in BS if they are `delete' breakpoints and
11645 all breakpoints that are marked for deletion, whether hit or not.
11646 This is called after any breakpoint is hit, or after errors. */
11647
11648void
fba45db2 11649breakpoint_auto_delete (bpstat bs)
c906108c 11650{
35df4500 11651 struct breakpoint *b, *b_tmp;
c906108c
SS
11652
11653 for (; bs; bs = bs->next)
f431efe5
PA
11654 if (bs->breakpoint_at
11655 && bs->breakpoint_at->disposition == disp_del
c906108c 11656 && bs->stop)
f431efe5 11657 delete_breakpoint (bs->breakpoint_at);
c906108c 11658
35df4500 11659 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 11660 {
b5de0fa7 11661 if (b->disposition == disp_del_at_next_stop)
c5aa993b
JM
11662 delete_breakpoint (b);
11663 }
c906108c
SS
11664}
11665
4a64f543 11666/* A comparison function for bp_location AP and BP being interfaced to
39ef2f62 11667 std::sort. Sort elements primarily by their ADDRESS (no matter what
cb1e4e32
PA
11668 bl_address_is_meaningful says), secondarily by ordering first
11669 permanent elements and terciarily just ensuring the array is sorted
39ef2f62 11670 stable way despite std::sort being an unstable algorithm. */
876fa593
JK
11671
11672static int
39ef2f62 11673bp_location_is_less_than (const bp_location *a, const bp_location *b)
876fa593 11674{
876fa593 11675 if (a->address != b->address)
39ef2f62 11676 return a->address < b->address;
876fa593 11677
dea2aa5f
LM
11678 /* Sort locations at the same address by their pspace number, keeping
11679 locations of the same inferior (in a multi-inferior environment)
11680 grouped. */
11681
11682 if (a->pspace->num != b->pspace->num)
39ef2f62 11683 return a->pspace->num < b->pspace->num;
dea2aa5f 11684
876fa593 11685 /* Sort permanent breakpoints first. */
1a853c52 11686 if (a->permanent != b->permanent)
39ef2f62 11687 return a->permanent > b->permanent;
876fa593 11688
7f32a4d5
PA
11689 /* Sort by type in order to make duplicate determination easier.
11690 See update_global_location_list. This is kept in sync with
11691 breakpoint_locations_match. */
11692 if (a->loc_type < b->loc_type)
11693 return true;
11694
11695 /* Likewise, for range-breakpoints, sort by length. */
11696 if (a->loc_type == bp_loc_hardware_breakpoint
11697 && b->loc_type == bp_loc_hardware_breakpoint
11698 && a->length < b->length)
11699 return true;
11700
c56a97f9
JK
11701 /* Make the internal GDB representation stable across GDB runs
11702 where A and B memory inside GDB can differ. Breakpoint locations of
11703 the same type at the same address can be sorted in arbitrary order. */
876fa593
JK
11704
11705 if (a->owner->number != b->owner->number)
39ef2f62 11706 return a->owner->number < b->owner->number;
876fa593 11707
39ef2f62 11708 return a < b;
876fa593
JK
11709}
11710
f5336ca5
PA
11711/* Set bp_locations_placed_address_before_address_max and
11712 bp_locations_shadow_len_after_address_max according to the current
11713 content of the bp_locations array. */
f7545552
TT
11714
11715static void
f5336ca5 11716bp_locations_target_extensions_update (void)
f7545552 11717{
876fa593
JK
11718 struct bp_location *bl, **blp_tmp;
11719
f5336ca5
PA
11720 bp_locations_placed_address_before_address_max = 0;
11721 bp_locations_shadow_len_after_address_max = 0;
876fa593
JK
11722
11723 ALL_BP_LOCATIONS (bl, blp_tmp)
11724 {
11725 CORE_ADDR start, end, addr;
11726
11727 if (!bp_location_has_shadow (bl))
11728 continue;
11729
11730 start = bl->target_info.placed_address;
11731 end = start + bl->target_info.shadow_len;
11732
11733 gdb_assert (bl->address >= start);
11734 addr = bl->address - start;
f5336ca5
PA
11735 if (addr > bp_locations_placed_address_before_address_max)
11736 bp_locations_placed_address_before_address_max = addr;
876fa593
JK
11737
11738 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
11739
11740 gdb_assert (bl->address < end);
11741 addr = end - bl->address;
f5336ca5
PA
11742 if (addr > bp_locations_shadow_len_after_address_max)
11743 bp_locations_shadow_len_after_address_max = addr;
876fa593 11744 }
f7545552
TT
11745}
11746
1e4d1764
YQ
11747/* Download tracepoint locations if they haven't been. */
11748
11749static void
11750download_tracepoint_locations (void)
11751{
7ed2c994 11752 struct breakpoint *b;
dd2e65cc 11753 enum tribool can_download_tracepoint = TRIBOOL_UNKNOWN;
1e4d1764 11754
5ed8105e 11755 scoped_restore_current_pspace_and_thread restore_pspace_thread;
1e4d1764 11756
7ed2c994 11757 ALL_TRACEPOINTS (b)
1e4d1764 11758 {
7ed2c994 11759 struct bp_location *bl;
1e4d1764 11760 struct tracepoint *t;
f2a8bc8a 11761 int bp_location_downloaded = 0;
1e4d1764 11762
7ed2c994 11763 if ((b->type == bp_fast_tracepoint
1e4d1764
YQ
11764 ? !may_insert_fast_tracepoints
11765 : !may_insert_tracepoints))
11766 continue;
11767
dd2e65cc
YQ
11768 if (can_download_tracepoint == TRIBOOL_UNKNOWN)
11769 {
11770 if (target_can_download_tracepoint ())
11771 can_download_tracepoint = TRIBOOL_TRUE;
11772 else
11773 can_download_tracepoint = TRIBOOL_FALSE;
11774 }
11775
11776 if (can_download_tracepoint == TRIBOOL_FALSE)
11777 break;
11778
7ed2c994
YQ
11779 for (bl = b->loc; bl; bl = bl->next)
11780 {
11781 /* In tracepoint, locations are _never_ duplicated, so
11782 should_be_inserted is equivalent to
11783 unduplicated_should_be_inserted. */
11784 if (!should_be_inserted (bl) || bl->inserted)
11785 continue;
1e4d1764 11786
7ed2c994 11787 switch_to_program_space_and_thread (bl->pspace);
1e4d1764 11788
7ed2c994 11789 target_download_tracepoint (bl);
1e4d1764 11790
7ed2c994 11791 bl->inserted = 1;
f2a8bc8a 11792 bp_location_downloaded = 1;
7ed2c994
YQ
11793 }
11794 t = (struct tracepoint *) b;
11795 t->number_on_target = b->number;
f2a8bc8a 11796 if (bp_location_downloaded)
76727919 11797 gdb::observers::breakpoint_modified.notify (b);
1e4d1764 11798 }
1e4d1764
YQ
11799}
11800
934709f0
PW
11801/* Swap the insertion/duplication state between two locations. */
11802
11803static void
11804swap_insertion (struct bp_location *left, struct bp_location *right)
11805{
11806 const int left_inserted = left->inserted;
11807 const int left_duplicate = left->duplicate;
b775012e 11808 const int left_needs_update = left->needs_update;
934709f0
PW
11809 const struct bp_target_info left_target_info = left->target_info;
11810
1e4d1764
YQ
11811 /* Locations of tracepoints can never be duplicated. */
11812 if (is_tracepoint (left->owner))
11813 gdb_assert (!left->duplicate);
11814 if (is_tracepoint (right->owner))
11815 gdb_assert (!right->duplicate);
11816
934709f0
PW
11817 left->inserted = right->inserted;
11818 left->duplicate = right->duplicate;
b775012e 11819 left->needs_update = right->needs_update;
934709f0
PW
11820 left->target_info = right->target_info;
11821 right->inserted = left_inserted;
11822 right->duplicate = left_duplicate;
b775012e 11823 right->needs_update = left_needs_update;
934709f0
PW
11824 right->target_info = left_target_info;
11825}
11826
b775012e
LM
11827/* Force the re-insertion of the locations at ADDRESS. This is called
11828 once a new/deleted/modified duplicate location is found and we are evaluating
11829 conditions on the target's side. Such conditions need to be updated on
11830 the target. */
11831
11832static void
11833force_breakpoint_reinsertion (struct bp_location *bl)
11834{
11835 struct bp_location **locp = NULL, **loc2p;
11836 struct bp_location *loc;
11837 CORE_ADDR address = 0;
11838 int pspace_num;
11839
11840 address = bl->address;
11841 pspace_num = bl->pspace->num;
11842
11843 /* This is only meaningful if the target is
11844 evaluating conditions and if the user has
11845 opted for condition evaluation on the target's
11846 side. */
11847 if (gdb_evaluates_breakpoint_condition_p ()
11848 || !target_supports_evaluation_of_breakpoint_conditions ())
11849 return;
11850
11851 /* Flag all breakpoint locations with this address and
11852 the same program space as the location
11853 as "its condition has changed". We need to
11854 update the conditions on the target's side. */
11855 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, address)
11856 {
11857 loc = *loc2p;
11858
11859 if (!is_breakpoint (loc->owner)
11860 || pspace_num != loc->pspace->num)
11861 continue;
11862
11863 /* Flag the location appropriately. We use a different state to
11864 let everyone know that we already updated the set of locations
11865 with addr bl->address and program space bl->pspace. This is so
11866 we don't have to keep calling these functions just to mark locations
11867 that have already been marked. */
11868 loc->condition_changed = condition_updated;
11869
11870 /* Free the agent expression bytecode as well. We will compute
11871 it later on. */
833177a4 11872 loc->cond_bytecode.reset ();
b775012e
LM
11873 }
11874}
7f32a4d5 11875
44702360
PA
11876/* Called whether new breakpoints are created, or existing breakpoints
11877 deleted, to update the global location list and recompute which
11878 locations are duplicate of which.
b775012e 11879
04086b45
PA
11880 The INSERT_MODE flag determines whether locations may not, may, or
11881 shall be inserted now. See 'enum ugll_insert_mode' for more
11882 info. */
b60e7edf 11883
0d381245 11884static void
44702360 11885update_global_location_list (enum ugll_insert_mode insert_mode)
0d381245 11886{
74960c60 11887 struct breakpoint *b;
876fa593 11888 struct bp_location **locp, *loc;
b775012e
LM
11889 /* Last breakpoint location address that was marked for update. */
11890 CORE_ADDR last_addr = 0;
11891 /* Last breakpoint location program space that was marked for update. */
11892 int last_pspace_num = -1;
f7545552 11893
2d134ed3
PA
11894 /* Used in the duplicates detection below. When iterating over all
11895 bp_locations, points to the first bp_location of a given address.
11896 Breakpoints and watchpoints of different types are never
11897 duplicates of each other. Keep one pointer for each type of
11898 breakpoint/watchpoint, so we only need to loop over all locations
11899 once. */
11900 struct bp_location *bp_loc_first; /* breakpoint */
11901 struct bp_location *wp_loc_first; /* hardware watchpoint */
11902 struct bp_location *awp_loc_first; /* access watchpoint */
11903 struct bp_location *rwp_loc_first; /* read watchpoint */
876fa593 11904
f5336ca5
PA
11905 /* Saved former bp_locations array which we compare against the newly
11906 built bp_locations from the current state of ALL_BREAKPOINTS. */
81b1e71c 11907 struct bp_location **old_locp;
f5336ca5 11908 unsigned old_locations_count;
81b1e71c 11909 gdb::unique_xmalloc_ptr<struct bp_location *> old_locations (bp_locations);
876fa593 11910
f5336ca5
PA
11911 old_locations_count = bp_locations_count;
11912 bp_locations = NULL;
11913 bp_locations_count = 0;
0d381245 11914
74960c60 11915 ALL_BREAKPOINTS (b)
876fa593 11916 for (loc = b->loc; loc; loc = loc->next)
f5336ca5 11917 bp_locations_count++;
876fa593 11918
f5336ca5
PA
11919 bp_locations = XNEWVEC (struct bp_location *, bp_locations_count);
11920 locp = bp_locations;
876fa593
JK
11921 ALL_BREAKPOINTS (b)
11922 for (loc = b->loc; loc; loc = loc->next)
11923 *locp++ = loc;
7f32a4d5
PA
11924
11925 /* See if we need to "upgrade" a software breakpoint to a hardware
11926 breakpoint. Do this before deciding whether locations are
11927 duplicates. Also do this before sorting because sorting order
11928 depends on location type. */
11929 for (locp = bp_locations;
11930 locp < bp_locations + bp_locations_count;
11931 locp++)
11932 {
11933 loc = *locp;
11934 if (!loc->inserted && should_be_inserted (loc))
11935 handle_automatic_hardware_breakpoints (loc);
11936 }
11937
39ef2f62
CB
11938 std::sort (bp_locations, bp_locations + bp_locations_count,
11939 bp_location_is_less_than);
876fa593 11940
f5336ca5 11941 bp_locations_target_extensions_update ();
74960c60 11942
4a64f543
MS
11943 /* Identify bp_location instances that are no longer present in the
11944 new list, and therefore should be freed. Note that it's not
11945 necessary that those locations should be removed from inferior --
11946 if there's another location at the same address (previously
11947 marked as duplicate), we don't need to remove/insert the
11948 location.
876fa593 11949
4a64f543
MS
11950 LOCP is kept in sync with OLD_LOCP, each pointing to the current
11951 and former bp_location array state respectively. */
876fa593 11952
f5336ca5 11953 locp = bp_locations;
81b1e71c
TT
11954 for (old_locp = old_locations.get ();
11955 old_locp < old_locations.get () + old_locations_count;
876fa593 11956 old_locp++)
74960c60 11957 {
876fa593 11958 struct bp_location *old_loc = *old_locp;
c7d46a38 11959 struct bp_location **loc2p;
876fa593 11960
e5dd4106 11961 /* Tells if 'old_loc' is found among the new locations. If
4a64f543 11962 not, we have to free it. */
c7d46a38 11963 int found_object = 0;
20874c92
VP
11964 /* Tells if the location should remain inserted in the target. */
11965 int keep_in_target = 0;
11966 int removed = 0;
876fa593 11967
4a64f543
MS
11968 /* Skip LOCP entries which will definitely never be needed.
11969 Stop either at or being the one matching OLD_LOC. */
f5336ca5 11970 while (locp < bp_locations + bp_locations_count
c7d46a38 11971 && (*locp)->address < old_loc->address)
876fa593 11972 locp++;
c7d46a38
PA
11973
11974 for (loc2p = locp;
f5336ca5 11975 (loc2p < bp_locations + bp_locations_count
c7d46a38
PA
11976 && (*loc2p)->address == old_loc->address);
11977 loc2p++)
11978 {
b775012e
LM
11979 /* Check if this is a new/duplicated location or a duplicated
11980 location that had its condition modified. If so, we want to send
11981 its condition to the target if evaluation of conditions is taking
11982 place there. */
11983 if ((*loc2p)->condition_changed == condition_modified
11984 && (last_addr != old_loc->address
11985 || last_pspace_num != old_loc->pspace->num))
c7d46a38 11986 {
b775012e
LM
11987 force_breakpoint_reinsertion (*loc2p);
11988 last_pspace_num = old_loc->pspace->num;
c7d46a38 11989 }
b775012e
LM
11990
11991 if (*loc2p == old_loc)
11992 found_object = 1;
c7d46a38 11993 }
74960c60 11994
b775012e
LM
11995 /* We have already handled this address, update it so that we don't
11996 have to go through updates again. */
11997 last_addr = old_loc->address;
11998
11999 /* Target-side condition evaluation: Handle deleted locations. */
12000 if (!found_object)
12001 force_breakpoint_reinsertion (old_loc);
12002
4a64f543
MS
12003 /* If this location is no longer present, and inserted, look if
12004 there's maybe a new location at the same address. If so,
12005 mark that one inserted, and don't remove this one. This is
12006 needed so that we don't have a time window where a breakpoint
12007 at certain location is not inserted. */
74960c60 12008
876fa593 12009 if (old_loc->inserted)
0d381245 12010 {
4a64f543
MS
12011 /* If the location is inserted now, we might have to remove
12012 it. */
74960c60 12013
876fa593 12014 if (found_object && should_be_inserted (old_loc))
74960c60 12015 {
4a64f543
MS
12016 /* The location is still present in the location list,
12017 and still should be inserted. Don't do anything. */
20874c92 12018 keep_in_target = 1;
74960c60
VP
12019 }
12020 else
12021 {
b775012e
LM
12022 /* This location still exists, but it won't be kept in the
12023 target since it may have been disabled. We proceed to
12024 remove its target-side condition. */
12025
4a64f543
MS
12026 /* The location is either no longer present, or got
12027 disabled. See if there's another location at the
12028 same address, in which case we don't need to remove
12029 this one from the target. */
876fa593 12030
2bdf28a0 12031 /* OLD_LOC comes from existing struct breakpoint. */
cb1e4e32 12032 if (bl_address_is_meaningful (old_loc))
876fa593 12033 {
876fa593 12034 for (loc2p = locp;
f5336ca5 12035 (loc2p < bp_locations + bp_locations_count
c7d46a38 12036 && (*loc2p)->address == old_loc->address);
876fa593
JK
12037 loc2p++)
12038 {
12039 struct bp_location *loc2 = *loc2p;
12040
7f32a4d5
PA
12041 if (loc2 == old_loc)
12042 continue;
12043
2d134ed3 12044 if (breakpoint_locations_match (loc2, old_loc))
c7d46a38 12045 {
85d721b8
PA
12046 /* Read watchpoint locations are switched to
12047 access watchpoints, if the former are not
12048 supported, but the latter are. */
12049 if (is_hardware_watchpoint (old_loc->owner))
12050 {
12051 gdb_assert (is_hardware_watchpoint (loc2->owner));
12052 loc2->watchpoint_type = old_loc->watchpoint_type;
12053 }
12054
934709f0
PW
12055 /* loc2 is a duplicated location. We need to check
12056 if it should be inserted in case it will be
12057 unduplicated. */
7f32a4d5 12058 if (unduplicated_should_be_inserted (loc2))
c7d46a38 12059 {
934709f0 12060 swap_insertion (old_loc, loc2);
c7d46a38
PA
12061 keep_in_target = 1;
12062 break;
12063 }
876fa593
JK
12064 }
12065 }
12066 }
74960c60
VP
12067 }
12068
20874c92
VP
12069 if (!keep_in_target)
12070 {
834c0d03 12071 if (remove_breakpoint (old_loc))
20874c92 12072 {
4a64f543
MS
12073 /* This is just about all we can do. We could keep
12074 this location on the global list, and try to
12075 remove it next time, but there's no particular
12076 reason why we will succeed next time.
20874c92 12077
4a64f543
MS
12078 Note that at this point, old_loc->owner is still
12079 valid, as delete_breakpoint frees the breakpoint
12080 only after calling us. */
3e43a32a
MS
12081 printf_filtered (_("warning: Error removing "
12082 "breakpoint %d\n"),
876fa593 12083 old_loc->owner->number);
20874c92
VP
12084 }
12085 removed = 1;
12086 }
0d381245 12087 }
74960c60
VP
12088
12089 if (!found_object)
1c5cfe86 12090 {
fbea99ea 12091 if (removed && target_is_non_stop_p ()
1cf4d951 12092 && need_moribund_for_location_type (old_loc))
20874c92 12093 {
db82e815
PA
12094 /* This location was removed from the target. In
12095 non-stop mode, a race condition is possible where
12096 we've removed a breakpoint, but stop events for that
12097 breakpoint are already queued and will arrive later.
12098 We apply an heuristic to be able to distinguish such
12099 SIGTRAPs from other random SIGTRAPs: we keep this
12100 breakpoint location for a bit, and will retire it
12101 after we see some number of events. The theory here
12102 is that reporting of events should, "on the average",
12103 be fair, so after a while we'll see events from all
12104 threads that have anything of interest, and no longer
12105 need to keep this breakpoint location around. We
12106 don't hold locations forever so to reduce chances of
12107 mistaking a non-breakpoint SIGTRAP for a breakpoint
12108 SIGTRAP.
12109
12110 The heuristic failing can be disastrous on
12111 decr_pc_after_break targets.
12112
12113 On decr_pc_after_break targets, like e.g., x86-linux,
12114 if we fail to recognize a late breakpoint SIGTRAP,
12115 because events_till_retirement has reached 0 too
12116 soon, we'll fail to do the PC adjustment, and report
12117 a random SIGTRAP to the user. When the user resumes
12118 the inferior, it will most likely immediately crash
2dec564e 12119 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
db82e815
PA
12120 corrupted, because of being resumed e.g., in the
12121 middle of a multi-byte instruction, or skipped a
12122 one-byte instruction. This was actually seen happen
12123 on native x86-linux, and should be less rare on
12124 targets that do not support new thread events, like
12125 remote, due to the heuristic depending on
12126 thread_count.
12127
12128 Mistaking a random SIGTRAP for a breakpoint trap
12129 causes similar symptoms (PC adjustment applied when
12130 it shouldn't), but then again, playing with SIGTRAPs
12131 behind the debugger's back is asking for trouble.
12132
12133 Since hardware watchpoint traps are always
12134 distinguishable from other traps, so we don't need to
12135 apply keep hardware watchpoint moribund locations
12136 around. We simply always ignore hardware watchpoint
12137 traps we can no longer explain. */
12138
5b6d1e4f
PA
12139 process_stratum_target *proc_target = nullptr;
12140 for (inferior *inf : all_inferiors ())
12141 if (inf->pspace == old_loc->pspace)
12142 {
12143 proc_target = inf->process_target ();
12144 break;
12145 }
12146 if (proc_target != nullptr)
12147 old_loc->events_till_retirement
12148 = 3 * (thread_count (proc_target) + 1);
12149 else
12150 old_loc->events_till_retirement = 1;
876fa593 12151 old_loc->owner = NULL;
20874c92 12152
1123588c 12153 moribund_locations.push_back (old_loc);
1c5cfe86
PA
12154 }
12155 else
f431efe5
PA
12156 {
12157 old_loc->owner = NULL;
12158 decref_bp_location (&old_loc);
12159 }
20874c92 12160 }
74960c60 12161 }
1c5cfe86 12162
348d480f
PA
12163 /* Rescan breakpoints at the same address and section, marking the
12164 first one as "first" and any others as "duplicates". This is so
12165 that the bpt instruction is only inserted once. If we have a
12166 permanent breakpoint at the same place as BPT, make that one the
12167 official one, and the rest as duplicates. Permanent breakpoints
12168 are sorted first for the same address.
12169
12170 Do the same for hardware watchpoints, but also considering the
12171 watchpoint's type (regular/access/read) and length. */
12172
12173 bp_loc_first = NULL;
12174 wp_loc_first = NULL;
12175 awp_loc_first = NULL;
12176 rwp_loc_first = NULL;
12177 ALL_BP_LOCATIONS (loc, locp)
12178 {
12179 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
12180 non-NULL. */
348d480f 12181 struct bp_location **loc_first_p;
d3fbdd86 12182 b = loc->owner;
348d480f 12183
6f380991 12184 if (!unduplicated_should_be_inserted (loc)
cb1e4e32 12185 || !bl_address_is_meaningful (loc)
1e4d1764
YQ
12186 /* Don't detect duplicate for tracepoint locations because they are
12187 never duplicated. See the comments in field `duplicate' of
12188 `struct bp_location'. */
348d480f 12189 || is_tracepoint (b))
b775012e
LM
12190 {
12191 /* Clear the condition modification flag. */
12192 loc->condition_changed = condition_unchanged;
12193 continue;
12194 }
348d480f 12195
348d480f
PA
12196 if (b->type == bp_hardware_watchpoint)
12197 loc_first_p = &wp_loc_first;
12198 else if (b->type == bp_read_watchpoint)
12199 loc_first_p = &rwp_loc_first;
12200 else if (b->type == bp_access_watchpoint)
12201 loc_first_p = &awp_loc_first;
12202 else
12203 loc_first_p = &bp_loc_first;
12204
12205 if (*loc_first_p == NULL
12206 || (overlay_debugging && loc->section != (*loc_first_p)->section)
12207 || !breakpoint_locations_match (loc, *loc_first_p))
12208 {
12209 *loc_first_p = loc;
12210 loc->duplicate = 0;
b775012e
LM
12211
12212 if (is_breakpoint (loc->owner) && loc->condition_changed)
12213 {
12214 loc->needs_update = 1;
12215 /* Clear the condition modification flag. */
12216 loc->condition_changed = condition_unchanged;
12217 }
348d480f
PA
12218 continue;
12219 }
12220
934709f0
PW
12221
12222 /* This and the above ensure the invariant that the first location
12223 is not duplicated, and is the inserted one.
12224 All following are marked as duplicated, and are not inserted. */
12225 if (loc->inserted)
12226 swap_insertion (loc, *loc_first_p);
348d480f
PA
12227 loc->duplicate = 1;
12228
b775012e
LM
12229 /* Clear the condition modification flag. */
12230 loc->condition_changed = condition_unchanged;
348d480f
PA
12231 }
12232
a25a5a45 12233 if (insert_mode == UGLL_INSERT || breakpoints_should_be_inserted_now ())
b775012e 12234 {
04086b45 12235 if (insert_mode != UGLL_DONT_INSERT)
b775012e
LM
12236 insert_breakpoint_locations ();
12237 else
12238 {
44702360
PA
12239 /* Even though the caller told us to not insert new
12240 locations, we may still need to update conditions on the
12241 target's side of breakpoints that were already inserted
12242 if the target is evaluating breakpoint conditions. We
b775012e
LM
12243 only update conditions for locations that are marked
12244 "needs_update". */
12245 update_inserted_breakpoint_locations ();
12246 }
12247 }
348d480f 12248
04086b45 12249 if (insert_mode != UGLL_DONT_INSERT)
1e4d1764 12250 download_tracepoint_locations ();
348d480f
PA
12251}
12252
12253void
12254breakpoint_retire_moribund (void)
12255{
1123588c
TT
12256 for (int ix = 0; ix < moribund_locations.size (); ++ix)
12257 {
12258 struct bp_location *loc = moribund_locations[ix];
12259 if (--(loc->events_till_retirement) == 0)
12260 {
12261 decref_bp_location (&loc);
12262 unordered_remove (moribund_locations, ix);
12263 --ix;
12264 }
12265 }
348d480f
PA
12266}
12267
12268static void
44702360 12269update_global_location_list_nothrow (enum ugll_insert_mode insert_mode)
348d480f 12270{
348d480f 12271
a70b8144 12272 try
492d29ea
PA
12273 {
12274 update_global_location_list (insert_mode);
12275 }
230d2906 12276 catch (const gdb_exception_error &e)
492d29ea
PA
12277 {
12278 }
348d480f
PA
12279}
12280
12281/* Clear BKP from a BPS. */
12282
12283static void
12284bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
12285{
12286 bpstat bs;
12287
12288 for (bs = bps; bs; bs = bs->next)
12289 if (bs->breakpoint_at == bpt)
12290 {
12291 bs->breakpoint_at = NULL;
12292 bs->old_val = NULL;
12293 /* bs->commands will be freed later. */
12294 }
12295}
12296
12297/* Callback for iterate_over_threads. */
12298static int
12299bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
12300{
9a3c8263 12301 struct breakpoint *bpt = (struct breakpoint *) data;
348d480f
PA
12302
12303 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
12304 return 0;
12305}
12306
12307/* Helper for breakpoint and tracepoint breakpoint_ops->mention
12308 callbacks. */
12309
12310static void
12311say_where (struct breakpoint *b)
12312{
12313 struct value_print_options opts;
12314
12315 get_user_print_options (&opts);
12316
12317 /* i18n: cagney/2005-02-11: Below needs to be merged into a
12318 single string. */
12319 if (b->loc == NULL)
12320 {
f00aae0f
KS
12321 /* For pending locations, the output differs slightly based
12322 on b->extra_string. If this is non-NULL, it contains either
12323 a condition or dprintf arguments. */
12324 if (b->extra_string == NULL)
12325 {
12326 printf_filtered (_(" (%s) pending."),
d28cd78a 12327 event_location_to_string (b->location.get ()));
f00aae0f
KS
12328 }
12329 else if (b->type == bp_dprintf)
12330 {
12331 printf_filtered (_(" (%s,%s) pending."),
d28cd78a 12332 event_location_to_string (b->location.get ()),
f00aae0f
KS
12333 b->extra_string);
12334 }
12335 else
12336 {
12337 printf_filtered (_(" (%s %s) pending."),
d28cd78a 12338 event_location_to_string (b->location.get ()),
f00aae0f
KS
12339 b->extra_string);
12340 }
348d480f
PA
12341 }
12342 else
12343 {
2f202fde 12344 if (opts.addressprint || b->loc->symtab == NULL)
6a831f06
PA
12345 printf_filtered (" at %ps",
12346 styled_string (address_style.style (),
12347 paddress (b->loc->gdbarch,
12348 b->loc->address)));
2f202fde 12349 if (b->loc->symtab != NULL)
f8eba3c6
TT
12350 {
12351 /* If there is a single location, we can print the location
12352 more nicely. */
12353 if (b->loc->next == NULL)
0bb296cb 12354 {
6a831f06
PA
12355 const char *filename
12356 = symtab_to_filename_for_display (b->loc->symtab);
12357 printf_filtered (": file %ps, line %d.",
12358 styled_string (file_name_style.style (),
12359 filename),
0bb296cb
TT
12360 b->loc->line_number);
12361 }
f8eba3c6
TT
12362 else
12363 /* This is not ideal, but each location may have a
12364 different file name, and this at least reflects the
12365 real situation somewhat. */
f00aae0f 12366 printf_filtered (": %s.",
d28cd78a 12367 event_location_to_string (b->location.get ()));
f8eba3c6 12368 }
348d480f
PA
12369
12370 if (b->loc->next)
12371 {
12372 struct bp_location *loc = b->loc;
12373 int n = 0;
12374 for (; loc; loc = loc->next)
12375 ++n;
12376 printf_filtered (" (%d locations)", n);
12377 }
12378 }
12379}
12380
5f486660 12381bp_location::~bp_location ()
348d480f 12382{
5f486660 12383 xfree (function_name);
348d480f
PA
12384}
12385
c1fc2657 12386/* Destructor for the breakpoint base class. */
348d480f 12387
c1fc2657 12388breakpoint::~breakpoint ()
348d480f 12389{
c1fc2657
SM
12390 xfree (this->cond_string);
12391 xfree (this->extra_string);
348d480f
PA
12392}
12393
2060206e
PA
12394static struct bp_location *
12395base_breakpoint_allocate_location (struct breakpoint *self)
348d480f 12396{
5f486660 12397 return new bp_location (self);
348d480f
PA
12398}
12399
2060206e
PA
12400static void
12401base_breakpoint_re_set (struct breakpoint *b)
12402{
12403 /* Nothing to re-set. */
12404}
12405
12406#define internal_error_pure_virtual_called() \
12407 gdb_assert_not_reached ("pure virtual function called")
12408
12409static int
12410base_breakpoint_insert_location (struct bp_location *bl)
12411{
12412 internal_error_pure_virtual_called ();
12413}
12414
12415static int
73971819
PA
12416base_breakpoint_remove_location (struct bp_location *bl,
12417 enum remove_bp_reason reason)
2060206e
PA
12418{
12419 internal_error_pure_virtual_called ();
12420}
12421
12422static int
12423base_breakpoint_breakpoint_hit (const struct bp_location *bl,
bd522513 12424 const address_space *aspace,
09ac7c10
TT
12425 CORE_ADDR bp_addr,
12426 const struct target_waitstatus *ws)
2060206e
PA
12427{
12428 internal_error_pure_virtual_called ();
12429}
12430
12431static void
12432base_breakpoint_check_status (bpstat bs)
12433{
12434 /* Always stop. */
12435}
12436
12437/* A "works_in_software_mode" breakpoint_ops method that just internal
12438 errors. */
12439
12440static int
12441base_breakpoint_works_in_software_mode (const struct breakpoint *b)
12442{
12443 internal_error_pure_virtual_called ();
12444}
12445
12446/* A "resources_needed" breakpoint_ops method that just internal
12447 errors. */
12448
12449static int
12450base_breakpoint_resources_needed (const struct bp_location *bl)
12451{
12452 internal_error_pure_virtual_called ();
12453}
12454
12455static enum print_stop_action
12456base_breakpoint_print_it (bpstat bs)
12457{
12458 internal_error_pure_virtual_called ();
12459}
12460
12461static void
12462base_breakpoint_print_one_detail (const struct breakpoint *self,
12463 struct ui_out *uiout)
12464{
12465 /* nothing */
12466}
12467
12468static void
12469base_breakpoint_print_mention (struct breakpoint *b)
12470{
12471 internal_error_pure_virtual_called ();
12472}
12473
12474static void
12475base_breakpoint_print_recreate (struct breakpoint *b, struct ui_file *fp)
12476{
12477 internal_error_pure_virtual_called ();
12478}
12479
983af33b 12480static void
f00aae0f 12481base_breakpoint_create_sals_from_location
626d2320 12482 (struct event_location *location,
f00aae0f
KS
12483 struct linespec_result *canonical,
12484 enum bptype type_wanted)
983af33b
SDJ
12485{
12486 internal_error_pure_virtual_called ();
12487}
12488
12489static void
12490base_breakpoint_create_breakpoints_sal (struct gdbarch *gdbarch,
12491 struct linespec_result *c,
e1e01040
PA
12492 gdb::unique_xmalloc_ptr<char> cond_string,
12493 gdb::unique_xmalloc_ptr<char> extra_string,
983af33b
SDJ
12494 enum bptype type_wanted,
12495 enum bpdisp disposition,
12496 int thread,
12497 int task, int ignore_count,
12498 const struct breakpoint_ops *o,
12499 int from_tty, int enabled,
44f238bb 12500 int internal, unsigned flags)
983af33b
SDJ
12501{
12502 internal_error_pure_virtual_called ();
12503}
12504
6c5b2ebe 12505static std::vector<symtab_and_line>
f00aae0f 12506base_breakpoint_decode_location (struct breakpoint *b,
626d2320 12507 struct event_location *location,
6c5b2ebe 12508 struct program_space *search_pspace)
983af33b
SDJ
12509{
12510 internal_error_pure_virtual_called ();
12511}
12512
ab04a2af
TT
12513/* The default 'explains_signal' method. */
12514
47591c29 12515static int
427cd150 12516base_breakpoint_explains_signal (struct breakpoint *b, enum gdb_signal sig)
ab04a2af 12517{
47591c29 12518 return 1;
ab04a2af
TT
12519}
12520
9d6e6e84
HZ
12521/* The default "after_condition_true" method. */
12522
12523static void
12524base_breakpoint_after_condition_true (struct bpstats *bs)
12525{
12526 /* Nothing to do. */
12527}
12528
ab04a2af 12529struct breakpoint_ops base_breakpoint_ops =
2060206e 12530{
2060206e
PA
12531 base_breakpoint_allocate_location,
12532 base_breakpoint_re_set,
12533 base_breakpoint_insert_location,
12534 base_breakpoint_remove_location,
12535 base_breakpoint_breakpoint_hit,
12536 base_breakpoint_check_status,
12537 base_breakpoint_resources_needed,
12538 base_breakpoint_works_in_software_mode,
12539 base_breakpoint_print_it,
12540 NULL,
12541 base_breakpoint_print_one_detail,
12542 base_breakpoint_print_mention,
983af33b 12543 base_breakpoint_print_recreate,
5f700d83 12544 base_breakpoint_create_sals_from_location,
983af33b 12545 base_breakpoint_create_breakpoints_sal,
5f700d83 12546 base_breakpoint_decode_location,
9d6e6e84
HZ
12547 base_breakpoint_explains_signal,
12548 base_breakpoint_after_condition_true,
2060206e
PA
12549};
12550
12551/* Default breakpoint_ops methods. */
12552
12553static void
348d480f
PA
12554bkpt_re_set (struct breakpoint *b)
12555{
06edf0c0 12556 /* FIXME: is this still reachable? */
9ef9e6a6 12557 if (breakpoint_event_location_empty_p (b))
06edf0c0 12558 {
f00aae0f 12559 /* Anything without a location can't be re-set. */
348d480f 12560 delete_breakpoint (b);
06edf0c0 12561 return;
348d480f 12562 }
06edf0c0
PA
12563
12564 breakpoint_re_set_default (b);
348d480f
PA
12565}
12566
2060206e 12567static int
348d480f
PA
12568bkpt_insert_location (struct bp_location *bl)
12569{
cd6c3b4f
YQ
12570 CORE_ADDR addr = bl->target_info.reqstd_address;
12571
579c6ad9 12572 bl->target_info.kind = breakpoint_kind (bl, &addr);
cd6c3b4f
YQ
12573 bl->target_info.placed_address = addr;
12574
348d480f 12575 if (bl->loc_type == bp_loc_hardware_breakpoint)
7c16b83e 12576 return target_insert_hw_breakpoint (bl->gdbarch, &bl->target_info);
348d480f 12577 else
7c16b83e 12578 return target_insert_breakpoint (bl->gdbarch, &bl->target_info);
348d480f
PA
12579}
12580
2060206e 12581static int
73971819 12582bkpt_remove_location (struct bp_location *bl, enum remove_bp_reason reason)
348d480f
PA
12583{
12584 if (bl->loc_type == bp_loc_hardware_breakpoint)
12585 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
12586 else
73971819 12587 return target_remove_breakpoint (bl->gdbarch, &bl->target_info, reason);
348d480f
PA
12588}
12589
2060206e 12590static int
348d480f 12591bkpt_breakpoint_hit (const struct bp_location *bl,
bd522513 12592 const address_space *aspace, CORE_ADDR bp_addr,
09ac7c10 12593 const struct target_waitstatus *ws)
348d480f 12594{
09ac7c10 12595 if (ws->kind != TARGET_WAITKIND_STOPPED
a493e3e2 12596 || ws->value.sig != GDB_SIGNAL_TRAP)
09ac7c10
TT
12597 return 0;
12598
348d480f
PA
12599 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
12600 aspace, bp_addr))
12601 return 0;
12602
12603 if (overlay_debugging /* unmapped overlay section */
12604 && section_is_overlay (bl->section)
12605 && !section_is_mapped (bl->section))
12606 return 0;
12607
12608 return 1;
12609}
12610
cd1608cc
PA
12611static int
12612dprintf_breakpoint_hit (const struct bp_location *bl,
bd522513 12613 const address_space *aspace, CORE_ADDR bp_addr,
cd1608cc
PA
12614 const struct target_waitstatus *ws)
12615{
12616 if (dprintf_style == dprintf_style_agent
12617 && target_can_run_breakpoint_commands ())
12618 {
12619 /* An agent-style dprintf never causes a stop. If we see a trap
12620 for this address it must be for a breakpoint that happens to
12621 be set at the same address. */
12622 return 0;
12623 }
12624
12625 return bkpt_breakpoint_hit (bl, aspace, bp_addr, ws);
12626}
12627
2060206e 12628static int
348d480f
PA
12629bkpt_resources_needed (const struct bp_location *bl)
12630{
12631 gdb_assert (bl->owner->type == bp_hardware_breakpoint);
12632
12633 return 1;
12634}
12635
2060206e 12636static enum print_stop_action
348d480f
PA
12637bkpt_print_it (bpstat bs)
12638{
348d480f
PA
12639 struct breakpoint *b;
12640 const struct bp_location *bl;
001c8c33 12641 int bp_temp;
79a45e25 12642 struct ui_out *uiout = current_uiout;
348d480f
PA
12643
12644 gdb_assert (bs->bp_location_at != NULL);
12645
b6433ede 12646 bl = bs->bp_location_at.get ();
348d480f
PA
12647 b = bs->breakpoint_at;
12648
001c8c33
PA
12649 bp_temp = b->disposition == disp_del;
12650 if (bl->address != bl->requested_address)
12651 breakpoint_adjustment_warning (bl->requested_address,
12652 bl->address,
12653 b->number, 1);
12654 annotate_breakpoint (b->number);
f303dbd6
PA
12655 maybe_print_thread_hit_breakpoint (uiout);
12656
112e8700 12657 if (uiout->is_mi_like_p ())
348d480f 12658 {
112e8700 12659 uiout->field_string ("reason",
001c8c33 12660 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
112e8700 12661 uiout->field_string ("disp", bpdisp_text (b->disposition));
06edf0c0 12662 }
6a831f06
PA
12663 if (bp_temp)
12664 uiout->message ("Temporary breakpoint %pF, ",
12665 signed_field ("bkptno", b->number));
12666 else
12667 uiout->message ("Breakpoint %pF, ",
12668 signed_field ("bkptno", b->number));
06edf0c0 12669
001c8c33 12670 return PRINT_SRC_AND_LOC;
06edf0c0
PA
12671}
12672
2060206e 12673static void
06edf0c0
PA
12674bkpt_print_mention (struct breakpoint *b)
12675{
112e8700 12676 if (current_uiout->is_mi_like_p ())
06edf0c0
PA
12677 return;
12678
12679 switch (b->type)
12680 {
12681 case bp_breakpoint:
12682 case bp_gnu_ifunc_resolver:
12683 if (b->disposition == disp_del)
12684 printf_filtered (_("Temporary breakpoint"));
12685 else
12686 printf_filtered (_("Breakpoint"));
12687 printf_filtered (_(" %d"), b->number);
12688 if (b->type == bp_gnu_ifunc_resolver)
12689 printf_filtered (_(" at gnu-indirect-function resolver"));
12690 break;
12691 case bp_hardware_breakpoint:
12692 printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
12693 break;
e7e0cddf
SS
12694 case bp_dprintf:
12695 printf_filtered (_("Dprintf %d"), b->number);
12696 break;
06edf0c0
PA
12697 }
12698
12699 say_where (b);
12700}
12701
2060206e 12702static void
06edf0c0
PA
12703bkpt_print_recreate (struct breakpoint *tp, struct ui_file *fp)
12704{
12705 if (tp->type == bp_breakpoint && tp->disposition == disp_del)
12706 fprintf_unfiltered (fp, "tbreak");
12707 else if (tp->type == bp_breakpoint)
12708 fprintf_unfiltered (fp, "break");
12709 else if (tp->type == bp_hardware_breakpoint
12710 && tp->disposition == disp_del)
12711 fprintf_unfiltered (fp, "thbreak");
12712 else if (tp->type == bp_hardware_breakpoint)
12713 fprintf_unfiltered (fp, "hbreak");
12714 else
12715 internal_error (__FILE__, __LINE__,
12716 _("unhandled breakpoint type %d"), (int) tp->type);
12717
f00aae0f 12718 fprintf_unfiltered (fp, " %s",
d28cd78a 12719 event_location_to_string (tp->location.get ()));
f00aae0f
KS
12720
12721 /* Print out extra_string if this breakpoint is pending. It might
12722 contain, for example, conditions that were set by the user. */
12723 if (tp->loc == NULL && tp->extra_string != NULL)
12724 fprintf_unfiltered (fp, " %s", tp->extra_string);
12725
dd11a36c 12726 print_recreate_thread (tp, fp);
06edf0c0
PA
12727}
12728
983af33b 12729static void
626d2320 12730bkpt_create_sals_from_location (struct event_location *location,
f00aae0f
KS
12731 struct linespec_result *canonical,
12732 enum bptype type_wanted)
983af33b 12733{
f00aae0f 12734 create_sals_from_location_default (location, canonical, type_wanted);
983af33b
SDJ
12735}
12736
12737static void
12738bkpt_create_breakpoints_sal (struct gdbarch *gdbarch,
12739 struct linespec_result *canonical,
e1e01040
PA
12740 gdb::unique_xmalloc_ptr<char> cond_string,
12741 gdb::unique_xmalloc_ptr<char> extra_string,
983af33b
SDJ
12742 enum bptype type_wanted,
12743 enum bpdisp disposition,
12744 int thread,
12745 int task, int ignore_count,
12746 const struct breakpoint_ops *ops,
12747 int from_tty, int enabled,
44f238bb 12748 int internal, unsigned flags)
983af33b 12749{
023fa29b 12750 create_breakpoints_sal_default (gdbarch, canonical,
e1e01040
PA
12751 std::move (cond_string),
12752 std::move (extra_string),
e7e0cddf 12753 type_wanted,
983af33b
SDJ
12754 disposition, thread, task,
12755 ignore_count, ops, from_tty,
44f238bb 12756 enabled, internal, flags);
983af33b
SDJ
12757}
12758
6c5b2ebe 12759static std::vector<symtab_and_line>
f00aae0f 12760bkpt_decode_location (struct breakpoint *b,
626d2320 12761 struct event_location *location,
6c5b2ebe 12762 struct program_space *search_pspace)
983af33b 12763{
6c5b2ebe 12764 return decode_location_default (b, location, search_pspace);
983af33b
SDJ
12765}
12766
06edf0c0
PA
12767/* Virtual table for internal breakpoints. */
12768
12769static void
12770internal_bkpt_re_set (struct breakpoint *b)
12771{
12772 switch (b->type)
12773 {
12774 /* Delete overlay event and longjmp master breakpoints; they
12775 will be reset later by breakpoint_re_set. */
12776 case bp_overlay_event:
12777 case bp_longjmp_master:
12778 case bp_std_terminate_master:
12779 case bp_exception_master:
12780 delete_breakpoint (b);
12781 break;
12782
12783 /* This breakpoint is special, it's set up when the inferior
dda83cd7 12784 starts and we really don't want to touch it. */
06edf0c0
PA
12785 case bp_shlib_event:
12786
12787 /* Like bp_shlib_event, this breakpoint type is special. Once
12788 it is set up, we do not want to touch it. */
12789 case bp_thread_event:
12790 break;
12791 }
12792}
12793
12794static void
12795internal_bkpt_check_status (bpstat bs)
12796{
a9b3a50f
PA
12797 if (bs->breakpoint_at->type == bp_shlib_event)
12798 {
12799 /* If requested, stop when the dynamic linker notifies GDB of
12800 events. This allows the user to get control and place
12801 breakpoints in initializer routines for dynamically loaded
12802 objects (among other things). */
12803 bs->stop = stop_on_solib_events;
12804 bs->print = stop_on_solib_events;
12805 }
12806 else
12807 bs->stop = 0;
06edf0c0
PA
12808}
12809
12810static enum print_stop_action
12811internal_bkpt_print_it (bpstat bs)
12812{
06edf0c0 12813 struct breakpoint *b;
06edf0c0 12814
06edf0c0
PA
12815 b = bs->breakpoint_at;
12816
06edf0c0
PA
12817 switch (b->type)
12818 {
348d480f
PA
12819 case bp_shlib_event:
12820 /* Did we stop because the user set the stop_on_solib_events
12821 variable? (If so, we report this as a generic, "Stopped due
12822 to shlib event" message.) */
edcc5120 12823 print_solib_event (0);
348d480f
PA
12824 break;
12825
12826 case bp_thread_event:
12827 /* Not sure how we will get here.
12828 GDB should not stop for these breakpoints. */
12829 printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
12830 break;
12831
12832 case bp_overlay_event:
12833 /* By analogy with the thread event, GDB should not stop for these. */
12834 printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
12835 break;
12836
12837 case bp_longjmp_master:
12838 /* These should never be enabled. */
12839 printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
348d480f
PA
12840 break;
12841
12842 case bp_std_terminate_master:
12843 /* These should never be enabled. */
12844 printf_filtered (_("std::terminate Master Breakpoint: "
12845 "gdb should not stop!\n"));
348d480f
PA
12846 break;
12847
12848 case bp_exception_master:
12849 /* These should never be enabled. */
12850 printf_filtered (_("Exception Master Breakpoint: "
12851 "gdb should not stop!\n"));
06edf0c0
PA
12852 break;
12853 }
12854
001c8c33 12855 return PRINT_NOTHING;
06edf0c0
PA
12856}
12857
12858static void
12859internal_bkpt_print_mention (struct breakpoint *b)
12860{
12861 /* Nothing to mention. These breakpoints are internal. */
12862}
12863
06edf0c0
PA
12864/* Virtual table for momentary breakpoints */
12865
12866static void
12867momentary_bkpt_re_set (struct breakpoint *b)
12868{
12869 /* Keep temporary breakpoints, which can be encountered when we step
4d1eb6b4 12870 over a dlopen call and solib_add is resetting the breakpoints.
06edf0c0
PA
12871 Otherwise these should have been blown away via the cleanup chain
12872 or by breakpoint_init_inferior when we rerun the executable. */
12873}
12874
12875static void
12876momentary_bkpt_check_status (bpstat bs)
12877{
12878 /* Nothing. The point of these breakpoints is causing a stop. */
12879}
12880
12881static enum print_stop_action
12882momentary_bkpt_print_it (bpstat bs)
12883{
001c8c33 12884 return PRINT_UNKNOWN;
348d480f
PA
12885}
12886
06edf0c0
PA
12887static void
12888momentary_bkpt_print_mention (struct breakpoint *b)
348d480f 12889{
06edf0c0 12890 /* Nothing to mention. These breakpoints are internal. */
348d480f
PA
12891}
12892
e2e4d78b
JK
12893/* Ensure INITIATING_FRAME is cleared when no such breakpoint exists.
12894
12895 It gets cleared already on the removal of the first one of such placed
12896 breakpoints. This is OK as they get all removed altogether. */
12897
c1fc2657 12898longjmp_breakpoint::~longjmp_breakpoint ()
e2e4d78b 12899{
c1fc2657 12900 thread_info *tp = find_thread_global_id (this->thread);
e2e4d78b 12901
c1fc2657 12902 if (tp != NULL)
e2e4d78b 12903 tp->initiating_frame = null_frame_id;
e2e4d78b
JK
12904}
12905
55aa24fb
SDJ
12906/* Specific methods for probe breakpoints. */
12907
12908static int
12909bkpt_probe_insert_location (struct bp_location *bl)
12910{
12911 int v = bkpt_insert_location (bl);
12912
12913 if (v == 0)
12914 {
12915 /* The insertion was successful, now let's set the probe's semaphore
12916 if needed. */
935676c9 12917 bl->probe.prob->set_semaphore (bl->probe.objfile, bl->gdbarch);
55aa24fb
SDJ
12918 }
12919
12920 return v;
12921}
12922
12923static int
73971819
PA
12924bkpt_probe_remove_location (struct bp_location *bl,
12925 enum remove_bp_reason reason)
55aa24fb
SDJ
12926{
12927 /* Let's clear the semaphore before removing the location. */
935676c9 12928 bl->probe.prob->clear_semaphore (bl->probe.objfile, bl->gdbarch);
55aa24fb 12929
73971819 12930 return bkpt_remove_location (bl, reason);
55aa24fb
SDJ
12931}
12932
12933static void
626d2320 12934bkpt_probe_create_sals_from_location (struct event_location *location,
5f700d83 12935 struct linespec_result *canonical,
f00aae0f 12936 enum bptype type_wanted)
55aa24fb
SDJ
12937{
12938 struct linespec_sals lsal;
12939
c2f4122d 12940 lsal.sals = parse_probes (location, NULL, canonical);
8e9e35b1
TT
12941 lsal.canonical
12942 = xstrdup (event_location_to_string (canonical->location.get ()));
6c5b2ebe 12943 canonical->lsals.push_back (std::move (lsal));
55aa24fb
SDJ
12944}
12945
6c5b2ebe 12946static std::vector<symtab_and_line>
f00aae0f 12947bkpt_probe_decode_location (struct breakpoint *b,
626d2320 12948 struct event_location *location,
6c5b2ebe 12949 struct program_space *search_pspace)
55aa24fb 12950{
6c5b2ebe
PA
12951 std::vector<symtab_and_line> sals = parse_probes (location, search_pspace, NULL);
12952 if (sals.empty ())
55aa24fb 12953 error (_("probe not found"));
6c5b2ebe 12954 return sals;
55aa24fb
SDJ
12955}
12956
348d480f 12957/* The breakpoint_ops structure to be used in tracepoints. */
876fa593 12958
348d480f
PA
12959static void
12960tracepoint_re_set (struct breakpoint *b)
12961{
12962 breakpoint_re_set_default (b);
12963}
876fa593 12964
348d480f
PA
12965static int
12966tracepoint_breakpoint_hit (const struct bp_location *bl,
bd522513 12967 const address_space *aspace, CORE_ADDR bp_addr,
09ac7c10 12968 const struct target_waitstatus *ws)
348d480f
PA
12969{
12970 /* By definition, the inferior does not report stops at
12971 tracepoints. */
12972 return 0;
74960c60
VP
12973}
12974
12975static void
348d480f
PA
12976tracepoint_print_one_detail (const struct breakpoint *self,
12977 struct ui_out *uiout)
74960c60 12978{
d9b3f62e 12979 struct tracepoint *tp = (struct tracepoint *) self;
5d9310c4 12980 if (!tp->static_trace_marker_id.empty ())
348d480f
PA
12981 {
12982 gdb_assert (self->type == bp_static_tracepoint);
cc59ec59 12983
6a831f06
PA
12984 uiout->message ("\tmarker id is %pF\n",
12985 string_field ("static-tracepoint-marker-string-id",
12986 tp->static_trace_marker_id.c_str ()));
348d480f 12987 }
0d381245
VP
12988}
12989
a474d7c2 12990static void
348d480f 12991tracepoint_print_mention (struct breakpoint *b)
a474d7c2 12992{
112e8700 12993 if (current_uiout->is_mi_like_p ())
348d480f 12994 return;
cc59ec59 12995
348d480f
PA
12996 switch (b->type)
12997 {
12998 case bp_tracepoint:
12999 printf_filtered (_("Tracepoint"));
13000 printf_filtered (_(" %d"), b->number);
13001 break;
13002 case bp_fast_tracepoint:
13003 printf_filtered (_("Fast tracepoint"));
13004 printf_filtered (_(" %d"), b->number);
13005 break;
13006 case bp_static_tracepoint:
13007 printf_filtered (_("Static tracepoint"));
13008 printf_filtered (_(" %d"), b->number);
13009 break;
13010 default:
13011 internal_error (__FILE__, __LINE__,
13012 _("unhandled tracepoint type %d"), (int) b->type);
13013 }
13014
13015 say_where (b);
a474d7c2
PA
13016}
13017
348d480f 13018static void
d9b3f62e 13019tracepoint_print_recreate (struct breakpoint *self, struct ui_file *fp)
a474d7c2 13020{
d9b3f62e
PA
13021 struct tracepoint *tp = (struct tracepoint *) self;
13022
13023 if (self->type == bp_fast_tracepoint)
348d480f 13024 fprintf_unfiltered (fp, "ftrace");
c93e8391 13025 else if (self->type == bp_static_tracepoint)
348d480f 13026 fprintf_unfiltered (fp, "strace");
d9b3f62e 13027 else if (self->type == bp_tracepoint)
348d480f
PA
13028 fprintf_unfiltered (fp, "trace");
13029 else
13030 internal_error (__FILE__, __LINE__,
d9b3f62e 13031 _("unhandled tracepoint type %d"), (int) self->type);
cc59ec59 13032
f00aae0f 13033 fprintf_unfiltered (fp, " %s",
d28cd78a 13034 event_location_to_string (self->location.get ()));
d9b3f62e
PA
13035 print_recreate_thread (self, fp);
13036
13037 if (tp->pass_count)
13038 fprintf_unfiltered (fp, " passcount %d\n", tp->pass_count);
a474d7c2
PA
13039}
13040
983af33b 13041static void
626d2320 13042tracepoint_create_sals_from_location (struct event_location *location,
f00aae0f
KS
13043 struct linespec_result *canonical,
13044 enum bptype type_wanted)
983af33b 13045{
f00aae0f 13046 create_sals_from_location_default (location, canonical, type_wanted);
983af33b
SDJ
13047}
13048
13049static void
13050tracepoint_create_breakpoints_sal (struct gdbarch *gdbarch,
13051 struct linespec_result *canonical,
e1e01040
PA
13052 gdb::unique_xmalloc_ptr<char> cond_string,
13053 gdb::unique_xmalloc_ptr<char> extra_string,
983af33b
SDJ
13054 enum bptype type_wanted,
13055 enum bpdisp disposition,
13056 int thread,
13057 int task, int ignore_count,
13058 const struct breakpoint_ops *ops,
13059 int from_tty, int enabled,
44f238bb 13060 int internal, unsigned flags)
983af33b 13061{
023fa29b 13062 create_breakpoints_sal_default (gdbarch, canonical,
e1e01040
PA
13063 std::move (cond_string),
13064 std::move (extra_string),
e7e0cddf 13065 type_wanted,
983af33b
SDJ
13066 disposition, thread, task,
13067 ignore_count, ops, from_tty,
44f238bb 13068 enabled, internal, flags);
983af33b
SDJ
13069}
13070
6c5b2ebe 13071static std::vector<symtab_and_line>
f00aae0f 13072tracepoint_decode_location (struct breakpoint *b,
626d2320 13073 struct event_location *location,
6c5b2ebe 13074 struct program_space *search_pspace)
983af33b 13075{
6c5b2ebe 13076 return decode_location_default (b, location, search_pspace);
983af33b
SDJ
13077}
13078
2060206e 13079struct breakpoint_ops tracepoint_breakpoint_ops;
348d480f 13080
bac7c5cf 13081/* Virtual table for tracepoints on static probes. */
55aa24fb
SDJ
13082
13083static void
f00aae0f 13084tracepoint_probe_create_sals_from_location
626d2320 13085 (struct event_location *location,
f00aae0f
KS
13086 struct linespec_result *canonical,
13087 enum bptype type_wanted)
55aa24fb
SDJ
13088{
13089 /* We use the same method for breakpoint on probes. */
f00aae0f 13090 bkpt_probe_create_sals_from_location (location, canonical, type_wanted);
55aa24fb
SDJ
13091}
13092
6c5b2ebe 13093static std::vector<symtab_and_line>
f00aae0f 13094tracepoint_probe_decode_location (struct breakpoint *b,
626d2320 13095 struct event_location *location,
6c5b2ebe 13096 struct program_space *search_pspace)
55aa24fb
SDJ
13097{
13098 /* We use the same method for breakpoint on probes. */
6c5b2ebe 13099 return bkpt_probe_decode_location (b, location, search_pspace);
55aa24fb
SDJ
13100}
13101
5c2b4418
HZ
13102/* Dprintf breakpoint_ops methods. */
13103
13104static void
13105dprintf_re_set (struct breakpoint *b)
13106{
13107 breakpoint_re_set_default (b);
13108
f00aae0f
KS
13109 /* extra_string should never be non-NULL for dprintf. */
13110 gdb_assert (b->extra_string != NULL);
5c2b4418
HZ
13111
13112 /* 1 - connect to target 1, that can run breakpoint commands.
13113 2 - create a dprintf, which resolves fine.
13114 3 - disconnect from target 1
13115 4 - connect to target 2, that can NOT run breakpoint commands.
13116
13117 After steps #3/#4, you'll want the dprintf command list to
13118 be updated, because target 1 and 2 may well return different
13119 answers for target_can_run_breakpoint_commands().
13120 Given absence of finer grained resetting, we get to do
13121 it all the time. */
13122 if (b->extra_string != NULL)
13123 update_dprintf_command_list (b);
13124}
13125
2d9442cc
HZ
13126/* Implement the "print_recreate" breakpoint_ops method for dprintf. */
13127
13128static void
13129dprintf_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13130{
f00aae0f 13131 fprintf_unfiltered (fp, "dprintf %s,%s",
d28cd78a 13132 event_location_to_string (tp->location.get ()),
2d9442cc
HZ
13133 tp->extra_string);
13134 print_recreate_thread (tp, fp);
13135}
13136
9d6e6e84
HZ
13137/* Implement the "after_condition_true" breakpoint_ops method for
13138 dprintf.
13139
13140 dprintf's are implemented with regular commands in their command
13141 list, but we run the commands here instead of before presenting the
13142 stop to the user, as dprintf's don't actually cause a stop. This
13143 also makes it so that the commands of multiple dprintfs at the same
13144 address are all handled. */
13145
13146static void
13147dprintf_after_condition_true (struct bpstats *bs)
13148{
04afa70c 13149 struct bpstats tmp_bs;
9d6e6e84
HZ
13150 struct bpstats *tmp_bs_p = &tmp_bs;
13151
13152 /* dprintf's never cause a stop. This wasn't set in the
13153 check_status hook instead because that would make the dprintf's
13154 condition not be evaluated. */
13155 bs->stop = 0;
13156
13157 /* Run the command list here. Take ownership of it instead of
13158 copying. We never want these commands to run later in
13159 bpstat_do_actions, if a breakpoint that causes a stop happens to
13160 be set at same address as this dprintf, or even if running the
13161 commands here throws. */
13162 tmp_bs.commands = bs->commands;
13163 bs->commands = NULL;
9d6e6e84
HZ
13164
13165 bpstat_do_actions_1 (&tmp_bs_p);
13166
13167 /* 'tmp_bs.commands' will usually be NULL by now, but
13168 bpstat_do_actions_1 may return early without processing the whole
13169 list. */
9d6e6e84
HZ
13170}
13171
983af33b
SDJ
13172/* The breakpoint_ops structure to be used on static tracepoints with
13173 markers (`-m'). */
13174
13175static void
626d2320 13176strace_marker_create_sals_from_location (struct event_location *location,
5f700d83 13177 struct linespec_result *canonical,
f00aae0f 13178 enum bptype type_wanted)
983af33b
SDJ
13179{
13180 struct linespec_sals lsal;
f00aae0f 13181 const char *arg_start, *arg;
983af33b 13182
a20714ff 13183 arg = arg_start = get_linespec_location (location)->spec_string;
f00aae0f 13184 lsal.sals = decode_static_tracepoint_spec (&arg);
983af33b 13185
f2fc3015
TT
13186 std::string str (arg_start, arg - arg_start);
13187 const char *ptr = str.c_str ();
a20714ff
PA
13188 canonical->location
13189 = new_linespec_location (&ptr, symbol_name_match_type::FULL);
983af33b 13190
8e9e35b1
TT
13191 lsal.canonical
13192 = xstrdup (event_location_to_string (canonical->location.get ()));
6c5b2ebe 13193 canonical->lsals.push_back (std::move (lsal));
983af33b
SDJ
13194}
13195
13196static void
13197strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
13198 struct linespec_result *canonical,
e1e01040
PA
13199 gdb::unique_xmalloc_ptr<char> cond_string,
13200 gdb::unique_xmalloc_ptr<char> extra_string,
983af33b
SDJ
13201 enum bptype type_wanted,
13202 enum bpdisp disposition,
13203 int thread,
13204 int task, int ignore_count,
13205 const struct breakpoint_ops *ops,
13206 int from_tty, int enabled,
44f238bb 13207 int internal, unsigned flags)
983af33b 13208{
6c5b2ebe 13209 const linespec_sals &lsal = canonical->lsals[0];
983af33b
SDJ
13210
13211 /* If the user is creating a static tracepoint by marker id
13212 (strace -m MARKER_ID), then store the sals index, so that
13213 breakpoint_re_set can try to match up which of the newly
13214 found markers corresponds to this one, and, don't try to
13215 expand multiple locations for each sal, given than SALS
13216 already should contain all sals for MARKER_ID. */
13217
6c5b2ebe 13218 for (size_t i = 0; i < lsal.sals.size (); i++)
983af33b 13219 {
6c5b2ebe
PA
13220 event_location_up location
13221 = copy_event_location (canonical->location.get ());
983af33b 13222
b270e6f9 13223 std::unique_ptr<tracepoint> tp (new tracepoint ());
6c5b2ebe 13224 init_breakpoint_sal (tp.get (), gdbarch, lsal.sals[i],
ffc2605c 13225 std::move (location), NULL,
e1e01040
PA
13226 std::move (cond_string),
13227 std::move (extra_string),
e7e0cddf 13228 type_wanted, disposition,
983af33b 13229 thread, task, ignore_count, ops,
44f238bb 13230 from_tty, enabled, internal, flags,
983af33b
SDJ
13231 canonical->special_display);
13232 /* Given that its possible to have multiple markers with
13233 the same string id, if the user is creating a static
13234 tracepoint by marker id ("strace -m MARKER_ID"), then
13235 store the sals index, so that breakpoint_re_set can
13236 try to match up which of the newly found markers
13237 corresponds to this one */
13238 tp->static_trace_marker_id_idx = i;
13239
b270e6f9 13240 install_breakpoint (internal, std::move (tp), 0);
983af33b
SDJ
13241 }
13242}
13243
6c5b2ebe 13244static std::vector<symtab_and_line>
f00aae0f 13245strace_marker_decode_location (struct breakpoint *b,
626d2320 13246 struct event_location *location,
6c5b2ebe 13247 struct program_space *search_pspace)
983af33b
SDJ
13248{
13249 struct tracepoint *tp = (struct tracepoint *) b;
a20714ff 13250 const char *s = get_linespec_location (location)->spec_string;
983af33b 13251
6c5b2ebe
PA
13252 std::vector<symtab_and_line> sals = decode_static_tracepoint_spec (&s);
13253 if (sals.size () > tp->static_trace_marker_id_idx)
983af33b 13254 {
6c5b2ebe
PA
13255 sals[0] = sals[tp->static_trace_marker_id_idx];
13256 sals.resize (1);
13257 return sals;
983af33b
SDJ
13258 }
13259 else
5d9310c4 13260 error (_("marker %s not found"), tp->static_trace_marker_id.c_str ());
983af33b
SDJ
13261}
13262
13263static struct breakpoint_ops strace_marker_breakpoint_ops;
13264
13265static int
13266strace_marker_p (struct breakpoint *b)
13267{
13268 return b->ops == &strace_marker_breakpoint_ops;
13269}
13270
53a5351d 13271/* Delete a breakpoint and clean up all traces of it in the data
f431efe5 13272 structures. */
c906108c
SS
13273
13274void
fba45db2 13275delete_breakpoint (struct breakpoint *bpt)
c906108c 13276{
52f0bd74 13277 struct breakpoint *b;
c906108c 13278
8a3fe4f8 13279 gdb_assert (bpt != NULL);
c906108c 13280
4a64f543
MS
13281 /* Has this bp already been deleted? This can happen because
13282 multiple lists can hold pointers to bp's. bpstat lists are
13283 especial culprits.
13284
13285 One example of this happening is a watchpoint's scope bp. When
13286 the scope bp triggers, we notice that the watchpoint is out of
13287 scope, and delete it. We also delete its scope bp. But the
13288 scope bp is marked "auto-deleting", and is already on a bpstat.
13289 That bpstat is then checked for auto-deleting bp's, which are
13290 deleted.
13291
13292 A real solution to this problem might involve reference counts in
13293 bp's, and/or giving them pointers back to their referencing
13294 bpstat's, and teaching delete_breakpoint to only free a bp's
13295 storage when no more references were extent. A cheaper bandaid
13296 was chosen. */
c906108c
SS
13297 if (bpt->type == bp_none)
13298 return;
13299
4a64f543
MS
13300 /* At least avoid this stale reference until the reference counting
13301 of breakpoints gets resolved. */
d0fb5eae 13302 if (bpt->related_breakpoint != bpt)
e5a0a904 13303 {
d0fb5eae 13304 struct breakpoint *related;
3a5c3e22 13305 struct watchpoint *w;
d0fb5eae
JK
13306
13307 if (bpt->type == bp_watchpoint_scope)
3a5c3e22 13308 w = (struct watchpoint *) bpt->related_breakpoint;
d0fb5eae 13309 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
3a5c3e22
PA
13310 w = (struct watchpoint *) bpt;
13311 else
13312 w = NULL;
13313 if (w != NULL)
13314 watchpoint_del_at_next_stop (w);
d0fb5eae
JK
13315
13316 /* Unlink bpt from the bpt->related_breakpoint ring. */
13317 for (related = bpt; related->related_breakpoint != bpt;
13318 related = related->related_breakpoint);
13319 related->related_breakpoint = bpt->related_breakpoint;
13320 bpt->related_breakpoint = bpt;
e5a0a904
JK
13321 }
13322
a9634178
TJB
13323 /* watch_command_1 creates a watchpoint but only sets its number if
13324 update_watchpoint succeeds in creating its bp_locations. If there's
13325 a problem in that process, we'll be asked to delete the half-created
13326 watchpoint. In that case, don't announce the deletion. */
13327 if (bpt->number)
76727919 13328 gdb::observers::breakpoint_deleted.notify (bpt);
c906108c 13329
c906108c
SS
13330 if (breakpoint_chain == bpt)
13331 breakpoint_chain = bpt->next;
13332
c906108c
SS
13333 ALL_BREAKPOINTS (b)
13334 if (b->next == bpt)
c5aa993b
JM
13335 {
13336 b->next = bpt->next;
13337 break;
13338 }
c906108c 13339
f431efe5
PA
13340 /* Be sure no bpstat's are pointing at the breakpoint after it's
13341 been freed. */
13342 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
e5dd4106 13343 in all threads for now. Note that we cannot just remove bpstats
f431efe5
PA
13344 pointing at bpt from the stop_bpstat list entirely, as breakpoint
13345 commands are associated with the bpstat; if we remove it here,
13346 then the later call to bpstat_do_actions (&stop_bpstat); in
13347 event-top.c won't do anything, and temporary breakpoints with
13348 commands won't work. */
13349
13350 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
13351
4a64f543
MS
13352 /* Now that breakpoint is removed from breakpoint list, update the
13353 global location list. This will remove locations that used to
13354 belong to this breakpoint. Do this before freeing the breakpoint
13355 itself, since remove_breakpoint looks at location's owner. It
13356 might be better design to have location completely
13357 self-contained, but it's not the case now. */
44702360 13358 update_global_location_list (UGLL_DONT_INSERT);
74960c60 13359
4a64f543
MS
13360 /* On the chance that someone will soon try again to delete this
13361 same bp, we mark it as deleted before freeing its storage. */
c906108c 13362 bpt->type = bp_none;
4d01a485 13363 delete bpt;
c906108c
SS
13364}
13365
51be5b68
PA
13366/* Iterator function to call a user-provided callback function once
13367 for each of B and its related breakpoints. */
13368
13369static void
13370iterate_over_related_breakpoints (struct breakpoint *b,
48649e1b 13371 gdb::function_view<void (breakpoint *)> function)
51be5b68
PA
13372{
13373 struct breakpoint *related;
13374
13375 related = b;
13376 do
13377 {
13378 struct breakpoint *next;
13379
13380 /* FUNCTION may delete RELATED. */
13381 next = related->related_breakpoint;
13382
13383 if (next == related)
13384 {
13385 /* RELATED is the last ring entry. */
48649e1b 13386 function (related);
51be5b68
PA
13387
13388 /* FUNCTION may have deleted it, so we'd never reach back to
13389 B. There's nothing left to do anyway, so just break
13390 out. */
13391 break;
13392 }
13393 else
48649e1b 13394 function (related);
51be5b68
PA
13395
13396 related = next;
13397 }
13398 while (related != b);
13399}
95a42b64 13400
4495129a 13401static void
981a3fb3 13402delete_command (const char *arg, int from_tty)
c906108c 13403{
35df4500 13404 struct breakpoint *b, *b_tmp;
c906108c 13405
ea9365bb
TT
13406 dont_repeat ();
13407
c906108c
SS
13408 if (arg == 0)
13409 {
13410 int breaks_to_delete = 0;
13411
46c6471b 13412 /* Delete all breakpoints if no argument. Do not delete
dda83cd7
SM
13413 internal breakpoints, these have to be deleted with an
13414 explicit breakpoint number argument. */
c5aa993b 13415 ALL_BREAKPOINTS (b)
46c6471b 13416 if (user_breakpoint_p (b))
973d738b
DJ
13417 {
13418 breaks_to_delete = 1;
13419 break;
13420 }
c906108c
SS
13421
13422 /* Ask user only if there are some breakpoints to delete. */
13423 if (!from_tty
e2e0b3e5 13424 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
c906108c 13425 {
35df4500 13426 ALL_BREAKPOINTS_SAFE (b, b_tmp)
46c6471b 13427 if (user_breakpoint_p (b))
c5aa993b 13428 delete_breakpoint (b);
c906108c
SS
13429 }
13430 }
13431 else
48649e1b 13432 map_breakpoint_numbers
b926417a 13433 (arg, [&] (breakpoint *br)
48649e1b 13434 {
b926417a 13435 iterate_over_related_breakpoints (br, delete_breakpoint);
48649e1b 13436 });
c906108c
SS
13437}
13438
c2f4122d
PA
13439/* Return true if all locations of B bound to PSPACE are pending. If
13440 PSPACE is NULL, all locations of all program spaces are
13441 considered. */
13442
0d381245 13443static int
c2f4122d 13444all_locations_are_pending (struct breakpoint *b, struct program_space *pspace)
fe3f5fa8 13445{
c2f4122d
PA
13446 struct bp_location *loc;
13447
13448 for (loc = b->loc; loc != NULL; loc = loc->next)
13449 if ((pspace == NULL
13450 || loc->pspace == pspace)
13451 && !loc->shlib_disabled
8645ff69 13452 && !loc->pspace->executing_startup)
0d381245
VP
13453 return 0;
13454 return 1;
fe3f5fa8
VP
13455}
13456
776592bf
DE
13457/* Subroutine of update_breakpoint_locations to simplify it.
13458 Return non-zero if multiple fns in list LOC have the same name.
13459 Null names are ignored. */
13460
13461static int
13462ambiguous_names_p (struct bp_location *loc)
13463{
13464 struct bp_location *l;
c1fb9836
TT
13465 htab_up htab (htab_create_alloc (13, htab_hash_string, streq_hash, NULL,
13466 xcalloc, xfree));
776592bf
DE
13467
13468 for (l = loc; l != NULL; l = l->next)
13469 {
13470 const char **slot;
13471 const char *name = l->function_name;
13472
13473 /* Allow for some names to be NULL, ignore them. */
13474 if (name == NULL)
13475 continue;
13476
c1fb9836 13477 slot = (const char **) htab_find_slot (htab.get (), (const void *) name,
776592bf 13478 INSERT);
4a64f543
MS
13479 /* NOTE: We can assume slot != NULL here because xcalloc never
13480 returns NULL. */
776592bf 13481 if (*slot != NULL)
c1fb9836 13482 return 1;
776592bf
DE
13483 *slot = name;
13484 }
13485
776592bf
DE
13486 return 0;
13487}
13488
0fb4aa4b
PA
13489/* When symbols change, it probably means the sources changed as well,
13490 and it might mean the static tracepoint markers are no longer at
13491 the same address or line numbers they used to be at last we
13492 checked. Losing your static tracepoints whenever you rebuild is
13493 undesirable. This function tries to resync/rematch gdb static
13494 tracepoints with the markers on the target, for static tracepoints
13495 that have not been set by marker id. Static tracepoint that have
13496 been set by marker id are reset by marker id in breakpoint_re_set.
13497 The heuristic is:
13498
13499 1) For a tracepoint set at a specific address, look for a marker at
13500 the old PC. If one is found there, assume to be the same marker.
13501 If the name / string id of the marker found is different from the
13502 previous known name, assume that means the user renamed the marker
13503 in the sources, and output a warning.
13504
13505 2) For a tracepoint set at a given line number, look for a marker
13506 at the new address of the old line number. If one is found there,
13507 assume to be the same marker. If the name / string id of the
13508 marker found is different from the previous known name, assume that
13509 means the user renamed the marker in the sources, and output a
13510 warning.
13511
13512 3) If a marker is no longer found at the same address or line, it
13513 may mean the marker no longer exists. But it may also just mean
13514 the code changed a bit. Maybe the user added a few lines of code
13515 that made the marker move up or down (in line number terms). Ask
13516 the target for info about the marker with the string id as we knew
13517 it. If found, update line number and address in the matching
13518 static tracepoint. This will get confused if there's more than one
13519 marker with the same ID (possible in UST, although unadvised
13520 precisely because it confuses tools). */
13521
13522static struct symtab_and_line
13523update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
13524{
d9b3f62e 13525 struct tracepoint *tp = (struct tracepoint *) b;
0fb4aa4b
PA
13526 struct static_tracepoint_marker marker;
13527 CORE_ADDR pc;
0fb4aa4b
PA
13528
13529 pc = sal.pc;
13530 if (sal.line)
13531 find_line_pc (sal.symtab, sal.line, &pc);
13532
13533 if (target_static_tracepoint_marker_at (pc, &marker))
13534 {
5d9310c4 13535 if (tp->static_trace_marker_id != marker.str_id)
0fb4aa4b 13536 warning (_("static tracepoint %d changed probed marker from %s to %s"),
5d9310c4
SM
13537 b->number, tp->static_trace_marker_id.c_str (),
13538 marker.str_id.c_str ());
0fb4aa4b 13539
5d9310c4 13540 tp->static_trace_marker_id = std::move (marker.str_id);
0fb4aa4b
PA
13541
13542 return sal;
13543 }
13544
13545 /* Old marker wasn't found on target at lineno. Try looking it up
13546 by string ID. */
13547 if (!sal.explicit_pc
13548 && sal.line != 0
13549 && sal.symtab != NULL
5d9310c4 13550 && !tp->static_trace_marker_id.empty ())
0fb4aa4b 13551 {
5d9310c4
SM
13552 std::vector<static_tracepoint_marker> markers
13553 = target_static_tracepoint_markers_by_strid
13554 (tp->static_trace_marker_id.c_str ());
0fb4aa4b 13555
5d9310c4 13556 if (!markers.empty ())
0fb4aa4b 13557 {
0fb4aa4b 13558 struct symbol *sym;
80e1d417 13559 struct static_tracepoint_marker *tpmarker;
79a45e25 13560 struct ui_out *uiout = current_uiout;
67994074 13561 struct explicit_location explicit_loc;
0fb4aa4b 13562
5d9310c4 13563 tpmarker = &markers[0];
0fb4aa4b 13564
5d9310c4 13565 tp->static_trace_marker_id = std::move (tpmarker->str_id);
0fb4aa4b
PA
13566
13567 warning (_("marker for static tracepoint %d (%s) not "
13568 "found at previous line number"),
5d9310c4 13569 b->number, tp->static_trace_marker_id.c_str ());
0fb4aa4b 13570
51abb421 13571 symtab_and_line sal2 = find_pc_line (tpmarker->address, 0);
80e1d417 13572 sym = find_pc_sect_function (tpmarker->address, NULL);
112e8700 13573 uiout->text ("Now in ");
0fb4aa4b
PA
13574 if (sym)
13575 {
987012b8 13576 uiout->field_string ("func", sym->print_name (),
e43b10e1 13577 function_name_style.style ());
112e8700 13578 uiout->text (" at ");
0fb4aa4b 13579 }
112e8700 13580 uiout->field_string ("file",
cbe56571 13581 symtab_to_filename_for_display (sal2.symtab),
e43b10e1 13582 file_name_style.style ());
112e8700 13583 uiout->text (":");
0fb4aa4b 13584
112e8700 13585 if (uiout->is_mi_like_p ())
0fb4aa4b 13586 {
0b0865da 13587 const char *fullname = symtab_to_fullname (sal2.symtab);
0fb4aa4b 13588
112e8700 13589 uiout->field_string ("fullname", fullname);
0fb4aa4b
PA
13590 }
13591
381befee 13592 uiout->field_signed ("line", sal2.line);
112e8700 13593 uiout->text ("\n");
0fb4aa4b 13594
80e1d417 13595 b->loc->line_number = sal2.line;
2f202fde 13596 b->loc->symtab = sym != NULL ? sal2.symtab : NULL;
0fb4aa4b 13597
d28cd78a 13598 b->location.reset (NULL);
67994074
KS
13599 initialize_explicit_location (&explicit_loc);
13600 explicit_loc.source_filename
00e52e53 13601 = ASTRDUP (symtab_to_filename_for_display (sal2.symtab));
67994074
KS
13602 explicit_loc.line_offset.offset = b->loc->line_number;
13603 explicit_loc.line_offset.sign = LINE_OFFSET_NONE;
d28cd78a 13604 b->location = new_explicit_location (&explicit_loc);
0fb4aa4b
PA
13605
13606 /* Might be nice to check if function changed, and warn if
13607 so. */
0fb4aa4b
PA
13608 }
13609 }
13610 return sal;
13611}
13612
8d3788bd
VP
13613/* Returns 1 iff locations A and B are sufficiently same that
13614 we don't need to report breakpoint as changed. */
13615
13616static int
13617locations_are_equal (struct bp_location *a, struct bp_location *b)
13618{
13619 while (a && b)
13620 {
13621 if (a->address != b->address)
13622 return 0;
13623
13624 if (a->shlib_disabled != b->shlib_disabled)
13625 return 0;
13626
13627 if (a->enabled != b->enabled)
13628 return 0;
13629
b5fa468f
TBA
13630 if (a->disabled_by_cond != b->disabled_by_cond)
13631 return 0;
13632
8d3788bd
VP
13633 a = a->next;
13634 b = b->next;
13635 }
13636
13637 if ((a == NULL) != (b == NULL))
13638 return 0;
13639
13640 return 1;
13641}
13642
c2f4122d
PA
13643/* Split all locations of B that are bound to PSPACE out of B's
13644 location list to a separate list and return that list's head. If
13645 PSPACE is NULL, hoist out all locations of B. */
13646
13647static struct bp_location *
13648hoist_existing_locations (struct breakpoint *b, struct program_space *pspace)
13649{
13650 struct bp_location head;
13651 struct bp_location *i = b->loc;
13652 struct bp_location **i_link = &b->loc;
13653 struct bp_location *hoisted = &head;
13654
13655 if (pspace == NULL)
13656 {
13657 i = b->loc;
13658 b->loc = NULL;
13659 return i;
13660 }
13661
13662 head.next = NULL;
13663
13664 while (i != NULL)
13665 {
13666 if (i->pspace == pspace)
13667 {
13668 *i_link = i->next;
13669 i->next = NULL;
13670 hoisted->next = i;
13671 hoisted = i;
13672 }
13673 else
13674 i_link = &i->next;
13675 i = *i_link;
13676 }
13677
13678 return head.next;
13679}
13680
13681/* Create new breakpoint locations for B (a hardware or software
13682 breakpoint) based on SALS and SALS_END. If SALS_END.NELTS is not
13683 zero, then B is a ranged breakpoint. Only recreates locations for
13684 FILTER_PSPACE. Locations of other program spaces are left
13685 untouched. */
f1310107 13686
0e30163f 13687void
0d381245 13688update_breakpoint_locations (struct breakpoint *b,
c2f4122d 13689 struct program_space *filter_pspace,
6c5b2ebe
PA
13690 gdb::array_view<const symtab_and_line> sals,
13691 gdb::array_view<const symtab_and_line> sals_end)
fe3f5fa8 13692{
c2f4122d 13693 struct bp_location *existing_locations;
0d381245 13694
6c5b2ebe 13695 if (!sals_end.empty () && (sals.size () != 1 || sals_end.size () != 1))
f8eba3c6
TT
13696 {
13697 /* Ranged breakpoints have only one start location and one end
13698 location. */
13699 b->enable_state = bp_disabled;
f8eba3c6
TT
13700 printf_unfiltered (_("Could not reset ranged breakpoint %d: "
13701 "multiple locations found\n"),
13702 b->number);
13703 return;
13704 }
f1310107 13705
4a64f543
MS
13706 /* If there's no new locations, and all existing locations are
13707 pending, don't do anything. This optimizes the common case where
13708 all locations are in the same shared library, that was unloaded.
13709 We'd like to retain the location, so that when the library is
13710 loaded again, we don't loose the enabled/disabled status of the
13711 individual locations. */
6c5b2ebe 13712 if (all_locations_are_pending (b, filter_pspace) && sals.empty ())
fe3f5fa8
VP
13713 return;
13714
c2f4122d 13715 existing_locations = hoist_existing_locations (b, filter_pspace);
fe3f5fa8 13716
6c5b2ebe 13717 for (const auto &sal : sals)
fe3f5fa8 13718 {
f8eba3c6
TT
13719 struct bp_location *new_loc;
13720
6c5b2ebe 13721 switch_to_program_space_and_thread (sal.pspace);
f8eba3c6 13722
6c5b2ebe 13723 new_loc = add_location_to_breakpoint (b, &sal);
fe3f5fa8 13724
0d381245
VP
13725 /* Reparse conditions, they might contain references to the
13726 old symtab. */
13727 if (b->cond_string != NULL)
13728 {
bbc13ae3 13729 const char *s;
fe3f5fa8 13730
0d381245 13731 s = b->cond_string;
a70b8144 13732 try
0d381245 13733 {
6c5b2ebe
PA
13734 new_loc->cond = parse_exp_1 (&s, sal.pc,
13735 block_for_pc (sal.pc),
0d381245
VP
13736 0);
13737 }
230d2906 13738 catch (const gdb_exception_error &e)
0d381245 13739 {
b5fa468f 13740 new_loc->disabled_by_cond = true;
0d381245
VP
13741 }
13742 }
fe3f5fa8 13743
6c5b2ebe 13744 if (!sals_end.empty ())
f1310107 13745 {
6c5b2ebe 13746 CORE_ADDR end = find_breakpoint_range_end (sals_end[0]);
f1310107 13747
6c5b2ebe 13748 new_loc->length = end - sals[0].pc + 1;
f1310107 13749 }
0d381245 13750 }
fe3f5fa8 13751
4a64f543
MS
13752 /* If possible, carry over 'disable' status from existing
13753 breakpoints. */
0d381245
VP
13754 {
13755 struct bp_location *e = existing_locations;
776592bf
DE
13756 /* If there are multiple breakpoints with the same function name,
13757 e.g. for inline functions, comparing function names won't work.
13758 Instead compare pc addresses; this is just a heuristic as things
13759 may have moved, but in practice it gives the correct answer
13760 often enough until a better solution is found. */
13761 int have_ambiguous_names = ambiguous_names_p (b->loc);
13762
0d381245
VP
13763 for (; e; e = e->next)
13764 {
b5fa468f 13765 if ((!e->enabled || e->disabled_by_cond) && e->function_name)
0d381245
VP
13766 {
13767 struct bp_location *l = b->loc;
776592bf
DE
13768 if (have_ambiguous_names)
13769 {
13770 for (; l; l = l->next)
7f32a4d5
PA
13771 {
13772 /* Ignore software vs hardware location type at
13773 this point, because with "set breakpoint
13774 auto-hw", after a re-set, locations that were
13775 hardware can end up as software, or vice versa.
13776 As mentioned above, this is an heuristic and in
13777 practice should give the correct answer often
13778 enough. */
13779 if (breakpoint_locations_match (e, l, true))
13780 {
b5fa468f
TBA
13781 l->enabled = e->enabled;
13782 l->disabled_by_cond = e->disabled_by_cond;
7f32a4d5
PA
13783 break;
13784 }
13785 }
776592bf
DE
13786 }
13787 else
13788 {
13789 for (; l; l = l->next)
13790 if (l->function_name
13791 && strcmp (e->function_name, l->function_name) == 0)
13792 {
b5fa468f
TBA
13793 l->enabled = e->enabled;
13794 l->disabled_by_cond = e->disabled_by_cond;
776592bf
DE
13795 break;
13796 }
13797 }
0d381245
VP
13798 }
13799 }
13800 }
fe3f5fa8 13801
8d3788bd 13802 if (!locations_are_equal (existing_locations, b->loc))
76727919 13803 gdb::observers::breakpoint_modified.notify (b);
fe3f5fa8
VP
13804}
13805
f00aae0f 13806/* Find the SaL locations corresponding to the given LOCATION.
ef23e705
TJB
13807 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
13808
6c5b2ebe 13809static std::vector<symtab_and_line>
f00aae0f 13810location_to_sals (struct breakpoint *b, struct event_location *location,
c2f4122d 13811 struct program_space *search_pspace, int *found)
ef23e705 13812{
cc06b668 13813 struct gdb_exception exception;
ef23e705 13814
983af33b 13815 gdb_assert (b->ops != NULL);
ef23e705 13816
6c5b2ebe
PA
13817 std::vector<symtab_and_line> sals;
13818
a70b8144 13819 try
ef23e705 13820 {
6c5b2ebe 13821 sals = b->ops->decode_location (b, location, search_pspace);
ef23e705 13822 }
94aeb44b 13823 catch (gdb_exception_error &e)
ef23e705
TJB
13824 {
13825 int not_found_and_ok = 0;
492d29ea 13826
ef23e705
TJB
13827 /* For pending breakpoints, it's expected that parsing will
13828 fail until the right shared library is loaded. User has
13829 already told to create pending breakpoints and don't need
13830 extra messages. If breakpoint is in bp_shlib_disabled
13831 state, then user already saw the message about that
13832 breakpoint being disabled, and don't want to see more
13833 errors. */
58438ac1 13834 if (e.error == NOT_FOUND_ERROR
c2f4122d
PA
13835 && (b->condition_not_parsed
13836 || (b->loc != NULL
13837 && search_pspace != NULL
13838 && b->loc->pspace != search_pspace)
ef23e705 13839 || (b->loc && b->loc->shlib_disabled)
f8eba3c6 13840 || (b->loc && b->loc->pspace->executing_startup)
ef23e705
TJB
13841 || b->enable_state == bp_disabled))
13842 not_found_and_ok = 1;
13843
13844 if (!not_found_and_ok)
13845 {
13846 /* We surely don't want to warn about the same breakpoint
13847 10 times. One solution, implemented here, is disable
13848 the breakpoint on error. Another solution would be to
13849 have separate 'warning emitted' flag. Since this
13850 happens only when a binary has changed, I don't know
13851 which approach is better. */
13852 b->enable_state = bp_disabled;
eedc3f4f 13853 throw;
ef23e705 13854 }
94aeb44b
TT
13855
13856 exception = std::move (e);
ef23e705
TJB
13857 }
13858
492d29ea 13859 if (exception.reason == 0 || exception.error != NOT_FOUND_ERROR)
ef23e705 13860 {
6c5b2ebe
PA
13861 for (auto &sal : sals)
13862 resolve_sal_pc (&sal);
f00aae0f 13863 if (b->condition_not_parsed && b->extra_string != NULL)
ef23e705 13864 {
ed1d1739
KS
13865 char *cond_string, *extra_string;
13866 int thread, task;
ef23e705 13867
b5fa468f
TBA
13868 find_condition_and_thread_for_sals (sals, b->extra_string,
13869 &cond_string, &thread,
13870 &task, &extra_string);
f00aae0f 13871 gdb_assert (b->cond_string == NULL);
ef23e705
TJB
13872 if (cond_string)
13873 b->cond_string = cond_string;
13874 b->thread = thread;
13875 b->task = task;
e7e0cddf 13876 if (extra_string)
f00aae0f
KS
13877 {
13878 xfree (b->extra_string);
13879 b->extra_string = extra_string;
13880 }
ef23e705
TJB
13881 b->condition_not_parsed = 0;
13882 }
13883
983af33b 13884 if (b->type == bp_static_tracepoint && !strace_marker_p (b))
6c5b2ebe 13885 sals[0] = update_static_tracepoint (b, sals[0]);
ef23e705 13886
58438ac1
TT
13887 *found = 1;
13888 }
13889 else
13890 *found = 0;
ef23e705
TJB
13891
13892 return sals;
13893}
13894
348d480f
PA
13895/* The default re_set method, for typical hardware or software
13896 breakpoints. Reevaluate the breakpoint and recreate its
13897 locations. */
13898
13899static void
28010a5d 13900breakpoint_re_set_default (struct breakpoint *b)
ef23e705 13901{
c2f4122d 13902 struct program_space *filter_pspace = current_program_space;
6c5b2ebe 13903 std::vector<symtab_and_line> expanded, expanded_end;
ef23e705 13904
6c5b2ebe
PA
13905 int found;
13906 std::vector<symtab_and_line> sals = location_to_sals (b, b->location.get (),
13907 filter_pspace, &found);
ef23e705 13908 if (found)
6c5b2ebe 13909 expanded = std::move (sals);
ef23e705 13910
f00aae0f 13911 if (b->location_range_end != NULL)
f1310107 13912 {
6c5b2ebe
PA
13913 std::vector<symtab_and_line> sals_end
13914 = location_to_sals (b, b->location_range_end.get (),
13915 filter_pspace, &found);
f1310107 13916 if (found)
6c5b2ebe 13917 expanded_end = std::move (sals_end);
f1310107
TJB
13918 }
13919
c2f4122d 13920 update_breakpoint_locations (b, filter_pspace, expanded, expanded_end);
28010a5d
PA
13921}
13922
983af33b
SDJ
13923/* Default method for creating SALs from an address string. It basically
13924 calls parse_breakpoint_sals. Return 1 for success, zero for failure. */
13925
13926static void
626d2320 13927create_sals_from_location_default (struct event_location *location,
f00aae0f
KS
13928 struct linespec_result *canonical,
13929 enum bptype type_wanted)
983af33b 13930{
f00aae0f 13931 parse_breakpoint_sals (location, canonical);
983af33b
SDJ
13932}
13933
13934/* Call create_breakpoints_sal for the given arguments. This is the default
13935 function for the `create_breakpoints_sal' method of
13936 breakpoint_ops. */
13937
13938static void
13939create_breakpoints_sal_default (struct gdbarch *gdbarch,
13940 struct linespec_result *canonical,
e1e01040
PA
13941 gdb::unique_xmalloc_ptr<char> cond_string,
13942 gdb::unique_xmalloc_ptr<char> extra_string,
983af33b
SDJ
13943 enum bptype type_wanted,
13944 enum bpdisp disposition,
13945 int thread,
13946 int task, int ignore_count,
13947 const struct breakpoint_ops *ops,
13948 int from_tty, int enabled,
44f238bb 13949 int internal, unsigned flags)
983af33b 13950{
e1e01040
PA
13951 create_breakpoints_sal (gdbarch, canonical,
13952 std::move (cond_string),
13953 std::move (extra_string),
983af33b
SDJ
13954 type_wanted, disposition,
13955 thread, task, ignore_count, ops, from_tty,
44f238bb 13956 enabled, internal, flags);
983af33b
SDJ
13957}
13958
13959/* Decode the line represented by S by calling decode_line_full. This is the
5f700d83 13960 default function for the `decode_location' method of breakpoint_ops. */
983af33b 13961
6c5b2ebe 13962static std::vector<symtab_and_line>
f00aae0f 13963decode_location_default (struct breakpoint *b,
626d2320 13964 struct event_location *location,
6c5b2ebe 13965 struct program_space *search_pspace)
983af33b
SDJ
13966{
13967 struct linespec_result canonical;
13968
c2f4122d 13969 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, search_pspace,
cafb3438 13970 NULL, 0, &canonical, multiple_symbols_all,
c0e8dcd8 13971 b->filter.get ());
983af33b
SDJ
13972
13973 /* We should get 0 or 1 resulting SALs. */
6c5b2ebe 13974 gdb_assert (canonical.lsals.size () < 2);
983af33b 13975
6c5b2ebe 13976 if (!canonical.lsals.empty ())
983af33b 13977 {
6c5b2ebe
PA
13978 const linespec_sals &lsal = canonical.lsals[0];
13979 return std::move (lsal.sals);
983af33b 13980 }
6c5b2ebe 13981 return {};
983af33b
SDJ
13982}
13983
bf469271 13984/* Reset a breakpoint. */
c906108c 13985
bf469271
PA
13986static void
13987breakpoint_re_set_one (breakpoint *b)
c906108c 13988{
fdf44873
TT
13989 input_radix = b->input_radix;
13990 set_language (b->language);
c906108c 13991
348d480f 13992 b->ops->re_set (b);
c906108c
SS
13993}
13994
c2f4122d
PA
13995/* Re-set breakpoint locations for the current program space.
13996 Locations bound to other program spaces are left untouched. */
13997
c906108c 13998void
69de3c6a 13999breakpoint_re_set (void)
c906108c 14000{
35df4500 14001 struct breakpoint *b, *b_tmp;
2a7f3dff 14002
c5aa993b 14003 {
fdf44873
TT
14004 scoped_restore_current_language save_language;
14005 scoped_restore save_input_radix = make_scoped_restore (&input_radix);
5ed8105e 14006 scoped_restore_current_pspace_and_thread restore_pspace_thread;
e62c965a 14007
8e817061
JB
14008 /* breakpoint_re_set_one sets the current_language to the language
14009 of the breakpoint it is resetting (see prepare_re_set_context)
14010 before re-evaluating the breakpoint's location. This change can
14011 unfortunately get undone by accident if the language_mode is set
14012 to auto, and we either switch frames, or more likely in this context,
14013 we select the current frame.
14014
14015 We prevent this by temporarily turning the language_mode to
14016 language_mode_manual. We restore it once all breakpoints
14017 have been reset. */
14018 scoped_restore save_language_mode = make_scoped_restore (&language_mode);
14019 language_mode = language_mode_manual;
14020
5ed8105e
PA
14021 /* Note: we must not try to insert locations until after all
14022 breakpoints have been re-set. Otherwise, e.g., when re-setting
14023 breakpoint 1, we'd insert the locations of breakpoint 2, which
14024 hadn't been re-set yet, and thus may have stale locations. */
4efc6507 14025
5ed8105e
PA
14026 ALL_BREAKPOINTS_SAFE (b, b_tmp)
14027 {
a70b8144 14028 try
bf469271
PA
14029 {
14030 breakpoint_re_set_one (b);
14031 }
230d2906 14032 catch (const gdb_exception &ex)
bf469271
PA
14033 {
14034 exception_fprintf (gdb_stderr, ex,
14035 "Error in re-setting breakpoint %d: ",
14036 b->number);
14037 }
5ed8105e 14038 }
5ed8105e
PA
14039
14040 jit_breakpoint_re_set ();
14041 }
6c95b8df 14042
af02033e
PP
14043 create_overlay_event_breakpoint ();
14044 create_longjmp_master_breakpoint ();
14045 create_std_terminate_master_breakpoint ();
186c406b 14046 create_exception_master_breakpoint ();
2a7f3dff
PA
14047
14048 /* Now we can insert. */
14049 update_global_location_list (UGLL_MAY_INSERT);
c906108c
SS
14050}
14051\f
c906108c
SS
14052/* Reset the thread number of this breakpoint:
14053
14054 - If the breakpoint is for all threads, leave it as-is.
4a64f543 14055 - Else, reset it to the current thread for inferior_ptid. */
c906108c 14056void
fba45db2 14057breakpoint_re_set_thread (struct breakpoint *b)
c906108c
SS
14058{
14059 if (b->thread != -1)
14060 {
00431a78 14061 b->thread = inferior_thread ()->global_num;
6c95b8df
PA
14062
14063 /* We're being called after following a fork. The new fork is
14064 selected as current, and unless this was a vfork will have a
14065 different program space from the original thread. Reset that
14066 as well. */
14067 b->loc->pspace = current_program_space;
c906108c
SS
14068 }
14069}
14070
03ac34d5
MS
14071/* Set ignore-count of breakpoint number BPTNUM to COUNT.
14072 If from_tty is nonzero, it prints a message to that effect,
14073 which ends with a period (no newline). */
14074
c906108c 14075void
fba45db2 14076set_ignore_count (int bptnum, int count, int from_tty)
c906108c 14077{
52f0bd74 14078 struct breakpoint *b;
c906108c
SS
14079
14080 if (count < 0)
14081 count = 0;
14082
14083 ALL_BREAKPOINTS (b)
14084 if (b->number == bptnum)
c5aa993b 14085 {
d77f58be
SS
14086 if (is_tracepoint (b))
14087 {
14088 if (from_tty && count != 0)
14089 printf_filtered (_("Ignore count ignored for tracepoint %d."),
14090 bptnum);
14091 return;
14092 }
14093
c5aa993b 14094 b->ignore_count = count;
221ea385
KS
14095 if (from_tty)
14096 {
14097 if (count == 0)
3e43a32a
MS
14098 printf_filtered (_("Will stop next time "
14099 "breakpoint %d is reached."),
221ea385
KS
14100 bptnum);
14101 else if (count == 1)
a3f17187 14102 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
221ea385
KS
14103 bptnum);
14104 else
3e43a32a
MS
14105 printf_filtered (_("Will ignore next %d "
14106 "crossings of breakpoint %d."),
221ea385
KS
14107 count, bptnum);
14108 }
76727919 14109 gdb::observers::breakpoint_modified.notify (b);
c5aa993b
JM
14110 return;
14111 }
c906108c 14112
8a3fe4f8 14113 error (_("No breakpoint number %d."), bptnum);
c906108c
SS
14114}
14115
c906108c
SS
14116/* Command to set ignore-count of breakpoint N to COUNT. */
14117
14118static void
0b39b52e 14119ignore_command (const char *args, int from_tty)
c906108c 14120{
0b39b52e 14121 const char *p = args;
52f0bd74 14122 int num;
c906108c
SS
14123
14124 if (p == 0)
e2e0b3e5 14125 error_no_arg (_("a breakpoint number"));
c5aa993b 14126
c906108c 14127 num = get_number (&p);
5c44784c 14128 if (num == 0)
8a3fe4f8 14129 error (_("bad breakpoint number: '%s'"), args);
c906108c 14130 if (*p == 0)
8a3fe4f8 14131 error (_("Second argument (specified ignore-count) is missing."));
c906108c
SS
14132
14133 set_ignore_count (num,
14134 longest_to_int (value_as_long (parse_and_eval (p))),
14135 from_tty);
221ea385
KS
14136 if (from_tty)
14137 printf_filtered ("\n");
c906108c
SS
14138}
14139\f
d0fe4701
XR
14140
14141/* Call FUNCTION on each of the breakpoints with numbers in the range
14142 defined by BP_NUM_RANGE (an inclusive range). */
c906108c
SS
14143
14144static void
d0fe4701
XR
14145map_breakpoint_number_range (std::pair<int, int> bp_num_range,
14146 gdb::function_view<void (breakpoint *)> function)
c906108c 14147{
d0fe4701
XR
14148 if (bp_num_range.first == 0)
14149 {
14150 warning (_("bad breakpoint number at or near '%d'"),
14151 bp_num_range.first);
14152 }
14153 else
c906108c 14154 {
d0fe4701 14155 struct breakpoint *b, *tmp;
197f0a60 14156
d0fe4701 14157 for (int i = bp_num_range.first; i <= bp_num_range.second; i++)
5c44784c 14158 {
d0fe4701
XR
14159 bool match = false;
14160
5c44784c 14161 ALL_BREAKPOINTS_SAFE (b, tmp)
d0fe4701 14162 if (b->number == i)
5c44784c 14163 {
bfd28288 14164 match = true;
48649e1b 14165 function (b);
11cf8741 14166 break;
5c44784c 14167 }
bfd28288 14168 if (!match)
d0fe4701 14169 printf_unfiltered (_("No breakpoint number %d.\n"), i);
c5aa993b 14170 }
c906108c
SS
14171 }
14172}
14173
d0fe4701
XR
14174/* Call FUNCTION on each of the breakpoints whose numbers are given in
14175 ARGS. */
14176
14177static void
14178map_breakpoint_numbers (const char *args,
14179 gdb::function_view<void (breakpoint *)> function)
14180{
14181 if (args == NULL || *args == '\0')
14182 error_no_arg (_("one or more breakpoint numbers"));
14183
14184 number_or_range_parser parser (args);
14185
14186 while (!parser.finished ())
14187 {
14188 int num = parser.get_number ();
14189 map_breakpoint_number_range (std::make_pair (num, num), function);
14190 }
14191}
14192
14193/* Return the breakpoint location structure corresponding to the
14194 BP_NUM and LOC_NUM values. */
14195
0d381245 14196static struct bp_location *
d0fe4701 14197find_location_by_number (int bp_num, int loc_num)
0d381245 14198{
0d381245 14199 struct breakpoint *b;
0d381245
VP
14200
14201 ALL_BREAKPOINTS (b)
14202 if (b->number == bp_num)
14203 {
14204 break;
14205 }
14206
14207 if (!b || b->number != bp_num)
d0fe4701 14208 error (_("Bad breakpoint number '%d'"), bp_num);
0d381245 14209
0d381245 14210 if (loc_num == 0)
d0fe4701 14211 error (_("Bad breakpoint location number '%d'"), loc_num);
0d381245 14212
d0fe4701
XR
14213 int n = 0;
14214 for (bp_location *loc = b->loc; loc != NULL; loc = loc->next)
14215 if (++n == loc_num)
14216 return loc;
14217
14218 error (_("Bad breakpoint location number '%d'"), loc_num);
0d381245
VP
14219}
14220
95e95a6d
PA
14221/* Modes of operation for extract_bp_num. */
14222enum class extract_bp_kind
14223{
14224 /* Extracting a breakpoint number. */
14225 bp,
14226
14227 /* Extracting a location number. */
14228 loc,
14229};
14230
14231/* Extract a breakpoint or location number (as determined by KIND)
14232 from the string starting at START. TRAILER is a character which
14233 can be found after the number. If you don't want a trailer, use
14234 '\0'. If END_OUT is not NULL, it is set to point after the parsed
14235 string. This always returns a positive integer. */
14236
14237static int
14238extract_bp_num (extract_bp_kind kind, const char *start,
14239 int trailer, const char **end_out = NULL)
14240{
14241 const char *end = start;
14242 int num = get_number_trailer (&end, trailer);
14243 if (num < 0)
14244 error (kind == extract_bp_kind::bp
14245 ? _("Negative breakpoint number '%.*s'")
14246 : _("Negative breakpoint location number '%.*s'"),
14247 int (end - start), start);
14248 if (num == 0)
14249 error (kind == extract_bp_kind::bp
14250 ? _("Bad breakpoint number '%.*s'")
14251 : _("Bad breakpoint location number '%.*s'"),
14252 int (end - start), start);
14253
14254 if (end_out != NULL)
14255 *end_out = end;
14256 return num;
14257}
14258
14259/* Extract a breakpoint or location range (as determined by KIND) in
14260 the form NUM1-NUM2 stored at &ARG[arg_offset]. Returns a std::pair
14261 representing the (inclusive) range. The returned pair's elements
14262 are always positive integers. */
14263
14264static std::pair<int, int>
14265extract_bp_or_bp_range (extract_bp_kind kind,
14266 const std::string &arg,
14267 std::string::size_type arg_offset)
14268{
14269 std::pair<int, int> range;
14270 const char *bp_loc = &arg[arg_offset];
14271 std::string::size_type dash = arg.find ('-', arg_offset);
14272 if (dash != std::string::npos)
14273 {
14274 /* bp_loc is a range (x-z). */
14275 if (arg.length () == dash + 1)
14276 error (kind == extract_bp_kind::bp
14277 ? _("Bad breakpoint number at or near: '%s'")
14278 : _("Bad breakpoint location number at or near: '%s'"),
14279 bp_loc);
14280
14281 const char *end;
14282 const char *start_first = bp_loc;
14283 const char *start_second = &arg[dash + 1];
14284 range.first = extract_bp_num (kind, start_first, '-');
14285 range.second = extract_bp_num (kind, start_second, '\0', &end);
14286
14287 if (range.first > range.second)
14288 error (kind == extract_bp_kind::bp
14289 ? _("Inverted breakpoint range at '%.*s'")
14290 : _("Inverted breakpoint location range at '%.*s'"),
14291 int (end - start_first), start_first);
14292 }
14293 else
14294 {
14295 /* bp_loc is a single value. */
14296 range.first = extract_bp_num (kind, bp_loc, '\0');
14297 range.second = range.first;
14298 }
14299 return range;
14300}
14301
d0fe4701
XR
14302/* Extract the breakpoint/location range specified by ARG. Returns
14303 the breakpoint range in BP_NUM_RANGE, and the location range in
14304 BP_LOC_RANGE.
14305
14306 ARG may be in any of the following forms:
14307
14308 x where 'x' is a breakpoint number.
14309 x-y where 'x' and 'y' specify a breakpoint numbers range.
14310 x.y where 'x' is a breakpoint number and 'y' a location number.
14311 x.y-z where 'x' is a breakpoint number and 'y' and 'z' specify a
14312 location number range.
14313*/
14314
cc638e86 14315static void
d0fe4701
XR
14316extract_bp_number_and_location (const std::string &arg,
14317 std::pair<int, int> &bp_num_range,
14318 std::pair<int, int> &bp_loc_range)
14319{
14320 std::string::size_type dot = arg.find ('.');
14321
14322 if (dot != std::string::npos)
14323 {
14324 /* Handle 'x.y' and 'x.y-z' cases. */
14325
14326 if (arg.length () == dot + 1 || dot == 0)
95e95a6d 14327 error (_("Bad breakpoint number at or near: '%s'"), arg.c_str ());
d0fe4701 14328
95e95a6d
PA
14329 bp_num_range.first
14330 = extract_bp_num (extract_bp_kind::bp, arg.c_str (), '.');
14331 bp_num_range.second = bp_num_range.first;
d0fe4701 14332
95e95a6d
PA
14333 bp_loc_range = extract_bp_or_bp_range (extract_bp_kind::loc,
14334 arg, dot + 1);
d0fe4701
XR
14335 }
14336 else
14337 {
14338 /* Handle x and x-y cases. */
d0fe4701 14339
95e95a6d 14340 bp_num_range = extract_bp_or_bp_range (extract_bp_kind::bp, arg, 0);
d0fe4701
XR
14341 bp_loc_range.first = 0;
14342 bp_loc_range.second = 0;
14343 }
d0fe4701
XR
14344}
14345
14346/* Enable or disable a breakpoint location BP_NUM.LOC_NUM. ENABLE
14347 specifies whether to enable or disable. */
14348
14349static void
14350enable_disable_bp_num_loc (int bp_num, int loc_num, bool enable)
14351{
14352 struct bp_location *loc = find_location_by_number (bp_num, loc_num);
14353 if (loc != NULL)
14354 {
b5fa468f
TBA
14355 if (loc->disabled_by_cond && enable)
14356 error (_("Breakpoint %d's condition is invalid at location %d, "
14357 "cannot enable."), bp_num, loc_num);
14358
d0fe4701
XR
14359 if (loc->enabled != enable)
14360 {
14361 loc->enabled = enable;
14362 mark_breakpoint_location_modified (loc);
14363 }
14364 if (target_supports_enable_disable_tracepoint ()
14365 && current_trace_status ()->running && loc->owner
14366 && is_tracepoint (loc->owner))
14367 target_disable_tracepoint (loc);
14368 }
14369 update_global_location_list (UGLL_DONT_INSERT);
d7154a8d
JV
14370
14371 gdb::observers::breakpoint_modified.notify (loc->owner);
d0fe4701
XR
14372}
14373
14374/* Enable or disable a range of breakpoint locations. BP_NUM is the
14375 number of the breakpoint, and BP_LOC_RANGE specifies the
14376 (inclusive) range of location numbers of that breakpoint to
14377 enable/disable. ENABLE specifies whether to enable or disable the
14378 location. */
14379
14380static void
14381enable_disable_breakpoint_location_range (int bp_num,
14382 std::pair<int, int> &bp_loc_range,
14383 bool enable)
14384{
14385 for (int i = bp_loc_range.first; i <= bp_loc_range.second; i++)
14386 enable_disable_bp_num_loc (bp_num, i, enable);
14387}
0d381245 14388
1900040c
MS
14389/* Set ignore-count of breakpoint number BPTNUM to COUNT.
14390 If from_tty is nonzero, it prints a message to that effect,
14391 which ends with a period (no newline). */
14392
c906108c 14393void
fba45db2 14394disable_breakpoint (struct breakpoint *bpt)
c906108c
SS
14395{
14396 /* Never disable a watchpoint scope breakpoint; we want to
14397 hit them when we leave scope so we can delete both the
14398 watchpoint and its scope breakpoint at that time. */
14399 if (bpt->type == bp_watchpoint_scope)
14400 return;
14401
b5de0fa7 14402 bpt->enable_state = bp_disabled;
c906108c 14403
b775012e
LM
14404 /* Mark breakpoint locations modified. */
14405 mark_breakpoint_modified (bpt);
14406
d248b706
KY
14407 if (target_supports_enable_disable_tracepoint ()
14408 && current_trace_status ()->running && is_tracepoint (bpt))
14409 {
14410 struct bp_location *location;
14411
14412 for (location = bpt->loc; location; location = location->next)
14413 target_disable_tracepoint (location);
14414 }
14415
44702360 14416 update_global_location_list (UGLL_DONT_INSERT);
c906108c 14417
76727919 14418 gdb::observers::breakpoint_modified.notify (bpt);
c906108c
SS
14419}
14420
d0fe4701
XR
14421/* Enable or disable the breakpoint(s) or breakpoint location(s)
14422 specified in ARGS. ARGS may be in any of the formats handled by
14423 extract_bp_number_and_location. ENABLE specifies whether to enable
14424 or disable the breakpoints/locations. */
14425
c906108c 14426static void
d0fe4701 14427enable_disable_command (const char *args, int from_tty, bool enable)
c906108c 14428{
c906108c 14429 if (args == 0)
46c6471b
PA
14430 {
14431 struct breakpoint *bpt;
14432
14433 ALL_BREAKPOINTS (bpt)
14434 if (user_breakpoint_p (bpt))
d0fe4701
XR
14435 {
14436 if (enable)
14437 enable_breakpoint (bpt);
14438 else
14439 disable_breakpoint (bpt);
14440 }
46c6471b 14441 }
9eaabc75 14442 else
0d381245 14443 {
cb791d59 14444 std::string num = extract_arg (&args);
9eaabc75 14445
cb791d59 14446 while (!num.empty ())
d248b706 14447 {
d0fe4701 14448 std::pair<int, int> bp_num_range, bp_loc_range;
9eaabc75 14449
cc638e86
PA
14450 extract_bp_number_and_location (num, bp_num_range, bp_loc_range);
14451
14452 if (bp_loc_range.first == bp_loc_range.second
14453 && bp_loc_range.first == 0)
d0fe4701 14454 {
cc638e86
PA
14455 /* Handle breakpoint ids with formats 'x' or 'x-z'. */
14456 map_breakpoint_number_range (bp_num_range,
14457 enable
14458 ? enable_breakpoint
14459 : disable_breakpoint);
14460 }
14461 else
14462 {
14463 /* Handle breakpoint ids with formats 'x.y' or
14464 'x.y-z'. */
14465 enable_disable_breakpoint_location_range
14466 (bp_num_range.first, bp_loc_range, enable);
b775012e 14467 }
9eaabc75 14468 num = extract_arg (&args);
d248b706 14469 }
0d381245 14470 }
c906108c
SS
14471}
14472
d0fe4701
XR
14473/* The disable command disables the specified breakpoints/locations
14474 (or all defined breakpoints) so they're no longer effective in
14475 stopping the inferior. ARGS may be in any of the forms defined in
14476 extract_bp_number_and_location. */
14477
14478static void
14479disable_command (const char *args, int from_tty)
14480{
14481 enable_disable_command (args, from_tty, false);
14482}
14483
c906108c 14484static void
816338b5
SS
14485enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
14486 int count)
c906108c 14487{
afe38095 14488 int target_resources_ok;
c906108c
SS
14489
14490 if (bpt->type == bp_hardware_breakpoint)
14491 {
14492 int i;
c5aa993b 14493 i = hw_breakpoint_used_count ();
53a5351d 14494 target_resources_ok =
d92524f1 14495 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
53a5351d 14496 i + 1, 0);
c906108c 14497 if (target_resources_ok == 0)
8a3fe4f8 14498 error (_("No hardware breakpoint support in the target."));
c906108c 14499 else if (target_resources_ok < 0)
8a3fe4f8 14500 error (_("Hardware breakpoints used exceeds limit."));
c906108c
SS
14501 }
14502
cc60f2e3 14503 if (is_watchpoint (bpt))
c906108c 14504 {
d07205c2 14505 /* Initialize it just to avoid a GCC false warning. */
f486487f 14506 enum enable_state orig_enable_state = bp_disabled;
dde02812 14507
a70b8144 14508 try
c906108c 14509 {
3a5c3e22
PA
14510 struct watchpoint *w = (struct watchpoint *) bpt;
14511
1e718ff1
TJB
14512 orig_enable_state = bpt->enable_state;
14513 bpt->enable_state = bp_enabled;
3a5c3e22 14514 update_watchpoint (w, 1 /* reparse */);
c906108c 14515 }
230d2906 14516 catch (const gdb_exception &e)
c5aa993b 14517 {
1e718ff1 14518 bpt->enable_state = orig_enable_state;
dde02812
ES
14519 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
14520 bpt->number);
14521 return;
c5aa993b 14522 }
c906108c 14523 }
0101ce28 14524
b775012e
LM
14525 bpt->enable_state = bp_enabled;
14526
14527 /* Mark breakpoint locations modified. */
14528 mark_breakpoint_modified (bpt);
14529
d248b706
KY
14530 if (target_supports_enable_disable_tracepoint ()
14531 && current_trace_status ()->running && is_tracepoint (bpt))
14532 {
14533 struct bp_location *location;
14534
14535 for (location = bpt->loc; location; location = location->next)
14536 target_enable_tracepoint (location);
14537 }
14538
b4c291bb 14539 bpt->disposition = disposition;
816338b5 14540 bpt->enable_count = count;
44702360 14541 update_global_location_list (UGLL_MAY_INSERT);
9c97429f 14542
76727919 14543 gdb::observers::breakpoint_modified.notify (bpt);
c906108c
SS
14544}
14545
fe3f5fa8 14546
c906108c 14547void
fba45db2 14548enable_breakpoint (struct breakpoint *bpt)
c906108c 14549{
816338b5 14550 enable_breakpoint_disp (bpt, bpt->disposition, 0);
51be5b68
PA
14551}
14552
d0fe4701
XR
14553/* The enable command enables the specified breakpoints/locations (or
14554 all defined breakpoints) so they once again become (or continue to
14555 be) effective in stopping the inferior. ARGS may be in any of the
14556 forms defined in extract_bp_number_and_location. */
c906108c 14557
c906108c 14558static void
981a3fb3 14559enable_command (const char *args, int from_tty)
c906108c 14560{
d0fe4701 14561 enable_disable_command (args, from_tty, true);
c906108c
SS
14562}
14563
c906108c 14564static void
4495129a 14565enable_once_command (const char *args, int from_tty)
c906108c 14566{
48649e1b
TT
14567 map_breakpoint_numbers
14568 (args, [&] (breakpoint *b)
14569 {
14570 iterate_over_related_breakpoints
14571 (b, [&] (breakpoint *bpt)
14572 {
14573 enable_breakpoint_disp (bpt, disp_disable, 1);
14574 });
14575 });
816338b5
SS
14576}
14577
14578static void
4495129a 14579enable_count_command (const char *args, int from_tty)
816338b5 14580{
b9d61307
SM
14581 int count;
14582
14583 if (args == NULL)
14584 error_no_arg (_("hit count"));
14585
14586 count = get_number (&args);
816338b5 14587
48649e1b
TT
14588 map_breakpoint_numbers
14589 (args, [&] (breakpoint *b)
14590 {
14591 iterate_over_related_breakpoints
14592 (b, [&] (breakpoint *bpt)
14593 {
14594 enable_breakpoint_disp (bpt, disp_disable, count);
14595 });
14596 });
c906108c
SS
14597}
14598
c906108c 14599static void
4495129a 14600enable_delete_command (const char *args, int from_tty)
c906108c 14601{
48649e1b
TT
14602 map_breakpoint_numbers
14603 (args, [&] (breakpoint *b)
14604 {
14605 iterate_over_related_breakpoints
14606 (b, [&] (breakpoint *bpt)
14607 {
14608 enable_breakpoint_disp (bpt, disp_del, 1);
14609 });
14610 });
c906108c
SS
14611}
14612\f
1f3b5d1b
PP
14613/* Invalidate last known value of any hardware watchpoint if
14614 the memory which that value represents has been written to by
14615 GDB itself. */
14616
14617static void
8de0566d
YQ
14618invalidate_bp_value_on_memory_change (struct inferior *inferior,
14619 CORE_ADDR addr, ssize_t len,
1f3b5d1b
PP
14620 const bfd_byte *data)
14621{
14622 struct breakpoint *bp;
14623
14624 ALL_BREAKPOINTS (bp)
14625 if (bp->enable_state == bp_enabled
3a5c3e22 14626 && bp->type == bp_hardware_watchpoint)
1f3b5d1b 14627 {
3a5c3e22 14628 struct watchpoint *wp = (struct watchpoint *) bp;
1f3b5d1b 14629
850645cf 14630 if (wp->val_valid && wp->val != nullptr)
3a5c3e22
PA
14631 {
14632 struct bp_location *loc;
14633
14634 for (loc = bp->loc; loc != NULL; loc = loc->next)
14635 if (loc->loc_type == bp_loc_hardware_watchpoint
14636 && loc->address + loc->length > addr
14637 && addr + len > loc->address)
14638 {
3a5c3e22 14639 wp->val = NULL;
4c1d86d9 14640 wp->val_valid = false;
3a5c3e22
PA
14641 }
14642 }
1f3b5d1b
PP
14643 }
14644}
14645
8181d85f
DJ
14646/* Create and insert a breakpoint for software single step. */
14647
14648void
6c95b8df 14649insert_single_step_breakpoint (struct gdbarch *gdbarch,
accd0bcd 14650 const address_space *aspace,
4a64f543 14651 CORE_ADDR next_pc)
8181d85f 14652{
7c16b83e
PA
14653 struct thread_info *tp = inferior_thread ();
14654 struct symtab_and_line sal;
14655 CORE_ADDR pc = next_pc;
8181d85f 14656
34b7e8a6
PA
14657 if (tp->control.single_step_breakpoints == NULL)
14658 {
14659 tp->control.single_step_breakpoints
5d5658a1 14660 = new_single_step_breakpoint (tp->global_num, gdbarch);
34b7e8a6 14661 }
8181d85f 14662
7c16b83e
PA
14663 sal = find_pc_line (pc, 0);
14664 sal.pc = pc;
14665 sal.section = find_pc_overlay (pc);
14666 sal.explicit_pc = 1;
34b7e8a6 14667 add_location_to_breakpoint (tp->control.single_step_breakpoints, &sal);
8181d85f 14668
7c16b83e 14669 update_global_location_list (UGLL_INSERT);
8181d85f
DJ
14670}
14671
93f9a11f
YQ
14672/* Insert single step breakpoints according to the current state. */
14673
14674int
14675insert_single_step_breakpoints (struct gdbarch *gdbarch)
14676{
f5ea389a 14677 struct regcache *regcache = get_current_regcache ();
a0ff9e1a 14678 std::vector<CORE_ADDR> next_pcs;
93f9a11f 14679
f5ea389a 14680 next_pcs = gdbarch_software_single_step (gdbarch, regcache);
93f9a11f 14681
a0ff9e1a 14682 if (!next_pcs.empty ())
93f9a11f 14683 {
f5ea389a 14684 struct frame_info *frame = get_current_frame ();
8b86c959 14685 const address_space *aspace = get_frame_address_space (frame);
93f9a11f 14686
a0ff9e1a 14687 for (CORE_ADDR pc : next_pcs)
93f9a11f
YQ
14688 insert_single_step_breakpoint (gdbarch, aspace, pc);
14689
93f9a11f
YQ
14690 return 1;
14691 }
14692 else
14693 return 0;
14694}
14695
34b7e8a6 14696/* See breakpoint.h. */
f02253f1
HZ
14697
14698int
7c16b83e 14699breakpoint_has_location_inserted_here (struct breakpoint *bp,
accd0bcd 14700 const address_space *aspace,
7c16b83e 14701 CORE_ADDR pc)
1aafd4da 14702{
7c16b83e 14703 struct bp_location *loc;
1aafd4da 14704
7c16b83e
PA
14705 for (loc = bp->loc; loc != NULL; loc = loc->next)
14706 if (loc->inserted
14707 && breakpoint_location_address_match (loc, aspace, pc))
14708 return 1;
1aafd4da 14709
7c16b83e 14710 return 0;
ef370185
JB
14711}
14712
14713/* Check whether a software single-step breakpoint is inserted at
14714 PC. */
14715
14716int
accd0bcd 14717single_step_breakpoint_inserted_here_p (const address_space *aspace,
ef370185
JB
14718 CORE_ADDR pc)
14719{
34b7e8a6
PA
14720 struct breakpoint *bpt;
14721
14722 ALL_BREAKPOINTS (bpt)
14723 {
14724 if (bpt->type == bp_single_step
14725 && breakpoint_has_location_inserted_here (bpt, aspace, pc))
14726 return 1;
14727 }
14728 return 0;
1aafd4da
UW
14729}
14730
1042e4c0
SS
14731/* Tracepoint-specific operations. */
14732
14733/* Set tracepoint count to NUM. */
14734static void
14735set_tracepoint_count (int num)
14736{
14737 tracepoint_count = num;
4fa62494 14738 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
1042e4c0
SS
14739}
14740
70221824 14741static void
0b39b52e 14742trace_command (const char *arg, int from_tty)
1042e4c0 14743{
ffc2605c
TT
14744 event_location_up location = string_to_event_location (&arg,
14745 current_language);
bac7c5cf
GB
14746 const struct breakpoint_ops *ops = breakpoint_ops_for_event_location
14747 (location.get (), true /* is_tracepoint */);
55aa24fb 14748
558a9d82 14749 create_breakpoint (get_current_arch (),
ffc2605c 14750 location.get (),
10a636cc 14751 NULL, 0, arg, false, 1 /* parse arg */,
558a9d82
YQ
14752 0 /* tempflag */,
14753 bp_tracepoint /* type_wanted */,
14754 0 /* Ignore count */,
14755 pending_break_support,
14756 ops,
14757 from_tty,
14758 1 /* enabled */,
14759 0 /* internal */, 0);
1042e4c0
SS
14760}
14761
70221824 14762static void
0b39b52e 14763ftrace_command (const char *arg, int from_tty)
7a697b8d 14764{
ffc2605c
TT
14765 event_location_up location = string_to_event_location (&arg,
14766 current_language);
558a9d82 14767 create_breakpoint (get_current_arch (),
ffc2605c 14768 location.get (),
10a636cc 14769 NULL, 0, arg, false, 1 /* parse arg */,
558a9d82
YQ
14770 0 /* tempflag */,
14771 bp_fast_tracepoint /* type_wanted */,
14772 0 /* Ignore count */,
14773 pending_break_support,
14774 &tracepoint_breakpoint_ops,
14775 from_tty,
14776 1 /* enabled */,
14777 0 /* internal */, 0);
0fb4aa4b
PA
14778}
14779
14780/* strace command implementation. Creates a static tracepoint. */
14781
70221824 14782static void
0b39b52e 14783strace_command (const char *arg, int from_tty)
0fb4aa4b 14784{
983af33b 14785 struct breakpoint_ops *ops;
ffc2605c 14786 event_location_up location;
983af33b
SDJ
14787
14788 /* Decide if we are dealing with a static tracepoint marker (`-m'),
14789 or with a normal static tracepoint. */
61012eef 14790 if (arg && startswith (arg, "-m") && isspace (arg[2]))
f00aae0f
KS
14791 {
14792 ops = &strace_marker_breakpoint_ops;
a20714ff 14793 location = new_linespec_location (&arg, symbol_name_match_type::FULL);
f00aae0f 14794 }
983af33b 14795 else
f00aae0f
KS
14796 {
14797 ops = &tracepoint_breakpoint_ops;
14798 location = string_to_event_location (&arg, current_language);
14799 }
983af33b 14800
558a9d82 14801 create_breakpoint (get_current_arch (),
ffc2605c 14802 location.get (),
10a636cc 14803 NULL, 0, arg, false, 1 /* parse arg */,
558a9d82
YQ
14804 0 /* tempflag */,
14805 bp_static_tracepoint /* type_wanted */,
14806 0 /* Ignore count */,
14807 pending_break_support,
14808 ops,
14809 from_tty,
14810 1 /* enabled */,
14811 0 /* internal */, 0);
7a697b8d
SS
14812}
14813
409873ef
SS
14814/* Set up a fake reader function that gets command lines from a linked
14815 list that was acquired during tracepoint uploading. */
14816
14817static struct uploaded_tp *this_utp;
3149d8c1 14818static int next_cmd;
409873ef
SS
14819
14820static char *
14821read_uploaded_action (void)
14822{
a18ba4e4 14823 char *rslt = nullptr;
409873ef 14824
a18ba4e4
SM
14825 if (next_cmd < this_utp->cmd_strings.size ())
14826 {
67aa1f3c 14827 rslt = this_utp->cmd_strings[next_cmd].get ();
a18ba4e4
SM
14828 next_cmd++;
14829 }
409873ef
SS
14830
14831 return rslt;
14832}
14833
00bf0b85
SS
14834/* Given information about a tracepoint as recorded on a target (which
14835 can be either a live system or a trace file), attempt to create an
14836 equivalent GDB tracepoint. This is not a reliable process, since
14837 the target does not necessarily have all the information used when
14838 the tracepoint was originally defined. */
14839
d9b3f62e 14840struct tracepoint *
00bf0b85 14841create_tracepoint_from_upload (struct uploaded_tp *utp)
d5551862 14842{
f2fc3015
TT
14843 const char *addr_str;
14844 char small_buf[100];
d9b3f62e 14845 struct tracepoint *tp;
fd9b8c24 14846
409873ef 14847 if (utp->at_string)
67aa1f3c 14848 addr_str = utp->at_string.get ();
409873ef
SS
14849 else
14850 {
14851 /* In the absence of a source location, fall back to raw
14852 address. Since there is no way to confirm that the address
14853 means the same thing as when the trace was started, warn the
14854 user. */
3e43a32a
MS
14855 warning (_("Uploaded tracepoint %d has no "
14856 "source location, using raw address"),
409873ef 14857 utp->number);
8c042590 14858 xsnprintf (small_buf, sizeof (small_buf), "*%s", hex_string (utp->addr));
409873ef
SS
14859 addr_str = small_buf;
14860 }
14861
14862 /* There's not much we can do with a sequence of bytecodes. */
14863 if (utp->cond && !utp->cond_string)
3e43a32a
MS
14864 warning (_("Uploaded tracepoint %d condition "
14865 "has no source form, ignoring it"),
409873ef 14866 utp->number);
d5551862 14867
ffc2605c
TT
14868 event_location_up location = string_to_event_location (&addr_str,
14869 current_language);
8cdf0e15 14870 if (!create_breakpoint (get_current_arch (),
ffc2605c 14871 location.get (),
67aa1f3c 14872 utp->cond_string.get (), -1, addr_str,
10a636cc 14873 false /* force_condition */,
e7e0cddf 14874 0 /* parse cond/thread */,
8cdf0e15 14875 0 /* tempflag */,
0fb4aa4b 14876 utp->type /* type_wanted */,
8cdf0e15
VP
14877 0 /* Ignore count */,
14878 pending_break_support,
348d480f 14879 &tracepoint_breakpoint_ops,
8cdf0e15 14880 0 /* from_tty */,
84f4c1fe 14881 utp->enabled /* enabled */,
44f238bb
PA
14882 0 /* internal */,
14883 CREATE_BREAKPOINT_FLAGS_INSERTED))
ffc2605c 14884 return NULL;
fd9b8c24 14885
409873ef 14886 /* Get the tracepoint we just created. */
fd9b8c24
PA
14887 tp = get_tracepoint (tracepoint_count);
14888 gdb_assert (tp != NULL);
d5551862 14889
00bf0b85
SS
14890 if (utp->pass > 0)
14891 {
8c042590 14892 xsnprintf (small_buf, sizeof (small_buf), "%d %d", utp->pass,
c1fc2657 14893 tp->number);
00bf0b85 14894
409873ef 14895 trace_pass_command (small_buf, 0);
00bf0b85
SS
14896 }
14897
409873ef
SS
14898 /* If we have uploaded versions of the original commands, set up a
14899 special-purpose "reader" function and call the usual command line
14900 reader, then pass the result to the breakpoint command-setting
14901 function. */
a18ba4e4 14902 if (!utp->cmd_strings.empty ())
00bf0b85 14903 {
12973681 14904 counted_command_line cmd_list;
00bf0b85 14905
409873ef 14906 this_utp = utp;
3149d8c1 14907 next_cmd = 0;
d5551862 14908
60b3cef2 14909 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL);
409873ef 14910
c1fc2657 14911 breakpoint_set_commands (tp, std::move (cmd_list));
00bf0b85 14912 }
a18ba4e4
SM
14913 else if (!utp->actions.empty ()
14914 || !utp->step_actions.empty ())
3e43a32a
MS
14915 warning (_("Uploaded tracepoint %d actions "
14916 "have no source form, ignoring them"),
409873ef 14917 utp->number);
00bf0b85 14918
f196051f 14919 /* Copy any status information that might be available. */
c1fc2657 14920 tp->hit_count = utp->hit_count;
f196051f
SS
14921 tp->traceframe_usage = utp->traceframe_usage;
14922
00bf0b85 14923 return tp;
d9b3f62e 14924}
00bf0b85 14925
1042e4c0
SS
14926/* Print information on tracepoint number TPNUM_EXP, or all if
14927 omitted. */
14928
14929static void
1d12d88f 14930info_tracepoints_command (const char *args, int from_tty)
1042e4c0 14931{
79a45e25 14932 struct ui_out *uiout = current_uiout;
e5a67952 14933 int num_printed;
1042e4c0 14934
5c458ae8 14935 num_printed = breakpoint_1 (args, false, is_tracepoint);
d77f58be
SS
14936
14937 if (num_printed == 0)
1042e4c0 14938 {
e5a67952 14939 if (args == NULL || *args == '\0')
112e8700 14940 uiout->message ("No tracepoints.\n");
d77f58be 14941 else
112e8700 14942 uiout->message ("No tracepoint matching '%s'.\n", args);
1042e4c0 14943 }
ad443146
SS
14944
14945 default_collect_info ();
1042e4c0
SS
14946}
14947
4a64f543 14948/* The 'enable trace' command enables tracepoints.
1042e4c0
SS
14949 Not supported by all targets. */
14950static void
5fed81ff 14951enable_trace_command (const char *args, int from_tty)
1042e4c0
SS
14952{
14953 enable_command (args, from_tty);
14954}
14955
4a64f543 14956/* The 'disable trace' command disables tracepoints.
1042e4c0
SS
14957 Not supported by all targets. */
14958static void
5fed81ff 14959disable_trace_command (const char *args, int from_tty)
1042e4c0
SS
14960{
14961 disable_command (args, from_tty);
14962}
14963
4a64f543 14964/* Remove a tracepoint (or all if no argument). */
1042e4c0 14965static void
4495129a 14966delete_trace_command (const char *arg, int from_tty)
1042e4c0 14967{
35df4500 14968 struct breakpoint *b, *b_tmp;
1042e4c0
SS
14969
14970 dont_repeat ();
14971
14972 if (arg == 0)
14973 {
14974 int breaks_to_delete = 0;
14975
14976 /* Delete all breakpoints if no argument.
dda83cd7
SM
14977 Do not delete internal or call-dummy breakpoints, these
14978 have to be deleted with an explicit breakpoint number
4a64f543 14979 argument. */
1042e4c0 14980 ALL_TRACEPOINTS (b)
46c6471b 14981 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0
SS
14982 {
14983 breaks_to_delete = 1;
14984 break;
14985 }
1042e4c0
SS
14986
14987 /* Ask user only if there are some breakpoints to delete. */
14988 if (!from_tty
14989 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
14990 {
35df4500 14991 ALL_BREAKPOINTS_SAFE (b, b_tmp)
46c6471b 14992 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0 14993 delete_breakpoint (b);
1042e4c0
SS
14994 }
14995 }
14996 else
48649e1b 14997 map_breakpoint_numbers
b926417a 14998 (arg, [&] (breakpoint *br)
48649e1b 14999 {
b926417a 15000 iterate_over_related_breakpoints (br, delete_breakpoint);
48649e1b 15001 });
1042e4c0
SS
15002}
15003
197f0a60
TT
15004/* Helper function for trace_pass_command. */
15005
15006static void
d9b3f62e 15007trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
197f0a60 15008{
d9b3f62e 15009 tp->pass_count = count;
76727919 15010 gdb::observers::breakpoint_modified.notify (tp);
197f0a60
TT
15011 if (from_tty)
15012 printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
c1fc2657 15013 tp->number, count);
197f0a60
TT
15014}
15015
1042e4c0
SS
15016/* Set passcount for tracepoint.
15017
15018 First command argument is passcount, second is tracepoint number.
15019 If tracepoint number omitted, apply to most recently defined.
15020 Also accepts special argument "all". */
15021
15022static void
0b39b52e 15023trace_pass_command (const char *args, int from_tty)
1042e4c0 15024{
d9b3f62e 15025 struct tracepoint *t1;
0b39b52e 15026 ULONGEST count;
1042e4c0
SS
15027
15028 if (args == 0 || *args == 0)
3e43a32a
MS
15029 error (_("passcount command requires an "
15030 "argument (count + optional TP num)"));
1042e4c0 15031
0b39b52e 15032 count = strtoulst (args, &args, 10); /* Count comes first, then TP num. */
1042e4c0 15033
529480d0 15034 args = skip_spaces (args);
1042e4c0
SS
15035 if (*args && strncasecmp (args, "all", 3) == 0)
15036 {
d9b3f62e
PA
15037 struct breakpoint *b;
15038
1042e4c0 15039 args += 3; /* Skip special argument "all". */
1042e4c0
SS
15040 if (*args)
15041 error (_("Junk at end of arguments."));
1042e4c0 15042
d9b3f62e 15043 ALL_TRACEPOINTS (b)
197f0a60 15044 {
d9b3f62e 15045 t1 = (struct tracepoint *) b;
197f0a60
TT
15046 trace_pass_set_count (t1, count, from_tty);
15047 }
15048 }
15049 else if (*args == '\0')
1042e4c0 15050 {
5fa1d40e 15051 t1 = get_tracepoint_by_number (&args, NULL);
1042e4c0 15052 if (t1)
197f0a60
TT
15053 trace_pass_set_count (t1, count, from_tty);
15054 }
15055 else
15056 {
bfd28288
PA
15057 number_or_range_parser parser (args);
15058 while (!parser.finished ())
1042e4c0 15059 {
bfd28288 15060 t1 = get_tracepoint_by_number (&args, &parser);
197f0a60
TT
15061 if (t1)
15062 trace_pass_set_count (t1, count, from_tty);
1042e4c0
SS
15063 }
15064 }
1042e4c0
SS
15065}
15066
d9b3f62e 15067struct tracepoint *
1042e4c0
SS
15068get_tracepoint (int num)
15069{
15070 struct breakpoint *t;
15071
15072 ALL_TRACEPOINTS (t)
15073 if (t->number == num)
d9b3f62e 15074 return (struct tracepoint *) t;
1042e4c0
SS
15075
15076 return NULL;
15077}
15078
d5551862
SS
15079/* Find the tracepoint with the given target-side number (which may be
15080 different from the tracepoint number after disconnecting and
15081 reconnecting). */
15082
d9b3f62e 15083struct tracepoint *
d5551862
SS
15084get_tracepoint_by_number_on_target (int num)
15085{
d9b3f62e 15086 struct breakpoint *b;
d5551862 15087
d9b3f62e
PA
15088 ALL_TRACEPOINTS (b)
15089 {
15090 struct tracepoint *t = (struct tracepoint *) b;
15091
15092 if (t->number_on_target == num)
15093 return t;
15094 }
d5551862
SS
15095
15096 return NULL;
15097}
15098
1042e4c0 15099/* Utility: parse a tracepoint number and look it up in the list.
197f0a60 15100 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
5fa1d40e
YQ
15101 If the argument is missing, the most recent tracepoint
15102 (tracepoint_count) is returned. */
15103
d9b3f62e 15104struct tracepoint *
0b39b52e 15105get_tracepoint_by_number (const char **arg,
bfd28288 15106 number_or_range_parser *parser)
1042e4c0 15107{
1042e4c0
SS
15108 struct breakpoint *t;
15109 int tpnum;
0b39b52e 15110 const char *instring = arg == NULL ? NULL : *arg;
1042e4c0 15111
bfd28288 15112 if (parser != NULL)
197f0a60 15113 {
bfd28288
PA
15114 gdb_assert (!parser->finished ());
15115 tpnum = parser->get_number ();
197f0a60
TT
15116 }
15117 else if (arg == NULL || *arg == NULL || ! **arg)
5fa1d40e 15118 tpnum = tracepoint_count;
1042e4c0 15119 else
197f0a60 15120 tpnum = get_number (arg);
1042e4c0
SS
15121
15122 if (tpnum <= 0)
15123 {
15124 if (instring && *instring)
15125 printf_filtered (_("bad tracepoint number at or near '%s'\n"),
15126 instring);
15127 else
5fa1d40e 15128 printf_filtered (_("No previous tracepoint\n"));
1042e4c0
SS
15129 return NULL;
15130 }
15131
15132 ALL_TRACEPOINTS (t)
15133 if (t->number == tpnum)
15134 {
d9b3f62e 15135 return (struct tracepoint *) t;
1042e4c0
SS
15136 }
15137
1042e4c0
SS
15138 printf_unfiltered ("No tracepoint number %d.\n", tpnum);
15139 return NULL;
15140}
15141
d9b3f62e
PA
15142void
15143print_recreate_thread (struct breakpoint *b, struct ui_file *fp)
15144{
15145 if (b->thread != -1)
15146 fprintf_unfiltered (fp, " thread %d", b->thread);
15147
15148 if (b->task != 0)
15149 fprintf_unfiltered (fp, " task %d", b->task);
15150
15151 fprintf_unfiltered (fp, "\n");
15152}
15153
6149aea9
PA
15154/* Save information on user settable breakpoints (watchpoints, etc) to
15155 a new script file named FILENAME. If FILTER is non-NULL, call it
15156 on each breakpoint and only include the ones for which it returns
f2478a7e 15157 true. */
6149aea9 15158
1042e4c0 15159static void
4495129a 15160save_breakpoints (const char *filename, int from_tty,
f2478a7e 15161 bool (*filter) (const struct breakpoint *))
1042e4c0
SS
15162{
15163 struct breakpoint *tp;
6149aea9 15164 int any = 0;
6149aea9 15165 int extra_trace_bits = 0;
1042e4c0 15166
6149aea9
PA
15167 if (filename == 0 || *filename == 0)
15168 error (_("Argument required (file name in which to save)"));
1042e4c0
SS
15169
15170 /* See if we have anything to save. */
6149aea9 15171 ALL_BREAKPOINTS (tp)
1042e4c0 15172 {
6149aea9 15173 /* Skip internal and momentary breakpoints. */
09d682a4 15174 if (!user_breakpoint_p (tp))
6149aea9
PA
15175 continue;
15176
15177 /* If we have a filter, only save the breakpoints it accepts. */
15178 if (filter && !filter (tp))
15179 continue;
15180
15181 any = 1;
15182
15183 if (is_tracepoint (tp))
15184 {
15185 extra_trace_bits = 1;
15186
15187 /* We can stop searching. */
15188 break;
15189 }
1042e4c0 15190 }
6149aea9
PA
15191
15192 if (!any)
1042e4c0 15193 {
6149aea9 15194 warning (_("Nothing to save."));
1042e4c0
SS
15195 return;
15196 }
15197
ee0c3293 15198 gdb::unique_xmalloc_ptr<char> expanded_filename (tilde_expand (filename));
d7e74731
PA
15199
15200 stdio_file fp;
15201
ee0c3293 15202 if (!fp.open (expanded_filename.get (), "w"))
6149aea9 15203 error (_("Unable to open file '%s' for saving (%s)"),
ee0c3293 15204 expanded_filename.get (), safe_strerror (errno));
8bf6485c 15205
6149aea9 15206 if (extra_trace_bits)
d7e74731 15207 save_trace_state_variables (&fp);
8bf6485c 15208
6149aea9 15209 ALL_BREAKPOINTS (tp)
1042e4c0 15210 {
6149aea9 15211 /* Skip internal and momentary breakpoints. */
09d682a4 15212 if (!user_breakpoint_p (tp))
6149aea9 15213 continue;
8bf6485c 15214
6149aea9
PA
15215 /* If we have a filter, only save the breakpoints it accepts. */
15216 if (filter && !filter (tp))
15217 continue;
15218
d7e74731 15219 tp->ops->print_recreate (tp, &fp);
1042e4c0 15220
6149aea9
PA
15221 /* Note, we can't rely on tp->number for anything, as we can't
15222 assume the recreated breakpoint numbers will match. Use $bpnum
15223 instead. */
15224
15225 if (tp->cond_string)
d7e74731 15226 fp.printf (" condition $bpnum %s\n", tp->cond_string);
6149aea9
PA
15227
15228 if (tp->ignore_count)
d7e74731 15229 fp.printf (" ignore $bpnum %d\n", tp->ignore_count);
6149aea9 15230
2d9442cc 15231 if (tp->type != bp_dprintf && tp->commands)
1042e4c0 15232 {
d7e74731 15233 fp.puts (" commands\n");
a7bdde9e 15234
d7e74731 15235 current_uiout->redirect (&fp);
a70b8144 15236 try
1042e4c0 15237 {
d1b0a7bf 15238 print_command_lines (current_uiout, tp->commands.get (), 2);
a7bdde9e 15239 }
230d2906 15240 catch (const gdb_exception &ex)
492d29ea 15241 {
112e8700 15242 current_uiout->redirect (NULL);
eedc3f4f 15243 throw;
492d29ea 15244 }
1042e4c0 15245
112e8700 15246 current_uiout->redirect (NULL);
d7e74731 15247 fp.puts (" end\n");
1042e4c0 15248 }
6149aea9
PA
15249
15250 if (tp->enable_state == bp_disabled)
d7e74731 15251 fp.puts ("disable $bpnum\n");
6149aea9
PA
15252
15253 /* If this is a multi-location breakpoint, check if the locations
15254 should be individually disabled. Watchpoint locations are
15255 special, and not user visible. */
15256 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
15257 {
15258 struct bp_location *loc;
15259 int n = 1;
15260
15261 for (loc = tp->loc; loc != NULL; loc = loc->next, n++)
15262 if (!loc->enabled)
d7e74731 15263 fp.printf ("disable $bpnum.%d\n", n);
6149aea9 15264 }
1042e4c0 15265 }
8bf6485c 15266
6149aea9 15267 if (extra_trace_bits && *default_collect)
d7e74731 15268 fp.printf ("set default-collect %s\n", default_collect);
8bf6485c 15269
1042e4c0 15270 if (from_tty)
ee0c3293 15271 printf_filtered (_("Saved to file '%s'.\n"), expanded_filename.get ());
6149aea9
PA
15272}
15273
15274/* The `save breakpoints' command. */
15275
15276static void
4495129a 15277save_breakpoints_command (const char *args, int from_tty)
6149aea9
PA
15278{
15279 save_breakpoints (args, from_tty, NULL);
15280}
15281
15282/* The `save tracepoints' command. */
15283
15284static void
4495129a 15285save_tracepoints_command (const char *args, int from_tty)
6149aea9
PA
15286{
15287 save_breakpoints (args, from_tty, is_tracepoint);
1042e4c0
SS
15288}
15289
15290/* Create a vector of all tracepoints. */
15291
f51e0e20 15292std::vector<breakpoint *>
eeae04df 15293all_tracepoints (void)
1042e4c0 15294{
f51e0e20 15295 std::vector<breakpoint *> tp_vec;
1042e4c0
SS
15296 struct breakpoint *tp;
15297
15298 ALL_TRACEPOINTS (tp)
15299 {
f51e0e20 15300 tp_vec.push_back (tp);
1042e4c0
SS
15301 }
15302
15303 return tp_vec;
15304}
15305
c906108c 15306\f
629500fa
KS
15307/* This help string is used to consolidate all the help string for specifying
15308 locations used by several commands. */
15309
15310#define LOCATION_HELP_STRING \
15311"Linespecs are colon-separated lists of location parameters, such as\n\
15312source filename, function name, label name, and line number.\n\
15313Example: To specify the start of a label named \"the_top\" in the\n\
15314function \"fact\" in the file \"factorial.c\", use\n\
15315\"factorial.c:fact:the_top\".\n\
15316\n\
15317Address locations begin with \"*\" and specify an exact address in the\n\
15318program. Example: To specify the fourth byte past the start function\n\
15319\"main\", use \"*main + 4\".\n\
15320\n\
15321Explicit locations are similar to linespecs but use an option/argument\n\
15322syntax to specify location parameters.\n\
15323Example: To specify the start of the label named \"the_top\" in the\n\
15324function \"fact\" in the file \"factorial.c\", use \"-source factorial.c\n\
a20714ff
PA
15325-function fact -label the_top\".\n\
15326\n\
15327By default, a specified function is matched against the program's\n\
15328functions in all scopes. For C++, this means in all namespaces and\n\
15329classes. For Ada, this means in all packages. E.g., in C++,\n\
15330\"func()\" matches \"A::func()\", \"A::B::func()\", etc. The\n\
15331\"-qualified\" flag overrides this behavior, making GDB interpret the\n\
89549d7f 15332specified name as a complete fully-qualified name instead."
629500fa 15333
4a64f543
MS
15334/* This help string is used for the break, hbreak, tbreak and thbreak
15335 commands. It is defined as a macro to prevent duplication.
15336 COMMAND should be a string constant containing the name of the
15337 command. */
629500fa 15338
31e2b00f 15339#define BREAK_ARGS_HELP(command) \
733d554a
TBA
15340command" [PROBE_MODIFIER] [LOCATION] [thread THREADNUM]\n\
15341\t[-force-condition] [if CONDITION]\n\
fb7b5af4
SDJ
15342PROBE_MODIFIER shall be present if the command is to be placed in a\n\
15343probe point. Accepted values are `-probe' (for a generic, automatically\n\
d4777acb
JM
15344guessed probe type), `-probe-stap' (for a SystemTap probe) or \n\
15345`-probe-dtrace' (for a DTrace probe).\n\
629500fa
KS
15346LOCATION may be a linespec, address, or explicit location as described\n\
15347below.\n\
15348\n\
dc10affe
PA
15349With no LOCATION, uses current execution address of the selected\n\
15350stack frame. This is useful for breaking on return to a stack frame.\n\
31e2b00f
AS
15351\n\
15352THREADNUM is the number from \"info threads\".\n\
15353CONDITION is a boolean expression.\n\
733d554a
TBA
15354\n\
15355With the \"-force-condition\" flag, the condition is defined even when\n\
15356it is invalid for all current locations.\n\
89549d7f 15357\n" LOCATION_HELP_STRING "\n\n\
d41c0fc8
PA
15358Multiple breakpoints at one place are permitted, and useful if their\n\
15359conditions are different.\n\
31e2b00f
AS
15360\n\
15361Do \"help breakpoints\" for info on other commands dealing with breakpoints."
15362
44feb3ce
TT
15363/* List of subcommands for "catch". */
15364static struct cmd_list_element *catch_cmdlist;
15365
15366/* List of subcommands for "tcatch". */
15367static struct cmd_list_element *tcatch_cmdlist;
15368
9ac4176b 15369void
a121b7c1 15370add_catch_command (const char *name, const char *docstring,
eb4c3f4a 15371 cmd_const_sfunc_ftype *sfunc,
625e8578 15372 completer_ftype *completer,
44feb3ce
TT
15373 void *user_data_catch,
15374 void *user_data_tcatch)
15375{
15376 struct cmd_list_element *command;
15377
0450cc4c 15378 command = add_cmd (name, class_breakpoint, docstring,
44feb3ce
TT
15379 &catch_cmdlist);
15380 set_cmd_sfunc (command, sfunc);
15381 set_cmd_context (command, user_data_catch);
a96d9b2e 15382 set_cmd_completer (command, completer);
44feb3ce 15383
0450cc4c 15384 command = add_cmd (name, class_breakpoint, docstring,
44feb3ce
TT
15385 &tcatch_cmdlist);
15386 set_cmd_sfunc (command, sfunc);
15387 set_cmd_context (command, user_data_tcatch);
a96d9b2e 15388 set_cmd_completer (command, completer);
44feb3ce
TT
15389}
15390
84f4c1fe 15391struct breakpoint *
95da600f 15392iterate_over_breakpoints (gdb::function_view<bool (breakpoint *)> callback)
84f4c1fe 15393{
35df4500 15394 struct breakpoint *b, *b_tmp;
84f4c1fe 15395
35df4500 15396 ALL_BREAKPOINTS_SAFE (b, b_tmp)
84f4c1fe 15397 {
95da600f 15398 if (callback (b))
84f4c1fe
PM
15399 return b;
15400 }
15401
15402 return NULL;
15403}
15404
0574c78f
GB
15405/* Zero if any of the breakpoint's locations could be a location where
15406 functions have been inlined, nonzero otherwise. */
15407
15408static int
15409is_non_inline_function (struct breakpoint *b)
15410{
15411 /* The shared library event breakpoint is set on the address of a
15412 non-inline function. */
15413 if (b->type == bp_shlib_event)
15414 return 1;
15415
15416 return 0;
15417}
15418
15419/* Nonzero if the specified PC cannot be a location where functions
15420 have been inlined. */
15421
15422int
accd0bcd 15423pc_at_non_inline_function (const address_space *aspace, CORE_ADDR pc,
09ac7c10 15424 const struct target_waitstatus *ws)
0574c78f
GB
15425{
15426 struct breakpoint *b;
15427 struct bp_location *bl;
15428
15429 ALL_BREAKPOINTS (b)
15430 {
15431 if (!is_non_inline_function (b))
15432 continue;
15433
15434 for (bl = b->loc; bl != NULL; bl = bl->next)
15435 {
15436 if (!bl->shlib_disabled
09ac7c10 15437 && bpstat_check_location (bl, aspace, pc, ws))
0574c78f
GB
15438 return 1;
15439 }
15440 }
15441
15442 return 0;
15443}
15444
2f202fde
JK
15445/* Remove any references to OBJFILE which is going to be freed. */
15446
15447void
15448breakpoint_free_objfile (struct objfile *objfile)
15449{
15450 struct bp_location **locp, *loc;
15451
15452 ALL_BP_LOCATIONS (loc, locp)
eb822aa6 15453 if (loc->symtab != NULL && SYMTAB_OBJFILE (loc->symtab) == objfile)
2f202fde
JK
15454 loc->symtab = NULL;
15455}
15456
2060206e
PA
15457void
15458initialize_breakpoint_ops (void)
15459{
15460 static int initialized = 0;
15461
15462 struct breakpoint_ops *ops;
15463
15464 if (initialized)
15465 return;
15466 initialized = 1;
15467
15468 /* The breakpoint_ops structure to be inherit by all kinds of
15469 breakpoints (real breakpoints, i.e., user "break" breakpoints,
15470 internal and momentary breakpoints, etc.). */
15471 ops = &bkpt_base_breakpoint_ops;
15472 *ops = base_breakpoint_ops;
15473 ops->re_set = bkpt_re_set;
15474 ops->insert_location = bkpt_insert_location;
15475 ops->remove_location = bkpt_remove_location;
15476 ops->breakpoint_hit = bkpt_breakpoint_hit;
5f700d83 15477 ops->create_sals_from_location = bkpt_create_sals_from_location;
983af33b 15478 ops->create_breakpoints_sal = bkpt_create_breakpoints_sal;
5f700d83 15479 ops->decode_location = bkpt_decode_location;
2060206e
PA
15480
15481 /* The breakpoint_ops structure to be used in regular breakpoints. */
15482 ops = &bkpt_breakpoint_ops;
15483 *ops = bkpt_base_breakpoint_ops;
15484 ops->re_set = bkpt_re_set;
15485 ops->resources_needed = bkpt_resources_needed;
15486 ops->print_it = bkpt_print_it;
15487 ops->print_mention = bkpt_print_mention;
15488 ops->print_recreate = bkpt_print_recreate;
15489
15490 /* Ranged breakpoints. */
15491 ops = &ranged_breakpoint_ops;
15492 *ops = bkpt_breakpoint_ops;
15493 ops->breakpoint_hit = breakpoint_hit_ranged_breakpoint;
15494 ops->resources_needed = resources_needed_ranged_breakpoint;
15495 ops->print_it = print_it_ranged_breakpoint;
15496 ops->print_one = print_one_ranged_breakpoint;
15497 ops->print_one_detail = print_one_detail_ranged_breakpoint;
15498 ops->print_mention = print_mention_ranged_breakpoint;
15499 ops->print_recreate = print_recreate_ranged_breakpoint;
15500
15501 /* Internal breakpoints. */
15502 ops = &internal_breakpoint_ops;
15503 *ops = bkpt_base_breakpoint_ops;
15504 ops->re_set = internal_bkpt_re_set;
15505 ops->check_status = internal_bkpt_check_status;
15506 ops->print_it = internal_bkpt_print_it;
15507 ops->print_mention = internal_bkpt_print_mention;
15508
15509 /* Momentary breakpoints. */
15510 ops = &momentary_breakpoint_ops;
15511 *ops = bkpt_base_breakpoint_ops;
15512 ops->re_set = momentary_bkpt_re_set;
15513 ops->check_status = momentary_bkpt_check_status;
15514 ops->print_it = momentary_bkpt_print_it;
15515 ops->print_mention = momentary_bkpt_print_mention;
15516
55aa24fb
SDJ
15517 /* Probe breakpoints. */
15518 ops = &bkpt_probe_breakpoint_ops;
15519 *ops = bkpt_breakpoint_ops;
15520 ops->insert_location = bkpt_probe_insert_location;
15521 ops->remove_location = bkpt_probe_remove_location;
5f700d83
KS
15522 ops->create_sals_from_location = bkpt_probe_create_sals_from_location;
15523 ops->decode_location = bkpt_probe_decode_location;
55aa24fb 15524
2060206e
PA
15525 /* Watchpoints. */
15526 ops = &watchpoint_breakpoint_ops;
15527 *ops = base_breakpoint_ops;
15528 ops->re_set = re_set_watchpoint;
15529 ops->insert_location = insert_watchpoint;
15530 ops->remove_location = remove_watchpoint;
15531 ops->breakpoint_hit = breakpoint_hit_watchpoint;
15532 ops->check_status = check_status_watchpoint;
15533 ops->resources_needed = resources_needed_watchpoint;
15534 ops->works_in_software_mode = works_in_software_mode_watchpoint;
15535 ops->print_it = print_it_watchpoint;
15536 ops->print_mention = print_mention_watchpoint;
15537 ops->print_recreate = print_recreate_watchpoint;
427cd150 15538 ops->explains_signal = explains_signal_watchpoint;
2060206e
PA
15539
15540 /* Masked watchpoints. */
15541 ops = &masked_watchpoint_breakpoint_ops;
15542 *ops = watchpoint_breakpoint_ops;
15543 ops->insert_location = insert_masked_watchpoint;
15544 ops->remove_location = remove_masked_watchpoint;
15545 ops->resources_needed = resources_needed_masked_watchpoint;
15546 ops->works_in_software_mode = works_in_software_mode_masked_watchpoint;
15547 ops->print_it = print_it_masked_watchpoint;
15548 ops->print_one_detail = print_one_detail_masked_watchpoint;
15549 ops->print_mention = print_mention_masked_watchpoint;
15550 ops->print_recreate = print_recreate_masked_watchpoint;
15551
15552 /* Tracepoints. */
15553 ops = &tracepoint_breakpoint_ops;
15554 *ops = base_breakpoint_ops;
15555 ops->re_set = tracepoint_re_set;
15556 ops->breakpoint_hit = tracepoint_breakpoint_hit;
15557 ops->print_one_detail = tracepoint_print_one_detail;
15558 ops->print_mention = tracepoint_print_mention;
15559 ops->print_recreate = tracepoint_print_recreate;
5f700d83 15560 ops->create_sals_from_location = tracepoint_create_sals_from_location;
983af33b 15561 ops->create_breakpoints_sal = tracepoint_create_breakpoints_sal;
5f700d83 15562 ops->decode_location = tracepoint_decode_location;
983af33b 15563
55aa24fb
SDJ
15564 /* Probe tracepoints. */
15565 ops = &tracepoint_probe_breakpoint_ops;
15566 *ops = tracepoint_breakpoint_ops;
5f700d83
KS
15567 ops->create_sals_from_location = tracepoint_probe_create_sals_from_location;
15568 ops->decode_location = tracepoint_probe_decode_location;
55aa24fb 15569
983af33b
SDJ
15570 /* Static tracepoints with marker (`-m'). */
15571 ops = &strace_marker_breakpoint_ops;
15572 *ops = tracepoint_breakpoint_ops;
5f700d83 15573 ops->create_sals_from_location = strace_marker_create_sals_from_location;
983af33b 15574 ops->create_breakpoints_sal = strace_marker_create_breakpoints_sal;
5f700d83 15575 ops->decode_location = strace_marker_decode_location;
2060206e
PA
15576
15577 /* Fork catchpoints. */
15578 ops = &catch_fork_breakpoint_ops;
15579 *ops = base_breakpoint_ops;
15580 ops->insert_location = insert_catch_fork;
15581 ops->remove_location = remove_catch_fork;
15582 ops->breakpoint_hit = breakpoint_hit_catch_fork;
15583 ops->print_it = print_it_catch_fork;
15584 ops->print_one = print_one_catch_fork;
15585 ops->print_mention = print_mention_catch_fork;
15586 ops->print_recreate = print_recreate_catch_fork;
15587
15588 /* Vfork catchpoints. */
15589 ops = &catch_vfork_breakpoint_ops;
15590 *ops = base_breakpoint_ops;
15591 ops->insert_location = insert_catch_vfork;
15592 ops->remove_location = remove_catch_vfork;
15593 ops->breakpoint_hit = breakpoint_hit_catch_vfork;
15594 ops->print_it = print_it_catch_vfork;
15595 ops->print_one = print_one_catch_vfork;
15596 ops->print_mention = print_mention_catch_vfork;
15597 ops->print_recreate = print_recreate_catch_vfork;
15598
15599 /* Exec catchpoints. */
15600 ops = &catch_exec_breakpoint_ops;
15601 *ops = base_breakpoint_ops;
2060206e
PA
15602 ops->insert_location = insert_catch_exec;
15603 ops->remove_location = remove_catch_exec;
15604 ops->breakpoint_hit = breakpoint_hit_catch_exec;
15605 ops->print_it = print_it_catch_exec;
15606 ops->print_one = print_one_catch_exec;
15607 ops->print_mention = print_mention_catch_exec;
15608 ops->print_recreate = print_recreate_catch_exec;
15609
edcc5120
TT
15610 /* Solib-related catchpoints. */
15611 ops = &catch_solib_breakpoint_ops;
15612 *ops = base_breakpoint_ops;
edcc5120
TT
15613 ops->insert_location = insert_catch_solib;
15614 ops->remove_location = remove_catch_solib;
15615 ops->breakpoint_hit = breakpoint_hit_catch_solib;
15616 ops->check_status = check_status_catch_solib;
15617 ops->print_it = print_it_catch_solib;
15618 ops->print_one = print_one_catch_solib;
15619 ops->print_mention = print_mention_catch_solib;
15620 ops->print_recreate = print_recreate_catch_solib;
e7e0cddf
SS
15621
15622 ops = &dprintf_breakpoint_ops;
15623 *ops = bkpt_base_breakpoint_ops;
5c2b4418 15624 ops->re_set = dprintf_re_set;
e7e0cddf
SS
15625 ops->resources_needed = bkpt_resources_needed;
15626 ops->print_it = bkpt_print_it;
15627 ops->print_mention = bkpt_print_mention;
2d9442cc 15628 ops->print_recreate = dprintf_print_recreate;
9d6e6e84 15629 ops->after_condition_true = dprintf_after_condition_true;
cd1608cc 15630 ops->breakpoint_hit = dprintf_breakpoint_hit;
2060206e
PA
15631}
15632
8bfd80db
YQ
15633/* Chain containing all defined "enable breakpoint" subcommands. */
15634
15635static struct cmd_list_element *enablebreaklist = NULL;
15636
8588b356
SM
15637/* See breakpoint.h. */
15638
15639cmd_list_element *commands_cmd_element = nullptr;
15640
6c265988 15641void _initialize_breakpoint ();
c906108c 15642void
6c265988 15643_initialize_breakpoint ()
c906108c
SS
15644{
15645 struct cmd_list_element *c;
15646
2060206e
PA
15647 initialize_breakpoint_ops ();
15648
c90e7d63
SM
15649 gdb::observers::solib_unloaded.attach (disable_breakpoints_in_unloaded_shlib,
15650 "breakpoint");
15651 gdb::observers::free_objfile.attach (disable_breakpoints_in_freed_objfile,
15652 "breakpoint");
15653 gdb::observers::memory_changed.attach (invalidate_bp_value_on_memory_change,
15654 "breakpoint");
84acb35a 15655
c906108c
SS
15656 breakpoint_chain = 0;
15657 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
15658 before a breakpoint is set. */
15659 breakpoint_count = 0;
15660
1042e4c0
SS
15661 tracepoint_count = 0;
15662
1bedd215
AC
15663 add_com ("ignore", class_breakpoint, ignore_command, _("\
15664Set ignore-count of breakpoint number N to COUNT.\n\
15665Usage is `ignore N COUNT'."));
c906108c 15666
8588b356
SM
15667 commands_cmd_element = add_com ("commands", class_breakpoint,
15668 commands_command, _("\
18da0c51
MG
15669Set commands to be executed when the given breakpoints are hit.\n\
15670Give a space-separated breakpoint list as argument after \"commands\".\n\
15671A list element can be a breakpoint number (e.g. `5') or a range of numbers\n\
15672(e.g. `5-7').\n\
c906108c
SS
15673With no argument, the targeted breakpoint is the last one set.\n\
15674The commands themselves follow starting on the next line.\n\
15675Type a line containing \"end\" to indicate the end of them.\n\
15676Give \"silent\" as the first line to make the breakpoint silent;\n\
1bedd215 15677then no output is printed when it is hit, except what the commands print."));
c906108c 15678
b1d4d8d1
TBA
15679 const auto cc_opts = make_condition_command_options_def_group (nullptr);
15680 static std::string condition_command_help
15681 = gdb::option::build_help (_("\
1bedd215 15682Specify breakpoint number N to break only if COND is true.\n\
b1d4d8d1 15683Usage is `condition [OPTION] N COND', where N is an integer and COND\n\
733d554a 15684is an expression to be evaluated whenever breakpoint N is reached.\n\
b1d4d8d1
TBA
15685\n\
15686Options:\n\
15687%OPTIONS%"), cc_opts);
15688
15689 c = add_com ("condition", class_breakpoint, condition_command,
15690 condition_command_help.c_str ());
15691 set_cmd_completer_handle_brkchars (c, condition_completer);
c906108c 15692
1bedd215 15693 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
31e2b00f 15694Set a temporary breakpoint.\n\
c906108c
SS
15695Like \"break\" except the breakpoint is only temporary,\n\
15696so it will be deleted when hit. Equivalent to \"break\" followed\n\
31e2b00f
AS
15697by using \"enable delete\" on the breakpoint number.\n\
15698\n"
15699BREAK_ARGS_HELP ("tbreak")));
5ba2abeb 15700 set_cmd_completer (c, location_completer);
c94fdfd0 15701
1bedd215 15702 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
a3be7890 15703Set a hardware assisted breakpoint.\n\
c906108c 15704Like \"break\" except the breakpoint requires hardware support,\n\
31e2b00f
AS
15705some target hardware may not have this support.\n\
15706\n"
15707BREAK_ARGS_HELP ("hbreak")));
5ba2abeb 15708 set_cmd_completer (c, location_completer);
c906108c 15709
1bedd215 15710 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
31e2b00f 15711Set a temporary hardware assisted breakpoint.\n\
c906108c 15712Like \"hbreak\" except the breakpoint is only temporary,\n\
31e2b00f
AS
15713so it will be deleted when hit.\n\
15714\n"
15715BREAK_ARGS_HELP ("thbreak")));
5ba2abeb 15716 set_cmd_completer (c, location_completer);
c906108c 15717
1bedd215 15718 add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
590042fc
PW
15719Enable all or some breakpoints.\n\
15720Usage: enable [BREAKPOINTNUM]...\n\
c906108c
SS
15721Give breakpoint numbers (separated by spaces) as arguments.\n\
15722With no subcommand, breakpoints are enabled until you command otherwise.\n\
15723This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 15724With a subcommand you can enable temporarily."),
c906108c 15725 &enablelist, "enable ", 1, &cmdlist);
c906108c
SS
15726
15727 add_com_alias ("en", "enable", class_breakpoint, 1);
15728
84951ab5 15729 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
590042fc
PW
15730Enable all or some breakpoints.\n\
15731Usage: enable breakpoints [BREAKPOINTNUM]...\n\
c906108c
SS
15732Give breakpoint numbers (separated by spaces) as arguments.\n\
15733This is used to cancel the effect of the \"disable\" command.\n\
89549d7f 15734May be abbreviated to simply \"enable\"."),
c5aa993b 15735 &enablebreaklist, "enable breakpoints ", 1, &enablelist);
c906108c 15736
1a966eab 15737 add_cmd ("once", no_class, enable_once_command, _("\
590042fc
PW
15738Enable some breakpoints for one hit.\n\
15739Usage: enable breakpoints once BREAKPOINTNUM...\n\
1a966eab 15740If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
c906108c
SS
15741 &enablebreaklist);
15742
1a966eab 15743 add_cmd ("delete", no_class, enable_delete_command, _("\
590042fc
PW
15744Enable some breakpoints and delete when hit.\n\
15745Usage: enable breakpoints delete BREAKPOINTNUM...\n\
1a966eab 15746If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
15747 &enablebreaklist);
15748
816338b5 15749 add_cmd ("count", no_class, enable_count_command, _("\
590042fc
PW
15750Enable some breakpoints for COUNT hits.\n\
15751Usage: enable breakpoints count COUNT BREAKPOINTNUM...\n\
816338b5
SS
15752If a breakpoint is hit while enabled in this fashion,\n\
15753the count is decremented; when it reaches zero, the breakpoint is disabled."),
15754 &enablebreaklist);
15755
1a966eab 15756 add_cmd ("delete", no_class, enable_delete_command, _("\
590042fc
PW
15757Enable some breakpoints and delete when hit.\n\
15758Usage: enable delete BREAKPOINTNUM...\n\
1a966eab 15759If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
15760 &enablelist);
15761
1a966eab 15762 add_cmd ("once", no_class, enable_once_command, _("\
590042fc
PW
15763Enable some breakpoints for one hit.\n\
15764Usage: enable once BREAKPOINTNUM...\n\
1a966eab 15765If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
816338b5
SS
15766 &enablelist);
15767
15768 add_cmd ("count", no_class, enable_count_command, _("\
590042fc
PW
15769Enable some breakpoints for COUNT hits.\n\
15770Usage: enable count COUNT BREAKPOINTNUM...\n\
816338b5
SS
15771If a breakpoint is hit while enabled in this fashion,\n\
15772the count is decremented; when it reaches zero, the breakpoint is disabled."),
c906108c
SS
15773 &enablelist);
15774
1bedd215 15775 add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
590042fc
PW
15776Disable all or some breakpoints.\n\
15777Usage: disable [BREAKPOINTNUM]...\n\
c906108c
SS
15778Arguments are breakpoint numbers with spaces in between.\n\
15779To disable all breakpoints, give no argument.\n\
64b9b334 15780A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
c906108c
SS
15781 &disablelist, "disable ", 1, &cmdlist);
15782 add_com_alias ("dis", "disable", class_breakpoint, 1);
15783 add_com_alias ("disa", "disable", class_breakpoint, 1);
c906108c 15784
57b4f16e 15785 add_cmd ("breakpoints", class_breakpoint, disable_command, _("\
590042fc
PW
15786Disable all or some breakpoints.\n\
15787Usage: disable breakpoints [BREAKPOINTNUM]...\n\
c906108c
SS
15788Arguments are breakpoint numbers with spaces in between.\n\
15789To disable all breakpoints, give no argument.\n\
64b9b334 15790A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
1a966eab 15791This command may be abbreviated \"disable\"."),
c906108c
SS
15792 &disablelist);
15793
1bedd215 15794 add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
590042fc
PW
15795Delete all or some breakpoints.\n\
15796Usage: delete [BREAKPOINTNUM]...\n\
c906108c
SS
15797Arguments are breakpoint numbers with spaces in between.\n\
15798To delete all breakpoints, give no argument.\n\
15799\n\
590042fc 15800Also a prefix command for deletion of other GDB objects."),
c906108c
SS
15801 &deletelist, "delete ", 1, &cmdlist);
15802 add_com_alias ("d", "delete", class_breakpoint, 1);
7f198e01 15803 add_com_alias ("del", "delete", class_breakpoint, 1);
c906108c 15804
57b4f16e 15805 add_cmd ("breakpoints", class_breakpoint, delete_command, _("\
590042fc
PW
15806Delete all or some breakpoints or auto-display expressions.\n\
15807Usage: delete breakpoints [BREAKPOINTNUM]...\n\
c906108c
SS
15808Arguments are breakpoint numbers with spaces in between.\n\
15809To delete all breakpoints, give no argument.\n\
1a966eab 15810This command may be abbreviated \"delete\"."),
c906108c
SS
15811 &deletelist);
15812
1bedd215 15813 add_com ("clear", class_breakpoint, clear_command, _("\
629500fa
KS
15814Clear breakpoint at specified location.\n\
15815Argument may be a linespec, explicit, or address location as described below.\n\
1bedd215
AC
15816\n\
15817With no argument, clears all breakpoints in the line that the selected frame\n\
629500fa 15818is executing in.\n"
89549d7f 15819"\n" LOCATION_HELP_STRING "\n\n\
1bedd215 15820See also the \"delete\" command which clears breakpoints by number."));
a6cc4789 15821 add_com_alias ("cl", "clear", class_breakpoint, 1);
c906108c 15822
1bedd215 15823 c = add_com ("break", class_breakpoint, break_command, _("\
629500fa 15824Set breakpoint at specified location.\n"
31e2b00f 15825BREAK_ARGS_HELP ("break")));
5ba2abeb 15826 set_cmd_completer (c, location_completer);
c94fdfd0 15827
c906108c
SS
15828 add_com_alias ("b", "break", class_run, 1);
15829 add_com_alias ("br", "break", class_run, 1);
15830 add_com_alias ("bre", "break", class_run, 1);
15831 add_com_alias ("brea", "break", class_run, 1);
15832
c906108c
SS
15833 if (dbx_commands)
15834 {
1bedd215
AC
15835 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
15836Break in function/address or break at a line in the current file."),
c5aa993b
JM
15837 &stoplist, "stop ", 1, &cmdlist);
15838 add_cmd ("in", class_breakpoint, stopin_command,
1a966eab 15839 _("Break in function or address."), &stoplist);
c5aa993b 15840 add_cmd ("at", class_breakpoint, stopat_command,
1a966eab 15841 _("Break at a line in the current file."), &stoplist);
11db9430 15842 add_com ("status", class_info, info_breakpoints_command, _("\
1bedd215 15843Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
15844The \"Type\" column indicates one of:\n\
15845\tbreakpoint - normal breakpoint\n\
15846\twatchpoint - watchpoint\n\
15847The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15848the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15849breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
15850address and file/line number respectively.\n\
15851\n\
15852Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
15853are set to the address of the last breakpoint listed unless the command\n\
15854is prefixed with \"server \".\n\n\
c906108c 15855Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 15856breakpoint set."));
c906108c
SS
15857 }
15858
11db9430 15859 add_info ("breakpoints", info_breakpoints_command, _("\
e5a67952 15860Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
c906108c
SS
15861The \"Type\" column indicates one of:\n\
15862\tbreakpoint - normal breakpoint\n\
15863\twatchpoint - watchpoint\n\
15864The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15865the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15866breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
15867address and file/line number respectively.\n\
15868\n\
15869Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
15870are set to the address of the last breakpoint listed unless the command\n\
15871is prefixed with \"server \".\n\n\
c906108c 15872Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 15873breakpoint set."));
c906108c 15874
6b04bdb7
MS
15875 add_info_alias ("b", "breakpoints", 1);
15876
1a966eab
AC
15877 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
15878Status of all breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
15879The \"Type\" column indicates one of:\n\
15880\tbreakpoint - normal breakpoint\n\
15881\twatchpoint - watchpoint\n\
15882\tlongjmp - internal breakpoint used to step through longjmp()\n\
15883\tlongjmp resume - internal breakpoint at the target of longjmp()\n\
15884\tuntil - internal breakpoint used by the \"until\" command\n\
1a966eab
AC
15885\tfinish - internal breakpoint used by the \"finish\" command\n\
15886The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
c906108c
SS
15887the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15888breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1a966eab
AC
15889address and file/line number respectively.\n\
15890\n\
15891Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
15892are set to the address of the last breakpoint listed unless the command\n\
15893is prefixed with \"server \".\n\n\
c906108c 15894Convenience variable \"$bpnum\" contains the number of the last\n\
1a966eab 15895breakpoint set."),
c906108c
SS
15896 &maintenanceinfolist);
15897
3b6acaee 15898 add_basic_prefix_cmd ("catch", class_breakpoint, _("\
44feb3ce 15899Set catchpoints to catch events."),
3b6acaee
TT
15900 &catch_cmdlist, "catch ",
15901 0/*allow-unknown*/, &cmdlist);
44feb3ce 15902
3b6acaee 15903 add_basic_prefix_cmd ("tcatch", class_breakpoint, _("\
44feb3ce 15904Set temporary catchpoints to catch events."),
3b6acaee
TT
15905 &tcatch_cmdlist, "tcatch ",
15906 0/*allow-unknown*/, &cmdlist);
44feb3ce 15907
44feb3ce
TT
15908 add_catch_command ("fork", _("Catch calls to fork."),
15909 catch_fork_command_1,
dda83cd7 15910 NULL,
44feb3ce
TT
15911 (void *) (uintptr_t) catch_fork_permanent,
15912 (void *) (uintptr_t) catch_fork_temporary);
15913 add_catch_command ("vfork", _("Catch calls to vfork."),
15914 catch_fork_command_1,
dda83cd7 15915 NULL,
44feb3ce
TT
15916 (void *) (uintptr_t) catch_vfork_permanent,
15917 (void *) (uintptr_t) catch_vfork_temporary);
15918 add_catch_command ("exec", _("Catch calls to exec."),
15919 catch_exec_command_1,
dda83cd7 15920 NULL,
a96d9b2e
SDJ
15921 CATCH_PERMANENT,
15922 CATCH_TEMPORARY);
edcc5120
TT
15923 add_catch_command ("load", _("Catch loads of shared libraries.\n\
15924Usage: catch load [REGEX]\n\
15925If REGEX is given, only stop for libraries matching the regular expression."),
15926 catch_load_command_1,
15927 NULL,
15928 CATCH_PERMANENT,
15929 CATCH_TEMPORARY);
15930 add_catch_command ("unload", _("Catch unloads of shared libraries.\n\
15931Usage: catch unload [REGEX]\n\
15932If REGEX is given, only stop for libraries matching the regular expression."),
15933 catch_unload_command_1,
15934 NULL,
15935 CATCH_PERMANENT,
15936 CATCH_TEMPORARY);
c5aa993b 15937
a15a5258
AB
15938 const auto opts = make_watch_options_def_group (nullptr);
15939
15940 static const std::string watch_help = gdb::option::build_help (_("\
15941Set a watchpoint for EXPRESSION.\n\
15942Usage: watch [-location] EXPRESSION\n\
15943\n\
15944Options:\n\
15945%OPTIONS%\n\
15946\n\
c906108c 15947A watchpoint stops execution of your program whenever the value of\n\
a15a5258
AB
15948an expression changes."), opts);
15949 c = add_com ("watch", class_breakpoint, watch_command,
15950 watch_help.c_str ());
15951 set_cmd_completer_handle_brkchars (c, watch_command_completer);
15952
15953 static const std::string rwatch_help = gdb::option::build_help (_("\
15954Set a read watchpoint for EXPRESSION.\n\
15955Usage: rwatch [-location] EXPRESSION\n\
15956\n\
15957Options:\n\
15958%OPTIONS%\n\
15959\n\
15960A read watchpoint stops execution of your program whenever the value of\n\
15961an expression is read."), opts);
15962 c = add_com ("rwatch", class_breakpoint, rwatch_command,
15963 rwatch_help.c_str ());
15964 set_cmd_completer_handle_brkchars (c, watch_command_completer);
15965
15966 static const std::string awatch_help = gdb::option::build_help (_("\
15967Set an access watchpoint for EXPRESSION.\n\
15968Usage: awatch [-location] EXPRESSION\n\
15969\n\
15970Options:\n\
15971%OPTIONS%\n\
15972\n\
15973An access watchpoint stops execution of your program whenever the value\n\
15974of an expression is either read or written."), opts);
15975 c = add_com ("awatch", class_breakpoint, awatch_command,
15976 awatch_help.c_str ());
15977 set_cmd_completer_handle_brkchars (c, watch_command_completer);
c906108c 15978
11db9430 15979 add_info ("watchpoints", info_watchpoints_command, _("\
e5a67952 15980Status of specified watchpoints (all watchpoints if no argument)."));
c906108c 15981
920d2a44
AC
15982 /* XXX: cagney/2005-02-23: This should be a boolean, and should
15983 respond to changes - contrary to the description. */
85c07804
AC
15984 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
15985 &can_use_hw_watchpoints, _("\
15986Set debugger's willingness to use watchpoint hardware."), _("\
15987Show debugger's willingness to use watchpoint hardware."), _("\
c906108c
SS
15988If zero, gdb will not use hardware for new watchpoints, even if\n\
15989such is available. (However, any hardware watchpoints that were\n\
15990created before setting this to nonzero, will continue to use watchpoint\n\
85c07804
AC
15991hardware.)"),
15992 NULL,
920d2a44 15993 show_can_use_hw_watchpoints,
85c07804 15994 &setlist, &showlist);
c906108c
SS
15995
15996 can_use_hw_watchpoints = 1;
fa8d40ab 15997
1042e4c0
SS
15998 /* Tracepoint manipulation commands. */
15999
16000 c = add_com ("trace", class_breakpoint, trace_command, _("\
629500fa 16001Set a tracepoint at specified location.\n\
1042e4c0
SS
16002\n"
16003BREAK_ARGS_HELP ("trace") "\n\
16004Do \"help tracepoints\" for info on other tracepoint commands."));
16005 set_cmd_completer (c, location_completer);
16006
57b4f16e
PW
16007 add_com_alias ("tp", "trace", class_breakpoint, 0);
16008 add_com_alias ("tr", "trace", class_breakpoint, 1);
16009 add_com_alias ("tra", "trace", class_breakpoint, 1);
16010 add_com_alias ("trac", "trace", class_breakpoint, 1);
1042e4c0 16011
7a697b8d 16012 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
629500fa 16013Set a fast tracepoint at specified location.\n\
7a697b8d
SS
16014\n"
16015BREAK_ARGS_HELP ("ftrace") "\n\
16016Do \"help tracepoints\" for info on other tracepoint commands."));
16017 set_cmd_completer (c, location_completer);
16018
0fb4aa4b 16019 c = add_com ("strace", class_breakpoint, strace_command, _("\
629500fa 16020Set a static tracepoint at location or marker.\n\
0fb4aa4b
PA
16021\n\
16022strace [LOCATION] [if CONDITION]\n\
629500fa
KS
16023LOCATION may be a linespec, explicit, or address location (described below) \n\
16024or -m MARKER_ID.\n\n\
16025If a marker id is specified, probe the marker with that name. With\n\
16026no LOCATION, uses current execution address of the selected stack frame.\n\
0fb4aa4b
PA
16027Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
16028This collects arbitrary user data passed in the probe point call to the\n\
16029tracing library. You can inspect it when analyzing the trace buffer,\n\
16030by printing the $_sdata variable like any other convenience variable.\n\
16031\n\
16032CONDITION is a boolean expression.\n\
89549d7f 16033\n" LOCATION_HELP_STRING "\n\n\
d41c0fc8
PA
16034Multiple tracepoints at one place are permitted, and useful if their\n\
16035conditions are different.\n\
0fb4aa4b
PA
16036\n\
16037Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
16038Do \"help tracepoints\" for info on other tracepoint commands."));
16039 set_cmd_completer (c, location_completer);
16040
11db9430 16041 add_info ("tracepoints", info_tracepoints_command, _("\
e5a67952 16042Status of specified tracepoints (all tracepoints if no argument).\n\
1042e4c0
SS
16043Convenience variable \"$tpnum\" contains the number of the\n\
16044last tracepoint set."));
16045
16046 add_info_alias ("tp", "tracepoints", 1);
16047
16048 add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
16049Delete specified tracepoints.\n\
16050Arguments are tracepoint numbers, separated by spaces.\n\
16051No argument means delete all tracepoints."),
16052 &deletelist);
7e20dfcd 16053 add_alias_cmd ("tr", "tracepoints", class_trace, 1, &deletelist);
1042e4c0
SS
16054
16055 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
16056Disable specified tracepoints.\n\
16057Arguments are tracepoint numbers, separated by spaces.\n\
16058No argument means disable all tracepoints."),
16059 &disablelist);
16060 deprecate_cmd (c, "disable");
16061
16062 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
16063Enable specified tracepoints.\n\
16064Arguments are tracepoint numbers, separated by spaces.\n\
16065No argument means enable all tracepoints."),
16066 &enablelist);
16067 deprecate_cmd (c, "enable");
16068
16069 add_com ("passcount", class_trace, trace_pass_command, _("\
16070Set the passcount for a tracepoint.\n\
16071The trace will end when the tracepoint has been passed 'count' times.\n\
16072Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
16073if TPNUM is omitted, passcount refers to the last tracepoint defined."));
16074
0743fc83
TT
16075 add_basic_prefix_cmd ("save", class_breakpoint,
16076 _("Save breakpoint definitions as a script."),
16077 &save_cmdlist, "save ",
16078 0/*allow-unknown*/, &cmdlist);
6149aea9
PA
16079
16080 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
16081Save current breakpoint definitions as a script.\n\
cce7e648 16082This includes all types of breakpoints (breakpoints, watchpoints,\n\
6149aea9
PA
16083catchpoints, tracepoints). Use the 'source' command in another debug\n\
16084session to restore them."),
16085 &save_cmdlist);
16086 set_cmd_completer (c, filename_completer);
16087
16088 c = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
1042e4c0 16089Save current tracepoint definitions as a script.\n\
6149aea9
PA
16090Use the 'source' command in another debug session to restore them."),
16091 &save_cmdlist);
1042e4c0
SS
16092 set_cmd_completer (c, filename_completer);
16093
6149aea9
PA
16094 c = add_com_alias ("save-tracepoints", "save tracepoints", class_trace, 0);
16095 deprecate_cmd (c, "save tracepoints");
16096
3b6acaee 16097 add_basic_prefix_cmd ("breakpoint", class_maintenance, _("\
590042fc 16098Breakpoint specific settings.\n\
fa8d40ab 16099Configure various breakpoint-specific variables such as\n\
590042fc 16100pending breakpoint behavior."),
3b6acaee
TT
16101 &breakpoint_set_cmdlist, "set breakpoint ",
16102 0/*allow-unknown*/, &setlist);
16103 add_show_prefix_cmd ("breakpoint", class_maintenance, _("\
590042fc 16104Breakpoint specific settings.\n\
fa8d40ab 16105Configure various breakpoint-specific variables such as\n\
590042fc 16106pending breakpoint behavior."),
3b6acaee
TT
16107 &breakpoint_show_cmdlist, "show breakpoint ",
16108 0/*allow-unknown*/, &showlist);
fa8d40ab 16109
7915a72c
AC
16110 add_setshow_auto_boolean_cmd ("pending", no_class,
16111 &pending_break_support, _("\
16112Set debugger's behavior regarding pending breakpoints."), _("\
16113Show debugger's behavior regarding pending breakpoints."), _("\
6e1d7d6c
AC
16114If on, an unrecognized breakpoint location will cause gdb to create a\n\
16115pending breakpoint. If off, an unrecognized breakpoint location results in\n\
16116an error. If auto, an unrecognized breakpoint location results in a\n\
7915a72c 16117user-query to see if a pending breakpoint should be created."),
2c5b56ce 16118 NULL,
920d2a44 16119 show_pending_break_support,
6e1d7d6c
AC
16120 &breakpoint_set_cmdlist,
16121 &breakpoint_show_cmdlist);
fa8d40ab
JJ
16122
16123 pending_break_support = AUTO_BOOLEAN_AUTO;
765dc015
VP
16124
16125 add_setshow_boolean_cmd ("auto-hw", no_class,
16126 &automatic_hardware_breakpoints, _("\
16127Set automatic usage of hardware breakpoints."), _("\
16128Show automatic usage of hardware breakpoints."), _("\
16129If set, the debugger will automatically use hardware breakpoints for\n\
16130breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
16131a warning will be emitted for such breakpoints."),
16132 NULL,
16133 show_automatic_hardware_breakpoints,
16134 &breakpoint_set_cmdlist,
16135 &breakpoint_show_cmdlist);
74960c60 16136
a25a5a45
PA
16137 add_setshow_boolean_cmd ("always-inserted", class_support,
16138 &always_inserted_mode, _("\
74960c60
VP
16139Set mode for inserting breakpoints."), _("\
16140Show mode for inserting breakpoints."), _("\
a25a5a45
PA
16141When this mode is on, breakpoints are inserted immediately as soon as\n\
16142they're created, kept inserted even when execution stops, and removed\n\
16143only when the user deletes them. When this mode is off (the default),\n\
16144breakpoints are inserted only when execution continues, and removed\n\
16145when execution stops."),
72d0e2c5
YQ
16146 NULL,
16147 &show_always_inserted_mode,
16148 &breakpoint_set_cmdlist,
16149 &breakpoint_show_cmdlist);
f1310107 16150
b775012e
LM
16151 add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
16152 condition_evaluation_enums,
16153 &condition_evaluation_mode_1, _("\
16154Set mode of breakpoint condition evaluation."), _("\
16155Show mode of breakpoint condition evaluation."), _("\
d1cda5d9 16156When this is set to \"host\", breakpoint conditions will be\n\
b775012e
LM
16157evaluated on the host's side by GDB. When it is set to \"target\",\n\
16158breakpoint conditions will be downloaded to the target (if the target\n\
16159supports such feature) and conditions will be evaluated on the target's side.\n\
16160If this is set to \"auto\" (default), this will be automatically set to\n\
16161\"target\" if it supports condition evaluation, otherwise it will\n\
7a4e8e7d 16162be set to \"host\"."),
b775012e
LM
16163 &set_condition_evaluation_mode,
16164 &show_condition_evaluation_mode,
16165 &breakpoint_set_cmdlist,
16166 &breakpoint_show_cmdlist);
16167
f1310107
TJB
16168 add_com ("break-range", class_breakpoint, break_range_command, _("\
16169Set a breakpoint for an address range.\n\
16170break-range START-LOCATION, END-LOCATION\n\
16171where START-LOCATION and END-LOCATION can be one of the following:\n\
16172 LINENUM, for that line in the current file,\n\
16173 FILE:LINENUM, for that line in that file,\n\
16174 +OFFSET, for that number of lines after the current line\n\
dda83cd7 16175 or the start of the range\n\
f1310107
TJB
16176 FUNCTION, for the first line in that function,\n\
16177 FILE:FUNCTION, to distinguish among like-named static functions.\n\
16178 *ADDRESS, for the instruction at that address.\n\
16179\n\
16180The breakpoint will stop execution of the inferior whenever it executes\n\
16181an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
16182range (including START-LOCATION and END-LOCATION)."));
16183
e7e0cddf 16184 c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
629500fa 16185Set a dynamic printf at specified location.\n\
e7e0cddf 16186dprintf location,format string,arg1,arg2,...\n\
629500fa
KS
16187location may be a linespec, explicit, or address location.\n"
16188"\n" LOCATION_HELP_STRING));
e7e0cddf
SS
16189 set_cmd_completer (c, location_completer);
16190
16191 add_setshow_enum_cmd ("dprintf-style", class_support,
16192 dprintf_style_enums, &dprintf_style, _("\
16193Set the style of usage for dynamic printf."), _("\
16194Show the style of usage for dynamic printf."), _("\
16195This setting chooses how GDB will do a dynamic printf.\n\
16196If the value is \"gdb\", then the printing is done by GDB to its own\n\
16197console, as with the \"printf\" command.\n\
16198If the value is \"call\", the print is done by calling a function in your\n\
16199program; by default printf(), but you can choose a different function or\n\
16200output stream by setting dprintf-function and dprintf-channel."),
16201 update_dprintf_commands, NULL,
16202 &setlist, &showlist);
16203
16204 dprintf_function = xstrdup ("printf");
16205 add_setshow_string_cmd ("dprintf-function", class_support,
16206 &dprintf_function, _("\
590042fc
PW
16207Set the function to use for dynamic printf."), _("\
16208Show the function to use for dynamic printf."), NULL,
e7e0cddf
SS
16209 update_dprintf_commands, NULL,
16210 &setlist, &showlist);
16211
16212 dprintf_channel = xstrdup ("");
16213 add_setshow_string_cmd ("dprintf-channel", class_support,
16214 &dprintf_channel, _("\
590042fc
PW
16215Set the channel to use for dynamic printf."), _("\
16216Show the channel to use for dynamic printf."), NULL,
e7e0cddf
SS
16217 update_dprintf_commands, NULL,
16218 &setlist, &showlist);
16219
d3ce09f5
SS
16220 add_setshow_boolean_cmd ("disconnected-dprintf", no_class,
16221 &disconnected_dprintf, _("\
16222Set whether dprintf continues after GDB disconnects."), _("\
16223Show whether dprintf continues after GDB disconnects."), _("\
16224Use this to let dprintf commands continue to hit and produce output\n\
16225even if GDB disconnects or detaches from the target."),
16226 NULL,
16227 NULL,
16228 &setlist, &showlist);
16229
16230 add_com ("agent-printf", class_vars, agent_printf_command, _("\
590042fc
PW
16231Target agent only formatted printing, like the C \"printf\" function.\n\
16232Usage: agent-printf \"format string\", ARG1, ARG2, ARG3, ..., ARGN\n\
16233This supports most C printf format specifications, like %s, %d, etc.\n\
16234This is useful for formatted output in user-defined commands."));
d3ce09f5 16235
491144b5 16236 automatic_hardware_breakpoints = true;
f3b1572e 16237
c90e7d63
SM
16238 gdb::observers::about_to_proceed.attach (breakpoint_about_to_proceed,
16239 "breakpoint");
16240 gdb::observers::thread_exit.attach (remove_threaded_breakpoints,
16241 "breakpoint");
c906108c 16242}
This page took 3.598247 seconds and 4 git commands to generate.