Merge branch 'master' into merge-job
[deliverable/binutils-gdb.git] / gdb / breakpoint.c
CommitLineData
c906108c 1/* Everything about breakpoints, for GDB.
8926118c 2
b811d2c2 3 Copyright (C) 1986-2020 Free Software Foundation, Inc.
4e5106e6 4 Copyright (C) 2019-2020 Advanced Micro Devices, Inc. All rights reserved.
c906108c 5
c5aa993b 6 This file is part of GDB.
c906108c 7
c5aa993b
JM
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
a9762ec7 10 the Free Software Foundation; either version 3 of the License, or
c5aa993b 11 (at your option) any later version.
c906108c 12
c5aa993b
JM
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
c906108c 17
c5aa993b 18 You should have received a copy of the GNU General Public License
a9762ec7 19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c
SS
20
21#include "defs.h"
d55e5aa6 22#include "arch-utils.h"
4de283e4
TT
23#include <ctype.h>
24#include "hashtab.h"
25#include "symtab.h"
26#include "frame.h"
c906108c 27#include "breakpoint.h"
4de283e4
TT
28#include "tracepoint.h"
29#include "gdbtypes.h"
c906108c 30#include "expression.h"
d55e5aa6 31#include "gdbcore.h"
4de283e4
TT
32#include "gdbcmd.h"
33#include "value.h"
34#include "command.h"
c906108c 35#include "inferior.h"
45741a9c 36#include "infrun.h"
4de283e4
TT
37#include "gdbthread.h"
38#include "target.h"
c906108c 39#include "language.h"
4de283e4
TT
40#include "gdb-demangle.h"
41#include "filenames.h"
42#include "annotate.h"
43#include "symfile.h"
d55e5aa6 44#include "objfiles.h"
4de283e4
TT
45#include "source.h"
46#include "linespec.h"
47#include "completer.h"
48#include "ui-out.h"
49#include "cli/cli-script.h"
50#include "block.h"
51#include "solib.h"
52#include "solist.h"
d55e5aa6 53#include "observable.h"
4de283e4
TT
54#include "memattr.h"
55#include "ada-lang.h"
56#include "top.h"
57#include "valprint.h"
58#include "jit.h"
65d79d4b 59#include "parser-defs.h"
4de283e4 60#include "gdb_regex.h"
55aa24fb 61#include "probe.h"
4de283e4 62#include "cli/cli-utils.h"
d55e5aa6 63#include "stack.h"
4de283e4
TT
64#include "ax-gdb.h"
65#include "dummy-frame.h"
66#include "interps.h"
268a13a5 67#include "gdbsupport/format.h"
cfc31633 68#include "thread-fsm.h"
5d5658a1 69#include "tid-parse.h"
4de283e4 70#include "cli/cli-style.h"
d3ce09f5 71
1042e4c0 72/* readline include files */
073bbbb0 73#include "readline/tilde.h"
1042e4c0
SS
74
75/* readline defines this. */
76#undef savestring
77
034dad6f 78#include "mi/mi-common.h"
6dddc817 79#include "extension.h"
325fac50 80#include <algorithm>
5ed8105e 81#include "progspace-and-thread.h"
268a13a5
TT
82#include "gdbsupport/array-view.h"
83#include "gdbsupport/gdb_optional.h"
104c1213 84
4a64f543 85/* Prototypes for local functions. */
c906108c 86
896b6bda 87static void map_breakpoint_numbers (const char *,
48649e1b 88 gdb::function_view<void (breakpoint *)>);
c906108c 89
348d480f
PA
90static void breakpoint_re_set_default (struct breakpoint *);
91
f00aae0f
KS
92static void
93 create_sals_from_location_default (const struct event_location *location,
94 struct linespec_result *canonical,
95 enum bptype type_wanted);
983af33b
SDJ
96
97static void create_breakpoints_sal_default (struct gdbarch *,
98 struct linespec_result *,
e1e01040
PA
99 gdb::unique_xmalloc_ptr<char>,
100 gdb::unique_xmalloc_ptr<char>,
101 enum bptype,
983af33b
SDJ
102 enum bpdisp, int, int,
103 int,
104 const struct breakpoint_ops *,
44f238bb 105 int, int, int, unsigned);
983af33b 106
6c5b2ebe
PA
107static std::vector<symtab_and_line> decode_location_default
108 (struct breakpoint *b, const struct event_location *location,
109 struct program_space *search_pspace);
983af33b 110
a6535de1
TT
111static int can_use_hardware_watchpoint
112 (const std::vector<value_ref_ptr> &vals);
c906108c 113
a14ed312 114static void mention (struct breakpoint *);
c906108c 115
348d480f
PA
116static struct breakpoint *set_raw_breakpoint_without_location (struct gdbarch *,
117 enum bptype,
c0a91b2b 118 const struct breakpoint_ops *);
3742cc8b
YQ
119static struct bp_location *add_location_to_breakpoint (struct breakpoint *,
120 const struct symtab_and_line *);
121
4a64f543
MS
122/* This function is used in gdbtk sources and thus can not be made
123 static. */
63c252f8 124struct breakpoint *set_raw_breakpoint (struct gdbarch *gdbarch,
348d480f 125 struct symtab_and_line,
c0a91b2b
TT
126 enum bptype,
127 const struct breakpoint_ops *);
c906108c 128
06edf0c0
PA
129static struct breakpoint *
130 momentary_breakpoint_from_master (struct breakpoint *orig,
131 enum bptype type,
a1aa2221
LM
132 const struct breakpoint_ops *ops,
133 int loc_enabled);
06edf0c0 134
76897487
KB
135static void breakpoint_adjustment_warning (CORE_ADDR, CORE_ADDR, int, int);
136
a6d9a66e
UW
137static CORE_ADDR adjust_breakpoint_address (struct gdbarch *gdbarch,
138 CORE_ADDR bpaddr,
88f7da05 139 enum bptype bptype);
76897487 140
6c95b8df
PA
141static void describe_other_breakpoints (struct gdbarch *,
142 struct program_space *, CORE_ADDR,
5af949e3 143 struct obj_section *, int);
c906108c 144
85d721b8
PA
145static int watchpoint_locations_match (struct bp_location *loc1,
146 struct bp_location *loc2);
147
f1310107 148static int breakpoint_location_address_match (struct bp_location *bl,
accd0bcd 149 const struct address_space *aspace,
f1310107
TJB
150 CORE_ADDR addr);
151
d35ae833 152static int breakpoint_location_address_range_overlap (struct bp_location *,
accd0bcd 153 const address_space *,
d35ae833
PA
154 CORE_ADDR, int);
155
834c0d03 156static int remove_breakpoint (struct bp_location *);
b2b6a7da 157static int remove_breakpoint_1 (struct bp_location *, enum remove_bp_reason);
c906108c 158
e514a9d6 159static enum print_stop_action print_bp_stop_message (bpstat bs);
c906108c 160
a14ed312 161static int hw_breakpoint_used_count (void);
c906108c 162
a1398e0c
PA
163static int hw_watchpoint_use_count (struct breakpoint *);
164
165static int hw_watchpoint_used_count_others (struct breakpoint *except,
166 enum bptype type,
167 int *other_type_used);
c906108c 168
816338b5
SS
169static void enable_breakpoint_disp (struct breakpoint *, enum bpdisp,
170 int count);
c906108c 171
fe3f5fa8 172static void free_bp_location (struct bp_location *loc);
f431efe5
PA
173static void incref_bp_location (struct bp_location *loc);
174static void decref_bp_location (struct bp_location **loc);
fe3f5fa8 175
39d61571 176static struct bp_location *allocate_bp_location (struct breakpoint *bpt);
a5606eee 177
44702360
PA
178/* update_global_location_list's modes of operation wrt to whether to
179 insert locations now. */
180enum ugll_insert_mode
181{
182 /* Don't insert any breakpoint locations into the inferior, only
183 remove already-inserted locations that no longer should be
184 inserted. Functions that delete a breakpoint or breakpoints
185 should specify this mode, so that deleting a breakpoint doesn't
186 have the side effect of inserting the locations of other
187 breakpoints that are marked not-inserted, but should_be_inserted
188 returns true on them.
189
190 This behavior is useful is situations close to tear-down -- e.g.,
191 after an exec, while the target still has execution, but
192 breakpoint shadows of the previous executable image should *NOT*
193 be restored to the new image; or before detaching, where the
194 target still has execution and wants to delete breakpoints from
195 GDB's lists, and all breakpoints had already been removed from
196 the inferior. */
197 UGLL_DONT_INSERT,
198
a25a5a45
PA
199 /* May insert breakpoints iff breakpoints_should_be_inserted_now
200 claims breakpoints should be inserted now. */
04086b45
PA
201 UGLL_MAY_INSERT,
202
a25a5a45
PA
203 /* Insert locations now, irrespective of
204 breakpoints_should_be_inserted_now. E.g., say all threads are
205 stopped right now, and the user did "continue". We need to
206 insert breakpoints _before_ resuming the target, but
207 UGLL_MAY_INSERT wouldn't insert them, because
208 breakpoints_should_be_inserted_now returns false at that point,
209 as no thread is running yet. */
04086b45 210 UGLL_INSERT
44702360
PA
211};
212
213static void update_global_location_list (enum ugll_insert_mode);
a5606eee 214
44702360 215static void update_global_location_list_nothrow (enum ugll_insert_mode);
74960c60 216
74960c60 217static void insert_breakpoint_locations (void);
a5606eee 218
0b39b52e 219static void trace_pass_command (const char *, int);
1042e4c0 220
558a9d82
YQ
221static void set_tracepoint_count (int num);
222
f2478a7e 223static bool is_masked_watchpoint (const struct breakpoint *b);
9c06b0b4 224
b775012e
LM
225static struct bp_location **get_first_locp_gte_addr (CORE_ADDR address);
226
983af33b
SDJ
227/* Return 1 if B refers to a static tracepoint set by marker ("-m"), zero
228 otherwise. */
229
230static int strace_marker_p (struct breakpoint *b);
0fb4aa4b 231
2060206e
PA
232/* The breakpoint_ops structure to be inherited by all breakpoint_ops
233 that are implemented on top of software or hardware breakpoints
234 (user breakpoints, internal and momentary breakpoints, etc.). */
235static struct breakpoint_ops bkpt_base_breakpoint_ops;
236
237/* Internal breakpoints class type. */
06edf0c0 238static struct breakpoint_ops internal_breakpoint_ops;
2060206e
PA
239
240/* Momentary breakpoints class type. */
06edf0c0
PA
241static struct breakpoint_ops momentary_breakpoint_ops;
242
2060206e
PA
243/* The breakpoint_ops structure to be used in regular user created
244 breakpoints. */
245struct breakpoint_ops bkpt_breakpoint_ops;
246
55aa24fb
SDJ
247/* Breakpoints set on probes. */
248static struct breakpoint_ops bkpt_probe_breakpoint_ops;
249
bac7c5cf
GB
250/* Tracepoints set on probes. */
251static struct breakpoint_ops tracepoint_probe_breakpoint_ops;
252
e7e0cddf 253/* Dynamic printf class type. */
c5867ab6 254struct breakpoint_ops dprintf_breakpoint_ops;
e7e0cddf 255
d3ce09f5
SS
256/* The style in which to perform a dynamic printf. This is a user
257 option because different output options have different tradeoffs;
258 if GDB does the printing, there is better error handling if there
259 is a problem with any of the arguments, but using an inferior
260 function lets you have special-purpose printers and sending of
261 output to the same place as compiled-in print functions. */
262
263static const char dprintf_style_gdb[] = "gdb";
264static const char dprintf_style_call[] = "call";
265static const char dprintf_style_agent[] = "agent";
266static const char *const dprintf_style_enums[] = {
267 dprintf_style_gdb,
268 dprintf_style_call,
269 dprintf_style_agent,
270 NULL
271};
272static const char *dprintf_style = dprintf_style_gdb;
273
274/* The function to use for dynamic printf if the preferred style is to
275 call into the inferior. The value is simply a string that is
276 copied into the command, so it can be anything that GDB can
277 evaluate to a callable address, not necessarily a function name. */
278
bde6261a 279static char *dprintf_function;
d3ce09f5
SS
280
281/* The channel to use for dynamic printf if the preferred style is to
282 call into the inferior; if a nonempty string, it will be passed to
283 the call as the first argument, with the format string as the
284 second. As with the dprintf function, this can be anything that
285 GDB knows how to evaluate, so in addition to common choices like
286 "stderr", this could be an app-specific expression like
287 "mystreams[curlogger]". */
288
bde6261a 289static char *dprintf_channel;
d3ce09f5
SS
290
291/* True if dprintf commands should continue to operate even if GDB
292 has disconnected. */
491144b5 293static bool disconnected_dprintf = true;
d3ce09f5 294
5cea2a26
PA
295struct command_line *
296breakpoint_commands (struct breakpoint *b)
297{
d1b0a7bf 298 return b->commands ? b->commands.get () : NULL;
5cea2a26 299}
3daf8fe5 300
f3b1572e
PA
301/* Flag indicating that a command has proceeded the inferior past the
302 current breakpoint. */
303
491144b5 304static bool breakpoint_proceeded;
f3b1572e 305
956a9fb9 306const char *
2cec12e5
AR
307bpdisp_text (enum bpdisp disp)
308{
4a64f543
MS
309 /* NOTE: the following values are a part of MI protocol and
310 represent values of 'disp' field returned when inferior stops at
311 a breakpoint. */
bc043ef3 312 static const char * const bpdisps[] = {"del", "dstp", "dis", "keep"};
cc59ec59 313
2cec12e5
AR
314 return bpdisps[(int) disp];
315}
c906108c 316
4a64f543 317/* Prototypes for exported functions. */
c906108c 318/* If FALSE, gdb will not use hardware support for watchpoints, even
4a64f543 319 if such is available. */
c906108c
SS
320static int can_use_hw_watchpoints;
321
920d2a44
AC
322static void
323show_can_use_hw_watchpoints (struct ui_file *file, int from_tty,
324 struct cmd_list_element *c,
325 const char *value)
326{
3e43a32a
MS
327 fprintf_filtered (file,
328 _("Debugger's willingness to use "
329 "watchpoint hardware is %s.\n"),
920d2a44
AC
330 value);
331}
332
fa8d40ab
JJ
333/* If AUTO_BOOLEAN_FALSE, gdb will not attempt to create pending breakpoints.
334 If AUTO_BOOLEAN_TRUE, gdb will automatically create pending breakpoints
4a64f543 335 for unrecognized breakpoint locations.
fa8d40ab
JJ
336 If AUTO_BOOLEAN_AUTO, gdb will query when breakpoints are unrecognized. */
337static enum auto_boolean pending_break_support;
920d2a44
AC
338static void
339show_pending_break_support (struct ui_file *file, int from_tty,
340 struct cmd_list_element *c,
341 const char *value)
342{
3e43a32a
MS
343 fprintf_filtered (file,
344 _("Debugger's behavior regarding "
345 "pending breakpoints is %s.\n"),
920d2a44
AC
346 value);
347}
fa8d40ab 348
491144b5 349/* If true, gdb will automatically use hardware breakpoints for breakpoints
4a64f543 350 set with "break" but falling in read-only memory.
491144b5 351 If false, gdb will warn about such breakpoints, but won't automatically
765dc015 352 use hardware breakpoints. */
491144b5 353static bool automatic_hardware_breakpoints;
765dc015
VP
354static void
355show_automatic_hardware_breakpoints (struct ui_file *file, int from_tty,
356 struct cmd_list_element *c,
357 const char *value)
358{
3e43a32a
MS
359 fprintf_filtered (file,
360 _("Automatic usage of hardware breakpoints is %s.\n"),
765dc015
VP
361 value);
362}
363
a25a5a45
PA
364/* If on, GDB keeps breakpoints inserted even if the inferior is
365 stopped, and immediately inserts any new breakpoints as soon as
366 they're created. If off (default), GDB keeps breakpoints off of
367 the target as long as possible. That is, it delays inserting
368 breakpoints until the next resume, and removes them again when the
369 target fully stops. This is a bit safer in case GDB crashes while
370 processing user input. */
5922befa
LM
371/* FIXME: this is a temporary workaround to make sure waves created while
372 all known threads are stopped, and the gdb prompt is presented, do not
373 execute past the enabled breakpoints. */
374static bool always_inserted_mode = true;
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 }
396 else if (target_has_execution)
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
b57bacec
PA
405 if (threads_are_executing ())
406 return 1;
372316f1
PA
407
408 /* Don't remove breakpoints yet if, even though all threads are
409 stopped, we still have events to process. */
08036331 410 for (thread_info *tp : all_non_exited_threads ())
372316f1
PA
411 if (tp->resumed
412 && tp->suspend.waitstatus_pending_p)
413 return 1;
a25a5a45
PA
414 }
415 return 0;
33e5cbd6 416}
765dc015 417
b775012e
LM
418static const char condition_evaluation_both[] = "host or target";
419
420/* Modes for breakpoint condition evaluation. */
421static const char condition_evaluation_auto[] = "auto";
422static const char condition_evaluation_host[] = "host";
423static const char condition_evaluation_target[] = "target";
424static const char *const condition_evaluation_enums[] = {
425 condition_evaluation_auto,
426 condition_evaluation_host,
427 condition_evaluation_target,
428 NULL
429};
430
431/* Global that holds the current mode for breakpoint condition evaluation. */
432static const char *condition_evaluation_mode_1 = condition_evaluation_auto;
433
434/* Global that we use to display information to the user (gets its value from
435 condition_evaluation_mode_1. */
436static const char *condition_evaluation_mode = condition_evaluation_auto;
437
438/* Translate a condition evaluation mode MODE into either "host"
439 or "target". This is used mostly to translate from "auto" to the
440 real setting that is being used. It returns the translated
441 evaluation mode. */
442
443static const char *
444translate_condition_evaluation_mode (const char *mode)
445{
446 if (mode == condition_evaluation_auto)
447 {
448 if (target_supports_evaluation_of_breakpoint_conditions ())
449 return condition_evaluation_target;
450 else
451 return condition_evaluation_host;
452 }
453 else
454 return mode;
455}
456
457/* Discovers what condition_evaluation_auto translates to. */
458
459static const char *
460breakpoint_condition_evaluation_mode (void)
461{
462 return translate_condition_evaluation_mode (condition_evaluation_mode);
463}
464
465/* Return true if GDB should evaluate breakpoint conditions or false
466 otherwise. */
467
468static int
469gdb_evaluates_breakpoint_condition_p (void)
470{
471 const char *mode = breakpoint_condition_evaluation_mode ();
472
473 return (mode == condition_evaluation_host);
474}
475
c906108c
SS
476/* Are we executing breakpoint commands? */
477static int executing_breakpoint_commands;
478
c02f5703
MS
479/* Are overlay event breakpoints enabled? */
480static int overlay_events_enabled;
481
e09342b5 482/* See description in breakpoint.h. */
491144b5 483bool target_exact_watchpoints = false;
e09342b5 484
c906108c 485/* Walk the following statement or block through all breakpoints.
e5dd4106 486 ALL_BREAKPOINTS_SAFE does so even if the statement deletes the
4a64f543 487 current breakpoint. */
c906108c 488
5c44784c 489#define ALL_BREAKPOINTS(B) for (B = breakpoint_chain; B; B = B->next)
c906108c 490
5c44784c
JM
491#define ALL_BREAKPOINTS_SAFE(B,TMP) \
492 for (B = breakpoint_chain; \
493 B ? (TMP=B->next, 1): 0; \
494 B = TMP)
c906108c 495
4a64f543
MS
496/* Similar iterator for the low-level breakpoints. SAFE variant is
497 not provided so update_global_location_list must not be called
498 while executing the block of ALL_BP_LOCATIONS. */
7cc221ef 499
876fa593 500#define ALL_BP_LOCATIONS(B,BP_TMP) \
f5336ca5
PA
501 for (BP_TMP = bp_locations; \
502 BP_TMP < bp_locations + bp_locations_count && (B = *BP_TMP);\
876fa593 503 BP_TMP++)
7cc221ef 504
b775012e
LM
505/* Iterates through locations with address ADDRESS for the currently selected
506 program space. BP_LOCP_TMP points to each object. BP_LOCP_START points
507 to where the loop should start from.
508 If BP_LOCP_START is a NULL pointer, the macro automatically seeks the
509 appropriate location to start with. */
510
511#define ALL_BP_LOCATIONS_AT_ADDR(BP_LOCP_TMP, BP_LOCP_START, ADDRESS) \
512 for (BP_LOCP_START = BP_LOCP_START == NULL ? get_first_locp_gte_addr (ADDRESS) : BP_LOCP_START, \
513 BP_LOCP_TMP = BP_LOCP_START; \
514 BP_LOCP_START \
f5336ca5 515 && (BP_LOCP_TMP < bp_locations + bp_locations_count \
b775012e
LM
516 && (*BP_LOCP_TMP)->address == ADDRESS); \
517 BP_LOCP_TMP++)
518
1042e4c0
SS
519/* Iterator for tracepoints only. */
520
521#define ALL_TRACEPOINTS(B) \
522 for (B = breakpoint_chain; B; B = B->next) \
d77f58be 523 if (is_tracepoint (B))
1042e4c0 524
7cc221ef 525/* Chains of all breakpoints defined. */
c906108c 526
81e6b8eb 527static struct breakpoint *breakpoint_chain;
c906108c 528
39ef2f62 529/* Array is sorted by bp_location_is_less_than - primarily by the ADDRESS. */
876fa593 530
f5336ca5 531static struct bp_location **bp_locations;
876fa593 532
f5336ca5 533/* Number of elements of BP_LOCATIONS. */
876fa593 534
f5336ca5 535static unsigned bp_locations_count;
876fa593 536
4a64f543 537/* Maximum alignment offset between bp_target_info.PLACED_ADDRESS and
f5336ca5 538 ADDRESS for the current elements of BP_LOCATIONS which get a valid
4a64f543 539 result from bp_location_has_shadow. You can use it for roughly
f5336ca5 540 limiting the subrange of BP_LOCATIONS to scan for shadow bytes for
4a64f543 541 an address you need to read. */
876fa593 542
f5336ca5 543static CORE_ADDR bp_locations_placed_address_before_address_max;
876fa593 544
4a64f543
MS
545/* Maximum offset plus alignment between bp_target_info.PLACED_ADDRESS
546 + bp_target_info.SHADOW_LEN and ADDRESS for the current elements of
f5336ca5
PA
547 BP_LOCATIONS which get a valid result from bp_location_has_shadow.
548 You can use it for roughly limiting the subrange of BP_LOCATIONS to
4a64f543 549 scan for shadow bytes for an address you need to read. */
876fa593 550
f5336ca5 551static CORE_ADDR bp_locations_shadow_len_after_address_max;
7cc221ef 552
4a64f543 553/* The locations that no longer correspond to any breakpoint, unlinked
f5336ca5
PA
554 from the bp_locations array, but for which a hit may still be
555 reported by a target. */
1123588c 556static std::vector<bp_location *> moribund_locations;
20874c92 557
c906108c
SS
558/* Number of last breakpoint made. */
559
95a42b64
TT
560static int breakpoint_count;
561
86b17b60
PA
562/* The value of `breakpoint_count' before the last command that
563 created breakpoints. If the last (break-like) command created more
564 than one breakpoint, then the difference between BREAKPOINT_COUNT
565 and PREV_BREAKPOINT_COUNT is more than one. */
566static int prev_breakpoint_count;
c906108c 567
1042e4c0
SS
568/* Number of last tracepoint made. */
569
95a42b64 570static int tracepoint_count;
1042e4c0 571
6149aea9
PA
572static struct cmd_list_element *breakpoint_set_cmdlist;
573static struct cmd_list_element *breakpoint_show_cmdlist;
9291a0cd 574struct cmd_list_element *save_cmdlist;
6149aea9 575
badd37ce
SDJ
576/* See declaration at breakpoint.h. */
577
578struct breakpoint *
579breakpoint_find_if (int (*func) (struct breakpoint *b, void *d),
580 void *user_data)
581{
582 struct breakpoint *b = NULL;
583
584 ALL_BREAKPOINTS (b)
585 {
586 if (func (b, user_data) != 0)
587 break;
588 }
589
590 return b;
591}
592
468d015d
JJ
593/* Return whether a breakpoint is an active enabled breakpoint. */
594static int
595breakpoint_enabled (struct breakpoint *b)
596{
0d381245 597 return (b->enable_state == bp_enabled);
468d015d
JJ
598}
599
c906108c
SS
600/* Set breakpoint count to NUM. */
601
95a42b64 602static void
fba45db2 603set_breakpoint_count (int num)
c906108c 604{
86b17b60 605 prev_breakpoint_count = breakpoint_count;
c906108c 606 breakpoint_count = num;
4fa62494 607 set_internalvar_integer (lookup_internalvar ("bpnum"), num);
c906108c
SS
608}
609
86b17b60
PA
610/* Used by `start_rbreak_breakpoints' below, to record the current
611 breakpoint count before "rbreak" creates any breakpoint. */
612static int rbreak_start_breakpoint_count;
613
95a42b64
TT
614/* Called at the start an "rbreak" command to record the first
615 breakpoint made. */
86b17b60 616
c80049d3 617scoped_rbreak_breakpoints::scoped_rbreak_breakpoints ()
95a42b64 618{
86b17b60 619 rbreak_start_breakpoint_count = breakpoint_count;
95a42b64
TT
620}
621
622/* Called at the end of an "rbreak" command to record the last
623 breakpoint made. */
86b17b60 624
c80049d3 625scoped_rbreak_breakpoints::~scoped_rbreak_breakpoints ()
95a42b64 626{
86b17b60 627 prev_breakpoint_count = rbreak_start_breakpoint_count;
95a42b64
TT
628}
629
4a64f543 630/* Used in run_command to zero the hit count when a new run starts. */
c906108c
SS
631
632void
fba45db2 633clear_breakpoint_hit_counts (void)
c906108c
SS
634{
635 struct breakpoint *b;
636
637 ALL_BREAKPOINTS (b)
638 b->hit_count = 0;
639}
640
c906108c 641\f
48cb2d85
VP
642/* Return the breakpoint with the specified number, or NULL
643 if the number does not refer to an existing breakpoint. */
644
645struct breakpoint *
646get_breakpoint (int num)
647{
648 struct breakpoint *b;
649
650 ALL_BREAKPOINTS (b)
651 if (b->number == num)
652 return b;
653
654 return NULL;
655}
5c44784c 656
c906108c 657\f
adc36818 658
b775012e
LM
659/* Mark locations as "conditions have changed" in case the target supports
660 evaluating conditions on its side. */
661
662static void
663mark_breakpoint_modified (struct breakpoint *b)
664{
665 struct bp_location *loc;
666
667 /* This is only meaningful if the target is
668 evaluating conditions and if the user has
669 opted for condition evaluation on the target's
670 side. */
671 if (gdb_evaluates_breakpoint_condition_p ()
672 || !target_supports_evaluation_of_breakpoint_conditions ())
673 return;
674
675 if (!is_breakpoint (b))
676 return;
677
678 for (loc = b->loc; loc; loc = loc->next)
679 loc->condition_changed = condition_modified;
680}
681
682/* Mark location as "conditions have changed" in case the target supports
683 evaluating conditions on its side. */
684
685static void
686mark_breakpoint_location_modified (struct bp_location *loc)
687{
688 /* This is only meaningful if the target is
689 evaluating conditions and if the user has
690 opted for condition evaluation on the target's
691 side. */
692 if (gdb_evaluates_breakpoint_condition_p ()
693 || !target_supports_evaluation_of_breakpoint_conditions ())
694
695 return;
696
697 if (!is_breakpoint (loc->owner))
698 return;
699
700 loc->condition_changed = condition_modified;
701}
702
703/* Sets the condition-evaluation mode using the static global
704 condition_evaluation_mode. */
705
706static void
eb4c3f4a 707set_condition_evaluation_mode (const char *args, int from_tty,
b775012e
LM
708 struct cmd_list_element *c)
709{
b775012e
LM
710 const char *old_mode, *new_mode;
711
712 if ((condition_evaluation_mode_1 == condition_evaluation_target)
713 && !target_supports_evaluation_of_breakpoint_conditions ())
714 {
715 condition_evaluation_mode_1 = condition_evaluation_mode;
716 warning (_("Target does not support breakpoint condition evaluation.\n"
717 "Using host evaluation mode instead."));
718 return;
719 }
720
721 new_mode = translate_condition_evaluation_mode (condition_evaluation_mode_1);
722 old_mode = translate_condition_evaluation_mode (condition_evaluation_mode);
723
abf1152a
JK
724 /* Flip the switch. Flip it even if OLD_MODE == NEW_MODE as one of the
725 settings was "auto". */
726 condition_evaluation_mode = condition_evaluation_mode_1;
727
b775012e
LM
728 /* Only update the mode if the user picked a different one. */
729 if (new_mode != old_mode)
730 {
731 struct bp_location *loc, **loc_tmp;
732 /* If the user switched to a different evaluation mode, we
733 need to synch the changes with the target as follows:
734
735 "host" -> "target": Send all (valid) conditions to the target.
736 "target" -> "host": Remove all the conditions from the target.
737 */
738
b775012e
LM
739 if (new_mode == condition_evaluation_target)
740 {
741 /* Mark everything modified and synch conditions with the
742 target. */
743 ALL_BP_LOCATIONS (loc, loc_tmp)
744 mark_breakpoint_location_modified (loc);
745 }
746 else
747 {
748 /* Manually mark non-duplicate locations to synch conditions
749 with the target. We do this to remove all the conditions the
750 target knows about. */
751 ALL_BP_LOCATIONS (loc, loc_tmp)
752 if (is_breakpoint (loc->owner) && loc->inserted)
753 loc->needs_update = 1;
754 }
755
756 /* Do the update. */
44702360 757 update_global_location_list (UGLL_MAY_INSERT);
b775012e
LM
758 }
759
760 return;
761}
762
763/* Shows the current mode of breakpoint condition evaluation. Explicitly shows
764 what "auto" is translating to. */
765
766static void
767show_condition_evaluation_mode (struct ui_file *file, int from_tty,
768 struct cmd_list_element *c, const char *value)
769{
770 if (condition_evaluation_mode == condition_evaluation_auto)
771 fprintf_filtered (file,
772 _("Breakpoint condition evaluation "
773 "mode is %s (currently %s).\n"),
774 value,
775 breakpoint_condition_evaluation_mode ());
776 else
777 fprintf_filtered (file, _("Breakpoint condition evaluation mode is %s.\n"),
778 value);
779}
780
781/* A comparison function for bp_location AP and BP that is used by
782 bsearch. This comparison function only cares about addresses, unlike
39ef2f62 783 the more general bp_location_is_less_than function. */
b775012e
LM
784
785static int
f5336ca5 786bp_locations_compare_addrs (const void *ap, const void *bp)
b775012e 787{
9a3c8263
SM
788 const struct bp_location *a = *(const struct bp_location **) ap;
789 const struct bp_location *b = *(const struct bp_location **) bp;
b775012e
LM
790
791 if (a->address == b->address)
792 return 0;
793 else
794 return ((a->address > b->address) - (a->address < b->address));
795}
796
797/* Helper function to skip all bp_locations with addresses
798 less than ADDRESS. It returns the first bp_location that
799 is greater than or equal to ADDRESS. If none is found, just
800 return NULL. */
801
802static struct bp_location **
803get_first_locp_gte_addr (CORE_ADDR address)
804{
805 struct bp_location dummy_loc;
806 struct bp_location *dummy_locp = &dummy_loc;
807 struct bp_location **locp_found = NULL;
808
809 /* Initialize the dummy location's address field. */
b775012e
LM
810 dummy_loc.address = address;
811
812 /* Find a close match to the first location at ADDRESS. */
9a3c8263 813 locp_found = ((struct bp_location **)
f5336ca5 814 bsearch (&dummy_locp, bp_locations, bp_locations_count,
9a3c8263 815 sizeof (struct bp_location **),
f5336ca5 816 bp_locations_compare_addrs));
b775012e
LM
817
818 /* Nothing was found, nothing left to do. */
819 if (locp_found == NULL)
820 return NULL;
821
822 /* We may have found a location that is at ADDRESS but is not the first in the
823 location's list. Go backwards (if possible) and locate the first one. */
f5336ca5 824 while ((locp_found - 1) >= bp_locations
b775012e
LM
825 && (*(locp_found - 1))->address == address)
826 locp_found--;
827
828 return locp_found;
829}
830
adc36818 831void
7a26bd4d 832set_breakpoint_condition (struct breakpoint *b, const char *exp,
adc36818
PM
833 int from_tty)
834{
3a5c3e22
PA
835 xfree (b->cond_string);
836 b->cond_string = NULL;
adc36818 837
3a5c3e22 838 if (is_watchpoint (b))
adc36818 839 {
3a5c3e22
PA
840 struct watchpoint *w = (struct watchpoint *) b;
841
4d01a485 842 w->cond_exp.reset ();
3a5c3e22
PA
843 }
844 else
845 {
846 struct bp_location *loc;
847
848 for (loc = b->loc; loc; loc = loc->next)
849 {
4d01a485 850 loc->cond.reset ();
b775012e
LM
851
852 /* No need to free the condition agent expression
853 bytecode (if we have one). We will handle this
854 when we go through update_global_location_list. */
3a5c3e22 855 }
adc36818 856 }
adc36818
PM
857
858 if (*exp == 0)
859 {
860 if (from_tty)
861 printf_filtered (_("Breakpoint %d now unconditional.\n"), b->number);
862 }
863 else
864 {
bbc13ae3 865 const char *arg = exp;
cc59ec59 866
adc36818
PM
867 /* I don't know if it matters whether this is the string the user
868 typed in or the decompiled expression. */
869 b->cond_string = xstrdup (arg);
870 b->condition_not_parsed = 0;
871
872 if (is_watchpoint (b))
873 {
3a5c3e22
PA
874 struct watchpoint *w = (struct watchpoint *) b;
875
699bd4cf 876 innermost_block_tracker tracker;
adc36818 877 arg = exp;
699bd4cf 878 w->cond_exp = parse_exp_1 (&arg, 0, 0, 0, &tracker);
adc36818
PM
879 if (*arg)
880 error (_("Junk at end of expression"));
699bd4cf 881 w->cond_exp_valid_block = tracker.block ();
adc36818
PM
882 }
883 else
884 {
3a5c3e22
PA
885 struct bp_location *loc;
886
adc36818
PM
887 for (loc = b->loc; loc; loc = loc->next)
888 {
889 arg = exp;
890 loc->cond =
1bb9788d
TT
891 parse_exp_1 (&arg, loc->address,
892 block_for_pc (loc->address), 0);
adc36818
PM
893 if (*arg)
894 error (_("Junk at end of expression"));
895 }
896 }
897 }
b775012e
LM
898 mark_breakpoint_modified (b);
899
76727919 900 gdb::observers::breakpoint_modified.notify (b);
adc36818
PM
901}
902
d55637df
TT
903/* Completion for the "condition" command. */
904
eb3ff9a5 905static void
6f937416 906condition_completer (struct cmd_list_element *cmd,
eb3ff9a5 907 completion_tracker &tracker,
6f937416 908 const char *text, const char *word)
d55637df 909{
6f937416 910 const char *space;
d55637df 911
f1735a53
TT
912 text = skip_spaces (text);
913 space = skip_to_space (text);
d55637df
TT
914 if (*space == '\0')
915 {
916 int len;
917 struct breakpoint *b;
d55637df
TT
918
919 if (text[0] == '$')
920 {
921 /* We don't support completion of history indices. */
eb3ff9a5
PA
922 if (!isdigit (text[1]))
923 complete_internalvar (tracker, &text[1]);
924 return;
d55637df
TT
925 }
926
927 /* We're completing the breakpoint number. */
928 len = strlen (text);
929
930 ALL_BREAKPOINTS (b)
58ce7251
SDJ
931 {
932 char number[50];
933
934 xsnprintf (number, sizeof (number), "%d", b->number);
935
936 if (strncmp (number, text, len) == 0)
b02f78f9 937 tracker.add_completion (make_unique_xstrdup (number));
58ce7251 938 }
d55637df 939
eb3ff9a5 940 return;
d55637df
TT
941 }
942
943 /* We're completing the expression part. */
f1735a53 944 text = skip_spaces (space);
eb3ff9a5 945 expression_completer (cmd, tracker, text, word);
d55637df
TT
946}
947
c906108c
SS
948/* condition N EXP -- set break condition of breakpoint N to EXP. */
949
950static void
0b39b52e 951condition_command (const char *arg, int from_tty)
c906108c 952{
52f0bd74 953 struct breakpoint *b;
0b39b52e 954 const char *p;
52f0bd74 955 int bnum;
c906108c
SS
956
957 if (arg == 0)
e2e0b3e5 958 error_no_arg (_("breakpoint number"));
c906108c
SS
959
960 p = arg;
961 bnum = get_number (&p);
5c44784c 962 if (bnum == 0)
8a3fe4f8 963 error (_("Bad breakpoint argument: '%s'"), arg);
c906108c
SS
964
965 ALL_BREAKPOINTS (b)
966 if (b->number == bnum)
2f069f6f 967 {
6dddc817
DE
968 /* Check if this breakpoint has a "stop" method implemented in an
969 extension language. This method and conditions entered into GDB
970 from the CLI are mutually exclusive. */
971 const struct extension_language_defn *extlang
972 = get_breakpoint_cond_ext_lang (b, EXT_LANG_NONE);
973
974 if (extlang != NULL)
975 {
976 error (_("Only one stop condition allowed. There is currently"
977 " a %s stop condition defined for this breakpoint."),
978 ext_lang_capitalized_name (extlang));
979 }
2566ad2d 980 set_breakpoint_condition (b, p, from_tty);
b775012e
LM
981
982 if (is_breakpoint (b))
44702360 983 update_global_location_list (UGLL_MAY_INSERT);
b775012e 984
2f069f6f
JB
985 return;
986 }
c906108c 987
8a3fe4f8 988 error (_("No breakpoint number %d."), bnum);
c906108c
SS
989}
990
a7bdde9e
VP
991/* Check that COMMAND do not contain commands that are suitable
992 only for tracepoints and not suitable for ordinary breakpoints.
4a64f543
MS
993 Throw if any such commands is found. */
994
a7bdde9e
VP
995static void
996check_no_tracepoint_commands (struct command_line *commands)
997{
998 struct command_line *c;
cc59ec59 999
a7bdde9e
VP
1000 for (c = commands; c; c = c->next)
1001 {
a7bdde9e 1002 if (c->control_type == while_stepping_control)
3e43a32a
MS
1003 error (_("The 'while-stepping' command can "
1004 "only be used for tracepoints"));
a7bdde9e 1005
12973681
TT
1006 check_no_tracepoint_commands (c->body_list_0.get ());
1007 check_no_tracepoint_commands (c->body_list_1.get ());
a7bdde9e
VP
1008
1009 /* Not that command parsing removes leading whitespace and comment
4a64f543 1010 lines and also empty lines. So, we only need to check for
a7bdde9e
VP
1011 command directly. */
1012 if (strstr (c->line, "collect ") == c->line)
1013 error (_("The 'collect' command can only be used for tracepoints"));
1014
51661e93
VP
1015 if (strstr (c->line, "teval ") == c->line)
1016 error (_("The 'teval' command can only be used for tracepoints"));
a7bdde9e
VP
1017 }
1018}
1019
c1fc2657 1020struct longjmp_breakpoint : public breakpoint
3b0871f4 1021{
c1fc2657 1022 ~longjmp_breakpoint () override;
3b0871f4
SM
1023};
1024
d77f58be
SS
1025/* Encapsulate tests for different types of tracepoints. */
1026
3b0871f4
SM
1027static bool
1028is_tracepoint_type (bptype type)
d9b3f62e
PA
1029{
1030 return (type == bp_tracepoint
1031 || type == bp_fast_tracepoint
1032 || type == bp_static_tracepoint);
1033}
1034
3b0871f4
SM
1035static bool
1036is_longjmp_type (bptype type)
1037{
1038 return type == bp_longjmp || type == bp_exception;
1039}
1040
f2478a7e
SM
1041/* See breakpoint.h. */
1042
1043bool
d77f58be 1044is_tracepoint (const struct breakpoint *b)
a7bdde9e 1045{
d9b3f62e 1046 return is_tracepoint_type (b->type);
a7bdde9e 1047}
d9b3f62e 1048
a5e364af
SM
1049/* Factory function to create an appropriate instance of breakpoint given
1050 TYPE. */
1051
1052static std::unique_ptr<breakpoint>
1053new_breakpoint_from_type (bptype type)
1054{
1055 breakpoint *b;
1056
1057 if (is_tracepoint_type (type))
c1fc2657 1058 b = new tracepoint ();
3b0871f4 1059 else if (is_longjmp_type (type))
c1fc2657 1060 b = new longjmp_breakpoint ();
a5e364af
SM
1061 else
1062 b = new breakpoint ();
1063
1064 return std::unique_ptr<breakpoint> (b);
1065}
1066
e5dd4106 1067/* A helper function that validates that COMMANDS are valid for a
95a42b64
TT
1068 breakpoint. This function will throw an exception if a problem is
1069 found. */
48cb2d85 1070
95a42b64
TT
1071static void
1072validate_commands_for_breakpoint (struct breakpoint *b,
1073 struct command_line *commands)
48cb2d85 1074{
d77f58be 1075 if (is_tracepoint (b))
a7bdde9e 1076 {
c9a6ce02 1077 struct tracepoint *t = (struct tracepoint *) b;
a7bdde9e
VP
1078 struct command_line *c;
1079 struct command_line *while_stepping = 0;
c9a6ce02
PA
1080
1081 /* Reset the while-stepping step count. The previous commands
1082 might have included a while-stepping action, while the new
1083 ones might not. */
1084 t->step_count = 0;
1085
1086 /* We need to verify that each top-level element of commands is
1087 valid for tracepoints, that there's at most one
1088 while-stepping element, and that the while-stepping's body
1089 has valid tracing commands excluding nested while-stepping.
1090 We also need to validate the tracepoint action line in the
1091 context of the tracepoint --- validate_actionline actually
1092 has side effects, like setting the tracepoint's
1093 while-stepping STEP_COUNT, in addition to checking if the
1094 collect/teval actions parse and make sense in the
1095 tracepoint's context. */
a7bdde9e
VP
1096 for (c = commands; c; c = c->next)
1097 {
a7bdde9e
VP
1098 if (c->control_type == while_stepping_control)
1099 {
1100 if (b->type == bp_fast_tracepoint)
3e43a32a
MS
1101 error (_("The 'while-stepping' command "
1102 "cannot be used for fast tracepoint"));
0fb4aa4b 1103 else if (b->type == bp_static_tracepoint)
3e43a32a
MS
1104 error (_("The 'while-stepping' command "
1105 "cannot be used for static tracepoint"));
a7bdde9e
VP
1106
1107 if (while_stepping)
3e43a32a
MS
1108 error (_("The 'while-stepping' command "
1109 "can be used only once"));
a7bdde9e
VP
1110 else
1111 while_stepping = c;
1112 }
c9a6ce02
PA
1113
1114 validate_actionline (c->line, b);
a7bdde9e
VP
1115 }
1116 if (while_stepping)
1117 {
1118 struct command_line *c2;
1119
12973681
TT
1120 gdb_assert (while_stepping->body_list_1 == nullptr);
1121 c2 = while_stepping->body_list_0.get ();
a7bdde9e
VP
1122 for (; c2; c2 = c2->next)
1123 {
a7bdde9e
VP
1124 if (c2->control_type == while_stepping_control)
1125 error (_("The 'while-stepping' command cannot be nested"));
1126 }
1127 }
1128 }
1129 else
1130 {
1131 check_no_tracepoint_commands (commands);
1132 }
95a42b64
TT
1133}
1134
0fb4aa4b
PA
1135/* Return a vector of all the static tracepoints set at ADDR. The
1136 caller is responsible for releasing the vector. */
1137
f51e0e20 1138std::vector<breakpoint *>
0fb4aa4b
PA
1139static_tracepoints_here (CORE_ADDR addr)
1140{
1141 struct breakpoint *b;
f51e0e20 1142 std::vector<breakpoint *> found;
0fb4aa4b
PA
1143 struct bp_location *loc;
1144
1145 ALL_BREAKPOINTS (b)
1146 if (b->type == bp_static_tracepoint)
1147 {
1148 for (loc = b->loc; loc; loc = loc->next)
1149 if (loc->address == addr)
f51e0e20 1150 found.push_back (b);
0fb4aa4b
PA
1151 }
1152
1153 return found;
1154}
1155
95a42b64 1156/* Set the command list of B to COMMANDS. If breakpoint is tracepoint,
4a64f543 1157 validate that only allowed commands are included. */
95a42b64
TT
1158
1159void
4a64f543 1160breakpoint_set_commands (struct breakpoint *b,
12973681 1161 counted_command_line &&commands)
95a42b64 1162{
93921405 1163 validate_commands_for_breakpoint (b, commands.get ());
a7bdde9e 1164
d1b0a7bf 1165 b->commands = std::move (commands);
76727919 1166 gdb::observers::breakpoint_modified.notify (b);
48cb2d85
VP
1167}
1168
45a43567
TT
1169/* Set the internal `silent' flag on the breakpoint. Note that this
1170 is not the same as the "silent" that may appear in the breakpoint's
1171 commands. */
1172
1173void
1174breakpoint_set_silent (struct breakpoint *b, int silent)
1175{
1176 int old_silent = b->silent;
1177
1178 b->silent = silent;
1179 if (old_silent != silent)
76727919 1180 gdb::observers::breakpoint_modified.notify (b);
45a43567
TT
1181}
1182
1183/* Set the thread for this breakpoint. If THREAD is -1, make the
1184 breakpoint work for any thread. */
1185
1186void
1187breakpoint_set_thread (struct breakpoint *b, int thread)
1188{
1189 int old_thread = b->thread;
1190
1191 b->thread = thread;
1192 if (old_thread != thread)
76727919 1193 gdb::observers::breakpoint_modified.notify (b);
45a43567
TT
1194}
1195
1196/* Set the task for this breakpoint. If TASK is 0, make the
1197 breakpoint work for any task. */
1198
1199void
1200breakpoint_set_task (struct breakpoint *b, int task)
1201{
1202 int old_task = b->task;
1203
1204 b->task = task;
1205 if (old_task != task)
76727919 1206 gdb::observers::breakpoint_modified.notify (b);
45a43567
TT
1207}
1208
95a42b64 1209static void
896b6bda 1210commands_command_1 (const char *arg, int from_tty,
4a64f543 1211 struct command_line *control)
95a42b64 1212{
d1b0a7bf 1213 counted_command_line cmd;
999700cd
PW
1214 /* cmd_read will be true once we have read cmd. Note that cmd might still be
1215 NULL after the call to read_command_lines if the user provides an empty
1216 list of command by just typing "end". */
1217 bool cmd_read = false;
95a42b64 1218
896b6bda
PA
1219 std::string new_arg;
1220
95a42b64
TT
1221 if (arg == NULL || !*arg)
1222 {
86b17b60 1223 if (breakpoint_count - prev_breakpoint_count > 1)
896b6bda
PA
1224 new_arg = string_printf ("%d-%d", prev_breakpoint_count + 1,
1225 breakpoint_count);
95a42b64 1226 else if (breakpoint_count > 0)
896b6bda 1227 new_arg = string_printf ("%d", breakpoint_count);
48649e1b
TT
1228 arg = new_arg.c_str ();
1229 }
1230
1231 map_breakpoint_numbers
1232 (arg, [&] (breakpoint *b)
1233 {
999700cd 1234 if (!cmd_read)
48649e1b 1235 {
999700cd 1236 gdb_assert (cmd == NULL);
48649e1b 1237 if (control != NULL)
12973681 1238 cmd = control->body_list_0;
48649e1b
TT
1239 else
1240 {
81b1e71c
TT
1241 std::string str
1242 = string_printf (_("Type commands for breakpoint(s) "
1243 "%s, one per line."),
1244 arg);
48649e1b 1245
60b3cef2
TT
1246 auto do_validate = [=] (const char *line)
1247 {
1248 validate_actionline (line, b);
1249 };
1250 gdb::function_view<void (const char *)> validator;
1251 if (is_tracepoint (b))
1252 validator = do_validate;
1253
1254 cmd = read_command_lines (str.c_str (), from_tty, 1, validator);
48649e1b 1255 }
999700cd 1256 cmd_read = true;
48649e1b
TT
1257 }
1258
1259 /* If a breakpoint was on the list more than once, we don't need to
1260 do anything. */
1261 if (b->commands != cmd)
1262 {
d1b0a7bf 1263 validate_commands_for_breakpoint (b, cmd.get ());
48649e1b 1264 b->commands = cmd;
76727919 1265 gdb::observers::breakpoint_modified.notify (b);
48649e1b
TT
1266 }
1267 });
95a42b64
TT
1268}
1269
1270static void
0b39b52e 1271commands_command (const char *arg, int from_tty)
95a42b64
TT
1272{
1273 commands_command_1 (arg, from_tty, NULL);
c906108c 1274}
40c03ae8
EZ
1275
1276/* Like commands_command, but instead of reading the commands from
1277 input stream, takes them from an already parsed command structure.
1278
1279 This is used by cli-script.c to DTRT with breakpoint commands
1280 that are part of if and while bodies. */
1281enum command_control_type
896b6bda 1282commands_from_control_command (const char *arg, struct command_line *cmd)
40c03ae8 1283{
95a42b64
TT
1284 commands_command_1 (arg, 0, cmd);
1285 return simple_control;
40c03ae8 1286}
876fa593
JK
1287
1288/* Return non-zero if BL->TARGET_INFO contains valid information. */
1289
1290static int
1291bp_location_has_shadow (struct bp_location *bl)
1292{
1293 if (bl->loc_type != bp_loc_software_breakpoint)
1294 return 0;
1295 if (!bl->inserted)
1296 return 0;
1297 if (bl->target_info.shadow_len == 0)
e5dd4106 1298 /* BL isn't valid, or doesn't shadow memory. */
876fa593
JK
1299 return 0;
1300 return 1;
1301}
1302
9d497a19
PA
1303/* Update BUF, which is LEN bytes read from the target address
1304 MEMADDR, by replacing a memory breakpoint with its shadowed
1305 contents.
1306
1307 If READBUF is not NULL, this buffer must not overlap with the of
1308 the breakpoint location's shadow_contents buffer. Otherwise, a
1309 failed assertion internal error will be raised. */
1310
1311static void
1312one_breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1313 const gdb_byte *writebuf_org,
1314 ULONGEST memaddr, LONGEST len,
1315 struct bp_target_info *target_info,
1316 struct gdbarch *gdbarch)
1317{
1318 /* Now do full processing of the found relevant range of elements. */
1319 CORE_ADDR bp_addr = 0;
1320 int bp_size = 0;
1321 int bptoffset = 0;
1322
1323 if (!breakpoint_address_match (target_info->placed_address_space, 0,
1324 current_program_space->aspace, 0))
1325 {
1326 /* The breakpoint is inserted in a different address space. */
1327 return;
1328 }
1329
1330 /* Addresses and length of the part of the breakpoint that
1331 we need to copy. */
1332 bp_addr = target_info->placed_address;
1333 bp_size = target_info->shadow_len;
1334
1335 if (bp_addr + bp_size <= memaddr)
1336 {
1337 /* The breakpoint is entirely before the chunk of memory we are
1338 reading. */
1339 return;
1340 }
1341
1342 if (bp_addr >= memaddr + len)
1343 {
1344 /* The breakpoint is entirely after the chunk of memory we are
1345 reading. */
1346 return;
1347 }
1348
1349 /* Offset within shadow_contents. */
1350 if (bp_addr < memaddr)
1351 {
1352 /* Only copy the second part of the breakpoint. */
1353 bp_size -= memaddr - bp_addr;
1354 bptoffset = memaddr - bp_addr;
1355 bp_addr = memaddr;
1356 }
1357
1358 if (bp_addr + bp_size > memaddr + len)
1359 {
1360 /* Only copy the first part of the breakpoint. */
1361 bp_size -= (bp_addr + bp_size) - (memaddr + len);
1362 }
1363
1364 if (readbuf != NULL)
1365 {
1366 /* Verify that the readbuf buffer does not overlap with the
1367 shadow_contents buffer. */
1368 gdb_assert (target_info->shadow_contents >= readbuf + len
1369 || readbuf >= (target_info->shadow_contents
1370 + target_info->shadow_len));
1371
1372 /* Update the read buffer with this inserted breakpoint's
1373 shadow. */
1374 memcpy (readbuf + bp_addr - memaddr,
1375 target_info->shadow_contents + bptoffset, bp_size);
1376 }
1377 else
1378 {
1379 const unsigned char *bp;
0d5ed153
MR
1380 CORE_ADDR addr = target_info->reqstd_address;
1381 int placed_size;
9d497a19
PA
1382
1383 /* Update the shadow with what we want to write to memory. */
1384 memcpy (target_info->shadow_contents + bptoffset,
1385 writebuf_org + bp_addr - memaddr, bp_size);
1386
1387 /* Determine appropriate breakpoint contents and size for this
1388 address. */
0d5ed153 1389 bp = gdbarch_breakpoint_from_pc (gdbarch, &addr, &placed_size);
9d497a19
PA
1390
1391 /* Update the final write buffer with this inserted
1392 breakpoint's INSN. */
1393 memcpy (writebuf + bp_addr - memaddr, bp + bptoffset, bp_size);
1394 }
1395}
1396
8defab1a 1397/* Update BUF, which is LEN bytes read from the target address MEMADDR,
876fa593
JK
1398 by replacing any memory breakpoints with their shadowed contents.
1399
35c63cd8
JB
1400 If READBUF is not NULL, this buffer must not overlap with any of
1401 the breakpoint location's shadow_contents buffers. Otherwise,
1402 a failed assertion internal error will be raised.
1403
876fa593 1404 The range of shadowed area by each bp_location is:
f5336ca5
PA
1405 bl->address - bp_locations_placed_address_before_address_max
1406 up to bl->address + bp_locations_shadow_len_after_address_max
876fa593
JK
1407 The range we were requested to resolve shadows for is:
1408 memaddr ... memaddr + len
1409 Thus the safe cutoff boundaries for performance optimization are
35df4500 1410 memaddr + len <= (bl->address
f5336ca5 1411 - bp_locations_placed_address_before_address_max)
876fa593 1412 and:
f5336ca5 1413 bl->address + bp_locations_shadow_len_after_address_max <= memaddr */
c906108c 1414
8defab1a 1415void
f0ba3972
PA
1416breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1417 const gdb_byte *writebuf_org,
1418 ULONGEST memaddr, LONGEST len)
c906108c 1419{
4a64f543
MS
1420 /* Left boundary, right boundary and median element of our binary
1421 search. */
876fa593
JK
1422 unsigned bc_l, bc_r, bc;
1423
4a64f543
MS
1424 /* Find BC_L which is a leftmost element which may affect BUF
1425 content. It is safe to report lower value but a failure to
1426 report higher one. */
876fa593
JK
1427
1428 bc_l = 0;
f5336ca5 1429 bc_r = bp_locations_count;
876fa593
JK
1430 while (bc_l + 1 < bc_r)
1431 {
35df4500 1432 struct bp_location *bl;
876fa593
JK
1433
1434 bc = (bc_l + bc_r) / 2;
f5336ca5 1435 bl = bp_locations[bc];
876fa593 1436
4a64f543
MS
1437 /* Check first BL->ADDRESS will not overflow due to the added
1438 constant. Then advance the left boundary only if we are sure
1439 the BC element can in no way affect the BUF content (MEMADDR
1440 to MEMADDR + LEN range).
876fa593 1441
f5336ca5 1442 Use the BP_LOCATIONS_SHADOW_LEN_AFTER_ADDRESS_MAX safety
4a64f543
MS
1443 offset so that we cannot miss a breakpoint with its shadow
1444 range tail still reaching MEMADDR. */
c5aa993b 1445
f5336ca5 1446 if ((bl->address + bp_locations_shadow_len_after_address_max
35df4500 1447 >= bl->address)
f5336ca5 1448 && (bl->address + bp_locations_shadow_len_after_address_max
35df4500 1449 <= memaddr))
876fa593
JK
1450 bc_l = bc;
1451 else
1452 bc_r = bc;
1453 }
1454
128070bb
PA
1455 /* Due to the binary search above, we need to make sure we pick the
1456 first location that's at BC_L's address. E.g., if there are
1457 multiple locations at the same address, BC_L may end up pointing
1458 at a duplicate location, and miss the "master"/"inserted"
1459 location. Say, given locations L1, L2 and L3 at addresses A and
1460 B:
1461
1462 L1@A, L2@A, L3@B, ...
1463
1464 BC_L could end up pointing at location L2, while the "master"
1465 location could be L1. Since the `loc->inserted' flag is only set
1466 on "master" locations, we'd forget to restore the shadow of L1
1467 and L2. */
1468 while (bc_l > 0
f5336ca5 1469 && bp_locations[bc_l]->address == bp_locations[bc_l - 1]->address)
128070bb
PA
1470 bc_l--;
1471
876fa593
JK
1472 /* Now do full processing of the found relevant range of elements. */
1473
f5336ca5 1474 for (bc = bc_l; bc < bp_locations_count; bc++)
c5aa993b 1475 {
f5336ca5 1476 struct bp_location *bl = bp_locations[bc];
876fa593 1477
35df4500
TJB
1478 /* bp_location array has BL->OWNER always non-NULL. */
1479 if (bl->owner->type == bp_none)
8a3fe4f8 1480 warning (_("reading through apparently deleted breakpoint #%d?"),
35df4500 1481 bl->owner->number);
ffce0d52 1482
e5dd4106 1483 /* Performance optimization: any further element can no longer affect BUF
876fa593
JK
1484 content. */
1485
f5336ca5
PA
1486 if (bl->address >= bp_locations_placed_address_before_address_max
1487 && memaddr + len <= (bl->address
1488 - bp_locations_placed_address_before_address_max))
876fa593
JK
1489 break;
1490
35df4500 1491 if (!bp_location_has_shadow (bl))
c5aa993b 1492 continue;
6c95b8df 1493
9d497a19
PA
1494 one_breakpoint_xfer_memory (readbuf, writebuf, writebuf_org,
1495 memaddr, len, &bl->target_info, bl->gdbarch);
1496 }
c906108c 1497}
9d497a19 1498
f2478a7e 1499/* See breakpoint.h. */
b775012e 1500
f2478a7e 1501bool
b775012e
LM
1502is_breakpoint (const struct breakpoint *bpt)
1503{
1504 return (bpt->type == bp_breakpoint
e7e0cddf
SS
1505 || bpt->type == bp_hardware_breakpoint
1506 || bpt->type == bp_dprintf);
b775012e
LM
1507}
1508
60e1c644
PA
1509/* Return true if BPT is of any hardware watchpoint kind. */
1510
f2478a7e 1511static bool
d77f58be 1512is_hardware_watchpoint (const struct breakpoint *bpt)
a5606eee
VP
1513{
1514 return (bpt->type == bp_hardware_watchpoint
1515 || bpt->type == bp_read_watchpoint
1516 || bpt->type == bp_access_watchpoint);
1517}
7270d8f2 1518
f2478a7e 1519/* See breakpoint.h. */
60e1c644 1520
f2478a7e 1521bool
d77f58be 1522is_watchpoint (const struct breakpoint *bpt)
60e1c644
PA
1523{
1524 return (is_hardware_watchpoint (bpt)
1525 || bpt->type == bp_watchpoint);
1526}
1527
3a5c3e22
PA
1528/* Returns true if the current thread and its running state are safe
1529 to evaluate or update watchpoint B. Watchpoints on local
1530 expressions need to be evaluated in the context of the thread that
1531 was current when the watchpoint was created, and, that thread needs
1532 to be stopped to be able to select the correct frame context.
1533 Watchpoints on global expressions can be evaluated on any thread,
1534 and in any state. It is presently left to the target allowing
1535 memory accesses when threads are running. */
f6bc2008
PA
1536
1537static int
3a5c3e22 1538watchpoint_in_thread_scope (struct watchpoint *b)
f6bc2008 1539{
c1fc2657 1540 return (b->pspace == current_program_space
d7e15655
TT
1541 && (b->watchpoint_thread == null_ptid
1542 || (inferior_ptid == b->watchpoint_thread
00431a78 1543 && !inferior_thread ()->executing)));
f6bc2008
PA
1544}
1545
d0fb5eae
JK
1546/* Set watchpoint B to disp_del_at_next_stop, even including its possible
1547 associated bp_watchpoint_scope breakpoint. */
1548
1549static void
3a5c3e22 1550watchpoint_del_at_next_stop (struct watchpoint *w)
d0fb5eae 1551{
c1fc2657 1552 if (w->related_breakpoint != w)
d0fb5eae 1553 {
c1fc2657
SM
1554 gdb_assert (w->related_breakpoint->type == bp_watchpoint_scope);
1555 gdb_assert (w->related_breakpoint->related_breakpoint == w);
1556 w->related_breakpoint->disposition = disp_del_at_next_stop;
1557 w->related_breakpoint->related_breakpoint = w->related_breakpoint;
1558 w->related_breakpoint = w;
d0fb5eae 1559 }
c1fc2657 1560 w->disposition = disp_del_at_next_stop;
d0fb5eae
JK
1561}
1562
bb9d5f81
PP
1563/* Extract a bitfield value from value VAL using the bit parameters contained in
1564 watchpoint W. */
1565
1566static struct value *
1567extract_bitfield_from_watchpoint_value (struct watchpoint *w, struct value *val)
1568{
1569 struct value *bit_val;
1570
1571 if (val == NULL)
1572 return NULL;
1573
1574 bit_val = allocate_value (value_type (val));
1575
1576 unpack_value_bitfield (bit_val,
1577 w->val_bitpos,
1578 w->val_bitsize,
1579 value_contents_for_printing (val),
1580 value_offset (val),
1581 val);
1582
1583 return bit_val;
1584}
1585
c6d81124
PA
1586/* Allocate a dummy location and add it to B, which must be a software
1587 watchpoint. This is required because even if a software watchpoint
1588 is not watching any memory, bpstat_stop_status requires a location
1589 to be able to report stops. */
1590
1591static void
1592software_watchpoint_add_no_memory_location (struct breakpoint *b,
1593 struct program_space *pspace)
1594{
1595 gdb_assert (b->type == bp_watchpoint && b->loc == NULL);
1596
1597 b->loc = allocate_bp_location (b);
1598 b->loc->pspace = pspace;
1599 b->loc->address = -1;
1600 b->loc->length = -1;
1601}
1602
1603/* Returns true if B is a software watchpoint that is not watching any
1604 memory (e.g., "watch $pc"). */
1605
f2478a7e 1606static bool
c6d81124
PA
1607is_no_memory_software_watchpoint (struct breakpoint *b)
1608{
1609 return (b->type == bp_watchpoint
1610 && b->loc != NULL
1611 && b->loc->next == NULL
1612 && b->loc->address == -1
1613 && b->loc->length == -1);
1614}
1615
567e1b4e
JB
1616/* Assuming that B is a watchpoint:
1617 - Reparse watchpoint expression, if REPARSE is non-zero
a5606eee 1618 - Evaluate expression and store the result in B->val
567e1b4e
JB
1619 - Evaluate the condition if there is one, and store the result
1620 in b->loc->cond.
a5606eee
VP
1621 - Update the list of values that must be watched in B->loc.
1622
4a64f543
MS
1623 If the watchpoint disposition is disp_del_at_next_stop, then do
1624 nothing. If this is local watchpoint that is out of scope, delete
1625 it.
1626
1627 Even with `set breakpoint always-inserted on' the watchpoints are
1628 removed + inserted on each stop here. Normal breakpoints must
1629 never be removed because they might be missed by a running thread
1630 when debugging in non-stop mode. On the other hand, hardware
1631 watchpoints (is_hardware_watchpoint; processed here) are specific
1632 to each LWP since they are stored in each LWP's hardware debug
1633 registers. Therefore, such LWP must be stopped first in order to
1634 be able to modify its hardware watchpoints.
1635
1636 Hardware watchpoints must be reset exactly once after being
1637 presented to the user. It cannot be done sooner, because it would
1638 reset the data used to present the watchpoint hit to the user. And
1639 it must not be done later because it could display the same single
1640 watchpoint hit during multiple GDB stops. Note that the latter is
1641 relevant only to the hardware watchpoint types bp_read_watchpoint
1642 and bp_access_watchpoint. False hit by bp_hardware_watchpoint is
1643 not user-visible - its hit is suppressed if the memory content has
1644 not changed.
1645
1646 The following constraints influence the location where we can reset
1647 hardware watchpoints:
1648
1649 * target_stopped_by_watchpoint and target_stopped_data_address are
1650 called several times when GDB stops.
1651
1652 [linux]
1653 * Multiple hardware watchpoints can be hit at the same time,
1654 causing GDB to stop. GDB only presents one hardware watchpoint
1655 hit at a time as the reason for stopping, and all the other hits
1656 are presented later, one after the other, each time the user
1657 requests the execution to be resumed. Execution is not resumed
1658 for the threads still having pending hit event stored in
1659 LWP_INFO->STATUS. While the watchpoint is already removed from
1660 the inferior on the first stop the thread hit event is kept being
1661 reported from its cached value by linux_nat_stopped_data_address
1662 until the real thread resume happens after the watchpoint gets
1663 presented and thus its LWP_INFO->STATUS gets reset.
1664
1665 Therefore the hardware watchpoint hit can get safely reset on the
1666 watchpoint removal from inferior. */
a79d3c27 1667
b40ce68a 1668static void
3a5c3e22 1669update_watchpoint (struct watchpoint *b, int reparse)
7270d8f2 1670{
a5606eee 1671 int within_current_scope;
a5606eee 1672 struct frame_id saved_frame_id;
66076460 1673 int frame_saved;
a5606eee 1674
f6bc2008
PA
1675 /* If this is a local watchpoint, we only want to check if the
1676 watchpoint frame is in scope if the current thread is the thread
1677 that was used to create the watchpoint. */
1678 if (!watchpoint_in_thread_scope (b))
1679 return;
1680
c1fc2657 1681 if (b->disposition == disp_del_at_next_stop)
a5606eee
VP
1682 return;
1683
66076460 1684 frame_saved = 0;
a5606eee
VP
1685
1686 /* Determine if the watchpoint is within scope. */
1687 if (b->exp_valid_block == NULL)
1688 within_current_scope = 1;
1689 else
1690 {
b5db5dfc
UW
1691 struct frame_info *fi = get_current_frame ();
1692 struct gdbarch *frame_arch = get_frame_arch (fi);
1693 CORE_ADDR frame_pc = get_frame_pc (fi);
1694
c9cf6e20
MG
1695 /* If we're at a point where the stack has been destroyed
1696 (e.g. in a function epilogue), unwinding may not work
1697 properly. Do not attempt to recreate locations at this
b5db5dfc 1698 point. See similar comments in watchpoint_check. */
c9cf6e20 1699 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
b5db5dfc 1700 return;
66076460
DJ
1701
1702 /* Save the current frame's ID so we can restore it after
1703 evaluating the watchpoint expression on its own frame. */
1704 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
1705 took a frame parameter, so that we didn't have to change the
1706 selected frame. */
1707 frame_saved = 1;
1708 saved_frame_id = get_frame_id (get_selected_frame (NULL));
1709
a5606eee
VP
1710 fi = frame_find_by_id (b->watchpoint_frame);
1711 within_current_scope = (fi != NULL);
1712 if (within_current_scope)
1713 select_frame (fi);
1714 }
1715
b5db5dfc
UW
1716 /* We don't free locations. They are stored in the bp_location array
1717 and update_global_location_list will eventually delete them and
1718 remove breakpoints if needed. */
c1fc2657 1719 b->loc = NULL;
b5db5dfc 1720
a5606eee
VP
1721 if (within_current_scope && reparse)
1722 {
bbc13ae3 1723 const char *s;
d63d0675 1724
4d01a485 1725 b->exp.reset ();
d63d0675 1726 s = b->exp_string_reparse ? b->exp_string_reparse : b->exp_string;
1bb9788d 1727 b->exp = parse_exp_1 (&s, 0, b->exp_valid_block, 0);
a5606eee
VP
1728 /* If the meaning of expression itself changed, the old value is
1729 no longer relevant. We don't want to report a watchpoint hit
1730 to the user when the old value and the new value may actually
1731 be completely different objects. */
fa4727a6 1732 b->val = NULL;
4c1d86d9 1733 b->val_valid = false;
60e1c644
PA
1734
1735 /* Note that unlike with breakpoints, the watchpoint's condition
1736 expression is stored in the breakpoint object, not in the
1737 locations (re)created below. */
c1fc2657 1738 if (b->cond_string != NULL)
60e1c644 1739 {
4d01a485 1740 b->cond_exp.reset ();
60e1c644 1741
c1fc2657 1742 s = b->cond_string;
1bb9788d 1743 b->cond_exp = parse_exp_1 (&s, 0, b->cond_exp_valid_block, 0);
60e1c644 1744 }
a5606eee 1745 }
a5606eee
VP
1746
1747 /* If we failed to parse the expression, for example because
1748 it refers to a global variable in a not-yet-loaded shared library,
1749 don't try to insert watchpoint. We don't automatically delete
1750 such watchpoint, though, since failure to parse expression
1751 is different from out-of-scope watchpoint. */
e8369a73 1752 if (!target_has_execution)
2d134ed3
PA
1753 {
1754 /* Without execution, memory can't change. No use to try and
1755 set watchpoint locations. The watchpoint will be reset when
1756 the target gains execution, through breakpoint_re_set. */
e8369a73
AB
1757 if (!can_use_hw_watchpoints)
1758 {
c1fc2657
SM
1759 if (b->ops->works_in_software_mode (b))
1760 b->type = bp_watchpoint;
e8369a73 1761 else
638aa5a1
AB
1762 error (_("Can't set read/access watchpoint when "
1763 "hardware watchpoints are disabled."));
e8369a73 1764 }
2d134ed3
PA
1765 }
1766 else if (within_current_scope && b->exp)
a5606eee 1767 {
0cf6dd15 1768 int pc = 0;
a6535de1 1769 std::vector<value_ref_ptr> val_chain;
8d49165d 1770 struct value *v, *result;
2d134ed3 1771 struct program_space *frame_pspace;
a5606eee 1772
4d01a485 1773 fetch_subexp_value (b->exp.get (), &pc, &v, &result, &val_chain, 0);
a5606eee 1774
a5606eee
VP
1775 /* Avoid setting b->val if it's already set. The meaning of
1776 b->val is 'the last value' user saw, and we should update
1777 it only if we reported that last value to user. As it
9c06b0b4
TJB
1778 happens, the code that reports it updates b->val directly.
1779 We don't keep track of the memory value for masked
1780 watchpoints. */
c1fc2657 1781 if (!b->val_valid && !is_masked_watchpoint (b))
fa4727a6 1782 {
bb9d5f81 1783 if (b->val_bitsize != 0)
850645cf
TT
1784 v = extract_bitfield_from_watchpoint_value (b, v);
1785 b->val = release_value (v);
4c1d86d9 1786 b->val_valid = true;
fa4727a6 1787 }
a5606eee 1788
2d134ed3
PA
1789 frame_pspace = get_frame_program_space (get_selected_frame (NULL));
1790
a5606eee 1791 /* Look at each value on the value chain. */
a6535de1
TT
1792 gdb_assert (!val_chain.empty ());
1793 for (const value_ref_ptr &iter : val_chain)
a5606eee 1794 {
a6535de1
TT
1795 v = iter.get ();
1796
a5606eee
VP
1797 /* If it's a memory location, and GDB actually needed
1798 its contents to evaluate the expression, then we
fa4727a6
DJ
1799 must watch it. If the first value returned is
1800 still lazy, that means an error occurred reading it;
1801 watch it anyway in case it becomes readable. */
a5606eee 1802 if (VALUE_LVAL (v) == lval_memory
a6535de1 1803 && (v == val_chain[0] || ! value_lazy (v)))
a5606eee
VP
1804 {
1805 struct type *vtype = check_typedef (value_type (v));
7270d8f2 1806
a5606eee
VP
1807 /* We only watch structs and arrays if user asked
1808 for it explicitly, never if they just happen to
1809 appear in the middle of some value chain. */
fa4727a6 1810 if (v == result
a5606eee
VP
1811 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
1812 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
1813 {
1814 CORE_ADDR addr;
f486487f 1815 enum target_hw_bp_type type;
a5606eee 1816 struct bp_location *loc, **tmp;
bb9d5f81
PP
1817 int bitpos = 0, bitsize = 0;
1818
1819 if (value_bitsize (v) != 0)
1820 {
1821 /* Extract the bit parameters out from the bitfield
1822 sub-expression. */
1823 bitpos = value_bitpos (v);
1824 bitsize = value_bitsize (v);
1825 }
1826 else if (v == result && b->val_bitsize != 0)
1827 {
1828 /* If VAL_BITSIZE != 0 then RESULT is actually a bitfield
1829 lvalue whose bit parameters are saved in the fields
1830 VAL_BITPOS and VAL_BITSIZE. */
1831 bitpos = b->val_bitpos;
1832 bitsize = b->val_bitsize;
1833 }
a5606eee 1834
42ae5230 1835 addr = value_address (v);
bb9d5f81
PP
1836 if (bitsize != 0)
1837 {
1838 /* Skip the bytes that don't contain the bitfield. */
1839 addr += bitpos / 8;
1840 }
1841
a5606eee 1842 type = hw_write;
c1fc2657 1843 if (b->type == bp_read_watchpoint)
a5606eee 1844 type = hw_read;
c1fc2657 1845 else if (b->type == bp_access_watchpoint)
a5606eee 1846 type = hw_access;
3a5c3e22 1847
c1fc2657
SM
1848 loc = allocate_bp_location (b);
1849 for (tmp = &(b->loc); *tmp != NULL; tmp = &((*tmp)->next))
a5606eee
VP
1850 ;
1851 *tmp = loc;
a6d9a66e 1852 loc->gdbarch = get_type_arch (value_type (v));
6c95b8df
PA
1853
1854 loc->pspace = frame_pspace;
f17d9474 1855 loc->address = address_significant (loc->gdbarch, addr);
bb9d5f81
PP
1856
1857 if (bitsize != 0)
1858 {
1859 /* Just cover the bytes that make up the bitfield. */
1860 loc->length = ((bitpos % 8) + bitsize + 7) / 8;
1861 }
1862 else
1863 loc->length = TYPE_LENGTH (value_type (v));
1864
a5606eee
VP
1865 loc->watchpoint_type = type;
1866 }
1867 }
9fa40276
TJB
1868 }
1869
1870 /* Change the type of breakpoint between hardware assisted or
1871 an ordinary watchpoint depending on the hardware support
1872 and free hardware slots. REPARSE is set when the inferior
1873 is started. */
a9634178 1874 if (reparse)
9fa40276 1875 {
e09342b5 1876 int reg_cnt;
9fa40276
TJB
1877 enum bp_loc_type loc_type;
1878 struct bp_location *bl;
a5606eee 1879
a9634178 1880 reg_cnt = can_use_hardware_watchpoint (val_chain);
e09342b5
TJB
1881
1882 if (reg_cnt)
9fa40276
TJB
1883 {
1884 int i, target_resources_ok, other_type_used;
a1398e0c 1885 enum bptype type;
9fa40276 1886
a9634178
TJB
1887 /* Use an exact watchpoint when there's only one memory region to be
1888 watched, and only one debug register is needed to watch it. */
1889 b->exact = target_exact_watchpoints && reg_cnt == 1;
1890
9fa40276 1891 /* We need to determine how many resources are already
e09342b5
TJB
1892 used for all other hardware watchpoints plus this one
1893 to see if we still have enough resources to also fit
a1398e0c
PA
1894 this watchpoint in as well. */
1895
1896 /* If this is a software watchpoint, we try to turn it
1897 to a hardware one -- count resources as if B was of
1898 hardware watchpoint type. */
c1fc2657 1899 type = b->type;
a1398e0c
PA
1900 if (type == bp_watchpoint)
1901 type = bp_hardware_watchpoint;
1902
1903 /* This watchpoint may or may not have been placed on
1904 the list yet at this point (it won't be in the list
1905 if we're trying to create it for the first time,
1906 through watch_command), so always account for it
1907 manually. */
1908
1909 /* Count resources used by all watchpoints except B. */
c1fc2657 1910 i = hw_watchpoint_used_count_others (b, type, &other_type_used);
a1398e0c
PA
1911
1912 /* Add in the resources needed for B. */
c1fc2657 1913 i += hw_watchpoint_use_count (b);
a1398e0c
PA
1914
1915 target_resources_ok
1916 = target_can_use_hardware_watchpoint (type, i, other_type_used);
e09342b5 1917 if (target_resources_ok <= 0)
a9634178 1918 {
c1fc2657 1919 int sw_mode = b->ops->works_in_software_mode (b);
9c06b0b4
TJB
1920
1921 if (target_resources_ok == 0 && !sw_mode)
a9634178
TJB
1922 error (_("Target does not support this type of "
1923 "hardware watchpoint."));
9c06b0b4
TJB
1924 else if (target_resources_ok < 0 && !sw_mode)
1925 error (_("There are not enough available hardware "
1926 "resources for this watchpoint."));
a1398e0c
PA
1927
1928 /* Downgrade to software watchpoint. */
c1fc2657 1929 b->type = bp_watchpoint;
a1398e0c
PA
1930 }
1931 else
1932 {
1933 /* If this was a software watchpoint, we've just
1934 found we have enough resources to turn it to a
1935 hardware watchpoint. Otherwise, this is a
1936 nop. */
c1fc2657 1937 b->type = type;
a9634178 1938 }
9fa40276 1939 }
c1fc2657 1940 else if (!b->ops->works_in_software_mode (b))
638aa5a1
AB
1941 {
1942 if (!can_use_hw_watchpoints)
1943 error (_("Can't set read/access watchpoint when "
1944 "hardware watchpoints are disabled."));
1945 else
1946 error (_("Expression cannot be implemented with "
1947 "read/access watchpoint."));
1948 }
9fa40276 1949 else
c1fc2657 1950 b->type = bp_watchpoint;
9fa40276 1951
c1fc2657 1952 loc_type = (b->type == bp_watchpoint? bp_loc_other
9fa40276 1953 : bp_loc_hardware_watchpoint);
c1fc2657 1954 for (bl = b->loc; bl; bl = bl->next)
9fa40276
TJB
1955 bl->loc_type = loc_type;
1956 }
1957
c7437ca6
PA
1958 /* If a software watchpoint is not watching any memory, then the
1959 above left it without any location set up. But,
1960 bpstat_stop_status requires a location to be able to report
1961 stops, so make sure there's at least a dummy one. */
c1fc2657
SM
1962 if (b->type == bp_watchpoint && b->loc == NULL)
1963 software_watchpoint_add_no_memory_location (b, frame_pspace);
a5606eee
VP
1964 }
1965 else if (!within_current_scope)
7270d8f2 1966 {
ac74f770
MS
1967 printf_filtered (_("\
1968Watchpoint %d deleted because the program has left the block\n\
1969in which its expression is valid.\n"),
c1fc2657 1970 b->number);
d0fb5eae 1971 watchpoint_del_at_next_stop (b);
7270d8f2 1972 }
a5606eee
VP
1973
1974 /* Restore the selected frame. */
66076460
DJ
1975 if (frame_saved)
1976 select_frame (frame_find_by_id (saved_frame_id));
7270d8f2
OF
1977}
1978
a5606eee 1979
74960c60 1980/* Returns 1 iff breakpoint location should be
1e4d1764
YQ
1981 inserted in the inferior. We don't differentiate the type of BL's owner
1982 (breakpoint vs. tracepoint), although insert_location in tracepoint's
1983 breakpoint_ops is not defined, because in insert_bp_location,
1984 tracepoint's insert_location will not be called. */
74960c60 1985static int
35df4500 1986should_be_inserted (struct bp_location *bl)
74960c60 1987{
35df4500 1988 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
74960c60
VP
1989 return 0;
1990
35df4500 1991 if (bl->owner->disposition == disp_del_at_next_stop)
74960c60
VP
1992 return 0;
1993
35df4500 1994 if (!bl->enabled || bl->shlib_disabled || bl->duplicate)
74960c60
VP
1995 return 0;
1996
f8eba3c6
TT
1997 if (user_breakpoint_p (bl->owner) && bl->pspace->executing_startup)
1998 return 0;
1999
56710373
PA
2000 /* This is set for example, when we're attached to the parent of a
2001 vfork, and have detached from the child. The child is running
2002 free, and we expect it to do an exec or exit, at which point the
2003 OS makes the parent schedulable again (and the target reports
2004 that the vfork is done). Until the child is done with the shared
2005 memory region, do not insert breakpoints in the parent, otherwise
2006 the child could still trip on the parent's breakpoints. Since
2007 the parent is blocked anyway, it won't miss any breakpoint. */
35df4500 2008 if (bl->pspace->breakpoints_not_allowed)
56710373
PA
2009 return 0;
2010
31e77af2 2011 /* Don't insert a breakpoint if we're trying to step past its
21edc42f
YQ
2012 location, except if the breakpoint is a single-step breakpoint,
2013 and the breakpoint's thread is the thread which is stepping past
2014 a breakpoint. */
31e77af2
PA
2015 if ((bl->loc_type == bp_loc_software_breakpoint
2016 || bl->loc_type == bp_loc_hardware_breakpoint)
2017 && stepping_past_instruction_at (bl->pspace->aspace,
21edc42f
YQ
2018 bl->address)
2019 /* The single-step breakpoint may be inserted at the location
2020 we're trying to step if the instruction branches to itself.
2021 However, the instruction won't be executed at all and it may
2022 break the semantics of the instruction, for example, the
2023 instruction is a conditional branch or updates some flags.
2024 We can't fix it unless GDB is able to emulate the instruction
2025 or switch to displaced stepping. */
2026 && !(bl->owner->type == bp_single_step
2027 && thread_is_stepping_over_breakpoint (bl->owner->thread)))
e558d7c1
PA
2028 {
2029 if (debug_infrun)
2030 {
2031 fprintf_unfiltered (gdb_stdlog,
2032 "infrun: skipping breakpoint: "
2033 "stepping past insn at: %s\n",
2034 paddress (bl->gdbarch, bl->address));
2035 }
2036 return 0;
2037 }
31e77af2 2038
963f9c80
PA
2039 /* Don't insert watchpoints if we're trying to step past the
2040 instruction that triggered one. */
2041 if ((bl->loc_type == bp_loc_hardware_watchpoint)
2042 && stepping_past_nonsteppable_watchpoint ())
2043 {
2044 if (debug_infrun)
2045 {
2046 fprintf_unfiltered (gdb_stdlog,
2047 "infrun: stepping past non-steppable watchpoint. "
2048 "skipping watchpoint at %s:%d\n",
2049 paddress (bl->gdbarch, bl->address),
2050 bl->length);
2051 }
2052 return 0;
2053 }
2054
74960c60
VP
2055 return 1;
2056}
2057
934709f0
PW
2058/* Same as should_be_inserted but does the check assuming
2059 that the location is not duplicated. */
2060
2061static int
2062unduplicated_should_be_inserted (struct bp_location *bl)
2063{
2064 int result;
2065 const int save_duplicate = bl->duplicate;
2066
2067 bl->duplicate = 0;
2068 result = should_be_inserted (bl);
2069 bl->duplicate = save_duplicate;
2070 return result;
2071}
2072
b775012e
LM
2073/* Parses a conditional described by an expression COND into an
2074 agent expression bytecode suitable for evaluation
2075 by the bytecode interpreter. Return NULL if there was
2076 any error during parsing. */
2077
833177a4 2078static agent_expr_up
b775012e
LM
2079parse_cond_to_aexpr (CORE_ADDR scope, struct expression *cond)
2080{
833177a4 2081 if (cond == NULL)
b775012e
LM
2082 return NULL;
2083
833177a4
PA
2084 agent_expr_up aexpr;
2085
b775012e
LM
2086 /* We don't want to stop processing, so catch any errors
2087 that may show up. */
a70b8144 2088 try
b775012e 2089 {
036e657b 2090 aexpr = gen_eval_for_expr (scope, cond);
b775012e
LM
2091 }
2092
230d2906 2093 catch (const gdb_exception_error &ex)
b775012e
LM
2094 {
2095 /* If we got here, it means the condition could not be parsed to a valid
2096 bytecode expression and thus can't be evaluated on the target's side.
2097 It's no use iterating through the conditions. */
b775012e
LM
2098 }
2099
2100 /* We have a valid agent expression. */
2101 return aexpr;
2102}
2103
2104/* Based on location BL, create a list of breakpoint conditions to be
2105 passed on to the target. If we have duplicated locations with different
2106 conditions, we will add such conditions to the list. The idea is that the
2107 target will evaluate the list of conditions and will only notify GDB when
2108 one of them is true. */
2109
2110static void
2111build_target_condition_list (struct bp_location *bl)
2112{
2113 struct bp_location **locp = NULL, **loc2p;
2114 int null_condition_or_parse_error = 0;
2115 int modified = bl->needs_update;
2116 struct bp_location *loc;
2117
8b4f3082 2118 /* Release conditions left over from a previous insert. */
3cde5c42 2119 bl->target_info.conditions.clear ();
8b4f3082 2120
b775012e
LM
2121 /* This is only meaningful if the target is
2122 evaluating conditions and if the user has
2123 opted for condition evaluation on the target's
2124 side. */
2125 if (gdb_evaluates_breakpoint_condition_p ()
2126 || !target_supports_evaluation_of_breakpoint_conditions ())
2127 return;
2128
2129 /* Do a first pass to check for locations with no assigned
2130 conditions or conditions that fail to parse to a valid agent expression
2131 bytecode. If any of these happen, then it's no use to send conditions
2132 to the target since this location will always trigger and generate a
2133 response back to GDB. */
2134 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2135 {
2136 loc = (*loc2p);
2137 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2138 {
2139 if (modified)
2140 {
b775012e
LM
2141 /* Re-parse the conditions since something changed. In that
2142 case we already freed the condition bytecodes (see
2143 force_breakpoint_reinsertion). We just
2144 need to parse the condition to bytecodes again. */
833177a4
PA
2145 loc->cond_bytecode = parse_cond_to_aexpr (bl->address,
2146 loc->cond.get ());
b775012e
LM
2147 }
2148
2149 /* If we have a NULL bytecode expression, it means something
2150 went wrong or we have a null condition expression. */
2151 if (!loc->cond_bytecode)
2152 {
2153 null_condition_or_parse_error = 1;
2154 break;
2155 }
2156 }
2157 }
2158
2159 /* If any of these happened, it means we will have to evaluate the conditions
2160 for the location's address on gdb's side. It is no use keeping bytecodes
2161 for all the other duplicate locations, thus we free all of them here.
2162
2163 This is so we have a finer control over which locations' conditions are
2164 being evaluated by GDB or the remote stub. */
2165 if (null_condition_or_parse_error)
2166 {
2167 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2168 {
2169 loc = (*loc2p);
2170 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2171 {
2172 /* Only go as far as the first NULL bytecode is
2173 located. */
2174 if (!loc->cond_bytecode)
2175 return;
2176
833177a4 2177 loc->cond_bytecode.reset ();
b775012e
LM
2178 }
2179 }
2180 }
2181
2182 /* No NULL conditions or failed bytecode generation. Build a condition list
2183 for this location's address. */
2184 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2185 {
2186 loc = (*loc2p);
2187 if (loc->cond
2188 && is_breakpoint (loc->owner)
2189 && loc->pspace->num == bl->pspace->num
2190 && loc->owner->enable_state == bp_enabled
2191 && loc->enabled)
3cde5c42
PA
2192 {
2193 /* Add the condition to the vector. This will be used later
2194 to send the conditions to the target. */
2195 bl->target_info.conditions.push_back (loc->cond_bytecode.get ());
2196 }
b775012e
LM
2197 }
2198
2199 return;
2200}
2201
d3ce09f5
SS
2202/* Parses a command described by string CMD into an agent expression
2203 bytecode suitable for evaluation by the bytecode interpreter.
2204 Return NULL if there was any error during parsing. */
2205
833177a4 2206static agent_expr_up
d3ce09f5
SS
2207parse_cmd_to_aexpr (CORE_ADDR scope, char *cmd)
2208{
bbc13ae3
KS
2209 const char *cmdrest;
2210 const char *format_start, *format_end;
d3ce09f5
SS
2211 struct gdbarch *gdbarch = get_current_arch ();
2212
833177a4 2213 if (cmd == NULL)
d3ce09f5
SS
2214 return NULL;
2215
2216 cmdrest = cmd;
2217
2218 if (*cmdrest == ',')
2219 ++cmdrest;
f1735a53 2220 cmdrest = skip_spaces (cmdrest);
d3ce09f5
SS
2221
2222 if (*cmdrest++ != '"')
2223 error (_("No format string following the location"));
2224
2225 format_start = cmdrest;
2226
8e481c3b 2227 format_pieces fpieces (&cmdrest);
d3ce09f5
SS
2228
2229 format_end = cmdrest;
2230
2231 if (*cmdrest++ != '"')
2232 error (_("Bad format string, non-terminated '\"'."));
2233
f1735a53 2234 cmdrest = skip_spaces (cmdrest);
d3ce09f5
SS
2235
2236 if (!(*cmdrest == ',' || *cmdrest == '\0'))
2237 error (_("Invalid argument syntax"));
2238
2239 if (*cmdrest == ',')
2240 cmdrest++;
f1735a53 2241 cmdrest = skip_spaces (cmdrest);
d3ce09f5
SS
2242
2243 /* For each argument, make an expression. */
2244
8e481c3b 2245 std::vector<struct expression *> argvec;
d3ce09f5
SS
2246 while (*cmdrest != '\0')
2247 {
bbc13ae3 2248 const char *cmd1;
d3ce09f5
SS
2249
2250 cmd1 = cmdrest;
4d01a485 2251 expression_up expr = parse_exp_1 (&cmd1, scope, block_for_pc (scope), 1);
8e481c3b 2252 argvec.push_back (expr.release ());
d3ce09f5
SS
2253 cmdrest = cmd1;
2254 if (*cmdrest == ',')
2255 ++cmdrest;
2256 }
2257
833177a4
PA
2258 agent_expr_up aexpr;
2259
d3ce09f5
SS
2260 /* We don't want to stop processing, so catch any errors
2261 that may show up. */
a70b8144 2262 try
d3ce09f5 2263 {
036e657b
JB
2264 aexpr = gen_printf (scope, gdbarch, 0, 0,
2265 format_start, format_end - format_start,
8e481c3b 2266 argvec.size (), argvec.data ());
d3ce09f5 2267 }
230d2906 2268 catch (const gdb_exception_error &ex)
d3ce09f5
SS
2269 {
2270 /* If we got here, it means the command could not be parsed to a valid
2271 bytecode expression and thus can't be evaluated on the target's side.
2272 It's no use iterating through the other commands. */
d3ce09f5 2273 }
492d29ea 2274
d3ce09f5
SS
2275 /* We have a valid agent expression, return it. */
2276 return aexpr;
2277}
2278
2279/* Based on location BL, create a list of breakpoint commands to be
2280 passed on to the target. If we have duplicated locations with
2281 different commands, we will add any such to the list. */
2282
2283static void
2284build_target_command_list (struct bp_location *bl)
2285{
2286 struct bp_location **locp = NULL, **loc2p;
2287 int null_command_or_parse_error = 0;
2288 int modified = bl->needs_update;
2289 struct bp_location *loc;
2290
3cde5c42
PA
2291 /* Clear commands left over from a previous insert. */
2292 bl->target_info.tcommands.clear ();
8b4f3082 2293
41fac0cf 2294 if (!target_can_run_breakpoint_commands ())
d3ce09f5
SS
2295 return;
2296
41fac0cf
PA
2297 /* For now, limit to agent-style dprintf breakpoints. */
2298 if (dprintf_style != dprintf_style_agent)
d3ce09f5
SS
2299 return;
2300
41fac0cf
PA
2301 /* For now, if we have any duplicate location that isn't a dprintf,
2302 don't install the target-side commands, as that would make the
2303 breakpoint not be reported to the core, and we'd lose
2304 control. */
2305 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2306 {
2307 loc = (*loc2p);
2308 if (is_breakpoint (loc->owner)
2309 && loc->pspace->num == bl->pspace->num
2310 && loc->owner->type != bp_dprintf)
2311 return;
2312 }
2313
d3ce09f5
SS
2314 /* Do a first pass to check for locations with no assigned
2315 conditions or conditions that fail to parse to a valid agent expression
2316 bytecode. If any of these happen, then it's no use to send conditions
2317 to the target since this location will always trigger and generate a
2318 response back to GDB. */
2319 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2320 {
2321 loc = (*loc2p);
2322 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2323 {
2324 if (modified)
2325 {
d3ce09f5
SS
2326 /* Re-parse the commands since something changed. In that
2327 case we already freed the command bytecodes (see
2328 force_breakpoint_reinsertion). We just
2329 need to parse the command to bytecodes again. */
833177a4
PA
2330 loc->cmd_bytecode
2331 = parse_cmd_to_aexpr (bl->address,
2332 loc->owner->extra_string);
d3ce09f5
SS
2333 }
2334
2335 /* If we have a NULL bytecode expression, it means something
2336 went wrong or we have a null command expression. */
2337 if (!loc->cmd_bytecode)
2338 {
2339 null_command_or_parse_error = 1;
2340 break;
2341 }
2342 }
2343 }
2344
2345 /* If anything failed, then we're not doing target-side commands,
2346 and so clean up. */
2347 if (null_command_or_parse_error)
2348 {
2349 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2350 {
2351 loc = (*loc2p);
2352 if (is_breakpoint (loc->owner)
2353 && loc->pspace->num == bl->pspace->num)
2354 {
2355 /* Only go as far as the first NULL bytecode is
2356 located. */
40fb6c5e 2357 if (loc->cmd_bytecode == NULL)
d3ce09f5
SS
2358 return;
2359
833177a4 2360 loc->cmd_bytecode.reset ();
d3ce09f5
SS
2361 }
2362 }
2363 }
2364
2365 /* No NULL commands or failed bytecode generation. Build a command list
2366 for this location's address. */
2367 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2368 {
2369 loc = (*loc2p);
2370 if (loc->owner->extra_string
2371 && is_breakpoint (loc->owner)
2372 && loc->pspace->num == bl->pspace->num
2373 && loc->owner->enable_state == bp_enabled
2374 && loc->enabled)
3cde5c42
PA
2375 {
2376 /* Add the command to the vector. This will be used later
2377 to send the commands to the target. */
2378 bl->target_info.tcommands.push_back (loc->cmd_bytecode.get ());
2379 }
d3ce09f5
SS
2380 }
2381
2382 bl->target_info.persist = 0;
2383 /* Maybe flag this location as persistent. */
2384 if (bl->owner->type == bp_dprintf && disconnected_dprintf)
2385 bl->target_info.persist = 1;
2386}
2387
833b7ab5
YQ
2388/* Return the kind of breakpoint on address *ADDR. Get the kind
2389 of breakpoint according to ADDR except single-step breakpoint.
2390 Get the kind of single-step breakpoint according to the current
2391 registers state. */
cd6c3b4f
YQ
2392
2393static int
2394breakpoint_kind (struct bp_location *bl, CORE_ADDR *addr)
2395{
833b7ab5
YQ
2396 if (bl->owner->type == bp_single_step)
2397 {
2398 struct thread_info *thr = find_thread_global_id (bl->owner->thread);
2399 struct regcache *regcache;
2400
00431a78 2401 regcache = get_thread_regcache (thr);
833b7ab5
YQ
2402
2403 return gdbarch_breakpoint_kind_from_current_state (bl->gdbarch,
2404 regcache, addr);
2405 }
2406 else
2407 return gdbarch_breakpoint_kind_from_pc (bl->gdbarch, addr);
cd6c3b4f
YQ
2408}
2409
35df4500
TJB
2410/* Insert a low-level "breakpoint" of some type. BL is the breakpoint
2411 location. Any error messages are printed to TMP_ERROR_STREAM; and
3fbb6ffa 2412 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
c30eee59
TJB
2413 Returns 0 for success, 1 if the bp_location type is not supported or
2414 -1 for failure.
879bfdc2 2415
4a64f543
MS
2416 NOTE drow/2003-09-09: This routine could be broken down to an
2417 object-style method for each breakpoint or catchpoint type. */
26bb91f3 2418static int
35df4500 2419insert_bp_location (struct bp_location *bl,
26bb91f3 2420 struct ui_file *tmp_error_stream,
3fbb6ffa 2421 int *disabled_breaks,
dd61ec5c
MW
2422 int *hw_breakpoint_error,
2423 int *hw_bp_error_explained_already)
879bfdc2 2424{
cc06b668 2425 gdb_exception bp_excpt;
879bfdc2 2426
b775012e 2427 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
879bfdc2
DJ
2428 return 0;
2429
35c63cd8
JB
2430 /* Note we don't initialize bl->target_info, as that wipes out
2431 the breakpoint location's shadow_contents if the breakpoint
2432 is still inserted at that location. This in turn breaks
2433 target_read_memory which depends on these buffers when
2434 a memory read is requested at the breakpoint location:
2435 Once the target_info has been wiped, we fail to see that
2436 we have a breakpoint inserted at that address and thus
2437 read the breakpoint instead of returning the data saved in
2438 the breakpoint location's shadow contents. */
0d5ed153 2439 bl->target_info.reqstd_address = bl->address;
35df4500 2440 bl->target_info.placed_address_space = bl->pspace->aspace;
f1310107 2441 bl->target_info.length = bl->length;
8181d85f 2442
b775012e
LM
2443 /* When working with target-side conditions, we must pass all the conditions
2444 for the same breakpoint address down to the target since GDB will not
2445 insert those locations. With a list of breakpoint conditions, the target
2446 can decide when to stop and notify GDB. */
2447
2448 if (is_breakpoint (bl->owner))
2449 {
2450 build_target_condition_list (bl);
d3ce09f5
SS
2451 build_target_command_list (bl);
2452 /* Reset the modification marker. */
b775012e
LM
2453 bl->needs_update = 0;
2454 }
2455
35df4500
TJB
2456 if (bl->loc_type == bp_loc_software_breakpoint
2457 || bl->loc_type == bp_loc_hardware_breakpoint)
879bfdc2 2458 {
35df4500 2459 if (bl->owner->type != bp_hardware_breakpoint)
765dc015
VP
2460 {
2461 /* If the explicitly specified breakpoint type
2462 is not hardware breakpoint, check the memory map to see
2463 if the breakpoint address is in read only memory or not.
4a64f543 2464
765dc015
VP
2465 Two important cases are:
2466 - location type is not hardware breakpoint, memory
2467 is readonly. We change the type of the location to
2468 hardware breakpoint.
4a64f543
MS
2469 - location type is hardware breakpoint, memory is
2470 read-write. This means we've previously made the
2471 location hardware one, but then the memory map changed,
2472 so we undo.
765dc015 2473
4a64f543
MS
2474 When breakpoints are removed, remove_breakpoints will use
2475 location types we've just set here, the only possible
2476 problem is that memory map has changed during running
2477 program, but it's not going to work anyway with current
2478 gdb. */
765dc015 2479 struct mem_region *mr
0d5ed153 2480 = lookup_mem_region (bl->target_info.reqstd_address);
765dc015
VP
2481
2482 if (mr)
2483 {
2484 if (automatic_hardware_breakpoints)
2485 {
765dc015
VP
2486 enum bp_loc_type new_type;
2487
2488 if (mr->attrib.mode != MEM_RW)
2489 new_type = bp_loc_hardware_breakpoint;
2490 else
2491 new_type = bp_loc_software_breakpoint;
2492
35df4500 2493 if (new_type != bl->loc_type)
765dc015
VP
2494 {
2495 static int said = 0;
cc59ec59 2496
35df4500 2497 bl->loc_type = new_type;
765dc015
VP
2498 if (!said)
2499 {
3e43a32a
MS
2500 fprintf_filtered (gdb_stdout,
2501 _("Note: automatically using "
2502 "hardware breakpoints for "
2503 "read-only addresses.\n"));
765dc015
VP
2504 said = 1;
2505 }
2506 }
2507 }
35df4500 2508 else if (bl->loc_type == bp_loc_software_breakpoint
0fec99e8
PA
2509 && mr->attrib.mode != MEM_RW)
2510 {
2511 fprintf_unfiltered (tmp_error_stream,
2512 _("Cannot insert breakpoint %d.\n"
2513 "Cannot set software breakpoint "
2514 "at read-only address %s\n"),
2515 bl->owner->number,
2516 paddress (bl->gdbarch, bl->address));
2517 return 1;
2518 }
765dc015
VP
2519 }
2520 }
2521
879bfdc2
DJ
2522 /* First check to see if we have to handle an overlay. */
2523 if (overlay_debugging == ovly_off
35df4500
TJB
2524 || bl->section == NULL
2525 || !(section_is_overlay (bl->section)))
879bfdc2
DJ
2526 {
2527 /* No overlay handling: just set the breakpoint. */
a70b8144 2528 try
dd61ec5c 2529 {
0000e5cc
PA
2530 int val;
2531
dd61ec5c 2532 val = bl->owner->ops->insert_location (bl);
0000e5cc 2533 if (val)
688fca4f 2534 bp_excpt = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
dd61ec5c 2535 }
94aeb44b 2536 catch (gdb_exception &e)
dd61ec5c 2537 {
94aeb44b 2538 bp_excpt = std::move (e);
dd61ec5c 2539 }
879bfdc2
DJ
2540 }
2541 else
2542 {
4a64f543 2543 /* This breakpoint is in an overlay section.
879bfdc2
DJ
2544 Shall we set a breakpoint at the LMA? */
2545 if (!overlay_events_enabled)
2546 {
2547 /* Yes -- overlay event support is not active,
2548 so we must try to set a breakpoint at the LMA.
2549 This will not work for a hardware breakpoint. */
35df4500 2550 if (bl->loc_type == bp_loc_hardware_breakpoint)
8a3fe4f8 2551 warning (_("hardware breakpoint %d not supported in overlay!"),
35df4500 2552 bl->owner->number);
879bfdc2
DJ
2553 else
2554 {
35df4500
TJB
2555 CORE_ADDR addr = overlay_unmapped_address (bl->address,
2556 bl->section);
879bfdc2 2557 /* Set a software (trap) breakpoint at the LMA. */
35df4500 2558 bl->overlay_target_info = bl->target_info;
0d5ed153 2559 bl->overlay_target_info.reqstd_address = addr;
0000e5cc
PA
2560
2561 /* No overlay handling: just set the breakpoint. */
a70b8144 2562 try
0000e5cc
PA
2563 {
2564 int val;
2565
579c6ad9 2566 bl->overlay_target_info.kind
cd6c3b4f
YQ
2567 = breakpoint_kind (bl, &addr);
2568 bl->overlay_target_info.placed_address = addr;
0000e5cc
PA
2569 val = target_insert_breakpoint (bl->gdbarch,
2570 &bl->overlay_target_info);
2571 if (val)
688fca4f
PA
2572 bp_excpt
2573 = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
0000e5cc 2574 }
94aeb44b 2575 catch (gdb_exception &e)
0000e5cc 2576 {
94aeb44b 2577 bp_excpt = std::move (e);
0000e5cc
PA
2578 }
2579
688fca4f 2580 if (bp_excpt.reason != 0)
99361f52 2581 fprintf_unfiltered (tmp_error_stream,
3e43a32a
MS
2582 "Overlay breakpoint %d "
2583 "failed: in ROM?\n",
35df4500 2584 bl->owner->number);
879bfdc2
DJ
2585 }
2586 }
2587 /* Shall we set a breakpoint at the VMA? */
35df4500 2588 if (section_is_mapped (bl->section))
879bfdc2
DJ
2589 {
2590 /* Yes. This overlay section is mapped into memory. */
a70b8144 2591 try
dd61ec5c 2592 {
0000e5cc
PA
2593 int val;
2594
dd61ec5c 2595 val = bl->owner->ops->insert_location (bl);
0000e5cc 2596 if (val)
688fca4f 2597 bp_excpt = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
dd61ec5c 2598 }
94aeb44b 2599 catch (gdb_exception &e)
dd61ec5c 2600 {
94aeb44b 2601 bp_excpt = std::move (e);
dd61ec5c 2602 }
879bfdc2
DJ
2603 }
2604 else
2605 {
2606 /* No. This breakpoint will not be inserted.
2607 No error, but do not mark the bp as 'inserted'. */
2608 return 0;
2609 }
2610 }
2611
688fca4f 2612 if (bp_excpt.reason != 0)
879bfdc2
DJ
2613 {
2614 /* Can't set the breakpoint. */
0000e5cc
PA
2615
2616 /* In some cases, we might not be able to insert a
2617 breakpoint in a shared library that has already been
2618 removed, but we have not yet processed the shlib unload
2619 event. Unfortunately, some targets that implement
076855f9
PA
2620 breakpoint insertion themselves can't tell why the
2621 breakpoint insertion failed (e.g., the remote target
2622 doesn't define error codes), so we must treat generic
2623 errors as memory errors. */
688fca4f
PA
2624 if (bp_excpt.reason == RETURN_ERROR
2625 && (bp_excpt.error == GENERIC_ERROR
2626 || bp_excpt.error == MEMORY_ERROR)
076855f9 2627 && bl->loc_type == bp_loc_software_breakpoint
08351840 2628 && (solib_name_from_address (bl->pspace, bl->address)
d03de421
PA
2629 || shared_objfile_contains_address_p (bl->pspace,
2630 bl->address)))
879bfdc2 2631 {
4a64f543 2632 /* See also: disable_breakpoints_in_shlibs. */
35df4500 2633 bl->shlib_disabled = 1;
76727919 2634 gdb::observers::breakpoint_modified.notify (bl->owner);
3fbb6ffa
TJB
2635 if (!*disabled_breaks)
2636 {
2637 fprintf_unfiltered (tmp_error_stream,
2638 "Cannot insert breakpoint %d.\n",
2639 bl->owner->number);
2640 fprintf_unfiltered (tmp_error_stream,
2641 "Temporarily disabling shared "
2642 "library breakpoints:\n");
2643 }
2644 *disabled_breaks = 1;
879bfdc2 2645 fprintf_unfiltered (tmp_error_stream,
35df4500 2646 "breakpoint #%d\n", bl->owner->number);
0000e5cc 2647 return 0;
879bfdc2
DJ
2648 }
2649 else
879bfdc2 2650 {
35df4500 2651 if (bl->loc_type == bp_loc_hardware_breakpoint)
879bfdc2 2652 {
0000e5cc 2653 *hw_breakpoint_error = 1;
688fca4f 2654 *hw_bp_error_explained_already = bp_excpt.message != NULL;
dd61ec5c
MW
2655 fprintf_unfiltered (tmp_error_stream,
2656 "Cannot insert hardware breakpoint %d%s",
688fca4f
PA
2657 bl->owner->number,
2658 bp_excpt.message ? ":" : ".\n");
2659 if (bp_excpt.message != NULL)
2660 fprintf_unfiltered (tmp_error_stream, "%s.\n",
3d6e9d23 2661 bp_excpt.what ());
879bfdc2
DJ
2662 }
2663 else
2664 {
688fca4f 2665 if (bp_excpt.message == NULL)
0000e5cc 2666 {
1ccbe998 2667 std::string message
0000e5cc
PA
2668 = memory_error_message (TARGET_XFER_E_IO,
2669 bl->gdbarch, bl->address);
0000e5cc
PA
2670
2671 fprintf_unfiltered (tmp_error_stream,
2672 "Cannot insert breakpoint %d.\n"
2673 "%s\n",
1ccbe998 2674 bl->owner->number, message.c_str ());
0000e5cc
PA
2675 }
2676 else
2677 {
2678 fprintf_unfiltered (tmp_error_stream,
2679 "Cannot insert breakpoint %d: %s\n",
2680 bl->owner->number,
3d6e9d23 2681 bp_excpt.what ());
0000e5cc 2682 }
879bfdc2 2683 }
0000e5cc 2684 return 1;
879bfdc2
DJ
2685
2686 }
2687 }
2688 else
35df4500 2689 bl->inserted = 1;
879bfdc2 2690
0000e5cc 2691 return 0;
879bfdc2
DJ
2692 }
2693
35df4500 2694 else if (bl->loc_type == bp_loc_hardware_watchpoint
879bfdc2 2695 /* NOTE drow/2003-09-08: This state only exists for removing
4a64f543 2696 watchpoints. It's not clear that it's necessary... */
35df4500 2697 && bl->owner->disposition != disp_del_at_next_stop)
879bfdc2 2698 {
0000e5cc
PA
2699 int val;
2700
77b06cd7
TJB
2701 gdb_assert (bl->owner->ops != NULL
2702 && bl->owner->ops->insert_location != NULL);
2703
2704 val = bl->owner->ops->insert_location (bl);
85d721b8
PA
2705
2706 /* If trying to set a read-watchpoint, and it turns out it's not
2707 supported, try emulating one with an access watchpoint. */
35df4500 2708 if (val == 1 && bl->watchpoint_type == hw_read)
85d721b8
PA
2709 {
2710 struct bp_location *loc, **loc_temp;
2711
2712 /* But don't try to insert it, if there's already another
2713 hw_access location that would be considered a duplicate
2714 of this one. */
2715 ALL_BP_LOCATIONS (loc, loc_temp)
35df4500 2716 if (loc != bl
85d721b8 2717 && loc->watchpoint_type == hw_access
35df4500 2718 && watchpoint_locations_match (bl, loc))
85d721b8 2719 {
35df4500
TJB
2720 bl->duplicate = 1;
2721 bl->inserted = 1;
2722 bl->target_info = loc->target_info;
2723 bl->watchpoint_type = hw_access;
85d721b8
PA
2724 val = 0;
2725 break;
2726 }
2727
2728 if (val == 1)
2729 {
77b06cd7
TJB
2730 bl->watchpoint_type = hw_access;
2731 val = bl->owner->ops->insert_location (bl);
2732
2733 if (val)
2734 /* Back to the original value. */
2735 bl->watchpoint_type = hw_read;
85d721b8
PA
2736 }
2737 }
2738
35df4500 2739 bl->inserted = (val == 0);
879bfdc2
DJ
2740 }
2741
35df4500 2742 else if (bl->owner->type == bp_catchpoint)
879bfdc2 2743 {
0000e5cc
PA
2744 int val;
2745
77b06cd7
TJB
2746 gdb_assert (bl->owner->ops != NULL
2747 && bl->owner->ops->insert_location != NULL);
2748
2749 val = bl->owner->ops->insert_location (bl);
2750 if (val)
2751 {
2752 bl->owner->enable_state = bp_disabled;
2753
2754 if (val == 1)
2755 warning (_("\
2756Error inserting catchpoint %d: Your system does not support this type\n\
2757of catchpoint."), bl->owner->number);
2758 else
2759 warning (_("Error inserting catchpoint %d."), bl->owner->number);
2760 }
2761
2762 bl->inserted = (val == 0);
1640b821
DJ
2763
2764 /* We've already printed an error message if there was a problem
2765 inserting this catchpoint, and we've disabled the catchpoint,
2766 so just return success. */
2767 return 0;
879bfdc2
DJ
2768 }
2769
2770 return 0;
2771}
2772
6c95b8df
PA
2773/* This function is called when program space PSPACE is about to be
2774 deleted. It takes care of updating breakpoints to not reference
2775 PSPACE anymore. */
2776
2777void
2778breakpoint_program_space_exit (struct program_space *pspace)
2779{
2780 struct breakpoint *b, *b_temp;
876fa593 2781 struct bp_location *loc, **loc_temp;
6c95b8df
PA
2782
2783 /* Remove any breakpoint that was set through this program space. */
2784 ALL_BREAKPOINTS_SAFE (b, b_temp)
2785 {
2786 if (b->pspace == pspace)
2787 delete_breakpoint (b);
2788 }
2789
2790 /* Breakpoints set through other program spaces could have locations
2791 bound to PSPACE as well. Remove those. */
876fa593 2792 ALL_BP_LOCATIONS (loc, loc_temp)
6c95b8df
PA
2793 {
2794 struct bp_location *tmp;
2795
2796 if (loc->pspace == pspace)
2797 {
2bdf28a0 2798 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
6c95b8df
PA
2799 if (loc->owner->loc == loc)
2800 loc->owner->loc = loc->next;
2801 else
2802 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
2803 if (tmp->next == loc)
2804 {
2805 tmp->next = loc->next;
2806 break;
2807 }
2808 }
2809 }
2810
2811 /* Now update the global location list to permanently delete the
2812 removed locations above. */
44702360 2813 update_global_location_list (UGLL_DONT_INSERT);
6c95b8df
PA
2814}
2815
74960c60
VP
2816/* Make sure all breakpoints are inserted in inferior.
2817 Throws exception on any error.
2818 A breakpoint that is already inserted won't be inserted
2819 again, so calling this function twice is safe. */
2820void
2821insert_breakpoints (void)
2822{
2823 struct breakpoint *bpt;
2824
2825 ALL_BREAKPOINTS (bpt)
2826 if (is_hardware_watchpoint (bpt))
3a5c3e22
PA
2827 {
2828 struct watchpoint *w = (struct watchpoint *) bpt;
2829
2830 update_watchpoint (w, 0 /* don't reparse. */);
2831 }
74960c60 2832
04086b45
PA
2833 /* Updating watchpoints creates new locations, so update the global
2834 location list. Explicitly tell ugll to insert locations and
2835 ignore breakpoints_always_inserted_mode. */
2836 update_global_location_list (UGLL_INSERT);
74960c60
VP
2837}
2838
20388dd6
YQ
2839/* Invoke CALLBACK for each of bp_location. */
2840
2841void
2842iterate_over_bp_locations (walk_bp_location_callback callback)
2843{
2844 struct bp_location *loc, **loc_tmp;
2845
2846 ALL_BP_LOCATIONS (loc, loc_tmp)
2847 {
2848 callback (loc, NULL);
2849 }
2850}
2851
b775012e
LM
2852/* This is used when we need to synch breakpoint conditions between GDB and the
2853 target. It is the case with deleting and disabling of breakpoints when using
2854 always-inserted mode. */
2855
2856static void
2857update_inserted_breakpoint_locations (void)
2858{
2859 struct bp_location *bl, **blp_tmp;
2860 int error_flag = 0;
2861 int val = 0;
2862 int disabled_breaks = 0;
2863 int hw_breakpoint_error = 0;
dd61ec5c 2864 int hw_bp_details_reported = 0;
b775012e 2865
d7e74731 2866 string_file tmp_error_stream;
b775012e
LM
2867
2868 /* Explicitly mark the warning -- this will only be printed if
2869 there was an error. */
d7e74731 2870 tmp_error_stream.puts ("Warning:\n");
b775012e 2871
5ed8105e 2872 scoped_restore_current_pspace_and_thread restore_pspace_thread;
b775012e
LM
2873
2874 ALL_BP_LOCATIONS (bl, blp_tmp)
2875 {
2876 /* We only want to update software breakpoints and hardware
2877 breakpoints. */
2878 if (!is_breakpoint (bl->owner))
2879 continue;
2880
2881 /* We only want to update locations that are already inserted
2882 and need updating. This is to avoid unwanted insertion during
2883 deletion of breakpoints. */
4daf1902 2884 if (!bl->inserted || !bl->needs_update)
b775012e
LM
2885 continue;
2886
2887 switch_to_program_space_and_thread (bl->pspace);
2888
2889 /* For targets that support global breakpoints, there's no need
2890 to select an inferior to insert breakpoint to. In fact, even
2891 if we aren't attached to any process yet, we should still
2892 insert breakpoints. */
f5656ead 2893 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
d7e15655 2894 && inferior_ptid == null_ptid)
b775012e
LM
2895 continue;
2896
d7e74731 2897 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
dd61ec5c 2898 &hw_breakpoint_error, &hw_bp_details_reported);
b775012e
LM
2899 if (val)
2900 error_flag = val;
2901 }
2902
2903 if (error_flag)
2904 {
223ffa71 2905 target_terminal::ours_for_output ();
b775012e
LM
2906 error_stream (tmp_error_stream);
2907 }
b775012e
LM
2908}
2909
c30eee59 2910/* Used when starting or continuing the program. */
c906108c 2911
74960c60
VP
2912static void
2913insert_breakpoint_locations (void)
c906108c 2914{
a5606eee 2915 struct breakpoint *bpt;
35df4500 2916 struct bp_location *bl, **blp_tmp;
eacd795a 2917 int error_flag = 0;
c906108c 2918 int val = 0;
3fbb6ffa 2919 int disabled_breaks = 0;
81d0cc19 2920 int hw_breakpoint_error = 0;
dd61ec5c 2921 int hw_bp_error_explained_already = 0;
c906108c 2922
d7e74731
PA
2923 string_file tmp_error_stream;
2924
81d0cc19
GS
2925 /* Explicitly mark the warning -- this will only be printed if
2926 there was an error. */
d7e74731 2927 tmp_error_stream.puts ("Warning:\n");
6c95b8df 2928
5ed8105e 2929 scoped_restore_current_pspace_and_thread restore_pspace_thread;
6c95b8df 2930
35df4500 2931 ALL_BP_LOCATIONS (bl, blp_tmp)
879bfdc2 2932 {
b775012e 2933 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
879bfdc2
DJ
2934 continue;
2935
4a64f543
MS
2936 /* There is no point inserting thread-specific breakpoints if
2937 the thread no longer exists. ALL_BP_LOCATIONS bp_location
2938 has BL->OWNER always non-NULL. */
35df4500 2939 if (bl->owner->thread != -1
5d5658a1 2940 && !valid_global_thread_id (bl->owner->thread))
f365de73
AS
2941 continue;
2942
35df4500 2943 switch_to_program_space_and_thread (bl->pspace);
6c95b8df
PA
2944
2945 /* For targets that support global breakpoints, there's no need
2946 to select an inferior to insert breakpoint to. In fact, even
2947 if we aren't attached to any process yet, we should still
2948 insert breakpoints. */
f5656ead 2949 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
d7e15655 2950 && inferior_ptid == null_ptid)
6c95b8df
PA
2951 continue;
2952
d7e74731 2953 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
dd61ec5c 2954 &hw_breakpoint_error, &hw_bp_error_explained_already);
879bfdc2 2955 if (val)
eacd795a 2956 error_flag = val;
879bfdc2 2957 }
c906108c 2958
4a64f543
MS
2959 /* If we failed to insert all locations of a watchpoint, remove
2960 them, as half-inserted watchpoint is of limited use. */
a5606eee
VP
2961 ALL_BREAKPOINTS (bpt)
2962 {
2963 int some_failed = 0;
2964 struct bp_location *loc;
2965
2966 if (!is_hardware_watchpoint (bpt))
2967 continue;
2968
d6b74ac4 2969 if (!breakpoint_enabled (bpt))
a5606eee 2970 continue;
74960c60
VP
2971
2972 if (bpt->disposition == disp_del_at_next_stop)
2973 continue;
a5606eee
VP
2974
2975 for (loc = bpt->loc; loc; loc = loc->next)
56710373 2976 if (!loc->inserted && should_be_inserted (loc))
a5606eee
VP
2977 {
2978 some_failed = 1;
2979 break;
2980 }
2981 if (some_failed)
2982 {
2983 for (loc = bpt->loc; loc; loc = loc->next)
2984 if (loc->inserted)
834c0d03 2985 remove_breakpoint (loc);
a5606eee
VP
2986
2987 hw_breakpoint_error = 1;
d7e74731
PA
2988 tmp_error_stream.printf ("Could not insert "
2989 "hardware watchpoint %d.\n",
2990 bpt->number);
eacd795a 2991 error_flag = -1;
a5606eee
VP
2992 }
2993 }
2994
eacd795a 2995 if (error_flag)
81d0cc19
GS
2996 {
2997 /* If a hardware breakpoint or watchpoint was inserted, add a
2998 message about possibly exhausted resources. */
dd61ec5c 2999 if (hw_breakpoint_error && !hw_bp_error_explained_already)
81d0cc19 3000 {
d7e74731 3001 tmp_error_stream.printf ("Could not insert hardware breakpoints:\n\
c6510018 3002You may have requested too many hardware breakpoints/watchpoints.\n");
81d0cc19 3003 }
223ffa71 3004 target_terminal::ours_for_output ();
81d0cc19
GS
3005 error_stream (tmp_error_stream);
3006 }
c906108c
SS
3007}
3008
c30eee59
TJB
3009/* Used when the program stops.
3010 Returns zero if successful, or non-zero if there was a problem
3011 removing a breakpoint location. */
3012
c906108c 3013int
fba45db2 3014remove_breakpoints (void)
c906108c 3015{
35df4500 3016 struct bp_location *bl, **blp_tmp;
3a1bae8e 3017 int val = 0;
c906108c 3018
35df4500 3019 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3020 {
1e4d1764 3021 if (bl->inserted && !is_tracepoint (bl->owner))
834c0d03 3022 val |= remove_breakpoint (bl);
c5aa993b 3023 }
3a1bae8e 3024 return val;
c906108c
SS
3025}
3026
49fa26b0
PA
3027/* When a thread exits, remove breakpoints that are related to
3028 that thread. */
3029
3030static void
3031remove_threaded_breakpoints (struct thread_info *tp, int silent)
3032{
3033 struct breakpoint *b, *b_tmp;
3034
3035 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3036 {
5d5658a1 3037 if (b->thread == tp->global_num && user_breakpoint_p (b))
49fa26b0
PA
3038 {
3039 b->disposition = disp_del_at_next_stop;
3040
3041 printf_filtered (_("\
43792cf0
PA
3042Thread-specific breakpoint %d deleted - thread %s no longer in the thread list.\n"),
3043 b->number, print_thread_id (tp));
49fa26b0
PA
3044
3045 /* Hide it from the user. */
3046 b->number = 0;
3047 }
3048 }
3049}
3050
f3869b1a 3051/* See breakpoint.h. */
6c95b8df 3052
f3869b1a 3053void
00431a78 3054remove_breakpoints_inf (inferior *inf)
6c95b8df 3055{
35df4500 3056 struct bp_location *bl, **blp_tmp;
6c95b8df 3057 int val;
6c95b8df 3058
35df4500 3059 ALL_BP_LOCATIONS (bl, blp_tmp)
6c95b8df 3060 {
35df4500 3061 if (bl->pspace != inf->pspace)
6c95b8df
PA
3062 continue;
3063
fc126975 3064 if (bl->inserted && !bl->target_info.persist)
6c95b8df 3065 {
834c0d03 3066 val = remove_breakpoint (bl);
6c95b8df 3067 if (val != 0)
f3869b1a 3068 return;
6c95b8df
PA
3069 }
3070 }
6c95b8df
PA
3071}
3072
e58b0e63
PA
3073static int internal_breakpoint_number = -1;
3074
84f4c1fe
PM
3075/* Set the breakpoint number of B, depending on the value of INTERNAL.
3076 If INTERNAL is non-zero, the breakpoint number will be populated
3077 from internal_breakpoint_number and that variable decremented.
e5dd4106 3078 Otherwise the breakpoint number will be populated from
84f4c1fe
PM
3079 breakpoint_count and that value incremented. Internal breakpoints
3080 do not set the internal var bpnum. */
3081static void
3082set_breakpoint_number (int internal, struct breakpoint *b)
3083{
3084 if (internal)
3085 b->number = internal_breakpoint_number--;
3086 else
3087 {
3088 set_breakpoint_count (breakpoint_count + 1);
3089 b->number = breakpoint_count;
3090 }
3091}
3092
e62c965a 3093static struct breakpoint *
a6d9a66e 3094create_internal_breakpoint (struct gdbarch *gdbarch,
06edf0c0 3095 CORE_ADDR address, enum bptype type,
c0a91b2b 3096 const struct breakpoint_ops *ops)
e62c965a 3097{
51abb421 3098 symtab_and_line sal;
e62c965a
PP
3099 sal.pc = address;
3100 sal.section = find_pc_overlay (sal.pc);
6c95b8df 3101 sal.pspace = current_program_space;
e62c965a 3102
51abb421 3103 breakpoint *b = set_raw_breakpoint (gdbarch, sal, type, ops);
e62c965a
PP
3104 b->number = internal_breakpoint_number--;
3105 b->disposition = disp_donttouch;
3106
3107 return b;
3108}
3109
17450429
PP
3110static const char *const longjmp_names[] =
3111 {
3112 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
3113 };
3114#define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
3115
3116/* Per-objfile data private to breakpoint.c. */
3117struct breakpoint_objfile_data
3118{
3119 /* Minimal symbol for "_ovly_debug_event" (if any). */
43dce439 3120 struct bound_minimal_symbol overlay_msym {};
17450429
PP
3121
3122 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
43dce439 3123 struct bound_minimal_symbol longjmp_msym[NUM_LONGJMP_NAMES] {};
17450429 3124
28106bc2 3125 /* True if we have looked for longjmp probes. */
43dce439 3126 int longjmp_searched = 0;
28106bc2 3127
45461e0d
SM
3128 /* SystemTap probe points for longjmp (if any). These are non-owning
3129 references. */
3130 std::vector<probe *> longjmp_probes;
28106bc2 3131
17450429 3132 /* Minimal symbol for "std::terminate()" (if any). */
43dce439 3133 struct bound_minimal_symbol terminate_msym {};
17450429
PP
3134
3135 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
43dce439 3136 struct bound_minimal_symbol exception_msym {};
28106bc2
SDJ
3137
3138 /* True if we have looked for exception probes. */
43dce439 3139 int exception_searched = 0;
28106bc2 3140
45461e0d
SM
3141 /* SystemTap probe points for unwinding (if any). These are non-owning
3142 references. */
3143 std::vector<probe *> exception_probes;
17450429
PP
3144};
3145
51d3063a
TT
3146static const struct objfile_key<breakpoint_objfile_data>
3147 breakpoint_objfile_key;
17450429
PP
3148
3149/* Minimal symbol not found sentinel. */
3150static struct minimal_symbol msym_not_found;
3151
3152/* Returns TRUE if MSYM point to the "not found" sentinel. */
3153
3154static int
3155msym_not_found_p (const struct minimal_symbol *msym)
3156{
3157 return msym == &msym_not_found;
3158}
3159
3160/* Return per-objfile data needed by breakpoint.c.
3161 Allocate the data if necessary. */
3162
3163static struct breakpoint_objfile_data *
3164get_breakpoint_objfile_data (struct objfile *objfile)
3165{
3166 struct breakpoint_objfile_data *bp_objfile_data;
3167
51d3063a 3168 bp_objfile_data = breakpoint_objfile_key.get (objfile);
17450429 3169 if (bp_objfile_data == NULL)
51d3063a 3170 bp_objfile_data = breakpoint_objfile_key.emplace (objfile);
17450429
PP
3171 return bp_objfile_data;
3172}
3173
e62c965a 3174static void
af02033e 3175create_overlay_event_breakpoint (void)
e62c965a 3176{
af02033e 3177 const char *const func_name = "_ovly_debug_event";
e62c965a 3178
2030c079 3179 for (objfile *objfile : current_program_space->objfiles ())
69de3c6a
PP
3180 {
3181 struct breakpoint *b;
17450429
PP
3182 struct breakpoint_objfile_data *bp_objfile_data;
3183 CORE_ADDR addr;
67994074 3184 struct explicit_location explicit_loc;
69de3c6a 3185
17450429
PP
3186 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3187
3b7344d5 3188 if (msym_not_found_p (bp_objfile_data->overlay_msym.minsym))
17450429
PP
3189 continue;
3190
3b7344d5 3191 if (bp_objfile_data->overlay_msym.minsym == NULL)
17450429 3192 {
3b7344d5 3193 struct bound_minimal_symbol m;
17450429
PP
3194
3195 m = lookup_minimal_symbol_text (func_name, objfile);
3b7344d5 3196 if (m.minsym == NULL)
17450429
PP
3197 {
3198 /* Avoid future lookups in this objfile. */
3b7344d5 3199 bp_objfile_data->overlay_msym.minsym = &msym_not_found;
17450429
PP
3200 continue;
3201 }
3202 bp_objfile_data->overlay_msym = m;
3203 }
e62c965a 3204
77e371c0 3205 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->overlay_msym);
17450429 3206 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
06edf0c0
PA
3207 bp_overlay_event,
3208 &internal_breakpoint_ops);
67994074
KS
3209 initialize_explicit_location (&explicit_loc);
3210 explicit_loc.function_name = ASTRDUP (func_name);
d28cd78a 3211 b->location = new_explicit_location (&explicit_loc);
e62c965a 3212
69de3c6a
PP
3213 if (overlay_debugging == ovly_auto)
3214 {
3215 b->enable_state = bp_enabled;
3216 overlay_events_enabled = 1;
3217 }
3218 else
3219 {
3220 b->enable_state = bp_disabled;
3221 overlay_events_enabled = 0;
3222 }
e62c965a 3223 }
e62c965a
PP
3224}
3225
0fd8e87f 3226static void
af02033e 3227create_longjmp_master_breakpoint (void)
0fd8e87f 3228{
6c95b8df 3229 struct program_space *pspace;
6c95b8df 3230
5ed8105e 3231 scoped_restore_current_program_space restore_pspace;
0fd8e87f 3232
6c95b8df 3233 ALL_PSPACES (pspace)
af02033e 3234 {
af02033e
PP
3235 set_current_program_space (pspace);
3236
2030c079 3237 for (objfile *objfile : current_program_space->objfiles ())
aed57c53
TT
3238 {
3239 int i;
3240 struct gdbarch *gdbarch;
3241 struct breakpoint_objfile_data *bp_objfile_data;
0fd8e87f 3242
aed57c53 3243 gdbarch = get_objfile_arch (objfile);
0fd8e87f 3244
aed57c53 3245 bp_objfile_data = get_breakpoint_objfile_data (objfile);
17450429 3246
aed57c53
TT
3247 if (!bp_objfile_data->longjmp_searched)
3248 {
3249 std::vector<probe *> ret
3250 = find_probes_in_objfile (objfile, "libc", "longjmp");
25f9533e 3251
aed57c53
TT
3252 if (!ret.empty ())
3253 {
3254 /* We are only interested in checking one element. */
3255 probe *p = ret[0];
3256
3257 if (!p->can_evaluate_arguments ())
3258 {
3259 /* We cannot use the probe interface here, because it does
3260 not know how to evaluate arguments. */
3261 ret.clear ();
3262 }
3263 }
3264 bp_objfile_data->longjmp_probes = ret;
3265 bp_objfile_data->longjmp_searched = 1;
3266 }
25f9533e 3267
aed57c53
TT
3268 if (!bp_objfile_data->longjmp_probes.empty ())
3269 {
3270 for (probe *p : bp_objfile_data->longjmp_probes)
3271 {
3272 struct breakpoint *b;
3273
3274 b = create_internal_breakpoint (gdbarch,
3275 p->get_relocated_address (objfile),
3276 bp_longjmp_master,
3277 &internal_breakpoint_ops);
3278 b->location = new_probe_location ("-probe-stap libc:longjmp");
3279 b->enable_state = bp_disabled;
3280 }
28106bc2 3281
aed57c53
TT
3282 continue;
3283 }
28106bc2 3284
aed57c53 3285 if (!gdbarch_get_longjmp_target_p (gdbarch))
28106bc2 3286 continue;
28106bc2 3287
aed57c53
TT
3288 for (i = 0; i < NUM_LONGJMP_NAMES; i++)
3289 {
3290 struct breakpoint *b;
3291 const char *func_name;
3292 CORE_ADDR addr;
3293 struct explicit_location explicit_loc;
0fd8e87f 3294
aed57c53
TT
3295 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i].minsym))
3296 continue;
17450429 3297
aed57c53
TT
3298 func_name = longjmp_names[i];
3299 if (bp_objfile_data->longjmp_msym[i].minsym == NULL)
3300 {
3301 struct bound_minimal_symbol m;
3302
3303 m = lookup_minimal_symbol_text (func_name, objfile);
3304 if (m.minsym == NULL)
3305 {
3306 /* Prevent future lookups in this objfile. */
3307 bp_objfile_data->longjmp_msym[i].minsym = &msym_not_found;
3308 continue;
3309 }
3310 bp_objfile_data->longjmp_msym[i] = m;
3311 }
17450429 3312
aed57c53
TT
3313 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->longjmp_msym[i]);
3314 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master,
3315 &internal_breakpoint_ops);
3316 initialize_explicit_location (&explicit_loc);
3317 explicit_loc.function_name = ASTRDUP (func_name);
3318 b->location = new_explicit_location (&explicit_loc);
3319 b->enable_state = bp_disabled;
3320 }
3321 }
af02033e 3322 }
0fd8e87f
UW
3323}
3324
af02033e 3325/* Create a master std::terminate breakpoint. */
aa7d318d 3326static void
af02033e 3327create_std_terminate_master_breakpoint (void)
aa7d318d
TT
3328{
3329 struct program_space *pspace;
af02033e 3330 const char *const func_name = "std::terminate()";
aa7d318d 3331
5ed8105e 3332 scoped_restore_current_program_space restore_pspace;
aa7d318d
TT
3333
3334 ALL_PSPACES (pspace)
17450429 3335 {
17450429
PP
3336 CORE_ADDR addr;
3337
3338 set_current_program_space (pspace);
3339
2030c079 3340 for (objfile *objfile : current_program_space->objfiles ())
aed57c53
TT
3341 {
3342 struct breakpoint *b;
3343 struct breakpoint_objfile_data *bp_objfile_data;
3344 struct explicit_location explicit_loc;
aa7d318d 3345
aed57c53 3346 bp_objfile_data = get_breakpoint_objfile_data (objfile);
aa7d318d 3347
aed57c53
TT
3348 if (msym_not_found_p (bp_objfile_data->terminate_msym.minsym))
3349 continue;
17450429 3350
aed57c53
TT
3351 if (bp_objfile_data->terminate_msym.minsym == NULL)
3352 {
3353 struct bound_minimal_symbol m;
17450429 3354
aed57c53
TT
3355 m = lookup_minimal_symbol (func_name, NULL, objfile);
3356 if (m.minsym == NULL || (MSYMBOL_TYPE (m.minsym) != mst_text
3357 && MSYMBOL_TYPE (m.minsym) != mst_file_text))
3358 {
3359 /* Prevent future lookups in this objfile. */
3360 bp_objfile_data->terminate_msym.minsym = &msym_not_found;
3361 continue;
3362 }
3363 bp_objfile_data->terminate_msym = m;
3364 }
aa7d318d 3365
aed57c53
TT
3366 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->terminate_msym);
3367 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
3368 bp_std_terminate_master,
3369 &internal_breakpoint_ops);
3370 initialize_explicit_location (&explicit_loc);
3371 explicit_loc.function_name = ASTRDUP (func_name);
3372 b->location = new_explicit_location (&explicit_loc);
3373 b->enable_state = bp_disabled;
3374 }
17450429 3375 }
aa7d318d
TT
3376}
3377
186c406b
TT
3378/* Install a master breakpoint on the unwinder's debug hook. */
3379
70221824 3380static void
186c406b
TT
3381create_exception_master_breakpoint (void)
3382{
17450429 3383 const char *const func_name = "_Unwind_DebugHook";
186c406b 3384
2030c079 3385 for (objfile *objfile : current_program_space->objfiles ())
186c406b 3386 {
17450429
PP
3387 struct breakpoint *b;
3388 struct gdbarch *gdbarch;
3389 struct breakpoint_objfile_data *bp_objfile_data;
3390 CORE_ADDR addr;
67994074 3391 struct explicit_location explicit_loc;
17450429
PP
3392
3393 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3394
28106bc2
SDJ
3395 /* We prefer the SystemTap probe point if it exists. */
3396 if (!bp_objfile_data->exception_searched)
3397 {
45461e0d
SM
3398 std::vector<probe *> ret
3399 = find_probes_in_objfile (objfile, "libgcc", "unwind");
25f9533e 3400
45461e0d 3401 if (!ret.empty ())
25f9533e
SDJ
3402 {
3403 /* We are only interested in checking one element. */
45461e0d 3404 probe *p = ret[0];
25f9533e 3405
935676c9 3406 if (!p->can_evaluate_arguments ())
25f9533e
SDJ
3407 {
3408 /* We cannot use the probe interface here, because it does
3409 not know how to evaluate arguments. */
45461e0d 3410 ret.clear ();
25f9533e
SDJ
3411 }
3412 }
3413 bp_objfile_data->exception_probes = ret;
28106bc2
SDJ
3414 bp_objfile_data->exception_searched = 1;
3415 }
3416
45461e0d 3417 if (!bp_objfile_data->exception_probes.empty ())
28106bc2 3418 {
b926417a 3419 gdbarch = get_objfile_arch (objfile);
45461e0d
SM
3420
3421 for (probe *p : bp_objfile_data->exception_probes)
28106bc2 3422 {
729662a5 3423 b = create_internal_breakpoint (gdbarch,
935676c9 3424 p->get_relocated_address (objfile),
28106bc2
SDJ
3425 bp_exception_master,
3426 &internal_breakpoint_ops);
d28cd78a 3427 b->location = new_probe_location ("-probe-stap libgcc:unwind");
28106bc2
SDJ
3428 b->enable_state = bp_disabled;
3429 }
3430
3431 continue;
3432 }
3433
3434 /* Otherwise, try the hook function. */
3435
3b7344d5 3436 if (msym_not_found_p (bp_objfile_data->exception_msym.minsym))
17450429
PP
3437 continue;
3438
3439 gdbarch = get_objfile_arch (objfile);
186c406b 3440
3b7344d5 3441 if (bp_objfile_data->exception_msym.minsym == NULL)
186c406b 3442 {
3b7344d5 3443 struct bound_minimal_symbol debug_hook;
186c406b 3444
17450429 3445 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
3b7344d5 3446 if (debug_hook.minsym == NULL)
17450429 3447 {
3b7344d5 3448 bp_objfile_data->exception_msym.minsym = &msym_not_found;
17450429
PP
3449 continue;
3450 }
3451
3452 bp_objfile_data->exception_msym = debug_hook;
186c406b 3453 }
17450429 3454
77e371c0 3455 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->exception_msym);
8b88a78e
PA
3456 addr = gdbarch_convert_from_func_ptr_addr (gdbarch, addr,
3457 current_top_target ());
06edf0c0
PA
3458 b = create_internal_breakpoint (gdbarch, addr, bp_exception_master,
3459 &internal_breakpoint_ops);
67994074
KS
3460 initialize_explicit_location (&explicit_loc);
3461 explicit_loc.function_name = ASTRDUP (func_name);
d28cd78a 3462 b->location = new_explicit_location (&explicit_loc);
17450429 3463 b->enable_state = bp_disabled;
186c406b 3464 }
186c406b
TT
3465}
3466
9ef9e6a6
KS
3467/* Does B have a location spec? */
3468
3469static int
3470breakpoint_event_location_empty_p (const struct breakpoint *b)
3471{
d28cd78a 3472 return b->location != NULL && event_location_empty_p (b->location.get ());
9ef9e6a6
KS
3473}
3474
c906108c 3475void
fba45db2 3476update_breakpoints_after_exec (void)
c906108c 3477{
35df4500 3478 struct breakpoint *b, *b_tmp;
876fa593 3479 struct bp_location *bploc, **bplocp_tmp;
c906108c 3480
25b22b0a
PA
3481 /* We're about to delete breakpoints from GDB's lists. If the
3482 INSERTED flag is true, GDB will try to lift the breakpoints by
3483 writing the breakpoints' "shadow contents" back into memory. The
3484 "shadow contents" are NOT valid after an exec, so GDB should not
3485 do that. Instead, the target is responsible from marking
3486 breakpoints out as soon as it detects an exec. We don't do that
3487 here instead, because there may be other attempts to delete
3488 breakpoints after detecting an exec and before reaching here. */
876fa593 3489 ALL_BP_LOCATIONS (bploc, bplocp_tmp)
6c95b8df
PA
3490 if (bploc->pspace == current_program_space)
3491 gdb_assert (!bploc->inserted);
c906108c 3492
35df4500 3493 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 3494 {
6c95b8df
PA
3495 if (b->pspace != current_program_space)
3496 continue;
3497
4a64f543 3498 /* Solib breakpoints must be explicitly reset after an exec(). */
c5aa993b
JM
3499 if (b->type == bp_shlib_event)
3500 {
3501 delete_breakpoint (b);
3502 continue;
3503 }
c906108c 3504
4a64f543 3505 /* JIT breakpoints must be explicitly reset after an exec(). */
4efc6507
DE
3506 if (b->type == bp_jit_event)
3507 {
3508 delete_breakpoint (b);
3509 continue;
3510 }
3511
1900040c 3512 /* Thread event breakpoints must be set anew after an exec(),
0fd8e87f
UW
3513 as must overlay event and longjmp master breakpoints. */
3514 if (b->type == bp_thread_event || b->type == bp_overlay_event
186c406b
TT
3515 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
3516 || b->type == bp_exception_master)
c4093a6a
JM
3517 {
3518 delete_breakpoint (b);
3519 continue;
3520 }
3521
4a64f543 3522 /* Step-resume breakpoints are meaningless after an exec(). */
2c03e5be 3523 if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
c5aa993b
JM
3524 {
3525 delete_breakpoint (b);
3526 continue;
3527 }
3528
7c16b83e
PA
3529 /* Just like single-step breakpoints. */
3530 if (b->type == bp_single_step)
3531 {
3532 delete_breakpoint (b);
3533 continue;
3534 }
3535
611c83ae
PA
3536 /* Longjmp and longjmp-resume breakpoints are also meaningless
3537 after an exec. */
186c406b 3538 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
e2e4d78b 3539 || b->type == bp_longjmp_call_dummy
186c406b 3540 || b->type == bp_exception || b->type == bp_exception_resume)
611c83ae
PA
3541 {
3542 delete_breakpoint (b);
3543 continue;
3544 }
3545
ce78b96d
JB
3546 if (b->type == bp_catchpoint)
3547 {
3548 /* For now, none of the bp_catchpoint breakpoints need to
3549 do anything at this point. In the future, if some of
3550 the catchpoints need to something, we will need to add
3551 a new method, and call this method from here. */
3552 continue;
3553 }
3554
c5aa993b
JM
3555 /* bp_finish is a special case. The only way we ought to be able
3556 to see one of these when an exec() has happened, is if the user
3557 caught a vfork, and then said "finish". Ordinarily a finish just
3558 carries them to the call-site of the current callee, by setting
3559 a temporary bp there and resuming. But in this case, the finish
3560 will carry them entirely through the vfork & exec.
3561
3562 We don't want to allow a bp_finish to remain inserted now. But
3563 we can't safely delete it, 'cause finish_command has a handle to
3564 the bp on a bpstat, and will later want to delete it. There's a
3565 chance (and I've seen it happen) that if we delete the bp_finish
3566 here, that its storage will get reused by the time finish_command
3567 gets 'round to deleting the "use to be a bp_finish" breakpoint.
3568 We really must allow finish_command to delete a bp_finish.
3569
e5dd4106 3570 In the absence of a general solution for the "how do we know
53a5351d
JM
3571 it's safe to delete something others may have handles to?"
3572 problem, what we'll do here is just uninsert the bp_finish, and
3573 let finish_command delete it.
3574
3575 (We know the bp_finish is "doomed" in the sense that it's
3576 momentary, and will be deleted as soon as finish_command sees
3577 the inferior stopped. So it doesn't matter that the bp's
3578 address is probably bogus in the new a.out, unlike e.g., the
3579 solib breakpoints.) */
c5aa993b 3580
c5aa993b
JM
3581 if (b->type == bp_finish)
3582 {
3583 continue;
3584 }
3585
3586 /* Without a symbolic address, we have little hope of the
3587 pre-exec() address meaning the same thing in the post-exec()
4a64f543 3588 a.out. */
9ef9e6a6 3589 if (breakpoint_event_location_empty_p (b))
c5aa993b
JM
3590 {
3591 delete_breakpoint (b);
3592 continue;
3593 }
c5aa993b 3594 }
c906108c
SS
3595}
3596
3597int
d80ee84f 3598detach_breakpoints (ptid_t ptid)
c906108c 3599{
35df4500 3600 struct bp_location *bl, **blp_tmp;
3a1bae8e 3601 int val = 0;
2989a365 3602 scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid);
6c95b8df 3603 struct inferior *inf = current_inferior ();
c5aa993b 3604
e99b03dc 3605 if (ptid.pid () == inferior_ptid.pid ())
8a3fe4f8 3606 error (_("Cannot detach breakpoints of inferior_ptid"));
c5aa993b 3607
6c95b8df 3608 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
d80ee84f 3609 inferior_ptid = ptid;
35df4500 3610 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3611 {
35df4500 3612 if (bl->pspace != inf->pspace)
6c95b8df
PA
3613 continue;
3614
bd9673a4
PW
3615 /* This function must physically remove breakpoints locations
3616 from the specified ptid, without modifying the breakpoint
3617 package's state. Locations of type bp_loc_other are only
3618 maintained at GDB side. So, there is no need to remove
3619 these bp_loc_other locations. Moreover, removing these
3620 would modify the breakpoint package's state. */
3621 if (bl->loc_type == bp_loc_other)
3622 continue;
3623
35df4500 3624 if (bl->inserted)
b2b6a7da 3625 val |= remove_breakpoint_1 (bl, DETACH_BREAKPOINT);
c5aa993b 3626 }
d03285ec 3627
3a1bae8e 3628 return val;
c906108c
SS
3629}
3630
35df4500 3631/* Remove the breakpoint location BL from the current address space.
6c95b8df
PA
3632 Note that this is used to detach breakpoints from a child fork.
3633 When we get here, the child isn't in the inferior list, and neither
3634 do we have objects to represent its address space --- we should
35df4500 3635 *not* look at bl->pspace->aspace here. */
6c95b8df 3636
c906108c 3637static int
b2b6a7da 3638remove_breakpoint_1 (struct bp_location *bl, enum remove_bp_reason reason)
c906108c
SS
3639{
3640 int val;
c5aa993b 3641
35df4500
TJB
3642 /* BL is never in moribund_locations by our callers. */
3643 gdb_assert (bl->owner != NULL);
2bdf28a0 3644
74960c60
VP
3645 /* The type of none suggests that owner is actually deleted.
3646 This should not ever happen. */
35df4500 3647 gdb_assert (bl->owner->type != bp_none);
0bde7532 3648
35df4500
TJB
3649 if (bl->loc_type == bp_loc_software_breakpoint
3650 || bl->loc_type == bp_loc_hardware_breakpoint)
c906108c 3651 {
c02f5703
MS
3652 /* "Normal" instruction breakpoint: either the standard
3653 trap-instruction bp (bp_breakpoint), or a
3654 bp_hardware_breakpoint. */
3655
3656 /* First check to see if we have to handle an overlay. */
3657 if (overlay_debugging == ovly_off
35df4500
TJB
3658 || bl->section == NULL
3659 || !(section_is_overlay (bl->section)))
c02f5703
MS
3660 {
3661 /* No overlay handling: just remove the breakpoint. */
08351840
PA
3662
3663 /* If we're trying to uninsert a memory breakpoint that we
3664 know is set in a dynamic object that is marked
3665 shlib_disabled, then either the dynamic object was
3666 removed with "remove-symbol-file" or with
3667 "nosharedlibrary". In the former case, we don't know
3668 whether another dynamic object might have loaded over the
3669 breakpoint's address -- the user might well let us know
3670 about it next with add-symbol-file (the whole point of
d03de421 3671 add-symbol-file is letting the user manually maintain a
08351840
PA
3672 list of dynamically loaded objects). If we have the
3673 breakpoint's shadow memory, that is, this is a software
3674 breakpoint managed by GDB, check whether the breakpoint
3675 is still inserted in memory, to avoid overwriting wrong
3676 code with stale saved shadow contents. Note that HW
3677 breakpoints don't have shadow memory, as they're
3678 implemented using a mechanism that is not dependent on
3679 being able to modify the target's memory, and as such
3680 they should always be removed. */
3681 if (bl->shlib_disabled
3682 && bl->target_info.shadow_len != 0
3683 && !memory_validate_breakpoint (bl->gdbarch, &bl->target_info))
3684 val = 0;
3685 else
73971819 3686 val = bl->owner->ops->remove_location (bl, reason);
c02f5703 3687 }
c906108c
SS
3688 else
3689 {
4a64f543 3690 /* This breakpoint is in an overlay section.
c02f5703
MS
3691 Did we set a breakpoint at the LMA? */
3692 if (!overlay_events_enabled)
3693 {
3694 /* Yes -- overlay event support is not active, so we
3695 should have set a breakpoint at the LMA. Remove it.
3696 */
c02f5703
MS
3697 /* Ignore any failures: if the LMA is in ROM, we will
3698 have already warned when we failed to insert it. */
35df4500
TJB
3699 if (bl->loc_type == bp_loc_hardware_breakpoint)
3700 target_remove_hw_breakpoint (bl->gdbarch,
3701 &bl->overlay_target_info);
c02f5703 3702 else
35df4500 3703 target_remove_breakpoint (bl->gdbarch,
73971819
PA
3704 &bl->overlay_target_info,
3705 reason);
c02f5703
MS
3706 }
3707 /* Did we set a breakpoint at the VMA?
3708 If so, we will have marked the breakpoint 'inserted'. */
35df4500 3709 if (bl->inserted)
c906108c 3710 {
c02f5703
MS
3711 /* Yes -- remove it. Previously we did not bother to
3712 remove the breakpoint if the section had been
3713 unmapped, but let's not rely on that being safe. We
3714 don't know what the overlay manager might do. */
aa67235e
UW
3715
3716 /* However, we should remove *software* breakpoints only
3717 if the section is still mapped, or else we overwrite
3718 wrong code with the saved shadow contents. */
348d480f
PA
3719 if (bl->loc_type == bp_loc_hardware_breakpoint
3720 || section_is_mapped (bl->section))
73971819 3721 val = bl->owner->ops->remove_location (bl, reason);
aa67235e
UW
3722 else
3723 val = 0;
c906108c 3724 }
c02f5703
MS
3725 else
3726 {
3727 /* No -- not inserted, so no need to remove. No error. */
3728 val = 0;
3729 }
c906108c 3730 }
879d1e6b 3731
08351840
PA
3732 /* In some cases, we might not be able to remove a breakpoint in
3733 a shared library that has already been removed, but we have
3734 not yet processed the shlib unload event. Similarly for an
3735 unloaded add-symbol-file object - the user might not yet have
3736 had the chance to remove-symbol-file it. shlib_disabled will
3737 be set if the library/object has already been removed, but
3738 the breakpoint hasn't been uninserted yet, e.g., after
3739 "nosharedlibrary" or "remove-symbol-file" with breakpoints
3740 always-inserted mode. */
076855f9 3741 if (val
08351840
PA
3742 && (bl->loc_type == bp_loc_software_breakpoint
3743 && (bl->shlib_disabled
3744 || solib_name_from_address (bl->pspace, bl->address)
d03de421
PA
3745 || shared_objfile_contains_address_p (bl->pspace,
3746 bl->address))))
879d1e6b
UW
3747 val = 0;
3748
c906108c
SS
3749 if (val)
3750 return val;
b2b6a7da 3751 bl->inserted = (reason == DETACH_BREAKPOINT);
c906108c 3752 }
35df4500 3753 else if (bl->loc_type == bp_loc_hardware_watchpoint)
c906108c 3754 {
77b06cd7
TJB
3755 gdb_assert (bl->owner->ops != NULL
3756 && bl->owner->ops->remove_location != NULL);
3757
b2b6a7da 3758 bl->inserted = (reason == DETACH_BREAKPOINT);
73971819 3759 bl->owner->ops->remove_location (bl, reason);
2e70b7b9 3760
c906108c 3761 /* Failure to remove any of the hardware watchpoints comes here. */
b2b6a7da 3762 if (reason == REMOVE_BREAKPOINT && bl->inserted)
8a3fe4f8 3763 warning (_("Could not remove hardware watchpoint %d."),
35df4500 3764 bl->owner->number);
c906108c 3765 }
35df4500
TJB
3766 else if (bl->owner->type == bp_catchpoint
3767 && breakpoint_enabled (bl->owner)
3768 && !bl->duplicate)
ce78b96d 3769 {
77b06cd7
TJB
3770 gdb_assert (bl->owner->ops != NULL
3771 && bl->owner->ops->remove_location != NULL);
ce78b96d 3772
73971819 3773 val = bl->owner->ops->remove_location (bl, reason);
ce78b96d
JB
3774 if (val)
3775 return val;
77b06cd7 3776
b2b6a7da 3777 bl->inserted = (reason == DETACH_BREAKPOINT);
ce78b96d 3778 }
c906108c
SS
3779
3780 return 0;
3781}
3782
6c95b8df 3783static int
834c0d03 3784remove_breakpoint (struct bp_location *bl)
6c95b8df 3785{
35df4500
TJB
3786 /* BL is never in moribund_locations by our callers. */
3787 gdb_assert (bl->owner != NULL);
2bdf28a0 3788
6c95b8df
PA
3789 /* The type of none suggests that owner is actually deleted.
3790 This should not ever happen. */
35df4500 3791 gdb_assert (bl->owner->type != bp_none);
6c95b8df 3792
5ed8105e 3793 scoped_restore_current_pspace_and_thread restore_pspace_thread;
6c95b8df 3794
35df4500 3795 switch_to_program_space_and_thread (bl->pspace);
6c95b8df 3796
5ed8105e 3797 return remove_breakpoint_1 (bl, REMOVE_BREAKPOINT);
6c95b8df
PA
3798}
3799
c906108c
SS
3800/* Clear the "inserted" flag in all breakpoints. */
3801
25b22b0a 3802void
fba45db2 3803mark_breakpoints_out (void)
c906108c 3804{
35df4500 3805 struct bp_location *bl, **blp_tmp;
c906108c 3806
35df4500 3807 ALL_BP_LOCATIONS (bl, blp_tmp)
66c4b3e8 3808 if (bl->pspace == current_program_space)
35df4500 3809 bl->inserted = 0;
c906108c
SS
3810}
3811
53a5351d
JM
3812/* Clear the "inserted" flag in all breakpoints and delete any
3813 breakpoints which should go away between runs of the program.
c906108c
SS
3814
3815 Plus other such housekeeping that has to be done for breakpoints
3816 between runs.
3817
53a5351d
JM
3818 Note: this function gets called at the end of a run (by
3819 generic_mourn_inferior) and when a run begins (by
4a64f543 3820 init_wait_for_inferior). */
c906108c
SS
3821
3822
3823
3824void
fba45db2 3825breakpoint_init_inferior (enum inf_context context)
c906108c 3826{
35df4500 3827 struct breakpoint *b, *b_tmp;
6c95b8df 3828 struct program_space *pspace = current_program_space;
c906108c 3829
50c71eaf
PA
3830 /* If breakpoint locations are shared across processes, then there's
3831 nothing to do. */
f5656ead 3832 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
50c71eaf
PA
3833 return;
3834
1a853c52 3835 mark_breakpoints_out ();
075f6582 3836
35df4500 3837 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 3838 {
6c95b8df
PA
3839 if (b->loc && b->loc->pspace != pspace)
3840 continue;
3841
c5aa993b
JM
3842 switch (b->type)
3843 {
3844 case bp_call_dummy:
e2e4d78b 3845 case bp_longjmp_call_dummy:
c906108c 3846
c5aa993b 3847 /* If the call dummy breakpoint is at the entry point it will
ab92d69b
PA
3848 cause problems when the inferior is rerun, so we better get
3849 rid of it. */
3850
3851 case bp_watchpoint_scope:
3852
3853 /* Also get rid of scope breakpoints. */
3854
3855 case bp_shlib_event:
3856
3857 /* Also remove solib event breakpoints. Their addresses may
3858 have changed since the last time we ran the program.
3859 Actually we may now be debugging against different target;
3860 and so the solib backend that installed this breakpoint may
3861 not be used in by the target. E.g.,
3862
3863 (gdb) file prog-linux
3864 (gdb) run # native linux target
3865 ...
3866 (gdb) kill
3867 (gdb) file prog-win.exe
3868 (gdb) tar rem :9999 # remote Windows gdbserver.
3869 */
c906108c 3870
f59f708a
PA
3871 case bp_step_resume:
3872
3873 /* Also remove step-resume breakpoints. */
3874
7c16b83e
PA
3875 case bp_single_step:
3876
3877 /* Also remove single-step breakpoints. */
3878
c5aa993b
JM
3879 delete_breakpoint (b);
3880 break;
c906108c 3881
c5aa993b
JM
3882 case bp_watchpoint:
3883 case bp_hardware_watchpoint:
3884 case bp_read_watchpoint:
3885 case bp_access_watchpoint:
3a5c3e22
PA
3886 {
3887 struct watchpoint *w = (struct watchpoint *) b;
c906108c 3888
3a5c3e22
PA
3889 /* Likewise for watchpoints on local expressions. */
3890 if (w->exp_valid_block != NULL)
3891 delete_breakpoint (b);
63000888 3892 else
3a5c3e22 3893 {
63000888
PA
3894 /* Get rid of existing locations, which are no longer
3895 valid. New ones will be created in
3896 update_watchpoint, when the inferior is restarted.
3897 The next update_global_location_list call will
3898 garbage collect them. */
3899 b->loc = NULL;
3900
3901 if (context == inf_starting)
3902 {
3903 /* Reset val field to force reread of starting value in
3904 insert_breakpoints. */
850645cf 3905 w->val.reset (nullptr);
4c1d86d9 3906 w->val_valid = false;
63000888
PA
3907 }
3908 }
3a5c3e22 3909 }
c5aa993b
JM
3910 break;
3911 default:
c5aa993b
JM
3912 break;
3913 }
3914 }
1c5cfe86
PA
3915
3916 /* Get rid of the moribund locations. */
1123588c 3917 for (bp_location *bl : moribund_locations)
35df4500 3918 decref_bp_location (&bl);
1123588c 3919 moribund_locations.clear ();
c906108c
SS
3920}
3921
6c95b8df
PA
3922/* These functions concern about actual breakpoints inserted in the
3923 target --- to e.g. check if we need to do decr_pc adjustment or if
3924 we need to hop over the bkpt --- so we check for address space
3925 match, not program space. */
3926
c2c6d25f
JM
3927/* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
3928 exists at PC. It returns ordinary_breakpoint_here if it's an
3929 ordinary breakpoint, or permanent_breakpoint_here if it's a
3930 permanent breakpoint.
3931 - When continuing from a location with an ordinary breakpoint, we
3932 actually single step once before calling insert_breakpoints.
e5dd4106 3933 - When continuing from a location with a permanent breakpoint, we
c2c6d25f
JM
3934 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
3935 the target, to advance the PC past the breakpoint. */
c906108c 3936
c2c6d25f 3937enum breakpoint_here
accd0bcd 3938breakpoint_here_p (const address_space *aspace, CORE_ADDR pc)
c906108c 3939{
35df4500 3940 struct bp_location *bl, **blp_tmp;
c2c6d25f 3941 int any_breakpoint_here = 0;
c906108c 3942
35df4500 3943 ALL_BP_LOCATIONS (bl, blp_tmp)
075f6582 3944 {
35df4500
TJB
3945 if (bl->loc_type != bp_loc_software_breakpoint
3946 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
3947 continue;
3948
f1310107 3949 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
35df4500 3950 if ((breakpoint_enabled (bl->owner)
1a853c52 3951 || bl->permanent)
f1310107 3952 && breakpoint_location_address_match (bl, aspace, pc))
075f6582
DJ
3953 {
3954 if (overlay_debugging
35df4500
TJB
3955 && section_is_overlay (bl->section)
3956 && !section_is_mapped (bl->section))
075f6582 3957 continue; /* unmapped overlay -- can't be a match */
1a853c52 3958 else if (bl->permanent)
075f6582
DJ
3959 return permanent_breakpoint_here;
3960 else
3961 any_breakpoint_here = 1;
3962 }
3963 }
c906108c 3964
f486487f 3965 return any_breakpoint_here ? ordinary_breakpoint_here : no_breakpoint_here;
c906108c
SS
3966}
3967
d35ae833
PA
3968/* See breakpoint.h. */
3969
3970int
accd0bcd 3971breakpoint_in_range_p (const address_space *aspace,
d35ae833
PA
3972 CORE_ADDR addr, ULONGEST len)
3973{
3974 struct bp_location *bl, **blp_tmp;
3975
3976 ALL_BP_LOCATIONS (bl, blp_tmp)
3977 {
3978 if (bl->loc_type != bp_loc_software_breakpoint
3979 && bl->loc_type != bp_loc_hardware_breakpoint)
3980 continue;
3981
3982 if ((breakpoint_enabled (bl->owner)
3983 || bl->permanent)
3984 && breakpoint_location_address_range_overlap (bl, aspace,
3985 addr, len))
3986 {
3987 if (overlay_debugging
3988 && section_is_overlay (bl->section)
3989 && !section_is_mapped (bl->section))
3990 {
3991 /* Unmapped overlay -- can't be a match. */
3992 continue;
3993 }
3994
3995 return 1;
3996 }
3997 }
3998
3999 return 0;
4000}
4001
1c5cfe86
PA
4002/* Return true if there's a moribund breakpoint at PC. */
4003
4004int
accd0bcd 4005moribund_breakpoint_here_p (const address_space *aspace, CORE_ADDR pc)
1c5cfe86 4006{
1123588c 4007 for (bp_location *loc : moribund_locations)
f1310107 4008 if (breakpoint_location_address_match (loc, aspace, pc))
1c5cfe86
PA
4009 return 1;
4010
4011 return 0;
4012}
c2c6d25f 4013
f7ce857f
PA
4014/* Returns non-zero iff BL is inserted at PC, in address space
4015 ASPACE. */
4016
4017static int
4018bp_location_inserted_here_p (struct bp_location *bl,
accd0bcd 4019 const address_space *aspace, CORE_ADDR pc)
f7ce857f
PA
4020{
4021 if (bl->inserted
4022 && breakpoint_address_match (bl->pspace->aspace, bl->address,
4023 aspace, pc))
4024 {
4025 if (overlay_debugging
4026 && section_is_overlay (bl->section)
4027 && !section_is_mapped (bl->section))
4028 return 0; /* unmapped overlay -- can't be a match */
4029 else
4030 return 1;
4031 }
4032 return 0;
4033}
4034
a1fd2fa5 4035/* Returns non-zero iff there's a breakpoint inserted at PC. */
c906108c
SS
4036
4037int
accd0bcd 4038breakpoint_inserted_here_p (const address_space *aspace, CORE_ADDR pc)
c906108c 4039{
f7ce857f 4040 struct bp_location **blp, **blp_tmp = NULL;
c906108c 4041
f7ce857f 4042 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
c5aa993b 4043 {
f7ce857f
PA
4044 struct bp_location *bl = *blp;
4045
35df4500
TJB
4046 if (bl->loc_type != bp_loc_software_breakpoint
4047 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
4048 continue;
4049
f7ce857f
PA
4050 if (bp_location_inserted_here_p (bl, aspace, pc))
4051 return 1;
c5aa993b 4052 }
c36b740a
VP
4053 return 0;
4054}
4055
a1fd2fa5
PA
4056/* This function returns non-zero iff there is a software breakpoint
4057 inserted at PC. */
c36b740a
VP
4058
4059int
accd0bcd 4060software_breakpoint_inserted_here_p (const address_space *aspace,
a1fd2fa5 4061 CORE_ADDR pc)
4fa8626c 4062{
f7ce857f 4063 struct bp_location **blp, **blp_tmp = NULL;
4fa8626c 4064
f7ce857f 4065 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4fa8626c 4066 {
f7ce857f
PA
4067 struct bp_location *bl = *blp;
4068
35df4500 4069 if (bl->loc_type != bp_loc_software_breakpoint)
4fa8626c
DJ
4070 continue;
4071
f7ce857f
PA
4072 if (bp_location_inserted_here_p (bl, aspace, pc))
4073 return 1;
4fa8626c
DJ
4074 }
4075
4076 return 0;
9c02b525
PA
4077}
4078
4079/* See breakpoint.h. */
4080
4081int
accd0bcd 4082hardware_breakpoint_inserted_here_p (const address_space *aspace,
9c02b525
PA
4083 CORE_ADDR pc)
4084{
4085 struct bp_location **blp, **blp_tmp = NULL;
9c02b525
PA
4086
4087 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4088 {
4089 struct bp_location *bl = *blp;
4090
4091 if (bl->loc_type != bp_loc_hardware_breakpoint)
4092 continue;
4093
4094 if (bp_location_inserted_here_p (bl, aspace, pc))
4095 return 1;
4096 }
4097
4098 return 0;
4fa8626c
DJ
4099}
4100
9093389c 4101int
accd0bcd 4102hardware_watchpoint_inserted_in_range (const address_space *aspace,
9093389c
PA
4103 CORE_ADDR addr, ULONGEST len)
4104{
4105 struct breakpoint *bpt;
4106
4107 ALL_BREAKPOINTS (bpt)
4108 {
4109 struct bp_location *loc;
4110
4111 if (bpt->type != bp_hardware_watchpoint
4112 && bpt->type != bp_access_watchpoint)
4113 continue;
4114
4115 if (!breakpoint_enabled (bpt))
4116 continue;
4117
4118 for (loc = bpt->loc; loc; loc = loc->next)
4119 if (loc->pspace->aspace == aspace && loc->inserted)
4120 {
4121 CORE_ADDR l, h;
4122
4123 /* Check for intersection. */
768adc05
PA
4124 l = std::max<CORE_ADDR> (loc->address, addr);
4125 h = std::min<CORE_ADDR> (loc->address + loc->length, addr + len);
9093389c
PA
4126 if (l < h)
4127 return 1;
4128 }
4129 }
4130 return 0;
4131}
c5aa993b 4132
f2478a7e 4133/* See breakpoint.h. */
c906108c 4134
f2478a7e
SM
4135bool
4136is_catchpoint (struct breakpoint *b)
c906108c 4137{
f2478a7e 4138 return (b->type == bp_catchpoint);
c906108c
SS
4139}
4140
f431efe5
PA
4141/* Frees any storage that is part of a bpstat. Does not walk the
4142 'next' chain. */
4143
04afa70c 4144bpstats::~bpstats ()
198757a8 4145{
04afa70c
TT
4146 if (bp_location_at != NULL)
4147 decref_bp_location (&bp_location_at);
198757a8
VP
4148}
4149
c906108c
SS
4150/* Clear a bpstat so that it says we are not at any breakpoint.
4151 Also free any storage that is part of a bpstat. */
4152
4153void
fba45db2 4154bpstat_clear (bpstat *bsp)
c906108c
SS
4155{
4156 bpstat p;
4157 bpstat q;
4158
4159 if (bsp == 0)
4160 return;
4161 p = *bsp;
4162 while (p != NULL)
4163 {
4164 q = p->next;
04afa70c 4165 delete p;
c906108c
SS
4166 p = q;
4167 }
4168 *bsp = NULL;
4169}
4170
04afa70c
TT
4171bpstats::bpstats (const bpstats &other)
4172 : next (NULL),
4173 bp_location_at (other.bp_location_at),
4174 breakpoint_at (other.breakpoint_at),
4175 commands (other.commands),
04afa70c
TT
4176 print (other.print),
4177 stop (other.stop),
4178 print_it (other.print_it)
4179{
850645cf
TT
4180 if (other.old_val != NULL)
4181 old_val = release_value (value_copy (other.old_val.get ()));
04afa70c 4182 incref_bp_location (bp_location_at);
04afa70c
TT
4183}
4184
c906108c
SS
4185/* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
4186 is part of the bpstat is copied as well. */
4187
4188bpstat
fba45db2 4189bpstat_copy (bpstat bs)
c906108c
SS
4190{
4191 bpstat p = NULL;
4192 bpstat tmp;
4193 bpstat retval = NULL;
4194
4195 if (bs == NULL)
4196 return bs;
4197
4198 for (; bs != NULL; bs = bs->next)
4199 {
04afa70c 4200 tmp = new bpstats (*bs);
31cc81e9 4201
c906108c
SS
4202 if (p == NULL)
4203 /* This is the first thing in the chain. */
4204 retval = tmp;
4205 else
4206 p->next = tmp;
4207 p = tmp;
4208 }
4209 p->next = NULL;
4210 return retval;
4211}
4212
4a64f543 4213/* Find the bpstat associated with this breakpoint. */
c906108c
SS
4214
4215bpstat
fba45db2 4216bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
c906108c 4217{
c5aa993b
JM
4218 if (bsp == NULL)
4219 return NULL;
c906108c 4220
c5aa993b
JM
4221 for (; bsp != NULL; bsp = bsp->next)
4222 {
f431efe5 4223 if (bsp->breakpoint_at == breakpoint)
c5aa993b
JM
4224 return bsp;
4225 }
c906108c
SS
4226 return NULL;
4227}
4228
ab04a2af
TT
4229/* See breakpoint.h. */
4230
4c462cb0 4231bool
427cd150 4232bpstat_explains_signal (bpstat bsp, enum gdb_signal sig)
ab04a2af 4233{
ab04a2af
TT
4234 for (; bsp != NULL; bsp = bsp->next)
4235 {
427cd150
TT
4236 if (bsp->breakpoint_at == NULL)
4237 {
4238 /* A moribund location can never explain a signal other than
4239 GDB_SIGNAL_TRAP. */
4240 if (sig == GDB_SIGNAL_TRAP)
4c462cb0 4241 return true;
427cd150
TT
4242 }
4243 else
47591c29
PA
4244 {
4245 if (bsp->breakpoint_at->ops->explains_signal (bsp->breakpoint_at,
4246 sig))
4c462cb0 4247 return true;
47591c29 4248 }
ab04a2af
TT
4249 }
4250
4c462cb0 4251 return false;
ab04a2af
TT
4252}
4253
4a64f543
MS
4254/* Put in *NUM the breakpoint number of the first breakpoint we are
4255 stopped at. *BSP upon return is a bpstat which points to the
4256 remaining breakpoints stopped at (but which is not guaranteed to be
4257 good for anything but further calls to bpstat_num).
4258
8671a17b
PA
4259 Return 0 if passed a bpstat which does not indicate any breakpoints.
4260 Return -1 if stopped at a breakpoint that has been deleted since
4261 we set it.
4262 Return 1 otherwise. */
c906108c
SS
4263
4264int
8671a17b 4265bpstat_num (bpstat *bsp, int *num)
c906108c
SS
4266{
4267 struct breakpoint *b;
4268
4269 if ((*bsp) == NULL)
4270 return 0; /* No more breakpoint values */
8671a17b 4271
4a64f543
MS
4272 /* We assume we'll never have several bpstats that correspond to a
4273 single breakpoint -- otherwise, this function might return the
4274 same number more than once and this will look ugly. */
f431efe5 4275 b = (*bsp)->breakpoint_at;
8671a17b
PA
4276 *bsp = (*bsp)->next;
4277 if (b == NULL)
4278 return -1; /* breakpoint that's been deleted since */
4279
4280 *num = b->number; /* We have its number */
4281 return 1;
c906108c
SS
4282}
4283
e93ca019 4284/* See breakpoint.h. */
c906108c
SS
4285
4286void
e93ca019 4287bpstat_clear_actions (void)
c906108c 4288{
e93ca019
JK
4289 bpstat bs;
4290
00431a78 4291 if (inferior_ptid == null_ptid)
e93ca019
JK
4292 return;
4293
00431a78 4294 thread_info *tp = inferior_thread ();
e93ca019 4295 for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
c906108c 4296 {
d1b0a7bf 4297 bs->commands = NULL;
850645cf 4298 bs->old_val.reset (nullptr);
c906108c
SS
4299 }
4300}
4301
f3b1572e
PA
4302/* Called when a command is about to proceed the inferior. */
4303
4304static void
4305breakpoint_about_to_proceed (void)
4306{
d7e15655 4307 if (inferior_ptid != null_ptid)
f3b1572e
PA
4308 {
4309 struct thread_info *tp = inferior_thread ();
4310
4311 /* Allow inferior function calls in breakpoint commands to not
4312 interrupt the command list. When the call finishes
4313 successfully, the inferior will be standing at the same
4314 breakpoint as if nothing happened. */
16c381f0 4315 if (tp->control.in_infcall)
f3b1572e
PA
4316 return;
4317 }
4318
4319 breakpoint_proceeded = 1;
4320}
4321
abf85f46
JK
4322/* Return non-zero iff CMD as the first line of a command sequence is `silent'
4323 or its equivalent. */
4324
4325static int
4326command_line_is_silent (struct command_line *cmd)
4327{
4f45d445 4328 return cmd && (strcmp ("silent", cmd->line) == 0);
abf85f46
JK
4329}
4330
4a64f543
MS
4331/* Execute all the commands associated with all the breakpoints at
4332 this location. Any of these commands could cause the process to
4333 proceed beyond this point, etc. We look out for such changes by
4334 checking the global "breakpoint_proceeded" after each command.
c906108c 4335
347bddb7
PA
4336 Returns true if a breakpoint command resumed the inferior. In that
4337 case, it is the caller's responsibility to recall it again with the
4338 bpstat of the current thread. */
4339
4340static int
4341bpstat_do_actions_1 (bpstat *bsp)
c906108c
SS
4342{
4343 bpstat bs;
347bddb7 4344 int again = 0;
c906108c
SS
4345
4346 /* Avoid endless recursion if a `source' command is contained
4347 in bs->commands. */
4348 if (executing_breakpoint_commands)
347bddb7 4349 return 0;
c906108c 4350
81b1e71c
TT
4351 scoped_restore save_executing
4352 = make_scoped_restore (&executing_breakpoint_commands, 1);
c906108c 4353
1ac32117 4354 scoped_restore preventer = prevent_dont_repeat ();
cf6c5ffb 4355
4a64f543 4356 /* This pointer will iterate over the list of bpstat's. */
c906108c
SS
4357 bs = *bsp;
4358
4359 breakpoint_proceeded = 0;
4360 for (; bs != NULL; bs = bs->next)
4361 {
d1b0a7bf 4362 struct command_line *cmd = NULL;
6c50ab1c
JB
4363
4364 /* Take ownership of the BSP's command tree, if it has one.
4365
4366 The command tree could legitimately contain commands like
4367 'step' and 'next', which call clear_proceed_status, which
4368 frees stop_bpstat's command tree. To make sure this doesn't
4369 free the tree we're executing out from under us, we need to
4370 take ownership of the tree ourselves. Since a given bpstat's
4371 commands are only executed once, we don't need to copy it; we
4372 can clear the pointer in the bpstat, and make sure we free
4373 the tree when we're done. */
d1b0a7bf 4374 counted_command_line ccmd = bs->commands;
9add0f1b 4375 bs->commands = NULL;
d1b0a7bf
TT
4376 if (ccmd != NULL)
4377 cmd = ccmd.get ();
abf85f46
JK
4378 if (command_line_is_silent (cmd))
4379 {
4380 /* The action has been already done by bpstat_stop_status. */
4381 cmd = cmd->next;
4382 }
6c50ab1c 4383
c906108c
SS
4384 while (cmd != NULL)
4385 {
4386 execute_control_command (cmd);
4387
4388 if (breakpoint_proceeded)
4389 break;
4390 else
4391 cmd = cmd->next;
4392 }
6c50ab1c 4393
c906108c 4394 if (breakpoint_proceeded)
32c1e744 4395 {
cb814510 4396 if (current_ui->async)
347bddb7
PA
4397 /* If we are in async mode, then the target might be still
4398 running, not stopped at any breakpoint, so nothing for
4399 us to do here -- just return to the event loop. */
4400 ;
32c1e744
VP
4401 else
4402 /* In sync mode, when execute_control_command returns
4403 we're already standing on the next breakpoint.
347bddb7
PA
4404 Breakpoint commands for that stop were not run, since
4405 execute_command does not run breakpoint commands --
4406 only command_line_handler does, but that one is not
4407 involved in execution of breakpoint commands. So, we
4408 can now execute breakpoint commands. It should be
4409 noted that making execute_command do bpstat actions is
4410 not an option -- in this case we'll have recursive
4411 invocation of bpstat for each breakpoint with a
4412 command, and can easily blow up GDB stack. Instead, we
4413 return true, which will trigger the caller to recall us
4414 with the new stop_bpstat. */
4415 again = 1;
4416 break;
32c1e744 4417 }
c906108c 4418 }
347bddb7
PA
4419 return again;
4420}
4421
00431a78
PA
4422/* Helper for bpstat_do_actions. Get the current thread, if there's
4423 one, is alive and has execution. Return NULL otherwise. */
4424
4425static thread_info *
4426get_bpstat_thread ()
4427{
4428 if (inferior_ptid == null_ptid || !target_has_execution)
4429 return NULL;
4430
4431 thread_info *tp = inferior_thread ();
4432 if (tp->state == THREAD_EXITED || tp->executing)
4433 return NULL;
4434 return tp;
4435}
4436
347bddb7
PA
4437void
4438bpstat_do_actions (void)
4439{
694c6bf5 4440 auto cleanup_if_error = make_scope_exit (bpstat_clear_actions);
00431a78 4441 thread_info *tp;
353d1d73 4442
347bddb7 4443 /* Do any commands attached to breakpoint we are stopped at. */
00431a78
PA
4444 while ((tp = get_bpstat_thread ()) != NULL)
4445 {
4446 /* Since in sync mode, bpstat_do_actions may resume the
4447 inferior, and only return when it is stopped at the next
4448 breakpoint, we keep doing breakpoint actions until it returns
4449 false to indicate the inferior was not resumed. */
4450 if (!bpstat_do_actions_1 (&tp->control.stop_bpstat))
4451 break;
4452 }
353d1d73 4453
694c6bf5 4454 cleanup_if_error.release ();
c906108c
SS
4455}
4456
fa4727a6
DJ
4457/* Print out the (old or new) value associated with a watchpoint. */
4458
4459static void
4460watchpoint_value_print (struct value *val, struct ui_file *stream)
4461{
4462 if (val == NULL)
7f6aba03 4463 fprintf_styled (stream, metadata_style.style (), _("<unreadable>"));
fa4727a6 4464 else
79a45b7d
TT
4465 {
4466 struct value_print_options opts;
4467 get_user_print_options (&opts);
4468 value_print (val, stream, &opts);
4469 }
fa4727a6
DJ
4470}
4471
f303dbd6
PA
4472/* Print the "Thread ID hit" part of "Thread ID hit Breakpoint N" if
4473 debugging multiple threads. */
4474
4475void
4476maybe_print_thread_hit_breakpoint (struct ui_out *uiout)
4477{
112e8700 4478 if (uiout->is_mi_like_p ())
f303dbd6
PA
4479 return;
4480
112e8700 4481 uiout->text ("\n");
f303dbd6
PA
4482
4483 if (show_thread_that_caused_stop ())
4484 {
4485 const char *name;
4486 struct thread_info *thr = inferior_thread ();
4487
112e8700 4488 uiout->text ("Thread ");
33eca680 4489 uiout->field_string ("thread-id", print_thread_id (thr));
f303dbd6
PA
4490
4491 name = thr->name != NULL ? thr->name : target_thread_name (thr);
4492 if (name != NULL)
4493 {
112e8700 4494 uiout->text (" \"");
33eca680 4495 uiout->field_string ("name", name);
112e8700 4496 uiout->text ("\"");
f303dbd6
PA
4497 }
4498
112e8700 4499 uiout->text (" hit ");
f303dbd6
PA
4500 }
4501}
4502
e514a9d6 4503/* Generic routine for printing messages indicating why we
4a64f543 4504 stopped. The behavior of this function depends on the value
e514a9d6
JM
4505 'print_it' in the bpstat structure. Under some circumstances we
4506 may decide not to print anything here and delegate the task to
4a64f543 4507 normal_stop(). */
e514a9d6
JM
4508
4509static enum print_stop_action
4510print_bp_stop_message (bpstat bs)
4511{
4512 switch (bs->print_it)
4513 {
4514 case print_it_noop:
4a64f543 4515 /* Nothing should be printed for this bpstat entry. */
e514a9d6
JM
4516 return PRINT_UNKNOWN;
4517 break;
4518
4519 case print_it_done:
4520 /* We still want to print the frame, but we already printed the
4a64f543 4521 relevant messages. */
e514a9d6
JM
4522 return PRINT_SRC_AND_LOC;
4523 break;
4524
4525 case print_it_normal:
4f8d1dc6 4526 {
f431efe5
PA
4527 struct breakpoint *b = bs->breakpoint_at;
4528
1a6a67de
TJB
4529 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
4530 which has since been deleted. */
4531 if (b == NULL)
4532 return PRINT_UNKNOWN;
4533
348d480f
PA
4534 /* Normal case. Call the breakpoint's print_it method. */
4535 return b->ops->print_it (bs);
4f8d1dc6 4536 }
348d480f 4537 break;
3086aeae 4538
e514a9d6 4539 default:
8e65ff28 4540 internal_error (__FILE__, __LINE__,
e2e0b3e5 4541 _("print_bp_stop_message: unrecognized enum value"));
e514a9d6 4542 break;
c906108c 4543 }
c906108c
SS
4544}
4545
edcc5120
TT
4546/* A helper function that prints a shared library stopped event. */
4547
4548static void
4549print_solib_event (int is_catchpoint)
4550{
6fb16ce6 4551 bool any_deleted = !current_program_space->deleted_solibs.empty ();
bcb430e4 4552 bool any_added = !current_program_space->added_solibs.empty ();
edcc5120
TT
4553
4554 if (!is_catchpoint)
4555 {
4556 if (any_added || any_deleted)
112e8700 4557 current_uiout->text (_("Stopped due to shared library event:\n"));
edcc5120 4558 else
112e8700
SM
4559 current_uiout->text (_("Stopped due to shared library event (no "
4560 "libraries added or removed)\n"));
edcc5120
TT
4561 }
4562
112e8700
SM
4563 if (current_uiout->is_mi_like_p ())
4564 current_uiout->field_string ("reason",
4565 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
edcc5120
TT
4566
4567 if (any_deleted)
4568 {
112e8700 4569 current_uiout->text (_(" Inferior unloaded "));
10f489e5 4570 ui_out_emit_list list_emitter (current_uiout, "removed");
6fb16ce6 4571 for (int ix = 0; ix < current_program_space->deleted_solibs.size (); ix++)
edcc5120 4572 {
6fb16ce6
SM
4573 const std::string &name = current_program_space->deleted_solibs[ix];
4574
edcc5120 4575 if (ix > 0)
112e8700
SM
4576 current_uiout->text (" ");
4577 current_uiout->field_string ("library", name);
4578 current_uiout->text ("\n");
edcc5120 4579 }
edcc5120
TT
4580 }
4581
4582 if (any_added)
4583 {
112e8700 4584 current_uiout->text (_(" Inferior loaded "));
10f489e5 4585 ui_out_emit_list list_emitter (current_uiout, "added");
bcb430e4 4586 bool first = true;
52941706 4587 for (so_list *iter : current_program_space->added_solibs)
edcc5120 4588 {
bcb430e4 4589 if (!first)
112e8700 4590 current_uiout->text (" ");
bcb430e4 4591 first = false;
112e8700
SM
4592 current_uiout->field_string ("library", iter->so_name);
4593 current_uiout->text ("\n");
edcc5120 4594 }
edcc5120
TT
4595 }
4596}
4597
e514a9d6
JM
4598/* Print a message indicating what happened. This is called from
4599 normal_stop(). The input to this routine is the head of the bpstat
36dfb11c
TT
4600 list - a list of the eventpoints that caused this stop. KIND is
4601 the target_waitkind for the stopping event. This
e514a9d6
JM
4602 routine calls the generic print routine for printing a message
4603 about reasons for stopping. This will print (for example) the
4604 "Breakpoint n," part of the output. The return value of this
4605 routine is one of:
c906108c 4606
4a64f543 4607 PRINT_UNKNOWN: Means we printed nothing.
917317f4 4608 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4a64f543 4609 code to print the location. An example is
c5aa993b
JM
4610 "Breakpoint 1, " which should be followed by
4611 the location.
917317f4 4612 PRINT_SRC_ONLY: Means we printed something, but there is no need
c5aa993b
JM
4613 to also print the location part of the message.
4614 An example is the catch/throw messages, which
4a64f543 4615 don't require a location appended to the end.
917317f4 4616 PRINT_NOTHING: We have done some printing and we don't need any
4a64f543 4617 further info to be printed. */
c906108c 4618
917317f4 4619enum print_stop_action
36dfb11c 4620bpstat_print (bpstat bs, int kind)
c906108c 4621{
f486487f 4622 enum print_stop_action val;
c5aa993b 4623
c906108c 4624 /* Maybe another breakpoint in the chain caused us to stop.
53a5351d
JM
4625 (Currently all watchpoints go on the bpstat whether hit or not.
4626 That probably could (should) be changed, provided care is taken
c906108c 4627 with respect to bpstat_explains_signal). */
e514a9d6
JM
4628 for (; bs; bs = bs->next)
4629 {
4630 val = print_bp_stop_message (bs);
4631 if (val == PRINT_SRC_ONLY
4632 || val == PRINT_SRC_AND_LOC
4633 || val == PRINT_NOTHING)
4634 return val;
4635 }
c906108c 4636
36dfb11c
TT
4637 /* If we had hit a shared library event breakpoint,
4638 print_bp_stop_message would print out this message. If we hit an
4639 OS-level shared library event, do the same thing. */
4640 if (kind == TARGET_WAITKIND_LOADED)
4641 {
edcc5120 4642 print_solib_event (0);
36dfb11c
TT
4643 return PRINT_NOTHING;
4644 }
4645
e514a9d6 4646 /* We reached the end of the chain, or we got a null BS to start
4a64f543 4647 with and nothing was printed. */
917317f4 4648 return PRINT_UNKNOWN;
c906108c
SS
4649}
4650
bf469271 4651/* Evaluate the boolean expression EXP and return the result. */
c906108c 4652
bf469271
PA
4653static bool
4654breakpoint_cond_eval (expression *exp)
c906108c 4655{
278cd55f 4656 struct value *mark = value_mark ();
bf469271 4657 bool res = value_true (evaluate_expression (exp));
cc59ec59 4658
c906108c 4659 value_free_to_mark (mark);
bf469271 4660 return res;
c906108c
SS
4661}
4662
5760d0ab 4663/* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
c906108c 4664
04afa70c
TT
4665bpstats::bpstats (struct bp_location *bl, bpstat **bs_link_pointer)
4666 : next (NULL),
4667 bp_location_at (bl),
4668 breakpoint_at (bl->owner),
4669 commands (NULL),
04afa70c
TT
4670 print (0),
4671 stop (0),
4672 print_it (print_it_normal)
c906108c 4673{
f431efe5 4674 incref_bp_location (bl);
04afa70c
TT
4675 **bs_link_pointer = this;
4676 *bs_link_pointer = &next;
4677}
4678
4679bpstats::bpstats ()
4680 : next (NULL),
4681 bp_location_at (NULL),
4682 breakpoint_at (NULL),
4683 commands (NULL),
04afa70c
TT
4684 print (0),
4685 stop (0),
4686 print_it (print_it_normal)
4687{
c906108c
SS
4688}
4689\f
d983da9c
DJ
4690/* The target has stopped with waitstatus WS. Check if any hardware
4691 watchpoints have triggered, according to the target. */
4692
4693int
4694watchpoints_triggered (struct target_waitstatus *ws)
4695{
57810aa7 4696 bool stopped_by_watchpoint = target_stopped_by_watchpoint ();
d983da9c
DJ
4697 CORE_ADDR addr;
4698 struct breakpoint *b;
4699
4700 if (!stopped_by_watchpoint)
4701 {
4702 /* We were not stopped by a watchpoint. Mark all watchpoints
4703 as not triggered. */
4704 ALL_BREAKPOINTS (b)
cc60f2e3 4705 if (is_hardware_watchpoint (b))
3a5c3e22
PA
4706 {
4707 struct watchpoint *w = (struct watchpoint *) b;
4708
4709 w->watchpoint_triggered = watch_triggered_no;
4710 }
d983da9c
DJ
4711
4712 return 0;
4713 }
4714
8b88a78e 4715 if (!target_stopped_data_address (current_top_target (), &addr))
d983da9c
DJ
4716 {
4717 /* We were stopped by a watchpoint, but we don't know where.
4718 Mark all watchpoints as unknown. */
4719 ALL_BREAKPOINTS (b)
cc60f2e3 4720 if (is_hardware_watchpoint (b))
3a5c3e22
PA
4721 {
4722 struct watchpoint *w = (struct watchpoint *) b;
4723
4724 w->watchpoint_triggered = watch_triggered_unknown;
4725 }
d983da9c 4726
3c4797ba 4727 return 1;
d983da9c
DJ
4728 }
4729
4730 /* The target could report the data address. Mark watchpoints
4731 affected by this data address as triggered, and all others as not
4732 triggered. */
4733
4734 ALL_BREAKPOINTS (b)
cc60f2e3 4735 if (is_hardware_watchpoint (b))
d983da9c 4736 {
3a5c3e22 4737 struct watchpoint *w = (struct watchpoint *) b;
a5606eee 4738 struct bp_location *loc;
d983da9c 4739
3a5c3e22 4740 w->watchpoint_triggered = watch_triggered_no;
a5606eee 4741 for (loc = b->loc; loc; loc = loc->next)
9c06b0b4 4742 {
3a5c3e22 4743 if (is_masked_watchpoint (b))
9c06b0b4 4744 {
3a5c3e22
PA
4745 CORE_ADDR newaddr = addr & w->hw_wp_mask;
4746 CORE_ADDR start = loc->address & w->hw_wp_mask;
9c06b0b4
TJB
4747
4748 if (newaddr == start)
4749 {
3a5c3e22 4750 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
4751 break;
4752 }
4753 }
4754 /* Exact match not required. Within range is sufficient. */
8b88a78e 4755 else if (target_watchpoint_addr_within_range (current_top_target (),
9c06b0b4
TJB
4756 addr, loc->address,
4757 loc->length))
4758 {
3a5c3e22 4759 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
4760 break;
4761 }
4762 }
d983da9c
DJ
4763 }
4764
4765 return 1;
4766}
4767
bf469271
PA
4768/* Possible return values for watchpoint_check. */
4769enum wp_check_result
4770 {
4771 /* The watchpoint has been deleted. */
4772 WP_DELETED = 1,
4773
4774 /* The value has changed. */
4775 WP_VALUE_CHANGED = 2,
4776
4777 /* The value has not changed. */
4778 WP_VALUE_NOT_CHANGED = 3,
4779
4780 /* Ignore this watchpoint, no matter if the value changed or not. */
4781 WP_IGNORE = 4,
4782 };
c906108c
SS
4783
4784#define BP_TEMPFLAG 1
4785#define BP_HARDWAREFLAG 2
4786
4a64f543 4787/* Evaluate watchpoint condition expression and check if its value
bf469271 4788 changed. */
553e4c11 4789
bf469271
PA
4790static wp_check_result
4791watchpoint_check (bpstat bs)
c906108c 4792{
3a5c3e22 4793 struct watchpoint *b;
c906108c
SS
4794 struct frame_info *fr;
4795 int within_current_scope;
4796
f431efe5 4797 /* BS is built from an existing struct breakpoint. */
2bdf28a0 4798 gdb_assert (bs->breakpoint_at != NULL);
3a5c3e22 4799 b = (struct watchpoint *) bs->breakpoint_at;
d0fb5eae 4800
f6bc2008
PA
4801 /* If this is a local watchpoint, we only want to check if the
4802 watchpoint frame is in scope if the current thread is the thread
4803 that was used to create the watchpoint. */
4804 if (!watchpoint_in_thread_scope (b))
60e1c644 4805 return WP_IGNORE;
f6bc2008 4806
c906108c
SS
4807 if (b->exp_valid_block == NULL)
4808 within_current_scope = 1;
4809 else
4810 {
edb3359d
DJ
4811 struct frame_info *frame = get_current_frame ();
4812 struct gdbarch *frame_arch = get_frame_arch (frame);
4813 CORE_ADDR frame_pc = get_frame_pc (frame);
4814
c9cf6e20 4815 /* stack_frame_destroyed_p() returns a non-zero value if we're
4a64f543
MS
4816 still in the function but the stack frame has already been
4817 invalidated. Since we can't rely on the values of local
4818 variables after the stack has been destroyed, we are treating
4819 the watchpoint in that state as `not changed' without further
4820 checking. Don't mark watchpoints as changed if the current
4821 frame is in an epilogue - even if they are in some other
4822 frame, our view of the stack is likely to be wrong and
4823 frame_find_by_id could error out. */
c9cf6e20 4824 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
60e1c644 4825 return WP_IGNORE;
a0f49112 4826
101dcfbe 4827 fr = frame_find_by_id (b->watchpoint_frame);
c906108c 4828 within_current_scope = (fr != NULL);
69fbadd5
DJ
4829
4830 /* If we've gotten confused in the unwinder, we might have
4831 returned a frame that can't describe this variable. */
edb3359d
DJ
4832 if (within_current_scope)
4833 {
4834 struct symbol *function;
4835
4836 function = get_frame_function (fr);
4837 if (function == NULL
4838 || !contained_in (b->exp_valid_block,
4839 SYMBOL_BLOCK_VALUE (function)))
4840 within_current_scope = 0;
4841 }
69fbadd5 4842
edb3359d 4843 if (within_current_scope)
c906108c
SS
4844 /* If we end up stopping, the current frame will get selected
4845 in normal_stop. So this call to select_frame won't affect
4846 the user. */
0f7d239c 4847 select_frame (fr);
c906108c 4848 }
c5aa993b 4849
c906108c
SS
4850 if (within_current_scope)
4851 {
4a64f543
MS
4852 /* We use value_{,free_to_}mark because it could be a *long*
4853 time before we return to the command level and call
4854 free_all_values. We can't call free_all_values because we
4855 might be in the middle of evaluating a function call. */
c906108c 4856
0cf6dd15 4857 int pc = 0;
9c06b0b4 4858 struct value *mark;
fa4727a6
DJ
4859 struct value *new_val;
4860
c1fc2657 4861 if (is_masked_watchpoint (b))
9c06b0b4
TJB
4862 /* Since we don't know the exact trigger address (from
4863 stopped_data_address), just tell the user we've triggered
4864 a mask watchpoint. */
4865 return WP_VALUE_CHANGED;
4866
4867 mark = value_mark ();
4d01a485 4868 fetch_subexp_value (b->exp.get (), &pc, &new_val, NULL, NULL, 0);
218d2fc6 4869
bb9d5f81
PP
4870 if (b->val_bitsize != 0)
4871 new_val = extract_bitfield_from_watchpoint_value (b, new_val);
4872
4a64f543
MS
4873 /* We use value_equal_contents instead of value_equal because
4874 the latter coerces an array to a pointer, thus comparing just
4875 the address of the array instead of its contents. This is
4876 not what we want. */
fa4727a6 4877 if ((b->val != NULL) != (new_val != NULL)
850645cf
TT
4878 || (b->val != NULL && !value_equal_contents (b->val.get (),
4879 new_val)))
c906108c 4880 {
c906108c 4881 bs->old_val = b->val;
850645cf 4882 b->val = release_value (new_val);
4c1d86d9 4883 b->val_valid = true;
850645cf
TT
4884 if (new_val != NULL)
4885 value_free_to_mark (mark);
c906108c
SS
4886 return WP_VALUE_CHANGED;
4887 }
4888 else
4889 {
60e1c644 4890 /* Nothing changed. */
c906108c 4891 value_free_to_mark (mark);
c906108c
SS
4892 return WP_VALUE_NOT_CHANGED;
4893 }
4894 }
4895 else
4896 {
4897 /* This seems like the only logical thing to do because
c5aa993b
JM
4898 if we temporarily ignored the watchpoint, then when
4899 we reenter the block in which it is valid it contains
4900 garbage (in the case of a function, it may have two
4901 garbage values, one before and one after the prologue).
4902 So we can't even detect the first assignment to it and
4903 watch after that (since the garbage may or may not equal
4904 the first value assigned). */
348d480f
PA
4905 /* We print all the stop information in
4906 breakpoint_ops->print_it, but in this case, by the time we
4907 call breakpoint_ops->print_it this bp will be deleted
4908 already. So we have no choice but print the information
4909 here. */
468afe6c 4910
0e454242 4911 SWITCH_THRU_ALL_UIS ()
468afe6c
PA
4912 {
4913 struct ui_out *uiout = current_uiout;
4914
112e8700
SM
4915 if (uiout->is_mi_like_p ())
4916 uiout->field_string
4917 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
6a831f06
PA
4918 uiout->message ("\nWatchpoint %pF deleted because the program has "
4919 "left the block in\n"
4920 "which its expression is valid.\n",
4921 signed_field ("wpnum", b->number));
468afe6c 4922 }
4ce44c66 4923
cdac0397 4924 /* Make sure the watchpoint's commands aren't executed. */
d1b0a7bf 4925 b->commands = NULL;
d0fb5eae 4926 watchpoint_del_at_next_stop (b);
c906108c
SS
4927
4928 return WP_DELETED;
4929 }
4930}
4931
18a18393 4932/* Return true if it looks like target has stopped due to hitting
348d480f
PA
4933 breakpoint location BL. This function does not check if we should
4934 stop, only if BL explains the stop. */
4935
18a18393 4936static int
6c95b8df 4937bpstat_check_location (const struct bp_location *bl,
accd0bcd 4938 const address_space *aspace, CORE_ADDR bp_addr,
09ac7c10 4939 const struct target_waitstatus *ws)
18a18393
VP
4940{
4941 struct breakpoint *b = bl->owner;
4942
348d480f 4943 /* BL is from an existing breakpoint. */
2bdf28a0
JK
4944 gdb_assert (b != NULL);
4945
bd522513 4946 return b->ops->breakpoint_hit (bl, aspace, bp_addr, ws);
18a18393
VP
4947}
4948
3a5c3e22
PA
4949/* Determine if the watched values have actually changed, and we
4950 should stop. If not, set BS->stop to 0. */
4951
18a18393
VP
4952static void
4953bpstat_check_watchpoint (bpstat bs)
4954{
2bdf28a0 4955 const struct bp_location *bl;
3a5c3e22 4956 struct watchpoint *b;
2bdf28a0
JK
4957
4958 /* BS is built for existing struct breakpoint. */
f431efe5 4959 bl = bs->bp_location_at;
2bdf28a0 4960 gdb_assert (bl != NULL);
3a5c3e22 4961 b = (struct watchpoint *) bs->breakpoint_at;
2bdf28a0 4962 gdb_assert (b != NULL);
18a18393 4963
18a18393 4964 {
18a18393
VP
4965 int must_check_value = 0;
4966
c1fc2657 4967 if (b->type == bp_watchpoint)
18a18393
VP
4968 /* For a software watchpoint, we must always check the
4969 watched value. */
4970 must_check_value = 1;
4971 else if (b->watchpoint_triggered == watch_triggered_yes)
4972 /* We have a hardware watchpoint (read, write, or access)
4973 and the target earlier reported an address watched by
4974 this watchpoint. */
4975 must_check_value = 1;
4976 else if (b->watchpoint_triggered == watch_triggered_unknown
c1fc2657 4977 && b->type == bp_hardware_watchpoint)
18a18393
VP
4978 /* We were stopped by a hardware watchpoint, but the target could
4979 not report the data address. We must check the watchpoint's
4980 value. Access and read watchpoints are out of luck; without
4981 a data address, we can't figure it out. */
4982 must_check_value = 1;
3a5c3e22 4983
18a18393
VP
4984 if (must_check_value)
4985 {
bf469271
PA
4986 wp_check_result e;
4987
a70b8144 4988 try
bf469271
PA
4989 {
4990 e = watchpoint_check (bs);
4991 }
230d2906 4992 catch (const gdb_exception &ex)
bf469271
PA
4993 {
4994 exception_fprintf (gdb_stderr, ex,
4995 "Error evaluating expression "
4996 "for watchpoint %d\n",
4997 b->number);
4998
4999 SWITCH_THRU_ALL_UIS ()
5000 {
5001 printf_filtered (_("Watchpoint %d deleted.\n"),
5002 b->number);
5003 }
5004 watchpoint_del_at_next_stop (b);
5005 e = WP_DELETED;
5006 }
bf469271 5007
18a18393
VP
5008 switch (e)
5009 {
5010 case WP_DELETED:
5011 /* We've already printed what needs to be printed. */
5012 bs->print_it = print_it_done;
5013 /* Stop. */
5014 break;
60e1c644
PA
5015 case WP_IGNORE:
5016 bs->print_it = print_it_noop;
5017 bs->stop = 0;
5018 break;
18a18393 5019 case WP_VALUE_CHANGED:
c1fc2657 5020 if (b->type == bp_read_watchpoint)
18a18393 5021 {
85d721b8
PA
5022 /* There are two cases to consider here:
5023
4a64f543 5024 1. We're watching the triggered memory for reads.
85d721b8
PA
5025 In that case, trust the target, and always report
5026 the watchpoint hit to the user. Even though
5027 reads don't cause value changes, the value may
5028 have changed since the last time it was read, and
5029 since we're not trapping writes, we will not see
5030 those, and as such we should ignore our notion of
5031 old value.
5032
4a64f543 5033 2. We're watching the triggered memory for both
85d721b8
PA
5034 reads and writes. There are two ways this may
5035 happen:
5036
4a64f543 5037 2.1. This is a target that can't break on data
85d721b8
PA
5038 reads only, but can break on accesses (reads or
5039 writes), such as e.g., x86. We detect this case
5040 at the time we try to insert read watchpoints.
5041
4a64f543 5042 2.2. Otherwise, the target supports read
85d721b8
PA
5043 watchpoints, but, the user set an access or write
5044 watchpoint watching the same memory as this read
5045 watchpoint.
5046
5047 If we're watching memory writes as well as reads,
5048 ignore watchpoint hits when we find that the
5049 value hasn't changed, as reads don't cause
5050 changes. This still gives false positives when
5051 the program writes the same value to memory as
5052 what there was already in memory (we will confuse
5053 it for a read), but it's much better than
5054 nothing. */
5055
5056 int other_write_watchpoint = 0;
5057
5058 if (bl->watchpoint_type == hw_read)
5059 {
5060 struct breakpoint *other_b;
5061
5062 ALL_BREAKPOINTS (other_b)
3a5c3e22
PA
5063 if (other_b->type == bp_hardware_watchpoint
5064 || other_b->type == bp_access_watchpoint)
85d721b8 5065 {
3a5c3e22
PA
5066 struct watchpoint *other_w =
5067 (struct watchpoint *) other_b;
5068
5069 if (other_w->watchpoint_triggered
5070 == watch_triggered_yes)
5071 {
5072 other_write_watchpoint = 1;
5073 break;
5074 }
85d721b8
PA
5075 }
5076 }
5077
5078 if (other_write_watchpoint
5079 || bl->watchpoint_type == hw_access)
5080 {
5081 /* We're watching the same memory for writes,
5082 and the value changed since the last time we
5083 updated it, so this trap must be for a write.
5084 Ignore it. */
5085 bs->print_it = print_it_noop;
5086 bs->stop = 0;
5087 }
18a18393
VP
5088 }
5089 break;
5090 case WP_VALUE_NOT_CHANGED:
c1fc2657
SM
5091 if (b->type == bp_hardware_watchpoint
5092 || b->type == bp_watchpoint)
18a18393
VP
5093 {
5094 /* Don't stop: write watchpoints shouldn't fire if
5095 the value hasn't changed. */
5096 bs->print_it = print_it_noop;
5097 bs->stop = 0;
5098 }
5099 /* Stop. */
5100 break;
5101 default:
5102 /* Can't happen. */
18a18393
VP
5103 break;
5104 }
5105 }
5106 else /* must_check_value == 0 */
5107 {
5108 /* This is a case where some watchpoint(s) triggered, but
5109 not at the address of this watchpoint, or else no
5110 watchpoint triggered after all. So don't print
5111 anything for this watchpoint. */
5112 bs->print_it = print_it_noop;
5113 bs->stop = 0;
5114 }
5115 }
5116}
5117
7d4df6a4
DE
5118/* For breakpoints that are currently marked as telling gdb to stop,
5119 check conditions (condition proper, frame, thread and ignore count)
18a18393
VP
5120 of breakpoint referred to by BS. If we should not stop for this
5121 breakpoint, set BS->stop to 0. */
f431efe5 5122
18a18393 5123static void
00431a78 5124bpstat_check_breakpoint_conditions (bpstat bs, thread_info *thread)
18a18393 5125{
2bdf28a0
JK
5126 const struct bp_location *bl;
5127 struct breakpoint *b;
bf469271
PA
5128 /* Assume stop. */
5129 bool condition_result = true;
7d4df6a4
DE
5130 struct expression *cond;
5131
5132 gdb_assert (bs->stop);
2bdf28a0
JK
5133
5134 /* BS is built for existing struct breakpoint. */
f431efe5 5135 bl = bs->bp_location_at;
2bdf28a0 5136 gdb_assert (bl != NULL);
f431efe5 5137 b = bs->breakpoint_at;
2bdf28a0 5138 gdb_assert (b != NULL);
18a18393 5139
b775012e
LM
5140 /* Even if the target evaluated the condition on its end and notified GDB, we
5141 need to do so again since GDB does not know if we stopped due to a
5142 breakpoint or a single step breakpoint. */
5143
18a18393 5144 if (frame_id_p (b->frame_id)
edb3359d 5145 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
18a18393 5146 {
7d4df6a4
DE
5147 bs->stop = 0;
5148 return;
5149 }
60e1c644 5150
12ab52e9
PA
5151 /* If this is a thread/task-specific breakpoint, don't waste cpu
5152 evaluating the condition if this isn't the specified
5153 thread/task. */
00431a78
PA
5154 if ((b->thread != -1 && b->thread != thread->global_num)
5155 || (b->task != 0 && b->task != ada_get_task_number (thread)))
6c1b0f7b
DE
5156 {
5157 bs->stop = 0;
5158 return;
5159 }
5160
6dddc817
DE
5161 /* Evaluate extension language breakpoints that have a "stop" method
5162 implemented. */
5163 bs->stop = breakpoint_ext_lang_cond_says_stop (b);
7371cf6d 5164
7d4df6a4
DE
5165 if (is_watchpoint (b))
5166 {
5167 struct watchpoint *w = (struct watchpoint *) b;
3a5c3e22 5168
4d01a485 5169 cond = w->cond_exp.get ();
7d4df6a4
DE
5170 }
5171 else
4d01a485 5172 cond = bl->cond.get ();
60e1c644 5173
7d4df6a4
DE
5174 if (cond && b->disposition != disp_del_at_next_stop)
5175 {
5176 int within_current_scope = 1;
5177 struct watchpoint * w;
60e1c644 5178
7d4df6a4
DE
5179 /* We use value_mark and value_free_to_mark because it could
5180 be a long time before we return to the command level and
5181 call free_all_values. We can't call free_all_values
5182 because we might be in the middle of evaluating a
5183 function call. */
5184 struct value *mark = value_mark ();
5185
5186 if (is_watchpoint (b))
5187 w = (struct watchpoint *) b;
5188 else
5189 w = NULL;
5190
5191 /* Need to select the frame, with all that implies so that
5192 the conditions will have the right context. Because we
5193 use the frame, we will not see an inlined function's
5194 variables when we arrive at a breakpoint at the start
5195 of the inlined function; the current frame will be the
5196 call site. */
5197 if (w == NULL || w->cond_exp_valid_block == NULL)
5198 select_frame (get_current_frame ());
5199 else
18a18393 5200 {
7d4df6a4
DE
5201 struct frame_info *frame;
5202
5203 /* For local watchpoint expressions, which particular
5204 instance of a local is being watched matters, so we
5205 keep track of the frame to evaluate the expression
5206 in. To evaluate the condition however, it doesn't
5207 really matter which instantiation of the function
5208 where the condition makes sense triggers the
5209 watchpoint. This allows an expression like "watch
5210 global if q > 10" set in `func', catch writes to
5211 global on all threads that call `func', or catch
5212 writes on all recursive calls of `func' by a single
5213 thread. We simply always evaluate the condition in
5214 the innermost frame that's executing where it makes
5215 sense to evaluate the condition. It seems
5216 intuitive. */
5217 frame = block_innermost_frame (w->cond_exp_valid_block);
5218 if (frame != NULL)
5219 select_frame (frame);
5220 else
5221 within_current_scope = 0;
18a18393 5222 }
7d4df6a4 5223 if (within_current_scope)
bf469271 5224 {
a70b8144 5225 try
bf469271
PA
5226 {
5227 condition_result = breakpoint_cond_eval (cond);
5228 }
230d2906 5229 catch (const gdb_exception &ex)
bf469271
PA
5230 {
5231 exception_fprintf (gdb_stderr, ex,
5232 "Error in testing breakpoint condition:\n");
5233 }
bf469271 5234 }
7d4df6a4 5235 else
18a18393 5236 {
7d4df6a4
DE
5237 warning (_("Watchpoint condition cannot be tested "
5238 "in the current scope"));
5239 /* If we failed to set the right context for this
5240 watchpoint, unconditionally report it. */
18a18393 5241 }
7d4df6a4
DE
5242 /* FIXME-someday, should give breakpoint #. */
5243 value_free_to_mark (mark);
18a18393 5244 }
7d4df6a4 5245
bf469271 5246 if (cond && !condition_result)
7d4df6a4
DE
5247 {
5248 bs->stop = 0;
5249 }
7d4df6a4
DE
5250 else if (b->ignore_count > 0)
5251 {
5252 b->ignore_count--;
5253 bs->stop = 0;
5254 /* Increase the hit count even though we don't stop. */
5255 ++(b->hit_count);
76727919 5256 gdb::observers::breakpoint_modified.notify (b);
7d4df6a4 5257 }
18a18393
VP
5258}
5259
1cf4d951
PA
5260/* Returns true if we need to track moribund locations of LOC's type
5261 on the current target. */
5262
5263static int
5264need_moribund_for_location_type (struct bp_location *loc)
5265{
5266 return ((loc->loc_type == bp_loc_software_breakpoint
5267 && !target_supports_stopped_by_sw_breakpoint ())
5268 || (loc->loc_type == bp_loc_hardware_breakpoint
5269 && !target_supports_stopped_by_hw_breakpoint ()));
5270}
5271
ddfe970e 5272/* See breakpoint.h. */
c906108c
SS
5273
5274bpstat
ddfe970e 5275build_bpstat_chain (const address_space *aspace, CORE_ADDR bp_addr,
09ac7c10 5276 const struct target_waitstatus *ws)
c906108c 5277{
ddfe970e 5278 struct breakpoint *b;
5760d0ab 5279 bpstat bs_head = NULL, *bs_link = &bs_head;
c5aa993b 5280
429374b8
JK
5281 ALL_BREAKPOINTS (b)
5282 {
1a853c52 5283 if (!breakpoint_enabled (b))
429374b8 5284 continue;
a5606eee 5285
ddfe970e 5286 for (bp_location *bl = b->loc; bl != NULL; bl = bl->next)
429374b8 5287 {
4a64f543
MS
5288 /* For hardware watchpoints, we look only at the first
5289 location. The watchpoint_check function will work on the
5290 entire expression, not the individual locations. For
5291 read watchpoints, the watchpoints_triggered function has
5292 checked all locations already. */
429374b8
JK
5293 if (b->type == bp_hardware_watchpoint && bl != b->loc)
5294 break;
18a18393 5295
f6592439 5296 if (!bl->enabled || bl->shlib_disabled)
429374b8 5297 continue;
c5aa993b 5298
09ac7c10 5299 if (!bpstat_check_location (bl, aspace, bp_addr, ws))
429374b8 5300 continue;
c5aa993b 5301
4a64f543
MS
5302 /* Come here if it's a watchpoint, or if the break address
5303 matches. */
c5aa993b 5304
ddfe970e
KS
5305 bpstat bs = new bpstats (bl, &bs_link); /* Alloc a bpstat to
5306 explain stop. */
c5aa993b 5307
f431efe5
PA
5308 /* Assume we stop. Should we find a watchpoint that is not
5309 actually triggered, or if the condition of the breakpoint
5310 evaluates as false, we'll reset 'stop' to 0. */
429374b8
JK
5311 bs->stop = 1;
5312 bs->print = 1;
d983da9c 5313
f431efe5
PA
5314 /* If this is a scope breakpoint, mark the associated
5315 watchpoint as triggered so that we will handle the
5316 out-of-scope event. We'll get to the watchpoint next
5317 iteration. */
d0fb5eae 5318 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
3a5c3e22
PA
5319 {
5320 struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
5321
5322 w->watchpoint_triggered = watch_triggered_yes;
5323 }
f431efe5
PA
5324 }
5325 }
5326
7c16b83e 5327 /* Check if a moribund breakpoint explains the stop. */
1cf4d951
PA
5328 if (!target_supports_stopped_by_sw_breakpoint ()
5329 || !target_supports_stopped_by_hw_breakpoint ())
f431efe5 5330 {
1123588c 5331 for (bp_location *loc : moribund_locations)
f431efe5 5332 {
1cf4d951
PA
5333 if (breakpoint_location_address_match (loc, aspace, bp_addr)
5334 && need_moribund_for_location_type (loc))
5335 {
ddfe970e 5336 bpstat bs = new bpstats (loc, &bs_link);
1cf4d951
PA
5337 /* For hits of moribund locations, we should just proceed. */
5338 bs->stop = 0;
5339 bs->print = 0;
5340 bs->print_it = print_it_noop;
5341 }
f431efe5
PA
5342 }
5343 }
5344
ddfe970e
KS
5345 return bs_head;
5346}
5347
5348/* See breakpoint.h. */
5349
5350bpstat
5351bpstat_stop_status (const address_space *aspace,
00431a78 5352 CORE_ADDR bp_addr, thread_info *thread,
ddfe970e
KS
5353 const struct target_waitstatus *ws,
5354 bpstat stop_chain)
5355{
5356 struct breakpoint *b = NULL;
5357 /* First item of allocated bpstat's. */
5358 bpstat bs_head = stop_chain;
5359 bpstat bs;
5360 int need_remove_insert;
5361 int removed_any;
5362
5363 /* First, build the bpstat chain with locations that explain a
5364 target stop, while being careful to not set the target running,
5365 as that may invalidate locations (in particular watchpoint
5366 locations are recreated). Resuming will happen here with
5367 breakpoint conditions or watchpoint expressions that include
5368 inferior function calls. */
5369 if (bs_head == NULL)
5370 bs_head = build_bpstat_chain (aspace, bp_addr, ws);
5371
edcc5120
TT
5372 /* A bit of special processing for shlib breakpoints. We need to
5373 process solib loading here, so that the lists of loaded and
5374 unloaded libraries are correct before we handle "catch load" and
5375 "catch unload". */
5376 for (bs = bs_head; bs != NULL; bs = bs->next)
5377 {
5d268276 5378 if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
edcc5120
TT
5379 {
5380 handle_solib_event ();
5381 break;
5382 }
5383 }
5384
f431efe5
PA
5385 /* Now go through the locations that caused the target to stop, and
5386 check whether we're interested in reporting this stop to higher
5387 layers, or whether we should resume the target transparently. */
5388
5389 removed_any = 0;
5390
5760d0ab 5391 for (bs = bs_head; bs != NULL; bs = bs->next)
f431efe5
PA
5392 {
5393 if (!bs->stop)
5394 continue;
5395
f431efe5 5396 b = bs->breakpoint_at;
348d480f
PA
5397 b->ops->check_status (bs);
5398 if (bs->stop)
28010a5d 5399 {
00431a78 5400 bpstat_check_breakpoint_conditions (bs, thread);
f431efe5 5401
429374b8
JK
5402 if (bs->stop)
5403 {
5404 ++(b->hit_count);
76727919 5405 gdb::observers::breakpoint_modified.notify (b);
c906108c 5406
4a64f543 5407 /* We will stop here. */
429374b8
JK
5408 if (b->disposition == disp_disable)
5409 {
816338b5 5410 --(b->enable_count);
1a853c52 5411 if (b->enable_count <= 0)
429374b8 5412 b->enable_state = bp_disabled;
f431efe5 5413 removed_any = 1;
429374b8
JK
5414 }
5415 if (b->silent)
5416 bs->print = 0;
5417 bs->commands = b->commands;
abf85f46 5418 if (command_line_is_silent (bs->commands
d1b0a7bf 5419 ? bs->commands.get () : NULL))
abf85f46 5420 bs->print = 0;
9d6e6e84
HZ
5421
5422 b->ops->after_condition_true (bs);
429374b8
JK
5423 }
5424
348d480f 5425 }
a9b3a50f
PA
5426
5427 /* Print nothing for this entry if we don't stop or don't
5428 print. */
5429 if (!bs->stop || !bs->print)
5430 bs->print_it = print_it_noop;
429374b8 5431 }
876fa593 5432
d983da9c
DJ
5433 /* If we aren't stopping, the value of some hardware watchpoint may
5434 not have changed, but the intermediate memory locations we are
5435 watching may have. Don't bother if we're stopping; this will get
5436 done later. */
d832cb68 5437 need_remove_insert = 0;
5760d0ab
JK
5438 if (! bpstat_causes_stop (bs_head))
5439 for (bs = bs_head; bs != NULL; bs = bs->next)
d983da9c 5440 if (!bs->stop
f431efe5
PA
5441 && bs->breakpoint_at
5442 && is_hardware_watchpoint (bs->breakpoint_at))
d983da9c 5443 {
3a5c3e22
PA
5444 struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
5445
5446 update_watchpoint (w, 0 /* don't reparse. */);
d832cb68 5447 need_remove_insert = 1;
d983da9c
DJ
5448 }
5449
d832cb68 5450 if (need_remove_insert)
44702360 5451 update_global_location_list (UGLL_MAY_INSERT);
f431efe5 5452 else if (removed_any)
44702360 5453 update_global_location_list (UGLL_DONT_INSERT);
d832cb68 5454
5760d0ab 5455 return bs_head;
c906108c 5456}
628fe4e4
JK
5457
5458static void
5459handle_jit_event (void)
5460{
5461 struct frame_info *frame;
5462 struct gdbarch *gdbarch;
5463
243a9253
PA
5464 if (debug_infrun)
5465 fprintf_unfiltered (gdb_stdlog, "handling bp_jit_event\n");
5466
628fe4e4
JK
5467 /* Switch terminal for any messages produced by
5468 breakpoint_re_set. */
223ffa71 5469 target_terminal::ours_for_output ();
628fe4e4
JK
5470
5471 frame = get_current_frame ();
5472 gdbarch = get_frame_arch (frame);
5473
5474 jit_event_handler (gdbarch);
5475
223ffa71 5476 target_terminal::inferior ();
628fe4e4
JK
5477}
5478
5479/* Prepare WHAT final decision for infrun. */
5480
5481/* Decide what infrun needs to do with this bpstat. */
5482
c906108c 5483struct bpstat_what
0e30163f 5484bpstat_what (bpstat bs_head)
c906108c 5485{
c906108c 5486 struct bpstat_what retval;
0e30163f 5487 bpstat bs;
c906108c 5488
628fe4e4 5489 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
aa7d318d 5490 retval.call_dummy = STOP_NONE;
e2d0f980 5491 retval.is_longjmp = false;
628fe4e4 5492
0e30163f 5493 for (bs = bs_head; bs != NULL; bs = bs->next)
c906108c 5494 {
628fe4e4
JK
5495 /* Extract this BS's action. After processing each BS, we check
5496 if its action overrides all we've seem so far. */
5497 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
5498 enum bptype bptype;
5499
c906108c 5500 if (bs->breakpoint_at == NULL)
628fe4e4
JK
5501 {
5502 /* I suspect this can happen if it was a momentary
5503 breakpoint which has since been deleted. */
5504 bptype = bp_none;
5505 }
20874c92 5506 else
f431efe5 5507 bptype = bs->breakpoint_at->type;
628fe4e4
JK
5508
5509 switch (bptype)
c906108c
SS
5510 {
5511 case bp_none:
628fe4e4 5512 break;
c906108c
SS
5513 case bp_breakpoint:
5514 case bp_hardware_breakpoint:
7c16b83e 5515 case bp_single_step:
c906108c
SS
5516 case bp_until:
5517 case bp_finish:
a9b3a50f 5518 case bp_shlib_event:
c906108c
SS
5519 if (bs->stop)
5520 {
5521 if (bs->print)
628fe4e4 5522 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 5523 else
628fe4e4 5524 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
5525 }
5526 else
628fe4e4 5527 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5528 break;
5529 case bp_watchpoint:
5530 case bp_hardware_watchpoint:
5531 case bp_read_watchpoint:
5532 case bp_access_watchpoint:
5533 if (bs->stop)
5534 {
5535 if (bs->print)
628fe4e4 5536 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 5537 else
628fe4e4 5538 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
5539 }
5540 else
628fe4e4
JK
5541 {
5542 /* There was a watchpoint, but we're not stopping.
5543 This requires no further action. */
5544 }
c906108c
SS
5545 break;
5546 case bp_longjmp:
e2e4d78b 5547 case bp_longjmp_call_dummy:
186c406b 5548 case bp_exception:
0a39bb32
PA
5549 if (bs->stop)
5550 {
5551 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
5552 retval.is_longjmp = bptype != bp_exception;
5553 }
5554 else
5555 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5556 break;
5557 case bp_longjmp_resume:
186c406b 5558 case bp_exception_resume:
0a39bb32
PA
5559 if (bs->stop)
5560 {
5561 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
5562 retval.is_longjmp = bptype == bp_longjmp_resume;
5563 }
5564 else
5565 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5566 break;
5567 case bp_step_resume:
5568 if (bs->stop)
628fe4e4
JK
5569 this_action = BPSTAT_WHAT_STEP_RESUME;
5570 else
c906108c 5571 {
628fe4e4
JK
5572 /* It is for the wrong frame. */
5573 this_action = BPSTAT_WHAT_SINGLE;
c906108c 5574 }
c906108c 5575 break;
2c03e5be
PA
5576 case bp_hp_step_resume:
5577 if (bs->stop)
5578 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
5579 else
5580 {
5581 /* It is for the wrong frame. */
5582 this_action = BPSTAT_WHAT_SINGLE;
5583 }
5584 break;
c906108c 5585 case bp_watchpoint_scope:
c4093a6a 5586 case bp_thread_event:
1900040c 5587 case bp_overlay_event:
0fd8e87f 5588 case bp_longjmp_master:
aa7d318d 5589 case bp_std_terminate_master:
186c406b 5590 case bp_exception_master:
628fe4e4 5591 this_action = BPSTAT_WHAT_SINGLE;
c4093a6a 5592 break;
ce78b96d 5593 case bp_catchpoint:
c5aa993b
JM
5594 if (bs->stop)
5595 {
5596 if (bs->print)
628fe4e4 5597 this_action = BPSTAT_WHAT_STOP_NOISY;
c5aa993b 5598 else
628fe4e4 5599 this_action = BPSTAT_WHAT_STOP_SILENT;
c5aa993b
JM
5600 }
5601 else
628fe4e4 5602 {
cb1e4e32
PA
5603 /* Some catchpoints are implemented with breakpoints.
5604 For those, we need to step over the breakpoint. */
5605 if (bs->bp_location_at->loc_type != bp_loc_other)
5606 this_action = BPSTAT_WHAT_SINGLE;
628fe4e4
JK
5607 }
5608 break;
628fe4e4 5609 case bp_jit_event:
628fe4e4 5610 this_action = BPSTAT_WHAT_SINGLE;
c5aa993b 5611 break;
c906108c 5612 case bp_call_dummy:
53a5351d
JM
5613 /* Make sure the action is stop (silent or noisy),
5614 so infrun.c pops the dummy frame. */
aa7d318d 5615 retval.call_dummy = STOP_STACK_DUMMY;
628fe4e4 5616 this_action = BPSTAT_WHAT_STOP_SILENT;
aa7d318d
TT
5617 break;
5618 case bp_std_terminate:
5619 /* Make sure the action is stop (silent or noisy),
5620 so infrun.c pops the dummy frame. */
aa7d318d 5621 retval.call_dummy = STOP_STD_TERMINATE;
628fe4e4 5622 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c 5623 break;
1042e4c0 5624 case bp_tracepoint:
7a697b8d 5625 case bp_fast_tracepoint:
0fb4aa4b 5626 case bp_static_tracepoint:
1042e4c0
SS
5627 /* Tracepoint hits should not be reported back to GDB, and
5628 if one got through somehow, it should have been filtered
5629 out already. */
5630 internal_error (__FILE__, __LINE__,
7a697b8d 5631 _("bpstat_what: tracepoint encountered"));
0e30163f
JK
5632 break;
5633 case bp_gnu_ifunc_resolver:
5634 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
5635 this_action = BPSTAT_WHAT_SINGLE;
5636 break;
5637 case bp_gnu_ifunc_resolver_return:
5638 /* The breakpoint will be removed, execution will restart from the
5639 PC of the former breakpoint. */
5640 this_action = BPSTAT_WHAT_KEEP_CHECKING;
5641 break;
e7e0cddf
SS
5642
5643 case bp_dprintf:
a11cfd87
HZ
5644 if (bs->stop)
5645 this_action = BPSTAT_WHAT_STOP_SILENT;
5646 else
5647 this_action = BPSTAT_WHAT_SINGLE;
e7e0cddf
SS
5648 break;
5649
628fe4e4
JK
5650 default:
5651 internal_error (__FILE__, __LINE__,
5652 _("bpstat_what: unhandled bptype %d"), (int) bptype);
c906108c 5653 }
628fe4e4 5654
325fac50 5655 retval.main_action = std::max (retval.main_action, this_action);
c906108c 5656 }
628fe4e4 5657
243a9253
PA
5658 return retval;
5659}
628fe4e4 5660
243a9253
PA
5661void
5662bpstat_run_callbacks (bpstat bs_head)
5663{
5664 bpstat bs;
628fe4e4 5665
0e30163f
JK
5666 for (bs = bs_head; bs != NULL; bs = bs->next)
5667 {
5668 struct breakpoint *b = bs->breakpoint_at;
5669
5670 if (b == NULL)
5671 continue;
5672 switch (b->type)
5673 {
243a9253
PA
5674 case bp_jit_event:
5675 handle_jit_event ();
5676 break;
0e30163f
JK
5677 case bp_gnu_ifunc_resolver:
5678 gnu_ifunc_resolver_stop (b);
5679 break;
5680 case bp_gnu_ifunc_resolver_return:
5681 gnu_ifunc_resolver_return_stop (b);
5682 break;
5683 }
5684 }
c906108c
SS
5685}
5686
4c462cb0 5687/* See breakpoint.h. */
c906108c 5688
4c462cb0
SM
5689bool
5690bpstat_should_step ()
c906108c
SS
5691{
5692 struct breakpoint *b;
cc59ec59 5693
c906108c 5694 ALL_BREAKPOINTS (b)
717a8278 5695 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
4c462cb0
SM
5696 return true;
5697 return false;
c906108c
SS
5698}
5699
4c462cb0
SM
5700/* See breakpoint.h. */
5701
5702bool
67822962
PA
5703bpstat_causes_stop (bpstat bs)
5704{
5705 for (; bs != NULL; bs = bs->next)
5706 if (bs->stop)
4c462cb0 5707 return true;
67822962 5708
4c462cb0 5709 return false;
67822962
PA
5710}
5711
c906108c 5712\f
c5aa993b 5713
170b53b2
UW
5714/* Compute a string of spaces suitable to indent the next line
5715 so it starts at the position corresponding to the table column
5716 named COL_NAME in the currently active table of UIOUT. */
5717
5718static char *
5719wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
5720{
5721 static char wrap_indent[80];
5722 int i, total_width, width, align;
c5209615 5723 const char *text;
170b53b2
UW
5724
5725 total_width = 0;
112e8700 5726 for (i = 1; uiout->query_table_field (i, &width, &align, &text); i++)
170b53b2
UW
5727 {
5728 if (strcmp (text, col_name) == 0)
5729 {
5730 gdb_assert (total_width < sizeof wrap_indent);
5731 memset (wrap_indent, ' ', total_width);
5732 wrap_indent[total_width] = 0;
5733
5734 return wrap_indent;
5735 }
5736
5737 total_width += width + 1;
5738 }
5739
5740 return NULL;
5741}
5742
b775012e
LM
5743/* Determine if the locations of this breakpoint will have their conditions
5744 evaluated by the target, host or a mix of both. Returns the following:
5745
5746 "host": Host evals condition.
5747 "host or target": Host or Target evals condition.
5748 "target": Target evals condition.
5749*/
5750
5751static const char *
5752bp_condition_evaluator (struct breakpoint *b)
5753{
5754 struct bp_location *bl;
5755 char host_evals = 0;
5756 char target_evals = 0;
5757
5758 if (!b)
5759 return NULL;
5760
5761 if (!is_breakpoint (b))
5762 return NULL;
5763
5764 if (gdb_evaluates_breakpoint_condition_p ()
5765 || !target_supports_evaluation_of_breakpoint_conditions ())
5766 return condition_evaluation_host;
5767
5768 for (bl = b->loc; bl; bl = bl->next)
5769 {
5770 if (bl->cond_bytecode)
5771 target_evals++;
5772 else
5773 host_evals++;
5774 }
5775
5776 if (host_evals && target_evals)
5777 return condition_evaluation_both;
5778 else if (target_evals)
5779 return condition_evaluation_target;
5780 else
5781 return condition_evaluation_host;
5782}
5783
5784/* Determine the breakpoint location's condition evaluator. This is
5785 similar to bp_condition_evaluator, but for locations. */
5786
5787static const char *
5788bp_location_condition_evaluator (struct bp_location *bl)
5789{
5790 if (bl && !is_breakpoint (bl->owner))
5791 return NULL;
5792
5793 if (gdb_evaluates_breakpoint_condition_p ()
5794 || !target_supports_evaluation_of_breakpoint_conditions ())
5795 return condition_evaluation_host;
5796
5797 if (bl && bl->cond_bytecode)
5798 return condition_evaluation_target;
5799 else
5800 return condition_evaluation_host;
5801}
5802
859825b8
JK
5803/* Print the LOC location out of the list of B->LOC locations. */
5804
170b53b2
UW
5805static void
5806print_breakpoint_location (struct breakpoint *b,
5807 struct bp_location *loc)
0d381245 5808{
79a45e25 5809 struct ui_out *uiout = current_uiout;
5ed8105e
PA
5810
5811 scoped_restore_current_program_space restore_pspace;
6c95b8df 5812
859825b8
JK
5813 if (loc != NULL && loc->shlib_disabled)
5814 loc = NULL;
5815
6c95b8df
PA
5816 if (loc != NULL)
5817 set_current_program_space (loc->pspace);
5818
56435ebe 5819 if (b->display_canonical)
d28cd78a 5820 uiout->field_string ("what", event_location_to_string (b->location.get ()));
2f202fde 5821 else if (loc && loc->symtab)
0d381245 5822 {
4a27f119
KS
5823 const struct symbol *sym = loc->symbol;
5824
0d381245
VP
5825 if (sym)
5826 {
112e8700 5827 uiout->text ("in ");
987012b8 5828 uiout->field_string ("func", sym->print_name (),
e43b10e1 5829 function_name_style.style ());
112e8700
SM
5830 uiout->text (" ");
5831 uiout->wrap_hint (wrap_indent_at_field (uiout, "what"));
5832 uiout->text ("at ");
0d381245 5833 }
112e8700 5834 uiout->field_string ("file",
cbe56571 5835 symtab_to_filename_for_display (loc->symtab),
e43b10e1 5836 file_name_style.style ());
112e8700 5837 uiout->text (":");
05cba821 5838
112e8700
SM
5839 if (uiout->is_mi_like_p ())
5840 uiout->field_string ("fullname", symtab_to_fullname (loc->symtab));
0d381245 5841
381befee 5842 uiout->field_signed ("line", loc->line_number);
0d381245 5843 }
859825b8 5844 else if (loc)
0d381245 5845 {
d7e74731 5846 string_file stb;
170b53b2 5847
d7e74731 5848 print_address_symbolic (loc->gdbarch, loc->address, &stb,
22e722e1 5849 demangle, "");
112e8700 5850 uiout->field_stream ("at", stb);
0d381245 5851 }
859825b8 5852 else
f00aae0f 5853 {
d28cd78a
TT
5854 uiout->field_string ("pending",
5855 event_location_to_string (b->location.get ()));
f00aae0f
KS
5856 /* If extra_string is available, it could be holding a condition
5857 or dprintf arguments. In either case, make sure it is printed,
5858 too, but only for non-MI streams. */
112e8700 5859 if (!uiout->is_mi_like_p () && b->extra_string != NULL)
f00aae0f
KS
5860 {
5861 if (b->type == bp_dprintf)
112e8700 5862 uiout->text (",");
f00aae0f 5863 else
112e8700
SM
5864 uiout->text (" ");
5865 uiout->text (b->extra_string);
f00aae0f
KS
5866 }
5867 }
6c95b8df 5868
b775012e
LM
5869 if (loc && is_breakpoint (b)
5870 && breakpoint_condition_evaluation_mode () == condition_evaluation_target
5871 && bp_condition_evaluator (b) == condition_evaluation_both)
5872 {
112e8700
SM
5873 uiout->text (" (");
5874 uiout->field_string ("evaluated-by",
b775012e 5875 bp_location_condition_evaluator (loc));
112e8700 5876 uiout->text (")");
b775012e 5877 }
0d381245
VP
5878}
5879
269b11a2
PA
5880static const char *
5881bptype_string (enum bptype type)
c906108c 5882{
c4093a6a
JM
5883 struct ep_type_description
5884 {
5885 enum bptype type;
a121b7c1 5886 const char *description;
c4093a6a
JM
5887 };
5888 static struct ep_type_description bptypes[] =
c906108c 5889 {
c5aa993b
JM
5890 {bp_none, "?deleted?"},
5891 {bp_breakpoint, "breakpoint"},
c906108c 5892 {bp_hardware_breakpoint, "hw breakpoint"},
7c16b83e 5893 {bp_single_step, "sw single-step"},
c5aa993b
JM
5894 {bp_until, "until"},
5895 {bp_finish, "finish"},
5896 {bp_watchpoint, "watchpoint"},
c906108c 5897 {bp_hardware_watchpoint, "hw watchpoint"},
c5aa993b
JM
5898 {bp_read_watchpoint, "read watchpoint"},
5899 {bp_access_watchpoint, "acc watchpoint"},
5900 {bp_longjmp, "longjmp"},
5901 {bp_longjmp_resume, "longjmp resume"},
e2e4d78b 5902 {bp_longjmp_call_dummy, "longjmp for call dummy"},
186c406b
TT
5903 {bp_exception, "exception"},
5904 {bp_exception_resume, "exception resume"},
c5aa993b 5905 {bp_step_resume, "step resume"},
2c03e5be 5906 {bp_hp_step_resume, "high-priority step resume"},
c5aa993b
JM
5907 {bp_watchpoint_scope, "watchpoint scope"},
5908 {bp_call_dummy, "call dummy"},
aa7d318d 5909 {bp_std_terminate, "std::terminate"},
c5aa993b 5910 {bp_shlib_event, "shlib events"},
c4093a6a 5911 {bp_thread_event, "thread events"},
1900040c 5912 {bp_overlay_event, "overlay events"},
0fd8e87f 5913 {bp_longjmp_master, "longjmp master"},
aa7d318d 5914 {bp_std_terminate_master, "std::terminate master"},
186c406b 5915 {bp_exception_master, "exception master"},
ce78b96d 5916 {bp_catchpoint, "catchpoint"},
1042e4c0 5917 {bp_tracepoint, "tracepoint"},
7a697b8d 5918 {bp_fast_tracepoint, "fast tracepoint"},
0fb4aa4b 5919 {bp_static_tracepoint, "static tracepoint"},
e7e0cddf 5920 {bp_dprintf, "dprintf"},
4efc6507 5921 {bp_jit_event, "jit events"},
0e30163f
JK
5922 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
5923 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
c5aa993b 5924 };
269b11a2
PA
5925
5926 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
5927 || ((int) type != bptypes[(int) type].type))
5928 internal_error (__FILE__, __LINE__,
5929 _("bptypes table does not describe type #%d."),
5930 (int) type);
5931
5932 return bptypes[(int) type].description;
5933}
5934
998580f1
MK
5935/* For MI, output a field named 'thread-groups' with a list as the value.
5936 For CLI, prefix the list with the string 'inf'. */
5937
5938static void
5939output_thread_groups (struct ui_out *uiout,
5940 const char *field_name,
5c632425 5941 const std::vector<int> &inf_nums,
998580f1
MK
5942 int mi_only)
5943{
112e8700 5944 int is_mi = uiout->is_mi_like_p ();
998580f1
MK
5945
5946 /* For backward compatibility, don't display inferiors in CLI unless
5947 there are several. Always display them for MI. */
5948 if (!is_mi && mi_only)
5949 return;
5950
10f489e5 5951 ui_out_emit_list list_emitter (uiout, field_name);
752eb8b4 5952
5c632425 5953 for (size_t i = 0; i < inf_nums.size (); i++)
998580f1
MK
5954 {
5955 if (is_mi)
5956 {
5957 char mi_group[10];
5958
5c632425 5959 xsnprintf (mi_group, sizeof (mi_group), "i%d", inf_nums[i]);
112e8700 5960 uiout->field_string (NULL, mi_group);
998580f1
MK
5961 }
5962 else
5963 {
5964 if (i == 0)
112e8700 5965 uiout->text (" inf ");
998580f1 5966 else
112e8700 5967 uiout->text (", ");
998580f1 5968
5c632425 5969 uiout->text (plongest (inf_nums[i]));
998580f1
MK
5970 }
5971 }
998580f1
MK
5972}
5973
a38118e5
PA
5974/* Print B to gdb_stdout. If RAW_LOC, print raw breakpoint locations
5975 instead of going via breakpoint_ops::print_one. This makes "maint
5976 info breakpoints" show the software breakpoint locations of
5977 catchpoints, which are considered internal implementation
5978 detail. */
269b11a2
PA
5979
5980static void
5981print_one_breakpoint_location (struct breakpoint *b,
5982 struct bp_location *loc,
5983 int loc_number,
5984 struct bp_location **last_loc,
a38118e5 5985 int allflag, bool raw_loc)
269b11a2
PA
5986{
5987 struct command_line *l;
c2c6d25f 5988 static char bpenables[] = "nynny";
c906108c 5989
79a45e25 5990 struct ui_out *uiout = current_uiout;
0d381245
VP
5991 int header_of_multiple = 0;
5992 int part_of_multiple = (loc != NULL);
79a45b7d
TT
5993 struct value_print_options opts;
5994
5995 get_user_print_options (&opts);
0d381245
VP
5996
5997 gdb_assert (!loc || loc_number != 0);
4a64f543
MS
5998 /* See comment in print_one_breakpoint concerning treatment of
5999 breakpoints with single disabled location. */
0d381245
VP
6000 if (loc == NULL
6001 && (b->loc != NULL
6002 && (b->loc->next != NULL || !b->loc->enabled)))
6003 header_of_multiple = 1;
6004 if (loc == NULL)
6005 loc = b->loc;
6006
c4093a6a
JM
6007 annotate_record ();
6008
6009 /* 1 */
6010 annotate_field (0);
0d381245 6011 if (part_of_multiple)
528e1572 6012 uiout->field_fmt ("number", "%d.%d", b->number, loc_number);
0d381245 6013 else
381befee 6014 uiout->field_signed ("number", b->number);
c4093a6a
JM
6015
6016 /* 2 */
6017 annotate_field (1);
0d381245 6018 if (part_of_multiple)
112e8700 6019 uiout->field_skip ("type");
269b11a2 6020 else
112e8700 6021 uiout->field_string ("type", bptype_string (b->type));
c4093a6a
JM
6022
6023 /* 3 */
6024 annotate_field (2);
0d381245 6025 if (part_of_multiple)
112e8700 6026 uiout->field_skip ("disp");
0d381245 6027 else
112e8700 6028 uiout->field_string ("disp", bpdisp_text (b->disposition));
0d381245 6029
c4093a6a
JM
6030 /* 4 */
6031 annotate_field (3);
0d381245 6032 if (part_of_multiple)
112e8700 6033 uiout->field_string ("enabled", loc->enabled ? "y" : "n");
0d381245 6034 else
112e8700 6035 uiout->field_fmt ("enabled", "%c", bpenables[(int) b->enable_state]);
0d381245 6036
c4093a6a 6037 /* 5 and 6 */
a38118e5 6038 if (!raw_loc && b->ops != NULL && b->ops->print_one != NULL)
b58a68fe 6039 b->ops->print_one (b, last_loc);
3086aeae 6040 else
a38118e5
PA
6041 {
6042 if (is_watchpoint (b))
3a5c3e22
PA
6043 {
6044 struct watchpoint *w = (struct watchpoint *) b;
6045
6046 /* Field 4, the address, is omitted (which makes the columns
6047 not line up too nicely with the headers, but the effect
6048 is relatively readable). */
6049 if (opts.addressprint)
112e8700 6050 uiout->field_skip ("addr");
3a5c3e22 6051 annotate_field (5);
112e8700 6052 uiout->field_string ("what", w->exp_string);
3a5c3e22 6053 }
f06f1252
TT
6054 else if (!is_catchpoint (b) || is_exception_catchpoint (b)
6055 || is_ada_exception_catchpoint (b))
a38118e5
PA
6056 {
6057 if (opts.addressprint)
6058 {
6059 annotate_field (4);
6060 if (header_of_multiple)
7f6aba03
TT
6061 uiout->field_string ("addr", "<MULTIPLE>",
6062 metadata_style.style ());
a38118e5 6063 else if (b->loc == NULL || loc->shlib_disabled)
7f6aba03
TT
6064 uiout->field_string ("addr", "<PENDING>",
6065 metadata_style.style ());
a38118e5
PA
6066 else
6067 uiout->field_core_addr ("addr",
6068 loc->gdbarch, loc->address);
6069 }
6070 annotate_field (5);
6071 if (!header_of_multiple)
6072 print_breakpoint_location (b, loc);
6073 if (b->loc)
6074 *last_loc = b->loc;
6075 }
6076 }
6c95b8df 6077
998580f1 6078 if (loc != NULL && !header_of_multiple)
6c95b8df 6079 {
5c632425 6080 std::vector<int> inf_nums;
998580f1 6081 int mi_only = 1;
6c95b8df 6082
08036331 6083 for (inferior *inf : all_inferiors ())
6c95b8df
PA
6084 {
6085 if (inf->pspace == loc->pspace)
5c632425 6086 inf_nums.push_back (inf->num);
6c95b8df 6087 }
998580f1
MK
6088
6089 /* For backward compatibility, don't display inferiors in CLI unless
6090 there are several. Always display for MI. */
6091 if (allflag
6092 || (!gdbarch_has_global_breakpoints (target_gdbarch ())
6093 && (number_of_program_spaces () > 1
6094 || number_of_inferiors () > 1)
6095 /* LOC is for existing B, it cannot be in
6096 moribund_locations and thus having NULL OWNER. */
6097 && loc->owner->type != bp_catchpoint))
6098 mi_only = 0;
5c632425 6099 output_thread_groups (uiout, "thread-groups", inf_nums, mi_only);
6c95b8df
PA
6100 }
6101
4a306c9a 6102 if (!part_of_multiple)
c4093a6a 6103 {
4a306c9a
JB
6104 if (b->thread != -1)
6105 {
6106 /* FIXME: This seems to be redundant and lost here; see the
4a64f543 6107 "stop only in" line a little further down. */
112e8700 6108 uiout->text (" thread ");
381befee 6109 uiout->field_signed ("thread", b->thread);
4a306c9a
JB
6110 }
6111 else if (b->task != 0)
6112 {
112e8700 6113 uiout->text (" task ");
381befee 6114 uiout->field_signed ("task", b->task);
4a306c9a 6115 }
c4093a6a 6116 }
f1310107 6117
112e8700 6118 uiout->text ("\n");
f1310107 6119
348d480f 6120 if (!part_of_multiple)
f1310107
TJB
6121 b->ops->print_one_detail (b, uiout);
6122
0d381245 6123 if (part_of_multiple && frame_id_p (b->frame_id))
c4093a6a
JM
6124 {
6125 annotate_field (6);
112e8700 6126 uiout->text ("\tstop only in stack frame at ");
e5dd4106 6127 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
818dd999 6128 the frame ID. */
112e8700 6129 uiout->field_core_addr ("frame",
5af949e3 6130 b->gdbarch, b->frame_id.stack_addr);
112e8700 6131 uiout->text ("\n");
c4093a6a
JM
6132 }
6133
28010a5d 6134 if (!part_of_multiple && b->cond_string)
c4093a6a
JM
6135 {
6136 annotate_field (7);
d77f58be 6137 if (is_tracepoint (b))
112e8700 6138 uiout->text ("\ttrace only if ");
1042e4c0 6139 else
112e8700
SM
6140 uiout->text ("\tstop only if ");
6141 uiout->field_string ("cond", b->cond_string);
b775012e
LM
6142
6143 /* Print whether the target is doing the breakpoint's condition
6144 evaluation. If GDB is doing the evaluation, don't print anything. */
6145 if (is_breakpoint (b)
6146 && breakpoint_condition_evaluation_mode ()
6147 == condition_evaluation_target)
6148 {
6a831f06
PA
6149 uiout->message (" (%pF evals)",
6150 string_field ("evaluated-by",
6151 bp_condition_evaluator (b)));
b775012e 6152 }
112e8700 6153 uiout->text ("\n");
0101ce28
JJ
6154 }
6155
0d381245 6156 if (!part_of_multiple && b->thread != -1)
c4093a6a 6157 {
4a64f543 6158 /* FIXME should make an annotation for this. */
112e8700
SM
6159 uiout->text ("\tstop only in thread ");
6160 if (uiout->is_mi_like_p ())
381befee 6161 uiout->field_signed ("thread", b->thread);
5d5658a1
PA
6162 else
6163 {
6164 struct thread_info *thr = find_thread_global_id (b->thread);
6165
112e8700 6166 uiout->field_string ("thread", print_thread_id (thr));
5d5658a1 6167 }
112e8700 6168 uiout->text ("\n");
c4093a6a
JM
6169 }
6170
556ec64d
YQ
6171 if (!part_of_multiple)
6172 {
6173 if (b->hit_count)
31f56a27
YQ
6174 {
6175 /* FIXME should make an annotation for this. */
6176 if (is_catchpoint (b))
112e8700 6177 uiout->text ("\tcatchpoint");
31f56a27 6178 else if (is_tracepoint (b))
112e8700 6179 uiout->text ("\ttracepoint");
31f56a27 6180 else
112e8700
SM
6181 uiout->text ("\tbreakpoint");
6182 uiout->text (" already hit ");
381befee 6183 uiout->field_signed ("times", b->hit_count);
31f56a27 6184 if (b->hit_count == 1)
112e8700 6185 uiout->text (" time\n");
31f56a27 6186 else
112e8700 6187 uiout->text (" times\n");
31f56a27 6188 }
556ec64d
YQ
6189 else
6190 {
31f56a27 6191 /* Output the count also if it is zero, but only if this is mi. */
112e8700 6192 if (uiout->is_mi_like_p ())
381befee 6193 uiout->field_signed ("times", b->hit_count);
556ec64d
YQ
6194 }
6195 }
8b93c638 6196
0d381245 6197 if (!part_of_multiple && b->ignore_count)
c4093a6a
JM
6198 {
6199 annotate_field (8);
6a831f06
PA
6200 uiout->message ("\tignore next %pF hits\n",
6201 signed_field ("ignore", b->ignore_count));
c4093a6a 6202 }
059fb39f 6203
816338b5
SS
6204 /* Note that an enable count of 1 corresponds to "enable once"
6205 behavior, which is reported by the combination of enablement and
6206 disposition, so we don't need to mention it here. */
6207 if (!part_of_multiple && b->enable_count > 1)
6208 {
6209 annotate_field (8);
112e8700 6210 uiout->text ("\tdisable after ");
816338b5
SS
6211 /* Tweak the wording to clarify that ignore and enable counts
6212 are distinct, and have additive effect. */
6213 if (b->ignore_count)
112e8700 6214 uiout->text ("additional ");
816338b5 6215 else
112e8700 6216 uiout->text ("next ");
381befee 6217 uiout->field_signed ("enable", b->enable_count);
112e8700 6218 uiout->text (" hits\n");
816338b5
SS
6219 }
6220
f196051f
SS
6221 if (!part_of_multiple && is_tracepoint (b))
6222 {
6223 struct tracepoint *tp = (struct tracepoint *) b;
6224
6225 if (tp->traceframe_usage)
6226 {
112e8700 6227 uiout->text ("\ttrace buffer usage ");
381befee 6228 uiout->field_signed ("traceframe-usage", tp->traceframe_usage);
112e8700 6229 uiout->text (" bytes\n");
f196051f
SS
6230 }
6231 }
d3ce09f5 6232
d1b0a7bf 6233 l = b->commands ? b->commands.get () : NULL;
059fb39f 6234 if (!part_of_multiple && l)
c4093a6a
JM
6235 {
6236 annotate_field (9);
2e783024 6237 ui_out_emit_tuple tuple_emitter (uiout, "script");
8b93c638 6238 print_command_lines (uiout, l, 4);
c4093a6a 6239 }
d24317b4 6240
d9b3f62e 6241 if (is_tracepoint (b))
1042e4c0 6242 {
d9b3f62e
PA
6243 struct tracepoint *t = (struct tracepoint *) b;
6244
6245 if (!part_of_multiple && t->pass_count)
6246 {
6247 annotate_field (10);
112e8700 6248 uiout->text ("\tpass count ");
381befee 6249 uiout->field_signed ("pass", t->pass_count);
112e8700 6250 uiout->text (" \n");
d9b3f62e 6251 }
f2a8bc8a
YQ
6252
6253 /* Don't display it when tracepoint or tracepoint location is
6254 pending. */
6255 if (!header_of_multiple && loc != NULL && !loc->shlib_disabled)
6256 {
6257 annotate_field (11);
6258
112e8700
SM
6259 if (uiout->is_mi_like_p ())
6260 uiout->field_string ("installed",
f2a8bc8a
YQ
6261 loc->inserted ? "y" : "n");
6262 else
6263 {
6264 if (loc->inserted)
112e8700 6265 uiout->text ("\t");
f2a8bc8a 6266 else
112e8700
SM
6267 uiout->text ("\tnot ");
6268 uiout->text ("installed on target\n");
f2a8bc8a
YQ
6269 }
6270 }
1042e4c0
SS
6271 }
6272
112e8700 6273 if (uiout->is_mi_like_p () && !part_of_multiple)
d24317b4 6274 {
3a5c3e22
PA
6275 if (is_watchpoint (b))
6276 {
6277 struct watchpoint *w = (struct watchpoint *) b;
6278
112e8700 6279 uiout->field_string ("original-location", w->exp_string);
3a5c3e22 6280 }
f00aae0f 6281 else if (b->location != NULL
d28cd78a 6282 && event_location_to_string (b->location.get ()) != NULL)
112e8700 6283 uiout->field_string ("original-location",
d28cd78a 6284 event_location_to_string (b->location.get ()));
d24317b4 6285 }
c4093a6a 6286}
c5aa993b 6287
13674803
SM
6288/* See breakpoint.h. */
6289
6290bool fix_multi_location_breakpoint_output_globally = false;
6291
0d381245
VP
6292static void
6293print_one_breakpoint (struct breakpoint *b,
4a64f543 6294 struct bp_location **last_loc,
6c95b8df 6295 int allflag)
0d381245 6296{
79a45e25 6297 struct ui_out *uiout = current_uiout;
13674803
SM
6298 bool use_fixed_output
6299 = (uiout->test_flags (fix_multi_location_breakpoint_output)
6300 || fix_multi_location_breakpoint_output_globally);
8d3788bd 6301
b4be1b06 6302 gdb::optional<ui_out_emit_tuple> bkpt_tuple_emitter (gdb::in_place, uiout, "bkpt");
a38118e5 6303 print_one_breakpoint_location (b, NULL, 0, last_loc, allflag, false);
8d3788bd 6304
b4be1b06
SM
6305 /* The mi2 broken format: the main breakpoint tuple ends here, the locations
6306 are outside. */
6307 if (!use_fixed_output)
6308 bkpt_tuple_emitter.reset ();
0d381245
VP
6309
6310 /* If this breakpoint has custom print function,
6311 it's already printed. Otherwise, print individual
6312 locations, if any. */
a38118e5
PA
6313 if (b->ops == NULL
6314 || b->ops->print_one == NULL
6315 || allflag)
0d381245 6316 {
4a64f543
MS
6317 /* If breakpoint has a single location that is disabled, we
6318 print it as if it had several locations, since otherwise it's
6319 hard to represent "breakpoint enabled, location disabled"
6320 situation.
6321
6322 Note that while hardware watchpoints have several locations
a38118e5
PA
6323 internally, that's not a property exposed to users.
6324
6325 Likewise, while catchpoints may be implemented with
6326 breakpoints (e.g., catch throw), that's not a property
6327 exposed to users. We do however display the internal
6328 breakpoint locations with "maint info breakpoints". */
6329 if (!is_hardware_watchpoint (b)
f06f1252
TT
6330 && (!is_catchpoint (b) || is_exception_catchpoint (b)
6331 || is_ada_exception_catchpoint (b))
a38118e5
PA
6332 && (allflag
6333 || (b->loc && (b->loc->next || !b->loc->enabled))))
0d381245 6334 {
b4be1b06
SM
6335 gdb::optional<ui_out_emit_list> locations_list;
6336
6337 /* For MI version <= 2, keep the behavior where GDB outputs an invalid
6338 MI record. For later versions, place breakpoint locations in a
6339 list. */
6340 if (uiout->is_mi_like_p () && use_fixed_output)
6341 locations_list.emplace (uiout, "locations");
8d3788bd 6342
b4be1b06
SM
6343 int n = 1;
6344 for (bp_location *loc = b->loc; loc != NULL; loc = loc->next, ++n)
8d3788bd 6345 {
b4be1b06 6346 ui_out_emit_tuple loc_tuple_emitter (uiout, NULL);
a38118e5
PA
6347 print_one_breakpoint_location (b, loc, n, last_loc,
6348 allflag, allflag);
8d3788bd 6349 }
0d381245
VP
6350 }
6351 }
6352}
6353
a6d9a66e
UW
6354static int
6355breakpoint_address_bits (struct breakpoint *b)
6356{
6357 int print_address_bits = 0;
6358 struct bp_location *loc;
6359
c6d81124
PA
6360 /* Software watchpoints that aren't watching memory don't have an
6361 address to print. */
6362 if (is_no_memory_software_watchpoint (b))
6363 return 0;
6364
a6d9a66e
UW
6365 for (loc = b->loc; loc; loc = loc->next)
6366 {
c7437ca6
PA
6367 int addr_bit;
6368
c7437ca6 6369 addr_bit = gdbarch_addr_bit (loc->gdbarch);
a6d9a66e
UW
6370 if (addr_bit > print_address_bits)
6371 print_address_bits = addr_bit;
6372 }
6373
6374 return print_address_bits;
6375}
0d381245 6376
65630365 6377/* See breakpoint.h. */
c5aa993b 6378
65630365
PA
6379void
6380print_breakpoint (breakpoint *b)
c4093a6a 6381{
a6d9a66e 6382 struct bp_location *dummy_loc = NULL;
65630365 6383 print_one_breakpoint (b, &dummy_loc, 0);
c4093a6a 6384}
c5aa993b 6385
09d682a4
TT
6386/* Return true if this breakpoint was set by the user, false if it is
6387 internal or momentary. */
6388
6389int
6390user_breakpoint_p (struct breakpoint *b)
6391{
46c6471b 6392 return b->number > 0;
09d682a4
TT
6393}
6394
93daf339
TT
6395/* See breakpoint.h. */
6396
6397int
6398pending_breakpoint_p (struct breakpoint *b)
6399{
6400 return b->loc == NULL;
6401}
6402
5c458ae8
SM
6403/* Print information on breakpoints (including watchpoints and tracepoints).
6404
6405 If non-NULL, BP_NUM_LIST is a list of numbers and number ranges as
6406 understood by number_or_range_parser. Only breakpoints included in this
6407 list are then printed.
6408
6409 If SHOW_INTERNAL is true, print internal breakpoints.
6410
6411 If FILTER is non-NULL, call it on each breakpoint and only include the
6412 ones for which it returns true.
6413
6414 Return the total number of breakpoints listed. */
c906108c 6415
d77f58be 6416static int
5c458ae8 6417breakpoint_1 (const char *bp_num_list, bool show_internal,
f2478a7e 6418 bool (*filter) (const struct breakpoint *))
c4093a6a 6419{
52f0bd74 6420 struct breakpoint *b;
a6d9a66e 6421 struct bp_location *last_loc = NULL;
7f3b0473 6422 int nr_printable_breakpoints;
79a45b7d 6423 struct value_print_options opts;
a6d9a66e 6424 int print_address_bits = 0;
269b11a2 6425 int print_type_col_width = 14;
79a45e25 6426 struct ui_out *uiout = current_uiout;
269b11a2 6427
79a45b7d
TT
6428 get_user_print_options (&opts);
6429
4a64f543
MS
6430 /* Compute the number of rows in the table, as well as the size
6431 required for address fields. */
7f3b0473
AC
6432 nr_printable_breakpoints = 0;
6433 ALL_BREAKPOINTS (b)
e5a67952
MS
6434 {
6435 /* If we have a filter, only list the breakpoints it accepts. */
6436 if (filter && !filter (b))
6437 continue;
6438
5c458ae8 6439 /* If we have a BP_NUM_LIST string, it is a list of breakpoints to
e5a67952 6440 accept. Skip the others. */
5c458ae8 6441 if (bp_num_list != NULL && *bp_num_list != '\0')
e5a67952 6442 {
5c458ae8 6443 if (show_internal && parse_and_eval_long (bp_num_list) != b->number)
e5a67952 6444 continue;
5c458ae8 6445 if (!show_internal && !number_is_in_list (bp_num_list, b->number))
e5a67952
MS
6446 continue;
6447 }
269b11a2 6448
5c458ae8 6449 if (show_internal || user_breakpoint_p (b))
e5a67952
MS
6450 {
6451 int addr_bit, type_len;
a6d9a66e 6452
e5a67952
MS
6453 addr_bit = breakpoint_address_bits (b);
6454 if (addr_bit > print_address_bits)
6455 print_address_bits = addr_bit;
269b11a2 6456
e5a67952
MS
6457 type_len = strlen (bptype_string (b->type));
6458 if (type_len > print_type_col_width)
6459 print_type_col_width = type_len;
6460
6461 nr_printable_breakpoints++;
6462 }
6463 }
7f3b0473 6464
4a2b031d
TT
6465 {
6466 ui_out_emit_table table_emitter (uiout,
6467 opts.addressprint ? 6 : 5,
6468 nr_printable_breakpoints,
6469 "BreakpointTable");
6470
6471 if (nr_printable_breakpoints > 0)
6472 annotate_breakpoints_headers ();
6473 if (nr_printable_breakpoints > 0)
6474 annotate_field (0);
6475 uiout->table_header (7, ui_left, "number", "Num"); /* 1 */
6476 if (nr_printable_breakpoints > 0)
6477 annotate_field (1);
6478 uiout->table_header (print_type_col_width, ui_left, "type", "Type"); /* 2 */
6479 if (nr_printable_breakpoints > 0)
6480 annotate_field (2);
6481 uiout->table_header (4, ui_left, "disp", "Disp"); /* 3 */
6482 if (nr_printable_breakpoints > 0)
6483 annotate_field (3);
6484 uiout->table_header (3, ui_left, "enabled", "Enb"); /* 4 */
6485 if (opts.addressprint)
6486 {
6487 if (nr_printable_breakpoints > 0)
6488 annotate_field (4);
6489 if (print_address_bits <= 32)
6490 uiout->table_header (10, ui_left, "addr", "Address"); /* 5 */
6491 else
6492 uiout->table_header (18, ui_left, "addr", "Address"); /* 5 */
6493 }
6494 if (nr_printable_breakpoints > 0)
6495 annotate_field (5);
6496 uiout->table_header (40, ui_noalign, "what", "What"); /* 6 */
6497 uiout->table_body ();
6498 if (nr_printable_breakpoints > 0)
6499 annotate_breakpoints_table ();
6500
6501 ALL_BREAKPOINTS (b)
6502 {
6503 QUIT;
6504 /* If we have a filter, only list the breakpoints it accepts. */
6505 if (filter && !filter (b))
6506 continue;
e5a67952 6507
5c458ae8 6508 /* If we have a BP_NUM_LIST string, it is a list of breakpoints to
4a2b031d 6509 accept. Skip the others. */
e5a67952 6510
5c458ae8 6511 if (bp_num_list != NULL && *bp_num_list != '\0')
4a2b031d 6512 {
5c458ae8 6513 if (show_internal) /* maintenance info breakpoint */
4a2b031d 6514 {
5c458ae8 6515 if (parse_and_eval_long (bp_num_list) != b->number)
4a2b031d
TT
6516 continue;
6517 }
6518 else /* all others */
6519 {
5c458ae8 6520 if (!number_is_in_list (bp_num_list, b->number))
4a2b031d
TT
6521 continue;
6522 }
6523 }
6524 /* We only print out user settable breakpoints unless the
5c458ae8
SM
6525 show_internal is set. */
6526 if (show_internal || user_breakpoint_p (b))
6527 print_one_breakpoint (b, &last_loc, show_internal);
4a2b031d
TT
6528 }
6529 }
698384cd 6530
7f3b0473 6531 if (nr_printable_breakpoints == 0)
c906108c 6532 {
4a64f543
MS
6533 /* If there's a filter, let the caller decide how to report
6534 empty list. */
d77f58be
SS
6535 if (!filter)
6536 {
5c458ae8 6537 if (bp_num_list == NULL || *bp_num_list == '\0')
112e8700 6538 uiout->message ("No breakpoints or watchpoints.\n");
d77f58be 6539 else
112e8700 6540 uiout->message ("No breakpoint or watchpoint matching '%s'.\n",
5c458ae8 6541 bp_num_list);
d77f58be 6542 }
c906108c
SS
6543 }
6544 else
c4093a6a 6545 {
a6d9a66e
UW
6546 if (last_loc && !server_command)
6547 set_next_address (last_loc->gdbarch, last_loc->address);
c4093a6a 6548 }
c906108c 6549
4a64f543 6550 /* FIXME? Should this be moved up so that it is only called when
c4093a6a 6551 there have been breakpoints? */
c906108c 6552 annotate_breakpoints_table_end ();
d77f58be
SS
6553
6554 return nr_printable_breakpoints;
c906108c
SS
6555}
6556
ad443146
SS
6557/* Display the value of default-collect in a way that is generally
6558 compatible with the breakpoint list. */
6559
6560static void
6561default_collect_info (void)
6562{
79a45e25
PA
6563 struct ui_out *uiout = current_uiout;
6564
ad443146
SS
6565 /* If it has no value (which is frequently the case), say nothing; a
6566 message like "No default-collect." gets in user's face when it's
6567 not wanted. */
6568 if (!*default_collect)
6569 return;
6570
6571 /* The following phrase lines up nicely with per-tracepoint collect
6572 actions. */
112e8700
SM
6573 uiout->text ("default collect ");
6574 uiout->field_string ("default-collect", default_collect);
6575 uiout->text (" \n");
ad443146
SS
6576}
6577
c906108c 6578static void
0b39b52e 6579info_breakpoints_command (const char *args, int from_tty)
c906108c 6580{
5c458ae8 6581 breakpoint_1 (args, false, NULL);
ad443146
SS
6582
6583 default_collect_info ();
d77f58be
SS
6584}
6585
6586static void
1d12d88f 6587info_watchpoints_command (const char *args, int from_tty)
d77f58be 6588{
5c458ae8 6589 int num_printed = breakpoint_1 (args, false, is_watchpoint);
79a45e25 6590 struct ui_out *uiout = current_uiout;
d77f58be
SS
6591
6592 if (num_printed == 0)
6593 {
e5a67952 6594 if (args == NULL || *args == '\0')
112e8700 6595 uiout->message ("No watchpoints.\n");
d77f58be 6596 else
112e8700 6597 uiout->message ("No watchpoint matching '%s'.\n", args);
d77f58be 6598 }
c906108c
SS
6599}
6600
7a292a7a 6601static void
4495129a 6602maintenance_info_breakpoints (const char *args, int from_tty)
c906108c 6603{
5c458ae8 6604 breakpoint_1 (args, true, NULL);
ad443146
SS
6605
6606 default_collect_info ();
c906108c
SS
6607}
6608
0d381245 6609static int
714835d5 6610breakpoint_has_pc (struct breakpoint *b,
6c95b8df 6611 struct program_space *pspace,
714835d5 6612 CORE_ADDR pc, struct obj_section *section)
0d381245
VP
6613{
6614 struct bp_location *bl = b->loc;
cc59ec59 6615
0d381245
VP
6616 for (; bl; bl = bl->next)
6617 {
6c95b8df
PA
6618 if (bl->pspace == pspace
6619 && bl->address == pc
0d381245
VP
6620 && (!overlay_debugging || bl->section == section))
6621 return 1;
6622 }
6623 return 0;
6624}
6625
672f9b60 6626/* Print a message describing any user-breakpoints set at PC. This
6c95b8df
PA
6627 concerns with logical breakpoints, so we match program spaces, not
6628 address spaces. */
c906108c
SS
6629
6630static void
6c95b8df
PA
6631describe_other_breakpoints (struct gdbarch *gdbarch,
6632 struct program_space *pspace, CORE_ADDR pc,
5af949e3 6633 struct obj_section *section, int thread)
c906108c 6634{
52f0bd74
AC
6635 int others = 0;
6636 struct breakpoint *b;
c906108c
SS
6637
6638 ALL_BREAKPOINTS (b)
672f9b60
KP
6639 others += (user_breakpoint_p (b)
6640 && breakpoint_has_pc (b, pspace, pc, section));
c906108c
SS
6641 if (others > 0)
6642 {
a3f17187
AC
6643 if (others == 1)
6644 printf_filtered (_("Note: breakpoint "));
6645 else /* if (others == ???) */
6646 printf_filtered (_("Note: breakpoints "));
c906108c 6647 ALL_BREAKPOINTS (b)
672f9b60 6648 if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
0d381245
VP
6649 {
6650 others--;
6651 printf_filtered ("%d", b->number);
6652 if (b->thread == -1 && thread != -1)
6653 printf_filtered (" (all threads)");
6654 else if (b->thread != -1)
6655 printf_filtered (" (thread %d)", b->thread);
6656 printf_filtered ("%s%s ",
059fb39f 6657 ((b->enable_state == bp_disabled
f8eba3c6 6658 || b->enable_state == bp_call_disabled)
0d381245 6659 ? " (disabled)"
0d381245
VP
6660 : ""),
6661 (others > 1) ? ","
6662 : ((others == 1) ? " and" : ""));
6663 }
6a831f06
PA
6664 current_uiout->message (_("also set at pc %ps.\n"),
6665 styled_string (address_style.style (),
6666 paddress (gdbarch, pc)));
c906108c
SS
6667 }
6668}
6669\f
c906108c 6670
cb1e4e32
PA
6671/* Return true iff it is meaningful to use the address member of LOC.
6672 For some breakpoint types, the locations' address members are
6673 irrelevant and it makes no sense to attempt to compare them to
6674 other addresses (or use them for any other purpose either).
2d134ed3 6675
cb1e4e32
PA
6676 More specifically, software watchpoints and catchpoints that are
6677 not backed by breakpoints always have a zero valued location
6678 address and we don't want to mark breakpoints of any of these types
6679 to be a duplicate of an actual breakpoint location at address
6680 zero. */
e4f237da 6681
cb1e4e32
PA
6682static bool
6683bl_address_is_meaningful (bp_location *loc)
e4f237da 6684{
cb1e4e32 6685 return loc->loc_type != bp_loc_other;
2d134ed3
PA
6686}
6687
6688/* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
6689 true if LOC1 and LOC2 represent the same watchpoint location. */
6690
6691static int
4a64f543
MS
6692watchpoint_locations_match (struct bp_location *loc1,
6693 struct bp_location *loc2)
2d134ed3 6694{
3a5c3e22
PA
6695 struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
6696 struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
6697
6698 /* Both of them must exist. */
6699 gdb_assert (w1 != NULL);
6700 gdb_assert (w2 != NULL);
2bdf28a0 6701
4a64f543
MS
6702 /* If the target can evaluate the condition expression in hardware,
6703 then we we need to insert both watchpoints even if they are at
6704 the same place. Otherwise the watchpoint will only trigger when
6705 the condition of whichever watchpoint was inserted evaluates to
6706 true, not giving a chance for GDB to check the condition of the
6707 other watchpoint. */
3a5c3e22 6708 if ((w1->cond_exp
4a64f543
MS
6709 && target_can_accel_watchpoint_condition (loc1->address,
6710 loc1->length,
0cf6dd15 6711 loc1->watchpoint_type,
4d01a485 6712 w1->cond_exp.get ()))
3a5c3e22 6713 || (w2->cond_exp
4a64f543
MS
6714 && target_can_accel_watchpoint_condition (loc2->address,
6715 loc2->length,
0cf6dd15 6716 loc2->watchpoint_type,
4d01a485 6717 w2->cond_exp.get ())))
0cf6dd15
TJB
6718 return 0;
6719
85d721b8
PA
6720 /* Note that this checks the owner's type, not the location's. In
6721 case the target does not support read watchpoints, but does
6722 support access watchpoints, we'll have bp_read_watchpoint
6723 watchpoints with hw_access locations. Those should be considered
6724 duplicates of hw_read locations. The hw_read locations will
6725 become hw_access locations later. */
2d134ed3
PA
6726 return (loc1->owner->type == loc2->owner->type
6727 && loc1->pspace->aspace == loc2->pspace->aspace
6728 && loc1->address == loc2->address
6729 && loc1->length == loc2->length);
e4f237da
KB
6730}
6731
31e77af2 6732/* See breakpoint.h. */
6c95b8df 6733
31e77af2 6734int
accd0bcd
YQ
6735breakpoint_address_match (const address_space *aspace1, CORE_ADDR addr1,
6736 const address_space *aspace2, CORE_ADDR addr2)
6c95b8df 6737{
f5656ead 6738 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
6c95b8df
PA
6739 || aspace1 == aspace2)
6740 && addr1 == addr2);
6741}
6742
f1310107
TJB
6743/* Returns true if {ASPACE2,ADDR2} falls within the range determined by
6744 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
6745 matches ASPACE2. On targets that have global breakpoints, the address
6746 space doesn't really matter. */
6747
6748static int
accd0bcd
YQ
6749breakpoint_address_match_range (const address_space *aspace1,
6750 CORE_ADDR addr1,
6751 int len1, const address_space *aspace2,
f1310107
TJB
6752 CORE_ADDR addr2)
6753{
f5656ead 6754 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
f1310107
TJB
6755 || aspace1 == aspace2)
6756 && addr2 >= addr1 && addr2 < addr1 + len1);
6757}
6758
6759/* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
6760 a ranged breakpoint. In most targets, a match happens only if ASPACE
6761 matches the breakpoint's address space. On targets that have global
6762 breakpoints, the address space doesn't really matter. */
6763
6764static int
6765breakpoint_location_address_match (struct bp_location *bl,
accd0bcd 6766 const address_space *aspace,
f1310107
TJB
6767 CORE_ADDR addr)
6768{
6769 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
6770 aspace, addr)
6771 || (bl->length
6772 && breakpoint_address_match_range (bl->pspace->aspace,
6773 bl->address, bl->length,
6774 aspace, addr)));
6775}
6776
d35ae833
PA
6777/* Returns true if the [ADDR,ADDR+LEN) range in ASPACE overlaps
6778 breakpoint BL. BL may be a ranged breakpoint. In most targets, a
6779 match happens only if ASPACE matches the breakpoint's address
6780 space. On targets that have global breakpoints, the address space
6781 doesn't really matter. */
6782
6783static int
6784breakpoint_location_address_range_overlap (struct bp_location *bl,
accd0bcd 6785 const address_space *aspace,
d35ae833
PA
6786 CORE_ADDR addr, int len)
6787{
6788 if (gdbarch_has_global_breakpoints (target_gdbarch ())
6789 || bl->pspace->aspace == aspace)
6790 {
6791 int bl_len = bl->length != 0 ? bl->length : 1;
6792
6793 if (mem_ranges_overlap (addr, len, bl->address, bl_len))
6794 return 1;
6795 }
6796 return 0;
6797}
6798
1e4d1764
YQ
6799/* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
6800 Then, if LOC1 and LOC2 represent the same tracepoint location, returns
6801 true, otherwise returns false. */
6802
6803static int
6804tracepoint_locations_match (struct bp_location *loc1,
6805 struct bp_location *loc2)
6806{
6807 if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
6808 /* Since tracepoint locations are never duplicated with others', tracepoint
6809 locations at the same address of different tracepoints are regarded as
6810 different locations. */
6811 return (loc1->address == loc2->address && loc1->owner == loc2->owner);
6812 else
6813 return 0;
6814}
6815
2d134ed3 6816/* Assuming LOC1 and LOC2's types' have meaningful target addresses
cb1e4e32
PA
6817 (bl_address_is_meaningful), returns true if LOC1 and LOC2 represent
6818 the same location. */
2d134ed3
PA
6819
6820static int
4a64f543
MS
6821breakpoint_locations_match (struct bp_location *loc1,
6822 struct bp_location *loc2)
2d134ed3 6823{
2bdf28a0
JK
6824 int hw_point1, hw_point2;
6825
6826 /* Both of them must not be in moribund_locations. */
6827 gdb_assert (loc1->owner != NULL);
6828 gdb_assert (loc2->owner != NULL);
6829
6830 hw_point1 = is_hardware_watchpoint (loc1->owner);
6831 hw_point2 = is_hardware_watchpoint (loc2->owner);
2d134ed3
PA
6832
6833 if (hw_point1 != hw_point2)
6834 return 0;
6835 else if (hw_point1)
6836 return watchpoint_locations_match (loc1, loc2);
1e4d1764
YQ
6837 else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
6838 return tracepoint_locations_match (loc1, loc2);
2d134ed3 6839 else
f1310107
TJB
6840 /* We compare bp_location.length in order to cover ranged breakpoints. */
6841 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
6842 loc2->pspace->aspace, loc2->address)
6843 && loc1->length == loc2->length);
2d134ed3
PA
6844}
6845
76897487
KB
6846static void
6847breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
6848 int bnum, int have_bnum)
6849{
f63fbe86
MS
6850 /* The longest string possibly returned by hex_string_custom
6851 is 50 chars. These must be at least that big for safety. */
6852 char astr1[64];
6853 char astr2[64];
76897487 6854
bb599908
PH
6855 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
6856 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
76897487 6857 if (have_bnum)
8a3fe4f8 6858 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
76897487
KB
6859 bnum, astr1, astr2);
6860 else
8a3fe4f8 6861 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
76897487
KB
6862}
6863
4a64f543
MS
6864/* Adjust a breakpoint's address to account for architectural
6865 constraints on breakpoint placement. Return the adjusted address.
6866 Note: Very few targets require this kind of adjustment. For most
6867 targets, this function is simply the identity function. */
76897487
KB
6868
6869static CORE_ADDR
a6d9a66e
UW
6870adjust_breakpoint_address (struct gdbarch *gdbarch,
6871 CORE_ADDR bpaddr, enum bptype bptype)
76897487 6872{
a0de8c21
YQ
6873 if (bptype == bp_watchpoint
6874 || bptype == bp_hardware_watchpoint
6875 || bptype == bp_read_watchpoint
6876 || bptype == bp_access_watchpoint
6877 || bptype == bp_catchpoint)
88f7da05
KB
6878 {
6879 /* Watchpoints and the various bp_catch_* eventpoints should not
6880 have their addresses modified. */
6881 return bpaddr;
6882 }
7c16b83e
PA
6883 else if (bptype == bp_single_step)
6884 {
6885 /* Single-step breakpoints should not have their addresses
6886 modified. If there's any architectural constrain that
6887 applies to this address, then it should have already been
6888 taken into account when the breakpoint was created in the
6889 first place. If we didn't do this, stepping through e.g.,
6890 Thumb-2 IT blocks would break. */
6891 return bpaddr;
6892 }
76897487
KB
6893 else
6894 {
a0de8c21
YQ
6895 CORE_ADDR adjusted_bpaddr = bpaddr;
6896
6897 if (gdbarch_adjust_breakpoint_address_p (gdbarch))
6898 {
6899 /* Some targets have architectural constraints on the placement
6900 of breakpoint instructions. Obtain the adjusted address. */
6901 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
6902 }
76897487 6903
a0de8c21 6904 adjusted_bpaddr = address_significant (gdbarch, adjusted_bpaddr);
76897487
KB
6905
6906 /* An adjusted breakpoint address can significantly alter
6907 a user's expectations. Print a warning if an adjustment
6908 is required. */
6909 if (adjusted_bpaddr != bpaddr)
6910 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
6911
6912 return adjusted_bpaddr;
6913 }
6914}
6915
cb1e4e32
PA
6916static bp_loc_type
6917bp_location_from_bp_type (bptype type)
7cc221ef 6918{
cb1e4e32 6919 switch (type)
e049a4b5
DJ
6920 {
6921 case bp_breakpoint:
7c16b83e 6922 case bp_single_step:
e049a4b5
DJ
6923 case bp_until:
6924 case bp_finish:
6925 case bp_longjmp:
6926 case bp_longjmp_resume:
e2e4d78b 6927 case bp_longjmp_call_dummy:
186c406b
TT
6928 case bp_exception:
6929 case bp_exception_resume:
e049a4b5 6930 case bp_step_resume:
2c03e5be 6931 case bp_hp_step_resume:
e049a4b5
DJ
6932 case bp_watchpoint_scope:
6933 case bp_call_dummy:
aa7d318d 6934 case bp_std_terminate:
e049a4b5
DJ
6935 case bp_shlib_event:
6936 case bp_thread_event:
6937 case bp_overlay_event:
4efc6507 6938 case bp_jit_event:
0fd8e87f 6939 case bp_longjmp_master:
aa7d318d 6940 case bp_std_terminate_master:
186c406b 6941 case bp_exception_master:
0e30163f
JK
6942 case bp_gnu_ifunc_resolver:
6943 case bp_gnu_ifunc_resolver_return:
e7e0cddf 6944 case bp_dprintf:
cb1e4e32 6945 return bp_loc_software_breakpoint;
e049a4b5 6946 case bp_hardware_breakpoint:
cb1e4e32 6947 return bp_loc_hardware_breakpoint;
e049a4b5
DJ
6948 case bp_hardware_watchpoint:
6949 case bp_read_watchpoint:
6950 case bp_access_watchpoint:
cb1e4e32 6951 return bp_loc_hardware_watchpoint;
e049a4b5 6952 case bp_watchpoint:
ce78b96d 6953 case bp_catchpoint:
15c3d785
PA
6954 case bp_tracepoint:
6955 case bp_fast_tracepoint:
0fb4aa4b 6956 case bp_static_tracepoint:
cb1e4e32 6957 return bp_loc_other;
e049a4b5 6958 default:
e2e0b3e5 6959 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
e049a4b5 6960 }
cb1e4e32
PA
6961}
6962
6963bp_location::bp_location (breakpoint *owner, bp_loc_type type)
6964{
6965 this->owner = owner;
6966 this->cond_bytecode = NULL;
6967 this->shlib_disabled = 0;
6968 this->enabled = 1;
6969
6970 this->loc_type = type;
e049a4b5 6971
cb1e4e32
PA
6972 if (this->loc_type == bp_loc_software_breakpoint
6973 || this->loc_type == bp_loc_hardware_breakpoint)
6974 mark_breakpoint_location_modified (this);
6975
6976 this->refc = 1;
6977}
6978
6979bp_location::bp_location (breakpoint *owner)
6980 : bp_location::bp_location (owner,
6981 bp_location_from_bp_type (owner->type))
6982{
28010a5d
PA
6983}
6984
6985/* Allocate a struct bp_location. */
6986
6987static struct bp_location *
6988allocate_bp_location (struct breakpoint *bpt)
6989{
348d480f
PA
6990 return bpt->ops->allocate_location (bpt);
6991}
7cc221ef 6992
f431efe5
PA
6993static void
6994free_bp_location (struct bp_location *loc)
fe3f5fa8 6995{
4d01a485 6996 delete loc;
fe3f5fa8
VP
6997}
6998
f431efe5
PA
6999/* Increment reference count. */
7000
7001static void
7002incref_bp_location (struct bp_location *bl)
7003{
7004 ++bl->refc;
7005}
7006
7007/* Decrement reference count. If the reference count reaches 0,
7008 destroy the bp_location. Sets *BLP to NULL. */
7009
7010static void
7011decref_bp_location (struct bp_location **blp)
7012{
0807b50c
PA
7013 gdb_assert ((*blp)->refc > 0);
7014
f431efe5
PA
7015 if (--(*blp)->refc == 0)
7016 free_bp_location (*blp);
7017 *blp = NULL;
7018}
7019
346774a9 7020/* Add breakpoint B at the end of the global breakpoint chain. */
c906108c 7021
b270e6f9
TT
7022static breakpoint *
7023add_to_breakpoint_chain (std::unique_ptr<breakpoint> &&b)
c906108c 7024{
346774a9 7025 struct breakpoint *b1;
b270e6f9 7026 struct breakpoint *result = b.get ();
c906108c 7027
346774a9
PA
7028 /* Add this breakpoint to the end of the chain so that a list of
7029 breakpoints will come out in order of increasing numbers. */
7030
7031 b1 = breakpoint_chain;
7032 if (b1 == 0)
b270e6f9 7033 breakpoint_chain = b.release ();
346774a9
PA
7034 else
7035 {
7036 while (b1->next)
7037 b1 = b1->next;
b270e6f9 7038 b1->next = b.release ();
346774a9 7039 }
b270e6f9
TT
7040
7041 return result;
346774a9
PA
7042}
7043
7044/* Initializes breakpoint B with type BPTYPE and no locations yet. */
7045
7046static void
7047init_raw_breakpoint_without_location (struct breakpoint *b,
7048 struct gdbarch *gdbarch,
28010a5d 7049 enum bptype bptype,
c0a91b2b 7050 const struct breakpoint_ops *ops)
346774a9 7051{
348d480f
PA
7052 gdb_assert (ops != NULL);
7053
28010a5d 7054 b->ops = ops;
4d28f7a8 7055 b->type = bptype;
a6d9a66e 7056 b->gdbarch = gdbarch;
c906108c
SS
7057 b->language = current_language->la_language;
7058 b->input_radix = input_radix;
d0fb5eae 7059 b->related_breakpoint = b;
346774a9
PA
7060}
7061
7062/* Helper to set_raw_breakpoint below. Creates a breakpoint
7063 that has type BPTYPE and has no locations as yet. */
346774a9
PA
7064
7065static struct breakpoint *
7066set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
348d480f 7067 enum bptype bptype,
c0a91b2b 7068 const struct breakpoint_ops *ops)
346774a9 7069{
3b0871f4 7070 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (bptype);
346774a9 7071
3b0871f4 7072 init_raw_breakpoint_without_location (b.get (), gdbarch, bptype, ops);
b270e6f9 7073 return add_to_breakpoint_chain (std::move (b));
0d381245
VP
7074}
7075
0ba852ab 7076/* Initialize loc->function_name. */
0e30163f 7077
0d381245 7078static void
0ba852ab 7079set_breakpoint_location_function (struct bp_location *loc)
0d381245 7080{
2bdf28a0
JK
7081 gdb_assert (loc->owner != NULL);
7082
0d381245 7083 if (loc->owner->type == bp_breakpoint
1042e4c0 7084 || loc->owner->type == bp_hardware_breakpoint
d77f58be 7085 || is_tracepoint (loc->owner))
0d381245 7086 {
2c02bd72 7087 const char *function_name;
0e30163f 7088
3467ec66 7089 if (loc->msymbol != NULL
f50776aa 7090 && (MSYMBOL_TYPE (loc->msymbol) == mst_text_gnu_ifunc
0ba852ab 7091 || MSYMBOL_TYPE (loc->msymbol) == mst_data_gnu_ifunc))
0e30163f
JK
7092 {
7093 struct breakpoint *b = loc->owner;
7094
c9d95fa3 7095 function_name = loc->msymbol->linkage_name ();
3467ec66
PA
7096
7097 if (b->type == bp_breakpoint && b->loc == loc
7098 && loc->next == NULL && b->related_breakpoint == b)
0e30163f
JK
7099 {
7100 /* Create only the whole new breakpoint of this type but do not
7101 mess more complicated breakpoints with multiple locations. */
7102 b->type = bp_gnu_ifunc_resolver;
6a3a010b
MR
7103 /* Remember the resolver's address for use by the return
7104 breakpoint. */
3467ec66 7105 loc->related_address = loc->address;
0e30163f
JK
7106 }
7107 }
3467ec66
PA
7108 else
7109 find_pc_partial_function (loc->address, &function_name, NULL, NULL);
0e30163f 7110
2c02bd72
DE
7111 if (function_name)
7112 loc->function_name = xstrdup (function_name);
0d381245
VP
7113 }
7114}
7115
a6d9a66e 7116/* Attempt to determine architecture of location identified by SAL. */
1bfeeb0f 7117struct gdbarch *
a6d9a66e
UW
7118get_sal_arch (struct symtab_and_line sal)
7119{
7120 if (sal.section)
7121 return get_objfile_arch (sal.section->objfile);
7122 if (sal.symtab)
eb822aa6 7123 return get_objfile_arch (SYMTAB_OBJFILE (sal.symtab));
a6d9a66e
UW
7124
7125 return NULL;
7126}
7127
346774a9
PA
7128/* Low level routine for partially initializing a breakpoint of type
7129 BPTYPE. The newly created breakpoint's address, section, source
c56053d2 7130 file name, and line number are provided by SAL.
0d381245
VP
7131
7132 It is expected that the caller will complete the initialization of
7133 the newly created breakpoint struct as well as output any status
c56053d2 7134 information regarding the creation of a new breakpoint. */
0d381245 7135
346774a9
PA
7136static void
7137init_raw_breakpoint (struct breakpoint *b, struct gdbarch *gdbarch,
28010a5d 7138 struct symtab_and_line sal, enum bptype bptype,
c0a91b2b 7139 const struct breakpoint_ops *ops)
0d381245 7140{
28010a5d 7141 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
346774a9 7142
3742cc8b 7143 add_location_to_breakpoint (b, &sal);
0d381245 7144
6c95b8df
PA
7145 if (bptype != bp_catchpoint)
7146 gdb_assert (sal.pspace != NULL);
7147
f8eba3c6
TT
7148 /* Store the program space that was used to set the breakpoint,
7149 except for ordinary breakpoints, which are independent of the
7150 program space. */
7151 if (bptype != bp_breakpoint && bptype != bp_hardware_breakpoint)
7152 b->pspace = sal.pspace;
346774a9 7153}
c906108c 7154
346774a9
PA
7155/* set_raw_breakpoint is a low level routine for allocating and
7156 partially initializing a breakpoint of type BPTYPE. The newly
7157 created breakpoint's address, section, source file name, and line
7158 number are provided by SAL. The newly created and partially
7159 initialized breakpoint is added to the breakpoint chain and
7160 is also returned as the value of this function.
7161
7162 It is expected that the caller will complete the initialization of
7163 the newly created breakpoint struct as well as output any status
7164 information regarding the creation of a new breakpoint. In
7165 particular, set_raw_breakpoint does NOT set the breakpoint
7166 number! Care should be taken to not allow an error to occur
7167 prior to completing the initialization of the breakpoint. If this
7168 should happen, a bogus breakpoint will be left on the chain. */
7169
7170struct breakpoint *
7171set_raw_breakpoint (struct gdbarch *gdbarch,
348d480f 7172 struct symtab_and_line sal, enum bptype bptype,
c0a91b2b 7173 const struct breakpoint_ops *ops)
346774a9 7174{
3b0871f4 7175 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (bptype);
346774a9 7176
3b0871f4 7177 init_raw_breakpoint (b.get (), gdbarch, sal, bptype, ops);
b270e6f9 7178 return add_to_breakpoint_chain (std::move (b));
c906108c
SS
7179}
7180
53a5351d 7181/* Call this routine when stepping and nexting to enable a breakpoint
186c406b
TT
7182 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
7183 initiated the operation. */
c906108c
SS
7184
7185void
186c406b 7186set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
c906108c 7187{
35df4500 7188 struct breakpoint *b, *b_tmp;
5d5658a1 7189 int thread = tp->global_num;
0fd8e87f
UW
7190
7191 /* To avoid having to rescan all objfile symbols at every step,
7192 we maintain a list of continually-inserted but always disabled
7193 longjmp "master" breakpoints. Here, we simply create momentary
7194 clones of those and enable them for the requested thread. */
35df4500 7195 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df 7196 if (b->pspace == current_program_space
186c406b
TT
7197 && (b->type == bp_longjmp_master
7198 || b->type == bp_exception_master))
0fd8e87f 7199 {
06edf0c0
PA
7200 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
7201 struct breakpoint *clone;
cc59ec59 7202
e2e4d78b
JK
7203 /* longjmp_breakpoint_ops ensures INITIATING_FRAME is cleared again
7204 after their removal. */
06edf0c0 7205 clone = momentary_breakpoint_from_master (b, type,
c1fc2657 7206 &momentary_breakpoint_ops, 1);
0fd8e87f
UW
7207 clone->thread = thread;
7208 }
186c406b
TT
7209
7210 tp->initiating_frame = frame;
c906108c
SS
7211}
7212
611c83ae 7213/* Delete all longjmp breakpoints from THREAD. */
c906108c 7214void
611c83ae 7215delete_longjmp_breakpoint (int thread)
c906108c 7216{
35df4500 7217 struct breakpoint *b, *b_tmp;
c906108c 7218
35df4500 7219 ALL_BREAKPOINTS_SAFE (b, b_tmp)
186c406b 7220 if (b->type == bp_longjmp || b->type == bp_exception)
611c83ae
PA
7221 {
7222 if (b->thread == thread)
7223 delete_breakpoint (b);
7224 }
c906108c
SS
7225}
7226
f59f708a
PA
7227void
7228delete_longjmp_breakpoint_at_next_stop (int thread)
7229{
7230 struct breakpoint *b, *b_tmp;
7231
7232 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7233 if (b->type == bp_longjmp || b->type == bp_exception)
7234 {
7235 if (b->thread == thread)
7236 b->disposition = disp_del_at_next_stop;
7237 }
7238}
7239
e2e4d78b
JK
7240/* Place breakpoints of type bp_longjmp_call_dummy to catch longjmp for
7241 INFERIOR_PTID thread. Chain them all by RELATED_BREAKPOINT and return
7242 pointer to any of them. Return NULL if this system cannot place longjmp
7243 breakpoints. */
7244
7245struct breakpoint *
7246set_longjmp_breakpoint_for_call_dummy (void)
7247{
7248 struct breakpoint *b, *retval = NULL;
7249
7250 ALL_BREAKPOINTS (b)
7251 if (b->pspace == current_program_space && b->type == bp_longjmp_master)
7252 {
7253 struct breakpoint *new_b;
7254
7255 new_b = momentary_breakpoint_from_master (b, bp_longjmp_call_dummy,
a1aa2221
LM
7256 &momentary_breakpoint_ops,
7257 1);
00431a78 7258 new_b->thread = inferior_thread ()->global_num;
e2e4d78b
JK
7259
7260 /* Link NEW_B into the chain of RETVAL breakpoints. */
7261
7262 gdb_assert (new_b->related_breakpoint == new_b);
7263 if (retval == NULL)
7264 retval = new_b;
7265 new_b->related_breakpoint = retval;
7266 while (retval->related_breakpoint != new_b->related_breakpoint)
7267 retval = retval->related_breakpoint;
7268 retval->related_breakpoint = new_b;
7269 }
7270
7271 return retval;
7272}
7273
7274/* Verify all existing dummy frames and their associated breakpoints for
b67a2c6f 7275 TP. Remove those which can no longer be found in the current frame
e2e4d78b
JK
7276 stack.
7277
7278 You should call this function only at places where it is safe to currently
7279 unwind the whole stack. Failed stack unwind would discard live dummy
7280 frames. */
7281
7282void
b67a2c6f 7283check_longjmp_breakpoint_for_call_dummy (struct thread_info *tp)
e2e4d78b
JK
7284{
7285 struct breakpoint *b, *b_tmp;
7286
7287 ALL_BREAKPOINTS_SAFE (b, b_tmp)
5d5658a1 7288 if (b->type == bp_longjmp_call_dummy && b->thread == tp->global_num)
e2e4d78b
JK
7289 {
7290 struct breakpoint *dummy_b = b->related_breakpoint;
7291
7292 while (dummy_b != b && dummy_b->type != bp_call_dummy)
7293 dummy_b = dummy_b->related_breakpoint;
7294 if (dummy_b->type != bp_call_dummy
7295 || frame_find_by_id (dummy_b->frame_id) != NULL)
7296 continue;
7297
00431a78 7298 dummy_frame_discard (dummy_b->frame_id, tp);
e2e4d78b
JK
7299
7300 while (b->related_breakpoint != b)
7301 {
7302 if (b_tmp == b->related_breakpoint)
7303 b_tmp = b->related_breakpoint->next;
7304 delete_breakpoint (b->related_breakpoint);
7305 }
7306 delete_breakpoint (b);
7307 }
7308}
7309
1900040c
MS
7310void
7311enable_overlay_breakpoints (void)
7312{
52f0bd74 7313 struct breakpoint *b;
1900040c
MS
7314
7315 ALL_BREAKPOINTS (b)
7316 if (b->type == bp_overlay_event)
7317 {
7318 b->enable_state = bp_enabled;
44702360 7319 update_global_location_list (UGLL_MAY_INSERT);
c02f5703 7320 overlay_events_enabled = 1;
1900040c
MS
7321 }
7322}
7323
7324void
7325disable_overlay_breakpoints (void)
7326{
52f0bd74 7327 struct breakpoint *b;
1900040c
MS
7328
7329 ALL_BREAKPOINTS (b)
7330 if (b->type == bp_overlay_event)
7331 {
7332 b->enable_state = bp_disabled;
44702360 7333 update_global_location_list (UGLL_DONT_INSERT);
c02f5703 7334 overlay_events_enabled = 0;
1900040c
MS
7335 }
7336}
7337
aa7d318d
TT
7338/* Set an active std::terminate breakpoint for each std::terminate
7339 master breakpoint. */
7340void
7341set_std_terminate_breakpoint (void)
7342{
35df4500 7343 struct breakpoint *b, *b_tmp;
aa7d318d 7344
35df4500 7345 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
7346 if (b->pspace == current_program_space
7347 && b->type == bp_std_terminate_master)
7348 {
06edf0c0 7349 momentary_breakpoint_from_master (b, bp_std_terminate,
a1aa2221 7350 &momentary_breakpoint_ops, 1);
aa7d318d
TT
7351 }
7352}
7353
7354/* Delete all the std::terminate breakpoints. */
7355void
7356delete_std_terminate_breakpoint (void)
7357{
35df4500 7358 struct breakpoint *b, *b_tmp;
aa7d318d 7359
35df4500 7360 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
7361 if (b->type == bp_std_terminate)
7362 delete_breakpoint (b);
7363}
7364
c4093a6a 7365struct breakpoint *
a6d9a66e 7366create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
c4093a6a
JM
7367{
7368 struct breakpoint *b;
c4093a6a 7369
06edf0c0
PA
7370 b = create_internal_breakpoint (gdbarch, address, bp_thread_event,
7371 &internal_breakpoint_ops);
7372
b5de0fa7 7373 b->enable_state = bp_enabled;
f00aae0f 7374 /* location has to be used or breakpoint_re_set will delete me. */
d28cd78a 7375 b->location = new_address_location (b->loc->address, NULL, 0);
c4093a6a 7376
44702360 7377 update_global_location_list_nothrow (UGLL_MAY_INSERT);
74960c60 7378
c4093a6a
JM
7379 return b;
7380}
7381
0101ce28
JJ
7382struct lang_and_radix
7383 {
7384 enum language lang;
7385 int radix;
7386 };
7387
4efc6507
DE
7388/* Create a breakpoint for JIT code registration and unregistration. */
7389
7390struct breakpoint *
7391create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7392{
2a7f3dff
PA
7393 return create_internal_breakpoint (gdbarch, address, bp_jit_event,
7394 &internal_breakpoint_ops);
4efc6507 7395}
0101ce28 7396
03673fc7
PP
7397/* Remove JIT code registration and unregistration breakpoint(s). */
7398
7399void
7400remove_jit_event_breakpoints (void)
7401{
7402 struct breakpoint *b, *b_tmp;
7403
7404 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7405 if (b->type == bp_jit_event
7406 && b->loc->pspace == current_program_space)
7407 delete_breakpoint (b);
7408}
7409
cae688ec
JJ
7410void
7411remove_solib_event_breakpoints (void)
7412{
35df4500 7413 struct breakpoint *b, *b_tmp;
cae688ec 7414
35df4500 7415 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df
PA
7416 if (b->type == bp_shlib_event
7417 && b->loc->pspace == current_program_space)
cae688ec
JJ
7418 delete_breakpoint (b);
7419}
7420
f37f681c
PA
7421/* See breakpoint.h. */
7422
7423void
7424remove_solib_event_breakpoints_at_next_stop (void)
7425{
7426 struct breakpoint *b, *b_tmp;
7427
7428 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7429 if (b->type == bp_shlib_event
7430 && b->loc->pspace == current_program_space)
7431 b->disposition = disp_del_at_next_stop;
7432}
7433
04086b45
PA
7434/* Helper for create_solib_event_breakpoint /
7435 create_and_insert_solib_event_breakpoint. Allows specifying which
7436 INSERT_MODE to pass through to update_global_location_list. */
7437
7438static struct breakpoint *
7439create_solib_event_breakpoint_1 (struct gdbarch *gdbarch, CORE_ADDR address,
7440 enum ugll_insert_mode insert_mode)
cae688ec
JJ
7441{
7442 struct breakpoint *b;
7443
06edf0c0
PA
7444 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event,
7445 &internal_breakpoint_ops);
04086b45 7446 update_global_location_list_nothrow (insert_mode);
cae688ec
JJ
7447 return b;
7448}
7449
04086b45
PA
7450struct breakpoint *
7451create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7452{
7453 return create_solib_event_breakpoint_1 (gdbarch, address, UGLL_MAY_INSERT);
7454}
7455
f37f681c
PA
7456/* See breakpoint.h. */
7457
7458struct breakpoint *
7459create_and_insert_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7460{
7461 struct breakpoint *b;
7462
04086b45
PA
7463 /* Explicitly tell update_global_location_list to insert
7464 locations. */
7465 b = create_solib_event_breakpoint_1 (gdbarch, address, UGLL_INSERT);
f37f681c
PA
7466 if (!b->loc->inserted)
7467 {
7468 delete_breakpoint (b);
7469 return NULL;
7470 }
7471 return b;
7472}
7473
cae688ec
JJ
7474/* Disable any breakpoints that are on code in shared libraries. Only
7475 apply to enabled breakpoints, disabled ones can just stay disabled. */
7476
7477void
cb851954 7478disable_breakpoints_in_shlibs (void)
cae688ec 7479{
876fa593 7480 struct bp_location *loc, **locp_tmp;
cae688ec 7481
876fa593 7482 ALL_BP_LOCATIONS (loc, locp_tmp)
cae688ec 7483 {
2bdf28a0 7484 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 7485 struct breakpoint *b = loc->owner;
2bdf28a0 7486
4a64f543
MS
7487 /* We apply the check to all breakpoints, including disabled for
7488 those with loc->duplicate set. This is so that when breakpoint
7489 becomes enabled, or the duplicate is removed, gdb will try to
7490 insert all breakpoints. If we don't set shlib_disabled here,
7491 we'll try to insert those breakpoints and fail. */
1042e4c0 7492 if (((b->type == bp_breakpoint)
508ccb1f 7493 || (b->type == bp_jit_event)
1042e4c0 7494 || (b->type == bp_hardware_breakpoint)
d77f58be 7495 || (is_tracepoint (b)))
6c95b8df 7496 && loc->pspace == current_program_space
0d381245 7497 && !loc->shlib_disabled
6c95b8df 7498 && solib_name_from_address (loc->pspace, loc->address)
a77053c2 7499 )
0d381245
VP
7500 {
7501 loc->shlib_disabled = 1;
7502 }
cae688ec
JJ
7503 }
7504}
7505
63644780
NB
7506/* Disable any breakpoints and tracepoints that are in SOLIB upon
7507 notification of unloaded_shlib. Only apply to enabled breakpoints,
7508 disabled ones can just stay disabled. */
84acb35a 7509
75149521 7510static void
84acb35a
JJ
7511disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
7512{
876fa593 7513 struct bp_location *loc, **locp_tmp;
84acb35a
JJ
7514 int disabled_shlib_breaks = 0;
7515
876fa593 7516 ALL_BP_LOCATIONS (loc, locp_tmp)
84acb35a 7517 {
2bdf28a0 7518 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 7519 struct breakpoint *b = loc->owner;
cc59ec59 7520
1e4d1764 7521 if (solib->pspace == loc->pspace
e2dd7057 7522 && !loc->shlib_disabled
1e4d1764
YQ
7523 && (((b->type == bp_breakpoint
7524 || b->type == bp_jit_event
7525 || b->type == bp_hardware_breakpoint)
7526 && (loc->loc_type == bp_loc_hardware_breakpoint
7527 || loc->loc_type == bp_loc_software_breakpoint))
7528 || is_tracepoint (b))
e2dd7057 7529 && solib_contains_address_p (solib, loc->address))
84acb35a 7530 {
e2dd7057
PP
7531 loc->shlib_disabled = 1;
7532 /* At this point, we cannot rely on remove_breakpoint
7533 succeeding so we must mark the breakpoint as not inserted
7534 to prevent future errors occurring in remove_breakpoints. */
7535 loc->inserted = 0;
8d3788bd
VP
7536
7537 /* This may cause duplicate notifications for the same breakpoint. */
76727919 7538 gdb::observers::breakpoint_modified.notify (b);
8d3788bd 7539
e2dd7057
PP
7540 if (!disabled_shlib_breaks)
7541 {
223ffa71 7542 target_terminal::ours_for_output ();
3e43a32a
MS
7543 warning (_("Temporarily disabling breakpoints "
7544 "for unloaded shared library \"%s\""),
e2dd7057 7545 solib->so_name);
84acb35a 7546 }
e2dd7057 7547 disabled_shlib_breaks = 1;
84acb35a
JJ
7548 }
7549 }
84acb35a
JJ
7550}
7551
63644780
NB
7552/* Disable any breakpoints and tracepoints in OBJFILE upon
7553 notification of free_objfile. Only apply to enabled breakpoints,
7554 disabled ones can just stay disabled. */
7555
7556static void
7557disable_breakpoints_in_freed_objfile (struct objfile *objfile)
7558{
7559 struct breakpoint *b;
7560
7561 if (objfile == NULL)
7562 return;
7563
d03de421
PA
7564 /* OBJF_SHARED|OBJF_USERLOADED objfiles are dynamic modules manually
7565 managed by the user with add-symbol-file/remove-symbol-file.
7566 Similarly to how breakpoints in shared libraries are handled in
7567 response to "nosharedlibrary", mark breakpoints in such modules
08351840
PA
7568 shlib_disabled so they end up uninserted on the next global
7569 location list update. Shared libraries not loaded by the user
7570 aren't handled here -- they're already handled in
7571 disable_breakpoints_in_unloaded_shlib, called by solib.c's
7572 solib_unloaded observer. We skip objfiles that are not
d03de421
PA
7573 OBJF_SHARED as those aren't considered dynamic objects (e.g. the
7574 main objfile). */
7575 if ((objfile->flags & OBJF_SHARED) == 0
7576 || (objfile->flags & OBJF_USERLOADED) == 0)
63644780
NB
7577 return;
7578
7579 ALL_BREAKPOINTS (b)
7580 {
7581 struct bp_location *loc;
7582 int bp_modified = 0;
7583
7584 if (!is_breakpoint (b) && !is_tracepoint (b))
7585 continue;
7586
7587 for (loc = b->loc; loc != NULL; loc = loc->next)
7588 {
7589 CORE_ADDR loc_addr = loc->address;
7590
7591 if (loc->loc_type != bp_loc_hardware_breakpoint
7592 && loc->loc_type != bp_loc_software_breakpoint)
7593 continue;
7594
7595 if (loc->shlib_disabled != 0)
7596 continue;
7597
7598 if (objfile->pspace != loc->pspace)
7599 continue;
7600
7601 if (loc->loc_type != bp_loc_hardware_breakpoint
7602 && loc->loc_type != bp_loc_software_breakpoint)
7603 continue;
7604
7605 if (is_addr_in_objfile (loc_addr, objfile))
7606 {
7607 loc->shlib_disabled = 1;
08351840
PA
7608 /* At this point, we don't know whether the object was
7609 unmapped from the inferior or not, so leave the
7610 inserted flag alone. We'll handle failure to
7611 uninsert quietly, in case the object was indeed
7612 unmapped. */
63644780
NB
7613
7614 mark_breakpoint_location_modified (loc);
7615
7616 bp_modified = 1;
7617 }
7618 }
7619
7620 if (bp_modified)
76727919 7621 gdb::observers::breakpoint_modified.notify (b);
63644780
NB
7622 }
7623}
7624
ce78b96d
JB
7625/* FORK & VFORK catchpoints. */
7626
e29a4733 7627/* An instance of this type is used to represent a fork or vfork
c1fc2657
SM
7628 catchpoint. A breakpoint is really of this type iff its ops pointer points
7629 to CATCH_FORK_BREAKPOINT_OPS. */
e29a4733 7630
c1fc2657 7631struct fork_catchpoint : public breakpoint
e29a4733 7632{
e29a4733
PA
7633 /* Process id of a child process whose forking triggered this
7634 catchpoint. This field is only valid immediately after this
7635 catchpoint has triggered. */
7636 ptid_t forked_inferior_pid;
7637};
7638
4a64f543
MS
7639/* Implement the "insert" breakpoint_ops method for fork
7640 catchpoints. */
ce78b96d 7641
77b06cd7
TJB
7642static int
7643insert_catch_fork (struct bp_location *bl)
ce78b96d 7644{
e99b03dc 7645 return target_insert_fork_catchpoint (inferior_ptid.pid ());
ce78b96d
JB
7646}
7647
4a64f543
MS
7648/* Implement the "remove" breakpoint_ops method for fork
7649 catchpoints. */
ce78b96d
JB
7650
7651static int
73971819 7652remove_catch_fork (struct bp_location *bl, enum remove_bp_reason reason)
ce78b96d 7653{
e99b03dc 7654 return target_remove_fork_catchpoint (inferior_ptid.pid ());
ce78b96d
JB
7655}
7656
7657/* Implement the "breakpoint_hit" breakpoint_ops method for fork
7658 catchpoints. */
7659
7660static int
f1310107 7661breakpoint_hit_catch_fork (const struct bp_location *bl,
bd522513 7662 const address_space *aspace, CORE_ADDR bp_addr,
09ac7c10 7663 const struct target_waitstatus *ws)
ce78b96d 7664{
e29a4733
PA
7665 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7666
f90263c1
TT
7667 if (ws->kind != TARGET_WAITKIND_FORKED)
7668 return 0;
7669
7670 c->forked_inferior_pid = ws->value.related_pid;
7671 return 1;
ce78b96d
JB
7672}
7673
4a64f543
MS
7674/* Implement the "print_it" breakpoint_ops method for fork
7675 catchpoints. */
ce78b96d
JB
7676
7677static enum print_stop_action
348d480f 7678print_it_catch_fork (bpstat bs)
ce78b96d 7679{
36dfb11c 7680 struct ui_out *uiout = current_uiout;
348d480f
PA
7681 struct breakpoint *b = bs->breakpoint_at;
7682 struct fork_catchpoint *c = (struct fork_catchpoint *) bs->breakpoint_at;
e29a4733 7683
ce78b96d 7684 annotate_catchpoint (b->number);
f303dbd6 7685 maybe_print_thread_hit_breakpoint (uiout);
36dfb11c 7686 if (b->disposition == disp_del)
112e8700 7687 uiout->text ("Temporary catchpoint ");
36dfb11c 7688 else
112e8700
SM
7689 uiout->text ("Catchpoint ");
7690 if (uiout->is_mi_like_p ())
36dfb11c 7691 {
112e8700
SM
7692 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_FORK));
7693 uiout->field_string ("disp", bpdisp_text (b->disposition));
36dfb11c 7694 }
381befee 7695 uiout->field_signed ("bkptno", b->number);
112e8700 7696 uiout->text (" (forked process ");
381befee 7697 uiout->field_signed ("newpid", c->forked_inferior_pid.pid ());
112e8700 7698 uiout->text ("), ");
ce78b96d
JB
7699 return PRINT_SRC_AND_LOC;
7700}
7701
4a64f543
MS
7702/* Implement the "print_one" breakpoint_ops method for fork
7703 catchpoints. */
ce78b96d
JB
7704
7705static void
a6d9a66e 7706print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 7707{
e29a4733 7708 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
79a45b7d 7709 struct value_print_options opts;
79a45e25 7710 struct ui_out *uiout = current_uiout;
79a45b7d
TT
7711
7712 get_user_print_options (&opts);
7713
4a64f543
MS
7714 /* Field 4, the address, is omitted (which makes the columns not
7715 line up too nicely with the headers, but the effect is relatively
7716 readable). */
79a45b7d 7717 if (opts.addressprint)
112e8700 7718 uiout->field_skip ("addr");
ce78b96d 7719 annotate_field (5);
112e8700 7720 uiout->text ("fork");
d7e15655 7721 if (c->forked_inferior_pid != null_ptid)
ce78b96d 7722 {
112e8700 7723 uiout->text (", process ");
381befee 7724 uiout->field_signed ("what", c->forked_inferior_pid.pid ());
112e8700 7725 uiout->spaces (1);
ce78b96d 7726 }
8ac3646f 7727
112e8700
SM
7728 if (uiout->is_mi_like_p ())
7729 uiout->field_string ("catch-type", "fork");
ce78b96d
JB
7730}
7731
7732/* Implement the "print_mention" breakpoint_ops method for fork
7733 catchpoints. */
7734
7735static void
7736print_mention_catch_fork (struct breakpoint *b)
7737{
7738 printf_filtered (_("Catchpoint %d (fork)"), b->number);
7739}
7740
6149aea9
PA
7741/* Implement the "print_recreate" breakpoint_ops method for fork
7742 catchpoints. */
7743
7744static void
7745print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
7746{
7747 fprintf_unfiltered (fp, "catch fork");
d9b3f62e 7748 print_recreate_thread (b, fp);
6149aea9
PA
7749}
7750
ce78b96d
JB
7751/* The breakpoint_ops structure to be used in fork catchpoints. */
7752
2060206e 7753static struct breakpoint_ops catch_fork_breakpoint_ops;
ce78b96d 7754
4a64f543
MS
7755/* Implement the "insert" breakpoint_ops method for vfork
7756 catchpoints. */
ce78b96d 7757
77b06cd7
TJB
7758static int
7759insert_catch_vfork (struct bp_location *bl)
ce78b96d 7760{
e99b03dc 7761 return target_insert_vfork_catchpoint (inferior_ptid.pid ());
ce78b96d
JB
7762}
7763
4a64f543
MS
7764/* Implement the "remove" breakpoint_ops method for vfork
7765 catchpoints. */
ce78b96d
JB
7766
7767static int
73971819 7768remove_catch_vfork (struct bp_location *bl, enum remove_bp_reason reason)
ce78b96d 7769{
e99b03dc 7770 return target_remove_vfork_catchpoint (inferior_ptid.pid ());
ce78b96d
JB
7771}
7772
7773/* Implement the "breakpoint_hit" breakpoint_ops method for vfork
7774 catchpoints. */
7775
7776static int
f1310107 7777breakpoint_hit_catch_vfork (const struct bp_location *bl,
bd522513 7778 const address_space *aspace, CORE_ADDR bp_addr,
09ac7c10 7779 const struct target_waitstatus *ws)
ce78b96d 7780{
e29a4733
PA
7781 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7782
f90263c1
TT
7783 if (ws->kind != TARGET_WAITKIND_VFORKED)
7784 return 0;
7785
7786 c->forked_inferior_pid = ws->value.related_pid;
7787 return 1;
ce78b96d
JB
7788}
7789
4a64f543
MS
7790/* Implement the "print_it" breakpoint_ops method for vfork
7791 catchpoints. */
ce78b96d
JB
7792
7793static enum print_stop_action
348d480f 7794print_it_catch_vfork (bpstat bs)
ce78b96d 7795{
36dfb11c 7796 struct ui_out *uiout = current_uiout;
348d480f 7797 struct breakpoint *b = bs->breakpoint_at;
e29a4733
PA
7798 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
7799
ce78b96d 7800 annotate_catchpoint (b->number);
f303dbd6 7801 maybe_print_thread_hit_breakpoint (uiout);
36dfb11c 7802 if (b->disposition == disp_del)
112e8700 7803 uiout->text ("Temporary catchpoint ");
36dfb11c 7804 else
112e8700
SM
7805 uiout->text ("Catchpoint ");
7806 if (uiout->is_mi_like_p ())
36dfb11c 7807 {
112e8700
SM
7808 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_VFORK));
7809 uiout->field_string ("disp", bpdisp_text (b->disposition));
36dfb11c 7810 }
381befee 7811 uiout->field_signed ("bkptno", b->number);
112e8700 7812 uiout->text (" (vforked process ");
381befee 7813 uiout->field_signed ("newpid", c->forked_inferior_pid.pid ());
112e8700 7814 uiout->text ("), ");
ce78b96d
JB
7815 return PRINT_SRC_AND_LOC;
7816}
7817
4a64f543
MS
7818/* Implement the "print_one" breakpoint_ops method for vfork
7819 catchpoints. */
ce78b96d
JB
7820
7821static void
a6d9a66e 7822print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 7823{
e29a4733 7824 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
79a45b7d 7825 struct value_print_options opts;
79a45e25 7826 struct ui_out *uiout = current_uiout;
79a45b7d
TT
7827
7828 get_user_print_options (&opts);
4a64f543
MS
7829 /* Field 4, the address, is omitted (which makes the columns not
7830 line up too nicely with the headers, but the effect is relatively
7831 readable). */
79a45b7d 7832 if (opts.addressprint)
112e8700 7833 uiout->field_skip ("addr");
ce78b96d 7834 annotate_field (5);
112e8700 7835 uiout->text ("vfork");
d7e15655 7836 if (c->forked_inferior_pid != null_ptid)
ce78b96d 7837 {
112e8700 7838 uiout->text (", process ");
381befee 7839 uiout->field_signed ("what", c->forked_inferior_pid.pid ());
112e8700 7840 uiout->spaces (1);
ce78b96d 7841 }
8ac3646f 7842
112e8700
SM
7843 if (uiout->is_mi_like_p ())
7844 uiout->field_string ("catch-type", "vfork");
ce78b96d
JB
7845}
7846
7847/* Implement the "print_mention" breakpoint_ops method for vfork
7848 catchpoints. */
7849
7850static void
7851print_mention_catch_vfork (struct breakpoint *b)
7852{
7853 printf_filtered (_("Catchpoint %d (vfork)"), b->number);
7854}
7855
6149aea9
PA
7856/* Implement the "print_recreate" breakpoint_ops method for vfork
7857 catchpoints. */
7858
7859static void
7860print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
7861{
7862 fprintf_unfiltered (fp, "catch vfork");
d9b3f62e 7863 print_recreate_thread (b, fp);
6149aea9
PA
7864}
7865
ce78b96d
JB
7866/* The breakpoint_ops structure to be used in vfork catchpoints. */
7867
2060206e 7868static struct breakpoint_ops catch_vfork_breakpoint_ops;
ce78b96d 7869
edcc5120 7870/* An instance of this type is used to represent an solib catchpoint.
c1fc2657 7871 A breakpoint is really of this type iff its ops pointer points to
edcc5120
TT
7872 CATCH_SOLIB_BREAKPOINT_OPS. */
7873
c1fc2657 7874struct solib_catchpoint : public breakpoint
edcc5120 7875{
c1fc2657 7876 ~solib_catchpoint () override;
edcc5120
TT
7877
7878 /* True for "catch load", false for "catch unload". */
7879 unsigned char is_load;
7880
7881 /* Regular expression to match, if any. COMPILED is only valid when
7882 REGEX is non-NULL. */
7883 char *regex;
2d7cc5c7 7884 std::unique_ptr<compiled_regex> compiled;
edcc5120
TT
7885};
7886
c1fc2657 7887solib_catchpoint::~solib_catchpoint ()
edcc5120 7888{
c1fc2657 7889 xfree (this->regex);
edcc5120
TT
7890}
7891
7892static int
7893insert_catch_solib (struct bp_location *ignore)
7894{
7895 return 0;
7896}
7897
7898static int
73971819 7899remove_catch_solib (struct bp_location *ignore, enum remove_bp_reason reason)
edcc5120
TT
7900{
7901 return 0;
7902}
7903
7904static int
7905breakpoint_hit_catch_solib (const struct bp_location *bl,
bd522513 7906 const address_space *aspace,
edcc5120
TT
7907 CORE_ADDR bp_addr,
7908 const struct target_waitstatus *ws)
7909{
7910 struct solib_catchpoint *self = (struct solib_catchpoint *) bl->owner;
7911 struct breakpoint *other;
7912
7913 if (ws->kind == TARGET_WAITKIND_LOADED)
7914 return 1;
7915
7916 ALL_BREAKPOINTS (other)
7917 {
7918 struct bp_location *other_bl;
7919
7920 if (other == bl->owner)
7921 continue;
7922
7923 if (other->type != bp_shlib_event)
7924 continue;
7925
c1fc2657 7926 if (self->pspace != NULL && other->pspace != self->pspace)
edcc5120
TT
7927 continue;
7928
7929 for (other_bl = other->loc; other_bl != NULL; other_bl = other_bl->next)
7930 {
7931 if (other->ops->breakpoint_hit (other_bl, aspace, bp_addr, ws))
7932 return 1;
7933 }
7934 }
7935
7936 return 0;
7937}
7938
7939static void
7940check_status_catch_solib (struct bpstats *bs)
7941{
7942 struct solib_catchpoint *self
7943 = (struct solib_catchpoint *) bs->breakpoint_at;
edcc5120
TT
7944
7945 if (self->is_load)
7946 {
52941706 7947 for (so_list *iter : current_program_space->added_solibs)
edcc5120
TT
7948 {
7949 if (!self->regex
2d7cc5c7 7950 || self->compiled->exec (iter->so_name, 0, NULL, 0) == 0)
edcc5120
TT
7951 return;
7952 }
7953 }
7954 else
7955 {
6fb16ce6 7956 for (const std::string &iter : current_program_space->deleted_solibs)
edcc5120
TT
7957 {
7958 if (!self->regex
6fb16ce6 7959 || self->compiled->exec (iter.c_str (), 0, NULL, 0) == 0)
edcc5120
TT
7960 return;
7961 }
7962 }
7963
7964 bs->stop = 0;
7965 bs->print_it = print_it_noop;
7966}
7967
7968static enum print_stop_action
7969print_it_catch_solib (bpstat bs)
7970{
7971 struct breakpoint *b = bs->breakpoint_at;
7972 struct ui_out *uiout = current_uiout;
7973
7974 annotate_catchpoint (b->number);
f303dbd6 7975 maybe_print_thread_hit_breakpoint (uiout);
edcc5120 7976 if (b->disposition == disp_del)
112e8700 7977 uiout->text ("Temporary catchpoint ");
edcc5120 7978 else
112e8700 7979 uiout->text ("Catchpoint ");
381befee 7980 uiout->field_signed ("bkptno", b->number);
112e8700
SM
7981 uiout->text ("\n");
7982 if (uiout->is_mi_like_p ())
7983 uiout->field_string ("disp", bpdisp_text (b->disposition));
edcc5120
TT
7984 print_solib_event (1);
7985 return PRINT_SRC_AND_LOC;
7986}
7987
7988static void
7989print_one_catch_solib (struct breakpoint *b, struct bp_location **locs)
7990{
7991 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
7992 struct value_print_options opts;
7993 struct ui_out *uiout = current_uiout;
edcc5120
TT
7994
7995 get_user_print_options (&opts);
7996 /* Field 4, the address, is omitted (which makes the columns not
7997 line up too nicely with the headers, but the effect is relatively
7998 readable). */
7999 if (opts.addressprint)
8000 {
8001 annotate_field (4);
112e8700 8002 uiout->field_skip ("addr");
edcc5120
TT
8003 }
8004
528e1572 8005 std::string msg;
edcc5120
TT
8006 annotate_field (5);
8007 if (self->is_load)
8008 {
8009 if (self->regex)
528e1572 8010 msg = string_printf (_("load of library matching %s"), self->regex);
edcc5120 8011 else
528e1572 8012 msg = _("load of library");
edcc5120
TT
8013 }
8014 else
8015 {
8016 if (self->regex)
528e1572 8017 msg = string_printf (_("unload of library matching %s"), self->regex);
edcc5120 8018 else
528e1572 8019 msg = _("unload of library");
edcc5120 8020 }
112e8700 8021 uiout->field_string ("what", msg);
8ac3646f 8022
112e8700
SM
8023 if (uiout->is_mi_like_p ())
8024 uiout->field_string ("catch-type", self->is_load ? "load" : "unload");
edcc5120
TT
8025}
8026
8027static void
8028print_mention_catch_solib (struct breakpoint *b)
8029{
8030 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8031
8032 printf_filtered (_("Catchpoint %d (%s)"), b->number,
8033 self->is_load ? "load" : "unload");
8034}
8035
8036static void
8037print_recreate_catch_solib (struct breakpoint *b, struct ui_file *fp)
8038{
8039 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8040
8041 fprintf_unfiltered (fp, "%s %s",
8042 b->disposition == disp_del ? "tcatch" : "catch",
8043 self->is_load ? "load" : "unload");
8044 if (self->regex)
8045 fprintf_unfiltered (fp, " %s", self->regex);
8046 fprintf_unfiltered (fp, "\n");
8047}
8048
8049static struct breakpoint_ops catch_solib_breakpoint_ops;
8050
91985142
MG
8051/* Shared helper function (MI and CLI) for creating and installing
8052 a shared object event catchpoint. If IS_LOAD is non-zero then
8053 the events to be caught are load events, otherwise they are
8054 unload events. If IS_TEMP is non-zero the catchpoint is a
8055 temporary one. If ENABLED is non-zero the catchpoint is
8056 created in an enabled state. */
edcc5120 8057
91985142 8058void
a121b7c1 8059add_solib_catchpoint (const char *arg, int is_load, int is_temp, int enabled)
edcc5120 8060{
edcc5120 8061 struct gdbarch *gdbarch = get_current_arch ();
edcc5120 8062
edcc5120
TT
8063 if (!arg)
8064 arg = "";
f1735a53 8065 arg = skip_spaces (arg);
edcc5120 8066
36bd8eaa 8067 std::unique_ptr<solib_catchpoint> c (new solib_catchpoint ());
edcc5120
TT
8068
8069 if (*arg != '\0')
8070 {
2d7cc5c7
PA
8071 c->compiled.reset (new compiled_regex (arg, REG_NOSUB,
8072 _("Invalid regexp")));
edcc5120
TT
8073 c->regex = xstrdup (arg);
8074 }
8075
8076 c->is_load = is_load;
36bd8eaa 8077 init_catchpoint (c.get (), gdbarch, is_temp, NULL,
edcc5120
TT
8078 &catch_solib_breakpoint_ops);
8079
c1fc2657 8080 c->enable_state = enabled ? bp_enabled : bp_disabled;
91985142 8081
b270e6f9 8082 install_breakpoint (0, std::move (c), 1);
edcc5120
TT
8083}
8084
91985142
MG
8085/* A helper function that does all the work for "catch load" and
8086 "catch unload". */
8087
8088static void
eb4c3f4a 8089catch_load_or_unload (const char *arg, int from_tty, int is_load,
91985142
MG
8090 struct cmd_list_element *command)
8091{
8092 int tempflag;
8093 const int enabled = 1;
8094
8095 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
8096
8097 add_solib_catchpoint (arg, is_load, tempflag, enabled);
8098}
8099
edcc5120 8100static void
eb4c3f4a 8101catch_load_command_1 (const char *arg, int from_tty,
edcc5120
TT
8102 struct cmd_list_element *command)
8103{
8104 catch_load_or_unload (arg, from_tty, 1, command);
8105}
8106
8107static void
eb4c3f4a 8108catch_unload_command_1 (const char *arg, int from_tty,
edcc5120
TT
8109 struct cmd_list_element *command)
8110{
8111 catch_load_or_unload (arg, from_tty, 0, command);
8112}
8113
346774a9
PA
8114/* Initialize a new breakpoint of the bp_catchpoint kind. If TEMPFLAG
8115 is non-zero, then make the breakpoint temporary. If COND_STRING is
8116 not NULL, then store it in the breakpoint. OPS, if not NULL, is
8117 the breakpoint_ops structure associated to the catchpoint. */
ce78b96d 8118
ab04a2af 8119void
346774a9
PA
8120init_catchpoint (struct breakpoint *b,
8121 struct gdbarch *gdbarch, int tempflag,
63160a43 8122 const char *cond_string,
c0a91b2b 8123 const struct breakpoint_ops *ops)
c906108c 8124{
51abb421 8125 symtab_and_line sal;
6c95b8df 8126 sal.pspace = current_program_space;
c5aa993b 8127
28010a5d 8128 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
ce78b96d 8129
1b36a34b 8130 b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
b5de0fa7 8131 b->disposition = tempflag ? disp_del : disp_donttouch;
346774a9
PA
8132}
8133
28010a5d 8134void
b270e6f9 8135install_breakpoint (int internal, std::unique_ptr<breakpoint> &&arg, int update_gll)
c56053d2 8136{
b270e6f9 8137 breakpoint *b = add_to_breakpoint_chain (std::move (arg));
3a5c3e22 8138 set_breakpoint_number (internal, b);
558a9d82
YQ
8139 if (is_tracepoint (b))
8140 set_tracepoint_count (breakpoint_count);
3a5c3e22
PA
8141 if (!internal)
8142 mention (b);
76727919 8143 gdb::observers::breakpoint_created.notify (b);
3ea46bff
YQ
8144
8145 if (update_gll)
44702360 8146 update_global_location_list (UGLL_MAY_INSERT);
c56053d2
PA
8147}
8148
9b70b993 8149static void
a6d9a66e 8150create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
63160a43 8151 int tempflag, const char *cond_string,
c0a91b2b 8152 const struct breakpoint_ops *ops)
c906108c 8153{
b270e6f9 8154 std::unique_ptr<fork_catchpoint> c (new fork_catchpoint ());
ce78b96d 8155
b270e6f9 8156 init_catchpoint (c.get (), gdbarch, tempflag, cond_string, ops);
e29a4733
PA
8157
8158 c->forked_inferior_pid = null_ptid;
8159
b270e6f9 8160 install_breakpoint (0, std::move (c), 1);
c906108c
SS
8161}
8162
fe798b75
JB
8163/* Exec catchpoints. */
8164
b4d90040 8165/* An instance of this type is used to represent an exec catchpoint.
c1fc2657 8166 A breakpoint is really of this type iff its ops pointer points to
b4d90040
PA
8167 CATCH_EXEC_BREAKPOINT_OPS. */
8168
c1fc2657 8169struct exec_catchpoint : public breakpoint
b4d90040 8170{
c1fc2657 8171 ~exec_catchpoint () override;
b4d90040
PA
8172
8173 /* Filename of a program whose exec triggered this catchpoint.
8174 This field is only valid immediately after this catchpoint has
8175 triggered. */
8176 char *exec_pathname;
8177};
8178
c1fc2657 8179/* Exec catchpoint destructor. */
b4d90040 8180
c1fc2657 8181exec_catchpoint::~exec_catchpoint ()
b4d90040 8182{
c1fc2657 8183 xfree (this->exec_pathname);
b4d90040
PA
8184}
8185
77b06cd7
TJB
8186static int
8187insert_catch_exec (struct bp_location *bl)
c906108c 8188{
e99b03dc 8189 return target_insert_exec_catchpoint (inferior_ptid.pid ());
fe798b75 8190}
c906108c 8191
fe798b75 8192static int
73971819 8193remove_catch_exec (struct bp_location *bl, enum remove_bp_reason reason)
fe798b75 8194{
e99b03dc 8195 return target_remove_exec_catchpoint (inferior_ptid.pid ());
fe798b75 8196}
c906108c 8197
fe798b75 8198static int
f1310107 8199breakpoint_hit_catch_exec (const struct bp_location *bl,
bd522513 8200 const address_space *aspace, CORE_ADDR bp_addr,
09ac7c10 8201 const struct target_waitstatus *ws)
fe798b75 8202{
b4d90040
PA
8203 struct exec_catchpoint *c = (struct exec_catchpoint *) bl->owner;
8204
f90263c1
TT
8205 if (ws->kind != TARGET_WAITKIND_EXECD)
8206 return 0;
8207
8208 c->exec_pathname = xstrdup (ws->value.execd_pathname);
8209 return 1;
fe798b75 8210}
c906108c 8211
fe798b75 8212static enum print_stop_action
348d480f 8213print_it_catch_exec (bpstat bs)
fe798b75 8214{
36dfb11c 8215 struct ui_out *uiout = current_uiout;
348d480f 8216 struct breakpoint *b = bs->breakpoint_at;
b4d90040
PA
8217 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8218
fe798b75 8219 annotate_catchpoint (b->number);
f303dbd6 8220 maybe_print_thread_hit_breakpoint (uiout);
36dfb11c 8221 if (b->disposition == disp_del)
112e8700 8222 uiout->text ("Temporary catchpoint ");
36dfb11c 8223 else
112e8700
SM
8224 uiout->text ("Catchpoint ");
8225 if (uiout->is_mi_like_p ())
36dfb11c 8226 {
112e8700
SM
8227 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_EXEC));
8228 uiout->field_string ("disp", bpdisp_text (b->disposition));
36dfb11c 8229 }
381befee 8230 uiout->field_signed ("bkptno", b->number);
112e8700
SM
8231 uiout->text (" (exec'd ");
8232 uiout->field_string ("new-exec", c->exec_pathname);
8233 uiout->text ("), ");
36dfb11c 8234
fe798b75 8235 return PRINT_SRC_AND_LOC;
c906108c
SS
8236}
8237
fe798b75 8238static void
a6d9a66e 8239print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
fe798b75 8240{
b4d90040 8241 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
fe798b75 8242 struct value_print_options opts;
79a45e25 8243 struct ui_out *uiout = current_uiout;
fe798b75
JB
8244
8245 get_user_print_options (&opts);
8246
8247 /* Field 4, the address, is omitted (which makes the columns
8248 not line up too nicely with the headers, but the effect
8249 is relatively readable). */
8250 if (opts.addressprint)
112e8700 8251 uiout->field_skip ("addr");
fe798b75 8252 annotate_field (5);
112e8700 8253 uiout->text ("exec");
b4d90040 8254 if (c->exec_pathname != NULL)
fe798b75 8255 {
112e8700
SM
8256 uiout->text (", program \"");
8257 uiout->field_string ("what", c->exec_pathname);
8258 uiout->text ("\" ");
fe798b75 8259 }
8ac3646f 8260
112e8700
SM
8261 if (uiout->is_mi_like_p ())
8262 uiout->field_string ("catch-type", "exec");
fe798b75
JB
8263}
8264
8265static void
8266print_mention_catch_exec (struct breakpoint *b)
8267{
8268 printf_filtered (_("Catchpoint %d (exec)"), b->number);
8269}
8270
6149aea9
PA
8271/* Implement the "print_recreate" breakpoint_ops method for exec
8272 catchpoints. */
8273
8274static void
8275print_recreate_catch_exec (struct breakpoint *b, struct ui_file *fp)
8276{
8277 fprintf_unfiltered (fp, "catch exec");
d9b3f62e 8278 print_recreate_thread (b, fp);
6149aea9
PA
8279}
8280
2060206e 8281static struct breakpoint_ops catch_exec_breakpoint_ops;
fe798b75 8282
c906108c 8283static int
fba45db2 8284hw_breakpoint_used_count (void)
c906108c 8285{
c906108c 8286 int i = 0;
f1310107
TJB
8287 struct breakpoint *b;
8288 struct bp_location *bl;
c906108c
SS
8289
8290 ALL_BREAKPOINTS (b)
c5aa993b 8291 {
d6b74ac4 8292 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
f1310107
TJB
8293 for (bl = b->loc; bl; bl = bl->next)
8294 {
8295 /* Special types of hardware breakpoints may use more than
8296 one register. */
348d480f 8297 i += b->ops->resources_needed (bl);
f1310107 8298 }
c5aa993b 8299 }
c906108c
SS
8300
8301 return i;
8302}
8303
a1398e0c
PA
8304/* Returns the resources B would use if it were a hardware
8305 watchpoint. */
8306
c906108c 8307static int
a1398e0c 8308hw_watchpoint_use_count (struct breakpoint *b)
c906108c 8309{
c906108c 8310 int i = 0;
e09342b5 8311 struct bp_location *bl;
c906108c 8312
a1398e0c
PA
8313 if (!breakpoint_enabled (b))
8314 return 0;
8315
8316 for (bl = b->loc; bl; bl = bl->next)
8317 {
8318 /* Special types of hardware watchpoints may use more than
8319 one register. */
8320 i += b->ops->resources_needed (bl);
8321 }
8322
8323 return i;
8324}
8325
8326/* Returns the sum the used resources of all hardware watchpoints of
8327 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
8328 the sum of the used resources of all hardware watchpoints of other
8329 types _not_ TYPE. */
8330
8331static int
8332hw_watchpoint_used_count_others (struct breakpoint *except,
8333 enum bptype type, int *other_type_used)
8334{
8335 int i = 0;
8336 struct breakpoint *b;
8337
c906108c
SS
8338 *other_type_used = 0;
8339 ALL_BREAKPOINTS (b)
e09342b5 8340 {
a1398e0c
PA
8341 if (b == except)
8342 continue;
e09342b5
TJB
8343 if (!breakpoint_enabled (b))
8344 continue;
8345
a1398e0c
PA
8346 if (b->type == type)
8347 i += hw_watchpoint_use_count (b);
8348 else if (is_hardware_watchpoint (b))
8349 *other_type_used = 1;
e09342b5
TJB
8350 }
8351
c906108c
SS
8352 return i;
8353}
8354
c906108c 8355void
fba45db2 8356disable_watchpoints_before_interactive_call_start (void)
c906108c 8357{
c5aa993b 8358 struct breakpoint *b;
c906108c
SS
8359
8360 ALL_BREAKPOINTS (b)
c5aa993b 8361 {
cc60f2e3 8362 if (is_watchpoint (b) && breakpoint_enabled (b))
c5aa993b 8363 {
b5de0fa7 8364 b->enable_state = bp_call_disabled;
44702360 8365 update_global_location_list (UGLL_DONT_INSERT);
c5aa993b
JM
8366 }
8367 }
c906108c
SS
8368}
8369
8370void
fba45db2 8371enable_watchpoints_after_interactive_call_stop (void)
c906108c 8372{
c5aa993b 8373 struct breakpoint *b;
c906108c
SS
8374
8375 ALL_BREAKPOINTS (b)
c5aa993b 8376 {
cc60f2e3 8377 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
c5aa993b 8378 {
b5de0fa7 8379 b->enable_state = bp_enabled;
44702360 8380 update_global_location_list (UGLL_MAY_INSERT);
c5aa993b
JM
8381 }
8382 }
c906108c
SS
8383}
8384
8bea4e01
UW
8385void
8386disable_breakpoints_before_startup (void)
8387{
6c95b8df 8388 current_program_space->executing_startup = 1;
44702360 8389 update_global_location_list (UGLL_DONT_INSERT);
8bea4e01
UW
8390}
8391
8392void
8393enable_breakpoints_after_startup (void)
8394{
6c95b8df 8395 current_program_space->executing_startup = 0;
f8eba3c6 8396 breakpoint_re_set ();
8bea4e01
UW
8397}
8398
7c16b83e
PA
8399/* Create a new single-step breakpoint for thread THREAD, with no
8400 locations. */
c906108c 8401
7c16b83e
PA
8402static struct breakpoint *
8403new_single_step_breakpoint (int thread, struct gdbarch *gdbarch)
8404{
b270e6f9 8405 std::unique_ptr<breakpoint> b (new breakpoint ());
7c16b83e 8406
b270e6f9 8407 init_raw_breakpoint_without_location (b.get (), gdbarch, bp_single_step,
7c16b83e
PA
8408 &momentary_breakpoint_ops);
8409
8410 b->disposition = disp_donttouch;
8411 b->frame_id = null_frame_id;
8412
8413 b->thread = thread;
8414 gdb_assert (b->thread != 0);
8415
b270e6f9 8416 return add_to_breakpoint_chain (std::move (b));
7c16b83e
PA
8417}
8418
8419/* Set a momentary breakpoint of type TYPE at address specified by
8420 SAL. If FRAME_ID is valid, the breakpoint is restricted to that
8421 frame. */
c906108c 8422
454dafbd 8423breakpoint_up
a6d9a66e
UW
8424set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
8425 struct frame_id frame_id, enum bptype type)
c906108c 8426{
52f0bd74 8427 struct breakpoint *b;
edb3359d 8428
193facb3
JK
8429 /* If FRAME_ID is valid, it should be a real frame, not an inlined or
8430 tail-called one. */
8431 gdb_assert (!frame_id_artificial_p (frame_id));
edb3359d 8432
06edf0c0 8433 b = set_raw_breakpoint (gdbarch, sal, type, &momentary_breakpoint_ops);
b5de0fa7
EZ
8434 b->enable_state = bp_enabled;
8435 b->disposition = disp_donttouch;
818dd999 8436 b->frame_id = frame_id;
c906108c 8437
00431a78 8438 b->thread = inferior_thread ()->global_num;
c906108c 8439
44702360 8440 update_global_location_list_nothrow (UGLL_MAY_INSERT);
74960c60 8441
454dafbd 8442 return breakpoint_up (b);
c906108c 8443}
611c83ae 8444
06edf0c0 8445/* Make a momentary breakpoint based on the master breakpoint ORIG.
a1aa2221
LM
8446 The new breakpoint will have type TYPE, use OPS as its
8447 breakpoint_ops, and will set enabled to LOC_ENABLED. */
e58b0e63 8448
06edf0c0
PA
8449static struct breakpoint *
8450momentary_breakpoint_from_master (struct breakpoint *orig,
8451 enum bptype type,
a1aa2221
LM
8452 const struct breakpoint_ops *ops,
8453 int loc_enabled)
e58b0e63
PA
8454{
8455 struct breakpoint *copy;
8456
06edf0c0 8457 copy = set_raw_breakpoint_without_location (orig->gdbarch, type, ops);
e58b0e63 8458 copy->loc = allocate_bp_location (copy);
0ba852ab 8459 set_breakpoint_location_function (copy->loc);
e58b0e63 8460
a6d9a66e 8461 copy->loc->gdbarch = orig->loc->gdbarch;
e58b0e63
PA
8462 copy->loc->requested_address = orig->loc->requested_address;
8463 copy->loc->address = orig->loc->address;
8464 copy->loc->section = orig->loc->section;
6c95b8df 8465 copy->loc->pspace = orig->loc->pspace;
55aa24fb 8466 copy->loc->probe = orig->loc->probe;
f8eba3c6 8467 copy->loc->line_number = orig->loc->line_number;
2f202fde 8468 copy->loc->symtab = orig->loc->symtab;
a1aa2221 8469 copy->loc->enabled = loc_enabled;
e58b0e63
PA
8470 copy->frame_id = orig->frame_id;
8471 copy->thread = orig->thread;
6c95b8df 8472 copy->pspace = orig->pspace;
e58b0e63
PA
8473
8474 copy->enable_state = bp_enabled;
8475 copy->disposition = disp_donttouch;
8476 copy->number = internal_breakpoint_number--;
8477
44702360 8478 update_global_location_list_nothrow (UGLL_DONT_INSERT);
e58b0e63
PA
8479 return copy;
8480}
8481
06edf0c0
PA
8482/* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
8483 ORIG is NULL. */
8484
8485struct breakpoint *
8486clone_momentary_breakpoint (struct breakpoint *orig)
8487{
8488 /* If there's nothing to clone, then return nothing. */
8489 if (orig == NULL)
8490 return NULL;
8491
a1aa2221 8492 return momentary_breakpoint_from_master (orig, orig->type, orig->ops, 0);
06edf0c0
PA
8493}
8494
454dafbd 8495breakpoint_up
a6d9a66e
UW
8496set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
8497 enum bptype type)
611c83ae
PA
8498{
8499 struct symtab_and_line sal;
8500
8501 sal = find_pc_line (pc, 0);
8502 sal.pc = pc;
8503 sal.section = find_pc_overlay (pc);
8504 sal.explicit_pc = 1;
8505
a6d9a66e 8506 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
611c83ae 8507}
c906108c 8508\f
c5aa993b 8509
c906108c
SS
8510/* Tell the user we have just set a breakpoint B. */
8511
8512static void
fba45db2 8513mention (struct breakpoint *b)
c906108c 8514{
348d480f 8515 b->ops->print_mention (b);
5922befa
LM
8516 if (current_uiout->is_mi_like_p ())
8517 return;
8518 printf_filtered ("\n");
c906108c 8519}
c906108c 8520\f
c5aa993b 8521
1a853c52
PA
8522static int bp_loc_is_permanent (struct bp_location *loc);
8523
0d381245 8524static struct bp_location *
39d61571 8525add_location_to_breakpoint (struct breakpoint *b,
0d381245
VP
8526 const struct symtab_and_line *sal)
8527{
8528 struct bp_location *loc, **tmp;
3742cc8b
YQ
8529 CORE_ADDR adjusted_address;
8530 struct gdbarch *loc_gdbarch = get_sal_arch (*sal);
8531
8532 if (loc_gdbarch == NULL)
8533 loc_gdbarch = b->gdbarch;
8534
8535 /* Adjust the breakpoint's address prior to allocating a location.
8536 Once we call allocate_bp_location(), that mostly uninitialized
8537 location will be placed on the location chain. Adjustment of the
8538 breakpoint may cause target_read_memory() to be called and we do
8539 not want its scan of the location chain to find a breakpoint and
8540 location that's only been partially initialized. */
8541 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
8542 sal->pc, b->type);
0d381245 8543
d30113d4 8544 /* Sort the locations by their ADDRESS. */
39d61571 8545 loc = allocate_bp_location (b);
d30113d4
JK
8546 for (tmp = &(b->loc); *tmp != NULL && (*tmp)->address <= adjusted_address;
8547 tmp = &((*tmp)->next))
0d381245 8548 ;
d30113d4 8549 loc->next = *tmp;
0d381245 8550 *tmp = loc;
3742cc8b 8551
0d381245 8552 loc->requested_address = sal->pc;
3742cc8b 8553 loc->address = adjusted_address;
6c95b8df 8554 loc->pspace = sal->pspace;
935676c9 8555 loc->probe.prob = sal->prob;
729662a5 8556 loc->probe.objfile = sal->objfile;
6c95b8df 8557 gdb_assert (loc->pspace != NULL);
0d381245 8558 loc->section = sal->section;
3742cc8b 8559 loc->gdbarch = loc_gdbarch;
f8eba3c6 8560 loc->line_number = sal->line;
2f202fde 8561 loc->symtab = sal->symtab;
4a27f119 8562 loc->symbol = sal->symbol;
3467ec66
PA
8563 loc->msymbol = sal->msymbol;
8564 loc->objfile = sal->objfile;
f8eba3c6 8565
0ba852ab 8566 set_breakpoint_location_function (loc);
1a853c52 8567
6ae88661
LM
8568 /* While by definition, permanent breakpoints are already present in the
8569 code, we don't mark the location as inserted. Normally one would expect
8570 that GDB could rely on that breakpoint instruction to stop the program,
8571 thus removing the need to insert its own breakpoint, except that executing
8572 the breakpoint instruction can kill the target instead of reporting a
8573 SIGTRAP. E.g., on SPARC, when interrupts are disabled, executing the
8574 instruction resets the CPU, so QEMU 2.0.0 for SPARC correspondingly dies
8575 with "Trap 0x02 while interrupts disabled, Error state". Letting the
8576 breakpoint be inserted normally results in QEMU knowing about the GDB
8577 breakpoint, and thus trap before the breakpoint instruction is executed.
8578 (If GDB later needs to continue execution past the permanent breakpoint,
8579 it manually increments the PC, thus avoiding executing the breakpoint
8580 instruction.) */
1a853c52 8581 if (bp_loc_is_permanent (loc))
6ae88661 8582 loc->permanent = 1;
1a853c52 8583
0d381245
VP
8584 return loc;
8585}
514f746b
AR
8586\f
8587
1cf4d951 8588/* See breakpoint.h. */
514f746b 8589
1cf4d951
PA
8590int
8591program_breakpoint_here_p (struct gdbarch *gdbarch, CORE_ADDR address)
514f746b
AR
8592{
8593 int len;
8594 CORE_ADDR addr;
1afeeb75 8595 const gdb_byte *bpoint;
514f746b
AR
8596 gdb_byte *target_mem;
8597
1cf4d951
PA
8598 addr = address;
8599 bpoint = gdbarch_breakpoint_from_pc (gdbarch, &addr, &len);
8600
8601 /* Software breakpoints unsupported? */
8602 if (bpoint == NULL)
8603 return 0;
8604
224c3ddb 8605 target_mem = (gdb_byte *) alloca (len);
1cf4d951
PA
8606
8607 /* Enable the automatic memory restoration from breakpoints while
8608 we read the memory. Otherwise we could say about our temporary
8609 breakpoints they are permanent. */
cb85b21b
TT
8610 scoped_restore restore_memory
8611 = make_scoped_restore_show_memory_breakpoints (0);
1cf4d951
PA
8612
8613 if (target_read_memory (address, target_mem, len) == 0
8614 && memcmp (target_mem, bpoint, len) == 0)
cb85b21b 8615 return 1;
1cf4d951 8616
cb85b21b 8617 return 0;
1cf4d951
PA
8618}
8619
8620/* Return 1 if LOC is pointing to a permanent breakpoint,
8621 return 0 otherwise. */
8622
8623static int
8624bp_loc_is_permanent (struct bp_location *loc)
8625{
514f746b
AR
8626 gdb_assert (loc != NULL);
8627
cb1e4e32
PA
8628 /* If we have a non-breakpoint-backed catchpoint or a software
8629 watchpoint, just return 0. We should not attempt to read from
8630 the addresses the locations of these breakpoint types point to.
8631 program_breakpoint_here_p, below, will attempt to read
244558af 8632 memory. */
cb1e4e32 8633 if (!bl_address_is_meaningful (loc))
244558af
LM
8634 return 0;
8635
5ed8105e 8636 scoped_restore_current_pspace_and_thread restore_pspace_thread;
6c95b8df 8637 switch_to_program_space_and_thread (loc->pspace);
5ed8105e 8638 return program_breakpoint_here_p (loc->gdbarch, loc->address);
514f746b
AR
8639}
8640
e7e0cddf
SS
8641/* Build a command list for the dprintf corresponding to the current
8642 settings of the dprintf style options. */
8643
8644static void
8645update_dprintf_command_list (struct breakpoint *b)
8646{
8647 char *dprintf_args = b->extra_string;
8648 char *printf_line = NULL;
8649
8650 if (!dprintf_args)
8651 return;
8652
8653 dprintf_args = skip_spaces (dprintf_args);
8654
8655 /* Allow a comma, as it may have terminated a location, but don't
8656 insist on it. */
8657 if (*dprintf_args == ',')
8658 ++dprintf_args;
8659 dprintf_args = skip_spaces (dprintf_args);
8660
8661 if (*dprintf_args != '"')
8662 error (_("Bad format string, missing '\"'."));
8663
d3ce09f5 8664 if (strcmp (dprintf_style, dprintf_style_gdb) == 0)
e7e0cddf 8665 printf_line = xstrprintf ("printf %s", dprintf_args);
d3ce09f5 8666 else if (strcmp (dprintf_style, dprintf_style_call) == 0)
e7e0cddf
SS
8667 {
8668 if (!dprintf_function)
8669 error (_("No function supplied for dprintf call"));
8670
8671 if (dprintf_channel && strlen (dprintf_channel) > 0)
8672 printf_line = xstrprintf ("call (void) %s (%s,%s)",
8673 dprintf_function,
8674 dprintf_channel,
8675 dprintf_args);
8676 else
8677 printf_line = xstrprintf ("call (void) %s (%s)",
8678 dprintf_function,
8679 dprintf_args);
8680 }
d3ce09f5
SS
8681 else if (strcmp (dprintf_style, dprintf_style_agent) == 0)
8682 {
8683 if (target_can_run_breakpoint_commands ())
8684 printf_line = xstrprintf ("agent-printf %s", dprintf_args);
8685 else
8686 {
8687 warning (_("Target cannot run dprintf commands, falling back to GDB printf"));
8688 printf_line = xstrprintf ("printf %s", dprintf_args);
8689 }
8690 }
e7e0cddf
SS
8691 else
8692 internal_error (__FILE__, __LINE__,
8693 _("Invalid dprintf style."));
8694
f28045c2 8695 gdb_assert (printf_line != NULL);
e7e0cddf 8696
12973681
TT
8697 /* Manufacture a printf sequence. */
8698 struct command_line *printf_cmd_line
8699 = new struct command_line (simple_control, printf_line);
8700 breakpoint_set_commands (b, counted_command_line (printf_cmd_line,
8701 command_lines_deleter ()));
e7e0cddf
SS
8702}
8703
8704/* Update all dprintf commands, making their command lists reflect
8705 current style settings. */
8706
8707static void
eb4c3f4a 8708update_dprintf_commands (const char *args, int from_tty,
e7e0cddf
SS
8709 struct cmd_list_element *c)
8710{
8711 struct breakpoint *b;
8712
8713 ALL_BREAKPOINTS (b)
8714 {
8715 if (b->type == bp_dprintf)
8716 update_dprintf_command_list (b);
8717 }
8718}
c3f6f71d 8719
f00aae0f
KS
8720/* Create a breakpoint with SAL as location. Use LOCATION
8721 as a description of the location, and COND_STRING
b35a8b2f
DE
8722 as condition expression. If LOCATION is NULL then create an
8723 "address location" from the address in the SAL. */
018d34a4
VP
8724
8725static void
d9b3f62e 8726init_breakpoint_sal (struct breakpoint *b, struct gdbarch *gdbarch,
6c5b2ebe 8727 gdb::array_view<const symtab_and_line> sals,
ffc2605c 8728 event_location_up &&location,
e1e01040
PA
8729 gdb::unique_xmalloc_ptr<char> filter,
8730 gdb::unique_xmalloc_ptr<char> cond_string,
8731 gdb::unique_xmalloc_ptr<char> extra_string,
d9b3f62e
PA
8732 enum bptype type, enum bpdisp disposition,
8733 int thread, int task, int ignore_count,
c0a91b2b 8734 const struct breakpoint_ops *ops, int from_tty,
44f238bb
PA
8735 int enabled, int internal, unsigned flags,
8736 int display_canonical)
018d34a4 8737{
0d381245 8738 int i;
018d34a4
VP
8739
8740 if (type == bp_hardware_breakpoint)
8741 {
fbbd034e
AS
8742 int target_resources_ok;
8743
8744 i = hw_breakpoint_used_count ();
8745 target_resources_ok =
8746 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
018d34a4
VP
8747 i + 1, 0);
8748 if (target_resources_ok == 0)
8749 error (_("No hardware breakpoint support in the target."));
8750 else if (target_resources_ok < 0)
8751 error (_("Hardware breakpoints used exceeds limit."));
8752 }
8753
6c5b2ebe 8754 gdb_assert (!sals.empty ());
6c95b8df 8755
6c5b2ebe 8756 for (const auto &sal : sals)
0d381245 8757 {
0d381245
VP
8758 struct bp_location *loc;
8759
8760 if (from_tty)
5af949e3
UW
8761 {
8762 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
8763 if (!loc_gdbarch)
8764 loc_gdbarch = gdbarch;
8765
8766 describe_other_breakpoints (loc_gdbarch,
6c95b8df 8767 sal.pspace, sal.pc, sal.section, thread);
5af949e3 8768 }
0d381245 8769
6c5b2ebe 8770 if (&sal == &sals[0])
0d381245 8771 {
d9b3f62e 8772 init_raw_breakpoint (b, gdbarch, sal, type, ops);
0d381245 8773 b->thread = thread;
4a306c9a 8774 b->task = task;
855a6e68 8775
e1e01040
PA
8776 b->cond_string = cond_string.release ();
8777 b->extra_string = extra_string.release ();
0d381245 8778 b->ignore_count = ignore_count;
41447f92 8779 b->enable_state = enabled ? bp_enabled : bp_disabled;
0d381245 8780 b->disposition = disposition;
6c95b8df 8781
44f238bb
PA
8782 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
8783 b->loc->inserted = 1;
8784
0fb4aa4b
PA
8785 if (type == bp_static_tracepoint)
8786 {
d9b3f62e 8787 struct tracepoint *t = (struct tracepoint *) b;
0fb4aa4b
PA
8788 struct static_tracepoint_marker marker;
8789
983af33b 8790 if (strace_marker_p (b))
0fb4aa4b
PA
8791 {
8792 /* We already know the marker exists, otherwise, we
8793 wouldn't see a sal for it. */
d28cd78a
TT
8794 const char *p
8795 = &event_location_to_string (b->location.get ())[3];
f00aae0f 8796 const char *endp;
0fb4aa4b 8797
f1735a53 8798 p = skip_spaces (p);
0fb4aa4b 8799
f1735a53 8800 endp = skip_to_space (p);
0fb4aa4b 8801
5d9310c4 8802 t->static_trace_marker_id.assign (p, endp - p);
0fb4aa4b 8803
3e43a32a
MS
8804 printf_filtered (_("Probed static tracepoint "
8805 "marker \"%s\"\n"),
5d9310c4 8806 t->static_trace_marker_id.c_str ());
0fb4aa4b
PA
8807 }
8808 else if (target_static_tracepoint_marker_at (sal.pc, &marker))
8809 {
5d9310c4 8810 t->static_trace_marker_id = std::move (marker.str_id);
0fb4aa4b 8811
3e43a32a
MS
8812 printf_filtered (_("Probed static tracepoint "
8813 "marker \"%s\"\n"),
5d9310c4 8814 t->static_trace_marker_id.c_str ());
0fb4aa4b
PA
8815 }
8816 else
3e43a32a
MS
8817 warning (_("Couldn't determine the static "
8818 "tracepoint marker to probe"));
0fb4aa4b
PA
8819 }
8820
0d381245
VP
8821 loc = b->loc;
8822 }
8823 else
018d34a4 8824 {
39d61571 8825 loc = add_location_to_breakpoint (b, &sal);
44f238bb
PA
8826 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
8827 loc->inserted = 1;
0d381245
VP
8828 }
8829
8830 if (b->cond_string)
8831 {
bbc13ae3
KS
8832 const char *arg = b->cond_string;
8833
1bb9788d
TT
8834 loc->cond = parse_exp_1 (&arg, loc->address,
8835 block_for_pc (loc->address), 0);
0d381245 8836 if (*arg)
588ae58c 8837 error (_("Garbage '%s' follows condition"), arg);
018d34a4 8838 }
e7e0cddf
SS
8839
8840 /* Dynamic printf requires and uses additional arguments on the
8841 command line, otherwise it's an error. */
8842 if (type == bp_dprintf)
8843 {
8844 if (b->extra_string)
8845 update_dprintf_command_list (b);
8846 else
8847 error (_("Format string required"));
8848 }
8849 else if (b->extra_string)
588ae58c 8850 error (_("Garbage '%s' at end of command"), b->extra_string);
855a6e68 8851 }
018d34a4 8852
56435ebe 8853 b->display_canonical = display_canonical;
f00aae0f 8854 if (location != NULL)
d28cd78a 8855 b->location = std::move (location);
018d34a4 8856 else
d28cd78a 8857 b->location = new_address_location (b->loc->address, NULL, 0);
c0e8dcd8 8858 b->filter = std::move (filter);
d9b3f62e 8859}
018d34a4 8860
d9b3f62e
PA
8861static void
8862create_breakpoint_sal (struct gdbarch *gdbarch,
6c5b2ebe 8863 gdb::array_view<const symtab_and_line> sals,
ffc2605c 8864 event_location_up &&location,
e1e01040
PA
8865 gdb::unique_xmalloc_ptr<char> filter,
8866 gdb::unique_xmalloc_ptr<char> cond_string,
8867 gdb::unique_xmalloc_ptr<char> extra_string,
d9b3f62e
PA
8868 enum bptype type, enum bpdisp disposition,
8869 int thread, int task, int ignore_count,
c0a91b2b 8870 const struct breakpoint_ops *ops, int from_tty,
44f238bb
PA
8871 int enabled, int internal, unsigned flags,
8872 int display_canonical)
d9b3f62e 8873{
a5e364af 8874 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (type);
d9b3f62e 8875
a5e364af 8876 init_breakpoint_sal (b.get (), gdbarch,
ffc2605c 8877 sals, std::move (location),
e1e01040
PA
8878 std::move (filter),
8879 std::move (cond_string),
8880 std::move (extra_string),
d9b3f62e
PA
8881 type, disposition,
8882 thread, task, ignore_count,
8883 ops, from_tty,
44f238bb
PA
8884 enabled, internal, flags,
8885 display_canonical);
d9b3f62e 8886
b270e6f9 8887 install_breakpoint (internal, std::move (b), 0);
018d34a4
VP
8888}
8889
8890/* Add SALS.nelts breakpoints to the breakpoint table. For each
8891 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
8892 value. COND_STRING, if not NULL, specified the condition to be
8893 used for all breakpoints. Essentially the only case where
8894 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
8895 function. In that case, it's still not possible to specify
8896 separate conditions for different overloaded functions, so
8897 we take just a single condition string.
8898
c3f6f71d 8899 NOTE: If the function succeeds, the caller is expected to cleanup
018d34a4 8900 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
c3f6f71d
JM
8901 array contents). If the function fails (error() is called), the
8902 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
4a64f543 8903 COND and SALS arrays and each of those arrays contents. */
c906108c
SS
8904
8905static void
8cdf0e15 8906create_breakpoints_sal (struct gdbarch *gdbarch,
7efd8fc2 8907 struct linespec_result *canonical,
e1e01040
PA
8908 gdb::unique_xmalloc_ptr<char> cond_string,
8909 gdb::unique_xmalloc_ptr<char> extra_string,
8cdf0e15
VP
8910 enum bptype type, enum bpdisp disposition,
8911 int thread, int task, int ignore_count,
c0a91b2b 8912 const struct breakpoint_ops *ops, int from_tty,
44f238bb 8913 int enabled, int internal, unsigned flags)
c906108c 8914{
f8eba3c6 8915 if (canonical->pre_expanded)
6c5b2ebe 8916 gdb_assert (canonical->lsals.size () == 1);
f8eba3c6 8917
6c5b2ebe 8918 for (const auto &lsal : canonical->lsals)
c3f6f71d 8919 {
f00aae0f 8920 /* Note that 'location' can be NULL in the case of a plain
f8eba3c6 8921 'break', without arguments. */
ffc2605c 8922 event_location_up location
f00aae0f 8923 = (canonical->location != NULL
8e9e35b1 8924 ? copy_event_location (canonical->location.get ()) : NULL);
e1e01040 8925 gdb::unique_xmalloc_ptr<char> filter_string
6c5b2ebe 8926 (lsal.canonical != NULL ? xstrdup (lsal.canonical) : NULL);
0d381245 8927
6c5b2ebe 8928 create_breakpoint_sal (gdbarch, lsal.sals,
ffc2605c 8929 std::move (location),
e1e01040
PA
8930 std::move (filter_string),
8931 std::move (cond_string),
8932 std::move (extra_string),
e7e0cddf 8933 type, disposition,
84f4c1fe 8934 thread, task, ignore_count, ops,
44f238bb 8935 from_tty, enabled, internal, flags,
56435ebe 8936 canonical->special_display);
c3f6f71d 8937 }
c3f6f71d 8938}
c906108c 8939
f00aae0f 8940/* Parse LOCATION which is assumed to be a SAL specification possibly
c3f6f71d 8941 followed by conditionals. On return, SALS contains an array of SAL
f00aae0f
KS
8942 addresses found. LOCATION points to the end of the SAL (for
8943 linespec locations).
9998af43
TJB
8944
8945 The array and the line spec strings are allocated on the heap, it is
8946 the caller's responsibility to free them. */
c906108c 8947
b9362cc7 8948static void
f00aae0f 8949parse_breakpoint_sals (const struct event_location *location,
58438ac1 8950 struct linespec_result *canonical)
c3f6f71d 8951{
f00aae0f
KS
8952 struct symtab_and_line cursal;
8953
8954 if (event_location_type (location) == LINESPEC_LOCATION)
8955 {
a20714ff 8956 const char *spec = get_linespec_location (location)->spec_string;
f00aae0f 8957
a20714ff 8958 if (spec == NULL)
f00aae0f
KS
8959 {
8960 /* The last displayed codepoint, if it's valid, is our default
8961 breakpoint address. */
8962 if (last_displayed_sal_is_valid ())
8963 {
f00aae0f
KS
8964 /* Set sal's pspace, pc, symtab, and line to the values
8965 corresponding to the last call to print_frame_info.
8966 Be sure to reinitialize LINE with NOTCURRENT == 0
8967 as the breakpoint line number is inappropriate otherwise.
8968 find_pc_line would adjust PC, re-set it back. */
51abb421
PA
8969 symtab_and_line sal = get_last_displayed_sal ();
8970 CORE_ADDR pc = sal.pc;
8971
f00aae0f
KS
8972 sal = find_pc_line (pc, 0);
8973
8974 /* "break" without arguments is equivalent to "break *PC"
8975 where PC is the last displayed codepoint's address. So
8976 make sure to set sal.explicit_pc to prevent GDB from
8977 trying to expand the list of sals to include all other
8978 instances with the same symtab and line. */
8979 sal.pc = pc;
8980 sal.explicit_pc = 1;
8981
6c5b2ebe
PA
8982 struct linespec_sals lsal;
8983 lsal.sals = {sal};
f00aae0f
KS
8984 lsal.canonical = NULL;
8985
6c5b2ebe 8986 canonical->lsals.push_back (std::move (lsal));
f00aae0f
KS
8987 return;
8988 }
8989 else
8990 error (_("No default breakpoint address now."));
c906108c 8991 }
c906108c 8992 }
f00aae0f
KS
8993
8994 /* Force almost all breakpoints to be in terms of the
8995 current_source_symtab (which is decode_line_1's default).
8996 This should produce the results we want almost all of the
8997 time while leaving default_breakpoint_* alone.
8998
8999 ObjC: However, don't match an Objective-C method name which
9000 may have a '+' or '-' succeeded by a '['. */
9001 cursal = get_current_source_symtab_and_line ();
9002 if (last_displayed_sal_is_valid ())
c906108c 9003 {
a20714ff 9004 const char *spec = NULL;
cc80f267 9005
f00aae0f 9006 if (event_location_type (location) == LINESPEC_LOCATION)
a20714ff 9007 spec = get_linespec_location (location)->spec_string;
cc80f267 9008
f00aae0f 9009 if (!cursal.symtab
a20714ff
PA
9010 || (spec != NULL
9011 && strchr ("+-", spec[0]) != NULL
9012 && spec[1] != '['))
f00aae0f 9013 {
c2f4122d 9014 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
f00aae0f
KS
9015 get_last_displayed_symtab (),
9016 get_last_displayed_line (),
9017 canonical, NULL, NULL);
9018 return;
9019 }
c906108c 9020 }
f00aae0f 9021
c2f4122d 9022 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
f00aae0f 9023 cursal.symtab, cursal.line, canonical, NULL, NULL);
c3f6f71d 9024}
c906108c 9025
c906108c 9026
c3f6f71d 9027/* Convert each SAL into a real PC. Verify that the PC can be
4a64f543 9028 inserted as a breakpoint. If it can't throw an error. */
c906108c 9029
b9362cc7 9030static void
6c5b2ebe 9031breakpoint_sals_to_pc (std::vector<symtab_and_line> &sals)
c3f6f71d 9032{
6c5b2ebe
PA
9033 for (auto &sal : sals)
9034 resolve_sal_pc (&sal);
c3f6f71d
JM
9035}
9036
7a697b8d
SS
9037/* Fast tracepoints may have restrictions on valid locations. For
9038 instance, a fast tracepoint using a jump instead of a trap will
9039 likely have to overwrite more bytes than a trap would, and so can
9040 only be placed where the instruction is longer than the jump, or a
9041 multi-instruction sequence does not have a jump into the middle of
9042 it, etc. */
9043
9044static void
9045check_fast_tracepoint_sals (struct gdbarch *gdbarch,
6c5b2ebe 9046 gdb::array_view<const symtab_and_line> sals)
7a697b8d 9047{
6c5b2ebe 9048 for (const auto &sal : sals)
7a697b8d 9049 {
f8eba3c6
TT
9050 struct gdbarch *sarch;
9051
6c5b2ebe 9052 sarch = get_sal_arch (sal);
f8eba3c6
TT
9053 /* We fall back to GDBARCH if there is no architecture
9054 associated with SAL. */
9055 if (sarch == NULL)
9056 sarch = gdbarch;
281d762b
TT
9057 std::string msg;
9058 if (!gdbarch_fast_tracepoint_valid_at (sarch, sal.pc, &msg))
53c3572a 9059 error (_("May not have a fast tracepoint at %s%s"),
281d762b 9060 paddress (sarch, sal.pc), msg.c_str ());
7a697b8d
SS
9061 }
9062}
9063
018d34a4
VP
9064/* Given TOK, a string specification of condition and thread, as
9065 accepted by the 'break' command, extract the condition
9066 string and thread number and set *COND_STRING and *THREAD.
4a64f543 9067 PC identifies the context at which the condition should be parsed.
018d34a4
VP
9068 If no condition is found, *COND_STRING is set to NULL.
9069 If no thread is found, *THREAD is set to -1. */
d634f2de
JB
9070
9071static void
bbc13ae3 9072find_condition_and_thread (const char *tok, CORE_ADDR pc,
e7e0cddf
SS
9073 char **cond_string, int *thread, int *task,
9074 char **rest)
018d34a4
VP
9075{
9076 *cond_string = NULL;
9077 *thread = -1;
ed1d1739
KS
9078 *task = 0;
9079 *rest = NULL;
9080
018d34a4
VP
9081 while (tok && *tok)
9082 {
bbc13ae3 9083 const char *end_tok;
018d34a4 9084 int toklen;
bbc13ae3
KS
9085 const char *cond_start = NULL;
9086 const char *cond_end = NULL;
cc59ec59 9087
f1735a53 9088 tok = skip_spaces (tok);
e7e0cddf
SS
9089
9090 if ((*tok == '"' || *tok == ',') && rest)
9091 {
9092 *rest = savestring (tok, strlen (tok));
9093 return;
9094 }
9095
f1735a53 9096 end_tok = skip_to_space (tok);
d634f2de 9097
018d34a4 9098 toklen = end_tok - tok;
d634f2de 9099
018d34a4
VP
9100 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
9101 {
9102 tok = cond_start = end_tok + 1;
4d01a485 9103 parse_exp_1 (&tok, pc, block_for_pc (pc), 0);
018d34a4 9104 cond_end = tok;
d634f2de 9105 *cond_string = savestring (cond_start, cond_end - cond_start);
018d34a4
VP
9106 }
9107 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
9108 {
5d5658a1
PA
9109 const char *tmptok;
9110 struct thread_info *thr;
d634f2de 9111
018d34a4 9112 tok = end_tok + 1;
5d5658a1 9113 thr = parse_thread_id (tok, &tmptok);
018d34a4
VP
9114 if (tok == tmptok)
9115 error (_("Junk after thread keyword."));
5d5658a1 9116 *thread = thr->global_num;
bbc13ae3 9117 tok = tmptok;
018d34a4 9118 }
4a306c9a
JB
9119 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
9120 {
9121 char *tmptok;
9122
9123 tok = end_tok + 1;
bbc13ae3 9124 *task = strtol (tok, &tmptok, 0);
4a306c9a
JB
9125 if (tok == tmptok)
9126 error (_("Junk after task keyword."));
9127 if (!valid_task_id (*task))
b6199126 9128 error (_("Unknown task %d."), *task);
bbc13ae3 9129 tok = tmptok;
4a306c9a 9130 }
e7e0cddf
SS
9131 else if (rest)
9132 {
9133 *rest = savestring (tok, strlen (tok));
ccab2054 9134 return;
e7e0cddf 9135 }
018d34a4
VP
9136 else
9137 error (_("Junk at end of arguments."));
9138 }
9139}
9140
0fb4aa4b
PA
9141/* Decode a static tracepoint marker spec. */
9142
6c5b2ebe 9143static std::vector<symtab_and_line>
f00aae0f 9144decode_static_tracepoint_spec (const char **arg_p)
0fb4aa4b 9145{
f00aae0f
KS
9146 const char *p = &(*arg_p)[3];
9147 const char *endp;
0fb4aa4b 9148
f1735a53 9149 p = skip_spaces (p);
0fb4aa4b 9150
f1735a53 9151 endp = skip_to_space (p);
0fb4aa4b 9152
81b1e71c 9153 std::string marker_str (p, endp - p);
0fb4aa4b 9154
5d9310c4
SM
9155 std::vector<static_tracepoint_marker> markers
9156 = target_static_tracepoint_markers_by_strid (marker_str.c_str ());
9157 if (markers.empty ())
81b1e71c
TT
9158 error (_("No known static tracepoint marker named %s"),
9159 marker_str.c_str ());
0fb4aa4b 9160
6c5b2ebe 9161 std::vector<symtab_and_line> sals;
5d9310c4 9162 sals.reserve (markers.size ());
0fb4aa4b 9163
5d9310c4 9164 for (const static_tracepoint_marker &marker : markers)
0fb4aa4b 9165 {
5d9310c4
SM
9166 symtab_and_line sal = find_pc_line (marker.address, 0);
9167 sal.pc = marker.address;
6c5b2ebe 9168 sals.push_back (sal);
5d9310c4 9169 }
0fb4aa4b 9170
0fb4aa4b
PA
9171 *arg_p = endp;
9172 return sals;
9173}
9174
bac7c5cf
GB
9175/* Returns the breakpoint ops appropriate for use with with LOCATION_TYPE and
9176 according to IS_TRACEPOINT. */
9177
9178static const struct breakpoint_ops *
9179breakpoint_ops_for_event_location_type (enum event_location_type location_type,
9180 bool is_tracepoint)
9181{
9182 if (is_tracepoint)
9183 {
9184 if (location_type == PROBE_LOCATION)
9185 return &tracepoint_probe_breakpoint_ops;
9186 else
9187 return &tracepoint_breakpoint_ops;
9188 }
9189 else
9190 {
9191 if (location_type == PROBE_LOCATION)
9192 return &bkpt_probe_breakpoint_ops;
9193 else
9194 return &bkpt_breakpoint_ops;
9195 }
9196}
9197
9198/* See breakpoint.h. */
9199
9200const struct breakpoint_ops *
9201breakpoint_ops_for_event_location (const struct event_location *location,
9202 bool is_tracepoint)
9203{
9204 if (location != nullptr)
9205 return breakpoint_ops_for_event_location_type
9206 (event_location_type (location), is_tracepoint);
9207 return is_tracepoint ? &tracepoint_breakpoint_ops : &bkpt_breakpoint_ops;
9208}
9209
f00aae0f 9210/* See breakpoint.h. */
0101ce28 9211
8cdf0e15
VP
9212int
9213create_breakpoint (struct gdbarch *gdbarch,
e1e01040
PA
9214 const struct event_location *location,
9215 const char *cond_string,
9216 int thread, const char *extra_string,
f00aae0f 9217 int parse_extra,
0fb4aa4b 9218 int tempflag, enum bptype type_wanted,
8cdf0e15
VP
9219 int ignore_count,
9220 enum auto_boolean pending_break_support,
c0a91b2b 9221 const struct breakpoint_ops *ops,
44f238bb
PA
9222 int from_tty, int enabled, int internal,
9223 unsigned flags)
c3f6f71d 9224{
7efd8fc2 9225 struct linespec_result canonical;
0101ce28 9226 int pending = 0;
4a306c9a 9227 int task = 0;
86b17b60 9228 int prev_bkpt_count = breakpoint_count;
c3f6f71d 9229
348d480f
PA
9230 gdb_assert (ops != NULL);
9231
f00aae0f
KS
9232 /* If extra_string isn't useful, set it to NULL. */
9233 if (extra_string != NULL && *extra_string == '\0')
9234 extra_string = NULL;
9235
a70b8144 9236 try
b78a6381 9237 {
f00aae0f 9238 ops->create_sals_from_location (location, &canonical, type_wanted);
b78a6381 9239 }
230d2906 9240 catch (const gdb_exception_error &e)
0101ce28 9241 {
492d29ea
PA
9242 /* If caller is interested in rc value from parse, set
9243 value. */
9244 if (e.error == NOT_FOUND_ERROR)
0101ce28 9245 {
05ff989b
AC
9246 /* If pending breakpoint support is turned off, throw
9247 error. */
fa8d40ab
JJ
9248
9249 if (pending_break_support == AUTO_BOOLEAN_FALSE)
eedc3f4f 9250 throw;
723a2275
VP
9251
9252 exception_print (gdb_stderr, e);
fa8d40ab 9253
05ff989b
AC
9254 /* If pending breakpoint support is auto query and the user
9255 selects no, then simply return the error code. */
059fb39f 9256 if (pending_break_support == AUTO_BOOLEAN_AUTO
bfccc43c
YQ
9257 && !nquery (_("Make %s pending on future shared library load? "),
9258 bptype_string (type_wanted)))
fd9b8c24 9259 return 0;
fa8d40ab 9260
05ff989b
AC
9261 /* At this point, either the user was queried about setting
9262 a pending breakpoint and selected yes, or pending
9263 breakpoint behavior is on and thus a pending breakpoint
9264 is defaulted on behalf of the user. */
f00aae0f 9265 pending = 1;
0101ce28 9266 }
492d29ea 9267 else
eedc3f4f 9268 throw;
0101ce28 9269 }
492d29ea 9270
6c5b2ebe 9271 if (!pending && canonical.lsals.empty ())
492d29ea 9272 return 0;
c3f6f71d 9273
c3f6f71d
JM
9274 /* Resolve all line numbers to PC's and verify that the addresses
9275 are ok for the target. */
0101ce28 9276 if (!pending)
f8eba3c6 9277 {
6c5b2ebe
PA
9278 for (auto &lsal : canonical.lsals)
9279 breakpoint_sals_to_pc (lsal.sals);
f8eba3c6 9280 }
c3f6f71d 9281
7a697b8d 9282 /* Fast tracepoints may have additional restrictions on location. */
bfccc43c 9283 if (!pending && type_wanted == bp_fast_tracepoint)
f8eba3c6 9284 {
6c5b2ebe
PA
9285 for (const auto &lsal : canonical.lsals)
9286 check_fast_tracepoint_sals (gdbarch, lsal.sals);
f8eba3c6 9287 }
7a697b8d 9288
c3f6f71d
JM
9289 /* Verify that condition can be parsed, before setting any
9290 breakpoints. Allocate a separate condition expression for each
4a64f543 9291 breakpoint. */
0101ce28 9292 if (!pending)
c3f6f71d 9293 {
e1e01040
PA
9294 gdb::unique_xmalloc_ptr<char> cond_string_copy;
9295 gdb::unique_xmalloc_ptr<char> extra_string_copy;
9296
f00aae0f 9297 if (parse_extra)
72b2ff0e 9298 {
0878d0fa 9299 char *rest;
e1e01040 9300 char *cond;
52d361e1 9301
6c5b2ebe 9302 const linespec_sals &lsal = canonical.lsals[0];
52d361e1 9303
0878d0fa
YQ
9304 /* Here we only parse 'arg' to separate condition
9305 from thread number, so parsing in context of first
9306 sal is OK. When setting the breakpoint we'll
9307 re-parse it in context of each sal. */
9308
6c5b2ebe 9309 find_condition_and_thread (extra_string, lsal.sals[0].pc,
e1e01040
PA
9310 &cond, &thread, &task, &rest);
9311 cond_string_copy.reset (cond);
9312 extra_string_copy.reset (rest);
72b2ff0e 9313 }
2f069f6f 9314 else
72b2ff0e 9315 {
f00aae0f
KS
9316 if (type_wanted != bp_dprintf
9317 && extra_string != NULL && *extra_string != '\0')
9318 error (_("Garbage '%s' at end of location"), extra_string);
0878d0fa
YQ
9319
9320 /* Create a private copy of condition string. */
9321 if (cond_string)
e1e01040 9322 cond_string_copy.reset (xstrdup (cond_string));
0878d0fa
YQ
9323 /* Create a private copy of any extra string. */
9324 if (extra_string)
e1e01040 9325 extra_string_copy.reset (xstrdup (extra_string));
72b2ff0e 9326 }
0fb4aa4b 9327
52d361e1 9328 ops->create_breakpoints_sal (gdbarch, &canonical,
e1e01040
PA
9329 std::move (cond_string_copy),
9330 std::move (extra_string_copy),
9331 type_wanted,
d9b3f62e
PA
9332 tempflag ? disp_del : disp_donttouch,
9333 thread, task, ignore_count, ops,
44f238bb 9334 from_tty, enabled, internal, flags);
c906108c 9335 }
0101ce28
JJ
9336 else
9337 {
a5e364af 9338 std::unique_ptr <breakpoint> b = new_breakpoint_from_type (type_wanted);
bfccc43c 9339
a5e364af 9340 init_raw_breakpoint_without_location (b.get (), gdbarch, type_wanted, ops);
d28cd78a 9341 b->location = copy_event_location (location);
bfccc43c 9342
f00aae0f
KS
9343 if (parse_extra)
9344 b->cond_string = NULL;
e12c7713
MK
9345 else
9346 {
9347 /* Create a private copy of condition string. */
e1e01040 9348 b->cond_string = cond_string != NULL ? xstrdup (cond_string) : NULL;
15630549 9349 b->thread = thread;
e12c7713 9350 }
f00aae0f
KS
9351
9352 /* Create a private copy of any extra string. */
e1e01040 9353 b->extra_string = extra_string != NULL ? xstrdup (extra_string) : NULL;
0101ce28 9354 b->ignore_count = ignore_count;
0101ce28 9355 b->disposition = tempflag ? disp_del : disp_donttouch;
0d381245 9356 b->condition_not_parsed = 1;
41447f92 9357 b->enable_state = enabled ? bp_enabled : bp_disabled;
cc72b2a2
KP
9358 if ((type_wanted != bp_breakpoint
9359 && type_wanted != bp_hardware_breakpoint) || thread != -1)
f8eba3c6 9360 b->pspace = current_program_space;
8bea4e01 9361
b270e6f9 9362 install_breakpoint (internal, std::move (b), 0);
0101ce28
JJ
9363 }
9364
6c5b2ebe 9365 if (canonical.lsals.size () > 1)
95a42b64 9366 {
3e43a32a
MS
9367 warning (_("Multiple breakpoints were set.\nUse the "
9368 "\"delete\" command to delete unwanted breakpoints."));
86b17b60 9369 prev_breakpoint_count = prev_bkpt_count;
95a42b64
TT
9370 }
9371
44702360 9372 update_global_location_list (UGLL_MAY_INSERT);
fd9b8c24
PA
9373
9374 return 1;
c3f6f71d 9375}
c906108c 9376
348d480f 9377/* Set a breakpoint.
72b2ff0e
VP
9378 ARG is a string describing breakpoint address,
9379 condition, and thread.
9380 FLAG specifies if a breakpoint is hardware on,
9381 and if breakpoint is temporary, using BP_HARDWARE_FLAG
9382 and BP_TEMPFLAG. */
348d480f 9383
98deb0da 9384static void
f2fc3015 9385break_command_1 (const char *arg, int flag, int from_tty)
c3f6f71d 9386{
72b2ff0e 9387 int tempflag = flag & BP_TEMPFLAG;
0fb4aa4b
PA
9388 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
9389 ? bp_hardware_breakpoint
9390 : bp_breakpoint);
f00aae0f 9391
ffc2605c 9392 event_location_up location = string_to_event_location (&arg, current_language);
bac7c5cf
GB
9393 const struct breakpoint_ops *ops = breakpoint_ops_for_event_location
9394 (location.get (), false /* is_tracepoint */);
c3f6f71d 9395
8cdf0e15 9396 create_breakpoint (get_current_arch (),
ffc2605c 9397 location.get (),
f00aae0f 9398 NULL, 0, arg, 1 /* parse arg */,
0fb4aa4b 9399 tempflag, type_wanted,
8cdf0e15
VP
9400 0 /* Ignore count */,
9401 pending_break_support,
55aa24fb 9402 ops,
8cdf0e15 9403 from_tty,
84f4c1fe 9404 1 /* enabled */,
44f238bb
PA
9405 0 /* internal */,
9406 0);
c906108c
SS
9407}
9408
c906108c
SS
9409/* Helper function for break_command_1 and disassemble_command. */
9410
9411void
fba45db2 9412resolve_sal_pc (struct symtab_and_line *sal)
c906108c
SS
9413{
9414 CORE_ADDR pc;
9415
9416 if (sal->pc == 0 && sal->symtab != NULL)
9417 {
9418 if (!find_line_pc (sal->symtab, sal->line, &pc))
8a3fe4f8 9419 error (_("No line %d in file \"%s\"."),
05cba821 9420 sal->line, symtab_to_filename_for_display (sal->symtab));
c906108c 9421 sal->pc = pc;
6a048695 9422
4a64f543
MS
9423 /* If this SAL corresponds to a breakpoint inserted using a line
9424 number, then skip the function prologue if necessary. */
6a048695 9425 if (sal->explicit_line)
059acae7 9426 skip_prologue_sal (sal);
c906108c
SS
9427 }
9428
9429 if (sal->section == 0 && sal->symtab != NULL)
9430 {
346d1dfe 9431 const struct blockvector *bv;
3977b71f 9432 const struct block *b;
c5aa993b 9433 struct symbol *sym;
c906108c 9434
43f3e411
DE
9435 bv = blockvector_for_pc_sect (sal->pc, 0, &b,
9436 SYMTAB_COMPUNIT (sal->symtab));
c906108c
SS
9437 if (bv != NULL)
9438 {
7f0df278 9439 sym = block_linkage_function (b);
c906108c
SS
9440 if (sym != NULL)
9441 {
eb822aa6
DE
9442 fixup_symbol_section (sym, SYMTAB_OBJFILE (sal->symtab));
9443 sal->section = SYMBOL_OBJ_SECTION (SYMTAB_OBJFILE (sal->symtab),
9444 sym);
c906108c
SS
9445 }
9446 else
9447 {
4a64f543
MS
9448 /* It really is worthwhile to have the section, so we'll
9449 just have to look harder. This case can be executed
9450 if we have line numbers but no functions (as can
9451 happen in assembly source). */
c906108c 9452
5ed8105e 9453 scoped_restore_current_pspace_and_thread restore_pspace_thread;
6c95b8df 9454 switch_to_program_space_and_thread (sal->pspace);
c906108c 9455
5ed8105e 9456 bound_minimal_symbol msym = lookup_minimal_symbol_by_pc (sal->pc);
7cbd4a93 9457 if (msym.minsym)
efd66ac6 9458 sal->section = MSYMBOL_OBJ_SECTION (msym.objfile, msym.minsym);
c906108c
SS
9459 }
9460 }
9461 }
9462}
9463
9464void
0b39b52e 9465break_command (const char *arg, int from_tty)
c906108c 9466{
db107f19 9467 break_command_1 (arg, 0, from_tty);
c906108c
SS
9468}
9469
c906108c 9470void
0b39b52e 9471tbreak_command (const char *arg, int from_tty)
c906108c 9472{
db107f19 9473 break_command_1 (arg, BP_TEMPFLAG, from_tty);
c906108c
SS
9474}
9475
c906108c 9476static void
0b39b52e 9477hbreak_command (const char *arg, int from_tty)
c906108c 9478{
db107f19 9479 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
c906108c
SS
9480}
9481
9482static void
0b39b52e 9483thbreak_command (const char *arg, int from_tty)
c906108c 9484{
db107f19 9485 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
c906108c
SS
9486}
9487
9488static void
ee7ddd71 9489stop_command (const char *arg, int from_tty)
c906108c 9490{
a3f17187 9491 printf_filtered (_("Specify the type of breakpoint to set.\n\
c906108c 9492Usage: stop in <function | address>\n\
a3f17187 9493 stop at <line>\n"));
c906108c
SS
9494}
9495
9496static void
4495129a 9497stopin_command (const char *arg, int from_tty)
c906108c
SS
9498{
9499 int badInput = 0;
9500
cafb3438 9501 if (arg == NULL)
c906108c
SS
9502 badInput = 1;
9503 else if (*arg != '*')
9504 {
4495129a 9505 const char *argptr = arg;
c906108c
SS
9506 int hasColon = 0;
9507
4a64f543 9508 /* Look for a ':'. If this is a line number specification, then
53a5351d 9509 say it is bad, otherwise, it should be an address or
4a64f543 9510 function/method name. */
c906108c 9511 while (*argptr && !hasColon)
c5aa993b
JM
9512 {
9513 hasColon = (*argptr == ':');
9514 argptr++;
9515 }
c906108c
SS
9516
9517 if (hasColon)
c5aa993b 9518 badInput = (*argptr != ':'); /* Not a class::method */
c906108c 9519 else
c5aa993b 9520 badInput = isdigit (*arg); /* a simple line number */
c906108c
SS
9521 }
9522
9523 if (badInput)
a3f17187 9524 printf_filtered (_("Usage: stop in <function | address>\n"));
c906108c 9525 else
db107f19 9526 break_command_1 (arg, 0, from_tty);
c906108c
SS
9527}
9528
9529static void
4495129a 9530stopat_command (const char *arg, int from_tty)
c906108c
SS
9531{
9532 int badInput = 0;
9533
cafb3438 9534 if (arg == NULL || *arg == '*') /* no line number */
c906108c
SS
9535 badInput = 1;
9536 else
9537 {
4495129a 9538 const char *argptr = arg;
c906108c
SS
9539 int hasColon = 0;
9540
4a64f543
MS
9541 /* Look for a ':'. If there is a '::' then get out, otherwise
9542 it is probably a line number. */
c906108c 9543 while (*argptr && !hasColon)
c5aa993b
JM
9544 {
9545 hasColon = (*argptr == ':');
9546 argptr++;
9547 }
c906108c
SS
9548
9549 if (hasColon)
c5aa993b 9550 badInput = (*argptr == ':'); /* we have class::method */
c906108c 9551 else
c5aa993b 9552 badInput = !isdigit (*arg); /* not a line number */
c906108c
SS
9553 }
9554
9555 if (badInput)
65e65158 9556 printf_filtered (_("Usage: stop at LINE\n"));
c906108c 9557 else
db107f19 9558 break_command_1 (arg, 0, from_tty);
c906108c
SS
9559}
9560
e7e0cddf
SS
9561/* The dynamic printf command is mostly like a regular breakpoint, but
9562 with a prewired command list consisting of a single output command,
9563 built from extra arguments supplied on the dprintf command
9564 line. */
9565
da821c7b 9566static void
0b39b52e 9567dprintf_command (const char *arg, int from_tty)
e7e0cddf 9568{
ffc2605c 9569 event_location_up location = string_to_event_location (&arg, current_language);
f00aae0f
KS
9570
9571 /* If non-NULL, ARG should have been advanced past the location;
9572 the next character must be ','. */
9573 if (arg != NULL)
9574 {
9575 if (arg[0] != ',' || arg[1] == '\0')
9576 error (_("Format string required"));
9577 else
9578 {
9579 /* Skip the comma. */
9580 ++arg;
9581 }
9582 }
9583
e7e0cddf 9584 create_breakpoint (get_current_arch (),
ffc2605c 9585 location.get (),
f00aae0f 9586 NULL, 0, arg, 1 /* parse arg */,
e7e0cddf
SS
9587 0, bp_dprintf,
9588 0 /* Ignore count */,
9589 pending_break_support,
9590 &dprintf_breakpoint_ops,
9591 from_tty,
9592 1 /* enabled */,
9593 0 /* internal */,
9594 0);
9595}
9596
d3ce09f5 9597static void
0b39b52e 9598agent_printf_command (const char *arg, int from_tty)
d3ce09f5
SS
9599{
9600 error (_("May only run agent-printf on the target"));
9601}
9602
f1310107
TJB
9603/* Implement the "breakpoint_hit" breakpoint_ops method for
9604 ranged breakpoints. */
9605
9606static int
9607breakpoint_hit_ranged_breakpoint (const struct bp_location *bl,
bd522513 9608 const address_space *aspace,
09ac7c10
TT
9609 CORE_ADDR bp_addr,
9610 const struct target_waitstatus *ws)
f1310107 9611{
09ac7c10 9612 if (ws->kind != TARGET_WAITKIND_STOPPED
a493e3e2 9613 || ws->value.sig != GDB_SIGNAL_TRAP)
09ac7c10
TT
9614 return 0;
9615
f1310107
TJB
9616 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
9617 bl->length, aspace, bp_addr);
9618}
9619
9620/* Implement the "resources_needed" breakpoint_ops method for
9621 ranged breakpoints. */
9622
9623static int
9624resources_needed_ranged_breakpoint (const struct bp_location *bl)
9625{
9626 return target_ranged_break_num_registers ();
9627}
9628
9629/* Implement the "print_it" breakpoint_ops method for
9630 ranged breakpoints. */
9631
9632static enum print_stop_action
348d480f 9633print_it_ranged_breakpoint (bpstat bs)
f1310107 9634{
348d480f 9635 struct breakpoint *b = bs->breakpoint_at;
f1310107 9636 struct bp_location *bl = b->loc;
79a45e25 9637 struct ui_out *uiout = current_uiout;
f1310107
TJB
9638
9639 gdb_assert (b->type == bp_hardware_breakpoint);
9640
9641 /* Ranged breakpoints have only one location. */
9642 gdb_assert (bl && bl->next == NULL);
9643
9644 annotate_breakpoint (b->number);
f303dbd6
PA
9645
9646 maybe_print_thread_hit_breakpoint (uiout);
9647
f1310107 9648 if (b->disposition == disp_del)
112e8700 9649 uiout->text ("Temporary ranged breakpoint ");
f1310107 9650 else
112e8700
SM
9651 uiout->text ("Ranged breakpoint ");
9652 if (uiout->is_mi_like_p ())
f1310107 9653 {
112e8700 9654 uiout->field_string ("reason",
f1310107 9655 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
112e8700 9656 uiout->field_string ("disp", bpdisp_text (b->disposition));
f1310107 9657 }
381befee 9658 uiout->field_signed ("bkptno", b->number);
112e8700 9659 uiout->text (", ");
f1310107
TJB
9660
9661 return PRINT_SRC_AND_LOC;
9662}
9663
9664/* Implement the "print_one" breakpoint_ops method for
9665 ranged breakpoints. */
9666
9667static void
9668print_one_ranged_breakpoint (struct breakpoint *b,
9669 struct bp_location **last_loc)
9670{
9671 struct bp_location *bl = b->loc;
9672 struct value_print_options opts;
79a45e25 9673 struct ui_out *uiout = current_uiout;
f1310107
TJB
9674
9675 /* Ranged breakpoints have only one location. */
9676 gdb_assert (bl && bl->next == NULL);
9677
9678 get_user_print_options (&opts);
9679
9680 if (opts.addressprint)
9681 /* We don't print the address range here, it will be printed later
9682 by print_one_detail_ranged_breakpoint. */
112e8700 9683 uiout->field_skip ("addr");
f1310107
TJB
9684 annotate_field (5);
9685 print_breakpoint_location (b, bl);
9686 *last_loc = bl;
9687}
9688
9689/* Implement the "print_one_detail" breakpoint_ops method for
9690 ranged breakpoints. */
9691
9692static void
9693print_one_detail_ranged_breakpoint (const struct breakpoint *b,
9694 struct ui_out *uiout)
9695{
9696 CORE_ADDR address_start, address_end;
9697 struct bp_location *bl = b->loc;
d7e74731 9698 string_file stb;
f1310107
TJB
9699
9700 gdb_assert (bl);
9701
9702 address_start = bl->address;
9703 address_end = address_start + bl->length - 1;
9704
112e8700 9705 uiout->text ("\taddress range: ");
d7e74731
PA
9706 stb.printf ("[%s, %s]",
9707 print_core_address (bl->gdbarch, address_start),
9708 print_core_address (bl->gdbarch, address_end));
112e8700
SM
9709 uiout->field_stream ("addr", stb);
9710 uiout->text ("\n");
f1310107
TJB
9711}
9712
9713/* Implement the "print_mention" breakpoint_ops method for
9714 ranged breakpoints. */
9715
9716static void
9717print_mention_ranged_breakpoint (struct breakpoint *b)
9718{
9719 struct bp_location *bl = b->loc;
79a45e25 9720 struct ui_out *uiout = current_uiout;
f1310107
TJB
9721
9722 gdb_assert (bl);
9723 gdb_assert (b->type == bp_hardware_breakpoint);
9724
2d33446d
TT
9725 uiout->message (_("Hardware assisted ranged breakpoint %d from %s to %s."),
9726 b->number, paddress (bl->gdbarch, bl->address),
9727 paddress (bl->gdbarch, bl->address + bl->length - 1));
f1310107
TJB
9728}
9729
9730/* Implement the "print_recreate" breakpoint_ops method for
9731 ranged breakpoints. */
9732
9733static void
9734print_recreate_ranged_breakpoint (struct breakpoint *b, struct ui_file *fp)
9735{
f00aae0f 9736 fprintf_unfiltered (fp, "break-range %s, %s",
d28cd78a
TT
9737 event_location_to_string (b->location.get ()),
9738 event_location_to_string (b->location_range_end.get ()));
d9b3f62e 9739 print_recreate_thread (b, fp);
f1310107
TJB
9740}
9741
9742/* The breakpoint_ops structure to be used in ranged breakpoints. */
9743
2060206e 9744static struct breakpoint_ops ranged_breakpoint_ops;
f1310107
TJB
9745
9746/* Find the address where the end of the breakpoint range should be
9747 placed, given the SAL of the end of the range. This is so that if
9748 the user provides a line number, the end of the range is set to the
9749 last instruction of the given line. */
9750
9751static CORE_ADDR
9752find_breakpoint_range_end (struct symtab_and_line sal)
9753{
9754 CORE_ADDR end;
9755
9756 /* If the user provided a PC value, use it. Otherwise,
9757 find the address of the end of the given location. */
9758 if (sal.explicit_pc)
9759 end = sal.pc;
9760 else
9761 {
9762 int ret;
9763 CORE_ADDR start;
9764
9765 ret = find_line_pc_range (sal, &start, &end);
9766 if (!ret)
9767 error (_("Could not find location of the end of the range."));
9768
9769 /* find_line_pc_range returns the start of the next line. */
9770 end--;
9771 }
9772
9773 return end;
9774}
9775
9776/* Implement the "break-range" CLI command. */
9777
9778static void
0b39b52e 9779break_range_command (const char *arg, int from_tty)
f1310107 9780{
f2fc3015 9781 const char *arg_start;
f1310107
TJB
9782 struct linespec_result canonical_start, canonical_end;
9783 int bp_count, can_use_bp, length;
9784 CORE_ADDR end;
9785 struct breakpoint *b;
f1310107
TJB
9786
9787 /* We don't support software ranged breakpoints. */
9788 if (target_ranged_break_num_registers () < 0)
9789 error (_("This target does not support hardware ranged breakpoints."));
9790
9791 bp_count = hw_breakpoint_used_count ();
9792 bp_count += target_ranged_break_num_registers ();
9793 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
9794 bp_count, 0);
9795 if (can_use_bp < 0)
9796 error (_("Hardware breakpoints used exceeds limit."));
9797
f8eba3c6 9798 arg = skip_spaces (arg);
f1310107
TJB
9799 if (arg == NULL || arg[0] == '\0')
9800 error(_("No address range specified."));
9801
f8eba3c6 9802 arg_start = arg;
ffc2605c
TT
9803 event_location_up start_location = string_to_event_location (&arg,
9804 current_language);
9805 parse_breakpoint_sals (start_location.get (), &canonical_start);
f1310107
TJB
9806
9807 if (arg[0] != ',')
9808 error (_("Too few arguments."));
6c5b2ebe 9809 else if (canonical_start.lsals.empty ())
f1310107 9810 error (_("Could not find location of the beginning of the range."));
f8eba3c6 9811
6c5b2ebe 9812 const linespec_sals &lsal_start = canonical_start.lsals[0];
f8eba3c6 9813
6c5b2ebe
PA
9814 if (canonical_start.lsals.size () > 1
9815 || lsal_start.sals.size () != 1)
f1310107
TJB
9816 error (_("Cannot create a ranged breakpoint with multiple locations."));
9817
6c5b2ebe 9818 const symtab_and_line &sal_start = lsal_start.sals[0];
81b1e71c 9819 std::string addr_string_start (arg_start, arg - arg_start);
f1310107
TJB
9820
9821 arg++; /* Skip the comma. */
f8eba3c6 9822 arg = skip_spaces (arg);
f1310107
TJB
9823
9824 /* Parse the end location. */
9825
f1310107
TJB
9826 arg_start = arg;
9827
f8eba3c6 9828 /* We call decode_line_full directly here instead of using
f1310107
TJB
9829 parse_breakpoint_sals because we need to specify the start location's
9830 symtab and line as the default symtab and line for the end of the
9831 range. This makes it possible to have ranges like "foo.c:27, +14",
9832 where +14 means 14 lines from the start location. */
ffc2605c
TT
9833 event_location_up end_location = string_to_event_location (&arg,
9834 current_language);
9835 decode_line_full (end_location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
f8eba3c6
TT
9836 sal_start.symtab, sal_start.line,
9837 &canonical_end, NULL, NULL);
9838
6c5b2ebe 9839 if (canonical_end.lsals.empty ())
f1310107 9840 error (_("Could not find location of the end of the range."));
f8eba3c6 9841
6c5b2ebe
PA
9842 const linespec_sals &lsal_end = canonical_end.lsals[0];
9843 if (canonical_end.lsals.size () > 1
9844 || lsal_end.sals.size () != 1)
f1310107
TJB
9845 error (_("Cannot create a ranged breakpoint with multiple locations."));
9846
6c5b2ebe 9847 const symtab_and_line &sal_end = lsal_end.sals[0];
f1310107
TJB
9848
9849 end = find_breakpoint_range_end (sal_end);
9850 if (sal_start.pc > end)
177b42fe 9851 error (_("Invalid address range, end precedes start."));
f1310107
TJB
9852
9853 length = end - sal_start.pc + 1;
9854 if (length < 0)
9855 /* Length overflowed. */
9856 error (_("Address range too large."));
9857 else if (length == 1)
9858 {
9859 /* This range is simple enough to be handled by
9860 the `hbreak' command. */
81b1e71c 9861 hbreak_command (&addr_string_start[0], 1);
f1310107
TJB
9862
9863 return;
9864 }
9865
9866 /* Now set up the breakpoint. */
9867 b = set_raw_breakpoint (get_current_arch (), sal_start,
348d480f 9868 bp_hardware_breakpoint, &ranged_breakpoint_ops);
f1310107
TJB
9869 set_breakpoint_count (breakpoint_count + 1);
9870 b->number = breakpoint_count;
9871 b->disposition = disp_donttouch;
d28cd78a
TT
9872 b->location = std::move (start_location);
9873 b->location_range_end = std::move (end_location);
f1310107
TJB
9874 b->loc->length = length;
9875
f1310107 9876 mention (b);
76727919 9877 gdb::observers::breakpoint_created.notify (b);
44702360 9878 update_global_location_list (UGLL_MAY_INSERT);
f1310107
TJB
9879}
9880
4a64f543
MS
9881/* Return non-zero if EXP is verified as constant. Returned zero
9882 means EXP is variable. Also the constant detection may fail for
9883 some constant expressions and in such case still falsely return
9884 zero. */
2e6e3d9c 9885
65d79d4b
SDJ
9886static int
9887watchpoint_exp_is_const (const struct expression *exp)
9888{
9889 int i = exp->nelts;
9890
9891 while (i > 0)
9892 {
9893 int oplenp, argsp;
9894
9895 /* We are only interested in the descriptor of each element. */
9896 operator_length (exp, i, &oplenp, &argsp);
9897 i -= oplenp;
9898
9899 switch (exp->elts[i].opcode)
9900 {
9901 case BINOP_ADD:
9902 case BINOP_SUB:
9903 case BINOP_MUL:
9904 case BINOP_DIV:
9905 case BINOP_REM:
9906 case BINOP_MOD:
9907 case BINOP_LSH:
9908 case BINOP_RSH:
9909 case BINOP_LOGICAL_AND:
9910 case BINOP_LOGICAL_OR:
9911 case BINOP_BITWISE_AND:
9912 case BINOP_BITWISE_IOR:
9913 case BINOP_BITWISE_XOR:
9914 case BINOP_EQUAL:
9915 case BINOP_NOTEQUAL:
9916 case BINOP_LESS:
9917 case BINOP_GTR:
9918 case BINOP_LEQ:
9919 case BINOP_GEQ:
9920 case BINOP_REPEAT:
9921 case BINOP_COMMA:
9922 case BINOP_EXP:
9923 case BINOP_MIN:
9924 case BINOP_MAX:
9925 case BINOP_INTDIV:
9926 case BINOP_CONCAT:
65d79d4b
SDJ
9927 case TERNOP_COND:
9928 case TERNOP_SLICE:
65d79d4b
SDJ
9929
9930 case OP_LONG:
edd079d9 9931 case OP_FLOAT:
65d79d4b
SDJ
9932 case OP_LAST:
9933 case OP_COMPLEX:
9934 case OP_STRING:
65d79d4b
SDJ
9935 case OP_ARRAY:
9936 case OP_TYPE:
608b4967
TT
9937 case OP_TYPEOF:
9938 case OP_DECLTYPE:
6e72ca20 9939 case OP_TYPEID:
65d79d4b
SDJ
9940 case OP_NAME:
9941 case OP_OBJC_NSSTRING:
9942
9943 case UNOP_NEG:
9944 case UNOP_LOGICAL_NOT:
9945 case UNOP_COMPLEMENT:
9946 case UNOP_ADDR:
9947 case UNOP_HIGH:
aeaa2474 9948 case UNOP_CAST:
9eaf6705
TT
9949
9950 case UNOP_CAST_TYPE:
9951 case UNOP_REINTERPRET_CAST:
9952 case UNOP_DYNAMIC_CAST:
4a64f543
MS
9953 /* Unary, binary and ternary operators: We have to check
9954 their operands. If they are constant, then so is the
9955 result of that operation. For instance, if A and B are
9956 determined to be constants, then so is "A + B".
9957
9958 UNOP_IND is one exception to the rule above, because the
9959 value of *ADDR is not necessarily a constant, even when
9960 ADDR is. */
65d79d4b
SDJ
9961 break;
9962
9963 case OP_VAR_VALUE:
9964 /* Check whether the associated symbol is a constant.
4a64f543 9965
65d79d4b 9966 We use SYMBOL_CLASS rather than TYPE_CONST because it's
4a64f543
MS
9967 possible that a buggy compiler could mark a variable as
9968 constant even when it is not, and TYPE_CONST would return
9969 true in this case, while SYMBOL_CLASS wouldn't.
9970
9971 We also have to check for function symbols because they
9972 are always constant. */
65d79d4b
SDJ
9973 {
9974 struct symbol *s = exp->elts[i + 2].symbol;
9975
9976 if (SYMBOL_CLASS (s) != LOC_BLOCK
9977 && SYMBOL_CLASS (s) != LOC_CONST
9978 && SYMBOL_CLASS (s) != LOC_CONST_BYTES)
9979 return 0;
9980 break;
9981 }
9982
9983 /* The default action is to return 0 because we are using
9984 the optimistic approach here: If we don't know something,
9985 then it is not a constant. */
9986 default:
9987 return 0;
9988 }
9989 }
9990
9991 return 1;
9992}
9993
c1fc2657 9994/* Watchpoint destructor. */
3a5c3e22 9995
c1fc2657 9996watchpoint::~watchpoint ()
3a5c3e22 9997{
c1fc2657
SM
9998 xfree (this->exp_string);
9999 xfree (this->exp_string_reparse);
3a5c3e22
PA
10000}
10001
348d480f
PA
10002/* Implement the "re_set" breakpoint_ops method for watchpoints. */
10003
10004static void
10005re_set_watchpoint (struct breakpoint *b)
10006{
3a5c3e22
PA
10007 struct watchpoint *w = (struct watchpoint *) b;
10008
348d480f
PA
10009 /* Watchpoint can be either on expression using entirely global
10010 variables, or it can be on local variables.
10011
10012 Watchpoints of the first kind are never auto-deleted, and even
10013 persist across program restarts. Since they can use variables
10014 from shared libraries, we need to reparse expression as libraries
10015 are loaded and unloaded.
10016
10017 Watchpoints on local variables can also change meaning as result
10018 of solib event. For example, if a watchpoint uses both a local
10019 and a global variables in expression, it's a local watchpoint,
10020 but unloading of a shared library will make the expression
10021 invalid. This is not a very common use case, but we still
10022 re-evaluate expression, to avoid surprises to the user.
10023
10024 Note that for local watchpoints, we re-evaluate it only if
10025 watchpoints frame id is still valid. If it's not, it means the
10026 watchpoint is out of scope and will be deleted soon. In fact,
10027 I'm not sure we'll ever be called in this case.
10028
10029 If a local watchpoint's frame id is still valid, then
3a5c3e22 10030 w->exp_valid_block is likewise valid, and we can safely use it.
348d480f 10031
3a5c3e22
PA
10032 Don't do anything about disabled watchpoints, since they will be
10033 reevaluated again when enabled. */
10034 update_watchpoint (w, 1 /* reparse */);
348d480f
PA
10035}
10036
77b06cd7
TJB
10037/* Implement the "insert" breakpoint_ops method for hardware watchpoints. */
10038
10039static int
10040insert_watchpoint (struct bp_location *bl)
10041{
3a5c3e22
PA
10042 struct watchpoint *w = (struct watchpoint *) bl->owner;
10043 int length = w->exact ? 1 : bl->length;
e09342b5
TJB
10044
10045 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
4d01a485 10046 w->cond_exp.get ());
77b06cd7
TJB
10047}
10048
10049/* Implement the "remove" breakpoint_ops method for hardware watchpoints. */
10050
10051static int
73971819 10052remove_watchpoint (struct bp_location *bl, enum remove_bp_reason reason)
77b06cd7 10053{
3a5c3e22
PA
10054 struct watchpoint *w = (struct watchpoint *) bl->owner;
10055 int length = w->exact ? 1 : bl->length;
e09342b5
TJB
10056
10057 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
4d01a485 10058 w->cond_exp.get ());
e09342b5
TJB
10059}
10060
e09342b5 10061static int
348d480f 10062breakpoint_hit_watchpoint (const struct bp_location *bl,
bd522513 10063 const address_space *aspace, CORE_ADDR bp_addr,
09ac7c10 10064 const struct target_waitstatus *ws)
e09342b5 10065{
348d480f 10066 struct breakpoint *b = bl->owner;
3a5c3e22 10067 struct watchpoint *w = (struct watchpoint *) b;
77b06cd7 10068
348d480f
PA
10069 /* Continuable hardware watchpoints are treated as non-existent if the
10070 reason we stopped wasn't a hardware watchpoint (we didn't stop on
10071 some data address). Otherwise gdb won't stop on a break instruction
10072 in the code (not from a breakpoint) when a hardware watchpoint has
10073 been defined. Also skip watchpoints which we know did not trigger
10074 (did not match the data address). */
10075 if (is_hardware_watchpoint (b)
3a5c3e22 10076 && w->watchpoint_triggered == watch_triggered_no)
348d480f 10077 return 0;
9c06b0b4 10078
348d480f 10079 return 1;
9c06b0b4
TJB
10080}
10081
348d480f
PA
10082static void
10083check_status_watchpoint (bpstat bs)
9c06b0b4 10084{
348d480f 10085 gdb_assert (is_watchpoint (bs->breakpoint_at));
9c06b0b4 10086
348d480f 10087 bpstat_check_watchpoint (bs);
9c06b0b4
TJB
10088}
10089
10090/* Implement the "resources_needed" breakpoint_ops method for
348d480f 10091 hardware watchpoints. */
9c06b0b4
TJB
10092
10093static int
348d480f 10094resources_needed_watchpoint (const struct bp_location *bl)
9c06b0b4 10095{
3a5c3e22
PA
10096 struct watchpoint *w = (struct watchpoint *) bl->owner;
10097 int length = w->exact? 1 : bl->length;
348d480f
PA
10098
10099 return target_region_ok_for_hw_watchpoint (bl->address, length);
9c06b0b4
TJB
10100}
10101
10102/* Implement the "works_in_software_mode" breakpoint_ops method for
348d480f 10103 hardware watchpoints. */
9c06b0b4
TJB
10104
10105static int
348d480f 10106works_in_software_mode_watchpoint (const struct breakpoint *b)
9c06b0b4 10107{
efa80663
PA
10108 /* Read and access watchpoints only work with hardware support. */
10109 return b->type == bp_watchpoint || b->type == bp_hardware_watchpoint;
9c06b0b4
TJB
10110}
10111
9c06b0b4 10112static enum print_stop_action
348d480f 10113print_it_watchpoint (bpstat bs)
9c06b0b4 10114{
348d480f 10115 struct breakpoint *b;
348d480f 10116 enum print_stop_action result;
3a5c3e22 10117 struct watchpoint *w;
79a45e25 10118 struct ui_out *uiout = current_uiout;
348d480f
PA
10119
10120 gdb_assert (bs->bp_location_at != NULL);
10121
348d480f 10122 b = bs->breakpoint_at;
3a5c3e22 10123 w = (struct watchpoint *) b;
348d480f 10124
f303dbd6
PA
10125 annotate_watchpoint (b->number);
10126 maybe_print_thread_hit_breakpoint (uiout);
10127
d7e74731
PA
10128 string_file stb;
10129
76f9c9cf 10130 gdb::optional<ui_out_emit_tuple> tuple_emitter;
9c06b0b4
TJB
10131 switch (b->type)
10132 {
348d480f 10133 case bp_watchpoint:
9c06b0b4 10134 case bp_hardware_watchpoint:
112e8700
SM
10135 if (uiout->is_mi_like_p ())
10136 uiout->field_string
10137 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
348d480f 10138 mention (b);
76f9c9cf 10139 tuple_emitter.emplace (uiout, "value");
112e8700 10140 uiout->text ("\nOld value = ");
850645cf 10141 watchpoint_value_print (bs->old_val.get (), &stb);
112e8700
SM
10142 uiout->field_stream ("old", stb);
10143 uiout->text ("\nNew value = ");
850645cf 10144 watchpoint_value_print (w->val.get (), &stb);
112e8700
SM
10145 uiout->field_stream ("new", stb);
10146 uiout->text ("\n");
348d480f
PA
10147 /* More than one watchpoint may have been triggered. */
10148 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10149 break;
10150
10151 case bp_read_watchpoint:
112e8700
SM
10152 if (uiout->is_mi_like_p ())
10153 uiout->field_string
10154 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
348d480f 10155 mention (b);
76f9c9cf 10156 tuple_emitter.emplace (uiout, "value");
112e8700 10157 uiout->text ("\nValue = ");
850645cf 10158 watchpoint_value_print (w->val.get (), &stb);
112e8700
SM
10159 uiout->field_stream ("value", stb);
10160 uiout->text ("\n");
348d480f 10161 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10162 break;
10163
10164 case bp_access_watchpoint:
348d480f
PA
10165 if (bs->old_val != NULL)
10166 {
112e8700
SM
10167 if (uiout->is_mi_like_p ())
10168 uiout->field_string
10169 ("reason",
348d480f
PA
10170 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10171 mention (b);
76f9c9cf 10172 tuple_emitter.emplace (uiout, "value");
112e8700 10173 uiout->text ("\nOld value = ");
850645cf 10174 watchpoint_value_print (bs->old_val.get (), &stb);
112e8700
SM
10175 uiout->field_stream ("old", stb);
10176 uiout->text ("\nNew value = ");
348d480f
PA
10177 }
10178 else
10179 {
10180 mention (b);
112e8700
SM
10181 if (uiout->is_mi_like_p ())
10182 uiout->field_string
10183 ("reason",
348d480f 10184 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
76f9c9cf 10185 tuple_emitter.emplace (uiout, "value");
112e8700 10186 uiout->text ("\nValue = ");
348d480f 10187 }
850645cf 10188 watchpoint_value_print (w->val.get (), &stb);
112e8700
SM
10189 uiout->field_stream ("new", stb);
10190 uiout->text ("\n");
348d480f 10191 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10192 break;
10193 default:
348d480f 10194 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10195 }
10196
348d480f
PA
10197 return result;
10198}
10199
10200/* Implement the "print_mention" breakpoint_ops method for hardware
10201 watchpoints. */
10202
10203static void
10204print_mention_watchpoint (struct breakpoint *b)
10205{
3a5c3e22 10206 struct watchpoint *w = (struct watchpoint *) b;
79a45e25 10207 struct ui_out *uiout = current_uiout;
46b9c129 10208 const char *tuple_name;
348d480f
PA
10209
10210 switch (b->type)
10211 {
10212 case bp_watchpoint:
112e8700 10213 uiout->text ("Watchpoint ");
46b9c129 10214 tuple_name = "wpt";
348d480f
PA
10215 break;
10216 case bp_hardware_watchpoint:
112e8700 10217 uiout->text ("Hardware watchpoint ");
46b9c129 10218 tuple_name = "wpt";
348d480f
PA
10219 break;
10220 case bp_read_watchpoint:
112e8700 10221 uiout->text ("Hardware read watchpoint ");
46b9c129 10222 tuple_name = "hw-rwpt";
348d480f
PA
10223 break;
10224 case bp_access_watchpoint:
112e8700 10225 uiout->text ("Hardware access (read/write) watchpoint ");
46b9c129 10226 tuple_name = "hw-awpt";
348d480f
PA
10227 break;
10228 default:
10229 internal_error (__FILE__, __LINE__,
10230 _("Invalid hardware watchpoint type."));
10231 }
10232
46b9c129 10233 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
381befee 10234 uiout->field_signed ("number", b->number);
112e8700
SM
10235 uiout->text (": ");
10236 uiout->field_string ("exp", w->exp_string);
348d480f
PA
10237}
10238
10239/* Implement the "print_recreate" breakpoint_ops method for
10240 watchpoints. */
10241
10242static void
10243print_recreate_watchpoint (struct breakpoint *b, struct ui_file *fp)
10244{
3a5c3e22
PA
10245 struct watchpoint *w = (struct watchpoint *) b;
10246
348d480f
PA
10247 switch (b->type)
10248 {
10249 case bp_watchpoint:
10250 case bp_hardware_watchpoint:
10251 fprintf_unfiltered (fp, "watch");
10252 break;
10253 case bp_read_watchpoint:
10254 fprintf_unfiltered (fp, "rwatch");
10255 break;
10256 case bp_access_watchpoint:
10257 fprintf_unfiltered (fp, "awatch");
10258 break;
10259 default:
10260 internal_error (__FILE__, __LINE__,
10261 _("Invalid watchpoint type."));
10262 }
10263
3a5c3e22 10264 fprintf_unfiltered (fp, " %s", w->exp_string);
d9b3f62e 10265 print_recreate_thread (b, fp);
348d480f
PA
10266}
10267
427cd150
TT
10268/* Implement the "explains_signal" breakpoint_ops method for
10269 watchpoints. */
10270
47591c29 10271static int
427cd150
TT
10272explains_signal_watchpoint (struct breakpoint *b, enum gdb_signal sig)
10273{
10274 /* A software watchpoint cannot cause a signal other than
10275 GDB_SIGNAL_TRAP. */
10276 if (b->type == bp_watchpoint && sig != GDB_SIGNAL_TRAP)
47591c29 10277 return 0;
427cd150 10278
47591c29 10279 return 1;
427cd150
TT
10280}
10281
348d480f
PA
10282/* The breakpoint_ops structure to be used in hardware watchpoints. */
10283
2060206e 10284static struct breakpoint_ops watchpoint_breakpoint_ops;
348d480f
PA
10285
10286/* Implement the "insert" breakpoint_ops method for
10287 masked hardware watchpoints. */
10288
10289static int
10290insert_masked_watchpoint (struct bp_location *bl)
10291{
3a5c3e22
PA
10292 struct watchpoint *w = (struct watchpoint *) bl->owner;
10293
10294 return target_insert_mask_watchpoint (bl->address, w->hw_wp_mask,
348d480f
PA
10295 bl->watchpoint_type);
10296}
10297
10298/* Implement the "remove" breakpoint_ops method for
10299 masked hardware watchpoints. */
10300
10301static int
73971819 10302remove_masked_watchpoint (struct bp_location *bl, enum remove_bp_reason reason)
348d480f 10303{
3a5c3e22
PA
10304 struct watchpoint *w = (struct watchpoint *) bl->owner;
10305
10306 return target_remove_mask_watchpoint (bl->address, w->hw_wp_mask,
348d480f
PA
10307 bl->watchpoint_type);
10308}
10309
10310/* Implement the "resources_needed" breakpoint_ops method for
10311 masked hardware watchpoints. */
10312
10313static int
10314resources_needed_masked_watchpoint (const struct bp_location *bl)
10315{
3a5c3e22
PA
10316 struct watchpoint *w = (struct watchpoint *) bl->owner;
10317
10318 return target_masked_watch_num_registers (bl->address, w->hw_wp_mask);
348d480f
PA
10319}
10320
10321/* Implement the "works_in_software_mode" breakpoint_ops method for
10322 masked hardware watchpoints. */
10323
10324static int
10325works_in_software_mode_masked_watchpoint (const struct breakpoint *b)
10326{
10327 return 0;
10328}
10329
10330/* Implement the "print_it" breakpoint_ops method for
10331 masked hardware watchpoints. */
10332
10333static enum print_stop_action
10334print_it_masked_watchpoint (bpstat bs)
10335{
10336 struct breakpoint *b = bs->breakpoint_at;
79a45e25 10337 struct ui_out *uiout = current_uiout;
348d480f
PA
10338
10339 /* Masked watchpoints have only one location. */
10340 gdb_assert (b->loc && b->loc->next == NULL);
10341
f303dbd6
PA
10342 annotate_watchpoint (b->number);
10343 maybe_print_thread_hit_breakpoint (uiout);
10344
348d480f
PA
10345 switch (b->type)
10346 {
10347 case bp_hardware_watchpoint:
112e8700
SM
10348 if (uiout->is_mi_like_p ())
10349 uiout->field_string
10350 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
348d480f
PA
10351 break;
10352
10353 case bp_read_watchpoint:
112e8700
SM
10354 if (uiout->is_mi_like_p ())
10355 uiout->field_string
10356 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
348d480f
PA
10357 break;
10358
10359 case bp_access_watchpoint:
112e8700
SM
10360 if (uiout->is_mi_like_p ())
10361 uiout->field_string
10362 ("reason",
348d480f
PA
10363 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10364 break;
10365 default:
10366 internal_error (__FILE__, __LINE__,
10367 _("Invalid hardware watchpoint type."));
10368 }
10369
10370 mention (b);
112e8700 10371 uiout->text (_("\n\
9c06b0b4
TJB
10372Check the underlying instruction at PC for the memory\n\
10373address and value which triggered this watchpoint.\n"));
112e8700 10374 uiout->text ("\n");
9c06b0b4
TJB
10375
10376 /* More than one watchpoint may have been triggered. */
10377 return PRINT_UNKNOWN;
10378}
10379
10380/* Implement the "print_one_detail" breakpoint_ops method for
10381 masked hardware watchpoints. */
10382
10383static void
10384print_one_detail_masked_watchpoint (const struct breakpoint *b,
10385 struct ui_out *uiout)
10386{
3a5c3e22
PA
10387 struct watchpoint *w = (struct watchpoint *) b;
10388
9c06b0b4
TJB
10389 /* Masked watchpoints have only one location. */
10390 gdb_assert (b->loc && b->loc->next == NULL);
10391
112e8700
SM
10392 uiout->text ("\tmask ");
10393 uiout->field_core_addr ("mask", b->loc->gdbarch, w->hw_wp_mask);
10394 uiout->text ("\n");
9c06b0b4
TJB
10395}
10396
10397/* Implement the "print_mention" breakpoint_ops method for
10398 masked hardware watchpoints. */
10399
10400static void
10401print_mention_masked_watchpoint (struct breakpoint *b)
10402{
3a5c3e22 10403 struct watchpoint *w = (struct watchpoint *) b;
79a45e25 10404 struct ui_out *uiout = current_uiout;
46b9c129 10405 const char *tuple_name;
9c06b0b4
TJB
10406
10407 switch (b->type)
10408 {
10409 case bp_hardware_watchpoint:
112e8700 10410 uiout->text ("Masked hardware watchpoint ");
46b9c129 10411 tuple_name = "wpt";
9c06b0b4
TJB
10412 break;
10413 case bp_read_watchpoint:
112e8700 10414 uiout->text ("Masked hardware read watchpoint ");
46b9c129 10415 tuple_name = "hw-rwpt";
9c06b0b4
TJB
10416 break;
10417 case bp_access_watchpoint:
112e8700 10418 uiout->text ("Masked hardware access (read/write) watchpoint ");
46b9c129 10419 tuple_name = "hw-awpt";
9c06b0b4
TJB
10420 break;
10421 default:
10422 internal_error (__FILE__, __LINE__,
10423 _("Invalid hardware watchpoint type."));
10424 }
10425
46b9c129 10426 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
381befee 10427 uiout->field_signed ("number", b->number);
112e8700
SM
10428 uiout->text (": ");
10429 uiout->field_string ("exp", w->exp_string);
9c06b0b4
TJB
10430}
10431
10432/* Implement the "print_recreate" breakpoint_ops method for
10433 masked hardware watchpoints. */
10434
10435static void
10436print_recreate_masked_watchpoint (struct breakpoint *b, struct ui_file *fp)
10437{
3a5c3e22 10438 struct watchpoint *w = (struct watchpoint *) b;
9c06b0b4
TJB
10439 char tmp[40];
10440
10441 switch (b->type)
10442 {
10443 case bp_hardware_watchpoint:
10444 fprintf_unfiltered (fp, "watch");
10445 break;
10446 case bp_read_watchpoint:
10447 fprintf_unfiltered (fp, "rwatch");
10448 break;
10449 case bp_access_watchpoint:
10450 fprintf_unfiltered (fp, "awatch");
10451 break;
10452 default:
10453 internal_error (__FILE__, __LINE__,
10454 _("Invalid hardware watchpoint type."));
10455 }
10456
3a5c3e22
PA
10457 sprintf_vma (tmp, w->hw_wp_mask);
10458 fprintf_unfiltered (fp, " %s mask 0x%s", w->exp_string, tmp);
d9b3f62e 10459 print_recreate_thread (b, fp);
9c06b0b4
TJB
10460}
10461
10462/* The breakpoint_ops structure to be used in masked hardware watchpoints. */
10463
2060206e 10464static struct breakpoint_ops masked_watchpoint_breakpoint_ops;
9c06b0b4
TJB
10465
10466/* Tell whether the given watchpoint is a masked hardware watchpoint. */
10467
f2478a7e 10468static bool
9c06b0b4
TJB
10469is_masked_watchpoint (const struct breakpoint *b)
10470{
10471 return b->ops == &masked_watchpoint_breakpoint_ops;
10472}
10473
53a5351d
JM
10474/* accessflag: hw_write: watch write,
10475 hw_read: watch read,
10476 hw_access: watch access (read or write) */
c906108c 10477static void
bbc13ae3 10478watch_command_1 (const char *arg, int accessflag, int from_tty,
84f4c1fe 10479 int just_location, int internal)
c906108c 10480{
c1fc2657 10481 struct breakpoint *scope_breakpoint = NULL;
270140bd 10482 const struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
b926417a 10483 struct value *result;
bb9d5f81 10484 int saved_bitpos = 0, saved_bitsize = 0;
bbc13ae3
KS
10485 const char *exp_start = NULL;
10486 const char *exp_end = NULL;
10487 const char *tok, *end_tok;
9c06b0b4 10488 int toklen = -1;
bbc13ae3
KS
10489 const char *cond_start = NULL;
10490 const char *cond_end = NULL;
c906108c 10491 enum bptype bp_type;
37e4754d 10492 int thread = -1;
0cf6dd15 10493 int pc = 0;
9c06b0b4
TJB
10494 /* Flag to indicate whether we are going to use masks for
10495 the hardware watchpoint. */
10496 int use_mask = 0;
10497 CORE_ADDR mask = 0;
c906108c 10498
37e4754d
LM
10499 /* Make sure that we actually have parameters to parse. */
10500 if (arg != NULL && arg[0] != '\0')
10501 {
bbc13ae3
KS
10502 const char *value_start;
10503
10504 exp_end = arg + strlen (arg);
37e4754d 10505
9c06b0b4
TJB
10506 /* Look for "parameter value" pairs at the end
10507 of the arguments string. */
bbc13ae3 10508 for (tok = exp_end - 1; tok > arg; tok--)
9c06b0b4
TJB
10509 {
10510 /* Skip whitespace at the end of the argument list. */
10511 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10512 tok--;
10513
10514 /* Find the beginning of the last token.
10515 This is the value of the parameter. */
10516 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10517 tok--;
10518 value_start = tok + 1;
10519
10520 /* Skip whitespace. */
10521 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10522 tok--;
10523
10524 end_tok = tok;
10525
10526 /* Find the beginning of the second to last token.
10527 This is the parameter itself. */
10528 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10529 tok--;
10530 tok++;
10531 toklen = end_tok - tok + 1;
10532
61012eef 10533 if (toklen == 6 && startswith (tok, "thread"))
9c06b0b4 10534 {
5d5658a1 10535 struct thread_info *thr;
9c06b0b4
TJB
10536 /* At this point we've found a "thread" token, which means
10537 the user is trying to set a watchpoint that triggers
10538 only in a specific thread. */
5d5658a1 10539 const char *endp;
37e4754d 10540
9c06b0b4
TJB
10541 if (thread != -1)
10542 error(_("You can specify only one thread."));
37e4754d 10543
9c06b0b4 10544 /* Extract the thread ID from the next token. */
5d5658a1 10545 thr = parse_thread_id (value_start, &endp);
37e4754d 10546
5d5658a1 10547 /* Check if the user provided a valid thread ID. */
9c06b0b4 10548 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
5d5658a1 10549 invalid_thread_id_error (value_start);
9c06b0b4 10550
5d5658a1 10551 thread = thr->global_num;
9c06b0b4 10552 }
61012eef 10553 else if (toklen == 4 && startswith (tok, "mask"))
9c06b0b4
TJB
10554 {
10555 /* We've found a "mask" token, which means the user wants to
10556 create a hardware watchpoint that is going to have the mask
10557 facility. */
10558 struct value *mask_value, *mark;
37e4754d 10559
9c06b0b4
TJB
10560 if (use_mask)
10561 error(_("You can specify only one mask."));
37e4754d 10562
9c06b0b4 10563 use_mask = just_location = 1;
37e4754d 10564
9c06b0b4
TJB
10565 mark = value_mark ();
10566 mask_value = parse_to_comma_and_eval (&value_start);
10567 mask = value_as_address (mask_value);
10568 value_free_to_mark (mark);
10569 }
10570 else
10571 /* We didn't recognize what we found. We should stop here. */
10572 break;
37e4754d 10573
9c06b0b4
TJB
10574 /* Truncate the string and get rid of the "parameter value" pair before
10575 the arguments string is parsed by the parse_exp_1 function. */
bbc13ae3 10576 exp_end = tok;
9c06b0b4 10577 }
37e4754d 10578 }
bbc13ae3
KS
10579 else
10580 exp_end = arg;
37e4754d 10581
bbc13ae3
KS
10582 /* Parse the rest of the arguments. From here on out, everything
10583 is in terms of a newly allocated string instead of the original
10584 ARG. */
81b1e71c
TT
10585 std::string expression (arg, exp_end - arg);
10586 exp_start = arg = expression.c_str ();
699bd4cf
TT
10587 innermost_block_tracker tracker;
10588 expression_up exp = parse_exp_1 (&arg, 0, 0, 0, &tracker);
c906108c 10589 exp_end = arg;
fa8a61dc
TT
10590 /* Remove trailing whitespace from the expression before saving it.
10591 This makes the eventual display of the expression string a bit
10592 prettier. */
10593 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
10594 --exp_end;
10595
65d79d4b 10596 /* Checking if the expression is not constant. */
4d01a485 10597 if (watchpoint_exp_is_const (exp.get ()))
65d79d4b
SDJ
10598 {
10599 int len;
10600
10601 len = exp_end - exp_start;
10602 while (len > 0 && isspace (exp_start[len - 1]))
10603 len--;
10604 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
10605 }
10606
699bd4cf 10607 exp_valid_block = tracker.block ();
b926417a 10608 struct value *mark = value_mark ();
850645cf
TT
10609 struct value *val_as_value = nullptr;
10610 fetch_subexp_value (exp.get (), &pc, &val_as_value, &result, NULL,
10611 just_location);
06a64a0b 10612
850645cf 10613 if (val_as_value != NULL && just_location)
bb9d5f81 10614 {
850645cf
TT
10615 saved_bitpos = value_bitpos (val_as_value);
10616 saved_bitsize = value_bitsize (val_as_value);
bb9d5f81
PP
10617 }
10618
850645cf 10619 value_ref_ptr val;
06a64a0b
TT
10620 if (just_location)
10621 {
9c06b0b4
TJB
10622 int ret;
10623
06a64a0b 10624 exp_valid_block = NULL;
850645cf 10625 val = release_value (value_addr (result));
06a64a0b 10626 value_free_to_mark (mark);
9c06b0b4
TJB
10627
10628 if (use_mask)
10629 {
850645cf 10630 ret = target_masked_watch_num_registers (value_as_address (val.get ()),
9c06b0b4
TJB
10631 mask);
10632 if (ret == -1)
10633 error (_("This target does not support masked watchpoints."));
10634 else if (ret == -2)
10635 error (_("Invalid mask or memory region."));
10636 }
06a64a0b 10637 }
850645cf
TT
10638 else if (val_as_value != NULL)
10639 val = release_value (val_as_value);
c906108c 10640
f1735a53
TT
10641 tok = skip_spaces (arg);
10642 end_tok = skip_to_space (tok);
c906108c
SS
10643
10644 toklen = end_tok - tok;
10645 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
10646 {
10647 tok = cond_start = end_tok + 1;
699bd4cf
TT
10648 innermost_block_tracker if_tracker;
10649 parse_exp_1 (&tok, 0, 0, 0, &if_tracker);
60e1c644
PA
10650
10651 /* The watchpoint expression may not be local, but the condition
10652 may still be. E.g.: `watch global if local > 0'. */
699bd4cf 10653 cond_exp_valid_block = if_tracker.block ();
60e1c644 10654
c906108c
SS
10655 cond_end = tok;
10656 }
10657 if (*tok)
8a3fe4f8 10658 error (_("Junk at end of command."));
c906108c 10659
441d7c93
PA
10660 frame_info *wp_frame = block_innermost_frame (exp_valid_block);
10661
10662 /* Save this because create_internal_breakpoint below invalidates
10663 'wp_frame'. */
10664 frame_id watchpoint_frame = get_frame_id (wp_frame);
d983da9c
DJ
10665
10666 /* If the expression is "local", then set up a "watchpoint scope"
10667 breakpoint at the point where we've left the scope of the watchpoint
10668 expression. Create the scope breakpoint before the watchpoint, so
10669 that we will encounter it first in bpstat_stop_status. */
441d7c93 10670 if (exp_valid_block != NULL && wp_frame != NULL)
d983da9c 10671 {
441d7c93
PA
10672 frame_id caller_frame_id = frame_unwind_caller_id (wp_frame);
10673
10674 if (frame_id_p (caller_frame_id))
edb3359d 10675 {
441d7c93
PA
10676 gdbarch *caller_arch = frame_unwind_caller_arch (wp_frame);
10677 CORE_ADDR caller_pc = frame_unwind_caller_pc (wp_frame);
10678
edb3359d 10679 scope_breakpoint
441d7c93 10680 = create_internal_breakpoint (caller_arch, caller_pc,
06edf0c0
PA
10681 bp_watchpoint_scope,
10682 &momentary_breakpoint_ops);
d983da9c 10683
441d7c93
PA
10684 /* create_internal_breakpoint could invalidate WP_FRAME. */
10685 wp_frame = NULL;
10686
edb3359d 10687 scope_breakpoint->enable_state = bp_enabled;
d983da9c 10688
edb3359d
DJ
10689 /* Automatically delete the breakpoint when it hits. */
10690 scope_breakpoint->disposition = disp_del;
d983da9c 10691
edb3359d 10692 /* Only break in the proper frame (help with recursion). */
441d7c93 10693 scope_breakpoint->frame_id = caller_frame_id;
d983da9c 10694
edb3359d 10695 /* Set the address at which we will stop. */
441d7c93
PA
10696 scope_breakpoint->loc->gdbarch = caller_arch;
10697 scope_breakpoint->loc->requested_address = caller_pc;
edb3359d 10698 scope_breakpoint->loc->address
a6d9a66e
UW
10699 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
10700 scope_breakpoint->loc->requested_address,
edb3359d
DJ
10701 scope_breakpoint->type);
10702 }
d983da9c
DJ
10703 }
10704
e8369a73
AB
10705 /* Now set up the breakpoint. We create all watchpoints as hardware
10706 watchpoints here even if hardware watchpoints are turned off, a call
10707 to update_watchpoint later in this function will cause the type to
10708 drop back to bp_watchpoint (software watchpoint) if required. */
10709
10710 if (accessflag == hw_read)
10711 bp_type = bp_read_watchpoint;
10712 else if (accessflag == hw_access)
10713 bp_type = bp_access_watchpoint;
10714 else
10715 bp_type = bp_hardware_watchpoint;
3a5c3e22 10716
b270e6f9 10717 std::unique_ptr<watchpoint> w (new watchpoint ());
c1fc2657 10718
348d480f 10719 if (use_mask)
b270e6f9 10720 init_raw_breakpoint_without_location (w.get (), NULL, bp_type,
3a5c3e22 10721 &masked_watchpoint_breakpoint_ops);
348d480f 10722 else
b270e6f9 10723 init_raw_breakpoint_without_location (w.get (), NULL, bp_type,
3a5c3e22 10724 &watchpoint_breakpoint_ops);
c1fc2657
SM
10725 w->thread = thread;
10726 w->disposition = disp_donttouch;
10727 w->pspace = current_program_space;
b22e99fd 10728 w->exp = std::move (exp);
3a5c3e22
PA
10729 w->exp_valid_block = exp_valid_block;
10730 w->cond_exp_valid_block = cond_exp_valid_block;
06a64a0b
TT
10731 if (just_location)
10732 {
850645cf
TT
10733 struct type *t = value_type (val.get ());
10734 CORE_ADDR addr = value_as_address (val.get ());
06a64a0b 10735
43cc5389
TT
10736 w->exp_string_reparse
10737 = current_language->la_watch_location_expression (t, addr).release ();
06a64a0b 10738
3a5c3e22 10739 w->exp_string = xstrprintf ("-location %.*s",
d63d0675 10740 (int) (exp_end - exp_start), exp_start);
06a64a0b
TT
10741 }
10742 else
3a5c3e22 10743 w->exp_string = savestring (exp_start, exp_end - exp_start);
9c06b0b4
TJB
10744
10745 if (use_mask)
10746 {
3a5c3e22 10747 w->hw_wp_mask = mask;
9c06b0b4
TJB
10748 }
10749 else
10750 {
3a5c3e22 10751 w->val = val;
bb9d5f81
PP
10752 w->val_bitpos = saved_bitpos;
10753 w->val_bitsize = saved_bitsize;
4c1d86d9 10754 w->val_valid = true;
9c06b0b4 10755 }
77b06cd7 10756
c906108c 10757 if (cond_start)
c1fc2657 10758 w->cond_string = savestring (cond_start, cond_end - cond_start);
c906108c 10759 else
c1fc2657 10760 w->cond_string = 0;
c5aa993b 10761
441d7c93 10762 if (frame_id_p (watchpoint_frame))
f6bc2008 10763 {
441d7c93 10764 w->watchpoint_frame = watchpoint_frame;
3a5c3e22 10765 w->watchpoint_thread = inferior_ptid;
f6bc2008 10766 }
c906108c 10767 else
f6bc2008 10768 {
3a5c3e22
PA
10769 w->watchpoint_frame = null_frame_id;
10770 w->watchpoint_thread = null_ptid;
f6bc2008 10771 }
c906108c 10772
d983da9c 10773 if (scope_breakpoint != NULL)
c906108c 10774 {
d983da9c
DJ
10775 /* The scope breakpoint is related to the watchpoint. We will
10776 need to act on them together. */
c1fc2657 10777 w->related_breakpoint = scope_breakpoint;
b270e6f9 10778 scope_breakpoint->related_breakpoint = w.get ();
c906108c 10779 }
d983da9c 10780
06a64a0b
TT
10781 if (!just_location)
10782 value_free_to_mark (mark);
2d134ed3 10783
b270e6f9
TT
10784 /* Finally update the new watchpoint. This creates the locations
10785 that should be inserted. */
10786 update_watchpoint (w.get (), 1);
a9634178 10787
b270e6f9 10788 install_breakpoint (internal, std::move (w), 1);
c906108c
SS
10789}
10790
e09342b5 10791/* Return count of debug registers needed to watch the given expression.
e09342b5 10792 If the watchpoint cannot be handled in hardware return zero. */
c906108c 10793
c906108c 10794static int
a6535de1 10795can_use_hardware_watchpoint (const std::vector<value_ref_ptr> &vals)
c906108c
SS
10796{
10797 int found_memory_cnt = 0;
10798
10799 /* Did the user specifically forbid us to use hardware watchpoints? */
c5aa993b 10800 if (!can_use_hw_watchpoints)
c906108c 10801 return 0;
c5aa993b 10802
a6535de1
TT
10803 gdb_assert (!vals.empty ());
10804 struct value *head = vals[0].get ();
10805
5c44784c
JM
10806 /* Make sure that the value of the expression depends only upon
10807 memory contents, and values computed from them within GDB. If we
10808 find any register references or function calls, we can't use a
10809 hardware watchpoint.
10810
10811 The idea here is that evaluating an expression generates a series
10812 of values, one holding the value of every subexpression. (The
10813 expression a*b+c has five subexpressions: a, b, a*b, c, and
10814 a*b+c.) GDB's values hold almost enough information to establish
10815 the criteria given above --- they identify memory lvalues,
10816 register lvalues, computed values, etcetera. So we can evaluate
10817 the expression, and then scan the chain of values that leaves
10818 behind to decide whether we can detect any possible change to the
10819 expression's final value using only hardware watchpoints.
10820
10821 However, I don't think that the values returned by inferior
10822 function calls are special in any way. So this function may not
10823 notice that an expression involving an inferior function call
10824 can't be watched with hardware watchpoints. FIXME. */
a6535de1 10825 for (const value_ref_ptr &iter : vals)
c906108c 10826 {
a6535de1
TT
10827 struct value *v = iter.get ();
10828
5c44784c 10829 if (VALUE_LVAL (v) == lval_memory)
c906108c 10830 {
8464be76
DJ
10831 if (v != head && value_lazy (v))
10832 /* A lazy memory lvalue in the chain is one that GDB never
10833 needed to fetch; we either just used its address (e.g.,
10834 `a' in `a.b') or we never needed it at all (e.g., `a'
10835 in `a,b'). This doesn't apply to HEAD; if that is
10836 lazy then it was not readable, but watch it anyway. */
5c44784c 10837 ;
53a5351d 10838 else
5c44784c
JM
10839 {
10840 /* Ahh, memory we actually used! Check if we can cover
10841 it with hardware watchpoints. */
df407dfe 10842 struct type *vtype = check_typedef (value_type (v));
2e70b7b9
MS
10843
10844 /* We only watch structs and arrays if user asked for it
10845 explicitly, never if they just happen to appear in a
10846 middle of some value chain. */
10847 if (v == head
10848 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
10849 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
10850 {
42ae5230 10851 CORE_ADDR vaddr = value_address (v);
e09342b5
TJB
10852 int len;
10853 int num_regs;
10854
a9634178 10855 len = (target_exact_watchpoints
e09342b5
TJB
10856 && is_scalar_type_recursive (vtype))?
10857 1 : TYPE_LENGTH (value_type (v));
2e70b7b9 10858
e09342b5
TJB
10859 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
10860 if (!num_regs)
2e70b7b9
MS
10861 return 0;
10862 else
e09342b5 10863 found_memory_cnt += num_regs;
2e70b7b9 10864 }
5c44784c 10865 }
c5aa993b 10866 }
5086187c
AC
10867 else if (VALUE_LVAL (v) != not_lval
10868 && deprecated_value_modifiable (v) == 0)
38b6c3b3 10869 return 0; /* These are values from the history (e.g., $1). */
5086187c 10870 else if (VALUE_LVAL (v) == lval_register)
38b6c3b3 10871 return 0; /* Cannot watch a register with a HW watchpoint. */
c906108c
SS
10872 }
10873
10874 /* The expression itself looks suitable for using a hardware
10875 watchpoint, but give the target machine a chance to reject it. */
10876 return found_memory_cnt;
10877}
10878
8b93c638 10879void
f2fc3015 10880watch_command_wrapper (const char *arg, int from_tty, int internal)
8b93c638 10881{
84f4c1fe 10882 watch_command_1 (arg, hw_write, from_tty, 0, internal);
06a64a0b
TT
10883}
10884
06a64a0b
TT
10885/* A helper function that looks for the "-location" argument and then
10886 calls watch_command_1. */
10887
10888static void
0b39b52e 10889watch_maybe_just_location (const char *arg, int accessflag, int from_tty)
06a64a0b
TT
10890{
10891 int just_location = 0;
10892
10893 if (arg
10894 && (check_for_argument (&arg, "-location", sizeof ("-location") - 1)
10895 || check_for_argument (&arg, "-l", sizeof ("-l") - 1)))
cbba3ecd 10896 just_location = 1;
06a64a0b 10897
84f4c1fe 10898 watch_command_1 (arg, accessflag, from_tty, just_location, 0);
8b93c638 10899}
8926118c 10900
c5aa993b 10901static void
0b39b52e 10902watch_command (const char *arg, int from_tty)
c906108c 10903{
06a64a0b 10904 watch_maybe_just_location (arg, hw_write, from_tty);
c906108c
SS
10905}
10906
8b93c638 10907void
f2fc3015 10908rwatch_command_wrapper (const char *arg, int from_tty, int internal)
8b93c638 10909{
84f4c1fe 10910 watch_command_1 (arg, hw_read, from_tty, 0, internal);
8b93c638 10911}
8926118c 10912
c5aa993b 10913static void
0b39b52e 10914rwatch_command (const char *arg, int from_tty)
c906108c 10915{
06a64a0b 10916 watch_maybe_just_location (arg, hw_read, from_tty);
c906108c
SS
10917}
10918
8b93c638 10919void
f2fc3015 10920awatch_command_wrapper (const char *arg, int from_tty, int internal)
8b93c638 10921{
84f4c1fe 10922 watch_command_1 (arg, hw_access, from_tty, 0, internal);
8b93c638 10923}
8926118c 10924
c5aa993b 10925static void
0b39b52e 10926awatch_command (const char *arg, int from_tty)
c906108c 10927{
06a64a0b 10928 watch_maybe_just_location (arg, hw_access, from_tty);
c906108c 10929}
c906108c 10930\f
c5aa993b 10931
cfc31633
PA
10932/* Data for the FSM that manages the until(location)/advance commands
10933 in infcmd.c. Here because it uses the mechanisms of
10934 breakpoints. */
c906108c 10935
46e3ed7f 10936struct until_break_fsm : public thread_fsm
bfec99b2 10937{
46e3ed7f 10938 /* The thread that was current when the command was executed. */
cfc31633
PA
10939 int thread;
10940
10941 /* The breakpoint set at the destination location. */
46e3ed7f 10942 breakpoint_up location_breakpoint;
cfc31633
PA
10943
10944 /* Breakpoint set at the return address in the caller frame. May be
10945 NULL. */
46e3ed7f 10946 breakpoint_up caller_breakpoint;
cfc31633 10947
46e3ed7f
TT
10948 until_break_fsm (struct interp *cmd_interp, int thread,
10949 breakpoint_up &&location_breakpoint,
10950 breakpoint_up &&caller_breakpoint)
10951 : thread_fsm (cmd_interp),
10952 thread (thread),
10953 location_breakpoint (std::move (location_breakpoint)),
10954 caller_breakpoint (std::move (caller_breakpoint))
10955 {
10956 }
cfc31633 10957
46e3ed7f
TT
10958 void clean_up (struct thread_info *thread) override;
10959 bool should_stop (struct thread_info *thread) override;
10960 enum async_reply_reason do_async_reply_reason () override;
cfc31633
PA
10961};
10962
cfc31633
PA
10963/* Implementation of the 'should_stop' FSM method for the
10964 until(location)/advance commands. */
10965
46e3ed7f
TT
10966bool
10967until_break_fsm::should_stop (struct thread_info *tp)
cfc31633 10968{
cfc31633 10969 if (bpstat_find_breakpoint (tp->control.stop_bpstat,
46e3ed7f
TT
10970 location_breakpoint.get ()) != NULL
10971 || (caller_breakpoint != NULL
cfc31633 10972 && bpstat_find_breakpoint (tp->control.stop_bpstat,
46e3ed7f
TT
10973 caller_breakpoint.get ()) != NULL))
10974 set_finished ();
cfc31633 10975
46e3ed7f 10976 return true;
cfc31633
PA
10977}
10978
10979/* Implementation of the 'clean_up' FSM method for the
10980 until(location)/advance commands. */
10981
46e3ed7f
TT
10982void
10983until_break_fsm::clean_up (struct thread_info *)
43ff13b4 10984{
cfc31633 10985 /* Clean up our temporary breakpoints. */
46e3ed7f
TT
10986 location_breakpoint.reset ();
10987 caller_breakpoint.reset ();
10988 delete_longjmp_breakpoint (thread);
cfc31633
PA
10989}
10990
10991/* Implementation of the 'async_reply_reason' FSM method for the
10992 until(location)/advance commands. */
10993
46e3ed7f
TT
10994enum async_reply_reason
10995until_break_fsm::do_async_reply_reason ()
cfc31633
PA
10996{
10997 return EXEC_ASYNC_LOCATION_REACHED;
43ff13b4
JM
10998}
10999
c906108c 11000void
f2fc3015 11001until_break_command (const char *arg, int from_tty, int anywhere)
c906108c 11002{
8556afb4
PA
11003 struct frame_info *frame;
11004 struct gdbarch *frame_gdbarch;
11005 struct frame_id stack_frame_id;
11006 struct frame_id caller_frame_id;
186c406b
TT
11007 int thread;
11008 struct thread_info *tp;
c906108c 11009
70509625 11010 clear_proceed_status (0);
c906108c
SS
11011
11012 /* Set a breakpoint where the user wants it and at return from
4a64f543 11013 this function. */
c5aa993b 11014
ffc2605c 11015 event_location_up location = string_to_event_location (&arg, current_language);
f00aae0f 11016
6c5b2ebe
PA
11017 std::vector<symtab_and_line> sals
11018 = (last_displayed_sal_is_valid ()
11019 ? decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
11020 get_last_displayed_symtab (),
11021 get_last_displayed_line ())
11022 : decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE,
cafb3438 11023 NULL, NULL, 0));
c5aa993b 11024
6c5b2ebe 11025 if (sals.size () != 1)
8a3fe4f8 11026 error (_("Couldn't get information on specified line."));
c5aa993b 11027
6c5b2ebe 11028 symtab_and_line &sal = sals[0];
c5aa993b 11029
c906108c 11030 if (*arg)
8a3fe4f8 11031 error (_("Junk at end of arguments."));
c5aa993b 11032
c906108c 11033 resolve_sal_pc (&sal);
c5aa993b 11034
186c406b 11035 tp = inferior_thread ();
5d5658a1 11036 thread = tp->global_num;
186c406b 11037
8556afb4
PA
11038 /* Note linespec handling above invalidates the frame chain.
11039 Installing a breakpoint also invalidates the frame chain (as it
11040 may need to switch threads), so do any frame handling before
11041 that. */
11042
11043 frame = get_selected_frame (NULL);
11044 frame_gdbarch = get_frame_arch (frame);
11045 stack_frame_id = get_stack_frame_id (frame);
11046 caller_frame_id = frame_unwind_caller_id (frame);
883bc8d1 11047
ae66c1fc
EZ
11048 /* Keep within the current frame, or in frames called by the current
11049 one. */
edb3359d 11050
454dafbd 11051 breakpoint_up caller_breakpoint;
5419bdae
TT
11052
11053 gdb::optional<delete_longjmp_breakpoint_cleanup> lj_deleter;
11054
883bc8d1 11055 if (frame_id_p (caller_frame_id))
c906108c 11056 {
883bc8d1 11057 struct symtab_and_line sal2;
cfc31633 11058 struct gdbarch *caller_gdbarch;
883bc8d1
PA
11059
11060 sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
11061 sal2.pc = frame_unwind_caller_pc (frame);
cfc31633
PA
11062 caller_gdbarch = frame_unwind_caller_arch (frame);
11063 caller_breakpoint = set_momentary_breakpoint (caller_gdbarch,
11064 sal2,
11065 caller_frame_id,
11066 bp_until);
186c406b 11067
883bc8d1 11068 set_longjmp_breakpoint (tp, caller_frame_id);
5419bdae 11069 lj_deleter.emplace (thread);
c906108c 11070 }
c5aa993b 11071
c70a6932
JK
11072 /* set_momentary_breakpoint could invalidate FRAME. */
11073 frame = NULL;
11074
454dafbd 11075 breakpoint_up location_breakpoint;
883bc8d1
PA
11076 if (anywhere)
11077 /* If the user told us to continue until a specified location,
11078 we don't specify a frame at which we need to stop. */
cfc31633
PA
11079 location_breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11080 null_frame_id, bp_until);
883bc8d1
PA
11081 else
11082 /* Otherwise, specify the selected frame, because we want to stop
11083 only at the very same frame. */
cfc31633
PA
11084 location_breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11085 stack_frame_id, bp_until);
883bc8d1 11086
46e3ed7f
TT
11087 tp->thread_fsm = new until_break_fsm (command_interp (), tp->global_num,
11088 std::move (location_breakpoint),
11089 std::move (caller_breakpoint));
f107f563 11090
5419bdae
TT
11091 if (lj_deleter)
11092 lj_deleter->release ();
f107f563 11093
cfc31633 11094 proceed (-1, GDB_SIGNAL_DEFAULT);
c906108c 11095}
ae66c1fc 11096
c906108c
SS
11097/* This function attempts to parse an optional "if <cond>" clause
11098 from the arg string. If one is not found, it returns NULL.
c5aa993b 11099
c906108c
SS
11100 Else, it returns a pointer to the condition string. (It does not
11101 attempt to evaluate the string against a particular block.) And,
11102 it updates arg to point to the first character following the parsed
4a64f543 11103 if clause in the arg string. */
53a5351d 11104
63160a43
PA
11105const char *
11106ep_parse_optional_if_clause (const char **arg)
c906108c 11107{
63160a43 11108 const char *cond_string;
c5aa993b
JM
11109
11110 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
c906108c 11111 return NULL;
c5aa993b 11112
4a64f543 11113 /* Skip the "if" keyword. */
c906108c 11114 (*arg) += 2;
c5aa993b 11115
c906108c 11116 /* Skip any extra leading whitespace, and record the start of the
4a64f543 11117 condition string. */
f1735a53 11118 *arg = skip_spaces (*arg);
c906108c 11119 cond_string = *arg;
c5aa993b 11120
4a64f543
MS
11121 /* Assume that the condition occupies the remainder of the arg
11122 string. */
c906108c 11123 (*arg) += strlen (cond_string);
c5aa993b 11124
c906108c
SS
11125 return cond_string;
11126}
c5aa993b 11127
c906108c
SS
11128/* Commands to deal with catching events, such as signals, exceptions,
11129 process start/exit, etc. */
c5aa993b
JM
11130
11131typedef enum
11132{
44feb3ce
TT
11133 catch_fork_temporary, catch_vfork_temporary,
11134 catch_fork_permanent, catch_vfork_permanent
c5aa993b
JM
11135}
11136catch_fork_kind;
11137
c906108c 11138static void
eb4c3f4a 11139catch_fork_command_1 (const char *arg, int from_tty,
cc59ec59 11140 struct cmd_list_element *command)
c906108c 11141{
a6d9a66e 11142 struct gdbarch *gdbarch = get_current_arch ();
63160a43 11143 const char *cond_string = NULL;
44feb3ce
TT
11144 catch_fork_kind fork_kind;
11145 int tempflag;
11146
11147 fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
11148 tempflag = (fork_kind == catch_fork_temporary
11149 || fork_kind == catch_vfork_temporary);
c5aa993b 11150
44feb3ce
TT
11151 if (!arg)
11152 arg = "";
f1735a53 11153 arg = skip_spaces (arg);
c5aa993b 11154
c906108c 11155 /* The allowed syntax is:
c5aa993b
JM
11156 catch [v]fork
11157 catch [v]fork if <cond>
11158
4a64f543 11159 First, check if there's an if clause. */
c906108c 11160 cond_string = ep_parse_optional_if_clause (&arg);
c5aa993b 11161
c906108c 11162 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 11163 error (_("Junk at end of arguments."));
c5aa993b 11164
c906108c 11165 /* If this target supports it, create a fork or vfork catchpoint
4a64f543 11166 and enable reporting of such events. */
c5aa993b
JM
11167 switch (fork_kind)
11168 {
44feb3ce
TT
11169 case catch_fork_temporary:
11170 case catch_fork_permanent:
a6d9a66e 11171 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
ce78b96d 11172 &catch_fork_breakpoint_ops);
c906108c 11173 break;
44feb3ce
TT
11174 case catch_vfork_temporary:
11175 case catch_vfork_permanent:
a6d9a66e 11176 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
ce78b96d 11177 &catch_vfork_breakpoint_ops);
c906108c 11178 break;
c5aa993b 11179 default:
8a3fe4f8 11180 error (_("unsupported or unknown fork kind; cannot catch it"));
c906108c 11181 break;
c5aa993b 11182 }
c906108c
SS
11183}
11184
11185static void
eb4c3f4a 11186catch_exec_command_1 (const char *arg, int from_tty,
cc59ec59 11187 struct cmd_list_element *command)
c906108c 11188{
a6d9a66e 11189 struct gdbarch *gdbarch = get_current_arch ();
44feb3ce 11190 int tempflag;
63160a43 11191 const char *cond_string = NULL;
c906108c 11192
44feb3ce
TT
11193 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
11194
11195 if (!arg)
11196 arg = "";
f1735a53 11197 arg = skip_spaces (arg);
c906108c
SS
11198
11199 /* The allowed syntax is:
c5aa993b
JM
11200 catch exec
11201 catch exec if <cond>
c906108c 11202
4a64f543 11203 First, check if there's an if clause. */
c906108c
SS
11204 cond_string = ep_parse_optional_if_clause (&arg);
11205
11206 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 11207 error (_("Junk at end of arguments."));
c906108c 11208
b270e6f9
TT
11209 std::unique_ptr<exec_catchpoint> c (new exec_catchpoint ());
11210 init_catchpoint (c.get (), gdbarch, tempflag, cond_string,
b4d90040
PA
11211 &catch_exec_breakpoint_ops);
11212 c->exec_pathname = NULL;
11213
b270e6f9 11214 install_breakpoint (0, std::move (c), 1);
c906108c 11215}
c5aa993b 11216
9ac4176b 11217void
28010a5d
PA
11218init_ada_exception_breakpoint (struct breakpoint *b,
11219 struct gdbarch *gdbarch,
11220 struct symtab_and_line sal,
f2fc3015 11221 const char *addr_string,
c0a91b2b 11222 const struct breakpoint_ops *ops,
28010a5d 11223 int tempflag,
349774ef 11224 int enabled,
28010a5d 11225 int from_tty)
f7f9143b 11226{
f7f9143b
JB
11227 if (from_tty)
11228 {
5af949e3
UW
11229 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
11230 if (!loc_gdbarch)
11231 loc_gdbarch = gdbarch;
11232
6c95b8df
PA
11233 describe_other_breakpoints (loc_gdbarch,
11234 sal.pspace, sal.pc, sal.section, -1);
f7f9143b
JB
11235 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
11236 version for exception catchpoints, because two catchpoints
11237 used for different exception names will use the same address.
11238 In this case, a "breakpoint ... also set at..." warning is
4a64f543 11239 unproductive. Besides, the warning phrasing is also a bit
e5dd4106 11240 inappropriate, we should use the word catchpoint, and tell
f7f9143b
JB
11241 the user what type of catchpoint it is. The above is good
11242 enough for now, though. */
11243 }
11244
f06f1252 11245 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
f7f9143b 11246
349774ef 11247 b->enable_state = enabled ? bp_enabled : bp_disabled;
f7f9143b 11248 b->disposition = tempflag ? disp_del : disp_donttouch;
d28cd78a
TT
11249 b->location = string_to_event_location (&addr_string,
11250 language_def (language_ada));
f7f9143b 11251 b->language = language_ada;
f7f9143b
JB
11252}
11253
c906108c 11254static void
981a3fb3 11255catch_command (const char *arg, int from_tty)
c906108c 11256{
44feb3ce 11257 error (_("Catch requires an event name."));
c906108c
SS
11258}
11259\f
11260
11261static void
981a3fb3 11262tcatch_command (const char *arg, int from_tty)
c906108c 11263{
44feb3ce 11264 error (_("Catch requires an event name."));
c906108c
SS
11265}
11266
81b1e71c 11267/* Compare two breakpoints and return a strcmp-like result. */
8a2c437b
TT
11268
11269static int
81b1e71c 11270compare_breakpoints (const breakpoint *a, const breakpoint *b)
8a2c437b 11271{
81b1e71c
TT
11272 uintptr_t ua = (uintptr_t) a;
11273 uintptr_t ub = (uintptr_t) b;
8a2c437b 11274
81b1e71c 11275 if (a->number < b->number)
8a2c437b 11276 return -1;
81b1e71c 11277 else if (a->number > b->number)
8a2c437b
TT
11278 return 1;
11279
11280 /* Now sort by address, in case we see, e..g, two breakpoints with
11281 the number 0. */
11282 if (ua < ub)
11283 return -1;
94b0e70d 11284 return ua > ub ? 1 : 0;
8a2c437b
TT
11285}
11286
80f8a6eb 11287/* Delete breakpoints by address or line. */
c906108c
SS
11288
11289static void
0b39b52e 11290clear_command (const char *arg, int from_tty)
c906108c 11291{
81b1e71c 11292 struct breakpoint *b;
c906108c 11293 int default_match;
c906108c 11294
6c5b2ebe
PA
11295 std::vector<symtab_and_line> decoded_sals;
11296 symtab_and_line last_sal;
11297 gdb::array_view<symtab_and_line> sals;
c906108c
SS
11298 if (arg)
11299 {
6c5b2ebe
PA
11300 decoded_sals
11301 = decode_line_with_current_source (arg,
11302 (DECODE_LINE_FUNFIRSTLINE
11303 | DECODE_LINE_LIST_MODE));
c906108c 11304 default_match = 0;
6c5b2ebe 11305 sals = decoded_sals;
c906108c
SS
11306 }
11307 else
11308 {
1bfeeb0f
JL
11309 /* Set sal's line, symtab, pc, and pspace to the values
11310 corresponding to the last call to print_frame_info. If the
11311 codepoint is not valid, this will set all the fields to 0. */
51abb421 11312 last_sal = get_last_displayed_sal ();
6c5b2ebe 11313 if (last_sal.symtab == 0)
8a3fe4f8 11314 error (_("No source file specified."));
c906108c 11315
c906108c 11316 default_match = 1;
6c5b2ebe 11317 sals = last_sal;
c906108c
SS
11318 }
11319
4a64f543
MS
11320 /* We don't call resolve_sal_pc here. That's not as bad as it
11321 seems, because all existing breakpoints typically have both
11322 file/line and pc set. So, if clear is given file/line, we can
11323 match this to existing breakpoint without obtaining pc at all.
ed0616c6
VP
11324
11325 We only support clearing given the address explicitly
11326 present in breakpoint table. Say, we've set breakpoint
4a64f543 11327 at file:line. There were several PC values for that file:line,
ed0616c6 11328 due to optimization, all in one block.
4a64f543
MS
11329
11330 We've picked one PC value. If "clear" is issued with another
ed0616c6
VP
11331 PC corresponding to the same file:line, the breakpoint won't
11332 be cleared. We probably can still clear the breakpoint, but
11333 since the other PC value is never presented to user, user
11334 can only find it by guessing, and it does not seem important
11335 to support that. */
11336
4a64f543
MS
11337 /* For each line spec given, delete bps which correspond to it. Do
11338 it in two passes, solely to preserve the current behavior that
11339 from_tty is forced true if we delete more than one
11340 breakpoint. */
c906108c 11341
81b1e71c 11342 std::vector<struct breakpoint *> found;
6c5b2ebe 11343 for (const auto &sal : sals)
c906108c 11344 {
05cba821
JK
11345 const char *sal_fullname;
11346
c906108c 11347 /* If exact pc given, clear bpts at that pc.
c5aa993b
JM
11348 If line given (pc == 0), clear all bpts on specified line.
11349 If defaulting, clear all bpts on default line
c906108c 11350 or at default pc.
c5aa993b
JM
11351
11352 defaulting sal.pc != 0 tests to do
11353
11354 0 1 pc
11355 1 1 pc _and_ line
11356 0 0 line
11357 1 0 <can't happen> */
c906108c 11358
05cba821
JK
11359 sal_fullname = (sal.symtab == NULL
11360 ? NULL : symtab_to_fullname (sal.symtab));
c906108c 11361
4a64f543 11362 /* Find all matching breakpoints and add them to 'found'. */
d6e956e5 11363 ALL_BREAKPOINTS (b)
c5aa993b 11364 {
0d381245 11365 int match = 0;
4a64f543 11366 /* Are we going to delete b? */
cc60f2e3 11367 if (b->type != bp_none && !is_watchpoint (b))
0d381245
VP
11368 {
11369 struct bp_location *loc = b->loc;
11370 for (; loc; loc = loc->next)
11371 {
f8eba3c6
TT
11372 /* If the user specified file:line, don't allow a PC
11373 match. This matches historical gdb behavior. */
11374 int pc_match = (!sal.explicit_line
11375 && sal.pc
11376 && (loc->pspace == sal.pspace)
11377 && (loc->address == sal.pc)
11378 && (!section_is_overlay (loc->section)
11379 || loc->section == sal.section));
4aac40c8
TT
11380 int line_match = 0;
11381
11382 if ((default_match || sal.explicit_line)
2f202fde 11383 && loc->symtab != NULL
05cba821 11384 && sal_fullname != NULL
4aac40c8 11385 && sal.pspace == loc->pspace
05cba821
JK
11386 && loc->line_number == sal.line
11387 && filename_cmp (symtab_to_fullname (loc->symtab),
11388 sal_fullname) == 0)
11389 line_match = 1;
4aac40c8 11390
0d381245
VP
11391 if (pc_match || line_match)
11392 {
11393 match = 1;
11394 break;
11395 }
11396 }
11397 }
11398
11399 if (match)
81b1e71c 11400 found.push_back (b);
c906108c 11401 }
80f8a6eb 11402 }
8a2c437b 11403
80f8a6eb 11404 /* Now go thru the 'found' chain and delete them. */
81b1e71c 11405 if (found.empty ())
80f8a6eb
MS
11406 {
11407 if (arg)
8a3fe4f8 11408 error (_("No breakpoint at %s."), arg);
80f8a6eb 11409 else
8a3fe4f8 11410 error (_("No breakpoint at this line."));
80f8a6eb 11411 }
c906108c 11412
8a2c437b 11413 /* Remove duplicates from the vec. */
81b1e71c 11414 std::sort (found.begin (), found.end (),
b926417a 11415 [] (const breakpoint *bp_a, const breakpoint *bp_b)
81b1e71c 11416 {
b926417a 11417 return compare_breakpoints (bp_a, bp_b) < 0;
81b1e71c
TT
11418 });
11419 found.erase (std::unique (found.begin (), found.end (),
b926417a 11420 [] (const breakpoint *bp_a, const breakpoint *bp_b)
81b1e71c 11421 {
b926417a 11422 return compare_breakpoints (bp_a, bp_b) == 0;
81b1e71c
TT
11423 }),
11424 found.end ());
8a2c437b 11425
81b1e71c 11426 if (found.size () > 1)
4a64f543 11427 from_tty = 1; /* Always report if deleted more than one. */
80f8a6eb 11428 if (from_tty)
a3f17187 11429 {
81b1e71c 11430 if (found.size () == 1)
a3f17187
AC
11431 printf_unfiltered (_("Deleted breakpoint "));
11432 else
11433 printf_unfiltered (_("Deleted breakpoints "));
11434 }
d6e956e5 11435
81b1e71c 11436 for (breakpoint *iter : found)
80f8a6eb 11437 {
c5aa993b 11438 if (from_tty)
81b1e71c
TT
11439 printf_unfiltered ("%d ", iter->number);
11440 delete_breakpoint (iter);
c906108c 11441 }
80f8a6eb
MS
11442 if (from_tty)
11443 putchar_unfiltered ('\n');
c906108c
SS
11444}
11445\f
11446/* Delete breakpoint in BS if they are `delete' breakpoints and
11447 all breakpoints that are marked for deletion, whether hit or not.
11448 This is called after any breakpoint is hit, or after errors. */
11449
11450void
fba45db2 11451breakpoint_auto_delete (bpstat bs)
c906108c 11452{
35df4500 11453 struct breakpoint *b, *b_tmp;
c906108c
SS
11454
11455 for (; bs; bs = bs->next)
f431efe5
PA
11456 if (bs->breakpoint_at
11457 && bs->breakpoint_at->disposition == disp_del
c906108c 11458 && bs->stop)
f431efe5 11459 delete_breakpoint (bs->breakpoint_at);
c906108c 11460
35df4500 11461 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 11462 {
b5de0fa7 11463 if (b->disposition == disp_del_at_next_stop)
c5aa993b
JM
11464 delete_breakpoint (b);
11465 }
c906108c
SS
11466}
11467
4a64f543 11468/* A comparison function for bp_location AP and BP being interfaced to
39ef2f62 11469 std::sort. Sort elements primarily by their ADDRESS (no matter what
cb1e4e32
PA
11470 bl_address_is_meaningful says), secondarily by ordering first
11471 permanent elements and terciarily just ensuring the array is sorted
39ef2f62 11472 stable way despite std::sort being an unstable algorithm. */
876fa593
JK
11473
11474static int
39ef2f62 11475bp_location_is_less_than (const bp_location *a, const bp_location *b)
876fa593 11476{
876fa593 11477 if (a->address != b->address)
39ef2f62 11478 return a->address < b->address;
876fa593 11479
dea2aa5f
LM
11480 /* Sort locations at the same address by their pspace number, keeping
11481 locations of the same inferior (in a multi-inferior environment)
11482 grouped. */
11483
11484 if (a->pspace->num != b->pspace->num)
39ef2f62 11485 return a->pspace->num < b->pspace->num;
dea2aa5f 11486
876fa593 11487 /* Sort permanent breakpoints first. */
1a853c52 11488 if (a->permanent != b->permanent)
39ef2f62 11489 return a->permanent > b->permanent;
876fa593 11490
c56a97f9
JK
11491 /* Make the internal GDB representation stable across GDB runs
11492 where A and B memory inside GDB can differ. Breakpoint locations of
11493 the same type at the same address can be sorted in arbitrary order. */
876fa593
JK
11494
11495 if (a->owner->number != b->owner->number)
39ef2f62 11496 return a->owner->number < b->owner->number;
876fa593 11497
39ef2f62 11498 return a < b;
876fa593
JK
11499}
11500
f5336ca5
PA
11501/* Set bp_locations_placed_address_before_address_max and
11502 bp_locations_shadow_len_after_address_max according to the current
11503 content of the bp_locations array. */
f7545552
TT
11504
11505static void
f5336ca5 11506bp_locations_target_extensions_update (void)
f7545552 11507{
876fa593
JK
11508 struct bp_location *bl, **blp_tmp;
11509
f5336ca5
PA
11510 bp_locations_placed_address_before_address_max = 0;
11511 bp_locations_shadow_len_after_address_max = 0;
876fa593
JK
11512
11513 ALL_BP_LOCATIONS (bl, blp_tmp)
11514 {
11515 CORE_ADDR start, end, addr;
11516
11517 if (!bp_location_has_shadow (bl))
11518 continue;
11519
11520 start = bl->target_info.placed_address;
11521 end = start + bl->target_info.shadow_len;
11522
11523 gdb_assert (bl->address >= start);
11524 addr = bl->address - start;
f5336ca5
PA
11525 if (addr > bp_locations_placed_address_before_address_max)
11526 bp_locations_placed_address_before_address_max = addr;
876fa593
JK
11527
11528 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
11529
11530 gdb_assert (bl->address < end);
11531 addr = end - bl->address;
f5336ca5
PA
11532 if (addr > bp_locations_shadow_len_after_address_max)
11533 bp_locations_shadow_len_after_address_max = addr;
876fa593 11534 }
f7545552
TT
11535}
11536
1e4d1764
YQ
11537/* Download tracepoint locations if they haven't been. */
11538
11539static void
11540download_tracepoint_locations (void)
11541{
7ed2c994 11542 struct breakpoint *b;
dd2e65cc 11543 enum tribool can_download_tracepoint = TRIBOOL_UNKNOWN;
1e4d1764 11544
5ed8105e 11545 scoped_restore_current_pspace_and_thread restore_pspace_thread;
1e4d1764 11546
7ed2c994 11547 ALL_TRACEPOINTS (b)
1e4d1764 11548 {
7ed2c994 11549 struct bp_location *bl;
1e4d1764 11550 struct tracepoint *t;
f2a8bc8a 11551 int bp_location_downloaded = 0;
1e4d1764 11552
7ed2c994 11553 if ((b->type == bp_fast_tracepoint
1e4d1764
YQ
11554 ? !may_insert_fast_tracepoints
11555 : !may_insert_tracepoints))
11556 continue;
11557
dd2e65cc
YQ
11558 if (can_download_tracepoint == TRIBOOL_UNKNOWN)
11559 {
11560 if (target_can_download_tracepoint ())
11561 can_download_tracepoint = TRIBOOL_TRUE;
11562 else
11563 can_download_tracepoint = TRIBOOL_FALSE;
11564 }
11565
11566 if (can_download_tracepoint == TRIBOOL_FALSE)
11567 break;
11568
7ed2c994
YQ
11569 for (bl = b->loc; bl; bl = bl->next)
11570 {
11571 /* In tracepoint, locations are _never_ duplicated, so
11572 should_be_inserted is equivalent to
11573 unduplicated_should_be_inserted. */
11574 if (!should_be_inserted (bl) || bl->inserted)
11575 continue;
1e4d1764 11576
7ed2c994 11577 switch_to_program_space_and_thread (bl->pspace);
1e4d1764 11578
7ed2c994 11579 target_download_tracepoint (bl);
1e4d1764 11580
7ed2c994 11581 bl->inserted = 1;
f2a8bc8a 11582 bp_location_downloaded = 1;
7ed2c994
YQ
11583 }
11584 t = (struct tracepoint *) b;
11585 t->number_on_target = b->number;
f2a8bc8a 11586 if (bp_location_downloaded)
76727919 11587 gdb::observers::breakpoint_modified.notify (b);
1e4d1764 11588 }
1e4d1764
YQ
11589}
11590
934709f0
PW
11591/* Swap the insertion/duplication state between two locations. */
11592
11593static void
11594swap_insertion (struct bp_location *left, struct bp_location *right)
11595{
11596 const int left_inserted = left->inserted;
11597 const int left_duplicate = left->duplicate;
b775012e 11598 const int left_needs_update = left->needs_update;
934709f0
PW
11599 const struct bp_target_info left_target_info = left->target_info;
11600
1e4d1764
YQ
11601 /* Locations of tracepoints can never be duplicated. */
11602 if (is_tracepoint (left->owner))
11603 gdb_assert (!left->duplicate);
11604 if (is_tracepoint (right->owner))
11605 gdb_assert (!right->duplicate);
11606
934709f0
PW
11607 left->inserted = right->inserted;
11608 left->duplicate = right->duplicate;
b775012e 11609 left->needs_update = right->needs_update;
934709f0
PW
11610 left->target_info = right->target_info;
11611 right->inserted = left_inserted;
11612 right->duplicate = left_duplicate;
b775012e 11613 right->needs_update = left_needs_update;
934709f0
PW
11614 right->target_info = left_target_info;
11615}
11616
b775012e
LM
11617/* Force the re-insertion of the locations at ADDRESS. This is called
11618 once a new/deleted/modified duplicate location is found and we are evaluating
11619 conditions on the target's side. Such conditions need to be updated on
11620 the target. */
11621
11622static void
11623force_breakpoint_reinsertion (struct bp_location *bl)
11624{
11625 struct bp_location **locp = NULL, **loc2p;
11626 struct bp_location *loc;
11627 CORE_ADDR address = 0;
11628 int pspace_num;
11629
11630 address = bl->address;
11631 pspace_num = bl->pspace->num;
11632
11633 /* This is only meaningful if the target is
11634 evaluating conditions and if the user has
11635 opted for condition evaluation on the target's
11636 side. */
11637 if (gdb_evaluates_breakpoint_condition_p ()
11638 || !target_supports_evaluation_of_breakpoint_conditions ())
11639 return;
11640
11641 /* Flag all breakpoint locations with this address and
11642 the same program space as the location
11643 as "its condition has changed". We need to
11644 update the conditions on the target's side. */
11645 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, address)
11646 {
11647 loc = *loc2p;
11648
11649 if (!is_breakpoint (loc->owner)
11650 || pspace_num != loc->pspace->num)
11651 continue;
11652
11653 /* Flag the location appropriately. We use a different state to
11654 let everyone know that we already updated the set of locations
11655 with addr bl->address and program space bl->pspace. This is so
11656 we don't have to keep calling these functions just to mark locations
11657 that have already been marked. */
11658 loc->condition_changed = condition_updated;
11659
11660 /* Free the agent expression bytecode as well. We will compute
11661 it later on. */
833177a4 11662 loc->cond_bytecode.reset ();
b775012e
LM
11663 }
11664}
44702360
PA
11665/* Called whether new breakpoints are created, or existing breakpoints
11666 deleted, to update the global location list and recompute which
11667 locations are duplicate of which.
b775012e 11668
04086b45
PA
11669 The INSERT_MODE flag determines whether locations may not, may, or
11670 shall be inserted now. See 'enum ugll_insert_mode' for more
11671 info. */
b60e7edf 11672
0d381245 11673static void
44702360 11674update_global_location_list (enum ugll_insert_mode insert_mode)
0d381245 11675{
74960c60 11676 struct breakpoint *b;
876fa593 11677 struct bp_location **locp, *loc;
b775012e
LM
11678 /* Last breakpoint location address that was marked for update. */
11679 CORE_ADDR last_addr = 0;
11680 /* Last breakpoint location program space that was marked for update. */
11681 int last_pspace_num = -1;
f7545552 11682
2d134ed3
PA
11683 /* Used in the duplicates detection below. When iterating over all
11684 bp_locations, points to the first bp_location of a given address.
11685 Breakpoints and watchpoints of different types are never
11686 duplicates of each other. Keep one pointer for each type of
11687 breakpoint/watchpoint, so we only need to loop over all locations
11688 once. */
11689 struct bp_location *bp_loc_first; /* breakpoint */
11690 struct bp_location *wp_loc_first; /* hardware watchpoint */
11691 struct bp_location *awp_loc_first; /* access watchpoint */
11692 struct bp_location *rwp_loc_first; /* read watchpoint */
876fa593 11693
f5336ca5
PA
11694 /* Saved former bp_locations array which we compare against the newly
11695 built bp_locations from the current state of ALL_BREAKPOINTS. */
81b1e71c 11696 struct bp_location **old_locp;
f5336ca5 11697 unsigned old_locations_count;
81b1e71c 11698 gdb::unique_xmalloc_ptr<struct bp_location *> old_locations (bp_locations);
876fa593 11699
f5336ca5
PA
11700 old_locations_count = bp_locations_count;
11701 bp_locations = NULL;
11702 bp_locations_count = 0;
0d381245 11703
74960c60 11704 ALL_BREAKPOINTS (b)
876fa593 11705 for (loc = b->loc; loc; loc = loc->next)
f5336ca5 11706 bp_locations_count++;
876fa593 11707
f5336ca5
PA
11708 bp_locations = XNEWVEC (struct bp_location *, bp_locations_count);
11709 locp = bp_locations;
876fa593
JK
11710 ALL_BREAKPOINTS (b)
11711 for (loc = b->loc; loc; loc = loc->next)
11712 *locp++ = loc;
39ef2f62
CB
11713 std::sort (bp_locations, bp_locations + bp_locations_count,
11714 bp_location_is_less_than);
876fa593 11715
f5336ca5 11716 bp_locations_target_extensions_update ();
74960c60 11717
4a64f543
MS
11718 /* Identify bp_location instances that are no longer present in the
11719 new list, and therefore should be freed. Note that it's not
11720 necessary that those locations should be removed from inferior --
11721 if there's another location at the same address (previously
11722 marked as duplicate), we don't need to remove/insert the
11723 location.
876fa593 11724
4a64f543
MS
11725 LOCP is kept in sync with OLD_LOCP, each pointing to the current
11726 and former bp_location array state respectively. */
876fa593 11727
f5336ca5 11728 locp = bp_locations;
81b1e71c
TT
11729 for (old_locp = old_locations.get ();
11730 old_locp < old_locations.get () + old_locations_count;
876fa593 11731 old_locp++)
74960c60 11732 {
876fa593 11733 struct bp_location *old_loc = *old_locp;
c7d46a38 11734 struct bp_location **loc2p;
876fa593 11735
e5dd4106 11736 /* Tells if 'old_loc' is found among the new locations. If
4a64f543 11737 not, we have to free it. */
c7d46a38 11738 int found_object = 0;
20874c92
VP
11739 /* Tells if the location should remain inserted in the target. */
11740 int keep_in_target = 0;
11741 int removed = 0;
876fa593 11742
4a64f543
MS
11743 /* Skip LOCP entries which will definitely never be needed.
11744 Stop either at or being the one matching OLD_LOC. */
f5336ca5 11745 while (locp < bp_locations + bp_locations_count
c7d46a38 11746 && (*locp)->address < old_loc->address)
876fa593 11747 locp++;
c7d46a38
PA
11748
11749 for (loc2p = locp;
f5336ca5 11750 (loc2p < bp_locations + bp_locations_count
c7d46a38
PA
11751 && (*loc2p)->address == old_loc->address);
11752 loc2p++)
11753 {
b775012e
LM
11754 /* Check if this is a new/duplicated location or a duplicated
11755 location that had its condition modified. If so, we want to send
11756 its condition to the target if evaluation of conditions is taking
11757 place there. */
11758 if ((*loc2p)->condition_changed == condition_modified
11759 && (last_addr != old_loc->address
11760 || last_pspace_num != old_loc->pspace->num))
c7d46a38 11761 {
b775012e
LM
11762 force_breakpoint_reinsertion (*loc2p);
11763 last_pspace_num = old_loc->pspace->num;
c7d46a38 11764 }
b775012e
LM
11765
11766 if (*loc2p == old_loc)
11767 found_object = 1;
c7d46a38 11768 }
74960c60 11769
b775012e
LM
11770 /* We have already handled this address, update it so that we don't
11771 have to go through updates again. */
11772 last_addr = old_loc->address;
11773
11774 /* Target-side condition evaluation: Handle deleted locations. */
11775 if (!found_object)
11776 force_breakpoint_reinsertion (old_loc);
11777
4a64f543
MS
11778 /* If this location is no longer present, and inserted, look if
11779 there's maybe a new location at the same address. If so,
11780 mark that one inserted, and don't remove this one. This is
11781 needed so that we don't have a time window where a breakpoint
11782 at certain location is not inserted. */
74960c60 11783
876fa593 11784 if (old_loc->inserted)
0d381245 11785 {
4a64f543
MS
11786 /* If the location is inserted now, we might have to remove
11787 it. */
74960c60 11788
876fa593 11789 if (found_object && should_be_inserted (old_loc))
74960c60 11790 {
4a64f543
MS
11791 /* The location is still present in the location list,
11792 and still should be inserted. Don't do anything. */
20874c92 11793 keep_in_target = 1;
74960c60
VP
11794 }
11795 else
11796 {
b775012e
LM
11797 /* This location still exists, but it won't be kept in the
11798 target since it may have been disabled. We proceed to
11799 remove its target-side condition. */
11800
4a64f543
MS
11801 /* The location is either no longer present, or got
11802 disabled. See if there's another location at the
11803 same address, in which case we don't need to remove
11804 this one from the target. */
876fa593 11805
2bdf28a0 11806 /* OLD_LOC comes from existing struct breakpoint. */
cb1e4e32 11807 if (bl_address_is_meaningful (old_loc))
876fa593 11808 {
876fa593 11809 for (loc2p = locp;
f5336ca5 11810 (loc2p < bp_locations + bp_locations_count
c7d46a38 11811 && (*loc2p)->address == old_loc->address);
876fa593
JK
11812 loc2p++)
11813 {
11814 struct bp_location *loc2 = *loc2p;
11815
2d134ed3 11816 if (breakpoint_locations_match (loc2, old_loc))
c7d46a38 11817 {
85d721b8
PA
11818 /* Read watchpoint locations are switched to
11819 access watchpoints, if the former are not
11820 supported, but the latter are. */
11821 if (is_hardware_watchpoint (old_loc->owner))
11822 {
11823 gdb_assert (is_hardware_watchpoint (loc2->owner));
11824 loc2->watchpoint_type = old_loc->watchpoint_type;
11825 }
11826
934709f0
PW
11827 /* loc2 is a duplicated location. We need to check
11828 if it should be inserted in case it will be
11829 unduplicated. */
11830 if (loc2 != old_loc
11831 && unduplicated_should_be_inserted (loc2))
c7d46a38 11832 {
934709f0 11833 swap_insertion (old_loc, loc2);
c7d46a38
PA
11834 keep_in_target = 1;
11835 break;
11836 }
876fa593
JK
11837 }
11838 }
11839 }
74960c60
VP
11840 }
11841
20874c92
VP
11842 if (!keep_in_target)
11843 {
834c0d03 11844 if (remove_breakpoint (old_loc))
20874c92 11845 {
4a64f543
MS
11846 /* This is just about all we can do. We could keep
11847 this location on the global list, and try to
11848 remove it next time, but there's no particular
11849 reason why we will succeed next time.
20874c92 11850
4a64f543
MS
11851 Note that at this point, old_loc->owner is still
11852 valid, as delete_breakpoint frees the breakpoint
11853 only after calling us. */
3e43a32a
MS
11854 printf_filtered (_("warning: Error removing "
11855 "breakpoint %d\n"),
876fa593 11856 old_loc->owner->number);
20874c92
VP
11857 }
11858 removed = 1;
11859 }
0d381245 11860 }
74960c60
VP
11861
11862 if (!found_object)
1c5cfe86 11863 {
fbea99ea 11864 if (removed && target_is_non_stop_p ()
1cf4d951 11865 && need_moribund_for_location_type (old_loc))
20874c92 11866 {
db82e815
PA
11867 /* This location was removed from the target. In
11868 non-stop mode, a race condition is possible where
11869 we've removed a breakpoint, but stop events for that
11870 breakpoint are already queued and will arrive later.
11871 We apply an heuristic to be able to distinguish such
11872 SIGTRAPs from other random SIGTRAPs: we keep this
11873 breakpoint location for a bit, and will retire it
11874 after we see some number of events. The theory here
11875 is that reporting of events should, "on the average",
11876 be fair, so after a while we'll see events from all
11877 threads that have anything of interest, and no longer
11878 need to keep this breakpoint location around. We
11879 don't hold locations forever so to reduce chances of
11880 mistaking a non-breakpoint SIGTRAP for a breakpoint
11881 SIGTRAP.
11882
11883 The heuristic failing can be disastrous on
11884 decr_pc_after_break targets.
11885
11886 On decr_pc_after_break targets, like e.g., x86-linux,
11887 if we fail to recognize a late breakpoint SIGTRAP,
11888 because events_till_retirement has reached 0 too
11889 soon, we'll fail to do the PC adjustment, and report
11890 a random SIGTRAP to the user. When the user resumes
11891 the inferior, it will most likely immediately crash
2dec564e 11892 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
db82e815
PA
11893 corrupted, because of being resumed e.g., in the
11894 middle of a multi-byte instruction, or skipped a
11895 one-byte instruction. This was actually seen happen
11896 on native x86-linux, and should be less rare on
11897 targets that do not support new thread events, like
11898 remote, due to the heuristic depending on
11899 thread_count.
11900
11901 Mistaking a random SIGTRAP for a breakpoint trap
11902 causes similar symptoms (PC adjustment applied when
11903 it shouldn't), but then again, playing with SIGTRAPs
11904 behind the debugger's back is asking for trouble.
11905
11906 Since hardware watchpoint traps are always
11907 distinguishable from other traps, so we don't need to
11908 apply keep hardware watchpoint moribund locations
11909 around. We simply always ignore hardware watchpoint
11910 traps we can no longer explain. */
11911
876fa593
JK
11912 old_loc->events_till_retirement = 3 * (thread_count () + 1);
11913 old_loc->owner = NULL;
20874c92 11914
1123588c 11915 moribund_locations.push_back (old_loc);
1c5cfe86
PA
11916 }
11917 else
f431efe5
PA
11918 {
11919 old_loc->owner = NULL;
11920 decref_bp_location (&old_loc);
11921 }
20874c92 11922 }
74960c60 11923 }
1c5cfe86 11924
348d480f
PA
11925 /* Rescan breakpoints at the same address and section, marking the
11926 first one as "first" and any others as "duplicates". This is so
11927 that the bpt instruction is only inserted once. If we have a
11928 permanent breakpoint at the same place as BPT, make that one the
11929 official one, and the rest as duplicates. Permanent breakpoints
11930 are sorted first for the same address.
11931
11932 Do the same for hardware watchpoints, but also considering the
11933 watchpoint's type (regular/access/read) and length. */
11934
11935 bp_loc_first = NULL;
11936 wp_loc_first = NULL;
11937 awp_loc_first = NULL;
11938 rwp_loc_first = NULL;
11939 ALL_BP_LOCATIONS (loc, locp)
11940 {
11941 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
11942 non-NULL. */
348d480f 11943 struct bp_location **loc_first_p;
d3fbdd86 11944 b = loc->owner;
348d480f 11945
6f380991 11946 if (!unduplicated_should_be_inserted (loc)
cb1e4e32 11947 || !bl_address_is_meaningful (loc)
1e4d1764
YQ
11948 /* Don't detect duplicate for tracepoint locations because they are
11949 never duplicated. See the comments in field `duplicate' of
11950 `struct bp_location'. */
348d480f 11951 || is_tracepoint (b))
b775012e
LM
11952 {
11953 /* Clear the condition modification flag. */
11954 loc->condition_changed = condition_unchanged;
11955 continue;
11956 }
348d480f 11957
348d480f
PA
11958 if (b->type == bp_hardware_watchpoint)
11959 loc_first_p = &wp_loc_first;
11960 else if (b->type == bp_read_watchpoint)
11961 loc_first_p = &rwp_loc_first;
11962 else if (b->type == bp_access_watchpoint)
11963 loc_first_p = &awp_loc_first;
11964 else
11965 loc_first_p = &bp_loc_first;
11966
11967 if (*loc_first_p == NULL
11968 || (overlay_debugging && loc->section != (*loc_first_p)->section)
11969 || !breakpoint_locations_match (loc, *loc_first_p))
11970 {
11971 *loc_first_p = loc;
11972 loc->duplicate = 0;
b775012e
LM
11973
11974 if (is_breakpoint (loc->owner) && loc->condition_changed)
11975 {
11976 loc->needs_update = 1;
11977 /* Clear the condition modification flag. */
11978 loc->condition_changed = condition_unchanged;
11979 }
348d480f
PA
11980 continue;
11981 }
11982
934709f0
PW
11983
11984 /* This and the above ensure the invariant that the first location
11985 is not duplicated, and is the inserted one.
11986 All following are marked as duplicated, and are not inserted. */
11987 if (loc->inserted)
11988 swap_insertion (loc, *loc_first_p);
348d480f
PA
11989 loc->duplicate = 1;
11990
b775012e
LM
11991 /* Clear the condition modification flag. */
11992 loc->condition_changed = condition_unchanged;
348d480f
PA
11993 }
11994
a25a5a45 11995 if (insert_mode == UGLL_INSERT || breakpoints_should_be_inserted_now ())
b775012e 11996 {
04086b45 11997 if (insert_mode != UGLL_DONT_INSERT)
b775012e
LM
11998 insert_breakpoint_locations ();
11999 else
12000 {
44702360
PA
12001 /* Even though the caller told us to not insert new
12002 locations, we may still need to update conditions on the
12003 target's side of breakpoints that were already inserted
12004 if the target is evaluating breakpoint conditions. We
b775012e
LM
12005 only update conditions for locations that are marked
12006 "needs_update". */
12007 update_inserted_breakpoint_locations ();
12008 }
12009 }
348d480f 12010
04086b45 12011 if (insert_mode != UGLL_DONT_INSERT)
1e4d1764 12012 download_tracepoint_locations ();
348d480f
PA
12013}
12014
12015void
12016breakpoint_retire_moribund (void)
12017{
1123588c
TT
12018 for (int ix = 0; ix < moribund_locations.size (); ++ix)
12019 {
12020 struct bp_location *loc = moribund_locations[ix];
12021 if (--(loc->events_till_retirement) == 0)
12022 {
12023 decref_bp_location (&loc);
12024 unordered_remove (moribund_locations, ix);
12025 --ix;
12026 }
12027 }
348d480f
PA
12028}
12029
12030static void
44702360 12031update_global_location_list_nothrow (enum ugll_insert_mode insert_mode)
348d480f 12032{
348d480f 12033
a70b8144 12034 try
492d29ea
PA
12035 {
12036 update_global_location_list (insert_mode);
12037 }
230d2906 12038 catch (const gdb_exception_error &e)
492d29ea
PA
12039 {
12040 }
348d480f
PA
12041}
12042
12043/* Clear BKP from a BPS. */
12044
12045static void
12046bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
12047{
12048 bpstat bs;
12049
12050 for (bs = bps; bs; bs = bs->next)
12051 if (bs->breakpoint_at == bpt)
12052 {
12053 bs->breakpoint_at = NULL;
12054 bs->old_val = NULL;
12055 /* bs->commands will be freed later. */
12056 }
12057}
12058
12059/* Callback for iterate_over_threads. */
12060static int
12061bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
12062{
9a3c8263 12063 struct breakpoint *bpt = (struct breakpoint *) data;
348d480f
PA
12064
12065 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
12066 return 0;
12067}
12068
12069/* Helper for breakpoint and tracepoint breakpoint_ops->mention
12070 callbacks. */
12071
12072static void
12073say_where (struct breakpoint *b)
12074{
12075 struct value_print_options opts;
12076
12077 get_user_print_options (&opts);
12078
12079 /* i18n: cagney/2005-02-11: Below needs to be merged into a
12080 single string. */
12081 if (b->loc == NULL)
12082 {
f00aae0f
KS
12083 /* For pending locations, the output differs slightly based
12084 on b->extra_string. If this is non-NULL, it contains either
12085 a condition or dprintf arguments. */
12086 if (b->extra_string == NULL)
12087 {
12088 printf_filtered (_(" (%s) pending."),
d28cd78a 12089 event_location_to_string (b->location.get ()));
f00aae0f
KS
12090 }
12091 else if (b->type == bp_dprintf)
12092 {
12093 printf_filtered (_(" (%s,%s) pending."),
d28cd78a 12094 event_location_to_string (b->location.get ()),
f00aae0f
KS
12095 b->extra_string);
12096 }
12097 else
12098 {
12099 printf_filtered (_(" (%s %s) pending."),
d28cd78a 12100 event_location_to_string (b->location.get ()),
f00aae0f
KS
12101 b->extra_string);
12102 }
348d480f
PA
12103 }
12104 else
12105 {
2f202fde 12106 if (opts.addressprint || b->loc->symtab == NULL)
6a831f06
PA
12107 printf_filtered (" at %ps",
12108 styled_string (address_style.style (),
12109 paddress (b->loc->gdbarch,
12110 b->loc->address)));
2f202fde 12111 if (b->loc->symtab != NULL)
f8eba3c6
TT
12112 {
12113 /* If there is a single location, we can print the location
12114 more nicely. */
12115 if (b->loc->next == NULL)
0bb296cb 12116 {
6a831f06
PA
12117 const char *filename
12118 = symtab_to_filename_for_display (b->loc->symtab);
12119 printf_filtered (": file %ps, line %d.",
12120 styled_string (file_name_style.style (),
12121 filename),
0bb296cb
TT
12122 b->loc->line_number);
12123 }
f8eba3c6
TT
12124 else
12125 /* This is not ideal, but each location may have a
12126 different file name, and this at least reflects the
12127 real situation somewhat. */
f00aae0f 12128 printf_filtered (": %s.",
d28cd78a 12129 event_location_to_string (b->location.get ()));
f8eba3c6 12130 }
348d480f
PA
12131
12132 if (b->loc->next)
12133 {
12134 struct bp_location *loc = b->loc;
12135 int n = 0;
12136 for (; loc; loc = loc->next)
12137 ++n;
12138 printf_filtered (" (%d locations)", n);
12139 }
12140 }
12141}
12142
5f486660 12143bp_location::~bp_location ()
348d480f 12144{
5f486660 12145 xfree (function_name);
348d480f
PA
12146}
12147
c1fc2657 12148/* Destructor for the breakpoint base class. */
348d480f 12149
c1fc2657 12150breakpoint::~breakpoint ()
348d480f 12151{
c1fc2657
SM
12152 xfree (this->cond_string);
12153 xfree (this->extra_string);
348d480f
PA
12154}
12155
2060206e
PA
12156static struct bp_location *
12157base_breakpoint_allocate_location (struct breakpoint *self)
348d480f 12158{
5f486660 12159 return new bp_location (self);
348d480f
PA
12160}
12161
2060206e
PA
12162static void
12163base_breakpoint_re_set (struct breakpoint *b)
12164{
12165 /* Nothing to re-set. */
12166}
12167
12168#define internal_error_pure_virtual_called() \
12169 gdb_assert_not_reached ("pure virtual function called")
12170
12171static int
12172base_breakpoint_insert_location (struct bp_location *bl)
12173{
12174 internal_error_pure_virtual_called ();
12175}
12176
12177static int
73971819
PA
12178base_breakpoint_remove_location (struct bp_location *bl,
12179 enum remove_bp_reason reason)
2060206e
PA
12180{
12181 internal_error_pure_virtual_called ();
12182}
12183
12184static int
12185base_breakpoint_breakpoint_hit (const struct bp_location *bl,
bd522513 12186 const address_space *aspace,
09ac7c10
TT
12187 CORE_ADDR bp_addr,
12188 const struct target_waitstatus *ws)
2060206e
PA
12189{
12190 internal_error_pure_virtual_called ();
12191}
12192
12193static void
12194base_breakpoint_check_status (bpstat bs)
12195{
12196 /* Always stop. */
12197}
12198
12199/* A "works_in_software_mode" breakpoint_ops method that just internal
12200 errors. */
12201
12202static int
12203base_breakpoint_works_in_software_mode (const struct breakpoint *b)
12204{
12205 internal_error_pure_virtual_called ();
12206}
12207
12208/* A "resources_needed" breakpoint_ops method that just internal
12209 errors. */
12210
12211static int
12212base_breakpoint_resources_needed (const struct bp_location *bl)
12213{
12214 internal_error_pure_virtual_called ();
12215}
12216
12217static enum print_stop_action
12218base_breakpoint_print_it (bpstat bs)
12219{
12220 internal_error_pure_virtual_called ();
12221}
12222
12223static void
12224base_breakpoint_print_one_detail (const struct breakpoint *self,
12225 struct ui_out *uiout)
12226{
12227 /* nothing */
12228}
12229
12230static void
12231base_breakpoint_print_mention (struct breakpoint *b)
12232{
12233 internal_error_pure_virtual_called ();
12234}
12235
12236static void
12237base_breakpoint_print_recreate (struct breakpoint *b, struct ui_file *fp)
12238{
12239 internal_error_pure_virtual_called ();
12240}
12241
983af33b 12242static void
f00aae0f
KS
12243base_breakpoint_create_sals_from_location
12244 (const struct event_location *location,
12245 struct linespec_result *canonical,
12246 enum bptype type_wanted)
983af33b
SDJ
12247{
12248 internal_error_pure_virtual_called ();
12249}
12250
12251static void
12252base_breakpoint_create_breakpoints_sal (struct gdbarch *gdbarch,
12253 struct linespec_result *c,
e1e01040
PA
12254 gdb::unique_xmalloc_ptr<char> cond_string,
12255 gdb::unique_xmalloc_ptr<char> extra_string,
983af33b
SDJ
12256 enum bptype type_wanted,
12257 enum bpdisp disposition,
12258 int thread,
12259 int task, int ignore_count,
12260 const struct breakpoint_ops *o,
12261 int from_tty, int enabled,
44f238bb 12262 int internal, unsigned flags)
983af33b
SDJ
12263{
12264 internal_error_pure_virtual_called ();
12265}
12266
6c5b2ebe 12267static std::vector<symtab_and_line>
f00aae0f
KS
12268base_breakpoint_decode_location (struct breakpoint *b,
12269 const struct event_location *location,
6c5b2ebe 12270 struct program_space *search_pspace)
983af33b
SDJ
12271{
12272 internal_error_pure_virtual_called ();
12273}
12274
ab04a2af
TT
12275/* The default 'explains_signal' method. */
12276
47591c29 12277static int
427cd150 12278base_breakpoint_explains_signal (struct breakpoint *b, enum gdb_signal sig)
ab04a2af 12279{
47591c29 12280 return 1;
ab04a2af
TT
12281}
12282
9d6e6e84
HZ
12283/* The default "after_condition_true" method. */
12284
12285static void
12286base_breakpoint_after_condition_true (struct bpstats *bs)
12287{
12288 /* Nothing to do. */
12289}
12290
ab04a2af 12291struct breakpoint_ops base_breakpoint_ops =
2060206e 12292{
2060206e
PA
12293 base_breakpoint_allocate_location,
12294 base_breakpoint_re_set,
12295 base_breakpoint_insert_location,
12296 base_breakpoint_remove_location,
12297 base_breakpoint_breakpoint_hit,
12298 base_breakpoint_check_status,
12299 base_breakpoint_resources_needed,
12300 base_breakpoint_works_in_software_mode,
12301 base_breakpoint_print_it,
12302 NULL,
12303 base_breakpoint_print_one_detail,
12304 base_breakpoint_print_mention,
983af33b 12305 base_breakpoint_print_recreate,
5f700d83 12306 base_breakpoint_create_sals_from_location,
983af33b 12307 base_breakpoint_create_breakpoints_sal,
5f700d83 12308 base_breakpoint_decode_location,
9d6e6e84
HZ
12309 base_breakpoint_explains_signal,
12310 base_breakpoint_after_condition_true,
2060206e
PA
12311};
12312
12313/* Default breakpoint_ops methods. */
12314
12315static void
348d480f
PA
12316bkpt_re_set (struct breakpoint *b)
12317{
06edf0c0 12318 /* FIXME: is this still reachable? */
9ef9e6a6 12319 if (breakpoint_event_location_empty_p (b))
06edf0c0 12320 {
f00aae0f 12321 /* Anything without a location can't be re-set. */
348d480f 12322 delete_breakpoint (b);
06edf0c0 12323 return;
348d480f 12324 }
06edf0c0
PA
12325
12326 breakpoint_re_set_default (b);
348d480f
PA
12327}
12328
2060206e 12329static int
348d480f
PA
12330bkpt_insert_location (struct bp_location *bl)
12331{
cd6c3b4f
YQ
12332 CORE_ADDR addr = bl->target_info.reqstd_address;
12333
579c6ad9 12334 bl->target_info.kind = breakpoint_kind (bl, &addr);
cd6c3b4f
YQ
12335 bl->target_info.placed_address = addr;
12336
348d480f 12337 if (bl->loc_type == bp_loc_hardware_breakpoint)
7c16b83e 12338 return target_insert_hw_breakpoint (bl->gdbarch, &bl->target_info);
348d480f 12339 else
7c16b83e 12340 return target_insert_breakpoint (bl->gdbarch, &bl->target_info);
348d480f
PA
12341}
12342
2060206e 12343static int
73971819 12344bkpt_remove_location (struct bp_location *bl, enum remove_bp_reason reason)
348d480f
PA
12345{
12346 if (bl->loc_type == bp_loc_hardware_breakpoint)
12347 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
12348 else
73971819 12349 return target_remove_breakpoint (bl->gdbarch, &bl->target_info, reason);
348d480f
PA
12350}
12351
2060206e 12352static int
348d480f 12353bkpt_breakpoint_hit (const struct bp_location *bl,
bd522513 12354 const address_space *aspace, CORE_ADDR bp_addr,
09ac7c10 12355 const struct target_waitstatus *ws)
348d480f 12356{
09ac7c10 12357 if (ws->kind != TARGET_WAITKIND_STOPPED
a493e3e2 12358 || ws->value.sig != GDB_SIGNAL_TRAP)
09ac7c10
TT
12359 return 0;
12360
348d480f
PA
12361 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
12362 aspace, bp_addr))
12363 return 0;
12364
12365 if (overlay_debugging /* unmapped overlay section */
12366 && section_is_overlay (bl->section)
12367 && !section_is_mapped (bl->section))
12368 return 0;
12369
12370 return 1;
12371}
12372
cd1608cc
PA
12373static int
12374dprintf_breakpoint_hit (const struct bp_location *bl,
bd522513 12375 const address_space *aspace, CORE_ADDR bp_addr,
cd1608cc
PA
12376 const struct target_waitstatus *ws)
12377{
12378 if (dprintf_style == dprintf_style_agent
12379 && target_can_run_breakpoint_commands ())
12380 {
12381 /* An agent-style dprintf never causes a stop. If we see a trap
12382 for this address it must be for a breakpoint that happens to
12383 be set at the same address. */
12384 return 0;
12385 }
12386
12387 return bkpt_breakpoint_hit (bl, aspace, bp_addr, ws);
12388}
12389
2060206e 12390static int
348d480f
PA
12391bkpt_resources_needed (const struct bp_location *bl)
12392{
12393 gdb_assert (bl->owner->type == bp_hardware_breakpoint);
12394
12395 return 1;
12396}
12397
2060206e 12398static enum print_stop_action
348d480f
PA
12399bkpt_print_it (bpstat bs)
12400{
348d480f
PA
12401 struct breakpoint *b;
12402 const struct bp_location *bl;
001c8c33 12403 int bp_temp;
79a45e25 12404 struct ui_out *uiout = current_uiout;
348d480f
PA
12405
12406 gdb_assert (bs->bp_location_at != NULL);
12407
12408 bl = bs->bp_location_at;
12409 b = bs->breakpoint_at;
12410
001c8c33
PA
12411 bp_temp = b->disposition == disp_del;
12412 if (bl->address != bl->requested_address)
12413 breakpoint_adjustment_warning (bl->requested_address,
12414 bl->address,
12415 b->number, 1);
12416 annotate_breakpoint (b->number);
f303dbd6
PA
12417 maybe_print_thread_hit_breakpoint (uiout);
12418
112e8700 12419 if (uiout->is_mi_like_p ())
348d480f 12420 {
112e8700 12421 uiout->field_string ("reason",
001c8c33 12422 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
112e8700 12423 uiout->field_string ("disp", bpdisp_text (b->disposition));
06edf0c0 12424 }
6a831f06
PA
12425 if (bp_temp)
12426 uiout->message ("Temporary breakpoint %pF, ",
12427 signed_field ("bkptno", b->number));
12428 else
12429 uiout->message ("Breakpoint %pF, ",
12430 signed_field ("bkptno", b->number));
06edf0c0 12431
001c8c33 12432 return PRINT_SRC_AND_LOC;
06edf0c0
PA
12433}
12434
2060206e 12435static void
06edf0c0
PA
12436bkpt_print_mention (struct breakpoint *b)
12437{
112e8700 12438 if (current_uiout->is_mi_like_p ())
06edf0c0
PA
12439 return;
12440
12441 switch (b->type)
12442 {
12443 case bp_breakpoint:
12444 case bp_gnu_ifunc_resolver:
12445 if (b->disposition == disp_del)
12446 printf_filtered (_("Temporary breakpoint"));
12447 else
12448 printf_filtered (_("Breakpoint"));
12449 printf_filtered (_(" %d"), b->number);
12450 if (b->type == bp_gnu_ifunc_resolver)
12451 printf_filtered (_(" at gnu-indirect-function resolver"));
12452 break;
12453 case bp_hardware_breakpoint:
12454 printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
12455 break;
e7e0cddf
SS
12456 case bp_dprintf:
12457 printf_filtered (_("Dprintf %d"), b->number);
12458 break;
06edf0c0
PA
12459 }
12460
12461 say_where (b);
12462}
12463
2060206e 12464static void
06edf0c0
PA
12465bkpt_print_recreate (struct breakpoint *tp, struct ui_file *fp)
12466{
12467 if (tp->type == bp_breakpoint && tp->disposition == disp_del)
12468 fprintf_unfiltered (fp, "tbreak");
12469 else if (tp->type == bp_breakpoint)
12470 fprintf_unfiltered (fp, "break");
12471 else if (tp->type == bp_hardware_breakpoint
12472 && tp->disposition == disp_del)
12473 fprintf_unfiltered (fp, "thbreak");
12474 else if (tp->type == bp_hardware_breakpoint)
12475 fprintf_unfiltered (fp, "hbreak");
12476 else
12477 internal_error (__FILE__, __LINE__,
12478 _("unhandled breakpoint type %d"), (int) tp->type);
12479
f00aae0f 12480 fprintf_unfiltered (fp, " %s",
d28cd78a 12481 event_location_to_string (tp->location.get ()));
f00aae0f
KS
12482
12483 /* Print out extra_string if this breakpoint is pending. It might
12484 contain, for example, conditions that were set by the user. */
12485 if (tp->loc == NULL && tp->extra_string != NULL)
12486 fprintf_unfiltered (fp, " %s", tp->extra_string);
12487
dd11a36c 12488 print_recreate_thread (tp, fp);
06edf0c0
PA
12489}
12490
983af33b 12491static void
f00aae0f
KS
12492bkpt_create_sals_from_location (const struct event_location *location,
12493 struct linespec_result *canonical,
12494 enum bptype type_wanted)
983af33b 12495{
f00aae0f 12496 create_sals_from_location_default (location, canonical, type_wanted);
983af33b
SDJ
12497}
12498
12499static void
12500bkpt_create_breakpoints_sal (struct gdbarch *gdbarch,
12501 struct linespec_result *canonical,
e1e01040
PA
12502 gdb::unique_xmalloc_ptr<char> cond_string,
12503 gdb::unique_xmalloc_ptr<char> extra_string,
983af33b
SDJ
12504 enum bptype type_wanted,
12505 enum bpdisp disposition,
12506 int thread,
12507 int task, int ignore_count,
12508 const struct breakpoint_ops *ops,
12509 int from_tty, int enabled,
44f238bb 12510 int internal, unsigned flags)
983af33b 12511{
023fa29b 12512 create_breakpoints_sal_default (gdbarch, canonical,
e1e01040
PA
12513 std::move (cond_string),
12514 std::move (extra_string),
e7e0cddf 12515 type_wanted,
983af33b
SDJ
12516 disposition, thread, task,
12517 ignore_count, ops, from_tty,
44f238bb 12518 enabled, internal, flags);
983af33b
SDJ
12519}
12520
6c5b2ebe 12521static std::vector<symtab_and_line>
f00aae0f
KS
12522bkpt_decode_location (struct breakpoint *b,
12523 const struct event_location *location,
6c5b2ebe 12524 struct program_space *search_pspace)
983af33b 12525{
6c5b2ebe 12526 return decode_location_default (b, location, search_pspace);
983af33b
SDJ
12527}
12528
06edf0c0
PA
12529/* Virtual table for internal breakpoints. */
12530
12531static void
12532internal_bkpt_re_set (struct breakpoint *b)
12533{
12534 switch (b->type)
12535 {
12536 /* Delete overlay event and longjmp master breakpoints; they
12537 will be reset later by breakpoint_re_set. */
12538 case bp_overlay_event:
12539 case bp_longjmp_master:
12540 case bp_std_terminate_master:
12541 case bp_exception_master:
12542 delete_breakpoint (b);
12543 break;
12544
12545 /* This breakpoint is special, it's set up when the inferior
12546 starts and we really don't want to touch it. */
12547 case bp_shlib_event:
12548
12549 /* Like bp_shlib_event, this breakpoint type is special. Once
12550 it is set up, we do not want to touch it. */
12551 case bp_thread_event:
12552 break;
12553 }
12554}
12555
12556static void
12557internal_bkpt_check_status (bpstat bs)
12558{
a9b3a50f
PA
12559 if (bs->breakpoint_at->type == bp_shlib_event)
12560 {
12561 /* If requested, stop when the dynamic linker notifies GDB of
12562 events. This allows the user to get control and place
12563 breakpoints in initializer routines for dynamically loaded
12564 objects (among other things). */
12565 bs->stop = stop_on_solib_events;
12566 bs->print = stop_on_solib_events;
12567 }
12568 else
12569 bs->stop = 0;
06edf0c0
PA
12570}
12571
12572static enum print_stop_action
12573internal_bkpt_print_it (bpstat bs)
12574{
06edf0c0 12575 struct breakpoint *b;
06edf0c0 12576
06edf0c0
PA
12577 b = bs->breakpoint_at;
12578
06edf0c0
PA
12579 switch (b->type)
12580 {
348d480f
PA
12581 case bp_shlib_event:
12582 /* Did we stop because the user set the stop_on_solib_events
12583 variable? (If so, we report this as a generic, "Stopped due
12584 to shlib event" message.) */
edcc5120 12585 print_solib_event (0);
348d480f
PA
12586 break;
12587
12588 case bp_thread_event:
12589 /* Not sure how we will get here.
12590 GDB should not stop for these breakpoints. */
12591 printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
12592 break;
12593
12594 case bp_overlay_event:
12595 /* By analogy with the thread event, GDB should not stop for these. */
12596 printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
12597 break;
12598
12599 case bp_longjmp_master:
12600 /* These should never be enabled. */
12601 printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
348d480f
PA
12602 break;
12603
12604 case bp_std_terminate_master:
12605 /* These should never be enabled. */
12606 printf_filtered (_("std::terminate Master Breakpoint: "
12607 "gdb should not stop!\n"));
348d480f
PA
12608 break;
12609
12610 case bp_exception_master:
12611 /* These should never be enabled. */
12612 printf_filtered (_("Exception Master Breakpoint: "
12613 "gdb should not stop!\n"));
06edf0c0
PA
12614 break;
12615 }
12616
001c8c33 12617 return PRINT_NOTHING;
06edf0c0
PA
12618}
12619
12620static void
12621internal_bkpt_print_mention (struct breakpoint *b)
12622{
12623 /* Nothing to mention. These breakpoints are internal. */
12624}
12625
06edf0c0
PA
12626/* Virtual table for momentary breakpoints */
12627
12628static void
12629momentary_bkpt_re_set (struct breakpoint *b)
12630{
12631 /* Keep temporary breakpoints, which can be encountered when we step
4d1eb6b4 12632 over a dlopen call and solib_add is resetting the breakpoints.
06edf0c0
PA
12633 Otherwise these should have been blown away via the cleanup chain
12634 or by breakpoint_init_inferior when we rerun the executable. */
12635}
12636
12637static void
12638momentary_bkpt_check_status (bpstat bs)
12639{
12640 /* Nothing. The point of these breakpoints is causing a stop. */
12641}
12642
12643static enum print_stop_action
12644momentary_bkpt_print_it (bpstat bs)
12645{
001c8c33 12646 return PRINT_UNKNOWN;
348d480f
PA
12647}
12648
06edf0c0
PA
12649static void
12650momentary_bkpt_print_mention (struct breakpoint *b)
348d480f 12651{
06edf0c0 12652 /* Nothing to mention. These breakpoints are internal. */
348d480f
PA
12653}
12654
e2e4d78b
JK
12655/* Ensure INITIATING_FRAME is cleared when no such breakpoint exists.
12656
12657 It gets cleared already on the removal of the first one of such placed
12658 breakpoints. This is OK as they get all removed altogether. */
12659
c1fc2657 12660longjmp_breakpoint::~longjmp_breakpoint ()
e2e4d78b 12661{
c1fc2657 12662 thread_info *tp = find_thread_global_id (this->thread);
e2e4d78b 12663
c1fc2657 12664 if (tp != NULL)
e2e4d78b 12665 tp->initiating_frame = null_frame_id;
e2e4d78b
JK
12666}
12667
55aa24fb
SDJ
12668/* Specific methods for probe breakpoints. */
12669
12670static int
12671bkpt_probe_insert_location (struct bp_location *bl)
12672{
12673 int v = bkpt_insert_location (bl);
12674
12675 if (v == 0)
12676 {
12677 /* The insertion was successful, now let's set the probe's semaphore
12678 if needed. */
935676c9 12679 bl->probe.prob->set_semaphore (bl->probe.objfile, bl->gdbarch);
55aa24fb
SDJ
12680 }
12681
12682 return v;
12683}
12684
12685static int
73971819
PA
12686bkpt_probe_remove_location (struct bp_location *bl,
12687 enum remove_bp_reason reason)
55aa24fb
SDJ
12688{
12689 /* Let's clear the semaphore before removing the location. */
935676c9 12690 bl->probe.prob->clear_semaphore (bl->probe.objfile, bl->gdbarch);
55aa24fb 12691
73971819 12692 return bkpt_remove_location (bl, reason);
55aa24fb
SDJ
12693}
12694
12695static void
f00aae0f 12696bkpt_probe_create_sals_from_location (const struct event_location *location,
5f700d83 12697 struct linespec_result *canonical,
f00aae0f 12698 enum bptype type_wanted)
55aa24fb
SDJ
12699{
12700 struct linespec_sals lsal;
12701
c2f4122d 12702 lsal.sals = parse_probes (location, NULL, canonical);
8e9e35b1
TT
12703 lsal.canonical
12704 = xstrdup (event_location_to_string (canonical->location.get ()));
6c5b2ebe 12705 canonical->lsals.push_back (std::move (lsal));
55aa24fb
SDJ
12706}
12707
6c5b2ebe 12708static std::vector<symtab_and_line>
f00aae0f
KS
12709bkpt_probe_decode_location (struct breakpoint *b,
12710 const struct event_location *location,
6c5b2ebe 12711 struct program_space *search_pspace)
55aa24fb 12712{
6c5b2ebe
PA
12713 std::vector<symtab_and_line> sals = parse_probes (location, search_pspace, NULL);
12714 if (sals.empty ())
55aa24fb 12715 error (_("probe not found"));
6c5b2ebe 12716 return sals;
55aa24fb
SDJ
12717}
12718
348d480f 12719/* The breakpoint_ops structure to be used in tracepoints. */
876fa593 12720
348d480f
PA
12721static void
12722tracepoint_re_set (struct breakpoint *b)
12723{
12724 breakpoint_re_set_default (b);
12725}
876fa593 12726
348d480f
PA
12727static int
12728tracepoint_breakpoint_hit (const struct bp_location *bl,
bd522513 12729 const address_space *aspace, CORE_ADDR bp_addr,
09ac7c10 12730 const struct target_waitstatus *ws)
348d480f
PA
12731{
12732 /* By definition, the inferior does not report stops at
12733 tracepoints. */
12734 return 0;
74960c60
VP
12735}
12736
12737static void
348d480f
PA
12738tracepoint_print_one_detail (const struct breakpoint *self,
12739 struct ui_out *uiout)
74960c60 12740{
d9b3f62e 12741 struct tracepoint *tp = (struct tracepoint *) self;
5d9310c4 12742 if (!tp->static_trace_marker_id.empty ())
348d480f
PA
12743 {
12744 gdb_assert (self->type == bp_static_tracepoint);
cc59ec59 12745
6a831f06
PA
12746 uiout->message ("\tmarker id is %pF\n",
12747 string_field ("static-tracepoint-marker-string-id",
12748 tp->static_trace_marker_id.c_str ()));
348d480f 12749 }
0d381245
VP
12750}
12751
a474d7c2 12752static void
348d480f 12753tracepoint_print_mention (struct breakpoint *b)
a474d7c2 12754{
112e8700 12755 if (current_uiout->is_mi_like_p ())
348d480f 12756 return;
cc59ec59 12757
348d480f
PA
12758 switch (b->type)
12759 {
12760 case bp_tracepoint:
12761 printf_filtered (_("Tracepoint"));
12762 printf_filtered (_(" %d"), b->number);
12763 break;
12764 case bp_fast_tracepoint:
12765 printf_filtered (_("Fast tracepoint"));
12766 printf_filtered (_(" %d"), b->number);
12767 break;
12768 case bp_static_tracepoint:
12769 printf_filtered (_("Static tracepoint"));
12770 printf_filtered (_(" %d"), b->number);
12771 break;
12772 default:
12773 internal_error (__FILE__, __LINE__,
12774 _("unhandled tracepoint type %d"), (int) b->type);
12775 }
12776
12777 say_where (b);
a474d7c2
PA
12778}
12779
348d480f 12780static void
d9b3f62e 12781tracepoint_print_recreate (struct breakpoint *self, struct ui_file *fp)
a474d7c2 12782{
d9b3f62e
PA
12783 struct tracepoint *tp = (struct tracepoint *) self;
12784
12785 if (self->type == bp_fast_tracepoint)
348d480f 12786 fprintf_unfiltered (fp, "ftrace");
c93e8391 12787 else if (self->type == bp_static_tracepoint)
348d480f 12788 fprintf_unfiltered (fp, "strace");
d9b3f62e 12789 else if (self->type == bp_tracepoint)
348d480f
PA
12790 fprintf_unfiltered (fp, "trace");
12791 else
12792 internal_error (__FILE__, __LINE__,
d9b3f62e 12793 _("unhandled tracepoint type %d"), (int) self->type);
cc59ec59 12794
f00aae0f 12795 fprintf_unfiltered (fp, " %s",
d28cd78a 12796 event_location_to_string (self->location.get ()));
d9b3f62e
PA
12797 print_recreate_thread (self, fp);
12798
12799 if (tp->pass_count)
12800 fprintf_unfiltered (fp, " passcount %d\n", tp->pass_count);
a474d7c2
PA
12801}
12802
983af33b 12803static void
f00aae0f
KS
12804tracepoint_create_sals_from_location (const struct event_location *location,
12805 struct linespec_result *canonical,
12806 enum bptype type_wanted)
983af33b 12807{
f00aae0f 12808 create_sals_from_location_default (location, canonical, type_wanted);
983af33b
SDJ
12809}
12810
12811static void
12812tracepoint_create_breakpoints_sal (struct gdbarch *gdbarch,
12813 struct linespec_result *canonical,
e1e01040
PA
12814 gdb::unique_xmalloc_ptr<char> cond_string,
12815 gdb::unique_xmalloc_ptr<char> extra_string,
983af33b
SDJ
12816 enum bptype type_wanted,
12817 enum bpdisp disposition,
12818 int thread,
12819 int task, int ignore_count,
12820 const struct breakpoint_ops *ops,
12821 int from_tty, int enabled,
44f238bb 12822 int internal, unsigned flags)
983af33b 12823{
023fa29b 12824 create_breakpoints_sal_default (gdbarch, canonical,
e1e01040
PA
12825 std::move (cond_string),
12826 std::move (extra_string),
e7e0cddf 12827 type_wanted,
983af33b
SDJ
12828 disposition, thread, task,
12829 ignore_count, ops, from_tty,
44f238bb 12830 enabled, internal, flags);
983af33b
SDJ
12831}
12832
6c5b2ebe 12833static std::vector<symtab_and_line>
f00aae0f
KS
12834tracepoint_decode_location (struct breakpoint *b,
12835 const struct event_location *location,
6c5b2ebe 12836 struct program_space *search_pspace)
983af33b 12837{
6c5b2ebe 12838 return decode_location_default (b, location, search_pspace);
983af33b
SDJ
12839}
12840
2060206e 12841struct breakpoint_ops tracepoint_breakpoint_ops;
348d480f 12842
bac7c5cf 12843/* Virtual table for tracepoints on static probes. */
55aa24fb
SDJ
12844
12845static void
f00aae0f
KS
12846tracepoint_probe_create_sals_from_location
12847 (const struct event_location *location,
12848 struct linespec_result *canonical,
12849 enum bptype type_wanted)
55aa24fb
SDJ
12850{
12851 /* We use the same method for breakpoint on probes. */
f00aae0f 12852 bkpt_probe_create_sals_from_location (location, canonical, type_wanted);
55aa24fb
SDJ
12853}
12854
6c5b2ebe 12855static std::vector<symtab_and_line>
f00aae0f
KS
12856tracepoint_probe_decode_location (struct breakpoint *b,
12857 const struct event_location *location,
6c5b2ebe 12858 struct program_space *search_pspace)
55aa24fb
SDJ
12859{
12860 /* We use the same method for breakpoint on probes. */
6c5b2ebe 12861 return bkpt_probe_decode_location (b, location, search_pspace);
55aa24fb
SDJ
12862}
12863
5c2b4418
HZ
12864/* Dprintf breakpoint_ops methods. */
12865
12866static void
12867dprintf_re_set (struct breakpoint *b)
12868{
12869 breakpoint_re_set_default (b);
12870
f00aae0f
KS
12871 /* extra_string should never be non-NULL for dprintf. */
12872 gdb_assert (b->extra_string != NULL);
5c2b4418
HZ
12873
12874 /* 1 - connect to target 1, that can run breakpoint commands.
12875 2 - create a dprintf, which resolves fine.
12876 3 - disconnect from target 1
12877 4 - connect to target 2, that can NOT run breakpoint commands.
12878
12879 After steps #3/#4, you'll want the dprintf command list to
12880 be updated, because target 1 and 2 may well return different
12881 answers for target_can_run_breakpoint_commands().
12882 Given absence of finer grained resetting, we get to do
12883 it all the time. */
12884 if (b->extra_string != NULL)
12885 update_dprintf_command_list (b);
12886}
12887
2d9442cc
HZ
12888/* Implement the "print_recreate" breakpoint_ops method for dprintf. */
12889
12890static void
12891dprintf_print_recreate (struct breakpoint *tp, struct ui_file *fp)
12892{
f00aae0f 12893 fprintf_unfiltered (fp, "dprintf %s,%s",
d28cd78a 12894 event_location_to_string (tp->location.get ()),
2d9442cc
HZ
12895 tp->extra_string);
12896 print_recreate_thread (tp, fp);
12897}
12898
9d6e6e84
HZ
12899/* Implement the "after_condition_true" breakpoint_ops method for
12900 dprintf.
12901
12902 dprintf's are implemented with regular commands in their command
12903 list, but we run the commands here instead of before presenting the
12904 stop to the user, as dprintf's don't actually cause a stop. This
12905 also makes it so that the commands of multiple dprintfs at the same
12906 address are all handled. */
12907
12908static void
12909dprintf_after_condition_true (struct bpstats *bs)
12910{
04afa70c 12911 struct bpstats tmp_bs;
9d6e6e84
HZ
12912 struct bpstats *tmp_bs_p = &tmp_bs;
12913
12914 /* dprintf's never cause a stop. This wasn't set in the
12915 check_status hook instead because that would make the dprintf's
12916 condition not be evaluated. */
12917 bs->stop = 0;
12918
12919 /* Run the command list here. Take ownership of it instead of
12920 copying. We never want these commands to run later in
12921 bpstat_do_actions, if a breakpoint that causes a stop happens to
12922 be set at same address as this dprintf, or even if running the
12923 commands here throws. */
12924 tmp_bs.commands = bs->commands;
12925 bs->commands = NULL;
9d6e6e84
HZ
12926
12927 bpstat_do_actions_1 (&tmp_bs_p);
12928
12929 /* 'tmp_bs.commands' will usually be NULL by now, but
12930 bpstat_do_actions_1 may return early without processing the whole
12931 list. */
9d6e6e84
HZ
12932}
12933
983af33b
SDJ
12934/* The breakpoint_ops structure to be used on static tracepoints with
12935 markers (`-m'). */
12936
12937static void
f00aae0f 12938strace_marker_create_sals_from_location (const struct event_location *location,
5f700d83 12939 struct linespec_result *canonical,
f00aae0f 12940 enum bptype type_wanted)
983af33b
SDJ
12941{
12942 struct linespec_sals lsal;
f00aae0f 12943 const char *arg_start, *arg;
983af33b 12944
a20714ff 12945 arg = arg_start = get_linespec_location (location)->spec_string;
f00aae0f 12946 lsal.sals = decode_static_tracepoint_spec (&arg);
983af33b 12947
f2fc3015
TT
12948 std::string str (arg_start, arg - arg_start);
12949 const char *ptr = str.c_str ();
a20714ff
PA
12950 canonical->location
12951 = new_linespec_location (&ptr, symbol_name_match_type::FULL);
983af33b 12952
8e9e35b1
TT
12953 lsal.canonical
12954 = xstrdup (event_location_to_string (canonical->location.get ()));
6c5b2ebe 12955 canonical->lsals.push_back (std::move (lsal));
983af33b
SDJ
12956}
12957
12958static void
12959strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
12960 struct linespec_result *canonical,
e1e01040
PA
12961 gdb::unique_xmalloc_ptr<char> cond_string,
12962 gdb::unique_xmalloc_ptr<char> extra_string,
983af33b
SDJ
12963 enum bptype type_wanted,
12964 enum bpdisp disposition,
12965 int thread,
12966 int task, int ignore_count,
12967 const struct breakpoint_ops *ops,
12968 int from_tty, int enabled,
44f238bb 12969 int internal, unsigned flags)
983af33b 12970{
6c5b2ebe 12971 const linespec_sals &lsal = canonical->lsals[0];
983af33b
SDJ
12972
12973 /* If the user is creating a static tracepoint by marker id
12974 (strace -m MARKER_ID), then store the sals index, so that
12975 breakpoint_re_set can try to match up which of the newly
12976 found markers corresponds to this one, and, don't try to
12977 expand multiple locations for each sal, given than SALS
12978 already should contain all sals for MARKER_ID. */
12979
6c5b2ebe 12980 for (size_t i = 0; i < lsal.sals.size (); i++)
983af33b 12981 {
6c5b2ebe
PA
12982 event_location_up location
12983 = copy_event_location (canonical->location.get ());
983af33b 12984
b270e6f9 12985 std::unique_ptr<tracepoint> tp (new tracepoint ());
6c5b2ebe 12986 init_breakpoint_sal (tp.get (), gdbarch, lsal.sals[i],
ffc2605c 12987 std::move (location), NULL,
e1e01040
PA
12988 std::move (cond_string),
12989 std::move (extra_string),
e7e0cddf 12990 type_wanted, disposition,
983af33b 12991 thread, task, ignore_count, ops,
44f238bb 12992 from_tty, enabled, internal, flags,
983af33b
SDJ
12993 canonical->special_display);
12994 /* Given that its possible to have multiple markers with
12995 the same string id, if the user is creating a static
12996 tracepoint by marker id ("strace -m MARKER_ID"), then
12997 store the sals index, so that breakpoint_re_set can
12998 try to match up which of the newly found markers
12999 corresponds to this one */
13000 tp->static_trace_marker_id_idx = i;
13001
b270e6f9 13002 install_breakpoint (internal, std::move (tp), 0);
983af33b
SDJ
13003 }
13004}
13005
6c5b2ebe 13006static std::vector<symtab_and_line>
f00aae0f
KS
13007strace_marker_decode_location (struct breakpoint *b,
13008 const struct event_location *location,
6c5b2ebe 13009 struct program_space *search_pspace)
983af33b
SDJ
13010{
13011 struct tracepoint *tp = (struct tracepoint *) b;
a20714ff 13012 const char *s = get_linespec_location (location)->spec_string;
983af33b 13013
6c5b2ebe
PA
13014 std::vector<symtab_and_line> sals = decode_static_tracepoint_spec (&s);
13015 if (sals.size () > tp->static_trace_marker_id_idx)
983af33b 13016 {
6c5b2ebe
PA
13017 sals[0] = sals[tp->static_trace_marker_id_idx];
13018 sals.resize (1);
13019 return sals;
983af33b
SDJ
13020 }
13021 else
5d9310c4 13022 error (_("marker %s not found"), tp->static_trace_marker_id.c_str ());
983af33b
SDJ
13023}
13024
13025static struct breakpoint_ops strace_marker_breakpoint_ops;
13026
13027static int
13028strace_marker_p (struct breakpoint *b)
13029{
13030 return b->ops == &strace_marker_breakpoint_ops;
13031}
13032
53a5351d 13033/* Delete a breakpoint and clean up all traces of it in the data
f431efe5 13034 structures. */
c906108c
SS
13035
13036void
fba45db2 13037delete_breakpoint (struct breakpoint *bpt)
c906108c 13038{
52f0bd74 13039 struct breakpoint *b;
c906108c 13040
8a3fe4f8 13041 gdb_assert (bpt != NULL);
c906108c 13042
4a64f543
MS
13043 /* Has this bp already been deleted? This can happen because
13044 multiple lists can hold pointers to bp's. bpstat lists are
13045 especial culprits.
13046
13047 One example of this happening is a watchpoint's scope bp. When
13048 the scope bp triggers, we notice that the watchpoint is out of
13049 scope, and delete it. We also delete its scope bp. But the
13050 scope bp is marked "auto-deleting", and is already on a bpstat.
13051 That bpstat is then checked for auto-deleting bp's, which are
13052 deleted.
13053
13054 A real solution to this problem might involve reference counts in
13055 bp's, and/or giving them pointers back to their referencing
13056 bpstat's, and teaching delete_breakpoint to only free a bp's
13057 storage when no more references were extent. A cheaper bandaid
13058 was chosen. */
c906108c
SS
13059 if (bpt->type == bp_none)
13060 return;
13061
4a64f543
MS
13062 /* At least avoid this stale reference until the reference counting
13063 of breakpoints gets resolved. */
d0fb5eae 13064 if (bpt->related_breakpoint != bpt)
e5a0a904 13065 {
d0fb5eae 13066 struct breakpoint *related;
3a5c3e22 13067 struct watchpoint *w;
d0fb5eae
JK
13068
13069 if (bpt->type == bp_watchpoint_scope)
3a5c3e22 13070 w = (struct watchpoint *) bpt->related_breakpoint;
d0fb5eae 13071 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
3a5c3e22
PA
13072 w = (struct watchpoint *) bpt;
13073 else
13074 w = NULL;
13075 if (w != NULL)
13076 watchpoint_del_at_next_stop (w);
d0fb5eae
JK
13077
13078 /* Unlink bpt from the bpt->related_breakpoint ring. */
13079 for (related = bpt; related->related_breakpoint != bpt;
13080 related = related->related_breakpoint);
13081 related->related_breakpoint = bpt->related_breakpoint;
13082 bpt->related_breakpoint = bpt;
e5a0a904
JK
13083 }
13084
a9634178
TJB
13085 /* watch_command_1 creates a watchpoint but only sets its number if
13086 update_watchpoint succeeds in creating its bp_locations. If there's
13087 a problem in that process, we'll be asked to delete the half-created
13088 watchpoint. In that case, don't announce the deletion. */
13089 if (bpt->number)
76727919 13090 gdb::observers::breakpoint_deleted.notify (bpt);
c906108c 13091
c906108c
SS
13092 if (breakpoint_chain == bpt)
13093 breakpoint_chain = bpt->next;
13094
c906108c
SS
13095 ALL_BREAKPOINTS (b)
13096 if (b->next == bpt)
c5aa993b
JM
13097 {
13098 b->next = bpt->next;
13099 break;
13100 }
c906108c 13101
f431efe5
PA
13102 /* Be sure no bpstat's are pointing at the breakpoint after it's
13103 been freed. */
13104 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
e5dd4106 13105 in all threads for now. Note that we cannot just remove bpstats
f431efe5
PA
13106 pointing at bpt from the stop_bpstat list entirely, as breakpoint
13107 commands are associated with the bpstat; if we remove it here,
13108 then the later call to bpstat_do_actions (&stop_bpstat); in
13109 event-top.c won't do anything, and temporary breakpoints with
13110 commands won't work. */
13111
13112 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
13113
4a64f543
MS
13114 /* Now that breakpoint is removed from breakpoint list, update the
13115 global location list. This will remove locations that used to
13116 belong to this breakpoint. Do this before freeing the breakpoint
13117 itself, since remove_breakpoint looks at location's owner. It
13118 might be better design to have location completely
13119 self-contained, but it's not the case now. */
44702360 13120 update_global_location_list (UGLL_DONT_INSERT);
74960c60 13121
4a64f543
MS
13122 /* On the chance that someone will soon try again to delete this
13123 same bp, we mark it as deleted before freeing its storage. */
c906108c 13124 bpt->type = bp_none;
4d01a485 13125 delete bpt;
c906108c
SS
13126}
13127
51be5b68
PA
13128/* Iterator function to call a user-provided callback function once
13129 for each of B and its related breakpoints. */
13130
13131static void
13132iterate_over_related_breakpoints (struct breakpoint *b,
48649e1b 13133 gdb::function_view<void (breakpoint *)> function)
51be5b68
PA
13134{
13135 struct breakpoint *related;
13136
13137 related = b;
13138 do
13139 {
13140 struct breakpoint *next;
13141
13142 /* FUNCTION may delete RELATED. */
13143 next = related->related_breakpoint;
13144
13145 if (next == related)
13146 {
13147 /* RELATED is the last ring entry. */
48649e1b 13148 function (related);
51be5b68
PA
13149
13150 /* FUNCTION may have deleted it, so we'd never reach back to
13151 B. There's nothing left to do anyway, so just break
13152 out. */
13153 break;
13154 }
13155 else
48649e1b 13156 function (related);
51be5b68
PA
13157
13158 related = next;
13159 }
13160 while (related != b);
13161}
95a42b64 13162
4495129a 13163static void
981a3fb3 13164delete_command (const char *arg, int from_tty)
c906108c 13165{
35df4500 13166 struct breakpoint *b, *b_tmp;
c906108c 13167
ea9365bb
TT
13168 dont_repeat ();
13169
c906108c
SS
13170 if (arg == 0)
13171 {
13172 int breaks_to_delete = 0;
13173
46c6471b
PA
13174 /* Delete all breakpoints if no argument. Do not delete
13175 internal breakpoints, these have to be deleted with an
13176 explicit breakpoint number argument. */
c5aa993b 13177 ALL_BREAKPOINTS (b)
46c6471b 13178 if (user_breakpoint_p (b))
973d738b
DJ
13179 {
13180 breaks_to_delete = 1;
13181 break;
13182 }
c906108c
SS
13183
13184 /* Ask user only if there are some breakpoints to delete. */
13185 if (!from_tty
e2e0b3e5 13186 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
c906108c 13187 {
35df4500 13188 ALL_BREAKPOINTS_SAFE (b, b_tmp)
46c6471b 13189 if (user_breakpoint_p (b))
c5aa993b 13190 delete_breakpoint (b);
c906108c
SS
13191 }
13192 }
13193 else
48649e1b 13194 map_breakpoint_numbers
b926417a 13195 (arg, [&] (breakpoint *br)
48649e1b 13196 {
b926417a 13197 iterate_over_related_breakpoints (br, delete_breakpoint);
48649e1b 13198 });
c906108c
SS
13199}
13200
c2f4122d
PA
13201/* Return true if all locations of B bound to PSPACE are pending. If
13202 PSPACE is NULL, all locations of all program spaces are
13203 considered. */
13204
0d381245 13205static int
c2f4122d 13206all_locations_are_pending (struct breakpoint *b, struct program_space *pspace)
fe3f5fa8 13207{
c2f4122d
PA
13208 struct bp_location *loc;
13209
13210 for (loc = b->loc; loc != NULL; loc = loc->next)
13211 if ((pspace == NULL
13212 || loc->pspace == pspace)
13213 && !loc->shlib_disabled
8645ff69 13214 && !loc->pspace->executing_startup)
0d381245
VP
13215 return 0;
13216 return 1;
fe3f5fa8
VP
13217}
13218
776592bf
DE
13219/* Subroutine of update_breakpoint_locations to simplify it.
13220 Return non-zero if multiple fns in list LOC have the same name.
13221 Null names are ignored. */
13222
13223static int
13224ambiguous_names_p (struct bp_location *loc)
13225{
13226 struct bp_location *l;
459a2e4c
TT
13227 htab_t htab = htab_create_alloc (13, htab_hash_string, streq_hash, NULL,
13228 xcalloc, xfree);
776592bf
DE
13229
13230 for (l = loc; l != NULL; l = l->next)
13231 {
13232 const char **slot;
13233 const char *name = l->function_name;
13234
13235 /* Allow for some names to be NULL, ignore them. */
13236 if (name == NULL)
13237 continue;
13238
13239 slot = (const char **) htab_find_slot (htab, (const void *) name,
13240 INSERT);
4a64f543
MS
13241 /* NOTE: We can assume slot != NULL here because xcalloc never
13242 returns NULL. */
776592bf
DE
13243 if (*slot != NULL)
13244 {
13245 htab_delete (htab);
13246 return 1;
13247 }
13248 *slot = name;
13249 }
13250
13251 htab_delete (htab);
13252 return 0;
13253}
13254
0fb4aa4b
PA
13255/* When symbols change, it probably means the sources changed as well,
13256 and it might mean the static tracepoint markers are no longer at
13257 the same address or line numbers they used to be at last we
13258 checked. Losing your static tracepoints whenever you rebuild is
13259 undesirable. This function tries to resync/rematch gdb static
13260 tracepoints with the markers on the target, for static tracepoints
13261 that have not been set by marker id. Static tracepoint that have
13262 been set by marker id are reset by marker id in breakpoint_re_set.
13263 The heuristic is:
13264
13265 1) For a tracepoint set at a specific address, look for a marker at
13266 the old PC. If one is found there, assume to be the same marker.
13267 If the name / string id of the marker found is different from the
13268 previous known name, assume that means the user renamed the marker
13269 in the sources, and output a warning.
13270
13271 2) For a tracepoint set at a given line number, look for a marker
13272 at the new address of the old line number. If one is found there,
13273 assume to be the same marker. If the name / string id of the
13274 marker found is different from the previous known name, assume that
13275 means the user renamed the marker in the sources, and output a
13276 warning.
13277
13278 3) If a marker is no longer found at the same address or line, it
13279 may mean the marker no longer exists. But it may also just mean
13280 the code changed a bit. Maybe the user added a few lines of code
13281 that made the marker move up or down (in line number terms). Ask
13282 the target for info about the marker with the string id as we knew
13283 it. If found, update line number and address in the matching
13284 static tracepoint. This will get confused if there's more than one
13285 marker with the same ID (possible in UST, although unadvised
13286 precisely because it confuses tools). */
13287
13288static struct symtab_and_line
13289update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
13290{
d9b3f62e 13291 struct tracepoint *tp = (struct tracepoint *) b;
0fb4aa4b
PA
13292 struct static_tracepoint_marker marker;
13293 CORE_ADDR pc;
0fb4aa4b
PA
13294
13295 pc = sal.pc;
13296 if (sal.line)
13297 find_line_pc (sal.symtab, sal.line, &pc);
13298
13299 if (target_static_tracepoint_marker_at (pc, &marker))
13300 {
5d9310c4 13301 if (tp->static_trace_marker_id != marker.str_id)
0fb4aa4b 13302 warning (_("static tracepoint %d changed probed marker from %s to %s"),
5d9310c4
SM
13303 b->number, tp->static_trace_marker_id.c_str (),
13304 marker.str_id.c_str ());
0fb4aa4b 13305
5d9310c4 13306 tp->static_trace_marker_id = std::move (marker.str_id);
0fb4aa4b
PA
13307
13308 return sal;
13309 }
13310
13311 /* Old marker wasn't found on target at lineno. Try looking it up
13312 by string ID. */
13313 if (!sal.explicit_pc
13314 && sal.line != 0
13315 && sal.symtab != NULL
5d9310c4 13316 && !tp->static_trace_marker_id.empty ())
0fb4aa4b 13317 {
5d9310c4
SM
13318 std::vector<static_tracepoint_marker> markers
13319 = target_static_tracepoint_markers_by_strid
13320 (tp->static_trace_marker_id.c_str ());
0fb4aa4b 13321
5d9310c4 13322 if (!markers.empty ())
0fb4aa4b 13323 {
0fb4aa4b 13324 struct symbol *sym;
80e1d417 13325 struct static_tracepoint_marker *tpmarker;
79a45e25 13326 struct ui_out *uiout = current_uiout;
67994074 13327 struct explicit_location explicit_loc;
0fb4aa4b 13328
5d9310c4 13329 tpmarker = &markers[0];
0fb4aa4b 13330
5d9310c4 13331 tp->static_trace_marker_id = std::move (tpmarker->str_id);
0fb4aa4b
PA
13332
13333 warning (_("marker for static tracepoint %d (%s) not "
13334 "found at previous line number"),
5d9310c4 13335 b->number, tp->static_trace_marker_id.c_str ());
0fb4aa4b 13336
51abb421 13337 symtab_and_line sal2 = find_pc_line (tpmarker->address, 0);
80e1d417 13338 sym = find_pc_sect_function (tpmarker->address, NULL);
112e8700 13339 uiout->text ("Now in ");
0fb4aa4b
PA
13340 if (sym)
13341 {
987012b8 13342 uiout->field_string ("func", sym->print_name (),
e43b10e1 13343 function_name_style.style ());
112e8700 13344 uiout->text (" at ");
0fb4aa4b 13345 }
112e8700 13346 uiout->field_string ("file",
cbe56571 13347 symtab_to_filename_for_display (sal2.symtab),
e43b10e1 13348 file_name_style.style ());
112e8700 13349 uiout->text (":");
0fb4aa4b 13350
112e8700 13351 if (uiout->is_mi_like_p ())
0fb4aa4b 13352 {
0b0865da 13353 const char *fullname = symtab_to_fullname (sal2.symtab);
0fb4aa4b 13354
112e8700 13355 uiout->field_string ("fullname", fullname);
0fb4aa4b
PA
13356 }
13357
381befee 13358 uiout->field_signed ("line", sal2.line);
112e8700 13359 uiout->text ("\n");
0fb4aa4b 13360
80e1d417 13361 b->loc->line_number = sal2.line;
2f202fde 13362 b->loc->symtab = sym != NULL ? sal2.symtab : NULL;
0fb4aa4b 13363
d28cd78a 13364 b->location.reset (NULL);
67994074
KS
13365 initialize_explicit_location (&explicit_loc);
13366 explicit_loc.source_filename
00e52e53 13367 = ASTRDUP (symtab_to_filename_for_display (sal2.symtab));
67994074
KS
13368 explicit_loc.line_offset.offset = b->loc->line_number;
13369 explicit_loc.line_offset.sign = LINE_OFFSET_NONE;
d28cd78a 13370 b->location = new_explicit_location (&explicit_loc);
0fb4aa4b
PA
13371
13372 /* Might be nice to check if function changed, and warn if
13373 so. */
0fb4aa4b
PA
13374 }
13375 }
13376 return sal;
13377}
13378
8d3788bd
VP
13379/* Returns 1 iff locations A and B are sufficiently same that
13380 we don't need to report breakpoint as changed. */
13381
13382static int
13383locations_are_equal (struct bp_location *a, struct bp_location *b)
13384{
13385 while (a && b)
13386 {
13387 if (a->address != b->address)
13388 return 0;
13389
13390 if (a->shlib_disabled != b->shlib_disabled)
13391 return 0;
13392
13393 if (a->enabled != b->enabled)
13394 return 0;
13395
13396 a = a->next;
13397 b = b->next;
13398 }
13399
13400 if ((a == NULL) != (b == NULL))
13401 return 0;
13402
13403 return 1;
13404}
13405
c2f4122d
PA
13406/* Split all locations of B that are bound to PSPACE out of B's
13407 location list to a separate list and return that list's head. If
13408 PSPACE is NULL, hoist out all locations of B. */
13409
13410static struct bp_location *
13411hoist_existing_locations (struct breakpoint *b, struct program_space *pspace)
13412{
13413 struct bp_location head;
13414 struct bp_location *i = b->loc;
13415 struct bp_location **i_link = &b->loc;
13416 struct bp_location *hoisted = &head;
13417
13418 if (pspace == NULL)
13419 {
13420 i = b->loc;
13421 b->loc = NULL;
13422 return i;
13423 }
13424
13425 head.next = NULL;
13426
13427 while (i != NULL)
13428 {
13429 if (i->pspace == pspace)
13430 {
13431 *i_link = i->next;
13432 i->next = NULL;
13433 hoisted->next = i;
13434 hoisted = i;
13435 }
13436 else
13437 i_link = &i->next;
13438 i = *i_link;
13439 }
13440
13441 return head.next;
13442}
13443
13444/* Create new breakpoint locations for B (a hardware or software
13445 breakpoint) based on SALS and SALS_END. If SALS_END.NELTS is not
13446 zero, then B is a ranged breakpoint. Only recreates locations for
13447 FILTER_PSPACE. Locations of other program spaces are left
13448 untouched. */
f1310107 13449
0e30163f 13450void
0d381245 13451update_breakpoint_locations (struct breakpoint *b,
c2f4122d 13452 struct program_space *filter_pspace,
6c5b2ebe
PA
13453 gdb::array_view<const symtab_and_line> sals,
13454 gdb::array_view<const symtab_and_line> sals_end)
fe3f5fa8 13455{
c2f4122d 13456 struct bp_location *existing_locations;
0d381245 13457
6c5b2ebe 13458 if (!sals_end.empty () && (sals.size () != 1 || sals_end.size () != 1))
f8eba3c6
TT
13459 {
13460 /* Ranged breakpoints have only one start location and one end
13461 location. */
13462 b->enable_state = bp_disabled;
f8eba3c6
TT
13463 printf_unfiltered (_("Could not reset ranged breakpoint %d: "
13464 "multiple locations found\n"),
13465 b->number);
13466 return;
13467 }
f1310107 13468
4a64f543
MS
13469 /* If there's no new locations, and all existing locations are
13470 pending, don't do anything. This optimizes the common case where
13471 all locations are in the same shared library, that was unloaded.
13472 We'd like to retain the location, so that when the library is
13473 loaded again, we don't loose the enabled/disabled status of the
13474 individual locations. */
6c5b2ebe 13475 if (all_locations_are_pending (b, filter_pspace) && sals.empty ())
fe3f5fa8
VP
13476 return;
13477
c2f4122d 13478 existing_locations = hoist_existing_locations (b, filter_pspace);
fe3f5fa8 13479
6c5b2ebe 13480 for (const auto &sal : sals)
fe3f5fa8 13481 {
f8eba3c6
TT
13482 struct bp_location *new_loc;
13483
6c5b2ebe 13484 switch_to_program_space_and_thread (sal.pspace);
f8eba3c6 13485
6c5b2ebe 13486 new_loc = add_location_to_breakpoint (b, &sal);
fe3f5fa8 13487
0d381245
VP
13488 /* Reparse conditions, they might contain references to the
13489 old symtab. */
13490 if (b->cond_string != NULL)
13491 {
bbc13ae3 13492 const char *s;
fe3f5fa8 13493
0d381245 13494 s = b->cond_string;
a70b8144 13495 try
0d381245 13496 {
6c5b2ebe
PA
13497 new_loc->cond = parse_exp_1 (&s, sal.pc,
13498 block_for_pc (sal.pc),
0d381245
VP
13499 0);
13500 }
230d2906 13501 catch (const gdb_exception_error &e)
0d381245 13502 {
3e43a32a
MS
13503 warning (_("failed to reevaluate condition "
13504 "for breakpoint %d: %s"),
3d6e9d23 13505 b->number, e.what ());
0d381245
VP
13506 new_loc->enabled = 0;
13507 }
13508 }
fe3f5fa8 13509
6c5b2ebe 13510 if (!sals_end.empty ())
f1310107 13511 {
6c5b2ebe 13512 CORE_ADDR end = find_breakpoint_range_end (sals_end[0]);
f1310107 13513
6c5b2ebe 13514 new_loc->length = end - sals[0].pc + 1;
f1310107 13515 }
0d381245 13516 }
fe3f5fa8 13517
4a64f543
MS
13518 /* If possible, carry over 'disable' status from existing
13519 breakpoints. */
0d381245
VP
13520 {
13521 struct bp_location *e = existing_locations;
776592bf
DE
13522 /* If there are multiple breakpoints with the same function name,
13523 e.g. for inline functions, comparing function names won't work.
13524 Instead compare pc addresses; this is just a heuristic as things
13525 may have moved, but in practice it gives the correct answer
13526 often enough until a better solution is found. */
13527 int have_ambiguous_names = ambiguous_names_p (b->loc);
13528
0d381245
VP
13529 for (; e; e = e->next)
13530 {
13531 if (!e->enabled && e->function_name)
13532 {
13533 struct bp_location *l = b->loc;
776592bf
DE
13534 if (have_ambiguous_names)
13535 {
13536 for (; l; l = l->next)
f1310107 13537 if (breakpoint_locations_match (e, l))
776592bf
DE
13538 {
13539 l->enabled = 0;
13540 break;
13541 }
13542 }
13543 else
13544 {
13545 for (; l; l = l->next)
13546 if (l->function_name
13547 && strcmp (e->function_name, l->function_name) == 0)
13548 {
13549 l->enabled = 0;
13550 break;
13551 }
13552 }
0d381245
VP
13553 }
13554 }
13555 }
fe3f5fa8 13556
8d3788bd 13557 if (!locations_are_equal (existing_locations, b->loc))
76727919 13558 gdb::observers::breakpoint_modified.notify (b);
fe3f5fa8
VP
13559}
13560
f00aae0f 13561/* Find the SaL locations corresponding to the given LOCATION.
ef23e705
TJB
13562 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
13563
6c5b2ebe 13564static std::vector<symtab_and_line>
f00aae0f 13565location_to_sals (struct breakpoint *b, struct event_location *location,
c2f4122d 13566 struct program_space *search_pspace, int *found)
ef23e705 13567{
cc06b668 13568 struct gdb_exception exception;
ef23e705 13569
983af33b 13570 gdb_assert (b->ops != NULL);
ef23e705 13571
6c5b2ebe
PA
13572 std::vector<symtab_and_line> sals;
13573
a70b8144 13574 try
ef23e705 13575 {
6c5b2ebe 13576 sals = b->ops->decode_location (b, location, search_pspace);
ef23e705 13577 }
94aeb44b 13578 catch (gdb_exception_error &e)
ef23e705
TJB
13579 {
13580 int not_found_and_ok = 0;
492d29ea 13581
ef23e705
TJB
13582 /* For pending breakpoints, it's expected that parsing will
13583 fail until the right shared library is loaded. User has
13584 already told to create pending breakpoints and don't need
13585 extra messages. If breakpoint is in bp_shlib_disabled
13586 state, then user already saw the message about that
13587 breakpoint being disabled, and don't want to see more
13588 errors. */
58438ac1 13589 if (e.error == NOT_FOUND_ERROR
c2f4122d
PA
13590 && (b->condition_not_parsed
13591 || (b->loc != NULL
13592 && search_pspace != NULL
13593 && b->loc->pspace != search_pspace)
ef23e705 13594 || (b->loc && b->loc->shlib_disabled)
f8eba3c6 13595 || (b->loc && b->loc->pspace->executing_startup)
ef23e705
TJB
13596 || b->enable_state == bp_disabled))
13597 not_found_and_ok = 1;
13598
13599 if (!not_found_and_ok)
13600 {
13601 /* We surely don't want to warn about the same breakpoint
13602 10 times. One solution, implemented here, is disable
13603 the breakpoint on error. Another solution would be to
13604 have separate 'warning emitted' flag. Since this
13605 happens only when a binary has changed, I don't know
13606 which approach is better. */
13607 b->enable_state = bp_disabled;
eedc3f4f 13608 throw;
ef23e705 13609 }
94aeb44b
TT
13610
13611 exception = std::move (e);
ef23e705
TJB
13612 }
13613
492d29ea 13614 if (exception.reason == 0 || exception.error != NOT_FOUND_ERROR)
ef23e705 13615 {
6c5b2ebe
PA
13616 for (auto &sal : sals)
13617 resolve_sal_pc (&sal);
f00aae0f 13618 if (b->condition_not_parsed && b->extra_string != NULL)
ef23e705 13619 {
ed1d1739
KS
13620 char *cond_string, *extra_string;
13621 int thread, task;
ef23e705 13622
6c5b2ebe 13623 find_condition_and_thread (b->extra_string, sals[0].pc,
e7e0cddf
SS
13624 &cond_string, &thread, &task,
13625 &extra_string);
f00aae0f 13626 gdb_assert (b->cond_string == NULL);
ef23e705
TJB
13627 if (cond_string)
13628 b->cond_string = cond_string;
13629 b->thread = thread;
13630 b->task = task;
e7e0cddf 13631 if (extra_string)
f00aae0f
KS
13632 {
13633 xfree (b->extra_string);
13634 b->extra_string = extra_string;
13635 }
ef23e705
TJB
13636 b->condition_not_parsed = 0;
13637 }
13638
983af33b 13639 if (b->type == bp_static_tracepoint && !strace_marker_p (b))
6c5b2ebe 13640 sals[0] = update_static_tracepoint (b, sals[0]);
ef23e705 13641
58438ac1
TT
13642 *found = 1;
13643 }
13644 else
13645 *found = 0;
ef23e705
TJB
13646
13647 return sals;
13648}
13649
348d480f
PA
13650/* The default re_set method, for typical hardware or software
13651 breakpoints. Reevaluate the breakpoint and recreate its
13652 locations. */
13653
13654static void
28010a5d 13655breakpoint_re_set_default (struct breakpoint *b)
ef23e705 13656{
c2f4122d 13657 struct program_space *filter_pspace = current_program_space;
6c5b2ebe 13658 std::vector<symtab_and_line> expanded, expanded_end;
ef23e705 13659
6c5b2ebe
PA
13660 int found;
13661 std::vector<symtab_and_line> sals = location_to_sals (b, b->location.get (),
13662 filter_pspace, &found);
ef23e705 13663 if (found)
6c5b2ebe 13664 expanded = std::move (sals);
ef23e705 13665
f00aae0f 13666 if (b->location_range_end != NULL)
f1310107 13667 {
6c5b2ebe
PA
13668 std::vector<symtab_and_line> sals_end
13669 = location_to_sals (b, b->location_range_end.get (),
13670 filter_pspace, &found);
f1310107 13671 if (found)
6c5b2ebe 13672 expanded_end = std::move (sals_end);
f1310107
TJB
13673 }
13674
c2f4122d 13675 update_breakpoint_locations (b, filter_pspace, expanded, expanded_end);
28010a5d
PA
13676}
13677
983af33b
SDJ
13678/* Default method for creating SALs from an address string. It basically
13679 calls parse_breakpoint_sals. Return 1 for success, zero for failure. */
13680
13681static void
f00aae0f
KS
13682create_sals_from_location_default (const struct event_location *location,
13683 struct linespec_result *canonical,
13684 enum bptype type_wanted)
983af33b 13685{
f00aae0f 13686 parse_breakpoint_sals (location, canonical);
983af33b
SDJ
13687}
13688
13689/* Call create_breakpoints_sal for the given arguments. This is the default
13690 function for the `create_breakpoints_sal' method of
13691 breakpoint_ops. */
13692
13693static void
13694create_breakpoints_sal_default (struct gdbarch *gdbarch,
13695 struct linespec_result *canonical,
e1e01040
PA
13696 gdb::unique_xmalloc_ptr<char> cond_string,
13697 gdb::unique_xmalloc_ptr<char> extra_string,
983af33b
SDJ
13698 enum bptype type_wanted,
13699 enum bpdisp disposition,
13700 int thread,
13701 int task, int ignore_count,
13702 const struct breakpoint_ops *ops,
13703 int from_tty, int enabled,
44f238bb 13704 int internal, unsigned flags)
983af33b 13705{
e1e01040
PA
13706 create_breakpoints_sal (gdbarch, canonical,
13707 std::move (cond_string),
13708 std::move (extra_string),
983af33b
SDJ
13709 type_wanted, disposition,
13710 thread, task, ignore_count, ops, from_tty,
44f238bb 13711 enabled, internal, flags);
983af33b
SDJ
13712}
13713
13714/* Decode the line represented by S by calling decode_line_full. This is the
5f700d83 13715 default function for the `decode_location' method of breakpoint_ops. */
983af33b 13716
6c5b2ebe 13717static std::vector<symtab_and_line>
f00aae0f
KS
13718decode_location_default (struct breakpoint *b,
13719 const struct event_location *location,
6c5b2ebe 13720 struct program_space *search_pspace)
983af33b
SDJ
13721{
13722 struct linespec_result canonical;
13723
c2f4122d 13724 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, search_pspace,
cafb3438 13725 NULL, 0, &canonical, multiple_symbols_all,
c0e8dcd8 13726 b->filter.get ());
983af33b
SDJ
13727
13728 /* We should get 0 or 1 resulting SALs. */
6c5b2ebe 13729 gdb_assert (canonical.lsals.size () < 2);
983af33b 13730
6c5b2ebe 13731 if (!canonical.lsals.empty ())
983af33b 13732 {
6c5b2ebe
PA
13733 const linespec_sals &lsal = canonical.lsals[0];
13734 return std::move (lsal.sals);
983af33b 13735 }
6c5b2ebe 13736 return {};
983af33b
SDJ
13737}
13738
bf469271 13739/* Reset a breakpoint. */
c906108c 13740
bf469271
PA
13741static void
13742breakpoint_re_set_one (breakpoint *b)
c906108c 13743{
fdf44873
TT
13744 input_radix = b->input_radix;
13745 set_language (b->language);
c906108c 13746
348d480f 13747 b->ops->re_set (b);
c906108c
SS
13748}
13749
c2f4122d
PA
13750/* Re-set breakpoint locations for the current program space.
13751 Locations bound to other program spaces are left untouched. */
13752
c906108c 13753void
69de3c6a 13754breakpoint_re_set (void)
c906108c 13755{
35df4500 13756 struct breakpoint *b, *b_tmp;
2a7f3dff 13757
c5aa993b 13758 {
fdf44873
TT
13759 scoped_restore_current_language save_language;
13760 scoped_restore save_input_radix = make_scoped_restore (&input_radix);
5ed8105e 13761 scoped_restore_current_pspace_and_thread restore_pspace_thread;
e62c965a 13762
8e817061
JB
13763 /* breakpoint_re_set_one sets the current_language to the language
13764 of the breakpoint it is resetting (see prepare_re_set_context)
13765 before re-evaluating the breakpoint's location. This change can
13766 unfortunately get undone by accident if the language_mode is set
13767 to auto, and we either switch frames, or more likely in this context,
13768 we select the current frame.
13769
13770 We prevent this by temporarily turning the language_mode to
13771 language_mode_manual. We restore it once all breakpoints
13772 have been reset. */
13773 scoped_restore save_language_mode = make_scoped_restore (&language_mode);
13774 language_mode = language_mode_manual;
13775
5ed8105e
PA
13776 /* Note: we must not try to insert locations until after all
13777 breakpoints have been re-set. Otherwise, e.g., when re-setting
13778 breakpoint 1, we'd insert the locations of breakpoint 2, which
13779 hadn't been re-set yet, and thus may have stale locations. */
4efc6507 13780
5ed8105e
PA
13781 ALL_BREAKPOINTS_SAFE (b, b_tmp)
13782 {
a70b8144 13783 try
bf469271
PA
13784 {
13785 breakpoint_re_set_one (b);
13786 }
230d2906 13787 catch (const gdb_exception &ex)
bf469271
PA
13788 {
13789 exception_fprintf (gdb_stderr, ex,
13790 "Error in re-setting breakpoint %d: ",
13791 b->number);
13792 }
5ed8105e 13793 }
5ed8105e
PA
13794
13795 jit_breakpoint_re_set ();
13796 }
6c95b8df 13797
af02033e
PP
13798 create_overlay_event_breakpoint ();
13799 create_longjmp_master_breakpoint ();
13800 create_std_terminate_master_breakpoint ();
186c406b 13801 create_exception_master_breakpoint ();
2a7f3dff
PA
13802
13803 /* Now we can insert. */
13804 update_global_location_list (UGLL_MAY_INSERT);
c906108c
SS
13805}
13806\f
c906108c
SS
13807/* Reset the thread number of this breakpoint:
13808
13809 - If the breakpoint is for all threads, leave it as-is.
4a64f543 13810 - Else, reset it to the current thread for inferior_ptid. */
c906108c 13811void
fba45db2 13812breakpoint_re_set_thread (struct breakpoint *b)
c906108c
SS
13813{
13814 if (b->thread != -1)
13815 {
00431a78 13816 b->thread = inferior_thread ()->global_num;
6c95b8df
PA
13817
13818 /* We're being called after following a fork. The new fork is
13819 selected as current, and unless this was a vfork will have a
13820 different program space from the original thread. Reset that
13821 as well. */
13822 b->loc->pspace = current_program_space;
c906108c
SS
13823 }
13824}
13825
03ac34d5
MS
13826/* Set ignore-count of breakpoint number BPTNUM to COUNT.
13827 If from_tty is nonzero, it prints a message to that effect,
13828 which ends with a period (no newline). */
13829
c906108c 13830void
fba45db2 13831set_ignore_count (int bptnum, int count, int from_tty)
c906108c 13832{
52f0bd74 13833 struct breakpoint *b;
c906108c
SS
13834
13835 if (count < 0)
13836 count = 0;
13837
13838 ALL_BREAKPOINTS (b)
13839 if (b->number == bptnum)
c5aa993b 13840 {
d77f58be
SS
13841 if (is_tracepoint (b))
13842 {
13843 if (from_tty && count != 0)
13844 printf_filtered (_("Ignore count ignored for tracepoint %d."),
13845 bptnum);
13846 return;
13847 }
13848
c5aa993b 13849 b->ignore_count = count;
221ea385
KS
13850 if (from_tty)
13851 {
13852 if (count == 0)
3e43a32a
MS
13853 printf_filtered (_("Will stop next time "
13854 "breakpoint %d is reached."),
221ea385
KS
13855 bptnum);
13856 else if (count == 1)
a3f17187 13857 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
221ea385
KS
13858 bptnum);
13859 else
3e43a32a
MS
13860 printf_filtered (_("Will ignore next %d "
13861 "crossings of breakpoint %d."),
221ea385
KS
13862 count, bptnum);
13863 }
76727919 13864 gdb::observers::breakpoint_modified.notify (b);
c5aa993b
JM
13865 return;
13866 }
c906108c 13867
8a3fe4f8 13868 error (_("No breakpoint number %d."), bptnum);
c906108c
SS
13869}
13870
c906108c
SS
13871/* Command to set ignore-count of breakpoint N to COUNT. */
13872
13873static void
0b39b52e 13874ignore_command (const char *args, int from_tty)
c906108c 13875{
0b39b52e 13876 const char *p = args;
52f0bd74 13877 int num;
c906108c
SS
13878
13879 if (p == 0)
e2e0b3e5 13880 error_no_arg (_("a breakpoint number"));
c5aa993b 13881
c906108c 13882 num = get_number (&p);
5c44784c 13883 if (num == 0)
8a3fe4f8 13884 error (_("bad breakpoint number: '%s'"), args);
c906108c 13885 if (*p == 0)
8a3fe4f8 13886 error (_("Second argument (specified ignore-count) is missing."));
c906108c
SS
13887
13888 set_ignore_count (num,
13889 longest_to_int (value_as_long (parse_and_eval (p))),
13890 from_tty);
221ea385
KS
13891 if (from_tty)
13892 printf_filtered ("\n");
c906108c
SS
13893}
13894\f
d0fe4701
XR
13895
13896/* Call FUNCTION on each of the breakpoints with numbers in the range
13897 defined by BP_NUM_RANGE (an inclusive range). */
c906108c
SS
13898
13899static void
d0fe4701
XR
13900map_breakpoint_number_range (std::pair<int, int> bp_num_range,
13901 gdb::function_view<void (breakpoint *)> function)
c906108c 13902{
d0fe4701
XR
13903 if (bp_num_range.first == 0)
13904 {
13905 warning (_("bad breakpoint number at or near '%d'"),
13906 bp_num_range.first);
13907 }
13908 else
c906108c 13909 {
d0fe4701 13910 struct breakpoint *b, *tmp;
197f0a60 13911
d0fe4701 13912 for (int i = bp_num_range.first; i <= bp_num_range.second; i++)
5c44784c 13913 {
d0fe4701
XR
13914 bool match = false;
13915
5c44784c 13916 ALL_BREAKPOINTS_SAFE (b, tmp)
d0fe4701 13917 if (b->number == i)
5c44784c 13918 {
bfd28288 13919 match = true;
48649e1b 13920 function (b);
11cf8741 13921 break;
5c44784c 13922 }
bfd28288 13923 if (!match)
d0fe4701 13924 printf_unfiltered (_("No breakpoint number %d.\n"), i);
c5aa993b 13925 }
c906108c
SS
13926 }
13927}
13928
d0fe4701
XR
13929/* Call FUNCTION on each of the breakpoints whose numbers are given in
13930 ARGS. */
13931
13932static void
13933map_breakpoint_numbers (const char *args,
13934 gdb::function_view<void (breakpoint *)> function)
13935{
13936 if (args == NULL || *args == '\0')
13937 error_no_arg (_("one or more breakpoint numbers"));
13938
13939 number_or_range_parser parser (args);
13940
13941 while (!parser.finished ())
13942 {
13943 int num = parser.get_number ();
13944 map_breakpoint_number_range (std::make_pair (num, num), function);
13945 }
13946}
13947
13948/* Return the breakpoint location structure corresponding to the
13949 BP_NUM and LOC_NUM values. */
13950
0d381245 13951static struct bp_location *
d0fe4701 13952find_location_by_number (int bp_num, int loc_num)
0d381245 13953{
0d381245 13954 struct breakpoint *b;
0d381245
VP
13955
13956 ALL_BREAKPOINTS (b)
13957 if (b->number == bp_num)
13958 {
13959 break;
13960 }
13961
13962 if (!b || b->number != bp_num)
d0fe4701 13963 error (_("Bad breakpoint number '%d'"), bp_num);
0d381245 13964
0d381245 13965 if (loc_num == 0)
d0fe4701 13966 error (_("Bad breakpoint location number '%d'"), loc_num);
0d381245 13967
d0fe4701
XR
13968 int n = 0;
13969 for (bp_location *loc = b->loc; loc != NULL; loc = loc->next)
13970 if (++n == loc_num)
13971 return loc;
13972
13973 error (_("Bad breakpoint location number '%d'"), loc_num);
0d381245
VP
13974}
13975
95e95a6d
PA
13976/* Modes of operation for extract_bp_num. */
13977enum class extract_bp_kind
13978{
13979 /* Extracting a breakpoint number. */
13980 bp,
13981
13982 /* Extracting a location number. */
13983 loc,
13984};
13985
13986/* Extract a breakpoint or location number (as determined by KIND)
13987 from the string starting at START. TRAILER is a character which
13988 can be found after the number. If you don't want a trailer, use
13989 '\0'. If END_OUT is not NULL, it is set to point after the parsed
13990 string. This always returns a positive integer. */
13991
13992static int
13993extract_bp_num (extract_bp_kind kind, const char *start,
13994 int trailer, const char **end_out = NULL)
13995{
13996 const char *end = start;
13997 int num = get_number_trailer (&end, trailer);
13998 if (num < 0)
13999 error (kind == extract_bp_kind::bp
14000 ? _("Negative breakpoint number '%.*s'")
14001 : _("Negative breakpoint location number '%.*s'"),
14002 int (end - start), start);
14003 if (num == 0)
14004 error (kind == extract_bp_kind::bp
14005 ? _("Bad breakpoint number '%.*s'")
14006 : _("Bad breakpoint location number '%.*s'"),
14007 int (end - start), start);
14008
14009 if (end_out != NULL)
14010 *end_out = end;
14011 return num;
14012}
14013
14014/* Extract a breakpoint or location range (as determined by KIND) in
14015 the form NUM1-NUM2 stored at &ARG[arg_offset]. Returns a std::pair
14016 representing the (inclusive) range. The returned pair's elements
14017 are always positive integers. */
14018
14019static std::pair<int, int>
14020extract_bp_or_bp_range (extract_bp_kind kind,
14021 const std::string &arg,
14022 std::string::size_type arg_offset)
14023{
14024 std::pair<int, int> range;
14025 const char *bp_loc = &arg[arg_offset];
14026 std::string::size_type dash = arg.find ('-', arg_offset);
14027 if (dash != std::string::npos)
14028 {
14029 /* bp_loc is a range (x-z). */
14030 if (arg.length () == dash + 1)
14031 error (kind == extract_bp_kind::bp
14032 ? _("Bad breakpoint number at or near: '%s'")
14033 : _("Bad breakpoint location number at or near: '%s'"),
14034 bp_loc);
14035
14036 const char *end;
14037 const char *start_first = bp_loc;
14038 const char *start_second = &arg[dash + 1];
14039 range.first = extract_bp_num (kind, start_first, '-');
14040 range.second = extract_bp_num (kind, start_second, '\0', &end);
14041
14042 if (range.first > range.second)
14043 error (kind == extract_bp_kind::bp
14044 ? _("Inverted breakpoint range at '%.*s'")
14045 : _("Inverted breakpoint location range at '%.*s'"),
14046 int (end - start_first), start_first);
14047 }
14048 else
14049 {
14050 /* bp_loc is a single value. */
14051 range.first = extract_bp_num (kind, bp_loc, '\0');
14052 range.second = range.first;
14053 }
14054 return range;
14055}
14056
d0fe4701
XR
14057/* Extract the breakpoint/location range specified by ARG. Returns
14058 the breakpoint range in BP_NUM_RANGE, and the location range in
14059 BP_LOC_RANGE.
14060
14061 ARG may be in any of the following forms:
14062
14063 x where 'x' is a breakpoint number.
14064 x-y where 'x' and 'y' specify a breakpoint numbers range.
14065 x.y where 'x' is a breakpoint number and 'y' a location number.
14066 x.y-z where 'x' is a breakpoint number and 'y' and 'z' specify a
14067 location number range.
14068*/
14069
cc638e86 14070static void
d0fe4701
XR
14071extract_bp_number_and_location (const std::string &arg,
14072 std::pair<int, int> &bp_num_range,
14073 std::pair<int, int> &bp_loc_range)
14074{
14075 std::string::size_type dot = arg.find ('.');
14076
14077 if (dot != std::string::npos)
14078 {
14079 /* Handle 'x.y' and 'x.y-z' cases. */
14080
14081 if (arg.length () == dot + 1 || dot == 0)
95e95a6d 14082 error (_("Bad breakpoint number at or near: '%s'"), arg.c_str ());
d0fe4701 14083
95e95a6d
PA
14084 bp_num_range.first
14085 = extract_bp_num (extract_bp_kind::bp, arg.c_str (), '.');
14086 bp_num_range.second = bp_num_range.first;
d0fe4701 14087
95e95a6d
PA
14088 bp_loc_range = extract_bp_or_bp_range (extract_bp_kind::loc,
14089 arg, dot + 1);
d0fe4701
XR
14090 }
14091 else
14092 {
14093 /* Handle x and x-y cases. */
d0fe4701 14094
95e95a6d 14095 bp_num_range = extract_bp_or_bp_range (extract_bp_kind::bp, arg, 0);
d0fe4701
XR
14096 bp_loc_range.first = 0;
14097 bp_loc_range.second = 0;
14098 }
d0fe4701
XR
14099}
14100
14101/* Enable or disable a breakpoint location BP_NUM.LOC_NUM. ENABLE
14102 specifies whether to enable or disable. */
14103
14104static void
14105enable_disable_bp_num_loc (int bp_num, int loc_num, bool enable)
14106{
14107 struct bp_location *loc = find_location_by_number (bp_num, loc_num);
14108 if (loc != NULL)
14109 {
14110 if (loc->enabled != enable)
14111 {
14112 loc->enabled = enable;
14113 mark_breakpoint_location_modified (loc);
14114 }
14115 if (target_supports_enable_disable_tracepoint ()
14116 && current_trace_status ()->running && loc->owner
14117 && is_tracepoint (loc->owner))
14118 target_disable_tracepoint (loc);
14119 }
14120 update_global_location_list (UGLL_DONT_INSERT);
d7154a8d
JV
14121
14122 gdb::observers::breakpoint_modified.notify (loc->owner);
d0fe4701
XR
14123}
14124
14125/* Enable or disable a range of breakpoint locations. BP_NUM is the
14126 number of the breakpoint, and BP_LOC_RANGE specifies the
14127 (inclusive) range of location numbers of that breakpoint to
14128 enable/disable. ENABLE specifies whether to enable or disable the
14129 location. */
14130
14131static void
14132enable_disable_breakpoint_location_range (int bp_num,
14133 std::pair<int, int> &bp_loc_range,
14134 bool enable)
14135{
14136 for (int i = bp_loc_range.first; i <= bp_loc_range.second; i++)
14137 enable_disable_bp_num_loc (bp_num, i, enable);
14138}
0d381245 14139
1900040c
MS
14140/* Set ignore-count of breakpoint number BPTNUM to COUNT.
14141 If from_tty is nonzero, it prints a message to that effect,
14142 which ends with a period (no newline). */
14143
c906108c 14144void
fba45db2 14145disable_breakpoint (struct breakpoint *bpt)
c906108c
SS
14146{
14147 /* Never disable a watchpoint scope breakpoint; we want to
14148 hit them when we leave scope so we can delete both the
14149 watchpoint and its scope breakpoint at that time. */
14150 if (bpt->type == bp_watchpoint_scope)
14151 return;
14152
b5de0fa7 14153 bpt->enable_state = bp_disabled;
c906108c 14154
b775012e
LM
14155 /* Mark breakpoint locations modified. */
14156 mark_breakpoint_modified (bpt);
14157
d248b706
KY
14158 if (target_supports_enable_disable_tracepoint ()
14159 && current_trace_status ()->running && is_tracepoint (bpt))
14160 {
14161 struct bp_location *location;
14162
14163 for (location = bpt->loc; location; location = location->next)
14164 target_disable_tracepoint (location);
14165 }
14166
44702360 14167 update_global_location_list (UGLL_DONT_INSERT);
c906108c 14168
76727919 14169 gdb::observers::breakpoint_modified.notify (bpt);
c906108c
SS
14170}
14171
d0fe4701
XR
14172/* Enable or disable the breakpoint(s) or breakpoint location(s)
14173 specified in ARGS. ARGS may be in any of the formats handled by
14174 extract_bp_number_and_location. ENABLE specifies whether to enable
14175 or disable the breakpoints/locations. */
14176
c906108c 14177static void
d0fe4701 14178enable_disable_command (const char *args, int from_tty, bool enable)
c906108c 14179{
c906108c 14180 if (args == 0)
46c6471b
PA
14181 {
14182 struct breakpoint *bpt;
14183
14184 ALL_BREAKPOINTS (bpt)
14185 if (user_breakpoint_p (bpt))
d0fe4701
XR
14186 {
14187 if (enable)
14188 enable_breakpoint (bpt);
14189 else
14190 disable_breakpoint (bpt);
14191 }
46c6471b 14192 }
9eaabc75 14193 else
0d381245 14194 {
cb791d59 14195 std::string num = extract_arg (&args);
9eaabc75 14196
cb791d59 14197 while (!num.empty ())
d248b706 14198 {
d0fe4701 14199 std::pair<int, int> bp_num_range, bp_loc_range;
9eaabc75 14200
cc638e86
PA
14201 extract_bp_number_and_location (num, bp_num_range, bp_loc_range);
14202
14203 if (bp_loc_range.first == bp_loc_range.second
14204 && bp_loc_range.first == 0)
d0fe4701 14205 {
cc638e86
PA
14206 /* Handle breakpoint ids with formats 'x' or 'x-z'. */
14207 map_breakpoint_number_range (bp_num_range,
14208 enable
14209 ? enable_breakpoint
14210 : disable_breakpoint);
14211 }
14212 else
14213 {
14214 /* Handle breakpoint ids with formats 'x.y' or
14215 'x.y-z'. */
14216 enable_disable_breakpoint_location_range
14217 (bp_num_range.first, bp_loc_range, enable);
b775012e 14218 }
9eaabc75 14219 num = extract_arg (&args);
d248b706 14220 }
0d381245 14221 }
c906108c
SS
14222}
14223
d0fe4701
XR
14224/* The disable command disables the specified breakpoints/locations
14225 (or all defined breakpoints) so they're no longer effective in
14226 stopping the inferior. ARGS may be in any of the forms defined in
14227 extract_bp_number_and_location. */
14228
14229static void
14230disable_command (const char *args, int from_tty)
14231{
14232 enable_disable_command (args, from_tty, false);
14233}
14234
c906108c 14235static void
816338b5
SS
14236enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
14237 int count)
c906108c 14238{
afe38095 14239 int target_resources_ok;
c906108c
SS
14240
14241 if (bpt->type == bp_hardware_breakpoint)
14242 {
14243 int i;
c5aa993b 14244 i = hw_breakpoint_used_count ();
53a5351d 14245 target_resources_ok =
d92524f1 14246 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
53a5351d 14247 i + 1, 0);
c906108c 14248 if (target_resources_ok == 0)
8a3fe4f8 14249 error (_("No hardware breakpoint support in the target."));
c906108c 14250 else if (target_resources_ok < 0)
8a3fe4f8 14251 error (_("Hardware breakpoints used exceeds limit."));
c906108c
SS
14252 }
14253
cc60f2e3 14254 if (is_watchpoint (bpt))
c906108c 14255 {
d07205c2 14256 /* Initialize it just to avoid a GCC false warning. */
f486487f 14257 enum enable_state orig_enable_state = bp_disabled;
dde02812 14258
a70b8144 14259 try
c906108c 14260 {
3a5c3e22
PA
14261 struct watchpoint *w = (struct watchpoint *) bpt;
14262
1e718ff1
TJB
14263 orig_enable_state = bpt->enable_state;
14264 bpt->enable_state = bp_enabled;
3a5c3e22 14265 update_watchpoint (w, 1 /* reparse */);
c906108c 14266 }
230d2906 14267 catch (const gdb_exception &e)
c5aa993b 14268 {
1e718ff1 14269 bpt->enable_state = orig_enable_state;
dde02812
ES
14270 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
14271 bpt->number);
14272 return;
c5aa993b 14273 }
c906108c 14274 }
0101ce28 14275
b775012e
LM
14276 bpt->enable_state = bp_enabled;
14277
14278 /* Mark breakpoint locations modified. */
14279 mark_breakpoint_modified (bpt);
14280
d248b706
KY
14281 if (target_supports_enable_disable_tracepoint ()
14282 && current_trace_status ()->running && is_tracepoint (bpt))
14283 {
14284 struct bp_location *location;
14285
14286 for (location = bpt->loc; location; location = location->next)
14287 target_enable_tracepoint (location);
14288 }
14289
b4c291bb 14290 bpt->disposition = disposition;
816338b5 14291 bpt->enable_count = count;
44702360 14292 update_global_location_list (UGLL_MAY_INSERT);
9c97429f 14293
76727919 14294 gdb::observers::breakpoint_modified.notify (bpt);
c906108c
SS
14295}
14296
fe3f5fa8 14297
c906108c 14298void
fba45db2 14299enable_breakpoint (struct breakpoint *bpt)
c906108c 14300{
816338b5 14301 enable_breakpoint_disp (bpt, bpt->disposition, 0);
51be5b68
PA
14302}
14303
d0fe4701
XR
14304/* The enable command enables the specified breakpoints/locations (or
14305 all defined breakpoints) so they once again become (or continue to
14306 be) effective in stopping the inferior. ARGS may be in any of the
14307 forms defined in extract_bp_number_and_location. */
c906108c 14308
c906108c 14309static void
981a3fb3 14310enable_command (const char *args, int from_tty)
c906108c 14311{
d0fe4701 14312 enable_disable_command (args, from_tty, true);
c906108c
SS
14313}
14314
c906108c 14315static void
4495129a 14316enable_once_command (const char *args, int from_tty)
c906108c 14317{
48649e1b
TT
14318 map_breakpoint_numbers
14319 (args, [&] (breakpoint *b)
14320 {
14321 iterate_over_related_breakpoints
14322 (b, [&] (breakpoint *bpt)
14323 {
14324 enable_breakpoint_disp (bpt, disp_disable, 1);
14325 });
14326 });
816338b5
SS
14327}
14328
14329static void
4495129a 14330enable_count_command (const char *args, int from_tty)
816338b5 14331{
b9d61307
SM
14332 int count;
14333
14334 if (args == NULL)
14335 error_no_arg (_("hit count"));
14336
14337 count = get_number (&args);
816338b5 14338
48649e1b
TT
14339 map_breakpoint_numbers
14340 (args, [&] (breakpoint *b)
14341 {
14342 iterate_over_related_breakpoints
14343 (b, [&] (breakpoint *bpt)
14344 {
14345 enable_breakpoint_disp (bpt, disp_disable, count);
14346 });
14347 });
c906108c
SS
14348}
14349
c906108c 14350static void
4495129a 14351enable_delete_command (const char *args, int from_tty)
c906108c 14352{
48649e1b
TT
14353 map_breakpoint_numbers
14354 (args, [&] (breakpoint *b)
14355 {
14356 iterate_over_related_breakpoints
14357 (b, [&] (breakpoint *bpt)
14358 {
14359 enable_breakpoint_disp (bpt, disp_del, 1);
14360 });
14361 });
c906108c
SS
14362}
14363\f
fa8d40ab 14364static void
981a3fb3 14365set_breakpoint_cmd (const char *args, int from_tty)
fa8d40ab
JJ
14366{
14367}
14368
14369static void
981a3fb3 14370show_breakpoint_cmd (const char *args, int from_tty)
fa8d40ab
JJ
14371{
14372}
14373
1f3b5d1b
PP
14374/* Invalidate last known value of any hardware watchpoint if
14375 the memory which that value represents has been written to by
14376 GDB itself. */
14377
14378static void
8de0566d
YQ
14379invalidate_bp_value_on_memory_change (struct inferior *inferior,
14380 CORE_ADDR addr, ssize_t len,
1f3b5d1b
PP
14381 const bfd_byte *data)
14382{
14383 struct breakpoint *bp;
14384
14385 ALL_BREAKPOINTS (bp)
14386 if (bp->enable_state == bp_enabled
3a5c3e22 14387 && bp->type == bp_hardware_watchpoint)
1f3b5d1b 14388 {
3a5c3e22 14389 struct watchpoint *wp = (struct watchpoint *) bp;
1f3b5d1b 14390
850645cf 14391 if (wp->val_valid && wp->val != nullptr)
3a5c3e22
PA
14392 {
14393 struct bp_location *loc;
14394
14395 for (loc = bp->loc; loc != NULL; loc = loc->next)
14396 if (loc->loc_type == bp_loc_hardware_watchpoint
14397 && loc->address + loc->length > addr
14398 && addr + len > loc->address)
14399 {
3a5c3e22 14400 wp->val = NULL;
4c1d86d9 14401 wp->val_valid = false;
3a5c3e22
PA
14402 }
14403 }
1f3b5d1b
PP
14404 }
14405}
14406
8181d85f
DJ
14407/* Create and insert a breakpoint for software single step. */
14408
14409void
6c95b8df 14410insert_single_step_breakpoint (struct gdbarch *gdbarch,
accd0bcd 14411 const address_space *aspace,
4a64f543 14412 CORE_ADDR next_pc)
8181d85f 14413{
7c16b83e
PA
14414 struct thread_info *tp = inferior_thread ();
14415 struct symtab_and_line sal;
14416 CORE_ADDR pc = next_pc;
8181d85f 14417
34b7e8a6
PA
14418 if (tp->control.single_step_breakpoints == NULL)
14419 {
14420 tp->control.single_step_breakpoints
5d5658a1 14421 = new_single_step_breakpoint (tp->global_num, gdbarch);
34b7e8a6 14422 }
8181d85f 14423
7c16b83e
PA
14424 sal = find_pc_line (pc, 0);
14425 sal.pc = pc;
14426 sal.section = find_pc_overlay (pc);
14427 sal.explicit_pc = 1;
34b7e8a6 14428 add_location_to_breakpoint (tp->control.single_step_breakpoints, &sal);
8181d85f 14429
7c16b83e 14430 update_global_location_list (UGLL_INSERT);
8181d85f
DJ
14431}
14432
93f9a11f
YQ
14433/* Insert single step breakpoints according to the current state. */
14434
14435int
14436insert_single_step_breakpoints (struct gdbarch *gdbarch)
14437{
f5ea389a 14438 struct regcache *regcache = get_current_regcache ();
a0ff9e1a 14439 std::vector<CORE_ADDR> next_pcs;
93f9a11f 14440
f5ea389a 14441 next_pcs = gdbarch_software_single_step (gdbarch, regcache);
93f9a11f 14442
a0ff9e1a 14443 if (!next_pcs.empty ())
93f9a11f 14444 {
f5ea389a 14445 struct frame_info *frame = get_current_frame ();
8b86c959 14446 const address_space *aspace = get_frame_address_space (frame);
93f9a11f 14447
a0ff9e1a 14448 for (CORE_ADDR pc : next_pcs)
93f9a11f
YQ
14449 insert_single_step_breakpoint (gdbarch, aspace, pc);
14450
93f9a11f
YQ
14451 return 1;
14452 }
14453 else
14454 return 0;
14455}
14456
34b7e8a6 14457/* See breakpoint.h. */
f02253f1
HZ
14458
14459int
7c16b83e 14460breakpoint_has_location_inserted_here (struct breakpoint *bp,
accd0bcd 14461 const address_space *aspace,
7c16b83e 14462 CORE_ADDR pc)
1aafd4da 14463{
7c16b83e 14464 struct bp_location *loc;
1aafd4da 14465
7c16b83e
PA
14466 for (loc = bp->loc; loc != NULL; loc = loc->next)
14467 if (loc->inserted
14468 && breakpoint_location_address_match (loc, aspace, pc))
14469 return 1;
1aafd4da 14470
7c16b83e 14471 return 0;
ef370185
JB
14472}
14473
14474/* Check whether a software single-step breakpoint is inserted at
14475 PC. */
14476
14477int
accd0bcd 14478single_step_breakpoint_inserted_here_p (const address_space *aspace,
ef370185
JB
14479 CORE_ADDR pc)
14480{
34b7e8a6
PA
14481 struct breakpoint *bpt;
14482
14483 ALL_BREAKPOINTS (bpt)
14484 {
14485 if (bpt->type == bp_single_step
14486 && breakpoint_has_location_inserted_here (bpt, aspace, pc))
14487 return 1;
14488 }
14489 return 0;
1aafd4da
UW
14490}
14491
1042e4c0
SS
14492/* Tracepoint-specific operations. */
14493
14494/* Set tracepoint count to NUM. */
14495static void
14496set_tracepoint_count (int num)
14497{
14498 tracepoint_count = num;
4fa62494 14499 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
1042e4c0
SS
14500}
14501
70221824 14502static void
0b39b52e 14503trace_command (const char *arg, int from_tty)
1042e4c0 14504{
ffc2605c
TT
14505 event_location_up location = string_to_event_location (&arg,
14506 current_language);
bac7c5cf
GB
14507 const struct breakpoint_ops *ops = breakpoint_ops_for_event_location
14508 (location.get (), true /* is_tracepoint */);
55aa24fb 14509
558a9d82 14510 create_breakpoint (get_current_arch (),
ffc2605c 14511 location.get (),
f00aae0f 14512 NULL, 0, arg, 1 /* parse arg */,
558a9d82
YQ
14513 0 /* tempflag */,
14514 bp_tracepoint /* type_wanted */,
14515 0 /* Ignore count */,
14516 pending_break_support,
14517 ops,
14518 from_tty,
14519 1 /* enabled */,
14520 0 /* internal */, 0);
1042e4c0
SS
14521}
14522
70221824 14523static void
0b39b52e 14524ftrace_command (const char *arg, int from_tty)
7a697b8d 14525{
ffc2605c
TT
14526 event_location_up location = string_to_event_location (&arg,
14527 current_language);
558a9d82 14528 create_breakpoint (get_current_arch (),
ffc2605c 14529 location.get (),
f00aae0f 14530 NULL, 0, arg, 1 /* parse arg */,
558a9d82
YQ
14531 0 /* tempflag */,
14532 bp_fast_tracepoint /* type_wanted */,
14533 0 /* Ignore count */,
14534 pending_break_support,
14535 &tracepoint_breakpoint_ops,
14536 from_tty,
14537 1 /* enabled */,
14538 0 /* internal */, 0);
0fb4aa4b
PA
14539}
14540
14541/* strace command implementation. Creates a static tracepoint. */
14542
70221824 14543static void
0b39b52e 14544strace_command (const char *arg, int from_tty)
0fb4aa4b 14545{
983af33b 14546 struct breakpoint_ops *ops;
ffc2605c 14547 event_location_up location;
983af33b
SDJ
14548
14549 /* Decide if we are dealing with a static tracepoint marker (`-m'),
14550 or with a normal static tracepoint. */
61012eef 14551 if (arg && startswith (arg, "-m") && isspace (arg[2]))
f00aae0f
KS
14552 {
14553 ops = &strace_marker_breakpoint_ops;
a20714ff 14554 location = new_linespec_location (&arg, symbol_name_match_type::FULL);
f00aae0f 14555 }
983af33b 14556 else
f00aae0f
KS
14557 {
14558 ops = &tracepoint_breakpoint_ops;
14559 location = string_to_event_location (&arg, current_language);
14560 }
983af33b 14561
558a9d82 14562 create_breakpoint (get_current_arch (),
ffc2605c 14563 location.get (),
f00aae0f 14564 NULL, 0, arg, 1 /* parse arg */,
558a9d82
YQ
14565 0 /* tempflag */,
14566 bp_static_tracepoint /* type_wanted */,
14567 0 /* Ignore count */,
14568 pending_break_support,
14569 ops,
14570 from_tty,
14571 1 /* enabled */,
14572 0 /* internal */, 0);
7a697b8d
SS
14573}
14574
409873ef
SS
14575/* Set up a fake reader function that gets command lines from a linked
14576 list that was acquired during tracepoint uploading. */
14577
14578static struct uploaded_tp *this_utp;
3149d8c1 14579static int next_cmd;
409873ef
SS
14580
14581static char *
14582read_uploaded_action (void)
14583{
a18ba4e4 14584 char *rslt = nullptr;
409873ef 14585
a18ba4e4
SM
14586 if (next_cmd < this_utp->cmd_strings.size ())
14587 {
67aa1f3c 14588 rslt = this_utp->cmd_strings[next_cmd].get ();
a18ba4e4
SM
14589 next_cmd++;
14590 }
409873ef
SS
14591
14592 return rslt;
14593}
14594
00bf0b85
SS
14595/* Given information about a tracepoint as recorded on a target (which
14596 can be either a live system or a trace file), attempt to create an
14597 equivalent GDB tracepoint. This is not a reliable process, since
14598 the target does not necessarily have all the information used when
14599 the tracepoint was originally defined. */
14600
d9b3f62e 14601struct tracepoint *
00bf0b85 14602create_tracepoint_from_upload (struct uploaded_tp *utp)
d5551862 14603{
f2fc3015
TT
14604 const char *addr_str;
14605 char small_buf[100];
d9b3f62e 14606 struct tracepoint *tp;
fd9b8c24 14607
409873ef 14608 if (utp->at_string)
67aa1f3c 14609 addr_str = utp->at_string.get ();
409873ef
SS
14610 else
14611 {
14612 /* In the absence of a source location, fall back to raw
14613 address. Since there is no way to confirm that the address
14614 means the same thing as when the trace was started, warn the
14615 user. */
3e43a32a
MS
14616 warning (_("Uploaded tracepoint %d has no "
14617 "source location, using raw address"),
409873ef 14618 utp->number);
8c042590 14619 xsnprintf (small_buf, sizeof (small_buf), "*%s", hex_string (utp->addr));
409873ef
SS
14620 addr_str = small_buf;
14621 }
14622
14623 /* There's not much we can do with a sequence of bytecodes. */
14624 if (utp->cond && !utp->cond_string)
3e43a32a
MS
14625 warning (_("Uploaded tracepoint %d condition "
14626 "has no source form, ignoring it"),
409873ef 14627 utp->number);
d5551862 14628
ffc2605c
TT
14629 event_location_up location = string_to_event_location (&addr_str,
14630 current_language);
8cdf0e15 14631 if (!create_breakpoint (get_current_arch (),
ffc2605c 14632 location.get (),
67aa1f3c 14633 utp->cond_string.get (), -1, addr_str,
e7e0cddf 14634 0 /* parse cond/thread */,
8cdf0e15 14635 0 /* tempflag */,
0fb4aa4b 14636 utp->type /* type_wanted */,
8cdf0e15
VP
14637 0 /* Ignore count */,
14638 pending_break_support,
348d480f 14639 &tracepoint_breakpoint_ops,
8cdf0e15 14640 0 /* from_tty */,
84f4c1fe 14641 utp->enabled /* enabled */,
44f238bb
PA
14642 0 /* internal */,
14643 CREATE_BREAKPOINT_FLAGS_INSERTED))
ffc2605c 14644 return NULL;
fd9b8c24 14645
409873ef 14646 /* Get the tracepoint we just created. */
fd9b8c24
PA
14647 tp = get_tracepoint (tracepoint_count);
14648 gdb_assert (tp != NULL);
d5551862 14649
00bf0b85
SS
14650 if (utp->pass > 0)
14651 {
8c042590 14652 xsnprintf (small_buf, sizeof (small_buf), "%d %d", utp->pass,
c1fc2657 14653 tp->number);
00bf0b85 14654
409873ef 14655 trace_pass_command (small_buf, 0);
00bf0b85
SS
14656 }
14657
409873ef
SS
14658 /* If we have uploaded versions of the original commands, set up a
14659 special-purpose "reader" function and call the usual command line
14660 reader, then pass the result to the breakpoint command-setting
14661 function. */
a18ba4e4 14662 if (!utp->cmd_strings.empty ())
00bf0b85 14663 {
12973681 14664 counted_command_line cmd_list;
00bf0b85 14665
409873ef 14666 this_utp = utp;
3149d8c1 14667 next_cmd = 0;
d5551862 14668
60b3cef2 14669 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL);
409873ef 14670
c1fc2657 14671 breakpoint_set_commands (tp, std::move (cmd_list));
00bf0b85 14672 }
a18ba4e4
SM
14673 else if (!utp->actions.empty ()
14674 || !utp->step_actions.empty ())
3e43a32a
MS
14675 warning (_("Uploaded tracepoint %d actions "
14676 "have no source form, ignoring them"),
409873ef 14677 utp->number);
00bf0b85 14678
f196051f 14679 /* Copy any status information that might be available. */
c1fc2657 14680 tp->hit_count = utp->hit_count;
f196051f
SS
14681 tp->traceframe_usage = utp->traceframe_usage;
14682
00bf0b85 14683 return tp;
d9b3f62e 14684}
00bf0b85 14685
1042e4c0
SS
14686/* Print information on tracepoint number TPNUM_EXP, or all if
14687 omitted. */
14688
14689static void
1d12d88f 14690info_tracepoints_command (const char *args, int from_tty)
1042e4c0 14691{
79a45e25 14692 struct ui_out *uiout = current_uiout;
e5a67952 14693 int num_printed;
1042e4c0 14694
5c458ae8 14695 num_printed = breakpoint_1 (args, false, is_tracepoint);
d77f58be
SS
14696
14697 if (num_printed == 0)
1042e4c0 14698 {
e5a67952 14699 if (args == NULL || *args == '\0')
112e8700 14700 uiout->message ("No tracepoints.\n");
d77f58be 14701 else
112e8700 14702 uiout->message ("No tracepoint matching '%s'.\n", args);
1042e4c0 14703 }
ad443146
SS
14704
14705 default_collect_info ();
1042e4c0
SS
14706}
14707
4a64f543 14708/* The 'enable trace' command enables tracepoints.
1042e4c0
SS
14709 Not supported by all targets. */
14710static void
5fed81ff 14711enable_trace_command (const char *args, int from_tty)
1042e4c0
SS
14712{
14713 enable_command (args, from_tty);
14714}
14715
4a64f543 14716/* The 'disable trace' command disables tracepoints.
1042e4c0
SS
14717 Not supported by all targets. */
14718static void
5fed81ff 14719disable_trace_command (const char *args, int from_tty)
1042e4c0
SS
14720{
14721 disable_command (args, from_tty);
14722}
14723
4a64f543 14724/* Remove a tracepoint (or all if no argument). */
1042e4c0 14725static void
4495129a 14726delete_trace_command (const char *arg, int from_tty)
1042e4c0 14727{
35df4500 14728 struct breakpoint *b, *b_tmp;
1042e4c0
SS
14729
14730 dont_repeat ();
14731
14732 if (arg == 0)
14733 {
14734 int breaks_to_delete = 0;
14735
14736 /* Delete all breakpoints if no argument.
14737 Do not delete internal or call-dummy breakpoints, these
4a64f543
MS
14738 have to be deleted with an explicit breakpoint number
14739 argument. */
1042e4c0 14740 ALL_TRACEPOINTS (b)
46c6471b 14741 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0
SS
14742 {
14743 breaks_to_delete = 1;
14744 break;
14745 }
1042e4c0
SS
14746
14747 /* Ask user only if there are some breakpoints to delete. */
14748 if (!from_tty
14749 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
14750 {
35df4500 14751 ALL_BREAKPOINTS_SAFE (b, b_tmp)
46c6471b 14752 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0 14753 delete_breakpoint (b);
1042e4c0
SS
14754 }
14755 }
14756 else
48649e1b 14757 map_breakpoint_numbers
b926417a 14758 (arg, [&] (breakpoint *br)
48649e1b 14759 {
b926417a 14760 iterate_over_related_breakpoints (br, delete_breakpoint);
48649e1b 14761 });
1042e4c0
SS
14762}
14763
197f0a60
TT
14764/* Helper function for trace_pass_command. */
14765
14766static void
d9b3f62e 14767trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
197f0a60 14768{
d9b3f62e 14769 tp->pass_count = count;
76727919 14770 gdb::observers::breakpoint_modified.notify (tp);
197f0a60
TT
14771 if (from_tty)
14772 printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
c1fc2657 14773 tp->number, count);
197f0a60
TT
14774}
14775
1042e4c0
SS
14776/* Set passcount for tracepoint.
14777
14778 First command argument is passcount, second is tracepoint number.
14779 If tracepoint number omitted, apply to most recently defined.
14780 Also accepts special argument "all". */
14781
14782static void
0b39b52e 14783trace_pass_command (const char *args, int from_tty)
1042e4c0 14784{
d9b3f62e 14785 struct tracepoint *t1;
0b39b52e 14786 ULONGEST count;
1042e4c0
SS
14787
14788 if (args == 0 || *args == 0)
3e43a32a
MS
14789 error (_("passcount command requires an "
14790 "argument (count + optional TP num)"));
1042e4c0 14791
0b39b52e 14792 count = strtoulst (args, &args, 10); /* Count comes first, then TP num. */
1042e4c0 14793
529480d0 14794 args = skip_spaces (args);
1042e4c0
SS
14795 if (*args && strncasecmp (args, "all", 3) == 0)
14796 {
d9b3f62e
PA
14797 struct breakpoint *b;
14798
1042e4c0 14799 args += 3; /* Skip special argument "all". */
1042e4c0
SS
14800 if (*args)
14801 error (_("Junk at end of arguments."));
1042e4c0 14802
d9b3f62e 14803 ALL_TRACEPOINTS (b)
197f0a60 14804 {
d9b3f62e 14805 t1 = (struct tracepoint *) b;
197f0a60
TT
14806 trace_pass_set_count (t1, count, from_tty);
14807 }
14808 }
14809 else if (*args == '\0')
1042e4c0 14810 {
5fa1d40e 14811 t1 = get_tracepoint_by_number (&args, NULL);
1042e4c0 14812 if (t1)
197f0a60
TT
14813 trace_pass_set_count (t1, count, from_tty);
14814 }
14815 else
14816 {
bfd28288
PA
14817 number_or_range_parser parser (args);
14818 while (!parser.finished ())
1042e4c0 14819 {
bfd28288 14820 t1 = get_tracepoint_by_number (&args, &parser);
197f0a60
TT
14821 if (t1)
14822 trace_pass_set_count (t1, count, from_tty);
1042e4c0
SS
14823 }
14824 }
1042e4c0
SS
14825}
14826
d9b3f62e 14827struct tracepoint *
1042e4c0
SS
14828get_tracepoint (int num)
14829{
14830 struct breakpoint *t;
14831
14832 ALL_TRACEPOINTS (t)
14833 if (t->number == num)
d9b3f62e 14834 return (struct tracepoint *) t;
1042e4c0
SS
14835
14836 return NULL;
14837}
14838
d5551862
SS
14839/* Find the tracepoint with the given target-side number (which may be
14840 different from the tracepoint number after disconnecting and
14841 reconnecting). */
14842
d9b3f62e 14843struct tracepoint *
d5551862
SS
14844get_tracepoint_by_number_on_target (int num)
14845{
d9b3f62e 14846 struct breakpoint *b;
d5551862 14847
d9b3f62e
PA
14848 ALL_TRACEPOINTS (b)
14849 {
14850 struct tracepoint *t = (struct tracepoint *) b;
14851
14852 if (t->number_on_target == num)
14853 return t;
14854 }
d5551862
SS
14855
14856 return NULL;
14857}
14858
1042e4c0 14859/* Utility: parse a tracepoint number and look it up in the list.
197f0a60 14860 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
5fa1d40e
YQ
14861 If the argument is missing, the most recent tracepoint
14862 (tracepoint_count) is returned. */
14863
d9b3f62e 14864struct tracepoint *
0b39b52e 14865get_tracepoint_by_number (const char **arg,
bfd28288 14866 number_or_range_parser *parser)
1042e4c0 14867{
1042e4c0
SS
14868 struct breakpoint *t;
14869 int tpnum;
0b39b52e 14870 const char *instring = arg == NULL ? NULL : *arg;
1042e4c0 14871
bfd28288 14872 if (parser != NULL)
197f0a60 14873 {
bfd28288
PA
14874 gdb_assert (!parser->finished ());
14875 tpnum = parser->get_number ();
197f0a60
TT
14876 }
14877 else if (arg == NULL || *arg == NULL || ! **arg)
5fa1d40e 14878 tpnum = tracepoint_count;
1042e4c0 14879 else
197f0a60 14880 tpnum = get_number (arg);
1042e4c0
SS
14881
14882 if (tpnum <= 0)
14883 {
14884 if (instring && *instring)
14885 printf_filtered (_("bad tracepoint number at or near '%s'\n"),
14886 instring);
14887 else
5fa1d40e 14888 printf_filtered (_("No previous tracepoint\n"));
1042e4c0
SS
14889 return NULL;
14890 }
14891
14892 ALL_TRACEPOINTS (t)
14893 if (t->number == tpnum)
14894 {
d9b3f62e 14895 return (struct tracepoint *) t;
1042e4c0
SS
14896 }
14897
1042e4c0
SS
14898 printf_unfiltered ("No tracepoint number %d.\n", tpnum);
14899 return NULL;
14900}
14901
d9b3f62e
PA
14902void
14903print_recreate_thread (struct breakpoint *b, struct ui_file *fp)
14904{
14905 if (b->thread != -1)
14906 fprintf_unfiltered (fp, " thread %d", b->thread);
14907
14908 if (b->task != 0)
14909 fprintf_unfiltered (fp, " task %d", b->task);
14910
14911 fprintf_unfiltered (fp, "\n");
14912}
14913
6149aea9
PA
14914/* Save information on user settable breakpoints (watchpoints, etc) to
14915 a new script file named FILENAME. If FILTER is non-NULL, call it
14916 on each breakpoint and only include the ones for which it returns
f2478a7e 14917 true. */
6149aea9 14918
1042e4c0 14919static void
4495129a 14920save_breakpoints (const char *filename, int from_tty,
f2478a7e 14921 bool (*filter) (const struct breakpoint *))
1042e4c0
SS
14922{
14923 struct breakpoint *tp;
6149aea9 14924 int any = 0;
6149aea9 14925 int extra_trace_bits = 0;
1042e4c0 14926
6149aea9
PA
14927 if (filename == 0 || *filename == 0)
14928 error (_("Argument required (file name in which to save)"));
1042e4c0
SS
14929
14930 /* See if we have anything to save. */
6149aea9 14931 ALL_BREAKPOINTS (tp)
1042e4c0 14932 {
6149aea9 14933 /* Skip internal and momentary breakpoints. */
09d682a4 14934 if (!user_breakpoint_p (tp))
6149aea9
PA
14935 continue;
14936
14937 /* If we have a filter, only save the breakpoints it accepts. */
14938 if (filter && !filter (tp))
14939 continue;
14940
14941 any = 1;
14942
14943 if (is_tracepoint (tp))
14944 {
14945 extra_trace_bits = 1;
14946
14947 /* We can stop searching. */
14948 break;
14949 }
1042e4c0 14950 }
6149aea9
PA
14951
14952 if (!any)
1042e4c0 14953 {
6149aea9 14954 warning (_("Nothing to save."));
1042e4c0
SS
14955 return;
14956 }
14957
ee0c3293 14958 gdb::unique_xmalloc_ptr<char> expanded_filename (tilde_expand (filename));
d7e74731
PA
14959
14960 stdio_file fp;
14961
ee0c3293 14962 if (!fp.open (expanded_filename.get (), "w"))
6149aea9 14963 error (_("Unable to open file '%s' for saving (%s)"),
ee0c3293 14964 expanded_filename.get (), safe_strerror (errno));
8bf6485c 14965
6149aea9 14966 if (extra_trace_bits)
d7e74731 14967 save_trace_state_variables (&fp);
8bf6485c 14968
6149aea9 14969 ALL_BREAKPOINTS (tp)
1042e4c0 14970 {
6149aea9 14971 /* Skip internal and momentary breakpoints. */
09d682a4 14972 if (!user_breakpoint_p (tp))
6149aea9 14973 continue;
8bf6485c 14974
6149aea9
PA
14975 /* If we have a filter, only save the breakpoints it accepts. */
14976 if (filter && !filter (tp))
14977 continue;
14978
d7e74731 14979 tp->ops->print_recreate (tp, &fp);
1042e4c0 14980
6149aea9
PA
14981 /* Note, we can't rely on tp->number for anything, as we can't
14982 assume the recreated breakpoint numbers will match. Use $bpnum
14983 instead. */
14984
14985 if (tp->cond_string)
d7e74731 14986 fp.printf (" condition $bpnum %s\n", tp->cond_string);
6149aea9
PA
14987
14988 if (tp->ignore_count)
d7e74731 14989 fp.printf (" ignore $bpnum %d\n", tp->ignore_count);
6149aea9 14990
2d9442cc 14991 if (tp->type != bp_dprintf && tp->commands)
1042e4c0 14992 {
d7e74731 14993 fp.puts (" commands\n");
a7bdde9e 14994
d7e74731 14995 current_uiout->redirect (&fp);
a70b8144 14996 try
1042e4c0 14997 {
d1b0a7bf 14998 print_command_lines (current_uiout, tp->commands.get (), 2);
a7bdde9e 14999 }
230d2906 15000 catch (const gdb_exception &ex)
492d29ea 15001 {
112e8700 15002 current_uiout->redirect (NULL);
eedc3f4f 15003 throw;
492d29ea 15004 }
1042e4c0 15005
112e8700 15006 current_uiout->redirect (NULL);
d7e74731 15007 fp.puts (" end\n");
1042e4c0 15008 }
6149aea9
PA
15009
15010 if (tp->enable_state == bp_disabled)
d7e74731 15011 fp.puts ("disable $bpnum\n");
6149aea9
PA
15012
15013 /* If this is a multi-location breakpoint, check if the locations
15014 should be individually disabled. Watchpoint locations are
15015 special, and not user visible. */
15016 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
15017 {
15018 struct bp_location *loc;
15019 int n = 1;
15020
15021 for (loc = tp->loc; loc != NULL; loc = loc->next, n++)
15022 if (!loc->enabled)
d7e74731 15023 fp.printf ("disable $bpnum.%d\n", n);
6149aea9 15024 }
1042e4c0 15025 }
8bf6485c 15026
6149aea9 15027 if (extra_trace_bits && *default_collect)
d7e74731 15028 fp.printf ("set default-collect %s\n", default_collect);
8bf6485c 15029
1042e4c0 15030 if (from_tty)
ee0c3293 15031 printf_filtered (_("Saved to file '%s'.\n"), expanded_filename.get ());
6149aea9
PA
15032}
15033
15034/* The `save breakpoints' command. */
15035
15036static void
4495129a 15037save_breakpoints_command (const char *args, int from_tty)
6149aea9
PA
15038{
15039 save_breakpoints (args, from_tty, NULL);
15040}
15041
15042/* The `save tracepoints' command. */
15043
15044static void
4495129a 15045save_tracepoints_command (const char *args, int from_tty)
6149aea9
PA
15046{
15047 save_breakpoints (args, from_tty, is_tracepoint);
1042e4c0
SS
15048}
15049
15050/* Create a vector of all tracepoints. */
15051
f51e0e20 15052std::vector<breakpoint *>
eeae04df 15053all_tracepoints (void)
1042e4c0 15054{
f51e0e20 15055 std::vector<breakpoint *> tp_vec;
1042e4c0
SS
15056 struct breakpoint *tp;
15057
15058 ALL_TRACEPOINTS (tp)
15059 {
f51e0e20 15060 tp_vec.push_back (tp);
1042e4c0
SS
15061 }
15062
15063 return tp_vec;
15064}
15065
c906108c 15066\f
629500fa
KS
15067/* This help string is used to consolidate all the help string for specifying
15068 locations used by several commands. */
15069
15070#define LOCATION_HELP_STRING \
15071"Linespecs are colon-separated lists of location parameters, such as\n\
15072source filename, function name, label name, and line number.\n\
15073Example: To specify the start of a label named \"the_top\" in the\n\
15074function \"fact\" in the file \"factorial.c\", use\n\
15075\"factorial.c:fact:the_top\".\n\
15076\n\
15077Address locations begin with \"*\" and specify an exact address in the\n\
15078program. Example: To specify the fourth byte past the start function\n\
15079\"main\", use \"*main + 4\".\n\
15080\n\
15081Explicit locations are similar to linespecs but use an option/argument\n\
15082syntax to specify location parameters.\n\
15083Example: To specify the start of the label named \"the_top\" in the\n\
15084function \"fact\" in the file \"factorial.c\", use \"-source factorial.c\n\
a20714ff
PA
15085-function fact -label the_top\".\n\
15086\n\
15087By default, a specified function is matched against the program's\n\
15088functions in all scopes. For C++, this means in all namespaces and\n\
15089classes. For Ada, this means in all packages. E.g., in C++,\n\
15090\"func()\" matches \"A::func()\", \"A::B::func()\", etc. The\n\
15091\"-qualified\" flag overrides this behavior, making GDB interpret the\n\
89549d7f 15092specified name as a complete fully-qualified name instead."
629500fa 15093
4a64f543
MS
15094/* This help string is used for the break, hbreak, tbreak and thbreak
15095 commands. It is defined as a macro to prevent duplication.
15096 COMMAND should be a string constant containing the name of the
15097 command. */
629500fa 15098
31e2b00f 15099#define BREAK_ARGS_HELP(command) \
fb7b5af4
SDJ
15100command" [PROBE_MODIFIER] [LOCATION] [thread THREADNUM] [if CONDITION]\n\
15101PROBE_MODIFIER shall be present if the command is to be placed in a\n\
15102probe point. Accepted values are `-probe' (for a generic, automatically\n\
d4777acb
JM
15103guessed probe type), `-probe-stap' (for a SystemTap probe) or \n\
15104`-probe-dtrace' (for a DTrace probe).\n\
629500fa
KS
15105LOCATION may be a linespec, address, or explicit location as described\n\
15106below.\n\
15107\n\
dc10affe
PA
15108With no LOCATION, uses current execution address of the selected\n\
15109stack frame. This is useful for breaking on return to a stack frame.\n\
31e2b00f
AS
15110\n\
15111THREADNUM is the number from \"info threads\".\n\
15112CONDITION is a boolean expression.\n\
89549d7f 15113\n" LOCATION_HELP_STRING "\n\n\
d41c0fc8
PA
15114Multiple breakpoints at one place are permitted, and useful if their\n\
15115conditions are different.\n\
31e2b00f
AS
15116\n\
15117Do \"help breakpoints\" for info on other commands dealing with breakpoints."
15118
44feb3ce
TT
15119/* List of subcommands for "catch". */
15120static struct cmd_list_element *catch_cmdlist;
15121
15122/* List of subcommands for "tcatch". */
15123static struct cmd_list_element *tcatch_cmdlist;
15124
9ac4176b 15125void
a121b7c1 15126add_catch_command (const char *name, const char *docstring,
eb4c3f4a 15127 cmd_const_sfunc_ftype *sfunc,
625e8578 15128 completer_ftype *completer,
44feb3ce
TT
15129 void *user_data_catch,
15130 void *user_data_tcatch)
15131{
15132 struct cmd_list_element *command;
15133
0450cc4c 15134 command = add_cmd (name, class_breakpoint, docstring,
44feb3ce
TT
15135 &catch_cmdlist);
15136 set_cmd_sfunc (command, sfunc);
15137 set_cmd_context (command, user_data_catch);
a96d9b2e 15138 set_cmd_completer (command, completer);
44feb3ce 15139
0450cc4c 15140 command = add_cmd (name, class_breakpoint, docstring,
44feb3ce
TT
15141 &tcatch_cmdlist);
15142 set_cmd_sfunc (command, sfunc);
15143 set_cmd_context (command, user_data_tcatch);
a96d9b2e 15144 set_cmd_completer (command, completer);
44feb3ce
TT
15145}
15146
6149aea9 15147static void
981a3fb3 15148save_command (const char *arg, int from_tty)
6149aea9 15149{
3e43a32a
MS
15150 printf_unfiltered (_("\"save\" must be followed by "
15151 "the name of a save subcommand.\n"));
635c7e8a 15152 help_list (save_cmdlist, "save ", all_commands, gdb_stdout);
6149aea9
PA
15153}
15154
84f4c1fe 15155struct breakpoint *
95da600f 15156iterate_over_breakpoints (gdb::function_view<bool (breakpoint *)> callback)
84f4c1fe 15157{
35df4500 15158 struct breakpoint *b, *b_tmp;
84f4c1fe 15159
35df4500 15160 ALL_BREAKPOINTS_SAFE (b, b_tmp)
84f4c1fe 15161 {
95da600f 15162 if (callback (b))
84f4c1fe
PM
15163 return b;
15164 }
15165
15166 return NULL;
15167}
15168
0574c78f
GB
15169/* Zero if any of the breakpoint's locations could be a location where
15170 functions have been inlined, nonzero otherwise. */
15171
15172static int
15173is_non_inline_function (struct breakpoint *b)
15174{
15175 /* The shared library event breakpoint is set on the address of a
15176 non-inline function. */
15177 if (b->type == bp_shlib_event)
15178 return 1;
15179
15180 return 0;
15181}
15182
15183/* Nonzero if the specified PC cannot be a location where functions
15184 have been inlined. */
15185
15186int
accd0bcd 15187pc_at_non_inline_function (const address_space *aspace, CORE_ADDR pc,
09ac7c10 15188 const struct target_waitstatus *ws)
0574c78f
GB
15189{
15190 struct breakpoint *b;
15191 struct bp_location *bl;
15192
15193 ALL_BREAKPOINTS (b)
15194 {
15195 if (!is_non_inline_function (b))
15196 continue;
15197
15198 for (bl = b->loc; bl != NULL; bl = bl->next)
15199 {
15200 if (!bl->shlib_disabled
09ac7c10 15201 && bpstat_check_location (bl, aspace, pc, ws))
0574c78f
GB
15202 return 1;
15203 }
15204 }
15205
15206 return 0;
15207}
15208
2f202fde
JK
15209/* Remove any references to OBJFILE which is going to be freed. */
15210
15211void
15212breakpoint_free_objfile (struct objfile *objfile)
15213{
15214 struct bp_location **locp, *loc;
15215
15216 ALL_BP_LOCATIONS (loc, locp)
eb822aa6 15217 if (loc->symtab != NULL && SYMTAB_OBJFILE (loc->symtab) == objfile)
2f202fde
JK
15218 loc->symtab = NULL;
15219}
15220
2060206e
PA
15221void
15222initialize_breakpoint_ops (void)
15223{
15224 static int initialized = 0;
15225
15226 struct breakpoint_ops *ops;
15227
15228 if (initialized)
15229 return;
15230 initialized = 1;
15231
15232 /* The breakpoint_ops structure to be inherit by all kinds of
15233 breakpoints (real breakpoints, i.e., user "break" breakpoints,
15234 internal and momentary breakpoints, etc.). */
15235 ops = &bkpt_base_breakpoint_ops;
15236 *ops = base_breakpoint_ops;
15237 ops->re_set = bkpt_re_set;
15238 ops->insert_location = bkpt_insert_location;
15239 ops->remove_location = bkpt_remove_location;
15240 ops->breakpoint_hit = bkpt_breakpoint_hit;
5f700d83 15241 ops->create_sals_from_location = bkpt_create_sals_from_location;
983af33b 15242 ops->create_breakpoints_sal = bkpt_create_breakpoints_sal;
5f700d83 15243 ops->decode_location = bkpt_decode_location;
2060206e
PA
15244
15245 /* The breakpoint_ops structure to be used in regular breakpoints. */
15246 ops = &bkpt_breakpoint_ops;
15247 *ops = bkpt_base_breakpoint_ops;
15248 ops->re_set = bkpt_re_set;
15249 ops->resources_needed = bkpt_resources_needed;
15250 ops->print_it = bkpt_print_it;
15251 ops->print_mention = bkpt_print_mention;
15252 ops->print_recreate = bkpt_print_recreate;
15253
15254 /* Ranged breakpoints. */
15255 ops = &ranged_breakpoint_ops;
15256 *ops = bkpt_breakpoint_ops;
15257 ops->breakpoint_hit = breakpoint_hit_ranged_breakpoint;
15258 ops->resources_needed = resources_needed_ranged_breakpoint;
15259 ops->print_it = print_it_ranged_breakpoint;
15260 ops->print_one = print_one_ranged_breakpoint;
15261 ops->print_one_detail = print_one_detail_ranged_breakpoint;
15262 ops->print_mention = print_mention_ranged_breakpoint;
15263 ops->print_recreate = print_recreate_ranged_breakpoint;
15264
15265 /* Internal breakpoints. */
15266 ops = &internal_breakpoint_ops;
15267 *ops = bkpt_base_breakpoint_ops;
15268 ops->re_set = internal_bkpt_re_set;
15269 ops->check_status = internal_bkpt_check_status;
15270 ops->print_it = internal_bkpt_print_it;
15271 ops->print_mention = internal_bkpt_print_mention;
15272
15273 /* Momentary breakpoints. */
15274 ops = &momentary_breakpoint_ops;
15275 *ops = bkpt_base_breakpoint_ops;
15276 ops->re_set = momentary_bkpt_re_set;
15277 ops->check_status = momentary_bkpt_check_status;
15278 ops->print_it = momentary_bkpt_print_it;
15279 ops->print_mention = momentary_bkpt_print_mention;
15280
55aa24fb
SDJ
15281 /* Probe breakpoints. */
15282 ops = &bkpt_probe_breakpoint_ops;
15283 *ops = bkpt_breakpoint_ops;
15284 ops->insert_location = bkpt_probe_insert_location;
15285 ops->remove_location = bkpt_probe_remove_location;
5f700d83
KS
15286 ops->create_sals_from_location = bkpt_probe_create_sals_from_location;
15287 ops->decode_location = bkpt_probe_decode_location;
55aa24fb 15288
2060206e
PA
15289 /* Watchpoints. */
15290 ops = &watchpoint_breakpoint_ops;
15291 *ops = base_breakpoint_ops;
15292 ops->re_set = re_set_watchpoint;
15293 ops->insert_location = insert_watchpoint;
15294 ops->remove_location = remove_watchpoint;
15295 ops->breakpoint_hit = breakpoint_hit_watchpoint;
15296 ops->check_status = check_status_watchpoint;
15297 ops->resources_needed = resources_needed_watchpoint;
15298 ops->works_in_software_mode = works_in_software_mode_watchpoint;
15299 ops->print_it = print_it_watchpoint;
15300 ops->print_mention = print_mention_watchpoint;
15301 ops->print_recreate = print_recreate_watchpoint;
427cd150 15302 ops->explains_signal = explains_signal_watchpoint;
2060206e
PA
15303
15304 /* Masked watchpoints. */
15305 ops = &masked_watchpoint_breakpoint_ops;
15306 *ops = watchpoint_breakpoint_ops;
15307 ops->insert_location = insert_masked_watchpoint;
15308 ops->remove_location = remove_masked_watchpoint;
15309 ops->resources_needed = resources_needed_masked_watchpoint;
15310 ops->works_in_software_mode = works_in_software_mode_masked_watchpoint;
15311 ops->print_it = print_it_masked_watchpoint;
15312 ops->print_one_detail = print_one_detail_masked_watchpoint;
15313 ops->print_mention = print_mention_masked_watchpoint;
15314 ops->print_recreate = print_recreate_masked_watchpoint;
15315
15316 /* Tracepoints. */
15317 ops = &tracepoint_breakpoint_ops;
15318 *ops = base_breakpoint_ops;
15319 ops->re_set = tracepoint_re_set;
15320 ops->breakpoint_hit = tracepoint_breakpoint_hit;
15321 ops->print_one_detail = tracepoint_print_one_detail;
15322 ops->print_mention = tracepoint_print_mention;
15323 ops->print_recreate = tracepoint_print_recreate;
5f700d83 15324 ops->create_sals_from_location = tracepoint_create_sals_from_location;
983af33b 15325 ops->create_breakpoints_sal = tracepoint_create_breakpoints_sal;
5f700d83 15326 ops->decode_location = tracepoint_decode_location;
983af33b 15327
55aa24fb
SDJ
15328 /* Probe tracepoints. */
15329 ops = &tracepoint_probe_breakpoint_ops;
15330 *ops = tracepoint_breakpoint_ops;
5f700d83
KS
15331 ops->create_sals_from_location = tracepoint_probe_create_sals_from_location;
15332 ops->decode_location = tracepoint_probe_decode_location;
55aa24fb 15333
983af33b
SDJ
15334 /* Static tracepoints with marker (`-m'). */
15335 ops = &strace_marker_breakpoint_ops;
15336 *ops = tracepoint_breakpoint_ops;
5f700d83 15337 ops->create_sals_from_location = strace_marker_create_sals_from_location;
983af33b 15338 ops->create_breakpoints_sal = strace_marker_create_breakpoints_sal;
5f700d83 15339 ops->decode_location = strace_marker_decode_location;
2060206e
PA
15340
15341 /* Fork catchpoints. */
15342 ops = &catch_fork_breakpoint_ops;
15343 *ops = base_breakpoint_ops;
15344 ops->insert_location = insert_catch_fork;
15345 ops->remove_location = remove_catch_fork;
15346 ops->breakpoint_hit = breakpoint_hit_catch_fork;
15347 ops->print_it = print_it_catch_fork;
15348 ops->print_one = print_one_catch_fork;
15349 ops->print_mention = print_mention_catch_fork;
15350 ops->print_recreate = print_recreate_catch_fork;
15351
15352 /* Vfork catchpoints. */
15353 ops = &catch_vfork_breakpoint_ops;
15354 *ops = base_breakpoint_ops;
15355 ops->insert_location = insert_catch_vfork;
15356 ops->remove_location = remove_catch_vfork;
15357 ops->breakpoint_hit = breakpoint_hit_catch_vfork;
15358 ops->print_it = print_it_catch_vfork;
15359 ops->print_one = print_one_catch_vfork;
15360 ops->print_mention = print_mention_catch_vfork;
15361 ops->print_recreate = print_recreate_catch_vfork;
15362
15363 /* Exec catchpoints. */
15364 ops = &catch_exec_breakpoint_ops;
15365 *ops = base_breakpoint_ops;
2060206e
PA
15366 ops->insert_location = insert_catch_exec;
15367 ops->remove_location = remove_catch_exec;
15368 ops->breakpoint_hit = breakpoint_hit_catch_exec;
15369 ops->print_it = print_it_catch_exec;
15370 ops->print_one = print_one_catch_exec;
15371 ops->print_mention = print_mention_catch_exec;
15372 ops->print_recreate = print_recreate_catch_exec;
15373
edcc5120
TT
15374 /* Solib-related catchpoints. */
15375 ops = &catch_solib_breakpoint_ops;
15376 *ops = base_breakpoint_ops;
edcc5120
TT
15377 ops->insert_location = insert_catch_solib;
15378 ops->remove_location = remove_catch_solib;
15379 ops->breakpoint_hit = breakpoint_hit_catch_solib;
15380 ops->check_status = check_status_catch_solib;
15381 ops->print_it = print_it_catch_solib;
15382 ops->print_one = print_one_catch_solib;
15383 ops->print_mention = print_mention_catch_solib;
15384 ops->print_recreate = print_recreate_catch_solib;
e7e0cddf
SS
15385
15386 ops = &dprintf_breakpoint_ops;
15387 *ops = bkpt_base_breakpoint_ops;
5c2b4418 15388 ops->re_set = dprintf_re_set;
e7e0cddf
SS
15389 ops->resources_needed = bkpt_resources_needed;
15390 ops->print_it = bkpt_print_it;
15391 ops->print_mention = bkpt_print_mention;
2d9442cc 15392 ops->print_recreate = dprintf_print_recreate;
9d6e6e84 15393 ops->after_condition_true = dprintf_after_condition_true;
cd1608cc 15394 ops->breakpoint_hit = dprintf_breakpoint_hit;
2060206e
PA
15395}
15396
8bfd80db
YQ
15397/* Chain containing all defined "enable breakpoint" subcommands. */
15398
15399static struct cmd_list_element *enablebreaklist = NULL;
15400
8588b356
SM
15401/* See breakpoint.h. */
15402
15403cmd_list_element *commands_cmd_element = nullptr;
15404
c906108c 15405void
fba45db2 15406_initialize_breakpoint (void)
c906108c
SS
15407{
15408 struct cmd_list_element *c;
15409
2060206e
PA
15410 initialize_breakpoint_ops ();
15411
76727919
TT
15412 gdb::observers::solib_unloaded.attach (disable_breakpoints_in_unloaded_shlib);
15413 gdb::observers::free_objfile.attach (disable_breakpoints_in_freed_objfile);
15414 gdb::observers::memory_changed.attach (invalidate_bp_value_on_memory_change);
84acb35a 15415
c906108c
SS
15416 breakpoint_chain = 0;
15417 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
15418 before a breakpoint is set. */
15419 breakpoint_count = 0;
15420
1042e4c0
SS
15421 tracepoint_count = 0;
15422
1bedd215
AC
15423 add_com ("ignore", class_breakpoint, ignore_command, _("\
15424Set ignore-count of breakpoint number N to COUNT.\n\
15425Usage is `ignore N COUNT'."));
c906108c 15426
8588b356
SM
15427 commands_cmd_element = add_com ("commands", class_breakpoint,
15428 commands_command, _("\
18da0c51
MG
15429Set commands to be executed when the given breakpoints are hit.\n\
15430Give a space-separated breakpoint list as argument after \"commands\".\n\
15431A list element can be a breakpoint number (e.g. `5') or a range of numbers\n\
15432(e.g. `5-7').\n\
c906108c
SS
15433With no argument, the targeted breakpoint is the last one set.\n\
15434The commands themselves follow starting on the next line.\n\
15435Type a line containing \"end\" to indicate the end of them.\n\
15436Give \"silent\" as the first line to make the breakpoint silent;\n\
1bedd215 15437then no output is printed when it is hit, except what the commands print."));
c906108c 15438
d55637df 15439 c = add_com ("condition", class_breakpoint, condition_command, _("\
1bedd215 15440Specify breakpoint number N to break only if COND is true.\n\
c906108c 15441Usage is `condition N COND', where N is an integer and COND is an\n\
1bedd215 15442expression to be evaluated whenever breakpoint N is reached."));
d55637df 15443 set_cmd_completer (c, condition_completer);
c906108c 15444
1bedd215 15445 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
31e2b00f 15446Set a temporary breakpoint.\n\
c906108c
SS
15447Like \"break\" except the breakpoint is only temporary,\n\
15448so it will be deleted when hit. Equivalent to \"break\" followed\n\
31e2b00f
AS
15449by using \"enable delete\" on the breakpoint number.\n\
15450\n"
15451BREAK_ARGS_HELP ("tbreak")));
5ba2abeb 15452 set_cmd_completer (c, location_completer);
c94fdfd0 15453
1bedd215 15454 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
a3be7890 15455Set a hardware assisted breakpoint.\n\
c906108c 15456Like \"break\" except the breakpoint requires hardware support,\n\
31e2b00f
AS
15457some target hardware may not have this support.\n\
15458\n"
15459BREAK_ARGS_HELP ("hbreak")));
5ba2abeb 15460 set_cmd_completer (c, location_completer);
c906108c 15461
1bedd215 15462 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
31e2b00f 15463Set a temporary hardware assisted breakpoint.\n\
c906108c 15464Like \"hbreak\" except the breakpoint is only temporary,\n\
31e2b00f
AS
15465so it will be deleted when hit.\n\
15466\n"
15467BREAK_ARGS_HELP ("thbreak")));
5ba2abeb 15468 set_cmd_completer (c, location_completer);
c906108c 15469
1bedd215 15470 add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
590042fc
PW
15471Enable all or some breakpoints.\n\
15472Usage: enable [BREAKPOINTNUM]...\n\
c906108c
SS
15473Give breakpoint numbers (separated by spaces) as arguments.\n\
15474With no subcommand, breakpoints are enabled until you command otherwise.\n\
15475This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 15476With a subcommand you can enable temporarily."),
c906108c 15477 &enablelist, "enable ", 1, &cmdlist);
c906108c
SS
15478
15479 add_com_alias ("en", "enable", class_breakpoint, 1);
15480
84951ab5 15481 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
590042fc
PW
15482Enable all or some breakpoints.\n\
15483Usage: enable breakpoints [BREAKPOINTNUM]...\n\
c906108c
SS
15484Give breakpoint numbers (separated by spaces) as arguments.\n\
15485This is used to cancel the effect of the \"disable\" command.\n\
89549d7f 15486May be abbreviated to simply \"enable\"."),
c5aa993b 15487 &enablebreaklist, "enable breakpoints ", 1, &enablelist);
c906108c 15488
1a966eab 15489 add_cmd ("once", no_class, enable_once_command, _("\
590042fc
PW
15490Enable some breakpoints for one hit.\n\
15491Usage: enable breakpoints once BREAKPOINTNUM...\n\
1a966eab 15492If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
c906108c
SS
15493 &enablebreaklist);
15494
1a966eab 15495 add_cmd ("delete", no_class, enable_delete_command, _("\
590042fc
PW
15496Enable some breakpoints and delete when hit.\n\
15497Usage: enable breakpoints delete BREAKPOINTNUM...\n\
1a966eab 15498If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
15499 &enablebreaklist);
15500
816338b5 15501 add_cmd ("count", no_class, enable_count_command, _("\
590042fc
PW
15502Enable some breakpoints for COUNT hits.\n\
15503Usage: enable breakpoints count COUNT BREAKPOINTNUM...\n\
816338b5
SS
15504If a breakpoint is hit while enabled in this fashion,\n\
15505the count is decremented; when it reaches zero, the breakpoint is disabled."),
15506 &enablebreaklist);
15507
1a966eab 15508 add_cmd ("delete", no_class, enable_delete_command, _("\
590042fc
PW
15509Enable some breakpoints and delete when hit.\n\
15510Usage: enable delete BREAKPOINTNUM...\n\
1a966eab 15511If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
15512 &enablelist);
15513
1a966eab 15514 add_cmd ("once", no_class, enable_once_command, _("\
590042fc
PW
15515Enable some breakpoints for one hit.\n\
15516Usage: enable once BREAKPOINTNUM...\n\
1a966eab 15517If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
816338b5
SS
15518 &enablelist);
15519
15520 add_cmd ("count", no_class, enable_count_command, _("\
590042fc
PW
15521Enable some breakpoints for COUNT hits.\n\
15522Usage: enable count COUNT BREAKPOINTNUM...\n\
816338b5
SS
15523If a breakpoint is hit while enabled in this fashion,\n\
15524the count is decremented; when it reaches zero, the breakpoint is disabled."),
c906108c
SS
15525 &enablelist);
15526
1bedd215 15527 add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
590042fc
PW
15528Disable all or some breakpoints.\n\
15529Usage: disable [BREAKPOINTNUM]...\n\
c906108c
SS
15530Arguments are breakpoint numbers with spaces in between.\n\
15531To disable all breakpoints, give no argument.\n\
64b9b334 15532A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
c906108c
SS
15533 &disablelist, "disable ", 1, &cmdlist);
15534 add_com_alias ("dis", "disable", class_breakpoint, 1);
15535 add_com_alias ("disa", "disable", class_breakpoint, 1);
c906108c 15536
1a966eab 15537 add_cmd ("breakpoints", class_alias, disable_command, _("\
590042fc
PW
15538Disable all or some breakpoints.\n\
15539Usage: disable breakpoints [BREAKPOINTNUM]...\n\
c906108c
SS
15540Arguments are breakpoint numbers with spaces in between.\n\
15541To disable all breakpoints, give no argument.\n\
64b9b334 15542A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
1a966eab 15543This command may be abbreviated \"disable\"."),
c906108c
SS
15544 &disablelist);
15545
1bedd215 15546 add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
590042fc
PW
15547Delete all or some breakpoints.\n\
15548Usage: delete [BREAKPOINTNUM]...\n\
c906108c
SS
15549Arguments are breakpoint numbers with spaces in between.\n\
15550To delete all breakpoints, give no argument.\n\
15551\n\
590042fc 15552Also a prefix command for deletion of other GDB objects."),
c906108c
SS
15553 &deletelist, "delete ", 1, &cmdlist);
15554 add_com_alias ("d", "delete", class_breakpoint, 1);
7f198e01 15555 add_com_alias ("del", "delete", class_breakpoint, 1);
c906108c 15556
1a966eab 15557 add_cmd ("breakpoints", class_alias, delete_command, _("\
590042fc
PW
15558Delete all or some breakpoints or auto-display expressions.\n\
15559Usage: delete breakpoints [BREAKPOINTNUM]...\n\
c906108c
SS
15560Arguments are breakpoint numbers with spaces in between.\n\
15561To delete all breakpoints, give no argument.\n\
1a966eab 15562This command may be abbreviated \"delete\"."),
c906108c
SS
15563 &deletelist);
15564
1bedd215 15565 add_com ("clear", class_breakpoint, clear_command, _("\
629500fa
KS
15566Clear breakpoint at specified location.\n\
15567Argument may be a linespec, explicit, or address location as described below.\n\
1bedd215
AC
15568\n\
15569With no argument, clears all breakpoints in the line that the selected frame\n\
629500fa 15570is executing in.\n"
89549d7f 15571"\n" LOCATION_HELP_STRING "\n\n\
1bedd215 15572See also the \"delete\" command which clears breakpoints by number."));
a6cc4789 15573 add_com_alias ("cl", "clear", class_breakpoint, 1);
c906108c 15574
1bedd215 15575 c = add_com ("break", class_breakpoint, break_command, _("\
629500fa 15576Set breakpoint at specified location.\n"
31e2b00f 15577BREAK_ARGS_HELP ("break")));
5ba2abeb 15578 set_cmd_completer (c, location_completer);
c94fdfd0 15579
c906108c
SS
15580 add_com_alias ("b", "break", class_run, 1);
15581 add_com_alias ("br", "break", class_run, 1);
15582 add_com_alias ("bre", "break", class_run, 1);
15583 add_com_alias ("brea", "break", class_run, 1);
15584
c906108c
SS
15585 if (dbx_commands)
15586 {
1bedd215
AC
15587 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
15588Break in function/address or break at a line in the current file."),
c5aa993b
JM
15589 &stoplist, "stop ", 1, &cmdlist);
15590 add_cmd ("in", class_breakpoint, stopin_command,
1a966eab 15591 _("Break in function or address."), &stoplist);
c5aa993b 15592 add_cmd ("at", class_breakpoint, stopat_command,
1a966eab 15593 _("Break at a line in the current file."), &stoplist);
11db9430 15594 add_com ("status", class_info, info_breakpoints_command, _("\
1bedd215 15595Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
15596The \"Type\" column indicates one of:\n\
15597\tbreakpoint - normal breakpoint\n\
15598\twatchpoint - watchpoint\n\
15599The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15600the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15601breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
15602address and file/line number respectively.\n\
15603\n\
15604Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
15605are set to the address of the last breakpoint listed unless the command\n\
15606is prefixed with \"server \".\n\n\
c906108c 15607Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 15608breakpoint set."));
c906108c
SS
15609 }
15610
11db9430 15611 add_info ("breakpoints", info_breakpoints_command, _("\
e5a67952 15612Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
c906108c
SS
15613The \"Type\" column indicates one of:\n\
15614\tbreakpoint - normal breakpoint\n\
15615\twatchpoint - watchpoint\n\
15616The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15617the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15618breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
15619address and file/line number respectively.\n\
15620\n\
15621Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
15622are set to the address of the last breakpoint listed unless the command\n\
15623is prefixed with \"server \".\n\n\
c906108c 15624Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 15625breakpoint set."));
c906108c 15626
6b04bdb7
MS
15627 add_info_alias ("b", "breakpoints", 1);
15628
1a966eab
AC
15629 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
15630Status of all breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
15631The \"Type\" column indicates one of:\n\
15632\tbreakpoint - normal breakpoint\n\
15633\twatchpoint - watchpoint\n\
15634\tlongjmp - internal breakpoint used to step through longjmp()\n\
15635\tlongjmp resume - internal breakpoint at the target of longjmp()\n\
15636\tuntil - internal breakpoint used by the \"until\" command\n\
1a966eab
AC
15637\tfinish - internal breakpoint used by the \"finish\" command\n\
15638The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
c906108c
SS
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\
1a966eab
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\
1a966eab 15647breakpoint set."),
c906108c
SS
15648 &maintenanceinfolist);
15649
44feb3ce
TT
15650 add_prefix_cmd ("catch", class_breakpoint, catch_command, _("\
15651Set catchpoints to catch events."),
15652 &catch_cmdlist, "catch ",
15653 0/*allow-unknown*/, &cmdlist);
15654
15655 add_prefix_cmd ("tcatch", class_breakpoint, tcatch_command, _("\
15656Set temporary catchpoints to catch events."),
15657 &tcatch_cmdlist, "tcatch ",
15658 0/*allow-unknown*/, &cmdlist);
15659
44feb3ce
TT
15660 add_catch_command ("fork", _("Catch calls to fork."),
15661 catch_fork_command_1,
a96d9b2e 15662 NULL,
44feb3ce
TT
15663 (void *) (uintptr_t) catch_fork_permanent,
15664 (void *) (uintptr_t) catch_fork_temporary);
15665 add_catch_command ("vfork", _("Catch calls to vfork."),
15666 catch_fork_command_1,
a96d9b2e 15667 NULL,
44feb3ce
TT
15668 (void *) (uintptr_t) catch_vfork_permanent,
15669 (void *) (uintptr_t) catch_vfork_temporary);
15670 add_catch_command ("exec", _("Catch calls to exec."),
15671 catch_exec_command_1,
a96d9b2e
SDJ
15672 NULL,
15673 CATCH_PERMANENT,
15674 CATCH_TEMPORARY);
edcc5120
TT
15675 add_catch_command ("load", _("Catch loads of shared libraries.\n\
15676Usage: catch load [REGEX]\n\
15677If REGEX is given, only stop for libraries matching the regular expression."),
15678 catch_load_command_1,
15679 NULL,
15680 CATCH_PERMANENT,
15681 CATCH_TEMPORARY);
15682 add_catch_command ("unload", _("Catch unloads of shared libraries.\n\
15683Usage: catch unload [REGEX]\n\
15684If REGEX is given, only stop for libraries matching the regular expression."),
15685 catch_unload_command_1,
15686 NULL,
15687 CATCH_PERMANENT,
15688 CATCH_TEMPORARY);
c5aa993b 15689
1bedd215
AC
15690 c = add_com ("watch", class_breakpoint, watch_command, _("\
15691Set a watchpoint for an expression.\n\
06a64a0b 15692Usage: watch [-l|-location] EXPRESSION\n\
c906108c 15693A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
15694an expression changes.\n\
15695If -l or -location is given, this evaluates EXPRESSION and watches\n\
15696the memory to which it refers."));
65d12d83 15697 set_cmd_completer (c, expression_completer);
c906108c 15698
1bedd215
AC
15699 c = add_com ("rwatch", class_breakpoint, rwatch_command, _("\
15700Set a read watchpoint for an expression.\n\
06a64a0b 15701Usage: rwatch [-l|-location] EXPRESSION\n\
c906108c 15702A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
15703an expression is read.\n\
15704If -l or -location is given, this evaluates EXPRESSION and watches\n\
15705the memory to which it refers."));
65d12d83 15706 set_cmd_completer (c, expression_completer);
c906108c 15707
1bedd215
AC
15708 c = add_com ("awatch", class_breakpoint, awatch_command, _("\
15709Set a watchpoint for an expression.\n\
06a64a0b 15710Usage: awatch [-l|-location] EXPRESSION\n\
c906108c 15711A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
15712an expression is either read or written.\n\
15713If -l or -location is given, this evaluates EXPRESSION and watches\n\
15714the memory to which it refers."));
65d12d83 15715 set_cmd_completer (c, expression_completer);
c906108c 15716
11db9430 15717 add_info ("watchpoints", info_watchpoints_command, _("\
e5a67952 15718Status of specified watchpoints (all watchpoints if no argument)."));
c906108c 15719
920d2a44
AC
15720 /* XXX: cagney/2005-02-23: This should be a boolean, and should
15721 respond to changes - contrary to the description. */
85c07804
AC
15722 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
15723 &can_use_hw_watchpoints, _("\
15724Set debugger's willingness to use watchpoint hardware."), _("\
15725Show debugger's willingness to use watchpoint hardware."), _("\
c906108c
SS
15726If zero, gdb will not use hardware for new watchpoints, even if\n\
15727such is available. (However, any hardware watchpoints that were\n\
15728created before setting this to nonzero, will continue to use watchpoint\n\
85c07804
AC
15729hardware.)"),
15730 NULL,
920d2a44 15731 show_can_use_hw_watchpoints,
85c07804 15732 &setlist, &showlist);
c906108c
SS
15733
15734 can_use_hw_watchpoints = 1;
fa8d40ab 15735
1042e4c0
SS
15736 /* Tracepoint manipulation commands. */
15737
15738 c = add_com ("trace", class_breakpoint, trace_command, _("\
629500fa 15739Set a tracepoint at specified location.\n\
1042e4c0
SS
15740\n"
15741BREAK_ARGS_HELP ("trace") "\n\
15742Do \"help tracepoints\" for info on other tracepoint commands."));
15743 set_cmd_completer (c, location_completer);
15744
15745 add_com_alias ("tp", "trace", class_alias, 0);
15746 add_com_alias ("tr", "trace", class_alias, 1);
15747 add_com_alias ("tra", "trace", class_alias, 1);
15748 add_com_alias ("trac", "trace", class_alias, 1);
15749
7a697b8d 15750 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
629500fa 15751Set a fast tracepoint at specified location.\n\
7a697b8d
SS
15752\n"
15753BREAK_ARGS_HELP ("ftrace") "\n\
15754Do \"help tracepoints\" for info on other tracepoint commands."));
15755 set_cmd_completer (c, location_completer);
15756
0fb4aa4b 15757 c = add_com ("strace", class_breakpoint, strace_command, _("\
629500fa 15758Set a static tracepoint at location or marker.\n\
0fb4aa4b
PA
15759\n\
15760strace [LOCATION] [if CONDITION]\n\
629500fa
KS
15761LOCATION may be a linespec, explicit, or address location (described below) \n\
15762or -m MARKER_ID.\n\n\
15763If a marker id is specified, probe the marker with that name. With\n\
15764no LOCATION, uses current execution address of the selected stack frame.\n\
0fb4aa4b
PA
15765Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
15766This collects arbitrary user data passed in the probe point call to the\n\
15767tracing library. You can inspect it when analyzing the trace buffer,\n\
15768by printing the $_sdata variable like any other convenience variable.\n\
15769\n\
15770CONDITION is a boolean expression.\n\
89549d7f 15771\n" LOCATION_HELP_STRING "\n\n\
d41c0fc8
PA
15772Multiple tracepoints at one place are permitted, and useful if their\n\
15773conditions are different.\n\
0fb4aa4b
PA
15774\n\
15775Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
15776Do \"help tracepoints\" for info on other tracepoint commands."));
15777 set_cmd_completer (c, location_completer);
15778
11db9430 15779 add_info ("tracepoints", info_tracepoints_command, _("\
e5a67952 15780Status of specified tracepoints (all tracepoints if no argument).\n\
1042e4c0
SS
15781Convenience variable \"$tpnum\" contains the number of the\n\
15782last tracepoint set."));
15783
15784 add_info_alias ("tp", "tracepoints", 1);
15785
15786 add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
15787Delete specified tracepoints.\n\
15788Arguments are tracepoint numbers, separated by spaces.\n\
15789No argument means delete all tracepoints."),
15790 &deletelist);
7e20dfcd 15791 add_alias_cmd ("tr", "tracepoints", class_trace, 1, &deletelist);
1042e4c0
SS
15792
15793 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
15794Disable specified tracepoints.\n\
15795Arguments are tracepoint numbers, separated by spaces.\n\
15796No argument means disable all tracepoints."),
15797 &disablelist);
15798 deprecate_cmd (c, "disable");
15799
15800 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
15801Enable specified tracepoints.\n\
15802Arguments are tracepoint numbers, separated by spaces.\n\
15803No argument means enable all tracepoints."),
15804 &enablelist);
15805 deprecate_cmd (c, "enable");
15806
15807 add_com ("passcount", class_trace, trace_pass_command, _("\
15808Set the passcount for a tracepoint.\n\
15809The trace will end when the tracepoint has been passed 'count' times.\n\
15810Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
15811if TPNUM is omitted, passcount refers to the last tracepoint defined."));
15812
6149aea9
PA
15813 add_prefix_cmd ("save", class_breakpoint, save_command,
15814 _("Save breakpoint definitions as a script."),
15815 &save_cmdlist, "save ",
15816 0/*allow-unknown*/, &cmdlist);
15817
15818 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
15819Save current breakpoint definitions as a script.\n\
cce7e648 15820This includes all types of breakpoints (breakpoints, watchpoints,\n\
6149aea9
PA
15821catchpoints, tracepoints). Use the 'source' command in another debug\n\
15822session to restore them."),
15823 &save_cmdlist);
15824 set_cmd_completer (c, filename_completer);
15825
15826 c = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
1042e4c0 15827Save current tracepoint definitions as a script.\n\
6149aea9
PA
15828Use the 'source' command in another debug session to restore them."),
15829 &save_cmdlist);
1042e4c0
SS
15830 set_cmd_completer (c, filename_completer);
15831
6149aea9
PA
15832 c = add_com_alias ("save-tracepoints", "save tracepoints", class_trace, 0);
15833 deprecate_cmd (c, "save tracepoints");
15834
1bedd215 15835 add_prefix_cmd ("breakpoint", class_maintenance, set_breakpoint_cmd, _("\
590042fc 15836Breakpoint specific settings.\n\
fa8d40ab 15837Configure various breakpoint-specific variables such as\n\
590042fc 15838pending breakpoint behavior."),
fa8d40ab
JJ
15839 &breakpoint_set_cmdlist, "set breakpoint ",
15840 0/*allow-unknown*/, &setlist);
1bedd215 15841 add_prefix_cmd ("breakpoint", class_maintenance, show_breakpoint_cmd, _("\
590042fc 15842Breakpoint specific settings.\n\
fa8d40ab 15843Configure various breakpoint-specific variables such as\n\
590042fc 15844pending breakpoint behavior."),
fa8d40ab
JJ
15845 &breakpoint_show_cmdlist, "show breakpoint ",
15846 0/*allow-unknown*/, &showlist);
15847
7915a72c
AC
15848 add_setshow_auto_boolean_cmd ("pending", no_class,
15849 &pending_break_support, _("\
15850Set debugger's behavior regarding pending breakpoints."), _("\
15851Show debugger's behavior regarding pending breakpoints."), _("\
6e1d7d6c
AC
15852If on, an unrecognized breakpoint location will cause gdb to create a\n\
15853pending breakpoint. If off, an unrecognized breakpoint location results in\n\
15854an error. If auto, an unrecognized breakpoint location results in a\n\
7915a72c 15855user-query to see if a pending breakpoint should be created."),
2c5b56ce 15856 NULL,
920d2a44 15857 show_pending_break_support,
6e1d7d6c
AC
15858 &breakpoint_set_cmdlist,
15859 &breakpoint_show_cmdlist);
fa8d40ab
JJ
15860
15861 pending_break_support = AUTO_BOOLEAN_AUTO;
765dc015
VP
15862
15863 add_setshow_boolean_cmd ("auto-hw", no_class,
15864 &automatic_hardware_breakpoints, _("\
15865Set automatic usage of hardware breakpoints."), _("\
15866Show automatic usage of hardware breakpoints."), _("\
15867If set, the debugger will automatically use hardware breakpoints for\n\
15868breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
15869a warning will be emitted for such breakpoints."),
15870 NULL,
15871 show_automatic_hardware_breakpoints,
15872 &breakpoint_set_cmdlist,
15873 &breakpoint_show_cmdlist);
74960c60 15874
a25a5a45
PA
15875 add_setshow_boolean_cmd ("always-inserted", class_support,
15876 &always_inserted_mode, _("\
74960c60
VP
15877Set mode for inserting breakpoints."), _("\
15878Show mode for inserting breakpoints."), _("\
a25a5a45
PA
15879When this mode is on, breakpoints are inserted immediately as soon as\n\
15880they're created, kept inserted even when execution stops, and removed\n\
15881only when the user deletes them. When this mode is off (the default),\n\
15882breakpoints are inserted only when execution continues, and removed\n\
15883when execution stops."),
72d0e2c5
YQ
15884 NULL,
15885 &show_always_inserted_mode,
15886 &breakpoint_set_cmdlist,
15887 &breakpoint_show_cmdlist);
f1310107 15888
b775012e
LM
15889 add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
15890 condition_evaluation_enums,
15891 &condition_evaluation_mode_1, _("\
15892Set mode of breakpoint condition evaluation."), _("\
15893Show mode of breakpoint condition evaluation."), _("\
d1cda5d9 15894When this is set to \"host\", breakpoint conditions will be\n\
b775012e
LM
15895evaluated on the host's side by GDB. When it is set to \"target\",\n\
15896breakpoint conditions will be downloaded to the target (if the target\n\
15897supports such feature) and conditions will be evaluated on the target's side.\n\
15898If this is set to \"auto\" (default), this will be automatically set to\n\
15899\"target\" if it supports condition evaluation, otherwise it will\n\
15900be set to \"gdb\""),
15901 &set_condition_evaluation_mode,
15902 &show_condition_evaluation_mode,
15903 &breakpoint_set_cmdlist,
15904 &breakpoint_show_cmdlist);
15905
f1310107
TJB
15906 add_com ("break-range", class_breakpoint, break_range_command, _("\
15907Set a breakpoint for an address range.\n\
15908break-range START-LOCATION, END-LOCATION\n\
15909where START-LOCATION and END-LOCATION can be one of the following:\n\
15910 LINENUM, for that line in the current file,\n\
15911 FILE:LINENUM, for that line in that file,\n\
15912 +OFFSET, for that number of lines after the current line\n\
15913 or the start of the range\n\
15914 FUNCTION, for the first line in that function,\n\
15915 FILE:FUNCTION, to distinguish among like-named static functions.\n\
15916 *ADDRESS, for the instruction at that address.\n\
15917\n\
15918The breakpoint will stop execution of the inferior whenever it executes\n\
15919an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
15920range (including START-LOCATION and END-LOCATION)."));
15921
e7e0cddf 15922 c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
629500fa 15923Set a dynamic printf at specified location.\n\
e7e0cddf 15924dprintf location,format string,arg1,arg2,...\n\
629500fa
KS
15925location may be a linespec, explicit, or address location.\n"
15926"\n" LOCATION_HELP_STRING));
e7e0cddf
SS
15927 set_cmd_completer (c, location_completer);
15928
15929 add_setshow_enum_cmd ("dprintf-style", class_support,
15930 dprintf_style_enums, &dprintf_style, _("\
15931Set the style of usage for dynamic printf."), _("\
15932Show the style of usage for dynamic printf."), _("\
15933This setting chooses how GDB will do a dynamic printf.\n\
15934If the value is \"gdb\", then the printing is done by GDB to its own\n\
15935console, as with the \"printf\" command.\n\
15936If the value is \"call\", the print is done by calling a function in your\n\
15937program; by default printf(), but you can choose a different function or\n\
15938output stream by setting dprintf-function and dprintf-channel."),
15939 update_dprintf_commands, NULL,
15940 &setlist, &showlist);
15941
15942 dprintf_function = xstrdup ("printf");
15943 add_setshow_string_cmd ("dprintf-function", class_support,
15944 &dprintf_function, _("\
590042fc
PW
15945Set the function to use for dynamic printf."), _("\
15946Show the function to use for dynamic printf."), NULL,
e7e0cddf
SS
15947 update_dprintf_commands, NULL,
15948 &setlist, &showlist);
15949
15950 dprintf_channel = xstrdup ("");
15951 add_setshow_string_cmd ("dprintf-channel", class_support,
15952 &dprintf_channel, _("\
590042fc
PW
15953Set the channel to use for dynamic printf."), _("\
15954Show the channel to use for dynamic printf."), NULL,
e7e0cddf
SS
15955 update_dprintf_commands, NULL,
15956 &setlist, &showlist);
15957
d3ce09f5
SS
15958 add_setshow_boolean_cmd ("disconnected-dprintf", no_class,
15959 &disconnected_dprintf, _("\
15960Set whether dprintf continues after GDB disconnects."), _("\
15961Show whether dprintf continues after GDB disconnects."), _("\
15962Use this to let dprintf commands continue to hit and produce output\n\
15963even if GDB disconnects or detaches from the target."),
15964 NULL,
15965 NULL,
15966 &setlist, &showlist);
15967
15968 add_com ("agent-printf", class_vars, agent_printf_command, _("\
590042fc
PW
15969Target agent only formatted printing, like the C \"printf\" function.\n\
15970Usage: agent-printf \"format string\", ARG1, ARG2, ARG3, ..., ARGN\n\
15971This supports most C printf format specifications, like %s, %d, etc.\n\
15972This is useful for formatted output in user-defined commands."));
d3ce09f5 15973
491144b5 15974 automatic_hardware_breakpoints = true;
f3b1572e 15975
76727919
TT
15976 gdb::observers::about_to_proceed.attach (breakpoint_about_to_proceed);
15977 gdb::observers::thread_exit.attach (remove_threaded_breakpoints);
c906108c 15978}
This page took 5.077654 seconds and 4 git commands to generate.