]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/breakpoint.c
gdb: rename regcache::current_regcache to regcache::regcaches
[thirdparty/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
KS
91static void
92 create_sals_from_location_default (const struct event_location *location,
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
PA
106static std::vector<symtab_and_line> decode_location_default
107 (struct breakpoint *b, const struct event_location *location,
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
adc36818 833void
7a26bd4d 834set_breakpoint_condition (struct breakpoint *b, const char *exp,
adc36818
PM
835 int from_tty)
836{
4c55e970 837 if (*exp == 0)
3a5c3e22 838 {
4c55e970
TBA
839 xfree (b->cond_string);
840 b->cond_string = nullptr;
3a5c3e22 841
4c55e970 842 if (is_watchpoint (b))
78319c15 843 static_cast<watchpoint *> (b)->cond_exp.reset ();
4c55e970
TBA
844 else
845 {
78319c15 846 for (bp_location *loc = b->loc; loc != nullptr; loc = loc->next)
4c55e970
TBA
847 {
848 loc->cond.reset ();
849
850 /* No need to free the condition agent expression
851 bytecode (if we have one). We will handle this
852 when we go through update_global_location_list. */
853 }
854 }
1e620590 855
adc36818
PM
856 if (from_tty)
857 printf_filtered (_("Breakpoint %d now unconditional.\n"), b->number);
858 }
859 else
860 {
adc36818
PM
861 if (is_watchpoint (b))
862 {
699bd4cf 863 innermost_block_tracker tracker;
78319c15 864 const char *arg = exp;
4c55e970 865 expression_up new_exp = parse_exp_1 (&arg, 0, 0, 0, &tracker);
78319c15 866 if (*arg != 0)
adc36818 867 error (_("Junk at end of expression"));
78319c15 868 watchpoint *w = static_cast<watchpoint *> (b);
4c55e970 869 w->cond_exp = std::move (new_exp);
699bd4cf 870 w->cond_exp_valid_block = tracker.block ();
adc36818
PM
871 }
872 else
873 {
4c55e970
TBA
874 /* Parse and set condition expressions. We make two passes.
875 In the first, we parse the condition string to see if it
876 is valid in all locations. If so, the condition would be
877 accepted. So we go ahead and set the locations'
878 conditions. In case a failing case is found, we throw
879 the error and the condition string will be rejected.
880 This two-pass approach is taken to avoid setting the
881 state of locations in case of a reject. */
78319c15 882 for (bp_location *loc = b->loc; loc != nullptr; loc = loc->next)
4c55e970 883 {
78319c15 884 const char *arg = exp;
4c55e970
TBA
885 parse_exp_1 (&arg, loc->address,
886 block_for_pc (loc->address), 0);
887 if (*arg != 0)
888 error (_("Junk at end of expression"));
889 }
890
891 /* If we reach here, the condition is valid at all locations. */
78319c15 892 for (bp_location *loc = b->loc; loc != nullptr; loc = loc->next)
adc36818 893 {
78319c15 894 const char *arg = exp;
adc36818 895 loc->cond =
1bb9788d
TT
896 parse_exp_1 (&arg, loc->address,
897 block_for_pc (loc->address), 0);
adc36818
PM
898 }
899 }
1e620590
TBA
900
901 /* We know that the new condition parsed successfully. The
902 condition string of the breakpoint can be safely updated. */
903 xfree (b->cond_string);
904 b->cond_string = xstrdup (exp);
905 b->condition_not_parsed = 0;
adc36818 906 }
b775012e
LM
907 mark_breakpoint_modified (b);
908
76727919 909 gdb::observers::breakpoint_modified.notify (b);
adc36818
PM
910}
911
d55637df
TT
912/* Completion for the "condition" command. */
913
eb3ff9a5 914static void
6f937416 915condition_completer (struct cmd_list_element *cmd,
eb3ff9a5 916 completion_tracker &tracker,
6f937416 917 const char *text, const char *word)
d55637df 918{
6f937416 919 const char *space;
d55637df 920
f1735a53
TT
921 text = skip_spaces (text);
922 space = skip_to_space (text);
d55637df
TT
923 if (*space == '\0')
924 {
925 int len;
926 struct breakpoint *b;
d55637df
TT
927
928 if (text[0] == '$')
929 {
930 /* We don't support completion of history indices. */
eb3ff9a5
PA
931 if (!isdigit (text[1]))
932 complete_internalvar (tracker, &text[1]);
933 return;
d55637df
TT
934 }
935
936 /* We're completing the breakpoint number. */
937 len = strlen (text);
938
939 ALL_BREAKPOINTS (b)
58ce7251
SDJ
940 {
941 char number[50];
942
943 xsnprintf (number, sizeof (number), "%d", b->number);
944
945 if (strncmp (number, text, len) == 0)
b02f78f9 946 tracker.add_completion (make_unique_xstrdup (number));
58ce7251 947 }
d55637df 948
eb3ff9a5 949 return;
d55637df
TT
950 }
951
952 /* We're completing the expression part. */
f1735a53 953 text = skip_spaces (space);
eb3ff9a5 954 expression_completer (cmd, tracker, text, word);
d55637df
TT
955}
956
c906108c
SS
957/* condition N EXP -- set break condition of breakpoint N to EXP. */
958
959static void
0b39b52e 960condition_command (const char *arg, int from_tty)
c906108c 961{
52f0bd74 962 struct breakpoint *b;
0b39b52e 963 const char *p;
52f0bd74 964 int bnum;
c906108c
SS
965
966 if (arg == 0)
e2e0b3e5 967 error_no_arg (_("breakpoint number"));
c906108c
SS
968
969 p = arg;
970 bnum = get_number (&p);
5c44784c 971 if (bnum == 0)
8a3fe4f8 972 error (_("Bad breakpoint argument: '%s'"), arg);
c906108c
SS
973
974 ALL_BREAKPOINTS (b)
975 if (b->number == bnum)
2f069f6f 976 {
6dddc817
DE
977 /* Check if this breakpoint has a "stop" method implemented in an
978 extension language. This method and conditions entered into GDB
979 from the CLI are mutually exclusive. */
980 const struct extension_language_defn *extlang
981 = get_breakpoint_cond_ext_lang (b, EXT_LANG_NONE);
982
983 if (extlang != NULL)
984 {
985 error (_("Only one stop condition allowed. There is currently"
986 " a %s stop condition defined for this breakpoint."),
987 ext_lang_capitalized_name (extlang));
988 }
2566ad2d 989 set_breakpoint_condition (b, p, from_tty);
b775012e
LM
990
991 if (is_breakpoint (b))
44702360 992 update_global_location_list (UGLL_MAY_INSERT);
b775012e 993
2f069f6f
JB
994 return;
995 }
c906108c 996
8a3fe4f8 997 error (_("No breakpoint number %d."), bnum);
c906108c
SS
998}
999
a7bdde9e
VP
1000/* Check that COMMAND do not contain commands that are suitable
1001 only for tracepoints and not suitable for ordinary breakpoints.
4a64f543
MS
1002 Throw if any such commands is found. */
1003
a7bdde9e
VP
1004static void
1005check_no_tracepoint_commands (struct command_line *commands)
1006{
1007 struct command_line *c;
cc59ec59 1008
a7bdde9e
VP
1009 for (c = commands; c; c = c->next)
1010 {
a7bdde9e 1011 if (c->control_type == while_stepping_control)
3e43a32a
MS
1012 error (_("The 'while-stepping' command can "
1013 "only be used for tracepoints"));
a7bdde9e 1014
12973681
TT
1015 check_no_tracepoint_commands (c->body_list_0.get ());
1016 check_no_tracepoint_commands (c->body_list_1.get ());
a7bdde9e
VP
1017
1018 /* Not that command parsing removes leading whitespace and comment
4a64f543 1019 lines and also empty lines. So, we only need to check for
a7bdde9e
VP
1020 command directly. */
1021 if (strstr (c->line, "collect ") == c->line)
1022 error (_("The 'collect' command can only be used for tracepoints"));
1023
51661e93
VP
1024 if (strstr (c->line, "teval ") == c->line)
1025 error (_("The 'teval' command can only be used for tracepoints"));
a7bdde9e
VP
1026 }
1027}
1028
c1fc2657 1029struct longjmp_breakpoint : public breakpoint
3b0871f4 1030{
c1fc2657 1031 ~longjmp_breakpoint () override;
3b0871f4
SM
1032};
1033
d77f58be
SS
1034/* Encapsulate tests for different types of tracepoints. */
1035
3b0871f4
SM
1036static bool
1037is_tracepoint_type (bptype type)
d9b3f62e
PA
1038{
1039 return (type == bp_tracepoint
1040 || type == bp_fast_tracepoint
1041 || type == bp_static_tracepoint);
1042}
1043
3b0871f4
SM
1044static bool
1045is_longjmp_type (bptype type)
1046{
1047 return type == bp_longjmp || type == bp_exception;
1048}
1049
f2478a7e
SM
1050/* See breakpoint.h. */
1051
1052bool
d77f58be 1053is_tracepoint (const struct breakpoint *b)
a7bdde9e 1054{
d9b3f62e 1055 return is_tracepoint_type (b->type);
a7bdde9e 1056}
d9b3f62e 1057
a5e364af
SM
1058/* Factory function to create an appropriate instance of breakpoint given
1059 TYPE. */
1060
1061static std::unique_ptr<breakpoint>
1062new_breakpoint_from_type (bptype type)
1063{
1064 breakpoint *b;
1065
1066 if (is_tracepoint_type (type))
c1fc2657 1067 b = new tracepoint ();
3b0871f4 1068 else if (is_longjmp_type (type))
c1fc2657 1069 b = new longjmp_breakpoint ();
a5e364af
SM
1070 else
1071 b = new breakpoint ();
1072
1073 return std::unique_ptr<breakpoint> (b);
1074}
1075
e5dd4106 1076/* A helper function that validates that COMMANDS are valid for a
95a42b64
TT
1077 breakpoint. This function will throw an exception if a problem is
1078 found. */
48cb2d85 1079
95a42b64
TT
1080static void
1081validate_commands_for_breakpoint (struct breakpoint *b,
1082 struct command_line *commands)
48cb2d85 1083{
d77f58be 1084 if (is_tracepoint (b))
a7bdde9e 1085 {
c9a6ce02 1086 struct tracepoint *t = (struct tracepoint *) b;
a7bdde9e
VP
1087 struct command_line *c;
1088 struct command_line *while_stepping = 0;
c9a6ce02
PA
1089
1090 /* Reset the while-stepping step count. The previous commands
1091 might have included a while-stepping action, while the new
1092 ones might not. */
1093 t->step_count = 0;
1094
1095 /* We need to verify that each top-level element of commands is
1096 valid for tracepoints, that there's at most one
1097 while-stepping element, and that the while-stepping's body
1098 has valid tracing commands excluding nested while-stepping.
1099 We also need to validate the tracepoint action line in the
1100 context of the tracepoint --- validate_actionline actually
1101 has side effects, like setting the tracepoint's
1102 while-stepping STEP_COUNT, in addition to checking if the
1103 collect/teval actions parse and make sense in the
1104 tracepoint's context. */
a7bdde9e
VP
1105 for (c = commands; c; c = c->next)
1106 {
a7bdde9e
VP
1107 if (c->control_type == while_stepping_control)
1108 {
1109 if (b->type == bp_fast_tracepoint)
3e43a32a
MS
1110 error (_("The 'while-stepping' command "
1111 "cannot be used for fast tracepoint"));
0fb4aa4b 1112 else if (b->type == bp_static_tracepoint)
3e43a32a
MS
1113 error (_("The 'while-stepping' command "
1114 "cannot be used for static tracepoint"));
a7bdde9e
VP
1115
1116 if (while_stepping)
3e43a32a
MS
1117 error (_("The 'while-stepping' command "
1118 "can be used only once"));
a7bdde9e
VP
1119 else
1120 while_stepping = c;
1121 }
c9a6ce02
PA
1122
1123 validate_actionline (c->line, b);
a7bdde9e
VP
1124 }
1125 if (while_stepping)
1126 {
1127 struct command_line *c2;
1128
12973681
TT
1129 gdb_assert (while_stepping->body_list_1 == nullptr);
1130 c2 = while_stepping->body_list_0.get ();
a7bdde9e
VP
1131 for (; c2; c2 = c2->next)
1132 {
a7bdde9e
VP
1133 if (c2->control_type == while_stepping_control)
1134 error (_("The 'while-stepping' command cannot be nested"));
1135 }
1136 }
1137 }
1138 else
1139 {
1140 check_no_tracepoint_commands (commands);
1141 }
95a42b64
TT
1142}
1143
0fb4aa4b
PA
1144/* Return a vector of all the static tracepoints set at ADDR. The
1145 caller is responsible for releasing the vector. */
1146
f51e0e20 1147std::vector<breakpoint *>
0fb4aa4b
PA
1148static_tracepoints_here (CORE_ADDR addr)
1149{
1150 struct breakpoint *b;
f51e0e20 1151 std::vector<breakpoint *> found;
0fb4aa4b
PA
1152 struct bp_location *loc;
1153
1154 ALL_BREAKPOINTS (b)
1155 if (b->type == bp_static_tracepoint)
1156 {
1157 for (loc = b->loc; loc; loc = loc->next)
1158 if (loc->address == addr)
f51e0e20 1159 found.push_back (b);
0fb4aa4b
PA
1160 }
1161
1162 return found;
1163}
1164
95a42b64 1165/* Set the command list of B to COMMANDS. If breakpoint is tracepoint,
4a64f543 1166 validate that only allowed commands are included. */
95a42b64
TT
1167
1168void
4a64f543 1169breakpoint_set_commands (struct breakpoint *b,
12973681 1170 counted_command_line &&commands)
95a42b64 1171{
93921405 1172 validate_commands_for_breakpoint (b, commands.get ());
a7bdde9e 1173
d1b0a7bf 1174 b->commands = std::move (commands);
76727919 1175 gdb::observers::breakpoint_modified.notify (b);
48cb2d85
VP
1176}
1177
45a43567
TT
1178/* Set the internal `silent' flag on the breakpoint. Note that this
1179 is not the same as the "silent" that may appear in the breakpoint's
1180 commands. */
1181
1182void
1183breakpoint_set_silent (struct breakpoint *b, int silent)
1184{
1185 int old_silent = b->silent;
1186
1187 b->silent = silent;
1188 if (old_silent != silent)
76727919 1189 gdb::observers::breakpoint_modified.notify (b);
45a43567
TT
1190}
1191
1192/* Set the thread for this breakpoint. If THREAD is -1, make the
1193 breakpoint work for any thread. */
1194
1195void
1196breakpoint_set_thread (struct breakpoint *b, int thread)
1197{
1198 int old_thread = b->thread;
1199
1200 b->thread = thread;
1201 if (old_thread != thread)
76727919 1202 gdb::observers::breakpoint_modified.notify (b);
45a43567
TT
1203}
1204
1205/* Set the task for this breakpoint. If TASK is 0, make the
1206 breakpoint work for any task. */
1207
1208void
1209breakpoint_set_task (struct breakpoint *b, int task)
1210{
1211 int old_task = b->task;
1212
1213 b->task = task;
1214 if (old_task != task)
76727919 1215 gdb::observers::breakpoint_modified.notify (b);
45a43567
TT
1216}
1217
95a42b64 1218static void
896b6bda 1219commands_command_1 (const char *arg, int from_tty,
4a64f543 1220 struct command_line *control)
95a42b64 1221{
d1b0a7bf 1222 counted_command_line cmd;
999700cd
PW
1223 /* cmd_read will be true once we have read cmd. Note that cmd might still be
1224 NULL after the call to read_command_lines if the user provides an empty
1225 list of command by just typing "end". */
1226 bool cmd_read = false;
95a42b64 1227
896b6bda
PA
1228 std::string new_arg;
1229
95a42b64
TT
1230 if (arg == NULL || !*arg)
1231 {
86b17b60 1232 if (breakpoint_count - prev_breakpoint_count > 1)
896b6bda
PA
1233 new_arg = string_printf ("%d-%d", prev_breakpoint_count + 1,
1234 breakpoint_count);
95a42b64 1235 else if (breakpoint_count > 0)
896b6bda 1236 new_arg = string_printf ("%d", breakpoint_count);
48649e1b
TT
1237 arg = new_arg.c_str ();
1238 }
1239
1240 map_breakpoint_numbers
1241 (arg, [&] (breakpoint *b)
1242 {
999700cd 1243 if (!cmd_read)
48649e1b 1244 {
999700cd 1245 gdb_assert (cmd == NULL);
48649e1b 1246 if (control != NULL)
12973681 1247 cmd = control->body_list_0;
48649e1b
TT
1248 else
1249 {
81b1e71c
TT
1250 std::string str
1251 = string_printf (_("Type commands for breakpoint(s) "
1252 "%s, one per line."),
1253 arg);
48649e1b 1254
60b3cef2
TT
1255 auto do_validate = [=] (const char *line)
1256 {
1257 validate_actionline (line, b);
1258 };
1259 gdb::function_view<void (const char *)> validator;
1260 if (is_tracepoint (b))
1261 validator = do_validate;
1262
1263 cmd = read_command_lines (str.c_str (), from_tty, 1, validator);
48649e1b 1264 }
999700cd 1265 cmd_read = true;
48649e1b
TT
1266 }
1267
1268 /* If a breakpoint was on the list more than once, we don't need to
1269 do anything. */
1270 if (b->commands != cmd)
1271 {
d1b0a7bf 1272 validate_commands_for_breakpoint (b, cmd.get ());
48649e1b 1273 b->commands = cmd;
76727919 1274 gdb::observers::breakpoint_modified.notify (b);
48649e1b
TT
1275 }
1276 });
95a42b64
TT
1277}
1278
1279static void
0b39b52e 1280commands_command (const char *arg, int from_tty)
95a42b64
TT
1281{
1282 commands_command_1 (arg, from_tty, NULL);
c906108c 1283}
40c03ae8
EZ
1284
1285/* Like commands_command, but instead of reading the commands from
1286 input stream, takes them from an already parsed command structure.
1287
1288 This is used by cli-script.c to DTRT with breakpoint commands
1289 that are part of if and while bodies. */
1290enum command_control_type
896b6bda 1291commands_from_control_command (const char *arg, struct command_line *cmd)
40c03ae8 1292{
95a42b64
TT
1293 commands_command_1 (arg, 0, cmd);
1294 return simple_control;
40c03ae8 1295}
876fa593
JK
1296
1297/* Return non-zero if BL->TARGET_INFO contains valid information. */
1298
1299static int
1300bp_location_has_shadow (struct bp_location *bl)
1301{
1302 if (bl->loc_type != bp_loc_software_breakpoint)
1303 return 0;
1304 if (!bl->inserted)
1305 return 0;
1306 if (bl->target_info.shadow_len == 0)
e5dd4106 1307 /* BL isn't valid, or doesn't shadow memory. */
876fa593
JK
1308 return 0;
1309 return 1;
1310}
1311
9d497a19
PA
1312/* Update BUF, which is LEN bytes read from the target address
1313 MEMADDR, by replacing a memory breakpoint with its shadowed
1314 contents.
1315
1316 If READBUF is not NULL, this buffer must not overlap with the of
1317 the breakpoint location's shadow_contents buffer. Otherwise, a
1318 failed assertion internal error will be raised. */
1319
1320static void
1321one_breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1322 const gdb_byte *writebuf_org,
1323 ULONGEST memaddr, LONGEST len,
1324 struct bp_target_info *target_info,
1325 struct gdbarch *gdbarch)
1326{
1327 /* Now do full processing of the found relevant range of elements. */
1328 CORE_ADDR bp_addr = 0;
1329 int bp_size = 0;
1330 int bptoffset = 0;
1331
1332 if (!breakpoint_address_match (target_info->placed_address_space, 0,
1333 current_program_space->aspace, 0))
1334 {
1335 /* The breakpoint is inserted in a different address space. */
1336 return;
1337 }
1338
1339 /* Addresses and length of the part of the breakpoint that
1340 we need to copy. */
1341 bp_addr = target_info->placed_address;
1342 bp_size = target_info->shadow_len;
1343
1344 if (bp_addr + bp_size <= memaddr)
1345 {
1346 /* The breakpoint is entirely before the chunk of memory we are
1347 reading. */
1348 return;
1349 }
1350
1351 if (bp_addr >= memaddr + len)
1352 {
1353 /* The breakpoint is entirely after the chunk of memory we are
1354 reading. */
1355 return;
1356 }
1357
1358 /* Offset within shadow_contents. */
1359 if (bp_addr < memaddr)
1360 {
1361 /* Only copy the second part of the breakpoint. */
1362 bp_size -= memaddr - bp_addr;
1363 bptoffset = memaddr - bp_addr;
1364 bp_addr = memaddr;
1365 }
1366
1367 if (bp_addr + bp_size > memaddr + len)
1368 {
1369 /* Only copy the first part of the breakpoint. */
1370 bp_size -= (bp_addr + bp_size) - (memaddr + len);
1371 }
1372
1373 if (readbuf != NULL)
1374 {
1375 /* Verify that the readbuf buffer does not overlap with the
1376 shadow_contents buffer. */
1377 gdb_assert (target_info->shadow_contents >= readbuf + len
1378 || readbuf >= (target_info->shadow_contents
1379 + target_info->shadow_len));
1380
1381 /* Update the read buffer with this inserted breakpoint's
1382 shadow. */
1383 memcpy (readbuf + bp_addr - memaddr,
1384 target_info->shadow_contents + bptoffset, bp_size);
1385 }
1386 else
1387 {
1388 const unsigned char *bp;
0d5ed153
MR
1389 CORE_ADDR addr = target_info->reqstd_address;
1390 int placed_size;
9d497a19
PA
1391
1392 /* Update the shadow with what we want to write to memory. */
1393 memcpy (target_info->shadow_contents + bptoffset,
1394 writebuf_org + bp_addr - memaddr, bp_size);
1395
1396 /* Determine appropriate breakpoint contents and size for this
1397 address. */
0d5ed153 1398 bp = gdbarch_breakpoint_from_pc (gdbarch, &addr, &placed_size);
9d497a19
PA
1399
1400 /* Update the final write buffer with this inserted
1401 breakpoint's INSN. */
1402 memcpy (writebuf + bp_addr - memaddr, bp + bptoffset, bp_size);
1403 }
1404}
1405
8defab1a 1406/* Update BUF, which is LEN bytes read from the target address MEMADDR,
876fa593
JK
1407 by replacing any memory breakpoints with their shadowed contents.
1408
35c63cd8
JB
1409 If READBUF is not NULL, this buffer must not overlap with any of
1410 the breakpoint location's shadow_contents buffers. Otherwise,
1411 a failed assertion internal error will be raised.
1412
876fa593 1413 The range of shadowed area by each bp_location is:
f5336ca5
PA
1414 bl->address - bp_locations_placed_address_before_address_max
1415 up to bl->address + bp_locations_shadow_len_after_address_max
876fa593
JK
1416 The range we were requested to resolve shadows for is:
1417 memaddr ... memaddr + len
1418 Thus the safe cutoff boundaries for performance optimization are
35df4500 1419 memaddr + len <= (bl->address
f5336ca5 1420 - bp_locations_placed_address_before_address_max)
876fa593 1421 and:
f5336ca5 1422 bl->address + bp_locations_shadow_len_after_address_max <= memaddr */
c906108c 1423
8defab1a 1424void
f0ba3972
PA
1425breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1426 const gdb_byte *writebuf_org,
1427 ULONGEST memaddr, LONGEST len)
c906108c 1428{
4a64f543
MS
1429 /* Left boundary, right boundary and median element of our binary
1430 search. */
876fa593
JK
1431 unsigned bc_l, bc_r, bc;
1432
4a64f543
MS
1433 /* Find BC_L which is a leftmost element which may affect BUF
1434 content. It is safe to report lower value but a failure to
1435 report higher one. */
876fa593
JK
1436
1437 bc_l = 0;
f5336ca5 1438 bc_r = bp_locations_count;
876fa593
JK
1439 while (bc_l + 1 < bc_r)
1440 {
35df4500 1441 struct bp_location *bl;
876fa593
JK
1442
1443 bc = (bc_l + bc_r) / 2;
f5336ca5 1444 bl = bp_locations[bc];
876fa593 1445
4a64f543
MS
1446 /* Check first BL->ADDRESS will not overflow due to the added
1447 constant. Then advance the left boundary only if we are sure
1448 the BC element can in no way affect the BUF content (MEMADDR
1449 to MEMADDR + LEN range).
876fa593 1450
f5336ca5 1451 Use the BP_LOCATIONS_SHADOW_LEN_AFTER_ADDRESS_MAX safety
4a64f543
MS
1452 offset so that we cannot miss a breakpoint with its shadow
1453 range tail still reaching MEMADDR. */
c5aa993b 1454
f5336ca5 1455 if ((bl->address + bp_locations_shadow_len_after_address_max
35df4500 1456 >= bl->address)
f5336ca5 1457 && (bl->address + bp_locations_shadow_len_after_address_max
35df4500 1458 <= memaddr))
876fa593
JK
1459 bc_l = bc;
1460 else
1461 bc_r = bc;
1462 }
1463
128070bb
PA
1464 /* Due to the binary search above, we need to make sure we pick the
1465 first location that's at BC_L's address. E.g., if there are
1466 multiple locations at the same address, BC_L may end up pointing
1467 at a duplicate location, and miss the "master"/"inserted"
1468 location. Say, given locations L1, L2 and L3 at addresses A and
1469 B:
1470
1471 L1@A, L2@A, L3@B, ...
1472
1473 BC_L could end up pointing at location L2, while the "master"
1474 location could be L1. Since the `loc->inserted' flag is only set
1475 on "master" locations, we'd forget to restore the shadow of L1
1476 and L2. */
1477 while (bc_l > 0
f5336ca5 1478 && bp_locations[bc_l]->address == bp_locations[bc_l - 1]->address)
128070bb
PA
1479 bc_l--;
1480
876fa593
JK
1481 /* Now do full processing of the found relevant range of elements. */
1482
f5336ca5 1483 for (bc = bc_l; bc < bp_locations_count; bc++)
c5aa993b 1484 {
f5336ca5 1485 struct bp_location *bl = bp_locations[bc];
876fa593 1486
35df4500
TJB
1487 /* bp_location array has BL->OWNER always non-NULL. */
1488 if (bl->owner->type == bp_none)
8a3fe4f8 1489 warning (_("reading through apparently deleted breakpoint #%d?"),
35df4500 1490 bl->owner->number);
ffce0d52 1491
e5dd4106 1492 /* Performance optimization: any further element can no longer affect BUF
876fa593
JK
1493 content. */
1494
f5336ca5
PA
1495 if (bl->address >= bp_locations_placed_address_before_address_max
1496 && memaddr + len <= (bl->address
1497 - bp_locations_placed_address_before_address_max))
876fa593
JK
1498 break;
1499
35df4500 1500 if (!bp_location_has_shadow (bl))
c5aa993b 1501 continue;
6c95b8df 1502
9d497a19
PA
1503 one_breakpoint_xfer_memory (readbuf, writebuf, writebuf_org,
1504 memaddr, len, &bl->target_info, bl->gdbarch);
1505 }
c906108c 1506}
9d497a19 1507
f2478a7e 1508/* See breakpoint.h. */
b775012e 1509
f2478a7e 1510bool
b775012e
LM
1511is_breakpoint (const struct breakpoint *bpt)
1512{
1513 return (bpt->type == bp_breakpoint
e7e0cddf
SS
1514 || bpt->type == bp_hardware_breakpoint
1515 || bpt->type == bp_dprintf);
b775012e
LM
1516}
1517
60e1c644
PA
1518/* Return true if BPT is of any hardware watchpoint kind. */
1519
f2478a7e 1520static bool
d77f58be 1521is_hardware_watchpoint (const struct breakpoint *bpt)
a5606eee
VP
1522{
1523 return (bpt->type == bp_hardware_watchpoint
1524 || bpt->type == bp_read_watchpoint
1525 || bpt->type == bp_access_watchpoint);
1526}
7270d8f2 1527
f2478a7e 1528/* See breakpoint.h. */
60e1c644 1529
f2478a7e 1530bool
d77f58be 1531is_watchpoint (const struct breakpoint *bpt)
60e1c644
PA
1532{
1533 return (is_hardware_watchpoint (bpt)
1534 || bpt->type == bp_watchpoint);
1535}
1536
3a5c3e22
PA
1537/* Returns true if the current thread and its running state are safe
1538 to evaluate or update watchpoint B. Watchpoints on local
1539 expressions need to be evaluated in the context of the thread that
1540 was current when the watchpoint was created, and, that thread needs
1541 to be stopped to be able to select the correct frame context.
1542 Watchpoints on global expressions can be evaluated on any thread,
1543 and in any state. It is presently left to the target allowing
1544 memory accesses when threads are running. */
f6bc2008
PA
1545
1546static int
3a5c3e22 1547watchpoint_in_thread_scope (struct watchpoint *b)
f6bc2008 1548{
c1fc2657 1549 return (b->pspace == current_program_space
d7e15655
TT
1550 && (b->watchpoint_thread == null_ptid
1551 || (inferior_ptid == b->watchpoint_thread
00431a78 1552 && !inferior_thread ()->executing)));
f6bc2008
PA
1553}
1554
d0fb5eae
JK
1555/* Set watchpoint B to disp_del_at_next_stop, even including its possible
1556 associated bp_watchpoint_scope breakpoint. */
1557
1558static void
3a5c3e22 1559watchpoint_del_at_next_stop (struct watchpoint *w)
d0fb5eae 1560{
c1fc2657 1561 if (w->related_breakpoint != w)
d0fb5eae 1562 {
c1fc2657
SM
1563 gdb_assert (w->related_breakpoint->type == bp_watchpoint_scope);
1564 gdb_assert (w->related_breakpoint->related_breakpoint == w);
1565 w->related_breakpoint->disposition = disp_del_at_next_stop;
1566 w->related_breakpoint->related_breakpoint = w->related_breakpoint;
1567 w->related_breakpoint = w;
d0fb5eae 1568 }
c1fc2657 1569 w->disposition = disp_del_at_next_stop;
d0fb5eae
JK
1570}
1571
bb9d5f81
PP
1572/* Extract a bitfield value from value VAL using the bit parameters contained in
1573 watchpoint W. */
1574
1575static struct value *
1576extract_bitfield_from_watchpoint_value (struct watchpoint *w, struct value *val)
1577{
1578 struct value *bit_val;
1579
1580 if (val == NULL)
1581 return NULL;
1582
1583 bit_val = allocate_value (value_type (val));
1584
1585 unpack_value_bitfield (bit_val,
1586 w->val_bitpos,
1587 w->val_bitsize,
1588 value_contents_for_printing (val),
1589 value_offset (val),
1590 val);
1591
1592 return bit_val;
1593}
1594
c6d81124
PA
1595/* Allocate a dummy location and add it to B, which must be a software
1596 watchpoint. This is required because even if a software watchpoint
1597 is not watching any memory, bpstat_stop_status requires a location
1598 to be able to report stops. */
1599
1600static void
1601software_watchpoint_add_no_memory_location (struct breakpoint *b,
1602 struct program_space *pspace)
1603{
1604 gdb_assert (b->type == bp_watchpoint && b->loc == NULL);
1605
1606 b->loc = allocate_bp_location (b);
1607 b->loc->pspace = pspace;
1608 b->loc->address = -1;
1609 b->loc->length = -1;
1610}
1611
1612/* Returns true if B is a software watchpoint that is not watching any
1613 memory (e.g., "watch $pc"). */
1614
f2478a7e 1615static bool
c6d81124
PA
1616is_no_memory_software_watchpoint (struct breakpoint *b)
1617{
1618 return (b->type == bp_watchpoint
1619 && b->loc != NULL
1620 && b->loc->next == NULL
1621 && b->loc->address == -1
1622 && b->loc->length == -1);
1623}
1624
567e1b4e
JB
1625/* Assuming that B is a watchpoint:
1626 - Reparse watchpoint expression, if REPARSE is non-zero
a5606eee 1627 - Evaluate expression and store the result in B->val
567e1b4e
JB
1628 - Evaluate the condition if there is one, and store the result
1629 in b->loc->cond.
a5606eee
VP
1630 - Update the list of values that must be watched in B->loc.
1631
4a64f543
MS
1632 If the watchpoint disposition is disp_del_at_next_stop, then do
1633 nothing. If this is local watchpoint that is out of scope, delete
1634 it.
1635
1636 Even with `set breakpoint always-inserted on' the watchpoints are
1637 removed + inserted on each stop here. Normal breakpoints must
1638 never be removed because they might be missed by a running thread
1639 when debugging in non-stop mode. On the other hand, hardware
1640 watchpoints (is_hardware_watchpoint; processed here) are specific
1641 to each LWP since they are stored in each LWP's hardware debug
1642 registers. Therefore, such LWP must be stopped first in order to
1643 be able to modify its hardware watchpoints.
1644
1645 Hardware watchpoints must be reset exactly once after being
1646 presented to the user. It cannot be done sooner, because it would
1647 reset the data used to present the watchpoint hit to the user. And
1648 it must not be done later because it could display the same single
1649 watchpoint hit during multiple GDB stops. Note that the latter is
1650 relevant only to the hardware watchpoint types bp_read_watchpoint
1651 and bp_access_watchpoint. False hit by bp_hardware_watchpoint is
1652 not user-visible - its hit is suppressed if the memory content has
1653 not changed.
1654
1655 The following constraints influence the location where we can reset
1656 hardware watchpoints:
1657
1658 * target_stopped_by_watchpoint and target_stopped_data_address are
1659 called several times when GDB stops.
1660
1661 [linux]
1662 * Multiple hardware watchpoints can be hit at the same time,
1663 causing GDB to stop. GDB only presents one hardware watchpoint
1664 hit at a time as the reason for stopping, and all the other hits
1665 are presented later, one after the other, each time the user
1666 requests the execution to be resumed. Execution is not resumed
1667 for the threads still having pending hit event stored in
1668 LWP_INFO->STATUS. While the watchpoint is already removed from
1669 the inferior on the first stop the thread hit event is kept being
1670 reported from its cached value by linux_nat_stopped_data_address
1671 until the real thread resume happens after the watchpoint gets
1672 presented and thus its LWP_INFO->STATUS gets reset.
1673
1674 Therefore the hardware watchpoint hit can get safely reset on the
1675 watchpoint removal from inferior. */
a79d3c27 1676
b40ce68a 1677static void
3a5c3e22 1678update_watchpoint (struct watchpoint *b, int reparse)
7270d8f2 1679{
a5606eee 1680 int within_current_scope;
a5606eee 1681 struct frame_id saved_frame_id;
66076460 1682 int frame_saved;
a5606eee 1683
f6bc2008
PA
1684 /* If this is a local watchpoint, we only want to check if the
1685 watchpoint frame is in scope if the current thread is the thread
1686 that was used to create the watchpoint. */
1687 if (!watchpoint_in_thread_scope (b))
1688 return;
1689
c1fc2657 1690 if (b->disposition == disp_del_at_next_stop)
a5606eee
VP
1691 return;
1692
66076460 1693 frame_saved = 0;
a5606eee
VP
1694
1695 /* Determine if the watchpoint is within scope. */
1696 if (b->exp_valid_block == NULL)
1697 within_current_scope = 1;
1698 else
1699 {
b5db5dfc
UW
1700 struct frame_info *fi = get_current_frame ();
1701 struct gdbarch *frame_arch = get_frame_arch (fi);
1702 CORE_ADDR frame_pc = get_frame_pc (fi);
1703
c9cf6e20
MG
1704 /* If we're at a point where the stack has been destroyed
1705 (e.g. in a function epilogue), unwinding may not work
1706 properly. Do not attempt to recreate locations at this
b5db5dfc 1707 point. See similar comments in watchpoint_check. */
c9cf6e20 1708 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
b5db5dfc 1709 return;
66076460
DJ
1710
1711 /* Save the current frame's ID so we can restore it after
1712 evaluating the watchpoint expression on its own frame. */
1713 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
1714 took a frame parameter, so that we didn't have to change the
1715 selected frame. */
1716 frame_saved = 1;
1717 saved_frame_id = get_frame_id (get_selected_frame (NULL));
1718
a5606eee
VP
1719 fi = frame_find_by_id (b->watchpoint_frame);
1720 within_current_scope = (fi != NULL);
1721 if (within_current_scope)
1722 select_frame (fi);
1723 }
1724
b5db5dfc
UW
1725 /* We don't free locations. They are stored in the bp_location array
1726 and update_global_location_list will eventually delete them and
1727 remove breakpoints if needed. */
c1fc2657 1728 b->loc = NULL;
b5db5dfc 1729
a5606eee
VP
1730 if (within_current_scope && reparse)
1731 {
bbc13ae3 1732 const char *s;
d63d0675 1733
4d01a485 1734 b->exp.reset ();
d63d0675 1735 s = b->exp_string_reparse ? b->exp_string_reparse : b->exp_string;
1bb9788d 1736 b->exp = parse_exp_1 (&s, 0, b->exp_valid_block, 0);
a5606eee
VP
1737 /* If the meaning of expression itself changed, the old value is
1738 no longer relevant. We don't want to report a watchpoint hit
1739 to the user when the old value and the new value may actually
1740 be completely different objects. */
fa4727a6 1741 b->val = NULL;
4c1d86d9 1742 b->val_valid = false;
60e1c644
PA
1743
1744 /* Note that unlike with breakpoints, the watchpoint's condition
1745 expression is stored in the breakpoint object, not in the
1746 locations (re)created below. */
c1fc2657 1747 if (b->cond_string != NULL)
60e1c644 1748 {
4d01a485 1749 b->cond_exp.reset ();
60e1c644 1750
c1fc2657 1751 s = b->cond_string;
1bb9788d 1752 b->cond_exp = parse_exp_1 (&s, 0, b->cond_exp_valid_block, 0);
60e1c644 1753 }
a5606eee 1754 }
a5606eee
VP
1755
1756 /* If we failed to parse the expression, for example because
1757 it refers to a global variable in a not-yet-loaded shared library,
1758 don't try to insert watchpoint. We don't automatically delete
1759 such watchpoint, though, since failure to parse expression
1760 is different from out-of-scope watchpoint. */
e8369a73 1761 if (!target_has_execution)
2d134ed3
PA
1762 {
1763 /* Without execution, memory can't change. No use to try and
1764 set watchpoint locations. The watchpoint will be reset when
1765 the target gains execution, through breakpoint_re_set. */
e8369a73
AB
1766 if (!can_use_hw_watchpoints)
1767 {
c1fc2657
SM
1768 if (b->ops->works_in_software_mode (b))
1769 b->type = bp_watchpoint;
e8369a73 1770 else
638aa5a1
AB
1771 error (_("Can't set read/access watchpoint when "
1772 "hardware watchpoints are disabled."));
e8369a73 1773 }
2d134ed3
PA
1774 }
1775 else if (within_current_scope && b->exp)
a5606eee 1776 {
0cf6dd15 1777 int pc = 0;
a6535de1 1778 std::vector<value_ref_ptr> val_chain;
8d49165d 1779 struct value *v, *result;
2d134ed3 1780 struct program_space *frame_pspace;
a5606eee 1781
4d01a485 1782 fetch_subexp_value (b->exp.get (), &pc, &v, &result, &val_chain, 0);
a5606eee 1783
a5606eee
VP
1784 /* Avoid setting b->val if it's already set. The meaning of
1785 b->val is 'the last value' user saw, and we should update
1786 it only if we reported that last value to user. As it
9c06b0b4
TJB
1787 happens, the code that reports it updates b->val directly.
1788 We don't keep track of the memory value for masked
1789 watchpoints. */
c1fc2657 1790 if (!b->val_valid && !is_masked_watchpoint (b))
fa4727a6 1791 {
bb9d5f81 1792 if (b->val_bitsize != 0)
850645cf
TT
1793 v = extract_bitfield_from_watchpoint_value (b, v);
1794 b->val = release_value (v);
4c1d86d9 1795 b->val_valid = true;
fa4727a6 1796 }
a5606eee 1797
2d134ed3
PA
1798 frame_pspace = get_frame_program_space (get_selected_frame (NULL));
1799
a5606eee 1800 /* Look at each value on the value chain. */
a6535de1
TT
1801 gdb_assert (!val_chain.empty ());
1802 for (const value_ref_ptr &iter : val_chain)
a5606eee 1803 {
a6535de1
TT
1804 v = iter.get ();
1805
a5606eee
VP
1806 /* If it's a memory location, and GDB actually needed
1807 its contents to evaluate the expression, then we
fa4727a6
DJ
1808 must watch it. If the first value returned is
1809 still lazy, that means an error occurred reading it;
1810 watch it anyway in case it becomes readable. */
a5606eee 1811 if (VALUE_LVAL (v) == lval_memory
a6535de1 1812 && (v == val_chain[0] || ! value_lazy (v)))
a5606eee
VP
1813 {
1814 struct type *vtype = check_typedef (value_type (v));
7270d8f2 1815
a5606eee
VP
1816 /* We only watch structs and arrays if user asked
1817 for it explicitly, never if they just happen to
1818 appear in the middle of some value chain. */
fa4727a6 1819 if (v == result
78134374
SM
1820 || (vtype->code () != TYPE_CODE_STRUCT
1821 && vtype->code () != TYPE_CODE_ARRAY))
a5606eee
VP
1822 {
1823 CORE_ADDR addr;
f486487f 1824 enum target_hw_bp_type type;
a5606eee 1825 struct bp_location *loc, **tmp;
bb9d5f81
PP
1826 int bitpos = 0, bitsize = 0;
1827
1828 if (value_bitsize (v) != 0)
1829 {
1830 /* Extract the bit parameters out from the bitfield
1831 sub-expression. */
1832 bitpos = value_bitpos (v);
1833 bitsize = value_bitsize (v);
1834 }
1835 else if (v == result && b->val_bitsize != 0)
1836 {
1837 /* If VAL_BITSIZE != 0 then RESULT is actually a bitfield
1838 lvalue whose bit parameters are saved in the fields
1839 VAL_BITPOS and VAL_BITSIZE. */
1840 bitpos = b->val_bitpos;
1841 bitsize = b->val_bitsize;
1842 }
a5606eee 1843
42ae5230 1844 addr = value_address (v);
bb9d5f81
PP
1845 if (bitsize != 0)
1846 {
1847 /* Skip the bytes that don't contain the bitfield. */
1848 addr += bitpos / 8;
1849 }
1850
a5606eee 1851 type = hw_write;
c1fc2657 1852 if (b->type == bp_read_watchpoint)
a5606eee 1853 type = hw_read;
c1fc2657 1854 else if (b->type == bp_access_watchpoint)
a5606eee 1855 type = hw_access;
3a5c3e22 1856
c1fc2657
SM
1857 loc = allocate_bp_location (b);
1858 for (tmp = &(b->loc); *tmp != NULL; tmp = &((*tmp)->next))
a5606eee
VP
1859 ;
1860 *tmp = loc;
a6d9a66e 1861 loc->gdbarch = get_type_arch (value_type (v));
6c95b8df
PA
1862
1863 loc->pspace = frame_pspace;
f17d9474 1864 loc->address = address_significant (loc->gdbarch, addr);
bb9d5f81
PP
1865
1866 if (bitsize != 0)
1867 {
1868 /* Just cover the bytes that make up the bitfield. */
1869 loc->length = ((bitpos % 8) + bitsize + 7) / 8;
1870 }
1871 else
1872 loc->length = TYPE_LENGTH (value_type (v));
1873
a5606eee
VP
1874 loc->watchpoint_type = type;
1875 }
1876 }
9fa40276
TJB
1877 }
1878
1879 /* Change the type of breakpoint between hardware assisted or
1880 an ordinary watchpoint depending on the hardware support
1881 and free hardware slots. REPARSE is set when the inferior
1882 is started. */
a9634178 1883 if (reparse)
9fa40276 1884 {
e09342b5 1885 int reg_cnt;
9fa40276
TJB
1886 enum bp_loc_type loc_type;
1887 struct bp_location *bl;
a5606eee 1888
a9634178 1889 reg_cnt = can_use_hardware_watchpoint (val_chain);
e09342b5
TJB
1890
1891 if (reg_cnt)
9fa40276
TJB
1892 {
1893 int i, target_resources_ok, other_type_used;
a1398e0c 1894 enum bptype type;
9fa40276 1895
a9634178
TJB
1896 /* Use an exact watchpoint when there's only one memory region to be
1897 watched, and only one debug register is needed to watch it. */
1898 b->exact = target_exact_watchpoints && reg_cnt == 1;
1899
9fa40276 1900 /* We need to determine how many resources are already
e09342b5
TJB
1901 used for all other hardware watchpoints plus this one
1902 to see if we still have enough resources to also fit
a1398e0c
PA
1903 this watchpoint in as well. */
1904
1905 /* If this is a software watchpoint, we try to turn it
1906 to a hardware one -- count resources as if B was of
1907 hardware watchpoint type. */
c1fc2657 1908 type = b->type;
a1398e0c
PA
1909 if (type == bp_watchpoint)
1910 type = bp_hardware_watchpoint;
1911
1912 /* This watchpoint may or may not have been placed on
1913 the list yet at this point (it won't be in the list
1914 if we're trying to create it for the first time,
1915 through watch_command), so always account for it
1916 manually. */
1917
1918 /* Count resources used by all watchpoints except B. */
c1fc2657 1919 i = hw_watchpoint_used_count_others (b, type, &other_type_used);
a1398e0c
PA
1920
1921 /* Add in the resources needed for B. */
c1fc2657 1922 i += hw_watchpoint_use_count (b);
a1398e0c
PA
1923
1924 target_resources_ok
1925 = target_can_use_hardware_watchpoint (type, i, other_type_used);
e09342b5 1926 if (target_resources_ok <= 0)
a9634178 1927 {
c1fc2657 1928 int sw_mode = b->ops->works_in_software_mode (b);
9c06b0b4
TJB
1929
1930 if (target_resources_ok == 0 && !sw_mode)
a9634178
TJB
1931 error (_("Target does not support this type of "
1932 "hardware watchpoint."));
9c06b0b4
TJB
1933 else if (target_resources_ok < 0 && !sw_mode)
1934 error (_("There are not enough available hardware "
1935 "resources for this watchpoint."));
a1398e0c
PA
1936
1937 /* Downgrade to software watchpoint. */
c1fc2657 1938 b->type = bp_watchpoint;
a1398e0c
PA
1939 }
1940 else
1941 {
1942 /* If this was a software watchpoint, we've just
1943 found we have enough resources to turn it to a
1944 hardware watchpoint. Otherwise, this is a
1945 nop. */
c1fc2657 1946 b->type = type;
a9634178 1947 }
9fa40276 1948 }
c1fc2657 1949 else if (!b->ops->works_in_software_mode (b))
638aa5a1
AB
1950 {
1951 if (!can_use_hw_watchpoints)
1952 error (_("Can't set read/access watchpoint when "
1953 "hardware watchpoints are disabled."));
1954 else
1955 error (_("Expression cannot be implemented with "
1956 "read/access watchpoint."));
1957 }
9fa40276 1958 else
c1fc2657 1959 b->type = bp_watchpoint;
9fa40276 1960
c1fc2657 1961 loc_type = (b->type == bp_watchpoint? bp_loc_other
9fa40276 1962 : bp_loc_hardware_watchpoint);
c1fc2657 1963 for (bl = b->loc; bl; bl = bl->next)
9fa40276
TJB
1964 bl->loc_type = loc_type;
1965 }
1966
c7437ca6
PA
1967 /* If a software watchpoint is not watching any memory, then the
1968 above left it without any location set up. But,
1969 bpstat_stop_status requires a location to be able to report
1970 stops, so make sure there's at least a dummy one. */
c1fc2657
SM
1971 if (b->type == bp_watchpoint && b->loc == NULL)
1972 software_watchpoint_add_no_memory_location (b, frame_pspace);
a5606eee
VP
1973 }
1974 else if (!within_current_scope)
7270d8f2 1975 {
ac74f770
MS
1976 printf_filtered (_("\
1977Watchpoint %d deleted because the program has left the block\n\
1978in which its expression is valid.\n"),
c1fc2657 1979 b->number);
d0fb5eae 1980 watchpoint_del_at_next_stop (b);
7270d8f2 1981 }
a5606eee
VP
1982
1983 /* Restore the selected frame. */
66076460
DJ
1984 if (frame_saved)
1985 select_frame (frame_find_by_id (saved_frame_id));
7270d8f2
OF
1986}
1987
a5606eee 1988
74960c60 1989/* Returns 1 iff breakpoint location should be
1e4d1764
YQ
1990 inserted in the inferior. We don't differentiate the type of BL's owner
1991 (breakpoint vs. tracepoint), although insert_location in tracepoint's
1992 breakpoint_ops is not defined, because in insert_bp_location,
1993 tracepoint's insert_location will not be called. */
74960c60 1994static int
35df4500 1995should_be_inserted (struct bp_location *bl)
74960c60 1996{
35df4500 1997 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
74960c60
VP
1998 return 0;
1999
35df4500 2000 if (bl->owner->disposition == disp_del_at_next_stop)
74960c60
VP
2001 return 0;
2002
35df4500 2003 if (!bl->enabled || bl->shlib_disabled || bl->duplicate)
74960c60
VP
2004 return 0;
2005
f8eba3c6
TT
2006 if (user_breakpoint_p (bl->owner) && bl->pspace->executing_startup)
2007 return 0;
2008
56710373
PA
2009 /* This is set for example, when we're attached to the parent of a
2010 vfork, and have detached from the child. The child is running
2011 free, and we expect it to do an exec or exit, at which point the
2012 OS makes the parent schedulable again (and the target reports
2013 that the vfork is done). Until the child is done with the shared
2014 memory region, do not insert breakpoints in the parent, otherwise
2015 the child could still trip on the parent's breakpoints. Since
2016 the parent is blocked anyway, it won't miss any breakpoint. */
35df4500 2017 if (bl->pspace->breakpoints_not_allowed)
56710373
PA
2018 return 0;
2019
31e77af2 2020 /* Don't insert a breakpoint if we're trying to step past its
21edc42f
YQ
2021 location, except if the breakpoint is a single-step breakpoint,
2022 and the breakpoint's thread is the thread which is stepping past
2023 a breakpoint. */
31e77af2
PA
2024 if ((bl->loc_type == bp_loc_software_breakpoint
2025 || bl->loc_type == bp_loc_hardware_breakpoint)
2026 && stepping_past_instruction_at (bl->pspace->aspace,
21edc42f
YQ
2027 bl->address)
2028 /* The single-step breakpoint may be inserted at the location
2029 we're trying to step if the instruction branches to itself.
2030 However, the instruction won't be executed at all and it may
2031 break the semantics of the instruction, for example, the
2032 instruction is a conditional branch or updates some flags.
2033 We can't fix it unless GDB is able to emulate the instruction
2034 or switch to displaced stepping. */
2035 && !(bl->owner->type == bp_single_step
2036 && thread_is_stepping_over_breakpoint (bl->owner->thread)))
e558d7c1
PA
2037 {
2038 if (debug_infrun)
2039 {
2040 fprintf_unfiltered (gdb_stdlog,
2041 "infrun: skipping breakpoint: "
2042 "stepping past insn at: %s\n",
2043 paddress (bl->gdbarch, bl->address));
2044 }
2045 return 0;
2046 }
31e77af2 2047
963f9c80
PA
2048 /* Don't insert watchpoints if we're trying to step past the
2049 instruction that triggered one. */
2050 if ((bl->loc_type == bp_loc_hardware_watchpoint)
2051 && stepping_past_nonsteppable_watchpoint ())
2052 {
2053 if (debug_infrun)
2054 {
2055 fprintf_unfiltered (gdb_stdlog,
2056 "infrun: stepping past non-steppable watchpoint. "
2057 "skipping watchpoint at %s:%d\n",
2058 paddress (bl->gdbarch, bl->address),
2059 bl->length);
2060 }
2061 return 0;
2062 }
2063
74960c60
VP
2064 return 1;
2065}
2066
934709f0
PW
2067/* Same as should_be_inserted but does the check assuming
2068 that the location is not duplicated. */
2069
2070static int
2071unduplicated_should_be_inserted (struct bp_location *bl)
2072{
2073 int result;
2074 const int save_duplicate = bl->duplicate;
2075
2076 bl->duplicate = 0;
2077 result = should_be_inserted (bl);
2078 bl->duplicate = save_duplicate;
2079 return result;
2080}
2081
b775012e
LM
2082/* Parses a conditional described by an expression COND into an
2083 agent expression bytecode suitable for evaluation
2084 by the bytecode interpreter. Return NULL if there was
2085 any error during parsing. */
2086
833177a4 2087static agent_expr_up
b775012e
LM
2088parse_cond_to_aexpr (CORE_ADDR scope, struct expression *cond)
2089{
833177a4 2090 if (cond == NULL)
b775012e
LM
2091 return NULL;
2092
833177a4
PA
2093 agent_expr_up aexpr;
2094
b775012e
LM
2095 /* We don't want to stop processing, so catch any errors
2096 that may show up. */
a70b8144 2097 try
b775012e 2098 {
036e657b 2099 aexpr = gen_eval_for_expr (scope, cond);
b775012e
LM
2100 }
2101
230d2906 2102 catch (const gdb_exception_error &ex)
b775012e
LM
2103 {
2104 /* If we got here, it means the condition could not be parsed to a valid
2105 bytecode expression and thus can't be evaluated on the target's side.
2106 It's no use iterating through the conditions. */
b775012e
LM
2107 }
2108
2109 /* We have a valid agent expression. */
2110 return aexpr;
2111}
2112
2113/* Based on location BL, create a list of breakpoint conditions to be
2114 passed on to the target. If we have duplicated locations with different
2115 conditions, we will add such conditions to the list. The idea is that the
2116 target will evaluate the list of conditions and will only notify GDB when
2117 one of them is true. */
2118
2119static void
2120build_target_condition_list (struct bp_location *bl)
2121{
2122 struct bp_location **locp = NULL, **loc2p;
2123 int null_condition_or_parse_error = 0;
2124 int modified = bl->needs_update;
2125 struct bp_location *loc;
2126
8b4f3082 2127 /* Release conditions left over from a previous insert. */
3cde5c42 2128 bl->target_info.conditions.clear ();
8b4f3082 2129
b775012e
LM
2130 /* This is only meaningful if the target is
2131 evaluating conditions and if the user has
2132 opted for condition evaluation on the target's
2133 side. */
2134 if (gdb_evaluates_breakpoint_condition_p ()
2135 || !target_supports_evaluation_of_breakpoint_conditions ())
2136 return;
2137
2138 /* Do a first pass to check for locations with no assigned
7f32a4d5
PA
2139 conditions or conditions that fail to parse to a valid agent
2140 expression bytecode. If any of these happen, then it's no use to
2141 send conditions to the target since this location will always
2142 trigger and generate a response back to GDB. Note we consider
2143 all locations at the same address irrespective of type, i.e.,
2144 even if the locations aren't considered duplicates (e.g.,
2145 software breakpoint and hardware breakpoint at the same
2146 address). */
b775012e
LM
2147 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2148 {
2149 loc = (*loc2p);
2150 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2151 {
2152 if (modified)
2153 {
b775012e
LM
2154 /* Re-parse the conditions since something changed. In that
2155 case we already freed the condition bytecodes (see
2156 force_breakpoint_reinsertion). We just
2157 need to parse the condition to bytecodes again. */
833177a4
PA
2158 loc->cond_bytecode = parse_cond_to_aexpr (bl->address,
2159 loc->cond.get ());
b775012e
LM
2160 }
2161
2162 /* If we have a NULL bytecode expression, it means something
2163 went wrong or we have a null condition expression. */
2164 if (!loc->cond_bytecode)
2165 {
2166 null_condition_or_parse_error = 1;
2167 break;
2168 }
2169 }
2170 }
2171
2172 /* If any of these happened, it means we will have to evaluate the conditions
2173 for the location's address on gdb's side. It is no use keeping bytecodes
2174 for all the other duplicate locations, thus we free all of them here.
2175
2176 This is so we have a finer control over which locations' conditions are
2177 being evaluated by GDB or the remote stub. */
2178 if (null_condition_or_parse_error)
2179 {
2180 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2181 {
2182 loc = (*loc2p);
2183 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2184 {
2185 /* Only go as far as the first NULL bytecode is
2186 located. */
2187 if (!loc->cond_bytecode)
2188 return;
2189
833177a4 2190 loc->cond_bytecode.reset ();
b775012e
LM
2191 }
2192 }
2193 }
2194
7f32a4d5
PA
2195 /* No NULL conditions or failed bytecode generation. Build a
2196 condition list for this location's address. If we have software
2197 and hardware locations at the same address, they aren't
2198 considered duplicates, but we still marge all the conditions
2199 anyway, as it's simpler, and doesn't really make a practical
2200 difference. */
b775012e
LM
2201 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2202 {
2203 loc = (*loc2p);
2204 if (loc->cond
2205 && is_breakpoint (loc->owner)
2206 && loc->pspace->num == bl->pspace->num
2207 && loc->owner->enable_state == bp_enabled
2208 && loc->enabled)
3cde5c42
PA
2209 {
2210 /* Add the condition to the vector. This will be used later
2211 to send the conditions to the target. */
2212 bl->target_info.conditions.push_back (loc->cond_bytecode.get ());
2213 }
b775012e
LM
2214 }
2215
2216 return;
2217}
2218
d3ce09f5
SS
2219/* Parses a command described by string CMD into an agent expression
2220 bytecode suitable for evaluation by the bytecode interpreter.
2221 Return NULL if there was any error during parsing. */
2222
833177a4 2223static agent_expr_up
d3ce09f5
SS
2224parse_cmd_to_aexpr (CORE_ADDR scope, char *cmd)
2225{
bbc13ae3
KS
2226 const char *cmdrest;
2227 const char *format_start, *format_end;
d3ce09f5
SS
2228 struct gdbarch *gdbarch = get_current_arch ();
2229
833177a4 2230 if (cmd == NULL)
d3ce09f5
SS
2231 return NULL;
2232
2233 cmdrest = cmd;
2234
2235 if (*cmdrest == ',')
2236 ++cmdrest;
f1735a53 2237 cmdrest = skip_spaces (cmdrest);
d3ce09f5
SS
2238
2239 if (*cmdrest++ != '"')
2240 error (_("No format string following the location"));
2241
2242 format_start = cmdrest;
2243
8e481c3b 2244 format_pieces fpieces (&cmdrest);
d3ce09f5
SS
2245
2246 format_end = cmdrest;
2247
2248 if (*cmdrest++ != '"')
2249 error (_("Bad format string, non-terminated '\"'."));
2250
f1735a53 2251 cmdrest = skip_spaces (cmdrest);
d3ce09f5
SS
2252
2253 if (!(*cmdrest == ',' || *cmdrest == '\0'))
2254 error (_("Invalid argument syntax"));
2255
2256 if (*cmdrest == ',')
2257 cmdrest++;
f1735a53 2258 cmdrest = skip_spaces (cmdrest);
d3ce09f5
SS
2259
2260 /* For each argument, make an expression. */
2261
8e481c3b 2262 std::vector<struct expression *> argvec;
d3ce09f5
SS
2263 while (*cmdrest != '\0')
2264 {
bbc13ae3 2265 const char *cmd1;
d3ce09f5
SS
2266
2267 cmd1 = cmdrest;
4d01a485 2268 expression_up expr = parse_exp_1 (&cmd1, scope, block_for_pc (scope), 1);
8e481c3b 2269 argvec.push_back (expr.release ());
d3ce09f5
SS
2270 cmdrest = cmd1;
2271 if (*cmdrest == ',')
2272 ++cmdrest;
2273 }
2274
833177a4
PA
2275 agent_expr_up aexpr;
2276
d3ce09f5
SS
2277 /* We don't want to stop processing, so catch any errors
2278 that may show up. */
a70b8144 2279 try
d3ce09f5 2280 {
036e657b
JB
2281 aexpr = gen_printf (scope, gdbarch, 0, 0,
2282 format_start, format_end - format_start,
8e481c3b 2283 argvec.size (), argvec.data ());
d3ce09f5 2284 }
230d2906 2285 catch (const gdb_exception_error &ex)
d3ce09f5
SS
2286 {
2287 /* If we got here, it means the command could not be parsed to a valid
2288 bytecode expression and thus can't be evaluated on the target's side.
2289 It's no use iterating through the other commands. */
d3ce09f5 2290 }
492d29ea 2291
d3ce09f5
SS
2292 /* We have a valid agent expression, return it. */
2293 return aexpr;
2294}
2295
2296/* Based on location BL, create a list of breakpoint commands to be
2297 passed on to the target. If we have duplicated locations with
2298 different commands, we will add any such to the list. */
2299
2300static void
2301build_target_command_list (struct bp_location *bl)
2302{
2303 struct bp_location **locp = NULL, **loc2p;
2304 int null_command_or_parse_error = 0;
2305 int modified = bl->needs_update;
2306 struct bp_location *loc;
2307
3cde5c42
PA
2308 /* Clear commands left over from a previous insert. */
2309 bl->target_info.tcommands.clear ();
8b4f3082 2310
41fac0cf 2311 if (!target_can_run_breakpoint_commands ())
d3ce09f5
SS
2312 return;
2313
41fac0cf
PA
2314 /* For now, limit to agent-style dprintf breakpoints. */
2315 if (dprintf_style != dprintf_style_agent)
d3ce09f5
SS
2316 return;
2317
7f32a4d5
PA
2318 /* For now, if we have any location at the same address that isn't a
2319 dprintf, don't install the target-side commands, as that would
2320 make the breakpoint not be reported to the core, and we'd lose
41fac0cf
PA
2321 control. */
2322 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2323 {
2324 loc = (*loc2p);
2325 if (is_breakpoint (loc->owner)
2326 && loc->pspace->num == bl->pspace->num
2327 && loc->owner->type != bp_dprintf)
2328 return;
2329 }
2330
d3ce09f5
SS
2331 /* Do a first pass to check for locations with no assigned
2332 conditions or conditions that fail to parse to a valid agent expression
2333 bytecode. If any of these happen, then it's no use to send conditions
2334 to the target since this location will always trigger and generate a
2335 response back to GDB. */
2336 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2337 {
2338 loc = (*loc2p);
2339 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2340 {
2341 if (modified)
2342 {
d3ce09f5
SS
2343 /* Re-parse the commands since something changed. In that
2344 case we already freed the command bytecodes (see
2345 force_breakpoint_reinsertion). We just
2346 need to parse the command to bytecodes again. */
833177a4
PA
2347 loc->cmd_bytecode
2348 = parse_cmd_to_aexpr (bl->address,
2349 loc->owner->extra_string);
d3ce09f5
SS
2350 }
2351
2352 /* If we have a NULL bytecode expression, it means something
2353 went wrong or we have a null command expression. */
2354 if (!loc->cmd_bytecode)
2355 {
2356 null_command_or_parse_error = 1;
2357 break;
2358 }
2359 }
2360 }
2361
2362 /* If anything failed, then we're not doing target-side commands,
2363 and so clean up. */
2364 if (null_command_or_parse_error)
2365 {
2366 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2367 {
2368 loc = (*loc2p);
2369 if (is_breakpoint (loc->owner)
2370 && loc->pspace->num == bl->pspace->num)
2371 {
2372 /* Only go as far as the first NULL bytecode is
2373 located. */
40fb6c5e 2374 if (loc->cmd_bytecode == NULL)
d3ce09f5
SS
2375 return;
2376
833177a4 2377 loc->cmd_bytecode.reset ();
d3ce09f5
SS
2378 }
2379 }
2380 }
2381
7f32a4d5
PA
2382 /* No NULL commands or failed bytecode generation. Build a command
2383 list for all duplicate locations at this location's address.
2384 Note that here we must care for whether the breakpoint location
2385 types are considered duplicates, otherwise, say, if we have a
2386 software and hardware location at the same address, the target
2387 could end up running the commands twice. For the moment, we only
2388 support targets-side commands with dprintf, but it doesn't hurt
2389 to be pedantically correct in case that changes. */
d3ce09f5
SS
2390 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2391 {
2392 loc = (*loc2p);
7f32a4d5
PA
2393 if (breakpoint_locations_match (bl, loc)
2394 && loc->owner->extra_string
d3ce09f5
SS
2395 && is_breakpoint (loc->owner)
2396 && loc->pspace->num == bl->pspace->num
2397 && loc->owner->enable_state == bp_enabled
2398 && loc->enabled)
3cde5c42
PA
2399 {
2400 /* Add the command to the vector. This will be used later
2401 to send the commands to the target. */
2402 bl->target_info.tcommands.push_back (loc->cmd_bytecode.get ());
2403 }
d3ce09f5
SS
2404 }
2405
2406 bl->target_info.persist = 0;
2407 /* Maybe flag this location as persistent. */
2408 if (bl->owner->type == bp_dprintf && disconnected_dprintf)
2409 bl->target_info.persist = 1;
2410}
2411
833b7ab5
YQ
2412/* Return the kind of breakpoint on address *ADDR. Get the kind
2413 of breakpoint according to ADDR except single-step breakpoint.
2414 Get the kind of single-step breakpoint according to the current
2415 registers state. */
cd6c3b4f
YQ
2416
2417static int
2418breakpoint_kind (struct bp_location *bl, CORE_ADDR *addr)
2419{
833b7ab5
YQ
2420 if (bl->owner->type == bp_single_step)
2421 {
2422 struct thread_info *thr = find_thread_global_id (bl->owner->thread);
2423 struct regcache *regcache;
2424
00431a78 2425 regcache = get_thread_regcache (thr);
833b7ab5
YQ
2426
2427 return gdbarch_breakpoint_kind_from_current_state (bl->gdbarch,
2428 regcache, addr);
2429 }
2430 else
2431 return gdbarch_breakpoint_kind_from_pc (bl->gdbarch, addr);
cd6c3b4f
YQ
2432}
2433
35df4500
TJB
2434/* Insert a low-level "breakpoint" of some type. BL is the breakpoint
2435 location. Any error messages are printed to TMP_ERROR_STREAM; and
3fbb6ffa 2436 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
c30eee59
TJB
2437 Returns 0 for success, 1 if the bp_location type is not supported or
2438 -1 for failure.
879bfdc2 2439
4a64f543
MS
2440 NOTE drow/2003-09-09: This routine could be broken down to an
2441 object-style method for each breakpoint or catchpoint type. */
26bb91f3 2442static int
35df4500 2443insert_bp_location (struct bp_location *bl,
26bb91f3 2444 struct ui_file *tmp_error_stream,
3fbb6ffa 2445 int *disabled_breaks,
dd61ec5c
MW
2446 int *hw_breakpoint_error,
2447 int *hw_bp_error_explained_already)
879bfdc2 2448{
cc06b668 2449 gdb_exception bp_excpt;
879bfdc2 2450
b775012e 2451 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
879bfdc2
DJ
2452 return 0;
2453
35c63cd8
JB
2454 /* Note we don't initialize bl->target_info, as that wipes out
2455 the breakpoint location's shadow_contents if the breakpoint
2456 is still inserted at that location. This in turn breaks
2457 target_read_memory which depends on these buffers when
2458 a memory read is requested at the breakpoint location:
2459 Once the target_info has been wiped, we fail to see that
2460 we have a breakpoint inserted at that address and thus
2461 read the breakpoint instead of returning the data saved in
2462 the breakpoint location's shadow contents. */
0d5ed153 2463 bl->target_info.reqstd_address = bl->address;
35df4500 2464 bl->target_info.placed_address_space = bl->pspace->aspace;
f1310107 2465 bl->target_info.length = bl->length;
8181d85f 2466
b775012e
LM
2467 /* When working with target-side conditions, we must pass all the conditions
2468 for the same breakpoint address down to the target since GDB will not
2469 insert those locations. With a list of breakpoint conditions, the target
2470 can decide when to stop and notify GDB. */
2471
2472 if (is_breakpoint (bl->owner))
2473 {
2474 build_target_condition_list (bl);
d3ce09f5
SS
2475 build_target_command_list (bl);
2476 /* Reset the modification marker. */
b775012e
LM
2477 bl->needs_update = 0;
2478 }
2479
7f32a4d5
PA
2480 /* If "set breakpoint auto-hw" is "on" and a software breakpoint was
2481 set at a read-only address, then a breakpoint location will have
2482 been changed to hardware breakpoint before we get here. If it is
2483 "off" however, error out before actually trying to insert the
2484 breakpoint, with a nicer error message. */
35df4500 2485 if (bl->loc_type == bp_loc_software_breakpoint
7f32a4d5 2486 && !automatic_hardware_breakpoints)
879bfdc2 2487 {
7f32a4d5 2488 mem_region *mr = lookup_mem_region (bl->address);
cc59ec59 2489
7f32a4d5
PA
2490 if (mr != nullptr && mr->attrib.mode != MEM_RW)
2491 {
2492 fprintf_unfiltered (tmp_error_stream,
2493 _("Cannot insert breakpoint %d.\n"
2494 "Cannot set software breakpoint "
2495 "at read-only address %s\n"),
2496 bl->owner->number,
2497 paddress (bl->gdbarch, bl->address));
2498 return 1;
765dc015 2499 }
7f32a4d5
PA
2500 }
2501
2502 if (bl->loc_type == bp_loc_software_breakpoint
2503 || bl->loc_type == bp_loc_hardware_breakpoint)
2504 {
879bfdc2
DJ
2505 /* First check to see if we have to handle an overlay. */
2506 if (overlay_debugging == ovly_off
35df4500
TJB
2507 || bl->section == NULL
2508 || !(section_is_overlay (bl->section)))
879bfdc2
DJ
2509 {
2510 /* No overlay handling: just set the breakpoint. */
a70b8144 2511 try
dd61ec5c 2512 {
0000e5cc
PA
2513 int val;
2514
dd61ec5c 2515 val = bl->owner->ops->insert_location (bl);
0000e5cc 2516 if (val)
688fca4f 2517 bp_excpt = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
dd61ec5c 2518 }
94aeb44b 2519 catch (gdb_exception &e)
dd61ec5c 2520 {
94aeb44b 2521 bp_excpt = std::move (e);
dd61ec5c 2522 }
879bfdc2
DJ
2523 }
2524 else
2525 {
4a64f543 2526 /* This breakpoint is in an overlay section.
879bfdc2
DJ
2527 Shall we set a breakpoint at the LMA? */
2528 if (!overlay_events_enabled)
2529 {
2530 /* Yes -- overlay event support is not active,
2531 so we must try to set a breakpoint at the LMA.
2532 This will not work for a hardware breakpoint. */
35df4500 2533 if (bl->loc_type == bp_loc_hardware_breakpoint)
8a3fe4f8 2534 warning (_("hardware breakpoint %d not supported in overlay!"),
35df4500 2535 bl->owner->number);
879bfdc2
DJ
2536 else
2537 {
35df4500
TJB
2538 CORE_ADDR addr = overlay_unmapped_address (bl->address,
2539 bl->section);
879bfdc2 2540 /* Set a software (trap) breakpoint at the LMA. */
35df4500 2541 bl->overlay_target_info = bl->target_info;
0d5ed153 2542 bl->overlay_target_info.reqstd_address = addr;
0000e5cc
PA
2543
2544 /* No overlay handling: just set the breakpoint. */
a70b8144 2545 try
0000e5cc
PA
2546 {
2547 int val;
2548
579c6ad9 2549 bl->overlay_target_info.kind
cd6c3b4f
YQ
2550 = breakpoint_kind (bl, &addr);
2551 bl->overlay_target_info.placed_address = addr;
0000e5cc
PA
2552 val = target_insert_breakpoint (bl->gdbarch,
2553 &bl->overlay_target_info);
2554 if (val)
688fca4f
PA
2555 bp_excpt
2556 = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
0000e5cc 2557 }
94aeb44b 2558 catch (gdb_exception &e)
0000e5cc 2559 {
94aeb44b 2560 bp_excpt = std::move (e);
0000e5cc
PA
2561 }
2562
688fca4f 2563 if (bp_excpt.reason != 0)
99361f52 2564 fprintf_unfiltered (tmp_error_stream,
3e43a32a
MS
2565 "Overlay breakpoint %d "
2566 "failed: in ROM?\n",
35df4500 2567 bl->owner->number);
879bfdc2
DJ
2568 }
2569 }
2570 /* Shall we set a breakpoint at the VMA? */
35df4500 2571 if (section_is_mapped (bl->section))
879bfdc2
DJ
2572 {
2573 /* Yes. This overlay section is mapped into memory. */
a70b8144 2574 try
dd61ec5c 2575 {
0000e5cc
PA
2576 int val;
2577
dd61ec5c 2578 val = bl->owner->ops->insert_location (bl);
0000e5cc 2579 if (val)
688fca4f 2580 bp_excpt = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
dd61ec5c 2581 }
94aeb44b 2582 catch (gdb_exception &e)
dd61ec5c 2583 {
94aeb44b 2584 bp_excpt = std::move (e);
dd61ec5c 2585 }
879bfdc2
DJ
2586 }
2587 else
2588 {
2589 /* No. This breakpoint will not be inserted.
2590 No error, but do not mark the bp as 'inserted'. */
2591 return 0;
2592 }
2593 }
2594
688fca4f 2595 if (bp_excpt.reason != 0)
879bfdc2
DJ
2596 {
2597 /* Can't set the breakpoint. */
0000e5cc
PA
2598
2599 /* In some cases, we might not be able to insert a
2600 breakpoint in a shared library that has already been
2601 removed, but we have not yet processed the shlib unload
2602 event. Unfortunately, some targets that implement
076855f9
PA
2603 breakpoint insertion themselves can't tell why the
2604 breakpoint insertion failed (e.g., the remote target
2605 doesn't define error codes), so we must treat generic
2606 errors as memory errors. */
688fca4f
PA
2607 if (bp_excpt.reason == RETURN_ERROR
2608 && (bp_excpt.error == GENERIC_ERROR
2609 || bp_excpt.error == MEMORY_ERROR)
076855f9 2610 && bl->loc_type == bp_loc_software_breakpoint
08351840 2611 && (solib_name_from_address (bl->pspace, bl->address)
d03de421
PA
2612 || shared_objfile_contains_address_p (bl->pspace,
2613 bl->address)))
879bfdc2 2614 {
4a64f543 2615 /* See also: disable_breakpoints_in_shlibs. */
35df4500 2616 bl->shlib_disabled = 1;
76727919 2617 gdb::observers::breakpoint_modified.notify (bl->owner);
3fbb6ffa
TJB
2618 if (!*disabled_breaks)
2619 {
2620 fprintf_unfiltered (tmp_error_stream,
2621 "Cannot insert breakpoint %d.\n",
2622 bl->owner->number);
2623 fprintf_unfiltered (tmp_error_stream,
2624 "Temporarily disabling shared "
2625 "library breakpoints:\n");
2626 }
2627 *disabled_breaks = 1;
879bfdc2 2628 fprintf_unfiltered (tmp_error_stream,
35df4500 2629 "breakpoint #%d\n", bl->owner->number);
0000e5cc 2630 return 0;
879bfdc2
DJ
2631 }
2632 else
879bfdc2 2633 {
35df4500 2634 if (bl->loc_type == bp_loc_hardware_breakpoint)
879bfdc2 2635 {
0000e5cc 2636 *hw_breakpoint_error = 1;
688fca4f 2637 *hw_bp_error_explained_already = bp_excpt.message != NULL;
dd61ec5c
MW
2638 fprintf_unfiltered (tmp_error_stream,
2639 "Cannot insert hardware breakpoint %d%s",
688fca4f
PA
2640 bl->owner->number,
2641 bp_excpt.message ? ":" : ".\n");
2642 if (bp_excpt.message != NULL)
2643 fprintf_unfiltered (tmp_error_stream, "%s.\n",
3d6e9d23 2644 bp_excpt.what ());
879bfdc2
DJ
2645 }
2646 else
2647 {
688fca4f 2648 if (bp_excpt.message == NULL)
0000e5cc 2649 {
1ccbe998 2650 std::string message
0000e5cc
PA
2651 = memory_error_message (TARGET_XFER_E_IO,
2652 bl->gdbarch, bl->address);
0000e5cc
PA
2653
2654 fprintf_unfiltered (tmp_error_stream,
2655 "Cannot insert breakpoint %d.\n"
2656 "%s\n",
1ccbe998 2657 bl->owner->number, message.c_str ());
0000e5cc
PA
2658 }
2659 else
2660 {
2661 fprintf_unfiltered (tmp_error_stream,
2662 "Cannot insert breakpoint %d: %s\n",
2663 bl->owner->number,
3d6e9d23 2664 bp_excpt.what ());
0000e5cc 2665 }
879bfdc2 2666 }
0000e5cc 2667 return 1;
879bfdc2
DJ
2668
2669 }
2670 }
2671 else
35df4500 2672 bl->inserted = 1;
879bfdc2 2673
0000e5cc 2674 return 0;
879bfdc2
DJ
2675 }
2676
35df4500 2677 else if (bl->loc_type == bp_loc_hardware_watchpoint
879bfdc2 2678 /* NOTE drow/2003-09-08: This state only exists for removing
4a64f543 2679 watchpoints. It's not clear that it's necessary... */
35df4500 2680 && bl->owner->disposition != disp_del_at_next_stop)
879bfdc2 2681 {
0000e5cc
PA
2682 int val;
2683
77b06cd7
TJB
2684 gdb_assert (bl->owner->ops != NULL
2685 && bl->owner->ops->insert_location != NULL);
2686
2687 val = bl->owner->ops->insert_location (bl);
85d721b8
PA
2688
2689 /* If trying to set a read-watchpoint, and it turns out it's not
2690 supported, try emulating one with an access watchpoint. */
35df4500 2691 if (val == 1 && bl->watchpoint_type == hw_read)
85d721b8
PA
2692 {
2693 struct bp_location *loc, **loc_temp;
2694
2695 /* But don't try to insert it, if there's already another
2696 hw_access location that would be considered a duplicate
2697 of this one. */
2698 ALL_BP_LOCATIONS (loc, loc_temp)
35df4500 2699 if (loc != bl
85d721b8 2700 && loc->watchpoint_type == hw_access
35df4500 2701 && watchpoint_locations_match (bl, loc))
85d721b8 2702 {
35df4500
TJB
2703 bl->duplicate = 1;
2704 bl->inserted = 1;
2705 bl->target_info = loc->target_info;
2706 bl->watchpoint_type = hw_access;
85d721b8
PA
2707 val = 0;
2708 break;
2709 }
2710
2711 if (val == 1)
2712 {
77b06cd7
TJB
2713 bl->watchpoint_type = hw_access;
2714 val = bl->owner->ops->insert_location (bl);
2715
2716 if (val)
2717 /* Back to the original value. */
2718 bl->watchpoint_type = hw_read;
85d721b8
PA
2719 }
2720 }
2721
35df4500 2722 bl->inserted = (val == 0);
879bfdc2
DJ
2723 }
2724
35df4500 2725 else if (bl->owner->type == bp_catchpoint)
879bfdc2 2726 {
0000e5cc
PA
2727 int val;
2728
77b06cd7
TJB
2729 gdb_assert (bl->owner->ops != NULL
2730 && bl->owner->ops->insert_location != NULL);
2731
2732 val = bl->owner->ops->insert_location (bl);
2733 if (val)
2734 {
2735 bl->owner->enable_state = bp_disabled;
2736
2737 if (val == 1)
2738 warning (_("\
2739Error inserting catchpoint %d: Your system does not support this type\n\
2740of catchpoint."), bl->owner->number);
2741 else
2742 warning (_("Error inserting catchpoint %d."), bl->owner->number);
2743 }
2744
2745 bl->inserted = (val == 0);
1640b821
DJ
2746
2747 /* We've already printed an error message if there was a problem
2748 inserting this catchpoint, and we've disabled the catchpoint,
2749 so just return success. */
2750 return 0;
879bfdc2
DJ
2751 }
2752
2753 return 0;
2754}
2755
6c95b8df
PA
2756/* This function is called when program space PSPACE is about to be
2757 deleted. It takes care of updating breakpoints to not reference
2758 PSPACE anymore. */
2759
2760void
2761breakpoint_program_space_exit (struct program_space *pspace)
2762{
2763 struct breakpoint *b, *b_temp;
876fa593 2764 struct bp_location *loc, **loc_temp;
6c95b8df
PA
2765
2766 /* Remove any breakpoint that was set through this program space. */
2767 ALL_BREAKPOINTS_SAFE (b, b_temp)
2768 {
2769 if (b->pspace == pspace)
2770 delete_breakpoint (b);
2771 }
2772
2773 /* Breakpoints set through other program spaces could have locations
2774 bound to PSPACE as well. Remove those. */
876fa593 2775 ALL_BP_LOCATIONS (loc, loc_temp)
6c95b8df
PA
2776 {
2777 struct bp_location *tmp;
2778
2779 if (loc->pspace == pspace)
2780 {
2bdf28a0 2781 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
6c95b8df
PA
2782 if (loc->owner->loc == loc)
2783 loc->owner->loc = loc->next;
2784 else
2785 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
2786 if (tmp->next == loc)
2787 {
2788 tmp->next = loc->next;
2789 break;
2790 }
2791 }
2792 }
2793
2794 /* Now update the global location list to permanently delete the
2795 removed locations above. */
44702360 2796 update_global_location_list (UGLL_DONT_INSERT);
6c95b8df
PA
2797}
2798
74960c60
VP
2799/* Make sure all breakpoints are inserted in inferior.
2800 Throws exception on any error.
2801 A breakpoint that is already inserted won't be inserted
2802 again, so calling this function twice is safe. */
2803void
2804insert_breakpoints (void)
2805{
2806 struct breakpoint *bpt;
2807
2808 ALL_BREAKPOINTS (bpt)
2809 if (is_hardware_watchpoint (bpt))
3a5c3e22
PA
2810 {
2811 struct watchpoint *w = (struct watchpoint *) bpt;
2812
2813 update_watchpoint (w, 0 /* don't reparse. */);
2814 }
74960c60 2815
04086b45
PA
2816 /* Updating watchpoints creates new locations, so update the global
2817 location list. Explicitly tell ugll to insert locations and
7f32a4d5
PA
2818 ignore breakpoints_always_inserted_mode. Also,
2819 update_global_location_list tries to "upgrade" software
2820 breakpoints to hardware breakpoints to handle "set breakpoint
2821 auto-hw", so we need to call it even if we don't have new
2822 locations. */
04086b45 2823 update_global_location_list (UGLL_INSERT);
74960c60
VP
2824}
2825
20388dd6
YQ
2826/* Invoke CALLBACK for each of bp_location. */
2827
2828void
2829iterate_over_bp_locations (walk_bp_location_callback callback)
2830{
2831 struct bp_location *loc, **loc_tmp;
2832
2833 ALL_BP_LOCATIONS (loc, loc_tmp)
2834 {
2835 callback (loc, NULL);
2836 }
2837}
2838
b775012e
LM
2839/* This is used when we need to synch breakpoint conditions between GDB and the
2840 target. It is the case with deleting and disabling of breakpoints when using
2841 always-inserted mode. */
2842
2843static void
2844update_inserted_breakpoint_locations (void)
2845{
2846 struct bp_location *bl, **blp_tmp;
2847 int error_flag = 0;
2848 int val = 0;
2849 int disabled_breaks = 0;
2850 int hw_breakpoint_error = 0;
dd61ec5c 2851 int hw_bp_details_reported = 0;
b775012e 2852
d7e74731 2853 string_file tmp_error_stream;
b775012e
LM
2854
2855 /* Explicitly mark the warning -- this will only be printed if
2856 there was an error. */
d7e74731 2857 tmp_error_stream.puts ("Warning:\n");
b775012e 2858
5ed8105e 2859 scoped_restore_current_pspace_and_thread restore_pspace_thread;
b775012e
LM
2860
2861 ALL_BP_LOCATIONS (bl, blp_tmp)
2862 {
2863 /* We only want to update software breakpoints and hardware
2864 breakpoints. */
2865 if (!is_breakpoint (bl->owner))
2866 continue;
2867
2868 /* We only want to update locations that are already inserted
2869 and need updating. This is to avoid unwanted insertion during
2870 deletion of breakpoints. */
4daf1902 2871 if (!bl->inserted || !bl->needs_update)
b775012e
LM
2872 continue;
2873
2874 switch_to_program_space_and_thread (bl->pspace);
2875
2876 /* For targets that support global breakpoints, there's no need
2877 to select an inferior to insert breakpoint to. In fact, even
2878 if we aren't attached to any process yet, we should still
2879 insert breakpoints. */
f5656ead 2880 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
5b6d1e4f 2881 && (inferior_ptid == null_ptid || !target_has_execution))
b775012e
LM
2882 continue;
2883
d7e74731 2884 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
dd61ec5c 2885 &hw_breakpoint_error, &hw_bp_details_reported);
b775012e
LM
2886 if (val)
2887 error_flag = val;
2888 }
2889
2890 if (error_flag)
2891 {
223ffa71 2892 target_terminal::ours_for_output ();
b775012e
LM
2893 error_stream (tmp_error_stream);
2894 }
b775012e
LM
2895}
2896
c30eee59 2897/* Used when starting or continuing the program. */
c906108c 2898
74960c60
VP
2899static void
2900insert_breakpoint_locations (void)
c906108c 2901{
a5606eee 2902 struct breakpoint *bpt;
35df4500 2903 struct bp_location *bl, **blp_tmp;
eacd795a 2904 int error_flag = 0;
c906108c 2905 int val = 0;
3fbb6ffa 2906 int disabled_breaks = 0;
81d0cc19 2907 int hw_breakpoint_error = 0;
dd61ec5c 2908 int hw_bp_error_explained_already = 0;
c906108c 2909
d7e74731
PA
2910 string_file tmp_error_stream;
2911
81d0cc19
GS
2912 /* Explicitly mark the warning -- this will only be printed if
2913 there was an error. */
d7e74731 2914 tmp_error_stream.puts ("Warning:\n");
6c95b8df 2915
5ed8105e 2916 scoped_restore_current_pspace_and_thread restore_pspace_thread;
6c95b8df 2917
35df4500 2918 ALL_BP_LOCATIONS (bl, blp_tmp)
879bfdc2 2919 {
b775012e 2920 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
879bfdc2
DJ
2921 continue;
2922
4a64f543
MS
2923 /* There is no point inserting thread-specific breakpoints if
2924 the thread no longer exists. ALL_BP_LOCATIONS bp_location
2925 has BL->OWNER always non-NULL. */
35df4500 2926 if (bl->owner->thread != -1
5d5658a1 2927 && !valid_global_thread_id (bl->owner->thread))
f365de73
AS
2928 continue;
2929
35df4500 2930 switch_to_program_space_and_thread (bl->pspace);
6c95b8df
PA
2931
2932 /* For targets that support global breakpoints, there's no need
2933 to select an inferior to insert breakpoint to. In fact, even
2934 if we aren't attached to any process yet, we should still
2935 insert breakpoints. */
f5656ead 2936 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
5b6d1e4f 2937 && (inferior_ptid == null_ptid || !target_has_execution))
6c95b8df
PA
2938 continue;
2939
d7e74731 2940 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
dd61ec5c 2941 &hw_breakpoint_error, &hw_bp_error_explained_already);
879bfdc2 2942 if (val)
eacd795a 2943 error_flag = val;
879bfdc2 2944 }
c906108c 2945
4a64f543
MS
2946 /* If we failed to insert all locations of a watchpoint, remove
2947 them, as half-inserted watchpoint is of limited use. */
a5606eee
VP
2948 ALL_BREAKPOINTS (bpt)
2949 {
2950 int some_failed = 0;
2951 struct bp_location *loc;
2952
2953 if (!is_hardware_watchpoint (bpt))
2954 continue;
2955
d6b74ac4 2956 if (!breakpoint_enabled (bpt))
a5606eee 2957 continue;
74960c60
VP
2958
2959 if (bpt->disposition == disp_del_at_next_stop)
2960 continue;
a5606eee
VP
2961
2962 for (loc = bpt->loc; loc; loc = loc->next)
56710373 2963 if (!loc->inserted && should_be_inserted (loc))
a5606eee
VP
2964 {
2965 some_failed = 1;
2966 break;
2967 }
2968 if (some_failed)
2969 {
2970 for (loc = bpt->loc; loc; loc = loc->next)
2971 if (loc->inserted)
834c0d03 2972 remove_breakpoint (loc);
a5606eee
VP
2973
2974 hw_breakpoint_error = 1;
d7e74731
PA
2975 tmp_error_stream.printf ("Could not insert "
2976 "hardware watchpoint %d.\n",
2977 bpt->number);
eacd795a 2978 error_flag = -1;
a5606eee
VP
2979 }
2980 }
2981
eacd795a 2982 if (error_flag)
81d0cc19
GS
2983 {
2984 /* If a hardware breakpoint or watchpoint was inserted, add a
2985 message about possibly exhausted resources. */
dd61ec5c 2986 if (hw_breakpoint_error && !hw_bp_error_explained_already)
81d0cc19 2987 {
d7e74731 2988 tmp_error_stream.printf ("Could not insert hardware breakpoints:\n\
c6510018 2989You may have requested too many hardware breakpoints/watchpoints.\n");
81d0cc19 2990 }
223ffa71 2991 target_terminal::ours_for_output ();
81d0cc19
GS
2992 error_stream (tmp_error_stream);
2993 }
c906108c
SS
2994}
2995
c30eee59
TJB
2996/* Used when the program stops.
2997 Returns zero if successful, or non-zero if there was a problem
2998 removing a breakpoint location. */
2999
c906108c 3000int
fba45db2 3001remove_breakpoints (void)
c906108c 3002{
35df4500 3003 struct bp_location *bl, **blp_tmp;
3a1bae8e 3004 int val = 0;
c906108c 3005
35df4500 3006 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3007 {
1e4d1764 3008 if (bl->inserted && !is_tracepoint (bl->owner))
834c0d03 3009 val |= remove_breakpoint (bl);
c5aa993b 3010 }
3a1bae8e 3011 return val;
c906108c
SS
3012}
3013
49fa26b0
PA
3014/* When a thread exits, remove breakpoints that are related to
3015 that thread. */
3016
3017static void
3018remove_threaded_breakpoints (struct thread_info *tp, int silent)
3019{
3020 struct breakpoint *b, *b_tmp;
3021
3022 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3023 {
5d5658a1 3024 if (b->thread == tp->global_num && user_breakpoint_p (b))
49fa26b0
PA
3025 {
3026 b->disposition = disp_del_at_next_stop;
3027
3028 printf_filtered (_("\
43792cf0
PA
3029Thread-specific breakpoint %d deleted - thread %s no longer in the thread list.\n"),
3030 b->number, print_thread_id (tp));
49fa26b0
PA
3031
3032 /* Hide it from the user. */
3033 b->number = 0;
3034 }
3035 }
3036}
3037
f3869b1a 3038/* See breakpoint.h. */
6c95b8df 3039
f3869b1a 3040void
00431a78 3041remove_breakpoints_inf (inferior *inf)
6c95b8df 3042{
35df4500 3043 struct bp_location *bl, **blp_tmp;
6c95b8df 3044 int val;
6c95b8df 3045
35df4500 3046 ALL_BP_LOCATIONS (bl, blp_tmp)
6c95b8df 3047 {
35df4500 3048 if (bl->pspace != inf->pspace)
6c95b8df
PA
3049 continue;
3050
fc126975 3051 if (bl->inserted && !bl->target_info.persist)
6c95b8df 3052 {
834c0d03 3053 val = remove_breakpoint (bl);
6c95b8df 3054 if (val != 0)
f3869b1a 3055 return;
6c95b8df
PA
3056 }
3057 }
6c95b8df
PA
3058}
3059
e58b0e63
PA
3060static int internal_breakpoint_number = -1;
3061
84f4c1fe
PM
3062/* Set the breakpoint number of B, depending on the value of INTERNAL.
3063 If INTERNAL is non-zero, the breakpoint number will be populated
3064 from internal_breakpoint_number and that variable decremented.
e5dd4106 3065 Otherwise the breakpoint number will be populated from
84f4c1fe
PM
3066 breakpoint_count and that value incremented. Internal breakpoints
3067 do not set the internal var bpnum. */
3068static void
3069set_breakpoint_number (int internal, struct breakpoint *b)
3070{
3071 if (internal)
3072 b->number = internal_breakpoint_number--;
3073 else
3074 {
3075 set_breakpoint_count (breakpoint_count + 1);
3076 b->number = breakpoint_count;
3077 }
3078}
3079
e62c965a 3080static struct breakpoint *
a6d9a66e 3081create_internal_breakpoint (struct gdbarch *gdbarch,
06edf0c0 3082 CORE_ADDR address, enum bptype type,
c0a91b2b 3083 const struct breakpoint_ops *ops)
e62c965a 3084{
51abb421 3085 symtab_and_line sal;
e62c965a
PP
3086 sal.pc = address;
3087 sal.section = find_pc_overlay (sal.pc);
6c95b8df 3088 sal.pspace = current_program_space;
e62c965a 3089
51abb421 3090 breakpoint *b = set_raw_breakpoint (gdbarch, sal, type, ops);
e62c965a
PP
3091 b->number = internal_breakpoint_number--;
3092 b->disposition = disp_donttouch;
3093
3094 return b;
3095}
3096
17450429
PP
3097static const char *const longjmp_names[] =
3098 {
3099 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
3100 };
3101#define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
3102
3103/* Per-objfile data private to breakpoint.c. */
3104struct breakpoint_objfile_data
3105{
3106 /* Minimal symbol for "_ovly_debug_event" (if any). */
43dce439 3107 struct bound_minimal_symbol overlay_msym {};
17450429
PP
3108
3109 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
43dce439 3110 struct bound_minimal_symbol longjmp_msym[NUM_LONGJMP_NAMES] {};
17450429 3111
28106bc2 3112 /* True if we have looked for longjmp probes. */
43dce439 3113 int longjmp_searched = 0;
28106bc2 3114
45461e0d
SM
3115 /* SystemTap probe points for longjmp (if any). These are non-owning
3116 references. */
3117 std::vector<probe *> longjmp_probes;
28106bc2 3118
17450429 3119 /* Minimal symbol for "std::terminate()" (if any). */
43dce439 3120 struct bound_minimal_symbol terminate_msym {};
17450429
PP
3121
3122 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
43dce439 3123 struct bound_minimal_symbol exception_msym {};
28106bc2
SDJ
3124
3125 /* True if we have looked for exception probes. */
43dce439 3126 int exception_searched = 0;
28106bc2 3127
45461e0d
SM
3128 /* SystemTap probe points for unwinding (if any). These are non-owning
3129 references. */
3130 std::vector<probe *> exception_probes;
17450429
PP
3131};
3132
51d3063a
TT
3133static const struct objfile_key<breakpoint_objfile_data>
3134 breakpoint_objfile_key;
17450429
PP
3135
3136/* Minimal symbol not found sentinel. */
3137static struct minimal_symbol msym_not_found;
3138
3139/* Returns TRUE if MSYM point to the "not found" sentinel. */
3140
3141static int
3142msym_not_found_p (const struct minimal_symbol *msym)
3143{
3144 return msym == &msym_not_found;
3145}
3146
3147/* Return per-objfile data needed by breakpoint.c.
3148 Allocate the data if necessary. */
3149
3150static struct breakpoint_objfile_data *
3151get_breakpoint_objfile_data (struct objfile *objfile)
3152{
3153 struct breakpoint_objfile_data *bp_objfile_data;
3154
51d3063a 3155 bp_objfile_data = breakpoint_objfile_key.get (objfile);
17450429 3156 if (bp_objfile_data == NULL)
51d3063a 3157 bp_objfile_data = breakpoint_objfile_key.emplace (objfile);
17450429
PP
3158 return bp_objfile_data;
3159}
3160
e62c965a 3161static void
af02033e 3162create_overlay_event_breakpoint (void)
e62c965a 3163{
af02033e 3164 const char *const func_name = "_ovly_debug_event";
e62c965a 3165
2030c079 3166 for (objfile *objfile : current_program_space->objfiles ())
69de3c6a
PP
3167 {
3168 struct breakpoint *b;
17450429
PP
3169 struct breakpoint_objfile_data *bp_objfile_data;
3170 CORE_ADDR addr;
67994074 3171 struct explicit_location explicit_loc;
69de3c6a 3172
17450429
PP
3173 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3174
3b7344d5 3175 if (msym_not_found_p (bp_objfile_data->overlay_msym.minsym))
17450429
PP
3176 continue;
3177
3b7344d5 3178 if (bp_objfile_data->overlay_msym.minsym == NULL)
17450429 3179 {
3b7344d5 3180 struct bound_minimal_symbol m;
17450429
PP
3181
3182 m = lookup_minimal_symbol_text (func_name, objfile);
3b7344d5 3183 if (m.minsym == NULL)
17450429
PP
3184 {
3185 /* Avoid future lookups in this objfile. */
3b7344d5 3186 bp_objfile_data->overlay_msym.minsym = &msym_not_found;
17450429
PP
3187 continue;
3188 }
3189 bp_objfile_data->overlay_msym = m;
3190 }
e62c965a 3191
77e371c0 3192 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->overlay_msym);
08feed99 3193 b = create_internal_breakpoint (objfile->arch (), addr,
06edf0c0
PA
3194 bp_overlay_event,
3195 &internal_breakpoint_ops);
67994074
KS
3196 initialize_explicit_location (&explicit_loc);
3197 explicit_loc.function_name = ASTRDUP (func_name);
d28cd78a 3198 b->location = new_explicit_location (&explicit_loc);
e62c965a 3199
69de3c6a
PP
3200 if (overlay_debugging == ovly_auto)
3201 {
3202 b->enable_state = bp_enabled;
3203 overlay_events_enabled = 1;
3204 }
3205 else
3206 {
3207 b->enable_state = bp_disabled;
3208 overlay_events_enabled = 0;
3209 }
e62c965a 3210 }
e62c965a
PP
3211}
3212
0fd8e87f 3213static void
af02033e 3214create_longjmp_master_breakpoint (void)
0fd8e87f 3215{
5ed8105e 3216 scoped_restore_current_program_space restore_pspace;
0fd8e87f 3217
94c93c35
TT
3218 for (struct program_space *pspace : program_spaces)
3219 {
3220 set_current_program_space (pspace);
af02033e 3221
94c93c35
TT
3222 for (objfile *objfile : current_program_space->objfiles ())
3223 {
3224 int i;
3225 struct gdbarch *gdbarch;
3226 struct breakpoint_objfile_data *bp_objfile_data;
0fd8e87f 3227
94c93c35 3228 gdbarch = objfile->arch ();
0fd8e87f 3229
94c93c35 3230 bp_objfile_data = get_breakpoint_objfile_data (objfile);
17450429 3231
94c93c35
TT
3232 if (!bp_objfile_data->longjmp_searched)
3233 {
3234 std::vector<probe *> ret
3235 = find_probes_in_objfile (objfile, "libc", "longjmp");
25f9533e 3236
94c93c35
TT
3237 if (!ret.empty ())
3238 {
3239 /* We are only interested in checking one element. */
3240 probe *p = ret[0];
aed57c53 3241
94c93c35
TT
3242 if (!p->can_evaluate_arguments ())
3243 {
3244 /* We cannot use the probe interface here,
3245 because it does not know how to evaluate
3246 arguments. */
3247 ret.clear ();
3248 }
3249 }
3250 bp_objfile_data->longjmp_probes = ret;
3251 bp_objfile_data->longjmp_searched = 1;
3252 }
25f9533e 3253
94c93c35
TT
3254 if (!bp_objfile_data->longjmp_probes.empty ())
3255 {
3256 for (probe *p : bp_objfile_data->longjmp_probes)
3257 {
3258 struct breakpoint *b;
3259
3260 b = create_internal_breakpoint (gdbarch,
3261 p->get_relocated_address (objfile),
3262 bp_longjmp_master,
3263 &internal_breakpoint_ops);
3264 b->location = new_probe_location ("-probe-stap libc:longjmp");
3265 b->enable_state = bp_disabled;
3266 }
28106bc2 3267
94c93c35
TT
3268 continue;
3269 }
28106bc2 3270
94c93c35
TT
3271 if (!gdbarch_get_longjmp_target_p (gdbarch))
3272 continue;
28106bc2 3273
94c93c35
TT
3274 for (i = 0; i < NUM_LONGJMP_NAMES; i++)
3275 {
3276 struct breakpoint *b;
3277 const char *func_name;
3278 CORE_ADDR addr;
3279 struct explicit_location explicit_loc;
0fd8e87f 3280
94c93c35
TT
3281 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i].minsym))
3282 continue;
17450429 3283
94c93c35
TT
3284 func_name = longjmp_names[i];
3285 if (bp_objfile_data->longjmp_msym[i].minsym == NULL)
3286 {
3287 struct bound_minimal_symbol m;
aed57c53 3288
94c93c35
TT
3289 m = lookup_minimal_symbol_text (func_name, objfile);
3290 if (m.minsym == NULL)
3291 {
3292 /* Prevent future lookups in this objfile. */
3293 bp_objfile_data->longjmp_msym[i].minsym = &msym_not_found;
3294 continue;
3295 }
3296 bp_objfile_data->longjmp_msym[i] = m;
3297 }
17450429 3298
94c93c35
TT
3299 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->longjmp_msym[i]);
3300 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master,
3301 &internal_breakpoint_ops);
3302 initialize_explicit_location (&explicit_loc);
3303 explicit_loc.function_name = ASTRDUP (func_name);
3304 b->location = new_explicit_location (&explicit_loc);
3305 b->enable_state = bp_disabled;
3306 }
3307 }
3308 }
0fd8e87f
UW
3309}
3310
af02033e 3311/* Create a master std::terminate breakpoint. */
aa7d318d 3312static void
af02033e 3313create_std_terminate_master_breakpoint (void)
aa7d318d 3314{
af02033e 3315 const char *const func_name = "std::terminate()";
aa7d318d 3316
5ed8105e 3317 scoped_restore_current_program_space restore_pspace;
aa7d318d 3318
94c93c35
TT
3319 for (struct program_space *pspace : program_spaces)
3320 {
3321 CORE_ADDR addr;
17450429 3322
94c93c35 3323 set_current_program_space (pspace);
17450429 3324
94c93c35
TT
3325 for (objfile *objfile : current_program_space->objfiles ())
3326 {
3327 struct breakpoint *b;
3328 struct breakpoint_objfile_data *bp_objfile_data;
3329 struct explicit_location explicit_loc;
aa7d318d 3330
94c93c35 3331 bp_objfile_data = get_breakpoint_objfile_data (objfile);
aa7d318d 3332
94c93c35
TT
3333 if (msym_not_found_p (bp_objfile_data->terminate_msym.minsym))
3334 continue;
17450429 3335
94c93c35
TT
3336 if (bp_objfile_data->terminate_msym.minsym == NULL)
3337 {
3338 struct bound_minimal_symbol m;
17450429 3339
94c93c35
TT
3340 m = lookup_minimal_symbol (func_name, NULL, objfile);
3341 if (m.minsym == NULL || (MSYMBOL_TYPE (m.minsym) != mst_text
3342 && MSYMBOL_TYPE (m.minsym) != mst_file_text))
3343 {
3344 /* Prevent future lookups in this objfile. */
3345 bp_objfile_data->terminate_msym.minsym = &msym_not_found;
3346 continue;
3347 }
3348 bp_objfile_data->terminate_msym = m;
3349 }
aa7d318d 3350
94c93c35
TT
3351 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->terminate_msym);
3352 b = create_internal_breakpoint (objfile->arch (), addr,
3353 bp_std_terminate_master,
3354 &internal_breakpoint_ops);
3355 initialize_explicit_location (&explicit_loc);
3356 explicit_loc.function_name = ASTRDUP (func_name);
3357 b->location = new_explicit_location (&explicit_loc);
3358 b->enable_state = bp_disabled;
3359 }
3360 }
aa7d318d
TT
3361}
3362
186c406b
TT
3363/* Install a master breakpoint on the unwinder's debug hook. */
3364
70221824 3365static void
186c406b
TT
3366create_exception_master_breakpoint (void)
3367{
17450429 3368 const char *const func_name = "_Unwind_DebugHook";
186c406b 3369
2030c079 3370 for (objfile *objfile : current_program_space->objfiles ())
186c406b 3371 {
17450429
PP
3372 struct breakpoint *b;
3373 struct gdbarch *gdbarch;
3374 struct breakpoint_objfile_data *bp_objfile_data;
3375 CORE_ADDR addr;
67994074 3376 struct explicit_location explicit_loc;
17450429
PP
3377
3378 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3379
28106bc2
SDJ
3380 /* We prefer the SystemTap probe point if it exists. */
3381 if (!bp_objfile_data->exception_searched)
3382 {
45461e0d
SM
3383 std::vector<probe *> ret
3384 = find_probes_in_objfile (objfile, "libgcc", "unwind");
25f9533e 3385
45461e0d 3386 if (!ret.empty ())
25f9533e
SDJ
3387 {
3388 /* We are only interested in checking one element. */
45461e0d 3389 probe *p = ret[0];
25f9533e 3390
935676c9 3391 if (!p->can_evaluate_arguments ())
25f9533e
SDJ
3392 {
3393 /* We cannot use the probe interface here, because it does
3394 not know how to evaluate arguments. */
45461e0d 3395 ret.clear ();
25f9533e
SDJ
3396 }
3397 }
3398 bp_objfile_data->exception_probes = ret;
28106bc2
SDJ
3399 bp_objfile_data->exception_searched = 1;
3400 }
3401
45461e0d 3402 if (!bp_objfile_data->exception_probes.empty ())
28106bc2 3403 {
08feed99 3404 gdbarch = objfile->arch ();
45461e0d
SM
3405
3406 for (probe *p : bp_objfile_data->exception_probes)
28106bc2 3407 {
729662a5 3408 b = create_internal_breakpoint (gdbarch,
935676c9 3409 p->get_relocated_address (objfile),
28106bc2
SDJ
3410 bp_exception_master,
3411 &internal_breakpoint_ops);
d28cd78a 3412 b->location = new_probe_location ("-probe-stap libgcc:unwind");
28106bc2
SDJ
3413 b->enable_state = bp_disabled;
3414 }
3415
3416 continue;
3417 }
3418
3419 /* Otherwise, try the hook function. */
3420
3b7344d5 3421 if (msym_not_found_p (bp_objfile_data->exception_msym.minsym))
17450429
PP
3422 continue;
3423
08feed99 3424 gdbarch = objfile->arch ();
186c406b 3425
3b7344d5 3426 if (bp_objfile_data->exception_msym.minsym == NULL)
186c406b 3427 {
3b7344d5 3428 struct bound_minimal_symbol debug_hook;
186c406b 3429
17450429 3430 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
3b7344d5 3431 if (debug_hook.minsym == NULL)
17450429 3432 {
3b7344d5 3433 bp_objfile_data->exception_msym.minsym = &msym_not_found;
17450429
PP
3434 continue;
3435 }
3436
3437 bp_objfile_data->exception_msym = debug_hook;
186c406b 3438 }
17450429 3439
77e371c0 3440 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->exception_msym);
8b88a78e
PA
3441 addr = gdbarch_convert_from_func_ptr_addr (gdbarch, addr,
3442 current_top_target ());
06edf0c0
PA
3443 b = create_internal_breakpoint (gdbarch, addr, bp_exception_master,
3444 &internal_breakpoint_ops);
67994074
KS
3445 initialize_explicit_location (&explicit_loc);
3446 explicit_loc.function_name = ASTRDUP (func_name);
d28cd78a 3447 b->location = new_explicit_location (&explicit_loc);
17450429 3448 b->enable_state = bp_disabled;
186c406b 3449 }
186c406b
TT
3450}
3451
9ef9e6a6
KS
3452/* Does B have a location spec? */
3453
3454static int
3455breakpoint_event_location_empty_p (const struct breakpoint *b)
3456{
d28cd78a 3457 return b->location != NULL && event_location_empty_p (b->location.get ());
9ef9e6a6
KS
3458}
3459
c906108c 3460void
fba45db2 3461update_breakpoints_after_exec (void)
c906108c 3462{
35df4500 3463 struct breakpoint *b, *b_tmp;
876fa593 3464 struct bp_location *bploc, **bplocp_tmp;
c906108c 3465
25b22b0a
PA
3466 /* We're about to delete breakpoints from GDB's lists. If the
3467 INSERTED flag is true, GDB will try to lift the breakpoints by
3468 writing the breakpoints' "shadow contents" back into memory. The
3469 "shadow contents" are NOT valid after an exec, so GDB should not
3470 do that. Instead, the target is responsible from marking
3471 breakpoints out as soon as it detects an exec. We don't do that
3472 here instead, because there may be other attempts to delete
3473 breakpoints after detecting an exec and before reaching here. */
876fa593 3474 ALL_BP_LOCATIONS (bploc, bplocp_tmp)
6c95b8df
PA
3475 if (bploc->pspace == current_program_space)
3476 gdb_assert (!bploc->inserted);
c906108c 3477
35df4500 3478 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 3479 {
6c95b8df
PA
3480 if (b->pspace != current_program_space)
3481 continue;
3482
4a64f543 3483 /* Solib breakpoints must be explicitly reset after an exec(). */
c5aa993b
JM
3484 if (b->type == bp_shlib_event)
3485 {
3486 delete_breakpoint (b);
3487 continue;
3488 }
c906108c 3489
4a64f543 3490 /* JIT breakpoints must be explicitly reset after an exec(). */
4efc6507
DE
3491 if (b->type == bp_jit_event)
3492 {
3493 delete_breakpoint (b);
3494 continue;
3495 }
3496
1900040c 3497 /* Thread event breakpoints must be set anew after an exec(),
0fd8e87f
UW
3498 as must overlay event and longjmp master breakpoints. */
3499 if (b->type == bp_thread_event || b->type == bp_overlay_event
186c406b
TT
3500 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
3501 || b->type == bp_exception_master)
c4093a6a
JM
3502 {
3503 delete_breakpoint (b);
3504 continue;
3505 }
3506
4a64f543 3507 /* Step-resume breakpoints are meaningless after an exec(). */
2c03e5be 3508 if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
c5aa993b
JM
3509 {
3510 delete_breakpoint (b);
3511 continue;
3512 }
3513
7c16b83e
PA
3514 /* Just like single-step breakpoints. */
3515 if (b->type == bp_single_step)
3516 {
3517 delete_breakpoint (b);
3518 continue;
3519 }
3520
611c83ae
PA
3521 /* Longjmp and longjmp-resume breakpoints are also meaningless
3522 after an exec. */
186c406b 3523 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
e2e4d78b 3524 || b->type == bp_longjmp_call_dummy
186c406b 3525 || b->type == bp_exception || b->type == bp_exception_resume)
611c83ae
PA
3526 {
3527 delete_breakpoint (b);
3528 continue;
3529 }
3530
ce78b96d
JB
3531 if (b->type == bp_catchpoint)
3532 {
3533 /* For now, none of the bp_catchpoint breakpoints need to
3534 do anything at this point. In the future, if some of
3535 the catchpoints need to something, we will need to add
3536 a new method, and call this method from here. */
3537 continue;
3538 }
3539
c5aa993b
JM
3540 /* bp_finish is a special case. The only way we ought to be able
3541 to see one of these when an exec() has happened, is if the user
3542 caught a vfork, and then said "finish". Ordinarily a finish just
3543 carries them to the call-site of the current callee, by setting
3544 a temporary bp there and resuming. But in this case, the finish
3545 will carry them entirely through the vfork & exec.
3546
3547 We don't want to allow a bp_finish to remain inserted now. But
3548 we can't safely delete it, 'cause finish_command has a handle to
3549 the bp on a bpstat, and will later want to delete it. There's a
3550 chance (and I've seen it happen) that if we delete the bp_finish
3551 here, that its storage will get reused by the time finish_command
3552 gets 'round to deleting the "use to be a bp_finish" breakpoint.
3553 We really must allow finish_command to delete a bp_finish.
3554
e5dd4106 3555 In the absence of a general solution for the "how do we know
53a5351d
JM
3556 it's safe to delete something others may have handles to?"
3557 problem, what we'll do here is just uninsert the bp_finish, and
3558 let finish_command delete it.
3559
3560 (We know the bp_finish is "doomed" in the sense that it's
3561 momentary, and will be deleted as soon as finish_command sees
3562 the inferior stopped. So it doesn't matter that the bp's
3563 address is probably bogus in the new a.out, unlike e.g., the
3564 solib breakpoints.) */
c5aa993b 3565
c5aa993b
JM
3566 if (b->type == bp_finish)
3567 {
3568 continue;
3569 }
3570
3571 /* Without a symbolic address, we have little hope of the
3572 pre-exec() address meaning the same thing in the post-exec()
4a64f543 3573 a.out. */
9ef9e6a6 3574 if (breakpoint_event_location_empty_p (b))
c5aa993b
JM
3575 {
3576 delete_breakpoint (b);
3577 continue;
3578 }
c5aa993b 3579 }
c906108c
SS
3580}
3581
3582int
d80ee84f 3583detach_breakpoints (ptid_t ptid)
c906108c 3584{
35df4500 3585 struct bp_location *bl, **blp_tmp;
3a1bae8e 3586 int val = 0;
2989a365 3587 scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid);
6c95b8df 3588 struct inferior *inf = current_inferior ();
c5aa993b 3589
e99b03dc 3590 if (ptid.pid () == inferior_ptid.pid ())
8a3fe4f8 3591 error (_("Cannot detach breakpoints of inferior_ptid"));
c5aa993b 3592
6c95b8df 3593 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
d80ee84f 3594 inferior_ptid = ptid;
35df4500 3595 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3596 {
35df4500 3597 if (bl->pspace != inf->pspace)
6c95b8df
PA
3598 continue;
3599
bd9673a4
PW
3600 /* This function must physically remove breakpoints locations
3601 from the specified ptid, without modifying the breakpoint
3602 package's state. Locations of type bp_loc_other are only
3603 maintained at GDB side. So, there is no need to remove
3604 these bp_loc_other locations. Moreover, removing these
3605 would modify the breakpoint package's state. */
3606 if (bl->loc_type == bp_loc_other)
3607 continue;
3608
35df4500 3609 if (bl->inserted)
b2b6a7da 3610 val |= remove_breakpoint_1 (bl, DETACH_BREAKPOINT);
c5aa993b 3611 }
d03285ec 3612
3a1bae8e 3613 return val;
c906108c
SS
3614}
3615
35df4500 3616/* Remove the breakpoint location BL from the current address space.
6c95b8df
PA
3617 Note that this is used to detach breakpoints from a child fork.
3618 When we get here, the child isn't in the inferior list, and neither
3619 do we have objects to represent its address space --- we should
35df4500 3620 *not* look at bl->pspace->aspace here. */
6c95b8df 3621
c906108c 3622static int
b2b6a7da 3623remove_breakpoint_1 (struct bp_location *bl, enum remove_bp_reason reason)
c906108c
SS
3624{
3625 int val;
c5aa993b 3626
35df4500
TJB
3627 /* BL is never in moribund_locations by our callers. */
3628 gdb_assert (bl->owner != NULL);
2bdf28a0 3629
74960c60
VP
3630 /* The type of none suggests that owner is actually deleted.
3631 This should not ever happen. */
35df4500 3632 gdb_assert (bl->owner->type != bp_none);
0bde7532 3633
35df4500
TJB
3634 if (bl->loc_type == bp_loc_software_breakpoint
3635 || bl->loc_type == bp_loc_hardware_breakpoint)
c906108c 3636 {
c02f5703
MS
3637 /* "Normal" instruction breakpoint: either the standard
3638 trap-instruction bp (bp_breakpoint), or a
3639 bp_hardware_breakpoint. */
3640
3641 /* First check to see if we have to handle an overlay. */
3642 if (overlay_debugging == ovly_off
35df4500
TJB
3643 || bl->section == NULL
3644 || !(section_is_overlay (bl->section)))
c02f5703
MS
3645 {
3646 /* No overlay handling: just remove the breakpoint. */
08351840
PA
3647
3648 /* If we're trying to uninsert a memory breakpoint that we
3649 know is set in a dynamic object that is marked
3650 shlib_disabled, then either the dynamic object was
3651 removed with "remove-symbol-file" or with
3652 "nosharedlibrary". In the former case, we don't know
3653 whether another dynamic object might have loaded over the
3654 breakpoint's address -- the user might well let us know
3655 about it next with add-symbol-file (the whole point of
d03de421 3656 add-symbol-file is letting the user manually maintain a
08351840
PA
3657 list of dynamically loaded objects). If we have the
3658 breakpoint's shadow memory, that is, this is a software
3659 breakpoint managed by GDB, check whether the breakpoint
3660 is still inserted in memory, to avoid overwriting wrong
3661 code with stale saved shadow contents. Note that HW
3662 breakpoints don't have shadow memory, as they're
3663 implemented using a mechanism that is not dependent on
3664 being able to modify the target's memory, and as such
3665 they should always be removed. */
3666 if (bl->shlib_disabled
3667 && bl->target_info.shadow_len != 0
3668 && !memory_validate_breakpoint (bl->gdbarch, &bl->target_info))
3669 val = 0;
3670 else
73971819 3671 val = bl->owner->ops->remove_location (bl, reason);
c02f5703 3672 }
c906108c
SS
3673 else
3674 {
4a64f543 3675 /* This breakpoint is in an overlay section.
c02f5703
MS
3676 Did we set a breakpoint at the LMA? */
3677 if (!overlay_events_enabled)
3678 {
3679 /* Yes -- overlay event support is not active, so we
3680 should have set a breakpoint at the LMA. Remove it.
3681 */
c02f5703
MS
3682 /* Ignore any failures: if the LMA is in ROM, we will
3683 have already warned when we failed to insert it. */
35df4500
TJB
3684 if (bl->loc_type == bp_loc_hardware_breakpoint)
3685 target_remove_hw_breakpoint (bl->gdbarch,
3686 &bl->overlay_target_info);
c02f5703 3687 else
35df4500 3688 target_remove_breakpoint (bl->gdbarch,
73971819
PA
3689 &bl->overlay_target_info,
3690 reason);
c02f5703
MS
3691 }
3692 /* Did we set a breakpoint at the VMA?
3693 If so, we will have marked the breakpoint 'inserted'. */
35df4500 3694 if (bl->inserted)
c906108c 3695 {
c02f5703
MS
3696 /* Yes -- remove it. Previously we did not bother to
3697 remove the breakpoint if the section had been
3698 unmapped, but let's not rely on that being safe. We
3699 don't know what the overlay manager might do. */
aa67235e
UW
3700
3701 /* However, we should remove *software* breakpoints only
3702 if the section is still mapped, or else we overwrite
3703 wrong code with the saved shadow contents. */
348d480f
PA
3704 if (bl->loc_type == bp_loc_hardware_breakpoint
3705 || section_is_mapped (bl->section))
73971819 3706 val = bl->owner->ops->remove_location (bl, reason);
aa67235e
UW
3707 else
3708 val = 0;
c906108c 3709 }
c02f5703
MS
3710 else
3711 {
3712 /* No -- not inserted, so no need to remove. No error. */
3713 val = 0;
3714 }
c906108c 3715 }
879d1e6b 3716
08351840
PA
3717 /* In some cases, we might not be able to remove a breakpoint in
3718 a shared library that has already been removed, but we have
3719 not yet processed the shlib unload event. Similarly for an
3720 unloaded add-symbol-file object - the user might not yet have
3721 had the chance to remove-symbol-file it. shlib_disabled will
3722 be set if the library/object has already been removed, but
3723 the breakpoint hasn't been uninserted yet, e.g., after
3724 "nosharedlibrary" or "remove-symbol-file" with breakpoints
3725 always-inserted mode. */
076855f9 3726 if (val
08351840
PA
3727 && (bl->loc_type == bp_loc_software_breakpoint
3728 && (bl->shlib_disabled
3729 || solib_name_from_address (bl->pspace, bl->address)
d03de421
PA
3730 || shared_objfile_contains_address_p (bl->pspace,
3731 bl->address))))
879d1e6b
UW
3732 val = 0;
3733
c906108c
SS
3734 if (val)
3735 return val;
b2b6a7da 3736 bl->inserted = (reason == DETACH_BREAKPOINT);
c906108c 3737 }
35df4500 3738 else if (bl->loc_type == bp_loc_hardware_watchpoint)
c906108c 3739 {
77b06cd7
TJB
3740 gdb_assert (bl->owner->ops != NULL
3741 && bl->owner->ops->remove_location != NULL);
3742
b2b6a7da 3743 bl->inserted = (reason == DETACH_BREAKPOINT);
73971819 3744 bl->owner->ops->remove_location (bl, reason);
2e70b7b9 3745
c906108c 3746 /* Failure to remove any of the hardware watchpoints comes here. */
b2b6a7da 3747 if (reason == REMOVE_BREAKPOINT && bl->inserted)
8a3fe4f8 3748 warning (_("Could not remove hardware watchpoint %d."),
35df4500 3749 bl->owner->number);
c906108c 3750 }
35df4500
TJB
3751 else if (bl->owner->type == bp_catchpoint
3752 && breakpoint_enabled (bl->owner)
3753 && !bl->duplicate)
ce78b96d 3754 {
77b06cd7
TJB
3755 gdb_assert (bl->owner->ops != NULL
3756 && bl->owner->ops->remove_location != NULL);
ce78b96d 3757
73971819 3758 val = bl->owner->ops->remove_location (bl, reason);
ce78b96d
JB
3759 if (val)
3760 return val;
77b06cd7 3761
b2b6a7da 3762 bl->inserted = (reason == DETACH_BREAKPOINT);
ce78b96d 3763 }
c906108c
SS
3764
3765 return 0;
3766}
3767
6c95b8df 3768static int
834c0d03 3769remove_breakpoint (struct bp_location *bl)
6c95b8df 3770{
35df4500
TJB
3771 /* BL is never in moribund_locations by our callers. */
3772 gdb_assert (bl->owner != NULL);
2bdf28a0 3773
6c95b8df
PA
3774 /* The type of none suggests that owner is actually deleted.
3775 This should not ever happen. */
35df4500 3776 gdb_assert (bl->owner->type != bp_none);
6c95b8df 3777
5ed8105e 3778 scoped_restore_current_pspace_and_thread restore_pspace_thread;
6c95b8df 3779
35df4500 3780 switch_to_program_space_and_thread (bl->pspace);
6c95b8df 3781
5ed8105e 3782 return remove_breakpoint_1 (bl, REMOVE_BREAKPOINT);
6c95b8df
PA
3783}
3784
c906108c
SS
3785/* Clear the "inserted" flag in all breakpoints. */
3786
25b22b0a 3787void
fba45db2 3788mark_breakpoints_out (void)
c906108c 3789{
35df4500 3790 struct bp_location *bl, **blp_tmp;
c906108c 3791
35df4500 3792 ALL_BP_LOCATIONS (bl, blp_tmp)
66c4b3e8 3793 if (bl->pspace == current_program_space)
35df4500 3794 bl->inserted = 0;
c906108c
SS
3795}
3796
53a5351d
JM
3797/* Clear the "inserted" flag in all breakpoints and delete any
3798 breakpoints which should go away between runs of the program.
c906108c
SS
3799
3800 Plus other such housekeeping that has to be done for breakpoints
3801 between runs.
3802
53a5351d
JM
3803 Note: this function gets called at the end of a run (by
3804 generic_mourn_inferior) and when a run begins (by
4a64f543 3805 init_wait_for_inferior). */
c906108c
SS
3806
3807
3808
3809void
fba45db2 3810breakpoint_init_inferior (enum inf_context context)
c906108c 3811{
35df4500 3812 struct breakpoint *b, *b_tmp;
6c95b8df 3813 struct program_space *pspace = current_program_space;
c906108c 3814
50c71eaf
PA
3815 /* If breakpoint locations are shared across processes, then there's
3816 nothing to do. */
f5656ead 3817 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
50c71eaf
PA
3818 return;
3819
1a853c52 3820 mark_breakpoints_out ();
075f6582 3821
35df4500 3822 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 3823 {
6c95b8df
PA
3824 if (b->loc && b->loc->pspace != pspace)
3825 continue;
3826
c5aa993b
JM
3827 switch (b->type)
3828 {
3829 case bp_call_dummy:
e2e4d78b 3830 case bp_longjmp_call_dummy:
c906108c 3831
c5aa993b 3832 /* If the call dummy breakpoint is at the entry point it will
ab92d69b
PA
3833 cause problems when the inferior is rerun, so we better get
3834 rid of it. */
3835
3836 case bp_watchpoint_scope:
3837
3838 /* Also get rid of scope breakpoints. */
3839
3840 case bp_shlib_event:
3841
3842 /* Also remove solib event breakpoints. Their addresses may
3843 have changed since the last time we ran the program.
3844 Actually we may now be debugging against different target;
3845 and so the solib backend that installed this breakpoint may
3846 not be used in by the target. E.g.,
3847
3848 (gdb) file prog-linux
3849 (gdb) run # native linux target
3850 ...
3851 (gdb) kill
3852 (gdb) file prog-win.exe
3853 (gdb) tar rem :9999 # remote Windows gdbserver.
3854 */
c906108c 3855
f59f708a
PA
3856 case bp_step_resume:
3857
3858 /* Also remove step-resume breakpoints. */
3859
7c16b83e
PA
3860 case bp_single_step:
3861
3862 /* Also remove single-step breakpoints. */
3863
c5aa993b
JM
3864 delete_breakpoint (b);
3865 break;
c906108c 3866
c5aa993b
JM
3867 case bp_watchpoint:
3868 case bp_hardware_watchpoint:
3869 case bp_read_watchpoint:
3870 case bp_access_watchpoint:
3a5c3e22
PA
3871 {
3872 struct watchpoint *w = (struct watchpoint *) b;
c906108c 3873
3a5c3e22
PA
3874 /* Likewise for watchpoints on local expressions. */
3875 if (w->exp_valid_block != NULL)
3876 delete_breakpoint (b);
63000888 3877 else
3a5c3e22 3878 {
63000888
PA
3879 /* Get rid of existing locations, which are no longer
3880 valid. New ones will be created in
3881 update_watchpoint, when the inferior is restarted.
3882 The next update_global_location_list call will
3883 garbage collect them. */
3884 b->loc = NULL;
3885
3886 if (context == inf_starting)
3887 {
3888 /* Reset val field to force reread of starting value in
3889 insert_breakpoints. */
850645cf 3890 w->val.reset (nullptr);
4c1d86d9 3891 w->val_valid = false;
63000888
PA
3892 }
3893 }
3a5c3e22 3894 }
c5aa993b
JM
3895 break;
3896 default:
c5aa993b
JM
3897 break;
3898 }
3899 }
1c5cfe86
PA
3900
3901 /* Get rid of the moribund locations. */
1123588c 3902 for (bp_location *bl : moribund_locations)
35df4500 3903 decref_bp_location (&bl);
1123588c 3904 moribund_locations.clear ();
c906108c
SS
3905}
3906
6c95b8df
PA
3907/* These functions concern about actual breakpoints inserted in the
3908 target --- to e.g. check if we need to do decr_pc adjustment or if
3909 we need to hop over the bkpt --- so we check for address space
3910 match, not program space. */
3911
c2c6d25f
JM
3912/* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
3913 exists at PC. It returns ordinary_breakpoint_here if it's an
3914 ordinary breakpoint, or permanent_breakpoint_here if it's a
3915 permanent breakpoint.
3916 - When continuing from a location with an ordinary breakpoint, we
3917 actually single step once before calling insert_breakpoints.
e5dd4106 3918 - When continuing from a location with a permanent breakpoint, we
c2c6d25f
JM
3919 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
3920 the target, to advance the PC past the breakpoint. */
c906108c 3921
c2c6d25f 3922enum breakpoint_here
accd0bcd 3923breakpoint_here_p (const address_space *aspace, CORE_ADDR pc)
c906108c 3924{
35df4500 3925 struct bp_location *bl, **blp_tmp;
c2c6d25f 3926 int any_breakpoint_here = 0;
c906108c 3927
35df4500 3928 ALL_BP_LOCATIONS (bl, blp_tmp)
075f6582 3929 {
35df4500
TJB
3930 if (bl->loc_type != bp_loc_software_breakpoint
3931 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
3932 continue;
3933
f1310107 3934 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
35df4500 3935 if ((breakpoint_enabled (bl->owner)
1a853c52 3936 || bl->permanent)
f1310107 3937 && breakpoint_location_address_match (bl, aspace, pc))
075f6582
DJ
3938 {
3939 if (overlay_debugging
35df4500
TJB
3940 && section_is_overlay (bl->section)
3941 && !section_is_mapped (bl->section))
075f6582 3942 continue; /* unmapped overlay -- can't be a match */
1a853c52 3943 else if (bl->permanent)
075f6582
DJ
3944 return permanent_breakpoint_here;
3945 else
3946 any_breakpoint_here = 1;
3947 }
3948 }
c906108c 3949
f486487f 3950 return any_breakpoint_here ? ordinary_breakpoint_here : no_breakpoint_here;
c906108c
SS
3951}
3952
d35ae833
PA
3953/* See breakpoint.h. */
3954
3955int
accd0bcd 3956breakpoint_in_range_p (const address_space *aspace,
d35ae833
PA
3957 CORE_ADDR addr, ULONGEST len)
3958{
3959 struct bp_location *bl, **blp_tmp;
3960
3961 ALL_BP_LOCATIONS (bl, blp_tmp)
3962 {
3963 if (bl->loc_type != bp_loc_software_breakpoint
3964 && bl->loc_type != bp_loc_hardware_breakpoint)
3965 continue;
3966
3967 if ((breakpoint_enabled (bl->owner)
3968 || bl->permanent)
3969 && breakpoint_location_address_range_overlap (bl, aspace,
3970 addr, len))
3971 {
3972 if (overlay_debugging
3973 && section_is_overlay (bl->section)
3974 && !section_is_mapped (bl->section))
3975 {
3976 /* Unmapped overlay -- can't be a match. */
3977 continue;
3978 }
3979
3980 return 1;
3981 }
3982 }
3983
3984 return 0;
3985}
3986
1c5cfe86
PA
3987/* Return true if there's a moribund breakpoint at PC. */
3988
3989int
accd0bcd 3990moribund_breakpoint_here_p (const address_space *aspace, CORE_ADDR pc)
1c5cfe86 3991{
1123588c 3992 for (bp_location *loc : moribund_locations)
f1310107 3993 if (breakpoint_location_address_match (loc, aspace, pc))
1c5cfe86
PA
3994 return 1;
3995
3996 return 0;
3997}
c2c6d25f 3998
f7ce857f
PA
3999/* Returns non-zero iff BL is inserted at PC, in address space
4000 ASPACE. */
4001
4002static int
4003bp_location_inserted_here_p (struct bp_location *bl,
accd0bcd 4004 const address_space *aspace, CORE_ADDR pc)
f7ce857f
PA
4005{
4006 if (bl->inserted
4007 && breakpoint_address_match (bl->pspace->aspace, bl->address,
4008 aspace, pc))
4009 {
4010 if (overlay_debugging
4011 && section_is_overlay (bl->section)
4012 && !section_is_mapped (bl->section))
4013 return 0; /* unmapped overlay -- can't be a match */
4014 else
4015 return 1;
4016 }
4017 return 0;
4018}
4019
a1fd2fa5 4020/* Returns non-zero iff there's a breakpoint inserted at PC. */
c906108c
SS
4021
4022int
accd0bcd 4023breakpoint_inserted_here_p (const address_space *aspace, CORE_ADDR pc)
c906108c 4024{
f7ce857f 4025 struct bp_location **blp, **blp_tmp = NULL;
c906108c 4026
f7ce857f 4027 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
c5aa993b 4028 {
f7ce857f
PA
4029 struct bp_location *bl = *blp;
4030
35df4500
TJB
4031 if (bl->loc_type != bp_loc_software_breakpoint
4032 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
4033 continue;
4034
f7ce857f
PA
4035 if (bp_location_inserted_here_p (bl, aspace, pc))
4036 return 1;
c5aa993b 4037 }
c36b740a
VP
4038 return 0;
4039}
4040
a1fd2fa5
PA
4041/* This function returns non-zero iff there is a software breakpoint
4042 inserted at PC. */
c36b740a
VP
4043
4044int
accd0bcd 4045software_breakpoint_inserted_here_p (const address_space *aspace,
a1fd2fa5 4046 CORE_ADDR pc)
4fa8626c 4047{
f7ce857f 4048 struct bp_location **blp, **blp_tmp = NULL;
4fa8626c 4049
f7ce857f 4050 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4fa8626c 4051 {
f7ce857f
PA
4052 struct bp_location *bl = *blp;
4053
35df4500 4054 if (bl->loc_type != bp_loc_software_breakpoint)
4fa8626c
DJ
4055 continue;
4056
f7ce857f
PA
4057 if (bp_location_inserted_here_p (bl, aspace, pc))
4058 return 1;
4fa8626c
DJ
4059 }
4060
4061 return 0;
9c02b525
PA
4062}
4063
4064/* See breakpoint.h. */
4065
4066int
accd0bcd 4067hardware_breakpoint_inserted_here_p (const address_space *aspace,
9c02b525
PA
4068 CORE_ADDR pc)
4069{
4070 struct bp_location **blp, **blp_tmp = NULL;
9c02b525
PA
4071
4072 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4073 {
4074 struct bp_location *bl = *blp;
4075
4076 if (bl->loc_type != bp_loc_hardware_breakpoint)
4077 continue;
4078
4079 if (bp_location_inserted_here_p (bl, aspace, pc))
4080 return 1;
4081 }
4082
4083 return 0;
4fa8626c
DJ
4084}
4085
9093389c 4086int
accd0bcd 4087hardware_watchpoint_inserted_in_range (const address_space *aspace,
9093389c
PA
4088 CORE_ADDR addr, ULONGEST len)
4089{
4090 struct breakpoint *bpt;
4091
4092 ALL_BREAKPOINTS (bpt)
4093 {
4094 struct bp_location *loc;
4095
4096 if (bpt->type != bp_hardware_watchpoint
4097 && bpt->type != bp_access_watchpoint)
4098 continue;
4099
4100 if (!breakpoint_enabled (bpt))
4101 continue;
4102
4103 for (loc = bpt->loc; loc; loc = loc->next)
4104 if (loc->pspace->aspace == aspace && loc->inserted)
4105 {
4106 CORE_ADDR l, h;
4107
4108 /* Check for intersection. */
768adc05
PA
4109 l = std::max<CORE_ADDR> (loc->address, addr);
4110 h = std::min<CORE_ADDR> (loc->address + loc->length, addr + len);
9093389c
PA
4111 if (l < h)
4112 return 1;
4113 }
4114 }
4115 return 0;
4116}
c5aa993b 4117
f2478a7e 4118/* See breakpoint.h. */
c906108c 4119
f2478a7e
SM
4120bool
4121is_catchpoint (struct breakpoint *b)
c906108c 4122{
f2478a7e 4123 return (b->type == bp_catchpoint);
c906108c
SS
4124}
4125
f431efe5
PA
4126/* Frees any storage that is part of a bpstat. Does not walk the
4127 'next' chain. */
4128
04afa70c 4129bpstats::~bpstats ()
198757a8 4130{
04afa70c
TT
4131 if (bp_location_at != NULL)
4132 decref_bp_location (&bp_location_at);
198757a8
VP
4133}
4134
c906108c
SS
4135/* Clear a bpstat so that it says we are not at any breakpoint.
4136 Also free any storage that is part of a bpstat. */
4137
4138void
fba45db2 4139bpstat_clear (bpstat *bsp)
c906108c
SS
4140{
4141 bpstat p;
4142 bpstat q;
4143
4144 if (bsp == 0)
4145 return;
4146 p = *bsp;
4147 while (p != NULL)
4148 {
4149 q = p->next;
04afa70c 4150 delete p;
c906108c
SS
4151 p = q;
4152 }
4153 *bsp = NULL;
4154}
4155
04afa70c
TT
4156bpstats::bpstats (const bpstats &other)
4157 : next (NULL),
4158 bp_location_at (other.bp_location_at),
4159 breakpoint_at (other.breakpoint_at),
4160 commands (other.commands),
04afa70c
TT
4161 print (other.print),
4162 stop (other.stop),
4163 print_it (other.print_it)
4164{
850645cf
TT
4165 if (other.old_val != NULL)
4166 old_val = release_value (value_copy (other.old_val.get ()));
04afa70c 4167 incref_bp_location (bp_location_at);
04afa70c
TT
4168}
4169
c906108c
SS
4170/* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
4171 is part of the bpstat is copied as well. */
4172
4173bpstat
fba45db2 4174bpstat_copy (bpstat bs)
c906108c
SS
4175{
4176 bpstat p = NULL;
4177 bpstat tmp;
4178 bpstat retval = NULL;
4179
4180 if (bs == NULL)
4181 return bs;
4182
4183 for (; bs != NULL; bs = bs->next)
4184 {
04afa70c 4185 tmp = new bpstats (*bs);
31cc81e9 4186
c906108c
SS
4187 if (p == NULL)
4188 /* This is the first thing in the chain. */
4189 retval = tmp;
4190 else
4191 p->next = tmp;
4192 p = tmp;
4193 }
4194 p->next = NULL;
4195 return retval;
4196}
4197
4a64f543 4198/* Find the bpstat associated with this breakpoint. */
c906108c
SS
4199
4200bpstat
fba45db2 4201bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
c906108c 4202{
c5aa993b
JM
4203 if (bsp == NULL)
4204 return NULL;
c906108c 4205
c5aa993b
JM
4206 for (; bsp != NULL; bsp = bsp->next)
4207 {
f431efe5 4208 if (bsp->breakpoint_at == breakpoint)
c5aa993b
JM
4209 return bsp;
4210 }
c906108c
SS
4211 return NULL;
4212}
4213
ab04a2af
TT
4214/* See breakpoint.h. */
4215
4c462cb0 4216bool
427cd150 4217bpstat_explains_signal (bpstat bsp, enum gdb_signal sig)
ab04a2af 4218{
ab04a2af
TT
4219 for (; bsp != NULL; bsp = bsp->next)
4220 {
427cd150
TT
4221 if (bsp->breakpoint_at == NULL)
4222 {
4223 /* A moribund location can never explain a signal other than
4224 GDB_SIGNAL_TRAP. */
4225 if (sig == GDB_SIGNAL_TRAP)
4c462cb0 4226 return true;
427cd150
TT
4227 }
4228 else
47591c29
PA
4229 {
4230 if (bsp->breakpoint_at->ops->explains_signal (bsp->breakpoint_at,
4231 sig))
4c462cb0 4232 return true;
47591c29 4233 }
ab04a2af
TT
4234 }
4235
4c462cb0 4236 return false;
ab04a2af
TT
4237}
4238
4a64f543
MS
4239/* Put in *NUM the breakpoint number of the first breakpoint we are
4240 stopped at. *BSP upon return is a bpstat which points to the
4241 remaining breakpoints stopped at (but which is not guaranteed to be
4242 good for anything but further calls to bpstat_num).
4243
8671a17b
PA
4244 Return 0 if passed a bpstat which does not indicate any breakpoints.
4245 Return -1 if stopped at a breakpoint that has been deleted since
4246 we set it.
4247 Return 1 otherwise. */
c906108c
SS
4248
4249int
8671a17b 4250bpstat_num (bpstat *bsp, int *num)
c906108c
SS
4251{
4252 struct breakpoint *b;
4253
4254 if ((*bsp) == NULL)
4255 return 0; /* No more breakpoint values */
8671a17b 4256
4a64f543
MS
4257 /* We assume we'll never have several bpstats that correspond to a
4258 single breakpoint -- otherwise, this function might return the
4259 same number more than once and this will look ugly. */
f431efe5 4260 b = (*bsp)->breakpoint_at;
8671a17b
PA
4261 *bsp = (*bsp)->next;
4262 if (b == NULL)
4263 return -1; /* breakpoint that's been deleted since */
4264
4265 *num = b->number; /* We have its number */
4266 return 1;
c906108c
SS
4267}
4268
e93ca019 4269/* See breakpoint.h. */
c906108c
SS
4270
4271void
e93ca019 4272bpstat_clear_actions (void)
c906108c 4273{
e93ca019
JK
4274 bpstat bs;
4275
00431a78 4276 if (inferior_ptid == null_ptid)
e93ca019
JK
4277 return;
4278
00431a78 4279 thread_info *tp = inferior_thread ();
e93ca019 4280 for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
c906108c 4281 {
d1b0a7bf 4282 bs->commands = NULL;
850645cf 4283 bs->old_val.reset (nullptr);
c906108c
SS
4284 }
4285}
4286
f3b1572e
PA
4287/* Called when a command is about to proceed the inferior. */
4288
4289static void
4290breakpoint_about_to_proceed (void)
4291{
d7e15655 4292 if (inferior_ptid != null_ptid)
f3b1572e
PA
4293 {
4294 struct thread_info *tp = inferior_thread ();
4295
4296 /* Allow inferior function calls in breakpoint commands to not
4297 interrupt the command list. When the call finishes
4298 successfully, the inferior will be standing at the same
4299 breakpoint as if nothing happened. */
16c381f0 4300 if (tp->control.in_infcall)
f3b1572e
PA
4301 return;
4302 }
4303
4304 breakpoint_proceeded = 1;
4305}
4306
abf85f46
JK
4307/* Return non-zero iff CMD as the first line of a command sequence is `silent'
4308 or its equivalent. */
4309
4310static int
4311command_line_is_silent (struct command_line *cmd)
4312{
4f45d445 4313 return cmd && (strcmp ("silent", cmd->line) == 0);
abf85f46
JK
4314}
4315
4a64f543
MS
4316/* Execute all the commands associated with all the breakpoints at
4317 this location. Any of these commands could cause the process to
4318 proceed beyond this point, etc. We look out for such changes by
4319 checking the global "breakpoint_proceeded" after each command.
c906108c 4320
347bddb7
PA
4321 Returns true if a breakpoint command resumed the inferior. In that
4322 case, it is the caller's responsibility to recall it again with the
4323 bpstat of the current thread. */
4324
4325static int
4326bpstat_do_actions_1 (bpstat *bsp)
c906108c
SS
4327{
4328 bpstat bs;
347bddb7 4329 int again = 0;
c906108c
SS
4330
4331 /* Avoid endless recursion if a `source' command is contained
4332 in bs->commands. */
4333 if (executing_breakpoint_commands)
347bddb7 4334 return 0;
c906108c 4335
81b1e71c
TT
4336 scoped_restore save_executing
4337 = make_scoped_restore (&executing_breakpoint_commands, 1);
c906108c 4338
1ac32117 4339 scoped_restore preventer = prevent_dont_repeat ();
cf6c5ffb 4340
4a64f543 4341 /* This pointer will iterate over the list of bpstat's. */
c906108c
SS
4342 bs = *bsp;
4343
4344 breakpoint_proceeded = 0;
4345 for (; bs != NULL; bs = bs->next)
4346 {
d1b0a7bf 4347 struct command_line *cmd = NULL;
6c50ab1c
JB
4348
4349 /* Take ownership of the BSP's command tree, if it has one.
4350
4351 The command tree could legitimately contain commands like
4352 'step' and 'next', which call clear_proceed_status, which
4353 frees stop_bpstat's command tree. To make sure this doesn't
4354 free the tree we're executing out from under us, we need to
4355 take ownership of the tree ourselves. Since a given bpstat's
4356 commands are only executed once, we don't need to copy it; we
4357 can clear the pointer in the bpstat, and make sure we free
4358 the tree when we're done. */
d1b0a7bf 4359 counted_command_line ccmd = bs->commands;
9add0f1b 4360 bs->commands = NULL;
d1b0a7bf
TT
4361 if (ccmd != NULL)
4362 cmd = ccmd.get ();
abf85f46
JK
4363 if (command_line_is_silent (cmd))
4364 {
4365 /* The action has been already done by bpstat_stop_status. */
4366 cmd = cmd->next;
4367 }
6c50ab1c 4368
c906108c
SS
4369 while (cmd != NULL)
4370 {
4371 execute_control_command (cmd);
4372
4373 if (breakpoint_proceeded)
4374 break;
4375 else
4376 cmd = cmd->next;
4377 }
6c50ab1c 4378
c906108c 4379 if (breakpoint_proceeded)
32c1e744 4380 {
cb814510 4381 if (current_ui->async)
347bddb7
PA
4382 /* If we are in async mode, then the target might be still
4383 running, not stopped at any breakpoint, so nothing for
4384 us to do here -- just return to the event loop. */
4385 ;
32c1e744
VP
4386 else
4387 /* In sync mode, when execute_control_command returns
4388 we're already standing on the next breakpoint.
347bddb7
PA
4389 Breakpoint commands for that stop were not run, since
4390 execute_command does not run breakpoint commands --
4391 only command_line_handler does, but that one is not
4392 involved in execution of breakpoint commands. So, we
4393 can now execute breakpoint commands. It should be
4394 noted that making execute_command do bpstat actions is
4395 not an option -- in this case we'll have recursive
4396 invocation of bpstat for each breakpoint with a
4397 command, and can easily blow up GDB stack. Instead, we
4398 return true, which will trigger the caller to recall us
4399 with the new stop_bpstat. */
4400 again = 1;
4401 break;
32c1e744 4402 }
c906108c 4403 }
347bddb7
PA
4404 return again;
4405}
4406
00431a78
PA
4407/* Helper for bpstat_do_actions. Get the current thread, if there's
4408 one, is alive and has execution. Return NULL otherwise. */
4409
4410static thread_info *
4411get_bpstat_thread ()
4412{
4413 if (inferior_ptid == null_ptid || !target_has_execution)
4414 return NULL;
4415
4416 thread_info *tp = inferior_thread ();
4417 if (tp->state == THREAD_EXITED || tp->executing)
4418 return NULL;
4419 return tp;
4420}
4421
347bddb7
PA
4422void
4423bpstat_do_actions (void)
4424{
694c6bf5 4425 auto cleanup_if_error = make_scope_exit (bpstat_clear_actions);
00431a78 4426 thread_info *tp;
353d1d73 4427
347bddb7 4428 /* Do any commands attached to breakpoint we are stopped at. */
00431a78
PA
4429 while ((tp = get_bpstat_thread ()) != NULL)
4430 {
4431 /* Since in sync mode, bpstat_do_actions may resume the
4432 inferior, and only return when it is stopped at the next
4433 breakpoint, we keep doing breakpoint actions until it returns
4434 false to indicate the inferior was not resumed. */
4435 if (!bpstat_do_actions_1 (&tp->control.stop_bpstat))
4436 break;
4437 }
353d1d73 4438
694c6bf5 4439 cleanup_if_error.release ();
c906108c
SS
4440}
4441
fa4727a6
DJ
4442/* Print out the (old or new) value associated with a watchpoint. */
4443
4444static void
4445watchpoint_value_print (struct value *val, struct ui_file *stream)
4446{
4447 if (val == NULL)
7f6aba03 4448 fprintf_styled (stream, metadata_style.style (), _("<unreadable>"));
fa4727a6 4449 else
79a45b7d
TT
4450 {
4451 struct value_print_options opts;
4452 get_user_print_options (&opts);
4453 value_print (val, stream, &opts);
4454 }
fa4727a6
DJ
4455}
4456
f303dbd6
PA
4457/* Print the "Thread ID hit" part of "Thread ID hit Breakpoint N" if
4458 debugging multiple threads. */
4459
4460void
4461maybe_print_thread_hit_breakpoint (struct ui_out *uiout)
4462{
112e8700 4463 if (uiout->is_mi_like_p ())
f303dbd6
PA
4464 return;
4465
112e8700 4466 uiout->text ("\n");
f303dbd6
PA
4467
4468 if (show_thread_that_caused_stop ())
4469 {
4470 const char *name;
4471 struct thread_info *thr = inferior_thread ();
4472
112e8700 4473 uiout->text ("Thread ");
33eca680 4474 uiout->field_string ("thread-id", print_thread_id (thr));
f303dbd6
PA
4475
4476 name = thr->name != NULL ? thr->name : target_thread_name (thr);
4477 if (name != NULL)
4478 {
112e8700 4479 uiout->text (" \"");
33eca680 4480 uiout->field_string ("name", name);
112e8700 4481 uiout->text ("\"");
f303dbd6
PA
4482 }
4483
112e8700 4484 uiout->text (" hit ");
f303dbd6
PA
4485 }
4486}
4487
e514a9d6 4488/* Generic routine for printing messages indicating why we
4a64f543 4489 stopped. The behavior of this function depends on the value
e514a9d6
JM
4490 'print_it' in the bpstat structure. Under some circumstances we
4491 may decide not to print anything here and delegate the task to
4a64f543 4492 normal_stop(). */
e514a9d6
JM
4493
4494static enum print_stop_action
4495print_bp_stop_message (bpstat bs)
4496{
4497 switch (bs->print_it)
4498 {
4499 case print_it_noop:
4a64f543 4500 /* Nothing should be printed for this bpstat entry. */
e514a9d6
JM
4501 return PRINT_UNKNOWN;
4502 break;
4503
4504 case print_it_done:
4505 /* We still want to print the frame, but we already printed the
4a64f543 4506 relevant messages. */
e514a9d6
JM
4507 return PRINT_SRC_AND_LOC;
4508 break;
4509
4510 case print_it_normal:
4f8d1dc6 4511 {
f431efe5
PA
4512 struct breakpoint *b = bs->breakpoint_at;
4513
1a6a67de
TJB
4514 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
4515 which has since been deleted. */
4516 if (b == NULL)
4517 return PRINT_UNKNOWN;
4518
348d480f
PA
4519 /* Normal case. Call the breakpoint's print_it method. */
4520 return b->ops->print_it (bs);
4f8d1dc6 4521 }
348d480f 4522 break;
3086aeae 4523
e514a9d6 4524 default:
8e65ff28 4525 internal_error (__FILE__, __LINE__,
e2e0b3e5 4526 _("print_bp_stop_message: unrecognized enum value"));
e514a9d6 4527 break;
c906108c 4528 }
c906108c
SS
4529}
4530
edcc5120
TT
4531/* A helper function that prints a shared library stopped event. */
4532
4533static void
4534print_solib_event (int is_catchpoint)
4535{
6fb16ce6 4536 bool any_deleted = !current_program_space->deleted_solibs.empty ();
bcb430e4 4537 bool any_added = !current_program_space->added_solibs.empty ();
edcc5120
TT
4538
4539 if (!is_catchpoint)
4540 {
4541 if (any_added || any_deleted)
112e8700 4542 current_uiout->text (_("Stopped due to shared library event:\n"));
edcc5120 4543 else
112e8700
SM
4544 current_uiout->text (_("Stopped due to shared library event (no "
4545 "libraries added or removed)\n"));
edcc5120
TT
4546 }
4547
112e8700
SM
4548 if (current_uiout->is_mi_like_p ())
4549 current_uiout->field_string ("reason",
4550 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
edcc5120
TT
4551
4552 if (any_deleted)
4553 {
112e8700 4554 current_uiout->text (_(" Inferior unloaded "));
10f489e5 4555 ui_out_emit_list list_emitter (current_uiout, "removed");
6fb16ce6 4556 for (int ix = 0; ix < current_program_space->deleted_solibs.size (); ix++)
edcc5120 4557 {
6fb16ce6
SM
4558 const std::string &name = current_program_space->deleted_solibs[ix];
4559
edcc5120 4560 if (ix > 0)
112e8700
SM
4561 current_uiout->text (" ");
4562 current_uiout->field_string ("library", name);
4563 current_uiout->text ("\n");
edcc5120 4564 }
edcc5120
TT
4565 }
4566
4567 if (any_added)
4568 {
112e8700 4569 current_uiout->text (_(" Inferior loaded "));
10f489e5 4570 ui_out_emit_list list_emitter (current_uiout, "added");
bcb430e4 4571 bool first = true;
52941706 4572 for (so_list *iter : current_program_space->added_solibs)
edcc5120 4573 {
bcb430e4 4574 if (!first)
112e8700 4575 current_uiout->text (" ");
bcb430e4 4576 first = false;
112e8700
SM
4577 current_uiout->field_string ("library", iter->so_name);
4578 current_uiout->text ("\n");
edcc5120 4579 }
edcc5120
TT
4580 }
4581}
4582
e514a9d6
JM
4583/* Print a message indicating what happened. This is called from
4584 normal_stop(). The input to this routine is the head of the bpstat
36dfb11c
TT
4585 list - a list of the eventpoints that caused this stop. KIND is
4586 the target_waitkind for the stopping event. This
e514a9d6
JM
4587 routine calls the generic print routine for printing a message
4588 about reasons for stopping. This will print (for example) the
4589 "Breakpoint n," part of the output. The return value of this
4590 routine is one of:
c906108c 4591
4a64f543 4592 PRINT_UNKNOWN: Means we printed nothing.
917317f4 4593 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4a64f543 4594 code to print the location. An example is
c5aa993b
JM
4595 "Breakpoint 1, " which should be followed by
4596 the location.
917317f4 4597 PRINT_SRC_ONLY: Means we printed something, but there is no need
c5aa993b
JM
4598 to also print the location part of the message.
4599 An example is the catch/throw messages, which
4a64f543 4600 don't require a location appended to the end.
917317f4 4601 PRINT_NOTHING: We have done some printing and we don't need any
4a64f543 4602 further info to be printed. */
c906108c 4603
917317f4 4604enum print_stop_action
36dfb11c 4605bpstat_print (bpstat bs, int kind)
c906108c 4606{
f486487f 4607 enum print_stop_action val;
c5aa993b 4608
c906108c 4609 /* Maybe another breakpoint in the chain caused us to stop.
53a5351d
JM
4610 (Currently all watchpoints go on the bpstat whether hit or not.
4611 That probably could (should) be changed, provided care is taken
c906108c 4612 with respect to bpstat_explains_signal). */
e514a9d6
JM
4613 for (; bs; bs = bs->next)
4614 {
4615 val = print_bp_stop_message (bs);
4616 if (val == PRINT_SRC_ONLY
4617 || val == PRINT_SRC_AND_LOC
4618 || val == PRINT_NOTHING)
4619 return val;
4620 }
c906108c 4621
36dfb11c
TT
4622 /* If we had hit a shared library event breakpoint,
4623 print_bp_stop_message would print out this message. If we hit an
4624 OS-level shared library event, do the same thing. */
4625 if (kind == TARGET_WAITKIND_LOADED)
4626 {
edcc5120 4627 print_solib_event (0);
36dfb11c
TT
4628 return PRINT_NOTHING;
4629 }
4630
e514a9d6 4631 /* We reached the end of the chain, or we got a null BS to start
4a64f543 4632 with and nothing was printed. */
917317f4 4633 return PRINT_UNKNOWN;
c906108c
SS
4634}
4635
bf469271 4636/* Evaluate the boolean expression EXP and return the result. */
c906108c 4637
bf469271
PA
4638static bool
4639breakpoint_cond_eval (expression *exp)
c906108c 4640{
278cd55f 4641 struct value *mark = value_mark ();
bf469271 4642 bool res = value_true (evaluate_expression (exp));
cc59ec59 4643
c906108c 4644 value_free_to_mark (mark);
bf469271 4645 return res;
c906108c
SS
4646}
4647
5760d0ab 4648/* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
c906108c 4649
04afa70c
TT
4650bpstats::bpstats (struct bp_location *bl, bpstat **bs_link_pointer)
4651 : next (NULL),
4652 bp_location_at (bl),
4653 breakpoint_at (bl->owner),
4654 commands (NULL),
04afa70c
TT
4655 print (0),
4656 stop (0),
4657 print_it (print_it_normal)
c906108c 4658{
f431efe5 4659 incref_bp_location (bl);
04afa70c
TT
4660 **bs_link_pointer = this;
4661 *bs_link_pointer = &next;
4662}
4663
4664bpstats::bpstats ()
4665 : next (NULL),
4666 bp_location_at (NULL),
4667 breakpoint_at (NULL),
4668 commands (NULL),
04afa70c
TT
4669 print (0),
4670 stop (0),
4671 print_it (print_it_normal)
4672{
c906108c
SS
4673}
4674\f
d983da9c
DJ
4675/* The target has stopped with waitstatus WS. Check if any hardware
4676 watchpoints have triggered, according to the target. */
4677
4678int
4679watchpoints_triggered (struct target_waitstatus *ws)
4680{
57810aa7 4681 bool stopped_by_watchpoint = target_stopped_by_watchpoint ();
d983da9c
DJ
4682 CORE_ADDR addr;
4683 struct breakpoint *b;
4684
4685 if (!stopped_by_watchpoint)
4686 {
4687 /* We were not stopped by a watchpoint. Mark all watchpoints
4688 as not triggered. */
4689 ALL_BREAKPOINTS (b)
cc60f2e3 4690 if (is_hardware_watchpoint (b))
3a5c3e22
PA
4691 {
4692 struct watchpoint *w = (struct watchpoint *) b;
4693
4694 w->watchpoint_triggered = watch_triggered_no;
4695 }
d983da9c
DJ
4696
4697 return 0;
4698 }
4699
8b88a78e 4700 if (!target_stopped_data_address (current_top_target (), &addr))
d983da9c
DJ
4701 {
4702 /* We were stopped by a watchpoint, but we don't know where.
4703 Mark all watchpoints as unknown. */
4704 ALL_BREAKPOINTS (b)
cc60f2e3 4705 if (is_hardware_watchpoint (b))
3a5c3e22
PA
4706 {
4707 struct watchpoint *w = (struct watchpoint *) b;
4708
4709 w->watchpoint_triggered = watch_triggered_unknown;
4710 }
d983da9c 4711
3c4797ba 4712 return 1;
d983da9c
DJ
4713 }
4714
4715 /* The target could report the data address. Mark watchpoints
4716 affected by this data address as triggered, and all others as not
4717 triggered. */
4718
4719 ALL_BREAKPOINTS (b)
cc60f2e3 4720 if (is_hardware_watchpoint (b))
d983da9c 4721 {
3a5c3e22 4722 struct watchpoint *w = (struct watchpoint *) b;
a5606eee 4723 struct bp_location *loc;
d983da9c 4724
3a5c3e22 4725 w->watchpoint_triggered = watch_triggered_no;
a5606eee 4726 for (loc = b->loc; loc; loc = loc->next)
9c06b0b4 4727 {
3a5c3e22 4728 if (is_masked_watchpoint (b))
9c06b0b4 4729 {
3a5c3e22
PA
4730 CORE_ADDR newaddr = addr & w->hw_wp_mask;
4731 CORE_ADDR start = loc->address & w->hw_wp_mask;
9c06b0b4
TJB
4732
4733 if (newaddr == start)
4734 {
3a5c3e22 4735 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
4736 break;
4737 }
4738 }
4739 /* Exact match not required. Within range is sufficient. */
8b88a78e 4740 else if (target_watchpoint_addr_within_range (current_top_target (),
9c06b0b4
TJB
4741 addr, loc->address,
4742 loc->length))
4743 {
3a5c3e22 4744 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
4745 break;
4746 }
4747 }
d983da9c
DJ
4748 }
4749
4750 return 1;
4751}
4752
bf469271
PA
4753/* Possible return values for watchpoint_check. */
4754enum wp_check_result
4755 {
4756 /* The watchpoint has been deleted. */
4757 WP_DELETED = 1,
4758
4759 /* The value has changed. */
4760 WP_VALUE_CHANGED = 2,
4761
4762 /* The value has not changed. */
4763 WP_VALUE_NOT_CHANGED = 3,
4764
4765 /* Ignore this watchpoint, no matter if the value changed or not. */
4766 WP_IGNORE = 4,
4767 };
c906108c
SS
4768
4769#define BP_TEMPFLAG 1
4770#define BP_HARDWAREFLAG 2
4771
4a64f543 4772/* Evaluate watchpoint condition expression and check if its value
bf469271 4773 changed. */
553e4c11 4774
bf469271
PA
4775static wp_check_result
4776watchpoint_check (bpstat bs)
c906108c 4777{
3a5c3e22 4778 struct watchpoint *b;
c906108c
SS
4779 struct frame_info *fr;
4780 int within_current_scope;
4781
f431efe5 4782 /* BS is built from an existing struct breakpoint. */
2bdf28a0 4783 gdb_assert (bs->breakpoint_at != NULL);
3a5c3e22 4784 b = (struct watchpoint *) bs->breakpoint_at;
d0fb5eae 4785
f6bc2008
PA
4786 /* If this is a local watchpoint, we only want to check if the
4787 watchpoint frame is in scope if the current thread is the thread
4788 that was used to create the watchpoint. */
4789 if (!watchpoint_in_thread_scope (b))
60e1c644 4790 return WP_IGNORE;
f6bc2008 4791
c906108c
SS
4792 if (b->exp_valid_block == NULL)
4793 within_current_scope = 1;
4794 else
4795 {
edb3359d
DJ
4796 struct frame_info *frame = get_current_frame ();
4797 struct gdbarch *frame_arch = get_frame_arch (frame);
4798 CORE_ADDR frame_pc = get_frame_pc (frame);
4799
c9cf6e20 4800 /* stack_frame_destroyed_p() returns a non-zero value if we're
4a64f543
MS
4801 still in the function but the stack frame has already been
4802 invalidated. Since we can't rely on the values of local
4803 variables after the stack has been destroyed, we are treating
4804 the watchpoint in that state as `not changed' without further
4805 checking. Don't mark watchpoints as changed if the current
4806 frame is in an epilogue - even if they are in some other
4807 frame, our view of the stack is likely to be wrong and
4808 frame_find_by_id could error out. */
c9cf6e20 4809 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
60e1c644 4810 return WP_IGNORE;
a0f49112 4811
101dcfbe 4812 fr = frame_find_by_id (b->watchpoint_frame);
c906108c 4813 within_current_scope = (fr != NULL);
69fbadd5
DJ
4814
4815 /* If we've gotten confused in the unwinder, we might have
4816 returned a frame that can't describe this variable. */
edb3359d
DJ
4817 if (within_current_scope)
4818 {
4819 struct symbol *function;
4820
4821 function = get_frame_function (fr);
4822 if (function == NULL
4823 || !contained_in (b->exp_valid_block,
4824 SYMBOL_BLOCK_VALUE (function)))
4825 within_current_scope = 0;
4826 }
69fbadd5 4827
edb3359d 4828 if (within_current_scope)
c906108c
SS
4829 /* If we end up stopping, the current frame will get selected
4830 in normal_stop. So this call to select_frame won't affect
4831 the user. */
0f7d239c 4832 select_frame (fr);
c906108c 4833 }
c5aa993b 4834
c906108c
SS
4835 if (within_current_scope)
4836 {
4a64f543
MS
4837 /* We use value_{,free_to_}mark because it could be a *long*
4838 time before we return to the command level and call
4839 free_all_values. We can't call free_all_values because we
4840 might be in the middle of evaluating a function call. */
c906108c 4841
0cf6dd15 4842 int pc = 0;
9c06b0b4 4843 struct value *mark;
fa4727a6
DJ
4844 struct value *new_val;
4845
c1fc2657 4846 if (is_masked_watchpoint (b))
9c06b0b4
TJB
4847 /* Since we don't know the exact trigger address (from
4848 stopped_data_address), just tell the user we've triggered
4849 a mask watchpoint. */
4850 return WP_VALUE_CHANGED;
4851
4852 mark = value_mark ();
4d01a485 4853 fetch_subexp_value (b->exp.get (), &pc, &new_val, NULL, NULL, 0);
218d2fc6 4854
bb9d5f81
PP
4855 if (b->val_bitsize != 0)
4856 new_val = extract_bitfield_from_watchpoint_value (b, new_val);
4857
4a64f543
MS
4858 /* We use value_equal_contents instead of value_equal because
4859 the latter coerces an array to a pointer, thus comparing just
4860 the address of the array instead of its contents. This is
4861 not what we want. */
fa4727a6 4862 if ((b->val != NULL) != (new_val != NULL)
850645cf
TT
4863 || (b->val != NULL && !value_equal_contents (b->val.get (),
4864 new_val)))
c906108c 4865 {
c906108c 4866 bs->old_val = b->val;
850645cf 4867 b->val = release_value (new_val);
4c1d86d9 4868 b->val_valid = true;
850645cf
TT
4869 if (new_val != NULL)
4870 value_free_to_mark (mark);
c906108c
SS
4871 return WP_VALUE_CHANGED;
4872 }
4873 else
4874 {
60e1c644 4875 /* Nothing changed. */
c906108c 4876 value_free_to_mark (mark);
c906108c
SS
4877 return WP_VALUE_NOT_CHANGED;
4878 }
4879 }
4880 else
4881 {
4882 /* This seems like the only logical thing to do because
c5aa993b
JM
4883 if we temporarily ignored the watchpoint, then when
4884 we reenter the block in which it is valid it contains
4885 garbage (in the case of a function, it may have two
4886 garbage values, one before and one after the prologue).
4887 So we can't even detect the first assignment to it and
4888 watch after that (since the garbage may or may not equal
4889 the first value assigned). */
348d480f
PA
4890 /* We print all the stop information in
4891 breakpoint_ops->print_it, but in this case, by the time we
4892 call breakpoint_ops->print_it this bp will be deleted
4893 already. So we have no choice but print the information
4894 here. */
468afe6c 4895
0e454242 4896 SWITCH_THRU_ALL_UIS ()
468afe6c
PA
4897 {
4898 struct ui_out *uiout = current_uiout;
4899
112e8700
SM
4900 if (uiout->is_mi_like_p ())
4901 uiout->field_string
4902 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
6a831f06
PA
4903 uiout->message ("\nWatchpoint %pF deleted because the program has "
4904 "left the block in\n"
4905 "which its expression is valid.\n",
4906 signed_field ("wpnum", b->number));
468afe6c 4907 }
4ce44c66 4908
cdac0397 4909 /* Make sure the watchpoint's commands aren't executed. */
d1b0a7bf 4910 b->commands = NULL;
d0fb5eae 4911 watchpoint_del_at_next_stop (b);
c906108c
SS
4912
4913 return WP_DELETED;
4914 }
4915}
4916
18a18393 4917/* Return true if it looks like target has stopped due to hitting
348d480f
PA
4918 breakpoint location BL. This function does not check if we should
4919 stop, only if BL explains the stop. */
4920
18a18393 4921static int
6c95b8df 4922bpstat_check_location (const struct bp_location *bl,
accd0bcd 4923 const address_space *aspace, CORE_ADDR bp_addr,
09ac7c10 4924 const struct target_waitstatus *ws)
18a18393
VP
4925{
4926 struct breakpoint *b = bl->owner;
4927
348d480f 4928 /* BL is from an existing breakpoint. */
2bdf28a0
JK
4929 gdb_assert (b != NULL);
4930
bd522513 4931 return b->ops->breakpoint_hit (bl, aspace, bp_addr, ws);
18a18393
VP
4932}
4933
3a5c3e22
PA
4934/* Determine if the watched values have actually changed, and we
4935 should stop. If not, set BS->stop to 0. */
4936
18a18393
VP
4937static void
4938bpstat_check_watchpoint (bpstat bs)
4939{
2bdf28a0 4940 const struct bp_location *bl;
3a5c3e22 4941 struct watchpoint *b;
2bdf28a0
JK
4942
4943 /* BS is built for existing struct breakpoint. */
f431efe5 4944 bl = bs->bp_location_at;
2bdf28a0 4945 gdb_assert (bl != NULL);
3a5c3e22 4946 b = (struct watchpoint *) bs->breakpoint_at;
2bdf28a0 4947 gdb_assert (b != NULL);
18a18393 4948
18a18393 4949 {
18a18393
VP
4950 int must_check_value = 0;
4951
c1fc2657 4952 if (b->type == bp_watchpoint)
18a18393
VP
4953 /* For a software watchpoint, we must always check the
4954 watched value. */
4955 must_check_value = 1;
4956 else if (b->watchpoint_triggered == watch_triggered_yes)
4957 /* We have a hardware watchpoint (read, write, or access)
4958 and the target earlier reported an address watched by
4959 this watchpoint. */
4960 must_check_value = 1;
4961 else if (b->watchpoint_triggered == watch_triggered_unknown
c1fc2657 4962 && b->type == bp_hardware_watchpoint)
18a18393
VP
4963 /* We were stopped by a hardware watchpoint, but the target could
4964 not report the data address. We must check the watchpoint's
4965 value. Access and read watchpoints are out of luck; without
4966 a data address, we can't figure it out. */
4967 must_check_value = 1;
3a5c3e22 4968
18a18393
VP
4969 if (must_check_value)
4970 {
bf469271
PA
4971 wp_check_result e;
4972
a70b8144 4973 try
bf469271
PA
4974 {
4975 e = watchpoint_check (bs);
4976 }
230d2906 4977 catch (const gdb_exception &ex)
bf469271
PA
4978 {
4979 exception_fprintf (gdb_stderr, ex,
4980 "Error evaluating expression "
4981 "for watchpoint %d\n",
4982 b->number);
4983
4984 SWITCH_THRU_ALL_UIS ()
4985 {
4986 printf_filtered (_("Watchpoint %d deleted.\n"),
4987 b->number);
4988 }
4989 watchpoint_del_at_next_stop (b);
4990 e = WP_DELETED;
4991 }
bf469271 4992
18a18393
VP
4993 switch (e)
4994 {
4995 case WP_DELETED:
4996 /* We've already printed what needs to be printed. */
4997 bs->print_it = print_it_done;
4998 /* Stop. */
4999 break;
60e1c644
PA
5000 case WP_IGNORE:
5001 bs->print_it = print_it_noop;
5002 bs->stop = 0;
5003 break;
18a18393 5004 case WP_VALUE_CHANGED:
c1fc2657 5005 if (b->type == bp_read_watchpoint)
18a18393 5006 {
85d721b8
PA
5007 /* There are two cases to consider here:
5008
4a64f543 5009 1. We're watching the triggered memory for reads.
85d721b8
PA
5010 In that case, trust the target, and always report
5011 the watchpoint hit to the user. Even though
5012 reads don't cause value changes, the value may
5013 have changed since the last time it was read, and
5014 since we're not trapping writes, we will not see
5015 those, and as such we should ignore our notion of
5016 old value.
5017
4a64f543 5018 2. We're watching the triggered memory for both
85d721b8
PA
5019 reads and writes. There are two ways this may
5020 happen:
5021
4a64f543 5022 2.1. This is a target that can't break on data
85d721b8
PA
5023 reads only, but can break on accesses (reads or
5024 writes), such as e.g., x86. We detect this case
5025 at the time we try to insert read watchpoints.
5026
4a64f543 5027 2.2. Otherwise, the target supports read
85d721b8
PA
5028 watchpoints, but, the user set an access or write
5029 watchpoint watching the same memory as this read
5030 watchpoint.
5031
5032 If we're watching memory writes as well as reads,
5033 ignore watchpoint hits when we find that the
5034 value hasn't changed, as reads don't cause
5035 changes. This still gives false positives when
5036 the program writes the same value to memory as
5037 what there was already in memory (we will confuse
5038 it for a read), but it's much better than
5039 nothing. */
5040
5041 int other_write_watchpoint = 0;
5042
5043 if (bl->watchpoint_type == hw_read)
5044 {
5045 struct breakpoint *other_b;
5046
5047 ALL_BREAKPOINTS (other_b)
3a5c3e22
PA
5048 if (other_b->type == bp_hardware_watchpoint
5049 || other_b->type == bp_access_watchpoint)
85d721b8 5050 {
3a5c3e22
PA
5051 struct watchpoint *other_w =
5052 (struct watchpoint *) other_b;
5053
5054 if (other_w->watchpoint_triggered
5055 == watch_triggered_yes)
5056 {
5057 other_write_watchpoint = 1;
5058 break;
5059 }
85d721b8
PA
5060 }
5061 }
5062
5063 if (other_write_watchpoint
5064 || bl->watchpoint_type == hw_access)
5065 {
5066 /* We're watching the same memory for writes,
5067 and the value changed since the last time we
5068 updated it, so this trap must be for a write.
5069 Ignore it. */
5070 bs->print_it = print_it_noop;
5071 bs->stop = 0;
5072 }
18a18393
VP
5073 }
5074 break;
5075 case WP_VALUE_NOT_CHANGED:
c1fc2657
SM
5076 if (b->type == bp_hardware_watchpoint
5077 || b->type == bp_watchpoint)
18a18393
VP
5078 {
5079 /* Don't stop: write watchpoints shouldn't fire if
5080 the value hasn't changed. */
5081 bs->print_it = print_it_noop;
5082 bs->stop = 0;
5083 }
5084 /* Stop. */
5085 break;
5086 default:
5087 /* Can't happen. */
18a18393
VP
5088 break;
5089 }
5090 }
5091 else /* must_check_value == 0 */
5092 {
5093 /* This is a case where some watchpoint(s) triggered, but
5094 not at the address of this watchpoint, or else no
5095 watchpoint triggered after all. So don't print
5096 anything for this watchpoint. */
5097 bs->print_it = print_it_noop;
5098 bs->stop = 0;
5099 }
5100 }
5101}
5102
7d4df6a4
DE
5103/* For breakpoints that are currently marked as telling gdb to stop,
5104 check conditions (condition proper, frame, thread and ignore count)
18a18393
VP
5105 of breakpoint referred to by BS. If we should not stop for this
5106 breakpoint, set BS->stop to 0. */
f431efe5 5107
18a18393 5108static void
00431a78 5109bpstat_check_breakpoint_conditions (bpstat bs, thread_info *thread)
18a18393 5110{
2bdf28a0
JK
5111 const struct bp_location *bl;
5112 struct breakpoint *b;
bf469271
PA
5113 /* Assume stop. */
5114 bool condition_result = true;
7d4df6a4
DE
5115 struct expression *cond;
5116
5117 gdb_assert (bs->stop);
2bdf28a0
JK
5118
5119 /* BS is built for existing struct breakpoint. */
f431efe5 5120 bl = bs->bp_location_at;
2bdf28a0 5121 gdb_assert (bl != NULL);
f431efe5 5122 b = bs->breakpoint_at;
2bdf28a0 5123 gdb_assert (b != NULL);
18a18393 5124
b775012e
LM
5125 /* Even if the target evaluated the condition on its end and notified GDB, we
5126 need to do so again since GDB does not know if we stopped due to a
5127 breakpoint or a single step breakpoint. */
5128
18a18393 5129 if (frame_id_p (b->frame_id)
edb3359d 5130 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
18a18393 5131 {
7d4df6a4
DE
5132 bs->stop = 0;
5133 return;
5134 }
60e1c644 5135
12ab52e9
PA
5136 /* If this is a thread/task-specific breakpoint, don't waste cpu
5137 evaluating the condition if this isn't the specified
5138 thread/task. */
00431a78
PA
5139 if ((b->thread != -1 && b->thread != thread->global_num)
5140 || (b->task != 0 && b->task != ada_get_task_number (thread)))
6c1b0f7b
DE
5141 {
5142 bs->stop = 0;
5143 return;
5144 }
5145
6dddc817
DE
5146 /* Evaluate extension language breakpoints that have a "stop" method
5147 implemented. */
5148 bs->stop = breakpoint_ext_lang_cond_says_stop (b);
7371cf6d 5149
7d4df6a4
DE
5150 if (is_watchpoint (b))
5151 {
5152 struct watchpoint *w = (struct watchpoint *) b;
3a5c3e22 5153
4d01a485 5154 cond = w->cond_exp.get ();
7d4df6a4
DE
5155 }
5156 else
4d01a485 5157 cond = bl->cond.get ();
60e1c644 5158
7d4df6a4
DE
5159 if (cond && b->disposition != disp_del_at_next_stop)
5160 {
5161 int within_current_scope = 1;
5162 struct watchpoint * w;
60e1c644 5163
7d4df6a4
DE
5164 /* We use value_mark and value_free_to_mark because it could
5165 be a long time before we return to the command level and
5166 call free_all_values. We can't call free_all_values
5167 because we might be in the middle of evaluating a
5168 function call. */
5169 struct value *mark = value_mark ();
5170
5171 if (is_watchpoint (b))
5172 w = (struct watchpoint *) b;
5173 else
5174 w = NULL;
5175
5176 /* Need to select the frame, with all that implies so that
5177 the conditions will have the right context. Because we
5178 use the frame, we will not see an inlined function's
5179 variables when we arrive at a breakpoint at the start
5180 of the inlined function; the current frame will be the
5181 call site. */
5182 if (w == NULL || w->cond_exp_valid_block == NULL)
5183 select_frame (get_current_frame ());
5184 else
18a18393 5185 {
7d4df6a4
DE
5186 struct frame_info *frame;
5187
5188 /* For local watchpoint expressions, which particular
5189 instance of a local is being watched matters, so we
5190 keep track of the frame to evaluate the expression
5191 in. To evaluate the condition however, it doesn't
5192 really matter which instantiation of the function
5193 where the condition makes sense triggers the
5194 watchpoint. This allows an expression like "watch
5195 global if q > 10" set in `func', catch writes to
5196 global on all threads that call `func', or catch
5197 writes on all recursive calls of `func' by a single
5198 thread. We simply always evaluate the condition in
5199 the innermost frame that's executing where it makes
5200 sense to evaluate the condition. It seems
5201 intuitive. */
5202 frame = block_innermost_frame (w->cond_exp_valid_block);
5203 if (frame != NULL)
5204 select_frame (frame);
5205 else
5206 within_current_scope = 0;
18a18393 5207 }
7d4df6a4 5208 if (within_current_scope)
bf469271 5209 {
a70b8144 5210 try
bf469271
PA
5211 {
5212 condition_result = breakpoint_cond_eval (cond);
5213 }
230d2906 5214 catch (const gdb_exception &ex)
bf469271
PA
5215 {
5216 exception_fprintf (gdb_stderr, ex,
5217 "Error in testing breakpoint condition:\n");
5218 }
bf469271 5219 }
7d4df6a4 5220 else
18a18393 5221 {
7d4df6a4
DE
5222 warning (_("Watchpoint condition cannot be tested "
5223 "in the current scope"));
5224 /* If we failed to set the right context for this
5225 watchpoint, unconditionally report it. */
18a18393 5226 }
7d4df6a4
DE
5227 /* FIXME-someday, should give breakpoint #. */
5228 value_free_to_mark (mark);
18a18393 5229 }
7d4df6a4 5230
bf469271 5231 if (cond && !condition_result)
7d4df6a4
DE
5232 {
5233 bs->stop = 0;
5234 }
7d4df6a4
DE
5235 else if (b->ignore_count > 0)
5236 {
5237 b->ignore_count--;
5238 bs->stop = 0;
5239 /* Increase the hit count even though we don't stop. */
5240 ++(b->hit_count);
76727919 5241 gdb::observers::breakpoint_modified.notify (b);
7d4df6a4 5242 }
18a18393
VP
5243}
5244
1cf4d951
PA
5245/* Returns true if we need to track moribund locations of LOC's type
5246 on the current target. */
5247
5248static int
5249need_moribund_for_location_type (struct bp_location *loc)
5250{
5251 return ((loc->loc_type == bp_loc_software_breakpoint
5252 && !target_supports_stopped_by_sw_breakpoint ())
5253 || (loc->loc_type == bp_loc_hardware_breakpoint
5254 && !target_supports_stopped_by_hw_breakpoint ()));
5255}
5256
ddfe970e 5257/* See breakpoint.h. */
c906108c
SS
5258
5259bpstat
ddfe970e 5260build_bpstat_chain (const address_space *aspace, CORE_ADDR bp_addr,
09ac7c10 5261 const struct target_waitstatus *ws)
c906108c 5262{
ddfe970e 5263 struct breakpoint *b;
5760d0ab 5264 bpstat bs_head = NULL, *bs_link = &bs_head;
c5aa993b 5265
429374b8
JK
5266 ALL_BREAKPOINTS (b)
5267 {
1a853c52 5268 if (!breakpoint_enabled (b))
429374b8 5269 continue;
a5606eee 5270
ddfe970e 5271 for (bp_location *bl = b->loc; bl != NULL; bl = bl->next)
429374b8 5272 {
4a64f543
MS
5273 /* For hardware watchpoints, we look only at the first
5274 location. The watchpoint_check function will work on the
5275 entire expression, not the individual locations. For
5276 read watchpoints, the watchpoints_triggered function has
5277 checked all locations already. */
429374b8
JK
5278 if (b->type == bp_hardware_watchpoint && bl != b->loc)
5279 break;
18a18393 5280
f6592439 5281 if (!bl->enabled || bl->shlib_disabled)
429374b8 5282 continue;
c5aa993b 5283
09ac7c10 5284 if (!bpstat_check_location (bl, aspace, bp_addr, ws))
429374b8 5285 continue;
c5aa993b 5286
4a64f543
MS
5287 /* Come here if it's a watchpoint, or if the break address
5288 matches. */
c5aa993b 5289
ddfe970e
KS
5290 bpstat bs = new bpstats (bl, &bs_link); /* Alloc a bpstat to
5291 explain stop. */
c5aa993b 5292
f431efe5
PA
5293 /* Assume we stop. Should we find a watchpoint that is not
5294 actually triggered, or if the condition of the breakpoint
5295 evaluates as false, we'll reset 'stop' to 0. */
429374b8
JK
5296 bs->stop = 1;
5297 bs->print = 1;
d983da9c 5298
f431efe5
PA
5299 /* If this is a scope breakpoint, mark the associated
5300 watchpoint as triggered so that we will handle the
5301 out-of-scope event. We'll get to the watchpoint next
5302 iteration. */
d0fb5eae 5303 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
3a5c3e22
PA
5304 {
5305 struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
5306
5307 w->watchpoint_triggered = watch_triggered_yes;
5308 }
f431efe5
PA
5309 }
5310 }
5311
7c16b83e 5312 /* Check if a moribund breakpoint explains the stop. */
1cf4d951
PA
5313 if (!target_supports_stopped_by_sw_breakpoint ()
5314 || !target_supports_stopped_by_hw_breakpoint ())
f431efe5 5315 {
1123588c 5316 for (bp_location *loc : moribund_locations)
f431efe5 5317 {
1cf4d951
PA
5318 if (breakpoint_location_address_match (loc, aspace, bp_addr)
5319 && need_moribund_for_location_type (loc))
5320 {
ddfe970e 5321 bpstat bs = new bpstats (loc, &bs_link);
1cf4d951
PA
5322 /* For hits of moribund locations, we should just proceed. */
5323 bs->stop = 0;
5324 bs->print = 0;
5325 bs->print_it = print_it_noop;
5326 }
f431efe5
PA
5327 }
5328 }
5329
ddfe970e
KS
5330 return bs_head;
5331}
5332
5333/* See breakpoint.h. */
5334
5335bpstat
5336bpstat_stop_status (const address_space *aspace,
00431a78 5337 CORE_ADDR bp_addr, thread_info *thread,
ddfe970e
KS
5338 const struct target_waitstatus *ws,
5339 bpstat stop_chain)
5340{
5341 struct breakpoint *b = NULL;
5342 /* First item of allocated bpstat's. */
5343 bpstat bs_head = stop_chain;
5344 bpstat bs;
5345 int need_remove_insert;
5346 int removed_any;
5347
5348 /* First, build the bpstat chain with locations that explain a
5349 target stop, while being careful to not set the target running,
5350 as that may invalidate locations (in particular watchpoint
5351 locations are recreated). Resuming will happen here with
5352 breakpoint conditions or watchpoint expressions that include
5353 inferior function calls. */
5354 if (bs_head == NULL)
5355 bs_head = build_bpstat_chain (aspace, bp_addr, ws);
5356
edcc5120
TT
5357 /* A bit of special processing for shlib breakpoints. We need to
5358 process solib loading here, so that the lists of loaded and
5359 unloaded libraries are correct before we handle "catch load" and
5360 "catch unload". */
5361 for (bs = bs_head; bs != NULL; bs = bs->next)
5362 {
5d268276 5363 if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
edcc5120
TT
5364 {
5365 handle_solib_event ();
5366 break;
5367 }
5368 }
5369
f431efe5
PA
5370 /* Now go through the locations that caused the target to stop, and
5371 check whether we're interested in reporting this stop to higher
5372 layers, or whether we should resume the target transparently. */
5373
5374 removed_any = 0;
5375
5760d0ab 5376 for (bs = bs_head; bs != NULL; bs = bs->next)
f431efe5
PA
5377 {
5378 if (!bs->stop)
5379 continue;
5380
f431efe5 5381 b = bs->breakpoint_at;
348d480f
PA
5382 b->ops->check_status (bs);
5383 if (bs->stop)
28010a5d 5384 {
00431a78 5385 bpstat_check_breakpoint_conditions (bs, thread);
f431efe5 5386
429374b8
JK
5387 if (bs->stop)
5388 {
5389 ++(b->hit_count);
76727919 5390 gdb::observers::breakpoint_modified.notify (b);
c906108c 5391
4a64f543 5392 /* We will stop here. */
429374b8
JK
5393 if (b->disposition == disp_disable)
5394 {
816338b5 5395 --(b->enable_count);
1a853c52 5396 if (b->enable_count <= 0)
429374b8 5397 b->enable_state = bp_disabled;
f431efe5 5398 removed_any = 1;
429374b8
JK
5399 }
5400 if (b->silent)
5401 bs->print = 0;
5402 bs->commands = b->commands;
abf85f46 5403 if (command_line_is_silent (bs->commands
d1b0a7bf 5404 ? bs->commands.get () : NULL))
abf85f46 5405 bs->print = 0;
9d6e6e84
HZ
5406
5407 b->ops->after_condition_true (bs);
429374b8
JK
5408 }
5409
348d480f 5410 }
a9b3a50f
PA
5411
5412 /* Print nothing for this entry if we don't stop or don't
5413 print. */
5414 if (!bs->stop || !bs->print)
5415 bs->print_it = print_it_noop;
429374b8 5416 }
876fa593 5417
d983da9c
DJ
5418 /* If we aren't stopping, the value of some hardware watchpoint may
5419 not have changed, but the intermediate memory locations we are
5420 watching may have. Don't bother if we're stopping; this will get
5421 done later. */
d832cb68 5422 need_remove_insert = 0;
5760d0ab
JK
5423 if (! bpstat_causes_stop (bs_head))
5424 for (bs = bs_head; bs != NULL; bs = bs->next)
d983da9c 5425 if (!bs->stop
f431efe5
PA
5426 && bs->breakpoint_at
5427 && is_hardware_watchpoint (bs->breakpoint_at))
d983da9c 5428 {
3a5c3e22
PA
5429 struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
5430
5431 update_watchpoint (w, 0 /* don't reparse. */);
d832cb68 5432 need_remove_insert = 1;
d983da9c
DJ
5433 }
5434
d832cb68 5435 if (need_remove_insert)
44702360 5436 update_global_location_list (UGLL_MAY_INSERT);
f431efe5 5437 else if (removed_any)
44702360 5438 update_global_location_list (UGLL_DONT_INSERT);
d832cb68 5439
5760d0ab 5440 return bs_head;
c906108c 5441}
628fe4e4
JK
5442
5443static void
5444handle_jit_event (void)
5445{
5446 struct frame_info *frame;
5447 struct gdbarch *gdbarch;
5448
243a9253
PA
5449 if (debug_infrun)
5450 fprintf_unfiltered (gdb_stdlog, "handling bp_jit_event\n");
5451
628fe4e4
JK
5452 /* Switch terminal for any messages produced by
5453 breakpoint_re_set. */
223ffa71 5454 target_terminal::ours_for_output ();
628fe4e4
JK
5455
5456 frame = get_current_frame ();
5457 gdbarch = get_frame_arch (frame);
fe053b9e 5458 objfile *jiter = symbol_objfile (get_frame_function (frame));
628fe4e4 5459
fe053b9e 5460 jit_event_handler (gdbarch, jiter);
628fe4e4 5461
223ffa71 5462 target_terminal::inferior ();
628fe4e4
JK
5463}
5464
5465/* Prepare WHAT final decision for infrun. */
5466
5467/* Decide what infrun needs to do with this bpstat. */
5468
c906108c 5469struct bpstat_what
0e30163f 5470bpstat_what (bpstat bs_head)
c906108c 5471{
c906108c 5472 struct bpstat_what retval;
0e30163f 5473 bpstat bs;
c906108c 5474
628fe4e4 5475 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
aa7d318d 5476 retval.call_dummy = STOP_NONE;
e2d0f980 5477 retval.is_longjmp = false;
628fe4e4 5478
0e30163f 5479 for (bs = bs_head; bs != NULL; bs = bs->next)
c906108c 5480 {
628fe4e4
JK
5481 /* Extract this BS's action. After processing each BS, we check
5482 if its action overrides all we've seem so far. */
5483 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
5484 enum bptype bptype;
5485
c906108c 5486 if (bs->breakpoint_at == NULL)
628fe4e4
JK
5487 {
5488 /* I suspect this can happen if it was a momentary
5489 breakpoint which has since been deleted. */
5490 bptype = bp_none;
5491 }
20874c92 5492 else
f431efe5 5493 bptype = bs->breakpoint_at->type;
628fe4e4
JK
5494
5495 switch (bptype)
c906108c
SS
5496 {
5497 case bp_none:
628fe4e4 5498 break;
c906108c
SS
5499 case bp_breakpoint:
5500 case bp_hardware_breakpoint:
7c16b83e 5501 case bp_single_step:
c906108c
SS
5502 case bp_until:
5503 case bp_finish:
a9b3a50f 5504 case bp_shlib_event:
c906108c
SS
5505 if (bs->stop)
5506 {
5507 if (bs->print)
628fe4e4 5508 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 5509 else
628fe4e4 5510 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
5511 }
5512 else
628fe4e4 5513 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5514 break;
5515 case bp_watchpoint:
5516 case bp_hardware_watchpoint:
5517 case bp_read_watchpoint:
5518 case bp_access_watchpoint:
5519 if (bs->stop)
5520 {
5521 if (bs->print)
628fe4e4 5522 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 5523 else
628fe4e4 5524 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
5525 }
5526 else
628fe4e4
JK
5527 {
5528 /* There was a watchpoint, but we're not stopping.
5529 This requires no further action. */
5530 }
c906108c
SS
5531 break;
5532 case bp_longjmp:
e2e4d78b 5533 case bp_longjmp_call_dummy:
186c406b 5534 case bp_exception:
0a39bb32
PA
5535 if (bs->stop)
5536 {
5537 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
5538 retval.is_longjmp = bptype != bp_exception;
5539 }
5540 else
5541 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5542 break;
5543 case bp_longjmp_resume:
186c406b 5544 case bp_exception_resume:
0a39bb32
PA
5545 if (bs->stop)
5546 {
5547 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
5548 retval.is_longjmp = bptype == bp_longjmp_resume;
5549 }
5550 else
5551 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5552 break;
5553 case bp_step_resume:
5554 if (bs->stop)
628fe4e4
JK
5555 this_action = BPSTAT_WHAT_STEP_RESUME;
5556 else
c906108c 5557 {
628fe4e4
JK
5558 /* It is for the wrong frame. */
5559 this_action = BPSTAT_WHAT_SINGLE;
c906108c 5560 }
c906108c 5561 break;
2c03e5be
PA
5562 case bp_hp_step_resume:
5563 if (bs->stop)
5564 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
5565 else
5566 {
5567 /* It is for the wrong frame. */
5568 this_action = BPSTAT_WHAT_SINGLE;
5569 }
5570 break;
c906108c 5571 case bp_watchpoint_scope:
c4093a6a 5572 case bp_thread_event:
1900040c 5573 case bp_overlay_event:
0fd8e87f 5574 case bp_longjmp_master:
aa7d318d 5575 case bp_std_terminate_master:
186c406b 5576 case bp_exception_master:
628fe4e4 5577 this_action = BPSTAT_WHAT_SINGLE;
c4093a6a 5578 break;
ce78b96d 5579 case bp_catchpoint:
c5aa993b
JM
5580 if (bs->stop)
5581 {
5582 if (bs->print)
628fe4e4 5583 this_action = BPSTAT_WHAT_STOP_NOISY;
c5aa993b 5584 else
628fe4e4 5585 this_action = BPSTAT_WHAT_STOP_SILENT;
c5aa993b
JM
5586 }
5587 else
628fe4e4 5588 {
cb1e4e32
PA
5589 /* Some catchpoints are implemented with breakpoints.
5590 For those, we need to step over the breakpoint. */
5591 if (bs->bp_location_at->loc_type != bp_loc_other)
5592 this_action = BPSTAT_WHAT_SINGLE;
628fe4e4
JK
5593 }
5594 break;
628fe4e4 5595 case bp_jit_event:
628fe4e4 5596 this_action = BPSTAT_WHAT_SINGLE;
c5aa993b 5597 break;
c906108c 5598 case bp_call_dummy:
53a5351d
JM
5599 /* Make sure the action is stop (silent or noisy),
5600 so infrun.c pops the dummy frame. */
aa7d318d 5601 retval.call_dummy = STOP_STACK_DUMMY;
628fe4e4 5602 this_action = BPSTAT_WHAT_STOP_SILENT;
aa7d318d
TT
5603 break;
5604 case bp_std_terminate:
5605 /* Make sure the action is stop (silent or noisy),
5606 so infrun.c pops the dummy frame. */
aa7d318d 5607 retval.call_dummy = STOP_STD_TERMINATE;
628fe4e4 5608 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c 5609 break;
1042e4c0 5610 case bp_tracepoint:
7a697b8d 5611 case bp_fast_tracepoint:
0fb4aa4b 5612 case bp_static_tracepoint:
1042e4c0
SS
5613 /* Tracepoint hits should not be reported back to GDB, and
5614 if one got through somehow, it should have been filtered
5615 out already. */
5616 internal_error (__FILE__, __LINE__,
7a697b8d 5617 _("bpstat_what: tracepoint encountered"));
0e30163f
JK
5618 break;
5619 case bp_gnu_ifunc_resolver:
5620 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
5621 this_action = BPSTAT_WHAT_SINGLE;
5622 break;
5623 case bp_gnu_ifunc_resolver_return:
5624 /* The breakpoint will be removed, execution will restart from the
5625 PC of the former breakpoint. */
5626 this_action = BPSTAT_WHAT_KEEP_CHECKING;
5627 break;
e7e0cddf
SS
5628
5629 case bp_dprintf:
a11cfd87
HZ
5630 if (bs->stop)
5631 this_action = BPSTAT_WHAT_STOP_SILENT;
5632 else
5633 this_action = BPSTAT_WHAT_SINGLE;
e7e0cddf
SS
5634 break;
5635
628fe4e4
JK
5636 default:
5637 internal_error (__FILE__, __LINE__,
5638 _("bpstat_what: unhandled bptype %d"), (int) bptype);
c906108c 5639 }
628fe4e4 5640
325fac50 5641 retval.main_action = std::max (retval.main_action, this_action);
c906108c 5642 }
628fe4e4 5643
243a9253
PA
5644 return retval;
5645}
628fe4e4 5646
243a9253
PA
5647void
5648bpstat_run_callbacks (bpstat bs_head)
5649{
5650 bpstat bs;
628fe4e4 5651
0e30163f
JK
5652 for (bs = bs_head; bs != NULL; bs = bs->next)
5653 {
5654 struct breakpoint *b = bs->breakpoint_at;
5655
5656 if (b == NULL)
5657 continue;
5658 switch (b->type)
5659 {
243a9253
PA
5660 case bp_jit_event:
5661 handle_jit_event ();
5662 break;
0e30163f
JK
5663 case bp_gnu_ifunc_resolver:
5664 gnu_ifunc_resolver_stop (b);
5665 break;
5666 case bp_gnu_ifunc_resolver_return:
5667 gnu_ifunc_resolver_return_stop (b);
5668 break;
5669 }
5670 }
c906108c
SS
5671}
5672
4c462cb0 5673/* See breakpoint.h. */
c906108c 5674
4c462cb0
SM
5675bool
5676bpstat_should_step ()
c906108c
SS
5677{
5678 struct breakpoint *b;
cc59ec59 5679
c906108c 5680 ALL_BREAKPOINTS (b)
717a8278 5681 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
4c462cb0
SM
5682 return true;
5683 return false;
c906108c
SS
5684}
5685
4c462cb0
SM
5686/* See breakpoint.h. */
5687
5688bool
67822962
PA
5689bpstat_causes_stop (bpstat bs)
5690{
5691 for (; bs != NULL; bs = bs->next)
5692 if (bs->stop)
4c462cb0 5693 return true;
67822962 5694
4c462cb0 5695 return false;
67822962
PA
5696}
5697
c906108c 5698\f
c5aa993b 5699
170b53b2
UW
5700/* Compute a string of spaces suitable to indent the next line
5701 so it starts at the position corresponding to the table column
5702 named COL_NAME in the currently active table of UIOUT. */
5703
5704static char *
5705wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
5706{
5707 static char wrap_indent[80];
5708 int i, total_width, width, align;
c5209615 5709 const char *text;
170b53b2
UW
5710
5711 total_width = 0;
112e8700 5712 for (i = 1; uiout->query_table_field (i, &width, &align, &text); i++)
170b53b2
UW
5713 {
5714 if (strcmp (text, col_name) == 0)
5715 {
5716 gdb_assert (total_width < sizeof wrap_indent);
5717 memset (wrap_indent, ' ', total_width);
5718 wrap_indent[total_width] = 0;
5719
5720 return wrap_indent;
5721 }
5722
5723 total_width += width + 1;
5724 }
5725
5726 return NULL;
5727}
5728
b775012e
LM
5729/* Determine if the locations of this breakpoint will have their conditions
5730 evaluated by the target, host or a mix of both. Returns the following:
5731
5732 "host": Host evals condition.
5733 "host or target": Host or Target evals condition.
5734 "target": Target evals condition.
5735*/
5736
5737static const char *
5738bp_condition_evaluator (struct breakpoint *b)
5739{
5740 struct bp_location *bl;
5741 char host_evals = 0;
5742 char target_evals = 0;
5743
5744 if (!b)
5745 return NULL;
5746
5747 if (!is_breakpoint (b))
5748 return NULL;
5749
5750 if (gdb_evaluates_breakpoint_condition_p ()
5751 || !target_supports_evaluation_of_breakpoint_conditions ())
5752 return condition_evaluation_host;
5753
5754 for (bl = b->loc; bl; bl = bl->next)
5755 {
5756 if (bl->cond_bytecode)
5757 target_evals++;
5758 else
5759 host_evals++;
5760 }
5761
5762 if (host_evals && target_evals)
5763 return condition_evaluation_both;
5764 else if (target_evals)
5765 return condition_evaluation_target;
5766 else
5767 return condition_evaluation_host;
5768}
5769
5770/* Determine the breakpoint location's condition evaluator. This is
5771 similar to bp_condition_evaluator, but for locations. */
5772
5773static const char *
5774bp_location_condition_evaluator (struct bp_location *bl)
5775{
5776 if (bl && !is_breakpoint (bl->owner))
5777 return NULL;
5778
5779 if (gdb_evaluates_breakpoint_condition_p ()
5780 || !target_supports_evaluation_of_breakpoint_conditions ())
5781 return condition_evaluation_host;
5782
5783 if (bl && bl->cond_bytecode)
5784 return condition_evaluation_target;
5785 else
5786 return condition_evaluation_host;
5787}
5788
859825b8
JK
5789/* Print the LOC location out of the list of B->LOC locations. */
5790
170b53b2
UW
5791static void
5792print_breakpoint_location (struct breakpoint *b,
5793 struct bp_location *loc)
0d381245 5794{
79a45e25 5795 struct ui_out *uiout = current_uiout;
5ed8105e
PA
5796
5797 scoped_restore_current_program_space restore_pspace;
6c95b8df 5798
859825b8
JK
5799 if (loc != NULL && loc->shlib_disabled)
5800 loc = NULL;
5801
6c95b8df
PA
5802 if (loc != NULL)
5803 set_current_program_space (loc->pspace);
5804
56435ebe 5805 if (b->display_canonical)
d28cd78a 5806 uiout->field_string ("what", event_location_to_string (b->location.get ()));
2f202fde 5807 else if (loc && loc->symtab)
0d381245 5808 {
4a27f119
KS
5809 const struct symbol *sym = loc->symbol;
5810
0d381245
VP
5811 if (sym)
5812 {
112e8700 5813 uiout->text ("in ");
987012b8 5814 uiout->field_string ("func", sym->print_name (),
e43b10e1 5815 function_name_style.style ());
112e8700
SM
5816 uiout->text (" ");
5817 uiout->wrap_hint (wrap_indent_at_field (uiout, "what"));
5818 uiout->text ("at ");
0d381245 5819 }
112e8700 5820 uiout->field_string ("file",
cbe56571 5821 symtab_to_filename_for_display (loc->symtab),
e43b10e1 5822 file_name_style.style ());
112e8700 5823 uiout->text (":");
05cba821 5824
112e8700
SM
5825 if (uiout->is_mi_like_p ())
5826 uiout->field_string ("fullname", symtab_to_fullname (loc->symtab));
0d381245 5827
381befee 5828 uiout->field_signed ("line", loc->line_number);
0d381245 5829 }
859825b8 5830 else if (loc)
0d381245 5831 {
d7e74731 5832 string_file stb;
170b53b2 5833
d7e74731 5834 print_address_symbolic (loc->gdbarch, loc->address, &stb,
22e722e1 5835 demangle, "");
112e8700 5836 uiout->field_stream ("at", stb);
0d381245 5837 }
859825b8 5838 else
f00aae0f 5839 {
d28cd78a
TT
5840 uiout->field_string ("pending",
5841 event_location_to_string (b->location.get ()));
f00aae0f
KS
5842 /* If extra_string is available, it could be holding a condition
5843 or dprintf arguments. In either case, make sure it is printed,
5844 too, but only for non-MI streams. */
112e8700 5845 if (!uiout->is_mi_like_p () && b->extra_string != NULL)
f00aae0f
KS
5846 {
5847 if (b->type == bp_dprintf)
112e8700 5848 uiout->text (",");
f00aae0f 5849 else
112e8700
SM
5850 uiout->text (" ");
5851 uiout->text (b->extra_string);
f00aae0f
KS
5852 }
5853 }
6c95b8df 5854
b775012e
LM
5855 if (loc && is_breakpoint (b)
5856 && breakpoint_condition_evaluation_mode () == condition_evaluation_target
5857 && bp_condition_evaluator (b) == condition_evaluation_both)
5858 {
112e8700
SM
5859 uiout->text (" (");
5860 uiout->field_string ("evaluated-by",
b775012e 5861 bp_location_condition_evaluator (loc));
112e8700 5862 uiout->text (")");
b775012e 5863 }
0d381245
VP
5864}
5865
269b11a2
PA
5866static const char *
5867bptype_string (enum bptype type)
c906108c 5868{
c4093a6a
JM
5869 struct ep_type_description
5870 {
5871 enum bptype type;
a121b7c1 5872 const char *description;
c4093a6a
JM
5873 };
5874 static struct ep_type_description bptypes[] =
c906108c 5875 {
c5aa993b
JM
5876 {bp_none, "?deleted?"},
5877 {bp_breakpoint, "breakpoint"},
c906108c 5878 {bp_hardware_breakpoint, "hw breakpoint"},
7c16b83e 5879 {bp_single_step, "sw single-step"},
c5aa993b
JM
5880 {bp_until, "until"},
5881 {bp_finish, "finish"},
5882 {bp_watchpoint, "watchpoint"},
c906108c 5883 {bp_hardware_watchpoint, "hw watchpoint"},
c5aa993b
JM
5884 {bp_read_watchpoint, "read watchpoint"},
5885 {bp_access_watchpoint, "acc watchpoint"},
5886 {bp_longjmp, "longjmp"},
5887 {bp_longjmp_resume, "longjmp resume"},
e2e4d78b 5888 {bp_longjmp_call_dummy, "longjmp for call dummy"},
186c406b
TT
5889 {bp_exception, "exception"},
5890 {bp_exception_resume, "exception resume"},
c5aa993b 5891 {bp_step_resume, "step resume"},
2c03e5be 5892 {bp_hp_step_resume, "high-priority step resume"},
c5aa993b
JM
5893 {bp_watchpoint_scope, "watchpoint scope"},
5894 {bp_call_dummy, "call dummy"},
aa7d318d 5895 {bp_std_terminate, "std::terminate"},
c5aa993b 5896 {bp_shlib_event, "shlib events"},
c4093a6a 5897 {bp_thread_event, "thread events"},
1900040c 5898 {bp_overlay_event, "overlay events"},
0fd8e87f 5899 {bp_longjmp_master, "longjmp master"},
aa7d318d 5900 {bp_std_terminate_master, "std::terminate master"},
186c406b 5901 {bp_exception_master, "exception master"},
ce78b96d 5902 {bp_catchpoint, "catchpoint"},
1042e4c0 5903 {bp_tracepoint, "tracepoint"},
7a697b8d 5904 {bp_fast_tracepoint, "fast tracepoint"},
0fb4aa4b 5905 {bp_static_tracepoint, "static tracepoint"},
e7e0cddf 5906 {bp_dprintf, "dprintf"},
4efc6507 5907 {bp_jit_event, "jit events"},
0e30163f
JK
5908 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
5909 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
c5aa993b 5910 };
269b11a2
PA
5911
5912 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
5913 || ((int) type != bptypes[(int) type].type))
5914 internal_error (__FILE__, __LINE__,
5915 _("bptypes table does not describe type #%d."),
5916 (int) type);
5917
5918 return bptypes[(int) type].description;
5919}
5920
998580f1
MK
5921/* For MI, output a field named 'thread-groups' with a list as the value.
5922 For CLI, prefix the list with the string 'inf'. */
5923
5924static void
5925output_thread_groups (struct ui_out *uiout,
5926 const char *field_name,
5c632425 5927 const std::vector<int> &inf_nums,
998580f1
MK
5928 int mi_only)
5929{
112e8700 5930 int is_mi = uiout->is_mi_like_p ();
998580f1
MK
5931
5932 /* For backward compatibility, don't display inferiors in CLI unless
5933 there are several. Always display them for MI. */
5934 if (!is_mi && mi_only)
5935 return;
5936
10f489e5 5937 ui_out_emit_list list_emitter (uiout, field_name);
752eb8b4 5938
5c632425 5939 for (size_t i = 0; i < inf_nums.size (); i++)
998580f1
MK
5940 {
5941 if (is_mi)
5942 {
5943 char mi_group[10];
5944
5c632425 5945 xsnprintf (mi_group, sizeof (mi_group), "i%d", inf_nums[i]);
112e8700 5946 uiout->field_string (NULL, mi_group);
998580f1
MK
5947 }
5948 else
5949 {
5950 if (i == 0)
112e8700 5951 uiout->text (" inf ");
998580f1 5952 else
112e8700 5953 uiout->text (", ");
998580f1 5954
5c632425 5955 uiout->text (plongest (inf_nums[i]));
998580f1
MK
5956 }
5957 }
998580f1
MK
5958}
5959
a38118e5
PA
5960/* Print B to gdb_stdout. If RAW_LOC, print raw breakpoint locations
5961 instead of going via breakpoint_ops::print_one. This makes "maint
5962 info breakpoints" show the software breakpoint locations of
5963 catchpoints, which are considered internal implementation
5964 detail. */
269b11a2
PA
5965
5966static void
5967print_one_breakpoint_location (struct breakpoint *b,
5968 struct bp_location *loc,
5969 int loc_number,
5970 struct bp_location **last_loc,
a38118e5 5971 int allflag, bool raw_loc)
269b11a2
PA
5972{
5973 struct command_line *l;
c2c6d25f 5974 static char bpenables[] = "nynny";
c906108c 5975
79a45e25 5976 struct ui_out *uiout = current_uiout;
0d381245
VP
5977 int header_of_multiple = 0;
5978 int part_of_multiple = (loc != NULL);
79a45b7d
TT
5979 struct value_print_options opts;
5980
5981 get_user_print_options (&opts);
0d381245
VP
5982
5983 gdb_assert (!loc || loc_number != 0);
4a64f543
MS
5984 /* See comment in print_one_breakpoint concerning treatment of
5985 breakpoints with single disabled location. */
0d381245
VP
5986 if (loc == NULL
5987 && (b->loc != NULL
5988 && (b->loc->next != NULL || !b->loc->enabled)))
5989 header_of_multiple = 1;
5990 if (loc == NULL)
5991 loc = b->loc;
5992
c4093a6a
JM
5993 annotate_record ();
5994
5995 /* 1 */
5996 annotate_field (0);
0d381245 5997 if (part_of_multiple)
528e1572 5998 uiout->field_fmt ("number", "%d.%d", b->number, loc_number);
0d381245 5999 else
381befee 6000 uiout->field_signed ("number", b->number);
c4093a6a
JM
6001
6002 /* 2 */
6003 annotate_field (1);
0d381245 6004 if (part_of_multiple)
112e8700 6005 uiout->field_skip ("type");
269b11a2 6006 else
112e8700 6007 uiout->field_string ("type", bptype_string (b->type));
c4093a6a
JM
6008
6009 /* 3 */
6010 annotate_field (2);
0d381245 6011 if (part_of_multiple)
112e8700 6012 uiout->field_skip ("disp");
0d381245 6013 else
112e8700 6014 uiout->field_string ("disp", bpdisp_text (b->disposition));
0d381245 6015
c4093a6a
JM
6016 /* 4 */
6017 annotate_field (3);
0d381245 6018 if (part_of_multiple)
112e8700 6019 uiout->field_string ("enabled", loc->enabled ? "y" : "n");
0d381245 6020 else
112e8700 6021 uiout->field_fmt ("enabled", "%c", bpenables[(int) b->enable_state]);
0d381245 6022
c4093a6a 6023 /* 5 and 6 */
a38118e5 6024 if (!raw_loc && b->ops != NULL && b->ops->print_one != NULL)
b58a68fe 6025 b->ops->print_one (b, last_loc);
3086aeae 6026 else
a38118e5
PA
6027 {
6028 if (is_watchpoint (b))
3a5c3e22
PA
6029 {
6030 struct watchpoint *w = (struct watchpoint *) b;
6031
6032 /* Field 4, the address, is omitted (which makes the columns
6033 not line up too nicely with the headers, but the effect
6034 is relatively readable). */
6035 if (opts.addressprint)
112e8700 6036 uiout->field_skip ("addr");
3a5c3e22 6037 annotate_field (5);
112e8700 6038 uiout->field_string ("what", w->exp_string);
3a5c3e22 6039 }
f06f1252
TT
6040 else if (!is_catchpoint (b) || is_exception_catchpoint (b)
6041 || is_ada_exception_catchpoint (b))
a38118e5
PA
6042 {
6043 if (opts.addressprint)
6044 {
6045 annotate_field (4);
6046 if (header_of_multiple)
7f6aba03
TT
6047 uiout->field_string ("addr", "<MULTIPLE>",
6048 metadata_style.style ());
a38118e5 6049 else if (b->loc == NULL || loc->shlib_disabled)
7f6aba03
TT
6050 uiout->field_string ("addr", "<PENDING>",
6051 metadata_style.style ());
a38118e5
PA
6052 else
6053 uiout->field_core_addr ("addr",
6054 loc->gdbarch, loc->address);
6055 }
6056 annotate_field (5);
6057 if (!header_of_multiple)
6058 print_breakpoint_location (b, loc);
6059 if (b->loc)
6060 *last_loc = b->loc;
6061 }
6062 }
6c95b8df 6063
998580f1 6064 if (loc != NULL && !header_of_multiple)
6c95b8df 6065 {
5c632425 6066 std::vector<int> inf_nums;
998580f1 6067 int mi_only = 1;
6c95b8df 6068
08036331 6069 for (inferior *inf : all_inferiors ())
6c95b8df
PA
6070 {
6071 if (inf->pspace == loc->pspace)
5c632425 6072 inf_nums.push_back (inf->num);
6c95b8df 6073 }
998580f1
MK
6074
6075 /* For backward compatibility, don't display inferiors in CLI unless
6076 there are several. Always display for MI. */
6077 if (allflag
6078 || (!gdbarch_has_global_breakpoints (target_gdbarch ())
94c93c35 6079 && (program_spaces.size () > 1
998580f1
MK
6080 || number_of_inferiors () > 1)
6081 /* LOC is for existing B, it cannot be in
6082 moribund_locations and thus having NULL OWNER. */
6083 && loc->owner->type != bp_catchpoint))
6084 mi_only = 0;
5c632425 6085 output_thread_groups (uiout, "thread-groups", inf_nums, mi_only);
6c95b8df
PA
6086 }
6087
4a306c9a 6088 if (!part_of_multiple)
c4093a6a 6089 {
4a306c9a
JB
6090 if (b->thread != -1)
6091 {
6092 /* FIXME: This seems to be redundant and lost here; see the
4a64f543 6093 "stop only in" line a little further down. */
112e8700 6094 uiout->text (" thread ");
381befee 6095 uiout->field_signed ("thread", b->thread);
4a306c9a
JB
6096 }
6097 else if (b->task != 0)
6098 {
112e8700 6099 uiout->text (" task ");
381befee 6100 uiout->field_signed ("task", b->task);
4a306c9a 6101 }
c4093a6a 6102 }
f1310107 6103
112e8700 6104 uiout->text ("\n");
f1310107 6105
348d480f 6106 if (!part_of_multiple)
f1310107
TJB
6107 b->ops->print_one_detail (b, uiout);
6108
0d381245 6109 if (part_of_multiple && frame_id_p (b->frame_id))
c4093a6a
JM
6110 {
6111 annotate_field (6);
112e8700 6112 uiout->text ("\tstop only in stack frame at ");
e5dd4106 6113 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
818dd999 6114 the frame ID. */
112e8700 6115 uiout->field_core_addr ("frame",
5af949e3 6116 b->gdbarch, b->frame_id.stack_addr);
112e8700 6117 uiout->text ("\n");
c4093a6a
JM
6118 }
6119
28010a5d 6120 if (!part_of_multiple && b->cond_string)
c4093a6a
JM
6121 {
6122 annotate_field (7);
d77f58be 6123 if (is_tracepoint (b))
112e8700 6124 uiout->text ("\ttrace only if ");
1042e4c0 6125 else
112e8700
SM
6126 uiout->text ("\tstop only if ");
6127 uiout->field_string ("cond", b->cond_string);
b775012e
LM
6128
6129 /* Print whether the target is doing the breakpoint's condition
6130 evaluation. If GDB is doing the evaluation, don't print anything. */
6131 if (is_breakpoint (b)
6132 && breakpoint_condition_evaluation_mode ()
6133 == condition_evaluation_target)
6134 {
6a831f06
PA
6135 uiout->message (" (%pF evals)",
6136 string_field ("evaluated-by",
6137 bp_condition_evaluator (b)));
b775012e 6138 }
112e8700 6139 uiout->text ("\n");
0101ce28
JJ
6140 }
6141
0d381245 6142 if (!part_of_multiple && b->thread != -1)
c4093a6a 6143 {
4a64f543 6144 /* FIXME should make an annotation for this. */
112e8700
SM
6145 uiout->text ("\tstop only in thread ");
6146 if (uiout->is_mi_like_p ())
381befee 6147 uiout->field_signed ("thread", b->thread);
5d5658a1
PA
6148 else
6149 {
6150 struct thread_info *thr = find_thread_global_id (b->thread);
6151
112e8700 6152 uiout->field_string ("thread", print_thread_id (thr));
5d5658a1 6153 }
112e8700 6154 uiout->text ("\n");
c4093a6a
JM
6155 }
6156
556ec64d
YQ
6157 if (!part_of_multiple)
6158 {
6159 if (b->hit_count)
31f56a27
YQ
6160 {
6161 /* FIXME should make an annotation for this. */
6162 if (is_catchpoint (b))
112e8700 6163 uiout->text ("\tcatchpoint");
31f56a27 6164 else if (is_tracepoint (b))
112e8700 6165 uiout->text ("\ttracepoint");
31f56a27 6166 else
112e8700
SM
6167 uiout->text ("\tbreakpoint");
6168 uiout->text (" already hit ");
381befee 6169 uiout->field_signed ("times", b->hit_count);
31f56a27 6170 if (b->hit_count == 1)
112e8700 6171 uiout->text (" time\n");
31f56a27 6172 else
112e8700 6173 uiout->text (" times\n");
31f56a27 6174 }
556ec64d
YQ
6175 else
6176 {
31f56a27 6177 /* Output the count also if it is zero, but only if this is mi. */
112e8700 6178 if (uiout->is_mi_like_p ())
381befee 6179 uiout->field_signed ("times", b->hit_count);
556ec64d
YQ
6180 }
6181 }
8b93c638 6182
0d381245 6183 if (!part_of_multiple && b->ignore_count)
c4093a6a
JM
6184 {
6185 annotate_field (8);
6a831f06
PA
6186 uiout->message ("\tignore next %pF hits\n",
6187 signed_field ("ignore", b->ignore_count));
c4093a6a 6188 }
059fb39f 6189
816338b5
SS
6190 /* Note that an enable count of 1 corresponds to "enable once"
6191 behavior, which is reported by the combination of enablement and
6192 disposition, so we don't need to mention it here. */
6193 if (!part_of_multiple && b->enable_count > 1)
6194 {
6195 annotate_field (8);
112e8700 6196 uiout->text ("\tdisable after ");
816338b5
SS
6197 /* Tweak the wording to clarify that ignore and enable counts
6198 are distinct, and have additive effect. */
6199 if (b->ignore_count)
112e8700 6200 uiout->text ("additional ");
816338b5 6201 else
112e8700 6202 uiout->text ("next ");
381befee 6203 uiout->field_signed ("enable", b->enable_count);
112e8700 6204 uiout->text (" hits\n");
816338b5
SS
6205 }
6206
f196051f
SS
6207 if (!part_of_multiple && is_tracepoint (b))
6208 {
6209 struct tracepoint *tp = (struct tracepoint *) b;
6210
6211 if (tp->traceframe_usage)
6212 {
112e8700 6213 uiout->text ("\ttrace buffer usage ");
381befee 6214 uiout->field_signed ("traceframe-usage", tp->traceframe_usage);
112e8700 6215 uiout->text (" bytes\n");
f196051f
SS
6216 }
6217 }
d3ce09f5 6218
d1b0a7bf 6219 l = b->commands ? b->commands.get () : NULL;
059fb39f 6220 if (!part_of_multiple && l)
c4093a6a
JM
6221 {
6222 annotate_field (9);
2e783024 6223 ui_out_emit_tuple tuple_emitter (uiout, "script");
8b93c638 6224 print_command_lines (uiout, l, 4);
c4093a6a 6225 }
d24317b4 6226
d9b3f62e 6227 if (is_tracepoint (b))
1042e4c0 6228 {
d9b3f62e
PA
6229 struct tracepoint *t = (struct tracepoint *) b;
6230
6231 if (!part_of_multiple && t->pass_count)
6232 {
6233 annotate_field (10);
112e8700 6234 uiout->text ("\tpass count ");
381befee 6235 uiout->field_signed ("pass", t->pass_count);
112e8700 6236 uiout->text (" \n");
d9b3f62e 6237 }
f2a8bc8a
YQ
6238
6239 /* Don't display it when tracepoint or tracepoint location is
6240 pending. */
6241 if (!header_of_multiple && loc != NULL && !loc->shlib_disabled)
6242 {
6243 annotate_field (11);
6244
112e8700
SM
6245 if (uiout->is_mi_like_p ())
6246 uiout->field_string ("installed",
f2a8bc8a
YQ
6247 loc->inserted ? "y" : "n");
6248 else
6249 {
6250 if (loc->inserted)
112e8700 6251 uiout->text ("\t");
f2a8bc8a 6252 else
112e8700
SM
6253 uiout->text ("\tnot ");
6254 uiout->text ("installed on target\n");
f2a8bc8a
YQ
6255 }
6256 }
1042e4c0
SS
6257 }
6258
112e8700 6259 if (uiout->is_mi_like_p () && !part_of_multiple)
d24317b4 6260 {
3a5c3e22
PA
6261 if (is_watchpoint (b))
6262 {
6263 struct watchpoint *w = (struct watchpoint *) b;
6264
112e8700 6265 uiout->field_string ("original-location", w->exp_string);
3a5c3e22 6266 }
f00aae0f 6267 else if (b->location != NULL
d28cd78a 6268 && event_location_to_string (b->location.get ()) != NULL)
112e8700 6269 uiout->field_string ("original-location",
d28cd78a 6270 event_location_to_string (b->location.get ()));
d24317b4 6271 }
c4093a6a 6272}
c5aa993b 6273
13674803
SM
6274/* See breakpoint.h. */
6275
6276bool fix_multi_location_breakpoint_output_globally = false;
6277
0d381245
VP
6278static void
6279print_one_breakpoint (struct breakpoint *b,
4a64f543 6280 struct bp_location **last_loc,
6c95b8df 6281 int allflag)
0d381245 6282{
79a45e25 6283 struct ui_out *uiout = current_uiout;
13674803
SM
6284 bool use_fixed_output
6285 = (uiout->test_flags (fix_multi_location_breakpoint_output)
6286 || fix_multi_location_breakpoint_output_globally);
8d3788bd 6287
b4be1b06 6288 gdb::optional<ui_out_emit_tuple> bkpt_tuple_emitter (gdb::in_place, uiout, "bkpt");
a38118e5 6289 print_one_breakpoint_location (b, NULL, 0, last_loc, allflag, false);
8d3788bd 6290
b4be1b06
SM
6291 /* The mi2 broken format: the main breakpoint tuple ends here, the locations
6292 are outside. */
6293 if (!use_fixed_output)
6294 bkpt_tuple_emitter.reset ();
0d381245
VP
6295
6296 /* If this breakpoint has custom print function,
6297 it's already printed. Otherwise, print individual
6298 locations, if any. */
a38118e5
PA
6299 if (b->ops == NULL
6300 || b->ops->print_one == NULL
6301 || allflag)
0d381245 6302 {
4a64f543
MS
6303 /* If breakpoint has a single location that is disabled, we
6304 print it as if it had several locations, since otherwise it's
6305 hard to represent "breakpoint enabled, location disabled"
6306 situation.
6307
6308 Note that while hardware watchpoints have several locations
a38118e5
PA
6309 internally, that's not a property exposed to users.
6310
6311 Likewise, while catchpoints may be implemented with
6312 breakpoints (e.g., catch throw), that's not a property
6313 exposed to users. We do however display the internal
6314 breakpoint locations with "maint info breakpoints". */
6315 if (!is_hardware_watchpoint (b)
f06f1252
TT
6316 && (!is_catchpoint (b) || is_exception_catchpoint (b)
6317 || is_ada_exception_catchpoint (b))
a38118e5
PA
6318 && (allflag
6319 || (b->loc && (b->loc->next || !b->loc->enabled))))
0d381245 6320 {
b4be1b06
SM
6321 gdb::optional<ui_out_emit_list> locations_list;
6322
6323 /* For MI version <= 2, keep the behavior where GDB outputs an invalid
6324 MI record. For later versions, place breakpoint locations in a
6325 list. */
6326 if (uiout->is_mi_like_p () && use_fixed_output)
6327 locations_list.emplace (uiout, "locations");
8d3788bd 6328
b4be1b06
SM
6329 int n = 1;
6330 for (bp_location *loc = b->loc; loc != NULL; loc = loc->next, ++n)
8d3788bd 6331 {
b4be1b06 6332 ui_out_emit_tuple loc_tuple_emitter (uiout, NULL);
a38118e5
PA
6333 print_one_breakpoint_location (b, loc, n, last_loc,
6334 allflag, allflag);
8d3788bd 6335 }
0d381245
VP
6336 }
6337 }
6338}
6339
a6d9a66e
UW
6340static int
6341breakpoint_address_bits (struct breakpoint *b)
6342{
6343 int print_address_bits = 0;
6344 struct bp_location *loc;
6345
c6d81124
PA
6346 /* Software watchpoints that aren't watching memory don't have an
6347 address to print. */
6348 if (is_no_memory_software_watchpoint (b))
6349 return 0;
6350
a6d9a66e
UW
6351 for (loc = b->loc; loc; loc = loc->next)
6352 {
c7437ca6
PA
6353 int addr_bit;
6354
c7437ca6 6355 addr_bit = gdbarch_addr_bit (loc->gdbarch);
a6d9a66e
UW
6356 if (addr_bit > print_address_bits)
6357 print_address_bits = addr_bit;
6358 }
6359
6360 return print_address_bits;
6361}
0d381245 6362
65630365 6363/* See breakpoint.h. */
c5aa993b 6364
65630365
PA
6365void
6366print_breakpoint (breakpoint *b)
c4093a6a 6367{
a6d9a66e 6368 struct bp_location *dummy_loc = NULL;
65630365 6369 print_one_breakpoint (b, &dummy_loc, 0);
c4093a6a 6370}
c5aa993b 6371
09d682a4
TT
6372/* Return true if this breakpoint was set by the user, false if it is
6373 internal or momentary. */
6374
6375int
6376user_breakpoint_p (struct breakpoint *b)
6377{
46c6471b 6378 return b->number > 0;
09d682a4
TT
6379}
6380
93daf339
TT
6381/* See breakpoint.h. */
6382
6383int
6384pending_breakpoint_p (struct breakpoint *b)
6385{
6386 return b->loc == NULL;
6387}
6388
5c458ae8
SM
6389/* Print information on breakpoints (including watchpoints and tracepoints).
6390
6391 If non-NULL, BP_NUM_LIST is a list of numbers and number ranges as
6392 understood by number_or_range_parser. Only breakpoints included in this
6393 list are then printed.
6394
6395 If SHOW_INTERNAL is true, print internal breakpoints.
6396
6397 If FILTER is non-NULL, call it on each breakpoint and only include the
6398 ones for which it returns true.
6399
6400 Return the total number of breakpoints listed. */
c906108c 6401
d77f58be 6402static int
5c458ae8 6403breakpoint_1 (const char *bp_num_list, bool show_internal,
f2478a7e 6404 bool (*filter) (const struct breakpoint *))
c4093a6a 6405{
52f0bd74 6406 struct breakpoint *b;
a6d9a66e 6407 struct bp_location *last_loc = NULL;
7f3b0473 6408 int nr_printable_breakpoints;
79a45b7d 6409 struct value_print_options opts;
a6d9a66e 6410 int print_address_bits = 0;
269b11a2 6411 int print_type_col_width = 14;
79a45e25 6412 struct ui_out *uiout = current_uiout;
269b11a2 6413
79a45b7d
TT
6414 get_user_print_options (&opts);
6415
4a64f543
MS
6416 /* Compute the number of rows in the table, as well as the size
6417 required for address fields. */
7f3b0473
AC
6418 nr_printable_breakpoints = 0;
6419 ALL_BREAKPOINTS (b)
e5a67952
MS
6420 {
6421 /* If we have a filter, only list the breakpoints it accepts. */
6422 if (filter && !filter (b))
6423 continue;
6424
5c458ae8 6425 /* If we have a BP_NUM_LIST string, it is a list of breakpoints to
e5a67952 6426 accept. Skip the others. */
5c458ae8 6427 if (bp_num_list != NULL && *bp_num_list != '\0')
e5a67952 6428 {
5c458ae8 6429 if (show_internal && parse_and_eval_long (bp_num_list) != b->number)
e5a67952 6430 continue;
5c458ae8 6431 if (!show_internal && !number_is_in_list (bp_num_list, b->number))
e5a67952
MS
6432 continue;
6433 }
269b11a2 6434
5c458ae8 6435 if (show_internal || user_breakpoint_p (b))
e5a67952
MS
6436 {
6437 int addr_bit, type_len;
a6d9a66e 6438
e5a67952
MS
6439 addr_bit = breakpoint_address_bits (b);
6440 if (addr_bit > print_address_bits)
6441 print_address_bits = addr_bit;
269b11a2 6442
e5a67952
MS
6443 type_len = strlen (bptype_string (b->type));
6444 if (type_len > print_type_col_width)
6445 print_type_col_width = type_len;
6446
6447 nr_printable_breakpoints++;
6448 }
6449 }
7f3b0473 6450
4a2b031d
TT
6451 {
6452 ui_out_emit_table table_emitter (uiout,
6453 opts.addressprint ? 6 : 5,
6454 nr_printable_breakpoints,
6455 "BreakpointTable");
6456
6457 if (nr_printable_breakpoints > 0)
6458 annotate_breakpoints_headers ();
6459 if (nr_printable_breakpoints > 0)
6460 annotate_field (0);
6461 uiout->table_header (7, ui_left, "number", "Num"); /* 1 */
6462 if (nr_printable_breakpoints > 0)
6463 annotate_field (1);
6464 uiout->table_header (print_type_col_width, ui_left, "type", "Type"); /* 2 */
6465 if (nr_printable_breakpoints > 0)
6466 annotate_field (2);
6467 uiout->table_header (4, ui_left, "disp", "Disp"); /* 3 */
6468 if (nr_printable_breakpoints > 0)
6469 annotate_field (3);
6470 uiout->table_header (3, ui_left, "enabled", "Enb"); /* 4 */
6471 if (opts.addressprint)
6472 {
6473 if (nr_printable_breakpoints > 0)
6474 annotate_field (4);
6475 if (print_address_bits <= 32)
6476 uiout->table_header (10, ui_left, "addr", "Address"); /* 5 */
6477 else
6478 uiout->table_header (18, ui_left, "addr", "Address"); /* 5 */
6479 }
6480 if (nr_printable_breakpoints > 0)
6481 annotate_field (5);
6482 uiout->table_header (40, ui_noalign, "what", "What"); /* 6 */
6483 uiout->table_body ();
6484 if (nr_printable_breakpoints > 0)
6485 annotate_breakpoints_table ();
6486
6487 ALL_BREAKPOINTS (b)
6488 {
6489 QUIT;
6490 /* If we have a filter, only list the breakpoints it accepts. */
6491 if (filter && !filter (b))
6492 continue;
e5a67952 6493
5c458ae8 6494 /* If we have a BP_NUM_LIST string, it is a list of breakpoints to
4a2b031d 6495 accept. Skip the others. */
e5a67952 6496
5c458ae8 6497 if (bp_num_list != NULL && *bp_num_list != '\0')
4a2b031d 6498 {
5c458ae8 6499 if (show_internal) /* maintenance info breakpoint */
4a2b031d 6500 {
5c458ae8 6501 if (parse_and_eval_long (bp_num_list) != b->number)
4a2b031d
TT
6502 continue;
6503 }
6504 else /* all others */
6505 {
5c458ae8 6506 if (!number_is_in_list (bp_num_list, b->number))
4a2b031d
TT
6507 continue;
6508 }
6509 }
6510 /* We only print out user settable breakpoints unless the
5c458ae8
SM
6511 show_internal is set. */
6512 if (show_internal || user_breakpoint_p (b))
6513 print_one_breakpoint (b, &last_loc, show_internal);
4a2b031d
TT
6514 }
6515 }
698384cd 6516
7f3b0473 6517 if (nr_printable_breakpoints == 0)
c906108c 6518 {
4a64f543
MS
6519 /* If there's a filter, let the caller decide how to report
6520 empty list. */
d77f58be
SS
6521 if (!filter)
6522 {
5c458ae8 6523 if (bp_num_list == NULL || *bp_num_list == '\0')
112e8700 6524 uiout->message ("No breakpoints or watchpoints.\n");
d77f58be 6525 else
112e8700 6526 uiout->message ("No breakpoint or watchpoint matching '%s'.\n",
5c458ae8 6527 bp_num_list);
d77f58be 6528 }
c906108c
SS
6529 }
6530 else
c4093a6a 6531 {
a6d9a66e
UW
6532 if (last_loc && !server_command)
6533 set_next_address (last_loc->gdbarch, last_loc->address);
c4093a6a 6534 }
c906108c 6535
4a64f543 6536 /* FIXME? Should this be moved up so that it is only called when
c4093a6a 6537 there have been breakpoints? */
c906108c 6538 annotate_breakpoints_table_end ();
d77f58be
SS
6539
6540 return nr_printable_breakpoints;
c906108c
SS
6541}
6542
ad443146
SS
6543/* Display the value of default-collect in a way that is generally
6544 compatible with the breakpoint list. */
6545
6546static void
6547default_collect_info (void)
6548{
79a45e25
PA
6549 struct ui_out *uiout = current_uiout;
6550
ad443146
SS
6551 /* If it has no value (which is frequently the case), say nothing; a
6552 message like "No default-collect." gets in user's face when it's
6553 not wanted. */
6554 if (!*default_collect)
6555 return;
6556
6557 /* The following phrase lines up nicely with per-tracepoint collect
6558 actions. */
112e8700
SM
6559 uiout->text ("default collect ");
6560 uiout->field_string ("default-collect", default_collect);
6561 uiout->text (" \n");
ad443146
SS
6562}
6563
c906108c 6564static void
0b39b52e 6565info_breakpoints_command (const char *args, int from_tty)
c906108c 6566{
5c458ae8 6567 breakpoint_1 (args, false, NULL);
ad443146
SS
6568
6569 default_collect_info ();
d77f58be
SS
6570}
6571
6572static void
1d12d88f 6573info_watchpoints_command (const char *args, int from_tty)
d77f58be 6574{
5c458ae8 6575 int num_printed = breakpoint_1 (args, false, is_watchpoint);
79a45e25 6576 struct ui_out *uiout = current_uiout;
d77f58be
SS
6577
6578 if (num_printed == 0)
6579 {
e5a67952 6580 if (args == NULL || *args == '\0')
112e8700 6581 uiout->message ("No watchpoints.\n");
d77f58be 6582 else
112e8700 6583 uiout->message ("No watchpoint matching '%s'.\n", args);
d77f58be 6584 }
c906108c
SS
6585}
6586
7a292a7a 6587static void
4495129a 6588maintenance_info_breakpoints (const char *args, int from_tty)
c906108c 6589{
5c458ae8 6590 breakpoint_1 (args, true, NULL);
ad443146
SS
6591
6592 default_collect_info ();
c906108c
SS
6593}
6594
0d381245 6595static int
714835d5 6596breakpoint_has_pc (struct breakpoint *b,
6c95b8df 6597 struct program_space *pspace,
714835d5 6598 CORE_ADDR pc, struct obj_section *section)
0d381245
VP
6599{
6600 struct bp_location *bl = b->loc;
cc59ec59 6601
0d381245
VP
6602 for (; bl; bl = bl->next)
6603 {
6c95b8df
PA
6604 if (bl->pspace == pspace
6605 && bl->address == pc
0d381245
VP
6606 && (!overlay_debugging || bl->section == section))
6607 return 1;
6608 }
6609 return 0;
6610}
6611
672f9b60 6612/* Print a message describing any user-breakpoints set at PC. This
6c95b8df
PA
6613 concerns with logical breakpoints, so we match program spaces, not
6614 address spaces. */
c906108c
SS
6615
6616static void
6c95b8df
PA
6617describe_other_breakpoints (struct gdbarch *gdbarch,
6618 struct program_space *pspace, CORE_ADDR pc,
5af949e3 6619 struct obj_section *section, int thread)
c906108c 6620{
52f0bd74
AC
6621 int others = 0;
6622 struct breakpoint *b;
c906108c
SS
6623
6624 ALL_BREAKPOINTS (b)
672f9b60
KP
6625 others += (user_breakpoint_p (b)
6626 && breakpoint_has_pc (b, pspace, pc, section));
c906108c
SS
6627 if (others > 0)
6628 {
a3f17187
AC
6629 if (others == 1)
6630 printf_filtered (_("Note: breakpoint "));
6631 else /* if (others == ???) */
6632 printf_filtered (_("Note: breakpoints "));
c906108c 6633 ALL_BREAKPOINTS (b)
672f9b60 6634 if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
0d381245
VP
6635 {
6636 others--;
6637 printf_filtered ("%d", b->number);
6638 if (b->thread == -1 && thread != -1)
6639 printf_filtered (" (all threads)");
6640 else if (b->thread != -1)
6641 printf_filtered (" (thread %d)", b->thread);
6642 printf_filtered ("%s%s ",
059fb39f 6643 ((b->enable_state == bp_disabled
f8eba3c6 6644 || b->enable_state == bp_call_disabled)
0d381245 6645 ? " (disabled)"
0d381245
VP
6646 : ""),
6647 (others > 1) ? ","
6648 : ((others == 1) ? " and" : ""));
6649 }
6a831f06
PA
6650 current_uiout->message (_("also set at pc %ps.\n"),
6651 styled_string (address_style.style (),
6652 paddress (gdbarch, pc)));
c906108c
SS
6653 }
6654}
6655\f
c906108c 6656
cb1e4e32
PA
6657/* Return true iff it is meaningful to use the address member of LOC.
6658 For some breakpoint types, the locations' address members are
6659 irrelevant and it makes no sense to attempt to compare them to
6660 other addresses (or use them for any other purpose either).
2d134ed3 6661
cb1e4e32
PA
6662 More specifically, software watchpoints and catchpoints that are
6663 not backed by breakpoints always have a zero valued location
6664 address and we don't want to mark breakpoints of any of these types
6665 to be a duplicate of an actual breakpoint location at address
6666 zero. */
e4f237da 6667
cb1e4e32
PA
6668static bool
6669bl_address_is_meaningful (bp_location *loc)
e4f237da 6670{
cb1e4e32 6671 return loc->loc_type != bp_loc_other;
2d134ed3
PA
6672}
6673
6674/* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
6675 true if LOC1 and LOC2 represent the same watchpoint location. */
6676
6677static int
4a64f543
MS
6678watchpoint_locations_match (struct bp_location *loc1,
6679 struct bp_location *loc2)
2d134ed3 6680{
3a5c3e22
PA
6681 struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
6682 struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
6683
6684 /* Both of them must exist. */
6685 gdb_assert (w1 != NULL);
6686 gdb_assert (w2 != NULL);
2bdf28a0 6687
4a64f543
MS
6688 /* If the target can evaluate the condition expression in hardware,
6689 then we we need to insert both watchpoints even if they are at
6690 the same place. Otherwise the watchpoint will only trigger when
6691 the condition of whichever watchpoint was inserted evaluates to
6692 true, not giving a chance for GDB to check the condition of the
6693 other watchpoint. */
3a5c3e22 6694 if ((w1->cond_exp
4a64f543
MS
6695 && target_can_accel_watchpoint_condition (loc1->address,
6696 loc1->length,
0cf6dd15 6697 loc1->watchpoint_type,
4d01a485 6698 w1->cond_exp.get ()))
3a5c3e22 6699 || (w2->cond_exp
4a64f543
MS
6700 && target_can_accel_watchpoint_condition (loc2->address,
6701 loc2->length,
0cf6dd15 6702 loc2->watchpoint_type,
4d01a485 6703 w2->cond_exp.get ())))
0cf6dd15
TJB
6704 return 0;
6705
85d721b8
PA
6706 /* Note that this checks the owner's type, not the location's. In
6707 case the target does not support read watchpoints, but does
6708 support access watchpoints, we'll have bp_read_watchpoint
6709 watchpoints with hw_access locations. Those should be considered
6710 duplicates of hw_read locations. The hw_read locations will
6711 become hw_access locations later. */
2d134ed3
PA
6712 return (loc1->owner->type == loc2->owner->type
6713 && loc1->pspace->aspace == loc2->pspace->aspace
6714 && loc1->address == loc2->address
6715 && loc1->length == loc2->length);
e4f237da
KB
6716}
6717
31e77af2 6718/* See breakpoint.h. */
6c95b8df 6719
31e77af2 6720int
accd0bcd
YQ
6721breakpoint_address_match (const address_space *aspace1, CORE_ADDR addr1,
6722 const address_space *aspace2, CORE_ADDR addr2)
6c95b8df 6723{
f5656ead 6724 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
6c95b8df
PA
6725 || aspace1 == aspace2)
6726 && addr1 == addr2);
6727}
6728
f1310107
TJB
6729/* Returns true if {ASPACE2,ADDR2} falls within the range determined by
6730 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
6731 matches ASPACE2. On targets that have global breakpoints, the address
6732 space doesn't really matter. */
6733
6734static int
accd0bcd
YQ
6735breakpoint_address_match_range (const address_space *aspace1,
6736 CORE_ADDR addr1,
6737 int len1, const address_space *aspace2,
f1310107
TJB
6738 CORE_ADDR addr2)
6739{
f5656ead 6740 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
f1310107
TJB
6741 || aspace1 == aspace2)
6742 && addr2 >= addr1 && addr2 < addr1 + len1);
6743}
6744
6745/* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
6746 a ranged breakpoint. In most targets, a match happens only if ASPACE
6747 matches the breakpoint's address space. On targets that have global
6748 breakpoints, the address space doesn't really matter. */
6749
6750static int
6751breakpoint_location_address_match (struct bp_location *bl,
accd0bcd 6752 const address_space *aspace,
f1310107
TJB
6753 CORE_ADDR addr)
6754{
6755 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
6756 aspace, addr)
6757 || (bl->length
6758 && breakpoint_address_match_range (bl->pspace->aspace,
6759 bl->address, bl->length,
6760 aspace, addr)));
6761}
6762
d35ae833
PA
6763/* Returns true if the [ADDR,ADDR+LEN) range in ASPACE overlaps
6764 breakpoint BL. BL may be a ranged breakpoint. In most targets, a
6765 match happens only if ASPACE matches the breakpoint's address
6766 space. On targets that have global breakpoints, the address space
6767 doesn't really matter. */
6768
6769static int
6770breakpoint_location_address_range_overlap (struct bp_location *bl,
accd0bcd 6771 const address_space *aspace,
d35ae833
PA
6772 CORE_ADDR addr, int len)
6773{
6774 if (gdbarch_has_global_breakpoints (target_gdbarch ())
6775 || bl->pspace->aspace == aspace)
6776 {
6777 int bl_len = bl->length != 0 ? bl->length : 1;
6778
6779 if (mem_ranges_overlap (addr, len, bl->address, bl_len))
6780 return 1;
6781 }
6782 return 0;
6783}
6784
1e4d1764
YQ
6785/* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
6786 Then, if LOC1 and LOC2 represent the same tracepoint location, returns
6787 true, otherwise returns false. */
6788
6789static int
6790tracepoint_locations_match (struct bp_location *loc1,
6791 struct bp_location *loc2)
6792{
6793 if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
6794 /* Since tracepoint locations are never duplicated with others', tracepoint
6795 locations at the same address of different tracepoints are regarded as
6796 different locations. */
6797 return (loc1->address == loc2->address && loc1->owner == loc2->owner);
6798 else
6799 return 0;
6800}
6801
2d134ed3 6802/* Assuming LOC1 and LOC2's types' have meaningful target addresses
cb1e4e32 6803 (bl_address_is_meaningful), returns true if LOC1 and LOC2 represent
7f32a4d5
PA
6804 the same location. If SW_HW_BPS_MATCH is true, then software
6805 breakpoint locations and hardware breakpoint locations match,
6806 otherwise they don't. */
2d134ed3
PA
6807
6808static int
7f32a4d5
PA
6809breakpoint_locations_match (struct bp_location *loc1,
6810 struct bp_location *loc2,
6811 bool sw_hw_bps_match)
2d134ed3 6812{
2bdf28a0
JK
6813 int hw_point1, hw_point2;
6814
6815 /* Both of them must not be in moribund_locations. */
6816 gdb_assert (loc1->owner != NULL);
6817 gdb_assert (loc2->owner != NULL);
6818
6819 hw_point1 = is_hardware_watchpoint (loc1->owner);
6820 hw_point2 = is_hardware_watchpoint (loc2->owner);
2d134ed3
PA
6821
6822 if (hw_point1 != hw_point2)
6823 return 0;
6824 else if (hw_point1)
6825 return watchpoint_locations_match (loc1, loc2);
1e4d1764
YQ
6826 else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
6827 return tracepoint_locations_match (loc1, loc2);
2d134ed3 6828 else
7f32a4d5
PA
6829 /* We compare bp_location.length in order to cover ranged
6830 breakpoints. Keep this in sync with
6831 bp_location_is_less_than. */
f1310107
TJB
6832 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
6833 loc2->pspace->aspace, loc2->address)
7f32a4d5 6834 && (loc1->loc_type == loc2->loc_type || sw_hw_bps_match)
f1310107 6835 && loc1->length == loc2->length);
2d134ed3
PA
6836}
6837
76897487
KB
6838static void
6839breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
6840 int bnum, int have_bnum)
6841{
f63fbe86
MS
6842 /* The longest string possibly returned by hex_string_custom
6843 is 50 chars. These must be at least that big for safety. */
6844 char astr1[64];
6845 char astr2[64];
76897487 6846
bb599908
PH
6847 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
6848 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
76897487 6849 if (have_bnum)
8a3fe4f8 6850 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
76897487
KB
6851 bnum, astr1, astr2);
6852 else
8a3fe4f8 6853 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
76897487
KB
6854}
6855
4a64f543
MS
6856/* Adjust a breakpoint's address to account for architectural
6857 constraints on breakpoint placement. Return the adjusted address.
6858 Note: Very few targets require this kind of adjustment. For most
6859 targets, this function is simply the identity function. */
76897487
KB
6860
6861static CORE_ADDR
a6d9a66e
UW
6862adjust_breakpoint_address (struct gdbarch *gdbarch,
6863 CORE_ADDR bpaddr, enum bptype bptype)
76897487 6864{
a0de8c21
YQ
6865 if (bptype == bp_watchpoint
6866 || bptype == bp_hardware_watchpoint
6867 || bptype == bp_read_watchpoint
6868 || bptype == bp_access_watchpoint
6869 || bptype == bp_catchpoint)
88f7da05
KB
6870 {
6871 /* Watchpoints and the various bp_catch_* eventpoints should not
6872 have their addresses modified. */
6873 return bpaddr;
6874 }
7c16b83e
PA
6875 else if (bptype == bp_single_step)
6876 {
6877 /* Single-step breakpoints should not have their addresses
6878 modified. If there's any architectural constrain that
6879 applies to this address, then it should have already been
6880 taken into account when the breakpoint was created in the
6881 first place. If we didn't do this, stepping through e.g.,
6882 Thumb-2 IT blocks would break. */
6883 return bpaddr;
6884 }
76897487
KB
6885 else
6886 {
a0de8c21
YQ
6887 CORE_ADDR adjusted_bpaddr = bpaddr;
6888
6889 if (gdbarch_adjust_breakpoint_address_p (gdbarch))
6890 {
6891 /* Some targets have architectural constraints on the placement
6892 of breakpoint instructions. Obtain the adjusted address. */
6893 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
6894 }
76897487 6895
a0de8c21 6896 adjusted_bpaddr = address_significant (gdbarch, adjusted_bpaddr);
76897487
KB
6897
6898 /* An adjusted breakpoint address can significantly alter
6899 a user's expectations. Print a warning if an adjustment
6900 is required. */
6901 if (adjusted_bpaddr != bpaddr)
6902 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
6903
6904 return adjusted_bpaddr;
6905 }
6906}
6907
cb1e4e32
PA
6908static bp_loc_type
6909bp_location_from_bp_type (bptype type)
7cc221ef 6910{
cb1e4e32 6911 switch (type)
e049a4b5
DJ
6912 {
6913 case bp_breakpoint:
7c16b83e 6914 case bp_single_step:
e049a4b5
DJ
6915 case bp_until:
6916 case bp_finish:
6917 case bp_longjmp:
6918 case bp_longjmp_resume:
e2e4d78b 6919 case bp_longjmp_call_dummy:
186c406b
TT
6920 case bp_exception:
6921 case bp_exception_resume:
e049a4b5 6922 case bp_step_resume:
2c03e5be 6923 case bp_hp_step_resume:
e049a4b5
DJ
6924 case bp_watchpoint_scope:
6925 case bp_call_dummy:
aa7d318d 6926 case bp_std_terminate:
e049a4b5
DJ
6927 case bp_shlib_event:
6928 case bp_thread_event:
6929 case bp_overlay_event:
4efc6507 6930 case bp_jit_event:
0fd8e87f 6931 case bp_longjmp_master:
aa7d318d 6932 case bp_std_terminate_master:
186c406b 6933 case bp_exception_master:
0e30163f
JK
6934 case bp_gnu_ifunc_resolver:
6935 case bp_gnu_ifunc_resolver_return:
e7e0cddf 6936 case bp_dprintf:
cb1e4e32 6937 return bp_loc_software_breakpoint;
e049a4b5 6938 case bp_hardware_breakpoint:
cb1e4e32 6939 return bp_loc_hardware_breakpoint;
e049a4b5
DJ
6940 case bp_hardware_watchpoint:
6941 case bp_read_watchpoint:
6942 case bp_access_watchpoint:
cb1e4e32 6943 return bp_loc_hardware_watchpoint;
e049a4b5 6944 case bp_watchpoint:
ce78b96d 6945 case bp_catchpoint:
15c3d785
PA
6946 case bp_tracepoint:
6947 case bp_fast_tracepoint:
0fb4aa4b 6948 case bp_static_tracepoint:
cb1e4e32 6949 return bp_loc_other;
e049a4b5 6950 default:
e2e0b3e5 6951 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
e049a4b5 6952 }
cb1e4e32
PA
6953}
6954
6955bp_location::bp_location (breakpoint *owner, bp_loc_type type)
6956{
6957 this->owner = owner;
6958 this->cond_bytecode = NULL;
6959 this->shlib_disabled = 0;
6960 this->enabled = 1;
6961
6962 this->loc_type = type;
e049a4b5 6963
cb1e4e32
PA
6964 if (this->loc_type == bp_loc_software_breakpoint
6965 || this->loc_type == bp_loc_hardware_breakpoint)
6966 mark_breakpoint_location_modified (this);
6967
6968 this->refc = 1;
6969}
6970
6971bp_location::bp_location (breakpoint *owner)
6972 : bp_location::bp_location (owner,
6973 bp_location_from_bp_type (owner->type))
6974{
28010a5d
PA
6975}
6976
6977/* Allocate a struct bp_location. */
6978
6979static struct bp_location *
6980allocate_bp_location (struct breakpoint *bpt)
6981{
348d480f
PA
6982 return bpt->ops->allocate_location (bpt);
6983}
7cc221ef 6984
f431efe5
PA
6985static void
6986free_bp_location (struct bp_location *loc)
fe3f5fa8 6987{
4d01a485 6988 delete loc;
fe3f5fa8
VP
6989}
6990
f431efe5
PA
6991/* Increment reference count. */
6992
6993static void
6994incref_bp_location (struct bp_location *bl)
6995{
6996 ++bl->refc;
6997}
6998
6999/* Decrement reference count. If the reference count reaches 0,
7000 destroy the bp_location. Sets *BLP to NULL. */
7001
7002static void
7003decref_bp_location (struct bp_location **blp)
7004{
0807b50c
PA
7005 gdb_assert ((*blp)->refc > 0);
7006
f431efe5
PA
7007 if (--(*blp)->refc == 0)
7008 free_bp_location (*blp);
7009 *blp = NULL;
7010}
7011
346774a9 7012/* Add breakpoint B at the end of the global breakpoint chain. */
c906108c 7013
b270e6f9
TT
7014static breakpoint *
7015add_to_breakpoint_chain (std::unique_ptr<breakpoint> &&b)
c906108c 7016{
346774a9 7017 struct breakpoint *b1;
b270e6f9 7018 struct breakpoint *result = b.get ();
c906108c 7019
346774a9
PA
7020 /* Add this breakpoint to the end of the chain so that a list of
7021 breakpoints will come out in order of increasing numbers. */
7022
7023 b1 = breakpoint_chain;
7024 if (b1 == 0)
b270e6f9 7025 breakpoint_chain = b.release ();
346774a9
PA
7026 else
7027 {
7028 while (b1->next)
7029 b1 = b1->next;
b270e6f9 7030 b1->next = b.release ();
346774a9 7031 }
b270e6f9
TT
7032
7033 return result;
346774a9
PA
7034}
7035
7036/* Initializes breakpoint B with type BPTYPE and no locations yet. */
7037
7038static void
7039init_raw_breakpoint_without_location (struct breakpoint *b,
7040 struct gdbarch *gdbarch,
28010a5d 7041 enum bptype bptype,
c0a91b2b 7042 const struct breakpoint_ops *ops)
346774a9 7043{
348d480f
PA
7044 gdb_assert (ops != NULL);
7045
28010a5d 7046 b->ops = ops;
4d28f7a8 7047 b->type = bptype;
a6d9a66e 7048 b->gdbarch = gdbarch;
c906108c
SS
7049 b->language = current_language->la_language;
7050 b->input_radix = input_radix;
d0fb5eae 7051 b->related_breakpoint = b;
346774a9
PA
7052}
7053
7054/* Helper to set_raw_breakpoint below. Creates a breakpoint
7055 that has type BPTYPE and has no locations as yet. */
346774a9
PA
7056
7057static struct breakpoint *
7058set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
348d480f 7059 enum bptype bptype,
c0a91b2b 7060 const struct breakpoint_ops *ops)
346774a9 7061{
3b0871f4 7062 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (bptype);
346774a9 7063
3b0871f4 7064 init_raw_breakpoint_without_location (b.get (), gdbarch, bptype, ops);
b270e6f9 7065 return add_to_breakpoint_chain (std::move (b));
0d381245
VP
7066}
7067
0ba852ab 7068/* Initialize loc->function_name. */
0e30163f 7069
0d381245 7070static void
0ba852ab 7071set_breakpoint_location_function (struct bp_location *loc)
0d381245 7072{
2bdf28a0
JK
7073 gdb_assert (loc->owner != NULL);
7074
0d381245 7075 if (loc->owner->type == bp_breakpoint
1042e4c0 7076 || loc->owner->type == bp_hardware_breakpoint
d77f58be 7077 || is_tracepoint (loc->owner))
0d381245 7078 {
2c02bd72 7079 const char *function_name;
0e30163f 7080
3467ec66 7081 if (loc->msymbol != NULL
f50776aa 7082 && (MSYMBOL_TYPE (loc->msymbol) == mst_text_gnu_ifunc
0ba852ab 7083 || MSYMBOL_TYPE (loc->msymbol) == mst_data_gnu_ifunc))
0e30163f
JK
7084 {
7085 struct breakpoint *b = loc->owner;
7086
c9d95fa3 7087 function_name = loc->msymbol->linkage_name ();
3467ec66
PA
7088
7089 if (b->type == bp_breakpoint && b->loc == loc
7090 && loc->next == NULL && b->related_breakpoint == b)
0e30163f
JK
7091 {
7092 /* Create only the whole new breakpoint of this type but do not
7093 mess more complicated breakpoints with multiple locations. */
7094 b->type = bp_gnu_ifunc_resolver;
6a3a010b
MR
7095 /* Remember the resolver's address for use by the return
7096 breakpoint. */
3467ec66 7097 loc->related_address = loc->address;
0e30163f
JK
7098 }
7099 }
3467ec66
PA
7100 else
7101 find_pc_partial_function (loc->address, &function_name, NULL, NULL);
0e30163f 7102
2c02bd72
DE
7103 if (function_name)
7104 loc->function_name = xstrdup (function_name);
0d381245
VP
7105 }
7106}
7107
a6d9a66e 7108/* Attempt to determine architecture of location identified by SAL. */
1bfeeb0f 7109struct gdbarch *
a6d9a66e
UW
7110get_sal_arch (struct symtab_and_line sal)
7111{
7112 if (sal.section)
08feed99 7113 return sal.section->objfile->arch ();
a6d9a66e 7114 if (sal.symtab)
08feed99 7115 return SYMTAB_OBJFILE (sal.symtab)->arch ();
a6d9a66e
UW
7116
7117 return NULL;
7118}
7119
346774a9
PA
7120/* Low level routine for partially initializing a breakpoint of type
7121 BPTYPE. The newly created breakpoint's address, section, source
c56053d2 7122 file name, and line number are provided by SAL.
0d381245
VP
7123
7124 It is expected that the caller will complete the initialization of
7125 the newly created breakpoint struct as well as output any status
c56053d2 7126 information regarding the creation of a new breakpoint. */
0d381245 7127
346774a9
PA
7128static void
7129init_raw_breakpoint (struct breakpoint *b, struct gdbarch *gdbarch,
28010a5d 7130 struct symtab_and_line sal, enum bptype bptype,
c0a91b2b 7131 const struct breakpoint_ops *ops)
0d381245 7132{
28010a5d 7133 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
346774a9 7134
3742cc8b 7135 add_location_to_breakpoint (b, &sal);
0d381245 7136
6c95b8df
PA
7137 if (bptype != bp_catchpoint)
7138 gdb_assert (sal.pspace != NULL);
7139
f8eba3c6
TT
7140 /* Store the program space that was used to set the breakpoint,
7141 except for ordinary breakpoints, which are independent of the
7142 program space. */
7143 if (bptype != bp_breakpoint && bptype != bp_hardware_breakpoint)
7144 b->pspace = sal.pspace;
346774a9 7145}
c906108c 7146
346774a9
PA
7147/* set_raw_breakpoint is a low level routine for allocating and
7148 partially initializing a breakpoint of type BPTYPE. The newly
7149 created breakpoint's address, section, source file name, and line
7150 number are provided by SAL. The newly created and partially
7151 initialized breakpoint is added to the breakpoint chain and
7152 is also returned as the value of this function.
7153
7154 It is expected that the caller will complete the initialization of
7155 the newly created breakpoint struct as well as output any status
7156 information regarding the creation of a new breakpoint. In
7157 particular, set_raw_breakpoint does NOT set the breakpoint
7158 number! Care should be taken to not allow an error to occur
7159 prior to completing the initialization of the breakpoint. If this
7160 should happen, a bogus breakpoint will be left on the chain. */
7161
7162struct breakpoint *
7163set_raw_breakpoint (struct gdbarch *gdbarch,
348d480f 7164 struct symtab_and_line sal, enum bptype bptype,
c0a91b2b 7165 const struct breakpoint_ops *ops)
346774a9 7166{
3b0871f4 7167 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (bptype);
346774a9 7168
3b0871f4 7169 init_raw_breakpoint (b.get (), gdbarch, sal, bptype, ops);
b270e6f9 7170 return add_to_breakpoint_chain (std::move (b));
c906108c
SS
7171}
7172
53a5351d 7173/* Call this routine when stepping and nexting to enable a breakpoint
186c406b
TT
7174 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
7175 initiated the operation. */
c906108c
SS
7176
7177void
186c406b 7178set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
c906108c 7179{
35df4500 7180 struct breakpoint *b, *b_tmp;
5d5658a1 7181 int thread = tp->global_num;
0fd8e87f
UW
7182
7183 /* To avoid having to rescan all objfile symbols at every step,
7184 we maintain a list of continually-inserted but always disabled
7185 longjmp "master" breakpoints. Here, we simply create momentary
7186 clones of those and enable them for the requested thread. */
35df4500 7187 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df 7188 if (b->pspace == current_program_space
186c406b
TT
7189 && (b->type == bp_longjmp_master
7190 || b->type == bp_exception_master))
0fd8e87f 7191 {
06edf0c0
PA
7192 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
7193 struct breakpoint *clone;
cc59ec59 7194
e2e4d78b
JK
7195 /* longjmp_breakpoint_ops ensures INITIATING_FRAME is cleared again
7196 after their removal. */
06edf0c0 7197 clone = momentary_breakpoint_from_master (b, type,
c1fc2657 7198 &momentary_breakpoint_ops, 1);
0fd8e87f
UW
7199 clone->thread = thread;
7200 }
186c406b
TT
7201
7202 tp->initiating_frame = frame;
c906108c
SS
7203}
7204
611c83ae 7205/* Delete all longjmp breakpoints from THREAD. */
c906108c 7206void
611c83ae 7207delete_longjmp_breakpoint (int thread)
c906108c 7208{
35df4500 7209 struct breakpoint *b, *b_tmp;
c906108c 7210
35df4500 7211 ALL_BREAKPOINTS_SAFE (b, b_tmp)
186c406b 7212 if (b->type == bp_longjmp || b->type == bp_exception)
611c83ae
PA
7213 {
7214 if (b->thread == thread)
7215 delete_breakpoint (b);
7216 }
c906108c
SS
7217}
7218
f59f708a
PA
7219void
7220delete_longjmp_breakpoint_at_next_stop (int thread)
7221{
7222 struct breakpoint *b, *b_tmp;
7223
7224 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7225 if (b->type == bp_longjmp || b->type == bp_exception)
7226 {
7227 if (b->thread == thread)
7228 b->disposition = disp_del_at_next_stop;
7229 }
7230}
7231
e2e4d78b
JK
7232/* Place breakpoints of type bp_longjmp_call_dummy to catch longjmp for
7233 INFERIOR_PTID thread. Chain them all by RELATED_BREAKPOINT and return
7234 pointer to any of them. Return NULL if this system cannot place longjmp
7235 breakpoints. */
7236
7237struct breakpoint *
7238set_longjmp_breakpoint_for_call_dummy (void)
7239{
7240 struct breakpoint *b, *retval = NULL;
7241
7242 ALL_BREAKPOINTS (b)
7243 if (b->pspace == current_program_space && b->type == bp_longjmp_master)
7244 {
7245 struct breakpoint *new_b;
7246
7247 new_b = momentary_breakpoint_from_master (b, bp_longjmp_call_dummy,
a1aa2221
LM
7248 &momentary_breakpoint_ops,
7249 1);
00431a78 7250 new_b->thread = inferior_thread ()->global_num;
e2e4d78b
JK
7251
7252 /* Link NEW_B into the chain of RETVAL breakpoints. */
7253
7254 gdb_assert (new_b->related_breakpoint == new_b);
7255 if (retval == NULL)
7256 retval = new_b;
7257 new_b->related_breakpoint = retval;
7258 while (retval->related_breakpoint != new_b->related_breakpoint)
7259 retval = retval->related_breakpoint;
7260 retval->related_breakpoint = new_b;
7261 }
7262
7263 return retval;
7264}
7265
7266/* Verify all existing dummy frames and their associated breakpoints for
b67a2c6f 7267 TP. Remove those which can no longer be found in the current frame
e2e4d78b
JK
7268 stack.
7269
7270 You should call this function only at places where it is safe to currently
7271 unwind the whole stack. Failed stack unwind would discard live dummy
7272 frames. */
7273
7274void
b67a2c6f 7275check_longjmp_breakpoint_for_call_dummy (struct thread_info *tp)
e2e4d78b
JK
7276{
7277 struct breakpoint *b, *b_tmp;
7278
7279 ALL_BREAKPOINTS_SAFE (b, b_tmp)
5d5658a1 7280 if (b->type == bp_longjmp_call_dummy && b->thread == tp->global_num)
e2e4d78b
JK
7281 {
7282 struct breakpoint *dummy_b = b->related_breakpoint;
7283
7284 while (dummy_b != b && dummy_b->type != bp_call_dummy)
7285 dummy_b = dummy_b->related_breakpoint;
7286 if (dummy_b->type != bp_call_dummy
7287 || frame_find_by_id (dummy_b->frame_id) != NULL)
7288 continue;
7289
00431a78 7290 dummy_frame_discard (dummy_b->frame_id, tp);
e2e4d78b
JK
7291
7292 while (b->related_breakpoint != b)
7293 {
7294 if (b_tmp == b->related_breakpoint)
7295 b_tmp = b->related_breakpoint->next;
7296 delete_breakpoint (b->related_breakpoint);
7297 }
7298 delete_breakpoint (b);
7299 }
7300}
7301
1900040c
MS
7302void
7303enable_overlay_breakpoints (void)
7304{
52f0bd74 7305 struct breakpoint *b;
1900040c
MS
7306
7307 ALL_BREAKPOINTS (b)
7308 if (b->type == bp_overlay_event)
7309 {
7310 b->enable_state = bp_enabled;
44702360 7311 update_global_location_list (UGLL_MAY_INSERT);
c02f5703 7312 overlay_events_enabled = 1;
1900040c
MS
7313 }
7314}
7315
7316void
7317disable_overlay_breakpoints (void)
7318{
52f0bd74 7319 struct breakpoint *b;
1900040c
MS
7320
7321 ALL_BREAKPOINTS (b)
7322 if (b->type == bp_overlay_event)
7323 {
7324 b->enable_state = bp_disabled;
44702360 7325 update_global_location_list (UGLL_DONT_INSERT);
c02f5703 7326 overlay_events_enabled = 0;
1900040c
MS
7327 }
7328}
7329
aa7d318d
TT
7330/* Set an active std::terminate breakpoint for each std::terminate
7331 master breakpoint. */
7332void
7333set_std_terminate_breakpoint (void)
7334{
35df4500 7335 struct breakpoint *b, *b_tmp;
aa7d318d 7336
35df4500 7337 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
7338 if (b->pspace == current_program_space
7339 && b->type == bp_std_terminate_master)
7340 {
06edf0c0 7341 momentary_breakpoint_from_master (b, bp_std_terminate,
a1aa2221 7342 &momentary_breakpoint_ops, 1);
aa7d318d
TT
7343 }
7344}
7345
7346/* Delete all the std::terminate breakpoints. */
7347void
7348delete_std_terminate_breakpoint (void)
7349{
35df4500 7350 struct breakpoint *b, *b_tmp;
aa7d318d 7351
35df4500 7352 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
7353 if (b->type == bp_std_terminate)
7354 delete_breakpoint (b);
7355}
7356
c4093a6a 7357struct breakpoint *
a6d9a66e 7358create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
c4093a6a
JM
7359{
7360 struct breakpoint *b;
c4093a6a 7361
06edf0c0
PA
7362 b = create_internal_breakpoint (gdbarch, address, bp_thread_event,
7363 &internal_breakpoint_ops);
7364
b5de0fa7 7365 b->enable_state = bp_enabled;
f00aae0f 7366 /* location has to be used or breakpoint_re_set will delete me. */
d28cd78a 7367 b->location = new_address_location (b->loc->address, NULL, 0);
c4093a6a 7368
44702360 7369 update_global_location_list_nothrow (UGLL_MAY_INSERT);
74960c60 7370
c4093a6a
JM
7371 return b;
7372}
7373
0101ce28
JJ
7374struct lang_and_radix
7375 {
7376 enum language lang;
7377 int radix;
7378 };
7379
4efc6507
DE
7380/* Create a breakpoint for JIT code registration and unregistration. */
7381
7382struct breakpoint *
7383create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7384{
2a7f3dff
PA
7385 return create_internal_breakpoint (gdbarch, address, bp_jit_event,
7386 &internal_breakpoint_ops);
4efc6507 7387}
0101ce28 7388
03673fc7
PP
7389/* Remove JIT code registration and unregistration breakpoint(s). */
7390
7391void
7392remove_jit_event_breakpoints (void)
7393{
7394 struct breakpoint *b, *b_tmp;
7395
7396 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7397 if (b->type == bp_jit_event
7398 && b->loc->pspace == current_program_space)
7399 delete_breakpoint (b);
7400}
7401
cae688ec
JJ
7402void
7403remove_solib_event_breakpoints (void)
7404{
35df4500 7405 struct breakpoint *b, *b_tmp;
cae688ec 7406
35df4500 7407 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df
PA
7408 if (b->type == bp_shlib_event
7409 && b->loc->pspace == current_program_space)
cae688ec
JJ
7410 delete_breakpoint (b);
7411}
7412
f37f681c
PA
7413/* See breakpoint.h. */
7414
7415void
7416remove_solib_event_breakpoints_at_next_stop (void)
7417{
7418 struct breakpoint *b, *b_tmp;
7419
7420 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7421 if (b->type == bp_shlib_event
7422 && b->loc->pspace == current_program_space)
7423 b->disposition = disp_del_at_next_stop;
7424}
7425
04086b45
PA
7426/* Helper for create_solib_event_breakpoint /
7427 create_and_insert_solib_event_breakpoint. Allows specifying which
7428 INSERT_MODE to pass through to update_global_location_list. */
7429
7430static struct breakpoint *
7431create_solib_event_breakpoint_1 (struct gdbarch *gdbarch, CORE_ADDR address,
7432 enum ugll_insert_mode insert_mode)
cae688ec
JJ
7433{
7434 struct breakpoint *b;
7435
06edf0c0
PA
7436 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event,
7437 &internal_breakpoint_ops);
04086b45 7438 update_global_location_list_nothrow (insert_mode);
cae688ec
JJ
7439 return b;
7440}
7441
04086b45
PA
7442struct breakpoint *
7443create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7444{
7445 return create_solib_event_breakpoint_1 (gdbarch, address, UGLL_MAY_INSERT);
7446}
7447
f37f681c
PA
7448/* See breakpoint.h. */
7449
7450struct breakpoint *
7451create_and_insert_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7452{
7453 struct breakpoint *b;
7454
04086b45
PA
7455 /* Explicitly tell update_global_location_list to insert
7456 locations. */
7457 b = create_solib_event_breakpoint_1 (gdbarch, address, UGLL_INSERT);
f37f681c
PA
7458 if (!b->loc->inserted)
7459 {
7460 delete_breakpoint (b);
7461 return NULL;
7462 }
7463 return b;
7464}
7465
cae688ec
JJ
7466/* Disable any breakpoints that are on code in shared libraries. Only
7467 apply to enabled breakpoints, disabled ones can just stay disabled. */
7468
7469void
cb851954 7470disable_breakpoints_in_shlibs (void)
cae688ec 7471{
876fa593 7472 struct bp_location *loc, **locp_tmp;
cae688ec 7473
876fa593 7474 ALL_BP_LOCATIONS (loc, locp_tmp)
cae688ec 7475 {
2bdf28a0 7476 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 7477 struct breakpoint *b = loc->owner;
2bdf28a0 7478
4a64f543
MS
7479 /* We apply the check to all breakpoints, including disabled for
7480 those with loc->duplicate set. This is so that when breakpoint
7481 becomes enabled, or the duplicate is removed, gdb will try to
7482 insert all breakpoints. If we don't set shlib_disabled here,
7483 we'll try to insert those breakpoints and fail. */
1042e4c0 7484 if (((b->type == bp_breakpoint)
508ccb1f 7485 || (b->type == bp_jit_event)
1042e4c0 7486 || (b->type == bp_hardware_breakpoint)
d77f58be 7487 || (is_tracepoint (b)))
6c95b8df 7488 && loc->pspace == current_program_space
0d381245 7489 && !loc->shlib_disabled
6c95b8df 7490 && solib_name_from_address (loc->pspace, loc->address)
a77053c2 7491 )
0d381245
VP
7492 {
7493 loc->shlib_disabled = 1;
7494 }
cae688ec
JJ
7495 }
7496}
7497
63644780
NB
7498/* Disable any breakpoints and tracepoints that are in SOLIB upon
7499 notification of unloaded_shlib. Only apply to enabled breakpoints,
7500 disabled ones can just stay disabled. */
84acb35a 7501
75149521 7502static void
84acb35a
JJ
7503disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
7504{
876fa593 7505 struct bp_location *loc, **locp_tmp;
84acb35a
JJ
7506 int disabled_shlib_breaks = 0;
7507
876fa593 7508 ALL_BP_LOCATIONS (loc, locp_tmp)
84acb35a 7509 {
2bdf28a0 7510 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 7511 struct breakpoint *b = loc->owner;
cc59ec59 7512
1e4d1764 7513 if (solib->pspace == loc->pspace
e2dd7057 7514 && !loc->shlib_disabled
1e4d1764
YQ
7515 && (((b->type == bp_breakpoint
7516 || b->type == bp_jit_event
7517 || b->type == bp_hardware_breakpoint)
7518 && (loc->loc_type == bp_loc_hardware_breakpoint
7519 || loc->loc_type == bp_loc_software_breakpoint))
7520 || is_tracepoint (b))
e2dd7057 7521 && solib_contains_address_p (solib, loc->address))
84acb35a 7522 {
e2dd7057
PP
7523 loc->shlib_disabled = 1;
7524 /* At this point, we cannot rely on remove_breakpoint
7525 succeeding so we must mark the breakpoint as not inserted
7526 to prevent future errors occurring in remove_breakpoints. */
7527 loc->inserted = 0;
8d3788bd
VP
7528
7529 /* This may cause duplicate notifications for the same breakpoint. */
76727919 7530 gdb::observers::breakpoint_modified.notify (b);
8d3788bd 7531
e2dd7057
PP
7532 if (!disabled_shlib_breaks)
7533 {
223ffa71 7534 target_terminal::ours_for_output ();
3e43a32a
MS
7535 warning (_("Temporarily disabling breakpoints "
7536 "for unloaded shared library \"%s\""),
e2dd7057 7537 solib->so_name);
84acb35a 7538 }
e2dd7057 7539 disabled_shlib_breaks = 1;
84acb35a
JJ
7540 }
7541 }
84acb35a
JJ
7542}
7543
63644780
NB
7544/* Disable any breakpoints and tracepoints in OBJFILE upon
7545 notification of free_objfile. Only apply to enabled breakpoints,
7546 disabled ones can just stay disabled. */
7547
7548static void
7549disable_breakpoints_in_freed_objfile (struct objfile *objfile)
7550{
7551 struct breakpoint *b;
7552
7553 if (objfile == NULL)
7554 return;
7555
d03de421
PA
7556 /* OBJF_SHARED|OBJF_USERLOADED objfiles are dynamic modules manually
7557 managed by the user with add-symbol-file/remove-symbol-file.
7558 Similarly to how breakpoints in shared libraries are handled in
7559 response to "nosharedlibrary", mark breakpoints in such modules
08351840
PA
7560 shlib_disabled so they end up uninserted on the next global
7561 location list update. Shared libraries not loaded by the user
7562 aren't handled here -- they're already handled in
7563 disable_breakpoints_in_unloaded_shlib, called by solib.c's
7564 solib_unloaded observer. We skip objfiles that are not
d03de421
PA
7565 OBJF_SHARED as those aren't considered dynamic objects (e.g. the
7566 main objfile). */
7567 if ((objfile->flags & OBJF_SHARED) == 0
7568 || (objfile->flags & OBJF_USERLOADED) == 0)
63644780
NB
7569 return;
7570
7571 ALL_BREAKPOINTS (b)
7572 {
7573 struct bp_location *loc;
7574 int bp_modified = 0;
7575
7576 if (!is_breakpoint (b) && !is_tracepoint (b))
7577 continue;
7578
7579 for (loc = b->loc; loc != NULL; loc = loc->next)
7580 {
7581 CORE_ADDR loc_addr = loc->address;
7582
7583 if (loc->loc_type != bp_loc_hardware_breakpoint
7584 && loc->loc_type != bp_loc_software_breakpoint)
7585 continue;
7586
7587 if (loc->shlib_disabled != 0)
7588 continue;
7589
7590 if (objfile->pspace != loc->pspace)
7591 continue;
7592
7593 if (loc->loc_type != bp_loc_hardware_breakpoint
7594 && loc->loc_type != bp_loc_software_breakpoint)
7595 continue;
7596
7597 if (is_addr_in_objfile (loc_addr, objfile))
7598 {
7599 loc->shlib_disabled = 1;
08351840
PA
7600 /* At this point, we don't know whether the object was
7601 unmapped from the inferior or not, so leave the
7602 inserted flag alone. We'll handle failure to
7603 uninsert quietly, in case the object was indeed
7604 unmapped. */
63644780
NB
7605
7606 mark_breakpoint_location_modified (loc);
7607
7608 bp_modified = 1;
7609 }
7610 }
7611
7612 if (bp_modified)
76727919 7613 gdb::observers::breakpoint_modified.notify (b);
63644780
NB
7614 }
7615}
7616
ce78b96d
JB
7617/* FORK & VFORK catchpoints. */
7618
e29a4733 7619/* An instance of this type is used to represent a fork or vfork
c1fc2657
SM
7620 catchpoint. A breakpoint is really of this type iff its ops pointer points
7621 to CATCH_FORK_BREAKPOINT_OPS. */
e29a4733 7622
c1fc2657 7623struct fork_catchpoint : public breakpoint
e29a4733 7624{
e29a4733
PA
7625 /* Process id of a child process whose forking triggered this
7626 catchpoint. This field is only valid immediately after this
7627 catchpoint has triggered. */
7628 ptid_t forked_inferior_pid;
7629};
7630
4a64f543
MS
7631/* Implement the "insert" breakpoint_ops method for fork
7632 catchpoints. */
ce78b96d 7633
77b06cd7
TJB
7634static int
7635insert_catch_fork (struct bp_location *bl)
ce78b96d 7636{
e99b03dc 7637 return target_insert_fork_catchpoint (inferior_ptid.pid ());
ce78b96d
JB
7638}
7639
4a64f543
MS
7640/* Implement the "remove" breakpoint_ops method for fork
7641 catchpoints. */
ce78b96d
JB
7642
7643static int
73971819 7644remove_catch_fork (struct bp_location *bl, enum remove_bp_reason reason)
ce78b96d 7645{
e99b03dc 7646 return target_remove_fork_catchpoint (inferior_ptid.pid ());
ce78b96d
JB
7647}
7648
7649/* Implement the "breakpoint_hit" breakpoint_ops method for fork
7650 catchpoints. */
7651
7652static int
f1310107 7653breakpoint_hit_catch_fork (const struct bp_location *bl,
bd522513 7654 const address_space *aspace, CORE_ADDR bp_addr,
09ac7c10 7655 const struct target_waitstatus *ws)
ce78b96d 7656{
e29a4733
PA
7657 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7658
f90263c1
TT
7659 if (ws->kind != TARGET_WAITKIND_FORKED)
7660 return 0;
7661
7662 c->forked_inferior_pid = ws->value.related_pid;
7663 return 1;
ce78b96d
JB
7664}
7665
4a64f543
MS
7666/* Implement the "print_it" breakpoint_ops method for fork
7667 catchpoints. */
ce78b96d
JB
7668
7669static enum print_stop_action
348d480f 7670print_it_catch_fork (bpstat bs)
ce78b96d 7671{
36dfb11c 7672 struct ui_out *uiout = current_uiout;
348d480f
PA
7673 struct breakpoint *b = bs->breakpoint_at;
7674 struct fork_catchpoint *c = (struct fork_catchpoint *) bs->breakpoint_at;
e29a4733 7675
ce78b96d 7676 annotate_catchpoint (b->number);
f303dbd6 7677 maybe_print_thread_hit_breakpoint (uiout);
36dfb11c 7678 if (b->disposition == disp_del)
112e8700 7679 uiout->text ("Temporary catchpoint ");
36dfb11c 7680 else
112e8700
SM
7681 uiout->text ("Catchpoint ");
7682 if (uiout->is_mi_like_p ())
36dfb11c 7683 {
112e8700
SM
7684 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_FORK));
7685 uiout->field_string ("disp", bpdisp_text (b->disposition));
36dfb11c 7686 }
381befee 7687 uiout->field_signed ("bkptno", b->number);
112e8700 7688 uiout->text (" (forked process ");
381befee 7689 uiout->field_signed ("newpid", c->forked_inferior_pid.pid ());
112e8700 7690 uiout->text ("), ");
ce78b96d
JB
7691 return PRINT_SRC_AND_LOC;
7692}
7693
4a64f543
MS
7694/* Implement the "print_one" breakpoint_ops method for fork
7695 catchpoints. */
ce78b96d
JB
7696
7697static void
a6d9a66e 7698print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 7699{
e29a4733 7700 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
79a45b7d 7701 struct value_print_options opts;
79a45e25 7702 struct ui_out *uiout = current_uiout;
79a45b7d
TT
7703
7704 get_user_print_options (&opts);
7705
4a64f543
MS
7706 /* Field 4, the address, is omitted (which makes the columns not
7707 line up too nicely with the headers, but the effect is relatively
7708 readable). */
79a45b7d 7709 if (opts.addressprint)
112e8700 7710 uiout->field_skip ("addr");
ce78b96d 7711 annotate_field (5);
112e8700 7712 uiout->text ("fork");
d7e15655 7713 if (c->forked_inferior_pid != null_ptid)
ce78b96d 7714 {
112e8700 7715 uiout->text (", process ");
381befee 7716 uiout->field_signed ("what", c->forked_inferior_pid.pid ());
112e8700 7717 uiout->spaces (1);
ce78b96d 7718 }
8ac3646f 7719
112e8700
SM
7720 if (uiout->is_mi_like_p ())
7721 uiout->field_string ("catch-type", "fork");
ce78b96d
JB
7722}
7723
7724/* Implement the "print_mention" breakpoint_ops method for fork
7725 catchpoints. */
7726
7727static void
7728print_mention_catch_fork (struct breakpoint *b)
7729{
7730 printf_filtered (_("Catchpoint %d (fork)"), b->number);
7731}
7732
6149aea9
PA
7733/* Implement the "print_recreate" breakpoint_ops method for fork
7734 catchpoints. */
7735
7736static void
7737print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
7738{
7739 fprintf_unfiltered (fp, "catch fork");
d9b3f62e 7740 print_recreate_thread (b, fp);
6149aea9
PA
7741}
7742
ce78b96d
JB
7743/* The breakpoint_ops structure to be used in fork catchpoints. */
7744
2060206e 7745static struct breakpoint_ops catch_fork_breakpoint_ops;
ce78b96d 7746
4a64f543
MS
7747/* Implement the "insert" breakpoint_ops method for vfork
7748 catchpoints. */
ce78b96d 7749
77b06cd7
TJB
7750static int
7751insert_catch_vfork (struct bp_location *bl)
ce78b96d 7752{
e99b03dc 7753 return target_insert_vfork_catchpoint (inferior_ptid.pid ());
ce78b96d
JB
7754}
7755
4a64f543
MS
7756/* Implement the "remove" breakpoint_ops method for vfork
7757 catchpoints. */
ce78b96d
JB
7758
7759static int
73971819 7760remove_catch_vfork (struct bp_location *bl, enum remove_bp_reason reason)
ce78b96d 7761{
e99b03dc 7762 return target_remove_vfork_catchpoint (inferior_ptid.pid ());
ce78b96d
JB
7763}
7764
7765/* Implement the "breakpoint_hit" breakpoint_ops method for vfork
7766 catchpoints. */
7767
7768static int
f1310107 7769breakpoint_hit_catch_vfork (const struct bp_location *bl,
bd522513 7770 const address_space *aspace, CORE_ADDR bp_addr,
09ac7c10 7771 const struct target_waitstatus *ws)
ce78b96d 7772{
e29a4733
PA
7773 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7774
f90263c1
TT
7775 if (ws->kind != TARGET_WAITKIND_VFORKED)
7776 return 0;
7777
7778 c->forked_inferior_pid = ws->value.related_pid;
7779 return 1;
ce78b96d
JB
7780}
7781
4a64f543
MS
7782/* Implement the "print_it" breakpoint_ops method for vfork
7783 catchpoints. */
ce78b96d
JB
7784
7785static enum print_stop_action
348d480f 7786print_it_catch_vfork (bpstat bs)
ce78b96d 7787{
36dfb11c 7788 struct ui_out *uiout = current_uiout;
348d480f 7789 struct breakpoint *b = bs->breakpoint_at;
e29a4733
PA
7790 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
7791
ce78b96d 7792 annotate_catchpoint (b->number);
f303dbd6 7793 maybe_print_thread_hit_breakpoint (uiout);
36dfb11c 7794 if (b->disposition == disp_del)
112e8700 7795 uiout->text ("Temporary catchpoint ");
36dfb11c 7796 else
112e8700
SM
7797 uiout->text ("Catchpoint ");
7798 if (uiout->is_mi_like_p ())
36dfb11c 7799 {
112e8700
SM
7800 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_VFORK));
7801 uiout->field_string ("disp", bpdisp_text (b->disposition));
36dfb11c 7802 }
381befee 7803 uiout->field_signed ("bkptno", b->number);
112e8700 7804 uiout->text (" (vforked process ");
381befee 7805 uiout->field_signed ("newpid", c->forked_inferior_pid.pid ());
112e8700 7806 uiout->text ("), ");
ce78b96d
JB
7807 return PRINT_SRC_AND_LOC;
7808}
7809
4a64f543
MS
7810/* Implement the "print_one" breakpoint_ops method for vfork
7811 catchpoints. */
ce78b96d
JB
7812
7813static void
a6d9a66e 7814print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 7815{
e29a4733 7816 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
79a45b7d 7817 struct value_print_options opts;
79a45e25 7818 struct ui_out *uiout = current_uiout;
79a45b7d
TT
7819
7820 get_user_print_options (&opts);
4a64f543
MS
7821 /* Field 4, the address, is omitted (which makes the columns not
7822 line up too nicely with the headers, but the effect is relatively
7823 readable). */
79a45b7d 7824 if (opts.addressprint)
112e8700 7825 uiout->field_skip ("addr");
ce78b96d 7826 annotate_field (5);
112e8700 7827 uiout->text ("vfork");
d7e15655 7828 if (c->forked_inferior_pid != null_ptid)
ce78b96d 7829 {
112e8700 7830 uiout->text (", process ");
381befee 7831 uiout->field_signed ("what", c->forked_inferior_pid.pid ());
112e8700 7832 uiout->spaces (1);
ce78b96d 7833 }
8ac3646f 7834
112e8700
SM
7835 if (uiout->is_mi_like_p ())
7836 uiout->field_string ("catch-type", "vfork");
ce78b96d
JB
7837}
7838
7839/* Implement the "print_mention" breakpoint_ops method for vfork
7840 catchpoints. */
7841
7842static void
7843print_mention_catch_vfork (struct breakpoint *b)
7844{
7845 printf_filtered (_("Catchpoint %d (vfork)"), b->number);
7846}
7847
6149aea9
PA
7848/* Implement the "print_recreate" breakpoint_ops method for vfork
7849 catchpoints. */
7850
7851static void
7852print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
7853{
7854 fprintf_unfiltered (fp, "catch vfork");
d9b3f62e 7855 print_recreate_thread (b, fp);
6149aea9
PA
7856}
7857
ce78b96d
JB
7858/* The breakpoint_ops structure to be used in vfork catchpoints. */
7859
2060206e 7860static struct breakpoint_ops catch_vfork_breakpoint_ops;
ce78b96d 7861
edcc5120 7862/* An instance of this type is used to represent an solib catchpoint.
c1fc2657 7863 A breakpoint is really of this type iff its ops pointer points to
edcc5120
TT
7864 CATCH_SOLIB_BREAKPOINT_OPS. */
7865
c1fc2657 7866struct solib_catchpoint : public breakpoint
edcc5120 7867{
c1fc2657 7868 ~solib_catchpoint () override;
edcc5120
TT
7869
7870 /* True for "catch load", false for "catch unload". */
7871 unsigned char is_load;
7872
7873 /* Regular expression to match, if any. COMPILED is only valid when
7874 REGEX is non-NULL. */
7875 char *regex;
2d7cc5c7 7876 std::unique_ptr<compiled_regex> compiled;
edcc5120
TT
7877};
7878
c1fc2657 7879solib_catchpoint::~solib_catchpoint ()
edcc5120 7880{
c1fc2657 7881 xfree (this->regex);
edcc5120
TT
7882}
7883
7884static int
7885insert_catch_solib (struct bp_location *ignore)
7886{
7887 return 0;
7888}
7889
7890static int
73971819 7891remove_catch_solib (struct bp_location *ignore, enum remove_bp_reason reason)
edcc5120
TT
7892{
7893 return 0;
7894}
7895
7896static int
7897breakpoint_hit_catch_solib (const struct bp_location *bl,
bd522513 7898 const address_space *aspace,
edcc5120
TT
7899 CORE_ADDR bp_addr,
7900 const struct target_waitstatus *ws)
7901{
7902 struct solib_catchpoint *self = (struct solib_catchpoint *) bl->owner;
7903 struct breakpoint *other;
7904
7905 if (ws->kind == TARGET_WAITKIND_LOADED)
7906 return 1;
7907
7908 ALL_BREAKPOINTS (other)
7909 {
7910 struct bp_location *other_bl;
7911
7912 if (other == bl->owner)
7913 continue;
7914
7915 if (other->type != bp_shlib_event)
7916 continue;
7917
c1fc2657 7918 if (self->pspace != NULL && other->pspace != self->pspace)
edcc5120
TT
7919 continue;
7920
7921 for (other_bl = other->loc; other_bl != NULL; other_bl = other_bl->next)
7922 {
7923 if (other->ops->breakpoint_hit (other_bl, aspace, bp_addr, ws))
7924 return 1;
7925 }
7926 }
7927
7928 return 0;
7929}
7930
7931static void
7932check_status_catch_solib (struct bpstats *bs)
7933{
7934 struct solib_catchpoint *self
7935 = (struct solib_catchpoint *) bs->breakpoint_at;
edcc5120
TT
7936
7937 if (self->is_load)
7938 {
52941706 7939 for (so_list *iter : current_program_space->added_solibs)
edcc5120
TT
7940 {
7941 if (!self->regex
2d7cc5c7 7942 || self->compiled->exec (iter->so_name, 0, NULL, 0) == 0)
edcc5120
TT
7943 return;
7944 }
7945 }
7946 else
7947 {
6fb16ce6 7948 for (const std::string &iter : current_program_space->deleted_solibs)
edcc5120
TT
7949 {
7950 if (!self->regex
6fb16ce6 7951 || self->compiled->exec (iter.c_str (), 0, NULL, 0) == 0)
edcc5120
TT
7952 return;
7953 }
7954 }
7955
7956 bs->stop = 0;
7957 bs->print_it = print_it_noop;
7958}
7959
7960static enum print_stop_action
7961print_it_catch_solib (bpstat bs)
7962{
7963 struct breakpoint *b = bs->breakpoint_at;
7964 struct ui_out *uiout = current_uiout;
7965
7966 annotate_catchpoint (b->number);
f303dbd6 7967 maybe_print_thread_hit_breakpoint (uiout);
edcc5120 7968 if (b->disposition == disp_del)
112e8700 7969 uiout->text ("Temporary catchpoint ");
edcc5120 7970 else
112e8700 7971 uiout->text ("Catchpoint ");
381befee 7972 uiout->field_signed ("bkptno", b->number);
112e8700
SM
7973 uiout->text ("\n");
7974 if (uiout->is_mi_like_p ())
7975 uiout->field_string ("disp", bpdisp_text (b->disposition));
edcc5120
TT
7976 print_solib_event (1);
7977 return PRINT_SRC_AND_LOC;
7978}
7979
7980static void
7981print_one_catch_solib (struct breakpoint *b, struct bp_location **locs)
7982{
7983 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
7984 struct value_print_options opts;
7985 struct ui_out *uiout = current_uiout;
edcc5120
TT
7986
7987 get_user_print_options (&opts);
7988 /* Field 4, the address, is omitted (which makes the columns not
7989 line up too nicely with the headers, but the effect is relatively
7990 readable). */
7991 if (opts.addressprint)
7992 {
7993 annotate_field (4);
112e8700 7994 uiout->field_skip ("addr");
edcc5120
TT
7995 }
7996
528e1572 7997 std::string msg;
edcc5120
TT
7998 annotate_field (5);
7999 if (self->is_load)
8000 {
8001 if (self->regex)
528e1572 8002 msg = string_printf (_("load of library matching %s"), self->regex);
edcc5120 8003 else
528e1572 8004 msg = _("load of library");
edcc5120
TT
8005 }
8006 else
8007 {
8008 if (self->regex)
528e1572 8009 msg = string_printf (_("unload of library matching %s"), self->regex);
edcc5120 8010 else
528e1572 8011 msg = _("unload of library");
edcc5120 8012 }
112e8700 8013 uiout->field_string ("what", msg);
8ac3646f 8014
112e8700
SM
8015 if (uiout->is_mi_like_p ())
8016 uiout->field_string ("catch-type", self->is_load ? "load" : "unload");
edcc5120
TT
8017}
8018
8019static void
8020print_mention_catch_solib (struct breakpoint *b)
8021{
8022 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8023
8024 printf_filtered (_("Catchpoint %d (%s)"), b->number,
8025 self->is_load ? "load" : "unload");
8026}
8027
8028static void
8029print_recreate_catch_solib (struct breakpoint *b, struct ui_file *fp)
8030{
8031 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8032
8033 fprintf_unfiltered (fp, "%s %s",
8034 b->disposition == disp_del ? "tcatch" : "catch",
8035 self->is_load ? "load" : "unload");
8036 if (self->regex)
8037 fprintf_unfiltered (fp, " %s", self->regex);
8038 fprintf_unfiltered (fp, "\n");
8039}
8040
8041static struct breakpoint_ops catch_solib_breakpoint_ops;
8042
91985142
MG
8043/* Shared helper function (MI and CLI) for creating and installing
8044 a shared object event catchpoint. If IS_LOAD is non-zero then
8045 the events to be caught are load events, otherwise they are
8046 unload events. If IS_TEMP is non-zero the catchpoint is a
8047 temporary one. If ENABLED is non-zero the catchpoint is
8048 created in an enabled state. */
edcc5120 8049
91985142 8050void
a121b7c1 8051add_solib_catchpoint (const char *arg, int is_load, int is_temp, int enabled)
edcc5120 8052{
edcc5120 8053 struct gdbarch *gdbarch = get_current_arch ();
edcc5120 8054
edcc5120
TT
8055 if (!arg)
8056 arg = "";
f1735a53 8057 arg = skip_spaces (arg);
edcc5120 8058
36bd8eaa 8059 std::unique_ptr<solib_catchpoint> c (new solib_catchpoint ());
edcc5120
TT
8060
8061 if (*arg != '\0')
8062 {
2d7cc5c7
PA
8063 c->compiled.reset (new compiled_regex (arg, REG_NOSUB,
8064 _("Invalid regexp")));
edcc5120
TT
8065 c->regex = xstrdup (arg);
8066 }
8067
8068 c->is_load = is_load;
36bd8eaa 8069 init_catchpoint (c.get (), gdbarch, is_temp, NULL,
edcc5120
TT
8070 &catch_solib_breakpoint_ops);
8071
c1fc2657 8072 c->enable_state = enabled ? bp_enabled : bp_disabled;
91985142 8073
b270e6f9 8074 install_breakpoint (0, std::move (c), 1);
edcc5120
TT
8075}
8076
91985142
MG
8077/* A helper function that does all the work for "catch load" and
8078 "catch unload". */
8079
8080static void
eb4c3f4a 8081catch_load_or_unload (const char *arg, int from_tty, int is_load,
91985142
MG
8082 struct cmd_list_element *command)
8083{
8084 int tempflag;
8085 const int enabled = 1;
8086
8087 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
8088
8089 add_solib_catchpoint (arg, is_load, tempflag, enabled);
8090}
8091
edcc5120 8092static void
eb4c3f4a 8093catch_load_command_1 (const char *arg, int from_tty,
edcc5120
TT
8094 struct cmd_list_element *command)
8095{
8096 catch_load_or_unload (arg, from_tty, 1, command);
8097}
8098
8099static void
eb4c3f4a 8100catch_unload_command_1 (const char *arg, int from_tty,
edcc5120
TT
8101 struct cmd_list_element *command)
8102{
8103 catch_load_or_unload (arg, from_tty, 0, command);
8104}
8105
346774a9
PA
8106/* Initialize a new breakpoint of the bp_catchpoint kind. If TEMPFLAG
8107 is non-zero, then make the breakpoint temporary. If COND_STRING is
8108 not NULL, then store it in the breakpoint. OPS, if not NULL, is
8109 the breakpoint_ops structure associated to the catchpoint. */
ce78b96d 8110
ab04a2af 8111void
346774a9
PA
8112init_catchpoint (struct breakpoint *b,
8113 struct gdbarch *gdbarch, int tempflag,
63160a43 8114 const char *cond_string,
c0a91b2b 8115 const struct breakpoint_ops *ops)
c906108c 8116{
51abb421 8117 symtab_and_line sal;
6c95b8df 8118 sal.pspace = current_program_space;
c5aa993b 8119
28010a5d 8120 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
ce78b96d 8121
1b36a34b 8122 b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
b5de0fa7 8123 b->disposition = tempflag ? disp_del : disp_donttouch;
346774a9
PA
8124}
8125
28010a5d 8126void
b270e6f9 8127install_breakpoint (int internal, std::unique_ptr<breakpoint> &&arg, int update_gll)
c56053d2 8128{
b270e6f9 8129 breakpoint *b = add_to_breakpoint_chain (std::move (arg));
3a5c3e22 8130 set_breakpoint_number (internal, b);
558a9d82
YQ
8131 if (is_tracepoint (b))
8132 set_tracepoint_count (breakpoint_count);
3a5c3e22
PA
8133 if (!internal)
8134 mention (b);
76727919 8135 gdb::observers::breakpoint_created.notify (b);
3ea46bff
YQ
8136
8137 if (update_gll)
44702360 8138 update_global_location_list (UGLL_MAY_INSERT);
c56053d2
PA
8139}
8140
9b70b993 8141static void
a6d9a66e 8142create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
63160a43 8143 int tempflag, const char *cond_string,
c0a91b2b 8144 const struct breakpoint_ops *ops)
c906108c 8145{
b270e6f9 8146 std::unique_ptr<fork_catchpoint> c (new fork_catchpoint ());
ce78b96d 8147
b270e6f9 8148 init_catchpoint (c.get (), gdbarch, tempflag, cond_string, ops);
e29a4733
PA
8149
8150 c->forked_inferior_pid = null_ptid;
8151
b270e6f9 8152 install_breakpoint (0, std::move (c), 1);
c906108c
SS
8153}
8154
fe798b75
JB
8155/* Exec catchpoints. */
8156
b4d90040 8157/* An instance of this type is used to represent an exec catchpoint.
c1fc2657 8158 A breakpoint is really of this type iff its ops pointer points to
b4d90040
PA
8159 CATCH_EXEC_BREAKPOINT_OPS. */
8160
c1fc2657 8161struct exec_catchpoint : public breakpoint
b4d90040 8162{
c1fc2657 8163 ~exec_catchpoint () override;
b4d90040
PA
8164
8165 /* Filename of a program whose exec triggered this catchpoint.
8166 This field is only valid immediately after this catchpoint has
8167 triggered. */
8168 char *exec_pathname;
8169};
8170
c1fc2657 8171/* Exec catchpoint destructor. */
b4d90040 8172
c1fc2657 8173exec_catchpoint::~exec_catchpoint ()
b4d90040 8174{
c1fc2657 8175 xfree (this->exec_pathname);
b4d90040
PA
8176}
8177
77b06cd7
TJB
8178static int
8179insert_catch_exec (struct bp_location *bl)
c906108c 8180{
e99b03dc 8181 return target_insert_exec_catchpoint (inferior_ptid.pid ());
fe798b75 8182}
c906108c 8183
fe798b75 8184static int
73971819 8185remove_catch_exec (struct bp_location *bl, enum remove_bp_reason reason)
fe798b75 8186{
e99b03dc 8187 return target_remove_exec_catchpoint (inferior_ptid.pid ());
fe798b75 8188}
c906108c 8189
fe798b75 8190static int
f1310107 8191breakpoint_hit_catch_exec (const struct bp_location *bl,
bd522513 8192 const address_space *aspace, CORE_ADDR bp_addr,
09ac7c10 8193 const struct target_waitstatus *ws)
fe798b75 8194{
b4d90040
PA
8195 struct exec_catchpoint *c = (struct exec_catchpoint *) bl->owner;
8196
f90263c1
TT
8197 if (ws->kind != TARGET_WAITKIND_EXECD)
8198 return 0;
8199
8200 c->exec_pathname = xstrdup (ws->value.execd_pathname);
8201 return 1;
fe798b75 8202}
c906108c 8203
fe798b75 8204static enum print_stop_action
348d480f 8205print_it_catch_exec (bpstat bs)
fe798b75 8206{
36dfb11c 8207 struct ui_out *uiout = current_uiout;
348d480f 8208 struct breakpoint *b = bs->breakpoint_at;
b4d90040
PA
8209 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8210
fe798b75 8211 annotate_catchpoint (b->number);
f303dbd6 8212 maybe_print_thread_hit_breakpoint (uiout);
36dfb11c 8213 if (b->disposition == disp_del)
112e8700 8214 uiout->text ("Temporary catchpoint ");
36dfb11c 8215 else
112e8700
SM
8216 uiout->text ("Catchpoint ");
8217 if (uiout->is_mi_like_p ())
36dfb11c 8218 {
112e8700
SM
8219 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_EXEC));
8220 uiout->field_string ("disp", bpdisp_text (b->disposition));
36dfb11c 8221 }
381befee 8222 uiout->field_signed ("bkptno", b->number);
112e8700
SM
8223 uiout->text (" (exec'd ");
8224 uiout->field_string ("new-exec", c->exec_pathname);
8225 uiout->text ("), ");
36dfb11c 8226
fe798b75 8227 return PRINT_SRC_AND_LOC;
c906108c
SS
8228}
8229
fe798b75 8230static void
a6d9a66e 8231print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
fe798b75 8232{
b4d90040 8233 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
fe798b75 8234 struct value_print_options opts;
79a45e25 8235 struct ui_out *uiout = current_uiout;
fe798b75
JB
8236
8237 get_user_print_options (&opts);
8238
8239 /* Field 4, the address, is omitted (which makes the columns
8240 not line up too nicely with the headers, but the effect
8241 is relatively readable). */
8242 if (opts.addressprint)
112e8700 8243 uiout->field_skip ("addr");
fe798b75 8244 annotate_field (5);
112e8700 8245 uiout->text ("exec");
b4d90040 8246 if (c->exec_pathname != NULL)
fe798b75 8247 {
112e8700
SM
8248 uiout->text (", program \"");
8249 uiout->field_string ("what", c->exec_pathname);
8250 uiout->text ("\" ");
fe798b75 8251 }
8ac3646f 8252
112e8700
SM
8253 if (uiout->is_mi_like_p ())
8254 uiout->field_string ("catch-type", "exec");
fe798b75
JB
8255}
8256
8257static void
8258print_mention_catch_exec (struct breakpoint *b)
8259{
8260 printf_filtered (_("Catchpoint %d (exec)"), b->number);
8261}
8262
6149aea9
PA
8263/* Implement the "print_recreate" breakpoint_ops method for exec
8264 catchpoints. */
8265
8266static void
8267print_recreate_catch_exec (struct breakpoint *b, struct ui_file *fp)
8268{
8269 fprintf_unfiltered (fp, "catch exec");
d9b3f62e 8270 print_recreate_thread (b, fp);
6149aea9
PA
8271}
8272
2060206e 8273static struct breakpoint_ops catch_exec_breakpoint_ops;
fe798b75 8274
c906108c 8275static int
fba45db2 8276hw_breakpoint_used_count (void)
c906108c 8277{
c906108c 8278 int i = 0;
f1310107
TJB
8279 struct breakpoint *b;
8280 struct bp_location *bl;
c906108c
SS
8281
8282 ALL_BREAKPOINTS (b)
c5aa993b 8283 {
d6b74ac4 8284 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
f1310107
TJB
8285 for (bl = b->loc; bl; bl = bl->next)
8286 {
8287 /* Special types of hardware breakpoints may use more than
8288 one register. */
348d480f 8289 i += b->ops->resources_needed (bl);
f1310107 8290 }
c5aa993b 8291 }
c906108c
SS
8292
8293 return i;
8294}
8295
a1398e0c
PA
8296/* Returns the resources B would use if it were a hardware
8297 watchpoint. */
8298
c906108c 8299static int
a1398e0c 8300hw_watchpoint_use_count (struct breakpoint *b)
c906108c 8301{
c906108c 8302 int i = 0;
e09342b5 8303 struct bp_location *bl;
c906108c 8304
a1398e0c
PA
8305 if (!breakpoint_enabled (b))
8306 return 0;
8307
8308 for (bl = b->loc; bl; bl = bl->next)
8309 {
8310 /* Special types of hardware watchpoints may use more than
8311 one register. */
8312 i += b->ops->resources_needed (bl);
8313 }
8314
8315 return i;
8316}
8317
8318/* Returns the sum the used resources of all hardware watchpoints of
8319 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
8320 the sum of the used resources of all hardware watchpoints of other
8321 types _not_ TYPE. */
8322
8323static int
8324hw_watchpoint_used_count_others (struct breakpoint *except,
8325 enum bptype type, int *other_type_used)
8326{
8327 int i = 0;
8328 struct breakpoint *b;
8329
c906108c
SS
8330 *other_type_used = 0;
8331 ALL_BREAKPOINTS (b)
e09342b5 8332 {
a1398e0c
PA
8333 if (b == except)
8334 continue;
e09342b5
TJB
8335 if (!breakpoint_enabled (b))
8336 continue;
8337
a1398e0c
PA
8338 if (b->type == type)
8339 i += hw_watchpoint_use_count (b);
8340 else if (is_hardware_watchpoint (b))
8341 *other_type_used = 1;
e09342b5
TJB
8342 }
8343
c906108c
SS
8344 return i;
8345}
8346
c906108c 8347void
fba45db2 8348disable_watchpoints_before_interactive_call_start (void)
c906108c 8349{
c5aa993b 8350 struct breakpoint *b;
c906108c
SS
8351
8352 ALL_BREAKPOINTS (b)
c5aa993b 8353 {
cc60f2e3 8354 if (is_watchpoint (b) && breakpoint_enabled (b))
c5aa993b 8355 {
b5de0fa7 8356 b->enable_state = bp_call_disabled;
44702360 8357 update_global_location_list (UGLL_DONT_INSERT);
c5aa993b
JM
8358 }
8359 }
c906108c
SS
8360}
8361
8362void
fba45db2 8363enable_watchpoints_after_interactive_call_stop (void)
c906108c 8364{
c5aa993b 8365 struct breakpoint *b;
c906108c
SS
8366
8367 ALL_BREAKPOINTS (b)
c5aa993b 8368 {
cc60f2e3 8369 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
c5aa993b 8370 {
b5de0fa7 8371 b->enable_state = bp_enabled;
44702360 8372 update_global_location_list (UGLL_MAY_INSERT);
c5aa993b
JM
8373 }
8374 }
c906108c
SS
8375}
8376
8bea4e01
UW
8377void
8378disable_breakpoints_before_startup (void)
8379{
6c95b8df 8380 current_program_space->executing_startup = 1;
44702360 8381 update_global_location_list (UGLL_DONT_INSERT);
8bea4e01
UW
8382}
8383
8384void
8385enable_breakpoints_after_startup (void)
8386{
6c95b8df 8387 current_program_space->executing_startup = 0;
f8eba3c6 8388 breakpoint_re_set ();
8bea4e01
UW
8389}
8390
7c16b83e
PA
8391/* Create a new single-step breakpoint for thread THREAD, with no
8392 locations. */
c906108c 8393
7c16b83e
PA
8394static struct breakpoint *
8395new_single_step_breakpoint (int thread, struct gdbarch *gdbarch)
8396{
b270e6f9 8397 std::unique_ptr<breakpoint> b (new breakpoint ());
7c16b83e 8398
b270e6f9 8399 init_raw_breakpoint_without_location (b.get (), gdbarch, bp_single_step,
7c16b83e
PA
8400 &momentary_breakpoint_ops);
8401
8402 b->disposition = disp_donttouch;
8403 b->frame_id = null_frame_id;
8404
8405 b->thread = thread;
8406 gdb_assert (b->thread != 0);
8407
b270e6f9 8408 return add_to_breakpoint_chain (std::move (b));
7c16b83e
PA
8409}
8410
8411/* Set a momentary breakpoint of type TYPE at address specified by
8412 SAL. If FRAME_ID is valid, the breakpoint is restricted to that
8413 frame. */
c906108c 8414
454dafbd 8415breakpoint_up
a6d9a66e
UW
8416set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
8417 struct frame_id frame_id, enum bptype type)
c906108c 8418{
52f0bd74 8419 struct breakpoint *b;
edb3359d 8420
193facb3
JK
8421 /* If FRAME_ID is valid, it should be a real frame, not an inlined or
8422 tail-called one. */
8423 gdb_assert (!frame_id_artificial_p (frame_id));
edb3359d 8424
06edf0c0 8425 b = set_raw_breakpoint (gdbarch, sal, type, &momentary_breakpoint_ops);
b5de0fa7
EZ
8426 b->enable_state = bp_enabled;
8427 b->disposition = disp_donttouch;
818dd999 8428 b->frame_id = frame_id;
c906108c 8429
00431a78 8430 b->thread = inferior_thread ()->global_num;
c906108c 8431
44702360 8432 update_global_location_list_nothrow (UGLL_MAY_INSERT);
74960c60 8433
454dafbd 8434 return breakpoint_up (b);
c906108c 8435}
611c83ae 8436
06edf0c0 8437/* Make a momentary breakpoint based on the master breakpoint ORIG.
a1aa2221
LM
8438 The new breakpoint will have type TYPE, use OPS as its
8439 breakpoint_ops, and will set enabled to LOC_ENABLED. */
e58b0e63 8440
06edf0c0
PA
8441static struct breakpoint *
8442momentary_breakpoint_from_master (struct breakpoint *orig,
8443 enum bptype type,
a1aa2221
LM
8444 const struct breakpoint_ops *ops,
8445 int loc_enabled)
e58b0e63
PA
8446{
8447 struct breakpoint *copy;
8448
06edf0c0 8449 copy = set_raw_breakpoint_without_location (orig->gdbarch, type, ops);
e58b0e63 8450 copy->loc = allocate_bp_location (copy);
0ba852ab 8451 set_breakpoint_location_function (copy->loc);
e58b0e63 8452
a6d9a66e 8453 copy->loc->gdbarch = orig->loc->gdbarch;
e58b0e63
PA
8454 copy->loc->requested_address = orig->loc->requested_address;
8455 copy->loc->address = orig->loc->address;
8456 copy->loc->section = orig->loc->section;
6c95b8df 8457 copy->loc->pspace = orig->loc->pspace;
55aa24fb 8458 copy->loc->probe = orig->loc->probe;
f8eba3c6 8459 copy->loc->line_number = orig->loc->line_number;
2f202fde 8460 copy->loc->symtab = orig->loc->symtab;
a1aa2221 8461 copy->loc->enabled = loc_enabled;
e58b0e63
PA
8462 copy->frame_id = orig->frame_id;
8463 copy->thread = orig->thread;
6c95b8df 8464 copy->pspace = orig->pspace;
e58b0e63
PA
8465
8466 copy->enable_state = bp_enabled;
8467 copy->disposition = disp_donttouch;
8468 copy->number = internal_breakpoint_number--;
8469
44702360 8470 update_global_location_list_nothrow (UGLL_DONT_INSERT);
e58b0e63
PA
8471 return copy;
8472}
8473
06edf0c0
PA
8474/* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
8475 ORIG is NULL. */
8476
8477struct breakpoint *
8478clone_momentary_breakpoint (struct breakpoint *orig)
8479{
8480 /* If there's nothing to clone, then return nothing. */
8481 if (orig == NULL)
8482 return NULL;
8483
a1aa2221 8484 return momentary_breakpoint_from_master (orig, orig->type, orig->ops, 0);
06edf0c0
PA
8485}
8486
454dafbd 8487breakpoint_up
a6d9a66e
UW
8488set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
8489 enum bptype type)
611c83ae
PA
8490{
8491 struct symtab_and_line sal;
8492
8493 sal = find_pc_line (pc, 0);
8494 sal.pc = pc;
8495 sal.section = find_pc_overlay (pc);
8496 sal.explicit_pc = 1;
8497
a6d9a66e 8498 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
611c83ae 8499}
c906108c 8500\f
c5aa993b 8501
c906108c
SS
8502/* Tell the user we have just set a breakpoint B. */
8503
8504static void
fba45db2 8505mention (struct breakpoint *b)
c906108c 8506{
348d480f 8507 b->ops->print_mention (b);
2d33446d 8508 current_uiout->text ("\n");
c906108c 8509}
c906108c 8510\f
c5aa993b 8511
5133a315 8512static bool bp_loc_is_permanent (struct bp_location *loc);
1a853c52 8513
7f32a4d5
PA
8514/* Handle "set breakpoint auto-hw on".
8515
8516 If the explicitly specified breakpoint type is not hardware
8517 breakpoint, check the memory map to see whether the breakpoint
8518 address is in read-only memory.
8519
8520 - location type is not hardware breakpoint, memory is read-only.
8521 We change the type of the location to hardware breakpoint.
8522
8523 - location type is hardware breakpoint, memory is read-write. This
8524 means we've previously made the location hardware one, but then the
8525 memory map changed, so we undo.
8526*/
8527
8528static void
8529handle_automatic_hardware_breakpoints (bp_location *bl)
8530{
8531 if (automatic_hardware_breakpoints
8532 && bl->owner->type != bp_hardware_breakpoint
8533 && (bl->loc_type == bp_loc_software_breakpoint
8534 || bl->loc_type == bp_loc_hardware_breakpoint))
8535 {
8536 /* When breakpoints are removed, remove_breakpoints will use
8537 location types we've just set here, the only possible problem
8538 is that memory map has changed during running program, but
8539 it's not going to work anyway with current gdb. */
8540 mem_region *mr = lookup_mem_region (bl->address);
8541
8542 if (mr != nullptr)
8543 {
8544 enum bp_loc_type new_type;
8545
8546 if (mr->attrib.mode != MEM_RW)
8547 new_type = bp_loc_hardware_breakpoint;
8548 else
8549 new_type = bp_loc_software_breakpoint;
8550
8551 if (new_type != bl->loc_type)
8552 {
8553 static bool said = false;
8554
8555 bl->loc_type = new_type;
8556 if (!said)
8557 {
8558 fprintf_filtered (gdb_stdout,
8559 _("Note: automatically using "
8560 "hardware breakpoints for "
8561 "read-only addresses.\n"));
8562 said = true;
8563 }
8564 }
8565 }
8566 }
8567}
8568
0d381245 8569static struct bp_location *
39d61571 8570add_location_to_breakpoint (struct breakpoint *b,
0d381245
VP
8571 const struct symtab_and_line *sal)
8572{
8573 struct bp_location *loc, **tmp;
3742cc8b
YQ
8574 CORE_ADDR adjusted_address;
8575 struct gdbarch *loc_gdbarch = get_sal_arch (*sal);
8576
8577 if (loc_gdbarch == NULL)
8578 loc_gdbarch = b->gdbarch;
8579
8580 /* Adjust the breakpoint's address prior to allocating a location.
8581 Once we call allocate_bp_location(), that mostly uninitialized
8582 location will be placed on the location chain. Adjustment of the
8583 breakpoint may cause target_read_memory() to be called and we do
8584 not want its scan of the location chain to find a breakpoint and
8585 location that's only been partially initialized. */
8586 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
8587 sal->pc, b->type);
0d381245 8588
d30113d4 8589 /* Sort the locations by their ADDRESS. */
39d61571 8590 loc = allocate_bp_location (b);
d30113d4
JK
8591 for (tmp = &(b->loc); *tmp != NULL && (*tmp)->address <= adjusted_address;
8592 tmp = &((*tmp)->next))
0d381245 8593 ;
d30113d4 8594 loc->next = *tmp;
0d381245 8595 *tmp = loc;
3742cc8b 8596
0d381245 8597 loc->requested_address = sal->pc;
3742cc8b 8598 loc->address = adjusted_address;
6c95b8df 8599 loc->pspace = sal->pspace;
935676c9 8600 loc->probe.prob = sal->prob;
729662a5 8601 loc->probe.objfile = sal->objfile;
6c95b8df 8602 gdb_assert (loc->pspace != NULL);
0d381245 8603 loc->section = sal->section;
3742cc8b 8604 loc->gdbarch = loc_gdbarch;
f8eba3c6 8605 loc->line_number = sal->line;
2f202fde 8606 loc->symtab = sal->symtab;
4a27f119 8607 loc->symbol = sal->symbol;
3467ec66
PA
8608 loc->msymbol = sal->msymbol;
8609 loc->objfile = sal->objfile;
f8eba3c6 8610
0ba852ab 8611 set_breakpoint_location_function (loc);
1a853c52 8612
6ae88661
LM
8613 /* While by definition, permanent breakpoints are already present in the
8614 code, we don't mark the location as inserted. Normally one would expect
8615 that GDB could rely on that breakpoint instruction to stop the program,
8616 thus removing the need to insert its own breakpoint, except that executing
8617 the breakpoint instruction can kill the target instead of reporting a
8618 SIGTRAP. E.g., on SPARC, when interrupts are disabled, executing the
8619 instruction resets the CPU, so QEMU 2.0.0 for SPARC correspondingly dies
8620 with "Trap 0x02 while interrupts disabled, Error state". Letting the
8621 breakpoint be inserted normally results in QEMU knowing about the GDB
8622 breakpoint, and thus trap before the breakpoint instruction is executed.
8623 (If GDB later needs to continue execution past the permanent breakpoint,
8624 it manually increments the PC, thus avoiding executing the breakpoint
8625 instruction.) */
1a853c52 8626 if (bp_loc_is_permanent (loc))
6ae88661 8627 loc->permanent = 1;
1a853c52 8628
0d381245
VP
8629 return loc;
8630}
514f746b
AR
8631\f
8632
5133a315
LM
8633/* Return true if LOC is pointing to a permanent breakpoint,
8634 return false otherwise. */
1cf4d951 8635
5133a315 8636static bool
1cf4d951
PA
8637bp_loc_is_permanent (struct bp_location *loc)
8638{
514f746b
AR
8639 gdb_assert (loc != NULL);
8640
cb1e4e32
PA
8641 /* If we have a non-breakpoint-backed catchpoint or a software
8642 watchpoint, just return 0. We should not attempt to read from
8643 the addresses the locations of these breakpoint types point to.
5133a315 8644 gdbarch_program_breakpoint_here_p, below, will attempt to read
244558af 8645 memory. */
cb1e4e32 8646 if (!bl_address_is_meaningful (loc))
5133a315 8647 return false;
244558af 8648
5ed8105e 8649 scoped_restore_current_pspace_and_thread restore_pspace_thread;
6c95b8df 8650 switch_to_program_space_and_thread (loc->pspace);
5133a315 8651 return gdbarch_program_breakpoint_here_p (loc->gdbarch, loc->address);
514f746b
AR
8652}
8653
e7e0cddf
SS
8654/* Build a command list for the dprintf corresponding to the current
8655 settings of the dprintf style options. */
8656
8657static void
8658update_dprintf_command_list (struct breakpoint *b)
8659{
8660 char *dprintf_args = b->extra_string;
8661 char *printf_line = NULL;
8662
8663 if (!dprintf_args)
8664 return;
8665
8666 dprintf_args = skip_spaces (dprintf_args);
8667
8668 /* Allow a comma, as it may have terminated a location, but don't
8669 insist on it. */
8670 if (*dprintf_args == ',')
8671 ++dprintf_args;
8672 dprintf_args = skip_spaces (dprintf_args);
8673
8674 if (*dprintf_args != '"')
8675 error (_("Bad format string, missing '\"'."));
8676
d3ce09f5 8677 if (strcmp (dprintf_style, dprintf_style_gdb) == 0)
e7e0cddf 8678 printf_line = xstrprintf ("printf %s", dprintf_args);
d3ce09f5 8679 else if (strcmp (dprintf_style, dprintf_style_call) == 0)
e7e0cddf
SS
8680 {
8681 if (!dprintf_function)
8682 error (_("No function supplied for dprintf call"));
8683
8684 if (dprintf_channel && strlen (dprintf_channel) > 0)
8685 printf_line = xstrprintf ("call (void) %s (%s,%s)",
8686 dprintf_function,
8687 dprintf_channel,
8688 dprintf_args);
8689 else
8690 printf_line = xstrprintf ("call (void) %s (%s)",
8691 dprintf_function,
8692 dprintf_args);
8693 }
d3ce09f5
SS
8694 else if (strcmp (dprintf_style, dprintf_style_agent) == 0)
8695 {
8696 if (target_can_run_breakpoint_commands ())
8697 printf_line = xstrprintf ("agent-printf %s", dprintf_args);
8698 else
8699 {
8700 warning (_("Target cannot run dprintf commands, falling back to GDB printf"));
8701 printf_line = xstrprintf ("printf %s", dprintf_args);
8702 }
8703 }
e7e0cddf
SS
8704 else
8705 internal_error (__FILE__, __LINE__,
8706 _("Invalid dprintf style."));
8707
f28045c2 8708 gdb_assert (printf_line != NULL);
e7e0cddf 8709
12973681
TT
8710 /* Manufacture a printf sequence. */
8711 struct command_line *printf_cmd_line
8712 = new struct command_line (simple_control, printf_line);
8713 breakpoint_set_commands (b, counted_command_line (printf_cmd_line,
8714 command_lines_deleter ()));
e7e0cddf
SS
8715}
8716
8717/* Update all dprintf commands, making their command lists reflect
8718 current style settings. */
8719
8720static void
eb4c3f4a 8721update_dprintf_commands (const char *args, int from_tty,
e7e0cddf
SS
8722 struct cmd_list_element *c)
8723{
8724 struct breakpoint *b;
8725
8726 ALL_BREAKPOINTS (b)
8727 {
8728 if (b->type == bp_dprintf)
8729 update_dprintf_command_list (b);
8730 }
8731}
c3f6f71d 8732
f00aae0f
KS
8733/* Create a breakpoint with SAL as location. Use LOCATION
8734 as a description of the location, and COND_STRING
b35a8b2f
DE
8735 as condition expression. If LOCATION is NULL then create an
8736 "address location" from the address in the SAL. */
018d34a4
VP
8737
8738static void
d9b3f62e 8739init_breakpoint_sal (struct breakpoint *b, struct gdbarch *gdbarch,
6c5b2ebe 8740 gdb::array_view<const symtab_and_line> sals,
ffc2605c 8741 event_location_up &&location,
e1e01040
PA
8742 gdb::unique_xmalloc_ptr<char> filter,
8743 gdb::unique_xmalloc_ptr<char> cond_string,
8744 gdb::unique_xmalloc_ptr<char> extra_string,
d9b3f62e
PA
8745 enum bptype type, enum bpdisp disposition,
8746 int thread, int task, int ignore_count,
c0a91b2b 8747 const struct breakpoint_ops *ops, int from_tty,
44f238bb
PA
8748 int enabled, int internal, unsigned flags,
8749 int display_canonical)
018d34a4 8750{
0d381245 8751 int i;
018d34a4
VP
8752
8753 if (type == bp_hardware_breakpoint)
8754 {
fbbd034e
AS
8755 int target_resources_ok;
8756
8757 i = hw_breakpoint_used_count ();
8758 target_resources_ok =
8759 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
018d34a4
VP
8760 i + 1, 0);
8761 if (target_resources_ok == 0)
8762 error (_("No hardware breakpoint support in the target."));
8763 else if (target_resources_ok < 0)
8764 error (_("Hardware breakpoints used exceeds limit."));
8765 }
8766
6c5b2ebe 8767 gdb_assert (!sals.empty ());
6c95b8df 8768
6c5b2ebe 8769 for (const auto &sal : sals)
0d381245 8770 {
0d381245
VP
8771 struct bp_location *loc;
8772
8773 if (from_tty)
5af949e3
UW
8774 {
8775 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
8776 if (!loc_gdbarch)
8777 loc_gdbarch = gdbarch;
8778
8779 describe_other_breakpoints (loc_gdbarch,
6c95b8df 8780 sal.pspace, sal.pc, sal.section, thread);
5af949e3 8781 }
0d381245 8782
6c5b2ebe 8783 if (&sal == &sals[0])
0d381245 8784 {
d9b3f62e 8785 init_raw_breakpoint (b, gdbarch, sal, type, ops);
0d381245 8786 b->thread = thread;
4a306c9a 8787 b->task = task;
855a6e68 8788
e1e01040
PA
8789 b->cond_string = cond_string.release ();
8790 b->extra_string = extra_string.release ();
0d381245 8791 b->ignore_count = ignore_count;
41447f92 8792 b->enable_state = enabled ? bp_enabled : bp_disabled;
0d381245 8793 b->disposition = disposition;
6c95b8df 8794
44f238bb
PA
8795 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
8796 b->loc->inserted = 1;
8797
0fb4aa4b
PA
8798 if (type == bp_static_tracepoint)
8799 {
d9b3f62e 8800 struct tracepoint *t = (struct tracepoint *) b;
0fb4aa4b
PA
8801 struct static_tracepoint_marker marker;
8802
983af33b 8803 if (strace_marker_p (b))
0fb4aa4b
PA
8804 {
8805 /* We already know the marker exists, otherwise, we
8806 wouldn't see a sal for it. */
d28cd78a
TT
8807 const char *p
8808 = &event_location_to_string (b->location.get ())[3];
f00aae0f 8809 const char *endp;
0fb4aa4b 8810
f1735a53 8811 p = skip_spaces (p);
0fb4aa4b 8812
f1735a53 8813 endp = skip_to_space (p);
0fb4aa4b 8814
5d9310c4 8815 t->static_trace_marker_id.assign (p, endp - p);
0fb4aa4b 8816
3e43a32a
MS
8817 printf_filtered (_("Probed static tracepoint "
8818 "marker \"%s\"\n"),
5d9310c4 8819 t->static_trace_marker_id.c_str ());
0fb4aa4b
PA
8820 }
8821 else if (target_static_tracepoint_marker_at (sal.pc, &marker))
8822 {
5d9310c4 8823 t->static_trace_marker_id = std::move (marker.str_id);
0fb4aa4b 8824
3e43a32a
MS
8825 printf_filtered (_("Probed static tracepoint "
8826 "marker \"%s\"\n"),
5d9310c4 8827 t->static_trace_marker_id.c_str ());
0fb4aa4b
PA
8828 }
8829 else
3e43a32a
MS
8830 warning (_("Couldn't determine the static "
8831 "tracepoint marker to probe"));
0fb4aa4b
PA
8832 }
8833
0d381245
VP
8834 loc = b->loc;
8835 }
8836 else
018d34a4 8837 {
39d61571 8838 loc = add_location_to_breakpoint (b, &sal);
44f238bb
PA
8839 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
8840 loc->inserted = 1;
0d381245
VP
8841 }
8842
8843 if (b->cond_string)
8844 {
bbc13ae3
KS
8845 const char *arg = b->cond_string;
8846
1bb9788d
TT
8847 loc->cond = parse_exp_1 (&arg, loc->address,
8848 block_for_pc (loc->address), 0);
0d381245 8849 if (*arg)
588ae58c 8850 error (_("Garbage '%s' follows condition"), arg);
018d34a4 8851 }
e7e0cddf
SS
8852
8853 /* Dynamic printf requires and uses additional arguments on the
8854 command line, otherwise it's an error. */
8855 if (type == bp_dprintf)
8856 {
8857 if (b->extra_string)
8858 update_dprintf_command_list (b);
8859 else
8860 error (_("Format string required"));
8861 }
8862 else if (b->extra_string)
588ae58c 8863 error (_("Garbage '%s' at end of command"), b->extra_string);
855a6e68 8864 }
018d34a4 8865
56435ebe 8866 b->display_canonical = display_canonical;
f00aae0f 8867 if (location != NULL)
d28cd78a 8868 b->location = std::move (location);
018d34a4 8869 else
d28cd78a 8870 b->location = new_address_location (b->loc->address, NULL, 0);
c0e8dcd8 8871 b->filter = std::move (filter);
d9b3f62e 8872}
018d34a4 8873
d9b3f62e
PA
8874static void
8875create_breakpoint_sal (struct gdbarch *gdbarch,
6c5b2ebe 8876 gdb::array_view<const symtab_and_line> sals,
ffc2605c 8877 event_location_up &&location,
e1e01040
PA
8878 gdb::unique_xmalloc_ptr<char> filter,
8879 gdb::unique_xmalloc_ptr<char> cond_string,
8880 gdb::unique_xmalloc_ptr<char> extra_string,
d9b3f62e
PA
8881 enum bptype type, enum bpdisp disposition,
8882 int thread, int task, int ignore_count,
c0a91b2b 8883 const struct breakpoint_ops *ops, int from_tty,
44f238bb
PA
8884 int enabled, int internal, unsigned flags,
8885 int display_canonical)
d9b3f62e 8886{
a5e364af 8887 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (type);
d9b3f62e 8888
a5e364af 8889 init_breakpoint_sal (b.get (), gdbarch,
ffc2605c 8890 sals, std::move (location),
e1e01040
PA
8891 std::move (filter),
8892 std::move (cond_string),
8893 std::move (extra_string),
d9b3f62e
PA
8894 type, disposition,
8895 thread, task, ignore_count,
8896 ops, from_tty,
44f238bb
PA
8897 enabled, internal, flags,
8898 display_canonical);
d9b3f62e 8899
b270e6f9 8900 install_breakpoint (internal, std::move (b), 0);
018d34a4
VP
8901}
8902
8903/* Add SALS.nelts breakpoints to the breakpoint table. For each
8904 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
8905 value. COND_STRING, if not NULL, specified the condition to be
8906 used for all breakpoints. Essentially the only case where
8907 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
8908 function. In that case, it's still not possible to specify
8909 separate conditions for different overloaded functions, so
8910 we take just a single condition string.
8911
c3f6f71d 8912 NOTE: If the function succeeds, the caller is expected to cleanup
018d34a4 8913 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
c3f6f71d
JM
8914 array contents). If the function fails (error() is called), the
8915 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
4a64f543 8916 COND and SALS arrays and each of those arrays contents. */
c906108c
SS
8917
8918static void
8cdf0e15 8919create_breakpoints_sal (struct gdbarch *gdbarch,
7efd8fc2 8920 struct linespec_result *canonical,
e1e01040
PA
8921 gdb::unique_xmalloc_ptr<char> cond_string,
8922 gdb::unique_xmalloc_ptr<char> extra_string,
8cdf0e15
VP
8923 enum bptype type, enum bpdisp disposition,
8924 int thread, int task, int ignore_count,
c0a91b2b 8925 const struct breakpoint_ops *ops, int from_tty,
44f238bb 8926 int enabled, int internal, unsigned flags)
c906108c 8927{
f8eba3c6 8928 if (canonical->pre_expanded)
6c5b2ebe 8929 gdb_assert (canonical->lsals.size () == 1);
f8eba3c6 8930
6c5b2ebe 8931 for (const auto &lsal : canonical->lsals)
c3f6f71d 8932 {
f00aae0f 8933 /* Note that 'location' can be NULL in the case of a plain
f8eba3c6 8934 'break', without arguments. */
ffc2605c 8935 event_location_up location
f00aae0f 8936 = (canonical->location != NULL
8e9e35b1 8937 ? copy_event_location (canonical->location.get ()) : NULL);
e1e01040 8938 gdb::unique_xmalloc_ptr<char> filter_string
6c5b2ebe 8939 (lsal.canonical != NULL ? xstrdup (lsal.canonical) : NULL);
0d381245 8940
6c5b2ebe 8941 create_breakpoint_sal (gdbarch, lsal.sals,
ffc2605c 8942 std::move (location),
e1e01040
PA
8943 std::move (filter_string),
8944 std::move (cond_string),
8945 std::move (extra_string),
e7e0cddf 8946 type, disposition,
84f4c1fe 8947 thread, task, ignore_count, ops,
44f238bb 8948 from_tty, enabled, internal, flags,
56435ebe 8949 canonical->special_display);
c3f6f71d 8950 }
c3f6f71d 8951}
c906108c 8952
f00aae0f 8953/* Parse LOCATION which is assumed to be a SAL specification possibly
c3f6f71d 8954 followed by conditionals. On return, SALS contains an array of SAL
f00aae0f
KS
8955 addresses found. LOCATION points to the end of the SAL (for
8956 linespec locations).
9998af43
TJB
8957
8958 The array and the line spec strings are allocated on the heap, it is
8959 the caller's responsibility to free them. */
c906108c 8960
b9362cc7 8961static void
f00aae0f 8962parse_breakpoint_sals (const struct event_location *location,
58438ac1 8963 struct linespec_result *canonical)
c3f6f71d 8964{
f00aae0f
KS
8965 struct symtab_and_line cursal;
8966
8967 if (event_location_type (location) == LINESPEC_LOCATION)
8968 {
a20714ff 8969 const char *spec = get_linespec_location (location)->spec_string;
f00aae0f 8970
a20714ff 8971 if (spec == NULL)
f00aae0f
KS
8972 {
8973 /* The last displayed codepoint, if it's valid, is our default
8974 breakpoint address. */
8975 if (last_displayed_sal_is_valid ())
8976 {
f00aae0f
KS
8977 /* Set sal's pspace, pc, symtab, and line to the values
8978 corresponding to the last call to print_frame_info.
8979 Be sure to reinitialize LINE with NOTCURRENT == 0
8980 as the breakpoint line number is inappropriate otherwise.
8981 find_pc_line would adjust PC, re-set it back. */
51abb421
PA
8982 symtab_and_line sal = get_last_displayed_sal ();
8983 CORE_ADDR pc = sal.pc;
8984
f00aae0f
KS
8985 sal = find_pc_line (pc, 0);
8986
8987 /* "break" without arguments is equivalent to "break *PC"
8988 where PC is the last displayed codepoint's address. So
8989 make sure to set sal.explicit_pc to prevent GDB from
8990 trying to expand the list of sals to include all other
8991 instances with the same symtab and line. */
8992 sal.pc = pc;
8993 sal.explicit_pc = 1;
8994
6c5b2ebe
PA
8995 struct linespec_sals lsal;
8996 lsal.sals = {sal};
f00aae0f
KS
8997 lsal.canonical = NULL;
8998
6c5b2ebe 8999 canonical->lsals.push_back (std::move (lsal));
f00aae0f
KS
9000 return;
9001 }
9002 else
9003 error (_("No default breakpoint address now."));
c906108c 9004 }
c906108c 9005 }
f00aae0f
KS
9006
9007 /* Force almost all breakpoints to be in terms of the
9008 current_source_symtab (which is decode_line_1's default).
9009 This should produce the results we want almost all of the
9010 time while leaving default_breakpoint_* alone.
9011
9012 ObjC: However, don't match an Objective-C method name which
9013 may have a '+' or '-' succeeded by a '['. */
9014 cursal = get_current_source_symtab_and_line ();
9015 if (last_displayed_sal_is_valid ())
c906108c 9016 {
a20714ff 9017 const char *spec = NULL;
cc80f267 9018
f00aae0f 9019 if (event_location_type (location) == LINESPEC_LOCATION)
a20714ff 9020 spec = get_linespec_location (location)->spec_string;
cc80f267 9021
f00aae0f 9022 if (!cursal.symtab
a20714ff
PA
9023 || (spec != NULL
9024 && strchr ("+-", spec[0]) != NULL
9025 && spec[1] != '['))
f00aae0f 9026 {
c2f4122d 9027 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
f00aae0f
KS
9028 get_last_displayed_symtab (),
9029 get_last_displayed_line (),
9030 canonical, NULL, NULL);
9031 return;
9032 }
c906108c 9033 }
f00aae0f 9034
c2f4122d 9035 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
f00aae0f 9036 cursal.symtab, cursal.line, canonical, NULL, NULL);
c3f6f71d 9037}
c906108c 9038
c906108c 9039
c3f6f71d 9040/* Convert each SAL into a real PC. Verify that the PC can be
4a64f543 9041 inserted as a breakpoint. If it can't throw an error. */
c906108c 9042
b9362cc7 9043static void
6c5b2ebe 9044breakpoint_sals_to_pc (std::vector<symtab_and_line> &sals)
c3f6f71d 9045{
6c5b2ebe
PA
9046 for (auto &sal : sals)
9047 resolve_sal_pc (&sal);
c3f6f71d
JM
9048}
9049
7a697b8d
SS
9050/* Fast tracepoints may have restrictions on valid locations. For
9051 instance, a fast tracepoint using a jump instead of a trap will
9052 likely have to overwrite more bytes than a trap would, and so can
9053 only be placed where the instruction is longer than the jump, or a
9054 multi-instruction sequence does not have a jump into the middle of
9055 it, etc. */
9056
9057static void
9058check_fast_tracepoint_sals (struct gdbarch *gdbarch,
6c5b2ebe 9059 gdb::array_view<const symtab_and_line> sals)
7a697b8d 9060{
6c5b2ebe 9061 for (const auto &sal : sals)
7a697b8d 9062 {
f8eba3c6
TT
9063 struct gdbarch *sarch;
9064
6c5b2ebe 9065 sarch = get_sal_arch (sal);
f8eba3c6
TT
9066 /* We fall back to GDBARCH if there is no architecture
9067 associated with SAL. */
9068 if (sarch == NULL)
9069 sarch = gdbarch;
281d762b
TT
9070 std::string msg;
9071 if (!gdbarch_fast_tracepoint_valid_at (sarch, sal.pc, &msg))
53c3572a 9072 error (_("May not have a fast tracepoint at %s%s"),
281d762b 9073 paddress (sarch, sal.pc), msg.c_str ());
7a697b8d
SS
9074 }
9075}
9076
018d34a4
VP
9077/* Given TOK, a string specification of condition and thread, as
9078 accepted by the 'break' command, extract the condition
9079 string and thread number and set *COND_STRING and *THREAD.
4a64f543 9080 PC identifies the context at which the condition should be parsed.
018d34a4
VP
9081 If no condition is found, *COND_STRING is set to NULL.
9082 If no thread is found, *THREAD is set to -1. */
d634f2de
JB
9083
9084static void
bbc13ae3 9085find_condition_and_thread (const char *tok, CORE_ADDR pc,
e7e0cddf
SS
9086 char **cond_string, int *thread, int *task,
9087 char **rest)
018d34a4
VP
9088{
9089 *cond_string = NULL;
9090 *thread = -1;
ed1d1739
KS
9091 *task = 0;
9092 *rest = NULL;
9093
018d34a4
VP
9094 while (tok && *tok)
9095 {
bbc13ae3 9096 const char *end_tok;
018d34a4 9097 int toklen;
bbc13ae3
KS
9098 const char *cond_start = NULL;
9099 const char *cond_end = NULL;
cc59ec59 9100
f1735a53 9101 tok = skip_spaces (tok);
e7e0cddf
SS
9102
9103 if ((*tok == '"' || *tok == ',') && rest)
9104 {
9105 *rest = savestring (tok, strlen (tok));
9106 return;
9107 }
9108
f1735a53 9109 end_tok = skip_to_space (tok);
d634f2de 9110
018d34a4 9111 toklen = end_tok - tok;
d634f2de 9112
018d34a4
VP
9113 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
9114 {
9115 tok = cond_start = end_tok + 1;
4d01a485 9116 parse_exp_1 (&tok, pc, block_for_pc (pc), 0);
018d34a4 9117 cond_end = tok;
d634f2de 9118 *cond_string = savestring (cond_start, cond_end - cond_start);
018d34a4
VP
9119 }
9120 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
9121 {
5d5658a1
PA
9122 const char *tmptok;
9123 struct thread_info *thr;
d634f2de 9124
018d34a4 9125 tok = end_tok + 1;
5d5658a1 9126 thr = parse_thread_id (tok, &tmptok);
018d34a4
VP
9127 if (tok == tmptok)
9128 error (_("Junk after thread keyword."));
5d5658a1 9129 *thread = thr->global_num;
bbc13ae3 9130 tok = tmptok;
018d34a4 9131 }
4a306c9a
JB
9132 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
9133 {
9134 char *tmptok;
9135
9136 tok = end_tok + 1;
bbc13ae3 9137 *task = strtol (tok, &tmptok, 0);
4a306c9a
JB
9138 if (tok == tmptok)
9139 error (_("Junk after task keyword."));
9140 if (!valid_task_id (*task))
b6199126 9141 error (_("Unknown task %d."), *task);
bbc13ae3 9142 tok = tmptok;
4a306c9a 9143 }
e7e0cddf
SS
9144 else if (rest)
9145 {
9146 *rest = savestring (tok, strlen (tok));
ccab2054 9147 return;
e7e0cddf 9148 }
018d34a4
VP
9149 else
9150 error (_("Junk at end of arguments."));
9151 }
9152}
9153
0fb4aa4b
PA
9154/* Decode a static tracepoint marker spec. */
9155
6c5b2ebe 9156static std::vector<symtab_and_line>
f00aae0f 9157decode_static_tracepoint_spec (const char **arg_p)
0fb4aa4b 9158{
f00aae0f
KS
9159 const char *p = &(*arg_p)[3];
9160 const char *endp;
0fb4aa4b 9161
f1735a53 9162 p = skip_spaces (p);
0fb4aa4b 9163
f1735a53 9164 endp = skip_to_space (p);
0fb4aa4b 9165
81b1e71c 9166 std::string marker_str (p, endp - p);
0fb4aa4b 9167
5d9310c4
SM
9168 std::vector<static_tracepoint_marker> markers
9169 = target_static_tracepoint_markers_by_strid (marker_str.c_str ());
9170 if (markers.empty ())
81b1e71c
TT
9171 error (_("No known static tracepoint marker named %s"),
9172 marker_str.c_str ());
0fb4aa4b 9173
6c5b2ebe 9174 std::vector<symtab_and_line> sals;
5d9310c4 9175 sals.reserve (markers.size ());
0fb4aa4b 9176
5d9310c4 9177 for (const static_tracepoint_marker &marker : markers)
0fb4aa4b 9178 {
5d9310c4
SM
9179 symtab_and_line sal = find_pc_line (marker.address, 0);
9180 sal.pc = marker.address;
6c5b2ebe 9181 sals.push_back (sal);
5d9310c4 9182 }
0fb4aa4b 9183
0fb4aa4b
PA
9184 *arg_p = endp;
9185 return sals;
9186}
9187
bac7c5cf
GB
9188/* Returns the breakpoint ops appropriate for use with with LOCATION_TYPE and
9189 according to IS_TRACEPOINT. */
9190
9191static const struct breakpoint_ops *
9192breakpoint_ops_for_event_location_type (enum event_location_type location_type,
9193 bool is_tracepoint)
9194{
9195 if (is_tracepoint)
9196 {
9197 if (location_type == PROBE_LOCATION)
9198 return &tracepoint_probe_breakpoint_ops;
9199 else
9200 return &tracepoint_breakpoint_ops;
9201 }
9202 else
9203 {
9204 if (location_type == PROBE_LOCATION)
9205 return &bkpt_probe_breakpoint_ops;
9206 else
9207 return &bkpt_breakpoint_ops;
9208 }
9209}
9210
9211/* See breakpoint.h. */
9212
9213const struct breakpoint_ops *
9214breakpoint_ops_for_event_location (const struct event_location *location,
9215 bool is_tracepoint)
9216{
9217 if (location != nullptr)
9218 return breakpoint_ops_for_event_location_type
9219 (event_location_type (location), is_tracepoint);
9220 return is_tracepoint ? &tracepoint_breakpoint_ops : &bkpt_breakpoint_ops;
9221}
9222
f00aae0f 9223/* See breakpoint.h. */
0101ce28 9224
8cdf0e15
VP
9225int
9226create_breakpoint (struct gdbarch *gdbarch,
e1e01040
PA
9227 const struct event_location *location,
9228 const char *cond_string,
9229 int thread, const char *extra_string,
f00aae0f 9230 int parse_extra,
0fb4aa4b 9231 int tempflag, enum bptype type_wanted,
8cdf0e15
VP
9232 int ignore_count,
9233 enum auto_boolean pending_break_support,
c0a91b2b 9234 const struct breakpoint_ops *ops,
44f238bb
PA
9235 int from_tty, int enabled, int internal,
9236 unsigned flags)
c3f6f71d 9237{
7efd8fc2 9238 struct linespec_result canonical;
0101ce28 9239 int pending = 0;
4a306c9a 9240 int task = 0;
86b17b60 9241 int prev_bkpt_count = breakpoint_count;
c3f6f71d 9242
348d480f
PA
9243 gdb_assert (ops != NULL);
9244
f00aae0f
KS
9245 /* If extra_string isn't useful, set it to NULL. */
9246 if (extra_string != NULL && *extra_string == '\0')
9247 extra_string = NULL;
9248
a70b8144 9249 try
b78a6381 9250 {
f00aae0f 9251 ops->create_sals_from_location (location, &canonical, type_wanted);
b78a6381 9252 }
230d2906 9253 catch (const gdb_exception_error &e)
0101ce28 9254 {
492d29ea
PA
9255 /* If caller is interested in rc value from parse, set
9256 value. */
9257 if (e.error == NOT_FOUND_ERROR)
0101ce28 9258 {
05ff989b
AC
9259 /* If pending breakpoint support is turned off, throw
9260 error. */
fa8d40ab
JJ
9261
9262 if (pending_break_support == AUTO_BOOLEAN_FALSE)
eedc3f4f 9263 throw;
723a2275
VP
9264
9265 exception_print (gdb_stderr, e);
fa8d40ab 9266
05ff989b
AC
9267 /* If pending breakpoint support is auto query and the user
9268 selects no, then simply return the error code. */
059fb39f 9269 if (pending_break_support == AUTO_BOOLEAN_AUTO
bfccc43c
YQ
9270 && !nquery (_("Make %s pending on future shared library load? "),
9271 bptype_string (type_wanted)))
fd9b8c24 9272 return 0;
fa8d40ab 9273
05ff989b
AC
9274 /* At this point, either the user was queried about setting
9275 a pending breakpoint and selected yes, or pending
9276 breakpoint behavior is on and thus a pending breakpoint
9277 is defaulted on behalf of the user. */
f00aae0f 9278 pending = 1;
0101ce28 9279 }
492d29ea 9280 else
eedc3f4f 9281 throw;
0101ce28 9282 }
492d29ea 9283
6c5b2ebe 9284 if (!pending && canonical.lsals.empty ())
492d29ea 9285 return 0;
c3f6f71d 9286
c3f6f71d
JM
9287 /* Resolve all line numbers to PC's and verify that the addresses
9288 are ok for the target. */
0101ce28 9289 if (!pending)
f8eba3c6 9290 {
6c5b2ebe
PA
9291 for (auto &lsal : canonical.lsals)
9292 breakpoint_sals_to_pc (lsal.sals);
f8eba3c6 9293 }
c3f6f71d 9294
7a697b8d 9295 /* Fast tracepoints may have additional restrictions on location. */
bfccc43c 9296 if (!pending && type_wanted == bp_fast_tracepoint)
f8eba3c6 9297 {
6c5b2ebe
PA
9298 for (const auto &lsal : canonical.lsals)
9299 check_fast_tracepoint_sals (gdbarch, lsal.sals);
f8eba3c6 9300 }
7a697b8d 9301
c3f6f71d
JM
9302 /* Verify that condition can be parsed, before setting any
9303 breakpoints. Allocate a separate condition expression for each
4a64f543 9304 breakpoint. */
0101ce28 9305 if (!pending)
c3f6f71d 9306 {
e1e01040
PA
9307 gdb::unique_xmalloc_ptr<char> cond_string_copy;
9308 gdb::unique_xmalloc_ptr<char> extra_string_copy;
9309
f00aae0f 9310 if (parse_extra)
72b2ff0e 9311 {
0878d0fa 9312 char *rest;
e1e01040 9313 char *cond;
52d361e1 9314
6c5b2ebe 9315 const linespec_sals &lsal = canonical.lsals[0];
52d361e1 9316
0878d0fa
YQ
9317 /* Here we only parse 'arg' to separate condition
9318 from thread number, so parsing in context of first
9319 sal is OK. When setting the breakpoint we'll
9320 re-parse it in context of each sal. */
9321
6c5b2ebe 9322 find_condition_and_thread (extra_string, lsal.sals[0].pc,
e1e01040
PA
9323 &cond, &thread, &task, &rest);
9324 cond_string_copy.reset (cond);
9325 extra_string_copy.reset (rest);
72b2ff0e 9326 }
2f069f6f 9327 else
72b2ff0e 9328 {
f00aae0f
KS
9329 if (type_wanted != bp_dprintf
9330 && extra_string != NULL && *extra_string != '\0')
9331 error (_("Garbage '%s' at end of location"), extra_string);
0878d0fa
YQ
9332
9333 /* Create a private copy of condition string. */
9334 if (cond_string)
e1e01040 9335 cond_string_copy.reset (xstrdup (cond_string));
0878d0fa
YQ
9336 /* Create a private copy of any extra string. */
9337 if (extra_string)
e1e01040 9338 extra_string_copy.reset (xstrdup (extra_string));
72b2ff0e 9339 }
0fb4aa4b 9340
52d361e1 9341 ops->create_breakpoints_sal (gdbarch, &canonical,
e1e01040
PA
9342 std::move (cond_string_copy),
9343 std::move (extra_string_copy),
9344 type_wanted,
d9b3f62e
PA
9345 tempflag ? disp_del : disp_donttouch,
9346 thread, task, ignore_count, ops,
44f238bb 9347 from_tty, enabled, internal, flags);
c906108c 9348 }
0101ce28
JJ
9349 else
9350 {
a5e364af 9351 std::unique_ptr <breakpoint> b = new_breakpoint_from_type (type_wanted);
bfccc43c 9352
a5e364af 9353 init_raw_breakpoint_without_location (b.get (), gdbarch, type_wanted, ops);
d28cd78a 9354 b->location = copy_event_location (location);
bfccc43c 9355
f00aae0f
KS
9356 if (parse_extra)
9357 b->cond_string = NULL;
e12c7713
MK
9358 else
9359 {
9360 /* Create a private copy of condition string. */
e1e01040 9361 b->cond_string = cond_string != NULL ? xstrdup (cond_string) : NULL;
15630549 9362 b->thread = thread;
e12c7713 9363 }
f00aae0f
KS
9364
9365 /* Create a private copy of any extra string. */
e1e01040 9366 b->extra_string = extra_string != NULL ? xstrdup (extra_string) : NULL;
0101ce28 9367 b->ignore_count = ignore_count;
0101ce28 9368 b->disposition = tempflag ? disp_del : disp_donttouch;
0d381245 9369 b->condition_not_parsed = 1;
41447f92 9370 b->enable_state = enabled ? bp_enabled : bp_disabled;
cc72b2a2
KP
9371 if ((type_wanted != bp_breakpoint
9372 && type_wanted != bp_hardware_breakpoint) || thread != -1)
f8eba3c6 9373 b->pspace = current_program_space;
8bea4e01 9374
b270e6f9 9375 install_breakpoint (internal, std::move (b), 0);
0101ce28
JJ
9376 }
9377
6c5b2ebe 9378 if (canonical.lsals.size () > 1)
95a42b64 9379 {
3e43a32a
MS
9380 warning (_("Multiple breakpoints were set.\nUse the "
9381 "\"delete\" command to delete unwanted breakpoints."));
86b17b60 9382 prev_breakpoint_count = prev_bkpt_count;
95a42b64
TT
9383 }
9384
44702360 9385 update_global_location_list (UGLL_MAY_INSERT);
fd9b8c24
PA
9386
9387 return 1;
c3f6f71d 9388}
c906108c 9389
348d480f 9390/* Set a breakpoint.
72b2ff0e
VP
9391 ARG is a string describing breakpoint address,
9392 condition, and thread.
9393 FLAG specifies if a breakpoint is hardware on,
9394 and if breakpoint is temporary, using BP_HARDWARE_FLAG
9395 and BP_TEMPFLAG. */
348d480f 9396
98deb0da 9397static void
f2fc3015 9398break_command_1 (const char *arg, int flag, int from_tty)
c3f6f71d 9399{
72b2ff0e 9400 int tempflag = flag & BP_TEMPFLAG;
0fb4aa4b
PA
9401 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
9402 ? bp_hardware_breakpoint
9403 : bp_breakpoint);
f00aae0f 9404
ffc2605c 9405 event_location_up location = string_to_event_location (&arg, current_language);
bac7c5cf
GB
9406 const struct breakpoint_ops *ops = breakpoint_ops_for_event_location
9407 (location.get (), false /* is_tracepoint */);
c3f6f71d 9408
8cdf0e15 9409 create_breakpoint (get_current_arch (),
ffc2605c 9410 location.get (),
f00aae0f 9411 NULL, 0, arg, 1 /* parse arg */,
0fb4aa4b 9412 tempflag, type_wanted,
8cdf0e15
VP
9413 0 /* Ignore count */,
9414 pending_break_support,
55aa24fb 9415 ops,
8cdf0e15 9416 from_tty,
84f4c1fe 9417 1 /* enabled */,
44f238bb
PA
9418 0 /* internal */,
9419 0);
c906108c
SS
9420}
9421
c906108c
SS
9422/* Helper function for break_command_1 and disassemble_command. */
9423
9424void
fba45db2 9425resolve_sal_pc (struct symtab_and_line *sal)
c906108c
SS
9426{
9427 CORE_ADDR pc;
9428
9429 if (sal->pc == 0 && sal->symtab != NULL)
9430 {
9431 if (!find_line_pc (sal->symtab, sal->line, &pc))
8a3fe4f8 9432 error (_("No line %d in file \"%s\"."),
05cba821 9433 sal->line, symtab_to_filename_for_display (sal->symtab));
c906108c 9434 sal->pc = pc;
6a048695 9435
4a64f543
MS
9436 /* If this SAL corresponds to a breakpoint inserted using a line
9437 number, then skip the function prologue if necessary. */
6a048695 9438 if (sal->explicit_line)
059acae7 9439 skip_prologue_sal (sal);
c906108c
SS
9440 }
9441
9442 if (sal->section == 0 && sal->symtab != NULL)
9443 {
346d1dfe 9444 const struct blockvector *bv;
3977b71f 9445 const struct block *b;
c5aa993b 9446 struct symbol *sym;
c906108c 9447
43f3e411
DE
9448 bv = blockvector_for_pc_sect (sal->pc, 0, &b,
9449 SYMTAB_COMPUNIT (sal->symtab));
c906108c
SS
9450 if (bv != NULL)
9451 {
7f0df278 9452 sym = block_linkage_function (b);
c906108c
SS
9453 if (sym != NULL)
9454 {
eb822aa6
DE
9455 fixup_symbol_section (sym, SYMTAB_OBJFILE (sal->symtab));
9456 sal->section = SYMBOL_OBJ_SECTION (SYMTAB_OBJFILE (sal->symtab),
9457 sym);
c906108c
SS
9458 }
9459 else
9460 {
4a64f543
MS
9461 /* It really is worthwhile to have the section, so we'll
9462 just have to look harder. This case can be executed
9463 if we have line numbers but no functions (as can
9464 happen in assembly source). */
c906108c 9465
5ed8105e 9466 scoped_restore_current_pspace_and_thread restore_pspace_thread;
6c95b8df 9467 switch_to_program_space_and_thread (sal->pspace);
c906108c 9468
5ed8105e 9469 bound_minimal_symbol msym = lookup_minimal_symbol_by_pc (sal->pc);
7cbd4a93 9470 if (msym.minsym)
efd66ac6 9471 sal->section = MSYMBOL_OBJ_SECTION (msym.objfile, msym.minsym);
c906108c
SS
9472 }
9473 }
9474 }
9475}
9476
9477void
0b39b52e 9478break_command (const char *arg, int from_tty)
c906108c 9479{
db107f19 9480 break_command_1 (arg, 0, from_tty);
c906108c
SS
9481}
9482
c906108c 9483void
0b39b52e 9484tbreak_command (const char *arg, int from_tty)
c906108c 9485{
db107f19 9486 break_command_1 (arg, BP_TEMPFLAG, from_tty);
c906108c
SS
9487}
9488
c906108c 9489static void
0b39b52e 9490hbreak_command (const char *arg, int from_tty)
c906108c 9491{
db107f19 9492 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
c906108c
SS
9493}
9494
9495static void
0b39b52e 9496thbreak_command (const char *arg, int from_tty)
c906108c 9497{
db107f19 9498 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
c906108c
SS
9499}
9500
9501static void
ee7ddd71 9502stop_command (const char *arg, int from_tty)
c906108c 9503{
a3f17187 9504 printf_filtered (_("Specify the type of breakpoint to set.\n\
c906108c 9505Usage: stop in <function | address>\n\
a3f17187 9506 stop at <line>\n"));
c906108c
SS
9507}
9508
9509static void
4495129a 9510stopin_command (const char *arg, int from_tty)
c906108c
SS
9511{
9512 int badInput = 0;
9513
cafb3438 9514 if (arg == NULL)
c906108c
SS
9515 badInput = 1;
9516 else if (*arg != '*')
9517 {
4495129a 9518 const char *argptr = arg;
c906108c
SS
9519 int hasColon = 0;
9520
4a64f543 9521 /* Look for a ':'. If this is a line number specification, then
53a5351d 9522 say it is bad, otherwise, it should be an address or
4a64f543 9523 function/method name. */
c906108c 9524 while (*argptr && !hasColon)
c5aa993b
JM
9525 {
9526 hasColon = (*argptr == ':');
9527 argptr++;
9528 }
c906108c
SS
9529
9530 if (hasColon)
c5aa993b 9531 badInput = (*argptr != ':'); /* Not a class::method */
c906108c 9532 else
c5aa993b 9533 badInput = isdigit (*arg); /* a simple line number */
c906108c
SS
9534 }
9535
9536 if (badInput)
a3f17187 9537 printf_filtered (_("Usage: stop in <function | address>\n"));
c906108c 9538 else
db107f19 9539 break_command_1 (arg, 0, from_tty);
c906108c
SS
9540}
9541
9542static void
4495129a 9543stopat_command (const char *arg, int from_tty)
c906108c
SS
9544{
9545 int badInput = 0;
9546
cafb3438 9547 if (arg == NULL || *arg == '*') /* no line number */
c906108c
SS
9548 badInput = 1;
9549 else
9550 {
4495129a 9551 const char *argptr = arg;
c906108c
SS
9552 int hasColon = 0;
9553
4a64f543
MS
9554 /* Look for a ':'. If there is a '::' then get out, otherwise
9555 it is probably a line number. */
c906108c 9556 while (*argptr && !hasColon)
c5aa993b
JM
9557 {
9558 hasColon = (*argptr == ':');
9559 argptr++;
9560 }
c906108c
SS
9561
9562 if (hasColon)
c5aa993b 9563 badInput = (*argptr == ':'); /* we have class::method */
c906108c 9564 else
c5aa993b 9565 badInput = !isdigit (*arg); /* not a line number */
c906108c
SS
9566 }
9567
9568 if (badInput)
65e65158 9569 printf_filtered (_("Usage: stop at LINE\n"));
c906108c 9570 else
db107f19 9571 break_command_1 (arg, 0, from_tty);
c906108c
SS
9572}
9573
e7e0cddf
SS
9574/* The dynamic printf command is mostly like a regular breakpoint, but
9575 with a prewired command list consisting of a single output command,
9576 built from extra arguments supplied on the dprintf command
9577 line. */
9578
da821c7b 9579static void
0b39b52e 9580dprintf_command (const char *arg, int from_tty)
e7e0cddf 9581{
ffc2605c 9582 event_location_up location = string_to_event_location (&arg, current_language);
f00aae0f
KS
9583
9584 /* If non-NULL, ARG should have been advanced past the location;
9585 the next character must be ','. */
9586 if (arg != NULL)
9587 {
9588 if (arg[0] != ',' || arg[1] == '\0')
9589 error (_("Format string required"));
9590 else
9591 {
9592 /* Skip the comma. */
9593 ++arg;
9594 }
9595 }
9596
e7e0cddf 9597 create_breakpoint (get_current_arch (),
ffc2605c 9598 location.get (),
f00aae0f 9599 NULL, 0, arg, 1 /* parse arg */,
e7e0cddf
SS
9600 0, bp_dprintf,
9601 0 /* Ignore count */,
9602 pending_break_support,
9603 &dprintf_breakpoint_ops,
9604 from_tty,
9605 1 /* enabled */,
9606 0 /* internal */,
9607 0);
9608}
9609
d3ce09f5 9610static void
0b39b52e 9611agent_printf_command (const char *arg, int from_tty)
d3ce09f5
SS
9612{
9613 error (_("May only run agent-printf on the target"));
9614}
9615
f1310107
TJB
9616/* Implement the "breakpoint_hit" breakpoint_ops method for
9617 ranged breakpoints. */
9618
9619static int
9620breakpoint_hit_ranged_breakpoint (const struct bp_location *bl,
bd522513 9621 const address_space *aspace,
09ac7c10
TT
9622 CORE_ADDR bp_addr,
9623 const struct target_waitstatus *ws)
f1310107 9624{
09ac7c10 9625 if (ws->kind != TARGET_WAITKIND_STOPPED
a493e3e2 9626 || ws->value.sig != GDB_SIGNAL_TRAP)
09ac7c10
TT
9627 return 0;
9628
f1310107
TJB
9629 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
9630 bl->length, aspace, bp_addr);
9631}
9632
9633/* Implement the "resources_needed" breakpoint_ops method for
9634 ranged breakpoints. */
9635
9636static int
9637resources_needed_ranged_breakpoint (const struct bp_location *bl)
9638{
9639 return target_ranged_break_num_registers ();
9640}
9641
9642/* Implement the "print_it" breakpoint_ops method for
9643 ranged breakpoints. */
9644
9645static enum print_stop_action
348d480f 9646print_it_ranged_breakpoint (bpstat bs)
f1310107 9647{
348d480f 9648 struct breakpoint *b = bs->breakpoint_at;
f1310107 9649 struct bp_location *bl = b->loc;
79a45e25 9650 struct ui_out *uiout = current_uiout;
f1310107
TJB
9651
9652 gdb_assert (b->type == bp_hardware_breakpoint);
9653
9654 /* Ranged breakpoints have only one location. */
9655 gdb_assert (bl && bl->next == NULL);
9656
9657 annotate_breakpoint (b->number);
f303dbd6
PA
9658
9659 maybe_print_thread_hit_breakpoint (uiout);
9660
f1310107 9661 if (b->disposition == disp_del)
112e8700 9662 uiout->text ("Temporary ranged breakpoint ");
f1310107 9663 else
112e8700
SM
9664 uiout->text ("Ranged breakpoint ");
9665 if (uiout->is_mi_like_p ())
f1310107 9666 {
112e8700 9667 uiout->field_string ("reason",
f1310107 9668 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
112e8700 9669 uiout->field_string ("disp", bpdisp_text (b->disposition));
f1310107 9670 }
381befee 9671 uiout->field_signed ("bkptno", b->number);
112e8700 9672 uiout->text (", ");
f1310107
TJB
9673
9674 return PRINT_SRC_AND_LOC;
9675}
9676
9677/* Implement the "print_one" breakpoint_ops method for
9678 ranged breakpoints. */
9679
9680static void
9681print_one_ranged_breakpoint (struct breakpoint *b,
9682 struct bp_location **last_loc)
9683{
9684 struct bp_location *bl = b->loc;
9685 struct value_print_options opts;
79a45e25 9686 struct ui_out *uiout = current_uiout;
f1310107
TJB
9687
9688 /* Ranged breakpoints have only one location. */
9689 gdb_assert (bl && bl->next == NULL);
9690
9691 get_user_print_options (&opts);
9692
9693 if (opts.addressprint)
9694 /* We don't print the address range here, it will be printed later
9695 by print_one_detail_ranged_breakpoint. */
112e8700 9696 uiout->field_skip ("addr");
f1310107
TJB
9697 annotate_field (5);
9698 print_breakpoint_location (b, bl);
9699 *last_loc = bl;
9700}
9701
9702/* Implement the "print_one_detail" breakpoint_ops method for
9703 ranged breakpoints. */
9704
9705static void
9706print_one_detail_ranged_breakpoint (const struct breakpoint *b,
9707 struct ui_out *uiout)
9708{
9709 CORE_ADDR address_start, address_end;
9710 struct bp_location *bl = b->loc;
d7e74731 9711 string_file stb;
f1310107
TJB
9712
9713 gdb_assert (bl);
9714
9715 address_start = bl->address;
9716 address_end = address_start + bl->length - 1;
9717
112e8700 9718 uiout->text ("\taddress range: ");
d7e74731
PA
9719 stb.printf ("[%s, %s]",
9720 print_core_address (bl->gdbarch, address_start),
9721 print_core_address (bl->gdbarch, address_end));
112e8700
SM
9722 uiout->field_stream ("addr", stb);
9723 uiout->text ("\n");
f1310107
TJB
9724}
9725
9726/* Implement the "print_mention" breakpoint_ops method for
9727 ranged breakpoints. */
9728
9729static void
9730print_mention_ranged_breakpoint (struct breakpoint *b)
9731{
9732 struct bp_location *bl = b->loc;
79a45e25 9733 struct ui_out *uiout = current_uiout;
f1310107
TJB
9734
9735 gdb_assert (bl);
9736 gdb_assert (b->type == bp_hardware_breakpoint);
9737
2d33446d
TT
9738 uiout->message (_("Hardware assisted ranged breakpoint %d from %s to %s."),
9739 b->number, paddress (bl->gdbarch, bl->address),
9740 paddress (bl->gdbarch, bl->address + bl->length - 1));
f1310107
TJB
9741}
9742
9743/* Implement the "print_recreate" breakpoint_ops method for
9744 ranged breakpoints. */
9745
9746static void
9747print_recreate_ranged_breakpoint (struct breakpoint *b, struct ui_file *fp)
9748{
f00aae0f 9749 fprintf_unfiltered (fp, "break-range %s, %s",
d28cd78a
TT
9750 event_location_to_string (b->location.get ()),
9751 event_location_to_string (b->location_range_end.get ()));
d9b3f62e 9752 print_recreate_thread (b, fp);
f1310107
TJB
9753}
9754
9755/* The breakpoint_ops structure to be used in ranged breakpoints. */
9756
2060206e 9757static struct breakpoint_ops ranged_breakpoint_ops;
f1310107
TJB
9758
9759/* Find the address where the end of the breakpoint range should be
9760 placed, given the SAL of the end of the range. This is so that if
9761 the user provides a line number, the end of the range is set to the
9762 last instruction of the given line. */
9763
9764static CORE_ADDR
9765find_breakpoint_range_end (struct symtab_and_line sal)
9766{
9767 CORE_ADDR end;
9768
9769 /* If the user provided a PC value, use it. Otherwise,
9770 find the address of the end of the given location. */
9771 if (sal.explicit_pc)
9772 end = sal.pc;
9773 else
9774 {
9775 int ret;
9776 CORE_ADDR start;
9777
9778 ret = find_line_pc_range (sal, &start, &end);
9779 if (!ret)
9780 error (_("Could not find location of the end of the range."));
9781
9782 /* find_line_pc_range returns the start of the next line. */
9783 end--;
9784 }
9785
9786 return end;
9787}
9788
9789/* Implement the "break-range" CLI command. */
9790
9791static void
0b39b52e 9792break_range_command (const char *arg, int from_tty)
f1310107 9793{
f2fc3015 9794 const char *arg_start;
f1310107
TJB
9795 struct linespec_result canonical_start, canonical_end;
9796 int bp_count, can_use_bp, length;
9797 CORE_ADDR end;
9798 struct breakpoint *b;
f1310107
TJB
9799
9800 /* We don't support software ranged breakpoints. */
9801 if (target_ranged_break_num_registers () < 0)
9802 error (_("This target does not support hardware ranged breakpoints."));
9803
9804 bp_count = hw_breakpoint_used_count ();
9805 bp_count += target_ranged_break_num_registers ();
9806 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
9807 bp_count, 0);
9808 if (can_use_bp < 0)
9809 error (_("Hardware breakpoints used exceeds limit."));
9810
f8eba3c6 9811 arg = skip_spaces (arg);
f1310107
TJB
9812 if (arg == NULL || arg[0] == '\0')
9813 error(_("No address range specified."));
9814
f8eba3c6 9815 arg_start = arg;
ffc2605c
TT
9816 event_location_up start_location = string_to_event_location (&arg,
9817 current_language);
9818 parse_breakpoint_sals (start_location.get (), &canonical_start);
f1310107
TJB
9819
9820 if (arg[0] != ',')
9821 error (_("Too few arguments."));
6c5b2ebe 9822 else if (canonical_start.lsals.empty ())
f1310107 9823 error (_("Could not find location of the beginning of the range."));
f8eba3c6 9824
6c5b2ebe 9825 const linespec_sals &lsal_start = canonical_start.lsals[0];
f8eba3c6 9826
6c5b2ebe
PA
9827 if (canonical_start.lsals.size () > 1
9828 || lsal_start.sals.size () != 1)
f1310107
TJB
9829 error (_("Cannot create a ranged breakpoint with multiple locations."));
9830
6c5b2ebe 9831 const symtab_and_line &sal_start = lsal_start.sals[0];
81b1e71c 9832 std::string addr_string_start (arg_start, arg - arg_start);
f1310107
TJB
9833
9834 arg++; /* Skip the comma. */
f8eba3c6 9835 arg = skip_spaces (arg);
f1310107
TJB
9836
9837 /* Parse the end location. */
9838
f1310107
TJB
9839 arg_start = arg;
9840
f8eba3c6 9841 /* We call decode_line_full directly here instead of using
f1310107
TJB
9842 parse_breakpoint_sals because we need to specify the start location's
9843 symtab and line as the default symtab and line for the end of the
9844 range. This makes it possible to have ranges like "foo.c:27, +14",
9845 where +14 means 14 lines from the start location. */
ffc2605c
TT
9846 event_location_up end_location = string_to_event_location (&arg,
9847 current_language);
9848 decode_line_full (end_location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
f8eba3c6
TT
9849 sal_start.symtab, sal_start.line,
9850 &canonical_end, NULL, NULL);
9851
6c5b2ebe 9852 if (canonical_end.lsals.empty ())
f1310107 9853 error (_("Could not find location of the end of the range."));
f8eba3c6 9854
6c5b2ebe
PA
9855 const linespec_sals &lsal_end = canonical_end.lsals[0];
9856 if (canonical_end.lsals.size () > 1
9857 || lsal_end.sals.size () != 1)
f1310107
TJB
9858 error (_("Cannot create a ranged breakpoint with multiple locations."));
9859
6c5b2ebe 9860 const symtab_and_line &sal_end = lsal_end.sals[0];
f1310107
TJB
9861
9862 end = find_breakpoint_range_end (sal_end);
9863 if (sal_start.pc > end)
177b42fe 9864 error (_("Invalid address range, end precedes start."));
f1310107
TJB
9865
9866 length = end - sal_start.pc + 1;
9867 if (length < 0)
9868 /* Length overflowed. */
9869 error (_("Address range too large."));
9870 else if (length == 1)
9871 {
9872 /* This range is simple enough to be handled by
9873 the `hbreak' command. */
81b1e71c 9874 hbreak_command (&addr_string_start[0], 1);
f1310107
TJB
9875
9876 return;
9877 }
9878
9879 /* Now set up the breakpoint. */
9880 b = set_raw_breakpoint (get_current_arch (), sal_start,
348d480f 9881 bp_hardware_breakpoint, &ranged_breakpoint_ops);
f1310107
TJB
9882 set_breakpoint_count (breakpoint_count + 1);
9883 b->number = breakpoint_count;
9884 b->disposition = disp_donttouch;
d28cd78a
TT
9885 b->location = std::move (start_location);
9886 b->location_range_end = std::move (end_location);
f1310107
TJB
9887 b->loc->length = length;
9888
f1310107 9889 mention (b);
76727919 9890 gdb::observers::breakpoint_created.notify (b);
44702360 9891 update_global_location_list (UGLL_MAY_INSERT);
f1310107
TJB
9892}
9893
4a64f543
MS
9894/* Return non-zero if EXP is verified as constant. Returned zero
9895 means EXP is variable. Also the constant detection may fail for
9896 some constant expressions and in such case still falsely return
9897 zero. */
2e6e3d9c 9898
65d79d4b
SDJ
9899static int
9900watchpoint_exp_is_const (const struct expression *exp)
9901{
9902 int i = exp->nelts;
9903
9904 while (i > 0)
9905 {
9906 int oplenp, argsp;
9907
9908 /* We are only interested in the descriptor of each element. */
9909 operator_length (exp, i, &oplenp, &argsp);
9910 i -= oplenp;
9911
9912 switch (exp->elts[i].opcode)
9913 {
9914 case BINOP_ADD:
9915 case BINOP_SUB:
9916 case BINOP_MUL:
9917 case BINOP_DIV:
9918 case BINOP_REM:
9919 case BINOP_MOD:
9920 case BINOP_LSH:
9921 case BINOP_RSH:
9922 case BINOP_LOGICAL_AND:
9923 case BINOP_LOGICAL_OR:
9924 case BINOP_BITWISE_AND:
9925 case BINOP_BITWISE_IOR:
9926 case BINOP_BITWISE_XOR:
9927 case BINOP_EQUAL:
9928 case BINOP_NOTEQUAL:
9929 case BINOP_LESS:
9930 case BINOP_GTR:
9931 case BINOP_LEQ:
9932 case BINOP_GEQ:
9933 case BINOP_REPEAT:
9934 case BINOP_COMMA:
9935 case BINOP_EXP:
9936 case BINOP_MIN:
9937 case BINOP_MAX:
9938 case BINOP_INTDIV:
9939 case BINOP_CONCAT:
65d79d4b
SDJ
9940 case TERNOP_COND:
9941 case TERNOP_SLICE:
65d79d4b
SDJ
9942
9943 case OP_LONG:
edd079d9 9944 case OP_FLOAT:
65d79d4b
SDJ
9945 case OP_LAST:
9946 case OP_COMPLEX:
9947 case OP_STRING:
65d79d4b
SDJ
9948 case OP_ARRAY:
9949 case OP_TYPE:
608b4967
TT
9950 case OP_TYPEOF:
9951 case OP_DECLTYPE:
6e72ca20 9952 case OP_TYPEID:
65d79d4b
SDJ
9953 case OP_NAME:
9954 case OP_OBJC_NSSTRING:
9955
9956 case UNOP_NEG:
9957 case UNOP_LOGICAL_NOT:
9958 case UNOP_COMPLEMENT:
9959 case UNOP_ADDR:
9960 case UNOP_HIGH:
aeaa2474 9961 case UNOP_CAST:
9eaf6705
TT
9962
9963 case UNOP_CAST_TYPE:
9964 case UNOP_REINTERPRET_CAST:
9965 case UNOP_DYNAMIC_CAST:
4a64f543
MS
9966 /* Unary, binary and ternary operators: We have to check
9967 their operands. If they are constant, then so is the
9968 result of that operation. For instance, if A and B are
9969 determined to be constants, then so is "A + B".
9970
9971 UNOP_IND is one exception to the rule above, because the
9972 value of *ADDR is not necessarily a constant, even when
9973 ADDR is. */
65d79d4b
SDJ
9974 break;
9975
9976 case OP_VAR_VALUE:
9977 /* Check whether the associated symbol is a constant.
4a64f543 9978
65d79d4b 9979 We use SYMBOL_CLASS rather than TYPE_CONST because it's
4a64f543
MS
9980 possible that a buggy compiler could mark a variable as
9981 constant even when it is not, and TYPE_CONST would return
9982 true in this case, while SYMBOL_CLASS wouldn't.
9983
9984 We also have to check for function symbols because they
9985 are always constant. */
65d79d4b
SDJ
9986 {
9987 struct symbol *s = exp->elts[i + 2].symbol;
9988
9989 if (SYMBOL_CLASS (s) != LOC_BLOCK
9990 && SYMBOL_CLASS (s) != LOC_CONST
9991 && SYMBOL_CLASS (s) != LOC_CONST_BYTES)
9992 return 0;
9993 break;
9994 }
9995
9996 /* The default action is to return 0 because we are using
9997 the optimistic approach here: If we don't know something,
9998 then it is not a constant. */
9999 default:
10000 return 0;
10001 }
10002 }
10003
10004 return 1;
10005}
10006
c1fc2657 10007/* Watchpoint destructor. */
3a5c3e22 10008
c1fc2657 10009watchpoint::~watchpoint ()
3a5c3e22 10010{
c1fc2657
SM
10011 xfree (this->exp_string);
10012 xfree (this->exp_string_reparse);
3a5c3e22
PA
10013}
10014
348d480f
PA
10015/* Implement the "re_set" breakpoint_ops method for watchpoints. */
10016
10017static void
10018re_set_watchpoint (struct breakpoint *b)
10019{
3a5c3e22
PA
10020 struct watchpoint *w = (struct watchpoint *) b;
10021
348d480f
PA
10022 /* Watchpoint can be either on expression using entirely global
10023 variables, or it can be on local variables.
10024
10025 Watchpoints of the first kind are never auto-deleted, and even
10026 persist across program restarts. Since they can use variables
10027 from shared libraries, we need to reparse expression as libraries
10028 are loaded and unloaded.
10029
10030 Watchpoints on local variables can also change meaning as result
10031 of solib event. For example, if a watchpoint uses both a local
10032 and a global variables in expression, it's a local watchpoint,
10033 but unloading of a shared library will make the expression
10034 invalid. This is not a very common use case, but we still
10035 re-evaluate expression, to avoid surprises to the user.
10036
10037 Note that for local watchpoints, we re-evaluate it only if
10038 watchpoints frame id is still valid. If it's not, it means the
10039 watchpoint is out of scope and will be deleted soon. In fact,
10040 I'm not sure we'll ever be called in this case.
10041
10042 If a local watchpoint's frame id is still valid, then
3a5c3e22 10043 w->exp_valid_block is likewise valid, and we can safely use it.
348d480f 10044
3a5c3e22
PA
10045 Don't do anything about disabled watchpoints, since they will be
10046 reevaluated again when enabled. */
10047 update_watchpoint (w, 1 /* reparse */);
348d480f
PA
10048}
10049
77b06cd7
TJB
10050/* Implement the "insert" breakpoint_ops method for hardware watchpoints. */
10051
10052static int
10053insert_watchpoint (struct bp_location *bl)
10054{
3a5c3e22
PA
10055 struct watchpoint *w = (struct watchpoint *) bl->owner;
10056 int length = w->exact ? 1 : bl->length;
e09342b5
TJB
10057
10058 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
4d01a485 10059 w->cond_exp.get ());
77b06cd7
TJB
10060}
10061
10062/* Implement the "remove" breakpoint_ops method for hardware watchpoints. */
10063
10064static int
73971819 10065remove_watchpoint (struct bp_location *bl, enum remove_bp_reason reason)
77b06cd7 10066{
3a5c3e22
PA
10067 struct watchpoint *w = (struct watchpoint *) bl->owner;
10068 int length = w->exact ? 1 : bl->length;
e09342b5
TJB
10069
10070 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
4d01a485 10071 w->cond_exp.get ());
e09342b5
TJB
10072}
10073
e09342b5 10074static int
348d480f 10075breakpoint_hit_watchpoint (const struct bp_location *bl,
bd522513 10076 const address_space *aspace, CORE_ADDR bp_addr,
09ac7c10 10077 const struct target_waitstatus *ws)
e09342b5 10078{
348d480f 10079 struct breakpoint *b = bl->owner;
3a5c3e22 10080 struct watchpoint *w = (struct watchpoint *) b;
77b06cd7 10081
348d480f
PA
10082 /* Continuable hardware watchpoints are treated as non-existent if the
10083 reason we stopped wasn't a hardware watchpoint (we didn't stop on
10084 some data address). Otherwise gdb won't stop on a break instruction
10085 in the code (not from a breakpoint) when a hardware watchpoint has
10086 been defined. Also skip watchpoints which we know did not trigger
10087 (did not match the data address). */
10088 if (is_hardware_watchpoint (b)
3a5c3e22 10089 && w->watchpoint_triggered == watch_triggered_no)
348d480f 10090 return 0;
9c06b0b4 10091
348d480f 10092 return 1;
9c06b0b4
TJB
10093}
10094
348d480f
PA
10095static void
10096check_status_watchpoint (bpstat bs)
9c06b0b4 10097{
348d480f 10098 gdb_assert (is_watchpoint (bs->breakpoint_at));
9c06b0b4 10099
348d480f 10100 bpstat_check_watchpoint (bs);
9c06b0b4
TJB
10101}
10102
10103/* Implement the "resources_needed" breakpoint_ops method for
348d480f 10104 hardware watchpoints. */
9c06b0b4
TJB
10105
10106static int
348d480f 10107resources_needed_watchpoint (const struct bp_location *bl)
9c06b0b4 10108{
3a5c3e22
PA
10109 struct watchpoint *w = (struct watchpoint *) bl->owner;
10110 int length = w->exact? 1 : bl->length;
348d480f
PA
10111
10112 return target_region_ok_for_hw_watchpoint (bl->address, length);
9c06b0b4
TJB
10113}
10114
10115/* Implement the "works_in_software_mode" breakpoint_ops method for
348d480f 10116 hardware watchpoints. */
9c06b0b4
TJB
10117
10118static int
348d480f 10119works_in_software_mode_watchpoint (const struct breakpoint *b)
9c06b0b4 10120{
efa80663
PA
10121 /* Read and access watchpoints only work with hardware support. */
10122 return b->type == bp_watchpoint || b->type == bp_hardware_watchpoint;
9c06b0b4
TJB
10123}
10124
9c06b0b4 10125static enum print_stop_action
348d480f 10126print_it_watchpoint (bpstat bs)
9c06b0b4 10127{
348d480f 10128 struct breakpoint *b;
348d480f 10129 enum print_stop_action result;
3a5c3e22 10130 struct watchpoint *w;
79a45e25 10131 struct ui_out *uiout = current_uiout;
348d480f
PA
10132
10133 gdb_assert (bs->bp_location_at != NULL);
10134
348d480f 10135 b = bs->breakpoint_at;
3a5c3e22 10136 w = (struct watchpoint *) b;
348d480f 10137
f303dbd6
PA
10138 annotate_watchpoint (b->number);
10139 maybe_print_thread_hit_breakpoint (uiout);
10140
d7e74731
PA
10141 string_file stb;
10142
76f9c9cf 10143 gdb::optional<ui_out_emit_tuple> tuple_emitter;
9c06b0b4
TJB
10144 switch (b->type)
10145 {
348d480f 10146 case bp_watchpoint:
9c06b0b4 10147 case bp_hardware_watchpoint:
112e8700
SM
10148 if (uiout->is_mi_like_p ())
10149 uiout->field_string
10150 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
348d480f 10151 mention (b);
76f9c9cf 10152 tuple_emitter.emplace (uiout, "value");
112e8700 10153 uiout->text ("\nOld value = ");
850645cf 10154 watchpoint_value_print (bs->old_val.get (), &stb);
112e8700
SM
10155 uiout->field_stream ("old", stb);
10156 uiout->text ("\nNew value = ");
850645cf 10157 watchpoint_value_print (w->val.get (), &stb);
112e8700
SM
10158 uiout->field_stream ("new", stb);
10159 uiout->text ("\n");
348d480f
PA
10160 /* More than one watchpoint may have been triggered. */
10161 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10162 break;
10163
10164 case bp_read_watchpoint:
112e8700
SM
10165 if (uiout->is_mi_like_p ())
10166 uiout->field_string
10167 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
348d480f 10168 mention (b);
76f9c9cf 10169 tuple_emitter.emplace (uiout, "value");
112e8700 10170 uiout->text ("\nValue = ");
850645cf 10171 watchpoint_value_print (w->val.get (), &stb);
112e8700
SM
10172 uiout->field_stream ("value", stb);
10173 uiout->text ("\n");
348d480f 10174 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10175 break;
10176
10177 case bp_access_watchpoint:
348d480f
PA
10178 if (bs->old_val != NULL)
10179 {
112e8700
SM
10180 if (uiout->is_mi_like_p ())
10181 uiout->field_string
10182 ("reason",
348d480f
PA
10183 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10184 mention (b);
76f9c9cf 10185 tuple_emitter.emplace (uiout, "value");
112e8700 10186 uiout->text ("\nOld value = ");
850645cf 10187 watchpoint_value_print (bs->old_val.get (), &stb);
112e8700
SM
10188 uiout->field_stream ("old", stb);
10189 uiout->text ("\nNew value = ");
348d480f
PA
10190 }
10191 else
10192 {
10193 mention (b);
112e8700
SM
10194 if (uiout->is_mi_like_p ())
10195 uiout->field_string
10196 ("reason",
348d480f 10197 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
76f9c9cf 10198 tuple_emitter.emplace (uiout, "value");
112e8700 10199 uiout->text ("\nValue = ");
348d480f 10200 }
850645cf 10201 watchpoint_value_print (w->val.get (), &stb);
112e8700
SM
10202 uiout->field_stream ("new", stb);
10203 uiout->text ("\n");
348d480f 10204 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10205 break;
10206 default:
348d480f 10207 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10208 }
10209
348d480f
PA
10210 return result;
10211}
10212
10213/* Implement the "print_mention" breakpoint_ops method for hardware
10214 watchpoints. */
10215
10216static void
10217print_mention_watchpoint (struct breakpoint *b)
10218{
3a5c3e22 10219 struct watchpoint *w = (struct watchpoint *) b;
79a45e25 10220 struct ui_out *uiout = current_uiout;
46b9c129 10221 const char *tuple_name;
348d480f
PA
10222
10223 switch (b->type)
10224 {
10225 case bp_watchpoint:
112e8700 10226 uiout->text ("Watchpoint ");
46b9c129 10227 tuple_name = "wpt";
348d480f
PA
10228 break;
10229 case bp_hardware_watchpoint:
112e8700 10230 uiout->text ("Hardware watchpoint ");
46b9c129 10231 tuple_name = "wpt";
348d480f
PA
10232 break;
10233 case bp_read_watchpoint:
112e8700 10234 uiout->text ("Hardware read watchpoint ");
46b9c129 10235 tuple_name = "hw-rwpt";
348d480f
PA
10236 break;
10237 case bp_access_watchpoint:
112e8700 10238 uiout->text ("Hardware access (read/write) watchpoint ");
46b9c129 10239 tuple_name = "hw-awpt";
348d480f
PA
10240 break;
10241 default:
10242 internal_error (__FILE__, __LINE__,
10243 _("Invalid hardware watchpoint type."));
10244 }
10245
46b9c129 10246 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
381befee 10247 uiout->field_signed ("number", b->number);
112e8700
SM
10248 uiout->text (": ");
10249 uiout->field_string ("exp", w->exp_string);
348d480f
PA
10250}
10251
10252/* Implement the "print_recreate" breakpoint_ops method for
10253 watchpoints. */
10254
10255static void
10256print_recreate_watchpoint (struct breakpoint *b, struct ui_file *fp)
10257{
3a5c3e22
PA
10258 struct watchpoint *w = (struct watchpoint *) b;
10259
348d480f
PA
10260 switch (b->type)
10261 {
10262 case bp_watchpoint:
10263 case bp_hardware_watchpoint:
10264 fprintf_unfiltered (fp, "watch");
10265 break;
10266 case bp_read_watchpoint:
10267 fprintf_unfiltered (fp, "rwatch");
10268 break;
10269 case bp_access_watchpoint:
10270 fprintf_unfiltered (fp, "awatch");
10271 break;
10272 default:
10273 internal_error (__FILE__, __LINE__,
10274 _("Invalid watchpoint type."));
10275 }
10276
3a5c3e22 10277 fprintf_unfiltered (fp, " %s", w->exp_string);
d9b3f62e 10278 print_recreate_thread (b, fp);
348d480f
PA
10279}
10280
427cd150
TT
10281/* Implement the "explains_signal" breakpoint_ops method for
10282 watchpoints. */
10283
47591c29 10284static int
427cd150
TT
10285explains_signal_watchpoint (struct breakpoint *b, enum gdb_signal sig)
10286{
10287 /* A software watchpoint cannot cause a signal other than
10288 GDB_SIGNAL_TRAP. */
10289 if (b->type == bp_watchpoint && sig != GDB_SIGNAL_TRAP)
47591c29 10290 return 0;
427cd150 10291
47591c29 10292 return 1;
427cd150
TT
10293}
10294
348d480f
PA
10295/* The breakpoint_ops structure to be used in hardware watchpoints. */
10296
2060206e 10297static struct breakpoint_ops watchpoint_breakpoint_ops;
348d480f
PA
10298
10299/* Implement the "insert" breakpoint_ops method for
10300 masked hardware watchpoints. */
10301
10302static int
10303insert_masked_watchpoint (struct bp_location *bl)
10304{
3a5c3e22
PA
10305 struct watchpoint *w = (struct watchpoint *) bl->owner;
10306
10307 return target_insert_mask_watchpoint (bl->address, w->hw_wp_mask,
348d480f
PA
10308 bl->watchpoint_type);
10309}
10310
10311/* Implement the "remove" breakpoint_ops method for
10312 masked hardware watchpoints. */
10313
10314static int
73971819 10315remove_masked_watchpoint (struct bp_location *bl, enum remove_bp_reason reason)
348d480f 10316{
3a5c3e22
PA
10317 struct watchpoint *w = (struct watchpoint *) bl->owner;
10318
10319 return target_remove_mask_watchpoint (bl->address, w->hw_wp_mask,
348d480f
PA
10320 bl->watchpoint_type);
10321}
10322
10323/* Implement the "resources_needed" breakpoint_ops method for
10324 masked hardware watchpoints. */
10325
10326static int
10327resources_needed_masked_watchpoint (const struct bp_location *bl)
10328{
3a5c3e22
PA
10329 struct watchpoint *w = (struct watchpoint *) bl->owner;
10330
10331 return target_masked_watch_num_registers (bl->address, w->hw_wp_mask);
348d480f
PA
10332}
10333
10334/* Implement the "works_in_software_mode" breakpoint_ops method for
10335 masked hardware watchpoints. */
10336
10337static int
10338works_in_software_mode_masked_watchpoint (const struct breakpoint *b)
10339{
10340 return 0;
10341}
10342
10343/* Implement the "print_it" breakpoint_ops method for
10344 masked hardware watchpoints. */
10345
10346static enum print_stop_action
10347print_it_masked_watchpoint (bpstat bs)
10348{
10349 struct breakpoint *b = bs->breakpoint_at;
79a45e25 10350 struct ui_out *uiout = current_uiout;
348d480f
PA
10351
10352 /* Masked watchpoints have only one location. */
10353 gdb_assert (b->loc && b->loc->next == NULL);
10354
f303dbd6
PA
10355 annotate_watchpoint (b->number);
10356 maybe_print_thread_hit_breakpoint (uiout);
10357
348d480f
PA
10358 switch (b->type)
10359 {
10360 case bp_hardware_watchpoint:
112e8700
SM
10361 if (uiout->is_mi_like_p ())
10362 uiout->field_string
10363 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
348d480f
PA
10364 break;
10365
10366 case bp_read_watchpoint:
112e8700
SM
10367 if (uiout->is_mi_like_p ())
10368 uiout->field_string
10369 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
348d480f
PA
10370 break;
10371
10372 case bp_access_watchpoint:
112e8700
SM
10373 if (uiout->is_mi_like_p ())
10374 uiout->field_string
10375 ("reason",
348d480f
PA
10376 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10377 break;
10378 default:
10379 internal_error (__FILE__, __LINE__,
10380 _("Invalid hardware watchpoint type."));
10381 }
10382
10383 mention (b);
112e8700 10384 uiout->text (_("\n\
9c06b0b4
TJB
10385Check the underlying instruction at PC for the memory\n\
10386address and value which triggered this watchpoint.\n"));
112e8700 10387 uiout->text ("\n");
9c06b0b4
TJB
10388
10389 /* More than one watchpoint may have been triggered. */
10390 return PRINT_UNKNOWN;
10391}
10392
10393/* Implement the "print_one_detail" breakpoint_ops method for
10394 masked hardware watchpoints. */
10395
10396static void
10397print_one_detail_masked_watchpoint (const struct breakpoint *b,
10398 struct ui_out *uiout)
10399{
3a5c3e22
PA
10400 struct watchpoint *w = (struct watchpoint *) b;
10401
9c06b0b4
TJB
10402 /* Masked watchpoints have only one location. */
10403 gdb_assert (b->loc && b->loc->next == NULL);
10404
112e8700
SM
10405 uiout->text ("\tmask ");
10406 uiout->field_core_addr ("mask", b->loc->gdbarch, w->hw_wp_mask);
10407 uiout->text ("\n");
9c06b0b4
TJB
10408}
10409
10410/* Implement the "print_mention" breakpoint_ops method for
10411 masked hardware watchpoints. */
10412
10413static void
10414print_mention_masked_watchpoint (struct breakpoint *b)
10415{
3a5c3e22 10416 struct watchpoint *w = (struct watchpoint *) b;
79a45e25 10417 struct ui_out *uiout = current_uiout;
46b9c129 10418 const char *tuple_name;
9c06b0b4
TJB
10419
10420 switch (b->type)
10421 {
10422 case bp_hardware_watchpoint:
112e8700 10423 uiout->text ("Masked hardware watchpoint ");
46b9c129 10424 tuple_name = "wpt";
9c06b0b4
TJB
10425 break;
10426 case bp_read_watchpoint:
112e8700 10427 uiout->text ("Masked hardware read watchpoint ");
46b9c129 10428 tuple_name = "hw-rwpt";
9c06b0b4
TJB
10429 break;
10430 case bp_access_watchpoint:
112e8700 10431 uiout->text ("Masked hardware access (read/write) watchpoint ");
46b9c129 10432 tuple_name = "hw-awpt";
9c06b0b4
TJB
10433 break;
10434 default:
10435 internal_error (__FILE__, __LINE__,
10436 _("Invalid hardware watchpoint type."));
10437 }
10438
46b9c129 10439 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
381befee 10440 uiout->field_signed ("number", b->number);
112e8700
SM
10441 uiout->text (": ");
10442 uiout->field_string ("exp", w->exp_string);
9c06b0b4
TJB
10443}
10444
10445/* Implement the "print_recreate" breakpoint_ops method for
10446 masked hardware watchpoints. */
10447
10448static void
10449print_recreate_masked_watchpoint (struct breakpoint *b, struct ui_file *fp)
10450{
3a5c3e22 10451 struct watchpoint *w = (struct watchpoint *) b;
9c06b0b4
TJB
10452
10453 switch (b->type)
10454 {
10455 case bp_hardware_watchpoint:
10456 fprintf_unfiltered (fp, "watch");
10457 break;
10458 case bp_read_watchpoint:
10459 fprintf_unfiltered (fp, "rwatch");
10460 break;
10461 case bp_access_watchpoint:
10462 fprintf_unfiltered (fp, "awatch");
10463 break;
10464 default:
10465 internal_error (__FILE__, __LINE__,
10466 _("Invalid hardware watchpoint type."));
10467 }
10468
53807e9f
TT
10469 fprintf_unfiltered (fp, " %s mask 0x%s", w->exp_string,
10470 phex (w->hw_wp_mask, sizeof (CORE_ADDR)));
d9b3f62e 10471 print_recreate_thread (b, fp);
9c06b0b4
TJB
10472}
10473
10474/* The breakpoint_ops structure to be used in masked hardware watchpoints. */
10475
2060206e 10476static struct breakpoint_ops masked_watchpoint_breakpoint_ops;
9c06b0b4
TJB
10477
10478/* Tell whether the given watchpoint is a masked hardware watchpoint. */
10479
f2478a7e 10480static bool
9c06b0b4
TJB
10481is_masked_watchpoint (const struct breakpoint *b)
10482{
10483 return b->ops == &masked_watchpoint_breakpoint_ops;
10484}
10485
53a5351d
JM
10486/* accessflag: hw_write: watch write,
10487 hw_read: watch read,
10488 hw_access: watch access (read or write) */
c906108c 10489static void
bbc13ae3 10490watch_command_1 (const char *arg, int accessflag, int from_tty,
84f4c1fe 10491 int just_location, int internal)
c906108c 10492{
c1fc2657 10493 struct breakpoint *scope_breakpoint = NULL;
270140bd 10494 const struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
b926417a 10495 struct value *result;
bb9d5f81 10496 int saved_bitpos = 0, saved_bitsize = 0;
bbc13ae3
KS
10497 const char *exp_start = NULL;
10498 const char *exp_end = NULL;
10499 const char *tok, *end_tok;
9c06b0b4 10500 int toklen = -1;
bbc13ae3
KS
10501 const char *cond_start = NULL;
10502 const char *cond_end = NULL;
c906108c 10503 enum bptype bp_type;
37e4754d 10504 int thread = -1;
0cf6dd15 10505 int pc = 0;
9c06b0b4
TJB
10506 /* Flag to indicate whether we are going to use masks for
10507 the hardware watchpoint. */
10508 int use_mask = 0;
10509 CORE_ADDR mask = 0;
c906108c 10510
37e4754d
LM
10511 /* Make sure that we actually have parameters to parse. */
10512 if (arg != NULL && arg[0] != '\0')
10513 {
bbc13ae3
KS
10514 const char *value_start;
10515
10516 exp_end = arg + strlen (arg);
37e4754d 10517
9c06b0b4
TJB
10518 /* Look for "parameter value" pairs at the end
10519 of the arguments string. */
bbc13ae3 10520 for (tok = exp_end - 1; tok > arg; tok--)
9c06b0b4
TJB
10521 {
10522 /* Skip whitespace at the end of the argument list. */
10523 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10524 tok--;
10525
10526 /* Find the beginning of the last token.
10527 This is the value of the parameter. */
10528 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10529 tok--;
10530 value_start = tok + 1;
10531
10532 /* Skip whitespace. */
10533 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10534 tok--;
10535
10536 end_tok = tok;
10537
10538 /* Find the beginning of the second to last token.
10539 This is the parameter itself. */
10540 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10541 tok--;
10542 tok++;
10543 toklen = end_tok - tok + 1;
10544
61012eef 10545 if (toklen == 6 && startswith (tok, "thread"))
9c06b0b4 10546 {
5d5658a1 10547 struct thread_info *thr;
9c06b0b4
TJB
10548 /* At this point we've found a "thread" token, which means
10549 the user is trying to set a watchpoint that triggers
10550 only in a specific thread. */
5d5658a1 10551 const char *endp;
37e4754d 10552
9c06b0b4
TJB
10553 if (thread != -1)
10554 error(_("You can specify only one thread."));
37e4754d 10555
9c06b0b4 10556 /* Extract the thread ID from the next token. */
5d5658a1 10557 thr = parse_thread_id (value_start, &endp);
37e4754d 10558
5d5658a1 10559 /* Check if the user provided a valid thread ID. */
9c06b0b4 10560 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
5d5658a1 10561 invalid_thread_id_error (value_start);
9c06b0b4 10562
5d5658a1 10563 thread = thr->global_num;
9c06b0b4 10564 }
61012eef 10565 else if (toklen == 4 && startswith (tok, "mask"))
9c06b0b4
TJB
10566 {
10567 /* We've found a "mask" token, which means the user wants to
10568 create a hardware watchpoint that is going to have the mask
10569 facility. */
10570 struct value *mask_value, *mark;
37e4754d 10571
9c06b0b4
TJB
10572 if (use_mask)
10573 error(_("You can specify only one mask."));
37e4754d 10574
9c06b0b4 10575 use_mask = just_location = 1;
37e4754d 10576
9c06b0b4
TJB
10577 mark = value_mark ();
10578 mask_value = parse_to_comma_and_eval (&value_start);
10579 mask = value_as_address (mask_value);
10580 value_free_to_mark (mark);
10581 }
10582 else
10583 /* We didn't recognize what we found. We should stop here. */
10584 break;
37e4754d 10585
9c06b0b4
TJB
10586 /* Truncate the string and get rid of the "parameter value" pair before
10587 the arguments string is parsed by the parse_exp_1 function. */
bbc13ae3 10588 exp_end = tok;
9c06b0b4 10589 }
37e4754d 10590 }
bbc13ae3
KS
10591 else
10592 exp_end = arg;
37e4754d 10593
bbc13ae3
KS
10594 /* Parse the rest of the arguments. From here on out, everything
10595 is in terms of a newly allocated string instead of the original
10596 ARG. */
81b1e71c
TT
10597 std::string expression (arg, exp_end - arg);
10598 exp_start = arg = expression.c_str ();
699bd4cf
TT
10599 innermost_block_tracker tracker;
10600 expression_up exp = parse_exp_1 (&arg, 0, 0, 0, &tracker);
c906108c 10601 exp_end = arg;
fa8a61dc
TT
10602 /* Remove trailing whitespace from the expression before saving it.
10603 This makes the eventual display of the expression string a bit
10604 prettier. */
10605 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
10606 --exp_end;
10607
65d79d4b 10608 /* Checking if the expression is not constant. */
4d01a485 10609 if (watchpoint_exp_is_const (exp.get ()))
65d79d4b
SDJ
10610 {
10611 int len;
10612
10613 len = exp_end - exp_start;
10614 while (len > 0 && isspace (exp_start[len - 1]))
10615 len--;
10616 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
10617 }
10618
699bd4cf 10619 exp_valid_block = tracker.block ();
b926417a 10620 struct value *mark = value_mark ();
850645cf
TT
10621 struct value *val_as_value = nullptr;
10622 fetch_subexp_value (exp.get (), &pc, &val_as_value, &result, NULL,
10623 just_location);
06a64a0b 10624
850645cf 10625 if (val_as_value != NULL && just_location)
bb9d5f81 10626 {
850645cf
TT
10627 saved_bitpos = value_bitpos (val_as_value);
10628 saved_bitsize = value_bitsize (val_as_value);
bb9d5f81
PP
10629 }
10630
850645cf 10631 value_ref_ptr val;
06a64a0b
TT
10632 if (just_location)
10633 {
9c06b0b4
TJB
10634 int ret;
10635
06a64a0b 10636 exp_valid_block = NULL;
850645cf 10637 val = release_value (value_addr (result));
06a64a0b 10638 value_free_to_mark (mark);
9c06b0b4
TJB
10639
10640 if (use_mask)
10641 {
850645cf 10642 ret = target_masked_watch_num_registers (value_as_address (val.get ()),
9c06b0b4
TJB
10643 mask);
10644 if (ret == -1)
10645 error (_("This target does not support masked watchpoints."));
10646 else if (ret == -2)
10647 error (_("Invalid mask or memory region."));
10648 }
06a64a0b 10649 }
850645cf
TT
10650 else if (val_as_value != NULL)
10651 val = release_value (val_as_value);
c906108c 10652
f1735a53
TT
10653 tok = skip_spaces (arg);
10654 end_tok = skip_to_space (tok);
c906108c
SS
10655
10656 toklen = end_tok - tok;
10657 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
10658 {
10659 tok = cond_start = end_tok + 1;
699bd4cf
TT
10660 innermost_block_tracker if_tracker;
10661 parse_exp_1 (&tok, 0, 0, 0, &if_tracker);
60e1c644
PA
10662
10663 /* The watchpoint expression may not be local, but the condition
10664 may still be. E.g.: `watch global if local > 0'. */
699bd4cf 10665 cond_exp_valid_block = if_tracker.block ();
60e1c644 10666
c906108c
SS
10667 cond_end = tok;
10668 }
10669 if (*tok)
8a3fe4f8 10670 error (_("Junk at end of command."));
c906108c 10671
441d7c93
PA
10672 frame_info *wp_frame = block_innermost_frame (exp_valid_block);
10673
10674 /* Save this because create_internal_breakpoint below invalidates
10675 'wp_frame'. */
10676 frame_id watchpoint_frame = get_frame_id (wp_frame);
d983da9c
DJ
10677
10678 /* If the expression is "local", then set up a "watchpoint scope"
10679 breakpoint at the point where we've left the scope of the watchpoint
10680 expression. Create the scope breakpoint before the watchpoint, so
10681 that we will encounter it first in bpstat_stop_status. */
441d7c93 10682 if (exp_valid_block != NULL && wp_frame != NULL)
d983da9c 10683 {
441d7c93
PA
10684 frame_id caller_frame_id = frame_unwind_caller_id (wp_frame);
10685
10686 if (frame_id_p (caller_frame_id))
edb3359d 10687 {
441d7c93
PA
10688 gdbarch *caller_arch = frame_unwind_caller_arch (wp_frame);
10689 CORE_ADDR caller_pc = frame_unwind_caller_pc (wp_frame);
10690
edb3359d 10691 scope_breakpoint
441d7c93 10692 = create_internal_breakpoint (caller_arch, caller_pc,
06edf0c0
PA
10693 bp_watchpoint_scope,
10694 &momentary_breakpoint_ops);
d983da9c 10695
441d7c93
PA
10696 /* create_internal_breakpoint could invalidate WP_FRAME. */
10697 wp_frame = NULL;
10698
edb3359d 10699 scope_breakpoint->enable_state = bp_enabled;
d983da9c 10700
edb3359d
DJ
10701 /* Automatically delete the breakpoint when it hits. */
10702 scope_breakpoint->disposition = disp_del;
d983da9c 10703
edb3359d 10704 /* Only break in the proper frame (help with recursion). */
441d7c93 10705 scope_breakpoint->frame_id = caller_frame_id;
d983da9c 10706
edb3359d 10707 /* Set the address at which we will stop. */
441d7c93
PA
10708 scope_breakpoint->loc->gdbarch = caller_arch;
10709 scope_breakpoint->loc->requested_address = caller_pc;
edb3359d 10710 scope_breakpoint->loc->address
a6d9a66e
UW
10711 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
10712 scope_breakpoint->loc->requested_address,
edb3359d
DJ
10713 scope_breakpoint->type);
10714 }
d983da9c
DJ
10715 }
10716
e8369a73
AB
10717 /* Now set up the breakpoint. We create all watchpoints as hardware
10718 watchpoints here even if hardware watchpoints are turned off, a call
10719 to update_watchpoint later in this function will cause the type to
10720 drop back to bp_watchpoint (software watchpoint) if required. */
10721
10722 if (accessflag == hw_read)
10723 bp_type = bp_read_watchpoint;
10724 else if (accessflag == hw_access)
10725 bp_type = bp_access_watchpoint;
10726 else
10727 bp_type = bp_hardware_watchpoint;
3a5c3e22 10728
b270e6f9 10729 std::unique_ptr<watchpoint> w (new watchpoint ());
c1fc2657 10730
348d480f 10731 if (use_mask)
b270e6f9 10732 init_raw_breakpoint_without_location (w.get (), NULL, bp_type,
3a5c3e22 10733 &masked_watchpoint_breakpoint_ops);
348d480f 10734 else
b270e6f9 10735 init_raw_breakpoint_without_location (w.get (), NULL, bp_type,
3a5c3e22 10736 &watchpoint_breakpoint_ops);
c1fc2657
SM
10737 w->thread = thread;
10738 w->disposition = disp_donttouch;
10739 w->pspace = current_program_space;
b22e99fd 10740 w->exp = std::move (exp);
3a5c3e22
PA
10741 w->exp_valid_block = exp_valid_block;
10742 w->cond_exp_valid_block = cond_exp_valid_block;
06a64a0b
TT
10743 if (just_location)
10744 {
850645cf
TT
10745 struct type *t = value_type (val.get ());
10746 CORE_ADDR addr = value_as_address (val.get ());
06a64a0b 10747
43cc5389 10748 w->exp_string_reparse
f16a9f57 10749 = current_language->watch_location_expression (t, addr).release ();
06a64a0b 10750
3a5c3e22 10751 w->exp_string = xstrprintf ("-location %.*s",
d63d0675 10752 (int) (exp_end - exp_start), exp_start);
06a64a0b
TT
10753 }
10754 else
3a5c3e22 10755 w->exp_string = savestring (exp_start, exp_end - exp_start);
9c06b0b4
TJB
10756
10757 if (use_mask)
10758 {
3a5c3e22 10759 w->hw_wp_mask = mask;
9c06b0b4
TJB
10760 }
10761 else
10762 {
3a5c3e22 10763 w->val = val;
bb9d5f81
PP
10764 w->val_bitpos = saved_bitpos;
10765 w->val_bitsize = saved_bitsize;
4c1d86d9 10766 w->val_valid = true;
9c06b0b4 10767 }
77b06cd7 10768
c906108c 10769 if (cond_start)
c1fc2657 10770 w->cond_string = savestring (cond_start, cond_end - cond_start);
c906108c 10771 else
c1fc2657 10772 w->cond_string = 0;
c5aa993b 10773
441d7c93 10774 if (frame_id_p (watchpoint_frame))
f6bc2008 10775 {
441d7c93 10776 w->watchpoint_frame = watchpoint_frame;
3a5c3e22 10777 w->watchpoint_thread = inferior_ptid;
f6bc2008 10778 }
c906108c 10779 else
f6bc2008 10780 {
3a5c3e22
PA
10781 w->watchpoint_frame = null_frame_id;
10782 w->watchpoint_thread = null_ptid;
f6bc2008 10783 }
c906108c 10784
d983da9c 10785 if (scope_breakpoint != NULL)
c906108c 10786 {
d983da9c
DJ
10787 /* The scope breakpoint is related to the watchpoint. We will
10788 need to act on them together. */
c1fc2657 10789 w->related_breakpoint = scope_breakpoint;
b270e6f9 10790 scope_breakpoint->related_breakpoint = w.get ();
c906108c 10791 }
d983da9c 10792
06a64a0b
TT
10793 if (!just_location)
10794 value_free_to_mark (mark);
2d134ed3 10795
b270e6f9
TT
10796 /* Finally update the new watchpoint. This creates the locations
10797 that should be inserted. */
10798 update_watchpoint (w.get (), 1);
a9634178 10799
b270e6f9 10800 install_breakpoint (internal, std::move (w), 1);
c906108c
SS
10801}
10802
e09342b5 10803/* Return count of debug registers needed to watch the given expression.
e09342b5 10804 If the watchpoint cannot be handled in hardware return zero. */
c906108c 10805
c906108c 10806static int
a6535de1 10807can_use_hardware_watchpoint (const std::vector<value_ref_ptr> &vals)
c906108c
SS
10808{
10809 int found_memory_cnt = 0;
10810
10811 /* Did the user specifically forbid us to use hardware watchpoints? */
c5aa993b 10812 if (!can_use_hw_watchpoints)
c906108c 10813 return 0;
c5aa993b 10814
a6535de1
TT
10815 gdb_assert (!vals.empty ());
10816 struct value *head = vals[0].get ();
10817
5c44784c
JM
10818 /* Make sure that the value of the expression depends only upon
10819 memory contents, and values computed from them within GDB. If we
10820 find any register references or function calls, we can't use a
10821 hardware watchpoint.
10822
10823 The idea here is that evaluating an expression generates a series
10824 of values, one holding the value of every subexpression. (The
10825 expression a*b+c has five subexpressions: a, b, a*b, c, and
10826 a*b+c.) GDB's values hold almost enough information to establish
10827 the criteria given above --- they identify memory lvalues,
10828 register lvalues, computed values, etcetera. So we can evaluate
10829 the expression, and then scan the chain of values that leaves
10830 behind to decide whether we can detect any possible change to the
10831 expression's final value using only hardware watchpoints.
10832
10833 However, I don't think that the values returned by inferior
10834 function calls are special in any way. So this function may not
10835 notice that an expression involving an inferior function call
10836 can't be watched with hardware watchpoints. FIXME. */
a6535de1 10837 for (const value_ref_ptr &iter : vals)
c906108c 10838 {
a6535de1
TT
10839 struct value *v = iter.get ();
10840
5c44784c 10841 if (VALUE_LVAL (v) == lval_memory)
c906108c 10842 {
8464be76
DJ
10843 if (v != head && value_lazy (v))
10844 /* A lazy memory lvalue in the chain is one that GDB never
10845 needed to fetch; we either just used its address (e.g.,
10846 `a' in `a.b') or we never needed it at all (e.g., `a'
10847 in `a,b'). This doesn't apply to HEAD; if that is
10848 lazy then it was not readable, but watch it anyway. */
5c44784c 10849 ;
53a5351d 10850 else
5c44784c
JM
10851 {
10852 /* Ahh, memory we actually used! Check if we can cover
10853 it with hardware watchpoints. */
df407dfe 10854 struct type *vtype = check_typedef (value_type (v));
2e70b7b9
MS
10855
10856 /* We only watch structs and arrays if user asked for it
10857 explicitly, never if they just happen to appear in a
10858 middle of some value chain. */
10859 if (v == head
78134374
SM
10860 || (vtype->code () != TYPE_CODE_STRUCT
10861 && vtype->code () != TYPE_CODE_ARRAY))
2e70b7b9 10862 {
42ae5230 10863 CORE_ADDR vaddr = value_address (v);
e09342b5
TJB
10864 int len;
10865 int num_regs;
10866
a9634178 10867 len = (target_exact_watchpoints
e09342b5
TJB
10868 && is_scalar_type_recursive (vtype))?
10869 1 : TYPE_LENGTH (value_type (v));
2e70b7b9 10870
e09342b5
TJB
10871 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
10872 if (!num_regs)
2e70b7b9
MS
10873 return 0;
10874 else
e09342b5 10875 found_memory_cnt += num_regs;
2e70b7b9 10876 }
5c44784c 10877 }
c5aa993b 10878 }
5086187c
AC
10879 else if (VALUE_LVAL (v) != not_lval
10880 && deprecated_value_modifiable (v) == 0)
38b6c3b3 10881 return 0; /* These are values from the history (e.g., $1). */
5086187c 10882 else if (VALUE_LVAL (v) == lval_register)
38b6c3b3 10883 return 0; /* Cannot watch a register with a HW watchpoint. */
c906108c
SS
10884 }
10885
10886 /* The expression itself looks suitable for using a hardware
10887 watchpoint, but give the target machine a chance to reject it. */
10888 return found_memory_cnt;
10889}
10890
8b93c638 10891void
f2fc3015 10892watch_command_wrapper (const char *arg, int from_tty, int internal)
8b93c638 10893{
84f4c1fe 10894 watch_command_1 (arg, hw_write, from_tty, 0, internal);
06a64a0b
TT
10895}
10896
06a64a0b
TT
10897/* A helper function that looks for the "-location" argument and then
10898 calls watch_command_1. */
10899
10900static void
0b39b52e 10901watch_maybe_just_location (const char *arg, int accessflag, int from_tty)
06a64a0b
TT
10902{
10903 int just_location = 0;
10904
10905 if (arg
10906 && (check_for_argument (&arg, "-location", sizeof ("-location") - 1)
10907 || check_for_argument (&arg, "-l", sizeof ("-l") - 1)))
cbba3ecd 10908 just_location = 1;
06a64a0b 10909
84f4c1fe 10910 watch_command_1 (arg, accessflag, from_tty, just_location, 0);
8b93c638 10911}
8926118c 10912
c5aa993b 10913static void
0b39b52e 10914watch_command (const char *arg, int from_tty)
c906108c 10915{
06a64a0b 10916 watch_maybe_just_location (arg, hw_write, from_tty);
c906108c
SS
10917}
10918
8b93c638 10919void
f2fc3015 10920rwatch_command_wrapper (const char *arg, int from_tty, int internal)
8b93c638 10921{
84f4c1fe 10922 watch_command_1 (arg, hw_read, from_tty, 0, internal);
8b93c638 10923}
8926118c 10924
c5aa993b 10925static void
0b39b52e 10926rwatch_command (const char *arg, int from_tty)
c906108c 10927{
06a64a0b 10928 watch_maybe_just_location (arg, hw_read, from_tty);
c906108c
SS
10929}
10930
8b93c638 10931void
f2fc3015 10932awatch_command_wrapper (const char *arg, int from_tty, int internal)
8b93c638 10933{
84f4c1fe 10934 watch_command_1 (arg, hw_access, from_tty, 0, internal);
8b93c638 10935}
8926118c 10936
c5aa993b 10937static void
0b39b52e 10938awatch_command (const char *arg, int from_tty)
c906108c 10939{
06a64a0b 10940 watch_maybe_just_location (arg, hw_access, from_tty);
c906108c 10941}
c906108c 10942\f
c5aa993b 10943
cfc31633
PA
10944/* Data for the FSM that manages the until(location)/advance commands
10945 in infcmd.c. Here because it uses the mechanisms of
10946 breakpoints. */
c906108c 10947
46e3ed7f 10948struct until_break_fsm : public thread_fsm
bfec99b2 10949{
46e3ed7f 10950 /* The thread that was current when the command was executed. */
cfc31633
PA
10951 int thread;
10952
10953 /* The breakpoint set at the destination location. */
46e3ed7f 10954 breakpoint_up location_breakpoint;
cfc31633
PA
10955
10956 /* Breakpoint set at the return address in the caller frame. May be
10957 NULL. */
46e3ed7f 10958 breakpoint_up caller_breakpoint;
cfc31633 10959
46e3ed7f
TT
10960 until_break_fsm (struct interp *cmd_interp, int thread,
10961 breakpoint_up &&location_breakpoint,
10962 breakpoint_up &&caller_breakpoint)
10963 : thread_fsm (cmd_interp),
10964 thread (thread),
10965 location_breakpoint (std::move (location_breakpoint)),
10966 caller_breakpoint (std::move (caller_breakpoint))
10967 {
10968 }
cfc31633 10969
46e3ed7f
TT
10970 void clean_up (struct thread_info *thread) override;
10971 bool should_stop (struct thread_info *thread) override;
10972 enum async_reply_reason do_async_reply_reason () override;
cfc31633
PA
10973};
10974
cfc31633
PA
10975/* Implementation of the 'should_stop' FSM method for the
10976 until(location)/advance commands. */
10977
46e3ed7f
TT
10978bool
10979until_break_fsm::should_stop (struct thread_info *tp)
cfc31633 10980{
cfc31633 10981 if (bpstat_find_breakpoint (tp->control.stop_bpstat,
46e3ed7f
TT
10982 location_breakpoint.get ()) != NULL
10983 || (caller_breakpoint != NULL
cfc31633 10984 && bpstat_find_breakpoint (tp->control.stop_bpstat,
46e3ed7f
TT
10985 caller_breakpoint.get ()) != NULL))
10986 set_finished ();
cfc31633 10987
46e3ed7f 10988 return true;
cfc31633
PA
10989}
10990
10991/* Implementation of the 'clean_up' FSM method for the
10992 until(location)/advance commands. */
10993
46e3ed7f
TT
10994void
10995until_break_fsm::clean_up (struct thread_info *)
43ff13b4 10996{
cfc31633 10997 /* Clean up our temporary breakpoints. */
46e3ed7f
TT
10998 location_breakpoint.reset ();
10999 caller_breakpoint.reset ();
11000 delete_longjmp_breakpoint (thread);
cfc31633
PA
11001}
11002
11003/* Implementation of the 'async_reply_reason' FSM method for the
11004 until(location)/advance commands. */
11005
46e3ed7f
TT
11006enum async_reply_reason
11007until_break_fsm::do_async_reply_reason ()
cfc31633
PA
11008{
11009 return EXEC_ASYNC_LOCATION_REACHED;
43ff13b4
JM
11010}
11011
c906108c 11012void
f2fc3015 11013until_break_command (const char *arg, int from_tty, int anywhere)
c906108c 11014{
8556afb4
PA
11015 struct frame_info *frame;
11016 struct gdbarch *frame_gdbarch;
11017 struct frame_id stack_frame_id;
11018 struct frame_id caller_frame_id;
186c406b
TT
11019 int thread;
11020 struct thread_info *tp;
c906108c 11021
70509625 11022 clear_proceed_status (0);
c906108c
SS
11023
11024 /* Set a breakpoint where the user wants it and at return from
4a64f543 11025 this function. */
c5aa993b 11026
ffc2605c 11027 event_location_up location = string_to_event_location (&arg, current_language);
f00aae0f 11028
6c5b2ebe
PA
11029 std::vector<symtab_and_line> sals
11030 = (last_displayed_sal_is_valid ()
11031 ? decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
11032 get_last_displayed_symtab (),
11033 get_last_displayed_line ())
11034 : decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE,
cafb3438 11035 NULL, NULL, 0));
c5aa993b 11036
6c5b2ebe 11037 if (sals.size () != 1)
8a3fe4f8 11038 error (_("Couldn't get information on specified line."));
c5aa993b 11039
6c5b2ebe 11040 symtab_and_line &sal = sals[0];
c5aa993b 11041
c906108c 11042 if (*arg)
8a3fe4f8 11043 error (_("Junk at end of arguments."));
c5aa993b 11044
c906108c 11045 resolve_sal_pc (&sal);
c5aa993b 11046
186c406b 11047 tp = inferior_thread ();
5d5658a1 11048 thread = tp->global_num;
186c406b 11049
8556afb4
PA
11050 /* Note linespec handling above invalidates the frame chain.
11051 Installing a breakpoint also invalidates the frame chain (as it
11052 may need to switch threads), so do any frame handling before
11053 that. */
11054
11055 frame = get_selected_frame (NULL);
11056 frame_gdbarch = get_frame_arch (frame);
11057 stack_frame_id = get_stack_frame_id (frame);
11058 caller_frame_id = frame_unwind_caller_id (frame);
883bc8d1 11059
ae66c1fc
EZ
11060 /* Keep within the current frame, or in frames called by the current
11061 one. */
edb3359d 11062
454dafbd 11063 breakpoint_up caller_breakpoint;
5419bdae
TT
11064
11065 gdb::optional<delete_longjmp_breakpoint_cleanup> lj_deleter;
11066
883bc8d1 11067 if (frame_id_p (caller_frame_id))
c906108c 11068 {
883bc8d1 11069 struct symtab_and_line sal2;
cfc31633 11070 struct gdbarch *caller_gdbarch;
883bc8d1
PA
11071
11072 sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
11073 sal2.pc = frame_unwind_caller_pc (frame);
cfc31633
PA
11074 caller_gdbarch = frame_unwind_caller_arch (frame);
11075 caller_breakpoint = set_momentary_breakpoint (caller_gdbarch,
11076 sal2,
11077 caller_frame_id,
11078 bp_until);
186c406b 11079
883bc8d1 11080 set_longjmp_breakpoint (tp, caller_frame_id);
5419bdae 11081 lj_deleter.emplace (thread);
c906108c 11082 }
c5aa993b 11083
c70a6932
JK
11084 /* set_momentary_breakpoint could invalidate FRAME. */
11085 frame = NULL;
11086
454dafbd 11087 breakpoint_up location_breakpoint;
883bc8d1
PA
11088 if (anywhere)
11089 /* If the user told us to continue until a specified location,
11090 we don't specify a frame at which we need to stop. */
cfc31633
PA
11091 location_breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11092 null_frame_id, bp_until);
883bc8d1
PA
11093 else
11094 /* Otherwise, specify the selected frame, because we want to stop
11095 only at the very same frame. */
cfc31633
PA
11096 location_breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11097 stack_frame_id, bp_until);
883bc8d1 11098
46e3ed7f
TT
11099 tp->thread_fsm = new until_break_fsm (command_interp (), tp->global_num,
11100 std::move (location_breakpoint),
11101 std::move (caller_breakpoint));
f107f563 11102
5419bdae
TT
11103 if (lj_deleter)
11104 lj_deleter->release ();
f107f563 11105
cfc31633 11106 proceed (-1, GDB_SIGNAL_DEFAULT);
c906108c 11107}
ae66c1fc 11108
c906108c
SS
11109/* This function attempts to parse an optional "if <cond>" clause
11110 from the arg string. If one is not found, it returns NULL.
c5aa993b 11111
c906108c
SS
11112 Else, it returns a pointer to the condition string. (It does not
11113 attempt to evaluate the string against a particular block.) And,
11114 it updates arg to point to the first character following the parsed
4a64f543 11115 if clause in the arg string. */
53a5351d 11116
63160a43
PA
11117const char *
11118ep_parse_optional_if_clause (const char **arg)
c906108c 11119{
63160a43 11120 const char *cond_string;
c5aa993b
JM
11121
11122 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
c906108c 11123 return NULL;
c5aa993b 11124
4a64f543 11125 /* Skip the "if" keyword. */
c906108c 11126 (*arg) += 2;
c5aa993b 11127
c906108c 11128 /* Skip any extra leading whitespace, and record the start of the
4a64f543 11129 condition string. */
f1735a53 11130 *arg = skip_spaces (*arg);
c906108c 11131 cond_string = *arg;
c5aa993b 11132
4a64f543
MS
11133 /* Assume that the condition occupies the remainder of the arg
11134 string. */
c906108c 11135 (*arg) += strlen (cond_string);
c5aa993b 11136
c906108c
SS
11137 return cond_string;
11138}
c5aa993b 11139
c906108c
SS
11140/* Commands to deal with catching events, such as signals, exceptions,
11141 process start/exit, etc. */
c5aa993b
JM
11142
11143typedef enum
11144{
44feb3ce
TT
11145 catch_fork_temporary, catch_vfork_temporary,
11146 catch_fork_permanent, catch_vfork_permanent
c5aa993b
JM
11147}
11148catch_fork_kind;
11149
c906108c 11150static void
eb4c3f4a 11151catch_fork_command_1 (const char *arg, int from_tty,
cc59ec59 11152 struct cmd_list_element *command)
c906108c 11153{
a6d9a66e 11154 struct gdbarch *gdbarch = get_current_arch ();
63160a43 11155 const char *cond_string = NULL;
44feb3ce
TT
11156 catch_fork_kind fork_kind;
11157 int tempflag;
11158
11159 fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
11160 tempflag = (fork_kind == catch_fork_temporary
11161 || fork_kind == catch_vfork_temporary);
c5aa993b 11162
44feb3ce
TT
11163 if (!arg)
11164 arg = "";
f1735a53 11165 arg = skip_spaces (arg);
c5aa993b 11166
c906108c 11167 /* The allowed syntax is:
c5aa993b
JM
11168 catch [v]fork
11169 catch [v]fork if <cond>
11170
4a64f543 11171 First, check if there's an if clause. */
c906108c 11172 cond_string = ep_parse_optional_if_clause (&arg);
c5aa993b 11173
c906108c 11174 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 11175 error (_("Junk at end of arguments."));
c5aa993b 11176
c906108c 11177 /* If this target supports it, create a fork or vfork catchpoint
4a64f543 11178 and enable reporting of such events. */
c5aa993b
JM
11179 switch (fork_kind)
11180 {
44feb3ce
TT
11181 case catch_fork_temporary:
11182 case catch_fork_permanent:
a6d9a66e 11183 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
ce78b96d 11184 &catch_fork_breakpoint_ops);
c906108c 11185 break;
44feb3ce
TT
11186 case catch_vfork_temporary:
11187 case catch_vfork_permanent:
a6d9a66e 11188 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
ce78b96d 11189 &catch_vfork_breakpoint_ops);
c906108c 11190 break;
c5aa993b 11191 default:
8a3fe4f8 11192 error (_("unsupported or unknown fork kind; cannot catch it"));
c906108c 11193 break;
c5aa993b 11194 }
c906108c
SS
11195}
11196
11197static void
eb4c3f4a 11198catch_exec_command_1 (const char *arg, int from_tty,
cc59ec59 11199 struct cmd_list_element *command)
c906108c 11200{
a6d9a66e 11201 struct gdbarch *gdbarch = get_current_arch ();
44feb3ce 11202 int tempflag;
63160a43 11203 const char *cond_string = NULL;
c906108c 11204
44feb3ce
TT
11205 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
11206
11207 if (!arg)
11208 arg = "";
f1735a53 11209 arg = skip_spaces (arg);
c906108c
SS
11210
11211 /* The allowed syntax is:
c5aa993b
JM
11212 catch exec
11213 catch exec if <cond>
c906108c 11214
4a64f543 11215 First, check if there's an if clause. */
c906108c
SS
11216 cond_string = ep_parse_optional_if_clause (&arg);
11217
11218 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 11219 error (_("Junk at end of arguments."));
c906108c 11220
b270e6f9
TT
11221 std::unique_ptr<exec_catchpoint> c (new exec_catchpoint ());
11222 init_catchpoint (c.get (), gdbarch, tempflag, cond_string,
b4d90040
PA
11223 &catch_exec_breakpoint_ops);
11224 c->exec_pathname = NULL;
11225
b270e6f9 11226 install_breakpoint (0, std::move (c), 1);
c906108c 11227}
c5aa993b 11228
9ac4176b 11229void
28010a5d
PA
11230init_ada_exception_breakpoint (struct breakpoint *b,
11231 struct gdbarch *gdbarch,
11232 struct symtab_and_line sal,
f2fc3015 11233 const char *addr_string,
c0a91b2b 11234 const struct breakpoint_ops *ops,
28010a5d 11235 int tempflag,
349774ef 11236 int enabled,
28010a5d 11237 int from_tty)
f7f9143b 11238{
f7f9143b
JB
11239 if (from_tty)
11240 {
5af949e3
UW
11241 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
11242 if (!loc_gdbarch)
11243 loc_gdbarch = gdbarch;
11244
6c95b8df
PA
11245 describe_other_breakpoints (loc_gdbarch,
11246 sal.pspace, sal.pc, sal.section, -1);
f7f9143b
JB
11247 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
11248 version for exception catchpoints, because two catchpoints
11249 used for different exception names will use the same address.
11250 In this case, a "breakpoint ... also set at..." warning is
4a64f543 11251 unproductive. Besides, the warning phrasing is also a bit
e5dd4106 11252 inappropriate, we should use the word catchpoint, and tell
f7f9143b
JB
11253 the user what type of catchpoint it is. The above is good
11254 enough for now, though. */
11255 }
11256
f06f1252 11257 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
f7f9143b 11258
349774ef 11259 b->enable_state = enabled ? bp_enabled : bp_disabled;
f7f9143b 11260 b->disposition = tempflag ? disp_del : disp_donttouch;
d28cd78a
TT
11261 b->location = string_to_event_location (&addr_string,
11262 language_def (language_ada));
f7f9143b 11263 b->language = language_ada;
f7f9143b
JB
11264}
11265
c906108c
SS
11266\f
11267
81b1e71c 11268/* Compare two breakpoints and return a strcmp-like result. */
8a2c437b
TT
11269
11270static int
81b1e71c 11271compare_breakpoints (const breakpoint *a, const breakpoint *b)
8a2c437b 11272{
81b1e71c
TT
11273 uintptr_t ua = (uintptr_t) a;
11274 uintptr_t ub = (uintptr_t) b;
8a2c437b 11275
81b1e71c 11276 if (a->number < b->number)
8a2c437b 11277 return -1;
81b1e71c 11278 else if (a->number > b->number)
8a2c437b
TT
11279 return 1;
11280
11281 /* Now sort by address, in case we see, e..g, two breakpoints with
11282 the number 0. */
11283 if (ua < ub)
11284 return -1;
94b0e70d 11285 return ua > ub ? 1 : 0;
8a2c437b
TT
11286}
11287
80f8a6eb 11288/* Delete breakpoints by address or line. */
c906108c
SS
11289
11290static void
0b39b52e 11291clear_command (const char *arg, int from_tty)
c906108c 11292{
81b1e71c 11293 struct breakpoint *b;
c906108c 11294 int default_match;
c906108c 11295
6c5b2ebe
PA
11296 std::vector<symtab_and_line> decoded_sals;
11297 symtab_and_line last_sal;
11298 gdb::array_view<symtab_and_line> sals;
c906108c
SS
11299 if (arg)
11300 {
6c5b2ebe
PA
11301 decoded_sals
11302 = decode_line_with_current_source (arg,
11303 (DECODE_LINE_FUNFIRSTLINE
11304 | DECODE_LINE_LIST_MODE));
c906108c 11305 default_match = 0;
6c5b2ebe 11306 sals = decoded_sals;
c906108c
SS
11307 }
11308 else
11309 {
1bfeeb0f
JL
11310 /* Set sal's line, symtab, pc, and pspace to the values
11311 corresponding to the last call to print_frame_info. If the
11312 codepoint is not valid, this will set all the fields to 0. */
51abb421 11313 last_sal = get_last_displayed_sal ();
6c5b2ebe 11314 if (last_sal.symtab == 0)
8a3fe4f8 11315 error (_("No source file specified."));
c906108c 11316
c906108c 11317 default_match = 1;
6c5b2ebe 11318 sals = last_sal;
c906108c
SS
11319 }
11320
4a64f543
MS
11321 /* We don't call resolve_sal_pc here. That's not as bad as it
11322 seems, because all existing breakpoints typically have both
11323 file/line and pc set. So, if clear is given file/line, we can
11324 match this to existing breakpoint without obtaining pc at all.
ed0616c6
VP
11325
11326 We only support clearing given the address explicitly
11327 present in breakpoint table. Say, we've set breakpoint
4a64f543 11328 at file:line. There were several PC values for that file:line,
ed0616c6 11329 due to optimization, all in one block.
4a64f543
MS
11330
11331 We've picked one PC value. If "clear" is issued with another
ed0616c6
VP
11332 PC corresponding to the same file:line, the breakpoint won't
11333 be cleared. We probably can still clear the breakpoint, but
11334 since the other PC value is never presented to user, user
11335 can only find it by guessing, and it does not seem important
11336 to support that. */
11337
4a64f543
MS
11338 /* For each line spec given, delete bps which correspond to it. Do
11339 it in two passes, solely to preserve the current behavior that
11340 from_tty is forced true if we delete more than one
11341 breakpoint. */
c906108c 11342
81b1e71c 11343 std::vector<struct breakpoint *> found;
6c5b2ebe 11344 for (const auto &sal : sals)
c906108c 11345 {
05cba821
JK
11346 const char *sal_fullname;
11347
c906108c 11348 /* If exact pc given, clear bpts at that pc.
c5aa993b
JM
11349 If line given (pc == 0), clear all bpts on specified line.
11350 If defaulting, clear all bpts on default line
c906108c 11351 or at default pc.
c5aa993b
JM
11352
11353 defaulting sal.pc != 0 tests to do
11354
11355 0 1 pc
11356 1 1 pc _and_ line
11357 0 0 line
11358 1 0 <can't happen> */
c906108c 11359
05cba821
JK
11360 sal_fullname = (sal.symtab == NULL
11361 ? NULL : symtab_to_fullname (sal.symtab));
c906108c 11362
4a64f543 11363 /* Find all matching breakpoints and add them to 'found'. */
d6e956e5 11364 ALL_BREAKPOINTS (b)
c5aa993b 11365 {
0d381245 11366 int match = 0;
4a64f543 11367 /* Are we going to delete b? */
cc60f2e3 11368 if (b->type != bp_none && !is_watchpoint (b))
0d381245
VP
11369 {
11370 struct bp_location *loc = b->loc;
11371 for (; loc; loc = loc->next)
11372 {
f8eba3c6
TT
11373 /* If the user specified file:line, don't allow a PC
11374 match. This matches historical gdb behavior. */
11375 int pc_match = (!sal.explicit_line
11376 && sal.pc
11377 && (loc->pspace == sal.pspace)
11378 && (loc->address == sal.pc)
11379 && (!section_is_overlay (loc->section)
11380 || loc->section == sal.section));
4aac40c8
TT
11381 int line_match = 0;
11382
11383 if ((default_match || sal.explicit_line)
2f202fde 11384 && loc->symtab != NULL
05cba821 11385 && sal_fullname != NULL
4aac40c8 11386 && sal.pspace == loc->pspace
05cba821
JK
11387 && loc->line_number == sal.line
11388 && filename_cmp (symtab_to_fullname (loc->symtab),
11389 sal_fullname) == 0)
11390 line_match = 1;
4aac40c8 11391
0d381245
VP
11392 if (pc_match || line_match)
11393 {
11394 match = 1;
11395 break;
11396 }
11397 }
11398 }
11399
11400 if (match)
81b1e71c 11401 found.push_back (b);
c906108c 11402 }
80f8a6eb 11403 }
8a2c437b 11404
80f8a6eb 11405 /* Now go thru the 'found' chain and delete them. */
81b1e71c 11406 if (found.empty ())
80f8a6eb
MS
11407 {
11408 if (arg)
8a3fe4f8 11409 error (_("No breakpoint at %s."), arg);
80f8a6eb 11410 else
8a3fe4f8 11411 error (_("No breakpoint at this line."));
80f8a6eb 11412 }
c906108c 11413
8a2c437b 11414 /* Remove duplicates from the vec. */
81b1e71c 11415 std::sort (found.begin (), found.end (),
b926417a 11416 [] (const breakpoint *bp_a, const breakpoint *bp_b)
81b1e71c 11417 {
b926417a 11418 return compare_breakpoints (bp_a, bp_b) < 0;
81b1e71c
TT
11419 });
11420 found.erase (std::unique (found.begin (), found.end (),
b926417a 11421 [] (const breakpoint *bp_a, const breakpoint *bp_b)
81b1e71c 11422 {
b926417a 11423 return compare_breakpoints (bp_a, bp_b) == 0;
81b1e71c
TT
11424 }),
11425 found.end ());
8a2c437b 11426
81b1e71c 11427 if (found.size () > 1)
4a64f543 11428 from_tty = 1; /* Always report if deleted more than one. */
80f8a6eb 11429 if (from_tty)
a3f17187 11430 {
81b1e71c 11431 if (found.size () == 1)
a3f17187
AC
11432 printf_unfiltered (_("Deleted breakpoint "));
11433 else
11434 printf_unfiltered (_("Deleted breakpoints "));
11435 }
d6e956e5 11436
81b1e71c 11437 for (breakpoint *iter : found)
80f8a6eb 11438 {
c5aa993b 11439 if (from_tty)
81b1e71c
TT
11440 printf_unfiltered ("%d ", iter->number);
11441 delete_breakpoint (iter);
c906108c 11442 }
80f8a6eb
MS
11443 if (from_tty)
11444 putchar_unfiltered ('\n');
c906108c
SS
11445}
11446\f
11447/* Delete breakpoint in BS if they are `delete' breakpoints and
11448 all breakpoints that are marked for deletion, whether hit or not.
11449 This is called after any breakpoint is hit, or after errors. */
11450
11451void
fba45db2 11452breakpoint_auto_delete (bpstat bs)
c906108c 11453{
35df4500 11454 struct breakpoint *b, *b_tmp;
c906108c
SS
11455
11456 for (; bs; bs = bs->next)
f431efe5
PA
11457 if (bs->breakpoint_at
11458 && bs->breakpoint_at->disposition == disp_del
c906108c 11459 && bs->stop)
f431efe5 11460 delete_breakpoint (bs->breakpoint_at);
c906108c 11461
35df4500 11462 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 11463 {
b5de0fa7 11464 if (b->disposition == disp_del_at_next_stop)
c5aa993b
JM
11465 delete_breakpoint (b);
11466 }
c906108c
SS
11467}
11468
4a64f543 11469/* A comparison function for bp_location AP and BP being interfaced to
39ef2f62 11470 std::sort. Sort elements primarily by their ADDRESS (no matter what
cb1e4e32
PA
11471 bl_address_is_meaningful says), secondarily by ordering first
11472 permanent elements and terciarily just ensuring the array is sorted
39ef2f62 11473 stable way despite std::sort being an unstable algorithm. */
876fa593
JK
11474
11475static int
39ef2f62 11476bp_location_is_less_than (const bp_location *a, const bp_location *b)
876fa593 11477{
876fa593 11478 if (a->address != b->address)
39ef2f62 11479 return a->address < b->address;
876fa593 11480
dea2aa5f
LM
11481 /* Sort locations at the same address by their pspace number, keeping
11482 locations of the same inferior (in a multi-inferior environment)
11483 grouped. */
11484
11485 if (a->pspace->num != b->pspace->num)
39ef2f62 11486 return a->pspace->num < b->pspace->num;
dea2aa5f 11487
876fa593 11488 /* Sort permanent breakpoints first. */
1a853c52 11489 if (a->permanent != b->permanent)
39ef2f62 11490 return a->permanent > b->permanent;
876fa593 11491
7f32a4d5
PA
11492 /* Sort by type in order to make duplicate determination easier.
11493 See update_global_location_list. This is kept in sync with
11494 breakpoint_locations_match. */
11495 if (a->loc_type < b->loc_type)
11496 return true;
11497
11498 /* Likewise, for range-breakpoints, sort by length. */
11499 if (a->loc_type == bp_loc_hardware_breakpoint
11500 && b->loc_type == bp_loc_hardware_breakpoint
11501 && a->length < b->length)
11502 return true;
11503
c56a97f9
JK
11504 /* Make the internal GDB representation stable across GDB runs
11505 where A and B memory inside GDB can differ. Breakpoint locations of
11506 the same type at the same address can be sorted in arbitrary order. */
876fa593
JK
11507
11508 if (a->owner->number != b->owner->number)
39ef2f62 11509 return a->owner->number < b->owner->number;
876fa593 11510
39ef2f62 11511 return a < b;
876fa593
JK
11512}
11513
f5336ca5
PA
11514/* Set bp_locations_placed_address_before_address_max and
11515 bp_locations_shadow_len_after_address_max according to the current
11516 content of the bp_locations array. */
f7545552
TT
11517
11518static void
f5336ca5 11519bp_locations_target_extensions_update (void)
f7545552 11520{
876fa593
JK
11521 struct bp_location *bl, **blp_tmp;
11522
f5336ca5
PA
11523 bp_locations_placed_address_before_address_max = 0;
11524 bp_locations_shadow_len_after_address_max = 0;
876fa593
JK
11525
11526 ALL_BP_LOCATIONS (bl, blp_tmp)
11527 {
11528 CORE_ADDR start, end, addr;
11529
11530 if (!bp_location_has_shadow (bl))
11531 continue;
11532
11533 start = bl->target_info.placed_address;
11534 end = start + bl->target_info.shadow_len;
11535
11536 gdb_assert (bl->address >= start);
11537 addr = bl->address - start;
f5336ca5
PA
11538 if (addr > bp_locations_placed_address_before_address_max)
11539 bp_locations_placed_address_before_address_max = addr;
876fa593
JK
11540
11541 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
11542
11543 gdb_assert (bl->address < end);
11544 addr = end - bl->address;
f5336ca5
PA
11545 if (addr > bp_locations_shadow_len_after_address_max)
11546 bp_locations_shadow_len_after_address_max = addr;
876fa593 11547 }
f7545552
TT
11548}
11549
1e4d1764
YQ
11550/* Download tracepoint locations if they haven't been. */
11551
11552static void
11553download_tracepoint_locations (void)
11554{
7ed2c994 11555 struct breakpoint *b;
dd2e65cc 11556 enum tribool can_download_tracepoint = TRIBOOL_UNKNOWN;
1e4d1764 11557
5ed8105e 11558 scoped_restore_current_pspace_and_thread restore_pspace_thread;
1e4d1764 11559
7ed2c994 11560 ALL_TRACEPOINTS (b)
1e4d1764 11561 {
7ed2c994 11562 struct bp_location *bl;
1e4d1764 11563 struct tracepoint *t;
f2a8bc8a 11564 int bp_location_downloaded = 0;
1e4d1764 11565
7ed2c994 11566 if ((b->type == bp_fast_tracepoint
1e4d1764
YQ
11567 ? !may_insert_fast_tracepoints
11568 : !may_insert_tracepoints))
11569 continue;
11570
dd2e65cc
YQ
11571 if (can_download_tracepoint == TRIBOOL_UNKNOWN)
11572 {
11573 if (target_can_download_tracepoint ())
11574 can_download_tracepoint = TRIBOOL_TRUE;
11575 else
11576 can_download_tracepoint = TRIBOOL_FALSE;
11577 }
11578
11579 if (can_download_tracepoint == TRIBOOL_FALSE)
11580 break;
11581
7ed2c994
YQ
11582 for (bl = b->loc; bl; bl = bl->next)
11583 {
11584 /* In tracepoint, locations are _never_ duplicated, so
11585 should_be_inserted is equivalent to
11586 unduplicated_should_be_inserted. */
11587 if (!should_be_inserted (bl) || bl->inserted)
11588 continue;
1e4d1764 11589
7ed2c994 11590 switch_to_program_space_and_thread (bl->pspace);
1e4d1764 11591
7ed2c994 11592 target_download_tracepoint (bl);
1e4d1764 11593
7ed2c994 11594 bl->inserted = 1;
f2a8bc8a 11595 bp_location_downloaded = 1;
7ed2c994
YQ
11596 }
11597 t = (struct tracepoint *) b;
11598 t->number_on_target = b->number;
f2a8bc8a 11599 if (bp_location_downloaded)
76727919 11600 gdb::observers::breakpoint_modified.notify (b);
1e4d1764 11601 }
1e4d1764
YQ
11602}
11603
934709f0
PW
11604/* Swap the insertion/duplication state between two locations. */
11605
11606static void
11607swap_insertion (struct bp_location *left, struct bp_location *right)
11608{
11609 const int left_inserted = left->inserted;
11610 const int left_duplicate = left->duplicate;
b775012e 11611 const int left_needs_update = left->needs_update;
934709f0
PW
11612 const struct bp_target_info left_target_info = left->target_info;
11613
1e4d1764
YQ
11614 /* Locations of tracepoints can never be duplicated. */
11615 if (is_tracepoint (left->owner))
11616 gdb_assert (!left->duplicate);
11617 if (is_tracepoint (right->owner))
11618 gdb_assert (!right->duplicate);
11619
934709f0
PW
11620 left->inserted = right->inserted;
11621 left->duplicate = right->duplicate;
b775012e 11622 left->needs_update = right->needs_update;
934709f0
PW
11623 left->target_info = right->target_info;
11624 right->inserted = left_inserted;
11625 right->duplicate = left_duplicate;
b775012e 11626 right->needs_update = left_needs_update;
934709f0
PW
11627 right->target_info = left_target_info;
11628}
11629
b775012e
LM
11630/* Force the re-insertion of the locations at ADDRESS. This is called
11631 once a new/deleted/modified duplicate location is found and we are evaluating
11632 conditions on the target's side. Such conditions need to be updated on
11633 the target. */
11634
11635static void
11636force_breakpoint_reinsertion (struct bp_location *bl)
11637{
11638 struct bp_location **locp = NULL, **loc2p;
11639 struct bp_location *loc;
11640 CORE_ADDR address = 0;
11641 int pspace_num;
11642
11643 address = bl->address;
11644 pspace_num = bl->pspace->num;
11645
11646 /* This is only meaningful if the target is
11647 evaluating conditions and if the user has
11648 opted for condition evaluation on the target's
11649 side. */
11650 if (gdb_evaluates_breakpoint_condition_p ()
11651 || !target_supports_evaluation_of_breakpoint_conditions ())
11652 return;
11653
11654 /* Flag all breakpoint locations with this address and
11655 the same program space as the location
11656 as "its condition has changed". We need to
11657 update the conditions on the target's side. */
11658 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, address)
11659 {
11660 loc = *loc2p;
11661
11662 if (!is_breakpoint (loc->owner)
11663 || pspace_num != loc->pspace->num)
11664 continue;
11665
11666 /* Flag the location appropriately. We use a different state to
11667 let everyone know that we already updated the set of locations
11668 with addr bl->address and program space bl->pspace. This is so
11669 we don't have to keep calling these functions just to mark locations
11670 that have already been marked. */
11671 loc->condition_changed = condition_updated;
11672
11673 /* Free the agent expression bytecode as well. We will compute
11674 it later on. */
833177a4 11675 loc->cond_bytecode.reset ();
b775012e
LM
11676 }
11677}
7f32a4d5 11678
44702360
PA
11679/* Called whether new breakpoints are created, or existing breakpoints
11680 deleted, to update the global location list and recompute which
11681 locations are duplicate of which.
b775012e 11682
04086b45
PA
11683 The INSERT_MODE flag determines whether locations may not, may, or
11684 shall be inserted now. See 'enum ugll_insert_mode' for more
11685 info. */
b60e7edf 11686
0d381245 11687static void
44702360 11688update_global_location_list (enum ugll_insert_mode insert_mode)
0d381245 11689{
74960c60 11690 struct breakpoint *b;
876fa593 11691 struct bp_location **locp, *loc;
b775012e
LM
11692 /* Last breakpoint location address that was marked for update. */
11693 CORE_ADDR last_addr = 0;
11694 /* Last breakpoint location program space that was marked for update. */
11695 int last_pspace_num = -1;
f7545552 11696
2d134ed3
PA
11697 /* Used in the duplicates detection below. When iterating over all
11698 bp_locations, points to the first bp_location of a given address.
11699 Breakpoints and watchpoints of different types are never
11700 duplicates of each other. Keep one pointer for each type of
11701 breakpoint/watchpoint, so we only need to loop over all locations
11702 once. */
11703 struct bp_location *bp_loc_first; /* breakpoint */
11704 struct bp_location *wp_loc_first; /* hardware watchpoint */
11705 struct bp_location *awp_loc_first; /* access watchpoint */
11706 struct bp_location *rwp_loc_first; /* read watchpoint */
876fa593 11707
f5336ca5
PA
11708 /* Saved former bp_locations array which we compare against the newly
11709 built bp_locations from the current state of ALL_BREAKPOINTS. */
81b1e71c 11710 struct bp_location **old_locp;
f5336ca5 11711 unsigned old_locations_count;
81b1e71c 11712 gdb::unique_xmalloc_ptr<struct bp_location *> old_locations (bp_locations);
876fa593 11713
f5336ca5
PA
11714 old_locations_count = bp_locations_count;
11715 bp_locations = NULL;
11716 bp_locations_count = 0;
0d381245 11717
74960c60 11718 ALL_BREAKPOINTS (b)
876fa593 11719 for (loc = b->loc; loc; loc = loc->next)
f5336ca5 11720 bp_locations_count++;
876fa593 11721
f5336ca5
PA
11722 bp_locations = XNEWVEC (struct bp_location *, bp_locations_count);
11723 locp = bp_locations;
876fa593
JK
11724 ALL_BREAKPOINTS (b)
11725 for (loc = b->loc; loc; loc = loc->next)
11726 *locp++ = loc;
7f32a4d5
PA
11727
11728 /* See if we need to "upgrade" a software breakpoint to a hardware
11729 breakpoint. Do this before deciding whether locations are
11730 duplicates. Also do this before sorting because sorting order
11731 depends on location type. */
11732 for (locp = bp_locations;
11733 locp < bp_locations + bp_locations_count;
11734 locp++)
11735 {
11736 loc = *locp;
11737 if (!loc->inserted && should_be_inserted (loc))
11738 handle_automatic_hardware_breakpoints (loc);
11739 }
11740
39ef2f62
CB
11741 std::sort (bp_locations, bp_locations + bp_locations_count,
11742 bp_location_is_less_than);
876fa593 11743
f5336ca5 11744 bp_locations_target_extensions_update ();
74960c60 11745
4a64f543
MS
11746 /* Identify bp_location instances that are no longer present in the
11747 new list, and therefore should be freed. Note that it's not
11748 necessary that those locations should be removed from inferior --
11749 if there's another location at the same address (previously
11750 marked as duplicate), we don't need to remove/insert the
11751 location.
876fa593 11752
4a64f543
MS
11753 LOCP is kept in sync with OLD_LOCP, each pointing to the current
11754 and former bp_location array state respectively. */
876fa593 11755
f5336ca5 11756 locp = bp_locations;
81b1e71c
TT
11757 for (old_locp = old_locations.get ();
11758 old_locp < old_locations.get () + old_locations_count;
876fa593 11759 old_locp++)
74960c60 11760 {
876fa593 11761 struct bp_location *old_loc = *old_locp;
c7d46a38 11762 struct bp_location **loc2p;
876fa593 11763
e5dd4106 11764 /* Tells if 'old_loc' is found among the new locations. If
4a64f543 11765 not, we have to free it. */
c7d46a38 11766 int found_object = 0;
20874c92
VP
11767 /* Tells if the location should remain inserted in the target. */
11768 int keep_in_target = 0;
11769 int removed = 0;
876fa593 11770
4a64f543
MS
11771 /* Skip LOCP entries which will definitely never be needed.
11772 Stop either at or being the one matching OLD_LOC. */
f5336ca5 11773 while (locp < bp_locations + bp_locations_count
c7d46a38 11774 && (*locp)->address < old_loc->address)
876fa593 11775 locp++;
c7d46a38
PA
11776
11777 for (loc2p = locp;
f5336ca5 11778 (loc2p < bp_locations + bp_locations_count
c7d46a38
PA
11779 && (*loc2p)->address == old_loc->address);
11780 loc2p++)
11781 {
b775012e
LM
11782 /* Check if this is a new/duplicated location or a duplicated
11783 location that had its condition modified. If so, we want to send
11784 its condition to the target if evaluation of conditions is taking
11785 place there. */
11786 if ((*loc2p)->condition_changed == condition_modified
11787 && (last_addr != old_loc->address
11788 || last_pspace_num != old_loc->pspace->num))
c7d46a38 11789 {
b775012e
LM
11790 force_breakpoint_reinsertion (*loc2p);
11791 last_pspace_num = old_loc->pspace->num;
c7d46a38 11792 }
b775012e
LM
11793
11794 if (*loc2p == old_loc)
11795 found_object = 1;
c7d46a38 11796 }
74960c60 11797
b775012e
LM
11798 /* We have already handled this address, update it so that we don't
11799 have to go through updates again. */
11800 last_addr = old_loc->address;
11801
11802 /* Target-side condition evaluation: Handle deleted locations. */
11803 if (!found_object)
11804 force_breakpoint_reinsertion (old_loc);
11805
4a64f543
MS
11806 /* If this location is no longer present, and inserted, look if
11807 there's maybe a new location at the same address. If so,
11808 mark that one inserted, and don't remove this one. This is
11809 needed so that we don't have a time window where a breakpoint
11810 at certain location is not inserted. */
74960c60 11811
876fa593 11812 if (old_loc->inserted)
0d381245 11813 {
4a64f543
MS
11814 /* If the location is inserted now, we might have to remove
11815 it. */
74960c60 11816
876fa593 11817 if (found_object && should_be_inserted (old_loc))
74960c60 11818 {
4a64f543
MS
11819 /* The location is still present in the location list,
11820 and still should be inserted. Don't do anything. */
20874c92 11821 keep_in_target = 1;
74960c60
VP
11822 }
11823 else
11824 {
b775012e
LM
11825 /* This location still exists, but it won't be kept in the
11826 target since it may have been disabled. We proceed to
11827 remove its target-side condition. */
11828
4a64f543
MS
11829 /* The location is either no longer present, or got
11830 disabled. See if there's another location at the
11831 same address, in which case we don't need to remove
11832 this one from the target. */
876fa593 11833
2bdf28a0 11834 /* OLD_LOC comes from existing struct breakpoint. */
cb1e4e32 11835 if (bl_address_is_meaningful (old_loc))
876fa593 11836 {
876fa593 11837 for (loc2p = locp;
f5336ca5 11838 (loc2p < bp_locations + bp_locations_count
c7d46a38 11839 && (*loc2p)->address == old_loc->address);
876fa593
JK
11840 loc2p++)
11841 {
11842 struct bp_location *loc2 = *loc2p;
11843
7f32a4d5
PA
11844 if (loc2 == old_loc)
11845 continue;
11846
2d134ed3 11847 if (breakpoint_locations_match (loc2, old_loc))
c7d46a38 11848 {
85d721b8
PA
11849 /* Read watchpoint locations are switched to
11850 access watchpoints, if the former are not
11851 supported, but the latter are. */
11852 if (is_hardware_watchpoint (old_loc->owner))
11853 {
11854 gdb_assert (is_hardware_watchpoint (loc2->owner));
11855 loc2->watchpoint_type = old_loc->watchpoint_type;
11856 }
11857
934709f0
PW
11858 /* loc2 is a duplicated location. We need to check
11859 if it should be inserted in case it will be
11860 unduplicated. */
7f32a4d5 11861 if (unduplicated_should_be_inserted (loc2))
c7d46a38 11862 {
934709f0 11863 swap_insertion (old_loc, loc2);
c7d46a38
PA
11864 keep_in_target = 1;
11865 break;
11866 }
876fa593
JK
11867 }
11868 }
11869 }
74960c60
VP
11870 }
11871
20874c92
VP
11872 if (!keep_in_target)
11873 {
834c0d03 11874 if (remove_breakpoint (old_loc))
20874c92 11875 {
4a64f543
MS
11876 /* This is just about all we can do. We could keep
11877 this location on the global list, and try to
11878 remove it next time, but there's no particular
11879 reason why we will succeed next time.
20874c92 11880
4a64f543
MS
11881 Note that at this point, old_loc->owner is still
11882 valid, as delete_breakpoint frees the breakpoint
11883 only after calling us. */
3e43a32a
MS
11884 printf_filtered (_("warning: Error removing "
11885 "breakpoint %d\n"),
876fa593 11886 old_loc->owner->number);
20874c92
VP
11887 }
11888 removed = 1;
11889 }
0d381245 11890 }
74960c60
VP
11891
11892 if (!found_object)
1c5cfe86 11893 {
fbea99ea 11894 if (removed && target_is_non_stop_p ()
1cf4d951 11895 && need_moribund_for_location_type (old_loc))
20874c92 11896 {
db82e815
PA
11897 /* This location was removed from the target. In
11898 non-stop mode, a race condition is possible where
11899 we've removed a breakpoint, but stop events for that
11900 breakpoint are already queued and will arrive later.
11901 We apply an heuristic to be able to distinguish such
11902 SIGTRAPs from other random SIGTRAPs: we keep this
11903 breakpoint location for a bit, and will retire it
11904 after we see some number of events. The theory here
11905 is that reporting of events should, "on the average",
11906 be fair, so after a while we'll see events from all
11907 threads that have anything of interest, and no longer
11908 need to keep this breakpoint location around. We
11909 don't hold locations forever so to reduce chances of
11910 mistaking a non-breakpoint SIGTRAP for a breakpoint
11911 SIGTRAP.
11912
11913 The heuristic failing can be disastrous on
11914 decr_pc_after_break targets.
11915
11916 On decr_pc_after_break targets, like e.g., x86-linux,
11917 if we fail to recognize a late breakpoint SIGTRAP,
11918 because events_till_retirement has reached 0 too
11919 soon, we'll fail to do the PC adjustment, and report
11920 a random SIGTRAP to the user. When the user resumes
11921 the inferior, it will most likely immediately crash
2dec564e 11922 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
db82e815
PA
11923 corrupted, because of being resumed e.g., in the
11924 middle of a multi-byte instruction, or skipped a
11925 one-byte instruction. This was actually seen happen
11926 on native x86-linux, and should be less rare on
11927 targets that do not support new thread events, like
11928 remote, due to the heuristic depending on
11929 thread_count.
11930
11931 Mistaking a random SIGTRAP for a breakpoint trap
11932 causes similar symptoms (PC adjustment applied when
11933 it shouldn't), but then again, playing with SIGTRAPs
11934 behind the debugger's back is asking for trouble.
11935
11936 Since hardware watchpoint traps are always
11937 distinguishable from other traps, so we don't need to
11938 apply keep hardware watchpoint moribund locations
11939 around. We simply always ignore hardware watchpoint
11940 traps we can no longer explain. */
11941
5b6d1e4f
PA
11942 process_stratum_target *proc_target = nullptr;
11943 for (inferior *inf : all_inferiors ())
11944 if (inf->pspace == old_loc->pspace)
11945 {
11946 proc_target = inf->process_target ();
11947 break;
11948 }
11949 if (proc_target != nullptr)
11950 old_loc->events_till_retirement
11951 = 3 * (thread_count (proc_target) + 1);
11952 else
11953 old_loc->events_till_retirement = 1;
876fa593 11954 old_loc->owner = NULL;
20874c92 11955
1123588c 11956 moribund_locations.push_back (old_loc);
1c5cfe86
PA
11957 }
11958 else
f431efe5
PA
11959 {
11960 old_loc->owner = NULL;
11961 decref_bp_location (&old_loc);
11962 }
20874c92 11963 }
74960c60 11964 }
1c5cfe86 11965
348d480f
PA
11966 /* Rescan breakpoints at the same address and section, marking the
11967 first one as "first" and any others as "duplicates". This is so
11968 that the bpt instruction is only inserted once. If we have a
11969 permanent breakpoint at the same place as BPT, make that one the
11970 official one, and the rest as duplicates. Permanent breakpoints
11971 are sorted first for the same address.
11972
11973 Do the same for hardware watchpoints, but also considering the
11974 watchpoint's type (regular/access/read) and length. */
11975
11976 bp_loc_first = NULL;
11977 wp_loc_first = NULL;
11978 awp_loc_first = NULL;
11979 rwp_loc_first = NULL;
11980 ALL_BP_LOCATIONS (loc, locp)
11981 {
11982 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
11983 non-NULL. */
348d480f 11984 struct bp_location **loc_first_p;
d3fbdd86 11985 b = loc->owner;
348d480f 11986
6f380991 11987 if (!unduplicated_should_be_inserted (loc)
cb1e4e32 11988 || !bl_address_is_meaningful (loc)
1e4d1764
YQ
11989 /* Don't detect duplicate for tracepoint locations because they are
11990 never duplicated. See the comments in field `duplicate' of
11991 `struct bp_location'. */
348d480f 11992 || is_tracepoint (b))
b775012e
LM
11993 {
11994 /* Clear the condition modification flag. */
11995 loc->condition_changed = condition_unchanged;
11996 continue;
11997 }
348d480f 11998
348d480f
PA
11999 if (b->type == bp_hardware_watchpoint)
12000 loc_first_p = &wp_loc_first;
12001 else if (b->type == bp_read_watchpoint)
12002 loc_first_p = &rwp_loc_first;
12003 else if (b->type == bp_access_watchpoint)
12004 loc_first_p = &awp_loc_first;
12005 else
12006 loc_first_p = &bp_loc_first;
12007
12008 if (*loc_first_p == NULL
12009 || (overlay_debugging && loc->section != (*loc_first_p)->section)
12010 || !breakpoint_locations_match (loc, *loc_first_p))
12011 {
12012 *loc_first_p = loc;
12013 loc->duplicate = 0;
b775012e
LM
12014
12015 if (is_breakpoint (loc->owner) && loc->condition_changed)
12016 {
12017 loc->needs_update = 1;
12018 /* Clear the condition modification flag. */
12019 loc->condition_changed = condition_unchanged;
12020 }
348d480f
PA
12021 continue;
12022 }
12023
934709f0
PW
12024
12025 /* This and the above ensure the invariant that the first location
12026 is not duplicated, and is the inserted one.
12027 All following are marked as duplicated, and are not inserted. */
12028 if (loc->inserted)
12029 swap_insertion (loc, *loc_first_p);
348d480f
PA
12030 loc->duplicate = 1;
12031
b775012e
LM
12032 /* Clear the condition modification flag. */
12033 loc->condition_changed = condition_unchanged;
348d480f
PA
12034 }
12035
a25a5a45 12036 if (insert_mode == UGLL_INSERT || breakpoints_should_be_inserted_now ())
b775012e 12037 {
04086b45 12038 if (insert_mode != UGLL_DONT_INSERT)
b775012e
LM
12039 insert_breakpoint_locations ();
12040 else
12041 {
44702360
PA
12042 /* Even though the caller told us to not insert new
12043 locations, we may still need to update conditions on the
12044 target's side of breakpoints that were already inserted
12045 if the target is evaluating breakpoint conditions. We
b775012e
LM
12046 only update conditions for locations that are marked
12047 "needs_update". */
12048 update_inserted_breakpoint_locations ();
12049 }
12050 }
348d480f 12051
04086b45 12052 if (insert_mode != UGLL_DONT_INSERT)
1e4d1764 12053 download_tracepoint_locations ();
348d480f
PA
12054}
12055
12056void
12057breakpoint_retire_moribund (void)
12058{
1123588c
TT
12059 for (int ix = 0; ix < moribund_locations.size (); ++ix)
12060 {
12061 struct bp_location *loc = moribund_locations[ix];
12062 if (--(loc->events_till_retirement) == 0)
12063 {
12064 decref_bp_location (&loc);
12065 unordered_remove (moribund_locations, ix);
12066 --ix;
12067 }
12068 }
348d480f
PA
12069}
12070
12071static void
44702360 12072update_global_location_list_nothrow (enum ugll_insert_mode insert_mode)
348d480f 12073{
348d480f 12074
a70b8144 12075 try
492d29ea
PA
12076 {
12077 update_global_location_list (insert_mode);
12078 }
230d2906 12079 catch (const gdb_exception_error &e)
492d29ea
PA
12080 {
12081 }
348d480f
PA
12082}
12083
12084/* Clear BKP from a BPS. */
12085
12086static void
12087bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
12088{
12089 bpstat bs;
12090
12091 for (bs = bps; bs; bs = bs->next)
12092 if (bs->breakpoint_at == bpt)
12093 {
12094 bs->breakpoint_at = NULL;
12095 bs->old_val = NULL;
12096 /* bs->commands will be freed later. */
12097 }
12098}
12099
12100/* Callback for iterate_over_threads. */
12101static int
12102bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
12103{
9a3c8263 12104 struct breakpoint *bpt = (struct breakpoint *) data;
348d480f
PA
12105
12106 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
12107 return 0;
12108}
12109
12110/* Helper for breakpoint and tracepoint breakpoint_ops->mention
12111 callbacks. */
12112
12113static void
12114say_where (struct breakpoint *b)
12115{
12116 struct value_print_options opts;
12117
12118 get_user_print_options (&opts);
12119
12120 /* i18n: cagney/2005-02-11: Below needs to be merged into a
12121 single string. */
12122 if (b->loc == NULL)
12123 {
f00aae0f
KS
12124 /* For pending locations, the output differs slightly based
12125 on b->extra_string. If this is non-NULL, it contains either
12126 a condition or dprintf arguments. */
12127 if (b->extra_string == NULL)
12128 {
12129 printf_filtered (_(" (%s) pending."),
d28cd78a 12130 event_location_to_string (b->location.get ()));
f00aae0f
KS
12131 }
12132 else if (b->type == bp_dprintf)
12133 {
12134 printf_filtered (_(" (%s,%s) pending."),
d28cd78a 12135 event_location_to_string (b->location.get ()),
f00aae0f
KS
12136 b->extra_string);
12137 }
12138 else
12139 {
12140 printf_filtered (_(" (%s %s) pending."),
d28cd78a 12141 event_location_to_string (b->location.get ()),
f00aae0f
KS
12142 b->extra_string);
12143 }
348d480f
PA
12144 }
12145 else
12146 {
2f202fde 12147 if (opts.addressprint || b->loc->symtab == NULL)
6a831f06
PA
12148 printf_filtered (" at %ps",
12149 styled_string (address_style.style (),
12150 paddress (b->loc->gdbarch,
12151 b->loc->address)));
2f202fde 12152 if (b->loc->symtab != NULL)
f8eba3c6
TT
12153 {
12154 /* If there is a single location, we can print the location
12155 more nicely. */
12156 if (b->loc->next == NULL)
0bb296cb 12157 {
6a831f06
PA
12158 const char *filename
12159 = symtab_to_filename_for_display (b->loc->symtab);
12160 printf_filtered (": file %ps, line %d.",
12161 styled_string (file_name_style.style (),
12162 filename),
0bb296cb
TT
12163 b->loc->line_number);
12164 }
f8eba3c6
TT
12165 else
12166 /* This is not ideal, but each location may have a
12167 different file name, and this at least reflects the
12168 real situation somewhat. */
f00aae0f 12169 printf_filtered (": %s.",
d28cd78a 12170 event_location_to_string (b->location.get ()));
f8eba3c6 12171 }
348d480f
PA
12172
12173 if (b->loc->next)
12174 {
12175 struct bp_location *loc = b->loc;
12176 int n = 0;
12177 for (; loc; loc = loc->next)
12178 ++n;
12179 printf_filtered (" (%d locations)", n);
12180 }
12181 }
12182}
12183
5f486660 12184bp_location::~bp_location ()
348d480f 12185{
5f486660 12186 xfree (function_name);
348d480f
PA
12187}
12188
c1fc2657 12189/* Destructor for the breakpoint base class. */
348d480f 12190
c1fc2657 12191breakpoint::~breakpoint ()
348d480f 12192{
c1fc2657
SM
12193 xfree (this->cond_string);
12194 xfree (this->extra_string);
348d480f
PA
12195}
12196
2060206e
PA
12197static struct bp_location *
12198base_breakpoint_allocate_location (struct breakpoint *self)
348d480f 12199{
5f486660 12200 return new bp_location (self);
348d480f
PA
12201}
12202
2060206e
PA
12203static void
12204base_breakpoint_re_set (struct breakpoint *b)
12205{
12206 /* Nothing to re-set. */
12207}
12208
12209#define internal_error_pure_virtual_called() \
12210 gdb_assert_not_reached ("pure virtual function called")
12211
12212static int
12213base_breakpoint_insert_location (struct bp_location *bl)
12214{
12215 internal_error_pure_virtual_called ();
12216}
12217
12218static int
73971819
PA
12219base_breakpoint_remove_location (struct bp_location *bl,
12220 enum remove_bp_reason reason)
2060206e
PA
12221{
12222 internal_error_pure_virtual_called ();
12223}
12224
12225static int
12226base_breakpoint_breakpoint_hit (const struct bp_location *bl,
bd522513 12227 const address_space *aspace,
09ac7c10
TT
12228 CORE_ADDR bp_addr,
12229 const struct target_waitstatus *ws)
2060206e
PA
12230{
12231 internal_error_pure_virtual_called ();
12232}
12233
12234static void
12235base_breakpoint_check_status (bpstat bs)
12236{
12237 /* Always stop. */
12238}
12239
12240/* A "works_in_software_mode" breakpoint_ops method that just internal
12241 errors. */
12242
12243static int
12244base_breakpoint_works_in_software_mode (const struct breakpoint *b)
12245{
12246 internal_error_pure_virtual_called ();
12247}
12248
12249/* A "resources_needed" breakpoint_ops method that just internal
12250 errors. */
12251
12252static int
12253base_breakpoint_resources_needed (const struct bp_location *bl)
12254{
12255 internal_error_pure_virtual_called ();
12256}
12257
12258static enum print_stop_action
12259base_breakpoint_print_it (bpstat bs)
12260{
12261 internal_error_pure_virtual_called ();
12262}
12263
12264static void
12265base_breakpoint_print_one_detail (const struct breakpoint *self,
12266 struct ui_out *uiout)
12267{
12268 /* nothing */
12269}
12270
12271static void
12272base_breakpoint_print_mention (struct breakpoint *b)
12273{
12274 internal_error_pure_virtual_called ();
12275}
12276
12277static void
12278base_breakpoint_print_recreate (struct breakpoint *b, struct ui_file *fp)
12279{
12280 internal_error_pure_virtual_called ();
12281}
12282
983af33b 12283static void
f00aae0f
KS
12284base_breakpoint_create_sals_from_location
12285 (const struct event_location *location,
12286 struct linespec_result *canonical,
12287 enum bptype type_wanted)
983af33b
SDJ
12288{
12289 internal_error_pure_virtual_called ();
12290}
12291
12292static void
12293base_breakpoint_create_breakpoints_sal (struct gdbarch *gdbarch,
12294 struct linespec_result *c,
e1e01040
PA
12295 gdb::unique_xmalloc_ptr<char> cond_string,
12296 gdb::unique_xmalloc_ptr<char> extra_string,
983af33b
SDJ
12297 enum bptype type_wanted,
12298 enum bpdisp disposition,
12299 int thread,
12300 int task, int ignore_count,
12301 const struct breakpoint_ops *o,
12302 int from_tty, int enabled,
44f238bb 12303 int internal, unsigned flags)
983af33b
SDJ
12304{
12305 internal_error_pure_virtual_called ();
12306}
12307
6c5b2ebe 12308static std::vector<symtab_and_line>
f00aae0f
KS
12309base_breakpoint_decode_location (struct breakpoint *b,
12310 const struct event_location *location,
6c5b2ebe 12311 struct program_space *search_pspace)
983af33b
SDJ
12312{
12313 internal_error_pure_virtual_called ();
12314}
12315
ab04a2af
TT
12316/* The default 'explains_signal' method. */
12317
47591c29 12318static int
427cd150 12319base_breakpoint_explains_signal (struct breakpoint *b, enum gdb_signal sig)
ab04a2af 12320{
47591c29 12321 return 1;
ab04a2af
TT
12322}
12323
9d6e6e84
HZ
12324/* The default "after_condition_true" method. */
12325
12326static void
12327base_breakpoint_after_condition_true (struct bpstats *bs)
12328{
12329 /* Nothing to do. */
12330}
12331
ab04a2af 12332struct breakpoint_ops base_breakpoint_ops =
2060206e 12333{
2060206e
PA
12334 base_breakpoint_allocate_location,
12335 base_breakpoint_re_set,
12336 base_breakpoint_insert_location,
12337 base_breakpoint_remove_location,
12338 base_breakpoint_breakpoint_hit,
12339 base_breakpoint_check_status,
12340 base_breakpoint_resources_needed,
12341 base_breakpoint_works_in_software_mode,
12342 base_breakpoint_print_it,
12343 NULL,
12344 base_breakpoint_print_one_detail,
12345 base_breakpoint_print_mention,
983af33b 12346 base_breakpoint_print_recreate,
5f700d83 12347 base_breakpoint_create_sals_from_location,
983af33b 12348 base_breakpoint_create_breakpoints_sal,
5f700d83 12349 base_breakpoint_decode_location,
9d6e6e84
HZ
12350 base_breakpoint_explains_signal,
12351 base_breakpoint_after_condition_true,
2060206e
PA
12352};
12353
12354/* Default breakpoint_ops methods. */
12355
12356static void
348d480f
PA
12357bkpt_re_set (struct breakpoint *b)
12358{
06edf0c0 12359 /* FIXME: is this still reachable? */
9ef9e6a6 12360 if (breakpoint_event_location_empty_p (b))
06edf0c0 12361 {
f00aae0f 12362 /* Anything without a location can't be re-set. */
348d480f 12363 delete_breakpoint (b);
06edf0c0 12364 return;
348d480f 12365 }
06edf0c0
PA
12366
12367 breakpoint_re_set_default (b);
348d480f
PA
12368}
12369
2060206e 12370static int
348d480f
PA
12371bkpt_insert_location (struct bp_location *bl)
12372{
cd6c3b4f
YQ
12373 CORE_ADDR addr = bl->target_info.reqstd_address;
12374
579c6ad9 12375 bl->target_info.kind = breakpoint_kind (bl, &addr);
cd6c3b4f
YQ
12376 bl->target_info.placed_address = addr;
12377
348d480f 12378 if (bl->loc_type == bp_loc_hardware_breakpoint)
7c16b83e 12379 return target_insert_hw_breakpoint (bl->gdbarch, &bl->target_info);
348d480f 12380 else
7c16b83e 12381 return target_insert_breakpoint (bl->gdbarch, &bl->target_info);
348d480f
PA
12382}
12383
2060206e 12384static int
73971819 12385bkpt_remove_location (struct bp_location *bl, enum remove_bp_reason reason)
348d480f
PA
12386{
12387 if (bl->loc_type == bp_loc_hardware_breakpoint)
12388 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
12389 else
73971819 12390 return target_remove_breakpoint (bl->gdbarch, &bl->target_info, reason);
348d480f
PA
12391}
12392
2060206e 12393static int
348d480f 12394bkpt_breakpoint_hit (const struct bp_location *bl,
bd522513 12395 const address_space *aspace, CORE_ADDR bp_addr,
09ac7c10 12396 const struct target_waitstatus *ws)
348d480f 12397{
09ac7c10 12398 if (ws->kind != TARGET_WAITKIND_STOPPED
a493e3e2 12399 || ws->value.sig != GDB_SIGNAL_TRAP)
09ac7c10
TT
12400 return 0;
12401
348d480f
PA
12402 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
12403 aspace, bp_addr))
12404 return 0;
12405
12406 if (overlay_debugging /* unmapped overlay section */
12407 && section_is_overlay (bl->section)
12408 && !section_is_mapped (bl->section))
12409 return 0;
12410
12411 return 1;
12412}
12413
cd1608cc
PA
12414static int
12415dprintf_breakpoint_hit (const struct bp_location *bl,
bd522513 12416 const address_space *aspace, CORE_ADDR bp_addr,
cd1608cc
PA
12417 const struct target_waitstatus *ws)
12418{
12419 if (dprintf_style == dprintf_style_agent
12420 && target_can_run_breakpoint_commands ())
12421 {
12422 /* An agent-style dprintf never causes a stop. If we see a trap
12423 for this address it must be for a breakpoint that happens to
12424 be set at the same address. */
12425 return 0;
12426 }
12427
12428 return bkpt_breakpoint_hit (bl, aspace, bp_addr, ws);
12429}
12430
2060206e 12431static int
348d480f
PA
12432bkpt_resources_needed (const struct bp_location *bl)
12433{
12434 gdb_assert (bl->owner->type == bp_hardware_breakpoint);
12435
12436 return 1;
12437}
12438
2060206e 12439static enum print_stop_action
348d480f
PA
12440bkpt_print_it (bpstat bs)
12441{
348d480f
PA
12442 struct breakpoint *b;
12443 const struct bp_location *bl;
001c8c33 12444 int bp_temp;
79a45e25 12445 struct ui_out *uiout = current_uiout;
348d480f
PA
12446
12447 gdb_assert (bs->bp_location_at != NULL);
12448
12449 bl = bs->bp_location_at;
12450 b = bs->breakpoint_at;
12451
001c8c33
PA
12452 bp_temp = b->disposition == disp_del;
12453 if (bl->address != bl->requested_address)
12454 breakpoint_adjustment_warning (bl->requested_address,
12455 bl->address,
12456 b->number, 1);
12457 annotate_breakpoint (b->number);
f303dbd6
PA
12458 maybe_print_thread_hit_breakpoint (uiout);
12459
112e8700 12460 if (uiout->is_mi_like_p ())
348d480f 12461 {
112e8700 12462 uiout->field_string ("reason",
001c8c33 12463 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
112e8700 12464 uiout->field_string ("disp", bpdisp_text (b->disposition));
06edf0c0 12465 }
6a831f06
PA
12466 if (bp_temp)
12467 uiout->message ("Temporary breakpoint %pF, ",
12468 signed_field ("bkptno", b->number));
12469 else
12470 uiout->message ("Breakpoint %pF, ",
12471 signed_field ("bkptno", b->number));
06edf0c0 12472
001c8c33 12473 return PRINT_SRC_AND_LOC;
06edf0c0
PA
12474}
12475
2060206e 12476static void
06edf0c0
PA
12477bkpt_print_mention (struct breakpoint *b)
12478{
112e8700 12479 if (current_uiout->is_mi_like_p ())
06edf0c0
PA
12480 return;
12481
12482 switch (b->type)
12483 {
12484 case bp_breakpoint:
12485 case bp_gnu_ifunc_resolver:
12486 if (b->disposition == disp_del)
12487 printf_filtered (_("Temporary breakpoint"));
12488 else
12489 printf_filtered (_("Breakpoint"));
12490 printf_filtered (_(" %d"), b->number);
12491 if (b->type == bp_gnu_ifunc_resolver)
12492 printf_filtered (_(" at gnu-indirect-function resolver"));
12493 break;
12494 case bp_hardware_breakpoint:
12495 printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
12496 break;
e7e0cddf
SS
12497 case bp_dprintf:
12498 printf_filtered (_("Dprintf %d"), b->number);
12499 break;
06edf0c0
PA
12500 }
12501
12502 say_where (b);
12503}
12504
2060206e 12505static void
06edf0c0
PA
12506bkpt_print_recreate (struct breakpoint *tp, struct ui_file *fp)
12507{
12508 if (tp->type == bp_breakpoint && tp->disposition == disp_del)
12509 fprintf_unfiltered (fp, "tbreak");
12510 else if (tp->type == bp_breakpoint)
12511 fprintf_unfiltered (fp, "break");
12512 else if (tp->type == bp_hardware_breakpoint
12513 && tp->disposition == disp_del)
12514 fprintf_unfiltered (fp, "thbreak");
12515 else if (tp->type == bp_hardware_breakpoint)
12516 fprintf_unfiltered (fp, "hbreak");
12517 else
12518 internal_error (__FILE__, __LINE__,
12519 _("unhandled breakpoint type %d"), (int) tp->type);
12520
f00aae0f 12521 fprintf_unfiltered (fp, " %s",
d28cd78a 12522 event_location_to_string (tp->location.get ()));
f00aae0f
KS
12523
12524 /* Print out extra_string if this breakpoint is pending. It might
12525 contain, for example, conditions that were set by the user. */
12526 if (tp->loc == NULL && tp->extra_string != NULL)
12527 fprintf_unfiltered (fp, " %s", tp->extra_string);
12528
dd11a36c 12529 print_recreate_thread (tp, fp);
06edf0c0
PA
12530}
12531
983af33b 12532static void
f00aae0f
KS
12533bkpt_create_sals_from_location (const struct event_location *location,
12534 struct linespec_result *canonical,
12535 enum bptype type_wanted)
983af33b 12536{
f00aae0f 12537 create_sals_from_location_default (location, canonical, type_wanted);
983af33b
SDJ
12538}
12539
12540static void
12541bkpt_create_breakpoints_sal (struct gdbarch *gdbarch,
12542 struct linespec_result *canonical,
e1e01040
PA
12543 gdb::unique_xmalloc_ptr<char> cond_string,
12544 gdb::unique_xmalloc_ptr<char> extra_string,
983af33b
SDJ
12545 enum bptype type_wanted,
12546 enum bpdisp disposition,
12547 int thread,
12548 int task, int ignore_count,
12549 const struct breakpoint_ops *ops,
12550 int from_tty, int enabled,
44f238bb 12551 int internal, unsigned flags)
983af33b 12552{
023fa29b 12553 create_breakpoints_sal_default (gdbarch, canonical,
e1e01040
PA
12554 std::move (cond_string),
12555 std::move (extra_string),
e7e0cddf 12556 type_wanted,
983af33b
SDJ
12557 disposition, thread, task,
12558 ignore_count, ops, from_tty,
44f238bb 12559 enabled, internal, flags);
983af33b
SDJ
12560}
12561
6c5b2ebe 12562static std::vector<symtab_and_line>
f00aae0f
KS
12563bkpt_decode_location (struct breakpoint *b,
12564 const struct event_location *location,
6c5b2ebe 12565 struct program_space *search_pspace)
983af33b 12566{
6c5b2ebe 12567 return decode_location_default (b, location, search_pspace);
983af33b
SDJ
12568}
12569
06edf0c0
PA
12570/* Virtual table for internal breakpoints. */
12571
12572static void
12573internal_bkpt_re_set (struct breakpoint *b)
12574{
12575 switch (b->type)
12576 {
12577 /* Delete overlay event and longjmp master breakpoints; they
12578 will be reset later by breakpoint_re_set. */
12579 case bp_overlay_event:
12580 case bp_longjmp_master:
12581 case bp_std_terminate_master:
12582 case bp_exception_master:
12583 delete_breakpoint (b);
12584 break;
12585
12586 /* This breakpoint is special, it's set up when the inferior
12587 starts and we really don't want to touch it. */
12588 case bp_shlib_event:
12589
12590 /* Like bp_shlib_event, this breakpoint type is special. Once
12591 it is set up, we do not want to touch it. */
12592 case bp_thread_event:
12593 break;
12594 }
12595}
12596
12597static void
12598internal_bkpt_check_status (bpstat bs)
12599{
a9b3a50f
PA
12600 if (bs->breakpoint_at->type == bp_shlib_event)
12601 {
12602 /* If requested, stop when the dynamic linker notifies GDB of
12603 events. This allows the user to get control and place
12604 breakpoints in initializer routines for dynamically loaded
12605 objects (among other things). */
12606 bs->stop = stop_on_solib_events;
12607 bs->print = stop_on_solib_events;
12608 }
12609 else
12610 bs->stop = 0;
06edf0c0
PA
12611}
12612
12613static enum print_stop_action
12614internal_bkpt_print_it (bpstat bs)
12615{
06edf0c0 12616 struct breakpoint *b;
06edf0c0 12617
06edf0c0
PA
12618 b = bs->breakpoint_at;
12619
06edf0c0
PA
12620 switch (b->type)
12621 {
348d480f
PA
12622 case bp_shlib_event:
12623 /* Did we stop because the user set the stop_on_solib_events
12624 variable? (If so, we report this as a generic, "Stopped due
12625 to shlib event" message.) */
edcc5120 12626 print_solib_event (0);
348d480f
PA
12627 break;
12628
12629 case bp_thread_event:
12630 /* Not sure how we will get here.
12631 GDB should not stop for these breakpoints. */
12632 printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
12633 break;
12634
12635 case bp_overlay_event:
12636 /* By analogy with the thread event, GDB should not stop for these. */
12637 printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
12638 break;
12639
12640 case bp_longjmp_master:
12641 /* These should never be enabled. */
12642 printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
348d480f
PA
12643 break;
12644
12645 case bp_std_terminate_master:
12646 /* These should never be enabled. */
12647 printf_filtered (_("std::terminate Master Breakpoint: "
12648 "gdb should not stop!\n"));
348d480f
PA
12649 break;
12650
12651 case bp_exception_master:
12652 /* These should never be enabled. */
12653 printf_filtered (_("Exception Master Breakpoint: "
12654 "gdb should not stop!\n"));
06edf0c0
PA
12655 break;
12656 }
12657
001c8c33 12658 return PRINT_NOTHING;
06edf0c0
PA
12659}
12660
12661static void
12662internal_bkpt_print_mention (struct breakpoint *b)
12663{
12664 /* Nothing to mention. These breakpoints are internal. */
12665}
12666
06edf0c0
PA
12667/* Virtual table for momentary breakpoints */
12668
12669static void
12670momentary_bkpt_re_set (struct breakpoint *b)
12671{
12672 /* Keep temporary breakpoints, which can be encountered when we step
4d1eb6b4 12673 over a dlopen call and solib_add is resetting the breakpoints.
06edf0c0
PA
12674 Otherwise these should have been blown away via the cleanup chain
12675 or by breakpoint_init_inferior when we rerun the executable. */
12676}
12677
12678static void
12679momentary_bkpt_check_status (bpstat bs)
12680{
12681 /* Nothing. The point of these breakpoints is causing a stop. */
12682}
12683
12684static enum print_stop_action
12685momentary_bkpt_print_it (bpstat bs)
12686{
001c8c33 12687 return PRINT_UNKNOWN;
348d480f
PA
12688}
12689
06edf0c0
PA
12690static void
12691momentary_bkpt_print_mention (struct breakpoint *b)
348d480f 12692{
06edf0c0 12693 /* Nothing to mention. These breakpoints are internal. */
348d480f
PA
12694}
12695
e2e4d78b
JK
12696/* Ensure INITIATING_FRAME is cleared when no such breakpoint exists.
12697
12698 It gets cleared already on the removal of the first one of such placed
12699 breakpoints. This is OK as they get all removed altogether. */
12700
c1fc2657 12701longjmp_breakpoint::~longjmp_breakpoint ()
e2e4d78b 12702{
c1fc2657 12703 thread_info *tp = find_thread_global_id (this->thread);
e2e4d78b 12704
c1fc2657 12705 if (tp != NULL)
e2e4d78b 12706 tp->initiating_frame = null_frame_id;
e2e4d78b
JK
12707}
12708
55aa24fb
SDJ
12709/* Specific methods for probe breakpoints. */
12710
12711static int
12712bkpt_probe_insert_location (struct bp_location *bl)
12713{
12714 int v = bkpt_insert_location (bl);
12715
12716 if (v == 0)
12717 {
12718 /* The insertion was successful, now let's set the probe's semaphore
12719 if needed. */
935676c9 12720 bl->probe.prob->set_semaphore (bl->probe.objfile, bl->gdbarch);
55aa24fb
SDJ
12721 }
12722
12723 return v;
12724}
12725
12726static int
73971819
PA
12727bkpt_probe_remove_location (struct bp_location *bl,
12728 enum remove_bp_reason reason)
55aa24fb
SDJ
12729{
12730 /* Let's clear the semaphore before removing the location. */
935676c9 12731 bl->probe.prob->clear_semaphore (bl->probe.objfile, bl->gdbarch);
55aa24fb 12732
73971819 12733 return bkpt_remove_location (bl, reason);
55aa24fb
SDJ
12734}
12735
12736static void
f00aae0f 12737bkpt_probe_create_sals_from_location (const struct event_location *location,
5f700d83 12738 struct linespec_result *canonical,
f00aae0f 12739 enum bptype type_wanted)
55aa24fb
SDJ
12740{
12741 struct linespec_sals lsal;
12742
c2f4122d 12743 lsal.sals = parse_probes (location, NULL, canonical);
8e9e35b1
TT
12744 lsal.canonical
12745 = xstrdup (event_location_to_string (canonical->location.get ()));
6c5b2ebe 12746 canonical->lsals.push_back (std::move (lsal));
55aa24fb
SDJ
12747}
12748
6c5b2ebe 12749static std::vector<symtab_and_line>
f00aae0f
KS
12750bkpt_probe_decode_location (struct breakpoint *b,
12751 const struct event_location *location,
6c5b2ebe 12752 struct program_space *search_pspace)
55aa24fb 12753{
6c5b2ebe
PA
12754 std::vector<symtab_and_line> sals = parse_probes (location, search_pspace, NULL);
12755 if (sals.empty ())
55aa24fb 12756 error (_("probe not found"));
6c5b2ebe 12757 return sals;
55aa24fb
SDJ
12758}
12759
348d480f 12760/* The breakpoint_ops structure to be used in tracepoints. */
876fa593 12761
348d480f
PA
12762static void
12763tracepoint_re_set (struct breakpoint *b)
12764{
12765 breakpoint_re_set_default (b);
12766}
876fa593 12767
348d480f
PA
12768static int
12769tracepoint_breakpoint_hit (const struct bp_location *bl,
bd522513 12770 const address_space *aspace, CORE_ADDR bp_addr,
09ac7c10 12771 const struct target_waitstatus *ws)
348d480f
PA
12772{
12773 /* By definition, the inferior does not report stops at
12774 tracepoints. */
12775 return 0;
74960c60
VP
12776}
12777
12778static void
348d480f
PA
12779tracepoint_print_one_detail (const struct breakpoint *self,
12780 struct ui_out *uiout)
74960c60 12781{
d9b3f62e 12782 struct tracepoint *tp = (struct tracepoint *) self;
5d9310c4 12783 if (!tp->static_trace_marker_id.empty ())
348d480f
PA
12784 {
12785 gdb_assert (self->type == bp_static_tracepoint);
cc59ec59 12786
6a831f06
PA
12787 uiout->message ("\tmarker id is %pF\n",
12788 string_field ("static-tracepoint-marker-string-id",
12789 tp->static_trace_marker_id.c_str ()));
348d480f 12790 }
0d381245
VP
12791}
12792
a474d7c2 12793static void
348d480f 12794tracepoint_print_mention (struct breakpoint *b)
a474d7c2 12795{
112e8700 12796 if (current_uiout->is_mi_like_p ())
348d480f 12797 return;
cc59ec59 12798
348d480f
PA
12799 switch (b->type)
12800 {
12801 case bp_tracepoint:
12802 printf_filtered (_("Tracepoint"));
12803 printf_filtered (_(" %d"), b->number);
12804 break;
12805 case bp_fast_tracepoint:
12806 printf_filtered (_("Fast tracepoint"));
12807 printf_filtered (_(" %d"), b->number);
12808 break;
12809 case bp_static_tracepoint:
12810 printf_filtered (_("Static tracepoint"));
12811 printf_filtered (_(" %d"), b->number);
12812 break;
12813 default:
12814 internal_error (__FILE__, __LINE__,
12815 _("unhandled tracepoint type %d"), (int) b->type);
12816 }
12817
12818 say_where (b);
a474d7c2
PA
12819}
12820
348d480f 12821static void
d9b3f62e 12822tracepoint_print_recreate (struct breakpoint *self, struct ui_file *fp)
a474d7c2 12823{
d9b3f62e
PA
12824 struct tracepoint *tp = (struct tracepoint *) self;
12825
12826 if (self->type == bp_fast_tracepoint)
348d480f 12827 fprintf_unfiltered (fp, "ftrace");
c93e8391 12828 else if (self->type == bp_static_tracepoint)
348d480f 12829 fprintf_unfiltered (fp, "strace");
d9b3f62e 12830 else if (self->type == bp_tracepoint)
348d480f
PA
12831 fprintf_unfiltered (fp, "trace");
12832 else
12833 internal_error (__FILE__, __LINE__,
d9b3f62e 12834 _("unhandled tracepoint type %d"), (int) self->type);
cc59ec59 12835
f00aae0f 12836 fprintf_unfiltered (fp, " %s",
d28cd78a 12837 event_location_to_string (self->location.get ()));
d9b3f62e
PA
12838 print_recreate_thread (self, fp);
12839
12840 if (tp->pass_count)
12841 fprintf_unfiltered (fp, " passcount %d\n", tp->pass_count);
a474d7c2
PA
12842}
12843
983af33b 12844static void
f00aae0f
KS
12845tracepoint_create_sals_from_location (const struct event_location *location,
12846 struct linespec_result *canonical,
12847 enum bptype type_wanted)
983af33b 12848{
f00aae0f 12849 create_sals_from_location_default (location, canonical, type_wanted);
983af33b
SDJ
12850}
12851
12852static void
12853tracepoint_create_breakpoints_sal (struct gdbarch *gdbarch,
12854 struct linespec_result *canonical,
e1e01040
PA
12855 gdb::unique_xmalloc_ptr<char> cond_string,
12856 gdb::unique_xmalloc_ptr<char> extra_string,
983af33b
SDJ
12857 enum bptype type_wanted,
12858 enum bpdisp disposition,
12859 int thread,
12860 int task, int ignore_count,
12861 const struct breakpoint_ops *ops,
12862 int from_tty, int enabled,
44f238bb 12863 int internal, unsigned flags)
983af33b 12864{
023fa29b 12865 create_breakpoints_sal_default (gdbarch, canonical,
e1e01040
PA
12866 std::move (cond_string),
12867 std::move (extra_string),
e7e0cddf 12868 type_wanted,
983af33b
SDJ
12869 disposition, thread, task,
12870 ignore_count, ops, from_tty,
44f238bb 12871 enabled, internal, flags);
983af33b
SDJ
12872}
12873
6c5b2ebe 12874static std::vector<symtab_and_line>
f00aae0f
KS
12875tracepoint_decode_location (struct breakpoint *b,
12876 const struct event_location *location,
6c5b2ebe 12877 struct program_space *search_pspace)
983af33b 12878{
6c5b2ebe 12879 return decode_location_default (b, location, search_pspace);
983af33b
SDJ
12880}
12881
2060206e 12882struct breakpoint_ops tracepoint_breakpoint_ops;
348d480f 12883
bac7c5cf 12884/* Virtual table for tracepoints on static probes. */
55aa24fb
SDJ
12885
12886static void
f00aae0f
KS
12887tracepoint_probe_create_sals_from_location
12888 (const struct event_location *location,
12889 struct linespec_result *canonical,
12890 enum bptype type_wanted)
55aa24fb
SDJ
12891{
12892 /* We use the same method for breakpoint on probes. */
f00aae0f 12893 bkpt_probe_create_sals_from_location (location, canonical, type_wanted);
55aa24fb
SDJ
12894}
12895
6c5b2ebe 12896static std::vector<symtab_and_line>
f00aae0f
KS
12897tracepoint_probe_decode_location (struct breakpoint *b,
12898 const struct event_location *location,
6c5b2ebe 12899 struct program_space *search_pspace)
55aa24fb
SDJ
12900{
12901 /* We use the same method for breakpoint on probes. */
6c5b2ebe 12902 return bkpt_probe_decode_location (b, location, search_pspace);
55aa24fb
SDJ
12903}
12904
5c2b4418
HZ
12905/* Dprintf breakpoint_ops methods. */
12906
12907static void
12908dprintf_re_set (struct breakpoint *b)
12909{
12910 breakpoint_re_set_default (b);
12911
f00aae0f
KS
12912 /* extra_string should never be non-NULL for dprintf. */
12913 gdb_assert (b->extra_string != NULL);
5c2b4418
HZ
12914
12915 /* 1 - connect to target 1, that can run breakpoint commands.
12916 2 - create a dprintf, which resolves fine.
12917 3 - disconnect from target 1
12918 4 - connect to target 2, that can NOT run breakpoint commands.
12919
12920 After steps #3/#4, you'll want the dprintf command list to
12921 be updated, because target 1 and 2 may well return different
12922 answers for target_can_run_breakpoint_commands().
12923 Given absence of finer grained resetting, we get to do
12924 it all the time. */
12925 if (b->extra_string != NULL)
12926 update_dprintf_command_list (b);
12927}
12928
2d9442cc
HZ
12929/* Implement the "print_recreate" breakpoint_ops method for dprintf. */
12930
12931static void
12932dprintf_print_recreate (struct breakpoint *tp, struct ui_file *fp)
12933{
f00aae0f 12934 fprintf_unfiltered (fp, "dprintf %s,%s",
d28cd78a 12935 event_location_to_string (tp->location.get ()),
2d9442cc
HZ
12936 tp->extra_string);
12937 print_recreate_thread (tp, fp);
12938}
12939
9d6e6e84
HZ
12940/* Implement the "after_condition_true" breakpoint_ops method for
12941 dprintf.
12942
12943 dprintf's are implemented with regular commands in their command
12944 list, but we run the commands here instead of before presenting the
12945 stop to the user, as dprintf's don't actually cause a stop. This
12946 also makes it so that the commands of multiple dprintfs at the same
12947 address are all handled. */
12948
12949static void
12950dprintf_after_condition_true (struct bpstats *bs)
12951{
04afa70c 12952 struct bpstats tmp_bs;
9d6e6e84
HZ
12953 struct bpstats *tmp_bs_p = &tmp_bs;
12954
12955 /* dprintf's never cause a stop. This wasn't set in the
12956 check_status hook instead because that would make the dprintf's
12957 condition not be evaluated. */
12958 bs->stop = 0;
12959
12960 /* Run the command list here. Take ownership of it instead of
12961 copying. We never want these commands to run later in
12962 bpstat_do_actions, if a breakpoint that causes a stop happens to
12963 be set at same address as this dprintf, or even if running the
12964 commands here throws. */
12965 tmp_bs.commands = bs->commands;
12966 bs->commands = NULL;
9d6e6e84
HZ
12967
12968 bpstat_do_actions_1 (&tmp_bs_p);
12969
12970 /* 'tmp_bs.commands' will usually be NULL by now, but
12971 bpstat_do_actions_1 may return early without processing the whole
12972 list. */
9d6e6e84
HZ
12973}
12974
983af33b
SDJ
12975/* The breakpoint_ops structure to be used on static tracepoints with
12976 markers (`-m'). */
12977
12978static void
f00aae0f 12979strace_marker_create_sals_from_location (const struct event_location *location,
5f700d83 12980 struct linespec_result *canonical,
f00aae0f 12981 enum bptype type_wanted)
983af33b
SDJ
12982{
12983 struct linespec_sals lsal;
f00aae0f 12984 const char *arg_start, *arg;
983af33b 12985
a20714ff 12986 arg = arg_start = get_linespec_location (location)->spec_string;
f00aae0f 12987 lsal.sals = decode_static_tracepoint_spec (&arg);
983af33b 12988
f2fc3015
TT
12989 std::string str (arg_start, arg - arg_start);
12990 const char *ptr = str.c_str ();
a20714ff
PA
12991 canonical->location
12992 = new_linespec_location (&ptr, symbol_name_match_type::FULL);
983af33b 12993
8e9e35b1
TT
12994 lsal.canonical
12995 = xstrdup (event_location_to_string (canonical->location.get ()));
6c5b2ebe 12996 canonical->lsals.push_back (std::move (lsal));
983af33b
SDJ
12997}
12998
12999static void
13000strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
13001 struct linespec_result *canonical,
e1e01040
PA
13002 gdb::unique_xmalloc_ptr<char> cond_string,
13003 gdb::unique_xmalloc_ptr<char> extra_string,
983af33b
SDJ
13004 enum bptype type_wanted,
13005 enum bpdisp disposition,
13006 int thread,
13007 int task, int ignore_count,
13008 const struct breakpoint_ops *ops,
13009 int from_tty, int enabled,
44f238bb 13010 int internal, unsigned flags)
983af33b 13011{
6c5b2ebe 13012 const linespec_sals &lsal = canonical->lsals[0];
983af33b
SDJ
13013
13014 /* If the user is creating a static tracepoint by marker id
13015 (strace -m MARKER_ID), then store the sals index, so that
13016 breakpoint_re_set can try to match up which of the newly
13017 found markers corresponds to this one, and, don't try to
13018 expand multiple locations for each sal, given than SALS
13019 already should contain all sals for MARKER_ID. */
13020
6c5b2ebe 13021 for (size_t i = 0; i < lsal.sals.size (); i++)
983af33b 13022 {
6c5b2ebe
PA
13023 event_location_up location
13024 = copy_event_location (canonical->location.get ());
983af33b 13025
b270e6f9 13026 std::unique_ptr<tracepoint> tp (new tracepoint ());
6c5b2ebe 13027 init_breakpoint_sal (tp.get (), gdbarch, lsal.sals[i],
ffc2605c 13028 std::move (location), NULL,
e1e01040
PA
13029 std::move (cond_string),
13030 std::move (extra_string),
e7e0cddf 13031 type_wanted, disposition,
983af33b 13032 thread, task, ignore_count, ops,
44f238bb 13033 from_tty, enabled, internal, flags,
983af33b
SDJ
13034 canonical->special_display);
13035 /* Given that its possible to have multiple markers with
13036 the same string id, if the user is creating a static
13037 tracepoint by marker id ("strace -m MARKER_ID"), then
13038 store the sals index, so that breakpoint_re_set can
13039 try to match up which of the newly found markers
13040 corresponds to this one */
13041 tp->static_trace_marker_id_idx = i;
13042
b270e6f9 13043 install_breakpoint (internal, std::move (tp), 0);
983af33b
SDJ
13044 }
13045}
13046
6c5b2ebe 13047static std::vector<symtab_and_line>
f00aae0f
KS
13048strace_marker_decode_location (struct breakpoint *b,
13049 const struct event_location *location,
6c5b2ebe 13050 struct program_space *search_pspace)
983af33b
SDJ
13051{
13052 struct tracepoint *tp = (struct tracepoint *) b;
a20714ff 13053 const char *s = get_linespec_location (location)->spec_string;
983af33b 13054
6c5b2ebe
PA
13055 std::vector<symtab_and_line> sals = decode_static_tracepoint_spec (&s);
13056 if (sals.size () > tp->static_trace_marker_id_idx)
983af33b 13057 {
6c5b2ebe
PA
13058 sals[0] = sals[tp->static_trace_marker_id_idx];
13059 sals.resize (1);
13060 return sals;
983af33b
SDJ
13061 }
13062 else
5d9310c4 13063 error (_("marker %s not found"), tp->static_trace_marker_id.c_str ());
983af33b
SDJ
13064}
13065
13066static struct breakpoint_ops strace_marker_breakpoint_ops;
13067
13068static int
13069strace_marker_p (struct breakpoint *b)
13070{
13071 return b->ops == &strace_marker_breakpoint_ops;
13072}
13073
53a5351d 13074/* Delete a breakpoint and clean up all traces of it in the data
f431efe5 13075 structures. */
c906108c
SS
13076
13077void
fba45db2 13078delete_breakpoint (struct breakpoint *bpt)
c906108c 13079{
52f0bd74 13080 struct breakpoint *b;
c906108c 13081
8a3fe4f8 13082 gdb_assert (bpt != NULL);
c906108c 13083
4a64f543
MS
13084 /* Has this bp already been deleted? This can happen because
13085 multiple lists can hold pointers to bp's. bpstat lists are
13086 especial culprits.
13087
13088 One example of this happening is a watchpoint's scope bp. When
13089 the scope bp triggers, we notice that the watchpoint is out of
13090 scope, and delete it. We also delete its scope bp. But the
13091 scope bp is marked "auto-deleting", and is already on a bpstat.
13092 That bpstat is then checked for auto-deleting bp's, which are
13093 deleted.
13094
13095 A real solution to this problem might involve reference counts in
13096 bp's, and/or giving them pointers back to their referencing
13097 bpstat's, and teaching delete_breakpoint to only free a bp's
13098 storage when no more references were extent. A cheaper bandaid
13099 was chosen. */
c906108c
SS
13100 if (bpt->type == bp_none)
13101 return;
13102
4a64f543
MS
13103 /* At least avoid this stale reference until the reference counting
13104 of breakpoints gets resolved. */
d0fb5eae 13105 if (bpt->related_breakpoint != bpt)
e5a0a904 13106 {
d0fb5eae 13107 struct breakpoint *related;
3a5c3e22 13108 struct watchpoint *w;
d0fb5eae
JK
13109
13110 if (bpt->type == bp_watchpoint_scope)
3a5c3e22 13111 w = (struct watchpoint *) bpt->related_breakpoint;
d0fb5eae 13112 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
3a5c3e22
PA
13113 w = (struct watchpoint *) bpt;
13114 else
13115 w = NULL;
13116 if (w != NULL)
13117 watchpoint_del_at_next_stop (w);
d0fb5eae
JK
13118
13119 /* Unlink bpt from the bpt->related_breakpoint ring. */
13120 for (related = bpt; related->related_breakpoint != bpt;
13121 related = related->related_breakpoint);
13122 related->related_breakpoint = bpt->related_breakpoint;
13123 bpt->related_breakpoint = bpt;
e5a0a904
JK
13124 }
13125
a9634178
TJB
13126 /* watch_command_1 creates a watchpoint but only sets its number if
13127 update_watchpoint succeeds in creating its bp_locations. If there's
13128 a problem in that process, we'll be asked to delete the half-created
13129 watchpoint. In that case, don't announce the deletion. */
13130 if (bpt->number)
76727919 13131 gdb::observers::breakpoint_deleted.notify (bpt);
c906108c 13132
c906108c
SS
13133 if (breakpoint_chain == bpt)
13134 breakpoint_chain = bpt->next;
13135
c906108c
SS
13136 ALL_BREAKPOINTS (b)
13137 if (b->next == bpt)
c5aa993b
JM
13138 {
13139 b->next = bpt->next;
13140 break;
13141 }
c906108c 13142
f431efe5
PA
13143 /* Be sure no bpstat's are pointing at the breakpoint after it's
13144 been freed. */
13145 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
e5dd4106 13146 in all threads for now. Note that we cannot just remove bpstats
f431efe5
PA
13147 pointing at bpt from the stop_bpstat list entirely, as breakpoint
13148 commands are associated with the bpstat; if we remove it here,
13149 then the later call to bpstat_do_actions (&stop_bpstat); in
13150 event-top.c won't do anything, and temporary breakpoints with
13151 commands won't work. */
13152
13153 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
13154
4a64f543
MS
13155 /* Now that breakpoint is removed from breakpoint list, update the
13156 global location list. This will remove locations that used to
13157 belong to this breakpoint. Do this before freeing the breakpoint
13158 itself, since remove_breakpoint looks at location's owner. It
13159 might be better design to have location completely
13160 self-contained, but it's not the case now. */
44702360 13161 update_global_location_list (UGLL_DONT_INSERT);
74960c60 13162
4a64f543
MS
13163 /* On the chance that someone will soon try again to delete this
13164 same bp, we mark it as deleted before freeing its storage. */
c906108c 13165 bpt->type = bp_none;
4d01a485 13166 delete bpt;
c906108c
SS
13167}
13168
51be5b68
PA
13169/* Iterator function to call a user-provided callback function once
13170 for each of B and its related breakpoints. */
13171
13172static void
13173iterate_over_related_breakpoints (struct breakpoint *b,
48649e1b 13174 gdb::function_view<void (breakpoint *)> function)
51be5b68
PA
13175{
13176 struct breakpoint *related;
13177
13178 related = b;
13179 do
13180 {
13181 struct breakpoint *next;
13182
13183 /* FUNCTION may delete RELATED. */
13184 next = related->related_breakpoint;
13185
13186 if (next == related)
13187 {
13188 /* RELATED is the last ring entry. */
48649e1b 13189 function (related);
51be5b68
PA
13190
13191 /* FUNCTION may have deleted it, so we'd never reach back to
13192 B. There's nothing left to do anyway, so just break
13193 out. */
13194 break;
13195 }
13196 else
48649e1b 13197 function (related);
51be5b68
PA
13198
13199 related = next;
13200 }
13201 while (related != b);
13202}
95a42b64 13203
4495129a 13204static void
981a3fb3 13205delete_command (const char *arg, int from_tty)
c906108c 13206{
35df4500 13207 struct breakpoint *b, *b_tmp;
c906108c 13208
ea9365bb
TT
13209 dont_repeat ();
13210
c906108c
SS
13211 if (arg == 0)
13212 {
13213 int breaks_to_delete = 0;
13214
46c6471b
PA
13215 /* Delete all breakpoints if no argument. Do not delete
13216 internal breakpoints, these have to be deleted with an
13217 explicit breakpoint number argument. */
c5aa993b 13218 ALL_BREAKPOINTS (b)
46c6471b 13219 if (user_breakpoint_p (b))
973d738b
DJ
13220 {
13221 breaks_to_delete = 1;
13222 break;
13223 }
c906108c
SS
13224
13225 /* Ask user only if there are some breakpoints to delete. */
13226 if (!from_tty
e2e0b3e5 13227 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
c906108c 13228 {
35df4500 13229 ALL_BREAKPOINTS_SAFE (b, b_tmp)
46c6471b 13230 if (user_breakpoint_p (b))
c5aa993b 13231 delete_breakpoint (b);
c906108c
SS
13232 }
13233 }
13234 else
48649e1b 13235 map_breakpoint_numbers
b926417a 13236 (arg, [&] (breakpoint *br)
48649e1b 13237 {
b926417a 13238 iterate_over_related_breakpoints (br, delete_breakpoint);
48649e1b 13239 });
c906108c
SS
13240}
13241
c2f4122d
PA
13242/* Return true if all locations of B bound to PSPACE are pending. If
13243 PSPACE is NULL, all locations of all program spaces are
13244 considered. */
13245
0d381245 13246static int
c2f4122d 13247all_locations_are_pending (struct breakpoint *b, struct program_space *pspace)
fe3f5fa8 13248{
c2f4122d
PA
13249 struct bp_location *loc;
13250
13251 for (loc = b->loc; loc != NULL; loc = loc->next)
13252 if ((pspace == NULL
13253 || loc->pspace == pspace)
13254 && !loc->shlib_disabled
8645ff69 13255 && !loc->pspace->executing_startup)
0d381245
VP
13256 return 0;
13257 return 1;
fe3f5fa8
VP
13258}
13259
776592bf
DE
13260/* Subroutine of update_breakpoint_locations to simplify it.
13261 Return non-zero if multiple fns in list LOC have the same name.
13262 Null names are ignored. */
13263
13264static int
13265ambiguous_names_p (struct bp_location *loc)
13266{
13267 struct bp_location *l;
459a2e4c
TT
13268 htab_t htab = htab_create_alloc (13, htab_hash_string, streq_hash, NULL,
13269 xcalloc, xfree);
776592bf
DE
13270
13271 for (l = loc; l != NULL; l = l->next)
13272 {
13273 const char **slot;
13274 const char *name = l->function_name;
13275
13276 /* Allow for some names to be NULL, ignore them. */
13277 if (name == NULL)
13278 continue;
13279
13280 slot = (const char **) htab_find_slot (htab, (const void *) name,
13281 INSERT);
4a64f543
MS
13282 /* NOTE: We can assume slot != NULL here because xcalloc never
13283 returns NULL. */
776592bf
DE
13284 if (*slot != NULL)
13285 {
13286 htab_delete (htab);
13287 return 1;
13288 }
13289 *slot = name;
13290 }
13291
13292 htab_delete (htab);
13293 return 0;
13294}
13295
0fb4aa4b
PA
13296/* When symbols change, it probably means the sources changed as well,
13297 and it might mean the static tracepoint markers are no longer at
13298 the same address or line numbers they used to be at last we
13299 checked. Losing your static tracepoints whenever you rebuild is
13300 undesirable. This function tries to resync/rematch gdb static
13301 tracepoints with the markers on the target, for static tracepoints
13302 that have not been set by marker id. Static tracepoint that have
13303 been set by marker id are reset by marker id in breakpoint_re_set.
13304 The heuristic is:
13305
13306 1) For a tracepoint set at a specific address, look for a marker at
13307 the old PC. If one is found there, assume to be the same marker.
13308 If the name / string id of the marker found is different from the
13309 previous known name, assume that means the user renamed the marker
13310 in the sources, and output a warning.
13311
13312 2) For a tracepoint set at a given line number, look for a marker
13313 at the new address of the old line number. If one is found there,
13314 assume to be the same marker. If the name / string id of the
13315 marker found is different from the previous known name, assume that
13316 means the user renamed the marker in the sources, and output a
13317 warning.
13318
13319 3) If a marker is no longer found at the same address or line, it
13320 may mean the marker no longer exists. But it may also just mean
13321 the code changed a bit. Maybe the user added a few lines of code
13322 that made the marker move up or down (in line number terms). Ask
13323 the target for info about the marker with the string id as we knew
13324 it. If found, update line number and address in the matching
13325 static tracepoint. This will get confused if there's more than one
13326 marker with the same ID (possible in UST, although unadvised
13327 precisely because it confuses tools). */
13328
13329static struct symtab_and_line
13330update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
13331{
d9b3f62e 13332 struct tracepoint *tp = (struct tracepoint *) b;
0fb4aa4b
PA
13333 struct static_tracepoint_marker marker;
13334 CORE_ADDR pc;
0fb4aa4b
PA
13335
13336 pc = sal.pc;
13337 if (sal.line)
13338 find_line_pc (sal.symtab, sal.line, &pc);
13339
13340 if (target_static_tracepoint_marker_at (pc, &marker))
13341 {
5d9310c4 13342 if (tp->static_trace_marker_id != marker.str_id)
0fb4aa4b 13343 warning (_("static tracepoint %d changed probed marker from %s to %s"),
5d9310c4
SM
13344 b->number, tp->static_trace_marker_id.c_str (),
13345 marker.str_id.c_str ());
0fb4aa4b 13346
5d9310c4 13347 tp->static_trace_marker_id = std::move (marker.str_id);
0fb4aa4b
PA
13348
13349 return sal;
13350 }
13351
13352 /* Old marker wasn't found on target at lineno. Try looking it up
13353 by string ID. */
13354 if (!sal.explicit_pc
13355 && sal.line != 0
13356 && sal.symtab != NULL
5d9310c4 13357 && !tp->static_trace_marker_id.empty ())
0fb4aa4b 13358 {
5d9310c4
SM
13359 std::vector<static_tracepoint_marker> markers
13360 = target_static_tracepoint_markers_by_strid
13361 (tp->static_trace_marker_id.c_str ());
0fb4aa4b 13362
5d9310c4 13363 if (!markers.empty ())
0fb4aa4b 13364 {
0fb4aa4b 13365 struct symbol *sym;
80e1d417 13366 struct static_tracepoint_marker *tpmarker;
79a45e25 13367 struct ui_out *uiout = current_uiout;
67994074 13368 struct explicit_location explicit_loc;
0fb4aa4b 13369
5d9310c4 13370 tpmarker = &markers[0];
0fb4aa4b 13371
5d9310c4 13372 tp->static_trace_marker_id = std::move (tpmarker->str_id);
0fb4aa4b
PA
13373
13374 warning (_("marker for static tracepoint %d (%s) not "
13375 "found at previous line number"),
5d9310c4 13376 b->number, tp->static_trace_marker_id.c_str ());
0fb4aa4b 13377
51abb421 13378 symtab_and_line sal2 = find_pc_line (tpmarker->address, 0);
80e1d417 13379 sym = find_pc_sect_function (tpmarker->address, NULL);
112e8700 13380 uiout->text ("Now in ");
0fb4aa4b
PA
13381 if (sym)
13382 {
987012b8 13383 uiout->field_string ("func", sym->print_name (),
e43b10e1 13384 function_name_style.style ());
112e8700 13385 uiout->text (" at ");
0fb4aa4b 13386 }
112e8700 13387 uiout->field_string ("file",
cbe56571 13388 symtab_to_filename_for_display (sal2.symtab),
e43b10e1 13389 file_name_style.style ());
112e8700 13390 uiout->text (":");
0fb4aa4b 13391
112e8700 13392 if (uiout->is_mi_like_p ())
0fb4aa4b 13393 {
0b0865da 13394 const char *fullname = symtab_to_fullname (sal2.symtab);
0fb4aa4b 13395
112e8700 13396 uiout->field_string ("fullname", fullname);
0fb4aa4b
PA
13397 }
13398
381befee 13399 uiout->field_signed ("line", sal2.line);
112e8700 13400 uiout->text ("\n");
0fb4aa4b 13401
80e1d417 13402 b->loc->line_number = sal2.line;
2f202fde 13403 b->loc->symtab = sym != NULL ? sal2.symtab : NULL;
0fb4aa4b 13404
d28cd78a 13405 b->location.reset (NULL);
67994074
KS
13406 initialize_explicit_location (&explicit_loc);
13407 explicit_loc.source_filename
00e52e53 13408 = ASTRDUP (symtab_to_filename_for_display (sal2.symtab));
67994074
KS
13409 explicit_loc.line_offset.offset = b->loc->line_number;
13410 explicit_loc.line_offset.sign = LINE_OFFSET_NONE;
d28cd78a 13411 b->location = new_explicit_location (&explicit_loc);
0fb4aa4b
PA
13412
13413 /* Might be nice to check if function changed, and warn if
13414 so. */
0fb4aa4b
PA
13415 }
13416 }
13417 return sal;
13418}
13419
8d3788bd
VP
13420/* Returns 1 iff locations A and B are sufficiently same that
13421 we don't need to report breakpoint as changed. */
13422
13423static int
13424locations_are_equal (struct bp_location *a, struct bp_location *b)
13425{
13426 while (a && b)
13427 {
13428 if (a->address != b->address)
13429 return 0;
13430
13431 if (a->shlib_disabled != b->shlib_disabled)
13432 return 0;
13433
13434 if (a->enabled != b->enabled)
13435 return 0;
13436
13437 a = a->next;
13438 b = b->next;
13439 }
13440
13441 if ((a == NULL) != (b == NULL))
13442 return 0;
13443
13444 return 1;
13445}
13446
c2f4122d
PA
13447/* Split all locations of B that are bound to PSPACE out of B's
13448 location list to a separate list and return that list's head. If
13449 PSPACE is NULL, hoist out all locations of B. */
13450
13451static struct bp_location *
13452hoist_existing_locations (struct breakpoint *b, struct program_space *pspace)
13453{
13454 struct bp_location head;
13455 struct bp_location *i = b->loc;
13456 struct bp_location **i_link = &b->loc;
13457 struct bp_location *hoisted = &head;
13458
13459 if (pspace == NULL)
13460 {
13461 i = b->loc;
13462 b->loc = NULL;
13463 return i;
13464 }
13465
13466 head.next = NULL;
13467
13468 while (i != NULL)
13469 {
13470 if (i->pspace == pspace)
13471 {
13472 *i_link = i->next;
13473 i->next = NULL;
13474 hoisted->next = i;
13475 hoisted = i;
13476 }
13477 else
13478 i_link = &i->next;
13479 i = *i_link;
13480 }
13481
13482 return head.next;
13483}
13484
13485/* Create new breakpoint locations for B (a hardware or software
13486 breakpoint) based on SALS and SALS_END. If SALS_END.NELTS is not
13487 zero, then B is a ranged breakpoint. Only recreates locations for
13488 FILTER_PSPACE. Locations of other program spaces are left
13489 untouched. */
f1310107 13490
0e30163f 13491void
0d381245 13492update_breakpoint_locations (struct breakpoint *b,
c2f4122d 13493 struct program_space *filter_pspace,
6c5b2ebe
PA
13494 gdb::array_view<const symtab_and_line> sals,
13495 gdb::array_view<const symtab_and_line> sals_end)
fe3f5fa8 13496{
c2f4122d 13497 struct bp_location *existing_locations;
0d381245 13498
6c5b2ebe 13499 if (!sals_end.empty () && (sals.size () != 1 || sals_end.size () != 1))
f8eba3c6
TT
13500 {
13501 /* Ranged breakpoints have only one start location and one end
13502 location. */
13503 b->enable_state = bp_disabled;
f8eba3c6
TT
13504 printf_unfiltered (_("Could not reset ranged breakpoint %d: "
13505 "multiple locations found\n"),
13506 b->number);
13507 return;
13508 }
f1310107 13509
4a64f543
MS
13510 /* If there's no new locations, and all existing locations are
13511 pending, don't do anything. This optimizes the common case where
13512 all locations are in the same shared library, that was unloaded.
13513 We'd like to retain the location, so that when the library is
13514 loaded again, we don't loose the enabled/disabled status of the
13515 individual locations. */
6c5b2ebe 13516 if (all_locations_are_pending (b, filter_pspace) && sals.empty ())
fe3f5fa8
VP
13517 return;
13518
c2f4122d 13519 existing_locations = hoist_existing_locations (b, filter_pspace);
fe3f5fa8 13520
6c5b2ebe 13521 for (const auto &sal : sals)
fe3f5fa8 13522 {
f8eba3c6
TT
13523 struct bp_location *new_loc;
13524
6c5b2ebe 13525 switch_to_program_space_and_thread (sal.pspace);
f8eba3c6 13526
6c5b2ebe 13527 new_loc = add_location_to_breakpoint (b, &sal);
fe3f5fa8 13528
0d381245
VP
13529 /* Reparse conditions, they might contain references to the
13530 old symtab. */
13531 if (b->cond_string != NULL)
13532 {
bbc13ae3 13533 const char *s;
fe3f5fa8 13534
0d381245 13535 s = b->cond_string;
a70b8144 13536 try
0d381245 13537 {
6c5b2ebe
PA
13538 new_loc->cond = parse_exp_1 (&s, sal.pc,
13539 block_for_pc (sal.pc),
0d381245
VP
13540 0);
13541 }
230d2906 13542 catch (const gdb_exception_error &e)
0d381245 13543 {
3e43a32a
MS
13544 warning (_("failed to reevaluate condition "
13545 "for breakpoint %d: %s"),
3d6e9d23 13546 b->number, e.what ());
0d381245
VP
13547 new_loc->enabled = 0;
13548 }
13549 }
fe3f5fa8 13550
6c5b2ebe 13551 if (!sals_end.empty ())
f1310107 13552 {
6c5b2ebe 13553 CORE_ADDR end = find_breakpoint_range_end (sals_end[0]);
f1310107 13554
6c5b2ebe 13555 new_loc->length = end - sals[0].pc + 1;
f1310107 13556 }
0d381245 13557 }
fe3f5fa8 13558
4a64f543
MS
13559 /* If possible, carry over 'disable' status from existing
13560 breakpoints. */
0d381245
VP
13561 {
13562 struct bp_location *e = existing_locations;
776592bf
DE
13563 /* If there are multiple breakpoints with the same function name,
13564 e.g. for inline functions, comparing function names won't work.
13565 Instead compare pc addresses; this is just a heuristic as things
13566 may have moved, but in practice it gives the correct answer
13567 often enough until a better solution is found. */
13568 int have_ambiguous_names = ambiguous_names_p (b->loc);
13569
0d381245
VP
13570 for (; e; e = e->next)
13571 {
13572 if (!e->enabled && e->function_name)
13573 {
13574 struct bp_location *l = b->loc;
776592bf
DE
13575 if (have_ambiguous_names)
13576 {
13577 for (; l; l = l->next)
7f32a4d5
PA
13578 {
13579 /* Ignore software vs hardware location type at
13580 this point, because with "set breakpoint
13581 auto-hw", after a re-set, locations that were
13582 hardware can end up as software, or vice versa.
13583 As mentioned above, this is an heuristic and in
13584 practice should give the correct answer often
13585 enough. */
13586 if (breakpoint_locations_match (e, l, true))
13587 {
13588 l->enabled = 0;
13589 break;
13590 }
13591 }
776592bf
DE
13592 }
13593 else
13594 {
13595 for (; l; l = l->next)
13596 if (l->function_name
13597 && strcmp (e->function_name, l->function_name) == 0)
13598 {
13599 l->enabled = 0;
13600 break;
13601 }
13602 }
0d381245
VP
13603 }
13604 }
13605 }
fe3f5fa8 13606
8d3788bd 13607 if (!locations_are_equal (existing_locations, b->loc))
76727919 13608 gdb::observers::breakpoint_modified.notify (b);
fe3f5fa8
VP
13609}
13610
f00aae0f 13611/* Find the SaL locations corresponding to the given LOCATION.
ef23e705
TJB
13612 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
13613
6c5b2ebe 13614static std::vector<symtab_and_line>
f00aae0f 13615location_to_sals (struct breakpoint *b, struct event_location *location,
c2f4122d 13616 struct program_space *search_pspace, int *found)
ef23e705 13617{
cc06b668 13618 struct gdb_exception exception;
ef23e705 13619
983af33b 13620 gdb_assert (b->ops != NULL);
ef23e705 13621
6c5b2ebe
PA
13622 std::vector<symtab_and_line> sals;
13623
a70b8144 13624 try
ef23e705 13625 {
6c5b2ebe 13626 sals = b->ops->decode_location (b, location, search_pspace);
ef23e705 13627 }
94aeb44b 13628 catch (gdb_exception_error &e)
ef23e705
TJB
13629 {
13630 int not_found_and_ok = 0;
492d29ea 13631
ef23e705
TJB
13632 /* For pending breakpoints, it's expected that parsing will
13633 fail until the right shared library is loaded. User has
13634 already told to create pending breakpoints and don't need
13635 extra messages. If breakpoint is in bp_shlib_disabled
13636 state, then user already saw the message about that
13637 breakpoint being disabled, and don't want to see more
13638 errors. */
58438ac1 13639 if (e.error == NOT_FOUND_ERROR
c2f4122d
PA
13640 && (b->condition_not_parsed
13641 || (b->loc != NULL
13642 && search_pspace != NULL
13643 && b->loc->pspace != search_pspace)
ef23e705 13644 || (b->loc && b->loc->shlib_disabled)
f8eba3c6 13645 || (b->loc && b->loc->pspace->executing_startup)
ef23e705
TJB
13646 || b->enable_state == bp_disabled))
13647 not_found_and_ok = 1;
13648
13649 if (!not_found_and_ok)
13650 {
13651 /* We surely don't want to warn about the same breakpoint
13652 10 times. One solution, implemented here, is disable
13653 the breakpoint on error. Another solution would be to
13654 have separate 'warning emitted' flag. Since this
13655 happens only when a binary has changed, I don't know
13656 which approach is better. */
13657 b->enable_state = bp_disabled;
eedc3f4f 13658 throw;
ef23e705 13659 }
94aeb44b
TT
13660
13661 exception = std::move (e);
ef23e705
TJB
13662 }
13663
492d29ea 13664 if (exception.reason == 0 || exception.error != NOT_FOUND_ERROR)
ef23e705 13665 {
6c5b2ebe
PA
13666 for (auto &sal : sals)
13667 resolve_sal_pc (&sal);
f00aae0f 13668 if (b->condition_not_parsed && b->extra_string != NULL)
ef23e705 13669 {
ed1d1739
KS
13670 char *cond_string, *extra_string;
13671 int thread, task;
ef23e705 13672
6c5b2ebe 13673 find_condition_and_thread (b->extra_string, sals[0].pc,
e7e0cddf
SS
13674 &cond_string, &thread, &task,
13675 &extra_string);
f00aae0f 13676 gdb_assert (b->cond_string == NULL);
ef23e705
TJB
13677 if (cond_string)
13678 b->cond_string = cond_string;
13679 b->thread = thread;
13680 b->task = task;
e7e0cddf 13681 if (extra_string)
f00aae0f
KS
13682 {
13683 xfree (b->extra_string);
13684 b->extra_string = extra_string;
13685 }
ef23e705
TJB
13686 b->condition_not_parsed = 0;
13687 }
13688
983af33b 13689 if (b->type == bp_static_tracepoint && !strace_marker_p (b))
6c5b2ebe 13690 sals[0] = update_static_tracepoint (b, sals[0]);
ef23e705 13691
58438ac1
TT
13692 *found = 1;
13693 }
13694 else
13695 *found = 0;
ef23e705
TJB
13696
13697 return sals;
13698}
13699
348d480f
PA
13700/* The default re_set method, for typical hardware or software
13701 breakpoints. Reevaluate the breakpoint and recreate its
13702 locations. */
13703
13704static void
28010a5d 13705breakpoint_re_set_default (struct breakpoint *b)
ef23e705 13706{
c2f4122d 13707 struct program_space *filter_pspace = current_program_space;
6c5b2ebe 13708 std::vector<symtab_and_line> expanded, expanded_end;
ef23e705 13709
6c5b2ebe
PA
13710 int found;
13711 std::vector<symtab_and_line> sals = location_to_sals (b, b->location.get (),
13712 filter_pspace, &found);
ef23e705 13713 if (found)
6c5b2ebe 13714 expanded = std::move (sals);
ef23e705 13715
f00aae0f 13716 if (b->location_range_end != NULL)
f1310107 13717 {
6c5b2ebe
PA
13718 std::vector<symtab_and_line> sals_end
13719 = location_to_sals (b, b->location_range_end.get (),
13720 filter_pspace, &found);
f1310107 13721 if (found)
6c5b2ebe 13722 expanded_end = std::move (sals_end);
f1310107
TJB
13723 }
13724
c2f4122d 13725 update_breakpoint_locations (b, filter_pspace, expanded, expanded_end);
28010a5d
PA
13726}
13727
983af33b
SDJ
13728/* Default method for creating SALs from an address string. It basically
13729 calls parse_breakpoint_sals. Return 1 for success, zero for failure. */
13730
13731static void
f00aae0f
KS
13732create_sals_from_location_default (const struct event_location *location,
13733 struct linespec_result *canonical,
13734 enum bptype type_wanted)
983af33b 13735{
f00aae0f 13736 parse_breakpoint_sals (location, canonical);
983af33b
SDJ
13737}
13738
13739/* Call create_breakpoints_sal for the given arguments. This is the default
13740 function for the `create_breakpoints_sal' method of
13741 breakpoint_ops. */
13742
13743static void
13744create_breakpoints_sal_default (struct gdbarch *gdbarch,
13745 struct linespec_result *canonical,
e1e01040
PA
13746 gdb::unique_xmalloc_ptr<char> cond_string,
13747 gdb::unique_xmalloc_ptr<char> extra_string,
983af33b
SDJ
13748 enum bptype type_wanted,
13749 enum bpdisp disposition,
13750 int thread,
13751 int task, int ignore_count,
13752 const struct breakpoint_ops *ops,
13753 int from_tty, int enabled,
44f238bb 13754 int internal, unsigned flags)
983af33b 13755{
e1e01040
PA
13756 create_breakpoints_sal (gdbarch, canonical,
13757 std::move (cond_string),
13758 std::move (extra_string),
983af33b
SDJ
13759 type_wanted, disposition,
13760 thread, task, ignore_count, ops, from_tty,
44f238bb 13761 enabled, internal, flags);
983af33b
SDJ
13762}
13763
13764/* Decode the line represented by S by calling decode_line_full. This is the
5f700d83 13765 default function for the `decode_location' method of breakpoint_ops. */
983af33b 13766
6c5b2ebe 13767static std::vector<symtab_and_line>
f00aae0f
KS
13768decode_location_default (struct breakpoint *b,
13769 const struct event_location *location,
6c5b2ebe 13770 struct program_space *search_pspace)
983af33b
SDJ
13771{
13772 struct linespec_result canonical;
13773
c2f4122d 13774 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, search_pspace,
cafb3438 13775 NULL, 0, &canonical, multiple_symbols_all,
c0e8dcd8 13776 b->filter.get ());
983af33b
SDJ
13777
13778 /* We should get 0 or 1 resulting SALs. */
6c5b2ebe 13779 gdb_assert (canonical.lsals.size () < 2);
983af33b 13780
6c5b2ebe 13781 if (!canonical.lsals.empty ())
983af33b 13782 {
6c5b2ebe
PA
13783 const linespec_sals &lsal = canonical.lsals[0];
13784 return std::move (lsal.sals);
983af33b 13785 }
6c5b2ebe 13786 return {};
983af33b
SDJ
13787}
13788
bf469271 13789/* Reset a breakpoint. */
c906108c 13790
bf469271
PA
13791static void
13792breakpoint_re_set_one (breakpoint *b)
c906108c 13793{
fdf44873
TT
13794 input_radix = b->input_radix;
13795 set_language (b->language);
c906108c 13796
348d480f 13797 b->ops->re_set (b);
c906108c
SS
13798}
13799
c2f4122d
PA
13800/* Re-set breakpoint locations for the current program space.
13801 Locations bound to other program spaces are left untouched. */
13802
c906108c 13803void
69de3c6a 13804breakpoint_re_set (void)
c906108c 13805{
35df4500 13806 struct breakpoint *b, *b_tmp;
2a7f3dff 13807
c5aa993b 13808 {
fdf44873
TT
13809 scoped_restore_current_language save_language;
13810 scoped_restore save_input_radix = make_scoped_restore (&input_radix);
5ed8105e 13811 scoped_restore_current_pspace_and_thread restore_pspace_thread;
e62c965a 13812
8e817061
JB
13813 /* breakpoint_re_set_one sets the current_language to the language
13814 of the breakpoint it is resetting (see prepare_re_set_context)
13815 before re-evaluating the breakpoint's location. This change can
13816 unfortunately get undone by accident if the language_mode is set
13817 to auto, and we either switch frames, or more likely in this context,
13818 we select the current frame.
13819
13820 We prevent this by temporarily turning the language_mode to
13821 language_mode_manual. We restore it once all breakpoints
13822 have been reset. */
13823 scoped_restore save_language_mode = make_scoped_restore (&language_mode);
13824 language_mode = language_mode_manual;
13825
5ed8105e
PA
13826 /* Note: we must not try to insert locations until after all
13827 breakpoints have been re-set. Otherwise, e.g., when re-setting
13828 breakpoint 1, we'd insert the locations of breakpoint 2, which
13829 hadn't been re-set yet, and thus may have stale locations. */
4efc6507 13830
5ed8105e
PA
13831 ALL_BREAKPOINTS_SAFE (b, b_tmp)
13832 {
a70b8144 13833 try
bf469271
PA
13834 {
13835 breakpoint_re_set_one (b);
13836 }
230d2906 13837 catch (const gdb_exception &ex)
bf469271
PA
13838 {
13839 exception_fprintf (gdb_stderr, ex,
13840 "Error in re-setting breakpoint %d: ",
13841 b->number);
13842 }
5ed8105e 13843 }
5ed8105e
PA
13844
13845 jit_breakpoint_re_set ();
13846 }
6c95b8df 13847
af02033e
PP
13848 create_overlay_event_breakpoint ();
13849 create_longjmp_master_breakpoint ();
13850 create_std_terminate_master_breakpoint ();
186c406b 13851 create_exception_master_breakpoint ();
2a7f3dff
PA
13852
13853 /* Now we can insert. */
13854 update_global_location_list (UGLL_MAY_INSERT);
c906108c
SS
13855}
13856\f
c906108c
SS
13857/* Reset the thread number of this breakpoint:
13858
13859 - If the breakpoint is for all threads, leave it as-is.
4a64f543 13860 - Else, reset it to the current thread for inferior_ptid. */
c906108c 13861void
fba45db2 13862breakpoint_re_set_thread (struct breakpoint *b)
c906108c
SS
13863{
13864 if (b->thread != -1)
13865 {
00431a78 13866 b->thread = inferior_thread ()->global_num;
6c95b8df
PA
13867
13868 /* We're being called after following a fork. The new fork is
13869 selected as current, and unless this was a vfork will have a
13870 different program space from the original thread. Reset that
13871 as well. */
13872 b->loc->pspace = current_program_space;
c906108c
SS
13873 }
13874}
13875
03ac34d5
MS
13876/* Set ignore-count of breakpoint number BPTNUM to COUNT.
13877 If from_tty is nonzero, it prints a message to that effect,
13878 which ends with a period (no newline). */
13879
c906108c 13880void
fba45db2 13881set_ignore_count (int bptnum, int count, int from_tty)
c906108c 13882{
52f0bd74 13883 struct breakpoint *b;
c906108c
SS
13884
13885 if (count < 0)
13886 count = 0;
13887
13888 ALL_BREAKPOINTS (b)
13889 if (b->number == bptnum)
c5aa993b 13890 {
d77f58be
SS
13891 if (is_tracepoint (b))
13892 {
13893 if (from_tty && count != 0)
13894 printf_filtered (_("Ignore count ignored for tracepoint %d."),
13895 bptnum);
13896 return;
13897 }
13898
c5aa993b 13899 b->ignore_count = count;
221ea385
KS
13900 if (from_tty)
13901 {
13902 if (count == 0)
3e43a32a
MS
13903 printf_filtered (_("Will stop next time "
13904 "breakpoint %d is reached."),
221ea385
KS
13905 bptnum);
13906 else if (count == 1)
a3f17187 13907 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
221ea385
KS
13908 bptnum);
13909 else
3e43a32a
MS
13910 printf_filtered (_("Will ignore next %d "
13911 "crossings of breakpoint %d."),
221ea385
KS
13912 count, bptnum);
13913 }
76727919 13914 gdb::observers::breakpoint_modified.notify (b);
c5aa993b
JM
13915 return;
13916 }
c906108c 13917
8a3fe4f8 13918 error (_("No breakpoint number %d."), bptnum);
c906108c
SS
13919}
13920
c906108c
SS
13921/* Command to set ignore-count of breakpoint N to COUNT. */
13922
13923static void
0b39b52e 13924ignore_command (const char *args, int from_tty)
c906108c 13925{
0b39b52e 13926 const char *p = args;
52f0bd74 13927 int num;
c906108c
SS
13928
13929 if (p == 0)
e2e0b3e5 13930 error_no_arg (_("a breakpoint number"));
c5aa993b 13931
c906108c 13932 num = get_number (&p);
5c44784c 13933 if (num == 0)
8a3fe4f8 13934 error (_("bad breakpoint number: '%s'"), args);
c906108c 13935 if (*p == 0)
8a3fe4f8 13936 error (_("Second argument (specified ignore-count) is missing."));
c906108c
SS
13937
13938 set_ignore_count (num,
13939 longest_to_int (value_as_long (parse_and_eval (p))),
13940 from_tty);
221ea385
KS
13941 if (from_tty)
13942 printf_filtered ("\n");
c906108c
SS
13943}
13944\f
d0fe4701
XR
13945
13946/* Call FUNCTION on each of the breakpoints with numbers in the range
13947 defined by BP_NUM_RANGE (an inclusive range). */
c906108c
SS
13948
13949static void
d0fe4701
XR
13950map_breakpoint_number_range (std::pair<int, int> bp_num_range,
13951 gdb::function_view<void (breakpoint *)> function)
c906108c 13952{
d0fe4701
XR
13953 if (bp_num_range.first == 0)
13954 {
13955 warning (_("bad breakpoint number at or near '%d'"),
13956 bp_num_range.first);
13957 }
13958 else
c906108c 13959 {
d0fe4701 13960 struct breakpoint *b, *tmp;
197f0a60 13961
d0fe4701 13962 for (int i = bp_num_range.first; i <= bp_num_range.second; i++)
5c44784c 13963 {
d0fe4701
XR
13964 bool match = false;
13965
5c44784c 13966 ALL_BREAKPOINTS_SAFE (b, tmp)
d0fe4701 13967 if (b->number == i)
5c44784c 13968 {
bfd28288 13969 match = true;
48649e1b 13970 function (b);
11cf8741 13971 break;
5c44784c 13972 }
bfd28288 13973 if (!match)
d0fe4701 13974 printf_unfiltered (_("No breakpoint number %d.\n"), i);
c5aa993b 13975 }
c906108c
SS
13976 }
13977}
13978
d0fe4701
XR
13979/* Call FUNCTION on each of the breakpoints whose numbers are given in
13980 ARGS. */
13981
13982static void
13983map_breakpoint_numbers (const char *args,
13984 gdb::function_view<void (breakpoint *)> function)
13985{
13986 if (args == NULL || *args == '\0')
13987 error_no_arg (_("one or more breakpoint numbers"));
13988
13989 number_or_range_parser parser (args);
13990
13991 while (!parser.finished ())
13992 {
13993 int num = parser.get_number ();
13994 map_breakpoint_number_range (std::make_pair (num, num), function);
13995 }
13996}
13997
13998/* Return the breakpoint location structure corresponding to the
13999 BP_NUM and LOC_NUM values. */
14000
0d381245 14001static struct bp_location *
d0fe4701 14002find_location_by_number (int bp_num, int loc_num)
0d381245 14003{
0d381245 14004 struct breakpoint *b;
0d381245
VP
14005
14006 ALL_BREAKPOINTS (b)
14007 if (b->number == bp_num)
14008 {
14009 break;
14010 }
14011
14012 if (!b || b->number != bp_num)
d0fe4701 14013 error (_("Bad breakpoint number '%d'"), bp_num);
0d381245 14014
0d381245 14015 if (loc_num == 0)
d0fe4701 14016 error (_("Bad breakpoint location number '%d'"), loc_num);
0d381245 14017
d0fe4701
XR
14018 int n = 0;
14019 for (bp_location *loc = b->loc; loc != NULL; loc = loc->next)
14020 if (++n == loc_num)
14021 return loc;
14022
14023 error (_("Bad breakpoint location number '%d'"), loc_num);
0d381245
VP
14024}
14025
95e95a6d
PA
14026/* Modes of operation for extract_bp_num. */
14027enum class extract_bp_kind
14028{
14029 /* Extracting a breakpoint number. */
14030 bp,
14031
14032 /* Extracting a location number. */
14033 loc,
14034};
14035
14036/* Extract a breakpoint or location number (as determined by KIND)
14037 from the string starting at START. TRAILER is a character which
14038 can be found after the number. If you don't want a trailer, use
14039 '\0'. If END_OUT is not NULL, it is set to point after the parsed
14040 string. This always returns a positive integer. */
14041
14042static int
14043extract_bp_num (extract_bp_kind kind, const char *start,
14044 int trailer, const char **end_out = NULL)
14045{
14046 const char *end = start;
14047 int num = get_number_trailer (&end, trailer);
14048 if (num < 0)
14049 error (kind == extract_bp_kind::bp
14050 ? _("Negative breakpoint number '%.*s'")
14051 : _("Negative breakpoint location number '%.*s'"),
14052 int (end - start), start);
14053 if (num == 0)
14054 error (kind == extract_bp_kind::bp
14055 ? _("Bad breakpoint number '%.*s'")
14056 : _("Bad breakpoint location number '%.*s'"),
14057 int (end - start), start);
14058
14059 if (end_out != NULL)
14060 *end_out = end;
14061 return num;
14062}
14063
14064/* Extract a breakpoint or location range (as determined by KIND) in
14065 the form NUM1-NUM2 stored at &ARG[arg_offset]. Returns a std::pair
14066 representing the (inclusive) range. The returned pair's elements
14067 are always positive integers. */
14068
14069static std::pair<int, int>
14070extract_bp_or_bp_range (extract_bp_kind kind,
14071 const std::string &arg,
14072 std::string::size_type arg_offset)
14073{
14074 std::pair<int, int> range;
14075 const char *bp_loc = &arg[arg_offset];
14076 std::string::size_type dash = arg.find ('-', arg_offset);
14077 if (dash != std::string::npos)
14078 {
14079 /* bp_loc is a range (x-z). */
14080 if (arg.length () == dash + 1)
14081 error (kind == extract_bp_kind::bp
14082 ? _("Bad breakpoint number at or near: '%s'")
14083 : _("Bad breakpoint location number at or near: '%s'"),
14084 bp_loc);
14085
14086 const char *end;
14087 const char *start_first = bp_loc;
14088 const char *start_second = &arg[dash + 1];
14089 range.first = extract_bp_num (kind, start_first, '-');
14090 range.second = extract_bp_num (kind, start_second, '\0', &end);
14091
14092 if (range.first > range.second)
14093 error (kind == extract_bp_kind::bp
14094 ? _("Inverted breakpoint range at '%.*s'")
14095 : _("Inverted breakpoint location range at '%.*s'"),
14096 int (end - start_first), start_first);
14097 }
14098 else
14099 {
14100 /* bp_loc is a single value. */
14101 range.first = extract_bp_num (kind, bp_loc, '\0');
14102 range.second = range.first;
14103 }
14104 return range;
14105}
14106
d0fe4701
XR
14107/* Extract the breakpoint/location range specified by ARG. Returns
14108 the breakpoint range in BP_NUM_RANGE, and the location range in
14109 BP_LOC_RANGE.
14110
14111 ARG may be in any of the following forms:
14112
14113 x where 'x' is a breakpoint number.
14114 x-y where 'x' and 'y' specify a breakpoint numbers range.
14115 x.y where 'x' is a breakpoint number and 'y' a location number.
14116 x.y-z where 'x' is a breakpoint number and 'y' and 'z' specify a
14117 location number range.
14118*/
14119
cc638e86 14120static void
d0fe4701
XR
14121extract_bp_number_and_location (const std::string &arg,
14122 std::pair<int, int> &bp_num_range,
14123 std::pair<int, int> &bp_loc_range)
14124{
14125 std::string::size_type dot = arg.find ('.');
14126
14127 if (dot != std::string::npos)
14128 {
14129 /* Handle 'x.y' and 'x.y-z' cases. */
14130
14131 if (arg.length () == dot + 1 || dot == 0)
95e95a6d 14132 error (_("Bad breakpoint number at or near: '%s'"), arg.c_str ());
d0fe4701 14133
95e95a6d
PA
14134 bp_num_range.first
14135 = extract_bp_num (extract_bp_kind::bp, arg.c_str (), '.');
14136 bp_num_range.second = bp_num_range.first;
d0fe4701 14137
95e95a6d
PA
14138 bp_loc_range = extract_bp_or_bp_range (extract_bp_kind::loc,
14139 arg, dot + 1);
d0fe4701
XR
14140 }
14141 else
14142 {
14143 /* Handle x and x-y cases. */
d0fe4701 14144
95e95a6d 14145 bp_num_range = extract_bp_or_bp_range (extract_bp_kind::bp, arg, 0);
d0fe4701
XR
14146 bp_loc_range.first = 0;
14147 bp_loc_range.second = 0;
14148 }
d0fe4701
XR
14149}
14150
14151/* Enable or disable a breakpoint location BP_NUM.LOC_NUM. ENABLE
14152 specifies whether to enable or disable. */
14153
14154static void
14155enable_disable_bp_num_loc (int bp_num, int loc_num, bool enable)
14156{
14157 struct bp_location *loc = find_location_by_number (bp_num, loc_num);
14158 if (loc != NULL)
14159 {
14160 if (loc->enabled != enable)
14161 {
14162 loc->enabled = enable;
14163 mark_breakpoint_location_modified (loc);
14164 }
14165 if (target_supports_enable_disable_tracepoint ()
14166 && current_trace_status ()->running && loc->owner
14167 && is_tracepoint (loc->owner))
14168 target_disable_tracepoint (loc);
14169 }
14170 update_global_location_list (UGLL_DONT_INSERT);
d7154a8d
JV
14171
14172 gdb::observers::breakpoint_modified.notify (loc->owner);
d0fe4701
XR
14173}
14174
14175/* Enable or disable a range of breakpoint locations. BP_NUM is the
14176 number of the breakpoint, and BP_LOC_RANGE specifies the
14177 (inclusive) range of location numbers of that breakpoint to
14178 enable/disable. ENABLE specifies whether to enable or disable the
14179 location. */
14180
14181static void
14182enable_disable_breakpoint_location_range (int bp_num,
14183 std::pair<int, int> &bp_loc_range,
14184 bool enable)
14185{
14186 for (int i = bp_loc_range.first; i <= bp_loc_range.second; i++)
14187 enable_disable_bp_num_loc (bp_num, i, enable);
14188}
0d381245 14189
1900040c
MS
14190/* Set ignore-count of breakpoint number BPTNUM to COUNT.
14191 If from_tty is nonzero, it prints a message to that effect,
14192 which ends with a period (no newline). */
14193
c906108c 14194void
fba45db2 14195disable_breakpoint (struct breakpoint *bpt)
c906108c
SS
14196{
14197 /* Never disable a watchpoint scope breakpoint; we want to
14198 hit them when we leave scope so we can delete both the
14199 watchpoint and its scope breakpoint at that time. */
14200 if (bpt->type == bp_watchpoint_scope)
14201 return;
14202
b5de0fa7 14203 bpt->enable_state = bp_disabled;
c906108c 14204
b775012e
LM
14205 /* Mark breakpoint locations modified. */
14206 mark_breakpoint_modified (bpt);
14207
d248b706
KY
14208 if (target_supports_enable_disable_tracepoint ()
14209 && current_trace_status ()->running && is_tracepoint (bpt))
14210 {
14211 struct bp_location *location;
14212
14213 for (location = bpt->loc; location; location = location->next)
14214 target_disable_tracepoint (location);
14215 }
14216
44702360 14217 update_global_location_list (UGLL_DONT_INSERT);
c906108c 14218
76727919 14219 gdb::observers::breakpoint_modified.notify (bpt);
c906108c
SS
14220}
14221
d0fe4701
XR
14222/* Enable or disable the breakpoint(s) or breakpoint location(s)
14223 specified in ARGS. ARGS may be in any of the formats handled by
14224 extract_bp_number_and_location. ENABLE specifies whether to enable
14225 or disable the breakpoints/locations. */
14226
c906108c 14227static void
d0fe4701 14228enable_disable_command (const char *args, int from_tty, bool enable)
c906108c 14229{
c906108c 14230 if (args == 0)
46c6471b
PA
14231 {
14232 struct breakpoint *bpt;
14233
14234 ALL_BREAKPOINTS (bpt)
14235 if (user_breakpoint_p (bpt))
d0fe4701
XR
14236 {
14237 if (enable)
14238 enable_breakpoint (bpt);
14239 else
14240 disable_breakpoint (bpt);
14241 }
46c6471b 14242 }
9eaabc75 14243 else
0d381245 14244 {
cb791d59 14245 std::string num = extract_arg (&args);
9eaabc75 14246
cb791d59 14247 while (!num.empty ())
d248b706 14248 {
d0fe4701 14249 std::pair<int, int> bp_num_range, bp_loc_range;
9eaabc75 14250
cc638e86
PA
14251 extract_bp_number_and_location (num, bp_num_range, bp_loc_range);
14252
14253 if (bp_loc_range.first == bp_loc_range.second
14254 && bp_loc_range.first == 0)
d0fe4701 14255 {
cc638e86
PA
14256 /* Handle breakpoint ids with formats 'x' or 'x-z'. */
14257 map_breakpoint_number_range (bp_num_range,
14258 enable
14259 ? enable_breakpoint
14260 : disable_breakpoint);
14261 }
14262 else
14263 {
14264 /* Handle breakpoint ids with formats 'x.y' or
14265 'x.y-z'. */
14266 enable_disable_breakpoint_location_range
14267 (bp_num_range.first, bp_loc_range, enable);
b775012e 14268 }
9eaabc75 14269 num = extract_arg (&args);
d248b706 14270 }
0d381245 14271 }
c906108c
SS
14272}
14273
d0fe4701
XR
14274/* The disable command disables the specified breakpoints/locations
14275 (or all defined breakpoints) so they're no longer effective in
14276 stopping the inferior. ARGS may be in any of the forms defined in
14277 extract_bp_number_and_location. */
14278
14279static void
14280disable_command (const char *args, int from_tty)
14281{
14282 enable_disable_command (args, from_tty, false);
14283}
14284
c906108c 14285static void
816338b5
SS
14286enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
14287 int count)
c906108c 14288{
afe38095 14289 int target_resources_ok;
c906108c
SS
14290
14291 if (bpt->type == bp_hardware_breakpoint)
14292 {
14293 int i;
c5aa993b 14294 i = hw_breakpoint_used_count ();
53a5351d 14295 target_resources_ok =
d92524f1 14296 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
53a5351d 14297 i + 1, 0);
c906108c 14298 if (target_resources_ok == 0)
8a3fe4f8 14299 error (_("No hardware breakpoint support in the target."));
c906108c 14300 else if (target_resources_ok < 0)
8a3fe4f8 14301 error (_("Hardware breakpoints used exceeds limit."));
c906108c
SS
14302 }
14303
cc60f2e3 14304 if (is_watchpoint (bpt))
c906108c 14305 {
d07205c2 14306 /* Initialize it just to avoid a GCC false warning. */
f486487f 14307 enum enable_state orig_enable_state = bp_disabled;
dde02812 14308
a70b8144 14309 try
c906108c 14310 {
3a5c3e22
PA
14311 struct watchpoint *w = (struct watchpoint *) bpt;
14312
1e718ff1
TJB
14313 orig_enable_state = bpt->enable_state;
14314 bpt->enable_state = bp_enabled;
3a5c3e22 14315 update_watchpoint (w, 1 /* reparse */);
c906108c 14316 }
230d2906 14317 catch (const gdb_exception &e)
c5aa993b 14318 {
1e718ff1 14319 bpt->enable_state = orig_enable_state;
dde02812
ES
14320 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
14321 bpt->number);
14322 return;
c5aa993b 14323 }
c906108c 14324 }
0101ce28 14325
b775012e
LM
14326 bpt->enable_state = bp_enabled;
14327
14328 /* Mark breakpoint locations modified. */
14329 mark_breakpoint_modified (bpt);
14330
d248b706
KY
14331 if (target_supports_enable_disable_tracepoint ()
14332 && current_trace_status ()->running && is_tracepoint (bpt))
14333 {
14334 struct bp_location *location;
14335
14336 for (location = bpt->loc; location; location = location->next)
14337 target_enable_tracepoint (location);
14338 }
14339
b4c291bb 14340 bpt->disposition = disposition;
816338b5 14341 bpt->enable_count = count;
44702360 14342 update_global_location_list (UGLL_MAY_INSERT);
9c97429f 14343
76727919 14344 gdb::observers::breakpoint_modified.notify (bpt);
c906108c
SS
14345}
14346
fe3f5fa8 14347
c906108c 14348void
fba45db2 14349enable_breakpoint (struct breakpoint *bpt)
c906108c 14350{
816338b5 14351 enable_breakpoint_disp (bpt, bpt->disposition, 0);
51be5b68
PA
14352}
14353
d0fe4701
XR
14354/* The enable command enables the specified breakpoints/locations (or
14355 all defined breakpoints) so they once again become (or continue to
14356 be) effective in stopping the inferior. ARGS may be in any of the
14357 forms defined in extract_bp_number_and_location. */
c906108c 14358
c906108c 14359static void
981a3fb3 14360enable_command (const char *args, int from_tty)
c906108c 14361{
d0fe4701 14362 enable_disable_command (args, from_tty, true);
c906108c
SS
14363}
14364
c906108c 14365static void
4495129a 14366enable_once_command (const char *args, int from_tty)
c906108c 14367{
48649e1b
TT
14368 map_breakpoint_numbers
14369 (args, [&] (breakpoint *b)
14370 {
14371 iterate_over_related_breakpoints
14372 (b, [&] (breakpoint *bpt)
14373 {
14374 enable_breakpoint_disp (bpt, disp_disable, 1);
14375 });
14376 });
816338b5
SS
14377}
14378
14379static void
4495129a 14380enable_count_command (const char *args, int from_tty)
816338b5 14381{
b9d61307
SM
14382 int count;
14383
14384 if (args == NULL)
14385 error_no_arg (_("hit count"));
14386
14387 count = get_number (&args);
816338b5 14388
48649e1b
TT
14389 map_breakpoint_numbers
14390 (args, [&] (breakpoint *b)
14391 {
14392 iterate_over_related_breakpoints
14393 (b, [&] (breakpoint *bpt)
14394 {
14395 enable_breakpoint_disp (bpt, disp_disable, count);
14396 });
14397 });
c906108c
SS
14398}
14399
c906108c 14400static void
4495129a 14401enable_delete_command (const char *args, int from_tty)
c906108c 14402{
48649e1b
TT
14403 map_breakpoint_numbers
14404 (args, [&] (breakpoint *b)
14405 {
14406 iterate_over_related_breakpoints
14407 (b, [&] (breakpoint *bpt)
14408 {
14409 enable_breakpoint_disp (bpt, disp_del, 1);
14410 });
14411 });
c906108c
SS
14412}
14413\f
1f3b5d1b
PP
14414/* Invalidate last known value of any hardware watchpoint if
14415 the memory which that value represents has been written to by
14416 GDB itself. */
14417
14418static void
8de0566d
YQ
14419invalidate_bp_value_on_memory_change (struct inferior *inferior,
14420 CORE_ADDR addr, ssize_t len,
1f3b5d1b
PP
14421 const bfd_byte *data)
14422{
14423 struct breakpoint *bp;
14424
14425 ALL_BREAKPOINTS (bp)
14426 if (bp->enable_state == bp_enabled
3a5c3e22 14427 && bp->type == bp_hardware_watchpoint)
1f3b5d1b 14428 {
3a5c3e22 14429 struct watchpoint *wp = (struct watchpoint *) bp;
1f3b5d1b 14430
850645cf 14431 if (wp->val_valid && wp->val != nullptr)
3a5c3e22
PA
14432 {
14433 struct bp_location *loc;
14434
14435 for (loc = bp->loc; loc != NULL; loc = loc->next)
14436 if (loc->loc_type == bp_loc_hardware_watchpoint
14437 && loc->address + loc->length > addr
14438 && addr + len > loc->address)
14439 {
3a5c3e22 14440 wp->val = NULL;
4c1d86d9 14441 wp->val_valid = false;
3a5c3e22
PA
14442 }
14443 }
1f3b5d1b
PP
14444 }
14445}
14446
8181d85f
DJ
14447/* Create and insert a breakpoint for software single step. */
14448
14449void
6c95b8df 14450insert_single_step_breakpoint (struct gdbarch *gdbarch,
accd0bcd 14451 const address_space *aspace,
4a64f543 14452 CORE_ADDR next_pc)
8181d85f 14453{
7c16b83e
PA
14454 struct thread_info *tp = inferior_thread ();
14455 struct symtab_and_line sal;
14456 CORE_ADDR pc = next_pc;
8181d85f 14457
34b7e8a6
PA
14458 if (tp->control.single_step_breakpoints == NULL)
14459 {
14460 tp->control.single_step_breakpoints
5d5658a1 14461 = new_single_step_breakpoint (tp->global_num, gdbarch);
34b7e8a6 14462 }
8181d85f 14463
7c16b83e
PA
14464 sal = find_pc_line (pc, 0);
14465 sal.pc = pc;
14466 sal.section = find_pc_overlay (pc);
14467 sal.explicit_pc = 1;
34b7e8a6 14468 add_location_to_breakpoint (tp->control.single_step_breakpoints, &sal);
8181d85f 14469
7c16b83e 14470 update_global_location_list (UGLL_INSERT);
8181d85f
DJ
14471}
14472
93f9a11f
YQ
14473/* Insert single step breakpoints according to the current state. */
14474
14475int
14476insert_single_step_breakpoints (struct gdbarch *gdbarch)
14477{
f5ea389a 14478 struct regcache *regcache = get_current_regcache ();
a0ff9e1a 14479 std::vector<CORE_ADDR> next_pcs;
93f9a11f 14480
f5ea389a 14481 next_pcs = gdbarch_software_single_step (gdbarch, regcache);
93f9a11f 14482
a0ff9e1a 14483 if (!next_pcs.empty ())
93f9a11f 14484 {
f5ea389a 14485 struct frame_info *frame = get_current_frame ();
8b86c959 14486 const address_space *aspace = get_frame_address_space (frame);
93f9a11f 14487
a0ff9e1a 14488 for (CORE_ADDR pc : next_pcs)
93f9a11f
YQ
14489 insert_single_step_breakpoint (gdbarch, aspace, pc);
14490
93f9a11f
YQ
14491 return 1;
14492 }
14493 else
14494 return 0;
14495}
14496
34b7e8a6 14497/* See breakpoint.h. */
f02253f1
HZ
14498
14499int
7c16b83e 14500breakpoint_has_location_inserted_here (struct breakpoint *bp,
accd0bcd 14501 const address_space *aspace,
7c16b83e 14502 CORE_ADDR pc)
1aafd4da 14503{
7c16b83e 14504 struct bp_location *loc;
1aafd4da 14505
7c16b83e
PA
14506 for (loc = bp->loc; loc != NULL; loc = loc->next)
14507 if (loc->inserted
14508 && breakpoint_location_address_match (loc, aspace, pc))
14509 return 1;
1aafd4da 14510
7c16b83e 14511 return 0;
ef370185
JB
14512}
14513
14514/* Check whether a software single-step breakpoint is inserted at
14515 PC. */
14516
14517int
accd0bcd 14518single_step_breakpoint_inserted_here_p (const address_space *aspace,
ef370185
JB
14519 CORE_ADDR pc)
14520{
34b7e8a6
PA
14521 struct breakpoint *bpt;
14522
14523 ALL_BREAKPOINTS (bpt)
14524 {
14525 if (bpt->type == bp_single_step
14526 && breakpoint_has_location_inserted_here (bpt, aspace, pc))
14527 return 1;
14528 }
14529 return 0;
1aafd4da
UW
14530}
14531
1042e4c0
SS
14532/* Tracepoint-specific operations. */
14533
14534/* Set tracepoint count to NUM. */
14535static void
14536set_tracepoint_count (int num)
14537{
14538 tracepoint_count = num;
4fa62494 14539 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
1042e4c0
SS
14540}
14541
70221824 14542static void
0b39b52e 14543trace_command (const char *arg, int from_tty)
1042e4c0 14544{
ffc2605c
TT
14545 event_location_up location = string_to_event_location (&arg,
14546 current_language);
bac7c5cf
GB
14547 const struct breakpoint_ops *ops = breakpoint_ops_for_event_location
14548 (location.get (), true /* is_tracepoint */);
55aa24fb 14549
558a9d82 14550 create_breakpoint (get_current_arch (),
ffc2605c 14551 location.get (),
f00aae0f 14552 NULL, 0, arg, 1 /* parse arg */,
558a9d82
YQ
14553 0 /* tempflag */,
14554 bp_tracepoint /* type_wanted */,
14555 0 /* Ignore count */,
14556 pending_break_support,
14557 ops,
14558 from_tty,
14559 1 /* enabled */,
14560 0 /* internal */, 0);
1042e4c0
SS
14561}
14562
70221824 14563static void
0b39b52e 14564ftrace_command (const char *arg, int from_tty)
7a697b8d 14565{
ffc2605c
TT
14566 event_location_up location = string_to_event_location (&arg,
14567 current_language);
558a9d82 14568 create_breakpoint (get_current_arch (),
ffc2605c 14569 location.get (),
f00aae0f 14570 NULL, 0, arg, 1 /* parse arg */,
558a9d82
YQ
14571 0 /* tempflag */,
14572 bp_fast_tracepoint /* type_wanted */,
14573 0 /* Ignore count */,
14574 pending_break_support,
14575 &tracepoint_breakpoint_ops,
14576 from_tty,
14577 1 /* enabled */,
14578 0 /* internal */, 0);
0fb4aa4b
PA
14579}
14580
14581/* strace command implementation. Creates a static tracepoint. */
14582
70221824 14583static void
0b39b52e 14584strace_command (const char *arg, int from_tty)
0fb4aa4b 14585{
983af33b 14586 struct breakpoint_ops *ops;
ffc2605c 14587 event_location_up location;
983af33b
SDJ
14588
14589 /* Decide if we are dealing with a static tracepoint marker (`-m'),
14590 or with a normal static tracepoint. */
61012eef 14591 if (arg && startswith (arg, "-m") && isspace (arg[2]))
f00aae0f
KS
14592 {
14593 ops = &strace_marker_breakpoint_ops;
a20714ff 14594 location = new_linespec_location (&arg, symbol_name_match_type::FULL);
f00aae0f 14595 }
983af33b 14596 else
f00aae0f
KS
14597 {
14598 ops = &tracepoint_breakpoint_ops;
14599 location = string_to_event_location (&arg, current_language);
14600 }
983af33b 14601
558a9d82 14602 create_breakpoint (get_current_arch (),
ffc2605c 14603 location.get (),
f00aae0f 14604 NULL, 0, arg, 1 /* parse arg */,
558a9d82
YQ
14605 0 /* tempflag */,
14606 bp_static_tracepoint /* type_wanted */,
14607 0 /* Ignore count */,
14608 pending_break_support,
14609 ops,
14610 from_tty,
14611 1 /* enabled */,
14612 0 /* internal */, 0);
7a697b8d
SS
14613}
14614
409873ef
SS
14615/* Set up a fake reader function that gets command lines from a linked
14616 list that was acquired during tracepoint uploading. */
14617
14618static struct uploaded_tp *this_utp;
3149d8c1 14619static int next_cmd;
409873ef
SS
14620
14621static char *
14622read_uploaded_action (void)
14623{
a18ba4e4 14624 char *rslt = nullptr;
409873ef 14625
a18ba4e4
SM
14626 if (next_cmd < this_utp->cmd_strings.size ())
14627 {
67aa1f3c 14628 rslt = this_utp->cmd_strings[next_cmd].get ();
a18ba4e4
SM
14629 next_cmd++;
14630 }
409873ef
SS
14631
14632 return rslt;
14633}
14634
00bf0b85
SS
14635/* Given information about a tracepoint as recorded on a target (which
14636 can be either a live system or a trace file), attempt to create an
14637 equivalent GDB tracepoint. This is not a reliable process, since
14638 the target does not necessarily have all the information used when
14639 the tracepoint was originally defined. */
14640
d9b3f62e 14641struct tracepoint *
00bf0b85 14642create_tracepoint_from_upload (struct uploaded_tp *utp)
d5551862 14643{
f2fc3015
TT
14644 const char *addr_str;
14645 char small_buf[100];
d9b3f62e 14646 struct tracepoint *tp;
fd9b8c24 14647
409873ef 14648 if (utp->at_string)
67aa1f3c 14649 addr_str = utp->at_string.get ();
409873ef
SS
14650 else
14651 {
14652 /* In the absence of a source location, fall back to raw
14653 address. Since there is no way to confirm that the address
14654 means the same thing as when the trace was started, warn the
14655 user. */
3e43a32a
MS
14656 warning (_("Uploaded tracepoint %d has no "
14657 "source location, using raw address"),
409873ef 14658 utp->number);
8c042590 14659 xsnprintf (small_buf, sizeof (small_buf), "*%s", hex_string (utp->addr));
409873ef
SS
14660 addr_str = small_buf;
14661 }
14662
14663 /* There's not much we can do with a sequence of bytecodes. */
14664 if (utp->cond && !utp->cond_string)
3e43a32a
MS
14665 warning (_("Uploaded tracepoint %d condition "
14666 "has no source form, ignoring it"),
409873ef 14667 utp->number);
d5551862 14668
ffc2605c
TT
14669 event_location_up location = string_to_event_location (&addr_str,
14670 current_language);
8cdf0e15 14671 if (!create_breakpoint (get_current_arch (),
ffc2605c 14672 location.get (),
67aa1f3c 14673 utp->cond_string.get (), -1, addr_str,
e7e0cddf 14674 0 /* parse cond/thread */,
8cdf0e15 14675 0 /* tempflag */,
0fb4aa4b 14676 utp->type /* type_wanted */,
8cdf0e15
VP
14677 0 /* Ignore count */,
14678 pending_break_support,
348d480f 14679 &tracepoint_breakpoint_ops,
8cdf0e15 14680 0 /* from_tty */,
84f4c1fe 14681 utp->enabled /* enabled */,
44f238bb
PA
14682 0 /* internal */,
14683 CREATE_BREAKPOINT_FLAGS_INSERTED))
ffc2605c 14684 return NULL;
fd9b8c24 14685
409873ef 14686 /* Get the tracepoint we just created. */
fd9b8c24
PA
14687 tp = get_tracepoint (tracepoint_count);
14688 gdb_assert (tp != NULL);
d5551862 14689
00bf0b85
SS
14690 if (utp->pass > 0)
14691 {
8c042590 14692 xsnprintf (small_buf, sizeof (small_buf), "%d %d", utp->pass,
c1fc2657 14693 tp->number);
00bf0b85 14694
409873ef 14695 trace_pass_command (small_buf, 0);
00bf0b85
SS
14696 }
14697
409873ef
SS
14698 /* If we have uploaded versions of the original commands, set up a
14699 special-purpose "reader" function and call the usual command line
14700 reader, then pass the result to the breakpoint command-setting
14701 function. */
a18ba4e4 14702 if (!utp->cmd_strings.empty ())
00bf0b85 14703 {
12973681 14704 counted_command_line cmd_list;
00bf0b85 14705
409873ef 14706 this_utp = utp;
3149d8c1 14707 next_cmd = 0;
d5551862 14708
60b3cef2 14709 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL);
409873ef 14710
c1fc2657 14711 breakpoint_set_commands (tp, std::move (cmd_list));
00bf0b85 14712 }
a18ba4e4
SM
14713 else if (!utp->actions.empty ()
14714 || !utp->step_actions.empty ())
3e43a32a
MS
14715 warning (_("Uploaded tracepoint %d actions "
14716 "have no source form, ignoring them"),
409873ef 14717 utp->number);
00bf0b85 14718
f196051f 14719 /* Copy any status information that might be available. */
c1fc2657 14720 tp->hit_count = utp->hit_count;
f196051f
SS
14721 tp->traceframe_usage = utp->traceframe_usage;
14722
00bf0b85 14723 return tp;
d9b3f62e 14724}
00bf0b85 14725
1042e4c0
SS
14726/* Print information on tracepoint number TPNUM_EXP, or all if
14727 omitted. */
14728
14729static void
1d12d88f 14730info_tracepoints_command (const char *args, int from_tty)
1042e4c0 14731{
79a45e25 14732 struct ui_out *uiout = current_uiout;
e5a67952 14733 int num_printed;
1042e4c0 14734
5c458ae8 14735 num_printed = breakpoint_1 (args, false, is_tracepoint);
d77f58be
SS
14736
14737 if (num_printed == 0)
1042e4c0 14738 {
e5a67952 14739 if (args == NULL || *args == '\0')
112e8700 14740 uiout->message ("No tracepoints.\n");
d77f58be 14741 else
112e8700 14742 uiout->message ("No tracepoint matching '%s'.\n", args);
1042e4c0 14743 }
ad443146
SS
14744
14745 default_collect_info ();
1042e4c0
SS
14746}
14747
4a64f543 14748/* The 'enable trace' command enables tracepoints.
1042e4c0
SS
14749 Not supported by all targets. */
14750static void
5fed81ff 14751enable_trace_command (const char *args, int from_tty)
1042e4c0
SS
14752{
14753 enable_command (args, from_tty);
14754}
14755
4a64f543 14756/* The 'disable trace' command disables tracepoints.
1042e4c0
SS
14757 Not supported by all targets. */
14758static void
5fed81ff 14759disable_trace_command (const char *args, int from_tty)
1042e4c0
SS
14760{
14761 disable_command (args, from_tty);
14762}
14763
4a64f543 14764/* Remove a tracepoint (or all if no argument). */
1042e4c0 14765static void
4495129a 14766delete_trace_command (const char *arg, int from_tty)
1042e4c0 14767{
35df4500 14768 struct breakpoint *b, *b_tmp;
1042e4c0
SS
14769
14770 dont_repeat ();
14771
14772 if (arg == 0)
14773 {
14774 int breaks_to_delete = 0;
14775
14776 /* Delete all breakpoints if no argument.
14777 Do not delete internal or call-dummy breakpoints, these
4a64f543
MS
14778 have to be deleted with an explicit breakpoint number
14779 argument. */
1042e4c0 14780 ALL_TRACEPOINTS (b)
46c6471b 14781 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0
SS
14782 {
14783 breaks_to_delete = 1;
14784 break;
14785 }
1042e4c0
SS
14786
14787 /* Ask user only if there are some breakpoints to delete. */
14788 if (!from_tty
14789 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
14790 {
35df4500 14791 ALL_BREAKPOINTS_SAFE (b, b_tmp)
46c6471b 14792 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0 14793 delete_breakpoint (b);
1042e4c0
SS
14794 }
14795 }
14796 else
48649e1b 14797 map_breakpoint_numbers
b926417a 14798 (arg, [&] (breakpoint *br)
48649e1b 14799 {
b926417a 14800 iterate_over_related_breakpoints (br, delete_breakpoint);
48649e1b 14801 });
1042e4c0
SS
14802}
14803
197f0a60
TT
14804/* Helper function for trace_pass_command. */
14805
14806static void
d9b3f62e 14807trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
197f0a60 14808{
d9b3f62e 14809 tp->pass_count = count;
76727919 14810 gdb::observers::breakpoint_modified.notify (tp);
197f0a60
TT
14811 if (from_tty)
14812 printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
c1fc2657 14813 tp->number, count);
197f0a60
TT
14814}
14815
1042e4c0
SS
14816/* Set passcount for tracepoint.
14817
14818 First command argument is passcount, second is tracepoint number.
14819 If tracepoint number omitted, apply to most recently defined.
14820 Also accepts special argument "all". */
14821
14822static void
0b39b52e 14823trace_pass_command (const char *args, int from_tty)
1042e4c0 14824{
d9b3f62e 14825 struct tracepoint *t1;
0b39b52e 14826 ULONGEST count;
1042e4c0
SS
14827
14828 if (args == 0 || *args == 0)
3e43a32a
MS
14829 error (_("passcount command requires an "
14830 "argument (count + optional TP num)"));
1042e4c0 14831
0b39b52e 14832 count = strtoulst (args, &args, 10); /* Count comes first, then TP num. */
1042e4c0 14833
529480d0 14834 args = skip_spaces (args);
1042e4c0
SS
14835 if (*args && strncasecmp (args, "all", 3) == 0)
14836 {
d9b3f62e
PA
14837 struct breakpoint *b;
14838
1042e4c0 14839 args += 3; /* Skip special argument "all". */
1042e4c0
SS
14840 if (*args)
14841 error (_("Junk at end of arguments."));
1042e4c0 14842
d9b3f62e 14843 ALL_TRACEPOINTS (b)
197f0a60 14844 {
d9b3f62e 14845 t1 = (struct tracepoint *) b;
197f0a60
TT
14846 trace_pass_set_count (t1, count, from_tty);
14847 }
14848 }
14849 else if (*args == '\0')
1042e4c0 14850 {
5fa1d40e 14851 t1 = get_tracepoint_by_number (&args, NULL);
1042e4c0 14852 if (t1)
197f0a60
TT
14853 trace_pass_set_count (t1, count, from_tty);
14854 }
14855 else
14856 {
bfd28288
PA
14857 number_or_range_parser parser (args);
14858 while (!parser.finished ())
1042e4c0 14859 {
bfd28288 14860 t1 = get_tracepoint_by_number (&args, &parser);
197f0a60
TT
14861 if (t1)
14862 trace_pass_set_count (t1, count, from_tty);
1042e4c0
SS
14863 }
14864 }
1042e4c0
SS
14865}
14866
d9b3f62e 14867struct tracepoint *
1042e4c0
SS
14868get_tracepoint (int num)
14869{
14870 struct breakpoint *t;
14871
14872 ALL_TRACEPOINTS (t)
14873 if (t->number == num)
d9b3f62e 14874 return (struct tracepoint *) t;
1042e4c0
SS
14875
14876 return NULL;
14877}
14878
d5551862
SS
14879/* Find the tracepoint with the given target-side number (which may be
14880 different from the tracepoint number after disconnecting and
14881 reconnecting). */
14882
d9b3f62e 14883struct tracepoint *
d5551862
SS
14884get_tracepoint_by_number_on_target (int num)
14885{
d9b3f62e 14886 struct breakpoint *b;
d5551862 14887
d9b3f62e
PA
14888 ALL_TRACEPOINTS (b)
14889 {
14890 struct tracepoint *t = (struct tracepoint *) b;
14891
14892 if (t->number_on_target == num)
14893 return t;
14894 }
d5551862
SS
14895
14896 return NULL;
14897}
14898
1042e4c0 14899/* Utility: parse a tracepoint number and look it up in the list.
197f0a60 14900 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
5fa1d40e
YQ
14901 If the argument is missing, the most recent tracepoint
14902 (tracepoint_count) is returned. */
14903
d9b3f62e 14904struct tracepoint *
0b39b52e 14905get_tracepoint_by_number (const char **arg,
bfd28288 14906 number_or_range_parser *parser)
1042e4c0 14907{
1042e4c0
SS
14908 struct breakpoint *t;
14909 int tpnum;
0b39b52e 14910 const char *instring = arg == NULL ? NULL : *arg;
1042e4c0 14911
bfd28288 14912 if (parser != NULL)
197f0a60 14913 {
bfd28288
PA
14914 gdb_assert (!parser->finished ());
14915 tpnum = parser->get_number ();
197f0a60
TT
14916 }
14917 else if (arg == NULL || *arg == NULL || ! **arg)
5fa1d40e 14918 tpnum = tracepoint_count;
1042e4c0 14919 else
197f0a60 14920 tpnum = get_number (arg);
1042e4c0
SS
14921
14922 if (tpnum <= 0)
14923 {
14924 if (instring && *instring)
14925 printf_filtered (_("bad tracepoint number at or near '%s'\n"),
14926 instring);
14927 else
5fa1d40e 14928 printf_filtered (_("No previous tracepoint\n"));
1042e4c0
SS
14929 return NULL;
14930 }
14931
14932 ALL_TRACEPOINTS (t)
14933 if (t->number == tpnum)
14934 {
d9b3f62e 14935 return (struct tracepoint *) t;
1042e4c0
SS
14936 }
14937
1042e4c0
SS
14938 printf_unfiltered ("No tracepoint number %d.\n", tpnum);
14939 return NULL;
14940}
14941
d9b3f62e
PA
14942void
14943print_recreate_thread (struct breakpoint *b, struct ui_file *fp)
14944{
14945 if (b->thread != -1)
14946 fprintf_unfiltered (fp, " thread %d", b->thread);
14947
14948 if (b->task != 0)
14949 fprintf_unfiltered (fp, " task %d", b->task);
14950
14951 fprintf_unfiltered (fp, "\n");
14952}
14953
6149aea9
PA
14954/* Save information on user settable breakpoints (watchpoints, etc) to
14955 a new script file named FILENAME. If FILTER is non-NULL, call it
14956 on each breakpoint and only include the ones for which it returns
f2478a7e 14957 true. */
6149aea9 14958
1042e4c0 14959static void
4495129a 14960save_breakpoints (const char *filename, int from_tty,
f2478a7e 14961 bool (*filter) (const struct breakpoint *))
1042e4c0
SS
14962{
14963 struct breakpoint *tp;
6149aea9 14964 int any = 0;
6149aea9 14965 int extra_trace_bits = 0;
1042e4c0 14966
6149aea9
PA
14967 if (filename == 0 || *filename == 0)
14968 error (_("Argument required (file name in which to save)"));
1042e4c0
SS
14969
14970 /* See if we have anything to save. */
6149aea9 14971 ALL_BREAKPOINTS (tp)
1042e4c0 14972 {
6149aea9 14973 /* Skip internal and momentary breakpoints. */
09d682a4 14974 if (!user_breakpoint_p (tp))
6149aea9
PA
14975 continue;
14976
14977 /* If we have a filter, only save the breakpoints it accepts. */
14978 if (filter && !filter (tp))
14979 continue;
14980
14981 any = 1;
14982
14983 if (is_tracepoint (tp))
14984 {
14985 extra_trace_bits = 1;
14986
14987 /* We can stop searching. */
14988 break;
14989 }
1042e4c0 14990 }
6149aea9
PA
14991
14992 if (!any)
1042e4c0 14993 {
6149aea9 14994 warning (_("Nothing to save."));
1042e4c0
SS
14995 return;
14996 }
14997
ee0c3293 14998 gdb::unique_xmalloc_ptr<char> expanded_filename (tilde_expand (filename));
d7e74731
PA
14999
15000 stdio_file fp;
15001
ee0c3293 15002 if (!fp.open (expanded_filename.get (), "w"))
6149aea9 15003 error (_("Unable to open file '%s' for saving (%s)"),
ee0c3293 15004 expanded_filename.get (), safe_strerror (errno));
8bf6485c 15005
6149aea9 15006 if (extra_trace_bits)
d7e74731 15007 save_trace_state_variables (&fp);
8bf6485c 15008
6149aea9 15009 ALL_BREAKPOINTS (tp)
1042e4c0 15010 {
6149aea9 15011 /* Skip internal and momentary breakpoints. */
09d682a4 15012 if (!user_breakpoint_p (tp))
6149aea9 15013 continue;
8bf6485c 15014
6149aea9
PA
15015 /* If we have a filter, only save the breakpoints it accepts. */
15016 if (filter && !filter (tp))
15017 continue;
15018
d7e74731 15019 tp->ops->print_recreate (tp, &fp);
1042e4c0 15020
6149aea9
PA
15021 /* Note, we can't rely on tp->number for anything, as we can't
15022 assume the recreated breakpoint numbers will match. Use $bpnum
15023 instead. */
15024
15025 if (tp->cond_string)
d7e74731 15026 fp.printf (" condition $bpnum %s\n", tp->cond_string);
6149aea9
PA
15027
15028 if (tp->ignore_count)
d7e74731 15029 fp.printf (" ignore $bpnum %d\n", tp->ignore_count);
6149aea9 15030
2d9442cc 15031 if (tp->type != bp_dprintf && tp->commands)
1042e4c0 15032 {
d7e74731 15033 fp.puts (" commands\n");
a7bdde9e 15034
d7e74731 15035 current_uiout->redirect (&fp);
a70b8144 15036 try
1042e4c0 15037 {
d1b0a7bf 15038 print_command_lines (current_uiout, tp->commands.get (), 2);
a7bdde9e 15039 }
230d2906 15040 catch (const gdb_exception &ex)
492d29ea 15041 {
112e8700 15042 current_uiout->redirect (NULL);
eedc3f4f 15043 throw;
492d29ea 15044 }
1042e4c0 15045
112e8700 15046 current_uiout->redirect (NULL);
d7e74731 15047 fp.puts (" end\n");
1042e4c0 15048 }
6149aea9
PA
15049
15050 if (tp->enable_state == bp_disabled)
d7e74731 15051 fp.puts ("disable $bpnum\n");
6149aea9
PA
15052
15053 /* If this is a multi-location breakpoint, check if the locations
15054 should be individually disabled. Watchpoint locations are
15055 special, and not user visible. */
15056 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
15057 {
15058 struct bp_location *loc;
15059 int n = 1;
15060
15061 for (loc = tp->loc; loc != NULL; loc = loc->next, n++)
15062 if (!loc->enabled)
d7e74731 15063 fp.printf ("disable $bpnum.%d\n", n);
6149aea9 15064 }
1042e4c0 15065 }
8bf6485c 15066
6149aea9 15067 if (extra_trace_bits && *default_collect)
d7e74731 15068 fp.printf ("set default-collect %s\n", default_collect);
8bf6485c 15069
1042e4c0 15070 if (from_tty)
ee0c3293 15071 printf_filtered (_("Saved to file '%s'.\n"), expanded_filename.get ());
6149aea9
PA
15072}
15073
15074/* The `save breakpoints' command. */
15075
15076static void
4495129a 15077save_breakpoints_command (const char *args, int from_tty)
6149aea9
PA
15078{
15079 save_breakpoints (args, from_tty, NULL);
15080}
15081
15082/* The `save tracepoints' command. */
15083
15084static void
4495129a 15085save_tracepoints_command (const char *args, int from_tty)
6149aea9
PA
15086{
15087 save_breakpoints (args, from_tty, is_tracepoint);
1042e4c0
SS
15088}
15089
15090/* Create a vector of all tracepoints. */
15091
f51e0e20 15092std::vector<breakpoint *>
eeae04df 15093all_tracepoints (void)
1042e4c0 15094{
f51e0e20 15095 std::vector<breakpoint *> tp_vec;
1042e4c0
SS
15096 struct breakpoint *tp;
15097
15098 ALL_TRACEPOINTS (tp)
15099 {
f51e0e20 15100 tp_vec.push_back (tp);
1042e4c0
SS
15101 }
15102
15103 return tp_vec;
15104}
15105
c906108c 15106\f
629500fa
KS
15107/* This help string is used to consolidate all the help string for specifying
15108 locations used by several commands. */
15109
15110#define LOCATION_HELP_STRING \
15111"Linespecs are colon-separated lists of location parameters, such as\n\
15112source filename, function name, label name, and line number.\n\
15113Example: To specify the start of a label named \"the_top\" in the\n\
15114function \"fact\" in the file \"factorial.c\", use\n\
15115\"factorial.c:fact:the_top\".\n\
15116\n\
15117Address locations begin with \"*\" and specify an exact address in the\n\
15118program. Example: To specify the fourth byte past the start function\n\
15119\"main\", use \"*main + 4\".\n\
15120\n\
15121Explicit locations are similar to linespecs but use an option/argument\n\
15122syntax to specify location parameters.\n\
15123Example: To specify the start of the label named \"the_top\" in the\n\
15124function \"fact\" in the file \"factorial.c\", use \"-source factorial.c\n\
a20714ff
PA
15125-function fact -label the_top\".\n\
15126\n\
15127By default, a specified function is matched against the program's\n\
15128functions in all scopes. For C++, this means in all namespaces and\n\
15129classes. For Ada, this means in all packages. E.g., in C++,\n\
15130\"func()\" matches \"A::func()\", \"A::B::func()\", etc. The\n\
15131\"-qualified\" flag overrides this behavior, making GDB interpret the\n\
89549d7f 15132specified name as a complete fully-qualified name instead."
629500fa 15133
4a64f543
MS
15134/* This help string is used for the break, hbreak, tbreak and thbreak
15135 commands. It is defined as a macro to prevent duplication.
15136 COMMAND should be a string constant containing the name of the
15137 command. */
629500fa 15138
31e2b00f 15139#define BREAK_ARGS_HELP(command) \
fb7b5af4
SDJ
15140command" [PROBE_MODIFIER] [LOCATION] [thread THREADNUM] [if CONDITION]\n\
15141PROBE_MODIFIER shall be present if the command is to be placed in a\n\
15142probe point. Accepted values are `-probe' (for a generic, automatically\n\
d4777acb
JM
15143guessed probe type), `-probe-stap' (for a SystemTap probe) or \n\
15144`-probe-dtrace' (for a DTrace probe).\n\
629500fa
KS
15145LOCATION may be a linespec, address, or explicit location as described\n\
15146below.\n\
15147\n\
dc10affe
PA
15148With no LOCATION, uses current execution address of the selected\n\
15149stack frame. This is useful for breaking on return to a stack frame.\n\
31e2b00f
AS
15150\n\
15151THREADNUM is the number from \"info threads\".\n\
15152CONDITION is a boolean expression.\n\
89549d7f 15153\n" LOCATION_HELP_STRING "\n\n\
d41c0fc8
PA
15154Multiple breakpoints at one place are permitted, and useful if their\n\
15155conditions are different.\n\
31e2b00f
AS
15156\n\
15157Do \"help breakpoints\" for info on other commands dealing with breakpoints."
15158
44feb3ce
TT
15159/* List of subcommands for "catch". */
15160static struct cmd_list_element *catch_cmdlist;
15161
15162/* List of subcommands for "tcatch". */
15163static struct cmd_list_element *tcatch_cmdlist;
15164
9ac4176b 15165void
a121b7c1 15166add_catch_command (const char *name, const char *docstring,
eb4c3f4a 15167 cmd_const_sfunc_ftype *sfunc,
625e8578 15168 completer_ftype *completer,
44feb3ce
TT
15169 void *user_data_catch,
15170 void *user_data_tcatch)
15171{
15172 struct cmd_list_element *command;
15173
0450cc4c 15174 command = add_cmd (name, class_breakpoint, docstring,
44feb3ce
TT
15175 &catch_cmdlist);
15176 set_cmd_sfunc (command, sfunc);
15177 set_cmd_context (command, user_data_catch);
a96d9b2e 15178 set_cmd_completer (command, completer);
44feb3ce 15179
0450cc4c 15180 command = add_cmd (name, class_breakpoint, docstring,
44feb3ce
TT
15181 &tcatch_cmdlist);
15182 set_cmd_sfunc (command, sfunc);
15183 set_cmd_context (command, user_data_tcatch);
a96d9b2e 15184 set_cmd_completer (command, completer);
44feb3ce
TT
15185}
15186
84f4c1fe 15187struct breakpoint *
95da600f 15188iterate_over_breakpoints (gdb::function_view<bool (breakpoint *)> callback)
84f4c1fe 15189{
35df4500 15190 struct breakpoint *b, *b_tmp;
84f4c1fe 15191
35df4500 15192 ALL_BREAKPOINTS_SAFE (b, b_tmp)
84f4c1fe 15193 {
95da600f 15194 if (callback (b))
84f4c1fe
PM
15195 return b;
15196 }
15197
15198 return NULL;
15199}
15200
0574c78f
GB
15201/* Zero if any of the breakpoint's locations could be a location where
15202 functions have been inlined, nonzero otherwise. */
15203
15204static int
15205is_non_inline_function (struct breakpoint *b)
15206{
15207 /* The shared library event breakpoint is set on the address of a
15208 non-inline function. */
15209 if (b->type == bp_shlib_event)
15210 return 1;
15211
15212 return 0;
15213}
15214
15215/* Nonzero if the specified PC cannot be a location where functions
15216 have been inlined. */
15217
15218int
accd0bcd 15219pc_at_non_inline_function (const address_space *aspace, CORE_ADDR pc,
09ac7c10 15220 const struct target_waitstatus *ws)
0574c78f
GB
15221{
15222 struct breakpoint *b;
15223 struct bp_location *bl;
15224
15225 ALL_BREAKPOINTS (b)
15226 {
15227 if (!is_non_inline_function (b))
15228 continue;
15229
15230 for (bl = b->loc; bl != NULL; bl = bl->next)
15231 {
15232 if (!bl->shlib_disabled
09ac7c10 15233 && bpstat_check_location (bl, aspace, pc, ws))
0574c78f
GB
15234 return 1;
15235 }
15236 }
15237
15238 return 0;
15239}
15240
2f202fde
JK
15241/* Remove any references to OBJFILE which is going to be freed. */
15242
15243void
15244breakpoint_free_objfile (struct objfile *objfile)
15245{
15246 struct bp_location **locp, *loc;
15247
15248 ALL_BP_LOCATIONS (loc, locp)
eb822aa6 15249 if (loc->symtab != NULL && SYMTAB_OBJFILE (loc->symtab) == objfile)
2f202fde
JK
15250 loc->symtab = NULL;
15251}
15252
2060206e
PA
15253void
15254initialize_breakpoint_ops (void)
15255{
15256 static int initialized = 0;
15257
15258 struct breakpoint_ops *ops;
15259
15260 if (initialized)
15261 return;
15262 initialized = 1;
15263
15264 /* The breakpoint_ops structure to be inherit by all kinds of
15265 breakpoints (real breakpoints, i.e., user "break" breakpoints,
15266 internal and momentary breakpoints, etc.). */
15267 ops = &bkpt_base_breakpoint_ops;
15268 *ops = base_breakpoint_ops;
15269 ops->re_set = bkpt_re_set;
15270 ops->insert_location = bkpt_insert_location;
15271 ops->remove_location = bkpt_remove_location;
15272 ops->breakpoint_hit = bkpt_breakpoint_hit;
5f700d83 15273 ops->create_sals_from_location = bkpt_create_sals_from_location;
983af33b 15274 ops->create_breakpoints_sal = bkpt_create_breakpoints_sal;
5f700d83 15275 ops->decode_location = bkpt_decode_location;
2060206e
PA
15276
15277 /* The breakpoint_ops structure to be used in regular breakpoints. */
15278 ops = &bkpt_breakpoint_ops;
15279 *ops = bkpt_base_breakpoint_ops;
15280 ops->re_set = bkpt_re_set;
15281 ops->resources_needed = bkpt_resources_needed;
15282 ops->print_it = bkpt_print_it;
15283 ops->print_mention = bkpt_print_mention;
15284 ops->print_recreate = bkpt_print_recreate;
15285
15286 /* Ranged breakpoints. */
15287 ops = &ranged_breakpoint_ops;
15288 *ops = bkpt_breakpoint_ops;
15289 ops->breakpoint_hit = breakpoint_hit_ranged_breakpoint;
15290 ops->resources_needed = resources_needed_ranged_breakpoint;
15291 ops->print_it = print_it_ranged_breakpoint;
15292 ops->print_one = print_one_ranged_breakpoint;
15293 ops->print_one_detail = print_one_detail_ranged_breakpoint;
15294 ops->print_mention = print_mention_ranged_breakpoint;
15295 ops->print_recreate = print_recreate_ranged_breakpoint;
15296
15297 /* Internal breakpoints. */
15298 ops = &internal_breakpoint_ops;
15299 *ops = bkpt_base_breakpoint_ops;
15300 ops->re_set = internal_bkpt_re_set;
15301 ops->check_status = internal_bkpt_check_status;
15302 ops->print_it = internal_bkpt_print_it;
15303 ops->print_mention = internal_bkpt_print_mention;
15304
15305 /* Momentary breakpoints. */
15306 ops = &momentary_breakpoint_ops;
15307 *ops = bkpt_base_breakpoint_ops;
15308 ops->re_set = momentary_bkpt_re_set;
15309 ops->check_status = momentary_bkpt_check_status;
15310 ops->print_it = momentary_bkpt_print_it;
15311 ops->print_mention = momentary_bkpt_print_mention;
15312
55aa24fb
SDJ
15313 /* Probe breakpoints. */
15314 ops = &bkpt_probe_breakpoint_ops;
15315 *ops = bkpt_breakpoint_ops;
15316 ops->insert_location = bkpt_probe_insert_location;
15317 ops->remove_location = bkpt_probe_remove_location;
5f700d83
KS
15318 ops->create_sals_from_location = bkpt_probe_create_sals_from_location;
15319 ops->decode_location = bkpt_probe_decode_location;
55aa24fb 15320
2060206e
PA
15321 /* Watchpoints. */
15322 ops = &watchpoint_breakpoint_ops;
15323 *ops = base_breakpoint_ops;
15324 ops->re_set = re_set_watchpoint;
15325 ops->insert_location = insert_watchpoint;
15326 ops->remove_location = remove_watchpoint;
15327 ops->breakpoint_hit = breakpoint_hit_watchpoint;
15328 ops->check_status = check_status_watchpoint;
15329 ops->resources_needed = resources_needed_watchpoint;
15330 ops->works_in_software_mode = works_in_software_mode_watchpoint;
15331 ops->print_it = print_it_watchpoint;
15332 ops->print_mention = print_mention_watchpoint;
15333 ops->print_recreate = print_recreate_watchpoint;
427cd150 15334 ops->explains_signal = explains_signal_watchpoint;
2060206e
PA
15335
15336 /* Masked watchpoints. */
15337 ops = &masked_watchpoint_breakpoint_ops;
15338 *ops = watchpoint_breakpoint_ops;
15339 ops->insert_location = insert_masked_watchpoint;
15340 ops->remove_location = remove_masked_watchpoint;
15341 ops->resources_needed = resources_needed_masked_watchpoint;
15342 ops->works_in_software_mode = works_in_software_mode_masked_watchpoint;
15343 ops->print_it = print_it_masked_watchpoint;
15344 ops->print_one_detail = print_one_detail_masked_watchpoint;
15345 ops->print_mention = print_mention_masked_watchpoint;
15346 ops->print_recreate = print_recreate_masked_watchpoint;
15347
15348 /* Tracepoints. */
15349 ops = &tracepoint_breakpoint_ops;
15350 *ops = base_breakpoint_ops;
15351 ops->re_set = tracepoint_re_set;
15352 ops->breakpoint_hit = tracepoint_breakpoint_hit;
15353 ops->print_one_detail = tracepoint_print_one_detail;
15354 ops->print_mention = tracepoint_print_mention;
15355 ops->print_recreate = tracepoint_print_recreate;
5f700d83 15356 ops->create_sals_from_location = tracepoint_create_sals_from_location;
983af33b 15357 ops->create_breakpoints_sal = tracepoint_create_breakpoints_sal;
5f700d83 15358 ops->decode_location = tracepoint_decode_location;
983af33b 15359
55aa24fb
SDJ
15360 /* Probe tracepoints. */
15361 ops = &tracepoint_probe_breakpoint_ops;
15362 *ops = tracepoint_breakpoint_ops;
5f700d83
KS
15363 ops->create_sals_from_location = tracepoint_probe_create_sals_from_location;
15364 ops->decode_location = tracepoint_probe_decode_location;
55aa24fb 15365
983af33b
SDJ
15366 /* Static tracepoints with marker (`-m'). */
15367 ops = &strace_marker_breakpoint_ops;
15368 *ops = tracepoint_breakpoint_ops;
5f700d83 15369 ops->create_sals_from_location = strace_marker_create_sals_from_location;
983af33b 15370 ops->create_breakpoints_sal = strace_marker_create_breakpoints_sal;
5f700d83 15371 ops->decode_location = strace_marker_decode_location;
2060206e
PA
15372
15373 /* Fork catchpoints. */
15374 ops = &catch_fork_breakpoint_ops;
15375 *ops = base_breakpoint_ops;
15376 ops->insert_location = insert_catch_fork;
15377 ops->remove_location = remove_catch_fork;
15378 ops->breakpoint_hit = breakpoint_hit_catch_fork;
15379 ops->print_it = print_it_catch_fork;
15380 ops->print_one = print_one_catch_fork;
15381 ops->print_mention = print_mention_catch_fork;
15382 ops->print_recreate = print_recreate_catch_fork;
15383
15384 /* Vfork catchpoints. */
15385 ops = &catch_vfork_breakpoint_ops;
15386 *ops = base_breakpoint_ops;
15387 ops->insert_location = insert_catch_vfork;
15388 ops->remove_location = remove_catch_vfork;
15389 ops->breakpoint_hit = breakpoint_hit_catch_vfork;
15390 ops->print_it = print_it_catch_vfork;
15391 ops->print_one = print_one_catch_vfork;
15392 ops->print_mention = print_mention_catch_vfork;
15393 ops->print_recreate = print_recreate_catch_vfork;
15394
15395 /* Exec catchpoints. */
15396 ops = &catch_exec_breakpoint_ops;
15397 *ops = base_breakpoint_ops;
2060206e
PA
15398 ops->insert_location = insert_catch_exec;
15399 ops->remove_location = remove_catch_exec;
15400 ops->breakpoint_hit = breakpoint_hit_catch_exec;
15401 ops->print_it = print_it_catch_exec;
15402 ops->print_one = print_one_catch_exec;
15403 ops->print_mention = print_mention_catch_exec;
15404 ops->print_recreate = print_recreate_catch_exec;
15405
edcc5120
TT
15406 /* Solib-related catchpoints. */
15407 ops = &catch_solib_breakpoint_ops;
15408 *ops = base_breakpoint_ops;
edcc5120
TT
15409 ops->insert_location = insert_catch_solib;
15410 ops->remove_location = remove_catch_solib;
15411 ops->breakpoint_hit = breakpoint_hit_catch_solib;
15412 ops->check_status = check_status_catch_solib;
15413 ops->print_it = print_it_catch_solib;
15414 ops->print_one = print_one_catch_solib;
15415 ops->print_mention = print_mention_catch_solib;
15416 ops->print_recreate = print_recreate_catch_solib;
e7e0cddf
SS
15417
15418 ops = &dprintf_breakpoint_ops;
15419 *ops = bkpt_base_breakpoint_ops;
5c2b4418 15420 ops->re_set = dprintf_re_set;
e7e0cddf
SS
15421 ops->resources_needed = bkpt_resources_needed;
15422 ops->print_it = bkpt_print_it;
15423 ops->print_mention = bkpt_print_mention;
2d9442cc 15424 ops->print_recreate = dprintf_print_recreate;
9d6e6e84 15425 ops->after_condition_true = dprintf_after_condition_true;
cd1608cc 15426 ops->breakpoint_hit = dprintf_breakpoint_hit;
2060206e
PA
15427}
15428
8bfd80db
YQ
15429/* Chain containing all defined "enable breakpoint" subcommands. */
15430
15431static struct cmd_list_element *enablebreaklist = NULL;
15432
8588b356
SM
15433/* See breakpoint.h. */
15434
15435cmd_list_element *commands_cmd_element = nullptr;
15436
6c265988 15437void _initialize_breakpoint ();
c906108c 15438void
6c265988 15439_initialize_breakpoint ()
c906108c
SS
15440{
15441 struct cmd_list_element *c;
15442
2060206e
PA
15443 initialize_breakpoint_ops ();
15444
76727919
TT
15445 gdb::observers::solib_unloaded.attach (disable_breakpoints_in_unloaded_shlib);
15446 gdb::observers::free_objfile.attach (disable_breakpoints_in_freed_objfile);
15447 gdb::observers::memory_changed.attach (invalidate_bp_value_on_memory_change);
84acb35a 15448
c906108c
SS
15449 breakpoint_chain = 0;
15450 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
15451 before a breakpoint is set. */
15452 breakpoint_count = 0;
15453
1042e4c0
SS
15454 tracepoint_count = 0;
15455
1bedd215
AC
15456 add_com ("ignore", class_breakpoint, ignore_command, _("\
15457Set ignore-count of breakpoint number N to COUNT.\n\
15458Usage is `ignore N COUNT'."));
c906108c 15459
8588b356
SM
15460 commands_cmd_element = add_com ("commands", class_breakpoint,
15461 commands_command, _("\
18da0c51
MG
15462Set commands to be executed when the given breakpoints are hit.\n\
15463Give a space-separated breakpoint list as argument after \"commands\".\n\
15464A list element can be a breakpoint number (e.g. `5') or a range of numbers\n\
15465(e.g. `5-7').\n\
c906108c
SS
15466With no argument, the targeted breakpoint is the last one set.\n\
15467The commands themselves follow starting on the next line.\n\
15468Type a line containing \"end\" to indicate the end of them.\n\
15469Give \"silent\" as the first line to make the breakpoint silent;\n\
1bedd215 15470then no output is printed when it is hit, except what the commands print."));
c906108c 15471
d55637df 15472 c = add_com ("condition", class_breakpoint, condition_command, _("\
1bedd215 15473Specify breakpoint number N to break only if COND is true.\n\
c906108c 15474Usage is `condition N COND', where N is an integer and COND is an\n\
1bedd215 15475expression to be evaluated whenever breakpoint N is reached."));
d55637df 15476 set_cmd_completer (c, condition_completer);
c906108c 15477
1bedd215 15478 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
31e2b00f 15479Set a temporary breakpoint.\n\
c906108c
SS
15480Like \"break\" except the breakpoint is only temporary,\n\
15481so it will be deleted when hit. Equivalent to \"break\" followed\n\
31e2b00f
AS
15482by using \"enable delete\" on the breakpoint number.\n\
15483\n"
15484BREAK_ARGS_HELP ("tbreak")));
5ba2abeb 15485 set_cmd_completer (c, location_completer);
c94fdfd0 15486
1bedd215 15487 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
a3be7890 15488Set a hardware assisted breakpoint.\n\
c906108c 15489Like \"break\" except the breakpoint requires hardware support,\n\
31e2b00f
AS
15490some target hardware may not have this support.\n\
15491\n"
15492BREAK_ARGS_HELP ("hbreak")));
5ba2abeb 15493 set_cmd_completer (c, location_completer);
c906108c 15494
1bedd215 15495 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
31e2b00f 15496Set a temporary hardware assisted breakpoint.\n\
c906108c 15497Like \"hbreak\" except the breakpoint is only temporary,\n\
31e2b00f
AS
15498so it will be deleted when hit.\n\
15499\n"
15500BREAK_ARGS_HELP ("thbreak")));
5ba2abeb 15501 set_cmd_completer (c, location_completer);
c906108c 15502
1bedd215 15503 add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
590042fc
PW
15504Enable all or some breakpoints.\n\
15505Usage: enable [BREAKPOINTNUM]...\n\
c906108c
SS
15506Give breakpoint numbers (separated by spaces) as arguments.\n\
15507With no subcommand, breakpoints are enabled until you command otherwise.\n\
15508This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 15509With a subcommand you can enable temporarily."),
c906108c 15510 &enablelist, "enable ", 1, &cmdlist);
c906108c
SS
15511
15512 add_com_alias ("en", "enable", class_breakpoint, 1);
15513
84951ab5 15514 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
590042fc
PW
15515Enable all or some breakpoints.\n\
15516Usage: enable breakpoints [BREAKPOINTNUM]...\n\
c906108c
SS
15517Give breakpoint numbers (separated by spaces) as arguments.\n\
15518This is used to cancel the effect of the \"disable\" command.\n\
89549d7f 15519May be abbreviated to simply \"enable\"."),
c5aa993b 15520 &enablebreaklist, "enable breakpoints ", 1, &enablelist);
c906108c 15521
1a966eab 15522 add_cmd ("once", no_class, enable_once_command, _("\
590042fc
PW
15523Enable some breakpoints for one hit.\n\
15524Usage: enable breakpoints once BREAKPOINTNUM...\n\
1a966eab 15525If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
c906108c
SS
15526 &enablebreaklist);
15527
1a966eab 15528 add_cmd ("delete", no_class, enable_delete_command, _("\
590042fc
PW
15529Enable some breakpoints and delete when hit.\n\
15530Usage: enable breakpoints delete BREAKPOINTNUM...\n\
1a966eab 15531If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
15532 &enablebreaklist);
15533
816338b5 15534 add_cmd ("count", no_class, enable_count_command, _("\
590042fc
PW
15535Enable some breakpoints for COUNT hits.\n\
15536Usage: enable breakpoints count COUNT BREAKPOINTNUM...\n\
816338b5
SS
15537If a breakpoint is hit while enabled in this fashion,\n\
15538the count is decremented; when it reaches zero, the breakpoint is disabled."),
15539 &enablebreaklist);
15540
1a966eab 15541 add_cmd ("delete", no_class, enable_delete_command, _("\
590042fc
PW
15542Enable some breakpoints and delete when hit.\n\
15543Usage: enable delete BREAKPOINTNUM...\n\
1a966eab 15544If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
15545 &enablelist);
15546
1a966eab 15547 add_cmd ("once", no_class, enable_once_command, _("\
590042fc
PW
15548Enable some breakpoints for one hit.\n\
15549Usage: enable once BREAKPOINTNUM...\n\
1a966eab 15550If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
816338b5
SS
15551 &enablelist);
15552
15553 add_cmd ("count", no_class, enable_count_command, _("\
590042fc
PW
15554Enable some breakpoints for COUNT hits.\n\
15555Usage: enable count COUNT BREAKPOINTNUM...\n\
816338b5
SS
15556If a breakpoint is hit while enabled in this fashion,\n\
15557the count is decremented; when it reaches zero, the breakpoint is disabled."),
c906108c
SS
15558 &enablelist);
15559
1bedd215 15560 add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
590042fc
PW
15561Disable all or some breakpoints.\n\
15562Usage: disable [BREAKPOINTNUM]...\n\
c906108c
SS
15563Arguments are breakpoint numbers with spaces in between.\n\
15564To disable all breakpoints, give no argument.\n\
64b9b334 15565A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
c906108c
SS
15566 &disablelist, "disable ", 1, &cmdlist);
15567 add_com_alias ("dis", "disable", class_breakpoint, 1);
15568 add_com_alias ("disa", "disable", class_breakpoint, 1);
c906108c 15569
57b4f16e 15570 add_cmd ("breakpoints", class_breakpoint, disable_command, _("\
590042fc
PW
15571Disable all or some breakpoints.\n\
15572Usage: disable breakpoints [BREAKPOINTNUM]...\n\
c906108c
SS
15573Arguments are breakpoint numbers with spaces in between.\n\
15574To disable all breakpoints, give no argument.\n\
64b9b334 15575A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
1a966eab 15576This command may be abbreviated \"disable\"."),
c906108c
SS
15577 &disablelist);
15578
1bedd215 15579 add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
590042fc
PW
15580Delete all or some breakpoints.\n\
15581Usage: delete [BREAKPOINTNUM]...\n\
c906108c
SS
15582Arguments are breakpoint numbers with spaces in between.\n\
15583To delete all breakpoints, give no argument.\n\
15584\n\
590042fc 15585Also a prefix command for deletion of other GDB objects."),
c906108c
SS
15586 &deletelist, "delete ", 1, &cmdlist);
15587 add_com_alias ("d", "delete", class_breakpoint, 1);
7f198e01 15588 add_com_alias ("del", "delete", class_breakpoint, 1);
c906108c 15589
57b4f16e 15590 add_cmd ("breakpoints", class_breakpoint, delete_command, _("\
590042fc
PW
15591Delete all or some breakpoints or auto-display expressions.\n\
15592Usage: delete breakpoints [BREAKPOINTNUM]...\n\
c906108c
SS
15593Arguments are breakpoint numbers with spaces in between.\n\
15594To delete all breakpoints, give no argument.\n\
1a966eab 15595This command may be abbreviated \"delete\"."),
c906108c
SS
15596 &deletelist);
15597
1bedd215 15598 add_com ("clear", class_breakpoint, clear_command, _("\
629500fa
KS
15599Clear breakpoint at specified location.\n\
15600Argument may be a linespec, explicit, or address location as described below.\n\
1bedd215
AC
15601\n\
15602With no argument, clears all breakpoints in the line that the selected frame\n\
629500fa 15603is executing in.\n"
89549d7f 15604"\n" LOCATION_HELP_STRING "\n\n\
1bedd215 15605See also the \"delete\" command which clears breakpoints by number."));
a6cc4789 15606 add_com_alias ("cl", "clear", class_breakpoint, 1);
c906108c 15607
1bedd215 15608 c = add_com ("break", class_breakpoint, break_command, _("\
629500fa 15609Set breakpoint at specified location.\n"
31e2b00f 15610BREAK_ARGS_HELP ("break")));
5ba2abeb 15611 set_cmd_completer (c, location_completer);
c94fdfd0 15612
c906108c
SS
15613 add_com_alias ("b", "break", class_run, 1);
15614 add_com_alias ("br", "break", class_run, 1);
15615 add_com_alias ("bre", "break", class_run, 1);
15616 add_com_alias ("brea", "break", class_run, 1);
15617
c906108c
SS
15618 if (dbx_commands)
15619 {
1bedd215
AC
15620 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
15621Break in function/address or break at a line in the current file."),
c5aa993b
JM
15622 &stoplist, "stop ", 1, &cmdlist);
15623 add_cmd ("in", class_breakpoint, stopin_command,
1a966eab 15624 _("Break in function or address."), &stoplist);
c5aa993b 15625 add_cmd ("at", class_breakpoint, stopat_command,
1a966eab 15626 _("Break at a line in the current file."), &stoplist);
11db9430 15627 add_com ("status", class_info, info_breakpoints_command, _("\
1bedd215 15628Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
15629The \"Type\" column indicates one of:\n\
15630\tbreakpoint - normal breakpoint\n\
15631\twatchpoint - watchpoint\n\
15632The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15633the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15634breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
15635address and file/line number respectively.\n\
15636\n\
15637Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
15638are set to the address of the last breakpoint listed unless the command\n\
15639is prefixed with \"server \".\n\n\
c906108c 15640Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 15641breakpoint set."));
c906108c
SS
15642 }
15643
11db9430 15644 add_info ("breakpoints", info_breakpoints_command, _("\
e5a67952 15645Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
c906108c
SS
15646The \"Type\" column indicates one of:\n\
15647\tbreakpoint - normal breakpoint\n\
15648\twatchpoint - watchpoint\n\
15649The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15650the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15651breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
15652address and file/line number respectively.\n\
15653\n\
15654Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
15655are set to the address of the last breakpoint listed unless the command\n\
15656is prefixed with \"server \".\n\n\
c906108c 15657Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 15658breakpoint set."));
c906108c 15659
6b04bdb7
MS
15660 add_info_alias ("b", "breakpoints", 1);
15661
1a966eab
AC
15662 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
15663Status of all breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
15664The \"Type\" column indicates one of:\n\
15665\tbreakpoint - normal breakpoint\n\
15666\twatchpoint - watchpoint\n\
15667\tlongjmp - internal breakpoint used to step through longjmp()\n\
15668\tlongjmp resume - internal breakpoint at the target of longjmp()\n\
15669\tuntil - internal breakpoint used by the \"until\" command\n\
1a966eab
AC
15670\tfinish - internal breakpoint used by the \"finish\" command\n\
15671The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
c906108c
SS
15672the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15673breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1a966eab
AC
15674address and file/line number respectively.\n\
15675\n\
15676Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
15677are set to the address of the last breakpoint listed unless the command\n\
15678is prefixed with \"server \".\n\n\
c906108c 15679Convenience variable \"$bpnum\" contains the number of the last\n\
1a966eab 15680breakpoint set."),
c906108c
SS
15681 &maintenanceinfolist);
15682
3b6acaee 15683 add_basic_prefix_cmd ("catch", class_breakpoint, _("\
44feb3ce 15684Set catchpoints to catch events."),
3b6acaee
TT
15685 &catch_cmdlist, "catch ",
15686 0/*allow-unknown*/, &cmdlist);
44feb3ce 15687
3b6acaee 15688 add_basic_prefix_cmd ("tcatch", class_breakpoint, _("\
44feb3ce 15689Set temporary catchpoints to catch events."),
3b6acaee
TT
15690 &tcatch_cmdlist, "tcatch ",
15691 0/*allow-unknown*/, &cmdlist);
44feb3ce 15692
44feb3ce
TT
15693 add_catch_command ("fork", _("Catch calls to fork."),
15694 catch_fork_command_1,
a96d9b2e 15695 NULL,
44feb3ce
TT
15696 (void *) (uintptr_t) catch_fork_permanent,
15697 (void *) (uintptr_t) catch_fork_temporary);
15698 add_catch_command ("vfork", _("Catch calls to vfork."),
15699 catch_fork_command_1,
a96d9b2e 15700 NULL,
44feb3ce
TT
15701 (void *) (uintptr_t) catch_vfork_permanent,
15702 (void *) (uintptr_t) catch_vfork_temporary);
15703 add_catch_command ("exec", _("Catch calls to exec."),
15704 catch_exec_command_1,
a96d9b2e
SDJ
15705 NULL,
15706 CATCH_PERMANENT,
15707 CATCH_TEMPORARY);
edcc5120
TT
15708 add_catch_command ("load", _("Catch loads of shared libraries.\n\
15709Usage: catch load [REGEX]\n\
15710If REGEX is given, only stop for libraries matching the regular expression."),
15711 catch_load_command_1,
15712 NULL,
15713 CATCH_PERMANENT,
15714 CATCH_TEMPORARY);
15715 add_catch_command ("unload", _("Catch unloads of shared libraries.\n\
15716Usage: catch unload [REGEX]\n\
15717If REGEX is given, only stop for libraries matching the regular expression."),
15718 catch_unload_command_1,
15719 NULL,
15720 CATCH_PERMANENT,
15721 CATCH_TEMPORARY);
c5aa993b 15722
1bedd215
AC
15723 c = add_com ("watch", class_breakpoint, watch_command, _("\
15724Set a watchpoint for an expression.\n\
06a64a0b 15725Usage: watch [-l|-location] EXPRESSION\n\
c906108c 15726A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
15727an expression changes.\n\
15728If -l or -location is given, this evaluates EXPRESSION and watches\n\
15729the memory to which it refers."));
65d12d83 15730 set_cmd_completer (c, expression_completer);
c906108c 15731
1bedd215
AC
15732 c = add_com ("rwatch", class_breakpoint, rwatch_command, _("\
15733Set a read watchpoint for an expression.\n\
06a64a0b 15734Usage: rwatch [-l|-location] EXPRESSION\n\
c906108c 15735A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
15736an expression is read.\n\
15737If -l or -location is given, this evaluates EXPRESSION and watches\n\
15738the memory to which it refers."));
65d12d83 15739 set_cmd_completer (c, expression_completer);
c906108c 15740
1bedd215
AC
15741 c = add_com ("awatch", class_breakpoint, awatch_command, _("\
15742Set a watchpoint for an expression.\n\
06a64a0b 15743Usage: awatch [-l|-location] EXPRESSION\n\
c906108c 15744A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
15745an expression is either read or written.\n\
15746If -l or -location is given, this evaluates EXPRESSION and watches\n\
15747the memory to which it refers."));
65d12d83 15748 set_cmd_completer (c, expression_completer);
c906108c 15749
11db9430 15750 add_info ("watchpoints", info_watchpoints_command, _("\
e5a67952 15751Status of specified watchpoints (all watchpoints if no argument)."));
c906108c 15752
920d2a44
AC
15753 /* XXX: cagney/2005-02-23: This should be a boolean, and should
15754 respond to changes - contrary to the description. */
85c07804
AC
15755 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
15756 &can_use_hw_watchpoints, _("\
15757Set debugger's willingness to use watchpoint hardware."), _("\
15758Show debugger's willingness to use watchpoint hardware."), _("\
c906108c
SS
15759If zero, gdb will not use hardware for new watchpoints, even if\n\
15760such is available. (However, any hardware watchpoints that were\n\
15761created before setting this to nonzero, will continue to use watchpoint\n\
85c07804
AC
15762hardware.)"),
15763 NULL,
920d2a44 15764 show_can_use_hw_watchpoints,
85c07804 15765 &setlist, &showlist);
c906108c
SS
15766
15767 can_use_hw_watchpoints = 1;
fa8d40ab 15768
1042e4c0
SS
15769 /* Tracepoint manipulation commands. */
15770
15771 c = add_com ("trace", class_breakpoint, trace_command, _("\
629500fa 15772Set a tracepoint at specified location.\n\
1042e4c0
SS
15773\n"
15774BREAK_ARGS_HELP ("trace") "\n\
15775Do \"help tracepoints\" for info on other tracepoint commands."));
15776 set_cmd_completer (c, location_completer);
15777
57b4f16e
PW
15778 add_com_alias ("tp", "trace", class_breakpoint, 0);
15779 add_com_alias ("tr", "trace", class_breakpoint, 1);
15780 add_com_alias ("tra", "trace", class_breakpoint, 1);
15781 add_com_alias ("trac", "trace", class_breakpoint, 1);
1042e4c0 15782
7a697b8d 15783 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
629500fa 15784Set a fast tracepoint at specified location.\n\
7a697b8d
SS
15785\n"
15786BREAK_ARGS_HELP ("ftrace") "\n\
15787Do \"help tracepoints\" for info on other tracepoint commands."));
15788 set_cmd_completer (c, location_completer);
15789
0fb4aa4b 15790 c = add_com ("strace", class_breakpoint, strace_command, _("\
629500fa 15791Set a static tracepoint at location or marker.\n\
0fb4aa4b
PA
15792\n\
15793strace [LOCATION] [if CONDITION]\n\
629500fa
KS
15794LOCATION may be a linespec, explicit, or address location (described below) \n\
15795or -m MARKER_ID.\n\n\
15796If a marker id is specified, probe the marker with that name. With\n\
15797no LOCATION, uses current execution address of the selected stack frame.\n\
0fb4aa4b
PA
15798Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
15799This collects arbitrary user data passed in the probe point call to the\n\
15800tracing library. You can inspect it when analyzing the trace buffer,\n\
15801by printing the $_sdata variable like any other convenience variable.\n\
15802\n\
15803CONDITION is a boolean expression.\n\
89549d7f 15804\n" LOCATION_HELP_STRING "\n\n\
d41c0fc8
PA
15805Multiple tracepoints at one place are permitted, and useful if their\n\
15806conditions are different.\n\
0fb4aa4b
PA
15807\n\
15808Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
15809Do \"help tracepoints\" for info on other tracepoint commands."));
15810 set_cmd_completer (c, location_completer);
15811
11db9430 15812 add_info ("tracepoints", info_tracepoints_command, _("\
e5a67952 15813Status of specified tracepoints (all tracepoints if no argument).\n\
1042e4c0
SS
15814Convenience variable \"$tpnum\" contains the number of the\n\
15815last tracepoint set."));
15816
15817 add_info_alias ("tp", "tracepoints", 1);
15818
15819 add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
15820Delete specified tracepoints.\n\
15821Arguments are tracepoint numbers, separated by spaces.\n\
15822No argument means delete all tracepoints."),
15823 &deletelist);
7e20dfcd 15824 add_alias_cmd ("tr", "tracepoints", class_trace, 1, &deletelist);
1042e4c0
SS
15825
15826 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
15827Disable specified tracepoints.\n\
15828Arguments are tracepoint numbers, separated by spaces.\n\
15829No argument means disable all tracepoints."),
15830 &disablelist);
15831 deprecate_cmd (c, "disable");
15832
15833 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
15834Enable specified tracepoints.\n\
15835Arguments are tracepoint numbers, separated by spaces.\n\
15836No argument means enable all tracepoints."),
15837 &enablelist);
15838 deprecate_cmd (c, "enable");
15839
15840 add_com ("passcount", class_trace, trace_pass_command, _("\
15841Set the passcount for a tracepoint.\n\
15842The trace will end when the tracepoint has been passed 'count' times.\n\
15843Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
15844if TPNUM is omitted, passcount refers to the last tracepoint defined."));
15845
0743fc83
TT
15846 add_basic_prefix_cmd ("save", class_breakpoint,
15847 _("Save breakpoint definitions as a script."),
15848 &save_cmdlist, "save ",
15849 0/*allow-unknown*/, &cmdlist);
6149aea9
PA
15850
15851 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
15852Save current breakpoint definitions as a script.\n\
cce7e648 15853This includes all types of breakpoints (breakpoints, watchpoints,\n\
6149aea9
PA
15854catchpoints, tracepoints). Use the 'source' command in another debug\n\
15855session to restore them."),
15856 &save_cmdlist);
15857 set_cmd_completer (c, filename_completer);
15858
15859 c = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
1042e4c0 15860Save current tracepoint definitions as a script.\n\
6149aea9
PA
15861Use the 'source' command in another debug session to restore them."),
15862 &save_cmdlist);
1042e4c0
SS
15863 set_cmd_completer (c, filename_completer);
15864
6149aea9
PA
15865 c = add_com_alias ("save-tracepoints", "save tracepoints", class_trace, 0);
15866 deprecate_cmd (c, "save tracepoints");
15867
3b6acaee 15868 add_basic_prefix_cmd ("breakpoint", class_maintenance, _("\
590042fc 15869Breakpoint specific settings.\n\
fa8d40ab 15870Configure various breakpoint-specific variables such as\n\
590042fc 15871pending breakpoint behavior."),
3b6acaee
TT
15872 &breakpoint_set_cmdlist, "set breakpoint ",
15873 0/*allow-unknown*/, &setlist);
15874 add_show_prefix_cmd ("breakpoint", class_maintenance, _("\
590042fc 15875Breakpoint specific settings.\n\
fa8d40ab 15876Configure various breakpoint-specific variables such as\n\
590042fc 15877pending breakpoint behavior."),
3b6acaee
TT
15878 &breakpoint_show_cmdlist, "show breakpoint ",
15879 0/*allow-unknown*/, &showlist);
fa8d40ab 15880
7915a72c
AC
15881 add_setshow_auto_boolean_cmd ("pending", no_class,
15882 &pending_break_support, _("\
15883Set debugger's behavior regarding pending breakpoints."), _("\
15884Show debugger's behavior regarding pending breakpoints."), _("\
6e1d7d6c
AC
15885If on, an unrecognized breakpoint location will cause gdb to create a\n\
15886pending breakpoint. If off, an unrecognized breakpoint location results in\n\
15887an error. If auto, an unrecognized breakpoint location results in a\n\
7915a72c 15888user-query to see if a pending breakpoint should be created."),
2c5b56ce 15889 NULL,
920d2a44 15890 show_pending_break_support,
6e1d7d6c
AC
15891 &breakpoint_set_cmdlist,
15892 &breakpoint_show_cmdlist);
fa8d40ab
JJ
15893
15894 pending_break_support = AUTO_BOOLEAN_AUTO;
765dc015
VP
15895
15896 add_setshow_boolean_cmd ("auto-hw", no_class,
15897 &automatic_hardware_breakpoints, _("\
15898Set automatic usage of hardware breakpoints."), _("\
15899Show automatic usage of hardware breakpoints."), _("\
15900If set, the debugger will automatically use hardware breakpoints for\n\
15901breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
15902a warning will be emitted for such breakpoints."),
15903 NULL,
15904 show_automatic_hardware_breakpoints,
15905 &breakpoint_set_cmdlist,
15906 &breakpoint_show_cmdlist);
74960c60 15907
a25a5a45
PA
15908 add_setshow_boolean_cmd ("always-inserted", class_support,
15909 &always_inserted_mode, _("\
74960c60
VP
15910Set mode for inserting breakpoints."), _("\
15911Show mode for inserting breakpoints."), _("\
a25a5a45
PA
15912When this mode is on, breakpoints are inserted immediately as soon as\n\
15913they're created, kept inserted even when execution stops, and removed\n\
15914only when the user deletes them. When this mode is off (the default),\n\
15915breakpoints are inserted only when execution continues, and removed\n\
15916when execution stops."),
72d0e2c5
YQ
15917 NULL,
15918 &show_always_inserted_mode,
15919 &breakpoint_set_cmdlist,
15920 &breakpoint_show_cmdlist);
f1310107 15921
b775012e
LM
15922 add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
15923 condition_evaluation_enums,
15924 &condition_evaluation_mode_1, _("\
15925Set mode of breakpoint condition evaluation."), _("\
15926Show mode of breakpoint condition evaluation."), _("\
d1cda5d9 15927When this is set to \"host\", breakpoint conditions will be\n\
b775012e
LM
15928evaluated on the host's side by GDB. When it is set to \"target\",\n\
15929breakpoint conditions will be downloaded to the target (if the target\n\
15930supports such feature) and conditions will be evaluated on the target's side.\n\
15931If this is set to \"auto\" (default), this will be automatically set to\n\
15932\"target\" if it supports condition evaluation, otherwise it will\n\
15933be set to \"gdb\""),
15934 &set_condition_evaluation_mode,
15935 &show_condition_evaluation_mode,
15936 &breakpoint_set_cmdlist,
15937 &breakpoint_show_cmdlist);
15938
f1310107
TJB
15939 add_com ("break-range", class_breakpoint, break_range_command, _("\
15940Set a breakpoint for an address range.\n\
15941break-range START-LOCATION, END-LOCATION\n\
15942where START-LOCATION and END-LOCATION can be one of the following:\n\
15943 LINENUM, for that line in the current file,\n\
15944 FILE:LINENUM, for that line in that file,\n\
15945 +OFFSET, for that number of lines after the current line\n\
15946 or the start of the range\n\
15947 FUNCTION, for the first line in that function,\n\
15948 FILE:FUNCTION, to distinguish among like-named static functions.\n\
15949 *ADDRESS, for the instruction at that address.\n\
15950\n\
15951The breakpoint will stop execution of the inferior whenever it executes\n\
15952an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
15953range (including START-LOCATION and END-LOCATION)."));
15954
e7e0cddf 15955 c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
629500fa 15956Set a dynamic printf at specified location.\n\
e7e0cddf 15957dprintf location,format string,arg1,arg2,...\n\
629500fa
KS
15958location may be a linespec, explicit, or address location.\n"
15959"\n" LOCATION_HELP_STRING));
e7e0cddf
SS
15960 set_cmd_completer (c, location_completer);
15961
15962 add_setshow_enum_cmd ("dprintf-style", class_support,
15963 dprintf_style_enums, &dprintf_style, _("\
15964Set the style of usage for dynamic printf."), _("\
15965Show the style of usage for dynamic printf."), _("\
15966This setting chooses how GDB will do a dynamic printf.\n\
15967If the value is \"gdb\", then the printing is done by GDB to its own\n\
15968console, as with the \"printf\" command.\n\
15969If the value is \"call\", the print is done by calling a function in your\n\
15970program; by default printf(), but you can choose a different function or\n\
15971output stream by setting dprintf-function and dprintf-channel."),
15972 update_dprintf_commands, NULL,
15973 &setlist, &showlist);
15974
15975 dprintf_function = xstrdup ("printf");
15976 add_setshow_string_cmd ("dprintf-function", class_support,
15977 &dprintf_function, _("\
590042fc
PW
15978Set the function to use for dynamic printf."), _("\
15979Show the function to use for dynamic printf."), NULL,
e7e0cddf
SS
15980 update_dprintf_commands, NULL,
15981 &setlist, &showlist);
15982
15983 dprintf_channel = xstrdup ("");
15984 add_setshow_string_cmd ("dprintf-channel", class_support,
15985 &dprintf_channel, _("\
590042fc
PW
15986Set the channel to use for dynamic printf."), _("\
15987Show the channel to use for dynamic printf."), NULL,
e7e0cddf
SS
15988 update_dprintf_commands, NULL,
15989 &setlist, &showlist);
15990
d3ce09f5
SS
15991 add_setshow_boolean_cmd ("disconnected-dprintf", no_class,
15992 &disconnected_dprintf, _("\
15993Set whether dprintf continues after GDB disconnects."), _("\
15994Show whether dprintf continues after GDB disconnects."), _("\
15995Use this to let dprintf commands continue to hit and produce output\n\
15996even if GDB disconnects or detaches from the target."),
15997 NULL,
15998 NULL,
15999 &setlist, &showlist);
16000
16001 add_com ("agent-printf", class_vars, agent_printf_command, _("\
590042fc
PW
16002Target agent only formatted printing, like the C \"printf\" function.\n\
16003Usage: agent-printf \"format string\", ARG1, ARG2, ARG3, ..., ARGN\n\
16004This supports most C printf format specifications, like %s, %d, etc.\n\
16005This is useful for formatted output in user-defined commands."));
d3ce09f5 16006
491144b5 16007 automatic_hardware_breakpoints = true;
f3b1572e 16008
76727919
TT
16009 gdb::observers::about_to_proceed.attach (breakpoint_about_to_proceed);
16010 gdb::observers::thread_exit.attach (remove_threaded_breakpoints);
c906108c 16011}