]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/breakpoint.c
"catch catch/throw/rethrow", breakpoint -> catchpoint
[thirdparty/binutils-gdb.git] / gdb / breakpoint.c
CommitLineData
c906108c 1/* Everything about breakpoints, for GDB.
8926118c 2
42a4f53d 3 Copyright (C) 1986-2019 Free Software Foundation, Inc.
c906108c 4
c5aa993b 5 This file is part of GDB.
c906108c 6
c5aa993b
JM
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
a9762ec7 9 the Free Software Foundation; either version 3 of the License, or
c5aa993b 10 (at your option) any later version.
c906108c 11
c5aa993b
JM
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
c906108c 16
c5aa993b 17 You should have received a copy of the GNU General Public License
a9762ec7 18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c
SS
19
20#include "defs.h"
d55e5aa6 21#include "arch-utils.h"
4de283e4
TT
22#include <ctype.h>
23#include "hashtab.h"
24#include "symtab.h"
25#include "frame.h"
c906108c 26#include "breakpoint.h"
4de283e4
TT
27#include "tracepoint.h"
28#include "gdbtypes.h"
c906108c 29#include "expression.h"
d55e5aa6 30#include "gdbcore.h"
4de283e4
TT
31#include "gdbcmd.h"
32#include "value.h"
33#include "command.h"
c906108c 34#include "inferior.h"
45741a9c 35#include "infrun.h"
4de283e4
TT
36#include "gdbthread.h"
37#include "target.h"
c906108c 38#include "language.h"
4de283e4
TT
39#include "gdb-demangle.h"
40#include "filenames.h"
41#include "annotate.h"
42#include "symfile.h"
d55e5aa6 43#include "objfiles.h"
4de283e4
TT
44#include "source.h"
45#include "linespec.h"
46#include "completer.h"
47#include "ui-out.h"
48#include "cli/cli-script.h"
49#include "block.h"
50#include "solib.h"
51#include "solist.h"
d55e5aa6 52#include "observable.h"
4de283e4
TT
53#include "memattr.h"
54#include "ada-lang.h"
55#include "top.h"
56#include "valprint.h"
57#include "jit.h"
65d79d4b 58#include "parser-defs.h"
4de283e4 59#include "gdb_regex.h"
55aa24fb 60#include "probe.h"
4de283e4
TT
61#include "cli/cli-utils.h"
62#include "continuations.h"
d55e5aa6 63#include "stack.h"
4de283e4
TT
64#include "skip.h"
65#include "ax-gdb.h"
66#include "dummy-frame.h"
67#include "interps.h"
268a13a5 68#include "gdbsupport/format.h"
cfc31633 69#include "thread-fsm.h"
5d5658a1 70#include "tid-parse.h"
4de283e4
TT
71#include "cli/cli-style.h"
72#include "mi/mi-main.h"
d3ce09f5 73
1042e4c0
SS
74/* readline include files */
75#include "readline/readline.h"
76#include "readline/history.h"
77
78/* readline defines this. */
79#undef savestring
80
034dad6f 81#include "mi/mi-common.h"
6dddc817 82#include "extension.h"
325fac50 83#include <algorithm>
5ed8105e 84#include "progspace-and-thread.h"
268a13a5
TT
85#include "gdbsupport/array-view.h"
86#include "gdbsupport/gdb_optional.h"
104c1213 87
4a64f543 88/* Prototypes for local functions. */
c906108c 89
896b6bda 90static void map_breakpoint_numbers (const char *,
48649e1b 91 gdb::function_view<void (breakpoint *)>);
c906108c 92
348d480f
PA
93static void breakpoint_re_set_default (struct breakpoint *);
94
f00aae0f
KS
95static void
96 create_sals_from_location_default (const struct event_location *location,
97 struct linespec_result *canonical,
98 enum bptype type_wanted);
983af33b
SDJ
99
100static void create_breakpoints_sal_default (struct gdbarch *,
101 struct linespec_result *,
e1e01040
PA
102 gdb::unique_xmalloc_ptr<char>,
103 gdb::unique_xmalloc_ptr<char>,
104 enum bptype,
983af33b
SDJ
105 enum bpdisp, int, int,
106 int,
107 const struct breakpoint_ops *,
44f238bb 108 int, int, int, unsigned);
983af33b 109
6c5b2ebe
PA
110static std::vector<symtab_and_line> decode_location_default
111 (struct breakpoint *b, const struct event_location *location,
112 struct program_space *search_pspace);
983af33b 113
a6535de1
TT
114static int can_use_hardware_watchpoint
115 (const std::vector<value_ref_ptr> &vals);
c906108c 116
a14ed312 117static void mention (struct breakpoint *);
c906108c 118
348d480f
PA
119static struct breakpoint *set_raw_breakpoint_without_location (struct gdbarch *,
120 enum bptype,
c0a91b2b 121 const struct breakpoint_ops *);
3742cc8b
YQ
122static struct bp_location *add_location_to_breakpoint (struct breakpoint *,
123 const struct symtab_and_line *);
124
4a64f543
MS
125/* This function is used in gdbtk sources and thus can not be made
126 static. */
63c252f8 127struct breakpoint *set_raw_breakpoint (struct gdbarch *gdbarch,
348d480f 128 struct symtab_and_line,
c0a91b2b
TT
129 enum bptype,
130 const struct breakpoint_ops *);
c906108c 131
06edf0c0
PA
132static struct breakpoint *
133 momentary_breakpoint_from_master (struct breakpoint *orig,
134 enum bptype type,
a1aa2221
LM
135 const struct breakpoint_ops *ops,
136 int loc_enabled);
06edf0c0 137
76897487
KB
138static void breakpoint_adjustment_warning (CORE_ADDR, CORE_ADDR, int, int);
139
a6d9a66e
UW
140static CORE_ADDR adjust_breakpoint_address (struct gdbarch *gdbarch,
141 CORE_ADDR bpaddr,
88f7da05 142 enum bptype bptype);
76897487 143
6c95b8df
PA
144static void describe_other_breakpoints (struct gdbarch *,
145 struct program_space *, CORE_ADDR,
5af949e3 146 struct obj_section *, int);
c906108c 147
85d721b8
PA
148static int watchpoint_locations_match (struct bp_location *loc1,
149 struct bp_location *loc2);
150
f1310107 151static int breakpoint_location_address_match (struct bp_location *bl,
accd0bcd 152 const struct address_space *aspace,
f1310107
TJB
153 CORE_ADDR addr);
154
d35ae833 155static int breakpoint_location_address_range_overlap (struct bp_location *,
accd0bcd 156 const address_space *,
d35ae833
PA
157 CORE_ADDR, int);
158
834c0d03 159static int remove_breakpoint (struct bp_location *);
b2b6a7da 160static int remove_breakpoint_1 (struct bp_location *, enum remove_bp_reason);
c906108c 161
e514a9d6 162static enum print_stop_action print_bp_stop_message (bpstat bs);
c906108c 163
a14ed312 164static int hw_breakpoint_used_count (void);
c906108c 165
a1398e0c
PA
166static int hw_watchpoint_use_count (struct breakpoint *);
167
168static int hw_watchpoint_used_count_others (struct breakpoint *except,
169 enum bptype type,
170 int *other_type_used);
c906108c 171
816338b5
SS
172static void enable_breakpoint_disp (struct breakpoint *, enum bpdisp,
173 int count);
c906108c 174
fe3f5fa8 175static void free_bp_location (struct bp_location *loc);
f431efe5
PA
176static void incref_bp_location (struct bp_location *loc);
177static void decref_bp_location (struct bp_location **loc);
fe3f5fa8 178
39d61571 179static struct bp_location *allocate_bp_location (struct breakpoint *bpt);
a5606eee 180
44702360
PA
181/* update_global_location_list's modes of operation wrt to whether to
182 insert locations now. */
183enum ugll_insert_mode
184{
185 /* Don't insert any breakpoint locations into the inferior, only
186 remove already-inserted locations that no longer should be
187 inserted. Functions that delete a breakpoint or breakpoints
188 should specify this mode, so that deleting a breakpoint doesn't
189 have the side effect of inserting the locations of other
190 breakpoints that are marked not-inserted, but should_be_inserted
191 returns true on them.
192
193 This behavior is useful is situations close to tear-down -- e.g.,
194 after an exec, while the target still has execution, but
195 breakpoint shadows of the previous executable image should *NOT*
196 be restored to the new image; or before detaching, where the
197 target still has execution and wants to delete breakpoints from
198 GDB's lists, and all breakpoints had already been removed from
199 the inferior. */
200 UGLL_DONT_INSERT,
201
a25a5a45
PA
202 /* May insert breakpoints iff breakpoints_should_be_inserted_now
203 claims breakpoints should be inserted now. */
04086b45
PA
204 UGLL_MAY_INSERT,
205
a25a5a45
PA
206 /* Insert locations now, irrespective of
207 breakpoints_should_be_inserted_now. E.g., say all threads are
208 stopped right now, and the user did "continue". We need to
209 insert breakpoints _before_ resuming the target, but
210 UGLL_MAY_INSERT wouldn't insert them, because
211 breakpoints_should_be_inserted_now returns false at that point,
212 as no thread is running yet. */
04086b45 213 UGLL_INSERT
44702360
PA
214};
215
216static void update_global_location_list (enum ugll_insert_mode);
a5606eee 217
44702360 218static void update_global_location_list_nothrow (enum ugll_insert_mode);
74960c60 219
d77f58be 220static int is_hardware_watchpoint (const struct breakpoint *bpt);
74960c60
VP
221
222static void insert_breakpoint_locations (void);
a5606eee 223
0b39b52e 224static void trace_pass_command (const char *, int);
1042e4c0 225
558a9d82
YQ
226static void set_tracepoint_count (int num);
227
9c06b0b4
TJB
228static int is_masked_watchpoint (const struct breakpoint *b);
229
b775012e
LM
230static struct bp_location **get_first_locp_gte_addr (CORE_ADDR address);
231
983af33b
SDJ
232/* Return 1 if B refers to a static tracepoint set by marker ("-m"), zero
233 otherwise. */
234
235static int strace_marker_p (struct breakpoint *b);
0fb4aa4b 236
2060206e
PA
237/* The breakpoint_ops structure to be inherited by all breakpoint_ops
238 that are implemented on top of software or hardware breakpoints
239 (user breakpoints, internal and momentary breakpoints, etc.). */
240static struct breakpoint_ops bkpt_base_breakpoint_ops;
241
242/* Internal breakpoints class type. */
06edf0c0 243static struct breakpoint_ops internal_breakpoint_ops;
2060206e
PA
244
245/* Momentary breakpoints class type. */
06edf0c0
PA
246static struct breakpoint_ops momentary_breakpoint_ops;
247
2060206e
PA
248/* The breakpoint_ops structure to be used in regular user created
249 breakpoints. */
250struct breakpoint_ops bkpt_breakpoint_ops;
251
55aa24fb
SDJ
252/* Breakpoints set on probes. */
253static struct breakpoint_ops bkpt_probe_breakpoint_ops;
254
e7e0cddf 255/* Dynamic printf class type. */
c5867ab6 256struct breakpoint_ops dprintf_breakpoint_ops;
e7e0cddf 257
d3ce09f5
SS
258/* The style in which to perform a dynamic printf. This is a user
259 option because different output options have different tradeoffs;
260 if GDB does the printing, there is better error handling if there
261 is a problem with any of the arguments, but using an inferior
262 function lets you have special-purpose printers and sending of
263 output to the same place as compiled-in print functions. */
264
265static const char dprintf_style_gdb[] = "gdb";
266static const char dprintf_style_call[] = "call";
267static const char dprintf_style_agent[] = "agent";
268static const char *const dprintf_style_enums[] = {
269 dprintf_style_gdb,
270 dprintf_style_call,
271 dprintf_style_agent,
272 NULL
273};
274static const char *dprintf_style = dprintf_style_gdb;
275
276/* The function to use for dynamic printf if the preferred style is to
277 call into the inferior. The value is simply a string that is
278 copied into the command, so it can be anything that GDB can
279 evaluate to a callable address, not necessarily a function name. */
280
bde6261a 281static char *dprintf_function;
d3ce09f5
SS
282
283/* The channel to use for dynamic printf if the preferred style is to
284 call into the inferior; if a nonempty string, it will be passed to
285 the call as the first argument, with the format string as the
286 second. As with the dprintf function, this can be anything that
287 GDB knows how to evaluate, so in addition to common choices like
288 "stderr", this could be an app-specific expression like
289 "mystreams[curlogger]". */
290
bde6261a 291static char *dprintf_channel;
d3ce09f5
SS
292
293/* True if dprintf commands should continue to operate even if GDB
294 has disconnected. */
295static int disconnected_dprintf = 1;
296
5cea2a26
PA
297struct command_line *
298breakpoint_commands (struct breakpoint *b)
299{
d1b0a7bf 300 return b->commands ? b->commands.get () : NULL;
5cea2a26 301}
3daf8fe5 302
f3b1572e
PA
303/* Flag indicating that a command has proceeded the inferior past the
304 current breakpoint. */
305
306static int breakpoint_proceeded;
307
956a9fb9 308const char *
2cec12e5
AR
309bpdisp_text (enum bpdisp disp)
310{
4a64f543
MS
311 /* NOTE: the following values are a part of MI protocol and
312 represent values of 'disp' field returned when inferior stops at
313 a breakpoint. */
bc043ef3 314 static const char * const bpdisps[] = {"del", "dstp", "dis", "keep"};
cc59ec59 315
2cec12e5
AR
316 return bpdisps[(int) disp];
317}
c906108c 318
4a64f543 319/* Prototypes for exported functions. */
c906108c 320/* If FALSE, gdb will not use hardware support for watchpoints, even
4a64f543 321 if such is available. */
c906108c
SS
322static int can_use_hw_watchpoints;
323
920d2a44
AC
324static void
325show_can_use_hw_watchpoints (struct ui_file *file, int from_tty,
326 struct cmd_list_element *c,
327 const char *value)
328{
3e43a32a
MS
329 fprintf_filtered (file,
330 _("Debugger's willingness to use "
331 "watchpoint hardware is %s.\n"),
920d2a44
AC
332 value);
333}
334
fa8d40ab
JJ
335/* If AUTO_BOOLEAN_FALSE, gdb will not attempt to create pending breakpoints.
336 If AUTO_BOOLEAN_TRUE, gdb will automatically create pending breakpoints
4a64f543 337 for unrecognized breakpoint locations.
fa8d40ab
JJ
338 If AUTO_BOOLEAN_AUTO, gdb will query when breakpoints are unrecognized. */
339static enum auto_boolean pending_break_support;
920d2a44
AC
340static void
341show_pending_break_support (struct ui_file *file, int from_tty,
342 struct cmd_list_element *c,
343 const char *value)
344{
3e43a32a
MS
345 fprintf_filtered (file,
346 _("Debugger's behavior regarding "
347 "pending breakpoints is %s.\n"),
920d2a44
AC
348 value);
349}
fa8d40ab 350
765dc015 351/* If 1, gdb will automatically use hardware breakpoints for breakpoints
4a64f543 352 set with "break" but falling in read-only memory.
765dc015
VP
353 If 0, gdb will warn about such breakpoints, but won't automatically
354 use hardware breakpoints. */
355static int automatic_hardware_breakpoints;
356static void
357show_automatic_hardware_breakpoints (struct ui_file *file, int from_tty,
358 struct cmd_list_element *c,
359 const char *value)
360{
3e43a32a
MS
361 fprintf_filtered (file,
362 _("Automatic usage of hardware breakpoints is %s.\n"),
765dc015
VP
363 value);
364}
365
a25a5a45
PA
366/* If on, GDB keeps breakpoints inserted even if the inferior is
367 stopped, and immediately inserts any new breakpoints as soon as
368 they're created. If off (default), GDB keeps breakpoints off of
369 the target as long as possible. That is, it delays inserting
370 breakpoints until the next resume, and removes them again when the
371 target fully stops. This is a bit safer in case GDB crashes while
372 processing user input. */
373static int always_inserted_mode = 0;
72d0e2c5 374
33e5cbd6 375static void
74960c60 376show_always_inserted_mode (struct ui_file *file, int from_tty,
33e5cbd6 377 struct cmd_list_element *c, const char *value)
74960c60 378{
a25a5a45
PA
379 fprintf_filtered (file, _("Always inserted breakpoint mode is %s.\n"),
380 value);
74960c60
VP
381}
382
b57bacec
PA
383/* See breakpoint.h. */
384
33e5cbd6 385int
a25a5a45 386breakpoints_should_be_inserted_now (void)
33e5cbd6 387{
a25a5a45
PA
388 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
389 {
390 /* If breakpoints are global, they should be inserted even if no
391 thread under gdb's control is running, or even if there are
392 no threads under GDB's control yet. */
393 return 1;
394 }
395 else if (target_has_execution)
396 {
a25a5a45
PA
397 if (always_inserted_mode)
398 {
399 /* The user wants breakpoints inserted even if all threads
400 are stopped. */
401 return 1;
402 }
403
b57bacec
PA
404 if (threads_are_executing ())
405 return 1;
372316f1
PA
406
407 /* Don't remove breakpoints yet if, even though all threads are
408 stopped, we still have events to process. */
08036331 409 for (thread_info *tp : all_non_exited_threads ())
372316f1
PA
410 if (tp->resumed
411 && tp->suspend.waitstatus_pending_p)
412 return 1;
a25a5a45
PA
413 }
414 return 0;
33e5cbd6 415}
765dc015 416
b775012e
LM
417static const char condition_evaluation_both[] = "host or target";
418
419/* Modes for breakpoint condition evaluation. */
420static const char condition_evaluation_auto[] = "auto";
421static const char condition_evaluation_host[] = "host";
422static const char condition_evaluation_target[] = "target";
423static const char *const condition_evaluation_enums[] = {
424 condition_evaluation_auto,
425 condition_evaluation_host,
426 condition_evaluation_target,
427 NULL
428};
429
430/* Global that holds the current mode for breakpoint condition evaluation. */
431static const char *condition_evaluation_mode_1 = condition_evaluation_auto;
432
433/* Global that we use to display information to the user (gets its value from
434 condition_evaluation_mode_1. */
435static const char *condition_evaluation_mode = condition_evaluation_auto;
436
437/* Translate a condition evaluation mode MODE into either "host"
438 or "target". This is used mostly to translate from "auto" to the
439 real setting that is being used. It returns the translated
440 evaluation mode. */
441
442static const char *
443translate_condition_evaluation_mode (const char *mode)
444{
445 if (mode == condition_evaluation_auto)
446 {
447 if (target_supports_evaluation_of_breakpoint_conditions ())
448 return condition_evaluation_target;
449 else
450 return condition_evaluation_host;
451 }
452 else
453 return mode;
454}
455
456/* Discovers what condition_evaluation_auto translates to. */
457
458static const char *
459breakpoint_condition_evaluation_mode (void)
460{
461 return translate_condition_evaluation_mode (condition_evaluation_mode);
462}
463
464/* Return true if GDB should evaluate breakpoint conditions or false
465 otherwise. */
466
467static int
468gdb_evaluates_breakpoint_condition_p (void)
469{
470 const char *mode = breakpoint_condition_evaluation_mode ();
471
472 return (mode == condition_evaluation_host);
473}
474
c906108c
SS
475/* Are we executing breakpoint commands? */
476static int executing_breakpoint_commands;
477
c02f5703
MS
478/* Are overlay event breakpoints enabled? */
479static int overlay_events_enabled;
480
e09342b5
TJB
481/* See description in breakpoint.h. */
482int target_exact_watchpoints = 0;
483
c906108c 484/* Walk the following statement or block through all breakpoints.
e5dd4106 485 ALL_BREAKPOINTS_SAFE does so even if the statement deletes the
4a64f543 486 current breakpoint. */
c906108c 487
5c44784c 488#define ALL_BREAKPOINTS(B) for (B = breakpoint_chain; B; B = B->next)
c906108c 489
5c44784c
JM
490#define ALL_BREAKPOINTS_SAFE(B,TMP) \
491 for (B = breakpoint_chain; \
492 B ? (TMP=B->next, 1): 0; \
493 B = TMP)
c906108c 494
4a64f543
MS
495/* Similar iterator for the low-level breakpoints. SAFE variant is
496 not provided so update_global_location_list must not be called
497 while executing the block of ALL_BP_LOCATIONS. */
7cc221ef 498
876fa593 499#define ALL_BP_LOCATIONS(B,BP_TMP) \
f5336ca5
PA
500 for (BP_TMP = bp_locations; \
501 BP_TMP < bp_locations + bp_locations_count && (B = *BP_TMP);\
876fa593 502 BP_TMP++)
7cc221ef 503
b775012e
LM
504/* Iterates through locations with address ADDRESS for the currently selected
505 program space. BP_LOCP_TMP points to each object. BP_LOCP_START points
506 to where the loop should start from.
507 If BP_LOCP_START is a NULL pointer, the macro automatically seeks the
508 appropriate location to start with. */
509
510#define ALL_BP_LOCATIONS_AT_ADDR(BP_LOCP_TMP, BP_LOCP_START, ADDRESS) \
511 for (BP_LOCP_START = BP_LOCP_START == NULL ? get_first_locp_gte_addr (ADDRESS) : BP_LOCP_START, \
512 BP_LOCP_TMP = BP_LOCP_START; \
513 BP_LOCP_START \
f5336ca5 514 && (BP_LOCP_TMP < bp_locations + bp_locations_count \
b775012e
LM
515 && (*BP_LOCP_TMP)->address == ADDRESS); \
516 BP_LOCP_TMP++)
517
1042e4c0
SS
518/* Iterator for tracepoints only. */
519
520#define ALL_TRACEPOINTS(B) \
521 for (B = breakpoint_chain; B; B = B->next) \
d77f58be 522 if (is_tracepoint (B))
1042e4c0 523
7cc221ef 524/* Chains of all breakpoints defined. */
c906108c
SS
525
526struct breakpoint *breakpoint_chain;
527
f5336ca5 528/* Array is sorted by bp_locations_compare - primarily by the ADDRESS. */
876fa593 529
f5336ca5 530static struct bp_location **bp_locations;
876fa593 531
f5336ca5 532/* Number of elements of BP_LOCATIONS. */
876fa593 533
f5336ca5 534static unsigned bp_locations_count;
876fa593 535
4a64f543 536/* Maximum alignment offset between bp_target_info.PLACED_ADDRESS and
f5336ca5 537 ADDRESS for the current elements of BP_LOCATIONS which get a valid
4a64f543 538 result from bp_location_has_shadow. You can use it for roughly
f5336ca5 539 limiting the subrange of BP_LOCATIONS to scan for shadow bytes for
4a64f543 540 an address you need to read. */
876fa593 541
f5336ca5 542static CORE_ADDR bp_locations_placed_address_before_address_max;
876fa593 543
4a64f543
MS
544/* Maximum offset plus alignment between bp_target_info.PLACED_ADDRESS
545 + bp_target_info.SHADOW_LEN and ADDRESS for the current elements of
f5336ca5
PA
546 BP_LOCATIONS which get a valid result from bp_location_has_shadow.
547 You can use it for roughly limiting the subrange of BP_LOCATIONS to
4a64f543 548 scan for shadow bytes for an address you need to read. */
876fa593 549
f5336ca5 550static CORE_ADDR bp_locations_shadow_len_after_address_max;
7cc221ef 551
4a64f543 552/* The locations that no longer correspond to any breakpoint, unlinked
f5336ca5
PA
553 from the bp_locations array, but for which a hit may still be
554 reported by a target. */
1123588c 555static std::vector<bp_location *> moribund_locations;
20874c92 556
c906108c
SS
557/* Number of last breakpoint made. */
558
95a42b64
TT
559static int breakpoint_count;
560
86b17b60
PA
561/* The value of `breakpoint_count' before the last command that
562 created breakpoints. If the last (break-like) command created more
563 than one breakpoint, then the difference between BREAKPOINT_COUNT
564 and PREV_BREAKPOINT_COUNT is more than one. */
565static int prev_breakpoint_count;
c906108c 566
1042e4c0
SS
567/* Number of last tracepoint made. */
568
95a42b64 569static int tracepoint_count;
1042e4c0 570
6149aea9
PA
571static struct cmd_list_element *breakpoint_set_cmdlist;
572static struct cmd_list_element *breakpoint_show_cmdlist;
9291a0cd 573struct cmd_list_element *save_cmdlist;
6149aea9 574
badd37ce
SDJ
575/* See declaration at breakpoint.h. */
576
577struct breakpoint *
578breakpoint_find_if (int (*func) (struct breakpoint *b, void *d),
579 void *user_data)
580{
581 struct breakpoint *b = NULL;
582
583 ALL_BREAKPOINTS (b)
584 {
585 if (func (b, user_data) != 0)
586 break;
587 }
588
589 return b;
590}
591
468d015d
JJ
592/* Return whether a breakpoint is an active enabled breakpoint. */
593static int
594breakpoint_enabled (struct breakpoint *b)
595{
0d381245 596 return (b->enable_state == bp_enabled);
468d015d
JJ
597}
598
c906108c
SS
599/* Set breakpoint count to NUM. */
600
95a42b64 601static void
fba45db2 602set_breakpoint_count (int num)
c906108c 603{
86b17b60 604 prev_breakpoint_count = breakpoint_count;
c906108c 605 breakpoint_count = num;
4fa62494 606 set_internalvar_integer (lookup_internalvar ("bpnum"), num);
c906108c
SS
607}
608
86b17b60
PA
609/* Used by `start_rbreak_breakpoints' below, to record the current
610 breakpoint count before "rbreak" creates any breakpoint. */
611static int rbreak_start_breakpoint_count;
612
95a42b64
TT
613/* Called at the start an "rbreak" command to record the first
614 breakpoint made. */
86b17b60 615
c80049d3 616scoped_rbreak_breakpoints::scoped_rbreak_breakpoints ()
95a42b64 617{
86b17b60 618 rbreak_start_breakpoint_count = breakpoint_count;
95a42b64
TT
619}
620
621/* Called at the end of an "rbreak" command to record the last
622 breakpoint made. */
86b17b60 623
c80049d3 624scoped_rbreak_breakpoints::~scoped_rbreak_breakpoints ()
95a42b64 625{
86b17b60 626 prev_breakpoint_count = rbreak_start_breakpoint_count;
95a42b64
TT
627}
628
4a64f543 629/* Used in run_command to zero the hit count when a new run starts. */
c906108c
SS
630
631void
fba45db2 632clear_breakpoint_hit_counts (void)
c906108c
SS
633{
634 struct breakpoint *b;
635
636 ALL_BREAKPOINTS (b)
637 b->hit_count = 0;
638}
639
c906108c 640\f
48cb2d85
VP
641/* Return the breakpoint with the specified number, or NULL
642 if the number does not refer to an existing breakpoint. */
643
644struct breakpoint *
645get_breakpoint (int num)
646{
647 struct breakpoint *b;
648
649 ALL_BREAKPOINTS (b)
650 if (b->number == num)
651 return b;
652
653 return NULL;
654}
5c44784c 655
c906108c 656\f
adc36818 657
b775012e
LM
658/* Mark locations as "conditions have changed" in case the target supports
659 evaluating conditions on its side. */
660
661static void
662mark_breakpoint_modified (struct breakpoint *b)
663{
664 struct bp_location *loc;
665
666 /* This is only meaningful if the target is
667 evaluating conditions and if the user has
668 opted for condition evaluation on the target's
669 side. */
670 if (gdb_evaluates_breakpoint_condition_p ()
671 || !target_supports_evaluation_of_breakpoint_conditions ())
672 return;
673
674 if (!is_breakpoint (b))
675 return;
676
677 for (loc = b->loc; loc; loc = loc->next)
678 loc->condition_changed = condition_modified;
679}
680
681/* Mark location as "conditions have changed" in case the target supports
682 evaluating conditions on its side. */
683
684static void
685mark_breakpoint_location_modified (struct bp_location *loc)
686{
687 /* This is only meaningful if the target is
688 evaluating conditions and if the user has
689 opted for condition evaluation on the target's
690 side. */
691 if (gdb_evaluates_breakpoint_condition_p ()
692 || !target_supports_evaluation_of_breakpoint_conditions ())
693
694 return;
695
696 if (!is_breakpoint (loc->owner))
697 return;
698
699 loc->condition_changed = condition_modified;
700}
701
702/* Sets the condition-evaluation mode using the static global
703 condition_evaluation_mode. */
704
705static void
eb4c3f4a 706set_condition_evaluation_mode (const char *args, int from_tty,
b775012e
LM
707 struct cmd_list_element *c)
708{
b775012e
LM
709 const char *old_mode, *new_mode;
710
711 if ((condition_evaluation_mode_1 == condition_evaluation_target)
712 && !target_supports_evaluation_of_breakpoint_conditions ())
713 {
714 condition_evaluation_mode_1 = condition_evaluation_mode;
715 warning (_("Target does not support breakpoint condition evaluation.\n"
716 "Using host evaluation mode instead."));
717 return;
718 }
719
720 new_mode = translate_condition_evaluation_mode (condition_evaluation_mode_1);
721 old_mode = translate_condition_evaluation_mode (condition_evaluation_mode);
722
abf1152a
JK
723 /* Flip the switch. Flip it even if OLD_MODE == NEW_MODE as one of the
724 settings was "auto". */
725 condition_evaluation_mode = condition_evaluation_mode_1;
726
b775012e
LM
727 /* Only update the mode if the user picked a different one. */
728 if (new_mode != old_mode)
729 {
730 struct bp_location *loc, **loc_tmp;
731 /* If the user switched to a different evaluation mode, we
732 need to synch the changes with the target as follows:
733
734 "host" -> "target": Send all (valid) conditions to the target.
735 "target" -> "host": Remove all the conditions from the target.
736 */
737
b775012e
LM
738 if (new_mode == condition_evaluation_target)
739 {
740 /* Mark everything modified and synch conditions with the
741 target. */
742 ALL_BP_LOCATIONS (loc, loc_tmp)
743 mark_breakpoint_location_modified (loc);
744 }
745 else
746 {
747 /* Manually mark non-duplicate locations to synch conditions
748 with the target. We do this to remove all the conditions the
749 target knows about. */
750 ALL_BP_LOCATIONS (loc, loc_tmp)
751 if (is_breakpoint (loc->owner) && loc->inserted)
752 loc->needs_update = 1;
753 }
754
755 /* Do the update. */
44702360 756 update_global_location_list (UGLL_MAY_INSERT);
b775012e
LM
757 }
758
759 return;
760}
761
762/* Shows the current mode of breakpoint condition evaluation. Explicitly shows
763 what "auto" is translating to. */
764
765static void
766show_condition_evaluation_mode (struct ui_file *file, int from_tty,
767 struct cmd_list_element *c, const char *value)
768{
769 if (condition_evaluation_mode == condition_evaluation_auto)
770 fprintf_filtered (file,
771 _("Breakpoint condition evaluation "
772 "mode is %s (currently %s).\n"),
773 value,
774 breakpoint_condition_evaluation_mode ());
775 else
776 fprintf_filtered (file, _("Breakpoint condition evaluation mode is %s.\n"),
777 value);
778}
779
780/* A comparison function for bp_location AP and BP that is used by
781 bsearch. This comparison function only cares about addresses, unlike
f5336ca5 782 the more general bp_locations_compare function. */
b775012e
LM
783
784static int
f5336ca5 785bp_locations_compare_addrs (const void *ap, const void *bp)
b775012e 786{
9a3c8263
SM
787 const struct bp_location *a = *(const struct bp_location **) ap;
788 const struct bp_location *b = *(const struct bp_location **) bp;
b775012e
LM
789
790 if (a->address == b->address)
791 return 0;
792 else
793 return ((a->address > b->address) - (a->address < b->address));
794}
795
796/* Helper function to skip all bp_locations with addresses
797 less than ADDRESS. It returns the first bp_location that
798 is greater than or equal to ADDRESS. If none is found, just
799 return NULL. */
800
801static struct bp_location **
802get_first_locp_gte_addr (CORE_ADDR address)
803{
804 struct bp_location dummy_loc;
805 struct bp_location *dummy_locp = &dummy_loc;
806 struct bp_location **locp_found = NULL;
807
808 /* Initialize the dummy location's address field. */
b775012e
LM
809 dummy_loc.address = address;
810
811 /* Find a close match to the first location at ADDRESS. */
9a3c8263 812 locp_found = ((struct bp_location **)
f5336ca5 813 bsearch (&dummy_locp, bp_locations, bp_locations_count,
9a3c8263 814 sizeof (struct bp_location **),
f5336ca5 815 bp_locations_compare_addrs));
b775012e
LM
816
817 /* Nothing was found, nothing left to do. */
818 if (locp_found == NULL)
819 return NULL;
820
821 /* We may have found a location that is at ADDRESS but is not the first in the
822 location's list. Go backwards (if possible) and locate the first one. */
f5336ca5 823 while ((locp_found - 1) >= bp_locations
b775012e
LM
824 && (*(locp_found - 1))->address == address)
825 locp_found--;
826
827 return locp_found;
828}
829
adc36818 830void
7a26bd4d 831set_breakpoint_condition (struct breakpoint *b, const char *exp,
adc36818
PM
832 int from_tty)
833{
3a5c3e22
PA
834 xfree (b->cond_string);
835 b->cond_string = NULL;
adc36818 836
3a5c3e22 837 if (is_watchpoint (b))
adc36818 838 {
3a5c3e22
PA
839 struct watchpoint *w = (struct watchpoint *) b;
840
4d01a485 841 w->cond_exp.reset ();
3a5c3e22
PA
842 }
843 else
844 {
845 struct bp_location *loc;
846
847 for (loc = b->loc; loc; loc = loc->next)
848 {
4d01a485 849 loc->cond.reset ();
b775012e
LM
850
851 /* No need to free the condition agent expression
852 bytecode (if we have one). We will handle this
853 when we go through update_global_location_list. */
3a5c3e22 854 }
adc36818 855 }
adc36818
PM
856
857 if (*exp == 0)
858 {
859 if (from_tty)
860 printf_filtered (_("Breakpoint %d now unconditional.\n"), b->number);
861 }
862 else
863 {
bbc13ae3 864 const char *arg = exp;
cc59ec59 865
adc36818
PM
866 /* I don't know if it matters whether this is the string the user
867 typed in or the decompiled expression. */
868 b->cond_string = xstrdup (arg);
869 b->condition_not_parsed = 0;
870
871 if (is_watchpoint (b))
872 {
3a5c3e22
PA
873 struct watchpoint *w = (struct watchpoint *) b;
874
699bd4cf 875 innermost_block_tracker tracker;
adc36818 876 arg = exp;
699bd4cf 877 w->cond_exp = parse_exp_1 (&arg, 0, 0, 0, &tracker);
adc36818
PM
878 if (*arg)
879 error (_("Junk at end of expression"));
699bd4cf 880 w->cond_exp_valid_block = tracker.block ();
adc36818
PM
881 }
882 else
883 {
3a5c3e22
PA
884 struct bp_location *loc;
885
adc36818
PM
886 for (loc = b->loc; loc; loc = loc->next)
887 {
888 arg = exp;
889 loc->cond =
1bb9788d
TT
890 parse_exp_1 (&arg, loc->address,
891 block_for_pc (loc->address), 0);
adc36818
PM
892 if (*arg)
893 error (_("Junk at end of expression"));
894 }
895 }
896 }
b775012e
LM
897 mark_breakpoint_modified (b);
898
76727919 899 gdb::observers::breakpoint_modified.notify (b);
adc36818
PM
900}
901
d55637df
TT
902/* Completion for the "condition" command. */
903
eb3ff9a5 904static void
6f937416 905condition_completer (struct cmd_list_element *cmd,
eb3ff9a5 906 completion_tracker &tracker,
6f937416 907 const char *text, const char *word)
d55637df 908{
6f937416 909 const char *space;
d55637df 910
f1735a53
TT
911 text = skip_spaces (text);
912 space = skip_to_space (text);
d55637df
TT
913 if (*space == '\0')
914 {
915 int len;
916 struct breakpoint *b;
d55637df
TT
917
918 if (text[0] == '$')
919 {
920 /* We don't support completion of history indices. */
eb3ff9a5
PA
921 if (!isdigit (text[1]))
922 complete_internalvar (tracker, &text[1]);
923 return;
d55637df
TT
924 }
925
926 /* We're completing the breakpoint number. */
927 len = strlen (text);
928
929 ALL_BREAKPOINTS (b)
58ce7251
SDJ
930 {
931 char number[50];
932
933 xsnprintf (number, sizeof (number), "%d", b->number);
934
935 if (strncmp (number, text, len) == 0)
b02f78f9 936 tracker.add_completion (make_unique_xstrdup (number));
58ce7251 937 }
d55637df 938
eb3ff9a5 939 return;
d55637df
TT
940 }
941
942 /* We're completing the expression part. */
f1735a53 943 text = skip_spaces (space);
eb3ff9a5 944 expression_completer (cmd, tracker, text, word);
d55637df
TT
945}
946
c906108c
SS
947/* condition N EXP -- set break condition of breakpoint N to EXP. */
948
949static void
0b39b52e 950condition_command (const char *arg, int from_tty)
c906108c 951{
52f0bd74 952 struct breakpoint *b;
0b39b52e 953 const char *p;
52f0bd74 954 int bnum;
c906108c
SS
955
956 if (arg == 0)
e2e0b3e5 957 error_no_arg (_("breakpoint number"));
c906108c
SS
958
959 p = arg;
960 bnum = get_number (&p);
5c44784c 961 if (bnum == 0)
8a3fe4f8 962 error (_("Bad breakpoint argument: '%s'"), arg);
c906108c
SS
963
964 ALL_BREAKPOINTS (b)
965 if (b->number == bnum)
2f069f6f 966 {
6dddc817
DE
967 /* Check if this breakpoint has a "stop" method implemented in an
968 extension language. This method and conditions entered into GDB
969 from the CLI are mutually exclusive. */
970 const struct extension_language_defn *extlang
971 = get_breakpoint_cond_ext_lang (b, EXT_LANG_NONE);
972
973 if (extlang != NULL)
974 {
975 error (_("Only one stop condition allowed. There is currently"
976 " a %s stop condition defined for this breakpoint."),
977 ext_lang_capitalized_name (extlang));
978 }
2566ad2d 979 set_breakpoint_condition (b, p, from_tty);
b775012e
LM
980
981 if (is_breakpoint (b))
44702360 982 update_global_location_list (UGLL_MAY_INSERT);
b775012e 983
2f069f6f
JB
984 return;
985 }
c906108c 986
8a3fe4f8 987 error (_("No breakpoint number %d."), bnum);
c906108c
SS
988}
989
a7bdde9e
VP
990/* Check that COMMAND do not contain commands that are suitable
991 only for tracepoints and not suitable for ordinary breakpoints.
4a64f543
MS
992 Throw if any such commands is found. */
993
a7bdde9e
VP
994static void
995check_no_tracepoint_commands (struct command_line *commands)
996{
997 struct command_line *c;
cc59ec59 998
a7bdde9e
VP
999 for (c = commands; c; c = c->next)
1000 {
a7bdde9e 1001 if (c->control_type == while_stepping_control)
3e43a32a
MS
1002 error (_("The 'while-stepping' command can "
1003 "only be used for tracepoints"));
a7bdde9e 1004
12973681
TT
1005 check_no_tracepoint_commands (c->body_list_0.get ());
1006 check_no_tracepoint_commands (c->body_list_1.get ());
a7bdde9e
VP
1007
1008 /* Not that command parsing removes leading whitespace and comment
4a64f543 1009 lines and also empty lines. So, we only need to check for
a7bdde9e
VP
1010 command directly. */
1011 if (strstr (c->line, "collect ") == c->line)
1012 error (_("The 'collect' command can only be used for tracepoints"));
1013
51661e93
VP
1014 if (strstr (c->line, "teval ") == c->line)
1015 error (_("The 'teval' command can only be used for tracepoints"));
a7bdde9e
VP
1016 }
1017}
1018
c1fc2657 1019struct longjmp_breakpoint : public breakpoint
3b0871f4 1020{
c1fc2657 1021 ~longjmp_breakpoint () override;
3b0871f4
SM
1022};
1023
d77f58be
SS
1024/* Encapsulate tests for different types of tracepoints. */
1025
3b0871f4
SM
1026static bool
1027is_tracepoint_type (bptype type)
d9b3f62e
PA
1028{
1029 return (type == bp_tracepoint
1030 || type == bp_fast_tracepoint
1031 || type == bp_static_tracepoint);
1032}
1033
3b0871f4
SM
1034static bool
1035is_longjmp_type (bptype type)
1036{
1037 return type == bp_longjmp || type == bp_exception;
1038}
1039
a7bdde9e 1040int
d77f58be 1041is_tracepoint (const struct breakpoint *b)
a7bdde9e 1042{
d9b3f62e 1043 return is_tracepoint_type (b->type);
a7bdde9e 1044}
d9b3f62e 1045
a5e364af
SM
1046/* Factory function to create an appropriate instance of breakpoint given
1047 TYPE. */
1048
1049static std::unique_ptr<breakpoint>
1050new_breakpoint_from_type (bptype type)
1051{
1052 breakpoint *b;
1053
1054 if (is_tracepoint_type (type))
c1fc2657 1055 b = new tracepoint ();
3b0871f4 1056 else if (is_longjmp_type (type))
c1fc2657 1057 b = new longjmp_breakpoint ();
a5e364af
SM
1058 else
1059 b = new breakpoint ();
1060
1061 return std::unique_ptr<breakpoint> (b);
1062}
1063
e5dd4106 1064/* A helper function that validates that COMMANDS are valid for a
95a42b64
TT
1065 breakpoint. This function will throw an exception if a problem is
1066 found. */
48cb2d85 1067
95a42b64
TT
1068static void
1069validate_commands_for_breakpoint (struct breakpoint *b,
1070 struct command_line *commands)
48cb2d85 1071{
d77f58be 1072 if (is_tracepoint (b))
a7bdde9e 1073 {
c9a6ce02 1074 struct tracepoint *t = (struct tracepoint *) b;
a7bdde9e
VP
1075 struct command_line *c;
1076 struct command_line *while_stepping = 0;
c9a6ce02
PA
1077
1078 /* Reset the while-stepping step count. The previous commands
1079 might have included a while-stepping action, while the new
1080 ones might not. */
1081 t->step_count = 0;
1082
1083 /* We need to verify that each top-level element of commands is
1084 valid for tracepoints, that there's at most one
1085 while-stepping element, and that the while-stepping's body
1086 has valid tracing commands excluding nested while-stepping.
1087 We also need to validate the tracepoint action line in the
1088 context of the tracepoint --- validate_actionline actually
1089 has side effects, like setting the tracepoint's
1090 while-stepping STEP_COUNT, in addition to checking if the
1091 collect/teval actions parse and make sense in the
1092 tracepoint's context. */
a7bdde9e
VP
1093 for (c = commands; c; c = c->next)
1094 {
a7bdde9e
VP
1095 if (c->control_type == while_stepping_control)
1096 {
1097 if (b->type == bp_fast_tracepoint)
3e43a32a
MS
1098 error (_("The 'while-stepping' command "
1099 "cannot be used for fast tracepoint"));
0fb4aa4b 1100 else if (b->type == bp_static_tracepoint)
3e43a32a
MS
1101 error (_("The 'while-stepping' command "
1102 "cannot be used for static tracepoint"));
a7bdde9e
VP
1103
1104 if (while_stepping)
3e43a32a
MS
1105 error (_("The 'while-stepping' command "
1106 "can be used only once"));
a7bdde9e
VP
1107 else
1108 while_stepping = c;
1109 }
c9a6ce02
PA
1110
1111 validate_actionline (c->line, b);
a7bdde9e
VP
1112 }
1113 if (while_stepping)
1114 {
1115 struct command_line *c2;
1116
12973681
TT
1117 gdb_assert (while_stepping->body_list_1 == nullptr);
1118 c2 = while_stepping->body_list_0.get ();
a7bdde9e
VP
1119 for (; c2; c2 = c2->next)
1120 {
a7bdde9e
VP
1121 if (c2->control_type == while_stepping_control)
1122 error (_("The 'while-stepping' command cannot be nested"));
1123 }
1124 }
1125 }
1126 else
1127 {
1128 check_no_tracepoint_commands (commands);
1129 }
95a42b64
TT
1130}
1131
0fb4aa4b
PA
1132/* Return a vector of all the static tracepoints set at ADDR. The
1133 caller is responsible for releasing the vector. */
1134
f51e0e20 1135std::vector<breakpoint *>
0fb4aa4b
PA
1136static_tracepoints_here (CORE_ADDR addr)
1137{
1138 struct breakpoint *b;
f51e0e20 1139 std::vector<breakpoint *> found;
0fb4aa4b
PA
1140 struct bp_location *loc;
1141
1142 ALL_BREAKPOINTS (b)
1143 if (b->type == bp_static_tracepoint)
1144 {
1145 for (loc = b->loc; loc; loc = loc->next)
1146 if (loc->address == addr)
f51e0e20 1147 found.push_back (b);
0fb4aa4b
PA
1148 }
1149
1150 return found;
1151}
1152
95a42b64 1153/* Set the command list of B to COMMANDS. If breakpoint is tracepoint,
4a64f543 1154 validate that only allowed commands are included. */
95a42b64
TT
1155
1156void
4a64f543 1157breakpoint_set_commands (struct breakpoint *b,
12973681 1158 counted_command_line &&commands)
95a42b64 1159{
93921405 1160 validate_commands_for_breakpoint (b, commands.get ());
a7bdde9e 1161
d1b0a7bf 1162 b->commands = std::move (commands);
76727919 1163 gdb::observers::breakpoint_modified.notify (b);
48cb2d85
VP
1164}
1165
45a43567
TT
1166/* Set the internal `silent' flag on the breakpoint. Note that this
1167 is not the same as the "silent" that may appear in the breakpoint's
1168 commands. */
1169
1170void
1171breakpoint_set_silent (struct breakpoint *b, int silent)
1172{
1173 int old_silent = b->silent;
1174
1175 b->silent = silent;
1176 if (old_silent != silent)
76727919 1177 gdb::observers::breakpoint_modified.notify (b);
45a43567
TT
1178}
1179
1180/* Set the thread for this breakpoint. If THREAD is -1, make the
1181 breakpoint work for any thread. */
1182
1183void
1184breakpoint_set_thread (struct breakpoint *b, int thread)
1185{
1186 int old_thread = b->thread;
1187
1188 b->thread = thread;
1189 if (old_thread != thread)
76727919 1190 gdb::observers::breakpoint_modified.notify (b);
45a43567
TT
1191}
1192
1193/* Set the task for this breakpoint. If TASK is 0, make the
1194 breakpoint work for any task. */
1195
1196void
1197breakpoint_set_task (struct breakpoint *b, int task)
1198{
1199 int old_task = b->task;
1200
1201 b->task = task;
1202 if (old_task != task)
76727919 1203 gdb::observers::breakpoint_modified.notify (b);
45a43567
TT
1204}
1205
95a42b64 1206static void
896b6bda 1207commands_command_1 (const char *arg, int from_tty,
4a64f543 1208 struct command_line *control)
95a42b64 1209{
d1b0a7bf 1210 counted_command_line cmd;
999700cd
PW
1211 /* cmd_read will be true once we have read cmd. Note that cmd might still be
1212 NULL after the call to read_command_lines if the user provides an empty
1213 list of command by just typing "end". */
1214 bool cmd_read = false;
95a42b64 1215
896b6bda
PA
1216 std::string new_arg;
1217
95a42b64
TT
1218 if (arg == NULL || !*arg)
1219 {
86b17b60 1220 if (breakpoint_count - prev_breakpoint_count > 1)
896b6bda
PA
1221 new_arg = string_printf ("%d-%d", prev_breakpoint_count + 1,
1222 breakpoint_count);
95a42b64 1223 else if (breakpoint_count > 0)
896b6bda 1224 new_arg = string_printf ("%d", breakpoint_count);
48649e1b
TT
1225 arg = new_arg.c_str ();
1226 }
1227
1228 map_breakpoint_numbers
1229 (arg, [&] (breakpoint *b)
1230 {
999700cd 1231 if (!cmd_read)
48649e1b 1232 {
999700cd 1233 gdb_assert (cmd == NULL);
48649e1b 1234 if (control != NULL)
12973681 1235 cmd = control->body_list_0;
48649e1b
TT
1236 else
1237 {
81b1e71c
TT
1238 std::string str
1239 = string_printf (_("Type commands for breakpoint(s) "
1240 "%s, one per line."),
1241 arg);
48649e1b 1242
60b3cef2
TT
1243 auto do_validate = [=] (const char *line)
1244 {
1245 validate_actionline (line, b);
1246 };
1247 gdb::function_view<void (const char *)> validator;
1248 if (is_tracepoint (b))
1249 validator = do_validate;
1250
1251 cmd = read_command_lines (str.c_str (), from_tty, 1, validator);
48649e1b 1252 }
999700cd 1253 cmd_read = true;
48649e1b
TT
1254 }
1255
1256 /* If a breakpoint was on the list more than once, we don't need to
1257 do anything. */
1258 if (b->commands != cmd)
1259 {
d1b0a7bf 1260 validate_commands_for_breakpoint (b, cmd.get ());
48649e1b 1261 b->commands = cmd;
76727919 1262 gdb::observers::breakpoint_modified.notify (b);
48649e1b
TT
1263 }
1264 });
95a42b64
TT
1265}
1266
1267static void
0b39b52e 1268commands_command (const char *arg, int from_tty)
95a42b64
TT
1269{
1270 commands_command_1 (arg, from_tty, NULL);
c906108c 1271}
40c03ae8
EZ
1272
1273/* Like commands_command, but instead of reading the commands from
1274 input stream, takes them from an already parsed command structure.
1275
1276 This is used by cli-script.c to DTRT with breakpoint commands
1277 that are part of if and while bodies. */
1278enum command_control_type
896b6bda 1279commands_from_control_command (const char *arg, struct command_line *cmd)
40c03ae8 1280{
95a42b64
TT
1281 commands_command_1 (arg, 0, cmd);
1282 return simple_control;
40c03ae8 1283}
876fa593
JK
1284
1285/* Return non-zero if BL->TARGET_INFO contains valid information. */
1286
1287static int
1288bp_location_has_shadow (struct bp_location *bl)
1289{
1290 if (bl->loc_type != bp_loc_software_breakpoint)
1291 return 0;
1292 if (!bl->inserted)
1293 return 0;
1294 if (bl->target_info.shadow_len == 0)
e5dd4106 1295 /* BL isn't valid, or doesn't shadow memory. */
876fa593
JK
1296 return 0;
1297 return 1;
1298}
1299
9d497a19
PA
1300/* Update BUF, which is LEN bytes read from the target address
1301 MEMADDR, by replacing a memory breakpoint with its shadowed
1302 contents.
1303
1304 If READBUF is not NULL, this buffer must not overlap with the of
1305 the breakpoint location's shadow_contents buffer. Otherwise, a
1306 failed assertion internal error will be raised. */
1307
1308static void
1309one_breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1310 const gdb_byte *writebuf_org,
1311 ULONGEST memaddr, LONGEST len,
1312 struct bp_target_info *target_info,
1313 struct gdbarch *gdbarch)
1314{
1315 /* Now do full processing of the found relevant range of elements. */
1316 CORE_ADDR bp_addr = 0;
1317 int bp_size = 0;
1318 int bptoffset = 0;
1319
1320 if (!breakpoint_address_match (target_info->placed_address_space, 0,
1321 current_program_space->aspace, 0))
1322 {
1323 /* The breakpoint is inserted in a different address space. */
1324 return;
1325 }
1326
1327 /* Addresses and length of the part of the breakpoint that
1328 we need to copy. */
1329 bp_addr = target_info->placed_address;
1330 bp_size = target_info->shadow_len;
1331
1332 if (bp_addr + bp_size <= memaddr)
1333 {
1334 /* The breakpoint is entirely before the chunk of memory we are
1335 reading. */
1336 return;
1337 }
1338
1339 if (bp_addr >= memaddr + len)
1340 {
1341 /* The breakpoint is entirely after the chunk of memory we are
1342 reading. */
1343 return;
1344 }
1345
1346 /* Offset within shadow_contents. */
1347 if (bp_addr < memaddr)
1348 {
1349 /* Only copy the second part of the breakpoint. */
1350 bp_size -= memaddr - bp_addr;
1351 bptoffset = memaddr - bp_addr;
1352 bp_addr = memaddr;
1353 }
1354
1355 if (bp_addr + bp_size > memaddr + len)
1356 {
1357 /* Only copy the first part of the breakpoint. */
1358 bp_size -= (bp_addr + bp_size) - (memaddr + len);
1359 }
1360
1361 if (readbuf != NULL)
1362 {
1363 /* Verify that the readbuf buffer does not overlap with the
1364 shadow_contents buffer. */
1365 gdb_assert (target_info->shadow_contents >= readbuf + len
1366 || readbuf >= (target_info->shadow_contents
1367 + target_info->shadow_len));
1368
1369 /* Update the read buffer with this inserted breakpoint's
1370 shadow. */
1371 memcpy (readbuf + bp_addr - memaddr,
1372 target_info->shadow_contents + bptoffset, bp_size);
1373 }
1374 else
1375 {
1376 const unsigned char *bp;
0d5ed153
MR
1377 CORE_ADDR addr = target_info->reqstd_address;
1378 int placed_size;
9d497a19
PA
1379
1380 /* Update the shadow with what we want to write to memory. */
1381 memcpy (target_info->shadow_contents + bptoffset,
1382 writebuf_org + bp_addr - memaddr, bp_size);
1383
1384 /* Determine appropriate breakpoint contents and size for this
1385 address. */
0d5ed153 1386 bp = gdbarch_breakpoint_from_pc (gdbarch, &addr, &placed_size);
9d497a19
PA
1387
1388 /* Update the final write buffer with this inserted
1389 breakpoint's INSN. */
1390 memcpy (writebuf + bp_addr - memaddr, bp + bptoffset, bp_size);
1391 }
1392}
1393
8defab1a 1394/* Update BUF, which is LEN bytes read from the target address MEMADDR,
876fa593
JK
1395 by replacing any memory breakpoints with their shadowed contents.
1396
35c63cd8
JB
1397 If READBUF is not NULL, this buffer must not overlap with any of
1398 the breakpoint location's shadow_contents buffers. Otherwise,
1399 a failed assertion internal error will be raised.
1400
876fa593 1401 The range of shadowed area by each bp_location is:
f5336ca5
PA
1402 bl->address - bp_locations_placed_address_before_address_max
1403 up to bl->address + bp_locations_shadow_len_after_address_max
876fa593
JK
1404 The range we were requested to resolve shadows for is:
1405 memaddr ... memaddr + len
1406 Thus the safe cutoff boundaries for performance optimization are
35df4500 1407 memaddr + len <= (bl->address
f5336ca5 1408 - bp_locations_placed_address_before_address_max)
876fa593 1409 and:
f5336ca5 1410 bl->address + bp_locations_shadow_len_after_address_max <= memaddr */
c906108c 1411
8defab1a 1412void
f0ba3972
PA
1413breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1414 const gdb_byte *writebuf_org,
1415 ULONGEST memaddr, LONGEST len)
c906108c 1416{
4a64f543
MS
1417 /* Left boundary, right boundary and median element of our binary
1418 search. */
876fa593
JK
1419 unsigned bc_l, bc_r, bc;
1420
4a64f543
MS
1421 /* Find BC_L which is a leftmost element which may affect BUF
1422 content. It is safe to report lower value but a failure to
1423 report higher one. */
876fa593
JK
1424
1425 bc_l = 0;
f5336ca5 1426 bc_r = bp_locations_count;
876fa593
JK
1427 while (bc_l + 1 < bc_r)
1428 {
35df4500 1429 struct bp_location *bl;
876fa593
JK
1430
1431 bc = (bc_l + bc_r) / 2;
f5336ca5 1432 bl = bp_locations[bc];
876fa593 1433
4a64f543
MS
1434 /* Check first BL->ADDRESS will not overflow due to the added
1435 constant. Then advance the left boundary only if we are sure
1436 the BC element can in no way affect the BUF content (MEMADDR
1437 to MEMADDR + LEN range).
876fa593 1438
f5336ca5 1439 Use the BP_LOCATIONS_SHADOW_LEN_AFTER_ADDRESS_MAX safety
4a64f543
MS
1440 offset so that we cannot miss a breakpoint with its shadow
1441 range tail still reaching MEMADDR. */
c5aa993b 1442
f5336ca5 1443 if ((bl->address + bp_locations_shadow_len_after_address_max
35df4500 1444 >= bl->address)
f5336ca5 1445 && (bl->address + bp_locations_shadow_len_after_address_max
35df4500 1446 <= memaddr))
876fa593
JK
1447 bc_l = bc;
1448 else
1449 bc_r = bc;
1450 }
1451
128070bb
PA
1452 /* Due to the binary search above, we need to make sure we pick the
1453 first location that's at BC_L's address. E.g., if there are
1454 multiple locations at the same address, BC_L may end up pointing
1455 at a duplicate location, and miss the "master"/"inserted"
1456 location. Say, given locations L1, L2 and L3 at addresses A and
1457 B:
1458
1459 L1@A, L2@A, L3@B, ...
1460
1461 BC_L could end up pointing at location L2, while the "master"
1462 location could be L1. Since the `loc->inserted' flag is only set
1463 on "master" locations, we'd forget to restore the shadow of L1
1464 and L2. */
1465 while (bc_l > 0
f5336ca5 1466 && bp_locations[bc_l]->address == bp_locations[bc_l - 1]->address)
128070bb
PA
1467 bc_l--;
1468
876fa593
JK
1469 /* Now do full processing of the found relevant range of elements. */
1470
f5336ca5 1471 for (bc = bc_l; bc < bp_locations_count; bc++)
c5aa993b 1472 {
f5336ca5 1473 struct bp_location *bl = bp_locations[bc];
876fa593 1474
35df4500
TJB
1475 /* bp_location array has BL->OWNER always non-NULL. */
1476 if (bl->owner->type == bp_none)
8a3fe4f8 1477 warning (_("reading through apparently deleted breakpoint #%d?"),
35df4500 1478 bl->owner->number);
ffce0d52 1479
e5dd4106 1480 /* Performance optimization: any further element can no longer affect BUF
876fa593
JK
1481 content. */
1482
f5336ca5
PA
1483 if (bl->address >= bp_locations_placed_address_before_address_max
1484 && memaddr + len <= (bl->address
1485 - bp_locations_placed_address_before_address_max))
876fa593
JK
1486 break;
1487
35df4500 1488 if (!bp_location_has_shadow (bl))
c5aa993b 1489 continue;
6c95b8df 1490
9d497a19
PA
1491 one_breakpoint_xfer_memory (readbuf, writebuf, writebuf_org,
1492 memaddr, len, &bl->target_info, bl->gdbarch);
1493 }
c906108c 1494}
9d497a19 1495
c906108c 1496\f
c5aa993b 1497
b775012e
LM
1498/* Return true if BPT is either a software breakpoint or a hardware
1499 breakpoint. */
1500
1501int
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
a5606eee 1511static int
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
60e1c644
PA
1519/* Return true if BPT is of any watchpoint kind, hardware or
1520 software. */
1521
3a5c3e22 1522int
d77f58be 1523is_watchpoint (const struct breakpoint *bpt)
60e1c644
PA
1524{
1525 return (is_hardware_watchpoint (bpt)
1526 || bpt->type == bp_watchpoint);
1527}
1528
3a5c3e22
PA
1529/* Returns true if the current thread and its running state are safe
1530 to evaluate or update watchpoint B. Watchpoints on local
1531 expressions need to be evaluated in the context of the thread that
1532 was current when the watchpoint was created, and, that thread needs
1533 to be stopped to be able to select the correct frame context.
1534 Watchpoints on global expressions can be evaluated on any thread,
1535 and in any state. It is presently left to the target allowing
1536 memory accesses when threads are running. */
f6bc2008
PA
1537
1538static int
3a5c3e22 1539watchpoint_in_thread_scope (struct watchpoint *b)
f6bc2008 1540{
c1fc2657 1541 return (b->pspace == current_program_space
d7e15655
TT
1542 && (b->watchpoint_thread == null_ptid
1543 || (inferior_ptid == b->watchpoint_thread
00431a78 1544 && !inferior_thread ()->executing)));
f6bc2008
PA
1545}
1546
d0fb5eae
JK
1547/* Set watchpoint B to disp_del_at_next_stop, even including its possible
1548 associated bp_watchpoint_scope breakpoint. */
1549
1550static void
3a5c3e22 1551watchpoint_del_at_next_stop (struct watchpoint *w)
d0fb5eae 1552{
c1fc2657 1553 if (w->related_breakpoint != w)
d0fb5eae 1554 {
c1fc2657
SM
1555 gdb_assert (w->related_breakpoint->type == bp_watchpoint_scope);
1556 gdb_assert (w->related_breakpoint->related_breakpoint == w);
1557 w->related_breakpoint->disposition = disp_del_at_next_stop;
1558 w->related_breakpoint->related_breakpoint = w->related_breakpoint;
1559 w->related_breakpoint = w;
d0fb5eae 1560 }
c1fc2657 1561 w->disposition = disp_del_at_next_stop;
d0fb5eae
JK
1562}
1563
bb9d5f81
PP
1564/* Extract a bitfield value from value VAL using the bit parameters contained in
1565 watchpoint W. */
1566
1567static struct value *
1568extract_bitfield_from_watchpoint_value (struct watchpoint *w, struct value *val)
1569{
1570 struct value *bit_val;
1571
1572 if (val == NULL)
1573 return NULL;
1574
1575 bit_val = allocate_value (value_type (val));
1576
1577 unpack_value_bitfield (bit_val,
1578 w->val_bitpos,
1579 w->val_bitsize,
1580 value_contents_for_printing (val),
1581 value_offset (val),
1582 val);
1583
1584 return bit_val;
1585}
1586
c6d81124
PA
1587/* Allocate a dummy location and add it to B, which must be a software
1588 watchpoint. This is required because even if a software watchpoint
1589 is not watching any memory, bpstat_stop_status requires a location
1590 to be able to report stops. */
1591
1592static void
1593software_watchpoint_add_no_memory_location (struct breakpoint *b,
1594 struct program_space *pspace)
1595{
1596 gdb_assert (b->type == bp_watchpoint && b->loc == NULL);
1597
1598 b->loc = allocate_bp_location (b);
1599 b->loc->pspace = pspace;
1600 b->loc->address = -1;
1601 b->loc->length = -1;
1602}
1603
1604/* Returns true if B is a software watchpoint that is not watching any
1605 memory (e.g., "watch $pc"). */
1606
1607static int
1608is_no_memory_software_watchpoint (struct breakpoint *b)
1609{
1610 return (b->type == bp_watchpoint
1611 && b->loc != NULL
1612 && b->loc->next == NULL
1613 && b->loc->address == -1
1614 && b->loc->length == -1);
1615}
1616
567e1b4e
JB
1617/* Assuming that B is a watchpoint:
1618 - Reparse watchpoint expression, if REPARSE is non-zero
a5606eee 1619 - Evaluate expression and store the result in B->val
567e1b4e
JB
1620 - Evaluate the condition if there is one, and store the result
1621 in b->loc->cond.
a5606eee
VP
1622 - Update the list of values that must be watched in B->loc.
1623
4a64f543
MS
1624 If the watchpoint disposition is disp_del_at_next_stop, then do
1625 nothing. If this is local watchpoint that is out of scope, delete
1626 it.
1627
1628 Even with `set breakpoint always-inserted on' the watchpoints are
1629 removed + inserted on each stop here. Normal breakpoints must
1630 never be removed because they might be missed by a running thread
1631 when debugging in non-stop mode. On the other hand, hardware
1632 watchpoints (is_hardware_watchpoint; processed here) are specific
1633 to each LWP since they are stored in each LWP's hardware debug
1634 registers. Therefore, such LWP must be stopped first in order to
1635 be able to modify its hardware watchpoints.
1636
1637 Hardware watchpoints must be reset exactly once after being
1638 presented to the user. It cannot be done sooner, because it would
1639 reset the data used to present the watchpoint hit to the user. And
1640 it must not be done later because it could display the same single
1641 watchpoint hit during multiple GDB stops. Note that the latter is
1642 relevant only to the hardware watchpoint types bp_read_watchpoint
1643 and bp_access_watchpoint. False hit by bp_hardware_watchpoint is
1644 not user-visible - its hit is suppressed if the memory content has
1645 not changed.
1646
1647 The following constraints influence the location where we can reset
1648 hardware watchpoints:
1649
1650 * target_stopped_by_watchpoint and target_stopped_data_address are
1651 called several times when GDB stops.
1652
1653 [linux]
1654 * Multiple hardware watchpoints can be hit at the same time,
1655 causing GDB to stop. GDB only presents one hardware watchpoint
1656 hit at a time as the reason for stopping, and all the other hits
1657 are presented later, one after the other, each time the user
1658 requests the execution to be resumed. Execution is not resumed
1659 for the threads still having pending hit event stored in
1660 LWP_INFO->STATUS. While the watchpoint is already removed from
1661 the inferior on the first stop the thread hit event is kept being
1662 reported from its cached value by linux_nat_stopped_data_address
1663 until the real thread resume happens after the watchpoint gets
1664 presented and thus its LWP_INFO->STATUS gets reset.
1665
1666 Therefore the hardware watchpoint hit can get safely reset on the
1667 watchpoint removal from inferior. */
a79d3c27 1668
b40ce68a 1669static void
3a5c3e22 1670update_watchpoint (struct watchpoint *b, int reparse)
7270d8f2 1671{
a5606eee 1672 int within_current_scope;
a5606eee 1673 struct frame_id saved_frame_id;
66076460 1674 int frame_saved;
a5606eee 1675
f6bc2008
PA
1676 /* If this is a local watchpoint, we only want to check if the
1677 watchpoint frame is in scope if the current thread is the thread
1678 that was used to create the watchpoint. */
1679 if (!watchpoint_in_thread_scope (b))
1680 return;
1681
c1fc2657 1682 if (b->disposition == disp_del_at_next_stop)
a5606eee
VP
1683 return;
1684
66076460 1685 frame_saved = 0;
a5606eee
VP
1686
1687 /* Determine if the watchpoint is within scope. */
1688 if (b->exp_valid_block == NULL)
1689 within_current_scope = 1;
1690 else
1691 {
b5db5dfc
UW
1692 struct frame_info *fi = get_current_frame ();
1693 struct gdbarch *frame_arch = get_frame_arch (fi);
1694 CORE_ADDR frame_pc = get_frame_pc (fi);
1695
c9cf6e20
MG
1696 /* If we're at a point where the stack has been destroyed
1697 (e.g. in a function epilogue), unwinding may not work
1698 properly. Do not attempt to recreate locations at this
b5db5dfc 1699 point. See similar comments in watchpoint_check. */
c9cf6e20 1700 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
b5db5dfc 1701 return;
66076460
DJ
1702
1703 /* Save the current frame's ID so we can restore it after
1704 evaluating the watchpoint expression on its own frame. */
1705 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
1706 took a frame parameter, so that we didn't have to change the
1707 selected frame. */
1708 frame_saved = 1;
1709 saved_frame_id = get_frame_id (get_selected_frame (NULL));
1710
a5606eee
VP
1711 fi = frame_find_by_id (b->watchpoint_frame);
1712 within_current_scope = (fi != NULL);
1713 if (within_current_scope)
1714 select_frame (fi);
1715 }
1716
b5db5dfc
UW
1717 /* We don't free locations. They are stored in the bp_location array
1718 and update_global_location_list will eventually delete them and
1719 remove breakpoints if needed. */
c1fc2657 1720 b->loc = NULL;
b5db5dfc 1721
a5606eee
VP
1722 if (within_current_scope && reparse)
1723 {
bbc13ae3 1724 const char *s;
d63d0675 1725
4d01a485 1726 b->exp.reset ();
d63d0675 1727 s = b->exp_string_reparse ? b->exp_string_reparse : b->exp_string;
1bb9788d 1728 b->exp = parse_exp_1 (&s, 0, b->exp_valid_block, 0);
a5606eee
VP
1729 /* If the meaning of expression itself changed, the old value is
1730 no longer relevant. We don't want to report a watchpoint hit
1731 to the user when the old value and the new value may actually
1732 be completely different objects. */
fa4727a6
DJ
1733 b->val = NULL;
1734 b->val_valid = 0;
60e1c644
PA
1735
1736 /* Note that unlike with breakpoints, the watchpoint's condition
1737 expression is stored in the breakpoint object, not in the
1738 locations (re)created below. */
c1fc2657 1739 if (b->cond_string != NULL)
60e1c644 1740 {
4d01a485 1741 b->cond_exp.reset ();
60e1c644 1742
c1fc2657 1743 s = b->cond_string;
1bb9788d 1744 b->cond_exp = parse_exp_1 (&s, 0, b->cond_exp_valid_block, 0);
60e1c644 1745 }
a5606eee 1746 }
a5606eee
VP
1747
1748 /* If we failed to parse the expression, for example because
1749 it refers to a global variable in a not-yet-loaded shared library,
1750 don't try to insert watchpoint. We don't automatically delete
1751 such watchpoint, though, since failure to parse expression
1752 is different from out-of-scope watchpoint. */
e8369a73 1753 if (!target_has_execution)
2d134ed3
PA
1754 {
1755 /* Without execution, memory can't change. No use to try and
1756 set watchpoint locations. The watchpoint will be reset when
1757 the target gains execution, through breakpoint_re_set. */
e8369a73
AB
1758 if (!can_use_hw_watchpoints)
1759 {
c1fc2657
SM
1760 if (b->ops->works_in_software_mode (b))
1761 b->type = bp_watchpoint;
e8369a73 1762 else
638aa5a1
AB
1763 error (_("Can't set read/access watchpoint when "
1764 "hardware watchpoints are disabled."));
e8369a73 1765 }
2d134ed3
PA
1766 }
1767 else if (within_current_scope && b->exp)
a5606eee 1768 {
0cf6dd15 1769 int pc = 0;
a6535de1 1770 std::vector<value_ref_ptr> val_chain;
8d49165d 1771 struct value *v, *result;
2d134ed3 1772 struct program_space *frame_pspace;
a5606eee 1773
4d01a485 1774 fetch_subexp_value (b->exp.get (), &pc, &v, &result, &val_chain, 0);
a5606eee 1775
a5606eee
VP
1776 /* Avoid setting b->val if it's already set. The meaning of
1777 b->val is 'the last value' user saw, and we should update
1778 it only if we reported that last value to user. As it
9c06b0b4
TJB
1779 happens, the code that reports it updates b->val directly.
1780 We don't keep track of the memory value for masked
1781 watchpoints. */
c1fc2657 1782 if (!b->val_valid && !is_masked_watchpoint (b))
fa4727a6 1783 {
bb9d5f81 1784 if (b->val_bitsize != 0)
850645cf
TT
1785 v = extract_bitfield_from_watchpoint_value (b, v);
1786 b->val = release_value (v);
fa4727a6
DJ
1787 b->val_valid = 1;
1788 }
a5606eee 1789
2d134ed3
PA
1790 frame_pspace = get_frame_program_space (get_selected_frame (NULL));
1791
a5606eee 1792 /* Look at each value on the value chain. */
a6535de1
TT
1793 gdb_assert (!val_chain.empty ());
1794 for (const value_ref_ptr &iter : val_chain)
a5606eee 1795 {
a6535de1
TT
1796 v = iter.get ();
1797
a5606eee
VP
1798 /* If it's a memory location, and GDB actually needed
1799 its contents to evaluate the expression, then we
fa4727a6
DJ
1800 must watch it. If the first value returned is
1801 still lazy, that means an error occurred reading it;
1802 watch it anyway in case it becomes readable. */
a5606eee 1803 if (VALUE_LVAL (v) == lval_memory
a6535de1 1804 && (v == val_chain[0] || ! value_lazy (v)))
a5606eee
VP
1805 {
1806 struct type *vtype = check_typedef (value_type (v));
7270d8f2 1807
a5606eee
VP
1808 /* We only watch structs and arrays if user asked
1809 for it explicitly, never if they just happen to
1810 appear in the middle of some value chain. */
fa4727a6 1811 if (v == result
a5606eee
VP
1812 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
1813 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
1814 {
1815 CORE_ADDR addr;
f486487f 1816 enum target_hw_bp_type type;
a5606eee 1817 struct bp_location *loc, **tmp;
bb9d5f81
PP
1818 int bitpos = 0, bitsize = 0;
1819
1820 if (value_bitsize (v) != 0)
1821 {
1822 /* Extract the bit parameters out from the bitfield
1823 sub-expression. */
1824 bitpos = value_bitpos (v);
1825 bitsize = value_bitsize (v);
1826 }
1827 else if (v == result && b->val_bitsize != 0)
1828 {
1829 /* If VAL_BITSIZE != 0 then RESULT is actually a bitfield
1830 lvalue whose bit parameters are saved in the fields
1831 VAL_BITPOS and VAL_BITSIZE. */
1832 bitpos = b->val_bitpos;
1833 bitsize = b->val_bitsize;
1834 }
a5606eee 1835
42ae5230 1836 addr = value_address (v);
bb9d5f81
PP
1837 if (bitsize != 0)
1838 {
1839 /* Skip the bytes that don't contain the bitfield. */
1840 addr += bitpos / 8;
1841 }
1842
a5606eee 1843 type = hw_write;
c1fc2657 1844 if (b->type == bp_read_watchpoint)
a5606eee 1845 type = hw_read;
c1fc2657 1846 else if (b->type == bp_access_watchpoint)
a5606eee 1847 type = hw_access;
3a5c3e22 1848
c1fc2657
SM
1849 loc = allocate_bp_location (b);
1850 for (tmp = &(b->loc); *tmp != NULL; tmp = &((*tmp)->next))
a5606eee
VP
1851 ;
1852 *tmp = loc;
a6d9a66e 1853 loc->gdbarch = get_type_arch (value_type (v));
6c95b8df
PA
1854
1855 loc->pspace = frame_pspace;
f17d9474 1856 loc->address = address_significant (loc->gdbarch, addr);
bb9d5f81
PP
1857
1858 if (bitsize != 0)
1859 {
1860 /* Just cover the bytes that make up the bitfield. */
1861 loc->length = ((bitpos % 8) + bitsize + 7) / 8;
1862 }
1863 else
1864 loc->length = TYPE_LENGTH (value_type (v));
1865
a5606eee
VP
1866 loc->watchpoint_type = type;
1867 }
1868 }
9fa40276
TJB
1869 }
1870
1871 /* Change the type of breakpoint between hardware assisted or
1872 an ordinary watchpoint depending on the hardware support
1873 and free hardware slots. REPARSE is set when the inferior
1874 is started. */
a9634178 1875 if (reparse)
9fa40276 1876 {
e09342b5 1877 int reg_cnt;
9fa40276
TJB
1878 enum bp_loc_type loc_type;
1879 struct bp_location *bl;
a5606eee 1880
a9634178 1881 reg_cnt = can_use_hardware_watchpoint (val_chain);
e09342b5
TJB
1882
1883 if (reg_cnt)
9fa40276
TJB
1884 {
1885 int i, target_resources_ok, other_type_used;
a1398e0c 1886 enum bptype type;
9fa40276 1887
a9634178
TJB
1888 /* Use an exact watchpoint when there's only one memory region to be
1889 watched, and only one debug register is needed to watch it. */
1890 b->exact = target_exact_watchpoints && reg_cnt == 1;
1891
9fa40276 1892 /* We need to determine how many resources are already
e09342b5
TJB
1893 used for all other hardware watchpoints plus this one
1894 to see if we still have enough resources to also fit
a1398e0c
PA
1895 this watchpoint in as well. */
1896
1897 /* If this is a software watchpoint, we try to turn it
1898 to a hardware one -- count resources as if B was of
1899 hardware watchpoint type. */
c1fc2657 1900 type = b->type;
a1398e0c
PA
1901 if (type == bp_watchpoint)
1902 type = bp_hardware_watchpoint;
1903
1904 /* This watchpoint may or may not have been placed on
1905 the list yet at this point (it won't be in the list
1906 if we're trying to create it for the first time,
1907 through watch_command), so always account for it
1908 manually. */
1909
1910 /* Count resources used by all watchpoints except B. */
c1fc2657 1911 i = hw_watchpoint_used_count_others (b, type, &other_type_used);
a1398e0c
PA
1912
1913 /* Add in the resources needed for B. */
c1fc2657 1914 i += hw_watchpoint_use_count (b);
a1398e0c
PA
1915
1916 target_resources_ok
1917 = target_can_use_hardware_watchpoint (type, i, other_type_used);
e09342b5 1918 if (target_resources_ok <= 0)
a9634178 1919 {
c1fc2657 1920 int sw_mode = b->ops->works_in_software_mode (b);
9c06b0b4
TJB
1921
1922 if (target_resources_ok == 0 && !sw_mode)
a9634178
TJB
1923 error (_("Target does not support this type of "
1924 "hardware watchpoint."));
9c06b0b4
TJB
1925 else if (target_resources_ok < 0 && !sw_mode)
1926 error (_("There are not enough available hardware "
1927 "resources for this watchpoint."));
a1398e0c
PA
1928
1929 /* Downgrade to software watchpoint. */
c1fc2657 1930 b->type = bp_watchpoint;
a1398e0c
PA
1931 }
1932 else
1933 {
1934 /* If this was a software watchpoint, we've just
1935 found we have enough resources to turn it to a
1936 hardware watchpoint. Otherwise, this is a
1937 nop. */
c1fc2657 1938 b->type = type;
a9634178 1939 }
9fa40276 1940 }
c1fc2657 1941 else if (!b->ops->works_in_software_mode (b))
638aa5a1
AB
1942 {
1943 if (!can_use_hw_watchpoints)
1944 error (_("Can't set read/access watchpoint when "
1945 "hardware watchpoints are disabled."));
1946 else
1947 error (_("Expression cannot be implemented with "
1948 "read/access watchpoint."));
1949 }
9fa40276 1950 else
c1fc2657 1951 b->type = bp_watchpoint;
9fa40276 1952
c1fc2657 1953 loc_type = (b->type == bp_watchpoint? bp_loc_other
9fa40276 1954 : bp_loc_hardware_watchpoint);
c1fc2657 1955 for (bl = b->loc; bl; bl = bl->next)
9fa40276
TJB
1956 bl->loc_type = loc_type;
1957 }
1958
c7437ca6
PA
1959 /* If a software watchpoint is not watching any memory, then the
1960 above left it without any location set up. But,
1961 bpstat_stop_status requires a location to be able to report
1962 stops, so make sure there's at least a dummy one. */
c1fc2657
SM
1963 if (b->type == bp_watchpoint && b->loc == NULL)
1964 software_watchpoint_add_no_memory_location (b, frame_pspace);
a5606eee
VP
1965 }
1966 else if (!within_current_scope)
7270d8f2 1967 {
ac74f770
MS
1968 printf_filtered (_("\
1969Watchpoint %d deleted because the program has left the block\n\
1970in which its expression is valid.\n"),
c1fc2657 1971 b->number);
d0fb5eae 1972 watchpoint_del_at_next_stop (b);
7270d8f2 1973 }
a5606eee
VP
1974
1975 /* Restore the selected frame. */
66076460
DJ
1976 if (frame_saved)
1977 select_frame (frame_find_by_id (saved_frame_id));
7270d8f2
OF
1978}
1979
a5606eee 1980
74960c60 1981/* Returns 1 iff breakpoint location should be
1e4d1764
YQ
1982 inserted in the inferior. We don't differentiate the type of BL's owner
1983 (breakpoint vs. tracepoint), although insert_location in tracepoint's
1984 breakpoint_ops is not defined, because in insert_bp_location,
1985 tracepoint's insert_location will not be called. */
74960c60 1986static int
35df4500 1987should_be_inserted (struct bp_location *bl)
74960c60 1988{
35df4500 1989 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
74960c60
VP
1990 return 0;
1991
35df4500 1992 if (bl->owner->disposition == disp_del_at_next_stop)
74960c60
VP
1993 return 0;
1994
35df4500 1995 if (!bl->enabled || bl->shlib_disabled || bl->duplicate)
74960c60
VP
1996 return 0;
1997
f8eba3c6
TT
1998 if (user_breakpoint_p (bl->owner) && bl->pspace->executing_startup)
1999 return 0;
2000
56710373
PA
2001 /* This is set for example, when we're attached to the parent of a
2002 vfork, and have detached from the child. The child is running
2003 free, and we expect it to do an exec or exit, at which point the
2004 OS makes the parent schedulable again (and the target reports
2005 that the vfork is done). Until the child is done with the shared
2006 memory region, do not insert breakpoints in the parent, otherwise
2007 the child could still trip on the parent's breakpoints. Since
2008 the parent is blocked anyway, it won't miss any breakpoint. */
35df4500 2009 if (bl->pspace->breakpoints_not_allowed)
56710373
PA
2010 return 0;
2011
31e77af2 2012 /* Don't insert a breakpoint if we're trying to step past its
21edc42f
YQ
2013 location, except if the breakpoint is a single-step breakpoint,
2014 and the breakpoint's thread is the thread which is stepping past
2015 a breakpoint. */
31e77af2
PA
2016 if ((bl->loc_type == bp_loc_software_breakpoint
2017 || bl->loc_type == bp_loc_hardware_breakpoint)
2018 && stepping_past_instruction_at (bl->pspace->aspace,
21edc42f
YQ
2019 bl->address)
2020 /* The single-step breakpoint may be inserted at the location
2021 we're trying to step if the instruction branches to itself.
2022 However, the instruction won't be executed at all and it may
2023 break the semantics of the instruction, for example, the
2024 instruction is a conditional branch or updates some flags.
2025 We can't fix it unless GDB is able to emulate the instruction
2026 or switch to displaced stepping. */
2027 && !(bl->owner->type == bp_single_step
2028 && thread_is_stepping_over_breakpoint (bl->owner->thread)))
e558d7c1
PA
2029 {
2030 if (debug_infrun)
2031 {
2032 fprintf_unfiltered (gdb_stdlog,
2033 "infrun: skipping breakpoint: "
2034 "stepping past insn at: %s\n",
2035 paddress (bl->gdbarch, bl->address));
2036 }
2037 return 0;
2038 }
31e77af2 2039
963f9c80
PA
2040 /* Don't insert watchpoints if we're trying to step past the
2041 instruction that triggered one. */
2042 if ((bl->loc_type == bp_loc_hardware_watchpoint)
2043 && stepping_past_nonsteppable_watchpoint ())
2044 {
2045 if (debug_infrun)
2046 {
2047 fprintf_unfiltered (gdb_stdlog,
2048 "infrun: stepping past non-steppable watchpoint. "
2049 "skipping watchpoint at %s:%d\n",
2050 paddress (bl->gdbarch, bl->address),
2051 bl->length);
2052 }
2053 return 0;
2054 }
2055
74960c60
VP
2056 return 1;
2057}
2058
934709f0
PW
2059/* Same as should_be_inserted but does the check assuming
2060 that the location is not duplicated. */
2061
2062static int
2063unduplicated_should_be_inserted (struct bp_location *bl)
2064{
2065 int result;
2066 const int save_duplicate = bl->duplicate;
2067
2068 bl->duplicate = 0;
2069 result = should_be_inserted (bl);
2070 bl->duplicate = save_duplicate;
2071 return result;
2072}
2073
b775012e
LM
2074/* Parses a conditional described by an expression COND into an
2075 agent expression bytecode suitable for evaluation
2076 by the bytecode interpreter. Return NULL if there was
2077 any error during parsing. */
2078
833177a4 2079static agent_expr_up
b775012e
LM
2080parse_cond_to_aexpr (CORE_ADDR scope, struct expression *cond)
2081{
833177a4 2082 if (cond == NULL)
b775012e
LM
2083 return NULL;
2084
833177a4
PA
2085 agent_expr_up aexpr;
2086
b775012e
LM
2087 /* We don't want to stop processing, so catch any errors
2088 that may show up. */
a70b8144 2089 try
b775012e 2090 {
036e657b 2091 aexpr = gen_eval_for_expr (scope, cond);
b775012e
LM
2092 }
2093
230d2906 2094 catch (const gdb_exception_error &ex)
b775012e
LM
2095 {
2096 /* If we got here, it means the condition could not be parsed to a valid
2097 bytecode expression and thus can't be evaluated on the target's side.
2098 It's no use iterating through the conditions. */
b775012e
LM
2099 }
2100
2101 /* We have a valid agent expression. */
2102 return aexpr;
2103}
2104
2105/* Based on location BL, create a list of breakpoint conditions to be
2106 passed on to the target. If we have duplicated locations with different
2107 conditions, we will add such conditions to the list. The idea is that the
2108 target will evaluate the list of conditions and will only notify GDB when
2109 one of them is true. */
2110
2111static void
2112build_target_condition_list (struct bp_location *bl)
2113{
2114 struct bp_location **locp = NULL, **loc2p;
2115 int null_condition_or_parse_error = 0;
2116 int modified = bl->needs_update;
2117 struct bp_location *loc;
2118
8b4f3082 2119 /* Release conditions left over from a previous insert. */
3cde5c42 2120 bl->target_info.conditions.clear ();
8b4f3082 2121
b775012e
LM
2122 /* This is only meaningful if the target is
2123 evaluating conditions and if the user has
2124 opted for condition evaluation on the target's
2125 side. */
2126 if (gdb_evaluates_breakpoint_condition_p ()
2127 || !target_supports_evaluation_of_breakpoint_conditions ())
2128 return;
2129
2130 /* Do a first pass to check for locations with no assigned
2131 conditions or conditions that fail to parse to a valid agent expression
2132 bytecode. If any of these happen, then it's no use to send conditions
2133 to the target since this location will always trigger and generate a
2134 response back to GDB. */
2135 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2136 {
2137 loc = (*loc2p);
2138 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2139 {
2140 if (modified)
2141 {
b775012e
LM
2142 /* Re-parse the conditions since something changed. In that
2143 case we already freed the condition bytecodes (see
2144 force_breakpoint_reinsertion). We just
2145 need to parse the condition to bytecodes again. */
833177a4
PA
2146 loc->cond_bytecode = parse_cond_to_aexpr (bl->address,
2147 loc->cond.get ());
b775012e
LM
2148 }
2149
2150 /* If we have a NULL bytecode expression, it means something
2151 went wrong or we have a null condition expression. */
2152 if (!loc->cond_bytecode)
2153 {
2154 null_condition_or_parse_error = 1;
2155 break;
2156 }
2157 }
2158 }
2159
2160 /* If any of these happened, it means we will have to evaluate the conditions
2161 for the location's address on gdb's side. It is no use keeping bytecodes
2162 for all the other duplicate locations, thus we free all of them here.
2163
2164 This is so we have a finer control over which locations' conditions are
2165 being evaluated by GDB or the remote stub. */
2166 if (null_condition_or_parse_error)
2167 {
2168 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2169 {
2170 loc = (*loc2p);
2171 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2172 {
2173 /* Only go as far as the first NULL bytecode is
2174 located. */
2175 if (!loc->cond_bytecode)
2176 return;
2177
833177a4 2178 loc->cond_bytecode.reset ();
b775012e
LM
2179 }
2180 }
2181 }
2182
2183 /* No NULL conditions or failed bytecode generation. Build a condition list
2184 for this location's address. */
2185 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2186 {
2187 loc = (*loc2p);
2188 if (loc->cond
2189 && is_breakpoint (loc->owner)
2190 && loc->pspace->num == bl->pspace->num
2191 && loc->owner->enable_state == bp_enabled
2192 && loc->enabled)
3cde5c42
PA
2193 {
2194 /* Add the condition to the vector. This will be used later
2195 to send the conditions to the target. */
2196 bl->target_info.conditions.push_back (loc->cond_bytecode.get ());
2197 }
b775012e
LM
2198 }
2199
2200 return;
2201}
2202
d3ce09f5
SS
2203/* Parses a command described by string CMD into an agent expression
2204 bytecode suitable for evaluation by the bytecode interpreter.
2205 Return NULL if there was any error during parsing. */
2206
833177a4 2207static agent_expr_up
d3ce09f5
SS
2208parse_cmd_to_aexpr (CORE_ADDR scope, char *cmd)
2209{
bbc13ae3
KS
2210 const char *cmdrest;
2211 const char *format_start, *format_end;
d3ce09f5
SS
2212 struct gdbarch *gdbarch = get_current_arch ();
2213
833177a4 2214 if (cmd == NULL)
d3ce09f5
SS
2215 return NULL;
2216
2217 cmdrest = cmd;
2218
2219 if (*cmdrest == ',')
2220 ++cmdrest;
f1735a53 2221 cmdrest = skip_spaces (cmdrest);
d3ce09f5
SS
2222
2223 if (*cmdrest++ != '"')
2224 error (_("No format string following the location"));
2225
2226 format_start = cmdrest;
2227
8e481c3b 2228 format_pieces fpieces (&cmdrest);
d3ce09f5
SS
2229
2230 format_end = cmdrest;
2231
2232 if (*cmdrest++ != '"')
2233 error (_("Bad format string, non-terminated '\"'."));
2234
f1735a53 2235 cmdrest = skip_spaces (cmdrest);
d3ce09f5
SS
2236
2237 if (!(*cmdrest == ',' || *cmdrest == '\0'))
2238 error (_("Invalid argument syntax"));
2239
2240 if (*cmdrest == ',')
2241 cmdrest++;
f1735a53 2242 cmdrest = skip_spaces (cmdrest);
d3ce09f5
SS
2243
2244 /* For each argument, make an expression. */
2245
8e481c3b 2246 std::vector<struct expression *> argvec;
d3ce09f5
SS
2247 while (*cmdrest != '\0')
2248 {
bbc13ae3 2249 const char *cmd1;
d3ce09f5
SS
2250
2251 cmd1 = cmdrest;
4d01a485 2252 expression_up expr = parse_exp_1 (&cmd1, scope, block_for_pc (scope), 1);
8e481c3b 2253 argvec.push_back (expr.release ());
d3ce09f5
SS
2254 cmdrest = cmd1;
2255 if (*cmdrest == ',')
2256 ++cmdrest;
2257 }
2258
833177a4
PA
2259 agent_expr_up aexpr;
2260
d3ce09f5
SS
2261 /* We don't want to stop processing, so catch any errors
2262 that may show up. */
a70b8144 2263 try
d3ce09f5 2264 {
036e657b
JB
2265 aexpr = gen_printf (scope, gdbarch, 0, 0,
2266 format_start, format_end - format_start,
8e481c3b 2267 argvec.size (), argvec.data ());
d3ce09f5 2268 }
230d2906 2269 catch (const gdb_exception_error &ex)
d3ce09f5
SS
2270 {
2271 /* If we got here, it means the command could not be parsed to a valid
2272 bytecode expression and thus can't be evaluated on the target's side.
2273 It's no use iterating through the other commands. */
d3ce09f5 2274 }
492d29ea 2275
d3ce09f5
SS
2276 /* We have a valid agent expression, return it. */
2277 return aexpr;
2278}
2279
2280/* Based on location BL, create a list of breakpoint commands to be
2281 passed on to the target. If we have duplicated locations with
2282 different commands, we will add any such to the list. */
2283
2284static void
2285build_target_command_list (struct bp_location *bl)
2286{
2287 struct bp_location **locp = NULL, **loc2p;
2288 int null_command_or_parse_error = 0;
2289 int modified = bl->needs_update;
2290 struct bp_location *loc;
2291
3cde5c42
PA
2292 /* Clear commands left over from a previous insert. */
2293 bl->target_info.tcommands.clear ();
8b4f3082 2294
41fac0cf 2295 if (!target_can_run_breakpoint_commands ())
d3ce09f5
SS
2296 return;
2297
41fac0cf
PA
2298 /* For now, limit to agent-style dprintf breakpoints. */
2299 if (dprintf_style != dprintf_style_agent)
d3ce09f5
SS
2300 return;
2301
41fac0cf
PA
2302 /* For now, if we have any duplicate location that isn't a dprintf,
2303 don't install the target-side commands, as that would make the
2304 breakpoint not be reported to the core, and we'd lose
2305 control. */
2306 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2307 {
2308 loc = (*loc2p);
2309 if (is_breakpoint (loc->owner)
2310 && loc->pspace->num == bl->pspace->num
2311 && loc->owner->type != bp_dprintf)
2312 return;
2313 }
2314
d3ce09f5
SS
2315 /* Do a first pass to check for locations with no assigned
2316 conditions or conditions that fail to parse to a valid agent expression
2317 bytecode. If any of these happen, then it's no use to send conditions
2318 to the target since this location will always trigger and generate a
2319 response back to GDB. */
2320 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2321 {
2322 loc = (*loc2p);
2323 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2324 {
2325 if (modified)
2326 {
d3ce09f5
SS
2327 /* Re-parse the commands since something changed. In that
2328 case we already freed the command bytecodes (see
2329 force_breakpoint_reinsertion). We just
2330 need to parse the command to bytecodes again. */
833177a4
PA
2331 loc->cmd_bytecode
2332 = parse_cmd_to_aexpr (bl->address,
2333 loc->owner->extra_string);
d3ce09f5
SS
2334 }
2335
2336 /* If we have a NULL bytecode expression, it means something
2337 went wrong or we have a null command expression. */
2338 if (!loc->cmd_bytecode)
2339 {
2340 null_command_or_parse_error = 1;
2341 break;
2342 }
2343 }
2344 }
2345
2346 /* If anything failed, then we're not doing target-side commands,
2347 and so clean up. */
2348 if (null_command_or_parse_error)
2349 {
2350 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2351 {
2352 loc = (*loc2p);
2353 if (is_breakpoint (loc->owner)
2354 && loc->pspace->num == bl->pspace->num)
2355 {
2356 /* Only go as far as the first NULL bytecode is
2357 located. */
40fb6c5e 2358 if (loc->cmd_bytecode == NULL)
d3ce09f5
SS
2359 return;
2360
833177a4 2361 loc->cmd_bytecode.reset ();
d3ce09f5
SS
2362 }
2363 }
2364 }
2365
2366 /* No NULL commands or failed bytecode generation. Build a command list
2367 for this location's address. */
2368 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2369 {
2370 loc = (*loc2p);
2371 if (loc->owner->extra_string
2372 && is_breakpoint (loc->owner)
2373 && loc->pspace->num == bl->pspace->num
2374 && loc->owner->enable_state == bp_enabled
2375 && loc->enabled)
3cde5c42
PA
2376 {
2377 /* Add the command to the vector. This will be used later
2378 to send the commands to the target. */
2379 bl->target_info.tcommands.push_back (loc->cmd_bytecode.get ());
2380 }
d3ce09f5
SS
2381 }
2382
2383 bl->target_info.persist = 0;
2384 /* Maybe flag this location as persistent. */
2385 if (bl->owner->type == bp_dprintf && disconnected_dprintf)
2386 bl->target_info.persist = 1;
2387}
2388
833b7ab5
YQ
2389/* Return the kind of breakpoint on address *ADDR. Get the kind
2390 of breakpoint according to ADDR except single-step breakpoint.
2391 Get the kind of single-step breakpoint according to the current
2392 registers state. */
cd6c3b4f
YQ
2393
2394static int
2395breakpoint_kind (struct bp_location *bl, CORE_ADDR *addr)
2396{
833b7ab5
YQ
2397 if (bl->owner->type == bp_single_step)
2398 {
2399 struct thread_info *thr = find_thread_global_id (bl->owner->thread);
2400 struct regcache *regcache;
2401
00431a78 2402 regcache = get_thread_regcache (thr);
833b7ab5
YQ
2403
2404 return gdbarch_breakpoint_kind_from_current_state (bl->gdbarch,
2405 regcache, addr);
2406 }
2407 else
2408 return gdbarch_breakpoint_kind_from_pc (bl->gdbarch, addr);
cd6c3b4f
YQ
2409}
2410
35df4500
TJB
2411/* Insert a low-level "breakpoint" of some type. BL is the breakpoint
2412 location. Any error messages are printed to TMP_ERROR_STREAM; and
3fbb6ffa 2413 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
c30eee59
TJB
2414 Returns 0 for success, 1 if the bp_location type is not supported or
2415 -1 for failure.
879bfdc2 2416
4a64f543
MS
2417 NOTE drow/2003-09-09: This routine could be broken down to an
2418 object-style method for each breakpoint or catchpoint type. */
26bb91f3 2419static int
35df4500 2420insert_bp_location (struct bp_location *bl,
26bb91f3 2421 struct ui_file *tmp_error_stream,
3fbb6ffa 2422 int *disabled_breaks,
dd61ec5c
MW
2423 int *hw_breakpoint_error,
2424 int *hw_bp_error_explained_already)
879bfdc2 2425{
cc06b668 2426 gdb_exception bp_excpt;
879bfdc2 2427
b775012e 2428 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
879bfdc2
DJ
2429 return 0;
2430
35c63cd8
JB
2431 /* Note we don't initialize bl->target_info, as that wipes out
2432 the breakpoint location's shadow_contents if the breakpoint
2433 is still inserted at that location. This in turn breaks
2434 target_read_memory which depends on these buffers when
2435 a memory read is requested at the breakpoint location:
2436 Once the target_info has been wiped, we fail to see that
2437 we have a breakpoint inserted at that address and thus
2438 read the breakpoint instead of returning the data saved in
2439 the breakpoint location's shadow contents. */
0d5ed153 2440 bl->target_info.reqstd_address = bl->address;
35df4500 2441 bl->target_info.placed_address_space = bl->pspace->aspace;
f1310107 2442 bl->target_info.length = bl->length;
8181d85f 2443
b775012e
LM
2444 /* When working with target-side conditions, we must pass all the conditions
2445 for the same breakpoint address down to the target since GDB will not
2446 insert those locations. With a list of breakpoint conditions, the target
2447 can decide when to stop and notify GDB. */
2448
2449 if (is_breakpoint (bl->owner))
2450 {
2451 build_target_condition_list (bl);
d3ce09f5
SS
2452 build_target_command_list (bl);
2453 /* Reset the modification marker. */
b775012e
LM
2454 bl->needs_update = 0;
2455 }
2456
35df4500
TJB
2457 if (bl->loc_type == bp_loc_software_breakpoint
2458 || bl->loc_type == bp_loc_hardware_breakpoint)
879bfdc2 2459 {
35df4500 2460 if (bl->owner->type != bp_hardware_breakpoint)
765dc015
VP
2461 {
2462 /* If the explicitly specified breakpoint type
2463 is not hardware breakpoint, check the memory map to see
2464 if the breakpoint address is in read only memory or not.
4a64f543 2465
765dc015
VP
2466 Two important cases are:
2467 - location type is not hardware breakpoint, memory
2468 is readonly. We change the type of the location to
2469 hardware breakpoint.
4a64f543
MS
2470 - location type is hardware breakpoint, memory is
2471 read-write. This means we've previously made the
2472 location hardware one, but then the memory map changed,
2473 so we undo.
765dc015 2474
4a64f543
MS
2475 When breakpoints are removed, remove_breakpoints will use
2476 location types we've just set here, the only possible
2477 problem is that memory map has changed during running
2478 program, but it's not going to work anyway with current
2479 gdb. */
765dc015 2480 struct mem_region *mr
0d5ed153 2481 = lookup_mem_region (bl->target_info.reqstd_address);
765dc015
VP
2482
2483 if (mr)
2484 {
2485 if (automatic_hardware_breakpoints)
2486 {
765dc015
VP
2487 enum bp_loc_type new_type;
2488
2489 if (mr->attrib.mode != MEM_RW)
2490 new_type = bp_loc_hardware_breakpoint;
2491 else
2492 new_type = bp_loc_software_breakpoint;
2493
35df4500 2494 if (new_type != bl->loc_type)
765dc015
VP
2495 {
2496 static int said = 0;
cc59ec59 2497
35df4500 2498 bl->loc_type = new_type;
765dc015
VP
2499 if (!said)
2500 {
3e43a32a
MS
2501 fprintf_filtered (gdb_stdout,
2502 _("Note: automatically using "
2503 "hardware breakpoints for "
2504 "read-only addresses.\n"));
765dc015
VP
2505 said = 1;
2506 }
2507 }
2508 }
35df4500 2509 else if (bl->loc_type == bp_loc_software_breakpoint
0fec99e8
PA
2510 && mr->attrib.mode != MEM_RW)
2511 {
2512 fprintf_unfiltered (tmp_error_stream,
2513 _("Cannot insert breakpoint %d.\n"
2514 "Cannot set software breakpoint "
2515 "at read-only address %s\n"),
2516 bl->owner->number,
2517 paddress (bl->gdbarch, bl->address));
2518 return 1;
2519 }
765dc015
VP
2520 }
2521 }
2522
879bfdc2
DJ
2523 /* First check to see if we have to handle an overlay. */
2524 if (overlay_debugging == ovly_off
35df4500
TJB
2525 || bl->section == NULL
2526 || !(section_is_overlay (bl->section)))
879bfdc2
DJ
2527 {
2528 /* No overlay handling: just set the breakpoint. */
a70b8144 2529 try
dd61ec5c 2530 {
0000e5cc
PA
2531 int val;
2532
dd61ec5c 2533 val = bl->owner->ops->insert_location (bl);
0000e5cc 2534 if (val)
688fca4f 2535 bp_excpt = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
dd61ec5c 2536 }
94aeb44b 2537 catch (gdb_exception &e)
dd61ec5c 2538 {
94aeb44b 2539 bp_excpt = std::move (e);
dd61ec5c 2540 }
879bfdc2
DJ
2541 }
2542 else
2543 {
4a64f543 2544 /* This breakpoint is in an overlay section.
879bfdc2
DJ
2545 Shall we set a breakpoint at the LMA? */
2546 if (!overlay_events_enabled)
2547 {
2548 /* Yes -- overlay event support is not active,
2549 so we must try to set a breakpoint at the LMA.
2550 This will not work for a hardware breakpoint. */
35df4500 2551 if (bl->loc_type == bp_loc_hardware_breakpoint)
8a3fe4f8 2552 warning (_("hardware breakpoint %d not supported in overlay!"),
35df4500 2553 bl->owner->number);
879bfdc2
DJ
2554 else
2555 {
35df4500
TJB
2556 CORE_ADDR addr = overlay_unmapped_address (bl->address,
2557 bl->section);
879bfdc2 2558 /* Set a software (trap) breakpoint at the LMA. */
35df4500 2559 bl->overlay_target_info = bl->target_info;
0d5ed153 2560 bl->overlay_target_info.reqstd_address = addr;
0000e5cc
PA
2561
2562 /* No overlay handling: just set the breakpoint. */
a70b8144 2563 try
0000e5cc
PA
2564 {
2565 int val;
2566
579c6ad9 2567 bl->overlay_target_info.kind
cd6c3b4f
YQ
2568 = breakpoint_kind (bl, &addr);
2569 bl->overlay_target_info.placed_address = addr;
0000e5cc
PA
2570 val = target_insert_breakpoint (bl->gdbarch,
2571 &bl->overlay_target_info);
2572 if (val)
688fca4f
PA
2573 bp_excpt
2574 = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
0000e5cc 2575 }
94aeb44b 2576 catch (gdb_exception &e)
0000e5cc 2577 {
94aeb44b 2578 bp_excpt = std::move (e);
0000e5cc
PA
2579 }
2580
688fca4f 2581 if (bp_excpt.reason != 0)
99361f52 2582 fprintf_unfiltered (tmp_error_stream,
3e43a32a
MS
2583 "Overlay breakpoint %d "
2584 "failed: in ROM?\n",
35df4500 2585 bl->owner->number);
879bfdc2
DJ
2586 }
2587 }
2588 /* Shall we set a breakpoint at the VMA? */
35df4500 2589 if (section_is_mapped (bl->section))
879bfdc2
DJ
2590 {
2591 /* Yes. This overlay section is mapped into memory. */
a70b8144 2592 try
dd61ec5c 2593 {
0000e5cc
PA
2594 int val;
2595
dd61ec5c 2596 val = bl->owner->ops->insert_location (bl);
0000e5cc 2597 if (val)
688fca4f 2598 bp_excpt = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
dd61ec5c 2599 }
94aeb44b 2600 catch (gdb_exception &e)
dd61ec5c 2601 {
94aeb44b 2602 bp_excpt = std::move (e);
dd61ec5c 2603 }
879bfdc2
DJ
2604 }
2605 else
2606 {
2607 /* No. This breakpoint will not be inserted.
2608 No error, but do not mark the bp as 'inserted'. */
2609 return 0;
2610 }
2611 }
2612
688fca4f 2613 if (bp_excpt.reason != 0)
879bfdc2
DJ
2614 {
2615 /* Can't set the breakpoint. */
0000e5cc
PA
2616
2617 /* In some cases, we might not be able to insert a
2618 breakpoint in a shared library that has already been
2619 removed, but we have not yet processed the shlib unload
2620 event. Unfortunately, some targets that implement
076855f9
PA
2621 breakpoint insertion themselves can't tell why the
2622 breakpoint insertion failed (e.g., the remote target
2623 doesn't define error codes), so we must treat generic
2624 errors as memory errors. */
688fca4f
PA
2625 if (bp_excpt.reason == RETURN_ERROR
2626 && (bp_excpt.error == GENERIC_ERROR
2627 || bp_excpt.error == MEMORY_ERROR)
076855f9 2628 && bl->loc_type == bp_loc_software_breakpoint
08351840 2629 && (solib_name_from_address (bl->pspace, bl->address)
d03de421
PA
2630 || shared_objfile_contains_address_p (bl->pspace,
2631 bl->address)))
879bfdc2 2632 {
4a64f543 2633 /* See also: disable_breakpoints_in_shlibs. */
35df4500 2634 bl->shlib_disabled = 1;
76727919 2635 gdb::observers::breakpoint_modified.notify (bl->owner);
3fbb6ffa
TJB
2636 if (!*disabled_breaks)
2637 {
2638 fprintf_unfiltered (tmp_error_stream,
2639 "Cannot insert breakpoint %d.\n",
2640 bl->owner->number);
2641 fprintf_unfiltered (tmp_error_stream,
2642 "Temporarily disabling shared "
2643 "library breakpoints:\n");
2644 }
2645 *disabled_breaks = 1;
879bfdc2 2646 fprintf_unfiltered (tmp_error_stream,
35df4500 2647 "breakpoint #%d\n", bl->owner->number);
0000e5cc 2648 return 0;
879bfdc2
DJ
2649 }
2650 else
879bfdc2 2651 {
35df4500 2652 if (bl->loc_type == bp_loc_hardware_breakpoint)
879bfdc2 2653 {
0000e5cc 2654 *hw_breakpoint_error = 1;
688fca4f 2655 *hw_bp_error_explained_already = bp_excpt.message != NULL;
dd61ec5c
MW
2656 fprintf_unfiltered (tmp_error_stream,
2657 "Cannot insert hardware breakpoint %d%s",
688fca4f
PA
2658 bl->owner->number,
2659 bp_excpt.message ? ":" : ".\n");
2660 if (bp_excpt.message != NULL)
2661 fprintf_unfiltered (tmp_error_stream, "%s.\n",
3d6e9d23 2662 bp_excpt.what ());
879bfdc2
DJ
2663 }
2664 else
2665 {
688fca4f 2666 if (bp_excpt.message == NULL)
0000e5cc 2667 {
1ccbe998 2668 std::string message
0000e5cc
PA
2669 = memory_error_message (TARGET_XFER_E_IO,
2670 bl->gdbarch, bl->address);
0000e5cc
PA
2671
2672 fprintf_unfiltered (tmp_error_stream,
2673 "Cannot insert breakpoint %d.\n"
2674 "%s\n",
1ccbe998 2675 bl->owner->number, message.c_str ());
0000e5cc
PA
2676 }
2677 else
2678 {
2679 fprintf_unfiltered (tmp_error_stream,
2680 "Cannot insert breakpoint %d: %s\n",
2681 bl->owner->number,
3d6e9d23 2682 bp_excpt.what ());
0000e5cc 2683 }
879bfdc2 2684 }
0000e5cc 2685 return 1;
879bfdc2
DJ
2686
2687 }
2688 }
2689 else
35df4500 2690 bl->inserted = 1;
879bfdc2 2691
0000e5cc 2692 return 0;
879bfdc2
DJ
2693 }
2694
35df4500 2695 else if (bl->loc_type == bp_loc_hardware_watchpoint
879bfdc2 2696 /* NOTE drow/2003-09-08: This state only exists for removing
4a64f543 2697 watchpoints. It's not clear that it's necessary... */
35df4500 2698 && bl->owner->disposition != disp_del_at_next_stop)
879bfdc2 2699 {
0000e5cc
PA
2700 int val;
2701
77b06cd7
TJB
2702 gdb_assert (bl->owner->ops != NULL
2703 && bl->owner->ops->insert_location != NULL);
2704
2705 val = bl->owner->ops->insert_location (bl);
85d721b8
PA
2706
2707 /* If trying to set a read-watchpoint, and it turns out it's not
2708 supported, try emulating one with an access watchpoint. */
35df4500 2709 if (val == 1 && bl->watchpoint_type == hw_read)
85d721b8
PA
2710 {
2711 struct bp_location *loc, **loc_temp;
2712
2713 /* But don't try to insert it, if there's already another
2714 hw_access location that would be considered a duplicate
2715 of this one. */
2716 ALL_BP_LOCATIONS (loc, loc_temp)
35df4500 2717 if (loc != bl
85d721b8 2718 && loc->watchpoint_type == hw_access
35df4500 2719 && watchpoint_locations_match (bl, loc))
85d721b8 2720 {
35df4500
TJB
2721 bl->duplicate = 1;
2722 bl->inserted = 1;
2723 bl->target_info = loc->target_info;
2724 bl->watchpoint_type = hw_access;
85d721b8
PA
2725 val = 0;
2726 break;
2727 }
2728
2729 if (val == 1)
2730 {
77b06cd7
TJB
2731 bl->watchpoint_type = hw_access;
2732 val = bl->owner->ops->insert_location (bl);
2733
2734 if (val)
2735 /* Back to the original value. */
2736 bl->watchpoint_type = hw_read;
85d721b8
PA
2737 }
2738 }
2739
35df4500 2740 bl->inserted = (val == 0);
879bfdc2
DJ
2741 }
2742
35df4500 2743 else if (bl->owner->type == bp_catchpoint)
879bfdc2 2744 {
0000e5cc
PA
2745 int val;
2746
77b06cd7
TJB
2747 gdb_assert (bl->owner->ops != NULL
2748 && bl->owner->ops->insert_location != NULL);
2749
2750 val = bl->owner->ops->insert_location (bl);
2751 if (val)
2752 {
2753 bl->owner->enable_state = bp_disabled;
2754
2755 if (val == 1)
2756 warning (_("\
2757Error inserting catchpoint %d: Your system does not support this type\n\
2758of catchpoint."), bl->owner->number);
2759 else
2760 warning (_("Error inserting catchpoint %d."), bl->owner->number);
2761 }
2762
2763 bl->inserted = (val == 0);
1640b821
DJ
2764
2765 /* We've already printed an error message if there was a problem
2766 inserting this catchpoint, and we've disabled the catchpoint,
2767 so just return success. */
2768 return 0;
879bfdc2
DJ
2769 }
2770
2771 return 0;
2772}
2773
6c95b8df
PA
2774/* This function is called when program space PSPACE is about to be
2775 deleted. It takes care of updating breakpoints to not reference
2776 PSPACE anymore. */
2777
2778void
2779breakpoint_program_space_exit (struct program_space *pspace)
2780{
2781 struct breakpoint *b, *b_temp;
876fa593 2782 struct bp_location *loc, **loc_temp;
6c95b8df
PA
2783
2784 /* Remove any breakpoint that was set through this program space. */
2785 ALL_BREAKPOINTS_SAFE (b, b_temp)
2786 {
2787 if (b->pspace == pspace)
2788 delete_breakpoint (b);
2789 }
2790
2791 /* Breakpoints set through other program spaces could have locations
2792 bound to PSPACE as well. Remove those. */
876fa593 2793 ALL_BP_LOCATIONS (loc, loc_temp)
6c95b8df
PA
2794 {
2795 struct bp_location *tmp;
2796
2797 if (loc->pspace == pspace)
2798 {
2bdf28a0 2799 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
6c95b8df
PA
2800 if (loc->owner->loc == loc)
2801 loc->owner->loc = loc->next;
2802 else
2803 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
2804 if (tmp->next == loc)
2805 {
2806 tmp->next = loc->next;
2807 break;
2808 }
2809 }
2810 }
2811
2812 /* Now update the global location list to permanently delete the
2813 removed locations above. */
44702360 2814 update_global_location_list (UGLL_DONT_INSERT);
6c95b8df
PA
2815}
2816
74960c60
VP
2817/* Make sure all breakpoints are inserted in inferior.
2818 Throws exception on any error.
2819 A breakpoint that is already inserted won't be inserted
2820 again, so calling this function twice is safe. */
2821void
2822insert_breakpoints (void)
2823{
2824 struct breakpoint *bpt;
2825
2826 ALL_BREAKPOINTS (bpt)
2827 if (is_hardware_watchpoint (bpt))
3a5c3e22
PA
2828 {
2829 struct watchpoint *w = (struct watchpoint *) bpt;
2830
2831 update_watchpoint (w, 0 /* don't reparse. */);
2832 }
74960c60 2833
04086b45
PA
2834 /* Updating watchpoints creates new locations, so update the global
2835 location list. Explicitly tell ugll to insert locations and
2836 ignore breakpoints_always_inserted_mode. */
2837 update_global_location_list (UGLL_INSERT);
74960c60
VP
2838}
2839
20388dd6
YQ
2840/* Invoke CALLBACK for each of bp_location. */
2841
2842void
2843iterate_over_bp_locations (walk_bp_location_callback callback)
2844{
2845 struct bp_location *loc, **loc_tmp;
2846
2847 ALL_BP_LOCATIONS (loc, loc_tmp)
2848 {
2849 callback (loc, NULL);
2850 }
2851}
2852
b775012e
LM
2853/* This is used when we need to synch breakpoint conditions between GDB and the
2854 target. It is the case with deleting and disabling of breakpoints when using
2855 always-inserted mode. */
2856
2857static void
2858update_inserted_breakpoint_locations (void)
2859{
2860 struct bp_location *bl, **blp_tmp;
2861 int error_flag = 0;
2862 int val = 0;
2863 int disabled_breaks = 0;
2864 int hw_breakpoint_error = 0;
dd61ec5c 2865 int hw_bp_details_reported = 0;
b775012e 2866
d7e74731 2867 string_file tmp_error_stream;
b775012e
LM
2868
2869 /* Explicitly mark the warning -- this will only be printed if
2870 there was an error. */
d7e74731 2871 tmp_error_stream.puts ("Warning:\n");
b775012e 2872
5ed8105e 2873 scoped_restore_current_pspace_and_thread restore_pspace_thread;
b775012e
LM
2874
2875 ALL_BP_LOCATIONS (bl, blp_tmp)
2876 {
2877 /* We only want to update software breakpoints and hardware
2878 breakpoints. */
2879 if (!is_breakpoint (bl->owner))
2880 continue;
2881
2882 /* We only want to update locations that are already inserted
2883 and need updating. This is to avoid unwanted insertion during
2884 deletion of breakpoints. */
4daf1902 2885 if (!bl->inserted || !bl->needs_update)
b775012e
LM
2886 continue;
2887
2888 switch_to_program_space_and_thread (bl->pspace);
2889
2890 /* For targets that support global breakpoints, there's no need
2891 to select an inferior to insert breakpoint to. In fact, even
2892 if we aren't attached to any process yet, we should still
2893 insert breakpoints. */
f5656ead 2894 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
d7e15655 2895 && inferior_ptid == null_ptid)
b775012e
LM
2896 continue;
2897
d7e74731 2898 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
dd61ec5c 2899 &hw_breakpoint_error, &hw_bp_details_reported);
b775012e
LM
2900 if (val)
2901 error_flag = val;
2902 }
2903
2904 if (error_flag)
2905 {
223ffa71 2906 target_terminal::ours_for_output ();
b775012e
LM
2907 error_stream (tmp_error_stream);
2908 }
b775012e
LM
2909}
2910
c30eee59 2911/* Used when starting or continuing the program. */
c906108c 2912
74960c60
VP
2913static void
2914insert_breakpoint_locations (void)
c906108c 2915{
a5606eee 2916 struct breakpoint *bpt;
35df4500 2917 struct bp_location *bl, **blp_tmp;
eacd795a 2918 int error_flag = 0;
c906108c 2919 int val = 0;
3fbb6ffa 2920 int disabled_breaks = 0;
81d0cc19 2921 int hw_breakpoint_error = 0;
dd61ec5c 2922 int hw_bp_error_explained_already = 0;
c906108c 2923
d7e74731
PA
2924 string_file tmp_error_stream;
2925
81d0cc19
GS
2926 /* Explicitly mark the warning -- this will only be printed if
2927 there was an error. */
d7e74731 2928 tmp_error_stream.puts ("Warning:\n");
6c95b8df 2929
5ed8105e 2930 scoped_restore_current_pspace_and_thread restore_pspace_thread;
6c95b8df 2931
35df4500 2932 ALL_BP_LOCATIONS (bl, blp_tmp)
879bfdc2 2933 {
b775012e 2934 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
879bfdc2
DJ
2935 continue;
2936
4a64f543
MS
2937 /* There is no point inserting thread-specific breakpoints if
2938 the thread no longer exists. ALL_BP_LOCATIONS bp_location
2939 has BL->OWNER always non-NULL. */
35df4500 2940 if (bl->owner->thread != -1
5d5658a1 2941 && !valid_global_thread_id (bl->owner->thread))
f365de73
AS
2942 continue;
2943
35df4500 2944 switch_to_program_space_and_thread (bl->pspace);
6c95b8df
PA
2945
2946 /* For targets that support global breakpoints, there's no need
2947 to select an inferior to insert breakpoint to. In fact, even
2948 if we aren't attached to any process yet, we should still
2949 insert breakpoints. */
f5656ead 2950 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
d7e15655 2951 && inferior_ptid == null_ptid)
6c95b8df
PA
2952 continue;
2953
d7e74731 2954 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
dd61ec5c 2955 &hw_breakpoint_error, &hw_bp_error_explained_already);
879bfdc2 2956 if (val)
eacd795a 2957 error_flag = val;
879bfdc2 2958 }
c906108c 2959
4a64f543
MS
2960 /* If we failed to insert all locations of a watchpoint, remove
2961 them, as half-inserted watchpoint is of limited use. */
a5606eee
VP
2962 ALL_BREAKPOINTS (bpt)
2963 {
2964 int some_failed = 0;
2965 struct bp_location *loc;
2966
2967 if (!is_hardware_watchpoint (bpt))
2968 continue;
2969
d6b74ac4 2970 if (!breakpoint_enabled (bpt))
a5606eee 2971 continue;
74960c60
VP
2972
2973 if (bpt->disposition == disp_del_at_next_stop)
2974 continue;
a5606eee
VP
2975
2976 for (loc = bpt->loc; loc; loc = loc->next)
56710373 2977 if (!loc->inserted && should_be_inserted (loc))
a5606eee
VP
2978 {
2979 some_failed = 1;
2980 break;
2981 }
2982 if (some_failed)
2983 {
2984 for (loc = bpt->loc; loc; loc = loc->next)
2985 if (loc->inserted)
834c0d03 2986 remove_breakpoint (loc);
a5606eee
VP
2987
2988 hw_breakpoint_error = 1;
d7e74731
PA
2989 tmp_error_stream.printf ("Could not insert "
2990 "hardware watchpoint %d.\n",
2991 bpt->number);
eacd795a 2992 error_flag = -1;
a5606eee
VP
2993 }
2994 }
2995
eacd795a 2996 if (error_flag)
81d0cc19
GS
2997 {
2998 /* If a hardware breakpoint or watchpoint was inserted, add a
2999 message about possibly exhausted resources. */
dd61ec5c 3000 if (hw_breakpoint_error && !hw_bp_error_explained_already)
81d0cc19 3001 {
d7e74731 3002 tmp_error_stream.printf ("Could not insert hardware breakpoints:\n\
c6510018 3003You may have requested too many hardware breakpoints/watchpoints.\n");
81d0cc19 3004 }
223ffa71 3005 target_terminal::ours_for_output ();
81d0cc19
GS
3006 error_stream (tmp_error_stream);
3007 }
c906108c
SS
3008}
3009
c30eee59
TJB
3010/* Used when the program stops.
3011 Returns zero if successful, or non-zero if there was a problem
3012 removing a breakpoint location. */
3013
c906108c 3014int
fba45db2 3015remove_breakpoints (void)
c906108c 3016{
35df4500 3017 struct bp_location *bl, **blp_tmp;
3a1bae8e 3018 int val = 0;
c906108c 3019
35df4500 3020 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3021 {
1e4d1764 3022 if (bl->inserted && !is_tracepoint (bl->owner))
834c0d03 3023 val |= remove_breakpoint (bl);
c5aa993b 3024 }
3a1bae8e 3025 return val;
c906108c
SS
3026}
3027
49fa26b0
PA
3028/* When a thread exits, remove breakpoints that are related to
3029 that thread. */
3030
3031static void
3032remove_threaded_breakpoints (struct thread_info *tp, int silent)
3033{
3034 struct breakpoint *b, *b_tmp;
3035
3036 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3037 {
5d5658a1 3038 if (b->thread == tp->global_num && user_breakpoint_p (b))
49fa26b0
PA
3039 {
3040 b->disposition = disp_del_at_next_stop;
3041
3042 printf_filtered (_("\
43792cf0
PA
3043Thread-specific breakpoint %d deleted - thread %s no longer in the thread list.\n"),
3044 b->number, print_thread_id (tp));
49fa26b0
PA
3045
3046 /* Hide it from the user. */
3047 b->number = 0;
3048 }
3049 }
3050}
3051
f3869b1a 3052/* See breakpoint.h. */
6c95b8df 3053
f3869b1a 3054void
00431a78 3055remove_breakpoints_inf (inferior *inf)
6c95b8df 3056{
35df4500 3057 struct bp_location *bl, **blp_tmp;
6c95b8df 3058 int val;
6c95b8df 3059
35df4500 3060 ALL_BP_LOCATIONS (bl, blp_tmp)
6c95b8df 3061 {
35df4500 3062 if (bl->pspace != inf->pspace)
6c95b8df
PA
3063 continue;
3064
fc126975 3065 if (bl->inserted && !bl->target_info.persist)
6c95b8df 3066 {
834c0d03 3067 val = remove_breakpoint (bl);
6c95b8df 3068 if (val != 0)
f3869b1a 3069 return;
6c95b8df
PA
3070 }
3071 }
6c95b8df
PA
3072}
3073
e58b0e63
PA
3074static int internal_breakpoint_number = -1;
3075
84f4c1fe
PM
3076/* Set the breakpoint number of B, depending on the value of INTERNAL.
3077 If INTERNAL is non-zero, the breakpoint number will be populated
3078 from internal_breakpoint_number and that variable decremented.
e5dd4106 3079 Otherwise the breakpoint number will be populated from
84f4c1fe
PM
3080 breakpoint_count and that value incremented. Internal breakpoints
3081 do not set the internal var bpnum. */
3082static void
3083set_breakpoint_number (int internal, struct breakpoint *b)
3084{
3085 if (internal)
3086 b->number = internal_breakpoint_number--;
3087 else
3088 {
3089 set_breakpoint_count (breakpoint_count + 1);
3090 b->number = breakpoint_count;
3091 }
3092}
3093
e62c965a 3094static struct breakpoint *
a6d9a66e 3095create_internal_breakpoint (struct gdbarch *gdbarch,
06edf0c0 3096 CORE_ADDR address, enum bptype type,
c0a91b2b 3097 const struct breakpoint_ops *ops)
e62c965a 3098{
51abb421 3099 symtab_and_line sal;
e62c965a
PP
3100 sal.pc = address;
3101 sal.section = find_pc_overlay (sal.pc);
6c95b8df 3102 sal.pspace = current_program_space;
e62c965a 3103
51abb421 3104 breakpoint *b = set_raw_breakpoint (gdbarch, sal, type, ops);
e62c965a
PP
3105 b->number = internal_breakpoint_number--;
3106 b->disposition = disp_donttouch;
3107
3108 return b;
3109}
3110
17450429
PP
3111static const char *const longjmp_names[] =
3112 {
3113 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
3114 };
3115#define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
3116
3117/* Per-objfile data private to breakpoint.c. */
3118struct breakpoint_objfile_data
3119{
3120 /* Minimal symbol for "_ovly_debug_event" (if any). */
43dce439 3121 struct bound_minimal_symbol overlay_msym {};
17450429
PP
3122
3123 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
43dce439 3124 struct bound_minimal_symbol longjmp_msym[NUM_LONGJMP_NAMES] {};
17450429 3125
28106bc2 3126 /* True if we have looked for longjmp probes. */
43dce439 3127 int longjmp_searched = 0;
28106bc2 3128
45461e0d
SM
3129 /* SystemTap probe points for longjmp (if any). These are non-owning
3130 references. */
3131 std::vector<probe *> longjmp_probes;
28106bc2 3132
17450429 3133 /* Minimal symbol for "std::terminate()" (if any). */
43dce439 3134 struct bound_minimal_symbol terminate_msym {};
17450429
PP
3135
3136 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
43dce439 3137 struct bound_minimal_symbol exception_msym {};
28106bc2
SDJ
3138
3139 /* True if we have looked for exception probes. */
43dce439 3140 int exception_searched = 0;
28106bc2 3141
45461e0d
SM
3142 /* SystemTap probe points for unwinding (if any). These are non-owning
3143 references. */
3144 std::vector<probe *> exception_probes;
17450429
PP
3145};
3146
51d3063a
TT
3147static const struct objfile_key<breakpoint_objfile_data>
3148 breakpoint_objfile_key;
17450429
PP
3149
3150/* Minimal symbol not found sentinel. */
3151static struct minimal_symbol msym_not_found;
3152
3153/* Returns TRUE if MSYM point to the "not found" sentinel. */
3154
3155static int
3156msym_not_found_p (const struct minimal_symbol *msym)
3157{
3158 return msym == &msym_not_found;
3159}
3160
3161/* Return per-objfile data needed by breakpoint.c.
3162 Allocate the data if necessary. */
3163
3164static struct breakpoint_objfile_data *
3165get_breakpoint_objfile_data (struct objfile *objfile)
3166{
3167 struct breakpoint_objfile_data *bp_objfile_data;
3168
51d3063a 3169 bp_objfile_data = breakpoint_objfile_key.get (objfile);
17450429 3170 if (bp_objfile_data == NULL)
51d3063a 3171 bp_objfile_data = breakpoint_objfile_key.emplace (objfile);
17450429
PP
3172 return bp_objfile_data;
3173}
3174
e62c965a 3175static void
af02033e 3176create_overlay_event_breakpoint (void)
e62c965a 3177{
af02033e 3178 const char *const func_name = "_ovly_debug_event";
e62c965a 3179
2030c079 3180 for (objfile *objfile : current_program_space->objfiles ())
69de3c6a
PP
3181 {
3182 struct breakpoint *b;
17450429
PP
3183 struct breakpoint_objfile_data *bp_objfile_data;
3184 CORE_ADDR addr;
67994074 3185 struct explicit_location explicit_loc;
69de3c6a 3186
17450429
PP
3187 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3188
3b7344d5 3189 if (msym_not_found_p (bp_objfile_data->overlay_msym.minsym))
17450429
PP
3190 continue;
3191
3b7344d5 3192 if (bp_objfile_data->overlay_msym.minsym == NULL)
17450429 3193 {
3b7344d5 3194 struct bound_minimal_symbol m;
17450429
PP
3195
3196 m = lookup_minimal_symbol_text (func_name, objfile);
3b7344d5 3197 if (m.minsym == NULL)
17450429
PP
3198 {
3199 /* Avoid future lookups in this objfile. */
3b7344d5 3200 bp_objfile_data->overlay_msym.minsym = &msym_not_found;
17450429
PP
3201 continue;
3202 }
3203 bp_objfile_data->overlay_msym = m;
3204 }
e62c965a 3205
77e371c0 3206 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->overlay_msym);
17450429 3207 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
06edf0c0
PA
3208 bp_overlay_event,
3209 &internal_breakpoint_ops);
67994074
KS
3210 initialize_explicit_location (&explicit_loc);
3211 explicit_loc.function_name = ASTRDUP (func_name);
d28cd78a 3212 b->location = new_explicit_location (&explicit_loc);
e62c965a 3213
69de3c6a
PP
3214 if (overlay_debugging == ovly_auto)
3215 {
3216 b->enable_state = bp_enabled;
3217 overlay_events_enabled = 1;
3218 }
3219 else
3220 {
3221 b->enable_state = bp_disabled;
3222 overlay_events_enabled = 0;
3223 }
e62c965a 3224 }
e62c965a
PP
3225}
3226
0fd8e87f 3227static void
af02033e 3228create_longjmp_master_breakpoint (void)
0fd8e87f 3229{
6c95b8df 3230 struct program_space *pspace;
6c95b8df 3231
5ed8105e 3232 scoped_restore_current_program_space restore_pspace;
0fd8e87f 3233
6c95b8df 3234 ALL_PSPACES (pspace)
af02033e 3235 {
af02033e
PP
3236 set_current_program_space (pspace);
3237
2030c079 3238 for (objfile *objfile : current_program_space->objfiles ())
aed57c53
TT
3239 {
3240 int i;
3241 struct gdbarch *gdbarch;
3242 struct breakpoint_objfile_data *bp_objfile_data;
0fd8e87f 3243
aed57c53 3244 gdbarch = get_objfile_arch (objfile);
0fd8e87f 3245
aed57c53 3246 bp_objfile_data = get_breakpoint_objfile_data (objfile);
17450429 3247
aed57c53
TT
3248 if (!bp_objfile_data->longjmp_searched)
3249 {
3250 std::vector<probe *> ret
3251 = find_probes_in_objfile (objfile, "libc", "longjmp");
25f9533e 3252
aed57c53
TT
3253 if (!ret.empty ())
3254 {
3255 /* We are only interested in checking one element. */
3256 probe *p = ret[0];
3257
3258 if (!p->can_evaluate_arguments ())
3259 {
3260 /* We cannot use the probe interface here, because it does
3261 not know how to evaluate arguments. */
3262 ret.clear ();
3263 }
3264 }
3265 bp_objfile_data->longjmp_probes = ret;
3266 bp_objfile_data->longjmp_searched = 1;
3267 }
25f9533e 3268
aed57c53
TT
3269 if (!bp_objfile_data->longjmp_probes.empty ())
3270 {
3271 for (probe *p : bp_objfile_data->longjmp_probes)
3272 {
3273 struct breakpoint *b;
3274
3275 b = create_internal_breakpoint (gdbarch,
3276 p->get_relocated_address (objfile),
3277 bp_longjmp_master,
3278 &internal_breakpoint_ops);
3279 b->location = new_probe_location ("-probe-stap libc:longjmp");
3280 b->enable_state = bp_disabled;
3281 }
28106bc2 3282
aed57c53
TT
3283 continue;
3284 }
28106bc2 3285
aed57c53 3286 if (!gdbarch_get_longjmp_target_p (gdbarch))
28106bc2 3287 continue;
28106bc2 3288
aed57c53
TT
3289 for (i = 0; i < NUM_LONGJMP_NAMES; i++)
3290 {
3291 struct breakpoint *b;
3292 const char *func_name;
3293 CORE_ADDR addr;
3294 struct explicit_location explicit_loc;
0fd8e87f 3295
aed57c53
TT
3296 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i].minsym))
3297 continue;
17450429 3298
aed57c53
TT
3299 func_name = longjmp_names[i];
3300 if (bp_objfile_data->longjmp_msym[i].minsym == NULL)
3301 {
3302 struct bound_minimal_symbol m;
3303
3304 m = lookup_minimal_symbol_text (func_name, objfile);
3305 if (m.minsym == NULL)
3306 {
3307 /* Prevent future lookups in this objfile. */
3308 bp_objfile_data->longjmp_msym[i].minsym = &msym_not_found;
3309 continue;
3310 }
3311 bp_objfile_data->longjmp_msym[i] = m;
3312 }
17450429 3313
aed57c53
TT
3314 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->longjmp_msym[i]);
3315 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master,
3316 &internal_breakpoint_ops);
3317 initialize_explicit_location (&explicit_loc);
3318 explicit_loc.function_name = ASTRDUP (func_name);
3319 b->location = new_explicit_location (&explicit_loc);
3320 b->enable_state = bp_disabled;
3321 }
3322 }
af02033e 3323 }
0fd8e87f
UW
3324}
3325
af02033e 3326/* Create a master std::terminate breakpoint. */
aa7d318d 3327static void
af02033e 3328create_std_terminate_master_breakpoint (void)
aa7d318d
TT
3329{
3330 struct program_space *pspace;
af02033e 3331 const char *const func_name = "std::terminate()";
aa7d318d 3332
5ed8105e 3333 scoped_restore_current_program_space restore_pspace;
aa7d318d
TT
3334
3335 ALL_PSPACES (pspace)
17450429 3336 {
17450429
PP
3337 CORE_ADDR addr;
3338
3339 set_current_program_space (pspace);
3340
2030c079 3341 for (objfile *objfile : current_program_space->objfiles ())
aed57c53
TT
3342 {
3343 struct breakpoint *b;
3344 struct breakpoint_objfile_data *bp_objfile_data;
3345 struct explicit_location explicit_loc;
aa7d318d 3346
aed57c53 3347 bp_objfile_data = get_breakpoint_objfile_data (objfile);
aa7d318d 3348
aed57c53
TT
3349 if (msym_not_found_p (bp_objfile_data->terminate_msym.minsym))
3350 continue;
17450429 3351
aed57c53
TT
3352 if (bp_objfile_data->terminate_msym.minsym == NULL)
3353 {
3354 struct bound_minimal_symbol m;
17450429 3355
aed57c53
TT
3356 m = lookup_minimal_symbol (func_name, NULL, objfile);
3357 if (m.minsym == NULL || (MSYMBOL_TYPE (m.minsym) != mst_text
3358 && MSYMBOL_TYPE (m.minsym) != mst_file_text))
3359 {
3360 /* Prevent future lookups in this objfile. */
3361 bp_objfile_data->terminate_msym.minsym = &msym_not_found;
3362 continue;
3363 }
3364 bp_objfile_data->terminate_msym = m;
3365 }
aa7d318d 3366
aed57c53
TT
3367 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->terminate_msym);
3368 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
3369 bp_std_terminate_master,
3370 &internal_breakpoint_ops);
3371 initialize_explicit_location (&explicit_loc);
3372 explicit_loc.function_name = ASTRDUP (func_name);
3373 b->location = new_explicit_location (&explicit_loc);
3374 b->enable_state = bp_disabled;
3375 }
17450429 3376 }
aa7d318d
TT
3377}
3378
186c406b
TT
3379/* Install a master breakpoint on the unwinder's debug hook. */
3380
70221824 3381static void
186c406b
TT
3382create_exception_master_breakpoint (void)
3383{
17450429 3384 const char *const func_name = "_Unwind_DebugHook";
186c406b 3385
2030c079 3386 for (objfile *objfile : current_program_space->objfiles ())
186c406b 3387 {
17450429
PP
3388 struct breakpoint *b;
3389 struct gdbarch *gdbarch;
3390 struct breakpoint_objfile_data *bp_objfile_data;
3391 CORE_ADDR addr;
67994074 3392 struct explicit_location explicit_loc;
17450429
PP
3393
3394 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3395
28106bc2
SDJ
3396 /* We prefer the SystemTap probe point if it exists. */
3397 if (!bp_objfile_data->exception_searched)
3398 {
45461e0d
SM
3399 std::vector<probe *> ret
3400 = find_probes_in_objfile (objfile, "libgcc", "unwind");
25f9533e 3401
45461e0d 3402 if (!ret.empty ())
25f9533e
SDJ
3403 {
3404 /* We are only interested in checking one element. */
45461e0d 3405 probe *p = ret[0];
25f9533e 3406
935676c9 3407 if (!p->can_evaluate_arguments ())
25f9533e
SDJ
3408 {
3409 /* We cannot use the probe interface here, because it does
3410 not know how to evaluate arguments. */
45461e0d 3411 ret.clear ();
25f9533e
SDJ
3412 }
3413 }
3414 bp_objfile_data->exception_probes = ret;
28106bc2
SDJ
3415 bp_objfile_data->exception_searched = 1;
3416 }
3417
45461e0d 3418 if (!bp_objfile_data->exception_probes.empty ())
28106bc2 3419 {
b926417a 3420 gdbarch = get_objfile_arch (objfile);
45461e0d
SM
3421
3422 for (probe *p : bp_objfile_data->exception_probes)
28106bc2 3423 {
729662a5 3424 b = create_internal_breakpoint (gdbarch,
935676c9 3425 p->get_relocated_address (objfile),
28106bc2
SDJ
3426 bp_exception_master,
3427 &internal_breakpoint_ops);
d28cd78a 3428 b->location = new_probe_location ("-probe-stap libgcc:unwind");
28106bc2
SDJ
3429 b->enable_state = bp_disabled;
3430 }
3431
3432 continue;
3433 }
3434
3435 /* Otherwise, try the hook function. */
3436
3b7344d5 3437 if (msym_not_found_p (bp_objfile_data->exception_msym.minsym))
17450429
PP
3438 continue;
3439
3440 gdbarch = get_objfile_arch (objfile);
186c406b 3441
3b7344d5 3442 if (bp_objfile_data->exception_msym.minsym == NULL)
186c406b 3443 {
3b7344d5 3444 struct bound_minimal_symbol debug_hook;
186c406b 3445
17450429 3446 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
3b7344d5 3447 if (debug_hook.minsym == NULL)
17450429 3448 {
3b7344d5 3449 bp_objfile_data->exception_msym.minsym = &msym_not_found;
17450429
PP
3450 continue;
3451 }
3452
3453 bp_objfile_data->exception_msym = debug_hook;
186c406b 3454 }
17450429 3455
77e371c0 3456 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->exception_msym);
8b88a78e
PA
3457 addr = gdbarch_convert_from_func_ptr_addr (gdbarch, addr,
3458 current_top_target ());
06edf0c0
PA
3459 b = create_internal_breakpoint (gdbarch, addr, bp_exception_master,
3460 &internal_breakpoint_ops);
67994074
KS
3461 initialize_explicit_location (&explicit_loc);
3462 explicit_loc.function_name = ASTRDUP (func_name);
d28cd78a 3463 b->location = new_explicit_location (&explicit_loc);
17450429 3464 b->enable_state = bp_disabled;
186c406b 3465 }
186c406b
TT
3466}
3467
9ef9e6a6
KS
3468/* Does B have a location spec? */
3469
3470static int
3471breakpoint_event_location_empty_p (const struct breakpoint *b)
3472{
d28cd78a 3473 return b->location != NULL && event_location_empty_p (b->location.get ());
9ef9e6a6
KS
3474}
3475
c906108c 3476void
fba45db2 3477update_breakpoints_after_exec (void)
c906108c 3478{
35df4500 3479 struct breakpoint *b, *b_tmp;
876fa593 3480 struct bp_location *bploc, **bplocp_tmp;
c906108c 3481
25b22b0a
PA
3482 /* We're about to delete breakpoints from GDB's lists. If the
3483 INSERTED flag is true, GDB will try to lift the breakpoints by
3484 writing the breakpoints' "shadow contents" back into memory. The
3485 "shadow contents" are NOT valid after an exec, so GDB should not
3486 do that. Instead, the target is responsible from marking
3487 breakpoints out as soon as it detects an exec. We don't do that
3488 here instead, because there may be other attempts to delete
3489 breakpoints after detecting an exec and before reaching here. */
876fa593 3490 ALL_BP_LOCATIONS (bploc, bplocp_tmp)
6c95b8df
PA
3491 if (bploc->pspace == current_program_space)
3492 gdb_assert (!bploc->inserted);
c906108c 3493
35df4500 3494 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 3495 {
6c95b8df
PA
3496 if (b->pspace != current_program_space)
3497 continue;
3498
4a64f543 3499 /* Solib breakpoints must be explicitly reset after an exec(). */
c5aa993b
JM
3500 if (b->type == bp_shlib_event)
3501 {
3502 delete_breakpoint (b);
3503 continue;
3504 }
c906108c 3505
4a64f543 3506 /* JIT breakpoints must be explicitly reset after an exec(). */
4efc6507
DE
3507 if (b->type == bp_jit_event)
3508 {
3509 delete_breakpoint (b);
3510 continue;
3511 }
3512
1900040c 3513 /* Thread event breakpoints must be set anew after an exec(),
0fd8e87f
UW
3514 as must overlay event and longjmp master breakpoints. */
3515 if (b->type == bp_thread_event || b->type == bp_overlay_event
186c406b
TT
3516 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
3517 || b->type == bp_exception_master)
c4093a6a
JM
3518 {
3519 delete_breakpoint (b);
3520 continue;
3521 }
3522
4a64f543 3523 /* Step-resume breakpoints are meaningless after an exec(). */
2c03e5be 3524 if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
c5aa993b
JM
3525 {
3526 delete_breakpoint (b);
3527 continue;
3528 }
3529
7c16b83e
PA
3530 /* Just like single-step breakpoints. */
3531 if (b->type == bp_single_step)
3532 {
3533 delete_breakpoint (b);
3534 continue;
3535 }
3536
611c83ae
PA
3537 /* Longjmp and longjmp-resume breakpoints are also meaningless
3538 after an exec. */
186c406b 3539 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
e2e4d78b 3540 || b->type == bp_longjmp_call_dummy
186c406b 3541 || b->type == bp_exception || b->type == bp_exception_resume)
611c83ae
PA
3542 {
3543 delete_breakpoint (b);
3544 continue;
3545 }
3546
ce78b96d
JB
3547 if (b->type == bp_catchpoint)
3548 {
3549 /* For now, none of the bp_catchpoint breakpoints need to
3550 do anything at this point. In the future, if some of
3551 the catchpoints need to something, we will need to add
3552 a new method, and call this method from here. */
3553 continue;
3554 }
3555
c5aa993b
JM
3556 /* bp_finish is a special case. The only way we ought to be able
3557 to see one of these when an exec() has happened, is if the user
3558 caught a vfork, and then said "finish". Ordinarily a finish just
3559 carries them to the call-site of the current callee, by setting
3560 a temporary bp there and resuming. But in this case, the finish
3561 will carry them entirely through the vfork & exec.
3562
3563 We don't want to allow a bp_finish to remain inserted now. But
3564 we can't safely delete it, 'cause finish_command has a handle to
3565 the bp on a bpstat, and will later want to delete it. There's a
3566 chance (and I've seen it happen) that if we delete the bp_finish
3567 here, that its storage will get reused by the time finish_command
3568 gets 'round to deleting the "use to be a bp_finish" breakpoint.
3569 We really must allow finish_command to delete a bp_finish.
3570
e5dd4106 3571 In the absence of a general solution for the "how do we know
53a5351d
JM
3572 it's safe to delete something others may have handles to?"
3573 problem, what we'll do here is just uninsert the bp_finish, and
3574 let finish_command delete it.
3575
3576 (We know the bp_finish is "doomed" in the sense that it's
3577 momentary, and will be deleted as soon as finish_command sees
3578 the inferior stopped. So it doesn't matter that the bp's
3579 address is probably bogus in the new a.out, unlike e.g., the
3580 solib breakpoints.) */
c5aa993b 3581
c5aa993b
JM
3582 if (b->type == bp_finish)
3583 {
3584 continue;
3585 }
3586
3587 /* Without a symbolic address, we have little hope of the
3588 pre-exec() address meaning the same thing in the post-exec()
4a64f543 3589 a.out. */
9ef9e6a6 3590 if (breakpoint_event_location_empty_p (b))
c5aa993b
JM
3591 {
3592 delete_breakpoint (b);
3593 continue;
3594 }
c5aa993b 3595 }
c906108c
SS
3596}
3597
3598int
d80ee84f 3599detach_breakpoints (ptid_t ptid)
c906108c 3600{
35df4500 3601 struct bp_location *bl, **blp_tmp;
3a1bae8e 3602 int val = 0;
2989a365 3603 scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid);
6c95b8df 3604 struct inferior *inf = current_inferior ();
c5aa993b 3605
e99b03dc 3606 if (ptid.pid () == inferior_ptid.pid ())
8a3fe4f8 3607 error (_("Cannot detach breakpoints of inferior_ptid"));
c5aa993b 3608
6c95b8df 3609 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
d80ee84f 3610 inferior_ptid = ptid;
35df4500 3611 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3612 {
35df4500 3613 if (bl->pspace != inf->pspace)
6c95b8df
PA
3614 continue;
3615
bd9673a4
PW
3616 /* This function must physically remove breakpoints locations
3617 from the specified ptid, without modifying the breakpoint
3618 package's state. Locations of type bp_loc_other are only
3619 maintained at GDB side. So, there is no need to remove
3620 these bp_loc_other locations. Moreover, removing these
3621 would modify the breakpoint package's state. */
3622 if (bl->loc_type == bp_loc_other)
3623 continue;
3624
35df4500 3625 if (bl->inserted)
b2b6a7da 3626 val |= remove_breakpoint_1 (bl, DETACH_BREAKPOINT);
c5aa993b 3627 }
d03285ec 3628
3a1bae8e 3629 return val;
c906108c
SS
3630}
3631
35df4500 3632/* Remove the breakpoint location BL from the current address space.
6c95b8df
PA
3633 Note that this is used to detach breakpoints from a child fork.
3634 When we get here, the child isn't in the inferior list, and neither
3635 do we have objects to represent its address space --- we should
35df4500 3636 *not* look at bl->pspace->aspace here. */
6c95b8df 3637
c906108c 3638static int
b2b6a7da 3639remove_breakpoint_1 (struct bp_location *bl, enum remove_bp_reason reason)
c906108c
SS
3640{
3641 int val;
c5aa993b 3642
35df4500
TJB
3643 /* BL is never in moribund_locations by our callers. */
3644 gdb_assert (bl->owner != NULL);
2bdf28a0 3645
74960c60
VP
3646 /* The type of none suggests that owner is actually deleted.
3647 This should not ever happen. */
35df4500 3648 gdb_assert (bl->owner->type != bp_none);
0bde7532 3649
35df4500
TJB
3650 if (bl->loc_type == bp_loc_software_breakpoint
3651 || bl->loc_type == bp_loc_hardware_breakpoint)
c906108c 3652 {
c02f5703
MS
3653 /* "Normal" instruction breakpoint: either the standard
3654 trap-instruction bp (bp_breakpoint), or a
3655 bp_hardware_breakpoint. */
3656
3657 /* First check to see if we have to handle an overlay. */
3658 if (overlay_debugging == ovly_off
35df4500
TJB
3659 || bl->section == NULL
3660 || !(section_is_overlay (bl->section)))
c02f5703
MS
3661 {
3662 /* No overlay handling: just remove the breakpoint. */
08351840
PA
3663
3664 /* If we're trying to uninsert a memory breakpoint that we
3665 know is set in a dynamic object that is marked
3666 shlib_disabled, then either the dynamic object was
3667 removed with "remove-symbol-file" or with
3668 "nosharedlibrary". In the former case, we don't know
3669 whether another dynamic object might have loaded over the
3670 breakpoint's address -- the user might well let us know
3671 about it next with add-symbol-file (the whole point of
d03de421 3672 add-symbol-file is letting the user manually maintain a
08351840
PA
3673 list of dynamically loaded objects). If we have the
3674 breakpoint's shadow memory, that is, this is a software
3675 breakpoint managed by GDB, check whether the breakpoint
3676 is still inserted in memory, to avoid overwriting wrong
3677 code with stale saved shadow contents. Note that HW
3678 breakpoints don't have shadow memory, as they're
3679 implemented using a mechanism that is not dependent on
3680 being able to modify the target's memory, and as such
3681 they should always be removed. */
3682 if (bl->shlib_disabled
3683 && bl->target_info.shadow_len != 0
3684 && !memory_validate_breakpoint (bl->gdbarch, &bl->target_info))
3685 val = 0;
3686 else
73971819 3687 val = bl->owner->ops->remove_location (bl, reason);
c02f5703 3688 }
c906108c
SS
3689 else
3690 {
4a64f543 3691 /* This breakpoint is in an overlay section.
c02f5703
MS
3692 Did we set a breakpoint at the LMA? */
3693 if (!overlay_events_enabled)
3694 {
3695 /* Yes -- overlay event support is not active, so we
3696 should have set a breakpoint at the LMA. Remove it.
3697 */
c02f5703
MS
3698 /* Ignore any failures: if the LMA is in ROM, we will
3699 have already warned when we failed to insert it. */
35df4500
TJB
3700 if (bl->loc_type == bp_loc_hardware_breakpoint)
3701 target_remove_hw_breakpoint (bl->gdbarch,
3702 &bl->overlay_target_info);
c02f5703 3703 else
35df4500 3704 target_remove_breakpoint (bl->gdbarch,
73971819
PA
3705 &bl->overlay_target_info,
3706 reason);
c02f5703
MS
3707 }
3708 /* Did we set a breakpoint at the VMA?
3709 If so, we will have marked the breakpoint 'inserted'. */
35df4500 3710 if (bl->inserted)
c906108c 3711 {
c02f5703
MS
3712 /* Yes -- remove it. Previously we did not bother to
3713 remove the breakpoint if the section had been
3714 unmapped, but let's not rely on that being safe. We
3715 don't know what the overlay manager might do. */
aa67235e
UW
3716
3717 /* However, we should remove *software* breakpoints only
3718 if the section is still mapped, or else we overwrite
3719 wrong code with the saved shadow contents. */
348d480f
PA
3720 if (bl->loc_type == bp_loc_hardware_breakpoint
3721 || section_is_mapped (bl->section))
73971819 3722 val = bl->owner->ops->remove_location (bl, reason);
aa67235e
UW
3723 else
3724 val = 0;
c906108c 3725 }
c02f5703
MS
3726 else
3727 {
3728 /* No -- not inserted, so no need to remove. No error. */
3729 val = 0;
3730 }
c906108c 3731 }
879d1e6b 3732
08351840
PA
3733 /* In some cases, we might not be able to remove a breakpoint in
3734 a shared library that has already been removed, but we have
3735 not yet processed the shlib unload event. Similarly for an
3736 unloaded add-symbol-file object - the user might not yet have
3737 had the chance to remove-symbol-file it. shlib_disabled will
3738 be set if the library/object has already been removed, but
3739 the breakpoint hasn't been uninserted yet, e.g., after
3740 "nosharedlibrary" or "remove-symbol-file" with breakpoints
3741 always-inserted mode. */
076855f9 3742 if (val
08351840
PA
3743 && (bl->loc_type == bp_loc_software_breakpoint
3744 && (bl->shlib_disabled
3745 || solib_name_from_address (bl->pspace, bl->address)
d03de421
PA
3746 || shared_objfile_contains_address_p (bl->pspace,
3747 bl->address))))
879d1e6b
UW
3748 val = 0;
3749
c906108c
SS
3750 if (val)
3751 return val;
b2b6a7da 3752 bl->inserted = (reason == DETACH_BREAKPOINT);
c906108c 3753 }
35df4500 3754 else if (bl->loc_type == bp_loc_hardware_watchpoint)
c906108c 3755 {
77b06cd7
TJB
3756 gdb_assert (bl->owner->ops != NULL
3757 && bl->owner->ops->remove_location != NULL);
3758
b2b6a7da 3759 bl->inserted = (reason == DETACH_BREAKPOINT);
73971819 3760 bl->owner->ops->remove_location (bl, reason);
2e70b7b9 3761
c906108c 3762 /* Failure to remove any of the hardware watchpoints comes here. */
b2b6a7da 3763 if (reason == REMOVE_BREAKPOINT && bl->inserted)
8a3fe4f8 3764 warning (_("Could not remove hardware watchpoint %d."),
35df4500 3765 bl->owner->number);
c906108c 3766 }
35df4500
TJB
3767 else if (bl->owner->type == bp_catchpoint
3768 && breakpoint_enabled (bl->owner)
3769 && !bl->duplicate)
ce78b96d 3770 {
77b06cd7
TJB
3771 gdb_assert (bl->owner->ops != NULL
3772 && bl->owner->ops->remove_location != NULL);
ce78b96d 3773
73971819 3774 val = bl->owner->ops->remove_location (bl, reason);
ce78b96d
JB
3775 if (val)
3776 return val;
77b06cd7 3777
b2b6a7da 3778 bl->inserted = (reason == DETACH_BREAKPOINT);
ce78b96d 3779 }
c906108c
SS
3780
3781 return 0;
3782}
3783
6c95b8df 3784static int
834c0d03 3785remove_breakpoint (struct bp_location *bl)
6c95b8df 3786{
35df4500
TJB
3787 /* BL is never in moribund_locations by our callers. */
3788 gdb_assert (bl->owner != NULL);
2bdf28a0 3789
6c95b8df
PA
3790 /* The type of none suggests that owner is actually deleted.
3791 This should not ever happen. */
35df4500 3792 gdb_assert (bl->owner->type != bp_none);
6c95b8df 3793
5ed8105e 3794 scoped_restore_current_pspace_and_thread restore_pspace_thread;
6c95b8df 3795
35df4500 3796 switch_to_program_space_and_thread (bl->pspace);
6c95b8df 3797
5ed8105e 3798 return remove_breakpoint_1 (bl, REMOVE_BREAKPOINT);
6c95b8df
PA
3799}
3800
c906108c
SS
3801/* Clear the "inserted" flag in all breakpoints. */
3802
25b22b0a 3803void
fba45db2 3804mark_breakpoints_out (void)
c906108c 3805{
35df4500 3806 struct bp_location *bl, **blp_tmp;
c906108c 3807
35df4500 3808 ALL_BP_LOCATIONS (bl, blp_tmp)
66c4b3e8 3809 if (bl->pspace == current_program_space)
35df4500 3810 bl->inserted = 0;
c906108c
SS
3811}
3812
53a5351d
JM
3813/* Clear the "inserted" flag in all breakpoints and delete any
3814 breakpoints which should go away between runs of the program.
c906108c
SS
3815
3816 Plus other such housekeeping that has to be done for breakpoints
3817 between runs.
3818
53a5351d
JM
3819 Note: this function gets called at the end of a run (by
3820 generic_mourn_inferior) and when a run begins (by
4a64f543 3821 init_wait_for_inferior). */
c906108c
SS
3822
3823
3824
3825void
fba45db2 3826breakpoint_init_inferior (enum inf_context context)
c906108c 3827{
35df4500 3828 struct breakpoint *b, *b_tmp;
6c95b8df 3829 struct program_space *pspace = current_program_space;
c906108c 3830
50c71eaf
PA
3831 /* If breakpoint locations are shared across processes, then there's
3832 nothing to do. */
f5656ead 3833 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
50c71eaf
PA
3834 return;
3835
1a853c52 3836 mark_breakpoints_out ();
075f6582 3837
35df4500 3838 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 3839 {
6c95b8df
PA
3840 if (b->loc && b->loc->pspace != pspace)
3841 continue;
3842
c5aa993b
JM
3843 switch (b->type)
3844 {
3845 case bp_call_dummy:
e2e4d78b 3846 case bp_longjmp_call_dummy:
c906108c 3847
c5aa993b 3848 /* If the call dummy breakpoint is at the entry point it will
ab92d69b
PA
3849 cause problems when the inferior is rerun, so we better get
3850 rid of it. */
3851
3852 case bp_watchpoint_scope:
3853
3854 /* Also get rid of scope breakpoints. */
3855
3856 case bp_shlib_event:
3857
3858 /* Also remove solib event breakpoints. Their addresses may
3859 have changed since the last time we ran the program.
3860 Actually we may now be debugging against different target;
3861 and so the solib backend that installed this breakpoint may
3862 not be used in by the target. E.g.,
3863
3864 (gdb) file prog-linux
3865 (gdb) run # native linux target
3866 ...
3867 (gdb) kill
3868 (gdb) file prog-win.exe
3869 (gdb) tar rem :9999 # remote Windows gdbserver.
3870 */
c906108c 3871
f59f708a
PA
3872 case bp_step_resume:
3873
3874 /* Also remove step-resume breakpoints. */
3875
7c16b83e
PA
3876 case bp_single_step:
3877
3878 /* Also remove single-step breakpoints. */
3879
c5aa993b
JM
3880 delete_breakpoint (b);
3881 break;
c906108c 3882
c5aa993b
JM
3883 case bp_watchpoint:
3884 case bp_hardware_watchpoint:
3885 case bp_read_watchpoint:
3886 case bp_access_watchpoint:
3a5c3e22
PA
3887 {
3888 struct watchpoint *w = (struct watchpoint *) b;
c906108c 3889
3a5c3e22
PA
3890 /* Likewise for watchpoints on local expressions. */
3891 if (w->exp_valid_block != NULL)
3892 delete_breakpoint (b);
63000888 3893 else
3a5c3e22 3894 {
63000888
PA
3895 /* Get rid of existing locations, which are no longer
3896 valid. New ones will be created in
3897 update_watchpoint, when the inferior is restarted.
3898 The next update_global_location_list call will
3899 garbage collect them. */
3900 b->loc = NULL;
3901
3902 if (context == inf_starting)
3903 {
3904 /* Reset val field to force reread of starting value in
3905 insert_breakpoints. */
850645cf 3906 w->val.reset (nullptr);
63000888
PA
3907 w->val_valid = 0;
3908 }
3909 }
3a5c3e22 3910 }
c5aa993b
JM
3911 break;
3912 default:
c5aa993b
JM
3913 break;
3914 }
3915 }
1c5cfe86
PA
3916
3917 /* Get rid of the moribund locations. */
1123588c 3918 for (bp_location *bl : moribund_locations)
35df4500 3919 decref_bp_location (&bl);
1123588c 3920 moribund_locations.clear ();
c906108c
SS
3921}
3922
6c95b8df
PA
3923/* These functions concern about actual breakpoints inserted in the
3924 target --- to e.g. check if we need to do decr_pc adjustment or if
3925 we need to hop over the bkpt --- so we check for address space
3926 match, not program space. */
3927
c2c6d25f
JM
3928/* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
3929 exists at PC. It returns ordinary_breakpoint_here if it's an
3930 ordinary breakpoint, or permanent_breakpoint_here if it's a
3931 permanent breakpoint.
3932 - When continuing from a location with an ordinary breakpoint, we
3933 actually single step once before calling insert_breakpoints.
e5dd4106 3934 - When continuing from a location with a permanent breakpoint, we
c2c6d25f
JM
3935 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
3936 the target, to advance the PC past the breakpoint. */
c906108c 3937
c2c6d25f 3938enum breakpoint_here
accd0bcd 3939breakpoint_here_p (const address_space *aspace, CORE_ADDR pc)
c906108c 3940{
35df4500 3941 struct bp_location *bl, **blp_tmp;
c2c6d25f 3942 int any_breakpoint_here = 0;
c906108c 3943
35df4500 3944 ALL_BP_LOCATIONS (bl, blp_tmp)
075f6582 3945 {
35df4500
TJB
3946 if (bl->loc_type != bp_loc_software_breakpoint
3947 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
3948 continue;
3949
f1310107 3950 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
35df4500 3951 if ((breakpoint_enabled (bl->owner)
1a853c52 3952 || bl->permanent)
f1310107 3953 && breakpoint_location_address_match (bl, aspace, pc))
075f6582
DJ
3954 {
3955 if (overlay_debugging
35df4500
TJB
3956 && section_is_overlay (bl->section)
3957 && !section_is_mapped (bl->section))
075f6582 3958 continue; /* unmapped overlay -- can't be a match */
1a853c52 3959 else if (bl->permanent)
075f6582
DJ
3960 return permanent_breakpoint_here;
3961 else
3962 any_breakpoint_here = 1;
3963 }
3964 }
c906108c 3965
f486487f 3966 return any_breakpoint_here ? ordinary_breakpoint_here : no_breakpoint_here;
c906108c
SS
3967}
3968
d35ae833
PA
3969/* See breakpoint.h. */
3970
3971int
accd0bcd 3972breakpoint_in_range_p (const address_space *aspace,
d35ae833
PA
3973 CORE_ADDR addr, ULONGEST len)
3974{
3975 struct bp_location *bl, **blp_tmp;
3976
3977 ALL_BP_LOCATIONS (bl, blp_tmp)
3978 {
3979 if (bl->loc_type != bp_loc_software_breakpoint
3980 && bl->loc_type != bp_loc_hardware_breakpoint)
3981 continue;
3982
3983 if ((breakpoint_enabled (bl->owner)
3984 || bl->permanent)
3985 && breakpoint_location_address_range_overlap (bl, aspace,
3986 addr, len))
3987 {
3988 if (overlay_debugging
3989 && section_is_overlay (bl->section)
3990 && !section_is_mapped (bl->section))
3991 {
3992 /* Unmapped overlay -- can't be a match. */
3993 continue;
3994 }
3995
3996 return 1;
3997 }
3998 }
3999
4000 return 0;
4001}
4002
1c5cfe86
PA
4003/* Return true if there's a moribund breakpoint at PC. */
4004
4005int
accd0bcd 4006moribund_breakpoint_here_p (const address_space *aspace, CORE_ADDR pc)
1c5cfe86 4007{
1123588c 4008 for (bp_location *loc : moribund_locations)
f1310107 4009 if (breakpoint_location_address_match (loc, aspace, pc))
1c5cfe86
PA
4010 return 1;
4011
4012 return 0;
4013}
c2c6d25f 4014
f7ce857f
PA
4015/* Returns non-zero iff BL is inserted at PC, in address space
4016 ASPACE. */
4017
4018static int
4019bp_location_inserted_here_p (struct bp_location *bl,
accd0bcd 4020 const address_space *aspace, CORE_ADDR pc)
f7ce857f
PA
4021{
4022 if (bl->inserted
4023 && breakpoint_address_match (bl->pspace->aspace, bl->address,
4024 aspace, pc))
4025 {
4026 if (overlay_debugging
4027 && section_is_overlay (bl->section)
4028 && !section_is_mapped (bl->section))
4029 return 0; /* unmapped overlay -- can't be a match */
4030 else
4031 return 1;
4032 }
4033 return 0;
4034}
4035
a1fd2fa5 4036/* Returns non-zero iff there's a breakpoint inserted at PC. */
c906108c
SS
4037
4038int
accd0bcd 4039breakpoint_inserted_here_p (const address_space *aspace, CORE_ADDR pc)
c906108c 4040{
f7ce857f 4041 struct bp_location **blp, **blp_tmp = NULL;
c906108c 4042
f7ce857f 4043 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
c5aa993b 4044 {
f7ce857f
PA
4045 struct bp_location *bl = *blp;
4046
35df4500
TJB
4047 if (bl->loc_type != bp_loc_software_breakpoint
4048 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
4049 continue;
4050
f7ce857f
PA
4051 if (bp_location_inserted_here_p (bl, aspace, pc))
4052 return 1;
c5aa993b 4053 }
c36b740a
VP
4054 return 0;
4055}
4056
a1fd2fa5
PA
4057/* This function returns non-zero iff there is a software breakpoint
4058 inserted at PC. */
c36b740a
VP
4059
4060int
accd0bcd 4061software_breakpoint_inserted_here_p (const address_space *aspace,
a1fd2fa5 4062 CORE_ADDR pc)
4fa8626c 4063{
f7ce857f 4064 struct bp_location **blp, **blp_tmp = NULL;
4fa8626c 4065
f7ce857f 4066 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4fa8626c 4067 {
f7ce857f
PA
4068 struct bp_location *bl = *blp;
4069
35df4500 4070 if (bl->loc_type != bp_loc_software_breakpoint)
4fa8626c
DJ
4071 continue;
4072
f7ce857f
PA
4073 if (bp_location_inserted_here_p (bl, aspace, pc))
4074 return 1;
4fa8626c
DJ
4075 }
4076
4077 return 0;
9c02b525
PA
4078}
4079
4080/* See breakpoint.h. */
4081
4082int
accd0bcd 4083hardware_breakpoint_inserted_here_p (const address_space *aspace,
9c02b525
PA
4084 CORE_ADDR pc)
4085{
4086 struct bp_location **blp, **blp_tmp = NULL;
9c02b525
PA
4087
4088 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4089 {
4090 struct bp_location *bl = *blp;
4091
4092 if (bl->loc_type != bp_loc_hardware_breakpoint)
4093 continue;
4094
4095 if (bp_location_inserted_here_p (bl, aspace, pc))
4096 return 1;
4097 }
4098
4099 return 0;
4fa8626c
DJ
4100}
4101
9093389c 4102int
accd0bcd 4103hardware_watchpoint_inserted_in_range (const address_space *aspace,
9093389c
PA
4104 CORE_ADDR addr, ULONGEST len)
4105{
4106 struct breakpoint *bpt;
4107
4108 ALL_BREAKPOINTS (bpt)
4109 {
4110 struct bp_location *loc;
4111
4112 if (bpt->type != bp_hardware_watchpoint
4113 && bpt->type != bp_access_watchpoint)
4114 continue;
4115
4116 if (!breakpoint_enabled (bpt))
4117 continue;
4118
4119 for (loc = bpt->loc; loc; loc = loc->next)
4120 if (loc->pspace->aspace == aspace && loc->inserted)
4121 {
4122 CORE_ADDR l, h;
4123
4124 /* Check for intersection. */
768adc05
PA
4125 l = std::max<CORE_ADDR> (loc->address, addr);
4126 h = std::min<CORE_ADDR> (loc->address + loc->length, addr + len);
9093389c
PA
4127 if (l < h)
4128 return 1;
4129 }
4130 }
4131 return 0;
4132}
c906108c 4133\f
c5aa993b 4134
c906108c
SS
4135/* bpstat stuff. External routines' interfaces are documented
4136 in breakpoint.h. */
4137
4138int
c326b90e 4139is_catchpoint (struct breakpoint *ep)
c906108c 4140{
533be4dd 4141 return (ep->type == bp_catchpoint);
c906108c
SS
4142}
4143
f431efe5
PA
4144/* Frees any storage that is part of a bpstat. Does not walk the
4145 'next' chain. */
4146
04afa70c 4147bpstats::~bpstats ()
198757a8 4148{
04afa70c
TT
4149 if (bp_location_at != NULL)
4150 decref_bp_location (&bp_location_at);
198757a8
VP
4151}
4152
c906108c
SS
4153/* Clear a bpstat so that it says we are not at any breakpoint.
4154 Also free any storage that is part of a bpstat. */
4155
4156void
fba45db2 4157bpstat_clear (bpstat *bsp)
c906108c
SS
4158{
4159 bpstat p;
4160 bpstat q;
4161
4162 if (bsp == 0)
4163 return;
4164 p = *bsp;
4165 while (p != NULL)
4166 {
4167 q = p->next;
04afa70c 4168 delete p;
c906108c
SS
4169 p = q;
4170 }
4171 *bsp = NULL;
4172}
4173
04afa70c
TT
4174bpstats::bpstats (const bpstats &other)
4175 : next (NULL),
4176 bp_location_at (other.bp_location_at),
4177 breakpoint_at (other.breakpoint_at),
4178 commands (other.commands),
04afa70c
TT
4179 print (other.print),
4180 stop (other.stop),
4181 print_it (other.print_it)
4182{
850645cf
TT
4183 if (other.old_val != NULL)
4184 old_val = release_value (value_copy (other.old_val.get ()));
04afa70c 4185 incref_bp_location (bp_location_at);
04afa70c
TT
4186}
4187
c906108c
SS
4188/* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
4189 is part of the bpstat is copied as well. */
4190
4191bpstat
fba45db2 4192bpstat_copy (bpstat bs)
c906108c
SS
4193{
4194 bpstat p = NULL;
4195 bpstat tmp;
4196 bpstat retval = NULL;
4197
4198 if (bs == NULL)
4199 return bs;
4200
4201 for (; bs != NULL; bs = bs->next)
4202 {
04afa70c 4203 tmp = new bpstats (*bs);
31cc81e9 4204
c906108c
SS
4205 if (p == NULL)
4206 /* This is the first thing in the chain. */
4207 retval = tmp;
4208 else
4209 p->next = tmp;
4210 p = tmp;
4211 }
4212 p->next = NULL;
4213 return retval;
4214}
4215
4a64f543 4216/* Find the bpstat associated with this breakpoint. */
c906108c
SS
4217
4218bpstat
fba45db2 4219bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
c906108c 4220{
c5aa993b
JM
4221 if (bsp == NULL)
4222 return NULL;
c906108c 4223
c5aa993b
JM
4224 for (; bsp != NULL; bsp = bsp->next)
4225 {
f431efe5 4226 if (bsp->breakpoint_at == breakpoint)
c5aa993b
JM
4227 return bsp;
4228 }
c906108c
SS
4229 return NULL;
4230}
4231
ab04a2af
TT
4232/* See breakpoint.h. */
4233
47591c29 4234int
427cd150 4235bpstat_explains_signal (bpstat bsp, enum gdb_signal sig)
ab04a2af 4236{
ab04a2af
TT
4237 for (; bsp != NULL; bsp = bsp->next)
4238 {
427cd150
TT
4239 if (bsp->breakpoint_at == NULL)
4240 {
4241 /* A moribund location can never explain a signal other than
4242 GDB_SIGNAL_TRAP. */
4243 if (sig == GDB_SIGNAL_TRAP)
47591c29 4244 return 1;
427cd150
TT
4245 }
4246 else
47591c29
PA
4247 {
4248 if (bsp->breakpoint_at->ops->explains_signal (bsp->breakpoint_at,
4249 sig))
4250 return 1;
4251 }
ab04a2af
TT
4252 }
4253
47591c29 4254 return 0;
ab04a2af
TT
4255}
4256
4a64f543
MS
4257/* Put in *NUM the breakpoint number of the first breakpoint we are
4258 stopped at. *BSP upon return is a bpstat which points to the
4259 remaining breakpoints stopped at (but which is not guaranteed to be
4260 good for anything but further calls to bpstat_num).
4261
8671a17b
PA
4262 Return 0 if passed a bpstat which does not indicate any breakpoints.
4263 Return -1 if stopped at a breakpoint that has been deleted since
4264 we set it.
4265 Return 1 otherwise. */
c906108c
SS
4266
4267int
8671a17b 4268bpstat_num (bpstat *bsp, int *num)
c906108c
SS
4269{
4270 struct breakpoint *b;
4271
4272 if ((*bsp) == NULL)
4273 return 0; /* No more breakpoint values */
8671a17b 4274
4a64f543
MS
4275 /* We assume we'll never have several bpstats that correspond to a
4276 single breakpoint -- otherwise, this function might return the
4277 same number more than once and this will look ugly. */
f431efe5 4278 b = (*bsp)->breakpoint_at;
8671a17b
PA
4279 *bsp = (*bsp)->next;
4280 if (b == NULL)
4281 return -1; /* breakpoint that's been deleted since */
4282
4283 *num = b->number; /* We have its number */
4284 return 1;
c906108c
SS
4285}
4286
e93ca019 4287/* See breakpoint.h. */
c906108c
SS
4288
4289void
e93ca019 4290bpstat_clear_actions (void)
c906108c 4291{
e93ca019
JK
4292 bpstat bs;
4293
00431a78 4294 if (inferior_ptid == null_ptid)
e93ca019
JK
4295 return;
4296
00431a78 4297 thread_info *tp = inferior_thread ();
e93ca019 4298 for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
c906108c 4299 {
d1b0a7bf 4300 bs->commands = NULL;
850645cf 4301 bs->old_val.reset (nullptr);
c906108c
SS
4302 }
4303}
4304
f3b1572e
PA
4305/* Called when a command is about to proceed the inferior. */
4306
4307static void
4308breakpoint_about_to_proceed (void)
4309{
d7e15655 4310 if (inferior_ptid != null_ptid)
f3b1572e
PA
4311 {
4312 struct thread_info *tp = inferior_thread ();
4313
4314 /* Allow inferior function calls in breakpoint commands to not
4315 interrupt the command list. When the call finishes
4316 successfully, the inferior will be standing at the same
4317 breakpoint as if nothing happened. */
16c381f0 4318 if (tp->control.in_infcall)
f3b1572e
PA
4319 return;
4320 }
4321
4322 breakpoint_proceeded = 1;
4323}
4324
abf85f46
JK
4325/* Return non-zero iff CMD as the first line of a command sequence is `silent'
4326 or its equivalent. */
4327
4328static int
4329command_line_is_silent (struct command_line *cmd)
4330{
4f45d445 4331 return cmd && (strcmp ("silent", cmd->line) == 0);
abf85f46
JK
4332}
4333
4a64f543
MS
4334/* Execute all the commands associated with all the breakpoints at
4335 this location. Any of these commands could cause the process to
4336 proceed beyond this point, etc. We look out for such changes by
4337 checking the global "breakpoint_proceeded" after each command.
c906108c 4338
347bddb7
PA
4339 Returns true if a breakpoint command resumed the inferior. In that
4340 case, it is the caller's responsibility to recall it again with the
4341 bpstat of the current thread. */
4342
4343static int
4344bpstat_do_actions_1 (bpstat *bsp)
c906108c
SS
4345{
4346 bpstat bs;
347bddb7 4347 int again = 0;
c906108c
SS
4348
4349 /* Avoid endless recursion if a `source' command is contained
4350 in bs->commands. */
4351 if (executing_breakpoint_commands)
347bddb7 4352 return 0;
c906108c 4353
81b1e71c
TT
4354 scoped_restore save_executing
4355 = make_scoped_restore (&executing_breakpoint_commands, 1);
c906108c 4356
1ac32117 4357 scoped_restore preventer = prevent_dont_repeat ();
cf6c5ffb 4358
4a64f543 4359 /* This pointer will iterate over the list of bpstat's. */
c906108c
SS
4360 bs = *bsp;
4361
4362 breakpoint_proceeded = 0;
4363 for (; bs != NULL; bs = bs->next)
4364 {
d1b0a7bf 4365 struct command_line *cmd = NULL;
6c50ab1c
JB
4366
4367 /* Take ownership of the BSP's command tree, if it has one.
4368
4369 The command tree could legitimately contain commands like
4370 'step' and 'next', which call clear_proceed_status, which
4371 frees stop_bpstat's command tree. To make sure this doesn't
4372 free the tree we're executing out from under us, we need to
4373 take ownership of the tree ourselves. Since a given bpstat's
4374 commands are only executed once, we don't need to copy it; we
4375 can clear the pointer in the bpstat, and make sure we free
4376 the tree when we're done. */
d1b0a7bf 4377 counted_command_line ccmd = bs->commands;
9add0f1b 4378 bs->commands = NULL;
d1b0a7bf
TT
4379 if (ccmd != NULL)
4380 cmd = ccmd.get ();
abf85f46
JK
4381 if (command_line_is_silent (cmd))
4382 {
4383 /* The action has been already done by bpstat_stop_status. */
4384 cmd = cmd->next;
4385 }
6c50ab1c 4386
c906108c
SS
4387 while (cmd != NULL)
4388 {
4389 execute_control_command (cmd);
4390
4391 if (breakpoint_proceeded)
4392 break;
4393 else
4394 cmd = cmd->next;
4395 }
6c50ab1c 4396
c906108c 4397 if (breakpoint_proceeded)
32c1e744 4398 {
cb814510 4399 if (current_ui->async)
347bddb7
PA
4400 /* If we are in async mode, then the target might be still
4401 running, not stopped at any breakpoint, so nothing for
4402 us to do here -- just return to the event loop. */
4403 ;
32c1e744
VP
4404 else
4405 /* In sync mode, when execute_control_command returns
4406 we're already standing on the next breakpoint.
347bddb7
PA
4407 Breakpoint commands for that stop were not run, since
4408 execute_command does not run breakpoint commands --
4409 only command_line_handler does, but that one is not
4410 involved in execution of breakpoint commands. So, we
4411 can now execute breakpoint commands. It should be
4412 noted that making execute_command do bpstat actions is
4413 not an option -- in this case we'll have recursive
4414 invocation of bpstat for each breakpoint with a
4415 command, and can easily blow up GDB stack. Instead, we
4416 return true, which will trigger the caller to recall us
4417 with the new stop_bpstat. */
4418 again = 1;
4419 break;
32c1e744 4420 }
c906108c 4421 }
347bddb7
PA
4422 return again;
4423}
4424
00431a78
PA
4425/* Helper for bpstat_do_actions. Get the current thread, if there's
4426 one, is alive and has execution. Return NULL otherwise. */
4427
4428static thread_info *
4429get_bpstat_thread ()
4430{
4431 if (inferior_ptid == null_ptid || !target_has_execution)
4432 return NULL;
4433
4434 thread_info *tp = inferior_thread ();
4435 if (tp->state == THREAD_EXITED || tp->executing)
4436 return NULL;
4437 return tp;
4438}
4439
347bddb7
PA
4440void
4441bpstat_do_actions (void)
4442{
694c6bf5 4443 auto cleanup_if_error = make_scope_exit (bpstat_clear_actions);
00431a78 4444 thread_info *tp;
353d1d73 4445
347bddb7 4446 /* Do any commands attached to breakpoint we are stopped at. */
00431a78
PA
4447 while ((tp = get_bpstat_thread ()) != NULL)
4448 {
4449 /* Since in sync mode, bpstat_do_actions may resume the
4450 inferior, and only return when it is stopped at the next
4451 breakpoint, we keep doing breakpoint actions until it returns
4452 false to indicate the inferior was not resumed. */
4453 if (!bpstat_do_actions_1 (&tp->control.stop_bpstat))
4454 break;
4455 }
353d1d73 4456
694c6bf5 4457 cleanup_if_error.release ();
c906108c
SS
4458}
4459
fa4727a6
DJ
4460/* Print out the (old or new) value associated with a watchpoint. */
4461
4462static void
4463watchpoint_value_print (struct value *val, struct ui_file *stream)
4464{
4465 if (val == NULL)
4466 fprintf_unfiltered (stream, _("<unreadable>"));
4467 else
79a45b7d
TT
4468 {
4469 struct value_print_options opts;
4470 get_user_print_options (&opts);
4471 value_print (val, stream, &opts);
4472 }
fa4727a6
DJ
4473}
4474
f303dbd6
PA
4475/* Print the "Thread ID hit" part of "Thread ID hit Breakpoint N" if
4476 debugging multiple threads. */
4477
4478void
4479maybe_print_thread_hit_breakpoint (struct ui_out *uiout)
4480{
112e8700 4481 if (uiout->is_mi_like_p ())
f303dbd6
PA
4482 return;
4483
112e8700 4484 uiout->text ("\n");
f303dbd6
PA
4485
4486 if (show_thread_that_caused_stop ())
4487 {
4488 const char *name;
4489 struct thread_info *thr = inferior_thread ();
4490
112e8700
SM
4491 uiout->text ("Thread ");
4492 uiout->field_fmt ("thread-id", "%s", print_thread_id (thr));
f303dbd6
PA
4493
4494 name = thr->name != NULL ? thr->name : target_thread_name (thr);
4495 if (name != NULL)
4496 {
112e8700
SM
4497 uiout->text (" \"");
4498 uiout->field_fmt ("name", "%s", name);
4499 uiout->text ("\"");
f303dbd6
PA
4500 }
4501
112e8700 4502 uiout->text (" hit ");
f303dbd6
PA
4503 }
4504}
4505
e514a9d6 4506/* Generic routine for printing messages indicating why we
4a64f543 4507 stopped. The behavior of this function depends on the value
e514a9d6
JM
4508 'print_it' in the bpstat structure. Under some circumstances we
4509 may decide not to print anything here and delegate the task to
4a64f543 4510 normal_stop(). */
e514a9d6
JM
4511
4512static enum print_stop_action
4513print_bp_stop_message (bpstat bs)
4514{
4515 switch (bs->print_it)
4516 {
4517 case print_it_noop:
4a64f543 4518 /* Nothing should be printed for this bpstat entry. */
e514a9d6
JM
4519 return PRINT_UNKNOWN;
4520 break;
4521
4522 case print_it_done:
4523 /* We still want to print the frame, but we already printed the
4a64f543 4524 relevant messages. */
e514a9d6
JM
4525 return PRINT_SRC_AND_LOC;
4526 break;
4527
4528 case print_it_normal:
4f8d1dc6 4529 {
f431efe5
PA
4530 struct breakpoint *b = bs->breakpoint_at;
4531
1a6a67de
TJB
4532 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
4533 which has since been deleted. */
4534 if (b == NULL)
4535 return PRINT_UNKNOWN;
4536
348d480f
PA
4537 /* Normal case. Call the breakpoint's print_it method. */
4538 return b->ops->print_it (bs);
4f8d1dc6 4539 }
348d480f 4540 break;
3086aeae 4541
e514a9d6 4542 default:
8e65ff28 4543 internal_error (__FILE__, __LINE__,
e2e0b3e5 4544 _("print_bp_stop_message: unrecognized enum value"));
e514a9d6 4545 break;
c906108c 4546 }
c906108c
SS
4547}
4548
edcc5120
TT
4549/* A helper function that prints a shared library stopped event. */
4550
4551static void
4552print_solib_event (int is_catchpoint)
4553{
6fb16ce6 4554 bool any_deleted = !current_program_space->deleted_solibs.empty ();
bcb430e4 4555 bool any_added = !current_program_space->added_solibs.empty ();
edcc5120
TT
4556
4557 if (!is_catchpoint)
4558 {
4559 if (any_added || any_deleted)
112e8700 4560 current_uiout->text (_("Stopped due to shared library event:\n"));
edcc5120 4561 else
112e8700
SM
4562 current_uiout->text (_("Stopped due to shared library event (no "
4563 "libraries added or removed)\n"));
edcc5120
TT
4564 }
4565
112e8700
SM
4566 if (current_uiout->is_mi_like_p ())
4567 current_uiout->field_string ("reason",
4568 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
edcc5120
TT
4569
4570 if (any_deleted)
4571 {
112e8700 4572 current_uiout->text (_(" Inferior unloaded "));
10f489e5 4573 ui_out_emit_list list_emitter (current_uiout, "removed");
6fb16ce6 4574 for (int ix = 0; ix < current_program_space->deleted_solibs.size (); ix++)
edcc5120 4575 {
6fb16ce6
SM
4576 const std::string &name = current_program_space->deleted_solibs[ix];
4577
edcc5120 4578 if (ix > 0)
112e8700
SM
4579 current_uiout->text (" ");
4580 current_uiout->field_string ("library", name);
4581 current_uiout->text ("\n");
edcc5120 4582 }
edcc5120
TT
4583 }
4584
4585 if (any_added)
4586 {
112e8700 4587 current_uiout->text (_(" Inferior loaded "));
10f489e5 4588 ui_out_emit_list list_emitter (current_uiout, "added");
bcb430e4 4589 bool first = true;
52941706 4590 for (so_list *iter : current_program_space->added_solibs)
edcc5120 4591 {
bcb430e4 4592 if (!first)
112e8700 4593 current_uiout->text (" ");
bcb430e4 4594 first = false;
112e8700
SM
4595 current_uiout->field_string ("library", iter->so_name);
4596 current_uiout->text ("\n");
edcc5120 4597 }
edcc5120
TT
4598 }
4599}
4600
e514a9d6
JM
4601/* Print a message indicating what happened. This is called from
4602 normal_stop(). The input to this routine is the head of the bpstat
36dfb11c
TT
4603 list - a list of the eventpoints that caused this stop. KIND is
4604 the target_waitkind for the stopping event. This
e514a9d6
JM
4605 routine calls the generic print routine for printing a message
4606 about reasons for stopping. This will print (for example) the
4607 "Breakpoint n," part of the output. The return value of this
4608 routine is one of:
c906108c 4609
4a64f543 4610 PRINT_UNKNOWN: Means we printed nothing.
917317f4 4611 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4a64f543 4612 code to print the location. An example is
c5aa993b
JM
4613 "Breakpoint 1, " which should be followed by
4614 the location.
917317f4 4615 PRINT_SRC_ONLY: Means we printed something, but there is no need
c5aa993b
JM
4616 to also print the location part of the message.
4617 An example is the catch/throw messages, which
4a64f543 4618 don't require a location appended to the end.
917317f4 4619 PRINT_NOTHING: We have done some printing and we don't need any
4a64f543 4620 further info to be printed. */
c906108c 4621
917317f4 4622enum print_stop_action
36dfb11c 4623bpstat_print (bpstat bs, int kind)
c906108c 4624{
f486487f 4625 enum print_stop_action val;
c5aa993b 4626
c906108c 4627 /* Maybe another breakpoint in the chain caused us to stop.
53a5351d
JM
4628 (Currently all watchpoints go on the bpstat whether hit or not.
4629 That probably could (should) be changed, provided care is taken
c906108c 4630 with respect to bpstat_explains_signal). */
e514a9d6
JM
4631 for (; bs; bs = bs->next)
4632 {
4633 val = print_bp_stop_message (bs);
4634 if (val == PRINT_SRC_ONLY
4635 || val == PRINT_SRC_AND_LOC
4636 || val == PRINT_NOTHING)
4637 return val;
4638 }
c906108c 4639
36dfb11c
TT
4640 /* If we had hit a shared library event breakpoint,
4641 print_bp_stop_message would print out this message. If we hit an
4642 OS-level shared library event, do the same thing. */
4643 if (kind == TARGET_WAITKIND_LOADED)
4644 {
edcc5120 4645 print_solib_event (0);
36dfb11c
TT
4646 return PRINT_NOTHING;
4647 }
4648
e514a9d6 4649 /* We reached the end of the chain, or we got a null BS to start
4a64f543 4650 with and nothing was printed. */
917317f4 4651 return PRINT_UNKNOWN;
c906108c
SS
4652}
4653
bf469271 4654/* Evaluate the boolean expression EXP and return the result. */
c906108c 4655
bf469271
PA
4656static bool
4657breakpoint_cond_eval (expression *exp)
c906108c 4658{
278cd55f 4659 struct value *mark = value_mark ();
bf469271 4660 bool res = value_true (evaluate_expression (exp));
cc59ec59 4661
c906108c 4662 value_free_to_mark (mark);
bf469271 4663 return res;
c906108c
SS
4664}
4665
5760d0ab 4666/* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
c906108c 4667
04afa70c
TT
4668bpstats::bpstats (struct bp_location *bl, bpstat **bs_link_pointer)
4669 : next (NULL),
4670 bp_location_at (bl),
4671 breakpoint_at (bl->owner),
4672 commands (NULL),
04afa70c
TT
4673 print (0),
4674 stop (0),
4675 print_it (print_it_normal)
c906108c 4676{
f431efe5 4677 incref_bp_location (bl);
04afa70c
TT
4678 **bs_link_pointer = this;
4679 *bs_link_pointer = &next;
4680}
4681
4682bpstats::bpstats ()
4683 : next (NULL),
4684 bp_location_at (NULL),
4685 breakpoint_at (NULL),
4686 commands (NULL),
04afa70c
TT
4687 print (0),
4688 stop (0),
4689 print_it (print_it_normal)
4690{
c906108c
SS
4691}
4692\f
d983da9c
DJ
4693/* The target has stopped with waitstatus WS. Check if any hardware
4694 watchpoints have triggered, according to the target. */
4695
4696int
4697watchpoints_triggered (struct target_waitstatus *ws)
4698{
57810aa7 4699 bool stopped_by_watchpoint = target_stopped_by_watchpoint ();
d983da9c
DJ
4700 CORE_ADDR addr;
4701 struct breakpoint *b;
4702
4703 if (!stopped_by_watchpoint)
4704 {
4705 /* We were not stopped by a watchpoint. Mark all watchpoints
4706 as not triggered. */
4707 ALL_BREAKPOINTS (b)
cc60f2e3 4708 if (is_hardware_watchpoint (b))
3a5c3e22
PA
4709 {
4710 struct watchpoint *w = (struct watchpoint *) b;
4711
4712 w->watchpoint_triggered = watch_triggered_no;
4713 }
d983da9c
DJ
4714
4715 return 0;
4716 }
4717
8b88a78e 4718 if (!target_stopped_data_address (current_top_target (), &addr))
d983da9c
DJ
4719 {
4720 /* We were stopped by a watchpoint, but we don't know where.
4721 Mark all watchpoints as unknown. */
4722 ALL_BREAKPOINTS (b)
cc60f2e3 4723 if (is_hardware_watchpoint (b))
3a5c3e22
PA
4724 {
4725 struct watchpoint *w = (struct watchpoint *) b;
4726
4727 w->watchpoint_triggered = watch_triggered_unknown;
4728 }
d983da9c 4729
3c4797ba 4730 return 1;
d983da9c
DJ
4731 }
4732
4733 /* The target could report the data address. Mark watchpoints
4734 affected by this data address as triggered, and all others as not
4735 triggered. */
4736
4737 ALL_BREAKPOINTS (b)
cc60f2e3 4738 if (is_hardware_watchpoint (b))
d983da9c 4739 {
3a5c3e22 4740 struct watchpoint *w = (struct watchpoint *) b;
a5606eee 4741 struct bp_location *loc;
d983da9c 4742
3a5c3e22 4743 w->watchpoint_triggered = watch_triggered_no;
a5606eee 4744 for (loc = b->loc; loc; loc = loc->next)
9c06b0b4 4745 {
3a5c3e22 4746 if (is_masked_watchpoint (b))
9c06b0b4 4747 {
3a5c3e22
PA
4748 CORE_ADDR newaddr = addr & w->hw_wp_mask;
4749 CORE_ADDR start = loc->address & w->hw_wp_mask;
9c06b0b4
TJB
4750
4751 if (newaddr == start)
4752 {
3a5c3e22 4753 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
4754 break;
4755 }
4756 }
4757 /* Exact match not required. Within range is sufficient. */
8b88a78e 4758 else if (target_watchpoint_addr_within_range (current_top_target (),
9c06b0b4
TJB
4759 addr, loc->address,
4760 loc->length))
4761 {
3a5c3e22 4762 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
4763 break;
4764 }
4765 }
d983da9c
DJ
4766 }
4767
4768 return 1;
4769}
4770
bf469271
PA
4771/* Possible return values for watchpoint_check. */
4772enum wp_check_result
4773 {
4774 /* The watchpoint has been deleted. */
4775 WP_DELETED = 1,
4776
4777 /* The value has changed. */
4778 WP_VALUE_CHANGED = 2,
4779
4780 /* The value has not changed. */
4781 WP_VALUE_NOT_CHANGED = 3,
4782
4783 /* Ignore this watchpoint, no matter if the value changed or not. */
4784 WP_IGNORE = 4,
4785 };
c906108c
SS
4786
4787#define BP_TEMPFLAG 1
4788#define BP_HARDWAREFLAG 2
4789
4a64f543 4790/* Evaluate watchpoint condition expression and check if its value
bf469271 4791 changed. */
553e4c11 4792
bf469271
PA
4793static wp_check_result
4794watchpoint_check (bpstat bs)
c906108c 4795{
3a5c3e22 4796 struct watchpoint *b;
c906108c
SS
4797 struct frame_info *fr;
4798 int within_current_scope;
4799
f431efe5 4800 /* BS is built from an existing struct breakpoint. */
2bdf28a0 4801 gdb_assert (bs->breakpoint_at != NULL);
3a5c3e22 4802 b = (struct watchpoint *) bs->breakpoint_at;
d0fb5eae 4803
f6bc2008
PA
4804 /* If this is a local watchpoint, we only want to check if the
4805 watchpoint frame is in scope if the current thread is the thread
4806 that was used to create the watchpoint. */
4807 if (!watchpoint_in_thread_scope (b))
60e1c644 4808 return WP_IGNORE;
f6bc2008 4809
c906108c
SS
4810 if (b->exp_valid_block == NULL)
4811 within_current_scope = 1;
4812 else
4813 {
edb3359d
DJ
4814 struct frame_info *frame = get_current_frame ();
4815 struct gdbarch *frame_arch = get_frame_arch (frame);
4816 CORE_ADDR frame_pc = get_frame_pc (frame);
4817
c9cf6e20 4818 /* stack_frame_destroyed_p() returns a non-zero value if we're
4a64f543
MS
4819 still in the function but the stack frame has already been
4820 invalidated. Since we can't rely on the values of local
4821 variables after the stack has been destroyed, we are treating
4822 the watchpoint in that state as `not changed' without further
4823 checking. Don't mark watchpoints as changed if the current
4824 frame is in an epilogue - even if they are in some other
4825 frame, our view of the stack is likely to be wrong and
4826 frame_find_by_id could error out. */
c9cf6e20 4827 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
60e1c644 4828 return WP_IGNORE;
a0f49112 4829
101dcfbe 4830 fr = frame_find_by_id (b->watchpoint_frame);
c906108c 4831 within_current_scope = (fr != NULL);
69fbadd5
DJ
4832
4833 /* If we've gotten confused in the unwinder, we might have
4834 returned a frame that can't describe this variable. */
edb3359d
DJ
4835 if (within_current_scope)
4836 {
4837 struct symbol *function;
4838
4839 function = get_frame_function (fr);
4840 if (function == NULL
4841 || !contained_in (b->exp_valid_block,
4842 SYMBOL_BLOCK_VALUE (function)))
4843 within_current_scope = 0;
4844 }
69fbadd5 4845
edb3359d 4846 if (within_current_scope)
c906108c
SS
4847 /* If we end up stopping, the current frame will get selected
4848 in normal_stop. So this call to select_frame won't affect
4849 the user. */
0f7d239c 4850 select_frame (fr);
c906108c 4851 }
c5aa993b 4852
c906108c
SS
4853 if (within_current_scope)
4854 {
4a64f543
MS
4855 /* We use value_{,free_to_}mark because it could be a *long*
4856 time before we return to the command level and call
4857 free_all_values. We can't call free_all_values because we
4858 might be in the middle of evaluating a function call. */
c906108c 4859
0cf6dd15 4860 int pc = 0;
9c06b0b4 4861 struct value *mark;
fa4727a6
DJ
4862 struct value *new_val;
4863
c1fc2657 4864 if (is_masked_watchpoint (b))
9c06b0b4
TJB
4865 /* Since we don't know the exact trigger address (from
4866 stopped_data_address), just tell the user we've triggered
4867 a mask watchpoint. */
4868 return WP_VALUE_CHANGED;
4869
4870 mark = value_mark ();
4d01a485 4871 fetch_subexp_value (b->exp.get (), &pc, &new_val, NULL, NULL, 0);
218d2fc6 4872
bb9d5f81
PP
4873 if (b->val_bitsize != 0)
4874 new_val = extract_bitfield_from_watchpoint_value (b, new_val);
4875
4a64f543
MS
4876 /* We use value_equal_contents instead of value_equal because
4877 the latter coerces an array to a pointer, thus comparing just
4878 the address of the array instead of its contents. This is
4879 not what we want. */
fa4727a6 4880 if ((b->val != NULL) != (new_val != NULL)
850645cf
TT
4881 || (b->val != NULL && !value_equal_contents (b->val.get (),
4882 new_val)))
c906108c 4883 {
c906108c 4884 bs->old_val = b->val;
850645cf 4885 b->val = release_value (new_val);
fa4727a6 4886 b->val_valid = 1;
850645cf
TT
4887 if (new_val != NULL)
4888 value_free_to_mark (mark);
c906108c
SS
4889 return WP_VALUE_CHANGED;
4890 }
4891 else
4892 {
60e1c644 4893 /* Nothing changed. */
c906108c 4894 value_free_to_mark (mark);
c906108c
SS
4895 return WP_VALUE_NOT_CHANGED;
4896 }
4897 }
4898 else
4899 {
4900 /* This seems like the only logical thing to do because
c5aa993b
JM
4901 if we temporarily ignored the watchpoint, then when
4902 we reenter the block in which it is valid it contains
4903 garbage (in the case of a function, it may have two
4904 garbage values, one before and one after the prologue).
4905 So we can't even detect the first assignment to it and
4906 watch after that (since the garbage may or may not equal
4907 the first value assigned). */
348d480f
PA
4908 /* We print all the stop information in
4909 breakpoint_ops->print_it, but in this case, by the time we
4910 call breakpoint_ops->print_it this bp will be deleted
4911 already. So we have no choice but print the information
4912 here. */
468afe6c 4913
0e454242 4914 SWITCH_THRU_ALL_UIS ()
468afe6c
PA
4915 {
4916 struct ui_out *uiout = current_uiout;
4917
112e8700
SM
4918 if (uiout->is_mi_like_p ())
4919 uiout->field_string
4920 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
4921 uiout->text ("\nWatchpoint ");
c1fc2657 4922 uiout->field_int ("wpnum", b->number);
112e8700 4923 uiout->text (" deleted because the program has left the block in\n"
468afe6c
PA
4924 "which its expression is valid.\n");
4925 }
4ce44c66 4926
cdac0397 4927 /* Make sure the watchpoint's commands aren't executed. */
d1b0a7bf 4928 b->commands = NULL;
d0fb5eae 4929 watchpoint_del_at_next_stop (b);
c906108c
SS
4930
4931 return WP_DELETED;
4932 }
4933}
4934
18a18393 4935/* Return true if it looks like target has stopped due to hitting
348d480f
PA
4936 breakpoint location BL. This function does not check if we should
4937 stop, only if BL explains the stop. */
4938
18a18393 4939static int
6c95b8df 4940bpstat_check_location (const struct bp_location *bl,
accd0bcd 4941 const address_space *aspace, CORE_ADDR bp_addr,
09ac7c10 4942 const struct target_waitstatus *ws)
18a18393
VP
4943{
4944 struct breakpoint *b = bl->owner;
4945
348d480f 4946 /* BL is from an existing breakpoint. */
2bdf28a0
JK
4947 gdb_assert (b != NULL);
4948
bd522513 4949 return b->ops->breakpoint_hit (bl, aspace, bp_addr, ws);
18a18393
VP
4950}
4951
3a5c3e22
PA
4952/* Determine if the watched values have actually changed, and we
4953 should stop. If not, set BS->stop to 0. */
4954
18a18393
VP
4955static void
4956bpstat_check_watchpoint (bpstat bs)
4957{
2bdf28a0 4958 const struct bp_location *bl;
3a5c3e22 4959 struct watchpoint *b;
2bdf28a0
JK
4960
4961 /* BS is built for existing struct breakpoint. */
f431efe5 4962 bl = bs->bp_location_at;
2bdf28a0 4963 gdb_assert (bl != NULL);
3a5c3e22 4964 b = (struct watchpoint *) bs->breakpoint_at;
2bdf28a0 4965 gdb_assert (b != NULL);
18a18393 4966
18a18393 4967 {
18a18393
VP
4968 int must_check_value = 0;
4969
c1fc2657 4970 if (b->type == bp_watchpoint)
18a18393
VP
4971 /* For a software watchpoint, we must always check the
4972 watched value. */
4973 must_check_value = 1;
4974 else if (b->watchpoint_triggered == watch_triggered_yes)
4975 /* We have a hardware watchpoint (read, write, or access)
4976 and the target earlier reported an address watched by
4977 this watchpoint. */
4978 must_check_value = 1;
4979 else if (b->watchpoint_triggered == watch_triggered_unknown
c1fc2657 4980 && b->type == bp_hardware_watchpoint)
18a18393
VP
4981 /* We were stopped by a hardware watchpoint, but the target could
4982 not report the data address. We must check the watchpoint's
4983 value. Access and read watchpoints are out of luck; without
4984 a data address, we can't figure it out. */
4985 must_check_value = 1;
3a5c3e22 4986
18a18393
VP
4987 if (must_check_value)
4988 {
bf469271
PA
4989 wp_check_result e;
4990
a70b8144 4991 try
bf469271
PA
4992 {
4993 e = watchpoint_check (bs);
4994 }
230d2906 4995 catch (const gdb_exception &ex)
bf469271
PA
4996 {
4997 exception_fprintf (gdb_stderr, ex,
4998 "Error evaluating expression "
4999 "for watchpoint %d\n",
5000 b->number);
5001
5002 SWITCH_THRU_ALL_UIS ()
5003 {
5004 printf_filtered (_("Watchpoint %d deleted.\n"),
5005 b->number);
5006 }
5007 watchpoint_del_at_next_stop (b);
5008 e = WP_DELETED;
5009 }
bf469271 5010
18a18393
VP
5011 switch (e)
5012 {
5013 case WP_DELETED:
5014 /* We've already printed what needs to be printed. */
5015 bs->print_it = print_it_done;
5016 /* Stop. */
5017 break;
60e1c644
PA
5018 case WP_IGNORE:
5019 bs->print_it = print_it_noop;
5020 bs->stop = 0;
5021 break;
18a18393 5022 case WP_VALUE_CHANGED:
c1fc2657 5023 if (b->type == bp_read_watchpoint)
18a18393 5024 {
85d721b8
PA
5025 /* There are two cases to consider here:
5026
4a64f543 5027 1. We're watching the triggered memory for reads.
85d721b8
PA
5028 In that case, trust the target, and always report
5029 the watchpoint hit to the user. Even though
5030 reads don't cause value changes, the value may
5031 have changed since the last time it was read, and
5032 since we're not trapping writes, we will not see
5033 those, and as such we should ignore our notion of
5034 old value.
5035
4a64f543 5036 2. We're watching the triggered memory for both
85d721b8
PA
5037 reads and writes. There are two ways this may
5038 happen:
5039
4a64f543 5040 2.1. This is a target that can't break on data
85d721b8
PA
5041 reads only, but can break on accesses (reads or
5042 writes), such as e.g., x86. We detect this case
5043 at the time we try to insert read watchpoints.
5044
4a64f543 5045 2.2. Otherwise, the target supports read
85d721b8
PA
5046 watchpoints, but, the user set an access or write
5047 watchpoint watching the same memory as this read
5048 watchpoint.
5049
5050 If we're watching memory writes as well as reads,
5051 ignore watchpoint hits when we find that the
5052 value hasn't changed, as reads don't cause
5053 changes. This still gives false positives when
5054 the program writes the same value to memory as
5055 what there was already in memory (we will confuse
5056 it for a read), but it's much better than
5057 nothing. */
5058
5059 int other_write_watchpoint = 0;
5060
5061 if (bl->watchpoint_type == hw_read)
5062 {
5063 struct breakpoint *other_b;
5064
5065 ALL_BREAKPOINTS (other_b)
3a5c3e22
PA
5066 if (other_b->type == bp_hardware_watchpoint
5067 || other_b->type == bp_access_watchpoint)
85d721b8 5068 {
3a5c3e22
PA
5069 struct watchpoint *other_w =
5070 (struct watchpoint *) other_b;
5071
5072 if (other_w->watchpoint_triggered
5073 == watch_triggered_yes)
5074 {
5075 other_write_watchpoint = 1;
5076 break;
5077 }
85d721b8
PA
5078 }
5079 }
5080
5081 if (other_write_watchpoint
5082 || bl->watchpoint_type == hw_access)
5083 {
5084 /* We're watching the same memory for writes,
5085 and the value changed since the last time we
5086 updated it, so this trap must be for a write.
5087 Ignore it. */
5088 bs->print_it = print_it_noop;
5089 bs->stop = 0;
5090 }
18a18393
VP
5091 }
5092 break;
5093 case WP_VALUE_NOT_CHANGED:
c1fc2657
SM
5094 if (b->type == bp_hardware_watchpoint
5095 || b->type == bp_watchpoint)
18a18393
VP
5096 {
5097 /* Don't stop: write watchpoints shouldn't fire if
5098 the value hasn't changed. */
5099 bs->print_it = print_it_noop;
5100 bs->stop = 0;
5101 }
5102 /* Stop. */
5103 break;
5104 default:
5105 /* Can't happen. */
18a18393
VP
5106 break;
5107 }
5108 }
5109 else /* must_check_value == 0 */
5110 {
5111 /* This is a case where some watchpoint(s) triggered, but
5112 not at the address of this watchpoint, or else no
5113 watchpoint triggered after all. So don't print
5114 anything for this watchpoint. */
5115 bs->print_it = print_it_noop;
5116 bs->stop = 0;
5117 }
5118 }
5119}
5120
7d4df6a4
DE
5121/* For breakpoints that are currently marked as telling gdb to stop,
5122 check conditions (condition proper, frame, thread and ignore count)
18a18393
VP
5123 of breakpoint referred to by BS. If we should not stop for this
5124 breakpoint, set BS->stop to 0. */
f431efe5 5125
18a18393 5126static void
00431a78 5127bpstat_check_breakpoint_conditions (bpstat bs, thread_info *thread)
18a18393 5128{
2bdf28a0
JK
5129 const struct bp_location *bl;
5130 struct breakpoint *b;
bf469271
PA
5131 /* Assume stop. */
5132 bool condition_result = true;
7d4df6a4
DE
5133 struct expression *cond;
5134
5135 gdb_assert (bs->stop);
2bdf28a0
JK
5136
5137 /* BS is built for existing struct breakpoint. */
f431efe5 5138 bl = bs->bp_location_at;
2bdf28a0 5139 gdb_assert (bl != NULL);
f431efe5 5140 b = bs->breakpoint_at;
2bdf28a0 5141 gdb_assert (b != NULL);
18a18393 5142
b775012e
LM
5143 /* Even if the target evaluated the condition on its end and notified GDB, we
5144 need to do so again since GDB does not know if we stopped due to a
5145 breakpoint or a single step breakpoint. */
5146
18a18393 5147 if (frame_id_p (b->frame_id)
edb3359d 5148 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
18a18393 5149 {
7d4df6a4
DE
5150 bs->stop = 0;
5151 return;
5152 }
60e1c644 5153
12ab52e9
PA
5154 /* If this is a thread/task-specific breakpoint, don't waste cpu
5155 evaluating the condition if this isn't the specified
5156 thread/task. */
00431a78
PA
5157 if ((b->thread != -1 && b->thread != thread->global_num)
5158 || (b->task != 0 && b->task != ada_get_task_number (thread)))
6c1b0f7b
DE
5159 {
5160 bs->stop = 0;
5161 return;
5162 }
5163
6dddc817
DE
5164 /* Evaluate extension language breakpoints that have a "stop" method
5165 implemented. */
5166 bs->stop = breakpoint_ext_lang_cond_says_stop (b);
7371cf6d 5167
7d4df6a4
DE
5168 if (is_watchpoint (b))
5169 {
5170 struct watchpoint *w = (struct watchpoint *) b;
3a5c3e22 5171
4d01a485 5172 cond = w->cond_exp.get ();
7d4df6a4
DE
5173 }
5174 else
4d01a485 5175 cond = bl->cond.get ();
60e1c644 5176
7d4df6a4
DE
5177 if (cond && b->disposition != disp_del_at_next_stop)
5178 {
5179 int within_current_scope = 1;
5180 struct watchpoint * w;
60e1c644 5181
7d4df6a4
DE
5182 /* We use value_mark and value_free_to_mark because it could
5183 be a long time before we return to the command level and
5184 call free_all_values. We can't call free_all_values
5185 because we might be in the middle of evaluating a
5186 function call. */
5187 struct value *mark = value_mark ();
5188
5189 if (is_watchpoint (b))
5190 w = (struct watchpoint *) b;
5191 else
5192 w = NULL;
5193
5194 /* Need to select the frame, with all that implies so that
5195 the conditions will have the right context. Because we
5196 use the frame, we will not see an inlined function's
5197 variables when we arrive at a breakpoint at the start
5198 of the inlined function; the current frame will be the
5199 call site. */
5200 if (w == NULL || w->cond_exp_valid_block == NULL)
5201 select_frame (get_current_frame ());
5202 else
18a18393 5203 {
7d4df6a4
DE
5204 struct frame_info *frame;
5205
5206 /* For local watchpoint expressions, which particular
5207 instance of a local is being watched matters, so we
5208 keep track of the frame to evaluate the expression
5209 in. To evaluate the condition however, it doesn't
5210 really matter which instantiation of the function
5211 where the condition makes sense triggers the
5212 watchpoint. This allows an expression like "watch
5213 global if q > 10" set in `func', catch writes to
5214 global on all threads that call `func', or catch
5215 writes on all recursive calls of `func' by a single
5216 thread. We simply always evaluate the condition in
5217 the innermost frame that's executing where it makes
5218 sense to evaluate the condition. It seems
5219 intuitive. */
5220 frame = block_innermost_frame (w->cond_exp_valid_block);
5221 if (frame != NULL)
5222 select_frame (frame);
5223 else
5224 within_current_scope = 0;
18a18393 5225 }
7d4df6a4 5226 if (within_current_scope)
bf469271 5227 {
a70b8144 5228 try
bf469271
PA
5229 {
5230 condition_result = breakpoint_cond_eval (cond);
5231 }
230d2906 5232 catch (const gdb_exception &ex)
bf469271
PA
5233 {
5234 exception_fprintf (gdb_stderr, ex,
5235 "Error in testing breakpoint condition:\n");
5236 }
bf469271 5237 }
7d4df6a4 5238 else
18a18393 5239 {
7d4df6a4
DE
5240 warning (_("Watchpoint condition cannot be tested "
5241 "in the current scope"));
5242 /* If we failed to set the right context for this
5243 watchpoint, unconditionally report it. */
18a18393 5244 }
7d4df6a4
DE
5245 /* FIXME-someday, should give breakpoint #. */
5246 value_free_to_mark (mark);
18a18393 5247 }
7d4df6a4 5248
bf469271 5249 if (cond && !condition_result)
7d4df6a4
DE
5250 {
5251 bs->stop = 0;
5252 }
7d4df6a4
DE
5253 else if (b->ignore_count > 0)
5254 {
5255 b->ignore_count--;
5256 bs->stop = 0;
5257 /* Increase the hit count even though we don't stop. */
5258 ++(b->hit_count);
76727919 5259 gdb::observers::breakpoint_modified.notify (b);
7d4df6a4 5260 }
18a18393
VP
5261}
5262
1cf4d951
PA
5263/* Returns true if we need to track moribund locations of LOC's type
5264 on the current target. */
5265
5266static int
5267need_moribund_for_location_type (struct bp_location *loc)
5268{
5269 return ((loc->loc_type == bp_loc_software_breakpoint
5270 && !target_supports_stopped_by_sw_breakpoint ())
5271 || (loc->loc_type == bp_loc_hardware_breakpoint
5272 && !target_supports_stopped_by_hw_breakpoint ()));
5273}
5274
ddfe970e 5275/* See breakpoint.h. */
c906108c
SS
5276
5277bpstat
ddfe970e 5278build_bpstat_chain (const address_space *aspace, CORE_ADDR bp_addr,
09ac7c10 5279 const struct target_waitstatus *ws)
c906108c 5280{
ddfe970e 5281 struct breakpoint *b;
5760d0ab 5282 bpstat bs_head = NULL, *bs_link = &bs_head;
c5aa993b 5283
429374b8
JK
5284 ALL_BREAKPOINTS (b)
5285 {
1a853c52 5286 if (!breakpoint_enabled (b))
429374b8 5287 continue;
a5606eee 5288
ddfe970e 5289 for (bp_location *bl = b->loc; bl != NULL; bl = bl->next)
429374b8 5290 {
4a64f543
MS
5291 /* For hardware watchpoints, we look only at the first
5292 location. The watchpoint_check function will work on the
5293 entire expression, not the individual locations. For
5294 read watchpoints, the watchpoints_triggered function has
5295 checked all locations already. */
429374b8
JK
5296 if (b->type == bp_hardware_watchpoint && bl != b->loc)
5297 break;
18a18393 5298
f6592439 5299 if (!bl->enabled || bl->shlib_disabled)
429374b8 5300 continue;
c5aa993b 5301
09ac7c10 5302 if (!bpstat_check_location (bl, aspace, bp_addr, ws))
429374b8 5303 continue;
c5aa993b 5304
4a64f543
MS
5305 /* Come here if it's a watchpoint, or if the break address
5306 matches. */
c5aa993b 5307
ddfe970e
KS
5308 bpstat bs = new bpstats (bl, &bs_link); /* Alloc a bpstat to
5309 explain stop. */
c5aa993b 5310
f431efe5
PA
5311 /* Assume we stop. Should we find a watchpoint that is not
5312 actually triggered, or if the condition of the breakpoint
5313 evaluates as false, we'll reset 'stop' to 0. */
429374b8
JK
5314 bs->stop = 1;
5315 bs->print = 1;
d983da9c 5316
f431efe5
PA
5317 /* If this is a scope breakpoint, mark the associated
5318 watchpoint as triggered so that we will handle the
5319 out-of-scope event. We'll get to the watchpoint next
5320 iteration. */
d0fb5eae 5321 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
3a5c3e22
PA
5322 {
5323 struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
5324
5325 w->watchpoint_triggered = watch_triggered_yes;
5326 }
f431efe5
PA
5327 }
5328 }
5329
7c16b83e 5330 /* Check if a moribund breakpoint explains the stop. */
1cf4d951
PA
5331 if (!target_supports_stopped_by_sw_breakpoint ()
5332 || !target_supports_stopped_by_hw_breakpoint ())
f431efe5 5333 {
1123588c 5334 for (bp_location *loc : moribund_locations)
f431efe5 5335 {
1cf4d951
PA
5336 if (breakpoint_location_address_match (loc, aspace, bp_addr)
5337 && need_moribund_for_location_type (loc))
5338 {
ddfe970e 5339 bpstat bs = new bpstats (loc, &bs_link);
1cf4d951
PA
5340 /* For hits of moribund locations, we should just proceed. */
5341 bs->stop = 0;
5342 bs->print = 0;
5343 bs->print_it = print_it_noop;
5344 }
f431efe5
PA
5345 }
5346 }
5347
ddfe970e
KS
5348 return bs_head;
5349}
5350
5351/* See breakpoint.h. */
5352
5353bpstat
5354bpstat_stop_status (const address_space *aspace,
00431a78 5355 CORE_ADDR bp_addr, thread_info *thread,
ddfe970e
KS
5356 const struct target_waitstatus *ws,
5357 bpstat stop_chain)
5358{
5359 struct breakpoint *b = NULL;
5360 /* First item of allocated bpstat's. */
5361 bpstat bs_head = stop_chain;
5362 bpstat bs;
5363 int need_remove_insert;
5364 int removed_any;
5365
5366 /* First, build the bpstat chain with locations that explain a
5367 target stop, while being careful to not set the target running,
5368 as that may invalidate locations (in particular watchpoint
5369 locations are recreated). Resuming will happen here with
5370 breakpoint conditions or watchpoint expressions that include
5371 inferior function calls. */
5372 if (bs_head == NULL)
5373 bs_head = build_bpstat_chain (aspace, bp_addr, ws);
5374
edcc5120
TT
5375 /* A bit of special processing for shlib breakpoints. We need to
5376 process solib loading here, so that the lists of loaded and
5377 unloaded libraries are correct before we handle "catch load" and
5378 "catch unload". */
5379 for (bs = bs_head; bs != NULL; bs = bs->next)
5380 {
5d268276 5381 if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
edcc5120
TT
5382 {
5383 handle_solib_event ();
5384 break;
5385 }
5386 }
5387
f431efe5
PA
5388 /* Now go through the locations that caused the target to stop, and
5389 check whether we're interested in reporting this stop to higher
5390 layers, or whether we should resume the target transparently. */
5391
5392 removed_any = 0;
5393
5760d0ab 5394 for (bs = bs_head; bs != NULL; bs = bs->next)
f431efe5
PA
5395 {
5396 if (!bs->stop)
5397 continue;
5398
f431efe5 5399 b = bs->breakpoint_at;
348d480f
PA
5400 b->ops->check_status (bs);
5401 if (bs->stop)
28010a5d 5402 {
00431a78 5403 bpstat_check_breakpoint_conditions (bs, thread);
f431efe5 5404
429374b8
JK
5405 if (bs->stop)
5406 {
5407 ++(b->hit_count);
76727919 5408 gdb::observers::breakpoint_modified.notify (b);
c906108c 5409
4a64f543 5410 /* We will stop here. */
429374b8
JK
5411 if (b->disposition == disp_disable)
5412 {
816338b5 5413 --(b->enable_count);
1a853c52 5414 if (b->enable_count <= 0)
429374b8 5415 b->enable_state = bp_disabled;
f431efe5 5416 removed_any = 1;
429374b8
JK
5417 }
5418 if (b->silent)
5419 bs->print = 0;
5420 bs->commands = b->commands;
abf85f46 5421 if (command_line_is_silent (bs->commands
d1b0a7bf 5422 ? bs->commands.get () : NULL))
abf85f46 5423 bs->print = 0;
9d6e6e84
HZ
5424
5425 b->ops->after_condition_true (bs);
429374b8
JK
5426 }
5427
348d480f 5428 }
a9b3a50f
PA
5429
5430 /* Print nothing for this entry if we don't stop or don't
5431 print. */
5432 if (!bs->stop || !bs->print)
5433 bs->print_it = print_it_noop;
429374b8 5434 }
876fa593 5435
d983da9c
DJ
5436 /* If we aren't stopping, the value of some hardware watchpoint may
5437 not have changed, but the intermediate memory locations we are
5438 watching may have. Don't bother if we're stopping; this will get
5439 done later. */
d832cb68 5440 need_remove_insert = 0;
5760d0ab
JK
5441 if (! bpstat_causes_stop (bs_head))
5442 for (bs = bs_head; bs != NULL; bs = bs->next)
d983da9c 5443 if (!bs->stop
f431efe5
PA
5444 && bs->breakpoint_at
5445 && is_hardware_watchpoint (bs->breakpoint_at))
d983da9c 5446 {
3a5c3e22
PA
5447 struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
5448
5449 update_watchpoint (w, 0 /* don't reparse. */);
d832cb68 5450 need_remove_insert = 1;
d983da9c
DJ
5451 }
5452
d832cb68 5453 if (need_remove_insert)
44702360 5454 update_global_location_list (UGLL_MAY_INSERT);
f431efe5 5455 else if (removed_any)
44702360 5456 update_global_location_list (UGLL_DONT_INSERT);
d832cb68 5457
5760d0ab 5458 return bs_head;
c906108c 5459}
628fe4e4
JK
5460
5461static void
5462handle_jit_event (void)
5463{
5464 struct frame_info *frame;
5465 struct gdbarch *gdbarch;
5466
243a9253
PA
5467 if (debug_infrun)
5468 fprintf_unfiltered (gdb_stdlog, "handling bp_jit_event\n");
5469
628fe4e4
JK
5470 /* Switch terminal for any messages produced by
5471 breakpoint_re_set. */
223ffa71 5472 target_terminal::ours_for_output ();
628fe4e4
JK
5473
5474 frame = get_current_frame ();
5475 gdbarch = get_frame_arch (frame);
5476
5477 jit_event_handler (gdbarch);
5478
223ffa71 5479 target_terminal::inferior ();
628fe4e4
JK
5480}
5481
5482/* Prepare WHAT final decision for infrun. */
5483
5484/* Decide what infrun needs to do with this bpstat. */
5485
c906108c 5486struct bpstat_what
0e30163f 5487bpstat_what (bpstat bs_head)
c906108c 5488{
c906108c 5489 struct bpstat_what retval;
0e30163f 5490 bpstat bs;
c906108c 5491
628fe4e4 5492 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
aa7d318d 5493 retval.call_dummy = STOP_NONE;
186c406b 5494 retval.is_longjmp = 0;
628fe4e4 5495
0e30163f 5496 for (bs = bs_head; bs != NULL; bs = bs->next)
c906108c 5497 {
628fe4e4
JK
5498 /* Extract this BS's action. After processing each BS, we check
5499 if its action overrides all we've seem so far. */
5500 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
5501 enum bptype bptype;
5502
c906108c 5503 if (bs->breakpoint_at == NULL)
628fe4e4
JK
5504 {
5505 /* I suspect this can happen if it was a momentary
5506 breakpoint which has since been deleted. */
5507 bptype = bp_none;
5508 }
20874c92 5509 else
f431efe5 5510 bptype = bs->breakpoint_at->type;
628fe4e4
JK
5511
5512 switch (bptype)
c906108c
SS
5513 {
5514 case bp_none:
628fe4e4 5515 break;
c906108c
SS
5516 case bp_breakpoint:
5517 case bp_hardware_breakpoint:
7c16b83e 5518 case bp_single_step:
c906108c
SS
5519 case bp_until:
5520 case bp_finish:
a9b3a50f 5521 case bp_shlib_event:
c906108c
SS
5522 if (bs->stop)
5523 {
5524 if (bs->print)
628fe4e4 5525 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 5526 else
628fe4e4 5527 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
5528 }
5529 else
628fe4e4 5530 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5531 break;
5532 case bp_watchpoint:
5533 case bp_hardware_watchpoint:
5534 case bp_read_watchpoint:
5535 case bp_access_watchpoint:
5536 if (bs->stop)
5537 {
5538 if (bs->print)
628fe4e4 5539 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 5540 else
628fe4e4 5541 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
5542 }
5543 else
628fe4e4
JK
5544 {
5545 /* There was a watchpoint, but we're not stopping.
5546 This requires no further action. */
5547 }
c906108c
SS
5548 break;
5549 case bp_longjmp:
e2e4d78b 5550 case bp_longjmp_call_dummy:
186c406b 5551 case bp_exception:
0a39bb32
PA
5552 if (bs->stop)
5553 {
5554 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
5555 retval.is_longjmp = bptype != bp_exception;
5556 }
5557 else
5558 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5559 break;
5560 case bp_longjmp_resume:
186c406b 5561 case bp_exception_resume:
0a39bb32
PA
5562 if (bs->stop)
5563 {
5564 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
5565 retval.is_longjmp = bptype == bp_longjmp_resume;
5566 }
5567 else
5568 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5569 break;
5570 case bp_step_resume:
5571 if (bs->stop)
628fe4e4
JK
5572 this_action = BPSTAT_WHAT_STEP_RESUME;
5573 else
c906108c 5574 {
628fe4e4
JK
5575 /* It is for the wrong frame. */
5576 this_action = BPSTAT_WHAT_SINGLE;
c906108c 5577 }
c906108c 5578 break;
2c03e5be
PA
5579 case bp_hp_step_resume:
5580 if (bs->stop)
5581 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
5582 else
5583 {
5584 /* It is for the wrong frame. */
5585 this_action = BPSTAT_WHAT_SINGLE;
5586 }
5587 break;
c906108c 5588 case bp_watchpoint_scope:
c4093a6a 5589 case bp_thread_event:
1900040c 5590 case bp_overlay_event:
0fd8e87f 5591 case bp_longjmp_master:
aa7d318d 5592 case bp_std_terminate_master:
186c406b 5593 case bp_exception_master:
628fe4e4 5594 this_action = BPSTAT_WHAT_SINGLE;
c4093a6a 5595 break;
ce78b96d 5596 case bp_catchpoint:
c5aa993b
JM
5597 if (bs->stop)
5598 {
5599 if (bs->print)
628fe4e4 5600 this_action = BPSTAT_WHAT_STOP_NOISY;
c5aa993b 5601 else
628fe4e4 5602 this_action = BPSTAT_WHAT_STOP_SILENT;
c5aa993b
JM
5603 }
5604 else
628fe4e4 5605 {
cb1e4e32
PA
5606 /* Some catchpoints are implemented with breakpoints.
5607 For those, we need to step over the breakpoint. */
5608 if (bs->bp_location_at->loc_type != bp_loc_other)
5609 this_action = BPSTAT_WHAT_SINGLE;
628fe4e4
JK
5610 }
5611 break;
628fe4e4 5612 case bp_jit_event:
628fe4e4 5613 this_action = BPSTAT_WHAT_SINGLE;
c5aa993b 5614 break;
c906108c 5615 case bp_call_dummy:
53a5351d
JM
5616 /* Make sure the action is stop (silent or noisy),
5617 so infrun.c pops the dummy frame. */
aa7d318d 5618 retval.call_dummy = STOP_STACK_DUMMY;
628fe4e4 5619 this_action = BPSTAT_WHAT_STOP_SILENT;
aa7d318d
TT
5620 break;
5621 case bp_std_terminate:
5622 /* Make sure the action is stop (silent or noisy),
5623 so infrun.c pops the dummy frame. */
aa7d318d 5624 retval.call_dummy = STOP_STD_TERMINATE;
628fe4e4 5625 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c 5626 break;
1042e4c0 5627 case bp_tracepoint:
7a697b8d 5628 case bp_fast_tracepoint:
0fb4aa4b 5629 case bp_static_tracepoint:
1042e4c0
SS
5630 /* Tracepoint hits should not be reported back to GDB, and
5631 if one got through somehow, it should have been filtered
5632 out already. */
5633 internal_error (__FILE__, __LINE__,
7a697b8d 5634 _("bpstat_what: tracepoint encountered"));
0e30163f
JK
5635 break;
5636 case bp_gnu_ifunc_resolver:
5637 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
5638 this_action = BPSTAT_WHAT_SINGLE;
5639 break;
5640 case bp_gnu_ifunc_resolver_return:
5641 /* The breakpoint will be removed, execution will restart from the
5642 PC of the former breakpoint. */
5643 this_action = BPSTAT_WHAT_KEEP_CHECKING;
5644 break;
e7e0cddf
SS
5645
5646 case bp_dprintf:
a11cfd87
HZ
5647 if (bs->stop)
5648 this_action = BPSTAT_WHAT_STOP_SILENT;
5649 else
5650 this_action = BPSTAT_WHAT_SINGLE;
e7e0cddf
SS
5651 break;
5652
628fe4e4
JK
5653 default:
5654 internal_error (__FILE__, __LINE__,
5655 _("bpstat_what: unhandled bptype %d"), (int) bptype);
c906108c 5656 }
628fe4e4 5657
325fac50 5658 retval.main_action = std::max (retval.main_action, this_action);
c906108c 5659 }
628fe4e4 5660
243a9253
PA
5661 return retval;
5662}
628fe4e4 5663
243a9253
PA
5664void
5665bpstat_run_callbacks (bpstat bs_head)
5666{
5667 bpstat bs;
628fe4e4 5668
0e30163f
JK
5669 for (bs = bs_head; bs != NULL; bs = bs->next)
5670 {
5671 struct breakpoint *b = bs->breakpoint_at;
5672
5673 if (b == NULL)
5674 continue;
5675 switch (b->type)
5676 {
243a9253
PA
5677 case bp_jit_event:
5678 handle_jit_event ();
5679 break;
0e30163f
JK
5680 case bp_gnu_ifunc_resolver:
5681 gnu_ifunc_resolver_stop (b);
5682 break;
5683 case bp_gnu_ifunc_resolver_return:
5684 gnu_ifunc_resolver_return_stop (b);
5685 break;
5686 }
5687 }
c906108c
SS
5688}
5689
5690/* Nonzero if we should step constantly (e.g. watchpoints on machines
5691 without hardware support). This isn't related to a specific bpstat,
5692 just to things like whether watchpoints are set. */
5693
c5aa993b 5694int
fba45db2 5695bpstat_should_step (void)
c906108c
SS
5696{
5697 struct breakpoint *b;
cc59ec59 5698
c906108c 5699 ALL_BREAKPOINTS (b)
717a8278 5700 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
3172dc30 5701 return 1;
c906108c
SS
5702 return 0;
5703}
5704
67822962
PA
5705int
5706bpstat_causes_stop (bpstat bs)
5707{
5708 for (; bs != NULL; bs = bs->next)
5709 if (bs->stop)
5710 return 1;
5711
5712 return 0;
5713}
5714
c906108c 5715\f
c5aa993b 5716
170b53b2
UW
5717/* Compute a string of spaces suitable to indent the next line
5718 so it starts at the position corresponding to the table column
5719 named COL_NAME in the currently active table of UIOUT. */
5720
5721static char *
5722wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
5723{
5724 static char wrap_indent[80];
5725 int i, total_width, width, align;
c5209615 5726 const char *text;
170b53b2
UW
5727
5728 total_width = 0;
112e8700 5729 for (i = 1; uiout->query_table_field (i, &width, &align, &text); i++)
170b53b2
UW
5730 {
5731 if (strcmp (text, col_name) == 0)
5732 {
5733 gdb_assert (total_width < sizeof wrap_indent);
5734 memset (wrap_indent, ' ', total_width);
5735 wrap_indent[total_width] = 0;
5736
5737 return wrap_indent;
5738 }
5739
5740 total_width += width + 1;
5741 }
5742
5743 return NULL;
5744}
5745
b775012e
LM
5746/* Determine if the locations of this breakpoint will have their conditions
5747 evaluated by the target, host or a mix of both. Returns the following:
5748
5749 "host": Host evals condition.
5750 "host or target": Host or Target evals condition.
5751 "target": Target evals condition.
5752*/
5753
5754static const char *
5755bp_condition_evaluator (struct breakpoint *b)
5756{
5757 struct bp_location *bl;
5758 char host_evals = 0;
5759 char target_evals = 0;
5760
5761 if (!b)
5762 return NULL;
5763
5764 if (!is_breakpoint (b))
5765 return NULL;
5766
5767 if (gdb_evaluates_breakpoint_condition_p ()
5768 || !target_supports_evaluation_of_breakpoint_conditions ())
5769 return condition_evaluation_host;
5770
5771 for (bl = b->loc; bl; bl = bl->next)
5772 {
5773 if (bl->cond_bytecode)
5774 target_evals++;
5775 else
5776 host_evals++;
5777 }
5778
5779 if (host_evals && target_evals)
5780 return condition_evaluation_both;
5781 else if (target_evals)
5782 return condition_evaluation_target;
5783 else
5784 return condition_evaluation_host;
5785}
5786
5787/* Determine the breakpoint location's condition evaluator. This is
5788 similar to bp_condition_evaluator, but for locations. */
5789
5790static const char *
5791bp_location_condition_evaluator (struct bp_location *bl)
5792{
5793 if (bl && !is_breakpoint (bl->owner))
5794 return NULL;
5795
5796 if (gdb_evaluates_breakpoint_condition_p ()
5797 || !target_supports_evaluation_of_breakpoint_conditions ())
5798 return condition_evaluation_host;
5799
5800 if (bl && bl->cond_bytecode)
5801 return condition_evaluation_target;
5802 else
5803 return condition_evaluation_host;
5804}
5805
859825b8
JK
5806/* Print the LOC location out of the list of B->LOC locations. */
5807
170b53b2
UW
5808static void
5809print_breakpoint_location (struct breakpoint *b,
5810 struct bp_location *loc)
0d381245 5811{
79a45e25 5812 struct ui_out *uiout = current_uiout;
5ed8105e
PA
5813
5814 scoped_restore_current_program_space restore_pspace;
6c95b8df 5815
859825b8
JK
5816 if (loc != NULL && loc->shlib_disabled)
5817 loc = NULL;
5818
6c95b8df
PA
5819 if (loc != NULL)
5820 set_current_program_space (loc->pspace);
5821
56435ebe 5822 if (b->display_canonical)
d28cd78a 5823 uiout->field_string ("what", event_location_to_string (b->location.get ()));
2f202fde 5824 else if (loc && loc->symtab)
0d381245 5825 {
4a27f119
KS
5826 const struct symbol *sym = loc->symbol;
5827
0d381245
VP
5828 if (sym)
5829 {
112e8700 5830 uiout->text ("in ");
cbe56571
TT
5831 uiout->field_string ("func", SYMBOL_PRINT_NAME (sym),
5832 ui_out_style_kind::FUNCTION);
112e8700
SM
5833 uiout->text (" ");
5834 uiout->wrap_hint (wrap_indent_at_field (uiout, "what"));
5835 uiout->text ("at ");
0d381245 5836 }
112e8700 5837 uiout->field_string ("file",
cbe56571
TT
5838 symtab_to_filename_for_display (loc->symtab),
5839 ui_out_style_kind::FILE);
112e8700 5840 uiout->text (":");
05cba821 5841
112e8700
SM
5842 if (uiout->is_mi_like_p ())
5843 uiout->field_string ("fullname", symtab_to_fullname (loc->symtab));
0d381245 5844
112e8700 5845 uiout->field_int ("line", loc->line_number);
0d381245 5846 }
859825b8 5847 else if (loc)
0d381245 5848 {
d7e74731 5849 string_file stb;
170b53b2 5850
d7e74731 5851 print_address_symbolic (loc->gdbarch, loc->address, &stb,
22e722e1 5852 demangle, "");
112e8700 5853 uiout->field_stream ("at", stb);
0d381245 5854 }
859825b8 5855 else
f00aae0f 5856 {
d28cd78a
TT
5857 uiout->field_string ("pending",
5858 event_location_to_string (b->location.get ()));
f00aae0f
KS
5859 /* If extra_string is available, it could be holding a condition
5860 or dprintf arguments. In either case, make sure it is printed,
5861 too, but only for non-MI streams. */
112e8700 5862 if (!uiout->is_mi_like_p () && b->extra_string != NULL)
f00aae0f
KS
5863 {
5864 if (b->type == bp_dprintf)
112e8700 5865 uiout->text (",");
f00aae0f 5866 else
112e8700
SM
5867 uiout->text (" ");
5868 uiout->text (b->extra_string);
f00aae0f
KS
5869 }
5870 }
6c95b8df 5871
b775012e
LM
5872 if (loc && is_breakpoint (b)
5873 && breakpoint_condition_evaluation_mode () == condition_evaluation_target
5874 && bp_condition_evaluator (b) == condition_evaluation_both)
5875 {
112e8700
SM
5876 uiout->text (" (");
5877 uiout->field_string ("evaluated-by",
b775012e 5878 bp_location_condition_evaluator (loc));
112e8700 5879 uiout->text (")");
b775012e 5880 }
0d381245
VP
5881}
5882
269b11a2
PA
5883static const char *
5884bptype_string (enum bptype type)
c906108c 5885{
c4093a6a
JM
5886 struct ep_type_description
5887 {
5888 enum bptype type;
a121b7c1 5889 const char *description;
c4093a6a
JM
5890 };
5891 static struct ep_type_description bptypes[] =
c906108c 5892 {
c5aa993b
JM
5893 {bp_none, "?deleted?"},
5894 {bp_breakpoint, "breakpoint"},
c906108c 5895 {bp_hardware_breakpoint, "hw breakpoint"},
7c16b83e 5896 {bp_single_step, "sw single-step"},
c5aa993b
JM
5897 {bp_until, "until"},
5898 {bp_finish, "finish"},
5899 {bp_watchpoint, "watchpoint"},
c906108c 5900 {bp_hardware_watchpoint, "hw watchpoint"},
c5aa993b
JM
5901 {bp_read_watchpoint, "read watchpoint"},
5902 {bp_access_watchpoint, "acc watchpoint"},
5903 {bp_longjmp, "longjmp"},
5904 {bp_longjmp_resume, "longjmp resume"},
e2e4d78b 5905 {bp_longjmp_call_dummy, "longjmp for call dummy"},
186c406b
TT
5906 {bp_exception, "exception"},
5907 {bp_exception_resume, "exception resume"},
c5aa993b 5908 {bp_step_resume, "step resume"},
2c03e5be 5909 {bp_hp_step_resume, "high-priority step resume"},
c5aa993b
JM
5910 {bp_watchpoint_scope, "watchpoint scope"},
5911 {bp_call_dummy, "call dummy"},
aa7d318d 5912 {bp_std_terminate, "std::terminate"},
c5aa993b 5913 {bp_shlib_event, "shlib events"},
c4093a6a 5914 {bp_thread_event, "thread events"},
1900040c 5915 {bp_overlay_event, "overlay events"},
0fd8e87f 5916 {bp_longjmp_master, "longjmp master"},
aa7d318d 5917 {bp_std_terminate_master, "std::terminate master"},
186c406b 5918 {bp_exception_master, "exception master"},
ce78b96d 5919 {bp_catchpoint, "catchpoint"},
1042e4c0 5920 {bp_tracepoint, "tracepoint"},
7a697b8d 5921 {bp_fast_tracepoint, "fast tracepoint"},
0fb4aa4b 5922 {bp_static_tracepoint, "static tracepoint"},
e7e0cddf 5923 {bp_dprintf, "dprintf"},
4efc6507 5924 {bp_jit_event, "jit events"},
0e30163f
JK
5925 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
5926 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
c5aa993b 5927 };
269b11a2
PA
5928
5929 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
5930 || ((int) type != bptypes[(int) type].type))
5931 internal_error (__FILE__, __LINE__,
5932 _("bptypes table does not describe type #%d."),
5933 (int) type);
5934
5935 return bptypes[(int) type].description;
5936}
5937
998580f1
MK
5938/* For MI, output a field named 'thread-groups' with a list as the value.
5939 For CLI, prefix the list with the string 'inf'. */
5940
5941static void
5942output_thread_groups (struct ui_out *uiout,
5943 const char *field_name,
5c632425 5944 const std::vector<int> &inf_nums,
998580f1
MK
5945 int mi_only)
5946{
112e8700 5947 int is_mi = uiout->is_mi_like_p ();
998580f1
MK
5948
5949 /* For backward compatibility, don't display inferiors in CLI unless
5950 there are several. Always display them for MI. */
5951 if (!is_mi && mi_only)
5952 return;
5953
10f489e5 5954 ui_out_emit_list list_emitter (uiout, field_name);
752eb8b4 5955
5c632425 5956 for (size_t i = 0; i < inf_nums.size (); i++)
998580f1
MK
5957 {
5958 if (is_mi)
5959 {
5960 char mi_group[10];
5961
5c632425 5962 xsnprintf (mi_group, sizeof (mi_group), "i%d", inf_nums[i]);
112e8700 5963 uiout->field_string (NULL, mi_group);
998580f1
MK
5964 }
5965 else
5966 {
5967 if (i == 0)
112e8700 5968 uiout->text (" inf ");
998580f1 5969 else
112e8700 5970 uiout->text (", ");
998580f1 5971
5c632425 5972 uiout->text (plongest (inf_nums[i]));
998580f1
MK
5973 }
5974 }
998580f1
MK
5975}
5976
269b11a2
PA
5977/* Print B to gdb_stdout. */
5978
5979static void
5980print_one_breakpoint_location (struct breakpoint *b,
5981 struct bp_location *loc,
5982 int loc_number,
5983 struct bp_location **last_loc,
269b11a2
PA
5984 int allflag)
5985{
5986 struct command_line *l;
c2c6d25f 5987 static char bpenables[] = "nynny";
c906108c 5988
79a45e25 5989 struct ui_out *uiout = current_uiout;
0d381245
VP
5990 int header_of_multiple = 0;
5991 int part_of_multiple = (loc != NULL);
79a45b7d
TT
5992 struct value_print_options opts;
5993
5994 get_user_print_options (&opts);
0d381245
VP
5995
5996 gdb_assert (!loc || loc_number != 0);
4a64f543
MS
5997 /* See comment in print_one_breakpoint concerning treatment of
5998 breakpoints with single disabled location. */
0d381245
VP
5999 if (loc == NULL
6000 && (b->loc != NULL
6001 && (b->loc->next != NULL || !b->loc->enabled)))
6002 header_of_multiple = 1;
6003 if (loc == NULL)
6004 loc = b->loc;
6005
c4093a6a
JM
6006 annotate_record ();
6007
6008 /* 1 */
6009 annotate_field (0);
0d381245 6010 if (part_of_multiple)
528e1572 6011 uiout->field_fmt ("number", "%d.%d", b->number, loc_number);
0d381245 6012 else
528e1572 6013 uiout->field_int ("number", b->number);
c4093a6a
JM
6014
6015 /* 2 */
6016 annotate_field (1);
0d381245 6017 if (part_of_multiple)
112e8700 6018 uiout->field_skip ("type");
269b11a2 6019 else
112e8700 6020 uiout->field_string ("type", bptype_string (b->type));
c4093a6a
JM
6021
6022 /* 3 */
6023 annotate_field (2);
0d381245 6024 if (part_of_multiple)
112e8700 6025 uiout->field_skip ("disp");
0d381245 6026 else
112e8700 6027 uiout->field_string ("disp", bpdisp_text (b->disposition));
0d381245 6028
c4093a6a
JM
6029 /* 4 */
6030 annotate_field (3);
0d381245 6031 if (part_of_multiple)
112e8700 6032 uiout->field_string ("enabled", loc->enabled ? "y" : "n");
0d381245 6033 else
112e8700 6034 uiout->field_fmt ("enabled", "%c", bpenables[(int) b->enable_state]);
0d381245 6035
c4093a6a 6036 /* 5 and 6 */
3086aeae 6037 if (b->ops != NULL && b->ops->print_one != NULL)
b58a68fe 6038 b->ops->print_one (b, last_loc);
3086aeae
DJ
6039 else
6040 switch (b->type)
6041 {
6042 case bp_none:
6043 internal_error (__FILE__, __LINE__,
e2e0b3e5 6044 _("print_one_breakpoint: bp_none encountered\n"));
3086aeae 6045 break;
c906108c 6046
3086aeae
DJ
6047 case bp_watchpoint:
6048 case bp_hardware_watchpoint:
6049 case bp_read_watchpoint:
6050 case bp_access_watchpoint:
3a5c3e22
PA
6051 {
6052 struct watchpoint *w = (struct watchpoint *) b;
6053
6054 /* Field 4, the address, is omitted (which makes the columns
6055 not line up too nicely with the headers, but the effect
6056 is relatively readable). */
6057 if (opts.addressprint)
112e8700 6058 uiout->field_skip ("addr");
3a5c3e22 6059 annotate_field (5);
112e8700 6060 uiout->field_string ("what", w->exp_string);
3a5c3e22 6061 }
3086aeae
DJ
6062 break;
6063
3086aeae
DJ
6064 case bp_breakpoint:
6065 case bp_hardware_breakpoint:
7c16b83e 6066 case bp_single_step:
3086aeae
DJ
6067 case bp_until:
6068 case bp_finish:
6069 case bp_longjmp:
6070 case bp_longjmp_resume:
e2e4d78b 6071 case bp_longjmp_call_dummy:
186c406b
TT
6072 case bp_exception:
6073 case bp_exception_resume:
3086aeae 6074 case bp_step_resume:
2c03e5be 6075 case bp_hp_step_resume:
3086aeae
DJ
6076 case bp_watchpoint_scope:
6077 case bp_call_dummy:
aa7d318d 6078 case bp_std_terminate:
3086aeae
DJ
6079 case bp_shlib_event:
6080 case bp_thread_event:
6081 case bp_overlay_event:
0fd8e87f 6082 case bp_longjmp_master:
aa7d318d 6083 case bp_std_terminate_master:
186c406b 6084 case bp_exception_master:
1042e4c0 6085 case bp_tracepoint:
7a697b8d 6086 case bp_fast_tracepoint:
0fb4aa4b 6087 case bp_static_tracepoint:
e7e0cddf 6088 case bp_dprintf:
4efc6507 6089 case bp_jit_event:
0e30163f
JK
6090 case bp_gnu_ifunc_resolver:
6091 case bp_gnu_ifunc_resolver_return:
79a45b7d 6092 if (opts.addressprint)
3086aeae
DJ
6093 {
6094 annotate_field (4);
54e52265 6095 if (header_of_multiple)
112e8700 6096 uiout->field_string ("addr", "<MULTIPLE>");
e9bbd7c5 6097 else if (b->loc == NULL || loc->shlib_disabled)
112e8700 6098 uiout->field_string ("addr", "<PENDING>");
0101ce28 6099 else
112e8700 6100 uiout->field_core_addr ("addr",
5af949e3 6101 loc->gdbarch, loc->address);
3086aeae
DJ
6102 }
6103 annotate_field (5);
0d381245 6104 if (!header_of_multiple)
170b53b2 6105 print_breakpoint_location (b, loc);
0d381245 6106 if (b->loc)
a6d9a66e 6107 *last_loc = b->loc;
3086aeae
DJ
6108 break;
6109 }
c906108c 6110
6c95b8df 6111
998580f1 6112 if (loc != NULL && !header_of_multiple)
6c95b8df 6113 {
5c632425 6114 std::vector<int> inf_nums;
998580f1 6115 int mi_only = 1;
6c95b8df 6116
08036331 6117 for (inferior *inf : all_inferiors ())
6c95b8df
PA
6118 {
6119 if (inf->pspace == loc->pspace)
5c632425 6120 inf_nums.push_back (inf->num);
6c95b8df 6121 }
998580f1
MK
6122
6123 /* For backward compatibility, don't display inferiors in CLI unless
6124 there are several. Always display for MI. */
6125 if (allflag
6126 || (!gdbarch_has_global_breakpoints (target_gdbarch ())
6127 && (number_of_program_spaces () > 1
6128 || number_of_inferiors () > 1)
6129 /* LOC is for existing B, it cannot be in
6130 moribund_locations and thus having NULL OWNER. */
6131 && loc->owner->type != bp_catchpoint))
6132 mi_only = 0;
5c632425 6133 output_thread_groups (uiout, "thread-groups", inf_nums, mi_only);
6c95b8df
PA
6134 }
6135
4a306c9a 6136 if (!part_of_multiple)
c4093a6a 6137 {
4a306c9a
JB
6138 if (b->thread != -1)
6139 {
6140 /* FIXME: This seems to be redundant and lost here; see the
4a64f543 6141 "stop only in" line a little further down. */
112e8700
SM
6142 uiout->text (" thread ");
6143 uiout->field_int ("thread", b->thread);
4a306c9a
JB
6144 }
6145 else if (b->task != 0)
6146 {
112e8700
SM
6147 uiout->text (" task ");
6148 uiout->field_int ("task", b->task);
4a306c9a 6149 }
c4093a6a 6150 }
f1310107 6151
112e8700 6152 uiout->text ("\n");
f1310107 6153
348d480f 6154 if (!part_of_multiple)
f1310107
TJB
6155 b->ops->print_one_detail (b, uiout);
6156
0d381245 6157 if (part_of_multiple && frame_id_p (b->frame_id))
c4093a6a
JM
6158 {
6159 annotate_field (6);
112e8700 6160 uiout->text ("\tstop only in stack frame at ");
e5dd4106 6161 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
818dd999 6162 the frame ID. */
112e8700 6163 uiout->field_core_addr ("frame",
5af949e3 6164 b->gdbarch, b->frame_id.stack_addr);
112e8700 6165 uiout->text ("\n");
c4093a6a
JM
6166 }
6167
28010a5d 6168 if (!part_of_multiple && b->cond_string)
c4093a6a
JM
6169 {
6170 annotate_field (7);
d77f58be 6171 if (is_tracepoint (b))
112e8700 6172 uiout->text ("\ttrace only if ");
1042e4c0 6173 else
112e8700
SM
6174 uiout->text ("\tstop only if ");
6175 uiout->field_string ("cond", b->cond_string);
b775012e
LM
6176
6177 /* Print whether the target is doing the breakpoint's condition
6178 evaluation. If GDB is doing the evaluation, don't print anything. */
6179 if (is_breakpoint (b)
6180 && breakpoint_condition_evaluation_mode ()
6181 == condition_evaluation_target)
6182 {
112e8700
SM
6183 uiout->text (" (");
6184 uiout->field_string ("evaluated-by",
b775012e 6185 bp_condition_evaluator (b));
112e8700 6186 uiout->text (" evals)");
b775012e 6187 }
112e8700 6188 uiout->text ("\n");
0101ce28
JJ
6189 }
6190
0d381245 6191 if (!part_of_multiple && b->thread != -1)
c4093a6a 6192 {
4a64f543 6193 /* FIXME should make an annotation for this. */
112e8700
SM
6194 uiout->text ("\tstop only in thread ");
6195 if (uiout->is_mi_like_p ())
6196 uiout->field_int ("thread", b->thread);
5d5658a1
PA
6197 else
6198 {
6199 struct thread_info *thr = find_thread_global_id (b->thread);
6200
112e8700 6201 uiout->field_string ("thread", print_thread_id (thr));
5d5658a1 6202 }
112e8700 6203 uiout->text ("\n");
c4093a6a
JM
6204 }
6205
556ec64d
YQ
6206 if (!part_of_multiple)
6207 {
6208 if (b->hit_count)
31f56a27
YQ
6209 {
6210 /* FIXME should make an annotation for this. */
6211 if (is_catchpoint (b))
112e8700 6212 uiout->text ("\tcatchpoint");
31f56a27 6213 else if (is_tracepoint (b))
112e8700 6214 uiout->text ("\ttracepoint");
31f56a27 6215 else
112e8700
SM
6216 uiout->text ("\tbreakpoint");
6217 uiout->text (" already hit ");
6218 uiout->field_int ("times", b->hit_count);
31f56a27 6219 if (b->hit_count == 1)
112e8700 6220 uiout->text (" time\n");
31f56a27 6221 else
112e8700 6222 uiout->text (" times\n");
31f56a27 6223 }
556ec64d
YQ
6224 else
6225 {
31f56a27 6226 /* Output the count also if it is zero, but only if this is mi. */
112e8700
SM
6227 if (uiout->is_mi_like_p ())
6228 uiout->field_int ("times", b->hit_count);
556ec64d
YQ
6229 }
6230 }
8b93c638 6231
0d381245 6232 if (!part_of_multiple && b->ignore_count)
c4093a6a
JM
6233 {
6234 annotate_field (8);
112e8700
SM
6235 uiout->text ("\tignore next ");
6236 uiout->field_int ("ignore", b->ignore_count);
6237 uiout->text (" hits\n");
c4093a6a 6238 }
059fb39f 6239
816338b5
SS
6240 /* Note that an enable count of 1 corresponds to "enable once"
6241 behavior, which is reported by the combination of enablement and
6242 disposition, so we don't need to mention it here. */
6243 if (!part_of_multiple && b->enable_count > 1)
6244 {
6245 annotate_field (8);
112e8700 6246 uiout->text ("\tdisable after ");
816338b5
SS
6247 /* Tweak the wording to clarify that ignore and enable counts
6248 are distinct, and have additive effect. */
6249 if (b->ignore_count)
112e8700 6250 uiout->text ("additional ");
816338b5 6251 else
112e8700
SM
6252 uiout->text ("next ");
6253 uiout->field_int ("enable", b->enable_count);
6254 uiout->text (" hits\n");
816338b5
SS
6255 }
6256
f196051f
SS
6257 if (!part_of_multiple && is_tracepoint (b))
6258 {
6259 struct tracepoint *tp = (struct tracepoint *) b;
6260
6261 if (tp->traceframe_usage)
6262 {
112e8700
SM
6263 uiout->text ("\ttrace buffer usage ");
6264 uiout->field_int ("traceframe-usage", tp->traceframe_usage);
6265 uiout->text (" bytes\n");
f196051f
SS
6266 }
6267 }
d3ce09f5 6268
d1b0a7bf 6269 l = b->commands ? b->commands.get () : NULL;
059fb39f 6270 if (!part_of_multiple && l)
c4093a6a
JM
6271 {
6272 annotate_field (9);
2e783024 6273 ui_out_emit_tuple tuple_emitter (uiout, "script");
8b93c638 6274 print_command_lines (uiout, l, 4);
c4093a6a 6275 }
d24317b4 6276
d9b3f62e 6277 if (is_tracepoint (b))
1042e4c0 6278 {
d9b3f62e
PA
6279 struct tracepoint *t = (struct tracepoint *) b;
6280
6281 if (!part_of_multiple && t->pass_count)
6282 {
6283 annotate_field (10);
112e8700
SM
6284 uiout->text ("\tpass count ");
6285 uiout->field_int ("pass", t->pass_count);
6286 uiout->text (" \n");
d9b3f62e 6287 }
f2a8bc8a
YQ
6288
6289 /* Don't display it when tracepoint or tracepoint location is
6290 pending. */
6291 if (!header_of_multiple && loc != NULL && !loc->shlib_disabled)
6292 {
6293 annotate_field (11);
6294
112e8700
SM
6295 if (uiout->is_mi_like_p ())
6296 uiout->field_string ("installed",
f2a8bc8a
YQ
6297 loc->inserted ? "y" : "n");
6298 else
6299 {
6300 if (loc->inserted)
112e8700 6301 uiout->text ("\t");
f2a8bc8a 6302 else
112e8700
SM
6303 uiout->text ("\tnot ");
6304 uiout->text ("installed on target\n");
f2a8bc8a
YQ
6305 }
6306 }
1042e4c0
SS
6307 }
6308
112e8700 6309 if (uiout->is_mi_like_p () && !part_of_multiple)
d24317b4 6310 {
3a5c3e22
PA
6311 if (is_watchpoint (b))
6312 {
6313 struct watchpoint *w = (struct watchpoint *) b;
6314
112e8700 6315 uiout->field_string ("original-location", w->exp_string);
3a5c3e22 6316 }
f00aae0f 6317 else if (b->location != NULL
d28cd78a 6318 && event_location_to_string (b->location.get ()) != NULL)
112e8700 6319 uiout->field_string ("original-location",
d28cd78a 6320 event_location_to_string (b->location.get ()));
d24317b4 6321 }
c4093a6a 6322}
c5aa993b 6323
13674803
SM
6324/* See breakpoint.h. */
6325
6326bool fix_multi_location_breakpoint_output_globally = false;
6327
0d381245
VP
6328static void
6329print_one_breakpoint (struct breakpoint *b,
4a64f543 6330 struct bp_location **last_loc,
6c95b8df 6331 int allflag)
0d381245 6332{
79a45e25 6333 struct ui_out *uiout = current_uiout;
13674803
SM
6334 bool use_fixed_output
6335 = (uiout->test_flags (fix_multi_location_breakpoint_output)
6336 || fix_multi_location_breakpoint_output_globally);
8d3788bd 6337
b4be1b06
SM
6338 gdb::optional<ui_out_emit_tuple> bkpt_tuple_emitter (gdb::in_place, uiout, "bkpt");
6339 print_one_breakpoint_location (b, NULL, 0, last_loc, allflag);
8d3788bd 6340
b4be1b06
SM
6341 /* The mi2 broken format: the main breakpoint tuple ends here, the locations
6342 are outside. */
6343 if (!use_fixed_output)
6344 bkpt_tuple_emitter.reset ();
0d381245
VP
6345
6346 /* If this breakpoint has custom print function,
6347 it's already printed. Otherwise, print individual
6348 locations, if any. */
6349 if (b->ops == NULL || b->ops->print_one == NULL)
6350 {
4a64f543
MS
6351 /* If breakpoint has a single location that is disabled, we
6352 print it as if it had several locations, since otherwise it's
6353 hard to represent "breakpoint enabled, location disabled"
6354 situation.
6355
6356 Note that while hardware watchpoints have several locations
a3be7890 6357 internally, that's not a property exposed to user. */
0d381245 6358 if (b->loc
a5606eee 6359 && !is_hardware_watchpoint (b)
8d3788bd 6360 && (b->loc->next || !b->loc->enabled))
0d381245 6361 {
b4be1b06
SM
6362 gdb::optional<ui_out_emit_list> locations_list;
6363
6364 /* For MI version <= 2, keep the behavior where GDB outputs an invalid
6365 MI record. For later versions, place breakpoint locations in a
6366 list. */
6367 if (uiout->is_mi_like_p () && use_fixed_output)
6368 locations_list.emplace (uiout, "locations");
8d3788bd 6369
b4be1b06
SM
6370 int n = 1;
6371 for (bp_location *loc = b->loc; loc != NULL; loc = loc->next, ++n)
8d3788bd 6372 {
b4be1b06 6373 ui_out_emit_tuple loc_tuple_emitter (uiout, NULL);
8d3788bd 6374 print_one_breakpoint_location (b, loc, n, last_loc, allflag);
8d3788bd 6375 }
0d381245
VP
6376 }
6377 }
6378}
6379
a6d9a66e
UW
6380static int
6381breakpoint_address_bits (struct breakpoint *b)
6382{
6383 int print_address_bits = 0;
6384 struct bp_location *loc;
6385
c6d81124
PA
6386 /* Software watchpoints that aren't watching memory don't have an
6387 address to print. */
6388 if (is_no_memory_software_watchpoint (b))
6389 return 0;
6390
a6d9a66e
UW
6391 for (loc = b->loc; loc; loc = loc->next)
6392 {
c7437ca6
PA
6393 int addr_bit;
6394
c7437ca6 6395 addr_bit = gdbarch_addr_bit (loc->gdbarch);
a6d9a66e
UW
6396 if (addr_bit > print_address_bits)
6397 print_address_bits = addr_bit;
6398 }
6399
6400 return print_address_bits;
6401}
0d381245 6402
65630365 6403/* See breakpoint.h. */
c5aa993b 6404
65630365
PA
6405void
6406print_breakpoint (breakpoint *b)
c4093a6a 6407{
a6d9a66e 6408 struct bp_location *dummy_loc = NULL;
65630365 6409 print_one_breakpoint (b, &dummy_loc, 0);
c4093a6a 6410}
c5aa993b 6411
09d682a4
TT
6412/* Return true if this breakpoint was set by the user, false if it is
6413 internal or momentary. */
6414
6415int
6416user_breakpoint_p (struct breakpoint *b)
6417{
46c6471b 6418 return b->number > 0;
09d682a4
TT
6419}
6420
93daf339
TT
6421/* See breakpoint.h. */
6422
6423int
6424pending_breakpoint_p (struct breakpoint *b)
6425{
6426 return b->loc == NULL;
6427}
6428
7f3b0473 6429/* Print information on user settable breakpoint (watchpoint, etc)
d77f58be
SS
6430 number BNUM. If BNUM is -1 print all user-settable breakpoints.
6431 If ALLFLAG is non-zero, include non-user-settable breakpoints. If
6432 FILTER is non-NULL, call it on each breakpoint and only include the
6433 ones for which it returns non-zero. Return the total number of
6434 breakpoints listed. */
c906108c 6435
d77f58be 6436static int
4495129a 6437breakpoint_1 (const char *args, int allflag,
4a64f543 6438 int (*filter) (const struct breakpoint *))
c4093a6a 6439{
52f0bd74 6440 struct breakpoint *b;
a6d9a66e 6441 struct bp_location *last_loc = NULL;
7f3b0473 6442 int nr_printable_breakpoints;
79a45b7d 6443 struct value_print_options opts;
a6d9a66e 6444 int print_address_bits = 0;
269b11a2 6445 int print_type_col_width = 14;
79a45e25 6446 struct ui_out *uiout = current_uiout;
269b11a2 6447
79a45b7d
TT
6448 get_user_print_options (&opts);
6449
4a64f543
MS
6450 /* Compute the number of rows in the table, as well as the size
6451 required for address fields. */
7f3b0473
AC
6452 nr_printable_breakpoints = 0;
6453 ALL_BREAKPOINTS (b)
e5a67952
MS
6454 {
6455 /* If we have a filter, only list the breakpoints it accepts. */
6456 if (filter && !filter (b))
6457 continue;
6458
6459 /* If we have an "args" string, it is a list of breakpoints to
6460 accept. Skip the others. */
6461 if (args != NULL && *args != '\0')
6462 {
6463 if (allflag && parse_and_eval_long (args) != b->number)
6464 continue;
6465 if (!allflag && !number_is_in_list (args, b->number))
6466 continue;
6467 }
269b11a2 6468
e5a67952
MS
6469 if (allflag || user_breakpoint_p (b))
6470 {
6471 int addr_bit, type_len;
a6d9a66e 6472
e5a67952
MS
6473 addr_bit = breakpoint_address_bits (b);
6474 if (addr_bit > print_address_bits)
6475 print_address_bits = addr_bit;
269b11a2 6476
e5a67952
MS
6477 type_len = strlen (bptype_string (b->type));
6478 if (type_len > print_type_col_width)
6479 print_type_col_width = type_len;
6480
6481 nr_printable_breakpoints++;
6482 }
6483 }
7f3b0473 6484
4a2b031d
TT
6485 {
6486 ui_out_emit_table table_emitter (uiout,
6487 opts.addressprint ? 6 : 5,
6488 nr_printable_breakpoints,
6489 "BreakpointTable");
6490
6491 if (nr_printable_breakpoints > 0)
6492 annotate_breakpoints_headers ();
6493 if (nr_printable_breakpoints > 0)
6494 annotate_field (0);
6495 uiout->table_header (7, ui_left, "number", "Num"); /* 1 */
6496 if (nr_printable_breakpoints > 0)
6497 annotate_field (1);
6498 uiout->table_header (print_type_col_width, ui_left, "type", "Type"); /* 2 */
6499 if (nr_printable_breakpoints > 0)
6500 annotate_field (2);
6501 uiout->table_header (4, ui_left, "disp", "Disp"); /* 3 */
6502 if (nr_printable_breakpoints > 0)
6503 annotate_field (3);
6504 uiout->table_header (3, ui_left, "enabled", "Enb"); /* 4 */
6505 if (opts.addressprint)
6506 {
6507 if (nr_printable_breakpoints > 0)
6508 annotate_field (4);
6509 if (print_address_bits <= 32)
6510 uiout->table_header (10, ui_left, "addr", "Address"); /* 5 */
6511 else
6512 uiout->table_header (18, ui_left, "addr", "Address"); /* 5 */
6513 }
6514 if (nr_printable_breakpoints > 0)
6515 annotate_field (5);
6516 uiout->table_header (40, ui_noalign, "what", "What"); /* 6 */
6517 uiout->table_body ();
6518 if (nr_printable_breakpoints > 0)
6519 annotate_breakpoints_table ();
6520
6521 ALL_BREAKPOINTS (b)
6522 {
6523 QUIT;
6524 /* If we have a filter, only list the breakpoints it accepts. */
6525 if (filter && !filter (b))
6526 continue;
e5a67952 6527
4a2b031d
TT
6528 /* If we have an "args" string, it is a list of breakpoints to
6529 accept. Skip the others. */
e5a67952 6530
4a2b031d
TT
6531 if (args != NULL && *args != '\0')
6532 {
6533 if (allflag) /* maintenance info breakpoint */
6534 {
6535 if (parse_and_eval_long (args) != b->number)
6536 continue;
6537 }
6538 else /* all others */
6539 {
6540 if (!number_is_in_list (args, b->number))
6541 continue;
6542 }
6543 }
6544 /* We only print out user settable breakpoints unless the
6545 allflag is set. */
6546 if (allflag || user_breakpoint_p (b))
6547 print_one_breakpoint (b, &last_loc, allflag);
6548 }
6549 }
698384cd 6550
7f3b0473 6551 if (nr_printable_breakpoints == 0)
c906108c 6552 {
4a64f543
MS
6553 /* If there's a filter, let the caller decide how to report
6554 empty list. */
d77f58be
SS
6555 if (!filter)
6556 {
e5a67952 6557 if (args == NULL || *args == '\0')
112e8700 6558 uiout->message ("No breakpoints or watchpoints.\n");
d77f58be 6559 else
112e8700 6560 uiout->message ("No breakpoint or watchpoint matching '%s'.\n",
e5a67952 6561 args);
d77f58be 6562 }
c906108c
SS
6563 }
6564 else
c4093a6a 6565 {
a6d9a66e
UW
6566 if (last_loc && !server_command)
6567 set_next_address (last_loc->gdbarch, last_loc->address);
c4093a6a 6568 }
c906108c 6569
4a64f543 6570 /* FIXME? Should this be moved up so that it is only called when
c4093a6a 6571 there have been breakpoints? */
c906108c 6572 annotate_breakpoints_table_end ();
d77f58be
SS
6573
6574 return nr_printable_breakpoints;
c906108c
SS
6575}
6576
ad443146
SS
6577/* Display the value of default-collect in a way that is generally
6578 compatible with the breakpoint list. */
6579
6580static void
6581default_collect_info (void)
6582{
79a45e25
PA
6583 struct ui_out *uiout = current_uiout;
6584
ad443146
SS
6585 /* If it has no value (which is frequently the case), say nothing; a
6586 message like "No default-collect." gets in user's face when it's
6587 not wanted. */
6588 if (!*default_collect)
6589 return;
6590
6591 /* The following phrase lines up nicely with per-tracepoint collect
6592 actions. */
112e8700
SM
6593 uiout->text ("default collect ");
6594 uiout->field_string ("default-collect", default_collect);
6595 uiout->text (" \n");
ad443146
SS
6596}
6597
c906108c 6598static void
0b39b52e 6599info_breakpoints_command (const char *args, int from_tty)
c906108c 6600{
e5a67952 6601 breakpoint_1 (args, 0, NULL);
ad443146
SS
6602
6603 default_collect_info ();
d77f58be
SS
6604}
6605
6606static void
1d12d88f 6607info_watchpoints_command (const char *args, int from_tty)
d77f58be 6608{
e5a67952 6609 int num_printed = breakpoint_1 (args, 0, is_watchpoint);
79a45e25 6610 struct ui_out *uiout = current_uiout;
d77f58be
SS
6611
6612 if (num_printed == 0)
6613 {
e5a67952 6614 if (args == NULL || *args == '\0')
112e8700 6615 uiout->message ("No watchpoints.\n");
d77f58be 6616 else
112e8700 6617 uiout->message ("No watchpoint matching '%s'.\n", args);
d77f58be 6618 }
c906108c
SS
6619}
6620
7a292a7a 6621static void
4495129a 6622maintenance_info_breakpoints (const char *args, int from_tty)
c906108c 6623{
e5a67952 6624 breakpoint_1 (args, 1, NULL);
ad443146
SS
6625
6626 default_collect_info ();
c906108c
SS
6627}
6628
0d381245 6629static int
714835d5 6630breakpoint_has_pc (struct breakpoint *b,
6c95b8df 6631 struct program_space *pspace,
714835d5 6632 CORE_ADDR pc, struct obj_section *section)
0d381245
VP
6633{
6634 struct bp_location *bl = b->loc;
cc59ec59 6635
0d381245
VP
6636 for (; bl; bl = bl->next)
6637 {
6c95b8df
PA
6638 if (bl->pspace == pspace
6639 && bl->address == pc
0d381245
VP
6640 && (!overlay_debugging || bl->section == section))
6641 return 1;
6642 }
6643 return 0;
6644}
6645
672f9b60 6646/* Print a message describing any user-breakpoints set at PC. This
6c95b8df
PA
6647 concerns with logical breakpoints, so we match program spaces, not
6648 address spaces. */
c906108c
SS
6649
6650static void
6c95b8df
PA
6651describe_other_breakpoints (struct gdbarch *gdbarch,
6652 struct program_space *pspace, CORE_ADDR pc,
5af949e3 6653 struct obj_section *section, int thread)
c906108c 6654{
52f0bd74
AC
6655 int others = 0;
6656 struct breakpoint *b;
c906108c
SS
6657
6658 ALL_BREAKPOINTS (b)
672f9b60
KP
6659 others += (user_breakpoint_p (b)
6660 && breakpoint_has_pc (b, pspace, pc, section));
c906108c
SS
6661 if (others > 0)
6662 {
a3f17187
AC
6663 if (others == 1)
6664 printf_filtered (_("Note: breakpoint "));
6665 else /* if (others == ???) */
6666 printf_filtered (_("Note: breakpoints "));
c906108c 6667 ALL_BREAKPOINTS (b)
672f9b60 6668 if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
0d381245
VP
6669 {
6670 others--;
6671 printf_filtered ("%d", b->number);
6672 if (b->thread == -1 && thread != -1)
6673 printf_filtered (" (all threads)");
6674 else if (b->thread != -1)
6675 printf_filtered (" (thread %d)", b->thread);
6676 printf_filtered ("%s%s ",
059fb39f 6677 ((b->enable_state == bp_disabled
f8eba3c6 6678 || b->enable_state == bp_call_disabled)
0d381245 6679 ? " (disabled)"
0d381245
VP
6680 : ""),
6681 (others > 1) ? ","
6682 : ((others == 1) ? " and" : ""));
6683 }
a3f17187 6684 printf_filtered (_("also set at pc "));
2636d81d 6685 fputs_styled (paddress (gdbarch, pc), address_style.style (), gdb_stdout);
c906108c
SS
6686 printf_filtered (".\n");
6687 }
6688}
6689\f
c906108c 6690
cb1e4e32
PA
6691/* Return true iff it is meaningful to use the address member of LOC.
6692 For some breakpoint types, the locations' address members are
6693 irrelevant and it makes no sense to attempt to compare them to
6694 other addresses (or use them for any other purpose either).
2d134ed3 6695
cb1e4e32
PA
6696 More specifically, software watchpoints and catchpoints that are
6697 not backed by breakpoints always have a zero valued location
6698 address and we don't want to mark breakpoints of any of these types
6699 to be a duplicate of an actual breakpoint location at address
6700 zero. */
e4f237da 6701
cb1e4e32
PA
6702static bool
6703bl_address_is_meaningful (bp_location *loc)
e4f237da 6704{
cb1e4e32 6705 return loc->loc_type != bp_loc_other;
2d134ed3
PA
6706}
6707
6708/* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
6709 true if LOC1 and LOC2 represent the same watchpoint location. */
6710
6711static int
4a64f543
MS
6712watchpoint_locations_match (struct bp_location *loc1,
6713 struct bp_location *loc2)
2d134ed3 6714{
3a5c3e22
PA
6715 struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
6716 struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
6717
6718 /* Both of them must exist. */
6719 gdb_assert (w1 != NULL);
6720 gdb_assert (w2 != NULL);
2bdf28a0 6721
4a64f543
MS
6722 /* If the target can evaluate the condition expression in hardware,
6723 then we we need to insert both watchpoints even if they are at
6724 the same place. Otherwise the watchpoint will only trigger when
6725 the condition of whichever watchpoint was inserted evaluates to
6726 true, not giving a chance for GDB to check the condition of the
6727 other watchpoint. */
3a5c3e22 6728 if ((w1->cond_exp
4a64f543
MS
6729 && target_can_accel_watchpoint_condition (loc1->address,
6730 loc1->length,
0cf6dd15 6731 loc1->watchpoint_type,
4d01a485 6732 w1->cond_exp.get ()))
3a5c3e22 6733 || (w2->cond_exp
4a64f543
MS
6734 && target_can_accel_watchpoint_condition (loc2->address,
6735 loc2->length,
0cf6dd15 6736 loc2->watchpoint_type,
4d01a485 6737 w2->cond_exp.get ())))
0cf6dd15
TJB
6738 return 0;
6739
85d721b8
PA
6740 /* Note that this checks the owner's type, not the location's. In
6741 case the target does not support read watchpoints, but does
6742 support access watchpoints, we'll have bp_read_watchpoint
6743 watchpoints with hw_access locations. Those should be considered
6744 duplicates of hw_read locations. The hw_read locations will
6745 become hw_access locations later. */
2d134ed3
PA
6746 return (loc1->owner->type == loc2->owner->type
6747 && loc1->pspace->aspace == loc2->pspace->aspace
6748 && loc1->address == loc2->address
6749 && loc1->length == loc2->length);
e4f237da
KB
6750}
6751
31e77af2 6752/* See breakpoint.h. */
6c95b8df 6753
31e77af2 6754int
accd0bcd
YQ
6755breakpoint_address_match (const address_space *aspace1, CORE_ADDR addr1,
6756 const address_space *aspace2, CORE_ADDR addr2)
6c95b8df 6757{
f5656ead 6758 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
6c95b8df
PA
6759 || aspace1 == aspace2)
6760 && addr1 == addr2);
6761}
6762
f1310107
TJB
6763/* Returns true if {ASPACE2,ADDR2} falls within the range determined by
6764 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
6765 matches ASPACE2. On targets that have global breakpoints, the address
6766 space doesn't really matter. */
6767
6768static int
accd0bcd
YQ
6769breakpoint_address_match_range (const address_space *aspace1,
6770 CORE_ADDR addr1,
6771 int len1, const address_space *aspace2,
f1310107
TJB
6772 CORE_ADDR addr2)
6773{
f5656ead 6774 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
f1310107
TJB
6775 || aspace1 == aspace2)
6776 && addr2 >= addr1 && addr2 < addr1 + len1);
6777}
6778
6779/* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
6780 a ranged breakpoint. In most targets, a match happens only if ASPACE
6781 matches the breakpoint's address space. On targets that have global
6782 breakpoints, the address space doesn't really matter. */
6783
6784static int
6785breakpoint_location_address_match (struct bp_location *bl,
accd0bcd 6786 const address_space *aspace,
f1310107
TJB
6787 CORE_ADDR addr)
6788{
6789 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
6790 aspace, addr)
6791 || (bl->length
6792 && breakpoint_address_match_range (bl->pspace->aspace,
6793 bl->address, bl->length,
6794 aspace, addr)));
6795}
6796
d35ae833
PA
6797/* Returns true if the [ADDR,ADDR+LEN) range in ASPACE overlaps
6798 breakpoint BL. BL may be a ranged breakpoint. In most targets, a
6799 match happens only if ASPACE matches the breakpoint's address
6800 space. On targets that have global breakpoints, the address space
6801 doesn't really matter. */
6802
6803static int
6804breakpoint_location_address_range_overlap (struct bp_location *bl,
accd0bcd 6805 const address_space *aspace,
d35ae833
PA
6806 CORE_ADDR addr, int len)
6807{
6808 if (gdbarch_has_global_breakpoints (target_gdbarch ())
6809 || bl->pspace->aspace == aspace)
6810 {
6811 int bl_len = bl->length != 0 ? bl->length : 1;
6812
6813 if (mem_ranges_overlap (addr, len, bl->address, bl_len))
6814 return 1;
6815 }
6816 return 0;
6817}
6818
1e4d1764
YQ
6819/* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
6820 Then, if LOC1 and LOC2 represent the same tracepoint location, returns
6821 true, otherwise returns false. */
6822
6823static int
6824tracepoint_locations_match (struct bp_location *loc1,
6825 struct bp_location *loc2)
6826{
6827 if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
6828 /* Since tracepoint locations are never duplicated with others', tracepoint
6829 locations at the same address of different tracepoints are regarded as
6830 different locations. */
6831 return (loc1->address == loc2->address && loc1->owner == loc2->owner);
6832 else
6833 return 0;
6834}
6835
2d134ed3 6836/* Assuming LOC1 and LOC2's types' have meaningful target addresses
cb1e4e32
PA
6837 (bl_address_is_meaningful), returns true if LOC1 and LOC2 represent
6838 the same location. */
2d134ed3
PA
6839
6840static int
4a64f543
MS
6841breakpoint_locations_match (struct bp_location *loc1,
6842 struct bp_location *loc2)
2d134ed3 6843{
2bdf28a0
JK
6844 int hw_point1, hw_point2;
6845
6846 /* Both of them must not be in moribund_locations. */
6847 gdb_assert (loc1->owner != NULL);
6848 gdb_assert (loc2->owner != NULL);
6849
6850 hw_point1 = is_hardware_watchpoint (loc1->owner);
6851 hw_point2 = is_hardware_watchpoint (loc2->owner);
2d134ed3
PA
6852
6853 if (hw_point1 != hw_point2)
6854 return 0;
6855 else if (hw_point1)
6856 return watchpoint_locations_match (loc1, loc2);
1e4d1764
YQ
6857 else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
6858 return tracepoint_locations_match (loc1, loc2);
2d134ed3 6859 else
f1310107
TJB
6860 /* We compare bp_location.length in order to cover ranged breakpoints. */
6861 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
6862 loc2->pspace->aspace, loc2->address)
6863 && loc1->length == loc2->length);
2d134ed3
PA
6864}
6865
76897487
KB
6866static void
6867breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
6868 int bnum, int have_bnum)
6869{
f63fbe86
MS
6870 /* The longest string possibly returned by hex_string_custom
6871 is 50 chars. These must be at least that big for safety. */
6872 char astr1[64];
6873 char astr2[64];
76897487 6874
bb599908
PH
6875 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
6876 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
76897487 6877 if (have_bnum)
8a3fe4f8 6878 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
76897487
KB
6879 bnum, astr1, astr2);
6880 else
8a3fe4f8 6881 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
76897487
KB
6882}
6883
4a64f543
MS
6884/* Adjust a breakpoint's address to account for architectural
6885 constraints on breakpoint placement. Return the adjusted address.
6886 Note: Very few targets require this kind of adjustment. For most
6887 targets, this function is simply the identity function. */
76897487
KB
6888
6889static CORE_ADDR
a6d9a66e
UW
6890adjust_breakpoint_address (struct gdbarch *gdbarch,
6891 CORE_ADDR bpaddr, enum bptype bptype)
76897487 6892{
a0de8c21
YQ
6893 if (bptype == bp_watchpoint
6894 || bptype == bp_hardware_watchpoint
6895 || bptype == bp_read_watchpoint
6896 || bptype == bp_access_watchpoint
6897 || bptype == bp_catchpoint)
88f7da05
KB
6898 {
6899 /* Watchpoints and the various bp_catch_* eventpoints should not
6900 have their addresses modified. */
6901 return bpaddr;
6902 }
7c16b83e
PA
6903 else if (bptype == bp_single_step)
6904 {
6905 /* Single-step breakpoints should not have their addresses
6906 modified. If there's any architectural constrain that
6907 applies to this address, then it should have already been
6908 taken into account when the breakpoint was created in the
6909 first place. If we didn't do this, stepping through e.g.,
6910 Thumb-2 IT blocks would break. */
6911 return bpaddr;
6912 }
76897487
KB
6913 else
6914 {
a0de8c21
YQ
6915 CORE_ADDR adjusted_bpaddr = bpaddr;
6916
6917 if (gdbarch_adjust_breakpoint_address_p (gdbarch))
6918 {
6919 /* Some targets have architectural constraints on the placement
6920 of breakpoint instructions. Obtain the adjusted address. */
6921 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
6922 }
76897487 6923
a0de8c21 6924 adjusted_bpaddr = address_significant (gdbarch, adjusted_bpaddr);
76897487
KB
6925
6926 /* An adjusted breakpoint address can significantly alter
6927 a user's expectations. Print a warning if an adjustment
6928 is required. */
6929 if (adjusted_bpaddr != bpaddr)
6930 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
6931
6932 return adjusted_bpaddr;
6933 }
6934}
6935
cb1e4e32
PA
6936static bp_loc_type
6937bp_location_from_bp_type (bptype type)
7cc221ef 6938{
cb1e4e32 6939 switch (type)
e049a4b5
DJ
6940 {
6941 case bp_breakpoint:
7c16b83e 6942 case bp_single_step:
e049a4b5
DJ
6943 case bp_until:
6944 case bp_finish:
6945 case bp_longjmp:
6946 case bp_longjmp_resume:
e2e4d78b 6947 case bp_longjmp_call_dummy:
186c406b
TT
6948 case bp_exception:
6949 case bp_exception_resume:
e049a4b5 6950 case bp_step_resume:
2c03e5be 6951 case bp_hp_step_resume:
e049a4b5
DJ
6952 case bp_watchpoint_scope:
6953 case bp_call_dummy:
aa7d318d 6954 case bp_std_terminate:
e049a4b5
DJ
6955 case bp_shlib_event:
6956 case bp_thread_event:
6957 case bp_overlay_event:
4efc6507 6958 case bp_jit_event:
0fd8e87f 6959 case bp_longjmp_master:
aa7d318d 6960 case bp_std_terminate_master:
186c406b 6961 case bp_exception_master:
0e30163f
JK
6962 case bp_gnu_ifunc_resolver:
6963 case bp_gnu_ifunc_resolver_return:
e7e0cddf 6964 case bp_dprintf:
cb1e4e32 6965 return bp_loc_software_breakpoint;
e049a4b5 6966 case bp_hardware_breakpoint:
cb1e4e32 6967 return bp_loc_hardware_breakpoint;
e049a4b5
DJ
6968 case bp_hardware_watchpoint:
6969 case bp_read_watchpoint:
6970 case bp_access_watchpoint:
cb1e4e32 6971 return bp_loc_hardware_watchpoint;
e049a4b5 6972 case bp_watchpoint:
ce78b96d 6973 case bp_catchpoint:
15c3d785
PA
6974 case bp_tracepoint:
6975 case bp_fast_tracepoint:
0fb4aa4b 6976 case bp_static_tracepoint:
cb1e4e32 6977 return bp_loc_other;
e049a4b5 6978 default:
e2e0b3e5 6979 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
e049a4b5 6980 }
cb1e4e32
PA
6981}
6982
6983bp_location::bp_location (breakpoint *owner, bp_loc_type type)
6984{
6985 this->owner = owner;
6986 this->cond_bytecode = NULL;
6987 this->shlib_disabled = 0;
6988 this->enabled = 1;
6989
6990 this->loc_type = type;
e049a4b5 6991
cb1e4e32
PA
6992 if (this->loc_type == bp_loc_software_breakpoint
6993 || this->loc_type == bp_loc_hardware_breakpoint)
6994 mark_breakpoint_location_modified (this);
6995
6996 this->refc = 1;
6997}
6998
6999bp_location::bp_location (breakpoint *owner)
7000 : bp_location::bp_location (owner,
7001 bp_location_from_bp_type (owner->type))
7002{
28010a5d
PA
7003}
7004
7005/* Allocate a struct bp_location. */
7006
7007static struct bp_location *
7008allocate_bp_location (struct breakpoint *bpt)
7009{
348d480f
PA
7010 return bpt->ops->allocate_location (bpt);
7011}
7cc221ef 7012
f431efe5
PA
7013static void
7014free_bp_location (struct bp_location *loc)
fe3f5fa8 7015{
4d01a485 7016 delete loc;
fe3f5fa8
VP
7017}
7018
f431efe5
PA
7019/* Increment reference count. */
7020
7021static void
7022incref_bp_location (struct bp_location *bl)
7023{
7024 ++bl->refc;
7025}
7026
7027/* Decrement reference count. If the reference count reaches 0,
7028 destroy the bp_location. Sets *BLP to NULL. */
7029
7030static void
7031decref_bp_location (struct bp_location **blp)
7032{
0807b50c
PA
7033 gdb_assert ((*blp)->refc > 0);
7034
f431efe5
PA
7035 if (--(*blp)->refc == 0)
7036 free_bp_location (*blp);
7037 *blp = NULL;
7038}
7039
346774a9 7040/* Add breakpoint B at the end of the global breakpoint chain. */
c906108c 7041
b270e6f9
TT
7042static breakpoint *
7043add_to_breakpoint_chain (std::unique_ptr<breakpoint> &&b)
c906108c 7044{
346774a9 7045 struct breakpoint *b1;
b270e6f9 7046 struct breakpoint *result = b.get ();
c906108c 7047
346774a9
PA
7048 /* Add this breakpoint to the end of the chain so that a list of
7049 breakpoints will come out in order of increasing numbers. */
7050
7051 b1 = breakpoint_chain;
7052 if (b1 == 0)
b270e6f9 7053 breakpoint_chain = b.release ();
346774a9
PA
7054 else
7055 {
7056 while (b1->next)
7057 b1 = b1->next;
b270e6f9 7058 b1->next = b.release ();
346774a9 7059 }
b270e6f9
TT
7060
7061 return result;
346774a9
PA
7062}
7063
7064/* Initializes breakpoint B with type BPTYPE and no locations yet. */
7065
7066static void
7067init_raw_breakpoint_without_location (struct breakpoint *b,
7068 struct gdbarch *gdbarch,
28010a5d 7069 enum bptype bptype,
c0a91b2b 7070 const struct breakpoint_ops *ops)
346774a9 7071{
348d480f
PA
7072 gdb_assert (ops != NULL);
7073
28010a5d 7074 b->ops = ops;
4d28f7a8 7075 b->type = bptype;
a6d9a66e 7076 b->gdbarch = gdbarch;
c906108c
SS
7077 b->language = current_language->la_language;
7078 b->input_radix = input_radix;
d0fb5eae 7079 b->related_breakpoint = b;
346774a9
PA
7080}
7081
7082/* Helper to set_raw_breakpoint below. Creates a breakpoint
7083 that has type BPTYPE and has no locations as yet. */
346774a9
PA
7084
7085static struct breakpoint *
7086set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
348d480f 7087 enum bptype bptype,
c0a91b2b 7088 const struct breakpoint_ops *ops)
346774a9 7089{
3b0871f4 7090 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (bptype);
346774a9 7091
3b0871f4 7092 init_raw_breakpoint_without_location (b.get (), gdbarch, bptype, ops);
b270e6f9 7093 return add_to_breakpoint_chain (std::move (b));
0d381245
VP
7094}
7095
0ba852ab 7096/* Initialize loc->function_name. */
0e30163f 7097
0d381245 7098static void
0ba852ab 7099set_breakpoint_location_function (struct bp_location *loc)
0d381245 7100{
2bdf28a0
JK
7101 gdb_assert (loc->owner != NULL);
7102
0d381245 7103 if (loc->owner->type == bp_breakpoint
1042e4c0 7104 || loc->owner->type == bp_hardware_breakpoint
d77f58be 7105 || is_tracepoint (loc->owner))
0d381245 7106 {
2c02bd72 7107 const char *function_name;
0e30163f 7108
3467ec66 7109 if (loc->msymbol != NULL
f50776aa 7110 && (MSYMBOL_TYPE (loc->msymbol) == mst_text_gnu_ifunc
0ba852ab 7111 || MSYMBOL_TYPE (loc->msymbol) == mst_data_gnu_ifunc))
0e30163f
JK
7112 {
7113 struct breakpoint *b = loc->owner;
7114
3467ec66
PA
7115 function_name = MSYMBOL_LINKAGE_NAME (loc->msymbol);
7116
7117 if (b->type == bp_breakpoint && b->loc == loc
7118 && loc->next == NULL && b->related_breakpoint == b)
0e30163f
JK
7119 {
7120 /* Create only the whole new breakpoint of this type but do not
7121 mess more complicated breakpoints with multiple locations. */
7122 b->type = bp_gnu_ifunc_resolver;
6a3a010b
MR
7123 /* Remember the resolver's address for use by the return
7124 breakpoint. */
3467ec66 7125 loc->related_address = loc->address;
0e30163f
JK
7126 }
7127 }
3467ec66
PA
7128 else
7129 find_pc_partial_function (loc->address, &function_name, NULL, NULL);
0e30163f 7130
2c02bd72
DE
7131 if (function_name)
7132 loc->function_name = xstrdup (function_name);
0d381245
VP
7133 }
7134}
7135
a6d9a66e 7136/* Attempt to determine architecture of location identified by SAL. */
1bfeeb0f 7137struct gdbarch *
a6d9a66e
UW
7138get_sal_arch (struct symtab_and_line sal)
7139{
7140 if (sal.section)
7141 return get_objfile_arch (sal.section->objfile);
7142 if (sal.symtab)
eb822aa6 7143 return get_objfile_arch (SYMTAB_OBJFILE (sal.symtab));
a6d9a66e
UW
7144
7145 return NULL;
7146}
7147
346774a9
PA
7148/* Low level routine for partially initializing a breakpoint of type
7149 BPTYPE. The newly created breakpoint's address, section, source
c56053d2 7150 file name, and line number are provided by SAL.
0d381245
VP
7151
7152 It is expected that the caller will complete the initialization of
7153 the newly created breakpoint struct as well as output any status
c56053d2 7154 information regarding the creation of a new breakpoint. */
0d381245 7155
346774a9
PA
7156static void
7157init_raw_breakpoint (struct breakpoint *b, struct gdbarch *gdbarch,
28010a5d 7158 struct symtab_and_line sal, enum bptype bptype,
c0a91b2b 7159 const struct breakpoint_ops *ops)
0d381245 7160{
28010a5d 7161 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
346774a9 7162
3742cc8b 7163 add_location_to_breakpoint (b, &sal);
0d381245 7164
6c95b8df
PA
7165 if (bptype != bp_catchpoint)
7166 gdb_assert (sal.pspace != NULL);
7167
f8eba3c6
TT
7168 /* Store the program space that was used to set the breakpoint,
7169 except for ordinary breakpoints, which are independent of the
7170 program space. */
7171 if (bptype != bp_breakpoint && bptype != bp_hardware_breakpoint)
7172 b->pspace = sal.pspace;
346774a9 7173}
c906108c 7174
346774a9
PA
7175/* set_raw_breakpoint is a low level routine for allocating and
7176 partially initializing a breakpoint of type BPTYPE. The newly
7177 created breakpoint's address, section, source file name, and line
7178 number are provided by SAL. The newly created and partially
7179 initialized breakpoint is added to the breakpoint chain and
7180 is also returned as the value of this function.
7181
7182 It is expected that the caller will complete the initialization of
7183 the newly created breakpoint struct as well as output any status
7184 information regarding the creation of a new breakpoint. In
7185 particular, set_raw_breakpoint does NOT set the breakpoint
7186 number! Care should be taken to not allow an error to occur
7187 prior to completing the initialization of the breakpoint. If this
7188 should happen, a bogus breakpoint will be left on the chain. */
7189
7190struct breakpoint *
7191set_raw_breakpoint (struct gdbarch *gdbarch,
348d480f 7192 struct symtab_and_line sal, enum bptype bptype,
c0a91b2b 7193 const struct breakpoint_ops *ops)
346774a9 7194{
3b0871f4 7195 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (bptype);
346774a9 7196
3b0871f4 7197 init_raw_breakpoint (b.get (), gdbarch, sal, bptype, ops);
b270e6f9 7198 return add_to_breakpoint_chain (std::move (b));
c906108c
SS
7199}
7200
53a5351d 7201/* Call this routine when stepping and nexting to enable a breakpoint
186c406b
TT
7202 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
7203 initiated the operation. */
c906108c
SS
7204
7205void
186c406b 7206set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
c906108c 7207{
35df4500 7208 struct breakpoint *b, *b_tmp;
5d5658a1 7209 int thread = tp->global_num;
0fd8e87f
UW
7210
7211 /* To avoid having to rescan all objfile symbols at every step,
7212 we maintain a list of continually-inserted but always disabled
7213 longjmp "master" breakpoints. Here, we simply create momentary
7214 clones of those and enable them for the requested thread. */
35df4500 7215 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df 7216 if (b->pspace == current_program_space
186c406b
TT
7217 && (b->type == bp_longjmp_master
7218 || b->type == bp_exception_master))
0fd8e87f 7219 {
06edf0c0
PA
7220 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
7221 struct breakpoint *clone;
cc59ec59 7222
e2e4d78b
JK
7223 /* longjmp_breakpoint_ops ensures INITIATING_FRAME is cleared again
7224 after their removal. */
06edf0c0 7225 clone = momentary_breakpoint_from_master (b, type,
c1fc2657 7226 &momentary_breakpoint_ops, 1);
0fd8e87f
UW
7227 clone->thread = thread;
7228 }
186c406b
TT
7229
7230 tp->initiating_frame = frame;
c906108c
SS
7231}
7232
611c83ae 7233/* Delete all longjmp breakpoints from THREAD. */
c906108c 7234void
611c83ae 7235delete_longjmp_breakpoint (int thread)
c906108c 7236{
35df4500 7237 struct breakpoint *b, *b_tmp;
c906108c 7238
35df4500 7239 ALL_BREAKPOINTS_SAFE (b, b_tmp)
186c406b 7240 if (b->type == bp_longjmp || b->type == bp_exception)
611c83ae
PA
7241 {
7242 if (b->thread == thread)
7243 delete_breakpoint (b);
7244 }
c906108c
SS
7245}
7246
f59f708a
PA
7247void
7248delete_longjmp_breakpoint_at_next_stop (int thread)
7249{
7250 struct breakpoint *b, *b_tmp;
7251
7252 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7253 if (b->type == bp_longjmp || b->type == bp_exception)
7254 {
7255 if (b->thread == thread)
7256 b->disposition = disp_del_at_next_stop;
7257 }
7258}
7259
e2e4d78b
JK
7260/* Place breakpoints of type bp_longjmp_call_dummy to catch longjmp for
7261 INFERIOR_PTID thread. Chain them all by RELATED_BREAKPOINT and return
7262 pointer to any of them. Return NULL if this system cannot place longjmp
7263 breakpoints. */
7264
7265struct breakpoint *
7266set_longjmp_breakpoint_for_call_dummy (void)
7267{
7268 struct breakpoint *b, *retval = NULL;
7269
7270 ALL_BREAKPOINTS (b)
7271 if (b->pspace == current_program_space && b->type == bp_longjmp_master)
7272 {
7273 struct breakpoint *new_b;
7274
7275 new_b = momentary_breakpoint_from_master (b, bp_longjmp_call_dummy,
a1aa2221
LM
7276 &momentary_breakpoint_ops,
7277 1);
00431a78 7278 new_b->thread = inferior_thread ()->global_num;
e2e4d78b
JK
7279
7280 /* Link NEW_B into the chain of RETVAL breakpoints. */
7281
7282 gdb_assert (new_b->related_breakpoint == new_b);
7283 if (retval == NULL)
7284 retval = new_b;
7285 new_b->related_breakpoint = retval;
7286 while (retval->related_breakpoint != new_b->related_breakpoint)
7287 retval = retval->related_breakpoint;
7288 retval->related_breakpoint = new_b;
7289 }
7290
7291 return retval;
7292}
7293
7294/* Verify all existing dummy frames and their associated breakpoints for
b67a2c6f 7295 TP. Remove those which can no longer be found in the current frame
e2e4d78b
JK
7296 stack.
7297
7298 You should call this function only at places where it is safe to currently
7299 unwind the whole stack. Failed stack unwind would discard live dummy
7300 frames. */
7301
7302void
b67a2c6f 7303check_longjmp_breakpoint_for_call_dummy (struct thread_info *tp)
e2e4d78b
JK
7304{
7305 struct breakpoint *b, *b_tmp;
7306
7307 ALL_BREAKPOINTS_SAFE (b, b_tmp)
5d5658a1 7308 if (b->type == bp_longjmp_call_dummy && b->thread == tp->global_num)
e2e4d78b
JK
7309 {
7310 struct breakpoint *dummy_b = b->related_breakpoint;
7311
7312 while (dummy_b != b && dummy_b->type != bp_call_dummy)
7313 dummy_b = dummy_b->related_breakpoint;
7314 if (dummy_b->type != bp_call_dummy
7315 || frame_find_by_id (dummy_b->frame_id) != NULL)
7316 continue;
7317
00431a78 7318 dummy_frame_discard (dummy_b->frame_id, tp);
e2e4d78b
JK
7319
7320 while (b->related_breakpoint != b)
7321 {
7322 if (b_tmp == b->related_breakpoint)
7323 b_tmp = b->related_breakpoint->next;
7324 delete_breakpoint (b->related_breakpoint);
7325 }
7326 delete_breakpoint (b);
7327 }
7328}
7329
1900040c
MS
7330void
7331enable_overlay_breakpoints (void)
7332{
52f0bd74 7333 struct breakpoint *b;
1900040c
MS
7334
7335 ALL_BREAKPOINTS (b)
7336 if (b->type == bp_overlay_event)
7337 {
7338 b->enable_state = bp_enabled;
44702360 7339 update_global_location_list (UGLL_MAY_INSERT);
c02f5703 7340 overlay_events_enabled = 1;
1900040c
MS
7341 }
7342}
7343
7344void
7345disable_overlay_breakpoints (void)
7346{
52f0bd74 7347 struct breakpoint *b;
1900040c
MS
7348
7349 ALL_BREAKPOINTS (b)
7350 if (b->type == bp_overlay_event)
7351 {
7352 b->enable_state = bp_disabled;
44702360 7353 update_global_location_list (UGLL_DONT_INSERT);
c02f5703 7354 overlay_events_enabled = 0;
1900040c
MS
7355 }
7356}
7357
aa7d318d
TT
7358/* Set an active std::terminate breakpoint for each std::terminate
7359 master breakpoint. */
7360void
7361set_std_terminate_breakpoint (void)
7362{
35df4500 7363 struct breakpoint *b, *b_tmp;
aa7d318d 7364
35df4500 7365 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
7366 if (b->pspace == current_program_space
7367 && b->type == bp_std_terminate_master)
7368 {
06edf0c0 7369 momentary_breakpoint_from_master (b, bp_std_terminate,
a1aa2221 7370 &momentary_breakpoint_ops, 1);
aa7d318d
TT
7371 }
7372}
7373
7374/* Delete all the std::terminate breakpoints. */
7375void
7376delete_std_terminate_breakpoint (void)
7377{
35df4500 7378 struct breakpoint *b, *b_tmp;
aa7d318d 7379
35df4500 7380 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
7381 if (b->type == bp_std_terminate)
7382 delete_breakpoint (b);
7383}
7384
c4093a6a 7385struct breakpoint *
a6d9a66e 7386create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
c4093a6a
JM
7387{
7388 struct breakpoint *b;
c4093a6a 7389
06edf0c0
PA
7390 b = create_internal_breakpoint (gdbarch, address, bp_thread_event,
7391 &internal_breakpoint_ops);
7392
b5de0fa7 7393 b->enable_state = bp_enabled;
f00aae0f 7394 /* location has to be used or breakpoint_re_set will delete me. */
d28cd78a 7395 b->location = new_address_location (b->loc->address, NULL, 0);
c4093a6a 7396
44702360 7397 update_global_location_list_nothrow (UGLL_MAY_INSERT);
74960c60 7398
c4093a6a
JM
7399 return b;
7400}
7401
0101ce28
JJ
7402struct lang_and_radix
7403 {
7404 enum language lang;
7405 int radix;
7406 };
7407
4efc6507
DE
7408/* Create a breakpoint for JIT code registration and unregistration. */
7409
7410struct breakpoint *
7411create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7412{
2a7f3dff
PA
7413 return create_internal_breakpoint (gdbarch, address, bp_jit_event,
7414 &internal_breakpoint_ops);
4efc6507 7415}
0101ce28 7416
03673fc7
PP
7417/* Remove JIT code registration and unregistration breakpoint(s). */
7418
7419void
7420remove_jit_event_breakpoints (void)
7421{
7422 struct breakpoint *b, *b_tmp;
7423
7424 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7425 if (b->type == bp_jit_event
7426 && b->loc->pspace == current_program_space)
7427 delete_breakpoint (b);
7428}
7429
cae688ec
JJ
7430void
7431remove_solib_event_breakpoints (void)
7432{
35df4500 7433 struct breakpoint *b, *b_tmp;
cae688ec 7434
35df4500 7435 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df
PA
7436 if (b->type == bp_shlib_event
7437 && b->loc->pspace == current_program_space)
cae688ec
JJ
7438 delete_breakpoint (b);
7439}
7440
f37f681c
PA
7441/* See breakpoint.h. */
7442
7443void
7444remove_solib_event_breakpoints_at_next_stop (void)
7445{
7446 struct breakpoint *b, *b_tmp;
7447
7448 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7449 if (b->type == bp_shlib_event
7450 && b->loc->pspace == current_program_space)
7451 b->disposition = disp_del_at_next_stop;
7452}
7453
04086b45
PA
7454/* Helper for create_solib_event_breakpoint /
7455 create_and_insert_solib_event_breakpoint. Allows specifying which
7456 INSERT_MODE to pass through to update_global_location_list. */
7457
7458static struct breakpoint *
7459create_solib_event_breakpoint_1 (struct gdbarch *gdbarch, CORE_ADDR address,
7460 enum ugll_insert_mode insert_mode)
cae688ec
JJ
7461{
7462 struct breakpoint *b;
7463
06edf0c0
PA
7464 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event,
7465 &internal_breakpoint_ops);
04086b45 7466 update_global_location_list_nothrow (insert_mode);
cae688ec
JJ
7467 return b;
7468}
7469
04086b45
PA
7470struct breakpoint *
7471create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7472{
7473 return create_solib_event_breakpoint_1 (gdbarch, address, UGLL_MAY_INSERT);
7474}
7475
f37f681c
PA
7476/* See breakpoint.h. */
7477
7478struct breakpoint *
7479create_and_insert_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7480{
7481 struct breakpoint *b;
7482
04086b45
PA
7483 /* Explicitly tell update_global_location_list to insert
7484 locations. */
7485 b = create_solib_event_breakpoint_1 (gdbarch, address, UGLL_INSERT);
f37f681c
PA
7486 if (!b->loc->inserted)
7487 {
7488 delete_breakpoint (b);
7489 return NULL;
7490 }
7491 return b;
7492}
7493
cae688ec
JJ
7494/* Disable any breakpoints that are on code in shared libraries. Only
7495 apply to enabled breakpoints, disabled ones can just stay disabled. */
7496
7497void
cb851954 7498disable_breakpoints_in_shlibs (void)
cae688ec 7499{
876fa593 7500 struct bp_location *loc, **locp_tmp;
cae688ec 7501
876fa593 7502 ALL_BP_LOCATIONS (loc, locp_tmp)
cae688ec 7503 {
2bdf28a0 7504 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 7505 struct breakpoint *b = loc->owner;
2bdf28a0 7506
4a64f543
MS
7507 /* We apply the check to all breakpoints, including disabled for
7508 those with loc->duplicate set. This is so that when breakpoint
7509 becomes enabled, or the duplicate is removed, gdb will try to
7510 insert all breakpoints. If we don't set shlib_disabled here,
7511 we'll try to insert those breakpoints and fail. */
1042e4c0 7512 if (((b->type == bp_breakpoint)
508ccb1f 7513 || (b->type == bp_jit_event)
1042e4c0 7514 || (b->type == bp_hardware_breakpoint)
d77f58be 7515 || (is_tracepoint (b)))
6c95b8df 7516 && loc->pspace == current_program_space
0d381245 7517 && !loc->shlib_disabled
6c95b8df 7518 && solib_name_from_address (loc->pspace, loc->address)
a77053c2 7519 )
0d381245
VP
7520 {
7521 loc->shlib_disabled = 1;
7522 }
cae688ec
JJ
7523 }
7524}
7525
63644780
NB
7526/* Disable any breakpoints and tracepoints that are in SOLIB upon
7527 notification of unloaded_shlib. Only apply to enabled breakpoints,
7528 disabled ones can just stay disabled. */
84acb35a 7529
75149521 7530static void
84acb35a
JJ
7531disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
7532{
876fa593 7533 struct bp_location *loc, **locp_tmp;
84acb35a
JJ
7534 int disabled_shlib_breaks = 0;
7535
876fa593 7536 ALL_BP_LOCATIONS (loc, locp_tmp)
84acb35a 7537 {
2bdf28a0 7538 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 7539 struct breakpoint *b = loc->owner;
cc59ec59 7540
1e4d1764 7541 if (solib->pspace == loc->pspace
e2dd7057 7542 && !loc->shlib_disabled
1e4d1764
YQ
7543 && (((b->type == bp_breakpoint
7544 || b->type == bp_jit_event
7545 || b->type == bp_hardware_breakpoint)
7546 && (loc->loc_type == bp_loc_hardware_breakpoint
7547 || loc->loc_type == bp_loc_software_breakpoint))
7548 || is_tracepoint (b))
e2dd7057 7549 && solib_contains_address_p (solib, loc->address))
84acb35a 7550 {
e2dd7057
PP
7551 loc->shlib_disabled = 1;
7552 /* At this point, we cannot rely on remove_breakpoint
7553 succeeding so we must mark the breakpoint as not inserted
7554 to prevent future errors occurring in remove_breakpoints. */
7555 loc->inserted = 0;
8d3788bd
VP
7556
7557 /* This may cause duplicate notifications for the same breakpoint. */
76727919 7558 gdb::observers::breakpoint_modified.notify (b);
8d3788bd 7559
e2dd7057
PP
7560 if (!disabled_shlib_breaks)
7561 {
223ffa71 7562 target_terminal::ours_for_output ();
3e43a32a
MS
7563 warning (_("Temporarily disabling breakpoints "
7564 "for unloaded shared library \"%s\""),
e2dd7057 7565 solib->so_name);
84acb35a 7566 }
e2dd7057 7567 disabled_shlib_breaks = 1;
84acb35a
JJ
7568 }
7569 }
84acb35a
JJ
7570}
7571
63644780
NB
7572/* Disable any breakpoints and tracepoints in OBJFILE upon
7573 notification of free_objfile. Only apply to enabled breakpoints,
7574 disabled ones can just stay disabled. */
7575
7576static void
7577disable_breakpoints_in_freed_objfile (struct objfile *objfile)
7578{
7579 struct breakpoint *b;
7580
7581 if (objfile == NULL)
7582 return;
7583
d03de421
PA
7584 /* OBJF_SHARED|OBJF_USERLOADED objfiles are dynamic modules manually
7585 managed by the user with add-symbol-file/remove-symbol-file.
7586 Similarly to how breakpoints in shared libraries are handled in
7587 response to "nosharedlibrary", mark breakpoints in such modules
08351840
PA
7588 shlib_disabled so they end up uninserted on the next global
7589 location list update. Shared libraries not loaded by the user
7590 aren't handled here -- they're already handled in
7591 disable_breakpoints_in_unloaded_shlib, called by solib.c's
7592 solib_unloaded observer. We skip objfiles that are not
d03de421
PA
7593 OBJF_SHARED as those aren't considered dynamic objects (e.g. the
7594 main objfile). */
7595 if ((objfile->flags & OBJF_SHARED) == 0
7596 || (objfile->flags & OBJF_USERLOADED) == 0)
63644780
NB
7597 return;
7598
7599 ALL_BREAKPOINTS (b)
7600 {
7601 struct bp_location *loc;
7602 int bp_modified = 0;
7603
7604 if (!is_breakpoint (b) && !is_tracepoint (b))
7605 continue;
7606
7607 for (loc = b->loc; loc != NULL; loc = loc->next)
7608 {
7609 CORE_ADDR loc_addr = loc->address;
7610
7611 if (loc->loc_type != bp_loc_hardware_breakpoint
7612 && loc->loc_type != bp_loc_software_breakpoint)
7613 continue;
7614
7615 if (loc->shlib_disabled != 0)
7616 continue;
7617
7618 if (objfile->pspace != loc->pspace)
7619 continue;
7620
7621 if (loc->loc_type != bp_loc_hardware_breakpoint
7622 && loc->loc_type != bp_loc_software_breakpoint)
7623 continue;
7624
7625 if (is_addr_in_objfile (loc_addr, objfile))
7626 {
7627 loc->shlib_disabled = 1;
08351840
PA
7628 /* At this point, we don't know whether the object was
7629 unmapped from the inferior or not, so leave the
7630 inserted flag alone. We'll handle failure to
7631 uninsert quietly, in case the object was indeed
7632 unmapped. */
63644780
NB
7633
7634 mark_breakpoint_location_modified (loc);
7635
7636 bp_modified = 1;
7637 }
7638 }
7639
7640 if (bp_modified)
76727919 7641 gdb::observers::breakpoint_modified.notify (b);
63644780
NB
7642 }
7643}
7644
ce78b96d
JB
7645/* FORK & VFORK catchpoints. */
7646
e29a4733 7647/* An instance of this type is used to represent a fork or vfork
c1fc2657
SM
7648 catchpoint. A breakpoint is really of this type iff its ops pointer points
7649 to CATCH_FORK_BREAKPOINT_OPS. */
e29a4733 7650
c1fc2657 7651struct fork_catchpoint : public breakpoint
e29a4733 7652{
e29a4733
PA
7653 /* Process id of a child process whose forking triggered this
7654 catchpoint. This field is only valid immediately after this
7655 catchpoint has triggered. */
7656 ptid_t forked_inferior_pid;
7657};
7658
4a64f543
MS
7659/* Implement the "insert" breakpoint_ops method for fork
7660 catchpoints. */
ce78b96d 7661
77b06cd7
TJB
7662static int
7663insert_catch_fork (struct bp_location *bl)
ce78b96d 7664{
e99b03dc 7665 return target_insert_fork_catchpoint (inferior_ptid.pid ());
ce78b96d
JB
7666}
7667
4a64f543
MS
7668/* Implement the "remove" breakpoint_ops method for fork
7669 catchpoints. */
ce78b96d
JB
7670
7671static int
73971819 7672remove_catch_fork (struct bp_location *bl, enum remove_bp_reason reason)
ce78b96d 7673{
e99b03dc 7674 return target_remove_fork_catchpoint (inferior_ptid.pid ());
ce78b96d
JB
7675}
7676
7677/* Implement the "breakpoint_hit" breakpoint_ops method for fork
7678 catchpoints. */
7679
7680static int
f1310107 7681breakpoint_hit_catch_fork (const struct bp_location *bl,
bd522513 7682 const address_space *aspace, CORE_ADDR bp_addr,
09ac7c10 7683 const struct target_waitstatus *ws)
ce78b96d 7684{
e29a4733
PA
7685 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7686
f90263c1
TT
7687 if (ws->kind != TARGET_WAITKIND_FORKED)
7688 return 0;
7689
7690 c->forked_inferior_pid = ws->value.related_pid;
7691 return 1;
ce78b96d
JB
7692}
7693
4a64f543
MS
7694/* Implement the "print_it" breakpoint_ops method for fork
7695 catchpoints. */
ce78b96d
JB
7696
7697static enum print_stop_action
348d480f 7698print_it_catch_fork (bpstat bs)
ce78b96d 7699{
36dfb11c 7700 struct ui_out *uiout = current_uiout;
348d480f
PA
7701 struct breakpoint *b = bs->breakpoint_at;
7702 struct fork_catchpoint *c = (struct fork_catchpoint *) bs->breakpoint_at;
e29a4733 7703
ce78b96d 7704 annotate_catchpoint (b->number);
f303dbd6 7705 maybe_print_thread_hit_breakpoint (uiout);
36dfb11c 7706 if (b->disposition == disp_del)
112e8700 7707 uiout->text ("Temporary catchpoint ");
36dfb11c 7708 else
112e8700
SM
7709 uiout->text ("Catchpoint ");
7710 if (uiout->is_mi_like_p ())
36dfb11c 7711 {
112e8700
SM
7712 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_FORK));
7713 uiout->field_string ("disp", bpdisp_text (b->disposition));
36dfb11c 7714 }
112e8700
SM
7715 uiout->field_int ("bkptno", b->number);
7716 uiout->text (" (forked process ");
e99b03dc 7717 uiout->field_int ("newpid", c->forked_inferior_pid.pid ());
112e8700 7718 uiout->text ("), ");
ce78b96d
JB
7719 return PRINT_SRC_AND_LOC;
7720}
7721
4a64f543
MS
7722/* Implement the "print_one" breakpoint_ops method for fork
7723 catchpoints. */
ce78b96d
JB
7724
7725static void
a6d9a66e 7726print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 7727{
e29a4733 7728 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
79a45b7d 7729 struct value_print_options opts;
79a45e25 7730 struct ui_out *uiout = current_uiout;
79a45b7d
TT
7731
7732 get_user_print_options (&opts);
7733
4a64f543
MS
7734 /* Field 4, the address, is omitted (which makes the columns not
7735 line up too nicely with the headers, but the effect is relatively
7736 readable). */
79a45b7d 7737 if (opts.addressprint)
112e8700 7738 uiout->field_skip ("addr");
ce78b96d 7739 annotate_field (5);
112e8700 7740 uiout->text ("fork");
d7e15655 7741 if (c->forked_inferior_pid != null_ptid)
ce78b96d 7742 {
112e8700 7743 uiout->text (", process ");
e99b03dc 7744 uiout->field_int ("what", c->forked_inferior_pid.pid ());
112e8700 7745 uiout->spaces (1);
ce78b96d 7746 }
8ac3646f 7747
112e8700
SM
7748 if (uiout->is_mi_like_p ())
7749 uiout->field_string ("catch-type", "fork");
ce78b96d
JB
7750}
7751
7752/* Implement the "print_mention" breakpoint_ops method for fork
7753 catchpoints. */
7754
7755static void
7756print_mention_catch_fork (struct breakpoint *b)
7757{
7758 printf_filtered (_("Catchpoint %d (fork)"), b->number);
7759}
7760
6149aea9
PA
7761/* Implement the "print_recreate" breakpoint_ops method for fork
7762 catchpoints. */
7763
7764static void
7765print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
7766{
7767 fprintf_unfiltered (fp, "catch fork");
d9b3f62e 7768 print_recreate_thread (b, fp);
6149aea9
PA
7769}
7770
ce78b96d
JB
7771/* The breakpoint_ops structure to be used in fork catchpoints. */
7772
2060206e 7773static struct breakpoint_ops catch_fork_breakpoint_ops;
ce78b96d 7774
4a64f543
MS
7775/* Implement the "insert" breakpoint_ops method for vfork
7776 catchpoints. */
ce78b96d 7777
77b06cd7
TJB
7778static int
7779insert_catch_vfork (struct bp_location *bl)
ce78b96d 7780{
e99b03dc 7781 return target_insert_vfork_catchpoint (inferior_ptid.pid ());
ce78b96d
JB
7782}
7783
4a64f543
MS
7784/* Implement the "remove" breakpoint_ops method for vfork
7785 catchpoints. */
ce78b96d
JB
7786
7787static int
73971819 7788remove_catch_vfork (struct bp_location *bl, enum remove_bp_reason reason)
ce78b96d 7789{
e99b03dc 7790 return target_remove_vfork_catchpoint (inferior_ptid.pid ());
ce78b96d
JB
7791}
7792
7793/* Implement the "breakpoint_hit" breakpoint_ops method for vfork
7794 catchpoints. */
7795
7796static int
f1310107 7797breakpoint_hit_catch_vfork (const struct bp_location *bl,
bd522513 7798 const address_space *aspace, CORE_ADDR bp_addr,
09ac7c10 7799 const struct target_waitstatus *ws)
ce78b96d 7800{
e29a4733
PA
7801 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7802
f90263c1
TT
7803 if (ws->kind != TARGET_WAITKIND_VFORKED)
7804 return 0;
7805
7806 c->forked_inferior_pid = ws->value.related_pid;
7807 return 1;
ce78b96d
JB
7808}
7809
4a64f543
MS
7810/* Implement the "print_it" breakpoint_ops method for vfork
7811 catchpoints. */
ce78b96d
JB
7812
7813static enum print_stop_action
348d480f 7814print_it_catch_vfork (bpstat bs)
ce78b96d 7815{
36dfb11c 7816 struct ui_out *uiout = current_uiout;
348d480f 7817 struct breakpoint *b = bs->breakpoint_at;
e29a4733
PA
7818 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
7819
ce78b96d 7820 annotate_catchpoint (b->number);
f303dbd6 7821 maybe_print_thread_hit_breakpoint (uiout);
36dfb11c 7822 if (b->disposition == disp_del)
112e8700 7823 uiout->text ("Temporary catchpoint ");
36dfb11c 7824 else
112e8700
SM
7825 uiout->text ("Catchpoint ");
7826 if (uiout->is_mi_like_p ())
36dfb11c 7827 {
112e8700
SM
7828 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_VFORK));
7829 uiout->field_string ("disp", bpdisp_text (b->disposition));
36dfb11c 7830 }
112e8700
SM
7831 uiout->field_int ("bkptno", b->number);
7832 uiout->text (" (vforked process ");
e99b03dc 7833 uiout->field_int ("newpid", c->forked_inferior_pid.pid ());
112e8700 7834 uiout->text ("), ");
ce78b96d
JB
7835 return PRINT_SRC_AND_LOC;
7836}
7837
4a64f543
MS
7838/* Implement the "print_one" breakpoint_ops method for vfork
7839 catchpoints. */
ce78b96d
JB
7840
7841static void
a6d9a66e 7842print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 7843{
e29a4733 7844 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
79a45b7d 7845 struct value_print_options opts;
79a45e25 7846 struct ui_out *uiout = current_uiout;
79a45b7d
TT
7847
7848 get_user_print_options (&opts);
4a64f543
MS
7849 /* Field 4, the address, is omitted (which makes the columns not
7850 line up too nicely with the headers, but the effect is relatively
7851 readable). */
79a45b7d 7852 if (opts.addressprint)
112e8700 7853 uiout->field_skip ("addr");
ce78b96d 7854 annotate_field (5);
112e8700 7855 uiout->text ("vfork");
d7e15655 7856 if (c->forked_inferior_pid != null_ptid)
ce78b96d 7857 {
112e8700 7858 uiout->text (", process ");
e99b03dc 7859 uiout->field_int ("what", c->forked_inferior_pid.pid ());
112e8700 7860 uiout->spaces (1);
ce78b96d 7861 }
8ac3646f 7862
112e8700
SM
7863 if (uiout->is_mi_like_p ())
7864 uiout->field_string ("catch-type", "vfork");
ce78b96d
JB
7865}
7866
7867/* Implement the "print_mention" breakpoint_ops method for vfork
7868 catchpoints. */
7869
7870static void
7871print_mention_catch_vfork (struct breakpoint *b)
7872{
7873 printf_filtered (_("Catchpoint %d (vfork)"), b->number);
7874}
7875
6149aea9
PA
7876/* Implement the "print_recreate" breakpoint_ops method for vfork
7877 catchpoints. */
7878
7879static void
7880print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
7881{
7882 fprintf_unfiltered (fp, "catch vfork");
d9b3f62e 7883 print_recreate_thread (b, fp);
6149aea9
PA
7884}
7885
ce78b96d
JB
7886/* The breakpoint_ops structure to be used in vfork catchpoints. */
7887
2060206e 7888static struct breakpoint_ops catch_vfork_breakpoint_ops;
ce78b96d 7889
edcc5120 7890/* An instance of this type is used to represent an solib catchpoint.
c1fc2657 7891 A breakpoint is really of this type iff its ops pointer points to
edcc5120
TT
7892 CATCH_SOLIB_BREAKPOINT_OPS. */
7893
c1fc2657 7894struct solib_catchpoint : public breakpoint
edcc5120 7895{
c1fc2657 7896 ~solib_catchpoint () override;
edcc5120
TT
7897
7898 /* True for "catch load", false for "catch unload". */
7899 unsigned char is_load;
7900
7901 /* Regular expression to match, if any. COMPILED is only valid when
7902 REGEX is non-NULL. */
7903 char *regex;
2d7cc5c7 7904 std::unique_ptr<compiled_regex> compiled;
edcc5120
TT
7905};
7906
c1fc2657 7907solib_catchpoint::~solib_catchpoint ()
edcc5120 7908{
c1fc2657 7909 xfree (this->regex);
edcc5120
TT
7910}
7911
7912static int
7913insert_catch_solib (struct bp_location *ignore)
7914{
7915 return 0;
7916}
7917
7918static int
73971819 7919remove_catch_solib (struct bp_location *ignore, enum remove_bp_reason reason)
edcc5120
TT
7920{
7921 return 0;
7922}
7923
7924static int
7925breakpoint_hit_catch_solib (const struct bp_location *bl,
bd522513 7926 const address_space *aspace,
edcc5120
TT
7927 CORE_ADDR bp_addr,
7928 const struct target_waitstatus *ws)
7929{
7930 struct solib_catchpoint *self = (struct solib_catchpoint *) bl->owner;
7931 struct breakpoint *other;
7932
7933 if (ws->kind == TARGET_WAITKIND_LOADED)
7934 return 1;
7935
7936 ALL_BREAKPOINTS (other)
7937 {
7938 struct bp_location *other_bl;
7939
7940 if (other == bl->owner)
7941 continue;
7942
7943 if (other->type != bp_shlib_event)
7944 continue;
7945
c1fc2657 7946 if (self->pspace != NULL && other->pspace != self->pspace)
edcc5120
TT
7947 continue;
7948
7949 for (other_bl = other->loc; other_bl != NULL; other_bl = other_bl->next)
7950 {
7951 if (other->ops->breakpoint_hit (other_bl, aspace, bp_addr, ws))
7952 return 1;
7953 }
7954 }
7955
7956 return 0;
7957}
7958
7959static void
7960check_status_catch_solib (struct bpstats *bs)
7961{
7962 struct solib_catchpoint *self
7963 = (struct solib_catchpoint *) bs->breakpoint_at;
edcc5120
TT
7964
7965 if (self->is_load)
7966 {
52941706 7967 for (so_list *iter : current_program_space->added_solibs)
edcc5120
TT
7968 {
7969 if (!self->regex
2d7cc5c7 7970 || self->compiled->exec (iter->so_name, 0, NULL, 0) == 0)
edcc5120
TT
7971 return;
7972 }
7973 }
7974 else
7975 {
6fb16ce6 7976 for (const std::string &iter : current_program_space->deleted_solibs)
edcc5120
TT
7977 {
7978 if (!self->regex
6fb16ce6 7979 || self->compiled->exec (iter.c_str (), 0, NULL, 0) == 0)
edcc5120
TT
7980 return;
7981 }
7982 }
7983
7984 bs->stop = 0;
7985 bs->print_it = print_it_noop;
7986}
7987
7988static enum print_stop_action
7989print_it_catch_solib (bpstat bs)
7990{
7991 struct breakpoint *b = bs->breakpoint_at;
7992 struct ui_out *uiout = current_uiout;
7993
7994 annotate_catchpoint (b->number);
f303dbd6 7995 maybe_print_thread_hit_breakpoint (uiout);
edcc5120 7996 if (b->disposition == disp_del)
112e8700 7997 uiout->text ("Temporary catchpoint ");
edcc5120 7998 else
112e8700
SM
7999 uiout->text ("Catchpoint ");
8000 uiout->field_int ("bkptno", b->number);
8001 uiout->text ("\n");
8002 if (uiout->is_mi_like_p ())
8003 uiout->field_string ("disp", bpdisp_text (b->disposition));
edcc5120
TT
8004 print_solib_event (1);
8005 return PRINT_SRC_AND_LOC;
8006}
8007
8008static void
8009print_one_catch_solib (struct breakpoint *b, struct bp_location **locs)
8010{
8011 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8012 struct value_print_options opts;
8013 struct ui_out *uiout = current_uiout;
edcc5120
TT
8014
8015 get_user_print_options (&opts);
8016 /* Field 4, the address, is omitted (which makes the columns not
8017 line up too nicely with the headers, but the effect is relatively
8018 readable). */
8019 if (opts.addressprint)
8020 {
8021 annotate_field (4);
112e8700 8022 uiout->field_skip ("addr");
edcc5120
TT
8023 }
8024
528e1572 8025 std::string msg;
edcc5120
TT
8026 annotate_field (5);
8027 if (self->is_load)
8028 {
8029 if (self->regex)
528e1572 8030 msg = string_printf (_("load of library matching %s"), self->regex);
edcc5120 8031 else
528e1572 8032 msg = _("load of library");
edcc5120
TT
8033 }
8034 else
8035 {
8036 if (self->regex)
528e1572 8037 msg = string_printf (_("unload of library matching %s"), self->regex);
edcc5120 8038 else
528e1572 8039 msg = _("unload of library");
edcc5120 8040 }
112e8700 8041 uiout->field_string ("what", msg);
8ac3646f 8042
112e8700
SM
8043 if (uiout->is_mi_like_p ())
8044 uiout->field_string ("catch-type", self->is_load ? "load" : "unload");
edcc5120
TT
8045}
8046
8047static void
8048print_mention_catch_solib (struct breakpoint *b)
8049{
8050 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8051
8052 printf_filtered (_("Catchpoint %d (%s)"), b->number,
8053 self->is_load ? "load" : "unload");
8054}
8055
8056static void
8057print_recreate_catch_solib (struct breakpoint *b, struct ui_file *fp)
8058{
8059 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8060
8061 fprintf_unfiltered (fp, "%s %s",
8062 b->disposition == disp_del ? "tcatch" : "catch",
8063 self->is_load ? "load" : "unload");
8064 if (self->regex)
8065 fprintf_unfiltered (fp, " %s", self->regex);
8066 fprintf_unfiltered (fp, "\n");
8067}
8068
8069static struct breakpoint_ops catch_solib_breakpoint_ops;
8070
91985142
MG
8071/* Shared helper function (MI and CLI) for creating and installing
8072 a shared object event catchpoint. If IS_LOAD is non-zero then
8073 the events to be caught are load events, otherwise they are
8074 unload events. If IS_TEMP is non-zero the catchpoint is a
8075 temporary one. If ENABLED is non-zero the catchpoint is
8076 created in an enabled state. */
edcc5120 8077
91985142 8078void
a121b7c1 8079add_solib_catchpoint (const char *arg, int is_load, int is_temp, int enabled)
edcc5120 8080{
edcc5120 8081 struct gdbarch *gdbarch = get_current_arch ();
edcc5120 8082
edcc5120
TT
8083 if (!arg)
8084 arg = "";
f1735a53 8085 arg = skip_spaces (arg);
edcc5120 8086
36bd8eaa 8087 std::unique_ptr<solib_catchpoint> c (new solib_catchpoint ());
edcc5120
TT
8088
8089 if (*arg != '\0')
8090 {
2d7cc5c7
PA
8091 c->compiled.reset (new compiled_regex (arg, REG_NOSUB,
8092 _("Invalid regexp")));
edcc5120
TT
8093 c->regex = xstrdup (arg);
8094 }
8095
8096 c->is_load = is_load;
36bd8eaa 8097 init_catchpoint (c.get (), gdbarch, is_temp, NULL,
edcc5120
TT
8098 &catch_solib_breakpoint_ops);
8099
c1fc2657 8100 c->enable_state = enabled ? bp_enabled : bp_disabled;
91985142 8101
b270e6f9 8102 install_breakpoint (0, std::move (c), 1);
edcc5120
TT
8103}
8104
91985142
MG
8105/* A helper function that does all the work for "catch load" and
8106 "catch unload". */
8107
8108static void
eb4c3f4a 8109catch_load_or_unload (const char *arg, int from_tty, int is_load,
91985142
MG
8110 struct cmd_list_element *command)
8111{
8112 int tempflag;
8113 const int enabled = 1;
8114
8115 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
8116
8117 add_solib_catchpoint (arg, is_load, tempflag, enabled);
8118}
8119
edcc5120 8120static void
eb4c3f4a 8121catch_load_command_1 (const char *arg, int from_tty,
edcc5120
TT
8122 struct cmd_list_element *command)
8123{
8124 catch_load_or_unload (arg, from_tty, 1, command);
8125}
8126
8127static void
eb4c3f4a 8128catch_unload_command_1 (const char *arg, int from_tty,
edcc5120
TT
8129 struct cmd_list_element *command)
8130{
8131 catch_load_or_unload (arg, from_tty, 0, command);
8132}
8133
346774a9
PA
8134/* Initialize a new breakpoint of the bp_catchpoint kind. If TEMPFLAG
8135 is non-zero, then make the breakpoint temporary. If COND_STRING is
8136 not NULL, then store it in the breakpoint. OPS, if not NULL, is
8137 the breakpoint_ops structure associated to the catchpoint. */
ce78b96d 8138
ab04a2af 8139void
346774a9
PA
8140init_catchpoint (struct breakpoint *b,
8141 struct gdbarch *gdbarch, int tempflag,
63160a43 8142 const char *cond_string,
c0a91b2b 8143 const struct breakpoint_ops *ops)
c906108c 8144{
51abb421 8145 symtab_and_line sal;
6c95b8df 8146 sal.pspace = current_program_space;
c5aa993b 8147
28010a5d 8148 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
ce78b96d 8149
1b36a34b 8150 b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
b5de0fa7 8151 b->disposition = tempflag ? disp_del : disp_donttouch;
346774a9
PA
8152}
8153
28010a5d 8154void
b270e6f9 8155install_breakpoint (int internal, std::unique_ptr<breakpoint> &&arg, int update_gll)
c56053d2 8156{
b270e6f9 8157 breakpoint *b = add_to_breakpoint_chain (std::move (arg));
3a5c3e22 8158 set_breakpoint_number (internal, b);
558a9d82
YQ
8159 if (is_tracepoint (b))
8160 set_tracepoint_count (breakpoint_count);
3a5c3e22
PA
8161 if (!internal)
8162 mention (b);
76727919 8163 gdb::observers::breakpoint_created.notify (b);
3ea46bff
YQ
8164
8165 if (update_gll)
44702360 8166 update_global_location_list (UGLL_MAY_INSERT);
c56053d2
PA
8167}
8168
9b70b993 8169static void
a6d9a66e 8170create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
63160a43 8171 int tempflag, const char *cond_string,
c0a91b2b 8172 const struct breakpoint_ops *ops)
c906108c 8173{
b270e6f9 8174 std::unique_ptr<fork_catchpoint> c (new fork_catchpoint ());
ce78b96d 8175
b270e6f9 8176 init_catchpoint (c.get (), gdbarch, tempflag, cond_string, ops);
e29a4733
PA
8177
8178 c->forked_inferior_pid = null_ptid;
8179
b270e6f9 8180 install_breakpoint (0, std::move (c), 1);
c906108c
SS
8181}
8182
fe798b75
JB
8183/* Exec catchpoints. */
8184
b4d90040 8185/* An instance of this type is used to represent an exec catchpoint.
c1fc2657 8186 A breakpoint is really of this type iff its ops pointer points to
b4d90040
PA
8187 CATCH_EXEC_BREAKPOINT_OPS. */
8188
c1fc2657 8189struct exec_catchpoint : public breakpoint
b4d90040 8190{
c1fc2657 8191 ~exec_catchpoint () override;
b4d90040
PA
8192
8193 /* Filename of a program whose exec triggered this catchpoint.
8194 This field is only valid immediately after this catchpoint has
8195 triggered. */
8196 char *exec_pathname;
8197};
8198
c1fc2657 8199/* Exec catchpoint destructor. */
b4d90040 8200
c1fc2657 8201exec_catchpoint::~exec_catchpoint ()
b4d90040 8202{
c1fc2657 8203 xfree (this->exec_pathname);
b4d90040
PA
8204}
8205
77b06cd7
TJB
8206static int
8207insert_catch_exec (struct bp_location *bl)
c906108c 8208{
e99b03dc 8209 return target_insert_exec_catchpoint (inferior_ptid.pid ());
fe798b75 8210}
c906108c 8211
fe798b75 8212static int
73971819 8213remove_catch_exec (struct bp_location *bl, enum remove_bp_reason reason)
fe798b75 8214{
e99b03dc 8215 return target_remove_exec_catchpoint (inferior_ptid.pid ());
fe798b75 8216}
c906108c 8217
fe798b75 8218static int
f1310107 8219breakpoint_hit_catch_exec (const struct bp_location *bl,
bd522513 8220 const address_space *aspace, CORE_ADDR bp_addr,
09ac7c10 8221 const struct target_waitstatus *ws)
fe798b75 8222{
b4d90040
PA
8223 struct exec_catchpoint *c = (struct exec_catchpoint *) bl->owner;
8224
f90263c1
TT
8225 if (ws->kind != TARGET_WAITKIND_EXECD)
8226 return 0;
8227
8228 c->exec_pathname = xstrdup (ws->value.execd_pathname);
8229 return 1;
fe798b75 8230}
c906108c 8231
fe798b75 8232static enum print_stop_action
348d480f 8233print_it_catch_exec (bpstat bs)
fe798b75 8234{
36dfb11c 8235 struct ui_out *uiout = current_uiout;
348d480f 8236 struct breakpoint *b = bs->breakpoint_at;
b4d90040
PA
8237 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8238
fe798b75 8239 annotate_catchpoint (b->number);
f303dbd6 8240 maybe_print_thread_hit_breakpoint (uiout);
36dfb11c 8241 if (b->disposition == disp_del)
112e8700 8242 uiout->text ("Temporary catchpoint ");
36dfb11c 8243 else
112e8700
SM
8244 uiout->text ("Catchpoint ");
8245 if (uiout->is_mi_like_p ())
36dfb11c 8246 {
112e8700
SM
8247 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_EXEC));
8248 uiout->field_string ("disp", bpdisp_text (b->disposition));
36dfb11c 8249 }
112e8700
SM
8250 uiout->field_int ("bkptno", b->number);
8251 uiout->text (" (exec'd ");
8252 uiout->field_string ("new-exec", c->exec_pathname);
8253 uiout->text ("), ");
36dfb11c 8254
fe798b75 8255 return PRINT_SRC_AND_LOC;
c906108c
SS
8256}
8257
fe798b75 8258static void
a6d9a66e 8259print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
fe798b75 8260{
b4d90040 8261 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
fe798b75 8262 struct value_print_options opts;
79a45e25 8263 struct ui_out *uiout = current_uiout;
fe798b75
JB
8264
8265 get_user_print_options (&opts);
8266
8267 /* Field 4, the address, is omitted (which makes the columns
8268 not line up too nicely with the headers, but the effect
8269 is relatively readable). */
8270 if (opts.addressprint)
112e8700 8271 uiout->field_skip ("addr");
fe798b75 8272 annotate_field (5);
112e8700 8273 uiout->text ("exec");
b4d90040 8274 if (c->exec_pathname != NULL)
fe798b75 8275 {
112e8700
SM
8276 uiout->text (", program \"");
8277 uiout->field_string ("what", c->exec_pathname);
8278 uiout->text ("\" ");
fe798b75 8279 }
8ac3646f 8280
112e8700
SM
8281 if (uiout->is_mi_like_p ())
8282 uiout->field_string ("catch-type", "exec");
fe798b75
JB
8283}
8284
8285static void
8286print_mention_catch_exec (struct breakpoint *b)
8287{
8288 printf_filtered (_("Catchpoint %d (exec)"), b->number);
8289}
8290
6149aea9
PA
8291/* Implement the "print_recreate" breakpoint_ops method for exec
8292 catchpoints. */
8293
8294static void
8295print_recreate_catch_exec (struct breakpoint *b, struct ui_file *fp)
8296{
8297 fprintf_unfiltered (fp, "catch exec");
d9b3f62e 8298 print_recreate_thread (b, fp);
6149aea9
PA
8299}
8300
2060206e 8301static struct breakpoint_ops catch_exec_breakpoint_ops;
fe798b75 8302
c906108c 8303static int
fba45db2 8304hw_breakpoint_used_count (void)
c906108c 8305{
c906108c 8306 int i = 0;
f1310107
TJB
8307 struct breakpoint *b;
8308 struct bp_location *bl;
c906108c
SS
8309
8310 ALL_BREAKPOINTS (b)
c5aa993b 8311 {
d6b74ac4 8312 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
f1310107
TJB
8313 for (bl = b->loc; bl; bl = bl->next)
8314 {
8315 /* Special types of hardware breakpoints may use more than
8316 one register. */
348d480f 8317 i += b->ops->resources_needed (bl);
f1310107 8318 }
c5aa993b 8319 }
c906108c
SS
8320
8321 return i;
8322}
8323
a1398e0c
PA
8324/* Returns the resources B would use if it were a hardware
8325 watchpoint. */
8326
c906108c 8327static int
a1398e0c 8328hw_watchpoint_use_count (struct breakpoint *b)
c906108c 8329{
c906108c 8330 int i = 0;
e09342b5 8331 struct bp_location *bl;
c906108c 8332
a1398e0c
PA
8333 if (!breakpoint_enabled (b))
8334 return 0;
8335
8336 for (bl = b->loc; bl; bl = bl->next)
8337 {
8338 /* Special types of hardware watchpoints may use more than
8339 one register. */
8340 i += b->ops->resources_needed (bl);
8341 }
8342
8343 return i;
8344}
8345
8346/* Returns the sum the used resources of all hardware watchpoints of
8347 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
8348 the sum of the used resources of all hardware watchpoints of other
8349 types _not_ TYPE. */
8350
8351static int
8352hw_watchpoint_used_count_others (struct breakpoint *except,
8353 enum bptype type, int *other_type_used)
8354{
8355 int i = 0;
8356 struct breakpoint *b;
8357
c906108c
SS
8358 *other_type_used = 0;
8359 ALL_BREAKPOINTS (b)
e09342b5 8360 {
a1398e0c
PA
8361 if (b == except)
8362 continue;
e09342b5
TJB
8363 if (!breakpoint_enabled (b))
8364 continue;
8365
a1398e0c
PA
8366 if (b->type == type)
8367 i += hw_watchpoint_use_count (b);
8368 else if (is_hardware_watchpoint (b))
8369 *other_type_used = 1;
e09342b5
TJB
8370 }
8371
c906108c
SS
8372 return i;
8373}
8374
c906108c 8375void
fba45db2 8376disable_watchpoints_before_interactive_call_start (void)
c906108c 8377{
c5aa993b 8378 struct breakpoint *b;
c906108c
SS
8379
8380 ALL_BREAKPOINTS (b)
c5aa993b 8381 {
cc60f2e3 8382 if (is_watchpoint (b) && breakpoint_enabled (b))
c5aa993b 8383 {
b5de0fa7 8384 b->enable_state = bp_call_disabled;
44702360 8385 update_global_location_list (UGLL_DONT_INSERT);
c5aa993b
JM
8386 }
8387 }
c906108c
SS
8388}
8389
8390void
fba45db2 8391enable_watchpoints_after_interactive_call_stop (void)
c906108c 8392{
c5aa993b 8393 struct breakpoint *b;
c906108c
SS
8394
8395 ALL_BREAKPOINTS (b)
c5aa993b 8396 {
cc60f2e3 8397 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
c5aa993b 8398 {
b5de0fa7 8399 b->enable_state = bp_enabled;
44702360 8400 update_global_location_list (UGLL_MAY_INSERT);
c5aa993b
JM
8401 }
8402 }
c906108c
SS
8403}
8404
8bea4e01
UW
8405void
8406disable_breakpoints_before_startup (void)
8407{
6c95b8df 8408 current_program_space->executing_startup = 1;
44702360 8409 update_global_location_list (UGLL_DONT_INSERT);
8bea4e01
UW
8410}
8411
8412void
8413enable_breakpoints_after_startup (void)
8414{
6c95b8df 8415 current_program_space->executing_startup = 0;
f8eba3c6 8416 breakpoint_re_set ();
8bea4e01
UW
8417}
8418
7c16b83e
PA
8419/* Create a new single-step breakpoint for thread THREAD, with no
8420 locations. */
c906108c 8421
7c16b83e
PA
8422static struct breakpoint *
8423new_single_step_breakpoint (int thread, struct gdbarch *gdbarch)
8424{
b270e6f9 8425 std::unique_ptr<breakpoint> b (new breakpoint ());
7c16b83e 8426
b270e6f9 8427 init_raw_breakpoint_without_location (b.get (), gdbarch, bp_single_step,
7c16b83e
PA
8428 &momentary_breakpoint_ops);
8429
8430 b->disposition = disp_donttouch;
8431 b->frame_id = null_frame_id;
8432
8433 b->thread = thread;
8434 gdb_assert (b->thread != 0);
8435
b270e6f9 8436 return add_to_breakpoint_chain (std::move (b));
7c16b83e
PA
8437}
8438
8439/* Set a momentary breakpoint of type TYPE at address specified by
8440 SAL. If FRAME_ID is valid, the breakpoint is restricted to that
8441 frame. */
c906108c 8442
454dafbd 8443breakpoint_up
a6d9a66e
UW
8444set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
8445 struct frame_id frame_id, enum bptype type)
c906108c 8446{
52f0bd74 8447 struct breakpoint *b;
edb3359d 8448
193facb3
JK
8449 /* If FRAME_ID is valid, it should be a real frame, not an inlined or
8450 tail-called one. */
8451 gdb_assert (!frame_id_artificial_p (frame_id));
edb3359d 8452
06edf0c0 8453 b = set_raw_breakpoint (gdbarch, sal, type, &momentary_breakpoint_ops);
b5de0fa7
EZ
8454 b->enable_state = bp_enabled;
8455 b->disposition = disp_donttouch;
818dd999 8456 b->frame_id = frame_id;
c906108c 8457
00431a78 8458 b->thread = inferior_thread ()->global_num;
c906108c 8459
44702360 8460 update_global_location_list_nothrow (UGLL_MAY_INSERT);
74960c60 8461
454dafbd 8462 return breakpoint_up (b);
c906108c 8463}
611c83ae 8464
06edf0c0 8465/* Make a momentary breakpoint based on the master breakpoint ORIG.
a1aa2221
LM
8466 The new breakpoint will have type TYPE, use OPS as its
8467 breakpoint_ops, and will set enabled to LOC_ENABLED. */
e58b0e63 8468
06edf0c0
PA
8469static struct breakpoint *
8470momentary_breakpoint_from_master (struct breakpoint *orig,
8471 enum bptype type,
a1aa2221
LM
8472 const struct breakpoint_ops *ops,
8473 int loc_enabled)
e58b0e63
PA
8474{
8475 struct breakpoint *copy;
8476
06edf0c0 8477 copy = set_raw_breakpoint_without_location (orig->gdbarch, type, ops);
e58b0e63 8478 copy->loc = allocate_bp_location (copy);
0ba852ab 8479 set_breakpoint_location_function (copy->loc);
e58b0e63 8480
a6d9a66e 8481 copy->loc->gdbarch = orig->loc->gdbarch;
e58b0e63
PA
8482 copy->loc->requested_address = orig->loc->requested_address;
8483 copy->loc->address = orig->loc->address;
8484 copy->loc->section = orig->loc->section;
6c95b8df 8485 copy->loc->pspace = orig->loc->pspace;
55aa24fb 8486 copy->loc->probe = orig->loc->probe;
f8eba3c6 8487 copy->loc->line_number = orig->loc->line_number;
2f202fde 8488 copy->loc->symtab = orig->loc->symtab;
a1aa2221 8489 copy->loc->enabled = loc_enabled;
e58b0e63
PA
8490 copy->frame_id = orig->frame_id;
8491 copy->thread = orig->thread;
6c95b8df 8492 copy->pspace = orig->pspace;
e58b0e63
PA
8493
8494 copy->enable_state = bp_enabled;
8495 copy->disposition = disp_donttouch;
8496 copy->number = internal_breakpoint_number--;
8497
44702360 8498 update_global_location_list_nothrow (UGLL_DONT_INSERT);
e58b0e63
PA
8499 return copy;
8500}
8501
06edf0c0
PA
8502/* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
8503 ORIG is NULL. */
8504
8505struct breakpoint *
8506clone_momentary_breakpoint (struct breakpoint *orig)
8507{
8508 /* If there's nothing to clone, then return nothing. */
8509 if (orig == NULL)
8510 return NULL;
8511
a1aa2221 8512 return momentary_breakpoint_from_master (orig, orig->type, orig->ops, 0);
06edf0c0
PA
8513}
8514
454dafbd 8515breakpoint_up
a6d9a66e
UW
8516set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
8517 enum bptype type)
611c83ae
PA
8518{
8519 struct symtab_and_line sal;
8520
8521 sal = find_pc_line (pc, 0);
8522 sal.pc = pc;
8523 sal.section = find_pc_overlay (pc);
8524 sal.explicit_pc = 1;
8525
a6d9a66e 8526 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
611c83ae 8527}
c906108c 8528\f
c5aa993b 8529
c906108c
SS
8530/* Tell the user we have just set a breakpoint B. */
8531
8532static void
fba45db2 8533mention (struct breakpoint *b)
c906108c 8534{
348d480f 8535 b->ops->print_mention (b);
2d33446d 8536 current_uiout->text ("\n");
c906108c 8537}
c906108c 8538\f
c5aa993b 8539
1a853c52
PA
8540static int bp_loc_is_permanent (struct bp_location *loc);
8541
0d381245 8542static struct bp_location *
39d61571 8543add_location_to_breakpoint (struct breakpoint *b,
0d381245
VP
8544 const struct symtab_and_line *sal)
8545{
8546 struct bp_location *loc, **tmp;
3742cc8b
YQ
8547 CORE_ADDR adjusted_address;
8548 struct gdbarch *loc_gdbarch = get_sal_arch (*sal);
8549
8550 if (loc_gdbarch == NULL)
8551 loc_gdbarch = b->gdbarch;
8552
8553 /* Adjust the breakpoint's address prior to allocating a location.
8554 Once we call allocate_bp_location(), that mostly uninitialized
8555 location will be placed on the location chain. Adjustment of the
8556 breakpoint may cause target_read_memory() to be called and we do
8557 not want its scan of the location chain to find a breakpoint and
8558 location that's only been partially initialized. */
8559 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
8560 sal->pc, b->type);
0d381245 8561
d30113d4 8562 /* Sort the locations by their ADDRESS. */
39d61571 8563 loc = allocate_bp_location (b);
d30113d4
JK
8564 for (tmp = &(b->loc); *tmp != NULL && (*tmp)->address <= adjusted_address;
8565 tmp = &((*tmp)->next))
0d381245 8566 ;
d30113d4 8567 loc->next = *tmp;
0d381245 8568 *tmp = loc;
3742cc8b 8569
0d381245 8570 loc->requested_address = sal->pc;
3742cc8b 8571 loc->address = adjusted_address;
6c95b8df 8572 loc->pspace = sal->pspace;
935676c9 8573 loc->probe.prob = sal->prob;
729662a5 8574 loc->probe.objfile = sal->objfile;
6c95b8df 8575 gdb_assert (loc->pspace != NULL);
0d381245 8576 loc->section = sal->section;
3742cc8b 8577 loc->gdbarch = loc_gdbarch;
f8eba3c6 8578 loc->line_number = sal->line;
2f202fde 8579 loc->symtab = sal->symtab;
4a27f119 8580 loc->symbol = sal->symbol;
3467ec66
PA
8581 loc->msymbol = sal->msymbol;
8582 loc->objfile = sal->objfile;
f8eba3c6 8583
0ba852ab 8584 set_breakpoint_location_function (loc);
1a853c52 8585
6ae88661
LM
8586 /* While by definition, permanent breakpoints are already present in the
8587 code, we don't mark the location as inserted. Normally one would expect
8588 that GDB could rely on that breakpoint instruction to stop the program,
8589 thus removing the need to insert its own breakpoint, except that executing
8590 the breakpoint instruction can kill the target instead of reporting a
8591 SIGTRAP. E.g., on SPARC, when interrupts are disabled, executing the
8592 instruction resets the CPU, so QEMU 2.0.0 for SPARC correspondingly dies
8593 with "Trap 0x02 while interrupts disabled, Error state". Letting the
8594 breakpoint be inserted normally results in QEMU knowing about the GDB
8595 breakpoint, and thus trap before the breakpoint instruction is executed.
8596 (If GDB later needs to continue execution past the permanent breakpoint,
8597 it manually increments the PC, thus avoiding executing the breakpoint
8598 instruction.) */
1a853c52 8599 if (bp_loc_is_permanent (loc))
6ae88661 8600 loc->permanent = 1;
1a853c52 8601
0d381245
VP
8602 return loc;
8603}
514f746b
AR
8604\f
8605
1cf4d951 8606/* See breakpoint.h. */
514f746b 8607
1cf4d951
PA
8608int
8609program_breakpoint_here_p (struct gdbarch *gdbarch, CORE_ADDR address)
514f746b
AR
8610{
8611 int len;
8612 CORE_ADDR addr;
1afeeb75 8613 const gdb_byte *bpoint;
514f746b
AR
8614 gdb_byte *target_mem;
8615
1cf4d951
PA
8616 addr = address;
8617 bpoint = gdbarch_breakpoint_from_pc (gdbarch, &addr, &len);
8618
8619 /* Software breakpoints unsupported? */
8620 if (bpoint == NULL)
8621 return 0;
8622
224c3ddb 8623 target_mem = (gdb_byte *) alloca (len);
1cf4d951
PA
8624
8625 /* Enable the automatic memory restoration from breakpoints while
8626 we read the memory. Otherwise we could say about our temporary
8627 breakpoints they are permanent. */
cb85b21b
TT
8628 scoped_restore restore_memory
8629 = make_scoped_restore_show_memory_breakpoints (0);
1cf4d951
PA
8630
8631 if (target_read_memory (address, target_mem, len) == 0
8632 && memcmp (target_mem, bpoint, len) == 0)
cb85b21b 8633 return 1;
1cf4d951 8634
cb85b21b 8635 return 0;
1cf4d951
PA
8636}
8637
8638/* Return 1 if LOC is pointing to a permanent breakpoint,
8639 return 0 otherwise. */
8640
8641static int
8642bp_loc_is_permanent (struct bp_location *loc)
8643{
514f746b
AR
8644 gdb_assert (loc != NULL);
8645
cb1e4e32
PA
8646 /* If we have a non-breakpoint-backed catchpoint or a software
8647 watchpoint, just return 0. We should not attempt to read from
8648 the addresses the locations of these breakpoint types point to.
8649 program_breakpoint_here_p, below, will attempt to read
244558af 8650 memory. */
cb1e4e32 8651 if (!bl_address_is_meaningful (loc))
244558af
LM
8652 return 0;
8653
5ed8105e 8654 scoped_restore_current_pspace_and_thread restore_pspace_thread;
6c95b8df 8655 switch_to_program_space_and_thread (loc->pspace);
5ed8105e 8656 return program_breakpoint_here_p (loc->gdbarch, loc->address);
514f746b
AR
8657}
8658
e7e0cddf
SS
8659/* Build a command list for the dprintf corresponding to the current
8660 settings of the dprintf style options. */
8661
8662static void
8663update_dprintf_command_list (struct breakpoint *b)
8664{
8665 char *dprintf_args = b->extra_string;
8666 char *printf_line = NULL;
8667
8668 if (!dprintf_args)
8669 return;
8670
8671 dprintf_args = skip_spaces (dprintf_args);
8672
8673 /* Allow a comma, as it may have terminated a location, but don't
8674 insist on it. */
8675 if (*dprintf_args == ',')
8676 ++dprintf_args;
8677 dprintf_args = skip_spaces (dprintf_args);
8678
8679 if (*dprintf_args != '"')
8680 error (_("Bad format string, missing '\"'."));
8681
d3ce09f5 8682 if (strcmp (dprintf_style, dprintf_style_gdb) == 0)
e7e0cddf 8683 printf_line = xstrprintf ("printf %s", dprintf_args);
d3ce09f5 8684 else if (strcmp (dprintf_style, dprintf_style_call) == 0)
e7e0cddf
SS
8685 {
8686 if (!dprintf_function)
8687 error (_("No function supplied for dprintf call"));
8688
8689 if (dprintf_channel && strlen (dprintf_channel) > 0)
8690 printf_line = xstrprintf ("call (void) %s (%s,%s)",
8691 dprintf_function,
8692 dprintf_channel,
8693 dprintf_args);
8694 else
8695 printf_line = xstrprintf ("call (void) %s (%s)",
8696 dprintf_function,
8697 dprintf_args);
8698 }
d3ce09f5
SS
8699 else if (strcmp (dprintf_style, dprintf_style_agent) == 0)
8700 {
8701 if (target_can_run_breakpoint_commands ())
8702 printf_line = xstrprintf ("agent-printf %s", dprintf_args);
8703 else
8704 {
8705 warning (_("Target cannot run dprintf commands, falling back to GDB printf"));
8706 printf_line = xstrprintf ("printf %s", dprintf_args);
8707 }
8708 }
e7e0cddf
SS
8709 else
8710 internal_error (__FILE__, __LINE__,
8711 _("Invalid dprintf style."));
8712
f28045c2 8713 gdb_assert (printf_line != NULL);
e7e0cddf 8714
12973681
TT
8715 /* Manufacture a printf sequence. */
8716 struct command_line *printf_cmd_line
8717 = new struct command_line (simple_control, printf_line);
8718 breakpoint_set_commands (b, counted_command_line (printf_cmd_line,
8719 command_lines_deleter ()));
e7e0cddf
SS
8720}
8721
8722/* Update all dprintf commands, making their command lists reflect
8723 current style settings. */
8724
8725static void
eb4c3f4a 8726update_dprintf_commands (const char *args, int from_tty,
e7e0cddf
SS
8727 struct cmd_list_element *c)
8728{
8729 struct breakpoint *b;
8730
8731 ALL_BREAKPOINTS (b)
8732 {
8733 if (b->type == bp_dprintf)
8734 update_dprintf_command_list (b);
8735 }
8736}
c3f6f71d 8737
f00aae0f
KS
8738/* Create a breakpoint with SAL as location. Use LOCATION
8739 as a description of the location, and COND_STRING
b35a8b2f
DE
8740 as condition expression. If LOCATION is NULL then create an
8741 "address location" from the address in the SAL. */
018d34a4
VP
8742
8743static void
d9b3f62e 8744init_breakpoint_sal (struct breakpoint *b, struct gdbarch *gdbarch,
6c5b2ebe 8745 gdb::array_view<const symtab_and_line> sals,
ffc2605c 8746 event_location_up &&location,
e1e01040
PA
8747 gdb::unique_xmalloc_ptr<char> filter,
8748 gdb::unique_xmalloc_ptr<char> cond_string,
8749 gdb::unique_xmalloc_ptr<char> extra_string,
d9b3f62e
PA
8750 enum bptype type, enum bpdisp disposition,
8751 int thread, int task, int ignore_count,
c0a91b2b 8752 const struct breakpoint_ops *ops, int from_tty,
44f238bb
PA
8753 int enabled, int internal, unsigned flags,
8754 int display_canonical)
018d34a4 8755{
0d381245 8756 int i;
018d34a4
VP
8757
8758 if (type == bp_hardware_breakpoint)
8759 {
fbbd034e
AS
8760 int target_resources_ok;
8761
8762 i = hw_breakpoint_used_count ();
8763 target_resources_ok =
8764 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
018d34a4
VP
8765 i + 1, 0);
8766 if (target_resources_ok == 0)
8767 error (_("No hardware breakpoint support in the target."));
8768 else if (target_resources_ok < 0)
8769 error (_("Hardware breakpoints used exceeds limit."));
8770 }
8771
6c5b2ebe 8772 gdb_assert (!sals.empty ());
6c95b8df 8773
6c5b2ebe 8774 for (const auto &sal : sals)
0d381245 8775 {
0d381245
VP
8776 struct bp_location *loc;
8777
8778 if (from_tty)
5af949e3
UW
8779 {
8780 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
8781 if (!loc_gdbarch)
8782 loc_gdbarch = gdbarch;
8783
8784 describe_other_breakpoints (loc_gdbarch,
6c95b8df 8785 sal.pspace, sal.pc, sal.section, thread);
5af949e3 8786 }
0d381245 8787
6c5b2ebe 8788 if (&sal == &sals[0])
0d381245 8789 {
d9b3f62e 8790 init_raw_breakpoint (b, gdbarch, sal, type, ops);
0d381245 8791 b->thread = thread;
4a306c9a 8792 b->task = task;
855a6e68 8793
e1e01040
PA
8794 b->cond_string = cond_string.release ();
8795 b->extra_string = extra_string.release ();
0d381245 8796 b->ignore_count = ignore_count;
41447f92 8797 b->enable_state = enabled ? bp_enabled : bp_disabled;
0d381245 8798 b->disposition = disposition;
6c95b8df 8799
44f238bb
PA
8800 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
8801 b->loc->inserted = 1;
8802
0fb4aa4b
PA
8803 if (type == bp_static_tracepoint)
8804 {
d9b3f62e 8805 struct tracepoint *t = (struct tracepoint *) b;
0fb4aa4b
PA
8806 struct static_tracepoint_marker marker;
8807
983af33b 8808 if (strace_marker_p (b))
0fb4aa4b
PA
8809 {
8810 /* We already know the marker exists, otherwise, we
8811 wouldn't see a sal for it. */
d28cd78a
TT
8812 const char *p
8813 = &event_location_to_string (b->location.get ())[3];
f00aae0f 8814 const char *endp;
0fb4aa4b 8815
f1735a53 8816 p = skip_spaces (p);
0fb4aa4b 8817
f1735a53 8818 endp = skip_to_space (p);
0fb4aa4b 8819
5d9310c4 8820 t->static_trace_marker_id.assign (p, endp - p);
0fb4aa4b 8821
3e43a32a
MS
8822 printf_filtered (_("Probed static tracepoint "
8823 "marker \"%s\"\n"),
5d9310c4 8824 t->static_trace_marker_id.c_str ());
0fb4aa4b
PA
8825 }
8826 else if (target_static_tracepoint_marker_at (sal.pc, &marker))
8827 {
5d9310c4 8828 t->static_trace_marker_id = std::move (marker.str_id);
0fb4aa4b 8829
3e43a32a
MS
8830 printf_filtered (_("Probed static tracepoint "
8831 "marker \"%s\"\n"),
5d9310c4 8832 t->static_trace_marker_id.c_str ());
0fb4aa4b
PA
8833 }
8834 else
3e43a32a
MS
8835 warning (_("Couldn't determine the static "
8836 "tracepoint marker to probe"));
0fb4aa4b
PA
8837 }
8838
0d381245
VP
8839 loc = b->loc;
8840 }
8841 else
018d34a4 8842 {
39d61571 8843 loc = add_location_to_breakpoint (b, &sal);
44f238bb
PA
8844 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
8845 loc->inserted = 1;
0d381245
VP
8846 }
8847
8848 if (b->cond_string)
8849 {
bbc13ae3
KS
8850 const char *arg = b->cond_string;
8851
1bb9788d
TT
8852 loc->cond = parse_exp_1 (&arg, loc->address,
8853 block_for_pc (loc->address), 0);
0d381245 8854 if (*arg)
588ae58c 8855 error (_("Garbage '%s' follows condition"), arg);
018d34a4 8856 }
e7e0cddf
SS
8857
8858 /* Dynamic printf requires and uses additional arguments on the
8859 command line, otherwise it's an error. */
8860 if (type == bp_dprintf)
8861 {
8862 if (b->extra_string)
8863 update_dprintf_command_list (b);
8864 else
8865 error (_("Format string required"));
8866 }
8867 else if (b->extra_string)
588ae58c 8868 error (_("Garbage '%s' at end of command"), b->extra_string);
855a6e68 8869 }
018d34a4 8870
56435ebe 8871 b->display_canonical = display_canonical;
f00aae0f 8872 if (location != NULL)
d28cd78a 8873 b->location = std::move (location);
018d34a4 8874 else
d28cd78a 8875 b->location = new_address_location (b->loc->address, NULL, 0);
e1e01040 8876 b->filter = filter.release ();
d9b3f62e 8877}
018d34a4 8878
d9b3f62e
PA
8879static void
8880create_breakpoint_sal (struct gdbarch *gdbarch,
6c5b2ebe 8881 gdb::array_view<const symtab_and_line> sals,
ffc2605c 8882 event_location_up &&location,
e1e01040
PA
8883 gdb::unique_xmalloc_ptr<char> filter,
8884 gdb::unique_xmalloc_ptr<char> cond_string,
8885 gdb::unique_xmalloc_ptr<char> extra_string,
d9b3f62e
PA
8886 enum bptype type, enum bpdisp disposition,
8887 int thread, int task, int ignore_count,
c0a91b2b 8888 const struct breakpoint_ops *ops, int from_tty,
44f238bb
PA
8889 int enabled, int internal, unsigned flags,
8890 int display_canonical)
d9b3f62e 8891{
a5e364af 8892 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (type);
d9b3f62e 8893
a5e364af 8894 init_breakpoint_sal (b.get (), gdbarch,
ffc2605c 8895 sals, std::move (location),
e1e01040
PA
8896 std::move (filter),
8897 std::move (cond_string),
8898 std::move (extra_string),
d9b3f62e
PA
8899 type, disposition,
8900 thread, task, ignore_count,
8901 ops, from_tty,
44f238bb
PA
8902 enabled, internal, flags,
8903 display_canonical);
d9b3f62e 8904
b270e6f9 8905 install_breakpoint (internal, std::move (b), 0);
018d34a4
VP
8906}
8907
8908/* Add SALS.nelts breakpoints to the breakpoint table. For each
8909 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
8910 value. COND_STRING, if not NULL, specified the condition to be
8911 used for all breakpoints. Essentially the only case where
8912 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
8913 function. In that case, it's still not possible to specify
8914 separate conditions for different overloaded functions, so
8915 we take just a single condition string.
8916
c3f6f71d 8917 NOTE: If the function succeeds, the caller is expected to cleanup
018d34a4 8918 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
c3f6f71d
JM
8919 array contents). If the function fails (error() is called), the
8920 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
4a64f543 8921 COND and SALS arrays and each of those arrays contents. */
c906108c
SS
8922
8923static void
8cdf0e15 8924create_breakpoints_sal (struct gdbarch *gdbarch,
7efd8fc2 8925 struct linespec_result *canonical,
e1e01040
PA
8926 gdb::unique_xmalloc_ptr<char> cond_string,
8927 gdb::unique_xmalloc_ptr<char> extra_string,
8cdf0e15
VP
8928 enum bptype type, enum bpdisp disposition,
8929 int thread, int task, int ignore_count,
c0a91b2b 8930 const struct breakpoint_ops *ops, int from_tty,
44f238bb 8931 int enabled, int internal, unsigned flags)
c906108c 8932{
f8eba3c6 8933 if (canonical->pre_expanded)
6c5b2ebe 8934 gdb_assert (canonical->lsals.size () == 1);
f8eba3c6 8935
6c5b2ebe 8936 for (const auto &lsal : canonical->lsals)
c3f6f71d 8937 {
f00aae0f 8938 /* Note that 'location' can be NULL in the case of a plain
f8eba3c6 8939 'break', without arguments. */
ffc2605c 8940 event_location_up location
f00aae0f 8941 = (canonical->location != NULL
8e9e35b1 8942 ? copy_event_location (canonical->location.get ()) : NULL);
e1e01040 8943 gdb::unique_xmalloc_ptr<char> filter_string
6c5b2ebe 8944 (lsal.canonical != NULL ? xstrdup (lsal.canonical) : NULL);
0d381245 8945
6c5b2ebe 8946 create_breakpoint_sal (gdbarch, lsal.sals,
ffc2605c 8947 std::move (location),
e1e01040
PA
8948 std::move (filter_string),
8949 std::move (cond_string),
8950 std::move (extra_string),
e7e0cddf 8951 type, disposition,
84f4c1fe 8952 thread, task, ignore_count, ops,
44f238bb 8953 from_tty, enabled, internal, flags,
56435ebe 8954 canonical->special_display);
c3f6f71d 8955 }
c3f6f71d 8956}
c906108c 8957
f00aae0f 8958/* Parse LOCATION which is assumed to be a SAL specification possibly
c3f6f71d 8959 followed by conditionals. On return, SALS contains an array of SAL
f00aae0f
KS
8960 addresses found. LOCATION points to the end of the SAL (for
8961 linespec locations).
9998af43
TJB
8962
8963 The array and the line spec strings are allocated on the heap, it is
8964 the caller's responsibility to free them. */
c906108c 8965
b9362cc7 8966static void
f00aae0f 8967parse_breakpoint_sals (const struct event_location *location,
58438ac1 8968 struct linespec_result *canonical)
c3f6f71d 8969{
f00aae0f
KS
8970 struct symtab_and_line cursal;
8971
8972 if (event_location_type (location) == LINESPEC_LOCATION)
8973 {
a20714ff 8974 const char *spec = get_linespec_location (location)->spec_string;
f00aae0f 8975
a20714ff 8976 if (spec == NULL)
f00aae0f
KS
8977 {
8978 /* The last displayed codepoint, if it's valid, is our default
8979 breakpoint address. */
8980 if (last_displayed_sal_is_valid ())
8981 {
f00aae0f
KS
8982 /* Set sal's pspace, pc, symtab, and line to the values
8983 corresponding to the last call to print_frame_info.
8984 Be sure to reinitialize LINE with NOTCURRENT == 0
8985 as the breakpoint line number is inappropriate otherwise.
8986 find_pc_line would adjust PC, re-set it back. */
51abb421
PA
8987 symtab_and_line sal = get_last_displayed_sal ();
8988 CORE_ADDR pc = sal.pc;
8989
f00aae0f
KS
8990 sal = find_pc_line (pc, 0);
8991
8992 /* "break" without arguments is equivalent to "break *PC"
8993 where PC is the last displayed codepoint's address. So
8994 make sure to set sal.explicit_pc to prevent GDB from
8995 trying to expand the list of sals to include all other
8996 instances with the same symtab and line. */
8997 sal.pc = pc;
8998 sal.explicit_pc = 1;
8999
6c5b2ebe
PA
9000 struct linespec_sals lsal;
9001 lsal.sals = {sal};
f00aae0f
KS
9002 lsal.canonical = NULL;
9003
6c5b2ebe 9004 canonical->lsals.push_back (std::move (lsal));
f00aae0f
KS
9005 return;
9006 }
9007 else
9008 error (_("No default breakpoint address now."));
c906108c 9009 }
c906108c 9010 }
f00aae0f
KS
9011
9012 /* Force almost all breakpoints to be in terms of the
9013 current_source_symtab (which is decode_line_1's default).
9014 This should produce the results we want almost all of the
9015 time while leaving default_breakpoint_* alone.
9016
9017 ObjC: However, don't match an Objective-C method name which
9018 may have a '+' or '-' succeeded by a '['. */
9019 cursal = get_current_source_symtab_and_line ();
9020 if (last_displayed_sal_is_valid ())
c906108c 9021 {
a20714ff 9022 const char *spec = NULL;
cc80f267 9023
f00aae0f 9024 if (event_location_type (location) == LINESPEC_LOCATION)
a20714ff 9025 spec = get_linespec_location (location)->spec_string;
cc80f267 9026
f00aae0f 9027 if (!cursal.symtab
a20714ff
PA
9028 || (spec != NULL
9029 && strchr ("+-", spec[0]) != NULL
9030 && spec[1] != '['))
f00aae0f 9031 {
c2f4122d 9032 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
f00aae0f
KS
9033 get_last_displayed_symtab (),
9034 get_last_displayed_line (),
9035 canonical, NULL, NULL);
9036 return;
9037 }
c906108c 9038 }
f00aae0f 9039
c2f4122d 9040 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
f00aae0f 9041 cursal.symtab, cursal.line, canonical, NULL, NULL);
c3f6f71d 9042}
c906108c 9043
c906108c 9044
c3f6f71d 9045/* Convert each SAL into a real PC. Verify that the PC can be
4a64f543 9046 inserted as a breakpoint. If it can't throw an error. */
c906108c 9047
b9362cc7 9048static void
6c5b2ebe 9049breakpoint_sals_to_pc (std::vector<symtab_and_line> &sals)
c3f6f71d 9050{
6c5b2ebe
PA
9051 for (auto &sal : sals)
9052 resolve_sal_pc (&sal);
c3f6f71d
JM
9053}
9054
7a697b8d
SS
9055/* Fast tracepoints may have restrictions on valid locations. For
9056 instance, a fast tracepoint using a jump instead of a trap will
9057 likely have to overwrite more bytes than a trap would, and so can
9058 only be placed where the instruction is longer than the jump, or a
9059 multi-instruction sequence does not have a jump into the middle of
9060 it, etc. */
9061
9062static void
9063check_fast_tracepoint_sals (struct gdbarch *gdbarch,
6c5b2ebe 9064 gdb::array_view<const symtab_and_line> sals)
7a697b8d 9065{
6c5b2ebe 9066 for (const auto &sal : sals)
7a697b8d 9067 {
f8eba3c6
TT
9068 struct gdbarch *sarch;
9069
6c5b2ebe 9070 sarch = get_sal_arch (sal);
f8eba3c6
TT
9071 /* We fall back to GDBARCH if there is no architecture
9072 associated with SAL. */
9073 if (sarch == NULL)
9074 sarch = gdbarch;
281d762b
TT
9075 std::string msg;
9076 if (!gdbarch_fast_tracepoint_valid_at (sarch, sal.pc, &msg))
53c3572a 9077 error (_("May not have a fast tracepoint at %s%s"),
281d762b 9078 paddress (sarch, sal.pc), msg.c_str ());
7a697b8d
SS
9079 }
9080}
9081
018d34a4
VP
9082/* Given TOK, a string specification of condition and thread, as
9083 accepted by the 'break' command, extract the condition
9084 string and thread number and set *COND_STRING and *THREAD.
4a64f543 9085 PC identifies the context at which the condition should be parsed.
018d34a4
VP
9086 If no condition is found, *COND_STRING is set to NULL.
9087 If no thread is found, *THREAD is set to -1. */
d634f2de
JB
9088
9089static void
bbc13ae3 9090find_condition_and_thread (const char *tok, CORE_ADDR pc,
e7e0cddf
SS
9091 char **cond_string, int *thread, int *task,
9092 char **rest)
018d34a4
VP
9093{
9094 *cond_string = NULL;
9095 *thread = -1;
ed1d1739
KS
9096 *task = 0;
9097 *rest = NULL;
9098
018d34a4
VP
9099 while (tok && *tok)
9100 {
bbc13ae3 9101 const char *end_tok;
018d34a4 9102 int toklen;
bbc13ae3
KS
9103 const char *cond_start = NULL;
9104 const char *cond_end = NULL;
cc59ec59 9105
f1735a53 9106 tok = skip_spaces (tok);
e7e0cddf
SS
9107
9108 if ((*tok == '"' || *tok == ',') && rest)
9109 {
9110 *rest = savestring (tok, strlen (tok));
9111 return;
9112 }
9113
f1735a53 9114 end_tok = skip_to_space (tok);
d634f2de 9115
018d34a4 9116 toklen = end_tok - tok;
d634f2de 9117
018d34a4
VP
9118 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
9119 {
9120 tok = cond_start = end_tok + 1;
4d01a485 9121 parse_exp_1 (&tok, pc, block_for_pc (pc), 0);
018d34a4 9122 cond_end = tok;
d634f2de 9123 *cond_string = savestring (cond_start, cond_end - cond_start);
018d34a4
VP
9124 }
9125 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
9126 {
5d5658a1
PA
9127 const char *tmptok;
9128 struct thread_info *thr;
d634f2de 9129
018d34a4 9130 tok = end_tok + 1;
5d5658a1 9131 thr = parse_thread_id (tok, &tmptok);
018d34a4
VP
9132 if (tok == tmptok)
9133 error (_("Junk after thread keyword."));
5d5658a1 9134 *thread = thr->global_num;
bbc13ae3 9135 tok = tmptok;
018d34a4 9136 }
4a306c9a
JB
9137 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
9138 {
9139 char *tmptok;
9140
9141 tok = end_tok + 1;
bbc13ae3 9142 *task = strtol (tok, &tmptok, 0);
4a306c9a
JB
9143 if (tok == tmptok)
9144 error (_("Junk after task keyword."));
9145 if (!valid_task_id (*task))
b6199126 9146 error (_("Unknown task %d."), *task);
bbc13ae3 9147 tok = tmptok;
4a306c9a 9148 }
e7e0cddf
SS
9149 else if (rest)
9150 {
9151 *rest = savestring (tok, strlen (tok));
ccab2054 9152 return;
e7e0cddf 9153 }
018d34a4
VP
9154 else
9155 error (_("Junk at end of arguments."));
9156 }
9157}
9158
0fb4aa4b
PA
9159/* Decode a static tracepoint marker spec. */
9160
6c5b2ebe 9161static std::vector<symtab_and_line>
f00aae0f 9162decode_static_tracepoint_spec (const char **arg_p)
0fb4aa4b 9163{
f00aae0f
KS
9164 const char *p = &(*arg_p)[3];
9165 const char *endp;
0fb4aa4b 9166
f1735a53 9167 p = skip_spaces (p);
0fb4aa4b 9168
f1735a53 9169 endp = skip_to_space (p);
0fb4aa4b 9170
81b1e71c 9171 std::string marker_str (p, endp - p);
0fb4aa4b 9172
5d9310c4
SM
9173 std::vector<static_tracepoint_marker> markers
9174 = target_static_tracepoint_markers_by_strid (marker_str.c_str ());
9175 if (markers.empty ())
81b1e71c
TT
9176 error (_("No known static tracepoint marker named %s"),
9177 marker_str.c_str ());
0fb4aa4b 9178
6c5b2ebe 9179 std::vector<symtab_and_line> sals;
5d9310c4 9180 sals.reserve (markers.size ());
0fb4aa4b 9181
5d9310c4 9182 for (const static_tracepoint_marker &marker : markers)
0fb4aa4b 9183 {
5d9310c4
SM
9184 symtab_and_line sal = find_pc_line (marker.address, 0);
9185 sal.pc = marker.address;
6c5b2ebe 9186 sals.push_back (sal);
5d9310c4 9187 }
0fb4aa4b 9188
0fb4aa4b
PA
9189 *arg_p = endp;
9190 return sals;
9191}
9192
f00aae0f 9193/* See breakpoint.h. */
0101ce28 9194
8cdf0e15
VP
9195int
9196create_breakpoint (struct gdbarch *gdbarch,
e1e01040
PA
9197 const struct event_location *location,
9198 const char *cond_string,
9199 int thread, const char *extra_string,
f00aae0f 9200 int parse_extra,
0fb4aa4b 9201 int tempflag, enum bptype type_wanted,
8cdf0e15
VP
9202 int ignore_count,
9203 enum auto_boolean pending_break_support,
c0a91b2b 9204 const struct breakpoint_ops *ops,
44f238bb
PA
9205 int from_tty, int enabled, int internal,
9206 unsigned flags)
c3f6f71d 9207{
7efd8fc2 9208 struct linespec_result canonical;
0101ce28 9209 int pending = 0;
4a306c9a 9210 int task = 0;
86b17b60 9211 int prev_bkpt_count = breakpoint_count;
c3f6f71d 9212
348d480f
PA
9213 gdb_assert (ops != NULL);
9214
f00aae0f
KS
9215 /* If extra_string isn't useful, set it to NULL. */
9216 if (extra_string != NULL && *extra_string == '\0')
9217 extra_string = NULL;
9218
a70b8144 9219 try
b78a6381 9220 {
f00aae0f 9221 ops->create_sals_from_location (location, &canonical, type_wanted);
b78a6381 9222 }
230d2906 9223 catch (const gdb_exception_error &e)
0101ce28 9224 {
492d29ea
PA
9225 /* If caller is interested in rc value from parse, set
9226 value. */
9227 if (e.error == NOT_FOUND_ERROR)
0101ce28 9228 {
05ff989b
AC
9229 /* If pending breakpoint support is turned off, throw
9230 error. */
fa8d40ab
JJ
9231
9232 if (pending_break_support == AUTO_BOOLEAN_FALSE)
eedc3f4f 9233 throw;
723a2275
VP
9234
9235 exception_print (gdb_stderr, e);
fa8d40ab 9236
05ff989b
AC
9237 /* If pending breakpoint support is auto query and the user
9238 selects no, then simply return the error code. */
059fb39f 9239 if (pending_break_support == AUTO_BOOLEAN_AUTO
bfccc43c
YQ
9240 && !nquery (_("Make %s pending on future shared library load? "),
9241 bptype_string (type_wanted)))
fd9b8c24 9242 return 0;
fa8d40ab 9243
05ff989b
AC
9244 /* At this point, either the user was queried about setting
9245 a pending breakpoint and selected yes, or pending
9246 breakpoint behavior is on and thus a pending breakpoint
9247 is defaulted on behalf of the user. */
f00aae0f 9248 pending = 1;
0101ce28 9249 }
492d29ea 9250 else
eedc3f4f 9251 throw;
0101ce28 9252 }
492d29ea 9253
6c5b2ebe 9254 if (!pending && canonical.lsals.empty ())
492d29ea 9255 return 0;
c3f6f71d 9256
c3f6f71d
JM
9257 /* Resolve all line numbers to PC's and verify that the addresses
9258 are ok for the target. */
0101ce28 9259 if (!pending)
f8eba3c6 9260 {
6c5b2ebe
PA
9261 for (auto &lsal : canonical.lsals)
9262 breakpoint_sals_to_pc (lsal.sals);
f8eba3c6 9263 }
c3f6f71d 9264
7a697b8d 9265 /* Fast tracepoints may have additional restrictions on location. */
bfccc43c 9266 if (!pending && type_wanted == bp_fast_tracepoint)
f8eba3c6 9267 {
6c5b2ebe
PA
9268 for (const auto &lsal : canonical.lsals)
9269 check_fast_tracepoint_sals (gdbarch, lsal.sals);
f8eba3c6 9270 }
7a697b8d 9271
c3f6f71d
JM
9272 /* Verify that condition can be parsed, before setting any
9273 breakpoints. Allocate a separate condition expression for each
4a64f543 9274 breakpoint. */
0101ce28 9275 if (!pending)
c3f6f71d 9276 {
e1e01040
PA
9277 gdb::unique_xmalloc_ptr<char> cond_string_copy;
9278 gdb::unique_xmalloc_ptr<char> extra_string_copy;
9279
f00aae0f 9280 if (parse_extra)
72b2ff0e 9281 {
0878d0fa 9282 char *rest;
e1e01040 9283 char *cond;
52d361e1 9284
6c5b2ebe 9285 const linespec_sals &lsal = canonical.lsals[0];
52d361e1 9286
0878d0fa
YQ
9287 /* Here we only parse 'arg' to separate condition
9288 from thread number, so parsing in context of first
9289 sal is OK. When setting the breakpoint we'll
9290 re-parse it in context of each sal. */
9291
6c5b2ebe 9292 find_condition_and_thread (extra_string, lsal.sals[0].pc,
e1e01040
PA
9293 &cond, &thread, &task, &rest);
9294 cond_string_copy.reset (cond);
9295 extra_string_copy.reset (rest);
72b2ff0e 9296 }
2f069f6f 9297 else
72b2ff0e 9298 {
f00aae0f
KS
9299 if (type_wanted != bp_dprintf
9300 && extra_string != NULL && *extra_string != '\0')
9301 error (_("Garbage '%s' at end of location"), extra_string);
0878d0fa
YQ
9302
9303 /* Create a private copy of condition string. */
9304 if (cond_string)
e1e01040 9305 cond_string_copy.reset (xstrdup (cond_string));
0878d0fa
YQ
9306 /* Create a private copy of any extra string. */
9307 if (extra_string)
e1e01040 9308 extra_string_copy.reset (xstrdup (extra_string));
72b2ff0e 9309 }
0fb4aa4b 9310
52d361e1 9311 ops->create_breakpoints_sal (gdbarch, &canonical,
e1e01040
PA
9312 std::move (cond_string_copy),
9313 std::move (extra_string_copy),
9314 type_wanted,
d9b3f62e
PA
9315 tempflag ? disp_del : disp_donttouch,
9316 thread, task, ignore_count, ops,
44f238bb 9317 from_tty, enabled, internal, flags);
c906108c 9318 }
0101ce28
JJ
9319 else
9320 {
a5e364af 9321 std::unique_ptr <breakpoint> b = new_breakpoint_from_type (type_wanted);
bfccc43c 9322
a5e364af 9323 init_raw_breakpoint_without_location (b.get (), gdbarch, type_wanted, ops);
d28cd78a 9324 b->location = copy_event_location (location);
bfccc43c 9325
f00aae0f
KS
9326 if (parse_extra)
9327 b->cond_string = NULL;
e12c7713
MK
9328 else
9329 {
9330 /* Create a private copy of condition string. */
e1e01040 9331 b->cond_string = cond_string != NULL ? xstrdup (cond_string) : NULL;
15630549 9332 b->thread = thread;
e12c7713 9333 }
f00aae0f
KS
9334
9335 /* Create a private copy of any extra string. */
e1e01040 9336 b->extra_string = extra_string != NULL ? xstrdup (extra_string) : NULL;
0101ce28 9337 b->ignore_count = ignore_count;
0101ce28 9338 b->disposition = tempflag ? disp_del : disp_donttouch;
0d381245 9339 b->condition_not_parsed = 1;
41447f92 9340 b->enable_state = enabled ? bp_enabled : bp_disabled;
cc72b2a2
KP
9341 if ((type_wanted != bp_breakpoint
9342 && type_wanted != bp_hardware_breakpoint) || thread != -1)
f8eba3c6 9343 b->pspace = current_program_space;
8bea4e01 9344
b270e6f9 9345 install_breakpoint (internal, std::move (b), 0);
0101ce28
JJ
9346 }
9347
6c5b2ebe 9348 if (canonical.lsals.size () > 1)
95a42b64 9349 {
3e43a32a
MS
9350 warning (_("Multiple breakpoints were set.\nUse the "
9351 "\"delete\" command to delete unwanted breakpoints."));
86b17b60 9352 prev_breakpoint_count = prev_bkpt_count;
95a42b64
TT
9353 }
9354
44702360 9355 update_global_location_list (UGLL_MAY_INSERT);
fd9b8c24
PA
9356
9357 return 1;
c3f6f71d 9358}
c906108c 9359
348d480f 9360/* Set a breakpoint.
72b2ff0e
VP
9361 ARG is a string describing breakpoint address,
9362 condition, and thread.
9363 FLAG specifies if a breakpoint is hardware on,
9364 and if breakpoint is temporary, using BP_HARDWARE_FLAG
9365 and BP_TEMPFLAG. */
348d480f 9366
98deb0da 9367static void
f2fc3015 9368break_command_1 (const char *arg, int flag, int from_tty)
c3f6f71d 9369{
72b2ff0e 9370 int tempflag = flag & BP_TEMPFLAG;
0fb4aa4b
PA
9371 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
9372 ? bp_hardware_breakpoint
9373 : bp_breakpoint);
55aa24fb 9374 struct breakpoint_ops *ops;
f00aae0f 9375
ffc2605c 9376 event_location_up location = string_to_event_location (&arg, current_language);
55aa24fb
SDJ
9377
9378 /* Matching breakpoints on probes. */
5b56227b 9379 if (location != NULL
ffc2605c 9380 && event_location_type (location.get ()) == PROBE_LOCATION)
55aa24fb
SDJ
9381 ops = &bkpt_probe_breakpoint_ops;
9382 else
9383 ops = &bkpt_breakpoint_ops;
c3f6f71d 9384
8cdf0e15 9385 create_breakpoint (get_current_arch (),
ffc2605c 9386 location.get (),
f00aae0f 9387 NULL, 0, arg, 1 /* parse arg */,
0fb4aa4b 9388 tempflag, type_wanted,
8cdf0e15
VP
9389 0 /* Ignore count */,
9390 pending_break_support,
55aa24fb 9391 ops,
8cdf0e15 9392 from_tty,
84f4c1fe 9393 1 /* enabled */,
44f238bb
PA
9394 0 /* internal */,
9395 0);
c906108c
SS
9396}
9397
c906108c
SS
9398/* Helper function for break_command_1 and disassemble_command. */
9399
9400void
fba45db2 9401resolve_sal_pc (struct symtab_and_line *sal)
c906108c
SS
9402{
9403 CORE_ADDR pc;
9404
9405 if (sal->pc == 0 && sal->symtab != NULL)
9406 {
9407 if (!find_line_pc (sal->symtab, sal->line, &pc))
8a3fe4f8 9408 error (_("No line %d in file \"%s\"."),
05cba821 9409 sal->line, symtab_to_filename_for_display (sal->symtab));
c906108c 9410 sal->pc = pc;
6a048695 9411
4a64f543
MS
9412 /* If this SAL corresponds to a breakpoint inserted using a line
9413 number, then skip the function prologue if necessary. */
6a048695 9414 if (sal->explicit_line)
059acae7 9415 skip_prologue_sal (sal);
c906108c
SS
9416 }
9417
9418 if (sal->section == 0 && sal->symtab != NULL)
9419 {
346d1dfe 9420 const struct blockvector *bv;
3977b71f 9421 const struct block *b;
c5aa993b 9422 struct symbol *sym;
c906108c 9423
43f3e411
DE
9424 bv = blockvector_for_pc_sect (sal->pc, 0, &b,
9425 SYMTAB_COMPUNIT (sal->symtab));
c906108c
SS
9426 if (bv != NULL)
9427 {
7f0df278 9428 sym = block_linkage_function (b);
c906108c
SS
9429 if (sym != NULL)
9430 {
eb822aa6
DE
9431 fixup_symbol_section (sym, SYMTAB_OBJFILE (sal->symtab));
9432 sal->section = SYMBOL_OBJ_SECTION (SYMTAB_OBJFILE (sal->symtab),
9433 sym);
c906108c
SS
9434 }
9435 else
9436 {
4a64f543
MS
9437 /* It really is worthwhile to have the section, so we'll
9438 just have to look harder. This case can be executed
9439 if we have line numbers but no functions (as can
9440 happen in assembly source). */
c906108c 9441
5ed8105e 9442 scoped_restore_current_pspace_and_thread restore_pspace_thread;
6c95b8df 9443 switch_to_program_space_and_thread (sal->pspace);
c906108c 9444
5ed8105e 9445 bound_minimal_symbol msym = lookup_minimal_symbol_by_pc (sal->pc);
7cbd4a93 9446 if (msym.minsym)
efd66ac6 9447 sal->section = MSYMBOL_OBJ_SECTION (msym.objfile, msym.minsym);
c906108c
SS
9448 }
9449 }
9450 }
9451}
9452
9453void
0b39b52e 9454break_command (const char *arg, int from_tty)
c906108c 9455{
db107f19 9456 break_command_1 (arg, 0, from_tty);
c906108c
SS
9457}
9458
c906108c 9459void
0b39b52e 9460tbreak_command (const char *arg, int from_tty)
c906108c 9461{
db107f19 9462 break_command_1 (arg, BP_TEMPFLAG, from_tty);
c906108c
SS
9463}
9464
c906108c 9465static void
0b39b52e 9466hbreak_command (const char *arg, int from_tty)
c906108c 9467{
db107f19 9468 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
c906108c
SS
9469}
9470
9471static void
0b39b52e 9472thbreak_command (const char *arg, int from_tty)
c906108c 9473{
db107f19 9474 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
c906108c
SS
9475}
9476
9477static void
ee7ddd71 9478stop_command (const char *arg, int from_tty)
c906108c 9479{
a3f17187 9480 printf_filtered (_("Specify the type of breakpoint to set.\n\
c906108c 9481Usage: stop in <function | address>\n\
a3f17187 9482 stop at <line>\n"));
c906108c
SS
9483}
9484
9485static void
4495129a 9486stopin_command (const char *arg, int from_tty)
c906108c
SS
9487{
9488 int badInput = 0;
9489
cafb3438 9490 if (arg == NULL)
c906108c
SS
9491 badInput = 1;
9492 else if (*arg != '*')
9493 {
4495129a 9494 const char *argptr = arg;
c906108c
SS
9495 int hasColon = 0;
9496
4a64f543 9497 /* Look for a ':'. If this is a line number specification, then
53a5351d 9498 say it is bad, otherwise, it should be an address or
4a64f543 9499 function/method name. */
c906108c 9500 while (*argptr && !hasColon)
c5aa993b
JM
9501 {
9502 hasColon = (*argptr == ':');
9503 argptr++;
9504 }
c906108c
SS
9505
9506 if (hasColon)
c5aa993b 9507 badInput = (*argptr != ':'); /* Not a class::method */
c906108c 9508 else
c5aa993b 9509 badInput = isdigit (*arg); /* a simple line number */
c906108c
SS
9510 }
9511
9512 if (badInput)
a3f17187 9513 printf_filtered (_("Usage: stop in <function | address>\n"));
c906108c 9514 else
db107f19 9515 break_command_1 (arg, 0, from_tty);
c906108c
SS
9516}
9517
9518static void
4495129a 9519stopat_command (const char *arg, int from_tty)
c906108c
SS
9520{
9521 int badInput = 0;
9522
cafb3438 9523 if (arg == NULL || *arg == '*') /* no line number */
c906108c
SS
9524 badInput = 1;
9525 else
9526 {
4495129a 9527 const char *argptr = arg;
c906108c
SS
9528 int hasColon = 0;
9529
4a64f543
MS
9530 /* Look for a ':'. If there is a '::' then get out, otherwise
9531 it is probably a line number. */
c906108c 9532 while (*argptr && !hasColon)
c5aa993b
JM
9533 {
9534 hasColon = (*argptr == ':');
9535 argptr++;
9536 }
c906108c
SS
9537
9538 if (hasColon)
c5aa993b 9539 badInput = (*argptr == ':'); /* we have class::method */
c906108c 9540 else
c5aa993b 9541 badInput = !isdigit (*arg); /* not a line number */
c906108c
SS
9542 }
9543
9544 if (badInput)
65e65158 9545 printf_filtered (_("Usage: stop at LINE\n"));
c906108c 9546 else
db107f19 9547 break_command_1 (arg, 0, from_tty);
c906108c
SS
9548}
9549
e7e0cddf
SS
9550/* The dynamic printf command is mostly like a regular breakpoint, but
9551 with a prewired command list consisting of a single output command,
9552 built from extra arguments supplied on the dprintf command
9553 line. */
9554
da821c7b 9555static void
0b39b52e 9556dprintf_command (const char *arg, int from_tty)
e7e0cddf 9557{
ffc2605c 9558 event_location_up location = string_to_event_location (&arg, current_language);
f00aae0f
KS
9559
9560 /* If non-NULL, ARG should have been advanced past the location;
9561 the next character must be ','. */
9562 if (arg != NULL)
9563 {
9564 if (arg[0] != ',' || arg[1] == '\0')
9565 error (_("Format string required"));
9566 else
9567 {
9568 /* Skip the comma. */
9569 ++arg;
9570 }
9571 }
9572
e7e0cddf 9573 create_breakpoint (get_current_arch (),
ffc2605c 9574 location.get (),
f00aae0f 9575 NULL, 0, arg, 1 /* parse arg */,
e7e0cddf
SS
9576 0, bp_dprintf,
9577 0 /* Ignore count */,
9578 pending_break_support,
9579 &dprintf_breakpoint_ops,
9580 from_tty,
9581 1 /* enabled */,
9582 0 /* internal */,
9583 0);
9584}
9585
d3ce09f5 9586static void
0b39b52e 9587agent_printf_command (const char *arg, int from_tty)
d3ce09f5
SS
9588{
9589 error (_("May only run agent-printf on the target"));
9590}
9591
f1310107
TJB
9592/* Implement the "breakpoint_hit" breakpoint_ops method for
9593 ranged breakpoints. */
9594
9595static int
9596breakpoint_hit_ranged_breakpoint (const struct bp_location *bl,
bd522513 9597 const address_space *aspace,
09ac7c10
TT
9598 CORE_ADDR bp_addr,
9599 const struct target_waitstatus *ws)
f1310107 9600{
09ac7c10 9601 if (ws->kind != TARGET_WAITKIND_STOPPED
a493e3e2 9602 || ws->value.sig != GDB_SIGNAL_TRAP)
09ac7c10
TT
9603 return 0;
9604
f1310107
TJB
9605 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
9606 bl->length, aspace, bp_addr);
9607}
9608
9609/* Implement the "resources_needed" breakpoint_ops method for
9610 ranged breakpoints. */
9611
9612static int
9613resources_needed_ranged_breakpoint (const struct bp_location *bl)
9614{
9615 return target_ranged_break_num_registers ();
9616}
9617
9618/* Implement the "print_it" breakpoint_ops method for
9619 ranged breakpoints. */
9620
9621static enum print_stop_action
348d480f 9622print_it_ranged_breakpoint (bpstat bs)
f1310107 9623{
348d480f 9624 struct breakpoint *b = bs->breakpoint_at;
f1310107 9625 struct bp_location *bl = b->loc;
79a45e25 9626 struct ui_out *uiout = current_uiout;
f1310107
TJB
9627
9628 gdb_assert (b->type == bp_hardware_breakpoint);
9629
9630 /* Ranged breakpoints have only one location. */
9631 gdb_assert (bl && bl->next == NULL);
9632
9633 annotate_breakpoint (b->number);
f303dbd6
PA
9634
9635 maybe_print_thread_hit_breakpoint (uiout);
9636
f1310107 9637 if (b->disposition == disp_del)
112e8700 9638 uiout->text ("Temporary ranged breakpoint ");
f1310107 9639 else
112e8700
SM
9640 uiout->text ("Ranged breakpoint ");
9641 if (uiout->is_mi_like_p ())
f1310107 9642 {
112e8700 9643 uiout->field_string ("reason",
f1310107 9644 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
112e8700 9645 uiout->field_string ("disp", bpdisp_text (b->disposition));
f1310107 9646 }
112e8700
SM
9647 uiout->field_int ("bkptno", b->number);
9648 uiout->text (", ");
f1310107
TJB
9649
9650 return PRINT_SRC_AND_LOC;
9651}
9652
9653/* Implement the "print_one" breakpoint_ops method for
9654 ranged breakpoints. */
9655
9656static void
9657print_one_ranged_breakpoint (struct breakpoint *b,
9658 struct bp_location **last_loc)
9659{
9660 struct bp_location *bl = b->loc;
9661 struct value_print_options opts;
79a45e25 9662 struct ui_out *uiout = current_uiout;
f1310107
TJB
9663
9664 /* Ranged breakpoints have only one location. */
9665 gdb_assert (bl && bl->next == NULL);
9666
9667 get_user_print_options (&opts);
9668
9669 if (opts.addressprint)
9670 /* We don't print the address range here, it will be printed later
9671 by print_one_detail_ranged_breakpoint. */
112e8700 9672 uiout->field_skip ("addr");
f1310107
TJB
9673 annotate_field (5);
9674 print_breakpoint_location (b, bl);
9675 *last_loc = bl;
9676}
9677
9678/* Implement the "print_one_detail" breakpoint_ops method for
9679 ranged breakpoints. */
9680
9681static void
9682print_one_detail_ranged_breakpoint (const struct breakpoint *b,
9683 struct ui_out *uiout)
9684{
9685 CORE_ADDR address_start, address_end;
9686 struct bp_location *bl = b->loc;
d7e74731 9687 string_file stb;
f1310107
TJB
9688
9689 gdb_assert (bl);
9690
9691 address_start = bl->address;
9692 address_end = address_start + bl->length - 1;
9693
112e8700 9694 uiout->text ("\taddress range: ");
d7e74731
PA
9695 stb.printf ("[%s, %s]",
9696 print_core_address (bl->gdbarch, address_start),
9697 print_core_address (bl->gdbarch, address_end));
112e8700
SM
9698 uiout->field_stream ("addr", stb);
9699 uiout->text ("\n");
f1310107
TJB
9700}
9701
9702/* Implement the "print_mention" breakpoint_ops method for
9703 ranged breakpoints. */
9704
9705static void
9706print_mention_ranged_breakpoint (struct breakpoint *b)
9707{
9708 struct bp_location *bl = b->loc;
79a45e25 9709 struct ui_out *uiout = current_uiout;
f1310107
TJB
9710
9711 gdb_assert (bl);
9712 gdb_assert (b->type == bp_hardware_breakpoint);
9713
2d33446d
TT
9714 uiout->message (_("Hardware assisted ranged breakpoint %d from %s to %s."),
9715 b->number, paddress (bl->gdbarch, bl->address),
9716 paddress (bl->gdbarch, bl->address + bl->length - 1));
f1310107
TJB
9717}
9718
9719/* Implement the "print_recreate" breakpoint_ops method for
9720 ranged breakpoints. */
9721
9722static void
9723print_recreate_ranged_breakpoint (struct breakpoint *b, struct ui_file *fp)
9724{
f00aae0f 9725 fprintf_unfiltered (fp, "break-range %s, %s",
d28cd78a
TT
9726 event_location_to_string (b->location.get ()),
9727 event_location_to_string (b->location_range_end.get ()));
d9b3f62e 9728 print_recreate_thread (b, fp);
f1310107
TJB
9729}
9730
9731/* The breakpoint_ops structure to be used in ranged breakpoints. */
9732
2060206e 9733static struct breakpoint_ops ranged_breakpoint_ops;
f1310107
TJB
9734
9735/* Find the address where the end of the breakpoint range should be
9736 placed, given the SAL of the end of the range. This is so that if
9737 the user provides a line number, the end of the range is set to the
9738 last instruction of the given line. */
9739
9740static CORE_ADDR
9741find_breakpoint_range_end (struct symtab_and_line sal)
9742{
9743 CORE_ADDR end;
9744
9745 /* If the user provided a PC value, use it. Otherwise,
9746 find the address of the end of the given location. */
9747 if (sal.explicit_pc)
9748 end = sal.pc;
9749 else
9750 {
9751 int ret;
9752 CORE_ADDR start;
9753
9754 ret = find_line_pc_range (sal, &start, &end);
9755 if (!ret)
9756 error (_("Could not find location of the end of the range."));
9757
9758 /* find_line_pc_range returns the start of the next line. */
9759 end--;
9760 }
9761
9762 return end;
9763}
9764
9765/* Implement the "break-range" CLI command. */
9766
9767static void
0b39b52e 9768break_range_command (const char *arg, int from_tty)
f1310107 9769{
f2fc3015 9770 const char *arg_start;
f1310107
TJB
9771 struct linespec_result canonical_start, canonical_end;
9772 int bp_count, can_use_bp, length;
9773 CORE_ADDR end;
9774 struct breakpoint *b;
f1310107
TJB
9775
9776 /* We don't support software ranged breakpoints. */
9777 if (target_ranged_break_num_registers () < 0)
9778 error (_("This target does not support hardware ranged breakpoints."));
9779
9780 bp_count = hw_breakpoint_used_count ();
9781 bp_count += target_ranged_break_num_registers ();
9782 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
9783 bp_count, 0);
9784 if (can_use_bp < 0)
9785 error (_("Hardware breakpoints used exceeds limit."));
9786
f8eba3c6 9787 arg = skip_spaces (arg);
f1310107
TJB
9788 if (arg == NULL || arg[0] == '\0')
9789 error(_("No address range specified."));
9790
f8eba3c6 9791 arg_start = arg;
ffc2605c
TT
9792 event_location_up start_location = string_to_event_location (&arg,
9793 current_language);
9794 parse_breakpoint_sals (start_location.get (), &canonical_start);
f1310107
TJB
9795
9796 if (arg[0] != ',')
9797 error (_("Too few arguments."));
6c5b2ebe 9798 else if (canonical_start.lsals.empty ())
f1310107 9799 error (_("Could not find location of the beginning of the range."));
f8eba3c6 9800
6c5b2ebe 9801 const linespec_sals &lsal_start = canonical_start.lsals[0];
f8eba3c6 9802
6c5b2ebe
PA
9803 if (canonical_start.lsals.size () > 1
9804 || lsal_start.sals.size () != 1)
f1310107
TJB
9805 error (_("Cannot create a ranged breakpoint with multiple locations."));
9806
6c5b2ebe 9807 const symtab_and_line &sal_start = lsal_start.sals[0];
81b1e71c 9808 std::string addr_string_start (arg_start, arg - arg_start);
f1310107
TJB
9809
9810 arg++; /* Skip the comma. */
f8eba3c6 9811 arg = skip_spaces (arg);
f1310107
TJB
9812
9813 /* Parse the end location. */
9814
f1310107
TJB
9815 arg_start = arg;
9816
f8eba3c6 9817 /* We call decode_line_full directly here instead of using
f1310107
TJB
9818 parse_breakpoint_sals because we need to specify the start location's
9819 symtab and line as the default symtab and line for the end of the
9820 range. This makes it possible to have ranges like "foo.c:27, +14",
9821 where +14 means 14 lines from the start location. */
ffc2605c
TT
9822 event_location_up end_location = string_to_event_location (&arg,
9823 current_language);
9824 decode_line_full (end_location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
f8eba3c6
TT
9825 sal_start.symtab, sal_start.line,
9826 &canonical_end, NULL, NULL);
9827
6c5b2ebe 9828 if (canonical_end.lsals.empty ())
f1310107 9829 error (_("Could not find location of the end of the range."));
f8eba3c6 9830
6c5b2ebe
PA
9831 const linespec_sals &lsal_end = canonical_end.lsals[0];
9832 if (canonical_end.lsals.size () > 1
9833 || lsal_end.sals.size () != 1)
f1310107
TJB
9834 error (_("Cannot create a ranged breakpoint with multiple locations."));
9835
6c5b2ebe 9836 const symtab_and_line &sal_end = lsal_end.sals[0];
f1310107
TJB
9837
9838 end = find_breakpoint_range_end (sal_end);
9839 if (sal_start.pc > end)
177b42fe 9840 error (_("Invalid address range, end precedes start."));
f1310107
TJB
9841
9842 length = end - sal_start.pc + 1;
9843 if (length < 0)
9844 /* Length overflowed. */
9845 error (_("Address range too large."));
9846 else if (length == 1)
9847 {
9848 /* This range is simple enough to be handled by
9849 the `hbreak' command. */
81b1e71c 9850 hbreak_command (&addr_string_start[0], 1);
f1310107
TJB
9851
9852 return;
9853 }
9854
9855 /* Now set up the breakpoint. */
9856 b = set_raw_breakpoint (get_current_arch (), sal_start,
348d480f 9857 bp_hardware_breakpoint, &ranged_breakpoint_ops);
f1310107
TJB
9858 set_breakpoint_count (breakpoint_count + 1);
9859 b->number = breakpoint_count;
9860 b->disposition = disp_donttouch;
d28cd78a
TT
9861 b->location = std::move (start_location);
9862 b->location_range_end = std::move (end_location);
f1310107
TJB
9863 b->loc->length = length;
9864
f1310107 9865 mention (b);
76727919 9866 gdb::observers::breakpoint_created.notify (b);
44702360 9867 update_global_location_list (UGLL_MAY_INSERT);
f1310107
TJB
9868}
9869
4a64f543
MS
9870/* Return non-zero if EXP is verified as constant. Returned zero
9871 means EXP is variable. Also the constant detection may fail for
9872 some constant expressions and in such case still falsely return
9873 zero. */
2e6e3d9c 9874
65d79d4b
SDJ
9875static int
9876watchpoint_exp_is_const (const struct expression *exp)
9877{
9878 int i = exp->nelts;
9879
9880 while (i > 0)
9881 {
9882 int oplenp, argsp;
9883
9884 /* We are only interested in the descriptor of each element. */
9885 operator_length (exp, i, &oplenp, &argsp);
9886 i -= oplenp;
9887
9888 switch (exp->elts[i].opcode)
9889 {
9890 case BINOP_ADD:
9891 case BINOP_SUB:
9892 case BINOP_MUL:
9893 case BINOP_DIV:
9894 case BINOP_REM:
9895 case BINOP_MOD:
9896 case BINOP_LSH:
9897 case BINOP_RSH:
9898 case BINOP_LOGICAL_AND:
9899 case BINOP_LOGICAL_OR:
9900 case BINOP_BITWISE_AND:
9901 case BINOP_BITWISE_IOR:
9902 case BINOP_BITWISE_XOR:
9903 case BINOP_EQUAL:
9904 case BINOP_NOTEQUAL:
9905 case BINOP_LESS:
9906 case BINOP_GTR:
9907 case BINOP_LEQ:
9908 case BINOP_GEQ:
9909 case BINOP_REPEAT:
9910 case BINOP_COMMA:
9911 case BINOP_EXP:
9912 case BINOP_MIN:
9913 case BINOP_MAX:
9914 case BINOP_INTDIV:
9915 case BINOP_CONCAT:
65d79d4b
SDJ
9916 case TERNOP_COND:
9917 case TERNOP_SLICE:
65d79d4b
SDJ
9918
9919 case OP_LONG:
edd079d9 9920 case OP_FLOAT:
65d79d4b
SDJ
9921 case OP_LAST:
9922 case OP_COMPLEX:
9923 case OP_STRING:
65d79d4b
SDJ
9924 case OP_ARRAY:
9925 case OP_TYPE:
608b4967
TT
9926 case OP_TYPEOF:
9927 case OP_DECLTYPE:
6e72ca20 9928 case OP_TYPEID:
65d79d4b
SDJ
9929 case OP_NAME:
9930 case OP_OBJC_NSSTRING:
9931
9932 case UNOP_NEG:
9933 case UNOP_LOGICAL_NOT:
9934 case UNOP_COMPLEMENT:
9935 case UNOP_ADDR:
9936 case UNOP_HIGH:
aeaa2474 9937 case UNOP_CAST:
9eaf6705
TT
9938
9939 case UNOP_CAST_TYPE:
9940 case UNOP_REINTERPRET_CAST:
9941 case UNOP_DYNAMIC_CAST:
4a64f543
MS
9942 /* Unary, binary and ternary operators: We have to check
9943 their operands. If they are constant, then so is the
9944 result of that operation. For instance, if A and B are
9945 determined to be constants, then so is "A + B".
9946
9947 UNOP_IND is one exception to the rule above, because the
9948 value of *ADDR is not necessarily a constant, even when
9949 ADDR is. */
65d79d4b
SDJ
9950 break;
9951
9952 case OP_VAR_VALUE:
9953 /* Check whether the associated symbol is a constant.
4a64f543 9954
65d79d4b 9955 We use SYMBOL_CLASS rather than TYPE_CONST because it's
4a64f543
MS
9956 possible that a buggy compiler could mark a variable as
9957 constant even when it is not, and TYPE_CONST would return
9958 true in this case, while SYMBOL_CLASS wouldn't.
9959
9960 We also have to check for function symbols because they
9961 are always constant. */
65d79d4b
SDJ
9962 {
9963 struct symbol *s = exp->elts[i + 2].symbol;
9964
9965 if (SYMBOL_CLASS (s) != LOC_BLOCK
9966 && SYMBOL_CLASS (s) != LOC_CONST
9967 && SYMBOL_CLASS (s) != LOC_CONST_BYTES)
9968 return 0;
9969 break;
9970 }
9971
9972 /* The default action is to return 0 because we are using
9973 the optimistic approach here: If we don't know something,
9974 then it is not a constant. */
9975 default:
9976 return 0;
9977 }
9978 }
9979
9980 return 1;
9981}
9982
c1fc2657 9983/* Watchpoint destructor. */
3a5c3e22 9984
c1fc2657 9985watchpoint::~watchpoint ()
3a5c3e22 9986{
c1fc2657
SM
9987 xfree (this->exp_string);
9988 xfree (this->exp_string_reparse);
3a5c3e22
PA
9989}
9990
348d480f
PA
9991/* Implement the "re_set" breakpoint_ops method for watchpoints. */
9992
9993static void
9994re_set_watchpoint (struct breakpoint *b)
9995{
3a5c3e22
PA
9996 struct watchpoint *w = (struct watchpoint *) b;
9997
348d480f
PA
9998 /* Watchpoint can be either on expression using entirely global
9999 variables, or it can be on local variables.
10000
10001 Watchpoints of the first kind are never auto-deleted, and even
10002 persist across program restarts. Since they can use variables
10003 from shared libraries, we need to reparse expression as libraries
10004 are loaded and unloaded.
10005
10006 Watchpoints on local variables can also change meaning as result
10007 of solib event. For example, if a watchpoint uses both a local
10008 and a global variables in expression, it's a local watchpoint,
10009 but unloading of a shared library will make the expression
10010 invalid. This is not a very common use case, but we still
10011 re-evaluate expression, to avoid surprises to the user.
10012
10013 Note that for local watchpoints, we re-evaluate it only if
10014 watchpoints frame id is still valid. If it's not, it means the
10015 watchpoint is out of scope and will be deleted soon. In fact,
10016 I'm not sure we'll ever be called in this case.
10017
10018 If a local watchpoint's frame id is still valid, then
3a5c3e22 10019 w->exp_valid_block is likewise valid, and we can safely use it.
348d480f 10020
3a5c3e22
PA
10021 Don't do anything about disabled watchpoints, since they will be
10022 reevaluated again when enabled. */
10023 update_watchpoint (w, 1 /* reparse */);
348d480f
PA
10024}
10025
77b06cd7
TJB
10026/* Implement the "insert" breakpoint_ops method for hardware watchpoints. */
10027
10028static int
10029insert_watchpoint (struct bp_location *bl)
10030{
3a5c3e22
PA
10031 struct watchpoint *w = (struct watchpoint *) bl->owner;
10032 int length = w->exact ? 1 : bl->length;
e09342b5
TJB
10033
10034 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
4d01a485 10035 w->cond_exp.get ());
77b06cd7
TJB
10036}
10037
10038/* Implement the "remove" breakpoint_ops method for hardware watchpoints. */
10039
10040static int
73971819 10041remove_watchpoint (struct bp_location *bl, enum remove_bp_reason reason)
77b06cd7 10042{
3a5c3e22
PA
10043 struct watchpoint *w = (struct watchpoint *) bl->owner;
10044 int length = w->exact ? 1 : bl->length;
e09342b5
TJB
10045
10046 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
4d01a485 10047 w->cond_exp.get ());
e09342b5
TJB
10048}
10049
e09342b5 10050static int
348d480f 10051breakpoint_hit_watchpoint (const struct bp_location *bl,
bd522513 10052 const address_space *aspace, CORE_ADDR bp_addr,
09ac7c10 10053 const struct target_waitstatus *ws)
e09342b5 10054{
348d480f 10055 struct breakpoint *b = bl->owner;
3a5c3e22 10056 struct watchpoint *w = (struct watchpoint *) b;
77b06cd7 10057
348d480f
PA
10058 /* Continuable hardware watchpoints are treated as non-existent if the
10059 reason we stopped wasn't a hardware watchpoint (we didn't stop on
10060 some data address). Otherwise gdb won't stop on a break instruction
10061 in the code (not from a breakpoint) when a hardware watchpoint has
10062 been defined. Also skip watchpoints which we know did not trigger
10063 (did not match the data address). */
10064 if (is_hardware_watchpoint (b)
3a5c3e22 10065 && w->watchpoint_triggered == watch_triggered_no)
348d480f 10066 return 0;
9c06b0b4 10067
348d480f 10068 return 1;
9c06b0b4
TJB
10069}
10070
348d480f
PA
10071static void
10072check_status_watchpoint (bpstat bs)
9c06b0b4 10073{
348d480f 10074 gdb_assert (is_watchpoint (bs->breakpoint_at));
9c06b0b4 10075
348d480f 10076 bpstat_check_watchpoint (bs);
9c06b0b4
TJB
10077}
10078
10079/* Implement the "resources_needed" breakpoint_ops method for
348d480f 10080 hardware watchpoints. */
9c06b0b4
TJB
10081
10082static int
348d480f 10083resources_needed_watchpoint (const struct bp_location *bl)
9c06b0b4 10084{
3a5c3e22
PA
10085 struct watchpoint *w = (struct watchpoint *) bl->owner;
10086 int length = w->exact? 1 : bl->length;
348d480f
PA
10087
10088 return target_region_ok_for_hw_watchpoint (bl->address, length);
9c06b0b4
TJB
10089}
10090
10091/* Implement the "works_in_software_mode" breakpoint_ops method for
348d480f 10092 hardware watchpoints. */
9c06b0b4
TJB
10093
10094static int
348d480f 10095works_in_software_mode_watchpoint (const struct breakpoint *b)
9c06b0b4 10096{
efa80663
PA
10097 /* Read and access watchpoints only work with hardware support. */
10098 return b->type == bp_watchpoint || b->type == bp_hardware_watchpoint;
9c06b0b4
TJB
10099}
10100
9c06b0b4 10101static enum print_stop_action
348d480f 10102print_it_watchpoint (bpstat bs)
9c06b0b4 10103{
348d480f 10104 struct breakpoint *b;
348d480f 10105 enum print_stop_action result;
3a5c3e22 10106 struct watchpoint *w;
79a45e25 10107 struct ui_out *uiout = current_uiout;
348d480f
PA
10108
10109 gdb_assert (bs->bp_location_at != NULL);
10110
348d480f 10111 b = bs->breakpoint_at;
3a5c3e22 10112 w = (struct watchpoint *) b;
348d480f 10113
f303dbd6
PA
10114 annotate_watchpoint (b->number);
10115 maybe_print_thread_hit_breakpoint (uiout);
10116
d7e74731
PA
10117 string_file stb;
10118
76f9c9cf 10119 gdb::optional<ui_out_emit_tuple> tuple_emitter;
9c06b0b4
TJB
10120 switch (b->type)
10121 {
348d480f 10122 case bp_watchpoint:
9c06b0b4 10123 case bp_hardware_watchpoint:
112e8700
SM
10124 if (uiout->is_mi_like_p ())
10125 uiout->field_string
10126 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
348d480f 10127 mention (b);
76f9c9cf 10128 tuple_emitter.emplace (uiout, "value");
112e8700 10129 uiout->text ("\nOld value = ");
850645cf 10130 watchpoint_value_print (bs->old_val.get (), &stb);
112e8700
SM
10131 uiout->field_stream ("old", stb);
10132 uiout->text ("\nNew value = ");
850645cf 10133 watchpoint_value_print (w->val.get (), &stb);
112e8700
SM
10134 uiout->field_stream ("new", stb);
10135 uiout->text ("\n");
348d480f
PA
10136 /* More than one watchpoint may have been triggered. */
10137 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10138 break;
10139
10140 case bp_read_watchpoint:
112e8700
SM
10141 if (uiout->is_mi_like_p ())
10142 uiout->field_string
10143 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
348d480f 10144 mention (b);
76f9c9cf 10145 tuple_emitter.emplace (uiout, "value");
112e8700 10146 uiout->text ("\nValue = ");
850645cf 10147 watchpoint_value_print (w->val.get (), &stb);
112e8700
SM
10148 uiout->field_stream ("value", stb);
10149 uiout->text ("\n");
348d480f 10150 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10151 break;
10152
10153 case bp_access_watchpoint:
348d480f
PA
10154 if (bs->old_val != NULL)
10155 {
112e8700
SM
10156 if (uiout->is_mi_like_p ())
10157 uiout->field_string
10158 ("reason",
348d480f
PA
10159 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10160 mention (b);
76f9c9cf 10161 tuple_emitter.emplace (uiout, "value");
112e8700 10162 uiout->text ("\nOld value = ");
850645cf 10163 watchpoint_value_print (bs->old_val.get (), &stb);
112e8700
SM
10164 uiout->field_stream ("old", stb);
10165 uiout->text ("\nNew value = ");
348d480f
PA
10166 }
10167 else
10168 {
10169 mention (b);
112e8700
SM
10170 if (uiout->is_mi_like_p ())
10171 uiout->field_string
10172 ("reason",
348d480f 10173 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
76f9c9cf 10174 tuple_emitter.emplace (uiout, "value");
112e8700 10175 uiout->text ("\nValue = ");
348d480f 10176 }
850645cf 10177 watchpoint_value_print (w->val.get (), &stb);
112e8700
SM
10178 uiout->field_stream ("new", stb);
10179 uiout->text ("\n");
348d480f 10180 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10181 break;
10182 default:
348d480f 10183 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10184 }
10185
348d480f
PA
10186 return result;
10187}
10188
10189/* Implement the "print_mention" breakpoint_ops method for hardware
10190 watchpoints. */
10191
10192static void
10193print_mention_watchpoint (struct breakpoint *b)
10194{
3a5c3e22 10195 struct watchpoint *w = (struct watchpoint *) b;
79a45e25 10196 struct ui_out *uiout = current_uiout;
46b9c129 10197 const char *tuple_name;
348d480f
PA
10198
10199 switch (b->type)
10200 {
10201 case bp_watchpoint:
112e8700 10202 uiout->text ("Watchpoint ");
46b9c129 10203 tuple_name = "wpt";
348d480f
PA
10204 break;
10205 case bp_hardware_watchpoint:
112e8700 10206 uiout->text ("Hardware watchpoint ");
46b9c129 10207 tuple_name = "wpt";
348d480f
PA
10208 break;
10209 case bp_read_watchpoint:
112e8700 10210 uiout->text ("Hardware read watchpoint ");
46b9c129 10211 tuple_name = "hw-rwpt";
348d480f
PA
10212 break;
10213 case bp_access_watchpoint:
112e8700 10214 uiout->text ("Hardware access (read/write) watchpoint ");
46b9c129 10215 tuple_name = "hw-awpt";
348d480f
PA
10216 break;
10217 default:
10218 internal_error (__FILE__, __LINE__,
10219 _("Invalid hardware watchpoint type."));
10220 }
10221
46b9c129 10222 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
112e8700
SM
10223 uiout->field_int ("number", b->number);
10224 uiout->text (": ");
10225 uiout->field_string ("exp", w->exp_string);
348d480f
PA
10226}
10227
10228/* Implement the "print_recreate" breakpoint_ops method for
10229 watchpoints. */
10230
10231static void
10232print_recreate_watchpoint (struct breakpoint *b, struct ui_file *fp)
10233{
3a5c3e22
PA
10234 struct watchpoint *w = (struct watchpoint *) b;
10235
348d480f
PA
10236 switch (b->type)
10237 {
10238 case bp_watchpoint:
10239 case bp_hardware_watchpoint:
10240 fprintf_unfiltered (fp, "watch");
10241 break;
10242 case bp_read_watchpoint:
10243 fprintf_unfiltered (fp, "rwatch");
10244 break;
10245 case bp_access_watchpoint:
10246 fprintf_unfiltered (fp, "awatch");
10247 break;
10248 default:
10249 internal_error (__FILE__, __LINE__,
10250 _("Invalid watchpoint type."));
10251 }
10252
3a5c3e22 10253 fprintf_unfiltered (fp, " %s", w->exp_string);
d9b3f62e 10254 print_recreate_thread (b, fp);
348d480f
PA
10255}
10256
427cd150
TT
10257/* Implement the "explains_signal" breakpoint_ops method for
10258 watchpoints. */
10259
47591c29 10260static int
427cd150
TT
10261explains_signal_watchpoint (struct breakpoint *b, enum gdb_signal sig)
10262{
10263 /* A software watchpoint cannot cause a signal other than
10264 GDB_SIGNAL_TRAP. */
10265 if (b->type == bp_watchpoint && sig != GDB_SIGNAL_TRAP)
47591c29 10266 return 0;
427cd150 10267
47591c29 10268 return 1;
427cd150
TT
10269}
10270
348d480f
PA
10271/* The breakpoint_ops structure to be used in hardware watchpoints. */
10272
2060206e 10273static struct breakpoint_ops watchpoint_breakpoint_ops;
348d480f
PA
10274
10275/* Implement the "insert" breakpoint_ops method for
10276 masked hardware watchpoints. */
10277
10278static int
10279insert_masked_watchpoint (struct bp_location *bl)
10280{
3a5c3e22
PA
10281 struct watchpoint *w = (struct watchpoint *) bl->owner;
10282
10283 return target_insert_mask_watchpoint (bl->address, w->hw_wp_mask,
348d480f
PA
10284 bl->watchpoint_type);
10285}
10286
10287/* Implement the "remove" breakpoint_ops method for
10288 masked hardware watchpoints. */
10289
10290static int
73971819 10291remove_masked_watchpoint (struct bp_location *bl, enum remove_bp_reason reason)
348d480f 10292{
3a5c3e22
PA
10293 struct watchpoint *w = (struct watchpoint *) bl->owner;
10294
10295 return target_remove_mask_watchpoint (bl->address, w->hw_wp_mask,
348d480f
PA
10296 bl->watchpoint_type);
10297}
10298
10299/* Implement the "resources_needed" breakpoint_ops method for
10300 masked hardware watchpoints. */
10301
10302static int
10303resources_needed_masked_watchpoint (const struct bp_location *bl)
10304{
3a5c3e22
PA
10305 struct watchpoint *w = (struct watchpoint *) bl->owner;
10306
10307 return target_masked_watch_num_registers (bl->address, w->hw_wp_mask);
348d480f
PA
10308}
10309
10310/* Implement the "works_in_software_mode" breakpoint_ops method for
10311 masked hardware watchpoints. */
10312
10313static int
10314works_in_software_mode_masked_watchpoint (const struct breakpoint *b)
10315{
10316 return 0;
10317}
10318
10319/* Implement the "print_it" breakpoint_ops method for
10320 masked hardware watchpoints. */
10321
10322static enum print_stop_action
10323print_it_masked_watchpoint (bpstat bs)
10324{
10325 struct breakpoint *b = bs->breakpoint_at;
79a45e25 10326 struct ui_out *uiout = current_uiout;
348d480f
PA
10327
10328 /* Masked watchpoints have only one location. */
10329 gdb_assert (b->loc && b->loc->next == NULL);
10330
f303dbd6
PA
10331 annotate_watchpoint (b->number);
10332 maybe_print_thread_hit_breakpoint (uiout);
10333
348d480f
PA
10334 switch (b->type)
10335 {
10336 case bp_hardware_watchpoint:
112e8700
SM
10337 if (uiout->is_mi_like_p ())
10338 uiout->field_string
10339 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
348d480f
PA
10340 break;
10341
10342 case bp_read_watchpoint:
112e8700
SM
10343 if (uiout->is_mi_like_p ())
10344 uiout->field_string
10345 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
348d480f
PA
10346 break;
10347
10348 case bp_access_watchpoint:
112e8700
SM
10349 if (uiout->is_mi_like_p ())
10350 uiout->field_string
10351 ("reason",
348d480f
PA
10352 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10353 break;
10354 default:
10355 internal_error (__FILE__, __LINE__,
10356 _("Invalid hardware watchpoint type."));
10357 }
10358
10359 mention (b);
112e8700 10360 uiout->text (_("\n\
9c06b0b4
TJB
10361Check the underlying instruction at PC for the memory\n\
10362address and value which triggered this watchpoint.\n"));
112e8700 10363 uiout->text ("\n");
9c06b0b4
TJB
10364
10365 /* More than one watchpoint may have been triggered. */
10366 return PRINT_UNKNOWN;
10367}
10368
10369/* Implement the "print_one_detail" breakpoint_ops method for
10370 masked hardware watchpoints. */
10371
10372static void
10373print_one_detail_masked_watchpoint (const struct breakpoint *b,
10374 struct ui_out *uiout)
10375{
3a5c3e22
PA
10376 struct watchpoint *w = (struct watchpoint *) b;
10377
9c06b0b4
TJB
10378 /* Masked watchpoints have only one location. */
10379 gdb_assert (b->loc && b->loc->next == NULL);
10380
112e8700
SM
10381 uiout->text ("\tmask ");
10382 uiout->field_core_addr ("mask", b->loc->gdbarch, w->hw_wp_mask);
10383 uiout->text ("\n");
9c06b0b4
TJB
10384}
10385
10386/* Implement the "print_mention" breakpoint_ops method for
10387 masked hardware watchpoints. */
10388
10389static void
10390print_mention_masked_watchpoint (struct breakpoint *b)
10391{
3a5c3e22 10392 struct watchpoint *w = (struct watchpoint *) b;
79a45e25 10393 struct ui_out *uiout = current_uiout;
46b9c129 10394 const char *tuple_name;
9c06b0b4
TJB
10395
10396 switch (b->type)
10397 {
10398 case bp_hardware_watchpoint:
112e8700 10399 uiout->text ("Masked hardware watchpoint ");
46b9c129 10400 tuple_name = "wpt";
9c06b0b4
TJB
10401 break;
10402 case bp_read_watchpoint:
112e8700 10403 uiout->text ("Masked hardware read watchpoint ");
46b9c129 10404 tuple_name = "hw-rwpt";
9c06b0b4
TJB
10405 break;
10406 case bp_access_watchpoint:
112e8700 10407 uiout->text ("Masked hardware access (read/write) watchpoint ");
46b9c129 10408 tuple_name = "hw-awpt";
9c06b0b4
TJB
10409 break;
10410 default:
10411 internal_error (__FILE__, __LINE__,
10412 _("Invalid hardware watchpoint type."));
10413 }
10414
46b9c129 10415 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
112e8700
SM
10416 uiout->field_int ("number", b->number);
10417 uiout->text (": ");
10418 uiout->field_string ("exp", w->exp_string);
9c06b0b4
TJB
10419}
10420
10421/* Implement the "print_recreate" breakpoint_ops method for
10422 masked hardware watchpoints. */
10423
10424static void
10425print_recreate_masked_watchpoint (struct breakpoint *b, struct ui_file *fp)
10426{
3a5c3e22 10427 struct watchpoint *w = (struct watchpoint *) b;
9c06b0b4
TJB
10428 char tmp[40];
10429
10430 switch (b->type)
10431 {
10432 case bp_hardware_watchpoint:
10433 fprintf_unfiltered (fp, "watch");
10434 break;
10435 case bp_read_watchpoint:
10436 fprintf_unfiltered (fp, "rwatch");
10437 break;
10438 case bp_access_watchpoint:
10439 fprintf_unfiltered (fp, "awatch");
10440 break;
10441 default:
10442 internal_error (__FILE__, __LINE__,
10443 _("Invalid hardware watchpoint type."));
10444 }
10445
3a5c3e22
PA
10446 sprintf_vma (tmp, w->hw_wp_mask);
10447 fprintf_unfiltered (fp, " %s mask 0x%s", w->exp_string, tmp);
d9b3f62e 10448 print_recreate_thread (b, fp);
9c06b0b4
TJB
10449}
10450
10451/* The breakpoint_ops structure to be used in masked hardware watchpoints. */
10452
2060206e 10453static struct breakpoint_ops masked_watchpoint_breakpoint_ops;
9c06b0b4
TJB
10454
10455/* Tell whether the given watchpoint is a masked hardware watchpoint. */
10456
10457static int
10458is_masked_watchpoint (const struct breakpoint *b)
10459{
10460 return b->ops == &masked_watchpoint_breakpoint_ops;
10461}
10462
53a5351d
JM
10463/* accessflag: hw_write: watch write,
10464 hw_read: watch read,
10465 hw_access: watch access (read or write) */
c906108c 10466static void
bbc13ae3 10467watch_command_1 (const char *arg, int accessflag, int from_tty,
84f4c1fe 10468 int just_location, int internal)
c906108c 10469{
c1fc2657 10470 struct breakpoint *scope_breakpoint = NULL;
270140bd 10471 const struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
b926417a 10472 struct value *result;
bb9d5f81 10473 int saved_bitpos = 0, saved_bitsize = 0;
bbc13ae3
KS
10474 const char *exp_start = NULL;
10475 const char *exp_end = NULL;
10476 const char *tok, *end_tok;
9c06b0b4 10477 int toklen = -1;
bbc13ae3
KS
10478 const char *cond_start = NULL;
10479 const char *cond_end = NULL;
c906108c 10480 enum bptype bp_type;
37e4754d 10481 int thread = -1;
0cf6dd15 10482 int pc = 0;
9c06b0b4
TJB
10483 /* Flag to indicate whether we are going to use masks for
10484 the hardware watchpoint. */
10485 int use_mask = 0;
10486 CORE_ADDR mask = 0;
c906108c 10487
37e4754d
LM
10488 /* Make sure that we actually have parameters to parse. */
10489 if (arg != NULL && arg[0] != '\0')
10490 {
bbc13ae3
KS
10491 const char *value_start;
10492
10493 exp_end = arg + strlen (arg);
37e4754d 10494
9c06b0b4
TJB
10495 /* Look for "parameter value" pairs at the end
10496 of the arguments string. */
bbc13ae3 10497 for (tok = exp_end - 1; tok > arg; tok--)
9c06b0b4
TJB
10498 {
10499 /* Skip whitespace at the end of the argument list. */
10500 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10501 tok--;
10502
10503 /* Find the beginning of the last token.
10504 This is the value of the parameter. */
10505 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10506 tok--;
10507 value_start = tok + 1;
10508
10509 /* Skip whitespace. */
10510 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10511 tok--;
10512
10513 end_tok = tok;
10514
10515 /* Find the beginning of the second to last token.
10516 This is the parameter itself. */
10517 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10518 tok--;
10519 tok++;
10520 toklen = end_tok - tok + 1;
10521
61012eef 10522 if (toklen == 6 && startswith (tok, "thread"))
9c06b0b4 10523 {
5d5658a1 10524 struct thread_info *thr;
9c06b0b4
TJB
10525 /* At this point we've found a "thread" token, which means
10526 the user is trying to set a watchpoint that triggers
10527 only in a specific thread. */
5d5658a1 10528 const char *endp;
37e4754d 10529
9c06b0b4
TJB
10530 if (thread != -1)
10531 error(_("You can specify only one thread."));
37e4754d 10532
9c06b0b4 10533 /* Extract the thread ID from the next token. */
5d5658a1 10534 thr = parse_thread_id (value_start, &endp);
37e4754d 10535
5d5658a1 10536 /* Check if the user provided a valid thread ID. */
9c06b0b4 10537 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
5d5658a1 10538 invalid_thread_id_error (value_start);
9c06b0b4 10539
5d5658a1 10540 thread = thr->global_num;
9c06b0b4 10541 }
61012eef 10542 else if (toklen == 4 && startswith (tok, "mask"))
9c06b0b4
TJB
10543 {
10544 /* We've found a "mask" token, which means the user wants to
10545 create a hardware watchpoint that is going to have the mask
10546 facility. */
10547 struct value *mask_value, *mark;
37e4754d 10548
9c06b0b4
TJB
10549 if (use_mask)
10550 error(_("You can specify only one mask."));
37e4754d 10551
9c06b0b4 10552 use_mask = just_location = 1;
37e4754d 10553
9c06b0b4
TJB
10554 mark = value_mark ();
10555 mask_value = parse_to_comma_and_eval (&value_start);
10556 mask = value_as_address (mask_value);
10557 value_free_to_mark (mark);
10558 }
10559 else
10560 /* We didn't recognize what we found. We should stop here. */
10561 break;
37e4754d 10562
9c06b0b4
TJB
10563 /* Truncate the string and get rid of the "parameter value" pair before
10564 the arguments string is parsed by the parse_exp_1 function. */
bbc13ae3 10565 exp_end = tok;
9c06b0b4 10566 }
37e4754d 10567 }
bbc13ae3
KS
10568 else
10569 exp_end = arg;
37e4754d 10570
bbc13ae3
KS
10571 /* Parse the rest of the arguments. From here on out, everything
10572 is in terms of a newly allocated string instead of the original
10573 ARG. */
81b1e71c
TT
10574 std::string expression (arg, exp_end - arg);
10575 exp_start = arg = expression.c_str ();
699bd4cf
TT
10576 innermost_block_tracker tracker;
10577 expression_up exp = parse_exp_1 (&arg, 0, 0, 0, &tracker);
c906108c 10578 exp_end = arg;
fa8a61dc
TT
10579 /* Remove trailing whitespace from the expression before saving it.
10580 This makes the eventual display of the expression string a bit
10581 prettier. */
10582 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
10583 --exp_end;
10584
65d79d4b 10585 /* Checking if the expression is not constant. */
4d01a485 10586 if (watchpoint_exp_is_const (exp.get ()))
65d79d4b
SDJ
10587 {
10588 int len;
10589
10590 len = exp_end - exp_start;
10591 while (len > 0 && isspace (exp_start[len - 1]))
10592 len--;
10593 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
10594 }
10595
699bd4cf 10596 exp_valid_block = tracker.block ();
b926417a 10597 struct value *mark = value_mark ();
850645cf
TT
10598 struct value *val_as_value = nullptr;
10599 fetch_subexp_value (exp.get (), &pc, &val_as_value, &result, NULL,
10600 just_location);
06a64a0b 10601
850645cf 10602 if (val_as_value != NULL && just_location)
bb9d5f81 10603 {
850645cf
TT
10604 saved_bitpos = value_bitpos (val_as_value);
10605 saved_bitsize = value_bitsize (val_as_value);
bb9d5f81
PP
10606 }
10607
850645cf 10608 value_ref_ptr val;
06a64a0b
TT
10609 if (just_location)
10610 {
9c06b0b4
TJB
10611 int ret;
10612
06a64a0b 10613 exp_valid_block = NULL;
850645cf 10614 val = release_value (value_addr (result));
06a64a0b 10615 value_free_to_mark (mark);
9c06b0b4
TJB
10616
10617 if (use_mask)
10618 {
850645cf 10619 ret = target_masked_watch_num_registers (value_as_address (val.get ()),
9c06b0b4
TJB
10620 mask);
10621 if (ret == -1)
10622 error (_("This target does not support masked watchpoints."));
10623 else if (ret == -2)
10624 error (_("Invalid mask or memory region."));
10625 }
06a64a0b 10626 }
850645cf
TT
10627 else if (val_as_value != NULL)
10628 val = release_value (val_as_value);
c906108c 10629
f1735a53
TT
10630 tok = skip_spaces (arg);
10631 end_tok = skip_to_space (tok);
c906108c
SS
10632
10633 toklen = end_tok - tok;
10634 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
10635 {
10636 tok = cond_start = end_tok + 1;
699bd4cf
TT
10637 innermost_block_tracker if_tracker;
10638 parse_exp_1 (&tok, 0, 0, 0, &if_tracker);
60e1c644
PA
10639
10640 /* The watchpoint expression may not be local, but the condition
10641 may still be. E.g.: `watch global if local > 0'. */
699bd4cf 10642 cond_exp_valid_block = if_tracker.block ();
60e1c644 10643
c906108c
SS
10644 cond_end = tok;
10645 }
10646 if (*tok)
8a3fe4f8 10647 error (_("Junk at end of command."));
c906108c 10648
441d7c93
PA
10649 frame_info *wp_frame = block_innermost_frame (exp_valid_block);
10650
10651 /* Save this because create_internal_breakpoint below invalidates
10652 'wp_frame'. */
10653 frame_id watchpoint_frame = get_frame_id (wp_frame);
d983da9c
DJ
10654
10655 /* If the expression is "local", then set up a "watchpoint scope"
10656 breakpoint at the point where we've left the scope of the watchpoint
10657 expression. Create the scope breakpoint before the watchpoint, so
10658 that we will encounter it first in bpstat_stop_status. */
441d7c93 10659 if (exp_valid_block != NULL && wp_frame != NULL)
d983da9c 10660 {
441d7c93
PA
10661 frame_id caller_frame_id = frame_unwind_caller_id (wp_frame);
10662
10663 if (frame_id_p (caller_frame_id))
edb3359d 10664 {
441d7c93
PA
10665 gdbarch *caller_arch = frame_unwind_caller_arch (wp_frame);
10666 CORE_ADDR caller_pc = frame_unwind_caller_pc (wp_frame);
10667
edb3359d 10668 scope_breakpoint
441d7c93 10669 = create_internal_breakpoint (caller_arch, caller_pc,
06edf0c0
PA
10670 bp_watchpoint_scope,
10671 &momentary_breakpoint_ops);
d983da9c 10672
441d7c93
PA
10673 /* create_internal_breakpoint could invalidate WP_FRAME. */
10674 wp_frame = NULL;
10675
edb3359d 10676 scope_breakpoint->enable_state = bp_enabled;
d983da9c 10677
edb3359d
DJ
10678 /* Automatically delete the breakpoint when it hits. */
10679 scope_breakpoint->disposition = disp_del;
d983da9c 10680
edb3359d 10681 /* Only break in the proper frame (help with recursion). */
441d7c93 10682 scope_breakpoint->frame_id = caller_frame_id;
d983da9c 10683
edb3359d 10684 /* Set the address at which we will stop. */
441d7c93
PA
10685 scope_breakpoint->loc->gdbarch = caller_arch;
10686 scope_breakpoint->loc->requested_address = caller_pc;
edb3359d 10687 scope_breakpoint->loc->address
a6d9a66e
UW
10688 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
10689 scope_breakpoint->loc->requested_address,
edb3359d
DJ
10690 scope_breakpoint->type);
10691 }
d983da9c
DJ
10692 }
10693
e8369a73
AB
10694 /* Now set up the breakpoint. We create all watchpoints as hardware
10695 watchpoints here even if hardware watchpoints are turned off, a call
10696 to update_watchpoint later in this function will cause the type to
10697 drop back to bp_watchpoint (software watchpoint) if required. */
10698
10699 if (accessflag == hw_read)
10700 bp_type = bp_read_watchpoint;
10701 else if (accessflag == hw_access)
10702 bp_type = bp_access_watchpoint;
10703 else
10704 bp_type = bp_hardware_watchpoint;
3a5c3e22 10705
b270e6f9 10706 std::unique_ptr<watchpoint> w (new watchpoint ());
c1fc2657 10707
348d480f 10708 if (use_mask)
b270e6f9 10709 init_raw_breakpoint_without_location (w.get (), NULL, bp_type,
3a5c3e22 10710 &masked_watchpoint_breakpoint_ops);
348d480f 10711 else
b270e6f9 10712 init_raw_breakpoint_without_location (w.get (), NULL, bp_type,
3a5c3e22 10713 &watchpoint_breakpoint_ops);
c1fc2657
SM
10714 w->thread = thread;
10715 w->disposition = disp_donttouch;
10716 w->pspace = current_program_space;
b22e99fd 10717 w->exp = std::move (exp);
3a5c3e22
PA
10718 w->exp_valid_block = exp_valid_block;
10719 w->cond_exp_valid_block = cond_exp_valid_block;
06a64a0b
TT
10720 if (just_location)
10721 {
850645cf
TT
10722 struct type *t = value_type (val.get ());
10723 CORE_ADDR addr = value_as_address (val.get ());
06a64a0b 10724
43cc5389
TT
10725 w->exp_string_reparse
10726 = current_language->la_watch_location_expression (t, addr).release ();
06a64a0b 10727
3a5c3e22 10728 w->exp_string = xstrprintf ("-location %.*s",
d63d0675 10729 (int) (exp_end - exp_start), exp_start);
06a64a0b
TT
10730 }
10731 else
3a5c3e22 10732 w->exp_string = savestring (exp_start, exp_end - exp_start);
9c06b0b4
TJB
10733
10734 if (use_mask)
10735 {
3a5c3e22 10736 w->hw_wp_mask = mask;
9c06b0b4
TJB
10737 }
10738 else
10739 {
3a5c3e22 10740 w->val = val;
bb9d5f81
PP
10741 w->val_bitpos = saved_bitpos;
10742 w->val_bitsize = saved_bitsize;
3a5c3e22 10743 w->val_valid = 1;
9c06b0b4 10744 }
77b06cd7 10745
c906108c 10746 if (cond_start)
c1fc2657 10747 w->cond_string = savestring (cond_start, cond_end - cond_start);
c906108c 10748 else
c1fc2657 10749 w->cond_string = 0;
c5aa993b 10750
441d7c93 10751 if (frame_id_p (watchpoint_frame))
f6bc2008 10752 {
441d7c93 10753 w->watchpoint_frame = watchpoint_frame;
3a5c3e22 10754 w->watchpoint_thread = inferior_ptid;
f6bc2008 10755 }
c906108c 10756 else
f6bc2008 10757 {
3a5c3e22
PA
10758 w->watchpoint_frame = null_frame_id;
10759 w->watchpoint_thread = null_ptid;
f6bc2008 10760 }
c906108c 10761
d983da9c 10762 if (scope_breakpoint != NULL)
c906108c 10763 {
d983da9c
DJ
10764 /* The scope breakpoint is related to the watchpoint. We will
10765 need to act on them together. */
c1fc2657 10766 w->related_breakpoint = scope_breakpoint;
b270e6f9 10767 scope_breakpoint->related_breakpoint = w.get ();
c906108c 10768 }
d983da9c 10769
06a64a0b
TT
10770 if (!just_location)
10771 value_free_to_mark (mark);
2d134ed3 10772
b270e6f9
TT
10773 /* Finally update the new watchpoint. This creates the locations
10774 that should be inserted. */
10775 update_watchpoint (w.get (), 1);
a9634178 10776
b270e6f9 10777 install_breakpoint (internal, std::move (w), 1);
c906108c
SS
10778}
10779
e09342b5 10780/* Return count of debug registers needed to watch the given expression.
e09342b5 10781 If the watchpoint cannot be handled in hardware return zero. */
c906108c 10782
c906108c 10783static int
a6535de1 10784can_use_hardware_watchpoint (const std::vector<value_ref_ptr> &vals)
c906108c
SS
10785{
10786 int found_memory_cnt = 0;
10787
10788 /* Did the user specifically forbid us to use hardware watchpoints? */
c5aa993b 10789 if (!can_use_hw_watchpoints)
c906108c 10790 return 0;
c5aa993b 10791
a6535de1
TT
10792 gdb_assert (!vals.empty ());
10793 struct value *head = vals[0].get ();
10794
5c44784c
JM
10795 /* Make sure that the value of the expression depends only upon
10796 memory contents, and values computed from them within GDB. If we
10797 find any register references or function calls, we can't use a
10798 hardware watchpoint.
10799
10800 The idea here is that evaluating an expression generates a series
10801 of values, one holding the value of every subexpression. (The
10802 expression a*b+c has five subexpressions: a, b, a*b, c, and
10803 a*b+c.) GDB's values hold almost enough information to establish
10804 the criteria given above --- they identify memory lvalues,
10805 register lvalues, computed values, etcetera. So we can evaluate
10806 the expression, and then scan the chain of values that leaves
10807 behind to decide whether we can detect any possible change to the
10808 expression's final value using only hardware watchpoints.
10809
10810 However, I don't think that the values returned by inferior
10811 function calls are special in any way. So this function may not
10812 notice that an expression involving an inferior function call
10813 can't be watched with hardware watchpoints. FIXME. */
a6535de1 10814 for (const value_ref_ptr &iter : vals)
c906108c 10815 {
a6535de1
TT
10816 struct value *v = iter.get ();
10817
5c44784c 10818 if (VALUE_LVAL (v) == lval_memory)
c906108c 10819 {
8464be76
DJ
10820 if (v != head && value_lazy (v))
10821 /* A lazy memory lvalue in the chain is one that GDB never
10822 needed to fetch; we either just used its address (e.g.,
10823 `a' in `a.b') or we never needed it at all (e.g., `a'
10824 in `a,b'). This doesn't apply to HEAD; if that is
10825 lazy then it was not readable, but watch it anyway. */
5c44784c 10826 ;
53a5351d 10827 else
5c44784c
JM
10828 {
10829 /* Ahh, memory we actually used! Check if we can cover
10830 it with hardware watchpoints. */
df407dfe 10831 struct type *vtype = check_typedef (value_type (v));
2e70b7b9
MS
10832
10833 /* We only watch structs and arrays if user asked for it
10834 explicitly, never if they just happen to appear in a
10835 middle of some value chain. */
10836 if (v == head
10837 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
10838 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
10839 {
42ae5230 10840 CORE_ADDR vaddr = value_address (v);
e09342b5
TJB
10841 int len;
10842 int num_regs;
10843
a9634178 10844 len = (target_exact_watchpoints
e09342b5
TJB
10845 && is_scalar_type_recursive (vtype))?
10846 1 : TYPE_LENGTH (value_type (v));
2e70b7b9 10847
e09342b5
TJB
10848 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
10849 if (!num_regs)
2e70b7b9
MS
10850 return 0;
10851 else
e09342b5 10852 found_memory_cnt += num_regs;
2e70b7b9 10853 }
5c44784c 10854 }
c5aa993b 10855 }
5086187c
AC
10856 else if (VALUE_LVAL (v) != not_lval
10857 && deprecated_value_modifiable (v) == 0)
38b6c3b3 10858 return 0; /* These are values from the history (e.g., $1). */
5086187c 10859 else if (VALUE_LVAL (v) == lval_register)
38b6c3b3 10860 return 0; /* Cannot watch a register with a HW watchpoint. */
c906108c
SS
10861 }
10862
10863 /* The expression itself looks suitable for using a hardware
10864 watchpoint, but give the target machine a chance to reject it. */
10865 return found_memory_cnt;
10866}
10867
8b93c638 10868void
f2fc3015 10869watch_command_wrapper (const char *arg, int from_tty, int internal)
8b93c638 10870{
84f4c1fe 10871 watch_command_1 (arg, hw_write, from_tty, 0, internal);
06a64a0b
TT
10872}
10873
06a64a0b
TT
10874/* A helper function that looks for the "-location" argument and then
10875 calls watch_command_1. */
10876
10877static void
0b39b52e 10878watch_maybe_just_location (const char *arg, int accessflag, int from_tty)
06a64a0b
TT
10879{
10880 int just_location = 0;
10881
10882 if (arg
10883 && (check_for_argument (&arg, "-location", sizeof ("-location") - 1)
10884 || check_for_argument (&arg, "-l", sizeof ("-l") - 1)))
cbba3ecd 10885 just_location = 1;
06a64a0b 10886
84f4c1fe 10887 watch_command_1 (arg, accessflag, from_tty, just_location, 0);
8b93c638 10888}
8926118c 10889
c5aa993b 10890static void
0b39b52e 10891watch_command (const char *arg, int from_tty)
c906108c 10892{
06a64a0b 10893 watch_maybe_just_location (arg, hw_write, from_tty);
c906108c
SS
10894}
10895
8b93c638 10896void
f2fc3015 10897rwatch_command_wrapper (const char *arg, int from_tty, int internal)
8b93c638 10898{
84f4c1fe 10899 watch_command_1 (arg, hw_read, from_tty, 0, internal);
8b93c638 10900}
8926118c 10901
c5aa993b 10902static void
0b39b52e 10903rwatch_command (const char *arg, int from_tty)
c906108c 10904{
06a64a0b 10905 watch_maybe_just_location (arg, hw_read, from_tty);
c906108c
SS
10906}
10907
8b93c638 10908void
f2fc3015 10909awatch_command_wrapper (const char *arg, int from_tty, int internal)
8b93c638 10910{
84f4c1fe 10911 watch_command_1 (arg, hw_access, from_tty, 0, internal);
8b93c638 10912}
8926118c 10913
c5aa993b 10914static void
0b39b52e 10915awatch_command (const char *arg, int from_tty)
c906108c 10916{
06a64a0b 10917 watch_maybe_just_location (arg, hw_access, from_tty);
c906108c 10918}
c906108c 10919\f
c5aa993b 10920
cfc31633
PA
10921/* Data for the FSM that manages the until(location)/advance commands
10922 in infcmd.c. Here because it uses the mechanisms of
10923 breakpoints. */
c906108c 10924
46e3ed7f 10925struct until_break_fsm : public thread_fsm
bfec99b2 10926{
46e3ed7f 10927 /* The thread that was current when the command was executed. */
cfc31633
PA
10928 int thread;
10929
10930 /* The breakpoint set at the destination location. */
46e3ed7f 10931 breakpoint_up location_breakpoint;
cfc31633
PA
10932
10933 /* Breakpoint set at the return address in the caller frame. May be
10934 NULL. */
46e3ed7f 10935 breakpoint_up caller_breakpoint;
cfc31633 10936
46e3ed7f
TT
10937 until_break_fsm (struct interp *cmd_interp, int thread,
10938 breakpoint_up &&location_breakpoint,
10939 breakpoint_up &&caller_breakpoint)
10940 : thread_fsm (cmd_interp),
10941 thread (thread),
10942 location_breakpoint (std::move (location_breakpoint)),
10943 caller_breakpoint (std::move (caller_breakpoint))
10944 {
10945 }
cfc31633 10946
46e3ed7f
TT
10947 void clean_up (struct thread_info *thread) override;
10948 bool should_stop (struct thread_info *thread) override;
10949 enum async_reply_reason do_async_reply_reason () override;
cfc31633
PA
10950};
10951
cfc31633
PA
10952/* Implementation of the 'should_stop' FSM method for the
10953 until(location)/advance commands. */
10954
46e3ed7f
TT
10955bool
10956until_break_fsm::should_stop (struct thread_info *tp)
cfc31633 10957{
cfc31633 10958 if (bpstat_find_breakpoint (tp->control.stop_bpstat,
46e3ed7f
TT
10959 location_breakpoint.get ()) != NULL
10960 || (caller_breakpoint != NULL
cfc31633 10961 && bpstat_find_breakpoint (tp->control.stop_bpstat,
46e3ed7f
TT
10962 caller_breakpoint.get ()) != NULL))
10963 set_finished ();
cfc31633 10964
46e3ed7f 10965 return true;
cfc31633
PA
10966}
10967
10968/* Implementation of the 'clean_up' FSM method for the
10969 until(location)/advance commands. */
10970
46e3ed7f
TT
10971void
10972until_break_fsm::clean_up (struct thread_info *)
43ff13b4 10973{
cfc31633 10974 /* Clean up our temporary breakpoints. */
46e3ed7f
TT
10975 location_breakpoint.reset ();
10976 caller_breakpoint.reset ();
10977 delete_longjmp_breakpoint (thread);
cfc31633
PA
10978}
10979
10980/* Implementation of the 'async_reply_reason' FSM method for the
10981 until(location)/advance commands. */
10982
46e3ed7f
TT
10983enum async_reply_reason
10984until_break_fsm::do_async_reply_reason ()
cfc31633
PA
10985{
10986 return EXEC_ASYNC_LOCATION_REACHED;
43ff13b4
JM
10987}
10988
c906108c 10989void
f2fc3015 10990until_break_command (const char *arg, int from_tty, int anywhere)
c906108c 10991{
8556afb4
PA
10992 struct frame_info *frame;
10993 struct gdbarch *frame_gdbarch;
10994 struct frame_id stack_frame_id;
10995 struct frame_id caller_frame_id;
186c406b
TT
10996 int thread;
10997 struct thread_info *tp;
c906108c 10998
70509625 10999 clear_proceed_status (0);
c906108c
SS
11000
11001 /* Set a breakpoint where the user wants it and at return from
4a64f543 11002 this function. */
c5aa993b 11003
ffc2605c 11004 event_location_up location = string_to_event_location (&arg, current_language);
f00aae0f 11005
6c5b2ebe
PA
11006 std::vector<symtab_and_line> sals
11007 = (last_displayed_sal_is_valid ()
11008 ? decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
11009 get_last_displayed_symtab (),
11010 get_last_displayed_line ())
11011 : decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE,
cafb3438 11012 NULL, NULL, 0));
c5aa993b 11013
6c5b2ebe 11014 if (sals.size () != 1)
8a3fe4f8 11015 error (_("Couldn't get information on specified line."));
c5aa993b 11016
6c5b2ebe 11017 symtab_and_line &sal = sals[0];
c5aa993b 11018
c906108c 11019 if (*arg)
8a3fe4f8 11020 error (_("Junk at end of arguments."));
c5aa993b 11021
c906108c 11022 resolve_sal_pc (&sal);
c5aa993b 11023
186c406b 11024 tp = inferior_thread ();
5d5658a1 11025 thread = tp->global_num;
186c406b 11026
8556afb4
PA
11027 /* Note linespec handling above invalidates the frame chain.
11028 Installing a breakpoint also invalidates the frame chain (as it
11029 may need to switch threads), so do any frame handling before
11030 that. */
11031
11032 frame = get_selected_frame (NULL);
11033 frame_gdbarch = get_frame_arch (frame);
11034 stack_frame_id = get_stack_frame_id (frame);
11035 caller_frame_id = frame_unwind_caller_id (frame);
883bc8d1 11036
ae66c1fc
EZ
11037 /* Keep within the current frame, or in frames called by the current
11038 one. */
edb3359d 11039
454dafbd 11040 breakpoint_up caller_breakpoint;
5419bdae
TT
11041
11042 gdb::optional<delete_longjmp_breakpoint_cleanup> lj_deleter;
11043
883bc8d1 11044 if (frame_id_p (caller_frame_id))
c906108c 11045 {
883bc8d1 11046 struct symtab_and_line sal2;
cfc31633 11047 struct gdbarch *caller_gdbarch;
883bc8d1
PA
11048
11049 sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
11050 sal2.pc = frame_unwind_caller_pc (frame);
cfc31633
PA
11051 caller_gdbarch = frame_unwind_caller_arch (frame);
11052 caller_breakpoint = set_momentary_breakpoint (caller_gdbarch,
11053 sal2,
11054 caller_frame_id,
11055 bp_until);
186c406b 11056
883bc8d1 11057 set_longjmp_breakpoint (tp, caller_frame_id);
5419bdae 11058 lj_deleter.emplace (thread);
c906108c 11059 }
c5aa993b 11060
c70a6932
JK
11061 /* set_momentary_breakpoint could invalidate FRAME. */
11062 frame = NULL;
11063
454dafbd 11064 breakpoint_up location_breakpoint;
883bc8d1
PA
11065 if (anywhere)
11066 /* If the user told us to continue until a specified location,
11067 we don't specify a frame at which we need to stop. */
cfc31633
PA
11068 location_breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11069 null_frame_id, bp_until);
883bc8d1
PA
11070 else
11071 /* Otherwise, specify the selected frame, because we want to stop
11072 only at the very same frame. */
cfc31633
PA
11073 location_breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11074 stack_frame_id, bp_until);
883bc8d1 11075
46e3ed7f
TT
11076 tp->thread_fsm = new until_break_fsm (command_interp (), tp->global_num,
11077 std::move (location_breakpoint),
11078 std::move (caller_breakpoint));
f107f563 11079
5419bdae
TT
11080 if (lj_deleter)
11081 lj_deleter->release ();
f107f563 11082
cfc31633 11083 proceed (-1, GDB_SIGNAL_DEFAULT);
c906108c 11084}
ae66c1fc 11085
c906108c
SS
11086/* This function attempts to parse an optional "if <cond>" clause
11087 from the arg string. If one is not found, it returns NULL.
c5aa993b 11088
c906108c
SS
11089 Else, it returns a pointer to the condition string. (It does not
11090 attempt to evaluate the string against a particular block.) And,
11091 it updates arg to point to the first character following the parsed
4a64f543 11092 if clause in the arg string. */
53a5351d 11093
63160a43
PA
11094const char *
11095ep_parse_optional_if_clause (const char **arg)
c906108c 11096{
63160a43 11097 const char *cond_string;
c5aa993b
JM
11098
11099 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
c906108c 11100 return NULL;
c5aa993b 11101
4a64f543 11102 /* Skip the "if" keyword. */
c906108c 11103 (*arg) += 2;
c5aa993b 11104
c906108c 11105 /* Skip any extra leading whitespace, and record the start of the
4a64f543 11106 condition string. */
f1735a53 11107 *arg = skip_spaces (*arg);
c906108c 11108 cond_string = *arg;
c5aa993b 11109
4a64f543
MS
11110 /* Assume that the condition occupies the remainder of the arg
11111 string. */
c906108c 11112 (*arg) += strlen (cond_string);
c5aa993b 11113
c906108c
SS
11114 return cond_string;
11115}
c5aa993b 11116
c906108c
SS
11117/* Commands to deal with catching events, such as signals, exceptions,
11118 process start/exit, etc. */
c5aa993b
JM
11119
11120typedef enum
11121{
44feb3ce
TT
11122 catch_fork_temporary, catch_vfork_temporary,
11123 catch_fork_permanent, catch_vfork_permanent
c5aa993b
JM
11124}
11125catch_fork_kind;
11126
c906108c 11127static void
eb4c3f4a 11128catch_fork_command_1 (const char *arg, int from_tty,
cc59ec59 11129 struct cmd_list_element *command)
c906108c 11130{
a6d9a66e 11131 struct gdbarch *gdbarch = get_current_arch ();
63160a43 11132 const char *cond_string = NULL;
44feb3ce
TT
11133 catch_fork_kind fork_kind;
11134 int tempflag;
11135
11136 fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
11137 tempflag = (fork_kind == catch_fork_temporary
11138 || fork_kind == catch_vfork_temporary);
c5aa993b 11139
44feb3ce
TT
11140 if (!arg)
11141 arg = "";
f1735a53 11142 arg = skip_spaces (arg);
c5aa993b 11143
c906108c 11144 /* The allowed syntax is:
c5aa993b
JM
11145 catch [v]fork
11146 catch [v]fork if <cond>
11147
4a64f543 11148 First, check if there's an if clause. */
c906108c 11149 cond_string = ep_parse_optional_if_clause (&arg);
c5aa993b 11150
c906108c 11151 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 11152 error (_("Junk at end of arguments."));
c5aa993b 11153
c906108c 11154 /* If this target supports it, create a fork or vfork catchpoint
4a64f543 11155 and enable reporting of such events. */
c5aa993b
JM
11156 switch (fork_kind)
11157 {
44feb3ce
TT
11158 case catch_fork_temporary:
11159 case catch_fork_permanent:
a6d9a66e 11160 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
ce78b96d 11161 &catch_fork_breakpoint_ops);
c906108c 11162 break;
44feb3ce
TT
11163 case catch_vfork_temporary:
11164 case catch_vfork_permanent:
a6d9a66e 11165 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
ce78b96d 11166 &catch_vfork_breakpoint_ops);
c906108c 11167 break;
c5aa993b 11168 default:
8a3fe4f8 11169 error (_("unsupported or unknown fork kind; cannot catch it"));
c906108c 11170 break;
c5aa993b 11171 }
c906108c
SS
11172}
11173
11174static void
eb4c3f4a 11175catch_exec_command_1 (const char *arg, int from_tty,
cc59ec59 11176 struct cmd_list_element *command)
c906108c 11177{
a6d9a66e 11178 struct gdbarch *gdbarch = get_current_arch ();
44feb3ce 11179 int tempflag;
63160a43 11180 const char *cond_string = NULL;
c906108c 11181
44feb3ce
TT
11182 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
11183
11184 if (!arg)
11185 arg = "";
f1735a53 11186 arg = skip_spaces (arg);
c906108c
SS
11187
11188 /* The allowed syntax is:
c5aa993b
JM
11189 catch exec
11190 catch exec if <cond>
c906108c 11191
4a64f543 11192 First, check if there's an if clause. */
c906108c
SS
11193 cond_string = ep_parse_optional_if_clause (&arg);
11194
11195 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 11196 error (_("Junk at end of arguments."));
c906108c 11197
b270e6f9
TT
11198 std::unique_ptr<exec_catchpoint> c (new exec_catchpoint ());
11199 init_catchpoint (c.get (), gdbarch, tempflag, cond_string,
b4d90040
PA
11200 &catch_exec_breakpoint_ops);
11201 c->exec_pathname = NULL;
11202
b270e6f9 11203 install_breakpoint (0, std::move (c), 1);
c906108c 11204}
c5aa993b 11205
9ac4176b 11206void
28010a5d
PA
11207init_ada_exception_breakpoint (struct breakpoint *b,
11208 struct gdbarch *gdbarch,
11209 struct symtab_and_line sal,
f2fc3015 11210 const char *addr_string,
c0a91b2b 11211 const struct breakpoint_ops *ops,
28010a5d 11212 int tempflag,
349774ef 11213 int enabled,
28010a5d 11214 int from_tty)
f7f9143b 11215{
f7f9143b
JB
11216 if (from_tty)
11217 {
5af949e3
UW
11218 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
11219 if (!loc_gdbarch)
11220 loc_gdbarch = gdbarch;
11221
6c95b8df
PA
11222 describe_other_breakpoints (loc_gdbarch,
11223 sal.pspace, sal.pc, sal.section, -1);
f7f9143b
JB
11224 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
11225 version for exception catchpoints, because two catchpoints
11226 used for different exception names will use the same address.
11227 In this case, a "breakpoint ... also set at..." warning is
4a64f543 11228 unproductive. Besides, the warning phrasing is also a bit
e5dd4106 11229 inappropriate, we should use the word catchpoint, and tell
f7f9143b
JB
11230 the user what type of catchpoint it is. The above is good
11231 enough for now, though. */
11232 }
11233
28010a5d 11234 init_raw_breakpoint (b, gdbarch, sal, bp_breakpoint, ops);
f7f9143b 11235
349774ef 11236 b->enable_state = enabled ? bp_enabled : bp_disabled;
f7f9143b 11237 b->disposition = tempflag ? disp_del : disp_donttouch;
d28cd78a
TT
11238 b->location = string_to_event_location (&addr_string,
11239 language_def (language_ada));
f7f9143b 11240 b->language = language_ada;
f7f9143b
JB
11241}
11242
c906108c 11243static void
981a3fb3 11244catch_command (const char *arg, int from_tty)
c906108c 11245{
44feb3ce 11246 error (_("Catch requires an event name."));
c906108c
SS
11247}
11248\f
11249
11250static void
981a3fb3 11251tcatch_command (const char *arg, int from_tty)
c906108c 11252{
44feb3ce 11253 error (_("Catch requires an event name."));
c906108c
SS
11254}
11255
81b1e71c 11256/* Compare two breakpoints and return a strcmp-like result. */
8a2c437b
TT
11257
11258static int
81b1e71c 11259compare_breakpoints (const breakpoint *a, const breakpoint *b)
8a2c437b 11260{
81b1e71c
TT
11261 uintptr_t ua = (uintptr_t) a;
11262 uintptr_t ub = (uintptr_t) b;
8a2c437b 11263
81b1e71c 11264 if (a->number < b->number)
8a2c437b 11265 return -1;
81b1e71c 11266 else if (a->number > b->number)
8a2c437b
TT
11267 return 1;
11268
11269 /* Now sort by address, in case we see, e..g, two breakpoints with
11270 the number 0. */
11271 if (ua < ub)
11272 return -1;
94b0e70d 11273 return ua > ub ? 1 : 0;
8a2c437b
TT
11274}
11275
80f8a6eb 11276/* Delete breakpoints by address or line. */
c906108c
SS
11277
11278static void
0b39b52e 11279clear_command (const char *arg, int from_tty)
c906108c 11280{
81b1e71c 11281 struct breakpoint *b;
c906108c 11282 int default_match;
c906108c 11283
6c5b2ebe
PA
11284 std::vector<symtab_and_line> decoded_sals;
11285 symtab_and_line last_sal;
11286 gdb::array_view<symtab_and_line> sals;
c906108c
SS
11287 if (arg)
11288 {
6c5b2ebe
PA
11289 decoded_sals
11290 = decode_line_with_current_source (arg,
11291 (DECODE_LINE_FUNFIRSTLINE
11292 | DECODE_LINE_LIST_MODE));
c906108c 11293 default_match = 0;
6c5b2ebe 11294 sals = decoded_sals;
c906108c
SS
11295 }
11296 else
11297 {
1bfeeb0f
JL
11298 /* Set sal's line, symtab, pc, and pspace to the values
11299 corresponding to the last call to print_frame_info. If the
11300 codepoint is not valid, this will set all the fields to 0. */
51abb421 11301 last_sal = get_last_displayed_sal ();
6c5b2ebe 11302 if (last_sal.symtab == 0)
8a3fe4f8 11303 error (_("No source file specified."));
c906108c 11304
c906108c 11305 default_match = 1;
6c5b2ebe 11306 sals = last_sal;
c906108c
SS
11307 }
11308
4a64f543
MS
11309 /* We don't call resolve_sal_pc here. That's not as bad as it
11310 seems, because all existing breakpoints typically have both
11311 file/line and pc set. So, if clear is given file/line, we can
11312 match this to existing breakpoint without obtaining pc at all.
ed0616c6
VP
11313
11314 We only support clearing given the address explicitly
11315 present in breakpoint table. Say, we've set breakpoint
4a64f543 11316 at file:line. There were several PC values for that file:line,
ed0616c6 11317 due to optimization, all in one block.
4a64f543
MS
11318
11319 We've picked one PC value. If "clear" is issued with another
ed0616c6
VP
11320 PC corresponding to the same file:line, the breakpoint won't
11321 be cleared. We probably can still clear the breakpoint, but
11322 since the other PC value is never presented to user, user
11323 can only find it by guessing, and it does not seem important
11324 to support that. */
11325
4a64f543
MS
11326 /* For each line spec given, delete bps which correspond to it. Do
11327 it in two passes, solely to preserve the current behavior that
11328 from_tty is forced true if we delete more than one
11329 breakpoint. */
c906108c 11330
81b1e71c 11331 std::vector<struct breakpoint *> found;
6c5b2ebe 11332 for (const auto &sal : sals)
c906108c 11333 {
05cba821
JK
11334 const char *sal_fullname;
11335
c906108c 11336 /* If exact pc given, clear bpts at that pc.
c5aa993b
JM
11337 If line given (pc == 0), clear all bpts on specified line.
11338 If defaulting, clear all bpts on default line
c906108c 11339 or at default pc.
c5aa993b
JM
11340
11341 defaulting sal.pc != 0 tests to do
11342
11343 0 1 pc
11344 1 1 pc _and_ line
11345 0 0 line
11346 1 0 <can't happen> */
c906108c 11347
05cba821
JK
11348 sal_fullname = (sal.symtab == NULL
11349 ? NULL : symtab_to_fullname (sal.symtab));
c906108c 11350
4a64f543 11351 /* Find all matching breakpoints and add them to 'found'. */
d6e956e5 11352 ALL_BREAKPOINTS (b)
c5aa993b 11353 {
0d381245 11354 int match = 0;
4a64f543 11355 /* Are we going to delete b? */
cc60f2e3 11356 if (b->type != bp_none && !is_watchpoint (b))
0d381245
VP
11357 {
11358 struct bp_location *loc = b->loc;
11359 for (; loc; loc = loc->next)
11360 {
f8eba3c6
TT
11361 /* If the user specified file:line, don't allow a PC
11362 match. This matches historical gdb behavior. */
11363 int pc_match = (!sal.explicit_line
11364 && sal.pc
11365 && (loc->pspace == sal.pspace)
11366 && (loc->address == sal.pc)
11367 && (!section_is_overlay (loc->section)
11368 || loc->section == sal.section));
4aac40c8
TT
11369 int line_match = 0;
11370
11371 if ((default_match || sal.explicit_line)
2f202fde 11372 && loc->symtab != NULL
05cba821 11373 && sal_fullname != NULL
4aac40c8 11374 && sal.pspace == loc->pspace
05cba821
JK
11375 && loc->line_number == sal.line
11376 && filename_cmp (symtab_to_fullname (loc->symtab),
11377 sal_fullname) == 0)
11378 line_match = 1;
4aac40c8 11379
0d381245
VP
11380 if (pc_match || line_match)
11381 {
11382 match = 1;
11383 break;
11384 }
11385 }
11386 }
11387
11388 if (match)
81b1e71c 11389 found.push_back (b);
c906108c 11390 }
80f8a6eb 11391 }
8a2c437b 11392
80f8a6eb 11393 /* Now go thru the 'found' chain and delete them. */
81b1e71c 11394 if (found.empty ())
80f8a6eb
MS
11395 {
11396 if (arg)
8a3fe4f8 11397 error (_("No breakpoint at %s."), arg);
80f8a6eb 11398 else
8a3fe4f8 11399 error (_("No breakpoint at this line."));
80f8a6eb 11400 }
c906108c 11401
8a2c437b 11402 /* Remove duplicates from the vec. */
81b1e71c 11403 std::sort (found.begin (), found.end (),
b926417a 11404 [] (const breakpoint *bp_a, const breakpoint *bp_b)
81b1e71c 11405 {
b926417a 11406 return compare_breakpoints (bp_a, bp_b) < 0;
81b1e71c
TT
11407 });
11408 found.erase (std::unique (found.begin (), found.end (),
b926417a 11409 [] (const breakpoint *bp_a, const breakpoint *bp_b)
81b1e71c 11410 {
b926417a 11411 return compare_breakpoints (bp_a, bp_b) == 0;
81b1e71c
TT
11412 }),
11413 found.end ());
8a2c437b 11414
81b1e71c 11415 if (found.size () > 1)
4a64f543 11416 from_tty = 1; /* Always report if deleted more than one. */
80f8a6eb 11417 if (from_tty)
a3f17187 11418 {
81b1e71c 11419 if (found.size () == 1)
a3f17187
AC
11420 printf_unfiltered (_("Deleted breakpoint "));
11421 else
11422 printf_unfiltered (_("Deleted breakpoints "));
11423 }
d6e956e5 11424
81b1e71c 11425 for (breakpoint *iter : found)
80f8a6eb 11426 {
c5aa993b 11427 if (from_tty)
81b1e71c
TT
11428 printf_unfiltered ("%d ", iter->number);
11429 delete_breakpoint (iter);
c906108c 11430 }
80f8a6eb
MS
11431 if (from_tty)
11432 putchar_unfiltered ('\n');
c906108c
SS
11433}
11434\f
11435/* Delete breakpoint in BS if they are `delete' breakpoints and
11436 all breakpoints that are marked for deletion, whether hit or not.
11437 This is called after any breakpoint is hit, or after errors. */
11438
11439void
fba45db2 11440breakpoint_auto_delete (bpstat bs)
c906108c 11441{
35df4500 11442 struct breakpoint *b, *b_tmp;
c906108c
SS
11443
11444 for (; bs; bs = bs->next)
f431efe5
PA
11445 if (bs->breakpoint_at
11446 && bs->breakpoint_at->disposition == disp_del
c906108c 11447 && bs->stop)
f431efe5 11448 delete_breakpoint (bs->breakpoint_at);
c906108c 11449
35df4500 11450 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 11451 {
b5de0fa7 11452 if (b->disposition == disp_del_at_next_stop)
c5aa993b
JM
11453 delete_breakpoint (b);
11454 }
c906108c
SS
11455}
11456
4a64f543
MS
11457/* A comparison function for bp_location AP and BP being interfaced to
11458 qsort. Sort elements primarily by their ADDRESS (no matter what
cb1e4e32
PA
11459 bl_address_is_meaningful says), secondarily by ordering first
11460 permanent elements and terciarily just ensuring the array is sorted
11461 stable way despite qsort being an unstable algorithm. */
876fa593
JK
11462
11463static int
f5336ca5 11464bp_locations_compare (const void *ap, const void *bp)
876fa593 11465{
9a3c8263
SM
11466 const struct bp_location *a = *(const struct bp_location **) ap;
11467 const struct bp_location *b = *(const struct bp_location **) bp;
876fa593
JK
11468
11469 if (a->address != b->address)
11470 return (a->address > b->address) - (a->address < b->address);
11471
dea2aa5f
LM
11472 /* Sort locations at the same address by their pspace number, keeping
11473 locations of the same inferior (in a multi-inferior environment)
11474 grouped. */
11475
11476 if (a->pspace->num != b->pspace->num)
11477 return ((a->pspace->num > b->pspace->num)
11478 - (a->pspace->num < b->pspace->num));
11479
876fa593 11480 /* Sort permanent breakpoints first. */
1a853c52
PA
11481 if (a->permanent != b->permanent)
11482 return (a->permanent < b->permanent) - (a->permanent > b->permanent);
876fa593 11483
c56a97f9
JK
11484 /* Make the internal GDB representation stable across GDB runs
11485 where A and B memory inside GDB can differ. Breakpoint locations of
11486 the same type at the same address can be sorted in arbitrary order. */
876fa593
JK
11487
11488 if (a->owner->number != b->owner->number)
c56a97f9
JK
11489 return ((a->owner->number > b->owner->number)
11490 - (a->owner->number < b->owner->number));
876fa593
JK
11491
11492 return (a > b) - (a < b);
11493}
11494
f5336ca5
PA
11495/* Set bp_locations_placed_address_before_address_max and
11496 bp_locations_shadow_len_after_address_max according to the current
11497 content of the bp_locations array. */
f7545552
TT
11498
11499static void
f5336ca5 11500bp_locations_target_extensions_update (void)
f7545552 11501{
876fa593
JK
11502 struct bp_location *bl, **blp_tmp;
11503
f5336ca5
PA
11504 bp_locations_placed_address_before_address_max = 0;
11505 bp_locations_shadow_len_after_address_max = 0;
876fa593
JK
11506
11507 ALL_BP_LOCATIONS (bl, blp_tmp)
11508 {
11509 CORE_ADDR start, end, addr;
11510
11511 if (!bp_location_has_shadow (bl))
11512 continue;
11513
11514 start = bl->target_info.placed_address;
11515 end = start + bl->target_info.shadow_len;
11516
11517 gdb_assert (bl->address >= start);
11518 addr = bl->address - start;
f5336ca5
PA
11519 if (addr > bp_locations_placed_address_before_address_max)
11520 bp_locations_placed_address_before_address_max = addr;
876fa593
JK
11521
11522 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
11523
11524 gdb_assert (bl->address < end);
11525 addr = end - bl->address;
f5336ca5
PA
11526 if (addr > bp_locations_shadow_len_after_address_max)
11527 bp_locations_shadow_len_after_address_max = addr;
876fa593 11528 }
f7545552
TT
11529}
11530
1e4d1764
YQ
11531/* Download tracepoint locations if they haven't been. */
11532
11533static void
11534download_tracepoint_locations (void)
11535{
7ed2c994 11536 struct breakpoint *b;
dd2e65cc 11537 enum tribool can_download_tracepoint = TRIBOOL_UNKNOWN;
1e4d1764 11538
5ed8105e 11539 scoped_restore_current_pspace_and_thread restore_pspace_thread;
1e4d1764 11540
7ed2c994 11541 ALL_TRACEPOINTS (b)
1e4d1764 11542 {
7ed2c994 11543 struct bp_location *bl;
1e4d1764 11544 struct tracepoint *t;
f2a8bc8a 11545 int bp_location_downloaded = 0;
1e4d1764 11546
7ed2c994 11547 if ((b->type == bp_fast_tracepoint
1e4d1764
YQ
11548 ? !may_insert_fast_tracepoints
11549 : !may_insert_tracepoints))
11550 continue;
11551
dd2e65cc
YQ
11552 if (can_download_tracepoint == TRIBOOL_UNKNOWN)
11553 {
11554 if (target_can_download_tracepoint ())
11555 can_download_tracepoint = TRIBOOL_TRUE;
11556 else
11557 can_download_tracepoint = TRIBOOL_FALSE;
11558 }
11559
11560 if (can_download_tracepoint == TRIBOOL_FALSE)
11561 break;
11562
7ed2c994
YQ
11563 for (bl = b->loc; bl; bl = bl->next)
11564 {
11565 /* In tracepoint, locations are _never_ duplicated, so
11566 should_be_inserted is equivalent to
11567 unduplicated_should_be_inserted. */
11568 if (!should_be_inserted (bl) || bl->inserted)
11569 continue;
1e4d1764 11570
7ed2c994 11571 switch_to_program_space_and_thread (bl->pspace);
1e4d1764 11572
7ed2c994 11573 target_download_tracepoint (bl);
1e4d1764 11574
7ed2c994 11575 bl->inserted = 1;
f2a8bc8a 11576 bp_location_downloaded = 1;
7ed2c994
YQ
11577 }
11578 t = (struct tracepoint *) b;
11579 t->number_on_target = b->number;
f2a8bc8a 11580 if (bp_location_downloaded)
76727919 11581 gdb::observers::breakpoint_modified.notify (b);
1e4d1764 11582 }
1e4d1764
YQ
11583}
11584
934709f0
PW
11585/* Swap the insertion/duplication state between two locations. */
11586
11587static void
11588swap_insertion (struct bp_location *left, struct bp_location *right)
11589{
11590 const int left_inserted = left->inserted;
11591 const int left_duplicate = left->duplicate;
b775012e 11592 const int left_needs_update = left->needs_update;
934709f0
PW
11593 const struct bp_target_info left_target_info = left->target_info;
11594
1e4d1764
YQ
11595 /* Locations of tracepoints can never be duplicated. */
11596 if (is_tracepoint (left->owner))
11597 gdb_assert (!left->duplicate);
11598 if (is_tracepoint (right->owner))
11599 gdb_assert (!right->duplicate);
11600
934709f0
PW
11601 left->inserted = right->inserted;
11602 left->duplicate = right->duplicate;
b775012e 11603 left->needs_update = right->needs_update;
934709f0
PW
11604 left->target_info = right->target_info;
11605 right->inserted = left_inserted;
11606 right->duplicate = left_duplicate;
b775012e 11607 right->needs_update = left_needs_update;
934709f0
PW
11608 right->target_info = left_target_info;
11609}
11610
b775012e
LM
11611/* Force the re-insertion of the locations at ADDRESS. This is called
11612 once a new/deleted/modified duplicate location is found and we are evaluating
11613 conditions on the target's side. Such conditions need to be updated on
11614 the target. */
11615
11616static void
11617force_breakpoint_reinsertion (struct bp_location *bl)
11618{
11619 struct bp_location **locp = NULL, **loc2p;
11620 struct bp_location *loc;
11621 CORE_ADDR address = 0;
11622 int pspace_num;
11623
11624 address = bl->address;
11625 pspace_num = bl->pspace->num;
11626
11627 /* This is only meaningful if the target is
11628 evaluating conditions and if the user has
11629 opted for condition evaluation on the target's
11630 side. */
11631 if (gdb_evaluates_breakpoint_condition_p ()
11632 || !target_supports_evaluation_of_breakpoint_conditions ())
11633 return;
11634
11635 /* Flag all breakpoint locations with this address and
11636 the same program space as the location
11637 as "its condition has changed". We need to
11638 update the conditions on the target's side. */
11639 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, address)
11640 {
11641 loc = *loc2p;
11642
11643 if (!is_breakpoint (loc->owner)
11644 || pspace_num != loc->pspace->num)
11645 continue;
11646
11647 /* Flag the location appropriately. We use a different state to
11648 let everyone know that we already updated the set of locations
11649 with addr bl->address and program space bl->pspace. This is so
11650 we don't have to keep calling these functions just to mark locations
11651 that have already been marked. */
11652 loc->condition_changed = condition_updated;
11653
11654 /* Free the agent expression bytecode as well. We will compute
11655 it later on. */
833177a4 11656 loc->cond_bytecode.reset ();
b775012e
LM
11657 }
11658}
44702360
PA
11659/* Called whether new breakpoints are created, or existing breakpoints
11660 deleted, to update the global location list and recompute which
11661 locations are duplicate of which.
b775012e 11662
04086b45
PA
11663 The INSERT_MODE flag determines whether locations may not, may, or
11664 shall be inserted now. See 'enum ugll_insert_mode' for more
11665 info. */
b60e7edf 11666
0d381245 11667static void
44702360 11668update_global_location_list (enum ugll_insert_mode insert_mode)
0d381245 11669{
74960c60 11670 struct breakpoint *b;
876fa593 11671 struct bp_location **locp, *loc;
b775012e
LM
11672 /* Last breakpoint location address that was marked for update. */
11673 CORE_ADDR last_addr = 0;
11674 /* Last breakpoint location program space that was marked for update. */
11675 int last_pspace_num = -1;
f7545552 11676
2d134ed3
PA
11677 /* Used in the duplicates detection below. When iterating over all
11678 bp_locations, points to the first bp_location of a given address.
11679 Breakpoints and watchpoints of different types are never
11680 duplicates of each other. Keep one pointer for each type of
11681 breakpoint/watchpoint, so we only need to loop over all locations
11682 once. */
11683 struct bp_location *bp_loc_first; /* breakpoint */
11684 struct bp_location *wp_loc_first; /* hardware watchpoint */
11685 struct bp_location *awp_loc_first; /* access watchpoint */
11686 struct bp_location *rwp_loc_first; /* read watchpoint */
876fa593 11687
f5336ca5
PA
11688 /* Saved former bp_locations array which we compare against the newly
11689 built bp_locations from the current state of ALL_BREAKPOINTS. */
81b1e71c 11690 struct bp_location **old_locp;
f5336ca5 11691 unsigned old_locations_count;
81b1e71c 11692 gdb::unique_xmalloc_ptr<struct bp_location *> old_locations (bp_locations);
876fa593 11693
f5336ca5
PA
11694 old_locations_count = bp_locations_count;
11695 bp_locations = NULL;
11696 bp_locations_count = 0;
0d381245 11697
74960c60 11698 ALL_BREAKPOINTS (b)
876fa593 11699 for (loc = b->loc; loc; loc = loc->next)
f5336ca5 11700 bp_locations_count++;
876fa593 11701
f5336ca5
PA
11702 bp_locations = XNEWVEC (struct bp_location *, bp_locations_count);
11703 locp = bp_locations;
876fa593
JK
11704 ALL_BREAKPOINTS (b)
11705 for (loc = b->loc; loc; loc = loc->next)
11706 *locp++ = loc;
f5336ca5
PA
11707 qsort (bp_locations, bp_locations_count, sizeof (*bp_locations),
11708 bp_locations_compare);
876fa593 11709
f5336ca5 11710 bp_locations_target_extensions_update ();
74960c60 11711
4a64f543
MS
11712 /* Identify bp_location instances that are no longer present in the
11713 new list, and therefore should be freed. Note that it's not
11714 necessary that those locations should be removed from inferior --
11715 if there's another location at the same address (previously
11716 marked as duplicate), we don't need to remove/insert the
11717 location.
876fa593 11718
4a64f543
MS
11719 LOCP is kept in sync with OLD_LOCP, each pointing to the current
11720 and former bp_location array state respectively. */
876fa593 11721
f5336ca5 11722 locp = bp_locations;
81b1e71c
TT
11723 for (old_locp = old_locations.get ();
11724 old_locp < old_locations.get () + old_locations_count;
876fa593 11725 old_locp++)
74960c60 11726 {
876fa593 11727 struct bp_location *old_loc = *old_locp;
c7d46a38 11728 struct bp_location **loc2p;
876fa593 11729
e5dd4106 11730 /* Tells if 'old_loc' is found among the new locations. If
4a64f543 11731 not, we have to free it. */
c7d46a38 11732 int found_object = 0;
20874c92
VP
11733 /* Tells if the location should remain inserted in the target. */
11734 int keep_in_target = 0;
11735 int removed = 0;
876fa593 11736
4a64f543
MS
11737 /* Skip LOCP entries which will definitely never be needed.
11738 Stop either at or being the one matching OLD_LOC. */
f5336ca5 11739 while (locp < bp_locations + bp_locations_count
c7d46a38 11740 && (*locp)->address < old_loc->address)
876fa593 11741 locp++;
c7d46a38
PA
11742
11743 for (loc2p = locp;
f5336ca5 11744 (loc2p < bp_locations + bp_locations_count
c7d46a38
PA
11745 && (*loc2p)->address == old_loc->address);
11746 loc2p++)
11747 {
b775012e
LM
11748 /* Check if this is a new/duplicated location or a duplicated
11749 location that had its condition modified. If so, we want to send
11750 its condition to the target if evaluation of conditions is taking
11751 place there. */
11752 if ((*loc2p)->condition_changed == condition_modified
11753 && (last_addr != old_loc->address
11754 || last_pspace_num != old_loc->pspace->num))
c7d46a38 11755 {
b775012e
LM
11756 force_breakpoint_reinsertion (*loc2p);
11757 last_pspace_num = old_loc->pspace->num;
c7d46a38 11758 }
b775012e
LM
11759
11760 if (*loc2p == old_loc)
11761 found_object = 1;
c7d46a38 11762 }
74960c60 11763
b775012e
LM
11764 /* We have already handled this address, update it so that we don't
11765 have to go through updates again. */
11766 last_addr = old_loc->address;
11767
11768 /* Target-side condition evaluation: Handle deleted locations. */
11769 if (!found_object)
11770 force_breakpoint_reinsertion (old_loc);
11771
4a64f543
MS
11772 /* If this location is no longer present, and inserted, look if
11773 there's maybe a new location at the same address. If so,
11774 mark that one inserted, and don't remove this one. This is
11775 needed so that we don't have a time window where a breakpoint
11776 at certain location is not inserted. */
74960c60 11777
876fa593 11778 if (old_loc->inserted)
0d381245 11779 {
4a64f543
MS
11780 /* If the location is inserted now, we might have to remove
11781 it. */
74960c60 11782
876fa593 11783 if (found_object && should_be_inserted (old_loc))
74960c60 11784 {
4a64f543
MS
11785 /* The location is still present in the location list,
11786 and still should be inserted. Don't do anything. */
20874c92 11787 keep_in_target = 1;
74960c60
VP
11788 }
11789 else
11790 {
b775012e
LM
11791 /* This location still exists, but it won't be kept in the
11792 target since it may have been disabled. We proceed to
11793 remove its target-side condition. */
11794
4a64f543
MS
11795 /* The location is either no longer present, or got
11796 disabled. See if there's another location at the
11797 same address, in which case we don't need to remove
11798 this one from the target. */
876fa593 11799
2bdf28a0 11800 /* OLD_LOC comes from existing struct breakpoint. */
cb1e4e32 11801 if (bl_address_is_meaningful (old_loc))
876fa593 11802 {
876fa593 11803 for (loc2p = locp;
f5336ca5 11804 (loc2p < bp_locations + bp_locations_count
c7d46a38 11805 && (*loc2p)->address == old_loc->address);
876fa593
JK
11806 loc2p++)
11807 {
11808 struct bp_location *loc2 = *loc2p;
11809
2d134ed3 11810 if (breakpoint_locations_match (loc2, old_loc))
c7d46a38 11811 {
85d721b8
PA
11812 /* Read watchpoint locations are switched to
11813 access watchpoints, if the former are not
11814 supported, but the latter are. */
11815 if (is_hardware_watchpoint (old_loc->owner))
11816 {
11817 gdb_assert (is_hardware_watchpoint (loc2->owner));
11818 loc2->watchpoint_type = old_loc->watchpoint_type;
11819 }
11820
934709f0
PW
11821 /* loc2 is a duplicated location. We need to check
11822 if it should be inserted in case it will be
11823 unduplicated. */
11824 if (loc2 != old_loc
11825 && unduplicated_should_be_inserted (loc2))
c7d46a38 11826 {
934709f0 11827 swap_insertion (old_loc, loc2);
c7d46a38
PA
11828 keep_in_target = 1;
11829 break;
11830 }
876fa593
JK
11831 }
11832 }
11833 }
74960c60
VP
11834 }
11835
20874c92
VP
11836 if (!keep_in_target)
11837 {
834c0d03 11838 if (remove_breakpoint (old_loc))
20874c92 11839 {
4a64f543
MS
11840 /* This is just about all we can do. We could keep
11841 this location on the global list, and try to
11842 remove it next time, but there's no particular
11843 reason why we will succeed next time.
20874c92 11844
4a64f543
MS
11845 Note that at this point, old_loc->owner is still
11846 valid, as delete_breakpoint frees the breakpoint
11847 only after calling us. */
3e43a32a
MS
11848 printf_filtered (_("warning: Error removing "
11849 "breakpoint %d\n"),
876fa593 11850 old_loc->owner->number);
20874c92
VP
11851 }
11852 removed = 1;
11853 }
0d381245 11854 }
74960c60
VP
11855
11856 if (!found_object)
1c5cfe86 11857 {
fbea99ea 11858 if (removed && target_is_non_stop_p ()
1cf4d951 11859 && need_moribund_for_location_type (old_loc))
20874c92 11860 {
db82e815
PA
11861 /* This location was removed from the target. In
11862 non-stop mode, a race condition is possible where
11863 we've removed a breakpoint, but stop events for that
11864 breakpoint are already queued and will arrive later.
11865 We apply an heuristic to be able to distinguish such
11866 SIGTRAPs from other random SIGTRAPs: we keep this
11867 breakpoint location for a bit, and will retire it
11868 after we see some number of events. The theory here
11869 is that reporting of events should, "on the average",
11870 be fair, so after a while we'll see events from all
11871 threads that have anything of interest, and no longer
11872 need to keep this breakpoint location around. We
11873 don't hold locations forever so to reduce chances of
11874 mistaking a non-breakpoint SIGTRAP for a breakpoint
11875 SIGTRAP.
11876
11877 The heuristic failing can be disastrous on
11878 decr_pc_after_break targets.
11879
11880 On decr_pc_after_break targets, like e.g., x86-linux,
11881 if we fail to recognize a late breakpoint SIGTRAP,
11882 because events_till_retirement has reached 0 too
11883 soon, we'll fail to do the PC adjustment, and report
11884 a random SIGTRAP to the user. When the user resumes
11885 the inferior, it will most likely immediately crash
2dec564e 11886 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
db82e815
PA
11887 corrupted, because of being resumed e.g., in the
11888 middle of a multi-byte instruction, or skipped a
11889 one-byte instruction. This was actually seen happen
11890 on native x86-linux, and should be less rare on
11891 targets that do not support new thread events, like
11892 remote, due to the heuristic depending on
11893 thread_count.
11894
11895 Mistaking a random SIGTRAP for a breakpoint trap
11896 causes similar symptoms (PC adjustment applied when
11897 it shouldn't), but then again, playing with SIGTRAPs
11898 behind the debugger's back is asking for trouble.
11899
11900 Since hardware watchpoint traps are always
11901 distinguishable from other traps, so we don't need to
11902 apply keep hardware watchpoint moribund locations
11903 around. We simply always ignore hardware watchpoint
11904 traps we can no longer explain. */
11905
876fa593
JK
11906 old_loc->events_till_retirement = 3 * (thread_count () + 1);
11907 old_loc->owner = NULL;
20874c92 11908
1123588c 11909 moribund_locations.push_back (old_loc);
1c5cfe86
PA
11910 }
11911 else
f431efe5
PA
11912 {
11913 old_loc->owner = NULL;
11914 decref_bp_location (&old_loc);
11915 }
20874c92 11916 }
74960c60 11917 }
1c5cfe86 11918
348d480f
PA
11919 /* Rescan breakpoints at the same address and section, marking the
11920 first one as "first" and any others as "duplicates". This is so
11921 that the bpt instruction is only inserted once. If we have a
11922 permanent breakpoint at the same place as BPT, make that one the
11923 official one, and the rest as duplicates. Permanent breakpoints
11924 are sorted first for the same address.
11925
11926 Do the same for hardware watchpoints, but also considering the
11927 watchpoint's type (regular/access/read) and length. */
11928
11929 bp_loc_first = NULL;
11930 wp_loc_first = NULL;
11931 awp_loc_first = NULL;
11932 rwp_loc_first = NULL;
11933 ALL_BP_LOCATIONS (loc, locp)
11934 {
11935 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
11936 non-NULL. */
348d480f 11937 struct bp_location **loc_first_p;
d3fbdd86 11938 b = loc->owner;
348d480f 11939
6f380991 11940 if (!unduplicated_should_be_inserted (loc)
cb1e4e32 11941 || !bl_address_is_meaningful (loc)
1e4d1764
YQ
11942 /* Don't detect duplicate for tracepoint locations because they are
11943 never duplicated. See the comments in field `duplicate' of
11944 `struct bp_location'. */
348d480f 11945 || is_tracepoint (b))
b775012e
LM
11946 {
11947 /* Clear the condition modification flag. */
11948 loc->condition_changed = condition_unchanged;
11949 continue;
11950 }
348d480f 11951
348d480f
PA
11952 if (b->type == bp_hardware_watchpoint)
11953 loc_first_p = &wp_loc_first;
11954 else if (b->type == bp_read_watchpoint)
11955 loc_first_p = &rwp_loc_first;
11956 else if (b->type == bp_access_watchpoint)
11957 loc_first_p = &awp_loc_first;
11958 else
11959 loc_first_p = &bp_loc_first;
11960
11961 if (*loc_first_p == NULL
11962 || (overlay_debugging && loc->section != (*loc_first_p)->section)
11963 || !breakpoint_locations_match (loc, *loc_first_p))
11964 {
11965 *loc_first_p = loc;
11966 loc->duplicate = 0;
b775012e
LM
11967
11968 if (is_breakpoint (loc->owner) && loc->condition_changed)
11969 {
11970 loc->needs_update = 1;
11971 /* Clear the condition modification flag. */
11972 loc->condition_changed = condition_unchanged;
11973 }
348d480f
PA
11974 continue;
11975 }
11976
934709f0
PW
11977
11978 /* This and the above ensure the invariant that the first location
11979 is not duplicated, and is the inserted one.
11980 All following are marked as duplicated, and are not inserted. */
11981 if (loc->inserted)
11982 swap_insertion (loc, *loc_first_p);
348d480f
PA
11983 loc->duplicate = 1;
11984
b775012e
LM
11985 /* Clear the condition modification flag. */
11986 loc->condition_changed = condition_unchanged;
348d480f
PA
11987 }
11988
a25a5a45 11989 if (insert_mode == UGLL_INSERT || breakpoints_should_be_inserted_now ())
b775012e 11990 {
04086b45 11991 if (insert_mode != UGLL_DONT_INSERT)
b775012e
LM
11992 insert_breakpoint_locations ();
11993 else
11994 {
44702360
PA
11995 /* Even though the caller told us to not insert new
11996 locations, we may still need to update conditions on the
11997 target's side of breakpoints that were already inserted
11998 if the target is evaluating breakpoint conditions. We
b775012e
LM
11999 only update conditions for locations that are marked
12000 "needs_update". */
12001 update_inserted_breakpoint_locations ();
12002 }
12003 }
348d480f 12004
04086b45 12005 if (insert_mode != UGLL_DONT_INSERT)
1e4d1764 12006 download_tracepoint_locations ();
348d480f
PA
12007}
12008
12009void
12010breakpoint_retire_moribund (void)
12011{
1123588c
TT
12012 for (int ix = 0; ix < moribund_locations.size (); ++ix)
12013 {
12014 struct bp_location *loc = moribund_locations[ix];
12015 if (--(loc->events_till_retirement) == 0)
12016 {
12017 decref_bp_location (&loc);
12018 unordered_remove (moribund_locations, ix);
12019 --ix;
12020 }
12021 }
348d480f
PA
12022}
12023
12024static void
44702360 12025update_global_location_list_nothrow (enum ugll_insert_mode insert_mode)
348d480f 12026{
348d480f 12027
a70b8144 12028 try
492d29ea
PA
12029 {
12030 update_global_location_list (insert_mode);
12031 }
230d2906 12032 catch (const gdb_exception_error &e)
492d29ea
PA
12033 {
12034 }
348d480f
PA
12035}
12036
12037/* Clear BKP from a BPS. */
12038
12039static void
12040bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
12041{
12042 bpstat bs;
12043
12044 for (bs = bps; bs; bs = bs->next)
12045 if (bs->breakpoint_at == bpt)
12046 {
12047 bs->breakpoint_at = NULL;
12048 bs->old_val = NULL;
12049 /* bs->commands will be freed later. */
12050 }
12051}
12052
12053/* Callback for iterate_over_threads. */
12054static int
12055bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
12056{
9a3c8263 12057 struct breakpoint *bpt = (struct breakpoint *) data;
348d480f
PA
12058
12059 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
12060 return 0;
12061}
12062
12063/* Helper for breakpoint and tracepoint breakpoint_ops->mention
12064 callbacks. */
12065
12066static void
12067say_where (struct breakpoint *b)
12068{
12069 struct value_print_options opts;
12070
12071 get_user_print_options (&opts);
12072
12073 /* i18n: cagney/2005-02-11: Below needs to be merged into a
12074 single string. */
12075 if (b->loc == NULL)
12076 {
f00aae0f
KS
12077 /* For pending locations, the output differs slightly based
12078 on b->extra_string. If this is non-NULL, it contains either
12079 a condition or dprintf arguments. */
12080 if (b->extra_string == NULL)
12081 {
12082 printf_filtered (_(" (%s) pending."),
d28cd78a 12083 event_location_to_string (b->location.get ()));
f00aae0f
KS
12084 }
12085 else if (b->type == bp_dprintf)
12086 {
12087 printf_filtered (_(" (%s,%s) pending."),
d28cd78a 12088 event_location_to_string (b->location.get ()),
f00aae0f
KS
12089 b->extra_string);
12090 }
12091 else
12092 {
12093 printf_filtered (_(" (%s %s) pending."),
d28cd78a 12094 event_location_to_string (b->location.get ()),
f00aae0f
KS
12095 b->extra_string);
12096 }
348d480f
PA
12097 }
12098 else
12099 {
2f202fde 12100 if (opts.addressprint || b->loc->symtab == NULL)
348d480f
PA
12101 {
12102 printf_filtered (" at ");
2636d81d
PW
12103 fputs_styled (paddress (b->loc->gdbarch, b->loc->address),
12104 address_style.style (),
12105 gdb_stdout);
348d480f 12106 }
2f202fde 12107 if (b->loc->symtab != NULL)
f8eba3c6
TT
12108 {
12109 /* If there is a single location, we can print the location
12110 more nicely. */
12111 if (b->loc->next == NULL)
0bb296cb
TT
12112 {
12113 puts_filtered (": file ");
12114 fputs_styled (symtab_to_filename_for_display (b->loc->symtab),
12115 file_name_style.style (),
12116 gdb_stdout);
12117 printf_filtered (", line %d.",
12118 b->loc->line_number);
12119 }
f8eba3c6
TT
12120 else
12121 /* This is not ideal, but each location may have a
12122 different file name, and this at least reflects the
12123 real situation somewhat. */
f00aae0f 12124 printf_filtered (": %s.",
d28cd78a 12125 event_location_to_string (b->location.get ()));
f8eba3c6 12126 }
348d480f
PA
12127
12128 if (b->loc->next)
12129 {
12130 struct bp_location *loc = b->loc;
12131 int n = 0;
12132 for (; loc; loc = loc->next)
12133 ++n;
12134 printf_filtered (" (%d locations)", n);
12135 }
12136 }
12137}
12138
5f486660 12139bp_location::~bp_location ()
348d480f 12140{
5f486660 12141 xfree (function_name);
348d480f
PA
12142}
12143
c1fc2657 12144/* Destructor for the breakpoint base class. */
348d480f 12145
c1fc2657 12146breakpoint::~breakpoint ()
348d480f 12147{
c1fc2657
SM
12148 xfree (this->cond_string);
12149 xfree (this->extra_string);
12150 xfree (this->filter);
348d480f
PA
12151}
12152
2060206e
PA
12153static struct bp_location *
12154base_breakpoint_allocate_location (struct breakpoint *self)
348d480f 12155{
5f486660 12156 return new bp_location (self);
348d480f
PA
12157}
12158
2060206e
PA
12159static void
12160base_breakpoint_re_set (struct breakpoint *b)
12161{
12162 /* Nothing to re-set. */
12163}
12164
12165#define internal_error_pure_virtual_called() \
12166 gdb_assert_not_reached ("pure virtual function called")
12167
12168static int
12169base_breakpoint_insert_location (struct bp_location *bl)
12170{
12171 internal_error_pure_virtual_called ();
12172}
12173
12174static int
73971819
PA
12175base_breakpoint_remove_location (struct bp_location *bl,
12176 enum remove_bp_reason reason)
2060206e
PA
12177{
12178 internal_error_pure_virtual_called ();
12179}
12180
12181static int
12182base_breakpoint_breakpoint_hit (const struct bp_location *bl,
bd522513 12183 const address_space *aspace,
09ac7c10
TT
12184 CORE_ADDR bp_addr,
12185 const struct target_waitstatus *ws)
2060206e
PA
12186{
12187 internal_error_pure_virtual_called ();
12188}
12189
12190static void
12191base_breakpoint_check_status (bpstat bs)
12192{
12193 /* Always stop. */
12194}
12195
12196/* A "works_in_software_mode" breakpoint_ops method that just internal
12197 errors. */
12198
12199static int
12200base_breakpoint_works_in_software_mode (const struct breakpoint *b)
12201{
12202 internal_error_pure_virtual_called ();
12203}
12204
12205/* A "resources_needed" breakpoint_ops method that just internal
12206 errors. */
12207
12208static int
12209base_breakpoint_resources_needed (const struct bp_location *bl)
12210{
12211 internal_error_pure_virtual_called ();
12212}
12213
12214static enum print_stop_action
12215base_breakpoint_print_it (bpstat bs)
12216{
12217 internal_error_pure_virtual_called ();
12218}
12219
12220static void
12221base_breakpoint_print_one_detail (const struct breakpoint *self,
12222 struct ui_out *uiout)
12223{
12224 /* nothing */
12225}
12226
12227static void
12228base_breakpoint_print_mention (struct breakpoint *b)
12229{
12230 internal_error_pure_virtual_called ();
12231}
12232
12233static void
12234base_breakpoint_print_recreate (struct breakpoint *b, struct ui_file *fp)
12235{
12236 internal_error_pure_virtual_called ();
12237}
12238
983af33b 12239static void
f00aae0f
KS
12240base_breakpoint_create_sals_from_location
12241 (const struct event_location *location,
12242 struct linespec_result *canonical,
12243 enum bptype type_wanted)
983af33b
SDJ
12244{
12245 internal_error_pure_virtual_called ();
12246}
12247
12248static void
12249base_breakpoint_create_breakpoints_sal (struct gdbarch *gdbarch,
12250 struct linespec_result *c,
e1e01040
PA
12251 gdb::unique_xmalloc_ptr<char> cond_string,
12252 gdb::unique_xmalloc_ptr<char> extra_string,
983af33b
SDJ
12253 enum bptype type_wanted,
12254 enum bpdisp disposition,
12255 int thread,
12256 int task, int ignore_count,
12257 const struct breakpoint_ops *o,
12258 int from_tty, int enabled,
44f238bb 12259 int internal, unsigned flags)
983af33b
SDJ
12260{
12261 internal_error_pure_virtual_called ();
12262}
12263
6c5b2ebe 12264static std::vector<symtab_and_line>
f00aae0f
KS
12265base_breakpoint_decode_location (struct breakpoint *b,
12266 const struct event_location *location,
6c5b2ebe 12267 struct program_space *search_pspace)
983af33b
SDJ
12268{
12269 internal_error_pure_virtual_called ();
12270}
12271
ab04a2af
TT
12272/* The default 'explains_signal' method. */
12273
47591c29 12274static int
427cd150 12275base_breakpoint_explains_signal (struct breakpoint *b, enum gdb_signal sig)
ab04a2af 12276{
47591c29 12277 return 1;
ab04a2af
TT
12278}
12279
9d6e6e84
HZ
12280/* The default "after_condition_true" method. */
12281
12282static void
12283base_breakpoint_after_condition_true (struct bpstats *bs)
12284{
12285 /* Nothing to do. */
12286}
12287
ab04a2af 12288struct breakpoint_ops base_breakpoint_ops =
2060206e 12289{
2060206e
PA
12290 base_breakpoint_allocate_location,
12291 base_breakpoint_re_set,
12292 base_breakpoint_insert_location,
12293 base_breakpoint_remove_location,
12294 base_breakpoint_breakpoint_hit,
12295 base_breakpoint_check_status,
12296 base_breakpoint_resources_needed,
12297 base_breakpoint_works_in_software_mode,
12298 base_breakpoint_print_it,
12299 NULL,
12300 base_breakpoint_print_one_detail,
12301 base_breakpoint_print_mention,
983af33b 12302 base_breakpoint_print_recreate,
5f700d83 12303 base_breakpoint_create_sals_from_location,
983af33b 12304 base_breakpoint_create_breakpoints_sal,
5f700d83 12305 base_breakpoint_decode_location,
9d6e6e84
HZ
12306 base_breakpoint_explains_signal,
12307 base_breakpoint_after_condition_true,
2060206e
PA
12308};
12309
12310/* Default breakpoint_ops methods. */
12311
12312static void
348d480f
PA
12313bkpt_re_set (struct breakpoint *b)
12314{
06edf0c0 12315 /* FIXME: is this still reachable? */
9ef9e6a6 12316 if (breakpoint_event_location_empty_p (b))
06edf0c0 12317 {
f00aae0f 12318 /* Anything without a location can't be re-set. */
348d480f 12319 delete_breakpoint (b);
06edf0c0 12320 return;
348d480f 12321 }
06edf0c0
PA
12322
12323 breakpoint_re_set_default (b);
348d480f
PA
12324}
12325
2060206e 12326static int
348d480f
PA
12327bkpt_insert_location (struct bp_location *bl)
12328{
cd6c3b4f
YQ
12329 CORE_ADDR addr = bl->target_info.reqstd_address;
12330
579c6ad9 12331 bl->target_info.kind = breakpoint_kind (bl, &addr);
cd6c3b4f
YQ
12332 bl->target_info.placed_address = addr;
12333
348d480f 12334 if (bl->loc_type == bp_loc_hardware_breakpoint)
7c16b83e 12335 return target_insert_hw_breakpoint (bl->gdbarch, &bl->target_info);
348d480f 12336 else
7c16b83e 12337 return target_insert_breakpoint (bl->gdbarch, &bl->target_info);
348d480f
PA
12338}
12339
2060206e 12340static int
73971819 12341bkpt_remove_location (struct bp_location *bl, enum remove_bp_reason reason)
348d480f
PA
12342{
12343 if (bl->loc_type == bp_loc_hardware_breakpoint)
12344 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
12345 else
73971819 12346 return target_remove_breakpoint (bl->gdbarch, &bl->target_info, reason);
348d480f
PA
12347}
12348
2060206e 12349static int
348d480f 12350bkpt_breakpoint_hit (const struct bp_location *bl,
bd522513 12351 const address_space *aspace, CORE_ADDR bp_addr,
09ac7c10 12352 const struct target_waitstatus *ws)
348d480f 12353{
09ac7c10 12354 if (ws->kind != TARGET_WAITKIND_STOPPED
a493e3e2 12355 || ws->value.sig != GDB_SIGNAL_TRAP)
09ac7c10
TT
12356 return 0;
12357
348d480f
PA
12358 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
12359 aspace, bp_addr))
12360 return 0;
12361
12362 if (overlay_debugging /* unmapped overlay section */
12363 && section_is_overlay (bl->section)
12364 && !section_is_mapped (bl->section))
12365 return 0;
12366
12367 return 1;
12368}
12369
cd1608cc
PA
12370static int
12371dprintf_breakpoint_hit (const struct bp_location *bl,
bd522513 12372 const address_space *aspace, CORE_ADDR bp_addr,
cd1608cc
PA
12373 const struct target_waitstatus *ws)
12374{
12375 if (dprintf_style == dprintf_style_agent
12376 && target_can_run_breakpoint_commands ())
12377 {
12378 /* An agent-style dprintf never causes a stop. If we see a trap
12379 for this address it must be for a breakpoint that happens to
12380 be set at the same address. */
12381 return 0;
12382 }
12383
12384 return bkpt_breakpoint_hit (bl, aspace, bp_addr, ws);
12385}
12386
2060206e 12387static int
348d480f
PA
12388bkpt_resources_needed (const struct bp_location *bl)
12389{
12390 gdb_assert (bl->owner->type == bp_hardware_breakpoint);
12391
12392 return 1;
12393}
12394
2060206e 12395static enum print_stop_action
348d480f
PA
12396bkpt_print_it (bpstat bs)
12397{
348d480f
PA
12398 struct breakpoint *b;
12399 const struct bp_location *bl;
001c8c33 12400 int bp_temp;
79a45e25 12401 struct ui_out *uiout = current_uiout;
348d480f
PA
12402
12403 gdb_assert (bs->bp_location_at != NULL);
12404
12405 bl = bs->bp_location_at;
12406 b = bs->breakpoint_at;
12407
001c8c33
PA
12408 bp_temp = b->disposition == disp_del;
12409 if (bl->address != bl->requested_address)
12410 breakpoint_adjustment_warning (bl->requested_address,
12411 bl->address,
12412 b->number, 1);
12413 annotate_breakpoint (b->number);
f303dbd6
PA
12414 maybe_print_thread_hit_breakpoint (uiout);
12415
001c8c33 12416 if (bp_temp)
112e8700 12417 uiout->text ("Temporary breakpoint ");
001c8c33 12418 else
112e8700
SM
12419 uiout->text ("Breakpoint ");
12420 if (uiout->is_mi_like_p ())
348d480f 12421 {
112e8700 12422 uiout->field_string ("reason",
001c8c33 12423 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
112e8700 12424 uiout->field_string ("disp", bpdisp_text (b->disposition));
06edf0c0 12425 }
112e8700
SM
12426 uiout->field_int ("bkptno", b->number);
12427 uiout->text (", ");
06edf0c0 12428
001c8c33 12429 return PRINT_SRC_AND_LOC;
06edf0c0
PA
12430}
12431
2060206e 12432static void
06edf0c0
PA
12433bkpt_print_mention (struct breakpoint *b)
12434{
112e8700 12435 if (current_uiout->is_mi_like_p ())
06edf0c0
PA
12436 return;
12437
12438 switch (b->type)
12439 {
12440 case bp_breakpoint:
12441 case bp_gnu_ifunc_resolver:
12442 if (b->disposition == disp_del)
12443 printf_filtered (_("Temporary breakpoint"));
12444 else
12445 printf_filtered (_("Breakpoint"));
12446 printf_filtered (_(" %d"), b->number);
12447 if (b->type == bp_gnu_ifunc_resolver)
12448 printf_filtered (_(" at gnu-indirect-function resolver"));
12449 break;
12450 case bp_hardware_breakpoint:
12451 printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
12452 break;
e7e0cddf
SS
12453 case bp_dprintf:
12454 printf_filtered (_("Dprintf %d"), b->number);
12455 break;
06edf0c0
PA
12456 }
12457
12458 say_where (b);
12459}
12460
2060206e 12461static void
06edf0c0
PA
12462bkpt_print_recreate (struct breakpoint *tp, struct ui_file *fp)
12463{
12464 if (tp->type == bp_breakpoint && tp->disposition == disp_del)
12465 fprintf_unfiltered (fp, "tbreak");
12466 else if (tp->type == bp_breakpoint)
12467 fprintf_unfiltered (fp, "break");
12468 else if (tp->type == bp_hardware_breakpoint
12469 && tp->disposition == disp_del)
12470 fprintf_unfiltered (fp, "thbreak");
12471 else if (tp->type == bp_hardware_breakpoint)
12472 fprintf_unfiltered (fp, "hbreak");
12473 else
12474 internal_error (__FILE__, __LINE__,
12475 _("unhandled breakpoint type %d"), (int) tp->type);
12476
f00aae0f 12477 fprintf_unfiltered (fp, " %s",
d28cd78a 12478 event_location_to_string (tp->location.get ()));
f00aae0f
KS
12479
12480 /* Print out extra_string if this breakpoint is pending. It might
12481 contain, for example, conditions that were set by the user. */
12482 if (tp->loc == NULL && tp->extra_string != NULL)
12483 fprintf_unfiltered (fp, " %s", tp->extra_string);
12484
dd11a36c 12485 print_recreate_thread (tp, fp);
06edf0c0
PA
12486}
12487
983af33b 12488static void
f00aae0f
KS
12489bkpt_create_sals_from_location (const struct event_location *location,
12490 struct linespec_result *canonical,
12491 enum bptype type_wanted)
983af33b 12492{
f00aae0f 12493 create_sals_from_location_default (location, canonical, type_wanted);
983af33b
SDJ
12494}
12495
12496static void
12497bkpt_create_breakpoints_sal (struct gdbarch *gdbarch,
12498 struct linespec_result *canonical,
e1e01040
PA
12499 gdb::unique_xmalloc_ptr<char> cond_string,
12500 gdb::unique_xmalloc_ptr<char> extra_string,
983af33b
SDJ
12501 enum bptype type_wanted,
12502 enum bpdisp disposition,
12503 int thread,
12504 int task, int ignore_count,
12505 const struct breakpoint_ops *ops,
12506 int from_tty, int enabled,
44f238bb 12507 int internal, unsigned flags)
983af33b 12508{
023fa29b 12509 create_breakpoints_sal_default (gdbarch, canonical,
e1e01040
PA
12510 std::move (cond_string),
12511 std::move (extra_string),
e7e0cddf 12512 type_wanted,
983af33b
SDJ
12513 disposition, thread, task,
12514 ignore_count, ops, from_tty,
44f238bb 12515 enabled, internal, flags);
983af33b
SDJ
12516}
12517
6c5b2ebe 12518static std::vector<symtab_and_line>
f00aae0f
KS
12519bkpt_decode_location (struct breakpoint *b,
12520 const struct event_location *location,
6c5b2ebe 12521 struct program_space *search_pspace)
983af33b 12522{
6c5b2ebe 12523 return decode_location_default (b, location, search_pspace);
983af33b
SDJ
12524}
12525
06edf0c0
PA
12526/* Virtual table for internal breakpoints. */
12527
12528static void
12529internal_bkpt_re_set (struct breakpoint *b)
12530{
12531 switch (b->type)
12532 {
12533 /* Delete overlay event and longjmp master breakpoints; they
12534 will be reset later by breakpoint_re_set. */
12535 case bp_overlay_event:
12536 case bp_longjmp_master:
12537 case bp_std_terminate_master:
12538 case bp_exception_master:
12539 delete_breakpoint (b);
12540 break;
12541
12542 /* This breakpoint is special, it's set up when the inferior
12543 starts and we really don't want to touch it. */
12544 case bp_shlib_event:
12545
12546 /* Like bp_shlib_event, this breakpoint type is special. Once
12547 it is set up, we do not want to touch it. */
12548 case bp_thread_event:
12549 break;
12550 }
12551}
12552
12553static void
12554internal_bkpt_check_status (bpstat bs)
12555{
a9b3a50f
PA
12556 if (bs->breakpoint_at->type == bp_shlib_event)
12557 {
12558 /* If requested, stop when the dynamic linker notifies GDB of
12559 events. This allows the user to get control and place
12560 breakpoints in initializer routines for dynamically loaded
12561 objects (among other things). */
12562 bs->stop = stop_on_solib_events;
12563 bs->print = stop_on_solib_events;
12564 }
12565 else
12566 bs->stop = 0;
06edf0c0
PA
12567}
12568
12569static enum print_stop_action
12570internal_bkpt_print_it (bpstat bs)
12571{
06edf0c0 12572 struct breakpoint *b;
06edf0c0 12573
06edf0c0
PA
12574 b = bs->breakpoint_at;
12575
06edf0c0
PA
12576 switch (b->type)
12577 {
348d480f
PA
12578 case bp_shlib_event:
12579 /* Did we stop because the user set the stop_on_solib_events
12580 variable? (If so, we report this as a generic, "Stopped due
12581 to shlib event" message.) */
edcc5120 12582 print_solib_event (0);
348d480f
PA
12583 break;
12584
12585 case bp_thread_event:
12586 /* Not sure how we will get here.
12587 GDB should not stop for these breakpoints. */
12588 printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
12589 break;
12590
12591 case bp_overlay_event:
12592 /* By analogy with the thread event, GDB should not stop for these. */
12593 printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
12594 break;
12595
12596 case bp_longjmp_master:
12597 /* These should never be enabled. */
12598 printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
348d480f
PA
12599 break;
12600
12601 case bp_std_terminate_master:
12602 /* These should never be enabled. */
12603 printf_filtered (_("std::terminate Master Breakpoint: "
12604 "gdb should not stop!\n"));
348d480f
PA
12605 break;
12606
12607 case bp_exception_master:
12608 /* These should never be enabled. */
12609 printf_filtered (_("Exception Master Breakpoint: "
12610 "gdb should not stop!\n"));
06edf0c0
PA
12611 break;
12612 }
12613
001c8c33 12614 return PRINT_NOTHING;
06edf0c0
PA
12615}
12616
12617static void
12618internal_bkpt_print_mention (struct breakpoint *b)
12619{
12620 /* Nothing to mention. These breakpoints are internal. */
12621}
12622
06edf0c0
PA
12623/* Virtual table for momentary breakpoints */
12624
12625static void
12626momentary_bkpt_re_set (struct breakpoint *b)
12627{
12628 /* Keep temporary breakpoints, which can be encountered when we step
4d1eb6b4 12629 over a dlopen call and solib_add is resetting the breakpoints.
06edf0c0
PA
12630 Otherwise these should have been blown away via the cleanup chain
12631 or by breakpoint_init_inferior when we rerun the executable. */
12632}
12633
12634static void
12635momentary_bkpt_check_status (bpstat bs)
12636{
12637 /* Nothing. The point of these breakpoints is causing a stop. */
12638}
12639
12640static enum print_stop_action
12641momentary_bkpt_print_it (bpstat bs)
12642{
001c8c33 12643 return PRINT_UNKNOWN;
348d480f
PA
12644}
12645
06edf0c0
PA
12646static void
12647momentary_bkpt_print_mention (struct breakpoint *b)
348d480f 12648{
06edf0c0 12649 /* Nothing to mention. These breakpoints are internal. */
348d480f
PA
12650}
12651
e2e4d78b
JK
12652/* Ensure INITIATING_FRAME is cleared when no such breakpoint exists.
12653
12654 It gets cleared already on the removal of the first one of such placed
12655 breakpoints. This is OK as they get all removed altogether. */
12656
c1fc2657 12657longjmp_breakpoint::~longjmp_breakpoint ()
e2e4d78b 12658{
c1fc2657 12659 thread_info *tp = find_thread_global_id (this->thread);
e2e4d78b 12660
c1fc2657 12661 if (tp != NULL)
e2e4d78b 12662 tp->initiating_frame = null_frame_id;
e2e4d78b
JK
12663}
12664
55aa24fb
SDJ
12665/* Specific methods for probe breakpoints. */
12666
12667static int
12668bkpt_probe_insert_location (struct bp_location *bl)
12669{
12670 int v = bkpt_insert_location (bl);
12671
12672 if (v == 0)
12673 {
12674 /* The insertion was successful, now let's set the probe's semaphore
12675 if needed. */
935676c9 12676 bl->probe.prob->set_semaphore (bl->probe.objfile, bl->gdbarch);
55aa24fb
SDJ
12677 }
12678
12679 return v;
12680}
12681
12682static int
73971819
PA
12683bkpt_probe_remove_location (struct bp_location *bl,
12684 enum remove_bp_reason reason)
55aa24fb
SDJ
12685{
12686 /* Let's clear the semaphore before removing the location. */
935676c9 12687 bl->probe.prob->clear_semaphore (bl->probe.objfile, bl->gdbarch);
55aa24fb 12688
73971819 12689 return bkpt_remove_location (bl, reason);
55aa24fb
SDJ
12690}
12691
12692static void
f00aae0f 12693bkpt_probe_create_sals_from_location (const struct event_location *location,
5f700d83 12694 struct linespec_result *canonical,
f00aae0f 12695 enum bptype type_wanted)
55aa24fb
SDJ
12696{
12697 struct linespec_sals lsal;
12698
c2f4122d 12699 lsal.sals = parse_probes (location, NULL, canonical);
8e9e35b1
TT
12700 lsal.canonical
12701 = xstrdup (event_location_to_string (canonical->location.get ()));
6c5b2ebe 12702 canonical->lsals.push_back (std::move (lsal));
55aa24fb
SDJ
12703}
12704
6c5b2ebe 12705static std::vector<symtab_and_line>
f00aae0f
KS
12706bkpt_probe_decode_location (struct breakpoint *b,
12707 const struct event_location *location,
6c5b2ebe 12708 struct program_space *search_pspace)
55aa24fb 12709{
6c5b2ebe
PA
12710 std::vector<symtab_and_line> sals = parse_probes (location, search_pspace, NULL);
12711 if (sals.empty ())
55aa24fb 12712 error (_("probe not found"));
6c5b2ebe 12713 return sals;
55aa24fb
SDJ
12714}
12715
348d480f 12716/* The breakpoint_ops structure to be used in tracepoints. */
876fa593 12717
348d480f
PA
12718static void
12719tracepoint_re_set (struct breakpoint *b)
12720{
12721 breakpoint_re_set_default (b);
12722}
876fa593 12723
348d480f
PA
12724static int
12725tracepoint_breakpoint_hit (const struct bp_location *bl,
bd522513 12726 const address_space *aspace, CORE_ADDR bp_addr,
09ac7c10 12727 const struct target_waitstatus *ws)
348d480f
PA
12728{
12729 /* By definition, the inferior does not report stops at
12730 tracepoints. */
12731 return 0;
74960c60
VP
12732}
12733
12734static void
348d480f
PA
12735tracepoint_print_one_detail (const struct breakpoint *self,
12736 struct ui_out *uiout)
74960c60 12737{
d9b3f62e 12738 struct tracepoint *tp = (struct tracepoint *) self;
5d9310c4 12739 if (!tp->static_trace_marker_id.empty ())
348d480f
PA
12740 {
12741 gdb_assert (self->type == bp_static_tracepoint);
cc59ec59 12742
112e8700
SM
12743 uiout->text ("\tmarker id is ");
12744 uiout->field_string ("static-tracepoint-marker-string-id",
d9b3f62e 12745 tp->static_trace_marker_id);
112e8700 12746 uiout->text ("\n");
348d480f 12747 }
0d381245
VP
12748}
12749
a474d7c2 12750static void
348d480f 12751tracepoint_print_mention (struct breakpoint *b)
a474d7c2 12752{
112e8700 12753 if (current_uiout->is_mi_like_p ())
348d480f 12754 return;
cc59ec59 12755
348d480f
PA
12756 switch (b->type)
12757 {
12758 case bp_tracepoint:
12759 printf_filtered (_("Tracepoint"));
12760 printf_filtered (_(" %d"), b->number);
12761 break;
12762 case bp_fast_tracepoint:
12763 printf_filtered (_("Fast tracepoint"));
12764 printf_filtered (_(" %d"), b->number);
12765 break;
12766 case bp_static_tracepoint:
12767 printf_filtered (_("Static tracepoint"));
12768 printf_filtered (_(" %d"), b->number);
12769 break;
12770 default:
12771 internal_error (__FILE__, __LINE__,
12772 _("unhandled tracepoint type %d"), (int) b->type);
12773 }
12774
12775 say_where (b);
a474d7c2
PA
12776}
12777
348d480f 12778static void
d9b3f62e 12779tracepoint_print_recreate (struct breakpoint *self, struct ui_file *fp)
a474d7c2 12780{
d9b3f62e
PA
12781 struct tracepoint *tp = (struct tracepoint *) self;
12782
12783 if (self->type == bp_fast_tracepoint)
348d480f 12784 fprintf_unfiltered (fp, "ftrace");
c93e8391 12785 else if (self->type == bp_static_tracepoint)
348d480f 12786 fprintf_unfiltered (fp, "strace");
d9b3f62e 12787 else if (self->type == bp_tracepoint)
348d480f
PA
12788 fprintf_unfiltered (fp, "trace");
12789 else
12790 internal_error (__FILE__, __LINE__,
d9b3f62e 12791 _("unhandled tracepoint type %d"), (int) self->type);
cc59ec59 12792
f00aae0f 12793 fprintf_unfiltered (fp, " %s",
d28cd78a 12794 event_location_to_string (self->location.get ()));
d9b3f62e
PA
12795 print_recreate_thread (self, fp);
12796
12797 if (tp->pass_count)
12798 fprintf_unfiltered (fp, " passcount %d\n", tp->pass_count);
a474d7c2
PA
12799}
12800
983af33b 12801static void
f00aae0f
KS
12802tracepoint_create_sals_from_location (const struct event_location *location,
12803 struct linespec_result *canonical,
12804 enum bptype type_wanted)
983af33b 12805{
f00aae0f 12806 create_sals_from_location_default (location, canonical, type_wanted);
983af33b
SDJ
12807}
12808
12809static void
12810tracepoint_create_breakpoints_sal (struct gdbarch *gdbarch,
12811 struct linespec_result *canonical,
e1e01040
PA
12812 gdb::unique_xmalloc_ptr<char> cond_string,
12813 gdb::unique_xmalloc_ptr<char> extra_string,
983af33b
SDJ
12814 enum bptype type_wanted,
12815 enum bpdisp disposition,
12816 int thread,
12817 int task, int ignore_count,
12818 const struct breakpoint_ops *ops,
12819 int from_tty, int enabled,
44f238bb 12820 int internal, unsigned flags)
983af33b 12821{
023fa29b 12822 create_breakpoints_sal_default (gdbarch, canonical,
e1e01040
PA
12823 std::move (cond_string),
12824 std::move (extra_string),
e7e0cddf 12825 type_wanted,
983af33b
SDJ
12826 disposition, thread, task,
12827 ignore_count, ops, from_tty,
44f238bb 12828 enabled, internal, flags);
983af33b
SDJ
12829}
12830
6c5b2ebe 12831static std::vector<symtab_and_line>
f00aae0f
KS
12832tracepoint_decode_location (struct breakpoint *b,
12833 const struct event_location *location,
6c5b2ebe 12834 struct program_space *search_pspace)
983af33b 12835{
6c5b2ebe 12836 return decode_location_default (b, location, search_pspace);
983af33b
SDJ
12837}
12838
2060206e 12839struct breakpoint_ops tracepoint_breakpoint_ops;
348d480f 12840
55aa24fb
SDJ
12841/* The breakpoint_ops structure to be use on tracepoints placed in a
12842 static probe. */
12843
12844static void
f00aae0f
KS
12845tracepoint_probe_create_sals_from_location
12846 (const struct event_location *location,
12847 struct linespec_result *canonical,
12848 enum bptype type_wanted)
55aa24fb
SDJ
12849{
12850 /* We use the same method for breakpoint on probes. */
f00aae0f 12851 bkpt_probe_create_sals_from_location (location, canonical, type_wanted);
55aa24fb
SDJ
12852}
12853
6c5b2ebe 12854static std::vector<symtab_and_line>
f00aae0f
KS
12855tracepoint_probe_decode_location (struct breakpoint *b,
12856 const struct event_location *location,
6c5b2ebe 12857 struct program_space *search_pspace)
55aa24fb
SDJ
12858{
12859 /* We use the same method for breakpoint on probes. */
6c5b2ebe 12860 return bkpt_probe_decode_location (b, location, search_pspace);
55aa24fb
SDJ
12861}
12862
12863static struct breakpoint_ops tracepoint_probe_breakpoint_ops;
12864
5c2b4418
HZ
12865/* Dprintf breakpoint_ops methods. */
12866
12867static void
12868dprintf_re_set (struct breakpoint *b)
12869{
12870 breakpoint_re_set_default (b);
12871
f00aae0f
KS
12872 /* extra_string should never be non-NULL for dprintf. */
12873 gdb_assert (b->extra_string != NULL);
5c2b4418
HZ
12874
12875 /* 1 - connect to target 1, that can run breakpoint commands.
12876 2 - create a dprintf, which resolves fine.
12877 3 - disconnect from target 1
12878 4 - connect to target 2, that can NOT run breakpoint commands.
12879
12880 After steps #3/#4, you'll want the dprintf command list to
12881 be updated, because target 1 and 2 may well return different
12882 answers for target_can_run_breakpoint_commands().
12883 Given absence of finer grained resetting, we get to do
12884 it all the time. */
12885 if (b->extra_string != NULL)
12886 update_dprintf_command_list (b);
12887}
12888
2d9442cc
HZ
12889/* Implement the "print_recreate" breakpoint_ops method for dprintf. */
12890
12891static void
12892dprintf_print_recreate (struct breakpoint *tp, struct ui_file *fp)
12893{
f00aae0f 12894 fprintf_unfiltered (fp, "dprintf %s,%s",
d28cd78a 12895 event_location_to_string (tp->location.get ()),
2d9442cc
HZ
12896 tp->extra_string);
12897 print_recreate_thread (tp, fp);
12898}
12899
9d6e6e84
HZ
12900/* Implement the "after_condition_true" breakpoint_ops method for
12901 dprintf.
12902
12903 dprintf's are implemented with regular commands in their command
12904 list, but we run the commands here instead of before presenting the
12905 stop to the user, as dprintf's don't actually cause a stop. This
12906 also makes it so that the commands of multiple dprintfs at the same
12907 address are all handled. */
12908
12909static void
12910dprintf_after_condition_true (struct bpstats *bs)
12911{
04afa70c 12912 struct bpstats tmp_bs;
9d6e6e84
HZ
12913 struct bpstats *tmp_bs_p = &tmp_bs;
12914
12915 /* dprintf's never cause a stop. This wasn't set in the
12916 check_status hook instead because that would make the dprintf's
12917 condition not be evaluated. */
12918 bs->stop = 0;
12919
12920 /* Run the command list here. Take ownership of it instead of
12921 copying. We never want these commands to run later in
12922 bpstat_do_actions, if a breakpoint that causes a stop happens to
12923 be set at same address as this dprintf, or even if running the
12924 commands here throws. */
12925 tmp_bs.commands = bs->commands;
12926 bs->commands = NULL;
9d6e6e84
HZ
12927
12928 bpstat_do_actions_1 (&tmp_bs_p);
12929
12930 /* 'tmp_bs.commands' will usually be NULL by now, but
12931 bpstat_do_actions_1 may return early without processing the whole
12932 list. */
9d6e6e84
HZ
12933}
12934
983af33b
SDJ
12935/* The breakpoint_ops structure to be used on static tracepoints with
12936 markers (`-m'). */
12937
12938static void
f00aae0f 12939strace_marker_create_sals_from_location (const struct event_location *location,
5f700d83 12940 struct linespec_result *canonical,
f00aae0f 12941 enum bptype type_wanted)
983af33b
SDJ
12942{
12943 struct linespec_sals lsal;
f00aae0f 12944 const char *arg_start, *arg;
983af33b 12945
a20714ff 12946 arg = arg_start = get_linespec_location (location)->spec_string;
f00aae0f 12947 lsal.sals = decode_static_tracepoint_spec (&arg);
983af33b 12948
f2fc3015
TT
12949 std::string str (arg_start, arg - arg_start);
12950 const char *ptr = str.c_str ();
a20714ff
PA
12951 canonical->location
12952 = new_linespec_location (&ptr, symbol_name_match_type::FULL);
983af33b 12953
8e9e35b1
TT
12954 lsal.canonical
12955 = xstrdup (event_location_to_string (canonical->location.get ()));
6c5b2ebe 12956 canonical->lsals.push_back (std::move (lsal));
983af33b
SDJ
12957}
12958
12959static void
12960strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
12961 struct linespec_result *canonical,
e1e01040
PA
12962 gdb::unique_xmalloc_ptr<char> cond_string,
12963 gdb::unique_xmalloc_ptr<char> extra_string,
983af33b
SDJ
12964 enum bptype type_wanted,
12965 enum bpdisp disposition,
12966 int thread,
12967 int task, int ignore_count,
12968 const struct breakpoint_ops *ops,
12969 int from_tty, int enabled,
44f238bb 12970 int internal, unsigned flags)
983af33b 12971{
6c5b2ebe 12972 const linespec_sals &lsal = canonical->lsals[0];
983af33b
SDJ
12973
12974 /* If the user is creating a static tracepoint by marker id
12975 (strace -m MARKER_ID), then store the sals index, so that
12976 breakpoint_re_set can try to match up which of the newly
12977 found markers corresponds to this one, and, don't try to
12978 expand multiple locations for each sal, given than SALS
12979 already should contain all sals for MARKER_ID. */
12980
6c5b2ebe 12981 for (size_t i = 0; i < lsal.sals.size (); i++)
983af33b 12982 {
6c5b2ebe
PA
12983 event_location_up location
12984 = copy_event_location (canonical->location.get ());
983af33b 12985
b270e6f9 12986 std::unique_ptr<tracepoint> tp (new tracepoint ());
6c5b2ebe 12987 init_breakpoint_sal (tp.get (), gdbarch, lsal.sals[i],
ffc2605c 12988 std::move (location), NULL,
e1e01040
PA
12989 std::move (cond_string),
12990 std::move (extra_string),
e7e0cddf 12991 type_wanted, disposition,
983af33b 12992 thread, task, ignore_count, ops,
44f238bb 12993 from_tty, enabled, internal, flags,
983af33b
SDJ
12994 canonical->special_display);
12995 /* Given that its possible to have multiple markers with
12996 the same string id, if the user is creating a static
12997 tracepoint by marker id ("strace -m MARKER_ID"), then
12998 store the sals index, so that breakpoint_re_set can
12999 try to match up which of the newly found markers
13000 corresponds to this one */
13001 tp->static_trace_marker_id_idx = i;
13002
b270e6f9 13003 install_breakpoint (internal, std::move (tp), 0);
983af33b
SDJ
13004 }
13005}
13006
6c5b2ebe 13007static std::vector<symtab_and_line>
f00aae0f
KS
13008strace_marker_decode_location (struct breakpoint *b,
13009 const struct event_location *location,
6c5b2ebe 13010 struct program_space *search_pspace)
983af33b
SDJ
13011{
13012 struct tracepoint *tp = (struct tracepoint *) b;
a20714ff 13013 const char *s = get_linespec_location (location)->spec_string;
983af33b 13014
6c5b2ebe
PA
13015 std::vector<symtab_and_line> sals = decode_static_tracepoint_spec (&s);
13016 if (sals.size () > tp->static_trace_marker_id_idx)
983af33b 13017 {
6c5b2ebe
PA
13018 sals[0] = sals[tp->static_trace_marker_id_idx];
13019 sals.resize (1);
13020 return sals;
983af33b
SDJ
13021 }
13022 else
5d9310c4 13023 error (_("marker %s not found"), tp->static_trace_marker_id.c_str ());
983af33b
SDJ
13024}
13025
13026static struct breakpoint_ops strace_marker_breakpoint_ops;
13027
13028static int
13029strace_marker_p (struct breakpoint *b)
13030{
13031 return b->ops == &strace_marker_breakpoint_ops;
13032}
13033
53a5351d 13034/* Delete a breakpoint and clean up all traces of it in the data
f431efe5 13035 structures. */
c906108c
SS
13036
13037void
fba45db2 13038delete_breakpoint (struct breakpoint *bpt)
c906108c 13039{
52f0bd74 13040 struct breakpoint *b;
c906108c 13041
8a3fe4f8 13042 gdb_assert (bpt != NULL);
c906108c 13043
4a64f543
MS
13044 /* Has this bp already been deleted? This can happen because
13045 multiple lists can hold pointers to bp's. bpstat lists are
13046 especial culprits.
13047
13048 One example of this happening is a watchpoint's scope bp. When
13049 the scope bp triggers, we notice that the watchpoint is out of
13050 scope, and delete it. We also delete its scope bp. But the
13051 scope bp is marked "auto-deleting", and is already on a bpstat.
13052 That bpstat is then checked for auto-deleting bp's, which are
13053 deleted.
13054
13055 A real solution to this problem might involve reference counts in
13056 bp's, and/or giving them pointers back to their referencing
13057 bpstat's, and teaching delete_breakpoint to only free a bp's
13058 storage when no more references were extent. A cheaper bandaid
13059 was chosen. */
c906108c
SS
13060 if (bpt->type == bp_none)
13061 return;
13062
4a64f543
MS
13063 /* At least avoid this stale reference until the reference counting
13064 of breakpoints gets resolved. */
d0fb5eae 13065 if (bpt->related_breakpoint != bpt)
e5a0a904 13066 {
d0fb5eae 13067 struct breakpoint *related;
3a5c3e22 13068 struct watchpoint *w;
d0fb5eae
JK
13069
13070 if (bpt->type == bp_watchpoint_scope)
3a5c3e22 13071 w = (struct watchpoint *) bpt->related_breakpoint;
d0fb5eae 13072 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
3a5c3e22
PA
13073 w = (struct watchpoint *) bpt;
13074 else
13075 w = NULL;
13076 if (w != NULL)
13077 watchpoint_del_at_next_stop (w);
d0fb5eae
JK
13078
13079 /* Unlink bpt from the bpt->related_breakpoint ring. */
13080 for (related = bpt; related->related_breakpoint != bpt;
13081 related = related->related_breakpoint);
13082 related->related_breakpoint = bpt->related_breakpoint;
13083 bpt->related_breakpoint = bpt;
e5a0a904
JK
13084 }
13085
a9634178
TJB
13086 /* watch_command_1 creates a watchpoint but only sets its number if
13087 update_watchpoint succeeds in creating its bp_locations. If there's
13088 a problem in that process, we'll be asked to delete the half-created
13089 watchpoint. In that case, don't announce the deletion. */
13090 if (bpt->number)
76727919 13091 gdb::observers::breakpoint_deleted.notify (bpt);
c906108c 13092
c906108c
SS
13093 if (breakpoint_chain == bpt)
13094 breakpoint_chain = bpt->next;
13095
c906108c
SS
13096 ALL_BREAKPOINTS (b)
13097 if (b->next == bpt)
c5aa993b
JM
13098 {
13099 b->next = bpt->next;
13100 break;
13101 }
c906108c 13102
f431efe5
PA
13103 /* Be sure no bpstat's are pointing at the breakpoint after it's
13104 been freed. */
13105 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
e5dd4106 13106 in all threads for now. Note that we cannot just remove bpstats
f431efe5
PA
13107 pointing at bpt from the stop_bpstat list entirely, as breakpoint
13108 commands are associated with the bpstat; if we remove it here,
13109 then the later call to bpstat_do_actions (&stop_bpstat); in
13110 event-top.c won't do anything, and temporary breakpoints with
13111 commands won't work. */
13112
13113 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
13114
4a64f543
MS
13115 /* Now that breakpoint is removed from breakpoint list, update the
13116 global location list. This will remove locations that used to
13117 belong to this breakpoint. Do this before freeing the breakpoint
13118 itself, since remove_breakpoint looks at location's owner. It
13119 might be better design to have location completely
13120 self-contained, but it's not the case now. */
44702360 13121 update_global_location_list (UGLL_DONT_INSERT);
74960c60 13122
4a64f543
MS
13123 /* On the chance that someone will soon try again to delete this
13124 same bp, we mark it as deleted before freeing its storage. */
c906108c 13125 bpt->type = bp_none;
4d01a485 13126 delete bpt;
c906108c
SS
13127}
13128
51be5b68
PA
13129/* Iterator function to call a user-provided callback function once
13130 for each of B and its related breakpoints. */
13131
13132static void
13133iterate_over_related_breakpoints (struct breakpoint *b,
48649e1b 13134 gdb::function_view<void (breakpoint *)> function)
51be5b68
PA
13135{
13136 struct breakpoint *related;
13137
13138 related = b;
13139 do
13140 {
13141 struct breakpoint *next;
13142
13143 /* FUNCTION may delete RELATED. */
13144 next = related->related_breakpoint;
13145
13146 if (next == related)
13147 {
13148 /* RELATED is the last ring entry. */
48649e1b 13149 function (related);
51be5b68
PA
13150
13151 /* FUNCTION may have deleted it, so we'd never reach back to
13152 B. There's nothing left to do anyway, so just break
13153 out. */
13154 break;
13155 }
13156 else
48649e1b 13157 function (related);
51be5b68
PA
13158
13159 related = next;
13160 }
13161 while (related != b);
13162}
95a42b64 13163
4495129a 13164static void
981a3fb3 13165delete_command (const char *arg, int from_tty)
c906108c 13166{
35df4500 13167 struct breakpoint *b, *b_tmp;
c906108c 13168
ea9365bb
TT
13169 dont_repeat ();
13170
c906108c
SS
13171 if (arg == 0)
13172 {
13173 int breaks_to_delete = 0;
13174
46c6471b
PA
13175 /* Delete all breakpoints if no argument. Do not delete
13176 internal breakpoints, these have to be deleted with an
13177 explicit breakpoint number argument. */
c5aa993b 13178 ALL_BREAKPOINTS (b)
46c6471b 13179 if (user_breakpoint_p (b))
973d738b
DJ
13180 {
13181 breaks_to_delete = 1;
13182 break;
13183 }
c906108c
SS
13184
13185 /* Ask user only if there are some breakpoints to delete. */
13186 if (!from_tty
e2e0b3e5 13187 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
c906108c 13188 {
35df4500 13189 ALL_BREAKPOINTS_SAFE (b, b_tmp)
46c6471b 13190 if (user_breakpoint_p (b))
c5aa993b 13191 delete_breakpoint (b);
c906108c
SS
13192 }
13193 }
13194 else
48649e1b 13195 map_breakpoint_numbers
b926417a 13196 (arg, [&] (breakpoint *br)
48649e1b 13197 {
b926417a 13198 iterate_over_related_breakpoints (br, delete_breakpoint);
48649e1b 13199 });
c906108c
SS
13200}
13201
c2f4122d
PA
13202/* Return true if all locations of B bound to PSPACE are pending. If
13203 PSPACE is NULL, all locations of all program spaces are
13204 considered. */
13205
0d381245 13206static int
c2f4122d 13207all_locations_are_pending (struct breakpoint *b, struct program_space *pspace)
fe3f5fa8 13208{
c2f4122d
PA
13209 struct bp_location *loc;
13210
13211 for (loc = b->loc; loc != NULL; loc = loc->next)
13212 if ((pspace == NULL
13213 || loc->pspace == pspace)
13214 && !loc->shlib_disabled
8645ff69 13215 && !loc->pspace->executing_startup)
0d381245
VP
13216 return 0;
13217 return 1;
fe3f5fa8
VP
13218}
13219
776592bf
DE
13220/* Subroutine of update_breakpoint_locations to simplify it.
13221 Return non-zero if multiple fns in list LOC have the same name.
13222 Null names are ignored. */
13223
13224static int
13225ambiguous_names_p (struct bp_location *loc)
13226{
13227 struct bp_location *l;
459a2e4c
TT
13228 htab_t htab = htab_create_alloc (13, htab_hash_string, streq_hash, NULL,
13229 xcalloc, xfree);
776592bf
DE
13230
13231 for (l = loc; l != NULL; l = l->next)
13232 {
13233 const char **slot;
13234 const char *name = l->function_name;
13235
13236 /* Allow for some names to be NULL, ignore them. */
13237 if (name == NULL)
13238 continue;
13239
13240 slot = (const char **) htab_find_slot (htab, (const void *) name,
13241 INSERT);
4a64f543
MS
13242 /* NOTE: We can assume slot != NULL here because xcalloc never
13243 returns NULL. */
776592bf
DE
13244 if (*slot != NULL)
13245 {
13246 htab_delete (htab);
13247 return 1;
13248 }
13249 *slot = name;
13250 }
13251
13252 htab_delete (htab);
13253 return 0;
13254}
13255
0fb4aa4b
PA
13256/* When symbols change, it probably means the sources changed as well,
13257 and it might mean the static tracepoint markers are no longer at
13258 the same address or line numbers they used to be at last we
13259 checked. Losing your static tracepoints whenever you rebuild is
13260 undesirable. This function tries to resync/rematch gdb static
13261 tracepoints with the markers on the target, for static tracepoints
13262 that have not been set by marker id. Static tracepoint that have
13263 been set by marker id are reset by marker id in breakpoint_re_set.
13264 The heuristic is:
13265
13266 1) For a tracepoint set at a specific address, look for a marker at
13267 the old PC. If one is found there, assume to be the same marker.
13268 If the name / string id of the marker found is different from the
13269 previous known name, assume that means the user renamed the marker
13270 in the sources, and output a warning.
13271
13272 2) For a tracepoint set at a given line number, look for a marker
13273 at the new address of the old line number. If one is found there,
13274 assume to be the same marker. If the name / string id of the
13275 marker found is different from the previous known name, assume that
13276 means the user renamed the marker in the sources, and output a
13277 warning.
13278
13279 3) If a marker is no longer found at the same address or line, it
13280 may mean the marker no longer exists. But it may also just mean
13281 the code changed a bit. Maybe the user added a few lines of code
13282 that made the marker move up or down (in line number terms). Ask
13283 the target for info about the marker with the string id as we knew
13284 it. If found, update line number and address in the matching
13285 static tracepoint. This will get confused if there's more than one
13286 marker with the same ID (possible in UST, although unadvised
13287 precisely because it confuses tools). */
13288
13289static struct symtab_and_line
13290update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
13291{
d9b3f62e 13292 struct tracepoint *tp = (struct tracepoint *) b;
0fb4aa4b
PA
13293 struct static_tracepoint_marker marker;
13294 CORE_ADDR pc;
0fb4aa4b
PA
13295
13296 pc = sal.pc;
13297 if (sal.line)
13298 find_line_pc (sal.symtab, sal.line, &pc);
13299
13300 if (target_static_tracepoint_marker_at (pc, &marker))
13301 {
5d9310c4 13302 if (tp->static_trace_marker_id != marker.str_id)
0fb4aa4b 13303 warning (_("static tracepoint %d changed probed marker from %s to %s"),
5d9310c4
SM
13304 b->number, tp->static_trace_marker_id.c_str (),
13305 marker.str_id.c_str ());
0fb4aa4b 13306
5d9310c4 13307 tp->static_trace_marker_id = std::move (marker.str_id);
0fb4aa4b
PA
13308
13309 return sal;
13310 }
13311
13312 /* Old marker wasn't found on target at lineno. Try looking it up
13313 by string ID. */
13314 if (!sal.explicit_pc
13315 && sal.line != 0
13316 && sal.symtab != NULL
5d9310c4 13317 && !tp->static_trace_marker_id.empty ())
0fb4aa4b 13318 {
5d9310c4
SM
13319 std::vector<static_tracepoint_marker> markers
13320 = target_static_tracepoint_markers_by_strid
13321 (tp->static_trace_marker_id.c_str ());
0fb4aa4b 13322
5d9310c4 13323 if (!markers.empty ())
0fb4aa4b 13324 {
0fb4aa4b 13325 struct symbol *sym;
80e1d417 13326 struct static_tracepoint_marker *tpmarker;
79a45e25 13327 struct ui_out *uiout = current_uiout;
67994074 13328 struct explicit_location explicit_loc;
0fb4aa4b 13329
5d9310c4 13330 tpmarker = &markers[0];
0fb4aa4b 13331
5d9310c4 13332 tp->static_trace_marker_id = std::move (tpmarker->str_id);
0fb4aa4b
PA
13333
13334 warning (_("marker for static tracepoint %d (%s) not "
13335 "found at previous line number"),
5d9310c4 13336 b->number, tp->static_trace_marker_id.c_str ());
0fb4aa4b 13337
51abb421 13338 symtab_and_line sal2 = find_pc_line (tpmarker->address, 0);
80e1d417 13339 sym = find_pc_sect_function (tpmarker->address, NULL);
112e8700 13340 uiout->text ("Now in ");
0fb4aa4b
PA
13341 if (sym)
13342 {
cbe56571
TT
13343 uiout->field_string ("func", SYMBOL_PRINT_NAME (sym),
13344 ui_out_style_kind::FUNCTION);
112e8700 13345 uiout->text (" at ");
0fb4aa4b 13346 }
112e8700 13347 uiout->field_string ("file",
cbe56571
TT
13348 symtab_to_filename_for_display (sal2.symtab),
13349 ui_out_style_kind::FILE);
112e8700 13350 uiout->text (":");
0fb4aa4b 13351
112e8700 13352 if (uiout->is_mi_like_p ())
0fb4aa4b 13353 {
0b0865da 13354 const char *fullname = symtab_to_fullname (sal2.symtab);
0fb4aa4b 13355
112e8700 13356 uiout->field_string ("fullname", fullname);
0fb4aa4b
PA
13357 }
13358
112e8700
SM
13359 uiout->field_int ("line", sal2.line);
13360 uiout->text ("\n");
0fb4aa4b 13361
80e1d417 13362 b->loc->line_number = sal2.line;
2f202fde 13363 b->loc->symtab = sym != NULL ? sal2.symtab : NULL;
0fb4aa4b 13364
d28cd78a 13365 b->location.reset (NULL);
67994074
KS
13366 initialize_explicit_location (&explicit_loc);
13367 explicit_loc.source_filename
00e52e53 13368 = ASTRDUP (symtab_to_filename_for_display (sal2.symtab));
67994074
KS
13369 explicit_loc.line_offset.offset = b->loc->line_number;
13370 explicit_loc.line_offset.sign = LINE_OFFSET_NONE;
d28cd78a 13371 b->location = new_explicit_location (&explicit_loc);
0fb4aa4b
PA
13372
13373 /* Might be nice to check if function changed, and warn if
13374 so. */
0fb4aa4b
PA
13375 }
13376 }
13377 return sal;
13378}
13379
8d3788bd
VP
13380/* Returns 1 iff locations A and B are sufficiently same that
13381 we don't need to report breakpoint as changed. */
13382
13383static int
13384locations_are_equal (struct bp_location *a, struct bp_location *b)
13385{
13386 while (a && b)
13387 {
13388 if (a->address != b->address)
13389 return 0;
13390
13391 if (a->shlib_disabled != b->shlib_disabled)
13392 return 0;
13393
13394 if (a->enabled != b->enabled)
13395 return 0;
13396
13397 a = a->next;
13398 b = b->next;
13399 }
13400
13401 if ((a == NULL) != (b == NULL))
13402 return 0;
13403
13404 return 1;
13405}
13406
c2f4122d
PA
13407/* Split all locations of B that are bound to PSPACE out of B's
13408 location list to a separate list and return that list's head. If
13409 PSPACE is NULL, hoist out all locations of B. */
13410
13411static struct bp_location *
13412hoist_existing_locations (struct breakpoint *b, struct program_space *pspace)
13413{
13414 struct bp_location head;
13415 struct bp_location *i = b->loc;
13416 struct bp_location **i_link = &b->loc;
13417 struct bp_location *hoisted = &head;
13418
13419 if (pspace == NULL)
13420 {
13421 i = b->loc;
13422 b->loc = NULL;
13423 return i;
13424 }
13425
13426 head.next = NULL;
13427
13428 while (i != NULL)
13429 {
13430 if (i->pspace == pspace)
13431 {
13432 *i_link = i->next;
13433 i->next = NULL;
13434 hoisted->next = i;
13435 hoisted = i;
13436 }
13437 else
13438 i_link = &i->next;
13439 i = *i_link;
13440 }
13441
13442 return head.next;
13443}
13444
13445/* Create new breakpoint locations for B (a hardware or software
13446 breakpoint) based on SALS and SALS_END. If SALS_END.NELTS is not
13447 zero, then B is a ranged breakpoint. Only recreates locations for
13448 FILTER_PSPACE. Locations of other program spaces are left
13449 untouched. */
f1310107 13450
0e30163f 13451void
0d381245 13452update_breakpoint_locations (struct breakpoint *b,
c2f4122d 13453 struct program_space *filter_pspace,
6c5b2ebe
PA
13454 gdb::array_view<const symtab_and_line> sals,
13455 gdb::array_view<const symtab_and_line> sals_end)
fe3f5fa8 13456{
c2f4122d 13457 struct bp_location *existing_locations;
0d381245 13458
6c5b2ebe 13459 if (!sals_end.empty () && (sals.size () != 1 || sals_end.size () != 1))
f8eba3c6
TT
13460 {
13461 /* Ranged breakpoints have only one start location and one end
13462 location. */
13463 b->enable_state = bp_disabled;
f8eba3c6
TT
13464 printf_unfiltered (_("Could not reset ranged breakpoint %d: "
13465 "multiple locations found\n"),
13466 b->number);
13467 return;
13468 }
f1310107 13469
4a64f543
MS
13470 /* If there's no new locations, and all existing locations are
13471 pending, don't do anything. This optimizes the common case where
13472 all locations are in the same shared library, that was unloaded.
13473 We'd like to retain the location, so that when the library is
13474 loaded again, we don't loose the enabled/disabled status of the
13475 individual locations. */
6c5b2ebe 13476 if (all_locations_are_pending (b, filter_pspace) && sals.empty ())
fe3f5fa8
VP
13477 return;
13478
c2f4122d 13479 existing_locations = hoist_existing_locations (b, filter_pspace);
fe3f5fa8 13480
6c5b2ebe 13481 for (const auto &sal : sals)
fe3f5fa8 13482 {
f8eba3c6
TT
13483 struct bp_location *new_loc;
13484
6c5b2ebe 13485 switch_to_program_space_and_thread (sal.pspace);
f8eba3c6 13486
6c5b2ebe 13487 new_loc = add_location_to_breakpoint (b, &sal);
fe3f5fa8 13488
0d381245
VP
13489 /* Reparse conditions, they might contain references to the
13490 old symtab. */
13491 if (b->cond_string != NULL)
13492 {
bbc13ae3 13493 const char *s;
fe3f5fa8 13494
0d381245 13495 s = b->cond_string;
a70b8144 13496 try
0d381245 13497 {
6c5b2ebe
PA
13498 new_loc->cond = parse_exp_1 (&s, sal.pc,
13499 block_for_pc (sal.pc),
0d381245
VP
13500 0);
13501 }
230d2906 13502 catch (const gdb_exception_error &e)
0d381245 13503 {
3e43a32a
MS
13504 warning (_("failed to reevaluate condition "
13505 "for breakpoint %d: %s"),
3d6e9d23 13506 b->number, e.what ());
0d381245
VP
13507 new_loc->enabled = 0;
13508 }
13509 }
fe3f5fa8 13510
6c5b2ebe 13511 if (!sals_end.empty ())
f1310107 13512 {
6c5b2ebe 13513 CORE_ADDR end = find_breakpoint_range_end (sals_end[0]);
f1310107 13514
6c5b2ebe 13515 new_loc->length = end - sals[0].pc + 1;
f1310107 13516 }
0d381245 13517 }
fe3f5fa8 13518
4a64f543
MS
13519 /* If possible, carry over 'disable' status from existing
13520 breakpoints. */
0d381245
VP
13521 {
13522 struct bp_location *e = existing_locations;
776592bf
DE
13523 /* If there are multiple breakpoints with the same function name,
13524 e.g. for inline functions, comparing function names won't work.
13525 Instead compare pc addresses; this is just a heuristic as things
13526 may have moved, but in practice it gives the correct answer
13527 often enough until a better solution is found. */
13528 int have_ambiguous_names = ambiguous_names_p (b->loc);
13529
0d381245
VP
13530 for (; e; e = e->next)
13531 {
13532 if (!e->enabled && e->function_name)
13533 {
13534 struct bp_location *l = b->loc;
776592bf
DE
13535 if (have_ambiguous_names)
13536 {
13537 for (; l; l = l->next)
f1310107 13538 if (breakpoint_locations_match (e, l))
776592bf
DE
13539 {
13540 l->enabled = 0;
13541 break;
13542 }
13543 }
13544 else
13545 {
13546 for (; l; l = l->next)
13547 if (l->function_name
13548 && strcmp (e->function_name, l->function_name) == 0)
13549 {
13550 l->enabled = 0;
13551 break;
13552 }
13553 }
0d381245
VP
13554 }
13555 }
13556 }
fe3f5fa8 13557
8d3788bd 13558 if (!locations_are_equal (existing_locations, b->loc))
76727919 13559 gdb::observers::breakpoint_modified.notify (b);
fe3f5fa8
VP
13560}
13561
f00aae0f 13562/* Find the SaL locations corresponding to the given LOCATION.
ef23e705
TJB
13563 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
13564
6c5b2ebe 13565static std::vector<symtab_and_line>
f00aae0f 13566location_to_sals (struct breakpoint *b, struct event_location *location,
c2f4122d 13567 struct program_space *search_pspace, int *found)
ef23e705 13568{
cc06b668 13569 struct gdb_exception exception;
ef23e705 13570
983af33b 13571 gdb_assert (b->ops != NULL);
ef23e705 13572
6c5b2ebe
PA
13573 std::vector<symtab_and_line> sals;
13574
a70b8144 13575 try
ef23e705 13576 {
6c5b2ebe 13577 sals = b->ops->decode_location (b, location, search_pspace);
ef23e705 13578 }
94aeb44b 13579 catch (gdb_exception_error &e)
ef23e705
TJB
13580 {
13581 int not_found_and_ok = 0;
492d29ea 13582
ef23e705
TJB
13583 /* For pending breakpoints, it's expected that parsing will
13584 fail until the right shared library is loaded. User has
13585 already told to create pending breakpoints and don't need
13586 extra messages. If breakpoint is in bp_shlib_disabled
13587 state, then user already saw the message about that
13588 breakpoint being disabled, and don't want to see more
13589 errors. */
58438ac1 13590 if (e.error == NOT_FOUND_ERROR
c2f4122d
PA
13591 && (b->condition_not_parsed
13592 || (b->loc != NULL
13593 && search_pspace != NULL
13594 && b->loc->pspace != search_pspace)
ef23e705 13595 || (b->loc && b->loc->shlib_disabled)
f8eba3c6 13596 || (b->loc && b->loc->pspace->executing_startup)
ef23e705
TJB
13597 || b->enable_state == bp_disabled))
13598 not_found_and_ok = 1;
13599
13600 if (!not_found_and_ok)
13601 {
13602 /* We surely don't want to warn about the same breakpoint
13603 10 times. One solution, implemented here, is disable
13604 the breakpoint on error. Another solution would be to
13605 have separate 'warning emitted' flag. Since this
13606 happens only when a binary has changed, I don't know
13607 which approach is better. */
13608 b->enable_state = bp_disabled;
eedc3f4f 13609 throw;
ef23e705 13610 }
94aeb44b
TT
13611
13612 exception = std::move (e);
ef23e705
TJB
13613 }
13614
492d29ea 13615 if (exception.reason == 0 || exception.error != NOT_FOUND_ERROR)
ef23e705 13616 {
6c5b2ebe
PA
13617 for (auto &sal : sals)
13618 resolve_sal_pc (&sal);
f00aae0f 13619 if (b->condition_not_parsed && b->extra_string != NULL)
ef23e705 13620 {
ed1d1739
KS
13621 char *cond_string, *extra_string;
13622 int thread, task;
ef23e705 13623
6c5b2ebe 13624 find_condition_and_thread (b->extra_string, sals[0].pc,
e7e0cddf
SS
13625 &cond_string, &thread, &task,
13626 &extra_string);
f00aae0f 13627 gdb_assert (b->cond_string == NULL);
ef23e705
TJB
13628 if (cond_string)
13629 b->cond_string = cond_string;
13630 b->thread = thread;
13631 b->task = task;
e7e0cddf 13632 if (extra_string)
f00aae0f
KS
13633 {
13634 xfree (b->extra_string);
13635 b->extra_string = extra_string;
13636 }
ef23e705
TJB
13637 b->condition_not_parsed = 0;
13638 }
13639
983af33b 13640 if (b->type == bp_static_tracepoint && !strace_marker_p (b))
6c5b2ebe 13641 sals[0] = update_static_tracepoint (b, sals[0]);
ef23e705 13642
58438ac1
TT
13643 *found = 1;
13644 }
13645 else
13646 *found = 0;
ef23e705
TJB
13647
13648 return sals;
13649}
13650
348d480f
PA
13651/* The default re_set method, for typical hardware or software
13652 breakpoints. Reevaluate the breakpoint and recreate its
13653 locations. */
13654
13655static void
28010a5d 13656breakpoint_re_set_default (struct breakpoint *b)
ef23e705 13657{
c2f4122d 13658 struct program_space *filter_pspace = current_program_space;
6c5b2ebe 13659 std::vector<symtab_and_line> expanded, expanded_end;
ef23e705 13660
6c5b2ebe
PA
13661 int found;
13662 std::vector<symtab_and_line> sals = location_to_sals (b, b->location.get (),
13663 filter_pspace, &found);
ef23e705 13664 if (found)
6c5b2ebe 13665 expanded = std::move (sals);
ef23e705 13666
f00aae0f 13667 if (b->location_range_end != NULL)
f1310107 13668 {
6c5b2ebe
PA
13669 std::vector<symtab_and_line> sals_end
13670 = location_to_sals (b, b->location_range_end.get (),
13671 filter_pspace, &found);
f1310107 13672 if (found)
6c5b2ebe 13673 expanded_end = std::move (sals_end);
f1310107
TJB
13674 }
13675
c2f4122d 13676 update_breakpoint_locations (b, filter_pspace, expanded, expanded_end);
28010a5d
PA
13677}
13678
983af33b
SDJ
13679/* Default method for creating SALs from an address string. It basically
13680 calls parse_breakpoint_sals. Return 1 for success, zero for failure. */
13681
13682static void
f00aae0f
KS
13683create_sals_from_location_default (const struct event_location *location,
13684 struct linespec_result *canonical,
13685 enum bptype type_wanted)
983af33b 13686{
f00aae0f 13687 parse_breakpoint_sals (location, canonical);
983af33b
SDJ
13688}
13689
13690/* Call create_breakpoints_sal for the given arguments. This is the default
13691 function for the `create_breakpoints_sal' method of
13692 breakpoint_ops. */
13693
13694static void
13695create_breakpoints_sal_default (struct gdbarch *gdbarch,
13696 struct linespec_result *canonical,
e1e01040
PA
13697 gdb::unique_xmalloc_ptr<char> cond_string,
13698 gdb::unique_xmalloc_ptr<char> extra_string,
983af33b
SDJ
13699 enum bptype type_wanted,
13700 enum bpdisp disposition,
13701 int thread,
13702 int task, int ignore_count,
13703 const struct breakpoint_ops *ops,
13704 int from_tty, int enabled,
44f238bb 13705 int internal, unsigned flags)
983af33b 13706{
e1e01040
PA
13707 create_breakpoints_sal (gdbarch, canonical,
13708 std::move (cond_string),
13709 std::move (extra_string),
983af33b
SDJ
13710 type_wanted, disposition,
13711 thread, task, ignore_count, ops, from_tty,
44f238bb 13712 enabled, internal, flags);
983af33b
SDJ
13713}
13714
13715/* Decode the line represented by S by calling decode_line_full. This is the
5f700d83 13716 default function for the `decode_location' method of breakpoint_ops. */
983af33b 13717
6c5b2ebe 13718static std::vector<symtab_and_line>
f00aae0f
KS
13719decode_location_default (struct breakpoint *b,
13720 const struct event_location *location,
6c5b2ebe 13721 struct program_space *search_pspace)
983af33b
SDJ
13722{
13723 struct linespec_result canonical;
13724
c2f4122d 13725 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, search_pspace,
cafb3438 13726 NULL, 0, &canonical, multiple_symbols_all,
983af33b
SDJ
13727 b->filter);
13728
13729 /* We should get 0 or 1 resulting SALs. */
6c5b2ebe 13730 gdb_assert (canonical.lsals.size () < 2);
983af33b 13731
6c5b2ebe 13732 if (!canonical.lsals.empty ())
983af33b 13733 {
6c5b2ebe
PA
13734 const linespec_sals &lsal = canonical.lsals[0];
13735 return std::move (lsal.sals);
983af33b 13736 }
6c5b2ebe 13737 return {};
983af33b
SDJ
13738}
13739
bf469271 13740/* Reset a breakpoint. */
c906108c 13741
bf469271
PA
13742static void
13743breakpoint_re_set_one (breakpoint *b)
c906108c 13744{
fdf44873
TT
13745 input_radix = b->input_radix;
13746 set_language (b->language);
c906108c 13747
348d480f 13748 b->ops->re_set (b);
c906108c
SS
13749}
13750
c2f4122d
PA
13751/* Re-set breakpoint locations for the current program space.
13752 Locations bound to other program spaces are left untouched. */
13753
c906108c 13754void
69de3c6a 13755breakpoint_re_set (void)
c906108c 13756{
35df4500 13757 struct breakpoint *b, *b_tmp;
2a7f3dff 13758
c5aa993b 13759 {
fdf44873
TT
13760 scoped_restore_current_language save_language;
13761 scoped_restore save_input_radix = make_scoped_restore (&input_radix);
5ed8105e 13762 scoped_restore_current_pspace_and_thread restore_pspace_thread;
e62c965a 13763
8e817061
JB
13764 /* breakpoint_re_set_one sets the current_language to the language
13765 of the breakpoint it is resetting (see prepare_re_set_context)
13766 before re-evaluating the breakpoint's location. This change can
13767 unfortunately get undone by accident if the language_mode is set
13768 to auto, and we either switch frames, or more likely in this context,
13769 we select the current frame.
13770
13771 We prevent this by temporarily turning the language_mode to
13772 language_mode_manual. We restore it once all breakpoints
13773 have been reset. */
13774 scoped_restore save_language_mode = make_scoped_restore (&language_mode);
13775 language_mode = language_mode_manual;
13776
5ed8105e
PA
13777 /* Note: we must not try to insert locations until after all
13778 breakpoints have been re-set. Otherwise, e.g., when re-setting
13779 breakpoint 1, we'd insert the locations of breakpoint 2, which
13780 hadn't been re-set yet, and thus may have stale locations. */
4efc6507 13781
5ed8105e
PA
13782 ALL_BREAKPOINTS_SAFE (b, b_tmp)
13783 {
a70b8144 13784 try
bf469271
PA
13785 {
13786 breakpoint_re_set_one (b);
13787 }
230d2906 13788 catch (const gdb_exception &ex)
bf469271
PA
13789 {
13790 exception_fprintf (gdb_stderr, ex,
13791 "Error in re-setting breakpoint %d: ",
13792 b->number);
13793 }
5ed8105e 13794 }
5ed8105e
PA
13795
13796 jit_breakpoint_re_set ();
13797 }
6c95b8df 13798
af02033e
PP
13799 create_overlay_event_breakpoint ();
13800 create_longjmp_master_breakpoint ();
13801 create_std_terminate_master_breakpoint ();
186c406b 13802 create_exception_master_breakpoint ();
2a7f3dff
PA
13803
13804 /* Now we can insert. */
13805 update_global_location_list (UGLL_MAY_INSERT);
c906108c
SS
13806}
13807\f
c906108c
SS
13808/* Reset the thread number of this breakpoint:
13809
13810 - If the breakpoint is for all threads, leave it as-is.
4a64f543 13811 - Else, reset it to the current thread for inferior_ptid. */
c906108c 13812void
fba45db2 13813breakpoint_re_set_thread (struct breakpoint *b)
c906108c
SS
13814{
13815 if (b->thread != -1)
13816 {
00431a78 13817 b->thread = inferior_thread ()->global_num;
6c95b8df
PA
13818
13819 /* We're being called after following a fork. The new fork is
13820 selected as current, and unless this was a vfork will have a
13821 different program space from the original thread. Reset that
13822 as well. */
13823 b->loc->pspace = current_program_space;
c906108c
SS
13824 }
13825}
13826
03ac34d5
MS
13827/* Set ignore-count of breakpoint number BPTNUM to COUNT.
13828 If from_tty is nonzero, it prints a message to that effect,
13829 which ends with a period (no newline). */
13830
c906108c 13831void
fba45db2 13832set_ignore_count (int bptnum, int count, int from_tty)
c906108c 13833{
52f0bd74 13834 struct breakpoint *b;
c906108c
SS
13835
13836 if (count < 0)
13837 count = 0;
13838
13839 ALL_BREAKPOINTS (b)
13840 if (b->number == bptnum)
c5aa993b 13841 {
d77f58be
SS
13842 if (is_tracepoint (b))
13843 {
13844 if (from_tty && count != 0)
13845 printf_filtered (_("Ignore count ignored for tracepoint %d."),
13846 bptnum);
13847 return;
13848 }
13849
c5aa993b 13850 b->ignore_count = count;
221ea385
KS
13851 if (from_tty)
13852 {
13853 if (count == 0)
3e43a32a
MS
13854 printf_filtered (_("Will stop next time "
13855 "breakpoint %d is reached."),
221ea385
KS
13856 bptnum);
13857 else if (count == 1)
a3f17187 13858 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
221ea385
KS
13859 bptnum);
13860 else
3e43a32a
MS
13861 printf_filtered (_("Will ignore next %d "
13862 "crossings of breakpoint %d."),
221ea385
KS
13863 count, bptnum);
13864 }
76727919 13865 gdb::observers::breakpoint_modified.notify (b);
c5aa993b
JM
13866 return;
13867 }
c906108c 13868
8a3fe4f8 13869 error (_("No breakpoint number %d."), bptnum);
c906108c
SS
13870}
13871
c906108c
SS
13872/* Command to set ignore-count of breakpoint N to COUNT. */
13873
13874static void
0b39b52e 13875ignore_command (const char *args, int from_tty)
c906108c 13876{
0b39b52e 13877 const char *p = args;
52f0bd74 13878 int num;
c906108c
SS
13879
13880 if (p == 0)
e2e0b3e5 13881 error_no_arg (_("a breakpoint number"));
c5aa993b 13882
c906108c 13883 num = get_number (&p);
5c44784c 13884 if (num == 0)
8a3fe4f8 13885 error (_("bad breakpoint number: '%s'"), args);
c906108c 13886 if (*p == 0)
8a3fe4f8 13887 error (_("Second argument (specified ignore-count) is missing."));
c906108c
SS
13888
13889 set_ignore_count (num,
13890 longest_to_int (value_as_long (parse_and_eval (p))),
13891 from_tty);
221ea385
KS
13892 if (from_tty)
13893 printf_filtered ("\n");
c906108c
SS
13894}
13895\f
d0fe4701
XR
13896
13897/* Call FUNCTION on each of the breakpoints with numbers in the range
13898 defined by BP_NUM_RANGE (an inclusive range). */
c906108c
SS
13899
13900static void
d0fe4701
XR
13901map_breakpoint_number_range (std::pair<int, int> bp_num_range,
13902 gdb::function_view<void (breakpoint *)> function)
c906108c 13903{
d0fe4701
XR
13904 if (bp_num_range.first == 0)
13905 {
13906 warning (_("bad breakpoint number at or near '%d'"),
13907 bp_num_range.first);
13908 }
13909 else
c906108c 13910 {
d0fe4701 13911 struct breakpoint *b, *tmp;
197f0a60 13912
d0fe4701 13913 for (int i = bp_num_range.first; i <= bp_num_range.second; i++)
5c44784c 13914 {
d0fe4701
XR
13915 bool match = false;
13916
5c44784c 13917 ALL_BREAKPOINTS_SAFE (b, tmp)
d0fe4701 13918 if (b->number == i)
5c44784c 13919 {
bfd28288 13920 match = true;
48649e1b 13921 function (b);
11cf8741 13922 break;
5c44784c 13923 }
bfd28288 13924 if (!match)
d0fe4701 13925 printf_unfiltered (_("No breakpoint number %d.\n"), i);
c5aa993b 13926 }
c906108c
SS
13927 }
13928}
13929
d0fe4701
XR
13930/* Call FUNCTION on each of the breakpoints whose numbers are given in
13931 ARGS. */
13932
13933static void
13934map_breakpoint_numbers (const char *args,
13935 gdb::function_view<void (breakpoint *)> function)
13936{
13937 if (args == NULL || *args == '\0')
13938 error_no_arg (_("one or more breakpoint numbers"));
13939
13940 number_or_range_parser parser (args);
13941
13942 while (!parser.finished ())
13943 {
13944 int num = parser.get_number ();
13945 map_breakpoint_number_range (std::make_pair (num, num), function);
13946 }
13947}
13948
13949/* Return the breakpoint location structure corresponding to the
13950 BP_NUM and LOC_NUM values. */
13951
0d381245 13952static struct bp_location *
d0fe4701 13953find_location_by_number (int bp_num, int loc_num)
0d381245 13954{
0d381245 13955 struct breakpoint *b;
0d381245
VP
13956
13957 ALL_BREAKPOINTS (b)
13958 if (b->number == bp_num)
13959 {
13960 break;
13961 }
13962
13963 if (!b || b->number != bp_num)
d0fe4701 13964 error (_("Bad breakpoint number '%d'"), bp_num);
0d381245 13965
0d381245 13966 if (loc_num == 0)
d0fe4701 13967 error (_("Bad breakpoint location number '%d'"), loc_num);
0d381245 13968
d0fe4701
XR
13969 int n = 0;
13970 for (bp_location *loc = b->loc; loc != NULL; loc = loc->next)
13971 if (++n == loc_num)
13972 return loc;
13973
13974 error (_("Bad breakpoint location number '%d'"), loc_num);
0d381245
VP
13975}
13976
95e95a6d
PA
13977/* Modes of operation for extract_bp_num. */
13978enum class extract_bp_kind
13979{
13980 /* Extracting a breakpoint number. */
13981 bp,
13982
13983 /* Extracting a location number. */
13984 loc,
13985};
13986
13987/* Extract a breakpoint or location number (as determined by KIND)
13988 from the string starting at START. TRAILER is a character which
13989 can be found after the number. If you don't want a trailer, use
13990 '\0'. If END_OUT is not NULL, it is set to point after the parsed
13991 string. This always returns a positive integer. */
13992
13993static int
13994extract_bp_num (extract_bp_kind kind, const char *start,
13995 int trailer, const char **end_out = NULL)
13996{
13997 const char *end = start;
13998 int num = get_number_trailer (&end, trailer);
13999 if (num < 0)
14000 error (kind == extract_bp_kind::bp
14001 ? _("Negative breakpoint number '%.*s'")
14002 : _("Negative breakpoint location number '%.*s'"),
14003 int (end - start), start);
14004 if (num == 0)
14005 error (kind == extract_bp_kind::bp
14006 ? _("Bad breakpoint number '%.*s'")
14007 : _("Bad breakpoint location number '%.*s'"),
14008 int (end - start), start);
14009
14010 if (end_out != NULL)
14011 *end_out = end;
14012 return num;
14013}
14014
14015/* Extract a breakpoint or location range (as determined by KIND) in
14016 the form NUM1-NUM2 stored at &ARG[arg_offset]. Returns a std::pair
14017 representing the (inclusive) range. The returned pair's elements
14018 are always positive integers. */
14019
14020static std::pair<int, int>
14021extract_bp_or_bp_range (extract_bp_kind kind,
14022 const std::string &arg,
14023 std::string::size_type arg_offset)
14024{
14025 std::pair<int, int> range;
14026 const char *bp_loc = &arg[arg_offset];
14027 std::string::size_type dash = arg.find ('-', arg_offset);
14028 if (dash != std::string::npos)
14029 {
14030 /* bp_loc is a range (x-z). */
14031 if (arg.length () == dash + 1)
14032 error (kind == extract_bp_kind::bp
14033 ? _("Bad breakpoint number at or near: '%s'")
14034 : _("Bad breakpoint location number at or near: '%s'"),
14035 bp_loc);
14036
14037 const char *end;
14038 const char *start_first = bp_loc;
14039 const char *start_second = &arg[dash + 1];
14040 range.first = extract_bp_num (kind, start_first, '-');
14041 range.second = extract_bp_num (kind, start_second, '\0', &end);
14042
14043 if (range.first > range.second)
14044 error (kind == extract_bp_kind::bp
14045 ? _("Inverted breakpoint range at '%.*s'")
14046 : _("Inverted breakpoint location range at '%.*s'"),
14047 int (end - start_first), start_first);
14048 }
14049 else
14050 {
14051 /* bp_loc is a single value. */
14052 range.first = extract_bp_num (kind, bp_loc, '\0');
14053 range.second = range.first;
14054 }
14055 return range;
14056}
14057
d0fe4701
XR
14058/* Extract the breakpoint/location range specified by ARG. Returns
14059 the breakpoint range in BP_NUM_RANGE, and the location range in
14060 BP_LOC_RANGE.
14061
14062 ARG may be in any of the following forms:
14063
14064 x where 'x' is a breakpoint number.
14065 x-y where 'x' and 'y' specify a breakpoint numbers range.
14066 x.y where 'x' is a breakpoint number and 'y' a location number.
14067 x.y-z where 'x' is a breakpoint number and 'y' and 'z' specify a
14068 location number range.
14069*/
14070
cc638e86 14071static void
d0fe4701
XR
14072extract_bp_number_and_location (const std::string &arg,
14073 std::pair<int, int> &bp_num_range,
14074 std::pair<int, int> &bp_loc_range)
14075{
14076 std::string::size_type dot = arg.find ('.');
14077
14078 if (dot != std::string::npos)
14079 {
14080 /* Handle 'x.y' and 'x.y-z' cases. */
14081
14082 if (arg.length () == dot + 1 || dot == 0)
95e95a6d 14083 error (_("Bad breakpoint number at or near: '%s'"), arg.c_str ());
d0fe4701 14084
95e95a6d
PA
14085 bp_num_range.first
14086 = extract_bp_num (extract_bp_kind::bp, arg.c_str (), '.');
14087 bp_num_range.second = bp_num_range.first;
d0fe4701 14088
95e95a6d
PA
14089 bp_loc_range = extract_bp_or_bp_range (extract_bp_kind::loc,
14090 arg, dot + 1);
d0fe4701
XR
14091 }
14092 else
14093 {
14094 /* Handle x and x-y cases. */
d0fe4701 14095
95e95a6d 14096 bp_num_range = extract_bp_or_bp_range (extract_bp_kind::bp, arg, 0);
d0fe4701
XR
14097 bp_loc_range.first = 0;
14098 bp_loc_range.second = 0;
14099 }
d0fe4701
XR
14100}
14101
14102/* Enable or disable a breakpoint location BP_NUM.LOC_NUM. ENABLE
14103 specifies whether to enable or disable. */
14104
14105static void
14106enable_disable_bp_num_loc (int bp_num, int loc_num, bool enable)
14107{
14108 struct bp_location *loc = find_location_by_number (bp_num, loc_num);
14109 if (loc != NULL)
14110 {
14111 if (loc->enabled != enable)
14112 {
14113 loc->enabled = enable;
14114 mark_breakpoint_location_modified (loc);
14115 }
14116 if (target_supports_enable_disable_tracepoint ()
14117 && current_trace_status ()->running && loc->owner
14118 && is_tracepoint (loc->owner))
14119 target_disable_tracepoint (loc);
14120 }
14121 update_global_location_list (UGLL_DONT_INSERT);
d7154a8d
JV
14122
14123 gdb::observers::breakpoint_modified.notify (loc->owner);
d0fe4701
XR
14124}
14125
14126/* Enable or disable a range of breakpoint locations. BP_NUM is the
14127 number of the breakpoint, and BP_LOC_RANGE specifies the
14128 (inclusive) range of location numbers of that breakpoint to
14129 enable/disable. ENABLE specifies whether to enable or disable the
14130 location. */
14131
14132static void
14133enable_disable_breakpoint_location_range (int bp_num,
14134 std::pair<int, int> &bp_loc_range,
14135 bool enable)
14136{
14137 for (int i = bp_loc_range.first; i <= bp_loc_range.second; i++)
14138 enable_disable_bp_num_loc (bp_num, i, enable);
14139}
0d381245 14140
1900040c
MS
14141/* Set ignore-count of breakpoint number BPTNUM to COUNT.
14142 If from_tty is nonzero, it prints a message to that effect,
14143 which ends with a period (no newline). */
14144
c906108c 14145void
fba45db2 14146disable_breakpoint (struct breakpoint *bpt)
c906108c
SS
14147{
14148 /* Never disable a watchpoint scope breakpoint; we want to
14149 hit them when we leave scope so we can delete both the
14150 watchpoint and its scope breakpoint at that time. */
14151 if (bpt->type == bp_watchpoint_scope)
14152 return;
14153
b5de0fa7 14154 bpt->enable_state = bp_disabled;
c906108c 14155
b775012e
LM
14156 /* Mark breakpoint locations modified. */
14157 mark_breakpoint_modified (bpt);
14158
d248b706
KY
14159 if (target_supports_enable_disable_tracepoint ()
14160 && current_trace_status ()->running && is_tracepoint (bpt))
14161 {
14162 struct bp_location *location;
14163
14164 for (location = bpt->loc; location; location = location->next)
14165 target_disable_tracepoint (location);
14166 }
14167
44702360 14168 update_global_location_list (UGLL_DONT_INSERT);
c906108c 14169
76727919 14170 gdb::observers::breakpoint_modified.notify (bpt);
c906108c
SS
14171}
14172
d0fe4701
XR
14173/* Enable or disable the breakpoint(s) or breakpoint location(s)
14174 specified in ARGS. ARGS may be in any of the formats handled by
14175 extract_bp_number_and_location. ENABLE specifies whether to enable
14176 or disable the breakpoints/locations. */
14177
c906108c 14178static void
d0fe4701 14179enable_disable_command (const char *args, int from_tty, bool enable)
c906108c 14180{
c906108c 14181 if (args == 0)
46c6471b
PA
14182 {
14183 struct breakpoint *bpt;
14184
14185 ALL_BREAKPOINTS (bpt)
14186 if (user_breakpoint_p (bpt))
d0fe4701
XR
14187 {
14188 if (enable)
14189 enable_breakpoint (bpt);
14190 else
14191 disable_breakpoint (bpt);
14192 }
46c6471b 14193 }
9eaabc75 14194 else
0d381245 14195 {
cb791d59 14196 std::string num = extract_arg (&args);
9eaabc75 14197
cb791d59 14198 while (!num.empty ())
d248b706 14199 {
d0fe4701 14200 std::pair<int, int> bp_num_range, bp_loc_range;
9eaabc75 14201
cc638e86
PA
14202 extract_bp_number_and_location (num, bp_num_range, bp_loc_range);
14203
14204 if (bp_loc_range.first == bp_loc_range.second
14205 && bp_loc_range.first == 0)
d0fe4701 14206 {
cc638e86
PA
14207 /* Handle breakpoint ids with formats 'x' or 'x-z'. */
14208 map_breakpoint_number_range (bp_num_range,
14209 enable
14210 ? enable_breakpoint
14211 : disable_breakpoint);
14212 }
14213 else
14214 {
14215 /* Handle breakpoint ids with formats 'x.y' or
14216 'x.y-z'. */
14217 enable_disable_breakpoint_location_range
14218 (bp_num_range.first, bp_loc_range, enable);
b775012e 14219 }
9eaabc75 14220 num = extract_arg (&args);
d248b706 14221 }
0d381245 14222 }
c906108c
SS
14223}
14224
d0fe4701
XR
14225/* The disable command disables the specified breakpoints/locations
14226 (or all defined breakpoints) so they're no longer effective in
14227 stopping the inferior. ARGS may be in any of the forms defined in
14228 extract_bp_number_and_location. */
14229
14230static void
14231disable_command (const char *args, int from_tty)
14232{
14233 enable_disable_command (args, from_tty, false);
14234}
14235
c906108c 14236static void
816338b5
SS
14237enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
14238 int count)
c906108c 14239{
afe38095 14240 int target_resources_ok;
c906108c
SS
14241
14242 if (bpt->type == bp_hardware_breakpoint)
14243 {
14244 int i;
c5aa993b 14245 i = hw_breakpoint_used_count ();
53a5351d 14246 target_resources_ok =
d92524f1 14247 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
53a5351d 14248 i + 1, 0);
c906108c 14249 if (target_resources_ok == 0)
8a3fe4f8 14250 error (_("No hardware breakpoint support in the target."));
c906108c 14251 else if (target_resources_ok < 0)
8a3fe4f8 14252 error (_("Hardware breakpoints used exceeds limit."));
c906108c
SS
14253 }
14254
cc60f2e3 14255 if (is_watchpoint (bpt))
c906108c 14256 {
d07205c2 14257 /* Initialize it just to avoid a GCC false warning. */
f486487f 14258 enum enable_state orig_enable_state = bp_disabled;
dde02812 14259
a70b8144 14260 try
c906108c 14261 {
3a5c3e22
PA
14262 struct watchpoint *w = (struct watchpoint *) bpt;
14263
1e718ff1
TJB
14264 orig_enable_state = bpt->enable_state;
14265 bpt->enable_state = bp_enabled;
3a5c3e22 14266 update_watchpoint (w, 1 /* reparse */);
c906108c 14267 }
230d2906 14268 catch (const gdb_exception &e)
c5aa993b 14269 {
1e718ff1 14270 bpt->enable_state = orig_enable_state;
dde02812
ES
14271 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
14272 bpt->number);
14273 return;
c5aa993b 14274 }
c906108c 14275 }
0101ce28 14276
b775012e
LM
14277 bpt->enable_state = bp_enabled;
14278
14279 /* Mark breakpoint locations modified. */
14280 mark_breakpoint_modified (bpt);
14281
d248b706
KY
14282 if (target_supports_enable_disable_tracepoint ()
14283 && current_trace_status ()->running && is_tracepoint (bpt))
14284 {
14285 struct bp_location *location;
14286
14287 for (location = bpt->loc; location; location = location->next)
14288 target_enable_tracepoint (location);
14289 }
14290
b4c291bb 14291 bpt->disposition = disposition;
816338b5 14292 bpt->enable_count = count;
44702360 14293 update_global_location_list (UGLL_MAY_INSERT);
9c97429f 14294
76727919 14295 gdb::observers::breakpoint_modified.notify (bpt);
c906108c
SS
14296}
14297
fe3f5fa8 14298
c906108c 14299void
fba45db2 14300enable_breakpoint (struct breakpoint *bpt)
c906108c 14301{
816338b5 14302 enable_breakpoint_disp (bpt, bpt->disposition, 0);
51be5b68
PA
14303}
14304
d0fe4701
XR
14305/* The enable command enables the specified breakpoints/locations (or
14306 all defined breakpoints) so they once again become (or continue to
14307 be) effective in stopping the inferior. ARGS may be in any of the
14308 forms defined in extract_bp_number_and_location. */
c906108c 14309
c906108c 14310static void
981a3fb3 14311enable_command (const char *args, int from_tty)
c906108c 14312{
d0fe4701 14313 enable_disable_command (args, from_tty, true);
c906108c
SS
14314}
14315
c906108c 14316static void
4495129a 14317enable_once_command (const char *args, int from_tty)
c906108c 14318{
48649e1b
TT
14319 map_breakpoint_numbers
14320 (args, [&] (breakpoint *b)
14321 {
14322 iterate_over_related_breakpoints
14323 (b, [&] (breakpoint *bpt)
14324 {
14325 enable_breakpoint_disp (bpt, disp_disable, 1);
14326 });
14327 });
816338b5
SS
14328}
14329
14330static void
4495129a 14331enable_count_command (const char *args, int from_tty)
816338b5 14332{
b9d61307
SM
14333 int count;
14334
14335 if (args == NULL)
14336 error_no_arg (_("hit count"));
14337
14338 count = get_number (&args);
816338b5 14339
48649e1b
TT
14340 map_breakpoint_numbers
14341 (args, [&] (breakpoint *b)
14342 {
14343 iterate_over_related_breakpoints
14344 (b, [&] (breakpoint *bpt)
14345 {
14346 enable_breakpoint_disp (bpt, disp_disable, count);
14347 });
14348 });
c906108c
SS
14349}
14350
c906108c 14351static void
4495129a 14352enable_delete_command (const char *args, int from_tty)
c906108c 14353{
48649e1b
TT
14354 map_breakpoint_numbers
14355 (args, [&] (breakpoint *b)
14356 {
14357 iterate_over_related_breakpoints
14358 (b, [&] (breakpoint *bpt)
14359 {
14360 enable_breakpoint_disp (bpt, disp_del, 1);
14361 });
14362 });
c906108c
SS
14363}
14364\f
fa8d40ab 14365static void
981a3fb3 14366set_breakpoint_cmd (const char *args, int from_tty)
fa8d40ab
JJ
14367{
14368}
14369
14370static void
981a3fb3 14371show_breakpoint_cmd (const char *args, int from_tty)
fa8d40ab
JJ
14372{
14373}
14374
1f3b5d1b
PP
14375/* Invalidate last known value of any hardware watchpoint if
14376 the memory which that value represents has been written to by
14377 GDB itself. */
14378
14379static void
8de0566d
YQ
14380invalidate_bp_value_on_memory_change (struct inferior *inferior,
14381 CORE_ADDR addr, ssize_t len,
1f3b5d1b
PP
14382 const bfd_byte *data)
14383{
14384 struct breakpoint *bp;
14385
14386 ALL_BREAKPOINTS (bp)
14387 if (bp->enable_state == bp_enabled
3a5c3e22 14388 && bp->type == bp_hardware_watchpoint)
1f3b5d1b 14389 {
3a5c3e22 14390 struct watchpoint *wp = (struct watchpoint *) bp;
1f3b5d1b 14391
850645cf 14392 if (wp->val_valid && wp->val != nullptr)
3a5c3e22
PA
14393 {
14394 struct bp_location *loc;
14395
14396 for (loc = bp->loc; loc != NULL; loc = loc->next)
14397 if (loc->loc_type == bp_loc_hardware_watchpoint
14398 && loc->address + loc->length > addr
14399 && addr + len > loc->address)
14400 {
3a5c3e22
PA
14401 wp->val = NULL;
14402 wp->val_valid = 0;
14403 }
14404 }
1f3b5d1b
PP
14405 }
14406}
14407
8181d85f
DJ
14408/* Create and insert a breakpoint for software single step. */
14409
14410void
6c95b8df 14411insert_single_step_breakpoint (struct gdbarch *gdbarch,
accd0bcd 14412 const address_space *aspace,
4a64f543 14413 CORE_ADDR next_pc)
8181d85f 14414{
7c16b83e
PA
14415 struct thread_info *tp = inferior_thread ();
14416 struct symtab_and_line sal;
14417 CORE_ADDR pc = next_pc;
8181d85f 14418
34b7e8a6
PA
14419 if (tp->control.single_step_breakpoints == NULL)
14420 {
14421 tp->control.single_step_breakpoints
5d5658a1 14422 = new_single_step_breakpoint (tp->global_num, gdbarch);
34b7e8a6 14423 }
8181d85f 14424
7c16b83e
PA
14425 sal = find_pc_line (pc, 0);
14426 sal.pc = pc;
14427 sal.section = find_pc_overlay (pc);
14428 sal.explicit_pc = 1;
34b7e8a6 14429 add_location_to_breakpoint (tp->control.single_step_breakpoints, &sal);
8181d85f 14430
7c16b83e 14431 update_global_location_list (UGLL_INSERT);
8181d85f
DJ
14432}
14433
93f9a11f
YQ
14434/* Insert single step breakpoints according to the current state. */
14435
14436int
14437insert_single_step_breakpoints (struct gdbarch *gdbarch)
14438{
f5ea389a 14439 struct regcache *regcache = get_current_regcache ();
a0ff9e1a 14440 std::vector<CORE_ADDR> next_pcs;
93f9a11f 14441
f5ea389a 14442 next_pcs = gdbarch_software_single_step (gdbarch, regcache);
93f9a11f 14443
a0ff9e1a 14444 if (!next_pcs.empty ())
93f9a11f 14445 {
f5ea389a 14446 struct frame_info *frame = get_current_frame ();
8b86c959 14447 const address_space *aspace = get_frame_address_space (frame);
93f9a11f 14448
a0ff9e1a 14449 for (CORE_ADDR pc : next_pcs)
93f9a11f
YQ
14450 insert_single_step_breakpoint (gdbarch, aspace, pc);
14451
93f9a11f
YQ
14452 return 1;
14453 }
14454 else
14455 return 0;
14456}
14457
34b7e8a6 14458/* See breakpoint.h. */
f02253f1
HZ
14459
14460int
7c16b83e 14461breakpoint_has_location_inserted_here (struct breakpoint *bp,
accd0bcd 14462 const address_space *aspace,
7c16b83e 14463 CORE_ADDR pc)
1aafd4da 14464{
7c16b83e 14465 struct bp_location *loc;
1aafd4da 14466
7c16b83e
PA
14467 for (loc = bp->loc; loc != NULL; loc = loc->next)
14468 if (loc->inserted
14469 && breakpoint_location_address_match (loc, aspace, pc))
14470 return 1;
1aafd4da 14471
7c16b83e 14472 return 0;
ef370185
JB
14473}
14474
14475/* Check whether a software single-step breakpoint is inserted at
14476 PC. */
14477
14478int
accd0bcd 14479single_step_breakpoint_inserted_here_p (const address_space *aspace,
ef370185
JB
14480 CORE_ADDR pc)
14481{
34b7e8a6
PA
14482 struct breakpoint *bpt;
14483
14484 ALL_BREAKPOINTS (bpt)
14485 {
14486 if (bpt->type == bp_single_step
14487 && breakpoint_has_location_inserted_here (bpt, aspace, pc))
14488 return 1;
14489 }
14490 return 0;
1aafd4da
UW
14491}
14492
1042e4c0
SS
14493/* Tracepoint-specific operations. */
14494
14495/* Set tracepoint count to NUM. */
14496static void
14497set_tracepoint_count (int num)
14498{
14499 tracepoint_count = num;
4fa62494 14500 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
1042e4c0
SS
14501}
14502
70221824 14503static void
0b39b52e 14504trace_command (const char *arg, int from_tty)
1042e4c0 14505{
55aa24fb 14506 struct breakpoint_ops *ops;
55aa24fb 14507
ffc2605c
TT
14508 event_location_up location = string_to_event_location (&arg,
14509 current_language);
5b56227b 14510 if (location != NULL
ffc2605c 14511 && event_location_type (location.get ()) == PROBE_LOCATION)
55aa24fb
SDJ
14512 ops = &tracepoint_probe_breakpoint_ops;
14513 else
14514 ops = &tracepoint_breakpoint_ops;
14515
558a9d82 14516 create_breakpoint (get_current_arch (),
ffc2605c 14517 location.get (),
f00aae0f 14518 NULL, 0, arg, 1 /* parse arg */,
558a9d82
YQ
14519 0 /* tempflag */,
14520 bp_tracepoint /* type_wanted */,
14521 0 /* Ignore count */,
14522 pending_break_support,
14523 ops,
14524 from_tty,
14525 1 /* enabled */,
14526 0 /* internal */, 0);
1042e4c0
SS
14527}
14528
70221824 14529static void
0b39b52e 14530ftrace_command (const char *arg, int from_tty)
7a697b8d 14531{
ffc2605c
TT
14532 event_location_up location = string_to_event_location (&arg,
14533 current_language);
558a9d82 14534 create_breakpoint (get_current_arch (),
ffc2605c 14535 location.get (),
f00aae0f 14536 NULL, 0, arg, 1 /* parse arg */,
558a9d82
YQ
14537 0 /* tempflag */,
14538 bp_fast_tracepoint /* type_wanted */,
14539 0 /* Ignore count */,
14540 pending_break_support,
14541 &tracepoint_breakpoint_ops,
14542 from_tty,
14543 1 /* enabled */,
14544 0 /* internal */, 0);
0fb4aa4b
PA
14545}
14546
14547/* strace command implementation. Creates a static tracepoint. */
14548
70221824 14549static void
0b39b52e 14550strace_command (const char *arg, int from_tty)
0fb4aa4b 14551{
983af33b 14552 struct breakpoint_ops *ops;
ffc2605c 14553 event_location_up location;
983af33b
SDJ
14554
14555 /* Decide if we are dealing with a static tracepoint marker (`-m'),
14556 or with a normal static tracepoint. */
61012eef 14557 if (arg && startswith (arg, "-m") && isspace (arg[2]))
f00aae0f
KS
14558 {
14559 ops = &strace_marker_breakpoint_ops;
a20714ff 14560 location = new_linespec_location (&arg, symbol_name_match_type::FULL);
f00aae0f 14561 }
983af33b 14562 else
f00aae0f
KS
14563 {
14564 ops = &tracepoint_breakpoint_ops;
14565 location = string_to_event_location (&arg, current_language);
14566 }
983af33b 14567
558a9d82 14568 create_breakpoint (get_current_arch (),
ffc2605c 14569 location.get (),
f00aae0f 14570 NULL, 0, arg, 1 /* parse arg */,
558a9d82
YQ
14571 0 /* tempflag */,
14572 bp_static_tracepoint /* type_wanted */,
14573 0 /* Ignore count */,
14574 pending_break_support,
14575 ops,
14576 from_tty,
14577 1 /* enabled */,
14578 0 /* internal */, 0);
7a697b8d
SS
14579}
14580
409873ef
SS
14581/* Set up a fake reader function that gets command lines from a linked
14582 list that was acquired during tracepoint uploading. */
14583
14584static struct uploaded_tp *this_utp;
3149d8c1 14585static int next_cmd;
409873ef
SS
14586
14587static char *
14588read_uploaded_action (void)
14589{
a18ba4e4 14590 char *rslt = nullptr;
409873ef 14591
a18ba4e4
SM
14592 if (next_cmd < this_utp->cmd_strings.size ())
14593 {
67aa1f3c 14594 rslt = this_utp->cmd_strings[next_cmd].get ();
a18ba4e4
SM
14595 next_cmd++;
14596 }
409873ef
SS
14597
14598 return rslt;
14599}
14600
00bf0b85
SS
14601/* Given information about a tracepoint as recorded on a target (which
14602 can be either a live system or a trace file), attempt to create an
14603 equivalent GDB tracepoint. This is not a reliable process, since
14604 the target does not necessarily have all the information used when
14605 the tracepoint was originally defined. */
14606
d9b3f62e 14607struct tracepoint *
00bf0b85 14608create_tracepoint_from_upload (struct uploaded_tp *utp)
d5551862 14609{
f2fc3015
TT
14610 const char *addr_str;
14611 char small_buf[100];
d9b3f62e 14612 struct tracepoint *tp;
fd9b8c24 14613
409873ef 14614 if (utp->at_string)
67aa1f3c 14615 addr_str = utp->at_string.get ();
409873ef
SS
14616 else
14617 {
14618 /* In the absence of a source location, fall back to raw
14619 address. Since there is no way to confirm that the address
14620 means the same thing as when the trace was started, warn the
14621 user. */
3e43a32a
MS
14622 warning (_("Uploaded tracepoint %d has no "
14623 "source location, using raw address"),
409873ef 14624 utp->number);
8c042590 14625 xsnprintf (small_buf, sizeof (small_buf), "*%s", hex_string (utp->addr));
409873ef
SS
14626 addr_str = small_buf;
14627 }
14628
14629 /* There's not much we can do with a sequence of bytecodes. */
14630 if (utp->cond && !utp->cond_string)
3e43a32a
MS
14631 warning (_("Uploaded tracepoint %d condition "
14632 "has no source form, ignoring it"),
409873ef 14633 utp->number);
d5551862 14634
ffc2605c
TT
14635 event_location_up location = string_to_event_location (&addr_str,
14636 current_language);
8cdf0e15 14637 if (!create_breakpoint (get_current_arch (),
ffc2605c 14638 location.get (),
67aa1f3c 14639 utp->cond_string.get (), -1, addr_str,
e7e0cddf 14640 0 /* parse cond/thread */,
8cdf0e15 14641 0 /* tempflag */,
0fb4aa4b 14642 utp->type /* type_wanted */,
8cdf0e15
VP
14643 0 /* Ignore count */,
14644 pending_break_support,
348d480f 14645 &tracepoint_breakpoint_ops,
8cdf0e15 14646 0 /* from_tty */,
84f4c1fe 14647 utp->enabled /* enabled */,
44f238bb
PA
14648 0 /* internal */,
14649 CREATE_BREAKPOINT_FLAGS_INSERTED))
ffc2605c 14650 return NULL;
fd9b8c24 14651
409873ef 14652 /* Get the tracepoint we just created. */
fd9b8c24
PA
14653 tp = get_tracepoint (tracepoint_count);
14654 gdb_assert (tp != NULL);
d5551862 14655
00bf0b85
SS
14656 if (utp->pass > 0)
14657 {
8c042590 14658 xsnprintf (small_buf, sizeof (small_buf), "%d %d", utp->pass,
c1fc2657 14659 tp->number);
00bf0b85 14660
409873ef 14661 trace_pass_command (small_buf, 0);
00bf0b85
SS
14662 }
14663
409873ef
SS
14664 /* If we have uploaded versions of the original commands, set up a
14665 special-purpose "reader" function and call the usual command line
14666 reader, then pass the result to the breakpoint command-setting
14667 function. */
a18ba4e4 14668 if (!utp->cmd_strings.empty ())
00bf0b85 14669 {
12973681 14670 counted_command_line cmd_list;
00bf0b85 14671
409873ef 14672 this_utp = utp;
3149d8c1 14673 next_cmd = 0;
d5551862 14674
60b3cef2 14675 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL);
409873ef 14676
c1fc2657 14677 breakpoint_set_commands (tp, std::move (cmd_list));
00bf0b85 14678 }
a18ba4e4
SM
14679 else if (!utp->actions.empty ()
14680 || !utp->step_actions.empty ())
3e43a32a
MS
14681 warning (_("Uploaded tracepoint %d actions "
14682 "have no source form, ignoring them"),
409873ef 14683 utp->number);
00bf0b85 14684
f196051f 14685 /* Copy any status information that might be available. */
c1fc2657 14686 tp->hit_count = utp->hit_count;
f196051f
SS
14687 tp->traceframe_usage = utp->traceframe_usage;
14688
00bf0b85 14689 return tp;
d9b3f62e 14690}
00bf0b85 14691
1042e4c0
SS
14692/* Print information on tracepoint number TPNUM_EXP, or all if
14693 omitted. */
14694
14695static void
1d12d88f 14696info_tracepoints_command (const char *args, int from_tty)
1042e4c0 14697{
79a45e25 14698 struct ui_out *uiout = current_uiout;
e5a67952 14699 int num_printed;
1042e4c0 14700
e5a67952 14701 num_printed = breakpoint_1 (args, 0, is_tracepoint);
d77f58be
SS
14702
14703 if (num_printed == 0)
1042e4c0 14704 {
e5a67952 14705 if (args == NULL || *args == '\0')
112e8700 14706 uiout->message ("No tracepoints.\n");
d77f58be 14707 else
112e8700 14708 uiout->message ("No tracepoint matching '%s'.\n", args);
1042e4c0 14709 }
ad443146
SS
14710
14711 default_collect_info ();
1042e4c0
SS
14712}
14713
4a64f543 14714/* The 'enable trace' command enables tracepoints.
1042e4c0
SS
14715 Not supported by all targets. */
14716static void
5fed81ff 14717enable_trace_command (const char *args, int from_tty)
1042e4c0
SS
14718{
14719 enable_command (args, from_tty);
14720}
14721
4a64f543 14722/* The 'disable trace' command disables tracepoints.
1042e4c0
SS
14723 Not supported by all targets. */
14724static void
5fed81ff 14725disable_trace_command (const char *args, int from_tty)
1042e4c0
SS
14726{
14727 disable_command (args, from_tty);
14728}
14729
4a64f543 14730/* Remove a tracepoint (or all if no argument). */
1042e4c0 14731static void
4495129a 14732delete_trace_command (const char *arg, int from_tty)
1042e4c0 14733{
35df4500 14734 struct breakpoint *b, *b_tmp;
1042e4c0
SS
14735
14736 dont_repeat ();
14737
14738 if (arg == 0)
14739 {
14740 int breaks_to_delete = 0;
14741
14742 /* Delete all breakpoints if no argument.
14743 Do not delete internal or call-dummy breakpoints, these
4a64f543
MS
14744 have to be deleted with an explicit breakpoint number
14745 argument. */
1042e4c0 14746 ALL_TRACEPOINTS (b)
46c6471b 14747 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0
SS
14748 {
14749 breaks_to_delete = 1;
14750 break;
14751 }
1042e4c0
SS
14752
14753 /* Ask user only if there are some breakpoints to delete. */
14754 if (!from_tty
14755 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
14756 {
35df4500 14757 ALL_BREAKPOINTS_SAFE (b, b_tmp)
46c6471b 14758 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0 14759 delete_breakpoint (b);
1042e4c0
SS
14760 }
14761 }
14762 else
48649e1b 14763 map_breakpoint_numbers
b926417a 14764 (arg, [&] (breakpoint *br)
48649e1b 14765 {
b926417a 14766 iterate_over_related_breakpoints (br, delete_breakpoint);
48649e1b 14767 });
1042e4c0
SS
14768}
14769
197f0a60
TT
14770/* Helper function for trace_pass_command. */
14771
14772static void
d9b3f62e 14773trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
197f0a60 14774{
d9b3f62e 14775 tp->pass_count = count;
76727919 14776 gdb::observers::breakpoint_modified.notify (tp);
197f0a60
TT
14777 if (from_tty)
14778 printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
c1fc2657 14779 tp->number, count);
197f0a60
TT
14780}
14781
1042e4c0
SS
14782/* Set passcount for tracepoint.
14783
14784 First command argument is passcount, second is tracepoint number.
14785 If tracepoint number omitted, apply to most recently defined.
14786 Also accepts special argument "all". */
14787
14788static void
0b39b52e 14789trace_pass_command (const char *args, int from_tty)
1042e4c0 14790{
d9b3f62e 14791 struct tracepoint *t1;
0b39b52e 14792 ULONGEST count;
1042e4c0
SS
14793
14794 if (args == 0 || *args == 0)
3e43a32a
MS
14795 error (_("passcount command requires an "
14796 "argument (count + optional TP num)"));
1042e4c0 14797
0b39b52e 14798 count = strtoulst (args, &args, 10); /* Count comes first, then TP num. */
1042e4c0 14799
529480d0 14800 args = skip_spaces (args);
1042e4c0
SS
14801 if (*args && strncasecmp (args, "all", 3) == 0)
14802 {
d9b3f62e
PA
14803 struct breakpoint *b;
14804
1042e4c0 14805 args += 3; /* Skip special argument "all". */
1042e4c0
SS
14806 if (*args)
14807 error (_("Junk at end of arguments."));
1042e4c0 14808
d9b3f62e 14809 ALL_TRACEPOINTS (b)
197f0a60 14810 {
d9b3f62e 14811 t1 = (struct tracepoint *) b;
197f0a60
TT
14812 trace_pass_set_count (t1, count, from_tty);
14813 }
14814 }
14815 else if (*args == '\0')
1042e4c0 14816 {
5fa1d40e 14817 t1 = get_tracepoint_by_number (&args, NULL);
1042e4c0 14818 if (t1)
197f0a60
TT
14819 trace_pass_set_count (t1, count, from_tty);
14820 }
14821 else
14822 {
bfd28288
PA
14823 number_or_range_parser parser (args);
14824 while (!parser.finished ())
1042e4c0 14825 {
bfd28288 14826 t1 = get_tracepoint_by_number (&args, &parser);
197f0a60
TT
14827 if (t1)
14828 trace_pass_set_count (t1, count, from_tty);
1042e4c0
SS
14829 }
14830 }
1042e4c0
SS
14831}
14832
d9b3f62e 14833struct tracepoint *
1042e4c0
SS
14834get_tracepoint (int num)
14835{
14836 struct breakpoint *t;
14837
14838 ALL_TRACEPOINTS (t)
14839 if (t->number == num)
d9b3f62e 14840 return (struct tracepoint *) t;
1042e4c0
SS
14841
14842 return NULL;
14843}
14844
d5551862
SS
14845/* Find the tracepoint with the given target-side number (which may be
14846 different from the tracepoint number after disconnecting and
14847 reconnecting). */
14848
d9b3f62e 14849struct tracepoint *
d5551862
SS
14850get_tracepoint_by_number_on_target (int num)
14851{
d9b3f62e 14852 struct breakpoint *b;
d5551862 14853
d9b3f62e
PA
14854 ALL_TRACEPOINTS (b)
14855 {
14856 struct tracepoint *t = (struct tracepoint *) b;
14857
14858 if (t->number_on_target == num)
14859 return t;
14860 }
d5551862
SS
14861
14862 return NULL;
14863}
14864
1042e4c0 14865/* Utility: parse a tracepoint number and look it up in the list.
197f0a60 14866 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
5fa1d40e
YQ
14867 If the argument is missing, the most recent tracepoint
14868 (tracepoint_count) is returned. */
14869
d9b3f62e 14870struct tracepoint *
0b39b52e 14871get_tracepoint_by_number (const char **arg,
bfd28288 14872 number_or_range_parser *parser)
1042e4c0 14873{
1042e4c0
SS
14874 struct breakpoint *t;
14875 int tpnum;
0b39b52e 14876 const char *instring = arg == NULL ? NULL : *arg;
1042e4c0 14877
bfd28288 14878 if (parser != NULL)
197f0a60 14879 {
bfd28288
PA
14880 gdb_assert (!parser->finished ());
14881 tpnum = parser->get_number ();
197f0a60
TT
14882 }
14883 else if (arg == NULL || *arg == NULL || ! **arg)
5fa1d40e 14884 tpnum = tracepoint_count;
1042e4c0 14885 else
197f0a60 14886 tpnum = get_number (arg);
1042e4c0
SS
14887
14888 if (tpnum <= 0)
14889 {
14890 if (instring && *instring)
14891 printf_filtered (_("bad tracepoint number at or near '%s'\n"),
14892 instring);
14893 else
5fa1d40e 14894 printf_filtered (_("No previous tracepoint\n"));
1042e4c0
SS
14895 return NULL;
14896 }
14897
14898 ALL_TRACEPOINTS (t)
14899 if (t->number == tpnum)
14900 {
d9b3f62e 14901 return (struct tracepoint *) t;
1042e4c0
SS
14902 }
14903
1042e4c0
SS
14904 printf_unfiltered ("No tracepoint number %d.\n", tpnum);
14905 return NULL;
14906}
14907
d9b3f62e
PA
14908void
14909print_recreate_thread (struct breakpoint *b, struct ui_file *fp)
14910{
14911 if (b->thread != -1)
14912 fprintf_unfiltered (fp, " thread %d", b->thread);
14913
14914 if (b->task != 0)
14915 fprintf_unfiltered (fp, " task %d", b->task);
14916
14917 fprintf_unfiltered (fp, "\n");
14918}
14919
6149aea9
PA
14920/* Save information on user settable breakpoints (watchpoints, etc) to
14921 a new script file named FILENAME. If FILTER is non-NULL, call it
14922 on each breakpoint and only include the ones for which it returns
14923 non-zero. */
14924
1042e4c0 14925static void
4495129a 14926save_breakpoints (const char *filename, int from_tty,
6149aea9 14927 int (*filter) (const struct breakpoint *))
1042e4c0
SS
14928{
14929 struct breakpoint *tp;
6149aea9 14930 int any = 0;
6149aea9 14931 int extra_trace_bits = 0;
1042e4c0 14932
6149aea9
PA
14933 if (filename == 0 || *filename == 0)
14934 error (_("Argument required (file name in which to save)"));
1042e4c0
SS
14935
14936 /* See if we have anything to save. */
6149aea9 14937 ALL_BREAKPOINTS (tp)
1042e4c0 14938 {
6149aea9 14939 /* Skip internal and momentary breakpoints. */
09d682a4 14940 if (!user_breakpoint_p (tp))
6149aea9
PA
14941 continue;
14942
14943 /* If we have a filter, only save the breakpoints it accepts. */
14944 if (filter && !filter (tp))
14945 continue;
14946
14947 any = 1;
14948
14949 if (is_tracepoint (tp))
14950 {
14951 extra_trace_bits = 1;
14952
14953 /* We can stop searching. */
14954 break;
14955 }
1042e4c0 14956 }
6149aea9
PA
14957
14958 if (!any)
1042e4c0 14959 {
6149aea9 14960 warning (_("Nothing to save."));
1042e4c0
SS
14961 return;
14962 }
14963
ee0c3293 14964 gdb::unique_xmalloc_ptr<char> expanded_filename (tilde_expand (filename));
d7e74731
PA
14965
14966 stdio_file fp;
14967
ee0c3293 14968 if (!fp.open (expanded_filename.get (), "w"))
6149aea9 14969 error (_("Unable to open file '%s' for saving (%s)"),
ee0c3293 14970 expanded_filename.get (), safe_strerror (errno));
8bf6485c 14971
6149aea9 14972 if (extra_trace_bits)
d7e74731 14973 save_trace_state_variables (&fp);
8bf6485c 14974
6149aea9 14975 ALL_BREAKPOINTS (tp)
1042e4c0 14976 {
6149aea9 14977 /* Skip internal and momentary breakpoints. */
09d682a4 14978 if (!user_breakpoint_p (tp))
6149aea9 14979 continue;
8bf6485c 14980
6149aea9
PA
14981 /* If we have a filter, only save the breakpoints it accepts. */
14982 if (filter && !filter (tp))
14983 continue;
14984
d7e74731 14985 tp->ops->print_recreate (tp, &fp);
1042e4c0 14986
6149aea9
PA
14987 /* Note, we can't rely on tp->number for anything, as we can't
14988 assume the recreated breakpoint numbers will match. Use $bpnum
14989 instead. */
14990
14991 if (tp->cond_string)
d7e74731 14992 fp.printf (" condition $bpnum %s\n", tp->cond_string);
6149aea9
PA
14993
14994 if (tp->ignore_count)
d7e74731 14995 fp.printf (" ignore $bpnum %d\n", tp->ignore_count);
6149aea9 14996
2d9442cc 14997 if (tp->type != bp_dprintf && tp->commands)
1042e4c0 14998 {
d7e74731 14999 fp.puts (" commands\n");
a7bdde9e 15000
d7e74731 15001 current_uiout->redirect (&fp);
a70b8144 15002 try
1042e4c0 15003 {
d1b0a7bf 15004 print_command_lines (current_uiout, tp->commands.get (), 2);
a7bdde9e 15005 }
230d2906 15006 catch (const gdb_exception &ex)
492d29ea 15007 {
112e8700 15008 current_uiout->redirect (NULL);
eedc3f4f 15009 throw;
492d29ea 15010 }
1042e4c0 15011
112e8700 15012 current_uiout->redirect (NULL);
d7e74731 15013 fp.puts (" end\n");
1042e4c0 15014 }
6149aea9
PA
15015
15016 if (tp->enable_state == bp_disabled)
d7e74731 15017 fp.puts ("disable $bpnum\n");
6149aea9
PA
15018
15019 /* If this is a multi-location breakpoint, check if the locations
15020 should be individually disabled. Watchpoint locations are
15021 special, and not user visible. */
15022 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
15023 {
15024 struct bp_location *loc;
15025 int n = 1;
15026
15027 for (loc = tp->loc; loc != NULL; loc = loc->next, n++)
15028 if (!loc->enabled)
d7e74731 15029 fp.printf ("disable $bpnum.%d\n", n);
6149aea9 15030 }
1042e4c0 15031 }
8bf6485c 15032
6149aea9 15033 if (extra_trace_bits && *default_collect)
d7e74731 15034 fp.printf ("set default-collect %s\n", default_collect);
8bf6485c 15035
1042e4c0 15036 if (from_tty)
ee0c3293 15037 printf_filtered (_("Saved to file '%s'.\n"), expanded_filename.get ());
6149aea9
PA
15038}
15039
15040/* The `save breakpoints' command. */
15041
15042static void
4495129a 15043save_breakpoints_command (const char *args, int from_tty)
6149aea9
PA
15044{
15045 save_breakpoints (args, from_tty, NULL);
15046}
15047
15048/* The `save tracepoints' command. */
15049
15050static void
4495129a 15051save_tracepoints_command (const char *args, int from_tty)
6149aea9
PA
15052{
15053 save_breakpoints (args, from_tty, is_tracepoint);
1042e4c0
SS
15054}
15055
15056/* Create a vector of all tracepoints. */
15057
f51e0e20 15058std::vector<breakpoint *>
eeae04df 15059all_tracepoints (void)
1042e4c0 15060{
f51e0e20 15061 std::vector<breakpoint *> tp_vec;
1042e4c0
SS
15062 struct breakpoint *tp;
15063
15064 ALL_TRACEPOINTS (tp)
15065 {
f51e0e20 15066 tp_vec.push_back (tp);
1042e4c0
SS
15067 }
15068
15069 return tp_vec;
15070}
15071
c906108c 15072\f
629500fa
KS
15073/* This help string is used to consolidate all the help string for specifying
15074 locations used by several commands. */
15075
15076#define LOCATION_HELP_STRING \
15077"Linespecs are colon-separated lists of location parameters, such as\n\
15078source filename, function name, label name, and line number.\n\
15079Example: To specify the start of a label named \"the_top\" in the\n\
15080function \"fact\" in the file \"factorial.c\", use\n\
15081\"factorial.c:fact:the_top\".\n\
15082\n\
15083Address locations begin with \"*\" and specify an exact address in the\n\
15084program. Example: To specify the fourth byte past the start function\n\
15085\"main\", use \"*main + 4\".\n\
15086\n\
15087Explicit locations are similar to linespecs but use an option/argument\n\
15088syntax to specify location parameters.\n\
15089Example: To specify the start of the label named \"the_top\" in the\n\
15090function \"fact\" in the file \"factorial.c\", use \"-source factorial.c\n\
a20714ff
PA
15091-function fact -label the_top\".\n\
15092\n\
15093By default, a specified function is matched against the program's\n\
15094functions in all scopes. For C++, this means in all namespaces and\n\
15095classes. For Ada, this means in all packages. E.g., in C++,\n\
15096\"func()\" matches \"A::func()\", \"A::B::func()\", etc. The\n\
15097\"-qualified\" flag overrides this behavior, making GDB interpret the\n\
89549d7f 15098specified name as a complete fully-qualified name instead."
629500fa 15099
4a64f543
MS
15100/* This help string is used for the break, hbreak, tbreak and thbreak
15101 commands. It is defined as a macro to prevent duplication.
15102 COMMAND should be a string constant containing the name of the
15103 command. */
629500fa 15104
31e2b00f 15105#define BREAK_ARGS_HELP(command) \
fb7b5af4
SDJ
15106command" [PROBE_MODIFIER] [LOCATION] [thread THREADNUM] [if CONDITION]\n\
15107PROBE_MODIFIER shall be present if the command is to be placed in a\n\
15108probe point. Accepted values are `-probe' (for a generic, automatically\n\
d4777acb
JM
15109guessed probe type), `-probe-stap' (for a SystemTap probe) or \n\
15110`-probe-dtrace' (for a DTrace probe).\n\
629500fa
KS
15111LOCATION may be a linespec, address, or explicit location as described\n\
15112below.\n\
15113\n\
dc10affe
PA
15114With no LOCATION, uses current execution address of the selected\n\
15115stack frame. This is useful for breaking on return to a stack frame.\n\
31e2b00f
AS
15116\n\
15117THREADNUM is the number from \"info threads\".\n\
15118CONDITION is a boolean expression.\n\
89549d7f 15119\n" LOCATION_HELP_STRING "\n\n\
d41c0fc8
PA
15120Multiple breakpoints at one place are permitted, and useful if their\n\
15121conditions are different.\n\
31e2b00f
AS
15122\n\
15123Do \"help breakpoints\" for info on other commands dealing with breakpoints."
15124
44feb3ce
TT
15125/* List of subcommands for "catch". */
15126static struct cmd_list_element *catch_cmdlist;
15127
15128/* List of subcommands for "tcatch". */
15129static struct cmd_list_element *tcatch_cmdlist;
15130
9ac4176b 15131void
a121b7c1 15132add_catch_command (const char *name, const char *docstring,
eb4c3f4a 15133 cmd_const_sfunc_ftype *sfunc,
625e8578 15134 completer_ftype *completer,
44feb3ce
TT
15135 void *user_data_catch,
15136 void *user_data_tcatch)
15137{
15138 struct cmd_list_element *command;
15139
0450cc4c 15140 command = add_cmd (name, class_breakpoint, docstring,
44feb3ce
TT
15141 &catch_cmdlist);
15142 set_cmd_sfunc (command, sfunc);
15143 set_cmd_context (command, user_data_catch);
a96d9b2e 15144 set_cmd_completer (command, completer);
44feb3ce 15145
0450cc4c 15146 command = add_cmd (name, class_breakpoint, docstring,
44feb3ce
TT
15147 &tcatch_cmdlist);
15148 set_cmd_sfunc (command, sfunc);
15149 set_cmd_context (command, user_data_tcatch);
a96d9b2e 15150 set_cmd_completer (command, completer);
44feb3ce
TT
15151}
15152
6149aea9 15153static void
981a3fb3 15154save_command (const char *arg, int from_tty)
6149aea9 15155{
3e43a32a
MS
15156 printf_unfiltered (_("\"save\" must be followed by "
15157 "the name of a save subcommand.\n"));
635c7e8a 15158 help_list (save_cmdlist, "save ", all_commands, gdb_stdout);
6149aea9
PA
15159}
15160
84f4c1fe
PM
15161struct breakpoint *
15162iterate_over_breakpoints (int (*callback) (struct breakpoint *, void *),
15163 void *data)
15164{
35df4500 15165 struct breakpoint *b, *b_tmp;
84f4c1fe 15166
35df4500 15167 ALL_BREAKPOINTS_SAFE (b, b_tmp)
84f4c1fe
PM
15168 {
15169 if ((*callback) (b, data))
15170 return b;
15171 }
15172
15173 return NULL;
15174}
15175
0574c78f
GB
15176/* Zero if any of the breakpoint's locations could be a location where
15177 functions have been inlined, nonzero otherwise. */
15178
15179static int
15180is_non_inline_function (struct breakpoint *b)
15181{
15182 /* The shared library event breakpoint is set on the address of a
15183 non-inline function. */
15184 if (b->type == bp_shlib_event)
15185 return 1;
15186
15187 return 0;
15188}
15189
15190/* Nonzero if the specified PC cannot be a location where functions
15191 have been inlined. */
15192
15193int
accd0bcd 15194pc_at_non_inline_function (const address_space *aspace, CORE_ADDR pc,
09ac7c10 15195 const struct target_waitstatus *ws)
0574c78f
GB
15196{
15197 struct breakpoint *b;
15198 struct bp_location *bl;
15199
15200 ALL_BREAKPOINTS (b)
15201 {
15202 if (!is_non_inline_function (b))
15203 continue;
15204
15205 for (bl = b->loc; bl != NULL; bl = bl->next)
15206 {
15207 if (!bl->shlib_disabled
09ac7c10 15208 && bpstat_check_location (bl, aspace, pc, ws))
0574c78f
GB
15209 return 1;
15210 }
15211 }
15212
15213 return 0;
15214}
15215
2f202fde
JK
15216/* Remove any references to OBJFILE which is going to be freed. */
15217
15218void
15219breakpoint_free_objfile (struct objfile *objfile)
15220{
15221 struct bp_location **locp, *loc;
15222
15223 ALL_BP_LOCATIONS (loc, locp)
eb822aa6 15224 if (loc->symtab != NULL && SYMTAB_OBJFILE (loc->symtab) == objfile)
2f202fde
JK
15225 loc->symtab = NULL;
15226}
15227
2060206e
PA
15228void
15229initialize_breakpoint_ops (void)
15230{
15231 static int initialized = 0;
15232
15233 struct breakpoint_ops *ops;
15234
15235 if (initialized)
15236 return;
15237 initialized = 1;
15238
15239 /* The breakpoint_ops structure to be inherit by all kinds of
15240 breakpoints (real breakpoints, i.e., user "break" breakpoints,
15241 internal and momentary breakpoints, etc.). */
15242 ops = &bkpt_base_breakpoint_ops;
15243 *ops = base_breakpoint_ops;
15244 ops->re_set = bkpt_re_set;
15245 ops->insert_location = bkpt_insert_location;
15246 ops->remove_location = bkpt_remove_location;
15247 ops->breakpoint_hit = bkpt_breakpoint_hit;
5f700d83 15248 ops->create_sals_from_location = bkpt_create_sals_from_location;
983af33b 15249 ops->create_breakpoints_sal = bkpt_create_breakpoints_sal;
5f700d83 15250 ops->decode_location = bkpt_decode_location;
2060206e
PA
15251
15252 /* The breakpoint_ops structure to be used in regular breakpoints. */
15253 ops = &bkpt_breakpoint_ops;
15254 *ops = bkpt_base_breakpoint_ops;
15255 ops->re_set = bkpt_re_set;
15256 ops->resources_needed = bkpt_resources_needed;
15257 ops->print_it = bkpt_print_it;
15258 ops->print_mention = bkpt_print_mention;
15259 ops->print_recreate = bkpt_print_recreate;
15260
15261 /* Ranged breakpoints. */
15262 ops = &ranged_breakpoint_ops;
15263 *ops = bkpt_breakpoint_ops;
15264 ops->breakpoint_hit = breakpoint_hit_ranged_breakpoint;
15265 ops->resources_needed = resources_needed_ranged_breakpoint;
15266 ops->print_it = print_it_ranged_breakpoint;
15267 ops->print_one = print_one_ranged_breakpoint;
15268 ops->print_one_detail = print_one_detail_ranged_breakpoint;
15269 ops->print_mention = print_mention_ranged_breakpoint;
15270 ops->print_recreate = print_recreate_ranged_breakpoint;
15271
15272 /* Internal breakpoints. */
15273 ops = &internal_breakpoint_ops;
15274 *ops = bkpt_base_breakpoint_ops;
15275 ops->re_set = internal_bkpt_re_set;
15276 ops->check_status = internal_bkpt_check_status;
15277 ops->print_it = internal_bkpt_print_it;
15278 ops->print_mention = internal_bkpt_print_mention;
15279
15280 /* Momentary breakpoints. */
15281 ops = &momentary_breakpoint_ops;
15282 *ops = bkpt_base_breakpoint_ops;
15283 ops->re_set = momentary_bkpt_re_set;
15284 ops->check_status = momentary_bkpt_check_status;
15285 ops->print_it = momentary_bkpt_print_it;
15286 ops->print_mention = momentary_bkpt_print_mention;
15287
55aa24fb
SDJ
15288 /* Probe breakpoints. */
15289 ops = &bkpt_probe_breakpoint_ops;
15290 *ops = bkpt_breakpoint_ops;
15291 ops->insert_location = bkpt_probe_insert_location;
15292 ops->remove_location = bkpt_probe_remove_location;
5f700d83
KS
15293 ops->create_sals_from_location = bkpt_probe_create_sals_from_location;
15294 ops->decode_location = bkpt_probe_decode_location;
55aa24fb 15295
2060206e
PA
15296 /* Watchpoints. */
15297 ops = &watchpoint_breakpoint_ops;
15298 *ops = base_breakpoint_ops;
15299 ops->re_set = re_set_watchpoint;
15300 ops->insert_location = insert_watchpoint;
15301 ops->remove_location = remove_watchpoint;
15302 ops->breakpoint_hit = breakpoint_hit_watchpoint;
15303 ops->check_status = check_status_watchpoint;
15304 ops->resources_needed = resources_needed_watchpoint;
15305 ops->works_in_software_mode = works_in_software_mode_watchpoint;
15306 ops->print_it = print_it_watchpoint;
15307 ops->print_mention = print_mention_watchpoint;
15308 ops->print_recreate = print_recreate_watchpoint;
427cd150 15309 ops->explains_signal = explains_signal_watchpoint;
2060206e
PA
15310
15311 /* Masked watchpoints. */
15312 ops = &masked_watchpoint_breakpoint_ops;
15313 *ops = watchpoint_breakpoint_ops;
15314 ops->insert_location = insert_masked_watchpoint;
15315 ops->remove_location = remove_masked_watchpoint;
15316 ops->resources_needed = resources_needed_masked_watchpoint;
15317 ops->works_in_software_mode = works_in_software_mode_masked_watchpoint;
15318 ops->print_it = print_it_masked_watchpoint;
15319 ops->print_one_detail = print_one_detail_masked_watchpoint;
15320 ops->print_mention = print_mention_masked_watchpoint;
15321 ops->print_recreate = print_recreate_masked_watchpoint;
15322
15323 /* Tracepoints. */
15324 ops = &tracepoint_breakpoint_ops;
15325 *ops = base_breakpoint_ops;
15326 ops->re_set = tracepoint_re_set;
15327 ops->breakpoint_hit = tracepoint_breakpoint_hit;
15328 ops->print_one_detail = tracepoint_print_one_detail;
15329 ops->print_mention = tracepoint_print_mention;
15330 ops->print_recreate = tracepoint_print_recreate;
5f700d83 15331 ops->create_sals_from_location = tracepoint_create_sals_from_location;
983af33b 15332 ops->create_breakpoints_sal = tracepoint_create_breakpoints_sal;
5f700d83 15333 ops->decode_location = tracepoint_decode_location;
983af33b 15334
55aa24fb
SDJ
15335 /* Probe tracepoints. */
15336 ops = &tracepoint_probe_breakpoint_ops;
15337 *ops = tracepoint_breakpoint_ops;
5f700d83
KS
15338 ops->create_sals_from_location = tracepoint_probe_create_sals_from_location;
15339 ops->decode_location = tracepoint_probe_decode_location;
55aa24fb 15340
983af33b
SDJ
15341 /* Static tracepoints with marker (`-m'). */
15342 ops = &strace_marker_breakpoint_ops;
15343 *ops = tracepoint_breakpoint_ops;
5f700d83 15344 ops->create_sals_from_location = strace_marker_create_sals_from_location;
983af33b 15345 ops->create_breakpoints_sal = strace_marker_create_breakpoints_sal;
5f700d83 15346 ops->decode_location = strace_marker_decode_location;
2060206e
PA
15347
15348 /* Fork catchpoints. */
15349 ops = &catch_fork_breakpoint_ops;
15350 *ops = base_breakpoint_ops;
15351 ops->insert_location = insert_catch_fork;
15352 ops->remove_location = remove_catch_fork;
15353 ops->breakpoint_hit = breakpoint_hit_catch_fork;
15354 ops->print_it = print_it_catch_fork;
15355 ops->print_one = print_one_catch_fork;
15356 ops->print_mention = print_mention_catch_fork;
15357 ops->print_recreate = print_recreate_catch_fork;
15358
15359 /* Vfork catchpoints. */
15360 ops = &catch_vfork_breakpoint_ops;
15361 *ops = base_breakpoint_ops;
15362 ops->insert_location = insert_catch_vfork;
15363 ops->remove_location = remove_catch_vfork;
15364 ops->breakpoint_hit = breakpoint_hit_catch_vfork;
15365 ops->print_it = print_it_catch_vfork;
15366 ops->print_one = print_one_catch_vfork;
15367 ops->print_mention = print_mention_catch_vfork;
15368 ops->print_recreate = print_recreate_catch_vfork;
15369
15370 /* Exec catchpoints. */
15371 ops = &catch_exec_breakpoint_ops;
15372 *ops = base_breakpoint_ops;
2060206e
PA
15373 ops->insert_location = insert_catch_exec;
15374 ops->remove_location = remove_catch_exec;
15375 ops->breakpoint_hit = breakpoint_hit_catch_exec;
15376 ops->print_it = print_it_catch_exec;
15377 ops->print_one = print_one_catch_exec;
15378 ops->print_mention = print_mention_catch_exec;
15379 ops->print_recreate = print_recreate_catch_exec;
15380
edcc5120
TT
15381 /* Solib-related catchpoints. */
15382 ops = &catch_solib_breakpoint_ops;
15383 *ops = base_breakpoint_ops;
edcc5120
TT
15384 ops->insert_location = insert_catch_solib;
15385 ops->remove_location = remove_catch_solib;
15386 ops->breakpoint_hit = breakpoint_hit_catch_solib;
15387 ops->check_status = check_status_catch_solib;
15388 ops->print_it = print_it_catch_solib;
15389 ops->print_one = print_one_catch_solib;
15390 ops->print_mention = print_mention_catch_solib;
15391 ops->print_recreate = print_recreate_catch_solib;
e7e0cddf
SS
15392
15393 ops = &dprintf_breakpoint_ops;
15394 *ops = bkpt_base_breakpoint_ops;
5c2b4418 15395 ops->re_set = dprintf_re_set;
e7e0cddf
SS
15396 ops->resources_needed = bkpt_resources_needed;
15397 ops->print_it = bkpt_print_it;
15398 ops->print_mention = bkpt_print_mention;
2d9442cc 15399 ops->print_recreate = dprintf_print_recreate;
9d6e6e84 15400 ops->after_condition_true = dprintf_after_condition_true;
cd1608cc 15401 ops->breakpoint_hit = dprintf_breakpoint_hit;
2060206e
PA
15402}
15403
8bfd80db
YQ
15404/* Chain containing all defined "enable breakpoint" subcommands. */
15405
15406static struct cmd_list_element *enablebreaklist = NULL;
15407
8588b356
SM
15408/* See breakpoint.h. */
15409
15410cmd_list_element *commands_cmd_element = nullptr;
15411
c906108c 15412void
fba45db2 15413_initialize_breakpoint (void)
c906108c
SS
15414{
15415 struct cmd_list_element *c;
15416
2060206e
PA
15417 initialize_breakpoint_ops ();
15418
76727919
TT
15419 gdb::observers::solib_unloaded.attach (disable_breakpoints_in_unloaded_shlib);
15420 gdb::observers::free_objfile.attach (disable_breakpoints_in_freed_objfile);
15421 gdb::observers::memory_changed.attach (invalidate_bp_value_on_memory_change);
84acb35a 15422
c906108c
SS
15423 breakpoint_chain = 0;
15424 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
15425 before a breakpoint is set. */
15426 breakpoint_count = 0;
15427
1042e4c0
SS
15428 tracepoint_count = 0;
15429
1bedd215
AC
15430 add_com ("ignore", class_breakpoint, ignore_command, _("\
15431Set ignore-count of breakpoint number N to COUNT.\n\
15432Usage is `ignore N COUNT'."));
c906108c 15433
8588b356
SM
15434 commands_cmd_element = add_com ("commands", class_breakpoint,
15435 commands_command, _("\
18da0c51
MG
15436Set commands to be executed when the given breakpoints are hit.\n\
15437Give a space-separated breakpoint list as argument after \"commands\".\n\
15438A list element can be a breakpoint number (e.g. `5') or a range of numbers\n\
15439(e.g. `5-7').\n\
c906108c
SS
15440With no argument, the targeted breakpoint is the last one set.\n\
15441The commands themselves follow starting on the next line.\n\
15442Type a line containing \"end\" to indicate the end of them.\n\
15443Give \"silent\" as the first line to make the breakpoint silent;\n\
1bedd215 15444then no output is printed when it is hit, except what the commands print."));
c906108c 15445
d55637df 15446 c = add_com ("condition", class_breakpoint, condition_command, _("\
1bedd215 15447Specify breakpoint number N to break only if COND is true.\n\
c906108c 15448Usage is `condition N COND', where N is an integer and COND is an\n\
1bedd215 15449expression to be evaluated whenever breakpoint N is reached."));
d55637df 15450 set_cmd_completer (c, condition_completer);
c906108c 15451
1bedd215 15452 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
31e2b00f 15453Set a temporary breakpoint.\n\
c906108c
SS
15454Like \"break\" except the breakpoint is only temporary,\n\
15455so it will be deleted when hit. Equivalent to \"break\" followed\n\
31e2b00f
AS
15456by using \"enable delete\" on the breakpoint number.\n\
15457\n"
15458BREAK_ARGS_HELP ("tbreak")));
5ba2abeb 15459 set_cmd_completer (c, location_completer);
c94fdfd0 15460
1bedd215 15461 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
a3be7890 15462Set a hardware assisted breakpoint.\n\
c906108c 15463Like \"break\" except the breakpoint requires hardware support,\n\
31e2b00f
AS
15464some target hardware may not have this support.\n\
15465\n"
15466BREAK_ARGS_HELP ("hbreak")));
5ba2abeb 15467 set_cmd_completer (c, location_completer);
c906108c 15468
1bedd215 15469 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
31e2b00f 15470Set a temporary hardware assisted breakpoint.\n\
c906108c 15471Like \"hbreak\" except the breakpoint is only temporary,\n\
31e2b00f
AS
15472so it will be deleted when hit.\n\
15473\n"
15474BREAK_ARGS_HELP ("thbreak")));
5ba2abeb 15475 set_cmd_completer (c, location_completer);
c906108c 15476
1bedd215
AC
15477 add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
15478Enable some breakpoints.\n\
c906108c
SS
15479Give breakpoint numbers (separated by spaces) as arguments.\n\
15480With no subcommand, breakpoints are enabled until you command otherwise.\n\
15481This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 15482With a subcommand you can enable temporarily."),
c906108c 15483 &enablelist, "enable ", 1, &cmdlist);
c906108c
SS
15484
15485 add_com_alias ("en", "enable", class_breakpoint, 1);
15486
84951ab5 15487 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
1bedd215 15488Enable some breakpoints.\n\
c906108c
SS
15489Give breakpoint numbers (separated by spaces) as arguments.\n\
15490This is used to cancel the effect of the \"disable\" command.\n\
89549d7f 15491May be abbreviated to simply \"enable\"."),
c5aa993b 15492 &enablebreaklist, "enable breakpoints ", 1, &enablelist);
c906108c 15493
1a966eab
AC
15494 add_cmd ("once", no_class, enable_once_command, _("\
15495Enable breakpoints for one hit. Give breakpoint numbers.\n\
15496If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
c906108c
SS
15497 &enablebreaklist);
15498
1a966eab
AC
15499 add_cmd ("delete", no_class, enable_delete_command, _("\
15500Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
15501If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
15502 &enablebreaklist);
15503
816338b5
SS
15504 add_cmd ("count", no_class, enable_count_command, _("\
15505Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
15506If a breakpoint is hit while enabled in this fashion,\n\
15507the count is decremented; when it reaches zero, the breakpoint is disabled."),
15508 &enablebreaklist);
15509
1a966eab
AC
15510 add_cmd ("delete", no_class, enable_delete_command, _("\
15511Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
15512If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
15513 &enablelist);
15514
1a966eab
AC
15515 add_cmd ("once", no_class, enable_once_command, _("\
15516Enable breakpoints for one hit. Give breakpoint numbers.\n\
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, _("\
15521Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
15522If a breakpoint is hit while enabled in this fashion,\n\
15523the count is decremented; when it reaches zero, the breakpoint is disabled."),
c906108c
SS
15524 &enablelist);
15525
1bedd215
AC
15526 add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
15527Disable some breakpoints.\n\
c906108c
SS
15528Arguments are breakpoint numbers with spaces in between.\n\
15529To disable all breakpoints, give no argument.\n\
64b9b334 15530A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
c906108c
SS
15531 &disablelist, "disable ", 1, &cmdlist);
15532 add_com_alias ("dis", "disable", class_breakpoint, 1);
15533 add_com_alias ("disa", "disable", class_breakpoint, 1);
c906108c 15534
1a966eab
AC
15535 add_cmd ("breakpoints", class_alias, disable_command, _("\
15536Disable some breakpoints.\n\
c906108c
SS
15537Arguments are breakpoint numbers with spaces in between.\n\
15538To disable all breakpoints, give no argument.\n\
64b9b334 15539A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
1a966eab 15540This command may be abbreviated \"disable\"."),
c906108c
SS
15541 &disablelist);
15542
1bedd215
AC
15543 add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
15544Delete some breakpoints or auto-display expressions.\n\
c906108c
SS
15545Arguments are breakpoint numbers with spaces in between.\n\
15546To delete all breakpoints, give no argument.\n\
15547\n\
15548Also a prefix command for deletion of other GDB objects.\n\
1bedd215 15549The \"unset\" command is also an alias for \"delete\"."),
c906108c
SS
15550 &deletelist, "delete ", 1, &cmdlist);
15551 add_com_alias ("d", "delete", class_breakpoint, 1);
7f198e01 15552 add_com_alias ("del", "delete", class_breakpoint, 1);
c906108c 15553
1a966eab
AC
15554 add_cmd ("breakpoints", class_alias, delete_command, _("\
15555Delete some breakpoints or auto-display expressions.\n\
c906108c
SS
15556Arguments are breakpoint numbers with spaces in between.\n\
15557To delete all breakpoints, give no argument.\n\
1a966eab 15558This command may be abbreviated \"delete\"."),
c906108c
SS
15559 &deletelist);
15560
1bedd215 15561 add_com ("clear", class_breakpoint, clear_command, _("\
629500fa
KS
15562Clear breakpoint at specified location.\n\
15563Argument may be a linespec, explicit, or address location as described below.\n\
1bedd215
AC
15564\n\
15565With no argument, clears all breakpoints in the line that the selected frame\n\
629500fa 15566is executing in.\n"
89549d7f 15567"\n" LOCATION_HELP_STRING "\n\n\
1bedd215 15568See also the \"delete\" command which clears breakpoints by number."));
a6cc4789 15569 add_com_alias ("cl", "clear", class_breakpoint, 1);
c906108c 15570
1bedd215 15571 c = add_com ("break", class_breakpoint, break_command, _("\
629500fa 15572Set breakpoint at specified location.\n"
31e2b00f 15573BREAK_ARGS_HELP ("break")));
5ba2abeb 15574 set_cmd_completer (c, location_completer);
c94fdfd0 15575
c906108c
SS
15576 add_com_alias ("b", "break", class_run, 1);
15577 add_com_alias ("br", "break", class_run, 1);
15578 add_com_alias ("bre", "break", class_run, 1);
15579 add_com_alias ("brea", "break", class_run, 1);
15580
c906108c
SS
15581 if (dbx_commands)
15582 {
1bedd215
AC
15583 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
15584Break in function/address or break at a line in the current file."),
c5aa993b
JM
15585 &stoplist, "stop ", 1, &cmdlist);
15586 add_cmd ("in", class_breakpoint, stopin_command,
1a966eab 15587 _("Break in function or address."), &stoplist);
c5aa993b 15588 add_cmd ("at", class_breakpoint, stopat_command,
1a966eab 15589 _("Break at a line in the current file."), &stoplist);
11db9430 15590 add_com ("status", class_info, info_breakpoints_command, _("\
1bedd215 15591Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
15592The \"Type\" column indicates one of:\n\
15593\tbreakpoint - normal breakpoint\n\
15594\twatchpoint - watchpoint\n\
15595The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15596the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15597breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
15598address and file/line number respectively.\n\
15599\n\
15600Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
15601are set to the address of the last breakpoint listed unless the command\n\
15602is prefixed with \"server \".\n\n\
c906108c 15603Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 15604breakpoint set."));
c906108c
SS
15605 }
15606
11db9430 15607 add_info ("breakpoints", info_breakpoints_command, _("\
e5a67952 15608Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
c906108c
SS
15609The \"Type\" column indicates one of:\n\
15610\tbreakpoint - normal breakpoint\n\
15611\twatchpoint - watchpoint\n\
15612The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15613the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15614breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
15615address and file/line number respectively.\n\
15616\n\
15617Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
15618are set to the address of the last breakpoint listed unless the command\n\
15619is prefixed with \"server \".\n\n\
c906108c 15620Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 15621breakpoint set."));
c906108c 15622
6b04bdb7
MS
15623 add_info_alias ("b", "breakpoints", 1);
15624
1a966eab
AC
15625 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
15626Status of all breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
15627The \"Type\" column indicates one of:\n\
15628\tbreakpoint - normal breakpoint\n\
15629\twatchpoint - watchpoint\n\
15630\tlongjmp - internal breakpoint used to step through longjmp()\n\
15631\tlongjmp resume - internal breakpoint at the target of longjmp()\n\
15632\tuntil - internal breakpoint used by the \"until\" command\n\
1a966eab
AC
15633\tfinish - internal breakpoint used by the \"finish\" command\n\
15634The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
c906108c
SS
15635the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15636breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1a966eab
AC
15637address and file/line number respectively.\n\
15638\n\
15639Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
15640are set to the address of the last breakpoint listed unless the command\n\
15641is prefixed with \"server \".\n\n\
c906108c 15642Convenience variable \"$bpnum\" contains the number of the last\n\
1a966eab 15643breakpoint set."),
c906108c
SS
15644 &maintenanceinfolist);
15645
44feb3ce
TT
15646 add_prefix_cmd ("catch", class_breakpoint, catch_command, _("\
15647Set catchpoints to catch events."),
15648 &catch_cmdlist, "catch ",
15649 0/*allow-unknown*/, &cmdlist);
15650
15651 add_prefix_cmd ("tcatch", class_breakpoint, tcatch_command, _("\
15652Set temporary catchpoints to catch events."),
15653 &tcatch_cmdlist, "tcatch ",
15654 0/*allow-unknown*/, &cmdlist);
15655
44feb3ce
TT
15656 add_catch_command ("fork", _("Catch calls to fork."),
15657 catch_fork_command_1,
a96d9b2e 15658 NULL,
44feb3ce
TT
15659 (void *) (uintptr_t) catch_fork_permanent,
15660 (void *) (uintptr_t) catch_fork_temporary);
15661 add_catch_command ("vfork", _("Catch calls to vfork."),
15662 catch_fork_command_1,
a96d9b2e 15663 NULL,
44feb3ce
TT
15664 (void *) (uintptr_t) catch_vfork_permanent,
15665 (void *) (uintptr_t) catch_vfork_temporary);
15666 add_catch_command ("exec", _("Catch calls to exec."),
15667 catch_exec_command_1,
a96d9b2e
SDJ
15668 NULL,
15669 CATCH_PERMANENT,
15670 CATCH_TEMPORARY);
edcc5120
TT
15671 add_catch_command ("load", _("Catch loads of shared libraries.\n\
15672Usage: catch load [REGEX]\n\
15673If REGEX is given, only stop for libraries matching the regular expression."),
15674 catch_load_command_1,
15675 NULL,
15676 CATCH_PERMANENT,
15677 CATCH_TEMPORARY);
15678 add_catch_command ("unload", _("Catch unloads of shared libraries.\n\
15679Usage: catch unload [REGEX]\n\
15680If REGEX is given, only stop for libraries matching the regular expression."),
15681 catch_unload_command_1,
15682 NULL,
15683 CATCH_PERMANENT,
15684 CATCH_TEMPORARY);
c5aa993b 15685
1bedd215
AC
15686 c = add_com ("watch", class_breakpoint, watch_command, _("\
15687Set a watchpoint for an expression.\n\
06a64a0b 15688Usage: watch [-l|-location] EXPRESSION\n\
c906108c 15689A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
15690an expression changes.\n\
15691If -l or -location is given, this evaluates EXPRESSION and watches\n\
15692the memory to which it refers."));
65d12d83 15693 set_cmd_completer (c, expression_completer);
c906108c 15694
1bedd215
AC
15695 c = add_com ("rwatch", class_breakpoint, rwatch_command, _("\
15696Set a read watchpoint for an expression.\n\
06a64a0b 15697Usage: rwatch [-l|-location] EXPRESSION\n\
c906108c 15698A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
15699an expression is read.\n\
15700If -l or -location is given, this evaluates EXPRESSION and watches\n\
15701the memory to which it refers."));
65d12d83 15702 set_cmd_completer (c, expression_completer);
c906108c 15703
1bedd215
AC
15704 c = add_com ("awatch", class_breakpoint, awatch_command, _("\
15705Set a watchpoint for an expression.\n\
06a64a0b 15706Usage: awatch [-l|-location] EXPRESSION\n\
c906108c 15707A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
15708an expression is either read or written.\n\
15709If -l or -location is given, this evaluates EXPRESSION and watches\n\
15710the memory to which it refers."));
65d12d83 15711 set_cmd_completer (c, expression_completer);
c906108c 15712
11db9430 15713 add_info ("watchpoints", info_watchpoints_command, _("\
e5a67952 15714Status of specified watchpoints (all watchpoints if no argument)."));
c906108c 15715
920d2a44
AC
15716 /* XXX: cagney/2005-02-23: This should be a boolean, and should
15717 respond to changes - contrary to the description. */
85c07804
AC
15718 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
15719 &can_use_hw_watchpoints, _("\
15720Set debugger's willingness to use watchpoint hardware."), _("\
15721Show debugger's willingness to use watchpoint hardware."), _("\
c906108c
SS
15722If zero, gdb will not use hardware for new watchpoints, even if\n\
15723such is available. (However, any hardware watchpoints that were\n\
15724created before setting this to nonzero, will continue to use watchpoint\n\
85c07804
AC
15725hardware.)"),
15726 NULL,
920d2a44 15727 show_can_use_hw_watchpoints,
85c07804 15728 &setlist, &showlist);
c906108c
SS
15729
15730 can_use_hw_watchpoints = 1;
fa8d40ab 15731
1042e4c0
SS
15732 /* Tracepoint manipulation commands. */
15733
15734 c = add_com ("trace", class_breakpoint, trace_command, _("\
629500fa 15735Set a tracepoint at specified location.\n\
1042e4c0
SS
15736\n"
15737BREAK_ARGS_HELP ("trace") "\n\
15738Do \"help tracepoints\" for info on other tracepoint commands."));
15739 set_cmd_completer (c, location_completer);
15740
15741 add_com_alias ("tp", "trace", class_alias, 0);
15742 add_com_alias ("tr", "trace", class_alias, 1);
15743 add_com_alias ("tra", "trace", class_alias, 1);
15744 add_com_alias ("trac", "trace", class_alias, 1);
15745
7a697b8d 15746 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
629500fa 15747Set a fast tracepoint at specified location.\n\
7a697b8d
SS
15748\n"
15749BREAK_ARGS_HELP ("ftrace") "\n\
15750Do \"help tracepoints\" for info on other tracepoint commands."));
15751 set_cmd_completer (c, location_completer);
15752
0fb4aa4b 15753 c = add_com ("strace", class_breakpoint, strace_command, _("\
629500fa 15754Set a static tracepoint at location or marker.\n\
0fb4aa4b
PA
15755\n\
15756strace [LOCATION] [if CONDITION]\n\
629500fa
KS
15757LOCATION may be a linespec, explicit, or address location (described below) \n\
15758or -m MARKER_ID.\n\n\
15759If a marker id is specified, probe the marker with that name. With\n\
15760no LOCATION, uses current execution address of the selected stack frame.\n\
0fb4aa4b
PA
15761Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
15762This collects arbitrary user data passed in the probe point call to the\n\
15763tracing library. You can inspect it when analyzing the trace buffer,\n\
15764by printing the $_sdata variable like any other convenience variable.\n\
15765\n\
15766CONDITION is a boolean expression.\n\
89549d7f 15767\n" LOCATION_HELP_STRING "\n\n\
d41c0fc8
PA
15768Multiple tracepoints at one place are permitted, and useful if their\n\
15769conditions are different.\n\
0fb4aa4b
PA
15770\n\
15771Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
15772Do \"help tracepoints\" for info on other tracepoint commands."));
15773 set_cmd_completer (c, location_completer);
15774
11db9430 15775 add_info ("tracepoints", info_tracepoints_command, _("\
e5a67952 15776Status of specified tracepoints (all tracepoints if no argument).\n\
1042e4c0
SS
15777Convenience variable \"$tpnum\" contains the number of the\n\
15778last tracepoint set."));
15779
15780 add_info_alias ("tp", "tracepoints", 1);
15781
15782 add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
15783Delete specified tracepoints.\n\
15784Arguments are tracepoint numbers, separated by spaces.\n\
15785No argument means delete all tracepoints."),
15786 &deletelist);
7e20dfcd 15787 add_alias_cmd ("tr", "tracepoints", class_trace, 1, &deletelist);
1042e4c0
SS
15788
15789 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
15790Disable specified tracepoints.\n\
15791Arguments are tracepoint numbers, separated by spaces.\n\
15792No argument means disable all tracepoints."),
15793 &disablelist);
15794 deprecate_cmd (c, "disable");
15795
15796 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
15797Enable specified tracepoints.\n\
15798Arguments are tracepoint numbers, separated by spaces.\n\
15799No argument means enable all tracepoints."),
15800 &enablelist);
15801 deprecate_cmd (c, "enable");
15802
15803 add_com ("passcount", class_trace, trace_pass_command, _("\
15804Set the passcount for a tracepoint.\n\
15805The trace will end when the tracepoint has been passed 'count' times.\n\
15806Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
15807if TPNUM is omitted, passcount refers to the last tracepoint defined."));
15808
6149aea9
PA
15809 add_prefix_cmd ("save", class_breakpoint, save_command,
15810 _("Save breakpoint definitions as a script."),
15811 &save_cmdlist, "save ",
15812 0/*allow-unknown*/, &cmdlist);
15813
15814 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
15815Save current breakpoint definitions as a script.\n\
cce7e648 15816This includes all types of breakpoints (breakpoints, watchpoints,\n\
6149aea9
PA
15817catchpoints, tracepoints). Use the 'source' command in another debug\n\
15818session to restore them."),
15819 &save_cmdlist);
15820 set_cmd_completer (c, filename_completer);
15821
15822 c = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
1042e4c0 15823Save current tracepoint definitions as a script.\n\
6149aea9
PA
15824Use the 'source' command in another debug session to restore them."),
15825 &save_cmdlist);
1042e4c0
SS
15826 set_cmd_completer (c, filename_completer);
15827
6149aea9
PA
15828 c = add_com_alias ("save-tracepoints", "save tracepoints", class_trace, 0);
15829 deprecate_cmd (c, "save tracepoints");
15830
1bedd215 15831 add_prefix_cmd ("breakpoint", class_maintenance, set_breakpoint_cmd, _("\
fa8d40ab
JJ
15832Breakpoint specific settings\n\
15833Configure various breakpoint-specific variables such as\n\
1bedd215 15834pending breakpoint behavior"),
fa8d40ab
JJ
15835 &breakpoint_set_cmdlist, "set breakpoint ",
15836 0/*allow-unknown*/, &setlist);
1bedd215 15837 add_prefix_cmd ("breakpoint", class_maintenance, show_breakpoint_cmd, _("\
fa8d40ab
JJ
15838Breakpoint specific settings\n\
15839Configure various breakpoint-specific variables such as\n\
1bedd215 15840pending breakpoint behavior"),
fa8d40ab
JJ
15841 &breakpoint_show_cmdlist, "show breakpoint ",
15842 0/*allow-unknown*/, &showlist);
15843
7915a72c
AC
15844 add_setshow_auto_boolean_cmd ("pending", no_class,
15845 &pending_break_support, _("\
15846Set debugger's behavior regarding pending breakpoints."), _("\
15847Show debugger's behavior regarding pending breakpoints."), _("\
6e1d7d6c
AC
15848If on, an unrecognized breakpoint location will cause gdb to create a\n\
15849pending breakpoint. If off, an unrecognized breakpoint location results in\n\
15850an error. If auto, an unrecognized breakpoint location results in a\n\
7915a72c 15851user-query to see if a pending breakpoint should be created."),
2c5b56ce 15852 NULL,
920d2a44 15853 show_pending_break_support,
6e1d7d6c
AC
15854 &breakpoint_set_cmdlist,
15855 &breakpoint_show_cmdlist);
fa8d40ab
JJ
15856
15857 pending_break_support = AUTO_BOOLEAN_AUTO;
765dc015
VP
15858
15859 add_setshow_boolean_cmd ("auto-hw", no_class,
15860 &automatic_hardware_breakpoints, _("\
15861Set automatic usage of hardware breakpoints."), _("\
15862Show automatic usage of hardware breakpoints."), _("\
15863If set, the debugger will automatically use hardware breakpoints for\n\
15864breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
15865a warning will be emitted for such breakpoints."),
15866 NULL,
15867 show_automatic_hardware_breakpoints,
15868 &breakpoint_set_cmdlist,
15869 &breakpoint_show_cmdlist);
74960c60 15870
a25a5a45
PA
15871 add_setshow_boolean_cmd ("always-inserted", class_support,
15872 &always_inserted_mode, _("\
74960c60
VP
15873Set mode for inserting breakpoints."), _("\
15874Show mode for inserting breakpoints."), _("\
a25a5a45
PA
15875When this mode is on, breakpoints are inserted immediately as soon as\n\
15876they're created, kept inserted even when execution stops, and removed\n\
15877only when the user deletes them. When this mode is off (the default),\n\
15878breakpoints are inserted only when execution continues, and removed\n\
15879when execution stops."),
72d0e2c5
YQ
15880 NULL,
15881 &show_always_inserted_mode,
15882 &breakpoint_set_cmdlist,
15883 &breakpoint_show_cmdlist);
f1310107 15884
b775012e
LM
15885 add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
15886 condition_evaluation_enums,
15887 &condition_evaluation_mode_1, _("\
15888Set mode of breakpoint condition evaluation."), _("\
15889Show mode of breakpoint condition evaluation."), _("\
d1cda5d9 15890When this is set to \"host\", breakpoint conditions will be\n\
b775012e
LM
15891evaluated on the host's side by GDB. When it is set to \"target\",\n\
15892breakpoint conditions will be downloaded to the target (if the target\n\
15893supports such feature) and conditions will be evaluated on the target's side.\n\
15894If this is set to \"auto\" (default), this will be automatically set to\n\
15895\"target\" if it supports condition evaluation, otherwise it will\n\
15896be set to \"gdb\""),
15897 &set_condition_evaluation_mode,
15898 &show_condition_evaluation_mode,
15899 &breakpoint_set_cmdlist,
15900 &breakpoint_show_cmdlist);
15901
f1310107
TJB
15902 add_com ("break-range", class_breakpoint, break_range_command, _("\
15903Set a breakpoint for an address range.\n\
15904break-range START-LOCATION, END-LOCATION\n\
15905where START-LOCATION and END-LOCATION can be one of the following:\n\
15906 LINENUM, for that line in the current file,\n\
15907 FILE:LINENUM, for that line in that file,\n\
15908 +OFFSET, for that number of lines after the current line\n\
15909 or the start of the range\n\
15910 FUNCTION, for the first line in that function,\n\
15911 FILE:FUNCTION, to distinguish among like-named static functions.\n\
15912 *ADDRESS, for the instruction at that address.\n\
15913\n\
15914The breakpoint will stop execution of the inferior whenever it executes\n\
15915an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
15916range (including START-LOCATION and END-LOCATION)."));
15917
e7e0cddf 15918 c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
629500fa 15919Set a dynamic printf at specified location.\n\
e7e0cddf 15920dprintf location,format string,arg1,arg2,...\n\
629500fa
KS
15921location may be a linespec, explicit, or address location.\n"
15922"\n" LOCATION_HELP_STRING));
e7e0cddf
SS
15923 set_cmd_completer (c, location_completer);
15924
15925 add_setshow_enum_cmd ("dprintf-style", class_support,
15926 dprintf_style_enums, &dprintf_style, _("\
15927Set the style of usage for dynamic printf."), _("\
15928Show the style of usage for dynamic printf."), _("\
15929This setting chooses how GDB will do a dynamic printf.\n\
15930If the value is \"gdb\", then the printing is done by GDB to its own\n\
15931console, as with the \"printf\" command.\n\
15932If the value is \"call\", the print is done by calling a function in your\n\
15933program; by default printf(), but you can choose a different function or\n\
15934output stream by setting dprintf-function and dprintf-channel."),
15935 update_dprintf_commands, NULL,
15936 &setlist, &showlist);
15937
15938 dprintf_function = xstrdup ("printf");
15939 add_setshow_string_cmd ("dprintf-function", class_support,
15940 &dprintf_function, _("\
15941Set the function to use for dynamic printf"), _("\
15942Show the function to use for dynamic printf"), NULL,
15943 update_dprintf_commands, NULL,
15944 &setlist, &showlist);
15945
15946 dprintf_channel = xstrdup ("");
15947 add_setshow_string_cmd ("dprintf-channel", class_support,
15948 &dprintf_channel, _("\
15949Set the channel to use for dynamic printf"), _("\
15950Show the channel to use for dynamic printf"), NULL,
15951 update_dprintf_commands, NULL,
15952 &setlist, &showlist);
15953
d3ce09f5
SS
15954 add_setshow_boolean_cmd ("disconnected-dprintf", no_class,
15955 &disconnected_dprintf, _("\
15956Set whether dprintf continues after GDB disconnects."), _("\
15957Show whether dprintf continues after GDB disconnects."), _("\
15958Use this to let dprintf commands continue to hit and produce output\n\
15959even if GDB disconnects or detaches from the target."),
15960 NULL,
15961 NULL,
15962 &setlist, &showlist);
15963
15964 add_com ("agent-printf", class_vars, agent_printf_command, _("\
15965agent-printf \"printf format string\", arg1, arg2, arg3, ..., argn\n\
15966(target agent only) This is useful for formatted output in user-defined commands."));
15967
765dc015 15968 automatic_hardware_breakpoints = 1;
f3b1572e 15969
76727919
TT
15970 gdb::observers::about_to_proceed.attach (breakpoint_about_to_proceed);
15971 gdb::observers::thread_exit.attach (remove_threaded_breakpoints);
c906108c 15972}