]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/breakpoint.c
Fix advance/until and inline frames (PR gdb/26523)
[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 2037 {
1eb8556f
SM
2038 infrun_debug_printf ("skipping breakpoint: stepping past insn at: %s",
2039 paddress (bl->gdbarch, bl->address));
e558d7c1
PA
2040 return 0;
2041 }
31e77af2 2042
963f9c80
PA
2043 /* Don't insert watchpoints if we're trying to step past the
2044 instruction that triggered one. */
2045 if ((bl->loc_type == bp_loc_hardware_watchpoint)
2046 && stepping_past_nonsteppable_watchpoint ())
2047 {
1eb8556f
SM
2048 infrun_debug_printf ("stepping past non-steppable watchpoint. "
2049 "skipping watchpoint at %s:%d\n",
2050 paddress (bl->gdbarch, bl->address), bl->length);
963f9c80
PA
2051 return 0;
2052 }
2053
74960c60
VP
2054 return 1;
2055}
2056
934709f0
PW
2057/* Same as should_be_inserted but does the check assuming
2058 that the location is not duplicated. */
2059
2060static int
2061unduplicated_should_be_inserted (struct bp_location *bl)
2062{
2063 int result;
2064 const int save_duplicate = bl->duplicate;
2065
2066 bl->duplicate = 0;
2067 result = should_be_inserted (bl);
2068 bl->duplicate = save_duplicate;
2069 return result;
2070}
2071
b775012e
LM
2072/* Parses a conditional described by an expression COND into an
2073 agent expression bytecode suitable for evaluation
2074 by the bytecode interpreter. Return NULL if there was
2075 any error during parsing. */
2076
833177a4 2077static agent_expr_up
b775012e
LM
2078parse_cond_to_aexpr (CORE_ADDR scope, struct expression *cond)
2079{
833177a4 2080 if (cond == NULL)
b775012e
LM
2081 return NULL;
2082
833177a4
PA
2083 agent_expr_up aexpr;
2084
b775012e
LM
2085 /* We don't want to stop processing, so catch any errors
2086 that may show up. */
a70b8144 2087 try
b775012e 2088 {
036e657b 2089 aexpr = gen_eval_for_expr (scope, cond);
b775012e
LM
2090 }
2091
230d2906 2092 catch (const gdb_exception_error &ex)
b775012e
LM
2093 {
2094 /* If we got here, it means the condition could not be parsed to a valid
2095 bytecode expression and thus can't be evaluated on the target's side.
2096 It's no use iterating through the conditions. */
b775012e
LM
2097 }
2098
2099 /* We have a valid agent expression. */
2100 return aexpr;
2101}
2102
2103/* Based on location BL, create a list of breakpoint conditions to be
2104 passed on to the target. If we have duplicated locations with different
2105 conditions, we will add such conditions to the list. The idea is that the
2106 target will evaluate the list of conditions and will only notify GDB when
2107 one of them is true. */
2108
2109static void
2110build_target_condition_list (struct bp_location *bl)
2111{
2112 struct bp_location **locp = NULL, **loc2p;
2113 int null_condition_or_parse_error = 0;
2114 int modified = bl->needs_update;
2115 struct bp_location *loc;
2116
8b4f3082 2117 /* Release conditions left over from a previous insert. */
3cde5c42 2118 bl->target_info.conditions.clear ();
8b4f3082 2119
b775012e
LM
2120 /* This is only meaningful if the target is
2121 evaluating conditions and if the user has
2122 opted for condition evaluation on the target's
2123 side. */
2124 if (gdb_evaluates_breakpoint_condition_p ()
2125 || !target_supports_evaluation_of_breakpoint_conditions ())
2126 return;
2127
2128 /* Do a first pass to check for locations with no assigned
7f32a4d5
PA
2129 conditions or conditions that fail to parse to a valid agent
2130 expression bytecode. If any of these happen, then it's no use to
2131 send conditions to the target since this location will always
2132 trigger and generate a response back to GDB. Note we consider
2133 all locations at the same address irrespective of type, i.e.,
2134 even if the locations aren't considered duplicates (e.g.,
2135 software breakpoint and hardware breakpoint at the same
2136 address). */
b775012e
LM
2137 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2138 {
2139 loc = (*loc2p);
2140 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2141 {
2142 if (modified)
2143 {
b775012e
LM
2144 /* Re-parse the conditions since something changed. In that
2145 case we already freed the condition bytecodes (see
2146 force_breakpoint_reinsertion). We just
2147 need to parse the condition to bytecodes again. */
833177a4
PA
2148 loc->cond_bytecode = parse_cond_to_aexpr (bl->address,
2149 loc->cond.get ());
b775012e
LM
2150 }
2151
2152 /* If we have a NULL bytecode expression, it means something
2153 went wrong or we have a null condition expression. */
2154 if (!loc->cond_bytecode)
2155 {
2156 null_condition_or_parse_error = 1;
2157 break;
2158 }
2159 }
2160 }
2161
2162 /* If any of these happened, it means we will have to evaluate the conditions
2163 for the location's address on gdb's side. It is no use keeping bytecodes
2164 for all the other duplicate locations, thus we free all of them here.
2165
2166 This is so we have a finer control over which locations' conditions are
2167 being evaluated by GDB or the remote stub. */
2168 if (null_condition_or_parse_error)
2169 {
2170 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2171 {
2172 loc = (*loc2p);
2173 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2174 {
2175 /* Only go as far as the first NULL bytecode is
2176 located. */
2177 if (!loc->cond_bytecode)
2178 return;
2179
833177a4 2180 loc->cond_bytecode.reset ();
b775012e
LM
2181 }
2182 }
2183 }
2184
7f32a4d5
PA
2185 /* No NULL conditions or failed bytecode generation. Build a
2186 condition list for this location's address. If we have software
2187 and hardware locations at the same address, they aren't
2188 considered duplicates, but we still marge all the conditions
2189 anyway, as it's simpler, and doesn't really make a practical
2190 difference. */
b775012e
LM
2191 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2192 {
2193 loc = (*loc2p);
2194 if (loc->cond
2195 && is_breakpoint (loc->owner)
2196 && loc->pspace->num == bl->pspace->num
2197 && loc->owner->enable_state == bp_enabled
2198 && loc->enabled)
3cde5c42
PA
2199 {
2200 /* Add the condition to the vector. This will be used later
2201 to send the conditions to the target. */
2202 bl->target_info.conditions.push_back (loc->cond_bytecode.get ());
2203 }
b775012e
LM
2204 }
2205
2206 return;
2207}
2208
d3ce09f5
SS
2209/* Parses a command described by string CMD into an agent expression
2210 bytecode suitable for evaluation by the bytecode interpreter.
2211 Return NULL if there was any error during parsing. */
2212
833177a4 2213static agent_expr_up
d3ce09f5
SS
2214parse_cmd_to_aexpr (CORE_ADDR scope, char *cmd)
2215{
bbc13ae3
KS
2216 const char *cmdrest;
2217 const char *format_start, *format_end;
d3ce09f5
SS
2218 struct gdbarch *gdbarch = get_current_arch ();
2219
833177a4 2220 if (cmd == NULL)
d3ce09f5
SS
2221 return NULL;
2222
2223 cmdrest = cmd;
2224
2225 if (*cmdrest == ',')
2226 ++cmdrest;
f1735a53 2227 cmdrest = skip_spaces (cmdrest);
d3ce09f5
SS
2228
2229 if (*cmdrest++ != '"')
2230 error (_("No format string following the location"));
2231
2232 format_start = cmdrest;
2233
8e481c3b 2234 format_pieces fpieces (&cmdrest);
d3ce09f5
SS
2235
2236 format_end = cmdrest;
2237
2238 if (*cmdrest++ != '"')
2239 error (_("Bad format string, non-terminated '\"'."));
2240
f1735a53 2241 cmdrest = skip_spaces (cmdrest);
d3ce09f5
SS
2242
2243 if (!(*cmdrest == ',' || *cmdrest == '\0'))
2244 error (_("Invalid argument syntax"));
2245
2246 if (*cmdrest == ',')
2247 cmdrest++;
f1735a53 2248 cmdrest = skip_spaces (cmdrest);
d3ce09f5
SS
2249
2250 /* For each argument, make an expression. */
2251
8e481c3b 2252 std::vector<struct expression *> argvec;
d3ce09f5
SS
2253 while (*cmdrest != '\0')
2254 {
bbc13ae3 2255 const char *cmd1;
d3ce09f5
SS
2256
2257 cmd1 = cmdrest;
4d01a485 2258 expression_up expr = parse_exp_1 (&cmd1, scope, block_for_pc (scope), 1);
8e481c3b 2259 argvec.push_back (expr.release ());
d3ce09f5
SS
2260 cmdrest = cmd1;
2261 if (*cmdrest == ',')
2262 ++cmdrest;
2263 }
2264
833177a4
PA
2265 agent_expr_up aexpr;
2266
d3ce09f5
SS
2267 /* We don't want to stop processing, so catch any errors
2268 that may show up. */
a70b8144 2269 try
d3ce09f5 2270 {
036e657b
JB
2271 aexpr = gen_printf (scope, gdbarch, 0, 0,
2272 format_start, format_end - format_start,
8e481c3b 2273 argvec.size (), argvec.data ());
d3ce09f5 2274 }
230d2906 2275 catch (const gdb_exception_error &ex)
d3ce09f5
SS
2276 {
2277 /* If we got here, it means the command could not be parsed to a valid
2278 bytecode expression and thus can't be evaluated on the target's side.
2279 It's no use iterating through the other commands. */
d3ce09f5 2280 }
492d29ea 2281
d3ce09f5
SS
2282 /* We have a valid agent expression, return it. */
2283 return aexpr;
2284}
2285
2286/* Based on location BL, create a list of breakpoint commands to be
2287 passed on to the target. If we have duplicated locations with
2288 different commands, we will add any such to the list. */
2289
2290static void
2291build_target_command_list (struct bp_location *bl)
2292{
2293 struct bp_location **locp = NULL, **loc2p;
2294 int null_command_or_parse_error = 0;
2295 int modified = bl->needs_update;
2296 struct bp_location *loc;
2297
3cde5c42
PA
2298 /* Clear commands left over from a previous insert. */
2299 bl->target_info.tcommands.clear ();
8b4f3082 2300
41fac0cf 2301 if (!target_can_run_breakpoint_commands ())
d3ce09f5
SS
2302 return;
2303
41fac0cf
PA
2304 /* For now, limit to agent-style dprintf breakpoints. */
2305 if (dprintf_style != dprintf_style_agent)
d3ce09f5
SS
2306 return;
2307
7f32a4d5
PA
2308 /* For now, if we have any location at the same address that isn't a
2309 dprintf, don't install the target-side commands, as that would
2310 make the breakpoint not be reported to the core, and we'd lose
41fac0cf
PA
2311 control. */
2312 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2313 {
2314 loc = (*loc2p);
2315 if (is_breakpoint (loc->owner)
2316 && loc->pspace->num == bl->pspace->num
2317 && loc->owner->type != bp_dprintf)
2318 return;
2319 }
2320
d3ce09f5
SS
2321 /* Do a first pass to check for locations with no assigned
2322 conditions or conditions that fail to parse to a valid agent expression
2323 bytecode. If any of these happen, then it's no use to send conditions
2324 to the target since this location will always trigger and generate a
2325 response back to GDB. */
2326 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2327 {
2328 loc = (*loc2p);
2329 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2330 {
2331 if (modified)
2332 {
d3ce09f5
SS
2333 /* Re-parse the commands since something changed. In that
2334 case we already freed the command bytecodes (see
2335 force_breakpoint_reinsertion). We just
2336 need to parse the command to bytecodes again. */
833177a4
PA
2337 loc->cmd_bytecode
2338 = parse_cmd_to_aexpr (bl->address,
2339 loc->owner->extra_string);
d3ce09f5
SS
2340 }
2341
2342 /* If we have a NULL bytecode expression, it means something
2343 went wrong or we have a null command expression. */
2344 if (!loc->cmd_bytecode)
2345 {
2346 null_command_or_parse_error = 1;
2347 break;
2348 }
2349 }
2350 }
2351
2352 /* If anything failed, then we're not doing target-side commands,
2353 and so clean up. */
2354 if (null_command_or_parse_error)
2355 {
2356 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2357 {
2358 loc = (*loc2p);
2359 if (is_breakpoint (loc->owner)
2360 && loc->pspace->num == bl->pspace->num)
2361 {
2362 /* Only go as far as the first NULL bytecode is
2363 located. */
40fb6c5e 2364 if (loc->cmd_bytecode == NULL)
d3ce09f5
SS
2365 return;
2366
833177a4 2367 loc->cmd_bytecode.reset ();
d3ce09f5
SS
2368 }
2369 }
2370 }
2371
7f32a4d5
PA
2372 /* No NULL commands or failed bytecode generation. Build a command
2373 list for all duplicate locations at this location's address.
2374 Note that here we must care for whether the breakpoint location
2375 types are considered duplicates, otherwise, say, if we have a
2376 software and hardware location at the same address, the target
2377 could end up running the commands twice. For the moment, we only
2378 support targets-side commands with dprintf, but it doesn't hurt
2379 to be pedantically correct in case that changes. */
d3ce09f5
SS
2380 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2381 {
2382 loc = (*loc2p);
7f32a4d5
PA
2383 if (breakpoint_locations_match (bl, loc)
2384 && loc->owner->extra_string
d3ce09f5
SS
2385 && is_breakpoint (loc->owner)
2386 && loc->pspace->num == bl->pspace->num
2387 && loc->owner->enable_state == bp_enabled
2388 && loc->enabled)
3cde5c42
PA
2389 {
2390 /* Add the command to the vector. This will be used later
2391 to send the commands to the target. */
2392 bl->target_info.tcommands.push_back (loc->cmd_bytecode.get ());
2393 }
d3ce09f5
SS
2394 }
2395
2396 bl->target_info.persist = 0;
2397 /* Maybe flag this location as persistent. */
2398 if (bl->owner->type == bp_dprintf && disconnected_dprintf)
2399 bl->target_info.persist = 1;
2400}
2401
833b7ab5
YQ
2402/* Return the kind of breakpoint on address *ADDR. Get the kind
2403 of breakpoint according to ADDR except single-step breakpoint.
2404 Get the kind of single-step breakpoint according to the current
2405 registers state. */
cd6c3b4f
YQ
2406
2407static int
2408breakpoint_kind (struct bp_location *bl, CORE_ADDR *addr)
2409{
833b7ab5
YQ
2410 if (bl->owner->type == bp_single_step)
2411 {
2412 struct thread_info *thr = find_thread_global_id (bl->owner->thread);
2413 struct regcache *regcache;
2414
00431a78 2415 regcache = get_thread_regcache (thr);
833b7ab5
YQ
2416
2417 return gdbarch_breakpoint_kind_from_current_state (bl->gdbarch,
2418 regcache, addr);
2419 }
2420 else
2421 return gdbarch_breakpoint_kind_from_pc (bl->gdbarch, addr);
cd6c3b4f
YQ
2422}
2423
35df4500
TJB
2424/* Insert a low-level "breakpoint" of some type. BL is the breakpoint
2425 location. Any error messages are printed to TMP_ERROR_STREAM; and
3fbb6ffa 2426 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
c30eee59
TJB
2427 Returns 0 for success, 1 if the bp_location type is not supported or
2428 -1 for failure.
879bfdc2 2429
4a64f543
MS
2430 NOTE drow/2003-09-09: This routine could be broken down to an
2431 object-style method for each breakpoint or catchpoint type. */
26bb91f3 2432static int
35df4500 2433insert_bp_location (struct bp_location *bl,
26bb91f3 2434 struct ui_file *tmp_error_stream,
3fbb6ffa 2435 int *disabled_breaks,
dd61ec5c
MW
2436 int *hw_breakpoint_error,
2437 int *hw_bp_error_explained_already)
879bfdc2 2438{
cc06b668 2439 gdb_exception bp_excpt;
879bfdc2 2440
b775012e 2441 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
879bfdc2
DJ
2442 return 0;
2443
35c63cd8
JB
2444 /* Note we don't initialize bl->target_info, as that wipes out
2445 the breakpoint location's shadow_contents if the breakpoint
2446 is still inserted at that location. This in turn breaks
2447 target_read_memory which depends on these buffers when
2448 a memory read is requested at the breakpoint location:
2449 Once the target_info has been wiped, we fail to see that
2450 we have a breakpoint inserted at that address and thus
2451 read the breakpoint instead of returning the data saved in
2452 the breakpoint location's shadow contents. */
0d5ed153 2453 bl->target_info.reqstd_address = bl->address;
35df4500 2454 bl->target_info.placed_address_space = bl->pspace->aspace;
f1310107 2455 bl->target_info.length = bl->length;
8181d85f 2456
b775012e
LM
2457 /* When working with target-side conditions, we must pass all the conditions
2458 for the same breakpoint address down to the target since GDB will not
2459 insert those locations. With a list of breakpoint conditions, the target
2460 can decide when to stop and notify GDB. */
2461
2462 if (is_breakpoint (bl->owner))
2463 {
2464 build_target_condition_list (bl);
d3ce09f5
SS
2465 build_target_command_list (bl);
2466 /* Reset the modification marker. */
b775012e
LM
2467 bl->needs_update = 0;
2468 }
2469
7f32a4d5
PA
2470 /* If "set breakpoint auto-hw" is "on" and a software breakpoint was
2471 set at a read-only address, then a breakpoint location will have
2472 been changed to hardware breakpoint before we get here. If it is
2473 "off" however, error out before actually trying to insert the
2474 breakpoint, with a nicer error message. */
35df4500 2475 if (bl->loc_type == bp_loc_software_breakpoint
7f32a4d5 2476 && !automatic_hardware_breakpoints)
879bfdc2 2477 {
7f32a4d5 2478 mem_region *mr = lookup_mem_region (bl->address);
cc59ec59 2479
7f32a4d5
PA
2480 if (mr != nullptr && mr->attrib.mode != MEM_RW)
2481 {
2482 fprintf_unfiltered (tmp_error_stream,
2483 _("Cannot insert breakpoint %d.\n"
2484 "Cannot set software breakpoint "
2485 "at read-only address %s\n"),
2486 bl->owner->number,
2487 paddress (bl->gdbarch, bl->address));
2488 return 1;
765dc015 2489 }
7f32a4d5
PA
2490 }
2491
2492 if (bl->loc_type == bp_loc_software_breakpoint
2493 || bl->loc_type == bp_loc_hardware_breakpoint)
2494 {
879bfdc2
DJ
2495 /* First check to see if we have to handle an overlay. */
2496 if (overlay_debugging == ovly_off
35df4500
TJB
2497 || bl->section == NULL
2498 || !(section_is_overlay (bl->section)))
879bfdc2
DJ
2499 {
2500 /* No overlay handling: just set the breakpoint. */
a70b8144 2501 try
dd61ec5c 2502 {
0000e5cc
PA
2503 int val;
2504
dd61ec5c 2505 val = bl->owner->ops->insert_location (bl);
0000e5cc 2506 if (val)
688fca4f 2507 bp_excpt = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
dd61ec5c 2508 }
94aeb44b 2509 catch (gdb_exception &e)
dd61ec5c 2510 {
94aeb44b 2511 bp_excpt = std::move (e);
dd61ec5c 2512 }
879bfdc2
DJ
2513 }
2514 else
2515 {
4a64f543 2516 /* This breakpoint is in an overlay section.
879bfdc2
DJ
2517 Shall we set a breakpoint at the LMA? */
2518 if (!overlay_events_enabled)
2519 {
2520 /* Yes -- overlay event support is not active,
2521 so we must try to set a breakpoint at the LMA.
2522 This will not work for a hardware breakpoint. */
35df4500 2523 if (bl->loc_type == bp_loc_hardware_breakpoint)
8a3fe4f8 2524 warning (_("hardware breakpoint %d not supported in overlay!"),
35df4500 2525 bl->owner->number);
879bfdc2
DJ
2526 else
2527 {
35df4500
TJB
2528 CORE_ADDR addr = overlay_unmapped_address (bl->address,
2529 bl->section);
879bfdc2 2530 /* Set a software (trap) breakpoint at the LMA. */
35df4500 2531 bl->overlay_target_info = bl->target_info;
0d5ed153 2532 bl->overlay_target_info.reqstd_address = addr;
0000e5cc
PA
2533
2534 /* No overlay handling: just set the breakpoint. */
a70b8144 2535 try
0000e5cc
PA
2536 {
2537 int val;
2538
579c6ad9 2539 bl->overlay_target_info.kind
cd6c3b4f
YQ
2540 = breakpoint_kind (bl, &addr);
2541 bl->overlay_target_info.placed_address = addr;
0000e5cc
PA
2542 val = target_insert_breakpoint (bl->gdbarch,
2543 &bl->overlay_target_info);
2544 if (val)
688fca4f
PA
2545 bp_excpt
2546 = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
0000e5cc 2547 }
94aeb44b 2548 catch (gdb_exception &e)
0000e5cc 2549 {
94aeb44b 2550 bp_excpt = std::move (e);
0000e5cc
PA
2551 }
2552
688fca4f 2553 if (bp_excpt.reason != 0)
99361f52 2554 fprintf_unfiltered (tmp_error_stream,
3e43a32a
MS
2555 "Overlay breakpoint %d "
2556 "failed: in ROM?\n",
35df4500 2557 bl->owner->number);
879bfdc2
DJ
2558 }
2559 }
2560 /* Shall we set a breakpoint at the VMA? */
35df4500 2561 if (section_is_mapped (bl->section))
879bfdc2
DJ
2562 {
2563 /* Yes. This overlay section is mapped into memory. */
a70b8144 2564 try
dd61ec5c 2565 {
0000e5cc
PA
2566 int val;
2567
dd61ec5c 2568 val = bl->owner->ops->insert_location (bl);
0000e5cc 2569 if (val)
688fca4f 2570 bp_excpt = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
dd61ec5c 2571 }
94aeb44b 2572 catch (gdb_exception &e)
dd61ec5c 2573 {
94aeb44b 2574 bp_excpt = std::move (e);
dd61ec5c 2575 }
879bfdc2
DJ
2576 }
2577 else
2578 {
2579 /* No. This breakpoint will not be inserted.
2580 No error, but do not mark the bp as 'inserted'. */
2581 return 0;
2582 }
2583 }
2584
688fca4f 2585 if (bp_excpt.reason != 0)
879bfdc2
DJ
2586 {
2587 /* Can't set the breakpoint. */
0000e5cc
PA
2588
2589 /* In some cases, we might not be able to insert a
2590 breakpoint in a shared library that has already been
2591 removed, but we have not yet processed the shlib unload
2592 event. Unfortunately, some targets that implement
076855f9
PA
2593 breakpoint insertion themselves can't tell why the
2594 breakpoint insertion failed (e.g., the remote target
2595 doesn't define error codes), so we must treat generic
2596 errors as memory errors. */
688fca4f
PA
2597 if (bp_excpt.reason == RETURN_ERROR
2598 && (bp_excpt.error == GENERIC_ERROR
2599 || bp_excpt.error == MEMORY_ERROR)
076855f9 2600 && bl->loc_type == bp_loc_software_breakpoint
08351840 2601 && (solib_name_from_address (bl->pspace, bl->address)
d03de421
PA
2602 || shared_objfile_contains_address_p (bl->pspace,
2603 bl->address)))
879bfdc2 2604 {
4a64f543 2605 /* See also: disable_breakpoints_in_shlibs. */
35df4500 2606 bl->shlib_disabled = 1;
76727919 2607 gdb::observers::breakpoint_modified.notify (bl->owner);
3fbb6ffa
TJB
2608 if (!*disabled_breaks)
2609 {
2610 fprintf_unfiltered (tmp_error_stream,
2611 "Cannot insert breakpoint %d.\n",
2612 bl->owner->number);
2613 fprintf_unfiltered (tmp_error_stream,
2614 "Temporarily disabling shared "
2615 "library breakpoints:\n");
2616 }
2617 *disabled_breaks = 1;
879bfdc2 2618 fprintf_unfiltered (tmp_error_stream,
35df4500 2619 "breakpoint #%d\n", bl->owner->number);
0000e5cc 2620 return 0;
879bfdc2
DJ
2621 }
2622 else
879bfdc2 2623 {
35df4500 2624 if (bl->loc_type == bp_loc_hardware_breakpoint)
879bfdc2 2625 {
0000e5cc 2626 *hw_breakpoint_error = 1;
688fca4f 2627 *hw_bp_error_explained_already = bp_excpt.message != NULL;
dd61ec5c
MW
2628 fprintf_unfiltered (tmp_error_stream,
2629 "Cannot insert hardware breakpoint %d%s",
688fca4f
PA
2630 bl->owner->number,
2631 bp_excpt.message ? ":" : ".\n");
2632 if (bp_excpt.message != NULL)
2633 fprintf_unfiltered (tmp_error_stream, "%s.\n",
3d6e9d23 2634 bp_excpt.what ());
879bfdc2
DJ
2635 }
2636 else
2637 {
688fca4f 2638 if (bp_excpt.message == NULL)
0000e5cc 2639 {
1ccbe998 2640 std::string message
0000e5cc
PA
2641 = memory_error_message (TARGET_XFER_E_IO,
2642 bl->gdbarch, bl->address);
0000e5cc
PA
2643
2644 fprintf_unfiltered (tmp_error_stream,
2645 "Cannot insert breakpoint %d.\n"
2646 "%s\n",
1ccbe998 2647 bl->owner->number, message.c_str ());
0000e5cc
PA
2648 }
2649 else
2650 {
2651 fprintf_unfiltered (tmp_error_stream,
2652 "Cannot insert breakpoint %d: %s\n",
2653 bl->owner->number,
3d6e9d23 2654 bp_excpt.what ());
0000e5cc 2655 }
879bfdc2 2656 }
0000e5cc 2657 return 1;
879bfdc2
DJ
2658
2659 }
2660 }
2661 else
35df4500 2662 bl->inserted = 1;
879bfdc2 2663
0000e5cc 2664 return 0;
879bfdc2
DJ
2665 }
2666
35df4500 2667 else if (bl->loc_type == bp_loc_hardware_watchpoint
879bfdc2 2668 /* NOTE drow/2003-09-08: This state only exists for removing
4a64f543 2669 watchpoints. It's not clear that it's necessary... */
35df4500 2670 && bl->owner->disposition != disp_del_at_next_stop)
879bfdc2 2671 {
0000e5cc
PA
2672 int val;
2673
77b06cd7
TJB
2674 gdb_assert (bl->owner->ops != NULL
2675 && bl->owner->ops->insert_location != NULL);
2676
2677 val = bl->owner->ops->insert_location (bl);
85d721b8
PA
2678
2679 /* If trying to set a read-watchpoint, and it turns out it's not
2680 supported, try emulating one with an access watchpoint. */
35df4500 2681 if (val == 1 && bl->watchpoint_type == hw_read)
85d721b8
PA
2682 {
2683 struct bp_location *loc, **loc_temp;
2684
2685 /* But don't try to insert it, if there's already another
2686 hw_access location that would be considered a duplicate
2687 of this one. */
2688 ALL_BP_LOCATIONS (loc, loc_temp)
35df4500 2689 if (loc != bl
85d721b8 2690 && loc->watchpoint_type == hw_access
35df4500 2691 && watchpoint_locations_match (bl, loc))
85d721b8 2692 {
35df4500
TJB
2693 bl->duplicate = 1;
2694 bl->inserted = 1;
2695 bl->target_info = loc->target_info;
2696 bl->watchpoint_type = hw_access;
85d721b8
PA
2697 val = 0;
2698 break;
2699 }
2700
2701 if (val == 1)
2702 {
77b06cd7
TJB
2703 bl->watchpoint_type = hw_access;
2704 val = bl->owner->ops->insert_location (bl);
2705
2706 if (val)
2707 /* Back to the original value. */
2708 bl->watchpoint_type = hw_read;
85d721b8
PA
2709 }
2710 }
2711
35df4500 2712 bl->inserted = (val == 0);
879bfdc2
DJ
2713 }
2714
35df4500 2715 else if (bl->owner->type == bp_catchpoint)
879bfdc2 2716 {
0000e5cc
PA
2717 int val;
2718
77b06cd7
TJB
2719 gdb_assert (bl->owner->ops != NULL
2720 && bl->owner->ops->insert_location != NULL);
2721
2722 val = bl->owner->ops->insert_location (bl);
2723 if (val)
2724 {
2725 bl->owner->enable_state = bp_disabled;
2726
2727 if (val == 1)
2728 warning (_("\
2729Error inserting catchpoint %d: Your system does not support this type\n\
2730of catchpoint."), bl->owner->number);
2731 else
2732 warning (_("Error inserting catchpoint %d."), bl->owner->number);
2733 }
2734
2735 bl->inserted = (val == 0);
1640b821
DJ
2736
2737 /* We've already printed an error message if there was a problem
2738 inserting this catchpoint, and we've disabled the catchpoint,
2739 so just return success. */
2740 return 0;
879bfdc2
DJ
2741 }
2742
2743 return 0;
2744}
2745
6c95b8df
PA
2746/* This function is called when program space PSPACE is about to be
2747 deleted. It takes care of updating breakpoints to not reference
2748 PSPACE anymore. */
2749
2750void
2751breakpoint_program_space_exit (struct program_space *pspace)
2752{
2753 struct breakpoint *b, *b_temp;
876fa593 2754 struct bp_location *loc, **loc_temp;
6c95b8df
PA
2755
2756 /* Remove any breakpoint that was set through this program space. */
2757 ALL_BREAKPOINTS_SAFE (b, b_temp)
2758 {
2759 if (b->pspace == pspace)
2760 delete_breakpoint (b);
2761 }
2762
2763 /* Breakpoints set through other program spaces could have locations
2764 bound to PSPACE as well. Remove those. */
876fa593 2765 ALL_BP_LOCATIONS (loc, loc_temp)
6c95b8df
PA
2766 {
2767 struct bp_location *tmp;
2768
2769 if (loc->pspace == pspace)
2770 {
2bdf28a0 2771 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
6c95b8df
PA
2772 if (loc->owner->loc == loc)
2773 loc->owner->loc = loc->next;
2774 else
2775 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
2776 if (tmp->next == loc)
2777 {
2778 tmp->next = loc->next;
2779 break;
2780 }
2781 }
2782 }
2783
2784 /* Now update the global location list to permanently delete the
2785 removed locations above. */
44702360 2786 update_global_location_list (UGLL_DONT_INSERT);
6c95b8df
PA
2787}
2788
74960c60
VP
2789/* Make sure all breakpoints are inserted in inferior.
2790 Throws exception on any error.
2791 A breakpoint that is already inserted won't be inserted
2792 again, so calling this function twice is safe. */
2793void
2794insert_breakpoints (void)
2795{
2796 struct breakpoint *bpt;
2797
2798 ALL_BREAKPOINTS (bpt)
2799 if (is_hardware_watchpoint (bpt))
3a5c3e22
PA
2800 {
2801 struct watchpoint *w = (struct watchpoint *) bpt;
2802
2803 update_watchpoint (w, 0 /* don't reparse. */);
2804 }
74960c60 2805
04086b45
PA
2806 /* Updating watchpoints creates new locations, so update the global
2807 location list. Explicitly tell ugll to insert locations and
7f32a4d5
PA
2808 ignore breakpoints_always_inserted_mode. Also,
2809 update_global_location_list tries to "upgrade" software
2810 breakpoints to hardware breakpoints to handle "set breakpoint
2811 auto-hw", so we need to call it even if we don't have new
2812 locations. */
04086b45 2813 update_global_location_list (UGLL_INSERT);
74960c60
VP
2814}
2815
20388dd6
YQ
2816/* Invoke CALLBACK for each of bp_location. */
2817
2818void
2819iterate_over_bp_locations (walk_bp_location_callback callback)
2820{
2821 struct bp_location *loc, **loc_tmp;
2822
2823 ALL_BP_LOCATIONS (loc, loc_tmp)
2824 {
2825 callback (loc, NULL);
2826 }
2827}
2828
b775012e
LM
2829/* This is used when we need to synch breakpoint conditions between GDB and the
2830 target. It is the case with deleting and disabling of breakpoints when using
2831 always-inserted mode. */
2832
2833static void
2834update_inserted_breakpoint_locations (void)
2835{
2836 struct bp_location *bl, **blp_tmp;
2837 int error_flag = 0;
2838 int val = 0;
2839 int disabled_breaks = 0;
2840 int hw_breakpoint_error = 0;
dd61ec5c 2841 int hw_bp_details_reported = 0;
b775012e 2842
d7e74731 2843 string_file tmp_error_stream;
b775012e
LM
2844
2845 /* Explicitly mark the warning -- this will only be printed if
2846 there was an error. */
d7e74731 2847 tmp_error_stream.puts ("Warning:\n");
b775012e 2848
5ed8105e 2849 scoped_restore_current_pspace_and_thread restore_pspace_thread;
b775012e
LM
2850
2851 ALL_BP_LOCATIONS (bl, blp_tmp)
2852 {
2853 /* We only want to update software breakpoints and hardware
2854 breakpoints. */
2855 if (!is_breakpoint (bl->owner))
2856 continue;
2857
2858 /* We only want to update locations that are already inserted
2859 and need updating. This is to avoid unwanted insertion during
2860 deletion of breakpoints. */
4daf1902 2861 if (!bl->inserted || !bl->needs_update)
b775012e
LM
2862 continue;
2863
2864 switch_to_program_space_and_thread (bl->pspace);
2865
2866 /* For targets that support global breakpoints, there's no need
2867 to select an inferior to insert breakpoint to. In fact, even
2868 if we aren't attached to any process yet, we should still
2869 insert breakpoints. */
f5656ead 2870 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
5b6d1e4f 2871 && (inferior_ptid == null_ptid || !target_has_execution))
b775012e
LM
2872 continue;
2873
d7e74731 2874 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
dd61ec5c 2875 &hw_breakpoint_error, &hw_bp_details_reported);
b775012e
LM
2876 if (val)
2877 error_flag = val;
2878 }
2879
2880 if (error_flag)
2881 {
223ffa71 2882 target_terminal::ours_for_output ();
b775012e
LM
2883 error_stream (tmp_error_stream);
2884 }
b775012e
LM
2885}
2886
c30eee59 2887/* Used when starting or continuing the program. */
c906108c 2888
74960c60
VP
2889static void
2890insert_breakpoint_locations (void)
c906108c 2891{
a5606eee 2892 struct breakpoint *bpt;
35df4500 2893 struct bp_location *bl, **blp_tmp;
eacd795a 2894 int error_flag = 0;
c906108c 2895 int val = 0;
3fbb6ffa 2896 int disabled_breaks = 0;
81d0cc19 2897 int hw_breakpoint_error = 0;
dd61ec5c 2898 int hw_bp_error_explained_already = 0;
c906108c 2899
d7e74731
PA
2900 string_file tmp_error_stream;
2901
81d0cc19
GS
2902 /* Explicitly mark the warning -- this will only be printed if
2903 there was an error. */
d7e74731 2904 tmp_error_stream.puts ("Warning:\n");
6c95b8df 2905
5ed8105e 2906 scoped_restore_current_pspace_and_thread restore_pspace_thread;
6c95b8df 2907
35df4500 2908 ALL_BP_LOCATIONS (bl, blp_tmp)
879bfdc2 2909 {
b775012e 2910 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
879bfdc2
DJ
2911 continue;
2912
4a64f543
MS
2913 /* There is no point inserting thread-specific breakpoints if
2914 the thread no longer exists. ALL_BP_LOCATIONS bp_location
2915 has BL->OWNER always non-NULL. */
35df4500 2916 if (bl->owner->thread != -1
5d5658a1 2917 && !valid_global_thread_id (bl->owner->thread))
f365de73
AS
2918 continue;
2919
35df4500 2920 switch_to_program_space_and_thread (bl->pspace);
6c95b8df
PA
2921
2922 /* For targets that support global breakpoints, there's no need
2923 to select an inferior to insert breakpoint to. In fact, even
2924 if we aren't attached to any process yet, we should still
2925 insert breakpoints. */
f5656ead 2926 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
5b6d1e4f 2927 && (inferior_ptid == null_ptid || !target_has_execution))
6c95b8df
PA
2928 continue;
2929
d7e74731 2930 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
dd61ec5c 2931 &hw_breakpoint_error, &hw_bp_error_explained_already);
879bfdc2 2932 if (val)
eacd795a 2933 error_flag = val;
879bfdc2 2934 }
c906108c 2935
4a64f543
MS
2936 /* If we failed to insert all locations of a watchpoint, remove
2937 them, as half-inserted watchpoint is of limited use. */
a5606eee
VP
2938 ALL_BREAKPOINTS (bpt)
2939 {
2940 int some_failed = 0;
2941 struct bp_location *loc;
2942
2943 if (!is_hardware_watchpoint (bpt))
2944 continue;
2945
d6b74ac4 2946 if (!breakpoint_enabled (bpt))
a5606eee 2947 continue;
74960c60
VP
2948
2949 if (bpt->disposition == disp_del_at_next_stop)
2950 continue;
a5606eee
VP
2951
2952 for (loc = bpt->loc; loc; loc = loc->next)
56710373 2953 if (!loc->inserted && should_be_inserted (loc))
a5606eee
VP
2954 {
2955 some_failed = 1;
2956 break;
2957 }
2958 if (some_failed)
2959 {
2960 for (loc = bpt->loc; loc; loc = loc->next)
2961 if (loc->inserted)
834c0d03 2962 remove_breakpoint (loc);
a5606eee
VP
2963
2964 hw_breakpoint_error = 1;
d7e74731
PA
2965 tmp_error_stream.printf ("Could not insert "
2966 "hardware watchpoint %d.\n",
2967 bpt->number);
eacd795a 2968 error_flag = -1;
a5606eee
VP
2969 }
2970 }
2971
eacd795a 2972 if (error_flag)
81d0cc19
GS
2973 {
2974 /* If a hardware breakpoint or watchpoint was inserted, add a
2975 message about possibly exhausted resources. */
dd61ec5c 2976 if (hw_breakpoint_error && !hw_bp_error_explained_already)
81d0cc19 2977 {
d7e74731 2978 tmp_error_stream.printf ("Could not insert hardware breakpoints:\n\
c6510018 2979You may have requested too many hardware breakpoints/watchpoints.\n");
81d0cc19 2980 }
223ffa71 2981 target_terminal::ours_for_output ();
81d0cc19
GS
2982 error_stream (tmp_error_stream);
2983 }
c906108c
SS
2984}
2985
c30eee59
TJB
2986/* Used when the program stops.
2987 Returns zero if successful, or non-zero if there was a problem
2988 removing a breakpoint location. */
2989
c906108c 2990int
fba45db2 2991remove_breakpoints (void)
c906108c 2992{
35df4500 2993 struct bp_location *bl, **blp_tmp;
3a1bae8e 2994 int val = 0;
c906108c 2995
35df4500 2996 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 2997 {
1e4d1764 2998 if (bl->inserted && !is_tracepoint (bl->owner))
834c0d03 2999 val |= remove_breakpoint (bl);
c5aa993b 3000 }
3a1bae8e 3001 return val;
c906108c
SS
3002}
3003
49fa26b0
PA
3004/* When a thread exits, remove breakpoints that are related to
3005 that thread. */
3006
3007static void
3008remove_threaded_breakpoints (struct thread_info *tp, int silent)
3009{
3010 struct breakpoint *b, *b_tmp;
3011
3012 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3013 {
5d5658a1 3014 if (b->thread == tp->global_num && user_breakpoint_p (b))
49fa26b0
PA
3015 {
3016 b->disposition = disp_del_at_next_stop;
3017
3018 printf_filtered (_("\
43792cf0
PA
3019Thread-specific breakpoint %d deleted - thread %s no longer in the thread list.\n"),
3020 b->number, print_thread_id (tp));
49fa26b0
PA
3021
3022 /* Hide it from the user. */
3023 b->number = 0;
3024 }
3025 }
3026}
3027
f3869b1a 3028/* See breakpoint.h. */
6c95b8df 3029
f3869b1a 3030void
00431a78 3031remove_breakpoints_inf (inferior *inf)
6c95b8df 3032{
35df4500 3033 struct bp_location *bl, **blp_tmp;
6c95b8df 3034 int val;
6c95b8df 3035
35df4500 3036 ALL_BP_LOCATIONS (bl, blp_tmp)
6c95b8df 3037 {
35df4500 3038 if (bl->pspace != inf->pspace)
6c95b8df
PA
3039 continue;
3040
fc126975 3041 if (bl->inserted && !bl->target_info.persist)
6c95b8df 3042 {
834c0d03 3043 val = remove_breakpoint (bl);
6c95b8df 3044 if (val != 0)
f3869b1a 3045 return;
6c95b8df
PA
3046 }
3047 }
6c95b8df
PA
3048}
3049
e58b0e63
PA
3050static int internal_breakpoint_number = -1;
3051
84f4c1fe
PM
3052/* Set the breakpoint number of B, depending on the value of INTERNAL.
3053 If INTERNAL is non-zero, the breakpoint number will be populated
3054 from internal_breakpoint_number and that variable decremented.
e5dd4106 3055 Otherwise the breakpoint number will be populated from
84f4c1fe
PM
3056 breakpoint_count and that value incremented. Internal breakpoints
3057 do not set the internal var bpnum. */
3058static void
3059set_breakpoint_number (int internal, struct breakpoint *b)
3060{
3061 if (internal)
3062 b->number = internal_breakpoint_number--;
3063 else
3064 {
3065 set_breakpoint_count (breakpoint_count + 1);
3066 b->number = breakpoint_count;
3067 }
3068}
3069
e62c965a 3070static struct breakpoint *
a6d9a66e 3071create_internal_breakpoint (struct gdbarch *gdbarch,
06edf0c0 3072 CORE_ADDR address, enum bptype type,
c0a91b2b 3073 const struct breakpoint_ops *ops)
e62c965a 3074{
51abb421 3075 symtab_and_line sal;
e62c965a
PP
3076 sal.pc = address;
3077 sal.section = find_pc_overlay (sal.pc);
6c95b8df 3078 sal.pspace = current_program_space;
e62c965a 3079
51abb421 3080 breakpoint *b = set_raw_breakpoint (gdbarch, sal, type, ops);
e62c965a
PP
3081 b->number = internal_breakpoint_number--;
3082 b->disposition = disp_donttouch;
3083
3084 return b;
3085}
3086
17450429
PP
3087static const char *const longjmp_names[] =
3088 {
3089 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
3090 };
3091#define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
3092
3093/* Per-objfile data private to breakpoint.c. */
3094struct breakpoint_objfile_data
3095{
3096 /* Minimal symbol for "_ovly_debug_event" (if any). */
43dce439 3097 struct bound_minimal_symbol overlay_msym {};
17450429
PP
3098
3099 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
43dce439 3100 struct bound_minimal_symbol longjmp_msym[NUM_LONGJMP_NAMES] {};
17450429 3101
28106bc2 3102 /* True if we have looked for longjmp probes. */
43dce439 3103 int longjmp_searched = 0;
28106bc2 3104
45461e0d
SM
3105 /* SystemTap probe points for longjmp (if any). These are non-owning
3106 references. */
3107 std::vector<probe *> longjmp_probes;
28106bc2 3108
17450429 3109 /* Minimal symbol for "std::terminate()" (if any). */
43dce439 3110 struct bound_minimal_symbol terminate_msym {};
17450429
PP
3111
3112 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
43dce439 3113 struct bound_minimal_symbol exception_msym {};
28106bc2
SDJ
3114
3115 /* True if we have looked for exception probes. */
43dce439 3116 int exception_searched = 0;
28106bc2 3117
45461e0d
SM
3118 /* SystemTap probe points for unwinding (if any). These are non-owning
3119 references. */
3120 std::vector<probe *> exception_probes;
17450429
PP
3121};
3122
51d3063a
TT
3123static const struct objfile_key<breakpoint_objfile_data>
3124 breakpoint_objfile_key;
17450429
PP
3125
3126/* Minimal symbol not found sentinel. */
3127static struct minimal_symbol msym_not_found;
3128
3129/* Returns TRUE if MSYM point to the "not found" sentinel. */
3130
3131static int
3132msym_not_found_p (const struct minimal_symbol *msym)
3133{
3134 return msym == &msym_not_found;
3135}
3136
3137/* Return per-objfile data needed by breakpoint.c.
3138 Allocate the data if necessary. */
3139
3140static struct breakpoint_objfile_data *
3141get_breakpoint_objfile_data (struct objfile *objfile)
3142{
3143 struct breakpoint_objfile_data *bp_objfile_data;
3144
51d3063a 3145 bp_objfile_data = breakpoint_objfile_key.get (objfile);
17450429 3146 if (bp_objfile_data == NULL)
51d3063a 3147 bp_objfile_data = breakpoint_objfile_key.emplace (objfile);
17450429
PP
3148 return bp_objfile_data;
3149}
3150
e62c965a 3151static void
af02033e 3152create_overlay_event_breakpoint (void)
e62c965a 3153{
af02033e 3154 const char *const func_name = "_ovly_debug_event";
e62c965a 3155
2030c079 3156 for (objfile *objfile : current_program_space->objfiles ())
69de3c6a
PP
3157 {
3158 struct breakpoint *b;
17450429
PP
3159 struct breakpoint_objfile_data *bp_objfile_data;
3160 CORE_ADDR addr;
67994074 3161 struct explicit_location explicit_loc;
69de3c6a 3162
17450429
PP
3163 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3164
3b7344d5 3165 if (msym_not_found_p (bp_objfile_data->overlay_msym.minsym))
17450429
PP
3166 continue;
3167
3b7344d5 3168 if (bp_objfile_data->overlay_msym.minsym == NULL)
17450429 3169 {
3b7344d5 3170 struct bound_minimal_symbol m;
17450429
PP
3171
3172 m = lookup_minimal_symbol_text (func_name, objfile);
3b7344d5 3173 if (m.minsym == NULL)
17450429
PP
3174 {
3175 /* Avoid future lookups in this objfile. */
3b7344d5 3176 bp_objfile_data->overlay_msym.minsym = &msym_not_found;
17450429
PP
3177 continue;
3178 }
3179 bp_objfile_data->overlay_msym = m;
3180 }
e62c965a 3181
77e371c0 3182 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->overlay_msym);
08feed99 3183 b = create_internal_breakpoint (objfile->arch (), addr,
06edf0c0
PA
3184 bp_overlay_event,
3185 &internal_breakpoint_ops);
67994074
KS
3186 initialize_explicit_location (&explicit_loc);
3187 explicit_loc.function_name = ASTRDUP (func_name);
d28cd78a 3188 b->location = new_explicit_location (&explicit_loc);
e62c965a 3189
69de3c6a
PP
3190 if (overlay_debugging == ovly_auto)
3191 {
3192 b->enable_state = bp_enabled;
3193 overlay_events_enabled = 1;
3194 }
3195 else
3196 {
3197 b->enable_state = bp_disabled;
3198 overlay_events_enabled = 0;
3199 }
e62c965a 3200 }
e62c965a
PP
3201}
3202
0fd8e87f 3203static void
af02033e 3204create_longjmp_master_breakpoint (void)
0fd8e87f 3205{
5ed8105e 3206 scoped_restore_current_program_space restore_pspace;
0fd8e87f 3207
94c93c35
TT
3208 for (struct program_space *pspace : program_spaces)
3209 {
3210 set_current_program_space (pspace);
af02033e 3211
94c93c35
TT
3212 for (objfile *objfile : current_program_space->objfiles ())
3213 {
3214 int i;
3215 struct gdbarch *gdbarch;
3216 struct breakpoint_objfile_data *bp_objfile_data;
0fd8e87f 3217
94c93c35 3218 gdbarch = objfile->arch ();
0fd8e87f 3219
94c93c35 3220 bp_objfile_data = get_breakpoint_objfile_data (objfile);
17450429 3221
94c93c35
TT
3222 if (!bp_objfile_data->longjmp_searched)
3223 {
3224 std::vector<probe *> ret
3225 = find_probes_in_objfile (objfile, "libc", "longjmp");
25f9533e 3226
94c93c35
TT
3227 if (!ret.empty ())
3228 {
3229 /* We are only interested in checking one element. */
3230 probe *p = ret[0];
aed57c53 3231
94c93c35
TT
3232 if (!p->can_evaluate_arguments ())
3233 {
3234 /* We cannot use the probe interface here,
3235 because it does not know how to evaluate
3236 arguments. */
3237 ret.clear ();
3238 }
3239 }
3240 bp_objfile_data->longjmp_probes = ret;
3241 bp_objfile_data->longjmp_searched = 1;
3242 }
25f9533e 3243
94c93c35
TT
3244 if (!bp_objfile_data->longjmp_probes.empty ())
3245 {
3246 for (probe *p : bp_objfile_data->longjmp_probes)
3247 {
3248 struct breakpoint *b;
3249
3250 b = create_internal_breakpoint (gdbarch,
3251 p->get_relocated_address (objfile),
3252 bp_longjmp_master,
3253 &internal_breakpoint_ops);
3254 b->location = new_probe_location ("-probe-stap libc:longjmp");
3255 b->enable_state = bp_disabled;
3256 }
28106bc2 3257
94c93c35
TT
3258 continue;
3259 }
28106bc2 3260
94c93c35
TT
3261 if (!gdbarch_get_longjmp_target_p (gdbarch))
3262 continue;
28106bc2 3263
94c93c35
TT
3264 for (i = 0; i < NUM_LONGJMP_NAMES; i++)
3265 {
3266 struct breakpoint *b;
3267 const char *func_name;
3268 CORE_ADDR addr;
3269 struct explicit_location explicit_loc;
0fd8e87f 3270
94c93c35
TT
3271 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i].minsym))
3272 continue;
17450429 3273
94c93c35
TT
3274 func_name = longjmp_names[i];
3275 if (bp_objfile_data->longjmp_msym[i].minsym == NULL)
3276 {
3277 struct bound_minimal_symbol m;
aed57c53 3278
94c93c35
TT
3279 m = lookup_minimal_symbol_text (func_name, objfile);
3280 if (m.minsym == NULL)
3281 {
3282 /* Prevent future lookups in this objfile. */
3283 bp_objfile_data->longjmp_msym[i].minsym = &msym_not_found;
3284 continue;
3285 }
3286 bp_objfile_data->longjmp_msym[i] = m;
3287 }
17450429 3288
94c93c35
TT
3289 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->longjmp_msym[i]);
3290 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master,
3291 &internal_breakpoint_ops);
3292 initialize_explicit_location (&explicit_loc);
3293 explicit_loc.function_name = ASTRDUP (func_name);
3294 b->location = new_explicit_location (&explicit_loc);
3295 b->enable_state = bp_disabled;
3296 }
3297 }
3298 }
0fd8e87f
UW
3299}
3300
af02033e 3301/* Create a master std::terminate breakpoint. */
aa7d318d 3302static void
af02033e 3303create_std_terminate_master_breakpoint (void)
aa7d318d 3304{
af02033e 3305 const char *const func_name = "std::terminate()";
aa7d318d 3306
5ed8105e 3307 scoped_restore_current_program_space restore_pspace;
aa7d318d 3308
94c93c35
TT
3309 for (struct program_space *pspace : program_spaces)
3310 {
3311 CORE_ADDR addr;
17450429 3312
94c93c35 3313 set_current_program_space (pspace);
17450429 3314
94c93c35
TT
3315 for (objfile *objfile : current_program_space->objfiles ())
3316 {
3317 struct breakpoint *b;
3318 struct breakpoint_objfile_data *bp_objfile_data;
3319 struct explicit_location explicit_loc;
aa7d318d 3320
94c93c35 3321 bp_objfile_data = get_breakpoint_objfile_data (objfile);
aa7d318d 3322
94c93c35
TT
3323 if (msym_not_found_p (bp_objfile_data->terminate_msym.minsym))
3324 continue;
17450429 3325
94c93c35
TT
3326 if (bp_objfile_data->terminate_msym.minsym == NULL)
3327 {
3328 struct bound_minimal_symbol m;
17450429 3329
94c93c35
TT
3330 m = lookup_minimal_symbol (func_name, NULL, objfile);
3331 if (m.minsym == NULL || (MSYMBOL_TYPE (m.minsym) != mst_text
3332 && MSYMBOL_TYPE (m.minsym) != mst_file_text))
3333 {
3334 /* Prevent future lookups in this objfile. */
3335 bp_objfile_data->terminate_msym.minsym = &msym_not_found;
3336 continue;
3337 }
3338 bp_objfile_data->terminate_msym = m;
3339 }
aa7d318d 3340
94c93c35
TT
3341 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->terminate_msym);
3342 b = create_internal_breakpoint (objfile->arch (), addr,
3343 bp_std_terminate_master,
3344 &internal_breakpoint_ops);
3345 initialize_explicit_location (&explicit_loc);
3346 explicit_loc.function_name = ASTRDUP (func_name);
3347 b->location = new_explicit_location (&explicit_loc);
3348 b->enable_state = bp_disabled;
3349 }
3350 }
aa7d318d
TT
3351}
3352
186c406b
TT
3353/* Install a master breakpoint on the unwinder's debug hook. */
3354
70221824 3355static void
186c406b
TT
3356create_exception_master_breakpoint (void)
3357{
17450429 3358 const char *const func_name = "_Unwind_DebugHook";
186c406b 3359
2030c079 3360 for (objfile *objfile : current_program_space->objfiles ())
186c406b 3361 {
17450429
PP
3362 struct breakpoint *b;
3363 struct gdbarch *gdbarch;
3364 struct breakpoint_objfile_data *bp_objfile_data;
3365 CORE_ADDR addr;
67994074 3366 struct explicit_location explicit_loc;
17450429
PP
3367
3368 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3369
28106bc2
SDJ
3370 /* We prefer the SystemTap probe point if it exists. */
3371 if (!bp_objfile_data->exception_searched)
3372 {
45461e0d
SM
3373 std::vector<probe *> ret
3374 = find_probes_in_objfile (objfile, "libgcc", "unwind");
25f9533e 3375
45461e0d 3376 if (!ret.empty ())
25f9533e
SDJ
3377 {
3378 /* We are only interested in checking one element. */
45461e0d 3379 probe *p = ret[0];
25f9533e 3380
935676c9 3381 if (!p->can_evaluate_arguments ())
25f9533e
SDJ
3382 {
3383 /* We cannot use the probe interface here, because it does
3384 not know how to evaluate arguments. */
45461e0d 3385 ret.clear ();
25f9533e
SDJ
3386 }
3387 }
3388 bp_objfile_data->exception_probes = ret;
28106bc2
SDJ
3389 bp_objfile_data->exception_searched = 1;
3390 }
3391
45461e0d 3392 if (!bp_objfile_data->exception_probes.empty ())
28106bc2 3393 {
08feed99 3394 gdbarch = objfile->arch ();
45461e0d
SM
3395
3396 for (probe *p : bp_objfile_data->exception_probes)
28106bc2 3397 {
729662a5 3398 b = create_internal_breakpoint (gdbarch,
935676c9 3399 p->get_relocated_address (objfile),
28106bc2
SDJ
3400 bp_exception_master,
3401 &internal_breakpoint_ops);
d28cd78a 3402 b->location = new_probe_location ("-probe-stap libgcc:unwind");
28106bc2
SDJ
3403 b->enable_state = bp_disabled;
3404 }
3405
3406 continue;
3407 }
3408
3409 /* Otherwise, try the hook function. */
3410
3b7344d5 3411 if (msym_not_found_p (bp_objfile_data->exception_msym.minsym))
17450429
PP
3412 continue;
3413
08feed99 3414 gdbarch = objfile->arch ();
186c406b 3415
3b7344d5 3416 if (bp_objfile_data->exception_msym.minsym == NULL)
186c406b 3417 {
3b7344d5 3418 struct bound_minimal_symbol debug_hook;
186c406b 3419
17450429 3420 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
3b7344d5 3421 if (debug_hook.minsym == NULL)
17450429 3422 {
3b7344d5 3423 bp_objfile_data->exception_msym.minsym = &msym_not_found;
17450429
PP
3424 continue;
3425 }
3426
3427 bp_objfile_data->exception_msym = debug_hook;
186c406b 3428 }
17450429 3429
77e371c0 3430 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->exception_msym);
8b88a78e
PA
3431 addr = gdbarch_convert_from_func_ptr_addr (gdbarch, addr,
3432 current_top_target ());
06edf0c0
PA
3433 b = create_internal_breakpoint (gdbarch, addr, bp_exception_master,
3434 &internal_breakpoint_ops);
67994074
KS
3435 initialize_explicit_location (&explicit_loc);
3436 explicit_loc.function_name = ASTRDUP (func_name);
d28cd78a 3437 b->location = new_explicit_location (&explicit_loc);
17450429 3438 b->enable_state = bp_disabled;
186c406b 3439 }
186c406b
TT
3440}
3441
9ef9e6a6
KS
3442/* Does B have a location spec? */
3443
3444static int
3445breakpoint_event_location_empty_p (const struct breakpoint *b)
3446{
d28cd78a 3447 return b->location != NULL && event_location_empty_p (b->location.get ());
9ef9e6a6
KS
3448}
3449
c906108c 3450void
fba45db2 3451update_breakpoints_after_exec (void)
c906108c 3452{
35df4500 3453 struct breakpoint *b, *b_tmp;
876fa593 3454 struct bp_location *bploc, **bplocp_tmp;
c906108c 3455
25b22b0a
PA
3456 /* We're about to delete breakpoints from GDB's lists. If the
3457 INSERTED flag is true, GDB will try to lift the breakpoints by
3458 writing the breakpoints' "shadow contents" back into memory. The
3459 "shadow contents" are NOT valid after an exec, so GDB should not
3460 do that. Instead, the target is responsible from marking
3461 breakpoints out as soon as it detects an exec. We don't do that
3462 here instead, because there may be other attempts to delete
3463 breakpoints after detecting an exec and before reaching here. */
876fa593 3464 ALL_BP_LOCATIONS (bploc, bplocp_tmp)
6c95b8df
PA
3465 if (bploc->pspace == current_program_space)
3466 gdb_assert (!bploc->inserted);
c906108c 3467
35df4500 3468 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 3469 {
6c95b8df
PA
3470 if (b->pspace != current_program_space)
3471 continue;
3472
4a64f543 3473 /* Solib breakpoints must be explicitly reset after an exec(). */
c5aa993b
JM
3474 if (b->type == bp_shlib_event)
3475 {
3476 delete_breakpoint (b);
3477 continue;
3478 }
c906108c 3479
4a64f543 3480 /* JIT breakpoints must be explicitly reset after an exec(). */
4efc6507
DE
3481 if (b->type == bp_jit_event)
3482 {
3483 delete_breakpoint (b);
3484 continue;
3485 }
3486
1900040c 3487 /* Thread event breakpoints must be set anew after an exec(),
0fd8e87f
UW
3488 as must overlay event and longjmp master breakpoints. */
3489 if (b->type == bp_thread_event || b->type == bp_overlay_event
186c406b
TT
3490 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
3491 || b->type == bp_exception_master)
c4093a6a
JM
3492 {
3493 delete_breakpoint (b);
3494 continue;
3495 }
3496
4a64f543 3497 /* Step-resume breakpoints are meaningless after an exec(). */
2c03e5be 3498 if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
c5aa993b
JM
3499 {
3500 delete_breakpoint (b);
3501 continue;
3502 }
3503
7c16b83e
PA
3504 /* Just like single-step breakpoints. */
3505 if (b->type == bp_single_step)
3506 {
3507 delete_breakpoint (b);
3508 continue;
3509 }
3510
611c83ae
PA
3511 /* Longjmp and longjmp-resume breakpoints are also meaningless
3512 after an exec. */
186c406b 3513 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
e2e4d78b 3514 || b->type == bp_longjmp_call_dummy
186c406b 3515 || b->type == bp_exception || b->type == bp_exception_resume)
611c83ae
PA
3516 {
3517 delete_breakpoint (b);
3518 continue;
3519 }
3520
ce78b96d
JB
3521 if (b->type == bp_catchpoint)
3522 {
3523 /* For now, none of the bp_catchpoint breakpoints need to
3524 do anything at this point. In the future, if some of
3525 the catchpoints need to something, we will need to add
3526 a new method, and call this method from here. */
3527 continue;
3528 }
3529
c5aa993b
JM
3530 /* bp_finish is a special case. The only way we ought to be able
3531 to see one of these when an exec() has happened, is if the user
3532 caught a vfork, and then said "finish". Ordinarily a finish just
3533 carries them to the call-site of the current callee, by setting
3534 a temporary bp there and resuming. But in this case, the finish
3535 will carry them entirely through the vfork & exec.
3536
3537 We don't want to allow a bp_finish to remain inserted now. But
3538 we can't safely delete it, 'cause finish_command has a handle to
3539 the bp on a bpstat, and will later want to delete it. There's a
3540 chance (and I've seen it happen) that if we delete the bp_finish
3541 here, that its storage will get reused by the time finish_command
3542 gets 'round to deleting the "use to be a bp_finish" breakpoint.
3543 We really must allow finish_command to delete a bp_finish.
3544
e5dd4106 3545 In the absence of a general solution for the "how do we know
53a5351d
JM
3546 it's safe to delete something others may have handles to?"
3547 problem, what we'll do here is just uninsert the bp_finish, and
3548 let finish_command delete it.
3549
3550 (We know the bp_finish is "doomed" in the sense that it's
3551 momentary, and will be deleted as soon as finish_command sees
3552 the inferior stopped. So it doesn't matter that the bp's
3553 address is probably bogus in the new a.out, unlike e.g., the
3554 solib breakpoints.) */
c5aa993b 3555
c5aa993b
JM
3556 if (b->type == bp_finish)
3557 {
3558 continue;
3559 }
3560
3561 /* Without a symbolic address, we have little hope of the
3562 pre-exec() address meaning the same thing in the post-exec()
4a64f543 3563 a.out. */
9ef9e6a6 3564 if (breakpoint_event_location_empty_p (b))
c5aa993b
JM
3565 {
3566 delete_breakpoint (b);
3567 continue;
3568 }
c5aa993b 3569 }
c906108c
SS
3570}
3571
3572int
d80ee84f 3573detach_breakpoints (ptid_t ptid)
c906108c 3574{
35df4500 3575 struct bp_location *bl, **blp_tmp;
3a1bae8e 3576 int val = 0;
2989a365 3577 scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid);
6c95b8df 3578 struct inferior *inf = current_inferior ();
c5aa993b 3579
e99b03dc 3580 if (ptid.pid () == inferior_ptid.pid ())
8a3fe4f8 3581 error (_("Cannot detach breakpoints of inferior_ptid"));
c5aa993b 3582
6c95b8df 3583 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
d80ee84f 3584 inferior_ptid = ptid;
35df4500 3585 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3586 {
35df4500 3587 if (bl->pspace != inf->pspace)
6c95b8df
PA
3588 continue;
3589
bd9673a4
PW
3590 /* This function must physically remove breakpoints locations
3591 from the specified ptid, without modifying the breakpoint
3592 package's state. Locations of type bp_loc_other are only
3593 maintained at GDB side. So, there is no need to remove
3594 these bp_loc_other locations. Moreover, removing these
3595 would modify the breakpoint package's state. */
3596 if (bl->loc_type == bp_loc_other)
3597 continue;
3598
35df4500 3599 if (bl->inserted)
b2b6a7da 3600 val |= remove_breakpoint_1 (bl, DETACH_BREAKPOINT);
c5aa993b 3601 }
d03285ec 3602
3a1bae8e 3603 return val;
c906108c
SS
3604}
3605
35df4500 3606/* Remove the breakpoint location BL from the current address space.
6c95b8df
PA
3607 Note that this is used to detach breakpoints from a child fork.
3608 When we get here, the child isn't in the inferior list, and neither
3609 do we have objects to represent its address space --- we should
35df4500 3610 *not* look at bl->pspace->aspace here. */
6c95b8df 3611
c906108c 3612static int
b2b6a7da 3613remove_breakpoint_1 (struct bp_location *bl, enum remove_bp_reason reason)
c906108c
SS
3614{
3615 int val;
c5aa993b 3616
35df4500
TJB
3617 /* BL is never in moribund_locations by our callers. */
3618 gdb_assert (bl->owner != NULL);
2bdf28a0 3619
74960c60
VP
3620 /* The type of none suggests that owner is actually deleted.
3621 This should not ever happen. */
35df4500 3622 gdb_assert (bl->owner->type != bp_none);
0bde7532 3623
35df4500
TJB
3624 if (bl->loc_type == bp_loc_software_breakpoint
3625 || bl->loc_type == bp_loc_hardware_breakpoint)
c906108c 3626 {
c02f5703
MS
3627 /* "Normal" instruction breakpoint: either the standard
3628 trap-instruction bp (bp_breakpoint), or a
3629 bp_hardware_breakpoint. */
3630
3631 /* First check to see if we have to handle an overlay. */
3632 if (overlay_debugging == ovly_off
35df4500
TJB
3633 || bl->section == NULL
3634 || !(section_is_overlay (bl->section)))
c02f5703
MS
3635 {
3636 /* No overlay handling: just remove the breakpoint. */
08351840
PA
3637
3638 /* If we're trying to uninsert a memory breakpoint that we
3639 know is set in a dynamic object that is marked
3640 shlib_disabled, then either the dynamic object was
3641 removed with "remove-symbol-file" or with
3642 "nosharedlibrary". In the former case, we don't know
3643 whether another dynamic object might have loaded over the
3644 breakpoint's address -- the user might well let us know
3645 about it next with add-symbol-file (the whole point of
d03de421 3646 add-symbol-file is letting the user manually maintain a
08351840
PA
3647 list of dynamically loaded objects). If we have the
3648 breakpoint's shadow memory, that is, this is a software
3649 breakpoint managed by GDB, check whether the breakpoint
3650 is still inserted in memory, to avoid overwriting wrong
3651 code with stale saved shadow contents. Note that HW
3652 breakpoints don't have shadow memory, as they're
3653 implemented using a mechanism that is not dependent on
3654 being able to modify the target's memory, and as such
3655 they should always be removed. */
3656 if (bl->shlib_disabled
3657 && bl->target_info.shadow_len != 0
3658 && !memory_validate_breakpoint (bl->gdbarch, &bl->target_info))
3659 val = 0;
3660 else
73971819 3661 val = bl->owner->ops->remove_location (bl, reason);
c02f5703 3662 }
c906108c
SS
3663 else
3664 {
4a64f543 3665 /* This breakpoint is in an overlay section.
c02f5703
MS
3666 Did we set a breakpoint at the LMA? */
3667 if (!overlay_events_enabled)
3668 {
3669 /* Yes -- overlay event support is not active, so we
3670 should have set a breakpoint at the LMA. Remove it.
3671 */
c02f5703
MS
3672 /* Ignore any failures: if the LMA is in ROM, we will
3673 have already warned when we failed to insert it. */
35df4500
TJB
3674 if (bl->loc_type == bp_loc_hardware_breakpoint)
3675 target_remove_hw_breakpoint (bl->gdbarch,
3676 &bl->overlay_target_info);
c02f5703 3677 else
35df4500 3678 target_remove_breakpoint (bl->gdbarch,
73971819
PA
3679 &bl->overlay_target_info,
3680 reason);
c02f5703
MS
3681 }
3682 /* Did we set a breakpoint at the VMA?
3683 If so, we will have marked the breakpoint 'inserted'. */
35df4500 3684 if (bl->inserted)
c906108c 3685 {
c02f5703
MS
3686 /* Yes -- remove it. Previously we did not bother to
3687 remove the breakpoint if the section had been
3688 unmapped, but let's not rely on that being safe. We
3689 don't know what the overlay manager might do. */
aa67235e
UW
3690
3691 /* However, we should remove *software* breakpoints only
3692 if the section is still mapped, or else we overwrite
3693 wrong code with the saved shadow contents. */
348d480f
PA
3694 if (bl->loc_type == bp_loc_hardware_breakpoint
3695 || section_is_mapped (bl->section))
73971819 3696 val = bl->owner->ops->remove_location (bl, reason);
aa67235e
UW
3697 else
3698 val = 0;
c906108c 3699 }
c02f5703
MS
3700 else
3701 {
3702 /* No -- not inserted, so no need to remove. No error. */
3703 val = 0;
3704 }
c906108c 3705 }
879d1e6b 3706
08351840
PA
3707 /* In some cases, we might not be able to remove a breakpoint in
3708 a shared library that has already been removed, but we have
3709 not yet processed the shlib unload event. Similarly for an
3710 unloaded add-symbol-file object - the user might not yet have
3711 had the chance to remove-symbol-file it. shlib_disabled will
3712 be set if the library/object has already been removed, but
3713 the breakpoint hasn't been uninserted yet, e.g., after
3714 "nosharedlibrary" or "remove-symbol-file" with breakpoints
3715 always-inserted mode. */
076855f9 3716 if (val
08351840
PA
3717 && (bl->loc_type == bp_loc_software_breakpoint
3718 && (bl->shlib_disabled
3719 || solib_name_from_address (bl->pspace, bl->address)
d03de421
PA
3720 || shared_objfile_contains_address_p (bl->pspace,
3721 bl->address))))
879d1e6b
UW
3722 val = 0;
3723
c906108c
SS
3724 if (val)
3725 return val;
b2b6a7da 3726 bl->inserted = (reason == DETACH_BREAKPOINT);
c906108c 3727 }
35df4500 3728 else if (bl->loc_type == bp_loc_hardware_watchpoint)
c906108c 3729 {
77b06cd7
TJB
3730 gdb_assert (bl->owner->ops != NULL
3731 && bl->owner->ops->remove_location != NULL);
3732
b2b6a7da 3733 bl->inserted = (reason == DETACH_BREAKPOINT);
73971819 3734 bl->owner->ops->remove_location (bl, reason);
2e70b7b9 3735
c906108c 3736 /* Failure to remove any of the hardware watchpoints comes here. */
b2b6a7da 3737 if (reason == REMOVE_BREAKPOINT && bl->inserted)
8a3fe4f8 3738 warning (_("Could not remove hardware watchpoint %d."),
35df4500 3739 bl->owner->number);
c906108c 3740 }
35df4500
TJB
3741 else if (bl->owner->type == bp_catchpoint
3742 && breakpoint_enabled (bl->owner)
3743 && !bl->duplicate)
ce78b96d 3744 {
77b06cd7
TJB
3745 gdb_assert (bl->owner->ops != NULL
3746 && bl->owner->ops->remove_location != NULL);
ce78b96d 3747
73971819 3748 val = bl->owner->ops->remove_location (bl, reason);
ce78b96d
JB
3749 if (val)
3750 return val;
77b06cd7 3751
b2b6a7da 3752 bl->inserted = (reason == DETACH_BREAKPOINT);
ce78b96d 3753 }
c906108c
SS
3754
3755 return 0;
3756}
3757
6c95b8df 3758static int
834c0d03 3759remove_breakpoint (struct bp_location *bl)
6c95b8df 3760{
35df4500
TJB
3761 /* BL is never in moribund_locations by our callers. */
3762 gdb_assert (bl->owner != NULL);
2bdf28a0 3763
6c95b8df
PA
3764 /* The type of none suggests that owner is actually deleted.
3765 This should not ever happen. */
35df4500 3766 gdb_assert (bl->owner->type != bp_none);
6c95b8df 3767
5ed8105e 3768 scoped_restore_current_pspace_and_thread restore_pspace_thread;
6c95b8df 3769
35df4500 3770 switch_to_program_space_and_thread (bl->pspace);
6c95b8df 3771
5ed8105e 3772 return remove_breakpoint_1 (bl, REMOVE_BREAKPOINT);
6c95b8df
PA
3773}
3774
c906108c
SS
3775/* Clear the "inserted" flag in all breakpoints. */
3776
25b22b0a 3777void
fba45db2 3778mark_breakpoints_out (void)
c906108c 3779{
35df4500 3780 struct bp_location *bl, **blp_tmp;
c906108c 3781
35df4500 3782 ALL_BP_LOCATIONS (bl, blp_tmp)
66c4b3e8 3783 if (bl->pspace == current_program_space)
35df4500 3784 bl->inserted = 0;
c906108c
SS
3785}
3786
53a5351d
JM
3787/* Clear the "inserted" flag in all breakpoints and delete any
3788 breakpoints which should go away between runs of the program.
c906108c
SS
3789
3790 Plus other such housekeeping that has to be done for breakpoints
3791 between runs.
3792
53a5351d
JM
3793 Note: this function gets called at the end of a run (by
3794 generic_mourn_inferior) and when a run begins (by
4a64f543 3795 init_wait_for_inferior). */
c906108c
SS
3796
3797
3798
3799void
fba45db2 3800breakpoint_init_inferior (enum inf_context context)
c906108c 3801{
35df4500 3802 struct breakpoint *b, *b_tmp;
6c95b8df 3803 struct program_space *pspace = current_program_space;
c906108c 3804
50c71eaf
PA
3805 /* If breakpoint locations are shared across processes, then there's
3806 nothing to do. */
f5656ead 3807 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
50c71eaf
PA
3808 return;
3809
1a853c52 3810 mark_breakpoints_out ();
075f6582 3811
35df4500 3812 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 3813 {
6c95b8df
PA
3814 if (b->loc && b->loc->pspace != pspace)
3815 continue;
3816
c5aa993b
JM
3817 switch (b->type)
3818 {
3819 case bp_call_dummy:
e2e4d78b 3820 case bp_longjmp_call_dummy:
c906108c 3821
c5aa993b 3822 /* If the call dummy breakpoint is at the entry point it will
ab92d69b
PA
3823 cause problems when the inferior is rerun, so we better get
3824 rid of it. */
3825
3826 case bp_watchpoint_scope:
3827
3828 /* Also get rid of scope breakpoints. */
3829
3830 case bp_shlib_event:
3831
3832 /* Also remove solib event breakpoints. Their addresses may
3833 have changed since the last time we ran the program.
3834 Actually we may now be debugging against different target;
3835 and so the solib backend that installed this breakpoint may
3836 not be used in by the target. E.g.,
3837
3838 (gdb) file prog-linux
3839 (gdb) run # native linux target
3840 ...
3841 (gdb) kill
3842 (gdb) file prog-win.exe
3843 (gdb) tar rem :9999 # remote Windows gdbserver.
3844 */
c906108c 3845
f59f708a
PA
3846 case bp_step_resume:
3847
3848 /* Also remove step-resume breakpoints. */
3849
7c16b83e
PA
3850 case bp_single_step:
3851
3852 /* Also remove single-step breakpoints. */
3853
c5aa993b
JM
3854 delete_breakpoint (b);
3855 break;
c906108c 3856
c5aa993b
JM
3857 case bp_watchpoint:
3858 case bp_hardware_watchpoint:
3859 case bp_read_watchpoint:
3860 case bp_access_watchpoint:
3a5c3e22
PA
3861 {
3862 struct watchpoint *w = (struct watchpoint *) b;
c906108c 3863
3a5c3e22
PA
3864 /* Likewise for watchpoints on local expressions. */
3865 if (w->exp_valid_block != NULL)
3866 delete_breakpoint (b);
63000888 3867 else
3a5c3e22 3868 {
63000888
PA
3869 /* Get rid of existing locations, which are no longer
3870 valid. New ones will be created in
3871 update_watchpoint, when the inferior is restarted.
3872 The next update_global_location_list call will
3873 garbage collect them. */
3874 b->loc = NULL;
3875
3876 if (context == inf_starting)
3877 {
3878 /* Reset val field to force reread of starting value in
3879 insert_breakpoints. */
850645cf 3880 w->val.reset (nullptr);
4c1d86d9 3881 w->val_valid = false;
63000888
PA
3882 }
3883 }
3a5c3e22 3884 }
c5aa993b
JM
3885 break;
3886 default:
c5aa993b
JM
3887 break;
3888 }
3889 }
1c5cfe86
PA
3890
3891 /* Get rid of the moribund locations. */
1123588c 3892 for (bp_location *bl : moribund_locations)
35df4500 3893 decref_bp_location (&bl);
1123588c 3894 moribund_locations.clear ();
c906108c
SS
3895}
3896
6c95b8df
PA
3897/* These functions concern about actual breakpoints inserted in the
3898 target --- to e.g. check if we need to do decr_pc adjustment or if
3899 we need to hop over the bkpt --- so we check for address space
3900 match, not program space. */
3901
c2c6d25f
JM
3902/* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
3903 exists at PC. It returns ordinary_breakpoint_here if it's an
3904 ordinary breakpoint, or permanent_breakpoint_here if it's a
3905 permanent breakpoint.
3906 - When continuing from a location with an ordinary breakpoint, we
3907 actually single step once before calling insert_breakpoints.
e5dd4106 3908 - When continuing from a location with a permanent breakpoint, we
c2c6d25f
JM
3909 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
3910 the target, to advance the PC past the breakpoint. */
c906108c 3911
c2c6d25f 3912enum breakpoint_here
accd0bcd 3913breakpoint_here_p (const address_space *aspace, CORE_ADDR pc)
c906108c 3914{
35df4500 3915 struct bp_location *bl, **blp_tmp;
c2c6d25f 3916 int any_breakpoint_here = 0;
c906108c 3917
35df4500 3918 ALL_BP_LOCATIONS (bl, blp_tmp)
075f6582 3919 {
35df4500
TJB
3920 if (bl->loc_type != bp_loc_software_breakpoint
3921 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
3922 continue;
3923
f1310107 3924 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
35df4500 3925 if ((breakpoint_enabled (bl->owner)
1a853c52 3926 || bl->permanent)
f1310107 3927 && breakpoint_location_address_match (bl, aspace, pc))
075f6582
DJ
3928 {
3929 if (overlay_debugging
35df4500
TJB
3930 && section_is_overlay (bl->section)
3931 && !section_is_mapped (bl->section))
075f6582 3932 continue; /* unmapped overlay -- can't be a match */
1a853c52 3933 else if (bl->permanent)
075f6582
DJ
3934 return permanent_breakpoint_here;
3935 else
3936 any_breakpoint_here = 1;
3937 }
3938 }
c906108c 3939
f486487f 3940 return any_breakpoint_here ? ordinary_breakpoint_here : no_breakpoint_here;
c906108c
SS
3941}
3942
d35ae833
PA
3943/* See breakpoint.h. */
3944
3945int
accd0bcd 3946breakpoint_in_range_p (const address_space *aspace,
d35ae833
PA
3947 CORE_ADDR addr, ULONGEST len)
3948{
3949 struct bp_location *bl, **blp_tmp;
3950
3951 ALL_BP_LOCATIONS (bl, blp_tmp)
3952 {
3953 if (bl->loc_type != bp_loc_software_breakpoint
3954 && bl->loc_type != bp_loc_hardware_breakpoint)
3955 continue;
3956
3957 if ((breakpoint_enabled (bl->owner)
3958 || bl->permanent)
3959 && breakpoint_location_address_range_overlap (bl, aspace,
3960 addr, len))
3961 {
3962 if (overlay_debugging
3963 && section_is_overlay (bl->section)
3964 && !section_is_mapped (bl->section))
3965 {
3966 /* Unmapped overlay -- can't be a match. */
3967 continue;
3968 }
3969
3970 return 1;
3971 }
3972 }
3973
3974 return 0;
3975}
3976
1c5cfe86
PA
3977/* Return true if there's a moribund breakpoint at PC. */
3978
3979int
accd0bcd 3980moribund_breakpoint_here_p (const address_space *aspace, CORE_ADDR pc)
1c5cfe86 3981{
1123588c 3982 for (bp_location *loc : moribund_locations)
f1310107 3983 if (breakpoint_location_address_match (loc, aspace, pc))
1c5cfe86
PA
3984 return 1;
3985
3986 return 0;
3987}
c2c6d25f 3988
f7ce857f
PA
3989/* Returns non-zero iff BL is inserted at PC, in address space
3990 ASPACE. */
3991
3992static int
3993bp_location_inserted_here_p (struct bp_location *bl,
accd0bcd 3994 const address_space *aspace, CORE_ADDR pc)
f7ce857f
PA
3995{
3996 if (bl->inserted
3997 && breakpoint_address_match (bl->pspace->aspace, bl->address,
3998 aspace, pc))
3999 {
4000 if (overlay_debugging
4001 && section_is_overlay (bl->section)
4002 && !section_is_mapped (bl->section))
4003 return 0; /* unmapped overlay -- can't be a match */
4004 else
4005 return 1;
4006 }
4007 return 0;
4008}
4009
a1fd2fa5 4010/* Returns non-zero iff there's a breakpoint inserted at PC. */
c906108c
SS
4011
4012int
accd0bcd 4013breakpoint_inserted_here_p (const address_space *aspace, CORE_ADDR pc)
c906108c 4014{
f7ce857f 4015 struct bp_location **blp, **blp_tmp = NULL;
c906108c 4016
f7ce857f 4017 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
c5aa993b 4018 {
f7ce857f
PA
4019 struct bp_location *bl = *blp;
4020
35df4500
TJB
4021 if (bl->loc_type != bp_loc_software_breakpoint
4022 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
4023 continue;
4024
f7ce857f
PA
4025 if (bp_location_inserted_here_p (bl, aspace, pc))
4026 return 1;
c5aa993b 4027 }
c36b740a
VP
4028 return 0;
4029}
4030
a1fd2fa5
PA
4031/* This function returns non-zero iff there is a software breakpoint
4032 inserted at PC. */
c36b740a
VP
4033
4034int
accd0bcd 4035software_breakpoint_inserted_here_p (const address_space *aspace,
a1fd2fa5 4036 CORE_ADDR pc)
4fa8626c 4037{
f7ce857f 4038 struct bp_location **blp, **blp_tmp = NULL;
4fa8626c 4039
f7ce857f 4040 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4fa8626c 4041 {
f7ce857f
PA
4042 struct bp_location *bl = *blp;
4043
35df4500 4044 if (bl->loc_type != bp_loc_software_breakpoint)
4fa8626c
DJ
4045 continue;
4046
f7ce857f
PA
4047 if (bp_location_inserted_here_p (bl, aspace, pc))
4048 return 1;
4fa8626c
DJ
4049 }
4050
4051 return 0;
9c02b525
PA
4052}
4053
4054/* See breakpoint.h. */
4055
4056int
accd0bcd 4057hardware_breakpoint_inserted_here_p (const address_space *aspace,
9c02b525
PA
4058 CORE_ADDR pc)
4059{
4060 struct bp_location **blp, **blp_tmp = NULL;
9c02b525
PA
4061
4062 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4063 {
4064 struct bp_location *bl = *blp;
4065
4066 if (bl->loc_type != bp_loc_hardware_breakpoint)
4067 continue;
4068
4069 if (bp_location_inserted_here_p (bl, aspace, pc))
4070 return 1;
4071 }
4072
4073 return 0;
4fa8626c
DJ
4074}
4075
9093389c 4076int
accd0bcd 4077hardware_watchpoint_inserted_in_range (const address_space *aspace,
9093389c
PA
4078 CORE_ADDR addr, ULONGEST len)
4079{
4080 struct breakpoint *bpt;
4081
4082 ALL_BREAKPOINTS (bpt)
4083 {
4084 struct bp_location *loc;
4085
4086 if (bpt->type != bp_hardware_watchpoint
4087 && bpt->type != bp_access_watchpoint)
4088 continue;
4089
4090 if (!breakpoint_enabled (bpt))
4091 continue;
4092
4093 for (loc = bpt->loc; loc; loc = loc->next)
4094 if (loc->pspace->aspace == aspace && loc->inserted)
4095 {
4096 CORE_ADDR l, h;
4097
4098 /* Check for intersection. */
768adc05
PA
4099 l = std::max<CORE_ADDR> (loc->address, addr);
4100 h = std::min<CORE_ADDR> (loc->address + loc->length, addr + len);
9093389c
PA
4101 if (l < h)
4102 return 1;
4103 }
4104 }
4105 return 0;
4106}
c5aa993b 4107
f2478a7e 4108/* See breakpoint.h. */
c906108c 4109
f2478a7e
SM
4110bool
4111is_catchpoint (struct breakpoint *b)
c906108c 4112{
f2478a7e 4113 return (b->type == bp_catchpoint);
c906108c
SS
4114}
4115
f431efe5
PA
4116/* Frees any storage that is part of a bpstat. Does not walk the
4117 'next' chain. */
4118
04afa70c 4119bpstats::~bpstats ()
198757a8 4120{
04afa70c
TT
4121 if (bp_location_at != NULL)
4122 decref_bp_location (&bp_location_at);
198757a8
VP
4123}
4124
c906108c
SS
4125/* Clear a bpstat so that it says we are not at any breakpoint.
4126 Also free any storage that is part of a bpstat. */
4127
4128void
fba45db2 4129bpstat_clear (bpstat *bsp)
c906108c
SS
4130{
4131 bpstat p;
4132 bpstat q;
4133
4134 if (bsp == 0)
4135 return;
4136 p = *bsp;
4137 while (p != NULL)
4138 {
4139 q = p->next;
04afa70c 4140 delete p;
c906108c
SS
4141 p = q;
4142 }
4143 *bsp = NULL;
4144}
4145
04afa70c
TT
4146bpstats::bpstats (const bpstats &other)
4147 : next (NULL),
4148 bp_location_at (other.bp_location_at),
4149 breakpoint_at (other.breakpoint_at),
4150 commands (other.commands),
04afa70c
TT
4151 print (other.print),
4152 stop (other.stop),
4153 print_it (other.print_it)
4154{
850645cf
TT
4155 if (other.old_val != NULL)
4156 old_val = release_value (value_copy (other.old_val.get ()));
04afa70c 4157 incref_bp_location (bp_location_at);
04afa70c
TT
4158}
4159
c906108c
SS
4160/* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
4161 is part of the bpstat is copied as well. */
4162
4163bpstat
fba45db2 4164bpstat_copy (bpstat bs)
c906108c
SS
4165{
4166 bpstat p = NULL;
4167 bpstat tmp;
4168 bpstat retval = NULL;
4169
4170 if (bs == NULL)
4171 return bs;
4172
4173 for (; bs != NULL; bs = bs->next)
4174 {
04afa70c 4175 tmp = new bpstats (*bs);
31cc81e9 4176
c906108c
SS
4177 if (p == NULL)
4178 /* This is the first thing in the chain. */
4179 retval = tmp;
4180 else
4181 p->next = tmp;
4182 p = tmp;
4183 }
4184 p->next = NULL;
4185 return retval;
4186}
4187
4a64f543 4188/* Find the bpstat associated with this breakpoint. */
c906108c
SS
4189
4190bpstat
fba45db2 4191bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
c906108c 4192{
c5aa993b
JM
4193 if (bsp == NULL)
4194 return NULL;
c906108c 4195
c5aa993b
JM
4196 for (; bsp != NULL; bsp = bsp->next)
4197 {
f431efe5 4198 if (bsp->breakpoint_at == breakpoint)
c5aa993b
JM
4199 return bsp;
4200 }
c906108c
SS
4201 return NULL;
4202}
4203
ab04a2af
TT
4204/* See breakpoint.h. */
4205
4c462cb0 4206bool
427cd150 4207bpstat_explains_signal (bpstat bsp, enum gdb_signal sig)
ab04a2af 4208{
ab04a2af
TT
4209 for (; bsp != NULL; bsp = bsp->next)
4210 {
427cd150
TT
4211 if (bsp->breakpoint_at == NULL)
4212 {
4213 /* A moribund location can never explain a signal other than
4214 GDB_SIGNAL_TRAP. */
4215 if (sig == GDB_SIGNAL_TRAP)
4c462cb0 4216 return true;
427cd150
TT
4217 }
4218 else
47591c29
PA
4219 {
4220 if (bsp->breakpoint_at->ops->explains_signal (bsp->breakpoint_at,
4221 sig))
4c462cb0 4222 return true;
47591c29 4223 }
ab04a2af
TT
4224 }
4225
4c462cb0 4226 return false;
ab04a2af
TT
4227}
4228
4a64f543
MS
4229/* Put in *NUM the breakpoint number of the first breakpoint we are
4230 stopped at. *BSP upon return is a bpstat which points to the
4231 remaining breakpoints stopped at (but which is not guaranteed to be
4232 good for anything but further calls to bpstat_num).
4233
8671a17b
PA
4234 Return 0 if passed a bpstat which does not indicate any breakpoints.
4235 Return -1 if stopped at a breakpoint that has been deleted since
4236 we set it.
4237 Return 1 otherwise. */
c906108c
SS
4238
4239int
8671a17b 4240bpstat_num (bpstat *bsp, int *num)
c906108c
SS
4241{
4242 struct breakpoint *b;
4243
4244 if ((*bsp) == NULL)
4245 return 0; /* No more breakpoint values */
8671a17b 4246
4a64f543
MS
4247 /* We assume we'll never have several bpstats that correspond to a
4248 single breakpoint -- otherwise, this function might return the
4249 same number more than once and this will look ugly. */
f431efe5 4250 b = (*bsp)->breakpoint_at;
8671a17b
PA
4251 *bsp = (*bsp)->next;
4252 if (b == NULL)
4253 return -1; /* breakpoint that's been deleted since */
4254
4255 *num = b->number; /* We have its number */
4256 return 1;
c906108c
SS
4257}
4258
e93ca019 4259/* See breakpoint.h. */
c906108c
SS
4260
4261void
e93ca019 4262bpstat_clear_actions (void)
c906108c 4263{
e93ca019
JK
4264 bpstat bs;
4265
00431a78 4266 if (inferior_ptid == null_ptid)
e93ca019
JK
4267 return;
4268
00431a78 4269 thread_info *tp = inferior_thread ();
e93ca019 4270 for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
c906108c 4271 {
d1b0a7bf 4272 bs->commands = NULL;
850645cf 4273 bs->old_val.reset (nullptr);
c906108c
SS
4274 }
4275}
4276
f3b1572e
PA
4277/* Called when a command is about to proceed the inferior. */
4278
4279static void
4280breakpoint_about_to_proceed (void)
4281{
d7e15655 4282 if (inferior_ptid != null_ptid)
f3b1572e
PA
4283 {
4284 struct thread_info *tp = inferior_thread ();
4285
4286 /* Allow inferior function calls in breakpoint commands to not
4287 interrupt the command list. When the call finishes
4288 successfully, the inferior will be standing at the same
4289 breakpoint as if nothing happened. */
16c381f0 4290 if (tp->control.in_infcall)
f3b1572e
PA
4291 return;
4292 }
4293
4294 breakpoint_proceeded = 1;
4295}
4296
abf85f46
JK
4297/* Return non-zero iff CMD as the first line of a command sequence is `silent'
4298 or its equivalent. */
4299
4300static int
4301command_line_is_silent (struct command_line *cmd)
4302{
4f45d445 4303 return cmd && (strcmp ("silent", cmd->line) == 0);
abf85f46
JK
4304}
4305
4a64f543
MS
4306/* Execute all the commands associated with all the breakpoints at
4307 this location. Any of these commands could cause the process to
4308 proceed beyond this point, etc. We look out for such changes by
4309 checking the global "breakpoint_proceeded" after each command.
c906108c 4310
347bddb7
PA
4311 Returns true if a breakpoint command resumed the inferior. In that
4312 case, it is the caller's responsibility to recall it again with the
4313 bpstat of the current thread. */
4314
4315static int
4316bpstat_do_actions_1 (bpstat *bsp)
c906108c
SS
4317{
4318 bpstat bs;
347bddb7 4319 int again = 0;
c906108c
SS
4320
4321 /* Avoid endless recursion if a `source' command is contained
4322 in bs->commands. */
4323 if (executing_breakpoint_commands)
347bddb7 4324 return 0;
c906108c 4325
81b1e71c
TT
4326 scoped_restore save_executing
4327 = make_scoped_restore (&executing_breakpoint_commands, 1);
c906108c 4328
1ac32117 4329 scoped_restore preventer = prevent_dont_repeat ();
cf6c5ffb 4330
4a64f543 4331 /* This pointer will iterate over the list of bpstat's. */
c906108c
SS
4332 bs = *bsp;
4333
4334 breakpoint_proceeded = 0;
4335 for (; bs != NULL; bs = bs->next)
4336 {
d1b0a7bf 4337 struct command_line *cmd = NULL;
6c50ab1c
JB
4338
4339 /* Take ownership of the BSP's command tree, if it has one.
4340
4341 The command tree could legitimately contain commands like
4342 'step' and 'next', which call clear_proceed_status, which
4343 frees stop_bpstat's command tree. To make sure this doesn't
4344 free the tree we're executing out from under us, we need to
4345 take ownership of the tree ourselves. Since a given bpstat's
4346 commands are only executed once, we don't need to copy it; we
4347 can clear the pointer in the bpstat, and make sure we free
4348 the tree when we're done. */
d1b0a7bf 4349 counted_command_line ccmd = bs->commands;
9add0f1b 4350 bs->commands = NULL;
d1b0a7bf
TT
4351 if (ccmd != NULL)
4352 cmd = ccmd.get ();
abf85f46
JK
4353 if (command_line_is_silent (cmd))
4354 {
4355 /* The action has been already done by bpstat_stop_status. */
4356 cmd = cmd->next;
4357 }
6c50ab1c 4358
c906108c
SS
4359 while (cmd != NULL)
4360 {
4361 execute_control_command (cmd);
4362
4363 if (breakpoint_proceeded)
4364 break;
4365 else
4366 cmd = cmd->next;
4367 }
6c50ab1c 4368
c906108c 4369 if (breakpoint_proceeded)
32c1e744 4370 {
cb814510 4371 if (current_ui->async)
347bddb7
PA
4372 /* If we are in async mode, then the target might be still
4373 running, not stopped at any breakpoint, so nothing for
4374 us to do here -- just return to the event loop. */
4375 ;
32c1e744
VP
4376 else
4377 /* In sync mode, when execute_control_command returns
4378 we're already standing on the next breakpoint.
347bddb7
PA
4379 Breakpoint commands for that stop were not run, since
4380 execute_command does not run breakpoint commands --
4381 only command_line_handler does, but that one is not
4382 involved in execution of breakpoint commands. So, we
4383 can now execute breakpoint commands. It should be
4384 noted that making execute_command do bpstat actions is
4385 not an option -- in this case we'll have recursive
4386 invocation of bpstat for each breakpoint with a
4387 command, and can easily blow up GDB stack. Instead, we
4388 return true, which will trigger the caller to recall us
4389 with the new stop_bpstat. */
4390 again = 1;
4391 break;
32c1e744 4392 }
c906108c 4393 }
347bddb7
PA
4394 return again;
4395}
4396
00431a78
PA
4397/* Helper for bpstat_do_actions. Get the current thread, if there's
4398 one, is alive and has execution. Return NULL otherwise. */
4399
4400static thread_info *
4401get_bpstat_thread ()
4402{
4403 if (inferior_ptid == null_ptid || !target_has_execution)
4404 return NULL;
4405
4406 thread_info *tp = inferior_thread ();
4407 if (tp->state == THREAD_EXITED || tp->executing)
4408 return NULL;
4409 return tp;
4410}
4411
347bddb7
PA
4412void
4413bpstat_do_actions (void)
4414{
694c6bf5 4415 auto cleanup_if_error = make_scope_exit (bpstat_clear_actions);
00431a78 4416 thread_info *tp;
353d1d73 4417
347bddb7 4418 /* Do any commands attached to breakpoint we are stopped at. */
00431a78
PA
4419 while ((tp = get_bpstat_thread ()) != NULL)
4420 {
4421 /* Since in sync mode, bpstat_do_actions may resume the
4422 inferior, and only return when it is stopped at the next
4423 breakpoint, we keep doing breakpoint actions until it returns
4424 false to indicate the inferior was not resumed. */
4425 if (!bpstat_do_actions_1 (&tp->control.stop_bpstat))
4426 break;
4427 }
353d1d73 4428
694c6bf5 4429 cleanup_if_error.release ();
c906108c
SS
4430}
4431
fa4727a6
DJ
4432/* Print out the (old or new) value associated with a watchpoint. */
4433
4434static void
4435watchpoint_value_print (struct value *val, struct ui_file *stream)
4436{
4437 if (val == NULL)
7f6aba03 4438 fprintf_styled (stream, metadata_style.style (), _("<unreadable>"));
fa4727a6 4439 else
79a45b7d
TT
4440 {
4441 struct value_print_options opts;
4442 get_user_print_options (&opts);
4443 value_print (val, stream, &opts);
4444 }
fa4727a6
DJ
4445}
4446
f303dbd6
PA
4447/* Print the "Thread ID hit" part of "Thread ID hit Breakpoint N" if
4448 debugging multiple threads. */
4449
4450void
4451maybe_print_thread_hit_breakpoint (struct ui_out *uiout)
4452{
112e8700 4453 if (uiout->is_mi_like_p ())
f303dbd6
PA
4454 return;
4455
112e8700 4456 uiout->text ("\n");
f303dbd6
PA
4457
4458 if (show_thread_that_caused_stop ())
4459 {
4460 const char *name;
4461 struct thread_info *thr = inferior_thread ();
4462
112e8700 4463 uiout->text ("Thread ");
33eca680 4464 uiout->field_string ("thread-id", print_thread_id (thr));
f303dbd6
PA
4465
4466 name = thr->name != NULL ? thr->name : target_thread_name (thr);
4467 if (name != NULL)
4468 {
112e8700 4469 uiout->text (" \"");
33eca680 4470 uiout->field_string ("name", name);
112e8700 4471 uiout->text ("\"");
f303dbd6
PA
4472 }
4473
112e8700 4474 uiout->text (" hit ");
f303dbd6
PA
4475 }
4476}
4477
e514a9d6 4478/* Generic routine for printing messages indicating why we
4a64f543 4479 stopped. The behavior of this function depends on the value
e514a9d6
JM
4480 'print_it' in the bpstat structure. Under some circumstances we
4481 may decide not to print anything here and delegate the task to
4a64f543 4482 normal_stop(). */
e514a9d6
JM
4483
4484static enum print_stop_action
4485print_bp_stop_message (bpstat bs)
4486{
4487 switch (bs->print_it)
4488 {
4489 case print_it_noop:
4a64f543 4490 /* Nothing should be printed for this bpstat entry. */
e514a9d6
JM
4491 return PRINT_UNKNOWN;
4492 break;
4493
4494 case print_it_done:
4495 /* We still want to print the frame, but we already printed the
4a64f543 4496 relevant messages. */
e514a9d6
JM
4497 return PRINT_SRC_AND_LOC;
4498 break;
4499
4500 case print_it_normal:
4f8d1dc6 4501 {
f431efe5
PA
4502 struct breakpoint *b = bs->breakpoint_at;
4503
1a6a67de
TJB
4504 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
4505 which has since been deleted. */
4506 if (b == NULL)
4507 return PRINT_UNKNOWN;
4508
348d480f
PA
4509 /* Normal case. Call the breakpoint's print_it method. */
4510 return b->ops->print_it (bs);
4f8d1dc6 4511 }
348d480f 4512 break;
3086aeae 4513
e514a9d6 4514 default:
8e65ff28 4515 internal_error (__FILE__, __LINE__,
e2e0b3e5 4516 _("print_bp_stop_message: unrecognized enum value"));
e514a9d6 4517 break;
c906108c 4518 }
c906108c
SS
4519}
4520
edcc5120
TT
4521/* A helper function that prints a shared library stopped event. */
4522
4523static void
4524print_solib_event (int is_catchpoint)
4525{
6fb16ce6 4526 bool any_deleted = !current_program_space->deleted_solibs.empty ();
bcb430e4 4527 bool any_added = !current_program_space->added_solibs.empty ();
edcc5120
TT
4528
4529 if (!is_catchpoint)
4530 {
4531 if (any_added || any_deleted)
112e8700 4532 current_uiout->text (_("Stopped due to shared library event:\n"));
edcc5120 4533 else
112e8700
SM
4534 current_uiout->text (_("Stopped due to shared library event (no "
4535 "libraries added or removed)\n"));
edcc5120
TT
4536 }
4537
112e8700
SM
4538 if (current_uiout->is_mi_like_p ())
4539 current_uiout->field_string ("reason",
4540 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
edcc5120
TT
4541
4542 if (any_deleted)
4543 {
112e8700 4544 current_uiout->text (_(" Inferior unloaded "));
10f489e5 4545 ui_out_emit_list list_emitter (current_uiout, "removed");
6fb16ce6 4546 for (int ix = 0; ix < current_program_space->deleted_solibs.size (); ix++)
edcc5120 4547 {
6fb16ce6
SM
4548 const std::string &name = current_program_space->deleted_solibs[ix];
4549
edcc5120 4550 if (ix > 0)
112e8700
SM
4551 current_uiout->text (" ");
4552 current_uiout->field_string ("library", name);
4553 current_uiout->text ("\n");
edcc5120 4554 }
edcc5120
TT
4555 }
4556
4557 if (any_added)
4558 {
112e8700 4559 current_uiout->text (_(" Inferior loaded "));
10f489e5 4560 ui_out_emit_list list_emitter (current_uiout, "added");
bcb430e4 4561 bool first = true;
52941706 4562 for (so_list *iter : current_program_space->added_solibs)
edcc5120 4563 {
bcb430e4 4564 if (!first)
112e8700 4565 current_uiout->text (" ");
bcb430e4 4566 first = false;
112e8700
SM
4567 current_uiout->field_string ("library", iter->so_name);
4568 current_uiout->text ("\n");
edcc5120 4569 }
edcc5120
TT
4570 }
4571}
4572
e514a9d6
JM
4573/* Print a message indicating what happened. This is called from
4574 normal_stop(). The input to this routine is the head of the bpstat
36dfb11c
TT
4575 list - a list of the eventpoints that caused this stop. KIND is
4576 the target_waitkind for the stopping event. This
e514a9d6
JM
4577 routine calls the generic print routine for printing a message
4578 about reasons for stopping. This will print (for example) the
4579 "Breakpoint n," part of the output. The return value of this
4580 routine is one of:
c906108c 4581
4a64f543 4582 PRINT_UNKNOWN: Means we printed nothing.
917317f4 4583 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4a64f543 4584 code to print the location. An example is
c5aa993b
JM
4585 "Breakpoint 1, " which should be followed by
4586 the location.
917317f4 4587 PRINT_SRC_ONLY: Means we printed something, but there is no need
c5aa993b
JM
4588 to also print the location part of the message.
4589 An example is the catch/throw messages, which
4a64f543 4590 don't require a location appended to the end.
917317f4 4591 PRINT_NOTHING: We have done some printing and we don't need any
4a64f543 4592 further info to be printed. */
c906108c 4593
917317f4 4594enum print_stop_action
36dfb11c 4595bpstat_print (bpstat bs, int kind)
c906108c 4596{
f486487f 4597 enum print_stop_action val;
c5aa993b 4598
c906108c 4599 /* Maybe another breakpoint in the chain caused us to stop.
53a5351d
JM
4600 (Currently all watchpoints go on the bpstat whether hit or not.
4601 That probably could (should) be changed, provided care is taken
c906108c 4602 with respect to bpstat_explains_signal). */
e514a9d6
JM
4603 for (; bs; bs = bs->next)
4604 {
4605 val = print_bp_stop_message (bs);
4606 if (val == PRINT_SRC_ONLY
4607 || val == PRINT_SRC_AND_LOC
4608 || val == PRINT_NOTHING)
4609 return val;
4610 }
c906108c 4611
36dfb11c
TT
4612 /* If we had hit a shared library event breakpoint,
4613 print_bp_stop_message would print out this message. If we hit an
4614 OS-level shared library event, do the same thing. */
4615 if (kind == TARGET_WAITKIND_LOADED)
4616 {
edcc5120 4617 print_solib_event (0);
36dfb11c
TT
4618 return PRINT_NOTHING;
4619 }
4620
e514a9d6 4621 /* We reached the end of the chain, or we got a null BS to start
4a64f543 4622 with and nothing was printed. */
917317f4 4623 return PRINT_UNKNOWN;
c906108c
SS
4624}
4625
bf469271 4626/* Evaluate the boolean expression EXP and return the result. */
c906108c 4627
bf469271
PA
4628static bool
4629breakpoint_cond_eval (expression *exp)
c906108c 4630{
278cd55f 4631 struct value *mark = value_mark ();
bf469271 4632 bool res = value_true (evaluate_expression (exp));
cc59ec59 4633
c906108c 4634 value_free_to_mark (mark);
bf469271 4635 return res;
c906108c
SS
4636}
4637
5760d0ab 4638/* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
c906108c 4639
04afa70c
TT
4640bpstats::bpstats (struct bp_location *bl, bpstat **bs_link_pointer)
4641 : next (NULL),
4642 bp_location_at (bl),
4643 breakpoint_at (bl->owner),
4644 commands (NULL),
04afa70c
TT
4645 print (0),
4646 stop (0),
4647 print_it (print_it_normal)
c906108c 4648{
f431efe5 4649 incref_bp_location (bl);
04afa70c
TT
4650 **bs_link_pointer = this;
4651 *bs_link_pointer = &next;
4652}
4653
4654bpstats::bpstats ()
4655 : next (NULL),
4656 bp_location_at (NULL),
4657 breakpoint_at (NULL),
4658 commands (NULL),
04afa70c
TT
4659 print (0),
4660 stop (0),
4661 print_it (print_it_normal)
4662{
c906108c
SS
4663}
4664\f
d983da9c
DJ
4665/* The target has stopped with waitstatus WS. Check if any hardware
4666 watchpoints have triggered, according to the target. */
4667
4668int
4669watchpoints_triggered (struct target_waitstatus *ws)
4670{
57810aa7 4671 bool stopped_by_watchpoint = target_stopped_by_watchpoint ();
d983da9c
DJ
4672 CORE_ADDR addr;
4673 struct breakpoint *b;
4674
4675 if (!stopped_by_watchpoint)
4676 {
4677 /* We were not stopped by a watchpoint. Mark all watchpoints
4678 as not triggered. */
4679 ALL_BREAKPOINTS (b)
cc60f2e3 4680 if (is_hardware_watchpoint (b))
3a5c3e22
PA
4681 {
4682 struct watchpoint *w = (struct watchpoint *) b;
4683
4684 w->watchpoint_triggered = watch_triggered_no;
4685 }
d983da9c
DJ
4686
4687 return 0;
4688 }
4689
8b88a78e 4690 if (!target_stopped_data_address (current_top_target (), &addr))
d983da9c
DJ
4691 {
4692 /* We were stopped by a watchpoint, but we don't know where.
4693 Mark all watchpoints as unknown. */
4694 ALL_BREAKPOINTS (b)
cc60f2e3 4695 if (is_hardware_watchpoint (b))
3a5c3e22
PA
4696 {
4697 struct watchpoint *w = (struct watchpoint *) b;
4698
4699 w->watchpoint_triggered = watch_triggered_unknown;
4700 }
d983da9c 4701
3c4797ba 4702 return 1;
d983da9c
DJ
4703 }
4704
4705 /* The target could report the data address. Mark watchpoints
4706 affected by this data address as triggered, and all others as not
4707 triggered. */
4708
4709 ALL_BREAKPOINTS (b)
cc60f2e3 4710 if (is_hardware_watchpoint (b))
d983da9c 4711 {
3a5c3e22 4712 struct watchpoint *w = (struct watchpoint *) b;
a5606eee 4713 struct bp_location *loc;
d983da9c 4714
3a5c3e22 4715 w->watchpoint_triggered = watch_triggered_no;
a5606eee 4716 for (loc = b->loc; loc; loc = loc->next)
9c06b0b4 4717 {
3a5c3e22 4718 if (is_masked_watchpoint (b))
9c06b0b4 4719 {
3a5c3e22
PA
4720 CORE_ADDR newaddr = addr & w->hw_wp_mask;
4721 CORE_ADDR start = loc->address & w->hw_wp_mask;
9c06b0b4
TJB
4722
4723 if (newaddr == start)
4724 {
3a5c3e22 4725 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
4726 break;
4727 }
4728 }
4729 /* Exact match not required. Within range is sufficient. */
8b88a78e 4730 else if (target_watchpoint_addr_within_range (current_top_target (),
9c06b0b4
TJB
4731 addr, loc->address,
4732 loc->length))
4733 {
3a5c3e22 4734 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
4735 break;
4736 }
4737 }
d983da9c
DJ
4738 }
4739
4740 return 1;
4741}
4742
bf469271
PA
4743/* Possible return values for watchpoint_check. */
4744enum wp_check_result
4745 {
4746 /* The watchpoint has been deleted. */
4747 WP_DELETED = 1,
4748
4749 /* The value has changed. */
4750 WP_VALUE_CHANGED = 2,
4751
4752 /* The value has not changed. */
4753 WP_VALUE_NOT_CHANGED = 3,
4754
4755 /* Ignore this watchpoint, no matter if the value changed or not. */
4756 WP_IGNORE = 4,
4757 };
c906108c
SS
4758
4759#define BP_TEMPFLAG 1
4760#define BP_HARDWAREFLAG 2
4761
4a64f543 4762/* Evaluate watchpoint condition expression and check if its value
bf469271 4763 changed. */
553e4c11 4764
bf469271
PA
4765static wp_check_result
4766watchpoint_check (bpstat bs)
c906108c 4767{
3a5c3e22 4768 struct watchpoint *b;
c906108c
SS
4769 struct frame_info *fr;
4770 int within_current_scope;
4771
f431efe5 4772 /* BS is built from an existing struct breakpoint. */
2bdf28a0 4773 gdb_assert (bs->breakpoint_at != NULL);
3a5c3e22 4774 b = (struct watchpoint *) bs->breakpoint_at;
d0fb5eae 4775
f6bc2008
PA
4776 /* If this is a local watchpoint, we only want to check if the
4777 watchpoint frame is in scope if the current thread is the thread
4778 that was used to create the watchpoint. */
4779 if (!watchpoint_in_thread_scope (b))
60e1c644 4780 return WP_IGNORE;
f6bc2008 4781
c906108c
SS
4782 if (b->exp_valid_block == NULL)
4783 within_current_scope = 1;
4784 else
4785 {
edb3359d
DJ
4786 struct frame_info *frame = get_current_frame ();
4787 struct gdbarch *frame_arch = get_frame_arch (frame);
4788 CORE_ADDR frame_pc = get_frame_pc (frame);
4789
c9cf6e20 4790 /* stack_frame_destroyed_p() returns a non-zero value if we're
4a64f543
MS
4791 still in the function but the stack frame has already been
4792 invalidated. Since we can't rely on the values of local
4793 variables after the stack has been destroyed, we are treating
4794 the watchpoint in that state as `not changed' without further
4795 checking. Don't mark watchpoints as changed if the current
4796 frame is in an epilogue - even if they are in some other
4797 frame, our view of the stack is likely to be wrong and
4798 frame_find_by_id could error out. */
c9cf6e20 4799 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
60e1c644 4800 return WP_IGNORE;
a0f49112 4801
101dcfbe 4802 fr = frame_find_by_id (b->watchpoint_frame);
c906108c 4803 within_current_scope = (fr != NULL);
69fbadd5
DJ
4804
4805 /* If we've gotten confused in the unwinder, we might have
4806 returned a frame that can't describe this variable. */
edb3359d
DJ
4807 if (within_current_scope)
4808 {
4809 struct symbol *function;
4810
4811 function = get_frame_function (fr);
4812 if (function == NULL
4813 || !contained_in (b->exp_valid_block,
4814 SYMBOL_BLOCK_VALUE (function)))
4815 within_current_scope = 0;
4816 }
69fbadd5 4817
edb3359d 4818 if (within_current_scope)
c906108c
SS
4819 /* If we end up stopping, the current frame will get selected
4820 in normal_stop. So this call to select_frame won't affect
4821 the user. */
0f7d239c 4822 select_frame (fr);
c906108c 4823 }
c5aa993b 4824
c906108c
SS
4825 if (within_current_scope)
4826 {
4a64f543
MS
4827 /* We use value_{,free_to_}mark because it could be a *long*
4828 time before we return to the command level and call
4829 free_all_values. We can't call free_all_values because we
4830 might be in the middle of evaluating a function call. */
c906108c 4831
0cf6dd15 4832 int pc = 0;
9c06b0b4 4833 struct value *mark;
fa4727a6
DJ
4834 struct value *new_val;
4835
c1fc2657 4836 if (is_masked_watchpoint (b))
9c06b0b4
TJB
4837 /* Since we don't know the exact trigger address (from
4838 stopped_data_address), just tell the user we've triggered
4839 a mask watchpoint. */
4840 return WP_VALUE_CHANGED;
4841
4842 mark = value_mark ();
4d01a485 4843 fetch_subexp_value (b->exp.get (), &pc, &new_val, NULL, NULL, 0);
218d2fc6 4844
bb9d5f81
PP
4845 if (b->val_bitsize != 0)
4846 new_val = extract_bitfield_from_watchpoint_value (b, new_val);
4847
4a64f543
MS
4848 /* We use value_equal_contents instead of value_equal because
4849 the latter coerces an array to a pointer, thus comparing just
4850 the address of the array instead of its contents. This is
4851 not what we want. */
fa4727a6 4852 if ((b->val != NULL) != (new_val != NULL)
850645cf
TT
4853 || (b->val != NULL && !value_equal_contents (b->val.get (),
4854 new_val)))
c906108c 4855 {
c906108c 4856 bs->old_val = b->val;
850645cf 4857 b->val = release_value (new_val);
4c1d86d9 4858 b->val_valid = true;
850645cf
TT
4859 if (new_val != NULL)
4860 value_free_to_mark (mark);
c906108c
SS
4861 return WP_VALUE_CHANGED;
4862 }
4863 else
4864 {
60e1c644 4865 /* Nothing changed. */
c906108c 4866 value_free_to_mark (mark);
c906108c
SS
4867 return WP_VALUE_NOT_CHANGED;
4868 }
4869 }
4870 else
4871 {
4872 /* This seems like the only logical thing to do because
c5aa993b
JM
4873 if we temporarily ignored the watchpoint, then when
4874 we reenter the block in which it is valid it contains
4875 garbage (in the case of a function, it may have two
4876 garbage values, one before and one after the prologue).
4877 So we can't even detect the first assignment to it and
4878 watch after that (since the garbage may or may not equal
4879 the first value assigned). */
348d480f
PA
4880 /* We print all the stop information in
4881 breakpoint_ops->print_it, but in this case, by the time we
4882 call breakpoint_ops->print_it this bp will be deleted
4883 already. So we have no choice but print the information
4884 here. */
468afe6c 4885
0e454242 4886 SWITCH_THRU_ALL_UIS ()
468afe6c
PA
4887 {
4888 struct ui_out *uiout = current_uiout;
4889
112e8700
SM
4890 if (uiout->is_mi_like_p ())
4891 uiout->field_string
4892 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
6a831f06
PA
4893 uiout->message ("\nWatchpoint %pF deleted because the program has "
4894 "left the block in\n"
4895 "which its expression is valid.\n",
4896 signed_field ("wpnum", b->number));
468afe6c 4897 }
4ce44c66 4898
cdac0397 4899 /* Make sure the watchpoint's commands aren't executed. */
d1b0a7bf 4900 b->commands = NULL;
d0fb5eae 4901 watchpoint_del_at_next_stop (b);
c906108c
SS
4902
4903 return WP_DELETED;
4904 }
4905}
4906
18a18393 4907/* Return true if it looks like target has stopped due to hitting
348d480f
PA
4908 breakpoint location BL. This function does not check if we should
4909 stop, only if BL explains the stop. */
4910
18a18393 4911static int
6c95b8df 4912bpstat_check_location (const struct bp_location *bl,
accd0bcd 4913 const address_space *aspace, CORE_ADDR bp_addr,
09ac7c10 4914 const struct target_waitstatus *ws)
18a18393
VP
4915{
4916 struct breakpoint *b = bl->owner;
4917
348d480f 4918 /* BL is from an existing breakpoint. */
2bdf28a0
JK
4919 gdb_assert (b != NULL);
4920
bd522513 4921 return b->ops->breakpoint_hit (bl, aspace, bp_addr, ws);
18a18393
VP
4922}
4923
3a5c3e22
PA
4924/* Determine if the watched values have actually changed, and we
4925 should stop. If not, set BS->stop to 0. */
4926
18a18393
VP
4927static void
4928bpstat_check_watchpoint (bpstat bs)
4929{
2bdf28a0 4930 const struct bp_location *bl;
3a5c3e22 4931 struct watchpoint *b;
2bdf28a0
JK
4932
4933 /* BS is built for existing struct breakpoint. */
f431efe5 4934 bl = bs->bp_location_at;
2bdf28a0 4935 gdb_assert (bl != NULL);
3a5c3e22 4936 b = (struct watchpoint *) bs->breakpoint_at;
2bdf28a0 4937 gdb_assert (b != NULL);
18a18393 4938
18a18393 4939 {
18a18393
VP
4940 int must_check_value = 0;
4941
c1fc2657 4942 if (b->type == bp_watchpoint)
18a18393
VP
4943 /* For a software watchpoint, we must always check the
4944 watched value. */
4945 must_check_value = 1;
4946 else if (b->watchpoint_triggered == watch_triggered_yes)
4947 /* We have a hardware watchpoint (read, write, or access)
4948 and the target earlier reported an address watched by
4949 this watchpoint. */
4950 must_check_value = 1;
4951 else if (b->watchpoint_triggered == watch_triggered_unknown
c1fc2657 4952 && b->type == bp_hardware_watchpoint)
18a18393
VP
4953 /* We were stopped by a hardware watchpoint, but the target could
4954 not report the data address. We must check the watchpoint's
4955 value. Access and read watchpoints are out of luck; without
4956 a data address, we can't figure it out. */
4957 must_check_value = 1;
3a5c3e22 4958
18a18393
VP
4959 if (must_check_value)
4960 {
bf469271
PA
4961 wp_check_result e;
4962
a70b8144 4963 try
bf469271
PA
4964 {
4965 e = watchpoint_check (bs);
4966 }
230d2906 4967 catch (const gdb_exception &ex)
bf469271
PA
4968 {
4969 exception_fprintf (gdb_stderr, ex,
4970 "Error evaluating expression "
4971 "for watchpoint %d\n",
4972 b->number);
4973
4974 SWITCH_THRU_ALL_UIS ()
4975 {
4976 printf_filtered (_("Watchpoint %d deleted.\n"),
4977 b->number);
4978 }
4979 watchpoint_del_at_next_stop (b);
4980 e = WP_DELETED;
4981 }
bf469271 4982
18a18393
VP
4983 switch (e)
4984 {
4985 case WP_DELETED:
4986 /* We've already printed what needs to be printed. */
4987 bs->print_it = print_it_done;
4988 /* Stop. */
4989 break;
60e1c644
PA
4990 case WP_IGNORE:
4991 bs->print_it = print_it_noop;
4992 bs->stop = 0;
4993 break;
18a18393 4994 case WP_VALUE_CHANGED:
c1fc2657 4995 if (b->type == bp_read_watchpoint)
18a18393 4996 {
85d721b8
PA
4997 /* There are two cases to consider here:
4998
4a64f543 4999 1. We're watching the triggered memory for reads.
85d721b8
PA
5000 In that case, trust the target, and always report
5001 the watchpoint hit to the user. Even though
5002 reads don't cause value changes, the value may
5003 have changed since the last time it was read, and
5004 since we're not trapping writes, we will not see
5005 those, and as such we should ignore our notion of
5006 old value.
5007
4a64f543 5008 2. We're watching the triggered memory for both
85d721b8
PA
5009 reads and writes. There are two ways this may
5010 happen:
5011
4a64f543 5012 2.1. This is a target that can't break on data
85d721b8
PA
5013 reads only, but can break on accesses (reads or
5014 writes), such as e.g., x86. We detect this case
5015 at the time we try to insert read watchpoints.
5016
4a64f543 5017 2.2. Otherwise, the target supports read
85d721b8
PA
5018 watchpoints, but, the user set an access or write
5019 watchpoint watching the same memory as this read
5020 watchpoint.
5021
5022 If we're watching memory writes as well as reads,
5023 ignore watchpoint hits when we find that the
5024 value hasn't changed, as reads don't cause
5025 changes. This still gives false positives when
5026 the program writes the same value to memory as
5027 what there was already in memory (we will confuse
5028 it for a read), but it's much better than
5029 nothing. */
5030
5031 int other_write_watchpoint = 0;
5032
5033 if (bl->watchpoint_type == hw_read)
5034 {
5035 struct breakpoint *other_b;
5036
5037 ALL_BREAKPOINTS (other_b)
3a5c3e22
PA
5038 if (other_b->type == bp_hardware_watchpoint
5039 || other_b->type == bp_access_watchpoint)
85d721b8 5040 {
3a5c3e22
PA
5041 struct watchpoint *other_w =
5042 (struct watchpoint *) other_b;
5043
5044 if (other_w->watchpoint_triggered
5045 == watch_triggered_yes)
5046 {
5047 other_write_watchpoint = 1;
5048 break;
5049 }
85d721b8
PA
5050 }
5051 }
5052
5053 if (other_write_watchpoint
5054 || bl->watchpoint_type == hw_access)
5055 {
5056 /* We're watching the same memory for writes,
5057 and the value changed since the last time we
5058 updated it, so this trap must be for a write.
5059 Ignore it. */
5060 bs->print_it = print_it_noop;
5061 bs->stop = 0;
5062 }
18a18393
VP
5063 }
5064 break;
5065 case WP_VALUE_NOT_CHANGED:
c1fc2657
SM
5066 if (b->type == bp_hardware_watchpoint
5067 || b->type == bp_watchpoint)
18a18393
VP
5068 {
5069 /* Don't stop: write watchpoints shouldn't fire if
5070 the value hasn't changed. */
5071 bs->print_it = print_it_noop;
5072 bs->stop = 0;
5073 }
5074 /* Stop. */
5075 break;
5076 default:
5077 /* Can't happen. */
18a18393
VP
5078 break;
5079 }
5080 }
5081 else /* must_check_value == 0 */
5082 {
5083 /* This is a case where some watchpoint(s) triggered, but
5084 not at the address of this watchpoint, or else no
5085 watchpoint triggered after all. So don't print
5086 anything for this watchpoint. */
5087 bs->print_it = print_it_noop;
5088 bs->stop = 0;
5089 }
5090 }
5091}
5092
7d4df6a4
DE
5093/* For breakpoints that are currently marked as telling gdb to stop,
5094 check conditions (condition proper, frame, thread and ignore count)
18a18393
VP
5095 of breakpoint referred to by BS. If we should not stop for this
5096 breakpoint, set BS->stop to 0. */
f431efe5 5097
18a18393 5098static void
00431a78 5099bpstat_check_breakpoint_conditions (bpstat bs, thread_info *thread)
18a18393 5100{
2bdf28a0
JK
5101 const struct bp_location *bl;
5102 struct breakpoint *b;
bf469271
PA
5103 /* Assume stop. */
5104 bool condition_result = true;
7d4df6a4
DE
5105 struct expression *cond;
5106
5107 gdb_assert (bs->stop);
2bdf28a0
JK
5108
5109 /* BS is built for existing struct breakpoint. */
f431efe5 5110 bl = bs->bp_location_at;
2bdf28a0 5111 gdb_assert (bl != NULL);
f431efe5 5112 b = bs->breakpoint_at;
2bdf28a0 5113 gdb_assert (b != NULL);
18a18393 5114
b775012e
LM
5115 /* Even if the target evaluated the condition on its end and notified GDB, we
5116 need to do so again since GDB does not know if we stopped due to a
5117 breakpoint or a single step breakpoint. */
5118
18a18393 5119 if (frame_id_p (b->frame_id)
edb3359d 5120 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
18a18393 5121 {
7d4df6a4
DE
5122 bs->stop = 0;
5123 return;
5124 }
60e1c644 5125
12ab52e9
PA
5126 /* If this is a thread/task-specific breakpoint, don't waste cpu
5127 evaluating the condition if this isn't the specified
5128 thread/task. */
00431a78
PA
5129 if ((b->thread != -1 && b->thread != thread->global_num)
5130 || (b->task != 0 && b->task != ada_get_task_number (thread)))
6c1b0f7b
DE
5131 {
5132 bs->stop = 0;
5133 return;
5134 }
5135
6dddc817
DE
5136 /* Evaluate extension language breakpoints that have a "stop" method
5137 implemented. */
5138 bs->stop = breakpoint_ext_lang_cond_says_stop (b);
7371cf6d 5139
7d4df6a4
DE
5140 if (is_watchpoint (b))
5141 {
5142 struct watchpoint *w = (struct watchpoint *) b;
3a5c3e22 5143
4d01a485 5144 cond = w->cond_exp.get ();
7d4df6a4
DE
5145 }
5146 else
4d01a485 5147 cond = bl->cond.get ();
60e1c644 5148
7d4df6a4
DE
5149 if (cond && b->disposition != disp_del_at_next_stop)
5150 {
5151 int within_current_scope = 1;
5152 struct watchpoint * w;
60e1c644 5153
7d4df6a4
DE
5154 /* We use value_mark and value_free_to_mark because it could
5155 be a long time before we return to the command level and
5156 call free_all_values. We can't call free_all_values
5157 because we might be in the middle of evaluating a
5158 function call. */
5159 struct value *mark = value_mark ();
5160
5161 if (is_watchpoint (b))
5162 w = (struct watchpoint *) b;
5163 else
5164 w = NULL;
5165
5166 /* Need to select the frame, with all that implies so that
5167 the conditions will have the right context. Because we
5168 use the frame, we will not see an inlined function's
5169 variables when we arrive at a breakpoint at the start
5170 of the inlined function; the current frame will be the
5171 call site. */
5172 if (w == NULL || w->cond_exp_valid_block == NULL)
5173 select_frame (get_current_frame ());
5174 else
18a18393 5175 {
7d4df6a4
DE
5176 struct frame_info *frame;
5177
5178 /* For local watchpoint expressions, which particular
5179 instance of a local is being watched matters, so we
5180 keep track of the frame to evaluate the expression
5181 in. To evaluate the condition however, it doesn't
5182 really matter which instantiation of the function
5183 where the condition makes sense triggers the
5184 watchpoint. This allows an expression like "watch
5185 global if q > 10" set in `func', catch writes to
5186 global on all threads that call `func', or catch
5187 writes on all recursive calls of `func' by a single
5188 thread. We simply always evaluate the condition in
5189 the innermost frame that's executing where it makes
5190 sense to evaluate the condition. It seems
5191 intuitive. */
5192 frame = block_innermost_frame (w->cond_exp_valid_block);
5193 if (frame != NULL)
5194 select_frame (frame);
5195 else
5196 within_current_scope = 0;
18a18393 5197 }
7d4df6a4 5198 if (within_current_scope)
bf469271 5199 {
a70b8144 5200 try
bf469271
PA
5201 {
5202 condition_result = breakpoint_cond_eval (cond);
5203 }
230d2906 5204 catch (const gdb_exception &ex)
bf469271
PA
5205 {
5206 exception_fprintf (gdb_stderr, ex,
5207 "Error in testing breakpoint condition:\n");
5208 }
bf469271 5209 }
7d4df6a4 5210 else
18a18393 5211 {
7d4df6a4
DE
5212 warning (_("Watchpoint condition cannot be tested "
5213 "in the current scope"));
5214 /* If we failed to set the right context for this
5215 watchpoint, unconditionally report it. */
18a18393 5216 }
7d4df6a4
DE
5217 /* FIXME-someday, should give breakpoint #. */
5218 value_free_to_mark (mark);
18a18393 5219 }
7d4df6a4 5220
bf469271 5221 if (cond && !condition_result)
7d4df6a4
DE
5222 {
5223 bs->stop = 0;
5224 }
7d4df6a4
DE
5225 else if (b->ignore_count > 0)
5226 {
5227 b->ignore_count--;
5228 bs->stop = 0;
5229 /* Increase the hit count even though we don't stop. */
5230 ++(b->hit_count);
76727919 5231 gdb::observers::breakpoint_modified.notify (b);
7d4df6a4 5232 }
18a18393
VP
5233}
5234
1cf4d951
PA
5235/* Returns true if we need to track moribund locations of LOC's type
5236 on the current target. */
5237
5238static int
5239need_moribund_for_location_type (struct bp_location *loc)
5240{
5241 return ((loc->loc_type == bp_loc_software_breakpoint
5242 && !target_supports_stopped_by_sw_breakpoint ())
5243 || (loc->loc_type == bp_loc_hardware_breakpoint
5244 && !target_supports_stopped_by_hw_breakpoint ()));
5245}
5246
ddfe970e 5247/* See breakpoint.h. */
c906108c
SS
5248
5249bpstat
ddfe970e 5250build_bpstat_chain (const address_space *aspace, CORE_ADDR bp_addr,
09ac7c10 5251 const struct target_waitstatus *ws)
c906108c 5252{
ddfe970e 5253 struct breakpoint *b;
5760d0ab 5254 bpstat bs_head = NULL, *bs_link = &bs_head;
c5aa993b 5255
429374b8
JK
5256 ALL_BREAKPOINTS (b)
5257 {
1a853c52 5258 if (!breakpoint_enabled (b))
429374b8 5259 continue;
a5606eee 5260
ddfe970e 5261 for (bp_location *bl = b->loc; bl != NULL; bl = bl->next)
429374b8 5262 {
4a64f543
MS
5263 /* For hardware watchpoints, we look only at the first
5264 location. The watchpoint_check function will work on the
5265 entire expression, not the individual locations. For
5266 read watchpoints, the watchpoints_triggered function has
5267 checked all locations already. */
429374b8
JK
5268 if (b->type == bp_hardware_watchpoint && bl != b->loc)
5269 break;
18a18393 5270
f6592439 5271 if (!bl->enabled || bl->shlib_disabled)
429374b8 5272 continue;
c5aa993b 5273
09ac7c10 5274 if (!bpstat_check_location (bl, aspace, bp_addr, ws))
429374b8 5275 continue;
c5aa993b 5276
4a64f543
MS
5277 /* Come here if it's a watchpoint, or if the break address
5278 matches. */
c5aa993b 5279
ddfe970e
KS
5280 bpstat bs = new bpstats (bl, &bs_link); /* Alloc a bpstat to
5281 explain stop. */
c5aa993b 5282
f431efe5
PA
5283 /* Assume we stop. Should we find a watchpoint that is not
5284 actually triggered, or if the condition of the breakpoint
5285 evaluates as false, we'll reset 'stop' to 0. */
429374b8
JK
5286 bs->stop = 1;
5287 bs->print = 1;
d983da9c 5288
f431efe5
PA
5289 /* If this is a scope breakpoint, mark the associated
5290 watchpoint as triggered so that we will handle the
5291 out-of-scope event. We'll get to the watchpoint next
5292 iteration. */
d0fb5eae 5293 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
3a5c3e22
PA
5294 {
5295 struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
5296
5297 w->watchpoint_triggered = watch_triggered_yes;
5298 }
f431efe5
PA
5299 }
5300 }
5301
7c16b83e 5302 /* Check if a moribund breakpoint explains the stop. */
1cf4d951
PA
5303 if (!target_supports_stopped_by_sw_breakpoint ()
5304 || !target_supports_stopped_by_hw_breakpoint ())
f431efe5 5305 {
1123588c 5306 for (bp_location *loc : moribund_locations)
f431efe5 5307 {
1cf4d951
PA
5308 if (breakpoint_location_address_match (loc, aspace, bp_addr)
5309 && need_moribund_for_location_type (loc))
5310 {
ddfe970e 5311 bpstat bs = new bpstats (loc, &bs_link);
1cf4d951
PA
5312 /* For hits of moribund locations, we should just proceed. */
5313 bs->stop = 0;
5314 bs->print = 0;
5315 bs->print_it = print_it_noop;
5316 }
f431efe5
PA
5317 }
5318 }
5319
ddfe970e
KS
5320 return bs_head;
5321}
5322
5323/* See breakpoint.h. */
5324
5325bpstat
5326bpstat_stop_status (const address_space *aspace,
00431a78 5327 CORE_ADDR bp_addr, thread_info *thread,
ddfe970e
KS
5328 const struct target_waitstatus *ws,
5329 bpstat stop_chain)
5330{
5331 struct breakpoint *b = NULL;
5332 /* First item of allocated bpstat's. */
5333 bpstat bs_head = stop_chain;
5334 bpstat bs;
5335 int need_remove_insert;
5336 int removed_any;
5337
5338 /* First, build the bpstat chain with locations that explain a
5339 target stop, while being careful to not set the target running,
5340 as that may invalidate locations (in particular watchpoint
5341 locations are recreated). Resuming will happen here with
5342 breakpoint conditions or watchpoint expressions that include
5343 inferior function calls. */
5344 if (bs_head == NULL)
5345 bs_head = build_bpstat_chain (aspace, bp_addr, ws);
5346
edcc5120
TT
5347 /* A bit of special processing for shlib breakpoints. We need to
5348 process solib loading here, so that the lists of loaded and
5349 unloaded libraries are correct before we handle "catch load" and
5350 "catch unload". */
5351 for (bs = bs_head; bs != NULL; bs = bs->next)
5352 {
5d268276 5353 if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
edcc5120
TT
5354 {
5355 handle_solib_event ();
5356 break;
5357 }
5358 }
5359
f431efe5
PA
5360 /* Now go through the locations that caused the target to stop, and
5361 check whether we're interested in reporting this stop to higher
5362 layers, or whether we should resume the target transparently. */
5363
5364 removed_any = 0;
5365
5760d0ab 5366 for (bs = bs_head; bs != NULL; bs = bs->next)
f431efe5
PA
5367 {
5368 if (!bs->stop)
5369 continue;
5370
f431efe5 5371 b = bs->breakpoint_at;
348d480f
PA
5372 b->ops->check_status (bs);
5373 if (bs->stop)
28010a5d 5374 {
00431a78 5375 bpstat_check_breakpoint_conditions (bs, thread);
f431efe5 5376
429374b8
JK
5377 if (bs->stop)
5378 {
5379 ++(b->hit_count);
76727919 5380 gdb::observers::breakpoint_modified.notify (b);
c906108c 5381
4a64f543 5382 /* We will stop here. */
429374b8
JK
5383 if (b->disposition == disp_disable)
5384 {
816338b5 5385 --(b->enable_count);
1a853c52 5386 if (b->enable_count <= 0)
429374b8 5387 b->enable_state = bp_disabled;
f431efe5 5388 removed_any = 1;
429374b8
JK
5389 }
5390 if (b->silent)
5391 bs->print = 0;
5392 bs->commands = b->commands;
abf85f46 5393 if (command_line_is_silent (bs->commands
d1b0a7bf 5394 ? bs->commands.get () : NULL))
abf85f46 5395 bs->print = 0;
9d6e6e84
HZ
5396
5397 b->ops->after_condition_true (bs);
429374b8
JK
5398 }
5399
348d480f 5400 }
a9b3a50f
PA
5401
5402 /* Print nothing for this entry if we don't stop or don't
5403 print. */
5404 if (!bs->stop || !bs->print)
5405 bs->print_it = print_it_noop;
429374b8 5406 }
876fa593 5407
d983da9c
DJ
5408 /* If we aren't stopping, the value of some hardware watchpoint may
5409 not have changed, but the intermediate memory locations we are
5410 watching may have. Don't bother if we're stopping; this will get
5411 done later. */
d832cb68 5412 need_remove_insert = 0;
5760d0ab
JK
5413 if (! bpstat_causes_stop (bs_head))
5414 for (bs = bs_head; bs != NULL; bs = bs->next)
d983da9c 5415 if (!bs->stop
f431efe5
PA
5416 && bs->breakpoint_at
5417 && is_hardware_watchpoint (bs->breakpoint_at))
d983da9c 5418 {
3a5c3e22
PA
5419 struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
5420
5421 update_watchpoint (w, 0 /* don't reparse. */);
d832cb68 5422 need_remove_insert = 1;
d983da9c
DJ
5423 }
5424
d832cb68 5425 if (need_remove_insert)
44702360 5426 update_global_location_list (UGLL_MAY_INSERT);
f431efe5 5427 else if (removed_any)
44702360 5428 update_global_location_list (UGLL_DONT_INSERT);
d832cb68 5429
5760d0ab 5430 return bs_head;
c906108c 5431}
628fe4e4
JK
5432
5433static void
5434handle_jit_event (void)
5435{
5436 struct frame_info *frame;
5437 struct gdbarch *gdbarch;
5438
1eb8556f 5439 infrun_debug_printf ("handling bp_jit_event");
243a9253 5440
628fe4e4
JK
5441 /* Switch terminal for any messages produced by
5442 breakpoint_re_set. */
223ffa71 5443 target_terminal::ours_for_output ();
628fe4e4
JK
5444
5445 frame = get_current_frame ();
5446 gdbarch = get_frame_arch (frame);
fe053b9e 5447 objfile *jiter = symbol_objfile (get_frame_function (frame));
628fe4e4 5448
fe053b9e 5449 jit_event_handler (gdbarch, jiter);
628fe4e4 5450
223ffa71 5451 target_terminal::inferior ();
628fe4e4
JK
5452}
5453
5454/* Prepare WHAT final decision for infrun. */
5455
5456/* Decide what infrun needs to do with this bpstat. */
5457
c906108c 5458struct bpstat_what
0e30163f 5459bpstat_what (bpstat bs_head)
c906108c 5460{
c906108c 5461 struct bpstat_what retval;
0e30163f 5462 bpstat bs;
c906108c 5463
628fe4e4 5464 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
aa7d318d 5465 retval.call_dummy = STOP_NONE;
e2d0f980 5466 retval.is_longjmp = false;
628fe4e4 5467
0e30163f 5468 for (bs = bs_head; bs != NULL; bs = bs->next)
c906108c 5469 {
628fe4e4
JK
5470 /* Extract this BS's action. After processing each BS, we check
5471 if its action overrides all we've seem so far. */
5472 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
5473 enum bptype bptype;
5474
c906108c 5475 if (bs->breakpoint_at == NULL)
628fe4e4
JK
5476 {
5477 /* I suspect this can happen if it was a momentary
5478 breakpoint which has since been deleted. */
5479 bptype = bp_none;
5480 }
20874c92 5481 else
f431efe5 5482 bptype = bs->breakpoint_at->type;
628fe4e4
JK
5483
5484 switch (bptype)
c906108c
SS
5485 {
5486 case bp_none:
628fe4e4 5487 break;
c906108c
SS
5488 case bp_breakpoint:
5489 case bp_hardware_breakpoint:
7c16b83e 5490 case bp_single_step:
c906108c
SS
5491 case bp_until:
5492 case bp_finish:
a9b3a50f 5493 case bp_shlib_event:
c906108c
SS
5494 if (bs->stop)
5495 {
5496 if (bs->print)
628fe4e4 5497 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 5498 else
628fe4e4 5499 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
5500 }
5501 else
628fe4e4 5502 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5503 break;
5504 case bp_watchpoint:
5505 case bp_hardware_watchpoint:
5506 case bp_read_watchpoint:
5507 case bp_access_watchpoint:
5508 if (bs->stop)
5509 {
5510 if (bs->print)
628fe4e4 5511 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 5512 else
628fe4e4 5513 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
5514 }
5515 else
628fe4e4
JK
5516 {
5517 /* There was a watchpoint, but we're not stopping.
5518 This requires no further action. */
5519 }
c906108c
SS
5520 break;
5521 case bp_longjmp:
e2e4d78b 5522 case bp_longjmp_call_dummy:
186c406b 5523 case bp_exception:
0a39bb32
PA
5524 if (bs->stop)
5525 {
5526 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
5527 retval.is_longjmp = bptype != bp_exception;
5528 }
5529 else
5530 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5531 break;
5532 case bp_longjmp_resume:
186c406b 5533 case bp_exception_resume:
0a39bb32
PA
5534 if (bs->stop)
5535 {
5536 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
5537 retval.is_longjmp = bptype == bp_longjmp_resume;
5538 }
5539 else
5540 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5541 break;
5542 case bp_step_resume:
5543 if (bs->stop)
628fe4e4
JK
5544 this_action = BPSTAT_WHAT_STEP_RESUME;
5545 else
c906108c 5546 {
628fe4e4
JK
5547 /* It is for the wrong frame. */
5548 this_action = BPSTAT_WHAT_SINGLE;
c906108c 5549 }
c906108c 5550 break;
2c03e5be
PA
5551 case bp_hp_step_resume:
5552 if (bs->stop)
5553 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
5554 else
5555 {
5556 /* It is for the wrong frame. */
5557 this_action = BPSTAT_WHAT_SINGLE;
5558 }
5559 break;
c906108c 5560 case bp_watchpoint_scope:
c4093a6a 5561 case bp_thread_event:
1900040c 5562 case bp_overlay_event:
0fd8e87f 5563 case bp_longjmp_master:
aa7d318d 5564 case bp_std_terminate_master:
186c406b 5565 case bp_exception_master:
628fe4e4 5566 this_action = BPSTAT_WHAT_SINGLE;
c4093a6a 5567 break;
ce78b96d 5568 case bp_catchpoint:
c5aa993b
JM
5569 if (bs->stop)
5570 {
5571 if (bs->print)
628fe4e4 5572 this_action = BPSTAT_WHAT_STOP_NOISY;
c5aa993b 5573 else
628fe4e4 5574 this_action = BPSTAT_WHAT_STOP_SILENT;
c5aa993b
JM
5575 }
5576 else
628fe4e4 5577 {
cb1e4e32
PA
5578 /* Some catchpoints are implemented with breakpoints.
5579 For those, we need to step over the breakpoint. */
5580 if (bs->bp_location_at->loc_type != bp_loc_other)
5581 this_action = BPSTAT_WHAT_SINGLE;
628fe4e4
JK
5582 }
5583 break;
628fe4e4 5584 case bp_jit_event:
628fe4e4 5585 this_action = BPSTAT_WHAT_SINGLE;
c5aa993b 5586 break;
c906108c 5587 case bp_call_dummy:
53a5351d
JM
5588 /* Make sure the action is stop (silent or noisy),
5589 so infrun.c pops the dummy frame. */
aa7d318d 5590 retval.call_dummy = STOP_STACK_DUMMY;
628fe4e4 5591 this_action = BPSTAT_WHAT_STOP_SILENT;
aa7d318d
TT
5592 break;
5593 case bp_std_terminate:
5594 /* Make sure the action is stop (silent or noisy),
5595 so infrun.c pops the dummy frame. */
aa7d318d 5596 retval.call_dummy = STOP_STD_TERMINATE;
628fe4e4 5597 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c 5598 break;
1042e4c0 5599 case bp_tracepoint:
7a697b8d 5600 case bp_fast_tracepoint:
0fb4aa4b 5601 case bp_static_tracepoint:
1042e4c0
SS
5602 /* Tracepoint hits should not be reported back to GDB, and
5603 if one got through somehow, it should have been filtered
5604 out already. */
5605 internal_error (__FILE__, __LINE__,
7a697b8d 5606 _("bpstat_what: tracepoint encountered"));
0e30163f
JK
5607 break;
5608 case bp_gnu_ifunc_resolver:
5609 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
5610 this_action = BPSTAT_WHAT_SINGLE;
5611 break;
5612 case bp_gnu_ifunc_resolver_return:
5613 /* The breakpoint will be removed, execution will restart from the
5614 PC of the former breakpoint. */
5615 this_action = BPSTAT_WHAT_KEEP_CHECKING;
5616 break;
e7e0cddf
SS
5617
5618 case bp_dprintf:
a11cfd87
HZ
5619 if (bs->stop)
5620 this_action = BPSTAT_WHAT_STOP_SILENT;
5621 else
5622 this_action = BPSTAT_WHAT_SINGLE;
e7e0cddf
SS
5623 break;
5624
628fe4e4
JK
5625 default:
5626 internal_error (__FILE__, __LINE__,
5627 _("bpstat_what: unhandled bptype %d"), (int) bptype);
c906108c 5628 }
628fe4e4 5629
325fac50 5630 retval.main_action = std::max (retval.main_action, this_action);
c906108c 5631 }
628fe4e4 5632
243a9253
PA
5633 return retval;
5634}
628fe4e4 5635
243a9253
PA
5636void
5637bpstat_run_callbacks (bpstat bs_head)
5638{
5639 bpstat bs;
628fe4e4 5640
0e30163f
JK
5641 for (bs = bs_head; bs != NULL; bs = bs->next)
5642 {
5643 struct breakpoint *b = bs->breakpoint_at;
5644
5645 if (b == NULL)
5646 continue;
5647 switch (b->type)
5648 {
243a9253
PA
5649 case bp_jit_event:
5650 handle_jit_event ();
5651 break;
0e30163f
JK
5652 case bp_gnu_ifunc_resolver:
5653 gnu_ifunc_resolver_stop (b);
5654 break;
5655 case bp_gnu_ifunc_resolver_return:
5656 gnu_ifunc_resolver_return_stop (b);
5657 break;
5658 }
5659 }
c906108c
SS
5660}
5661
4c462cb0 5662/* See breakpoint.h. */
c906108c 5663
4c462cb0
SM
5664bool
5665bpstat_should_step ()
c906108c
SS
5666{
5667 struct breakpoint *b;
cc59ec59 5668
c906108c 5669 ALL_BREAKPOINTS (b)
717a8278 5670 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
4c462cb0
SM
5671 return true;
5672 return false;
c906108c
SS
5673}
5674
4c462cb0
SM
5675/* See breakpoint.h. */
5676
5677bool
67822962
PA
5678bpstat_causes_stop (bpstat bs)
5679{
5680 for (; bs != NULL; bs = bs->next)
5681 if (bs->stop)
4c462cb0 5682 return true;
67822962 5683
4c462cb0 5684 return false;
67822962
PA
5685}
5686
c906108c 5687\f
c5aa993b 5688
170b53b2
UW
5689/* Compute a string of spaces suitable to indent the next line
5690 so it starts at the position corresponding to the table column
5691 named COL_NAME in the currently active table of UIOUT. */
5692
5693static char *
5694wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
5695{
5696 static char wrap_indent[80];
5697 int i, total_width, width, align;
c5209615 5698 const char *text;
170b53b2
UW
5699
5700 total_width = 0;
112e8700 5701 for (i = 1; uiout->query_table_field (i, &width, &align, &text); i++)
170b53b2
UW
5702 {
5703 if (strcmp (text, col_name) == 0)
5704 {
5705 gdb_assert (total_width < sizeof wrap_indent);
5706 memset (wrap_indent, ' ', total_width);
5707 wrap_indent[total_width] = 0;
5708
5709 return wrap_indent;
5710 }
5711
5712 total_width += width + 1;
5713 }
5714
5715 return NULL;
5716}
5717
b775012e
LM
5718/* Determine if the locations of this breakpoint will have their conditions
5719 evaluated by the target, host or a mix of both. Returns the following:
5720
5721 "host": Host evals condition.
5722 "host or target": Host or Target evals condition.
5723 "target": Target evals condition.
5724*/
5725
5726static const char *
5727bp_condition_evaluator (struct breakpoint *b)
5728{
5729 struct bp_location *bl;
5730 char host_evals = 0;
5731 char target_evals = 0;
5732
5733 if (!b)
5734 return NULL;
5735
5736 if (!is_breakpoint (b))
5737 return NULL;
5738
5739 if (gdb_evaluates_breakpoint_condition_p ()
5740 || !target_supports_evaluation_of_breakpoint_conditions ())
5741 return condition_evaluation_host;
5742
5743 for (bl = b->loc; bl; bl = bl->next)
5744 {
5745 if (bl->cond_bytecode)
5746 target_evals++;
5747 else
5748 host_evals++;
5749 }
5750
5751 if (host_evals && target_evals)
5752 return condition_evaluation_both;
5753 else if (target_evals)
5754 return condition_evaluation_target;
5755 else
5756 return condition_evaluation_host;
5757}
5758
5759/* Determine the breakpoint location's condition evaluator. This is
5760 similar to bp_condition_evaluator, but for locations. */
5761
5762static const char *
5763bp_location_condition_evaluator (struct bp_location *bl)
5764{
5765 if (bl && !is_breakpoint (bl->owner))
5766 return NULL;
5767
5768 if (gdb_evaluates_breakpoint_condition_p ()
5769 || !target_supports_evaluation_of_breakpoint_conditions ())
5770 return condition_evaluation_host;
5771
5772 if (bl && bl->cond_bytecode)
5773 return condition_evaluation_target;
5774 else
5775 return condition_evaluation_host;
5776}
5777
859825b8
JK
5778/* Print the LOC location out of the list of B->LOC locations. */
5779
170b53b2
UW
5780static void
5781print_breakpoint_location (struct breakpoint *b,
5782 struct bp_location *loc)
0d381245 5783{
79a45e25 5784 struct ui_out *uiout = current_uiout;
5ed8105e
PA
5785
5786 scoped_restore_current_program_space restore_pspace;
6c95b8df 5787
859825b8
JK
5788 if (loc != NULL && loc->shlib_disabled)
5789 loc = NULL;
5790
6c95b8df
PA
5791 if (loc != NULL)
5792 set_current_program_space (loc->pspace);
5793
56435ebe 5794 if (b->display_canonical)
d28cd78a 5795 uiout->field_string ("what", event_location_to_string (b->location.get ()));
2f202fde 5796 else if (loc && loc->symtab)
0d381245 5797 {
4a27f119
KS
5798 const struct symbol *sym = loc->symbol;
5799
0d381245
VP
5800 if (sym)
5801 {
112e8700 5802 uiout->text ("in ");
987012b8 5803 uiout->field_string ("func", sym->print_name (),
e43b10e1 5804 function_name_style.style ());
112e8700
SM
5805 uiout->text (" ");
5806 uiout->wrap_hint (wrap_indent_at_field (uiout, "what"));
5807 uiout->text ("at ");
0d381245 5808 }
112e8700 5809 uiout->field_string ("file",
cbe56571 5810 symtab_to_filename_for_display (loc->symtab),
e43b10e1 5811 file_name_style.style ());
112e8700 5812 uiout->text (":");
05cba821 5813
112e8700
SM
5814 if (uiout->is_mi_like_p ())
5815 uiout->field_string ("fullname", symtab_to_fullname (loc->symtab));
0d381245 5816
381befee 5817 uiout->field_signed ("line", loc->line_number);
0d381245 5818 }
859825b8 5819 else if (loc)
0d381245 5820 {
d7e74731 5821 string_file stb;
170b53b2 5822
d7e74731 5823 print_address_symbolic (loc->gdbarch, loc->address, &stb,
22e722e1 5824 demangle, "");
112e8700 5825 uiout->field_stream ("at", stb);
0d381245 5826 }
859825b8 5827 else
f00aae0f 5828 {
d28cd78a
TT
5829 uiout->field_string ("pending",
5830 event_location_to_string (b->location.get ()));
f00aae0f
KS
5831 /* If extra_string is available, it could be holding a condition
5832 or dprintf arguments. In either case, make sure it is printed,
5833 too, but only for non-MI streams. */
112e8700 5834 if (!uiout->is_mi_like_p () && b->extra_string != NULL)
f00aae0f
KS
5835 {
5836 if (b->type == bp_dprintf)
112e8700 5837 uiout->text (",");
f00aae0f 5838 else
112e8700
SM
5839 uiout->text (" ");
5840 uiout->text (b->extra_string);
f00aae0f
KS
5841 }
5842 }
6c95b8df 5843
b775012e
LM
5844 if (loc && is_breakpoint (b)
5845 && breakpoint_condition_evaluation_mode () == condition_evaluation_target
5846 && bp_condition_evaluator (b) == condition_evaluation_both)
5847 {
112e8700
SM
5848 uiout->text (" (");
5849 uiout->field_string ("evaluated-by",
b775012e 5850 bp_location_condition_evaluator (loc));
112e8700 5851 uiout->text (")");
b775012e 5852 }
0d381245
VP
5853}
5854
269b11a2
PA
5855static const char *
5856bptype_string (enum bptype type)
c906108c 5857{
c4093a6a
JM
5858 struct ep_type_description
5859 {
5860 enum bptype type;
a121b7c1 5861 const char *description;
c4093a6a
JM
5862 };
5863 static struct ep_type_description bptypes[] =
c906108c 5864 {
c5aa993b
JM
5865 {bp_none, "?deleted?"},
5866 {bp_breakpoint, "breakpoint"},
c906108c 5867 {bp_hardware_breakpoint, "hw breakpoint"},
7c16b83e 5868 {bp_single_step, "sw single-step"},
c5aa993b
JM
5869 {bp_until, "until"},
5870 {bp_finish, "finish"},
5871 {bp_watchpoint, "watchpoint"},
c906108c 5872 {bp_hardware_watchpoint, "hw watchpoint"},
c5aa993b
JM
5873 {bp_read_watchpoint, "read watchpoint"},
5874 {bp_access_watchpoint, "acc watchpoint"},
5875 {bp_longjmp, "longjmp"},
5876 {bp_longjmp_resume, "longjmp resume"},
e2e4d78b 5877 {bp_longjmp_call_dummy, "longjmp for call dummy"},
186c406b
TT
5878 {bp_exception, "exception"},
5879 {bp_exception_resume, "exception resume"},
c5aa993b 5880 {bp_step_resume, "step resume"},
2c03e5be 5881 {bp_hp_step_resume, "high-priority step resume"},
c5aa993b
JM
5882 {bp_watchpoint_scope, "watchpoint scope"},
5883 {bp_call_dummy, "call dummy"},
aa7d318d 5884 {bp_std_terminate, "std::terminate"},
c5aa993b 5885 {bp_shlib_event, "shlib events"},
c4093a6a 5886 {bp_thread_event, "thread events"},
1900040c 5887 {bp_overlay_event, "overlay events"},
0fd8e87f 5888 {bp_longjmp_master, "longjmp master"},
aa7d318d 5889 {bp_std_terminate_master, "std::terminate master"},
186c406b 5890 {bp_exception_master, "exception master"},
ce78b96d 5891 {bp_catchpoint, "catchpoint"},
1042e4c0 5892 {bp_tracepoint, "tracepoint"},
7a697b8d 5893 {bp_fast_tracepoint, "fast tracepoint"},
0fb4aa4b 5894 {bp_static_tracepoint, "static tracepoint"},
e7e0cddf 5895 {bp_dprintf, "dprintf"},
4efc6507 5896 {bp_jit_event, "jit events"},
0e30163f
JK
5897 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
5898 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
c5aa993b 5899 };
269b11a2
PA
5900
5901 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
5902 || ((int) type != bptypes[(int) type].type))
5903 internal_error (__FILE__, __LINE__,
5904 _("bptypes table does not describe type #%d."),
5905 (int) type);
5906
5907 return bptypes[(int) type].description;
5908}
5909
998580f1
MK
5910/* For MI, output a field named 'thread-groups' with a list as the value.
5911 For CLI, prefix the list with the string 'inf'. */
5912
5913static void
5914output_thread_groups (struct ui_out *uiout,
5915 const char *field_name,
5c632425 5916 const std::vector<int> &inf_nums,
998580f1
MK
5917 int mi_only)
5918{
112e8700 5919 int is_mi = uiout->is_mi_like_p ();
998580f1
MK
5920
5921 /* For backward compatibility, don't display inferiors in CLI unless
5922 there are several. Always display them for MI. */
5923 if (!is_mi && mi_only)
5924 return;
5925
10f489e5 5926 ui_out_emit_list list_emitter (uiout, field_name);
752eb8b4 5927
5c632425 5928 for (size_t i = 0; i < inf_nums.size (); i++)
998580f1
MK
5929 {
5930 if (is_mi)
5931 {
5932 char mi_group[10];
5933
5c632425 5934 xsnprintf (mi_group, sizeof (mi_group), "i%d", inf_nums[i]);
112e8700 5935 uiout->field_string (NULL, mi_group);
998580f1
MK
5936 }
5937 else
5938 {
5939 if (i == 0)
112e8700 5940 uiout->text (" inf ");
998580f1 5941 else
112e8700 5942 uiout->text (", ");
998580f1 5943
5c632425 5944 uiout->text (plongest (inf_nums[i]));
998580f1
MK
5945 }
5946 }
998580f1
MK
5947}
5948
a38118e5
PA
5949/* Print B to gdb_stdout. If RAW_LOC, print raw breakpoint locations
5950 instead of going via breakpoint_ops::print_one. This makes "maint
5951 info breakpoints" show the software breakpoint locations of
5952 catchpoints, which are considered internal implementation
5953 detail. */
269b11a2
PA
5954
5955static void
5956print_one_breakpoint_location (struct breakpoint *b,
5957 struct bp_location *loc,
5958 int loc_number,
5959 struct bp_location **last_loc,
a38118e5 5960 int allflag, bool raw_loc)
269b11a2
PA
5961{
5962 struct command_line *l;
c2c6d25f 5963 static char bpenables[] = "nynny";
c906108c 5964
79a45e25 5965 struct ui_out *uiout = current_uiout;
0d381245
VP
5966 int header_of_multiple = 0;
5967 int part_of_multiple = (loc != NULL);
79a45b7d
TT
5968 struct value_print_options opts;
5969
5970 get_user_print_options (&opts);
0d381245
VP
5971
5972 gdb_assert (!loc || loc_number != 0);
4a64f543
MS
5973 /* See comment in print_one_breakpoint concerning treatment of
5974 breakpoints with single disabled location. */
0d381245
VP
5975 if (loc == NULL
5976 && (b->loc != NULL
5977 && (b->loc->next != NULL || !b->loc->enabled)))
5978 header_of_multiple = 1;
5979 if (loc == NULL)
5980 loc = b->loc;
5981
c4093a6a
JM
5982 annotate_record ();
5983
5984 /* 1 */
5985 annotate_field (0);
0d381245 5986 if (part_of_multiple)
528e1572 5987 uiout->field_fmt ("number", "%d.%d", b->number, loc_number);
0d381245 5988 else
381befee 5989 uiout->field_signed ("number", b->number);
c4093a6a
JM
5990
5991 /* 2 */
5992 annotate_field (1);
0d381245 5993 if (part_of_multiple)
112e8700 5994 uiout->field_skip ("type");
269b11a2 5995 else
112e8700 5996 uiout->field_string ("type", bptype_string (b->type));
c4093a6a
JM
5997
5998 /* 3 */
5999 annotate_field (2);
0d381245 6000 if (part_of_multiple)
112e8700 6001 uiout->field_skip ("disp");
0d381245 6002 else
112e8700 6003 uiout->field_string ("disp", bpdisp_text (b->disposition));
0d381245 6004
c4093a6a
JM
6005 /* 4 */
6006 annotate_field (3);
0d381245 6007 if (part_of_multiple)
112e8700 6008 uiout->field_string ("enabled", loc->enabled ? "y" : "n");
0d381245 6009 else
112e8700 6010 uiout->field_fmt ("enabled", "%c", bpenables[(int) b->enable_state]);
0d381245 6011
c4093a6a 6012 /* 5 and 6 */
a38118e5 6013 if (!raw_loc && b->ops != NULL && b->ops->print_one != NULL)
b58a68fe 6014 b->ops->print_one (b, last_loc);
3086aeae 6015 else
a38118e5
PA
6016 {
6017 if (is_watchpoint (b))
3a5c3e22
PA
6018 {
6019 struct watchpoint *w = (struct watchpoint *) b;
6020
6021 /* Field 4, the address, is omitted (which makes the columns
6022 not line up too nicely with the headers, but the effect
6023 is relatively readable). */
6024 if (opts.addressprint)
112e8700 6025 uiout->field_skip ("addr");
3a5c3e22 6026 annotate_field (5);
112e8700 6027 uiout->field_string ("what", w->exp_string);
3a5c3e22 6028 }
f06f1252
TT
6029 else if (!is_catchpoint (b) || is_exception_catchpoint (b)
6030 || is_ada_exception_catchpoint (b))
a38118e5
PA
6031 {
6032 if (opts.addressprint)
6033 {
6034 annotate_field (4);
6035 if (header_of_multiple)
7f6aba03
TT
6036 uiout->field_string ("addr", "<MULTIPLE>",
6037 metadata_style.style ());
a38118e5 6038 else if (b->loc == NULL || loc->shlib_disabled)
7f6aba03
TT
6039 uiout->field_string ("addr", "<PENDING>",
6040 metadata_style.style ());
a38118e5
PA
6041 else
6042 uiout->field_core_addr ("addr",
6043 loc->gdbarch, loc->address);
6044 }
6045 annotate_field (5);
6046 if (!header_of_multiple)
6047 print_breakpoint_location (b, loc);
6048 if (b->loc)
6049 *last_loc = b->loc;
6050 }
6051 }
6c95b8df 6052
998580f1 6053 if (loc != NULL && !header_of_multiple)
6c95b8df 6054 {
5c632425 6055 std::vector<int> inf_nums;
998580f1 6056 int mi_only = 1;
6c95b8df 6057
08036331 6058 for (inferior *inf : all_inferiors ())
6c95b8df
PA
6059 {
6060 if (inf->pspace == loc->pspace)
5c632425 6061 inf_nums.push_back (inf->num);
6c95b8df 6062 }
998580f1
MK
6063
6064 /* For backward compatibility, don't display inferiors in CLI unless
6065 there are several. Always display for MI. */
6066 if (allflag
6067 || (!gdbarch_has_global_breakpoints (target_gdbarch ())
94c93c35 6068 && (program_spaces.size () > 1
998580f1
MK
6069 || number_of_inferiors () > 1)
6070 /* LOC is for existing B, it cannot be in
6071 moribund_locations and thus having NULL OWNER. */
6072 && loc->owner->type != bp_catchpoint))
6073 mi_only = 0;
5c632425 6074 output_thread_groups (uiout, "thread-groups", inf_nums, mi_only);
6c95b8df
PA
6075 }
6076
4a306c9a 6077 if (!part_of_multiple)
c4093a6a 6078 {
4a306c9a
JB
6079 if (b->thread != -1)
6080 {
6081 /* FIXME: This seems to be redundant and lost here; see the
4a64f543 6082 "stop only in" line a little further down. */
112e8700 6083 uiout->text (" thread ");
381befee 6084 uiout->field_signed ("thread", b->thread);
4a306c9a
JB
6085 }
6086 else if (b->task != 0)
6087 {
112e8700 6088 uiout->text (" task ");
381befee 6089 uiout->field_signed ("task", b->task);
4a306c9a 6090 }
c4093a6a 6091 }
f1310107 6092
112e8700 6093 uiout->text ("\n");
f1310107 6094
348d480f 6095 if (!part_of_multiple)
f1310107
TJB
6096 b->ops->print_one_detail (b, uiout);
6097
0d381245 6098 if (part_of_multiple && frame_id_p (b->frame_id))
c4093a6a
JM
6099 {
6100 annotate_field (6);
112e8700 6101 uiout->text ("\tstop only in stack frame at ");
e5dd4106 6102 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
818dd999 6103 the frame ID. */
112e8700 6104 uiout->field_core_addr ("frame",
5af949e3 6105 b->gdbarch, b->frame_id.stack_addr);
112e8700 6106 uiout->text ("\n");
c4093a6a
JM
6107 }
6108
28010a5d 6109 if (!part_of_multiple && b->cond_string)
c4093a6a
JM
6110 {
6111 annotate_field (7);
d77f58be 6112 if (is_tracepoint (b))
112e8700 6113 uiout->text ("\ttrace only if ");
1042e4c0 6114 else
112e8700
SM
6115 uiout->text ("\tstop only if ");
6116 uiout->field_string ("cond", b->cond_string);
b775012e
LM
6117
6118 /* Print whether the target is doing the breakpoint's condition
6119 evaluation. If GDB is doing the evaluation, don't print anything. */
6120 if (is_breakpoint (b)
6121 && breakpoint_condition_evaluation_mode ()
6122 == condition_evaluation_target)
6123 {
6a831f06
PA
6124 uiout->message (" (%pF evals)",
6125 string_field ("evaluated-by",
6126 bp_condition_evaluator (b)));
b775012e 6127 }
112e8700 6128 uiout->text ("\n");
0101ce28
JJ
6129 }
6130
0d381245 6131 if (!part_of_multiple && b->thread != -1)
c4093a6a 6132 {
4a64f543 6133 /* FIXME should make an annotation for this. */
112e8700
SM
6134 uiout->text ("\tstop only in thread ");
6135 if (uiout->is_mi_like_p ())
381befee 6136 uiout->field_signed ("thread", b->thread);
5d5658a1
PA
6137 else
6138 {
6139 struct thread_info *thr = find_thread_global_id (b->thread);
6140
112e8700 6141 uiout->field_string ("thread", print_thread_id (thr));
5d5658a1 6142 }
112e8700 6143 uiout->text ("\n");
c4093a6a
JM
6144 }
6145
556ec64d
YQ
6146 if (!part_of_multiple)
6147 {
6148 if (b->hit_count)
31f56a27
YQ
6149 {
6150 /* FIXME should make an annotation for this. */
6151 if (is_catchpoint (b))
112e8700 6152 uiout->text ("\tcatchpoint");
31f56a27 6153 else if (is_tracepoint (b))
112e8700 6154 uiout->text ("\ttracepoint");
31f56a27 6155 else
112e8700
SM
6156 uiout->text ("\tbreakpoint");
6157 uiout->text (" already hit ");
381befee 6158 uiout->field_signed ("times", b->hit_count);
31f56a27 6159 if (b->hit_count == 1)
112e8700 6160 uiout->text (" time\n");
31f56a27 6161 else
112e8700 6162 uiout->text (" times\n");
31f56a27 6163 }
556ec64d
YQ
6164 else
6165 {
31f56a27 6166 /* Output the count also if it is zero, but only if this is mi. */
112e8700 6167 if (uiout->is_mi_like_p ())
381befee 6168 uiout->field_signed ("times", b->hit_count);
556ec64d
YQ
6169 }
6170 }
8b93c638 6171
0d381245 6172 if (!part_of_multiple && b->ignore_count)
c4093a6a
JM
6173 {
6174 annotate_field (8);
6a831f06
PA
6175 uiout->message ("\tignore next %pF hits\n",
6176 signed_field ("ignore", b->ignore_count));
c4093a6a 6177 }
059fb39f 6178
816338b5
SS
6179 /* Note that an enable count of 1 corresponds to "enable once"
6180 behavior, which is reported by the combination of enablement and
6181 disposition, so we don't need to mention it here. */
6182 if (!part_of_multiple && b->enable_count > 1)
6183 {
6184 annotate_field (8);
112e8700 6185 uiout->text ("\tdisable after ");
816338b5
SS
6186 /* Tweak the wording to clarify that ignore and enable counts
6187 are distinct, and have additive effect. */
6188 if (b->ignore_count)
112e8700 6189 uiout->text ("additional ");
816338b5 6190 else
112e8700 6191 uiout->text ("next ");
381befee 6192 uiout->field_signed ("enable", b->enable_count);
112e8700 6193 uiout->text (" hits\n");
816338b5
SS
6194 }
6195
f196051f
SS
6196 if (!part_of_multiple && is_tracepoint (b))
6197 {
6198 struct tracepoint *tp = (struct tracepoint *) b;
6199
6200 if (tp->traceframe_usage)
6201 {
112e8700 6202 uiout->text ("\ttrace buffer usage ");
381befee 6203 uiout->field_signed ("traceframe-usage", tp->traceframe_usage);
112e8700 6204 uiout->text (" bytes\n");
f196051f
SS
6205 }
6206 }
d3ce09f5 6207
d1b0a7bf 6208 l = b->commands ? b->commands.get () : NULL;
059fb39f 6209 if (!part_of_multiple && l)
c4093a6a
JM
6210 {
6211 annotate_field (9);
2e783024 6212 ui_out_emit_tuple tuple_emitter (uiout, "script");
8b93c638 6213 print_command_lines (uiout, l, 4);
c4093a6a 6214 }
d24317b4 6215
d9b3f62e 6216 if (is_tracepoint (b))
1042e4c0 6217 {
d9b3f62e
PA
6218 struct tracepoint *t = (struct tracepoint *) b;
6219
6220 if (!part_of_multiple && t->pass_count)
6221 {
6222 annotate_field (10);
112e8700 6223 uiout->text ("\tpass count ");
381befee 6224 uiout->field_signed ("pass", t->pass_count);
112e8700 6225 uiout->text (" \n");
d9b3f62e 6226 }
f2a8bc8a
YQ
6227
6228 /* Don't display it when tracepoint or tracepoint location is
6229 pending. */
6230 if (!header_of_multiple && loc != NULL && !loc->shlib_disabled)
6231 {
6232 annotate_field (11);
6233
112e8700
SM
6234 if (uiout->is_mi_like_p ())
6235 uiout->field_string ("installed",
f2a8bc8a
YQ
6236 loc->inserted ? "y" : "n");
6237 else
6238 {
6239 if (loc->inserted)
112e8700 6240 uiout->text ("\t");
f2a8bc8a 6241 else
112e8700
SM
6242 uiout->text ("\tnot ");
6243 uiout->text ("installed on target\n");
f2a8bc8a
YQ
6244 }
6245 }
1042e4c0
SS
6246 }
6247
112e8700 6248 if (uiout->is_mi_like_p () && !part_of_multiple)
d24317b4 6249 {
3a5c3e22
PA
6250 if (is_watchpoint (b))
6251 {
6252 struct watchpoint *w = (struct watchpoint *) b;
6253
112e8700 6254 uiout->field_string ("original-location", w->exp_string);
3a5c3e22 6255 }
f00aae0f 6256 else if (b->location != NULL
d28cd78a 6257 && event_location_to_string (b->location.get ()) != NULL)
112e8700 6258 uiout->field_string ("original-location",
d28cd78a 6259 event_location_to_string (b->location.get ()));
d24317b4 6260 }
c4093a6a 6261}
c5aa993b 6262
13674803
SM
6263/* See breakpoint.h. */
6264
6265bool fix_multi_location_breakpoint_output_globally = false;
6266
0d381245
VP
6267static void
6268print_one_breakpoint (struct breakpoint *b,
4a64f543 6269 struct bp_location **last_loc,
6c95b8df 6270 int allflag)
0d381245 6271{
79a45e25 6272 struct ui_out *uiout = current_uiout;
13674803
SM
6273 bool use_fixed_output
6274 = (uiout->test_flags (fix_multi_location_breakpoint_output)
6275 || fix_multi_location_breakpoint_output_globally);
8d3788bd 6276
b4be1b06 6277 gdb::optional<ui_out_emit_tuple> bkpt_tuple_emitter (gdb::in_place, uiout, "bkpt");
a38118e5 6278 print_one_breakpoint_location (b, NULL, 0, last_loc, allflag, false);
8d3788bd 6279
b4be1b06
SM
6280 /* The mi2 broken format: the main breakpoint tuple ends here, the locations
6281 are outside. */
6282 if (!use_fixed_output)
6283 bkpt_tuple_emitter.reset ();
0d381245
VP
6284
6285 /* If this breakpoint has custom print function,
6286 it's already printed. Otherwise, print individual
6287 locations, if any. */
a38118e5
PA
6288 if (b->ops == NULL
6289 || b->ops->print_one == NULL
6290 || allflag)
0d381245 6291 {
4a64f543
MS
6292 /* If breakpoint has a single location that is disabled, we
6293 print it as if it had several locations, since otherwise it's
6294 hard to represent "breakpoint enabled, location disabled"
6295 situation.
6296
6297 Note that while hardware watchpoints have several locations
a38118e5
PA
6298 internally, that's not a property exposed to users.
6299
6300 Likewise, while catchpoints may be implemented with
6301 breakpoints (e.g., catch throw), that's not a property
6302 exposed to users. We do however display the internal
6303 breakpoint locations with "maint info breakpoints". */
6304 if (!is_hardware_watchpoint (b)
f06f1252
TT
6305 && (!is_catchpoint (b) || is_exception_catchpoint (b)
6306 || is_ada_exception_catchpoint (b))
a38118e5
PA
6307 && (allflag
6308 || (b->loc && (b->loc->next || !b->loc->enabled))))
0d381245 6309 {
b4be1b06
SM
6310 gdb::optional<ui_out_emit_list> locations_list;
6311
6312 /* For MI version <= 2, keep the behavior where GDB outputs an invalid
6313 MI record. For later versions, place breakpoint locations in a
6314 list. */
6315 if (uiout->is_mi_like_p () && use_fixed_output)
6316 locations_list.emplace (uiout, "locations");
8d3788bd 6317
b4be1b06
SM
6318 int n = 1;
6319 for (bp_location *loc = b->loc; loc != NULL; loc = loc->next, ++n)
8d3788bd 6320 {
b4be1b06 6321 ui_out_emit_tuple loc_tuple_emitter (uiout, NULL);
a38118e5
PA
6322 print_one_breakpoint_location (b, loc, n, last_loc,
6323 allflag, allflag);
8d3788bd 6324 }
0d381245
VP
6325 }
6326 }
6327}
6328
a6d9a66e
UW
6329static int
6330breakpoint_address_bits (struct breakpoint *b)
6331{
6332 int print_address_bits = 0;
6333 struct bp_location *loc;
6334
c6d81124
PA
6335 /* Software watchpoints that aren't watching memory don't have an
6336 address to print. */
6337 if (is_no_memory_software_watchpoint (b))
6338 return 0;
6339
a6d9a66e
UW
6340 for (loc = b->loc; loc; loc = loc->next)
6341 {
c7437ca6
PA
6342 int addr_bit;
6343
c7437ca6 6344 addr_bit = gdbarch_addr_bit (loc->gdbarch);
a6d9a66e
UW
6345 if (addr_bit > print_address_bits)
6346 print_address_bits = addr_bit;
6347 }
6348
6349 return print_address_bits;
6350}
0d381245 6351
65630365 6352/* See breakpoint.h. */
c5aa993b 6353
65630365
PA
6354void
6355print_breakpoint (breakpoint *b)
c4093a6a 6356{
a6d9a66e 6357 struct bp_location *dummy_loc = NULL;
65630365 6358 print_one_breakpoint (b, &dummy_loc, 0);
c4093a6a 6359}
c5aa993b 6360
09d682a4
TT
6361/* Return true if this breakpoint was set by the user, false if it is
6362 internal or momentary. */
6363
6364int
6365user_breakpoint_p (struct breakpoint *b)
6366{
46c6471b 6367 return b->number > 0;
09d682a4
TT
6368}
6369
93daf339
TT
6370/* See breakpoint.h. */
6371
6372int
6373pending_breakpoint_p (struct breakpoint *b)
6374{
6375 return b->loc == NULL;
6376}
6377
5c458ae8
SM
6378/* Print information on breakpoints (including watchpoints and tracepoints).
6379
6380 If non-NULL, BP_NUM_LIST is a list of numbers and number ranges as
6381 understood by number_or_range_parser. Only breakpoints included in this
6382 list are then printed.
6383
6384 If SHOW_INTERNAL is true, print internal breakpoints.
6385
6386 If FILTER is non-NULL, call it on each breakpoint and only include the
6387 ones for which it returns true.
6388
6389 Return the total number of breakpoints listed. */
c906108c 6390
d77f58be 6391static int
5c458ae8 6392breakpoint_1 (const char *bp_num_list, bool show_internal,
f2478a7e 6393 bool (*filter) (const struct breakpoint *))
c4093a6a 6394{
52f0bd74 6395 struct breakpoint *b;
a6d9a66e 6396 struct bp_location *last_loc = NULL;
7f3b0473 6397 int nr_printable_breakpoints;
79a45b7d 6398 struct value_print_options opts;
a6d9a66e 6399 int print_address_bits = 0;
269b11a2 6400 int print_type_col_width = 14;
79a45e25 6401 struct ui_out *uiout = current_uiout;
269b11a2 6402
79a45b7d
TT
6403 get_user_print_options (&opts);
6404
4a64f543
MS
6405 /* Compute the number of rows in the table, as well as the size
6406 required for address fields. */
7f3b0473
AC
6407 nr_printable_breakpoints = 0;
6408 ALL_BREAKPOINTS (b)
e5a67952
MS
6409 {
6410 /* If we have a filter, only list the breakpoints it accepts. */
6411 if (filter && !filter (b))
6412 continue;
6413
5c458ae8 6414 /* If we have a BP_NUM_LIST string, it is a list of breakpoints to
e5a67952 6415 accept. Skip the others. */
5c458ae8 6416 if (bp_num_list != NULL && *bp_num_list != '\0')
e5a67952 6417 {
5c458ae8 6418 if (show_internal && parse_and_eval_long (bp_num_list) != b->number)
e5a67952 6419 continue;
5c458ae8 6420 if (!show_internal && !number_is_in_list (bp_num_list, b->number))
e5a67952
MS
6421 continue;
6422 }
269b11a2 6423
5c458ae8 6424 if (show_internal || user_breakpoint_p (b))
e5a67952
MS
6425 {
6426 int addr_bit, type_len;
a6d9a66e 6427
e5a67952
MS
6428 addr_bit = breakpoint_address_bits (b);
6429 if (addr_bit > print_address_bits)
6430 print_address_bits = addr_bit;
269b11a2 6431
e5a67952
MS
6432 type_len = strlen (bptype_string (b->type));
6433 if (type_len > print_type_col_width)
6434 print_type_col_width = type_len;
6435
6436 nr_printable_breakpoints++;
6437 }
6438 }
7f3b0473 6439
4a2b031d
TT
6440 {
6441 ui_out_emit_table table_emitter (uiout,
6442 opts.addressprint ? 6 : 5,
6443 nr_printable_breakpoints,
6444 "BreakpointTable");
6445
6446 if (nr_printable_breakpoints > 0)
6447 annotate_breakpoints_headers ();
6448 if (nr_printable_breakpoints > 0)
6449 annotate_field (0);
6450 uiout->table_header (7, ui_left, "number", "Num"); /* 1 */
6451 if (nr_printable_breakpoints > 0)
6452 annotate_field (1);
6453 uiout->table_header (print_type_col_width, ui_left, "type", "Type"); /* 2 */
6454 if (nr_printable_breakpoints > 0)
6455 annotate_field (2);
6456 uiout->table_header (4, ui_left, "disp", "Disp"); /* 3 */
6457 if (nr_printable_breakpoints > 0)
6458 annotate_field (3);
6459 uiout->table_header (3, ui_left, "enabled", "Enb"); /* 4 */
6460 if (opts.addressprint)
6461 {
6462 if (nr_printable_breakpoints > 0)
6463 annotate_field (4);
6464 if (print_address_bits <= 32)
6465 uiout->table_header (10, ui_left, "addr", "Address"); /* 5 */
6466 else
6467 uiout->table_header (18, ui_left, "addr", "Address"); /* 5 */
6468 }
6469 if (nr_printable_breakpoints > 0)
6470 annotate_field (5);
6471 uiout->table_header (40, ui_noalign, "what", "What"); /* 6 */
6472 uiout->table_body ();
6473 if (nr_printable_breakpoints > 0)
6474 annotate_breakpoints_table ();
6475
6476 ALL_BREAKPOINTS (b)
6477 {
6478 QUIT;
6479 /* If we have a filter, only list the breakpoints it accepts. */
6480 if (filter && !filter (b))
6481 continue;
e5a67952 6482
5c458ae8 6483 /* If we have a BP_NUM_LIST string, it is a list of breakpoints to
4a2b031d 6484 accept. Skip the others. */
e5a67952 6485
5c458ae8 6486 if (bp_num_list != NULL && *bp_num_list != '\0')
4a2b031d 6487 {
5c458ae8 6488 if (show_internal) /* maintenance info breakpoint */
4a2b031d 6489 {
5c458ae8 6490 if (parse_and_eval_long (bp_num_list) != b->number)
4a2b031d
TT
6491 continue;
6492 }
6493 else /* all others */
6494 {
5c458ae8 6495 if (!number_is_in_list (bp_num_list, b->number))
4a2b031d
TT
6496 continue;
6497 }
6498 }
6499 /* We only print out user settable breakpoints unless the
5c458ae8
SM
6500 show_internal is set. */
6501 if (show_internal || user_breakpoint_p (b))
6502 print_one_breakpoint (b, &last_loc, show_internal);
4a2b031d
TT
6503 }
6504 }
698384cd 6505
7f3b0473 6506 if (nr_printable_breakpoints == 0)
c906108c 6507 {
4a64f543
MS
6508 /* If there's a filter, let the caller decide how to report
6509 empty list. */
d77f58be
SS
6510 if (!filter)
6511 {
5c458ae8 6512 if (bp_num_list == NULL || *bp_num_list == '\0')
112e8700 6513 uiout->message ("No breakpoints or watchpoints.\n");
d77f58be 6514 else
112e8700 6515 uiout->message ("No breakpoint or watchpoint matching '%s'.\n",
5c458ae8 6516 bp_num_list);
d77f58be 6517 }
c906108c
SS
6518 }
6519 else
c4093a6a 6520 {
a6d9a66e
UW
6521 if (last_loc && !server_command)
6522 set_next_address (last_loc->gdbarch, last_loc->address);
c4093a6a 6523 }
c906108c 6524
4a64f543 6525 /* FIXME? Should this be moved up so that it is only called when
c4093a6a 6526 there have been breakpoints? */
c906108c 6527 annotate_breakpoints_table_end ();
d77f58be
SS
6528
6529 return nr_printable_breakpoints;
c906108c
SS
6530}
6531
ad443146
SS
6532/* Display the value of default-collect in a way that is generally
6533 compatible with the breakpoint list. */
6534
6535static void
6536default_collect_info (void)
6537{
79a45e25
PA
6538 struct ui_out *uiout = current_uiout;
6539
ad443146
SS
6540 /* If it has no value (which is frequently the case), say nothing; a
6541 message like "No default-collect." gets in user's face when it's
6542 not wanted. */
6543 if (!*default_collect)
6544 return;
6545
6546 /* The following phrase lines up nicely with per-tracepoint collect
6547 actions. */
112e8700
SM
6548 uiout->text ("default collect ");
6549 uiout->field_string ("default-collect", default_collect);
6550 uiout->text (" \n");
ad443146
SS
6551}
6552
c906108c 6553static void
0b39b52e 6554info_breakpoints_command (const char *args, int from_tty)
c906108c 6555{
5c458ae8 6556 breakpoint_1 (args, false, NULL);
ad443146
SS
6557
6558 default_collect_info ();
d77f58be
SS
6559}
6560
6561static void
1d12d88f 6562info_watchpoints_command (const char *args, int from_tty)
d77f58be 6563{
5c458ae8 6564 int num_printed = breakpoint_1 (args, false, is_watchpoint);
79a45e25 6565 struct ui_out *uiout = current_uiout;
d77f58be
SS
6566
6567 if (num_printed == 0)
6568 {
e5a67952 6569 if (args == NULL || *args == '\0')
112e8700 6570 uiout->message ("No watchpoints.\n");
d77f58be 6571 else
112e8700 6572 uiout->message ("No watchpoint matching '%s'.\n", args);
d77f58be 6573 }
c906108c
SS
6574}
6575
7a292a7a 6576static void
4495129a 6577maintenance_info_breakpoints (const char *args, int from_tty)
c906108c 6578{
5c458ae8 6579 breakpoint_1 (args, true, NULL);
ad443146
SS
6580
6581 default_collect_info ();
c906108c
SS
6582}
6583
0d381245 6584static int
714835d5 6585breakpoint_has_pc (struct breakpoint *b,
6c95b8df 6586 struct program_space *pspace,
714835d5 6587 CORE_ADDR pc, struct obj_section *section)
0d381245
VP
6588{
6589 struct bp_location *bl = b->loc;
cc59ec59 6590
0d381245
VP
6591 for (; bl; bl = bl->next)
6592 {
6c95b8df
PA
6593 if (bl->pspace == pspace
6594 && bl->address == pc
0d381245
VP
6595 && (!overlay_debugging || bl->section == section))
6596 return 1;
6597 }
6598 return 0;
6599}
6600
672f9b60 6601/* Print a message describing any user-breakpoints set at PC. This
6c95b8df
PA
6602 concerns with logical breakpoints, so we match program spaces, not
6603 address spaces. */
c906108c
SS
6604
6605static void
6c95b8df
PA
6606describe_other_breakpoints (struct gdbarch *gdbarch,
6607 struct program_space *pspace, CORE_ADDR pc,
5af949e3 6608 struct obj_section *section, int thread)
c906108c 6609{
52f0bd74
AC
6610 int others = 0;
6611 struct breakpoint *b;
c906108c
SS
6612
6613 ALL_BREAKPOINTS (b)
672f9b60
KP
6614 others += (user_breakpoint_p (b)
6615 && breakpoint_has_pc (b, pspace, pc, section));
c906108c
SS
6616 if (others > 0)
6617 {
a3f17187
AC
6618 if (others == 1)
6619 printf_filtered (_("Note: breakpoint "));
6620 else /* if (others == ???) */
6621 printf_filtered (_("Note: breakpoints "));
c906108c 6622 ALL_BREAKPOINTS (b)
672f9b60 6623 if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
0d381245
VP
6624 {
6625 others--;
6626 printf_filtered ("%d", b->number);
6627 if (b->thread == -1 && thread != -1)
6628 printf_filtered (" (all threads)");
6629 else if (b->thread != -1)
6630 printf_filtered (" (thread %d)", b->thread);
6631 printf_filtered ("%s%s ",
059fb39f 6632 ((b->enable_state == bp_disabled
f8eba3c6 6633 || b->enable_state == bp_call_disabled)
0d381245 6634 ? " (disabled)"
0d381245
VP
6635 : ""),
6636 (others > 1) ? ","
6637 : ((others == 1) ? " and" : ""));
6638 }
6a831f06
PA
6639 current_uiout->message (_("also set at pc %ps.\n"),
6640 styled_string (address_style.style (),
6641 paddress (gdbarch, pc)));
c906108c
SS
6642 }
6643}
6644\f
c906108c 6645
cb1e4e32
PA
6646/* Return true iff it is meaningful to use the address member of LOC.
6647 For some breakpoint types, the locations' address members are
6648 irrelevant and it makes no sense to attempt to compare them to
6649 other addresses (or use them for any other purpose either).
2d134ed3 6650
cb1e4e32
PA
6651 More specifically, software watchpoints and catchpoints that are
6652 not backed by breakpoints always have a zero valued location
6653 address and we don't want to mark breakpoints of any of these types
6654 to be a duplicate of an actual breakpoint location at address
6655 zero. */
e4f237da 6656
cb1e4e32
PA
6657static bool
6658bl_address_is_meaningful (bp_location *loc)
e4f237da 6659{
cb1e4e32 6660 return loc->loc_type != bp_loc_other;
2d134ed3
PA
6661}
6662
6663/* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
6664 true if LOC1 and LOC2 represent the same watchpoint location. */
6665
6666static int
4a64f543
MS
6667watchpoint_locations_match (struct bp_location *loc1,
6668 struct bp_location *loc2)
2d134ed3 6669{
3a5c3e22
PA
6670 struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
6671 struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
6672
6673 /* Both of them must exist. */
6674 gdb_assert (w1 != NULL);
6675 gdb_assert (w2 != NULL);
2bdf28a0 6676
4a64f543
MS
6677 /* If the target can evaluate the condition expression in hardware,
6678 then we we need to insert both watchpoints even if they are at
6679 the same place. Otherwise the watchpoint will only trigger when
6680 the condition of whichever watchpoint was inserted evaluates to
6681 true, not giving a chance for GDB to check the condition of the
6682 other watchpoint. */
3a5c3e22 6683 if ((w1->cond_exp
4a64f543
MS
6684 && target_can_accel_watchpoint_condition (loc1->address,
6685 loc1->length,
0cf6dd15 6686 loc1->watchpoint_type,
4d01a485 6687 w1->cond_exp.get ()))
3a5c3e22 6688 || (w2->cond_exp
4a64f543
MS
6689 && target_can_accel_watchpoint_condition (loc2->address,
6690 loc2->length,
0cf6dd15 6691 loc2->watchpoint_type,
4d01a485 6692 w2->cond_exp.get ())))
0cf6dd15
TJB
6693 return 0;
6694
85d721b8
PA
6695 /* Note that this checks the owner's type, not the location's. In
6696 case the target does not support read watchpoints, but does
6697 support access watchpoints, we'll have bp_read_watchpoint
6698 watchpoints with hw_access locations. Those should be considered
6699 duplicates of hw_read locations. The hw_read locations will
6700 become hw_access locations later. */
2d134ed3
PA
6701 return (loc1->owner->type == loc2->owner->type
6702 && loc1->pspace->aspace == loc2->pspace->aspace
6703 && loc1->address == loc2->address
6704 && loc1->length == loc2->length);
e4f237da
KB
6705}
6706
31e77af2 6707/* See breakpoint.h. */
6c95b8df 6708
31e77af2 6709int
accd0bcd
YQ
6710breakpoint_address_match (const address_space *aspace1, CORE_ADDR addr1,
6711 const address_space *aspace2, CORE_ADDR addr2)
6c95b8df 6712{
f5656ead 6713 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
6c95b8df
PA
6714 || aspace1 == aspace2)
6715 && addr1 == addr2);
6716}
6717
f1310107
TJB
6718/* Returns true if {ASPACE2,ADDR2} falls within the range determined by
6719 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
6720 matches ASPACE2. On targets that have global breakpoints, the address
6721 space doesn't really matter. */
6722
6723static int
accd0bcd
YQ
6724breakpoint_address_match_range (const address_space *aspace1,
6725 CORE_ADDR addr1,
6726 int len1, const address_space *aspace2,
f1310107
TJB
6727 CORE_ADDR addr2)
6728{
f5656ead 6729 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
f1310107
TJB
6730 || aspace1 == aspace2)
6731 && addr2 >= addr1 && addr2 < addr1 + len1);
6732}
6733
6734/* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
6735 a ranged breakpoint. In most targets, a match happens only if ASPACE
6736 matches the breakpoint's address space. On targets that have global
6737 breakpoints, the address space doesn't really matter. */
6738
6739static int
6740breakpoint_location_address_match (struct bp_location *bl,
accd0bcd 6741 const address_space *aspace,
f1310107
TJB
6742 CORE_ADDR addr)
6743{
6744 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
6745 aspace, addr)
6746 || (bl->length
6747 && breakpoint_address_match_range (bl->pspace->aspace,
6748 bl->address, bl->length,
6749 aspace, addr)));
6750}
6751
d35ae833
PA
6752/* Returns true if the [ADDR,ADDR+LEN) range in ASPACE overlaps
6753 breakpoint BL. BL may be a ranged breakpoint. In most targets, a
6754 match happens only if ASPACE matches the breakpoint's address
6755 space. On targets that have global breakpoints, the address space
6756 doesn't really matter. */
6757
6758static int
6759breakpoint_location_address_range_overlap (struct bp_location *bl,
accd0bcd 6760 const address_space *aspace,
d35ae833
PA
6761 CORE_ADDR addr, int len)
6762{
6763 if (gdbarch_has_global_breakpoints (target_gdbarch ())
6764 || bl->pspace->aspace == aspace)
6765 {
6766 int bl_len = bl->length != 0 ? bl->length : 1;
6767
6768 if (mem_ranges_overlap (addr, len, bl->address, bl_len))
6769 return 1;
6770 }
6771 return 0;
6772}
6773
1e4d1764
YQ
6774/* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
6775 Then, if LOC1 and LOC2 represent the same tracepoint location, returns
6776 true, otherwise returns false. */
6777
6778static int
6779tracepoint_locations_match (struct bp_location *loc1,
6780 struct bp_location *loc2)
6781{
6782 if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
6783 /* Since tracepoint locations are never duplicated with others', tracepoint
6784 locations at the same address of different tracepoints are regarded as
6785 different locations. */
6786 return (loc1->address == loc2->address && loc1->owner == loc2->owner);
6787 else
6788 return 0;
6789}
6790
2d134ed3 6791/* Assuming LOC1 and LOC2's types' have meaningful target addresses
cb1e4e32 6792 (bl_address_is_meaningful), returns true if LOC1 and LOC2 represent
7f32a4d5
PA
6793 the same location. If SW_HW_BPS_MATCH is true, then software
6794 breakpoint locations and hardware breakpoint locations match,
6795 otherwise they don't. */
2d134ed3
PA
6796
6797static int
7f32a4d5
PA
6798breakpoint_locations_match (struct bp_location *loc1,
6799 struct bp_location *loc2,
6800 bool sw_hw_bps_match)
2d134ed3 6801{
2bdf28a0
JK
6802 int hw_point1, hw_point2;
6803
6804 /* Both of them must not be in moribund_locations. */
6805 gdb_assert (loc1->owner != NULL);
6806 gdb_assert (loc2->owner != NULL);
6807
6808 hw_point1 = is_hardware_watchpoint (loc1->owner);
6809 hw_point2 = is_hardware_watchpoint (loc2->owner);
2d134ed3
PA
6810
6811 if (hw_point1 != hw_point2)
6812 return 0;
6813 else if (hw_point1)
6814 return watchpoint_locations_match (loc1, loc2);
1e4d1764
YQ
6815 else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
6816 return tracepoint_locations_match (loc1, loc2);
2d134ed3 6817 else
7f32a4d5
PA
6818 /* We compare bp_location.length in order to cover ranged
6819 breakpoints. Keep this in sync with
6820 bp_location_is_less_than. */
f1310107
TJB
6821 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
6822 loc2->pspace->aspace, loc2->address)
7f32a4d5 6823 && (loc1->loc_type == loc2->loc_type || sw_hw_bps_match)
f1310107 6824 && loc1->length == loc2->length);
2d134ed3
PA
6825}
6826
76897487
KB
6827static void
6828breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
6829 int bnum, int have_bnum)
6830{
f63fbe86
MS
6831 /* The longest string possibly returned by hex_string_custom
6832 is 50 chars. These must be at least that big for safety. */
6833 char astr1[64];
6834 char astr2[64];
76897487 6835
bb599908
PH
6836 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
6837 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
76897487 6838 if (have_bnum)
8a3fe4f8 6839 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
76897487
KB
6840 bnum, astr1, astr2);
6841 else
8a3fe4f8 6842 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
76897487
KB
6843}
6844
4a64f543
MS
6845/* Adjust a breakpoint's address to account for architectural
6846 constraints on breakpoint placement. Return the adjusted address.
6847 Note: Very few targets require this kind of adjustment. For most
6848 targets, this function is simply the identity function. */
76897487
KB
6849
6850static CORE_ADDR
a6d9a66e
UW
6851adjust_breakpoint_address (struct gdbarch *gdbarch,
6852 CORE_ADDR bpaddr, enum bptype bptype)
76897487 6853{
a0de8c21
YQ
6854 if (bptype == bp_watchpoint
6855 || bptype == bp_hardware_watchpoint
6856 || bptype == bp_read_watchpoint
6857 || bptype == bp_access_watchpoint
6858 || bptype == bp_catchpoint)
88f7da05
KB
6859 {
6860 /* Watchpoints and the various bp_catch_* eventpoints should not
6861 have their addresses modified. */
6862 return bpaddr;
6863 }
7c16b83e
PA
6864 else if (bptype == bp_single_step)
6865 {
6866 /* Single-step breakpoints should not have their addresses
6867 modified. If there's any architectural constrain that
6868 applies to this address, then it should have already been
6869 taken into account when the breakpoint was created in the
6870 first place. If we didn't do this, stepping through e.g.,
6871 Thumb-2 IT blocks would break. */
6872 return bpaddr;
6873 }
76897487
KB
6874 else
6875 {
a0de8c21
YQ
6876 CORE_ADDR adjusted_bpaddr = bpaddr;
6877
6878 if (gdbarch_adjust_breakpoint_address_p (gdbarch))
6879 {
6880 /* Some targets have architectural constraints on the placement
6881 of breakpoint instructions. Obtain the adjusted address. */
6882 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
6883 }
76897487 6884
a0de8c21 6885 adjusted_bpaddr = address_significant (gdbarch, adjusted_bpaddr);
76897487
KB
6886
6887 /* An adjusted breakpoint address can significantly alter
6888 a user's expectations. Print a warning if an adjustment
6889 is required. */
6890 if (adjusted_bpaddr != bpaddr)
6891 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
6892
6893 return adjusted_bpaddr;
6894 }
6895}
6896
cb1e4e32
PA
6897static bp_loc_type
6898bp_location_from_bp_type (bptype type)
7cc221ef 6899{
cb1e4e32 6900 switch (type)
e049a4b5
DJ
6901 {
6902 case bp_breakpoint:
7c16b83e 6903 case bp_single_step:
e049a4b5
DJ
6904 case bp_until:
6905 case bp_finish:
6906 case bp_longjmp:
6907 case bp_longjmp_resume:
e2e4d78b 6908 case bp_longjmp_call_dummy:
186c406b
TT
6909 case bp_exception:
6910 case bp_exception_resume:
e049a4b5 6911 case bp_step_resume:
2c03e5be 6912 case bp_hp_step_resume:
e049a4b5
DJ
6913 case bp_watchpoint_scope:
6914 case bp_call_dummy:
aa7d318d 6915 case bp_std_terminate:
e049a4b5
DJ
6916 case bp_shlib_event:
6917 case bp_thread_event:
6918 case bp_overlay_event:
4efc6507 6919 case bp_jit_event:
0fd8e87f 6920 case bp_longjmp_master:
aa7d318d 6921 case bp_std_terminate_master:
186c406b 6922 case bp_exception_master:
0e30163f
JK
6923 case bp_gnu_ifunc_resolver:
6924 case bp_gnu_ifunc_resolver_return:
e7e0cddf 6925 case bp_dprintf:
cb1e4e32 6926 return bp_loc_software_breakpoint;
e049a4b5 6927 case bp_hardware_breakpoint:
cb1e4e32 6928 return bp_loc_hardware_breakpoint;
e049a4b5
DJ
6929 case bp_hardware_watchpoint:
6930 case bp_read_watchpoint:
6931 case bp_access_watchpoint:
cb1e4e32 6932 return bp_loc_hardware_watchpoint;
e049a4b5 6933 case bp_watchpoint:
ce78b96d 6934 case bp_catchpoint:
15c3d785
PA
6935 case bp_tracepoint:
6936 case bp_fast_tracepoint:
0fb4aa4b 6937 case bp_static_tracepoint:
cb1e4e32 6938 return bp_loc_other;
e049a4b5 6939 default:
e2e0b3e5 6940 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
e049a4b5 6941 }
cb1e4e32
PA
6942}
6943
6944bp_location::bp_location (breakpoint *owner, bp_loc_type type)
6945{
6946 this->owner = owner;
6947 this->cond_bytecode = NULL;
6948 this->shlib_disabled = 0;
6949 this->enabled = 1;
6950
6951 this->loc_type = type;
e049a4b5 6952
cb1e4e32
PA
6953 if (this->loc_type == bp_loc_software_breakpoint
6954 || this->loc_type == bp_loc_hardware_breakpoint)
6955 mark_breakpoint_location_modified (this);
6956
6957 this->refc = 1;
6958}
6959
6960bp_location::bp_location (breakpoint *owner)
6961 : bp_location::bp_location (owner,
6962 bp_location_from_bp_type (owner->type))
6963{
28010a5d
PA
6964}
6965
6966/* Allocate a struct bp_location. */
6967
6968static struct bp_location *
6969allocate_bp_location (struct breakpoint *bpt)
6970{
348d480f
PA
6971 return bpt->ops->allocate_location (bpt);
6972}
7cc221ef 6973
f431efe5
PA
6974static void
6975free_bp_location (struct bp_location *loc)
fe3f5fa8 6976{
4d01a485 6977 delete loc;
fe3f5fa8
VP
6978}
6979
f431efe5
PA
6980/* Increment reference count. */
6981
6982static void
6983incref_bp_location (struct bp_location *bl)
6984{
6985 ++bl->refc;
6986}
6987
6988/* Decrement reference count. If the reference count reaches 0,
6989 destroy the bp_location. Sets *BLP to NULL. */
6990
6991static void
6992decref_bp_location (struct bp_location **blp)
6993{
0807b50c
PA
6994 gdb_assert ((*blp)->refc > 0);
6995
f431efe5
PA
6996 if (--(*blp)->refc == 0)
6997 free_bp_location (*blp);
6998 *blp = NULL;
6999}
7000
346774a9 7001/* Add breakpoint B at the end of the global breakpoint chain. */
c906108c 7002
b270e6f9
TT
7003static breakpoint *
7004add_to_breakpoint_chain (std::unique_ptr<breakpoint> &&b)
c906108c 7005{
346774a9 7006 struct breakpoint *b1;
b270e6f9 7007 struct breakpoint *result = b.get ();
c906108c 7008
346774a9
PA
7009 /* Add this breakpoint to the end of the chain so that a list of
7010 breakpoints will come out in order of increasing numbers. */
7011
7012 b1 = breakpoint_chain;
7013 if (b1 == 0)
b270e6f9 7014 breakpoint_chain = b.release ();
346774a9
PA
7015 else
7016 {
7017 while (b1->next)
7018 b1 = b1->next;
b270e6f9 7019 b1->next = b.release ();
346774a9 7020 }
b270e6f9
TT
7021
7022 return result;
346774a9
PA
7023}
7024
7025/* Initializes breakpoint B with type BPTYPE and no locations yet. */
7026
7027static void
7028init_raw_breakpoint_without_location (struct breakpoint *b,
7029 struct gdbarch *gdbarch,
28010a5d 7030 enum bptype bptype,
c0a91b2b 7031 const struct breakpoint_ops *ops)
346774a9 7032{
348d480f
PA
7033 gdb_assert (ops != NULL);
7034
28010a5d 7035 b->ops = ops;
4d28f7a8 7036 b->type = bptype;
a6d9a66e 7037 b->gdbarch = gdbarch;
c906108c
SS
7038 b->language = current_language->la_language;
7039 b->input_radix = input_radix;
d0fb5eae 7040 b->related_breakpoint = b;
346774a9
PA
7041}
7042
7043/* Helper to set_raw_breakpoint below. Creates a breakpoint
7044 that has type BPTYPE and has no locations as yet. */
346774a9
PA
7045
7046static struct breakpoint *
7047set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
348d480f 7048 enum bptype bptype,
c0a91b2b 7049 const struct breakpoint_ops *ops)
346774a9 7050{
3b0871f4 7051 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (bptype);
346774a9 7052
3b0871f4 7053 init_raw_breakpoint_without_location (b.get (), gdbarch, bptype, ops);
b270e6f9 7054 return add_to_breakpoint_chain (std::move (b));
0d381245
VP
7055}
7056
0ba852ab 7057/* Initialize loc->function_name. */
0e30163f 7058
0d381245 7059static void
0ba852ab 7060set_breakpoint_location_function (struct bp_location *loc)
0d381245 7061{
2bdf28a0
JK
7062 gdb_assert (loc->owner != NULL);
7063
0d381245 7064 if (loc->owner->type == bp_breakpoint
1042e4c0 7065 || loc->owner->type == bp_hardware_breakpoint
d77f58be 7066 || is_tracepoint (loc->owner))
0d381245 7067 {
2c02bd72 7068 const char *function_name;
0e30163f 7069
3467ec66 7070 if (loc->msymbol != NULL
f50776aa 7071 && (MSYMBOL_TYPE (loc->msymbol) == mst_text_gnu_ifunc
0ba852ab 7072 || MSYMBOL_TYPE (loc->msymbol) == mst_data_gnu_ifunc))
0e30163f
JK
7073 {
7074 struct breakpoint *b = loc->owner;
7075
c9d95fa3 7076 function_name = loc->msymbol->linkage_name ();
3467ec66
PA
7077
7078 if (b->type == bp_breakpoint && b->loc == loc
7079 && loc->next == NULL && b->related_breakpoint == b)
0e30163f
JK
7080 {
7081 /* Create only the whole new breakpoint of this type but do not
7082 mess more complicated breakpoints with multiple locations. */
7083 b->type = bp_gnu_ifunc_resolver;
6a3a010b
MR
7084 /* Remember the resolver's address for use by the return
7085 breakpoint. */
3467ec66 7086 loc->related_address = loc->address;
0e30163f
JK
7087 }
7088 }
3467ec66
PA
7089 else
7090 find_pc_partial_function (loc->address, &function_name, NULL, NULL);
0e30163f 7091
2c02bd72
DE
7092 if (function_name)
7093 loc->function_name = xstrdup (function_name);
0d381245
VP
7094 }
7095}
7096
a6d9a66e 7097/* Attempt to determine architecture of location identified by SAL. */
1bfeeb0f 7098struct gdbarch *
a6d9a66e
UW
7099get_sal_arch (struct symtab_and_line sal)
7100{
7101 if (sal.section)
08feed99 7102 return sal.section->objfile->arch ();
a6d9a66e 7103 if (sal.symtab)
08feed99 7104 return SYMTAB_OBJFILE (sal.symtab)->arch ();
a6d9a66e
UW
7105
7106 return NULL;
7107}
7108
346774a9
PA
7109/* Low level routine for partially initializing a breakpoint of type
7110 BPTYPE. The newly created breakpoint's address, section, source
c56053d2 7111 file name, and line number are provided by SAL.
0d381245
VP
7112
7113 It is expected that the caller will complete the initialization of
7114 the newly created breakpoint struct as well as output any status
c56053d2 7115 information regarding the creation of a new breakpoint. */
0d381245 7116
346774a9
PA
7117static void
7118init_raw_breakpoint (struct breakpoint *b, struct gdbarch *gdbarch,
28010a5d 7119 struct symtab_and_line sal, enum bptype bptype,
c0a91b2b 7120 const struct breakpoint_ops *ops)
0d381245 7121{
28010a5d 7122 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
346774a9 7123
3742cc8b 7124 add_location_to_breakpoint (b, &sal);
0d381245 7125
6c95b8df
PA
7126 if (bptype != bp_catchpoint)
7127 gdb_assert (sal.pspace != NULL);
7128
f8eba3c6
TT
7129 /* Store the program space that was used to set the breakpoint,
7130 except for ordinary breakpoints, which are independent of the
7131 program space. */
7132 if (bptype != bp_breakpoint && bptype != bp_hardware_breakpoint)
7133 b->pspace = sal.pspace;
346774a9 7134}
c906108c 7135
346774a9
PA
7136/* set_raw_breakpoint is a low level routine for allocating and
7137 partially initializing a breakpoint of type BPTYPE. The newly
7138 created breakpoint's address, section, source file name, and line
7139 number are provided by SAL. The newly created and partially
7140 initialized breakpoint is added to the breakpoint chain and
7141 is also returned as the value of this function.
7142
7143 It is expected that the caller will complete the initialization of
7144 the newly created breakpoint struct as well as output any status
7145 information regarding the creation of a new breakpoint. In
7146 particular, set_raw_breakpoint does NOT set the breakpoint
7147 number! Care should be taken to not allow an error to occur
7148 prior to completing the initialization of the breakpoint. If this
7149 should happen, a bogus breakpoint will be left on the chain. */
7150
7151struct breakpoint *
7152set_raw_breakpoint (struct gdbarch *gdbarch,
348d480f 7153 struct symtab_and_line sal, enum bptype bptype,
c0a91b2b 7154 const struct breakpoint_ops *ops)
346774a9 7155{
3b0871f4 7156 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (bptype);
346774a9 7157
3b0871f4 7158 init_raw_breakpoint (b.get (), gdbarch, sal, bptype, ops);
b270e6f9 7159 return add_to_breakpoint_chain (std::move (b));
c906108c
SS
7160}
7161
53a5351d 7162/* Call this routine when stepping and nexting to enable a breakpoint
186c406b
TT
7163 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
7164 initiated the operation. */
c906108c
SS
7165
7166void
186c406b 7167set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
c906108c 7168{
35df4500 7169 struct breakpoint *b, *b_tmp;
5d5658a1 7170 int thread = tp->global_num;
0fd8e87f
UW
7171
7172 /* To avoid having to rescan all objfile symbols at every step,
7173 we maintain a list of continually-inserted but always disabled
7174 longjmp "master" breakpoints. Here, we simply create momentary
7175 clones of those and enable them for the requested thread. */
35df4500 7176 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df 7177 if (b->pspace == current_program_space
186c406b
TT
7178 && (b->type == bp_longjmp_master
7179 || b->type == bp_exception_master))
0fd8e87f 7180 {
06edf0c0
PA
7181 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
7182 struct breakpoint *clone;
cc59ec59 7183
e2e4d78b
JK
7184 /* longjmp_breakpoint_ops ensures INITIATING_FRAME is cleared again
7185 after their removal. */
06edf0c0 7186 clone = momentary_breakpoint_from_master (b, type,
c1fc2657 7187 &momentary_breakpoint_ops, 1);
0fd8e87f
UW
7188 clone->thread = thread;
7189 }
186c406b
TT
7190
7191 tp->initiating_frame = frame;
c906108c
SS
7192}
7193
611c83ae 7194/* Delete all longjmp breakpoints from THREAD. */
c906108c 7195void
611c83ae 7196delete_longjmp_breakpoint (int thread)
c906108c 7197{
35df4500 7198 struct breakpoint *b, *b_tmp;
c906108c 7199
35df4500 7200 ALL_BREAKPOINTS_SAFE (b, b_tmp)
186c406b 7201 if (b->type == bp_longjmp || b->type == bp_exception)
611c83ae
PA
7202 {
7203 if (b->thread == thread)
7204 delete_breakpoint (b);
7205 }
c906108c
SS
7206}
7207
f59f708a
PA
7208void
7209delete_longjmp_breakpoint_at_next_stop (int thread)
7210{
7211 struct breakpoint *b, *b_tmp;
7212
7213 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7214 if (b->type == bp_longjmp || b->type == bp_exception)
7215 {
7216 if (b->thread == thread)
7217 b->disposition = disp_del_at_next_stop;
7218 }
7219}
7220
e2e4d78b
JK
7221/* Place breakpoints of type bp_longjmp_call_dummy to catch longjmp for
7222 INFERIOR_PTID thread. Chain them all by RELATED_BREAKPOINT and return
7223 pointer to any of them. Return NULL if this system cannot place longjmp
7224 breakpoints. */
7225
7226struct breakpoint *
7227set_longjmp_breakpoint_for_call_dummy (void)
7228{
7229 struct breakpoint *b, *retval = NULL;
7230
7231 ALL_BREAKPOINTS (b)
7232 if (b->pspace == current_program_space && b->type == bp_longjmp_master)
7233 {
7234 struct breakpoint *new_b;
7235
7236 new_b = momentary_breakpoint_from_master (b, bp_longjmp_call_dummy,
a1aa2221
LM
7237 &momentary_breakpoint_ops,
7238 1);
00431a78 7239 new_b->thread = inferior_thread ()->global_num;
e2e4d78b
JK
7240
7241 /* Link NEW_B into the chain of RETVAL breakpoints. */
7242
7243 gdb_assert (new_b->related_breakpoint == new_b);
7244 if (retval == NULL)
7245 retval = new_b;
7246 new_b->related_breakpoint = retval;
7247 while (retval->related_breakpoint != new_b->related_breakpoint)
7248 retval = retval->related_breakpoint;
7249 retval->related_breakpoint = new_b;
7250 }
7251
7252 return retval;
7253}
7254
7255/* Verify all existing dummy frames and their associated breakpoints for
b67a2c6f 7256 TP. Remove those which can no longer be found in the current frame
e2e4d78b
JK
7257 stack.
7258
7259 You should call this function only at places where it is safe to currently
7260 unwind the whole stack. Failed stack unwind would discard live dummy
7261 frames. */
7262
7263void
b67a2c6f 7264check_longjmp_breakpoint_for_call_dummy (struct thread_info *tp)
e2e4d78b
JK
7265{
7266 struct breakpoint *b, *b_tmp;
7267
7268 ALL_BREAKPOINTS_SAFE (b, b_tmp)
5d5658a1 7269 if (b->type == bp_longjmp_call_dummy && b->thread == tp->global_num)
e2e4d78b
JK
7270 {
7271 struct breakpoint *dummy_b = b->related_breakpoint;
7272
7273 while (dummy_b != b && dummy_b->type != bp_call_dummy)
7274 dummy_b = dummy_b->related_breakpoint;
7275 if (dummy_b->type != bp_call_dummy
7276 || frame_find_by_id (dummy_b->frame_id) != NULL)
7277 continue;
7278
00431a78 7279 dummy_frame_discard (dummy_b->frame_id, tp);
e2e4d78b
JK
7280
7281 while (b->related_breakpoint != b)
7282 {
7283 if (b_tmp == b->related_breakpoint)
7284 b_tmp = b->related_breakpoint->next;
7285 delete_breakpoint (b->related_breakpoint);
7286 }
7287 delete_breakpoint (b);
7288 }
7289}
7290
1900040c
MS
7291void
7292enable_overlay_breakpoints (void)
7293{
52f0bd74 7294 struct breakpoint *b;
1900040c
MS
7295
7296 ALL_BREAKPOINTS (b)
7297 if (b->type == bp_overlay_event)
7298 {
7299 b->enable_state = bp_enabled;
44702360 7300 update_global_location_list (UGLL_MAY_INSERT);
c02f5703 7301 overlay_events_enabled = 1;
1900040c
MS
7302 }
7303}
7304
7305void
7306disable_overlay_breakpoints (void)
7307{
52f0bd74 7308 struct breakpoint *b;
1900040c
MS
7309
7310 ALL_BREAKPOINTS (b)
7311 if (b->type == bp_overlay_event)
7312 {
7313 b->enable_state = bp_disabled;
44702360 7314 update_global_location_list (UGLL_DONT_INSERT);
c02f5703 7315 overlay_events_enabled = 0;
1900040c
MS
7316 }
7317}
7318
aa7d318d
TT
7319/* Set an active std::terminate breakpoint for each std::terminate
7320 master breakpoint. */
7321void
7322set_std_terminate_breakpoint (void)
7323{
35df4500 7324 struct breakpoint *b, *b_tmp;
aa7d318d 7325
35df4500 7326 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
7327 if (b->pspace == current_program_space
7328 && b->type == bp_std_terminate_master)
7329 {
06edf0c0 7330 momentary_breakpoint_from_master (b, bp_std_terminate,
a1aa2221 7331 &momentary_breakpoint_ops, 1);
aa7d318d
TT
7332 }
7333}
7334
7335/* Delete all the std::terminate breakpoints. */
7336void
7337delete_std_terminate_breakpoint (void)
7338{
35df4500 7339 struct breakpoint *b, *b_tmp;
aa7d318d 7340
35df4500 7341 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
7342 if (b->type == bp_std_terminate)
7343 delete_breakpoint (b);
7344}
7345
c4093a6a 7346struct breakpoint *
a6d9a66e 7347create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
c4093a6a
JM
7348{
7349 struct breakpoint *b;
c4093a6a 7350
06edf0c0
PA
7351 b = create_internal_breakpoint (gdbarch, address, bp_thread_event,
7352 &internal_breakpoint_ops);
7353
b5de0fa7 7354 b->enable_state = bp_enabled;
f00aae0f 7355 /* location has to be used or breakpoint_re_set will delete me. */
d28cd78a 7356 b->location = new_address_location (b->loc->address, NULL, 0);
c4093a6a 7357
44702360 7358 update_global_location_list_nothrow (UGLL_MAY_INSERT);
74960c60 7359
c4093a6a
JM
7360 return b;
7361}
7362
0101ce28
JJ
7363struct lang_and_radix
7364 {
7365 enum language lang;
7366 int radix;
7367 };
7368
4efc6507
DE
7369/* Create a breakpoint for JIT code registration and unregistration. */
7370
7371struct breakpoint *
7372create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7373{
2a7f3dff
PA
7374 return create_internal_breakpoint (gdbarch, address, bp_jit_event,
7375 &internal_breakpoint_ops);
4efc6507 7376}
0101ce28 7377
03673fc7
PP
7378/* Remove JIT code registration and unregistration breakpoint(s). */
7379
7380void
7381remove_jit_event_breakpoints (void)
7382{
7383 struct breakpoint *b, *b_tmp;
7384
7385 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7386 if (b->type == bp_jit_event
7387 && b->loc->pspace == current_program_space)
7388 delete_breakpoint (b);
7389}
7390
cae688ec
JJ
7391void
7392remove_solib_event_breakpoints (void)
7393{
35df4500 7394 struct breakpoint *b, *b_tmp;
cae688ec 7395
35df4500 7396 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df
PA
7397 if (b->type == bp_shlib_event
7398 && b->loc->pspace == current_program_space)
cae688ec
JJ
7399 delete_breakpoint (b);
7400}
7401
f37f681c
PA
7402/* See breakpoint.h. */
7403
7404void
7405remove_solib_event_breakpoints_at_next_stop (void)
7406{
7407 struct breakpoint *b, *b_tmp;
7408
7409 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7410 if (b->type == bp_shlib_event
7411 && b->loc->pspace == current_program_space)
7412 b->disposition = disp_del_at_next_stop;
7413}
7414
04086b45
PA
7415/* Helper for create_solib_event_breakpoint /
7416 create_and_insert_solib_event_breakpoint. Allows specifying which
7417 INSERT_MODE to pass through to update_global_location_list. */
7418
7419static struct breakpoint *
7420create_solib_event_breakpoint_1 (struct gdbarch *gdbarch, CORE_ADDR address,
7421 enum ugll_insert_mode insert_mode)
cae688ec
JJ
7422{
7423 struct breakpoint *b;
7424
06edf0c0
PA
7425 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event,
7426 &internal_breakpoint_ops);
04086b45 7427 update_global_location_list_nothrow (insert_mode);
cae688ec
JJ
7428 return b;
7429}
7430
04086b45
PA
7431struct breakpoint *
7432create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7433{
7434 return create_solib_event_breakpoint_1 (gdbarch, address, UGLL_MAY_INSERT);
7435}
7436
f37f681c
PA
7437/* See breakpoint.h. */
7438
7439struct breakpoint *
7440create_and_insert_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7441{
7442 struct breakpoint *b;
7443
04086b45
PA
7444 /* Explicitly tell update_global_location_list to insert
7445 locations. */
7446 b = create_solib_event_breakpoint_1 (gdbarch, address, UGLL_INSERT);
f37f681c
PA
7447 if (!b->loc->inserted)
7448 {
7449 delete_breakpoint (b);
7450 return NULL;
7451 }
7452 return b;
7453}
7454
cae688ec
JJ
7455/* Disable any breakpoints that are on code in shared libraries. Only
7456 apply to enabled breakpoints, disabled ones can just stay disabled. */
7457
7458void
cb851954 7459disable_breakpoints_in_shlibs (void)
cae688ec 7460{
876fa593 7461 struct bp_location *loc, **locp_tmp;
cae688ec 7462
876fa593 7463 ALL_BP_LOCATIONS (loc, locp_tmp)
cae688ec 7464 {
2bdf28a0 7465 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 7466 struct breakpoint *b = loc->owner;
2bdf28a0 7467
4a64f543
MS
7468 /* We apply the check to all breakpoints, including disabled for
7469 those with loc->duplicate set. This is so that when breakpoint
7470 becomes enabled, or the duplicate is removed, gdb will try to
7471 insert all breakpoints. If we don't set shlib_disabled here,
7472 we'll try to insert those breakpoints and fail. */
1042e4c0 7473 if (((b->type == bp_breakpoint)
508ccb1f 7474 || (b->type == bp_jit_event)
1042e4c0 7475 || (b->type == bp_hardware_breakpoint)
d77f58be 7476 || (is_tracepoint (b)))
6c95b8df 7477 && loc->pspace == current_program_space
0d381245 7478 && !loc->shlib_disabled
6c95b8df 7479 && solib_name_from_address (loc->pspace, loc->address)
a77053c2 7480 )
0d381245
VP
7481 {
7482 loc->shlib_disabled = 1;
7483 }
cae688ec
JJ
7484 }
7485}
7486
63644780
NB
7487/* Disable any breakpoints and tracepoints that are in SOLIB upon
7488 notification of unloaded_shlib. Only apply to enabled breakpoints,
7489 disabled ones can just stay disabled. */
84acb35a 7490
75149521 7491static void
84acb35a
JJ
7492disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
7493{
876fa593 7494 struct bp_location *loc, **locp_tmp;
84acb35a
JJ
7495 int disabled_shlib_breaks = 0;
7496
876fa593 7497 ALL_BP_LOCATIONS (loc, locp_tmp)
84acb35a 7498 {
2bdf28a0 7499 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 7500 struct breakpoint *b = loc->owner;
cc59ec59 7501
1e4d1764 7502 if (solib->pspace == loc->pspace
e2dd7057 7503 && !loc->shlib_disabled
1e4d1764
YQ
7504 && (((b->type == bp_breakpoint
7505 || b->type == bp_jit_event
7506 || b->type == bp_hardware_breakpoint)
7507 && (loc->loc_type == bp_loc_hardware_breakpoint
7508 || loc->loc_type == bp_loc_software_breakpoint))
7509 || is_tracepoint (b))
e2dd7057 7510 && solib_contains_address_p (solib, loc->address))
84acb35a 7511 {
e2dd7057
PP
7512 loc->shlib_disabled = 1;
7513 /* At this point, we cannot rely on remove_breakpoint
7514 succeeding so we must mark the breakpoint as not inserted
7515 to prevent future errors occurring in remove_breakpoints. */
7516 loc->inserted = 0;
8d3788bd
VP
7517
7518 /* This may cause duplicate notifications for the same breakpoint. */
76727919 7519 gdb::observers::breakpoint_modified.notify (b);
8d3788bd 7520
e2dd7057
PP
7521 if (!disabled_shlib_breaks)
7522 {
223ffa71 7523 target_terminal::ours_for_output ();
3e43a32a
MS
7524 warning (_("Temporarily disabling breakpoints "
7525 "for unloaded shared library \"%s\""),
e2dd7057 7526 solib->so_name);
84acb35a 7527 }
e2dd7057 7528 disabled_shlib_breaks = 1;
84acb35a
JJ
7529 }
7530 }
84acb35a
JJ
7531}
7532
63644780
NB
7533/* Disable any breakpoints and tracepoints in OBJFILE upon
7534 notification of free_objfile. Only apply to enabled breakpoints,
7535 disabled ones can just stay disabled. */
7536
7537static void
7538disable_breakpoints_in_freed_objfile (struct objfile *objfile)
7539{
7540 struct breakpoint *b;
7541
7542 if (objfile == NULL)
7543 return;
7544
d03de421
PA
7545 /* OBJF_SHARED|OBJF_USERLOADED objfiles are dynamic modules manually
7546 managed by the user with add-symbol-file/remove-symbol-file.
7547 Similarly to how breakpoints in shared libraries are handled in
7548 response to "nosharedlibrary", mark breakpoints in such modules
08351840
PA
7549 shlib_disabled so they end up uninserted on the next global
7550 location list update. Shared libraries not loaded by the user
7551 aren't handled here -- they're already handled in
7552 disable_breakpoints_in_unloaded_shlib, called by solib.c's
7553 solib_unloaded observer. We skip objfiles that are not
d03de421
PA
7554 OBJF_SHARED as those aren't considered dynamic objects (e.g. the
7555 main objfile). */
7556 if ((objfile->flags & OBJF_SHARED) == 0
7557 || (objfile->flags & OBJF_USERLOADED) == 0)
63644780
NB
7558 return;
7559
7560 ALL_BREAKPOINTS (b)
7561 {
7562 struct bp_location *loc;
7563 int bp_modified = 0;
7564
7565 if (!is_breakpoint (b) && !is_tracepoint (b))
7566 continue;
7567
7568 for (loc = b->loc; loc != NULL; loc = loc->next)
7569 {
7570 CORE_ADDR loc_addr = loc->address;
7571
7572 if (loc->loc_type != bp_loc_hardware_breakpoint
7573 && loc->loc_type != bp_loc_software_breakpoint)
7574 continue;
7575
7576 if (loc->shlib_disabled != 0)
7577 continue;
7578
7579 if (objfile->pspace != loc->pspace)
7580 continue;
7581
7582 if (loc->loc_type != bp_loc_hardware_breakpoint
7583 && loc->loc_type != bp_loc_software_breakpoint)
7584 continue;
7585
7586 if (is_addr_in_objfile (loc_addr, objfile))
7587 {
7588 loc->shlib_disabled = 1;
08351840
PA
7589 /* At this point, we don't know whether the object was
7590 unmapped from the inferior or not, so leave the
7591 inserted flag alone. We'll handle failure to
7592 uninsert quietly, in case the object was indeed
7593 unmapped. */
63644780
NB
7594
7595 mark_breakpoint_location_modified (loc);
7596
7597 bp_modified = 1;
7598 }
7599 }
7600
7601 if (bp_modified)
76727919 7602 gdb::observers::breakpoint_modified.notify (b);
63644780
NB
7603 }
7604}
7605
ce78b96d
JB
7606/* FORK & VFORK catchpoints. */
7607
e29a4733 7608/* An instance of this type is used to represent a fork or vfork
c1fc2657
SM
7609 catchpoint. A breakpoint is really of this type iff its ops pointer points
7610 to CATCH_FORK_BREAKPOINT_OPS. */
e29a4733 7611
c1fc2657 7612struct fork_catchpoint : public breakpoint
e29a4733 7613{
e29a4733
PA
7614 /* Process id of a child process whose forking triggered this
7615 catchpoint. This field is only valid immediately after this
7616 catchpoint has triggered. */
7617 ptid_t forked_inferior_pid;
7618};
7619
4a64f543
MS
7620/* Implement the "insert" breakpoint_ops method for fork
7621 catchpoints. */
ce78b96d 7622
77b06cd7
TJB
7623static int
7624insert_catch_fork (struct bp_location *bl)
ce78b96d 7625{
e99b03dc 7626 return target_insert_fork_catchpoint (inferior_ptid.pid ());
ce78b96d
JB
7627}
7628
4a64f543
MS
7629/* Implement the "remove" breakpoint_ops method for fork
7630 catchpoints. */
ce78b96d
JB
7631
7632static int
73971819 7633remove_catch_fork (struct bp_location *bl, enum remove_bp_reason reason)
ce78b96d 7634{
e99b03dc 7635 return target_remove_fork_catchpoint (inferior_ptid.pid ());
ce78b96d
JB
7636}
7637
7638/* Implement the "breakpoint_hit" breakpoint_ops method for fork
7639 catchpoints. */
7640
7641static int
f1310107 7642breakpoint_hit_catch_fork (const struct bp_location *bl,
bd522513 7643 const address_space *aspace, CORE_ADDR bp_addr,
09ac7c10 7644 const struct target_waitstatus *ws)
ce78b96d 7645{
e29a4733
PA
7646 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7647
f90263c1
TT
7648 if (ws->kind != TARGET_WAITKIND_FORKED)
7649 return 0;
7650
7651 c->forked_inferior_pid = ws->value.related_pid;
7652 return 1;
ce78b96d
JB
7653}
7654
4a64f543
MS
7655/* Implement the "print_it" breakpoint_ops method for fork
7656 catchpoints. */
ce78b96d
JB
7657
7658static enum print_stop_action
348d480f 7659print_it_catch_fork (bpstat bs)
ce78b96d 7660{
36dfb11c 7661 struct ui_out *uiout = current_uiout;
348d480f
PA
7662 struct breakpoint *b = bs->breakpoint_at;
7663 struct fork_catchpoint *c = (struct fork_catchpoint *) bs->breakpoint_at;
e29a4733 7664
ce78b96d 7665 annotate_catchpoint (b->number);
f303dbd6 7666 maybe_print_thread_hit_breakpoint (uiout);
36dfb11c 7667 if (b->disposition == disp_del)
112e8700 7668 uiout->text ("Temporary catchpoint ");
36dfb11c 7669 else
112e8700
SM
7670 uiout->text ("Catchpoint ");
7671 if (uiout->is_mi_like_p ())
36dfb11c 7672 {
112e8700
SM
7673 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_FORK));
7674 uiout->field_string ("disp", bpdisp_text (b->disposition));
36dfb11c 7675 }
381befee 7676 uiout->field_signed ("bkptno", b->number);
112e8700 7677 uiout->text (" (forked process ");
381befee 7678 uiout->field_signed ("newpid", c->forked_inferior_pid.pid ());
112e8700 7679 uiout->text ("), ");
ce78b96d
JB
7680 return PRINT_SRC_AND_LOC;
7681}
7682
4a64f543
MS
7683/* Implement the "print_one" breakpoint_ops method for fork
7684 catchpoints. */
ce78b96d
JB
7685
7686static void
a6d9a66e 7687print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 7688{
e29a4733 7689 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
79a45b7d 7690 struct value_print_options opts;
79a45e25 7691 struct ui_out *uiout = current_uiout;
79a45b7d
TT
7692
7693 get_user_print_options (&opts);
7694
4a64f543
MS
7695 /* Field 4, the address, is omitted (which makes the columns not
7696 line up too nicely with the headers, but the effect is relatively
7697 readable). */
79a45b7d 7698 if (opts.addressprint)
112e8700 7699 uiout->field_skip ("addr");
ce78b96d 7700 annotate_field (5);
112e8700 7701 uiout->text ("fork");
d7e15655 7702 if (c->forked_inferior_pid != null_ptid)
ce78b96d 7703 {
112e8700 7704 uiout->text (", process ");
381befee 7705 uiout->field_signed ("what", c->forked_inferior_pid.pid ());
112e8700 7706 uiout->spaces (1);
ce78b96d 7707 }
8ac3646f 7708
112e8700
SM
7709 if (uiout->is_mi_like_p ())
7710 uiout->field_string ("catch-type", "fork");
ce78b96d
JB
7711}
7712
7713/* Implement the "print_mention" breakpoint_ops method for fork
7714 catchpoints. */
7715
7716static void
7717print_mention_catch_fork (struct breakpoint *b)
7718{
7719 printf_filtered (_("Catchpoint %d (fork)"), b->number);
7720}
7721
6149aea9
PA
7722/* Implement the "print_recreate" breakpoint_ops method for fork
7723 catchpoints. */
7724
7725static void
7726print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
7727{
7728 fprintf_unfiltered (fp, "catch fork");
d9b3f62e 7729 print_recreate_thread (b, fp);
6149aea9
PA
7730}
7731
ce78b96d
JB
7732/* The breakpoint_ops structure to be used in fork catchpoints. */
7733
2060206e 7734static struct breakpoint_ops catch_fork_breakpoint_ops;
ce78b96d 7735
4a64f543
MS
7736/* Implement the "insert" breakpoint_ops method for vfork
7737 catchpoints. */
ce78b96d 7738
77b06cd7
TJB
7739static int
7740insert_catch_vfork (struct bp_location *bl)
ce78b96d 7741{
e99b03dc 7742 return target_insert_vfork_catchpoint (inferior_ptid.pid ());
ce78b96d
JB
7743}
7744
4a64f543
MS
7745/* Implement the "remove" breakpoint_ops method for vfork
7746 catchpoints. */
ce78b96d
JB
7747
7748static int
73971819 7749remove_catch_vfork (struct bp_location *bl, enum remove_bp_reason reason)
ce78b96d 7750{
e99b03dc 7751 return target_remove_vfork_catchpoint (inferior_ptid.pid ());
ce78b96d
JB
7752}
7753
7754/* Implement the "breakpoint_hit" breakpoint_ops method for vfork
7755 catchpoints. */
7756
7757static int
f1310107 7758breakpoint_hit_catch_vfork (const struct bp_location *bl,
bd522513 7759 const address_space *aspace, CORE_ADDR bp_addr,
09ac7c10 7760 const struct target_waitstatus *ws)
ce78b96d 7761{
e29a4733
PA
7762 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7763
f90263c1
TT
7764 if (ws->kind != TARGET_WAITKIND_VFORKED)
7765 return 0;
7766
7767 c->forked_inferior_pid = ws->value.related_pid;
7768 return 1;
ce78b96d
JB
7769}
7770
4a64f543
MS
7771/* Implement the "print_it" breakpoint_ops method for vfork
7772 catchpoints. */
ce78b96d
JB
7773
7774static enum print_stop_action
348d480f 7775print_it_catch_vfork (bpstat bs)
ce78b96d 7776{
36dfb11c 7777 struct ui_out *uiout = current_uiout;
348d480f 7778 struct breakpoint *b = bs->breakpoint_at;
e29a4733
PA
7779 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
7780
ce78b96d 7781 annotate_catchpoint (b->number);
f303dbd6 7782 maybe_print_thread_hit_breakpoint (uiout);
36dfb11c 7783 if (b->disposition == disp_del)
112e8700 7784 uiout->text ("Temporary catchpoint ");
36dfb11c 7785 else
112e8700
SM
7786 uiout->text ("Catchpoint ");
7787 if (uiout->is_mi_like_p ())
36dfb11c 7788 {
112e8700
SM
7789 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_VFORK));
7790 uiout->field_string ("disp", bpdisp_text (b->disposition));
36dfb11c 7791 }
381befee 7792 uiout->field_signed ("bkptno", b->number);
112e8700 7793 uiout->text (" (vforked process ");
381befee 7794 uiout->field_signed ("newpid", c->forked_inferior_pid.pid ());
112e8700 7795 uiout->text ("), ");
ce78b96d
JB
7796 return PRINT_SRC_AND_LOC;
7797}
7798
4a64f543
MS
7799/* Implement the "print_one" breakpoint_ops method for vfork
7800 catchpoints. */
ce78b96d
JB
7801
7802static void
a6d9a66e 7803print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 7804{
e29a4733 7805 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
79a45b7d 7806 struct value_print_options opts;
79a45e25 7807 struct ui_out *uiout = current_uiout;
79a45b7d
TT
7808
7809 get_user_print_options (&opts);
4a64f543
MS
7810 /* Field 4, the address, is omitted (which makes the columns not
7811 line up too nicely with the headers, but the effect is relatively
7812 readable). */
79a45b7d 7813 if (opts.addressprint)
112e8700 7814 uiout->field_skip ("addr");
ce78b96d 7815 annotate_field (5);
112e8700 7816 uiout->text ("vfork");
d7e15655 7817 if (c->forked_inferior_pid != null_ptid)
ce78b96d 7818 {
112e8700 7819 uiout->text (", process ");
381befee 7820 uiout->field_signed ("what", c->forked_inferior_pid.pid ());
112e8700 7821 uiout->spaces (1);
ce78b96d 7822 }
8ac3646f 7823
112e8700
SM
7824 if (uiout->is_mi_like_p ())
7825 uiout->field_string ("catch-type", "vfork");
ce78b96d
JB
7826}
7827
7828/* Implement the "print_mention" breakpoint_ops method for vfork
7829 catchpoints. */
7830
7831static void
7832print_mention_catch_vfork (struct breakpoint *b)
7833{
7834 printf_filtered (_("Catchpoint %d (vfork)"), b->number);
7835}
7836
6149aea9
PA
7837/* Implement the "print_recreate" breakpoint_ops method for vfork
7838 catchpoints. */
7839
7840static void
7841print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
7842{
7843 fprintf_unfiltered (fp, "catch vfork");
d9b3f62e 7844 print_recreate_thread (b, fp);
6149aea9
PA
7845}
7846
ce78b96d
JB
7847/* The breakpoint_ops structure to be used in vfork catchpoints. */
7848
2060206e 7849static struct breakpoint_ops catch_vfork_breakpoint_ops;
ce78b96d 7850
edcc5120 7851/* An instance of this type is used to represent an solib catchpoint.
c1fc2657 7852 A breakpoint is really of this type iff its ops pointer points to
edcc5120
TT
7853 CATCH_SOLIB_BREAKPOINT_OPS. */
7854
c1fc2657 7855struct solib_catchpoint : public breakpoint
edcc5120 7856{
c1fc2657 7857 ~solib_catchpoint () override;
edcc5120
TT
7858
7859 /* True for "catch load", false for "catch unload". */
7860 unsigned char is_load;
7861
7862 /* Regular expression to match, if any. COMPILED is only valid when
7863 REGEX is non-NULL. */
7864 char *regex;
2d7cc5c7 7865 std::unique_ptr<compiled_regex> compiled;
edcc5120
TT
7866};
7867
c1fc2657 7868solib_catchpoint::~solib_catchpoint ()
edcc5120 7869{
c1fc2657 7870 xfree (this->regex);
edcc5120
TT
7871}
7872
7873static int
7874insert_catch_solib (struct bp_location *ignore)
7875{
7876 return 0;
7877}
7878
7879static int
73971819 7880remove_catch_solib (struct bp_location *ignore, enum remove_bp_reason reason)
edcc5120
TT
7881{
7882 return 0;
7883}
7884
7885static int
7886breakpoint_hit_catch_solib (const struct bp_location *bl,
bd522513 7887 const address_space *aspace,
edcc5120
TT
7888 CORE_ADDR bp_addr,
7889 const struct target_waitstatus *ws)
7890{
7891 struct solib_catchpoint *self = (struct solib_catchpoint *) bl->owner;
7892 struct breakpoint *other;
7893
7894 if (ws->kind == TARGET_WAITKIND_LOADED)
7895 return 1;
7896
7897 ALL_BREAKPOINTS (other)
7898 {
7899 struct bp_location *other_bl;
7900
7901 if (other == bl->owner)
7902 continue;
7903
7904 if (other->type != bp_shlib_event)
7905 continue;
7906
c1fc2657 7907 if (self->pspace != NULL && other->pspace != self->pspace)
edcc5120
TT
7908 continue;
7909
7910 for (other_bl = other->loc; other_bl != NULL; other_bl = other_bl->next)
7911 {
7912 if (other->ops->breakpoint_hit (other_bl, aspace, bp_addr, ws))
7913 return 1;
7914 }
7915 }
7916
7917 return 0;
7918}
7919
7920static void
7921check_status_catch_solib (struct bpstats *bs)
7922{
7923 struct solib_catchpoint *self
7924 = (struct solib_catchpoint *) bs->breakpoint_at;
edcc5120
TT
7925
7926 if (self->is_load)
7927 {
52941706 7928 for (so_list *iter : current_program_space->added_solibs)
edcc5120
TT
7929 {
7930 if (!self->regex
2d7cc5c7 7931 || self->compiled->exec (iter->so_name, 0, NULL, 0) == 0)
edcc5120
TT
7932 return;
7933 }
7934 }
7935 else
7936 {
6fb16ce6 7937 for (const std::string &iter : current_program_space->deleted_solibs)
edcc5120
TT
7938 {
7939 if (!self->regex
6fb16ce6 7940 || self->compiled->exec (iter.c_str (), 0, NULL, 0) == 0)
edcc5120
TT
7941 return;
7942 }
7943 }
7944
7945 bs->stop = 0;
7946 bs->print_it = print_it_noop;
7947}
7948
7949static enum print_stop_action
7950print_it_catch_solib (bpstat bs)
7951{
7952 struct breakpoint *b = bs->breakpoint_at;
7953 struct ui_out *uiout = current_uiout;
7954
7955 annotate_catchpoint (b->number);
f303dbd6 7956 maybe_print_thread_hit_breakpoint (uiout);
edcc5120 7957 if (b->disposition == disp_del)
112e8700 7958 uiout->text ("Temporary catchpoint ");
edcc5120 7959 else
112e8700 7960 uiout->text ("Catchpoint ");
381befee 7961 uiout->field_signed ("bkptno", b->number);
112e8700
SM
7962 uiout->text ("\n");
7963 if (uiout->is_mi_like_p ())
7964 uiout->field_string ("disp", bpdisp_text (b->disposition));
edcc5120
TT
7965 print_solib_event (1);
7966 return PRINT_SRC_AND_LOC;
7967}
7968
7969static void
7970print_one_catch_solib (struct breakpoint *b, struct bp_location **locs)
7971{
7972 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
7973 struct value_print_options opts;
7974 struct ui_out *uiout = current_uiout;
edcc5120
TT
7975
7976 get_user_print_options (&opts);
7977 /* Field 4, the address, is omitted (which makes the columns not
7978 line up too nicely with the headers, but the effect is relatively
7979 readable). */
7980 if (opts.addressprint)
7981 {
7982 annotate_field (4);
112e8700 7983 uiout->field_skip ("addr");
edcc5120
TT
7984 }
7985
528e1572 7986 std::string msg;
edcc5120
TT
7987 annotate_field (5);
7988 if (self->is_load)
7989 {
7990 if (self->regex)
528e1572 7991 msg = string_printf (_("load of library matching %s"), self->regex);
edcc5120 7992 else
528e1572 7993 msg = _("load of library");
edcc5120
TT
7994 }
7995 else
7996 {
7997 if (self->regex)
528e1572 7998 msg = string_printf (_("unload of library matching %s"), self->regex);
edcc5120 7999 else
528e1572 8000 msg = _("unload of library");
edcc5120 8001 }
112e8700 8002 uiout->field_string ("what", msg);
8ac3646f 8003
112e8700
SM
8004 if (uiout->is_mi_like_p ())
8005 uiout->field_string ("catch-type", self->is_load ? "load" : "unload");
edcc5120
TT
8006}
8007
8008static void
8009print_mention_catch_solib (struct breakpoint *b)
8010{
8011 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8012
8013 printf_filtered (_("Catchpoint %d (%s)"), b->number,
8014 self->is_load ? "load" : "unload");
8015}
8016
8017static void
8018print_recreate_catch_solib (struct breakpoint *b, struct ui_file *fp)
8019{
8020 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8021
8022 fprintf_unfiltered (fp, "%s %s",
8023 b->disposition == disp_del ? "tcatch" : "catch",
8024 self->is_load ? "load" : "unload");
8025 if (self->regex)
8026 fprintf_unfiltered (fp, " %s", self->regex);
8027 fprintf_unfiltered (fp, "\n");
8028}
8029
8030static struct breakpoint_ops catch_solib_breakpoint_ops;
8031
91985142
MG
8032/* Shared helper function (MI and CLI) for creating and installing
8033 a shared object event catchpoint. If IS_LOAD is non-zero then
8034 the events to be caught are load events, otherwise they are
8035 unload events. If IS_TEMP is non-zero the catchpoint is a
8036 temporary one. If ENABLED is non-zero the catchpoint is
8037 created in an enabled state. */
edcc5120 8038
91985142 8039void
a121b7c1 8040add_solib_catchpoint (const char *arg, int is_load, int is_temp, int enabled)
edcc5120 8041{
edcc5120 8042 struct gdbarch *gdbarch = get_current_arch ();
edcc5120 8043
edcc5120
TT
8044 if (!arg)
8045 arg = "";
f1735a53 8046 arg = skip_spaces (arg);
edcc5120 8047
36bd8eaa 8048 std::unique_ptr<solib_catchpoint> c (new solib_catchpoint ());
edcc5120
TT
8049
8050 if (*arg != '\0')
8051 {
2d7cc5c7
PA
8052 c->compiled.reset (new compiled_regex (arg, REG_NOSUB,
8053 _("Invalid regexp")));
edcc5120
TT
8054 c->regex = xstrdup (arg);
8055 }
8056
8057 c->is_load = is_load;
36bd8eaa 8058 init_catchpoint (c.get (), gdbarch, is_temp, NULL,
edcc5120
TT
8059 &catch_solib_breakpoint_ops);
8060
c1fc2657 8061 c->enable_state = enabled ? bp_enabled : bp_disabled;
91985142 8062
b270e6f9 8063 install_breakpoint (0, std::move (c), 1);
edcc5120
TT
8064}
8065
91985142
MG
8066/* A helper function that does all the work for "catch load" and
8067 "catch unload". */
8068
8069static void
eb4c3f4a 8070catch_load_or_unload (const char *arg, int from_tty, int is_load,
91985142
MG
8071 struct cmd_list_element *command)
8072{
8073 int tempflag;
8074 const int enabled = 1;
8075
8076 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
8077
8078 add_solib_catchpoint (arg, is_load, tempflag, enabled);
8079}
8080
edcc5120 8081static void
eb4c3f4a 8082catch_load_command_1 (const char *arg, int from_tty,
edcc5120
TT
8083 struct cmd_list_element *command)
8084{
8085 catch_load_or_unload (arg, from_tty, 1, command);
8086}
8087
8088static void
eb4c3f4a 8089catch_unload_command_1 (const char *arg, int from_tty,
edcc5120
TT
8090 struct cmd_list_element *command)
8091{
8092 catch_load_or_unload (arg, from_tty, 0, command);
8093}
8094
346774a9
PA
8095/* Initialize a new breakpoint of the bp_catchpoint kind. If TEMPFLAG
8096 is non-zero, then make the breakpoint temporary. If COND_STRING is
8097 not NULL, then store it in the breakpoint. OPS, if not NULL, is
8098 the breakpoint_ops structure associated to the catchpoint. */
ce78b96d 8099
ab04a2af 8100void
346774a9
PA
8101init_catchpoint (struct breakpoint *b,
8102 struct gdbarch *gdbarch, int tempflag,
63160a43 8103 const char *cond_string,
c0a91b2b 8104 const struct breakpoint_ops *ops)
c906108c 8105{
51abb421 8106 symtab_and_line sal;
6c95b8df 8107 sal.pspace = current_program_space;
c5aa993b 8108
28010a5d 8109 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
ce78b96d 8110
1b36a34b 8111 b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
b5de0fa7 8112 b->disposition = tempflag ? disp_del : disp_donttouch;
346774a9
PA
8113}
8114
28010a5d 8115void
b270e6f9 8116install_breakpoint (int internal, std::unique_ptr<breakpoint> &&arg, int update_gll)
c56053d2 8117{
b270e6f9 8118 breakpoint *b = add_to_breakpoint_chain (std::move (arg));
3a5c3e22 8119 set_breakpoint_number (internal, b);
558a9d82
YQ
8120 if (is_tracepoint (b))
8121 set_tracepoint_count (breakpoint_count);
3a5c3e22
PA
8122 if (!internal)
8123 mention (b);
76727919 8124 gdb::observers::breakpoint_created.notify (b);
3ea46bff
YQ
8125
8126 if (update_gll)
44702360 8127 update_global_location_list (UGLL_MAY_INSERT);
c56053d2
PA
8128}
8129
9b70b993 8130static void
a6d9a66e 8131create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
63160a43 8132 int tempflag, const char *cond_string,
c0a91b2b 8133 const struct breakpoint_ops *ops)
c906108c 8134{
b270e6f9 8135 std::unique_ptr<fork_catchpoint> c (new fork_catchpoint ());
ce78b96d 8136
b270e6f9 8137 init_catchpoint (c.get (), gdbarch, tempflag, cond_string, ops);
e29a4733
PA
8138
8139 c->forked_inferior_pid = null_ptid;
8140
b270e6f9 8141 install_breakpoint (0, std::move (c), 1);
c906108c
SS
8142}
8143
fe798b75
JB
8144/* Exec catchpoints. */
8145
b4d90040 8146/* An instance of this type is used to represent an exec catchpoint.
c1fc2657 8147 A breakpoint is really of this type iff its ops pointer points to
b4d90040
PA
8148 CATCH_EXEC_BREAKPOINT_OPS. */
8149
c1fc2657 8150struct exec_catchpoint : public breakpoint
b4d90040 8151{
c1fc2657 8152 ~exec_catchpoint () override;
b4d90040
PA
8153
8154 /* Filename of a program whose exec triggered this catchpoint.
8155 This field is only valid immediately after this catchpoint has
8156 triggered. */
8157 char *exec_pathname;
8158};
8159
c1fc2657 8160/* Exec catchpoint destructor. */
b4d90040 8161
c1fc2657 8162exec_catchpoint::~exec_catchpoint ()
b4d90040 8163{
c1fc2657 8164 xfree (this->exec_pathname);
b4d90040
PA
8165}
8166
77b06cd7
TJB
8167static int
8168insert_catch_exec (struct bp_location *bl)
c906108c 8169{
e99b03dc 8170 return target_insert_exec_catchpoint (inferior_ptid.pid ());
fe798b75 8171}
c906108c 8172
fe798b75 8173static int
73971819 8174remove_catch_exec (struct bp_location *bl, enum remove_bp_reason reason)
fe798b75 8175{
e99b03dc 8176 return target_remove_exec_catchpoint (inferior_ptid.pid ());
fe798b75 8177}
c906108c 8178
fe798b75 8179static int
f1310107 8180breakpoint_hit_catch_exec (const struct bp_location *bl,
bd522513 8181 const address_space *aspace, CORE_ADDR bp_addr,
09ac7c10 8182 const struct target_waitstatus *ws)
fe798b75 8183{
b4d90040
PA
8184 struct exec_catchpoint *c = (struct exec_catchpoint *) bl->owner;
8185
f90263c1
TT
8186 if (ws->kind != TARGET_WAITKIND_EXECD)
8187 return 0;
8188
8189 c->exec_pathname = xstrdup (ws->value.execd_pathname);
8190 return 1;
fe798b75 8191}
c906108c 8192
fe798b75 8193static enum print_stop_action
348d480f 8194print_it_catch_exec (bpstat bs)
fe798b75 8195{
36dfb11c 8196 struct ui_out *uiout = current_uiout;
348d480f 8197 struct breakpoint *b = bs->breakpoint_at;
b4d90040
PA
8198 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8199
fe798b75 8200 annotate_catchpoint (b->number);
f303dbd6 8201 maybe_print_thread_hit_breakpoint (uiout);
36dfb11c 8202 if (b->disposition == disp_del)
112e8700 8203 uiout->text ("Temporary catchpoint ");
36dfb11c 8204 else
112e8700
SM
8205 uiout->text ("Catchpoint ");
8206 if (uiout->is_mi_like_p ())
36dfb11c 8207 {
112e8700
SM
8208 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_EXEC));
8209 uiout->field_string ("disp", bpdisp_text (b->disposition));
36dfb11c 8210 }
381befee 8211 uiout->field_signed ("bkptno", b->number);
112e8700
SM
8212 uiout->text (" (exec'd ");
8213 uiout->field_string ("new-exec", c->exec_pathname);
8214 uiout->text ("), ");
36dfb11c 8215
fe798b75 8216 return PRINT_SRC_AND_LOC;
c906108c
SS
8217}
8218
fe798b75 8219static void
a6d9a66e 8220print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
fe798b75 8221{
b4d90040 8222 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
fe798b75 8223 struct value_print_options opts;
79a45e25 8224 struct ui_out *uiout = current_uiout;
fe798b75
JB
8225
8226 get_user_print_options (&opts);
8227
8228 /* Field 4, the address, is omitted (which makes the columns
8229 not line up too nicely with the headers, but the effect
8230 is relatively readable). */
8231 if (opts.addressprint)
112e8700 8232 uiout->field_skip ("addr");
fe798b75 8233 annotate_field (5);
112e8700 8234 uiout->text ("exec");
b4d90040 8235 if (c->exec_pathname != NULL)
fe798b75 8236 {
112e8700
SM
8237 uiout->text (", program \"");
8238 uiout->field_string ("what", c->exec_pathname);
8239 uiout->text ("\" ");
fe798b75 8240 }
8ac3646f 8241
112e8700
SM
8242 if (uiout->is_mi_like_p ())
8243 uiout->field_string ("catch-type", "exec");
fe798b75
JB
8244}
8245
8246static void
8247print_mention_catch_exec (struct breakpoint *b)
8248{
8249 printf_filtered (_("Catchpoint %d (exec)"), b->number);
8250}
8251
6149aea9
PA
8252/* Implement the "print_recreate" breakpoint_ops method for exec
8253 catchpoints. */
8254
8255static void
8256print_recreate_catch_exec (struct breakpoint *b, struct ui_file *fp)
8257{
8258 fprintf_unfiltered (fp, "catch exec");
d9b3f62e 8259 print_recreate_thread (b, fp);
6149aea9
PA
8260}
8261
2060206e 8262static struct breakpoint_ops catch_exec_breakpoint_ops;
fe798b75 8263
c906108c 8264static int
fba45db2 8265hw_breakpoint_used_count (void)
c906108c 8266{
c906108c 8267 int i = 0;
f1310107
TJB
8268 struct breakpoint *b;
8269 struct bp_location *bl;
c906108c
SS
8270
8271 ALL_BREAKPOINTS (b)
c5aa993b 8272 {
d6b74ac4 8273 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
f1310107
TJB
8274 for (bl = b->loc; bl; bl = bl->next)
8275 {
8276 /* Special types of hardware breakpoints may use more than
8277 one register. */
348d480f 8278 i += b->ops->resources_needed (bl);
f1310107 8279 }
c5aa993b 8280 }
c906108c
SS
8281
8282 return i;
8283}
8284
a1398e0c
PA
8285/* Returns the resources B would use if it were a hardware
8286 watchpoint. */
8287
c906108c 8288static int
a1398e0c 8289hw_watchpoint_use_count (struct breakpoint *b)
c906108c 8290{
c906108c 8291 int i = 0;
e09342b5 8292 struct bp_location *bl;
c906108c 8293
a1398e0c
PA
8294 if (!breakpoint_enabled (b))
8295 return 0;
8296
8297 for (bl = b->loc; bl; bl = bl->next)
8298 {
8299 /* Special types of hardware watchpoints may use more than
8300 one register. */
8301 i += b->ops->resources_needed (bl);
8302 }
8303
8304 return i;
8305}
8306
8307/* Returns the sum the used resources of all hardware watchpoints of
8308 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
8309 the sum of the used resources of all hardware watchpoints of other
8310 types _not_ TYPE. */
8311
8312static int
8313hw_watchpoint_used_count_others (struct breakpoint *except,
8314 enum bptype type, int *other_type_used)
8315{
8316 int i = 0;
8317 struct breakpoint *b;
8318
c906108c
SS
8319 *other_type_used = 0;
8320 ALL_BREAKPOINTS (b)
e09342b5 8321 {
a1398e0c
PA
8322 if (b == except)
8323 continue;
e09342b5
TJB
8324 if (!breakpoint_enabled (b))
8325 continue;
8326
a1398e0c
PA
8327 if (b->type == type)
8328 i += hw_watchpoint_use_count (b);
8329 else if (is_hardware_watchpoint (b))
8330 *other_type_used = 1;
e09342b5
TJB
8331 }
8332
c906108c
SS
8333 return i;
8334}
8335
c906108c 8336void
fba45db2 8337disable_watchpoints_before_interactive_call_start (void)
c906108c 8338{
c5aa993b 8339 struct breakpoint *b;
c906108c
SS
8340
8341 ALL_BREAKPOINTS (b)
c5aa993b 8342 {
cc60f2e3 8343 if (is_watchpoint (b) && breakpoint_enabled (b))
c5aa993b 8344 {
b5de0fa7 8345 b->enable_state = bp_call_disabled;
44702360 8346 update_global_location_list (UGLL_DONT_INSERT);
c5aa993b
JM
8347 }
8348 }
c906108c
SS
8349}
8350
8351void
fba45db2 8352enable_watchpoints_after_interactive_call_stop (void)
c906108c 8353{
c5aa993b 8354 struct breakpoint *b;
c906108c
SS
8355
8356 ALL_BREAKPOINTS (b)
c5aa993b 8357 {
cc60f2e3 8358 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
c5aa993b 8359 {
b5de0fa7 8360 b->enable_state = bp_enabled;
44702360 8361 update_global_location_list (UGLL_MAY_INSERT);
c5aa993b
JM
8362 }
8363 }
c906108c
SS
8364}
8365
8bea4e01
UW
8366void
8367disable_breakpoints_before_startup (void)
8368{
6c95b8df 8369 current_program_space->executing_startup = 1;
44702360 8370 update_global_location_list (UGLL_DONT_INSERT);
8bea4e01
UW
8371}
8372
8373void
8374enable_breakpoints_after_startup (void)
8375{
6c95b8df 8376 current_program_space->executing_startup = 0;
f8eba3c6 8377 breakpoint_re_set ();
8bea4e01
UW
8378}
8379
7c16b83e
PA
8380/* Create a new single-step breakpoint for thread THREAD, with no
8381 locations. */
c906108c 8382
7c16b83e
PA
8383static struct breakpoint *
8384new_single_step_breakpoint (int thread, struct gdbarch *gdbarch)
8385{
b270e6f9 8386 std::unique_ptr<breakpoint> b (new breakpoint ());
7c16b83e 8387
b270e6f9 8388 init_raw_breakpoint_without_location (b.get (), gdbarch, bp_single_step,
7c16b83e
PA
8389 &momentary_breakpoint_ops);
8390
8391 b->disposition = disp_donttouch;
8392 b->frame_id = null_frame_id;
8393
8394 b->thread = thread;
8395 gdb_assert (b->thread != 0);
8396
b270e6f9 8397 return add_to_breakpoint_chain (std::move (b));
7c16b83e
PA
8398}
8399
8400/* Set a momentary breakpoint of type TYPE at address specified by
8401 SAL. If FRAME_ID is valid, the breakpoint is restricted to that
8402 frame. */
c906108c 8403
454dafbd 8404breakpoint_up
a6d9a66e
UW
8405set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
8406 struct frame_id frame_id, enum bptype type)
c906108c 8407{
52f0bd74 8408 struct breakpoint *b;
edb3359d 8409
193facb3
JK
8410 /* If FRAME_ID is valid, it should be a real frame, not an inlined or
8411 tail-called one. */
8412 gdb_assert (!frame_id_artificial_p (frame_id));
edb3359d 8413
06edf0c0 8414 b = set_raw_breakpoint (gdbarch, sal, type, &momentary_breakpoint_ops);
b5de0fa7
EZ
8415 b->enable_state = bp_enabled;
8416 b->disposition = disp_donttouch;
818dd999 8417 b->frame_id = frame_id;
c906108c 8418
00431a78 8419 b->thread = inferior_thread ()->global_num;
c906108c 8420
44702360 8421 update_global_location_list_nothrow (UGLL_MAY_INSERT);
74960c60 8422
454dafbd 8423 return breakpoint_up (b);
c906108c 8424}
611c83ae 8425
06edf0c0 8426/* Make a momentary breakpoint based on the master breakpoint ORIG.
a1aa2221
LM
8427 The new breakpoint will have type TYPE, use OPS as its
8428 breakpoint_ops, and will set enabled to LOC_ENABLED. */
e58b0e63 8429
06edf0c0
PA
8430static struct breakpoint *
8431momentary_breakpoint_from_master (struct breakpoint *orig,
8432 enum bptype type,
a1aa2221
LM
8433 const struct breakpoint_ops *ops,
8434 int loc_enabled)
e58b0e63
PA
8435{
8436 struct breakpoint *copy;
8437
06edf0c0 8438 copy = set_raw_breakpoint_without_location (orig->gdbarch, type, ops);
e58b0e63 8439 copy->loc = allocate_bp_location (copy);
0ba852ab 8440 set_breakpoint_location_function (copy->loc);
e58b0e63 8441
a6d9a66e 8442 copy->loc->gdbarch = orig->loc->gdbarch;
e58b0e63
PA
8443 copy->loc->requested_address = orig->loc->requested_address;
8444 copy->loc->address = orig->loc->address;
8445 copy->loc->section = orig->loc->section;
6c95b8df 8446 copy->loc->pspace = orig->loc->pspace;
55aa24fb 8447 copy->loc->probe = orig->loc->probe;
f8eba3c6 8448 copy->loc->line_number = orig->loc->line_number;
2f202fde 8449 copy->loc->symtab = orig->loc->symtab;
a1aa2221 8450 copy->loc->enabled = loc_enabled;
e58b0e63
PA
8451 copy->frame_id = orig->frame_id;
8452 copy->thread = orig->thread;
6c95b8df 8453 copy->pspace = orig->pspace;
e58b0e63
PA
8454
8455 copy->enable_state = bp_enabled;
8456 copy->disposition = disp_donttouch;
8457 copy->number = internal_breakpoint_number--;
8458
44702360 8459 update_global_location_list_nothrow (UGLL_DONT_INSERT);
e58b0e63
PA
8460 return copy;
8461}
8462
06edf0c0
PA
8463/* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
8464 ORIG is NULL. */
8465
8466struct breakpoint *
8467clone_momentary_breakpoint (struct breakpoint *orig)
8468{
8469 /* If there's nothing to clone, then return nothing. */
8470 if (orig == NULL)
8471 return NULL;
8472
a1aa2221 8473 return momentary_breakpoint_from_master (orig, orig->type, orig->ops, 0);
06edf0c0
PA
8474}
8475
454dafbd 8476breakpoint_up
a6d9a66e
UW
8477set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
8478 enum bptype type)
611c83ae
PA
8479{
8480 struct symtab_and_line sal;
8481
8482 sal = find_pc_line (pc, 0);
8483 sal.pc = pc;
8484 sal.section = find_pc_overlay (pc);
8485 sal.explicit_pc = 1;
8486
a6d9a66e 8487 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
611c83ae 8488}
c906108c 8489\f
c5aa993b 8490
c906108c
SS
8491/* Tell the user we have just set a breakpoint B. */
8492
8493static void
fba45db2 8494mention (struct breakpoint *b)
c906108c 8495{
348d480f 8496 b->ops->print_mention (b);
2d33446d 8497 current_uiout->text ("\n");
c906108c 8498}
c906108c 8499\f
c5aa993b 8500
5133a315 8501static bool bp_loc_is_permanent (struct bp_location *loc);
1a853c52 8502
7f32a4d5
PA
8503/* Handle "set breakpoint auto-hw on".
8504
8505 If the explicitly specified breakpoint type is not hardware
8506 breakpoint, check the memory map to see whether the breakpoint
8507 address is in read-only memory.
8508
8509 - location type is not hardware breakpoint, memory is read-only.
8510 We change the type of the location to hardware breakpoint.
8511
8512 - location type is hardware breakpoint, memory is read-write. This
8513 means we've previously made the location hardware one, but then the
8514 memory map changed, so we undo.
8515*/
8516
8517static void
8518handle_automatic_hardware_breakpoints (bp_location *bl)
8519{
8520 if (automatic_hardware_breakpoints
8521 && bl->owner->type != bp_hardware_breakpoint
8522 && (bl->loc_type == bp_loc_software_breakpoint
8523 || bl->loc_type == bp_loc_hardware_breakpoint))
8524 {
8525 /* When breakpoints are removed, remove_breakpoints will use
8526 location types we've just set here, the only possible problem
8527 is that memory map has changed during running program, but
8528 it's not going to work anyway with current gdb. */
8529 mem_region *mr = lookup_mem_region (bl->address);
8530
8531 if (mr != nullptr)
8532 {
8533 enum bp_loc_type new_type;
8534
8535 if (mr->attrib.mode != MEM_RW)
8536 new_type = bp_loc_hardware_breakpoint;
8537 else
8538 new_type = bp_loc_software_breakpoint;
8539
8540 if (new_type != bl->loc_type)
8541 {
8542 static bool said = false;
8543
8544 bl->loc_type = new_type;
8545 if (!said)
8546 {
8547 fprintf_filtered (gdb_stdout,
8548 _("Note: automatically using "
8549 "hardware breakpoints for "
8550 "read-only addresses.\n"));
8551 said = true;
8552 }
8553 }
8554 }
8555 }
8556}
8557
0d381245 8558static struct bp_location *
39d61571 8559add_location_to_breakpoint (struct breakpoint *b,
0d381245
VP
8560 const struct symtab_and_line *sal)
8561{
8562 struct bp_location *loc, **tmp;
3742cc8b
YQ
8563 CORE_ADDR adjusted_address;
8564 struct gdbarch *loc_gdbarch = get_sal_arch (*sal);
8565
8566 if (loc_gdbarch == NULL)
8567 loc_gdbarch = b->gdbarch;
8568
8569 /* Adjust the breakpoint's address prior to allocating a location.
8570 Once we call allocate_bp_location(), that mostly uninitialized
8571 location will be placed on the location chain. Adjustment of the
8572 breakpoint may cause target_read_memory() to be called and we do
8573 not want its scan of the location chain to find a breakpoint and
8574 location that's only been partially initialized. */
8575 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
8576 sal->pc, b->type);
0d381245 8577
d30113d4 8578 /* Sort the locations by their ADDRESS. */
39d61571 8579 loc = allocate_bp_location (b);
d30113d4
JK
8580 for (tmp = &(b->loc); *tmp != NULL && (*tmp)->address <= adjusted_address;
8581 tmp = &((*tmp)->next))
0d381245 8582 ;
d30113d4 8583 loc->next = *tmp;
0d381245 8584 *tmp = loc;
3742cc8b 8585
0d381245 8586 loc->requested_address = sal->pc;
3742cc8b 8587 loc->address = adjusted_address;
6c95b8df 8588 loc->pspace = sal->pspace;
935676c9 8589 loc->probe.prob = sal->prob;
729662a5 8590 loc->probe.objfile = sal->objfile;
6c95b8df 8591 gdb_assert (loc->pspace != NULL);
0d381245 8592 loc->section = sal->section;
3742cc8b 8593 loc->gdbarch = loc_gdbarch;
f8eba3c6 8594 loc->line_number = sal->line;
2f202fde 8595 loc->symtab = sal->symtab;
4a27f119 8596 loc->symbol = sal->symbol;
3467ec66
PA
8597 loc->msymbol = sal->msymbol;
8598 loc->objfile = sal->objfile;
f8eba3c6 8599
0ba852ab 8600 set_breakpoint_location_function (loc);
1a853c52 8601
6ae88661
LM
8602 /* While by definition, permanent breakpoints are already present in the
8603 code, we don't mark the location as inserted. Normally one would expect
8604 that GDB could rely on that breakpoint instruction to stop the program,
8605 thus removing the need to insert its own breakpoint, except that executing
8606 the breakpoint instruction can kill the target instead of reporting a
8607 SIGTRAP. E.g., on SPARC, when interrupts are disabled, executing the
8608 instruction resets the CPU, so QEMU 2.0.0 for SPARC correspondingly dies
8609 with "Trap 0x02 while interrupts disabled, Error state". Letting the
8610 breakpoint be inserted normally results in QEMU knowing about the GDB
8611 breakpoint, and thus trap before the breakpoint instruction is executed.
8612 (If GDB later needs to continue execution past the permanent breakpoint,
8613 it manually increments the PC, thus avoiding executing the breakpoint
8614 instruction.) */
1a853c52 8615 if (bp_loc_is_permanent (loc))
6ae88661 8616 loc->permanent = 1;
1a853c52 8617
0d381245
VP
8618 return loc;
8619}
514f746b
AR
8620\f
8621
5133a315
LM
8622/* Return true if LOC is pointing to a permanent breakpoint,
8623 return false otherwise. */
1cf4d951 8624
5133a315 8625static bool
1cf4d951
PA
8626bp_loc_is_permanent (struct bp_location *loc)
8627{
514f746b
AR
8628 gdb_assert (loc != NULL);
8629
cb1e4e32
PA
8630 /* If we have a non-breakpoint-backed catchpoint or a software
8631 watchpoint, just return 0. We should not attempt to read from
8632 the addresses the locations of these breakpoint types point to.
5133a315 8633 gdbarch_program_breakpoint_here_p, below, will attempt to read
244558af 8634 memory. */
cb1e4e32 8635 if (!bl_address_is_meaningful (loc))
5133a315 8636 return false;
244558af 8637
5ed8105e 8638 scoped_restore_current_pspace_and_thread restore_pspace_thread;
6c95b8df 8639 switch_to_program_space_and_thread (loc->pspace);
5133a315 8640 return gdbarch_program_breakpoint_here_p (loc->gdbarch, loc->address);
514f746b
AR
8641}
8642
e7e0cddf
SS
8643/* Build a command list for the dprintf corresponding to the current
8644 settings of the dprintf style options. */
8645
8646static void
8647update_dprintf_command_list (struct breakpoint *b)
8648{
8649 char *dprintf_args = b->extra_string;
8650 char *printf_line = NULL;
8651
8652 if (!dprintf_args)
8653 return;
8654
8655 dprintf_args = skip_spaces (dprintf_args);
8656
8657 /* Allow a comma, as it may have terminated a location, but don't
8658 insist on it. */
8659 if (*dprintf_args == ',')
8660 ++dprintf_args;
8661 dprintf_args = skip_spaces (dprintf_args);
8662
8663 if (*dprintf_args != '"')
8664 error (_("Bad format string, missing '\"'."));
8665
d3ce09f5 8666 if (strcmp (dprintf_style, dprintf_style_gdb) == 0)
e7e0cddf 8667 printf_line = xstrprintf ("printf %s", dprintf_args);
d3ce09f5 8668 else if (strcmp (dprintf_style, dprintf_style_call) == 0)
e7e0cddf
SS
8669 {
8670 if (!dprintf_function)
8671 error (_("No function supplied for dprintf call"));
8672
8673 if (dprintf_channel && strlen (dprintf_channel) > 0)
8674 printf_line = xstrprintf ("call (void) %s (%s,%s)",
8675 dprintf_function,
8676 dprintf_channel,
8677 dprintf_args);
8678 else
8679 printf_line = xstrprintf ("call (void) %s (%s)",
8680 dprintf_function,
8681 dprintf_args);
8682 }
d3ce09f5
SS
8683 else if (strcmp (dprintf_style, dprintf_style_agent) == 0)
8684 {
8685 if (target_can_run_breakpoint_commands ())
8686 printf_line = xstrprintf ("agent-printf %s", dprintf_args);
8687 else
8688 {
8689 warning (_("Target cannot run dprintf commands, falling back to GDB printf"));
8690 printf_line = xstrprintf ("printf %s", dprintf_args);
8691 }
8692 }
e7e0cddf
SS
8693 else
8694 internal_error (__FILE__, __LINE__,
8695 _("Invalid dprintf style."));
8696
f28045c2 8697 gdb_assert (printf_line != NULL);
e7e0cddf 8698
12973681
TT
8699 /* Manufacture a printf sequence. */
8700 struct command_line *printf_cmd_line
8701 = new struct command_line (simple_control, printf_line);
8702 breakpoint_set_commands (b, counted_command_line (printf_cmd_line,
8703 command_lines_deleter ()));
e7e0cddf
SS
8704}
8705
8706/* Update all dprintf commands, making their command lists reflect
8707 current style settings. */
8708
8709static void
eb4c3f4a 8710update_dprintf_commands (const char *args, int from_tty,
e7e0cddf
SS
8711 struct cmd_list_element *c)
8712{
8713 struct breakpoint *b;
8714
8715 ALL_BREAKPOINTS (b)
8716 {
8717 if (b->type == bp_dprintf)
8718 update_dprintf_command_list (b);
8719 }
8720}
c3f6f71d 8721
f00aae0f
KS
8722/* Create a breakpoint with SAL as location. Use LOCATION
8723 as a description of the location, and COND_STRING
b35a8b2f
DE
8724 as condition expression. If LOCATION is NULL then create an
8725 "address location" from the address in the SAL. */
018d34a4
VP
8726
8727static void
d9b3f62e 8728init_breakpoint_sal (struct breakpoint *b, struct gdbarch *gdbarch,
6c5b2ebe 8729 gdb::array_view<const symtab_and_line> sals,
ffc2605c 8730 event_location_up &&location,
e1e01040
PA
8731 gdb::unique_xmalloc_ptr<char> filter,
8732 gdb::unique_xmalloc_ptr<char> cond_string,
8733 gdb::unique_xmalloc_ptr<char> extra_string,
d9b3f62e
PA
8734 enum bptype type, enum bpdisp disposition,
8735 int thread, int task, int ignore_count,
c0a91b2b 8736 const struct breakpoint_ops *ops, int from_tty,
44f238bb
PA
8737 int enabled, int internal, unsigned flags,
8738 int display_canonical)
018d34a4 8739{
0d381245 8740 int i;
018d34a4
VP
8741
8742 if (type == bp_hardware_breakpoint)
8743 {
fbbd034e
AS
8744 int target_resources_ok;
8745
8746 i = hw_breakpoint_used_count ();
8747 target_resources_ok =
8748 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
018d34a4
VP
8749 i + 1, 0);
8750 if (target_resources_ok == 0)
8751 error (_("No hardware breakpoint support in the target."));
8752 else if (target_resources_ok < 0)
8753 error (_("Hardware breakpoints used exceeds limit."));
8754 }
8755
6c5b2ebe 8756 gdb_assert (!sals.empty ());
6c95b8df 8757
6c5b2ebe 8758 for (const auto &sal : sals)
0d381245 8759 {
0d381245
VP
8760 struct bp_location *loc;
8761
8762 if (from_tty)
5af949e3
UW
8763 {
8764 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
8765 if (!loc_gdbarch)
8766 loc_gdbarch = gdbarch;
8767
8768 describe_other_breakpoints (loc_gdbarch,
6c95b8df 8769 sal.pspace, sal.pc, sal.section, thread);
5af949e3 8770 }
0d381245 8771
6c5b2ebe 8772 if (&sal == &sals[0])
0d381245 8773 {
d9b3f62e 8774 init_raw_breakpoint (b, gdbarch, sal, type, ops);
0d381245 8775 b->thread = thread;
4a306c9a 8776 b->task = task;
855a6e68 8777
e1e01040
PA
8778 b->cond_string = cond_string.release ();
8779 b->extra_string = extra_string.release ();
0d381245 8780 b->ignore_count = ignore_count;
41447f92 8781 b->enable_state = enabled ? bp_enabled : bp_disabled;
0d381245 8782 b->disposition = disposition;
6c95b8df 8783
44f238bb
PA
8784 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
8785 b->loc->inserted = 1;
8786
0fb4aa4b
PA
8787 if (type == bp_static_tracepoint)
8788 {
d9b3f62e 8789 struct tracepoint *t = (struct tracepoint *) b;
0fb4aa4b
PA
8790 struct static_tracepoint_marker marker;
8791
983af33b 8792 if (strace_marker_p (b))
0fb4aa4b
PA
8793 {
8794 /* We already know the marker exists, otherwise, we
8795 wouldn't see a sal for it. */
d28cd78a
TT
8796 const char *p
8797 = &event_location_to_string (b->location.get ())[3];
f00aae0f 8798 const char *endp;
0fb4aa4b 8799
f1735a53 8800 p = skip_spaces (p);
0fb4aa4b 8801
f1735a53 8802 endp = skip_to_space (p);
0fb4aa4b 8803
5d9310c4 8804 t->static_trace_marker_id.assign (p, endp - p);
0fb4aa4b 8805
3e43a32a
MS
8806 printf_filtered (_("Probed static tracepoint "
8807 "marker \"%s\"\n"),
5d9310c4 8808 t->static_trace_marker_id.c_str ());
0fb4aa4b
PA
8809 }
8810 else if (target_static_tracepoint_marker_at (sal.pc, &marker))
8811 {
5d9310c4 8812 t->static_trace_marker_id = std::move (marker.str_id);
0fb4aa4b 8813
3e43a32a
MS
8814 printf_filtered (_("Probed static tracepoint "
8815 "marker \"%s\"\n"),
5d9310c4 8816 t->static_trace_marker_id.c_str ());
0fb4aa4b
PA
8817 }
8818 else
3e43a32a
MS
8819 warning (_("Couldn't determine the static "
8820 "tracepoint marker to probe"));
0fb4aa4b
PA
8821 }
8822
0d381245
VP
8823 loc = b->loc;
8824 }
8825 else
018d34a4 8826 {
39d61571 8827 loc = add_location_to_breakpoint (b, &sal);
44f238bb
PA
8828 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
8829 loc->inserted = 1;
0d381245
VP
8830 }
8831
8832 if (b->cond_string)
8833 {
bbc13ae3
KS
8834 const char *arg = b->cond_string;
8835
1bb9788d
TT
8836 loc->cond = parse_exp_1 (&arg, loc->address,
8837 block_for_pc (loc->address), 0);
0d381245 8838 if (*arg)
588ae58c 8839 error (_("Garbage '%s' follows condition"), arg);
018d34a4 8840 }
e7e0cddf
SS
8841
8842 /* Dynamic printf requires and uses additional arguments on the
8843 command line, otherwise it's an error. */
8844 if (type == bp_dprintf)
8845 {
8846 if (b->extra_string)
8847 update_dprintf_command_list (b);
8848 else
8849 error (_("Format string required"));
8850 }
8851 else if (b->extra_string)
588ae58c 8852 error (_("Garbage '%s' at end of command"), b->extra_string);
855a6e68 8853 }
018d34a4 8854
56435ebe 8855 b->display_canonical = display_canonical;
f00aae0f 8856 if (location != NULL)
d28cd78a 8857 b->location = std::move (location);
018d34a4 8858 else
d28cd78a 8859 b->location = new_address_location (b->loc->address, NULL, 0);
c0e8dcd8 8860 b->filter = std::move (filter);
d9b3f62e 8861}
018d34a4 8862
d9b3f62e
PA
8863static void
8864create_breakpoint_sal (struct gdbarch *gdbarch,
6c5b2ebe 8865 gdb::array_view<const symtab_and_line> sals,
ffc2605c 8866 event_location_up &&location,
e1e01040
PA
8867 gdb::unique_xmalloc_ptr<char> filter,
8868 gdb::unique_xmalloc_ptr<char> cond_string,
8869 gdb::unique_xmalloc_ptr<char> extra_string,
d9b3f62e
PA
8870 enum bptype type, enum bpdisp disposition,
8871 int thread, int task, int ignore_count,
c0a91b2b 8872 const struct breakpoint_ops *ops, int from_tty,
44f238bb
PA
8873 int enabled, int internal, unsigned flags,
8874 int display_canonical)
d9b3f62e 8875{
a5e364af 8876 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (type);
d9b3f62e 8877
a5e364af 8878 init_breakpoint_sal (b.get (), gdbarch,
ffc2605c 8879 sals, std::move (location),
e1e01040
PA
8880 std::move (filter),
8881 std::move (cond_string),
8882 std::move (extra_string),
d9b3f62e
PA
8883 type, disposition,
8884 thread, task, ignore_count,
8885 ops, from_tty,
44f238bb
PA
8886 enabled, internal, flags,
8887 display_canonical);
d9b3f62e 8888
b270e6f9 8889 install_breakpoint (internal, std::move (b), 0);
018d34a4
VP
8890}
8891
8892/* Add SALS.nelts breakpoints to the breakpoint table. For each
8893 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
8894 value. COND_STRING, if not NULL, specified the condition to be
8895 used for all breakpoints. Essentially the only case where
8896 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
8897 function. In that case, it's still not possible to specify
8898 separate conditions for different overloaded functions, so
8899 we take just a single condition string.
8900
c3f6f71d 8901 NOTE: If the function succeeds, the caller is expected to cleanup
018d34a4 8902 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
c3f6f71d
JM
8903 array contents). If the function fails (error() is called), the
8904 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
4a64f543 8905 COND and SALS arrays and each of those arrays contents. */
c906108c
SS
8906
8907static void
8cdf0e15 8908create_breakpoints_sal (struct gdbarch *gdbarch,
7efd8fc2 8909 struct linespec_result *canonical,
e1e01040
PA
8910 gdb::unique_xmalloc_ptr<char> cond_string,
8911 gdb::unique_xmalloc_ptr<char> extra_string,
8cdf0e15
VP
8912 enum bptype type, enum bpdisp disposition,
8913 int thread, int task, int ignore_count,
c0a91b2b 8914 const struct breakpoint_ops *ops, int from_tty,
44f238bb 8915 int enabled, int internal, unsigned flags)
c906108c 8916{
f8eba3c6 8917 if (canonical->pre_expanded)
6c5b2ebe 8918 gdb_assert (canonical->lsals.size () == 1);
f8eba3c6 8919
6c5b2ebe 8920 for (const auto &lsal : canonical->lsals)
c3f6f71d 8921 {
f00aae0f 8922 /* Note that 'location' can be NULL in the case of a plain
f8eba3c6 8923 'break', without arguments. */
ffc2605c 8924 event_location_up location
f00aae0f 8925 = (canonical->location != NULL
8e9e35b1 8926 ? copy_event_location (canonical->location.get ()) : NULL);
e1e01040 8927 gdb::unique_xmalloc_ptr<char> filter_string
6c5b2ebe 8928 (lsal.canonical != NULL ? xstrdup (lsal.canonical) : NULL);
0d381245 8929
6c5b2ebe 8930 create_breakpoint_sal (gdbarch, lsal.sals,
ffc2605c 8931 std::move (location),
e1e01040
PA
8932 std::move (filter_string),
8933 std::move (cond_string),
8934 std::move (extra_string),
e7e0cddf 8935 type, disposition,
84f4c1fe 8936 thread, task, ignore_count, ops,
44f238bb 8937 from_tty, enabled, internal, flags,
56435ebe 8938 canonical->special_display);
c3f6f71d 8939 }
c3f6f71d 8940}
c906108c 8941
f00aae0f 8942/* Parse LOCATION which is assumed to be a SAL specification possibly
c3f6f71d 8943 followed by conditionals. On return, SALS contains an array of SAL
f00aae0f
KS
8944 addresses found. LOCATION points to the end of the SAL (for
8945 linespec locations).
9998af43
TJB
8946
8947 The array and the line spec strings are allocated on the heap, it is
8948 the caller's responsibility to free them. */
c906108c 8949
b9362cc7 8950static void
f00aae0f 8951parse_breakpoint_sals (const struct event_location *location,
58438ac1 8952 struct linespec_result *canonical)
c3f6f71d 8953{
f00aae0f
KS
8954 struct symtab_and_line cursal;
8955
8956 if (event_location_type (location) == LINESPEC_LOCATION)
8957 {
a20714ff 8958 const char *spec = get_linespec_location (location)->spec_string;
f00aae0f 8959
a20714ff 8960 if (spec == NULL)
f00aae0f
KS
8961 {
8962 /* The last displayed codepoint, if it's valid, is our default
8963 breakpoint address. */
8964 if (last_displayed_sal_is_valid ())
8965 {
f00aae0f
KS
8966 /* Set sal's pspace, pc, symtab, and line to the values
8967 corresponding to the last call to print_frame_info.
8968 Be sure to reinitialize LINE with NOTCURRENT == 0
8969 as the breakpoint line number is inappropriate otherwise.
8970 find_pc_line would adjust PC, re-set it back. */
51abb421
PA
8971 symtab_and_line sal = get_last_displayed_sal ();
8972 CORE_ADDR pc = sal.pc;
8973
f00aae0f
KS
8974 sal = find_pc_line (pc, 0);
8975
8976 /* "break" without arguments is equivalent to "break *PC"
8977 where PC is the last displayed codepoint's address. So
8978 make sure to set sal.explicit_pc to prevent GDB from
8979 trying to expand the list of sals to include all other
8980 instances with the same symtab and line. */
8981 sal.pc = pc;
8982 sal.explicit_pc = 1;
8983
6c5b2ebe
PA
8984 struct linespec_sals lsal;
8985 lsal.sals = {sal};
f00aae0f
KS
8986 lsal.canonical = NULL;
8987
6c5b2ebe 8988 canonical->lsals.push_back (std::move (lsal));
f00aae0f
KS
8989 return;
8990 }
8991 else
8992 error (_("No default breakpoint address now."));
c906108c 8993 }
c906108c 8994 }
f00aae0f
KS
8995
8996 /* Force almost all breakpoints to be in terms of the
8997 current_source_symtab (which is decode_line_1's default).
8998 This should produce the results we want almost all of the
8999 time while leaving default_breakpoint_* alone.
9000
9001 ObjC: However, don't match an Objective-C method name which
9002 may have a '+' or '-' succeeded by a '['. */
9003 cursal = get_current_source_symtab_and_line ();
9004 if (last_displayed_sal_is_valid ())
c906108c 9005 {
a20714ff 9006 const char *spec = NULL;
cc80f267 9007
f00aae0f 9008 if (event_location_type (location) == LINESPEC_LOCATION)
a20714ff 9009 spec = get_linespec_location (location)->spec_string;
cc80f267 9010
f00aae0f 9011 if (!cursal.symtab
a20714ff
PA
9012 || (spec != NULL
9013 && strchr ("+-", spec[0]) != NULL
9014 && spec[1] != '['))
f00aae0f 9015 {
c2f4122d 9016 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
f00aae0f
KS
9017 get_last_displayed_symtab (),
9018 get_last_displayed_line (),
9019 canonical, NULL, NULL);
9020 return;
9021 }
c906108c 9022 }
f00aae0f 9023
c2f4122d 9024 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
f00aae0f 9025 cursal.symtab, cursal.line, canonical, NULL, NULL);
c3f6f71d 9026}
c906108c 9027
c906108c 9028
c3f6f71d 9029/* Convert each SAL into a real PC. Verify that the PC can be
4a64f543 9030 inserted as a breakpoint. If it can't throw an error. */
c906108c 9031
b9362cc7 9032static void
6c5b2ebe 9033breakpoint_sals_to_pc (std::vector<symtab_and_line> &sals)
c3f6f71d 9034{
6c5b2ebe
PA
9035 for (auto &sal : sals)
9036 resolve_sal_pc (&sal);
c3f6f71d
JM
9037}
9038
7a697b8d
SS
9039/* Fast tracepoints may have restrictions on valid locations. For
9040 instance, a fast tracepoint using a jump instead of a trap will
9041 likely have to overwrite more bytes than a trap would, and so can
9042 only be placed where the instruction is longer than the jump, or a
9043 multi-instruction sequence does not have a jump into the middle of
9044 it, etc. */
9045
9046static void
9047check_fast_tracepoint_sals (struct gdbarch *gdbarch,
6c5b2ebe 9048 gdb::array_view<const symtab_and_line> sals)
7a697b8d 9049{
6c5b2ebe 9050 for (const auto &sal : sals)
7a697b8d 9051 {
f8eba3c6
TT
9052 struct gdbarch *sarch;
9053
6c5b2ebe 9054 sarch = get_sal_arch (sal);
f8eba3c6
TT
9055 /* We fall back to GDBARCH if there is no architecture
9056 associated with SAL. */
9057 if (sarch == NULL)
9058 sarch = gdbarch;
281d762b
TT
9059 std::string msg;
9060 if (!gdbarch_fast_tracepoint_valid_at (sarch, sal.pc, &msg))
53c3572a 9061 error (_("May not have a fast tracepoint at %s%s"),
281d762b 9062 paddress (sarch, sal.pc), msg.c_str ());
7a697b8d
SS
9063 }
9064}
9065
018d34a4
VP
9066/* Given TOK, a string specification of condition and thread, as
9067 accepted by the 'break' command, extract the condition
9068 string and thread number and set *COND_STRING and *THREAD.
4a64f543 9069 PC identifies the context at which the condition should be parsed.
018d34a4
VP
9070 If no condition is found, *COND_STRING is set to NULL.
9071 If no thread is found, *THREAD is set to -1. */
d634f2de
JB
9072
9073static void
bbc13ae3 9074find_condition_and_thread (const char *tok, CORE_ADDR pc,
e7e0cddf
SS
9075 char **cond_string, int *thread, int *task,
9076 char **rest)
018d34a4
VP
9077{
9078 *cond_string = NULL;
9079 *thread = -1;
ed1d1739
KS
9080 *task = 0;
9081 *rest = NULL;
9082
018d34a4
VP
9083 while (tok && *tok)
9084 {
bbc13ae3 9085 const char *end_tok;
018d34a4 9086 int toklen;
bbc13ae3
KS
9087 const char *cond_start = NULL;
9088 const char *cond_end = NULL;
cc59ec59 9089
f1735a53 9090 tok = skip_spaces (tok);
e7e0cddf
SS
9091
9092 if ((*tok == '"' || *tok == ',') && rest)
9093 {
9094 *rest = savestring (tok, strlen (tok));
9095 return;
9096 }
9097
f1735a53 9098 end_tok = skip_to_space (tok);
d634f2de 9099
018d34a4 9100 toklen = end_tok - tok;
d634f2de 9101
018d34a4
VP
9102 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
9103 {
9104 tok = cond_start = end_tok + 1;
4d01a485 9105 parse_exp_1 (&tok, pc, block_for_pc (pc), 0);
018d34a4 9106 cond_end = tok;
d634f2de 9107 *cond_string = savestring (cond_start, cond_end - cond_start);
018d34a4
VP
9108 }
9109 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
9110 {
5d5658a1
PA
9111 const char *tmptok;
9112 struct thread_info *thr;
d634f2de 9113
018d34a4 9114 tok = end_tok + 1;
5d5658a1 9115 thr = parse_thread_id (tok, &tmptok);
018d34a4
VP
9116 if (tok == tmptok)
9117 error (_("Junk after thread keyword."));
5d5658a1 9118 *thread = thr->global_num;
bbc13ae3 9119 tok = tmptok;
018d34a4 9120 }
4a306c9a
JB
9121 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
9122 {
9123 char *tmptok;
9124
9125 tok = end_tok + 1;
bbc13ae3 9126 *task = strtol (tok, &tmptok, 0);
4a306c9a
JB
9127 if (tok == tmptok)
9128 error (_("Junk after task keyword."));
9129 if (!valid_task_id (*task))
b6199126 9130 error (_("Unknown task %d."), *task);
bbc13ae3 9131 tok = tmptok;
4a306c9a 9132 }
e7e0cddf
SS
9133 else if (rest)
9134 {
9135 *rest = savestring (tok, strlen (tok));
ccab2054 9136 return;
e7e0cddf 9137 }
018d34a4
VP
9138 else
9139 error (_("Junk at end of arguments."));
9140 }
9141}
9142
0fb4aa4b
PA
9143/* Decode a static tracepoint marker spec. */
9144
6c5b2ebe 9145static std::vector<symtab_and_line>
f00aae0f 9146decode_static_tracepoint_spec (const char **arg_p)
0fb4aa4b 9147{
f00aae0f
KS
9148 const char *p = &(*arg_p)[3];
9149 const char *endp;
0fb4aa4b 9150
f1735a53 9151 p = skip_spaces (p);
0fb4aa4b 9152
f1735a53 9153 endp = skip_to_space (p);
0fb4aa4b 9154
81b1e71c 9155 std::string marker_str (p, endp - p);
0fb4aa4b 9156
5d9310c4
SM
9157 std::vector<static_tracepoint_marker> markers
9158 = target_static_tracepoint_markers_by_strid (marker_str.c_str ());
9159 if (markers.empty ())
81b1e71c
TT
9160 error (_("No known static tracepoint marker named %s"),
9161 marker_str.c_str ());
0fb4aa4b 9162
6c5b2ebe 9163 std::vector<symtab_and_line> sals;
5d9310c4 9164 sals.reserve (markers.size ());
0fb4aa4b 9165
5d9310c4 9166 for (const static_tracepoint_marker &marker : markers)
0fb4aa4b 9167 {
5d9310c4
SM
9168 symtab_and_line sal = find_pc_line (marker.address, 0);
9169 sal.pc = marker.address;
6c5b2ebe 9170 sals.push_back (sal);
5d9310c4 9171 }
0fb4aa4b 9172
0fb4aa4b
PA
9173 *arg_p = endp;
9174 return sals;
9175}
9176
bac7c5cf
GB
9177/* Returns the breakpoint ops appropriate for use with with LOCATION_TYPE and
9178 according to IS_TRACEPOINT. */
9179
9180static const struct breakpoint_ops *
9181breakpoint_ops_for_event_location_type (enum event_location_type location_type,
9182 bool is_tracepoint)
9183{
9184 if (is_tracepoint)
9185 {
9186 if (location_type == PROBE_LOCATION)
9187 return &tracepoint_probe_breakpoint_ops;
9188 else
9189 return &tracepoint_breakpoint_ops;
9190 }
9191 else
9192 {
9193 if (location_type == PROBE_LOCATION)
9194 return &bkpt_probe_breakpoint_ops;
9195 else
9196 return &bkpt_breakpoint_ops;
9197 }
9198}
9199
9200/* See breakpoint.h. */
9201
9202const struct breakpoint_ops *
9203breakpoint_ops_for_event_location (const struct event_location *location,
9204 bool is_tracepoint)
9205{
9206 if (location != nullptr)
9207 return breakpoint_ops_for_event_location_type
9208 (event_location_type (location), is_tracepoint);
9209 return is_tracepoint ? &tracepoint_breakpoint_ops : &bkpt_breakpoint_ops;
9210}
9211
f00aae0f 9212/* See breakpoint.h. */
0101ce28 9213
8cdf0e15
VP
9214int
9215create_breakpoint (struct gdbarch *gdbarch,
e1e01040
PA
9216 const struct event_location *location,
9217 const char *cond_string,
9218 int thread, const char *extra_string,
f00aae0f 9219 int parse_extra,
0fb4aa4b 9220 int tempflag, enum bptype type_wanted,
8cdf0e15
VP
9221 int ignore_count,
9222 enum auto_boolean pending_break_support,
c0a91b2b 9223 const struct breakpoint_ops *ops,
44f238bb
PA
9224 int from_tty, int enabled, int internal,
9225 unsigned flags)
c3f6f71d 9226{
7efd8fc2 9227 struct linespec_result canonical;
0101ce28 9228 int pending = 0;
4a306c9a 9229 int task = 0;
86b17b60 9230 int prev_bkpt_count = breakpoint_count;
c3f6f71d 9231
348d480f
PA
9232 gdb_assert (ops != NULL);
9233
f00aae0f
KS
9234 /* If extra_string isn't useful, set it to NULL. */
9235 if (extra_string != NULL && *extra_string == '\0')
9236 extra_string = NULL;
9237
a70b8144 9238 try
b78a6381 9239 {
f00aae0f 9240 ops->create_sals_from_location (location, &canonical, type_wanted);
b78a6381 9241 }
230d2906 9242 catch (const gdb_exception_error &e)
0101ce28 9243 {
492d29ea
PA
9244 /* If caller is interested in rc value from parse, set
9245 value. */
9246 if (e.error == NOT_FOUND_ERROR)
0101ce28 9247 {
05ff989b
AC
9248 /* If pending breakpoint support is turned off, throw
9249 error. */
fa8d40ab
JJ
9250
9251 if (pending_break_support == AUTO_BOOLEAN_FALSE)
eedc3f4f 9252 throw;
723a2275
VP
9253
9254 exception_print (gdb_stderr, e);
fa8d40ab 9255
05ff989b
AC
9256 /* If pending breakpoint support is auto query and the user
9257 selects no, then simply return the error code. */
059fb39f 9258 if (pending_break_support == AUTO_BOOLEAN_AUTO
bfccc43c
YQ
9259 && !nquery (_("Make %s pending on future shared library load? "),
9260 bptype_string (type_wanted)))
fd9b8c24 9261 return 0;
fa8d40ab 9262
05ff989b
AC
9263 /* At this point, either the user was queried about setting
9264 a pending breakpoint and selected yes, or pending
9265 breakpoint behavior is on and thus a pending breakpoint
9266 is defaulted on behalf of the user. */
f00aae0f 9267 pending = 1;
0101ce28 9268 }
492d29ea 9269 else
eedc3f4f 9270 throw;
0101ce28 9271 }
492d29ea 9272
6c5b2ebe 9273 if (!pending && canonical.lsals.empty ())
492d29ea 9274 return 0;
c3f6f71d 9275
c3f6f71d
JM
9276 /* Resolve all line numbers to PC's and verify that the addresses
9277 are ok for the target. */
0101ce28 9278 if (!pending)
f8eba3c6 9279 {
6c5b2ebe
PA
9280 for (auto &lsal : canonical.lsals)
9281 breakpoint_sals_to_pc (lsal.sals);
f8eba3c6 9282 }
c3f6f71d 9283
7a697b8d 9284 /* Fast tracepoints may have additional restrictions on location. */
bfccc43c 9285 if (!pending && type_wanted == bp_fast_tracepoint)
f8eba3c6 9286 {
6c5b2ebe
PA
9287 for (const auto &lsal : canonical.lsals)
9288 check_fast_tracepoint_sals (gdbarch, lsal.sals);
f8eba3c6 9289 }
7a697b8d 9290
c3f6f71d
JM
9291 /* Verify that condition can be parsed, before setting any
9292 breakpoints. Allocate a separate condition expression for each
4a64f543 9293 breakpoint. */
0101ce28 9294 if (!pending)
c3f6f71d 9295 {
e1e01040
PA
9296 gdb::unique_xmalloc_ptr<char> cond_string_copy;
9297 gdb::unique_xmalloc_ptr<char> extra_string_copy;
9298
f00aae0f 9299 if (parse_extra)
72b2ff0e 9300 {
0878d0fa 9301 char *rest;
e1e01040 9302 char *cond;
52d361e1 9303
6c5b2ebe 9304 const linespec_sals &lsal = canonical.lsals[0];
52d361e1 9305
0878d0fa
YQ
9306 /* Here we only parse 'arg' to separate condition
9307 from thread number, so parsing in context of first
9308 sal is OK. When setting the breakpoint we'll
9309 re-parse it in context of each sal. */
9310
6c5b2ebe 9311 find_condition_and_thread (extra_string, lsal.sals[0].pc,
e1e01040
PA
9312 &cond, &thread, &task, &rest);
9313 cond_string_copy.reset (cond);
9314 extra_string_copy.reset (rest);
72b2ff0e 9315 }
2f069f6f 9316 else
72b2ff0e 9317 {
f00aae0f
KS
9318 if (type_wanted != bp_dprintf
9319 && extra_string != NULL && *extra_string != '\0')
9320 error (_("Garbage '%s' at end of location"), extra_string);
0878d0fa
YQ
9321
9322 /* Create a private copy of condition string. */
9323 if (cond_string)
e1e01040 9324 cond_string_copy.reset (xstrdup (cond_string));
0878d0fa
YQ
9325 /* Create a private copy of any extra string. */
9326 if (extra_string)
e1e01040 9327 extra_string_copy.reset (xstrdup (extra_string));
72b2ff0e 9328 }
0fb4aa4b 9329
52d361e1 9330 ops->create_breakpoints_sal (gdbarch, &canonical,
e1e01040
PA
9331 std::move (cond_string_copy),
9332 std::move (extra_string_copy),
9333 type_wanted,
d9b3f62e
PA
9334 tempflag ? disp_del : disp_donttouch,
9335 thread, task, ignore_count, ops,
44f238bb 9336 from_tty, enabled, internal, flags);
c906108c 9337 }
0101ce28
JJ
9338 else
9339 {
a5e364af 9340 std::unique_ptr <breakpoint> b = new_breakpoint_from_type (type_wanted);
bfccc43c 9341
a5e364af 9342 init_raw_breakpoint_without_location (b.get (), gdbarch, type_wanted, ops);
d28cd78a 9343 b->location = copy_event_location (location);
bfccc43c 9344
f00aae0f
KS
9345 if (parse_extra)
9346 b->cond_string = NULL;
e12c7713
MK
9347 else
9348 {
9349 /* Create a private copy of condition string. */
e1e01040 9350 b->cond_string = cond_string != NULL ? xstrdup (cond_string) : NULL;
15630549 9351 b->thread = thread;
e12c7713 9352 }
f00aae0f
KS
9353
9354 /* Create a private copy of any extra string. */
e1e01040 9355 b->extra_string = extra_string != NULL ? xstrdup (extra_string) : NULL;
0101ce28 9356 b->ignore_count = ignore_count;
0101ce28 9357 b->disposition = tempflag ? disp_del : disp_donttouch;
0d381245 9358 b->condition_not_parsed = 1;
41447f92 9359 b->enable_state = enabled ? bp_enabled : bp_disabled;
cc72b2a2
KP
9360 if ((type_wanted != bp_breakpoint
9361 && type_wanted != bp_hardware_breakpoint) || thread != -1)
f8eba3c6 9362 b->pspace = current_program_space;
8bea4e01 9363
b270e6f9 9364 install_breakpoint (internal, std::move (b), 0);
0101ce28
JJ
9365 }
9366
6c5b2ebe 9367 if (canonical.lsals.size () > 1)
95a42b64 9368 {
3e43a32a
MS
9369 warning (_("Multiple breakpoints were set.\nUse the "
9370 "\"delete\" command to delete unwanted breakpoints."));
86b17b60 9371 prev_breakpoint_count = prev_bkpt_count;
95a42b64
TT
9372 }
9373
44702360 9374 update_global_location_list (UGLL_MAY_INSERT);
fd9b8c24
PA
9375
9376 return 1;
c3f6f71d 9377}
c906108c 9378
348d480f 9379/* Set a breakpoint.
72b2ff0e
VP
9380 ARG is a string describing breakpoint address,
9381 condition, and thread.
9382 FLAG specifies if a breakpoint is hardware on,
9383 and if breakpoint is temporary, using BP_HARDWARE_FLAG
9384 and BP_TEMPFLAG. */
348d480f 9385
98deb0da 9386static void
f2fc3015 9387break_command_1 (const char *arg, int flag, int from_tty)
c3f6f71d 9388{
72b2ff0e 9389 int tempflag = flag & BP_TEMPFLAG;
0fb4aa4b
PA
9390 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
9391 ? bp_hardware_breakpoint
9392 : bp_breakpoint);
f00aae0f 9393
ffc2605c 9394 event_location_up location = string_to_event_location (&arg, current_language);
bac7c5cf
GB
9395 const struct breakpoint_ops *ops = breakpoint_ops_for_event_location
9396 (location.get (), false /* is_tracepoint */);
c3f6f71d 9397
8cdf0e15 9398 create_breakpoint (get_current_arch (),
ffc2605c 9399 location.get (),
f00aae0f 9400 NULL, 0, arg, 1 /* parse arg */,
0fb4aa4b 9401 tempflag, type_wanted,
8cdf0e15
VP
9402 0 /* Ignore count */,
9403 pending_break_support,
55aa24fb 9404 ops,
8cdf0e15 9405 from_tty,
84f4c1fe 9406 1 /* enabled */,
44f238bb
PA
9407 0 /* internal */,
9408 0);
c906108c
SS
9409}
9410
c906108c
SS
9411/* Helper function for break_command_1 and disassemble_command. */
9412
9413void
fba45db2 9414resolve_sal_pc (struct symtab_and_line *sal)
c906108c
SS
9415{
9416 CORE_ADDR pc;
9417
9418 if (sal->pc == 0 && sal->symtab != NULL)
9419 {
9420 if (!find_line_pc (sal->symtab, sal->line, &pc))
8a3fe4f8 9421 error (_("No line %d in file \"%s\"."),
05cba821 9422 sal->line, symtab_to_filename_for_display (sal->symtab));
c906108c 9423 sal->pc = pc;
6a048695 9424
4a64f543
MS
9425 /* If this SAL corresponds to a breakpoint inserted using a line
9426 number, then skip the function prologue if necessary. */
6a048695 9427 if (sal->explicit_line)
059acae7 9428 skip_prologue_sal (sal);
c906108c
SS
9429 }
9430
9431 if (sal->section == 0 && sal->symtab != NULL)
9432 {
346d1dfe 9433 const struct blockvector *bv;
3977b71f 9434 const struct block *b;
c5aa993b 9435 struct symbol *sym;
c906108c 9436
43f3e411
DE
9437 bv = blockvector_for_pc_sect (sal->pc, 0, &b,
9438 SYMTAB_COMPUNIT (sal->symtab));
c906108c
SS
9439 if (bv != NULL)
9440 {
7f0df278 9441 sym = block_linkage_function (b);
c906108c
SS
9442 if (sym != NULL)
9443 {
eb822aa6
DE
9444 fixup_symbol_section (sym, SYMTAB_OBJFILE (sal->symtab));
9445 sal->section = SYMBOL_OBJ_SECTION (SYMTAB_OBJFILE (sal->symtab),
9446 sym);
c906108c
SS
9447 }
9448 else
9449 {
4a64f543
MS
9450 /* It really is worthwhile to have the section, so we'll
9451 just have to look harder. This case can be executed
9452 if we have line numbers but no functions (as can
9453 happen in assembly source). */
c906108c 9454
5ed8105e 9455 scoped_restore_current_pspace_and_thread restore_pspace_thread;
6c95b8df 9456 switch_to_program_space_and_thread (sal->pspace);
c906108c 9457
5ed8105e 9458 bound_minimal_symbol msym = lookup_minimal_symbol_by_pc (sal->pc);
7cbd4a93 9459 if (msym.minsym)
efd66ac6 9460 sal->section = MSYMBOL_OBJ_SECTION (msym.objfile, msym.minsym);
c906108c
SS
9461 }
9462 }
9463 }
9464}
9465
9466void
0b39b52e 9467break_command (const char *arg, int from_tty)
c906108c 9468{
db107f19 9469 break_command_1 (arg, 0, from_tty);
c906108c
SS
9470}
9471
c906108c 9472void
0b39b52e 9473tbreak_command (const char *arg, int from_tty)
c906108c 9474{
db107f19 9475 break_command_1 (arg, BP_TEMPFLAG, from_tty);
c906108c
SS
9476}
9477
c906108c 9478static void
0b39b52e 9479hbreak_command (const char *arg, int from_tty)
c906108c 9480{
db107f19 9481 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
c906108c
SS
9482}
9483
9484static void
0b39b52e 9485thbreak_command (const char *arg, int from_tty)
c906108c 9486{
db107f19 9487 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
c906108c
SS
9488}
9489
9490static void
ee7ddd71 9491stop_command (const char *arg, int from_tty)
c906108c 9492{
a3f17187 9493 printf_filtered (_("Specify the type of breakpoint to set.\n\
c906108c 9494Usage: stop in <function | address>\n\
a3f17187 9495 stop at <line>\n"));
c906108c
SS
9496}
9497
9498static void
4495129a 9499stopin_command (const char *arg, int from_tty)
c906108c
SS
9500{
9501 int badInput = 0;
9502
cafb3438 9503 if (arg == NULL)
c906108c
SS
9504 badInput = 1;
9505 else if (*arg != '*')
9506 {
4495129a 9507 const char *argptr = arg;
c906108c
SS
9508 int hasColon = 0;
9509
4a64f543 9510 /* Look for a ':'. If this is a line number specification, then
53a5351d 9511 say it is bad, otherwise, it should be an address or
4a64f543 9512 function/method name. */
c906108c 9513 while (*argptr && !hasColon)
c5aa993b
JM
9514 {
9515 hasColon = (*argptr == ':');
9516 argptr++;
9517 }
c906108c
SS
9518
9519 if (hasColon)
c5aa993b 9520 badInput = (*argptr != ':'); /* Not a class::method */
c906108c 9521 else
c5aa993b 9522 badInput = isdigit (*arg); /* a simple line number */
c906108c
SS
9523 }
9524
9525 if (badInput)
a3f17187 9526 printf_filtered (_("Usage: stop in <function | address>\n"));
c906108c 9527 else
db107f19 9528 break_command_1 (arg, 0, from_tty);
c906108c
SS
9529}
9530
9531static void
4495129a 9532stopat_command (const char *arg, int from_tty)
c906108c
SS
9533{
9534 int badInput = 0;
9535
cafb3438 9536 if (arg == NULL || *arg == '*') /* no line number */
c906108c
SS
9537 badInput = 1;
9538 else
9539 {
4495129a 9540 const char *argptr = arg;
c906108c
SS
9541 int hasColon = 0;
9542
4a64f543
MS
9543 /* Look for a ':'. If there is a '::' then get out, otherwise
9544 it is probably a line number. */
c906108c 9545 while (*argptr && !hasColon)
c5aa993b
JM
9546 {
9547 hasColon = (*argptr == ':');
9548 argptr++;
9549 }
c906108c
SS
9550
9551 if (hasColon)
c5aa993b 9552 badInput = (*argptr == ':'); /* we have class::method */
c906108c 9553 else
c5aa993b 9554 badInput = !isdigit (*arg); /* not a line number */
c906108c
SS
9555 }
9556
9557 if (badInput)
65e65158 9558 printf_filtered (_("Usage: stop at LINE\n"));
c906108c 9559 else
db107f19 9560 break_command_1 (arg, 0, from_tty);
c906108c
SS
9561}
9562
e7e0cddf
SS
9563/* The dynamic printf command is mostly like a regular breakpoint, but
9564 with a prewired command list consisting of a single output command,
9565 built from extra arguments supplied on the dprintf command
9566 line. */
9567
da821c7b 9568static void
0b39b52e 9569dprintf_command (const char *arg, int from_tty)
e7e0cddf 9570{
ffc2605c 9571 event_location_up location = string_to_event_location (&arg, current_language);
f00aae0f
KS
9572
9573 /* If non-NULL, ARG should have been advanced past the location;
9574 the next character must be ','. */
9575 if (arg != NULL)
9576 {
9577 if (arg[0] != ',' || arg[1] == '\0')
9578 error (_("Format string required"));
9579 else
9580 {
9581 /* Skip the comma. */
9582 ++arg;
9583 }
9584 }
9585
e7e0cddf 9586 create_breakpoint (get_current_arch (),
ffc2605c 9587 location.get (),
f00aae0f 9588 NULL, 0, arg, 1 /* parse arg */,
e7e0cddf
SS
9589 0, bp_dprintf,
9590 0 /* Ignore count */,
9591 pending_break_support,
9592 &dprintf_breakpoint_ops,
9593 from_tty,
9594 1 /* enabled */,
9595 0 /* internal */,
9596 0);
9597}
9598
d3ce09f5 9599static void
0b39b52e 9600agent_printf_command (const char *arg, int from_tty)
d3ce09f5
SS
9601{
9602 error (_("May only run agent-printf on the target"));
9603}
9604
f1310107
TJB
9605/* Implement the "breakpoint_hit" breakpoint_ops method for
9606 ranged breakpoints. */
9607
9608static int
9609breakpoint_hit_ranged_breakpoint (const struct bp_location *bl,
bd522513 9610 const address_space *aspace,
09ac7c10
TT
9611 CORE_ADDR bp_addr,
9612 const struct target_waitstatus *ws)
f1310107 9613{
09ac7c10 9614 if (ws->kind != TARGET_WAITKIND_STOPPED
a493e3e2 9615 || ws->value.sig != GDB_SIGNAL_TRAP)
09ac7c10
TT
9616 return 0;
9617
f1310107
TJB
9618 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
9619 bl->length, aspace, bp_addr);
9620}
9621
9622/* Implement the "resources_needed" breakpoint_ops method for
9623 ranged breakpoints. */
9624
9625static int
9626resources_needed_ranged_breakpoint (const struct bp_location *bl)
9627{
9628 return target_ranged_break_num_registers ();
9629}
9630
9631/* Implement the "print_it" breakpoint_ops method for
9632 ranged breakpoints. */
9633
9634static enum print_stop_action
348d480f 9635print_it_ranged_breakpoint (bpstat bs)
f1310107 9636{
348d480f 9637 struct breakpoint *b = bs->breakpoint_at;
f1310107 9638 struct bp_location *bl = b->loc;
79a45e25 9639 struct ui_out *uiout = current_uiout;
f1310107
TJB
9640
9641 gdb_assert (b->type == bp_hardware_breakpoint);
9642
9643 /* Ranged breakpoints have only one location. */
9644 gdb_assert (bl && bl->next == NULL);
9645
9646 annotate_breakpoint (b->number);
f303dbd6
PA
9647
9648 maybe_print_thread_hit_breakpoint (uiout);
9649
f1310107 9650 if (b->disposition == disp_del)
112e8700 9651 uiout->text ("Temporary ranged breakpoint ");
f1310107 9652 else
112e8700
SM
9653 uiout->text ("Ranged breakpoint ");
9654 if (uiout->is_mi_like_p ())
f1310107 9655 {
112e8700 9656 uiout->field_string ("reason",
f1310107 9657 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
112e8700 9658 uiout->field_string ("disp", bpdisp_text (b->disposition));
f1310107 9659 }
381befee 9660 uiout->field_signed ("bkptno", b->number);
112e8700 9661 uiout->text (", ");
f1310107
TJB
9662
9663 return PRINT_SRC_AND_LOC;
9664}
9665
9666/* Implement the "print_one" breakpoint_ops method for
9667 ranged breakpoints. */
9668
9669static void
9670print_one_ranged_breakpoint (struct breakpoint *b,
9671 struct bp_location **last_loc)
9672{
9673 struct bp_location *bl = b->loc;
9674 struct value_print_options opts;
79a45e25 9675 struct ui_out *uiout = current_uiout;
f1310107
TJB
9676
9677 /* Ranged breakpoints have only one location. */
9678 gdb_assert (bl && bl->next == NULL);
9679
9680 get_user_print_options (&opts);
9681
9682 if (opts.addressprint)
9683 /* We don't print the address range here, it will be printed later
9684 by print_one_detail_ranged_breakpoint. */
112e8700 9685 uiout->field_skip ("addr");
f1310107
TJB
9686 annotate_field (5);
9687 print_breakpoint_location (b, bl);
9688 *last_loc = bl;
9689}
9690
9691/* Implement the "print_one_detail" breakpoint_ops method for
9692 ranged breakpoints. */
9693
9694static void
9695print_one_detail_ranged_breakpoint (const struct breakpoint *b,
9696 struct ui_out *uiout)
9697{
9698 CORE_ADDR address_start, address_end;
9699 struct bp_location *bl = b->loc;
d7e74731 9700 string_file stb;
f1310107
TJB
9701
9702 gdb_assert (bl);
9703
9704 address_start = bl->address;
9705 address_end = address_start + bl->length - 1;
9706
112e8700 9707 uiout->text ("\taddress range: ");
d7e74731
PA
9708 stb.printf ("[%s, %s]",
9709 print_core_address (bl->gdbarch, address_start),
9710 print_core_address (bl->gdbarch, address_end));
112e8700
SM
9711 uiout->field_stream ("addr", stb);
9712 uiout->text ("\n");
f1310107
TJB
9713}
9714
9715/* Implement the "print_mention" breakpoint_ops method for
9716 ranged breakpoints. */
9717
9718static void
9719print_mention_ranged_breakpoint (struct breakpoint *b)
9720{
9721 struct bp_location *bl = b->loc;
79a45e25 9722 struct ui_out *uiout = current_uiout;
f1310107
TJB
9723
9724 gdb_assert (bl);
9725 gdb_assert (b->type == bp_hardware_breakpoint);
9726
2d33446d
TT
9727 uiout->message (_("Hardware assisted ranged breakpoint %d from %s to %s."),
9728 b->number, paddress (bl->gdbarch, bl->address),
9729 paddress (bl->gdbarch, bl->address + bl->length - 1));
f1310107
TJB
9730}
9731
9732/* Implement the "print_recreate" breakpoint_ops method for
9733 ranged breakpoints. */
9734
9735static void
9736print_recreate_ranged_breakpoint (struct breakpoint *b, struct ui_file *fp)
9737{
f00aae0f 9738 fprintf_unfiltered (fp, "break-range %s, %s",
d28cd78a
TT
9739 event_location_to_string (b->location.get ()),
9740 event_location_to_string (b->location_range_end.get ()));
d9b3f62e 9741 print_recreate_thread (b, fp);
f1310107
TJB
9742}
9743
9744/* The breakpoint_ops structure to be used in ranged breakpoints. */
9745
2060206e 9746static struct breakpoint_ops ranged_breakpoint_ops;
f1310107
TJB
9747
9748/* Find the address where the end of the breakpoint range should be
9749 placed, given the SAL of the end of the range. This is so that if
9750 the user provides a line number, the end of the range is set to the
9751 last instruction of the given line. */
9752
9753static CORE_ADDR
9754find_breakpoint_range_end (struct symtab_and_line sal)
9755{
9756 CORE_ADDR end;
9757
9758 /* If the user provided a PC value, use it. Otherwise,
9759 find the address of the end of the given location. */
9760 if (sal.explicit_pc)
9761 end = sal.pc;
9762 else
9763 {
9764 int ret;
9765 CORE_ADDR start;
9766
9767 ret = find_line_pc_range (sal, &start, &end);
9768 if (!ret)
9769 error (_("Could not find location of the end of the range."));
9770
9771 /* find_line_pc_range returns the start of the next line. */
9772 end--;
9773 }
9774
9775 return end;
9776}
9777
9778/* Implement the "break-range" CLI command. */
9779
9780static void
0b39b52e 9781break_range_command (const char *arg, int from_tty)
f1310107 9782{
f2fc3015 9783 const char *arg_start;
f1310107
TJB
9784 struct linespec_result canonical_start, canonical_end;
9785 int bp_count, can_use_bp, length;
9786 CORE_ADDR end;
9787 struct breakpoint *b;
f1310107
TJB
9788
9789 /* We don't support software ranged breakpoints. */
9790 if (target_ranged_break_num_registers () < 0)
9791 error (_("This target does not support hardware ranged breakpoints."));
9792
9793 bp_count = hw_breakpoint_used_count ();
9794 bp_count += target_ranged_break_num_registers ();
9795 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
9796 bp_count, 0);
9797 if (can_use_bp < 0)
9798 error (_("Hardware breakpoints used exceeds limit."));
9799
f8eba3c6 9800 arg = skip_spaces (arg);
f1310107
TJB
9801 if (arg == NULL || arg[0] == '\0')
9802 error(_("No address range specified."));
9803
f8eba3c6 9804 arg_start = arg;
ffc2605c
TT
9805 event_location_up start_location = string_to_event_location (&arg,
9806 current_language);
9807 parse_breakpoint_sals (start_location.get (), &canonical_start);
f1310107
TJB
9808
9809 if (arg[0] != ',')
9810 error (_("Too few arguments."));
6c5b2ebe 9811 else if (canonical_start.lsals.empty ())
f1310107 9812 error (_("Could not find location of the beginning of the range."));
f8eba3c6 9813
6c5b2ebe 9814 const linespec_sals &lsal_start = canonical_start.lsals[0];
f8eba3c6 9815
6c5b2ebe
PA
9816 if (canonical_start.lsals.size () > 1
9817 || lsal_start.sals.size () != 1)
f1310107
TJB
9818 error (_("Cannot create a ranged breakpoint with multiple locations."));
9819
6c5b2ebe 9820 const symtab_and_line &sal_start = lsal_start.sals[0];
81b1e71c 9821 std::string addr_string_start (arg_start, arg - arg_start);
f1310107
TJB
9822
9823 arg++; /* Skip the comma. */
f8eba3c6 9824 arg = skip_spaces (arg);
f1310107
TJB
9825
9826 /* Parse the end location. */
9827
f1310107
TJB
9828 arg_start = arg;
9829
f8eba3c6 9830 /* We call decode_line_full directly here instead of using
f1310107
TJB
9831 parse_breakpoint_sals because we need to specify the start location's
9832 symtab and line as the default symtab and line for the end of the
9833 range. This makes it possible to have ranges like "foo.c:27, +14",
9834 where +14 means 14 lines from the start location. */
ffc2605c
TT
9835 event_location_up end_location = string_to_event_location (&arg,
9836 current_language);
9837 decode_line_full (end_location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
f8eba3c6
TT
9838 sal_start.symtab, sal_start.line,
9839 &canonical_end, NULL, NULL);
9840
6c5b2ebe 9841 if (canonical_end.lsals.empty ())
f1310107 9842 error (_("Could not find location of the end of the range."));
f8eba3c6 9843
6c5b2ebe
PA
9844 const linespec_sals &lsal_end = canonical_end.lsals[0];
9845 if (canonical_end.lsals.size () > 1
9846 || lsal_end.sals.size () != 1)
f1310107
TJB
9847 error (_("Cannot create a ranged breakpoint with multiple locations."));
9848
6c5b2ebe 9849 const symtab_and_line &sal_end = lsal_end.sals[0];
f1310107
TJB
9850
9851 end = find_breakpoint_range_end (sal_end);
9852 if (sal_start.pc > end)
177b42fe 9853 error (_("Invalid address range, end precedes start."));
f1310107
TJB
9854
9855 length = end - sal_start.pc + 1;
9856 if (length < 0)
9857 /* Length overflowed. */
9858 error (_("Address range too large."));
9859 else if (length == 1)
9860 {
9861 /* This range is simple enough to be handled by
9862 the `hbreak' command. */
81b1e71c 9863 hbreak_command (&addr_string_start[0], 1);
f1310107
TJB
9864
9865 return;
9866 }
9867
9868 /* Now set up the breakpoint. */
9869 b = set_raw_breakpoint (get_current_arch (), sal_start,
348d480f 9870 bp_hardware_breakpoint, &ranged_breakpoint_ops);
f1310107
TJB
9871 set_breakpoint_count (breakpoint_count + 1);
9872 b->number = breakpoint_count;
9873 b->disposition = disp_donttouch;
d28cd78a
TT
9874 b->location = std::move (start_location);
9875 b->location_range_end = std::move (end_location);
f1310107
TJB
9876 b->loc->length = length;
9877
f1310107 9878 mention (b);
76727919 9879 gdb::observers::breakpoint_created.notify (b);
44702360 9880 update_global_location_list (UGLL_MAY_INSERT);
f1310107
TJB
9881}
9882
4a64f543
MS
9883/* Return non-zero if EXP is verified as constant. Returned zero
9884 means EXP is variable. Also the constant detection may fail for
9885 some constant expressions and in such case still falsely return
9886 zero. */
2e6e3d9c 9887
65d79d4b
SDJ
9888static int
9889watchpoint_exp_is_const (const struct expression *exp)
9890{
9891 int i = exp->nelts;
9892
9893 while (i > 0)
9894 {
9895 int oplenp, argsp;
9896
9897 /* We are only interested in the descriptor of each element. */
9898 operator_length (exp, i, &oplenp, &argsp);
9899 i -= oplenp;
9900
9901 switch (exp->elts[i].opcode)
9902 {
9903 case BINOP_ADD:
9904 case BINOP_SUB:
9905 case BINOP_MUL:
9906 case BINOP_DIV:
9907 case BINOP_REM:
9908 case BINOP_MOD:
9909 case BINOP_LSH:
9910 case BINOP_RSH:
9911 case BINOP_LOGICAL_AND:
9912 case BINOP_LOGICAL_OR:
9913 case BINOP_BITWISE_AND:
9914 case BINOP_BITWISE_IOR:
9915 case BINOP_BITWISE_XOR:
9916 case BINOP_EQUAL:
9917 case BINOP_NOTEQUAL:
9918 case BINOP_LESS:
9919 case BINOP_GTR:
9920 case BINOP_LEQ:
9921 case BINOP_GEQ:
9922 case BINOP_REPEAT:
9923 case BINOP_COMMA:
9924 case BINOP_EXP:
9925 case BINOP_MIN:
9926 case BINOP_MAX:
9927 case BINOP_INTDIV:
9928 case BINOP_CONCAT:
65d79d4b
SDJ
9929 case TERNOP_COND:
9930 case TERNOP_SLICE:
65d79d4b
SDJ
9931
9932 case OP_LONG:
edd079d9 9933 case OP_FLOAT:
65d79d4b
SDJ
9934 case OP_LAST:
9935 case OP_COMPLEX:
9936 case OP_STRING:
65d79d4b
SDJ
9937 case OP_ARRAY:
9938 case OP_TYPE:
608b4967
TT
9939 case OP_TYPEOF:
9940 case OP_DECLTYPE:
6e72ca20 9941 case OP_TYPEID:
65d79d4b
SDJ
9942 case OP_NAME:
9943 case OP_OBJC_NSSTRING:
9944
9945 case UNOP_NEG:
9946 case UNOP_LOGICAL_NOT:
9947 case UNOP_COMPLEMENT:
9948 case UNOP_ADDR:
9949 case UNOP_HIGH:
aeaa2474 9950 case UNOP_CAST:
9eaf6705
TT
9951
9952 case UNOP_CAST_TYPE:
9953 case UNOP_REINTERPRET_CAST:
9954 case UNOP_DYNAMIC_CAST:
4a64f543
MS
9955 /* Unary, binary and ternary operators: We have to check
9956 their operands. If they are constant, then so is the
9957 result of that operation. For instance, if A and B are
9958 determined to be constants, then so is "A + B".
9959
9960 UNOP_IND is one exception to the rule above, because the
9961 value of *ADDR is not necessarily a constant, even when
9962 ADDR is. */
65d79d4b
SDJ
9963 break;
9964
9965 case OP_VAR_VALUE:
9966 /* Check whether the associated symbol is a constant.
4a64f543 9967
65d79d4b 9968 We use SYMBOL_CLASS rather than TYPE_CONST because it's
4a64f543
MS
9969 possible that a buggy compiler could mark a variable as
9970 constant even when it is not, and TYPE_CONST would return
9971 true in this case, while SYMBOL_CLASS wouldn't.
9972
9973 We also have to check for function symbols because they
9974 are always constant. */
65d79d4b
SDJ
9975 {
9976 struct symbol *s = exp->elts[i + 2].symbol;
9977
9978 if (SYMBOL_CLASS (s) != LOC_BLOCK
9979 && SYMBOL_CLASS (s) != LOC_CONST
9980 && SYMBOL_CLASS (s) != LOC_CONST_BYTES)
9981 return 0;
9982 break;
9983 }
9984
9985 /* The default action is to return 0 because we are using
9986 the optimistic approach here: If we don't know something,
9987 then it is not a constant. */
9988 default:
9989 return 0;
9990 }
9991 }
9992
9993 return 1;
9994}
9995
c1fc2657 9996/* Watchpoint destructor. */
3a5c3e22 9997
c1fc2657 9998watchpoint::~watchpoint ()
3a5c3e22 9999{
c1fc2657
SM
10000 xfree (this->exp_string);
10001 xfree (this->exp_string_reparse);
3a5c3e22
PA
10002}
10003
348d480f
PA
10004/* Implement the "re_set" breakpoint_ops method for watchpoints. */
10005
10006static void
10007re_set_watchpoint (struct breakpoint *b)
10008{
3a5c3e22
PA
10009 struct watchpoint *w = (struct watchpoint *) b;
10010
348d480f
PA
10011 /* Watchpoint can be either on expression using entirely global
10012 variables, or it can be on local variables.
10013
10014 Watchpoints of the first kind are never auto-deleted, and even
10015 persist across program restarts. Since they can use variables
10016 from shared libraries, we need to reparse expression as libraries
10017 are loaded and unloaded.
10018
10019 Watchpoints on local variables can also change meaning as result
10020 of solib event. For example, if a watchpoint uses both a local
10021 and a global variables in expression, it's a local watchpoint,
10022 but unloading of a shared library will make the expression
10023 invalid. This is not a very common use case, but we still
10024 re-evaluate expression, to avoid surprises to the user.
10025
10026 Note that for local watchpoints, we re-evaluate it only if
10027 watchpoints frame id is still valid. If it's not, it means the
10028 watchpoint is out of scope and will be deleted soon. In fact,
10029 I'm not sure we'll ever be called in this case.
10030
10031 If a local watchpoint's frame id is still valid, then
3a5c3e22 10032 w->exp_valid_block is likewise valid, and we can safely use it.
348d480f 10033
3a5c3e22
PA
10034 Don't do anything about disabled watchpoints, since they will be
10035 reevaluated again when enabled. */
10036 update_watchpoint (w, 1 /* reparse */);
348d480f
PA
10037}
10038
77b06cd7
TJB
10039/* Implement the "insert" breakpoint_ops method for hardware watchpoints. */
10040
10041static int
10042insert_watchpoint (struct bp_location *bl)
10043{
3a5c3e22
PA
10044 struct watchpoint *w = (struct watchpoint *) bl->owner;
10045 int length = w->exact ? 1 : bl->length;
e09342b5
TJB
10046
10047 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
4d01a485 10048 w->cond_exp.get ());
77b06cd7
TJB
10049}
10050
10051/* Implement the "remove" breakpoint_ops method for hardware watchpoints. */
10052
10053static int
73971819 10054remove_watchpoint (struct bp_location *bl, enum remove_bp_reason reason)
77b06cd7 10055{
3a5c3e22
PA
10056 struct watchpoint *w = (struct watchpoint *) bl->owner;
10057 int length = w->exact ? 1 : bl->length;
e09342b5
TJB
10058
10059 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
4d01a485 10060 w->cond_exp.get ());
e09342b5
TJB
10061}
10062
e09342b5 10063static int
348d480f 10064breakpoint_hit_watchpoint (const struct bp_location *bl,
bd522513 10065 const address_space *aspace, CORE_ADDR bp_addr,
09ac7c10 10066 const struct target_waitstatus *ws)
e09342b5 10067{
348d480f 10068 struct breakpoint *b = bl->owner;
3a5c3e22 10069 struct watchpoint *w = (struct watchpoint *) b;
77b06cd7 10070
348d480f
PA
10071 /* Continuable hardware watchpoints are treated as non-existent if the
10072 reason we stopped wasn't a hardware watchpoint (we didn't stop on
10073 some data address). Otherwise gdb won't stop on a break instruction
10074 in the code (not from a breakpoint) when a hardware watchpoint has
10075 been defined. Also skip watchpoints which we know did not trigger
10076 (did not match the data address). */
10077 if (is_hardware_watchpoint (b)
3a5c3e22 10078 && w->watchpoint_triggered == watch_triggered_no)
348d480f 10079 return 0;
9c06b0b4 10080
348d480f 10081 return 1;
9c06b0b4
TJB
10082}
10083
348d480f
PA
10084static void
10085check_status_watchpoint (bpstat bs)
9c06b0b4 10086{
348d480f 10087 gdb_assert (is_watchpoint (bs->breakpoint_at));
9c06b0b4 10088
348d480f 10089 bpstat_check_watchpoint (bs);
9c06b0b4
TJB
10090}
10091
10092/* Implement the "resources_needed" breakpoint_ops method for
348d480f 10093 hardware watchpoints. */
9c06b0b4
TJB
10094
10095static int
348d480f 10096resources_needed_watchpoint (const struct bp_location *bl)
9c06b0b4 10097{
3a5c3e22
PA
10098 struct watchpoint *w = (struct watchpoint *) bl->owner;
10099 int length = w->exact? 1 : bl->length;
348d480f
PA
10100
10101 return target_region_ok_for_hw_watchpoint (bl->address, length);
9c06b0b4
TJB
10102}
10103
10104/* Implement the "works_in_software_mode" breakpoint_ops method for
348d480f 10105 hardware watchpoints. */
9c06b0b4
TJB
10106
10107static int
348d480f 10108works_in_software_mode_watchpoint (const struct breakpoint *b)
9c06b0b4 10109{
efa80663
PA
10110 /* Read and access watchpoints only work with hardware support. */
10111 return b->type == bp_watchpoint || b->type == bp_hardware_watchpoint;
9c06b0b4
TJB
10112}
10113
9c06b0b4 10114static enum print_stop_action
348d480f 10115print_it_watchpoint (bpstat bs)
9c06b0b4 10116{
348d480f 10117 struct breakpoint *b;
348d480f 10118 enum print_stop_action result;
3a5c3e22 10119 struct watchpoint *w;
79a45e25 10120 struct ui_out *uiout = current_uiout;
348d480f
PA
10121
10122 gdb_assert (bs->bp_location_at != NULL);
10123
348d480f 10124 b = bs->breakpoint_at;
3a5c3e22 10125 w = (struct watchpoint *) b;
348d480f 10126
f303dbd6
PA
10127 annotate_watchpoint (b->number);
10128 maybe_print_thread_hit_breakpoint (uiout);
10129
d7e74731
PA
10130 string_file stb;
10131
76f9c9cf 10132 gdb::optional<ui_out_emit_tuple> tuple_emitter;
9c06b0b4
TJB
10133 switch (b->type)
10134 {
348d480f 10135 case bp_watchpoint:
9c06b0b4 10136 case bp_hardware_watchpoint:
112e8700
SM
10137 if (uiout->is_mi_like_p ())
10138 uiout->field_string
10139 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
348d480f 10140 mention (b);
76f9c9cf 10141 tuple_emitter.emplace (uiout, "value");
112e8700 10142 uiout->text ("\nOld value = ");
850645cf 10143 watchpoint_value_print (bs->old_val.get (), &stb);
112e8700
SM
10144 uiout->field_stream ("old", stb);
10145 uiout->text ("\nNew value = ");
850645cf 10146 watchpoint_value_print (w->val.get (), &stb);
112e8700
SM
10147 uiout->field_stream ("new", stb);
10148 uiout->text ("\n");
348d480f
PA
10149 /* More than one watchpoint may have been triggered. */
10150 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10151 break;
10152
10153 case bp_read_watchpoint:
112e8700
SM
10154 if (uiout->is_mi_like_p ())
10155 uiout->field_string
10156 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
348d480f 10157 mention (b);
76f9c9cf 10158 tuple_emitter.emplace (uiout, "value");
112e8700 10159 uiout->text ("\nValue = ");
850645cf 10160 watchpoint_value_print (w->val.get (), &stb);
112e8700
SM
10161 uiout->field_stream ("value", stb);
10162 uiout->text ("\n");
348d480f 10163 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10164 break;
10165
10166 case bp_access_watchpoint:
348d480f
PA
10167 if (bs->old_val != NULL)
10168 {
112e8700
SM
10169 if (uiout->is_mi_like_p ())
10170 uiout->field_string
10171 ("reason",
348d480f
PA
10172 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10173 mention (b);
76f9c9cf 10174 tuple_emitter.emplace (uiout, "value");
112e8700 10175 uiout->text ("\nOld value = ");
850645cf 10176 watchpoint_value_print (bs->old_val.get (), &stb);
112e8700
SM
10177 uiout->field_stream ("old", stb);
10178 uiout->text ("\nNew value = ");
348d480f
PA
10179 }
10180 else
10181 {
10182 mention (b);
112e8700
SM
10183 if (uiout->is_mi_like_p ())
10184 uiout->field_string
10185 ("reason",
348d480f 10186 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
76f9c9cf 10187 tuple_emitter.emplace (uiout, "value");
112e8700 10188 uiout->text ("\nValue = ");
348d480f 10189 }
850645cf 10190 watchpoint_value_print (w->val.get (), &stb);
112e8700
SM
10191 uiout->field_stream ("new", stb);
10192 uiout->text ("\n");
348d480f 10193 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10194 break;
10195 default:
348d480f 10196 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10197 }
10198
348d480f
PA
10199 return result;
10200}
10201
10202/* Implement the "print_mention" breakpoint_ops method for hardware
10203 watchpoints. */
10204
10205static void
10206print_mention_watchpoint (struct breakpoint *b)
10207{
3a5c3e22 10208 struct watchpoint *w = (struct watchpoint *) b;
79a45e25 10209 struct ui_out *uiout = current_uiout;
46b9c129 10210 const char *tuple_name;
348d480f
PA
10211
10212 switch (b->type)
10213 {
10214 case bp_watchpoint:
112e8700 10215 uiout->text ("Watchpoint ");
46b9c129 10216 tuple_name = "wpt";
348d480f
PA
10217 break;
10218 case bp_hardware_watchpoint:
112e8700 10219 uiout->text ("Hardware watchpoint ");
46b9c129 10220 tuple_name = "wpt";
348d480f
PA
10221 break;
10222 case bp_read_watchpoint:
112e8700 10223 uiout->text ("Hardware read watchpoint ");
46b9c129 10224 tuple_name = "hw-rwpt";
348d480f
PA
10225 break;
10226 case bp_access_watchpoint:
112e8700 10227 uiout->text ("Hardware access (read/write) watchpoint ");
46b9c129 10228 tuple_name = "hw-awpt";
348d480f
PA
10229 break;
10230 default:
10231 internal_error (__FILE__, __LINE__,
10232 _("Invalid hardware watchpoint type."));
10233 }
10234
46b9c129 10235 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
381befee 10236 uiout->field_signed ("number", b->number);
112e8700
SM
10237 uiout->text (": ");
10238 uiout->field_string ("exp", w->exp_string);
348d480f
PA
10239}
10240
10241/* Implement the "print_recreate" breakpoint_ops method for
10242 watchpoints. */
10243
10244static void
10245print_recreate_watchpoint (struct breakpoint *b, struct ui_file *fp)
10246{
3a5c3e22
PA
10247 struct watchpoint *w = (struct watchpoint *) b;
10248
348d480f
PA
10249 switch (b->type)
10250 {
10251 case bp_watchpoint:
10252 case bp_hardware_watchpoint:
10253 fprintf_unfiltered (fp, "watch");
10254 break;
10255 case bp_read_watchpoint:
10256 fprintf_unfiltered (fp, "rwatch");
10257 break;
10258 case bp_access_watchpoint:
10259 fprintf_unfiltered (fp, "awatch");
10260 break;
10261 default:
10262 internal_error (__FILE__, __LINE__,
10263 _("Invalid watchpoint type."));
10264 }
10265
3a5c3e22 10266 fprintf_unfiltered (fp, " %s", w->exp_string);
d9b3f62e 10267 print_recreate_thread (b, fp);
348d480f
PA
10268}
10269
427cd150
TT
10270/* Implement the "explains_signal" breakpoint_ops method for
10271 watchpoints. */
10272
47591c29 10273static int
427cd150
TT
10274explains_signal_watchpoint (struct breakpoint *b, enum gdb_signal sig)
10275{
10276 /* A software watchpoint cannot cause a signal other than
10277 GDB_SIGNAL_TRAP. */
10278 if (b->type == bp_watchpoint && sig != GDB_SIGNAL_TRAP)
47591c29 10279 return 0;
427cd150 10280
47591c29 10281 return 1;
427cd150
TT
10282}
10283
348d480f
PA
10284/* The breakpoint_ops structure to be used in hardware watchpoints. */
10285
2060206e 10286static struct breakpoint_ops watchpoint_breakpoint_ops;
348d480f
PA
10287
10288/* Implement the "insert" breakpoint_ops method for
10289 masked hardware watchpoints. */
10290
10291static int
10292insert_masked_watchpoint (struct bp_location *bl)
10293{
3a5c3e22
PA
10294 struct watchpoint *w = (struct watchpoint *) bl->owner;
10295
10296 return target_insert_mask_watchpoint (bl->address, w->hw_wp_mask,
348d480f
PA
10297 bl->watchpoint_type);
10298}
10299
10300/* Implement the "remove" breakpoint_ops method for
10301 masked hardware watchpoints. */
10302
10303static int
73971819 10304remove_masked_watchpoint (struct bp_location *bl, enum remove_bp_reason reason)
348d480f 10305{
3a5c3e22
PA
10306 struct watchpoint *w = (struct watchpoint *) bl->owner;
10307
10308 return target_remove_mask_watchpoint (bl->address, w->hw_wp_mask,
348d480f
PA
10309 bl->watchpoint_type);
10310}
10311
10312/* Implement the "resources_needed" breakpoint_ops method for
10313 masked hardware watchpoints. */
10314
10315static int
10316resources_needed_masked_watchpoint (const struct bp_location *bl)
10317{
3a5c3e22
PA
10318 struct watchpoint *w = (struct watchpoint *) bl->owner;
10319
10320 return target_masked_watch_num_registers (bl->address, w->hw_wp_mask);
348d480f
PA
10321}
10322
10323/* Implement the "works_in_software_mode" breakpoint_ops method for
10324 masked hardware watchpoints. */
10325
10326static int
10327works_in_software_mode_masked_watchpoint (const struct breakpoint *b)
10328{
10329 return 0;
10330}
10331
10332/* Implement the "print_it" breakpoint_ops method for
10333 masked hardware watchpoints. */
10334
10335static enum print_stop_action
10336print_it_masked_watchpoint (bpstat bs)
10337{
10338 struct breakpoint *b = bs->breakpoint_at;
79a45e25 10339 struct ui_out *uiout = current_uiout;
348d480f
PA
10340
10341 /* Masked watchpoints have only one location. */
10342 gdb_assert (b->loc && b->loc->next == NULL);
10343
f303dbd6
PA
10344 annotate_watchpoint (b->number);
10345 maybe_print_thread_hit_breakpoint (uiout);
10346
348d480f
PA
10347 switch (b->type)
10348 {
10349 case bp_hardware_watchpoint:
112e8700
SM
10350 if (uiout->is_mi_like_p ())
10351 uiout->field_string
10352 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
348d480f
PA
10353 break;
10354
10355 case bp_read_watchpoint:
112e8700
SM
10356 if (uiout->is_mi_like_p ())
10357 uiout->field_string
10358 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
348d480f
PA
10359 break;
10360
10361 case bp_access_watchpoint:
112e8700
SM
10362 if (uiout->is_mi_like_p ())
10363 uiout->field_string
10364 ("reason",
348d480f
PA
10365 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10366 break;
10367 default:
10368 internal_error (__FILE__, __LINE__,
10369 _("Invalid hardware watchpoint type."));
10370 }
10371
10372 mention (b);
112e8700 10373 uiout->text (_("\n\
9c06b0b4
TJB
10374Check the underlying instruction at PC for the memory\n\
10375address and value which triggered this watchpoint.\n"));
112e8700 10376 uiout->text ("\n");
9c06b0b4
TJB
10377
10378 /* More than one watchpoint may have been triggered. */
10379 return PRINT_UNKNOWN;
10380}
10381
10382/* Implement the "print_one_detail" breakpoint_ops method for
10383 masked hardware watchpoints. */
10384
10385static void
10386print_one_detail_masked_watchpoint (const struct breakpoint *b,
10387 struct ui_out *uiout)
10388{
3a5c3e22
PA
10389 struct watchpoint *w = (struct watchpoint *) b;
10390
9c06b0b4
TJB
10391 /* Masked watchpoints have only one location. */
10392 gdb_assert (b->loc && b->loc->next == NULL);
10393
112e8700
SM
10394 uiout->text ("\tmask ");
10395 uiout->field_core_addr ("mask", b->loc->gdbarch, w->hw_wp_mask);
10396 uiout->text ("\n");
9c06b0b4
TJB
10397}
10398
10399/* Implement the "print_mention" breakpoint_ops method for
10400 masked hardware watchpoints. */
10401
10402static void
10403print_mention_masked_watchpoint (struct breakpoint *b)
10404{
3a5c3e22 10405 struct watchpoint *w = (struct watchpoint *) b;
79a45e25 10406 struct ui_out *uiout = current_uiout;
46b9c129 10407 const char *tuple_name;
9c06b0b4
TJB
10408
10409 switch (b->type)
10410 {
10411 case bp_hardware_watchpoint:
112e8700 10412 uiout->text ("Masked hardware watchpoint ");
46b9c129 10413 tuple_name = "wpt";
9c06b0b4
TJB
10414 break;
10415 case bp_read_watchpoint:
112e8700 10416 uiout->text ("Masked hardware read watchpoint ");
46b9c129 10417 tuple_name = "hw-rwpt";
9c06b0b4
TJB
10418 break;
10419 case bp_access_watchpoint:
112e8700 10420 uiout->text ("Masked hardware access (read/write) watchpoint ");
46b9c129 10421 tuple_name = "hw-awpt";
9c06b0b4
TJB
10422 break;
10423 default:
10424 internal_error (__FILE__, __LINE__,
10425 _("Invalid hardware watchpoint type."));
10426 }
10427
46b9c129 10428 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
381befee 10429 uiout->field_signed ("number", b->number);
112e8700
SM
10430 uiout->text (": ");
10431 uiout->field_string ("exp", w->exp_string);
9c06b0b4
TJB
10432}
10433
10434/* Implement the "print_recreate" breakpoint_ops method for
10435 masked hardware watchpoints. */
10436
10437static void
10438print_recreate_masked_watchpoint (struct breakpoint *b, struct ui_file *fp)
10439{
3a5c3e22 10440 struct watchpoint *w = (struct watchpoint *) b;
9c06b0b4
TJB
10441
10442 switch (b->type)
10443 {
10444 case bp_hardware_watchpoint:
10445 fprintf_unfiltered (fp, "watch");
10446 break;
10447 case bp_read_watchpoint:
10448 fprintf_unfiltered (fp, "rwatch");
10449 break;
10450 case bp_access_watchpoint:
10451 fprintf_unfiltered (fp, "awatch");
10452 break;
10453 default:
10454 internal_error (__FILE__, __LINE__,
10455 _("Invalid hardware watchpoint type."));
10456 }
10457
53807e9f
TT
10458 fprintf_unfiltered (fp, " %s mask 0x%s", w->exp_string,
10459 phex (w->hw_wp_mask, sizeof (CORE_ADDR)));
d9b3f62e 10460 print_recreate_thread (b, fp);
9c06b0b4
TJB
10461}
10462
10463/* The breakpoint_ops structure to be used in masked hardware watchpoints. */
10464
2060206e 10465static struct breakpoint_ops masked_watchpoint_breakpoint_ops;
9c06b0b4
TJB
10466
10467/* Tell whether the given watchpoint is a masked hardware watchpoint. */
10468
f2478a7e 10469static bool
9c06b0b4
TJB
10470is_masked_watchpoint (const struct breakpoint *b)
10471{
10472 return b->ops == &masked_watchpoint_breakpoint_ops;
10473}
10474
53a5351d
JM
10475/* accessflag: hw_write: watch write,
10476 hw_read: watch read,
10477 hw_access: watch access (read or write) */
c906108c 10478static void
bbc13ae3 10479watch_command_1 (const char *arg, int accessflag, int from_tty,
84f4c1fe 10480 int just_location, int internal)
c906108c 10481{
c1fc2657 10482 struct breakpoint *scope_breakpoint = NULL;
270140bd 10483 const struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
b926417a 10484 struct value *result;
bb9d5f81 10485 int saved_bitpos = 0, saved_bitsize = 0;
bbc13ae3
KS
10486 const char *exp_start = NULL;
10487 const char *exp_end = NULL;
10488 const char *tok, *end_tok;
9c06b0b4 10489 int toklen = -1;
bbc13ae3
KS
10490 const char *cond_start = NULL;
10491 const char *cond_end = NULL;
c906108c 10492 enum bptype bp_type;
37e4754d 10493 int thread = -1;
0cf6dd15 10494 int pc = 0;
9c06b0b4
TJB
10495 /* Flag to indicate whether we are going to use masks for
10496 the hardware watchpoint. */
10497 int use_mask = 0;
10498 CORE_ADDR mask = 0;
c906108c 10499
37e4754d
LM
10500 /* Make sure that we actually have parameters to parse. */
10501 if (arg != NULL && arg[0] != '\0')
10502 {
bbc13ae3
KS
10503 const char *value_start;
10504
10505 exp_end = arg + strlen (arg);
37e4754d 10506
9c06b0b4
TJB
10507 /* Look for "parameter value" pairs at the end
10508 of the arguments string. */
bbc13ae3 10509 for (tok = exp_end - 1; tok > arg; tok--)
9c06b0b4
TJB
10510 {
10511 /* Skip whitespace at the end of the argument list. */
10512 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10513 tok--;
10514
10515 /* Find the beginning of the last token.
10516 This is the value of the parameter. */
10517 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10518 tok--;
10519 value_start = tok + 1;
10520
10521 /* Skip whitespace. */
10522 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10523 tok--;
10524
10525 end_tok = tok;
10526
10527 /* Find the beginning of the second to last token.
10528 This is the parameter itself. */
10529 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10530 tok--;
10531 tok++;
10532 toklen = end_tok - tok + 1;
10533
61012eef 10534 if (toklen == 6 && startswith (tok, "thread"))
9c06b0b4 10535 {
5d5658a1 10536 struct thread_info *thr;
9c06b0b4
TJB
10537 /* At this point we've found a "thread" token, which means
10538 the user is trying to set a watchpoint that triggers
10539 only in a specific thread. */
5d5658a1 10540 const char *endp;
37e4754d 10541
9c06b0b4
TJB
10542 if (thread != -1)
10543 error(_("You can specify only one thread."));
37e4754d 10544
9c06b0b4 10545 /* Extract the thread ID from the next token. */
5d5658a1 10546 thr = parse_thread_id (value_start, &endp);
37e4754d 10547
5d5658a1 10548 /* Check if the user provided a valid thread ID. */
9c06b0b4 10549 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
5d5658a1 10550 invalid_thread_id_error (value_start);
9c06b0b4 10551
5d5658a1 10552 thread = thr->global_num;
9c06b0b4 10553 }
61012eef 10554 else if (toklen == 4 && startswith (tok, "mask"))
9c06b0b4
TJB
10555 {
10556 /* We've found a "mask" token, which means the user wants to
10557 create a hardware watchpoint that is going to have the mask
10558 facility. */
10559 struct value *mask_value, *mark;
37e4754d 10560
9c06b0b4
TJB
10561 if (use_mask)
10562 error(_("You can specify only one mask."));
37e4754d 10563
9c06b0b4 10564 use_mask = just_location = 1;
37e4754d 10565
9c06b0b4
TJB
10566 mark = value_mark ();
10567 mask_value = parse_to_comma_and_eval (&value_start);
10568 mask = value_as_address (mask_value);
10569 value_free_to_mark (mark);
10570 }
10571 else
10572 /* We didn't recognize what we found. We should stop here. */
10573 break;
37e4754d 10574
9c06b0b4
TJB
10575 /* Truncate the string and get rid of the "parameter value" pair before
10576 the arguments string is parsed by the parse_exp_1 function. */
bbc13ae3 10577 exp_end = tok;
9c06b0b4 10578 }
37e4754d 10579 }
bbc13ae3
KS
10580 else
10581 exp_end = arg;
37e4754d 10582
bbc13ae3
KS
10583 /* Parse the rest of the arguments. From here on out, everything
10584 is in terms of a newly allocated string instead of the original
10585 ARG. */
81b1e71c
TT
10586 std::string expression (arg, exp_end - arg);
10587 exp_start = arg = expression.c_str ();
699bd4cf
TT
10588 innermost_block_tracker tracker;
10589 expression_up exp = parse_exp_1 (&arg, 0, 0, 0, &tracker);
c906108c 10590 exp_end = arg;
fa8a61dc
TT
10591 /* Remove trailing whitespace from the expression before saving it.
10592 This makes the eventual display of the expression string a bit
10593 prettier. */
10594 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
10595 --exp_end;
10596
65d79d4b 10597 /* Checking if the expression is not constant. */
4d01a485 10598 if (watchpoint_exp_is_const (exp.get ()))
65d79d4b
SDJ
10599 {
10600 int len;
10601
10602 len = exp_end - exp_start;
10603 while (len > 0 && isspace (exp_start[len - 1]))
10604 len--;
10605 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
10606 }
10607
699bd4cf 10608 exp_valid_block = tracker.block ();
b926417a 10609 struct value *mark = value_mark ();
850645cf
TT
10610 struct value *val_as_value = nullptr;
10611 fetch_subexp_value (exp.get (), &pc, &val_as_value, &result, NULL,
10612 just_location);
06a64a0b 10613
850645cf 10614 if (val_as_value != NULL && just_location)
bb9d5f81 10615 {
850645cf
TT
10616 saved_bitpos = value_bitpos (val_as_value);
10617 saved_bitsize = value_bitsize (val_as_value);
bb9d5f81
PP
10618 }
10619
850645cf 10620 value_ref_ptr val;
06a64a0b
TT
10621 if (just_location)
10622 {
9c06b0b4
TJB
10623 int ret;
10624
06a64a0b 10625 exp_valid_block = NULL;
850645cf 10626 val = release_value (value_addr (result));
06a64a0b 10627 value_free_to_mark (mark);
9c06b0b4
TJB
10628
10629 if (use_mask)
10630 {
850645cf 10631 ret = target_masked_watch_num_registers (value_as_address (val.get ()),
9c06b0b4
TJB
10632 mask);
10633 if (ret == -1)
10634 error (_("This target does not support masked watchpoints."));
10635 else if (ret == -2)
10636 error (_("Invalid mask or memory region."));
10637 }
06a64a0b 10638 }
850645cf
TT
10639 else if (val_as_value != NULL)
10640 val = release_value (val_as_value);
c906108c 10641
f1735a53
TT
10642 tok = skip_spaces (arg);
10643 end_tok = skip_to_space (tok);
c906108c
SS
10644
10645 toklen = end_tok - tok;
10646 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
10647 {
10648 tok = cond_start = end_tok + 1;
699bd4cf
TT
10649 innermost_block_tracker if_tracker;
10650 parse_exp_1 (&tok, 0, 0, 0, &if_tracker);
60e1c644
PA
10651
10652 /* The watchpoint expression may not be local, but the condition
10653 may still be. E.g.: `watch global if local > 0'. */
699bd4cf 10654 cond_exp_valid_block = if_tracker.block ();
60e1c644 10655
c906108c
SS
10656 cond_end = tok;
10657 }
10658 if (*tok)
8a3fe4f8 10659 error (_("Junk at end of command."));
c906108c 10660
441d7c93
PA
10661 frame_info *wp_frame = block_innermost_frame (exp_valid_block);
10662
10663 /* Save this because create_internal_breakpoint below invalidates
10664 'wp_frame'. */
10665 frame_id watchpoint_frame = get_frame_id (wp_frame);
d983da9c
DJ
10666
10667 /* If the expression is "local", then set up a "watchpoint scope"
10668 breakpoint at the point where we've left the scope of the watchpoint
10669 expression. Create the scope breakpoint before the watchpoint, so
10670 that we will encounter it first in bpstat_stop_status. */
441d7c93 10671 if (exp_valid_block != NULL && wp_frame != NULL)
d983da9c 10672 {
441d7c93
PA
10673 frame_id caller_frame_id = frame_unwind_caller_id (wp_frame);
10674
10675 if (frame_id_p (caller_frame_id))
edb3359d 10676 {
441d7c93
PA
10677 gdbarch *caller_arch = frame_unwind_caller_arch (wp_frame);
10678 CORE_ADDR caller_pc = frame_unwind_caller_pc (wp_frame);
10679
edb3359d 10680 scope_breakpoint
441d7c93 10681 = create_internal_breakpoint (caller_arch, caller_pc,
06edf0c0
PA
10682 bp_watchpoint_scope,
10683 &momentary_breakpoint_ops);
d983da9c 10684
441d7c93
PA
10685 /* create_internal_breakpoint could invalidate WP_FRAME. */
10686 wp_frame = NULL;
10687
edb3359d 10688 scope_breakpoint->enable_state = bp_enabled;
d983da9c 10689
edb3359d
DJ
10690 /* Automatically delete the breakpoint when it hits. */
10691 scope_breakpoint->disposition = disp_del;
d983da9c 10692
edb3359d 10693 /* Only break in the proper frame (help with recursion). */
441d7c93 10694 scope_breakpoint->frame_id = caller_frame_id;
d983da9c 10695
edb3359d 10696 /* Set the address at which we will stop. */
441d7c93
PA
10697 scope_breakpoint->loc->gdbarch = caller_arch;
10698 scope_breakpoint->loc->requested_address = caller_pc;
edb3359d 10699 scope_breakpoint->loc->address
a6d9a66e
UW
10700 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
10701 scope_breakpoint->loc->requested_address,
edb3359d
DJ
10702 scope_breakpoint->type);
10703 }
d983da9c
DJ
10704 }
10705
e8369a73
AB
10706 /* Now set up the breakpoint. We create all watchpoints as hardware
10707 watchpoints here even if hardware watchpoints are turned off, a call
10708 to update_watchpoint later in this function will cause the type to
10709 drop back to bp_watchpoint (software watchpoint) if required. */
10710
10711 if (accessflag == hw_read)
10712 bp_type = bp_read_watchpoint;
10713 else if (accessflag == hw_access)
10714 bp_type = bp_access_watchpoint;
10715 else
10716 bp_type = bp_hardware_watchpoint;
3a5c3e22 10717
b270e6f9 10718 std::unique_ptr<watchpoint> w (new watchpoint ());
c1fc2657 10719
348d480f 10720 if (use_mask)
b270e6f9 10721 init_raw_breakpoint_without_location (w.get (), NULL, bp_type,
3a5c3e22 10722 &masked_watchpoint_breakpoint_ops);
348d480f 10723 else
b270e6f9 10724 init_raw_breakpoint_without_location (w.get (), NULL, bp_type,
3a5c3e22 10725 &watchpoint_breakpoint_ops);
c1fc2657
SM
10726 w->thread = thread;
10727 w->disposition = disp_donttouch;
10728 w->pspace = current_program_space;
b22e99fd 10729 w->exp = std::move (exp);
3a5c3e22
PA
10730 w->exp_valid_block = exp_valid_block;
10731 w->cond_exp_valid_block = cond_exp_valid_block;
06a64a0b
TT
10732 if (just_location)
10733 {
850645cf
TT
10734 struct type *t = value_type (val.get ());
10735 CORE_ADDR addr = value_as_address (val.get ());
06a64a0b 10736
43cc5389 10737 w->exp_string_reparse
f16a9f57 10738 = current_language->watch_location_expression (t, addr).release ();
06a64a0b 10739
3a5c3e22 10740 w->exp_string = xstrprintf ("-location %.*s",
d63d0675 10741 (int) (exp_end - exp_start), exp_start);
06a64a0b
TT
10742 }
10743 else
3a5c3e22 10744 w->exp_string = savestring (exp_start, exp_end - exp_start);
9c06b0b4
TJB
10745
10746 if (use_mask)
10747 {
3a5c3e22 10748 w->hw_wp_mask = mask;
9c06b0b4
TJB
10749 }
10750 else
10751 {
3a5c3e22 10752 w->val = val;
bb9d5f81
PP
10753 w->val_bitpos = saved_bitpos;
10754 w->val_bitsize = saved_bitsize;
4c1d86d9 10755 w->val_valid = true;
9c06b0b4 10756 }
77b06cd7 10757
c906108c 10758 if (cond_start)
c1fc2657 10759 w->cond_string = savestring (cond_start, cond_end - cond_start);
c906108c 10760 else
c1fc2657 10761 w->cond_string = 0;
c5aa993b 10762
441d7c93 10763 if (frame_id_p (watchpoint_frame))
f6bc2008 10764 {
441d7c93 10765 w->watchpoint_frame = watchpoint_frame;
3a5c3e22 10766 w->watchpoint_thread = inferior_ptid;
f6bc2008 10767 }
c906108c 10768 else
f6bc2008 10769 {
3a5c3e22
PA
10770 w->watchpoint_frame = null_frame_id;
10771 w->watchpoint_thread = null_ptid;
f6bc2008 10772 }
c906108c 10773
d983da9c 10774 if (scope_breakpoint != NULL)
c906108c 10775 {
d983da9c
DJ
10776 /* The scope breakpoint is related to the watchpoint. We will
10777 need to act on them together. */
c1fc2657 10778 w->related_breakpoint = scope_breakpoint;
b270e6f9 10779 scope_breakpoint->related_breakpoint = w.get ();
c906108c 10780 }
d983da9c 10781
06a64a0b
TT
10782 if (!just_location)
10783 value_free_to_mark (mark);
2d134ed3 10784
b270e6f9
TT
10785 /* Finally update the new watchpoint. This creates the locations
10786 that should be inserted. */
10787 update_watchpoint (w.get (), 1);
a9634178 10788
b270e6f9 10789 install_breakpoint (internal, std::move (w), 1);
c906108c
SS
10790}
10791
e09342b5 10792/* Return count of debug registers needed to watch the given expression.
e09342b5 10793 If the watchpoint cannot be handled in hardware return zero. */
c906108c 10794
c906108c 10795static int
a6535de1 10796can_use_hardware_watchpoint (const std::vector<value_ref_ptr> &vals)
c906108c
SS
10797{
10798 int found_memory_cnt = 0;
10799
10800 /* Did the user specifically forbid us to use hardware watchpoints? */
c5aa993b 10801 if (!can_use_hw_watchpoints)
c906108c 10802 return 0;
c5aa993b 10803
a6535de1
TT
10804 gdb_assert (!vals.empty ());
10805 struct value *head = vals[0].get ();
10806
5c44784c
JM
10807 /* Make sure that the value of the expression depends only upon
10808 memory contents, and values computed from them within GDB. If we
10809 find any register references or function calls, we can't use a
10810 hardware watchpoint.
10811
10812 The idea here is that evaluating an expression generates a series
10813 of values, one holding the value of every subexpression. (The
10814 expression a*b+c has five subexpressions: a, b, a*b, c, and
10815 a*b+c.) GDB's values hold almost enough information to establish
10816 the criteria given above --- they identify memory lvalues,
10817 register lvalues, computed values, etcetera. So we can evaluate
10818 the expression, and then scan the chain of values that leaves
10819 behind to decide whether we can detect any possible change to the
10820 expression's final value using only hardware watchpoints.
10821
10822 However, I don't think that the values returned by inferior
10823 function calls are special in any way. So this function may not
10824 notice that an expression involving an inferior function call
10825 can't be watched with hardware watchpoints. FIXME. */
a6535de1 10826 for (const value_ref_ptr &iter : vals)
c906108c 10827 {
a6535de1
TT
10828 struct value *v = iter.get ();
10829
5c44784c 10830 if (VALUE_LVAL (v) == lval_memory)
c906108c 10831 {
8464be76
DJ
10832 if (v != head && value_lazy (v))
10833 /* A lazy memory lvalue in the chain is one that GDB never
10834 needed to fetch; we either just used its address (e.g.,
10835 `a' in `a.b') or we never needed it at all (e.g., `a'
10836 in `a,b'). This doesn't apply to HEAD; if that is
10837 lazy then it was not readable, but watch it anyway. */
5c44784c 10838 ;
53a5351d 10839 else
5c44784c
JM
10840 {
10841 /* Ahh, memory we actually used! Check if we can cover
10842 it with hardware watchpoints. */
df407dfe 10843 struct type *vtype = check_typedef (value_type (v));
2e70b7b9
MS
10844
10845 /* We only watch structs and arrays if user asked for it
10846 explicitly, never if they just happen to appear in a
10847 middle of some value chain. */
10848 if (v == head
78134374
SM
10849 || (vtype->code () != TYPE_CODE_STRUCT
10850 && vtype->code () != TYPE_CODE_ARRAY))
2e70b7b9 10851 {
42ae5230 10852 CORE_ADDR vaddr = value_address (v);
e09342b5
TJB
10853 int len;
10854 int num_regs;
10855
a9634178 10856 len = (target_exact_watchpoints
e09342b5
TJB
10857 && is_scalar_type_recursive (vtype))?
10858 1 : TYPE_LENGTH (value_type (v));
2e70b7b9 10859
e09342b5
TJB
10860 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
10861 if (!num_regs)
2e70b7b9
MS
10862 return 0;
10863 else
e09342b5 10864 found_memory_cnt += num_regs;
2e70b7b9 10865 }
5c44784c 10866 }
c5aa993b 10867 }
5086187c
AC
10868 else if (VALUE_LVAL (v) != not_lval
10869 && deprecated_value_modifiable (v) == 0)
38b6c3b3 10870 return 0; /* These are values from the history (e.g., $1). */
5086187c 10871 else if (VALUE_LVAL (v) == lval_register)
38b6c3b3 10872 return 0; /* Cannot watch a register with a HW watchpoint. */
c906108c
SS
10873 }
10874
10875 /* The expression itself looks suitable for using a hardware
10876 watchpoint, but give the target machine a chance to reject it. */
10877 return found_memory_cnt;
10878}
10879
8b93c638 10880void
f2fc3015 10881watch_command_wrapper (const char *arg, int from_tty, int internal)
8b93c638 10882{
84f4c1fe 10883 watch_command_1 (arg, hw_write, from_tty, 0, internal);
06a64a0b
TT
10884}
10885
06a64a0b
TT
10886/* A helper function that looks for the "-location" argument and then
10887 calls watch_command_1. */
10888
10889static void
0b39b52e 10890watch_maybe_just_location (const char *arg, int accessflag, int from_tty)
06a64a0b
TT
10891{
10892 int just_location = 0;
10893
10894 if (arg
10895 && (check_for_argument (&arg, "-location", sizeof ("-location") - 1)
10896 || check_for_argument (&arg, "-l", sizeof ("-l") - 1)))
cbba3ecd 10897 just_location = 1;
06a64a0b 10898
84f4c1fe 10899 watch_command_1 (arg, accessflag, from_tty, just_location, 0);
8b93c638 10900}
8926118c 10901
c5aa993b 10902static void
0b39b52e 10903watch_command (const char *arg, int from_tty)
c906108c 10904{
06a64a0b 10905 watch_maybe_just_location (arg, hw_write, from_tty);
c906108c
SS
10906}
10907
8b93c638 10908void
f2fc3015 10909rwatch_command_wrapper (const char *arg, int from_tty, int internal)
8b93c638 10910{
84f4c1fe 10911 watch_command_1 (arg, hw_read, from_tty, 0, internal);
8b93c638 10912}
8926118c 10913
c5aa993b 10914static void
0b39b52e 10915rwatch_command (const char *arg, int from_tty)
c906108c 10916{
06a64a0b 10917 watch_maybe_just_location (arg, hw_read, from_tty);
c906108c
SS
10918}
10919
8b93c638 10920void
f2fc3015 10921awatch_command_wrapper (const char *arg, int from_tty, int internal)
8b93c638 10922{
84f4c1fe 10923 watch_command_1 (arg, hw_access, from_tty, 0, internal);
8b93c638 10924}
8926118c 10925
c5aa993b 10926static void
0b39b52e 10927awatch_command (const char *arg, int from_tty)
c906108c 10928{
06a64a0b 10929 watch_maybe_just_location (arg, hw_access, from_tty);
c906108c 10930}
c906108c 10931\f
c5aa993b 10932
cfc31633
PA
10933/* Data for the FSM that manages the until(location)/advance commands
10934 in infcmd.c. Here because it uses the mechanisms of
10935 breakpoints. */
c906108c 10936
46e3ed7f 10937struct until_break_fsm : public thread_fsm
bfec99b2 10938{
46e3ed7f 10939 /* The thread that was current when the command was executed. */
cfc31633
PA
10940 int thread;
10941
10942 /* The breakpoint set at the destination location. */
46e3ed7f 10943 breakpoint_up location_breakpoint;
cfc31633
PA
10944
10945 /* Breakpoint set at the return address in the caller frame. May be
10946 NULL. */
46e3ed7f 10947 breakpoint_up caller_breakpoint;
cfc31633 10948
46e3ed7f
TT
10949 until_break_fsm (struct interp *cmd_interp, int thread,
10950 breakpoint_up &&location_breakpoint,
10951 breakpoint_up &&caller_breakpoint)
10952 : thread_fsm (cmd_interp),
10953 thread (thread),
10954 location_breakpoint (std::move (location_breakpoint)),
10955 caller_breakpoint (std::move (caller_breakpoint))
10956 {
10957 }
cfc31633 10958
46e3ed7f
TT
10959 void clean_up (struct thread_info *thread) override;
10960 bool should_stop (struct thread_info *thread) override;
10961 enum async_reply_reason do_async_reply_reason () override;
cfc31633
PA
10962};
10963
cfc31633
PA
10964/* Implementation of the 'should_stop' FSM method for the
10965 until(location)/advance commands. */
10966
46e3ed7f
TT
10967bool
10968until_break_fsm::should_stop (struct thread_info *tp)
cfc31633 10969{
cfc31633 10970 if (bpstat_find_breakpoint (tp->control.stop_bpstat,
46e3ed7f
TT
10971 location_breakpoint.get ()) != NULL
10972 || (caller_breakpoint != NULL
cfc31633 10973 && bpstat_find_breakpoint (tp->control.stop_bpstat,
46e3ed7f
TT
10974 caller_breakpoint.get ()) != NULL))
10975 set_finished ();
cfc31633 10976
46e3ed7f 10977 return true;
cfc31633
PA
10978}
10979
10980/* Implementation of the 'clean_up' FSM method for the
10981 until(location)/advance commands. */
10982
46e3ed7f
TT
10983void
10984until_break_fsm::clean_up (struct thread_info *)
43ff13b4 10985{
cfc31633 10986 /* Clean up our temporary breakpoints. */
46e3ed7f
TT
10987 location_breakpoint.reset ();
10988 caller_breakpoint.reset ();
10989 delete_longjmp_breakpoint (thread);
cfc31633
PA
10990}
10991
10992/* Implementation of the 'async_reply_reason' FSM method for the
10993 until(location)/advance commands. */
10994
46e3ed7f
TT
10995enum async_reply_reason
10996until_break_fsm::do_async_reply_reason ()
cfc31633
PA
10997{
10998 return EXEC_ASYNC_LOCATION_REACHED;
43ff13b4
JM
10999}
11000
c906108c 11001void
f2fc3015 11002until_break_command (const char *arg, int from_tty, int anywhere)
c906108c 11003{
8556afb4
PA
11004 struct frame_info *frame;
11005 struct gdbarch *frame_gdbarch;
11006 struct frame_id stack_frame_id;
11007 struct frame_id caller_frame_id;
186c406b
TT
11008 int thread;
11009 struct thread_info *tp;
c906108c 11010
70509625 11011 clear_proceed_status (0);
c906108c
SS
11012
11013 /* Set a breakpoint where the user wants it and at return from
4a64f543 11014 this function. */
c5aa993b 11015
ffc2605c 11016 event_location_up location = string_to_event_location (&arg, current_language);
f00aae0f 11017
6c5b2ebe
PA
11018 std::vector<symtab_and_line> sals
11019 = (last_displayed_sal_is_valid ()
11020 ? decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
11021 get_last_displayed_symtab (),
11022 get_last_displayed_line ())
11023 : decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE,
cafb3438 11024 NULL, NULL, 0));
c5aa993b 11025
6c5b2ebe 11026 if (sals.size () != 1)
8a3fe4f8 11027 error (_("Couldn't get information on specified line."));
c5aa993b 11028
6c5b2ebe 11029 symtab_and_line &sal = sals[0];
c5aa993b 11030
c906108c 11031 if (*arg)
8a3fe4f8 11032 error (_("Junk at end of arguments."));
c5aa993b 11033
c906108c 11034 resolve_sal_pc (&sal);
c5aa993b 11035
186c406b 11036 tp = inferior_thread ();
5d5658a1 11037 thread = tp->global_num;
186c406b 11038
8556afb4
PA
11039 /* Note linespec handling above invalidates the frame chain.
11040 Installing a breakpoint also invalidates the frame chain (as it
11041 may need to switch threads), so do any frame handling before
11042 that. */
11043
11044 frame = get_selected_frame (NULL);
11045 frame_gdbarch = get_frame_arch (frame);
11046 stack_frame_id = get_stack_frame_id (frame);
11047 caller_frame_id = frame_unwind_caller_id (frame);
883bc8d1 11048
ae66c1fc
EZ
11049 /* Keep within the current frame, or in frames called by the current
11050 one. */
edb3359d 11051
454dafbd 11052 breakpoint_up caller_breakpoint;
5419bdae
TT
11053
11054 gdb::optional<delete_longjmp_breakpoint_cleanup> lj_deleter;
11055
883bc8d1 11056 if (frame_id_p (caller_frame_id))
c906108c 11057 {
883bc8d1 11058 struct symtab_and_line sal2;
cfc31633 11059 struct gdbarch *caller_gdbarch;
883bc8d1
PA
11060
11061 sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
11062 sal2.pc = frame_unwind_caller_pc (frame);
cfc31633
PA
11063 caller_gdbarch = frame_unwind_caller_arch (frame);
11064 caller_breakpoint = set_momentary_breakpoint (caller_gdbarch,
11065 sal2,
11066 caller_frame_id,
11067 bp_until);
186c406b 11068
883bc8d1 11069 set_longjmp_breakpoint (tp, caller_frame_id);
5419bdae 11070 lj_deleter.emplace (thread);
c906108c 11071 }
c5aa993b 11072
c70a6932
JK
11073 /* set_momentary_breakpoint could invalidate FRAME. */
11074 frame = NULL;
11075
454dafbd 11076 breakpoint_up location_breakpoint;
883bc8d1
PA
11077 if (anywhere)
11078 /* If the user told us to continue until a specified location,
11079 we don't specify a frame at which we need to stop. */
cfc31633
PA
11080 location_breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11081 null_frame_id, bp_until);
883bc8d1
PA
11082 else
11083 /* Otherwise, specify the selected frame, because we want to stop
11084 only at the very same frame. */
cfc31633
PA
11085 location_breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11086 stack_frame_id, bp_until);
883bc8d1 11087
46e3ed7f
TT
11088 tp->thread_fsm = new until_break_fsm (command_interp (), tp->global_num,
11089 std::move (location_breakpoint),
11090 std::move (caller_breakpoint));
f107f563 11091
5419bdae
TT
11092 if (lj_deleter)
11093 lj_deleter->release ();
f107f563 11094
cfc31633 11095 proceed (-1, GDB_SIGNAL_DEFAULT);
c906108c 11096}
ae66c1fc 11097
c906108c
SS
11098/* This function attempts to parse an optional "if <cond>" clause
11099 from the arg string. If one is not found, it returns NULL.
c5aa993b 11100
c906108c
SS
11101 Else, it returns a pointer to the condition string. (It does not
11102 attempt to evaluate the string against a particular block.) And,
11103 it updates arg to point to the first character following the parsed
4a64f543 11104 if clause in the arg string. */
53a5351d 11105
63160a43
PA
11106const char *
11107ep_parse_optional_if_clause (const char **arg)
c906108c 11108{
63160a43 11109 const char *cond_string;
c5aa993b
JM
11110
11111 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
c906108c 11112 return NULL;
c5aa993b 11113
4a64f543 11114 /* Skip the "if" keyword. */
c906108c 11115 (*arg) += 2;
c5aa993b 11116
c906108c 11117 /* Skip any extra leading whitespace, and record the start of the
4a64f543 11118 condition string. */
f1735a53 11119 *arg = skip_spaces (*arg);
c906108c 11120 cond_string = *arg;
c5aa993b 11121
4a64f543
MS
11122 /* Assume that the condition occupies the remainder of the arg
11123 string. */
c906108c 11124 (*arg) += strlen (cond_string);
c5aa993b 11125
c906108c
SS
11126 return cond_string;
11127}
c5aa993b 11128
c906108c
SS
11129/* Commands to deal with catching events, such as signals, exceptions,
11130 process start/exit, etc. */
c5aa993b
JM
11131
11132typedef enum
11133{
44feb3ce
TT
11134 catch_fork_temporary, catch_vfork_temporary,
11135 catch_fork_permanent, catch_vfork_permanent
c5aa993b
JM
11136}
11137catch_fork_kind;
11138
c906108c 11139static void
eb4c3f4a 11140catch_fork_command_1 (const char *arg, int from_tty,
cc59ec59 11141 struct cmd_list_element *command)
c906108c 11142{
a6d9a66e 11143 struct gdbarch *gdbarch = get_current_arch ();
63160a43 11144 const char *cond_string = NULL;
44feb3ce
TT
11145 catch_fork_kind fork_kind;
11146 int tempflag;
11147
11148 fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
11149 tempflag = (fork_kind == catch_fork_temporary
11150 || fork_kind == catch_vfork_temporary);
c5aa993b 11151
44feb3ce
TT
11152 if (!arg)
11153 arg = "";
f1735a53 11154 arg = skip_spaces (arg);
c5aa993b 11155
c906108c 11156 /* The allowed syntax is:
c5aa993b
JM
11157 catch [v]fork
11158 catch [v]fork if <cond>
11159
4a64f543 11160 First, check if there's an if clause. */
c906108c 11161 cond_string = ep_parse_optional_if_clause (&arg);
c5aa993b 11162
c906108c 11163 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 11164 error (_("Junk at end of arguments."));
c5aa993b 11165
c906108c 11166 /* If this target supports it, create a fork or vfork catchpoint
4a64f543 11167 and enable reporting of such events. */
c5aa993b
JM
11168 switch (fork_kind)
11169 {
44feb3ce
TT
11170 case catch_fork_temporary:
11171 case catch_fork_permanent:
a6d9a66e 11172 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
ce78b96d 11173 &catch_fork_breakpoint_ops);
c906108c 11174 break;
44feb3ce
TT
11175 case catch_vfork_temporary:
11176 case catch_vfork_permanent:
a6d9a66e 11177 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
ce78b96d 11178 &catch_vfork_breakpoint_ops);
c906108c 11179 break;
c5aa993b 11180 default:
8a3fe4f8 11181 error (_("unsupported or unknown fork kind; cannot catch it"));
c906108c 11182 break;
c5aa993b 11183 }
c906108c
SS
11184}
11185
11186static void
eb4c3f4a 11187catch_exec_command_1 (const char *arg, int from_tty,
cc59ec59 11188 struct cmd_list_element *command)
c906108c 11189{
a6d9a66e 11190 struct gdbarch *gdbarch = get_current_arch ();
44feb3ce 11191 int tempflag;
63160a43 11192 const char *cond_string = NULL;
c906108c 11193
44feb3ce
TT
11194 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
11195
11196 if (!arg)
11197 arg = "";
f1735a53 11198 arg = skip_spaces (arg);
c906108c
SS
11199
11200 /* The allowed syntax is:
c5aa993b
JM
11201 catch exec
11202 catch exec if <cond>
c906108c 11203
4a64f543 11204 First, check if there's an if clause. */
c906108c
SS
11205 cond_string = ep_parse_optional_if_clause (&arg);
11206
11207 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 11208 error (_("Junk at end of arguments."));
c906108c 11209
b270e6f9
TT
11210 std::unique_ptr<exec_catchpoint> c (new exec_catchpoint ());
11211 init_catchpoint (c.get (), gdbarch, tempflag, cond_string,
b4d90040
PA
11212 &catch_exec_breakpoint_ops);
11213 c->exec_pathname = NULL;
11214
b270e6f9 11215 install_breakpoint (0, std::move (c), 1);
c906108c 11216}
c5aa993b 11217
9ac4176b 11218void
28010a5d
PA
11219init_ada_exception_breakpoint (struct breakpoint *b,
11220 struct gdbarch *gdbarch,
11221 struct symtab_and_line sal,
f2fc3015 11222 const char *addr_string,
c0a91b2b 11223 const struct breakpoint_ops *ops,
28010a5d 11224 int tempflag,
349774ef 11225 int enabled,
28010a5d 11226 int from_tty)
f7f9143b 11227{
f7f9143b
JB
11228 if (from_tty)
11229 {
5af949e3
UW
11230 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
11231 if (!loc_gdbarch)
11232 loc_gdbarch = gdbarch;
11233
6c95b8df
PA
11234 describe_other_breakpoints (loc_gdbarch,
11235 sal.pspace, sal.pc, sal.section, -1);
f7f9143b
JB
11236 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
11237 version for exception catchpoints, because two catchpoints
11238 used for different exception names will use the same address.
11239 In this case, a "breakpoint ... also set at..." warning is
4a64f543 11240 unproductive. Besides, the warning phrasing is also a bit
e5dd4106 11241 inappropriate, we should use the word catchpoint, and tell
f7f9143b
JB
11242 the user what type of catchpoint it is. The above is good
11243 enough for now, though. */
11244 }
11245
f06f1252 11246 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
f7f9143b 11247
349774ef 11248 b->enable_state = enabled ? bp_enabled : bp_disabled;
f7f9143b 11249 b->disposition = tempflag ? disp_del : disp_donttouch;
d28cd78a
TT
11250 b->location = string_to_event_location (&addr_string,
11251 language_def (language_ada));
f7f9143b 11252 b->language = language_ada;
f7f9143b
JB
11253}
11254
c906108c
SS
11255\f
11256
81b1e71c 11257/* Compare two breakpoints and return a strcmp-like result. */
8a2c437b
TT
11258
11259static int
81b1e71c 11260compare_breakpoints (const breakpoint *a, const breakpoint *b)
8a2c437b 11261{
81b1e71c
TT
11262 uintptr_t ua = (uintptr_t) a;
11263 uintptr_t ub = (uintptr_t) b;
8a2c437b 11264
81b1e71c 11265 if (a->number < b->number)
8a2c437b 11266 return -1;
81b1e71c 11267 else if (a->number > b->number)
8a2c437b
TT
11268 return 1;
11269
11270 /* Now sort by address, in case we see, e..g, two breakpoints with
11271 the number 0. */
11272 if (ua < ub)
11273 return -1;
94b0e70d 11274 return ua > ub ? 1 : 0;
8a2c437b
TT
11275}
11276
80f8a6eb 11277/* Delete breakpoints by address or line. */
c906108c
SS
11278
11279static void
0b39b52e 11280clear_command (const char *arg, int from_tty)
c906108c 11281{
81b1e71c 11282 struct breakpoint *b;
c906108c 11283 int default_match;
c906108c 11284
6c5b2ebe
PA
11285 std::vector<symtab_and_line> decoded_sals;
11286 symtab_and_line last_sal;
11287 gdb::array_view<symtab_and_line> sals;
c906108c
SS
11288 if (arg)
11289 {
6c5b2ebe
PA
11290 decoded_sals
11291 = decode_line_with_current_source (arg,
11292 (DECODE_LINE_FUNFIRSTLINE
11293 | DECODE_LINE_LIST_MODE));
c906108c 11294 default_match = 0;
6c5b2ebe 11295 sals = decoded_sals;
c906108c
SS
11296 }
11297 else
11298 {
1bfeeb0f
JL
11299 /* Set sal's line, symtab, pc, and pspace to the values
11300 corresponding to the last call to print_frame_info. If the
11301 codepoint is not valid, this will set all the fields to 0. */
51abb421 11302 last_sal = get_last_displayed_sal ();
6c5b2ebe 11303 if (last_sal.symtab == 0)
8a3fe4f8 11304 error (_("No source file specified."));
c906108c 11305
c906108c 11306 default_match = 1;
6c5b2ebe 11307 sals = last_sal;
c906108c
SS
11308 }
11309
4a64f543
MS
11310 /* We don't call resolve_sal_pc here. That's not as bad as it
11311 seems, because all existing breakpoints typically have both
11312 file/line and pc set. So, if clear is given file/line, we can
11313 match this to existing breakpoint without obtaining pc at all.
ed0616c6
VP
11314
11315 We only support clearing given the address explicitly
11316 present in breakpoint table. Say, we've set breakpoint
4a64f543 11317 at file:line. There were several PC values for that file:line,
ed0616c6 11318 due to optimization, all in one block.
4a64f543
MS
11319
11320 We've picked one PC value. If "clear" is issued with another
ed0616c6
VP
11321 PC corresponding to the same file:line, the breakpoint won't
11322 be cleared. We probably can still clear the breakpoint, but
11323 since the other PC value is never presented to user, user
11324 can only find it by guessing, and it does not seem important
11325 to support that. */
11326
4a64f543
MS
11327 /* For each line spec given, delete bps which correspond to it. Do
11328 it in two passes, solely to preserve the current behavior that
11329 from_tty is forced true if we delete more than one
11330 breakpoint. */
c906108c 11331
81b1e71c 11332 std::vector<struct breakpoint *> found;
6c5b2ebe 11333 for (const auto &sal : sals)
c906108c 11334 {
05cba821
JK
11335 const char *sal_fullname;
11336
c906108c 11337 /* If exact pc given, clear bpts at that pc.
c5aa993b
JM
11338 If line given (pc == 0), clear all bpts on specified line.
11339 If defaulting, clear all bpts on default line
c906108c 11340 or at default pc.
c5aa993b
JM
11341
11342 defaulting sal.pc != 0 tests to do
11343
11344 0 1 pc
11345 1 1 pc _and_ line
11346 0 0 line
11347 1 0 <can't happen> */
c906108c 11348
05cba821
JK
11349 sal_fullname = (sal.symtab == NULL
11350 ? NULL : symtab_to_fullname (sal.symtab));
c906108c 11351
4a64f543 11352 /* Find all matching breakpoints and add them to 'found'. */
d6e956e5 11353 ALL_BREAKPOINTS (b)
c5aa993b 11354 {
0d381245 11355 int match = 0;
4a64f543 11356 /* Are we going to delete b? */
cc60f2e3 11357 if (b->type != bp_none && !is_watchpoint (b))
0d381245
VP
11358 {
11359 struct bp_location *loc = b->loc;
11360 for (; loc; loc = loc->next)
11361 {
f8eba3c6
TT
11362 /* If the user specified file:line, don't allow a PC
11363 match. This matches historical gdb behavior. */
11364 int pc_match = (!sal.explicit_line
11365 && sal.pc
11366 && (loc->pspace == sal.pspace)
11367 && (loc->address == sal.pc)
11368 && (!section_is_overlay (loc->section)
11369 || loc->section == sal.section));
4aac40c8
TT
11370 int line_match = 0;
11371
11372 if ((default_match || sal.explicit_line)
2f202fde 11373 && loc->symtab != NULL
05cba821 11374 && sal_fullname != NULL
4aac40c8 11375 && sal.pspace == loc->pspace
05cba821
JK
11376 && loc->line_number == sal.line
11377 && filename_cmp (symtab_to_fullname (loc->symtab),
11378 sal_fullname) == 0)
11379 line_match = 1;
4aac40c8 11380
0d381245
VP
11381 if (pc_match || line_match)
11382 {
11383 match = 1;
11384 break;
11385 }
11386 }
11387 }
11388
11389 if (match)
81b1e71c 11390 found.push_back (b);
c906108c 11391 }
80f8a6eb 11392 }
8a2c437b 11393
80f8a6eb 11394 /* Now go thru the 'found' chain and delete them. */
81b1e71c 11395 if (found.empty ())
80f8a6eb
MS
11396 {
11397 if (arg)
8a3fe4f8 11398 error (_("No breakpoint at %s."), arg);
80f8a6eb 11399 else
8a3fe4f8 11400 error (_("No breakpoint at this line."));
80f8a6eb 11401 }
c906108c 11402
8a2c437b 11403 /* Remove duplicates from the vec. */
81b1e71c 11404 std::sort (found.begin (), found.end (),
b926417a 11405 [] (const breakpoint *bp_a, const breakpoint *bp_b)
81b1e71c 11406 {
b926417a 11407 return compare_breakpoints (bp_a, bp_b) < 0;
81b1e71c
TT
11408 });
11409 found.erase (std::unique (found.begin (), found.end (),
b926417a 11410 [] (const breakpoint *bp_a, const breakpoint *bp_b)
81b1e71c 11411 {
b926417a 11412 return compare_breakpoints (bp_a, bp_b) == 0;
81b1e71c
TT
11413 }),
11414 found.end ());
8a2c437b 11415
81b1e71c 11416 if (found.size () > 1)
4a64f543 11417 from_tty = 1; /* Always report if deleted more than one. */
80f8a6eb 11418 if (from_tty)
a3f17187 11419 {
81b1e71c 11420 if (found.size () == 1)
a3f17187
AC
11421 printf_unfiltered (_("Deleted breakpoint "));
11422 else
11423 printf_unfiltered (_("Deleted breakpoints "));
11424 }
d6e956e5 11425
81b1e71c 11426 for (breakpoint *iter : found)
80f8a6eb 11427 {
c5aa993b 11428 if (from_tty)
81b1e71c
TT
11429 printf_unfiltered ("%d ", iter->number);
11430 delete_breakpoint (iter);
c906108c 11431 }
80f8a6eb
MS
11432 if (from_tty)
11433 putchar_unfiltered ('\n');
c906108c
SS
11434}
11435\f
11436/* Delete breakpoint in BS if they are `delete' breakpoints and
11437 all breakpoints that are marked for deletion, whether hit or not.
11438 This is called after any breakpoint is hit, or after errors. */
11439
11440void
fba45db2 11441breakpoint_auto_delete (bpstat bs)
c906108c 11442{
35df4500 11443 struct breakpoint *b, *b_tmp;
c906108c
SS
11444
11445 for (; bs; bs = bs->next)
f431efe5
PA
11446 if (bs->breakpoint_at
11447 && bs->breakpoint_at->disposition == disp_del
c906108c 11448 && bs->stop)
f431efe5 11449 delete_breakpoint (bs->breakpoint_at);
c906108c 11450
35df4500 11451 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 11452 {
b5de0fa7 11453 if (b->disposition == disp_del_at_next_stop)
c5aa993b
JM
11454 delete_breakpoint (b);
11455 }
c906108c
SS
11456}
11457
4a64f543 11458/* A comparison function for bp_location AP and BP being interfaced to
39ef2f62 11459 std::sort. Sort elements primarily by their ADDRESS (no matter what
cb1e4e32
PA
11460 bl_address_is_meaningful says), secondarily by ordering first
11461 permanent elements and terciarily just ensuring the array is sorted
39ef2f62 11462 stable way despite std::sort being an unstable algorithm. */
876fa593
JK
11463
11464static int
39ef2f62 11465bp_location_is_less_than (const bp_location *a, const bp_location *b)
876fa593 11466{
876fa593 11467 if (a->address != b->address)
39ef2f62 11468 return a->address < b->address;
876fa593 11469
dea2aa5f
LM
11470 /* Sort locations at the same address by their pspace number, keeping
11471 locations of the same inferior (in a multi-inferior environment)
11472 grouped. */
11473
11474 if (a->pspace->num != b->pspace->num)
39ef2f62 11475 return a->pspace->num < b->pspace->num;
dea2aa5f 11476
876fa593 11477 /* Sort permanent breakpoints first. */
1a853c52 11478 if (a->permanent != b->permanent)
39ef2f62 11479 return a->permanent > b->permanent;
876fa593 11480
7f32a4d5
PA
11481 /* Sort by type in order to make duplicate determination easier.
11482 See update_global_location_list. This is kept in sync with
11483 breakpoint_locations_match. */
11484 if (a->loc_type < b->loc_type)
11485 return true;
11486
11487 /* Likewise, for range-breakpoints, sort by length. */
11488 if (a->loc_type == bp_loc_hardware_breakpoint
11489 && b->loc_type == bp_loc_hardware_breakpoint
11490 && a->length < b->length)
11491 return true;
11492
c56a97f9
JK
11493 /* Make the internal GDB representation stable across GDB runs
11494 where A and B memory inside GDB can differ. Breakpoint locations of
11495 the same type at the same address can be sorted in arbitrary order. */
876fa593
JK
11496
11497 if (a->owner->number != b->owner->number)
39ef2f62 11498 return a->owner->number < b->owner->number;
876fa593 11499
39ef2f62 11500 return a < b;
876fa593
JK
11501}
11502
f5336ca5
PA
11503/* Set bp_locations_placed_address_before_address_max and
11504 bp_locations_shadow_len_after_address_max according to the current
11505 content of the bp_locations array. */
f7545552
TT
11506
11507static void
f5336ca5 11508bp_locations_target_extensions_update (void)
f7545552 11509{
876fa593
JK
11510 struct bp_location *bl, **blp_tmp;
11511
f5336ca5
PA
11512 bp_locations_placed_address_before_address_max = 0;
11513 bp_locations_shadow_len_after_address_max = 0;
876fa593
JK
11514
11515 ALL_BP_LOCATIONS (bl, blp_tmp)
11516 {
11517 CORE_ADDR start, end, addr;
11518
11519 if (!bp_location_has_shadow (bl))
11520 continue;
11521
11522 start = bl->target_info.placed_address;
11523 end = start + bl->target_info.shadow_len;
11524
11525 gdb_assert (bl->address >= start);
11526 addr = bl->address - start;
f5336ca5
PA
11527 if (addr > bp_locations_placed_address_before_address_max)
11528 bp_locations_placed_address_before_address_max = addr;
876fa593
JK
11529
11530 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
11531
11532 gdb_assert (bl->address < end);
11533 addr = end - bl->address;
f5336ca5
PA
11534 if (addr > bp_locations_shadow_len_after_address_max)
11535 bp_locations_shadow_len_after_address_max = addr;
876fa593 11536 }
f7545552
TT
11537}
11538
1e4d1764
YQ
11539/* Download tracepoint locations if they haven't been. */
11540
11541static void
11542download_tracepoint_locations (void)
11543{
7ed2c994 11544 struct breakpoint *b;
dd2e65cc 11545 enum tribool can_download_tracepoint = TRIBOOL_UNKNOWN;
1e4d1764 11546
5ed8105e 11547 scoped_restore_current_pspace_and_thread restore_pspace_thread;
1e4d1764 11548
7ed2c994 11549 ALL_TRACEPOINTS (b)
1e4d1764 11550 {
7ed2c994 11551 struct bp_location *bl;
1e4d1764 11552 struct tracepoint *t;
f2a8bc8a 11553 int bp_location_downloaded = 0;
1e4d1764 11554
7ed2c994 11555 if ((b->type == bp_fast_tracepoint
1e4d1764
YQ
11556 ? !may_insert_fast_tracepoints
11557 : !may_insert_tracepoints))
11558 continue;
11559
dd2e65cc
YQ
11560 if (can_download_tracepoint == TRIBOOL_UNKNOWN)
11561 {
11562 if (target_can_download_tracepoint ())
11563 can_download_tracepoint = TRIBOOL_TRUE;
11564 else
11565 can_download_tracepoint = TRIBOOL_FALSE;
11566 }
11567
11568 if (can_download_tracepoint == TRIBOOL_FALSE)
11569 break;
11570
7ed2c994
YQ
11571 for (bl = b->loc; bl; bl = bl->next)
11572 {
11573 /* In tracepoint, locations are _never_ duplicated, so
11574 should_be_inserted is equivalent to
11575 unduplicated_should_be_inserted. */
11576 if (!should_be_inserted (bl) || bl->inserted)
11577 continue;
1e4d1764 11578
7ed2c994 11579 switch_to_program_space_and_thread (bl->pspace);
1e4d1764 11580
7ed2c994 11581 target_download_tracepoint (bl);
1e4d1764 11582
7ed2c994 11583 bl->inserted = 1;
f2a8bc8a 11584 bp_location_downloaded = 1;
7ed2c994
YQ
11585 }
11586 t = (struct tracepoint *) b;
11587 t->number_on_target = b->number;
f2a8bc8a 11588 if (bp_location_downloaded)
76727919 11589 gdb::observers::breakpoint_modified.notify (b);
1e4d1764 11590 }
1e4d1764
YQ
11591}
11592
934709f0
PW
11593/* Swap the insertion/duplication state between two locations. */
11594
11595static void
11596swap_insertion (struct bp_location *left, struct bp_location *right)
11597{
11598 const int left_inserted = left->inserted;
11599 const int left_duplicate = left->duplicate;
b775012e 11600 const int left_needs_update = left->needs_update;
934709f0
PW
11601 const struct bp_target_info left_target_info = left->target_info;
11602
1e4d1764
YQ
11603 /* Locations of tracepoints can never be duplicated. */
11604 if (is_tracepoint (left->owner))
11605 gdb_assert (!left->duplicate);
11606 if (is_tracepoint (right->owner))
11607 gdb_assert (!right->duplicate);
11608
934709f0
PW
11609 left->inserted = right->inserted;
11610 left->duplicate = right->duplicate;
b775012e 11611 left->needs_update = right->needs_update;
934709f0
PW
11612 left->target_info = right->target_info;
11613 right->inserted = left_inserted;
11614 right->duplicate = left_duplicate;
b775012e 11615 right->needs_update = left_needs_update;
934709f0
PW
11616 right->target_info = left_target_info;
11617}
11618
b775012e
LM
11619/* Force the re-insertion of the locations at ADDRESS. This is called
11620 once a new/deleted/modified duplicate location is found and we are evaluating
11621 conditions on the target's side. Such conditions need to be updated on
11622 the target. */
11623
11624static void
11625force_breakpoint_reinsertion (struct bp_location *bl)
11626{
11627 struct bp_location **locp = NULL, **loc2p;
11628 struct bp_location *loc;
11629 CORE_ADDR address = 0;
11630 int pspace_num;
11631
11632 address = bl->address;
11633 pspace_num = bl->pspace->num;
11634
11635 /* This is only meaningful if the target is
11636 evaluating conditions and if the user has
11637 opted for condition evaluation on the target's
11638 side. */
11639 if (gdb_evaluates_breakpoint_condition_p ()
11640 || !target_supports_evaluation_of_breakpoint_conditions ())
11641 return;
11642
11643 /* Flag all breakpoint locations with this address and
11644 the same program space as the location
11645 as "its condition has changed". We need to
11646 update the conditions on the target's side. */
11647 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, address)
11648 {
11649 loc = *loc2p;
11650
11651 if (!is_breakpoint (loc->owner)
11652 || pspace_num != loc->pspace->num)
11653 continue;
11654
11655 /* Flag the location appropriately. We use a different state to
11656 let everyone know that we already updated the set of locations
11657 with addr bl->address and program space bl->pspace. This is so
11658 we don't have to keep calling these functions just to mark locations
11659 that have already been marked. */
11660 loc->condition_changed = condition_updated;
11661
11662 /* Free the agent expression bytecode as well. We will compute
11663 it later on. */
833177a4 11664 loc->cond_bytecode.reset ();
b775012e
LM
11665 }
11666}
7f32a4d5 11667
44702360
PA
11668/* Called whether new breakpoints are created, or existing breakpoints
11669 deleted, to update the global location list and recompute which
11670 locations are duplicate of which.
b775012e 11671
04086b45
PA
11672 The INSERT_MODE flag determines whether locations may not, may, or
11673 shall be inserted now. See 'enum ugll_insert_mode' for more
11674 info. */
b60e7edf 11675
0d381245 11676static void
44702360 11677update_global_location_list (enum ugll_insert_mode insert_mode)
0d381245 11678{
74960c60 11679 struct breakpoint *b;
876fa593 11680 struct bp_location **locp, *loc;
b775012e
LM
11681 /* Last breakpoint location address that was marked for update. */
11682 CORE_ADDR last_addr = 0;
11683 /* Last breakpoint location program space that was marked for update. */
11684 int last_pspace_num = -1;
f7545552 11685
2d134ed3
PA
11686 /* Used in the duplicates detection below. When iterating over all
11687 bp_locations, points to the first bp_location of a given address.
11688 Breakpoints and watchpoints of different types are never
11689 duplicates of each other. Keep one pointer for each type of
11690 breakpoint/watchpoint, so we only need to loop over all locations
11691 once. */
11692 struct bp_location *bp_loc_first; /* breakpoint */
11693 struct bp_location *wp_loc_first; /* hardware watchpoint */
11694 struct bp_location *awp_loc_first; /* access watchpoint */
11695 struct bp_location *rwp_loc_first; /* read watchpoint */
876fa593 11696
f5336ca5
PA
11697 /* Saved former bp_locations array which we compare against the newly
11698 built bp_locations from the current state of ALL_BREAKPOINTS. */
81b1e71c 11699 struct bp_location **old_locp;
f5336ca5 11700 unsigned old_locations_count;
81b1e71c 11701 gdb::unique_xmalloc_ptr<struct bp_location *> old_locations (bp_locations);
876fa593 11702
f5336ca5
PA
11703 old_locations_count = bp_locations_count;
11704 bp_locations = NULL;
11705 bp_locations_count = 0;
0d381245 11706
74960c60 11707 ALL_BREAKPOINTS (b)
876fa593 11708 for (loc = b->loc; loc; loc = loc->next)
f5336ca5 11709 bp_locations_count++;
876fa593 11710
f5336ca5
PA
11711 bp_locations = XNEWVEC (struct bp_location *, bp_locations_count);
11712 locp = bp_locations;
876fa593
JK
11713 ALL_BREAKPOINTS (b)
11714 for (loc = b->loc; loc; loc = loc->next)
11715 *locp++ = loc;
7f32a4d5
PA
11716
11717 /* See if we need to "upgrade" a software breakpoint to a hardware
11718 breakpoint. Do this before deciding whether locations are
11719 duplicates. Also do this before sorting because sorting order
11720 depends on location type. */
11721 for (locp = bp_locations;
11722 locp < bp_locations + bp_locations_count;
11723 locp++)
11724 {
11725 loc = *locp;
11726 if (!loc->inserted && should_be_inserted (loc))
11727 handle_automatic_hardware_breakpoints (loc);
11728 }
11729
39ef2f62
CB
11730 std::sort (bp_locations, bp_locations + bp_locations_count,
11731 bp_location_is_less_than);
876fa593 11732
f5336ca5 11733 bp_locations_target_extensions_update ();
74960c60 11734
4a64f543
MS
11735 /* Identify bp_location instances that are no longer present in the
11736 new list, and therefore should be freed. Note that it's not
11737 necessary that those locations should be removed from inferior --
11738 if there's another location at the same address (previously
11739 marked as duplicate), we don't need to remove/insert the
11740 location.
876fa593 11741
4a64f543
MS
11742 LOCP is kept in sync with OLD_LOCP, each pointing to the current
11743 and former bp_location array state respectively. */
876fa593 11744
f5336ca5 11745 locp = bp_locations;
81b1e71c
TT
11746 for (old_locp = old_locations.get ();
11747 old_locp < old_locations.get () + old_locations_count;
876fa593 11748 old_locp++)
74960c60 11749 {
876fa593 11750 struct bp_location *old_loc = *old_locp;
c7d46a38 11751 struct bp_location **loc2p;
876fa593 11752
e5dd4106 11753 /* Tells if 'old_loc' is found among the new locations. If
4a64f543 11754 not, we have to free it. */
c7d46a38 11755 int found_object = 0;
20874c92
VP
11756 /* Tells if the location should remain inserted in the target. */
11757 int keep_in_target = 0;
11758 int removed = 0;
876fa593 11759
4a64f543
MS
11760 /* Skip LOCP entries which will definitely never be needed.
11761 Stop either at or being the one matching OLD_LOC. */
f5336ca5 11762 while (locp < bp_locations + bp_locations_count
c7d46a38 11763 && (*locp)->address < old_loc->address)
876fa593 11764 locp++;
c7d46a38
PA
11765
11766 for (loc2p = locp;
f5336ca5 11767 (loc2p < bp_locations + bp_locations_count
c7d46a38
PA
11768 && (*loc2p)->address == old_loc->address);
11769 loc2p++)
11770 {
b775012e
LM
11771 /* Check if this is a new/duplicated location or a duplicated
11772 location that had its condition modified. If so, we want to send
11773 its condition to the target if evaluation of conditions is taking
11774 place there. */
11775 if ((*loc2p)->condition_changed == condition_modified
11776 && (last_addr != old_loc->address
11777 || last_pspace_num != old_loc->pspace->num))
c7d46a38 11778 {
b775012e
LM
11779 force_breakpoint_reinsertion (*loc2p);
11780 last_pspace_num = old_loc->pspace->num;
c7d46a38 11781 }
b775012e
LM
11782
11783 if (*loc2p == old_loc)
11784 found_object = 1;
c7d46a38 11785 }
74960c60 11786
b775012e
LM
11787 /* We have already handled this address, update it so that we don't
11788 have to go through updates again. */
11789 last_addr = old_loc->address;
11790
11791 /* Target-side condition evaluation: Handle deleted locations. */
11792 if (!found_object)
11793 force_breakpoint_reinsertion (old_loc);
11794
4a64f543
MS
11795 /* If this location is no longer present, and inserted, look if
11796 there's maybe a new location at the same address. If so,
11797 mark that one inserted, and don't remove this one. This is
11798 needed so that we don't have a time window where a breakpoint
11799 at certain location is not inserted. */
74960c60 11800
876fa593 11801 if (old_loc->inserted)
0d381245 11802 {
4a64f543
MS
11803 /* If the location is inserted now, we might have to remove
11804 it. */
74960c60 11805
876fa593 11806 if (found_object && should_be_inserted (old_loc))
74960c60 11807 {
4a64f543
MS
11808 /* The location is still present in the location list,
11809 and still should be inserted. Don't do anything. */
20874c92 11810 keep_in_target = 1;
74960c60
VP
11811 }
11812 else
11813 {
b775012e
LM
11814 /* This location still exists, but it won't be kept in the
11815 target since it may have been disabled. We proceed to
11816 remove its target-side condition. */
11817
4a64f543
MS
11818 /* The location is either no longer present, or got
11819 disabled. See if there's another location at the
11820 same address, in which case we don't need to remove
11821 this one from the target. */
876fa593 11822
2bdf28a0 11823 /* OLD_LOC comes from existing struct breakpoint. */
cb1e4e32 11824 if (bl_address_is_meaningful (old_loc))
876fa593 11825 {
876fa593 11826 for (loc2p = locp;
f5336ca5 11827 (loc2p < bp_locations + bp_locations_count
c7d46a38 11828 && (*loc2p)->address == old_loc->address);
876fa593
JK
11829 loc2p++)
11830 {
11831 struct bp_location *loc2 = *loc2p;
11832
7f32a4d5
PA
11833 if (loc2 == old_loc)
11834 continue;
11835
2d134ed3 11836 if (breakpoint_locations_match (loc2, old_loc))
c7d46a38 11837 {
85d721b8
PA
11838 /* Read watchpoint locations are switched to
11839 access watchpoints, if the former are not
11840 supported, but the latter are. */
11841 if (is_hardware_watchpoint (old_loc->owner))
11842 {
11843 gdb_assert (is_hardware_watchpoint (loc2->owner));
11844 loc2->watchpoint_type = old_loc->watchpoint_type;
11845 }
11846
934709f0
PW
11847 /* loc2 is a duplicated location. We need to check
11848 if it should be inserted in case it will be
11849 unduplicated. */
7f32a4d5 11850 if (unduplicated_should_be_inserted (loc2))
c7d46a38 11851 {
934709f0 11852 swap_insertion (old_loc, loc2);
c7d46a38
PA
11853 keep_in_target = 1;
11854 break;
11855 }
876fa593
JK
11856 }
11857 }
11858 }
74960c60
VP
11859 }
11860
20874c92
VP
11861 if (!keep_in_target)
11862 {
834c0d03 11863 if (remove_breakpoint (old_loc))
20874c92 11864 {
4a64f543
MS
11865 /* This is just about all we can do. We could keep
11866 this location on the global list, and try to
11867 remove it next time, but there's no particular
11868 reason why we will succeed next time.
20874c92 11869
4a64f543
MS
11870 Note that at this point, old_loc->owner is still
11871 valid, as delete_breakpoint frees the breakpoint
11872 only after calling us. */
3e43a32a
MS
11873 printf_filtered (_("warning: Error removing "
11874 "breakpoint %d\n"),
876fa593 11875 old_loc->owner->number);
20874c92
VP
11876 }
11877 removed = 1;
11878 }
0d381245 11879 }
74960c60
VP
11880
11881 if (!found_object)
1c5cfe86 11882 {
fbea99ea 11883 if (removed && target_is_non_stop_p ()
1cf4d951 11884 && need_moribund_for_location_type (old_loc))
20874c92 11885 {
db82e815
PA
11886 /* This location was removed from the target. In
11887 non-stop mode, a race condition is possible where
11888 we've removed a breakpoint, but stop events for that
11889 breakpoint are already queued and will arrive later.
11890 We apply an heuristic to be able to distinguish such
11891 SIGTRAPs from other random SIGTRAPs: we keep this
11892 breakpoint location for a bit, and will retire it
11893 after we see some number of events. The theory here
11894 is that reporting of events should, "on the average",
11895 be fair, so after a while we'll see events from all
11896 threads that have anything of interest, and no longer
11897 need to keep this breakpoint location around. We
11898 don't hold locations forever so to reduce chances of
11899 mistaking a non-breakpoint SIGTRAP for a breakpoint
11900 SIGTRAP.
11901
11902 The heuristic failing can be disastrous on
11903 decr_pc_after_break targets.
11904
11905 On decr_pc_after_break targets, like e.g., x86-linux,
11906 if we fail to recognize a late breakpoint SIGTRAP,
11907 because events_till_retirement has reached 0 too
11908 soon, we'll fail to do the PC adjustment, and report
11909 a random SIGTRAP to the user. When the user resumes
11910 the inferior, it will most likely immediately crash
2dec564e 11911 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
db82e815
PA
11912 corrupted, because of being resumed e.g., in the
11913 middle of a multi-byte instruction, or skipped a
11914 one-byte instruction. This was actually seen happen
11915 on native x86-linux, and should be less rare on
11916 targets that do not support new thread events, like
11917 remote, due to the heuristic depending on
11918 thread_count.
11919
11920 Mistaking a random SIGTRAP for a breakpoint trap
11921 causes similar symptoms (PC adjustment applied when
11922 it shouldn't), but then again, playing with SIGTRAPs
11923 behind the debugger's back is asking for trouble.
11924
11925 Since hardware watchpoint traps are always
11926 distinguishable from other traps, so we don't need to
11927 apply keep hardware watchpoint moribund locations
11928 around. We simply always ignore hardware watchpoint
11929 traps we can no longer explain. */
11930
5b6d1e4f
PA
11931 process_stratum_target *proc_target = nullptr;
11932 for (inferior *inf : all_inferiors ())
11933 if (inf->pspace == old_loc->pspace)
11934 {
11935 proc_target = inf->process_target ();
11936 break;
11937 }
11938 if (proc_target != nullptr)
11939 old_loc->events_till_retirement
11940 = 3 * (thread_count (proc_target) + 1);
11941 else
11942 old_loc->events_till_retirement = 1;
876fa593 11943 old_loc->owner = NULL;
20874c92 11944
1123588c 11945 moribund_locations.push_back (old_loc);
1c5cfe86
PA
11946 }
11947 else
f431efe5
PA
11948 {
11949 old_loc->owner = NULL;
11950 decref_bp_location (&old_loc);
11951 }
20874c92 11952 }
74960c60 11953 }
1c5cfe86 11954
348d480f
PA
11955 /* Rescan breakpoints at the same address and section, marking the
11956 first one as "first" and any others as "duplicates". This is so
11957 that the bpt instruction is only inserted once. If we have a
11958 permanent breakpoint at the same place as BPT, make that one the
11959 official one, and the rest as duplicates. Permanent breakpoints
11960 are sorted first for the same address.
11961
11962 Do the same for hardware watchpoints, but also considering the
11963 watchpoint's type (regular/access/read) and length. */
11964
11965 bp_loc_first = NULL;
11966 wp_loc_first = NULL;
11967 awp_loc_first = NULL;
11968 rwp_loc_first = NULL;
11969 ALL_BP_LOCATIONS (loc, locp)
11970 {
11971 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
11972 non-NULL. */
348d480f 11973 struct bp_location **loc_first_p;
d3fbdd86 11974 b = loc->owner;
348d480f 11975
6f380991 11976 if (!unduplicated_should_be_inserted (loc)
cb1e4e32 11977 || !bl_address_is_meaningful (loc)
1e4d1764
YQ
11978 /* Don't detect duplicate for tracepoint locations because they are
11979 never duplicated. See the comments in field `duplicate' of
11980 `struct bp_location'. */
348d480f 11981 || is_tracepoint (b))
b775012e
LM
11982 {
11983 /* Clear the condition modification flag. */
11984 loc->condition_changed = condition_unchanged;
11985 continue;
11986 }
348d480f 11987
348d480f
PA
11988 if (b->type == bp_hardware_watchpoint)
11989 loc_first_p = &wp_loc_first;
11990 else if (b->type == bp_read_watchpoint)
11991 loc_first_p = &rwp_loc_first;
11992 else if (b->type == bp_access_watchpoint)
11993 loc_first_p = &awp_loc_first;
11994 else
11995 loc_first_p = &bp_loc_first;
11996
11997 if (*loc_first_p == NULL
11998 || (overlay_debugging && loc->section != (*loc_first_p)->section)
11999 || !breakpoint_locations_match (loc, *loc_first_p))
12000 {
12001 *loc_first_p = loc;
12002 loc->duplicate = 0;
b775012e
LM
12003
12004 if (is_breakpoint (loc->owner) && loc->condition_changed)
12005 {
12006 loc->needs_update = 1;
12007 /* Clear the condition modification flag. */
12008 loc->condition_changed = condition_unchanged;
12009 }
348d480f
PA
12010 continue;
12011 }
12012
934709f0
PW
12013
12014 /* This and the above ensure the invariant that the first location
12015 is not duplicated, and is the inserted one.
12016 All following are marked as duplicated, and are not inserted. */
12017 if (loc->inserted)
12018 swap_insertion (loc, *loc_first_p);
348d480f
PA
12019 loc->duplicate = 1;
12020
b775012e
LM
12021 /* Clear the condition modification flag. */
12022 loc->condition_changed = condition_unchanged;
348d480f
PA
12023 }
12024
a25a5a45 12025 if (insert_mode == UGLL_INSERT || breakpoints_should_be_inserted_now ())
b775012e 12026 {
04086b45 12027 if (insert_mode != UGLL_DONT_INSERT)
b775012e
LM
12028 insert_breakpoint_locations ();
12029 else
12030 {
44702360
PA
12031 /* Even though the caller told us to not insert new
12032 locations, we may still need to update conditions on the
12033 target's side of breakpoints that were already inserted
12034 if the target is evaluating breakpoint conditions. We
b775012e
LM
12035 only update conditions for locations that are marked
12036 "needs_update". */
12037 update_inserted_breakpoint_locations ();
12038 }
12039 }
348d480f 12040
04086b45 12041 if (insert_mode != UGLL_DONT_INSERT)
1e4d1764 12042 download_tracepoint_locations ();
348d480f
PA
12043}
12044
12045void
12046breakpoint_retire_moribund (void)
12047{
1123588c
TT
12048 for (int ix = 0; ix < moribund_locations.size (); ++ix)
12049 {
12050 struct bp_location *loc = moribund_locations[ix];
12051 if (--(loc->events_till_retirement) == 0)
12052 {
12053 decref_bp_location (&loc);
12054 unordered_remove (moribund_locations, ix);
12055 --ix;
12056 }
12057 }
348d480f
PA
12058}
12059
12060static void
44702360 12061update_global_location_list_nothrow (enum ugll_insert_mode insert_mode)
348d480f 12062{
348d480f 12063
a70b8144 12064 try
492d29ea
PA
12065 {
12066 update_global_location_list (insert_mode);
12067 }
230d2906 12068 catch (const gdb_exception_error &e)
492d29ea
PA
12069 {
12070 }
348d480f
PA
12071}
12072
12073/* Clear BKP from a BPS. */
12074
12075static void
12076bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
12077{
12078 bpstat bs;
12079
12080 for (bs = bps; bs; bs = bs->next)
12081 if (bs->breakpoint_at == bpt)
12082 {
12083 bs->breakpoint_at = NULL;
12084 bs->old_val = NULL;
12085 /* bs->commands will be freed later. */
12086 }
12087}
12088
12089/* Callback for iterate_over_threads. */
12090static int
12091bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
12092{
9a3c8263 12093 struct breakpoint *bpt = (struct breakpoint *) data;
348d480f
PA
12094
12095 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
12096 return 0;
12097}
12098
12099/* Helper for breakpoint and tracepoint breakpoint_ops->mention
12100 callbacks. */
12101
12102static void
12103say_where (struct breakpoint *b)
12104{
12105 struct value_print_options opts;
12106
12107 get_user_print_options (&opts);
12108
12109 /* i18n: cagney/2005-02-11: Below needs to be merged into a
12110 single string. */
12111 if (b->loc == NULL)
12112 {
f00aae0f
KS
12113 /* For pending locations, the output differs slightly based
12114 on b->extra_string. If this is non-NULL, it contains either
12115 a condition or dprintf arguments. */
12116 if (b->extra_string == NULL)
12117 {
12118 printf_filtered (_(" (%s) pending."),
d28cd78a 12119 event_location_to_string (b->location.get ()));
f00aae0f
KS
12120 }
12121 else if (b->type == bp_dprintf)
12122 {
12123 printf_filtered (_(" (%s,%s) pending."),
d28cd78a 12124 event_location_to_string (b->location.get ()),
f00aae0f
KS
12125 b->extra_string);
12126 }
12127 else
12128 {
12129 printf_filtered (_(" (%s %s) pending."),
d28cd78a 12130 event_location_to_string (b->location.get ()),
f00aae0f
KS
12131 b->extra_string);
12132 }
348d480f
PA
12133 }
12134 else
12135 {
2f202fde 12136 if (opts.addressprint || b->loc->symtab == NULL)
6a831f06
PA
12137 printf_filtered (" at %ps",
12138 styled_string (address_style.style (),
12139 paddress (b->loc->gdbarch,
12140 b->loc->address)));
2f202fde 12141 if (b->loc->symtab != NULL)
f8eba3c6
TT
12142 {
12143 /* If there is a single location, we can print the location
12144 more nicely. */
12145 if (b->loc->next == NULL)
0bb296cb 12146 {
6a831f06
PA
12147 const char *filename
12148 = symtab_to_filename_for_display (b->loc->symtab);
12149 printf_filtered (": file %ps, line %d.",
12150 styled_string (file_name_style.style (),
12151 filename),
0bb296cb
TT
12152 b->loc->line_number);
12153 }
f8eba3c6
TT
12154 else
12155 /* This is not ideal, but each location may have a
12156 different file name, and this at least reflects the
12157 real situation somewhat. */
f00aae0f 12158 printf_filtered (": %s.",
d28cd78a 12159 event_location_to_string (b->location.get ()));
f8eba3c6 12160 }
348d480f
PA
12161
12162 if (b->loc->next)
12163 {
12164 struct bp_location *loc = b->loc;
12165 int n = 0;
12166 for (; loc; loc = loc->next)
12167 ++n;
12168 printf_filtered (" (%d locations)", n);
12169 }
12170 }
12171}
12172
5f486660 12173bp_location::~bp_location ()
348d480f 12174{
5f486660 12175 xfree (function_name);
348d480f
PA
12176}
12177
c1fc2657 12178/* Destructor for the breakpoint base class. */
348d480f 12179
c1fc2657 12180breakpoint::~breakpoint ()
348d480f 12181{
c1fc2657
SM
12182 xfree (this->cond_string);
12183 xfree (this->extra_string);
348d480f
PA
12184}
12185
2060206e
PA
12186static struct bp_location *
12187base_breakpoint_allocate_location (struct breakpoint *self)
348d480f 12188{
5f486660 12189 return new bp_location (self);
348d480f
PA
12190}
12191
2060206e
PA
12192static void
12193base_breakpoint_re_set (struct breakpoint *b)
12194{
12195 /* Nothing to re-set. */
12196}
12197
12198#define internal_error_pure_virtual_called() \
12199 gdb_assert_not_reached ("pure virtual function called")
12200
12201static int
12202base_breakpoint_insert_location (struct bp_location *bl)
12203{
12204 internal_error_pure_virtual_called ();
12205}
12206
12207static int
73971819
PA
12208base_breakpoint_remove_location (struct bp_location *bl,
12209 enum remove_bp_reason reason)
2060206e
PA
12210{
12211 internal_error_pure_virtual_called ();
12212}
12213
12214static int
12215base_breakpoint_breakpoint_hit (const struct bp_location *bl,
bd522513 12216 const address_space *aspace,
09ac7c10
TT
12217 CORE_ADDR bp_addr,
12218 const struct target_waitstatus *ws)
2060206e
PA
12219{
12220 internal_error_pure_virtual_called ();
12221}
12222
12223static void
12224base_breakpoint_check_status (bpstat bs)
12225{
12226 /* Always stop. */
12227}
12228
12229/* A "works_in_software_mode" breakpoint_ops method that just internal
12230 errors. */
12231
12232static int
12233base_breakpoint_works_in_software_mode (const struct breakpoint *b)
12234{
12235 internal_error_pure_virtual_called ();
12236}
12237
12238/* A "resources_needed" breakpoint_ops method that just internal
12239 errors. */
12240
12241static int
12242base_breakpoint_resources_needed (const struct bp_location *bl)
12243{
12244 internal_error_pure_virtual_called ();
12245}
12246
12247static enum print_stop_action
12248base_breakpoint_print_it (bpstat bs)
12249{
12250 internal_error_pure_virtual_called ();
12251}
12252
12253static void
12254base_breakpoint_print_one_detail (const struct breakpoint *self,
12255 struct ui_out *uiout)
12256{
12257 /* nothing */
12258}
12259
12260static void
12261base_breakpoint_print_mention (struct breakpoint *b)
12262{
12263 internal_error_pure_virtual_called ();
12264}
12265
12266static void
12267base_breakpoint_print_recreate (struct breakpoint *b, struct ui_file *fp)
12268{
12269 internal_error_pure_virtual_called ();
12270}
12271
983af33b 12272static void
f00aae0f
KS
12273base_breakpoint_create_sals_from_location
12274 (const struct event_location *location,
12275 struct linespec_result *canonical,
12276 enum bptype type_wanted)
983af33b
SDJ
12277{
12278 internal_error_pure_virtual_called ();
12279}
12280
12281static void
12282base_breakpoint_create_breakpoints_sal (struct gdbarch *gdbarch,
12283 struct linespec_result *c,
e1e01040
PA
12284 gdb::unique_xmalloc_ptr<char> cond_string,
12285 gdb::unique_xmalloc_ptr<char> extra_string,
983af33b
SDJ
12286 enum bptype type_wanted,
12287 enum bpdisp disposition,
12288 int thread,
12289 int task, int ignore_count,
12290 const struct breakpoint_ops *o,
12291 int from_tty, int enabled,
44f238bb 12292 int internal, unsigned flags)
983af33b
SDJ
12293{
12294 internal_error_pure_virtual_called ();
12295}
12296
6c5b2ebe 12297static std::vector<symtab_and_line>
f00aae0f
KS
12298base_breakpoint_decode_location (struct breakpoint *b,
12299 const struct event_location *location,
6c5b2ebe 12300 struct program_space *search_pspace)
983af33b
SDJ
12301{
12302 internal_error_pure_virtual_called ();
12303}
12304
ab04a2af
TT
12305/* The default 'explains_signal' method. */
12306
47591c29 12307static int
427cd150 12308base_breakpoint_explains_signal (struct breakpoint *b, enum gdb_signal sig)
ab04a2af 12309{
47591c29 12310 return 1;
ab04a2af
TT
12311}
12312
9d6e6e84
HZ
12313/* The default "after_condition_true" method. */
12314
12315static void
12316base_breakpoint_after_condition_true (struct bpstats *bs)
12317{
12318 /* Nothing to do. */
12319}
12320
ab04a2af 12321struct breakpoint_ops base_breakpoint_ops =
2060206e 12322{
2060206e
PA
12323 base_breakpoint_allocate_location,
12324 base_breakpoint_re_set,
12325 base_breakpoint_insert_location,
12326 base_breakpoint_remove_location,
12327 base_breakpoint_breakpoint_hit,
12328 base_breakpoint_check_status,
12329 base_breakpoint_resources_needed,
12330 base_breakpoint_works_in_software_mode,
12331 base_breakpoint_print_it,
12332 NULL,
12333 base_breakpoint_print_one_detail,
12334 base_breakpoint_print_mention,
983af33b 12335 base_breakpoint_print_recreate,
5f700d83 12336 base_breakpoint_create_sals_from_location,
983af33b 12337 base_breakpoint_create_breakpoints_sal,
5f700d83 12338 base_breakpoint_decode_location,
9d6e6e84
HZ
12339 base_breakpoint_explains_signal,
12340 base_breakpoint_after_condition_true,
2060206e
PA
12341};
12342
12343/* Default breakpoint_ops methods. */
12344
12345static void
348d480f
PA
12346bkpt_re_set (struct breakpoint *b)
12347{
06edf0c0 12348 /* FIXME: is this still reachable? */
9ef9e6a6 12349 if (breakpoint_event_location_empty_p (b))
06edf0c0 12350 {
f00aae0f 12351 /* Anything without a location can't be re-set. */
348d480f 12352 delete_breakpoint (b);
06edf0c0 12353 return;
348d480f 12354 }
06edf0c0
PA
12355
12356 breakpoint_re_set_default (b);
348d480f
PA
12357}
12358
2060206e 12359static int
348d480f
PA
12360bkpt_insert_location (struct bp_location *bl)
12361{
cd6c3b4f
YQ
12362 CORE_ADDR addr = bl->target_info.reqstd_address;
12363
579c6ad9 12364 bl->target_info.kind = breakpoint_kind (bl, &addr);
cd6c3b4f
YQ
12365 bl->target_info.placed_address = addr;
12366
348d480f 12367 if (bl->loc_type == bp_loc_hardware_breakpoint)
7c16b83e 12368 return target_insert_hw_breakpoint (bl->gdbarch, &bl->target_info);
348d480f 12369 else
7c16b83e 12370 return target_insert_breakpoint (bl->gdbarch, &bl->target_info);
348d480f
PA
12371}
12372
2060206e 12373static int
73971819 12374bkpt_remove_location (struct bp_location *bl, enum remove_bp_reason reason)
348d480f
PA
12375{
12376 if (bl->loc_type == bp_loc_hardware_breakpoint)
12377 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
12378 else
73971819 12379 return target_remove_breakpoint (bl->gdbarch, &bl->target_info, reason);
348d480f
PA
12380}
12381
2060206e 12382static int
348d480f 12383bkpt_breakpoint_hit (const struct bp_location *bl,
bd522513 12384 const address_space *aspace, CORE_ADDR bp_addr,
09ac7c10 12385 const struct target_waitstatus *ws)
348d480f 12386{
09ac7c10 12387 if (ws->kind != TARGET_WAITKIND_STOPPED
a493e3e2 12388 || ws->value.sig != GDB_SIGNAL_TRAP)
09ac7c10
TT
12389 return 0;
12390
348d480f
PA
12391 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
12392 aspace, bp_addr))
12393 return 0;
12394
12395 if (overlay_debugging /* unmapped overlay section */
12396 && section_is_overlay (bl->section)
12397 && !section_is_mapped (bl->section))
12398 return 0;
12399
12400 return 1;
12401}
12402
cd1608cc
PA
12403static int
12404dprintf_breakpoint_hit (const struct bp_location *bl,
bd522513 12405 const address_space *aspace, CORE_ADDR bp_addr,
cd1608cc
PA
12406 const struct target_waitstatus *ws)
12407{
12408 if (dprintf_style == dprintf_style_agent
12409 && target_can_run_breakpoint_commands ())
12410 {
12411 /* An agent-style dprintf never causes a stop. If we see a trap
12412 for this address it must be for a breakpoint that happens to
12413 be set at the same address. */
12414 return 0;
12415 }
12416
12417 return bkpt_breakpoint_hit (bl, aspace, bp_addr, ws);
12418}
12419
2060206e 12420static int
348d480f
PA
12421bkpt_resources_needed (const struct bp_location *bl)
12422{
12423 gdb_assert (bl->owner->type == bp_hardware_breakpoint);
12424
12425 return 1;
12426}
12427
2060206e 12428static enum print_stop_action
348d480f
PA
12429bkpt_print_it (bpstat bs)
12430{
348d480f
PA
12431 struct breakpoint *b;
12432 const struct bp_location *bl;
001c8c33 12433 int bp_temp;
79a45e25 12434 struct ui_out *uiout = current_uiout;
348d480f
PA
12435
12436 gdb_assert (bs->bp_location_at != NULL);
12437
12438 bl = bs->bp_location_at;
12439 b = bs->breakpoint_at;
12440
001c8c33
PA
12441 bp_temp = b->disposition == disp_del;
12442 if (bl->address != bl->requested_address)
12443 breakpoint_adjustment_warning (bl->requested_address,
12444 bl->address,
12445 b->number, 1);
12446 annotate_breakpoint (b->number);
f303dbd6
PA
12447 maybe_print_thread_hit_breakpoint (uiout);
12448
112e8700 12449 if (uiout->is_mi_like_p ())
348d480f 12450 {
112e8700 12451 uiout->field_string ("reason",
001c8c33 12452 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
112e8700 12453 uiout->field_string ("disp", bpdisp_text (b->disposition));
06edf0c0 12454 }
6a831f06
PA
12455 if (bp_temp)
12456 uiout->message ("Temporary breakpoint %pF, ",
12457 signed_field ("bkptno", b->number));
12458 else
12459 uiout->message ("Breakpoint %pF, ",
12460 signed_field ("bkptno", b->number));
06edf0c0 12461
001c8c33 12462 return PRINT_SRC_AND_LOC;
06edf0c0
PA
12463}
12464
2060206e 12465static void
06edf0c0
PA
12466bkpt_print_mention (struct breakpoint *b)
12467{
112e8700 12468 if (current_uiout->is_mi_like_p ())
06edf0c0
PA
12469 return;
12470
12471 switch (b->type)
12472 {
12473 case bp_breakpoint:
12474 case bp_gnu_ifunc_resolver:
12475 if (b->disposition == disp_del)
12476 printf_filtered (_("Temporary breakpoint"));
12477 else
12478 printf_filtered (_("Breakpoint"));
12479 printf_filtered (_(" %d"), b->number);
12480 if (b->type == bp_gnu_ifunc_resolver)
12481 printf_filtered (_(" at gnu-indirect-function resolver"));
12482 break;
12483 case bp_hardware_breakpoint:
12484 printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
12485 break;
e7e0cddf
SS
12486 case bp_dprintf:
12487 printf_filtered (_("Dprintf %d"), b->number);
12488 break;
06edf0c0
PA
12489 }
12490
12491 say_where (b);
12492}
12493
2060206e 12494static void
06edf0c0
PA
12495bkpt_print_recreate (struct breakpoint *tp, struct ui_file *fp)
12496{
12497 if (tp->type == bp_breakpoint && tp->disposition == disp_del)
12498 fprintf_unfiltered (fp, "tbreak");
12499 else if (tp->type == bp_breakpoint)
12500 fprintf_unfiltered (fp, "break");
12501 else if (tp->type == bp_hardware_breakpoint
12502 && tp->disposition == disp_del)
12503 fprintf_unfiltered (fp, "thbreak");
12504 else if (tp->type == bp_hardware_breakpoint)
12505 fprintf_unfiltered (fp, "hbreak");
12506 else
12507 internal_error (__FILE__, __LINE__,
12508 _("unhandled breakpoint type %d"), (int) tp->type);
12509
f00aae0f 12510 fprintf_unfiltered (fp, " %s",
d28cd78a 12511 event_location_to_string (tp->location.get ()));
f00aae0f
KS
12512
12513 /* Print out extra_string if this breakpoint is pending. It might
12514 contain, for example, conditions that were set by the user. */
12515 if (tp->loc == NULL && tp->extra_string != NULL)
12516 fprintf_unfiltered (fp, " %s", tp->extra_string);
12517
dd11a36c 12518 print_recreate_thread (tp, fp);
06edf0c0
PA
12519}
12520
983af33b 12521static void
f00aae0f
KS
12522bkpt_create_sals_from_location (const struct event_location *location,
12523 struct linespec_result *canonical,
12524 enum bptype type_wanted)
983af33b 12525{
f00aae0f 12526 create_sals_from_location_default (location, canonical, type_wanted);
983af33b
SDJ
12527}
12528
12529static void
12530bkpt_create_breakpoints_sal (struct gdbarch *gdbarch,
12531 struct linespec_result *canonical,
e1e01040
PA
12532 gdb::unique_xmalloc_ptr<char> cond_string,
12533 gdb::unique_xmalloc_ptr<char> extra_string,
983af33b
SDJ
12534 enum bptype type_wanted,
12535 enum bpdisp disposition,
12536 int thread,
12537 int task, int ignore_count,
12538 const struct breakpoint_ops *ops,
12539 int from_tty, int enabled,
44f238bb 12540 int internal, unsigned flags)
983af33b 12541{
023fa29b 12542 create_breakpoints_sal_default (gdbarch, canonical,
e1e01040
PA
12543 std::move (cond_string),
12544 std::move (extra_string),
e7e0cddf 12545 type_wanted,
983af33b
SDJ
12546 disposition, thread, task,
12547 ignore_count, ops, from_tty,
44f238bb 12548 enabled, internal, flags);
983af33b
SDJ
12549}
12550
6c5b2ebe 12551static std::vector<symtab_and_line>
f00aae0f
KS
12552bkpt_decode_location (struct breakpoint *b,
12553 const struct event_location *location,
6c5b2ebe 12554 struct program_space *search_pspace)
983af33b 12555{
6c5b2ebe 12556 return decode_location_default (b, location, search_pspace);
983af33b
SDJ
12557}
12558
06edf0c0
PA
12559/* Virtual table for internal breakpoints. */
12560
12561static void
12562internal_bkpt_re_set (struct breakpoint *b)
12563{
12564 switch (b->type)
12565 {
12566 /* Delete overlay event and longjmp master breakpoints; they
12567 will be reset later by breakpoint_re_set. */
12568 case bp_overlay_event:
12569 case bp_longjmp_master:
12570 case bp_std_terminate_master:
12571 case bp_exception_master:
12572 delete_breakpoint (b);
12573 break;
12574
12575 /* This breakpoint is special, it's set up when the inferior
12576 starts and we really don't want to touch it. */
12577 case bp_shlib_event:
12578
12579 /* Like bp_shlib_event, this breakpoint type is special. Once
12580 it is set up, we do not want to touch it. */
12581 case bp_thread_event:
12582 break;
12583 }
12584}
12585
12586static void
12587internal_bkpt_check_status (bpstat bs)
12588{
a9b3a50f
PA
12589 if (bs->breakpoint_at->type == bp_shlib_event)
12590 {
12591 /* If requested, stop when the dynamic linker notifies GDB of
12592 events. This allows the user to get control and place
12593 breakpoints in initializer routines for dynamically loaded
12594 objects (among other things). */
12595 bs->stop = stop_on_solib_events;
12596 bs->print = stop_on_solib_events;
12597 }
12598 else
12599 bs->stop = 0;
06edf0c0
PA
12600}
12601
12602static enum print_stop_action
12603internal_bkpt_print_it (bpstat bs)
12604{
06edf0c0 12605 struct breakpoint *b;
06edf0c0 12606
06edf0c0
PA
12607 b = bs->breakpoint_at;
12608
06edf0c0
PA
12609 switch (b->type)
12610 {
348d480f
PA
12611 case bp_shlib_event:
12612 /* Did we stop because the user set the stop_on_solib_events
12613 variable? (If so, we report this as a generic, "Stopped due
12614 to shlib event" message.) */
edcc5120 12615 print_solib_event (0);
348d480f
PA
12616 break;
12617
12618 case bp_thread_event:
12619 /* Not sure how we will get here.
12620 GDB should not stop for these breakpoints. */
12621 printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
12622 break;
12623
12624 case bp_overlay_event:
12625 /* By analogy with the thread event, GDB should not stop for these. */
12626 printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
12627 break;
12628
12629 case bp_longjmp_master:
12630 /* These should never be enabled. */
12631 printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
348d480f
PA
12632 break;
12633
12634 case bp_std_terminate_master:
12635 /* These should never be enabled. */
12636 printf_filtered (_("std::terminate Master Breakpoint: "
12637 "gdb should not stop!\n"));
348d480f
PA
12638 break;
12639
12640 case bp_exception_master:
12641 /* These should never be enabled. */
12642 printf_filtered (_("Exception Master Breakpoint: "
12643 "gdb should not stop!\n"));
06edf0c0
PA
12644 break;
12645 }
12646
001c8c33 12647 return PRINT_NOTHING;
06edf0c0
PA
12648}
12649
12650static void
12651internal_bkpt_print_mention (struct breakpoint *b)
12652{
12653 /* Nothing to mention. These breakpoints are internal. */
12654}
12655
06edf0c0
PA
12656/* Virtual table for momentary breakpoints */
12657
12658static void
12659momentary_bkpt_re_set (struct breakpoint *b)
12660{
12661 /* Keep temporary breakpoints, which can be encountered when we step
4d1eb6b4 12662 over a dlopen call and solib_add is resetting the breakpoints.
06edf0c0
PA
12663 Otherwise these should have been blown away via the cleanup chain
12664 or by breakpoint_init_inferior when we rerun the executable. */
12665}
12666
12667static void
12668momentary_bkpt_check_status (bpstat bs)
12669{
12670 /* Nothing. The point of these breakpoints is causing a stop. */
12671}
12672
12673static enum print_stop_action
12674momentary_bkpt_print_it (bpstat bs)
12675{
001c8c33 12676 return PRINT_UNKNOWN;
348d480f
PA
12677}
12678
06edf0c0
PA
12679static void
12680momentary_bkpt_print_mention (struct breakpoint *b)
348d480f 12681{
06edf0c0 12682 /* Nothing to mention. These breakpoints are internal. */
348d480f
PA
12683}
12684
e2e4d78b
JK
12685/* Ensure INITIATING_FRAME is cleared when no such breakpoint exists.
12686
12687 It gets cleared already on the removal of the first one of such placed
12688 breakpoints. This is OK as they get all removed altogether. */
12689
c1fc2657 12690longjmp_breakpoint::~longjmp_breakpoint ()
e2e4d78b 12691{
c1fc2657 12692 thread_info *tp = find_thread_global_id (this->thread);
e2e4d78b 12693
c1fc2657 12694 if (tp != NULL)
e2e4d78b 12695 tp->initiating_frame = null_frame_id;
e2e4d78b
JK
12696}
12697
55aa24fb
SDJ
12698/* Specific methods for probe breakpoints. */
12699
12700static int
12701bkpt_probe_insert_location (struct bp_location *bl)
12702{
12703 int v = bkpt_insert_location (bl);
12704
12705 if (v == 0)
12706 {
12707 /* The insertion was successful, now let's set the probe's semaphore
12708 if needed. */
935676c9 12709 bl->probe.prob->set_semaphore (bl->probe.objfile, bl->gdbarch);
55aa24fb
SDJ
12710 }
12711
12712 return v;
12713}
12714
12715static int
73971819
PA
12716bkpt_probe_remove_location (struct bp_location *bl,
12717 enum remove_bp_reason reason)
55aa24fb
SDJ
12718{
12719 /* Let's clear the semaphore before removing the location. */
935676c9 12720 bl->probe.prob->clear_semaphore (bl->probe.objfile, bl->gdbarch);
55aa24fb 12721
73971819 12722 return bkpt_remove_location (bl, reason);
55aa24fb
SDJ
12723}
12724
12725static void
f00aae0f 12726bkpt_probe_create_sals_from_location (const struct event_location *location,
5f700d83 12727 struct linespec_result *canonical,
f00aae0f 12728 enum bptype type_wanted)
55aa24fb
SDJ
12729{
12730 struct linespec_sals lsal;
12731
c2f4122d 12732 lsal.sals = parse_probes (location, NULL, canonical);
8e9e35b1
TT
12733 lsal.canonical
12734 = xstrdup (event_location_to_string (canonical->location.get ()));
6c5b2ebe 12735 canonical->lsals.push_back (std::move (lsal));
55aa24fb
SDJ
12736}
12737
6c5b2ebe 12738static std::vector<symtab_and_line>
f00aae0f
KS
12739bkpt_probe_decode_location (struct breakpoint *b,
12740 const struct event_location *location,
6c5b2ebe 12741 struct program_space *search_pspace)
55aa24fb 12742{
6c5b2ebe
PA
12743 std::vector<symtab_and_line> sals = parse_probes (location, search_pspace, NULL);
12744 if (sals.empty ())
55aa24fb 12745 error (_("probe not found"));
6c5b2ebe 12746 return sals;
55aa24fb
SDJ
12747}
12748
348d480f 12749/* The breakpoint_ops structure to be used in tracepoints. */
876fa593 12750
348d480f
PA
12751static void
12752tracepoint_re_set (struct breakpoint *b)
12753{
12754 breakpoint_re_set_default (b);
12755}
876fa593 12756
348d480f
PA
12757static int
12758tracepoint_breakpoint_hit (const struct bp_location *bl,
bd522513 12759 const address_space *aspace, CORE_ADDR bp_addr,
09ac7c10 12760 const struct target_waitstatus *ws)
348d480f
PA
12761{
12762 /* By definition, the inferior does not report stops at
12763 tracepoints. */
12764 return 0;
74960c60
VP
12765}
12766
12767static void
348d480f
PA
12768tracepoint_print_one_detail (const struct breakpoint *self,
12769 struct ui_out *uiout)
74960c60 12770{
d9b3f62e 12771 struct tracepoint *tp = (struct tracepoint *) self;
5d9310c4 12772 if (!tp->static_trace_marker_id.empty ())
348d480f
PA
12773 {
12774 gdb_assert (self->type == bp_static_tracepoint);
cc59ec59 12775
6a831f06
PA
12776 uiout->message ("\tmarker id is %pF\n",
12777 string_field ("static-tracepoint-marker-string-id",
12778 tp->static_trace_marker_id.c_str ()));
348d480f 12779 }
0d381245
VP
12780}
12781
a474d7c2 12782static void
348d480f 12783tracepoint_print_mention (struct breakpoint *b)
a474d7c2 12784{
112e8700 12785 if (current_uiout->is_mi_like_p ())
348d480f 12786 return;
cc59ec59 12787
348d480f
PA
12788 switch (b->type)
12789 {
12790 case bp_tracepoint:
12791 printf_filtered (_("Tracepoint"));
12792 printf_filtered (_(" %d"), b->number);
12793 break;
12794 case bp_fast_tracepoint:
12795 printf_filtered (_("Fast tracepoint"));
12796 printf_filtered (_(" %d"), b->number);
12797 break;
12798 case bp_static_tracepoint:
12799 printf_filtered (_("Static tracepoint"));
12800 printf_filtered (_(" %d"), b->number);
12801 break;
12802 default:
12803 internal_error (__FILE__, __LINE__,
12804 _("unhandled tracepoint type %d"), (int) b->type);
12805 }
12806
12807 say_where (b);
a474d7c2
PA
12808}
12809
348d480f 12810static void
d9b3f62e 12811tracepoint_print_recreate (struct breakpoint *self, struct ui_file *fp)
a474d7c2 12812{
d9b3f62e
PA
12813 struct tracepoint *tp = (struct tracepoint *) self;
12814
12815 if (self->type == bp_fast_tracepoint)
348d480f 12816 fprintf_unfiltered (fp, "ftrace");
c93e8391 12817 else if (self->type == bp_static_tracepoint)
348d480f 12818 fprintf_unfiltered (fp, "strace");
d9b3f62e 12819 else if (self->type == bp_tracepoint)
348d480f
PA
12820 fprintf_unfiltered (fp, "trace");
12821 else
12822 internal_error (__FILE__, __LINE__,
d9b3f62e 12823 _("unhandled tracepoint type %d"), (int) self->type);
cc59ec59 12824
f00aae0f 12825 fprintf_unfiltered (fp, " %s",
d28cd78a 12826 event_location_to_string (self->location.get ()));
d9b3f62e
PA
12827 print_recreate_thread (self, fp);
12828
12829 if (tp->pass_count)
12830 fprintf_unfiltered (fp, " passcount %d\n", tp->pass_count);
a474d7c2
PA
12831}
12832
983af33b 12833static void
f00aae0f
KS
12834tracepoint_create_sals_from_location (const struct event_location *location,
12835 struct linespec_result *canonical,
12836 enum bptype type_wanted)
983af33b 12837{
f00aae0f 12838 create_sals_from_location_default (location, canonical, type_wanted);
983af33b
SDJ
12839}
12840
12841static void
12842tracepoint_create_breakpoints_sal (struct gdbarch *gdbarch,
12843 struct linespec_result *canonical,
e1e01040
PA
12844 gdb::unique_xmalloc_ptr<char> cond_string,
12845 gdb::unique_xmalloc_ptr<char> extra_string,
983af33b
SDJ
12846 enum bptype type_wanted,
12847 enum bpdisp disposition,
12848 int thread,
12849 int task, int ignore_count,
12850 const struct breakpoint_ops *ops,
12851 int from_tty, int enabled,
44f238bb 12852 int internal, unsigned flags)
983af33b 12853{
023fa29b 12854 create_breakpoints_sal_default (gdbarch, canonical,
e1e01040
PA
12855 std::move (cond_string),
12856 std::move (extra_string),
e7e0cddf 12857 type_wanted,
983af33b
SDJ
12858 disposition, thread, task,
12859 ignore_count, ops, from_tty,
44f238bb 12860 enabled, internal, flags);
983af33b
SDJ
12861}
12862
6c5b2ebe 12863static std::vector<symtab_and_line>
f00aae0f
KS
12864tracepoint_decode_location (struct breakpoint *b,
12865 const struct event_location *location,
6c5b2ebe 12866 struct program_space *search_pspace)
983af33b 12867{
6c5b2ebe 12868 return decode_location_default (b, location, search_pspace);
983af33b
SDJ
12869}
12870
2060206e 12871struct breakpoint_ops tracepoint_breakpoint_ops;
348d480f 12872
bac7c5cf 12873/* Virtual table for tracepoints on static probes. */
55aa24fb
SDJ
12874
12875static void
f00aae0f
KS
12876tracepoint_probe_create_sals_from_location
12877 (const struct event_location *location,
12878 struct linespec_result *canonical,
12879 enum bptype type_wanted)
55aa24fb
SDJ
12880{
12881 /* We use the same method for breakpoint on probes. */
f00aae0f 12882 bkpt_probe_create_sals_from_location (location, canonical, type_wanted);
55aa24fb
SDJ
12883}
12884
6c5b2ebe 12885static std::vector<symtab_and_line>
f00aae0f
KS
12886tracepoint_probe_decode_location (struct breakpoint *b,
12887 const struct event_location *location,
6c5b2ebe 12888 struct program_space *search_pspace)
55aa24fb
SDJ
12889{
12890 /* We use the same method for breakpoint on probes. */
6c5b2ebe 12891 return bkpt_probe_decode_location (b, location, search_pspace);
55aa24fb
SDJ
12892}
12893
5c2b4418
HZ
12894/* Dprintf breakpoint_ops methods. */
12895
12896static void
12897dprintf_re_set (struct breakpoint *b)
12898{
12899 breakpoint_re_set_default (b);
12900
f00aae0f
KS
12901 /* extra_string should never be non-NULL for dprintf. */
12902 gdb_assert (b->extra_string != NULL);
5c2b4418
HZ
12903
12904 /* 1 - connect to target 1, that can run breakpoint commands.
12905 2 - create a dprintf, which resolves fine.
12906 3 - disconnect from target 1
12907 4 - connect to target 2, that can NOT run breakpoint commands.
12908
12909 After steps #3/#4, you'll want the dprintf command list to
12910 be updated, because target 1 and 2 may well return different
12911 answers for target_can_run_breakpoint_commands().
12912 Given absence of finer grained resetting, we get to do
12913 it all the time. */
12914 if (b->extra_string != NULL)
12915 update_dprintf_command_list (b);
12916}
12917
2d9442cc
HZ
12918/* Implement the "print_recreate" breakpoint_ops method for dprintf. */
12919
12920static void
12921dprintf_print_recreate (struct breakpoint *tp, struct ui_file *fp)
12922{
f00aae0f 12923 fprintf_unfiltered (fp, "dprintf %s,%s",
d28cd78a 12924 event_location_to_string (tp->location.get ()),
2d9442cc
HZ
12925 tp->extra_string);
12926 print_recreate_thread (tp, fp);
12927}
12928
9d6e6e84
HZ
12929/* Implement the "after_condition_true" breakpoint_ops method for
12930 dprintf.
12931
12932 dprintf's are implemented with regular commands in their command
12933 list, but we run the commands here instead of before presenting the
12934 stop to the user, as dprintf's don't actually cause a stop. This
12935 also makes it so that the commands of multiple dprintfs at the same
12936 address are all handled. */
12937
12938static void
12939dprintf_after_condition_true (struct bpstats *bs)
12940{
04afa70c 12941 struct bpstats tmp_bs;
9d6e6e84
HZ
12942 struct bpstats *tmp_bs_p = &tmp_bs;
12943
12944 /* dprintf's never cause a stop. This wasn't set in the
12945 check_status hook instead because that would make the dprintf's
12946 condition not be evaluated. */
12947 bs->stop = 0;
12948
12949 /* Run the command list here. Take ownership of it instead of
12950 copying. We never want these commands to run later in
12951 bpstat_do_actions, if a breakpoint that causes a stop happens to
12952 be set at same address as this dprintf, or even if running the
12953 commands here throws. */
12954 tmp_bs.commands = bs->commands;
12955 bs->commands = NULL;
9d6e6e84
HZ
12956
12957 bpstat_do_actions_1 (&tmp_bs_p);
12958
12959 /* 'tmp_bs.commands' will usually be NULL by now, but
12960 bpstat_do_actions_1 may return early without processing the whole
12961 list. */
9d6e6e84
HZ
12962}
12963
983af33b
SDJ
12964/* The breakpoint_ops structure to be used on static tracepoints with
12965 markers (`-m'). */
12966
12967static void
f00aae0f 12968strace_marker_create_sals_from_location (const struct event_location *location,
5f700d83 12969 struct linespec_result *canonical,
f00aae0f 12970 enum bptype type_wanted)
983af33b
SDJ
12971{
12972 struct linespec_sals lsal;
f00aae0f 12973 const char *arg_start, *arg;
983af33b 12974
a20714ff 12975 arg = arg_start = get_linespec_location (location)->spec_string;
f00aae0f 12976 lsal.sals = decode_static_tracepoint_spec (&arg);
983af33b 12977
f2fc3015
TT
12978 std::string str (arg_start, arg - arg_start);
12979 const char *ptr = str.c_str ();
a20714ff
PA
12980 canonical->location
12981 = new_linespec_location (&ptr, symbol_name_match_type::FULL);
983af33b 12982
8e9e35b1
TT
12983 lsal.canonical
12984 = xstrdup (event_location_to_string (canonical->location.get ()));
6c5b2ebe 12985 canonical->lsals.push_back (std::move (lsal));
983af33b
SDJ
12986}
12987
12988static void
12989strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
12990 struct linespec_result *canonical,
e1e01040
PA
12991 gdb::unique_xmalloc_ptr<char> cond_string,
12992 gdb::unique_xmalloc_ptr<char> extra_string,
983af33b
SDJ
12993 enum bptype type_wanted,
12994 enum bpdisp disposition,
12995 int thread,
12996 int task, int ignore_count,
12997 const struct breakpoint_ops *ops,
12998 int from_tty, int enabled,
44f238bb 12999 int internal, unsigned flags)
983af33b 13000{
6c5b2ebe 13001 const linespec_sals &lsal = canonical->lsals[0];
983af33b
SDJ
13002
13003 /* If the user is creating a static tracepoint by marker id
13004 (strace -m MARKER_ID), then store the sals index, so that
13005 breakpoint_re_set can try to match up which of the newly
13006 found markers corresponds to this one, and, don't try to
13007 expand multiple locations for each sal, given than SALS
13008 already should contain all sals for MARKER_ID. */
13009
6c5b2ebe 13010 for (size_t i = 0; i < lsal.sals.size (); i++)
983af33b 13011 {
6c5b2ebe
PA
13012 event_location_up location
13013 = copy_event_location (canonical->location.get ());
983af33b 13014
b270e6f9 13015 std::unique_ptr<tracepoint> tp (new tracepoint ());
6c5b2ebe 13016 init_breakpoint_sal (tp.get (), gdbarch, lsal.sals[i],
ffc2605c 13017 std::move (location), NULL,
e1e01040
PA
13018 std::move (cond_string),
13019 std::move (extra_string),
e7e0cddf 13020 type_wanted, disposition,
983af33b 13021 thread, task, ignore_count, ops,
44f238bb 13022 from_tty, enabled, internal, flags,
983af33b
SDJ
13023 canonical->special_display);
13024 /* Given that its possible to have multiple markers with
13025 the same string id, if the user is creating a static
13026 tracepoint by marker id ("strace -m MARKER_ID"), then
13027 store the sals index, so that breakpoint_re_set can
13028 try to match up which of the newly found markers
13029 corresponds to this one */
13030 tp->static_trace_marker_id_idx = i;
13031
b270e6f9 13032 install_breakpoint (internal, std::move (tp), 0);
983af33b
SDJ
13033 }
13034}
13035
6c5b2ebe 13036static std::vector<symtab_and_line>
f00aae0f
KS
13037strace_marker_decode_location (struct breakpoint *b,
13038 const struct event_location *location,
6c5b2ebe 13039 struct program_space *search_pspace)
983af33b
SDJ
13040{
13041 struct tracepoint *tp = (struct tracepoint *) b;
a20714ff 13042 const char *s = get_linespec_location (location)->spec_string;
983af33b 13043
6c5b2ebe
PA
13044 std::vector<symtab_and_line> sals = decode_static_tracepoint_spec (&s);
13045 if (sals.size () > tp->static_trace_marker_id_idx)
983af33b 13046 {
6c5b2ebe
PA
13047 sals[0] = sals[tp->static_trace_marker_id_idx];
13048 sals.resize (1);
13049 return sals;
983af33b
SDJ
13050 }
13051 else
5d9310c4 13052 error (_("marker %s not found"), tp->static_trace_marker_id.c_str ());
983af33b
SDJ
13053}
13054
13055static struct breakpoint_ops strace_marker_breakpoint_ops;
13056
13057static int
13058strace_marker_p (struct breakpoint *b)
13059{
13060 return b->ops == &strace_marker_breakpoint_ops;
13061}
13062
53a5351d 13063/* Delete a breakpoint and clean up all traces of it in the data
f431efe5 13064 structures. */
c906108c
SS
13065
13066void
fba45db2 13067delete_breakpoint (struct breakpoint *bpt)
c906108c 13068{
52f0bd74 13069 struct breakpoint *b;
c906108c 13070
8a3fe4f8 13071 gdb_assert (bpt != NULL);
c906108c 13072
4a64f543
MS
13073 /* Has this bp already been deleted? This can happen because
13074 multiple lists can hold pointers to bp's. bpstat lists are
13075 especial culprits.
13076
13077 One example of this happening is a watchpoint's scope bp. When
13078 the scope bp triggers, we notice that the watchpoint is out of
13079 scope, and delete it. We also delete its scope bp. But the
13080 scope bp is marked "auto-deleting", and is already on a bpstat.
13081 That bpstat is then checked for auto-deleting bp's, which are
13082 deleted.
13083
13084 A real solution to this problem might involve reference counts in
13085 bp's, and/or giving them pointers back to their referencing
13086 bpstat's, and teaching delete_breakpoint to only free a bp's
13087 storage when no more references were extent. A cheaper bandaid
13088 was chosen. */
c906108c
SS
13089 if (bpt->type == bp_none)
13090 return;
13091
4a64f543
MS
13092 /* At least avoid this stale reference until the reference counting
13093 of breakpoints gets resolved. */
d0fb5eae 13094 if (bpt->related_breakpoint != bpt)
e5a0a904 13095 {
d0fb5eae 13096 struct breakpoint *related;
3a5c3e22 13097 struct watchpoint *w;
d0fb5eae
JK
13098
13099 if (bpt->type == bp_watchpoint_scope)
3a5c3e22 13100 w = (struct watchpoint *) bpt->related_breakpoint;
d0fb5eae 13101 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
3a5c3e22
PA
13102 w = (struct watchpoint *) bpt;
13103 else
13104 w = NULL;
13105 if (w != NULL)
13106 watchpoint_del_at_next_stop (w);
d0fb5eae
JK
13107
13108 /* Unlink bpt from the bpt->related_breakpoint ring. */
13109 for (related = bpt; related->related_breakpoint != bpt;
13110 related = related->related_breakpoint);
13111 related->related_breakpoint = bpt->related_breakpoint;
13112 bpt->related_breakpoint = bpt;
e5a0a904
JK
13113 }
13114
a9634178
TJB
13115 /* watch_command_1 creates a watchpoint but only sets its number if
13116 update_watchpoint succeeds in creating its bp_locations. If there's
13117 a problem in that process, we'll be asked to delete the half-created
13118 watchpoint. In that case, don't announce the deletion. */
13119 if (bpt->number)
76727919 13120 gdb::observers::breakpoint_deleted.notify (bpt);
c906108c 13121
c906108c
SS
13122 if (breakpoint_chain == bpt)
13123 breakpoint_chain = bpt->next;
13124
c906108c
SS
13125 ALL_BREAKPOINTS (b)
13126 if (b->next == bpt)
c5aa993b
JM
13127 {
13128 b->next = bpt->next;
13129 break;
13130 }
c906108c 13131
f431efe5
PA
13132 /* Be sure no bpstat's are pointing at the breakpoint after it's
13133 been freed. */
13134 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
e5dd4106 13135 in all threads for now. Note that we cannot just remove bpstats
f431efe5
PA
13136 pointing at bpt from the stop_bpstat list entirely, as breakpoint
13137 commands are associated with the bpstat; if we remove it here,
13138 then the later call to bpstat_do_actions (&stop_bpstat); in
13139 event-top.c won't do anything, and temporary breakpoints with
13140 commands won't work. */
13141
13142 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
13143
4a64f543
MS
13144 /* Now that breakpoint is removed from breakpoint list, update the
13145 global location list. This will remove locations that used to
13146 belong to this breakpoint. Do this before freeing the breakpoint
13147 itself, since remove_breakpoint looks at location's owner. It
13148 might be better design to have location completely
13149 self-contained, but it's not the case now. */
44702360 13150 update_global_location_list (UGLL_DONT_INSERT);
74960c60 13151
4a64f543
MS
13152 /* On the chance that someone will soon try again to delete this
13153 same bp, we mark it as deleted before freeing its storage. */
c906108c 13154 bpt->type = bp_none;
4d01a485 13155 delete bpt;
c906108c
SS
13156}
13157
51be5b68
PA
13158/* Iterator function to call a user-provided callback function once
13159 for each of B and its related breakpoints. */
13160
13161static void
13162iterate_over_related_breakpoints (struct breakpoint *b,
48649e1b 13163 gdb::function_view<void (breakpoint *)> function)
51be5b68
PA
13164{
13165 struct breakpoint *related;
13166
13167 related = b;
13168 do
13169 {
13170 struct breakpoint *next;
13171
13172 /* FUNCTION may delete RELATED. */
13173 next = related->related_breakpoint;
13174
13175 if (next == related)
13176 {
13177 /* RELATED is the last ring entry. */
48649e1b 13178 function (related);
51be5b68
PA
13179
13180 /* FUNCTION may have deleted it, so we'd never reach back to
13181 B. There's nothing left to do anyway, so just break
13182 out. */
13183 break;
13184 }
13185 else
48649e1b 13186 function (related);
51be5b68
PA
13187
13188 related = next;
13189 }
13190 while (related != b);
13191}
95a42b64 13192
4495129a 13193static void
981a3fb3 13194delete_command (const char *arg, int from_tty)
c906108c 13195{
35df4500 13196 struct breakpoint *b, *b_tmp;
c906108c 13197
ea9365bb
TT
13198 dont_repeat ();
13199
c906108c
SS
13200 if (arg == 0)
13201 {
13202 int breaks_to_delete = 0;
13203
46c6471b
PA
13204 /* Delete all breakpoints if no argument. Do not delete
13205 internal breakpoints, these have to be deleted with an
13206 explicit breakpoint number argument. */
c5aa993b 13207 ALL_BREAKPOINTS (b)
46c6471b 13208 if (user_breakpoint_p (b))
973d738b
DJ
13209 {
13210 breaks_to_delete = 1;
13211 break;
13212 }
c906108c
SS
13213
13214 /* Ask user only if there are some breakpoints to delete. */
13215 if (!from_tty
e2e0b3e5 13216 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
c906108c 13217 {
35df4500 13218 ALL_BREAKPOINTS_SAFE (b, b_tmp)
46c6471b 13219 if (user_breakpoint_p (b))
c5aa993b 13220 delete_breakpoint (b);
c906108c
SS
13221 }
13222 }
13223 else
48649e1b 13224 map_breakpoint_numbers
b926417a 13225 (arg, [&] (breakpoint *br)
48649e1b 13226 {
b926417a 13227 iterate_over_related_breakpoints (br, delete_breakpoint);
48649e1b 13228 });
c906108c
SS
13229}
13230
c2f4122d
PA
13231/* Return true if all locations of B bound to PSPACE are pending. If
13232 PSPACE is NULL, all locations of all program spaces are
13233 considered. */
13234
0d381245 13235static int
c2f4122d 13236all_locations_are_pending (struct breakpoint *b, struct program_space *pspace)
fe3f5fa8 13237{
c2f4122d
PA
13238 struct bp_location *loc;
13239
13240 for (loc = b->loc; loc != NULL; loc = loc->next)
13241 if ((pspace == NULL
13242 || loc->pspace == pspace)
13243 && !loc->shlib_disabled
8645ff69 13244 && !loc->pspace->executing_startup)
0d381245
VP
13245 return 0;
13246 return 1;
fe3f5fa8
VP
13247}
13248
776592bf
DE
13249/* Subroutine of update_breakpoint_locations to simplify it.
13250 Return non-zero if multiple fns in list LOC have the same name.
13251 Null names are ignored. */
13252
13253static int
13254ambiguous_names_p (struct bp_location *loc)
13255{
13256 struct bp_location *l;
459a2e4c
TT
13257 htab_t htab = htab_create_alloc (13, htab_hash_string, streq_hash, NULL,
13258 xcalloc, xfree);
776592bf
DE
13259
13260 for (l = loc; l != NULL; l = l->next)
13261 {
13262 const char **slot;
13263 const char *name = l->function_name;
13264
13265 /* Allow for some names to be NULL, ignore them. */
13266 if (name == NULL)
13267 continue;
13268
13269 slot = (const char **) htab_find_slot (htab, (const void *) name,
13270 INSERT);
4a64f543
MS
13271 /* NOTE: We can assume slot != NULL here because xcalloc never
13272 returns NULL. */
776592bf
DE
13273 if (*slot != NULL)
13274 {
13275 htab_delete (htab);
13276 return 1;
13277 }
13278 *slot = name;
13279 }
13280
13281 htab_delete (htab);
13282 return 0;
13283}
13284
0fb4aa4b
PA
13285/* When symbols change, it probably means the sources changed as well,
13286 and it might mean the static tracepoint markers are no longer at
13287 the same address or line numbers they used to be at last we
13288 checked. Losing your static tracepoints whenever you rebuild is
13289 undesirable. This function tries to resync/rematch gdb static
13290 tracepoints with the markers on the target, for static tracepoints
13291 that have not been set by marker id. Static tracepoint that have
13292 been set by marker id are reset by marker id in breakpoint_re_set.
13293 The heuristic is:
13294
13295 1) For a tracepoint set at a specific address, look for a marker at
13296 the old PC. If one is found there, assume to be the same marker.
13297 If the name / string id of the marker found is different from the
13298 previous known name, assume that means the user renamed the marker
13299 in the sources, and output a warning.
13300
13301 2) For a tracepoint set at a given line number, look for a marker
13302 at the new address of the old line number. If one is found there,
13303 assume to be the same marker. If the name / string id of the
13304 marker found is different from the previous known name, assume that
13305 means the user renamed the marker in the sources, and output a
13306 warning.
13307
13308 3) If a marker is no longer found at the same address or line, it
13309 may mean the marker no longer exists. But it may also just mean
13310 the code changed a bit. Maybe the user added a few lines of code
13311 that made the marker move up or down (in line number terms). Ask
13312 the target for info about the marker with the string id as we knew
13313 it. If found, update line number and address in the matching
13314 static tracepoint. This will get confused if there's more than one
13315 marker with the same ID (possible in UST, although unadvised
13316 precisely because it confuses tools). */
13317
13318static struct symtab_and_line
13319update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
13320{
d9b3f62e 13321 struct tracepoint *tp = (struct tracepoint *) b;
0fb4aa4b
PA
13322 struct static_tracepoint_marker marker;
13323 CORE_ADDR pc;
0fb4aa4b
PA
13324
13325 pc = sal.pc;
13326 if (sal.line)
13327 find_line_pc (sal.symtab, sal.line, &pc);
13328
13329 if (target_static_tracepoint_marker_at (pc, &marker))
13330 {
5d9310c4 13331 if (tp->static_trace_marker_id != marker.str_id)
0fb4aa4b 13332 warning (_("static tracepoint %d changed probed marker from %s to %s"),
5d9310c4
SM
13333 b->number, tp->static_trace_marker_id.c_str (),
13334 marker.str_id.c_str ());
0fb4aa4b 13335
5d9310c4 13336 tp->static_trace_marker_id = std::move (marker.str_id);
0fb4aa4b
PA
13337
13338 return sal;
13339 }
13340
13341 /* Old marker wasn't found on target at lineno. Try looking it up
13342 by string ID. */
13343 if (!sal.explicit_pc
13344 && sal.line != 0
13345 && sal.symtab != NULL
5d9310c4 13346 && !tp->static_trace_marker_id.empty ())
0fb4aa4b 13347 {
5d9310c4
SM
13348 std::vector<static_tracepoint_marker> markers
13349 = target_static_tracepoint_markers_by_strid
13350 (tp->static_trace_marker_id.c_str ());
0fb4aa4b 13351
5d9310c4 13352 if (!markers.empty ())
0fb4aa4b 13353 {
0fb4aa4b 13354 struct symbol *sym;
80e1d417 13355 struct static_tracepoint_marker *tpmarker;
79a45e25 13356 struct ui_out *uiout = current_uiout;
67994074 13357 struct explicit_location explicit_loc;
0fb4aa4b 13358
5d9310c4 13359 tpmarker = &markers[0];
0fb4aa4b 13360
5d9310c4 13361 tp->static_trace_marker_id = std::move (tpmarker->str_id);
0fb4aa4b
PA
13362
13363 warning (_("marker for static tracepoint %d (%s) not "
13364 "found at previous line number"),
5d9310c4 13365 b->number, tp->static_trace_marker_id.c_str ());
0fb4aa4b 13366
51abb421 13367 symtab_and_line sal2 = find_pc_line (tpmarker->address, 0);
80e1d417 13368 sym = find_pc_sect_function (tpmarker->address, NULL);
112e8700 13369 uiout->text ("Now in ");
0fb4aa4b
PA
13370 if (sym)
13371 {
987012b8 13372 uiout->field_string ("func", sym->print_name (),
e43b10e1 13373 function_name_style.style ());
112e8700 13374 uiout->text (" at ");
0fb4aa4b 13375 }
112e8700 13376 uiout->field_string ("file",
cbe56571 13377 symtab_to_filename_for_display (sal2.symtab),
e43b10e1 13378 file_name_style.style ());
112e8700 13379 uiout->text (":");
0fb4aa4b 13380
112e8700 13381 if (uiout->is_mi_like_p ())
0fb4aa4b 13382 {
0b0865da 13383 const char *fullname = symtab_to_fullname (sal2.symtab);
0fb4aa4b 13384
112e8700 13385 uiout->field_string ("fullname", fullname);
0fb4aa4b
PA
13386 }
13387
381befee 13388 uiout->field_signed ("line", sal2.line);
112e8700 13389 uiout->text ("\n");
0fb4aa4b 13390
80e1d417 13391 b->loc->line_number = sal2.line;
2f202fde 13392 b->loc->symtab = sym != NULL ? sal2.symtab : NULL;
0fb4aa4b 13393
d28cd78a 13394 b->location.reset (NULL);
67994074
KS
13395 initialize_explicit_location (&explicit_loc);
13396 explicit_loc.source_filename
00e52e53 13397 = ASTRDUP (symtab_to_filename_for_display (sal2.symtab));
67994074
KS
13398 explicit_loc.line_offset.offset = b->loc->line_number;
13399 explicit_loc.line_offset.sign = LINE_OFFSET_NONE;
d28cd78a 13400 b->location = new_explicit_location (&explicit_loc);
0fb4aa4b
PA
13401
13402 /* Might be nice to check if function changed, and warn if
13403 so. */
0fb4aa4b
PA
13404 }
13405 }
13406 return sal;
13407}
13408
8d3788bd
VP
13409/* Returns 1 iff locations A and B are sufficiently same that
13410 we don't need to report breakpoint as changed. */
13411
13412static int
13413locations_are_equal (struct bp_location *a, struct bp_location *b)
13414{
13415 while (a && b)
13416 {
13417 if (a->address != b->address)
13418 return 0;
13419
13420 if (a->shlib_disabled != b->shlib_disabled)
13421 return 0;
13422
13423 if (a->enabled != b->enabled)
13424 return 0;
13425
13426 a = a->next;
13427 b = b->next;
13428 }
13429
13430 if ((a == NULL) != (b == NULL))
13431 return 0;
13432
13433 return 1;
13434}
13435
c2f4122d
PA
13436/* Split all locations of B that are bound to PSPACE out of B's
13437 location list to a separate list and return that list's head. If
13438 PSPACE is NULL, hoist out all locations of B. */
13439
13440static struct bp_location *
13441hoist_existing_locations (struct breakpoint *b, struct program_space *pspace)
13442{
13443 struct bp_location head;
13444 struct bp_location *i = b->loc;
13445 struct bp_location **i_link = &b->loc;
13446 struct bp_location *hoisted = &head;
13447
13448 if (pspace == NULL)
13449 {
13450 i = b->loc;
13451 b->loc = NULL;
13452 return i;
13453 }
13454
13455 head.next = NULL;
13456
13457 while (i != NULL)
13458 {
13459 if (i->pspace == pspace)
13460 {
13461 *i_link = i->next;
13462 i->next = NULL;
13463 hoisted->next = i;
13464 hoisted = i;
13465 }
13466 else
13467 i_link = &i->next;
13468 i = *i_link;
13469 }
13470
13471 return head.next;
13472}
13473
13474/* Create new breakpoint locations for B (a hardware or software
13475 breakpoint) based on SALS and SALS_END. If SALS_END.NELTS is not
13476 zero, then B is a ranged breakpoint. Only recreates locations for
13477 FILTER_PSPACE. Locations of other program spaces are left
13478 untouched. */
f1310107 13479
0e30163f 13480void
0d381245 13481update_breakpoint_locations (struct breakpoint *b,
c2f4122d 13482 struct program_space *filter_pspace,
6c5b2ebe
PA
13483 gdb::array_view<const symtab_and_line> sals,
13484 gdb::array_view<const symtab_and_line> sals_end)
fe3f5fa8 13485{
c2f4122d 13486 struct bp_location *existing_locations;
0d381245 13487
6c5b2ebe 13488 if (!sals_end.empty () && (sals.size () != 1 || sals_end.size () != 1))
f8eba3c6
TT
13489 {
13490 /* Ranged breakpoints have only one start location and one end
13491 location. */
13492 b->enable_state = bp_disabled;
f8eba3c6
TT
13493 printf_unfiltered (_("Could not reset ranged breakpoint %d: "
13494 "multiple locations found\n"),
13495 b->number);
13496 return;
13497 }
f1310107 13498
4a64f543
MS
13499 /* If there's no new locations, and all existing locations are
13500 pending, don't do anything. This optimizes the common case where
13501 all locations are in the same shared library, that was unloaded.
13502 We'd like to retain the location, so that when the library is
13503 loaded again, we don't loose the enabled/disabled status of the
13504 individual locations. */
6c5b2ebe 13505 if (all_locations_are_pending (b, filter_pspace) && sals.empty ())
fe3f5fa8
VP
13506 return;
13507
c2f4122d 13508 existing_locations = hoist_existing_locations (b, filter_pspace);
fe3f5fa8 13509
6c5b2ebe 13510 for (const auto &sal : sals)
fe3f5fa8 13511 {
f8eba3c6
TT
13512 struct bp_location *new_loc;
13513
6c5b2ebe 13514 switch_to_program_space_and_thread (sal.pspace);
f8eba3c6 13515
6c5b2ebe 13516 new_loc = add_location_to_breakpoint (b, &sal);
fe3f5fa8 13517
0d381245
VP
13518 /* Reparse conditions, they might contain references to the
13519 old symtab. */
13520 if (b->cond_string != NULL)
13521 {
bbc13ae3 13522 const char *s;
fe3f5fa8 13523
0d381245 13524 s = b->cond_string;
a70b8144 13525 try
0d381245 13526 {
6c5b2ebe
PA
13527 new_loc->cond = parse_exp_1 (&s, sal.pc,
13528 block_for_pc (sal.pc),
0d381245
VP
13529 0);
13530 }
230d2906 13531 catch (const gdb_exception_error &e)
0d381245 13532 {
3e43a32a
MS
13533 warning (_("failed to reevaluate condition "
13534 "for breakpoint %d: %s"),
3d6e9d23 13535 b->number, e.what ());
0d381245
VP
13536 new_loc->enabled = 0;
13537 }
13538 }
fe3f5fa8 13539
6c5b2ebe 13540 if (!sals_end.empty ())
f1310107 13541 {
6c5b2ebe 13542 CORE_ADDR end = find_breakpoint_range_end (sals_end[0]);
f1310107 13543
6c5b2ebe 13544 new_loc->length = end - sals[0].pc + 1;
f1310107 13545 }
0d381245 13546 }
fe3f5fa8 13547
4a64f543
MS
13548 /* If possible, carry over 'disable' status from existing
13549 breakpoints. */
0d381245
VP
13550 {
13551 struct bp_location *e = existing_locations;
776592bf
DE
13552 /* If there are multiple breakpoints with the same function name,
13553 e.g. for inline functions, comparing function names won't work.
13554 Instead compare pc addresses; this is just a heuristic as things
13555 may have moved, but in practice it gives the correct answer
13556 often enough until a better solution is found. */
13557 int have_ambiguous_names = ambiguous_names_p (b->loc);
13558
0d381245
VP
13559 for (; e; e = e->next)
13560 {
13561 if (!e->enabled && e->function_name)
13562 {
13563 struct bp_location *l = b->loc;
776592bf
DE
13564 if (have_ambiguous_names)
13565 {
13566 for (; l; l = l->next)
7f32a4d5
PA
13567 {
13568 /* Ignore software vs hardware location type at
13569 this point, because with "set breakpoint
13570 auto-hw", after a re-set, locations that were
13571 hardware can end up as software, or vice versa.
13572 As mentioned above, this is an heuristic and in
13573 practice should give the correct answer often
13574 enough. */
13575 if (breakpoint_locations_match (e, l, true))
13576 {
13577 l->enabled = 0;
13578 break;
13579 }
13580 }
776592bf
DE
13581 }
13582 else
13583 {
13584 for (; l; l = l->next)
13585 if (l->function_name
13586 && strcmp (e->function_name, l->function_name) == 0)
13587 {
13588 l->enabled = 0;
13589 break;
13590 }
13591 }
0d381245
VP
13592 }
13593 }
13594 }
fe3f5fa8 13595
8d3788bd 13596 if (!locations_are_equal (existing_locations, b->loc))
76727919 13597 gdb::observers::breakpoint_modified.notify (b);
fe3f5fa8
VP
13598}
13599
f00aae0f 13600/* Find the SaL locations corresponding to the given LOCATION.
ef23e705
TJB
13601 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
13602
6c5b2ebe 13603static std::vector<symtab_and_line>
f00aae0f 13604location_to_sals (struct breakpoint *b, struct event_location *location,
c2f4122d 13605 struct program_space *search_pspace, int *found)
ef23e705 13606{
cc06b668 13607 struct gdb_exception exception;
ef23e705 13608
983af33b 13609 gdb_assert (b->ops != NULL);
ef23e705 13610
6c5b2ebe
PA
13611 std::vector<symtab_and_line> sals;
13612
a70b8144 13613 try
ef23e705 13614 {
6c5b2ebe 13615 sals = b->ops->decode_location (b, location, search_pspace);
ef23e705 13616 }
94aeb44b 13617 catch (gdb_exception_error &e)
ef23e705
TJB
13618 {
13619 int not_found_and_ok = 0;
492d29ea 13620
ef23e705
TJB
13621 /* For pending breakpoints, it's expected that parsing will
13622 fail until the right shared library is loaded. User has
13623 already told to create pending breakpoints and don't need
13624 extra messages. If breakpoint is in bp_shlib_disabled
13625 state, then user already saw the message about that
13626 breakpoint being disabled, and don't want to see more
13627 errors. */
58438ac1 13628 if (e.error == NOT_FOUND_ERROR
c2f4122d
PA
13629 && (b->condition_not_parsed
13630 || (b->loc != NULL
13631 && search_pspace != NULL
13632 && b->loc->pspace != search_pspace)
ef23e705 13633 || (b->loc && b->loc->shlib_disabled)
f8eba3c6 13634 || (b->loc && b->loc->pspace->executing_startup)
ef23e705
TJB
13635 || b->enable_state == bp_disabled))
13636 not_found_and_ok = 1;
13637
13638 if (!not_found_and_ok)
13639 {
13640 /* We surely don't want to warn about the same breakpoint
13641 10 times. One solution, implemented here, is disable
13642 the breakpoint on error. Another solution would be to
13643 have separate 'warning emitted' flag. Since this
13644 happens only when a binary has changed, I don't know
13645 which approach is better. */
13646 b->enable_state = bp_disabled;
eedc3f4f 13647 throw;
ef23e705 13648 }
94aeb44b
TT
13649
13650 exception = std::move (e);
ef23e705
TJB
13651 }
13652
492d29ea 13653 if (exception.reason == 0 || exception.error != NOT_FOUND_ERROR)
ef23e705 13654 {
6c5b2ebe
PA
13655 for (auto &sal : sals)
13656 resolve_sal_pc (&sal);
f00aae0f 13657 if (b->condition_not_parsed && b->extra_string != NULL)
ef23e705 13658 {
ed1d1739
KS
13659 char *cond_string, *extra_string;
13660 int thread, task;
ef23e705 13661
6c5b2ebe 13662 find_condition_and_thread (b->extra_string, sals[0].pc,
e7e0cddf
SS
13663 &cond_string, &thread, &task,
13664 &extra_string);
f00aae0f 13665 gdb_assert (b->cond_string == NULL);
ef23e705
TJB
13666 if (cond_string)
13667 b->cond_string = cond_string;
13668 b->thread = thread;
13669 b->task = task;
e7e0cddf 13670 if (extra_string)
f00aae0f
KS
13671 {
13672 xfree (b->extra_string);
13673 b->extra_string = extra_string;
13674 }
ef23e705
TJB
13675 b->condition_not_parsed = 0;
13676 }
13677
983af33b 13678 if (b->type == bp_static_tracepoint && !strace_marker_p (b))
6c5b2ebe 13679 sals[0] = update_static_tracepoint (b, sals[0]);
ef23e705 13680
58438ac1
TT
13681 *found = 1;
13682 }
13683 else
13684 *found = 0;
ef23e705
TJB
13685
13686 return sals;
13687}
13688
348d480f
PA
13689/* The default re_set method, for typical hardware or software
13690 breakpoints. Reevaluate the breakpoint and recreate its
13691 locations. */
13692
13693static void
28010a5d 13694breakpoint_re_set_default (struct breakpoint *b)
ef23e705 13695{
c2f4122d 13696 struct program_space *filter_pspace = current_program_space;
6c5b2ebe 13697 std::vector<symtab_and_line> expanded, expanded_end;
ef23e705 13698
6c5b2ebe
PA
13699 int found;
13700 std::vector<symtab_and_line> sals = location_to_sals (b, b->location.get (),
13701 filter_pspace, &found);
ef23e705 13702 if (found)
6c5b2ebe 13703 expanded = std::move (sals);
ef23e705 13704
f00aae0f 13705 if (b->location_range_end != NULL)
f1310107 13706 {
6c5b2ebe
PA
13707 std::vector<symtab_and_line> sals_end
13708 = location_to_sals (b, b->location_range_end.get (),
13709 filter_pspace, &found);
f1310107 13710 if (found)
6c5b2ebe 13711 expanded_end = std::move (sals_end);
f1310107
TJB
13712 }
13713
c2f4122d 13714 update_breakpoint_locations (b, filter_pspace, expanded, expanded_end);
28010a5d
PA
13715}
13716
983af33b
SDJ
13717/* Default method for creating SALs from an address string. It basically
13718 calls parse_breakpoint_sals. Return 1 for success, zero for failure. */
13719
13720static void
f00aae0f
KS
13721create_sals_from_location_default (const struct event_location *location,
13722 struct linespec_result *canonical,
13723 enum bptype type_wanted)
983af33b 13724{
f00aae0f 13725 parse_breakpoint_sals (location, canonical);
983af33b
SDJ
13726}
13727
13728/* Call create_breakpoints_sal for the given arguments. This is the default
13729 function for the `create_breakpoints_sal' method of
13730 breakpoint_ops. */
13731
13732static void
13733create_breakpoints_sal_default (struct gdbarch *gdbarch,
13734 struct linespec_result *canonical,
e1e01040
PA
13735 gdb::unique_xmalloc_ptr<char> cond_string,
13736 gdb::unique_xmalloc_ptr<char> extra_string,
983af33b
SDJ
13737 enum bptype type_wanted,
13738 enum bpdisp disposition,
13739 int thread,
13740 int task, int ignore_count,
13741 const struct breakpoint_ops *ops,
13742 int from_tty, int enabled,
44f238bb 13743 int internal, unsigned flags)
983af33b 13744{
e1e01040
PA
13745 create_breakpoints_sal (gdbarch, canonical,
13746 std::move (cond_string),
13747 std::move (extra_string),
983af33b
SDJ
13748 type_wanted, disposition,
13749 thread, task, ignore_count, ops, from_tty,
44f238bb 13750 enabled, internal, flags);
983af33b
SDJ
13751}
13752
13753/* Decode the line represented by S by calling decode_line_full. This is the
5f700d83 13754 default function for the `decode_location' method of breakpoint_ops. */
983af33b 13755
6c5b2ebe 13756static std::vector<symtab_and_line>
f00aae0f
KS
13757decode_location_default (struct breakpoint *b,
13758 const struct event_location *location,
6c5b2ebe 13759 struct program_space *search_pspace)
983af33b
SDJ
13760{
13761 struct linespec_result canonical;
13762
c2f4122d 13763 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, search_pspace,
cafb3438 13764 NULL, 0, &canonical, multiple_symbols_all,
c0e8dcd8 13765 b->filter.get ());
983af33b
SDJ
13766
13767 /* We should get 0 or 1 resulting SALs. */
6c5b2ebe 13768 gdb_assert (canonical.lsals.size () < 2);
983af33b 13769
6c5b2ebe 13770 if (!canonical.lsals.empty ())
983af33b 13771 {
6c5b2ebe
PA
13772 const linespec_sals &lsal = canonical.lsals[0];
13773 return std::move (lsal.sals);
983af33b 13774 }
6c5b2ebe 13775 return {};
983af33b
SDJ
13776}
13777
bf469271 13778/* Reset a breakpoint. */
c906108c 13779
bf469271
PA
13780static void
13781breakpoint_re_set_one (breakpoint *b)
c906108c 13782{
fdf44873
TT
13783 input_radix = b->input_radix;
13784 set_language (b->language);
c906108c 13785
348d480f 13786 b->ops->re_set (b);
c906108c
SS
13787}
13788
c2f4122d
PA
13789/* Re-set breakpoint locations for the current program space.
13790 Locations bound to other program spaces are left untouched. */
13791
c906108c 13792void
69de3c6a 13793breakpoint_re_set (void)
c906108c 13794{
35df4500 13795 struct breakpoint *b, *b_tmp;
2a7f3dff 13796
c5aa993b 13797 {
fdf44873
TT
13798 scoped_restore_current_language save_language;
13799 scoped_restore save_input_radix = make_scoped_restore (&input_radix);
5ed8105e 13800 scoped_restore_current_pspace_and_thread restore_pspace_thread;
e62c965a 13801
8e817061
JB
13802 /* breakpoint_re_set_one sets the current_language to the language
13803 of the breakpoint it is resetting (see prepare_re_set_context)
13804 before re-evaluating the breakpoint's location. This change can
13805 unfortunately get undone by accident if the language_mode is set
13806 to auto, and we either switch frames, or more likely in this context,
13807 we select the current frame.
13808
13809 We prevent this by temporarily turning the language_mode to
13810 language_mode_manual. We restore it once all breakpoints
13811 have been reset. */
13812 scoped_restore save_language_mode = make_scoped_restore (&language_mode);
13813 language_mode = language_mode_manual;
13814
5ed8105e
PA
13815 /* Note: we must not try to insert locations until after all
13816 breakpoints have been re-set. Otherwise, e.g., when re-setting
13817 breakpoint 1, we'd insert the locations of breakpoint 2, which
13818 hadn't been re-set yet, and thus may have stale locations. */
4efc6507 13819
5ed8105e
PA
13820 ALL_BREAKPOINTS_SAFE (b, b_tmp)
13821 {
a70b8144 13822 try
bf469271
PA
13823 {
13824 breakpoint_re_set_one (b);
13825 }
230d2906 13826 catch (const gdb_exception &ex)
bf469271
PA
13827 {
13828 exception_fprintf (gdb_stderr, ex,
13829 "Error in re-setting breakpoint %d: ",
13830 b->number);
13831 }
5ed8105e 13832 }
5ed8105e
PA
13833
13834 jit_breakpoint_re_set ();
13835 }
6c95b8df 13836
af02033e
PP
13837 create_overlay_event_breakpoint ();
13838 create_longjmp_master_breakpoint ();
13839 create_std_terminate_master_breakpoint ();
186c406b 13840 create_exception_master_breakpoint ();
2a7f3dff
PA
13841
13842 /* Now we can insert. */
13843 update_global_location_list (UGLL_MAY_INSERT);
c906108c
SS
13844}
13845\f
c906108c
SS
13846/* Reset the thread number of this breakpoint:
13847
13848 - If the breakpoint is for all threads, leave it as-is.
4a64f543 13849 - Else, reset it to the current thread for inferior_ptid. */
c906108c 13850void
fba45db2 13851breakpoint_re_set_thread (struct breakpoint *b)
c906108c
SS
13852{
13853 if (b->thread != -1)
13854 {
00431a78 13855 b->thread = inferior_thread ()->global_num;
6c95b8df
PA
13856
13857 /* We're being called after following a fork. The new fork is
13858 selected as current, and unless this was a vfork will have a
13859 different program space from the original thread. Reset that
13860 as well. */
13861 b->loc->pspace = current_program_space;
c906108c
SS
13862 }
13863}
13864
03ac34d5
MS
13865/* Set ignore-count of breakpoint number BPTNUM to COUNT.
13866 If from_tty is nonzero, it prints a message to that effect,
13867 which ends with a period (no newline). */
13868
c906108c 13869void
fba45db2 13870set_ignore_count (int bptnum, int count, int from_tty)
c906108c 13871{
52f0bd74 13872 struct breakpoint *b;
c906108c
SS
13873
13874 if (count < 0)
13875 count = 0;
13876
13877 ALL_BREAKPOINTS (b)
13878 if (b->number == bptnum)
c5aa993b 13879 {
d77f58be
SS
13880 if (is_tracepoint (b))
13881 {
13882 if (from_tty && count != 0)
13883 printf_filtered (_("Ignore count ignored for tracepoint %d."),
13884 bptnum);
13885 return;
13886 }
13887
c5aa993b 13888 b->ignore_count = count;
221ea385
KS
13889 if (from_tty)
13890 {
13891 if (count == 0)
3e43a32a
MS
13892 printf_filtered (_("Will stop next time "
13893 "breakpoint %d is reached."),
221ea385
KS
13894 bptnum);
13895 else if (count == 1)
a3f17187 13896 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
221ea385
KS
13897 bptnum);
13898 else
3e43a32a
MS
13899 printf_filtered (_("Will ignore next %d "
13900 "crossings of breakpoint %d."),
221ea385
KS
13901 count, bptnum);
13902 }
76727919 13903 gdb::observers::breakpoint_modified.notify (b);
c5aa993b
JM
13904 return;
13905 }
c906108c 13906
8a3fe4f8 13907 error (_("No breakpoint number %d."), bptnum);
c906108c
SS
13908}
13909
c906108c
SS
13910/* Command to set ignore-count of breakpoint N to COUNT. */
13911
13912static void
0b39b52e 13913ignore_command (const char *args, int from_tty)
c906108c 13914{
0b39b52e 13915 const char *p = args;
52f0bd74 13916 int num;
c906108c
SS
13917
13918 if (p == 0)
e2e0b3e5 13919 error_no_arg (_("a breakpoint number"));
c5aa993b 13920
c906108c 13921 num = get_number (&p);
5c44784c 13922 if (num == 0)
8a3fe4f8 13923 error (_("bad breakpoint number: '%s'"), args);
c906108c 13924 if (*p == 0)
8a3fe4f8 13925 error (_("Second argument (specified ignore-count) is missing."));
c906108c
SS
13926
13927 set_ignore_count (num,
13928 longest_to_int (value_as_long (parse_and_eval (p))),
13929 from_tty);
221ea385
KS
13930 if (from_tty)
13931 printf_filtered ("\n");
c906108c
SS
13932}
13933\f
d0fe4701
XR
13934
13935/* Call FUNCTION on each of the breakpoints with numbers in the range
13936 defined by BP_NUM_RANGE (an inclusive range). */
c906108c
SS
13937
13938static void
d0fe4701
XR
13939map_breakpoint_number_range (std::pair<int, int> bp_num_range,
13940 gdb::function_view<void (breakpoint *)> function)
c906108c 13941{
d0fe4701
XR
13942 if (bp_num_range.first == 0)
13943 {
13944 warning (_("bad breakpoint number at or near '%d'"),
13945 bp_num_range.first);
13946 }
13947 else
c906108c 13948 {
d0fe4701 13949 struct breakpoint *b, *tmp;
197f0a60 13950
d0fe4701 13951 for (int i = bp_num_range.first; i <= bp_num_range.second; i++)
5c44784c 13952 {
d0fe4701
XR
13953 bool match = false;
13954
5c44784c 13955 ALL_BREAKPOINTS_SAFE (b, tmp)
d0fe4701 13956 if (b->number == i)
5c44784c 13957 {
bfd28288 13958 match = true;
48649e1b 13959 function (b);
11cf8741 13960 break;
5c44784c 13961 }
bfd28288 13962 if (!match)
d0fe4701 13963 printf_unfiltered (_("No breakpoint number %d.\n"), i);
c5aa993b 13964 }
c906108c
SS
13965 }
13966}
13967
d0fe4701
XR
13968/* Call FUNCTION on each of the breakpoints whose numbers are given in
13969 ARGS. */
13970
13971static void
13972map_breakpoint_numbers (const char *args,
13973 gdb::function_view<void (breakpoint *)> function)
13974{
13975 if (args == NULL || *args == '\0')
13976 error_no_arg (_("one or more breakpoint numbers"));
13977
13978 number_or_range_parser parser (args);
13979
13980 while (!parser.finished ())
13981 {
13982 int num = parser.get_number ();
13983 map_breakpoint_number_range (std::make_pair (num, num), function);
13984 }
13985}
13986
13987/* Return the breakpoint location structure corresponding to the
13988 BP_NUM and LOC_NUM values. */
13989
0d381245 13990static struct bp_location *
d0fe4701 13991find_location_by_number (int bp_num, int loc_num)
0d381245 13992{
0d381245 13993 struct breakpoint *b;
0d381245
VP
13994
13995 ALL_BREAKPOINTS (b)
13996 if (b->number == bp_num)
13997 {
13998 break;
13999 }
14000
14001 if (!b || b->number != bp_num)
d0fe4701 14002 error (_("Bad breakpoint number '%d'"), bp_num);
0d381245 14003
0d381245 14004 if (loc_num == 0)
d0fe4701 14005 error (_("Bad breakpoint location number '%d'"), loc_num);
0d381245 14006
d0fe4701
XR
14007 int n = 0;
14008 for (bp_location *loc = b->loc; loc != NULL; loc = loc->next)
14009 if (++n == loc_num)
14010 return loc;
14011
14012 error (_("Bad breakpoint location number '%d'"), loc_num);
0d381245
VP
14013}
14014
95e95a6d
PA
14015/* Modes of operation for extract_bp_num. */
14016enum class extract_bp_kind
14017{
14018 /* Extracting a breakpoint number. */
14019 bp,
14020
14021 /* Extracting a location number. */
14022 loc,
14023};
14024
14025/* Extract a breakpoint or location number (as determined by KIND)
14026 from the string starting at START. TRAILER is a character which
14027 can be found after the number. If you don't want a trailer, use
14028 '\0'. If END_OUT is not NULL, it is set to point after the parsed
14029 string. This always returns a positive integer. */
14030
14031static int
14032extract_bp_num (extract_bp_kind kind, const char *start,
14033 int trailer, const char **end_out = NULL)
14034{
14035 const char *end = start;
14036 int num = get_number_trailer (&end, trailer);
14037 if (num < 0)
14038 error (kind == extract_bp_kind::bp
14039 ? _("Negative breakpoint number '%.*s'")
14040 : _("Negative breakpoint location number '%.*s'"),
14041 int (end - start), start);
14042 if (num == 0)
14043 error (kind == extract_bp_kind::bp
14044 ? _("Bad breakpoint number '%.*s'")
14045 : _("Bad breakpoint location number '%.*s'"),
14046 int (end - start), start);
14047
14048 if (end_out != NULL)
14049 *end_out = end;
14050 return num;
14051}
14052
14053/* Extract a breakpoint or location range (as determined by KIND) in
14054 the form NUM1-NUM2 stored at &ARG[arg_offset]. Returns a std::pair
14055 representing the (inclusive) range. The returned pair's elements
14056 are always positive integers. */
14057
14058static std::pair<int, int>
14059extract_bp_or_bp_range (extract_bp_kind kind,
14060 const std::string &arg,
14061 std::string::size_type arg_offset)
14062{
14063 std::pair<int, int> range;
14064 const char *bp_loc = &arg[arg_offset];
14065 std::string::size_type dash = arg.find ('-', arg_offset);
14066 if (dash != std::string::npos)
14067 {
14068 /* bp_loc is a range (x-z). */
14069 if (arg.length () == dash + 1)
14070 error (kind == extract_bp_kind::bp
14071 ? _("Bad breakpoint number at or near: '%s'")
14072 : _("Bad breakpoint location number at or near: '%s'"),
14073 bp_loc);
14074
14075 const char *end;
14076 const char *start_first = bp_loc;
14077 const char *start_second = &arg[dash + 1];
14078 range.first = extract_bp_num (kind, start_first, '-');
14079 range.second = extract_bp_num (kind, start_second, '\0', &end);
14080
14081 if (range.first > range.second)
14082 error (kind == extract_bp_kind::bp
14083 ? _("Inverted breakpoint range at '%.*s'")
14084 : _("Inverted breakpoint location range at '%.*s'"),
14085 int (end - start_first), start_first);
14086 }
14087 else
14088 {
14089 /* bp_loc is a single value. */
14090 range.first = extract_bp_num (kind, bp_loc, '\0');
14091 range.second = range.first;
14092 }
14093 return range;
14094}
14095
d0fe4701
XR
14096/* Extract the breakpoint/location range specified by ARG. Returns
14097 the breakpoint range in BP_NUM_RANGE, and the location range in
14098 BP_LOC_RANGE.
14099
14100 ARG may be in any of the following forms:
14101
14102 x where 'x' is a breakpoint number.
14103 x-y where 'x' and 'y' specify a breakpoint numbers range.
14104 x.y where 'x' is a breakpoint number and 'y' a location number.
14105 x.y-z where 'x' is a breakpoint number and 'y' and 'z' specify a
14106 location number range.
14107*/
14108
cc638e86 14109static void
d0fe4701
XR
14110extract_bp_number_and_location (const std::string &arg,
14111 std::pair<int, int> &bp_num_range,
14112 std::pair<int, int> &bp_loc_range)
14113{
14114 std::string::size_type dot = arg.find ('.');
14115
14116 if (dot != std::string::npos)
14117 {
14118 /* Handle 'x.y' and 'x.y-z' cases. */
14119
14120 if (arg.length () == dot + 1 || dot == 0)
95e95a6d 14121 error (_("Bad breakpoint number at or near: '%s'"), arg.c_str ());
d0fe4701 14122
95e95a6d
PA
14123 bp_num_range.first
14124 = extract_bp_num (extract_bp_kind::bp, arg.c_str (), '.');
14125 bp_num_range.second = bp_num_range.first;
d0fe4701 14126
95e95a6d
PA
14127 bp_loc_range = extract_bp_or_bp_range (extract_bp_kind::loc,
14128 arg, dot + 1);
d0fe4701
XR
14129 }
14130 else
14131 {
14132 /* Handle x and x-y cases. */
d0fe4701 14133
95e95a6d 14134 bp_num_range = extract_bp_or_bp_range (extract_bp_kind::bp, arg, 0);
d0fe4701
XR
14135 bp_loc_range.first = 0;
14136 bp_loc_range.second = 0;
14137 }
d0fe4701
XR
14138}
14139
14140/* Enable or disable a breakpoint location BP_NUM.LOC_NUM. ENABLE
14141 specifies whether to enable or disable. */
14142
14143static void
14144enable_disable_bp_num_loc (int bp_num, int loc_num, bool enable)
14145{
14146 struct bp_location *loc = find_location_by_number (bp_num, loc_num);
14147 if (loc != NULL)
14148 {
14149 if (loc->enabled != enable)
14150 {
14151 loc->enabled = enable;
14152 mark_breakpoint_location_modified (loc);
14153 }
14154 if (target_supports_enable_disable_tracepoint ()
14155 && current_trace_status ()->running && loc->owner
14156 && is_tracepoint (loc->owner))
14157 target_disable_tracepoint (loc);
14158 }
14159 update_global_location_list (UGLL_DONT_INSERT);
d7154a8d
JV
14160
14161 gdb::observers::breakpoint_modified.notify (loc->owner);
d0fe4701
XR
14162}
14163
14164/* Enable or disable a range of breakpoint locations. BP_NUM is the
14165 number of the breakpoint, and BP_LOC_RANGE specifies the
14166 (inclusive) range of location numbers of that breakpoint to
14167 enable/disable. ENABLE specifies whether to enable or disable the
14168 location. */
14169
14170static void
14171enable_disable_breakpoint_location_range (int bp_num,
14172 std::pair<int, int> &bp_loc_range,
14173 bool enable)
14174{
14175 for (int i = bp_loc_range.first; i <= bp_loc_range.second; i++)
14176 enable_disable_bp_num_loc (bp_num, i, enable);
14177}
0d381245 14178
1900040c
MS
14179/* Set ignore-count of breakpoint number BPTNUM to COUNT.
14180 If from_tty is nonzero, it prints a message to that effect,
14181 which ends with a period (no newline). */
14182
c906108c 14183void
fba45db2 14184disable_breakpoint (struct breakpoint *bpt)
c906108c
SS
14185{
14186 /* Never disable a watchpoint scope breakpoint; we want to
14187 hit them when we leave scope so we can delete both the
14188 watchpoint and its scope breakpoint at that time. */
14189 if (bpt->type == bp_watchpoint_scope)
14190 return;
14191
b5de0fa7 14192 bpt->enable_state = bp_disabled;
c906108c 14193
b775012e
LM
14194 /* Mark breakpoint locations modified. */
14195 mark_breakpoint_modified (bpt);
14196
d248b706
KY
14197 if (target_supports_enable_disable_tracepoint ()
14198 && current_trace_status ()->running && is_tracepoint (bpt))
14199 {
14200 struct bp_location *location;
14201
14202 for (location = bpt->loc; location; location = location->next)
14203 target_disable_tracepoint (location);
14204 }
14205
44702360 14206 update_global_location_list (UGLL_DONT_INSERT);
c906108c 14207
76727919 14208 gdb::observers::breakpoint_modified.notify (bpt);
c906108c
SS
14209}
14210
d0fe4701
XR
14211/* Enable or disable the breakpoint(s) or breakpoint location(s)
14212 specified in ARGS. ARGS may be in any of the formats handled by
14213 extract_bp_number_and_location. ENABLE specifies whether to enable
14214 or disable the breakpoints/locations. */
14215
c906108c 14216static void
d0fe4701 14217enable_disable_command (const char *args, int from_tty, bool enable)
c906108c 14218{
c906108c 14219 if (args == 0)
46c6471b
PA
14220 {
14221 struct breakpoint *bpt;
14222
14223 ALL_BREAKPOINTS (bpt)
14224 if (user_breakpoint_p (bpt))
d0fe4701
XR
14225 {
14226 if (enable)
14227 enable_breakpoint (bpt);
14228 else
14229 disable_breakpoint (bpt);
14230 }
46c6471b 14231 }
9eaabc75 14232 else
0d381245 14233 {
cb791d59 14234 std::string num = extract_arg (&args);
9eaabc75 14235
cb791d59 14236 while (!num.empty ())
d248b706 14237 {
d0fe4701 14238 std::pair<int, int> bp_num_range, bp_loc_range;
9eaabc75 14239
cc638e86
PA
14240 extract_bp_number_and_location (num, bp_num_range, bp_loc_range);
14241
14242 if (bp_loc_range.first == bp_loc_range.second
14243 && bp_loc_range.first == 0)
d0fe4701 14244 {
cc638e86
PA
14245 /* Handle breakpoint ids with formats 'x' or 'x-z'. */
14246 map_breakpoint_number_range (bp_num_range,
14247 enable
14248 ? enable_breakpoint
14249 : disable_breakpoint);
14250 }
14251 else
14252 {
14253 /* Handle breakpoint ids with formats 'x.y' or
14254 'x.y-z'. */
14255 enable_disable_breakpoint_location_range
14256 (bp_num_range.first, bp_loc_range, enable);
b775012e 14257 }
9eaabc75 14258 num = extract_arg (&args);
d248b706 14259 }
0d381245 14260 }
c906108c
SS
14261}
14262
d0fe4701
XR
14263/* The disable command disables the specified breakpoints/locations
14264 (or all defined breakpoints) so they're no longer effective in
14265 stopping the inferior. ARGS may be in any of the forms defined in
14266 extract_bp_number_and_location. */
14267
14268static void
14269disable_command (const char *args, int from_tty)
14270{
14271 enable_disable_command (args, from_tty, false);
14272}
14273
c906108c 14274static void
816338b5
SS
14275enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
14276 int count)
c906108c 14277{
afe38095 14278 int target_resources_ok;
c906108c
SS
14279
14280 if (bpt->type == bp_hardware_breakpoint)
14281 {
14282 int i;
c5aa993b 14283 i = hw_breakpoint_used_count ();
53a5351d 14284 target_resources_ok =
d92524f1 14285 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
53a5351d 14286 i + 1, 0);
c906108c 14287 if (target_resources_ok == 0)
8a3fe4f8 14288 error (_("No hardware breakpoint support in the target."));
c906108c 14289 else if (target_resources_ok < 0)
8a3fe4f8 14290 error (_("Hardware breakpoints used exceeds limit."));
c906108c
SS
14291 }
14292
cc60f2e3 14293 if (is_watchpoint (bpt))
c906108c 14294 {
d07205c2 14295 /* Initialize it just to avoid a GCC false warning. */
f486487f 14296 enum enable_state orig_enable_state = bp_disabled;
dde02812 14297
a70b8144 14298 try
c906108c 14299 {
3a5c3e22
PA
14300 struct watchpoint *w = (struct watchpoint *) bpt;
14301
1e718ff1
TJB
14302 orig_enable_state = bpt->enable_state;
14303 bpt->enable_state = bp_enabled;
3a5c3e22 14304 update_watchpoint (w, 1 /* reparse */);
c906108c 14305 }
230d2906 14306 catch (const gdb_exception &e)
c5aa993b 14307 {
1e718ff1 14308 bpt->enable_state = orig_enable_state;
dde02812
ES
14309 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
14310 bpt->number);
14311 return;
c5aa993b 14312 }
c906108c 14313 }
0101ce28 14314
b775012e
LM
14315 bpt->enable_state = bp_enabled;
14316
14317 /* Mark breakpoint locations modified. */
14318 mark_breakpoint_modified (bpt);
14319
d248b706
KY
14320 if (target_supports_enable_disable_tracepoint ()
14321 && current_trace_status ()->running && is_tracepoint (bpt))
14322 {
14323 struct bp_location *location;
14324
14325 for (location = bpt->loc; location; location = location->next)
14326 target_enable_tracepoint (location);
14327 }
14328
b4c291bb 14329 bpt->disposition = disposition;
816338b5 14330 bpt->enable_count = count;
44702360 14331 update_global_location_list (UGLL_MAY_INSERT);
9c97429f 14332
76727919 14333 gdb::observers::breakpoint_modified.notify (bpt);
c906108c
SS
14334}
14335
fe3f5fa8 14336
c906108c 14337void
fba45db2 14338enable_breakpoint (struct breakpoint *bpt)
c906108c 14339{
816338b5 14340 enable_breakpoint_disp (bpt, bpt->disposition, 0);
51be5b68
PA
14341}
14342
d0fe4701
XR
14343/* The enable command enables the specified breakpoints/locations (or
14344 all defined breakpoints) so they once again become (or continue to
14345 be) effective in stopping the inferior. ARGS may be in any of the
14346 forms defined in extract_bp_number_and_location. */
c906108c 14347
c906108c 14348static void
981a3fb3 14349enable_command (const char *args, int from_tty)
c906108c 14350{
d0fe4701 14351 enable_disable_command (args, from_tty, true);
c906108c
SS
14352}
14353
c906108c 14354static void
4495129a 14355enable_once_command (const char *args, int from_tty)
c906108c 14356{
48649e1b
TT
14357 map_breakpoint_numbers
14358 (args, [&] (breakpoint *b)
14359 {
14360 iterate_over_related_breakpoints
14361 (b, [&] (breakpoint *bpt)
14362 {
14363 enable_breakpoint_disp (bpt, disp_disable, 1);
14364 });
14365 });
816338b5
SS
14366}
14367
14368static void
4495129a 14369enable_count_command (const char *args, int from_tty)
816338b5 14370{
b9d61307
SM
14371 int count;
14372
14373 if (args == NULL)
14374 error_no_arg (_("hit count"));
14375
14376 count = get_number (&args);
816338b5 14377
48649e1b
TT
14378 map_breakpoint_numbers
14379 (args, [&] (breakpoint *b)
14380 {
14381 iterate_over_related_breakpoints
14382 (b, [&] (breakpoint *bpt)
14383 {
14384 enable_breakpoint_disp (bpt, disp_disable, count);
14385 });
14386 });
c906108c
SS
14387}
14388
c906108c 14389static void
4495129a 14390enable_delete_command (const char *args, int from_tty)
c906108c 14391{
48649e1b
TT
14392 map_breakpoint_numbers
14393 (args, [&] (breakpoint *b)
14394 {
14395 iterate_over_related_breakpoints
14396 (b, [&] (breakpoint *bpt)
14397 {
14398 enable_breakpoint_disp (bpt, disp_del, 1);
14399 });
14400 });
c906108c
SS
14401}
14402\f
1f3b5d1b
PP
14403/* Invalidate last known value of any hardware watchpoint if
14404 the memory which that value represents has been written to by
14405 GDB itself. */
14406
14407static void
8de0566d
YQ
14408invalidate_bp_value_on_memory_change (struct inferior *inferior,
14409 CORE_ADDR addr, ssize_t len,
1f3b5d1b
PP
14410 const bfd_byte *data)
14411{
14412 struct breakpoint *bp;
14413
14414 ALL_BREAKPOINTS (bp)
14415 if (bp->enable_state == bp_enabled
3a5c3e22 14416 && bp->type == bp_hardware_watchpoint)
1f3b5d1b 14417 {
3a5c3e22 14418 struct watchpoint *wp = (struct watchpoint *) bp;
1f3b5d1b 14419
850645cf 14420 if (wp->val_valid && wp->val != nullptr)
3a5c3e22
PA
14421 {
14422 struct bp_location *loc;
14423
14424 for (loc = bp->loc; loc != NULL; loc = loc->next)
14425 if (loc->loc_type == bp_loc_hardware_watchpoint
14426 && loc->address + loc->length > addr
14427 && addr + len > loc->address)
14428 {
3a5c3e22 14429 wp->val = NULL;
4c1d86d9 14430 wp->val_valid = false;
3a5c3e22
PA
14431 }
14432 }
1f3b5d1b
PP
14433 }
14434}
14435
8181d85f
DJ
14436/* Create and insert a breakpoint for software single step. */
14437
14438void
6c95b8df 14439insert_single_step_breakpoint (struct gdbarch *gdbarch,
accd0bcd 14440 const address_space *aspace,
4a64f543 14441 CORE_ADDR next_pc)
8181d85f 14442{
7c16b83e
PA
14443 struct thread_info *tp = inferior_thread ();
14444 struct symtab_and_line sal;
14445 CORE_ADDR pc = next_pc;
8181d85f 14446
34b7e8a6
PA
14447 if (tp->control.single_step_breakpoints == NULL)
14448 {
14449 tp->control.single_step_breakpoints
5d5658a1 14450 = new_single_step_breakpoint (tp->global_num, gdbarch);
34b7e8a6 14451 }
8181d85f 14452
7c16b83e
PA
14453 sal = find_pc_line (pc, 0);
14454 sal.pc = pc;
14455 sal.section = find_pc_overlay (pc);
14456 sal.explicit_pc = 1;
34b7e8a6 14457 add_location_to_breakpoint (tp->control.single_step_breakpoints, &sal);
8181d85f 14458
7c16b83e 14459 update_global_location_list (UGLL_INSERT);
8181d85f
DJ
14460}
14461
93f9a11f
YQ
14462/* Insert single step breakpoints according to the current state. */
14463
14464int
14465insert_single_step_breakpoints (struct gdbarch *gdbarch)
14466{
f5ea389a 14467 struct regcache *regcache = get_current_regcache ();
a0ff9e1a 14468 std::vector<CORE_ADDR> next_pcs;
93f9a11f 14469
f5ea389a 14470 next_pcs = gdbarch_software_single_step (gdbarch, regcache);
93f9a11f 14471
a0ff9e1a 14472 if (!next_pcs.empty ())
93f9a11f 14473 {
f5ea389a 14474 struct frame_info *frame = get_current_frame ();
8b86c959 14475 const address_space *aspace = get_frame_address_space (frame);
93f9a11f 14476
a0ff9e1a 14477 for (CORE_ADDR pc : next_pcs)
93f9a11f
YQ
14478 insert_single_step_breakpoint (gdbarch, aspace, pc);
14479
93f9a11f
YQ
14480 return 1;
14481 }
14482 else
14483 return 0;
14484}
14485
34b7e8a6 14486/* See breakpoint.h. */
f02253f1
HZ
14487
14488int
7c16b83e 14489breakpoint_has_location_inserted_here (struct breakpoint *bp,
accd0bcd 14490 const address_space *aspace,
7c16b83e 14491 CORE_ADDR pc)
1aafd4da 14492{
7c16b83e 14493 struct bp_location *loc;
1aafd4da 14494
7c16b83e
PA
14495 for (loc = bp->loc; loc != NULL; loc = loc->next)
14496 if (loc->inserted
14497 && breakpoint_location_address_match (loc, aspace, pc))
14498 return 1;
1aafd4da 14499
7c16b83e 14500 return 0;
ef370185
JB
14501}
14502
14503/* Check whether a software single-step breakpoint is inserted at
14504 PC. */
14505
14506int
accd0bcd 14507single_step_breakpoint_inserted_here_p (const address_space *aspace,
ef370185
JB
14508 CORE_ADDR pc)
14509{
34b7e8a6
PA
14510 struct breakpoint *bpt;
14511
14512 ALL_BREAKPOINTS (bpt)
14513 {
14514 if (bpt->type == bp_single_step
14515 && breakpoint_has_location_inserted_here (bpt, aspace, pc))
14516 return 1;
14517 }
14518 return 0;
1aafd4da
UW
14519}
14520
1042e4c0
SS
14521/* Tracepoint-specific operations. */
14522
14523/* Set tracepoint count to NUM. */
14524static void
14525set_tracepoint_count (int num)
14526{
14527 tracepoint_count = num;
4fa62494 14528 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
1042e4c0
SS
14529}
14530
70221824 14531static void
0b39b52e 14532trace_command (const char *arg, int from_tty)
1042e4c0 14533{
ffc2605c
TT
14534 event_location_up location = string_to_event_location (&arg,
14535 current_language);
bac7c5cf
GB
14536 const struct breakpoint_ops *ops = breakpoint_ops_for_event_location
14537 (location.get (), true /* is_tracepoint */);
55aa24fb 14538
558a9d82 14539 create_breakpoint (get_current_arch (),
ffc2605c 14540 location.get (),
f00aae0f 14541 NULL, 0, arg, 1 /* parse arg */,
558a9d82
YQ
14542 0 /* tempflag */,
14543 bp_tracepoint /* type_wanted */,
14544 0 /* Ignore count */,
14545 pending_break_support,
14546 ops,
14547 from_tty,
14548 1 /* enabled */,
14549 0 /* internal */, 0);
1042e4c0
SS
14550}
14551
70221824 14552static void
0b39b52e 14553ftrace_command (const char *arg, int from_tty)
7a697b8d 14554{
ffc2605c
TT
14555 event_location_up location = string_to_event_location (&arg,
14556 current_language);
558a9d82 14557 create_breakpoint (get_current_arch (),
ffc2605c 14558 location.get (),
f00aae0f 14559 NULL, 0, arg, 1 /* parse arg */,
558a9d82
YQ
14560 0 /* tempflag */,
14561 bp_fast_tracepoint /* type_wanted */,
14562 0 /* Ignore count */,
14563 pending_break_support,
14564 &tracepoint_breakpoint_ops,
14565 from_tty,
14566 1 /* enabled */,
14567 0 /* internal */, 0);
0fb4aa4b
PA
14568}
14569
14570/* strace command implementation. Creates a static tracepoint. */
14571
70221824 14572static void
0b39b52e 14573strace_command (const char *arg, int from_tty)
0fb4aa4b 14574{
983af33b 14575 struct breakpoint_ops *ops;
ffc2605c 14576 event_location_up location;
983af33b
SDJ
14577
14578 /* Decide if we are dealing with a static tracepoint marker (`-m'),
14579 or with a normal static tracepoint. */
61012eef 14580 if (arg && startswith (arg, "-m") && isspace (arg[2]))
f00aae0f
KS
14581 {
14582 ops = &strace_marker_breakpoint_ops;
a20714ff 14583 location = new_linespec_location (&arg, symbol_name_match_type::FULL);
f00aae0f 14584 }
983af33b 14585 else
f00aae0f
KS
14586 {
14587 ops = &tracepoint_breakpoint_ops;
14588 location = string_to_event_location (&arg, current_language);
14589 }
983af33b 14590
558a9d82 14591 create_breakpoint (get_current_arch (),
ffc2605c 14592 location.get (),
f00aae0f 14593 NULL, 0, arg, 1 /* parse arg */,
558a9d82
YQ
14594 0 /* tempflag */,
14595 bp_static_tracepoint /* type_wanted */,
14596 0 /* Ignore count */,
14597 pending_break_support,
14598 ops,
14599 from_tty,
14600 1 /* enabled */,
14601 0 /* internal */, 0);
7a697b8d
SS
14602}
14603
409873ef
SS
14604/* Set up a fake reader function that gets command lines from a linked
14605 list that was acquired during tracepoint uploading. */
14606
14607static struct uploaded_tp *this_utp;
3149d8c1 14608static int next_cmd;
409873ef
SS
14609
14610static char *
14611read_uploaded_action (void)
14612{
a18ba4e4 14613 char *rslt = nullptr;
409873ef 14614
a18ba4e4
SM
14615 if (next_cmd < this_utp->cmd_strings.size ())
14616 {
67aa1f3c 14617 rslt = this_utp->cmd_strings[next_cmd].get ();
a18ba4e4
SM
14618 next_cmd++;
14619 }
409873ef
SS
14620
14621 return rslt;
14622}
14623
00bf0b85
SS
14624/* Given information about a tracepoint as recorded on a target (which
14625 can be either a live system or a trace file), attempt to create an
14626 equivalent GDB tracepoint. This is not a reliable process, since
14627 the target does not necessarily have all the information used when
14628 the tracepoint was originally defined. */
14629
d9b3f62e 14630struct tracepoint *
00bf0b85 14631create_tracepoint_from_upload (struct uploaded_tp *utp)
d5551862 14632{
f2fc3015
TT
14633 const char *addr_str;
14634 char small_buf[100];
d9b3f62e 14635 struct tracepoint *tp;
fd9b8c24 14636
409873ef 14637 if (utp->at_string)
67aa1f3c 14638 addr_str = utp->at_string.get ();
409873ef
SS
14639 else
14640 {
14641 /* In the absence of a source location, fall back to raw
14642 address. Since there is no way to confirm that the address
14643 means the same thing as when the trace was started, warn the
14644 user. */
3e43a32a
MS
14645 warning (_("Uploaded tracepoint %d has no "
14646 "source location, using raw address"),
409873ef 14647 utp->number);
8c042590 14648 xsnprintf (small_buf, sizeof (small_buf), "*%s", hex_string (utp->addr));
409873ef
SS
14649 addr_str = small_buf;
14650 }
14651
14652 /* There's not much we can do with a sequence of bytecodes. */
14653 if (utp->cond && !utp->cond_string)
3e43a32a
MS
14654 warning (_("Uploaded tracepoint %d condition "
14655 "has no source form, ignoring it"),
409873ef 14656 utp->number);
d5551862 14657
ffc2605c
TT
14658 event_location_up location = string_to_event_location (&addr_str,
14659 current_language);
8cdf0e15 14660 if (!create_breakpoint (get_current_arch (),
ffc2605c 14661 location.get (),
67aa1f3c 14662 utp->cond_string.get (), -1, addr_str,
e7e0cddf 14663 0 /* parse cond/thread */,
8cdf0e15 14664 0 /* tempflag */,
0fb4aa4b 14665 utp->type /* type_wanted */,
8cdf0e15
VP
14666 0 /* Ignore count */,
14667 pending_break_support,
348d480f 14668 &tracepoint_breakpoint_ops,
8cdf0e15 14669 0 /* from_tty */,
84f4c1fe 14670 utp->enabled /* enabled */,
44f238bb
PA
14671 0 /* internal */,
14672 CREATE_BREAKPOINT_FLAGS_INSERTED))
ffc2605c 14673 return NULL;
fd9b8c24 14674
409873ef 14675 /* Get the tracepoint we just created. */
fd9b8c24
PA
14676 tp = get_tracepoint (tracepoint_count);
14677 gdb_assert (tp != NULL);
d5551862 14678
00bf0b85
SS
14679 if (utp->pass > 0)
14680 {
8c042590 14681 xsnprintf (small_buf, sizeof (small_buf), "%d %d", utp->pass,
c1fc2657 14682 tp->number);
00bf0b85 14683
409873ef 14684 trace_pass_command (small_buf, 0);
00bf0b85
SS
14685 }
14686
409873ef
SS
14687 /* If we have uploaded versions of the original commands, set up a
14688 special-purpose "reader" function and call the usual command line
14689 reader, then pass the result to the breakpoint command-setting
14690 function. */
a18ba4e4 14691 if (!utp->cmd_strings.empty ())
00bf0b85 14692 {
12973681 14693 counted_command_line cmd_list;
00bf0b85 14694
409873ef 14695 this_utp = utp;
3149d8c1 14696 next_cmd = 0;
d5551862 14697
60b3cef2 14698 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL);
409873ef 14699
c1fc2657 14700 breakpoint_set_commands (tp, std::move (cmd_list));
00bf0b85 14701 }
a18ba4e4
SM
14702 else if (!utp->actions.empty ()
14703 || !utp->step_actions.empty ())
3e43a32a
MS
14704 warning (_("Uploaded tracepoint %d actions "
14705 "have no source form, ignoring them"),
409873ef 14706 utp->number);
00bf0b85 14707
f196051f 14708 /* Copy any status information that might be available. */
c1fc2657 14709 tp->hit_count = utp->hit_count;
f196051f
SS
14710 tp->traceframe_usage = utp->traceframe_usage;
14711
00bf0b85 14712 return tp;
d9b3f62e 14713}
00bf0b85 14714
1042e4c0
SS
14715/* Print information on tracepoint number TPNUM_EXP, or all if
14716 omitted. */
14717
14718static void
1d12d88f 14719info_tracepoints_command (const char *args, int from_tty)
1042e4c0 14720{
79a45e25 14721 struct ui_out *uiout = current_uiout;
e5a67952 14722 int num_printed;
1042e4c0 14723
5c458ae8 14724 num_printed = breakpoint_1 (args, false, is_tracepoint);
d77f58be
SS
14725
14726 if (num_printed == 0)
1042e4c0 14727 {
e5a67952 14728 if (args == NULL || *args == '\0')
112e8700 14729 uiout->message ("No tracepoints.\n");
d77f58be 14730 else
112e8700 14731 uiout->message ("No tracepoint matching '%s'.\n", args);
1042e4c0 14732 }
ad443146
SS
14733
14734 default_collect_info ();
1042e4c0
SS
14735}
14736
4a64f543 14737/* The 'enable trace' command enables tracepoints.
1042e4c0
SS
14738 Not supported by all targets. */
14739static void
5fed81ff 14740enable_trace_command (const char *args, int from_tty)
1042e4c0
SS
14741{
14742 enable_command (args, from_tty);
14743}
14744
4a64f543 14745/* The 'disable trace' command disables tracepoints.
1042e4c0
SS
14746 Not supported by all targets. */
14747static void
5fed81ff 14748disable_trace_command (const char *args, int from_tty)
1042e4c0
SS
14749{
14750 disable_command (args, from_tty);
14751}
14752
4a64f543 14753/* Remove a tracepoint (or all if no argument). */
1042e4c0 14754static void
4495129a 14755delete_trace_command (const char *arg, int from_tty)
1042e4c0 14756{
35df4500 14757 struct breakpoint *b, *b_tmp;
1042e4c0
SS
14758
14759 dont_repeat ();
14760
14761 if (arg == 0)
14762 {
14763 int breaks_to_delete = 0;
14764
14765 /* Delete all breakpoints if no argument.
14766 Do not delete internal or call-dummy breakpoints, these
4a64f543
MS
14767 have to be deleted with an explicit breakpoint number
14768 argument. */
1042e4c0 14769 ALL_TRACEPOINTS (b)
46c6471b 14770 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0
SS
14771 {
14772 breaks_to_delete = 1;
14773 break;
14774 }
1042e4c0
SS
14775
14776 /* Ask user only if there are some breakpoints to delete. */
14777 if (!from_tty
14778 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
14779 {
35df4500 14780 ALL_BREAKPOINTS_SAFE (b, b_tmp)
46c6471b 14781 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0 14782 delete_breakpoint (b);
1042e4c0
SS
14783 }
14784 }
14785 else
48649e1b 14786 map_breakpoint_numbers
b926417a 14787 (arg, [&] (breakpoint *br)
48649e1b 14788 {
b926417a 14789 iterate_over_related_breakpoints (br, delete_breakpoint);
48649e1b 14790 });
1042e4c0
SS
14791}
14792
197f0a60
TT
14793/* Helper function for trace_pass_command. */
14794
14795static void
d9b3f62e 14796trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
197f0a60 14797{
d9b3f62e 14798 tp->pass_count = count;
76727919 14799 gdb::observers::breakpoint_modified.notify (tp);
197f0a60
TT
14800 if (from_tty)
14801 printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
c1fc2657 14802 tp->number, count);
197f0a60
TT
14803}
14804
1042e4c0
SS
14805/* Set passcount for tracepoint.
14806
14807 First command argument is passcount, second is tracepoint number.
14808 If tracepoint number omitted, apply to most recently defined.
14809 Also accepts special argument "all". */
14810
14811static void
0b39b52e 14812trace_pass_command (const char *args, int from_tty)
1042e4c0 14813{
d9b3f62e 14814 struct tracepoint *t1;
0b39b52e 14815 ULONGEST count;
1042e4c0
SS
14816
14817 if (args == 0 || *args == 0)
3e43a32a
MS
14818 error (_("passcount command requires an "
14819 "argument (count + optional TP num)"));
1042e4c0 14820
0b39b52e 14821 count = strtoulst (args, &args, 10); /* Count comes first, then TP num. */
1042e4c0 14822
529480d0 14823 args = skip_spaces (args);
1042e4c0
SS
14824 if (*args && strncasecmp (args, "all", 3) == 0)
14825 {
d9b3f62e
PA
14826 struct breakpoint *b;
14827
1042e4c0 14828 args += 3; /* Skip special argument "all". */
1042e4c0
SS
14829 if (*args)
14830 error (_("Junk at end of arguments."));
1042e4c0 14831
d9b3f62e 14832 ALL_TRACEPOINTS (b)
197f0a60 14833 {
d9b3f62e 14834 t1 = (struct tracepoint *) b;
197f0a60
TT
14835 trace_pass_set_count (t1, count, from_tty);
14836 }
14837 }
14838 else if (*args == '\0')
1042e4c0 14839 {
5fa1d40e 14840 t1 = get_tracepoint_by_number (&args, NULL);
1042e4c0 14841 if (t1)
197f0a60
TT
14842 trace_pass_set_count (t1, count, from_tty);
14843 }
14844 else
14845 {
bfd28288
PA
14846 number_or_range_parser parser (args);
14847 while (!parser.finished ())
1042e4c0 14848 {
bfd28288 14849 t1 = get_tracepoint_by_number (&args, &parser);
197f0a60
TT
14850 if (t1)
14851 trace_pass_set_count (t1, count, from_tty);
1042e4c0
SS
14852 }
14853 }
1042e4c0
SS
14854}
14855
d9b3f62e 14856struct tracepoint *
1042e4c0
SS
14857get_tracepoint (int num)
14858{
14859 struct breakpoint *t;
14860
14861 ALL_TRACEPOINTS (t)
14862 if (t->number == num)
d9b3f62e 14863 return (struct tracepoint *) t;
1042e4c0
SS
14864
14865 return NULL;
14866}
14867
d5551862
SS
14868/* Find the tracepoint with the given target-side number (which may be
14869 different from the tracepoint number after disconnecting and
14870 reconnecting). */
14871
d9b3f62e 14872struct tracepoint *
d5551862
SS
14873get_tracepoint_by_number_on_target (int num)
14874{
d9b3f62e 14875 struct breakpoint *b;
d5551862 14876
d9b3f62e
PA
14877 ALL_TRACEPOINTS (b)
14878 {
14879 struct tracepoint *t = (struct tracepoint *) b;
14880
14881 if (t->number_on_target == num)
14882 return t;
14883 }
d5551862
SS
14884
14885 return NULL;
14886}
14887
1042e4c0 14888/* Utility: parse a tracepoint number and look it up in the list.
197f0a60 14889 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
5fa1d40e
YQ
14890 If the argument is missing, the most recent tracepoint
14891 (tracepoint_count) is returned. */
14892
d9b3f62e 14893struct tracepoint *
0b39b52e 14894get_tracepoint_by_number (const char **arg,
bfd28288 14895 number_or_range_parser *parser)
1042e4c0 14896{
1042e4c0
SS
14897 struct breakpoint *t;
14898 int tpnum;
0b39b52e 14899 const char *instring = arg == NULL ? NULL : *arg;
1042e4c0 14900
bfd28288 14901 if (parser != NULL)
197f0a60 14902 {
bfd28288
PA
14903 gdb_assert (!parser->finished ());
14904 tpnum = parser->get_number ();
197f0a60
TT
14905 }
14906 else if (arg == NULL || *arg == NULL || ! **arg)
5fa1d40e 14907 tpnum = tracepoint_count;
1042e4c0 14908 else
197f0a60 14909 tpnum = get_number (arg);
1042e4c0
SS
14910
14911 if (tpnum <= 0)
14912 {
14913 if (instring && *instring)
14914 printf_filtered (_("bad tracepoint number at or near '%s'\n"),
14915 instring);
14916 else
5fa1d40e 14917 printf_filtered (_("No previous tracepoint\n"));
1042e4c0
SS
14918 return NULL;
14919 }
14920
14921 ALL_TRACEPOINTS (t)
14922 if (t->number == tpnum)
14923 {
d9b3f62e 14924 return (struct tracepoint *) t;
1042e4c0
SS
14925 }
14926
1042e4c0
SS
14927 printf_unfiltered ("No tracepoint number %d.\n", tpnum);
14928 return NULL;
14929}
14930
d9b3f62e
PA
14931void
14932print_recreate_thread (struct breakpoint *b, struct ui_file *fp)
14933{
14934 if (b->thread != -1)
14935 fprintf_unfiltered (fp, " thread %d", b->thread);
14936
14937 if (b->task != 0)
14938 fprintf_unfiltered (fp, " task %d", b->task);
14939
14940 fprintf_unfiltered (fp, "\n");
14941}
14942
6149aea9
PA
14943/* Save information on user settable breakpoints (watchpoints, etc) to
14944 a new script file named FILENAME. If FILTER is non-NULL, call it
14945 on each breakpoint and only include the ones for which it returns
f2478a7e 14946 true. */
6149aea9 14947
1042e4c0 14948static void
4495129a 14949save_breakpoints (const char *filename, int from_tty,
f2478a7e 14950 bool (*filter) (const struct breakpoint *))
1042e4c0
SS
14951{
14952 struct breakpoint *tp;
6149aea9 14953 int any = 0;
6149aea9 14954 int extra_trace_bits = 0;
1042e4c0 14955
6149aea9
PA
14956 if (filename == 0 || *filename == 0)
14957 error (_("Argument required (file name in which to save)"));
1042e4c0
SS
14958
14959 /* See if we have anything to save. */
6149aea9 14960 ALL_BREAKPOINTS (tp)
1042e4c0 14961 {
6149aea9 14962 /* Skip internal and momentary breakpoints. */
09d682a4 14963 if (!user_breakpoint_p (tp))
6149aea9
PA
14964 continue;
14965
14966 /* If we have a filter, only save the breakpoints it accepts. */
14967 if (filter && !filter (tp))
14968 continue;
14969
14970 any = 1;
14971
14972 if (is_tracepoint (tp))
14973 {
14974 extra_trace_bits = 1;
14975
14976 /* We can stop searching. */
14977 break;
14978 }
1042e4c0 14979 }
6149aea9
PA
14980
14981 if (!any)
1042e4c0 14982 {
6149aea9 14983 warning (_("Nothing to save."));
1042e4c0
SS
14984 return;
14985 }
14986
ee0c3293 14987 gdb::unique_xmalloc_ptr<char> expanded_filename (tilde_expand (filename));
d7e74731
PA
14988
14989 stdio_file fp;
14990
ee0c3293 14991 if (!fp.open (expanded_filename.get (), "w"))
6149aea9 14992 error (_("Unable to open file '%s' for saving (%s)"),
ee0c3293 14993 expanded_filename.get (), safe_strerror (errno));
8bf6485c 14994
6149aea9 14995 if (extra_trace_bits)
d7e74731 14996 save_trace_state_variables (&fp);
8bf6485c 14997
6149aea9 14998 ALL_BREAKPOINTS (tp)
1042e4c0 14999 {
6149aea9 15000 /* Skip internal and momentary breakpoints. */
09d682a4 15001 if (!user_breakpoint_p (tp))
6149aea9 15002 continue;
8bf6485c 15003
6149aea9
PA
15004 /* If we have a filter, only save the breakpoints it accepts. */
15005 if (filter && !filter (tp))
15006 continue;
15007
d7e74731 15008 tp->ops->print_recreate (tp, &fp);
1042e4c0 15009
6149aea9
PA
15010 /* Note, we can't rely on tp->number for anything, as we can't
15011 assume the recreated breakpoint numbers will match. Use $bpnum
15012 instead. */
15013
15014 if (tp->cond_string)
d7e74731 15015 fp.printf (" condition $bpnum %s\n", tp->cond_string);
6149aea9
PA
15016
15017 if (tp->ignore_count)
d7e74731 15018 fp.printf (" ignore $bpnum %d\n", tp->ignore_count);
6149aea9 15019
2d9442cc 15020 if (tp->type != bp_dprintf && tp->commands)
1042e4c0 15021 {
d7e74731 15022 fp.puts (" commands\n");
a7bdde9e 15023
d7e74731 15024 current_uiout->redirect (&fp);
a70b8144 15025 try
1042e4c0 15026 {
d1b0a7bf 15027 print_command_lines (current_uiout, tp->commands.get (), 2);
a7bdde9e 15028 }
230d2906 15029 catch (const gdb_exception &ex)
492d29ea 15030 {
112e8700 15031 current_uiout->redirect (NULL);
eedc3f4f 15032 throw;
492d29ea 15033 }
1042e4c0 15034
112e8700 15035 current_uiout->redirect (NULL);
d7e74731 15036 fp.puts (" end\n");
1042e4c0 15037 }
6149aea9
PA
15038
15039 if (tp->enable_state == bp_disabled)
d7e74731 15040 fp.puts ("disable $bpnum\n");
6149aea9
PA
15041
15042 /* If this is a multi-location breakpoint, check if the locations
15043 should be individually disabled. Watchpoint locations are
15044 special, and not user visible. */
15045 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
15046 {
15047 struct bp_location *loc;
15048 int n = 1;
15049
15050 for (loc = tp->loc; loc != NULL; loc = loc->next, n++)
15051 if (!loc->enabled)
d7e74731 15052 fp.printf ("disable $bpnum.%d\n", n);
6149aea9 15053 }
1042e4c0 15054 }
8bf6485c 15055
6149aea9 15056 if (extra_trace_bits && *default_collect)
d7e74731 15057 fp.printf ("set default-collect %s\n", default_collect);
8bf6485c 15058
1042e4c0 15059 if (from_tty)
ee0c3293 15060 printf_filtered (_("Saved to file '%s'.\n"), expanded_filename.get ());
6149aea9
PA
15061}
15062
15063/* The `save breakpoints' command. */
15064
15065static void
4495129a 15066save_breakpoints_command (const char *args, int from_tty)
6149aea9
PA
15067{
15068 save_breakpoints (args, from_tty, NULL);
15069}
15070
15071/* The `save tracepoints' command. */
15072
15073static void
4495129a 15074save_tracepoints_command (const char *args, int from_tty)
6149aea9
PA
15075{
15076 save_breakpoints (args, from_tty, is_tracepoint);
1042e4c0
SS
15077}
15078
15079/* Create a vector of all tracepoints. */
15080
f51e0e20 15081std::vector<breakpoint *>
eeae04df 15082all_tracepoints (void)
1042e4c0 15083{
f51e0e20 15084 std::vector<breakpoint *> tp_vec;
1042e4c0
SS
15085 struct breakpoint *tp;
15086
15087 ALL_TRACEPOINTS (tp)
15088 {
f51e0e20 15089 tp_vec.push_back (tp);
1042e4c0
SS
15090 }
15091
15092 return tp_vec;
15093}
15094
c906108c 15095\f
629500fa
KS
15096/* This help string is used to consolidate all the help string for specifying
15097 locations used by several commands. */
15098
15099#define LOCATION_HELP_STRING \
15100"Linespecs are colon-separated lists of location parameters, such as\n\
15101source filename, function name, label name, and line number.\n\
15102Example: To specify the start of a label named \"the_top\" in the\n\
15103function \"fact\" in the file \"factorial.c\", use\n\
15104\"factorial.c:fact:the_top\".\n\
15105\n\
15106Address locations begin with \"*\" and specify an exact address in the\n\
15107program. Example: To specify the fourth byte past the start function\n\
15108\"main\", use \"*main + 4\".\n\
15109\n\
15110Explicit locations are similar to linespecs but use an option/argument\n\
15111syntax to specify location parameters.\n\
15112Example: To specify the start of the label named \"the_top\" in the\n\
15113function \"fact\" in the file \"factorial.c\", use \"-source factorial.c\n\
a20714ff
PA
15114-function fact -label the_top\".\n\
15115\n\
15116By default, a specified function is matched against the program's\n\
15117functions in all scopes. For C++, this means in all namespaces and\n\
15118classes. For Ada, this means in all packages. E.g., in C++,\n\
15119\"func()\" matches \"A::func()\", \"A::B::func()\", etc. The\n\
15120\"-qualified\" flag overrides this behavior, making GDB interpret the\n\
89549d7f 15121specified name as a complete fully-qualified name instead."
629500fa 15122
4a64f543
MS
15123/* This help string is used for the break, hbreak, tbreak and thbreak
15124 commands. It is defined as a macro to prevent duplication.
15125 COMMAND should be a string constant containing the name of the
15126 command. */
629500fa 15127
31e2b00f 15128#define BREAK_ARGS_HELP(command) \
fb7b5af4
SDJ
15129command" [PROBE_MODIFIER] [LOCATION] [thread THREADNUM] [if CONDITION]\n\
15130PROBE_MODIFIER shall be present if the command is to be placed in a\n\
15131probe point. Accepted values are `-probe' (for a generic, automatically\n\
d4777acb
JM
15132guessed probe type), `-probe-stap' (for a SystemTap probe) or \n\
15133`-probe-dtrace' (for a DTrace probe).\n\
629500fa
KS
15134LOCATION may be a linespec, address, or explicit location as described\n\
15135below.\n\
15136\n\
dc10affe
PA
15137With no LOCATION, uses current execution address of the selected\n\
15138stack frame. This is useful for breaking on return to a stack frame.\n\
31e2b00f
AS
15139\n\
15140THREADNUM is the number from \"info threads\".\n\
15141CONDITION is a boolean expression.\n\
89549d7f 15142\n" LOCATION_HELP_STRING "\n\n\
d41c0fc8
PA
15143Multiple breakpoints at one place are permitted, and useful if their\n\
15144conditions are different.\n\
31e2b00f
AS
15145\n\
15146Do \"help breakpoints\" for info on other commands dealing with breakpoints."
15147
44feb3ce
TT
15148/* List of subcommands for "catch". */
15149static struct cmd_list_element *catch_cmdlist;
15150
15151/* List of subcommands for "tcatch". */
15152static struct cmd_list_element *tcatch_cmdlist;
15153
9ac4176b 15154void
a121b7c1 15155add_catch_command (const char *name, const char *docstring,
eb4c3f4a 15156 cmd_const_sfunc_ftype *sfunc,
625e8578 15157 completer_ftype *completer,
44feb3ce
TT
15158 void *user_data_catch,
15159 void *user_data_tcatch)
15160{
15161 struct cmd_list_element *command;
15162
0450cc4c 15163 command = add_cmd (name, class_breakpoint, docstring,
44feb3ce
TT
15164 &catch_cmdlist);
15165 set_cmd_sfunc (command, sfunc);
15166 set_cmd_context (command, user_data_catch);
a96d9b2e 15167 set_cmd_completer (command, completer);
44feb3ce 15168
0450cc4c 15169 command = add_cmd (name, class_breakpoint, docstring,
44feb3ce
TT
15170 &tcatch_cmdlist);
15171 set_cmd_sfunc (command, sfunc);
15172 set_cmd_context (command, user_data_tcatch);
a96d9b2e 15173 set_cmd_completer (command, completer);
44feb3ce
TT
15174}
15175
84f4c1fe 15176struct breakpoint *
95da600f 15177iterate_over_breakpoints (gdb::function_view<bool (breakpoint *)> callback)
84f4c1fe 15178{
35df4500 15179 struct breakpoint *b, *b_tmp;
84f4c1fe 15180
35df4500 15181 ALL_BREAKPOINTS_SAFE (b, b_tmp)
84f4c1fe 15182 {
95da600f 15183 if (callback (b))
84f4c1fe
PM
15184 return b;
15185 }
15186
15187 return NULL;
15188}
15189
0574c78f
GB
15190/* Zero if any of the breakpoint's locations could be a location where
15191 functions have been inlined, nonzero otherwise. */
15192
15193static int
15194is_non_inline_function (struct breakpoint *b)
15195{
15196 /* The shared library event breakpoint is set on the address of a
15197 non-inline function. */
15198 if (b->type == bp_shlib_event)
15199 return 1;
15200
15201 return 0;
15202}
15203
15204/* Nonzero if the specified PC cannot be a location where functions
15205 have been inlined. */
15206
15207int
accd0bcd 15208pc_at_non_inline_function (const address_space *aspace, CORE_ADDR pc,
09ac7c10 15209 const struct target_waitstatus *ws)
0574c78f
GB
15210{
15211 struct breakpoint *b;
15212 struct bp_location *bl;
15213
15214 ALL_BREAKPOINTS (b)
15215 {
15216 if (!is_non_inline_function (b))
15217 continue;
15218
15219 for (bl = b->loc; bl != NULL; bl = bl->next)
15220 {
15221 if (!bl->shlib_disabled
09ac7c10 15222 && bpstat_check_location (bl, aspace, pc, ws))
0574c78f
GB
15223 return 1;
15224 }
15225 }
15226
15227 return 0;
15228}
15229
2f202fde
JK
15230/* Remove any references to OBJFILE which is going to be freed. */
15231
15232void
15233breakpoint_free_objfile (struct objfile *objfile)
15234{
15235 struct bp_location **locp, *loc;
15236
15237 ALL_BP_LOCATIONS (loc, locp)
eb822aa6 15238 if (loc->symtab != NULL && SYMTAB_OBJFILE (loc->symtab) == objfile)
2f202fde
JK
15239 loc->symtab = NULL;
15240}
15241
2060206e
PA
15242void
15243initialize_breakpoint_ops (void)
15244{
15245 static int initialized = 0;
15246
15247 struct breakpoint_ops *ops;
15248
15249 if (initialized)
15250 return;
15251 initialized = 1;
15252
15253 /* The breakpoint_ops structure to be inherit by all kinds of
15254 breakpoints (real breakpoints, i.e., user "break" breakpoints,
15255 internal and momentary breakpoints, etc.). */
15256 ops = &bkpt_base_breakpoint_ops;
15257 *ops = base_breakpoint_ops;
15258 ops->re_set = bkpt_re_set;
15259 ops->insert_location = bkpt_insert_location;
15260 ops->remove_location = bkpt_remove_location;
15261 ops->breakpoint_hit = bkpt_breakpoint_hit;
5f700d83 15262 ops->create_sals_from_location = bkpt_create_sals_from_location;
983af33b 15263 ops->create_breakpoints_sal = bkpt_create_breakpoints_sal;
5f700d83 15264 ops->decode_location = bkpt_decode_location;
2060206e
PA
15265
15266 /* The breakpoint_ops structure to be used in regular breakpoints. */
15267 ops = &bkpt_breakpoint_ops;
15268 *ops = bkpt_base_breakpoint_ops;
15269 ops->re_set = bkpt_re_set;
15270 ops->resources_needed = bkpt_resources_needed;
15271 ops->print_it = bkpt_print_it;
15272 ops->print_mention = bkpt_print_mention;
15273 ops->print_recreate = bkpt_print_recreate;
15274
15275 /* Ranged breakpoints. */
15276 ops = &ranged_breakpoint_ops;
15277 *ops = bkpt_breakpoint_ops;
15278 ops->breakpoint_hit = breakpoint_hit_ranged_breakpoint;
15279 ops->resources_needed = resources_needed_ranged_breakpoint;
15280 ops->print_it = print_it_ranged_breakpoint;
15281 ops->print_one = print_one_ranged_breakpoint;
15282 ops->print_one_detail = print_one_detail_ranged_breakpoint;
15283 ops->print_mention = print_mention_ranged_breakpoint;
15284 ops->print_recreate = print_recreate_ranged_breakpoint;
15285
15286 /* Internal breakpoints. */
15287 ops = &internal_breakpoint_ops;
15288 *ops = bkpt_base_breakpoint_ops;
15289 ops->re_set = internal_bkpt_re_set;
15290 ops->check_status = internal_bkpt_check_status;
15291 ops->print_it = internal_bkpt_print_it;
15292 ops->print_mention = internal_bkpt_print_mention;
15293
15294 /* Momentary breakpoints. */
15295 ops = &momentary_breakpoint_ops;
15296 *ops = bkpt_base_breakpoint_ops;
15297 ops->re_set = momentary_bkpt_re_set;
15298 ops->check_status = momentary_bkpt_check_status;
15299 ops->print_it = momentary_bkpt_print_it;
15300 ops->print_mention = momentary_bkpt_print_mention;
15301
55aa24fb
SDJ
15302 /* Probe breakpoints. */
15303 ops = &bkpt_probe_breakpoint_ops;
15304 *ops = bkpt_breakpoint_ops;
15305 ops->insert_location = bkpt_probe_insert_location;
15306 ops->remove_location = bkpt_probe_remove_location;
5f700d83
KS
15307 ops->create_sals_from_location = bkpt_probe_create_sals_from_location;
15308 ops->decode_location = bkpt_probe_decode_location;
55aa24fb 15309
2060206e
PA
15310 /* Watchpoints. */
15311 ops = &watchpoint_breakpoint_ops;
15312 *ops = base_breakpoint_ops;
15313 ops->re_set = re_set_watchpoint;
15314 ops->insert_location = insert_watchpoint;
15315 ops->remove_location = remove_watchpoint;
15316 ops->breakpoint_hit = breakpoint_hit_watchpoint;
15317 ops->check_status = check_status_watchpoint;
15318 ops->resources_needed = resources_needed_watchpoint;
15319 ops->works_in_software_mode = works_in_software_mode_watchpoint;
15320 ops->print_it = print_it_watchpoint;
15321 ops->print_mention = print_mention_watchpoint;
15322 ops->print_recreate = print_recreate_watchpoint;
427cd150 15323 ops->explains_signal = explains_signal_watchpoint;
2060206e
PA
15324
15325 /* Masked watchpoints. */
15326 ops = &masked_watchpoint_breakpoint_ops;
15327 *ops = watchpoint_breakpoint_ops;
15328 ops->insert_location = insert_masked_watchpoint;
15329 ops->remove_location = remove_masked_watchpoint;
15330 ops->resources_needed = resources_needed_masked_watchpoint;
15331 ops->works_in_software_mode = works_in_software_mode_masked_watchpoint;
15332 ops->print_it = print_it_masked_watchpoint;
15333 ops->print_one_detail = print_one_detail_masked_watchpoint;
15334 ops->print_mention = print_mention_masked_watchpoint;
15335 ops->print_recreate = print_recreate_masked_watchpoint;
15336
15337 /* Tracepoints. */
15338 ops = &tracepoint_breakpoint_ops;
15339 *ops = base_breakpoint_ops;
15340 ops->re_set = tracepoint_re_set;
15341 ops->breakpoint_hit = tracepoint_breakpoint_hit;
15342 ops->print_one_detail = tracepoint_print_one_detail;
15343 ops->print_mention = tracepoint_print_mention;
15344 ops->print_recreate = tracepoint_print_recreate;
5f700d83 15345 ops->create_sals_from_location = tracepoint_create_sals_from_location;
983af33b 15346 ops->create_breakpoints_sal = tracepoint_create_breakpoints_sal;
5f700d83 15347 ops->decode_location = tracepoint_decode_location;
983af33b 15348
55aa24fb
SDJ
15349 /* Probe tracepoints. */
15350 ops = &tracepoint_probe_breakpoint_ops;
15351 *ops = tracepoint_breakpoint_ops;
5f700d83
KS
15352 ops->create_sals_from_location = tracepoint_probe_create_sals_from_location;
15353 ops->decode_location = tracepoint_probe_decode_location;
55aa24fb 15354
983af33b
SDJ
15355 /* Static tracepoints with marker (`-m'). */
15356 ops = &strace_marker_breakpoint_ops;
15357 *ops = tracepoint_breakpoint_ops;
5f700d83 15358 ops->create_sals_from_location = strace_marker_create_sals_from_location;
983af33b 15359 ops->create_breakpoints_sal = strace_marker_create_breakpoints_sal;
5f700d83 15360 ops->decode_location = strace_marker_decode_location;
2060206e
PA
15361
15362 /* Fork catchpoints. */
15363 ops = &catch_fork_breakpoint_ops;
15364 *ops = base_breakpoint_ops;
15365 ops->insert_location = insert_catch_fork;
15366 ops->remove_location = remove_catch_fork;
15367 ops->breakpoint_hit = breakpoint_hit_catch_fork;
15368 ops->print_it = print_it_catch_fork;
15369 ops->print_one = print_one_catch_fork;
15370 ops->print_mention = print_mention_catch_fork;
15371 ops->print_recreate = print_recreate_catch_fork;
15372
15373 /* Vfork catchpoints. */
15374 ops = &catch_vfork_breakpoint_ops;
15375 *ops = base_breakpoint_ops;
15376 ops->insert_location = insert_catch_vfork;
15377 ops->remove_location = remove_catch_vfork;
15378 ops->breakpoint_hit = breakpoint_hit_catch_vfork;
15379 ops->print_it = print_it_catch_vfork;
15380 ops->print_one = print_one_catch_vfork;
15381 ops->print_mention = print_mention_catch_vfork;
15382 ops->print_recreate = print_recreate_catch_vfork;
15383
15384 /* Exec catchpoints. */
15385 ops = &catch_exec_breakpoint_ops;
15386 *ops = base_breakpoint_ops;
2060206e
PA
15387 ops->insert_location = insert_catch_exec;
15388 ops->remove_location = remove_catch_exec;
15389 ops->breakpoint_hit = breakpoint_hit_catch_exec;
15390 ops->print_it = print_it_catch_exec;
15391 ops->print_one = print_one_catch_exec;
15392 ops->print_mention = print_mention_catch_exec;
15393 ops->print_recreate = print_recreate_catch_exec;
15394
edcc5120
TT
15395 /* Solib-related catchpoints. */
15396 ops = &catch_solib_breakpoint_ops;
15397 *ops = base_breakpoint_ops;
edcc5120
TT
15398 ops->insert_location = insert_catch_solib;
15399 ops->remove_location = remove_catch_solib;
15400 ops->breakpoint_hit = breakpoint_hit_catch_solib;
15401 ops->check_status = check_status_catch_solib;
15402 ops->print_it = print_it_catch_solib;
15403 ops->print_one = print_one_catch_solib;
15404 ops->print_mention = print_mention_catch_solib;
15405 ops->print_recreate = print_recreate_catch_solib;
e7e0cddf
SS
15406
15407 ops = &dprintf_breakpoint_ops;
15408 *ops = bkpt_base_breakpoint_ops;
5c2b4418 15409 ops->re_set = dprintf_re_set;
e7e0cddf
SS
15410 ops->resources_needed = bkpt_resources_needed;
15411 ops->print_it = bkpt_print_it;
15412 ops->print_mention = bkpt_print_mention;
2d9442cc 15413 ops->print_recreate = dprintf_print_recreate;
9d6e6e84 15414 ops->after_condition_true = dprintf_after_condition_true;
cd1608cc 15415 ops->breakpoint_hit = dprintf_breakpoint_hit;
2060206e
PA
15416}
15417
8bfd80db
YQ
15418/* Chain containing all defined "enable breakpoint" subcommands. */
15419
15420static struct cmd_list_element *enablebreaklist = NULL;
15421
8588b356
SM
15422/* See breakpoint.h. */
15423
15424cmd_list_element *commands_cmd_element = nullptr;
15425
6c265988 15426void _initialize_breakpoint ();
c906108c 15427void
6c265988 15428_initialize_breakpoint ()
c906108c
SS
15429{
15430 struct cmd_list_element *c;
15431
2060206e
PA
15432 initialize_breakpoint_ops ();
15433
76727919
TT
15434 gdb::observers::solib_unloaded.attach (disable_breakpoints_in_unloaded_shlib);
15435 gdb::observers::free_objfile.attach (disable_breakpoints_in_freed_objfile);
15436 gdb::observers::memory_changed.attach (invalidate_bp_value_on_memory_change);
84acb35a 15437
c906108c
SS
15438 breakpoint_chain = 0;
15439 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
15440 before a breakpoint is set. */
15441 breakpoint_count = 0;
15442
1042e4c0
SS
15443 tracepoint_count = 0;
15444
1bedd215
AC
15445 add_com ("ignore", class_breakpoint, ignore_command, _("\
15446Set ignore-count of breakpoint number N to COUNT.\n\
15447Usage is `ignore N COUNT'."));
c906108c 15448
8588b356
SM
15449 commands_cmd_element = add_com ("commands", class_breakpoint,
15450 commands_command, _("\
18da0c51
MG
15451Set commands to be executed when the given breakpoints are hit.\n\
15452Give a space-separated breakpoint list as argument after \"commands\".\n\
15453A list element can be a breakpoint number (e.g. `5') or a range of numbers\n\
15454(e.g. `5-7').\n\
c906108c
SS
15455With no argument, the targeted breakpoint is the last one set.\n\
15456The commands themselves follow starting on the next line.\n\
15457Type a line containing \"end\" to indicate the end of them.\n\
15458Give \"silent\" as the first line to make the breakpoint silent;\n\
1bedd215 15459then no output is printed when it is hit, except what the commands print."));
c906108c 15460
d55637df 15461 c = add_com ("condition", class_breakpoint, condition_command, _("\
1bedd215 15462Specify breakpoint number N to break only if COND is true.\n\
c906108c 15463Usage is `condition N COND', where N is an integer and COND is an\n\
1bedd215 15464expression to be evaluated whenever breakpoint N is reached."));
d55637df 15465 set_cmd_completer (c, condition_completer);
c906108c 15466
1bedd215 15467 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
31e2b00f 15468Set a temporary breakpoint.\n\
c906108c
SS
15469Like \"break\" except the breakpoint is only temporary,\n\
15470so it will be deleted when hit. Equivalent to \"break\" followed\n\
31e2b00f
AS
15471by using \"enable delete\" on the breakpoint number.\n\
15472\n"
15473BREAK_ARGS_HELP ("tbreak")));
5ba2abeb 15474 set_cmd_completer (c, location_completer);
c94fdfd0 15475
1bedd215 15476 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
a3be7890 15477Set a hardware assisted breakpoint.\n\
c906108c 15478Like \"break\" except the breakpoint requires hardware support,\n\
31e2b00f
AS
15479some target hardware may not have this support.\n\
15480\n"
15481BREAK_ARGS_HELP ("hbreak")));
5ba2abeb 15482 set_cmd_completer (c, location_completer);
c906108c 15483
1bedd215 15484 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
31e2b00f 15485Set a temporary hardware assisted breakpoint.\n\
c906108c 15486Like \"hbreak\" except the breakpoint is only temporary,\n\
31e2b00f
AS
15487so it will be deleted when hit.\n\
15488\n"
15489BREAK_ARGS_HELP ("thbreak")));
5ba2abeb 15490 set_cmd_completer (c, location_completer);
c906108c 15491
1bedd215 15492 add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
590042fc
PW
15493Enable all or some breakpoints.\n\
15494Usage: enable [BREAKPOINTNUM]...\n\
c906108c
SS
15495Give breakpoint numbers (separated by spaces) as arguments.\n\
15496With no subcommand, breakpoints are enabled until you command otherwise.\n\
15497This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 15498With a subcommand you can enable temporarily."),
c906108c 15499 &enablelist, "enable ", 1, &cmdlist);
c906108c
SS
15500
15501 add_com_alias ("en", "enable", class_breakpoint, 1);
15502
84951ab5 15503 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
590042fc
PW
15504Enable all or some breakpoints.\n\
15505Usage: enable breakpoints [BREAKPOINTNUM]...\n\
c906108c
SS
15506Give breakpoint numbers (separated by spaces) as arguments.\n\
15507This is used to cancel the effect of the \"disable\" command.\n\
89549d7f 15508May be abbreviated to simply \"enable\"."),
c5aa993b 15509 &enablebreaklist, "enable breakpoints ", 1, &enablelist);
c906108c 15510
1a966eab 15511 add_cmd ("once", no_class, enable_once_command, _("\
590042fc
PW
15512Enable some breakpoints for one hit.\n\
15513Usage: enable breakpoints once BREAKPOINTNUM...\n\
1a966eab 15514If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
c906108c
SS
15515 &enablebreaklist);
15516
1a966eab 15517 add_cmd ("delete", no_class, enable_delete_command, _("\
590042fc
PW
15518Enable some breakpoints and delete when hit.\n\
15519Usage: enable breakpoints delete BREAKPOINTNUM...\n\
1a966eab 15520If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
15521 &enablebreaklist);
15522
816338b5 15523 add_cmd ("count", no_class, enable_count_command, _("\
590042fc
PW
15524Enable some breakpoints for COUNT hits.\n\
15525Usage: enable breakpoints count COUNT BREAKPOINTNUM...\n\
816338b5
SS
15526If a breakpoint is hit while enabled in this fashion,\n\
15527the count is decremented; when it reaches zero, the breakpoint is disabled."),
15528 &enablebreaklist);
15529
1a966eab 15530 add_cmd ("delete", no_class, enable_delete_command, _("\
590042fc
PW
15531Enable some breakpoints and delete when hit.\n\
15532Usage: enable delete BREAKPOINTNUM...\n\
1a966eab 15533If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
15534 &enablelist);
15535
1a966eab 15536 add_cmd ("once", no_class, enable_once_command, _("\
590042fc
PW
15537Enable some breakpoints for one hit.\n\
15538Usage: enable once BREAKPOINTNUM...\n\
1a966eab 15539If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
816338b5
SS
15540 &enablelist);
15541
15542 add_cmd ("count", no_class, enable_count_command, _("\
590042fc
PW
15543Enable some breakpoints for COUNT hits.\n\
15544Usage: enable count COUNT BREAKPOINTNUM...\n\
816338b5
SS
15545If a breakpoint is hit while enabled in this fashion,\n\
15546the count is decremented; when it reaches zero, the breakpoint is disabled."),
c906108c
SS
15547 &enablelist);
15548
1bedd215 15549 add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
590042fc
PW
15550Disable all or some breakpoints.\n\
15551Usage: disable [BREAKPOINTNUM]...\n\
c906108c
SS
15552Arguments are breakpoint numbers with spaces in between.\n\
15553To disable all breakpoints, give no argument.\n\
64b9b334 15554A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
c906108c
SS
15555 &disablelist, "disable ", 1, &cmdlist);
15556 add_com_alias ("dis", "disable", class_breakpoint, 1);
15557 add_com_alias ("disa", "disable", class_breakpoint, 1);
c906108c 15558
57b4f16e 15559 add_cmd ("breakpoints", class_breakpoint, disable_command, _("\
590042fc
PW
15560Disable all or some breakpoints.\n\
15561Usage: disable breakpoints [BREAKPOINTNUM]...\n\
c906108c
SS
15562Arguments are breakpoint numbers with spaces in between.\n\
15563To disable all breakpoints, give no argument.\n\
64b9b334 15564A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
1a966eab 15565This command may be abbreviated \"disable\"."),
c906108c
SS
15566 &disablelist);
15567
1bedd215 15568 add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
590042fc
PW
15569Delete all or some breakpoints.\n\
15570Usage: delete [BREAKPOINTNUM]...\n\
c906108c
SS
15571Arguments are breakpoint numbers with spaces in between.\n\
15572To delete all breakpoints, give no argument.\n\
15573\n\
590042fc 15574Also a prefix command for deletion of other GDB objects."),
c906108c
SS
15575 &deletelist, "delete ", 1, &cmdlist);
15576 add_com_alias ("d", "delete", class_breakpoint, 1);
7f198e01 15577 add_com_alias ("del", "delete", class_breakpoint, 1);
c906108c 15578
57b4f16e 15579 add_cmd ("breakpoints", class_breakpoint, delete_command, _("\
590042fc
PW
15580Delete all or some breakpoints or auto-display expressions.\n\
15581Usage: delete breakpoints [BREAKPOINTNUM]...\n\
c906108c
SS
15582Arguments are breakpoint numbers with spaces in between.\n\
15583To delete all breakpoints, give no argument.\n\
1a966eab 15584This command may be abbreviated \"delete\"."),
c906108c
SS
15585 &deletelist);
15586
1bedd215 15587 add_com ("clear", class_breakpoint, clear_command, _("\
629500fa
KS
15588Clear breakpoint at specified location.\n\
15589Argument may be a linespec, explicit, or address location as described below.\n\
1bedd215
AC
15590\n\
15591With no argument, clears all breakpoints in the line that the selected frame\n\
629500fa 15592is executing in.\n"
89549d7f 15593"\n" LOCATION_HELP_STRING "\n\n\
1bedd215 15594See also the \"delete\" command which clears breakpoints by number."));
a6cc4789 15595 add_com_alias ("cl", "clear", class_breakpoint, 1);
c906108c 15596
1bedd215 15597 c = add_com ("break", class_breakpoint, break_command, _("\
629500fa 15598Set breakpoint at specified location.\n"
31e2b00f 15599BREAK_ARGS_HELP ("break")));
5ba2abeb 15600 set_cmd_completer (c, location_completer);
c94fdfd0 15601
c906108c
SS
15602 add_com_alias ("b", "break", class_run, 1);
15603 add_com_alias ("br", "break", class_run, 1);
15604 add_com_alias ("bre", "break", class_run, 1);
15605 add_com_alias ("brea", "break", class_run, 1);
15606
c906108c
SS
15607 if (dbx_commands)
15608 {
1bedd215
AC
15609 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
15610Break in function/address or break at a line in the current file."),
c5aa993b
JM
15611 &stoplist, "stop ", 1, &cmdlist);
15612 add_cmd ("in", class_breakpoint, stopin_command,
1a966eab 15613 _("Break in function or address."), &stoplist);
c5aa993b 15614 add_cmd ("at", class_breakpoint, stopat_command,
1a966eab 15615 _("Break at a line in the current file."), &stoplist);
11db9430 15616 add_com ("status", class_info, info_breakpoints_command, _("\
1bedd215 15617Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
15618The \"Type\" column indicates one of:\n\
15619\tbreakpoint - normal breakpoint\n\
15620\twatchpoint - watchpoint\n\
15621The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15622the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15623breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
15624address and file/line number respectively.\n\
15625\n\
15626Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
15627are set to the address of the last breakpoint listed unless the command\n\
15628is prefixed with \"server \".\n\n\
c906108c 15629Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 15630breakpoint set."));
c906108c
SS
15631 }
15632
11db9430 15633 add_info ("breakpoints", info_breakpoints_command, _("\
e5a67952 15634Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
c906108c
SS
15635The \"Type\" column indicates one of:\n\
15636\tbreakpoint - normal breakpoint\n\
15637\twatchpoint - watchpoint\n\
15638The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15639the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15640breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
15641address and file/line number respectively.\n\
15642\n\
15643Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
15644are set to the address of the last breakpoint listed unless the command\n\
15645is prefixed with \"server \".\n\n\
c906108c 15646Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 15647breakpoint set."));
c906108c 15648
6b04bdb7
MS
15649 add_info_alias ("b", "breakpoints", 1);
15650
1a966eab
AC
15651 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
15652Status of all breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
15653The \"Type\" column indicates one of:\n\
15654\tbreakpoint - normal breakpoint\n\
15655\twatchpoint - watchpoint\n\
15656\tlongjmp - internal breakpoint used to step through longjmp()\n\
15657\tlongjmp resume - internal breakpoint at the target of longjmp()\n\
15658\tuntil - internal breakpoint used by the \"until\" command\n\
1a966eab
AC
15659\tfinish - internal breakpoint used by the \"finish\" command\n\
15660The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
c906108c
SS
15661the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15662breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1a966eab
AC
15663address and file/line number respectively.\n\
15664\n\
15665Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
15666are set to the address of the last breakpoint listed unless the command\n\
15667is prefixed with \"server \".\n\n\
c906108c 15668Convenience variable \"$bpnum\" contains the number of the last\n\
1a966eab 15669breakpoint set."),
c906108c
SS
15670 &maintenanceinfolist);
15671
3b6acaee 15672 add_basic_prefix_cmd ("catch", class_breakpoint, _("\
44feb3ce 15673Set catchpoints to catch events."),
3b6acaee
TT
15674 &catch_cmdlist, "catch ",
15675 0/*allow-unknown*/, &cmdlist);
44feb3ce 15676
3b6acaee 15677 add_basic_prefix_cmd ("tcatch", class_breakpoint, _("\
44feb3ce 15678Set temporary catchpoints to catch events."),
3b6acaee
TT
15679 &tcatch_cmdlist, "tcatch ",
15680 0/*allow-unknown*/, &cmdlist);
44feb3ce 15681
44feb3ce
TT
15682 add_catch_command ("fork", _("Catch calls to fork."),
15683 catch_fork_command_1,
a96d9b2e 15684 NULL,
44feb3ce
TT
15685 (void *) (uintptr_t) catch_fork_permanent,
15686 (void *) (uintptr_t) catch_fork_temporary);
15687 add_catch_command ("vfork", _("Catch calls to vfork."),
15688 catch_fork_command_1,
a96d9b2e 15689 NULL,
44feb3ce
TT
15690 (void *) (uintptr_t) catch_vfork_permanent,
15691 (void *) (uintptr_t) catch_vfork_temporary);
15692 add_catch_command ("exec", _("Catch calls to exec."),
15693 catch_exec_command_1,
a96d9b2e
SDJ
15694 NULL,
15695 CATCH_PERMANENT,
15696 CATCH_TEMPORARY);
edcc5120
TT
15697 add_catch_command ("load", _("Catch loads of shared libraries.\n\
15698Usage: catch load [REGEX]\n\
15699If REGEX is given, only stop for libraries matching the regular expression."),
15700 catch_load_command_1,
15701 NULL,
15702 CATCH_PERMANENT,
15703 CATCH_TEMPORARY);
15704 add_catch_command ("unload", _("Catch unloads of shared libraries.\n\
15705Usage: catch unload [REGEX]\n\
15706If REGEX is given, only stop for libraries matching the regular expression."),
15707 catch_unload_command_1,
15708 NULL,
15709 CATCH_PERMANENT,
15710 CATCH_TEMPORARY);
c5aa993b 15711
1bedd215
AC
15712 c = add_com ("watch", class_breakpoint, watch_command, _("\
15713Set a watchpoint for an expression.\n\
06a64a0b 15714Usage: watch [-l|-location] EXPRESSION\n\
c906108c 15715A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
15716an expression changes.\n\
15717If -l or -location is given, this evaluates EXPRESSION and watches\n\
15718the memory to which it refers."));
65d12d83 15719 set_cmd_completer (c, expression_completer);
c906108c 15720
1bedd215
AC
15721 c = add_com ("rwatch", class_breakpoint, rwatch_command, _("\
15722Set a read watchpoint for an expression.\n\
06a64a0b 15723Usage: rwatch [-l|-location] EXPRESSION\n\
c906108c 15724A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
15725an expression is read.\n\
15726If -l or -location is given, this evaluates EXPRESSION and watches\n\
15727the memory to which it refers."));
65d12d83 15728 set_cmd_completer (c, expression_completer);
c906108c 15729
1bedd215
AC
15730 c = add_com ("awatch", class_breakpoint, awatch_command, _("\
15731Set a watchpoint for an expression.\n\
06a64a0b 15732Usage: awatch [-l|-location] EXPRESSION\n\
c906108c 15733A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
15734an expression is either read or written.\n\
15735If -l or -location is given, this evaluates EXPRESSION and watches\n\
15736the memory to which it refers."));
65d12d83 15737 set_cmd_completer (c, expression_completer);
c906108c 15738
11db9430 15739 add_info ("watchpoints", info_watchpoints_command, _("\
e5a67952 15740Status of specified watchpoints (all watchpoints if no argument)."));
c906108c 15741
920d2a44
AC
15742 /* XXX: cagney/2005-02-23: This should be a boolean, and should
15743 respond to changes - contrary to the description. */
85c07804
AC
15744 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
15745 &can_use_hw_watchpoints, _("\
15746Set debugger's willingness to use watchpoint hardware."), _("\
15747Show debugger's willingness to use watchpoint hardware."), _("\
c906108c
SS
15748If zero, gdb will not use hardware for new watchpoints, even if\n\
15749such is available. (However, any hardware watchpoints that were\n\
15750created before setting this to nonzero, will continue to use watchpoint\n\
85c07804
AC
15751hardware.)"),
15752 NULL,
920d2a44 15753 show_can_use_hw_watchpoints,
85c07804 15754 &setlist, &showlist);
c906108c
SS
15755
15756 can_use_hw_watchpoints = 1;
fa8d40ab 15757
1042e4c0
SS
15758 /* Tracepoint manipulation commands. */
15759
15760 c = add_com ("trace", class_breakpoint, trace_command, _("\
629500fa 15761Set a tracepoint at specified location.\n\
1042e4c0
SS
15762\n"
15763BREAK_ARGS_HELP ("trace") "\n\
15764Do \"help tracepoints\" for info on other tracepoint commands."));
15765 set_cmd_completer (c, location_completer);
15766
57b4f16e
PW
15767 add_com_alias ("tp", "trace", class_breakpoint, 0);
15768 add_com_alias ("tr", "trace", class_breakpoint, 1);
15769 add_com_alias ("tra", "trace", class_breakpoint, 1);
15770 add_com_alias ("trac", "trace", class_breakpoint, 1);
1042e4c0 15771
7a697b8d 15772 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
629500fa 15773Set a fast tracepoint at specified location.\n\
7a697b8d
SS
15774\n"
15775BREAK_ARGS_HELP ("ftrace") "\n\
15776Do \"help tracepoints\" for info on other tracepoint commands."));
15777 set_cmd_completer (c, location_completer);
15778
0fb4aa4b 15779 c = add_com ("strace", class_breakpoint, strace_command, _("\
629500fa 15780Set a static tracepoint at location or marker.\n\
0fb4aa4b
PA
15781\n\
15782strace [LOCATION] [if CONDITION]\n\
629500fa
KS
15783LOCATION may be a linespec, explicit, or address location (described below) \n\
15784or -m MARKER_ID.\n\n\
15785If a marker id is specified, probe the marker with that name. With\n\
15786no LOCATION, uses current execution address of the selected stack frame.\n\
0fb4aa4b
PA
15787Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
15788This collects arbitrary user data passed in the probe point call to the\n\
15789tracing library. You can inspect it when analyzing the trace buffer,\n\
15790by printing the $_sdata variable like any other convenience variable.\n\
15791\n\
15792CONDITION is a boolean expression.\n\
89549d7f 15793\n" LOCATION_HELP_STRING "\n\n\
d41c0fc8
PA
15794Multiple tracepoints at one place are permitted, and useful if their\n\
15795conditions are different.\n\
0fb4aa4b
PA
15796\n\
15797Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
15798Do \"help tracepoints\" for info on other tracepoint commands."));
15799 set_cmd_completer (c, location_completer);
15800
11db9430 15801 add_info ("tracepoints", info_tracepoints_command, _("\
e5a67952 15802Status of specified tracepoints (all tracepoints if no argument).\n\
1042e4c0
SS
15803Convenience variable \"$tpnum\" contains the number of the\n\
15804last tracepoint set."));
15805
15806 add_info_alias ("tp", "tracepoints", 1);
15807
15808 add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
15809Delete specified tracepoints.\n\
15810Arguments are tracepoint numbers, separated by spaces.\n\
15811No argument means delete all tracepoints."),
15812 &deletelist);
7e20dfcd 15813 add_alias_cmd ("tr", "tracepoints", class_trace, 1, &deletelist);
1042e4c0
SS
15814
15815 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
15816Disable specified tracepoints.\n\
15817Arguments are tracepoint numbers, separated by spaces.\n\
15818No argument means disable all tracepoints."),
15819 &disablelist);
15820 deprecate_cmd (c, "disable");
15821
15822 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
15823Enable specified tracepoints.\n\
15824Arguments are tracepoint numbers, separated by spaces.\n\
15825No argument means enable all tracepoints."),
15826 &enablelist);
15827 deprecate_cmd (c, "enable");
15828
15829 add_com ("passcount", class_trace, trace_pass_command, _("\
15830Set the passcount for a tracepoint.\n\
15831The trace will end when the tracepoint has been passed 'count' times.\n\
15832Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
15833if TPNUM is omitted, passcount refers to the last tracepoint defined."));
15834
0743fc83
TT
15835 add_basic_prefix_cmd ("save", class_breakpoint,
15836 _("Save breakpoint definitions as a script."),
15837 &save_cmdlist, "save ",
15838 0/*allow-unknown*/, &cmdlist);
6149aea9
PA
15839
15840 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
15841Save current breakpoint definitions as a script.\n\
cce7e648 15842This includes all types of breakpoints (breakpoints, watchpoints,\n\
6149aea9
PA
15843catchpoints, tracepoints). Use the 'source' command in another debug\n\
15844session to restore them."),
15845 &save_cmdlist);
15846 set_cmd_completer (c, filename_completer);
15847
15848 c = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
1042e4c0 15849Save current tracepoint definitions as a script.\n\
6149aea9
PA
15850Use the 'source' command in another debug session to restore them."),
15851 &save_cmdlist);
1042e4c0
SS
15852 set_cmd_completer (c, filename_completer);
15853
6149aea9
PA
15854 c = add_com_alias ("save-tracepoints", "save tracepoints", class_trace, 0);
15855 deprecate_cmd (c, "save tracepoints");
15856
3b6acaee 15857 add_basic_prefix_cmd ("breakpoint", class_maintenance, _("\
590042fc 15858Breakpoint specific settings.\n\
fa8d40ab 15859Configure various breakpoint-specific variables such as\n\
590042fc 15860pending breakpoint behavior."),
3b6acaee
TT
15861 &breakpoint_set_cmdlist, "set breakpoint ",
15862 0/*allow-unknown*/, &setlist);
15863 add_show_prefix_cmd ("breakpoint", class_maintenance, _("\
590042fc 15864Breakpoint specific settings.\n\
fa8d40ab 15865Configure various breakpoint-specific variables such as\n\
590042fc 15866pending breakpoint behavior."),
3b6acaee
TT
15867 &breakpoint_show_cmdlist, "show breakpoint ",
15868 0/*allow-unknown*/, &showlist);
fa8d40ab 15869
7915a72c
AC
15870 add_setshow_auto_boolean_cmd ("pending", no_class,
15871 &pending_break_support, _("\
15872Set debugger's behavior regarding pending breakpoints."), _("\
15873Show debugger's behavior regarding pending breakpoints."), _("\
6e1d7d6c
AC
15874If on, an unrecognized breakpoint location will cause gdb to create a\n\
15875pending breakpoint. If off, an unrecognized breakpoint location results in\n\
15876an error. If auto, an unrecognized breakpoint location results in a\n\
7915a72c 15877user-query to see if a pending breakpoint should be created."),
2c5b56ce 15878 NULL,
920d2a44 15879 show_pending_break_support,
6e1d7d6c
AC
15880 &breakpoint_set_cmdlist,
15881 &breakpoint_show_cmdlist);
fa8d40ab
JJ
15882
15883 pending_break_support = AUTO_BOOLEAN_AUTO;
765dc015
VP
15884
15885 add_setshow_boolean_cmd ("auto-hw", no_class,
15886 &automatic_hardware_breakpoints, _("\
15887Set automatic usage of hardware breakpoints."), _("\
15888Show automatic usage of hardware breakpoints."), _("\
15889If set, the debugger will automatically use hardware breakpoints for\n\
15890breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
15891a warning will be emitted for such breakpoints."),
15892 NULL,
15893 show_automatic_hardware_breakpoints,
15894 &breakpoint_set_cmdlist,
15895 &breakpoint_show_cmdlist);
74960c60 15896
a25a5a45
PA
15897 add_setshow_boolean_cmd ("always-inserted", class_support,
15898 &always_inserted_mode, _("\
74960c60
VP
15899Set mode for inserting breakpoints."), _("\
15900Show mode for inserting breakpoints."), _("\
a25a5a45
PA
15901When this mode is on, breakpoints are inserted immediately as soon as\n\
15902they're created, kept inserted even when execution stops, and removed\n\
15903only when the user deletes them. When this mode is off (the default),\n\
15904breakpoints are inserted only when execution continues, and removed\n\
15905when execution stops."),
72d0e2c5
YQ
15906 NULL,
15907 &show_always_inserted_mode,
15908 &breakpoint_set_cmdlist,
15909 &breakpoint_show_cmdlist);
f1310107 15910
b775012e
LM
15911 add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
15912 condition_evaluation_enums,
15913 &condition_evaluation_mode_1, _("\
15914Set mode of breakpoint condition evaluation."), _("\
15915Show mode of breakpoint condition evaluation."), _("\
d1cda5d9 15916When this is set to \"host\", breakpoint conditions will be\n\
b775012e
LM
15917evaluated on the host's side by GDB. When it is set to \"target\",\n\
15918breakpoint conditions will be downloaded to the target (if the target\n\
15919supports such feature) and conditions will be evaluated on the target's side.\n\
15920If this is set to \"auto\" (default), this will be automatically set to\n\
15921\"target\" if it supports condition evaluation, otherwise it will\n\
15922be set to \"gdb\""),
15923 &set_condition_evaluation_mode,
15924 &show_condition_evaluation_mode,
15925 &breakpoint_set_cmdlist,
15926 &breakpoint_show_cmdlist);
15927
f1310107
TJB
15928 add_com ("break-range", class_breakpoint, break_range_command, _("\
15929Set a breakpoint for an address range.\n\
15930break-range START-LOCATION, END-LOCATION\n\
15931where START-LOCATION and END-LOCATION can be one of the following:\n\
15932 LINENUM, for that line in the current file,\n\
15933 FILE:LINENUM, for that line in that file,\n\
15934 +OFFSET, for that number of lines after the current line\n\
15935 or the start of the range\n\
15936 FUNCTION, for the first line in that function,\n\
15937 FILE:FUNCTION, to distinguish among like-named static functions.\n\
15938 *ADDRESS, for the instruction at that address.\n\
15939\n\
15940The breakpoint will stop execution of the inferior whenever it executes\n\
15941an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
15942range (including START-LOCATION and END-LOCATION)."));
15943
e7e0cddf 15944 c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
629500fa 15945Set a dynamic printf at specified location.\n\
e7e0cddf 15946dprintf location,format string,arg1,arg2,...\n\
629500fa
KS
15947location may be a linespec, explicit, or address location.\n"
15948"\n" LOCATION_HELP_STRING));
e7e0cddf
SS
15949 set_cmd_completer (c, location_completer);
15950
15951 add_setshow_enum_cmd ("dprintf-style", class_support,
15952 dprintf_style_enums, &dprintf_style, _("\
15953Set the style of usage for dynamic printf."), _("\
15954Show the style of usage for dynamic printf."), _("\
15955This setting chooses how GDB will do a dynamic printf.\n\
15956If the value is \"gdb\", then the printing is done by GDB to its own\n\
15957console, as with the \"printf\" command.\n\
15958If the value is \"call\", the print is done by calling a function in your\n\
15959program; by default printf(), but you can choose a different function or\n\
15960output stream by setting dprintf-function and dprintf-channel."),
15961 update_dprintf_commands, NULL,
15962 &setlist, &showlist);
15963
15964 dprintf_function = xstrdup ("printf");
15965 add_setshow_string_cmd ("dprintf-function", class_support,
15966 &dprintf_function, _("\
590042fc
PW
15967Set the function to use for dynamic printf."), _("\
15968Show the function to use for dynamic printf."), NULL,
e7e0cddf
SS
15969 update_dprintf_commands, NULL,
15970 &setlist, &showlist);
15971
15972 dprintf_channel = xstrdup ("");
15973 add_setshow_string_cmd ("dprintf-channel", class_support,
15974 &dprintf_channel, _("\
590042fc
PW
15975Set the channel to use for dynamic printf."), _("\
15976Show the channel to use for dynamic printf."), NULL,
e7e0cddf
SS
15977 update_dprintf_commands, NULL,
15978 &setlist, &showlist);
15979
d3ce09f5
SS
15980 add_setshow_boolean_cmd ("disconnected-dprintf", no_class,
15981 &disconnected_dprintf, _("\
15982Set whether dprintf continues after GDB disconnects."), _("\
15983Show whether dprintf continues after GDB disconnects."), _("\
15984Use this to let dprintf commands continue to hit and produce output\n\
15985even if GDB disconnects or detaches from the target."),
15986 NULL,
15987 NULL,
15988 &setlist, &showlist);
15989
15990 add_com ("agent-printf", class_vars, agent_printf_command, _("\
590042fc
PW
15991Target agent only formatted printing, like the C \"printf\" function.\n\
15992Usage: agent-printf \"format string\", ARG1, ARG2, ARG3, ..., ARGN\n\
15993This supports most C printf format specifications, like %s, %d, etc.\n\
15994This is useful for formatted output in user-defined commands."));
d3ce09f5 15995
491144b5 15996 automatic_hardware_breakpoints = true;
f3b1572e 15997
76727919
TT
15998 gdb::observers::about_to_proceed.attach (breakpoint_about_to_proceed);
15999 gdb::observers::thread_exit.attach (remove_threaded_breakpoints);
c906108c 16000}