gdb/breakpoint: add flags to 'condition' and 'break' commands to force condition
[deliverable/binutils-gdb.git] / gdb / breakpoint.c
CommitLineData
c906108c 1/* Everything about breakpoints, for GDB.
8926118c 2
b811d2c2 3 Copyright (C) 1986-2020 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,
88f7da05 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
fe3f5fa8 175static void free_bp_location (struct bp_location *loc);
f431efe5
PA
176static void incref_bp_location (struct bp_location *loc);
177static void decref_bp_location (struct bp_location **loc);
fe3f5fa8 178
39d61571 179static struct bp_location *allocate_bp_location (struct breakpoint *bpt);
a5606eee 180
44702360
PA
181/* update_global_location_list's modes of operation wrt to whether to
182 insert locations now. */
183enum ugll_insert_mode
184{
185 /* Don't insert any breakpoint locations into the inferior, only
186 remove already-inserted locations that no longer should be
187 inserted. Functions that delete a breakpoint or breakpoints
188 should specify this mode, so that deleting a breakpoint doesn't
189 have the side effect of inserting the locations of other
190 breakpoints that are marked not-inserted, but should_be_inserted
191 returns true on them.
192
193 This behavior is useful is situations close to tear-down -- e.g.,
194 after an exec, while the target still has execution, but
195 breakpoint shadows of the previous executable image should *NOT*
196 be restored to the new image; or before detaching, where the
197 target still has execution and wants to delete breakpoints from
198 GDB's lists, and all breakpoints had already been removed from
199 the inferior. */
200 UGLL_DONT_INSERT,
201
a25a5a45
PA
202 /* May insert breakpoints iff breakpoints_should_be_inserted_now
203 claims breakpoints should be inserted now. */
04086b45
PA
204 UGLL_MAY_INSERT,
205
a25a5a45
PA
206 /* Insert locations now, irrespective of
207 breakpoints_should_be_inserted_now. E.g., say all threads are
208 stopped right now, and the user did "continue". We need to
209 insert breakpoints _before_ resuming the target, but
210 UGLL_MAY_INSERT wouldn't insert them, because
211 breakpoints_should_be_inserted_now returns false at that point,
212 as no thread is running yet. */
04086b45 213 UGLL_INSERT
44702360
PA
214};
215
216static void update_global_location_list (enum ugll_insert_mode);
a5606eee 217
44702360 218static void update_global_location_list_nothrow (enum ugll_insert_mode);
74960c60 219
74960c60 220static void insert_breakpoint_locations (void);
a5606eee 221
0b39b52e 222static void trace_pass_command (const char *, int);
1042e4c0 223
558a9d82
YQ
224static void set_tracepoint_count (int num);
225
f2478a7e 226static bool is_masked_watchpoint (const struct breakpoint *b);
9c06b0b4 227
b775012e
LM
228static struct bp_location **get_first_locp_gte_addr (CORE_ADDR address);
229
983af33b
SDJ
230/* Return 1 if B refers to a static tracepoint set by marker ("-m"), zero
231 otherwise. */
232
233static int strace_marker_p (struct breakpoint *b);
0fb4aa4b 234
2060206e
PA
235/* The breakpoint_ops structure to be inherited by all breakpoint_ops
236 that are implemented on top of software or hardware breakpoints
237 (user breakpoints, internal and momentary breakpoints, etc.). */
238static struct breakpoint_ops bkpt_base_breakpoint_ops;
239
240/* Internal breakpoints class type. */
06edf0c0 241static struct breakpoint_ops internal_breakpoint_ops;
2060206e
PA
242
243/* Momentary breakpoints class type. */
06edf0c0
PA
244static struct breakpoint_ops momentary_breakpoint_ops;
245
2060206e
PA
246/* The breakpoint_ops structure to be used in regular user created
247 breakpoints. */
248struct breakpoint_ops bkpt_breakpoint_ops;
249
55aa24fb
SDJ
250/* Breakpoints set on probes. */
251static struct breakpoint_ops bkpt_probe_breakpoint_ops;
252
bac7c5cf
GB
253/* Tracepoints set on probes. */
254static struct breakpoint_ops tracepoint_probe_breakpoint_ops;
255
e7e0cddf 256/* Dynamic printf class type. */
c5867ab6 257struct breakpoint_ops dprintf_breakpoint_ops;
e7e0cddf 258
d3ce09f5
SS
259/* The style in which to perform a dynamic printf. This is a user
260 option because different output options have different tradeoffs;
261 if GDB does the printing, there is better error handling if there
262 is a problem with any of the arguments, but using an inferior
263 function lets you have special-purpose printers and sending of
264 output to the same place as compiled-in print functions. */
265
266static const char dprintf_style_gdb[] = "gdb";
267static const char dprintf_style_call[] = "call";
268static const char dprintf_style_agent[] = "agent";
269static const char *const dprintf_style_enums[] = {
270 dprintf_style_gdb,
271 dprintf_style_call,
272 dprintf_style_agent,
273 NULL
274};
275static const char *dprintf_style = dprintf_style_gdb;
276
277/* The function to use for dynamic printf if the preferred style is to
278 call into the inferior. The value is simply a string that is
279 copied into the command, so it can be anything that GDB can
280 evaluate to a callable address, not necessarily a function name. */
281
bde6261a 282static char *dprintf_function;
d3ce09f5
SS
283
284/* The channel to use for dynamic printf if the preferred style is to
285 call into the inferior; if a nonempty string, it will be passed to
286 the call as the first argument, with the format string as the
287 second. As with the dprintf function, this can be anything that
288 GDB knows how to evaluate, so in addition to common choices like
289 "stderr", this could be an app-specific expression like
290 "mystreams[curlogger]". */
291
bde6261a 292static char *dprintf_channel;
d3ce09f5
SS
293
294/* True if dprintf commands should continue to operate even if GDB
295 has disconnected. */
491144b5 296static bool disconnected_dprintf = true;
d3ce09f5 297
5cea2a26
PA
298struct command_line *
299breakpoint_commands (struct breakpoint *b)
300{
d1b0a7bf 301 return b->commands ? b->commands.get () : NULL;
5cea2a26 302}
3daf8fe5 303
f3b1572e
PA
304/* Flag indicating that a command has proceeded the inferior past the
305 current breakpoint. */
306
491144b5 307static bool breakpoint_proceeded;
f3b1572e 308
956a9fb9 309const char *
2cec12e5
AR
310bpdisp_text (enum bpdisp disp)
311{
4a64f543
MS
312 /* NOTE: the following values are a part of MI protocol and
313 represent values of 'disp' field returned when inferior stops at
314 a breakpoint. */
bc043ef3 315 static const char * const bpdisps[] = {"del", "dstp", "dis", "keep"};
cc59ec59 316
2cec12e5
AR
317 return bpdisps[(int) disp];
318}
c906108c 319
4a64f543 320/* Prototypes for exported functions. */
c906108c 321/* If FALSE, gdb will not use hardware support for watchpoints, even
4a64f543 322 if such is available. */
c906108c
SS
323static int can_use_hw_watchpoints;
324
920d2a44
AC
325static void
326show_can_use_hw_watchpoints (struct ui_file *file, int from_tty,
327 struct cmd_list_element *c,
328 const char *value)
329{
3e43a32a
MS
330 fprintf_filtered (file,
331 _("Debugger's willingness to use "
332 "watchpoint hardware is %s.\n"),
920d2a44
AC
333 value);
334}
335
fa8d40ab
JJ
336/* If AUTO_BOOLEAN_FALSE, gdb will not attempt to create pending breakpoints.
337 If AUTO_BOOLEAN_TRUE, gdb will automatically create pending breakpoints
4a64f543 338 for unrecognized breakpoint locations.
fa8d40ab
JJ
339 If AUTO_BOOLEAN_AUTO, gdb will query when breakpoints are unrecognized. */
340static enum auto_boolean pending_break_support;
920d2a44
AC
341static void
342show_pending_break_support (struct ui_file *file, int from_tty,
343 struct cmd_list_element *c,
344 const char *value)
345{
3e43a32a
MS
346 fprintf_filtered (file,
347 _("Debugger's behavior regarding "
348 "pending breakpoints is %s.\n"),
920d2a44
AC
349 value);
350}
fa8d40ab 351
491144b5 352/* If true, gdb will automatically use hardware breakpoints for breakpoints
4a64f543 353 set with "break" but falling in read-only memory.
491144b5 354 If false, gdb will warn about such breakpoints, but won't automatically
765dc015 355 use hardware breakpoints. */
491144b5 356static bool automatic_hardware_breakpoints;
765dc015
VP
357static void
358show_automatic_hardware_breakpoints (struct ui_file *file, int from_tty,
359 struct cmd_list_element *c,
360 const char *value)
361{
3e43a32a
MS
362 fprintf_filtered (file,
363 _("Automatic usage of hardware breakpoints is %s.\n"),
765dc015
VP
364 value);
365}
366
a25a5a45
PA
367/* If on, GDB keeps breakpoints inserted even if the inferior is
368 stopped, and immediately inserts any new breakpoints as soon as
369 they're created. If off (default), GDB keeps breakpoints off of
370 the target as long as possible. That is, it delays inserting
371 breakpoints until the next resume, and removes them again when the
372 target fully stops. This is a bit safer in case GDB crashes while
373 processing user input. */
491144b5 374static bool always_inserted_mode = false;
72d0e2c5 375
33e5cbd6 376static void
74960c60 377show_always_inserted_mode (struct ui_file *file, int from_tty,
33e5cbd6 378 struct cmd_list_element *c, const char *value)
74960c60 379{
a25a5a45
PA
380 fprintf_filtered (file, _("Always inserted breakpoint mode is %s.\n"),
381 value);
74960c60
VP
382}
383
b57bacec
PA
384/* See breakpoint.h. */
385
33e5cbd6 386int
a25a5a45 387breakpoints_should_be_inserted_now (void)
33e5cbd6 388{
a25a5a45
PA
389 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
390 {
391 /* If breakpoints are global, they should be inserted even if no
392 thread under gdb's control is running, or even if there are
393 no threads under GDB's control yet. */
394 return 1;
395 }
5b6d1e4f 396 else
a25a5a45 397 {
a25a5a45
PA
398 if (always_inserted_mode)
399 {
400 /* The user wants breakpoints inserted even if all threads
401 are stopped. */
402 return 1;
403 }
404
5b6d1e4f
PA
405 for (inferior *inf : all_inferiors ())
406 if (inf->has_execution ()
407 && threads_are_executing (inf->process_target ()))
408 return 1;
372316f1
PA
409
410 /* Don't remove breakpoints yet if, even though all threads are
411 stopped, we still have events to process. */
08036331 412 for (thread_info *tp : all_non_exited_threads ())
372316f1
PA
413 if (tp->resumed
414 && tp->suspend.waitstatus_pending_p)
415 return 1;
a25a5a45
PA
416 }
417 return 0;
33e5cbd6 418}
765dc015 419
b775012e
LM
420static const char condition_evaluation_both[] = "host or target";
421
422/* Modes for breakpoint condition evaluation. */
423static const char condition_evaluation_auto[] = "auto";
424static const char condition_evaluation_host[] = "host";
425static const char condition_evaluation_target[] = "target";
426static const char *const condition_evaluation_enums[] = {
427 condition_evaluation_auto,
428 condition_evaluation_host,
429 condition_evaluation_target,
430 NULL
431};
432
433/* Global that holds the current mode for breakpoint condition evaluation. */
434static const char *condition_evaluation_mode_1 = condition_evaluation_auto;
435
436/* Global that we use to display information to the user (gets its value from
437 condition_evaluation_mode_1. */
438static const char *condition_evaluation_mode = condition_evaluation_auto;
439
440/* Translate a condition evaluation mode MODE into either "host"
441 or "target". This is used mostly to translate from "auto" to the
442 real setting that is being used. It returns the translated
443 evaluation mode. */
444
445static const char *
446translate_condition_evaluation_mode (const char *mode)
447{
448 if (mode == condition_evaluation_auto)
449 {
450 if (target_supports_evaluation_of_breakpoint_conditions ())
451 return condition_evaluation_target;
452 else
453 return condition_evaluation_host;
454 }
455 else
456 return mode;
457}
458
459/* Discovers what condition_evaluation_auto translates to. */
460
461static const char *
462breakpoint_condition_evaluation_mode (void)
463{
464 return translate_condition_evaluation_mode (condition_evaluation_mode);
465}
466
467/* Return true if GDB should evaluate breakpoint conditions or false
468 otherwise. */
469
470static int
471gdb_evaluates_breakpoint_condition_p (void)
472{
473 const char *mode = breakpoint_condition_evaluation_mode ();
474
475 return (mode == condition_evaluation_host);
476}
477
c906108c
SS
478/* Are we executing breakpoint commands? */
479static int executing_breakpoint_commands;
480
c02f5703
MS
481/* Are overlay event breakpoints enabled? */
482static int overlay_events_enabled;
483
e09342b5 484/* See description in breakpoint.h. */
491144b5 485bool target_exact_watchpoints = false;
e09342b5 486
c906108c 487/* Walk the following statement or block through all breakpoints.
e5dd4106 488 ALL_BREAKPOINTS_SAFE does so even if the statement deletes the
4a64f543 489 current breakpoint. */
c906108c 490
5c44784c 491#define ALL_BREAKPOINTS(B) for (B = breakpoint_chain; B; B = B->next)
c906108c 492
5c44784c
JM
493#define ALL_BREAKPOINTS_SAFE(B,TMP) \
494 for (B = breakpoint_chain; \
495 B ? (TMP=B->next, 1): 0; \
496 B = TMP)
c906108c 497
4a64f543
MS
498/* Similar iterator for the low-level breakpoints. SAFE variant is
499 not provided so update_global_location_list must not be called
500 while executing the block of ALL_BP_LOCATIONS. */
7cc221ef 501
876fa593 502#define ALL_BP_LOCATIONS(B,BP_TMP) \
f5336ca5
PA
503 for (BP_TMP = bp_locations; \
504 BP_TMP < bp_locations + bp_locations_count && (B = *BP_TMP);\
876fa593 505 BP_TMP++)
7cc221ef 506
b775012e
LM
507/* Iterates through locations with address ADDRESS for the currently selected
508 program space. BP_LOCP_TMP points to each object. BP_LOCP_START points
509 to where the loop should start from.
510 If BP_LOCP_START is a NULL pointer, the macro automatically seeks the
511 appropriate location to start with. */
512
513#define ALL_BP_LOCATIONS_AT_ADDR(BP_LOCP_TMP, BP_LOCP_START, ADDRESS) \
514 for (BP_LOCP_START = BP_LOCP_START == NULL ? get_first_locp_gte_addr (ADDRESS) : BP_LOCP_START, \
515 BP_LOCP_TMP = BP_LOCP_START; \
516 BP_LOCP_START \
f5336ca5 517 && (BP_LOCP_TMP < bp_locations + bp_locations_count \
b775012e
LM
518 && (*BP_LOCP_TMP)->address == ADDRESS); \
519 BP_LOCP_TMP++)
520
1042e4c0
SS
521/* Iterator for tracepoints only. */
522
523#define ALL_TRACEPOINTS(B) \
524 for (B = breakpoint_chain; B; B = B->next) \
d77f58be 525 if (is_tracepoint (B))
1042e4c0 526
7cc221ef 527/* Chains of all breakpoints defined. */
c906108c 528
81e6b8eb 529static struct breakpoint *breakpoint_chain;
c906108c 530
39ef2f62 531/* Array is sorted by bp_location_is_less_than - primarily by the ADDRESS. */
876fa593 532
f5336ca5 533static struct bp_location **bp_locations;
876fa593 534
f5336ca5 535/* Number of elements of BP_LOCATIONS. */
876fa593 536
f5336ca5 537static unsigned bp_locations_count;
876fa593 538
4a64f543 539/* Maximum alignment offset between bp_target_info.PLACED_ADDRESS and
f5336ca5 540 ADDRESS for the current elements of BP_LOCATIONS which get a valid
4a64f543 541 result from bp_location_has_shadow. You can use it for roughly
f5336ca5 542 limiting the subrange of BP_LOCATIONS to scan for shadow bytes for
4a64f543 543 an address you need to read. */
876fa593 544
f5336ca5 545static CORE_ADDR bp_locations_placed_address_before_address_max;
876fa593 546
4a64f543
MS
547/* Maximum offset plus alignment between bp_target_info.PLACED_ADDRESS
548 + bp_target_info.SHADOW_LEN and ADDRESS for the current elements of
f5336ca5
PA
549 BP_LOCATIONS which get a valid result from bp_location_has_shadow.
550 You can use it for roughly limiting the subrange of BP_LOCATIONS to
4a64f543 551 scan for shadow bytes for an address you need to read. */
876fa593 552
f5336ca5 553static CORE_ADDR bp_locations_shadow_len_after_address_max;
7cc221ef 554
4a64f543 555/* The locations that no longer correspond to any breakpoint, unlinked
f5336ca5
PA
556 from the bp_locations array, but for which a hit may still be
557 reported by a target. */
1123588c 558static std::vector<bp_location *> moribund_locations;
20874c92 559
c906108c
SS
560/* Number of last breakpoint made. */
561
95a42b64
TT
562static int breakpoint_count;
563
86b17b60
PA
564/* The value of `breakpoint_count' before the last command that
565 created breakpoints. If the last (break-like) command created more
566 than one breakpoint, then the difference between BREAKPOINT_COUNT
567 and PREV_BREAKPOINT_COUNT is more than one. */
568static int prev_breakpoint_count;
c906108c 569
1042e4c0
SS
570/* Number of last tracepoint made. */
571
95a42b64 572static int tracepoint_count;
1042e4c0 573
6149aea9
PA
574static struct cmd_list_element *breakpoint_set_cmdlist;
575static struct cmd_list_element *breakpoint_show_cmdlist;
9291a0cd 576struct cmd_list_element *save_cmdlist;
6149aea9 577
badd37ce
SDJ
578/* See declaration at breakpoint.h. */
579
580struct breakpoint *
581breakpoint_find_if (int (*func) (struct breakpoint *b, void *d),
582 void *user_data)
583{
584 struct breakpoint *b = NULL;
585
586 ALL_BREAKPOINTS (b)
587 {
588 if (func (b, user_data) != 0)
589 break;
590 }
591
592 return b;
593}
594
468d015d
JJ
595/* Return whether a breakpoint is an active enabled breakpoint. */
596static int
597breakpoint_enabled (struct breakpoint *b)
598{
0d381245 599 return (b->enable_state == bp_enabled);
468d015d
JJ
600}
601
c906108c
SS
602/* Set breakpoint count to NUM. */
603
95a42b64 604static void
fba45db2 605set_breakpoint_count (int num)
c906108c 606{
86b17b60 607 prev_breakpoint_count = breakpoint_count;
c906108c 608 breakpoint_count = num;
4fa62494 609 set_internalvar_integer (lookup_internalvar ("bpnum"), num);
c906108c
SS
610}
611
86b17b60
PA
612/* Used by `start_rbreak_breakpoints' below, to record the current
613 breakpoint count before "rbreak" creates any breakpoint. */
614static int rbreak_start_breakpoint_count;
615
95a42b64
TT
616/* Called at the start an "rbreak" command to record the first
617 breakpoint made. */
86b17b60 618
c80049d3 619scoped_rbreak_breakpoints::scoped_rbreak_breakpoints ()
95a42b64 620{
86b17b60 621 rbreak_start_breakpoint_count = breakpoint_count;
95a42b64
TT
622}
623
624/* Called at the end of an "rbreak" command to record the last
625 breakpoint made. */
86b17b60 626
c80049d3 627scoped_rbreak_breakpoints::~scoped_rbreak_breakpoints ()
95a42b64 628{
86b17b60 629 prev_breakpoint_count = rbreak_start_breakpoint_count;
95a42b64
TT
630}
631
4a64f543 632/* Used in run_command to zero the hit count when a new run starts. */
c906108c
SS
633
634void
fba45db2 635clear_breakpoint_hit_counts (void)
c906108c
SS
636{
637 struct breakpoint *b;
638
639 ALL_BREAKPOINTS (b)
640 b->hit_count = 0;
641}
642
c906108c 643\f
48cb2d85
VP
644/* Return the breakpoint with the specified number, or NULL
645 if the number does not refer to an existing breakpoint. */
646
647struct breakpoint *
648get_breakpoint (int num)
649{
650 struct breakpoint *b;
651
652 ALL_BREAKPOINTS (b)
653 if (b->number == num)
654 return b;
655
656 return NULL;
657}
5c44784c 658
c906108c 659\f
adc36818 660
b775012e
LM
661/* Mark locations as "conditions have changed" in case the target supports
662 evaluating conditions on its side. */
663
664static void
665mark_breakpoint_modified (struct breakpoint *b)
666{
667 struct bp_location *loc;
668
669 /* This is only meaningful if the target is
670 evaluating conditions and if the user has
671 opted for condition evaluation on the target's
672 side. */
673 if (gdb_evaluates_breakpoint_condition_p ()
674 || !target_supports_evaluation_of_breakpoint_conditions ())
675 return;
676
677 if (!is_breakpoint (b))
678 return;
679
680 for (loc = b->loc; loc; loc = loc->next)
681 loc->condition_changed = condition_modified;
682}
683
684/* Mark location as "conditions have changed" in case the target supports
685 evaluating conditions on its side. */
686
687static void
688mark_breakpoint_location_modified (struct bp_location *loc)
689{
690 /* This is only meaningful if the target is
691 evaluating conditions and if the user has
692 opted for condition evaluation on the target's
693 side. */
694 if (gdb_evaluates_breakpoint_condition_p ()
695 || !target_supports_evaluation_of_breakpoint_conditions ())
696
697 return;
698
699 if (!is_breakpoint (loc->owner))
700 return;
701
702 loc->condition_changed = condition_modified;
703}
704
705/* Sets the condition-evaluation mode using the static global
706 condition_evaluation_mode. */
707
708static void
eb4c3f4a 709set_condition_evaluation_mode (const char *args, int from_tty,
b775012e
LM
710 struct cmd_list_element *c)
711{
b775012e
LM
712 const char *old_mode, *new_mode;
713
714 if ((condition_evaluation_mode_1 == condition_evaluation_target)
715 && !target_supports_evaluation_of_breakpoint_conditions ())
716 {
717 condition_evaluation_mode_1 = condition_evaluation_mode;
718 warning (_("Target does not support breakpoint condition evaluation.\n"
719 "Using host evaluation mode instead."));
720 return;
721 }
722
723 new_mode = translate_condition_evaluation_mode (condition_evaluation_mode_1);
724 old_mode = translate_condition_evaluation_mode (condition_evaluation_mode);
725
abf1152a
JK
726 /* Flip the switch. Flip it even if OLD_MODE == NEW_MODE as one of the
727 settings was "auto". */
728 condition_evaluation_mode = condition_evaluation_mode_1;
729
b775012e
LM
730 /* Only update the mode if the user picked a different one. */
731 if (new_mode != old_mode)
732 {
733 struct bp_location *loc, **loc_tmp;
734 /* If the user switched to a different evaluation mode, we
735 need to synch the changes with the target as follows:
736
737 "host" -> "target": Send all (valid) conditions to the target.
738 "target" -> "host": Remove all the conditions from the target.
739 */
740
b775012e
LM
741 if (new_mode == condition_evaluation_target)
742 {
743 /* Mark everything modified and synch conditions with the
744 target. */
745 ALL_BP_LOCATIONS (loc, loc_tmp)
746 mark_breakpoint_location_modified (loc);
747 }
748 else
749 {
750 /* Manually mark non-duplicate locations to synch conditions
751 with the target. We do this to remove all the conditions the
752 target knows about. */
753 ALL_BP_LOCATIONS (loc, loc_tmp)
754 if (is_breakpoint (loc->owner) && loc->inserted)
755 loc->needs_update = 1;
756 }
757
758 /* Do the update. */
44702360 759 update_global_location_list (UGLL_MAY_INSERT);
b775012e
LM
760 }
761
762 return;
763}
764
765/* Shows the current mode of breakpoint condition evaluation. Explicitly shows
766 what "auto" is translating to. */
767
768static void
769show_condition_evaluation_mode (struct ui_file *file, int from_tty,
770 struct cmd_list_element *c, const char *value)
771{
772 if (condition_evaluation_mode == condition_evaluation_auto)
773 fprintf_filtered (file,
774 _("Breakpoint condition evaluation "
775 "mode is %s (currently %s).\n"),
776 value,
777 breakpoint_condition_evaluation_mode ());
778 else
779 fprintf_filtered (file, _("Breakpoint condition evaluation mode is %s.\n"),
780 value);
781}
782
783/* A comparison function for bp_location AP and BP that is used by
784 bsearch. This comparison function only cares about addresses, unlike
39ef2f62 785 the more general bp_location_is_less_than function. */
b775012e
LM
786
787static int
f5336ca5 788bp_locations_compare_addrs (const void *ap, const void *bp)
b775012e 789{
9a3c8263
SM
790 const struct bp_location *a = *(const struct bp_location **) ap;
791 const struct bp_location *b = *(const struct bp_location **) bp;
b775012e
LM
792
793 if (a->address == b->address)
794 return 0;
795 else
796 return ((a->address > b->address) - (a->address < b->address));
797}
798
799/* Helper function to skip all bp_locations with addresses
800 less than ADDRESS. It returns the first bp_location that
801 is greater than or equal to ADDRESS. If none is found, just
802 return NULL. */
803
804static struct bp_location **
805get_first_locp_gte_addr (CORE_ADDR address)
806{
807 struct bp_location dummy_loc;
808 struct bp_location *dummy_locp = &dummy_loc;
809 struct bp_location **locp_found = NULL;
810
811 /* Initialize the dummy location's address field. */
b775012e
LM
812 dummy_loc.address = address;
813
814 /* Find a close match to the first location at ADDRESS. */
9a3c8263 815 locp_found = ((struct bp_location **)
f5336ca5 816 bsearch (&dummy_locp, bp_locations, bp_locations_count,
9a3c8263 817 sizeof (struct bp_location **),
f5336ca5 818 bp_locations_compare_addrs));
b775012e
LM
819
820 /* Nothing was found, nothing left to do. */
821 if (locp_found == NULL)
822 return NULL;
823
824 /* We may have found a location that is at ADDRESS but is not the first in the
825 location's list. Go backwards (if possible) and locate the first one. */
f5336ca5 826 while ((locp_found - 1) >= bp_locations
b775012e
LM
827 && (*(locp_found - 1))->address == address)
828 locp_found--;
829
830 return locp_found;
831}
832
b5fa468f
TBA
833/* Parse COND_STRING in the context of LOC and set as the condition
834 expression of LOC. BP_NUM is the number of LOC's owner, LOC_NUM is
835 the number of LOC within its owner. In case of parsing error, mark
836 LOC as DISABLED_BY_COND. In case of success, unset DISABLED_BY_COND. */
837
838static void
839set_breakpoint_location_condition (const char *cond_string, bp_location *loc,
840 int bp_num, int loc_num)
841{
842 bool has_junk = false;
843 try
844 {
845 expression_up new_exp = parse_exp_1 (&cond_string, loc->address,
846 block_for_pc (loc->address), 0);
847 if (*cond_string != 0)
848 has_junk = true;
849 else
850 {
851 loc->cond = std::move (new_exp);
852 if (loc->disabled_by_cond && loc->enabled)
853 printf_filtered (_("Breakpoint %d's condition is now valid at "
854 "location %d, enabling.\n"),
855 bp_num, loc_num);
856
857 loc->disabled_by_cond = false;
858 }
859 }
860 catch (const gdb_exception_error &e)
861 {
862 if (loc->enabled)
863 {
864 /* Warn if a user-enabled location is now becoming disabled-by-cond.
865 BP_NUM is 0 if the breakpoint is being defined for the first
866 time using the "break ... if ..." command, and non-zero if
867 already defined. */
868 if (bp_num != 0)
869 warning (_("failed to validate condition at location %d.%d, "
870 "disabling:\n %s"), bp_num, loc_num, e.what ());
871 else
872 warning (_("failed to validate condition at location %d, "
873 "disabling:\n %s"), loc_num, e.what ());
874 }
875
876 loc->disabled_by_cond = true;
877 }
878
879 if (has_junk)
880 error (_("Garbage '%s' follows condition"), cond_string);
881}
882
adc36818 883void
7a26bd4d 884set_breakpoint_condition (struct breakpoint *b, const char *exp,
733d554a 885 int from_tty, bool force)
adc36818 886{
4c55e970 887 if (*exp == 0)
3a5c3e22 888 {
4c55e970
TBA
889 xfree (b->cond_string);
890 b->cond_string = nullptr;
3a5c3e22 891
4c55e970 892 if (is_watchpoint (b))
78319c15 893 static_cast<watchpoint *> (b)->cond_exp.reset ();
4c55e970
TBA
894 else
895 {
b5fa468f 896 int loc_num = 1;
78319c15 897 for (bp_location *loc = b->loc; loc != nullptr; loc = loc->next)
4c55e970
TBA
898 {
899 loc->cond.reset ();
b5fa468f
TBA
900 if (loc->disabled_by_cond && loc->enabled)
901 printf_filtered (_("Breakpoint %d's condition is now valid at "
902 "location %d, enabling.\n"),
903 b->number, loc_num);
904 loc->disabled_by_cond = false;
905 loc_num++;
4c55e970
TBA
906
907 /* No need to free the condition agent expression
908 bytecode (if we have one). We will handle this
909 when we go through update_global_location_list. */
910 }
911 }
1e620590 912
adc36818
PM
913 if (from_tty)
914 printf_filtered (_("Breakpoint %d now unconditional.\n"), b->number);
915 }
916 else
917 {
adc36818
PM
918 if (is_watchpoint (b))
919 {
699bd4cf 920 innermost_block_tracker tracker;
78319c15 921 const char *arg = exp;
4c55e970 922 expression_up new_exp = parse_exp_1 (&arg, 0, 0, 0, &tracker);
78319c15 923 if (*arg != 0)
adc36818 924 error (_("Junk at end of expression"));
78319c15 925 watchpoint *w = static_cast<watchpoint *> (b);
4c55e970 926 w->cond_exp = std::move (new_exp);
699bd4cf 927 w->cond_exp_valid_block = tracker.block ();
adc36818
PM
928 }
929 else
930 {
4c55e970
TBA
931 /* Parse and set condition expressions. We make two passes.
932 In the first, we parse the condition string to see if it
b5fa468f
TBA
933 is valid in at least one location. If so, the condition
934 would be accepted. So we go ahead and set the locations'
935 conditions. In case no valid case is found, we throw
4c55e970
TBA
936 the error and the condition string will be rejected.
937 This two-pass approach is taken to avoid setting the
938 state of locations in case of a reject. */
78319c15 939 for (bp_location *loc = b->loc; loc != nullptr; loc = loc->next)
4c55e970 940 {
b5fa468f
TBA
941 try
942 {
943 const char *arg = exp;
944 parse_exp_1 (&arg, loc->address,
945 block_for_pc (loc->address), 0);
946 if (*arg != 0)
947 error (_("Junk at end of expression"));
948 break;
949 }
950 catch (const gdb_exception_error &e)
951 {
952 /* Condition string is invalid. If this happens to
733d554a
TBA
953 be the last loc, abandon (if not forced) or continue
954 (if forced). */
955 if (loc->next == nullptr && !force)
b5fa468f
TBA
956 throw;
957 }
4c55e970
TBA
958 }
959
b5fa468f
TBA
960 /* If we reach here, the condition is valid at some locations. */
961 int loc_num = 1;
962 for (bp_location *loc = b->loc; loc != nullptr;
963 loc = loc->next, loc_num++)
964 set_breakpoint_location_condition (exp, loc, b->number, loc_num);
adc36818 965 }
1e620590
TBA
966
967 /* We know that the new condition parsed successfully. The
968 condition string of the breakpoint can be safely updated. */
969 xfree (b->cond_string);
970 b->cond_string = xstrdup (exp);
971 b->condition_not_parsed = 0;
adc36818 972 }
b775012e
LM
973 mark_breakpoint_modified (b);
974
76727919 975 gdb::observers::breakpoint_modified.notify (b);
adc36818
PM
976}
977
d55637df
TT
978/* Completion for the "condition" command. */
979
eb3ff9a5 980static void
6f937416 981condition_completer (struct cmd_list_element *cmd,
eb3ff9a5 982 completion_tracker &tracker,
6f937416 983 const char *text, const char *word)
d55637df 984{
6f937416 985 const char *space;
d55637df 986
f1735a53
TT
987 text = skip_spaces (text);
988 space = skip_to_space (text);
d55637df
TT
989 if (*space == '\0')
990 {
991 int len;
992 struct breakpoint *b;
d55637df
TT
993
994 if (text[0] == '$')
995 {
996 /* We don't support completion of history indices. */
eb3ff9a5
PA
997 if (!isdigit (text[1]))
998 complete_internalvar (tracker, &text[1]);
999 return;
d55637df
TT
1000 }
1001
1002 /* We're completing the breakpoint number. */
1003 len = strlen (text);
1004
1005 ALL_BREAKPOINTS (b)
58ce7251
SDJ
1006 {
1007 char number[50];
1008
1009 xsnprintf (number, sizeof (number), "%d", b->number);
1010
1011 if (strncmp (number, text, len) == 0)
b02f78f9 1012 tracker.add_completion (make_unique_xstrdup (number));
58ce7251 1013 }
d55637df 1014
eb3ff9a5 1015 return;
d55637df
TT
1016 }
1017
1018 /* We're completing the expression part. */
f1735a53 1019 text = skip_spaces (space);
eb3ff9a5 1020 expression_completer (cmd, tracker, text, word);
d55637df
TT
1021}
1022
c906108c
SS
1023/* condition N EXP -- set break condition of breakpoint N to EXP. */
1024
1025static void
0b39b52e 1026condition_command (const char *arg, int from_tty)
c906108c 1027{
52f0bd74 1028 struct breakpoint *b;
0b39b52e 1029 const char *p;
52f0bd74 1030 int bnum;
c906108c
SS
1031
1032 if (arg == 0)
e2e0b3e5 1033 error_no_arg (_("breakpoint number"));
c906108c
SS
1034
1035 p = arg;
733d554a
TBA
1036
1037 /* Check if the "-force" flag was passed. */
1038 bool force = false;
1039 const char *tok = skip_spaces (p);
1040 const char *end_tok = skip_to_space (tok);
1041 int toklen = end_tok - tok;
1042 if (toklen >= 1 && strncmp (tok, "-force", toklen) == 0)
1043 {
1044 force = true;
1045 p = end_tok + 1;
1046 }
1047
c906108c 1048 bnum = get_number (&p);
5c44784c 1049 if (bnum == 0)
8a3fe4f8 1050 error (_("Bad breakpoint argument: '%s'"), arg);
c906108c
SS
1051
1052 ALL_BREAKPOINTS (b)
1053 if (b->number == bnum)
2f069f6f 1054 {
6dddc817
DE
1055 /* Check if this breakpoint has a "stop" method implemented in an
1056 extension language. This method and conditions entered into GDB
1057 from the CLI are mutually exclusive. */
1058 const struct extension_language_defn *extlang
1059 = get_breakpoint_cond_ext_lang (b, EXT_LANG_NONE);
1060
1061 if (extlang != NULL)
1062 {
1063 error (_("Only one stop condition allowed. There is currently"
1064 " a %s stop condition defined for this breakpoint."),
1065 ext_lang_capitalized_name (extlang));
1066 }
733d554a 1067 set_breakpoint_condition (b, p, from_tty, force);
b775012e
LM
1068
1069 if (is_breakpoint (b))
44702360 1070 update_global_location_list (UGLL_MAY_INSERT);
b775012e 1071
2f069f6f
JB
1072 return;
1073 }
c906108c 1074
8a3fe4f8 1075 error (_("No breakpoint number %d."), bnum);
c906108c
SS
1076}
1077
a7bdde9e
VP
1078/* Check that COMMAND do not contain commands that are suitable
1079 only for tracepoints and not suitable for ordinary breakpoints.
4a64f543
MS
1080 Throw if any such commands is found. */
1081
a7bdde9e
VP
1082static void
1083check_no_tracepoint_commands (struct command_line *commands)
1084{
1085 struct command_line *c;
cc59ec59 1086
a7bdde9e
VP
1087 for (c = commands; c; c = c->next)
1088 {
a7bdde9e 1089 if (c->control_type == while_stepping_control)
3e43a32a
MS
1090 error (_("The 'while-stepping' command can "
1091 "only be used for tracepoints"));
a7bdde9e 1092
12973681
TT
1093 check_no_tracepoint_commands (c->body_list_0.get ());
1094 check_no_tracepoint_commands (c->body_list_1.get ());
a7bdde9e
VP
1095
1096 /* Not that command parsing removes leading whitespace and comment
4a64f543 1097 lines and also empty lines. So, we only need to check for
a7bdde9e
VP
1098 command directly. */
1099 if (strstr (c->line, "collect ") == c->line)
1100 error (_("The 'collect' command can only be used for tracepoints"));
1101
51661e93
VP
1102 if (strstr (c->line, "teval ") == c->line)
1103 error (_("The 'teval' command can only be used for tracepoints"));
a7bdde9e
VP
1104 }
1105}
1106
c1fc2657 1107struct longjmp_breakpoint : public breakpoint
3b0871f4 1108{
c1fc2657 1109 ~longjmp_breakpoint () override;
3b0871f4
SM
1110};
1111
d77f58be
SS
1112/* Encapsulate tests for different types of tracepoints. */
1113
3b0871f4
SM
1114static bool
1115is_tracepoint_type (bptype type)
d9b3f62e
PA
1116{
1117 return (type == bp_tracepoint
1118 || type == bp_fast_tracepoint
1119 || type == bp_static_tracepoint);
1120}
1121
3b0871f4
SM
1122static bool
1123is_longjmp_type (bptype type)
1124{
1125 return type == bp_longjmp || type == bp_exception;
1126}
1127
f2478a7e
SM
1128/* See breakpoint.h. */
1129
1130bool
d77f58be 1131is_tracepoint (const struct breakpoint *b)
a7bdde9e 1132{
d9b3f62e 1133 return is_tracepoint_type (b->type);
a7bdde9e 1134}
d9b3f62e 1135
a5e364af
SM
1136/* Factory function to create an appropriate instance of breakpoint given
1137 TYPE. */
1138
1139static std::unique_ptr<breakpoint>
1140new_breakpoint_from_type (bptype type)
1141{
1142 breakpoint *b;
1143
1144 if (is_tracepoint_type (type))
c1fc2657 1145 b = new tracepoint ();
3b0871f4 1146 else if (is_longjmp_type (type))
c1fc2657 1147 b = new longjmp_breakpoint ();
a5e364af
SM
1148 else
1149 b = new breakpoint ();
1150
1151 return std::unique_ptr<breakpoint> (b);
1152}
1153
e5dd4106 1154/* A helper function that validates that COMMANDS are valid for a
95a42b64
TT
1155 breakpoint. This function will throw an exception if a problem is
1156 found. */
48cb2d85 1157
95a42b64
TT
1158static void
1159validate_commands_for_breakpoint (struct breakpoint *b,
1160 struct command_line *commands)
48cb2d85 1161{
d77f58be 1162 if (is_tracepoint (b))
a7bdde9e 1163 {
c9a6ce02 1164 struct tracepoint *t = (struct tracepoint *) b;
a7bdde9e
VP
1165 struct command_line *c;
1166 struct command_line *while_stepping = 0;
c9a6ce02
PA
1167
1168 /* Reset the while-stepping step count. The previous commands
1169 might have included a while-stepping action, while the new
1170 ones might not. */
1171 t->step_count = 0;
1172
1173 /* We need to verify that each top-level element of commands is
1174 valid for tracepoints, that there's at most one
1175 while-stepping element, and that the while-stepping's body
1176 has valid tracing commands excluding nested while-stepping.
1177 We also need to validate the tracepoint action line in the
1178 context of the tracepoint --- validate_actionline actually
1179 has side effects, like setting the tracepoint's
1180 while-stepping STEP_COUNT, in addition to checking if the
1181 collect/teval actions parse and make sense in the
1182 tracepoint's context. */
a7bdde9e
VP
1183 for (c = commands; c; c = c->next)
1184 {
a7bdde9e
VP
1185 if (c->control_type == while_stepping_control)
1186 {
1187 if (b->type == bp_fast_tracepoint)
3e43a32a
MS
1188 error (_("The 'while-stepping' command "
1189 "cannot be used for fast tracepoint"));
0fb4aa4b 1190 else if (b->type == bp_static_tracepoint)
3e43a32a
MS
1191 error (_("The 'while-stepping' command "
1192 "cannot be used for static tracepoint"));
a7bdde9e
VP
1193
1194 if (while_stepping)
3e43a32a
MS
1195 error (_("The 'while-stepping' command "
1196 "can be used only once"));
a7bdde9e
VP
1197 else
1198 while_stepping = c;
1199 }
c9a6ce02
PA
1200
1201 validate_actionline (c->line, b);
a7bdde9e
VP
1202 }
1203 if (while_stepping)
1204 {
1205 struct command_line *c2;
1206
12973681
TT
1207 gdb_assert (while_stepping->body_list_1 == nullptr);
1208 c2 = while_stepping->body_list_0.get ();
a7bdde9e
VP
1209 for (; c2; c2 = c2->next)
1210 {
a7bdde9e
VP
1211 if (c2->control_type == while_stepping_control)
1212 error (_("The 'while-stepping' command cannot be nested"));
1213 }
1214 }
1215 }
1216 else
1217 {
1218 check_no_tracepoint_commands (commands);
1219 }
95a42b64
TT
1220}
1221
0fb4aa4b
PA
1222/* Return a vector of all the static tracepoints set at ADDR. The
1223 caller is responsible for releasing the vector. */
1224
f51e0e20 1225std::vector<breakpoint *>
0fb4aa4b
PA
1226static_tracepoints_here (CORE_ADDR addr)
1227{
1228 struct breakpoint *b;
f51e0e20 1229 std::vector<breakpoint *> found;
0fb4aa4b
PA
1230 struct bp_location *loc;
1231
1232 ALL_BREAKPOINTS (b)
1233 if (b->type == bp_static_tracepoint)
1234 {
1235 for (loc = b->loc; loc; loc = loc->next)
1236 if (loc->address == addr)
f51e0e20 1237 found.push_back (b);
0fb4aa4b
PA
1238 }
1239
1240 return found;
1241}
1242
95a42b64 1243/* Set the command list of B to COMMANDS. If breakpoint is tracepoint,
4a64f543 1244 validate that only allowed commands are included. */
95a42b64
TT
1245
1246void
4a64f543 1247breakpoint_set_commands (struct breakpoint *b,
12973681 1248 counted_command_line &&commands)
95a42b64 1249{
93921405 1250 validate_commands_for_breakpoint (b, commands.get ());
a7bdde9e 1251
d1b0a7bf 1252 b->commands = std::move (commands);
76727919 1253 gdb::observers::breakpoint_modified.notify (b);
48cb2d85
VP
1254}
1255
45a43567
TT
1256/* Set the internal `silent' flag on the breakpoint. Note that this
1257 is not the same as the "silent" that may appear in the breakpoint's
1258 commands. */
1259
1260void
1261breakpoint_set_silent (struct breakpoint *b, int silent)
1262{
1263 int old_silent = b->silent;
1264
1265 b->silent = silent;
1266 if (old_silent != silent)
76727919 1267 gdb::observers::breakpoint_modified.notify (b);
45a43567
TT
1268}
1269
1270/* Set the thread for this breakpoint. If THREAD is -1, make the
1271 breakpoint work for any thread. */
1272
1273void
1274breakpoint_set_thread (struct breakpoint *b, int thread)
1275{
1276 int old_thread = b->thread;
1277
1278 b->thread = thread;
1279 if (old_thread != thread)
76727919 1280 gdb::observers::breakpoint_modified.notify (b);
45a43567
TT
1281}
1282
1283/* Set the task for this breakpoint. If TASK is 0, make the
1284 breakpoint work for any task. */
1285
1286void
1287breakpoint_set_task (struct breakpoint *b, int task)
1288{
1289 int old_task = b->task;
1290
1291 b->task = task;
1292 if (old_task != task)
76727919 1293 gdb::observers::breakpoint_modified.notify (b);
45a43567
TT
1294}
1295
95a42b64 1296static void
896b6bda 1297commands_command_1 (const char *arg, int from_tty,
4a64f543 1298 struct command_line *control)
95a42b64 1299{
d1b0a7bf 1300 counted_command_line cmd;
999700cd
PW
1301 /* cmd_read will be true once we have read cmd. Note that cmd might still be
1302 NULL after the call to read_command_lines if the user provides an empty
1303 list of command by just typing "end". */
1304 bool cmd_read = false;
95a42b64 1305
896b6bda
PA
1306 std::string new_arg;
1307
95a42b64
TT
1308 if (arg == NULL || !*arg)
1309 {
da1df1db 1310 /* Argument not explicitly given. Synthesize it. */
86b17b60 1311 if (breakpoint_count - prev_breakpoint_count > 1)
896b6bda
PA
1312 new_arg = string_printf ("%d-%d", prev_breakpoint_count + 1,
1313 breakpoint_count);
95a42b64 1314 else if (breakpoint_count > 0)
896b6bda 1315 new_arg = string_printf ("%d", breakpoint_count);
48649e1b 1316 }
da1df1db
TBA
1317 else
1318 {
1319 /* Create a copy of ARG. This is needed because the "commands"
1320 command may be coming from a script. In that case, the read
1321 line buffer is going to be overwritten in the lambda of
1322 'map_breakpoint_numbers' below when reading the next line
1323 before we are are done parsing the breakpoint numbers. */
1324 new_arg = arg;
1325 }
1326 arg = new_arg.c_str ();
48649e1b
TT
1327
1328 map_breakpoint_numbers
1329 (arg, [&] (breakpoint *b)
1330 {
999700cd 1331 if (!cmd_read)
48649e1b 1332 {
999700cd 1333 gdb_assert (cmd == NULL);
48649e1b 1334 if (control != NULL)
12973681 1335 cmd = control->body_list_0;
48649e1b
TT
1336 else
1337 {
81b1e71c
TT
1338 std::string str
1339 = string_printf (_("Type commands for breakpoint(s) "
1340 "%s, one per line."),
1341 arg);
48649e1b 1342
60b3cef2
TT
1343 auto do_validate = [=] (const char *line)
1344 {
1345 validate_actionline (line, b);
1346 };
1347 gdb::function_view<void (const char *)> validator;
1348 if (is_tracepoint (b))
1349 validator = do_validate;
1350
1351 cmd = read_command_lines (str.c_str (), from_tty, 1, validator);
48649e1b 1352 }
999700cd 1353 cmd_read = true;
48649e1b
TT
1354 }
1355
1356 /* If a breakpoint was on the list more than once, we don't need to
1357 do anything. */
1358 if (b->commands != cmd)
1359 {
d1b0a7bf 1360 validate_commands_for_breakpoint (b, cmd.get ());
48649e1b 1361 b->commands = cmd;
76727919 1362 gdb::observers::breakpoint_modified.notify (b);
48649e1b
TT
1363 }
1364 });
95a42b64
TT
1365}
1366
1367static void
0b39b52e 1368commands_command (const char *arg, int from_tty)
95a42b64
TT
1369{
1370 commands_command_1 (arg, from_tty, NULL);
c906108c 1371}
40c03ae8
EZ
1372
1373/* Like commands_command, but instead of reading the commands from
1374 input stream, takes them from an already parsed command structure.
1375
1376 This is used by cli-script.c to DTRT with breakpoint commands
1377 that are part of if and while bodies. */
1378enum command_control_type
896b6bda 1379commands_from_control_command (const char *arg, struct command_line *cmd)
40c03ae8 1380{
95a42b64
TT
1381 commands_command_1 (arg, 0, cmd);
1382 return simple_control;
40c03ae8 1383}
876fa593
JK
1384
1385/* Return non-zero if BL->TARGET_INFO contains valid information. */
1386
1387static int
1388bp_location_has_shadow (struct bp_location *bl)
1389{
1390 if (bl->loc_type != bp_loc_software_breakpoint)
1391 return 0;
1392 if (!bl->inserted)
1393 return 0;
1394 if (bl->target_info.shadow_len == 0)
e5dd4106 1395 /* BL isn't valid, or doesn't shadow memory. */
876fa593
JK
1396 return 0;
1397 return 1;
1398}
1399
9d497a19
PA
1400/* Update BUF, which is LEN bytes read from the target address
1401 MEMADDR, by replacing a memory breakpoint with its shadowed
1402 contents.
1403
1404 If READBUF is not NULL, this buffer must not overlap with the of
1405 the breakpoint location's shadow_contents buffer. Otherwise, a
1406 failed assertion internal error will be raised. */
1407
1408static void
1409one_breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1410 const gdb_byte *writebuf_org,
1411 ULONGEST memaddr, LONGEST len,
1412 struct bp_target_info *target_info,
1413 struct gdbarch *gdbarch)
1414{
1415 /* Now do full processing of the found relevant range of elements. */
1416 CORE_ADDR bp_addr = 0;
1417 int bp_size = 0;
1418 int bptoffset = 0;
1419
1420 if (!breakpoint_address_match (target_info->placed_address_space, 0,
1421 current_program_space->aspace, 0))
1422 {
1423 /* The breakpoint is inserted in a different address space. */
1424 return;
1425 }
1426
1427 /* Addresses and length of the part of the breakpoint that
1428 we need to copy. */
1429 bp_addr = target_info->placed_address;
1430 bp_size = target_info->shadow_len;
1431
1432 if (bp_addr + bp_size <= memaddr)
1433 {
1434 /* The breakpoint is entirely before the chunk of memory we are
1435 reading. */
1436 return;
1437 }
1438
1439 if (bp_addr >= memaddr + len)
1440 {
1441 /* The breakpoint is entirely after the chunk of memory we are
1442 reading. */
1443 return;
1444 }
1445
1446 /* Offset within shadow_contents. */
1447 if (bp_addr < memaddr)
1448 {
1449 /* Only copy the second part of the breakpoint. */
1450 bp_size -= memaddr - bp_addr;
1451 bptoffset = memaddr - bp_addr;
1452 bp_addr = memaddr;
1453 }
1454
1455 if (bp_addr + bp_size > memaddr + len)
1456 {
1457 /* Only copy the first part of the breakpoint. */
1458 bp_size -= (bp_addr + bp_size) - (memaddr + len);
1459 }
1460
1461 if (readbuf != NULL)
1462 {
1463 /* Verify that the readbuf buffer does not overlap with the
1464 shadow_contents buffer. */
1465 gdb_assert (target_info->shadow_contents >= readbuf + len
1466 || readbuf >= (target_info->shadow_contents
1467 + target_info->shadow_len));
1468
1469 /* Update the read buffer with this inserted breakpoint's
1470 shadow. */
1471 memcpy (readbuf + bp_addr - memaddr,
1472 target_info->shadow_contents + bptoffset, bp_size);
1473 }
1474 else
1475 {
1476 const unsigned char *bp;
0d5ed153
MR
1477 CORE_ADDR addr = target_info->reqstd_address;
1478 int placed_size;
9d497a19
PA
1479
1480 /* Update the shadow with what we want to write to memory. */
1481 memcpy (target_info->shadow_contents + bptoffset,
1482 writebuf_org + bp_addr - memaddr, bp_size);
1483
1484 /* Determine appropriate breakpoint contents and size for this
1485 address. */
0d5ed153 1486 bp = gdbarch_breakpoint_from_pc (gdbarch, &addr, &placed_size);
9d497a19
PA
1487
1488 /* Update the final write buffer with this inserted
1489 breakpoint's INSN. */
1490 memcpy (writebuf + bp_addr - memaddr, bp + bptoffset, bp_size);
1491 }
1492}
1493
8defab1a 1494/* Update BUF, which is LEN bytes read from the target address MEMADDR,
876fa593
JK
1495 by replacing any memory breakpoints with their shadowed contents.
1496
35c63cd8
JB
1497 If READBUF is not NULL, this buffer must not overlap with any of
1498 the breakpoint location's shadow_contents buffers. Otherwise,
1499 a failed assertion internal error will be raised.
1500
876fa593 1501 The range of shadowed area by each bp_location is:
f5336ca5
PA
1502 bl->address - bp_locations_placed_address_before_address_max
1503 up to bl->address + bp_locations_shadow_len_after_address_max
876fa593
JK
1504 The range we were requested to resolve shadows for is:
1505 memaddr ... memaddr + len
1506 Thus the safe cutoff boundaries for performance optimization are
35df4500 1507 memaddr + len <= (bl->address
f5336ca5 1508 - bp_locations_placed_address_before_address_max)
876fa593 1509 and:
f5336ca5 1510 bl->address + bp_locations_shadow_len_after_address_max <= memaddr */
c906108c 1511
8defab1a 1512void
f0ba3972
PA
1513breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1514 const gdb_byte *writebuf_org,
1515 ULONGEST memaddr, LONGEST len)
c906108c 1516{
4a64f543
MS
1517 /* Left boundary, right boundary and median element of our binary
1518 search. */
876fa593
JK
1519 unsigned bc_l, bc_r, bc;
1520
4a64f543
MS
1521 /* Find BC_L which is a leftmost element which may affect BUF
1522 content. It is safe to report lower value but a failure to
1523 report higher one. */
876fa593
JK
1524
1525 bc_l = 0;
f5336ca5 1526 bc_r = bp_locations_count;
876fa593
JK
1527 while (bc_l + 1 < bc_r)
1528 {
35df4500 1529 struct bp_location *bl;
876fa593
JK
1530
1531 bc = (bc_l + bc_r) / 2;
f5336ca5 1532 bl = bp_locations[bc];
876fa593 1533
4a64f543
MS
1534 /* Check first BL->ADDRESS will not overflow due to the added
1535 constant. Then advance the left boundary only if we are sure
1536 the BC element can in no way affect the BUF content (MEMADDR
1537 to MEMADDR + LEN range).
876fa593 1538
f5336ca5 1539 Use the BP_LOCATIONS_SHADOW_LEN_AFTER_ADDRESS_MAX safety
4a64f543
MS
1540 offset so that we cannot miss a breakpoint with its shadow
1541 range tail still reaching MEMADDR. */
c5aa993b 1542
f5336ca5 1543 if ((bl->address + bp_locations_shadow_len_after_address_max
35df4500 1544 >= bl->address)
f5336ca5 1545 && (bl->address + bp_locations_shadow_len_after_address_max
35df4500 1546 <= memaddr))
876fa593
JK
1547 bc_l = bc;
1548 else
1549 bc_r = bc;
1550 }
1551
128070bb
PA
1552 /* Due to the binary search above, we need to make sure we pick the
1553 first location that's at BC_L's address. E.g., if there are
1554 multiple locations at the same address, BC_L may end up pointing
1555 at a duplicate location, and miss the "master"/"inserted"
1556 location. Say, given locations L1, L2 and L3 at addresses A and
1557 B:
1558
1559 L1@A, L2@A, L3@B, ...
1560
1561 BC_L could end up pointing at location L2, while the "master"
1562 location could be L1. Since the `loc->inserted' flag is only set
1563 on "master" locations, we'd forget to restore the shadow of L1
1564 and L2. */
1565 while (bc_l > 0
f5336ca5 1566 && bp_locations[bc_l]->address == bp_locations[bc_l - 1]->address)
128070bb
PA
1567 bc_l--;
1568
876fa593
JK
1569 /* Now do full processing of the found relevant range of elements. */
1570
f5336ca5 1571 for (bc = bc_l; bc < bp_locations_count; bc++)
c5aa993b 1572 {
f5336ca5 1573 struct bp_location *bl = bp_locations[bc];
876fa593 1574
35df4500
TJB
1575 /* bp_location array has BL->OWNER always non-NULL. */
1576 if (bl->owner->type == bp_none)
8a3fe4f8 1577 warning (_("reading through apparently deleted breakpoint #%d?"),
35df4500 1578 bl->owner->number);
ffce0d52 1579
e5dd4106 1580 /* Performance optimization: any further element can no longer affect BUF
876fa593
JK
1581 content. */
1582
f5336ca5
PA
1583 if (bl->address >= bp_locations_placed_address_before_address_max
1584 && memaddr + len <= (bl->address
1585 - bp_locations_placed_address_before_address_max))
876fa593
JK
1586 break;
1587
35df4500 1588 if (!bp_location_has_shadow (bl))
c5aa993b 1589 continue;
6c95b8df 1590
9d497a19
PA
1591 one_breakpoint_xfer_memory (readbuf, writebuf, writebuf_org,
1592 memaddr, len, &bl->target_info, bl->gdbarch);
1593 }
c906108c 1594}
9d497a19 1595
f2478a7e 1596/* See breakpoint.h. */
b775012e 1597
f2478a7e 1598bool
b775012e
LM
1599is_breakpoint (const struct breakpoint *bpt)
1600{
1601 return (bpt->type == bp_breakpoint
e7e0cddf
SS
1602 || bpt->type == bp_hardware_breakpoint
1603 || bpt->type == bp_dprintf);
b775012e
LM
1604}
1605
60e1c644
PA
1606/* Return true if BPT is of any hardware watchpoint kind. */
1607
f2478a7e 1608static bool
d77f58be 1609is_hardware_watchpoint (const struct breakpoint *bpt)
a5606eee
VP
1610{
1611 return (bpt->type == bp_hardware_watchpoint
1612 || bpt->type == bp_read_watchpoint
1613 || bpt->type == bp_access_watchpoint);
1614}
7270d8f2 1615
f2478a7e 1616/* See breakpoint.h. */
60e1c644 1617
f2478a7e 1618bool
d77f58be 1619is_watchpoint (const struct breakpoint *bpt)
60e1c644
PA
1620{
1621 return (is_hardware_watchpoint (bpt)
1622 || bpt->type == bp_watchpoint);
1623}
1624
3a5c3e22
PA
1625/* Returns true if the current thread and its running state are safe
1626 to evaluate or update watchpoint B. Watchpoints on local
1627 expressions need to be evaluated in the context of the thread that
1628 was current when the watchpoint was created, and, that thread needs
1629 to be stopped to be able to select the correct frame context.
1630 Watchpoints on global expressions can be evaluated on any thread,
1631 and in any state. It is presently left to the target allowing
1632 memory accesses when threads are running. */
f6bc2008
PA
1633
1634static int
3a5c3e22 1635watchpoint_in_thread_scope (struct watchpoint *b)
f6bc2008 1636{
c1fc2657 1637 return (b->pspace == current_program_space
d7e15655
TT
1638 && (b->watchpoint_thread == null_ptid
1639 || (inferior_ptid == b->watchpoint_thread
00431a78 1640 && !inferior_thread ()->executing)));
f6bc2008
PA
1641}
1642
d0fb5eae
JK
1643/* Set watchpoint B to disp_del_at_next_stop, even including its possible
1644 associated bp_watchpoint_scope breakpoint. */
1645
1646static void
3a5c3e22 1647watchpoint_del_at_next_stop (struct watchpoint *w)
d0fb5eae 1648{
c1fc2657 1649 if (w->related_breakpoint != w)
d0fb5eae 1650 {
c1fc2657
SM
1651 gdb_assert (w->related_breakpoint->type == bp_watchpoint_scope);
1652 gdb_assert (w->related_breakpoint->related_breakpoint == w);
1653 w->related_breakpoint->disposition = disp_del_at_next_stop;
1654 w->related_breakpoint->related_breakpoint = w->related_breakpoint;
1655 w->related_breakpoint = w;
d0fb5eae 1656 }
c1fc2657 1657 w->disposition = disp_del_at_next_stop;
d0fb5eae
JK
1658}
1659
bb9d5f81
PP
1660/* Extract a bitfield value from value VAL using the bit parameters contained in
1661 watchpoint W. */
1662
1663static struct value *
1664extract_bitfield_from_watchpoint_value (struct watchpoint *w, struct value *val)
1665{
1666 struct value *bit_val;
1667
1668 if (val == NULL)
1669 return NULL;
1670
1671 bit_val = allocate_value (value_type (val));
1672
1673 unpack_value_bitfield (bit_val,
1674 w->val_bitpos,
1675 w->val_bitsize,
1676 value_contents_for_printing (val),
1677 value_offset (val),
1678 val);
1679
1680 return bit_val;
1681}
1682
c6d81124
PA
1683/* Allocate a dummy location and add it to B, which must be a software
1684 watchpoint. This is required because even if a software watchpoint
1685 is not watching any memory, bpstat_stop_status requires a location
1686 to be able to report stops. */
1687
1688static void
1689software_watchpoint_add_no_memory_location (struct breakpoint *b,
1690 struct program_space *pspace)
1691{
1692 gdb_assert (b->type == bp_watchpoint && b->loc == NULL);
1693
1694 b->loc = allocate_bp_location (b);
1695 b->loc->pspace = pspace;
1696 b->loc->address = -1;
1697 b->loc->length = -1;
1698}
1699
1700/* Returns true if B is a software watchpoint that is not watching any
1701 memory (e.g., "watch $pc"). */
1702
f2478a7e 1703static bool
c6d81124
PA
1704is_no_memory_software_watchpoint (struct breakpoint *b)
1705{
1706 return (b->type == bp_watchpoint
1707 && b->loc != NULL
1708 && b->loc->next == NULL
1709 && b->loc->address == -1
1710 && b->loc->length == -1);
1711}
1712
567e1b4e
JB
1713/* Assuming that B is a watchpoint:
1714 - Reparse watchpoint expression, if REPARSE is non-zero
a5606eee 1715 - Evaluate expression and store the result in B->val
567e1b4e
JB
1716 - Evaluate the condition if there is one, and store the result
1717 in b->loc->cond.
a5606eee
VP
1718 - Update the list of values that must be watched in B->loc.
1719
4a64f543
MS
1720 If the watchpoint disposition is disp_del_at_next_stop, then do
1721 nothing. If this is local watchpoint that is out of scope, delete
1722 it.
1723
1724 Even with `set breakpoint always-inserted on' the watchpoints are
1725 removed + inserted on each stop here. Normal breakpoints must
1726 never be removed because they might be missed by a running thread
1727 when debugging in non-stop mode. On the other hand, hardware
1728 watchpoints (is_hardware_watchpoint; processed here) are specific
1729 to each LWP since they are stored in each LWP's hardware debug
1730 registers. Therefore, such LWP must be stopped first in order to
1731 be able to modify its hardware watchpoints.
1732
1733 Hardware watchpoints must be reset exactly once after being
1734 presented to the user. It cannot be done sooner, because it would
1735 reset the data used to present the watchpoint hit to the user. And
1736 it must not be done later because it could display the same single
1737 watchpoint hit during multiple GDB stops. Note that the latter is
1738 relevant only to the hardware watchpoint types bp_read_watchpoint
1739 and bp_access_watchpoint. False hit by bp_hardware_watchpoint is
1740 not user-visible - its hit is suppressed if the memory content has
1741 not changed.
1742
1743 The following constraints influence the location where we can reset
1744 hardware watchpoints:
1745
1746 * target_stopped_by_watchpoint and target_stopped_data_address are
1747 called several times when GDB stops.
1748
1749 [linux]
1750 * Multiple hardware watchpoints can be hit at the same time,
1751 causing GDB to stop. GDB only presents one hardware watchpoint
1752 hit at a time as the reason for stopping, and all the other hits
1753 are presented later, one after the other, each time the user
1754 requests the execution to be resumed. Execution is not resumed
1755 for the threads still having pending hit event stored in
1756 LWP_INFO->STATUS. While the watchpoint is already removed from
1757 the inferior on the first stop the thread hit event is kept being
1758 reported from its cached value by linux_nat_stopped_data_address
1759 until the real thread resume happens after the watchpoint gets
1760 presented and thus its LWP_INFO->STATUS gets reset.
1761
1762 Therefore the hardware watchpoint hit can get safely reset on the
1763 watchpoint removal from inferior. */
a79d3c27 1764
b40ce68a 1765static void
3a5c3e22 1766update_watchpoint (struct watchpoint *b, int reparse)
7270d8f2 1767{
a5606eee 1768 int within_current_scope;
a5606eee 1769 struct frame_id saved_frame_id;
66076460 1770 int frame_saved;
a5606eee 1771
f6bc2008
PA
1772 /* If this is a local watchpoint, we only want to check if the
1773 watchpoint frame is in scope if the current thread is the thread
1774 that was used to create the watchpoint. */
1775 if (!watchpoint_in_thread_scope (b))
1776 return;
1777
c1fc2657 1778 if (b->disposition == disp_del_at_next_stop)
a5606eee
VP
1779 return;
1780
66076460 1781 frame_saved = 0;
a5606eee
VP
1782
1783 /* Determine if the watchpoint is within scope. */
1784 if (b->exp_valid_block == NULL)
1785 within_current_scope = 1;
1786 else
1787 {
b5db5dfc
UW
1788 struct frame_info *fi = get_current_frame ();
1789 struct gdbarch *frame_arch = get_frame_arch (fi);
1790 CORE_ADDR frame_pc = get_frame_pc (fi);
1791
c9cf6e20
MG
1792 /* If we're at a point where the stack has been destroyed
1793 (e.g. in a function epilogue), unwinding may not work
1794 properly. Do not attempt to recreate locations at this
b5db5dfc 1795 point. See similar comments in watchpoint_check. */
c9cf6e20 1796 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
b5db5dfc 1797 return;
66076460
DJ
1798
1799 /* Save the current frame's ID so we can restore it after
1800 evaluating the watchpoint expression on its own frame. */
1801 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
1802 took a frame parameter, so that we didn't have to change the
1803 selected frame. */
1804 frame_saved = 1;
1805 saved_frame_id = get_frame_id (get_selected_frame (NULL));
1806
a5606eee
VP
1807 fi = frame_find_by_id (b->watchpoint_frame);
1808 within_current_scope = (fi != NULL);
1809 if (within_current_scope)
1810 select_frame (fi);
1811 }
1812
b5db5dfc
UW
1813 /* We don't free locations. They are stored in the bp_location array
1814 and update_global_location_list will eventually delete them and
1815 remove breakpoints if needed. */
c1fc2657 1816 b->loc = NULL;
b5db5dfc 1817
a5606eee
VP
1818 if (within_current_scope && reparse)
1819 {
bbc13ae3 1820 const char *s;
d63d0675 1821
4d01a485 1822 b->exp.reset ();
d63d0675 1823 s = b->exp_string_reparse ? b->exp_string_reparse : b->exp_string;
1bb9788d 1824 b->exp = parse_exp_1 (&s, 0, b->exp_valid_block, 0);
a5606eee
VP
1825 /* If the meaning of expression itself changed, the old value is
1826 no longer relevant. We don't want to report a watchpoint hit
1827 to the user when the old value and the new value may actually
1828 be completely different objects. */
fa4727a6 1829 b->val = NULL;
4c1d86d9 1830 b->val_valid = false;
60e1c644
PA
1831
1832 /* Note that unlike with breakpoints, the watchpoint's condition
1833 expression is stored in the breakpoint object, not in the
1834 locations (re)created below. */
c1fc2657 1835 if (b->cond_string != NULL)
60e1c644 1836 {
4d01a485 1837 b->cond_exp.reset ();
60e1c644 1838
c1fc2657 1839 s = b->cond_string;
1bb9788d 1840 b->cond_exp = parse_exp_1 (&s, 0, b->cond_exp_valid_block, 0);
60e1c644 1841 }
a5606eee 1842 }
a5606eee
VP
1843
1844 /* If we failed to parse the expression, for example because
1845 it refers to a global variable in a not-yet-loaded shared library,
1846 don't try to insert watchpoint. We don't automatically delete
1847 such watchpoint, though, since failure to parse expression
1848 is different from out-of-scope watchpoint. */
55f6301a 1849 if (!target_has_execution ())
2d134ed3
PA
1850 {
1851 /* Without execution, memory can't change. No use to try and
1852 set watchpoint locations. The watchpoint will be reset when
1853 the target gains execution, through breakpoint_re_set. */
e8369a73
AB
1854 if (!can_use_hw_watchpoints)
1855 {
c1fc2657
SM
1856 if (b->ops->works_in_software_mode (b))
1857 b->type = bp_watchpoint;
e8369a73 1858 else
638aa5a1
AB
1859 error (_("Can't set read/access watchpoint when "
1860 "hardware watchpoints are disabled."));
e8369a73 1861 }
2d134ed3
PA
1862 }
1863 else if (within_current_scope && b->exp)
a5606eee 1864 {
0cf6dd15 1865 int pc = 0;
a6535de1 1866 std::vector<value_ref_ptr> val_chain;
8d49165d 1867 struct value *v, *result;
2d134ed3 1868 struct program_space *frame_pspace;
a5606eee 1869
4d01a485 1870 fetch_subexp_value (b->exp.get (), &pc, &v, &result, &val_chain, 0);
a5606eee 1871
a5606eee
VP
1872 /* Avoid setting b->val if it's already set. The meaning of
1873 b->val is 'the last value' user saw, and we should update
1874 it only if we reported that last value to user. As it
9c06b0b4
TJB
1875 happens, the code that reports it updates b->val directly.
1876 We don't keep track of the memory value for masked
1877 watchpoints. */
c1fc2657 1878 if (!b->val_valid && !is_masked_watchpoint (b))
fa4727a6 1879 {
bb9d5f81 1880 if (b->val_bitsize != 0)
850645cf
TT
1881 v = extract_bitfield_from_watchpoint_value (b, v);
1882 b->val = release_value (v);
4c1d86d9 1883 b->val_valid = true;
fa4727a6 1884 }
a5606eee 1885
2d134ed3
PA
1886 frame_pspace = get_frame_program_space (get_selected_frame (NULL));
1887
a5606eee 1888 /* Look at each value on the value chain. */
a6535de1
TT
1889 gdb_assert (!val_chain.empty ());
1890 for (const value_ref_ptr &iter : val_chain)
a5606eee 1891 {
a6535de1
TT
1892 v = iter.get ();
1893
a5606eee
VP
1894 /* If it's a memory location, and GDB actually needed
1895 its contents to evaluate the expression, then we
fa4727a6
DJ
1896 must watch it. If the first value returned is
1897 still lazy, that means an error occurred reading it;
1898 watch it anyway in case it becomes readable. */
a5606eee 1899 if (VALUE_LVAL (v) == lval_memory
a6535de1 1900 && (v == val_chain[0] || ! value_lazy (v)))
a5606eee
VP
1901 {
1902 struct type *vtype = check_typedef (value_type (v));
7270d8f2 1903
a5606eee
VP
1904 /* We only watch structs and arrays if user asked
1905 for it explicitly, never if they just happen to
1906 appear in the middle of some value chain. */
fa4727a6 1907 if (v == result
78134374
SM
1908 || (vtype->code () != TYPE_CODE_STRUCT
1909 && vtype->code () != TYPE_CODE_ARRAY))
a5606eee
VP
1910 {
1911 CORE_ADDR addr;
f486487f 1912 enum target_hw_bp_type type;
a5606eee 1913 struct bp_location *loc, **tmp;
bb9d5f81
PP
1914 int bitpos = 0, bitsize = 0;
1915
1916 if (value_bitsize (v) != 0)
1917 {
1918 /* Extract the bit parameters out from the bitfield
1919 sub-expression. */
1920 bitpos = value_bitpos (v);
1921 bitsize = value_bitsize (v);
1922 }
1923 else if (v == result && b->val_bitsize != 0)
1924 {
1925 /* If VAL_BITSIZE != 0 then RESULT is actually a bitfield
1926 lvalue whose bit parameters are saved in the fields
1927 VAL_BITPOS and VAL_BITSIZE. */
1928 bitpos = b->val_bitpos;
1929 bitsize = b->val_bitsize;
1930 }
a5606eee 1931
42ae5230 1932 addr = value_address (v);
bb9d5f81
PP
1933 if (bitsize != 0)
1934 {
1935 /* Skip the bytes that don't contain the bitfield. */
1936 addr += bitpos / 8;
1937 }
1938
a5606eee 1939 type = hw_write;
c1fc2657 1940 if (b->type == bp_read_watchpoint)
a5606eee 1941 type = hw_read;
c1fc2657 1942 else if (b->type == bp_access_watchpoint)
a5606eee 1943 type = hw_access;
3a5c3e22 1944
c1fc2657
SM
1945 loc = allocate_bp_location (b);
1946 for (tmp = &(b->loc); *tmp != NULL; tmp = &((*tmp)->next))
a5606eee
VP
1947 ;
1948 *tmp = loc;
a6d9a66e 1949 loc->gdbarch = get_type_arch (value_type (v));
6c95b8df
PA
1950
1951 loc->pspace = frame_pspace;
f17d9474 1952 loc->address = address_significant (loc->gdbarch, addr);
bb9d5f81
PP
1953
1954 if (bitsize != 0)
1955 {
1956 /* Just cover the bytes that make up the bitfield. */
1957 loc->length = ((bitpos % 8) + bitsize + 7) / 8;
1958 }
1959 else
1960 loc->length = TYPE_LENGTH (value_type (v));
1961
a5606eee
VP
1962 loc->watchpoint_type = type;
1963 }
1964 }
9fa40276
TJB
1965 }
1966
1967 /* Change the type of breakpoint between hardware assisted or
1968 an ordinary watchpoint depending on the hardware support
1969 and free hardware slots. REPARSE is set when the inferior
1970 is started. */
a9634178 1971 if (reparse)
9fa40276 1972 {
e09342b5 1973 int reg_cnt;
9fa40276
TJB
1974 enum bp_loc_type loc_type;
1975 struct bp_location *bl;
a5606eee 1976
a9634178 1977 reg_cnt = can_use_hardware_watchpoint (val_chain);
e09342b5
TJB
1978
1979 if (reg_cnt)
9fa40276
TJB
1980 {
1981 int i, target_resources_ok, other_type_used;
a1398e0c 1982 enum bptype type;
9fa40276 1983
a9634178
TJB
1984 /* Use an exact watchpoint when there's only one memory region to be
1985 watched, and only one debug register is needed to watch it. */
1986 b->exact = target_exact_watchpoints && reg_cnt == 1;
1987
9fa40276 1988 /* We need to determine how many resources are already
e09342b5
TJB
1989 used for all other hardware watchpoints plus this one
1990 to see if we still have enough resources to also fit
a1398e0c
PA
1991 this watchpoint in as well. */
1992
1993 /* If this is a software watchpoint, we try to turn it
1994 to a hardware one -- count resources as if B was of
1995 hardware watchpoint type. */
c1fc2657 1996 type = b->type;
a1398e0c
PA
1997 if (type == bp_watchpoint)
1998 type = bp_hardware_watchpoint;
1999
2000 /* This watchpoint may or may not have been placed on
2001 the list yet at this point (it won't be in the list
2002 if we're trying to create it for the first time,
2003 through watch_command), so always account for it
2004 manually. */
2005
2006 /* Count resources used by all watchpoints except B. */
c1fc2657 2007 i = hw_watchpoint_used_count_others (b, type, &other_type_used);
a1398e0c
PA
2008
2009 /* Add in the resources needed for B. */
c1fc2657 2010 i += hw_watchpoint_use_count (b);
a1398e0c
PA
2011
2012 target_resources_ok
2013 = target_can_use_hardware_watchpoint (type, i, other_type_used);
e09342b5 2014 if (target_resources_ok <= 0)
a9634178 2015 {
c1fc2657 2016 int sw_mode = b->ops->works_in_software_mode (b);
9c06b0b4
TJB
2017
2018 if (target_resources_ok == 0 && !sw_mode)
a9634178
TJB
2019 error (_("Target does not support this type of "
2020 "hardware watchpoint."));
9c06b0b4
TJB
2021 else if (target_resources_ok < 0 && !sw_mode)
2022 error (_("There are not enough available hardware "
2023 "resources for this watchpoint."));
a1398e0c
PA
2024
2025 /* Downgrade to software watchpoint. */
c1fc2657 2026 b->type = bp_watchpoint;
a1398e0c
PA
2027 }
2028 else
2029 {
2030 /* If this was a software watchpoint, we've just
2031 found we have enough resources to turn it to a
2032 hardware watchpoint. Otherwise, this is a
2033 nop. */
c1fc2657 2034 b->type = type;
a9634178 2035 }
9fa40276 2036 }
c1fc2657 2037 else if (!b->ops->works_in_software_mode (b))
638aa5a1
AB
2038 {
2039 if (!can_use_hw_watchpoints)
2040 error (_("Can't set read/access watchpoint when "
2041 "hardware watchpoints are disabled."));
2042 else
2043 error (_("Expression cannot be implemented with "
2044 "read/access watchpoint."));
2045 }
9fa40276 2046 else
c1fc2657 2047 b->type = bp_watchpoint;
9fa40276 2048
c1fc2657 2049 loc_type = (b->type == bp_watchpoint? bp_loc_other
9fa40276 2050 : bp_loc_hardware_watchpoint);
c1fc2657 2051 for (bl = b->loc; bl; bl = bl->next)
9fa40276
TJB
2052 bl->loc_type = loc_type;
2053 }
2054
c7437ca6
PA
2055 /* If a software watchpoint is not watching any memory, then the
2056 above left it without any location set up. But,
2057 bpstat_stop_status requires a location to be able to report
2058 stops, so make sure there's at least a dummy one. */
c1fc2657
SM
2059 if (b->type == bp_watchpoint && b->loc == NULL)
2060 software_watchpoint_add_no_memory_location (b, frame_pspace);
a5606eee
VP
2061 }
2062 else if (!within_current_scope)
7270d8f2 2063 {
ac74f770
MS
2064 printf_filtered (_("\
2065Watchpoint %d deleted because the program has left the block\n\
2066in which its expression is valid.\n"),
c1fc2657 2067 b->number);
d0fb5eae 2068 watchpoint_del_at_next_stop (b);
7270d8f2 2069 }
a5606eee
VP
2070
2071 /* Restore the selected frame. */
66076460
DJ
2072 if (frame_saved)
2073 select_frame (frame_find_by_id (saved_frame_id));
7270d8f2
OF
2074}
2075
a5606eee 2076
74960c60 2077/* Returns 1 iff breakpoint location should be
1e4d1764
YQ
2078 inserted in the inferior. We don't differentiate the type of BL's owner
2079 (breakpoint vs. tracepoint), although insert_location in tracepoint's
2080 breakpoint_ops is not defined, because in insert_bp_location,
2081 tracepoint's insert_location will not be called. */
74960c60 2082static int
35df4500 2083should_be_inserted (struct bp_location *bl)
74960c60 2084{
35df4500 2085 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
74960c60
VP
2086 return 0;
2087
35df4500 2088 if (bl->owner->disposition == disp_del_at_next_stop)
74960c60
VP
2089 return 0;
2090
b5fa468f
TBA
2091 if (!bl->enabled || bl->disabled_by_cond
2092 || bl->shlib_disabled || bl->duplicate)
74960c60
VP
2093 return 0;
2094
f8eba3c6
TT
2095 if (user_breakpoint_p (bl->owner) && bl->pspace->executing_startup)
2096 return 0;
2097
56710373
PA
2098 /* This is set for example, when we're attached to the parent of a
2099 vfork, and have detached from the child. The child is running
2100 free, and we expect it to do an exec or exit, at which point the
2101 OS makes the parent schedulable again (and the target reports
2102 that the vfork is done). Until the child is done with the shared
2103 memory region, do not insert breakpoints in the parent, otherwise
2104 the child could still trip on the parent's breakpoints. Since
2105 the parent is blocked anyway, it won't miss any breakpoint. */
35df4500 2106 if (bl->pspace->breakpoints_not_allowed)
56710373
PA
2107 return 0;
2108
31e77af2 2109 /* Don't insert a breakpoint if we're trying to step past its
21edc42f
YQ
2110 location, except if the breakpoint is a single-step breakpoint,
2111 and the breakpoint's thread is the thread which is stepping past
2112 a breakpoint. */
31e77af2
PA
2113 if ((bl->loc_type == bp_loc_software_breakpoint
2114 || bl->loc_type == bp_loc_hardware_breakpoint)
2115 && stepping_past_instruction_at (bl->pspace->aspace,
21edc42f
YQ
2116 bl->address)
2117 /* The single-step breakpoint may be inserted at the location
2118 we're trying to step if the instruction branches to itself.
2119 However, the instruction won't be executed at all and it may
2120 break the semantics of the instruction, for example, the
2121 instruction is a conditional branch or updates some flags.
2122 We can't fix it unless GDB is able to emulate the instruction
2123 or switch to displaced stepping. */
2124 && !(bl->owner->type == bp_single_step
2125 && thread_is_stepping_over_breakpoint (bl->owner->thread)))
e558d7c1 2126 {
1eb8556f
SM
2127 infrun_debug_printf ("skipping breakpoint: stepping past insn at: %s",
2128 paddress (bl->gdbarch, bl->address));
e558d7c1
PA
2129 return 0;
2130 }
31e77af2 2131
963f9c80
PA
2132 /* Don't insert watchpoints if we're trying to step past the
2133 instruction that triggered one. */
2134 if ((bl->loc_type == bp_loc_hardware_watchpoint)
2135 && stepping_past_nonsteppable_watchpoint ())
2136 {
1eb8556f
SM
2137 infrun_debug_printf ("stepping past non-steppable watchpoint. "
2138 "skipping watchpoint at %s:%d\n",
2139 paddress (bl->gdbarch, bl->address), bl->length);
963f9c80
PA
2140 return 0;
2141 }
2142
74960c60
VP
2143 return 1;
2144}
2145
934709f0
PW
2146/* Same as should_be_inserted but does the check assuming
2147 that the location is not duplicated. */
2148
2149static int
2150unduplicated_should_be_inserted (struct bp_location *bl)
2151{
2152 int result;
2153 const int save_duplicate = bl->duplicate;
2154
2155 bl->duplicate = 0;
2156 result = should_be_inserted (bl);
2157 bl->duplicate = save_duplicate;
2158 return result;
2159}
2160
b775012e
LM
2161/* Parses a conditional described by an expression COND into an
2162 agent expression bytecode suitable for evaluation
2163 by the bytecode interpreter. Return NULL if there was
2164 any error during parsing. */
2165
833177a4 2166static agent_expr_up
b775012e
LM
2167parse_cond_to_aexpr (CORE_ADDR scope, struct expression *cond)
2168{
833177a4 2169 if (cond == NULL)
b775012e
LM
2170 return NULL;
2171
833177a4
PA
2172 agent_expr_up aexpr;
2173
b775012e
LM
2174 /* We don't want to stop processing, so catch any errors
2175 that may show up. */
a70b8144 2176 try
b775012e 2177 {
036e657b 2178 aexpr = gen_eval_for_expr (scope, cond);
b775012e
LM
2179 }
2180
230d2906 2181 catch (const gdb_exception_error &ex)
b775012e
LM
2182 {
2183 /* If we got here, it means the condition could not be parsed to a valid
2184 bytecode expression and thus can't be evaluated on the target's side.
2185 It's no use iterating through the conditions. */
b775012e
LM
2186 }
2187
2188 /* We have a valid agent expression. */
2189 return aexpr;
2190}
2191
2192/* Based on location BL, create a list of breakpoint conditions to be
2193 passed on to the target. If we have duplicated locations with different
2194 conditions, we will add such conditions to the list. The idea is that the
2195 target will evaluate the list of conditions and will only notify GDB when
2196 one of them is true. */
2197
2198static void
2199build_target_condition_list (struct bp_location *bl)
2200{
2201 struct bp_location **locp = NULL, **loc2p;
2202 int null_condition_or_parse_error = 0;
2203 int modified = bl->needs_update;
2204 struct bp_location *loc;
2205
8b4f3082 2206 /* Release conditions left over from a previous insert. */
3cde5c42 2207 bl->target_info.conditions.clear ();
8b4f3082 2208
b775012e
LM
2209 /* This is only meaningful if the target is
2210 evaluating conditions and if the user has
2211 opted for condition evaluation on the target's
2212 side. */
2213 if (gdb_evaluates_breakpoint_condition_p ()
2214 || !target_supports_evaluation_of_breakpoint_conditions ())
2215 return;
2216
2217 /* Do a first pass to check for locations with no assigned
7f32a4d5
PA
2218 conditions or conditions that fail to parse to a valid agent
2219 expression bytecode. If any of these happen, then it's no use to
2220 send conditions to the target since this location will always
2221 trigger and generate a response back to GDB. Note we consider
2222 all locations at the same address irrespective of type, i.e.,
2223 even if the locations aren't considered duplicates (e.g.,
2224 software breakpoint and hardware breakpoint at the same
2225 address). */
b775012e
LM
2226 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2227 {
2228 loc = (*loc2p);
2229 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2230 {
2231 if (modified)
2232 {
b775012e
LM
2233 /* Re-parse the conditions since something changed. In that
2234 case we already freed the condition bytecodes (see
2235 force_breakpoint_reinsertion). We just
2236 need to parse the condition to bytecodes again. */
833177a4
PA
2237 loc->cond_bytecode = parse_cond_to_aexpr (bl->address,
2238 loc->cond.get ());
b775012e
LM
2239 }
2240
2241 /* If we have a NULL bytecode expression, it means something
2242 went wrong or we have a null condition expression. */
2243 if (!loc->cond_bytecode)
2244 {
2245 null_condition_or_parse_error = 1;
2246 break;
2247 }
2248 }
2249 }
2250
2251 /* If any of these happened, it means we will have to evaluate the conditions
2252 for the location's address on gdb's side. It is no use keeping bytecodes
2253 for all the other duplicate locations, thus we free all of them here.
2254
2255 This is so we have a finer control over which locations' conditions are
2256 being evaluated by GDB or the remote stub. */
2257 if (null_condition_or_parse_error)
2258 {
2259 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2260 {
2261 loc = (*loc2p);
2262 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2263 {
2264 /* Only go as far as the first NULL bytecode is
2265 located. */
2266 if (!loc->cond_bytecode)
2267 return;
2268
833177a4 2269 loc->cond_bytecode.reset ();
b775012e
LM
2270 }
2271 }
2272 }
2273
7f32a4d5
PA
2274 /* No NULL conditions or failed bytecode generation. Build a
2275 condition list for this location's address. If we have software
2276 and hardware locations at the same address, they aren't
2277 considered duplicates, but we still marge all the conditions
2278 anyway, as it's simpler, and doesn't really make a practical
2279 difference. */
b775012e
LM
2280 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2281 {
2282 loc = (*loc2p);
2283 if (loc->cond
2284 && is_breakpoint (loc->owner)
2285 && loc->pspace->num == bl->pspace->num
2286 && loc->owner->enable_state == bp_enabled
b5fa468f
TBA
2287 && loc->enabled
2288 && !loc->disabled_by_cond)
3cde5c42
PA
2289 {
2290 /* Add the condition to the vector. This will be used later
2291 to send the conditions to the target. */
2292 bl->target_info.conditions.push_back (loc->cond_bytecode.get ());
2293 }
b775012e
LM
2294 }
2295
2296 return;
2297}
2298
d3ce09f5
SS
2299/* Parses a command described by string CMD into an agent expression
2300 bytecode suitable for evaluation by the bytecode interpreter.
2301 Return NULL if there was any error during parsing. */
2302
833177a4 2303static agent_expr_up
d3ce09f5
SS
2304parse_cmd_to_aexpr (CORE_ADDR scope, char *cmd)
2305{
bbc13ae3
KS
2306 const char *cmdrest;
2307 const char *format_start, *format_end;
d3ce09f5
SS
2308 struct gdbarch *gdbarch = get_current_arch ();
2309
833177a4 2310 if (cmd == NULL)
d3ce09f5
SS
2311 return NULL;
2312
2313 cmdrest = cmd;
2314
2315 if (*cmdrest == ',')
2316 ++cmdrest;
f1735a53 2317 cmdrest = skip_spaces (cmdrest);
d3ce09f5
SS
2318
2319 if (*cmdrest++ != '"')
2320 error (_("No format string following the location"));
2321
2322 format_start = cmdrest;
2323
8e481c3b 2324 format_pieces fpieces (&cmdrest);
d3ce09f5
SS
2325
2326 format_end = cmdrest;
2327
2328 if (*cmdrest++ != '"')
2329 error (_("Bad format string, non-terminated '\"'."));
2330
f1735a53 2331 cmdrest = skip_spaces (cmdrest);
d3ce09f5
SS
2332
2333 if (!(*cmdrest == ',' || *cmdrest == '\0'))
2334 error (_("Invalid argument syntax"));
2335
2336 if (*cmdrest == ',')
2337 cmdrest++;
f1735a53 2338 cmdrest = skip_spaces (cmdrest);
d3ce09f5
SS
2339
2340 /* For each argument, make an expression. */
2341
8e481c3b 2342 std::vector<struct expression *> argvec;
d3ce09f5
SS
2343 while (*cmdrest != '\0')
2344 {
bbc13ae3 2345 const char *cmd1;
d3ce09f5
SS
2346
2347 cmd1 = cmdrest;
4d01a485 2348 expression_up expr = parse_exp_1 (&cmd1, scope, block_for_pc (scope), 1);
8e481c3b 2349 argvec.push_back (expr.release ());
d3ce09f5
SS
2350 cmdrest = cmd1;
2351 if (*cmdrest == ',')
2352 ++cmdrest;
2353 }
2354
833177a4
PA
2355 agent_expr_up aexpr;
2356
d3ce09f5
SS
2357 /* We don't want to stop processing, so catch any errors
2358 that may show up. */
a70b8144 2359 try
d3ce09f5 2360 {
036e657b
JB
2361 aexpr = gen_printf (scope, gdbarch, 0, 0,
2362 format_start, format_end - format_start,
8e481c3b 2363 argvec.size (), argvec.data ());
d3ce09f5 2364 }
230d2906 2365 catch (const gdb_exception_error &ex)
d3ce09f5
SS
2366 {
2367 /* If we got here, it means the command could not be parsed to a valid
2368 bytecode expression and thus can't be evaluated on the target's side.
2369 It's no use iterating through the other commands. */
d3ce09f5 2370 }
492d29ea 2371
d3ce09f5
SS
2372 /* We have a valid agent expression, return it. */
2373 return aexpr;
2374}
2375
2376/* Based on location BL, create a list of breakpoint commands to be
2377 passed on to the target. If we have duplicated locations with
2378 different commands, we will add any such to the list. */
2379
2380static void
2381build_target_command_list (struct bp_location *bl)
2382{
2383 struct bp_location **locp = NULL, **loc2p;
2384 int null_command_or_parse_error = 0;
2385 int modified = bl->needs_update;
2386 struct bp_location *loc;
2387
3cde5c42
PA
2388 /* Clear commands left over from a previous insert. */
2389 bl->target_info.tcommands.clear ();
8b4f3082 2390
41fac0cf 2391 if (!target_can_run_breakpoint_commands ())
d3ce09f5
SS
2392 return;
2393
41fac0cf
PA
2394 /* For now, limit to agent-style dprintf breakpoints. */
2395 if (dprintf_style != dprintf_style_agent)
d3ce09f5
SS
2396 return;
2397
7f32a4d5
PA
2398 /* For now, if we have any location at the same address that isn't a
2399 dprintf, don't install the target-side commands, as that would
2400 make the breakpoint not be reported to the core, and we'd lose
41fac0cf
PA
2401 control. */
2402 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2403 {
2404 loc = (*loc2p);
2405 if (is_breakpoint (loc->owner)
2406 && loc->pspace->num == bl->pspace->num
2407 && loc->owner->type != bp_dprintf)
2408 return;
2409 }
2410
d3ce09f5
SS
2411 /* Do a first pass to check for locations with no assigned
2412 conditions or conditions that fail to parse to a valid agent expression
2413 bytecode. If any of these happen, then it's no use to send conditions
2414 to the target since this location will always trigger and generate a
2415 response back to GDB. */
2416 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2417 {
2418 loc = (*loc2p);
2419 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2420 {
2421 if (modified)
2422 {
d3ce09f5
SS
2423 /* Re-parse the commands since something changed. In that
2424 case we already freed the command bytecodes (see
2425 force_breakpoint_reinsertion). We just
2426 need to parse the command to bytecodes again. */
833177a4
PA
2427 loc->cmd_bytecode
2428 = parse_cmd_to_aexpr (bl->address,
2429 loc->owner->extra_string);
d3ce09f5
SS
2430 }
2431
2432 /* If we have a NULL bytecode expression, it means something
2433 went wrong or we have a null command expression. */
2434 if (!loc->cmd_bytecode)
2435 {
2436 null_command_or_parse_error = 1;
2437 break;
2438 }
2439 }
2440 }
2441
2442 /* If anything failed, then we're not doing target-side commands,
2443 and so clean up. */
2444 if (null_command_or_parse_error)
2445 {
2446 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2447 {
2448 loc = (*loc2p);
2449 if (is_breakpoint (loc->owner)
2450 && loc->pspace->num == bl->pspace->num)
2451 {
2452 /* Only go as far as the first NULL bytecode is
2453 located. */
40fb6c5e 2454 if (loc->cmd_bytecode == NULL)
d3ce09f5
SS
2455 return;
2456
833177a4 2457 loc->cmd_bytecode.reset ();
d3ce09f5
SS
2458 }
2459 }
2460 }
2461
7f32a4d5
PA
2462 /* No NULL commands or failed bytecode generation. Build a command
2463 list for all duplicate locations at this location's address.
2464 Note that here we must care for whether the breakpoint location
2465 types are considered duplicates, otherwise, say, if we have a
2466 software and hardware location at the same address, the target
2467 could end up running the commands twice. For the moment, we only
2468 support targets-side commands with dprintf, but it doesn't hurt
2469 to be pedantically correct in case that changes. */
d3ce09f5
SS
2470 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2471 {
2472 loc = (*loc2p);
7f32a4d5
PA
2473 if (breakpoint_locations_match (bl, loc)
2474 && loc->owner->extra_string
d3ce09f5
SS
2475 && is_breakpoint (loc->owner)
2476 && loc->pspace->num == bl->pspace->num
2477 && loc->owner->enable_state == bp_enabled
b5fa468f
TBA
2478 && loc->enabled
2479 && !loc->disabled_by_cond)
3cde5c42
PA
2480 {
2481 /* Add the command to the vector. This will be used later
2482 to send the commands to the target. */
2483 bl->target_info.tcommands.push_back (loc->cmd_bytecode.get ());
2484 }
d3ce09f5
SS
2485 }
2486
2487 bl->target_info.persist = 0;
2488 /* Maybe flag this location as persistent. */
2489 if (bl->owner->type == bp_dprintf && disconnected_dprintf)
2490 bl->target_info.persist = 1;
2491}
2492
833b7ab5
YQ
2493/* Return the kind of breakpoint on address *ADDR. Get the kind
2494 of breakpoint according to ADDR except single-step breakpoint.
2495 Get the kind of single-step breakpoint according to the current
2496 registers state. */
cd6c3b4f
YQ
2497
2498static int
2499breakpoint_kind (struct bp_location *bl, CORE_ADDR *addr)
2500{
833b7ab5
YQ
2501 if (bl->owner->type == bp_single_step)
2502 {
2503 struct thread_info *thr = find_thread_global_id (bl->owner->thread);
2504 struct regcache *regcache;
2505
00431a78 2506 regcache = get_thread_regcache (thr);
833b7ab5
YQ
2507
2508 return gdbarch_breakpoint_kind_from_current_state (bl->gdbarch,
2509 regcache, addr);
2510 }
2511 else
2512 return gdbarch_breakpoint_kind_from_pc (bl->gdbarch, addr);
cd6c3b4f
YQ
2513}
2514
35df4500
TJB
2515/* Insert a low-level "breakpoint" of some type. BL is the breakpoint
2516 location. Any error messages are printed to TMP_ERROR_STREAM; and
3fbb6ffa 2517 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
c30eee59
TJB
2518 Returns 0 for success, 1 if the bp_location type is not supported or
2519 -1 for failure.
879bfdc2 2520
4a64f543
MS
2521 NOTE drow/2003-09-09: This routine could be broken down to an
2522 object-style method for each breakpoint or catchpoint type. */
26bb91f3 2523static int
35df4500 2524insert_bp_location (struct bp_location *bl,
26bb91f3 2525 struct ui_file *tmp_error_stream,
3fbb6ffa 2526 int *disabled_breaks,
dd61ec5c
MW
2527 int *hw_breakpoint_error,
2528 int *hw_bp_error_explained_already)
879bfdc2 2529{
cc06b668 2530 gdb_exception bp_excpt;
879bfdc2 2531
b775012e 2532 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
879bfdc2
DJ
2533 return 0;
2534
35c63cd8
JB
2535 /* Note we don't initialize bl->target_info, as that wipes out
2536 the breakpoint location's shadow_contents if the breakpoint
2537 is still inserted at that location. This in turn breaks
2538 target_read_memory which depends on these buffers when
2539 a memory read is requested at the breakpoint location:
2540 Once the target_info has been wiped, we fail to see that
2541 we have a breakpoint inserted at that address and thus
2542 read the breakpoint instead of returning the data saved in
2543 the breakpoint location's shadow contents. */
0d5ed153 2544 bl->target_info.reqstd_address = bl->address;
35df4500 2545 bl->target_info.placed_address_space = bl->pspace->aspace;
f1310107 2546 bl->target_info.length = bl->length;
8181d85f 2547
b775012e
LM
2548 /* When working with target-side conditions, we must pass all the conditions
2549 for the same breakpoint address down to the target since GDB will not
2550 insert those locations. With a list of breakpoint conditions, the target
2551 can decide when to stop and notify GDB. */
2552
2553 if (is_breakpoint (bl->owner))
2554 {
2555 build_target_condition_list (bl);
d3ce09f5
SS
2556 build_target_command_list (bl);
2557 /* Reset the modification marker. */
b775012e
LM
2558 bl->needs_update = 0;
2559 }
2560
7f32a4d5
PA
2561 /* If "set breakpoint auto-hw" is "on" and a software breakpoint was
2562 set at a read-only address, then a breakpoint location will have
2563 been changed to hardware breakpoint before we get here. If it is
2564 "off" however, error out before actually trying to insert the
2565 breakpoint, with a nicer error message. */
35df4500 2566 if (bl->loc_type == bp_loc_software_breakpoint
7f32a4d5 2567 && !automatic_hardware_breakpoints)
879bfdc2 2568 {
7f32a4d5 2569 mem_region *mr = lookup_mem_region (bl->address);
cc59ec59 2570
7f32a4d5
PA
2571 if (mr != nullptr && mr->attrib.mode != MEM_RW)
2572 {
2573 fprintf_unfiltered (tmp_error_stream,
2574 _("Cannot insert breakpoint %d.\n"
2575 "Cannot set software breakpoint "
2576 "at read-only address %s\n"),
2577 bl->owner->number,
2578 paddress (bl->gdbarch, bl->address));
2579 return 1;
765dc015 2580 }
7f32a4d5
PA
2581 }
2582
2583 if (bl->loc_type == bp_loc_software_breakpoint
2584 || bl->loc_type == bp_loc_hardware_breakpoint)
2585 {
879bfdc2
DJ
2586 /* First check to see if we have to handle an overlay. */
2587 if (overlay_debugging == ovly_off
35df4500
TJB
2588 || bl->section == NULL
2589 || !(section_is_overlay (bl->section)))
879bfdc2
DJ
2590 {
2591 /* No overlay handling: just set the breakpoint. */
a70b8144 2592 try
dd61ec5c 2593 {
0000e5cc
PA
2594 int val;
2595
dd61ec5c 2596 val = bl->owner->ops->insert_location (bl);
0000e5cc 2597 if (val)
688fca4f 2598 bp_excpt = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
dd61ec5c 2599 }
94aeb44b 2600 catch (gdb_exception &e)
dd61ec5c 2601 {
94aeb44b 2602 bp_excpt = std::move (e);
dd61ec5c 2603 }
879bfdc2
DJ
2604 }
2605 else
2606 {
4a64f543 2607 /* This breakpoint is in an overlay section.
879bfdc2
DJ
2608 Shall we set a breakpoint at the LMA? */
2609 if (!overlay_events_enabled)
2610 {
2611 /* Yes -- overlay event support is not active,
2612 so we must try to set a breakpoint at the LMA.
2613 This will not work for a hardware breakpoint. */
35df4500 2614 if (bl->loc_type == bp_loc_hardware_breakpoint)
8a3fe4f8 2615 warning (_("hardware breakpoint %d not supported in overlay!"),
35df4500 2616 bl->owner->number);
879bfdc2
DJ
2617 else
2618 {
35df4500
TJB
2619 CORE_ADDR addr = overlay_unmapped_address (bl->address,
2620 bl->section);
879bfdc2 2621 /* Set a software (trap) breakpoint at the LMA. */
35df4500 2622 bl->overlay_target_info = bl->target_info;
0d5ed153 2623 bl->overlay_target_info.reqstd_address = addr;
0000e5cc
PA
2624
2625 /* No overlay handling: just set the breakpoint. */
a70b8144 2626 try
0000e5cc
PA
2627 {
2628 int val;
2629
579c6ad9 2630 bl->overlay_target_info.kind
cd6c3b4f
YQ
2631 = breakpoint_kind (bl, &addr);
2632 bl->overlay_target_info.placed_address = addr;
0000e5cc
PA
2633 val = target_insert_breakpoint (bl->gdbarch,
2634 &bl->overlay_target_info);
2635 if (val)
688fca4f
PA
2636 bp_excpt
2637 = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
0000e5cc 2638 }
94aeb44b 2639 catch (gdb_exception &e)
0000e5cc 2640 {
94aeb44b 2641 bp_excpt = std::move (e);
0000e5cc
PA
2642 }
2643
688fca4f 2644 if (bp_excpt.reason != 0)
99361f52 2645 fprintf_unfiltered (tmp_error_stream,
3e43a32a
MS
2646 "Overlay breakpoint %d "
2647 "failed: in ROM?\n",
35df4500 2648 bl->owner->number);
879bfdc2
DJ
2649 }
2650 }
2651 /* Shall we set a breakpoint at the VMA? */
35df4500 2652 if (section_is_mapped (bl->section))
879bfdc2
DJ
2653 {
2654 /* Yes. This overlay section is mapped into memory. */
a70b8144 2655 try
dd61ec5c 2656 {
0000e5cc
PA
2657 int val;
2658
dd61ec5c 2659 val = bl->owner->ops->insert_location (bl);
0000e5cc 2660 if (val)
688fca4f 2661 bp_excpt = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
dd61ec5c 2662 }
94aeb44b 2663 catch (gdb_exception &e)
dd61ec5c 2664 {
94aeb44b 2665 bp_excpt = std::move (e);
dd61ec5c 2666 }
879bfdc2
DJ
2667 }
2668 else
2669 {
2670 /* No. This breakpoint will not be inserted.
2671 No error, but do not mark the bp as 'inserted'. */
2672 return 0;
2673 }
2674 }
2675
688fca4f 2676 if (bp_excpt.reason != 0)
879bfdc2
DJ
2677 {
2678 /* Can't set the breakpoint. */
0000e5cc
PA
2679
2680 /* In some cases, we might not be able to insert a
2681 breakpoint in a shared library that has already been
2682 removed, but we have not yet processed the shlib unload
2683 event. Unfortunately, some targets that implement
076855f9
PA
2684 breakpoint insertion themselves can't tell why the
2685 breakpoint insertion failed (e.g., the remote target
2686 doesn't define error codes), so we must treat generic
2687 errors as memory errors. */
688fca4f
PA
2688 if (bp_excpt.reason == RETURN_ERROR
2689 && (bp_excpt.error == GENERIC_ERROR
2690 || bp_excpt.error == MEMORY_ERROR)
076855f9 2691 && bl->loc_type == bp_loc_software_breakpoint
08351840 2692 && (solib_name_from_address (bl->pspace, bl->address)
d03de421
PA
2693 || shared_objfile_contains_address_p (bl->pspace,
2694 bl->address)))
879bfdc2 2695 {
4a64f543 2696 /* See also: disable_breakpoints_in_shlibs. */
35df4500 2697 bl->shlib_disabled = 1;
76727919 2698 gdb::observers::breakpoint_modified.notify (bl->owner);
3fbb6ffa
TJB
2699 if (!*disabled_breaks)
2700 {
2701 fprintf_unfiltered (tmp_error_stream,
2702 "Cannot insert breakpoint %d.\n",
2703 bl->owner->number);
2704 fprintf_unfiltered (tmp_error_stream,
2705 "Temporarily disabling shared "
2706 "library breakpoints:\n");
2707 }
2708 *disabled_breaks = 1;
879bfdc2 2709 fprintf_unfiltered (tmp_error_stream,
35df4500 2710 "breakpoint #%d\n", bl->owner->number);
0000e5cc 2711 return 0;
879bfdc2
DJ
2712 }
2713 else
879bfdc2 2714 {
35df4500 2715 if (bl->loc_type == bp_loc_hardware_breakpoint)
879bfdc2 2716 {
0000e5cc 2717 *hw_breakpoint_error = 1;
688fca4f 2718 *hw_bp_error_explained_already = bp_excpt.message != NULL;
dd61ec5c
MW
2719 fprintf_unfiltered (tmp_error_stream,
2720 "Cannot insert hardware breakpoint %d%s",
688fca4f
PA
2721 bl->owner->number,
2722 bp_excpt.message ? ":" : ".\n");
2723 if (bp_excpt.message != NULL)
2724 fprintf_unfiltered (tmp_error_stream, "%s.\n",
3d6e9d23 2725 bp_excpt.what ());
879bfdc2
DJ
2726 }
2727 else
2728 {
688fca4f 2729 if (bp_excpt.message == NULL)
0000e5cc 2730 {
1ccbe998 2731 std::string message
0000e5cc
PA
2732 = memory_error_message (TARGET_XFER_E_IO,
2733 bl->gdbarch, bl->address);
0000e5cc
PA
2734
2735 fprintf_unfiltered (tmp_error_stream,
2736 "Cannot insert breakpoint %d.\n"
2737 "%s\n",
1ccbe998 2738 bl->owner->number, message.c_str ());
0000e5cc
PA
2739 }
2740 else
2741 {
2742 fprintf_unfiltered (tmp_error_stream,
2743 "Cannot insert breakpoint %d: %s\n",
2744 bl->owner->number,
3d6e9d23 2745 bp_excpt.what ());
0000e5cc 2746 }
879bfdc2 2747 }
0000e5cc 2748 return 1;
879bfdc2
DJ
2749
2750 }
2751 }
2752 else
35df4500 2753 bl->inserted = 1;
879bfdc2 2754
0000e5cc 2755 return 0;
879bfdc2
DJ
2756 }
2757
35df4500 2758 else if (bl->loc_type == bp_loc_hardware_watchpoint
879bfdc2 2759 /* NOTE drow/2003-09-08: This state only exists for removing
4a64f543 2760 watchpoints. It's not clear that it's necessary... */
35df4500 2761 && bl->owner->disposition != disp_del_at_next_stop)
879bfdc2 2762 {
0000e5cc
PA
2763 int val;
2764
77b06cd7
TJB
2765 gdb_assert (bl->owner->ops != NULL
2766 && bl->owner->ops->insert_location != NULL);
2767
2768 val = bl->owner->ops->insert_location (bl);
85d721b8
PA
2769
2770 /* If trying to set a read-watchpoint, and it turns out it's not
2771 supported, try emulating one with an access watchpoint. */
35df4500 2772 if (val == 1 && bl->watchpoint_type == hw_read)
85d721b8
PA
2773 {
2774 struct bp_location *loc, **loc_temp;
2775
2776 /* But don't try to insert it, if there's already another
2777 hw_access location that would be considered a duplicate
2778 of this one. */
2779 ALL_BP_LOCATIONS (loc, loc_temp)
35df4500 2780 if (loc != bl
85d721b8 2781 && loc->watchpoint_type == hw_access
35df4500 2782 && watchpoint_locations_match (bl, loc))
85d721b8 2783 {
35df4500
TJB
2784 bl->duplicate = 1;
2785 bl->inserted = 1;
2786 bl->target_info = loc->target_info;
2787 bl->watchpoint_type = hw_access;
85d721b8
PA
2788 val = 0;
2789 break;
2790 }
2791
2792 if (val == 1)
2793 {
77b06cd7
TJB
2794 bl->watchpoint_type = hw_access;
2795 val = bl->owner->ops->insert_location (bl);
2796
2797 if (val)
2798 /* Back to the original value. */
2799 bl->watchpoint_type = hw_read;
85d721b8
PA
2800 }
2801 }
2802
35df4500 2803 bl->inserted = (val == 0);
879bfdc2
DJ
2804 }
2805
35df4500 2806 else if (bl->owner->type == bp_catchpoint)
879bfdc2 2807 {
0000e5cc
PA
2808 int val;
2809
77b06cd7
TJB
2810 gdb_assert (bl->owner->ops != NULL
2811 && bl->owner->ops->insert_location != NULL);
2812
2813 val = bl->owner->ops->insert_location (bl);
2814 if (val)
2815 {
2816 bl->owner->enable_state = bp_disabled;
2817
2818 if (val == 1)
2819 warning (_("\
2820Error inserting catchpoint %d: Your system does not support this type\n\
2821of catchpoint."), bl->owner->number);
2822 else
2823 warning (_("Error inserting catchpoint %d."), bl->owner->number);
2824 }
2825
2826 bl->inserted = (val == 0);
1640b821
DJ
2827
2828 /* We've already printed an error message if there was a problem
2829 inserting this catchpoint, and we've disabled the catchpoint,
2830 so just return success. */
2831 return 0;
879bfdc2
DJ
2832 }
2833
2834 return 0;
2835}
2836
6c95b8df
PA
2837/* This function is called when program space PSPACE is about to be
2838 deleted. It takes care of updating breakpoints to not reference
2839 PSPACE anymore. */
2840
2841void
2842breakpoint_program_space_exit (struct program_space *pspace)
2843{
2844 struct breakpoint *b, *b_temp;
876fa593 2845 struct bp_location *loc, **loc_temp;
6c95b8df
PA
2846
2847 /* Remove any breakpoint that was set through this program space. */
2848 ALL_BREAKPOINTS_SAFE (b, b_temp)
2849 {
2850 if (b->pspace == pspace)
2851 delete_breakpoint (b);
2852 }
2853
2854 /* Breakpoints set through other program spaces could have locations
2855 bound to PSPACE as well. Remove those. */
876fa593 2856 ALL_BP_LOCATIONS (loc, loc_temp)
6c95b8df
PA
2857 {
2858 struct bp_location *tmp;
2859
2860 if (loc->pspace == pspace)
2861 {
2bdf28a0 2862 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
6c95b8df
PA
2863 if (loc->owner->loc == loc)
2864 loc->owner->loc = loc->next;
2865 else
2866 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
2867 if (tmp->next == loc)
2868 {
2869 tmp->next = loc->next;
2870 break;
2871 }
2872 }
2873 }
2874
2875 /* Now update the global location list to permanently delete the
2876 removed locations above. */
44702360 2877 update_global_location_list (UGLL_DONT_INSERT);
6c95b8df
PA
2878}
2879
74960c60
VP
2880/* Make sure all breakpoints are inserted in inferior.
2881 Throws exception on any error.
2882 A breakpoint that is already inserted won't be inserted
2883 again, so calling this function twice is safe. */
2884void
2885insert_breakpoints (void)
2886{
2887 struct breakpoint *bpt;
2888
2889 ALL_BREAKPOINTS (bpt)
2890 if (is_hardware_watchpoint (bpt))
3a5c3e22
PA
2891 {
2892 struct watchpoint *w = (struct watchpoint *) bpt;
2893
2894 update_watchpoint (w, 0 /* don't reparse. */);
2895 }
74960c60 2896
04086b45
PA
2897 /* Updating watchpoints creates new locations, so update the global
2898 location list. Explicitly tell ugll to insert locations and
7f32a4d5
PA
2899 ignore breakpoints_always_inserted_mode. Also,
2900 update_global_location_list tries to "upgrade" software
2901 breakpoints to hardware breakpoints to handle "set breakpoint
2902 auto-hw", so we need to call it even if we don't have new
2903 locations. */
04086b45 2904 update_global_location_list (UGLL_INSERT);
74960c60
VP
2905}
2906
20388dd6
YQ
2907/* Invoke CALLBACK for each of bp_location. */
2908
2909void
2910iterate_over_bp_locations (walk_bp_location_callback callback)
2911{
2912 struct bp_location *loc, **loc_tmp;
2913
2914 ALL_BP_LOCATIONS (loc, loc_tmp)
2915 {
2916 callback (loc, NULL);
2917 }
2918}
2919
b775012e
LM
2920/* This is used when we need to synch breakpoint conditions between GDB and the
2921 target. It is the case with deleting and disabling of breakpoints when using
2922 always-inserted mode. */
2923
2924static void
2925update_inserted_breakpoint_locations (void)
2926{
2927 struct bp_location *bl, **blp_tmp;
2928 int error_flag = 0;
2929 int val = 0;
2930 int disabled_breaks = 0;
2931 int hw_breakpoint_error = 0;
dd61ec5c 2932 int hw_bp_details_reported = 0;
b775012e 2933
d7e74731 2934 string_file tmp_error_stream;
b775012e
LM
2935
2936 /* Explicitly mark the warning -- this will only be printed if
2937 there was an error. */
d7e74731 2938 tmp_error_stream.puts ("Warning:\n");
b775012e 2939
5ed8105e 2940 scoped_restore_current_pspace_and_thread restore_pspace_thread;
b775012e
LM
2941
2942 ALL_BP_LOCATIONS (bl, blp_tmp)
2943 {
2944 /* We only want to update software breakpoints and hardware
2945 breakpoints. */
2946 if (!is_breakpoint (bl->owner))
2947 continue;
2948
2949 /* We only want to update locations that are already inserted
2950 and need updating. This is to avoid unwanted insertion during
2951 deletion of breakpoints. */
4daf1902 2952 if (!bl->inserted || !bl->needs_update)
b775012e
LM
2953 continue;
2954
2955 switch_to_program_space_and_thread (bl->pspace);
2956
2957 /* For targets that support global breakpoints, there's no need
2958 to select an inferior to insert breakpoint to. In fact, even
2959 if we aren't attached to any process yet, we should still
2960 insert breakpoints. */
f5656ead 2961 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
55f6301a 2962 && (inferior_ptid == null_ptid || !target_has_execution ()))
b775012e
LM
2963 continue;
2964
d7e74731 2965 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
dd61ec5c 2966 &hw_breakpoint_error, &hw_bp_details_reported);
b775012e
LM
2967 if (val)
2968 error_flag = val;
2969 }
2970
2971 if (error_flag)
2972 {
223ffa71 2973 target_terminal::ours_for_output ();
b775012e
LM
2974 error_stream (tmp_error_stream);
2975 }
b775012e
LM
2976}
2977
c30eee59 2978/* Used when starting or continuing the program. */
c906108c 2979
74960c60
VP
2980static void
2981insert_breakpoint_locations (void)
c906108c 2982{
a5606eee 2983 struct breakpoint *bpt;
35df4500 2984 struct bp_location *bl, **blp_tmp;
eacd795a 2985 int error_flag = 0;
c906108c 2986 int val = 0;
3fbb6ffa 2987 int disabled_breaks = 0;
81d0cc19 2988 int hw_breakpoint_error = 0;
dd61ec5c 2989 int hw_bp_error_explained_already = 0;
c906108c 2990
d7e74731
PA
2991 string_file tmp_error_stream;
2992
81d0cc19
GS
2993 /* Explicitly mark the warning -- this will only be printed if
2994 there was an error. */
d7e74731 2995 tmp_error_stream.puts ("Warning:\n");
6c95b8df 2996
5ed8105e 2997 scoped_restore_current_pspace_and_thread restore_pspace_thread;
6c95b8df 2998
35df4500 2999 ALL_BP_LOCATIONS (bl, blp_tmp)
879bfdc2 3000 {
b775012e 3001 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
879bfdc2
DJ
3002 continue;
3003
4a64f543
MS
3004 /* There is no point inserting thread-specific breakpoints if
3005 the thread no longer exists. ALL_BP_LOCATIONS bp_location
3006 has BL->OWNER always non-NULL. */
35df4500 3007 if (bl->owner->thread != -1
5d5658a1 3008 && !valid_global_thread_id (bl->owner->thread))
f365de73
AS
3009 continue;
3010
35df4500 3011 switch_to_program_space_and_thread (bl->pspace);
6c95b8df
PA
3012
3013 /* For targets that support global breakpoints, there's no need
3014 to select an inferior to insert breakpoint to. In fact, even
3015 if we aren't attached to any process yet, we should still
3016 insert breakpoints. */
f5656ead 3017 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
55f6301a 3018 && (inferior_ptid == null_ptid || !target_has_execution ()))
6c95b8df
PA
3019 continue;
3020
d7e74731 3021 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
dd61ec5c 3022 &hw_breakpoint_error, &hw_bp_error_explained_already);
879bfdc2 3023 if (val)
eacd795a 3024 error_flag = val;
879bfdc2 3025 }
c906108c 3026
4a64f543
MS
3027 /* If we failed to insert all locations of a watchpoint, remove
3028 them, as half-inserted watchpoint is of limited use. */
a5606eee
VP
3029 ALL_BREAKPOINTS (bpt)
3030 {
3031 int some_failed = 0;
3032 struct bp_location *loc;
3033
3034 if (!is_hardware_watchpoint (bpt))
3035 continue;
3036
d6b74ac4 3037 if (!breakpoint_enabled (bpt))
a5606eee 3038 continue;
74960c60
VP
3039
3040 if (bpt->disposition == disp_del_at_next_stop)
3041 continue;
a5606eee
VP
3042
3043 for (loc = bpt->loc; loc; loc = loc->next)
56710373 3044 if (!loc->inserted && should_be_inserted (loc))
a5606eee
VP
3045 {
3046 some_failed = 1;
3047 break;
3048 }
3049 if (some_failed)
3050 {
3051 for (loc = bpt->loc; loc; loc = loc->next)
3052 if (loc->inserted)
834c0d03 3053 remove_breakpoint (loc);
a5606eee
VP
3054
3055 hw_breakpoint_error = 1;
d7e74731
PA
3056 tmp_error_stream.printf ("Could not insert "
3057 "hardware watchpoint %d.\n",
3058 bpt->number);
eacd795a 3059 error_flag = -1;
a5606eee
VP
3060 }
3061 }
3062
eacd795a 3063 if (error_flag)
81d0cc19
GS
3064 {
3065 /* If a hardware breakpoint or watchpoint was inserted, add a
3066 message about possibly exhausted resources. */
dd61ec5c 3067 if (hw_breakpoint_error && !hw_bp_error_explained_already)
81d0cc19 3068 {
d7e74731 3069 tmp_error_stream.printf ("Could not insert hardware breakpoints:\n\
c6510018 3070You may have requested too many hardware breakpoints/watchpoints.\n");
81d0cc19 3071 }
223ffa71 3072 target_terminal::ours_for_output ();
81d0cc19
GS
3073 error_stream (tmp_error_stream);
3074 }
c906108c
SS
3075}
3076
c30eee59
TJB
3077/* Used when the program stops.
3078 Returns zero if successful, or non-zero if there was a problem
3079 removing a breakpoint location. */
3080
c906108c 3081int
fba45db2 3082remove_breakpoints (void)
c906108c 3083{
35df4500 3084 struct bp_location *bl, **blp_tmp;
3a1bae8e 3085 int val = 0;
c906108c 3086
35df4500 3087 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3088 {
1e4d1764 3089 if (bl->inserted && !is_tracepoint (bl->owner))
834c0d03 3090 val |= remove_breakpoint (bl);
c5aa993b 3091 }
3a1bae8e 3092 return val;
c906108c
SS
3093}
3094
49fa26b0
PA
3095/* When a thread exits, remove breakpoints that are related to
3096 that thread. */
3097
3098static void
3099remove_threaded_breakpoints (struct thread_info *tp, int silent)
3100{
3101 struct breakpoint *b, *b_tmp;
3102
3103 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3104 {
5d5658a1 3105 if (b->thread == tp->global_num && user_breakpoint_p (b))
49fa26b0
PA
3106 {
3107 b->disposition = disp_del_at_next_stop;
3108
3109 printf_filtered (_("\
43792cf0
PA
3110Thread-specific breakpoint %d deleted - thread %s no longer in the thread list.\n"),
3111 b->number, print_thread_id (tp));
49fa26b0
PA
3112
3113 /* Hide it from the user. */
3114 b->number = 0;
3115 }
3116 }
3117}
3118
f3869b1a 3119/* See breakpoint.h. */
6c95b8df 3120
f3869b1a 3121void
00431a78 3122remove_breakpoints_inf (inferior *inf)
6c95b8df 3123{
35df4500 3124 struct bp_location *bl, **blp_tmp;
6c95b8df 3125 int val;
6c95b8df 3126
35df4500 3127 ALL_BP_LOCATIONS (bl, blp_tmp)
6c95b8df 3128 {
35df4500 3129 if (bl->pspace != inf->pspace)
6c95b8df
PA
3130 continue;
3131
fc126975 3132 if (bl->inserted && !bl->target_info.persist)
6c95b8df 3133 {
834c0d03 3134 val = remove_breakpoint (bl);
6c95b8df 3135 if (val != 0)
f3869b1a 3136 return;
6c95b8df
PA
3137 }
3138 }
6c95b8df
PA
3139}
3140
e58b0e63
PA
3141static int internal_breakpoint_number = -1;
3142
84f4c1fe
PM
3143/* Set the breakpoint number of B, depending on the value of INTERNAL.
3144 If INTERNAL is non-zero, the breakpoint number will be populated
3145 from internal_breakpoint_number and that variable decremented.
e5dd4106 3146 Otherwise the breakpoint number will be populated from
84f4c1fe
PM
3147 breakpoint_count and that value incremented. Internal breakpoints
3148 do not set the internal var bpnum. */
3149static void
3150set_breakpoint_number (int internal, struct breakpoint *b)
3151{
3152 if (internal)
3153 b->number = internal_breakpoint_number--;
3154 else
3155 {
3156 set_breakpoint_count (breakpoint_count + 1);
3157 b->number = breakpoint_count;
3158 }
3159}
3160
e62c965a 3161static struct breakpoint *
a6d9a66e 3162create_internal_breakpoint (struct gdbarch *gdbarch,
06edf0c0 3163 CORE_ADDR address, enum bptype type,
c0a91b2b 3164 const struct breakpoint_ops *ops)
e62c965a 3165{
51abb421 3166 symtab_and_line sal;
e62c965a
PP
3167 sal.pc = address;
3168 sal.section = find_pc_overlay (sal.pc);
6c95b8df 3169 sal.pspace = current_program_space;
e62c965a 3170
51abb421 3171 breakpoint *b = set_raw_breakpoint (gdbarch, sal, type, ops);
e62c965a
PP
3172 b->number = internal_breakpoint_number--;
3173 b->disposition = disp_donttouch;
3174
3175 return b;
3176}
3177
17450429
PP
3178static const char *const longjmp_names[] =
3179 {
3180 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
3181 };
3182#define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
3183
3184/* Per-objfile data private to breakpoint.c. */
3185struct breakpoint_objfile_data
3186{
3187 /* Minimal symbol for "_ovly_debug_event" (if any). */
43dce439 3188 struct bound_minimal_symbol overlay_msym {};
17450429
PP
3189
3190 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
43dce439 3191 struct bound_minimal_symbol longjmp_msym[NUM_LONGJMP_NAMES] {};
17450429 3192
28106bc2 3193 /* True if we have looked for longjmp probes. */
43dce439 3194 int longjmp_searched = 0;
28106bc2 3195
45461e0d
SM
3196 /* SystemTap probe points for longjmp (if any). These are non-owning
3197 references. */
3198 std::vector<probe *> longjmp_probes;
28106bc2 3199
17450429 3200 /* Minimal symbol for "std::terminate()" (if any). */
43dce439 3201 struct bound_minimal_symbol terminate_msym {};
17450429
PP
3202
3203 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
43dce439 3204 struct bound_minimal_symbol exception_msym {};
28106bc2
SDJ
3205
3206 /* True if we have looked for exception probes. */
43dce439 3207 int exception_searched = 0;
28106bc2 3208
45461e0d
SM
3209 /* SystemTap probe points for unwinding (if any). These are non-owning
3210 references. */
3211 std::vector<probe *> exception_probes;
17450429
PP
3212};
3213
51d3063a
TT
3214static const struct objfile_key<breakpoint_objfile_data>
3215 breakpoint_objfile_key;
17450429
PP
3216
3217/* Minimal symbol not found sentinel. */
3218static struct minimal_symbol msym_not_found;
3219
3220/* Returns TRUE if MSYM point to the "not found" sentinel. */
3221
3222static int
3223msym_not_found_p (const struct minimal_symbol *msym)
3224{
3225 return msym == &msym_not_found;
3226}
3227
3228/* Return per-objfile data needed by breakpoint.c.
3229 Allocate the data if necessary. */
3230
3231static struct breakpoint_objfile_data *
3232get_breakpoint_objfile_data (struct objfile *objfile)
3233{
3234 struct breakpoint_objfile_data *bp_objfile_data;
3235
51d3063a 3236 bp_objfile_data = breakpoint_objfile_key.get (objfile);
17450429 3237 if (bp_objfile_data == NULL)
51d3063a 3238 bp_objfile_data = breakpoint_objfile_key.emplace (objfile);
17450429
PP
3239 return bp_objfile_data;
3240}
3241
e62c965a 3242static void
af02033e 3243create_overlay_event_breakpoint (void)
e62c965a 3244{
af02033e 3245 const char *const func_name = "_ovly_debug_event";
e62c965a 3246
2030c079 3247 for (objfile *objfile : current_program_space->objfiles ())
69de3c6a
PP
3248 {
3249 struct breakpoint *b;
17450429
PP
3250 struct breakpoint_objfile_data *bp_objfile_data;
3251 CORE_ADDR addr;
67994074 3252 struct explicit_location explicit_loc;
69de3c6a 3253
17450429
PP
3254 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3255
3b7344d5 3256 if (msym_not_found_p (bp_objfile_data->overlay_msym.minsym))
17450429
PP
3257 continue;
3258
3b7344d5 3259 if (bp_objfile_data->overlay_msym.minsym == NULL)
17450429 3260 {
3b7344d5 3261 struct bound_minimal_symbol m;
17450429
PP
3262
3263 m = lookup_minimal_symbol_text (func_name, objfile);
3b7344d5 3264 if (m.minsym == NULL)
17450429
PP
3265 {
3266 /* Avoid future lookups in this objfile. */
3b7344d5 3267 bp_objfile_data->overlay_msym.minsym = &msym_not_found;
17450429
PP
3268 continue;
3269 }
3270 bp_objfile_data->overlay_msym = m;
3271 }
e62c965a 3272
77e371c0 3273 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->overlay_msym);
08feed99 3274 b = create_internal_breakpoint (objfile->arch (), addr,
06edf0c0
PA
3275 bp_overlay_event,
3276 &internal_breakpoint_ops);
67994074
KS
3277 initialize_explicit_location (&explicit_loc);
3278 explicit_loc.function_name = ASTRDUP (func_name);
d28cd78a 3279 b->location = new_explicit_location (&explicit_loc);
e62c965a 3280
69de3c6a
PP
3281 if (overlay_debugging == ovly_auto)
3282 {
3283 b->enable_state = bp_enabled;
3284 overlay_events_enabled = 1;
3285 }
3286 else
3287 {
3288 b->enable_state = bp_disabled;
3289 overlay_events_enabled = 0;
3290 }
e62c965a 3291 }
e62c965a
PP
3292}
3293
0fd8e87f 3294static void
af02033e 3295create_longjmp_master_breakpoint (void)
0fd8e87f 3296{
5ed8105e 3297 scoped_restore_current_program_space restore_pspace;
0fd8e87f 3298
94c93c35
TT
3299 for (struct program_space *pspace : program_spaces)
3300 {
3301 set_current_program_space (pspace);
af02033e 3302
94c93c35
TT
3303 for (objfile *objfile : current_program_space->objfiles ())
3304 {
3305 int i;
3306 struct gdbarch *gdbarch;
3307 struct breakpoint_objfile_data *bp_objfile_data;
0fd8e87f 3308
94c93c35 3309 gdbarch = objfile->arch ();
0fd8e87f 3310
94c93c35 3311 bp_objfile_data = get_breakpoint_objfile_data (objfile);
17450429 3312
94c93c35
TT
3313 if (!bp_objfile_data->longjmp_searched)
3314 {
3315 std::vector<probe *> ret
3316 = find_probes_in_objfile (objfile, "libc", "longjmp");
25f9533e 3317
94c93c35
TT
3318 if (!ret.empty ())
3319 {
3320 /* We are only interested in checking one element. */
3321 probe *p = ret[0];
aed57c53 3322
94c93c35
TT
3323 if (!p->can_evaluate_arguments ())
3324 {
3325 /* We cannot use the probe interface here,
3326 because it does not know how to evaluate
3327 arguments. */
3328 ret.clear ();
3329 }
3330 }
3331 bp_objfile_data->longjmp_probes = ret;
3332 bp_objfile_data->longjmp_searched = 1;
3333 }
25f9533e 3334
94c93c35
TT
3335 if (!bp_objfile_data->longjmp_probes.empty ())
3336 {
3337 for (probe *p : bp_objfile_data->longjmp_probes)
3338 {
3339 struct breakpoint *b;
3340
3341 b = create_internal_breakpoint (gdbarch,
3342 p->get_relocated_address (objfile),
3343 bp_longjmp_master,
3344 &internal_breakpoint_ops);
3345 b->location = new_probe_location ("-probe-stap libc:longjmp");
3346 b->enable_state = bp_disabled;
3347 }
28106bc2 3348
94c93c35
TT
3349 continue;
3350 }
28106bc2 3351
94c93c35
TT
3352 if (!gdbarch_get_longjmp_target_p (gdbarch))
3353 continue;
28106bc2 3354
94c93c35
TT
3355 for (i = 0; i < NUM_LONGJMP_NAMES; i++)
3356 {
3357 struct breakpoint *b;
3358 const char *func_name;
3359 CORE_ADDR addr;
3360 struct explicit_location explicit_loc;
0fd8e87f 3361
94c93c35
TT
3362 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i].minsym))
3363 continue;
17450429 3364
94c93c35
TT
3365 func_name = longjmp_names[i];
3366 if (bp_objfile_data->longjmp_msym[i].minsym == NULL)
3367 {
3368 struct bound_minimal_symbol m;
aed57c53 3369
94c93c35
TT
3370 m = lookup_minimal_symbol_text (func_name, objfile);
3371 if (m.minsym == NULL)
3372 {
3373 /* Prevent future lookups in this objfile. */
3374 bp_objfile_data->longjmp_msym[i].minsym = &msym_not_found;
3375 continue;
3376 }
3377 bp_objfile_data->longjmp_msym[i] = m;
3378 }
17450429 3379
94c93c35
TT
3380 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->longjmp_msym[i]);
3381 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master,
3382 &internal_breakpoint_ops);
3383 initialize_explicit_location (&explicit_loc);
3384 explicit_loc.function_name = ASTRDUP (func_name);
3385 b->location = new_explicit_location (&explicit_loc);
3386 b->enable_state = bp_disabled;
3387 }
3388 }
3389 }
0fd8e87f
UW
3390}
3391
af02033e 3392/* Create a master std::terminate breakpoint. */
aa7d318d 3393static void
af02033e 3394create_std_terminate_master_breakpoint (void)
aa7d318d 3395{
af02033e 3396 const char *const func_name = "std::terminate()";
aa7d318d 3397
5ed8105e 3398 scoped_restore_current_program_space restore_pspace;
aa7d318d 3399
94c93c35
TT
3400 for (struct program_space *pspace : program_spaces)
3401 {
3402 CORE_ADDR addr;
17450429 3403
94c93c35 3404 set_current_program_space (pspace);
17450429 3405
94c93c35
TT
3406 for (objfile *objfile : current_program_space->objfiles ())
3407 {
3408 struct breakpoint *b;
3409 struct breakpoint_objfile_data *bp_objfile_data;
3410 struct explicit_location explicit_loc;
aa7d318d 3411
94c93c35 3412 bp_objfile_data = get_breakpoint_objfile_data (objfile);
aa7d318d 3413
94c93c35
TT
3414 if (msym_not_found_p (bp_objfile_data->terminate_msym.minsym))
3415 continue;
17450429 3416
94c93c35
TT
3417 if (bp_objfile_data->terminate_msym.minsym == NULL)
3418 {
3419 struct bound_minimal_symbol m;
17450429 3420
94c93c35
TT
3421 m = lookup_minimal_symbol (func_name, NULL, objfile);
3422 if (m.minsym == NULL || (MSYMBOL_TYPE (m.minsym) != mst_text
3423 && MSYMBOL_TYPE (m.minsym) != mst_file_text))
3424 {
3425 /* Prevent future lookups in this objfile. */
3426 bp_objfile_data->terminate_msym.minsym = &msym_not_found;
3427 continue;
3428 }
3429 bp_objfile_data->terminate_msym = m;
3430 }
aa7d318d 3431
94c93c35
TT
3432 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->terminate_msym);
3433 b = create_internal_breakpoint (objfile->arch (), addr,
3434 bp_std_terminate_master,
3435 &internal_breakpoint_ops);
3436 initialize_explicit_location (&explicit_loc);
3437 explicit_loc.function_name = ASTRDUP (func_name);
3438 b->location = new_explicit_location (&explicit_loc);
3439 b->enable_state = bp_disabled;
3440 }
3441 }
aa7d318d
TT
3442}
3443
186c406b
TT
3444/* Install a master breakpoint on the unwinder's debug hook. */
3445
70221824 3446static void
186c406b
TT
3447create_exception_master_breakpoint (void)
3448{
17450429 3449 const char *const func_name = "_Unwind_DebugHook";
186c406b 3450
2030c079 3451 for (objfile *objfile : current_program_space->objfiles ())
186c406b 3452 {
17450429
PP
3453 struct breakpoint *b;
3454 struct gdbarch *gdbarch;
3455 struct breakpoint_objfile_data *bp_objfile_data;
3456 CORE_ADDR addr;
67994074 3457 struct explicit_location explicit_loc;
17450429
PP
3458
3459 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3460
28106bc2
SDJ
3461 /* We prefer the SystemTap probe point if it exists. */
3462 if (!bp_objfile_data->exception_searched)
3463 {
45461e0d
SM
3464 std::vector<probe *> ret
3465 = find_probes_in_objfile (objfile, "libgcc", "unwind");
25f9533e 3466
45461e0d 3467 if (!ret.empty ())
25f9533e
SDJ
3468 {
3469 /* We are only interested in checking one element. */
45461e0d 3470 probe *p = ret[0];
25f9533e 3471
935676c9 3472 if (!p->can_evaluate_arguments ())
25f9533e
SDJ
3473 {
3474 /* We cannot use the probe interface here, because it does
3475 not know how to evaluate arguments. */
45461e0d 3476 ret.clear ();
25f9533e
SDJ
3477 }
3478 }
3479 bp_objfile_data->exception_probes = ret;
28106bc2
SDJ
3480 bp_objfile_data->exception_searched = 1;
3481 }
3482
45461e0d 3483 if (!bp_objfile_data->exception_probes.empty ())
28106bc2 3484 {
08feed99 3485 gdbarch = objfile->arch ();
45461e0d
SM
3486
3487 for (probe *p : bp_objfile_data->exception_probes)
28106bc2 3488 {
729662a5 3489 b = create_internal_breakpoint (gdbarch,
935676c9 3490 p->get_relocated_address (objfile),
28106bc2
SDJ
3491 bp_exception_master,
3492 &internal_breakpoint_ops);
d28cd78a 3493 b->location = new_probe_location ("-probe-stap libgcc:unwind");
28106bc2
SDJ
3494 b->enable_state = bp_disabled;
3495 }
3496
3497 continue;
3498 }
3499
3500 /* Otherwise, try the hook function. */
3501
3b7344d5 3502 if (msym_not_found_p (bp_objfile_data->exception_msym.minsym))
17450429
PP
3503 continue;
3504
08feed99 3505 gdbarch = objfile->arch ();
186c406b 3506
3b7344d5 3507 if (bp_objfile_data->exception_msym.minsym == NULL)
186c406b 3508 {
3b7344d5 3509 struct bound_minimal_symbol debug_hook;
186c406b 3510
17450429 3511 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
3b7344d5 3512 if (debug_hook.minsym == NULL)
17450429 3513 {
3b7344d5 3514 bp_objfile_data->exception_msym.minsym = &msym_not_found;
17450429
PP
3515 continue;
3516 }
3517
3518 bp_objfile_data->exception_msym = debug_hook;
186c406b 3519 }
17450429 3520
77e371c0 3521 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->exception_msym);
8b88a78e
PA
3522 addr = gdbarch_convert_from_func_ptr_addr (gdbarch, addr,
3523 current_top_target ());
06edf0c0
PA
3524 b = create_internal_breakpoint (gdbarch, addr, bp_exception_master,
3525 &internal_breakpoint_ops);
67994074
KS
3526 initialize_explicit_location (&explicit_loc);
3527 explicit_loc.function_name = ASTRDUP (func_name);
d28cd78a 3528 b->location = new_explicit_location (&explicit_loc);
17450429 3529 b->enable_state = bp_disabled;
186c406b 3530 }
186c406b
TT
3531}
3532
9ef9e6a6
KS
3533/* Does B have a location spec? */
3534
3535static int
3536breakpoint_event_location_empty_p (const struct breakpoint *b)
3537{
d28cd78a 3538 return b->location != NULL && event_location_empty_p (b->location.get ());
9ef9e6a6
KS
3539}
3540
c906108c 3541void
fba45db2 3542update_breakpoints_after_exec (void)
c906108c 3543{
35df4500 3544 struct breakpoint *b, *b_tmp;
876fa593 3545 struct bp_location *bploc, **bplocp_tmp;
c906108c 3546
25b22b0a
PA
3547 /* We're about to delete breakpoints from GDB's lists. If the
3548 INSERTED flag is true, GDB will try to lift the breakpoints by
3549 writing the breakpoints' "shadow contents" back into memory. The
3550 "shadow contents" are NOT valid after an exec, so GDB should not
3551 do that. Instead, the target is responsible from marking
3552 breakpoints out as soon as it detects an exec. We don't do that
3553 here instead, because there may be other attempts to delete
3554 breakpoints after detecting an exec and before reaching here. */
876fa593 3555 ALL_BP_LOCATIONS (bploc, bplocp_tmp)
6c95b8df
PA
3556 if (bploc->pspace == current_program_space)
3557 gdb_assert (!bploc->inserted);
c906108c 3558
35df4500 3559 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 3560 {
6c95b8df
PA
3561 if (b->pspace != current_program_space)
3562 continue;
3563
4a64f543 3564 /* Solib breakpoints must be explicitly reset after an exec(). */
c5aa993b
JM
3565 if (b->type == bp_shlib_event)
3566 {
3567 delete_breakpoint (b);
3568 continue;
3569 }
c906108c 3570
4a64f543 3571 /* JIT breakpoints must be explicitly reset after an exec(). */
4efc6507
DE
3572 if (b->type == bp_jit_event)
3573 {
3574 delete_breakpoint (b);
3575 continue;
3576 }
3577
1900040c 3578 /* Thread event breakpoints must be set anew after an exec(),
0fd8e87f
UW
3579 as must overlay event and longjmp master breakpoints. */
3580 if (b->type == bp_thread_event || b->type == bp_overlay_event
186c406b
TT
3581 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
3582 || b->type == bp_exception_master)
c4093a6a
JM
3583 {
3584 delete_breakpoint (b);
3585 continue;
3586 }
3587
4a64f543 3588 /* Step-resume breakpoints are meaningless after an exec(). */
2c03e5be 3589 if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
c5aa993b
JM
3590 {
3591 delete_breakpoint (b);
3592 continue;
3593 }
3594
7c16b83e
PA
3595 /* Just like single-step breakpoints. */
3596 if (b->type == bp_single_step)
3597 {
3598 delete_breakpoint (b);
3599 continue;
3600 }
3601
611c83ae
PA
3602 /* Longjmp and longjmp-resume breakpoints are also meaningless
3603 after an exec. */
186c406b 3604 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
e2e4d78b 3605 || b->type == bp_longjmp_call_dummy
186c406b 3606 || b->type == bp_exception || b->type == bp_exception_resume)
611c83ae
PA
3607 {
3608 delete_breakpoint (b);
3609 continue;
3610 }
3611
ce78b96d
JB
3612 if (b->type == bp_catchpoint)
3613 {
3614 /* For now, none of the bp_catchpoint breakpoints need to
3615 do anything at this point. In the future, if some of
3616 the catchpoints need to something, we will need to add
3617 a new method, and call this method from here. */
3618 continue;
3619 }
3620
c5aa993b
JM
3621 /* bp_finish is a special case. The only way we ought to be able
3622 to see one of these when an exec() has happened, is if the user
3623 caught a vfork, and then said "finish". Ordinarily a finish just
3624 carries them to the call-site of the current callee, by setting
3625 a temporary bp there and resuming. But in this case, the finish
3626 will carry them entirely through the vfork & exec.
3627
3628 We don't want to allow a bp_finish to remain inserted now. But
3629 we can't safely delete it, 'cause finish_command has a handle to
3630 the bp on a bpstat, and will later want to delete it. There's a
3631 chance (and I've seen it happen) that if we delete the bp_finish
3632 here, that its storage will get reused by the time finish_command
3633 gets 'round to deleting the "use to be a bp_finish" breakpoint.
3634 We really must allow finish_command to delete a bp_finish.
3635
e5dd4106 3636 In the absence of a general solution for the "how do we know
53a5351d
JM
3637 it's safe to delete something others may have handles to?"
3638 problem, what we'll do here is just uninsert the bp_finish, and
3639 let finish_command delete it.
3640
3641 (We know the bp_finish is "doomed" in the sense that it's
3642 momentary, and will be deleted as soon as finish_command sees
3643 the inferior stopped. So it doesn't matter that the bp's
3644 address is probably bogus in the new a.out, unlike e.g., the
3645 solib breakpoints.) */
c5aa993b 3646
c5aa993b
JM
3647 if (b->type == bp_finish)
3648 {
3649 continue;
3650 }
3651
3652 /* Without a symbolic address, we have little hope of the
3653 pre-exec() address meaning the same thing in the post-exec()
4a64f543 3654 a.out. */
9ef9e6a6 3655 if (breakpoint_event_location_empty_p (b))
c5aa993b
JM
3656 {
3657 delete_breakpoint (b);
3658 continue;
3659 }
c5aa993b 3660 }
c906108c
SS
3661}
3662
3663int
d80ee84f 3664detach_breakpoints (ptid_t ptid)
c906108c 3665{
35df4500 3666 struct bp_location *bl, **blp_tmp;
3a1bae8e 3667 int val = 0;
2989a365 3668 scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid);
6c95b8df 3669 struct inferior *inf = current_inferior ();
c5aa993b 3670
e99b03dc 3671 if (ptid.pid () == inferior_ptid.pid ())
8a3fe4f8 3672 error (_("Cannot detach breakpoints of inferior_ptid"));
c5aa993b 3673
6c95b8df 3674 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
d80ee84f 3675 inferior_ptid = ptid;
35df4500 3676 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3677 {
35df4500 3678 if (bl->pspace != inf->pspace)
6c95b8df
PA
3679 continue;
3680
bd9673a4
PW
3681 /* This function must physically remove breakpoints locations
3682 from the specified ptid, without modifying the breakpoint
3683 package's state. Locations of type bp_loc_other are only
3684 maintained at GDB side. So, there is no need to remove
3685 these bp_loc_other locations. Moreover, removing these
3686 would modify the breakpoint package's state. */
3687 if (bl->loc_type == bp_loc_other)
3688 continue;
3689
35df4500 3690 if (bl->inserted)
b2b6a7da 3691 val |= remove_breakpoint_1 (bl, DETACH_BREAKPOINT);
c5aa993b 3692 }
d03285ec 3693
3a1bae8e 3694 return val;
c906108c
SS
3695}
3696
35df4500 3697/* Remove the breakpoint location BL from the current address space.
6c95b8df
PA
3698 Note that this is used to detach breakpoints from a child fork.
3699 When we get here, the child isn't in the inferior list, and neither
3700 do we have objects to represent its address space --- we should
35df4500 3701 *not* look at bl->pspace->aspace here. */
6c95b8df 3702
c906108c 3703static int
b2b6a7da 3704remove_breakpoint_1 (struct bp_location *bl, enum remove_bp_reason reason)
c906108c
SS
3705{
3706 int val;
c5aa993b 3707
35df4500
TJB
3708 /* BL is never in moribund_locations by our callers. */
3709 gdb_assert (bl->owner != NULL);
2bdf28a0 3710
74960c60
VP
3711 /* The type of none suggests that owner is actually deleted.
3712 This should not ever happen. */
35df4500 3713 gdb_assert (bl->owner->type != bp_none);
0bde7532 3714
35df4500
TJB
3715 if (bl->loc_type == bp_loc_software_breakpoint
3716 || bl->loc_type == bp_loc_hardware_breakpoint)
c906108c 3717 {
c02f5703
MS
3718 /* "Normal" instruction breakpoint: either the standard
3719 trap-instruction bp (bp_breakpoint), or a
3720 bp_hardware_breakpoint. */
3721
3722 /* First check to see if we have to handle an overlay. */
3723 if (overlay_debugging == ovly_off
35df4500
TJB
3724 || bl->section == NULL
3725 || !(section_is_overlay (bl->section)))
c02f5703
MS
3726 {
3727 /* No overlay handling: just remove the breakpoint. */
08351840
PA
3728
3729 /* If we're trying to uninsert a memory breakpoint that we
3730 know is set in a dynamic object that is marked
3731 shlib_disabled, then either the dynamic object was
3732 removed with "remove-symbol-file" or with
3733 "nosharedlibrary". In the former case, we don't know
3734 whether another dynamic object might have loaded over the
3735 breakpoint's address -- the user might well let us know
3736 about it next with add-symbol-file (the whole point of
d03de421 3737 add-symbol-file is letting the user manually maintain a
08351840
PA
3738 list of dynamically loaded objects). If we have the
3739 breakpoint's shadow memory, that is, this is a software
3740 breakpoint managed by GDB, check whether the breakpoint
3741 is still inserted in memory, to avoid overwriting wrong
3742 code with stale saved shadow contents. Note that HW
3743 breakpoints don't have shadow memory, as they're
3744 implemented using a mechanism that is not dependent on
3745 being able to modify the target's memory, and as such
3746 they should always be removed. */
3747 if (bl->shlib_disabled
3748 && bl->target_info.shadow_len != 0
3749 && !memory_validate_breakpoint (bl->gdbarch, &bl->target_info))
3750 val = 0;
3751 else
73971819 3752 val = bl->owner->ops->remove_location (bl, reason);
c02f5703 3753 }
c906108c
SS
3754 else
3755 {
4a64f543 3756 /* This breakpoint is in an overlay section.
c02f5703
MS
3757 Did we set a breakpoint at the LMA? */
3758 if (!overlay_events_enabled)
3759 {
3760 /* Yes -- overlay event support is not active, so we
3761 should have set a breakpoint at the LMA. Remove it.
3762 */
c02f5703
MS
3763 /* Ignore any failures: if the LMA is in ROM, we will
3764 have already warned when we failed to insert it. */
35df4500
TJB
3765 if (bl->loc_type == bp_loc_hardware_breakpoint)
3766 target_remove_hw_breakpoint (bl->gdbarch,
3767 &bl->overlay_target_info);
c02f5703 3768 else
35df4500 3769 target_remove_breakpoint (bl->gdbarch,
73971819
PA
3770 &bl->overlay_target_info,
3771 reason);
c02f5703
MS
3772 }
3773 /* Did we set a breakpoint at the VMA?
3774 If so, we will have marked the breakpoint 'inserted'. */
35df4500 3775 if (bl->inserted)
c906108c 3776 {
c02f5703
MS
3777 /* Yes -- remove it. Previously we did not bother to
3778 remove the breakpoint if the section had been
3779 unmapped, but let's not rely on that being safe. We
3780 don't know what the overlay manager might do. */
aa67235e
UW
3781
3782 /* However, we should remove *software* breakpoints only
3783 if the section is still mapped, or else we overwrite
3784 wrong code with the saved shadow contents. */
348d480f
PA
3785 if (bl->loc_type == bp_loc_hardware_breakpoint
3786 || section_is_mapped (bl->section))
73971819 3787 val = bl->owner->ops->remove_location (bl, reason);
aa67235e
UW
3788 else
3789 val = 0;
c906108c 3790 }
c02f5703
MS
3791 else
3792 {
3793 /* No -- not inserted, so no need to remove. No error. */
3794 val = 0;
3795 }
c906108c 3796 }
879d1e6b 3797
08351840
PA
3798 /* In some cases, we might not be able to remove a breakpoint in
3799 a shared library that has already been removed, but we have
3800 not yet processed the shlib unload event. Similarly for an
3801 unloaded add-symbol-file object - the user might not yet have
3802 had the chance to remove-symbol-file it. shlib_disabled will
3803 be set if the library/object has already been removed, but
3804 the breakpoint hasn't been uninserted yet, e.g., after
3805 "nosharedlibrary" or "remove-symbol-file" with breakpoints
3806 always-inserted mode. */
076855f9 3807 if (val
08351840
PA
3808 && (bl->loc_type == bp_loc_software_breakpoint
3809 && (bl->shlib_disabled
3810 || solib_name_from_address (bl->pspace, bl->address)
d03de421
PA
3811 || shared_objfile_contains_address_p (bl->pspace,
3812 bl->address))))
879d1e6b
UW
3813 val = 0;
3814
c906108c
SS
3815 if (val)
3816 return val;
b2b6a7da 3817 bl->inserted = (reason == DETACH_BREAKPOINT);
c906108c 3818 }
35df4500 3819 else if (bl->loc_type == bp_loc_hardware_watchpoint)
c906108c 3820 {
77b06cd7
TJB
3821 gdb_assert (bl->owner->ops != NULL
3822 && bl->owner->ops->remove_location != NULL);
3823
b2b6a7da 3824 bl->inserted = (reason == DETACH_BREAKPOINT);
73971819 3825 bl->owner->ops->remove_location (bl, reason);
2e70b7b9 3826
c906108c 3827 /* Failure to remove any of the hardware watchpoints comes here. */
b2b6a7da 3828 if (reason == REMOVE_BREAKPOINT && bl->inserted)
8a3fe4f8 3829 warning (_("Could not remove hardware watchpoint %d."),
35df4500 3830 bl->owner->number);
c906108c 3831 }
35df4500
TJB
3832 else if (bl->owner->type == bp_catchpoint
3833 && breakpoint_enabled (bl->owner)
3834 && !bl->duplicate)
ce78b96d 3835 {
77b06cd7
TJB
3836 gdb_assert (bl->owner->ops != NULL
3837 && bl->owner->ops->remove_location != NULL);
ce78b96d 3838
73971819 3839 val = bl->owner->ops->remove_location (bl, reason);
ce78b96d
JB
3840 if (val)
3841 return val;
77b06cd7 3842
b2b6a7da 3843 bl->inserted = (reason == DETACH_BREAKPOINT);
ce78b96d 3844 }
c906108c
SS
3845
3846 return 0;
3847}
3848
6c95b8df 3849static int
834c0d03 3850remove_breakpoint (struct bp_location *bl)
6c95b8df 3851{
35df4500
TJB
3852 /* BL is never in moribund_locations by our callers. */
3853 gdb_assert (bl->owner != NULL);
2bdf28a0 3854
6c95b8df
PA
3855 /* The type of none suggests that owner is actually deleted.
3856 This should not ever happen. */
35df4500 3857 gdb_assert (bl->owner->type != bp_none);
6c95b8df 3858
5ed8105e 3859 scoped_restore_current_pspace_and_thread restore_pspace_thread;
6c95b8df 3860
35df4500 3861 switch_to_program_space_and_thread (bl->pspace);
6c95b8df 3862
5ed8105e 3863 return remove_breakpoint_1 (bl, REMOVE_BREAKPOINT);
6c95b8df
PA
3864}
3865
c906108c
SS
3866/* Clear the "inserted" flag in all breakpoints. */
3867
25b22b0a 3868void
fba45db2 3869mark_breakpoints_out (void)
c906108c 3870{
35df4500 3871 struct bp_location *bl, **blp_tmp;
c906108c 3872
35df4500 3873 ALL_BP_LOCATIONS (bl, blp_tmp)
66c4b3e8 3874 if (bl->pspace == current_program_space)
35df4500 3875 bl->inserted = 0;
c906108c
SS
3876}
3877
53a5351d
JM
3878/* Clear the "inserted" flag in all breakpoints and delete any
3879 breakpoints which should go away between runs of the program.
c906108c
SS
3880
3881 Plus other such housekeeping that has to be done for breakpoints
3882 between runs.
3883
53a5351d
JM
3884 Note: this function gets called at the end of a run (by
3885 generic_mourn_inferior) and when a run begins (by
4a64f543 3886 init_wait_for_inferior). */
c906108c
SS
3887
3888
3889
3890void
fba45db2 3891breakpoint_init_inferior (enum inf_context context)
c906108c 3892{
35df4500 3893 struct breakpoint *b, *b_tmp;
6c95b8df 3894 struct program_space *pspace = current_program_space;
c906108c 3895
50c71eaf
PA
3896 /* If breakpoint locations are shared across processes, then there's
3897 nothing to do. */
f5656ead 3898 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
50c71eaf
PA
3899 return;
3900
1a853c52 3901 mark_breakpoints_out ();
075f6582 3902
35df4500 3903 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 3904 {
6c95b8df
PA
3905 if (b->loc && b->loc->pspace != pspace)
3906 continue;
3907
c5aa993b
JM
3908 switch (b->type)
3909 {
3910 case bp_call_dummy:
e2e4d78b 3911 case bp_longjmp_call_dummy:
c906108c 3912
c5aa993b 3913 /* If the call dummy breakpoint is at the entry point it will
ab92d69b
PA
3914 cause problems when the inferior is rerun, so we better get
3915 rid of it. */
3916
3917 case bp_watchpoint_scope:
3918
3919 /* Also get rid of scope breakpoints. */
3920
3921 case bp_shlib_event:
3922
3923 /* Also remove solib event breakpoints. Their addresses may
3924 have changed since the last time we ran the program.
3925 Actually we may now be debugging against different target;
3926 and so the solib backend that installed this breakpoint may
3927 not be used in by the target. E.g.,
3928
3929 (gdb) file prog-linux
3930 (gdb) run # native linux target
3931 ...
3932 (gdb) kill
3933 (gdb) file prog-win.exe
3934 (gdb) tar rem :9999 # remote Windows gdbserver.
3935 */
c906108c 3936
f59f708a
PA
3937 case bp_step_resume:
3938
3939 /* Also remove step-resume breakpoints. */
3940
7c16b83e
PA
3941 case bp_single_step:
3942
3943 /* Also remove single-step breakpoints. */
3944
c5aa993b
JM
3945 delete_breakpoint (b);
3946 break;
c906108c 3947
c5aa993b
JM
3948 case bp_watchpoint:
3949 case bp_hardware_watchpoint:
3950 case bp_read_watchpoint:
3951 case bp_access_watchpoint:
3a5c3e22
PA
3952 {
3953 struct watchpoint *w = (struct watchpoint *) b;
c906108c 3954
3a5c3e22
PA
3955 /* Likewise for watchpoints on local expressions. */
3956 if (w->exp_valid_block != NULL)
3957 delete_breakpoint (b);
63000888 3958 else
3a5c3e22 3959 {
63000888
PA
3960 /* Get rid of existing locations, which are no longer
3961 valid. New ones will be created in
3962 update_watchpoint, when the inferior is restarted.
3963 The next update_global_location_list call will
3964 garbage collect them. */
3965 b->loc = NULL;
3966
3967 if (context == inf_starting)
3968 {
3969 /* Reset val field to force reread of starting value in
3970 insert_breakpoints. */
850645cf 3971 w->val.reset (nullptr);
4c1d86d9 3972 w->val_valid = false;
63000888
PA
3973 }
3974 }
3a5c3e22 3975 }
c5aa993b
JM
3976 break;
3977 default:
c5aa993b
JM
3978 break;
3979 }
3980 }
1c5cfe86
PA
3981
3982 /* Get rid of the moribund locations. */
1123588c 3983 for (bp_location *bl : moribund_locations)
35df4500 3984 decref_bp_location (&bl);
1123588c 3985 moribund_locations.clear ();
c906108c
SS
3986}
3987
6c95b8df
PA
3988/* These functions concern about actual breakpoints inserted in the
3989 target --- to e.g. check if we need to do decr_pc adjustment or if
3990 we need to hop over the bkpt --- so we check for address space
3991 match, not program space. */
3992
c2c6d25f
JM
3993/* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
3994 exists at PC. It returns ordinary_breakpoint_here if it's an
3995 ordinary breakpoint, or permanent_breakpoint_here if it's a
3996 permanent breakpoint.
3997 - When continuing from a location with an ordinary breakpoint, we
3998 actually single step once before calling insert_breakpoints.
e5dd4106 3999 - When continuing from a location with a permanent breakpoint, we
c2c6d25f
JM
4000 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
4001 the target, to advance the PC past the breakpoint. */
c906108c 4002
c2c6d25f 4003enum breakpoint_here
accd0bcd 4004breakpoint_here_p (const address_space *aspace, CORE_ADDR pc)
c906108c 4005{
35df4500 4006 struct bp_location *bl, **blp_tmp;
c2c6d25f 4007 int any_breakpoint_here = 0;
c906108c 4008
35df4500 4009 ALL_BP_LOCATIONS (bl, blp_tmp)
075f6582 4010 {
35df4500
TJB
4011 if (bl->loc_type != bp_loc_software_breakpoint
4012 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
4013 continue;
4014
f1310107 4015 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
35df4500 4016 if ((breakpoint_enabled (bl->owner)
1a853c52 4017 || bl->permanent)
f1310107 4018 && breakpoint_location_address_match (bl, aspace, pc))
075f6582
DJ
4019 {
4020 if (overlay_debugging
35df4500
TJB
4021 && section_is_overlay (bl->section)
4022 && !section_is_mapped (bl->section))
075f6582 4023 continue; /* unmapped overlay -- can't be a match */
1a853c52 4024 else if (bl->permanent)
075f6582
DJ
4025 return permanent_breakpoint_here;
4026 else
4027 any_breakpoint_here = 1;
4028 }
4029 }
c906108c 4030
f486487f 4031 return any_breakpoint_here ? ordinary_breakpoint_here : no_breakpoint_here;
c906108c
SS
4032}
4033
d35ae833
PA
4034/* See breakpoint.h. */
4035
4036int
accd0bcd 4037breakpoint_in_range_p (const address_space *aspace,
d35ae833
PA
4038 CORE_ADDR addr, ULONGEST len)
4039{
4040 struct bp_location *bl, **blp_tmp;
4041
4042 ALL_BP_LOCATIONS (bl, blp_tmp)
4043 {
4044 if (bl->loc_type != bp_loc_software_breakpoint
4045 && bl->loc_type != bp_loc_hardware_breakpoint)
4046 continue;
4047
4048 if ((breakpoint_enabled (bl->owner)
4049 || bl->permanent)
4050 && breakpoint_location_address_range_overlap (bl, aspace,
4051 addr, len))
4052 {
4053 if (overlay_debugging
4054 && section_is_overlay (bl->section)
4055 && !section_is_mapped (bl->section))
4056 {
4057 /* Unmapped overlay -- can't be a match. */
4058 continue;
4059 }
4060
4061 return 1;
4062 }
4063 }
4064
4065 return 0;
4066}
4067
1c5cfe86
PA
4068/* Return true if there's a moribund breakpoint at PC. */
4069
4070int
accd0bcd 4071moribund_breakpoint_here_p (const address_space *aspace, CORE_ADDR pc)
1c5cfe86 4072{
1123588c 4073 for (bp_location *loc : moribund_locations)
f1310107 4074 if (breakpoint_location_address_match (loc, aspace, pc))
1c5cfe86
PA
4075 return 1;
4076
4077 return 0;
4078}
c2c6d25f 4079
f7ce857f
PA
4080/* Returns non-zero iff BL is inserted at PC, in address space
4081 ASPACE. */
4082
4083static int
4084bp_location_inserted_here_p (struct bp_location *bl,
accd0bcd 4085 const address_space *aspace, CORE_ADDR pc)
f7ce857f
PA
4086{
4087 if (bl->inserted
4088 && breakpoint_address_match (bl->pspace->aspace, bl->address,
4089 aspace, pc))
4090 {
4091 if (overlay_debugging
4092 && section_is_overlay (bl->section)
4093 && !section_is_mapped (bl->section))
4094 return 0; /* unmapped overlay -- can't be a match */
4095 else
4096 return 1;
4097 }
4098 return 0;
4099}
4100
a1fd2fa5 4101/* Returns non-zero iff there's a breakpoint inserted at PC. */
c906108c
SS
4102
4103int
accd0bcd 4104breakpoint_inserted_here_p (const address_space *aspace, CORE_ADDR pc)
c906108c 4105{
f7ce857f 4106 struct bp_location **blp, **blp_tmp = NULL;
c906108c 4107
f7ce857f 4108 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
c5aa993b 4109 {
f7ce857f
PA
4110 struct bp_location *bl = *blp;
4111
35df4500
TJB
4112 if (bl->loc_type != bp_loc_software_breakpoint
4113 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
4114 continue;
4115
f7ce857f
PA
4116 if (bp_location_inserted_here_p (bl, aspace, pc))
4117 return 1;
c5aa993b 4118 }
c36b740a
VP
4119 return 0;
4120}
4121
a1fd2fa5
PA
4122/* This function returns non-zero iff there is a software breakpoint
4123 inserted at PC. */
c36b740a
VP
4124
4125int
accd0bcd 4126software_breakpoint_inserted_here_p (const address_space *aspace,
a1fd2fa5 4127 CORE_ADDR pc)
4fa8626c 4128{
f7ce857f 4129 struct bp_location **blp, **blp_tmp = NULL;
4fa8626c 4130
f7ce857f 4131 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4fa8626c 4132 {
f7ce857f
PA
4133 struct bp_location *bl = *blp;
4134
35df4500 4135 if (bl->loc_type != bp_loc_software_breakpoint)
4fa8626c
DJ
4136 continue;
4137
f7ce857f
PA
4138 if (bp_location_inserted_here_p (bl, aspace, pc))
4139 return 1;
4fa8626c
DJ
4140 }
4141
4142 return 0;
9c02b525
PA
4143}
4144
4145/* See breakpoint.h. */
4146
4147int
accd0bcd 4148hardware_breakpoint_inserted_here_p (const address_space *aspace,
9c02b525
PA
4149 CORE_ADDR pc)
4150{
4151 struct bp_location **blp, **blp_tmp = NULL;
9c02b525
PA
4152
4153 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4154 {
4155 struct bp_location *bl = *blp;
4156
4157 if (bl->loc_type != bp_loc_hardware_breakpoint)
4158 continue;
4159
4160 if (bp_location_inserted_here_p (bl, aspace, pc))
4161 return 1;
4162 }
4163
4164 return 0;
4fa8626c
DJ
4165}
4166
9093389c 4167int
accd0bcd 4168hardware_watchpoint_inserted_in_range (const address_space *aspace,
9093389c
PA
4169 CORE_ADDR addr, ULONGEST len)
4170{
4171 struct breakpoint *bpt;
4172
4173 ALL_BREAKPOINTS (bpt)
4174 {
4175 struct bp_location *loc;
4176
4177 if (bpt->type != bp_hardware_watchpoint
4178 && bpt->type != bp_access_watchpoint)
4179 continue;
4180
4181 if (!breakpoint_enabled (bpt))
4182 continue;
4183
4184 for (loc = bpt->loc; loc; loc = loc->next)
4185 if (loc->pspace->aspace == aspace && loc->inserted)
4186 {
4187 CORE_ADDR l, h;
4188
4189 /* Check for intersection. */
768adc05
PA
4190 l = std::max<CORE_ADDR> (loc->address, addr);
4191 h = std::min<CORE_ADDR> (loc->address + loc->length, addr + len);
9093389c
PA
4192 if (l < h)
4193 return 1;
4194 }
4195 }
4196 return 0;
4197}
c5aa993b 4198
f2478a7e 4199/* See breakpoint.h. */
c906108c 4200
f2478a7e
SM
4201bool
4202is_catchpoint (struct breakpoint *b)
c906108c 4203{
f2478a7e 4204 return (b->type == bp_catchpoint);
c906108c
SS
4205}
4206
f431efe5
PA
4207/* Frees any storage that is part of a bpstat. Does not walk the
4208 'next' chain. */
4209
04afa70c 4210bpstats::~bpstats ()
198757a8 4211{
04afa70c
TT
4212 if (bp_location_at != NULL)
4213 decref_bp_location (&bp_location_at);
198757a8
VP
4214}
4215
c906108c
SS
4216/* Clear a bpstat so that it says we are not at any breakpoint.
4217 Also free any storage that is part of a bpstat. */
4218
4219void
fba45db2 4220bpstat_clear (bpstat *bsp)
c906108c
SS
4221{
4222 bpstat p;
4223 bpstat q;
4224
4225 if (bsp == 0)
4226 return;
4227 p = *bsp;
4228 while (p != NULL)
4229 {
4230 q = p->next;
04afa70c 4231 delete p;
c906108c
SS
4232 p = q;
4233 }
4234 *bsp = NULL;
4235}
4236
04afa70c
TT
4237bpstats::bpstats (const bpstats &other)
4238 : next (NULL),
4239 bp_location_at (other.bp_location_at),
4240 breakpoint_at (other.breakpoint_at),
4241 commands (other.commands),
04afa70c
TT
4242 print (other.print),
4243 stop (other.stop),
4244 print_it (other.print_it)
4245{
850645cf
TT
4246 if (other.old_val != NULL)
4247 old_val = release_value (value_copy (other.old_val.get ()));
04afa70c 4248 incref_bp_location (bp_location_at);
04afa70c
TT
4249}
4250
c906108c
SS
4251/* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
4252 is part of the bpstat is copied as well. */
4253
4254bpstat
fba45db2 4255bpstat_copy (bpstat bs)
c906108c
SS
4256{
4257 bpstat p = NULL;
4258 bpstat tmp;
4259 bpstat retval = NULL;
4260
4261 if (bs == NULL)
4262 return bs;
4263
4264 for (; bs != NULL; bs = bs->next)
4265 {
04afa70c 4266 tmp = new bpstats (*bs);
31cc81e9 4267
c906108c
SS
4268 if (p == NULL)
4269 /* This is the first thing in the chain. */
4270 retval = tmp;
4271 else
4272 p->next = tmp;
4273 p = tmp;
4274 }
4275 p->next = NULL;
4276 return retval;
4277}
4278
4a64f543 4279/* Find the bpstat associated with this breakpoint. */
c906108c
SS
4280
4281bpstat
fba45db2 4282bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
c906108c 4283{
c5aa993b
JM
4284 if (bsp == NULL)
4285 return NULL;
c906108c 4286
c5aa993b
JM
4287 for (; bsp != NULL; bsp = bsp->next)
4288 {
f431efe5 4289 if (bsp->breakpoint_at == breakpoint)
c5aa993b
JM
4290 return bsp;
4291 }
c906108c
SS
4292 return NULL;
4293}
4294
ab04a2af
TT
4295/* See breakpoint.h. */
4296
4c462cb0 4297bool
427cd150 4298bpstat_explains_signal (bpstat bsp, enum gdb_signal sig)
ab04a2af 4299{
ab04a2af
TT
4300 for (; bsp != NULL; bsp = bsp->next)
4301 {
427cd150
TT
4302 if (bsp->breakpoint_at == NULL)
4303 {
4304 /* A moribund location can never explain a signal other than
4305 GDB_SIGNAL_TRAP. */
4306 if (sig == GDB_SIGNAL_TRAP)
4c462cb0 4307 return true;
427cd150
TT
4308 }
4309 else
47591c29
PA
4310 {
4311 if (bsp->breakpoint_at->ops->explains_signal (bsp->breakpoint_at,
4312 sig))
4c462cb0 4313 return true;
47591c29 4314 }
ab04a2af
TT
4315 }
4316
4c462cb0 4317 return false;
ab04a2af
TT
4318}
4319
4a64f543
MS
4320/* Put in *NUM the breakpoint number of the first breakpoint we are
4321 stopped at. *BSP upon return is a bpstat which points to the
4322 remaining breakpoints stopped at (but which is not guaranteed to be
4323 good for anything but further calls to bpstat_num).
4324
8671a17b
PA
4325 Return 0 if passed a bpstat which does not indicate any breakpoints.
4326 Return -1 if stopped at a breakpoint that has been deleted since
4327 we set it.
4328 Return 1 otherwise. */
c906108c
SS
4329
4330int
8671a17b 4331bpstat_num (bpstat *bsp, int *num)
c906108c
SS
4332{
4333 struct breakpoint *b;
4334
4335 if ((*bsp) == NULL)
4336 return 0; /* No more breakpoint values */
8671a17b 4337
4a64f543
MS
4338 /* We assume we'll never have several bpstats that correspond to a
4339 single breakpoint -- otherwise, this function might return the
4340 same number more than once and this will look ugly. */
f431efe5 4341 b = (*bsp)->breakpoint_at;
8671a17b
PA
4342 *bsp = (*bsp)->next;
4343 if (b == NULL)
4344 return -1; /* breakpoint that's been deleted since */
4345
4346 *num = b->number; /* We have its number */
4347 return 1;
c906108c
SS
4348}
4349
e93ca019 4350/* See breakpoint.h. */
c906108c
SS
4351
4352void
e93ca019 4353bpstat_clear_actions (void)
c906108c 4354{
e93ca019
JK
4355 bpstat bs;
4356
00431a78 4357 if (inferior_ptid == null_ptid)
e93ca019
JK
4358 return;
4359
00431a78 4360 thread_info *tp = inferior_thread ();
e93ca019 4361 for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
c906108c 4362 {
d1b0a7bf 4363 bs->commands = NULL;
850645cf 4364 bs->old_val.reset (nullptr);
c906108c
SS
4365 }
4366}
4367
f3b1572e
PA
4368/* Called when a command is about to proceed the inferior. */
4369
4370static void
4371breakpoint_about_to_proceed (void)
4372{
d7e15655 4373 if (inferior_ptid != null_ptid)
f3b1572e
PA
4374 {
4375 struct thread_info *tp = inferior_thread ();
4376
4377 /* Allow inferior function calls in breakpoint commands to not
4378 interrupt the command list. When the call finishes
4379 successfully, the inferior will be standing at the same
4380 breakpoint as if nothing happened. */
16c381f0 4381 if (tp->control.in_infcall)
f3b1572e
PA
4382 return;
4383 }
4384
4385 breakpoint_proceeded = 1;
4386}
4387
abf85f46
JK
4388/* Return non-zero iff CMD as the first line of a command sequence is `silent'
4389 or its equivalent. */
4390
4391static int
4392command_line_is_silent (struct command_line *cmd)
4393{
4f45d445 4394 return cmd && (strcmp ("silent", cmd->line) == 0);
abf85f46
JK
4395}
4396
4a64f543
MS
4397/* Execute all the commands associated with all the breakpoints at
4398 this location. Any of these commands could cause the process to
4399 proceed beyond this point, etc. We look out for such changes by
4400 checking the global "breakpoint_proceeded" after each command.
c906108c 4401
347bddb7
PA
4402 Returns true if a breakpoint command resumed the inferior. In that
4403 case, it is the caller's responsibility to recall it again with the
4404 bpstat of the current thread. */
4405
4406static int
4407bpstat_do_actions_1 (bpstat *bsp)
c906108c
SS
4408{
4409 bpstat bs;
347bddb7 4410 int again = 0;
c906108c
SS
4411
4412 /* Avoid endless recursion if a `source' command is contained
4413 in bs->commands. */
4414 if (executing_breakpoint_commands)
347bddb7 4415 return 0;
c906108c 4416
81b1e71c
TT
4417 scoped_restore save_executing
4418 = make_scoped_restore (&executing_breakpoint_commands, 1);
c906108c 4419
1ac32117 4420 scoped_restore preventer = prevent_dont_repeat ();
cf6c5ffb 4421
4a64f543 4422 /* This pointer will iterate over the list of bpstat's. */
c906108c
SS
4423 bs = *bsp;
4424
4425 breakpoint_proceeded = 0;
4426 for (; bs != NULL; bs = bs->next)
4427 {
d1b0a7bf 4428 struct command_line *cmd = NULL;
6c50ab1c
JB
4429
4430 /* Take ownership of the BSP's command tree, if it has one.
4431
4432 The command tree could legitimately contain commands like
4433 'step' and 'next', which call clear_proceed_status, which
4434 frees stop_bpstat's command tree. To make sure this doesn't
4435 free the tree we're executing out from under us, we need to
4436 take ownership of the tree ourselves. Since a given bpstat's
4437 commands are only executed once, we don't need to copy it; we
4438 can clear the pointer in the bpstat, and make sure we free
4439 the tree when we're done. */
d1b0a7bf 4440 counted_command_line ccmd = bs->commands;
9add0f1b 4441 bs->commands = NULL;
d1b0a7bf
TT
4442 if (ccmd != NULL)
4443 cmd = ccmd.get ();
abf85f46
JK
4444 if (command_line_is_silent (cmd))
4445 {
4446 /* The action has been already done by bpstat_stop_status. */
4447 cmd = cmd->next;
4448 }
6c50ab1c 4449
c906108c
SS
4450 while (cmd != NULL)
4451 {
4452 execute_control_command (cmd);
4453
4454 if (breakpoint_proceeded)
4455 break;
4456 else
4457 cmd = cmd->next;
4458 }
6c50ab1c 4459
c906108c 4460 if (breakpoint_proceeded)
32c1e744 4461 {
cb814510 4462 if (current_ui->async)
347bddb7
PA
4463 /* If we are in async mode, then the target might be still
4464 running, not stopped at any breakpoint, so nothing for
4465 us to do here -- just return to the event loop. */
4466 ;
32c1e744
VP
4467 else
4468 /* In sync mode, when execute_control_command returns
4469 we're already standing on the next breakpoint.
347bddb7
PA
4470 Breakpoint commands for that stop were not run, since
4471 execute_command does not run breakpoint commands --
4472 only command_line_handler does, but that one is not
4473 involved in execution of breakpoint commands. So, we
4474 can now execute breakpoint commands. It should be
4475 noted that making execute_command do bpstat actions is
4476 not an option -- in this case we'll have recursive
4477 invocation of bpstat for each breakpoint with a
4478 command, and can easily blow up GDB stack. Instead, we
4479 return true, which will trigger the caller to recall us
4480 with the new stop_bpstat. */
4481 again = 1;
4482 break;
32c1e744 4483 }
c906108c 4484 }
347bddb7
PA
4485 return again;
4486}
4487
00431a78
PA
4488/* Helper for bpstat_do_actions. Get the current thread, if there's
4489 one, is alive and has execution. Return NULL otherwise. */
4490
4491static thread_info *
4492get_bpstat_thread ()
4493{
55f6301a 4494 if (inferior_ptid == null_ptid || !target_has_execution ())
00431a78
PA
4495 return NULL;
4496
4497 thread_info *tp = inferior_thread ();
4498 if (tp->state == THREAD_EXITED || tp->executing)
4499 return NULL;
4500 return tp;
4501}
4502
347bddb7
PA
4503void
4504bpstat_do_actions (void)
4505{
694c6bf5 4506 auto cleanup_if_error = make_scope_exit (bpstat_clear_actions);
00431a78 4507 thread_info *tp;
353d1d73 4508
347bddb7 4509 /* Do any commands attached to breakpoint we are stopped at. */
00431a78
PA
4510 while ((tp = get_bpstat_thread ()) != NULL)
4511 {
4512 /* Since in sync mode, bpstat_do_actions may resume the
4513 inferior, and only return when it is stopped at the next
4514 breakpoint, we keep doing breakpoint actions until it returns
4515 false to indicate the inferior was not resumed. */
4516 if (!bpstat_do_actions_1 (&tp->control.stop_bpstat))
4517 break;
4518 }
353d1d73 4519
694c6bf5 4520 cleanup_if_error.release ();
c906108c
SS
4521}
4522
fa4727a6
DJ
4523/* Print out the (old or new) value associated with a watchpoint. */
4524
4525static void
4526watchpoint_value_print (struct value *val, struct ui_file *stream)
4527{
4528 if (val == NULL)
7f6aba03 4529 fprintf_styled (stream, metadata_style.style (), _("<unreadable>"));
fa4727a6 4530 else
79a45b7d
TT
4531 {
4532 struct value_print_options opts;
4533 get_user_print_options (&opts);
4534 value_print (val, stream, &opts);
4535 }
fa4727a6
DJ
4536}
4537
f303dbd6
PA
4538/* Print the "Thread ID hit" part of "Thread ID hit Breakpoint N" if
4539 debugging multiple threads. */
4540
4541void
4542maybe_print_thread_hit_breakpoint (struct ui_out *uiout)
4543{
112e8700 4544 if (uiout->is_mi_like_p ())
f303dbd6
PA
4545 return;
4546
112e8700 4547 uiout->text ("\n");
f303dbd6
PA
4548
4549 if (show_thread_that_caused_stop ())
4550 {
4551 const char *name;
4552 struct thread_info *thr = inferior_thread ();
4553
112e8700 4554 uiout->text ("Thread ");
33eca680 4555 uiout->field_string ("thread-id", print_thread_id (thr));
f303dbd6
PA
4556
4557 name = thr->name != NULL ? thr->name : target_thread_name (thr);
4558 if (name != NULL)
4559 {
112e8700 4560 uiout->text (" \"");
33eca680 4561 uiout->field_string ("name", name);
112e8700 4562 uiout->text ("\"");
f303dbd6
PA
4563 }
4564
112e8700 4565 uiout->text (" hit ");
f303dbd6
PA
4566 }
4567}
4568
e514a9d6 4569/* Generic routine for printing messages indicating why we
4a64f543 4570 stopped. The behavior of this function depends on the value
e514a9d6
JM
4571 'print_it' in the bpstat structure. Under some circumstances we
4572 may decide not to print anything here and delegate the task to
4a64f543 4573 normal_stop(). */
e514a9d6
JM
4574
4575static enum print_stop_action
4576print_bp_stop_message (bpstat bs)
4577{
4578 switch (bs->print_it)
4579 {
4580 case print_it_noop:
4a64f543 4581 /* Nothing should be printed for this bpstat entry. */
e514a9d6
JM
4582 return PRINT_UNKNOWN;
4583 break;
4584
4585 case print_it_done:
4586 /* We still want to print the frame, but we already printed the
4a64f543 4587 relevant messages. */
e514a9d6
JM
4588 return PRINT_SRC_AND_LOC;
4589 break;
4590
4591 case print_it_normal:
4f8d1dc6 4592 {
f431efe5
PA
4593 struct breakpoint *b = bs->breakpoint_at;
4594
1a6a67de
TJB
4595 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
4596 which has since been deleted. */
4597 if (b == NULL)
4598 return PRINT_UNKNOWN;
4599
348d480f
PA
4600 /* Normal case. Call the breakpoint's print_it method. */
4601 return b->ops->print_it (bs);
4f8d1dc6 4602 }
348d480f 4603 break;
3086aeae 4604
e514a9d6 4605 default:
8e65ff28 4606 internal_error (__FILE__, __LINE__,
e2e0b3e5 4607 _("print_bp_stop_message: unrecognized enum value"));
e514a9d6 4608 break;
c906108c 4609 }
c906108c
SS
4610}
4611
edcc5120
TT
4612/* A helper function that prints a shared library stopped event. */
4613
4614static void
4615print_solib_event (int is_catchpoint)
4616{
6fb16ce6 4617 bool any_deleted = !current_program_space->deleted_solibs.empty ();
bcb430e4 4618 bool any_added = !current_program_space->added_solibs.empty ();
edcc5120
TT
4619
4620 if (!is_catchpoint)
4621 {
4622 if (any_added || any_deleted)
112e8700 4623 current_uiout->text (_("Stopped due to shared library event:\n"));
edcc5120 4624 else
112e8700
SM
4625 current_uiout->text (_("Stopped due to shared library event (no "
4626 "libraries added or removed)\n"));
edcc5120
TT
4627 }
4628
112e8700
SM
4629 if (current_uiout->is_mi_like_p ())
4630 current_uiout->field_string ("reason",
4631 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
edcc5120
TT
4632
4633 if (any_deleted)
4634 {
112e8700 4635 current_uiout->text (_(" Inferior unloaded "));
10f489e5 4636 ui_out_emit_list list_emitter (current_uiout, "removed");
6fb16ce6 4637 for (int ix = 0; ix < current_program_space->deleted_solibs.size (); ix++)
edcc5120 4638 {
6fb16ce6
SM
4639 const std::string &name = current_program_space->deleted_solibs[ix];
4640
edcc5120 4641 if (ix > 0)
112e8700
SM
4642 current_uiout->text (" ");
4643 current_uiout->field_string ("library", name);
4644 current_uiout->text ("\n");
edcc5120 4645 }
edcc5120
TT
4646 }
4647
4648 if (any_added)
4649 {
112e8700 4650 current_uiout->text (_(" Inferior loaded "));
10f489e5 4651 ui_out_emit_list list_emitter (current_uiout, "added");
bcb430e4 4652 bool first = true;
52941706 4653 for (so_list *iter : current_program_space->added_solibs)
edcc5120 4654 {
bcb430e4 4655 if (!first)
112e8700 4656 current_uiout->text (" ");
bcb430e4 4657 first = false;
112e8700
SM
4658 current_uiout->field_string ("library", iter->so_name);
4659 current_uiout->text ("\n");
edcc5120 4660 }
edcc5120
TT
4661 }
4662}
4663
e514a9d6
JM
4664/* Print a message indicating what happened. This is called from
4665 normal_stop(). The input to this routine is the head of the bpstat
36dfb11c
TT
4666 list - a list of the eventpoints that caused this stop. KIND is
4667 the target_waitkind for the stopping event. This
e514a9d6
JM
4668 routine calls the generic print routine for printing a message
4669 about reasons for stopping. This will print (for example) the
4670 "Breakpoint n," part of the output. The return value of this
4671 routine is one of:
c906108c 4672
4a64f543 4673 PRINT_UNKNOWN: Means we printed nothing.
917317f4 4674 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4a64f543 4675 code to print the location. An example is
c5aa993b
JM
4676 "Breakpoint 1, " which should be followed by
4677 the location.
917317f4 4678 PRINT_SRC_ONLY: Means we printed something, but there is no need
c5aa993b
JM
4679 to also print the location part of the message.
4680 An example is the catch/throw messages, which
4a64f543 4681 don't require a location appended to the end.
917317f4 4682 PRINT_NOTHING: We have done some printing and we don't need any
4a64f543 4683 further info to be printed. */
c906108c 4684
917317f4 4685enum print_stop_action
36dfb11c 4686bpstat_print (bpstat bs, int kind)
c906108c 4687{
f486487f 4688 enum print_stop_action val;
c5aa993b 4689
c906108c 4690 /* Maybe another breakpoint in the chain caused us to stop.
53a5351d
JM
4691 (Currently all watchpoints go on the bpstat whether hit or not.
4692 That probably could (should) be changed, provided care is taken
c906108c 4693 with respect to bpstat_explains_signal). */
e514a9d6
JM
4694 for (; bs; bs = bs->next)
4695 {
4696 val = print_bp_stop_message (bs);
4697 if (val == PRINT_SRC_ONLY
4698 || val == PRINT_SRC_AND_LOC
4699 || val == PRINT_NOTHING)
4700 return val;
4701 }
c906108c 4702
36dfb11c
TT
4703 /* If we had hit a shared library event breakpoint,
4704 print_bp_stop_message would print out this message. If we hit an
4705 OS-level shared library event, do the same thing. */
4706 if (kind == TARGET_WAITKIND_LOADED)
4707 {
edcc5120 4708 print_solib_event (0);
36dfb11c
TT
4709 return PRINT_NOTHING;
4710 }
4711
e514a9d6 4712 /* We reached the end of the chain, or we got a null BS to start
4a64f543 4713 with and nothing was printed. */
917317f4 4714 return PRINT_UNKNOWN;
c906108c
SS
4715}
4716
bf469271 4717/* Evaluate the boolean expression EXP and return the result. */
c906108c 4718
bf469271
PA
4719static bool
4720breakpoint_cond_eval (expression *exp)
c906108c 4721{
278cd55f 4722 struct value *mark = value_mark ();
bf469271 4723 bool res = value_true (evaluate_expression (exp));
cc59ec59 4724
c906108c 4725 value_free_to_mark (mark);
bf469271 4726 return res;
c906108c
SS
4727}
4728
5760d0ab 4729/* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
c906108c 4730
04afa70c
TT
4731bpstats::bpstats (struct bp_location *bl, bpstat **bs_link_pointer)
4732 : next (NULL),
4733 bp_location_at (bl),
4734 breakpoint_at (bl->owner),
4735 commands (NULL),
04afa70c
TT
4736 print (0),
4737 stop (0),
4738 print_it (print_it_normal)
c906108c 4739{
f431efe5 4740 incref_bp_location (bl);
04afa70c
TT
4741 **bs_link_pointer = this;
4742 *bs_link_pointer = &next;
4743}
4744
4745bpstats::bpstats ()
4746 : next (NULL),
4747 bp_location_at (NULL),
4748 breakpoint_at (NULL),
4749 commands (NULL),
04afa70c
TT
4750 print (0),
4751 stop (0),
4752 print_it (print_it_normal)
4753{
c906108c
SS
4754}
4755\f
d983da9c
DJ
4756/* The target has stopped with waitstatus WS. Check if any hardware
4757 watchpoints have triggered, according to the target. */
4758
4759int
4760watchpoints_triggered (struct target_waitstatus *ws)
4761{
57810aa7 4762 bool stopped_by_watchpoint = target_stopped_by_watchpoint ();
d983da9c
DJ
4763 CORE_ADDR addr;
4764 struct breakpoint *b;
4765
4766 if (!stopped_by_watchpoint)
4767 {
4768 /* We were not stopped by a watchpoint. Mark all watchpoints
4769 as not triggered. */
4770 ALL_BREAKPOINTS (b)
cc60f2e3 4771 if (is_hardware_watchpoint (b))
3a5c3e22
PA
4772 {
4773 struct watchpoint *w = (struct watchpoint *) b;
4774
4775 w->watchpoint_triggered = watch_triggered_no;
4776 }
d983da9c
DJ
4777
4778 return 0;
4779 }
4780
8b88a78e 4781 if (!target_stopped_data_address (current_top_target (), &addr))
d983da9c
DJ
4782 {
4783 /* We were stopped by a watchpoint, but we don't know where.
4784 Mark all watchpoints as unknown. */
4785 ALL_BREAKPOINTS (b)
cc60f2e3 4786 if (is_hardware_watchpoint (b))
3a5c3e22
PA
4787 {
4788 struct watchpoint *w = (struct watchpoint *) b;
4789
4790 w->watchpoint_triggered = watch_triggered_unknown;
4791 }
d983da9c 4792
3c4797ba 4793 return 1;
d983da9c
DJ
4794 }
4795
4796 /* The target could report the data address. Mark watchpoints
4797 affected by this data address as triggered, and all others as not
4798 triggered. */
4799
4800 ALL_BREAKPOINTS (b)
cc60f2e3 4801 if (is_hardware_watchpoint (b))
d983da9c 4802 {
3a5c3e22 4803 struct watchpoint *w = (struct watchpoint *) b;
a5606eee 4804 struct bp_location *loc;
d983da9c 4805
3a5c3e22 4806 w->watchpoint_triggered = watch_triggered_no;
a5606eee 4807 for (loc = b->loc; loc; loc = loc->next)
9c06b0b4 4808 {
3a5c3e22 4809 if (is_masked_watchpoint (b))
9c06b0b4 4810 {
3a5c3e22
PA
4811 CORE_ADDR newaddr = addr & w->hw_wp_mask;
4812 CORE_ADDR start = loc->address & w->hw_wp_mask;
9c06b0b4
TJB
4813
4814 if (newaddr == start)
4815 {
3a5c3e22 4816 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
4817 break;
4818 }
4819 }
4820 /* Exact match not required. Within range is sufficient. */
8b88a78e 4821 else if (target_watchpoint_addr_within_range (current_top_target (),
9c06b0b4
TJB
4822 addr, loc->address,
4823 loc->length))
4824 {
3a5c3e22 4825 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
4826 break;
4827 }
4828 }
d983da9c
DJ
4829 }
4830
4831 return 1;
4832}
4833
bf469271
PA
4834/* Possible return values for watchpoint_check. */
4835enum wp_check_result
4836 {
4837 /* The watchpoint has been deleted. */
4838 WP_DELETED = 1,
4839
4840 /* The value has changed. */
4841 WP_VALUE_CHANGED = 2,
4842
4843 /* The value has not changed. */
4844 WP_VALUE_NOT_CHANGED = 3,
4845
4846 /* Ignore this watchpoint, no matter if the value changed or not. */
4847 WP_IGNORE = 4,
4848 };
c906108c
SS
4849
4850#define BP_TEMPFLAG 1
4851#define BP_HARDWAREFLAG 2
4852
4a64f543 4853/* Evaluate watchpoint condition expression and check if its value
bf469271 4854 changed. */
553e4c11 4855
bf469271
PA
4856static wp_check_result
4857watchpoint_check (bpstat bs)
c906108c 4858{
3a5c3e22 4859 struct watchpoint *b;
c906108c
SS
4860 struct frame_info *fr;
4861 int within_current_scope;
4862
f431efe5 4863 /* BS is built from an existing struct breakpoint. */
2bdf28a0 4864 gdb_assert (bs->breakpoint_at != NULL);
3a5c3e22 4865 b = (struct watchpoint *) bs->breakpoint_at;
d0fb5eae 4866
f6bc2008
PA
4867 /* If this is a local watchpoint, we only want to check if the
4868 watchpoint frame is in scope if the current thread is the thread
4869 that was used to create the watchpoint. */
4870 if (!watchpoint_in_thread_scope (b))
60e1c644 4871 return WP_IGNORE;
f6bc2008 4872
c906108c
SS
4873 if (b->exp_valid_block == NULL)
4874 within_current_scope = 1;
4875 else
4876 {
edb3359d
DJ
4877 struct frame_info *frame = get_current_frame ();
4878 struct gdbarch *frame_arch = get_frame_arch (frame);
4879 CORE_ADDR frame_pc = get_frame_pc (frame);
4880
c9cf6e20 4881 /* stack_frame_destroyed_p() returns a non-zero value if we're
4a64f543
MS
4882 still in the function but the stack frame has already been
4883 invalidated. Since we can't rely on the values of local
4884 variables after the stack has been destroyed, we are treating
4885 the watchpoint in that state as `not changed' without further
4886 checking. Don't mark watchpoints as changed if the current
4887 frame is in an epilogue - even if they are in some other
4888 frame, our view of the stack is likely to be wrong and
4889 frame_find_by_id could error out. */
c9cf6e20 4890 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
60e1c644 4891 return WP_IGNORE;
a0f49112 4892
101dcfbe 4893 fr = frame_find_by_id (b->watchpoint_frame);
c906108c 4894 within_current_scope = (fr != NULL);
69fbadd5
DJ
4895
4896 /* If we've gotten confused in the unwinder, we might have
4897 returned a frame that can't describe this variable. */
edb3359d
DJ
4898 if (within_current_scope)
4899 {
4900 struct symbol *function;
4901
4902 function = get_frame_function (fr);
4903 if (function == NULL
4904 || !contained_in (b->exp_valid_block,
4905 SYMBOL_BLOCK_VALUE (function)))
4906 within_current_scope = 0;
4907 }
69fbadd5 4908
edb3359d 4909 if (within_current_scope)
c906108c
SS
4910 /* If we end up stopping, the current frame will get selected
4911 in normal_stop. So this call to select_frame won't affect
4912 the user. */
0f7d239c 4913 select_frame (fr);
c906108c 4914 }
c5aa993b 4915
c906108c
SS
4916 if (within_current_scope)
4917 {
4a64f543
MS
4918 /* We use value_{,free_to_}mark because it could be a *long*
4919 time before we return to the command level and call
4920 free_all_values. We can't call free_all_values because we
4921 might be in the middle of evaluating a function call. */
c906108c 4922
0cf6dd15 4923 int pc = 0;
9c06b0b4 4924 struct value *mark;
fa4727a6
DJ
4925 struct value *new_val;
4926
c1fc2657 4927 if (is_masked_watchpoint (b))
9c06b0b4
TJB
4928 /* Since we don't know the exact trigger address (from
4929 stopped_data_address), just tell the user we've triggered
4930 a mask watchpoint. */
4931 return WP_VALUE_CHANGED;
4932
4933 mark = value_mark ();
4d01a485 4934 fetch_subexp_value (b->exp.get (), &pc, &new_val, NULL, NULL, 0);
218d2fc6 4935
bb9d5f81
PP
4936 if (b->val_bitsize != 0)
4937 new_val = extract_bitfield_from_watchpoint_value (b, new_val);
4938
4a64f543
MS
4939 /* We use value_equal_contents instead of value_equal because
4940 the latter coerces an array to a pointer, thus comparing just
4941 the address of the array instead of its contents. This is
4942 not what we want. */
fa4727a6 4943 if ((b->val != NULL) != (new_val != NULL)
850645cf
TT
4944 || (b->val != NULL && !value_equal_contents (b->val.get (),
4945 new_val)))
c906108c 4946 {
c906108c 4947 bs->old_val = b->val;
850645cf 4948 b->val = release_value (new_val);
4c1d86d9 4949 b->val_valid = true;
850645cf
TT
4950 if (new_val != NULL)
4951 value_free_to_mark (mark);
c906108c
SS
4952 return WP_VALUE_CHANGED;
4953 }
4954 else
4955 {
60e1c644 4956 /* Nothing changed. */
c906108c 4957 value_free_to_mark (mark);
c906108c
SS
4958 return WP_VALUE_NOT_CHANGED;
4959 }
4960 }
4961 else
4962 {
4963 /* This seems like the only logical thing to do because
c5aa993b
JM
4964 if we temporarily ignored the watchpoint, then when
4965 we reenter the block in which it is valid it contains
4966 garbage (in the case of a function, it may have two
4967 garbage values, one before and one after the prologue).
4968 So we can't even detect the first assignment to it and
4969 watch after that (since the garbage may or may not equal
4970 the first value assigned). */
348d480f
PA
4971 /* We print all the stop information in
4972 breakpoint_ops->print_it, but in this case, by the time we
4973 call breakpoint_ops->print_it this bp will be deleted
4974 already. So we have no choice but print the information
4975 here. */
468afe6c 4976
0e454242 4977 SWITCH_THRU_ALL_UIS ()
468afe6c
PA
4978 {
4979 struct ui_out *uiout = current_uiout;
4980
112e8700
SM
4981 if (uiout->is_mi_like_p ())
4982 uiout->field_string
4983 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
6a831f06
PA
4984 uiout->message ("\nWatchpoint %pF deleted because the program has "
4985 "left the block in\n"
4986 "which its expression is valid.\n",
4987 signed_field ("wpnum", b->number));
468afe6c 4988 }
4ce44c66 4989
cdac0397 4990 /* Make sure the watchpoint's commands aren't executed. */
d1b0a7bf 4991 b->commands = NULL;
d0fb5eae 4992 watchpoint_del_at_next_stop (b);
c906108c
SS
4993
4994 return WP_DELETED;
4995 }
4996}
4997
18a18393 4998/* Return true if it looks like target has stopped due to hitting
348d480f
PA
4999 breakpoint location BL. This function does not check if we should
5000 stop, only if BL explains the stop. */
5001
18a18393 5002static int
6c95b8df 5003bpstat_check_location (const struct bp_location *bl,
accd0bcd 5004 const address_space *aspace, CORE_ADDR bp_addr,
09ac7c10 5005 const struct target_waitstatus *ws)
18a18393
VP
5006{
5007 struct breakpoint *b = bl->owner;
5008
348d480f 5009 /* BL is from an existing breakpoint. */
2bdf28a0
JK
5010 gdb_assert (b != NULL);
5011
bd522513 5012 return b->ops->breakpoint_hit (bl, aspace, bp_addr, ws);
18a18393
VP
5013}
5014
3a5c3e22
PA
5015/* Determine if the watched values have actually changed, and we
5016 should stop. If not, set BS->stop to 0. */
5017
18a18393
VP
5018static void
5019bpstat_check_watchpoint (bpstat bs)
5020{
2bdf28a0 5021 const struct bp_location *bl;
3a5c3e22 5022 struct watchpoint *b;
2bdf28a0
JK
5023
5024 /* BS is built for existing struct breakpoint. */
f431efe5 5025 bl = bs->bp_location_at;
2bdf28a0 5026 gdb_assert (bl != NULL);
3a5c3e22 5027 b = (struct watchpoint *) bs->breakpoint_at;
2bdf28a0 5028 gdb_assert (b != NULL);
18a18393 5029
18a18393 5030 {
18a18393
VP
5031 int must_check_value = 0;
5032
c1fc2657 5033 if (b->type == bp_watchpoint)
18a18393
VP
5034 /* For a software watchpoint, we must always check the
5035 watched value. */
5036 must_check_value = 1;
5037 else if (b->watchpoint_triggered == watch_triggered_yes)
5038 /* We have a hardware watchpoint (read, write, or access)
5039 and the target earlier reported an address watched by
5040 this watchpoint. */
5041 must_check_value = 1;
5042 else if (b->watchpoint_triggered == watch_triggered_unknown
c1fc2657 5043 && b->type == bp_hardware_watchpoint)
18a18393
VP
5044 /* We were stopped by a hardware watchpoint, but the target could
5045 not report the data address. We must check the watchpoint's
5046 value. Access and read watchpoints are out of luck; without
5047 a data address, we can't figure it out. */
5048 must_check_value = 1;
3a5c3e22 5049
18a18393
VP
5050 if (must_check_value)
5051 {
bf469271
PA
5052 wp_check_result e;
5053
a70b8144 5054 try
bf469271
PA
5055 {
5056 e = watchpoint_check (bs);
5057 }
230d2906 5058 catch (const gdb_exception &ex)
bf469271
PA
5059 {
5060 exception_fprintf (gdb_stderr, ex,
5061 "Error evaluating expression "
5062 "for watchpoint %d\n",
5063 b->number);
5064
5065 SWITCH_THRU_ALL_UIS ()
5066 {
5067 printf_filtered (_("Watchpoint %d deleted.\n"),
5068 b->number);
5069 }
5070 watchpoint_del_at_next_stop (b);
5071 e = WP_DELETED;
5072 }
bf469271 5073
18a18393
VP
5074 switch (e)
5075 {
5076 case WP_DELETED:
5077 /* We've already printed what needs to be printed. */
5078 bs->print_it = print_it_done;
5079 /* Stop. */
5080 break;
60e1c644
PA
5081 case WP_IGNORE:
5082 bs->print_it = print_it_noop;
5083 bs->stop = 0;
5084 break;
18a18393 5085 case WP_VALUE_CHANGED:
c1fc2657 5086 if (b->type == bp_read_watchpoint)
18a18393 5087 {
85d721b8
PA
5088 /* There are two cases to consider here:
5089
4a64f543 5090 1. We're watching the triggered memory for reads.
85d721b8
PA
5091 In that case, trust the target, and always report
5092 the watchpoint hit to the user. Even though
5093 reads don't cause value changes, the value may
5094 have changed since the last time it was read, and
5095 since we're not trapping writes, we will not see
5096 those, and as such we should ignore our notion of
5097 old value.
5098
4a64f543 5099 2. We're watching the triggered memory for both
85d721b8
PA
5100 reads and writes. There are two ways this may
5101 happen:
5102
4a64f543 5103 2.1. This is a target that can't break on data
85d721b8
PA
5104 reads only, but can break on accesses (reads or
5105 writes), such as e.g., x86. We detect this case
5106 at the time we try to insert read watchpoints.
5107
4a64f543 5108 2.2. Otherwise, the target supports read
85d721b8
PA
5109 watchpoints, but, the user set an access or write
5110 watchpoint watching the same memory as this read
5111 watchpoint.
5112
5113 If we're watching memory writes as well as reads,
5114 ignore watchpoint hits when we find that the
5115 value hasn't changed, as reads don't cause
5116 changes. This still gives false positives when
5117 the program writes the same value to memory as
5118 what there was already in memory (we will confuse
5119 it for a read), but it's much better than
5120 nothing. */
5121
5122 int other_write_watchpoint = 0;
5123
5124 if (bl->watchpoint_type == hw_read)
5125 {
5126 struct breakpoint *other_b;
5127
5128 ALL_BREAKPOINTS (other_b)
3a5c3e22
PA
5129 if (other_b->type == bp_hardware_watchpoint
5130 || other_b->type == bp_access_watchpoint)
85d721b8 5131 {
3a5c3e22
PA
5132 struct watchpoint *other_w =
5133 (struct watchpoint *) other_b;
5134
5135 if (other_w->watchpoint_triggered
5136 == watch_triggered_yes)
5137 {
5138 other_write_watchpoint = 1;
5139 break;
5140 }
85d721b8
PA
5141 }
5142 }
5143
5144 if (other_write_watchpoint
5145 || bl->watchpoint_type == hw_access)
5146 {
5147 /* We're watching the same memory for writes,
5148 and the value changed since the last time we
5149 updated it, so this trap must be for a write.
5150 Ignore it. */
5151 bs->print_it = print_it_noop;
5152 bs->stop = 0;
5153 }
18a18393
VP
5154 }
5155 break;
5156 case WP_VALUE_NOT_CHANGED:
c1fc2657
SM
5157 if (b->type == bp_hardware_watchpoint
5158 || b->type == bp_watchpoint)
18a18393
VP
5159 {
5160 /* Don't stop: write watchpoints shouldn't fire if
5161 the value hasn't changed. */
5162 bs->print_it = print_it_noop;
5163 bs->stop = 0;
5164 }
5165 /* Stop. */
5166 break;
5167 default:
5168 /* Can't happen. */
18a18393
VP
5169 break;
5170 }
5171 }
5172 else /* must_check_value == 0 */
5173 {
5174 /* This is a case where some watchpoint(s) triggered, but
5175 not at the address of this watchpoint, or else no
5176 watchpoint triggered after all. So don't print
5177 anything for this watchpoint. */
5178 bs->print_it = print_it_noop;
5179 bs->stop = 0;
5180 }
5181 }
5182}
5183
7d4df6a4
DE
5184/* For breakpoints that are currently marked as telling gdb to stop,
5185 check conditions (condition proper, frame, thread and ignore count)
18a18393
VP
5186 of breakpoint referred to by BS. If we should not stop for this
5187 breakpoint, set BS->stop to 0. */
f431efe5 5188
18a18393 5189static void
00431a78 5190bpstat_check_breakpoint_conditions (bpstat bs, thread_info *thread)
18a18393 5191{
2bdf28a0
JK
5192 const struct bp_location *bl;
5193 struct breakpoint *b;
bf469271
PA
5194 /* Assume stop. */
5195 bool condition_result = true;
7d4df6a4
DE
5196 struct expression *cond;
5197
5198 gdb_assert (bs->stop);
2bdf28a0
JK
5199
5200 /* BS is built for existing struct breakpoint. */
f431efe5 5201 bl = bs->bp_location_at;
2bdf28a0 5202 gdb_assert (bl != NULL);
f431efe5 5203 b = bs->breakpoint_at;
2bdf28a0 5204 gdb_assert (b != NULL);
18a18393 5205
b775012e
LM
5206 /* Even if the target evaluated the condition on its end and notified GDB, we
5207 need to do so again since GDB does not know if we stopped due to a
5208 breakpoint or a single step breakpoint. */
5209
18a18393 5210 if (frame_id_p (b->frame_id)
edb3359d 5211 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
18a18393 5212 {
7d4df6a4
DE
5213 bs->stop = 0;
5214 return;
5215 }
60e1c644 5216
12ab52e9
PA
5217 /* If this is a thread/task-specific breakpoint, don't waste cpu
5218 evaluating the condition if this isn't the specified
5219 thread/task. */
00431a78
PA
5220 if ((b->thread != -1 && b->thread != thread->global_num)
5221 || (b->task != 0 && b->task != ada_get_task_number (thread)))
6c1b0f7b
DE
5222 {
5223 bs->stop = 0;
5224 return;
5225 }
5226
6dddc817
DE
5227 /* Evaluate extension language breakpoints that have a "stop" method
5228 implemented. */
5229 bs->stop = breakpoint_ext_lang_cond_says_stop (b);
7371cf6d 5230
7d4df6a4
DE
5231 if (is_watchpoint (b))
5232 {
5233 struct watchpoint *w = (struct watchpoint *) b;
3a5c3e22 5234
4d01a485 5235 cond = w->cond_exp.get ();
7d4df6a4
DE
5236 }
5237 else
4d01a485 5238 cond = bl->cond.get ();
60e1c644 5239
7d4df6a4
DE
5240 if (cond && b->disposition != disp_del_at_next_stop)
5241 {
5242 int within_current_scope = 1;
5243 struct watchpoint * w;
60e1c644 5244
7d4df6a4
DE
5245 /* We use value_mark and value_free_to_mark because it could
5246 be a long time before we return to the command level and
5247 call free_all_values. We can't call free_all_values
5248 because we might be in the middle of evaluating a
5249 function call. */
5250 struct value *mark = value_mark ();
5251
5252 if (is_watchpoint (b))
5253 w = (struct watchpoint *) b;
5254 else
5255 w = NULL;
5256
5257 /* Need to select the frame, with all that implies so that
5258 the conditions will have the right context. Because we
5259 use the frame, we will not see an inlined function's
5260 variables when we arrive at a breakpoint at the start
5261 of the inlined function; the current frame will be the
5262 call site. */
5263 if (w == NULL || w->cond_exp_valid_block == NULL)
5264 select_frame (get_current_frame ());
5265 else
18a18393 5266 {
7d4df6a4
DE
5267 struct frame_info *frame;
5268
5269 /* For local watchpoint expressions, which particular
5270 instance of a local is being watched matters, so we
5271 keep track of the frame to evaluate the expression
5272 in. To evaluate the condition however, it doesn't
5273 really matter which instantiation of the function
5274 where the condition makes sense triggers the
5275 watchpoint. This allows an expression like "watch
5276 global if q > 10" set in `func', catch writes to
5277 global on all threads that call `func', or catch
5278 writes on all recursive calls of `func' by a single
5279 thread. We simply always evaluate the condition in
5280 the innermost frame that's executing where it makes
5281 sense to evaluate the condition. It seems
5282 intuitive. */
5283 frame = block_innermost_frame (w->cond_exp_valid_block);
5284 if (frame != NULL)
5285 select_frame (frame);
5286 else
5287 within_current_scope = 0;
18a18393 5288 }
7d4df6a4 5289 if (within_current_scope)
bf469271 5290 {
a70b8144 5291 try
bf469271
PA
5292 {
5293 condition_result = breakpoint_cond_eval (cond);
5294 }
230d2906 5295 catch (const gdb_exception &ex)
bf469271
PA
5296 {
5297 exception_fprintf (gdb_stderr, ex,
5298 "Error in testing breakpoint condition:\n");
5299 }
bf469271 5300 }
7d4df6a4 5301 else
18a18393 5302 {
7d4df6a4
DE
5303 warning (_("Watchpoint condition cannot be tested "
5304 "in the current scope"));
5305 /* If we failed to set the right context for this
5306 watchpoint, unconditionally report it. */
18a18393 5307 }
7d4df6a4
DE
5308 /* FIXME-someday, should give breakpoint #. */
5309 value_free_to_mark (mark);
18a18393 5310 }
7d4df6a4 5311
bf469271 5312 if (cond && !condition_result)
7d4df6a4
DE
5313 {
5314 bs->stop = 0;
5315 }
7d4df6a4
DE
5316 else if (b->ignore_count > 0)
5317 {
5318 b->ignore_count--;
5319 bs->stop = 0;
5320 /* Increase the hit count even though we don't stop. */
5321 ++(b->hit_count);
76727919 5322 gdb::observers::breakpoint_modified.notify (b);
7d4df6a4 5323 }
18a18393
VP
5324}
5325
1cf4d951
PA
5326/* Returns true if we need to track moribund locations of LOC's type
5327 on the current target. */
5328
5329static int
5330need_moribund_for_location_type (struct bp_location *loc)
5331{
5332 return ((loc->loc_type == bp_loc_software_breakpoint
5333 && !target_supports_stopped_by_sw_breakpoint ())
5334 || (loc->loc_type == bp_loc_hardware_breakpoint
5335 && !target_supports_stopped_by_hw_breakpoint ()));
5336}
5337
ddfe970e 5338/* See breakpoint.h. */
c906108c
SS
5339
5340bpstat
ddfe970e 5341build_bpstat_chain (const address_space *aspace, CORE_ADDR bp_addr,
09ac7c10 5342 const struct target_waitstatus *ws)
c906108c 5343{
ddfe970e 5344 struct breakpoint *b;
5760d0ab 5345 bpstat bs_head = NULL, *bs_link = &bs_head;
c5aa993b 5346
429374b8
JK
5347 ALL_BREAKPOINTS (b)
5348 {
1a853c52 5349 if (!breakpoint_enabled (b))
429374b8 5350 continue;
a5606eee 5351
ddfe970e 5352 for (bp_location *bl = b->loc; bl != NULL; bl = bl->next)
429374b8 5353 {
4a64f543
MS
5354 /* For hardware watchpoints, we look only at the first
5355 location. The watchpoint_check function will work on the
5356 entire expression, not the individual locations. For
5357 read watchpoints, the watchpoints_triggered function has
5358 checked all locations already. */
429374b8
JK
5359 if (b->type == bp_hardware_watchpoint && bl != b->loc)
5360 break;
18a18393 5361
b5fa468f 5362 if (!bl->enabled || bl->disabled_by_cond || bl->shlib_disabled)
429374b8 5363 continue;
c5aa993b 5364
09ac7c10 5365 if (!bpstat_check_location (bl, aspace, bp_addr, ws))
429374b8 5366 continue;
c5aa993b 5367
4a64f543
MS
5368 /* Come here if it's a watchpoint, or if the break address
5369 matches. */
c5aa993b 5370
ddfe970e
KS
5371 bpstat bs = new bpstats (bl, &bs_link); /* Alloc a bpstat to
5372 explain stop. */
c5aa993b 5373
f431efe5
PA
5374 /* Assume we stop. Should we find a watchpoint that is not
5375 actually triggered, or if the condition of the breakpoint
5376 evaluates as false, we'll reset 'stop' to 0. */
429374b8
JK
5377 bs->stop = 1;
5378 bs->print = 1;
d983da9c 5379
f431efe5
PA
5380 /* If this is a scope breakpoint, mark the associated
5381 watchpoint as triggered so that we will handle the
5382 out-of-scope event. We'll get to the watchpoint next
5383 iteration. */
d0fb5eae 5384 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
3a5c3e22
PA
5385 {
5386 struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
5387
5388 w->watchpoint_triggered = watch_triggered_yes;
5389 }
f431efe5
PA
5390 }
5391 }
5392
7c16b83e 5393 /* Check if a moribund breakpoint explains the stop. */
1cf4d951
PA
5394 if (!target_supports_stopped_by_sw_breakpoint ()
5395 || !target_supports_stopped_by_hw_breakpoint ())
f431efe5 5396 {
1123588c 5397 for (bp_location *loc : moribund_locations)
f431efe5 5398 {
1cf4d951
PA
5399 if (breakpoint_location_address_match (loc, aspace, bp_addr)
5400 && need_moribund_for_location_type (loc))
5401 {
ddfe970e 5402 bpstat bs = new bpstats (loc, &bs_link);
1cf4d951
PA
5403 /* For hits of moribund locations, we should just proceed. */
5404 bs->stop = 0;
5405 bs->print = 0;
5406 bs->print_it = print_it_noop;
5407 }
f431efe5
PA
5408 }
5409 }
5410
ddfe970e
KS
5411 return bs_head;
5412}
5413
5414/* See breakpoint.h. */
5415
5416bpstat
5417bpstat_stop_status (const address_space *aspace,
00431a78 5418 CORE_ADDR bp_addr, thread_info *thread,
ddfe970e
KS
5419 const struct target_waitstatus *ws,
5420 bpstat stop_chain)
5421{
5422 struct breakpoint *b = NULL;
5423 /* First item of allocated bpstat's. */
5424 bpstat bs_head = stop_chain;
5425 bpstat bs;
5426 int need_remove_insert;
5427 int removed_any;
5428
5429 /* First, build the bpstat chain with locations that explain a
5430 target stop, while being careful to not set the target running,
5431 as that may invalidate locations (in particular watchpoint
5432 locations are recreated). Resuming will happen here with
5433 breakpoint conditions or watchpoint expressions that include
5434 inferior function calls. */
5435 if (bs_head == NULL)
5436 bs_head = build_bpstat_chain (aspace, bp_addr, ws);
5437
edcc5120
TT
5438 /* A bit of special processing for shlib breakpoints. We need to
5439 process solib loading here, so that the lists of loaded and
5440 unloaded libraries are correct before we handle "catch load" and
5441 "catch unload". */
5442 for (bs = bs_head; bs != NULL; bs = bs->next)
5443 {
5d268276 5444 if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
edcc5120
TT
5445 {
5446 handle_solib_event ();
5447 break;
5448 }
5449 }
5450
f431efe5
PA
5451 /* Now go through the locations that caused the target to stop, and
5452 check whether we're interested in reporting this stop to higher
5453 layers, or whether we should resume the target transparently. */
5454
5455 removed_any = 0;
5456
5760d0ab 5457 for (bs = bs_head; bs != NULL; bs = bs->next)
f431efe5
PA
5458 {
5459 if (!bs->stop)
5460 continue;
5461
f431efe5 5462 b = bs->breakpoint_at;
348d480f
PA
5463 b->ops->check_status (bs);
5464 if (bs->stop)
28010a5d 5465 {
00431a78 5466 bpstat_check_breakpoint_conditions (bs, thread);
f431efe5 5467
429374b8
JK
5468 if (bs->stop)
5469 {
5470 ++(b->hit_count);
76727919 5471 gdb::observers::breakpoint_modified.notify (b);
c906108c 5472
4a64f543 5473 /* We will stop here. */
429374b8
JK
5474 if (b->disposition == disp_disable)
5475 {
816338b5 5476 --(b->enable_count);
1a853c52 5477 if (b->enable_count <= 0)
429374b8 5478 b->enable_state = bp_disabled;
f431efe5 5479 removed_any = 1;
429374b8
JK
5480 }
5481 if (b->silent)
5482 bs->print = 0;
5483 bs->commands = b->commands;
abf85f46 5484 if (command_line_is_silent (bs->commands
d1b0a7bf 5485 ? bs->commands.get () : NULL))
abf85f46 5486 bs->print = 0;
9d6e6e84
HZ
5487
5488 b->ops->after_condition_true (bs);
429374b8
JK
5489 }
5490
348d480f 5491 }
a9b3a50f
PA
5492
5493 /* Print nothing for this entry if we don't stop or don't
5494 print. */
5495 if (!bs->stop || !bs->print)
5496 bs->print_it = print_it_noop;
429374b8 5497 }
876fa593 5498
d983da9c
DJ
5499 /* If we aren't stopping, the value of some hardware watchpoint may
5500 not have changed, but the intermediate memory locations we are
5501 watching may have. Don't bother if we're stopping; this will get
5502 done later. */
d832cb68 5503 need_remove_insert = 0;
5760d0ab
JK
5504 if (! bpstat_causes_stop (bs_head))
5505 for (bs = bs_head; bs != NULL; bs = bs->next)
d983da9c 5506 if (!bs->stop
f431efe5
PA
5507 && bs->breakpoint_at
5508 && is_hardware_watchpoint (bs->breakpoint_at))
d983da9c 5509 {
3a5c3e22
PA
5510 struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
5511
5512 update_watchpoint (w, 0 /* don't reparse. */);
d832cb68 5513 need_remove_insert = 1;
d983da9c
DJ
5514 }
5515
d832cb68 5516 if (need_remove_insert)
44702360 5517 update_global_location_list (UGLL_MAY_INSERT);
f431efe5 5518 else if (removed_any)
44702360 5519 update_global_location_list (UGLL_DONT_INSERT);
d832cb68 5520
5760d0ab 5521 return bs_head;
c906108c 5522}
628fe4e4
JK
5523
5524static void
61c26be8 5525handle_jit_event (CORE_ADDR address)
628fe4e4 5526{
628fe4e4
JK
5527 struct gdbarch *gdbarch;
5528
1eb8556f 5529 infrun_debug_printf ("handling bp_jit_event");
243a9253 5530
628fe4e4
JK
5531 /* Switch terminal for any messages produced by
5532 breakpoint_re_set. */
223ffa71 5533 target_terminal::ours_for_output ();
628fe4e4 5534
61c26be8
MS
5535 gdbarch = get_frame_arch (get_current_frame ());
5536 /* This event is caused by a breakpoint set in `jit_breakpoint_re_set`,
5537 thus it is expected that its objectfile can be found through
5538 minimal symbol lookup. If it doesn't work (and assert fails), it
5539 most likely means that `jit_breakpoint_re_set` was changes and this
5540 function needs to be updated too. */
5541 bound_minimal_symbol jit_bp_sym = lookup_minimal_symbol_by_pc (address);
5542 gdb_assert (jit_bp_sym.objfile != nullptr);
5543 jit_event_handler (gdbarch, jit_bp_sym.objfile);
628fe4e4 5544
223ffa71 5545 target_terminal::inferior ();
628fe4e4
JK
5546}
5547
5548/* Prepare WHAT final decision for infrun. */
5549
5550/* Decide what infrun needs to do with this bpstat. */
5551
c906108c 5552struct bpstat_what
0e30163f 5553bpstat_what (bpstat bs_head)
c906108c 5554{
c906108c 5555 struct bpstat_what retval;
0e30163f 5556 bpstat bs;
c906108c 5557
628fe4e4 5558 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
aa7d318d 5559 retval.call_dummy = STOP_NONE;
e2d0f980 5560 retval.is_longjmp = false;
628fe4e4 5561
0e30163f 5562 for (bs = bs_head; bs != NULL; bs = bs->next)
c906108c 5563 {
628fe4e4
JK
5564 /* Extract this BS's action. After processing each BS, we check
5565 if its action overrides all we've seem so far. */
5566 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
5567 enum bptype bptype;
5568
c906108c 5569 if (bs->breakpoint_at == NULL)
628fe4e4
JK
5570 {
5571 /* I suspect this can happen if it was a momentary
5572 breakpoint which has since been deleted. */
5573 bptype = bp_none;
5574 }
20874c92 5575 else
f431efe5 5576 bptype = bs->breakpoint_at->type;
628fe4e4
JK
5577
5578 switch (bptype)
c906108c
SS
5579 {
5580 case bp_none:
628fe4e4 5581 break;
c906108c
SS
5582 case bp_breakpoint:
5583 case bp_hardware_breakpoint:
7c16b83e 5584 case bp_single_step:
c906108c
SS
5585 case bp_until:
5586 case bp_finish:
a9b3a50f 5587 case bp_shlib_event:
c906108c
SS
5588 if (bs->stop)
5589 {
5590 if (bs->print)
628fe4e4 5591 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 5592 else
628fe4e4 5593 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
5594 }
5595 else
628fe4e4 5596 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5597 break;
5598 case bp_watchpoint:
5599 case bp_hardware_watchpoint:
5600 case bp_read_watchpoint:
5601 case bp_access_watchpoint:
5602 if (bs->stop)
5603 {
5604 if (bs->print)
628fe4e4 5605 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 5606 else
628fe4e4 5607 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
5608 }
5609 else
628fe4e4
JK
5610 {
5611 /* There was a watchpoint, but we're not stopping.
5612 This requires no further action. */
5613 }
c906108c
SS
5614 break;
5615 case bp_longjmp:
e2e4d78b 5616 case bp_longjmp_call_dummy:
186c406b 5617 case bp_exception:
0a39bb32
PA
5618 if (bs->stop)
5619 {
5620 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
5621 retval.is_longjmp = bptype != bp_exception;
5622 }
5623 else
5624 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5625 break;
5626 case bp_longjmp_resume:
186c406b 5627 case bp_exception_resume:
0a39bb32
PA
5628 if (bs->stop)
5629 {
5630 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
5631 retval.is_longjmp = bptype == bp_longjmp_resume;
5632 }
5633 else
5634 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5635 break;
5636 case bp_step_resume:
5637 if (bs->stop)
628fe4e4
JK
5638 this_action = BPSTAT_WHAT_STEP_RESUME;
5639 else
c906108c 5640 {
628fe4e4
JK
5641 /* It is for the wrong frame. */
5642 this_action = BPSTAT_WHAT_SINGLE;
c906108c 5643 }
c906108c 5644 break;
2c03e5be
PA
5645 case bp_hp_step_resume:
5646 if (bs->stop)
5647 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
5648 else
5649 {
5650 /* It is for the wrong frame. */
5651 this_action = BPSTAT_WHAT_SINGLE;
5652 }
5653 break;
c906108c 5654 case bp_watchpoint_scope:
c4093a6a 5655 case bp_thread_event:
1900040c 5656 case bp_overlay_event:
0fd8e87f 5657 case bp_longjmp_master:
aa7d318d 5658 case bp_std_terminate_master:
186c406b 5659 case bp_exception_master:
628fe4e4 5660 this_action = BPSTAT_WHAT_SINGLE;
c4093a6a 5661 break;
ce78b96d 5662 case bp_catchpoint:
c5aa993b
JM
5663 if (bs->stop)
5664 {
5665 if (bs->print)
628fe4e4 5666 this_action = BPSTAT_WHAT_STOP_NOISY;
c5aa993b 5667 else
628fe4e4 5668 this_action = BPSTAT_WHAT_STOP_SILENT;
c5aa993b
JM
5669 }
5670 else
628fe4e4 5671 {
cb1e4e32
PA
5672 /* Some catchpoints are implemented with breakpoints.
5673 For those, we need to step over the breakpoint. */
5674 if (bs->bp_location_at->loc_type != bp_loc_other)
5675 this_action = BPSTAT_WHAT_SINGLE;
628fe4e4
JK
5676 }
5677 break;
628fe4e4 5678 case bp_jit_event:
628fe4e4 5679 this_action = BPSTAT_WHAT_SINGLE;
c5aa993b 5680 break;
c906108c 5681 case bp_call_dummy:
53a5351d
JM
5682 /* Make sure the action is stop (silent or noisy),
5683 so infrun.c pops the dummy frame. */
aa7d318d 5684 retval.call_dummy = STOP_STACK_DUMMY;
628fe4e4 5685 this_action = BPSTAT_WHAT_STOP_SILENT;
aa7d318d
TT
5686 break;
5687 case bp_std_terminate:
5688 /* Make sure the action is stop (silent or noisy),
5689 so infrun.c pops the dummy frame. */
aa7d318d 5690 retval.call_dummy = STOP_STD_TERMINATE;
628fe4e4 5691 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c 5692 break;
1042e4c0 5693 case bp_tracepoint:
7a697b8d 5694 case bp_fast_tracepoint:
0fb4aa4b 5695 case bp_static_tracepoint:
1042e4c0
SS
5696 /* Tracepoint hits should not be reported back to GDB, and
5697 if one got through somehow, it should have been filtered
5698 out already. */
5699 internal_error (__FILE__, __LINE__,
7a697b8d 5700 _("bpstat_what: tracepoint encountered"));
0e30163f
JK
5701 break;
5702 case bp_gnu_ifunc_resolver:
5703 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
5704 this_action = BPSTAT_WHAT_SINGLE;
5705 break;
5706 case bp_gnu_ifunc_resolver_return:
5707 /* The breakpoint will be removed, execution will restart from the
5708 PC of the former breakpoint. */
5709 this_action = BPSTAT_WHAT_KEEP_CHECKING;
5710 break;
e7e0cddf
SS
5711
5712 case bp_dprintf:
a11cfd87
HZ
5713 if (bs->stop)
5714 this_action = BPSTAT_WHAT_STOP_SILENT;
5715 else
5716 this_action = BPSTAT_WHAT_SINGLE;
e7e0cddf
SS
5717 break;
5718
628fe4e4
JK
5719 default:
5720 internal_error (__FILE__, __LINE__,
5721 _("bpstat_what: unhandled bptype %d"), (int) bptype);
c906108c 5722 }
628fe4e4 5723
325fac50 5724 retval.main_action = std::max (retval.main_action, this_action);
c906108c 5725 }
628fe4e4 5726
243a9253
PA
5727 return retval;
5728}
628fe4e4 5729
243a9253
PA
5730void
5731bpstat_run_callbacks (bpstat bs_head)
5732{
5733 bpstat bs;
628fe4e4 5734
0e30163f
JK
5735 for (bs = bs_head; bs != NULL; bs = bs->next)
5736 {
5737 struct breakpoint *b = bs->breakpoint_at;
5738
5739 if (b == NULL)
5740 continue;
5741 switch (b->type)
5742 {
243a9253 5743 case bp_jit_event:
61c26be8 5744 handle_jit_event (bs->bp_location_at->address);
243a9253 5745 break;
0e30163f
JK
5746 case bp_gnu_ifunc_resolver:
5747 gnu_ifunc_resolver_stop (b);
5748 break;
5749 case bp_gnu_ifunc_resolver_return:
5750 gnu_ifunc_resolver_return_stop (b);
5751 break;
5752 }
5753 }
c906108c
SS
5754}
5755
4c462cb0 5756/* See breakpoint.h. */
c906108c 5757
4c462cb0
SM
5758bool
5759bpstat_should_step ()
c906108c
SS
5760{
5761 struct breakpoint *b;
cc59ec59 5762
c906108c 5763 ALL_BREAKPOINTS (b)
717a8278 5764 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
4c462cb0
SM
5765 return true;
5766 return false;
c906108c
SS
5767}
5768
4c462cb0
SM
5769/* See breakpoint.h. */
5770
5771bool
67822962
PA
5772bpstat_causes_stop (bpstat bs)
5773{
5774 for (; bs != NULL; bs = bs->next)
5775 if (bs->stop)
4c462cb0 5776 return true;
67822962 5777
4c462cb0 5778 return false;
67822962
PA
5779}
5780
c906108c 5781\f
c5aa993b 5782
170b53b2
UW
5783/* Compute a string of spaces suitable to indent the next line
5784 so it starts at the position corresponding to the table column
5785 named COL_NAME in the currently active table of UIOUT. */
5786
5787static char *
5788wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
5789{
5790 static char wrap_indent[80];
5791 int i, total_width, width, align;
c5209615 5792 const char *text;
170b53b2
UW
5793
5794 total_width = 0;
112e8700 5795 for (i = 1; uiout->query_table_field (i, &width, &align, &text); i++)
170b53b2
UW
5796 {
5797 if (strcmp (text, col_name) == 0)
5798 {
5799 gdb_assert (total_width < sizeof wrap_indent);
5800 memset (wrap_indent, ' ', total_width);
5801 wrap_indent[total_width] = 0;
5802
5803 return wrap_indent;
5804 }
5805
5806 total_width += width + 1;
5807 }
5808
5809 return NULL;
5810}
5811
b775012e
LM
5812/* Determine if the locations of this breakpoint will have their conditions
5813 evaluated by the target, host or a mix of both. Returns the following:
5814
5815 "host": Host evals condition.
5816 "host or target": Host or Target evals condition.
5817 "target": Target evals condition.
5818*/
5819
5820static const char *
5821bp_condition_evaluator (struct breakpoint *b)
5822{
5823 struct bp_location *bl;
5824 char host_evals = 0;
5825 char target_evals = 0;
5826
5827 if (!b)
5828 return NULL;
5829
5830 if (!is_breakpoint (b))
5831 return NULL;
5832
5833 if (gdb_evaluates_breakpoint_condition_p ()
5834 || !target_supports_evaluation_of_breakpoint_conditions ())
5835 return condition_evaluation_host;
5836
5837 for (bl = b->loc; bl; bl = bl->next)
5838 {
5839 if (bl->cond_bytecode)
5840 target_evals++;
5841 else
5842 host_evals++;
5843 }
5844
5845 if (host_evals && target_evals)
5846 return condition_evaluation_both;
5847 else if (target_evals)
5848 return condition_evaluation_target;
5849 else
5850 return condition_evaluation_host;
5851}
5852
5853/* Determine the breakpoint location's condition evaluator. This is
5854 similar to bp_condition_evaluator, but for locations. */
5855
5856static const char *
5857bp_location_condition_evaluator (struct bp_location *bl)
5858{
5859 if (bl && !is_breakpoint (bl->owner))
5860 return NULL;
5861
5862 if (gdb_evaluates_breakpoint_condition_p ()
5863 || !target_supports_evaluation_of_breakpoint_conditions ())
5864 return condition_evaluation_host;
5865
5866 if (bl && bl->cond_bytecode)
5867 return condition_evaluation_target;
5868 else
5869 return condition_evaluation_host;
5870}
5871
859825b8
JK
5872/* Print the LOC location out of the list of B->LOC locations. */
5873
170b53b2
UW
5874static void
5875print_breakpoint_location (struct breakpoint *b,
5876 struct bp_location *loc)
0d381245 5877{
79a45e25 5878 struct ui_out *uiout = current_uiout;
5ed8105e
PA
5879
5880 scoped_restore_current_program_space restore_pspace;
6c95b8df 5881
859825b8
JK
5882 if (loc != NULL && loc->shlib_disabled)
5883 loc = NULL;
5884
6c95b8df
PA
5885 if (loc != NULL)
5886 set_current_program_space (loc->pspace);
5887
56435ebe 5888 if (b->display_canonical)
d28cd78a 5889 uiout->field_string ("what", event_location_to_string (b->location.get ()));
2f202fde 5890 else if (loc && loc->symtab)
0d381245 5891 {
4a27f119
KS
5892 const struct symbol *sym = loc->symbol;
5893
0d381245
VP
5894 if (sym)
5895 {
112e8700 5896 uiout->text ("in ");
987012b8 5897 uiout->field_string ("func", sym->print_name (),
e43b10e1 5898 function_name_style.style ());
112e8700
SM
5899 uiout->text (" ");
5900 uiout->wrap_hint (wrap_indent_at_field (uiout, "what"));
5901 uiout->text ("at ");
0d381245 5902 }
112e8700 5903 uiout->field_string ("file",
cbe56571 5904 symtab_to_filename_for_display (loc->symtab),
e43b10e1 5905 file_name_style.style ());
112e8700 5906 uiout->text (":");
05cba821 5907
112e8700
SM
5908 if (uiout->is_mi_like_p ())
5909 uiout->field_string ("fullname", symtab_to_fullname (loc->symtab));
0d381245 5910
381befee 5911 uiout->field_signed ("line", loc->line_number);
0d381245 5912 }
859825b8 5913 else if (loc)
0d381245 5914 {
d7e74731 5915 string_file stb;
170b53b2 5916
d7e74731 5917 print_address_symbolic (loc->gdbarch, loc->address, &stb,
22e722e1 5918 demangle, "");
112e8700 5919 uiout->field_stream ("at", stb);
0d381245 5920 }
859825b8 5921 else
f00aae0f 5922 {
d28cd78a
TT
5923 uiout->field_string ("pending",
5924 event_location_to_string (b->location.get ()));
f00aae0f
KS
5925 /* If extra_string is available, it could be holding a condition
5926 or dprintf arguments. In either case, make sure it is printed,
5927 too, but only for non-MI streams. */
112e8700 5928 if (!uiout->is_mi_like_p () && b->extra_string != NULL)
f00aae0f
KS
5929 {
5930 if (b->type == bp_dprintf)
112e8700 5931 uiout->text (",");
f00aae0f 5932 else
112e8700
SM
5933 uiout->text (" ");
5934 uiout->text (b->extra_string);
f00aae0f
KS
5935 }
5936 }
6c95b8df 5937
b775012e
LM
5938 if (loc && is_breakpoint (b)
5939 && breakpoint_condition_evaluation_mode () == condition_evaluation_target
5940 && bp_condition_evaluator (b) == condition_evaluation_both)
5941 {
112e8700
SM
5942 uiout->text (" (");
5943 uiout->field_string ("evaluated-by",
b775012e 5944 bp_location_condition_evaluator (loc));
112e8700 5945 uiout->text (")");
b775012e 5946 }
0d381245
VP
5947}
5948
269b11a2
PA
5949static const char *
5950bptype_string (enum bptype type)
c906108c 5951{
c4093a6a
JM
5952 struct ep_type_description
5953 {
5954 enum bptype type;
a121b7c1 5955 const char *description;
c4093a6a
JM
5956 };
5957 static struct ep_type_description bptypes[] =
c906108c 5958 {
c5aa993b
JM
5959 {bp_none, "?deleted?"},
5960 {bp_breakpoint, "breakpoint"},
c906108c 5961 {bp_hardware_breakpoint, "hw breakpoint"},
7c16b83e 5962 {bp_single_step, "sw single-step"},
c5aa993b
JM
5963 {bp_until, "until"},
5964 {bp_finish, "finish"},
5965 {bp_watchpoint, "watchpoint"},
c906108c 5966 {bp_hardware_watchpoint, "hw watchpoint"},
c5aa993b
JM
5967 {bp_read_watchpoint, "read watchpoint"},
5968 {bp_access_watchpoint, "acc watchpoint"},
5969 {bp_longjmp, "longjmp"},
5970 {bp_longjmp_resume, "longjmp resume"},
e2e4d78b 5971 {bp_longjmp_call_dummy, "longjmp for call dummy"},
186c406b
TT
5972 {bp_exception, "exception"},
5973 {bp_exception_resume, "exception resume"},
c5aa993b 5974 {bp_step_resume, "step resume"},
2c03e5be 5975 {bp_hp_step_resume, "high-priority step resume"},
c5aa993b
JM
5976 {bp_watchpoint_scope, "watchpoint scope"},
5977 {bp_call_dummy, "call dummy"},
aa7d318d 5978 {bp_std_terminate, "std::terminate"},
c5aa993b 5979 {bp_shlib_event, "shlib events"},
c4093a6a 5980 {bp_thread_event, "thread events"},
1900040c 5981 {bp_overlay_event, "overlay events"},
0fd8e87f 5982 {bp_longjmp_master, "longjmp master"},
aa7d318d 5983 {bp_std_terminate_master, "std::terminate master"},
186c406b 5984 {bp_exception_master, "exception master"},
ce78b96d 5985 {bp_catchpoint, "catchpoint"},
1042e4c0 5986 {bp_tracepoint, "tracepoint"},
7a697b8d 5987 {bp_fast_tracepoint, "fast tracepoint"},
0fb4aa4b 5988 {bp_static_tracepoint, "static tracepoint"},
e7e0cddf 5989 {bp_dprintf, "dprintf"},
4efc6507 5990 {bp_jit_event, "jit events"},
0e30163f
JK
5991 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
5992 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
c5aa993b 5993 };
269b11a2
PA
5994
5995 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
5996 || ((int) type != bptypes[(int) type].type))
5997 internal_error (__FILE__, __LINE__,
5998 _("bptypes table does not describe type #%d."),
5999 (int) type);
6000
6001 return bptypes[(int) type].description;
6002}
6003
998580f1
MK
6004/* For MI, output a field named 'thread-groups' with a list as the value.
6005 For CLI, prefix the list with the string 'inf'. */
6006
6007static void
6008output_thread_groups (struct ui_out *uiout,
6009 const char *field_name,
5c632425 6010 const std::vector<int> &inf_nums,
998580f1
MK
6011 int mi_only)
6012{
112e8700 6013 int is_mi = uiout->is_mi_like_p ();
998580f1
MK
6014
6015 /* For backward compatibility, don't display inferiors in CLI unless
6016 there are several. Always display them for MI. */
6017 if (!is_mi && mi_only)
6018 return;
6019
10f489e5 6020 ui_out_emit_list list_emitter (uiout, field_name);
752eb8b4 6021
5c632425 6022 for (size_t i = 0; i < inf_nums.size (); i++)
998580f1
MK
6023 {
6024 if (is_mi)
6025 {
6026 char mi_group[10];
6027
5c632425 6028 xsnprintf (mi_group, sizeof (mi_group), "i%d", inf_nums[i]);
112e8700 6029 uiout->field_string (NULL, mi_group);
998580f1
MK
6030 }
6031 else
6032 {
6033 if (i == 0)
112e8700 6034 uiout->text (" inf ");
998580f1 6035 else
112e8700 6036 uiout->text (", ");
998580f1 6037
5c632425 6038 uiout->text (plongest (inf_nums[i]));
998580f1
MK
6039 }
6040 }
998580f1
MK
6041}
6042
a38118e5
PA
6043/* Print B to gdb_stdout. If RAW_LOC, print raw breakpoint locations
6044 instead of going via breakpoint_ops::print_one. This makes "maint
6045 info breakpoints" show the software breakpoint locations of
6046 catchpoints, which are considered internal implementation
6047 detail. */
269b11a2
PA
6048
6049static void
6050print_one_breakpoint_location (struct breakpoint *b,
6051 struct bp_location *loc,
6052 int loc_number,
6053 struct bp_location **last_loc,
a38118e5 6054 int allflag, bool raw_loc)
269b11a2
PA
6055{
6056 struct command_line *l;
c2c6d25f 6057 static char bpenables[] = "nynny";
c906108c 6058
79a45e25 6059 struct ui_out *uiout = current_uiout;
0d381245
VP
6060 int header_of_multiple = 0;
6061 int part_of_multiple = (loc != NULL);
79a45b7d
TT
6062 struct value_print_options opts;
6063
6064 get_user_print_options (&opts);
0d381245
VP
6065
6066 gdb_assert (!loc || loc_number != 0);
4a64f543
MS
6067 /* See comment in print_one_breakpoint concerning treatment of
6068 breakpoints with single disabled location. */
0d381245
VP
6069 if (loc == NULL
6070 && (b->loc != NULL
b5fa468f
TBA
6071 && (b->loc->next != NULL
6072 || !b->loc->enabled || b->loc->disabled_by_cond)))
0d381245
VP
6073 header_of_multiple = 1;
6074 if (loc == NULL)
6075 loc = b->loc;
6076
c4093a6a
JM
6077 annotate_record ();
6078
6079 /* 1 */
6080 annotate_field (0);
0d381245 6081 if (part_of_multiple)
528e1572 6082 uiout->field_fmt ("number", "%d.%d", b->number, loc_number);
0d381245 6083 else
381befee 6084 uiout->field_signed ("number", b->number);
c4093a6a
JM
6085
6086 /* 2 */
6087 annotate_field (1);
0d381245 6088 if (part_of_multiple)
112e8700 6089 uiout->field_skip ("type");
269b11a2 6090 else
112e8700 6091 uiout->field_string ("type", bptype_string (b->type));
c4093a6a
JM
6092
6093 /* 3 */
6094 annotate_field (2);
0d381245 6095 if (part_of_multiple)
112e8700 6096 uiout->field_skip ("disp");
0d381245 6097 else
112e8700 6098 uiout->field_string ("disp", bpdisp_text (b->disposition));
0d381245 6099
c4093a6a
JM
6100 /* 4 */
6101 annotate_field (3);
0d381245 6102 if (part_of_multiple)
b5fa468f
TBA
6103 uiout->field_string ("enabled", (loc->disabled_by_cond ? "N*"
6104 : (loc->enabled ? "y" : "n")));
0d381245 6105 else
112e8700 6106 uiout->field_fmt ("enabled", "%c", bpenables[(int) b->enable_state]);
0d381245 6107
c4093a6a 6108 /* 5 and 6 */
a38118e5 6109 if (!raw_loc && b->ops != NULL && b->ops->print_one != NULL)
b58a68fe 6110 b->ops->print_one (b, last_loc);
3086aeae 6111 else
a38118e5
PA
6112 {
6113 if (is_watchpoint (b))
3a5c3e22
PA
6114 {
6115 struct watchpoint *w = (struct watchpoint *) b;
6116
6117 /* Field 4, the address, is omitted (which makes the columns
6118 not line up too nicely with the headers, but the effect
6119 is relatively readable). */
6120 if (opts.addressprint)
112e8700 6121 uiout->field_skip ("addr");
3a5c3e22 6122 annotate_field (5);
112e8700 6123 uiout->field_string ("what", w->exp_string);
3a5c3e22 6124 }
f06f1252
TT
6125 else if (!is_catchpoint (b) || is_exception_catchpoint (b)
6126 || is_ada_exception_catchpoint (b))
a38118e5
PA
6127 {
6128 if (opts.addressprint)
6129 {
6130 annotate_field (4);
6131 if (header_of_multiple)
7f6aba03
TT
6132 uiout->field_string ("addr", "<MULTIPLE>",
6133 metadata_style.style ());
a38118e5 6134 else if (b->loc == NULL || loc->shlib_disabled)
7f6aba03
TT
6135 uiout->field_string ("addr", "<PENDING>",
6136 metadata_style.style ());
a38118e5
PA
6137 else
6138 uiout->field_core_addr ("addr",
6139 loc->gdbarch, loc->address);
6140 }
6141 annotate_field (5);
6142 if (!header_of_multiple)
6143 print_breakpoint_location (b, loc);
6144 if (b->loc)
6145 *last_loc = b->loc;
6146 }
6147 }
6c95b8df 6148
998580f1 6149 if (loc != NULL && !header_of_multiple)
6c95b8df 6150 {
5c632425 6151 std::vector<int> inf_nums;
998580f1 6152 int mi_only = 1;
6c95b8df 6153
08036331 6154 for (inferior *inf : all_inferiors ())
6c95b8df
PA
6155 {
6156 if (inf->pspace == loc->pspace)
5c632425 6157 inf_nums.push_back (inf->num);
6c95b8df 6158 }
998580f1
MK
6159
6160 /* For backward compatibility, don't display inferiors in CLI unless
6161 there are several. Always display for MI. */
6162 if (allflag
6163 || (!gdbarch_has_global_breakpoints (target_gdbarch ())
94c93c35 6164 && (program_spaces.size () > 1
998580f1
MK
6165 || number_of_inferiors () > 1)
6166 /* LOC is for existing B, it cannot be in
6167 moribund_locations and thus having NULL OWNER. */
6168 && loc->owner->type != bp_catchpoint))
6169 mi_only = 0;
5c632425 6170 output_thread_groups (uiout, "thread-groups", inf_nums, mi_only);
6c95b8df
PA
6171 }
6172
4a306c9a 6173 if (!part_of_multiple)
c4093a6a 6174 {
4a306c9a
JB
6175 if (b->thread != -1)
6176 {
6177 /* FIXME: This seems to be redundant and lost here; see the
4a64f543 6178 "stop only in" line a little further down. */
112e8700 6179 uiout->text (" thread ");
381befee 6180 uiout->field_signed ("thread", b->thread);
4a306c9a
JB
6181 }
6182 else if (b->task != 0)
6183 {
112e8700 6184 uiout->text (" task ");
381befee 6185 uiout->field_signed ("task", b->task);
4a306c9a 6186 }
c4093a6a 6187 }
f1310107 6188
112e8700 6189 uiout->text ("\n");
f1310107 6190
348d480f 6191 if (!part_of_multiple)
f1310107
TJB
6192 b->ops->print_one_detail (b, uiout);
6193
0d381245 6194 if (part_of_multiple && frame_id_p (b->frame_id))
c4093a6a
JM
6195 {
6196 annotate_field (6);
112e8700 6197 uiout->text ("\tstop only in stack frame at ");
e5dd4106 6198 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
818dd999 6199 the frame ID. */
112e8700 6200 uiout->field_core_addr ("frame",
5af949e3 6201 b->gdbarch, b->frame_id.stack_addr);
112e8700 6202 uiout->text ("\n");
c4093a6a
JM
6203 }
6204
28010a5d 6205 if (!part_of_multiple && b->cond_string)
c4093a6a
JM
6206 {
6207 annotate_field (7);
d77f58be 6208 if (is_tracepoint (b))
112e8700 6209 uiout->text ("\ttrace only if ");
1042e4c0 6210 else
112e8700
SM
6211 uiout->text ("\tstop only if ");
6212 uiout->field_string ("cond", b->cond_string);
b775012e
LM
6213
6214 /* Print whether the target is doing the breakpoint's condition
6215 evaluation. If GDB is doing the evaluation, don't print anything. */
6216 if (is_breakpoint (b)
6217 && breakpoint_condition_evaluation_mode ()
6218 == condition_evaluation_target)
6219 {
6a831f06
PA
6220 uiout->message (" (%pF evals)",
6221 string_field ("evaluated-by",
6222 bp_condition_evaluator (b)));
b775012e 6223 }
112e8700 6224 uiout->text ("\n");
0101ce28
JJ
6225 }
6226
0d381245 6227 if (!part_of_multiple && b->thread != -1)
c4093a6a 6228 {
4a64f543 6229 /* FIXME should make an annotation for this. */
112e8700
SM
6230 uiout->text ("\tstop only in thread ");
6231 if (uiout->is_mi_like_p ())
381befee 6232 uiout->field_signed ("thread", b->thread);
5d5658a1
PA
6233 else
6234 {
6235 struct thread_info *thr = find_thread_global_id (b->thread);
6236
112e8700 6237 uiout->field_string ("thread", print_thread_id (thr));
5d5658a1 6238 }
112e8700 6239 uiout->text ("\n");
c4093a6a
JM
6240 }
6241
556ec64d
YQ
6242 if (!part_of_multiple)
6243 {
6244 if (b->hit_count)
31f56a27
YQ
6245 {
6246 /* FIXME should make an annotation for this. */
6247 if (is_catchpoint (b))
112e8700 6248 uiout->text ("\tcatchpoint");
31f56a27 6249 else if (is_tracepoint (b))
112e8700 6250 uiout->text ("\ttracepoint");
31f56a27 6251 else
112e8700
SM
6252 uiout->text ("\tbreakpoint");
6253 uiout->text (" already hit ");
381befee 6254 uiout->field_signed ("times", b->hit_count);
31f56a27 6255 if (b->hit_count == 1)
112e8700 6256 uiout->text (" time\n");
31f56a27 6257 else
112e8700 6258 uiout->text (" times\n");
31f56a27 6259 }
556ec64d
YQ
6260 else
6261 {
31f56a27 6262 /* Output the count also if it is zero, but only if this is mi. */
112e8700 6263 if (uiout->is_mi_like_p ())
381befee 6264 uiout->field_signed ("times", b->hit_count);
556ec64d
YQ
6265 }
6266 }
8b93c638 6267
0d381245 6268 if (!part_of_multiple && b->ignore_count)
c4093a6a
JM
6269 {
6270 annotate_field (8);
6a831f06
PA
6271 uiout->message ("\tignore next %pF hits\n",
6272 signed_field ("ignore", b->ignore_count));
c4093a6a 6273 }
059fb39f 6274
816338b5
SS
6275 /* Note that an enable count of 1 corresponds to "enable once"
6276 behavior, which is reported by the combination of enablement and
6277 disposition, so we don't need to mention it here. */
6278 if (!part_of_multiple && b->enable_count > 1)
6279 {
6280 annotate_field (8);
112e8700 6281 uiout->text ("\tdisable after ");
816338b5
SS
6282 /* Tweak the wording to clarify that ignore and enable counts
6283 are distinct, and have additive effect. */
6284 if (b->ignore_count)
112e8700 6285 uiout->text ("additional ");
816338b5 6286 else
112e8700 6287 uiout->text ("next ");
381befee 6288 uiout->field_signed ("enable", b->enable_count);
112e8700 6289 uiout->text (" hits\n");
816338b5
SS
6290 }
6291
f196051f
SS
6292 if (!part_of_multiple && is_tracepoint (b))
6293 {
6294 struct tracepoint *tp = (struct tracepoint *) b;
6295
6296 if (tp->traceframe_usage)
6297 {
112e8700 6298 uiout->text ("\ttrace buffer usage ");
381befee 6299 uiout->field_signed ("traceframe-usage", tp->traceframe_usage);
112e8700 6300 uiout->text (" bytes\n");
f196051f
SS
6301 }
6302 }
d3ce09f5 6303
d1b0a7bf 6304 l = b->commands ? b->commands.get () : NULL;
059fb39f 6305 if (!part_of_multiple && l)
c4093a6a
JM
6306 {
6307 annotate_field (9);
2e783024 6308 ui_out_emit_tuple tuple_emitter (uiout, "script");
8b93c638 6309 print_command_lines (uiout, l, 4);
c4093a6a 6310 }
d24317b4 6311
d9b3f62e 6312 if (is_tracepoint (b))
1042e4c0 6313 {
d9b3f62e
PA
6314 struct tracepoint *t = (struct tracepoint *) b;
6315
6316 if (!part_of_multiple && t->pass_count)
6317 {
6318 annotate_field (10);
112e8700 6319 uiout->text ("\tpass count ");
381befee 6320 uiout->field_signed ("pass", t->pass_count);
112e8700 6321 uiout->text (" \n");
d9b3f62e 6322 }
f2a8bc8a
YQ
6323
6324 /* Don't display it when tracepoint or tracepoint location is
6325 pending. */
6326 if (!header_of_multiple && loc != NULL && !loc->shlib_disabled)
6327 {
6328 annotate_field (11);
6329
112e8700
SM
6330 if (uiout->is_mi_like_p ())
6331 uiout->field_string ("installed",
f2a8bc8a
YQ
6332 loc->inserted ? "y" : "n");
6333 else
6334 {
6335 if (loc->inserted)
112e8700 6336 uiout->text ("\t");
f2a8bc8a 6337 else
112e8700
SM
6338 uiout->text ("\tnot ");
6339 uiout->text ("installed on target\n");
f2a8bc8a
YQ
6340 }
6341 }
1042e4c0
SS
6342 }
6343
112e8700 6344 if (uiout->is_mi_like_p () && !part_of_multiple)
d24317b4 6345 {
3a5c3e22
PA
6346 if (is_watchpoint (b))
6347 {
6348 struct watchpoint *w = (struct watchpoint *) b;
6349
112e8700 6350 uiout->field_string ("original-location", w->exp_string);
3a5c3e22 6351 }
f00aae0f 6352 else if (b->location != NULL
d28cd78a 6353 && event_location_to_string (b->location.get ()) != NULL)
112e8700 6354 uiout->field_string ("original-location",
d28cd78a 6355 event_location_to_string (b->location.get ()));
d24317b4 6356 }
c4093a6a 6357}
c5aa993b 6358
13674803
SM
6359/* See breakpoint.h. */
6360
6361bool fix_multi_location_breakpoint_output_globally = false;
6362
0d381245
VP
6363static void
6364print_one_breakpoint (struct breakpoint *b,
4a64f543 6365 struct bp_location **last_loc,
6c95b8df 6366 int allflag)
0d381245 6367{
79a45e25 6368 struct ui_out *uiout = current_uiout;
13674803
SM
6369 bool use_fixed_output
6370 = (uiout->test_flags (fix_multi_location_breakpoint_output)
6371 || fix_multi_location_breakpoint_output_globally);
8d3788bd 6372
b4be1b06 6373 gdb::optional<ui_out_emit_tuple> bkpt_tuple_emitter (gdb::in_place, uiout, "bkpt");
a38118e5 6374 print_one_breakpoint_location (b, NULL, 0, last_loc, allflag, false);
8d3788bd 6375
b4be1b06
SM
6376 /* The mi2 broken format: the main breakpoint tuple ends here, the locations
6377 are outside. */
6378 if (!use_fixed_output)
6379 bkpt_tuple_emitter.reset ();
0d381245
VP
6380
6381 /* If this breakpoint has custom print function,
6382 it's already printed. Otherwise, print individual
6383 locations, if any. */
a38118e5
PA
6384 if (b->ops == NULL
6385 || b->ops->print_one == NULL
6386 || allflag)
0d381245 6387 {
4a64f543
MS
6388 /* If breakpoint has a single location that is disabled, we
6389 print it as if it had several locations, since otherwise it's
6390 hard to represent "breakpoint enabled, location disabled"
6391 situation.
6392
6393 Note that while hardware watchpoints have several locations
a38118e5
PA
6394 internally, that's not a property exposed to users.
6395
6396 Likewise, while catchpoints may be implemented with
6397 breakpoints (e.g., catch throw), that's not a property
6398 exposed to users. We do however display the internal
6399 breakpoint locations with "maint info breakpoints". */
6400 if (!is_hardware_watchpoint (b)
f06f1252
TT
6401 && (!is_catchpoint (b) || is_exception_catchpoint (b)
6402 || is_ada_exception_catchpoint (b))
a38118e5 6403 && (allflag
b5fa468f
TBA
6404 || (b->loc && (b->loc->next
6405 || !b->loc->enabled
6406 || b->loc->disabled_by_cond))))
0d381245 6407 {
b4be1b06
SM
6408 gdb::optional<ui_out_emit_list> locations_list;
6409
6410 /* For MI version <= 2, keep the behavior where GDB outputs an invalid
6411 MI record. For later versions, place breakpoint locations in a
6412 list. */
6413 if (uiout->is_mi_like_p () && use_fixed_output)
6414 locations_list.emplace (uiout, "locations");
8d3788bd 6415
b4be1b06
SM
6416 int n = 1;
6417 for (bp_location *loc = b->loc; loc != NULL; loc = loc->next, ++n)
8d3788bd 6418 {
b4be1b06 6419 ui_out_emit_tuple loc_tuple_emitter (uiout, NULL);
a38118e5
PA
6420 print_one_breakpoint_location (b, loc, n, last_loc,
6421 allflag, allflag);
8d3788bd 6422 }
0d381245
VP
6423 }
6424 }
6425}
6426
a6d9a66e
UW
6427static int
6428breakpoint_address_bits (struct breakpoint *b)
6429{
6430 int print_address_bits = 0;
6431 struct bp_location *loc;
6432
c6d81124
PA
6433 /* Software watchpoints that aren't watching memory don't have an
6434 address to print. */
6435 if (is_no_memory_software_watchpoint (b))
6436 return 0;
6437
a6d9a66e
UW
6438 for (loc = b->loc; loc; loc = loc->next)
6439 {
c7437ca6
PA
6440 int addr_bit;
6441
c7437ca6 6442 addr_bit = gdbarch_addr_bit (loc->gdbarch);
a6d9a66e
UW
6443 if (addr_bit > print_address_bits)
6444 print_address_bits = addr_bit;
6445 }
6446
6447 return print_address_bits;
6448}
0d381245 6449
65630365 6450/* See breakpoint.h. */
c5aa993b 6451
65630365
PA
6452void
6453print_breakpoint (breakpoint *b)
c4093a6a 6454{
a6d9a66e 6455 struct bp_location *dummy_loc = NULL;
65630365 6456 print_one_breakpoint (b, &dummy_loc, 0);
c4093a6a 6457}
c5aa993b 6458
09d682a4
TT
6459/* Return true if this breakpoint was set by the user, false if it is
6460 internal or momentary. */
6461
6462int
6463user_breakpoint_p (struct breakpoint *b)
6464{
46c6471b 6465 return b->number > 0;
09d682a4
TT
6466}
6467
93daf339
TT
6468/* See breakpoint.h. */
6469
6470int
6471pending_breakpoint_p (struct breakpoint *b)
6472{
6473 return b->loc == NULL;
6474}
6475
5c458ae8
SM
6476/* Print information on breakpoints (including watchpoints and tracepoints).
6477
6478 If non-NULL, BP_NUM_LIST is a list of numbers and number ranges as
6479 understood by number_or_range_parser. Only breakpoints included in this
6480 list are then printed.
6481
6482 If SHOW_INTERNAL is true, print internal breakpoints.
6483
6484 If FILTER is non-NULL, call it on each breakpoint and only include the
6485 ones for which it returns true.
6486
6487 Return the total number of breakpoints listed. */
c906108c 6488
d77f58be 6489static int
5c458ae8 6490breakpoint_1 (const char *bp_num_list, bool show_internal,
f2478a7e 6491 bool (*filter) (const struct breakpoint *))
c4093a6a 6492{
52f0bd74 6493 struct breakpoint *b;
a6d9a66e 6494 struct bp_location *last_loc = NULL;
7f3b0473 6495 int nr_printable_breakpoints;
79a45b7d 6496 struct value_print_options opts;
a6d9a66e 6497 int print_address_bits = 0;
269b11a2 6498 int print_type_col_width = 14;
79a45e25 6499 struct ui_out *uiout = current_uiout;
b5fa468f 6500 bool has_disabled_by_cond_location = false;
269b11a2 6501
79a45b7d
TT
6502 get_user_print_options (&opts);
6503
4a64f543
MS
6504 /* Compute the number of rows in the table, as well as the size
6505 required for address fields. */
7f3b0473
AC
6506 nr_printable_breakpoints = 0;
6507 ALL_BREAKPOINTS (b)
e5a67952
MS
6508 {
6509 /* If we have a filter, only list the breakpoints it accepts. */
6510 if (filter && !filter (b))
6511 continue;
6512
5c458ae8 6513 /* If we have a BP_NUM_LIST string, it is a list of breakpoints to
e5a67952 6514 accept. Skip the others. */
5c458ae8 6515 if (bp_num_list != NULL && *bp_num_list != '\0')
e5a67952 6516 {
5c458ae8 6517 if (show_internal && parse_and_eval_long (bp_num_list) != b->number)
e5a67952 6518 continue;
5c458ae8 6519 if (!show_internal && !number_is_in_list (bp_num_list, b->number))
e5a67952
MS
6520 continue;
6521 }
269b11a2 6522
5c458ae8 6523 if (show_internal || user_breakpoint_p (b))
e5a67952
MS
6524 {
6525 int addr_bit, type_len;
a6d9a66e 6526
e5a67952
MS
6527 addr_bit = breakpoint_address_bits (b);
6528 if (addr_bit > print_address_bits)
6529 print_address_bits = addr_bit;
269b11a2 6530
e5a67952
MS
6531 type_len = strlen (bptype_string (b->type));
6532 if (type_len > print_type_col_width)
6533 print_type_col_width = type_len;
6534
6535 nr_printable_breakpoints++;
6536 }
6537 }
7f3b0473 6538
4a2b031d
TT
6539 {
6540 ui_out_emit_table table_emitter (uiout,
6541 opts.addressprint ? 6 : 5,
6542 nr_printable_breakpoints,
6543 "BreakpointTable");
6544
6545 if (nr_printable_breakpoints > 0)
6546 annotate_breakpoints_headers ();
6547 if (nr_printable_breakpoints > 0)
6548 annotate_field (0);
6549 uiout->table_header (7, ui_left, "number", "Num"); /* 1 */
6550 if (nr_printable_breakpoints > 0)
6551 annotate_field (1);
6552 uiout->table_header (print_type_col_width, ui_left, "type", "Type"); /* 2 */
6553 if (nr_printable_breakpoints > 0)
6554 annotate_field (2);
6555 uiout->table_header (4, ui_left, "disp", "Disp"); /* 3 */
6556 if (nr_printable_breakpoints > 0)
6557 annotate_field (3);
6558 uiout->table_header (3, ui_left, "enabled", "Enb"); /* 4 */
6559 if (opts.addressprint)
6560 {
6561 if (nr_printable_breakpoints > 0)
6562 annotate_field (4);
6563 if (print_address_bits <= 32)
6564 uiout->table_header (10, ui_left, "addr", "Address"); /* 5 */
6565 else
6566 uiout->table_header (18, ui_left, "addr", "Address"); /* 5 */
6567 }
6568 if (nr_printable_breakpoints > 0)
6569 annotate_field (5);
6570 uiout->table_header (40, ui_noalign, "what", "What"); /* 6 */
6571 uiout->table_body ();
6572 if (nr_printable_breakpoints > 0)
6573 annotate_breakpoints_table ();
6574
6575 ALL_BREAKPOINTS (b)
6576 {
6577 QUIT;
6578 /* If we have a filter, only list the breakpoints it accepts. */
6579 if (filter && !filter (b))
6580 continue;
e5a67952 6581
5c458ae8 6582 /* If we have a BP_NUM_LIST string, it is a list of breakpoints to
4a2b031d 6583 accept. Skip the others. */
e5a67952 6584
5c458ae8 6585 if (bp_num_list != NULL && *bp_num_list != '\0')
4a2b031d 6586 {
5c458ae8 6587 if (show_internal) /* maintenance info breakpoint */
4a2b031d 6588 {
5c458ae8 6589 if (parse_and_eval_long (bp_num_list) != b->number)
4a2b031d
TT
6590 continue;
6591 }
6592 else /* all others */
6593 {
5c458ae8 6594 if (!number_is_in_list (bp_num_list, b->number))
4a2b031d
TT
6595 continue;
6596 }
6597 }
6598 /* We only print out user settable breakpoints unless the
5c458ae8
SM
6599 show_internal is set. */
6600 if (show_internal || user_breakpoint_p (b))
b5fa468f
TBA
6601 {
6602 print_one_breakpoint (b, &last_loc, show_internal);
6603 for (bp_location *loc = b->loc; loc != NULL; loc = loc->next)
6604 if (loc->disabled_by_cond)
6605 has_disabled_by_cond_location = true;
6606 }
4a2b031d
TT
6607 }
6608 }
698384cd 6609
7f3b0473 6610 if (nr_printable_breakpoints == 0)
c906108c 6611 {
4a64f543
MS
6612 /* If there's a filter, let the caller decide how to report
6613 empty list. */
d77f58be
SS
6614 if (!filter)
6615 {
5c458ae8 6616 if (bp_num_list == NULL || *bp_num_list == '\0')
112e8700 6617 uiout->message ("No breakpoints or watchpoints.\n");
d77f58be 6618 else
112e8700 6619 uiout->message ("No breakpoint or watchpoint matching '%s'.\n",
5c458ae8 6620 bp_num_list);
d77f58be 6621 }
c906108c
SS
6622 }
6623 else
c4093a6a 6624 {
a6d9a66e
UW
6625 if (last_loc && !server_command)
6626 set_next_address (last_loc->gdbarch, last_loc->address);
b5fa468f
TBA
6627
6628 if (has_disabled_by_cond_location)
6629 uiout->message (_("(*): Breakpoint condition is invalid at this "
6630 "location.\n"));
c4093a6a 6631 }
c906108c 6632
4a64f543 6633 /* FIXME? Should this be moved up so that it is only called when
c4093a6a 6634 there have been breakpoints? */
c906108c 6635 annotate_breakpoints_table_end ();
d77f58be
SS
6636
6637 return nr_printable_breakpoints;
c906108c
SS
6638}
6639
ad443146
SS
6640/* Display the value of default-collect in a way that is generally
6641 compatible with the breakpoint list. */
6642
6643static void
6644default_collect_info (void)
6645{
79a45e25
PA
6646 struct ui_out *uiout = current_uiout;
6647
ad443146
SS
6648 /* If it has no value (which is frequently the case), say nothing; a
6649 message like "No default-collect." gets in user's face when it's
6650 not wanted. */
6651 if (!*default_collect)
6652 return;
6653
6654 /* The following phrase lines up nicely with per-tracepoint collect
6655 actions. */
112e8700
SM
6656 uiout->text ("default collect ");
6657 uiout->field_string ("default-collect", default_collect);
6658 uiout->text (" \n");
ad443146
SS
6659}
6660
c906108c 6661static void
0b39b52e 6662info_breakpoints_command (const char *args, int from_tty)
c906108c 6663{
5c458ae8 6664 breakpoint_1 (args, false, NULL);
ad443146
SS
6665
6666 default_collect_info ();
d77f58be
SS
6667}
6668
6669static void
1d12d88f 6670info_watchpoints_command (const char *args, int from_tty)
d77f58be 6671{
5c458ae8 6672 int num_printed = breakpoint_1 (args, false, is_watchpoint);
79a45e25 6673 struct ui_out *uiout = current_uiout;
d77f58be
SS
6674
6675 if (num_printed == 0)
6676 {
e5a67952 6677 if (args == NULL || *args == '\0')
112e8700 6678 uiout->message ("No watchpoints.\n");
d77f58be 6679 else
112e8700 6680 uiout->message ("No watchpoint matching '%s'.\n", args);
d77f58be 6681 }
c906108c
SS
6682}
6683
7a292a7a 6684static void
4495129a 6685maintenance_info_breakpoints (const char *args, int from_tty)
c906108c 6686{
5c458ae8 6687 breakpoint_1 (args, true, NULL);
ad443146
SS
6688
6689 default_collect_info ();
c906108c
SS
6690}
6691
0d381245 6692static int
714835d5 6693breakpoint_has_pc (struct breakpoint *b,
6c95b8df 6694 struct program_space *pspace,
714835d5 6695 CORE_ADDR pc, struct obj_section *section)
0d381245
VP
6696{
6697 struct bp_location *bl = b->loc;
cc59ec59 6698
0d381245
VP
6699 for (; bl; bl = bl->next)
6700 {
6c95b8df
PA
6701 if (bl->pspace == pspace
6702 && bl->address == pc
0d381245
VP
6703 && (!overlay_debugging || bl->section == section))
6704 return 1;
6705 }
6706 return 0;
6707}
6708
672f9b60 6709/* Print a message describing any user-breakpoints set at PC. This
6c95b8df
PA
6710 concerns with logical breakpoints, so we match program spaces, not
6711 address spaces. */
c906108c
SS
6712
6713static void
6c95b8df
PA
6714describe_other_breakpoints (struct gdbarch *gdbarch,
6715 struct program_space *pspace, CORE_ADDR pc,
5af949e3 6716 struct obj_section *section, int thread)
c906108c 6717{
52f0bd74
AC
6718 int others = 0;
6719 struct breakpoint *b;
c906108c
SS
6720
6721 ALL_BREAKPOINTS (b)
672f9b60
KP
6722 others += (user_breakpoint_p (b)
6723 && breakpoint_has_pc (b, pspace, pc, section));
c906108c
SS
6724 if (others > 0)
6725 {
a3f17187
AC
6726 if (others == 1)
6727 printf_filtered (_("Note: breakpoint "));
6728 else /* if (others == ???) */
6729 printf_filtered (_("Note: breakpoints "));
c906108c 6730 ALL_BREAKPOINTS (b)
672f9b60 6731 if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
0d381245
VP
6732 {
6733 others--;
6734 printf_filtered ("%d", b->number);
6735 if (b->thread == -1 && thread != -1)
6736 printf_filtered (" (all threads)");
6737 else if (b->thread != -1)
6738 printf_filtered (" (thread %d)", b->thread);
6739 printf_filtered ("%s%s ",
059fb39f 6740 ((b->enable_state == bp_disabled
f8eba3c6 6741 || b->enable_state == bp_call_disabled)
0d381245 6742 ? " (disabled)"
0d381245
VP
6743 : ""),
6744 (others > 1) ? ","
6745 : ((others == 1) ? " and" : ""));
6746 }
6a831f06
PA
6747 current_uiout->message (_("also set at pc %ps.\n"),
6748 styled_string (address_style.style (),
6749 paddress (gdbarch, pc)));
c906108c
SS
6750 }
6751}
6752\f
c906108c 6753
cb1e4e32
PA
6754/* Return true iff it is meaningful to use the address member of LOC.
6755 For some breakpoint types, the locations' address members are
6756 irrelevant and it makes no sense to attempt to compare them to
6757 other addresses (or use them for any other purpose either).
2d134ed3 6758
cb1e4e32
PA
6759 More specifically, software watchpoints and catchpoints that are
6760 not backed by breakpoints always have a zero valued location
6761 address and we don't want to mark breakpoints of any of these types
6762 to be a duplicate of an actual breakpoint location at address
6763 zero. */
e4f237da 6764
cb1e4e32
PA
6765static bool
6766bl_address_is_meaningful (bp_location *loc)
e4f237da 6767{
cb1e4e32 6768 return loc->loc_type != bp_loc_other;
2d134ed3
PA
6769}
6770
6771/* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
6772 true if LOC1 and LOC2 represent the same watchpoint location. */
6773
6774static int
4a64f543
MS
6775watchpoint_locations_match (struct bp_location *loc1,
6776 struct bp_location *loc2)
2d134ed3 6777{
3a5c3e22
PA
6778 struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
6779 struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
6780
6781 /* Both of them must exist. */
6782 gdb_assert (w1 != NULL);
6783 gdb_assert (w2 != NULL);
2bdf28a0 6784
4a64f543
MS
6785 /* If the target can evaluate the condition expression in hardware,
6786 then we we need to insert both watchpoints even if they are at
6787 the same place. Otherwise the watchpoint will only trigger when
6788 the condition of whichever watchpoint was inserted evaluates to
6789 true, not giving a chance for GDB to check the condition of the
6790 other watchpoint. */
3a5c3e22 6791 if ((w1->cond_exp
4a64f543
MS
6792 && target_can_accel_watchpoint_condition (loc1->address,
6793 loc1->length,
0cf6dd15 6794 loc1->watchpoint_type,
4d01a485 6795 w1->cond_exp.get ()))
3a5c3e22 6796 || (w2->cond_exp
4a64f543
MS
6797 && target_can_accel_watchpoint_condition (loc2->address,
6798 loc2->length,
0cf6dd15 6799 loc2->watchpoint_type,
4d01a485 6800 w2->cond_exp.get ())))
0cf6dd15
TJB
6801 return 0;
6802
85d721b8
PA
6803 /* Note that this checks the owner's type, not the location's. In
6804 case the target does not support read watchpoints, but does
6805 support access watchpoints, we'll have bp_read_watchpoint
6806 watchpoints with hw_access locations. Those should be considered
6807 duplicates of hw_read locations. The hw_read locations will
6808 become hw_access locations later. */
2d134ed3
PA
6809 return (loc1->owner->type == loc2->owner->type
6810 && loc1->pspace->aspace == loc2->pspace->aspace
6811 && loc1->address == loc2->address
6812 && loc1->length == loc2->length);
e4f237da
KB
6813}
6814
31e77af2 6815/* See breakpoint.h. */
6c95b8df 6816
31e77af2 6817int
accd0bcd
YQ
6818breakpoint_address_match (const address_space *aspace1, CORE_ADDR addr1,
6819 const address_space *aspace2, CORE_ADDR addr2)
6c95b8df 6820{
f5656ead 6821 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
6c95b8df
PA
6822 || aspace1 == aspace2)
6823 && addr1 == addr2);
6824}
6825
f1310107
TJB
6826/* Returns true if {ASPACE2,ADDR2} falls within the range determined by
6827 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
6828 matches ASPACE2. On targets that have global breakpoints, the address
6829 space doesn't really matter. */
6830
6831static int
accd0bcd
YQ
6832breakpoint_address_match_range (const address_space *aspace1,
6833 CORE_ADDR addr1,
6834 int len1, const address_space *aspace2,
f1310107
TJB
6835 CORE_ADDR addr2)
6836{
f5656ead 6837 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
f1310107
TJB
6838 || aspace1 == aspace2)
6839 && addr2 >= addr1 && addr2 < addr1 + len1);
6840}
6841
6842/* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
6843 a ranged breakpoint. In most targets, a match happens only if ASPACE
6844 matches the breakpoint's address space. On targets that have global
6845 breakpoints, the address space doesn't really matter. */
6846
6847static int
6848breakpoint_location_address_match (struct bp_location *bl,
accd0bcd 6849 const address_space *aspace,
f1310107
TJB
6850 CORE_ADDR addr)
6851{
6852 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
6853 aspace, addr)
6854 || (bl->length
6855 && breakpoint_address_match_range (bl->pspace->aspace,
6856 bl->address, bl->length,
6857 aspace, addr)));
6858}
6859
d35ae833
PA
6860/* Returns true if the [ADDR,ADDR+LEN) range in ASPACE overlaps
6861 breakpoint BL. BL may be a ranged breakpoint. In most targets, a
6862 match happens only if ASPACE matches the breakpoint's address
6863 space. On targets that have global breakpoints, the address space
6864 doesn't really matter. */
6865
6866static int
6867breakpoint_location_address_range_overlap (struct bp_location *bl,
accd0bcd 6868 const address_space *aspace,
d35ae833
PA
6869 CORE_ADDR addr, int len)
6870{
6871 if (gdbarch_has_global_breakpoints (target_gdbarch ())
6872 || bl->pspace->aspace == aspace)
6873 {
6874 int bl_len = bl->length != 0 ? bl->length : 1;
6875
6876 if (mem_ranges_overlap (addr, len, bl->address, bl_len))
6877 return 1;
6878 }
6879 return 0;
6880}
6881
1e4d1764
YQ
6882/* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
6883 Then, if LOC1 and LOC2 represent the same tracepoint location, returns
6884 true, otherwise returns false. */
6885
6886static int
6887tracepoint_locations_match (struct bp_location *loc1,
6888 struct bp_location *loc2)
6889{
6890 if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
6891 /* Since tracepoint locations are never duplicated with others', tracepoint
6892 locations at the same address of different tracepoints are regarded as
6893 different locations. */
6894 return (loc1->address == loc2->address && loc1->owner == loc2->owner);
6895 else
6896 return 0;
6897}
6898
2d134ed3 6899/* Assuming LOC1 and LOC2's types' have meaningful target addresses
cb1e4e32 6900 (bl_address_is_meaningful), returns true if LOC1 and LOC2 represent
7f32a4d5
PA
6901 the same location. If SW_HW_BPS_MATCH is true, then software
6902 breakpoint locations and hardware breakpoint locations match,
6903 otherwise they don't. */
2d134ed3
PA
6904
6905static int
7f32a4d5
PA
6906breakpoint_locations_match (struct bp_location *loc1,
6907 struct bp_location *loc2,
6908 bool sw_hw_bps_match)
2d134ed3 6909{
2bdf28a0
JK
6910 int hw_point1, hw_point2;
6911
6912 /* Both of them must not be in moribund_locations. */
6913 gdb_assert (loc1->owner != NULL);
6914 gdb_assert (loc2->owner != NULL);
6915
6916 hw_point1 = is_hardware_watchpoint (loc1->owner);
6917 hw_point2 = is_hardware_watchpoint (loc2->owner);
2d134ed3
PA
6918
6919 if (hw_point1 != hw_point2)
6920 return 0;
6921 else if (hw_point1)
6922 return watchpoint_locations_match (loc1, loc2);
1e4d1764
YQ
6923 else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
6924 return tracepoint_locations_match (loc1, loc2);
2d134ed3 6925 else
7f32a4d5
PA
6926 /* We compare bp_location.length in order to cover ranged
6927 breakpoints. Keep this in sync with
6928 bp_location_is_less_than. */
f1310107
TJB
6929 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
6930 loc2->pspace->aspace, loc2->address)
7f32a4d5 6931 && (loc1->loc_type == loc2->loc_type || sw_hw_bps_match)
f1310107 6932 && loc1->length == loc2->length);
2d134ed3
PA
6933}
6934
76897487
KB
6935static void
6936breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
6937 int bnum, int have_bnum)
6938{
f63fbe86
MS
6939 /* The longest string possibly returned by hex_string_custom
6940 is 50 chars. These must be at least that big for safety. */
6941 char astr1[64];
6942 char astr2[64];
76897487 6943
bb599908
PH
6944 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
6945 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
76897487 6946 if (have_bnum)
8a3fe4f8 6947 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
76897487
KB
6948 bnum, astr1, astr2);
6949 else
8a3fe4f8 6950 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
76897487
KB
6951}
6952
4a64f543
MS
6953/* Adjust a breakpoint's address to account for architectural
6954 constraints on breakpoint placement. Return the adjusted address.
6955 Note: Very few targets require this kind of adjustment. For most
6956 targets, this function is simply the identity function. */
76897487
KB
6957
6958static CORE_ADDR
a6d9a66e
UW
6959adjust_breakpoint_address (struct gdbarch *gdbarch,
6960 CORE_ADDR bpaddr, enum bptype bptype)
76897487 6961{
a0de8c21
YQ
6962 if (bptype == bp_watchpoint
6963 || bptype == bp_hardware_watchpoint
6964 || bptype == bp_read_watchpoint
6965 || bptype == bp_access_watchpoint
6966 || bptype == bp_catchpoint)
88f7da05
KB
6967 {
6968 /* Watchpoints and the various bp_catch_* eventpoints should not
6969 have their addresses modified. */
6970 return bpaddr;
6971 }
7c16b83e
PA
6972 else if (bptype == bp_single_step)
6973 {
6974 /* Single-step breakpoints should not have their addresses
6975 modified. If there's any architectural constrain that
6976 applies to this address, then it should have already been
6977 taken into account when the breakpoint was created in the
6978 first place. If we didn't do this, stepping through e.g.,
6979 Thumb-2 IT blocks would break. */
6980 return bpaddr;
6981 }
76897487
KB
6982 else
6983 {
a0de8c21
YQ
6984 CORE_ADDR adjusted_bpaddr = bpaddr;
6985
6986 if (gdbarch_adjust_breakpoint_address_p (gdbarch))
6987 {
6988 /* Some targets have architectural constraints on the placement
6989 of breakpoint instructions. Obtain the adjusted address. */
6990 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
6991 }
76897487 6992
a0de8c21 6993 adjusted_bpaddr = address_significant (gdbarch, adjusted_bpaddr);
76897487
KB
6994
6995 /* An adjusted breakpoint address can significantly alter
6996 a user's expectations. Print a warning if an adjustment
6997 is required. */
6998 if (adjusted_bpaddr != bpaddr)
6999 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
7000
7001 return adjusted_bpaddr;
7002 }
7003}
7004
cb1e4e32
PA
7005static bp_loc_type
7006bp_location_from_bp_type (bptype type)
7cc221ef 7007{
cb1e4e32 7008 switch (type)
e049a4b5
DJ
7009 {
7010 case bp_breakpoint:
7c16b83e 7011 case bp_single_step:
e049a4b5
DJ
7012 case bp_until:
7013 case bp_finish:
7014 case bp_longjmp:
7015 case bp_longjmp_resume:
e2e4d78b 7016 case bp_longjmp_call_dummy:
186c406b
TT
7017 case bp_exception:
7018 case bp_exception_resume:
e049a4b5 7019 case bp_step_resume:
2c03e5be 7020 case bp_hp_step_resume:
e049a4b5
DJ
7021 case bp_watchpoint_scope:
7022 case bp_call_dummy:
aa7d318d 7023 case bp_std_terminate:
e049a4b5
DJ
7024 case bp_shlib_event:
7025 case bp_thread_event:
7026 case bp_overlay_event:
4efc6507 7027 case bp_jit_event:
0fd8e87f 7028 case bp_longjmp_master:
aa7d318d 7029 case bp_std_terminate_master:
186c406b 7030 case bp_exception_master:
0e30163f
JK
7031 case bp_gnu_ifunc_resolver:
7032 case bp_gnu_ifunc_resolver_return:
e7e0cddf 7033 case bp_dprintf:
cb1e4e32 7034 return bp_loc_software_breakpoint;
e049a4b5 7035 case bp_hardware_breakpoint:
cb1e4e32 7036 return bp_loc_hardware_breakpoint;
e049a4b5
DJ
7037 case bp_hardware_watchpoint:
7038 case bp_read_watchpoint:
7039 case bp_access_watchpoint:
cb1e4e32 7040 return bp_loc_hardware_watchpoint;
e049a4b5 7041 case bp_watchpoint:
ce78b96d 7042 case bp_catchpoint:
15c3d785
PA
7043 case bp_tracepoint:
7044 case bp_fast_tracepoint:
0fb4aa4b 7045 case bp_static_tracepoint:
cb1e4e32 7046 return bp_loc_other;
e049a4b5 7047 default:
e2e0b3e5 7048 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
e049a4b5 7049 }
cb1e4e32
PA
7050}
7051
7052bp_location::bp_location (breakpoint *owner, bp_loc_type type)
7053{
7054 this->owner = owner;
7055 this->cond_bytecode = NULL;
7056 this->shlib_disabled = 0;
7057 this->enabled = 1;
b5fa468f 7058 this->disabled_by_cond = false;
cb1e4e32
PA
7059
7060 this->loc_type = type;
e049a4b5 7061
cb1e4e32
PA
7062 if (this->loc_type == bp_loc_software_breakpoint
7063 || this->loc_type == bp_loc_hardware_breakpoint)
7064 mark_breakpoint_location_modified (this);
7065
7066 this->refc = 1;
7067}
7068
7069bp_location::bp_location (breakpoint *owner)
7070 : bp_location::bp_location (owner,
7071 bp_location_from_bp_type (owner->type))
7072{
28010a5d
PA
7073}
7074
7075/* Allocate a struct bp_location. */
7076
7077static struct bp_location *
7078allocate_bp_location (struct breakpoint *bpt)
7079{
348d480f
PA
7080 return bpt->ops->allocate_location (bpt);
7081}
7cc221ef 7082
f431efe5
PA
7083static void
7084free_bp_location (struct bp_location *loc)
fe3f5fa8 7085{
4d01a485 7086 delete loc;
fe3f5fa8
VP
7087}
7088
f431efe5
PA
7089/* Increment reference count. */
7090
7091static void
7092incref_bp_location (struct bp_location *bl)
7093{
7094 ++bl->refc;
7095}
7096
7097/* Decrement reference count. If the reference count reaches 0,
7098 destroy the bp_location. Sets *BLP to NULL. */
7099
7100static void
7101decref_bp_location (struct bp_location **blp)
7102{
0807b50c
PA
7103 gdb_assert ((*blp)->refc > 0);
7104
f431efe5
PA
7105 if (--(*blp)->refc == 0)
7106 free_bp_location (*blp);
7107 *blp = NULL;
7108}
7109
346774a9 7110/* Add breakpoint B at the end of the global breakpoint chain. */
c906108c 7111
b270e6f9
TT
7112static breakpoint *
7113add_to_breakpoint_chain (std::unique_ptr<breakpoint> &&b)
c906108c 7114{
346774a9 7115 struct breakpoint *b1;
b270e6f9 7116 struct breakpoint *result = b.get ();
c906108c 7117
346774a9
PA
7118 /* Add this breakpoint to the end of the chain so that a list of
7119 breakpoints will come out in order of increasing numbers. */
7120
7121 b1 = breakpoint_chain;
7122 if (b1 == 0)
b270e6f9 7123 breakpoint_chain = b.release ();
346774a9
PA
7124 else
7125 {
7126 while (b1->next)
7127 b1 = b1->next;
b270e6f9 7128 b1->next = b.release ();
346774a9 7129 }
b270e6f9
TT
7130
7131 return result;
346774a9
PA
7132}
7133
7134/* Initializes breakpoint B with type BPTYPE and no locations yet. */
7135
7136static void
7137init_raw_breakpoint_without_location (struct breakpoint *b,
7138 struct gdbarch *gdbarch,
28010a5d 7139 enum bptype bptype,
c0a91b2b 7140 const struct breakpoint_ops *ops)
346774a9 7141{
348d480f
PA
7142 gdb_assert (ops != NULL);
7143
28010a5d 7144 b->ops = ops;
4d28f7a8 7145 b->type = bptype;
a6d9a66e 7146 b->gdbarch = gdbarch;
c906108c
SS
7147 b->language = current_language->la_language;
7148 b->input_radix = input_radix;
d0fb5eae 7149 b->related_breakpoint = b;
346774a9
PA
7150}
7151
7152/* Helper to set_raw_breakpoint below. Creates a breakpoint
7153 that has type BPTYPE and has no locations as yet. */
346774a9
PA
7154
7155static struct breakpoint *
7156set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
348d480f 7157 enum bptype bptype,
c0a91b2b 7158 const struct breakpoint_ops *ops)
346774a9 7159{
3b0871f4 7160 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (bptype);
346774a9 7161
3b0871f4 7162 init_raw_breakpoint_without_location (b.get (), gdbarch, bptype, ops);
b270e6f9 7163 return add_to_breakpoint_chain (std::move (b));
0d381245
VP
7164}
7165
0ba852ab 7166/* Initialize loc->function_name. */
0e30163f 7167
0d381245 7168static void
0ba852ab 7169set_breakpoint_location_function (struct bp_location *loc)
0d381245 7170{
2bdf28a0
JK
7171 gdb_assert (loc->owner != NULL);
7172
0d381245 7173 if (loc->owner->type == bp_breakpoint
1042e4c0 7174 || loc->owner->type == bp_hardware_breakpoint
d77f58be 7175 || is_tracepoint (loc->owner))
0d381245 7176 {
2c02bd72 7177 const char *function_name;
0e30163f 7178
3467ec66 7179 if (loc->msymbol != NULL
f50776aa 7180 && (MSYMBOL_TYPE (loc->msymbol) == mst_text_gnu_ifunc
0ba852ab 7181 || MSYMBOL_TYPE (loc->msymbol) == mst_data_gnu_ifunc))
0e30163f
JK
7182 {
7183 struct breakpoint *b = loc->owner;
7184
c9d95fa3 7185 function_name = loc->msymbol->linkage_name ();
3467ec66
PA
7186
7187 if (b->type == bp_breakpoint && b->loc == loc
7188 && loc->next == NULL && b->related_breakpoint == b)
0e30163f
JK
7189 {
7190 /* Create only the whole new breakpoint of this type but do not
7191 mess more complicated breakpoints with multiple locations. */
7192 b->type = bp_gnu_ifunc_resolver;
6a3a010b
MR
7193 /* Remember the resolver's address for use by the return
7194 breakpoint. */
3467ec66 7195 loc->related_address = loc->address;
0e30163f
JK
7196 }
7197 }
3467ec66
PA
7198 else
7199 find_pc_partial_function (loc->address, &function_name, NULL, NULL);
0e30163f 7200
2c02bd72
DE
7201 if (function_name)
7202 loc->function_name = xstrdup (function_name);
0d381245
VP
7203 }
7204}
7205
a6d9a66e 7206/* Attempt to determine architecture of location identified by SAL. */
1bfeeb0f 7207struct gdbarch *
a6d9a66e
UW
7208get_sal_arch (struct symtab_and_line sal)
7209{
7210 if (sal.section)
08feed99 7211 return sal.section->objfile->arch ();
a6d9a66e 7212 if (sal.symtab)
08feed99 7213 return SYMTAB_OBJFILE (sal.symtab)->arch ();
a6d9a66e
UW
7214
7215 return NULL;
7216}
7217
346774a9
PA
7218/* Low level routine for partially initializing a breakpoint of type
7219 BPTYPE. The newly created breakpoint's address, section, source
c56053d2 7220 file name, and line number are provided by SAL.
0d381245
VP
7221
7222 It is expected that the caller will complete the initialization of
7223 the newly created breakpoint struct as well as output any status
c56053d2 7224 information regarding the creation of a new breakpoint. */
0d381245 7225
346774a9
PA
7226static void
7227init_raw_breakpoint (struct breakpoint *b, struct gdbarch *gdbarch,
28010a5d 7228 struct symtab_and_line sal, enum bptype bptype,
c0a91b2b 7229 const struct breakpoint_ops *ops)
0d381245 7230{
28010a5d 7231 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
346774a9 7232
3742cc8b 7233 add_location_to_breakpoint (b, &sal);
0d381245 7234
6c95b8df
PA
7235 if (bptype != bp_catchpoint)
7236 gdb_assert (sal.pspace != NULL);
7237
f8eba3c6
TT
7238 /* Store the program space that was used to set the breakpoint,
7239 except for ordinary breakpoints, which are independent of the
7240 program space. */
7241 if (bptype != bp_breakpoint && bptype != bp_hardware_breakpoint)
7242 b->pspace = sal.pspace;
346774a9 7243}
c906108c 7244
346774a9
PA
7245/* set_raw_breakpoint is a low level routine for allocating and
7246 partially initializing a breakpoint of type BPTYPE. The newly
7247 created breakpoint's address, section, source file name, and line
7248 number are provided by SAL. The newly created and partially
7249 initialized breakpoint is added to the breakpoint chain and
7250 is also returned as the value of this function.
7251
7252 It is expected that the caller will complete the initialization of
7253 the newly created breakpoint struct as well as output any status
7254 information regarding the creation of a new breakpoint. In
7255 particular, set_raw_breakpoint does NOT set the breakpoint
7256 number! Care should be taken to not allow an error to occur
7257 prior to completing the initialization of the breakpoint. If this
7258 should happen, a bogus breakpoint will be left on the chain. */
7259
7260struct breakpoint *
7261set_raw_breakpoint (struct gdbarch *gdbarch,
348d480f 7262 struct symtab_and_line sal, enum bptype bptype,
c0a91b2b 7263 const struct breakpoint_ops *ops)
346774a9 7264{
3b0871f4 7265 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (bptype);
346774a9 7266
3b0871f4 7267 init_raw_breakpoint (b.get (), gdbarch, sal, bptype, ops);
b270e6f9 7268 return add_to_breakpoint_chain (std::move (b));
c906108c
SS
7269}
7270
53a5351d 7271/* Call this routine when stepping and nexting to enable a breakpoint
186c406b
TT
7272 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
7273 initiated the operation. */
c906108c
SS
7274
7275void
186c406b 7276set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
c906108c 7277{
35df4500 7278 struct breakpoint *b, *b_tmp;
5d5658a1 7279 int thread = tp->global_num;
0fd8e87f
UW
7280
7281 /* To avoid having to rescan all objfile symbols at every step,
7282 we maintain a list of continually-inserted but always disabled
7283 longjmp "master" breakpoints. Here, we simply create momentary
7284 clones of those and enable them for the requested thread. */
35df4500 7285 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df 7286 if (b->pspace == current_program_space
186c406b
TT
7287 && (b->type == bp_longjmp_master
7288 || b->type == bp_exception_master))
0fd8e87f 7289 {
06edf0c0
PA
7290 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
7291 struct breakpoint *clone;
cc59ec59 7292
e2e4d78b
JK
7293 /* longjmp_breakpoint_ops ensures INITIATING_FRAME is cleared again
7294 after their removal. */
06edf0c0 7295 clone = momentary_breakpoint_from_master (b, type,
c1fc2657 7296 &momentary_breakpoint_ops, 1);
0fd8e87f
UW
7297 clone->thread = thread;
7298 }
186c406b
TT
7299
7300 tp->initiating_frame = frame;
c906108c
SS
7301}
7302
611c83ae 7303/* Delete all longjmp breakpoints from THREAD. */
c906108c 7304void
611c83ae 7305delete_longjmp_breakpoint (int thread)
c906108c 7306{
35df4500 7307 struct breakpoint *b, *b_tmp;
c906108c 7308
35df4500 7309 ALL_BREAKPOINTS_SAFE (b, b_tmp)
186c406b 7310 if (b->type == bp_longjmp || b->type == bp_exception)
611c83ae
PA
7311 {
7312 if (b->thread == thread)
7313 delete_breakpoint (b);
7314 }
c906108c
SS
7315}
7316
f59f708a
PA
7317void
7318delete_longjmp_breakpoint_at_next_stop (int thread)
7319{
7320 struct breakpoint *b, *b_tmp;
7321
7322 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7323 if (b->type == bp_longjmp || b->type == bp_exception)
7324 {
7325 if (b->thread == thread)
7326 b->disposition = disp_del_at_next_stop;
7327 }
7328}
7329
e2e4d78b
JK
7330/* Place breakpoints of type bp_longjmp_call_dummy to catch longjmp for
7331 INFERIOR_PTID thread. Chain them all by RELATED_BREAKPOINT and return
7332 pointer to any of them. Return NULL if this system cannot place longjmp
7333 breakpoints. */
7334
7335struct breakpoint *
7336set_longjmp_breakpoint_for_call_dummy (void)
7337{
7338 struct breakpoint *b, *retval = NULL;
7339
7340 ALL_BREAKPOINTS (b)
7341 if (b->pspace == current_program_space && b->type == bp_longjmp_master)
7342 {
7343 struct breakpoint *new_b;
7344
7345 new_b = momentary_breakpoint_from_master (b, bp_longjmp_call_dummy,
a1aa2221
LM
7346 &momentary_breakpoint_ops,
7347 1);
00431a78 7348 new_b->thread = inferior_thread ()->global_num;
e2e4d78b
JK
7349
7350 /* Link NEW_B into the chain of RETVAL breakpoints. */
7351
7352 gdb_assert (new_b->related_breakpoint == new_b);
7353 if (retval == NULL)
7354 retval = new_b;
7355 new_b->related_breakpoint = retval;
7356 while (retval->related_breakpoint != new_b->related_breakpoint)
7357 retval = retval->related_breakpoint;
7358 retval->related_breakpoint = new_b;
7359 }
7360
7361 return retval;
7362}
7363
7364/* Verify all existing dummy frames and their associated breakpoints for
b67a2c6f 7365 TP. Remove those which can no longer be found in the current frame
e2e4d78b
JK
7366 stack.
7367
7368 You should call this function only at places where it is safe to currently
7369 unwind the whole stack. Failed stack unwind would discard live dummy
7370 frames. */
7371
7372void
b67a2c6f 7373check_longjmp_breakpoint_for_call_dummy (struct thread_info *tp)
e2e4d78b
JK
7374{
7375 struct breakpoint *b, *b_tmp;
7376
7377 ALL_BREAKPOINTS_SAFE (b, b_tmp)
5d5658a1 7378 if (b->type == bp_longjmp_call_dummy && b->thread == tp->global_num)
e2e4d78b
JK
7379 {
7380 struct breakpoint *dummy_b = b->related_breakpoint;
7381
7382 while (dummy_b != b && dummy_b->type != bp_call_dummy)
7383 dummy_b = dummy_b->related_breakpoint;
7384 if (dummy_b->type != bp_call_dummy
7385 || frame_find_by_id (dummy_b->frame_id) != NULL)
7386 continue;
7387
00431a78 7388 dummy_frame_discard (dummy_b->frame_id, tp);
e2e4d78b
JK
7389
7390 while (b->related_breakpoint != b)
7391 {
7392 if (b_tmp == b->related_breakpoint)
7393 b_tmp = b->related_breakpoint->next;
7394 delete_breakpoint (b->related_breakpoint);
7395 }
7396 delete_breakpoint (b);
7397 }
7398}
7399
1900040c
MS
7400void
7401enable_overlay_breakpoints (void)
7402{
52f0bd74 7403 struct breakpoint *b;
1900040c
MS
7404
7405 ALL_BREAKPOINTS (b)
7406 if (b->type == bp_overlay_event)
7407 {
7408 b->enable_state = bp_enabled;
44702360 7409 update_global_location_list (UGLL_MAY_INSERT);
c02f5703 7410 overlay_events_enabled = 1;
1900040c
MS
7411 }
7412}
7413
7414void
7415disable_overlay_breakpoints (void)
7416{
52f0bd74 7417 struct breakpoint *b;
1900040c
MS
7418
7419 ALL_BREAKPOINTS (b)
7420 if (b->type == bp_overlay_event)
7421 {
7422 b->enable_state = bp_disabled;
44702360 7423 update_global_location_list (UGLL_DONT_INSERT);
c02f5703 7424 overlay_events_enabled = 0;
1900040c
MS
7425 }
7426}
7427
aa7d318d
TT
7428/* Set an active std::terminate breakpoint for each std::terminate
7429 master breakpoint. */
7430void
7431set_std_terminate_breakpoint (void)
7432{
35df4500 7433 struct breakpoint *b, *b_tmp;
aa7d318d 7434
35df4500 7435 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
7436 if (b->pspace == current_program_space
7437 && b->type == bp_std_terminate_master)
7438 {
06edf0c0 7439 momentary_breakpoint_from_master (b, bp_std_terminate,
a1aa2221 7440 &momentary_breakpoint_ops, 1);
aa7d318d
TT
7441 }
7442}
7443
7444/* Delete all the std::terminate breakpoints. */
7445void
7446delete_std_terminate_breakpoint (void)
7447{
35df4500 7448 struct breakpoint *b, *b_tmp;
aa7d318d 7449
35df4500 7450 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
7451 if (b->type == bp_std_terminate)
7452 delete_breakpoint (b);
7453}
7454
c4093a6a 7455struct breakpoint *
a6d9a66e 7456create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
c4093a6a
JM
7457{
7458 struct breakpoint *b;
c4093a6a 7459
06edf0c0
PA
7460 b = create_internal_breakpoint (gdbarch, address, bp_thread_event,
7461 &internal_breakpoint_ops);
7462
b5de0fa7 7463 b->enable_state = bp_enabled;
f00aae0f 7464 /* location has to be used or breakpoint_re_set will delete me. */
d28cd78a 7465 b->location = new_address_location (b->loc->address, NULL, 0);
c4093a6a 7466
44702360 7467 update_global_location_list_nothrow (UGLL_MAY_INSERT);
74960c60 7468
c4093a6a
JM
7469 return b;
7470}
7471
0101ce28
JJ
7472struct lang_and_radix
7473 {
7474 enum language lang;
7475 int radix;
7476 };
7477
4efc6507
DE
7478/* Create a breakpoint for JIT code registration and unregistration. */
7479
7480struct breakpoint *
7481create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7482{
2a7f3dff
PA
7483 return create_internal_breakpoint (gdbarch, address, bp_jit_event,
7484 &internal_breakpoint_ops);
4efc6507 7485}
0101ce28 7486
03673fc7
PP
7487/* Remove JIT code registration and unregistration breakpoint(s). */
7488
7489void
7490remove_jit_event_breakpoints (void)
7491{
7492 struct breakpoint *b, *b_tmp;
7493
7494 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7495 if (b->type == bp_jit_event
7496 && b->loc->pspace == current_program_space)
7497 delete_breakpoint (b);
7498}
7499
cae688ec
JJ
7500void
7501remove_solib_event_breakpoints (void)
7502{
35df4500 7503 struct breakpoint *b, *b_tmp;
cae688ec 7504
35df4500 7505 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df
PA
7506 if (b->type == bp_shlib_event
7507 && b->loc->pspace == current_program_space)
cae688ec
JJ
7508 delete_breakpoint (b);
7509}
7510
f37f681c
PA
7511/* See breakpoint.h. */
7512
7513void
7514remove_solib_event_breakpoints_at_next_stop (void)
7515{
7516 struct breakpoint *b, *b_tmp;
7517
7518 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7519 if (b->type == bp_shlib_event
7520 && b->loc->pspace == current_program_space)
7521 b->disposition = disp_del_at_next_stop;
7522}
7523
04086b45
PA
7524/* Helper for create_solib_event_breakpoint /
7525 create_and_insert_solib_event_breakpoint. Allows specifying which
7526 INSERT_MODE to pass through to update_global_location_list. */
7527
7528static struct breakpoint *
7529create_solib_event_breakpoint_1 (struct gdbarch *gdbarch, CORE_ADDR address,
7530 enum ugll_insert_mode insert_mode)
cae688ec
JJ
7531{
7532 struct breakpoint *b;
7533
06edf0c0
PA
7534 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event,
7535 &internal_breakpoint_ops);
04086b45 7536 update_global_location_list_nothrow (insert_mode);
cae688ec
JJ
7537 return b;
7538}
7539
04086b45
PA
7540struct breakpoint *
7541create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7542{
7543 return create_solib_event_breakpoint_1 (gdbarch, address, UGLL_MAY_INSERT);
7544}
7545
f37f681c
PA
7546/* See breakpoint.h. */
7547
7548struct breakpoint *
7549create_and_insert_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7550{
7551 struct breakpoint *b;
7552
04086b45
PA
7553 /* Explicitly tell update_global_location_list to insert
7554 locations. */
7555 b = create_solib_event_breakpoint_1 (gdbarch, address, UGLL_INSERT);
f37f681c
PA
7556 if (!b->loc->inserted)
7557 {
7558 delete_breakpoint (b);
7559 return NULL;
7560 }
7561 return b;
7562}
7563
cae688ec
JJ
7564/* Disable any breakpoints that are on code in shared libraries. Only
7565 apply to enabled breakpoints, disabled ones can just stay disabled. */
7566
7567void
cb851954 7568disable_breakpoints_in_shlibs (void)
cae688ec 7569{
876fa593 7570 struct bp_location *loc, **locp_tmp;
cae688ec 7571
876fa593 7572 ALL_BP_LOCATIONS (loc, locp_tmp)
cae688ec 7573 {
2bdf28a0 7574 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 7575 struct breakpoint *b = loc->owner;
2bdf28a0 7576
4a64f543
MS
7577 /* We apply the check to all breakpoints, including disabled for
7578 those with loc->duplicate set. This is so that when breakpoint
7579 becomes enabled, or the duplicate is removed, gdb will try to
7580 insert all breakpoints. If we don't set shlib_disabled here,
7581 we'll try to insert those breakpoints and fail. */
1042e4c0 7582 if (((b->type == bp_breakpoint)
508ccb1f 7583 || (b->type == bp_jit_event)
1042e4c0 7584 || (b->type == bp_hardware_breakpoint)
d77f58be 7585 || (is_tracepoint (b)))
6c95b8df 7586 && loc->pspace == current_program_space
0d381245 7587 && !loc->shlib_disabled
6c95b8df 7588 && solib_name_from_address (loc->pspace, loc->address)
a77053c2 7589 )
0d381245
VP
7590 {
7591 loc->shlib_disabled = 1;
7592 }
cae688ec
JJ
7593 }
7594}
7595
63644780
NB
7596/* Disable any breakpoints and tracepoints that are in SOLIB upon
7597 notification of unloaded_shlib. Only apply to enabled breakpoints,
7598 disabled ones can just stay disabled. */
84acb35a 7599
75149521 7600static void
84acb35a
JJ
7601disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
7602{
876fa593 7603 struct bp_location *loc, **locp_tmp;
84acb35a
JJ
7604 int disabled_shlib_breaks = 0;
7605
876fa593 7606 ALL_BP_LOCATIONS (loc, locp_tmp)
84acb35a 7607 {
2bdf28a0 7608 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 7609 struct breakpoint *b = loc->owner;
cc59ec59 7610
1e4d1764 7611 if (solib->pspace == loc->pspace
e2dd7057 7612 && !loc->shlib_disabled
1e4d1764
YQ
7613 && (((b->type == bp_breakpoint
7614 || b->type == bp_jit_event
7615 || b->type == bp_hardware_breakpoint)
7616 && (loc->loc_type == bp_loc_hardware_breakpoint
7617 || loc->loc_type == bp_loc_software_breakpoint))
7618 || is_tracepoint (b))
e2dd7057 7619 && solib_contains_address_p (solib, loc->address))
84acb35a 7620 {
e2dd7057
PP
7621 loc->shlib_disabled = 1;
7622 /* At this point, we cannot rely on remove_breakpoint
7623 succeeding so we must mark the breakpoint as not inserted
7624 to prevent future errors occurring in remove_breakpoints. */
7625 loc->inserted = 0;
8d3788bd
VP
7626
7627 /* This may cause duplicate notifications for the same breakpoint. */
76727919 7628 gdb::observers::breakpoint_modified.notify (b);
8d3788bd 7629
e2dd7057
PP
7630 if (!disabled_shlib_breaks)
7631 {
223ffa71 7632 target_terminal::ours_for_output ();
3e43a32a
MS
7633 warning (_("Temporarily disabling breakpoints "
7634 "for unloaded shared library \"%s\""),
e2dd7057 7635 solib->so_name);
84acb35a 7636 }
e2dd7057 7637 disabled_shlib_breaks = 1;
84acb35a
JJ
7638 }
7639 }
84acb35a
JJ
7640}
7641
63644780
NB
7642/* Disable any breakpoints and tracepoints in OBJFILE upon
7643 notification of free_objfile. Only apply to enabled breakpoints,
7644 disabled ones can just stay disabled. */
7645
7646static void
7647disable_breakpoints_in_freed_objfile (struct objfile *objfile)
7648{
7649 struct breakpoint *b;
7650
7651 if (objfile == NULL)
7652 return;
7653
d03de421
PA
7654 /* OBJF_SHARED|OBJF_USERLOADED objfiles are dynamic modules manually
7655 managed by the user with add-symbol-file/remove-symbol-file.
7656 Similarly to how breakpoints in shared libraries are handled in
7657 response to "nosharedlibrary", mark breakpoints in such modules
08351840
PA
7658 shlib_disabled so they end up uninserted on the next global
7659 location list update. Shared libraries not loaded by the user
7660 aren't handled here -- they're already handled in
7661 disable_breakpoints_in_unloaded_shlib, called by solib.c's
7662 solib_unloaded observer. We skip objfiles that are not
d03de421
PA
7663 OBJF_SHARED as those aren't considered dynamic objects (e.g. the
7664 main objfile). */
7665 if ((objfile->flags & OBJF_SHARED) == 0
7666 || (objfile->flags & OBJF_USERLOADED) == 0)
63644780
NB
7667 return;
7668
7669 ALL_BREAKPOINTS (b)
7670 {
7671 struct bp_location *loc;
7672 int bp_modified = 0;
7673
7674 if (!is_breakpoint (b) && !is_tracepoint (b))
7675 continue;
7676
7677 for (loc = b->loc; loc != NULL; loc = loc->next)
7678 {
7679 CORE_ADDR loc_addr = loc->address;
7680
7681 if (loc->loc_type != bp_loc_hardware_breakpoint
7682 && loc->loc_type != bp_loc_software_breakpoint)
7683 continue;
7684
7685 if (loc->shlib_disabled != 0)
7686 continue;
7687
7688 if (objfile->pspace != loc->pspace)
7689 continue;
7690
7691 if (loc->loc_type != bp_loc_hardware_breakpoint
7692 && loc->loc_type != bp_loc_software_breakpoint)
7693 continue;
7694
7695 if (is_addr_in_objfile (loc_addr, objfile))
7696 {
7697 loc->shlib_disabled = 1;
08351840
PA
7698 /* At this point, we don't know whether the object was
7699 unmapped from the inferior or not, so leave the
7700 inserted flag alone. We'll handle failure to
7701 uninsert quietly, in case the object was indeed
7702 unmapped. */
63644780
NB
7703
7704 mark_breakpoint_location_modified (loc);
7705
7706 bp_modified = 1;
7707 }
7708 }
7709
7710 if (bp_modified)
76727919 7711 gdb::observers::breakpoint_modified.notify (b);
63644780
NB
7712 }
7713}
7714
ce78b96d
JB
7715/* FORK & VFORK catchpoints. */
7716
e29a4733 7717/* An instance of this type is used to represent a fork or vfork
c1fc2657
SM
7718 catchpoint. A breakpoint is really of this type iff its ops pointer points
7719 to CATCH_FORK_BREAKPOINT_OPS. */
e29a4733 7720
c1fc2657 7721struct fork_catchpoint : public breakpoint
e29a4733 7722{
e29a4733
PA
7723 /* Process id of a child process whose forking triggered this
7724 catchpoint. This field is only valid immediately after this
7725 catchpoint has triggered. */
7726 ptid_t forked_inferior_pid;
7727};
7728
4a64f543
MS
7729/* Implement the "insert" breakpoint_ops method for fork
7730 catchpoints. */
ce78b96d 7731
77b06cd7
TJB
7732static int
7733insert_catch_fork (struct bp_location *bl)
ce78b96d 7734{
e99b03dc 7735 return target_insert_fork_catchpoint (inferior_ptid.pid ());
ce78b96d
JB
7736}
7737
4a64f543
MS
7738/* Implement the "remove" breakpoint_ops method for fork
7739 catchpoints. */
ce78b96d
JB
7740
7741static int
73971819 7742remove_catch_fork (struct bp_location *bl, enum remove_bp_reason reason)
ce78b96d 7743{
e99b03dc 7744 return target_remove_fork_catchpoint (inferior_ptid.pid ());
ce78b96d
JB
7745}
7746
7747/* Implement the "breakpoint_hit" breakpoint_ops method for fork
7748 catchpoints. */
7749
7750static int
f1310107 7751breakpoint_hit_catch_fork (const struct bp_location *bl,
bd522513 7752 const address_space *aspace, CORE_ADDR bp_addr,
09ac7c10 7753 const struct target_waitstatus *ws)
ce78b96d 7754{
e29a4733
PA
7755 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7756
f90263c1
TT
7757 if (ws->kind != TARGET_WAITKIND_FORKED)
7758 return 0;
7759
7760 c->forked_inferior_pid = ws->value.related_pid;
7761 return 1;
ce78b96d
JB
7762}
7763
4a64f543
MS
7764/* Implement the "print_it" breakpoint_ops method for fork
7765 catchpoints. */
ce78b96d
JB
7766
7767static enum print_stop_action
348d480f 7768print_it_catch_fork (bpstat bs)
ce78b96d 7769{
36dfb11c 7770 struct ui_out *uiout = current_uiout;
348d480f
PA
7771 struct breakpoint *b = bs->breakpoint_at;
7772 struct fork_catchpoint *c = (struct fork_catchpoint *) bs->breakpoint_at;
e29a4733 7773
ce78b96d 7774 annotate_catchpoint (b->number);
f303dbd6 7775 maybe_print_thread_hit_breakpoint (uiout);
36dfb11c 7776 if (b->disposition == disp_del)
112e8700 7777 uiout->text ("Temporary catchpoint ");
36dfb11c 7778 else
112e8700
SM
7779 uiout->text ("Catchpoint ");
7780 if (uiout->is_mi_like_p ())
36dfb11c 7781 {
112e8700
SM
7782 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_FORK));
7783 uiout->field_string ("disp", bpdisp_text (b->disposition));
36dfb11c 7784 }
381befee 7785 uiout->field_signed ("bkptno", b->number);
112e8700 7786 uiout->text (" (forked process ");
381befee 7787 uiout->field_signed ("newpid", c->forked_inferior_pid.pid ());
112e8700 7788 uiout->text ("), ");
ce78b96d
JB
7789 return PRINT_SRC_AND_LOC;
7790}
7791
4a64f543
MS
7792/* Implement the "print_one" breakpoint_ops method for fork
7793 catchpoints. */
ce78b96d
JB
7794
7795static void
a6d9a66e 7796print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 7797{
e29a4733 7798 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
79a45b7d 7799 struct value_print_options opts;
79a45e25 7800 struct ui_out *uiout = current_uiout;
79a45b7d
TT
7801
7802 get_user_print_options (&opts);
7803
4a64f543
MS
7804 /* Field 4, the address, is omitted (which makes the columns not
7805 line up too nicely with the headers, but the effect is relatively
7806 readable). */
79a45b7d 7807 if (opts.addressprint)
112e8700 7808 uiout->field_skip ("addr");
ce78b96d 7809 annotate_field (5);
112e8700 7810 uiout->text ("fork");
d7e15655 7811 if (c->forked_inferior_pid != null_ptid)
ce78b96d 7812 {
112e8700 7813 uiout->text (", process ");
381befee 7814 uiout->field_signed ("what", c->forked_inferior_pid.pid ());
112e8700 7815 uiout->spaces (1);
ce78b96d 7816 }
8ac3646f 7817
112e8700
SM
7818 if (uiout->is_mi_like_p ())
7819 uiout->field_string ("catch-type", "fork");
ce78b96d
JB
7820}
7821
7822/* Implement the "print_mention" breakpoint_ops method for fork
7823 catchpoints. */
7824
7825static void
7826print_mention_catch_fork (struct breakpoint *b)
7827{
7828 printf_filtered (_("Catchpoint %d (fork)"), b->number);
7829}
7830
6149aea9
PA
7831/* Implement the "print_recreate" breakpoint_ops method for fork
7832 catchpoints. */
7833
7834static void
7835print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
7836{
7837 fprintf_unfiltered (fp, "catch fork");
d9b3f62e 7838 print_recreate_thread (b, fp);
6149aea9
PA
7839}
7840
ce78b96d
JB
7841/* The breakpoint_ops structure to be used in fork catchpoints. */
7842
2060206e 7843static struct breakpoint_ops catch_fork_breakpoint_ops;
ce78b96d 7844
4a64f543
MS
7845/* Implement the "insert" breakpoint_ops method for vfork
7846 catchpoints. */
ce78b96d 7847
77b06cd7
TJB
7848static int
7849insert_catch_vfork (struct bp_location *bl)
ce78b96d 7850{
e99b03dc 7851 return target_insert_vfork_catchpoint (inferior_ptid.pid ());
ce78b96d
JB
7852}
7853
4a64f543
MS
7854/* Implement the "remove" breakpoint_ops method for vfork
7855 catchpoints. */
ce78b96d
JB
7856
7857static int
73971819 7858remove_catch_vfork (struct bp_location *bl, enum remove_bp_reason reason)
ce78b96d 7859{
e99b03dc 7860 return target_remove_vfork_catchpoint (inferior_ptid.pid ());
ce78b96d
JB
7861}
7862
7863/* Implement the "breakpoint_hit" breakpoint_ops method for vfork
7864 catchpoints. */
7865
7866static int
f1310107 7867breakpoint_hit_catch_vfork (const struct bp_location *bl,
bd522513 7868 const address_space *aspace, CORE_ADDR bp_addr,
09ac7c10 7869 const struct target_waitstatus *ws)
ce78b96d 7870{
e29a4733
PA
7871 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7872
f90263c1
TT
7873 if (ws->kind != TARGET_WAITKIND_VFORKED)
7874 return 0;
7875
7876 c->forked_inferior_pid = ws->value.related_pid;
7877 return 1;
ce78b96d
JB
7878}
7879
4a64f543
MS
7880/* Implement the "print_it" breakpoint_ops method for vfork
7881 catchpoints. */
ce78b96d
JB
7882
7883static enum print_stop_action
348d480f 7884print_it_catch_vfork (bpstat bs)
ce78b96d 7885{
36dfb11c 7886 struct ui_out *uiout = current_uiout;
348d480f 7887 struct breakpoint *b = bs->breakpoint_at;
e29a4733
PA
7888 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
7889
ce78b96d 7890 annotate_catchpoint (b->number);
f303dbd6 7891 maybe_print_thread_hit_breakpoint (uiout);
36dfb11c 7892 if (b->disposition == disp_del)
112e8700 7893 uiout->text ("Temporary catchpoint ");
36dfb11c 7894 else
112e8700
SM
7895 uiout->text ("Catchpoint ");
7896 if (uiout->is_mi_like_p ())
36dfb11c 7897 {
112e8700
SM
7898 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_VFORK));
7899 uiout->field_string ("disp", bpdisp_text (b->disposition));
36dfb11c 7900 }
381befee 7901 uiout->field_signed ("bkptno", b->number);
112e8700 7902 uiout->text (" (vforked process ");
381befee 7903 uiout->field_signed ("newpid", c->forked_inferior_pid.pid ());
112e8700 7904 uiout->text ("), ");
ce78b96d
JB
7905 return PRINT_SRC_AND_LOC;
7906}
7907
4a64f543
MS
7908/* Implement the "print_one" breakpoint_ops method for vfork
7909 catchpoints. */
ce78b96d
JB
7910
7911static void
a6d9a66e 7912print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 7913{
e29a4733 7914 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
79a45b7d 7915 struct value_print_options opts;
79a45e25 7916 struct ui_out *uiout = current_uiout;
79a45b7d
TT
7917
7918 get_user_print_options (&opts);
4a64f543
MS
7919 /* Field 4, the address, is omitted (which makes the columns not
7920 line up too nicely with the headers, but the effect is relatively
7921 readable). */
79a45b7d 7922 if (opts.addressprint)
112e8700 7923 uiout->field_skip ("addr");
ce78b96d 7924 annotate_field (5);
112e8700 7925 uiout->text ("vfork");
d7e15655 7926 if (c->forked_inferior_pid != null_ptid)
ce78b96d 7927 {
112e8700 7928 uiout->text (", process ");
381befee 7929 uiout->field_signed ("what", c->forked_inferior_pid.pid ());
112e8700 7930 uiout->spaces (1);
ce78b96d 7931 }
8ac3646f 7932
112e8700
SM
7933 if (uiout->is_mi_like_p ())
7934 uiout->field_string ("catch-type", "vfork");
ce78b96d
JB
7935}
7936
7937/* Implement the "print_mention" breakpoint_ops method for vfork
7938 catchpoints. */
7939
7940static void
7941print_mention_catch_vfork (struct breakpoint *b)
7942{
7943 printf_filtered (_("Catchpoint %d (vfork)"), b->number);
7944}
7945
6149aea9
PA
7946/* Implement the "print_recreate" breakpoint_ops method for vfork
7947 catchpoints. */
7948
7949static void
7950print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
7951{
7952 fprintf_unfiltered (fp, "catch vfork");
d9b3f62e 7953 print_recreate_thread (b, fp);
6149aea9
PA
7954}
7955
ce78b96d
JB
7956/* The breakpoint_ops structure to be used in vfork catchpoints. */
7957
2060206e 7958static struct breakpoint_ops catch_vfork_breakpoint_ops;
ce78b96d 7959
edcc5120 7960/* An instance of this type is used to represent an solib catchpoint.
c1fc2657 7961 A breakpoint is really of this type iff its ops pointer points to
edcc5120
TT
7962 CATCH_SOLIB_BREAKPOINT_OPS. */
7963
c1fc2657 7964struct solib_catchpoint : public breakpoint
edcc5120 7965{
c1fc2657 7966 ~solib_catchpoint () override;
edcc5120
TT
7967
7968 /* True for "catch load", false for "catch unload". */
b650a282 7969 bool is_load;
edcc5120
TT
7970
7971 /* Regular expression to match, if any. COMPILED is only valid when
7972 REGEX is non-NULL. */
7973 char *regex;
2d7cc5c7 7974 std::unique_ptr<compiled_regex> compiled;
edcc5120
TT
7975};
7976
c1fc2657 7977solib_catchpoint::~solib_catchpoint ()
edcc5120 7978{
c1fc2657 7979 xfree (this->regex);
edcc5120
TT
7980}
7981
7982static int
7983insert_catch_solib (struct bp_location *ignore)
7984{
7985 return 0;
7986}
7987
7988static int
73971819 7989remove_catch_solib (struct bp_location *ignore, enum remove_bp_reason reason)
edcc5120
TT
7990{
7991 return 0;
7992}
7993
7994static int
7995breakpoint_hit_catch_solib (const struct bp_location *bl,
bd522513 7996 const address_space *aspace,
edcc5120
TT
7997 CORE_ADDR bp_addr,
7998 const struct target_waitstatus *ws)
7999{
8000 struct solib_catchpoint *self = (struct solib_catchpoint *) bl->owner;
8001 struct breakpoint *other;
8002
8003 if (ws->kind == TARGET_WAITKIND_LOADED)
8004 return 1;
8005
8006 ALL_BREAKPOINTS (other)
8007 {
8008 struct bp_location *other_bl;
8009
8010 if (other == bl->owner)
8011 continue;
8012
8013 if (other->type != bp_shlib_event)
8014 continue;
8015
c1fc2657 8016 if (self->pspace != NULL && other->pspace != self->pspace)
edcc5120
TT
8017 continue;
8018
8019 for (other_bl = other->loc; other_bl != NULL; other_bl = other_bl->next)
8020 {
8021 if (other->ops->breakpoint_hit (other_bl, aspace, bp_addr, ws))
8022 return 1;
8023 }
8024 }
8025
8026 return 0;
8027}
8028
8029static void
8030check_status_catch_solib (struct bpstats *bs)
8031{
8032 struct solib_catchpoint *self
8033 = (struct solib_catchpoint *) bs->breakpoint_at;
edcc5120
TT
8034
8035 if (self->is_load)
8036 {
52941706 8037 for (so_list *iter : current_program_space->added_solibs)
edcc5120
TT
8038 {
8039 if (!self->regex
2d7cc5c7 8040 || self->compiled->exec (iter->so_name, 0, NULL, 0) == 0)
edcc5120
TT
8041 return;
8042 }
8043 }
8044 else
8045 {
6fb16ce6 8046 for (const std::string &iter : current_program_space->deleted_solibs)
edcc5120
TT
8047 {
8048 if (!self->regex
6fb16ce6 8049 || self->compiled->exec (iter.c_str (), 0, NULL, 0) == 0)
edcc5120
TT
8050 return;
8051 }
8052 }
8053
8054 bs->stop = 0;
8055 bs->print_it = print_it_noop;
8056}
8057
8058static enum print_stop_action
8059print_it_catch_solib (bpstat bs)
8060{
8061 struct breakpoint *b = bs->breakpoint_at;
8062 struct ui_out *uiout = current_uiout;
8063
8064 annotate_catchpoint (b->number);
f303dbd6 8065 maybe_print_thread_hit_breakpoint (uiout);
edcc5120 8066 if (b->disposition == disp_del)
112e8700 8067 uiout->text ("Temporary catchpoint ");
edcc5120 8068 else
112e8700 8069 uiout->text ("Catchpoint ");
381befee 8070 uiout->field_signed ("bkptno", b->number);
112e8700
SM
8071 uiout->text ("\n");
8072 if (uiout->is_mi_like_p ())
8073 uiout->field_string ("disp", bpdisp_text (b->disposition));
edcc5120
TT
8074 print_solib_event (1);
8075 return PRINT_SRC_AND_LOC;
8076}
8077
8078static void
8079print_one_catch_solib (struct breakpoint *b, struct bp_location **locs)
8080{
8081 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8082 struct value_print_options opts;
8083 struct ui_out *uiout = current_uiout;
edcc5120
TT
8084
8085 get_user_print_options (&opts);
8086 /* Field 4, the address, is omitted (which makes the columns not
8087 line up too nicely with the headers, but the effect is relatively
8088 readable). */
8089 if (opts.addressprint)
8090 {
8091 annotate_field (4);
112e8700 8092 uiout->field_skip ("addr");
edcc5120
TT
8093 }
8094
528e1572 8095 std::string msg;
edcc5120
TT
8096 annotate_field (5);
8097 if (self->is_load)
8098 {
8099 if (self->regex)
528e1572 8100 msg = string_printf (_("load of library matching %s"), self->regex);
edcc5120 8101 else
528e1572 8102 msg = _("load of library");
edcc5120
TT
8103 }
8104 else
8105 {
8106 if (self->regex)
528e1572 8107 msg = string_printf (_("unload of library matching %s"), self->regex);
edcc5120 8108 else
528e1572 8109 msg = _("unload of library");
edcc5120 8110 }
112e8700 8111 uiout->field_string ("what", msg);
8ac3646f 8112
112e8700
SM
8113 if (uiout->is_mi_like_p ())
8114 uiout->field_string ("catch-type", self->is_load ? "load" : "unload");
edcc5120
TT
8115}
8116
8117static void
8118print_mention_catch_solib (struct breakpoint *b)
8119{
8120 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8121
8122 printf_filtered (_("Catchpoint %d (%s)"), b->number,
8123 self->is_load ? "load" : "unload");
8124}
8125
8126static void
8127print_recreate_catch_solib (struct breakpoint *b, struct ui_file *fp)
8128{
8129 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8130
8131 fprintf_unfiltered (fp, "%s %s",
8132 b->disposition == disp_del ? "tcatch" : "catch",
8133 self->is_load ? "load" : "unload");
8134 if (self->regex)
8135 fprintf_unfiltered (fp, " %s", self->regex);
8136 fprintf_unfiltered (fp, "\n");
8137}
8138
8139static struct breakpoint_ops catch_solib_breakpoint_ops;
8140
b650a282 8141/* See breakpoint.h. */
edcc5120 8142
91985142 8143void
b650a282 8144add_solib_catchpoint (const char *arg, bool is_load, bool is_temp, bool enabled)
edcc5120 8145{
edcc5120 8146 struct gdbarch *gdbarch = get_current_arch ();
edcc5120 8147
edcc5120
TT
8148 if (!arg)
8149 arg = "";
f1735a53 8150 arg = skip_spaces (arg);
edcc5120 8151
36bd8eaa 8152 std::unique_ptr<solib_catchpoint> c (new solib_catchpoint ());
edcc5120
TT
8153
8154 if (*arg != '\0')
8155 {
2d7cc5c7
PA
8156 c->compiled.reset (new compiled_regex (arg, REG_NOSUB,
8157 _("Invalid regexp")));
edcc5120
TT
8158 c->regex = xstrdup (arg);
8159 }
8160
8161 c->is_load = is_load;
36bd8eaa 8162 init_catchpoint (c.get (), gdbarch, is_temp, NULL,
edcc5120
TT
8163 &catch_solib_breakpoint_ops);
8164
c1fc2657 8165 c->enable_state = enabled ? bp_enabled : bp_disabled;
91985142 8166
b270e6f9 8167 install_breakpoint (0, std::move (c), 1);
edcc5120
TT
8168}
8169
91985142
MG
8170/* A helper function that does all the work for "catch load" and
8171 "catch unload". */
8172
8173static void
eb4c3f4a 8174catch_load_or_unload (const char *arg, int from_tty, int is_load,
91985142
MG
8175 struct cmd_list_element *command)
8176{
91985142 8177 const int enabled = 1;
b650a282 8178 bool temp = get_cmd_context (command) == CATCH_TEMPORARY;
91985142 8179
b650a282 8180 add_solib_catchpoint (arg, is_load, temp, enabled);
91985142
MG
8181}
8182
edcc5120 8183static void
eb4c3f4a 8184catch_load_command_1 (const char *arg, int from_tty,
edcc5120
TT
8185 struct cmd_list_element *command)
8186{
8187 catch_load_or_unload (arg, from_tty, 1, command);
8188}
8189
8190static void
eb4c3f4a 8191catch_unload_command_1 (const char *arg, int from_tty,
edcc5120
TT
8192 struct cmd_list_element *command)
8193{
8194 catch_load_or_unload (arg, from_tty, 0, command);
8195}
8196
b650a282 8197/* See breakpoint.h. */
ce78b96d 8198
ab04a2af 8199void
346774a9 8200init_catchpoint (struct breakpoint *b,
b650a282 8201 struct gdbarch *gdbarch, bool temp,
63160a43 8202 const char *cond_string,
c0a91b2b 8203 const struct breakpoint_ops *ops)
c906108c 8204{
51abb421 8205 symtab_and_line sal;
6c95b8df 8206 sal.pspace = current_program_space;
c5aa993b 8207
28010a5d 8208 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
ce78b96d 8209
1b36a34b 8210 b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
b650a282 8211 b->disposition = temp ? disp_del : disp_donttouch;
346774a9
PA
8212}
8213
28010a5d 8214void
b270e6f9 8215install_breakpoint (int internal, std::unique_ptr<breakpoint> &&arg, int update_gll)
c56053d2 8216{
b270e6f9 8217 breakpoint *b = add_to_breakpoint_chain (std::move (arg));
3a5c3e22 8218 set_breakpoint_number (internal, b);
558a9d82
YQ
8219 if (is_tracepoint (b))
8220 set_tracepoint_count (breakpoint_count);
3a5c3e22
PA
8221 if (!internal)
8222 mention (b);
76727919 8223 gdb::observers::breakpoint_created.notify (b);
3ea46bff
YQ
8224
8225 if (update_gll)
44702360 8226 update_global_location_list (UGLL_MAY_INSERT);
c56053d2
PA
8227}
8228
9b70b993 8229static void
a6d9a66e 8230create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
b650a282 8231 bool temp, const char *cond_string,
c0a91b2b 8232 const struct breakpoint_ops *ops)
c906108c 8233{
b270e6f9 8234 std::unique_ptr<fork_catchpoint> c (new fork_catchpoint ());
ce78b96d 8235
b650a282 8236 init_catchpoint (c.get (), gdbarch, temp, cond_string, ops);
e29a4733
PA
8237
8238 c->forked_inferior_pid = null_ptid;
8239
b270e6f9 8240 install_breakpoint (0, std::move (c), 1);
c906108c
SS
8241}
8242
fe798b75
JB
8243/* Exec catchpoints. */
8244
b4d90040 8245/* An instance of this type is used to represent an exec catchpoint.
c1fc2657 8246 A breakpoint is really of this type iff its ops pointer points to
b4d90040
PA
8247 CATCH_EXEC_BREAKPOINT_OPS. */
8248
c1fc2657 8249struct exec_catchpoint : public breakpoint
b4d90040 8250{
c1fc2657 8251 ~exec_catchpoint () override;
b4d90040
PA
8252
8253 /* Filename of a program whose exec triggered this catchpoint.
8254 This field is only valid immediately after this catchpoint has
8255 triggered. */
8256 char *exec_pathname;
8257};
8258
c1fc2657 8259/* Exec catchpoint destructor. */
b4d90040 8260
c1fc2657 8261exec_catchpoint::~exec_catchpoint ()
b4d90040 8262{
c1fc2657 8263 xfree (this->exec_pathname);
b4d90040
PA
8264}
8265
77b06cd7
TJB
8266static int
8267insert_catch_exec (struct bp_location *bl)
c906108c 8268{
e99b03dc 8269 return target_insert_exec_catchpoint (inferior_ptid.pid ());
fe798b75 8270}
c906108c 8271
fe798b75 8272static int
73971819 8273remove_catch_exec (struct bp_location *bl, enum remove_bp_reason reason)
fe798b75 8274{
e99b03dc 8275 return target_remove_exec_catchpoint (inferior_ptid.pid ());
fe798b75 8276}
c906108c 8277
fe798b75 8278static int
f1310107 8279breakpoint_hit_catch_exec (const struct bp_location *bl,
bd522513 8280 const address_space *aspace, CORE_ADDR bp_addr,
09ac7c10 8281 const struct target_waitstatus *ws)
fe798b75 8282{
b4d90040
PA
8283 struct exec_catchpoint *c = (struct exec_catchpoint *) bl->owner;
8284
f90263c1
TT
8285 if (ws->kind != TARGET_WAITKIND_EXECD)
8286 return 0;
8287
8288 c->exec_pathname = xstrdup (ws->value.execd_pathname);
8289 return 1;
fe798b75 8290}
c906108c 8291
fe798b75 8292static enum print_stop_action
348d480f 8293print_it_catch_exec (bpstat bs)
fe798b75 8294{
36dfb11c 8295 struct ui_out *uiout = current_uiout;
348d480f 8296 struct breakpoint *b = bs->breakpoint_at;
b4d90040
PA
8297 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8298
fe798b75 8299 annotate_catchpoint (b->number);
f303dbd6 8300 maybe_print_thread_hit_breakpoint (uiout);
36dfb11c 8301 if (b->disposition == disp_del)
112e8700 8302 uiout->text ("Temporary catchpoint ");
36dfb11c 8303 else
112e8700
SM
8304 uiout->text ("Catchpoint ");
8305 if (uiout->is_mi_like_p ())
36dfb11c 8306 {
112e8700
SM
8307 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_EXEC));
8308 uiout->field_string ("disp", bpdisp_text (b->disposition));
36dfb11c 8309 }
381befee 8310 uiout->field_signed ("bkptno", b->number);
112e8700
SM
8311 uiout->text (" (exec'd ");
8312 uiout->field_string ("new-exec", c->exec_pathname);
8313 uiout->text ("), ");
36dfb11c 8314
fe798b75 8315 return PRINT_SRC_AND_LOC;
c906108c
SS
8316}
8317
fe798b75 8318static void
a6d9a66e 8319print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
fe798b75 8320{
b4d90040 8321 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
fe798b75 8322 struct value_print_options opts;
79a45e25 8323 struct ui_out *uiout = current_uiout;
fe798b75
JB
8324
8325 get_user_print_options (&opts);
8326
8327 /* Field 4, the address, is omitted (which makes the columns
8328 not line up too nicely with the headers, but the effect
8329 is relatively readable). */
8330 if (opts.addressprint)
112e8700 8331 uiout->field_skip ("addr");
fe798b75 8332 annotate_field (5);
112e8700 8333 uiout->text ("exec");
b4d90040 8334 if (c->exec_pathname != NULL)
fe798b75 8335 {
112e8700
SM
8336 uiout->text (", program \"");
8337 uiout->field_string ("what", c->exec_pathname);
8338 uiout->text ("\" ");
fe798b75 8339 }
8ac3646f 8340
112e8700
SM
8341 if (uiout->is_mi_like_p ())
8342 uiout->field_string ("catch-type", "exec");
fe798b75
JB
8343}
8344
8345static void
8346print_mention_catch_exec (struct breakpoint *b)
8347{
8348 printf_filtered (_("Catchpoint %d (exec)"), b->number);
8349}
8350
6149aea9
PA
8351/* Implement the "print_recreate" breakpoint_ops method for exec
8352 catchpoints. */
8353
8354static void
8355print_recreate_catch_exec (struct breakpoint *b, struct ui_file *fp)
8356{
8357 fprintf_unfiltered (fp, "catch exec");
d9b3f62e 8358 print_recreate_thread (b, fp);
6149aea9
PA
8359}
8360
2060206e 8361static struct breakpoint_ops catch_exec_breakpoint_ops;
fe798b75 8362
c906108c 8363static int
fba45db2 8364hw_breakpoint_used_count (void)
c906108c 8365{
c906108c 8366 int i = 0;
f1310107
TJB
8367 struct breakpoint *b;
8368 struct bp_location *bl;
c906108c
SS
8369
8370 ALL_BREAKPOINTS (b)
c5aa993b 8371 {
d6b74ac4 8372 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
f1310107
TJB
8373 for (bl = b->loc; bl; bl = bl->next)
8374 {
8375 /* Special types of hardware breakpoints may use more than
8376 one register. */
348d480f 8377 i += b->ops->resources_needed (bl);
f1310107 8378 }
c5aa993b 8379 }
c906108c
SS
8380
8381 return i;
8382}
8383
a1398e0c
PA
8384/* Returns the resources B would use if it were a hardware
8385 watchpoint. */
8386
c906108c 8387static int
a1398e0c 8388hw_watchpoint_use_count (struct breakpoint *b)
c906108c 8389{
c906108c 8390 int i = 0;
e09342b5 8391 struct bp_location *bl;
c906108c 8392
a1398e0c
PA
8393 if (!breakpoint_enabled (b))
8394 return 0;
8395
8396 for (bl = b->loc; bl; bl = bl->next)
8397 {
8398 /* Special types of hardware watchpoints may use more than
8399 one register. */
8400 i += b->ops->resources_needed (bl);
8401 }
8402
8403 return i;
8404}
8405
8406/* Returns the sum the used resources of all hardware watchpoints of
8407 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
8408 the sum of the used resources of all hardware watchpoints of other
8409 types _not_ TYPE. */
8410
8411static int
8412hw_watchpoint_used_count_others (struct breakpoint *except,
8413 enum bptype type, int *other_type_used)
8414{
8415 int i = 0;
8416 struct breakpoint *b;
8417
c906108c
SS
8418 *other_type_used = 0;
8419 ALL_BREAKPOINTS (b)
e09342b5 8420 {
a1398e0c
PA
8421 if (b == except)
8422 continue;
e09342b5
TJB
8423 if (!breakpoint_enabled (b))
8424 continue;
8425
a1398e0c
PA
8426 if (b->type == type)
8427 i += hw_watchpoint_use_count (b);
8428 else if (is_hardware_watchpoint (b))
8429 *other_type_used = 1;
e09342b5
TJB
8430 }
8431
c906108c
SS
8432 return i;
8433}
8434
c906108c 8435void
fba45db2 8436disable_watchpoints_before_interactive_call_start (void)
c906108c 8437{
c5aa993b 8438 struct breakpoint *b;
c906108c
SS
8439
8440 ALL_BREAKPOINTS (b)
c5aa993b 8441 {
cc60f2e3 8442 if (is_watchpoint (b) && breakpoint_enabled (b))
c5aa993b 8443 {
b5de0fa7 8444 b->enable_state = bp_call_disabled;
44702360 8445 update_global_location_list (UGLL_DONT_INSERT);
c5aa993b
JM
8446 }
8447 }
c906108c
SS
8448}
8449
8450void
fba45db2 8451enable_watchpoints_after_interactive_call_stop (void)
c906108c 8452{
c5aa993b 8453 struct breakpoint *b;
c906108c
SS
8454
8455 ALL_BREAKPOINTS (b)
c5aa993b 8456 {
cc60f2e3 8457 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
c5aa993b 8458 {
b5de0fa7 8459 b->enable_state = bp_enabled;
44702360 8460 update_global_location_list (UGLL_MAY_INSERT);
c5aa993b
JM
8461 }
8462 }
c906108c
SS
8463}
8464
8bea4e01
UW
8465void
8466disable_breakpoints_before_startup (void)
8467{
6c95b8df 8468 current_program_space->executing_startup = 1;
44702360 8469 update_global_location_list (UGLL_DONT_INSERT);
8bea4e01
UW
8470}
8471
8472void
8473enable_breakpoints_after_startup (void)
8474{
6c95b8df 8475 current_program_space->executing_startup = 0;
f8eba3c6 8476 breakpoint_re_set ();
8bea4e01
UW
8477}
8478
7c16b83e
PA
8479/* Create a new single-step breakpoint for thread THREAD, with no
8480 locations. */
c906108c 8481
7c16b83e
PA
8482static struct breakpoint *
8483new_single_step_breakpoint (int thread, struct gdbarch *gdbarch)
8484{
b270e6f9 8485 std::unique_ptr<breakpoint> b (new breakpoint ());
7c16b83e 8486
b270e6f9 8487 init_raw_breakpoint_without_location (b.get (), gdbarch, bp_single_step,
7c16b83e
PA
8488 &momentary_breakpoint_ops);
8489
8490 b->disposition = disp_donttouch;
8491 b->frame_id = null_frame_id;
8492
8493 b->thread = thread;
8494 gdb_assert (b->thread != 0);
8495
b270e6f9 8496 return add_to_breakpoint_chain (std::move (b));
7c16b83e
PA
8497}
8498
8499/* Set a momentary breakpoint of type TYPE at address specified by
8500 SAL. If FRAME_ID is valid, the breakpoint is restricted to that
8501 frame. */
c906108c 8502
454dafbd 8503breakpoint_up
a6d9a66e
UW
8504set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
8505 struct frame_id frame_id, enum bptype type)
c906108c 8506{
52f0bd74 8507 struct breakpoint *b;
edb3359d 8508
193facb3
JK
8509 /* If FRAME_ID is valid, it should be a real frame, not an inlined or
8510 tail-called one. */
8511 gdb_assert (!frame_id_artificial_p (frame_id));
edb3359d 8512
06edf0c0 8513 b = set_raw_breakpoint (gdbarch, sal, type, &momentary_breakpoint_ops);
b5de0fa7
EZ
8514 b->enable_state = bp_enabled;
8515 b->disposition = disp_donttouch;
818dd999 8516 b->frame_id = frame_id;
c906108c 8517
00431a78 8518 b->thread = inferior_thread ()->global_num;
c906108c 8519
44702360 8520 update_global_location_list_nothrow (UGLL_MAY_INSERT);
74960c60 8521
454dafbd 8522 return breakpoint_up (b);
c906108c 8523}
611c83ae 8524
06edf0c0 8525/* Make a momentary breakpoint based on the master breakpoint ORIG.
a1aa2221
LM
8526 The new breakpoint will have type TYPE, use OPS as its
8527 breakpoint_ops, and will set enabled to LOC_ENABLED. */
e58b0e63 8528
06edf0c0
PA
8529static struct breakpoint *
8530momentary_breakpoint_from_master (struct breakpoint *orig,
8531 enum bptype type,
a1aa2221
LM
8532 const struct breakpoint_ops *ops,
8533 int loc_enabled)
e58b0e63
PA
8534{
8535 struct breakpoint *copy;
8536
06edf0c0 8537 copy = set_raw_breakpoint_without_location (orig->gdbarch, type, ops);
e58b0e63 8538 copy->loc = allocate_bp_location (copy);
0ba852ab 8539 set_breakpoint_location_function (copy->loc);
e58b0e63 8540
a6d9a66e 8541 copy->loc->gdbarch = orig->loc->gdbarch;
e58b0e63
PA
8542 copy->loc->requested_address = orig->loc->requested_address;
8543 copy->loc->address = orig->loc->address;
8544 copy->loc->section = orig->loc->section;
6c95b8df 8545 copy->loc->pspace = orig->loc->pspace;
55aa24fb 8546 copy->loc->probe = orig->loc->probe;
f8eba3c6 8547 copy->loc->line_number = orig->loc->line_number;
2f202fde 8548 copy->loc->symtab = orig->loc->symtab;
a1aa2221 8549 copy->loc->enabled = loc_enabled;
e58b0e63
PA
8550 copy->frame_id = orig->frame_id;
8551 copy->thread = orig->thread;
6c95b8df 8552 copy->pspace = orig->pspace;
e58b0e63
PA
8553
8554 copy->enable_state = bp_enabled;
8555 copy->disposition = disp_donttouch;
8556 copy->number = internal_breakpoint_number--;
8557
44702360 8558 update_global_location_list_nothrow (UGLL_DONT_INSERT);
e58b0e63
PA
8559 return copy;
8560}
8561
06edf0c0
PA
8562/* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
8563 ORIG is NULL. */
8564
8565struct breakpoint *
8566clone_momentary_breakpoint (struct breakpoint *orig)
8567{
8568 /* If there's nothing to clone, then return nothing. */
8569 if (orig == NULL)
8570 return NULL;
8571
a1aa2221 8572 return momentary_breakpoint_from_master (orig, orig->type, orig->ops, 0);
06edf0c0
PA
8573}
8574
454dafbd 8575breakpoint_up
a6d9a66e
UW
8576set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
8577 enum bptype type)
611c83ae
PA
8578{
8579 struct symtab_and_line sal;
8580
8581 sal = find_pc_line (pc, 0);
8582 sal.pc = pc;
8583 sal.section = find_pc_overlay (pc);
8584 sal.explicit_pc = 1;
8585
a6d9a66e 8586 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
611c83ae 8587}
c906108c 8588\f
c5aa993b 8589
c906108c
SS
8590/* Tell the user we have just set a breakpoint B. */
8591
8592static void
fba45db2 8593mention (struct breakpoint *b)
c906108c 8594{
348d480f 8595 b->ops->print_mention (b);
2d33446d 8596 current_uiout->text ("\n");
c906108c 8597}
c906108c 8598\f
c5aa993b 8599
5133a315 8600static bool bp_loc_is_permanent (struct bp_location *loc);
1a853c52 8601
7f32a4d5
PA
8602/* Handle "set breakpoint auto-hw on".
8603
8604 If the explicitly specified breakpoint type is not hardware
8605 breakpoint, check the memory map to see whether the breakpoint
8606 address is in read-only memory.
8607
8608 - location type is not hardware breakpoint, memory is read-only.
8609 We change the type of the location to hardware breakpoint.
8610
8611 - location type is hardware breakpoint, memory is read-write. This
8612 means we've previously made the location hardware one, but then the
8613 memory map changed, so we undo.
8614*/
8615
8616static void
8617handle_automatic_hardware_breakpoints (bp_location *bl)
8618{
8619 if (automatic_hardware_breakpoints
8620 && bl->owner->type != bp_hardware_breakpoint
8621 && (bl->loc_type == bp_loc_software_breakpoint
8622 || bl->loc_type == bp_loc_hardware_breakpoint))
8623 {
8624 /* When breakpoints are removed, remove_breakpoints will use
8625 location types we've just set here, the only possible problem
8626 is that memory map has changed during running program, but
8627 it's not going to work anyway with current gdb. */
8628 mem_region *mr = lookup_mem_region (bl->address);
8629
8630 if (mr != nullptr)
8631 {
8632 enum bp_loc_type new_type;
8633
8634 if (mr->attrib.mode != MEM_RW)
8635 new_type = bp_loc_hardware_breakpoint;
8636 else
8637 new_type = bp_loc_software_breakpoint;
8638
8639 if (new_type != bl->loc_type)
8640 {
8641 static bool said = false;
8642
8643 bl->loc_type = new_type;
8644 if (!said)
8645 {
8646 fprintf_filtered (gdb_stdout,
8647 _("Note: automatically using "
8648 "hardware breakpoints for "
8649 "read-only addresses.\n"));
8650 said = true;
8651 }
8652 }
8653 }
8654 }
8655}
8656
0d381245 8657static struct bp_location *
39d61571 8658add_location_to_breakpoint (struct breakpoint *b,
0d381245
VP
8659 const struct symtab_and_line *sal)
8660{
8661 struct bp_location *loc, **tmp;
3742cc8b
YQ
8662 CORE_ADDR adjusted_address;
8663 struct gdbarch *loc_gdbarch = get_sal_arch (*sal);
8664
8665 if (loc_gdbarch == NULL)
8666 loc_gdbarch = b->gdbarch;
8667
8668 /* Adjust the breakpoint's address prior to allocating a location.
8669 Once we call allocate_bp_location(), that mostly uninitialized
8670 location will be placed on the location chain. Adjustment of the
8671 breakpoint may cause target_read_memory() to be called and we do
8672 not want its scan of the location chain to find a breakpoint and
8673 location that's only been partially initialized. */
8674 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
8675 sal->pc, b->type);
0d381245 8676
d30113d4 8677 /* Sort the locations by their ADDRESS. */
39d61571 8678 loc = allocate_bp_location (b);
d30113d4
JK
8679 for (tmp = &(b->loc); *tmp != NULL && (*tmp)->address <= adjusted_address;
8680 tmp = &((*tmp)->next))
0d381245 8681 ;
d30113d4 8682 loc->next = *tmp;
0d381245 8683 *tmp = loc;
3742cc8b 8684
0d381245 8685 loc->requested_address = sal->pc;
3742cc8b 8686 loc->address = adjusted_address;
6c95b8df 8687 loc->pspace = sal->pspace;
935676c9 8688 loc->probe.prob = sal->prob;
729662a5 8689 loc->probe.objfile = sal->objfile;
6c95b8df 8690 gdb_assert (loc->pspace != NULL);
0d381245 8691 loc->section = sal->section;
3742cc8b 8692 loc->gdbarch = loc_gdbarch;
f8eba3c6 8693 loc->line_number = sal->line;
2f202fde 8694 loc->symtab = sal->symtab;
4a27f119 8695 loc->symbol = sal->symbol;
3467ec66
PA
8696 loc->msymbol = sal->msymbol;
8697 loc->objfile = sal->objfile;
f8eba3c6 8698
0ba852ab 8699 set_breakpoint_location_function (loc);
1a853c52 8700
6ae88661
LM
8701 /* While by definition, permanent breakpoints are already present in the
8702 code, we don't mark the location as inserted. Normally one would expect
8703 that GDB could rely on that breakpoint instruction to stop the program,
8704 thus removing the need to insert its own breakpoint, except that executing
8705 the breakpoint instruction can kill the target instead of reporting a
8706 SIGTRAP. E.g., on SPARC, when interrupts are disabled, executing the
8707 instruction resets the CPU, so QEMU 2.0.0 for SPARC correspondingly dies
8708 with "Trap 0x02 while interrupts disabled, Error state". Letting the
8709 breakpoint be inserted normally results in QEMU knowing about the GDB
8710 breakpoint, and thus trap before the breakpoint instruction is executed.
8711 (If GDB later needs to continue execution past the permanent breakpoint,
8712 it manually increments the PC, thus avoiding executing the breakpoint
8713 instruction.) */
1a853c52 8714 if (bp_loc_is_permanent (loc))
6ae88661 8715 loc->permanent = 1;
1a853c52 8716
0d381245
VP
8717 return loc;
8718}
514f746b
AR
8719\f
8720
5133a315
LM
8721/* Return true if LOC is pointing to a permanent breakpoint,
8722 return false otherwise. */
1cf4d951 8723
5133a315 8724static bool
1cf4d951
PA
8725bp_loc_is_permanent (struct bp_location *loc)
8726{
514f746b
AR
8727 gdb_assert (loc != NULL);
8728
cb1e4e32
PA
8729 /* If we have a non-breakpoint-backed catchpoint or a software
8730 watchpoint, just return 0. We should not attempt to read from
8731 the addresses the locations of these breakpoint types point to.
5133a315 8732 gdbarch_program_breakpoint_here_p, below, will attempt to read
244558af 8733 memory. */
cb1e4e32 8734 if (!bl_address_is_meaningful (loc))
5133a315 8735 return false;
244558af 8736
5ed8105e 8737 scoped_restore_current_pspace_and_thread restore_pspace_thread;
6c95b8df 8738 switch_to_program_space_and_thread (loc->pspace);
5133a315 8739 return gdbarch_program_breakpoint_here_p (loc->gdbarch, loc->address);
514f746b
AR
8740}
8741
e7e0cddf
SS
8742/* Build a command list for the dprintf corresponding to the current
8743 settings of the dprintf style options. */
8744
8745static void
8746update_dprintf_command_list (struct breakpoint *b)
8747{
8748 char *dprintf_args = b->extra_string;
8749 char *printf_line = NULL;
8750
8751 if (!dprintf_args)
8752 return;
8753
8754 dprintf_args = skip_spaces (dprintf_args);
8755
8756 /* Allow a comma, as it may have terminated a location, but don't
8757 insist on it. */
8758 if (*dprintf_args == ',')
8759 ++dprintf_args;
8760 dprintf_args = skip_spaces (dprintf_args);
8761
8762 if (*dprintf_args != '"')
8763 error (_("Bad format string, missing '\"'."));
8764
d3ce09f5 8765 if (strcmp (dprintf_style, dprintf_style_gdb) == 0)
e7e0cddf 8766 printf_line = xstrprintf ("printf %s", dprintf_args);
d3ce09f5 8767 else if (strcmp (dprintf_style, dprintf_style_call) == 0)
e7e0cddf
SS
8768 {
8769 if (!dprintf_function)
8770 error (_("No function supplied for dprintf call"));
8771
8772 if (dprintf_channel && strlen (dprintf_channel) > 0)
8773 printf_line = xstrprintf ("call (void) %s (%s,%s)",
8774 dprintf_function,
8775 dprintf_channel,
8776 dprintf_args);
8777 else
8778 printf_line = xstrprintf ("call (void) %s (%s)",
8779 dprintf_function,
8780 dprintf_args);
8781 }
d3ce09f5
SS
8782 else if (strcmp (dprintf_style, dprintf_style_agent) == 0)
8783 {
8784 if (target_can_run_breakpoint_commands ())
8785 printf_line = xstrprintf ("agent-printf %s", dprintf_args);
8786 else
8787 {
8788 warning (_("Target cannot run dprintf commands, falling back to GDB printf"));
8789 printf_line = xstrprintf ("printf %s", dprintf_args);
8790 }
8791 }
e7e0cddf
SS
8792 else
8793 internal_error (__FILE__, __LINE__,
8794 _("Invalid dprintf style."));
8795
f28045c2 8796 gdb_assert (printf_line != NULL);
e7e0cddf 8797
12973681
TT
8798 /* Manufacture a printf sequence. */
8799 struct command_line *printf_cmd_line
8800 = new struct command_line (simple_control, printf_line);
8801 breakpoint_set_commands (b, counted_command_line (printf_cmd_line,
8802 command_lines_deleter ()));
e7e0cddf
SS
8803}
8804
8805/* Update all dprintf commands, making their command lists reflect
8806 current style settings. */
8807
8808static void
eb4c3f4a 8809update_dprintf_commands (const char *args, int from_tty,
e7e0cddf
SS
8810 struct cmd_list_element *c)
8811{
8812 struct breakpoint *b;
8813
8814 ALL_BREAKPOINTS (b)
8815 {
8816 if (b->type == bp_dprintf)
8817 update_dprintf_command_list (b);
8818 }
8819}
c3f6f71d 8820
f00aae0f
KS
8821/* Create a breakpoint with SAL as location. Use LOCATION
8822 as a description of the location, and COND_STRING
b35a8b2f
DE
8823 as condition expression. If LOCATION is NULL then create an
8824 "address location" from the address in the SAL. */
018d34a4
VP
8825
8826static void
d9b3f62e 8827init_breakpoint_sal (struct breakpoint *b, struct gdbarch *gdbarch,
6c5b2ebe 8828 gdb::array_view<const symtab_and_line> sals,
ffc2605c 8829 event_location_up &&location,
e1e01040
PA
8830 gdb::unique_xmalloc_ptr<char> filter,
8831 gdb::unique_xmalloc_ptr<char> cond_string,
8832 gdb::unique_xmalloc_ptr<char> extra_string,
d9b3f62e
PA
8833 enum bptype type, enum bpdisp disposition,
8834 int thread, int task, int ignore_count,
c0a91b2b 8835 const struct breakpoint_ops *ops, int from_tty,
44f238bb
PA
8836 int enabled, int internal, unsigned flags,
8837 int display_canonical)
018d34a4 8838{
0d381245 8839 int i;
018d34a4
VP
8840
8841 if (type == bp_hardware_breakpoint)
8842 {
fbbd034e
AS
8843 int target_resources_ok;
8844
8845 i = hw_breakpoint_used_count ();
8846 target_resources_ok =
8847 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
018d34a4
VP
8848 i + 1, 0);
8849 if (target_resources_ok == 0)
8850 error (_("No hardware breakpoint support in the target."));
8851 else if (target_resources_ok < 0)
8852 error (_("Hardware breakpoints used exceeds limit."));
8853 }
8854
6c5b2ebe 8855 gdb_assert (!sals.empty ());
6c95b8df 8856
6c5b2ebe 8857 for (const auto &sal : sals)
0d381245 8858 {
0d381245
VP
8859 struct bp_location *loc;
8860
8861 if (from_tty)
5af949e3
UW
8862 {
8863 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
8864 if (!loc_gdbarch)
8865 loc_gdbarch = gdbarch;
8866
8867 describe_other_breakpoints (loc_gdbarch,
6c95b8df 8868 sal.pspace, sal.pc, sal.section, thread);
5af949e3 8869 }
0d381245 8870
6c5b2ebe 8871 if (&sal == &sals[0])
0d381245 8872 {
d9b3f62e 8873 init_raw_breakpoint (b, gdbarch, sal, type, ops);
0d381245 8874 b->thread = thread;
4a306c9a 8875 b->task = task;
855a6e68 8876
e1e01040
PA
8877 b->cond_string = cond_string.release ();
8878 b->extra_string = extra_string.release ();
0d381245 8879 b->ignore_count = ignore_count;
41447f92 8880 b->enable_state = enabled ? bp_enabled : bp_disabled;
0d381245 8881 b->disposition = disposition;
6c95b8df 8882
44f238bb
PA
8883 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
8884 b->loc->inserted = 1;
8885
0fb4aa4b
PA
8886 if (type == bp_static_tracepoint)
8887 {
d9b3f62e 8888 struct tracepoint *t = (struct tracepoint *) b;
0fb4aa4b
PA
8889 struct static_tracepoint_marker marker;
8890
983af33b 8891 if (strace_marker_p (b))
0fb4aa4b
PA
8892 {
8893 /* We already know the marker exists, otherwise, we
8894 wouldn't see a sal for it. */
d28cd78a
TT
8895 const char *p
8896 = &event_location_to_string (b->location.get ())[3];
f00aae0f 8897 const char *endp;
0fb4aa4b 8898
f1735a53 8899 p = skip_spaces (p);
0fb4aa4b 8900
f1735a53 8901 endp = skip_to_space (p);
0fb4aa4b 8902
5d9310c4 8903 t->static_trace_marker_id.assign (p, endp - p);
0fb4aa4b 8904
3e43a32a
MS
8905 printf_filtered (_("Probed static tracepoint "
8906 "marker \"%s\"\n"),
5d9310c4 8907 t->static_trace_marker_id.c_str ());
0fb4aa4b
PA
8908 }
8909 else if (target_static_tracepoint_marker_at (sal.pc, &marker))
8910 {
5d9310c4 8911 t->static_trace_marker_id = std::move (marker.str_id);
0fb4aa4b 8912
3e43a32a
MS
8913 printf_filtered (_("Probed static tracepoint "
8914 "marker \"%s\"\n"),
5d9310c4 8915 t->static_trace_marker_id.c_str ());
0fb4aa4b
PA
8916 }
8917 else
3e43a32a
MS
8918 warning (_("Couldn't determine the static "
8919 "tracepoint marker to probe"));
0fb4aa4b
PA
8920 }
8921
0d381245
VP
8922 loc = b->loc;
8923 }
8924 else
018d34a4 8925 {
39d61571 8926 loc = add_location_to_breakpoint (b, &sal);
44f238bb
PA
8927 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
8928 loc->inserted = 1;
0d381245
VP
8929 }
8930
b5fa468f
TBA
8931 /* Do not set breakpoint locations conditions yet. As locations
8932 are inserted, they get sorted based on their addresses. Let
8933 the list stabilize to have reliable location numbers. */
e7e0cddf
SS
8934
8935 /* Dynamic printf requires and uses additional arguments on the
8936 command line, otherwise it's an error. */
8937 if (type == bp_dprintf)
8938 {
8939 if (b->extra_string)
8940 update_dprintf_command_list (b);
8941 else
8942 error (_("Format string required"));
8943 }
8944 else if (b->extra_string)
588ae58c 8945 error (_("Garbage '%s' at end of command"), b->extra_string);
855a6e68 8946 }
018d34a4 8947
b5fa468f
TBA
8948
8949 /* The order of the locations is now stable. Set the location
8950 condition using the location's number. */
8951 int loc_num = 1;
8952 for (bp_location *loc = b->loc; loc != nullptr; loc = loc->next)
8953 {
8954 if (b->cond_string != nullptr)
8955 set_breakpoint_location_condition (b->cond_string, loc, b->number,
8956 loc_num);
8957
8958 ++loc_num;
8959 }
8960
56435ebe 8961 b->display_canonical = display_canonical;
f00aae0f 8962 if (location != NULL)
d28cd78a 8963 b->location = std::move (location);
018d34a4 8964 else
d28cd78a 8965 b->location = new_address_location (b->loc->address, NULL, 0);
c0e8dcd8 8966 b->filter = std::move (filter);
d9b3f62e 8967}
018d34a4 8968
d9b3f62e
PA
8969static void
8970create_breakpoint_sal (struct gdbarch *gdbarch,
6c5b2ebe 8971 gdb::array_view<const symtab_and_line> sals,
ffc2605c 8972 event_location_up &&location,
e1e01040
PA
8973 gdb::unique_xmalloc_ptr<char> filter,
8974 gdb::unique_xmalloc_ptr<char> cond_string,
8975 gdb::unique_xmalloc_ptr<char> extra_string,
d9b3f62e
PA
8976 enum bptype type, enum bpdisp disposition,
8977 int thread, int task, int ignore_count,
c0a91b2b 8978 const struct breakpoint_ops *ops, int from_tty,
44f238bb
PA
8979 int enabled, int internal, unsigned flags,
8980 int display_canonical)
d9b3f62e 8981{
a5e364af 8982 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (type);
d9b3f62e 8983
a5e364af 8984 init_breakpoint_sal (b.get (), gdbarch,
ffc2605c 8985 sals, std::move (location),
e1e01040
PA
8986 std::move (filter),
8987 std::move (cond_string),
8988 std::move (extra_string),
d9b3f62e
PA
8989 type, disposition,
8990 thread, task, ignore_count,
8991 ops, from_tty,
44f238bb
PA
8992 enabled, internal, flags,
8993 display_canonical);
d9b3f62e 8994
b270e6f9 8995 install_breakpoint (internal, std::move (b), 0);
018d34a4
VP
8996}
8997
8998/* Add SALS.nelts breakpoints to the breakpoint table. For each
8999 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
9000 value. COND_STRING, if not NULL, specified the condition to be
9001 used for all breakpoints. Essentially the only case where
9002 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
9003 function. In that case, it's still not possible to specify
9004 separate conditions for different overloaded functions, so
9005 we take just a single condition string.
9006
c3f6f71d 9007 NOTE: If the function succeeds, the caller is expected to cleanup
018d34a4 9008 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
c3f6f71d
JM
9009 array contents). If the function fails (error() is called), the
9010 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
4a64f543 9011 COND and SALS arrays and each of those arrays contents. */
c906108c
SS
9012
9013static void
8cdf0e15 9014create_breakpoints_sal (struct gdbarch *gdbarch,
7efd8fc2 9015 struct linespec_result *canonical,
e1e01040
PA
9016 gdb::unique_xmalloc_ptr<char> cond_string,
9017 gdb::unique_xmalloc_ptr<char> extra_string,
8cdf0e15
VP
9018 enum bptype type, enum bpdisp disposition,
9019 int thread, int task, int ignore_count,
c0a91b2b 9020 const struct breakpoint_ops *ops, int from_tty,
44f238bb 9021 int enabled, int internal, unsigned flags)
c906108c 9022{
f8eba3c6 9023 if (canonical->pre_expanded)
6c5b2ebe 9024 gdb_assert (canonical->lsals.size () == 1);
f8eba3c6 9025
6c5b2ebe 9026 for (const auto &lsal : canonical->lsals)
c3f6f71d 9027 {
f00aae0f 9028 /* Note that 'location' can be NULL in the case of a plain
f8eba3c6 9029 'break', without arguments. */
ffc2605c 9030 event_location_up location
f00aae0f 9031 = (canonical->location != NULL
8e9e35b1 9032 ? copy_event_location (canonical->location.get ()) : NULL);
e1e01040 9033 gdb::unique_xmalloc_ptr<char> filter_string
6c5b2ebe 9034 (lsal.canonical != NULL ? xstrdup (lsal.canonical) : NULL);
0d381245 9035
6c5b2ebe 9036 create_breakpoint_sal (gdbarch, lsal.sals,
ffc2605c 9037 std::move (location),
e1e01040
PA
9038 std::move (filter_string),
9039 std::move (cond_string),
9040 std::move (extra_string),
e7e0cddf 9041 type, disposition,
84f4c1fe 9042 thread, task, ignore_count, ops,
44f238bb 9043 from_tty, enabled, internal, flags,
56435ebe 9044 canonical->special_display);
c3f6f71d 9045 }
c3f6f71d 9046}
c906108c 9047
f00aae0f 9048/* Parse LOCATION which is assumed to be a SAL specification possibly
c3f6f71d 9049 followed by conditionals. On return, SALS contains an array of SAL
f00aae0f
KS
9050 addresses found. LOCATION points to the end of the SAL (for
9051 linespec locations).
9998af43
TJB
9052
9053 The array and the line spec strings are allocated on the heap, it is
9054 the caller's responsibility to free them. */
c906108c 9055
b9362cc7 9056static void
626d2320 9057parse_breakpoint_sals (struct event_location *location,
58438ac1 9058 struct linespec_result *canonical)
c3f6f71d 9059{
f00aae0f
KS
9060 struct symtab_and_line cursal;
9061
9062 if (event_location_type (location) == LINESPEC_LOCATION)
9063 {
a20714ff 9064 const char *spec = get_linespec_location (location)->spec_string;
f00aae0f 9065
a20714ff 9066 if (spec == NULL)
f00aae0f
KS
9067 {
9068 /* The last displayed codepoint, if it's valid, is our default
9069 breakpoint address. */
9070 if (last_displayed_sal_is_valid ())
9071 {
f00aae0f
KS
9072 /* Set sal's pspace, pc, symtab, and line to the values
9073 corresponding to the last call to print_frame_info.
9074 Be sure to reinitialize LINE with NOTCURRENT == 0
9075 as the breakpoint line number is inappropriate otherwise.
9076 find_pc_line would adjust PC, re-set it back. */
51abb421
PA
9077 symtab_and_line sal = get_last_displayed_sal ();
9078 CORE_ADDR pc = sal.pc;
9079
f00aae0f
KS
9080 sal = find_pc_line (pc, 0);
9081
9082 /* "break" without arguments is equivalent to "break *PC"
9083 where PC is the last displayed codepoint's address. So
9084 make sure to set sal.explicit_pc to prevent GDB from
9085 trying to expand the list of sals to include all other
9086 instances with the same symtab and line. */
9087 sal.pc = pc;
9088 sal.explicit_pc = 1;
9089
6c5b2ebe
PA
9090 struct linespec_sals lsal;
9091 lsal.sals = {sal};
f00aae0f
KS
9092 lsal.canonical = NULL;
9093
6c5b2ebe 9094 canonical->lsals.push_back (std::move (lsal));
f00aae0f
KS
9095 return;
9096 }
9097 else
9098 error (_("No default breakpoint address now."));
c906108c 9099 }
c906108c 9100 }
f00aae0f
KS
9101
9102 /* Force almost all breakpoints to be in terms of the
9103 current_source_symtab (which is decode_line_1's default).
9104 This should produce the results we want almost all of the
9105 time while leaving default_breakpoint_* alone.
9106
9107 ObjC: However, don't match an Objective-C method name which
9108 may have a '+' or '-' succeeded by a '['. */
9109 cursal = get_current_source_symtab_and_line ();
9110 if (last_displayed_sal_is_valid ())
c906108c 9111 {
a20714ff 9112 const char *spec = NULL;
cc80f267 9113
f00aae0f 9114 if (event_location_type (location) == LINESPEC_LOCATION)
a20714ff 9115 spec = get_linespec_location (location)->spec_string;
cc80f267 9116
f00aae0f 9117 if (!cursal.symtab
a20714ff
PA
9118 || (spec != NULL
9119 && strchr ("+-", spec[0]) != NULL
9120 && spec[1] != '['))
f00aae0f 9121 {
c2f4122d 9122 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
f00aae0f
KS
9123 get_last_displayed_symtab (),
9124 get_last_displayed_line (),
9125 canonical, NULL, NULL);
9126 return;
9127 }
c906108c 9128 }
f00aae0f 9129
c2f4122d 9130 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
f00aae0f 9131 cursal.symtab, cursal.line, canonical, NULL, NULL);
c3f6f71d 9132}
c906108c 9133
c906108c 9134
c3f6f71d 9135/* Convert each SAL into a real PC. Verify that the PC can be
4a64f543 9136 inserted as a breakpoint. If it can't throw an error. */
c906108c 9137
b9362cc7 9138static void
6c5b2ebe 9139breakpoint_sals_to_pc (std::vector<symtab_and_line> &sals)
c3f6f71d 9140{
6c5b2ebe
PA
9141 for (auto &sal : sals)
9142 resolve_sal_pc (&sal);
c3f6f71d
JM
9143}
9144
7a697b8d
SS
9145/* Fast tracepoints may have restrictions on valid locations. For
9146 instance, a fast tracepoint using a jump instead of a trap will
9147 likely have to overwrite more bytes than a trap would, and so can
9148 only be placed where the instruction is longer than the jump, or a
9149 multi-instruction sequence does not have a jump into the middle of
9150 it, etc. */
9151
9152static void
9153check_fast_tracepoint_sals (struct gdbarch *gdbarch,
6c5b2ebe 9154 gdb::array_view<const symtab_and_line> sals)
7a697b8d 9155{
6c5b2ebe 9156 for (const auto &sal : sals)
7a697b8d 9157 {
f8eba3c6
TT
9158 struct gdbarch *sarch;
9159
6c5b2ebe 9160 sarch = get_sal_arch (sal);
f8eba3c6
TT
9161 /* We fall back to GDBARCH if there is no architecture
9162 associated with SAL. */
9163 if (sarch == NULL)
9164 sarch = gdbarch;
281d762b
TT
9165 std::string msg;
9166 if (!gdbarch_fast_tracepoint_valid_at (sarch, sal.pc, &msg))
53c3572a 9167 error (_("May not have a fast tracepoint at %s%s"),
281d762b 9168 paddress (sarch, sal.pc), msg.c_str ());
7a697b8d
SS
9169 }
9170}
9171
018d34a4
VP
9172/* Given TOK, a string specification of condition and thread, as
9173 accepted by the 'break' command, extract the condition
9174 string and thread number and set *COND_STRING and *THREAD.
4a64f543 9175 PC identifies the context at which the condition should be parsed.
018d34a4
VP
9176 If no condition is found, *COND_STRING is set to NULL.
9177 If no thread is found, *THREAD is set to -1. */
d634f2de
JB
9178
9179static void
bbc13ae3 9180find_condition_and_thread (const char *tok, CORE_ADDR pc,
e7e0cddf
SS
9181 char **cond_string, int *thread, int *task,
9182 char **rest)
018d34a4
VP
9183{
9184 *cond_string = NULL;
9185 *thread = -1;
ed1d1739
KS
9186 *task = 0;
9187 *rest = NULL;
733d554a 9188 bool force = false;
ed1d1739 9189
018d34a4
VP
9190 while (tok && *tok)
9191 {
bbc13ae3 9192 const char *end_tok;
018d34a4 9193 int toklen;
bbc13ae3
KS
9194 const char *cond_start = NULL;
9195 const char *cond_end = NULL;
cc59ec59 9196
f1735a53 9197 tok = skip_spaces (tok);
e7e0cddf
SS
9198
9199 if ((*tok == '"' || *tok == ',') && rest)
9200 {
9201 *rest = savestring (tok, strlen (tok));
9202 return;
9203 }
9204
f1735a53 9205 end_tok = skip_to_space (tok);
d634f2de 9206
018d34a4 9207 toklen = end_tok - tok;
d634f2de 9208
018d34a4
VP
9209 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
9210 {
9211 tok = cond_start = end_tok + 1;
733d554a
TBA
9212 try
9213 {
9214 parse_exp_1 (&tok, pc, block_for_pc (pc), 0);
9215 }
9216 catch (const gdb_exception_error &)
9217 {
9218 if (!force)
9219 throw;
9220 else
9221 tok = tok + strlen (tok);
9222 }
018d34a4 9223 cond_end = tok;
d634f2de 9224 *cond_string = savestring (cond_start, cond_end - cond_start);
018d34a4 9225 }
733d554a
TBA
9226 else if (toklen >= 1 && strncmp (tok, "-force-condition", toklen) == 0)
9227 {
9228 tok = cond_start = end_tok + 1;
9229 force = true;
9230 }
018d34a4
VP
9231 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
9232 {
5d5658a1
PA
9233 const char *tmptok;
9234 struct thread_info *thr;
d634f2de 9235
018d34a4 9236 tok = end_tok + 1;
5d5658a1 9237 thr = parse_thread_id (tok, &tmptok);
018d34a4
VP
9238 if (tok == tmptok)
9239 error (_("Junk after thread keyword."));
5d5658a1 9240 *thread = thr->global_num;
bbc13ae3 9241 tok = tmptok;
018d34a4 9242 }
4a306c9a
JB
9243 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
9244 {
9245 char *tmptok;
9246
9247 tok = end_tok + 1;
bbc13ae3 9248 *task = strtol (tok, &tmptok, 0);
4a306c9a
JB
9249 if (tok == tmptok)
9250 error (_("Junk after task keyword."));
9251 if (!valid_task_id (*task))
b6199126 9252 error (_("Unknown task %d."), *task);
bbc13ae3 9253 tok = tmptok;
4a306c9a 9254 }
e7e0cddf
SS
9255 else if (rest)
9256 {
9257 *rest = savestring (tok, strlen (tok));
ccab2054 9258 return;
e7e0cddf 9259 }
018d34a4
VP
9260 else
9261 error (_("Junk at end of arguments."));
9262 }
9263}
9264
b5fa468f
TBA
9265/* Call 'find_condition_and_thread' for each sal in SALS until a parse
9266 succeeds. The parsed values are written to COND_STRING, THREAD,
9267 TASK, and REST. See the comment of 'find_condition_and_thread'
9268 for the description of these parameters and INPUT. */
9269
9270static void
9271find_condition_and_thread_for_sals (const std::vector<symtab_and_line> &sals,
9272 const char *input, char **cond_string,
9273 int *thread, int *task, char **rest)
9274{
9275 int num_failures = 0;
9276 for (auto &sal : sals)
9277 {
9278 char *cond = nullptr;
9279 int thread_id = 0;
9280 int task_id = 0;
9281 char *remaining = nullptr;
9282
9283 /* Here we want to parse 'arg' to separate condition from thread
9284 number. But because parsing happens in a context and the
9285 contexts of sals might be different, try each until there is
9286 success. Finding one successful parse is sufficient for our
9287 goal. When setting the breakpoint we'll re-parse the
9288 condition in the context of each sal. */
9289 try
9290 {
9291 find_condition_and_thread (input, sal.pc, &cond, &thread_id,
9292 &task_id, &remaining);
9293 *cond_string = cond;
9294 *thread = thread_id;
9295 *task = task_id;
9296 *rest = remaining;
9297 break;
9298 }
9299 catch (const gdb_exception_error &e)
9300 {
9301 num_failures++;
9302 /* If no sal remains, do not continue. */
9303 if (num_failures == sals.size ())
9304 throw;
9305 }
9306 }
9307}
9308
0fb4aa4b
PA
9309/* Decode a static tracepoint marker spec. */
9310
6c5b2ebe 9311static std::vector<symtab_and_line>
f00aae0f 9312decode_static_tracepoint_spec (const char **arg_p)
0fb4aa4b 9313{
f00aae0f
KS
9314 const char *p = &(*arg_p)[3];
9315 const char *endp;
0fb4aa4b 9316
f1735a53 9317 p = skip_spaces (p);
0fb4aa4b 9318
f1735a53 9319 endp = skip_to_space (p);
0fb4aa4b 9320
81b1e71c 9321 std::string marker_str (p, endp - p);
0fb4aa4b 9322
5d9310c4
SM
9323 std::vector<static_tracepoint_marker> markers
9324 = target_static_tracepoint_markers_by_strid (marker_str.c_str ());
9325 if (markers.empty ())
81b1e71c
TT
9326 error (_("No known static tracepoint marker named %s"),
9327 marker_str.c_str ());
0fb4aa4b 9328
6c5b2ebe 9329 std::vector<symtab_and_line> sals;
5d9310c4 9330 sals.reserve (markers.size ());
0fb4aa4b 9331
5d9310c4 9332 for (const static_tracepoint_marker &marker : markers)
0fb4aa4b 9333 {
5d9310c4
SM
9334 symtab_and_line sal = find_pc_line (marker.address, 0);
9335 sal.pc = marker.address;
6c5b2ebe 9336 sals.push_back (sal);
5d9310c4 9337 }
0fb4aa4b 9338
0fb4aa4b
PA
9339 *arg_p = endp;
9340 return sals;
9341}
9342
bac7c5cf
GB
9343/* Returns the breakpoint ops appropriate for use with with LOCATION_TYPE and
9344 according to IS_TRACEPOINT. */
9345
9346static const struct breakpoint_ops *
9347breakpoint_ops_for_event_location_type (enum event_location_type location_type,
9348 bool is_tracepoint)
9349{
9350 if (is_tracepoint)
9351 {
9352 if (location_type == PROBE_LOCATION)
9353 return &tracepoint_probe_breakpoint_ops;
9354 else
9355 return &tracepoint_breakpoint_ops;
9356 }
9357 else
9358 {
9359 if (location_type == PROBE_LOCATION)
9360 return &bkpt_probe_breakpoint_ops;
9361 else
9362 return &bkpt_breakpoint_ops;
9363 }
9364}
9365
9366/* See breakpoint.h. */
9367
9368const struct breakpoint_ops *
9369breakpoint_ops_for_event_location (const struct event_location *location,
9370 bool is_tracepoint)
9371{
9372 if (location != nullptr)
9373 return breakpoint_ops_for_event_location_type
9374 (event_location_type (location), is_tracepoint);
9375 return is_tracepoint ? &tracepoint_breakpoint_ops : &bkpt_breakpoint_ops;
9376}
9377
f00aae0f 9378/* See breakpoint.h. */
0101ce28 9379
8cdf0e15
VP
9380int
9381create_breakpoint (struct gdbarch *gdbarch,
626d2320 9382 struct event_location *location,
e1e01040
PA
9383 const char *cond_string,
9384 int thread, const char *extra_string,
f00aae0f 9385 int parse_extra,
0fb4aa4b 9386 int tempflag, enum bptype type_wanted,
8cdf0e15
VP
9387 int ignore_count,
9388 enum auto_boolean pending_break_support,
c0a91b2b 9389 const struct breakpoint_ops *ops,
44f238bb
PA
9390 int from_tty, int enabled, int internal,
9391 unsigned flags)
c3f6f71d 9392{
7efd8fc2 9393 struct linespec_result canonical;
0101ce28 9394 int pending = 0;
4a306c9a 9395 int task = 0;
86b17b60 9396 int prev_bkpt_count = breakpoint_count;
c3f6f71d 9397
348d480f
PA
9398 gdb_assert (ops != NULL);
9399
f00aae0f
KS
9400 /* If extra_string isn't useful, set it to NULL. */
9401 if (extra_string != NULL && *extra_string == '\0')
9402 extra_string = NULL;
9403
a70b8144 9404 try
b78a6381 9405 {
f00aae0f 9406 ops->create_sals_from_location (location, &canonical, type_wanted);
b78a6381 9407 }
230d2906 9408 catch (const gdb_exception_error &e)
0101ce28 9409 {
492d29ea
PA
9410 /* If caller is interested in rc value from parse, set
9411 value. */
9412 if (e.error == NOT_FOUND_ERROR)
0101ce28 9413 {
05ff989b
AC
9414 /* If pending breakpoint support is turned off, throw
9415 error. */
fa8d40ab
JJ
9416
9417 if (pending_break_support == AUTO_BOOLEAN_FALSE)
eedc3f4f 9418 throw;
723a2275
VP
9419
9420 exception_print (gdb_stderr, e);
fa8d40ab 9421
05ff989b
AC
9422 /* If pending breakpoint support is auto query and the user
9423 selects no, then simply return the error code. */
059fb39f 9424 if (pending_break_support == AUTO_BOOLEAN_AUTO
bfccc43c
YQ
9425 && !nquery (_("Make %s pending on future shared library load? "),
9426 bptype_string (type_wanted)))
fd9b8c24 9427 return 0;
fa8d40ab 9428
05ff989b
AC
9429 /* At this point, either the user was queried about setting
9430 a pending breakpoint and selected yes, or pending
9431 breakpoint behavior is on and thus a pending breakpoint
9432 is defaulted on behalf of the user. */
f00aae0f 9433 pending = 1;
0101ce28 9434 }
492d29ea 9435 else
eedc3f4f 9436 throw;
0101ce28 9437 }
492d29ea 9438
6c5b2ebe 9439 if (!pending && canonical.lsals.empty ())
492d29ea 9440 return 0;
c3f6f71d 9441
c3f6f71d
JM
9442 /* Resolve all line numbers to PC's and verify that the addresses
9443 are ok for the target. */
0101ce28 9444 if (!pending)
f8eba3c6 9445 {
6c5b2ebe
PA
9446 for (auto &lsal : canonical.lsals)
9447 breakpoint_sals_to_pc (lsal.sals);
f8eba3c6 9448 }
c3f6f71d 9449
7a697b8d 9450 /* Fast tracepoints may have additional restrictions on location. */
bfccc43c 9451 if (!pending && type_wanted == bp_fast_tracepoint)
f8eba3c6 9452 {
6c5b2ebe
PA
9453 for (const auto &lsal : canonical.lsals)
9454 check_fast_tracepoint_sals (gdbarch, lsal.sals);
f8eba3c6 9455 }
7a697b8d 9456
c3f6f71d
JM
9457 /* Verify that condition can be parsed, before setting any
9458 breakpoints. Allocate a separate condition expression for each
4a64f543 9459 breakpoint. */
0101ce28 9460 if (!pending)
c3f6f71d 9461 {
e1e01040
PA
9462 gdb::unique_xmalloc_ptr<char> cond_string_copy;
9463 gdb::unique_xmalloc_ptr<char> extra_string_copy;
9464
f00aae0f 9465 if (parse_extra)
72b2ff0e 9466 {
0878d0fa 9467 char *rest;
e1e01040 9468 char *cond;
52d361e1 9469
6c5b2ebe 9470 const linespec_sals &lsal = canonical.lsals[0];
52d361e1 9471
b5fa468f
TBA
9472 find_condition_and_thread_for_sals (lsal.sals, extra_string,
9473 &cond, &thread, &task, &rest);
e1e01040
PA
9474 cond_string_copy.reset (cond);
9475 extra_string_copy.reset (rest);
72b2ff0e 9476 }
2f069f6f 9477 else
72b2ff0e 9478 {
f00aae0f
KS
9479 if (type_wanted != bp_dprintf
9480 && extra_string != NULL && *extra_string != '\0')
9481 error (_("Garbage '%s' at end of location"), extra_string);
0878d0fa
YQ
9482
9483 /* Create a private copy of condition string. */
9484 if (cond_string)
e1e01040 9485 cond_string_copy.reset (xstrdup (cond_string));
0878d0fa
YQ
9486 /* Create a private copy of any extra string. */
9487 if (extra_string)
e1e01040 9488 extra_string_copy.reset (xstrdup (extra_string));
72b2ff0e 9489 }
0fb4aa4b 9490
52d361e1 9491 ops->create_breakpoints_sal (gdbarch, &canonical,
e1e01040
PA
9492 std::move (cond_string_copy),
9493 std::move (extra_string_copy),
9494 type_wanted,
d9b3f62e
PA
9495 tempflag ? disp_del : disp_donttouch,
9496 thread, task, ignore_count, ops,
44f238bb 9497 from_tty, enabled, internal, flags);
c906108c 9498 }
0101ce28
JJ
9499 else
9500 {
a5e364af 9501 std::unique_ptr <breakpoint> b = new_breakpoint_from_type (type_wanted);
bfccc43c 9502
a5e364af 9503 init_raw_breakpoint_without_location (b.get (), gdbarch, type_wanted, ops);
d28cd78a 9504 b->location = copy_event_location (location);
bfccc43c 9505
f00aae0f
KS
9506 if (parse_extra)
9507 b->cond_string = NULL;
e12c7713
MK
9508 else
9509 {
9510 /* Create a private copy of condition string. */
e1e01040 9511 b->cond_string = cond_string != NULL ? xstrdup (cond_string) : NULL;
15630549 9512 b->thread = thread;
e12c7713 9513 }
f00aae0f
KS
9514
9515 /* Create a private copy of any extra string. */
e1e01040 9516 b->extra_string = extra_string != NULL ? xstrdup (extra_string) : NULL;
0101ce28 9517 b->ignore_count = ignore_count;
0101ce28 9518 b->disposition = tempflag ? disp_del : disp_donttouch;
0d381245 9519 b->condition_not_parsed = 1;
41447f92 9520 b->enable_state = enabled ? bp_enabled : bp_disabled;
cc72b2a2
KP
9521 if ((type_wanted != bp_breakpoint
9522 && type_wanted != bp_hardware_breakpoint) || thread != -1)
f8eba3c6 9523 b->pspace = current_program_space;
8bea4e01 9524
b270e6f9 9525 install_breakpoint (internal, std::move (b), 0);
0101ce28
JJ
9526 }
9527
6c5b2ebe 9528 if (canonical.lsals.size () > 1)
95a42b64 9529 {
3e43a32a
MS
9530 warning (_("Multiple breakpoints were set.\nUse the "
9531 "\"delete\" command to delete unwanted breakpoints."));
86b17b60 9532 prev_breakpoint_count = prev_bkpt_count;
95a42b64
TT
9533 }
9534
44702360 9535 update_global_location_list (UGLL_MAY_INSERT);
fd9b8c24
PA
9536
9537 return 1;
c3f6f71d 9538}
c906108c 9539
348d480f 9540/* Set a breakpoint.
72b2ff0e
VP
9541 ARG is a string describing breakpoint address,
9542 condition, and thread.
9543 FLAG specifies if a breakpoint is hardware on,
9544 and if breakpoint is temporary, using BP_HARDWARE_FLAG
9545 and BP_TEMPFLAG. */
348d480f 9546
98deb0da 9547static void
f2fc3015 9548break_command_1 (const char *arg, int flag, int from_tty)
c3f6f71d 9549{
72b2ff0e 9550 int tempflag = flag & BP_TEMPFLAG;
0fb4aa4b
PA
9551 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
9552 ? bp_hardware_breakpoint
9553 : bp_breakpoint);
f00aae0f 9554
ffc2605c 9555 event_location_up location = string_to_event_location (&arg, current_language);
bac7c5cf
GB
9556 const struct breakpoint_ops *ops = breakpoint_ops_for_event_location
9557 (location.get (), false /* is_tracepoint */);
c3f6f71d 9558
8cdf0e15 9559 create_breakpoint (get_current_arch (),
ffc2605c 9560 location.get (),
f00aae0f 9561 NULL, 0, arg, 1 /* parse arg */,
0fb4aa4b 9562 tempflag, type_wanted,
8cdf0e15
VP
9563 0 /* Ignore count */,
9564 pending_break_support,
55aa24fb 9565 ops,
8cdf0e15 9566 from_tty,
84f4c1fe 9567 1 /* enabled */,
44f238bb
PA
9568 0 /* internal */,
9569 0);
c906108c
SS
9570}
9571
c906108c
SS
9572/* Helper function for break_command_1 and disassemble_command. */
9573
9574void
fba45db2 9575resolve_sal_pc (struct symtab_and_line *sal)
c906108c
SS
9576{
9577 CORE_ADDR pc;
9578
9579 if (sal->pc == 0 && sal->symtab != NULL)
9580 {
9581 if (!find_line_pc (sal->symtab, sal->line, &pc))
8a3fe4f8 9582 error (_("No line %d in file \"%s\"."),
05cba821 9583 sal->line, symtab_to_filename_for_display (sal->symtab));
c906108c 9584 sal->pc = pc;
6a048695 9585
4a64f543
MS
9586 /* If this SAL corresponds to a breakpoint inserted using a line
9587 number, then skip the function prologue if necessary. */
6a048695 9588 if (sal->explicit_line)
059acae7 9589 skip_prologue_sal (sal);
c906108c
SS
9590 }
9591
9592 if (sal->section == 0 && sal->symtab != NULL)
9593 {
346d1dfe 9594 const struct blockvector *bv;
3977b71f 9595 const struct block *b;
c5aa993b 9596 struct symbol *sym;
c906108c 9597
43f3e411
DE
9598 bv = blockvector_for_pc_sect (sal->pc, 0, &b,
9599 SYMTAB_COMPUNIT (sal->symtab));
c906108c
SS
9600 if (bv != NULL)
9601 {
7f0df278 9602 sym = block_linkage_function (b);
c906108c
SS
9603 if (sym != NULL)
9604 {
eb822aa6
DE
9605 fixup_symbol_section (sym, SYMTAB_OBJFILE (sal->symtab));
9606 sal->section = SYMBOL_OBJ_SECTION (SYMTAB_OBJFILE (sal->symtab),
9607 sym);
c906108c
SS
9608 }
9609 else
9610 {
4a64f543
MS
9611 /* It really is worthwhile to have the section, so we'll
9612 just have to look harder. This case can be executed
9613 if we have line numbers but no functions (as can
9614 happen in assembly source). */
c906108c 9615
5ed8105e 9616 scoped_restore_current_pspace_and_thread restore_pspace_thread;
6c95b8df 9617 switch_to_program_space_and_thread (sal->pspace);
c906108c 9618
5ed8105e 9619 bound_minimal_symbol msym = lookup_minimal_symbol_by_pc (sal->pc);
7cbd4a93 9620 if (msym.minsym)
efd66ac6 9621 sal->section = MSYMBOL_OBJ_SECTION (msym.objfile, msym.minsym);
c906108c
SS
9622 }
9623 }
9624 }
9625}
9626
9627void
0b39b52e 9628break_command (const char *arg, int from_tty)
c906108c 9629{
db107f19 9630 break_command_1 (arg, 0, from_tty);
c906108c
SS
9631}
9632
c906108c 9633void
0b39b52e 9634tbreak_command (const char *arg, int from_tty)
c906108c 9635{
db107f19 9636 break_command_1 (arg, BP_TEMPFLAG, from_tty);
c906108c
SS
9637}
9638
c906108c 9639static void
0b39b52e 9640hbreak_command (const char *arg, int from_tty)
c906108c 9641{
db107f19 9642 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
c906108c
SS
9643}
9644
9645static void
0b39b52e 9646thbreak_command (const char *arg, int from_tty)
c906108c 9647{
db107f19 9648 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
c906108c
SS
9649}
9650
9651static void
ee7ddd71 9652stop_command (const char *arg, int from_tty)
c906108c 9653{
a3f17187 9654 printf_filtered (_("Specify the type of breakpoint to set.\n\
c906108c 9655Usage: stop in <function | address>\n\
a3f17187 9656 stop at <line>\n"));
c906108c
SS
9657}
9658
9659static void
4495129a 9660stopin_command (const char *arg, int from_tty)
c906108c
SS
9661{
9662 int badInput = 0;
9663
cafb3438 9664 if (arg == NULL)
c906108c
SS
9665 badInput = 1;
9666 else if (*arg != '*')
9667 {
4495129a 9668 const char *argptr = arg;
c906108c
SS
9669 int hasColon = 0;
9670
4a64f543 9671 /* Look for a ':'. If this is a line number specification, then
53a5351d 9672 say it is bad, otherwise, it should be an address or
4a64f543 9673 function/method name. */
c906108c 9674 while (*argptr && !hasColon)
c5aa993b
JM
9675 {
9676 hasColon = (*argptr == ':');
9677 argptr++;
9678 }
c906108c
SS
9679
9680 if (hasColon)
c5aa993b 9681 badInput = (*argptr != ':'); /* Not a class::method */
c906108c 9682 else
c5aa993b 9683 badInput = isdigit (*arg); /* a simple line number */
c906108c
SS
9684 }
9685
9686 if (badInput)
a3f17187 9687 printf_filtered (_("Usage: stop in <function | address>\n"));
c906108c 9688 else
db107f19 9689 break_command_1 (arg, 0, from_tty);
c906108c
SS
9690}
9691
9692static void
4495129a 9693stopat_command (const char *arg, int from_tty)
c906108c
SS
9694{
9695 int badInput = 0;
9696
cafb3438 9697 if (arg == NULL || *arg == '*') /* no line number */
c906108c
SS
9698 badInput = 1;
9699 else
9700 {
4495129a 9701 const char *argptr = arg;
c906108c
SS
9702 int hasColon = 0;
9703
4a64f543
MS
9704 /* Look for a ':'. If there is a '::' then get out, otherwise
9705 it is probably a line number. */
c906108c 9706 while (*argptr && !hasColon)
c5aa993b
JM
9707 {
9708 hasColon = (*argptr == ':');
9709 argptr++;
9710 }
c906108c
SS
9711
9712 if (hasColon)
c5aa993b 9713 badInput = (*argptr == ':'); /* we have class::method */
c906108c 9714 else
c5aa993b 9715 badInput = !isdigit (*arg); /* not a line number */
c906108c
SS
9716 }
9717
9718 if (badInput)
65e65158 9719 printf_filtered (_("Usage: stop at LINE\n"));
c906108c 9720 else
db107f19 9721 break_command_1 (arg, 0, from_tty);
c906108c
SS
9722}
9723
e7e0cddf
SS
9724/* The dynamic printf command is mostly like a regular breakpoint, but
9725 with a prewired command list consisting of a single output command,
9726 built from extra arguments supplied on the dprintf command
9727 line. */
9728
da821c7b 9729static void
0b39b52e 9730dprintf_command (const char *arg, int from_tty)
e7e0cddf 9731{
ffc2605c 9732 event_location_up location = string_to_event_location (&arg, current_language);
f00aae0f
KS
9733
9734 /* If non-NULL, ARG should have been advanced past the location;
9735 the next character must be ','. */
9736 if (arg != NULL)
9737 {
9738 if (arg[0] != ',' || arg[1] == '\0')
9739 error (_("Format string required"));
9740 else
9741 {
9742 /* Skip the comma. */
9743 ++arg;
9744 }
9745 }
9746
e7e0cddf 9747 create_breakpoint (get_current_arch (),
ffc2605c 9748 location.get (),
f00aae0f 9749 NULL, 0, arg, 1 /* parse arg */,
e7e0cddf
SS
9750 0, bp_dprintf,
9751 0 /* Ignore count */,
9752 pending_break_support,
9753 &dprintf_breakpoint_ops,
9754 from_tty,
9755 1 /* enabled */,
9756 0 /* internal */,
9757 0);
9758}
9759
d3ce09f5 9760static void
0b39b52e 9761agent_printf_command (const char *arg, int from_tty)
d3ce09f5
SS
9762{
9763 error (_("May only run agent-printf on the target"));
9764}
9765
f1310107
TJB
9766/* Implement the "breakpoint_hit" breakpoint_ops method for
9767 ranged breakpoints. */
9768
9769static int
9770breakpoint_hit_ranged_breakpoint (const struct bp_location *bl,
bd522513 9771 const address_space *aspace,
09ac7c10
TT
9772 CORE_ADDR bp_addr,
9773 const struct target_waitstatus *ws)
f1310107 9774{
09ac7c10 9775 if (ws->kind != TARGET_WAITKIND_STOPPED
a493e3e2 9776 || ws->value.sig != GDB_SIGNAL_TRAP)
09ac7c10
TT
9777 return 0;
9778
f1310107
TJB
9779 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
9780 bl->length, aspace, bp_addr);
9781}
9782
9783/* Implement the "resources_needed" breakpoint_ops method for
9784 ranged breakpoints. */
9785
9786static int
9787resources_needed_ranged_breakpoint (const struct bp_location *bl)
9788{
9789 return target_ranged_break_num_registers ();
9790}
9791
9792/* Implement the "print_it" breakpoint_ops method for
9793 ranged breakpoints. */
9794
9795static enum print_stop_action
348d480f 9796print_it_ranged_breakpoint (bpstat bs)
f1310107 9797{
348d480f 9798 struct breakpoint *b = bs->breakpoint_at;
f1310107 9799 struct bp_location *bl = b->loc;
79a45e25 9800 struct ui_out *uiout = current_uiout;
f1310107
TJB
9801
9802 gdb_assert (b->type == bp_hardware_breakpoint);
9803
9804 /* Ranged breakpoints have only one location. */
9805 gdb_assert (bl && bl->next == NULL);
9806
9807 annotate_breakpoint (b->number);
f303dbd6
PA
9808
9809 maybe_print_thread_hit_breakpoint (uiout);
9810
f1310107 9811 if (b->disposition == disp_del)
112e8700 9812 uiout->text ("Temporary ranged breakpoint ");
f1310107 9813 else
112e8700
SM
9814 uiout->text ("Ranged breakpoint ");
9815 if (uiout->is_mi_like_p ())
f1310107 9816 {
112e8700 9817 uiout->field_string ("reason",
f1310107 9818 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
112e8700 9819 uiout->field_string ("disp", bpdisp_text (b->disposition));
f1310107 9820 }
381befee 9821 uiout->field_signed ("bkptno", b->number);
112e8700 9822 uiout->text (", ");
f1310107
TJB
9823
9824 return PRINT_SRC_AND_LOC;
9825}
9826
9827/* Implement the "print_one" breakpoint_ops method for
9828 ranged breakpoints. */
9829
9830static void
9831print_one_ranged_breakpoint (struct breakpoint *b,
9832 struct bp_location **last_loc)
9833{
9834 struct bp_location *bl = b->loc;
9835 struct value_print_options opts;
79a45e25 9836 struct ui_out *uiout = current_uiout;
f1310107
TJB
9837
9838 /* Ranged breakpoints have only one location. */
9839 gdb_assert (bl && bl->next == NULL);
9840
9841 get_user_print_options (&opts);
9842
9843 if (opts.addressprint)
9844 /* We don't print the address range here, it will be printed later
9845 by print_one_detail_ranged_breakpoint. */
112e8700 9846 uiout->field_skip ("addr");
f1310107
TJB
9847 annotate_field (5);
9848 print_breakpoint_location (b, bl);
9849 *last_loc = bl;
9850}
9851
9852/* Implement the "print_one_detail" breakpoint_ops method for
9853 ranged breakpoints. */
9854
9855static void
9856print_one_detail_ranged_breakpoint (const struct breakpoint *b,
9857 struct ui_out *uiout)
9858{
9859 CORE_ADDR address_start, address_end;
9860 struct bp_location *bl = b->loc;
d7e74731 9861 string_file stb;
f1310107
TJB
9862
9863 gdb_assert (bl);
9864
9865 address_start = bl->address;
9866 address_end = address_start + bl->length - 1;
9867
112e8700 9868 uiout->text ("\taddress range: ");
d7e74731
PA
9869 stb.printf ("[%s, %s]",
9870 print_core_address (bl->gdbarch, address_start),
9871 print_core_address (bl->gdbarch, address_end));
112e8700
SM
9872 uiout->field_stream ("addr", stb);
9873 uiout->text ("\n");
f1310107
TJB
9874}
9875
9876/* Implement the "print_mention" breakpoint_ops method for
9877 ranged breakpoints. */
9878
9879static void
9880print_mention_ranged_breakpoint (struct breakpoint *b)
9881{
9882 struct bp_location *bl = b->loc;
79a45e25 9883 struct ui_out *uiout = current_uiout;
f1310107
TJB
9884
9885 gdb_assert (bl);
9886 gdb_assert (b->type == bp_hardware_breakpoint);
9887
2d33446d
TT
9888 uiout->message (_("Hardware assisted ranged breakpoint %d from %s to %s."),
9889 b->number, paddress (bl->gdbarch, bl->address),
9890 paddress (bl->gdbarch, bl->address + bl->length - 1));
f1310107
TJB
9891}
9892
9893/* Implement the "print_recreate" breakpoint_ops method for
9894 ranged breakpoints. */
9895
9896static void
9897print_recreate_ranged_breakpoint (struct breakpoint *b, struct ui_file *fp)
9898{
f00aae0f 9899 fprintf_unfiltered (fp, "break-range %s, %s",
d28cd78a
TT
9900 event_location_to_string (b->location.get ()),
9901 event_location_to_string (b->location_range_end.get ()));
d9b3f62e 9902 print_recreate_thread (b, fp);
f1310107
TJB
9903}
9904
9905/* The breakpoint_ops structure to be used in ranged breakpoints. */
9906
2060206e 9907static struct breakpoint_ops ranged_breakpoint_ops;
f1310107
TJB
9908
9909/* Find the address where the end of the breakpoint range should be
9910 placed, given the SAL of the end of the range. This is so that if
9911 the user provides a line number, the end of the range is set to the
9912 last instruction of the given line. */
9913
9914static CORE_ADDR
9915find_breakpoint_range_end (struct symtab_and_line sal)
9916{
9917 CORE_ADDR end;
9918
9919 /* If the user provided a PC value, use it. Otherwise,
9920 find the address of the end of the given location. */
9921 if (sal.explicit_pc)
9922 end = sal.pc;
9923 else
9924 {
9925 int ret;
9926 CORE_ADDR start;
9927
9928 ret = find_line_pc_range (sal, &start, &end);
9929 if (!ret)
9930 error (_("Could not find location of the end of the range."));
9931
9932 /* find_line_pc_range returns the start of the next line. */
9933 end--;
9934 }
9935
9936 return end;
9937}
9938
9939/* Implement the "break-range" CLI command. */
9940
9941static void
0b39b52e 9942break_range_command (const char *arg, int from_tty)
f1310107 9943{
f2fc3015 9944 const char *arg_start;
f1310107
TJB
9945 struct linespec_result canonical_start, canonical_end;
9946 int bp_count, can_use_bp, length;
9947 CORE_ADDR end;
9948 struct breakpoint *b;
f1310107
TJB
9949
9950 /* We don't support software ranged breakpoints. */
9951 if (target_ranged_break_num_registers () < 0)
9952 error (_("This target does not support hardware ranged breakpoints."));
9953
9954 bp_count = hw_breakpoint_used_count ();
9955 bp_count += target_ranged_break_num_registers ();
9956 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
9957 bp_count, 0);
9958 if (can_use_bp < 0)
9959 error (_("Hardware breakpoints used exceeds limit."));
9960
f8eba3c6 9961 arg = skip_spaces (arg);
f1310107
TJB
9962 if (arg == NULL || arg[0] == '\0')
9963 error(_("No address range specified."));
9964
f8eba3c6 9965 arg_start = arg;
ffc2605c
TT
9966 event_location_up start_location = string_to_event_location (&arg,
9967 current_language);
9968 parse_breakpoint_sals (start_location.get (), &canonical_start);
f1310107
TJB
9969
9970 if (arg[0] != ',')
9971 error (_("Too few arguments."));
6c5b2ebe 9972 else if (canonical_start.lsals.empty ())
f1310107 9973 error (_("Could not find location of the beginning of the range."));
f8eba3c6 9974
6c5b2ebe 9975 const linespec_sals &lsal_start = canonical_start.lsals[0];
f8eba3c6 9976
6c5b2ebe
PA
9977 if (canonical_start.lsals.size () > 1
9978 || lsal_start.sals.size () != 1)
f1310107
TJB
9979 error (_("Cannot create a ranged breakpoint with multiple locations."));
9980
6c5b2ebe 9981 const symtab_and_line &sal_start = lsal_start.sals[0];
81b1e71c 9982 std::string addr_string_start (arg_start, arg - arg_start);
f1310107
TJB
9983
9984 arg++; /* Skip the comma. */
f8eba3c6 9985 arg = skip_spaces (arg);
f1310107
TJB
9986
9987 /* Parse the end location. */
9988
f1310107
TJB
9989 arg_start = arg;
9990
f8eba3c6 9991 /* We call decode_line_full directly here instead of using
f1310107
TJB
9992 parse_breakpoint_sals because we need to specify the start location's
9993 symtab and line as the default symtab and line for the end of the
9994 range. This makes it possible to have ranges like "foo.c:27, +14",
9995 where +14 means 14 lines from the start location. */
ffc2605c
TT
9996 event_location_up end_location = string_to_event_location (&arg,
9997 current_language);
9998 decode_line_full (end_location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
f8eba3c6
TT
9999 sal_start.symtab, sal_start.line,
10000 &canonical_end, NULL, NULL);
10001
6c5b2ebe 10002 if (canonical_end.lsals.empty ())
f1310107 10003 error (_("Could not find location of the end of the range."));
f8eba3c6 10004
6c5b2ebe
PA
10005 const linespec_sals &lsal_end = canonical_end.lsals[0];
10006 if (canonical_end.lsals.size () > 1
10007 || lsal_end.sals.size () != 1)
f1310107
TJB
10008 error (_("Cannot create a ranged breakpoint with multiple locations."));
10009
6c5b2ebe 10010 const symtab_and_line &sal_end = lsal_end.sals[0];
f1310107
TJB
10011
10012 end = find_breakpoint_range_end (sal_end);
10013 if (sal_start.pc > end)
177b42fe 10014 error (_("Invalid address range, end precedes start."));
f1310107
TJB
10015
10016 length = end - sal_start.pc + 1;
10017 if (length < 0)
10018 /* Length overflowed. */
10019 error (_("Address range too large."));
10020 else if (length == 1)
10021 {
10022 /* This range is simple enough to be handled by
10023 the `hbreak' command. */
81b1e71c 10024 hbreak_command (&addr_string_start[0], 1);
f1310107
TJB
10025
10026 return;
10027 }
10028
10029 /* Now set up the breakpoint. */
10030 b = set_raw_breakpoint (get_current_arch (), sal_start,
348d480f 10031 bp_hardware_breakpoint, &ranged_breakpoint_ops);
f1310107
TJB
10032 set_breakpoint_count (breakpoint_count + 1);
10033 b->number = breakpoint_count;
10034 b->disposition = disp_donttouch;
d28cd78a
TT
10035 b->location = std::move (start_location);
10036 b->location_range_end = std::move (end_location);
f1310107
TJB
10037 b->loc->length = length;
10038
f1310107 10039 mention (b);
76727919 10040 gdb::observers::breakpoint_created.notify (b);
44702360 10041 update_global_location_list (UGLL_MAY_INSERT);
f1310107
TJB
10042}
10043
4a64f543
MS
10044/* Return non-zero if EXP is verified as constant. Returned zero
10045 means EXP is variable. Also the constant detection may fail for
10046 some constant expressions and in such case still falsely return
10047 zero. */
2e6e3d9c 10048
65d79d4b
SDJ
10049static int
10050watchpoint_exp_is_const (const struct expression *exp)
10051{
10052 int i = exp->nelts;
10053
10054 while (i > 0)
10055 {
10056 int oplenp, argsp;
10057
10058 /* We are only interested in the descriptor of each element. */
10059 operator_length (exp, i, &oplenp, &argsp);
10060 i -= oplenp;
10061
10062 switch (exp->elts[i].opcode)
10063 {
10064 case BINOP_ADD:
10065 case BINOP_SUB:
10066 case BINOP_MUL:
10067 case BINOP_DIV:
10068 case BINOP_REM:
10069 case BINOP_MOD:
10070 case BINOP_LSH:
10071 case BINOP_RSH:
10072 case BINOP_LOGICAL_AND:
10073 case BINOP_LOGICAL_OR:
10074 case BINOP_BITWISE_AND:
10075 case BINOP_BITWISE_IOR:
10076 case BINOP_BITWISE_XOR:
10077 case BINOP_EQUAL:
10078 case BINOP_NOTEQUAL:
10079 case BINOP_LESS:
10080 case BINOP_GTR:
10081 case BINOP_LEQ:
10082 case BINOP_GEQ:
10083 case BINOP_REPEAT:
10084 case BINOP_COMMA:
10085 case BINOP_EXP:
10086 case BINOP_MIN:
10087 case BINOP_MAX:
10088 case BINOP_INTDIV:
10089 case BINOP_CONCAT:
65d79d4b
SDJ
10090 case TERNOP_COND:
10091 case TERNOP_SLICE:
65d79d4b
SDJ
10092
10093 case OP_LONG:
edd079d9 10094 case OP_FLOAT:
65d79d4b
SDJ
10095 case OP_LAST:
10096 case OP_COMPLEX:
10097 case OP_STRING:
65d79d4b
SDJ
10098 case OP_ARRAY:
10099 case OP_TYPE:
608b4967
TT
10100 case OP_TYPEOF:
10101 case OP_DECLTYPE:
6e72ca20 10102 case OP_TYPEID:
65d79d4b
SDJ
10103 case OP_NAME:
10104 case OP_OBJC_NSSTRING:
10105
10106 case UNOP_NEG:
10107 case UNOP_LOGICAL_NOT:
10108 case UNOP_COMPLEMENT:
10109 case UNOP_ADDR:
10110 case UNOP_HIGH:
aeaa2474 10111 case UNOP_CAST:
9eaf6705
TT
10112
10113 case UNOP_CAST_TYPE:
10114 case UNOP_REINTERPRET_CAST:
10115 case UNOP_DYNAMIC_CAST:
4a64f543
MS
10116 /* Unary, binary and ternary operators: We have to check
10117 their operands. If they are constant, then so is the
10118 result of that operation. For instance, if A and B are
10119 determined to be constants, then so is "A + B".
10120
10121 UNOP_IND is one exception to the rule above, because the
10122 value of *ADDR is not necessarily a constant, even when
10123 ADDR is. */
65d79d4b
SDJ
10124 break;
10125
10126 case OP_VAR_VALUE:
10127 /* Check whether the associated symbol is a constant.
4a64f543 10128
65d79d4b 10129 We use SYMBOL_CLASS rather than TYPE_CONST because it's
4a64f543
MS
10130 possible that a buggy compiler could mark a variable as
10131 constant even when it is not, and TYPE_CONST would return
10132 true in this case, while SYMBOL_CLASS wouldn't.
10133
10134 We also have to check for function symbols because they
10135 are always constant. */
65d79d4b
SDJ
10136 {
10137 struct symbol *s = exp->elts[i + 2].symbol;
10138
10139 if (SYMBOL_CLASS (s) != LOC_BLOCK
10140 && SYMBOL_CLASS (s) != LOC_CONST
10141 && SYMBOL_CLASS (s) != LOC_CONST_BYTES)
10142 return 0;
10143 break;
10144 }
10145
10146 /* The default action is to return 0 because we are using
10147 the optimistic approach here: If we don't know something,
10148 then it is not a constant. */
10149 default:
10150 return 0;
10151 }
10152 }
10153
10154 return 1;
10155}
10156
c1fc2657 10157/* Watchpoint destructor. */
3a5c3e22 10158
c1fc2657 10159watchpoint::~watchpoint ()
3a5c3e22 10160{
c1fc2657
SM
10161 xfree (this->exp_string);
10162 xfree (this->exp_string_reparse);
3a5c3e22
PA
10163}
10164
348d480f
PA
10165/* Implement the "re_set" breakpoint_ops method for watchpoints. */
10166
10167static void
10168re_set_watchpoint (struct breakpoint *b)
10169{
3a5c3e22
PA
10170 struct watchpoint *w = (struct watchpoint *) b;
10171
348d480f
PA
10172 /* Watchpoint can be either on expression using entirely global
10173 variables, or it can be on local variables.
10174
10175 Watchpoints of the first kind are never auto-deleted, and even
10176 persist across program restarts. Since they can use variables
10177 from shared libraries, we need to reparse expression as libraries
10178 are loaded and unloaded.
10179
10180 Watchpoints on local variables can also change meaning as result
10181 of solib event. For example, if a watchpoint uses both a local
10182 and a global variables in expression, it's a local watchpoint,
10183 but unloading of a shared library will make the expression
10184 invalid. This is not a very common use case, but we still
10185 re-evaluate expression, to avoid surprises to the user.
10186
10187 Note that for local watchpoints, we re-evaluate it only if
10188 watchpoints frame id is still valid. If it's not, it means the
10189 watchpoint is out of scope and will be deleted soon. In fact,
10190 I'm not sure we'll ever be called in this case.
10191
10192 If a local watchpoint's frame id is still valid, then
3a5c3e22 10193 w->exp_valid_block is likewise valid, and we can safely use it.
348d480f 10194
3a5c3e22
PA
10195 Don't do anything about disabled watchpoints, since they will be
10196 reevaluated again when enabled. */
10197 update_watchpoint (w, 1 /* reparse */);
348d480f
PA
10198}
10199
77b06cd7
TJB
10200/* Implement the "insert" breakpoint_ops method for hardware watchpoints. */
10201
10202static int
10203insert_watchpoint (struct bp_location *bl)
10204{
3a5c3e22
PA
10205 struct watchpoint *w = (struct watchpoint *) bl->owner;
10206 int length = w->exact ? 1 : bl->length;
e09342b5
TJB
10207
10208 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
4d01a485 10209 w->cond_exp.get ());
77b06cd7
TJB
10210}
10211
10212/* Implement the "remove" breakpoint_ops method for hardware watchpoints. */
10213
10214static int
73971819 10215remove_watchpoint (struct bp_location *bl, enum remove_bp_reason reason)
77b06cd7 10216{
3a5c3e22
PA
10217 struct watchpoint *w = (struct watchpoint *) bl->owner;
10218 int length = w->exact ? 1 : bl->length;
e09342b5
TJB
10219
10220 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
4d01a485 10221 w->cond_exp.get ());
e09342b5
TJB
10222}
10223
e09342b5 10224static int
348d480f 10225breakpoint_hit_watchpoint (const struct bp_location *bl,
bd522513 10226 const address_space *aspace, CORE_ADDR bp_addr,
09ac7c10 10227 const struct target_waitstatus *ws)
e09342b5 10228{
348d480f 10229 struct breakpoint *b = bl->owner;
3a5c3e22 10230 struct watchpoint *w = (struct watchpoint *) b;
77b06cd7 10231
348d480f
PA
10232 /* Continuable hardware watchpoints are treated as non-existent if the
10233 reason we stopped wasn't a hardware watchpoint (we didn't stop on
10234 some data address). Otherwise gdb won't stop on a break instruction
10235 in the code (not from a breakpoint) when a hardware watchpoint has
10236 been defined. Also skip watchpoints which we know did not trigger
10237 (did not match the data address). */
10238 if (is_hardware_watchpoint (b)
3a5c3e22 10239 && w->watchpoint_triggered == watch_triggered_no)
348d480f 10240 return 0;
9c06b0b4 10241
348d480f 10242 return 1;
9c06b0b4
TJB
10243}
10244
348d480f
PA
10245static void
10246check_status_watchpoint (bpstat bs)
9c06b0b4 10247{
348d480f 10248 gdb_assert (is_watchpoint (bs->breakpoint_at));
9c06b0b4 10249
348d480f 10250 bpstat_check_watchpoint (bs);
9c06b0b4
TJB
10251}
10252
10253/* Implement the "resources_needed" breakpoint_ops method for
348d480f 10254 hardware watchpoints. */
9c06b0b4
TJB
10255
10256static int
348d480f 10257resources_needed_watchpoint (const struct bp_location *bl)
9c06b0b4 10258{
3a5c3e22
PA
10259 struct watchpoint *w = (struct watchpoint *) bl->owner;
10260 int length = w->exact? 1 : bl->length;
348d480f
PA
10261
10262 return target_region_ok_for_hw_watchpoint (bl->address, length);
9c06b0b4
TJB
10263}
10264
10265/* Implement the "works_in_software_mode" breakpoint_ops method for
348d480f 10266 hardware watchpoints. */
9c06b0b4
TJB
10267
10268static int
348d480f 10269works_in_software_mode_watchpoint (const struct breakpoint *b)
9c06b0b4 10270{
efa80663
PA
10271 /* Read and access watchpoints only work with hardware support. */
10272 return b->type == bp_watchpoint || b->type == bp_hardware_watchpoint;
9c06b0b4
TJB
10273}
10274
9c06b0b4 10275static enum print_stop_action
348d480f 10276print_it_watchpoint (bpstat bs)
9c06b0b4 10277{
348d480f 10278 struct breakpoint *b;
348d480f 10279 enum print_stop_action result;
3a5c3e22 10280 struct watchpoint *w;
79a45e25 10281 struct ui_out *uiout = current_uiout;
348d480f
PA
10282
10283 gdb_assert (bs->bp_location_at != NULL);
10284
348d480f 10285 b = bs->breakpoint_at;
3a5c3e22 10286 w = (struct watchpoint *) b;
348d480f 10287
f303dbd6
PA
10288 annotate_watchpoint (b->number);
10289 maybe_print_thread_hit_breakpoint (uiout);
10290
d7e74731
PA
10291 string_file stb;
10292
76f9c9cf 10293 gdb::optional<ui_out_emit_tuple> tuple_emitter;
9c06b0b4
TJB
10294 switch (b->type)
10295 {
348d480f 10296 case bp_watchpoint:
9c06b0b4 10297 case bp_hardware_watchpoint:
112e8700
SM
10298 if (uiout->is_mi_like_p ())
10299 uiout->field_string
10300 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
348d480f 10301 mention (b);
76f9c9cf 10302 tuple_emitter.emplace (uiout, "value");
112e8700 10303 uiout->text ("\nOld value = ");
850645cf 10304 watchpoint_value_print (bs->old_val.get (), &stb);
112e8700
SM
10305 uiout->field_stream ("old", stb);
10306 uiout->text ("\nNew value = ");
850645cf 10307 watchpoint_value_print (w->val.get (), &stb);
112e8700
SM
10308 uiout->field_stream ("new", stb);
10309 uiout->text ("\n");
348d480f
PA
10310 /* More than one watchpoint may have been triggered. */
10311 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10312 break;
10313
10314 case bp_read_watchpoint:
112e8700
SM
10315 if (uiout->is_mi_like_p ())
10316 uiout->field_string
10317 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
348d480f 10318 mention (b);
76f9c9cf 10319 tuple_emitter.emplace (uiout, "value");
112e8700 10320 uiout->text ("\nValue = ");
850645cf 10321 watchpoint_value_print (w->val.get (), &stb);
112e8700
SM
10322 uiout->field_stream ("value", stb);
10323 uiout->text ("\n");
348d480f 10324 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10325 break;
10326
10327 case bp_access_watchpoint:
348d480f
PA
10328 if (bs->old_val != NULL)
10329 {
112e8700
SM
10330 if (uiout->is_mi_like_p ())
10331 uiout->field_string
10332 ("reason",
348d480f
PA
10333 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10334 mention (b);
76f9c9cf 10335 tuple_emitter.emplace (uiout, "value");
112e8700 10336 uiout->text ("\nOld value = ");
850645cf 10337 watchpoint_value_print (bs->old_val.get (), &stb);
112e8700
SM
10338 uiout->field_stream ("old", stb);
10339 uiout->text ("\nNew value = ");
348d480f
PA
10340 }
10341 else
10342 {
10343 mention (b);
112e8700
SM
10344 if (uiout->is_mi_like_p ())
10345 uiout->field_string
10346 ("reason",
348d480f 10347 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
76f9c9cf 10348 tuple_emitter.emplace (uiout, "value");
112e8700 10349 uiout->text ("\nValue = ");
348d480f 10350 }
850645cf 10351 watchpoint_value_print (w->val.get (), &stb);
112e8700
SM
10352 uiout->field_stream ("new", stb);
10353 uiout->text ("\n");
348d480f 10354 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10355 break;
10356 default:
348d480f 10357 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10358 }
10359
348d480f
PA
10360 return result;
10361}
10362
10363/* Implement the "print_mention" breakpoint_ops method for hardware
10364 watchpoints. */
10365
10366static void
10367print_mention_watchpoint (struct breakpoint *b)
10368{
3a5c3e22 10369 struct watchpoint *w = (struct watchpoint *) b;
79a45e25 10370 struct ui_out *uiout = current_uiout;
46b9c129 10371 const char *tuple_name;
348d480f
PA
10372
10373 switch (b->type)
10374 {
10375 case bp_watchpoint:
112e8700 10376 uiout->text ("Watchpoint ");
46b9c129 10377 tuple_name = "wpt";
348d480f
PA
10378 break;
10379 case bp_hardware_watchpoint:
112e8700 10380 uiout->text ("Hardware watchpoint ");
46b9c129 10381 tuple_name = "wpt";
348d480f
PA
10382 break;
10383 case bp_read_watchpoint:
112e8700 10384 uiout->text ("Hardware read watchpoint ");
46b9c129 10385 tuple_name = "hw-rwpt";
348d480f
PA
10386 break;
10387 case bp_access_watchpoint:
112e8700 10388 uiout->text ("Hardware access (read/write) watchpoint ");
46b9c129 10389 tuple_name = "hw-awpt";
348d480f
PA
10390 break;
10391 default:
10392 internal_error (__FILE__, __LINE__,
10393 _("Invalid hardware watchpoint type."));
10394 }
10395
46b9c129 10396 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
381befee 10397 uiout->field_signed ("number", b->number);
112e8700
SM
10398 uiout->text (": ");
10399 uiout->field_string ("exp", w->exp_string);
348d480f
PA
10400}
10401
10402/* Implement the "print_recreate" breakpoint_ops method for
10403 watchpoints. */
10404
10405static void
10406print_recreate_watchpoint (struct breakpoint *b, struct ui_file *fp)
10407{
3a5c3e22
PA
10408 struct watchpoint *w = (struct watchpoint *) b;
10409
348d480f
PA
10410 switch (b->type)
10411 {
10412 case bp_watchpoint:
10413 case bp_hardware_watchpoint:
10414 fprintf_unfiltered (fp, "watch");
10415 break;
10416 case bp_read_watchpoint:
10417 fprintf_unfiltered (fp, "rwatch");
10418 break;
10419 case bp_access_watchpoint:
10420 fprintf_unfiltered (fp, "awatch");
10421 break;
10422 default:
10423 internal_error (__FILE__, __LINE__,
10424 _("Invalid watchpoint type."));
10425 }
10426
3a5c3e22 10427 fprintf_unfiltered (fp, " %s", w->exp_string);
d9b3f62e 10428 print_recreate_thread (b, fp);
348d480f
PA
10429}
10430
427cd150
TT
10431/* Implement the "explains_signal" breakpoint_ops method for
10432 watchpoints. */
10433
47591c29 10434static int
427cd150
TT
10435explains_signal_watchpoint (struct breakpoint *b, enum gdb_signal sig)
10436{
10437 /* A software watchpoint cannot cause a signal other than
10438 GDB_SIGNAL_TRAP. */
10439 if (b->type == bp_watchpoint && sig != GDB_SIGNAL_TRAP)
47591c29 10440 return 0;
427cd150 10441
47591c29 10442 return 1;
427cd150
TT
10443}
10444
348d480f
PA
10445/* The breakpoint_ops structure to be used in hardware watchpoints. */
10446
2060206e 10447static struct breakpoint_ops watchpoint_breakpoint_ops;
348d480f
PA
10448
10449/* Implement the "insert" breakpoint_ops method for
10450 masked hardware watchpoints. */
10451
10452static int
10453insert_masked_watchpoint (struct bp_location *bl)
10454{
3a5c3e22
PA
10455 struct watchpoint *w = (struct watchpoint *) bl->owner;
10456
10457 return target_insert_mask_watchpoint (bl->address, w->hw_wp_mask,
348d480f
PA
10458 bl->watchpoint_type);
10459}
10460
10461/* Implement the "remove" breakpoint_ops method for
10462 masked hardware watchpoints. */
10463
10464static int
73971819 10465remove_masked_watchpoint (struct bp_location *bl, enum remove_bp_reason reason)
348d480f 10466{
3a5c3e22
PA
10467 struct watchpoint *w = (struct watchpoint *) bl->owner;
10468
10469 return target_remove_mask_watchpoint (bl->address, w->hw_wp_mask,
348d480f
PA
10470 bl->watchpoint_type);
10471}
10472
10473/* Implement the "resources_needed" breakpoint_ops method for
10474 masked hardware watchpoints. */
10475
10476static int
10477resources_needed_masked_watchpoint (const struct bp_location *bl)
10478{
3a5c3e22
PA
10479 struct watchpoint *w = (struct watchpoint *) bl->owner;
10480
10481 return target_masked_watch_num_registers (bl->address, w->hw_wp_mask);
348d480f
PA
10482}
10483
10484/* Implement the "works_in_software_mode" breakpoint_ops method for
10485 masked hardware watchpoints. */
10486
10487static int
10488works_in_software_mode_masked_watchpoint (const struct breakpoint *b)
10489{
10490 return 0;
10491}
10492
10493/* Implement the "print_it" breakpoint_ops method for
10494 masked hardware watchpoints. */
10495
10496static enum print_stop_action
10497print_it_masked_watchpoint (bpstat bs)
10498{
10499 struct breakpoint *b = bs->breakpoint_at;
79a45e25 10500 struct ui_out *uiout = current_uiout;
348d480f
PA
10501
10502 /* Masked watchpoints have only one location. */
10503 gdb_assert (b->loc && b->loc->next == NULL);
10504
f303dbd6
PA
10505 annotate_watchpoint (b->number);
10506 maybe_print_thread_hit_breakpoint (uiout);
10507
348d480f
PA
10508 switch (b->type)
10509 {
10510 case bp_hardware_watchpoint:
112e8700
SM
10511 if (uiout->is_mi_like_p ())
10512 uiout->field_string
10513 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
348d480f
PA
10514 break;
10515
10516 case bp_read_watchpoint:
112e8700
SM
10517 if (uiout->is_mi_like_p ())
10518 uiout->field_string
10519 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
348d480f
PA
10520 break;
10521
10522 case bp_access_watchpoint:
112e8700
SM
10523 if (uiout->is_mi_like_p ())
10524 uiout->field_string
10525 ("reason",
348d480f
PA
10526 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10527 break;
10528 default:
10529 internal_error (__FILE__, __LINE__,
10530 _("Invalid hardware watchpoint type."));
10531 }
10532
10533 mention (b);
112e8700 10534 uiout->text (_("\n\
9c06b0b4
TJB
10535Check the underlying instruction at PC for the memory\n\
10536address and value which triggered this watchpoint.\n"));
112e8700 10537 uiout->text ("\n");
9c06b0b4
TJB
10538
10539 /* More than one watchpoint may have been triggered. */
10540 return PRINT_UNKNOWN;
10541}
10542
10543/* Implement the "print_one_detail" breakpoint_ops method for
10544 masked hardware watchpoints. */
10545
10546static void
10547print_one_detail_masked_watchpoint (const struct breakpoint *b,
10548 struct ui_out *uiout)
10549{
3a5c3e22
PA
10550 struct watchpoint *w = (struct watchpoint *) b;
10551
9c06b0b4
TJB
10552 /* Masked watchpoints have only one location. */
10553 gdb_assert (b->loc && b->loc->next == NULL);
10554
112e8700
SM
10555 uiout->text ("\tmask ");
10556 uiout->field_core_addr ("mask", b->loc->gdbarch, w->hw_wp_mask);
10557 uiout->text ("\n");
9c06b0b4
TJB
10558}
10559
10560/* Implement the "print_mention" breakpoint_ops method for
10561 masked hardware watchpoints. */
10562
10563static void
10564print_mention_masked_watchpoint (struct breakpoint *b)
10565{
3a5c3e22 10566 struct watchpoint *w = (struct watchpoint *) b;
79a45e25 10567 struct ui_out *uiout = current_uiout;
46b9c129 10568 const char *tuple_name;
9c06b0b4
TJB
10569
10570 switch (b->type)
10571 {
10572 case bp_hardware_watchpoint:
112e8700 10573 uiout->text ("Masked hardware watchpoint ");
46b9c129 10574 tuple_name = "wpt";
9c06b0b4
TJB
10575 break;
10576 case bp_read_watchpoint:
112e8700 10577 uiout->text ("Masked hardware read watchpoint ");
46b9c129 10578 tuple_name = "hw-rwpt";
9c06b0b4
TJB
10579 break;
10580 case bp_access_watchpoint:
112e8700 10581 uiout->text ("Masked hardware access (read/write) watchpoint ");
46b9c129 10582 tuple_name = "hw-awpt";
9c06b0b4
TJB
10583 break;
10584 default:
10585 internal_error (__FILE__, __LINE__,
10586 _("Invalid hardware watchpoint type."));
10587 }
10588
46b9c129 10589 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
381befee 10590 uiout->field_signed ("number", b->number);
112e8700
SM
10591 uiout->text (": ");
10592 uiout->field_string ("exp", w->exp_string);
9c06b0b4
TJB
10593}
10594
10595/* Implement the "print_recreate" breakpoint_ops method for
10596 masked hardware watchpoints. */
10597
10598static void
10599print_recreate_masked_watchpoint (struct breakpoint *b, struct ui_file *fp)
10600{
3a5c3e22 10601 struct watchpoint *w = (struct watchpoint *) b;
9c06b0b4
TJB
10602
10603 switch (b->type)
10604 {
10605 case bp_hardware_watchpoint:
10606 fprintf_unfiltered (fp, "watch");
10607 break;
10608 case bp_read_watchpoint:
10609 fprintf_unfiltered (fp, "rwatch");
10610 break;
10611 case bp_access_watchpoint:
10612 fprintf_unfiltered (fp, "awatch");
10613 break;
10614 default:
10615 internal_error (__FILE__, __LINE__,
10616 _("Invalid hardware watchpoint type."));
10617 }
10618
53807e9f
TT
10619 fprintf_unfiltered (fp, " %s mask 0x%s", w->exp_string,
10620 phex (w->hw_wp_mask, sizeof (CORE_ADDR)));
d9b3f62e 10621 print_recreate_thread (b, fp);
9c06b0b4
TJB
10622}
10623
10624/* The breakpoint_ops structure to be used in masked hardware watchpoints. */
10625
2060206e 10626static struct breakpoint_ops masked_watchpoint_breakpoint_ops;
9c06b0b4
TJB
10627
10628/* Tell whether the given watchpoint is a masked hardware watchpoint. */
10629
f2478a7e 10630static bool
9c06b0b4
TJB
10631is_masked_watchpoint (const struct breakpoint *b)
10632{
10633 return b->ops == &masked_watchpoint_breakpoint_ops;
10634}
10635
53a5351d
JM
10636/* accessflag: hw_write: watch write,
10637 hw_read: watch read,
10638 hw_access: watch access (read or write) */
c906108c 10639static void
bbc13ae3 10640watch_command_1 (const char *arg, int accessflag, int from_tty,
84f4c1fe 10641 int just_location, int internal)
c906108c 10642{
c1fc2657 10643 struct breakpoint *scope_breakpoint = NULL;
270140bd 10644 const struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
b926417a 10645 struct value *result;
bb9d5f81 10646 int saved_bitpos = 0, saved_bitsize = 0;
bbc13ae3
KS
10647 const char *exp_start = NULL;
10648 const char *exp_end = NULL;
10649 const char *tok, *end_tok;
9c06b0b4 10650 int toklen = -1;
bbc13ae3
KS
10651 const char *cond_start = NULL;
10652 const char *cond_end = NULL;
c906108c 10653 enum bptype bp_type;
37e4754d 10654 int thread = -1;
0cf6dd15 10655 int pc = 0;
9c06b0b4
TJB
10656 /* Flag to indicate whether we are going to use masks for
10657 the hardware watchpoint. */
10658 int use_mask = 0;
10659 CORE_ADDR mask = 0;
c906108c 10660
37e4754d
LM
10661 /* Make sure that we actually have parameters to parse. */
10662 if (arg != NULL && arg[0] != '\0')
10663 {
bbc13ae3
KS
10664 const char *value_start;
10665
10666 exp_end = arg + strlen (arg);
37e4754d 10667
9c06b0b4
TJB
10668 /* Look for "parameter value" pairs at the end
10669 of the arguments string. */
bbc13ae3 10670 for (tok = exp_end - 1; tok > arg; tok--)
9c06b0b4
TJB
10671 {
10672 /* Skip whitespace at the end of the argument list. */
10673 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10674 tok--;
10675
10676 /* Find the beginning of the last token.
10677 This is the value of the parameter. */
10678 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10679 tok--;
10680 value_start = tok + 1;
10681
10682 /* Skip whitespace. */
10683 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10684 tok--;
10685
10686 end_tok = tok;
10687
10688 /* Find the beginning of the second to last token.
10689 This is the parameter itself. */
10690 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10691 tok--;
10692 tok++;
10693 toklen = end_tok - tok + 1;
10694
61012eef 10695 if (toklen == 6 && startswith (tok, "thread"))
9c06b0b4 10696 {
5d5658a1 10697 struct thread_info *thr;
9c06b0b4
TJB
10698 /* At this point we've found a "thread" token, which means
10699 the user is trying to set a watchpoint that triggers
10700 only in a specific thread. */
5d5658a1 10701 const char *endp;
37e4754d 10702
9c06b0b4
TJB
10703 if (thread != -1)
10704 error(_("You can specify only one thread."));
37e4754d 10705
9c06b0b4 10706 /* Extract the thread ID from the next token. */
5d5658a1 10707 thr = parse_thread_id (value_start, &endp);
37e4754d 10708
5d5658a1 10709 /* Check if the user provided a valid thread ID. */
9c06b0b4 10710 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
5d5658a1 10711 invalid_thread_id_error (value_start);
9c06b0b4 10712
5d5658a1 10713 thread = thr->global_num;
9c06b0b4 10714 }
61012eef 10715 else if (toklen == 4 && startswith (tok, "mask"))
9c06b0b4
TJB
10716 {
10717 /* We've found a "mask" token, which means the user wants to
10718 create a hardware watchpoint that is going to have the mask
10719 facility. */
10720 struct value *mask_value, *mark;
37e4754d 10721
9c06b0b4
TJB
10722 if (use_mask)
10723 error(_("You can specify only one mask."));
37e4754d 10724
9c06b0b4 10725 use_mask = just_location = 1;
37e4754d 10726
9c06b0b4
TJB
10727 mark = value_mark ();
10728 mask_value = parse_to_comma_and_eval (&value_start);
10729 mask = value_as_address (mask_value);
10730 value_free_to_mark (mark);
10731 }
10732 else
10733 /* We didn't recognize what we found. We should stop here. */
10734 break;
37e4754d 10735
9c06b0b4
TJB
10736 /* Truncate the string and get rid of the "parameter value" pair before
10737 the arguments string is parsed by the parse_exp_1 function. */
bbc13ae3 10738 exp_end = tok;
9c06b0b4 10739 }
37e4754d 10740 }
bbc13ae3
KS
10741 else
10742 exp_end = arg;
37e4754d 10743
bbc13ae3
KS
10744 /* Parse the rest of the arguments. From here on out, everything
10745 is in terms of a newly allocated string instead of the original
10746 ARG. */
81b1e71c
TT
10747 std::string expression (arg, exp_end - arg);
10748 exp_start = arg = expression.c_str ();
699bd4cf
TT
10749 innermost_block_tracker tracker;
10750 expression_up exp = parse_exp_1 (&arg, 0, 0, 0, &tracker);
c906108c 10751 exp_end = arg;
fa8a61dc
TT
10752 /* Remove trailing whitespace from the expression before saving it.
10753 This makes the eventual display of the expression string a bit
10754 prettier. */
10755 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
10756 --exp_end;
10757
65d79d4b 10758 /* Checking if the expression is not constant. */
4d01a485 10759 if (watchpoint_exp_is_const (exp.get ()))
65d79d4b
SDJ
10760 {
10761 int len;
10762
10763 len = exp_end - exp_start;
10764 while (len > 0 && isspace (exp_start[len - 1]))
10765 len--;
10766 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
10767 }
10768
699bd4cf 10769 exp_valid_block = tracker.block ();
b926417a 10770 struct value *mark = value_mark ();
850645cf
TT
10771 struct value *val_as_value = nullptr;
10772 fetch_subexp_value (exp.get (), &pc, &val_as_value, &result, NULL,
10773 just_location);
06a64a0b 10774
850645cf 10775 if (val_as_value != NULL && just_location)
bb9d5f81 10776 {
850645cf
TT
10777 saved_bitpos = value_bitpos (val_as_value);
10778 saved_bitsize = value_bitsize (val_as_value);
bb9d5f81
PP
10779 }
10780
850645cf 10781 value_ref_ptr val;
06a64a0b
TT
10782 if (just_location)
10783 {
9c06b0b4
TJB
10784 int ret;
10785
06a64a0b 10786 exp_valid_block = NULL;
850645cf 10787 val = release_value (value_addr (result));
06a64a0b 10788 value_free_to_mark (mark);
9c06b0b4
TJB
10789
10790 if (use_mask)
10791 {
850645cf 10792 ret = target_masked_watch_num_registers (value_as_address (val.get ()),
9c06b0b4
TJB
10793 mask);
10794 if (ret == -1)
10795 error (_("This target does not support masked watchpoints."));
10796 else if (ret == -2)
10797 error (_("Invalid mask or memory region."));
10798 }
06a64a0b 10799 }
850645cf
TT
10800 else if (val_as_value != NULL)
10801 val = release_value (val_as_value);
c906108c 10802
f1735a53
TT
10803 tok = skip_spaces (arg);
10804 end_tok = skip_to_space (tok);
c906108c
SS
10805
10806 toklen = end_tok - tok;
10807 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
10808 {
10809 tok = cond_start = end_tok + 1;
699bd4cf
TT
10810 innermost_block_tracker if_tracker;
10811 parse_exp_1 (&tok, 0, 0, 0, &if_tracker);
60e1c644
PA
10812
10813 /* The watchpoint expression may not be local, but the condition
10814 may still be. E.g.: `watch global if local > 0'. */
699bd4cf 10815 cond_exp_valid_block = if_tracker.block ();
60e1c644 10816
c906108c
SS
10817 cond_end = tok;
10818 }
10819 if (*tok)
8a3fe4f8 10820 error (_("Junk at end of command."));
c906108c 10821
441d7c93
PA
10822 frame_info *wp_frame = block_innermost_frame (exp_valid_block);
10823
10824 /* Save this because create_internal_breakpoint below invalidates
10825 'wp_frame'. */
10826 frame_id watchpoint_frame = get_frame_id (wp_frame);
d983da9c
DJ
10827
10828 /* If the expression is "local", then set up a "watchpoint scope"
10829 breakpoint at the point where we've left the scope of the watchpoint
10830 expression. Create the scope breakpoint before the watchpoint, so
10831 that we will encounter it first in bpstat_stop_status. */
441d7c93 10832 if (exp_valid_block != NULL && wp_frame != NULL)
d983da9c 10833 {
441d7c93
PA
10834 frame_id caller_frame_id = frame_unwind_caller_id (wp_frame);
10835
10836 if (frame_id_p (caller_frame_id))
edb3359d 10837 {
441d7c93
PA
10838 gdbarch *caller_arch = frame_unwind_caller_arch (wp_frame);
10839 CORE_ADDR caller_pc = frame_unwind_caller_pc (wp_frame);
10840
edb3359d 10841 scope_breakpoint
441d7c93 10842 = create_internal_breakpoint (caller_arch, caller_pc,
06edf0c0
PA
10843 bp_watchpoint_scope,
10844 &momentary_breakpoint_ops);
d983da9c 10845
441d7c93
PA
10846 /* create_internal_breakpoint could invalidate WP_FRAME. */
10847 wp_frame = NULL;
10848
edb3359d 10849 scope_breakpoint->enable_state = bp_enabled;
d983da9c 10850
edb3359d
DJ
10851 /* Automatically delete the breakpoint when it hits. */
10852 scope_breakpoint->disposition = disp_del;
d983da9c 10853
edb3359d 10854 /* Only break in the proper frame (help with recursion). */
441d7c93 10855 scope_breakpoint->frame_id = caller_frame_id;
d983da9c 10856
edb3359d 10857 /* Set the address at which we will stop. */
441d7c93
PA
10858 scope_breakpoint->loc->gdbarch = caller_arch;
10859 scope_breakpoint->loc->requested_address = caller_pc;
edb3359d 10860 scope_breakpoint->loc->address
a6d9a66e
UW
10861 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
10862 scope_breakpoint->loc->requested_address,
edb3359d
DJ
10863 scope_breakpoint->type);
10864 }
d983da9c
DJ
10865 }
10866
e8369a73
AB
10867 /* Now set up the breakpoint. We create all watchpoints as hardware
10868 watchpoints here even if hardware watchpoints are turned off, a call
10869 to update_watchpoint later in this function will cause the type to
10870 drop back to bp_watchpoint (software watchpoint) if required. */
10871
10872 if (accessflag == hw_read)
10873 bp_type = bp_read_watchpoint;
10874 else if (accessflag == hw_access)
10875 bp_type = bp_access_watchpoint;
10876 else
10877 bp_type = bp_hardware_watchpoint;
3a5c3e22 10878
b270e6f9 10879 std::unique_ptr<watchpoint> w (new watchpoint ());
c1fc2657 10880
348d480f 10881 if (use_mask)
b270e6f9 10882 init_raw_breakpoint_without_location (w.get (), NULL, bp_type,
3a5c3e22 10883 &masked_watchpoint_breakpoint_ops);
348d480f 10884 else
b270e6f9 10885 init_raw_breakpoint_without_location (w.get (), NULL, bp_type,
3a5c3e22 10886 &watchpoint_breakpoint_ops);
c1fc2657
SM
10887 w->thread = thread;
10888 w->disposition = disp_donttouch;
10889 w->pspace = current_program_space;
b22e99fd 10890 w->exp = std::move (exp);
3a5c3e22
PA
10891 w->exp_valid_block = exp_valid_block;
10892 w->cond_exp_valid_block = cond_exp_valid_block;
06a64a0b
TT
10893 if (just_location)
10894 {
850645cf
TT
10895 struct type *t = value_type (val.get ());
10896 CORE_ADDR addr = value_as_address (val.get ());
06a64a0b 10897
43cc5389 10898 w->exp_string_reparse
f16a9f57 10899 = current_language->watch_location_expression (t, addr).release ();
06a64a0b 10900
3a5c3e22 10901 w->exp_string = xstrprintf ("-location %.*s",
d63d0675 10902 (int) (exp_end - exp_start), exp_start);
06a64a0b
TT
10903 }
10904 else
3a5c3e22 10905 w->exp_string = savestring (exp_start, exp_end - exp_start);
9c06b0b4
TJB
10906
10907 if (use_mask)
10908 {
3a5c3e22 10909 w->hw_wp_mask = mask;
9c06b0b4
TJB
10910 }
10911 else
10912 {
3a5c3e22 10913 w->val = val;
bb9d5f81
PP
10914 w->val_bitpos = saved_bitpos;
10915 w->val_bitsize = saved_bitsize;
4c1d86d9 10916 w->val_valid = true;
9c06b0b4 10917 }
77b06cd7 10918
c906108c 10919 if (cond_start)
c1fc2657 10920 w->cond_string = savestring (cond_start, cond_end - cond_start);
c906108c 10921 else
c1fc2657 10922 w->cond_string = 0;
c5aa993b 10923
441d7c93 10924 if (frame_id_p (watchpoint_frame))
f6bc2008 10925 {
441d7c93 10926 w->watchpoint_frame = watchpoint_frame;
3a5c3e22 10927 w->watchpoint_thread = inferior_ptid;
f6bc2008 10928 }
c906108c 10929 else
f6bc2008 10930 {
3a5c3e22
PA
10931 w->watchpoint_frame = null_frame_id;
10932 w->watchpoint_thread = null_ptid;
f6bc2008 10933 }
c906108c 10934
d983da9c 10935 if (scope_breakpoint != NULL)
c906108c 10936 {
d983da9c
DJ
10937 /* The scope breakpoint is related to the watchpoint. We will
10938 need to act on them together. */
c1fc2657 10939 w->related_breakpoint = scope_breakpoint;
b270e6f9 10940 scope_breakpoint->related_breakpoint = w.get ();
c906108c 10941 }
d983da9c 10942
06a64a0b
TT
10943 if (!just_location)
10944 value_free_to_mark (mark);
2d134ed3 10945
b270e6f9
TT
10946 /* Finally update the new watchpoint. This creates the locations
10947 that should be inserted. */
10948 update_watchpoint (w.get (), 1);
a9634178 10949
b270e6f9 10950 install_breakpoint (internal, std::move (w), 1);
c906108c
SS
10951}
10952
e09342b5 10953/* Return count of debug registers needed to watch the given expression.
e09342b5 10954 If the watchpoint cannot be handled in hardware return zero. */
c906108c 10955
c906108c 10956static int
a6535de1 10957can_use_hardware_watchpoint (const std::vector<value_ref_ptr> &vals)
c906108c
SS
10958{
10959 int found_memory_cnt = 0;
10960
10961 /* Did the user specifically forbid us to use hardware watchpoints? */
c5aa993b 10962 if (!can_use_hw_watchpoints)
c906108c 10963 return 0;
c5aa993b 10964
a6535de1
TT
10965 gdb_assert (!vals.empty ());
10966 struct value *head = vals[0].get ();
10967
5c44784c
JM
10968 /* Make sure that the value of the expression depends only upon
10969 memory contents, and values computed from them within GDB. If we
10970 find any register references or function calls, we can't use a
10971 hardware watchpoint.
10972
10973 The idea here is that evaluating an expression generates a series
10974 of values, one holding the value of every subexpression. (The
10975 expression a*b+c has five subexpressions: a, b, a*b, c, and
10976 a*b+c.) GDB's values hold almost enough information to establish
10977 the criteria given above --- they identify memory lvalues,
10978 register lvalues, computed values, etcetera. So we can evaluate
10979 the expression, and then scan the chain of values that leaves
10980 behind to decide whether we can detect any possible change to the
10981 expression's final value using only hardware watchpoints.
10982
10983 However, I don't think that the values returned by inferior
10984 function calls are special in any way. So this function may not
10985 notice that an expression involving an inferior function call
10986 can't be watched with hardware watchpoints. FIXME. */
a6535de1 10987 for (const value_ref_ptr &iter : vals)
c906108c 10988 {
a6535de1
TT
10989 struct value *v = iter.get ();
10990
5c44784c 10991 if (VALUE_LVAL (v) == lval_memory)
c906108c 10992 {
8464be76
DJ
10993 if (v != head && value_lazy (v))
10994 /* A lazy memory lvalue in the chain is one that GDB never
10995 needed to fetch; we either just used its address (e.g.,
10996 `a' in `a.b') or we never needed it at all (e.g., `a'
10997 in `a,b'). This doesn't apply to HEAD; if that is
10998 lazy then it was not readable, but watch it anyway. */
5c44784c 10999 ;
53a5351d 11000 else
5c44784c
JM
11001 {
11002 /* Ahh, memory we actually used! Check if we can cover
11003 it with hardware watchpoints. */
df407dfe 11004 struct type *vtype = check_typedef (value_type (v));
2e70b7b9
MS
11005
11006 /* We only watch structs and arrays if user asked for it
11007 explicitly, never if they just happen to appear in a
11008 middle of some value chain. */
11009 if (v == head
78134374
SM
11010 || (vtype->code () != TYPE_CODE_STRUCT
11011 && vtype->code () != TYPE_CODE_ARRAY))
2e70b7b9 11012 {
42ae5230 11013 CORE_ADDR vaddr = value_address (v);
e09342b5
TJB
11014 int len;
11015 int num_regs;
11016
a9634178 11017 len = (target_exact_watchpoints
e09342b5
TJB
11018 && is_scalar_type_recursive (vtype))?
11019 1 : TYPE_LENGTH (value_type (v));
2e70b7b9 11020
e09342b5
TJB
11021 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
11022 if (!num_regs)
2e70b7b9
MS
11023 return 0;
11024 else
e09342b5 11025 found_memory_cnt += num_regs;
2e70b7b9 11026 }
5c44784c 11027 }
c5aa993b 11028 }
5086187c
AC
11029 else if (VALUE_LVAL (v) != not_lval
11030 && deprecated_value_modifiable (v) == 0)
38b6c3b3 11031 return 0; /* These are values from the history (e.g., $1). */
5086187c 11032 else if (VALUE_LVAL (v) == lval_register)
38b6c3b3 11033 return 0; /* Cannot watch a register with a HW watchpoint. */
c906108c
SS
11034 }
11035
11036 /* The expression itself looks suitable for using a hardware
11037 watchpoint, but give the target machine a chance to reject it. */
11038 return found_memory_cnt;
11039}
11040
8b93c638 11041void
f2fc3015 11042watch_command_wrapper (const char *arg, int from_tty, int internal)
8b93c638 11043{
84f4c1fe 11044 watch_command_1 (arg, hw_write, from_tty, 0, internal);
06a64a0b
TT
11045}
11046
06a64a0b
TT
11047/* A helper function that looks for the "-location" argument and then
11048 calls watch_command_1. */
11049
11050static void
0b39b52e 11051watch_maybe_just_location (const char *arg, int accessflag, int from_tty)
06a64a0b
TT
11052{
11053 int just_location = 0;
11054
11055 if (arg
11056 && (check_for_argument (&arg, "-location", sizeof ("-location") - 1)
11057 || check_for_argument (&arg, "-l", sizeof ("-l") - 1)))
cbba3ecd 11058 just_location = 1;
06a64a0b 11059
84f4c1fe 11060 watch_command_1 (arg, accessflag, from_tty, just_location, 0);
8b93c638 11061}
8926118c 11062
c5aa993b 11063static void
0b39b52e 11064watch_command (const char *arg, int from_tty)
c906108c 11065{
06a64a0b 11066 watch_maybe_just_location (arg, hw_write, from_tty);
c906108c
SS
11067}
11068
8b93c638 11069void
f2fc3015 11070rwatch_command_wrapper (const char *arg, int from_tty, int internal)
8b93c638 11071{
84f4c1fe 11072 watch_command_1 (arg, hw_read, from_tty, 0, internal);
8b93c638 11073}
8926118c 11074
c5aa993b 11075static void
0b39b52e 11076rwatch_command (const char *arg, int from_tty)
c906108c 11077{
06a64a0b 11078 watch_maybe_just_location (arg, hw_read, from_tty);
c906108c
SS
11079}
11080
8b93c638 11081void
f2fc3015 11082awatch_command_wrapper (const char *arg, int from_tty, int internal)
8b93c638 11083{
84f4c1fe 11084 watch_command_1 (arg, hw_access, from_tty, 0, internal);
8b93c638 11085}
8926118c 11086
c5aa993b 11087static void
0b39b52e 11088awatch_command (const char *arg, int from_tty)
c906108c 11089{
06a64a0b 11090 watch_maybe_just_location (arg, hw_access, from_tty);
c906108c 11091}
c906108c 11092\f
c5aa993b 11093
cfc31633
PA
11094/* Data for the FSM that manages the until(location)/advance commands
11095 in infcmd.c. Here because it uses the mechanisms of
11096 breakpoints. */
c906108c 11097
46e3ed7f 11098struct until_break_fsm : public thread_fsm
bfec99b2 11099{
46e3ed7f 11100 /* The thread that was current when the command was executed. */
cfc31633
PA
11101 int thread;
11102
df631783
PA
11103 /* The breakpoint set at the return address in the caller frame,
11104 plus breakpoints at all the destination locations. */
11105 std::vector<breakpoint_up> breakpoints;
cfc31633 11106
46e3ed7f 11107 until_break_fsm (struct interp *cmd_interp, int thread,
df631783 11108 std::vector<breakpoint_up> &&breakpoints)
46e3ed7f
TT
11109 : thread_fsm (cmd_interp),
11110 thread (thread),
df631783 11111 breakpoints (std::move (breakpoints))
46e3ed7f
TT
11112 {
11113 }
cfc31633 11114
46e3ed7f
TT
11115 void clean_up (struct thread_info *thread) override;
11116 bool should_stop (struct thread_info *thread) override;
11117 enum async_reply_reason do_async_reply_reason () override;
cfc31633
PA
11118};
11119
cfc31633
PA
11120/* Implementation of the 'should_stop' FSM method for the
11121 until(location)/advance commands. */
11122
46e3ed7f
TT
11123bool
11124until_break_fsm::should_stop (struct thread_info *tp)
cfc31633 11125{
df631783
PA
11126 for (const breakpoint_up &bp : breakpoints)
11127 if (bpstat_find_breakpoint (tp->control.stop_bpstat,
11128 bp.get ()) != NULL)
11129 {
11130 set_finished ();
11131 break;
11132 }
cfc31633 11133
46e3ed7f 11134 return true;
cfc31633
PA
11135}
11136
11137/* Implementation of the 'clean_up' FSM method for the
11138 until(location)/advance commands. */
11139
46e3ed7f
TT
11140void
11141until_break_fsm::clean_up (struct thread_info *)
43ff13b4 11142{
cfc31633 11143 /* Clean up our temporary breakpoints. */
df631783 11144 breakpoints.clear ();
46e3ed7f 11145 delete_longjmp_breakpoint (thread);
cfc31633
PA
11146}
11147
11148/* Implementation of the 'async_reply_reason' FSM method for the
11149 until(location)/advance commands. */
11150
46e3ed7f
TT
11151enum async_reply_reason
11152until_break_fsm::do_async_reply_reason ()
cfc31633
PA
11153{
11154 return EXEC_ASYNC_LOCATION_REACHED;
43ff13b4
JM
11155}
11156
c906108c 11157void
f2fc3015 11158until_break_command (const char *arg, int from_tty, int anywhere)
c906108c 11159{
8556afb4
PA
11160 struct frame_info *frame;
11161 struct gdbarch *frame_gdbarch;
11162 struct frame_id stack_frame_id;
11163 struct frame_id caller_frame_id;
186c406b
TT
11164 int thread;
11165 struct thread_info *tp;
c906108c 11166
70509625 11167 clear_proceed_status (0);
c906108c
SS
11168
11169 /* Set a breakpoint where the user wants it and at return from
4a64f543 11170 this function. */
c5aa993b 11171
ffc2605c 11172 event_location_up location = string_to_event_location (&arg, current_language);
f00aae0f 11173
6c5b2ebe
PA
11174 std::vector<symtab_and_line> sals
11175 = (last_displayed_sal_is_valid ()
11176 ? decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
11177 get_last_displayed_symtab (),
11178 get_last_displayed_line ())
11179 : decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE,
cafb3438 11180 NULL, NULL, 0));
c5aa993b 11181
df631783 11182 if (sals.empty ())
8a3fe4f8 11183 error (_("Couldn't get information on specified line."));
c5aa993b 11184
c906108c 11185 if (*arg)
8a3fe4f8 11186 error (_("Junk at end of arguments."));
c5aa993b 11187
186c406b 11188 tp = inferior_thread ();
5d5658a1 11189 thread = tp->global_num;
186c406b 11190
8556afb4
PA
11191 /* Note linespec handling above invalidates the frame chain.
11192 Installing a breakpoint also invalidates the frame chain (as it
11193 may need to switch threads), so do any frame handling before
11194 that. */
11195
11196 frame = get_selected_frame (NULL);
11197 frame_gdbarch = get_frame_arch (frame);
11198 stack_frame_id = get_stack_frame_id (frame);
11199 caller_frame_id = frame_unwind_caller_id (frame);
883bc8d1 11200
ae66c1fc
EZ
11201 /* Keep within the current frame, or in frames called by the current
11202 one. */
edb3359d 11203
df631783 11204 std::vector<breakpoint_up> breakpoints;
5419bdae
TT
11205
11206 gdb::optional<delete_longjmp_breakpoint_cleanup> lj_deleter;
11207
883bc8d1 11208 if (frame_id_p (caller_frame_id))
c906108c 11209 {
883bc8d1 11210 struct symtab_and_line sal2;
cfc31633 11211 struct gdbarch *caller_gdbarch;
883bc8d1
PA
11212
11213 sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
11214 sal2.pc = frame_unwind_caller_pc (frame);
cfc31633 11215 caller_gdbarch = frame_unwind_caller_arch (frame);
df631783
PA
11216
11217 breakpoint_up caller_breakpoint
11218 = set_momentary_breakpoint (caller_gdbarch, sal2,
11219 caller_frame_id, bp_until);
11220 breakpoints.emplace_back (std::move (caller_breakpoint));
186c406b 11221
883bc8d1 11222 set_longjmp_breakpoint (tp, caller_frame_id);
5419bdae 11223 lj_deleter.emplace (thread);
c906108c 11224 }
c5aa993b 11225
c70a6932
JK
11226 /* set_momentary_breakpoint could invalidate FRAME. */
11227 frame = NULL;
11228
df631783
PA
11229 /* If the user told us to continue until a specified location, we
11230 don't specify a frame at which we need to stop. Otherwise,
11231 specify the selected frame, because we want to stop only at the
11232 very same frame. */
11233 frame_id stop_frame_id = anywhere ? null_frame_id : stack_frame_id;
11234
11235 for (symtab_and_line &sal : sals)
11236 {
11237 resolve_sal_pc (&sal);
11238
11239 breakpoint_up location_breakpoint
11240 = set_momentary_breakpoint (frame_gdbarch, sal,
11241 stop_frame_id, bp_until);
11242 breakpoints.emplace_back (std::move (location_breakpoint));
11243 }
883bc8d1 11244
46e3ed7f 11245 tp->thread_fsm = new until_break_fsm (command_interp (), tp->global_num,
df631783 11246 std::move (breakpoints));
f107f563 11247
5419bdae
TT
11248 if (lj_deleter)
11249 lj_deleter->release ();
f107f563 11250
cfc31633 11251 proceed (-1, GDB_SIGNAL_DEFAULT);
c906108c 11252}
ae66c1fc 11253
c906108c
SS
11254/* This function attempts to parse an optional "if <cond>" clause
11255 from the arg string. If one is not found, it returns NULL.
c5aa993b 11256
c906108c
SS
11257 Else, it returns a pointer to the condition string. (It does not
11258 attempt to evaluate the string against a particular block.) And,
11259 it updates arg to point to the first character following the parsed
4a64f543 11260 if clause in the arg string. */
53a5351d 11261
63160a43
PA
11262const char *
11263ep_parse_optional_if_clause (const char **arg)
c906108c 11264{
63160a43 11265 const char *cond_string;
c5aa993b
JM
11266
11267 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
c906108c 11268 return NULL;
c5aa993b 11269
4a64f543 11270 /* Skip the "if" keyword. */
c906108c 11271 (*arg) += 2;
c5aa993b 11272
c906108c 11273 /* Skip any extra leading whitespace, and record the start of the
4a64f543 11274 condition string. */
f1735a53 11275 *arg = skip_spaces (*arg);
c906108c 11276 cond_string = *arg;
c5aa993b 11277
4a64f543
MS
11278 /* Assume that the condition occupies the remainder of the arg
11279 string. */
c906108c 11280 (*arg) += strlen (cond_string);
c5aa993b 11281
c906108c
SS
11282 return cond_string;
11283}
c5aa993b 11284
c906108c
SS
11285/* Commands to deal with catching events, such as signals, exceptions,
11286 process start/exit, etc. */
c5aa993b
JM
11287
11288typedef enum
11289{
44feb3ce
TT
11290 catch_fork_temporary, catch_vfork_temporary,
11291 catch_fork_permanent, catch_vfork_permanent
c5aa993b
JM
11292}
11293catch_fork_kind;
11294
c906108c 11295static void
eb4c3f4a 11296catch_fork_command_1 (const char *arg, int from_tty,
cc59ec59 11297 struct cmd_list_element *command)
c906108c 11298{
a6d9a66e 11299 struct gdbarch *gdbarch = get_current_arch ();
63160a43 11300 const char *cond_string = NULL;
44feb3ce 11301 catch_fork_kind fork_kind;
44feb3ce
TT
11302
11303 fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
b650a282
SM
11304 bool temp = (fork_kind == catch_fork_temporary
11305 || fork_kind == catch_vfork_temporary);
c5aa993b 11306
44feb3ce
TT
11307 if (!arg)
11308 arg = "";
f1735a53 11309 arg = skip_spaces (arg);
c5aa993b 11310
c906108c 11311 /* The allowed syntax is:
c5aa993b
JM
11312 catch [v]fork
11313 catch [v]fork if <cond>
11314
4a64f543 11315 First, check if there's an if clause. */
c906108c 11316 cond_string = ep_parse_optional_if_clause (&arg);
c5aa993b 11317
c906108c 11318 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 11319 error (_("Junk at end of arguments."));
c5aa993b 11320
c906108c 11321 /* If this target supports it, create a fork or vfork catchpoint
4a64f543 11322 and enable reporting of such events. */
c5aa993b
JM
11323 switch (fork_kind)
11324 {
44feb3ce
TT
11325 case catch_fork_temporary:
11326 case catch_fork_permanent:
b650a282 11327 create_fork_vfork_event_catchpoint (gdbarch, temp, cond_string,
ce78b96d 11328 &catch_fork_breakpoint_ops);
c906108c 11329 break;
44feb3ce
TT
11330 case catch_vfork_temporary:
11331 case catch_vfork_permanent:
b650a282 11332 create_fork_vfork_event_catchpoint (gdbarch, temp, cond_string,
ce78b96d 11333 &catch_vfork_breakpoint_ops);
c906108c 11334 break;
c5aa993b 11335 default:
8a3fe4f8 11336 error (_("unsupported or unknown fork kind; cannot catch it"));
c906108c 11337 break;
c5aa993b 11338 }
c906108c
SS
11339}
11340
11341static void
eb4c3f4a 11342catch_exec_command_1 (const char *arg, int from_tty,
cc59ec59 11343 struct cmd_list_element *command)
c906108c 11344{
a6d9a66e 11345 struct gdbarch *gdbarch = get_current_arch ();
63160a43 11346 const char *cond_string = NULL;
b650a282 11347 bool temp = get_cmd_context (command) == CATCH_TEMPORARY;
44feb3ce
TT
11348
11349 if (!arg)
11350 arg = "";
f1735a53 11351 arg = skip_spaces (arg);
c906108c
SS
11352
11353 /* The allowed syntax is:
c5aa993b
JM
11354 catch exec
11355 catch exec if <cond>
c906108c 11356
4a64f543 11357 First, check if there's an if clause. */
c906108c
SS
11358 cond_string = ep_parse_optional_if_clause (&arg);
11359
11360 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 11361 error (_("Junk at end of arguments."));
c906108c 11362
b270e6f9 11363 std::unique_ptr<exec_catchpoint> c (new exec_catchpoint ());
b650a282 11364 init_catchpoint (c.get (), gdbarch, temp, cond_string,
b4d90040
PA
11365 &catch_exec_breakpoint_ops);
11366 c->exec_pathname = NULL;
11367
b270e6f9 11368 install_breakpoint (0, std::move (c), 1);
c906108c 11369}
c5aa993b 11370
9ac4176b 11371void
28010a5d
PA
11372init_ada_exception_breakpoint (struct breakpoint *b,
11373 struct gdbarch *gdbarch,
11374 struct symtab_and_line sal,
f2fc3015 11375 const char *addr_string,
c0a91b2b 11376 const struct breakpoint_ops *ops,
28010a5d 11377 int tempflag,
349774ef 11378 int enabled,
28010a5d 11379 int from_tty)
f7f9143b 11380{
f7f9143b
JB
11381 if (from_tty)
11382 {
5af949e3
UW
11383 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
11384 if (!loc_gdbarch)
11385 loc_gdbarch = gdbarch;
11386
6c95b8df
PA
11387 describe_other_breakpoints (loc_gdbarch,
11388 sal.pspace, sal.pc, sal.section, -1);
f7f9143b
JB
11389 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
11390 version for exception catchpoints, because two catchpoints
11391 used for different exception names will use the same address.
11392 In this case, a "breakpoint ... also set at..." warning is
4a64f543 11393 unproductive. Besides, the warning phrasing is also a bit
e5dd4106 11394 inappropriate, we should use the word catchpoint, and tell
f7f9143b
JB
11395 the user what type of catchpoint it is. The above is good
11396 enough for now, though. */
11397 }
11398
f06f1252 11399 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
f7f9143b 11400
349774ef 11401 b->enable_state = enabled ? bp_enabled : bp_disabled;
f7f9143b 11402 b->disposition = tempflag ? disp_del : disp_donttouch;
d28cd78a
TT
11403 b->location = string_to_event_location (&addr_string,
11404 language_def (language_ada));
f7f9143b 11405 b->language = language_ada;
f7f9143b
JB
11406}
11407
c906108c
SS
11408\f
11409
81b1e71c 11410/* Compare two breakpoints and return a strcmp-like result. */
8a2c437b
TT
11411
11412static int
81b1e71c 11413compare_breakpoints (const breakpoint *a, const breakpoint *b)
8a2c437b 11414{
81b1e71c
TT
11415 uintptr_t ua = (uintptr_t) a;
11416 uintptr_t ub = (uintptr_t) b;
8a2c437b 11417
81b1e71c 11418 if (a->number < b->number)
8a2c437b 11419 return -1;
81b1e71c 11420 else if (a->number > b->number)
8a2c437b
TT
11421 return 1;
11422
11423 /* Now sort by address, in case we see, e..g, two breakpoints with
11424 the number 0. */
11425 if (ua < ub)
11426 return -1;
94b0e70d 11427 return ua > ub ? 1 : 0;
8a2c437b
TT
11428}
11429
80f8a6eb 11430/* Delete breakpoints by address or line. */
c906108c
SS
11431
11432static void
0b39b52e 11433clear_command (const char *arg, int from_tty)
c906108c 11434{
81b1e71c 11435 struct breakpoint *b;
c906108c 11436 int default_match;
c906108c 11437
6c5b2ebe
PA
11438 std::vector<symtab_and_line> decoded_sals;
11439 symtab_and_line last_sal;
11440 gdb::array_view<symtab_and_line> sals;
c906108c
SS
11441 if (arg)
11442 {
6c5b2ebe
PA
11443 decoded_sals
11444 = decode_line_with_current_source (arg,
11445 (DECODE_LINE_FUNFIRSTLINE
11446 | DECODE_LINE_LIST_MODE));
c906108c 11447 default_match = 0;
6c5b2ebe 11448 sals = decoded_sals;
c906108c
SS
11449 }
11450 else
11451 {
1bfeeb0f
JL
11452 /* Set sal's line, symtab, pc, and pspace to the values
11453 corresponding to the last call to print_frame_info. If the
11454 codepoint is not valid, this will set all the fields to 0. */
51abb421 11455 last_sal = get_last_displayed_sal ();
6c5b2ebe 11456 if (last_sal.symtab == 0)
8a3fe4f8 11457 error (_("No source file specified."));
c906108c 11458
c906108c 11459 default_match = 1;
6c5b2ebe 11460 sals = last_sal;
c906108c
SS
11461 }
11462
4a64f543
MS
11463 /* We don't call resolve_sal_pc here. That's not as bad as it
11464 seems, because all existing breakpoints typically have both
11465 file/line and pc set. So, if clear is given file/line, we can
11466 match this to existing breakpoint without obtaining pc at all.
ed0616c6
VP
11467
11468 We only support clearing given the address explicitly
11469 present in breakpoint table. Say, we've set breakpoint
4a64f543 11470 at file:line. There were several PC values for that file:line,
ed0616c6 11471 due to optimization, all in one block.
4a64f543
MS
11472
11473 We've picked one PC value. If "clear" is issued with another
ed0616c6
VP
11474 PC corresponding to the same file:line, the breakpoint won't
11475 be cleared. We probably can still clear the breakpoint, but
11476 since the other PC value is never presented to user, user
11477 can only find it by guessing, and it does not seem important
11478 to support that. */
11479
4a64f543
MS
11480 /* For each line spec given, delete bps which correspond to it. Do
11481 it in two passes, solely to preserve the current behavior that
11482 from_tty is forced true if we delete more than one
11483 breakpoint. */
c906108c 11484
81b1e71c 11485 std::vector<struct breakpoint *> found;
6c5b2ebe 11486 for (const auto &sal : sals)
c906108c 11487 {
05cba821
JK
11488 const char *sal_fullname;
11489
c906108c 11490 /* If exact pc given, clear bpts at that pc.
c5aa993b
JM
11491 If line given (pc == 0), clear all bpts on specified line.
11492 If defaulting, clear all bpts on default line
c906108c 11493 or at default pc.
c5aa993b
JM
11494
11495 defaulting sal.pc != 0 tests to do
11496
11497 0 1 pc
11498 1 1 pc _and_ line
11499 0 0 line
11500 1 0 <can't happen> */
c906108c 11501
05cba821
JK
11502 sal_fullname = (sal.symtab == NULL
11503 ? NULL : symtab_to_fullname (sal.symtab));
c906108c 11504
4a64f543 11505 /* Find all matching breakpoints and add them to 'found'. */
d6e956e5 11506 ALL_BREAKPOINTS (b)
c5aa993b 11507 {
0d381245 11508 int match = 0;
4a64f543 11509 /* Are we going to delete b? */
cc60f2e3 11510 if (b->type != bp_none && !is_watchpoint (b))
0d381245
VP
11511 {
11512 struct bp_location *loc = b->loc;
11513 for (; loc; loc = loc->next)
11514 {
f8eba3c6
TT
11515 /* If the user specified file:line, don't allow a PC
11516 match. This matches historical gdb behavior. */
11517 int pc_match = (!sal.explicit_line
11518 && sal.pc
11519 && (loc->pspace == sal.pspace)
11520 && (loc->address == sal.pc)
11521 && (!section_is_overlay (loc->section)
11522 || loc->section == sal.section));
4aac40c8
TT
11523 int line_match = 0;
11524
11525 if ((default_match || sal.explicit_line)
2f202fde 11526 && loc->symtab != NULL
05cba821 11527 && sal_fullname != NULL
4aac40c8 11528 && sal.pspace == loc->pspace
05cba821
JK
11529 && loc->line_number == sal.line
11530 && filename_cmp (symtab_to_fullname (loc->symtab),
11531 sal_fullname) == 0)
11532 line_match = 1;
4aac40c8 11533
0d381245
VP
11534 if (pc_match || line_match)
11535 {
11536 match = 1;
11537 break;
11538 }
11539 }
11540 }
11541
11542 if (match)
81b1e71c 11543 found.push_back (b);
c906108c 11544 }
80f8a6eb 11545 }
8a2c437b 11546
80f8a6eb 11547 /* Now go thru the 'found' chain and delete them. */
81b1e71c 11548 if (found.empty ())
80f8a6eb
MS
11549 {
11550 if (arg)
8a3fe4f8 11551 error (_("No breakpoint at %s."), arg);
80f8a6eb 11552 else
8a3fe4f8 11553 error (_("No breakpoint at this line."));
80f8a6eb 11554 }
c906108c 11555
8a2c437b 11556 /* Remove duplicates from the vec. */
81b1e71c 11557 std::sort (found.begin (), found.end (),
b926417a 11558 [] (const breakpoint *bp_a, const breakpoint *bp_b)
81b1e71c 11559 {
b926417a 11560 return compare_breakpoints (bp_a, bp_b) < 0;
81b1e71c
TT
11561 });
11562 found.erase (std::unique (found.begin (), found.end (),
b926417a 11563 [] (const breakpoint *bp_a, const breakpoint *bp_b)
81b1e71c 11564 {
b926417a 11565 return compare_breakpoints (bp_a, bp_b) == 0;
81b1e71c
TT
11566 }),
11567 found.end ());
8a2c437b 11568
81b1e71c 11569 if (found.size () > 1)
4a64f543 11570 from_tty = 1; /* Always report if deleted more than one. */
80f8a6eb 11571 if (from_tty)
a3f17187 11572 {
81b1e71c 11573 if (found.size () == 1)
a3f17187
AC
11574 printf_unfiltered (_("Deleted breakpoint "));
11575 else
11576 printf_unfiltered (_("Deleted breakpoints "));
11577 }
d6e956e5 11578
81b1e71c 11579 for (breakpoint *iter : found)
80f8a6eb 11580 {
c5aa993b 11581 if (from_tty)
81b1e71c
TT
11582 printf_unfiltered ("%d ", iter->number);
11583 delete_breakpoint (iter);
c906108c 11584 }
80f8a6eb
MS
11585 if (from_tty)
11586 putchar_unfiltered ('\n');
c906108c
SS
11587}
11588\f
11589/* Delete breakpoint in BS if they are `delete' breakpoints and
11590 all breakpoints that are marked for deletion, whether hit or not.
11591 This is called after any breakpoint is hit, or after errors. */
11592
11593void
fba45db2 11594breakpoint_auto_delete (bpstat bs)
c906108c 11595{
35df4500 11596 struct breakpoint *b, *b_tmp;
c906108c
SS
11597
11598 for (; bs; bs = bs->next)
f431efe5
PA
11599 if (bs->breakpoint_at
11600 && bs->breakpoint_at->disposition == disp_del
c906108c 11601 && bs->stop)
f431efe5 11602 delete_breakpoint (bs->breakpoint_at);
c906108c 11603
35df4500 11604 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 11605 {
b5de0fa7 11606 if (b->disposition == disp_del_at_next_stop)
c5aa993b
JM
11607 delete_breakpoint (b);
11608 }
c906108c
SS
11609}
11610
4a64f543 11611/* A comparison function for bp_location AP and BP being interfaced to
39ef2f62 11612 std::sort. Sort elements primarily by their ADDRESS (no matter what
cb1e4e32
PA
11613 bl_address_is_meaningful says), secondarily by ordering first
11614 permanent elements and terciarily just ensuring the array is sorted
39ef2f62 11615 stable way despite std::sort being an unstable algorithm. */
876fa593
JK
11616
11617static int
39ef2f62 11618bp_location_is_less_than (const bp_location *a, const bp_location *b)
876fa593 11619{
876fa593 11620 if (a->address != b->address)
39ef2f62 11621 return a->address < b->address;
876fa593 11622
dea2aa5f
LM
11623 /* Sort locations at the same address by their pspace number, keeping
11624 locations of the same inferior (in a multi-inferior environment)
11625 grouped. */
11626
11627 if (a->pspace->num != b->pspace->num)
39ef2f62 11628 return a->pspace->num < b->pspace->num;
dea2aa5f 11629
876fa593 11630 /* Sort permanent breakpoints first. */
1a853c52 11631 if (a->permanent != b->permanent)
39ef2f62 11632 return a->permanent > b->permanent;
876fa593 11633
7f32a4d5
PA
11634 /* Sort by type in order to make duplicate determination easier.
11635 See update_global_location_list. This is kept in sync with
11636 breakpoint_locations_match. */
11637 if (a->loc_type < b->loc_type)
11638 return true;
11639
11640 /* Likewise, for range-breakpoints, sort by length. */
11641 if (a->loc_type == bp_loc_hardware_breakpoint
11642 && b->loc_type == bp_loc_hardware_breakpoint
11643 && a->length < b->length)
11644 return true;
11645
c56a97f9
JK
11646 /* Make the internal GDB representation stable across GDB runs
11647 where A and B memory inside GDB can differ. Breakpoint locations of
11648 the same type at the same address can be sorted in arbitrary order. */
876fa593
JK
11649
11650 if (a->owner->number != b->owner->number)
39ef2f62 11651 return a->owner->number < b->owner->number;
876fa593 11652
39ef2f62 11653 return a < b;
876fa593
JK
11654}
11655
f5336ca5
PA
11656/* Set bp_locations_placed_address_before_address_max and
11657 bp_locations_shadow_len_after_address_max according to the current
11658 content of the bp_locations array. */
f7545552
TT
11659
11660static void
f5336ca5 11661bp_locations_target_extensions_update (void)
f7545552 11662{
876fa593
JK
11663 struct bp_location *bl, **blp_tmp;
11664
f5336ca5
PA
11665 bp_locations_placed_address_before_address_max = 0;
11666 bp_locations_shadow_len_after_address_max = 0;
876fa593
JK
11667
11668 ALL_BP_LOCATIONS (bl, blp_tmp)
11669 {
11670 CORE_ADDR start, end, addr;
11671
11672 if (!bp_location_has_shadow (bl))
11673 continue;
11674
11675 start = bl->target_info.placed_address;
11676 end = start + bl->target_info.shadow_len;
11677
11678 gdb_assert (bl->address >= start);
11679 addr = bl->address - start;
f5336ca5
PA
11680 if (addr > bp_locations_placed_address_before_address_max)
11681 bp_locations_placed_address_before_address_max = addr;
876fa593
JK
11682
11683 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
11684
11685 gdb_assert (bl->address < end);
11686 addr = end - bl->address;
f5336ca5
PA
11687 if (addr > bp_locations_shadow_len_after_address_max)
11688 bp_locations_shadow_len_after_address_max = addr;
876fa593 11689 }
f7545552
TT
11690}
11691
1e4d1764
YQ
11692/* Download tracepoint locations if they haven't been. */
11693
11694static void
11695download_tracepoint_locations (void)
11696{
7ed2c994 11697 struct breakpoint *b;
dd2e65cc 11698 enum tribool can_download_tracepoint = TRIBOOL_UNKNOWN;
1e4d1764 11699
5ed8105e 11700 scoped_restore_current_pspace_and_thread restore_pspace_thread;
1e4d1764 11701
7ed2c994 11702 ALL_TRACEPOINTS (b)
1e4d1764 11703 {
7ed2c994 11704 struct bp_location *bl;
1e4d1764 11705 struct tracepoint *t;
f2a8bc8a 11706 int bp_location_downloaded = 0;
1e4d1764 11707
7ed2c994 11708 if ((b->type == bp_fast_tracepoint
1e4d1764
YQ
11709 ? !may_insert_fast_tracepoints
11710 : !may_insert_tracepoints))
11711 continue;
11712
dd2e65cc
YQ
11713 if (can_download_tracepoint == TRIBOOL_UNKNOWN)
11714 {
11715 if (target_can_download_tracepoint ())
11716 can_download_tracepoint = TRIBOOL_TRUE;
11717 else
11718 can_download_tracepoint = TRIBOOL_FALSE;
11719 }
11720
11721 if (can_download_tracepoint == TRIBOOL_FALSE)
11722 break;
11723
7ed2c994
YQ
11724 for (bl = b->loc; bl; bl = bl->next)
11725 {
11726 /* In tracepoint, locations are _never_ duplicated, so
11727 should_be_inserted is equivalent to
11728 unduplicated_should_be_inserted. */
11729 if (!should_be_inserted (bl) || bl->inserted)
11730 continue;
1e4d1764 11731
7ed2c994 11732 switch_to_program_space_and_thread (bl->pspace);
1e4d1764 11733
7ed2c994 11734 target_download_tracepoint (bl);
1e4d1764 11735
7ed2c994 11736 bl->inserted = 1;
f2a8bc8a 11737 bp_location_downloaded = 1;
7ed2c994
YQ
11738 }
11739 t = (struct tracepoint *) b;
11740 t->number_on_target = b->number;
f2a8bc8a 11741 if (bp_location_downloaded)
76727919 11742 gdb::observers::breakpoint_modified.notify (b);
1e4d1764 11743 }
1e4d1764
YQ
11744}
11745
934709f0
PW
11746/* Swap the insertion/duplication state between two locations. */
11747
11748static void
11749swap_insertion (struct bp_location *left, struct bp_location *right)
11750{
11751 const int left_inserted = left->inserted;
11752 const int left_duplicate = left->duplicate;
b775012e 11753 const int left_needs_update = left->needs_update;
934709f0
PW
11754 const struct bp_target_info left_target_info = left->target_info;
11755
1e4d1764
YQ
11756 /* Locations of tracepoints can never be duplicated. */
11757 if (is_tracepoint (left->owner))
11758 gdb_assert (!left->duplicate);
11759 if (is_tracepoint (right->owner))
11760 gdb_assert (!right->duplicate);
11761
934709f0
PW
11762 left->inserted = right->inserted;
11763 left->duplicate = right->duplicate;
b775012e 11764 left->needs_update = right->needs_update;
934709f0
PW
11765 left->target_info = right->target_info;
11766 right->inserted = left_inserted;
11767 right->duplicate = left_duplicate;
b775012e 11768 right->needs_update = left_needs_update;
934709f0
PW
11769 right->target_info = left_target_info;
11770}
11771
b775012e
LM
11772/* Force the re-insertion of the locations at ADDRESS. This is called
11773 once a new/deleted/modified duplicate location is found and we are evaluating
11774 conditions on the target's side. Such conditions need to be updated on
11775 the target. */
11776
11777static void
11778force_breakpoint_reinsertion (struct bp_location *bl)
11779{
11780 struct bp_location **locp = NULL, **loc2p;
11781 struct bp_location *loc;
11782 CORE_ADDR address = 0;
11783 int pspace_num;
11784
11785 address = bl->address;
11786 pspace_num = bl->pspace->num;
11787
11788 /* This is only meaningful if the target is
11789 evaluating conditions and if the user has
11790 opted for condition evaluation on the target's
11791 side. */
11792 if (gdb_evaluates_breakpoint_condition_p ()
11793 || !target_supports_evaluation_of_breakpoint_conditions ())
11794 return;
11795
11796 /* Flag all breakpoint locations with this address and
11797 the same program space as the location
11798 as "its condition has changed". We need to
11799 update the conditions on the target's side. */
11800 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, address)
11801 {
11802 loc = *loc2p;
11803
11804 if (!is_breakpoint (loc->owner)
11805 || pspace_num != loc->pspace->num)
11806 continue;
11807
11808 /* Flag the location appropriately. We use a different state to
11809 let everyone know that we already updated the set of locations
11810 with addr bl->address and program space bl->pspace. This is so
11811 we don't have to keep calling these functions just to mark locations
11812 that have already been marked. */
11813 loc->condition_changed = condition_updated;
11814
11815 /* Free the agent expression bytecode as well. We will compute
11816 it later on. */
833177a4 11817 loc->cond_bytecode.reset ();
b775012e
LM
11818 }
11819}
7f32a4d5 11820
44702360
PA
11821/* Called whether new breakpoints are created, or existing breakpoints
11822 deleted, to update the global location list and recompute which
11823 locations are duplicate of which.
b775012e 11824
04086b45
PA
11825 The INSERT_MODE flag determines whether locations may not, may, or
11826 shall be inserted now. See 'enum ugll_insert_mode' for more
11827 info. */
b60e7edf 11828
0d381245 11829static void
44702360 11830update_global_location_list (enum ugll_insert_mode insert_mode)
0d381245 11831{
74960c60 11832 struct breakpoint *b;
876fa593 11833 struct bp_location **locp, *loc;
b775012e
LM
11834 /* Last breakpoint location address that was marked for update. */
11835 CORE_ADDR last_addr = 0;
11836 /* Last breakpoint location program space that was marked for update. */
11837 int last_pspace_num = -1;
f7545552 11838
2d134ed3
PA
11839 /* Used in the duplicates detection below. When iterating over all
11840 bp_locations, points to the first bp_location of a given address.
11841 Breakpoints and watchpoints of different types are never
11842 duplicates of each other. Keep one pointer for each type of
11843 breakpoint/watchpoint, so we only need to loop over all locations
11844 once. */
11845 struct bp_location *bp_loc_first; /* breakpoint */
11846 struct bp_location *wp_loc_first; /* hardware watchpoint */
11847 struct bp_location *awp_loc_first; /* access watchpoint */
11848 struct bp_location *rwp_loc_first; /* read watchpoint */
876fa593 11849
f5336ca5
PA
11850 /* Saved former bp_locations array which we compare against the newly
11851 built bp_locations from the current state of ALL_BREAKPOINTS. */
81b1e71c 11852 struct bp_location **old_locp;
f5336ca5 11853 unsigned old_locations_count;
81b1e71c 11854 gdb::unique_xmalloc_ptr<struct bp_location *> old_locations (bp_locations);
876fa593 11855
f5336ca5
PA
11856 old_locations_count = bp_locations_count;
11857 bp_locations = NULL;
11858 bp_locations_count = 0;
0d381245 11859
74960c60 11860 ALL_BREAKPOINTS (b)
876fa593 11861 for (loc = b->loc; loc; loc = loc->next)
f5336ca5 11862 bp_locations_count++;
876fa593 11863
f5336ca5
PA
11864 bp_locations = XNEWVEC (struct bp_location *, bp_locations_count);
11865 locp = bp_locations;
876fa593
JK
11866 ALL_BREAKPOINTS (b)
11867 for (loc = b->loc; loc; loc = loc->next)
11868 *locp++ = loc;
7f32a4d5
PA
11869
11870 /* See if we need to "upgrade" a software breakpoint to a hardware
11871 breakpoint. Do this before deciding whether locations are
11872 duplicates. Also do this before sorting because sorting order
11873 depends on location type. */
11874 for (locp = bp_locations;
11875 locp < bp_locations + bp_locations_count;
11876 locp++)
11877 {
11878 loc = *locp;
11879 if (!loc->inserted && should_be_inserted (loc))
11880 handle_automatic_hardware_breakpoints (loc);
11881 }
11882
39ef2f62
CB
11883 std::sort (bp_locations, bp_locations + bp_locations_count,
11884 bp_location_is_less_than);
876fa593 11885
f5336ca5 11886 bp_locations_target_extensions_update ();
74960c60 11887
4a64f543
MS
11888 /* Identify bp_location instances that are no longer present in the
11889 new list, and therefore should be freed. Note that it's not
11890 necessary that those locations should be removed from inferior --
11891 if there's another location at the same address (previously
11892 marked as duplicate), we don't need to remove/insert the
11893 location.
876fa593 11894
4a64f543
MS
11895 LOCP is kept in sync with OLD_LOCP, each pointing to the current
11896 and former bp_location array state respectively. */
876fa593 11897
f5336ca5 11898 locp = bp_locations;
81b1e71c
TT
11899 for (old_locp = old_locations.get ();
11900 old_locp < old_locations.get () + old_locations_count;
876fa593 11901 old_locp++)
74960c60 11902 {
876fa593 11903 struct bp_location *old_loc = *old_locp;
c7d46a38 11904 struct bp_location **loc2p;
876fa593 11905
e5dd4106 11906 /* Tells if 'old_loc' is found among the new locations. If
4a64f543 11907 not, we have to free it. */
c7d46a38 11908 int found_object = 0;
20874c92
VP
11909 /* Tells if the location should remain inserted in the target. */
11910 int keep_in_target = 0;
11911 int removed = 0;
876fa593 11912
4a64f543
MS
11913 /* Skip LOCP entries which will definitely never be needed.
11914 Stop either at or being the one matching OLD_LOC. */
f5336ca5 11915 while (locp < bp_locations + bp_locations_count
c7d46a38 11916 && (*locp)->address < old_loc->address)
876fa593 11917 locp++;
c7d46a38
PA
11918
11919 for (loc2p = locp;
f5336ca5 11920 (loc2p < bp_locations + bp_locations_count
c7d46a38
PA
11921 && (*loc2p)->address == old_loc->address);
11922 loc2p++)
11923 {
b775012e
LM
11924 /* Check if this is a new/duplicated location or a duplicated
11925 location that had its condition modified. If so, we want to send
11926 its condition to the target if evaluation of conditions is taking
11927 place there. */
11928 if ((*loc2p)->condition_changed == condition_modified
11929 && (last_addr != old_loc->address
11930 || last_pspace_num != old_loc->pspace->num))
c7d46a38 11931 {
b775012e
LM
11932 force_breakpoint_reinsertion (*loc2p);
11933 last_pspace_num = old_loc->pspace->num;
c7d46a38 11934 }
b775012e
LM
11935
11936 if (*loc2p == old_loc)
11937 found_object = 1;
c7d46a38 11938 }
74960c60 11939
b775012e
LM
11940 /* We have already handled this address, update it so that we don't
11941 have to go through updates again. */
11942 last_addr = old_loc->address;
11943
11944 /* Target-side condition evaluation: Handle deleted locations. */
11945 if (!found_object)
11946 force_breakpoint_reinsertion (old_loc);
11947
4a64f543
MS
11948 /* If this location is no longer present, and inserted, look if
11949 there's maybe a new location at the same address. If so,
11950 mark that one inserted, and don't remove this one. This is
11951 needed so that we don't have a time window where a breakpoint
11952 at certain location is not inserted. */
74960c60 11953
876fa593 11954 if (old_loc->inserted)
0d381245 11955 {
4a64f543
MS
11956 /* If the location is inserted now, we might have to remove
11957 it. */
74960c60 11958
876fa593 11959 if (found_object && should_be_inserted (old_loc))
74960c60 11960 {
4a64f543
MS
11961 /* The location is still present in the location list,
11962 and still should be inserted. Don't do anything. */
20874c92 11963 keep_in_target = 1;
74960c60
VP
11964 }
11965 else
11966 {
b775012e
LM
11967 /* This location still exists, but it won't be kept in the
11968 target since it may have been disabled. We proceed to
11969 remove its target-side condition. */
11970
4a64f543
MS
11971 /* The location is either no longer present, or got
11972 disabled. See if there's another location at the
11973 same address, in which case we don't need to remove
11974 this one from the target. */
876fa593 11975
2bdf28a0 11976 /* OLD_LOC comes from existing struct breakpoint. */
cb1e4e32 11977 if (bl_address_is_meaningful (old_loc))
876fa593 11978 {
876fa593 11979 for (loc2p = locp;
f5336ca5 11980 (loc2p < bp_locations + bp_locations_count
c7d46a38 11981 && (*loc2p)->address == old_loc->address);
876fa593
JK
11982 loc2p++)
11983 {
11984 struct bp_location *loc2 = *loc2p;
11985
7f32a4d5
PA
11986 if (loc2 == old_loc)
11987 continue;
11988
2d134ed3 11989 if (breakpoint_locations_match (loc2, old_loc))
c7d46a38 11990 {
85d721b8
PA
11991 /* Read watchpoint locations are switched to
11992 access watchpoints, if the former are not
11993 supported, but the latter are. */
11994 if (is_hardware_watchpoint (old_loc->owner))
11995 {
11996 gdb_assert (is_hardware_watchpoint (loc2->owner));
11997 loc2->watchpoint_type = old_loc->watchpoint_type;
11998 }
11999
934709f0
PW
12000 /* loc2 is a duplicated location. We need to check
12001 if it should be inserted in case it will be
12002 unduplicated. */
7f32a4d5 12003 if (unduplicated_should_be_inserted (loc2))
c7d46a38 12004 {
934709f0 12005 swap_insertion (old_loc, loc2);
c7d46a38
PA
12006 keep_in_target = 1;
12007 break;
12008 }
876fa593
JK
12009 }
12010 }
12011 }
74960c60
VP
12012 }
12013
20874c92
VP
12014 if (!keep_in_target)
12015 {
834c0d03 12016 if (remove_breakpoint (old_loc))
20874c92 12017 {
4a64f543
MS
12018 /* This is just about all we can do. We could keep
12019 this location on the global list, and try to
12020 remove it next time, but there's no particular
12021 reason why we will succeed next time.
20874c92 12022
4a64f543
MS
12023 Note that at this point, old_loc->owner is still
12024 valid, as delete_breakpoint frees the breakpoint
12025 only after calling us. */
3e43a32a
MS
12026 printf_filtered (_("warning: Error removing "
12027 "breakpoint %d\n"),
876fa593 12028 old_loc->owner->number);
20874c92
VP
12029 }
12030 removed = 1;
12031 }
0d381245 12032 }
74960c60
VP
12033
12034 if (!found_object)
1c5cfe86 12035 {
fbea99ea 12036 if (removed && target_is_non_stop_p ()
1cf4d951 12037 && need_moribund_for_location_type (old_loc))
20874c92 12038 {
db82e815
PA
12039 /* This location was removed from the target. In
12040 non-stop mode, a race condition is possible where
12041 we've removed a breakpoint, but stop events for that
12042 breakpoint are already queued and will arrive later.
12043 We apply an heuristic to be able to distinguish such
12044 SIGTRAPs from other random SIGTRAPs: we keep this
12045 breakpoint location for a bit, and will retire it
12046 after we see some number of events. The theory here
12047 is that reporting of events should, "on the average",
12048 be fair, so after a while we'll see events from all
12049 threads that have anything of interest, and no longer
12050 need to keep this breakpoint location around. We
12051 don't hold locations forever so to reduce chances of
12052 mistaking a non-breakpoint SIGTRAP for a breakpoint
12053 SIGTRAP.
12054
12055 The heuristic failing can be disastrous on
12056 decr_pc_after_break targets.
12057
12058 On decr_pc_after_break targets, like e.g., x86-linux,
12059 if we fail to recognize a late breakpoint SIGTRAP,
12060 because events_till_retirement has reached 0 too
12061 soon, we'll fail to do the PC adjustment, and report
12062 a random SIGTRAP to the user. When the user resumes
12063 the inferior, it will most likely immediately crash
2dec564e 12064 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
db82e815
PA
12065 corrupted, because of being resumed e.g., in the
12066 middle of a multi-byte instruction, or skipped a
12067 one-byte instruction. This was actually seen happen
12068 on native x86-linux, and should be less rare on
12069 targets that do not support new thread events, like
12070 remote, due to the heuristic depending on
12071 thread_count.
12072
12073 Mistaking a random SIGTRAP for a breakpoint trap
12074 causes similar symptoms (PC adjustment applied when
12075 it shouldn't), but then again, playing with SIGTRAPs
12076 behind the debugger's back is asking for trouble.
12077
12078 Since hardware watchpoint traps are always
12079 distinguishable from other traps, so we don't need to
12080 apply keep hardware watchpoint moribund locations
12081 around. We simply always ignore hardware watchpoint
12082 traps we can no longer explain. */
12083
5b6d1e4f
PA
12084 process_stratum_target *proc_target = nullptr;
12085 for (inferior *inf : all_inferiors ())
12086 if (inf->pspace == old_loc->pspace)
12087 {
12088 proc_target = inf->process_target ();
12089 break;
12090 }
12091 if (proc_target != nullptr)
12092 old_loc->events_till_retirement
12093 = 3 * (thread_count (proc_target) + 1);
12094 else
12095 old_loc->events_till_retirement = 1;
876fa593 12096 old_loc->owner = NULL;
20874c92 12097
1123588c 12098 moribund_locations.push_back (old_loc);
1c5cfe86
PA
12099 }
12100 else
f431efe5
PA
12101 {
12102 old_loc->owner = NULL;
12103 decref_bp_location (&old_loc);
12104 }
20874c92 12105 }
74960c60 12106 }
1c5cfe86 12107
348d480f
PA
12108 /* Rescan breakpoints at the same address and section, marking the
12109 first one as "first" and any others as "duplicates". This is so
12110 that the bpt instruction is only inserted once. If we have a
12111 permanent breakpoint at the same place as BPT, make that one the
12112 official one, and the rest as duplicates. Permanent breakpoints
12113 are sorted first for the same address.
12114
12115 Do the same for hardware watchpoints, but also considering the
12116 watchpoint's type (regular/access/read) and length. */
12117
12118 bp_loc_first = NULL;
12119 wp_loc_first = NULL;
12120 awp_loc_first = NULL;
12121 rwp_loc_first = NULL;
12122 ALL_BP_LOCATIONS (loc, locp)
12123 {
12124 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
12125 non-NULL. */
348d480f 12126 struct bp_location **loc_first_p;
d3fbdd86 12127 b = loc->owner;
348d480f 12128
6f380991 12129 if (!unduplicated_should_be_inserted (loc)
cb1e4e32 12130 || !bl_address_is_meaningful (loc)
1e4d1764
YQ
12131 /* Don't detect duplicate for tracepoint locations because they are
12132 never duplicated. See the comments in field `duplicate' of
12133 `struct bp_location'. */
348d480f 12134 || is_tracepoint (b))
b775012e
LM
12135 {
12136 /* Clear the condition modification flag. */
12137 loc->condition_changed = condition_unchanged;
12138 continue;
12139 }
348d480f 12140
348d480f
PA
12141 if (b->type == bp_hardware_watchpoint)
12142 loc_first_p = &wp_loc_first;
12143 else if (b->type == bp_read_watchpoint)
12144 loc_first_p = &rwp_loc_first;
12145 else if (b->type == bp_access_watchpoint)
12146 loc_first_p = &awp_loc_first;
12147 else
12148 loc_first_p = &bp_loc_first;
12149
12150 if (*loc_first_p == NULL
12151 || (overlay_debugging && loc->section != (*loc_first_p)->section)
12152 || !breakpoint_locations_match (loc, *loc_first_p))
12153 {
12154 *loc_first_p = loc;
12155 loc->duplicate = 0;
b775012e
LM
12156
12157 if (is_breakpoint (loc->owner) && loc->condition_changed)
12158 {
12159 loc->needs_update = 1;
12160 /* Clear the condition modification flag. */
12161 loc->condition_changed = condition_unchanged;
12162 }
348d480f
PA
12163 continue;
12164 }
12165
934709f0
PW
12166
12167 /* This and the above ensure the invariant that the first location
12168 is not duplicated, and is the inserted one.
12169 All following are marked as duplicated, and are not inserted. */
12170 if (loc->inserted)
12171 swap_insertion (loc, *loc_first_p);
348d480f
PA
12172 loc->duplicate = 1;
12173
b775012e
LM
12174 /* Clear the condition modification flag. */
12175 loc->condition_changed = condition_unchanged;
348d480f
PA
12176 }
12177
a25a5a45 12178 if (insert_mode == UGLL_INSERT || breakpoints_should_be_inserted_now ())
b775012e 12179 {
04086b45 12180 if (insert_mode != UGLL_DONT_INSERT)
b775012e
LM
12181 insert_breakpoint_locations ();
12182 else
12183 {
44702360
PA
12184 /* Even though the caller told us to not insert new
12185 locations, we may still need to update conditions on the
12186 target's side of breakpoints that were already inserted
12187 if the target is evaluating breakpoint conditions. We
b775012e
LM
12188 only update conditions for locations that are marked
12189 "needs_update". */
12190 update_inserted_breakpoint_locations ();
12191 }
12192 }
348d480f 12193
04086b45 12194 if (insert_mode != UGLL_DONT_INSERT)
1e4d1764 12195 download_tracepoint_locations ();
348d480f
PA
12196}
12197
12198void
12199breakpoint_retire_moribund (void)
12200{
1123588c
TT
12201 for (int ix = 0; ix < moribund_locations.size (); ++ix)
12202 {
12203 struct bp_location *loc = moribund_locations[ix];
12204 if (--(loc->events_till_retirement) == 0)
12205 {
12206 decref_bp_location (&loc);
12207 unordered_remove (moribund_locations, ix);
12208 --ix;
12209 }
12210 }
348d480f
PA
12211}
12212
12213static void
44702360 12214update_global_location_list_nothrow (enum ugll_insert_mode insert_mode)
348d480f 12215{
348d480f 12216
a70b8144 12217 try
492d29ea
PA
12218 {
12219 update_global_location_list (insert_mode);
12220 }
230d2906 12221 catch (const gdb_exception_error &e)
492d29ea
PA
12222 {
12223 }
348d480f
PA
12224}
12225
12226/* Clear BKP from a BPS. */
12227
12228static void
12229bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
12230{
12231 bpstat bs;
12232
12233 for (bs = bps; bs; bs = bs->next)
12234 if (bs->breakpoint_at == bpt)
12235 {
12236 bs->breakpoint_at = NULL;
12237 bs->old_val = NULL;
12238 /* bs->commands will be freed later. */
12239 }
12240}
12241
12242/* Callback for iterate_over_threads. */
12243static int
12244bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
12245{
9a3c8263 12246 struct breakpoint *bpt = (struct breakpoint *) data;
348d480f
PA
12247
12248 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
12249 return 0;
12250}
12251
12252/* Helper for breakpoint and tracepoint breakpoint_ops->mention
12253 callbacks. */
12254
12255static void
12256say_where (struct breakpoint *b)
12257{
12258 struct value_print_options opts;
12259
12260 get_user_print_options (&opts);
12261
12262 /* i18n: cagney/2005-02-11: Below needs to be merged into a
12263 single string. */
12264 if (b->loc == NULL)
12265 {
f00aae0f
KS
12266 /* For pending locations, the output differs slightly based
12267 on b->extra_string. If this is non-NULL, it contains either
12268 a condition or dprintf arguments. */
12269 if (b->extra_string == NULL)
12270 {
12271 printf_filtered (_(" (%s) pending."),
d28cd78a 12272 event_location_to_string (b->location.get ()));
f00aae0f
KS
12273 }
12274 else if (b->type == bp_dprintf)
12275 {
12276 printf_filtered (_(" (%s,%s) pending."),
d28cd78a 12277 event_location_to_string (b->location.get ()),
f00aae0f
KS
12278 b->extra_string);
12279 }
12280 else
12281 {
12282 printf_filtered (_(" (%s %s) pending."),
d28cd78a 12283 event_location_to_string (b->location.get ()),
f00aae0f
KS
12284 b->extra_string);
12285 }
348d480f
PA
12286 }
12287 else
12288 {
2f202fde 12289 if (opts.addressprint || b->loc->symtab == NULL)
6a831f06
PA
12290 printf_filtered (" at %ps",
12291 styled_string (address_style.style (),
12292 paddress (b->loc->gdbarch,
12293 b->loc->address)));
2f202fde 12294 if (b->loc->symtab != NULL)
f8eba3c6
TT
12295 {
12296 /* If there is a single location, we can print the location
12297 more nicely. */
12298 if (b->loc->next == NULL)
0bb296cb 12299 {
6a831f06
PA
12300 const char *filename
12301 = symtab_to_filename_for_display (b->loc->symtab);
12302 printf_filtered (": file %ps, line %d.",
12303 styled_string (file_name_style.style (),
12304 filename),
0bb296cb
TT
12305 b->loc->line_number);
12306 }
f8eba3c6
TT
12307 else
12308 /* This is not ideal, but each location may have a
12309 different file name, and this at least reflects the
12310 real situation somewhat. */
f00aae0f 12311 printf_filtered (": %s.",
d28cd78a 12312 event_location_to_string (b->location.get ()));
f8eba3c6 12313 }
348d480f
PA
12314
12315 if (b->loc->next)
12316 {
12317 struct bp_location *loc = b->loc;
12318 int n = 0;
12319 for (; loc; loc = loc->next)
12320 ++n;
12321 printf_filtered (" (%d locations)", n);
12322 }
12323 }
12324}
12325
5f486660 12326bp_location::~bp_location ()
348d480f 12327{
5f486660 12328 xfree (function_name);
348d480f
PA
12329}
12330
c1fc2657 12331/* Destructor for the breakpoint base class. */
348d480f 12332
c1fc2657 12333breakpoint::~breakpoint ()
348d480f 12334{
c1fc2657
SM
12335 xfree (this->cond_string);
12336 xfree (this->extra_string);
348d480f
PA
12337}
12338
2060206e
PA
12339static struct bp_location *
12340base_breakpoint_allocate_location (struct breakpoint *self)
348d480f 12341{
5f486660 12342 return new bp_location (self);
348d480f
PA
12343}
12344
2060206e
PA
12345static void
12346base_breakpoint_re_set (struct breakpoint *b)
12347{
12348 /* Nothing to re-set. */
12349}
12350
12351#define internal_error_pure_virtual_called() \
12352 gdb_assert_not_reached ("pure virtual function called")
12353
12354static int
12355base_breakpoint_insert_location (struct bp_location *bl)
12356{
12357 internal_error_pure_virtual_called ();
12358}
12359
12360static int
73971819
PA
12361base_breakpoint_remove_location (struct bp_location *bl,
12362 enum remove_bp_reason reason)
2060206e
PA
12363{
12364 internal_error_pure_virtual_called ();
12365}
12366
12367static int
12368base_breakpoint_breakpoint_hit (const struct bp_location *bl,
bd522513 12369 const address_space *aspace,
09ac7c10
TT
12370 CORE_ADDR bp_addr,
12371 const struct target_waitstatus *ws)
2060206e
PA
12372{
12373 internal_error_pure_virtual_called ();
12374}
12375
12376static void
12377base_breakpoint_check_status (bpstat bs)
12378{
12379 /* Always stop. */
12380}
12381
12382/* A "works_in_software_mode" breakpoint_ops method that just internal
12383 errors. */
12384
12385static int
12386base_breakpoint_works_in_software_mode (const struct breakpoint *b)
12387{
12388 internal_error_pure_virtual_called ();
12389}
12390
12391/* A "resources_needed" breakpoint_ops method that just internal
12392 errors. */
12393
12394static int
12395base_breakpoint_resources_needed (const struct bp_location *bl)
12396{
12397 internal_error_pure_virtual_called ();
12398}
12399
12400static enum print_stop_action
12401base_breakpoint_print_it (bpstat bs)
12402{
12403 internal_error_pure_virtual_called ();
12404}
12405
12406static void
12407base_breakpoint_print_one_detail (const struct breakpoint *self,
12408 struct ui_out *uiout)
12409{
12410 /* nothing */
12411}
12412
12413static void
12414base_breakpoint_print_mention (struct breakpoint *b)
12415{
12416 internal_error_pure_virtual_called ();
12417}
12418
12419static void
12420base_breakpoint_print_recreate (struct breakpoint *b, struct ui_file *fp)
12421{
12422 internal_error_pure_virtual_called ();
12423}
12424
983af33b 12425static void
f00aae0f 12426base_breakpoint_create_sals_from_location
626d2320 12427 (struct event_location *location,
f00aae0f
KS
12428 struct linespec_result *canonical,
12429 enum bptype type_wanted)
983af33b
SDJ
12430{
12431 internal_error_pure_virtual_called ();
12432}
12433
12434static void
12435base_breakpoint_create_breakpoints_sal (struct gdbarch *gdbarch,
12436 struct linespec_result *c,
e1e01040
PA
12437 gdb::unique_xmalloc_ptr<char> cond_string,
12438 gdb::unique_xmalloc_ptr<char> extra_string,
983af33b
SDJ
12439 enum bptype type_wanted,
12440 enum bpdisp disposition,
12441 int thread,
12442 int task, int ignore_count,
12443 const struct breakpoint_ops *o,
12444 int from_tty, int enabled,
44f238bb 12445 int internal, unsigned flags)
983af33b
SDJ
12446{
12447 internal_error_pure_virtual_called ();
12448}
12449
6c5b2ebe 12450static std::vector<symtab_and_line>
f00aae0f 12451base_breakpoint_decode_location (struct breakpoint *b,
626d2320 12452 struct event_location *location,
6c5b2ebe 12453 struct program_space *search_pspace)
983af33b
SDJ
12454{
12455 internal_error_pure_virtual_called ();
12456}
12457
ab04a2af
TT
12458/* The default 'explains_signal' method. */
12459
47591c29 12460static int
427cd150 12461base_breakpoint_explains_signal (struct breakpoint *b, enum gdb_signal sig)
ab04a2af 12462{
47591c29 12463 return 1;
ab04a2af
TT
12464}
12465
9d6e6e84
HZ
12466/* The default "after_condition_true" method. */
12467
12468static void
12469base_breakpoint_after_condition_true (struct bpstats *bs)
12470{
12471 /* Nothing to do. */
12472}
12473
ab04a2af 12474struct breakpoint_ops base_breakpoint_ops =
2060206e 12475{
2060206e
PA
12476 base_breakpoint_allocate_location,
12477 base_breakpoint_re_set,
12478 base_breakpoint_insert_location,
12479 base_breakpoint_remove_location,
12480 base_breakpoint_breakpoint_hit,
12481 base_breakpoint_check_status,
12482 base_breakpoint_resources_needed,
12483 base_breakpoint_works_in_software_mode,
12484 base_breakpoint_print_it,
12485 NULL,
12486 base_breakpoint_print_one_detail,
12487 base_breakpoint_print_mention,
983af33b 12488 base_breakpoint_print_recreate,
5f700d83 12489 base_breakpoint_create_sals_from_location,
983af33b 12490 base_breakpoint_create_breakpoints_sal,
5f700d83 12491 base_breakpoint_decode_location,
9d6e6e84
HZ
12492 base_breakpoint_explains_signal,
12493 base_breakpoint_after_condition_true,
2060206e
PA
12494};
12495
12496/* Default breakpoint_ops methods. */
12497
12498static void
348d480f
PA
12499bkpt_re_set (struct breakpoint *b)
12500{
06edf0c0 12501 /* FIXME: is this still reachable? */
9ef9e6a6 12502 if (breakpoint_event_location_empty_p (b))
06edf0c0 12503 {
f00aae0f 12504 /* Anything without a location can't be re-set. */
348d480f 12505 delete_breakpoint (b);
06edf0c0 12506 return;
348d480f 12507 }
06edf0c0
PA
12508
12509 breakpoint_re_set_default (b);
348d480f
PA
12510}
12511
2060206e 12512static int
348d480f
PA
12513bkpt_insert_location (struct bp_location *bl)
12514{
cd6c3b4f
YQ
12515 CORE_ADDR addr = bl->target_info.reqstd_address;
12516
579c6ad9 12517 bl->target_info.kind = breakpoint_kind (bl, &addr);
cd6c3b4f
YQ
12518 bl->target_info.placed_address = addr;
12519
348d480f 12520 if (bl->loc_type == bp_loc_hardware_breakpoint)
7c16b83e 12521 return target_insert_hw_breakpoint (bl->gdbarch, &bl->target_info);
348d480f 12522 else
7c16b83e 12523 return target_insert_breakpoint (bl->gdbarch, &bl->target_info);
348d480f
PA
12524}
12525
2060206e 12526static int
73971819 12527bkpt_remove_location (struct bp_location *bl, enum remove_bp_reason reason)
348d480f
PA
12528{
12529 if (bl->loc_type == bp_loc_hardware_breakpoint)
12530 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
12531 else
73971819 12532 return target_remove_breakpoint (bl->gdbarch, &bl->target_info, reason);
348d480f
PA
12533}
12534
2060206e 12535static int
348d480f 12536bkpt_breakpoint_hit (const struct bp_location *bl,
bd522513 12537 const address_space *aspace, CORE_ADDR bp_addr,
09ac7c10 12538 const struct target_waitstatus *ws)
348d480f 12539{
09ac7c10 12540 if (ws->kind != TARGET_WAITKIND_STOPPED
a493e3e2 12541 || ws->value.sig != GDB_SIGNAL_TRAP)
09ac7c10
TT
12542 return 0;
12543
348d480f
PA
12544 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
12545 aspace, bp_addr))
12546 return 0;
12547
12548 if (overlay_debugging /* unmapped overlay section */
12549 && section_is_overlay (bl->section)
12550 && !section_is_mapped (bl->section))
12551 return 0;
12552
12553 return 1;
12554}
12555
cd1608cc
PA
12556static int
12557dprintf_breakpoint_hit (const struct bp_location *bl,
bd522513 12558 const address_space *aspace, CORE_ADDR bp_addr,
cd1608cc
PA
12559 const struct target_waitstatus *ws)
12560{
12561 if (dprintf_style == dprintf_style_agent
12562 && target_can_run_breakpoint_commands ())
12563 {
12564 /* An agent-style dprintf never causes a stop. If we see a trap
12565 for this address it must be for a breakpoint that happens to
12566 be set at the same address. */
12567 return 0;
12568 }
12569
12570 return bkpt_breakpoint_hit (bl, aspace, bp_addr, ws);
12571}
12572
2060206e 12573static int
348d480f
PA
12574bkpt_resources_needed (const struct bp_location *bl)
12575{
12576 gdb_assert (bl->owner->type == bp_hardware_breakpoint);
12577
12578 return 1;
12579}
12580
2060206e 12581static enum print_stop_action
348d480f
PA
12582bkpt_print_it (bpstat bs)
12583{
348d480f
PA
12584 struct breakpoint *b;
12585 const struct bp_location *bl;
001c8c33 12586 int bp_temp;
79a45e25 12587 struct ui_out *uiout = current_uiout;
348d480f
PA
12588
12589 gdb_assert (bs->bp_location_at != NULL);
12590
12591 bl = bs->bp_location_at;
12592 b = bs->breakpoint_at;
12593
001c8c33
PA
12594 bp_temp = b->disposition == disp_del;
12595 if (bl->address != bl->requested_address)
12596 breakpoint_adjustment_warning (bl->requested_address,
12597 bl->address,
12598 b->number, 1);
12599 annotate_breakpoint (b->number);
f303dbd6
PA
12600 maybe_print_thread_hit_breakpoint (uiout);
12601
112e8700 12602 if (uiout->is_mi_like_p ())
348d480f 12603 {
112e8700 12604 uiout->field_string ("reason",
001c8c33 12605 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
112e8700 12606 uiout->field_string ("disp", bpdisp_text (b->disposition));
06edf0c0 12607 }
6a831f06
PA
12608 if (bp_temp)
12609 uiout->message ("Temporary breakpoint %pF, ",
12610 signed_field ("bkptno", b->number));
12611 else
12612 uiout->message ("Breakpoint %pF, ",
12613 signed_field ("bkptno", b->number));
06edf0c0 12614
001c8c33 12615 return PRINT_SRC_AND_LOC;
06edf0c0
PA
12616}
12617
2060206e 12618static void
06edf0c0
PA
12619bkpt_print_mention (struct breakpoint *b)
12620{
112e8700 12621 if (current_uiout->is_mi_like_p ())
06edf0c0
PA
12622 return;
12623
12624 switch (b->type)
12625 {
12626 case bp_breakpoint:
12627 case bp_gnu_ifunc_resolver:
12628 if (b->disposition == disp_del)
12629 printf_filtered (_("Temporary breakpoint"));
12630 else
12631 printf_filtered (_("Breakpoint"));
12632 printf_filtered (_(" %d"), b->number);
12633 if (b->type == bp_gnu_ifunc_resolver)
12634 printf_filtered (_(" at gnu-indirect-function resolver"));
12635 break;
12636 case bp_hardware_breakpoint:
12637 printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
12638 break;
e7e0cddf
SS
12639 case bp_dprintf:
12640 printf_filtered (_("Dprintf %d"), b->number);
12641 break;
06edf0c0
PA
12642 }
12643
12644 say_where (b);
12645}
12646
2060206e 12647static void
06edf0c0
PA
12648bkpt_print_recreate (struct breakpoint *tp, struct ui_file *fp)
12649{
12650 if (tp->type == bp_breakpoint && tp->disposition == disp_del)
12651 fprintf_unfiltered (fp, "tbreak");
12652 else if (tp->type == bp_breakpoint)
12653 fprintf_unfiltered (fp, "break");
12654 else if (tp->type == bp_hardware_breakpoint
12655 && tp->disposition == disp_del)
12656 fprintf_unfiltered (fp, "thbreak");
12657 else if (tp->type == bp_hardware_breakpoint)
12658 fprintf_unfiltered (fp, "hbreak");
12659 else
12660 internal_error (__FILE__, __LINE__,
12661 _("unhandled breakpoint type %d"), (int) tp->type);
12662
f00aae0f 12663 fprintf_unfiltered (fp, " %s",
d28cd78a 12664 event_location_to_string (tp->location.get ()));
f00aae0f
KS
12665
12666 /* Print out extra_string if this breakpoint is pending. It might
12667 contain, for example, conditions that were set by the user. */
12668 if (tp->loc == NULL && tp->extra_string != NULL)
12669 fprintf_unfiltered (fp, " %s", tp->extra_string);
12670
dd11a36c 12671 print_recreate_thread (tp, fp);
06edf0c0
PA
12672}
12673
983af33b 12674static void
626d2320 12675bkpt_create_sals_from_location (struct event_location *location,
f00aae0f
KS
12676 struct linespec_result *canonical,
12677 enum bptype type_wanted)
983af33b 12678{
f00aae0f 12679 create_sals_from_location_default (location, canonical, type_wanted);
983af33b
SDJ
12680}
12681
12682static void
12683bkpt_create_breakpoints_sal (struct gdbarch *gdbarch,
12684 struct linespec_result *canonical,
e1e01040
PA
12685 gdb::unique_xmalloc_ptr<char> cond_string,
12686 gdb::unique_xmalloc_ptr<char> extra_string,
983af33b
SDJ
12687 enum bptype type_wanted,
12688 enum bpdisp disposition,
12689 int thread,
12690 int task, int ignore_count,
12691 const struct breakpoint_ops *ops,
12692 int from_tty, int enabled,
44f238bb 12693 int internal, unsigned flags)
983af33b 12694{
023fa29b 12695 create_breakpoints_sal_default (gdbarch, canonical,
e1e01040
PA
12696 std::move (cond_string),
12697 std::move (extra_string),
e7e0cddf 12698 type_wanted,
983af33b
SDJ
12699 disposition, thread, task,
12700 ignore_count, ops, from_tty,
44f238bb 12701 enabled, internal, flags);
983af33b
SDJ
12702}
12703
6c5b2ebe 12704static std::vector<symtab_and_line>
f00aae0f 12705bkpt_decode_location (struct breakpoint *b,
626d2320 12706 struct event_location *location,
6c5b2ebe 12707 struct program_space *search_pspace)
983af33b 12708{
6c5b2ebe 12709 return decode_location_default (b, location, search_pspace);
983af33b
SDJ
12710}
12711
06edf0c0
PA
12712/* Virtual table for internal breakpoints. */
12713
12714static void
12715internal_bkpt_re_set (struct breakpoint *b)
12716{
12717 switch (b->type)
12718 {
12719 /* Delete overlay event and longjmp master breakpoints; they
12720 will be reset later by breakpoint_re_set. */
12721 case bp_overlay_event:
12722 case bp_longjmp_master:
12723 case bp_std_terminate_master:
12724 case bp_exception_master:
12725 delete_breakpoint (b);
12726 break;
12727
12728 /* This breakpoint is special, it's set up when the inferior
12729 starts and we really don't want to touch it. */
12730 case bp_shlib_event:
12731
12732 /* Like bp_shlib_event, this breakpoint type is special. Once
12733 it is set up, we do not want to touch it. */
12734 case bp_thread_event:
12735 break;
12736 }
12737}
12738
12739static void
12740internal_bkpt_check_status (bpstat bs)
12741{
a9b3a50f
PA
12742 if (bs->breakpoint_at->type == bp_shlib_event)
12743 {
12744 /* If requested, stop when the dynamic linker notifies GDB of
12745 events. This allows the user to get control and place
12746 breakpoints in initializer routines for dynamically loaded
12747 objects (among other things). */
12748 bs->stop = stop_on_solib_events;
12749 bs->print = stop_on_solib_events;
12750 }
12751 else
12752 bs->stop = 0;
06edf0c0
PA
12753}
12754
12755static enum print_stop_action
12756internal_bkpt_print_it (bpstat bs)
12757{
06edf0c0 12758 struct breakpoint *b;
06edf0c0 12759
06edf0c0
PA
12760 b = bs->breakpoint_at;
12761
06edf0c0
PA
12762 switch (b->type)
12763 {
348d480f
PA
12764 case bp_shlib_event:
12765 /* Did we stop because the user set the stop_on_solib_events
12766 variable? (If so, we report this as a generic, "Stopped due
12767 to shlib event" message.) */
edcc5120 12768 print_solib_event (0);
348d480f
PA
12769 break;
12770
12771 case bp_thread_event:
12772 /* Not sure how we will get here.
12773 GDB should not stop for these breakpoints. */
12774 printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
12775 break;
12776
12777 case bp_overlay_event:
12778 /* By analogy with the thread event, GDB should not stop for these. */
12779 printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
12780 break;
12781
12782 case bp_longjmp_master:
12783 /* These should never be enabled. */
12784 printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
348d480f
PA
12785 break;
12786
12787 case bp_std_terminate_master:
12788 /* These should never be enabled. */
12789 printf_filtered (_("std::terminate Master Breakpoint: "
12790 "gdb should not stop!\n"));
348d480f
PA
12791 break;
12792
12793 case bp_exception_master:
12794 /* These should never be enabled. */
12795 printf_filtered (_("Exception Master Breakpoint: "
12796 "gdb should not stop!\n"));
06edf0c0
PA
12797 break;
12798 }
12799
001c8c33 12800 return PRINT_NOTHING;
06edf0c0
PA
12801}
12802
12803static void
12804internal_bkpt_print_mention (struct breakpoint *b)
12805{
12806 /* Nothing to mention. These breakpoints are internal. */
12807}
12808
06edf0c0
PA
12809/* Virtual table for momentary breakpoints */
12810
12811static void
12812momentary_bkpt_re_set (struct breakpoint *b)
12813{
12814 /* Keep temporary breakpoints, which can be encountered when we step
4d1eb6b4 12815 over a dlopen call and solib_add is resetting the breakpoints.
06edf0c0
PA
12816 Otherwise these should have been blown away via the cleanup chain
12817 or by breakpoint_init_inferior when we rerun the executable. */
12818}
12819
12820static void
12821momentary_bkpt_check_status (bpstat bs)
12822{
12823 /* Nothing. The point of these breakpoints is causing a stop. */
12824}
12825
12826static enum print_stop_action
12827momentary_bkpt_print_it (bpstat bs)
12828{
001c8c33 12829 return PRINT_UNKNOWN;
348d480f
PA
12830}
12831
06edf0c0
PA
12832static void
12833momentary_bkpt_print_mention (struct breakpoint *b)
348d480f 12834{
06edf0c0 12835 /* Nothing to mention. These breakpoints are internal. */
348d480f
PA
12836}
12837
e2e4d78b
JK
12838/* Ensure INITIATING_FRAME is cleared when no such breakpoint exists.
12839
12840 It gets cleared already on the removal of the first one of such placed
12841 breakpoints. This is OK as they get all removed altogether. */
12842
c1fc2657 12843longjmp_breakpoint::~longjmp_breakpoint ()
e2e4d78b 12844{
c1fc2657 12845 thread_info *tp = find_thread_global_id (this->thread);
e2e4d78b 12846
c1fc2657 12847 if (tp != NULL)
e2e4d78b 12848 tp->initiating_frame = null_frame_id;
e2e4d78b
JK
12849}
12850
55aa24fb
SDJ
12851/* Specific methods for probe breakpoints. */
12852
12853static int
12854bkpt_probe_insert_location (struct bp_location *bl)
12855{
12856 int v = bkpt_insert_location (bl);
12857
12858 if (v == 0)
12859 {
12860 /* The insertion was successful, now let's set the probe's semaphore
12861 if needed. */
935676c9 12862 bl->probe.prob->set_semaphore (bl->probe.objfile, bl->gdbarch);
55aa24fb
SDJ
12863 }
12864
12865 return v;
12866}
12867
12868static int
73971819
PA
12869bkpt_probe_remove_location (struct bp_location *bl,
12870 enum remove_bp_reason reason)
55aa24fb
SDJ
12871{
12872 /* Let's clear the semaphore before removing the location. */
935676c9 12873 bl->probe.prob->clear_semaphore (bl->probe.objfile, bl->gdbarch);
55aa24fb 12874
73971819 12875 return bkpt_remove_location (bl, reason);
55aa24fb
SDJ
12876}
12877
12878static void
626d2320 12879bkpt_probe_create_sals_from_location (struct event_location *location,
5f700d83 12880 struct linespec_result *canonical,
f00aae0f 12881 enum bptype type_wanted)
55aa24fb
SDJ
12882{
12883 struct linespec_sals lsal;
12884
c2f4122d 12885 lsal.sals = parse_probes (location, NULL, canonical);
8e9e35b1
TT
12886 lsal.canonical
12887 = xstrdup (event_location_to_string (canonical->location.get ()));
6c5b2ebe 12888 canonical->lsals.push_back (std::move (lsal));
55aa24fb
SDJ
12889}
12890
6c5b2ebe 12891static std::vector<symtab_and_line>
f00aae0f 12892bkpt_probe_decode_location (struct breakpoint *b,
626d2320 12893 struct event_location *location,
6c5b2ebe 12894 struct program_space *search_pspace)
55aa24fb 12895{
6c5b2ebe
PA
12896 std::vector<symtab_and_line> sals = parse_probes (location, search_pspace, NULL);
12897 if (sals.empty ())
55aa24fb 12898 error (_("probe not found"));
6c5b2ebe 12899 return sals;
55aa24fb
SDJ
12900}
12901
348d480f 12902/* The breakpoint_ops structure to be used in tracepoints. */
876fa593 12903
348d480f
PA
12904static void
12905tracepoint_re_set (struct breakpoint *b)
12906{
12907 breakpoint_re_set_default (b);
12908}
876fa593 12909
348d480f
PA
12910static int
12911tracepoint_breakpoint_hit (const struct bp_location *bl,
bd522513 12912 const address_space *aspace, CORE_ADDR bp_addr,
09ac7c10 12913 const struct target_waitstatus *ws)
348d480f
PA
12914{
12915 /* By definition, the inferior does not report stops at
12916 tracepoints. */
12917 return 0;
74960c60
VP
12918}
12919
12920static void
348d480f
PA
12921tracepoint_print_one_detail (const struct breakpoint *self,
12922 struct ui_out *uiout)
74960c60 12923{
d9b3f62e 12924 struct tracepoint *tp = (struct tracepoint *) self;
5d9310c4 12925 if (!tp->static_trace_marker_id.empty ())
348d480f
PA
12926 {
12927 gdb_assert (self->type == bp_static_tracepoint);
cc59ec59 12928
6a831f06
PA
12929 uiout->message ("\tmarker id is %pF\n",
12930 string_field ("static-tracepoint-marker-string-id",
12931 tp->static_trace_marker_id.c_str ()));
348d480f 12932 }
0d381245
VP
12933}
12934
a474d7c2 12935static void
348d480f 12936tracepoint_print_mention (struct breakpoint *b)
a474d7c2 12937{
112e8700 12938 if (current_uiout->is_mi_like_p ())
348d480f 12939 return;
cc59ec59 12940
348d480f
PA
12941 switch (b->type)
12942 {
12943 case bp_tracepoint:
12944 printf_filtered (_("Tracepoint"));
12945 printf_filtered (_(" %d"), b->number);
12946 break;
12947 case bp_fast_tracepoint:
12948 printf_filtered (_("Fast tracepoint"));
12949 printf_filtered (_(" %d"), b->number);
12950 break;
12951 case bp_static_tracepoint:
12952 printf_filtered (_("Static tracepoint"));
12953 printf_filtered (_(" %d"), b->number);
12954 break;
12955 default:
12956 internal_error (__FILE__, __LINE__,
12957 _("unhandled tracepoint type %d"), (int) b->type);
12958 }
12959
12960 say_where (b);
a474d7c2
PA
12961}
12962
348d480f 12963static void
d9b3f62e 12964tracepoint_print_recreate (struct breakpoint *self, struct ui_file *fp)
a474d7c2 12965{
d9b3f62e
PA
12966 struct tracepoint *tp = (struct tracepoint *) self;
12967
12968 if (self->type == bp_fast_tracepoint)
348d480f 12969 fprintf_unfiltered (fp, "ftrace");
c93e8391 12970 else if (self->type == bp_static_tracepoint)
348d480f 12971 fprintf_unfiltered (fp, "strace");
d9b3f62e 12972 else if (self->type == bp_tracepoint)
348d480f
PA
12973 fprintf_unfiltered (fp, "trace");
12974 else
12975 internal_error (__FILE__, __LINE__,
d9b3f62e 12976 _("unhandled tracepoint type %d"), (int) self->type);
cc59ec59 12977
f00aae0f 12978 fprintf_unfiltered (fp, " %s",
d28cd78a 12979 event_location_to_string (self->location.get ()));
d9b3f62e
PA
12980 print_recreate_thread (self, fp);
12981
12982 if (tp->pass_count)
12983 fprintf_unfiltered (fp, " passcount %d\n", tp->pass_count);
a474d7c2
PA
12984}
12985
983af33b 12986static void
626d2320 12987tracepoint_create_sals_from_location (struct event_location *location,
f00aae0f
KS
12988 struct linespec_result *canonical,
12989 enum bptype type_wanted)
983af33b 12990{
f00aae0f 12991 create_sals_from_location_default (location, canonical, type_wanted);
983af33b
SDJ
12992}
12993
12994static void
12995tracepoint_create_breakpoints_sal (struct gdbarch *gdbarch,
12996 struct linespec_result *canonical,
e1e01040
PA
12997 gdb::unique_xmalloc_ptr<char> cond_string,
12998 gdb::unique_xmalloc_ptr<char> extra_string,
983af33b
SDJ
12999 enum bptype type_wanted,
13000 enum bpdisp disposition,
13001 int thread,
13002 int task, int ignore_count,
13003 const struct breakpoint_ops *ops,
13004 int from_tty, int enabled,
44f238bb 13005 int internal, unsigned flags)
983af33b 13006{
023fa29b 13007 create_breakpoints_sal_default (gdbarch, canonical,
e1e01040
PA
13008 std::move (cond_string),
13009 std::move (extra_string),
e7e0cddf 13010 type_wanted,
983af33b
SDJ
13011 disposition, thread, task,
13012 ignore_count, ops, from_tty,
44f238bb 13013 enabled, internal, flags);
983af33b
SDJ
13014}
13015
6c5b2ebe 13016static std::vector<symtab_and_line>
f00aae0f 13017tracepoint_decode_location (struct breakpoint *b,
626d2320 13018 struct event_location *location,
6c5b2ebe 13019 struct program_space *search_pspace)
983af33b 13020{
6c5b2ebe 13021 return decode_location_default (b, location, search_pspace);
983af33b
SDJ
13022}
13023
2060206e 13024struct breakpoint_ops tracepoint_breakpoint_ops;
348d480f 13025
bac7c5cf 13026/* Virtual table for tracepoints on static probes. */
55aa24fb
SDJ
13027
13028static void
f00aae0f 13029tracepoint_probe_create_sals_from_location
626d2320 13030 (struct event_location *location,
f00aae0f
KS
13031 struct linespec_result *canonical,
13032 enum bptype type_wanted)
55aa24fb
SDJ
13033{
13034 /* We use the same method for breakpoint on probes. */
f00aae0f 13035 bkpt_probe_create_sals_from_location (location, canonical, type_wanted);
55aa24fb
SDJ
13036}
13037
6c5b2ebe 13038static std::vector<symtab_and_line>
f00aae0f 13039tracepoint_probe_decode_location (struct breakpoint *b,
626d2320 13040 struct event_location *location,
6c5b2ebe 13041 struct program_space *search_pspace)
55aa24fb
SDJ
13042{
13043 /* We use the same method for breakpoint on probes. */
6c5b2ebe 13044 return bkpt_probe_decode_location (b, location, search_pspace);
55aa24fb
SDJ
13045}
13046
5c2b4418
HZ
13047/* Dprintf breakpoint_ops methods. */
13048
13049static void
13050dprintf_re_set (struct breakpoint *b)
13051{
13052 breakpoint_re_set_default (b);
13053
f00aae0f
KS
13054 /* extra_string should never be non-NULL for dprintf. */
13055 gdb_assert (b->extra_string != NULL);
5c2b4418
HZ
13056
13057 /* 1 - connect to target 1, that can run breakpoint commands.
13058 2 - create a dprintf, which resolves fine.
13059 3 - disconnect from target 1
13060 4 - connect to target 2, that can NOT run breakpoint commands.
13061
13062 After steps #3/#4, you'll want the dprintf command list to
13063 be updated, because target 1 and 2 may well return different
13064 answers for target_can_run_breakpoint_commands().
13065 Given absence of finer grained resetting, we get to do
13066 it all the time. */
13067 if (b->extra_string != NULL)
13068 update_dprintf_command_list (b);
13069}
13070
2d9442cc
HZ
13071/* Implement the "print_recreate" breakpoint_ops method for dprintf. */
13072
13073static void
13074dprintf_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13075{
f00aae0f 13076 fprintf_unfiltered (fp, "dprintf %s,%s",
d28cd78a 13077 event_location_to_string (tp->location.get ()),
2d9442cc
HZ
13078 tp->extra_string);
13079 print_recreate_thread (tp, fp);
13080}
13081
9d6e6e84
HZ
13082/* Implement the "after_condition_true" breakpoint_ops method for
13083 dprintf.
13084
13085 dprintf's are implemented with regular commands in their command
13086 list, but we run the commands here instead of before presenting the
13087 stop to the user, as dprintf's don't actually cause a stop. This
13088 also makes it so that the commands of multiple dprintfs at the same
13089 address are all handled. */
13090
13091static void
13092dprintf_after_condition_true (struct bpstats *bs)
13093{
04afa70c 13094 struct bpstats tmp_bs;
9d6e6e84
HZ
13095 struct bpstats *tmp_bs_p = &tmp_bs;
13096
13097 /* dprintf's never cause a stop. This wasn't set in the
13098 check_status hook instead because that would make the dprintf's
13099 condition not be evaluated. */
13100 bs->stop = 0;
13101
13102 /* Run the command list here. Take ownership of it instead of
13103 copying. We never want these commands to run later in
13104 bpstat_do_actions, if a breakpoint that causes a stop happens to
13105 be set at same address as this dprintf, or even if running the
13106 commands here throws. */
13107 tmp_bs.commands = bs->commands;
13108 bs->commands = NULL;
9d6e6e84
HZ
13109
13110 bpstat_do_actions_1 (&tmp_bs_p);
13111
13112 /* 'tmp_bs.commands' will usually be NULL by now, but
13113 bpstat_do_actions_1 may return early without processing the whole
13114 list. */
9d6e6e84
HZ
13115}
13116
983af33b
SDJ
13117/* The breakpoint_ops structure to be used on static tracepoints with
13118 markers (`-m'). */
13119
13120static void
626d2320 13121strace_marker_create_sals_from_location (struct event_location *location,
5f700d83 13122 struct linespec_result *canonical,
f00aae0f 13123 enum bptype type_wanted)
983af33b
SDJ
13124{
13125 struct linespec_sals lsal;
f00aae0f 13126 const char *arg_start, *arg;
983af33b 13127
a20714ff 13128 arg = arg_start = get_linespec_location (location)->spec_string;
f00aae0f 13129 lsal.sals = decode_static_tracepoint_spec (&arg);
983af33b 13130
f2fc3015
TT
13131 std::string str (arg_start, arg - arg_start);
13132 const char *ptr = str.c_str ();
a20714ff
PA
13133 canonical->location
13134 = new_linespec_location (&ptr, symbol_name_match_type::FULL);
983af33b 13135
8e9e35b1
TT
13136 lsal.canonical
13137 = xstrdup (event_location_to_string (canonical->location.get ()));
6c5b2ebe 13138 canonical->lsals.push_back (std::move (lsal));
983af33b
SDJ
13139}
13140
13141static void
13142strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
13143 struct linespec_result *canonical,
e1e01040
PA
13144 gdb::unique_xmalloc_ptr<char> cond_string,
13145 gdb::unique_xmalloc_ptr<char> extra_string,
983af33b
SDJ
13146 enum bptype type_wanted,
13147 enum bpdisp disposition,
13148 int thread,
13149 int task, int ignore_count,
13150 const struct breakpoint_ops *ops,
13151 int from_tty, int enabled,
44f238bb 13152 int internal, unsigned flags)
983af33b 13153{
6c5b2ebe 13154 const linespec_sals &lsal = canonical->lsals[0];
983af33b
SDJ
13155
13156 /* If the user is creating a static tracepoint by marker id
13157 (strace -m MARKER_ID), then store the sals index, so that
13158 breakpoint_re_set can try to match up which of the newly
13159 found markers corresponds to this one, and, don't try to
13160 expand multiple locations for each sal, given than SALS
13161 already should contain all sals for MARKER_ID. */
13162
6c5b2ebe 13163 for (size_t i = 0; i < lsal.sals.size (); i++)
983af33b 13164 {
6c5b2ebe
PA
13165 event_location_up location
13166 = copy_event_location (canonical->location.get ());
983af33b 13167
b270e6f9 13168 std::unique_ptr<tracepoint> tp (new tracepoint ());
6c5b2ebe 13169 init_breakpoint_sal (tp.get (), gdbarch, lsal.sals[i],
ffc2605c 13170 std::move (location), NULL,
e1e01040
PA
13171 std::move (cond_string),
13172 std::move (extra_string),
e7e0cddf 13173 type_wanted, disposition,
983af33b 13174 thread, task, ignore_count, ops,
44f238bb 13175 from_tty, enabled, internal, flags,
983af33b
SDJ
13176 canonical->special_display);
13177 /* Given that its possible to have multiple markers with
13178 the same string id, if the user is creating a static
13179 tracepoint by marker id ("strace -m MARKER_ID"), then
13180 store the sals index, so that breakpoint_re_set can
13181 try to match up which of the newly found markers
13182 corresponds to this one */
13183 tp->static_trace_marker_id_idx = i;
13184
b270e6f9 13185 install_breakpoint (internal, std::move (tp), 0);
983af33b
SDJ
13186 }
13187}
13188
6c5b2ebe 13189static std::vector<symtab_and_line>
f00aae0f 13190strace_marker_decode_location (struct breakpoint *b,
626d2320 13191 struct event_location *location,
6c5b2ebe 13192 struct program_space *search_pspace)
983af33b
SDJ
13193{
13194 struct tracepoint *tp = (struct tracepoint *) b;
a20714ff 13195 const char *s = get_linespec_location (location)->spec_string;
983af33b 13196
6c5b2ebe
PA
13197 std::vector<symtab_and_line> sals = decode_static_tracepoint_spec (&s);
13198 if (sals.size () > tp->static_trace_marker_id_idx)
983af33b 13199 {
6c5b2ebe
PA
13200 sals[0] = sals[tp->static_trace_marker_id_idx];
13201 sals.resize (1);
13202 return sals;
983af33b
SDJ
13203 }
13204 else
5d9310c4 13205 error (_("marker %s not found"), tp->static_trace_marker_id.c_str ());
983af33b
SDJ
13206}
13207
13208static struct breakpoint_ops strace_marker_breakpoint_ops;
13209
13210static int
13211strace_marker_p (struct breakpoint *b)
13212{
13213 return b->ops == &strace_marker_breakpoint_ops;
13214}
13215
53a5351d 13216/* Delete a breakpoint and clean up all traces of it in the data
f431efe5 13217 structures. */
c906108c
SS
13218
13219void
fba45db2 13220delete_breakpoint (struct breakpoint *bpt)
c906108c 13221{
52f0bd74 13222 struct breakpoint *b;
c906108c 13223
8a3fe4f8 13224 gdb_assert (bpt != NULL);
c906108c 13225
4a64f543
MS
13226 /* Has this bp already been deleted? This can happen because
13227 multiple lists can hold pointers to bp's. bpstat lists are
13228 especial culprits.
13229
13230 One example of this happening is a watchpoint's scope bp. When
13231 the scope bp triggers, we notice that the watchpoint is out of
13232 scope, and delete it. We also delete its scope bp. But the
13233 scope bp is marked "auto-deleting", and is already on a bpstat.
13234 That bpstat is then checked for auto-deleting bp's, which are
13235 deleted.
13236
13237 A real solution to this problem might involve reference counts in
13238 bp's, and/or giving them pointers back to their referencing
13239 bpstat's, and teaching delete_breakpoint to only free a bp's
13240 storage when no more references were extent. A cheaper bandaid
13241 was chosen. */
c906108c
SS
13242 if (bpt->type == bp_none)
13243 return;
13244
4a64f543
MS
13245 /* At least avoid this stale reference until the reference counting
13246 of breakpoints gets resolved. */
d0fb5eae 13247 if (bpt->related_breakpoint != bpt)
e5a0a904 13248 {
d0fb5eae 13249 struct breakpoint *related;
3a5c3e22 13250 struct watchpoint *w;
d0fb5eae
JK
13251
13252 if (bpt->type == bp_watchpoint_scope)
3a5c3e22 13253 w = (struct watchpoint *) bpt->related_breakpoint;
d0fb5eae 13254 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
3a5c3e22
PA
13255 w = (struct watchpoint *) bpt;
13256 else
13257 w = NULL;
13258 if (w != NULL)
13259 watchpoint_del_at_next_stop (w);
d0fb5eae
JK
13260
13261 /* Unlink bpt from the bpt->related_breakpoint ring. */
13262 for (related = bpt; related->related_breakpoint != bpt;
13263 related = related->related_breakpoint);
13264 related->related_breakpoint = bpt->related_breakpoint;
13265 bpt->related_breakpoint = bpt;
e5a0a904
JK
13266 }
13267
a9634178
TJB
13268 /* watch_command_1 creates a watchpoint but only sets its number if
13269 update_watchpoint succeeds in creating its bp_locations. If there's
13270 a problem in that process, we'll be asked to delete the half-created
13271 watchpoint. In that case, don't announce the deletion. */
13272 if (bpt->number)
76727919 13273 gdb::observers::breakpoint_deleted.notify (bpt);
c906108c 13274
c906108c
SS
13275 if (breakpoint_chain == bpt)
13276 breakpoint_chain = bpt->next;
13277
c906108c
SS
13278 ALL_BREAKPOINTS (b)
13279 if (b->next == bpt)
c5aa993b
JM
13280 {
13281 b->next = bpt->next;
13282 break;
13283 }
c906108c 13284
f431efe5
PA
13285 /* Be sure no bpstat's are pointing at the breakpoint after it's
13286 been freed. */
13287 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
e5dd4106 13288 in all threads for now. Note that we cannot just remove bpstats
f431efe5
PA
13289 pointing at bpt from the stop_bpstat list entirely, as breakpoint
13290 commands are associated with the bpstat; if we remove it here,
13291 then the later call to bpstat_do_actions (&stop_bpstat); in
13292 event-top.c won't do anything, and temporary breakpoints with
13293 commands won't work. */
13294
13295 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
13296
4a64f543
MS
13297 /* Now that breakpoint is removed from breakpoint list, update the
13298 global location list. This will remove locations that used to
13299 belong to this breakpoint. Do this before freeing the breakpoint
13300 itself, since remove_breakpoint looks at location's owner. It
13301 might be better design to have location completely
13302 self-contained, but it's not the case now. */
44702360 13303 update_global_location_list (UGLL_DONT_INSERT);
74960c60 13304
4a64f543
MS
13305 /* On the chance that someone will soon try again to delete this
13306 same bp, we mark it as deleted before freeing its storage. */
c906108c 13307 bpt->type = bp_none;
4d01a485 13308 delete bpt;
c906108c
SS
13309}
13310
51be5b68
PA
13311/* Iterator function to call a user-provided callback function once
13312 for each of B and its related breakpoints. */
13313
13314static void
13315iterate_over_related_breakpoints (struct breakpoint *b,
48649e1b 13316 gdb::function_view<void (breakpoint *)> function)
51be5b68
PA
13317{
13318 struct breakpoint *related;
13319
13320 related = b;
13321 do
13322 {
13323 struct breakpoint *next;
13324
13325 /* FUNCTION may delete RELATED. */
13326 next = related->related_breakpoint;
13327
13328 if (next == related)
13329 {
13330 /* RELATED is the last ring entry. */
48649e1b 13331 function (related);
51be5b68
PA
13332
13333 /* FUNCTION may have deleted it, so we'd never reach back to
13334 B. There's nothing left to do anyway, so just break
13335 out. */
13336 break;
13337 }
13338 else
48649e1b 13339 function (related);
51be5b68
PA
13340
13341 related = next;
13342 }
13343 while (related != b);
13344}
95a42b64 13345
4495129a 13346static void
981a3fb3 13347delete_command (const char *arg, int from_tty)
c906108c 13348{
35df4500 13349 struct breakpoint *b, *b_tmp;
c906108c 13350
ea9365bb
TT
13351 dont_repeat ();
13352
c906108c
SS
13353 if (arg == 0)
13354 {
13355 int breaks_to_delete = 0;
13356
46c6471b
PA
13357 /* Delete all breakpoints if no argument. Do not delete
13358 internal breakpoints, these have to be deleted with an
13359 explicit breakpoint number argument. */
c5aa993b 13360 ALL_BREAKPOINTS (b)
46c6471b 13361 if (user_breakpoint_p (b))
973d738b
DJ
13362 {
13363 breaks_to_delete = 1;
13364 break;
13365 }
c906108c
SS
13366
13367 /* Ask user only if there are some breakpoints to delete. */
13368 if (!from_tty
e2e0b3e5 13369 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
c906108c 13370 {
35df4500 13371 ALL_BREAKPOINTS_SAFE (b, b_tmp)
46c6471b 13372 if (user_breakpoint_p (b))
c5aa993b 13373 delete_breakpoint (b);
c906108c
SS
13374 }
13375 }
13376 else
48649e1b 13377 map_breakpoint_numbers
b926417a 13378 (arg, [&] (breakpoint *br)
48649e1b 13379 {
b926417a 13380 iterate_over_related_breakpoints (br, delete_breakpoint);
48649e1b 13381 });
c906108c
SS
13382}
13383
c2f4122d
PA
13384/* Return true if all locations of B bound to PSPACE are pending. If
13385 PSPACE is NULL, all locations of all program spaces are
13386 considered. */
13387
0d381245 13388static int
c2f4122d 13389all_locations_are_pending (struct breakpoint *b, struct program_space *pspace)
fe3f5fa8 13390{
c2f4122d
PA
13391 struct bp_location *loc;
13392
13393 for (loc = b->loc; loc != NULL; loc = loc->next)
13394 if ((pspace == NULL
13395 || loc->pspace == pspace)
13396 && !loc->shlib_disabled
8645ff69 13397 && !loc->pspace->executing_startup)
0d381245
VP
13398 return 0;
13399 return 1;
fe3f5fa8
VP
13400}
13401
776592bf
DE
13402/* Subroutine of update_breakpoint_locations to simplify it.
13403 Return non-zero if multiple fns in list LOC have the same name.
13404 Null names are ignored. */
13405
13406static int
13407ambiguous_names_p (struct bp_location *loc)
13408{
13409 struct bp_location *l;
c1fb9836
TT
13410 htab_up htab (htab_create_alloc (13, htab_hash_string, streq_hash, NULL,
13411 xcalloc, xfree));
776592bf
DE
13412
13413 for (l = loc; l != NULL; l = l->next)
13414 {
13415 const char **slot;
13416 const char *name = l->function_name;
13417
13418 /* Allow for some names to be NULL, ignore them. */
13419 if (name == NULL)
13420 continue;
13421
c1fb9836 13422 slot = (const char **) htab_find_slot (htab.get (), (const void *) name,
776592bf 13423 INSERT);
4a64f543
MS
13424 /* NOTE: We can assume slot != NULL here because xcalloc never
13425 returns NULL. */
776592bf 13426 if (*slot != NULL)
c1fb9836 13427 return 1;
776592bf
DE
13428 *slot = name;
13429 }
13430
776592bf
DE
13431 return 0;
13432}
13433
0fb4aa4b
PA
13434/* When symbols change, it probably means the sources changed as well,
13435 and it might mean the static tracepoint markers are no longer at
13436 the same address or line numbers they used to be at last we
13437 checked. Losing your static tracepoints whenever you rebuild is
13438 undesirable. This function tries to resync/rematch gdb static
13439 tracepoints with the markers on the target, for static tracepoints
13440 that have not been set by marker id. Static tracepoint that have
13441 been set by marker id are reset by marker id in breakpoint_re_set.
13442 The heuristic is:
13443
13444 1) For a tracepoint set at a specific address, look for a marker at
13445 the old PC. If one is found there, assume to be the same marker.
13446 If the name / string id of the marker found is different from the
13447 previous known name, assume that means the user renamed the marker
13448 in the sources, and output a warning.
13449
13450 2) For a tracepoint set at a given line number, look for a marker
13451 at the new address of the old line number. If one is found there,
13452 assume to be the same marker. If the name / string id of the
13453 marker found is different from the previous known name, assume that
13454 means the user renamed the marker in the sources, and output a
13455 warning.
13456
13457 3) If a marker is no longer found at the same address or line, it
13458 may mean the marker no longer exists. But it may also just mean
13459 the code changed a bit. Maybe the user added a few lines of code
13460 that made the marker move up or down (in line number terms). Ask
13461 the target for info about the marker with the string id as we knew
13462 it. If found, update line number and address in the matching
13463 static tracepoint. This will get confused if there's more than one
13464 marker with the same ID (possible in UST, although unadvised
13465 precisely because it confuses tools). */
13466
13467static struct symtab_and_line
13468update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
13469{
d9b3f62e 13470 struct tracepoint *tp = (struct tracepoint *) b;
0fb4aa4b
PA
13471 struct static_tracepoint_marker marker;
13472 CORE_ADDR pc;
0fb4aa4b
PA
13473
13474 pc = sal.pc;
13475 if (sal.line)
13476 find_line_pc (sal.symtab, sal.line, &pc);
13477
13478 if (target_static_tracepoint_marker_at (pc, &marker))
13479 {
5d9310c4 13480 if (tp->static_trace_marker_id != marker.str_id)
0fb4aa4b 13481 warning (_("static tracepoint %d changed probed marker from %s to %s"),
5d9310c4
SM
13482 b->number, tp->static_trace_marker_id.c_str (),
13483 marker.str_id.c_str ());
0fb4aa4b 13484
5d9310c4 13485 tp->static_trace_marker_id = std::move (marker.str_id);
0fb4aa4b
PA
13486
13487 return sal;
13488 }
13489
13490 /* Old marker wasn't found on target at lineno. Try looking it up
13491 by string ID. */
13492 if (!sal.explicit_pc
13493 && sal.line != 0
13494 && sal.symtab != NULL
5d9310c4 13495 && !tp->static_trace_marker_id.empty ())
0fb4aa4b 13496 {
5d9310c4
SM
13497 std::vector<static_tracepoint_marker> markers
13498 = target_static_tracepoint_markers_by_strid
13499 (tp->static_trace_marker_id.c_str ());
0fb4aa4b 13500
5d9310c4 13501 if (!markers.empty ())
0fb4aa4b 13502 {
0fb4aa4b 13503 struct symbol *sym;
80e1d417 13504 struct static_tracepoint_marker *tpmarker;
79a45e25 13505 struct ui_out *uiout = current_uiout;
67994074 13506 struct explicit_location explicit_loc;
0fb4aa4b 13507
5d9310c4 13508 tpmarker = &markers[0];
0fb4aa4b 13509
5d9310c4 13510 tp->static_trace_marker_id = std::move (tpmarker->str_id);
0fb4aa4b
PA
13511
13512 warning (_("marker for static tracepoint %d (%s) not "
13513 "found at previous line number"),
5d9310c4 13514 b->number, tp->static_trace_marker_id.c_str ());
0fb4aa4b 13515
51abb421 13516 symtab_and_line sal2 = find_pc_line (tpmarker->address, 0);
80e1d417 13517 sym = find_pc_sect_function (tpmarker->address, NULL);
112e8700 13518 uiout->text ("Now in ");
0fb4aa4b
PA
13519 if (sym)
13520 {
987012b8 13521 uiout->field_string ("func", sym->print_name (),
e43b10e1 13522 function_name_style.style ());
112e8700 13523 uiout->text (" at ");
0fb4aa4b 13524 }
112e8700 13525 uiout->field_string ("file",
cbe56571 13526 symtab_to_filename_for_display (sal2.symtab),
e43b10e1 13527 file_name_style.style ());
112e8700 13528 uiout->text (":");
0fb4aa4b 13529
112e8700 13530 if (uiout->is_mi_like_p ())
0fb4aa4b 13531 {
0b0865da 13532 const char *fullname = symtab_to_fullname (sal2.symtab);
0fb4aa4b 13533
112e8700 13534 uiout->field_string ("fullname", fullname);
0fb4aa4b
PA
13535 }
13536
381befee 13537 uiout->field_signed ("line", sal2.line);
112e8700 13538 uiout->text ("\n");
0fb4aa4b 13539
80e1d417 13540 b->loc->line_number = sal2.line;
2f202fde 13541 b->loc->symtab = sym != NULL ? sal2.symtab : NULL;
0fb4aa4b 13542
d28cd78a 13543 b->location.reset (NULL);
67994074
KS
13544 initialize_explicit_location (&explicit_loc);
13545 explicit_loc.source_filename
00e52e53 13546 = ASTRDUP (symtab_to_filename_for_display (sal2.symtab));
67994074
KS
13547 explicit_loc.line_offset.offset = b->loc->line_number;
13548 explicit_loc.line_offset.sign = LINE_OFFSET_NONE;
d28cd78a 13549 b->location = new_explicit_location (&explicit_loc);
0fb4aa4b
PA
13550
13551 /* Might be nice to check if function changed, and warn if
13552 so. */
0fb4aa4b
PA
13553 }
13554 }
13555 return sal;
13556}
13557
8d3788bd
VP
13558/* Returns 1 iff locations A and B are sufficiently same that
13559 we don't need to report breakpoint as changed. */
13560
13561static int
13562locations_are_equal (struct bp_location *a, struct bp_location *b)
13563{
13564 while (a && b)
13565 {
13566 if (a->address != b->address)
13567 return 0;
13568
13569 if (a->shlib_disabled != b->shlib_disabled)
13570 return 0;
13571
13572 if (a->enabled != b->enabled)
13573 return 0;
13574
b5fa468f
TBA
13575 if (a->disabled_by_cond != b->disabled_by_cond)
13576 return 0;
13577
8d3788bd
VP
13578 a = a->next;
13579 b = b->next;
13580 }
13581
13582 if ((a == NULL) != (b == NULL))
13583 return 0;
13584
13585 return 1;
13586}
13587
c2f4122d
PA
13588/* Split all locations of B that are bound to PSPACE out of B's
13589 location list to a separate list and return that list's head. If
13590 PSPACE is NULL, hoist out all locations of B. */
13591
13592static struct bp_location *
13593hoist_existing_locations (struct breakpoint *b, struct program_space *pspace)
13594{
13595 struct bp_location head;
13596 struct bp_location *i = b->loc;
13597 struct bp_location **i_link = &b->loc;
13598 struct bp_location *hoisted = &head;
13599
13600 if (pspace == NULL)
13601 {
13602 i = b->loc;
13603 b->loc = NULL;
13604 return i;
13605 }
13606
13607 head.next = NULL;
13608
13609 while (i != NULL)
13610 {
13611 if (i->pspace == pspace)
13612 {
13613 *i_link = i->next;
13614 i->next = NULL;
13615 hoisted->next = i;
13616 hoisted = i;
13617 }
13618 else
13619 i_link = &i->next;
13620 i = *i_link;
13621 }
13622
13623 return head.next;
13624}
13625
13626/* Create new breakpoint locations for B (a hardware or software
13627 breakpoint) based on SALS and SALS_END. If SALS_END.NELTS is not
13628 zero, then B is a ranged breakpoint. Only recreates locations for
13629 FILTER_PSPACE. Locations of other program spaces are left
13630 untouched. */
f1310107 13631
0e30163f 13632void
0d381245 13633update_breakpoint_locations (struct breakpoint *b,
c2f4122d 13634 struct program_space *filter_pspace,
6c5b2ebe
PA
13635 gdb::array_view<const symtab_and_line> sals,
13636 gdb::array_view<const symtab_and_line> sals_end)
fe3f5fa8 13637{
c2f4122d 13638 struct bp_location *existing_locations;
0d381245 13639
6c5b2ebe 13640 if (!sals_end.empty () && (sals.size () != 1 || sals_end.size () != 1))
f8eba3c6
TT
13641 {
13642 /* Ranged breakpoints have only one start location and one end
13643 location. */
13644 b->enable_state = bp_disabled;
f8eba3c6
TT
13645 printf_unfiltered (_("Could not reset ranged breakpoint %d: "
13646 "multiple locations found\n"),
13647 b->number);
13648 return;
13649 }
f1310107 13650
4a64f543
MS
13651 /* If there's no new locations, and all existing locations are
13652 pending, don't do anything. This optimizes the common case where
13653 all locations are in the same shared library, that was unloaded.
13654 We'd like to retain the location, so that when the library is
13655 loaded again, we don't loose the enabled/disabled status of the
13656 individual locations. */
6c5b2ebe 13657 if (all_locations_are_pending (b, filter_pspace) && sals.empty ())
fe3f5fa8
VP
13658 return;
13659
c2f4122d 13660 existing_locations = hoist_existing_locations (b, filter_pspace);
fe3f5fa8 13661
6c5b2ebe 13662 for (const auto &sal : sals)
fe3f5fa8 13663 {
f8eba3c6
TT
13664 struct bp_location *new_loc;
13665
6c5b2ebe 13666 switch_to_program_space_and_thread (sal.pspace);
f8eba3c6 13667
6c5b2ebe 13668 new_loc = add_location_to_breakpoint (b, &sal);
fe3f5fa8 13669
0d381245
VP
13670 /* Reparse conditions, they might contain references to the
13671 old symtab. */
13672 if (b->cond_string != NULL)
13673 {
bbc13ae3 13674 const char *s;
fe3f5fa8 13675
0d381245 13676 s = b->cond_string;
a70b8144 13677 try
0d381245 13678 {
6c5b2ebe
PA
13679 new_loc->cond = parse_exp_1 (&s, sal.pc,
13680 block_for_pc (sal.pc),
0d381245
VP
13681 0);
13682 }
230d2906 13683 catch (const gdb_exception_error &e)
0d381245 13684 {
b5fa468f 13685 new_loc->disabled_by_cond = true;
0d381245
VP
13686 }
13687 }
fe3f5fa8 13688
6c5b2ebe 13689 if (!sals_end.empty ())
f1310107 13690 {
6c5b2ebe 13691 CORE_ADDR end = find_breakpoint_range_end (sals_end[0]);
f1310107 13692
6c5b2ebe 13693 new_loc->length = end - sals[0].pc + 1;
f1310107 13694 }
0d381245 13695 }
fe3f5fa8 13696
4a64f543
MS
13697 /* If possible, carry over 'disable' status from existing
13698 breakpoints. */
0d381245
VP
13699 {
13700 struct bp_location *e = existing_locations;
776592bf
DE
13701 /* If there are multiple breakpoints with the same function name,
13702 e.g. for inline functions, comparing function names won't work.
13703 Instead compare pc addresses; this is just a heuristic as things
13704 may have moved, but in practice it gives the correct answer
13705 often enough until a better solution is found. */
13706 int have_ambiguous_names = ambiguous_names_p (b->loc);
13707
0d381245
VP
13708 for (; e; e = e->next)
13709 {
b5fa468f 13710 if ((!e->enabled || e->disabled_by_cond) && e->function_name)
0d381245
VP
13711 {
13712 struct bp_location *l = b->loc;
776592bf
DE
13713 if (have_ambiguous_names)
13714 {
13715 for (; l; l = l->next)
7f32a4d5
PA
13716 {
13717 /* Ignore software vs hardware location type at
13718 this point, because with "set breakpoint
13719 auto-hw", after a re-set, locations that were
13720 hardware can end up as software, or vice versa.
13721 As mentioned above, this is an heuristic and in
13722 practice should give the correct answer often
13723 enough. */
13724 if (breakpoint_locations_match (e, l, true))
13725 {
b5fa468f
TBA
13726 l->enabled = e->enabled;
13727 l->disabled_by_cond = e->disabled_by_cond;
7f32a4d5
PA
13728 break;
13729 }
13730 }
776592bf
DE
13731 }
13732 else
13733 {
13734 for (; l; l = l->next)
13735 if (l->function_name
13736 && strcmp (e->function_name, l->function_name) == 0)
13737 {
b5fa468f
TBA
13738 l->enabled = e->enabled;
13739 l->disabled_by_cond = e->disabled_by_cond;
776592bf
DE
13740 break;
13741 }
13742 }
0d381245
VP
13743 }
13744 }
13745 }
fe3f5fa8 13746
8d3788bd 13747 if (!locations_are_equal (existing_locations, b->loc))
76727919 13748 gdb::observers::breakpoint_modified.notify (b);
fe3f5fa8
VP
13749}
13750
f00aae0f 13751/* Find the SaL locations corresponding to the given LOCATION.
ef23e705
TJB
13752 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
13753
6c5b2ebe 13754static std::vector<symtab_and_line>
f00aae0f 13755location_to_sals (struct breakpoint *b, struct event_location *location,
c2f4122d 13756 struct program_space *search_pspace, int *found)
ef23e705 13757{
cc06b668 13758 struct gdb_exception exception;
ef23e705 13759
983af33b 13760 gdb_assert (b->ops != NULL);
ef23e705 13761
6c5b2ebe
PA
13762 std::vector<symtab_and_line> sals;
13763
a70b8144 13764 try
ef23e705 13765 {
6c5b2ebe 13766 sals = b->ops->decode_location (b, location, search_pspace);
ef23e705 13767 }
94aeb44b 13768 catch (gdb_exception_error &e)
ef23e705
TJB
13769 {
13770 int not_found_and_ok = 0;
492d29ea 13771
ef23e705
TJB
13772 /* For pending breakpoints, it's expected that parsing will
13773 fail until the right shared library is loaded. User has
13774 already told to create pending breakpoints and don't need
13775 extra messages. If breakpoint is in bp_shlib_disabled
13776 state, then user already saw the message about that
13777 breakpoint being disabled, and don't want to see more
13778 errors. */
58438ac1 13779 if (e.error == NOT_FOUND_ERROR
c2f4122d
PA
13780 && (b->condition_not_parsed
13781 || (b->loc != NULL
13782 && search_pspace != NULL
13783 && b->loc->pspace != search_pspace)
ef23e705 13784 || (b->loc && b->loc->shlib_disabled)
f8eba3c6 13785 || (b->loc && b->loc->pspace->executing_startup)
ef23e705
TJB
13786 || b->enable_state == bp_disabled))
13787 not_found_and_ok = 1;
13788
13789 if (!not_found_and_ok)
13790 {
13791 /* We surely don't want to warn about the same breakpoint
13792 10 times. One solution, implemented here, is disable
13793 the breakpoint on error. Another solution would be to
13794 have separate 'warning emitted' flag. Since this
13795 happens only when a binary has changed, I don't know
13796 which approach is better. */
13797 b->enable_state = bp_disabled;
eedc3f4f 13798 throw;
ef23e705 13799 }
94aeb44b
TT
13800
13801 exception = std::move (e);
ef23e705
TJB
13802 }
13803
492d29ea 13804 if (exception.reason == 0 || exception.error != NOT_FOUND_ERROR)
ef23e705 13805 {
6c5b2ebe
PA
13806 for (auto &sal : sals)
13807 resolve_sal_pc (&sal);
f00aae0f 13808 if (b->condition_not_parsed && b->extra_string != NULL)
ef23e705 13809 {
ed1d1739
KS
13810 char *cond_string, *extra_string;
13811 int thread, task;
ef23e705 13812
b5fa468f
TBA
13813 find_condition_and_thread_for_sals (sals, b->extra_string,
13814 &cond_string, &thread,
13815 &task, &extra_string);
f00aae0f 13816 gdb_assert (b->cond_string == NULL);
ef23e705
TJB
13817 if (cond_string)
13818 b->cond_string = cond_string;
13819 b->thread = thread;
13820 b->task = task;
e7e0cddf 13821 if (extra_string)
f00aae0f
KS
13822 {
13823 xfree (b->extra_string);
13824 b->extra_string = extra_string;
13825 }
ef23e705
TJB
13826 b->condition_not_parsed = 0;
13827 }
13828
983af33b 13829 if (b->type == bp_static_tracepoint && !strace_marker_p (b))
6c5b2ebe 13830 sals[0] = update_static_tracepoint (b, sals[0]);
ef23e705 13831
58438ac1
TT
13832 *found = 1;
13833 }
13834 else
13835 *found = 0;
ef23e705
TJB
13836
13837 return sals;
13838}
13839
348d480f
PA
13840/* The default re_set method, for typical hardware or software
13841 breakpoints. Reevaluate the breakpoint and recreate its
13842 locations. */
13843
13844static void
28010a5d 13845breakpoint_re_set_default (struct breakpoint *b)
ef23e705 13846{
c2f4122d 13847 struct program_space *filter_pspace = current_program_space;
6c5b2ebe 13848 std::vector<symtab_and_line> expanded, expanded_end;
ef23e705 13849
6c5b2ebe
PA
13850 int found;
13851 std::vector<symtab_and_line> sals = location_to_sals (b, b->location.get (),
13852 filter_pspace, &found);
ef23e705 13853 if (found)
6c5b2ebe 13854 expanded = std::move (sals);
ef23e705 13855
f00aae0f 13856 if (b->location_range_end != NULL)
f1310107 13857 {
6c5b2ebe
PA
13858 std::vector<symtab_and_line> sals_end
13859 = location_to_sals (b, b->location_range_end.get (),
13860 filter_pspace, &found);
f1310107 13861 if (found)
6c5b2ebe 13862 expanded_end = std::move (sals_end);
f1310107
TJB
13863 }
13864
c2f4122d 13865 update_breakpoint_locations (b, filter_pspace, expanded, expanded_end);
28010a5d
PA
13866}
13867
983af33b
SDJ
13868/* Default method for creating SALs from an address string. It basically
13869 calls parse_breakpoint_sals. Return 1 for success, zero for failure. */
13870
13871static void
626d2320 13872create_sals_from_location_default (struct event_location *location,
f00aae0f
KS
13873 struct linespec_result *canonical,
13874 enum bptype type_wanted)
983af33b 13875{
f00aae0f 13876 parse_breakpoint_sals (location, canonical);
983af33b
SDJ
13877}
13878
13879/* Call create_breakpoints_sal for the given arguments. This is the default
13880 function for the `create_breakpoints_sal' method of
13881 breakpoint_ops. */
13882
13883static void
13884create_breakpoints_sal_default (struct gdbarch *gdbarch,
13885 struct linespec_result *canonical,
e1e01040
PA
13886 gdb::unique_xmalloc_ptr<char> cond_string,
13887 gdb::unique_xmalloc_ptr<char> extra_string,
983af33b
SDJ
13888 enum bptype type_wanted,
13889 enum bpdisp disposition,
13890 int thread,
13891 int task, int ignore_count,
13892 const struct breakpoint_ops *ops,
13893 int from_tty, int enabled,
44f238bb 13894 int internal, unsigned flags)
983af33b 13895{
e1e01040
PA
13896 create_breakpoints_sal (gdbarch, canonical,
13897 std::move (cond_string),
13898 std::move (extra_string),
983af33b
SDJ
13899 type_wanted, disposition,
13900 thread, task, ignore_count, ops, from_tty,
44f238bb 13901 enabled, internal, flags);
983af33b
SDJ
13902}
13903
13904/* Decode the line represented by S by calling decode_line_full. This is the
5f700d83 13905 default function for the `decode_location' method of breakpoint_ops. */
983af33b 13906
6c5b2ebe 13907static std::vector<symtab_and_line>
f00aae0f 13908decode_location_default (struct breakpoint *b,
626d2320 13909 struct event_location *location,
6c5b2ebe 13910 struct program_space *search_pspace)
983af33b
SDJ
13911{
13912 struct linespec_result canonical;
13913
c2f4122d 13914 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, search_pspace,
cafb3438 13915 NULL, 0, &canonical, multiple_symbols_all,
c0e8dcd8 13916 b->filter.get ());
983af33b
SDJ
13917
13918 /* We should get 0 or 1 resulting SALs. */
6c5b2ebe 13919 gdb_assert (canonical.lsals.size () < 2);
983af33b 13920
6c5b2ebe 13921 if (!canonical.lsals.empty ())
983af33b 13922 {
6c5b2ebe
PA
13923 const linespec_sals &lsal = canonical.lsals[0];
13924 return std::move (lsal.sals);
983af33b 13925 }
6c5b2ebe 13926 return {};
983af33b
SDJ
13927}
13928
bf469271 13929/* Reset a breakpoint. */
c906108c 13930
bf469271
PA
13931static void
13932breakpoint_re_set_one (breakpoint *b)
c906108c 13933{
fdf44873
TT
13934 input_radix = b->input_radix;
13935 set_language (b->language);
c906108c 13936
348d480f 13937 b->ops->re_set (b);
c906108c
SS
13938}
13939
c2f4122d
PA
13940/* Re-set breakpoint locations for the current program space.
13941 Locations bound to other program spaces are left untouched. */
13942
c906108c 13943void
69de3c6a 13944breakpoint_re_set (void)
c906108c 13945{
35df4500 13946 struct breakpoint *b, *b_tmp;
2a7f3dff 13947
c5aa993b 13948 {
fdf44873
TT
13949 scoped_restore_current_language save_language;
13950 scoped_restore save_input_radix = make_scoped_restore (&input_radix);
5ed8105e 13951 scoped_restore_current_pspace_and_thread restore_pspace_thread;
e62c965a 13952
8e817061
JB
13953 /* breakpoint_re_set_one sets the current_language to the language
13954 of the breakpoint it is resetting (see prepare_re_set_context)
13955 before re-evaluating the breakpoint's location. This change can
13956 unfortunately get undone by accident if the language_mode is set
13957 to auto, and we either switch frames, or more likely in this context,
13958 we select the current frame.
13959
13960 We prevent this by temporarily turning the language_mode to
13961 language_mode_manual. We restore it once all breakpoints
13962 have been reset. */
13963 scoped_restore save_language_mode = make_scoped_restore (&language_mode);
13964 language_mode = language_mode_manual;
13965
5ed8105e
PA
13966 /* Note: we must not try to insert locations until after all
13967 breakpoints have been re-set. Otherwise, e.g., when re-setting
13968 breakpoint 1, we'd insert the locations of breakpoint 2, which
13969 hadn't been re-set yet, and thus may have stale locations. */
4efc6507 13970
5ed8105e
PA
13971 ALL_BREAKPOINTS_SAFE (b, b_tmp)
13972 {
a70b8144 13973 try
bf469271
PA
13974 {
13975 breakpoint_re_set_one (b);
13976 }
230d2906 13977 catch (const gdb_exception &ex)
bf469271
PA
13978 {
13979 exception_fprintf (gdb_stderr, ex,
13980 "Error in re-setting breakpoint %d: ",
13981 b->number);
13982 }
5ed8105e 13983 }
5ed8105e
PA
13984
13985 jit_breakpoint_re_set ();
13986 }
6c95b8df 13987
af02033e
PP
13988 create_overlay_event_breakpoint ();
13989 create_longjmp_master_breakpoint ();
13990 create_std_terminate_master_breakpoint ();
186c406b 13991 create_exception_master_breakpoint ();
2a7f3dff
PA
13992
13993 /* Now we can insert. */
13994 update_global_location_list (UGLL_MAY_INSERT);
c906108c
SS
13995}
13996\f
c906108c
SS
13997/* Reset the thread number of this breakpoint:
13998
13999 - If the breakpoint is for all threads, leave it as-is.
4a64f543 14000 - Else, reset it to the current thread for inferior_ptid. */
c906108c 14001void
fba45db2 14002breakpoint_re_set_thread (struct breakpoint *b)
c906108c
SS
14003{
14004 if (b->thread != -1)
14005 {
00431a78 14006 b->thread = inferior_thread ()->global_num;
6c95b8df
PA
14007
14008 /* We're being called after following a fork. The new fork is
14009 selected as current, and unless this was a vfork will have a
14010 different program space from the original thread. Reset that
14011 as well. */
14012 b->loc->pspace = current_program_space;
c906108c
SS
14013 }
14014}
14015
03ac34d5
MS
14016/* Set ignore-count of breakpoint number BPTNUM to COUNT.
14017 If from_tty is nonzero, it prints a message to that effect,
14018 which ends with a period (no newline). */
14019
c906108c 14020void
fba45db2 14021set_ignore_count (int bptnum, int count, int from_tty)
c906108c 14022{
52f0bd74 14023 struct breakpoint *b;
c906108c
SS
14024
14025 if (count < 0)
14026 count = 0;
14027
14028 ALL_BREAKPOINTS (b)
14029 if (b->number == bptnum)
c5aa993b 14030 {
d77f58be
SS
14031 if (is_tracepoint (b))
14032 {
14033 if (from_tty && count != 0)
14034 printf_filtered (_("Ignore count ignored for tracepoint %d."),
14035 bptnum);
14036 return;
14037 }
14038
c5aa993b 14039 b->ignore_count = count;
221ea385
KS
14040 if (from_tty)
14041 {
14042 if (count == 0)
3e43a32a
MS
14043 printf_filtered (_("Will stop next time "
14044 "breakpoint %d is reached."),
221ea385
KS
14045 bptnum);
14046 else if (count == 1)
a3f17187 14047 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
221ea385
KS
14048 bptnum);
14049 else
3e43a32a
MS
14050 printf_filtered (_("Will ignore next %d "
14051 "crossings of breakpoint %d."),
221ea385
KS
14052 count, bptnum);
14053 }
76727919 14054 gdb::observers::breakpoint_modified.notify (b);
c5aa993b
JM
14055 return;
14056 }
c906108c 14057
8a3fe4f8 14058 error (_("No breakpoint number %d."), bptnum);
c906108c
SS
14059}
14060
c906108c
SS
14061/* Command to set ignore-count of breakpoint N to COUNT. */
14062
14063static void
0b39b52e 14064ignore_command (const char *args, int from_tty)
c906108c 14065{
0b39b52e 14066 const char *p = args;
52f0bd74 14067 int num;
c906108c
SS
14068
14069 if (p == 0)
e2e0b3e5 14070 error_no_arg (_("a breakpoint number"));
c5aa993b 14071
c906108c 14072 num = get_number (&p);
5c44784c 14073 if (num == 0)
8a3fe4f8 14074 error (_("bad breakpoint number: '%s'"), args);
c906108c 14075 if (*p == 0)
8a3fe4f8 14076 error (_("Second argument (specified ignore-count) is missing."));
c906108c
SS
14077
14078 set_ignore_count (num,
14079 longest_to_int (value_as_long (parse_and_eval (p))),
14080 from_tty);
221ea385
KS
14081 if (from_tty)
14082 printf_filtered ("\n");
c906108c
SS
14083}
14084\f
d0fe4701
XR
14085
14086/* Call FUNCTION on each of the breakpoints with numbers in the range
14087 defined by BP_NUM_RANGE (an inclusive range). */
c906108c
SS
14088
14089static void
d0fe4701
XR
14090map_breakpoint_number_range (std::pair<int, int> bp_num_range,
14091 gdb::function_view<void (breakpoint *)> function)
c906108c 14092{
d0fe4701
XR
14093 if (bp_num_range.first == 0)
14094 {
14095 warning (_("bad breakpoint number at or near '%d'"),
14096 bp_num_range.first);
14097 }
14098 else
c906108c 14099 {
d0fe4701 14100 struct breakpoint *b, *tmp;
197f0a60 14101
d0fe4701 14102 for (int i = bp_num_range.first; i <= bp_num_range.second; i++)
5c44784c 14103 {
d0fe4701
XR
14104 bool match = false;
14105
5c44784c 14106 ALL_BREAKPOINTS_SAFE (b, tmp)
d0fe4701 14107 if (b->number == i)
5c44784c 14108 {
bfd28288 14109 match = true;
48649e1b 14110 function (b);
11cf8741 14111 break;
5c44784c 14112 }
bfd28288 14113 if (!match)
d0fe4701 14114 printf_unfiltered (_("No breakpoint number %d.\n"), i);
c5aa993b 14115 }
c906108c
SS
14116 }
14117}
14118
d0fe4701
XR
14119/* Call FUNCTION on each of the breakpoints whose numbers are given in
14120 ARGS. */
14121
14122static void
14123map_breakpoint_numbers (const char *args,
14124 gdb::function_view<void (breakpoint *)> function)
14125{
14126 if (args == NULL || *args == '\0')
14127 error_no_arg (_("one or more breakpoint numbers"));
14128
14129 number_or_range_parser parser (args);
14130
14131 while (!parser.finished ())
14132 {
14133 int num = parser.get_number ();
14134 map_breakpoint_number_range (std::make_pair (num, num), function);
14135 }
14136}
14137
14138/* Return the breakpoint location structure corresponding to the
14139 BP_NUM and LOC_NUM values. */
14140
0d381245 14141static struct bp_location *
d0fe4701 14142find_location_by_number (int bp_num, int loc_num)
0d381245 14143{
0d381245 14144 struct breakpoint *b;
0d381245
VP
14145
14146 ALL_BREAKPOINTS (b)
14147 if (b->number == bp_num)
14148 {
14149 break;
14150 }
14151
14152 if (!b || b->number != bp_num)
d0fe4701 14153 error (_("Bad breakpoint number '%d'"), bp_num);
0d381245 14154
0d381245 14155 if (loc_num == 0)
d0fe4701 14156 error (_("Bad breakpoint location number '%d'"), loc_num);
0d381245 14157
d0fe4701
XR
14158 int n = 0;
14159 for (bp_location *loc = b->loc; loc != NULL; loc = loc->next)
14160 if (++n == loc_num)
14161 return loc;
14162
14163 error (_("Bad breakpoint location number '%d'"), loc_num);
0d381245
VP
14164}
14165
95e95a6d
PA
14166/* Modes of operation for extract_bp_num. */
14167enum class extract_bp_kind
14168{
14169 /* Extracting a breakpoint number. */
14170 bp,
14171
14172 /* Extracting a location number. */
14173 loc,
14174};
14175
14176/* Extract a breakpoint or location number (as determined by KIND)
14177 from the string starting at START. TRAILER is a character which
14178 can be found after the number. If you don't want a trailer, use
14179 '\0'. If END_OUT is not NULL, it is set to point after the parsed
14180 string. This always returns a positive integer. */
14181
14182static int
14183extract_bp_num (extract_bp_kind kind, const char *start,
14184 int trailer, const char **end_out = NULL)
14185{
14186 const char *end = start;
14187 int num = get_number_trailer (&end, trailer);
14188 if (num < 0)
14189 error (kind == extract_bp_kind::bp
14190 ? _("Negative breakpoint number '%.*s'")
14191 : _("Negative breakpoint location number '%.*s'"),
14192 int (end - start), start);
14193 if (num == 0)
14194 error (kind == extract_bp_kind::bp
14195 ? _("Bad breakpoint number '%.*s'")
14196 : _("Bad breakpoint location number '%.*s'"),
14197 int (end - start), start);
14198
14199 if (end_out != NULL)
14200 *end_out = end;
14201 return num;
14202}
14203
14204/* Extract a breakpoint or location range (as determined by KIND) in
14205 the form NUM1-NUM2 stored at &ARG[arg_offset]. Returns a std::pair
14206 representing the (inclusive) range. The returned pair's elements
14207 are always positive integers. */
14208
14209static std::pair<int, int>
14210extract_bp_or_bp_range (extract_bp_kind kind,
14211 const std::string &arg,
14212 std::string::size_type arg_offset)
14213{
14214 std::pair<int, int> range;
14215 const char *bp_loc = &arg[arg_offset];
14216 std::string::size_type dash = arg.find ('-', arg_offset);
14217 if (dash != std::string::npos)
14218 {
14219 /* bp_loc is a range (x-z). */
14220 if (arg.length () == dash + 1)
14221 error (kind == extract_bp_kind::bp
14222 ? _("Bad breakpoint number at or near: '%s'")
14223 : _("Bad breakpoint location number at or near: '%s'"),
14224 bp_loc);
14225
14226 const char *end;
14227 const char *start_first = bp_loc;
14228 const char *start_second = &arg[dash + 1];
14229 range.first = extract_bp_num (kind, start_first, '-');
14230 range.second = extract_bp_num (kind, start_second, '\0', &end);
14231
14232 if (range.first > range.second)
14233 error (kind == extract_bp_kind::bp
14234 ? _("Inverted breakpoint range at '%.*s'")
14235 : _("Inverted breakpoint location range at '%.*s'"),
14236 int (end - start_first), start_first);
14237 }
14238 else
14239 {
14240 /* bp_loc is a single value. */
14241 range.first = extract_bp_num (kind, bp_loc, '\0');
14242 range.second = range.first;
14243 }
14244 return range;
14245}
14246
d0fe4701
XR
14247/* Extract the breakpoint/location range specified by ARG. Returns
14248 the breakpoint range in BP_NUM_RANGE, and the location range in
14249 BP_LOC_RANGE.
14250
14251 ARG may be in any of the following forms:
14252
14253 x where 'x' is a breakpoint number.
14254 x-y where 'x' and 'y' specify a breakpoint numbers range.
14255 x.y where 'x' is a breakpoint number and 'y' a location number.
14256 x.y-z where 'x' is a breakpoint number and 'y' and 'z' specify a
14257 location number range.
14258*/
14259
cc638e86 14260static void
d0fe4701
XR
14261extract_bp_number_and_location (const std::string &arg,
14262 std::pair<int, int> &bp_num_range,
14263 std::pair<int, int> &bp_loc_range)
14264{
14265 std::string::size_type dot = arg.find ('.');
14266
14267 if (dot != std::string::npos)
14268 {
14269 /* Handle 'x.y' and 'x.y-z' cases. */
14270
14271 if (arg.length () == dot + 1 || dot == 0)
95e95a6d 14272 error (_("Bad breakpoint number at or near: '%s'"), arg.c_str ());
d0fe4701 14273
95e95a6d
PA
14274 bp_num_range.first
14275 = extract_bp_num (extract_bp_kind::bp, arg.c_str (), '.');
14276 bp_num_range.second = bp_num_range.first;
d0fe4701 14277
95e95a6d
PA
14278 bp_loc_range = extract_bp_or_bp_range (extract_bp_kind::loc,
14279 arg, dot + 1);
d0fe4701
XR
14280 }
14281 else
14282 {
14283 /* Handle x and x-y cases. */
d0fe4701 14284
95e95a6d 14285 bp_num_range = extract_bp_or_bp_range (extract_bp_kind::bp, arg, 0);
d0fe4701
XR
14286 bp_loc_range.first = 0;
14287 bp_loc_range.second = 0;
14288 }
d0fe4701
XR
14289}
14290
14291/* Enable or disable a breakpoint location BP_NUM.LOC_NUM. ENABLE
14292 specifies whether to enable or disable. */
14293
14294static void
14295enable_disable_bp_num_loc (int bp_num, int loc_num, bool enable)
14296{
14297 struct bp_location *loc = find_location_by_number (bp_num, loc_num);
14298 if (loc != NULL)
14299 {
b5fa468f
TBA
14300 if (loc->disabled_by_cond && enable)
14301 error (_("Breakpoint %d's condition is invalid at location %d, "
14302 "cannot enable."), bp_num, loc_num);
14303
d0fe4701
XR
14304 if (loc->enabled != enable)
14305 {
14306 loc->enabled = enable;
14307 mark_breakpoint_location_modified (loc);
14308 }
14309 if (target_supports_enable_disable_tracepoint ()
14310 && current_trace_status ()->running && loc->owner
14311 && is_tracepoint (loc->owner))
14312 target_disable_tracepoint (loc);
14313 }
14314 update_global_location_list (UGLL_DONT_INSERT);
d7154a8d
JV
14315
14316 gdb::observers::breakpoint_modified.notify (loc->owner);
d0fe4701
XR
14317}
14318
14319/* Enable or disable a range of breakpoint locations. BP_NUM is the
14320 number of the breakpoint, and BP_LOC_RANGE specifies the
14321 (inclusive) range of location numbers of that breakpoint to
14322 enable/disable. ENABLE specifies whether to enable or disable the
14323 location. */
14324
14325static void
14326enable_disable_breakpoint_location_range (int bp_num,
14327 std::pair<int, int> &bp_loc_range,
14328 bool enable)
14329{
14330 for (int i = bp_loc_range.first; i <= bp_loc_range.second; i++)
14331 enable_disable_bp_num_loc (bp_num, i, enable);
14332}
0d381245 14333
1900040c
MS
14334/* Set ignore-count of breakpoint number BPTNUM to COUNT.
14335 If from_tty is nonzero, it prints a message to that effect,
14336 which ends with a period (no newline). */
14337
c906108c 14338void
fba45db2 14339disable_breakpoint (struct breakpoint *bpt)
c906108c
SS
14340{
14341 /* Never disable a watchpoint scope breakpoint; we want to
14342 hit them when we leave scope so we can delete both the
14343 watchpoint and its scope breakpoint at that time. */
14344 if (bpt->type == bp_watchpoint_scope)
14345 return;
14346
b5de0fa7 14347 bpt->enable_state = bp_disabled;
c906108c 14348
b775012e
LM
14349 /* Mark breakpoint locations modified. */
14350 mark_breakpoint_modified (bpt);
14351
d248b706
KY
14352 if (target_supports_enable_disable_tracepoint ()
14353 && current_trace_status ()->running && is_tracepoint (bpt))
14354 {
14355 struct bp_location *location;
14356
14357 for (location = bpt->loc; location; location = location->next)
14358 target_disable_tracepoint (location);
14359 }
14360
44702360 14361 update_global_location_list (UGLL_DONT_INSERT);
c906108c 14362
76727919 14363 gdb::observers::breakpoint_modified.notify (bpt);
c906108c
SS
14364}
14365
d0fe4701
XR
14366/* Enable or disable the breakpoint(s) or breakpoint location(s)
14367 specified in ARGS. ARGS may be in any of the formats handled by
14368 extract_bp_number_and_location. ENABLE specifies whether to enable
14369 or disable the breakpoints/locations. */
14370
c906108c 14371static void
d0fe4701 14372enable_disable_command (const char *args, int from_tty, bool enable)
c906108c 14373{
c906108c 14374 if (args == 0)
46c6471b
PA
14375 {
14376 struct breakpoint *bpt;
14377
14378 ALL_BREAKPOINTS (bpt)
14379 if (user_breakpoint_p (bpt))
d0fe4701
XR
14380 {
14381 if (enable)
14382 enable_breakpoint (bpt);
14383 else
14384 disable_breakpoint (bpt);
14385 }
46c6471b 14386 }
9eaabc75 14387 else
0d381245 14388 {
cb791d59 14389 std::string num = extract_arg (&args);
9eaabc75 14390
cb791d59 14391 while (!num.empty ())
d248b706 14392 {
d0fe4701 14393 std::pair<int, int> bp_num_range, bp_loc_range;
9eaabc75 14394
cc638e86
PA
14395 extract_bp_number_and_location (num, bp_num_range, bp_loc_range);
14396
14397 if (bp_loc_range.first == bp_loc_range.second
14398 && bp_loc_range.first == 0)
d0fe4701 14399 {
cc638e86
PA
14400 /* Handle breakpoint ids with formats 'x' or 'x-z'. */
14401 map_breakpoint_number_range (bp_num_range,
14402 enable
14403 ? enable_breakpoint
14404 : disable_breakpoint);
14405 }
14406 else
14407 {
14408 /* Handle breakpoint ids with formats 'x.y' or
14409 'x.y-z'. */
14410 enable_disable_breakpoint_location_range
14411 (bp_num_range.first, bp_loc_range, enable);
b775012e 14412 }
9eaabc75 14413 num = extract_arg (&args);
d248b706 14414 }
0d381245 14415 }
c906108c
SS
14416}
14417
d0fe4701
XR
14418/* The disable command disables the specified breakpoints/locations
14419 (or all defined breakpoints) so they're no longer effective in
14420 stopping the inferior. ARGS may be in any of the forms defined in
14421 extract_bp_number_and_location. */
14422
14423static void
14424disable_command (const char *args, int from_tty)
14425{
14426 enable_disable_command (args, from_tty, false);
14427}
14428
c906108c 14429static void
816338b5
SS
14430enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
14431 int count)
c906108c 14432{
afe38095 14433 int target_resources_ok;
c906108c
SS
14434
14435 if (bpt->type == bp_hardware_breakpoint)
14436 {
14437 int i;
c5aa993b 14438 i = hw_breakpoint_used_count ();
53a5351d 14439 target_resources_ok =
d92524f1 14440 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
53a5351d 14441 i + 1, 0);
c906108c 14442 if (target_resources_ok == 0)
8a3fe4f8 14443 error (_("No hardware breakpoint support in the target."));
c906108c 14444 else if (target_resources_ok < 0)
8a3fe4f8 14445 error (_("Hardware breakpoints used exceeds limit."));
c906108c
SS
14446 }
14447
cc60f2e3 14448 if (is_watchpoint (bpt))
c906108c 14449 {
d07205c2 14450 /* Initialize it just to avoid a GCC false warning. */
f486487f 14451 enum enable_state orig_enable_state = bp_disabled;
dde02812 14452
a70b8144 14453 try
c906108c 14454 {
3a5c3e22
PA
14455 struct watchpoint *w = (struct watchpoint *) bpt;
14456
1e718ff1
TJB
14457 orig_enable_state = bpt->enable_state;
14458 bpt->enable_state = bp_enabled;
3a5c3e22 14459 update_watchpoint (w, 1 /* reparse */);
c906108c 14460 }
230d2906 14461 catch (const gdb_exception &e)
c5aa993b 14462 {
1e718ff1 14463 bpt->enable_state = orig_enable_state;
dde02812
ES
14464 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
14465 bpt->number);
14466 return;
c5aa993b 14467 }
c906108c 14468 }
0101ce28 14469
b775012e
LM
14470 bpt->enable_state = bp_enabled;
14471
14472 /* Mark breakpoint locations modified. */
14473 mark_breakpoint_modified (bpt);
14474
d248b706
KY
14475 if (target_supports_enable_disable_tracepoint ()
14476 && current_trace_status ()->running && is_tracepoint (bpt))
14477 {
14478 struct bp_location *location;
14479
14480 for (location = bpt->loc; location; location = location->next)
14481 target_enable_tracepoint (location);
14482 }
14483
b4c291bb 14484 bpt->disposition = disposition;
816338b5 14485 bpt->enable_count = count;
44702360 14486 update_global_location_list (UGLL_MAY_INSERT);
9c97429f 14487
76727919 14488 gdb::observers::breakpoint_modified.notify (bpt);
c906108c
SS
14489}
14490
fe3f5fa8 14491
c906108c 14492void
fba45db2 14493enable_breakpoint (struct breakpoint *bpt)
c906108c 14494{
816338b5 14495 enable_breakpoint_disp (bpt, bpt->disposition, 0);
51be5b68
PA
14496}
14497
d0fe4701
XR
14498/* The enable command enables the specified breakpoints/locations (or
14499 all defined breakpoints) so they once again become (or continue to
14500 be) effective in stopping the inferior. ARGS may be in any of the
14501 forms defined in extract_bp_number_and_location. */
c906108c 14502
c906108c 14503static void
981a3fb3 14504enable_command (const char *args, int from_tty)
c906108c 14505{
d0fe4701 14506 enable_disable_command (args, from_tty, true);
c906108c
SS
14507}
14508
c906108c 14509static void
4495129a 14510enable_once_command (const char *args, int from_tty)
c906108c 14511{
48649e1b
TT
14512 map_breakpoint_numbers
14513 (args, [&] (breakpoint *b)
14514 {
14515 iterate_over_related_breakpoints
14516 (b, [&] (breakpoint *bpt)
14517 {
14518 enable_breakpoint_disp (bpt, disp_disable, 1);
14519 });
14520 });
816338b5
SS
14521}
14522
14523static void
4495129a 14524enable_count_command (const char *args, int from_tty)
816338b5 14525{
b9d61307
SM
14526 int count;
14527
14528 if (args == NULL)
14529 error_no_arg (_("hit count"));
14530
14531 count = get_number (&args);
816338b5 14532
48649e1b
TT
14533 map_breakpoint_numbers
14534 (args, [&] (breakpoint *b)
14535 {
14536 iterate_over_related_breakpoints
14537 (b, [&] (breakpoint *bpt)
14538 {
14539 enable_breakpoint_disp (bpt, disp_disable, count);
14540 });
14541 });
c906108c
SS
14542}
14543
c906108c 14544static void
4495129a 14545enable_delete_command (const char *args, int from_tty)
c906108c 14546{
48649e1b
TT
14547 map_breakpoint_numbers
14548 (args, [&] (breakpoint *b)
14549 {
14550 iterate_over_related_breakpoints
14551 (b, [&] (breakpoint *bpt)
14552 {
14553 enable_breakpoint_disp (bpt, disp_del, 1);
14554 });
14555 });
c906108c
SS
14556}
14557\f
1f3b5d1b
PP
14558/* Invalidate last known value of any hardware watchpoint if
14559 the memory which that value represents has been written to by
14560 GDB itself. */
14561
14562static void
8de0566d
YQ
14563invalidate_bp_value_on_memory_change (struct inferior *inferior,
14564 CORE_ADDR addr, ssize_t len,
1f3b5d1b
PP
14565 const bfd_byte *data)
14566{
14567 struct breakpoint *bp;
14568
14569 ALL_BREAKPOINTS (bp)
14570 if (bp->enable_state == bp_enabled
3a5c3e22 14571 && bp->type == bp_hardware_watchpoint)
1f3b5d1b 14572 {
3a5c3e22 14573 struct watchpoint *wp = (struct watchpoint *) bp;
1f3b5d1b 14574
850645cf 14575 if (wp->val_valid && wp->val != nullptr)
3a5c3e22
PA
14576 {
14577 struct bp_location *loc;
14578
14579 for (loc = bp->loc; loc != NULL; loc = loc->next)
14580 if (loc->loc_type == bp_loc_hardware_watchpoint
14581 && loc->address + loc->length > addr
14582 && addr + len > loc->address)
14583 {
3a5c3e22 14584 wp->val = NULL;
4c1d86d9 14585 wp->val_valid = false;
3a5c3e22
PA
14586 }
14587 }
1f3b5d1b
PP
14588 }
14589}
14590
8181d85f
DJ
14591/* Create and insert a breakpoint for software single step. */
14592
14593void
6c95b8df 14594insert_single_step_breakpoint (struct gdbarch *gdbarch,
accd0bcd 14595 const address_space *aspace,
4a64f543 14596 CORE_ADDR next_pc)
8181d85f 14597{
7c16b83e
PA
14598 struct thread_info *tp = inferior_thread ();
14599 struct symtab_and_line sal;
14600 CORE_ADDR pc = next_pc;
8181d85f 14601
34b7e8a6
PA
14602 if (tp->control.single_step_breakpoints == NULL)
14603 {
14604 tp->control.single_step_breakpoints
5d5658a1 14605 = new_single_step_breakpoint (tp->global_num, gdbarch);
34b7e8a6 14606 }
8181d85f 14607
7c16b83e
PA
14608 sal = find_pc_line (pc, 0);
14609 sal.pc = pc;
14610 sal.section = find_pc_overlay (pc);
14611 sal.explicit_pc = 1;
34b7e8a6 14612 add_location_to_breakpoint (tp->control.single_step_breakpoints, &sal);
8181d85f 14613
7c16b83e 14614 update_global_location_list (UGLL_INSERT);
8181d85f
DJ
14615}
14616
93f9a11f
YQ
14617/* Insert single step breakpoints according to the current state. */
14618
14619int
14620insert_single_step_breakpoints (struct gdbarch *gdbarch)
14621{
f5ea389a 14622 struct regcache *regcache = get_current_regcache ();
a0ff9e1a 14623 std::vector<CORE_ADDR> next_pcs;
93f9a11f 14624
f5ea389a 14625 next_pcs = gdbarch_software_single_step (gdbarch, regcache);
93f9a11f 14626
a0ff9e1a 14627 if (!next_pcs.empty ())
93f9a11f 14628 {
f5ea389a 14629 struct frame_info *frame = get_current_frame ();
8b86c959 14630 const address_space *aspace = get_frame_address_space (frame);
93f9a11f 14631
a0ff9e1a 14632 for (CORE_ADDR pc : next_pcs)
93f9a11f
YQ
14633 insert_single_step_breakpoint (gdbarch, aspace, pc);
14634
93f9a11f
YQ
14635 return 1;
14636 }
14637 else
14638 return 0;
14639}
14640
34b7e8a6 14641/* See breakpoint.h. */
f02253f1
HZ
14642
14643int
7c16b83e 14644breakpoint_has_location_inserted_here (struct breakpoint *bp,
accd0bcd 14645 const address_space *aspace,
7c16b83e 14646 CORE_ADDR pc)
1aafd4da 14647{
7c16b83e 14648 struct bp_location *loc;
1aafd4da 14649
7c16b83e
PA
14650 for (loc = bp->loc; loc != NULL; loc = loc->next)
14651 if (loc->inserted
14652 && breakpoint_location_address_match (loc, aspace, pc))
14653 return 1;
1aafd4da 14654
7c16b83e 14655 return 0;
ef370185
JB
14656}
14657
14658/* Check whether a software single-step breakpoint is inserted at
14659 PC. */
14660
14661int
accd0bcd 14662single_step_breakpoint_inserted_here_p (const address_space *aspace,
ef370185
JB
14663 CORE_ADDR pc)
14664{
34b7e8a6
PA
14665 struct breakpoint *bpt;
14666
14667 ALL_BREAKPOINTS (bpt)
14668 {
14669 if (bpt->type == bp_single_step
14670 && breakpoint_has_location_inserted_here (bpt, aspace, pc))
14671 return 1;
14672 }
14673 return 0;
1aafd4da
UW
14674}
14675
1042e4c0
SS
14676/* Tracepoint-specific operations. */
14677
14678/* Set tracepoint count to NUM. */
14679static void
14680set_tracepoint_count (int num)
14681{
14682 tracepoint_count = num;
4fa62494 14683 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
1042e4c0
SS
14684}
14685
70221824 14686static void
0b39b52e 14687trace_command (const char *arg, int from_tty)
1042e4c0 14688{
ffc2605c
TT
14689 event_location_up location = string_to_event_location (&arg,
14690 current_language);
bac7c5cf
GB
14691 const struct breakpoint_ops *ops = breakpoint_ops_for_event_location
14692 (location.get (), true /* is_tracepoint */);
55aa24fb 14693
558a9d82 14694 create_breakpoint (get_current_arch (),
ffc2605c 14695 location.get (),
f00aae0f 14696 NULL, 0, arg, 1 /* parse arg */,
558a9d82
YQ
14697 0 /* tempflag */,
14698 bp_tracepoint /* type_wanted */,
14699 0 /* Ignore count */,
14700 pending_break_support,
14701 ops,
14702 from_tty,
14703 1 /* enabled */,
14704 0 /* internal */, 0);
1042e4c0
SS
14705}
14706
70221824 14707static void
0b39b52e 14708ftrace_command (const char *arg, int from_tty)
7a697b8d 14709{
ffc2605c
TT
14710 event_location_up location = string_to_event_location (&arg,
14711 current_language);
558a9d82 14712 create_breakpoint (get_current_arch (),
ffc2605c 14713 location.get (),
f00aae0f 14714 NULL, 0, arg, 1 /* parse arg */,
558a9d82
YQ
14715 0 /* tempflag */,
14716 bp_fast_tracepoint /* type_wanted */,
14717 0 /* Ignore count */,
14718 pending_break_support,
14719 &tracepoint_breakpoint_ops,
14720 from_tty,
14721 1 /* enabled */,
14722 0 /* internal */, 0);
0fb4aa4b
PA
14723}
14724
14725/* strace command implementation. Creates a static tracepoint. */
14726
70221824 14727static void
0b39b52e 14728strace_command (const char *arg, int from_tty)
0fb4aa4b 14729{
983af33b 14730 struct breakpoint_ops *ops;
ffc2605c 14731 event_location_up location;
983af33b
SDJ
14732
14733 /* Decide if we are dealing with a static tracepoint marker (`-m'),
14734 or with a normal static tracepoint. */
61012eef 14735 if (arg && startswith (arg, "-m") && isspace (arg[2]))
f00aae0f
KS
14736 {
14737 ops = &strace_marker_breakpoint_ops;
a20714ff 14738 location = new_linespec_location (&arg, symbol_name_match_type::FULL);
f00aae0f 14739 }
983af33b 14740 else
f00aae0f
KS
14741 {
14742 ops = &tracepoint_breakpoint_ops;
14743 location = string_to_event_location (&arg, current_language);
14744 }
983af33b 14745
558a9d82 14746 create_breakpoint (get_current_arch (),
ffc2605c 14747 location.get (),
f00aae0f 14748 NULL, 0, arg, 1 /* parse arg */,
558a9d82
YQ
14749 0 /* tempflag */,
14750 bp_static_tracepoint /* type_wanted */,
14751 0 /* Ignore count */,
14752 pending_break_support,
14753 ops,
14754 from_tty,
14755 1 /* enabled */,
14756 0 /* internal */, 0);
7a697b8d
SS
14757}
14758
409873ef
SS
14759/* Set up a fake reader function that gets command lines from a linked
14760 list that was acquired during tracepoint uploading. */
14761
14762static struct uploaded_tp *this_utp;
3149d8c1 14763static int next_cmd;
409873ef
SS
14764
14765static char *
14766read_uploaded_action (void)
14767{
a18ba4e4 14768 char *rslt = nullptr;
409873ef 14769
a18ba4e4
SM
14770 if (next_cmd < this_utp->cmd_strings.size ())
14771 {
67aa1f3c 14772 rslt = this_utp->cmd_strings[next_cmd].get ();
a18ba4e4
SM
14773 next_cmd++;
14774 }
409873ef
SS
14775
14776 return rslt;
14777}
14778
00bf0b85
SS
14779/* Given information about a tracepoint as recorded on a target (which
14780 can be either a live system or a trace file), attempt to create an
14781 equivalent GDB tracepoint. This is not a reliable process, since
14782 the target does not necessarily have all the information used when
14783 the tracepoint was originally defined. */
14784
d9b3f62e 14785struct tracepoint *
00bf0b85 14786create_tracepoint_from_upload (struct uploaded_tp *utp)
d5551862 14787{
f2fc3015
TT
14788 const char *addr_str;
14789 char small_buf[100];
d9b3f62e 14790 struct tracepoint *tp;
fd9b8c24 14791
409873ef 14792 if (utp->at_string)
67aa1f3c 14793 addr_str = utp->at_string.get ();
409873ef
SS
14794 else
14795 {
14796 /* In the absence of a source location, fall back to raw
14797 address. Since there is no way to confirm that the address
14798 means the same thing as when the trace was started, warn the
14799 user. */
3e43a32a
MS
14800 warning (_("Uploaded tracepoint %d has no "
14801 "source location, using raw address"),
409873ef 14802 utp->number);
8c042590 14803 xsnprintf (small_buf, sizeof (small_buf), "*%s", hex_string (utp->addr));
409873ef
SS
14804 addr_str = small_buf;
14805 }
14806
14807 /* There's not much we can do with a sequence of bytecodes. */
14808 if (utp->cond && !utp->cond_string)
3e43a32a
MS
14809 warning (_("Uploaded tracepoint %d condition "
14810 "has no source form, ignoring it"),
409873ef 14811 utp->number);
d5551862 14812
ffc2605c
TT
14813 event_location_up location = string_to_event_location (&addr_str,
14814 current_language);
8cdf0e15 14815 if (!create_breakpoint (get_current_arch (),
ffc2605c 14816 location.get (),
67aa1f3c 14817 utp->cond_string.get (), -1, addr_str,
e7e0cddf 14818 0 /* parse cond/thread */,
8cdf0e15 14819 0 /* tempflag */,
0fb4aa4b 14820 utp->type /* type_wanted */,
8cdf0e15
VP
14821 0 /* Ignore count */,
14822 pending_break_support,
348d480f 14823 &tracepoint_breakpoint_ops,
8cdf0e15 14824 0 /* from_tty */,
84f4c1fe 14825 utp->enabled /* enabled */,
44f238bb
PA
14826 0 /* internal */,
14827 CREATE_BREAKPOINT_FLAGS_INSERTED))
ffc2605c 14828 return NULL;
fd9b8c24 14829
409873ef 14830 /* Get the tracepoint we just created. */
fd9b8c24
PA
14831 tp = get_tracepoint (tracepoint_count);
14832 gdb_assert (tp != NULL);
d5551862 14833
00bf0b85
SS
14834 if (utp->pass > 0)
14835 {
8c042590 14836 xsnprintf (small_buf, sizeof (small_buf), "%d %d", utp->pass,
c1fc2657 14837 tp->number);
00bf0b85 14838
409873ef 14839 trace_pass_command (small_buf, 0);
00bf0b85
SS
14840 }
14841
409873ef
SS
14842 /* If we have uploaded versions of the original commands, set up a
14843 special-purpose "reader" function and call the usual command line
14844 reader, then pass the result to the breakpoint command-setting
14845 function. */
a18ba4e4 14846 if (!utp->cmd_strings.empty ())
00bf0b85 14847 {
12973681 14848 counted_command_line cmd_list;
00bf0b85 14849
409873ef 14850 this_utp = utp;
3149d8c1 14851 next_cmd = 0;
d5551862 14852
60b3cef2 14853 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL);
409873ef 14854
c1fc2657 14855 breakpoint_set_commands (tp, std::move (cmd_list));
00bf0b85 14856 }
a18ba4e4
SM
14857 else if (!utp->actions.empty ()
14858 || !utp->step_actions.empty ())
3e43a32a
MS
14859 warning (_("Uploaded tracepoint %d actions "
14860 "have no source form, ignoring them"),
409873ef 14861 utp->number);
00bf0b85 14862
f196051f 14863 /* Copy any status information that might be available. */
c1fc2657 14864 tp->hit_count = utp->hit_count;
f196051f
SS
14865 tp->traceframe_usage = utp->traceframe_usage;
14866
00bf0b85 14867 return tp;
d9b3f62e 14868}
00bf0b85 14869
1042e4c0
SS
14870/* Print information on tracepoint number TPNUM_EXP, or all if
14871 omitted. */
14872
14873static void
1d12d88f 14874info_tracepoints_command (const char *args, int from_tty)
1042e4c0 14875{
79a45e25 14876 struct ui_out *uiout = current_uiout;
e5a67952 14877 int num_printed;
1042e4c0 14878
5c458ae8 14879 num_printed = breakpoint_1 (args, false, is_tracepoint);
d77f58be
SS
14880
14881 if (num_printed == 0)
1042e4c0 14882 {
e5a67952 14883 if (args == NULL || *args == '\0')
112e8700 14884 uiout->message ("No tracepoints.\n");
d77f58be 14885 else
112e8700 14886 uiout->message ("No tracepoint matching '%s'.\n", args);
1042e4c0 14887 }
ad443146
SS
14888
14889 default_collect_info ();
1042e4c0
SS
14890}
14891
4a64f543 14892/* The 'enable trace' command enables tracepoints.
1042e4c0
SS
14893 Not supported by all targets. */
14894static void
5fed81ff 14895enable_trace_command (const char *args, int from_tty)
1042e4c0
SS
14896{
14897 enable_command (args, from_tty);
14898}
14899
4a64f543 14900/* The 'disable trace' command disables tracepoints.
1042e4c0
SS
14901 Not supported by all targets. */
14902static void
5fed81ff 14903disable_trace_command (const char *args, int from_tty)
1042e4c0
SS
14904{
14905 disable_command (args, from_tty);
14906}
14907
4a64f543 14908/* Remove a tracepoint (or all if no argument). */
1042e4c0 14909static void
4495129a 14910delete_trace_command (const char *arg, int from_tty)
1042e4c0 14911{
35df4500 14912 struct breakpoint *b, *b_tmp;
1042e4c0
SS
14913
14914 dont_repeat ();
14915
14916 if (arg == 0)
14917 {
14918 int breaks_to_delete = 0;
14919
14920 /* Delete all breakpoints if no argument.
14921 Do not delete internal or call-dummy breakpoints, these
4a64f543
MS
14922 have to be deleted with an explicit breakpoint number
14923 argument. */
1042e4c0 14924 ALL_TRACEPOINTS (b)
46c6471b 14925 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0
SS
14926 {
14927 breaks_to_delete = 1;
14928 break;
14929 }
1042e4c0
SS
14930
14931 /* Ask user only if there are some breakpoints to delete. */
14932 if (!from_tty
14933 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
14934 {
35df4500 14935 ALL_BREAKPOINTS_SAFE (b, b_tmp)
46c6471b 14936 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0 14937 delete_breakpoint (b);
1042e4c0
SS
14938 }
14939 }
14940 else
48649e1b 14941 map_breakpoint_numbers
b926417a 14942 (arg, [&] (breakpoint *br)
48649e1b 14943 {
b926417a 14944 iterate_over_related_breakpoints (br, delete_breakpoint);
48649e1b 14945 });
1042e4c0
SS
14946}
14947
197f0a60
TT
14948/* Helper function for trace_pass_command. */
14949
14950static void
d9b3f62e 14951trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
197f0a60 14952{
d9b3f62e 14953 tp->pass_count = count;
76727919 14954 gdb::observers::breakpoint_modified.notify (tp);
197f0a60
TT
14955 if (from_tty)
14956 printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
c1fc2657 14957 tp->number, count);
197f0a60
TT
14958}
14959
1042e4c0
SS
14960/* Set passcount for tracepoint.
14961
14962 First command argument is passcount, second is tracepoint number.
14963 If tracepoint number omitted, apply to most recently defined.
14964 Also accepts special argument "all". */
14965
14966static void
0b39b52e 14967trace_pass_command (const char *args, int from_tty)
1042e4c0 14968{
d9b3f62e 14969 struct tracepoint *t1;
0b39b52e 14970 ULONGEST count;
1042e4c0
SS
14971
14972 if (args == 0 || *args == 0)
3e43a32a
MS
14973 error (_("passcount command requires an "
14974 "argument (count + optional TP num)"));
1042e4c0 14975
0b39b52e 14976 count = strtoulst (args, &args, 10); /* Count comes first, then TP num. */
1042e4c0 14977
529480d0 14978 args = skip_spaces (args);
1042e4c0
SS
14979 if (*args && strncasecmp (args, "all", 3) == 0)
14980 {
d9b3f62e
PA
14981 struct breakpoint *b;
14982
1042e4c0 14983 args += 3; /* Skip special argument "all". */
1042e4c0
SS
14984 if (*args)
14985 error (_("Junk at end of arguments."));
1042e4c0 14986
d9b3f62e 14987 ALL_TRACEPOINTS (b)
197f0a60 14988 {
d9b3f62e 14989 t1 = (struct tracepoint *) b;
197f0a60
TT
14990 trace_pass_set_count (t1, count, from_tty);
14991 }
14992 }
14993 else if (*args == '\0')
1042e4c0 14994 {
5fa1d40e 14995 t1 = get_tracepoint_by_number (&args, NULL);
1042e4c0 14996 if (t1)
197f0a60
TT
14997 trace_pass_set_count (t1, count, from_tty);
14998 }
14999 else
15000 {
bfd28288
PA
15001 number_or_range_parser parser (args);
15002 while (!parser.finished ())
1042e4c0 15003 {
bfd28288 15004 t1 = get_tracepoint_by_number (&args, &parser);
197f0a60
TT
15005 if (t1)
15006 trace_pass_set_count (t1, count, from_tty);
1042e4c0
SS
15007 }
15008 }
1042e4c0
SS
15009}
15010
d9b3f62e 15011struct tracepoint *
1042e4c0
SS
15012get_tracepoint (int num)
15013{
15014 struct breakpoint *t;
15015
15016 ALL_TRACEPOINTS (t)
15017 if (t->number == num)
d9b3f62e 15018 return (struct tracepoint *) t;
1042e4c0
SS
15019
15020 return NULL;
15021}
15022
d5551862
SS
15023/* Find the tracepoint with the given target-side number (which may be
15024 different from the tracepoint number after disconnecting and
15025 reconnecting). */
15026
d9b3f62e 15027struct tracepoint *
d5551862
SS
15028get_tracepoint_by_number_on_target (int num)
15029{
d9b3f62e 15030 struct breakpoint *b;
d5551862 15031
d9b3f62e
PA
15032 ALL_TRACEPOINTS (b)
15033 {
15034 struct tracepoint *t = (struct tracepoint *) b;
15035
15036 if (t->number_on_target == num)
15037 return t;
15038 }
d5551862
SS
15039
15040 return NULL;
15041}
15042
1042e4c0 15043/* Utility: parse a tracepoint number and look it up in the list.
197f0a60 15044 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
5fa1d40e
YQ
15045 If the argument is missing, the most recent tracepoint
15046 (tracepoint_count) is returned. */
15047
d9b3f62e 15048struct tracepoint *
0b39b52e 15049get_tracepoint_by_number (const char **arg,
bfd28288 15050 number_or_range_parser *parser)
1042e4c0 15051{
1042e4c0
SS
15052 struct breakpoint *t;
15053 int tpnum;
0b39b52e 15054 const char *instring = arg == NULL ? NULL : *arg;
1042e4c0 15055
bfd28288 15056 if (parser != NULL)
197f0a60 15057 {
bfd28288
PA
15058 gdb_assert (!parser->finished ());
15059 tpnum = parser->get_number ();
197f0a60
TT
15060 }
15061 else if (arg == NULL || *arg == NULL || ! **arg)
5fa1d40e 15062 tpnum = tracepoint_count;
1042e4c0 15063 else
197f0a60 15064 tpnum = get_number (arg);
1042e4c0
SS
15065
15066 if (tpnum <= 0)
15067 {
15068 if (instring && *instring)
15069 printf_filtered (_("bad tracepoint number at or near '%s'\n"),
15070 instring);
15071 else
5fa1d40e 15072 printf_filtered (_("No previous tracepoint\n"));
1042e4c0
SS
15073 return NULL;
15074 }
15075
15076 ALL_TRACEPOINTS (t)
15077 if (t->number == tpnum)
15078 {
d9b3f62e 15079 return (struct tracepoint *) t;
1042e4c0
SS
15080 }
15081
1042e4c0
SS
15082 printf_unfiltered ("No tracepoint number %d.\n", tpnum);
15083 return NULL;
15084}
15085
d9b3f62e
PA
15086void
15087print_recreate_thread (struct breakpoint *b, struct ui_file *fp)
15088{
15089 if (b->thread != -1)
15090 fprintf_unfiltered (fp, " thread %d", b->thread);
15091
15092 if (b->task != 0)
15093 fprintf_unfiltered (fp, " task %d", b->task);
15094
15095 fprintf_unfiltered (fp, "\n");
15096}
15097
6149aea9
PA
15098/* Save information on user settable breakpoints (watchpoints, etc) to
15099 a new script file named FILENAME. If FILTER is non-NULL, call it
15100 on each breakpoint and only include the ones for which it returns
f2478a7e 15101 true. */
6149aea9 15102
1042e4c0 15103static void
4495129a 15104save_breakpoints (const char *filename, int from_tty,
f2478a7e 15105 bool (*filter) (const struct breakpoint *))
1042e4c0
SS
15106{
15107 struct breakpoint *tp;
6149aea9 15108 int any = 0;
6149aea9 15109 int extra_trace_bits = 0;
1042e4c0 15110
6149aea9
PA
15111 if (filename == 0 || *filename == 0)
15112 error (_("Argument required (file name in which to save)"));
1042e4c0
SS
15113
15114 /* See if we have anything to save. */
6149aea9 15115 ALL_BREAKPOINTS (tp)
1042e4c0 15116 {
6149aea9 15117 /* Skip internal and momentary breakpoints. */
09d682a4 15118 if (!user_breakpoint_p (tp))
6149aea9
PA
15119 continue;
15120
15121 /* If we have a filter, only save the breakpoints it accepts. */
15122 if (filter && !filter (tp))
15123 continue;
15124
15125 any = 1;
15126
15127 if (is_tracepoint (tp))
15128 {
15129 extra_trace_bits = 1;
15130
15131 /* We can stop searching. */
15132 break;
15133 }
1042e4c0 15134 }
6149aea9
PA
15135
15136 if (!any)
1042e4c0 15137 {
6149aea9 15138 warning (_("Nothing to save."));
1042e4c0
SS
15139 return;
15140 }
15141
ee0c3293 15142 gdb::unique_xmalloc_ptr<char> expanded_filename (tilde_expand (filename));
d7e74731
PA
15143
15144 stdio_file fp;
15145
ee0c3293 15146 if (!fp.open (expanded_filename.get (), "w"))
6149aea9 15147 error (_("Unable to open file '%s' for saving (%s)"),
ee0c3293 15148 expanded_filename.get (), safe_strerror (errno));
8bf6485c 15149
6149aea9 15150 if (extra_trace_bits)
d7e74731 15151 save_trace_state_variables (&fp);
8bf6485c 15152
6149aea9 15153 ALL_BREAKPOINTS (tp)
1042e4c0 15154 {
6149aea9 15155 /* Skip internal and momentary breakpoints. */
09d682a4 15156 if (!user_breakpoint_p (tp))
6149aea9 15157 continue;
8bf6485c 15158
6149aea9
PA
15159 /* If we have a filter, only save the breakpoints it accepts. */
15160 if (filter && !filter (tp))
15161 continue;
15162
d7e74731 15163 tp->ops->print_recreate (tp, &fp);
1042e4c0 15164
6149aea9
PA
15165 /* Note, we can't rely on tp->number for anything, as we can't
15166 assume the recreated breakpoint numbers will match. Use $bpnum
15167 instead. */
15168
15169 if (tp->cond_string)
d7e74731 15170 fp.printf (" condition $bpnum %s\n", tp->cond_string);
6149aea9
PA
15171
15172 if (tp->ignore_count)
d7e74731 15173 fp.printf (" ignore $bpnum %d\n", tp->ignore_count);
6149aea9 15174
2d9442cc 15175 if (tp->type != bp_dprintf && tp->commands)
1042e4c0 15176 {
d7e74731 15177 fp.puts (" commands\n");
a7bdde9e 15178
d7e74731 15179 current_uiout->redirect (&fp);
a70b8144 15180 try
1042e4c0 15181 {
d1b0a7bf 15182 print_command_lines (current_uiout, tp->commands.get (), 2);
a7bdde9e 15183 }
230d2906 15184 catch (const gdb_exception &ex)
492d29ea 15185 {
112e8700 15186 current_uiout->redirect (NULL);
eedc3f4f 15187 throw;
492d29ea 15188 }
1042e4c0 15189
112e8700 15190 current_uiout->redirect (NULL);
d7e74731 15191 fp.puts (" end\n");
1042e4c0 15192 }
6149aea9
PA
15193
15194 if (tp->enable_state == bp_disabled)
d7e74731 15195 fp.puts ("disable $bpnum\n");
6149aea9
PA
15196
15197 /* If this is a multi-location breakpoint, check if the locations
15198 should be individually disabled. Watchpoint locations are
15199 special, and not user visible. */
15200 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
15201 {
15202 struct bp_location *loc;
15203 int n = 1;
15204
15205 for (loc = tp->loc; loc != NULL; loc = loc->next, n++)
15206 if (!loc->enabled)
d7e74731 15207 fp.printf ("disable $bpnum.%d\n", n);
6149aea9 15208 }
1042e4c0 15209 }
8bf6485c 15210
6149aea9 15211 if (extra_trace_bits && *default_collect)
d7e74731 15212 fp.printf ("set default-collect %s\n", default_collect);
8bf6485c 15213
1042e4c0 15214 if (from_tty)
ee0c3293 15215 printf_filtered (_("Saved to file '%s'.\n"), expanded_filename.get ());
6149aea9
PA
15216}
15217
15218/* The `save breakpoints' command. */
15219
15220static void
4495129a 15221save_breakpoints_command (const char *args, int from_tty)
6149aea9
PA
15222{
15223 save_breakpoints (args, from_tty, NULL);
15224}
15225
15226/* The `save tracepoints' command. */
15227
15228static void
4495129a 15229save_tracepoints_command (const char *args, int from_tty)
6149aea9
PA
15230{
15231 save_breakpoints (args, from_tty, is_tracepoint);
1042e4c0
SS
15232}
15233
15234/* Create a vector of all tracepoints. */
15235
f51e0e20 15236std::vector<breakpoint *>
eeae04df 15237all_tracepoints (void)
1042e4c0 15238{
f51e0e20 15239 std::vector<breakpoint *> tp_vec;
1042e4c0
SS
15240 struct breakpoint *tp;
15241
15242 ALL_TRACEPOINTS (tp)
15243 {
f51e0e20 15244 tp_vec.push_back (tp);
1042e4c0
SS
15245 }
15246
15247 return tp_vec;
15248}
15249
c906108c 15250\f
629500fa
KS
15251/* This help string is used to consolidate all the help string for specifying
15252 locations used by several commands. */
15253
15254#define LOCATION_HELP_STRING \
15255"Linespecs are colon-separated lists of location parameters, such as\n\
15256source filename, function name, label name, and line number.\n\
15257Example: To specify the start of a label named \"the_top\" in the\n\
15258function \"fact\" in the file \"factorial.c\", use\n\
15259\"factorial.c:fact:the_top\".\n\
15260\n\
15261Address locations begin with \"*\" and specify an exact address in the\n\
15262program. Example: To specify the fourth byte past the start function\n\
15263\"main\", use \"*main + 4\".\n\
15264\n\
15265Explicit locations are similar to linespecs but use an option/argument\n\
15266syntax to specify location parameters.\n\
15267Example: To specify the start of the label named \"the_top\" in the\n\
15268function \"fact\" in the file \"factorial.c\", use \"-source factorial.c\n\
a20714ff
PA
15269-function fact -label the_top\".\n\
15270\n\
15271By default, a specified function is matched against the program's\n\
15272functions in all scopes. For C++, this means in all namespaces and\n\
15273classes. For Ada, this means in all packages. E.g., in C++,\n\
15274\"func()\" matches \"A::func()\", \"A::B::func()\", etc. The\n\
15275\"-qualified\" flag overrides this behavior, making GDB interpret the\n\
89549d7f 15276specified name as a complete fully-qualified name instead."
629500fa 15277
4a64f543
MS
15278/* This help string is used for the break, hbreak, tbreak and thbreak
15279 commands. It is defined as a macro to prevent duplication.
15280 COMMAND should be a string constant containing the name of the
15281 command. */
629500fa 15282
31e2b00f 15283#define BREAK_ARGS_HELP(command) \
733d554a
TBA
15284command" [PROBE_MODIFIER] [LOCATION] [thread THREADNUM]\n\
15285\t[-force-condition] [if CONDITION]\n\
fb7b5af4
SDJ
15286PROBE_MODIFIER shall be present if the command is to be placed in a\n\
15287probe point. Accepted values are `-probe' (for a generic, automatically\n\
d4777acb
JM
15288guessed probe type), `-probe-stap' (for a SystemTap probe) or \n\
15289`-probe-dtrace' (for a DTrace probe).\n\
629500fa
KS
15290LOCATION may be a linespec, address, or explicit location as described\n\
15291below.\n\
15292\n\
dc10affe
PA
15293With no LOCATION, uses current execution address of the selected\n\
15294stack frame. This is useful for breaking on return to a stack frame.\n\
31e2b00f
AS
15295\n\
15296THREADNUM is the number from \"info threads\".\n\
15297CONDITION is a boolean expression.\n\
733d554a
TBA
15298\n\
15299With the \"-force-condition\" flag, the condition is defined even when\n\
15300it is invalid for all current locations.\n\
89549d7f 15301\n" LOCATION_HELP_STRING "\n\n\
d41c0fc8
PA
15302Multiple breakpoints at one place are permitted, and useful if their\n\
15303conditions are different.\n\
31e2b00f
AS
15304\n\
15305Do \"help breakpoints\" for info on other commands dealing with breakpoints."
15306
44feb3ce
TT
15307/* List of subcommands for "catch". */
15308static struct cmd_list_element *catch_cmdlist;
15309
15310/* List of subcommands for "tcatch". */
15311static struct cmd_list_element *tcatch_cmdlist;
15312
9ac4176b 15313void
a121b7c1 15314add_catch_command (const char *name, const char *docstring,
eb4c3f4a 15315 cmd_const_sfunc_ftype *sfunc,
625e8578 15316 completer_ftype *completer,
44feb3ce
TT
15317 void *user_data_catch,
15318 void *user_data_tcatch)
15319{
15320 struct cmd_list_element *command;
15321
0450cc4c 15322 command = add_cmd (name, class_breakpoint, docstring,
44feb3ce
TT
15323 &catch_cmdlist);
15324 set_cmd_sfunc (command, sfunc);
15325 set_cmd_context (command, user_data_catch);
a96d9b2e 15326 set_cmd_completer (command, completer);
44feb3ce 15327
0450cc4c 15328 command = add_cmd (name, class_breakpoint, docstring,
44feb3ce
TT
15329 &tcatch_cmdlist);
15330 set_cmd_sfunc (command, sfunc);
15331 set_cmd_context (command, user_data_tcatch);
a96d9b2e 15332 set_cmd_completer (command, completer);
44feb3ce
TT
15333}
15334
84f4c1fe 15335struct breakpoint *
95da600f 15336iterate_over_breakpoints (gdb::function_view<bool (breakpoint *)> callback)
84f4c1fe 15337{
35df4500 15338 struct breakpoint *b, *b_tmp;
84f4c1fe 15339
35df4500 15340 ALL_BREAKPOINTS_SAFE (b, b_tmp)
84f4c1fe 15341 {
95da600f 15342 if (callback (b))
84f4c1fe
PM
15343 return b;
15344 }
15345
15346 return NULL;
15347}
15348
0574c78f
GB
15349/* Zero if any of the breakpoint's locations could be a location where
15350 functions have been inlined, nonzero otherwise. */
15351
15352static int
15353is_non_inline_function (struct breakpoint *b)
15354{
15355 /* The shared library event breakpoint is set on the address of a
15356 non-inline function. */
15357 if (b->type == bp_shlib_event)
15358 return 1;
15359
15360 return 0;
15361}
15362
15363/* Nonzero if the specified PC cannot be a location where functions
15364 have been inlined. */
15365
15366int
accd0bcd 15367pc_at_non_inline_function (const address_space *aspace, CORE_ADDR pc,
09ac7c10 15368 const struct target_waitstatus *ws)
0574c78f
GB
15369{
15370 struct breakpoint *b;
15371 struct bp_location *bl;
15372
15373 ALL_BREAKPOINTS (b)
15374 {
15375 if (!is_non_inline_function (b))
15376 continue;
15377
15378 for (bl = b->loc; bl != NULL; bl = bl->next)
15379 {
15380 if (!bl->shlib_disabled
09ac7c10 15381 && bpstat_check_location (bl, aspace, pc, ws))
0574c78f
GB
15382 return 1;
15383 }
15384 }
15385
15386 return 0;
15387}
15388
2f202fde
JK
15389/* Remove any references to OBJFILE which is going to be freed. */
15390
15391void
15392breakpoint_free_objfile (struct objfile *objfile)
15393{
15394 struct bp_location **locp, *loc;
15395
15396 ALL_BP_LOCATIONS (loc, locp)
eb822aa6 15397 if (loc->symtab != NULL && SYMTAB_OBJFILE (loc->symtab) == objfile)
2f202fde
JK
15398 loc->symtab = NULL;
15399}
15400
2060206e
PA
15401void
15402initialize_breakpoint_ops (void)
15403{
15404 static int initialized = 0;
15405
15406 struct breakpoint_ops *ops;
15407
15408 if (initialized)
15409 return;
15410 initialized = 1;
15411
15412 /* The breakpoint_ops structure to be inherit by all kinds of
15413 breakpoints (real breakpoints, i.e., user "break" breakpoints,
15414 internal and momentary breakpoints, etc.). */
15415 ops = &bkpt_base_breakpoint_ops;
15416 *ops = base_breakpoint_ops;
15417 ops->re_set = bkpt_re_set;
15418 ops->insert_location = bkpt_insert_location;
15419 ops->remove_location = bkpt_remove_location;
15420 ops->breakpoint_hit = bkpt_breakpoint_hit;
5f700d83 15421 ops->create_sals_from_location = bkpt_create_sals_from_location;
983af33b 15422 ops->create_breakpoints_sal = bkpt_create_breakpoints_sal;
5f700d83 15423 ops->decode_location = bkpt_decode_location;
2060206e
PA
15424
15425 /* The breakpoint_ops structure to be used in regular breakpoints. */
15426 ops = &bkpt_breakpoint_ops;
15427 *ops = bkpt_base_breakpoint_ops;
15428 ops->re_set = bkpt_re_set;
15429 ops->resources_needed = bkpt_resources_needed;
15430 ops->print_it = bkpt_print_it;
15431 ops->print_mention = bkpt_print_mention;
15432 ops->print_recreate = bkpt_print_recreate;
15433
15434 /* Ranged breakpoints. */
15435 ops = &ranged_breakpoint_ops;
15436 *ops = bkpt_breakpoint_ops;
15437 ops->breakpoint_hit = breakpoint_hit_ranged_breakpoint;
15438 ops->resources_needed = resources_needed_ranged_breakpoint;
15439 ops->print_it = print_it_ranged_breakpoint;
15440 ops->print_one = print_one_ranged_breakpoint;
15441 ops->print_one_detail = print_one_detail_ranged_breakpoint;
15442 ops->print_mention = print_mention_ranged_breakpoint;
15443 ops->print_recreate = print_recreate_ranged_breakpoint;
15444
15445 /* Internal breakpoints. */
15446 ops = &internal_breakpoint_ops;
15447 *ops = bkpt_base_breakpoint_ops;
15448 ops->re_set = internal_bkpt_re_set;
15449 ops->check_status = internal_bkpt_check_status;
15450 ops->print_it = internal_bkpt_print_it;
15451 ops->print_mention = internal_bkpt_print_mention;
15452
15453 /* Momentary breakpoints. */
15454 ops = &momentary_breakpoint_ops;
15455 *ops = bkpt_base_breakpoint_ops;
15456 ops->re_set = momentary_bkpt_re_set;
15457 ops->check_status = momentary_bkpt_check_status;
15458 ops->print_it = momentary_bkpt_print_it;
15459 ops->print_mention = momentary_bkpt_print_mention;
15460
55aa24fb
SDJ
15461 /* Probe breakpoints. */
15462 ops = &bkpt_probe_breakpoint_ops;
15463 *ops = bkpt_breakpoint_ops;
15464 ops->insert_location = bkpt_probe_insert_location;
15465 ops->remove_location = bkpt_probe_remove_location;
5f700d83
KS
15466 ops->create_sals_from_location = bkpt_probe_create_sals_from_location;
15467 ops->decode_location = bkpt_probe_decode_location;
55aa24fb 15468
2060206e
PA
15469 /* Watchpoints. */
15470 ops = &watchpoint_breakpoint_ops;
15471 *ops = base_breakpoint_ops;
15472 ops->re_set = re_set_watchpoint;
15473 ops->insert_location = insert_watchpoint;
15474 ops->remove_location = remove_watchpoint;
15475 ops->breakpoint_hit = breakpoint_hit_watchpoint;
15476 ops->check_status = check_status_watchpoint;
15477 ops->resources_needed = resources_needed_watchpoint;
15478 ops->works_in_software_mode = works_in_software_mode_watchpoint;
15479 ops->print_it = print_it_watchpoint;
15480 ops->print_mention = print_mention_watchpoint;
15481 ops->print_recreate = print_recreate_watchpoint;
427cd150 15482 ops->explains_signal = explains_signal_watchpoint;
2060206e
PA
15483
15484 /* Masked watchpoints. */
15485 ops = &masked_watchpoint_breakpoint_ops;
15486 *ops = watchpoint_breakpoint_ops;
15487 ops->insert_location = insert_masked_watchpoint;
15488 ops->remove_location = remove_masked_watchpoint;
15489 ops->resources_needed = resources_needed_masked_watchpoint;
15490 ops->works_in_software_mode = works_in_software_mode_masked_watchpoint;
15491 ops->print_it = print_it_masked_watchpoint;
15492 ops->print_one_detail = print_one_detail_masked_watchpoint;
15493 ops->print_mention = print_mention_masked_watchpoint;
15494 ops->print_recreate = print_recreate_masked_watchpoint;
15495
15496 /* Tracepoints. */
15497 ops = &tracepoint_breakpoint_ops;
15498 *ops = base_breakpoint_ops;
15499 ops->re_set = tracepoint_re_set;
15500 ops->breakpoint_hit = tracepoint_breakpoint_hit;
15501 ops->print_one_detail = tracepoint_print_one_detail;
15502 ops->print_mention = tracepoint_print_mention;
15503 ops->print_recreate = tracepoint_print_recreate;
5f700d83 15504 ops->create_sals_from_location = tracepoint_create_sals_from_location;
983af33b 15505 ops->create_breakpoints_sal = tracepoint_create_breakpoints_sal;
5f700d83 15506 ops->decode_location = tracepoint_decode_location;
983af33b 15507
55aa24fb
SDJ
15508 /* Probe tracepoints. */
15509 ops = &tracepoint_probe_breakpoint_ops;
15510 *ops = tracepoint_breakpoint_ops;
5f700d83
KS
15511 ops->create_sals_from_location = tracepoint_probe_create_sals_from_location;
15512 ops->decode_location = tracepoint_probe_decode_location;
55aa24fb 15513
983af33b
SDJ
15514 /* Static tracepoints with marker (`-m'). */
15515 ops = &strace_marker_breakpoint_ops;
15516 *ops = tracepoint_breakpoint_ops;
5f700d83 15517 ops->create_sals_from_location = strace_marker_create_sals_from_location;
983af33b 15518 ops->create_breakpoints_sal = strace_marker_create_breakpoints_sal;
5f700d83 15519 ops->decode_location = strace_marker_decode_location;
2060206e
PA
15520
15521 /* Fork catchpoints. */
15522 ops = &catch_fork_breakpoint_ops;
15523 *ops = base_breakpoint_ops;
15524 ops->insert_location = insert_catch_fork;
15525 ops->remove_location = remove_catch_fork;
15526 ops->breakpoint_hit = breakpoint_hit_catch_fork;
15527 ops->print_it = print_it_catch_fork;
15528 ops->print_one = print_one_catch_fork;
15529 ops->print_mention = print_mention_catch_fork;
15530 ops->print_recreate = print_recreate_catch_fork;
15531
15532 /* Vfork catchpoints. */
15533 ops = &catch_vfork_breakpoint_ops;
15534 *ops = base_breakpoint_ops;
15535 ops->insert_location = insert_catch_vfork;
15536 ops->remove_location = remove_catch_vfork;
15537 ops->breakpoint_hit = breakpoint_hit_catch_vfork;
15538 ops->print_it = print_it_catch_vfork;
15539 ops->print_one = print_one_catch_vfork;
15540 ops->print_mention = print_mention_catch_vfork;
15541 ops->print_recreate = print_recreate_catch_vfork;
15542
15543 /* Exec catchpoints. */
15544 ops = &catch_exec_breakpoint_ops;
15545 *ops = base_breakpoint_ops;
2060206e
PA
15546 ops->insert_location = insert_catch_exec;
15547 ops->remove_location = remove_catch_exec;
15548 ops->breakpoint_hit = breakpoint_hit_catch_exec;
15549 ops->print_it = print_it_catch_exec;
15550 ops->print_one = print_one_catch_exec;
15551 ops->print_mention = print_mention_catch_exec;
15552 ops->print_recreate = print_recreate_catch_exec;
15553
edcc5120
TT
15554 /* Solib-related catchpoints. */
15555 ops = &catch_solib_breakpoint_ops;
15556 *ops = base_breakpoint_ops;
edcc5120
TT
15557 ops->insert_location = insert_catch_solib;
15558 ops->remove_location = remove_catch_solib;
15559 ops->breakpoint_hit = breakpoint_hit_catch_solib;
15560 ops->check_status = check_status_catch_solib;
15561 ops->print_it = print_it_catch_solib;
15562 ops->print_one = print_one_catch_solib;
15563 ops->print_mention = print_mention_catch_solib;
15564 ops->print_recreate = print_recreate_catch_solib;
e7e0cddf
SS
15565
15566 ops = &dprintf_breakpoint_ops;
15567 *ops = bkpt_base_breakpoint_ops;
5c2b4418 15568 ops->re_set = dprintf_re_set;
e7e0cddf
SS
15569 ops->resources_needed = bkpt_resources_needed;
15570 ops->print_it = bkpt_print_it;
15571 ops->print_mention = bkpt_print_mention;
2d9442cc 15572 ops->print_recreate = dprintf_print_recreate;
9d6e6e84 15573 ops->after_condition_true = dprintf_after_condition_true;
cd1608cc 15574 ops->breakpoint_hit = dprintf_breakpoint_hit;
2060206e
PA
15575}
15576
8bfd80db
YQ
15577/* Chain containing all defined "enable breakpoint" subcommands. */
15578
15579static struct cmd_list_element *enablebreaklist = NULL;
15580
8588b356
SM
15581/* See breakpoint.h. */
15582
15583cmd_list_element *commands_cmd_element = nullptr;
15584
6c265988 15585void _initialize_breakpoint ();
c906108c 15586void
6c265988 15587_initialize_breakpoint ()
c906108c
SS
15588{
15589 struct cmd_list_element *c;
15590
2060206e
PA
15591 initialize_breakpoint_ops ();
15592
76727919
TT
15593 gdb::observers::solib_unloaded.attach (disable_breakpoints_in_unloaded_shlib);
15594 gdb::observers::free_objfile.attach (disable_breakpoints_in_freed_objfile);
15595 gdb::observers::memory_changed.attach (invalidate_bp_value_on_memory_change);
84acb35a 15596
c906108c
SS
15597 breakpoint_chain = 0;
15598 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
15599 before a breakpoint is set. */
15600 breakpoint_count = 0;
15601
1042e4c0
SS
15602 tracepoint_count = 0;
15603
1bedd215
AC
15604 add_com ("ignore", class_breakpoint, ignore_command, _("\
15605Set ignore-count of breakpoint number N to COUNT.\n\
15606Usage is `ignore N COUNT'."));
c906108c 15607
8588b356
SM
15608 commands_cmd_element = add_com ("commands", class_breakpoint,
15609 commands_command, _("\
18da0c51
MG
15610Set commands to be executed when the given breakpoints are hit.\n\
15611Give a space-separated breakpoint list as argument after \"commands\".\n\
15612A list element can be a breakpoint number (e.g. `5') or a range of numbers\n\
15613(e.g. `5-7').\n\
c906108c
SS
15614With no argument, the targeted breakpoint is the last one set.\n\
15615The commands themselves follow starting on the next line.\n\
15616Type a line containing \"end\" to indicate the end of them.\n\
15617Give \"silent\" as the first line to make the breakpoint silent;\n\
1bedd215 15618then no output is printed when it is hit, except what the commands print."));
c906108c 15619
d55637df 15620 c = add_com ("condition", class_breakpoint, condition_command, _("\
1bedd215 15621Specify breakpoint number N to break only if COND is true.\n\
733d554a
TBA
15622Usage is `condition [-force] N COND', where N is an integer and COND\n\
15623is an expression to be evaluated whenever breakpoint N is reached.\n\
15624With the \"-force\" flag, the condition is defined even when it is\n\
15625invalid for all current locations."));
d55637df 15626 set_cmd_completer (c, condition_completer);
c906108c 15627
1bedd215 15628 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
31e2b00f 15629Set a temporary breakpoint.\n\
c906108c
SS
15630Like \"break\" except the breakpoint is only temporary,\n\
15631so it will be deleted when hit. Equivalent to \"break\" followed\n\
31e2b00f
AS
15632by using \"enable delete\" on the breakpoint number.\n\
15633\n"
15634BREAK_ARGS_HELP ("tbreak")));
5ba2abeb 15635 set_cmd_completer (c, location_completer);
c94fdfd0 15636
1bedd215 15637 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
a3be7890 15638Set a hardware assisted breakpoint.\n\
c906108c 15639Like \"break\" except the breakpoint requires hardware support,\n\
31e2b00f
AS
15640some target hardware may not have this support.\n\
15641\n"
15642BREAK_ARGS_HELP ("hbreak")));
5ba2abeb 15643 set_cmd_completer (c, location_completer);
c906108c 15644
1bedd215 15645 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
31e2b00f 15646Set a temporary hardware assisted breakpoint.\n\
c906108c 15647Like \"hbreak\" except the breakpoint is only temporary,\n\
31e2b00f
AS
15648so it will be deleted when hit.\n\
15649\n"
15650BREAK_ARGS_HELP ("thbreak")));
5ba2abeb 15651 set_cmd_completer (c, location_completer);
c906108c 15652
1bedd215 15653 add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
590042fc
PW
15654Enable all or some breakpoints.\n\
15655Usage: enable [BREAKPOINTNUM]...\n\
c906108c
SS
15656Give breakpoint numbers (separated by spaces) as arguments.\n\
15657With no subcommand, breakpoints are enabled until you command otherwise.\n\
15658This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 15659With a subcommand you can enable temporarily."),
c906108c 15660 &enablelist, "enable ", 1, &cmdlist);
c906108c
SS
15661
15662 add_com_alias ("en", "enable", class_breakpoint, 1);
15663
84951ab5 15664 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
590042fc
PW
15665Enable all or some breakpoints.\n\
15666Usage: enable breakpoints [BREAKPOINTNUM]...\n\
c906108c
SS
15667Give breakpoint numbers (separated by spaces) as arguments.\n\
15668This is used to cancel the effect of the \"disable\" command.\n\
89549d7f 15669May be abbreviated to simply \"enable\"."),
c5aa993b 15670 &enablebreaklist, "enable breakpoints ", 1, &enablelist);
c906108c 15671
1a966eab 15672 add_cmd ("once", no_class, enable_once_command, _("\
590042fc
PW
15673Enable some breakpoints for one hit.\n\
15674Usage: enable breakpoints once BREAKPOINTNUM...\n\
1a966eab 15675If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
c906108c
SS
15676 &enablebreaklist);
15677
1a966eab 15678 add_cmd ("delete", no_class, enable_delete_command, _("\
590042fc
PW
15679Enable some breakpoints and delete when hit.\n\
15680Usage: enable breakpoints delete BREAKPOINTNUM...\n\
1a966eab 15681If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
15682 &enablebreaklist);
15683
816338b5 15684 add_cmd ("count", no_class, enable_count_command, _("\
590042fc
PW
15685Enable some breakpoints for COUNT hits.\n\
15686Usage: enable breakpoints count COUNT BREAKPOINTNUM...\n\
816338b5
SS
15687If a breakpoint is hit while enabled in this fashion,\n\
15688the count is decremented; when it reaches zero, the breakpoint is disabled."),
15689 &enablebreaklist);
15690
1a966eab 15691 add_cmd ("delete", no_class, enable_delete_command, _("\
590042fc
PW
15692Enable some breakpoints and delete when hit.\n\
15693Usage: enable delete BREAKPOINTNUM...\n\
1a966eab 15694If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
15695 &enablelist);
15696
1a966eab 15697 add_cmd ("once", no_class, enable_once_command, _("\
590042fc
PW
15698Enable some breakpoints for one hit.\n\
15699Usage: enable once BREAKPOINTNUM...\n\
1a966eab 15700If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
816338b5
SS
15701 &enablelist);
15702
15703 add_cmd ("count", no_class, enable_count_command, _("\
590042fc
PW
15704Enable some breakpoints for COUNT hits.\n\
15705Usage: enable count COUNT BREAKPOINTNUM...\n\
816338b5
SS
15706If a breakpoint is hit while enabled in this fashion,\n\
15707the count is decremented; when it reaches zero, the breakpoint is disabled."),
c906108c
SS
15708 &enablelist);
15709
1bedd215 15710 add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
590042fc
PW
15711Disable all or some breakpoints.\n\
15712Usage: disable [BREAKPOINTNUM]...\n\
c906108c
SS
15713Arguments are breakpoint numbers with spaces in between.\n\
15714To disable all breakpoints, give no argument.\n\
64b9b334 15715A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
c906108c
SS
15716 &disablelist, "disable ", 1, &cmdlist);
15717 add_com_alias ("dis", "disable", class_breakpoint, 1);
15718 add_com_alias ("disa", "disable", class_breakpoint, 1);
c906108c 15719
57b4f16e 15720 add_cmd ("breakpoints", class_breakpoint, disable_command, _("\
590042fc
PW
15721Disable all or some breakpoints.\n\
15722Usage: disable breakpoints [BREAKPOINTNUM]...\n\
c906108c
SS
15723Arguments are breakpoint numbers with spaces in between.\n\
15724To disable all breakpoints, give no argument.\n\
64b9b334 15725A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
1a966eab 15726This command may be abbreviated \"disable\"."),
c906108c
SS
15727 &disablelist);
15728
1bedd215 15729 add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
590042fc
PW
15730Delete all or some breakpoints.\n\
15731Usage: delete [BREAKPOINTNUM]...\n\
c906108c
SS
15732Arguments are breakpoint numbers with spaces in between.\n\
15733To delete all breakpoints, give no argument.\n\
15734\n\
590042fc 15735Also a prefix command for deletion of other GDB objects."),
c906108c
SS
15736 &deletelist, "delete ", 1, &cmdlist);
15737 add_com_alias ("d", "delete", class_breakpoint, 1);
7f198e01 15738 add_com_alias ("del", "delete", class_breakpoint, 1);
c906108c 15739
57b4f16e 15740 add_cmd ("breakpoints", class_breakpoint, delete_command, _("\
590042fc
PW
15741Delete all or some breakpoints or auto-display expressions.\n\
15742Usage: delete breakpoints [BREAKPOINTNUM]...\n\
c906108c
SS
15743Arguments are breakpoint numbers with spaces in between.\n\
15744To delete all breakpoints, give no argument.\n\
1a966eab 15745This command may be abbreviated \"delete\"."),
c906108c
SS
15746 &deletelist);
15747
1bedd215 15748 add_com ("clear", class_breakpoint, clear_command, _("\
629500fa
KS
15749Clear breakpoint at specified location.\n\
15750Argument may be a linespec, explicit, or address location as described below.\n\
1bedd215
AC
15751\n\
15752With no argument, clears all breakpoints in the line that the selected frame\n\
629500fa 15753is executing in.\n"
89549d7f 15754"\n" LOCATION_HELP_STRING "\n\n\
1bedd215 15755See also the \"delete\" command which clears breakpoints by number."));
a6cc4789 15756 add_com_alias ("cl", "clear", class_breakpoint, 1);
c906108c 15757
1bedd215 15758 c = add_com ("break", class_breakpoint, break_command, _("\
629500fa 15759Set breakpoint at specified location.\n"
31e2b00f 15760BREAK_ARGS_HELP ("break")));
5ba2abeb 15761 set_cmd_completer (c, location_completer);
c94fdfd0 15762
c906108c
SS
15763 add_com_alias ("b", "break", class_run, 1);
15764 add_com_alias ("br", "break", class_run, 1);
15765 add_com_alias ("bre", "break", class_run, 1);
15766 add_com_alias ("brea", "break", class_run, 1);
15767
c906108c
SS
15768 if (dbx_commands)
15769 {
1bedd215
AC
15770 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
15771Break in function/address or break at a line in the current file."),
c5aa993b
JM
15772 &stoplist, "stop ", 1, &cmdlist);
15773 add_cmd ("in", class_breakpoint, stopin_command,
1a966eab 15774 _("Break in function or address."), &stoplist);
c5aa993b 15775 add_cmd ("at", class_breakpoint, stopat_command,
1a966eab 15776 _("Break at a line in the current file."), &stoplist);
11db9430 15777 add_com ("status", class_info, info_breakpoints_command, _("\
1bedd215 15778Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
15779The \"Type\" column indicates one of:\n\
15780\tbreakpoint - normal breakpoint\n\
15781\twatchpoint - watchpoint\n\
15782The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15783the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15784breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
15785address and file/line number respectively.\n\
15786\n\
15787Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
15788are set to the address of the last breakpoint listed unless the command\n\
15789is prefixed with \"server \".\n\n\
c906108c 15790Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 15791breakpoint set."));
c906108c
SS
15792 }
15793
11db9430 15794 add_info ("breakpoints", info_breakpoints_command, _("\
e5a67952 15795Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
c906108c
SS
15796The \"Type\" column indicates one of:\n\
15797\tbreakpoint - normal breakpoint\n\
15798\twatchpoint - watchpoint\n\
15799The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15800the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15801breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
15802address and file/line number respectively.\n\
15803\n\
15804Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
15805are set to the address of the last breakpoint listed unless the command\n\
15806is prefixed with \"server \".\n\n\
c906108c 15807Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 15808breakpoint set."));
c906108c 15809
6b04bdb7
MS
15810 add_info_alias ("b", "breakpoints", 1);
15811
1a966eab
AC
15812 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
15813Status of all breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
15814The \"Type\" column indicates one of:\n\
15815\tbreakpoint - normal breakpoint\n\
15816\twatchpoint - watchpoint\n\
15817\tlongjmp - internal breakpoint used to step through longjmp()\n\
15818\tlongjmp resume - internal breakpoint at the target of longjmp()\n\
15819\tuntil - internal breakpoint used by the \"until\" command\n\
1a966eab
AC
15820\tfinish - internal breakpoint used by the \"finish\" command\n\
15821The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
c906108c
SS
15822the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15823breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1a966eab
AC
15824address and file/line number respectively.\n\
15825\n\
15826Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
15827are set to the address of the last breakpoint listed unless the command\n\
15828is prefixed with \"server \".\n\n\
c906108c 15829Convenience variable \"$bpnum\" contains the number of the last\n\
1a966eab 15830breakpoint set."),
c906108c
SS
15831 &maintenanceinfolist);
15832
3b6acaee 15833 add_basic_prefix_cmd ("catch", class_breakpoint, _("\
44feb3ce 15834Set catchpoints to catch events."),
3b6acaee
TT
15835 &catch_cmdlist, "catch ",
15836 0/*allow-unknown*/, &cmdlist);
44feb3ce 15837
3b6acaee 15838 add_basic_prefix_cmd ("tcatch", class_breakpoint, _("\
44feb3ce 15839Set temporary catchpoints to catch events."),
3b6acaee
TT
15840 &tcatch_cmdlist, "tcatch ",
15841 0/*allow-unknown*/, &cmdlist);
44feb3ce 15842
44feb3ce
TT
15843 add_catch_command ("fork", _("Catch calls to fork."),
15844 catch_fork_command_1,
a96d9b2e 15845 NULL,
44feb3ce
TT
15846 (void *) (uintptr_t) catch_fork_permanent,
15847 (void *) (uintptr_t) catch_fork_temporary);
15848 add_catch_command ("vfork", _("Catch calls to vfork."),
15849 catch_fork_command_1,
a96d9b2e 15850 NULL,
44feb3ce
TT
15851 (void *) (uintptr_t) catch_vfork_permanent,
15852 (void *) (uintptr_t) catch_vfork_temporary);
15853 add_catch_command ("exec", _("Catch calls to exec."),
15854 catch_exec_command_1,
a96d9b2e
SDJ
15855 NULL,
15856 CATCH_PERMANENT,
15857 CATCH_TEMPORARY);
edcc5120
TT
15858 add_catch_command ("load", _("Catch loads of shared libraries.\n\
15859Usage: catch load [REGEX]\n\
15860If REGEX is given, only stop for libraries matching the regular expression."),
15861 catch_load_command_1,
15862 NULL,
15863 CATCH_PERMANENT,
15864 CATCH_TEMPORARY);
15865 add_catch_command ("unload", _("Catch unloads of shared libraries.\n\
15866Usage: catch unload [REGEX]\n\
15867If REGEX is given, only stop for libraries matching the regular expression."),
15868 catch_unload_command_1,
15869 NULL,
15870 CATCH_PERMANENT,
15871 CATCH_TEMPORARY);
c5aa993b 15872
1bedd215
AC
15873 c = add_com ("watch", class_breakpoint, watch_command, _("\
15874Set a watchpoint for an expression.\n\
06a64a0b 15875Usage: watch [-l|-location] EXPRESSION\n\
c906108c 15876A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
15877an expression changes.\n\
15878If -l or -location is given, this evaluates EXPRESSION and watches\n\
15879the memory to which it refers."));
65d12d83 15880 set_cmd_completer (c, expression_completer);
c906108c 15881
1bedd215
AC
15882 c = add_com ("rwatch", class_breakpoint, rwatch_command, _("\
15883Set a read watchpoint for an expression.\n\
06a64a0b 15884Usage: rwatch [-l|-location] EXPRESSION\n\
c906108c 15885A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
15886an expression is read.\n\
15887If -l or -location is given, this evaluates EXPRESSION and watches\n\
15888the memory to which it refers."));
65d12d83 15889 set_cmd_completer (c, expression_completer);
c906108c 15890
1bedd215
AC
15891 c = add_com ("awatch", class_breakpoint, awatch_command, _("\
15892Set a watchpoint for an expression.\n\
06a64a0b 15893Usage: awatch [-l|-location] EXPRESSION\n\
c906108c 15894A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
15895an expression is either read or written.\n\
15896If -l or -location is given, this evaluates EXPRESSION and watches\n\
15897the memory to which it refers."));
65d12d83 15898 set_cmd_completer (c, expression_completer);
c906108c 15899
11db9430 15900 add_info ("watchpoints", info_watchpoints_command, _("\
e5a67952 15901Status of specified watchpoints (all watchpoints if no argument)."));
c906108c 15902
920d2a44
AC
15903 /* XXX: cagney/2005-02-23: This should be a boolean, and should
15904 respond to changes - contrary to the description. */
85c07804
AC
15905 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
15906 &can_use_hw_watchpoints, _("\
15907Set debugger's willingness to use watchpoint hardware."), _("\
15908Show debugger's willingness to use watchpoint hardware."), _("\
c906108c
SS
15909If zero, gdb will not use hardware for new watchpoints, even if\n\
15910such is available. (However, any hardware watchpoints that were\n\
15911created before setting this to nonzero, will continue to use watchpoint\n\
85c07804
AC
15912hardware.)"),
15913 NULL,
920d2a44 15914 show_can_use_hw_watchpoints,
85c07804 15915 &setlist, &showlist);
c906108c
SS
15916
15917 can_use_hw_watchpoints = 1;
fa8d40ab 15918
1042e4c0
SS
15919 /* Tracepoint manipulation commands. */
15920
15921 c = add_com ("trace", class_breakpoint, trace_command, _("\
629500fa 15922Set a tracepoint at specified location.\n\
1042e4c0
SS
15923\n"
15924BREAK_ARGS_HELP ("trace") "\n\
15925Do \"help tracepoints\" for info on other tracepoint commands."));
15926 set_cmd_completer (c, location_completer);
15927
57b4f16e
PW
15928 add_com_alias ("tp", "trace", class_breakpoint, 0);
15929 add_com_alias ("tr", "trace", class_breakpoint, 1);
15930 add_com_alias ("tra", "trace", class_breakpoint, 1);
15931 add_com_alias ("trac", "trace", class_breakpoint, 1);
1042e4c0 15932
7a697b8d 15933 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
629500fa 15934Set a fast tracepoint at specified location.\n\
7a697b8d
SS
15935\n"
15936BREAK_ARGS_HELP ("ftrace") "\n\
15937Do \"help tracepoints\" for info on other tracepoint commands."));
15938 set_cmd_completer (c, location_completer);
15939
0fb4aa4b 15940 c = add_com ("strace", class_breakpoint, strace_command, _("\
629500fa 15941Set a static tracepoint at location or marker.\n\
0fb4aa4b
PA
15942\n\
15943strace [LOCATION] [if CONDITION]\n\
629500fa
KS
15944LOCATION may be a linespec, explicit, or address location (described below) \n\
15945or -m MARKER_ID.\n\n\
15946If a marker id is specified, probe the marker with that name. With\n\
15947no LOCATION, uses current execution address of the selected stack frame.\n\
0fb4aa4b
PA
15948Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
15949This collects arbitrary user data passed in the probe point call to the\n\
15950tracing library. You can inspect it when analyzing the trace buffer,\n\
15951by printing the $_sdata variable like any other convenience variable.\n\
15952\n\
15953CONDITION is a boolean expression.\n\
89549d7f 15954\n" LOCATION_HELP_STRING "\n\n\
d41c0fc8
PA
15955Multiple tracepoints at one place are permitted, and useful if their\n\
15956conditions are different.\n\
0fb4aa4b
PA
15957\n\
15958Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
15959Do \"help tracepoints\" for info on other tracepoint commands."));
15960 set_cmd_completer (c, location_completer);
15961
11db9430 15962 add_info ("tracepoints", info_tracepoints_command, _("\
e5a67952 15963Status of specified tracepoints (all tracepoints if no argument).\n\
1042e4c0
SS
15964Convenience variable \"$tpnum\" contains the number of the\n\
15965last tracepoint set."));
15966
15967 add_info_alias ("tp", "tracepoints", 1);
15968
15969 add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
15970Delete specified tracepoints.\n\
15971Arguments are tracepoint numbers, separated by spaces.\n\
15972No argument means delete all tracepoints."),
15973 &deletelist);
7e20dfcd 15974 add_alias_cmd ("tr", "tracepoints", class_trace, 1, &deletelist);
1042e4c0
SS
15975
15976 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
15977Disable specified tracepoints.\n\
15978Arguments are tracepoint numbers, separated by spaces.\n\
15979No argument means disable all tracepoints."),
15980 &disablelist);
15981 deprecate_cmd (c, "disable");
15982
15983 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
15984Enable specified tracepoints.\n\
15985Arguments are tracepoint numbers, separated by spaces.\n\
15986No argument means enable all tracepoints."),
15987 &enablelist);
15988 deprecate_cmd (c, "enable");
15989
15990 add_com ("passcount", class_trace, trace_pass_command, _("\
15991Set the passcount for a tracepoint.\n\
15992The trace will end when the tracepoint has been passed 'count' times.\n\
15993Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
15994if TPNUM is omitted, passcount refers to the last tracepoint defined."));
15995
0743fc83
TT
15996 add_basic_prefix_cmd ("save", class_breakpoint,
15997 _("Save breakpoint definitions as a script."),
15998 &save_cmdlist, "save ",
15999 0/*allow-unknown*/, &cmdlist);
6149aea9
PA
16000
16001 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
16002Save current breakpoint definitions as a script.\n\
cce7e648 16003This includes all types of breakpoints (breakpoints, watchpoints,\n\
6149aea9
PA
16004catchpoints, tracepoints). Use the 'source' command in another debug\n\
16005session to restore them."),
16006 &save_cmdlist);
16007 set_cmd_completer (c, filename_completer);
16008
16009 c = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
1042e4c0 16010Save current tracepoint definitions as a script.\n\
6149aea9
PA
16011Use the 'source' command in another debug session to restore them."),
16012 &save_cmdlist);
1042e4c0
SS
16013 set_cmd_completer (c, filename_completer);
16014
6149aea9
PA
16015 c = add_com_alias ("save-tracepoints", "save tracepoints", class_trace, 0);
16016 deprecate_cmd (c, "save tracepoints");
16017
3b6acaee 16018 add_basic_prefix_cmd ("breakpoint", class_maintenance, _("\
590042fc 16019Breakpoint specific settings.\n\
fa8d40ab 16020Configure various breakpoint-specific variables such as\n\
590042fc 16021pending breakpoint behavior."),
3b6acaee
TT
16022 &breakpoint_set_cmdlist, "set breakpoint ",
16023 0/*allow-unknown*/, &setlist);
16024 add_show_prefix_cmd ("breakpoint", class_maintenance, _("\
590042fc 16025Breakpoint specific settings.\n\
fa8d40ab 16026Configure various breakpoint-specific variables such as\n\
590042fc 16027pending breakpoint behavior."),
3b6acaee
TT
16028 &breakpoint_show_cmdlist, "show breakpoint ",
16029 0/*allow-unknown*/, &showlist);
fa8d40ab 16030
7915a72c
AC
16031 add_setshow_auto_boolean_cmd ("pending", no_class,
16032 &pending_break_support, _("\
16033Set debugger's behavior regarding pending breakpoints."), _("\
16034Show debugger's behavior regarding pending breakpoints."), _("\
6e1d7d6c
AC
16035If on, an unrecognized breakpoint location will cause gdb to create a\n\
16036pending breakpoint. If off, an unrecognized breakpoint location results in\n\
16037an error. If auto, an unrecognized breakpoint location results in a\n\
7915a72c 16038user-query to see if a pending breakpoint should be created."),
2c5b56ce 16039 NULL,
920d2a44 16040 show_pending_break_support,
6e1d7d6c
AC
16041 &breakpoint_set_cmdlist,
16042 &breakpoint_show_cmdlist);
fa8d40ab
JJ
16043
16044 pending_break_support = AUTO_BOOLEAN_AUTO;
765dc015
VP
16045
16046 add_setshow_boolean_cmd ("auto-hw", no_class,
16047 &automatic_hardware_breakpoints, _("\
16048Set automatic usage of hardware breakpoints."), _("\
16049Show automatic usage of hardware breakpoints."), _("\
16050If set, the debugger will automatically use hardware breakpoints for\n\
16051breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
16052a warning will be emitted for such breakpoints."),
16053 NULL,
16054 show_automatic_hardware_breakpoints,
16055 &breakpoint_set_cmdlist,
16056 &breakpoint_show_cmdlist);
74960c60 16057
a25a5a45
PA
16058 add_setshow_boolean_cmd ("always-inserted", class_support,
16059 &always_inserted_mode, _("\
74960c60
VP
16060Set mode for inserting breakpoints."), _("\
16061Show mode for inserting breakpoints."), _("\
a25a5a45
PA
16062When this mode is on, breakpoints are inserted immediately as soon as\n\
16063they're created, kept inserted even when execution stops, and removed\n\
16064only when the user deletes them. When this mode is off (the default),\n\
16065breakpoints are inserted only when execution continues, and removed\n\
16066when execution stops."),
72d0e2c5
YQ
16067 NULL,
16068 &show_always_inserted_mode,
16069 &breakpoint_set_cmdlist,
16070 &breakpoint_show_cmdlist);
f1310107 16071
b775012e
LM
16072 add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
16073 condition_evaluation_enums,
16074 &condition_evaluation_mode_1, _("\
16075Set mode of breakpoint condition evaluation."), _("\
16076Show mode of breakpoint condition evaluation."), _("\
d1cda5d9 16077When this is set to \"host\", breakpoint conditions will be\n\
b775012e
LM
16078evaluated on the host's side by GDB. When it is set to \"target\",\n\
16079breakpoint conditions will be downloaded to the target (if the target\n\
16080supports such feature) and conditions will be evaluated on the target's side.\n\
16081If this is set to \"auto\" (default), this will be automatically set to\n\
16082\"target\" if it supports condition evaluation, otherwise it will\n\
7a4e8e7d 16083be set to \"host\"."),
b775012e
LM
16084 &set_condition_evaluation_mode,
16085 &show_condition_evaluation_mode,
16086 &breakpoint_set_cmdlist,
16087 &breakpoint_show_cmdlist);
16088
f1310107
TJB
16089 add_com ("break-range", class_breakpoint, break_range_command, _("\
16090Set a breakpoint for an address range.\n\
16091break-range START-LOCATION, END-LOCATION\n\
16092where START-LOCATION and END-LOCATION can be one of the following:\n\
16093 LINENUM, for that line in the current file,\n\
16094 FILE:LINENUM, for that line in that file,\n\
16095 +OFFSET, for that number of lines after the current line\n\
16096 or the start of the range\n\
16097 FUNCTION, for the first line in that function,\n\
16098 FILE:FUNCTION, to distinguish among like-named static functions.\n\
16099 *ADDRESS, for the instruction at that address.\n\
16100\n\
16101The breakpoint will stop execution of the inferior whenever it executes\n\
16102an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
16103range (including START-LOCATION and END-LOCATION)."));
16104
e7e0cddf 16105 c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
629500fa 16106Set a dynamic printf at specified location.\n\
e7e0cddf 16107dprintf location,format string,arg1,arg2,...\n\
629500fa
KS
16108location may be a linespec, explicit, or address location.\n"
16109"\n" LOCATION_HELP_STRING));
e7e0cddf
SS
16110 set_cmd_completer (c, location_completer);
16111
16112 add_setshow_enum_cmd ("dprintf-style", class_support,
16113 dprintf_style_enums, &dprintf_style, _("\
16114Set the style of usage for dynamic printf."), _("\
16115Show the style of usage for dynamic printf."), _("\
16116This setting chooses how GDB will do a dynamic printf.\n\
16117If the value is \"gdb\", then the printing is done by GDB to its own\n\
16118console, as with the \"printf\" command.\n\
16119If the value is \"call\", the print is done by calling a function in your\n\
16120program; by default printf(), but you can choose a different function or\n\
16121output stream by setting dprintf-function and dprintf-channel."),
16122 update_dprintf_commands, NULL,
16123 &setlist, &showlist);
16124
16125 dprintf_function = xstrdup ("printf");
16126 add_setshow_string_cmd ("dprintf-function", class_support,
16127 &dprintf_function, _("\
590042fc
PW
16128Set the function to use for dynamic printf."), _("\
16129Show the function to use for dynamic printf."), NULL,
e7e0cddf
SS
16130 update_dprintf_commands, NULL,
16131 &setlist, &showlist);
16132
16133 dprintf_channel = xstrdup ("");
16134 add_setshow_string_cmd ("dprintf-channel", class_support,
16135 &dprintf_channel, _("\
590042fc
PW
16136Set the channel to use for dynamic printf."), _("\
16137Show the channel to use for dynamic printf."), NULL,
e7e0cddf
SS
16138 update_dprintf_commands, NULL,
16139 &setlist, &showlist);
16140
d3ce09f5
SS
16141 add_setshow_boolean_cmd ("disconnected-dprintf", no_class,
16142 &disconnected_dprintf, _("\
16143Set whether dprintf continues after GDB disconnects."), _("\
16144Show whether dprintf continues after GDB disconnects."), _("\
16145Use this to let dprintf commands continue to hit and produce output\n\
16146even if GDB disconnects or detaches from the target."),
16147 NULL,
16148 NULL,
16149 &setlist, &showlist);
16150
16151 add_com ("agent-printf", class_vars, agent_printf_command, _("\
590042fc
PW
16152Target agent only formatted printing, like the C \"printf\" function.\n\
16153Usage: agent-printf \"format string\", ARG1, ARG2, ARG3, ..., ARGN\n\
16154This supports most C printf format specifications, like %s, %d, etc.\n\
16155This is useful for formatted output in user-defined commands."));
d3ce09f5 16156
491144b5 16157 automatic_hardware_breakpoints = true;
f3b1572e 16158
76727919
TT
16159 gdb::observers::about_to_proceed.attach (breakpoint_about_to_proceed);
16160 gdb::observers::thread_exit.attach (remove_threaded_breakpoints);
c906108c 16161}
This page took 3.522968 seconds and 4 git commands to generate.