]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/breakpoint.c
Support parallel make check with GNU make 4.2+
[thirdparty/binutils-gdb.git] / gdb / breakpoint.c
CommitLineData
c906108c 1/* Everything about breakpoints, for GDB.
8926118c 2
e2882c85 3 Copyright (C) 1986-2018 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"
a6d9a66e 21#include "arch-utils.h"
c906108c 22#include <ctype.h>
776592bf 23#include "hashtab.h"
c906108c
SS
24#include "symtab.h"
25#include "frame.h"
26#include "breakpoint.h"
1042e4c0 27#include "tracepoint.h"
c906108c
SS
28#include "gdbtypes.h"
29#include "expression.h"
30#include "gdbcore.h"
31#include "gdbcmd.h"
32#include "value.h"
33#include "command.h"
34#include "inferior.h"
45741a9c 35#include "infrun.h"
c906108c
SS
36#include "gdbthread.h"
37#include "target.h"
38#include "language.h"
50f182aa 39#include "gdb-demangle.h"
0ba1096a 40#include "filenames.h"
c906108c
SS
41#include "annotate.h"
42#include "symfile.h"
43#include "objfiles.h"
0378c332 44#include "source.h"
c5f0f3d0 45#include "linespec.h"
c94fdfd0 46#include "completer.h"
8b93c638 47#include "ui-out.h"
e1507482 48#include "cli/cli-script.h"
fe898f56 49#include "block.h"
a77053c2 50#include "solib.h"
84acb35a 51#include "solist.h"
76727919 52#include "observable.h"
765dc015 53#include "memattr.h"
f7f9143b 54#include "ada-lang.h"
d1aa2f50 55#include "top.h"
79a45b7d 56#include "valprint.h"
4efc6507 57#include "jit.h"
65d79d4b 58#include "parser-defs.h"
55aa24fb
SDJ
59#include "gdb_regex.h"
60#include "probe.h"
e9cafbcc 61#include "cli/cli-utils.h"
be34f849 62#include "continuations.h"
1bfeeb0f
JL
63#include "stack.h"
64#include "skip.h"
b775012e 65#include "ax-gdb.h"
e2e4d78b 66#include "dummy-frame.h"
5589af0e 67#include "interps.h"
d3ce09f5 68#include "format.h"
cfc31633 69#include "thread-fsm.h"
5d5658a1 70#include "tid-parse.h"
d3ce09f5 71
1042e4c0
SS
72/* readline include files */
73#include "readline/readline.h"
74#include "readline/history.h"
75
76/* readline defines this. */
77#undef savestring
78
034dad6f 79#include "mi/mi-common.h"
6dddc817 80#include "extension.h"
325fac50 81#include <algorithm>
5ed8105e 82#include "progspace-and-thread.h"
6c5b2ebe 83#include "common/array-view.h"
76f9c9cf 84#include "common/gdb_optional.h"
104c1213 85
e7e8980f
YQ
86/* Enums for exception-handling support. */
87enum exception_event_kind
88{
89 EX_EVENT_THROW,
591f19e8 90 EX_EVENT_RETHROW,
e7e8980f
YQ
91 EX_EVENT_CATCH
92};
93
4a64f543 94/* Prototypes for local functions. */
c906108c 95
896b6bda 96static void map_breakpoint_numbers (const char *,
48649e1b 97 gdb::function_view<void (breakpoint *)>);
c906108c 98
348d480f
PA
99static void breakpoint_re_set_default (struct breakpoint *);
100
f00aae0f
KS
101static void
102 create_sals_from_location_default (const struct event_location *location,
103 struct linespec_result *canonical,
104 enum bptype type_wanted);
983af33b
SDJ
105
106static void create_breakpoints_sal_default (struct gdbarch *,
107 struct linespec_result *,
e1e01040
PA
108 gdb::unique_xmalloc_ptr<char>,
109 gdb::unique_xmalloc_ptr<char>,
110 enum bptype,
983af33b
SDJ
111 enum bpdisp, int, int,
112 int,
113 const struct breakpoint_ops *,
44f238bb 114 int, int, int, unsigned);
983af33b 115
6c5b2ebe
PA
116static std::vector<symtab_and_line> decode_location_default
117 (struct breakpoint *b, const struct event_location *location,
118 struct program_space *search_pspace);
983af33b 119
a6535de1
TT
120static int can_use_hardware_watchpoint
121 (const std::vector<value_ref_ptr> &vals);
c906108c 122
a14ed312 123static void mention (struct breakpoint *);
c906108c 124
348d480f
PA
125static struct breakpoint *set_raw_breakpoint_without_location (struct gdbarch *,
126 enum bptype,
c0a91b2b 127 const struct breakpoint_ops *);
3742cc8b
YQ
128static struct bp_location *add_location_to_breakpoint (struct breakpoint *,
129 const struct symtab_and_line *);
130
4a64f543
MS
131/* This function is used in gdbtk sources and thus can not be made
132 static. */
63c252f8 133struct breakpoint *set_raw_breakpoint (struct gdbarch *gdbarch,
348d480f 134 struct symtab_and_line,
c0a91b2b
TT
135 enum bptype,
136 const struct breakpoint_ops *);
c906108c 137
06edf0c0
PA
138static struct breakpoint *
139 momentary_breakpoint_from_master (struct breakpoint *orig,
140 enum bptype type,
a1aa2221
LM
141 const struct breakpoint_ops *ops,
142 int loc_enabled);
06edf0c0 143
76897487
KB
144static void breakpoint_adjustment_warning (CORE_ADDR, CORE_ADDR, int, int);
145
a6d9a66e
UW
146static CORE_ADDR adjust_breakpoint_address (struct gdbarch *gdbarch,
147 CORE_ADDR bpaddr,
88f7da05 148 enum bptype bptype);
76897487 149
6c95b8df
PA
150static void describe_other_breakpoints (struct gdbarch *,
151 struct program_space *, CORE_ADDR,
5af949e3 152 struct obj_section *, int);
c906108c 153
85d721b8
PA
154static int watchpoint_locations_match (struct bp_location *loc1,
155 struct bp_location *loc2);
156
f1310107 157static int breakpoint_location_address_match (struct bp_location *bl,
accd0bcd 158 const struct address_space *aspace,
f1310107
TJB
159 CORE_ADDR addr);
160
d35ae833 161static int breakpoint_location_address_range_overlap (struct bp_location *,
accd0bcd 162 const address_space *,
d35ae833
PA
163 CORE_ADDR, int);
164
834c0d03 165static int remove_breakpoint (struct bp_location *);
b2b6a7da 166static int remove_breakpoint_1 (struct bp_location *, enum remove_bp_reason);
c906108c 167
e514a9d6 168static enum print_stop_action print_bp_stop_message (bpstat bs);
c906108c 169
a14ed312 170static int hw_breakpoint_used_count (void);
c906108c 171
a1398e0c
PA
172static int hw_watchpoint_use_count (struct breakpoint *);
173
174static int hw_watchpoint_used_count_others (struct breakpoint *except,
175 enum bptype type,
176 int *other_type_used);
c906108c 177
816338b5
SS
178static void enable_breakpoint_disp (struct breakpoint *, enum bpdisp,
179 int count);
c906108c 180
fe3f5fa8 181static void free_bp_location (struct bp_location *loc);
f431efe5
PA
182static void incref_bp_location (struct bp_location *loc);
183static void decref_bp_location (struct bp_location **loc);
fe3f5fa8 184
39d61571 185static struct bp_location *allocate_bp_location (struct breakpoint *bpt);
a5606eee 186
44702360
PA
187/* update_global_location_list's modes of operation wrt to whether to
188 insert locations now. */
189enum ugll_insert_mode
190{
191 /* Don't insert any breakpoint locations into the inferior, only
192 remove already-inserted locations that no longer should be
193 inserted. Functions that delete a breakpoint or breakpoints
194 should specify this mode, so that deleting a breakpoint doesn't
195 have the side effect of inserting the locations of other
196 breakpoints that are marked not-inserted, but should_be_inserted
197 returns true on them.
198
199 This behavior is useful is situations close to tear-down -- e.g.,
200 after an exec, while the target still has execution, but
201 breakpoint shadows of the previous executable image should *NOT*
202 be restored to the new image; or before detaching, where the
203 target still has execution and wants to delete breakpoints from
204 GDB's lists, and all breakpoints had already been removed from
205 the inferior. */
206 UGLL_DONT_INSERT,
207
a25a5a45
PA
208 /* May insert breakpoints iff breakpoints_should_be_inserted_now
209 claims breakpoints should be inserted now. */
04086b45
PA
210 UGLL_MAY_INSERT,
211
a25a5a45
PA
212 /* Insert locations now, irrespective of
213 breakpoints_should_be_inserted_now. E.g., say all threads are
214 stopped right now, and the user did "continue". We need to
215 insert breakpoints _before_ resuming the target, but
216 UGLL_MAY_INSERT wouldn't insert them, because
217 breakpoints_should_be_inserted_now returns false at that point,
218 as no thread is running yet. */
04086b45 219 UGLL_INSERT
44702360
PA
220};
221
222static void update_global_location_list (enum ugll_insert_mode);
a5606eee 223
44702360 224static void update_global_location_list_nothrow (enum ugll_insert_mode);
74960c60 225
d77f58be 226static int is_hardware_watchpoint (const struct breakpoint *bpt);
74960c60
VP
227
228static void insert_breakpoint_locations (void);
a5606eee 229
0b39b52e 230static void trace_pass_command (const char *, int);
1042e4c0 231
558a9d82
YQ
232static void set_tracepoint_count (int num);
233
9c06b0b4
TJB
234static int is_masked_watchpoint (const struct breakpoint *b);
235
b775012e
LM
236static struct bp_location **get_first_locp_gte_addr (CORE_ADDR address);
237
983af33b
SDJ
238/* Return 1 if B refers to a static tracepoint set by marker ("-m"), zero
239 otherwise. */
240
241static int strace_marker_p (struct breakpoint *b);
0fb4aa4b 242
2060206e
PA
243/* The breakpoint_ops structure to be inherited by all breakpoint_ops
244 that are implemented on top of software or hardware breakpoints
245 (user breakpoints, internal and momentary breakpoints, etc.). */
246static struct breakpoint_ops bkpt_base_breakpoint_ops;
247
248/* Internal breakpoints class type. */
06edf0c0 249static struct breakpoint_ops internal_breakpoint_ops;
2060206e
PA
250
251/* Momentary breakpoints class type. */
06edf0c0
PA
252static struct breakpoint_ops momentary_breakpoint_ops;
253
2060206e
PA
254/* The breakpoint_ops structure to be used in regular user created
255 breakpoints. */
256struct breakpoint_ops bkpt_breakpoint_ops;
257
55aa24fb
SDJ
258/* Breakpoints set on probes. */
259static struct breakpoint_ops bkpt_probe_breakpoint_ops;
260
e7e0cddf 261/* Dynamic printf class type. */
c5867ab6 262struct breakpoint_ops dprintf_breakpoint_ops;
e7e0cddf 263
d3ce09f5
SS
264/* The style in which to perform a dynamic printf. This is a user
265 option because different output options have different tradeoffs;
266 if GDB does the printing, there is better error handling if there
267 is a problem with any of the arguments, but using an inferior
268 function lets you have special-purpose printers and sending of
269 output to the same place as compiled-in print functions. */
270
271static const char dprintf_style_gdb[] = "gdb";
272static const char dprintf_style_call[] = "call";
273static const char dprintf_style_agent[] = "agent";
274static const char *const dprintf_style_enums[] = {
275 dprintf_style_gdb,
276 dprintf_style_call,
277 dprintf_style_agent,
278 NULL
279};
280static const char *dprintf_style = dprintf_style_gdb;
281
282/* The function to use for dynamic printf if the preferred style is to
283 call into the inferior. The value is simply a string that is
284 copied into the command, so it can be anything that GDB can
285 evaluate to a callable address, not necessarily a function name. */
286
bde6261a 287static char *dprintf_function;
d3ce09f5
SS
288
289/* The channel to use for dynamic printf if the preferred style is to
290 call into the inferior; if a nonempty string, it will be passed to
291 the call as the first argument, with the format string as the
292 second. As with the dprintf function, this can be anything that
293 GDB knows how to evaluate, so in addition to common choices like
294 "stderr", this could be an app-specific expression like
295 "mystreams[curlogger]". */
296
bde6261a 297static char *dprintf_channel;
d3ce09f5
SS
298
299/* True if dprintf commands should continue to operate even if GDB
300 has disconnected. */
301static int disconnected_dprintf = 1;
302
5cea2a26
PA
303struct command_line *
304breakpoint_commands (struct breakpoint *b)
305{
d1b0a7bf 306 return b->commands ? b->commands.get () : NULL;
5cea2a26 307}
3daf8fe5 308
f3b1572e
PA
309/* Flag indicating that a command has proceeded the inferior past the
310 current breakpoint. */
311
312static int breakpoint_proceeded;
313
956a9fb9 314const char *
2cec12e5
AR
315bpdisp_text (enum bpdisp disp)
316{
4a64f543
MS
317 /* NOTE: the following values are a part of MI protocol and
318 represent values of 'disp' field returned when inferior stops at
319 a breakpoint. */
bc043ef3 320 static const char * const bpdisps[] = {"del", "dstp", "dis", "keep"};
cc59ec59 321
2cec12e5
AR
322 return bpdisps[(int) disp];
323}
c906108c 324
4a64f543 325/* Prototypes for exported functions. */
c906108c 326/* If FALSE, gdb will not use hardware support for watchpoints, even
4a64f543 327 if such is available. */
c906108c
SS
328static int can_use_hw_watchpoints;
329
920d2a44
AC
330static void
331show_can_use_hw_watchpoints (struct ui_file *file, int from_tty,
332 struct cmd_list_element *c,
333 const char *value)
334{
3e43a32a
MS
335 fprintf_filtered (file,
336 _("Debugger's willingness to use "
337 "watchpoint hardware is %s.\n"),
920d2a44
AC
338 value);
339}
340
fa8d40ab
JJ
341/* If AUTO_BOOLEAN_FALSE, gdb will not attempt to create pending breakpoints.
342 If AUTO_BOOLEAN_TRUE, gdb will automatically create pending breakpoints
4a64f543 343 for unrecognized breakpoint locations.
fa8d40ab
JJ
344 If AUTO_BOOLEAN_AUTO, gdb will query when breakpoints are unrecognized. */
345static enum auto_boolean pending_break_support;
920d2a44
AC
346static void
347show_pending_break_support (struct ui_file *file, int from_tty,
348 struct cmd_list_element *c,
349 const char *value)
350{
3e43a32a
MS
351 fprintf_filtered (file,
352 _("Debugger's behavior regarding "
353 "pending breakpoints is %s.\n"),
920d2a44
AC
354 value);
355}
fa8d40ab 356
765dc015 357/* If 1, gdb will automatically use hardware breakpoints for breakpoints
4a64f543 358 set with "break" but falling in read-only memory.
765dc015
VP
359 If 0, gdb will warn about such breakpoints, but won't automatically
360 use hardware breakpoints. */
361static int automatic_hardware_breakpoints;
362static void
363show_automatic_hardware_breakpoints (struct ui_file *file, int from_tty,
364 struct cmd_list_element *c,
365 const char *value)
366{
3e43a32a
MS
367 fprintf_filtered (file,
368 _("Automatic usage of hardware breakpoints is %s.\n"),
765dc015
VP
369 value);
370}
371
a25a5a45
PA
372/* If on, GDB keeps breakpoints inserted even if the inferior is
373 stopped, and immediately inserts any new breakpoints as soon as
374 they're created. If off (default), GDB keeps breakpoints off of
375 the target as long as possible. That is, it delays inserting
376 breakpoints until the next resume, and removes them again when the
377 target fully stops. This is a bit safer in case GDB crashes while
378 processing user input. */
379static int always_inserted_mode = 0;
72d0e2c5 380
33e5cbd6 381static void
74960c60 382show_always_inserted_mode (struct ui_file *file, int from_tty,
33e5cbd6 383 struct cmd_list_element *c, const char *value)
74960c60 384{
a25a5a45
PA
385 fprintf_filtered (file, _("Always inserted breakpoint mode is %s.\n"),
386 value);
74960c60
VP
387}
388
b57bacec
PA
389/* See breakpoint.h. */
390
33e5cbd6 391int
a25a5a45 392breakpoints_should_be_inserted_now (void)
33e5cbd6 393{
a25a5a45
PA
394 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
395 {
396 /* If breakpoints are global, they should be inserted even if no
397 thread under gdb's control is running, or even if there are
398 no threads under GDB's control yet. */
399 return 1;
400 }
401 else if (target_has_execution)
402 {
372316f1
PA
403 struct thread_info *tp;
404
a25a5a45
PA
405 if (always_inserted_mode)
406 {
407 /* The user wants breakpoints inserted even if all threads
408 are stopped. */
409 return 1;
410 }
411
b57bacec
PA
412 if (threads_are_executing ())
413 return 1;
372316f1
PA
414
415 /* Don't remove breakpoints yet if, even though all threads are
416 stopped, we still have events to process. */
417 ALL_NON_EXITED_THREADS (tp)
418 if (tp->resumed
419 && tp->suspend.waitstatus_pending_p)
420 return 1;
a25a5a45
PA
421 }
422 return 0;
33e5cbd6 423}
765dc015 424
b775012e
LM
425static const char condition_evaluation_both[] = "host or target";
426
427/* Modes for breakpoint condition evaluation. */
428static const char condition_evaluation_auto[] = "auto";
429static const char condition_evaluation_host[] = "host";
430static const char condition_evaluation_target[] = "target";
431static const char *const condition_evaluation_enums[] = {
432 condition_evaluation_auto,
433 condition_evaluation_host,
434 condition_evaluation_target,
435 NULL
436};
437
438/* Global that holds the current mode for breakpoint condition evaluation. */
439static const char *condition_evaluation_mode_1 = condition_evaluation_auto;
440
441/* Global that we use to display information to the user (gets its value from
442 condition_evaluation_mode_1. */
443static const char *condition_evaluation_mode = condition_evaluation_auto;
444
445/* Translate a condition evaluation mode MODE into either "host"
446 or "target". This is used mostly to translate from "auto" to the
447 real setting that is being used. It returns the translated
448 evaluation mode. */
449
450static const char *
451translate_condition_evaluation_mode (const char *mode)
452{
453 if (mode == condition_evaluation_auto)
454 {
455 if (target_supports_evaluation_of_breakpoint_conditions ())
456 return condition_evaluation_target;
457 else
458 return condition_evaluation_host;
459 }
460 else
461 return mode;
462}
463
464/* Discovers what condition_evaluation_auto translates to. */
465
466static const char *
467breakpoint_condition_evaluation_mode (void)
468{
469 return translate_condition_evaluation_mode (condition_evaluation_mode);
470}
471
472/* Return true if GDB should evaluate breakpoint conditions or false
473 otherwise. */
474
475static int
476gdb_evaluates_breakpoint_condition_p (void)
477{
478 const char *mode = breakpoint_condition_evaluation_mode ();
479
480 return (mode == condition_evaluation_host);
481}
482
c906108c
SS
483/* Are we executing breakpoint commands? */
484static int executing_breakpoint_commands;
485
c02f5703
MS
486/* Are overlay event breakpoints enabled? */
487static int overlay_events_enabled;
488
e09342b5
TJB
489/* See description in breakpoint.h. */
490int target_exact_watchpoints = 0;
491
c906108c 492/* Walk the following statement or block through all breakpoints.
e5dd4106 493 ALL_BREAKPOINTS_SAFE does so even if the statement deletes the
4a64f543 494 current breakpoint. */
c906108c 495
5c44784c 496#define ALL_BREAKPOINTS(B) for (B = breakpoint_chain; B; B = B->next)
c906108c 497
5c44784c
JM
498#define ALL_BREAKPOINTS_SAFE(B,TMP) \
499 for (B = breakpoint_chain; \
500 B ? (TMP=B->next, 1): 0; \
501 B = TMP)
c906108c 502
4a64f543
MS
503/* Similar iterator for the low-level breakpoints. SAFE variant is
504 not provided so update_global_location_list must not be called
505 while executing the block of ALL_BP_LOCATIONS. */
7cc221ef 506
876fa593 507#define ALL_BP_LOCATIONS(B,BP_TMP) \
f5336ca5
PA
508 for (BP_TMP = bp_locations; \
509 BP_TMP < bp_locations + bp_locations_count && (B = *BP_TMP);\
876fa593 510 BP_TMP++)
7cc221ef 511
b775012e
LM
512/* Iterates through locations with address ADDRESS for the currently selected
513 program space. BP_LOCP_TMP points to each object. BP_LOCP_START points
514 to where the loop should start from.
515 If BP_LOCP_START is a NULL pointer, the macro automatically seeks the
516 appropriate location to start with. */
517
518#define ALL_BP_LOCATIONS_AT_ADDR(BP_LOCP_TMP, BP_LOCP_START, ADDRESS) \
519 for (BP_LOCP_START = BP_LOCP_START == NULL ? get_first_locp_gte_addr (ADDRESS) : BP_LOCP_START, \
520 BP_LOCP_TMP = BP_LOCP_START; \
521 BP_LOCP_START \
f5336ca5 522 && (BP_LOCP_TMP < bp_locations + bp_locations_count \
b775012e
LM
523 && (*BP_LOCP_TMP)->address == ADDRESS); \
524 BP_LOCP_TMP++)
525
1042e4c0
SS
526/* Iterator for tracepoints only. */
527
528#define ALL_TRACEPOINTS(B) \
529 for (B = breakpoint_chain; B; B = B->next) \
d77f58be 530 if (is_tracepoint (B))
1042e4c0 531
7cc221ef 532/* Chains of all breakpoints defined. */
c906108c
SS
533
534struct breakpoint *breakpoint_chain;
535
f5336ca5 536/* Array is sorted by bp_locations_compare - primarily by the ADDRESS. */
876fa593 537
f5336ca5 538static struct bp_location **bp_locations;
876fa593 539
f5336ca5 540/* Number of elements of BP_LOCATIONS. */
876fa593 541
f5336ca5 542static unsigned bp_locations_count;
876fa593 543
4a64f543 544/* Maximum alignment offset between bp_target_info.PLACED_ADDRESS and
f5336ca5 545 ADDRESS for the current elements of BP_LOCATIONS which get a valid
4a64f543 546 result from bp_location_has_shadow. You can use it for roughly
f5336ca5 547 limiting the subrange of BP_LOCATIONS to scan for shadow bytes for
4a64f543 548 an address you need to read. */
876fa593 549
f5336ca5 550static CORE_ADDR bp_locations_placed_address_before_address_max;
876fa593 551
4a64f543
MS
552/* Maximum offset plus alignment between bp_target_info.PLACED_ADDRESS
553 + bp_target_info.SHADOW_LEN and ADDRESS for the current elements of
f5336ca5
PA
554 BP_LOCATIONS which get a valid result from bp_location_has_shadow.
555 You can use it for roughly limiting the subrange of BP_LOCATIONS to
4a64f543 556 scan for shadow bytes for an address you need to read. */
876fa593 557
f5336ca5 558static CORE_ADDR bp_locations_shadow_len_after_address_max;
7cc221ef 559
4a64f543 560/* The locations that no longer correspond to any breakpoint, unlinked
f5336ca5
PA
561 from the bp_locations array, but for which a hit may still be
562 reported by a target. */
1123588c 563static std::vector<bp_location *> moribund_locations;
20874c92 564
c906108c
SS
565/* Number of last breakpoint made. */
566
95a42b64
TT
567static int breakpoint_count;
568
86b17b60
PA
569/* The value of `breakpoint_count' before the last command that
570 created breakpoints. If the last (break-like) command created more
571 than one breakpoint, then the difference between BREAKPOINT_COUNT
572 and PREV_BREAKPOINT_COUNT is more than one. */
573static int prev_breakpoint_count;
c906108c 574
1042e4c0
SS
575/* Number of last tracepoint made. */
576
95a42b64 577static int tracepoint_count;
1042e4c0 578
6149aea9
PA
579static struct cmd_list_element *breakpoint_set_cmdlist;
580static struct cmd_list_element *breakpoint_show_cmdlist;
9291a0cd 581struct cmd_list_element *save_cmdlist;
6149aea9 582
badd37ce
SDJ
583/* See declaration at breakpoint.h. */
584
585struct breakpoint *
586breakpoint_find_if (int (*func) (struct breakpoint *b, void *d),
587 void *user_data)
588{
589 struct breakpoint *b = NULL;
590
591 ALL_BREAKPOINTS (b)
592 {
593 if (func (b, user_data) != 0)
594 break;
595 }
596
597 return b;
598}
599
468d015d
JJ
600/* Return whether a breakpoint is an active enabled breakpoint. */
601static int
602breakpoint_enabled (struct breakpoint *b)
603{
0d381245 604 return (b->enable_state == bp_enabled);
468d015d
JJ
605}
606
c906108c
SS
607/* Set breakpoint count to NUM. */
608
95a42b64 609static void
fba45db2 610set_breakpoint_count (int num)
c906108c 611{
86b17b60 612 prev_breakpoint_count = breakpoint_count;
c906108c 613 breakpoint_count = num;
4fa62494 614 set_internalvar_integer (lookup_internalvar ("bpnum"), num);
c906108c
SS
615}
616
86b17b60
PA
617/* Used by `start_rbreak_breakpoints' below, to record the current
618 breakpoint count before "rbreak" creates any breakpoint. */
619static int rbreak_start_breakpoint_count;
620
95a42b64
TT
621/* Called at the start an "rbreak" command to record the first
622 breakpoint made. */
86b17b60 623
c80049d3 624scoped_rbreak_breakpoints::scoped_rbreak_breakpoints ()
95a42b64 625{
86b17b60 626 rbreak_start_breakpoint_count = breakpoint_count;
95a42b64
TT
627}
628
629/* Called at the end of an "rbreak" command to record the last
630 breakpoint made. */
86b17b60 631
c80049d3 632scoped_rbreak_breakpoints::~scoped_rbreak_breakpoints ()
95a42b64 633{
86b17b60 634 prev_breakpoint_count = rbreak_start_breakpoint_count;
95a42b64
TT
635}
636
4a64f543 637/* Used in run_command to zero the hit count when a new run starts. */
c906108c
SS
638
639void
fba45db2 640clear_breakpoint_hit_counts (void)
c906108c
SS
641{
642 struct breakpoint *b;
643
644 ALL_BREAKPOINTS (b)
645 b->hit_count = 0;
646}
647
c906108c 648\f
48cb2d85
VP
649/* Return the breakpoint with the specified number, or NULL
650 if the number does not refer to an existing breakpoint. */
651
652struct breakpoint *
653get_breakpoint (int num)
654{
655 struct breakpoint *b;
656
657 ALL_BREAKPOINTS (b)
658 if (b->number == num)
659 return b;
660
661 return NULL;
662}
5c44784c 663
c906108c 664\f
adc36818 665
b775012e
LM
666/* Mark locations as "conditions have changed" in case the target supports
667 evaluating conditions on its side. */
668
669static void
670mark_breakpoint_modified (struct breakpoint *b)
671{
672 struct bp_location *loc;
673
674 /* This is only meaningful if the target is
675 evaluating conditions and if the user has
676 opted for condition evaluation on the target's
677 side. */
678 if (gdb_evaluates_breakpoint_condition_p ()
679 || !target_supports_evaluation_of_breakpoint_conditions ())
680 return;
681
682 if (!is_breakpoint (b))
683 return;
684
685 for (loc = b->loc; loc; loc = loc->next)
686 loc->condition_changed = condition_modified;
687}
688
689/* Mark location as "conditions have changed" in case the target supports
690 evaluating conditions on its side. */
691
692static void
693mark_breakpoint_location_modified (struct bp_location *loc)
694{
695 /* This is only meaningful if the target is
696 evaluating conditions and if the user has
697 opted for condition evaluation on the target's
698 side. */
699 if (gdb_evaluates_breakpoint_condition_p ()
700 || !target_supports_evaluation_of_breakpoint_conditions ())
701
702 return;
703
704 if (!is_breakpoint (loc->owner))
705 return;
706
707 loc->condition_changed = condition_modified;
708}
709
710/* Sets the condition-evaluation mode using the static global
711 condition_evaluation_mode. */
712
713static void
eb4c3f4a 714set_condition_evaluation_mode (const char *args, int from_tty,
b775012e
LM
715 struct cmd_list_element *c)
716{
b775012e
LM
717 const char *old_mode, *new_mode;
718
719 if ((condition_evaluation_mode_1 == condition_evaluation_target)
720 && !target_supports_evaluation_of_breakpoint_conditions ())
721 {
722 condition_evaluation_mode_1 = condition_evaluation_mode;
723 warning (_("Target does not support breakpoint condition evaluation.\n"
724 "Using host evaluation mode instead."));
725 return;
726 }
727
728 new_mode = translate_condition_evaluation_mode (condition_evaluation_mode_1);
729 old_mode = translate_condition_evaluation_mode (condition_evaluation_mode);
730
abf1152a
JK
731 /* Flip the switch. Flip it even if OLD_MODE == NEW_MODE as one of the
732 settings was "auto". */
733 condition_evaluation_mode = condition_evaluation_mode_1;
734
b775012e
LM
735 /* Only update the mode if the user picked a different one. */
736 if (new_mode != old_mode)
737 {
738 struct bp_location *loc, **loc_tmp;
739 /* If the user switched to a different evaluation mode, we
740 need to synch the changes with the target as follows:
741
742 "host" -> "target": Send all (valid) conditions to the target.
743 "target" -> "host": Remove all the conditions from the target.
744 */
745
b775012e
LM
746 if (new_mode == condition_evaluation_target)
747 {
748 /* Mark everything modified and synch conditions with the
749 target. */
750 ALL_BP_LOCATIONS (loc, loc_tmp)
751 mark_breakpoint_location_modified (loc);
752 }
753 else
754 {
755 /* Manually mark non-duplicate locations to synch conditions
756 with the target. We do this to remove all the conditions the
757 target knows about. */
758 ALL_BP_LOCATIONS (loc, loc_tmp)
759 if (is_breakpoint (loc->owner) && loc->inserted)
760 loc->needs_update = 1;
761 }
762
763 /* Do the update. */
44702360 764 update_global_location_list (UGLL_MAY_INSERT);
b775012e
LM
765 }
766
767 return;
768}
769
770/* Shows the current mode of breakpoint condition evaluation. Explicitly shows
771 what "auto" is translating to. */
772
773static void
774show_condition_evaluation_mode (struct ui_file *file, int from_tty,
775 struct cmd_list_element *c, const char *value)
776{
777 if (condition_evaluation_mode == condition_evaluation_auto)
778 fprintf_filtered (file,
779 _("Breakpoint condition evaluation "
780 "mode is %s (currently %s).\n"),
781 value,
782 breakpoint_condition_evaluation_mode ());
783 else
784 fprintf_filtered (file, _("Breakpoint condition evaluation mode is %s.\n"),
785 value);
786}
787
788/* A comparison function for bp_location AP and BP that is used by
789 bsearch. This comparison function only cares about addresses, unlike
f5336ca5 790 the more general bp_locations_compare function. */
b775012e
LM
791
792static int
f5336ca5 793bp_locations_compare_addrs (const void *ap, const void *bp)
b775012e 794{
9a3c8263
SM
795 const struct bp_location *a = *(const struct bp_location **) ap;
796 const struct bp_location *b = *(const struct bp_location **) bp;
b775012e
LM
797
798 if (a->address == b->address)
799 return 0;
800 else
801 return ((a->address > b->address) - (a->address < b->address));
802}
803
804/* Helper function to skip all bp_locations with addresses
805 less than ADDRESS. It returns the first bp_location that
806 is greater than or equal to ADDRESS. If none is found, just
807 return NULL. */
808
809static struct bp_location **
810get_first_locp_gte_addr (CORE_ADDR address)
811{
812 struct bp_location dummy_loc;
813 struct bp_location *dummy_locp = &dummy_loc;
814 struct bp_location **locp_found = NULL;
815
816 /* Initialize the dummy location's address field. */
b775012e
LM
817 dummy_loc.address = address;
818
819 /* Find a close match to the first location at ADDRESS. */
9a3c8263 820 locp_found = ((struct bp_location **)
f5336ca5 821 bsearch (&dummy_locp, bp_locations, bp_locations_count,
9a3c8263 822 sizeof (struct bp_location **),
f5336ca5 823 bp_locations_compare_addrs));
b775012e
LM
824
825 /* Nothing was found, nothing left to do. */
826 if (locp_found == NULL)
827 return NULL;
828
829 /* We may have found a location that is at ADDRESS but is not the first in the
830 location's list. Go backwards (if possible) and locate the first one. */
f5336ca5 831 while ((locp_found - 1) >= bp_locations
b775012e
LM
832 && (*(locp_found - 1))->address == address)
833 locp_found--;
834
835 return locp_found;
836}
837
adc36818 838void
7a26bd4d 839set_breakpoint_condition (struct breakpoint *b, const char *exp,
adc36818
PM
840 int from_tty)
841{
3a5c3e22
PA
842 xfree (b->cond_string);
843 b->cond_string = NULL;
adc36818 844
3a5c3e22 845 if (is_watchpoint (b))
adc36818 846 {
3a5c3e22
PA
847 struct watchpoint *w = (struct watchpoint *) b;
848
4d01a485 849 w->cond_exp.reset ();
3a5c3e22
PA
850 }
851 else
852 {
853 struct bp_location *loc;
854
855 for (loc = b->loc; loc; loc = loc->next)
856 {
4d01a485 857 loc->cond.reset ();
b775012e
LM
858
859 /* No need to free the condition agent expression
860 bytecode (if we have one). We will handle this
861 when we go through update_global_location_list. */
3a5c3e22 862 }
adc36818 863 }
adc36818
PM
864
865 if (*exp == 0)
866 {
867 if (from_tty)
868 printf_filtered (_("Breakpoint %d now unconditional.\n"), b->number);
869 }
870 else
871 {
bbc13ae3 872 const char *arg = exp;
cc59ec59 873
adc36818
PM
874 /* I don't know if it matters whether this is the string the user
875 typed in or the decompiled expression. */
876 b->cond_string = xstrdup (arg);
877 b->condition_not_parsed = 0;
878
879 if (is_watchpoint (b))
880 {
3a5c3e22
PA
881 struct watchpoint *w = (struct watchpoint *) b;
882
aee1fcdf 883 innermost_block.reset ();
adc36818 884 arg = exp;
1bb9788d 885 w->cond_exp = parse_exp_1 (&arg, 0, 0, 0);
adc36818
PM
886 if (*arg)
887 error (_("Junk at end of expression"));
aee1fcdf 888 w->cond_exp_valid_block = innermost_block.block ();
adc36818
PM
889 }
890 else
891 {
3a5c3e22
PA
892 struct bp_location *loc;
893
adc36818
PM
894 for (loc = b->loc; loc; loc = loc->next)
895 {
896 arg = exp;
897 loc->cond =
1bb9788d
TT
898 parse_exp_1 (&arg, loc->address,
899 block_for_pc (loc->address), 0);
adc36818
PM
900 if (*arg)
901 error (_("Junk at end of expression"));
902 }
903 }
904 }
b775012e
LM
905 mark_breakpoint_modified (b);
906
76727919 907 gdb::observers::breakpoint_modified.notify (b);
adc36818
PM
908}
909
d55637df
TT
910/* Completion for the "condition" command. */
911
eb3ff9a5 912static void
6f937416 913condition_completer (struct cmd_list_element *cmd,
eb3ff9a5 914 completion_tracker &tracker,
6f937416 915 const char *text, const char *word)
d55637df 916{
6f937416 917 const char *space;
d55637df 918
f1735a53
TT
919 text = skip_spaces (text);
920 space = skip_to_space (text);
d55637df
TT
921 if (*space == '\0')
922 {
923 int len;
924 struct breakpoint *b;
d55637df
TT
925
926 if (text[0] == '$')
927 {
928 /* We don't support completion of history indices. */
eb3ff9a5
PA
929 if (!isdigit (text[1]))
930 complete_internalvar (tracker, &text[1]);
931 return;
d55637df
TT
932 }
933
934 /* We're completing the breakpoint number. */
935 len = strlen (text);
936
937 ALL_BREAKPOINTS (b)
58ce7251
SDJ
938 {
939 char number[50];
940
941 xsnprintf (number, sizeof (number), "%d", b->number);
942
943 if (strncmp (number, text, len) == 0)
eb3ff9a5
PA
944 {
945 gdb::unique_xmalloc_ptr<char> copy (xstrdup (number));
946 tracker.add_completion (std::move (copy));
947 }
58ce7251 948 }
d55637df 949
eb3ff9a5 950 return;
d55637df
TT
951 }
952
953 /* We're completing the expression part. */
f1735a53 954 text = skip_spaces (space);
eb3ff9a5 955 expression_completer (cmd, tracker, text, word);
d55637df
TT
956}
957
c906108c
SS
958/* condition N EXP -- set break condition of breakpoint N to EXP. */
959
960static void
0b39b52e 961condition_command (const char *arg, int from_tty)
c906108c 962{
52f0bd74 963 struct breakpoint *b;
0b39b52e 964 const char *p;
52f0bd74 965 int bnum;
c906108c
SS
966
967 if (arg == 0)
e2e0b3e5 968 error_no_arg (_("breakpoint number"));
c906108c
SS
969
970 p = arg;
971 bnum = get_number (&p);
5c44784c 972 if (bnum == 0)
8a3fe4f8 973 error (_("Bad breakpoint argument: '%s'"), arg);
c906108c
SS
974
975 ALL_BREAKPOINTS (b)
976 if (b->number == bnum)
2f069f6f 977 {
6dddc817
DE
978 /* Check if this breakpoint has a "stop" method implemented in an
979 extension language. This method and conditions entered into GDB
980 from the CLI are mutually exclusive. */
981 const struct extension_language_defn *extlang
982 = get_breakpoint_cond_ext_lang (b, EXT_LANG_NONE);
983
984 if (extlang != NULL)
985 {
986 error (_("Only one stop condition allowed. There is currently"
987 " a %s stop condition defined for this breakpoint."),
988 ext_lang_capitalized_name (extlang));
989 }
2566ad2d 990 set_breakpoint_condition (b, p, from_tty);
b775012e
LM
991
992 if (is_breakpoint (b))
44702360 993 update_global_location_list (UGLL_MAY_INSERT);
b775012e 994
2f069f6f
JB
995 return;
996 }
c906108c 997
8a3fe4f8 998 error (_("No breakpoint number %d."), bnum);
c906108c
SS
999}
1000
a7bdde9e
VP
1001/* Check that COMMAND do not contain commands that are suitable
1002 only for tracepoints and not suitable for ordinary breakpoints.
4a64f543
MS
1003 Throw if any such commands is found. */
1004
a7bdde9e
VP
1005static void
1006check_no_tracepoint_commands (struct command_line *commands)
1007{
1008 struct command_line *c;
cc59ec59 1009
a7bdde9e
VP
1010 for (c = commands; c; c = c->next)
1011 {
a7bdde9e 1012 if (c->control_type == while_stepping_control)
3e43a32a
MS
1013 error (_("The 'while-stepping' command can "
1014 "only be used for tracepoints"));
a7bdde9e 1015
12973681
TT
1016 check_no_tracepoint_commands (c->body_list_0.get ());
1017 check_no_tracepoint_commands (c->body_list_1.get ());
a7bdde9e
VP
1018
1019 /* Not that command parsing removes leading whitespace and comment
4a64f543 1020 lines and also empty lines. So, we only need to check for
a7bdde9e
VP
1021 command directly. */
1022 if (strstr (c->line, "collect ") == c->line)
1023 error (_("The 'collect' command can only be used for tracepoints"));
1024
51661e93
VP
1025 if (strstr (c->line, "teval ") == c->line)
1026 error (_("The 'teval' command can only be used for tracepoints"));
a7bdde9e
VP
1027 }
1028}
1029
c1fc2657 1030struct longjmp_breakpoint : public breakpoint
3b0871f4 1031{
c1fc2657 1032 ~longjmp_breakpoint () override;
3b0871f4
SM
1033};
1034
d77f58be
SS
1035/* Encapsulate tests for different types of tracepoints. */
1036
3b0871f4
SM
1037static bool
1038is_tracepoint_type (bptype type)
d9b3f62e
PA
1039{
1040 return (type == bp_tracepoint
1041 || type == bp_fast_tracepoint
1042 || type == bp_static_tracepoint);
1043}
1044
3b0871f4
SM
1045static bool
1046is_longjmp_type (bptype type)
1047{
1048 return type == bp_longjmp || type == bp_exception;
1049}
1050
a7bdde9e 1051int
d77f58be 1052is_tracepoint (const struct breakpoint *b)
a7bdde9e 1053{
d9b3f62e 1054 return is_tracepoint_type (b->type);
a7bdde9e 1055}
d9b3f62e 1056
a5e364af
SM
1057/* Factory function to create an appropriate instance of breakpoint given
1058 TYPE. */
1059
1060static std::unique_ptr<breakpoint>
1061new_breakpoint_from_type (bptype type)
1062{
1063 breakpoint *b;
1064
1065 if (is_tracepoint_type (type))
c1fc2657 1066 b = new tracepoint ();
3b0871f4 1067 else if (is_longjmp_type (type))
c1fc2657 1068 b = new longjmp_breakpoint ();
a5e364af
SM
1069 else
1070 b = new breakpoint ();
1071
1072 return std::unique_ptr<breakpoint> (b);
1073}
1074
e5dd4106 1075/* A helper function that validates that COMMANDS are valid for a
95a42b64
TT
1076 breakpoint. This function will throw an exception if a problem is
1077 found. */
48cb2d85 1078
95a42b64
TT
1079static void
1080validate_commands_for_breakpoint (struct breakpoint *b,
1081 struct command_line *commands)
48cb2d85 1082{
d77f58be 1083 if (is_tracepoint (b))
a7bdde9e 1084 {
c9a6ce02 1085 struct tracepoint *t = (struct tracepoint *) b;
a7bdde9e
VP
1086 struct command_line *c;
1087 struct command_line *while_stepping = 0;
c9a6ce02
PA
1088
1089 /* Reset the while-stepping step count. The previous commands
1090 might have included a while-stepping action, while the new
1091 ones might not. */
1092 t->step_count = 0;
1093
1094 /* We need to verify that each top-level element of commands is
1095 valid for tracepoints, that there's at most one
1096 while-stepping element, and that the while-stepping's body
1097 has valid tracing commands excluding nested while-stepping.
1098 We also need to validate the tracepoint action line in the
1099 context of the tracepoint --- validate_actionline actually
1100 has side effects, like setting the tracepoint's
1101 while-stepping STEP_COUNT, in addition to checking if the
1102 collect/teval actions parse and make sense in the
1103 tracepoint's context. */
a7bdde9e
VP
1104 for (c = commands; c; c = c->next)
1105 {
a7bdde9e
VP
1106 if (c->control_type == while_stepping_control)
1107 {
1108 if (b->type == bp_fast_tracepoint)
3e43a32a
MS
1109 error (_("The 'while-stepping' command "
1110 "cannot be used for fast tracepoint"));
0fb4aa4b 1111 else if (b->type == bp_static_tracepoint)
3e43a32a
MS
1112 error (_("The 'while-stepping' command "
1113 "cannot be used for static tracepoint"));
a7bdde9e
VP
1114
1115 if (while_stepping)
3e43a32a
MS
1116 error (_("The 'while-stepping' command "
1117 "can be used only once"));
a7bdde9e
VP
1118 else
1119 while_stepping = c;
1120 }
c9a6ce02
PA
1121
1122 validate_actionline (c->line, b);
a7bdde9e
VP
1123 }
1124 if (while_stepping)
1125 {
1126 struct command_line *c2;
1127
12973681
TT
1128 gdb_assert (while_stepping->body_list_1 == nullptr);
1129 c2 = while_stepping->body_list_0.get ();
a7bdde9e
VP
1130 for (; c2; c2 = c2->next)
1131 {
a7bdde9e
VP
1132 if (c2->control_type == while_stepping_control)
1133 error (_("The 'while-stepping' command cannot be nested"));
1134 }
1135 }
1136 }
1137 else
1138 {
1139 check_no_tracepoint_commands (commands);
1140 }
95a42b64
TT
1141}
1142
0fb4aa4b
PA
1143/* Return a vector of all the static tracepoints set at ADDR. The
1144 caller is responsible for releasing the vector. */
1145
f51e0e20 1146std::vector<breakpoint *>
0fb4aa4b
PA
1147static_tracepoints_here (CORE_ADDR addr)
1148{
1149 struct breakpoint *b;
f51e0e20 1150 std::vector<breakpoint *> found;
0fb4aa4b
PA
1151 struct bp_location *loc;
1152
1153 ALL_BREAKPOINTS (b)
1154 if (b->type == bp_static_tracepoint)
1155 {
1156 for (loc = b->loc; loc; loc = loc->next)
1157 if (loc->address == addr)
f51e0e20 1158 found.push_back (b);
0fb4aa4b
PA
1159 }
1160
1161 return found;
1162}
1163
95a42b64 1164/* Set the command list of B to COMMANDS. If breakpoint is tracepoint,
4a64f543 1165 validate that only allowed commands are included. */
95a42b64
TT
1166
1167void
4a64f543 1168breakpoint_set_commands (struct breakpoint *b,
12973681 1169 counted_command_line &&commands)
95a42b64 1170{
93921405 1171 validate_commands_for_breakpoint (b, commands.get ());
a7bdde9e 1172
d1b0a7bf 1173 b->commands = std::move (commands);
76727919 1174 gdb::observers::breakpoint_modified.notify (b);
48cb2d85
VP
1175}
1176
45a43567
TT
1177/* Set the internal `silent' flag on the breakpoint. Note that this
1178 is not the same as the "silent" that may appear in the breakpoint's
1179 commands. */
1180
1181void
1182breakpoint_set_silent (struct breakpoint *b, int silent)
1183{
1184 int old_silent = b->silent;
1185
1186 b->silent = silent;
1187 if (old_silent != silent)
76727919 1188 gdb::observers::breakpoint_modified.notify (b);
45a43567
TT
1189}
1190
1191/* Set the thread for this breakpoint. If THREAD is -1, make the
1192 breakpoint work for any thread. */
1193
1194void
1195breakpoint_set_thread (struct breakpoint *b, int thread)
1196{
1197 int old_thread = b->thread;
1198
1199 b->thread = thread;
1200 if (old_thread != thread)
76727919 1201 gdb::observers::breakpoint_modified.notify (b);
45a43567
TT
1202}
1203
1204/* Set the task for this breakpoint. If TASK is 0, make the
1205 breakpoint work for any task. */
1206
1207void
1208breakpoint_set_task (struct breakpoint *b, int task)
1209{
1210 int old_task = b->task;
1211
1212 b->task = task;
1213 if (old_task != task)
76727919 1214 gdb::observers::breakpoint_modified.notify (b);
45a43567
TT
1215}
1216
95a42b64 1217static void
896b6bda 1218commands_command_1 (const char *arg, int from_tty,
4a64f543 1219 struct command_line *control)
95a42b64 1220{
d1b0a7bf 1221 counted_command_line cmd;
95a42b64 1222
896b6bda
PA
1223 std::string new_arg;
1224
95a42b64
TT
1225 if (arg == NULL || !*arg)
1226 {
86b17b60 1227 if (breakpoint_count - prev_breakpoint_count > 1)
896b6bda
PA
1228 new_arg = string_printf ("%d-%d", prev_breakpoint_count + 1,
1229 breakpoint_count);
95a42b64 1230 else if (breakpoint_count > 0)
896b6bda 1231 new_arg = string_printf ("%d", breakpoint_count);
48649e1b
TT
1232 arg = new_arg.c_str ();
1233 }
1234
1235 map_breakpoint_numbers
1236 (arg, [&] (breakpoint *b)
1237 {
1238 if (cmd == NULL)
1239 {
48649e1b 1240 if (control != NULL)
12973681 1241 cmd = control->body_list_0;
48649e1b
TT
1242 else
1243 {
81b1e71c
TT
1244 std::string str
1245 = string_printf (_("Type commands for breakpoint(s) "
1246 "%s, one per line."),
1247 arg);
48649e1b 1248
60b3cef2
TT
1249 auto do_validate = [=] (const char *line)
1250 {
1251 validate_actionline (line, b);
1252 };
1253 gdb::function_view<void (const char *)> validator;
1254 if (is_tracepoint (b))
1255 validator = do_validate;
1256
1257 cmd = read_command_lines (str.c_str (), from_tty, 1, validator);
48649e1b 1258 }
48649e1b
TT
1259 }
1260
1261 /* If a breakpoint was on the list more than once, we don't need to
1262 do anything. */
1263 if (b->commands != cmd)
1264 {
d1b0a7bf 1265 validate_commands_for_breakpoint (b, cmd.get ());
48649e1b 1266 b->commands = cmd;
76727919 1267 gdb::observers::breakpoint_modified.notify (b);
48649e1b
TT
1268 }
1269 });
95a42b64
TT
1270}
1271
1272static void
0b39b52e 1273commands_command (const char *arg, int from_tty)
95a42b64
TT
1274{
1275 commands_command_1 (arg, from_tty, NULL);
c906108c 1276}
40c03ae8
EZ
1277
1278/* Like commands_command, but instead of reading the commands from
1279 input stream, takes them from an already parsed command structure.
1280
1281 This is used by cli-script.c to DTRT with breakpoint commands
1282 that are part of if and while bodies. */
1283enum command_control_type
896b6bda 1284commands_from_control_command (const char *arg, struct command_line *cmd)
40c03ae8 1285{
95a42b64
TT
1286 commands_command_1 (arg, 0, cmd);
1287 return simple_control;
40c03ae8 1288}
876fa593
JK
1289
1290/* Return non-zero if BL->TARGET_INFO contains valid information. */
1291
1292static int
1293bp_location_has_shadow (struct bp_location *bl)
1294{
1295 if (bl->loc_type != bp_loc_software_breakpoint)
1296 return 0;
1297 if (!bl->inserted)
1298 return 0;
1299 if (bl->target_info.shadow_len == 0)
e5dd4106 1300 /* BL isn't valid, or doesn't shadow memory. */
876fa593
JK
1301 return 0;
1302 return 1;
1303}
1304
9d497a19
PA
1305/* Update BUF, which is LEN bytes read from the target address
1306 MEMADDR, by replacing a memory breakpoint with its shadowed
1307 contents.
1308
1309 If READBUF is not NULL, this buffer must not overlap with the of
1310 the breakpoint location's shadow_contents buffer. Otherwise, a
1311 failed assertion internal error will be raised. */
1312
1313static void
1314one_breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1315 const gdb_byte *writebuf_org,
1316 ULONGEST memaddr, LONGEST len,
1317 struct bp_target_info *target_info,
1318 struct gdbarch *gdbarch)
1319{
1320 /* Now do full processing of the found relevant range of elements. */
1321 CORE_ADDR bp_addr = 0;
1322 int bp_size = 0;
1323 int bptoffset = 0;
1324
1325 if (!breakpoint_address_match (target_info->placed_address_space, 0,
1326 current_program_space->aspace, 0))
1327 {
1328 /* The breakpoint is inserted in a different address space. */
1329 return;
1330 }
1331
1332 /* Addresses and length of the part of the breakpoint that
1333 we need to copy. */
1334 bp_addr = target_info->placed_address;
1335 bp_size = target_info->shadow_len;
1336
1337 if (bp_addr + bp_size <= memaddr)
1338 {
1339 /* The breakpoint is entirely before the chunk of memory we are
1340 reading. */
1341 return;
1342 }
1343
1344 if (bp_addr >= memaddr + len)
1345 {
1346 /* The breakpoint is entirely after the chunk of memory we are
1347 reading. */
1348 return;
1349 }
1350
1351 /* Offset within shadow_contents. */
1352 if (bp_addr < memaddr)
1353 {
1354 /* Only copy the second part of the breakpoint. */
1355 bp_size -= memaddr - bp_addr;
1356 bptoffset = memaddr - bp_addr;
1357 bp_addr = memaddr;
1358 }
1359
1360 if (bp_addr + bp_size > memaddr + len)
1361 {
1362 /* Only copy the first part of the breakpoint. */
1363 bp_size -= (bp_addr + bp_size) - (memaddr + len);
1364 }
1365
1366 if (readbuf != NULL)
1367 {
1368 /* Verify that the readbuf buffer does not overlap with the
1369 shadow_contents buffer. */
1370 gdb_assert (target_info->shadow_contents >= readbuf + len
1371 || readbuf >= (target_info->shadow_contents
1372 + target_info->shadow_len));
1373
1374 /* Update the read buffer with this inserted breakpoint's
1375 shadow. */
1376 memcpy (readbuf + bp_addr - memaddr,
1377 target_info->shadow_contents + bptoffset, bp_size);
1378 }
1379 else
1380 {
1381 const unsigned char *bp;
0d5ed153
MR
1382 CORE_ADDR addr = target_info->reqstd_address;
1383 int placed_size;
9d497a19
PA
1384
1385 /* Update the shadow with what we want to write to memory. */
1386 memcpy (target_info->shadow_contents + bptoffset,
1387 writebuf_org + bp_addr - memaddr, bp_size);
1388
1389 /* Determine appropriate breakpoint contents and size for this
1390 address. */
0d5ed153 1391 bp = gdbarch_breakpoint_from_pc (gdbarch, &addr, &placed_size);
9d497a19
PA
1392
1393 /* Update the final write buffer with this inserted
1394 breakpoint's INSN. */
1395 memcpy (writebuf + bp_addr - memaddr, bp + bptoffset, bp_size);
1396 }
1397}
1398
8defab1a 1399/* Update BUF, which is LEN bytes read from the target address MEMADDR,
876fa593
JK
1400 by replacing any memory breakpoints with their shadowed contents.
1401
35c63cd8
JB
1402 If READBUF is not NULL, this buffer must not overlap with any of
1403 the breakpoint location's shadow_contents buffers. Otherwise,
1404 a failed assertion internal error will be raised.
1405
876fa593 1406 The range of shadowed area by each bp_location is:
f5336ca5
PA
1407 bl->address - bp_locations_placed_address_before_address_max
1408 up to bl->address + bp_locations_shadow_len_after_address_max
876fa593
JK
1409 The range we were requested to resolve shadows for is:
1410 memaddr ... memaddr + len
1411 Thus the safe cutoff boundaries for performance optimization are
35df4500 1412 memaddr + len <= (bl->address
f5336ca5 1413 - bp_locations_placed_address_before_address_max)
876fa593 1414 and:
f5336ca5 1415 bl->address + bp_locations_shadow_len_after_address_max <= memaddr */
c906108c 1416
8defab1a 1417void
f0ba3972
PA
1418breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1419 const gdb_byte *writebuf_org,
1420 ULONGEST memaddr, LONGEST len)
c906108c 1421{
4a64f543
MS
1422 /* Left boundary, right boundary and median element of our binary
1423 search. */
876fa593
JK
1424 unsigned bc_l, bc_r, bc;
1425
4a64f543
MS
1426 /* Find BC_L which is a leftmost element which may affect BUF
1427 content. It is safe to report lower value but a failure to
1428 report higher one. */
876fa593
JK
1429
1430 bc_l = 0;
f5336ca5 1431 bc_r = bp_locations_count;
876fa593
JK
1432 while (bc_l + 1 < bc_r)
1433 {
35df4500 1434 struct bp_location *bl;
876fa593
JK
1435
1436 bc = (bc_l + bc_r) / 2;
f5336ca5 1437 bl = bp_locations[bc];
876fa593 1438
4a64f543
MS
1439 /* Check first BL->ADDRESS will not overflow due to the added
1440 constant. Then advance the left boundary only if we are sure
1441 the BC element can in no way affect the BUF content (MEMADDR
1442 to MEMADDR + LEN range).
876fa593 1443
f5336ca5 1444 Use the BP_LOCATIONS_SHADOW_LEN_AFTER_ADDRESS_MAX safety
4a64f543
MS
1445 offset so that we cannot miss a breakpoint with its shadow
1446 range tail still reaching MEMADDR. */
c5aa993b 1447
f5336ca5 1448 if ((bl->address + bp_locations_shadow_len_after_address_max
35df4500 1449 >= bl->address)
f5336ca5 1450 && (bl->address + bp_locations_shadow_len_after_address_max
35df4500 1451 <= memaddr))
876fa593
JK
1452 bc_l = bc;
1453 else
1454 bc_r = bc;
1455 }
1456
128070bb
PA
1457 /* Due to the binary search above, we need to make sure we pick the
1458 first location that's at BC_L's address. E.g., if there are
1459 multiple locations at the same address, BC_L may end up pointing
1460 at a duplicate location, and miss the "master"/"inserted"
1461 location. Say, given locations L1, L2 and L3 at addresses A and
1462 B:
1463
1464 L1@A, L2@A, L3@B, ...
1465
1466 BC_L could end up pointing at location L2, while the "master"
1467 location could be L1. Since the `loc->inserted' flag is only set
1468 on "master" locations, we'd forget to restore the shadow of L1
1469 and L2. */
1470 while (bc_l > 0
f5336ca5 1471 && bp_locations[bc_l]->address == bp_locations[bc_l - 1]->address)
128070bb
PA
1472 bc_l--;
1473
876fa593
JK
1474 /* Now do full processing of the found relevant range of elements. */
1475
f5336ca5 1476 for (bc = bc_l; bc < bp_locations_count; bc++)
c5aa993b 1477 {
f5336ca5 1478 struct bp_location *bl = bp_locations[bc];
876fa593 1479
35df4500
TJB
1480 /* bp_location array has BL->OWNER always non-NULL. */
1481 if (bl->owner->type == bp_none)
8a3fe4f8 1482 warning (_("reading through apparently deleted breakpoint #%d?"),
35df4500 1483 bl->owner->number);
ffce0d52 1484
e5dd4106 1485 /* Performance optimization: any further element can no longer affect BUF
876fa593
JK
1486 content. */
1487
f5336ca5
PA
1488 if (bl->address >= bp_locations_placed_address_before_address_max
1489 && memaddr + len <= (bl->address
1490 - bp_locations_placed_address_before_address_max))
876fa593
JK
1491 break;
1492
35df4500 1493 if (!bp_location_has_shadow (bl))
c5aa993b 1494 continue;
6c95b8df 1495
9d497a19
PA
1496 one_breakpoint_xfer_memory (readbuf, writebuf, writebuf_org,
1497 memaddr, len, &bl->target_info, bl->gdbarch);
1498 }
c906108c 1499}
9d497a19 1500
c906108c 1501\f
c5aa993b 1502
b775012e
LM
1503/* Return true if BPT is either a software breakpoint or a hardware
1504 breakpoint. */
1505
1506int
1507is_breakpoint (const struct breakpoint *bpt)
1508{
1509 return (bpt->type == bp_breakpoint
e7e0cddf
SS
1510 || bpt->type == bp_hardware_breakpoint
1511 || bpt->type == bp_dprintf);
b775012e
LM
1512}
1513
60e1c644
PA
1514/* Return true if BPT is of any hardware watchpoint kind. */
1515
a5606eee 1516static int
d77f58be 1517is_hardware_watchpoint (const struct breakpoint *bpt)
a5606eee
VP
1518{
1519 return (bpt->type == bp_hardware_watchpoint
1520 || bpt->type == bp_read_watchpoint
1521 || bpt->type == bp_access_watchpoint);
1522}
7270d8f2 1523
60e1c644
PA
1524/* Return true if BPT is of any watchpoint kind, hardware or
1525 software. */
1526
3a5c3e22 1527int
d77f58be 1528is_watchpoint (const struct breakpoint *bpt)
60e1c644
PA
1529{
1530 return (is_hardware_watchpoint (bpt)
1531 || bpt->type == bp_watchpoint);
1532}
1533
3a5c3e22
PA
1534/* Returns true if the current thread and its running state are safe
1535 to evaluate or update watchpoint B. Watchpoints on local
1536 expressions need to be evaluated in the context of the thread that
1537 was current when the watchpoint was created, and, that thread needs
1538 to be stopped to be able to select the correct frame context.
1539 Watchpoints on global expressions can be evaluated on any thread,
1540 and in any state. It is presently left to the target allowing
1541 memory accesses when threads are running. */
f6bc2008
PA
1542
1543static int
3a5c3e22 1544watchpoint_in_thread_scope (struct watchpoint *b)
f6bc2008 1545{
c1fc2657 1546 return (b->pspace == current_program_space
d7e15655
TT
1547 && (b->watchpoint_thread == null_ptid
1548 || (inferior_ptid == b->watchpoint_thread
00431a78 1549 && !inferior_thread ()->executing)));
f6bc2008
PA
1550}
1551
d0fb5eae
JK
1552/* Set watchpoint B to disp_del_at_next_stop, even including its possible
1553 associated bp_watchpoint_scope breakpoint. */
1554
1555static void
3a5c3e22 1556watchpoint_del_at_next_stop (struct watchpoint *w)
d0fb5eae 1557{
c1fc2657 1558 if (w->related_breakpoint != w)
d0fb5eae 1559 {
c1fc2657
SM
1560 gdb_assert (w->related_breakpoint->type == bp_watchpoint_scope);
1561 gdb_assert (w->related_breakpoint->related_breakpoint == w);
1562 w->related_breakpoint->disposition = disp_del_at_next_stop;
1563 w->related_breakpoint->related_breakpoint = w->related_breakpoint;
1564 w->related_breakpoint = w;
d0fb5eae 1565 }
c1fc2657 1566 w->disposition = disp_del_at_next_stop;
d0fb5eae
JK
1567}
1568
bb9d5f81
PP
1569/* Extract a bitfield value from value VAL using the bit parameters contained in
1570 watchpoint W. */
1571
1572static struct value *
1573extract_bitfield_from_watchpoint_value (struct watchpoint *w, struct value *val)
1574{
1575 struct value *bit_val;
1576
1577 if (val == NULL)
1578 return NULL;
1579
1580 bit_val = allocate_value (value_type (val));
1581
1582 unpack_value_bitfield (bit_val,
1583 w->val_bitpos,
1584 w->val_bitsize,
1585 value_contents_for_printing (val),
1586 value_offset (val),
1587 val);
1588
1589 return bit_val;
1590}
1591
c6d81124
PA
1592/* Allocate a dummy location and add it to B, which must be a software
1593 watchpoint. This is required because even if a software watchpoint
1594 is not watching any memory, bpstat_stop_status requires a location
1595 to be able to report stops. */
1596
1597static void
1598software_watchpoint_add_no_memory_location (struct breakpoint *b,
1599 struct program_space *pspace)
1600{
1601 gdb_assert (b->type == bp_watchpoint && b->loc == NULL);
1602
1603 b->loc = allocate_bp_location (b);
1604 b->loc->pspace = pspace;
1605 b->loc->address = -1;
1606 b->loc->length = -1;
1607}
1608
1609/* Returns true if B is a software watchpoint that is not watching any
1610 memory (e.g., "watch $pc"). */
1611
1612static int
1613is_no_memory_software_watchpoint (struct breakpoint *b)
1614{
1615 return (b->type == bp_watchpoint
1616 && b->loc != NULL
1617 && b->loc->next == NULL
1618 && b->loc->address == -1
1619 && b->loc->length == -1);
1620}
1621
567e1b4e
JB
1622/* Assuming that B is a watchpoint:
1623 - Reparse watchpoint expression, if REPARSE is non-zero
a5606eee 1624 - Evaluate expression and store the result in B->val
567e1b4e
JB
1625 - Evaluate the condition if there is one, and store the result
1626 in b->loc->cond.
a5606eee
VP
1627 - Update the list of values that must be watched in B->loc.
1628
4a64f543
MS
1629 If the watchpoint disposition is disp_del_at_next_stop, then do
1630 nothing. If this is local watchpoint that is out of scope, delete
1631 it.
1632
1633 Even with `set breakpoint always-inserted on' the watchpoints are
1634 removed + inserted on each stop here. Normal breakpoints must
1635 never be removed because they might be missed by a running thread
1636 when debugging in non-stop mode. On the other hand, hardware
1637 watchpoints (is_hardware_watchpoint; processed here) are specific
1638 to each LWP since they are stored in each LWP's hardware debug
1639 registers. Therefore, such LWP must be stopped first in order to
1640 be able to modify its hardware watchpoints.
1641
1642 Hardware watchpoints must be reset exactly once after being
1643 presented to the user. It cannot be done sooner, because it would
1644 reset the data used to present the watchpoint hit to the user. And
1645 it must not be done later because it could display the same single
1646 watchpoint hit during multiple GDB stops. Note that the latter is
1647 relevant only to the hardware watchpoint types bp_read_watchpoint
1648 and bp_access_watchpoint. False hit by bp_hardware_watchpoint is
1649 not user-visible - its hit is suppressed if the memory content has
1650 not changed.
1651
1652 The following constraints influence the location where we can reset
1653 hardware watchpoints:
1654
1655 * target_stopped_by_watchpoint and target_stopped_data_address are
1656 called several times when GDB stops.
1657
1658 [linux]
1659 * Multiple hardware watchpoints can be hit at the same time,
1660 causing GDB to stop. GDB only presents one hardware watchpoint
1661 hit at a time as the reason for stopping, and all the other hits
1662 are presented later, one after the other, each time the user
1663 requests the execution to be resumed. Execution is not resumed
1664 for the threads still having pending hit event stored in
1665 LWP_INFO->STATUS. While the watchpoint is already removed from
1666 the inferior on the first stop the thread hit event is kept being
1667 reported from its cached value by linux_nat_stopped_data_address
1668 until the real thread resume happens after the watchpoint gets
1669 presented and thus its LWP_INFO->STATUS gets reset.
1670
1671 Therefore the hardware watchpoint hit can get safely reset on the
1672 watchpoint removal from inferior. */
a79d3c27 1673
b40ce68a 1674static void
3a5c3e22 1675update_watchpoint (struct watchpoint *b, int reparse)
7270d8f2 1676{
a5606eee 1677 int within_current_scope;
a5606eee 1678 struct frame_id saved_frame_id;
66076460 1679 int frame_saved;
a5606eee 1680
f6bc2008
PA
1681 /* If this is a local watchpoint, we only want to check if the
1682 watchpoint frame is in scope if the current thread is the thread
1683 that was used to create the watchpoint. */
1684 if (!watchpoint_in_thread_scope (b))
1685 return;
1686
c1fc2657 1687 if (b->disposition == disp_del_at_next_stop)
a5606eee
VP
1688 return;
1689
66076460 1690 frame_saved = 0;
a5606eee
VP
1691
1692 /* Determine if the watchpoint is within scope. */
1693 if (b->exp_valid_block == NULL)
1694 within_current_scope = 1;
1695 else
1696 {
b5db5dfc
UW
1697 struct frame_info *fi = get_current_frame ();
1698 struct gdbarch *frame_arch = get_frame_arch (fi);
1699 CORE_ADDR frame_pc = get_frame_pc (fi);
1700
c9cf6e20
MG
1701 /* If we're at a point where the stack has been destroyed
1702 (e.g. in a function epilogue), unwinding may not work
1703 properly. Do not attempt to recreate locations at this
b5db5dfc 1704 point. See similar comments in watchpoint_check. */
c9cf6e20 1705 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
b5db5dfc 1706 return;
66076460
DJ
1707
1708 /* Save the current frame's ID so we can restore it after
1709 evaluating the watchpoint expression on its own frame. */
1710 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
1711 took a frame parameter, so that we didn't have to change the
1712 selected frame. */
1713 frame_saved = 1;
1714 saved_frame_id = get_frame_id (get_selected_frame (NULL));
1715
a5606eee
VP
1716 fi = frame_find_by_id (b->watchpoint_frame);
1717 within_current_scope = (fi != NULL);
1718 if (within_current_scope)
1719 select_frame (fi);
1720 }
1721
b5db5dfc
UW
1722 /* We don't free locations. They are stored in the bp_location array
1723 and update_global_location_list will eventually delete them and
1724 remove breakpoints if needed. */
c1fc2657 1725 b->loc = NULL;
b5db5dfc 1726
a5606eee
VP
1727 if (within_current_scope && reparse)
1728 {
bbc13ae3 1729 const char *s;
d63d0675 1730
4d01a485 1731 b->exp.reset ();
d63d0675 1732 s = b->exp_string_reparse ? b->exp_string_reparse : b->exp_string;
1bb9788d 1733 b->exp = parse_exp_1 (&s, 0, b->exp_valid_block, 0);
a5606eee
VP
1734 /* If the meaning of expression itself changed, the old value is
1735 no longer relevant. We don't want to report a watchpoint hit
1736 to the user when the old value and the new value may actually
1737 be completely different objects. */
fa4727a6
DJ
1738 b->val = NULL;
1739 b->val_valid = 0;
60e1c644
PA
1740
1741 /* Note that unlike with breakpoints, the watchpoint's condition
1742 expression is stored in the breakpoint object, not in the
1743 locations (re)created below. */
c1fc2657 1744 if (b->cond_string != NULL)
60e1c644 1745 {
4d01a485 1746 b->cond_exp.reset ();
60e1c644 1747
c1fc2657 1748 s = b->cond_string;
1bb9788d 1749 b->cond_exp = parse_exp_1 (&s, 0, b->cond_exp_valid_block, 0);
60e1c644 1750 }
a5606eee 1751 }
a5606eee
VP
1752
1753 /* If we failed to parse the expression, for example because
1754 it refers to a global variable in a not-yet-loaded shared library,
1755 don't try to insert watchpoint. We don't automatically delete
1756 such watchpoint, though, since failure to parse expression
1757 is different from out-of-scope watchpoint. */
e8369a73 1758 if (!target_has_execution)
2d134ed3
PA
1759 {
1760 /* Without execution, memory can't change. No use to try and
1761 set watchpoint locations. The watchpoint will be reset when
1762 the target gains execution, through breakpoint_re_set. */
e8369a73
AB
1763 if (!can_use_hw_watchpoints)
1764 {
c1fc2657
SM
1765 if (b->ops->works_in_software_mode (b))
1766 b->type = bp_watchpoint;
e8369a73 1767 else
638aa5a1
AB
1768 error (_("Can't set read/access watchpoint when "
1769 "hardware watchpoints are disabled."));
e8369a73 1770 }
2d134ed3
PA
1771 }
1772 else if (within_current_scope && b->exp)
a5606eee 1773 {
0cf6dd15 1774 int pc = 0;
a6535de1 1775 std::vector<value_ref_ptr> val_chain;
8d49165d 1776 struct value *v, *result;
2d134ed3 1777 struct program_space *frame_pspace;
a5606eee 1778
4d01a485 1779 fetch_subexp_value (b->exp.get (), &pc, &v, &result, &val_chain, 0);
a5606eee 1780
a5606eee
VP
1781 /* Avoid setting b->val if it's already set. The meaning of
1782 b->val is 'the last value' user saw, and we should update
1783 it only if we reported that last value to user. As it
9c06b0b4
TJB
1784 happens, the code that reports it updates b->val directly.
1785 We don't keep track of the memory value for masked
1786 watchpoints. */
c1fc2657 1787 if (!b->val_valid && !is_masked_watchpoint (b))
fa4727a6 1788 {
bb9d5f81 1789 if (b->val_bitsize != 0)
850645cf
TT
1790 v = extract_bitfield_from_watchpoint_value (b, v);
1791 b->val = release_value (v);
fa4727a6
DJ
1792 b->val_valid = 1;
1793 }
a5606eee 1794
2d134ed3
PA
1795 frame_pspace = get_frame_program_space (get_selected_frame (NULL));
1796
a5606eee 1797 /* Look at each value on the value chain. */
a6535de1
TT
1798 gdb_assert (!val_chain.empty ());
1799 for (const value_ref_ptr &iter : val_chain)
a5606eee 1800 {
a6535de1
TT
1801 v = iter.get ();
1802
a5606eee
VP
1803 /* If it's a memory location, and GDB actually needed
1804 its contents to evaluate the expression, then we
fa4727a6
DJ
1805 must watch it. If the first value returned is
1806 still lazy, that means an error occurred reading it;
1807 watch it anyway in case it becomes readable. */
a5606eee 1808 if (VALUE_LVAL (v) == lval_memory
a6535de1 1809 && (v == val_chain[0] || ! value_lazy (v)))
a5606eee
VP
1810 {
1811 struct type *vtype = check_typedef (value_type (v));
7270d8f2 1812
a5606eee
VP
1813 /* We only watch structs and arrays if user asked
1814 for it explicitly, never if they just happen to
1815 appear in the middle of some value chain. */
fa4727a6 1816 if (v == result
a5606eee
VP
1817 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
1818 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
1819 {
1820 CORE_ADDR addr;
f486487f 1821 enum target_hw_bp_type type;
a5606eee 1822 struct bp_location *loc, **tmp;
bb9d5f81
PP
1823 int bitpos = 0, bitsize = 0;
1824
1825 if (value_bitsize (v) != 0)
1826 {
1827 /* Extract the bit parameters out from the bitfield
1828 sub-expression. */
1829 bitpos = value_bitpos (v);
1830 bitsize = value_bitsize (v);
1831 }
1832 else if (v == result && b->val_bitsize != 0)
1833 {
1834 /* If VAL_BITSIZE != 0 then RESULT is actually a bitfield
1835 lvalue whose bit parameters are saved in the fields
1836 VAL_BITPOS and VAL_BITSIZE. */
1837 bitpos = b->val_bitpos;
1838 bitsize = b->val_bitsize;
1839 }
a5606eee 1840
42ae5230 1841 addr = value_address (v);
bb9d5f81
PP
1842 if (bitsize != 0)
1843 {
1844 /* Skip the bytes that don't contain the bitfield. */
1845 addr += bitpos / 8;
1846 }
1847
a5606eee 1848 type = hw_write;
c1fc2657 1849 if (b->type == bp_read_watchpoint)
a5606eee 1850 type = hw_read;
c1fc2657 1851 else if (b->type == bp_access_watchpoint)
a5606eee 1852 type = hw_access;
3a5c3e22 1853
c1fc2657
SM
1854 loc = allocate_bp_location (b);
1855 for (tmp = &(b->loc); *tmp != NULL; tmp = &((*tmp)->next))
a5606eee
VP
1856 ;
1857 *tmp = loc;
a6d9a66e 1858 loc->gdbarch = get_type_arch (value_type (v));
6c95b8df
PA
1859
1860 loc->pspace = frame_pspace;
f17d9474 1861 loc->address = address_significant (loc->gdbarch, addr);
bb9d5f81
PP
1862
1863 if (bitsize != 0)
1864 {
1865 /* Just cover the bytes that make up the bitfield. */
1866 loc->length = ((bitpos % 8) + bitsize + 7) / 8;
1867 }
1868 else
1869 loc->length = TYPE_LENGTH (value_type (v));
1870
a5606eee
VP
1871 loc->watchpoint_type = type;
1872 }
1873 }
9fa40276
TJB
1874 }
1875
1876 /* Change the type of breakpoint between hardware assisted or
1877 an ordinary watchpoint depending on the hardware support
1878 and free hardware slots. REPARSE is set when the inferior
1879 is started. */
a9634178 1880 if (reparse)
9fa40276 1881 {
e09342b5 1882 int reg_cnt;
9fa40276
TJB
1883 enum bp_loc_type loc_type;
1884 struct bp_location *bl;
a5606eee 1885
a9634178 1886 reg_cnt = can_use_hardware_watchpoint (val_chain);
e09342b5
TJB
1887
1888 if (reg_cnt)
9fa40276
TJB
1889 {
1890 int i, target_resources_ok, other_type_used;
a1398e0c 1891 enum bptype type;
9fa40276 1892
a9634178
TJB
1893 /* Use an exact watchpoint when there's only one memory region to be
1894 watched, and only one debug register is needed to watch it. */
1895 b->exact = target_exact_watchpoints && reg_cnt == 1;
1896
9fa40276 1897 /* We need to determine how many resources are already
e09342b5
TJB
1898 used for all other hardware watchpoints plus this one
1899 to see if we still have enough resources to also fit
a1398e0c
PA
1900 this watchpoint in as well. */
1901
1902 /* If this is a software watchpoint, we try to turn it
1903 to a hardware one -- count resources as if B was of
1904 hardware watchpoint type. */
c1fc2657 1905 type = b->type;
a1398e0c
PA
1906 if (type == bp_watchpoint)
1907 type = bp_hardware_watchpoint;
1908
1909 /* This watchpoint may or may not have been placed on
1910 the list yet at this point (it won't be in the list
1911 if we're trying to create it for the first time,
1912 through watch_command), so always account for it
1913 manually. */
1914
1915 /* Count resources used by all watchpoints except B. */
c1fc2657 1916 i = hw_watchpoint_used_count_others (b, type, &other_type_used);
a1398e0c
PA
1917
1918 /* Add in the resources needed for B. */
c1fc2657 1919 i += hw_watchpoint_use_count (b);
a1398e0c
PA
1920
1921 target_resources_ok
1922 = target_can_use_hardware_watchpoint (type, i, other_type_used);
e09342b5 1923 if (target_resources_ok <= 0)
a9634178 1924 {
c1fc2657 1925 int sw_mode = b->ops->works_in_software_mode (b);
9c06b0b4
TJB
1926
1927 if (target_resources_ok == 0 && !sw_mode)
a9634178
TJB
1928 error (_("Target does not support this type of "
1929 "hardware watchpoint."));
9c06b0b4
TJB
1930 else if (target_resources_ok < 0 && !sw_mode)
1931 error (_("There are not enough available hardware "
1932 "resources for this watchpoint."));
a1398e0c
PA
1933
1934 /* Downgrade to software watchpoint. */
c1fc2657 1935 b->type = bp_watchpoint;
a1398e0c
PA
1936 }
1937 else
1938 {
1939 /* If this was a software watchpoint, we've just
1940 found we have enough resources to turn it to a
1941 hardware watchpoint. Otherwise, this is a
1942 nop. */
c1fc2657 1943 b->type = type;
a9634178 1944 }
9fa40276 1945 }
c1fc2657 1946 else if (!b->ops->works_in_software_mode (b))
638aa5a1
AB
1947 {
1948 if (!can_use_hw_watchpoints)
1949 error (_("Can't set read/access watchpoint when "
1950 "hardware watchpoints are disabled."));
1951 else
1952 error (_("Expression cannot be implemented with "
1953 "read/access watchpoint."));
1954 }
9fa40276 1955 else
c1fc2657 1956 b->type = bp_watchpoint;
9fa40276 1957
c1fc2657 1958 loc_type = (b->type == bp_watchpoint? bp_loc_other
9fa40276 1959 : bp_loc_hardware_watchpoint);
c1fc2657 1960 for (bl = b->loc; bl; bl = bl->next)
9fa40276
TJB
1961 bl->loc_type = loc_type;
1962 }
1963
c7437ca6
PA
1964 /* If a software watchpoint is not watching any memory, then the
1965 above left it without any location set up. But,
1966 bpstat_stop_status requires a location to be able to report
1967 stops, so make sure there's at least a dummy one. */
c1fc2657
SM
1968 if (b->type == bp_watchpoint && b->loc == NULL)
1969 software_watchpoint_add_no_memory_location (b, frame_pspace);
a5606eee
VP
1970 }
1971 else if (!within_current_scope)
7270d8f2 1972 {
ac74f770
MS
1973 printf_filtered (_("\
1974Watchpoint %d deleted because the program has left the block\n\
1975in which its expression is valid.\n"),
c1fc2657 1976 b->number);
d0fb5eae 1977 watchpoint_del_at_next_stop (b);
7270d8f2 1978 }
a5606eee
VP
1979
1980 /* Restore the selected frame. */
66076460
DJ
1981 if (frame_saved)
1982 select_frame (frame_find_by_id (saved_frame_id));
7270d8f2
OF
1983}
1984
a5606eee 1985
74960c60 1986/* Returns 1 iff breakpoint location should be
1e4d1764
YQ
1987 inserted in the inferior. We don't differentiate the type of BL's owner
1988 (breakpoint vs. tracepoint), although insert_location in tracepoint's
1989 breakpoint_ops is not defined, because in insert_bp_location,
1990 tracepoint's insert_location will not be called. */
74960c60 1991static int
35df4500 1992should_be_inserted (struct bp_location *bl)
74960c60 1993{
35df4500 1994 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
74960c60
VP
1995 return 0;
1996
35df4500 1997 if (bl->owner->disposition == disp_del_at_next_stop)
74960c60
VP
1998 return 0;
1999
35df4500 2000 if (!bl->enabled || bl->shlib_disabled || bl->duplicate)
74960c60
VP
2001 return 0;
2002
f8eba3c6
TT
2003 if (user_breakpoint_p (bl->owner) && bl->pspace->executing_startup)
2004 return 0;
2005
56710373
PA
2006 /* This is set for example, when we're attached to the parent of a
2007 vfork, and have detached from the child. The child is running
2008 free, and we expect it to do an exec or exit, at which point the
2009 OS makes the parent schedulable again (and the target reports
2010 that the vfork is done). Until the child is done with the shared
2011 memory region, do not insert breakpoints in the parent, otherwise
2012 the child could still trip on the parent's breakpoints. Since
2013 the parent is blocked anyway, it won't miss any breakpoint. */
35df4500 2014 if (bl->pspace->breakpoints_not_allowed)
56710373
PA
2015 return 0;
2016
31e77af2 2017 /* Don't insert a breakpoint if we're trying to step past its
21edc42f
YQ
2018 location, except if the breakpoint is a single-step breakpoint,
2019 and the breakpoint's thread is the thread which is stepping past
2020 a breakpoint. */
31e77af2
PA
2021 if ((bl->loc_type == bp_loc_software_breakpoint
2022 || bl->loc_type == bp_loc_hardware_breakpoint)
2023 && stepping_past_instruction_at (bl->pspace->aspace,
21edc42f
YQ
2024 bl->address)
2025 /* The single-step breakpoint may be inserted at the location
2026 we're trying to step if the instruction branches to itself.
2027 However, the instruction won't be executed at all and it may
2028 break the semantics of the instruction, for example, the
2029 instruction is a conditional branch or updates some flags.
2030 We can't fix it unless GDB is able to emulate the instruction
2031 or switch to displaced stepping. */
2032 && !(bl->owner->type == bp_single_step
2033 && thread_is_stepping_over_breakpoint (bl->owner->thread)))
e558d7c1
PA
2034 {
2035 if (debug_infrun)
2036 {
2037 fprintf_unfiltered (gdb_stdlog,
2038 "infrun: skipping breakpoint: "
2039 "stepping past insn at: %s\n",
2040 paddress (bl->gdbarch, bl->address));
2041 }
2042 return 0;
2043 }
31e77af2 2044
963f9c80
PA
2045 /* Don't insert watchpoints if we're trying to step past the
2046 instruction that triggered one. */
2047 if ((bl->loc_type == bp_loc_hardware_watchpoint)
2048 && stepping_past_nonsteppable_watchpoint ())
2049 {
2050 if (debug_infrun)
2051 {
2052 fprintf_unfiltered (gdb_stdlog,
2053 "infrun: stepping past non-steppable watchpoint. "
2054 "skipping watchpoint at %s:%d\n",
2055 paddress (bl->gdbarch, bl->address),
2056 bl->length);
2057 }
2058 return 0;
2059 }
2060
74960c60
VP
2061 return 1;
2062}
2063
934709f0
PW
2064/* Same as should_be_inserted but does the check assuming
2065 that the location is not duplicated. */
2066
2067static int
2068unduplicated_should_be_inserted (struct bp_location *bl)
2069{
2070 int result;
2071 const int save_duplicate = bl->duplicate;
2072
2073 bl->duplicate = 0;
2074 result = should_be_inserted (bl);
2075 bl->duplicate = save_duplicate;
2076 return result;
2077}
2078
b775012e
LM
2079/* Parses a conditional described by an expression COND into an
2080 agent expression bytecode suitable for evaluation
2081 by the bytecode interpreter. Return NULL if there was
2082 any error during parsing. */
2083
833177a4 2084static agent_expr_up
b775012e
LM
2085parse_cond_to_aexpr (CORE_ADDR scope, struct expression *cond)
2086{
833177a4 2087 if (cond == NULL)
b775012e
LM
2088 return NULL;
2089
833177a4
PA
2090 agent_expr_up aexpr;
2091
b775012e
LM
2092 /* We don't want to stop processing, so catch any errors
2093 that may show up. */
492d29ea 2094 TRY
b775012e 2095 {
036e657b 2096 aexpr = gen_eval_for_expr (scope, cond);
b775012e
LM
2097 }
2098
492d29ea 2099 CATCH (ex, RETURN_MASK_ERROR)
b775012e
LM
2100 {
2101 /* If we got here, it means the condition could not be parsed to a valid
2102 bytecode expression and thus can't be evaluated on the target's side.
2103 It's no use iterating through the conditions. */
b775012e 2104 }
492d29ea 2105 END_CATCH
b775012e
LM
2106
2107 /* We have a valid agent expression. */
2108 return aexpr;
2109}
2110
2111/* Based on location BL, create a list of breakpoint conditions to be
2112 passed on to the target. If we have duplicated locations with different
2113 conditions, we will add such conditions to the list. The idea is that the
2114 target will evaluate the list of conditions and will only notify GDB when
2115 one of them is true. */
2116
2117static void
2118build_target_condition_list (struct bp_location *bl)
2119{
2120 struct bp_location **locp = NULL, **loc2p;
2121 int null_condition_or_parse_error = 0;
2122 int modified = bl->needs_update;
2123 struct bp_location *loc;
2124
8b4f3082 2125 /* Release conditions left over from a previous insert. */
3cde5c42 2126 bl->target_info.conditions.clear ();
8b4f3082 2127
b775012e
LM
2128 /* This is only meaningful if the target is
2129 evaluating conditions and if the user has
2130 opted for condition evaluation on the target's
2131 side. */
2132 if (gdb_evaluates_breakpoint_condition_p ()
2133 || !target_supports_evaluation_of_breakpoint_conditions ())
2134 return;
2135
2136 /* Do a first pass to check for locations with no assigned
2137 conditions or conditions that fail to parse to a valid agent expression
2138 bytecode. If any of these happen, then it's no use to send conditions
2139 to the target since this location will always trigger and generate a
2140 response back to GDB. */
2141 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2142 {
2143 loc = (*loc2p);
2144 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2145 {
2146 if (modified)
2147 {
b775012e
LM
2148 /* Re-parse the conditions since something changed. In that
2149 case we already freed the condition bytecodes (see
2150 force_breakpoint_reinsertion). We just
2151 need to parse the condition to bytecodes again. */
833177a4
PA
2152 loc->cond_bytecode = parse_cond_to_aexpr (bl->address,
2153 loc->cond.get ());
b775012e
LM
2154 }
2155
2156 /* If we have a NULL bytecode expression, it means something
2157 went wrong or we have a null condition expression. */
2158 if (!loc->cond_bytecode)
2159 {
2160 null_condition_or_parse_error = 1;
2161 break;
2162 }
2163 }
2164 }
2165
2166 /* If any of these happened, it means we will have to evaluate the conditions
2167 for the location's address on gdb's side. It is no use keeping bytecodes
2168 for all the other duplicate locations, thus we free all of them here.
2169
2170 This is so we have a finer control over which locations' conditions are
2171 being evaluated by GDB or the remote stub. */
2172 if (null_condition_or_parse_error)
2173 {
2174 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2175 {
2176 loc = (*loc2p);
2177 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2178 {
2179 /* Only go as far as the first NULL bytecode is
2180 located. */
2181 if (!loc->cond_bytecode)
2182 return;
2183
833177a4 2184 loc->cond_bytecode.reset ();
b775012e
LM
2185 }
2186 }
2187 }
2188
2189 /* No NULL conditions or failed bytecode generation. Build a condition list
2190 for this location's address. */
2191 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2192 {
2193 loc = (*loc2p);
2194 if (loc->cond
2195 && is_breakpoint (loc->owner)
2196 && loc->pspace->num == bl->pspace->num
2197 && loc->owner->enable_state == bp_enabled
2198 && loc->enabled)
3cde5c42
PA
2199 {
2200 /* Add the condition to the vector. This will be used later
2201 to send the conditions to the target. */
2202 bl->target_info.conditions.push_back (loc->cond_bytecode.get ());
2203 }
b775012e
LM
2204 }
2205
2206 return;
2207}
2208
d3ce09f5
SS
2209/* Parses a command described by string CMD into an agent expression
2210 bytecode suitable for evaluation by the bytecode interpreter.
2211 Return NULL if there was any error during parsing. */
2212
833177a4 2213static agent_expr_up
d3ce09f5
SS
2214parse_cmd_to_aexpr (CORE_ADDR scope, char *cmd)
2215{
bbc13ae3
KS
2216 const char *cmdrest;
2217 const char *format_start, *format_end;
d3ce09f5
SS
2218 struct gdbarch *gdbarch = get_current_arch ();
2219
833177a4 2220 if (cmd == NULL)
d3ce09f5
SS
2221 return NULL;
2222
2223 cmdrest = cmd;
2224
2225 if (*cmdrest == ',')
2226 ++cmdrest;
f1735a53 2227 cmdrest = skip_spaces (cmdrest);
d3ce09f5
SS
2228
2229 if (*cmdrest++ != '"')
2230 error (_("No format string following the location"));
2231
2232 format_start = cmdrest;
2233
8e481c3b 2234 format_pieces fpieces (&cmdrest);
d3ce09f5
SS
2235
2236 format_end = cmdrest;
2237
2238 if (*cmdrest++ != '"')
2239 error (_("Bad format string, non-terminated '\"'."));
2240
f1735a53 2241 cmdrest = skip_spaces (cmdrest);
d3ce09f5
SS
2242
2243 if (!(*cmdrest == ',' || *cmdrest == '\0'))
2244 error (_("Invalid argument syntax"));
2245
2246 if (*cmdrest == ',')
2247 cmdrest++;
f1735a53 2248 cmdrest = skip_spaces (cmdrest);
d3ce09f5
SS
2249
2250 /* For each argument, make an expression. */
2251
8e481c3b 2252 std::vector<struct expression *> argvec;
d3ce09f5
SS
2253 while (*cmdrest != '\0')
2254 {
bbc13ae3 2255 const char *cmd1;
d3ce09f5
SS
2256
2257 cmd1 = cmdrest;
4d01a485 2258 expression_up expr = parse_exp_1 (&cmd1, scope, block_for_pc (scope), 1);
8e481c3b 2259 argvec.push_back (expr.release ());
d3ce09f5
SS
2260 cmdrest = cmd1;
2261 if (*cmdrest == ',')
2262 ++cmdrest;
2263 }
2264
833177a4
PA
2265 agent_expr_up aexpr;
2266
d3ce09f5
SS
2267 /* We don't want to stop processing, so catch any errors
2268 that may show up. */
492d29ea 2269 TRY
d3ce09f5 2270 {
036e657b
JB
2271 aexpr = gen_printf (scope, gdbarch, 0, 0,
2272 format_start, format_end - format_start,
8e481c3b 2273 argvec.size (), argvec.data ());
d3ce09f5 2274 }
492d29ea 2275 CATCH (ex, RETURN_MASK_ERROR)
d3ce09f5
SS
2276 {
2277 /* If we got here, it means the command could not be parsed to a valid
2278 bytecode expression and thus can't be evaluated on the target's side.
2279 It's no use iterating through the other commands. */
d3ce09f5 2280 }
492d29ea
PA
2281 END_CATCH
2282
d3ce09f5
SS
2283 /* We have a valid agent expression, return it. */
2284 return aexpr;
2285}
2286
2287/* Based on location BL, create a list of breakpoint commands to be
2288 passed on to the target. If we have duplicated locations with
2289 different commands, we will add any such to the list. */
2290
2291static void
2292build_target_command_list (struct bp_location *bl)
2293{
2294 struct bp_location **locp = NULL, **loc2p;
2295 int null_command_or_parse_error = 0;
2296 int modified = bl->needs_update;
2297 struct bp_location *loc;
2298
3cde5c42
PA
2299 /* Clear commands left over from a previous insert. */
2300 bl->target_info.tcommands.clear ();
8b4f3082 2301
41fac0cf 2302 if (!target_can_run_breakpoint_commands ())
d3ce09f5
SS
2303 return;
2304
41fac0cf
PA
2305 /* For now, limit to agent-style dprintf breakpoints. */
2306 if (dprintf_style != dprintf_style_agent)
d3ce09f5
SS
2307 return;
2308
41fac0cf
PA
2309 /* For now, if we have any duplicate location that isn't a dprintf,
2310 don't install the target-side commands, as that would make the
2311 breakpoint not be reported to the core, and we'd lose
2312 control. */
2313 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2314 {
2315 loc = (*loc2p);
2316 if (is_breakpoint (loc->owner)
2317 && loc->pspace->num == bl->pspace->num
2318 && loc->owner->type != bp_dprintf)
2319 return;
2320 }
2321
d3ce09f5
SS
2322 /* Do a first pass to check for locations with no assigned
2323 conditions or conditions that fail to parse to a valid agent expression
2324 bytecode. If any of these happen, then it's no use to send conditions
2325 to the target since this location will always trigger and generate a
2326 response back to GDB. */
2327 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2328 {
2329 loc = (*loc2p);
2330 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2331 {
2332 if (modified)
2333 {
d3ce09f5
SS
2334 /* Re-parse the commands since something changed. In that
2335 case we already freed the command bytecodes (see
2336 force_breakpoint_reinsertion). We just
2337 need to parse the command to bytecodes again. */
833177a4
PA
2338 loc->cmd_bytecode
2339 = parse_cmd_to_aexpr (bl->address,
2340 loc->owner->extra_string);
d3ce09f5
SS
2341 }
2342
2343 /* If we have a NULL bytecode expression, it means something
2344 went wrong or we have a null command expression. */
2345 if (!loc->cmd_bytecode)
2346 {
2347 null_command_or_parse_error = 1;
2348 break;
2349 }
2350 }
2351 }
2352
2353 /* If anything failed, then we're not doing target-side commands,
2354 and so clean up. */
2355 if (null_command_or_parse_error)
2356 {
2357 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2358 {
2359 loc = (*loc2p);
2360 if (is_breakpoint (loc->owner)
2361 && loc->pspace->num == bl->pspace->num)
2362 {
2363 /* Only go as far as the first NULL bytecode is
2364 located. */
40fb6c5e 2365 if (loc->cmd_bytecode == NULL)
d3ce09f5
SS
2366 return;
2367
833177a4 2368 loc->cmd_bytecode.reset ();
d3ce09f5
SS
2369 }
2370 }
2371 }
2372
2373 /* No NULL commands or failed bytecode generation. Build a command list
2374 for this location's address. */
2375 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2376 {
2377 loc = (*loc2p);
2378 if (loc->owner->extra_string
2379 && is_breakpoint (loc->owner)
2380 && loc->pspace->num == bl->pspace->num
2381 && loc->owner->enable_state == bp_enabled
2382 && loc->enabled)
3cde5c42
PA
2383 {
2384 /* Add the command to the vector. This will be used later
2385 to send the commands to the target. */
2386 bl->target_info.tcommands.push_back (loc->cmd_bytecode.get ());
2387 }
d3ce09f5
SS
2388 }
2389
2390 bl->target_info.persist = 0;
2391 /* Maybe flag this location as persistent. */
2392 if (bl->owner->type == bp_dprintf && disconnected_dprintf)
2393 bl->target_info.persist = 1;
2394}
2395
833b7ab5
YQ
2396/* Return the kind of breakpoint on address *ADDR. Get the kind
2397 of breakpoint according to ADDR except single-step breakpoint.
2398 Get the kind of single-step breakpoint according to the current
2399 registers state. */
cd6c3b4f
YQ
2400
2401static int
2402breakpoint_kind (struct bp_location *bl, CORE_ADDR *addr)
2403{
833b7ab5
YQ
2404 if (bl->owner->type == bp_single_step)
2405 {
2406 struct thread_info *thr = find_thread_global_id (bl->owner->thread);
2407 struct regcache *regcache;
2408
00431a78 2409 regcache = get_thread_regcache (thr);
833b7ab5
YQ
2410
2411 return gdbarch_breakpoint_kind_from_current_state (bl->gdbarch,
2412 regcache, addr);
2413 }
2414 else
2415 return gdbarch_breakpoint_kind_from_pc (bl->gdbarch, addr);
cd6c3b4f
YQ
2416}
2417
35df4500
TJB
2418/* Insert a low-level "breakpoint" of some type. BL is the breakpoint
2419 location. Any error messages are printed to TMP_ERROR_STREAM; and
3fbb6ffa 2420 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
c30eee59
TJB
2421 Returns 0 for success, 1 if the bp_location type is not supported or
2422 -1 for failure.
879bfdc2 2423
4a64f543
MS
2424 NOTE drow/2003-09-09: This routine could be broken down to an
2425 object-style method for each breakpoint or catchpoint type. */
26bb91f3 2426static int
35df4500 2427insert_bp_location (struct bp_location *bl,
26bb91f3 2428 struct ui_file *tmp_error_stream,
3fbb6ffa 2429 int *disabled_breaks,
dd61ec5c
MW
2430 int *hw_breakpoint_error,
2431 int *hw_bp_error_explained_already)
879bfdc2 2432{
688fca4f 2433 gdb_exception bp_excpt = exception_none;
879bfdc2 2434
b775012e 2435 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
879bfdc2
DJ
2436 return 0;
2437
35c63cd8
JB
2438 /* Note we don't initialize bl->target_info, as that wipes out
2439 the breakpoint location's shadow_contents if the breakpoint
2440 is still inserted at that location. This in turn breaks
2441 target_read_memory which depends on these buffers when
2442 a memory read is requested at the breakpoint location:
2443 Once the target_info has been wiped, we fail to see that
2444 we have a breakpoint inserted at that address and thus
2445 read the breakpoint instead of returning the data saved in
2446 the breakpoint location's shadow contents. */
0d5ed153 2447 bl->target_info.reqstd_address = bl->address;
35df4500 2448 bl->target_info.placed_address_space = bl->pspace->aspace;
f1310107 2449 bl->target_info.length = bl->length;
8181d85f 2450
b775012e
LM
2451 /* When working with target-side conditions, we must pass all the conditions
2452 for the same breakpoint address down to the target since GDB will not
2453 insert those locations. With a list of breakpoint conditions, the target
2454 can decide when to stop and notify GDB. */
2455
2456 if (is_breakpoint (bl->owner))
2457 {
2458 build_target_condition_list (bl);
d3ce09f5
SS
2459 build_target_command_list (bl);
2460 /* Reset the modification marker. */
b775012e
LM
2461 bl->needs_update = 0;
2462 }
2463
35df4500
TJB
2464 if (bl->loc_type == bp_loc_software_breakpoint
2465 || bl->loc_type == bp_loc_hardware_breakpoint)
879bfdc2 2466 {
35df4500 2467 if (bl->owner->type != bp_hardware_breakpoint)
765dc015
VP
2468 {
2469 /* If the explicitly specified breakpoint type
2470 is not hardware breakpoint, check the memory map to see
2471 if the breakpoint address is in read only memory or not.
4a64f543 2472
765dc015
VP
2473 Two important cases are:
2474 - location type is not hardware breakpoint, memory
2475 is readonly. We change the type of the location to
2476 hardware breakpoint.
4a64f543
MS
2477 - location type is hardware breakpoint, memory is
2478 read-write. This means we've previously made the
2479 location hardware one, but then the memory map changed,
2480 so we undo.
765dc015 2481
4a64f543
MS
2482 When breakpoints are removed, remove_breakpoints will use
2483 location types we've just set here, the only possible
2484 problem is that memory map has changed during running
2485 program, but it's not going to work anyway with current
2486 gdb. */
765dc015 2487 struct mem_region *mr
0d5ed153 2488 = lookup_mem_region (bl->target_info.reqstd_address);
765dc015
VP
2489
2490 if (mr)
2491 {
2492 if (automatic_hardware_breakpoints)
2493 {
765dc015
VP
2494 enum bp_loc_type new_type;
2495
2496 if (mr->attrib.mode != MEM_RW)
2497 new_type = bp_loc_hardware_breakpoint;
2498 else
2499 new_type = bp_loc_software_breakpoint;
2500
35df4500 2501 if (new_type != bl->loc_type)
765dc015
VP
2502 {
2503 static int said = 0;
cc59ec59 2504
35df4500 2505 bl->loc_type = new_type;
765dc015
VP
2506 if (!said)
2507 {
3e43a32a
MS
2508 fprintf_filtered (gdb_stdout,
2509 _("Note: automatically using "
2510 "hardware breakpoints for "
2511 "read-only addresses.\n"));
765dc015
VP
2512 said = 1;
2513 }
2514 }
2515 }
35df4500 2516 else if (bl->loc_type == bp_loc_software_breakpoint
0fec99e8
PA
2517 && mr->attrib.mode != MEM_RW)
2518 {
2519 fprintf_unfiltered (tmp_error_stream,
2520 _("Cannot insert breakpoint %d.\n"
2521 "Cannot set software breakpoint "
2522 "at read-only address %s\n"),
2523 bl->owner->number,
2524 paddress (bl->gdbarch, bl->address));
2525 return 1;
2526 }
765dc015
VP
2527 }
2528 }
2529
879bfdc2
DJ
2530 /* First check to see if we have to handle an overlay. */
2531 if (overlay_debugging == ovly_off
35df4500
TJB
2532 || bl->section == NULL
2533 || !(section_is_overlay (bl->section)))
879bfdc2
DJ
2534 {
2535 /* No overlay handling: just set the breakpoint. */
492d29ea 2536 TRY
dd61ec5c 2537 {
0000e5cc
PA
2538 int val;
2539
dd61ec5c 2540 val = bl->owner->ops->insert_location (bl);
0000e5cc 2541 if (val)
688fca4f 2542 bp_excpt = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
dd61ec5c 2543 }
492d29ea 2544 CATCH (e, RETURN_MASK_ALL)
dd61ec5c 2545 {
688fca4f 2546 bp_excpt = e;
dd61ec5c 2547 }
492d29ea 2548 END_CATCH
879bfdc2
DJ
2549 }
2550 else
2551 {
4a64f543 2552 /* This breakpoint is in an overlay section.
879bfdc2
DJ
2553 Shall we set a breakpoint at the LMA? */
2554 if (!overlay_events_enabled)
2555 {
2556 /* Yes -- overlay event support is not active,
2557 so we must try to set a breakpoint at the LMA.
2558 This will not work for a hardware breakpoint. */
35df4500 2559 if (bl->loc_type == bp_loc_hardware_breakpoint)
8a3fe4f8 2560 warning (_("hardware breakpoint %d not supported in overlay!"),
35df4500 2561 bl->owner->number);
879bfdc2
DJ
2562 else
2563 {
35df4500
TJB
2564 CORE_ADDR addr = overlay_unmapped_address (bl->address,
2565 bl->section);
879bfdc2 2566 /* Set a software (trap) breakpoint at the LMA. */
35df4500 2567 bl->overlay_target_info = bl->target_info;
0d5ed153 2568 bl->overlay_target_info.reqstd_address = addr;
0000e5cc
PA
2569
2570 /* No overlay handling: just set the breakpoint. */
492d29ea 2571 TRY
0000e5cc
PA
2572 {
2573 int val;
2574
579c6ad9 2575 bl->overlay_target_info.kind
cd6c3b4f
YQ
2576 = breakpoint_kind (bl, &addr);
2577 bl->overlay_target_info.placed_address = addr;
0000e5cc
PA
2578 val = target_insert_breakpoint (bl->gdbarch,
2579 &bl->overlay_target_info);
2580 if (val)
688fca4f
PA
2581 bp_excpt
2582 = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
0000e5cc 2583 }
492d29ea 2584 CATCH (e, RETURN_MASK_ALL)
0000e5cc 2585 {
688fca4f 2586 bp_excpt = e;
0000e5cc 2587 }
492d29ea 2588 END_CATCH
0000e5cc 2589
688fca4f 2590 if (bp_excpt.reason != 0)
99361f52 2591 fprintf_unfiltered (tmp_error_stream,
3e43a32a
MS
2592 "Overlay breakpoint %d "
2593 "failed: in ROM?\n",
35df4500 2594 bl->owner->number);
879bfdc2
DJ
2595 }
2596 }
2597 /* Shall we set a breakpoint at the VMA? */
35df4500 2598 if (section_is_mapped (bl->section))
879bfdc2
DJ
2599 {
2600 /* Yes. This overlay section is mapped into memory. */
492d29ea 2601 TRY
dd61ec5c 2602 {
0000e5cc
PA
2603 int val;
2604
dd61ec5c 2605 val = bl->owner->ops->insert_location (bl);
0000e5cc 2606 if (val)
688fca4f 2607 bp_excpt = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
dd61ec5c 2608 }
492d29ea 2609 CATCH (e, RETURN_MASK_ALL)
dd61ec5c 2610 {
688fca4f 2611 bp_excpt = e;
dd61ec5c 2612 }
492d29ea 2613 END_CATCH
879bfdc2
DJ
2614 }
2615 else
2616 {
2617 /* No. This breakpoint will not be inserted.
2618 No error, but do not mark the bp as 'inserted'. */
2619 return 0;
2620 }
2621 }
2622
688fca4f 2623 if (bp_excpt.reason != 0)
879bfdc2
DJ
2624 {
2625 /* Can't set the breakpoint. */
0000e5cc
PA
2626
2627 /* In some cases, we might not be able to insert a
2628 breakpoint in a shared library that has already been
2629 removed, but we have not yet processed the shlib unload
2630 event. Unfortunately, some targets that implement
076855f9
PA
2631 breakpoint insertion themselves can't tell why the
2632 breakpoint insertion failed (e.g., the remote target
2633 doesn't define error codes), so we must treat generic
2634 errors as memory errors. */
688fca4f
PA
2635 if (bp_excpt.reason == RETURN_ERROR
2636 && (bp_excpt.error == GENERIC_ERROR
2637 || bp_excpt.error == MEMORY_ERROR)
076855f9 2638 && bl->loc_type == bp_loc_software_breakpoint
08351840 2639 && (solib_name_from_address (bl->pspace, bl->address)
d03de421
PA
2640 || shared_objfile_contains_address_p (bl->pspace,
2641 bl->address)))
879bfdc2 2642 {
4a64f543 2643 /* See also: disable_breakpoints_in_shlibs. */
35df4500 2644 bl->shlib_disabled = 1;
76727919 2645 gdb::observers::breakpoint_modified.notify (bl->owner);
3fbb6ffa
TJB
2646 if (!*disabled_breaks)
2647 {
2648 fprintf_unfiltered (tmp_error_stream,
2649 "Cannot insert breakpoint %d.\n",
2650 bl->owner->number);
2651 fprintf_unfiltered (tmp_error_stream,
2652 "Temporarily disabling shared "
2653 "library breakpoints:\n");
2654 }
2655 *disabled_breaks = 1;
879bfdc2 2656 fprintf_unfiltered (tmp_error_stream,
35df4500 2657 "breakpoint #%d\n", bl->owner->number);
0000e5cc 2658 return 0;
879bfdc2
DJ
2659 }
2660 else
879bfdc2 2661 {
35df4500 2662 if (bl->loc_type == bp_loc_hardware_breakpoint)
879bfdc2 2663 {
0000e5cc 2664 *hw_breakpoint_error = 1;
688fca4f 2665 *hw_bp_error_explained_already = bp_excpt.message != NULL;
dd61ec5c
MW
2666 fprintf_unfiltered (tmp_error_stream,
2667 "Cannot insert hardware breakpoint %d%s",
688fca4f
PA
2668 bl->owner->number,
2669 bp_excpt.message ? ":" : ".\n");
2670 if (bp_excpt.message != NULL)
2671 fprintf_unfiltered (tmp_error_stream, "%s.\n",
2672 bp_excpt.message);
879bfdc2
DJ
2673 }
2674 else
2675 {
688fca4f 2676 if (bp_excpt.message == NULL)
0000e5cc 2677 {
1ccbe998 2678 std::string message
0000e5cc
PA
2679 = memory_error_message (TARGET_XFER_E_IO,
2680 bl->gdbarch, bl->address);
0000e5cc
PA
2681
2682 fprintf_unfiltered (tmp_error_stream,
2683 "Cannot insert breakpoint %d.\n"
2684 "%s\n",
1ccbe998 2685 bl->owner->number, message.c_str ());
0000e5cc
PA
2686 }
2687 else
2688 {
2689 fprintf_unfiltered (tmp_error_stream,
2690 "Cannot insert breakpoint %d: %s\n",
2691 bl->owner->number,
688fca4f 2692 bp_excpt.message);
0000e5cc 2693 }
879bfdc2 2694 }
0000e5cc 2695 return 1;
879bfdc2
DJ
2696
2697 }
2698 }
2699 else
35df4500 2700 bl->inserted = 1;
879bfdc2 2701
0000e5cc 2702 return 0;
879bfdc2
DJ
2703 }
2704
35df4500 2705 else if (bl->loc_type == bp_loc_hardware_watchpoint
879bfdc2 2706 /* NOTE drow/2003-09-08: This state only exists for removing
4a64f543 2707 watchpoints. It's not clear that it's necessary... */
35df4500 2708 && bl->owner->disposition != disp_del_at_next_stop)
879bfdc2 2709 {
0000e5cc
PA
2710 int val;
2711
77b06cd7
TJB
2712 gdb_assert (bl->owner->ops != NULL
2713 && bl->owner->ops->insert_location != NULL);
2714
2715 val = bl->owner->ops->insert_location (bl);
85d721b8
PA
2716
2717 /* If trying to set a read-watchpoint, and it turns out it's not
2718 supported, try emulating one with an access watchpoint. */
35df4500 2719 if (val == 1 && bl->watchpoint_type == hw_read)
85d721b8
PA
2720 {
2721 struct bp_location *loc, **loc_temp;
2722
2723 /* But don't try to insert it, if there's already another
2724 hw_access location that would be considered a duplicate
2725 of this one. */
2726 ALL_BP_LOCATIONS (loc, loc_temp)
35df4500 2727 if (loc != bl
85d721b8 2728 && loc->watchpoint_type == hw_access
35df4500 2729 && watchpoint_locations_match (bl, loc))
85d721b8 2730 {
35df4500
TJB
2731 bl->duplicate = 1;
2732 bl->inserted = 1;
2733 bl->target_info = loc->target_info;
2734 bl->watchpoint_type = hw_access;
85d721b8
PA
2735 val = 0;
2736 break;
2737 }
2738
2739 if (val == 1)
2740 {
77b06cd7
TJB
2741 bl->watchpoint_type = hw_access;
2742 val = bl->owner->ops->insert_location (bl);
2743
2744 if (val)
2745 /* Back to the original value. */
2746 bl->watchpoint_type = hw_read;
85d721b8
PA
2747 }
2748 }
2749
35df4500 2750 bl->inserted = (val == 0);
879bfdc2
DJ
2751 }
2752
35df4500 2753 else if (bl->owner->type == bp_catchpoint)
879bfdc2 2754 {
0000e5cc
PA
2755 int val;
2756
77b06cd7
TJB
2757 gdb_assert (bl->owner->ops != NULL
2758 && bl->owner->ops->insert_location != NULL);
2759
2760 val = bl->owner->ops->insert_location (bl);
2761 if (val)
2762 {
2763 bl->owner->enable_state = bp_disabled;
2764
2765 if (val == 1)
2766 warning (_("\
2767Error inserting catchpoint %d: Your system does not support this type\n\
2768of catchpoint."), bl->owner->number);
2769 else
2770 warning (_("Error inserting catchpoint %d."), bl->owner->number);
2771 }
2772
2773 bl->inserted = (val == 0);
1640b821
DJ
2774
2775 /* We've already printed an error message if there was a problem
2776 inserting this catchpoint, and we've disabled the catchpoint,
2777 so just return success. */
2778 return 0;
879bfdc2
DJ
2779 }
2780
2781 return 0;
2782}
2783
6c95b8df
PA
2784/* This function is called when program space PSPACE is about to be
2785 deleted. It takes care of updating breakpoints to not reference
2786 PSPACE anymore. */
2787
2788void
2789breakpoint_program_space_exit (struct program_space *pspace)
2790{
2791 struct breakpoint *b, *b_temp;
876fa593 2792 struct bp_location *loc, **loc_temp;
6c95b8df
PA
2793
2794 /* Remove any breakpoint that was set through this program space. */
2795 ALL_BREAKPOINTS_SAFE (b, b_temp)
2796 {
2797 if (b->pspace == pspace)
2798 delete_breakpoint (b);
2799 }
2800
2801 /* Breakpoints set through other program spaces could have locations
2802 bound to PSPACE as well. Remove those. */
876fa593 2803 ALL_BP_LOCATIONS (loc, loc_temp)
6c95b8df
PA
2804 {
2805 struct bp_location *tmp;
2806
2807 if (loc->pspace == pspace)
2808 {
2bdf28a0 2809 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
6c95b8df
PA
2810 if (loc->owner->loc == loc)
2811 loc->owner->loc = loc->next;
2812 else
2813 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
2814 if (tmp->next == loc)
2815 {
2816 tmp->next = loc->next;
2817 break;
2818 }
2819 }
2820 }
2821
2822 /* Now update the global location list to permanently delete the
2823 removed locations above. */
44702360 2824 update_global_location_list (UGLL_DONT_INSERT);
6c95b8df
PA
2825}
2826
74960c60
VP
2827/* Make sure all breakpoints are inserted in inferior.
2828 Throws exception on any error.
2829 A breakpoint that is already inserted won't be inserted
2830 again, so calling this function twice is safe. */
2831void
2832insert_breakpoints (void)
2833{
2834 struct breakpoint *bpt;
2835
2836 ALL_BREAKPOINTS (bpt)
2837 if (is_hardware_watchpoint (bpt))
3a5c3e22
PA
2838 {
2839 struct watchpoint *w = (struct watchpoint *) bpt;
2840
2841 update_watchpoint (w, 0 /* don't reparse. */);
2842 }
74960c60 2843
04086b45
PA
2844 /* Updating watchpoints creates new locations, so update the global
2845 location list. Explicitly tell ugll to insert locations and
2846 ignore breakpoints_always_inserted_mode. */
2847 update_global_location_list (UGLL_INSERT);
74960c60
VP
2848}
2849
20388dd6
YQ
2850/* Invoke CALLBACK for each of bp_location. */
2851
2852void
2853iterate_over_bp_locations (walk_bp_location_callback callback)
2854{
2855 struct bp_location *loc, **loc_tmp;
2856
2857 ALL_BP_LOCATIONS (loc, loc_tmp)
2858 {
2859 callback (loc, NULL);
2860 }
2861}
2862
b775012e
LM
2863/* This is used when we need to synch breakpoint conditions between GDB and the
2864 target. It is the case with deleting and disabling of breakpoints when using
2865 always-inserted mode. */
2866
2867static void
2868update_inserted_breakpoint_locations (void)
2869{
2870 struct bp_location *bl, **blp_tmp;
2871 int error_flag = 0;
2872 int val = 0;
2873 int disabled_breaks = 0;
2874 int hw_breakpoint_error = 0;
dd61ec5c 2875 int hw_bp_details_reported = 0;
b775012e 2876
d7e74731 2877 string_file tmp_error_stream;
b775012e
LM
2878
2879 /* Explicitly mark the warning -- this will only be printed if
2880 there was an error. */
d7e74731 2881 tmp_error_stream.puts ("Warning:\n");
b775012e 2882
5ed8105e 2883 scoped_restore_current_pspace_and_thread restore_pspace_thread;
b775012e
LM
2884
2885 ALL_BP_LOCATIONS (bl, blp_tmp)
2886 {
2887 /* We only want to update software breakpoints and hardware
2888 breakpoints. */
2889 if (!is_breakpoint (bl->owner))
2890 continue;
2891
2892 /* We only want to update locations that are already inserted
2893 and need updating. This is to avoid unwanted insertion during
2894 deletion of breakpoints. */
2895 if (!bl->inserted || (bl->inserted && !bl->needs_update))
2896 continue;
2897
2898 switch_to_program_space_and_thread (bl->pspace);
2899
2900 /* For targets that support global breakpoints, there's no need
2901 to select an inferior to insert breakpoint to. In fact, even
2902 if we aren't attached to any process yet, we should still
2903 insert breakpoints. */
f5656ead 2904 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
d7e15655 2905 && inferior_ptid == null_ptid)
b775012e
LM
2906 continue;
2907
d7e74731 2908 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
dd61ec5c 2909 &hw_breakpoint_error, &hw_bp_details_reported);
b775012e
LM
2910 if (val)
2911 error_flag = val;
2912 }
2913
2914 if (error_flag)
2915 {
223ffa71 2916 target_terminal::ours_for_output ();
b775012e
LM
2917 error_stream (tmp_error_stream);
2918 }
b775012e
LM
2919}
2920
c30eee59 2921/* Used when starting or continuing the program. */
c906108c 2922
74960c60
VP
2923static void
2924insert_breakpoint_locations (void)
c906108c 2925{
a5606eee 2926 struct breakpoint *bpt;
35df4500 2927 struct bp_location *bl, **blp_tmp;
eacd795a 2928 int error_flag = 0;
c906108c 2929 int val = 0;
3fbb6ffa 2930 int disabled_breaks = 0;
81d0cc19 2931 int hw_breakpoint_error = 0;
dd61ec5c 2932 int hw_bp_error_explained_already = 0;
c906108c 2933
d7e74731
PA
2934 string_file tmp_error_stream;
2935
81d0cc19
GS
2936 /* Explicitly mark the warning -- this will only be printed if
2937 there was an error. */
d7e74731 2938 tmp_error_stream.puts ("Warning:\n");
6c95b8df 2939
5ed8105e 2940 scoped_restore_current_pspace_and_thread restore_pspace_thread;
6c95b8df 2941
35df4500 2942 ALL_BP_LOCATIONS (bl, blp_tmp)
879bfdc2 2943 {
b775012e 2944 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
879bfdc2
DJ
2945 continue;
2946
4a64f543
MS
2947 /* There is no point inserting thread-specific breakpoints if
2948 the thread no longer exists. ALL_BP_LOCATIONS bp_location
2949 has BL->OWNER always non-NULL. */
35df4500 2950 if (bl->owner->thread != -1
5d5658a1 2951 && !valid_global_thread_id (bl->owner->thread))
f365de73
AS
2952 continue;
2953
35df4500 2954 switch_to_program_space_and_thread (bl->pspace);
6c95b8df
PA
2955
2956 /* For targets that support global breakpoints, there's no need
2957 to select an inferior to insert breakpoint to. In fact, even
2958 if we aren't attached to any process yet, we should still
2959 insert breakpoints. */
f5656ead 2960 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
d7e15655 2961 && inferior_ptid == null_ptid)
6c95b8df
PA
2962 continue;
2963
d7e74731 2964 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
dd61ec5c 2965 &hw_breakpoint_error, &hw_bp_error_explained_already);
879bfdc2 2966 if (val)
eacd795a 2967 error_flag = val;
879bfdc2 2968 }
c906108c 2969
4a64f543
MS
2970 /* If we failed to insert all locations of a watchpoint, remove
2971 them, as half-inserted watchpoint is of limited use. */
a5606eee
VP
2972 ALL_BREAKPOINTS (bpt)
2973 {
2974 int some_failed = 0;
2975 struct bp_location *loc;
2976
2977 if (!is_hardware_watchpoint (bpt))
2978 continue;
2979
d6b74ac4 2980 if (!breakpoint_enabled (bpt))
a5606eee 2981 continue;
74960c60
VP
2982
2983 if (bpt->disposition == disp_del_at_next_stop)
2984 continue;
a5606eee
VP
2985
2986 for (loc = bpt->loc; loc; loc = loc->next)
56710373 2987 if (!loc->inserted && should_be_inserted (loc))
a5606eee
VP
2988 {
2989 some_failed = 1;
2990 break;
2991 }
2992 if (some_failed)
2993 {
2994 for (loc = bpt->loc; loc; loc = loc->next)
2995 if (loc->inserted)
834c0d03 2996 remove_breakpoint (loc);
a5606eee
VP
2997
2998 hw_breakpoint_error = 1;
d7e74731
PA
2999 tmp_error_stream.printf ("Could not insert "
3000 "hardware watchpoint %d.\n",
3001 bpt->number);
eacd795a 3002 error_flag = -1;
a5606eee
VP
3003 }
3004 }
3005
eacd795a 3006 if (error_flag)
81d0cc19
GS
3007 {
3008 /* If a hardware breakpoint or watchpoint was inserted, add a
3009 message about possibly exhausted resources. */
dd61ec5c 3010 if (hw_breakpoint_error && !hw_bp_error_explained_already)
81d0cc19 3011 {
d7e74731 3012 tmp_error_stream.printf ("Could not insert hardware breakpoints:\n\
c6510018 3013You may have requested too many hardware breakpoints/watchpoints.\n");
81d0cc19 3014 }
223ffa71 3015 target_terminal::ours_for_output ();
81d0cc19
GS
3016 error_stream (tmp_error_stream);
3017 }
c906108c
SS
3018}
3019
c30eee59
TJB
3020/* Used when the program stops.
3021 Returns zero if successful, or non-zero if there was a problem
3022 removing a breakpoint location. */
3023
c906108c 3024int
fba45db2 3025remove_breakpoints (void)
c906108c 3026{
35df4500 3027 struct bp_location *bl, **blp_tmp;
3a1bae8e 3028 int val = 0;
c906108c 3029
35df4500 3030 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3031 {
1e4d1764 3032 if (bl->inserted && !is_tracepoint (bl->owner))
834c0d03 3033 val |= remove_breakpoint (bl);
c5aa993b 3034 }
3a1bae8e 3035 return val;
c906108c
SS
3036}
3037
49fa26b0
PA
3038/* When a thread exits, remove breakpoints that are related to
3039 that thread. */
3040
3041static void
3042remove_threaded_breakpoints (struct thread_info *tp, int silent)
3043{
3044 struct breakpoint *b, *b_tmp;
3045
3046 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3047 {
5d5658a1 3048 if (b->thread == tp->global_num && user_breakpoint_p (b))
49fa26b0
PA
3049 {
3050 b->disposition = disp_del_at_next_stop;
3051
3052 printf_filtered (_("\
43792cf0
PA
3053Thread-specific breakpoint %d deleted - thread %s no longer in the thread list.\n"),
3054 b->number, print_thread_id (tp));
49fa26b0
PA
3055
3056 /* Hide it from the user. */
3057 b->number = 0;
3058 }
3059 }
3060}
3061
00431a78 3062/* Remove breakpoints of inferior INF. */
6c95b8df
PA
3063
3064int
00431a78 3065remove_breakpoints_inf (inferior *inf)
6c95b8df 3066{
35df4500 3067 struct bp_location *bl, **blp_tmp;
6c95b8df 3068 int val;
6c95b8df 3069
35df4500 3070 ALL_BP_LOCATIONS (bl, blp_tmp)
6c95b8df 3071 {
35df4500 3072 if (bl->pspace != inf->pspace)
6c95b8df
PA
3073 continue;
3074
fc126975 3075 if (bl->inserted && !bl->target_info.persist)
6c95b8df 3076 {
834c0d03 3077 val = remove_breakpoint (bl);
6c95b8df
PA
3078 if (val != 0)
3079 return val;
3080 }
3081 }
3082 return 0;
3083}
3084
e58b0e63
PA
3085static int internal_breakpoint_number = -1;
3086
84f4c1fe
PM
3087/* Set the breakpoint number of B, depending on the value of INTERNAL.
3088 If INTERNAL is non-zero, the breakpoint number will be populated
3089 from internal_breakpoint_number and that variable decremented.
e5dd4106 3090 Otherwise the breakpoint number will be populated from
84f4c1fe
PM
3091 breakpoint_count and that value incremented. Internal breakpoints
3092 do not set the internal var bpnum. */
3093static void
3094set_breakpoint_number (int internal, struct breakpoint *b)
3095{
3096 if (internal)
3097 b->number = internal_breakpoint_number--;
3098 else
3099 {
3100 set_breakpoint_count (breakpoint_count + 1);
3101 b->number = breakpoint_count;
3102 }
3103}
3104
e62c965a 3105static struct breakpoint *
a6d9a66e 3106create_internal_breakpoint (struct gdbarch *gdbarch,
06edf0c0 3107 CORE_ADDR address, enum bptype type,
c0a91b2b 3108 const struct breakpoint_ops *ops)
e62c965a 3109{
51abb421 3110 symtab_and_line sal;
e62c965a
PP
3111 sal.pc = address;
3112 sal.section = find_pc_overlay (sal.pc);
6c95b8df 3113 sal.pspace = current_program_space;
e62c965a 3114
51abb421 3115 breakpoint *b = set_raw_breakpoint (gdbarch, sal, type, ops);
e62c965a
PP
3116 b->number = internal_breakpoint_number--;
3117 b->disposition = disp_donttouch;
3118
3119 return b;
3120}
3121
17450429
PP
3122static const char *const longjmp_names[] =
3123 {
3124 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
3125 };
3126#define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
3127
3128/* Per-objfile data private to breakpoint.c. */
3129struct breakpoint_objfile_data
3130{
3131 /* Minimal symbol for "_ovly_debug_event" (if any). */
43dce439 3132 struct bound_minimal_symbol overlay_msym {};
17450429
PP
3133
3134 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
43dce439 3135 struct bound_minimal_symbol longjmp_msym[NUM_LONGJMP_NAMES] {};
17450429 3136
28106bc2 3137 /* True if we have looked for longjmp probes. */
43dce439 3138 int longjmp_searched = 0;
28106bc2 3139
45461e0d
SM
3140 /* SystemTap probe points for longjmp (if any). These are non-owning
3141 references. */
3142 std::vector<probe *> longjmp_probes;
28106bc2 3143
17450429 3144 /* Minimal symbol for "std::terminate()" (if any). */
43dce439 3145 struct bound_minimal_symbol terminate_msym {};
17450429
PP
3146
3147 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
43dce439 3148 struct bound_minimal_symbol exception_msym {};
28106bc2
SDJ
3149
3150 /* True if we have looked for exception probes. */
43dce439 3151 int exception_searched = 0;
28106bc2 3152
45461e0d
SM
3153 /* SystemTap probe points for unwinding (if any). These are non-owning
3154 references. */
3155 std::vector<probe *> exception_probes;
17450429
PP
3156};
3157
3158static const struct objfile_data *breakpoint_objfile_key;
3159
3160/* Minimal symbol not found sentinel. */
3161static struct minimal_symbol msym_not_found;
3162
3163/* Returns TRUE if MSYM point to the "not found" sentinel. */
3164
3165static int
3166msym_not_found_p (const struct minimal_symbol *msym)
3167{
3168 return msym == &msym_not_found;
3169}
3170
3171/* Return per-objfile data needed by breakpoint.c.
3172 Allocate the data if necessary. */
3173
3174static struct breakpoint_objfile_data *
3175get_breakpoint_objfile_data (struct objfile *objfile)
3176{
3177 struct breakpoint_objfile_data *bp_objfile_data;
3178
9a3c8263
SM
3179 bp_objfile_data = ((struct breakpoint_objfile_data *)
3180 objfile_data (objfile, breakpoint_objfile_key));
17450429
PP
3181 if (bp_objfile_data == NULL)
3182 {
43dce439 3183 bp_objfile_data = new breakpoint_objfile_data ();
17450429
PP
3184 set_objfile_data (objfile, breakpoint_objfile_key, bp_objfile_data);
3185 }
3186 return bp_objfile_data;
3187}
3188
28106bc2 3189static void
43dce439 3190free_breakpoint_objfile_data (struct objfile *obj, void *data)
28106bc2 3191{
9a3c8263
SM
3192 struct breakpoint_objfile_data *bp_objfile_data
3193 = (struct breakpoint_objfile_data *) data;
28106bc2 3194
43dce439 3195 delete bp_objfile_data;
28106bc2
SDJ
3196}
3197
e62c965a 3198static void
af02033e 3199create_overlay_event_breakpoint (void)
e62c965a 3200{
69de3c6a 3201 struct objfile *objfile;
af02033e 3202 const char *const func_name = "_ovly_debug_event";
e62c965a 3203
69de3c6a
PP
3204 ALL_OBJFILES (objfile)
3205 {
3206 struct breakpoint *b;
17450429
PP
3207 struct breakpoint_objfile_data *bp_objfile_data;
3208 CORE_ADDR addr;
67994074 3209 struct explicit_location explicit_loc;
69de3c6a 3210
17450429
PP
3211 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3212
3b7344d5 3213 if (msym_not_found_p (bp_objfile_data->overlay_msym.minsym))
17450429
PP
3214 continue;
3215
3b7344d5 3216 if (bp_objfile_data->overlay_msym.minsym == NULL)
17450429 3217 {
3b7344d5 3218 struct bound_minimal_symbol m;
17450429
PP
3219
3220 m = lookup_minimal_symbol_text (func_name, objfile);
3b7344d5 3221 if (m.minsym == NULL)
17450429
PP
3222 {
3223 /* Avoid future lookups in this objfile. */
3b7344d5 3224 bp_objfile_data->overlay_msym.minsym = &msym_not_found;
17450429
PP
3225 continue;
3226 }
3227 bp_objfile_data->overlay_msym = m;
3228 }
e62c965a 3229
77e371c0 3230 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->overlay_msym);
17450429 3231 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
06edf0c0
PA
3232 bp_overlay_event,
3233 &internal_breakpoint_ops);
67994074
KS
3234 initialize_explicit_location (&explicit_loc);
3235 explicit_loc.function_name = ASTRDUP (func_name);
d28cd78a 3236 b->location = new_explicit_location (&explicit_loc);
e62c965a 3237
69de3c6a
PP
3238 if (overlay_debugging == ovly_auto)
3239 {
3240 b->enable_state = bp_enabled;
3241 overlay_events_enabled = 1;
3242 }
3243 else
3244 {
3245 b->enable_state = bp_disabled;
3246 overlay_events_enabled = 0;
3247 }
e62c965a 3248 }
e62c965a
PP
3249}
3250
0fd8e87f 3251static void
af02033e 3252create_longjmp_master_breakpoint (void)
0fd8e87f 3253{
6c95b8df 3254 struct program_space *pspace;
6c95b8df 3255
5ed8105e 3256 scoped_restore_current_program_space restore_pspace;
0fd8e87f 3257
6c95b8df 3258 ALL_PSPACES (pspace)
af02033e
PP
3259 {
3260 struct objfile *objfile;
3261
3262 set_current_program_space (pspace);
3263
3264 ALL_OBJFILES (objfile)
0fd8e87f 3265 {
af02033e
PP
3266 int i;
3267 struct gdbarch *gdbarch;
17450429 3268 struct breakpoint_objfile_data *bp_objfile_data;
0fd8e87f 3269
af02033e 3270 gdbarch = get_objfile_arch (objfile);
0fd8e87f 3271
17450429
PP
3272 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3273
28106bc2
SDJ
3274 if (!bp_objfile_data->longjmp_searched)
3275 {
45461e0d
SM
3276 std::vector<probe *> ret
3277 = find_probes_in_objfile (objfile, "libc", "longjmp");
25f9533e 3278
45461e0d 3279 if (!ret.empty ())
25f9533e
SDJ
3280 {
3281 /* We are only interested in checking one element. */
45461e0d 3282 probe *p = ret[0];
25f9533e 3283
935676c9 3284 if (!p->can_evaluate_arguments ())
25f9533e
SDJ
3285 {
3286 /* We cannot use the probe interface here, because it does
3287 not know how to evaluate arguments. */
45461e0d 3288 ret.clear ();
25f9533e
SDJ
3289 }
3290 }
3291 bp_objfile_data->longjmp_probes = ret;
28106bc2
SDJ
3292 bp_objfile_data->longjmp_searched = 1;
3293 }
3294
45461e0d 3295 if (!bp_objfile_data->longjmp_probes.empty ())
28106bc2 3296 {
28106bc2
SDJ
3297 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3298
45461e0d 3299 for (probe *p : bp_objfile_data->longjmp_probes)
28106bc2
SDJ
3300 {
3301 struct breakpoint *b;
3302
729662a5 3303 b = create_internal_breakpoint (gdbarch,
935676c9 3304 p->get_relocated_address (objfile),
28106bc2
SDJ
3305 bp_longjmp_master,
3306 &internal_breakpoint_ops);
d28cd78a 3307 b->location = new_probe_location ("-probe-stap libc:longjmp");
28106bc2
SDJ
3308 b->enable_state = bp_disabled;
3309 }
3310
3311 continue;
3312 }
3313
0569175e
TSD
3314 if (!gdbarch_get_longjmp_target_p (gdbarch))
3315 continue;
3316
17450429 3317 for (i = 0; i < NUM_LONGJMP_NAMES; i++)
af02033e
PP
3318 {
3319 struct breakpoint *b;
af02033e 3320 const char *func_name;
17450429 3321 CORE_ADDR addr;
67994074 3322 struct explicit_location explicit_loc;
6c95b8df 3323
3b7344d5 3324 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i].minsym))
af02033e 3325 continue;
0fd8e87f 3326
17450429 3327 func_name = longjmp_names[i];
3b7344d5 3328 if (bp_objfile_data->longjmp_msym[i].minsym == NULL)
17450429 3329 {
3b7344d5 3330 struct bound_minimal_symbol m;
17450429
PP
3331
3332 m = lookup_minimal_symbol_text (func_name, objfile);
3b7344d5 3333 if (m.minsym == NULL)
17450429
PP
3334 {
3335 /* Prevent future lookups in this objfile. */
3b7344d5 3336 bp_objfile_data->longjmp_msym[i].minsym = &msym_not_found;
17450429
PP
3337 continue;
3338 }
3339 bp_objfile_data->longjmp_msym[i] = m;
3340 }
3341
77e371c0 3342 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->longjmp_msym[i]);
06edf0c0
PA
3343 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master,
3344 &internal_breakpoint_ops);
67994074
KS
3345 initialize_explicit_location (&explicit_loc);
3346 explicit_loc.function_name = ASTRDUP (func_name);
d28cd78a 3347 b->location = new_explicit_location (&explicit_loc);
af02033e
PP
3348 b->enable_state = bp_disabled;
3349 }
0fd8e87f 3350 }
af02033e 3351 }
0fd8e87f
UW
3352}
3353
af02033e 3354/* Create a master std::terminate breakpoint. */
aa7d318d 3355static void
af02033e 3356create_std_terminate_master_breakpoint (void)
aa7d318d
TT
3357{
3358 struct program_space *pspace;
af02033e 3359 const char *const func_name = "std::terminate()";
aa7d318d 3360
5ed8105e 3361 scoped_restore_current_program_space restore_pspace;
aa7d318d
TT
3362
3363 ALL_PSPACES (pspace)
17450429
PP
3364 {
3365 struct objfile *objfile;
3366 CORE_ADDR addr;
3367
3368 set_current_program_space (pspace);
3369
aa7d318d
TT
3370 ALL_OBJFILES (objfile)
3371 {
3372 struct breakpoint *b;
17450429 3373 struct breakpoint_objfile_data *bp_objfile_data;
67994074 3374 struct explicit_location explicit_loc;
aa7d318d 3375
17450429 3376 bp_objfile_data = get_breakpoint_objfile_data (objfile);
aa7d318d 3377
3b7344d5 3378 if (msym_not_found_p (bp_objfile_data->terminate_msym.minsym))
17450429
PP
3379 continue;
3380
3b7344d5 3381 if (bp_objfile_data->terminate_msym.minsym == NULL)
17450429 3382 {
3b7344d5 3383 struct bound_minimal_symbol m;
17450429
PP
3384
3385 m = lookup_minimal_symbol (func_name, NULL, objfile);
3b7344d5
TT
3386 if (m.minsym == NULL || (MSYMBOL_TYPE (m.minsym) != mst_text
3387 && MSYMBOL_TYPE (m.minsym) != mst_file_text))
17450429
PP
3388 {
3389 /* Prevent future lookups in this objfile. */
3b7344d5 3390 bp_objfile_data->terminate_msym.minsym = &msym_not_found;
17450429
PP
3391 continue;
3392 }
3393 bp_objfile_data->terminate_msym = m;
3394 }
aa7d318d 3395
77e371c0 3396 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->terminate_msym);
17450429 3397 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
06edf0c0
PA
3398 bp_std_terminate_master,
3399 &internal_breakpoint_ops);
67994074
KS
3400 initialize_explicit_location (&explicit_loc);
3401 explicit_loc.function_name = ASTRDUP (func_name);
d28cd78a 3402 b->location = new_explicit_location (&explicit_loc);
aa7d318d
TT
3403 b->enable_state = bp_disabled;
3404 }
17450429 3405 }
aa7d318d
TT
3406}
3407
186c406b
TT
3408/* Install a master breakpoint on the unwinder's debug hook. */
3409
70221824 3410static void
186c406b
TT
3411create_exception_master_breakpoint (void)
3412{
3413 struct objfile *objfile;
17450429 3414 const char *const func_name = "_Unwind_DebugHook";
186c406b
TT
3415
3416 ALL_OBJFILES (objfile)
3417 {
17450429
PP
3418 struct breakpoint *b;
3419 struct gdbarch *gdbarch;
3420 struct breakpoint_objfile_data *bp_objfile_data;
3421 CORE_ADDR addr;
67994074 3422 struct explicit_location explicit_loc;
17450429
PP
3423
3424 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3425
28106bc2
SDJ
3426 /* We prefer the SystemTap probe point if it exists. */
3427 if (!bp_objfile_data->exception_searched)
3428 {
45461e0d
SM
3429 std::vector<probe *> ret
3430 = find_probes_in_objfile (objfile, "libgcc", "unwind");
25f9533e 3431
45461e0d 3432 if (!ret.empty ())
25f9533e
SDJ
3433 {
3434 /* We are only interested in checking one element. */
45461e0d 3435 probe *p = ret[0];
25f9533e 3436
935676c9 3437 if (!p->can_evaluate_arguments ())
25f9533e
SDJ
3438 {
3439 /* We cannot use the probe interface here, because it does
3440 not know how to evaluate arguments. */
45461e0d 3441 ret.clear ();
25f9533e
SDJ
3442 }
3443 }
3444 bp_objfile_data->exception_probes = ret;
28106bc2
SDJ
3445 bp_objfile_data->exception_searched = 1;
3446 }
3447
45461e0d 3448 if (!bp_objfile_data->exception_probes.empty ())
28106bc2
SDJ
3449 {
3450 struct gdbarch *gdbarch = get_objfile_arch (objfile);
45461e0d
SM
3451
3452 for (probe *p : bp_objfile_data->exception_probes)
28106bc2
SDJ
3453 {
3454 struct breakpoint *b;
3455
729662a5 3456 b = create_internal_breakpoint (gdbarch,
935676c9 3457 p->get_relocated_address (objfile),
28106bc2
SDJ
3458 bp_exception_master,
3459 &internal_breakpoint_ops);
d28cd78a 3460 b->location = new_probe_location ("-probe-stap libgcc:unwind");
28106bc2
SDJ
3461 b->enable_state = bp_disabled;
3462 }
3463
3464 continue;
3465 }
3466
3467 /* Otherwise, try the hook function. */
3468
3b7344d5 3469 if (msym_not_found_p (bp_objfile_data->exception_msym.minsym))
17450429
PP
3470 continue;
3471
3472 gdbarch = get_objfile_arch (objfile);
186c406b 3473
3b7344d5 3474 if (bp_objfile_data->exception_msym.minsym == NULL)
186c406b 3475 {
3b7344d5 3476 struct bound_minimal_symbol debug_hook;
186c406b 3477
17450429 3478 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
3b7344d5 3479 if (debug_hook.minsym == NULL)
17450429 3480 {
3b7344d5 3481 bp_objfile_data->exception_msym.minsym = &msym_not_found;
17450429
PP
3482 continue;
3483 }
3484
3485 bp_objfile_data->exception_msym = debug_hook;
186c406b 3486 }
17450429 3487
77e371c0 3488 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->exception_msym);
8b88a78e
PA
3489 addr = gdbarch_convert_from_func_ptr_addr (gdbarch, addr,
3490 current_top_target ());
06edf0c0
PA
3491 b = create_internal_breakpoint (gdbarch, addr, bp_exception_master,
3492 &internal_breakpoint_ops);
67994074
KS
3493 initialize_explicit_location (&explicit_loc);
3494 explicit_loc.function_name = ASTRDUP (func_name);
d28cd78a 3495 b->location = new_explicit_location (&explicit_loc);
17450429 3496 b->enable_state = bp_disabled;
186c406b 3497 }
186c406b
TT
3498}
3499
9ef9e6a6
KS
3500/* Does B have a location spec? */
3501
3502static int
3503breakpoint_event_location_empty_p (const struct breakpoint *b)
3504{
d28cd78a 3505 return b->location != NULL && event_location_empty_p (b->location.get ());
9ef9e6a6
KS
3506}
3507
c906108c 3508void
fba45db2 3509update_breakpoints_after_exec (void)
c906108c 3510{
35df4500 3511 struct breakpoint *b, *b_tmp;
876fa593 3512 struct bp_location *bploc, **bplocp_tmp;
c906108c 3513
25b22b0a
PA
3514 /* We're about to delete breakpoints from GDB's lists. If the
3515 INSERTED flag is true, GDB will try to lift the breakpoints by
3516 writing the breakpoints' "shadow contents" back into memory. The
3517 "shadow contents" are NOT valid after an exec, so GDB should not
3518 do that. Instead, the target is responsible from marking
3519 breakpoints out as soon as it detects an exec. We don't do that
3520 here instead, because there may be other attempts to delete
3521 breakpoints after detecting an exec and before reaching here. */
876fa593 3522 ALL_BP_LOCATIONS (bploc, bplocp_tmp)
6c95b8df
PA
3523 if (bploc->pspace == current_program_space)
3524 gdb_assert (!bploc->inserted);
c906108c 3525
35df4500 3526 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 3527 {
6c95b8df
PA
3528 if (b->pspace != current_program_space)
3529 continue;
3530
4a64f543 3531 /* Solib breakpoints must be explicitly reset after an exec(). */
c5aa993b
JM
3532 if (b->type == bp_shlib_event)
3533 {
3534 delete_breakpoint (b);
3535 continue;
3536 }
c906108c 3537
4a64f543 3538 /* JIT breakpoints must be explicitly reset after an exec(). */
4efc6507
DE
3539 if (b->type == bp_jit_event)
3540 {
3541 delete_breakpoint (b);
3542 continue;
3543 }
3544
1900040c 3545 /* Thread event breakpoints must be set anew after an exec(),
0fd8e87f
UW
3546 as must overlay event and longjmp master breakpoints. */
3547 if (b->type == bp_thread_event || b->type == bp_overlay_event
186c406b
TT
3548 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
3549 || b->type == bp_exception_master)
c4093a6a
JM
3550 {
3551 delete_breakpoint (b);
3552 continue;
3553 }
3554
4a64f543 3555 /* Step-resume breakpoints are meaningless after an exec(). */
2c03e5be 3556 if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
c5aa993b
JM
3557 {
3558 delete_breakpoint (b);
3559 continue;
3560 }
3561
7c16b83e
PA
3562 /* Just like single-step breakpoints. */
3563 if (b->type == bp_single_step)
3564 {
3565 delete_breakpoint (b);
3566 continue;
3567 }
3568
611c83ae
PA
3569 /* Longjmp and longjmp-resume breakpoints are also meaningless
3570 after an exec. */
186c406b 3571 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
e2e4d78b 3572 || b->type == bp_longjmp_call_dummy
186c406b 3573 || b->type == bp_exception || b->type == bp_exception_resume)
611c83ae
PA
3574 {
3575 delete_breakpoint (b);
3576 continue;
3577 }
3578
ce78b96d
JB
3579 if (b->type == bp_catchpoint)
3580 {
3581 /* For now, none of the bp_catchpoint breakpoints need to
3582 do anything at this point. In the future, if some of
3583 the catchpoints need to something, we will need to add
3584 a new method, and call this method from here. */
3585 continue;
3586 }
3587
c5aa993b
JM
3588 /* bp_finish is a special case. The only way we ought to be able
3589 to see one of these when an exec() has happened, is if the user
3590 caught a vfork, and then said "finish". Ordinarily a finish just
3591 carries them to the call-site of the current callee, by setting
3592 a temporary bp there and resuming. But in this case, the finish
3593 will carry them entirely through the vfork & exec.
3594
3595 We don't want to allow a bp_finish to remain inserted now. But
3596 we can't safely delete it, 'cause finish_command has a handle to
3597 the bp on a bpstat, and will later want to delete it. There's a
3598 chance (and I've seen it happen) that if we delete the bp_finish
3599 here, that its storage will get reused by the time finish_command
3600 gets 'round to deleting the "use to be a bp_finish" breakpoint.
3601 We really must allow finish_command to delete a bp_finish.
3602
e5dd4106 3603 In the absence of a general solution for the "how do we know
53a5351d
JM
3604 it's safe to delete something others may have handles to?"
3605 problem, what we'll do here is just uninsert the bp_finish, and
3606 let finish_command delete it.
3607
3608 (We know the bp_finish is "doomed" in the sense that it's
3609 momentary, and will be deleted as soon as finish_command sees
3610 the inferior stopped. So it doesn't matter that the bp's
3611 address is probably bogus in the new a.out, unlike e.g., the
3612 solib breakpoints.) */
c5aa993b 3613
c5aa993b
JM
3614 if (b->type == bp_finish)
3615 {
3616 continue;
3617 }
3618
3619 /* Without a symbolic address, we have little hope of the
3620 pre-exec() address meaning the same thing in the post-exec()
4a64f543 3621 a.out. */
9ef9e6a6 3622 if (breakpoint_event_location_empty_p (b))
c5aa993b
JM
3623 {
3624 delete_breakpoint (b);
3625 continue;
3626 }
c5aa993b 3627 }
c906108c
SS
3628}
3629
3630int
d80ee84f 3631detach_breakpoints (ptid_t ptid)
c906108c 3632{
35df4500 3633 struct bp_location *bl, **blp_tmp;
3a1bae8e 3634 int val = 0;
2989a365 3635 scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid);
6c95b8df 3636 struct inferior *inf = current_inferior ();
c5aa993b 3637
e99b03dc 3638 if (ptid.pid () == inferior_ptid.pid ())
8a3fe4f8 3639 error (_("Cannot detach breakpoints of inferior_ptid"));
c5aa993b 3640
6c95b8df 3641 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
d80ee84f 3642 inferior_ptid = ptid;
35df4500 3643 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3644 {
35df4500 3645 if (bl->pspace != inf->pspace)
6c95b8df
PA
3646 continue;
3647
bd9673a4
PW
3648 /* This function must physically remove breakpoints locations
3649 from the specified ptid, without modifying the breakpoint
3650 package's state. Locations of type bp_loc_other are only
3651 maintained at GDB side. So, there is no need to remove
3652 these bp_loc_other locations. Moreover, removing these
3653 would modify the breakpoint package's state. */
3654 if (bl->loc_type == bp_loc_other)
3655 continue;
3656
35df4500 3657 if (bl->inserted)
b2b6a7da 3658 val |= remove_breakpoint_1 (bl, DETACH_BREAKPOINT);
c5aa993b 3659 }
d03285ec 3660
3a1bae8e 3661 return val;
c906108c
SS
3662}
3663
35df4500 3664/* Remove the breakpoint location BL from the current address space.
6c95b8df
PA
3665 Note that this is used to detach breakpoints from a child fork.
3666 When we get here, the child isn't in the inferior list, and neither
3667 do we have objects to represent its address space --- we should
35df4500 3668 *not* look at bl->pspace->aspace here. */
6c95b8df 3669
c906108c 3670static int
b2b6a7da 3671remove_breakpoint_1 (struct bp_location *bl, enum remove_bp_reason reason)
c906108c
SS
3672{
3673 int val;
c5aa993b 3674
35df4500
TJB
3675 /* BL is never in moribund_locations by our callers. */
3676 gdb_assert (bl->owner != NULL);
2bdf28a0 3677
74960c60
VP
3678 /* The type of none suggests that owner is actually deleted.
3679 This should not ever happen. */
35df4500 3680 gdb_assert (bl->owner->type != bp_none);
0bde7532 3681
35df4500
TJB
3682 if (bl->loc_type == bp_loc_software_breakpoint
3683 || bl->loc_type == bp_loc_hardware_breakpoint)
c906108c 3684 {
c02f5703
MS
3685 /* "Normal" instruction breakpoint: either the standard
3686 trap-instruction bp (bp_breakpoint), or a
3687 bp_hardware_breakpoint. */
3688
3689 /* First check to see if we have to handle an overlay. */
3690 if (overlay_debugging == ovly_off
35df4500
TJB
3691 || bl->section == NULL
3692 || !(section_is_overlay (bl->section)))
c02f5703
MS
3693 {
3694 /* No overlay handling: just remove the breakpoint. */
08351840
PA
3695
3696 /* If we're trying to uninsert a memory breakpoint that we
3697 know is set in a dynamic object that is marked
3698 shlib_disabled, then either the dynamic object was
3699 removed with "remove-symbol-file" or with
3700 "nosharedlibrary". In the former case, we don't know
3701 whether another dynamic object might have loaded over the
3702 breakpoint's address -- the user might well let us know
3703 about it next with add-symbol-file (the whole point of
d03de421 3704 add-symbol-file is letting the user manually maintain a
08351840
PA
3705 list of dynamically loaded objects). If we have the
3706 breakpoint's shadow memory, that is, this is a software
3707 breakpoint managed by GDB, check whether the breakpoint
3708 is still inserted in memory, to avoid overwriting wrong
3709 code with stale saved shadow contents. Note that HW
3710 breakpoints don't have shadow memory, as they're
3711 implemented using a mechanism that is not dependent on
3712 being able to modify the target's memory, and as such
3713 they should always be removed. */
3714 if (bl->shlib_disabled
3715 && bl->target_info.shadow_len != 0
3716 && !memory_validate_breakpoint (bl->gdbarch, &bl->target_info))
3717 val = 0;
3718 else
73971819 3719 val = bl->owner->ops->remove_location (bl, reason);
c02f5703 3720 }
c906108c
SS
3721 else
3722 {
4a64f543 3723 /* This breakpoint is in an overlay section.
c02f5703
MS
3724 Did we set a breakpoint at the LMA? */
3725 if (!overlay_events_enabled)
3726 {
3727 /* Yes -- overlay event support is not active, so we
3728 should have set a breakpoint at the LMA. Remove it.
3729 */
c02f5703
MS
3730 /* Ignore any failures: if the LMA is in ROM, we will
3731 have already warned when we failed to insert it. */
35df4500
TJB
3732 if (bl->loc_type == bp_loc_hardware_breakpoint)
3733 target_remove_hw_breakpoint (bl->gdbarch,
3734 &bl->overlay_target_info);
c02f5703 3735 else
35df4500 3736 target_remove_breakpoint (bl->gdbarch,
73971819
PA
3737 &bl->overlay_target_info,
3738 reason);
c02f5703
MS
3739 }
3740 /* Did we set a breakpoint at the VMA?
3741 If so, we will have marked the breakpoint 'inserted'. */
35df4500 3742 if (bl->inserted)
c906108c 3743 {
c02f5703
MS
3744 /* Yes -- remove it. Previously we did not bother to
3745 remove the breakpoint if the section had been
3746 unmapped, but let's not rely on that being safe. We
3747 don't know what the overlay manager might do. */
aa67235e
UW
3748
3749 /* However, we should remove *software* breakpoints only
3750 if the section is still mapped, or else we overwrite
3751 wrong code with the saved shadow contents. */
348d480f
PA
3752 if (bl->loc_type == bp_loc_hardware_breakpoint
3753 || section_is_mapped (bl->section))
73971819 3754 val = bl->owner->ops->remove_location (bl, reason);
aa67235e
UW
3755 else
3756 val = 0;
c906108c 3757 }
c02f5703
MS
3758 else
3759 {
3760 /* No -- not inserted, so no need to remove. No error. */
3761 val = 0;
3762 }
c906108c 3763 }
879d1e6b 3764
08351840
PA
3765 /* In some cases, we might not be able to remove a breakpoint in
3766 a shared library that has already been removed, but we have
3767 not yet processed the shlib unload event. Similarly for an
3768 unloaded add-symbol-file object - the user might not yet have
3769 had the chance to remove-symbol-file it. shlib_disabled will
3770 be set if the library/object has already been removed, but
3771 the breakpoint hasn't been uninserted yet, e.g., after
3772 "nosharedlibrary" or "remove-symbol-file" with breakpoints
3773 always-inserted mode. */
076855f9 3774 if (val
08351840
PA
3775 && (bl->loc_type == bp_loc_software_breakpoint
3776 && (bl->shlib_disabled
3777 || solib_name_from_address (bl->pspace, bl->address)
d03de421
PA
3778 || shared_objfile_contains_address_p (bl->pspace,
3779 bl->address))))
879d1e6b
UW
3780 val = 0;
3781
c906108c
SS
3782 if (val)
3783 return val;
b2b6a7da 3784 bl->inserted = (reason == DETACH_BREAKPOINT);
c906108c 3785 }
35df4500 3786 else if (bl->loc_type == bp_loc_hardware_watchpoint)
c906108c 3787 {
77b06cd7
TJB
3788 gdb_assert (bl->owner->ops != NULL
3789 && bl->owner->ops->remove_location != NULL);
3790
b2b6a7da 3791 bl->inserted = (reason == DETACH_BREAKPOINT);
73971819 3792 bl->owner->ops->remove_location (bl, reason);
2e70b7b9 3793
c906108c 3794 /* Failure to remove any of the hardware watchpoints comes here. */
b2b6a7da 3795 if (reason == REMOVE_BREAKPOINT && bl->inserted)
8a3fe4f8 3796 warning (_("Could not remove hardware watchpoint %d."),
35df4500 3797 bl->owner->number);
c906108c 3798 }
35df4500
TJB
3799 else if (bl->owner->type == bp_catchpoint
3800 && breakpoint_enabled (bl->owner)
3801 && !bl->duplicate)
ce78b96d 3802 {
77b06cd7
TJB
3803 gdb_assert (bl->owner->ops != NULL
3804 && bl->owner->ops->remove_location != NULL);
ce78b96d 3805
73971819 3806 val = bl->owner->ops->remove_location (bl, reason);
ce78b96d
JB
3807 if (val)
3808 return val;
77b06cd7 3809
b2b6a7da 3810 bl->inserted = (reason == DETACH_BREAKPOINT);
ce78b96d 3811 }
c906108c
SS
3812
3813 return 0;
3814}
3815
6c95b8df 3816static int
834c0d03 3817remove_breakpoint (struct bp_location *bl)
6c95b8df 3818{
35df4500
TJB
3819 /* BL is never in moribund_locations by our callers. */
3820 gdb_assert (bl->owner != NULL);
2bdf28a0 3821
6c95b8df
PA
3822 /* The type of none suggests that owner is actually deleted.
3823 This should not ever happen. */
35df4500 3824 gdb_assert (bl->owner->type != bp_none);
6c95b8df 3825
5ed8105e 3826 scoped_restore_current_pspace_and_thread restore_pspace_thread;
6c95b8df 3827
35df4500 3828 switch_to_program_space_and_thread (bl->pspace);
6c95b8df 3829
5ed8105e 3830 return remove_breakpoint_1 (bl, REMOVE_BREAKPOINT);
6c95b8df
PA
3831}
3832
c906108c
SS
3833/* Clear the "inserted" flag in all breakpoints. */
3834
25b22b0a 3835void
fba45db2 3836mark_breakpoints_out (void)
c906108c 3837{
35df4500 3838 struct bp_location *bl, **blp_tmp;
c906108c 3839
35df4500 3840 ALL_BP_LOCATIONS (bl, blp_tmp)
66c4b3e8 3841 if (bl->pspace == current_program_space)
35df4500 3842 bl->inserted = 0;
c906108c
SS
3843}
3844
53a5351d
JM
3845/* Clear the "inserted" flag in all breakpoints and delete any
3846 breakpoints which should go away between runs of the program.
c906108c
SS
3847
3848 Plus other such housekeeping that has to be done for breakpoints
3849 between runs.
3850
53a5351d
JM
3851 Note: this function gets called at the end of a run (by
3852 generic_mourn_inferior) and when a run begins (by
4a64f543 3853 init_wait_for_inferior). */
c906108c
SS
3854
3855
3856
3857void
fba45db2 3858breakpoint_init_inferior (enum inf_context context)
c906108c 3859{
35df4500 3860 struct breakpoint *b, *b_tmp;
6c95b8df 3861 struct program_space *pspace = current_program_space;
c906108c 3862
50c71eaf
PA
3863 /* If breakpoint locations are shared across processes, then there's
3864 nothing to do. */
f5656ead 3865 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
50c71eaf
PA
3866 return;
3867
1a853c52 3868 mark_breakpoints_out ();
075f6582 3869
35df4500 3870 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 3871 {
6c95b8df
PA
3872 if (b->loc && b->loc->pspace != pspace)
3873 continue;
3874
c5aa993b
JM
3875 switch (b->type)
3876 {
3877 case bp_call_dummy:
e2e4d78b 3878 case bp_longjmp_call_dummy:
c906108c 3879
c5aa993b 3880 /* If the call dummy breakpoint is at the entry point it will
ab92d69b
PA
3881 cause problems when the inferior is rerun, so we better get
3882 rid of it. */
3883
3884 case bp_watchpoint_scope:
3885
3886 /* Also get rid of scope breakpoints. */
3887
3888 case bp_shlib_event:
3889
3890 /* Also remove solib event breakpoints. Their addresses may
3891 have changed since the last time we ran the program.
3892 Actually we may now be debugging against different target;
3893 and so the solib backend that installed this breakpoint may
3894 not be used in by the target. E.g.,
3895
3896 (gdb) file prog-linux
3897 (gdb) run # native linux target
3898 ...
3899 (gdb) kill
3900 (gdb) file prog-win.exe
3901 (gdb) tar rem :9999 # remote Windows gdbserver.
3902 */
c906108c 3903
f59f708a
PA
3904 case bp_step_resume:
3905
3906 /* Also remove step-resume breakpoints. */
3907
7c16b83e
PA
3908 case bp_single_step:
3909
3910 /* Also remove single-step breakpoints. */
3911
c5aa993b
JM
3912 delete_breakpoint (b);
3913 break;
c906108c 3914
c5aa993b
JM
3915 case bp_watchpoint:
3916 case bp_hardware_watchpoint:
3917 case bp_read_watchpoint:
3918 case bp_access_watchpoint:
3a5c3e22
PA
3919 {
3920 struct watchpoint *w = (struct watchpoint *) b;
c906108c 3921
3a5c3e22
PA
3922 /* Likewise for watchpoints on local expressions. */
3923 if (w->exp_valid_block != NULL)
3924 delete_breakpoint (b);
63000888 3925 else
3a5c3e22 3926 {
63000888
PA
3927 /* Get rid of existing locations, which are no longer
3928 valid. New ones will be created in
3929 update_watchpoint, when the inferior is restarted.
3930 The next update_global_location_list call will
3931 garbage collect them. */
3932 b->loc = NULL;
3933
3934 if (context == inf_starting)
3935 {
3936 /* Reset val field to force reread of starting value in
3937 insert_breakpoints. */
850645cf 3938 w->val.reset (nullptr);
63000888
PA
3939 w->val_valid = 0;
3940 }
3941 }
3a5c3e22 3942 }
c5aa993b
JM
3943 break;
3944 default:
c5aa993b
JM
3945 break;
3946 }
3947 }
1c5cfe86
PA
3948
3949 /* Get rid of the moribund locations. */
1123588c 3950 for (bp_location *bl : moribund_locations)
35df4500 3951 decref_bp_location (&bl);
1123588c 3952 moribund_locations.clear ();
c906108c
SS
3953}
3954
6c95b8df
PA
3955/* These functions concern about actual breakpoints inserted in the
3956 target --- to e.g. check if we need to do decr_pc adjustment or if
3957 we need to hop over the bkpt --- so we check for address space
3958 match, not program space. */
3959
c2c6d25f
JM
3960/* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
3961 exists at PC. It returns ordinary_breakpoint_here if it's an
3962 ordinary breakpoint, or permanent_breakpoint_here if it's a
3963 permanent breakpoint.
3964 - When continuing from a location with an ordinary breakpoint, we
3965 actually single step once before calling insert_breakpoints.
e5dd4106 3966 - When continuing from a location with a permanent breakpoint, we
c2c6d25f
JM
3967 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
3968 the target, to advance the PC past the breakpoint. */
c906108c 3969
c2c6d25f 3970enum breakpoint_here
accd0bcd 3971breakpoint_here_p (const address_space *aspace, CORE_ADDR pc)
c906108c 3972{
35df4500 3973 struct bp_location *bl, **blp_tmp;
c2c6d25f 3974 int any_breakpoint_here = 0;
c906108c 3975
35df4500 3976 ALL_BP_LOCATIONS (bl, blp_tmp)
075f6582 3977 {
35df4500
TJB
3978 if (bl->loc_type != bp_loc_software_breakpoint
3979 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
3980 continue;
3981
f1310107 3982 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
35df4500 3983 if ((breakpoint_enabled (bl->owner)
1a853c52 3984 || bl->permanent)
f1310107 3985 && breakpoint_location_address_match (bl, aspace, pc))
075f6582
DJ
3986 {
3987 if (overlay_debugging
35df4500
TJB
3988 && section_is_overlay (bl->section)
3989 && !section_is_mapped (bl->section))
075f6582 3990 continue; /* unmapped overlay -- can't be a match */
1a853c52 3991 else if (bl->permanent)
075f6582
DJ
3992 return permanent_breakpoint_here;
3993 else
3994 any_breakpoint_here = 1;
3995 }
3996 }
c906108c 3997
f486487f 3998 return any_breakpoint_here ? ordinary_breakpoint_here : no_breakpoint_here;
c906108c
SS
3999}
4000
d35ae833
PA
4001/* See breakpoint.h. */
4002
4003int
accd0bcd 4004breakpoint_in_range_p (const address_space *aspace,
d35ae833
PA
4005 CORE_ADDR addr, ULONGEST len)
4006{
4007 struct bp_location *bl, **blp_tmp;
4008
4009 ALL_BP_LOCATIONS (bl, blp_tmp)
4010 {
4011 if (bl->loc_type != bp_loc_software_breakpoint
4012 && bl->loc_type != bp_loc_hardware_breakpoint)
4013 continue;
4014
4015 if ((breakpoint_enabled (bl->owner)
4016 || bl->permanent)
4017 && breakpoint_location_address_range_overlap (bl, aspace,
4018 addr, len))
4019 {
4020 if (overlay_debugging
4021 && section_is_overlay (bl->section)
4022 && !section_is_mapped (bl->section))
4023 {
4024 /* Unmapped overlay -- can't be a match. */
4025 continue;
4026 }
4027
4028 return 1;
4029 }
4030 }
4031
4032 return 0;
4033}
4034
1c5cfe86
PA
4035/* Return true if there's a moribund breakpoint at PC. */
4036
4037int
accd0bcd 4038moribund_breakpoint_here_p (const address_space *aspace, CORE_ADDR pc)
1c5cfe86 4039{
1123588c 4040 for (bp_location *loc : moribund_locations)
f1310107 4041 if (breakpoint_location_address_match (loc, aspace, pc))
1c5cfe86
PA
4042 return 1;
4043
4044 return 0;
4045}
c2c6d25f 4046
f7ce857f
PA
4047/* Returns non-zero iff BL is inserted at PC, in address space
4048 ASPACE. */
4049
4050static int
4051bp_location_inserted_here_p (struct bp_location *bl,
accd0bcd 4052 const address_space *aspace, CORE_ADDR pc)
f7ce857f
PA
4053{
4054 if (bl->inserted
4055 && breakpoint_address_match (bl->pspace->aspace, bl->address,
4056 aspace, pc))
4057 {
4058 if (overlay_debugging
4059 && section_is_overlay (bl->section)
4060 && !section_is_mapped (bl->section))
4061 return 0; /* unmapped overlay -- can't be a match */
4062 else
4063 return 1;
4064 }
4065 return 0;
4066}
4067
a1fd2fa5 4068/* Returns non-zero iff there's a breakpoint inserted at PC. */
c906108c
SS
4069
4070int
accd0bcd 4071breakpoint_inserted_here_p (const address_space *aspace, CORE_ADDR pc)
c906108c 4072{
f7ce857f 4073 struct bp_location **blp, **blp_tmp = NULL;
c906108c 4074
f7ce857f 4075 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
c5aa993b 4076 {
f7ce857f
PA
4077 struct bp_location *bl = *blp;
4078
35df4500
TJB
4079 if (bl->loc_type != bp_loc_software_breakpoint
4080 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
4081 continue;
4082
f7ce857f
PA
4083 if (bp_location_inserted_here_p (bl, aspace, pc))
4084 return 1;
c5aa993b 4085 }
c36b740a
VP
4086 return 0;
4087}
4088
a1fd2fa5
PA
4089/* This function returns non-zero iff there is a software breakpoint
4090 inserted at PC. */
c36b740a
VP
4091
4092int
accd0bcd 4093software_breakpoint_inserted_here_p (const address_space *aspace,
a1fd2fa5 4094 CORE_ADDR pc)
4fa8626c 4095{
f7ce857f 4096 struct bp_location **blp, **blp_tmp = NULL;
4fa8626c 4097
f7ce857f 4098 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4fa8626c 4099 {
f7ce857f
PA
4100 struct bp_location *bl = *blp;
4101
35df4500 4102 if (bl->loc_type != bp_loc_software_breakpoint)
4fa8626c
DJ
4103 continue;
4104
f7ce857f
PA
4105 if (bp_location_inserted_here_p (bl, aspace, pc))
4106 return 1;
4fa8626c
DJ
4107 }
4108
4109 return 0;
9c02b525
PA
4110}
4111
4112/* See breakpoint.h. */
4113
4114int
accd0bcd 4115hardware_breakpoint_inserted_here_p (const address_space *aspace,
9c02b525
PA
4116 CORE_ADDR pc)
4117{
4118 struct bp_location **blp, **blp_tmp = NULL;
9c02b525
PA
4119
4120 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4121 {
4122 struct bp_location *bl = *blp;
4123
4124 if (bl->loc_type != bp_loc_hardware_breakpoint)
4125 continue;
4126
4127 if (bp_location_inserted_here_p (bl, aspace, pc))
4128 return 1;
4129 }
4130
4131 return 0;
4fa8626c
DJ
4132}
4133
9093389c 4134int
accd0bcd 4135hardware_watchpoint_inserted_in_range (const address_space *aspace,
9093389c
PA
4136 CORE_ADDR addr, ULONGEST len)
4137{
4138 struct breakpoint *bpt;
4139
4140 ALL_BREAKPOINTS (bpt)
4141 {
4142 struct bp_location *loc;
4143
4144 if (bpt->type != bp_hardware_watchpoint
4145 && bpt->type != bp_access_watchpoint)
4146 continue;
4147
4148 if (!breakpoint_enabled (bpt))
4149 continue;
4150
4151 for (loc = bpt->loc; loc; loc = loc->next)
4152 if (loc->pspace->aspace == aspace && loc->inserted)
4153 {
4154 CORE_ADDR l, h;
4155
4156 /* Check for intersection. */
768adc05
PA
4157 l = std::max<CORE_ADDR> (loc->address, addr);
4158 h = std::min<CORE_ADDR> (loc->address + loc->length, addr + len);
9093389c
PA
4159 if (l < h)
4160 return 1;
4161 }
4162 }
4163 return 0;
4164}
c906108c 4165\f
c5aa993b 4166
c906108c
SS
4167/* bpstat stuff. External routines' interfaces are documented
4168 in breakpoint.h. */
4169
4170int
c326b90e 4171is_catchpoint (struct breakpoint *ep)
c906108c 4172{
533be4dd 4173 return (ep->type == bp_catchpoint);
c906108c
SS
4174}
4175
f431efe5
PA
4176/* Frees any storage that is part of a bpstat. Does not walk the
4177 'next' chain. */
4178
04afa70c 4179bpstats::~bpstats ()
198757a8 4180{
04afa70c
TT
4181 if (bp_location_at != NULL)
4182 decref_bp_location (&bp_location_at);
198757a8
VP
4183}
4184
c906108c
SS
4185/* Clear a bpstat so that it says we are not at any breakpoint.
4186 Also free any storage that is part of a bpstat. */
4187
4188void
fba45db2 4189bpstat_clear (bpstat *bsp)
c906108c
SS
4190{
4191 bpstat p;
4192 bpstat q;
4193
4194 if (bsp == 0)
4195 return;
4196 p = *bsp;
4197 while (p != NULL)
4198 {
4199 q = p->next;
04afa70c 4200 delete p;
c906108c
SS
4201 p = q;
4202 }
4203 *bsp = NULL;
4204}
4205
04afa70c
TT
4206bpstats::bpstats (const bpstats &other)
4207 : next (NULL),
4208 bp_location_at (other.bp_location_at),
4209 breakpoint_at (other.breakpoint_at),
4210 commands (other.commands),
04afa70c
TT
4211 print (other.print),
4212 stop (other.stop),
4213 print_it (other.print_it)
4214{
850645cf
TT
4215 if (other.old_val != NULL)
4216 old_val = release_value (value_copy (other.old_val.get ()));
04afa70c 4217 incref_bp_location (bp_location_at);
04afa70c
TT
4218}
4219
c906108c
SS
4220/* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
4221 is part of the bpstat is copied as well. */
4222
4223bpstat
fba45db2 4224bpstat_copy (bpstat bs)
c906108c
SS
4225{
4226 bpstat p = NULL;
4227 bpstat tmp;
4228 bpstat retval = NULL;
4229
4230 if (bs == NULL)
4231 return bs;
4232
4233 for (; bs != NULL; bs = bs->next)
4234 {
04afa70c 4235 tmp = new bpstats (*bs);
31cc81e9 4236
c906108c
SS
4237 if (p == NULL)
4238 /* This is the first thing in the chain. */
4239 retval = tmp;
4240 else
4241 p->next = tmp;
4242 p = tmp;
4243 }
4244 p->next = NULL;
4245 return retval;
4246}
4247
4a64f543 4248/* Find the bpstat associated with this breakpoint. */
c906108c
SS
4249
4250bpstat
fba45db2 4251bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
c906108c 4252{
c5aa993b
JM
4253 if (bsp == NULL)
4254 return NULL;
c906108c 4255
c5aa993b
JM
4256 for (; bsp != NULL; bsp = bsp->next)
4257 {
f431efe5 4258 if (bsp->breakpoint_at == breakpoint)
c5aa993b
JM
4259 return bsp;
4260 }
c906108c
SS
4261 return NULL;
4262}
4263
ab04a2af
TT
4264/* See breakpoint.h. */
4265
47591c29 4266int
427cd150 4267bpstat_explains_signal (bpstat bsp, enum gdb_signal sig)
ab04a2af 4268{
ab04a2af
TT
4269 for (; bsp != NULL; bsp = bsp->next)
4270 {
427cd150
TT
4271 if (bsp->breakpoint_at == NULL)
4272 {
4273 /* A moribund location can never explain a signal other than
4274 GDB_SIGNAL_TRAP. */
4275 if (sig == GDB_SIGNAL_TRAP)
47591c29 4276 return 1;
427cd150
TT
4277 }
4278 else
47591c29
PA
4279 {
4280 if (bsp->breakpoint_at->ops->explains_signal (bsp->breakpoint_at,
4281 sig))
4282 return 1;
4283 }
ab04a2af
TT
4284 }
4285
47591c29 4286 return 0;
ab04a2af
TT
4287}
4288
4a64f543
MS
4289/* Put in *NUM the breakpoint number of the first breakpoint we are
4290 stopped at. *BSP upon return is a bpstat which points to the
4291 remaining breakpoints stopped at (but which is not guaranteed to be
4292 good for anything but further calls to bpstat_num).
4293
8671a17b
PA
4294 Return 0 if passed a bpstat which does not indicate any breakpoints.
4295 Return -1 if stopped at a breakpoint that has been deleted since
4296 we set it.
4297 Return 1 otherwise. */
c906108c
SS
4298
4299int
8671a17b 4300bpstat_num (bpstat *bsp, int *num)
c906108c
SS
4301{
4302 struct breakpoint *b;
4303
4304 if ((*bsp) == NULL)
4305 return 0; /* No more breakpoint values */
8671a17b 4306
4a64f543
MS
4307 /* We assume we'll never have several bpstats that correspond to a
4308 single breakpoint -- otherwise, this function might return the
4309 same number more than once and this will look ugly. */
f431efe5 4310 b = (*bsp)->breakpoint_at;
8671a17b
PA
4311 *bsp = (*bsp)->next;
4312 if (b == NULL)
4313 return -1; /* breakpoint that's been deleted since */
4314
4315 *num = b->number; /* We have its number */
4316 return 1;
c906108c
SS
4317}
4318
e93ca019 4319/* See breakpoint.h. */
c906108c
SS
4320
4321void
e93ca019 4322bpstat_clear_actions (void)
c906108c 4323{
e93ca019
JK
4324 bpstat bs;
4325
00431a78 4326 if (inferior_ptid == null_ptid)
e93ca019
JK
4327 return;
4328
00431a78 4329 thread_info *tp = inferior_thread ();
e93ca019 4330 for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
c906108c 4331 {
d1b0a7bf 4332 bs->commands = NULL;
850645cf 4333 bs->old_val.reset (nullptr);
c906108c
SS
4334 }
4335}
4336
f3b1572e
PA
4337/* Called when a command is about to proceed the inferior. */
4338
4339static void
4340breakpoint_about_to_proceed (void)
4341{
d7e15655 4342 if (inferior_ptid != null_ptid)
f3b1572e
PA
4343 {
4344 struct thread_info *tp = inferior_thread ();
4345
4346 /* Allow inferior function calls in breakpoint commands to not
4347 interrupt the command list. When the call finishes
4348 successfully, the inferior will be standing at the same
4349 breakpoint as if nothing happened. */
16c381f0 4350 if (tp->control.in_infcall)
f3b1572e
PA
4351 return;
4352 }
4353
4354 breakpoint_proceeded = 1;
4355}
4356
abf85f46
JK
4357/* Return non-zero iff CMD as the first line of a command sequence is `silent'
4358 or its equivalent. */
4359
4360static int
4361command_line_is_silent (struct command_line *cmd)
4362{
4f45d445 4363 return cmd && (strcmp ("silent", cmd->line) == 0);
abf85f46
JK
4364}
4365
4a64f543
MS
4366/* Execute all the commands associated with all the breakpoints at
4367 this location. Any of these commands could cause the process to
4368 proceed beyond this point, etc. We look out for such changes by
4369 checking the global "breakpoint_proceeded" after each command.
c906108c 4370
347bddb7
PA
4371 Returns true if a breakpoint command resumed the inferior. In that
4372 case, it is the caller's responsibility to recall it again with the
4373 bpstat of the current thread. */
4374
4375static int
4376bpstat_do_actions_1 (bpstat *bsp)
c906108c
SS
4377{
4378 bpstat bs;
347bddb7 4379 int again = 0;
c906108c
SS
4380
4381 /* Avoid endless recursion if a `source' command is contained
4382 in bs->commands. */
4383 if (executing_breakpoint_commands)
347bddb7 4384 return 0;
c906108c 4385
81b1e71c
TT
4386 scoped_restore save_executing
4387 = make_scoped_restore (&executing_breakpoint_commands, 1);
c906108c 4388
1ac32117 4389 scoped_restore preventer = prevent_dont_repeat ();
cf6c5ffb 4390
4a64f543 4391 /* This pointer will iterate over the list of bpstat's. */
c906108c
SS
4392 bs = *bsp;
4393
4394 breakpoint_proceeded = 0;
4395 for (; bs != NULL; bs = bs->next)
4396 {
d1b0a7bf 4397 struct command_line *cmd = NULL;
6c50ab1c
JB
4398
4399 /* Take ownership of the BSP's command tree, if it has one.
4400
4401 The command tree could legitimately contain commands like
4402 'step' and 'next', which call clear_proceed_status, which
4403 frees stop_bpstat's command tree. To make sure this doesn't
4404 free the tree we're executing out from under us, we need to
4405 take ownership of the tree ourselves. Since a given bpstat's
4406 commands are only executed once, we don't need to copy it; we
4407 can clear the pointer in the bpstat, and make sure we free
4408 the tree when we're done. */
d1b0a7bf 4409 counted_command_line ccmd = bs->commands;
9add0f1b 4410 bs->commands = NULL;
d1b0a7bf
TT
4411 if (ccmd != NULL)
4412 cmd = ccmd.get ();
abf85f46
JK
4413 if (command_line_is_silent (cmd))
4414 {
4415 /* The action has been already done by bpstat_stop_status. */
4416 cmd = cmd->next;
4417 }
6c50ab1c 4418
c906108c
SS
4419 while (cmd != NULL)
4420 {
4421 execute_control_command (cmd);
4422
4423 if (breakpoint_proceeded)
4424 break;
4425 else
4426 cmd = cmd->next;
4427 }
6c50ab1c 4428
c906108c 4429 if (breakpoint_proceeded)
32c1e744 4430 {
cb814510 4431 if (current_ui->async)
347bddb7
PA
4432 /* If we are in async mode, then the target might be still
4433 running, not stopped at any breakpoint, so nothing for
4434 us to do here -- just return to the event loop. */
4435 ;
32c1e744
VP
4436 else
4437 /* In sync mode, when execute_control_command returns
4438 we're already standing on the next breakpoint.
347bddb7
PA
4439 Breakpoint commands for that stop were not run, since
4440 execute_command does not run breakpoint commands --
4441 only command_line_handler does, but that one is not
4442 involved in execution of breakpoint commands. So, we
4443 can now execute breakpoint commands. It should be
4444 noted that making execute_command do bpstat actions is
4445 not an option -- in this case we'll have recursive
4446 invocation of bpstat for each breakpoint with a
4447 command, and can easily blow up GDB stack. Instead, we
4448 return true, which will trigger the caller to recall us
4449 with the new stop_bpstat. */
4450 again = 1;
4451 break;
32c1e744 4452 }
c906108c 4453 }
347bddb7
PA
4454 return again;
4455}
4456
00431a78
PA
4457/* Helper for bpstat_do_actions. Get the current thread, if there's
4458 one, is alive and has execution. Return NULL otherwise. */
4459
4460static thread_info *
4461get_bpstat_thread ()
4462{
4463 if (inferior_ptid == null_ptid || !target_has_execution)
4464 return NULL;
4465
4466 thread_info *tp = inferior_thread ();
4467 if (tp->state == THREAD_EXITED || tp->executing)
4468 return NULL;
4469 return tp;
4470}
4471
347bddb7
PA
4472void
4473bpstat_do_actions (void)
4474{
353d1d73 4475 struct cleanup *cleanup_if_error = make_bpstat_clear_actions_cleanup ();
00431a78 4476 thread_info *tp;
353d1d73 4477
347bddb7 4478 /* Do any commands attached to breakpoint we are stopped at. */
00431a78
PA
4479 while ((tp = get_bpstat_thread ()) != NULL)
4480 {
4481 /* Since in sync mode, bpstat_do_actions may resume the
4482 inferior, and only return when it is stopped at the next
4483 breakpoint, we keep doing breakpoint actions until it returns
4484 false to indicate the inferior was not resumed. */
4485 if (!bpstat_do_actions_1 (&tp->control.stop_bpstat))
4486 break;
4487 }
353d1d73
JK
4488
4489 discard_cleanups (cleanup_if_error);
c906108c
SS
4490}
4491
fa4727a6
DJ
4492/* Print out the (old or new) value associated with a watchpoint. */
4493
4494static void
4495watchpoint_value_print (struct value *val, struct ui_file *stream)
4496{
4497 if (val == NULL)
4498 fprintf_unfiltered (stream, _("<unreadable>"));
4499 else
79a45b7d
TT
4500 {
4501 struct value_print_options opts;
4502 get_user_print_options (&opts);
4503 value_print (val, stream, &opts);
4504 }
fa4727a6
DJ
4505}
4506
f303dbd6
PA
4507/* Print the "Thread ID hit" part of "Thread ID hit Breakpoint N" if
4508 debugging multiple threads. */
4509
4510void
4511maybe_print_thread_hit_breakpoint (struct ui_out *uiout)
4512{
112e8700 4513 if (uiout->is_mi_like_p ())
f303dbd6
PA
4514 return;
4515
112e8700 4516 uiout->text ("\n");
f303dbd6
PA
4517
4518 if (show_thread_that_caused_stop ())
4519 {
4520 const char *name;
4521 struct thread_info *thr = inferior_thread ();
4522
112e8700
SM
4523 uiout->text ("Thread ");
4524 uiout->field_fmt ("thread-id", "%s", print_thread_id (thr));
f303dbd6
PA
4525
4526 name = thr->name != NULL ? thr->name : target_thread_name (thr);
4527 if (name != NULL)
4528 {
112e8700
SM
4529 uiout->text (" \"");
4530 uiout->field_fmt ("name", "%s", name);
4531 uiout->text ("\"");
f303dbd6
PA
4532 }
4533
112e8700 4534 uiout->text (" hit ");
f303dbd6
PA
4535 }
4536}
4537
e514a9d6 4538/* Generic routine for printing messages indicating why we
4a64f543 4539 stopped. The behavior of this function depends on the value
e514a9d6
JM
4540 'print_it' in the bpstat structure. Under some circumstances we
4541 may decide not to print anything here and delegate the task to
4a64f543 4542 normal_stop(). */
e514a9d6
JM
4543
4544static enum print_stop_action
4545print_bp_stop_message (bpstat bs)
4546{
4547 switch (bs->print_it)
4548 {
4549 case print_it_noop:
4a64f543 4550 /* Nothing should be printed for this bpstat entry. */
e514a9d6
JM
4551 return PRINT_UNKNOWN;
4552 break;
4553
4554 case print_it_done:
4555 /* We still want to print the frame, but we already printed the
4a64f543 4556 relevant messages. */
e514a9d6
JM
4557 return PRINT_SRC_AND_LOC;
4558 break;
4559
4560 case print_it_normal:
4f8d1dc6 4561 {
f431efe5
PA
4562 struct breakpoint *b = bs->breakpoint_at;
4563
1a6a67de
TJB
4564 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
4565 which has since been deleted. */
4566 if (b == NULL)
4567 return PRINT_UNKNOWN;
4568
348d480f
PA
4569 /* Normal case. Call the breakpoint's print_it method. */
4570 return b->ops->print_it (bs);
4f8d1dc6 4571 }
348d480f 4572 break;
3086aeae 4573
e514a9d6 4574 default:
8e65ff28 4575 internal_error (__FILE__, __LINE__,
e2e0b3e5 4576 _("print_bp_stop_message: unrecognized enum value"));
e514a9d6 4577 break;
c906108c 4578 }
c906108c
SS
4579}
4580
edcc5120
TT
4581/* A helper function that prints a shared library stopped event. */
4582
4583static void
4584print_solib_event (int is_catchpoint)
4585{
6fb16ce6 4586 bool any_deleted = !current_program_space->deleted_solibs.empty ();
bcb430e4 4587 bool any_added = !current_program_space->added_solibs.empty ();
edcc5120
TT
4588
4589 if (!is_catchpoint)
4590 {
4591 if (any_added || any_deleted)
112e8700 4592 current_uiout->text (_("Stopped due to shared library event:\n"));
edcc5120 4593 else
112e8700
SM
4594 current_uiout->text (_("Stopped due to shared library event (no "
4595 "libraries added or removed)\n"));
edcc5120
TT
4596 }
4597
112e8700
SM
4598 if (current_uiout->is_mi_like_p ())
4599 current_uiout->field_string ("reason",
4600 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
edcc5120
TT
4601
4602 if (any_deleted)
4603 {
112e8700 4604 current_uiout->text (_(" Inferior unloaded "));
10f489e5 4605 ui_out_emit_list list_emitter (current_uiout, "removed");
6fb16ce6 4606 for (int ix = 0; ix < current_program_space->deleted_solibs.size (); ix++)
edcc5120 4607 {
6fb16ce6
SM
4608 const std::string &name = current_program_space->deleted_solibs[ix];
4609
edcc5120 4610 if (ix > 0)
112e8700
SM
4611 current_uiout->text (" ");
4612 current_uiout->field_string ("library", name);
4613 current_uiout->text ("\n");
edcc5120 4614 }
edcc5120
TT
4615 }
4616
4617 if (any_added)
4618 {
112e8700 4619 current_uiout->text (_(" Inferior loaded "));
10f489e5 4620 ui_out_emit_list list_emitter (current_uiout, "added");
bcb430e4 4621 bool first = true;
52941706 4622 for (so_list *iter : current_program_space->added_solibs)
edcc5120 4623 {
bcb430e4 4624 if (!first)
112e8700 4625 current_uiout->text (" ");
bcb430e4 4626 first = false;
112e8700
SM
4627 current_uiout->field_string ("library", iter->so_name);
4628 current_uiout->text ("\n");
edcc5120 4629 }
edcc5120
TT
4630 }
4631}
4632
e514a9d6
JM
4633/* Print a message indicating what happened. This is called from
4634 normal_stop(). The input to this routine is the head of the bpstat
36dfb11c
TT
4635 list - a list of the eventpoints that caused this stop. KIND is
4636 the target_waitkind for the stopping event. This
e514a9d6
JM
4637 routine calls the generic print routine for printing a message
4638 about reasons for stopping. This will print (for example) the
4639 "Breakpoint n," part of the output. The return value of this
4640 routine is one of:
c906108c 4641
4a64f543 4642 PRINT_UNKNOWN: Means we printed nothing.
917317f4 4643 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4a64f543 4644 code to print the location. An example is
c5aa993b
JM
4645 "Breakpoint 1, " which should be followed by
4646 the location.
917317f4 4647 PRINT_SRC_ONLY: Means we printed something, but there is no need
c5aa993b
JM
4648 to also print the location part of the message.
4649 An example is the catch/throw messages, which
4a64f543 4650 don't require a location appended to the end.
917317f4 4651 PRINT_NOTHING: We have done some printing and we don't need any
4a64f543 4652 further info to be printed. */
c906108c 4653
917317f4 4654enum print_stop_action
36dfb11c 4655bpstat_print (bpstat bs, int kind)
c906108c 4656{
f486487f 4657 enum print_stop_action val;
c5aa993b 4658
c906108c 4659 /* Maybe another breakpoint in the chain caused us to stop.
53a5351d
JM
4660 (Currently all watchpoints go on the bpstat whether hit or not.
4661 That probably could (should) be changed, provided care is taken
c906108c 4662 with respect to bpstat_explains_signal). */
e514a9d6
JM
4663 for (; bs; bs = bs->next)
4664 {
4665 val = print_bp_stop_message (bs);
4666 if (val == PRINT_SRC_ONLY
4667 || val == PRINT_SRC_AND_LOC
4668 || val == PRINT_NOTHING)
4669 return val;
4670 }
c906108c 4671
36dfb11c
TT
4672 /* If we had hit a shared library event breakpoint,
4673 print_bp_stop_message would print out this message. If we hit an
4674 OS-level shared library event, do the same thing. */
4675 if (kind == TARGET_WAITKIND_LOADED)
4676 {
edcc5120 4677 print_solib_event (0);
36dfb11c
TT
4678 return PRINT_NOTHING;
4679 }
4680
e514a9d6 4681 /* We reached the end of the chain, or we got a null BS to start
4a64f543 4682 with and nothing was printed. */
917317f4 4683 return PRINT_UNKNOWN;
c906108c
SS
4684}
4685
bf469271 4686/* Evaluate the boolean expression EXP and return the result. */
c906108c 4687
bf469271
PA
4688static bool
4689breakpoint_cond_eval (expression *exp)
c906108c 4690{
278cd55f 4691 struct value *mark = value_mark ();
bf469271 4692 bool res = value_true (evaluate_expression (exp));
cc59ec59 4693
c906108c 4694 value_free_to_mark (mark);
bf469271 4695 return res;
c906108c
SS
4696}
4697
5760d0ab 4698/* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
c906108c 4699
04afa70c
TT
4700bpstats::bpstats (struct bp_location *bl, bpstat **bs_link_pointer)
4701 : next (NULL),
4702 bp_location_at (bl),
4703 breakpoint_at (bl->owner),
4704 commands (NULL),
04afa70c
TT
4705 print (0),
4706 stop (0),
4707 print_it (print_it_normal)
c906108c 4708{
f431efe5 4709 incref_bp_location (bl);
04afa70c
TT
4710 **bs_link_pointer = this;
4711 *bs_link_pointer = &next;
4712}
4713
4714bpstats::bpstats ()
4715 : next (NULL),
4716 bp_location_at (NULL),
4717 breakpoint_at (NULL),
4718 commands (NULL),
04afa70c
TT
4719 print (0),
4720 stop (0),
4721 print_it (print_it_normal)
4722{
c906108c
SS
4723}
4724\f
d983da9c
DJ
4725/* The target has stopped with waitstatus WS. Check if any hardware
4726 watchpoints have triggered, according to the target. */
4727
4728int
4729watchpoints_triggered (struct target_waitstatus *ws)
4730{
57810aa7 4731 bool stopped_by_watchpoint = target_stopped_by_watchpoint ();
d983da9c
DJ
4732 CORE_ADDR addr;
4733 struct breakpoint *b;
4734
4735 if (!stopped_by_watchpoint)
4736 {
4737 /* We were not stopped by a watchpoint. Mark all watchpoints
4738 as not triggered. */
4739 ALL_BREAKPOINTS (b)
cc60f2e3 4740 if (is_hardware_watchpoint (b))
3a5c3e22
PA
4741 {
4742 struct watchpoint *w = (struct watchpoint *) b;
4743
4744 w->watchpoint_triggered = watch_triggered_no;
4745 }
d983da9c
DJ
4746
4747 return 0;
4748 }
4749
8b88a78e 4750 if (!target_stopped_data_address (current_top_target (), &addr))
d983da9c
DJ
4751 {
4752 /* We were stopped by a watchpoint, but we don't know where.
4753 Mark all watchpoints as unknown. */
4754 ALL_BREAKPOINTS (b)
cc60f2e3 4755 if (is_hardware_watchpoint (b))
3a5c3e22
PA
4756 {
4757 struct watchpoint *w = (struct watchpoint *) b;
4758
4759 w->watchpoint_triggered = watch_triggered_unknown;
4760 }
d983da9c 4761
3c4797ba 4762 return 1;
d983da9c
DJ
4763 }
4764
4765 /* The target could report the data address. Mark watchpoints
4766 affected by this data address as triggered, and all others as not
4767 triggered. */
4768
4769 ALL_BREAKPOINTS (b)
cc60f2e3 4770 if (is_hardware_watchpoint (b))
d983da9c 4771 {
3a5c3e22 4772 struct watchpoint *w = (struct watchpoint *) b;
a5606eee 4773 struct bp_location *loc;
d983da9c 4774
3a5c3e22 4775 w->watchpoint_triggered = watch_triggered_no;
a5606eee 4776 for (loc = b->loc; loc; loc = loc->next)
9c06b0b4 4777 {
3a5c3e22 4778 if (is_masked_watchpoint (b))
9c06b0b4 4779 {
3a5c3e22
PA
4780 CORE_ADDR newaddr = addr & w->hw_wp_mask;
4781 CORE_ADDR start = loc->address & w->hw_wp_mask;
9c06b0b4
TJB
4782
4783 if (newaddr == start)
4784 {
3a5c3e22 4785 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
4786 break;
4787 }
4788 }
4789 /* Exact match not required. Within range is sufficient. */
8b88a78e 4790 else if (target_watchpoint_addr_within_range (current_top_target (),
9c06b0b4
TJB
4791 addr, loc->address,
4792 loc->length))
4793 {
3a5c3e22 4794 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
4795 break;
4796 }
4797 }
d983da9c
DJ
4798 }
4799
4800 return 1;
4801}
4802
bf469271
PA
4803/* Possible return values for watchpoint_check. */
4804enum wp_check_result
4805 {
4806 /* The watchpoint has been deleted. */
4807 WP_DELETED = 1,
4808
4809 /* The value has changed. */
4810 WP_VALUE_CHANGED = 2,
4811
4812 /* The value has not changed. */
4813 WP_VALUE_NOT_CHANGED = 3,
4814
4815 /* Ignore this watchpoint, no matter if the value changed or not. */
4816 WP_IGNORE = 4,
4817 };
c906108c
SS
4818
4819#define BP_TEMPFLAG 1
4820#define BP_HARDWAREFLAG 2
4821
4a64f543 4822/* Evaluate watchpoint condition expression and check if its value
bf469271 4823 changed. */
553e4c11 4824
bf469271
PA
4825static wp_check_result
4826watchpoint_check (bpstat bs)
c906108c 4827{
3a5c3e22 4828 struct watchpoint *b;
c906108c
SS
4829 struct frame_info *fr;
4830 int within_current_scope;
4831
f431efe5 4832 /* BS is built from an existing struct breakpoint. */
2bdf28a0 4833 gdb_assert (bs->breakpoint_at != NULL);
3a5c3e22 4834 b = (struct watchpoint *) bs->breakpoint_at;
d0fb5eae 4835
f6bc2008
PA
4836 /* If this is a local watchpoint, we only want to check if the
4837 watchpoint frame is in scope if the current thread is the thread
4838 that was used to create the watchpoint. */
4839 if (!watchpoint_in_thread_scope (b))
60e1c644 4840 return WP_IGNORE;
f6bc2008 4841
c906108c
SS
4842 if (b->exp_valid_block == NULL)
4843 within_current_scope = 1;
4844 else
4845 {
edb3359d
DJ
4846 struct frame_info *frame = get_current_frame ();
4847 struct gdbarch *frame_arch = get_frame_arch (frame);
4848 CORE_ADDR frame_pc = get_frame_pc (frame);
4849
c9cf6e20 4850 /* stack_frame_destroyed_p() returns a non-zero value if we're
4a64f543
MS
4851 still in the function but the stack frame has already been
4852 invalidated. Since we can't rely on the values of local
4853 variables after the stack has been destroyed, we are treating
4854 the watchpoint in that state as `not changed' without further
4855 checking. Don't mark watchpoints as changed if the current
4856 frame is in an epilogue - even if they are in some other
4857 frame, our view of the stack is likely to be wrong and
4858 frame_find_by_id could error out. */
c9cf6e20 4859 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
60e1c644 4860 return WP_IGNORE;
a0f49112 4861
101dcfbe 4862 fr = frame_find_by_id (b->watchpoint_frame);
c906108c 4863 within_current_scope = (fr != NULL);
69fbadd5
DJ
4864
4865 /* If we've gotten confused in the unwinder, we might have
4866 returned a frame that can't describe this variable. */
edb3359d
DJ
4867 if (within_current_scope)
4868 {
4869 struct symbol *function;
4870
4871 function = get_frame_function (fr);
4872 if (function == NULL
4873 || !contained_in (b->exp_valid_block,
4874 SYMBOL_BLOCK_VALUE (function)))
4875 within_current_scope = 0;
4876 }
69fbadd5 4877
edb3359d 4878 if (within_current_scope)
c906108c
SS
4879 /* If we end up stopping, the current frame will get selected
4880 in normal_stop. So this call to select_frame won't affect
4881 the user. */
0f7d239c 4882 select_frame (fr);
c906108c 4883 }
c5aa993b 4884
c906108c
SS
4885 if (within_current_scope)
4886 {
4a64f543
MS
4887 /* We use value_{,free_to_}mark because it could be a *long*
4888 time before we return to the command level and call
4889 free_all_values. We can't call free_all_values because we
4890 might be in the middle of evaluating a function call. */
c906108c 4891
0cf6dd15 4892 int pc = 0;
9c06b0b4 4893 struct value *mark;
fa4727a6
DJ
4894 struct value *new_val;
4895
c1fc2657 4896 if (is_masked_watchpoint (b))
9c06b0b4
TJB
4897 /* Since we don't know the exact trigger address (from
4898 stopped_data_address), just tell the user we've triggered
4899 a mask watchpoint. */
4900 return WP_VALUE_CHANGED;
4901
4902 mark = value_mark ();
4d01a485 4903 fetch_subexp_value (b->exp.get (), &pc, &new_val, NULL, NULL, 0);
218d2fc6 4904
bb9d5f81
PP
4905 if (b->val_bitsize != 0)
4906 new_val = extract_bitfield_from_watchpoint_value (b, new_val);
4907
4a64f543
MS
4908 /* We use value_equal_contents instead of value_equal because
4909 the latter coerces an array to a pointer, thus comparing just
4910 the address of the array instead of its contents. This is
4911 not what we want. */
fa4727a6 4912 if ((b->val != NULL) != (new_val != NULL)
850645cf
TT
4913 || (b->val != NULL && !value_equal_contents (b->val.get (),
4914 new_val)))
c906108c 4915 {
c906108c 4916 bs->old_val = b->val;
850645cf 4917 b->val = release_value (new_val);
fa4727a6 4918 b->val_valid = 1;
850645cf
TT
4919 if (new_val != NULL)
4920 value_free_to_mark (mark);
c906108c
SS
4921 return WP_VALUE_CHANGED;
4922 }
4923 else
4924 {
60e1c644 4925 /* Nothing changed. */
c906108c 4926 value_free_to_mark (mark);
c906108c
SS
4927 return WP_VALUE_NOT_CHANGED;
4928 }
4929 }
4930 else
4931 {
4932 /* This seems like the only logical thing to do because
c5aa993b
JM
4933 if we temporarily ignored the watchpoint, then when
4934 we reenter the block in which it is valid it contains
4935 garbage (in the case of a function, it may have two
4936 garbage values, one before and one after the prologue).
4937 So we can't even detect the first assignment to it and
4938 watch after that (since the garbage may or may not equal
4939 the first value assigned). */
348d480f
PA
4940 /* We print all the stop information in
4941 breakpoint_ops->print_it, but in this case, by the time we
4942 call breakpoint_ops->print_it this bp will be deleted
4943 already. So we have no choice but print the information
4944 here. */
468afe6c 4945
0e454242 4946 SWITCH_THRU_ALL_UIS ()
468afe6c
PA
4947 {
4948 struct ui_out *uiout = current_uiout;
4949
112e8700
SM
4950 if (uiout->is_mi_like_p ())
4951 uiout->field_string
4952 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
4953 uiout->text ("\nWatchpoint ");
c1fc2657 4954 uiout->field_int ("wpnum", b->number);
112e8700 4955 uiout->text (" deleted because the program has left the block in\n"
468afe6c
PA
4956 "which its expression is valid.\n");
4957 }
4ce44c66 4958
cdac0397 4959 /* Make sure the watchpoint's commands aren't executed. */
d1b0a7bf 4960 b->commands = NULL;
d0fb5eae 4961 watchpoint_del_at_next_stop (b);
c906108c
SS
4962
4963 return WP_DELETED;
4964 }
4965}
4966
18a18393 4967/* Return true if it looks like target has stopped due to hitting
348d480f
PA
4968 breakpoint location BL. This function does not check if we should
4969 stop, only if BL explains the stop. */
4970
18a18393 4971static int
6c95b8df 4972bpstat_check_location (const struct bp_location *bl,
accd0bcd 4973 const address_space *aspace, CORE_ADDR bp_addr,
09ac7c10 4974 const struct target_waitstatus *ws)
18a18393
VP
4975{
4976 struct breakpoint *b = bl->owner;
4977
348d480f 4978 /* BL is from an existing breakpoint. */
2bdf28a0
JK
4979 gdb_assert (b != NULL);
4980
bd522513 4981 return b->ops->breakpoint_hit (bl, aspace, bp_addr, ws);
18a18393
VP
4982}
4983
3a5c3e22
PA
4984/* Determine if the watched values have actually changed, and we
4985 should stop. If not, set BS->stop to 0. */
4986
18a18393
VP
4987static void
4988bpstat_check_watchpoint (bpstat bs)
4989{
2bdf28a0 4990 const struct bp_location *bl;
3a5c3e22 4991 struct watchpoint *b;
2bdf28a0
JK
4992
4993 /* BS is built for existing struct breakpoint. */
f431efe5 4994 bl = bs->bp_location_at;
2bdf28a0 4995 gdb_assert (bl != NULL);
3a5c3e22 4996 b = (struct watchpoint *) bs->breakpoint_at;
2bdf28a0 4997 gdb_assert (b != NULL);
18a18393 4998
18a18393 4999 {
18a18393
VP
5000 int must_check_value = 0;
5001
c1fc2657 5002 if (b->type == bp_watchpoint)
18a18393
VP
5003 /* For a software watchpoint, we must always check the
5004 watched value. */
5005 must_check_value = 1;
5006 else if (b->watchpoint_triggered == watch_triggered_yes)
5007 /* We have a hardware watchpoint (read, write, or access)
5008 and the target earlier reported an address watched by
5009 this watchpoint. */
5010 must_check_value = 1;
5011 else if (b->watchpoint_triggered == watch_triggered_unknown
c1fc2657 5012 && b->type == bp_hardware_watchpoint)
18a18393
VP
5013 /* We were stopped by a hardware watchpoint, but the target could
5014 not report the data address. We must check the watchpoint's
5015 value. Access and read watchpoints are out of luck; without
5016 a data address, we can't figure it out. */
5017 must_check_value = 1;
3a5c3e22 5018
18a18393
VP
5019 if (must_check_value)
5020 {
bf469271
PA
5021 wp_check_result e;
5022
5023 TRY
5024 {
5025 e = watchpoint_check (bs);
5026 }
5027 CATCH (ex, RETURN_MASK_ALL)
5028 {
5029 exception_fprintf (gdb_stderr, ex,
5030 "Error evaluating expression "
5031 "for watchpoint %d\n",
5032 b->number);
5033
5034 SWITCH_THRU_ALL_UIS ()
5035 {
5036 printf_filtered (_("Watchpoint %d deleted.\n"),
5037 b->number);
5038 }
5039 watchpoint_del_at_next_stop (b);
5040 e = WP_DELETED;
5041 }
5042 END_CATCH
5043
18a18393
VP
5044 switch (e)
5045 {
5046 case WP_DELETED:
5047 /* We've already printed what needs to be printed. */
5048 bs->print_it = print_it_done;
5049 /* Stop. */
5050 break;
60e1c644
PA
5051 case WP_IGNORE:
5052 bs->print_it = print_it_noop;
5053 bs->stop = 0;
5054 break;
18a18393 5055 case WP_VALUE_CHANGED:
c1fc2657 5056 if (b->type == bp_read_watchpoint)
18a18393 5057 {
85d721b8
PA
5058 /* There are two cases to consider here:
5059
4a64f543 5060 1. We're watching the triggered memory for reads.
85d721b8
PA
5061 In that case, trust the target, and always report
5062 the watchpoint hit to the user. Even though
5063 reads don't cause value changes, the value may
5064 have changed since the last time it was read, and
5065 since we're not trapping writes, we will not see
5066 those, and as such we should ignore our notion of
5067 old value.
5068
4a64f543 5069 2. We're watching the triggered memory for both
85d721b8
PA
5070 reads and writes. There are two ways this may
5071 happen:
5072
4a64f543 5073 2.1. This is a target that can't break on data
85d721b8
PA
5074 reads only, but can break on accesses (reads or
5075 writes), such as e.g., x86. We detect this case
5076 at the time we try to insert read watchpoints.
5077
4a64f543 5078 2.2. Otherwise, the target supports read
85d721b8
PA
5079 watchpoints, but, the user set an access or write
5080 watchpoint watching the same memory as this read
5081 watchpoint.
5082
5083 If we're watching memory writes as well as reads,
5084 ignore watchpoint hits when we find that the
5085 value hasn't changed, as reads don't cause
5086 changes. This still gives false positives when
5087 the program writes the same value to memory as
5088 what there was already in memory (we will confuse
5089 it for a read), but it's much better than
5090 nothing. */
5091
5092 int other_write_watchpoint = 0;
5093
5094 if (bl->watchpoint_type == hw_read)
5095 {
5096 struct breakpoint *other_b;
5097
5098 ALL_BREAKPOINTS (other_b)
3a5c3e22
PA
5099 if (other_b->type == bp_hardware_watchpoint
5100 || other_b->type == bp_access_watchpoint)
85d721b8 5101 {
3a5c3e22
PA
5102 struct watchpoint *other_w =
5103 (struct watchpoint *) other_b;
5104
5105 if (other_w->watchpoint_triggered
5106 == watch_triggered_yes)
5107 {
5108 other_write_watchpoint = 1;
5109 break;
5110 }
85d721b8
PA
5111 }
5112 }
5113
5114 if (other_write_watchpoint
5115 || bl->watchpoint_type == hw_access)
5116 {
5117 /* We're watching the same memory for writes,
5118 and the value changed since the last time we
5119 updated it, so this trap must be for a write.
5120 Ignore it. */
5121 bs->print_it = print_it_noop;
5122 bs->stop = 0;
5123 }
18a18393
VP
5124 }
5125 break;
5126 case WP_VALUE_NOT_CHANGED:
c1fc2657
SM
5127 if (b->type == bp_hardware_watchpoint
5128 || b->type == bp_watchpoint)
18a18393
VP
5129 {
5130 /* Don't stop: write watchpoints shouldn't fire if
5131 the value hasn't changed. */
5132 bs->print_it = print_it_noop;
5133 bs->stop = 0;
5134 }
5135 /* Stop. */
5136 break;
5137 default:
5138 /* Can't happen. */
18a18393
VP
5139 break;
5140 }
5141 }
5142 else /* must_check_value == 0 */
5143 {
5144 /* This is a case where some watchpoint(s) triggered, but
5145 not at the address of this watchpoint, or else no
5146 watchpoint triggered after all. So don't print
5147 anything for this watchpoint. */
5148 bs->print_it = print_it_noop;
5149 bs->stop = 0;
5150 }
5151 }
5152}
5153
7d4df6a4
DE
5154/* For breakpoints that are currently marked as telling gdb to stop,
5155 check conditions (condition proper, frame, thread and ignore count)
18a18393
VP
5156 of breakpoint referred to by BS. If we should not stop for this
5157 breakpoint, set BS->stop to 0. */
f431efe5 5158
18a18393 5159static void
00431a78 5160bpstat_check_breakpoint_conditions (bpstat bs, thread_info *thread)
18a18393 5161{
2bdf28a0
JK
5162 const struct bp_location *bl;
5163 struct breakpoint *b;
bf469271
PA
5164 /* Assume stop. */
5165 bool condition_result = true;
7d4df6a4
DE
5166 struct expression *cond;
5167
5168 gdb_assert (bs->stop);
2bdf28a0
JK
5169
5170 /* BS is built for existing struct breakpoint. */
f431efe5 5171 bl = bs->bp_location_at;
2bdf28a0 5172 gdb_assert (bl != NULL);
f431efe5 5173 b = bs->breakpoint_at;
2bdf28a0 5174 gdb_assert (b != NULL);
18a18393 5175
b775012e
LM
5176 /* Even if the target evaluated the condition on its end and notified GDB, we
5177 need to do so again since GDB does not know if we stopped due to a
5178 breakpoint or a single step breakpoint. */
5179
18a18393 5180 if (frame_id_p (b->frame_id)
edb3359d 5181 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
18a18393 5182 {
7d4df6a4
DE
5183 bs->stop = 0;
5184 return;
5185 }
60e1c644 5186
12ab52e9
PA
5187 /* If this is a thread/task-specific breakpoint, don't waste cpu
5188 evaluating the condition if this isn't the specified
5189 thread/task. */
00431a78
PA
5190 if ((b->thread != -1 && b->thread != thread->global_num)
5191 || (b->task != 0 && b->task != ada_get_task_number (thread)))
6c1b0f7b
DE
5192 {
5193 bs->stop = 0;
5194 return;
5195 }
5196
6dddc817
DE
5197 /* Evaluate extension language breakpoints that have a "stop" method
5198 implemented. */
5199 bs->stop = breakpoint_ext_lang_cond_says_stop (b);
7371cf6d 5200
7d4df6a4
DE
5201 if (is_watchpoint (b))
5202 {
5203 struct watchpoint *w = (struct watchpoint *) b;
3a5c3e22 5204
4d01a485 5205 cond = w->cond_exp.get ();
7d4df6a4
DE
5206 }
5207 else
4d01a485 5208 cond = bl->cond.get ();
60e1c644 5209
7d4df6a4
DE
5210 if (cond && b->disposition != disp_del_at_next_stop)
5211 {
5212 int within_current_scope = 1;
5213 struct watchpoint * w;
60e1c644 5214
7d4df6a4
DE
5215 /* We use value_mark and value_free_to_mark because it could
5216 be a long time before we return to the command level and
5217 call free_all_values. We can't call free_all_values
5218 because we might be in the middle of evaluating a
5219 function call. */
5220 struct value *mark = value_mark ();
5221
5222 if (is_watchpoint (b))
5223 w = (struct watchpoint *) b;
5224 else
5225 w = NULL;
5226
5227 /* Need to select the frame, with all that implies so that
5228 the conditions will have the right context. Because we
5229 use the frame, we will not see an inlined function's
5230 variables when we arrive at a breakpoint at the start
5231 of the inlined function; the current frame will be the
5232 call site. */
5233 if (w == NULL || w->cond_exp_valid_block == NULL)
5234 select_frame (get_current_frame ());
5235 else
18a18393 5236 {
7d4df6a4
DE
5237 struct frame_info *frame;
5238
5239 /* For local watchpoint expressions, which particular
5240 instance of a local is being watched matters, so we
5241 keep track of the frame to evaluate the expression
5242 in. To evaluate the condition however, it doesn't
5243 really matter which instantiation of the function
5244 where the condition makes sense triggers the
5245 watchpoint. This allows an expression like "watch
5246 global if q > 10" set in `func', catch writes to
5247 global on all threads that call `func', or catch
5248 writes on all recursive calls of `func' by a single
5249 thread. We simply always evaluate the condition in
5250 the innermost frame that's executing where it makes
5251 sense to evaluate the condition. It seems
5252 intuitive. */
5253 frame = block_innermost_frame (w->cond_exp_valid_block);
5254 if (frame != NULL)
5255 select_frame (frame);
5256 else
5257 within_current_scope = 0;
18a18393 5258 }
7d4df6a4 5259 if (within_current_scope)
bf469271
PA
5260 {
5261 TRY
5262 {
5263 condition_result = breakpoint_cond_eval (cond);
5264 }
5265 CATCH (ex, RETURN_MASK_ALL)
5266 {
5267 exception_fprintf (gdb_stderr, ex,
5268 "Error in testing breakpoint condition:\n");
5269 }
5270 END_CATCH
5271 }
7d4df6a4 5272 else
18a18393 5273 {
7d4df6a4
DE
5274 warning (_("Watchpoint condition cannot be tested "
5275 "in the current scope"));
5276 /* If we failed to set the right context for this
5277 watchpoint, unconditionally report it. */
18a18393 5278 }
7d4df6a4
DE
5279 /* FIXME-someday, should give breakpoint #. */
5280 value_free_to_mark (mark);
18a18393 5281 }
7d4df6a4 5282
bf469271 5283 if (cond && !condition_result)
7d4df6a4
DE
5284 {
5285 bs->stop = 0;
5286 }
7d4df6a4
DE
5287 else if (b->ignore_count > 0)
5288 {
5289 b->ignore_count--;
5290 bs->stop = 0;
5291 /* Increase the hit count even though we don't stop. */
5292 ++(b->hit_count);
76727919 5293 gdb::observers::breakpoint_modified.notify (b);
7d4df6a4 5294 }
18a18393
VP
5295}
5296
1cf4d951
PA
5297/* Returns true if we need to track moribund locations of LOC's type
5298 on the current target. */
5299
5300static int
5301need_moribund_for_location_type (struct bp_location *loc)
5302{
5303 return ((loc->loc_type == bp_loc_software_breakpoint
5304 && !target_supports_stopped_by_sw_breakpoint ())
5305 || (loc->loc_type == bp_loc_hardware_breakpoint
5306 && !target_supports_stopped_by_hw_breakpoint ()));
5307}
5308
ddfe970e 5309/* See breakpoint.h. */
c906108c
SS
5310
5311bpstat
ddfe970e 5312build_bpstat_chain (const address_space *aspace, CORE_ADDR bp_addr,
09ac7c10 5313 const struct target_waitstatus *ws)
c906108c 5314{
ddfe970e 5315 struct breakpoint *b;
5760d0ab 5316 bpstat bs_head = NULL, *bs_link = &bs_head;
c5aa993b 5317
429374b8
JK
5318 ALL_BREAKPOINTS (b)
5319 {
1a853c52 5320 if (!breakpoint_enabled (b))
429374b8 5321 continue;
a5606eee 5322
ddfe970e 5323 for (bp_location *bl = b->loc; bl != NULL; bl = bl->next)
429374b8 5324 {
4a64f543
MS
5325 /* For hardware watchpoints, we look only at the first
5326 location. The watchpoint_check function will work on the
5327 entire expression, not the individual locations. For
5328 read watchpoints, the watchpoints_triggered function has
5329 checked all locations already. */
429374b8
JK
5330 if (b->type == bp_hardware_watchpoint && bl != b->loc)
5331 break;
18a18393 5332
f6592439 5333 if (!bl->enabled || bl->shlib_disabled)
429374b8 5334 continue;
c5aa993b 5335
09ac7c10 5336 if (!bpstat_check_location (bl, aspace, bp_addr, ws))
429374b8 5337 continue;
c5aa993b 5338
4a64f543
MS
5339 /* Come here if it's a watchpoint, or if the break address
5340 matches. */
c5aa993b 5341
ddfe970e
KS
5342 bpstat bs = new bpstats (bl, &bs_link); /* Alloc a bpstat to
5343 explain stop. */
c5aa993b 5344
f431efe5
PA
5345 /* Assume we stop. Should we find a watchpoint that is not
5346 actually triggered, or if the condition of the breakpoint
5347 evaluates as false, we'll reset 'stop' to 0. */
429374b8
JK
5348 bs->stop = 1;
5349 bs->print = 1;
d983da9c 5350
f431efe5
PA
5351 /* If this is a scope breakpoint, mark the associated
5352 watchpoint as triggered so that we will handle the
5353 out-of-scope event. We'll get to the watchpoint next
5354 iteration. */
d0fb5eae 5355 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
3a5c3e22
PA
5356 {
5357 struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
5358
5359 w->watchpoint_triggered = watch_triggered_yes;
5360 }
f431efe5
PA
5361 }
5362 }
5363
7c16b83e 5364 /* Check if a moribund breakpoint explains the stop. */
1cf4d951
PA
5365 if (!target_supports_stopped_by_sw_breakpoint ()
5366 || !target_supports_stopped_by_hw_breakpoint ())
f431efe5 5367 {
1123588c 5368 for (bp_location *loc : moribund_locations)
f431efe5 5369 {
1cf4d951
PA
5370 if (breakpoint_location_address_match (loc, aspace, bp_addr)
5371 && need_moribund_for_location_type (loc))
5372 {
ddfe970e 5373 bpstat bs = new bpstats (loc, &bs_link);
1cf4d951
PA
5374 /* For hits of moribund locations, we should just proceed. */
5375 bs->stop = 0;
5376 bs->print = 0;
5377 bs->print_it = print_it_noop;
5378 }
f431efe5
PA
5379 }
5380 }
5381
ddfe970e
KS
5382 return bs_head;
5383}
5384
5385/* See breakpoint.h. */
5386
5387bpstat
5388bpstat_stop_status (const address_space *aspace,
00431a78 5389 CORE_ADDR bp_addr, thread_info *thread,
ddfe970e
KS
5390 const struct target_waitstatus *ws,
5391 bpstat stop_chain)
5392{
5393 struct breakpoint *b = NULL;
5394 /* First item of allocated bpstat's. */
5395 bpstat bs_head = stop_chain;
5396 bpstat bs;
5397 int need_remove_insert;
5398 int removed_any;
5399
5400 /* First, build the bpstat chain with locations that explain a
5401 target stop, while being careful to not set the target running,
5402 as that may invalidate locations (in particular watchpoint
5403 locations are recreated). Resuming will happen here with
5404 breakpoint conditions or watchpoint expressions that include
5405 inferior function calls. */
5406 if (bs_head == NULL)
5407 bs_head = build_bpstat_chain (aspace, bp_addr, ws);
5408
edcc5120
TT
5409 /* A bit of special processing for shlib breakpoints. We need to
5410 process solib loading here, so that the lists of loaded and
5411 unloaded libraries are correct before we handle "catch load" and
5412 "catch unload". */
5413 for (bs = bs_head; bs != NULL; bs = bs->next)
5414 {
5d268276 5415 if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
edcc5120
TT
5416 {
5417 handle_solib_event ();
5418 break;
5419 }
5420 }
5421
f431efe5
PA
5422 /* Now go through the locations that caused the target to stop, and
5423 check whether we're interested in reporting this stop to higher
5424 layers, or whether we should resume the target transparently. */
5425
5426 removed_any = 0;
5427
5760d0ab 5428 for (bs = bs_head; bs != NULL; bs = bs->next)
f431efe5
PA
5429 {
5430 if (!bs->stop)
5431 continue;
5432
f431efe5 5433 b = bs->breakpoint_at;
348d480f
PA
5434 b->ops->check_status (bs);
5435 if (bs->stop)
28010a5d 5436 {
00431a78 5437 bpstat_check_breakpoint_conditions (bs, thread);
f431efe5 5438
429374b8
JK
5439 if (bs->stop)
5440 {
5441 ++(b->hit_count);
76727919 5442 gdb::observers::breakpoint_modified.notify (b);
c906108c 5443
4a64f543 5444 /* We will stop here. */
429374b8
JK
5445 if (b->disposition == disp_disable)
5446 {
816338b5 5447 --(b->enable_count);
1a853c52 5448 if (b->enable_count <= 0)
429374b8 5449 b->enable_state = bp_disabled;
f431efe5 5450 removed_any = 1;
429374b8
JK
5451 }
5452 if (b->silent)
5453 bs->print = 0;
5454 bs->commands = b->commands;
abf85f46 5455 if (command_line_is_silent (bs->commands
d1b0a7bf 5456 ? bs->commands.get () : NULL))
abf85f46 5457 bs->print = 0;
9d6e6e84
HZ
5458
5459 b->ops->after_condition_true (bs);
429374b8
JK
5460 }
5461
348d480f 5462 }
a9b3a50f
PA
5463
5464 /* Print nothing for this entry if we don't stop or don't
5465 print. */
5466 if (!bs->stop || !bs->print)
5467 bs->print_it = print_it_noop;
429374b8 5468 }
876fa593 5469
d983da9c
DJ
5470 /* If we aren't stopping, the value of some hardware watchpoint may
5471 not have changed, but the intermediate memory locations we are
5472 watching may have. Don't bother if we're stopping; this will get
5473 done later. */
d832cb68 5474 need_remove_insert = 0;
5760d0ab
JK
5475 if (! bpstat_causes_stop (bs_head))
5476 for (bs = bs_head; bs != NULL; bs = bs->next)
d983da9c 5477 if (!bs->stop
f431efe5
PA
5478 && bs->breakpoint_at
5479 && is_hardware_watchpoint (bs->breakpoint_at))
d983da9c 5480 {
3a5c3e22
PA
5481 struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
5482
5483 update_watchpoint (w, 0 /* don't reparse. */);
d832cb68 5484 need_remove_insert = 1;
d983da9c
DJ
5485 }
5486
d832cb68 5487 if (need_remove_insert)
44702360 5488 update_global_location_list (UGLL_MAY_INSERT);
f431efe5 5489 else if (removed_any)
44702360 5490 update_global_location_list (UGLL_DONT_INSERT);
d832cb68 5491
5760d0ab 5492 return bs_head;
c906108c 5493}
628fe4e4
JK
5494
5495static void
5496handle_jit_event (void)
5497{
5498 struct frame_info *frame;
5499 struct gdbarch *gdbarch;
5500
243a9253
PA
5501 if (debug_infrun)
5502 fprintf_unfiltered (gdb_stdlog, "handling bp_jit_event\n");
5503
628fe4e4
JK
5504 /* Switch terminal for any messages produced by
5505 breakpoint_re_set. */
223ffa71 5506 target_terminal::ours_for_output ();
628fe4e4
JK
5507
5508 frame = get_current_frame ();
5509 gdbarch = get_frame_arch (frame);
5510
5511 jit_event_handler (gdbarch);
5512
223ffa71 5513 target_terminal::inferior ();
628fe4e4
JK
5514}
5515
5516/* Prepare WHAT final decision for infrun. */
5517
5518/* Decide what infrun needs to do with this bpstat. */
5519
c906108c 5520struct bpstat_what
0e30163f 5521bpstat_what (bpstat bs_head)
c906108c 5522{
c906108c 5523 struct bpstat_what retval;
0e30163f 5524 bpstat bs;
c906108c 5525
628fe4e4 5526 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
aa7d318d 5527 retval.call_dummy = STOP_NONE;
186c406b 5528 retval.is_longjmp = 0;
628fe4e4 5529
0e30163f 5530 for (bs = bs_head; bs != NULL; bs = bs->next)
c906108c 5531 {
628fe4e4
JK
5532 /* Extract this BS's action. After processing each BS, we check
5533 if its action overrides all we've seem so far. */
5534 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
5535 enum bptype bptype;
5536
c906108c 5537 if (bs->breakpoint_at == NULL)
628fe4e4
JK
5538 {
5539 /* I suspect this can happen if it was a momentary
5540 breakpoint which has since been deleted. */
5541 bptype = bp_none;
5542 }
20874c92 5543 else
f431efe5 5544 bptype = bs->breakpoint_at->type;
628fe4e4
JK
5545
5546 switch (bptype)
c906108c
SS
5547 {
5548 case bp_none:
628fe4e4 5549 break;
c906108c
SS
5550 case bp_breakpoint:
5551 case bp_hardware_breakpoint:
7c16b83e 5552 case bp_single_step:
c906108c
SS
5553 case bp_until:
5554 case bp_finish:
a9b3a50f 5555 case bp_shlib_event:
c906108c
SS
5556 if (bs->stop)
5557 {
5558 if (bs->print)
628fe4e4 5559 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 5560 else
628fe4e4 5561 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
5562 }
5563 else
628fe4e4 5564 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5565 break;
5566 case bp_watchpoint:
5567 case bp_hardware_watchpoint:
5568 case bp_read_watchpoint:
5569 case bp_access_watchpoint:
5570 if (bs->stop)
5571 {
5572 if (bs->print)
628fe4e4 5573 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 5574 else
628fe4e4 5575 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
5576 }
5577 else
628fe4e4
JK
5578 {
5579 /* There was a watchpoint, but we're not stopping.
5580 This requires no further action. */
5581 }
c906108c
SS
5582 break;
5583 case bp_longjmp:
e2e4d78b 5584 case bp_longjmp_call_dummy:
186c406b 5585 case bp_exception:
0a39bb32
PA
5586 if (bs->stop)
5587 {
5588 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
5589 retval.is_longjmp = bptype != bp_exception;
5590 }
5591 else
5592 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5593 break;
5594 case bp_longjmp_resume:
186c406b 5595 case bp_exception_resume:
0a39bb32
PA
5596 if (bs->stop)
5597 {
5598 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
5599 retval.is_longjmp = bptype == bp_longjmp_resume;
5600 }
5601 else
5602 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5603 break;
5604 case bp_step_resume:
5605 if (bs->stop)
628fe4e4
JK
5606 this_action = BPSTAT_WHAT_STEP_RESUME;
5607 else
c906108c 5608 {
628fe4e4
JK
5609 /* It is for the wrong frame. */
5610 this_action = BPSTAT_WHAT_SINGLE;
c906108c 5611 }
c906108c 5612 break;
2c03e5be
PA
5613 case bp_hp_step_resume:
5614 if (bs->stop)
5615 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
5616 else
5617 {
5618 /* It is for the wrong frame. */
5619 this_action = BPSTAT_WHAT_SINGLE;
5620 }
5621 break;
c906108c 5622 case bp_watchpoint_scope:
c4093a6a 5623 case bp_thread_event:
1900040c 5624 case bp_overlay_event:
0fd8e87f 5625 case bp_longjmp_master:
aa7d318d 5626 case bp_std_terminate_master:
186c406b 5627 case bp_exception_master:
628fe4e4 5628 this_action = BPSTAT_WHAT_SINGLE;
c4093a6a 5629 break;
ce78b96d 5630 case bp_catchpoint:
c5aa993b
JM
5631 if (bs->stop)
5632 {
5633 if (bs->print)
628fe4e4 5634 this_action = BPSTAT_WHAT_STOP_NOISY;
c5aa993b 5635 else
628fe4e4 5636 this_action = BPSTAT_WHAT_STOP_SILENT;
c5aa993b
JM
5637 }
5638 else
628fe4e4
JK
5639 {
5640 /* There was a catchpoint, but we're not stopping.
5641 This requires no further action. */
5642 }
5643 break;
628fe4e4 5644 case bp_jit_event:
628fe4e4 5645 this_action = BPSTAT_WHAT_SINGLE;
c5aa993b 5646 break;
c906108c 5647 case bp_call_dummy:
53a5351d
JM
5648 /* Make sure the action is stop (silent or noisy),
5649 so infrun.c pops the dummy frame. */
aa7d318d 5650 retval.call_dummy = STOP_STACK_DUMMY;
628fe4e4 5651 this_action = BPSTAT_WHAT_STOP_SILENT;
aa7d318d
TT
5652 break;
5653 case bp_std_terminate:
5654 /* Make sure the action is stop (silent or noisy),
5655 so infrun.c pops the dummy frame. */
aa7d318d 5656 retval.call_dummy = STOP_STD_TERMINATE;
628fe4e4 5657 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c 5658 break;
1042e4c0 5659 case bp_tracepoint:
7a697b8d 5660 case bp_fast_tracepoint:
0fb4aa4b 5661 case bp_static_tracepoint:
1042e4c0
SS
5662 /* Tracepoint hits should not be reported back to GDB, and
5663 if one got through somehow, it should have been filtered
5664 out already. */
5665 internal_error (__FILE__, __LINE__,
7a697b8d 5666 _("bpstat_what: tracepoint encountered"));
0e30163f
JK
5667 break;
5668 case bp_gnu_ifunc_resolver:
5669 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
5670 this_action = BPSTAT_WHAT_SINGLE;
5671 break;
5672 case bp_gnu_ifunc_resolver_return:
5673 /* The breakpoint will be removed, execution will restart from the
5674 PC of the former breakpoint. */
5675 this_action = BPSTAT_WHAT_KEEP_CHECKING;
5676 break;
e7e0cddf
SS
5677
5678 case bp_dprintf:
a11cfd87
HZ
5679 if (bs->stop)
5680 this_action = BPSTAT_WHAT_STOP_SILENT;
5681 else
5682 this_action = BPSTAT_WHAT_SINGLE;
e7e0cddf
SS
5683 break;
5684
628fe4e4
JK
5685 default:
5686 internal_error (__FILE__, __LINE__,
5687 _("bpstat_what: unhandled bptype %d"), (int) bptype);
c906108c 5688 }
628fe4e4 5689
325fac50 5690 retval.main_action = std::max (retval.main_action, this_action);
c906108c 5691 }
628fe4e4 5692
243a9253
PA
5693 return retval;
5694}
628fe4e4 5695
243a9253
PA
5696void
5697bpstat_run_callbacks (bpstat bs_head)
5698{
5699 bpstat bs;
628fe4e4 5700
0e30163f
JK
5701 for (bs = bs_head; bs != NULL; bs = bs->next)
5702 {
5703 struct breakpoint *b = bs->breakpoint_at;
5704
5705 if (b == NULL)
5706 continue;
5707 switch (b->type)
5708 {
243a9253
PA
5709 case bp_jit_event:
5710 handle_jit_event ();
5711 break;
0e30163f
JK
5712 case bp_gnu_ifunc_resolver:
5713 gnu_ifunc_resolver_stop (b);
5714 break;
5715 case bp_gnu_ifunc_resolver_return:
5716 gnu_ifunc_resolver_return_stop (b);
5717 break;
5718 }
5719 }
c906108c
SS
5720}
5721
5722/* Nonzero if we should step constantly (e.g. watchpoints on machines
5723 without hardware support). This isn't related to a specific bpstat,
5724 just to things like whether watchpoints are set. */
5725
c5aa993b 5726int
fba45db2 5727bpstat_should_step (void)
c906108c
SS
5728{
5729 struct breakpoint *b;
cc59ec59 5730
c906108c 5731 ALL_BREAKPOINTS (b)
717a8278 5732 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
3172dc30 5733 return 1;
c906108c
SS
5734 return 0;
5735}
5736
67822962
PA
5737int
5738bpstat_causes_stop (bpstat bs)
5739{
5740 for (; bs != NULL; bs = bs->next)
5741 if (bs->stop)
5742 return 1;
5743
5744 return 0;
5745}
5746
c906108c 5747\f
c5aa993b 5748
170b53b2
UW
5749/* Compute a string of spaces suitable to indent the next line
5750 so it starts at the position corresponding to the table column
5751 named COL_NAME in the currently active table of UIOUT. */
5752
5753static char *
5754wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
5755{
5756 static char wrap_indent[80];
5757 int i, total_width, width, align;
c5209615 5758 const char *text;
170b53b2
UW
5759
5760 total_width = 0;
112e8700 5761 for (i = 1; uiout->query_table_field (i, &width, &align, &text); i++)
170b53b2
UW
5762 {
5763 if (strcmp (text, col_name) == 0)
5764 {
5765 gdb_assert (total_width < sizeof wrap_indent);
5766 memset (wrap_indent, ' ', total_width);
5767 wrap_indent[total_width] = 0;
5768
5769 return wrap_indent;
5770 }
5771
5772 total_width += width + 1;
5773 }
5774
5775 return NULL;
5776}
5777
b775012e
LM
5778/* Determine if the locations of this breakpoint will have their conditions
5779 evaluated by the target, host or a mix of both. Returns the following:
5780
5781 "host": Host evals condition.
5782 "host or target": Host or Target evals condition.
5783 "target": Target evals condition.
5784*/
5785
5786static const char *
5787bp_condition_evaluator (struct breakpoint *b)
5788{
5789 struct bp_location *bl;
5790 char host_evals = 0;
5791 char target_evals = 0;
5792
5793 if (!b)
5794 return NULL;
5795
5796 if (!is_breakpoint (b))
5797 return NULL;
5798
5799 if (gdb_evaluates_breakpoint_condition_p ()
5800 || !target_supports_evaluation_of_breakpoint_conditions ())
5801 return condition_evaluation_host;
5802
5803 for (bl = b->loc; bl; bl = bl->next)
5804 {
5805 if (bl->cond_bytecode)
5806 target_evals++;
5807 else
5808 host_evals++;
5809 }
5810
5811 if (host_evals && target_evals)
5812 return condition_evaluation_both;
5813 else if (target_evals)
5814 return condition_evaluation_target;
5815 else
5816 return condition_evaluation_host;
5817}
5818
5819/* Determine the breakpoint location's condition evaluator. This is
5820 similar to bp_condition_evaluator, but for locations. */
5821
5822static const char *
5823bp_location_condition_evaluator (struct bp_location *bl)
5824{
5825 if (bl && !is_breakpoint (bl->owner))
5826 return NULL;
5827
5828 if (gdb_evaluates_breakpoint_condition_p ()
5829 || !target_supports_evaluation_of_breakpoint_conditions ())
5830 return condition_evaluation_host;
5831
5832 if (bl && bl->cond_bytecode)
5833 return condition_evaluation_target;
5834 else
5835 return condition_evaluation_host;
5836}
5837
859825b8
JK
5838/* Print the LOC location out of the list of B->LOC locations. */
5839
170b53b2
UW
5840static void
5841print_breakpoint_location (struct breakpoint *b,
5842 struct bp_location *loc)
0d381245 5843{
79a45e25 5844 struct ui_out *uiout = current_uiout;
5ed8105e
PA
5845
5846 scoped_restore_current_program_space restore_pspace;
6c95b8df 5847
859825b8
JK
5848 if (loc != NULL && loc->shlib_disabled)
5849 loc = NULL;
5850
6c95b8df
PA
5851 if (loc != NULL)
5852 set_current_program_space (loc->pspace);
5853
56435ebe 5854 if (b->display_canonical)
d28cd78a 5855 uiout->field_string ("what", event_location_to_string (b->location.get ()));
2f202fde 5856 else if (loc && loc->symtab)
0d381245 5857 {
4a27f119
KS
5858 const struct symbol *sym = loc->symbol;
5859
0d381245
VP
5860 if (sym)
5861 {
112e8700
SM
5862 uiout->text ("in ");
5863 uiout->field_string ("func", SYMBOL_PRINT_NAME (sym));
5864 uiout->text (" ");
5865 uiout->wrap_hint (wrap_indent_at_field (uiout, "what"));
5866 uiout->text ("at ");
0d381245 5867 }
112e8700 5868 uiout->field_string ("file",
05cba821 5869 symtab_to_filename_for_display (loc->symtab));
112e8700 5870 uiout->text (":");
05cba821 5871
112e8700
SM
5872 if (uiout->is_mi_like_p ())
5873 uiout->field_string ("fullname", symtab_to_fullname (loc->symtab));
0d381245 5874
112e8700 5875 uiout->field_int ("line", loc->line_number);
0d381245 5876 }
859825b8 5877 else if (loc)
0d381245 5878 {
d7e74731 5879 string_file stb;
170b53b2 5880
d7e74731 5881 print_address_symbolic (loc->gdbarch, loc->address, &stb,
22e722e1 5882 demangle, "");
112e8700 5883 uiout->field_stream ("at", stb);
0d381245 5884 }
859825b8 5885 else
f00aae0f 5886 {
d28cd78a
TT
5887 uiout->field_string ("pending",
5888 event_location_to_string (b->location.get ()));
f00aae0f
KS
5889 /* If extra_string is available, it could be holding a condition
5890 or dprintf arguments. In either case, make sure it is printed,
5891 too, but only for non-MI streams. */
112e8700 5892 if (!uiout->is_mi_like_p () && b->extra_string != NULL)
f00aae0f
KS
5893 {
5894 if (b->type == bp_dprintf)
112e8700 5895 uiout->text (",");
f00aae0f 5896 else
112e8700
SM
5897 uiout->text (" ");
5898 uiout->text (b->extra_string);
f00aae0f
KS
5899 }
5900 }
6c95b8df 5901
b775012e
LM
5902 if (loc && is_breakpoint (b)
5903 && breakpoint_condition_evaluation_mode () == condition_evaluation_target
5904 && bp_condition_evaluator (b) == condition_evaluation_both)
5905 {
112e8700
SM
5906 uiout->text (" (");
5907 uiout->field_string ("evaluated-by",
b775012e 5908 bp_location_condition_evaluator (loc));
112e8700 5909 uiout->text (")");
b775012e 5910 }
0d381245
VP
5911}
5912
269b11a2
PA
5913static const char *
5914bptype_string (enum bptype type)
c906108c 5915{
c4093a6a
JM
5916 struct ep_type_description
5917 {
5918 enum bptype type;
a121b7c1 5919 const char *description;
c4093a6a
JM
5920 };
5921 static struct ep_type_description bptypes[] =
c906108c 5922 {
c5aa993b
JM
5923 {bp_none, "?deleted?"},
5924 {bp_breakpoint, "breakpoint"},
c906108c 5925 {bp_hardware_breakpoint, "hw breakpoint"},
7c16b83e 5926 {bp_single_step, "sw single-step"},
c5aa993b
JM
5927 {bp_until, "until"},
5928 {bp_finish, "finish"},
5929 {bp_watchpoint, "watchpoint"},
c906108c 5930 {bp_hardware_watchpoint, "hw watchpoint"},
c5aa993b
JM
5931 {bp_read_watchpoint, "read watchpoint"},
5932 {bp_access_watchpoint, "acc watchpoint"},
5933 {bp_longjmp, "longjmp"},
5934 {bp_longjmp_resume, "longjmp resume"},
e2e4d78b 5935 {bp_longjmp_call_dummy, "longjmp for call dummy"},
186c406b
TT
5936 {bp_exception, "exception"},
5937 {bp_exception_resume, "exception resume"},
c5aa993b 5938 {bp_step_resume, "step resume"},
2c03e5be 5939 {bp_hp_step_resume, "high-priority step resume"},
c5aa993b
JM
5940 {bp_watchpoint_scope, "watchpoint scope"},
5941 {bp_call_dummy, "call dummy"},
aa7d318d 5942 {bp_std_terminate, "std::terminate"},
c5aa993b 5943 {bp_shlib_event, "shlib events"},
c4093a6a 5944 {bp_thread_event, "thread events"},
1900040c 5945 {bp_overlay_event, "overlay events"},
0fd8e87f 5946 {bp_longjmp_master, "longjmp master"},
aa7d318d 5947 {bp_std_terminate_master, "std::terminate master"},
186c406b 5948 {bp_exception_master, "exception master"},
ce78b96d 5949 {bp_catchpoint, "catchpoint"},
1042e4c0 5950 {bp_tracepoint, "tracepoint"},
7a697b8d 5951 {bp_fast_tracepoint, "fast tracepoint"},
0fb4aa4b 5952 {bp_static_tracepoint, "static tracepoint"},
e7e0cddf 5953 {bp_dprintf, "dprintf"},
4efc6507 5954 {bp_jit_event, "jit events"},
0e30163f
JK
5955 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
5956 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
c5aa993b 5957 };
269b11a2
PA
5958
5959 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
5960 || ((int) type != bptypes[(int) type].type))
5961 internal_error (__FILE__, __LINE__,
5962 _("bptypes table does not describe type #%d."),
5963 (int) type);
5964
5965 return bptypes[(int) type].description;
5966}
5967
998580f1
MK
5968/* For MI, output a field named 'thread-groups' with a list as the value.
5969 For CLI, prefix the list with the string 'inf'. */
5970
5971static void
5972output_thread_groups (struct ui_out *uiout,
5973 const char *field_name,
5c632425 5974 const std::vector<int> &inf_nums,
998580f1
MK
5975 int mi_only)
5976{
112e8700 5977 int is_mi = uiout->is_mi_like_p ();
998580f1
MK
5978
5979 /* For backward compatibility, don't display inferiors in CLI unless
5980 there are several. Always display them for MI. */
5981 if (!is_mi && mi_only)
5982 return;
5983
10f489e5 5984 ui_out_emit_list list_emitter (uiout, field_name);
752eb8b4 5985
5c632425 5986 for (size_t i = 0; i < inf_nums.size (); i++)
998580f1
MK
5987 {
5988 if (is_mi)
5989 {
5990 char mi_group[10];
5991
5c632425 5992 xsnprintf (mi_group, sizeof (mi_group), "i%d", inf_nums[i]);
112e8700 5993 uiout->field_string (NULL, mi_group);
998580f1
MK
5994 }
5995 else
5996 {
5997 if (i == 0)
112e8700 5998 uiout->text (" inf ");
998580f1 5999 else
112e8700 6000 uiout->text (", ");
998580f1 6001
5c632425 6002 uiout->text (plongest (inf_nums[i]));
998580f1
MK
6003 }
6004 }
998580f1
MK
6005}
6006
269b11a2
PA
6007/* Print B to gdb_stdout. */
6008
6009static void
6010print_one_breakpoint_location (struct breakpoint *b,
6011 struct bp_location *loc,
6012 int loc_number,
6013 struct bp_location **last_loc,
269b11a2
PA
6014 int allflag)
6015{
6016 struct command_line *l;
c2c6d25f 6017 static char bpenables[] = "nynny";
c906108c 6018
79a45e25 6019 struct ui_out *uiout = current_uiout;
0d381245
VP
6020 int header_of_multiple = 0;
6021 int part_of_multiple = (loc != NULL);
79a45b7d
TT
6022 struct value_print_options opts;
6023
6024 get_user_print_options (&opts);
0d381245
VP
6025
6026 gdb_assert (!loc || loc_number != 0);
4a64f543
MS
6027 /* See comment in print_one_breakpoint concerning treatment of
6028 breakpoints with single disabled location. */
0d381245
VP
6029 if (loc == NULL
6030 && (b->loc != NULL
6031 && (b->loc->next != NULL || !b->loc->enabled)))
6032 header_of_multiple = 1;
6033 if (loc == NULL)
6034 loc = b->loc;
6035
c4093a6a
JM
6036 annotate_record ();
6037
6038 /* 1 */
6039 annotate_field (0);
0d381245
VP
6040 if (part_of_multiple)
6041 {
6042 char *formatted;
0c6773c1 6043 formatted = xstrprintf ("%d.%d", b->number, loc_number);
112e8700 6044 uiout->field_string ("number", formatted);
0d381245
VP
6045 xfree (formatted);
6046 }
6047 else
6048 {
112e8700 6049 uiout->field_int ("number", b->number);
0d381245 6050 }
c4093a6a
JM
6051
6052 /* 2 */
6053 annotate_field (1);
0d381245 6054 if (part_of_multiple)
112e8700 6055 uiout->field_skip ("type");
269b11a2 6056 else
112e8700 6057 uiout->field_string ("type", bptype_string (b->type));
c4093a6a
JM
6058
6059 /* 3 */
6060 annotate_field (2);
0d381245 6061 if (part_of_multiple)
112e8700 6062 uiout->field_skip ("disp");
0d381245 6063 else
112e8700 6064 uiout->field_string ("disp", bpdisp_text (b->disposition));
0d381245 6065
c4093a6a
JM
6066
6067 /* 4 */
6068 annotate_field (3);
0d381245 6069 if (part_of_multiple)
112e8700 6070 uiout->field_string ("enabled", loc->enabled ? "y" : "n");
0d381245 6071 else
112e8700
SM
6072 uiout->field_fmt ("enabled", "%c", bpenables[(int) b->enable_state]);
6073 uiout->spaces (2);
0d381245 6074
c4093a6a
JM
6075
6076 /* 5 and 6 */
3086aeae 6077 if (b->ops != NULL && b->ops->print_one != NULL)
0d381245 6078 {
4a64f543
MS
6079 /* Although the print_one can possibly print all locations,
6080 calling it here is not likely to get any nice result. So,
6081 make sure there's just one location. */
0d381245 6082 gdb_assert (b->loc == NULL || b->loc->next == NULL);
a6d9a66e 6083 b->ops->print_one (b, last_loc);
0d381245 6084 }
3086aeae
DJ
6085 else
6086 switch (b->type)
6087 {
6088 case bp_none:
6089 internal_error (__FILE__, __LINE__,
e2e0b3e5 6090 _("print_one_breakpoint: bp_none encountered\n"));
3086aeae 6091 break;
c906108c 6092
3086aeae
DJ
6093 case bp_watchpoint:
6094 case bp_hardware_watchpoint:
6095 case bp_read_watchpoint:
6096 case bp_access_watchpoint:
3a5c3e22
PA
6097 {
6098 struct watchpoint *w = (struct watchpoint *) b;
6099
6100 /* Field 4, the address, is omitted (which makes the columns
6101 not line up too nicely with the headers, but the effect
6102 is relatively readable). */
6103 if (opts.addressprint)
112e8700 6104 uiout->field_skip ("addr");
3a5c3e22 6105 annotate_field (5);
112e8700 6106 uiout->field_string ("what", w->exp_string);
3a5c3e22 6107 }
3086aeae
DJ
6108 break;
6109
3086aeae
DJ
6110 case bp_breakpoint:
6111 case bp_hardware_breakpoint:
7c16b83e 6112 case bp_single_step:
3086aeae
DJ
6113 case bp_until:
6114 case bp_finish:
6115 case bp_longjmp:
6116 case bp_longjmp_resume:
e2e4d78b 6117 case bp_longjmp_call_dummy:
186c406b
TT
6118 case bp_exception:
6119 case bp_exception_resume:
3086aeae 6120 case bp_step_resume:
2c03e5be 6121 case bp_hp_step_resume:
3086aeae
DJ
6122 case bp_watchpoint_scope:
6123 case bp_call_dummy:
aa7d318d 6124 case bp_std_terminate:
3086aeae
DJ
6125 case bp_shlib_event:
6126 case bp_thread_event:
6127 case bp_overlay_event:
0fd8e87f 6128 case bp_longjmp_master:
aa7d318d 6129 case bp_std_terminate_master:
186c406b 6130 case bp_exception_master:
1042e4c0 6131 case bp_tracepoint:
7a697b8d 6132 case bp_fast_tracepoint:
0fb4aa4b 6133 case bp_static_tracepoint:
e7e0cddf 6134 case bp_dprintf:
4efc6507 6135 case bp_jit_event:
0e30163f
JK
6136 case bp_gnu_ifunc_resolver:
6137 case bp_gnu_ifunc_resolver_return:
79a45b7d 6138 if (opts.addressprint)
3086aeae
DJ
6139 {
6140 annotate_field (4);
54e52265 6141 if (header_of_multiple)
112e8700 6142 uiout->field_string ("addr", "<MULTIPLE>");
e9bbd7c5 6143 else if (b->loc == NULL || loc->shlib_disabled)
112e8700 6144 uiout->field_string ("addr", "<PENDING>");
0101ce28 6145 else
112e8700 6146 uiout->field_core_addr ("addr",
5af949e3 6147 loc->gdbarch, loc->address);
3086aeae
DJ
6148 }
6149 annotate_field (5);
0d381245 6150 if (!header_of_multiple)
170b53b2 6151 print_breakpoint_location (b, loc);
0d381245 6152 if (b->loc)
a6d9a66e 6153 *last_loc = b->loc;
3086aeae
DJ
6154 break;
6155 }
c906108c 6156
6c95b8df 6157
998580f1 6158 if (loc != NULL && !header_of_multiple)
6c95b8df
PA
6159 {
6160 struct inferior *inf;
5c632425 6161 std::vector<int> inf_nums;
998580f1 6162 int mi_only = 1;
6c95b8df 6163
998580f1 6164 ALL_INFERIORS (inf)
6c95b8df
PA
6165 {
6166 if (inf->pspace == loc->pspace)
5c632425 6167 inf_nums.push_back (inf->num);
6c95b8df 6168 }
998580f1
MK
6169
6170 /* For backward compatibility, don't display inferiors in CLI unless
6171 there are several. Always display for MI. */
6172 if (allflag
6173 || (!gdbarch_has_global_breakpoints (target_gdbarch ())
6174 && (number_of_program_spaces () > 1
6175 || number_of_inferiors () > 1)
6176 /* LOC is for existing B, it cannot be in
6177 moribund_locations and thus having NULL OWNER. */
6178 && loc->owner->type != bp_catchpoint))
6179 mi_only = 0;
5c632425 6180 output_thread_groups (uiout, "thread-groups", inf_nums, mi_only);
6c95b8df
PA
6181 }
6182
4a306c9a 6183 if (!part_of_multiple)
c4093a6a 6184 {
4a306c9a
JB
6185 if (b->thread != -1)
6186 {
6187 /* FIXME: This seems to be redundant and lost here; see the
4a64f543 6188 "stop only in" line a little further down. */
112e8700
SM
6189 uiout->text (" thread ");
6190 uiout->field_int ("thread", b->thread);
4a306c9a
JB
6191 }
6192 else if (b->task != 0)
6193 {
112e8700
SM
6194 uiout->text (" task ");
6195 uiout->field_int ("task", b->task);
4a306c9a 6196 }
c4093a6a 6197 }
f1310107 6198
112e8700 6199 uiout->text ("\n");
f1310107 6200
348d480f 6201 if (!part_of_multiple)
f1310107
TJB
6202 b->ops->print_one_detail (b, uiout);
6203
0d381245 6204 if (part_of_multiple && frame_id_p (b->frame_id))
c4093a6a
JM
6205 {
6206 annotate_field (6);
112e8700 6207 uiout->text ("\tstop only in stack frame at ");
e5dd4106 6208 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
818dd999 6209 the frame ID. */
112e8700 6210 uiout->field_core_addr ("frame",
5af949e3 6211 b->gdbarch, b->frame_id.stack_addr);
112e8700 6212 uiout->text ("\n");
c4093a6a
JM
6213 }
6214
28010a5d 6215 if (!part_of_multiple && b->cond_string)
c4093a6a
JM
6216 {
6217 annotate_field (7);
d77f58be 6218 if (is_tracepoint (b))
112e8700 6219 uiout->text ("\ttrace only if ");
1042e4c0 6220 else
112e8700
SM
6221 uiout->text ("\tstop only if ");
6222 uiout->field_string ("cond", b->cond_string);
b775012e
LM
6223
6224 /* Print whether the target is doing the breakpoint's condition
6225 evaluation. If GDB is doing the evaluation, don't print anything. */
6226 if (is_breakpoint (b)
6227 && breakpoint_condition_evaluation_mode ()
6228 == condition_evaluation_target)
6229 {
112e8700
SM
6230 uiout->text (" (");
6231 uiout->field_string ("evaluated-by",
b775012e 6232 bp_condition_evaluator (b));
112e8700 6233 uiout->text (" evals)");
b775012e 6234 }
112e8700 6235 uiout->text ("\n");
0101ce28
JJ
6236 }
6237
0d381245 6238 if (!part_of_multiple && b->thread != -1)
c4093a6a 6239 {
4a64f543 6240 /* FIXME should make an annotation for this. */
112e8700
SM
6241 uiout->text ("\tstop only in thread ");
6242 if (uiout->is_mi_like_p ())
6243 uiout->field_int ("thread", b->thread);
5d5658a1
PA
6244 else
6245 {
6246 struct thread_info *thr = find_thread_global_id (b->thread);
6247
112e8700 6248 uiout->field_string ("thread", print_thread_id (thr));
5d5658a1 6249 }
112e8700 6250 uiout->text ("\n");
c4093a6a
JM
6251 }
6252
556ec64d
YQ
6253 if (!part_of_multiple)
6254 {
6255 if (b->hit_count)
31f56a27
YQ
6256 {
6257 /* FIXME should make an annotation for this. */
6258 if (is_catchpoint (b))
112e8700 6259 uiout->text ("\tcatchpoint");
31f56a27 6260 else if (is_tracepoint (b))
112e8700 6261 uiout->text ("\ttracepoint");
31f56a27 6262 else
112e8700
SM
6263 uiout->text ("\tbreakpoint");
6264 uiout->text (" already hit ");
6265 uiout->field_int ("times", b->hit_count);
31f56a27 6266 if (b->hit_count == 1)
112e8700 6267 uiout->text (" time\n");
31f56a27 6268 else
112e8700 6269 uiout->text (" times\n");
31f56a27 6270 }
556ec64d
YQ
6271 else
6272 {
31f56a27 6273 /* Output the count also if it is zero, but only if this is mi. */
112e8700
SM
6274 if (uiout->is_mi_like_p ())
6275 uiout->field_int ("times", b->hit_count);
556ec64d
YQ
6276 }
6277 }
8b93c638 6278
0d381245 6279 if (!part_of_multiple && b->ignore_count)
c4093a6a
JM
6280 {
6281 annotate_field (8);
112e8700
SM
6282 uiout->text ("\tignore next ");
6283 uiout->field_int ("ignore", b->ignore_count);
6284 uiout->text (" hits\n");
c4093a6a 6285 }
059fb39f 6286
816338b5
SS
6287 /* Note that an enable count of 1 corresponds to "enable once"
6288 behavior, which is reported by the combination of enablement and
6289 disposition, so we don't need to mention it here. */
6290 if (!part_of_multiple && b->enable_count > 1)
6291 {
6292 annotate_field (8);
112e8700 6293 uiout->text ("\tdisable after ");
816338b5
SS
6294 /* Tweak the wording to clarify that ignore and enable counts
6295 are distinct, and have additive effect. */
6296 if (b->ignore_count)
112e8700 6297 uiout->text ("additional ");
816338b5 6298 else
112e8700
SM
6299 uiout->text ("next ");
6300 uiout->field_int ("enable", b->enable_count);
6301 uiout->text (" hits\n");
816338b5
SS
6302 }
6303
f196051f
SS
6304 if (!part_of_multiple && is_tracepoint (b))
6305 {
6306 struct tracepoint *tp = (struct tracepoint *) b;
6307
6308 if (tp->traceframe_usage)
6309 {
112e8700
SM
6310 uiout->text ("\ttrace buffer usage ");
6311 uiout->field_int ("traceframe-usage", tp->traceframe_usage);
6312 uiout->text (" bytes\n");
f196051f
SS
6313 }
6314 }
d3ce09f5 6315
d1b0a7bf 6316 l = b->commands ? b->commands.get () : NULL;
059fb39f 6317 if (!part_of_multiple && l)
c4093a6a
JM
6318 {
6319 annotate_field (9);
2e783024 6320 ui_out_emit_tuple tuple_emitter (uiout, "script");
8b93c638 6321 print_command_lines (uiout, l, 4);
c4093a6a 6322 }
d24317b4 6323
d9b3f62e 6324 if (is_tracepoint (b))
1042e4c0 6325 {
d9b3f62e
PA
6326 struct tracepoint *t = (struct tracepoint *) b;
6327
6328 if (!part_of_multiple && t->pass_count)
6329 {
6330 annotate_field (10);
112e8700
SM
6331 uiout->text ("\tpass count ");
6332 uiout->field_int ("pass", t->pass_count);
6333 uiout->text (" \n");
d9b3f62e 6334 }
f2a8bc8a
YQ
6335
6336 /* Don't display it when tracepoint or tracepoint location is
6337 pending. */
6338 if (!header_of_multiple && loc != NULL && !loc->shlib_disabled)
6339 {
6340 annotate_field (11);
6341
112e8700
SM
6342 if (uiout->is_mi_like_p ())
6343 uiout->field_string ("installed",
f2a8bc8a
YQ
6344 loc->inserted ? "y" : "n");
6345 else
6346 {
6347 if (loc->inserted)
112e8700 6348 uiout->text ("\t");
f2a8bc8a 6349 else
112e8700
SM
6350 uiout->text ("\tnot ");
6351 uiout->text ("installed on target\n");
f2a8bc8a
YQ
6352 }
6353 }
1042e4c0
SS
6354 }
6355
112e8700 6356 if (uiout->is_mi_like_p () && !part_of_multiple)
d24317b4 6357 {
3a5c3e22
PA
6358 if (is_watchpoint (b))
6359 {
6360 struct watchpoint *w = (struct watchpoint *) b;
6361
112e8700 6362 uiout->field_string ("original-location", w->exp_string);
3a5c3e22 6363 }
f00aae0f 6364 else if (b->location != NULL
d28cd78a 6365 && event_location_to_string (b->location.get ()) != NULL)
112e8700 6366 uiout->field_string ("original-location",
d28cd78a 6367 event_location_to_string (b->location.get ()));
d24317b4 6368 }
c4093a6a 6369}
c5aa993b 6370
0d381245
VP
6371static void
6372print_one_breakpoint (struct breakpoint *b,
4a64f543 6373 struct bp_location **last_loc,
6c95b8df 6374 int allflag)
0d381245 6375{
79a45e25 6376 struct ui_out *uiout = current_uiout;
8d3788bd 6377
2e783024
TT
6378 {
6379 ui_out_emit_tuple tuple_emitter (uiout, "bkpt");
8d3788bd 6380
2e783024
TT
6381 print_one_breakpoint_location (b, NULL, 0, last_loc, allflag);
6382 }
0d381245
VP
6383
6384 /* If this breakpoint has custom print function,
6385 it's already printed. Otherwise, print individual
6386 locations, if any. */
6387 if (b->ops == NULL || b->ops->print_one == NULL)
6388 {
4a64f543
MS
6389 /* If breakpoint has a single location that is disabled, we
6390 print it as if it had several locations, since otherwise it's
6391 hard to represent "breakpoint enabled, location disabled"
6392 situation.
6393
6394 Note that while hardware watchpoints have several locations
a3be7890 6395 internally, that's not a property exposed to user. */
0d381245 6396 if (b->loc
a5606eee 6397 && !is_hardware_watchpoint (b)
8d3788bd 6398 && (b->loc->next || !b->loc->enabled))
0d381245
VP
6399 {
6400 struct bp_location *loc;
6401 int n = 1;
8d3788bd 6402
0d381245 6403 for (loc = b->loc; loc; loc = loc->next, ++n)
8d3788bd 6404 {
2e783024 6405 ui_out_emit_tuple tuple_emitter (uiout, NULL);
8d3788bd 6406 print_one_breakpoint_location (b, loc, n, last_loc, allflag);
8d3788bd 6407 }
0d381245
VP
6408 }
6409 }
6410}
6411
a6d9a66e
UW
6412static int
6413breakpoint_address_bits (struct breakpoint *b)
6414{
6415 int print_address_bits = 0;
6416 struct bp_location *loc;
6417
c6d81124
PA
6418 /* Software watchpoints that aren't watching memory don't have an
6419 address to print. */
6420 if (is_no_memory_software_watchpoint (b))
6421 return 0;
6422
a6d9a66e
UW
6423 for (loc = b->loc; loc; loc = loc->next)
6424 {
c7437ca6
PA
6425 int addr_bit;
6426
c7437ca6 6427 addr_bit = gdbarch_addr_bit (loc->gdbarch);
a6d9a66e
UW
6428 if (addr_bit > print_address_bits)
6429 print_address_bits = addr_bit;
6430 }
6431
6432 return print_address_bits;
6433}
0d381245 6434
65630365 6435/* See breakpoint.h. */
c5aa993b 6436
65630365
PA
6437void
6438print_breakpoint (breakpoint *b)
c4093a6a 6439{
a6d9a66e 6440 struct bp_location *dummy_loc = NULL;
65630365 6441 print_one_breakpoint (b, &dummy_loc, 0);
c4093a6a 6442}
c5aa993b 6443
09d682a4
TT
6444/* Return true if this breakpoint was set by the user, false if it is
6445 internal or momentary. */
6446
6447int
6448user_breakpoint_p (struct breakpoint *b)
6449{
46c6471b 6450 return b->number > 0;
09d682a4
TT
6451}
6452
93daf339
TT
6453/* See breakpoint.h. */
6454
6455int
6456pending_breakpoint_p (struct breakpoint *b)
6457{
6458 return b->loc == NULL;
6459}
6460
7f3b0473 6461/* Print information on user settable breakpoint (watchpoint, etc)
d77f58be
SS
6462 number BNUM. If BNUM is -1 print all user-settable breakpoints.
6463 If ALLFLAG is non-zero, include non-user-settable breakpoints. If
6464 FILTER is non-NULL, call it on each breakpoint and only include the
6465 ones for which it returns non-zero. Return the total number of
6466 breakpoints listed. */
c906108c 6467
d77f58be 6468static int
4495129a 6469breakpoint_1 (const char *args, int allflag,
4a64f543 6470 int (*filter) (const struct breakpoint *))
c4093a6a 6471{
52f0bd74 6472 struct breakpoint *b;
a6d9a66e 6473 struct bp_location *last_loc = NULL;
7f3b0473 6474 int nr_printable_breakpoints;
79a45b7d 6475 struct value_print_options opts;
a6d9a66e 6476 int print_address_bits = 0;
269b11a2 6477 int print_type_col_width = 14;
79a45e25 6478 struct ui_out *uiout = current_uiout;
269b11a2 6479
79a45b7d
TT
6480 get_user_print_options (&opts);
6481
4a64f543
MS
6482 /* Compute the number of rows in the table, as well as the size
6483 required for address fields. */
7f3b0473
AC
6484 nr_printable_breakpoints = 0;
6485 ALL_BREAKPOINTS (b)
e5a67952
MS
6486 {
6487 /* If we have a filter, only list the breakpoints it accepts. */
6488 if (filter && !filter (b))
6489 continue;
6490
6491 /* If we have an "args" string, it is a list of breakpoints to
6492 accept. Skip the others. */
6493 if (args != NULL && *args != '\0')
6494 {
6495 if (allflag && parse_and_eval_long (args) != b->number)
6496 continue;
6497 if (!allflag && !number_is_in_list (args, b->number))
6498 continue;
6499 }
269b11a2 6500
e5a67952
MS
6501 if (allflag || user_breakpoint_p (b))
6502 {
6503 int addr_bit, type_len;
a6d9a66e 6504
e5a67952
MS
6505 addr_bit = breakpoint_address_bits (b);
6506 if (addr_bit > print_address_bits)
6507 print_address_bits = addr_bit;
269b11a2 6508
e5a67952
MS
6509 type_len = strlen (bptype_string (b->type));
6510 if (type_len > print_type_col_width)
6511 print_type_col_width = type_len;
6512
6513 nr_printable_breakpoints++;
6514 }
6515 }
7f3b0473 6516
4a2b031d
TT
6517 {
6518 ui_out_emit_table table_emitter (uiout,
6519 opts.addressprint ? 6 : 5,
6520 nr_printable_breakpoints,
6521 "BreakpointTable");
6522
6523 if (nr_printable_breakpoints > 0)
6524 annotate_breakpoints_headers ();
6525 if (nr_printable_breakpoints > 0)
6526 annotate_field (0);
6527 uiout->table_header (7, ui_left, "number", "Num"); /* 1 */
6528 if (nr_printable_breakpoints > 0)
6529 annotate_field (1);
6530 uiout->table_header (print_type_col_width, ui_left, "type", "Type"); /* 2 */
6531 if (nr_printable_breakpoints > 0)
6532 annotate_field (2);
6533 uiout->table_header (4, ui_left, "disp", "Disp"); /* 3 */
6534 if (nr_printable_breakpoints > 0)
6535 annotate_field (3);
6536 uiout->table_header (3, ui_left, "enabled", "Enb"); /* 4 */
6537 if (opts.addressprint)
6538 {
6539 if (nr_printable_breakpoints > 0)
6540 annotate_field (4);
6541 if (print_address_bits <= 32)
6542 uiout->table_header (10, ui_left, "addr", "Address"); /* 5 */
6543 else
6544 uiout->table_header (18, ui_left, "addr", "Address"); /* 5 */
6545 }
6546 if (nr_printable_breakpoints > 0)
6547 annotate_field (5);
6548 uiout->table_header (40, ui_noalign, "what", "What"); /* 6 */
6549 uiout->table_body ();
6550 if (nr_printable_breakpoints > 0)
6551 annotate_breakpoints_table ();
6552
6553 ALL_BREAKPOINTS (b)
6554 {
6555 QUIT;
6556 /* If we have a filter, only list the breakpoints it accepts. */
6557 if (filter && !filter (b))
6558 continue;
e5a67952 6559
4a2b031d
TT
6560 /* If we have an "args" string, it is a list of breakpoints to
6561 accept. Skip the others. */
e5a67952 6562
4a2b031d
TT
6563 if (args != NULL && *args != '\0')
6564 {
6565 if (allflag) /* maintenance info breakpoint */
6566 {
6567 if (parse_and_eval_long (args) != b->number)
6568 continue;
6569 }
6570 else /* all others */
6571 {
6572 if (!number_is_in_list (args, b->number))
6573 continue;
6574 }
6575 }
6576 /* We only print out user settable breakpoints unless the
6577 allflag is set. */
6578 if (allflag || user_breakpoint_p (b))
6579 print_one_breakpoint (b, &last_loc, allflag);
6580 }
6581 }
698384cd 6582
7f3b0473 6583 if (nr_printable_breakpoints == 0)
c906108c 6584 {
4a64f543
MS
6585 /* If there's a filter, let the caller decide how to report
6586 empty list. */
d77f58be
SS
6587 if (!filter)
6588 {
e5a67952 6589 if (args == NULL || *args == '\0')
112e8700 6590 uiout->message ("No breakpoints or watchpoints.\n");
d77f58be 6591 else
112e8700 6592 uiout->message ("No breakpoint or watchpoint matching '%s'.\n",
e5a67952 6593 args);
d77f58be 6594 }
c906108c
SS
6595 }
6596 else
c4093a6a 6597 {
a6d9a66e
UW
6598 if (last_loc && !server_command)
6599 set_next_address (last_loc->gdbarch, last_loc->address);
c4093a6a 6600 }
c906108c 6601
4a64f543 6602 /* FIXME? Should this be moved up so that it is only called when
c4093a6a 6603 there have been breakpoints? */
c906108c 6604 annotate_breakpoints_table_end ();
d77f58be
SS
6605
6606 return nr_printable_breakpoints;
c906108c
SS
6607}
6608
ad443146
SS
6609/* Display the value of default-collect in a way that is generally
6610 compatible with the breakpoint list. */
6611
6612static void
6613default_collect_info (void)
6614{
79a45e25
PA
6615 struct ui_out *uiout = current_uiout;
6616
ad443146
SS
6617 /* If it has no value (which is frequently the case), say nothing; a
6618 message like "No default-collect." gets in user's face when it's
6619 not wanted. */
6620 if (!*default_collect)
6621 return;
6622
6623 /* The following phrase lines up nicely with per-tracepoint collect
6624 actions. */
112e8700
SM
6625 uiout->text ("default collect ");
6626 uiout->field_string ("default-collect", default_collect);
6627 uiout->text (" \n");
ad443146
SS
6628}
6629
c906108c 6630static void
0b39b52e 6631info_breakpoints_command (const char *args, int from_tty)
c906108c 6632{
e5a67952 6633 breakpoint_1 (args, 0, NULL);
ad443146
SS
6634
6635 default_collect_info ();
d77f58be
SS
6636}
6637
6638static void
1d12d88f 6639info_watchpoints_command (const char *args, int from_tty)
d77f58be 6640{
e5a67952 6641 int num_printed = breakpoint_1 (args, 0, is_watchpoint);
79a45e25 6642 struct ui_out *uiout = current_uiout;
d77f58be
SS
6643
6644 if (num_printed == 0)
6645 {
e5a67952 6646 if (args == NULL || *args == '\0')
112e8700 6647 uiout->message ("No watchpoints.\n");
d77f58be 6648 else
112e8700 6649 uiout->message ("No watchpoint matching '%s'.\n", args);
d77f58be 6650 }
c906108c
SS
6651}
6652
7a292a7a 6653static void
4495129a 6654maintenance_info_breakpoints (const char *args, int from_tty)
c906108c 6655{
e5a67952 6656 breakpoint_1 (args, 1, NULL);
ad443146
SS
6657
6658 default_collect_info ();
c906108c
SS
6659}
6660
0d381245 6661static int
714835d5 6662breakpoint_has_pc (struct breakpoint *b,
6c95b8df 6663 struct program_space *pspace,
714835d5 6664 CORE_ADDR pc, struct obj_section *section)
0d381245
VP
6665{
6666 struct bp_location *bl = b->loc;
cc59ec59 6667
0d381245
VP
6668 for (; bl; bl = bl->next)
6669 {
6c95b8df
PA
6670 if (bl->pspace == pspace
6671 && bl->address == pc
0d381245
VP
6672 && (!overlay_debugging || bl->section == section))
6673 return 1;
6674 }
6675 return 0;
6676}
6677
672f9b60 6678/* Print a message describing any user-breakpoints set at PC. This
6c95b8df
PA
6679 concerns with logical breakpoints, so we match program spaces, not
6680 address spaces. */
c906108c
SS
6681
6682static void
6c95b8df
PA
6683describe_other_breakpoints (struct gdbarch *gdbarch,
6684 struct program_space *pspace, CORE_ADDR pc,
5af949e3 6685 struct obj_section *section, int thread)
c906108c 6686{
52f0bd74
AC
6687 int others = 0;
6688 struct breakpoint *b;
c906108c
SS
6689
6690 ALL_BREAKPOINTS (b)
672f9b60
KP
6691 others += (user_breakpoint_p (b)
6692 && breakpoint_has_pc (b, pspace, pc, section));
c906108c
SS
6693 if (others > 0)
6694 {
a3f17187
AC
6695 if (others == 1)
6696 printf_filtered (_("Note: breakpoint "));
6697 else /* if (others == ???) */
6698 printf_filtered (_("Note: breakpoints "));
c906108c 6699 ALL_BREAKPOINTS (b)
672f9b60 6700 if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
0d381245
VP
6701 {
6702 others--;
6703 printf_filtered ("%d", b->number);
6704 if (b->thread == -1 && thread != -1)
6705 printf_filtered (" (all threads)");
6706 else if (b->thread != -1)
6707 printf_filtered (" (thread %d)", b->thread);
6708 printf_filtered ("%s%s ",
059fb39f 6709 ((b->enable_state == bp_disabled
f8eba3c6 6710 || b->enable_state == bp_call_disabled)
0d381245 6711 ? " (disabled)"
0d381245
VP
6712 : ""),
6713 (others > 1) ? ","
6714 : ((others == 1) ? " and" : ""));
6715 }
a3f17187 6716 printf_filtered (_("also set at pc "));
5af949e3 6717 fputs_filtered (paddress (gdbarch, pc), gdb_stdout);
c906108c
SS
6718 printf_filtered (".\n");
6719 }
6720}
6721\f
c906108c 6722
e4f237da 6723/* Return true iff it is meaningful to use the address member of
244558af
LM
6724 BPT locations. For some breakpoint types, the locations' address members
6725 are irrelevant and it makes no sense to attempt to compare them to other
6726 addresses (or use them for any other purpose either).
e4f237da 6727
4a64f543 6728 More specifically, each of the following breakpoint types will
244558af 6729 always have a zero valued location address and we don't want to mark
4a64f543 6730 breakpoints of any of these types to be a duplicate of an actual
244558af 6731 breakpoint location at address zero:
e4f237da
KB
6732
6733 bp_watchpoint
2d134ed3
PA
6734 bp_catchpoint
6735
6736*/
e4f237da
KB
6737
6738static int
6739breakpoint_address_is_meaningful (struct breakpoint *bpt)
6740{
6741 enum bptype type = bpt->type;
6742
2d134ed3
PA
6743 return (type != bp_watchpoint && type != bp_catchpoint);
6744}
6745
6746/* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
6747 true if LOC1 and LOC2 represent the same watchpoint location. */
6748
6749static int
4a64f543
MS
6750watchpoint_locations_match (struct bp_location *loc1,
6751 struct bp_location *loc2)
2d134ed3 6752{
3a5c3e22
PA
6753 struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
6754 struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
6755
6756 /* Both of them must exist. */
6757 gdb_assert (w1 != NULL);
6758 gdb_assert (w2 != NULL);
2bdf28a0 6759
4a64f543
MS
6760 /* If the target can evaluate the condition expression in hardware,
6761 then we we need to insert both watchpoints even if they are at
6762 the same place. Otherwise the watchpoint will only trigger when
6763 the condition of whichever watchpoint was inserted evaluates to
6764 true, not giving a chance for GDB to check the condition of the
6765 other watchpoint. */
3a5c3e22 6766 if ((w1->cond_exp
4a64f543
MS
6767 && target_can_accel_watchpoint_condition (loc1->address,
6768 loc1->length,
0cf6dd15 6769 loc1->watchpoint_type,
4d01a485 6770 w1->cond_exp.get ()))
3a5c3e22 6771 || (w2->cond_exp
4a64f543
MS
6772 && target_can_accel_watchpoint_condition (loc2->address,
6773 loc2->length,
0cf6dd15 6774 loc2->watchpoint_type,
4d01a485 6775 w2->cond_exp.get ())))
0cf6dd15
TJB
6776 return 0;
6777
85d721b8
PA
6778 /* Note that this checks the owner's type, not the location's. In
6779 case the target does not support read watchpoints, but does
6780 support access watchpoints, we'll have bp_read_watchpoint
6781 watchpoints with hw_access locations. Those should be considered
6782 duplicates of hw_read locations. The hw_read locations will
6783 become hw_access locations later. */
2d134ed3
PA
6784 return (loc1->owner->type == loc2->owner->type
6785 && loc1->pspace->aspace == loc2->pspace->aspace
6786 && loc1->address == loc2->address
6787 && loc1->length == loc2->length);
e4f237da
KB
6788}
6789
31e77af2 6790/* See breakpoint.h. */
6c95b8df 6791
31e77af2 6792int
accd0bcd
YQ
6793breakpoint_address_match (const address_space *aspace1, CORE_ADDR addr1,
6794 const address_space *aspace2, CORE_ADDR addr2)
6c95b8df 6795{
f5656ead 6796 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
6c95b8df
PA
6797 || aspace1 == aspace2)
6798 && addr1 == addr2);
6799}
6800
f1310107
TJB
6801/* Returns true if {ASPACE2,ADDR2} falls within the range determined by
6802 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
6803 matches ASPACE2. On targets that have global breakpoints, the address
6804 space doesn't really matter. */
6805
6806static int
accd0bcd
YQ
6807breakpoint_address_match_range (const address_space *aspace1,
6808 CORE_ADDR addr1,
6809 int len1, const address_space *aspace2,
f1310107
TJB
6810 CORE_ADDR addr2)
6811{
f5656ead 6812 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
f1310107
TJB
6813 || aspace1 == aspace2)
6814 && addr2 >= addr1 && addr2 < addr1 + len1);
6815}
6816
6817/* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
6818 a ranged breakpoint. In most targets, a match happens only if ASPACE
6819 matches the breakpoint's address space. On targets that have global
6820 breakpoints, the address space doesn't really matter. */
6821
6822static int
6823breakpoint_location_address_match (struct bp_location *bl,
accd0bcd 6824 const address_space *aspace,
f1310107
TJB
6825 CORE_ADDR addr)
6826{
6827 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
6828 aspace, addr)
6829 || (bl->length
6830 && breakpoint_address_match_range (bl->pspace->aspace,
6831 bl->address, bl->length,
6832 aspace, addr)));
6833}
6834
d35ae833
PA
6835/* Returns true if the [ADDR,ADDR+LEN) range in ASPACE overlaps
6836 breakpoint BL. BL may be a ranged breakpoint. In most targets, a
6837 match happens only if ASPACE matches the breakpoint's address
6838 space. On targets that have global breakpoints, the address space
6839 doesn't really matter. */
6840
6841static int
6842breakpoint_location_address_range_overlap (struct bp_location *bl,
accd0bcd 6843 const address_space *aspace,
d35ae833
PA
6844 CORE_ADDR addr, int len)
6845{
6846 if (gdbarch_has_global_breakpoints (target_gdbarch ())
6847 || bl->pspace->aspace == aspace)
6848 {
6849 int bl_len = bl->length != 0 ? bl->length : 1;
6850
6851 if (mem_ranges_overlap (addr, len, bl->address, bl_len))
6852 return 1;
6853 }
6854 return 0;
6855}
6856
1e4d1764
YQ
6857/* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
6858 Then, if LOC1 and LOC2 represent the same tracepoint location, returns
6859 true, otherwise returns false. */
6860
6861static int
6862tracepoint_locations_match (struct bp_location *loc1,
6863 struct bp_location *loc2)
6864{
6865 if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
6866 /* Since tracepoint locations are never duplicated with others', tracepoint
6867 locations at the same address of different tracepoints are regarded as
6868 different locations. */
6869 return (loc1->address == loc2->address && loc1->owner == loc2->owner);
6870 else
6871 return 0;
6872}
6873
2d134ed3
PA
6874/* Assuming LOC1 and LOC2's types' have meaningful target addresses
6875 (breakpoint_address_is_meaningful), returns true if LOC1 and LOC2
6876 represent the same location. */
6877
6878static int
4a64f543
MS
6879breakpoint_locations_match (struct bp_location *loc1,
6880 struct bp_location *loc2)
2d134ed3 6881{
2bdf28a0
JK
6882 int hw_point1, hw_point2;
6883
6884 /* Both of them must not be in moribund_locations. */
6885 gdb_assert (loc1->owner != NULL);
6886 gdb_assert (loc2->owner != NULL);
6887
6888 hw_point1 = is_hardware_watchpoint (loc1->owner);
6889 hw_point2 = is_hardware_watchpoint (loc2->owner);
2d134ed3
PA
6890
6891 if (hw_point1 != hw_point2)
6892 return 0;
6893 else if (hw_point1)
6894 return watchpoint_locations_match (loc1, loc2);
1e4d1764
YQ
6895 else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
6896 return tracepoint_locations_match (loc1, loc2);
2d134ed3 6897 else
f1310107
TJB
6898 /* We compare bp_location.length in order to cover ranged breakpoints. */
6899 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
6900 loc2->pspace->aspace, loc2->address)
6901 && loc1->length == loc2->length);
2d134ed3
PA
6902}
6903
76897487
KB
6904static void
6905breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
6906 int bnum, int have_bnum)
6907{
f63fbe86
MS
6908 /* The longest string possibly returned by hex_string_custom
6909 is 50 chars. These must be at least that big for safety. */
6910 char astr1[64];
6911 char astr2[64];
76897487 6912
bb599908
PH
6913 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
6914 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
76897487 6915 if (have_bnum)
8a3fe4f8 6916 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
76897487
KB
6917 bnum, astr1, astr2);
6918 else
8a3fe4f8 6919 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
76897487
KB
6920}
6921
4a64f543
MS
6922/* Adjust a breakpoint's address to account for architectural
6923 constraints on breakpoint placement. Return the adjusted address.
6924 Note: Very few targets require this kind of adjustment. For most
6925 targets, this function is simply the identity function. */
76897487
KB
6926
6927static CORE_ADDR
a6d9a66e
UW
6928adjust_breakpoint_address (struct gdbarch *gdbarch,
6929 CORE_ADDR bpaddr, enum bptype bptype)
76897487 6930{
a0de8c21
YQ
6931 if (bptype == bp_watchpoint
6932 || bptype == bp_hardware_watchpoint
6933 || bptype == bp_read_watchpoint
6934 || bptype == bp_access_watchpoint
6935 || bptype == bp_catchpoint)
88f7da05
KB
6936 {
6937 /* Watchpoints and the various bp_catch_* eventpoints should not
6938 have their addresses modified. */
6939 return bpaddr;
6940 }
7c16b83e
PA
6941 else if (bptype == bp_single_step)
6942 {
6943 /* Single-step breakpoints should not have their addresses
6944 modified. If there's any architectural constrain that
6945 applies to this address, then it should have already been
6946 taken into account when the breakpoint was created in the
6947 first place. If we didn't do this, stepping through e.g.,
6948 Thumb-2 IT blocks would break. */
6949 return bpaddr;
6950 }
76897487
KB
6951 else
6952 {
a0de8c21
YQ
6953 CORE_ADDR adjusted_bpaddr = bpaddr;
6954
6955 if (gdbarch_adjust_breakpoint_address_p (gdbarch))
6956 {
6957 /* Some targets have architectural constraints on the placement
6958 of breakpoint instructions. Obtain the adjusted address. */
6959 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
6960 }
76897487 6961
a0de8c21 6962 adjusted_bpaddr = address_significant (gdbarch, adjusted_bpaddr);
76897487
KB
6963
6964 /* An adjusted breakpoint address can significantly alter
6965 a user's expectations. Print a warning if an adjustment
6966 is required. */
6967 if (adjusted_bpaddr != bpaddr)
6968 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
6969
6970 return adjusted_bpaddr;
6971 }
6972}
6973
5625a286 6974bp_location::bp_location (const bp_location_ops *ops, breakpoint *owner)
7cc221ef 6975{
5625a286 6976 bp_location *loc = this;
7cc221ef 6977
348d480f
PA
6978 gdb_assert (ops != NULL);
6979
28010a5d
PA
6980 loc->ops = ops;
6981 loc->owner = owner;
b775012e 6982 loc->cond_bytecode = NULL;
0d381245
VP
6983 loc->shlib_disabled = 0;
6984 loc->enabled = 1;
e049a4b5 6985
28010a5d 6986 switch (owner->type)
e049a4b5
DJ
6987 {
6988 case bp_breakpoint:
7c16b83e 6989 case bp_single_step:
e049a4b5
DJ
6990 case bp_until:
6991 case bp_finish:
6992 case bp_longjmp:
6993 case bp_longjmp_resume:
e2e4d78b 6994 case bp_longjmp_call_dummy:
186c406b
TT
6995 case bp_exception:
6996 case bp_exception_resume:
e049a4b5 6997 case bp_step_resume:
2c03e5be 6998 case bp_hp_step_resume:
e049a4b5
DJ
6999 case bp_watchpoint_scope:
7000 case bp_call_dummy:
aa7d318d 7001 case bp_std_terminate:
e049a4b5
DJ
7002 case bp_shlib_event:
7003 case bp_thread_event:
7004 case bp_overlay_event:
4efc6507 7005 case bp_jit_event:
0fd8e87f 7006 case bp_longjmp_master:
aa7d318d 7007 case bp_std_terminate_master:
186c406b 7008 case bp_exception_master:
0e30163f
JK
7009 case bp_gnu_ifunc_resolver:
7010 case bp_gnu_ifunc_resolver_return:
e7e0cddf 7011 case bp_dprintf:
e049a4b5 7012 loc->loc_type = bp_loc_software_breakpoint;
b775012e 7013 mark_breakpoint_location_modified (loc);
e049a4b5
DJ
7014 break;
7015 case bp_hardware_breakpoint:
7016 loc->loc_type = bp_loc_hardware_breakpoint;
b775012e 7017 mark_breakpoint_location_modified (loc);
e049a4b5
DJ
7018 break;
7019 case bp_hardware_watchpoint:
7020 case bp_read_watchpoint:
7021 case bp_access_watchpoint:
7022 loc->loc_type = bp_loc_hardware_watchpoint;
7023 break;
7024 case bp_watchpoint:
ce78b96d 7025 case bp_catchpoint:
15c3d785
PA
7026 case bp_tracepoint:
7027 case bp_fast_tracepoint:
0fb4aa4b 7028 case bp_static_tracepoint:
e049a4b5
DJ
7029 loc->loc_type = bp_loc_other;
7030 break;
7031 default:
e2e0b3e5 7032 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
e049a4b5
DJ
7033 }
7034
f431efe5 7035 loc->refc = 1;
28010a5d
PA
7036}
7037
7038/* Allocate a struct bp_location. */
7039
7040static struct bp_location *
7041allocate_bp_location (struct breakpoint *bpt)
7042{
348d480f
PA
7043 return bpt->ops->allocate_location (bpt);
7044}
7cc221ef 7045
f431efe5
PA
7046static void
7047free_bp_location (struct bp_location *loc)
fe3f5fa8 7048{
348d480f 7049 loc->ops->dtor (loc);
4d01a485 7050 delete loc;
fe3f5fa8
VP
7051}
7052
f431efe5
PA
7053/* Increment reference count. */
7054
7055static void
7056incref_bp_location (struct bp_location *bl)
7057{
7058 ++bl->refc;
7059}
7060
7061/* Decrement reference count. If the reference count reaches 0,
7062 destroy the bp_location. Sets *BLP to NULL. */
7063
7064static void
7065decref_bp_location (struct bp_location **blp)
7066{
0807b50c
PA
7067 gdb_assert ((*blp)->refc > 0);
7068
f431efe5
PA
7069 if (--(*blp)->refc == 0)
7070 free_bp_location (*blp);
7071 *blp = NULL;
7072}
7073
346774a9 7074/* Add breakpoint B at the end of the global breakpoint chain. */
c906108c 7075
b270e6f9
TT
7076static breakpoint *
7077add_to_breakpoint_chain (std::unique_ptr<breakpoint> &&b)
c906108c 7078{
346774a9 7079 struct breakpoint *b1;
b270e6f9 7080 struct breakpoint *result = b.get ();
c906108c 7081
346774a9
PA
7082 /* Add this breakpoint to the end of the chain so that a list of
7083 breakpoints will come out in order of increasing numbers. */
7084
7085 b1 = breakpoint_chain;
7086 if (b1 == 0)
b270e6f9 7087 breakpoint_chain = b.release ();
346774a9
PA
7088 else
7089 {
7090 while (b1->next)
7091 b1 = b1->next;
b270e6f9 7092 b1->next = b.release ();
346774a9 7093 }
b270e6f9
TT
7094
7095 return result;
346774a9
PA
7096}
7097
7098/* Initializes breakpoint B with type BPTYPE and no locations yet. */
7099
7100static void
7101init_raw_breakpoint_without_location (struct breakpoint *b,
7102 struct gdbarch *gdbarch,
28010a5d 7103 enum bptype bptype,
c0a91b2b 7104 const struct breakpoint_ops *ops)
346774a9 7105{
348d480f
PA
7106 gdb_assert (ops != NULL);
7107
28010a5d 7108 b->ops = ops;
4d28f7a8 7109 b->type = bptype;
a6d9a66e 7110 b->gdbarch = gdbarch;
c906108c
SS
7111 b->language = current_language->la_language;
7112 b->input_radix = input_radix;
d0fb5eae 7113 b->related_breakpoint = b;
346774a9
PA
7114}
7115
7116/* Helper to set_raw_breakpoint below. Creates a breakpoint
7117 that has type BPTYPE and has no locations as yet. */
346774a9
PA
7118
7119static struct breakpoint *
7120set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
348d480f 7121 enum bptype bptype,
c0a91b2b 7122 const struct breakpoint_ops *ops)
346774a9 7123{
3b0871f4 7124 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (bptype);
346774a9 7125
3b0871f4 7126 init_raw_breakpoint_without_location (b.get (), gdbarch, bptype, ops);
b270e6f9 7127 return add_to_breakpoint_chain (std::move (b));
0d381245
VP
7128}
7129
0e30163f
JK
7130/* Initialize loc->function_name. EXPLICIT_LOC says no indirect function
7131 resolutions should be made as the user specified the location explicitly
7132 enough. */
7133
0d381245 7134static void
0e30163f 7135set_breakpoint_location_function (struct bp_location *loc, int explicit_loc)
0d381245 7136{
2bdf28a0
JK
7137 gdb_assert (loc->owner != NULL);
7138
0d381245 7139 if (loc->owner->type == bp_breakpoint
1042e4c0 7140 || loc->owner->type == bp_hardware_breakpoint
d77f58be 7141 || is_tracepoint (loc->owner))
0d381245 7142 {
2c02bd72 7143 const char *function_name;
0e30163f 7144
3467ec66 7145 if (loc->msymbol != NULL
f50776aa
PA
7146 && (MSYMBOL_TYPE (loc->msymbol) == mst_text_gnu_ifunc
7147 || MSYMBOL_TYPE (loc->msymbol) == mst_data_gnu_ifunc)
3467ec66 7148 && !explicit_loc)
0e30163f
JK
7149 {
7150 struct breakpoint *b = loc->owner;
7151
3467ec66
PA
7152 function_name = MSYMBOL_LINKAGE_NAME (loc->msymbol);
7153
7154 if (b->type == bp_breakpoint && b->loc == loc
7155 && loc->next == NULL && b->related_breakpoint == b)
0e30163f
JK
7156 {
7157 /* Create only the whole new breakpoint of this type but do not
7158 mess more complicated breakpoints with multiple locations. */
7159 b->type = bp_gnu_ifunc_resolver;
6a3a010b
MR
7160 /* Remember the resolver's address for use by the return
7161 breakpoint. */
3467ec66 7162 loc->related_address = loc->address;
0e30163f
JK
7163 }
7164 }
3467ec66
PA
7165 else
7166 find_pc_partial_function (loc->address, &function_name, NULL, NULL);
0e30163f 7167
2c02bd72
DE
7168 if (function_name)
7169 loc->function_name = xstrdup (function_name);
0d381245
VP
7170 }
7171}
7172
a6d9a66e 7173/* Attempt to determine architecture of location identified by SAL. */
1bfeeb0f 7174struct gdbarch *
a6d9a66e
UW
7175get_sal_arch (struct symtab_and_line sal)
7176{
7177 if (sal.section)
7178 return get_objfile_arch (sal.section->objfile);
7179 if (sal.symtab)
eb822aa6 7180 return get_objfile_arch (SYMTAB_OBJFILE (sal.symtab));
a6d9a66e
UW
7181
7182 return NULL;
7183}
7184
346774a9
PA
7185/* Low level routine for partially initializing a breakpoint of type
7186 BPTYPE. The newly created breakpoint's address, section, source
c56053d2 7187 file name, and line number are provided by SAL.
0d381245
VP
7188
7189 It is expected that the caller will complete the initialization of
7190 the newly created breakpoint struct as well as output any status
c56053d2 7191 information regarding the creation of a new breakpoint. */
0d381245 7192
346774a9
PA
7193static void
7194init_raw_breakpoint (struct breakpoint *b, struct gdbarch *gdbarch,
28010a5d 7195 struct symtab_and_line sal, enum bptype bptype,
c0a91b2b 7196 const struct breakpoint_ops *ops)
0d381245 7197{
28010a5d 7198 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
346774a9 7199
3742cc8b 7200 add_location_to_breakpoint (b, &sal);
0d381245 7201
6c95b8df
PA
7202 if (bptype != bp_catchpoint)
7203 gdb_assert (sal.pspace != NULL);
7204
f8eba3c6
TT
7205 /* Store the program space that was used to set the breakpoint,
7206 except for ordinary breakpoints, which are independent of the
7207 program space. */
7208 if (bptype != bp_breakpoint && bptype != bp_hardware_breakpoint)
7209 b->pspace = sal.pspace;
346774a9 7210}
c906108c 7211
346774a9
PA
7212/* set_raw_breakpoint is a low level routine for allocating and
7213 partially initializing a breakpoint of type BPTYPE. The newly
7214 created breakpoint's address, section, source file name, and line
7215 number are provided by SAL. The newly created and partially
7216 initialized breakpoint is added to the breakpoint chain and
7217 is also returned as the value of this function.
7218
7219 It is expected that the caller will complete the initialization of
7220 the newly created breakpoint struct as well as output any status
7221 information regarding the creation of a new breakpoint. In
7222 particular, set_raw_breakpoint does NOT set the breakpoint
7223 number! Care should be taken to not allow an error to occur
7224 prior to completing the initialization of the breakpoint. If this
7225 should happen, a bogus breakpoint will be left on the chain. */
7226
7227struct breakpoint *
7228set_raw_breakpoint (struct gdbarch *gdbarch,
348d480f 7229 struct symtab_and_line sal, enum bptype bptype,
c0a91b2b 7230 const struct breakpoint_ops *ops)
346774a9 7231{
3b0871f4 7232 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (bptype);
346774a9 7233
3b0871f4 7234 init_raw_breakpoint (b.get (), gdbarch, sal, bptype, ops);
b270e6f9 7235 return add_to_breakpoint_chain (std::move (b));
c906108c
SS
7236}
7237
53a5351d 7238/* Call this routine when stepping and nexting to enable a breakpoint
186c406b
TT
7239 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
7240 initiated the operation. */
c906108c
SS
7241
7242void
186c406b 7243set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
c906108c 7244{
35df4500 7245 struct breakpoint *b, *b_tmp;
5d5658a1 7246 int thread = tp->global_num;
0fd8e87f
UW
7247
7248 /* To avoid having to rescan all objfile symbols at every step,
7249 we maintain a list of continually-inserted but always disabled
7250 longjmp "master" breakpoints. Here, we simply create momentary
7251 clones of those and enable them for the requested thread. */
35df4500 7252 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df 7253 if (b->pspace == current_program_space
186c406b
TT
7254 && (b->type == bp_longjmp_master
7255 || b->type == bp_exception_master))
0fd8e87f 7256 {
06edf0c0
PA
7257 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
7258 struct breakpoint *clone;
cc59ec59 7259
e2e4d78b
JK
7260 /* longjmp_breakpoint_ops ensures INITIATING_FRAME is cleared again
7261 after their removal. */
06edf0c0 7262 clone = momentary_breakpoint_from_master (b, type,
c1fc2657 7263 &momentary_breakpoint_ops, 1);
0fd8e87f
UW
7264 clone->thread = thread;
7265 }
186c406b
TT
7266
7267 tp->initiating_frame = frame;
c906108c
SS
7268}
7269
611c83ae 7270/* Delete all longjmp breakpoints from THREAD. */
c906108c 7271void
611c83ae 7272delete_longjmp_breakpoint (int thread)
c906108c 7273{
35df4500 7274 struct breakpoint *b, *b_tmp;
c906108c 7275
35df4500 7276 ALL_BREAKPOINTS_SAFE (b, b_tmp)
186c406b 7277 if (b->type == bp_longjmp || b->type == bp_exception)
611c83ae
PA
7278 {
7279 if (b->thread == thread)
7280 delete_breakpoint (b);
7281 }
c906108c
SS
7282}
7283
f59f708a
PA
7284void
7285delete_longjmp_breakpoint_at_next_stop (int thread)
7286{
7287 struct breakpoint *b, *b_tmp;
7288
7289 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7290 if (b->type == bp_longjmp || b->type == bp_exception)
7291 {
7292 if (b->thread == thread)
7293 b->disposition = disp_del_at_next_stop;
7294 }
7295}
7296
e2e4d78b
JK
7297/* Place breakpoints of type bp_longjmp_call_dummy to catch longjmp for
7298 INFERIOR_PTID thread. Chain them all by RELATED_BREAKPOINT and return
7299 pointer to any of them. Return NULL if this system cannot place longjmp
7300 breakpoints. */
7301
7302struct breakpoint *
7303set_longjmp_breakpoint_for_call_dummy (void)
7304{
7305 struct breakpoint *b, *retval = NULL;
7306
7307 ALL_BREAKPOINTS (b)
7308 if (b->pspace == current_program_space && b->type == bp_longjmp_master)
7309 {
7310 struct breakpoint *new_b;
7311
7312 new_b = momentary_breakpoint_from_master (b, bp_longjmp_call_dummy,
a1aa2221
LM
7313 &momentary_breakpoint_ops,
7314 1);
00431a78 7315 new_b->thread = inferior_thread ()->global_num;
e2e4d78b
JK
7316
7317 /* Link NEW_B into the chain of RETVAL breakpoints. */
7318
7319 gdb_assert (new_b->related_breakpoint == new_b);
7320 if (retval == NULL)
7321 retval = new_b;
7322 new_b->related_breakpoint = retval;
7323 while (retval->related_breakpoint != new_b->related_breakpoint)
7324 retval = retval->related_breakpoint;
7325 retval->related_breakpoint = new_b;
7326 }
7327
7328 return retval;
7329}
7330
7331/* Verify all existing dummy frames and their associated breakpoints for
b67a2c6f 7332 TP. Remove those which can no longer be found in the current frame
e2e4d78b
JK
7333 stack.
7334
7335 You should call this function only at places where it is safe to currently
7336 unwind the whole stack. Failed stack unwind would discard live dummy
7337 frames. */
7338
7339void
b67a2c6f 7340check_longjmp_breakpoint_for_call_dummy (struct thread_info *tp)
e2e4d78b
JK
7341{
7342 struct breakpoint *b, *b_tmp;
7343
7344 ALL_BREAKPOINTS_SAFE (b, b_tmp)
5d5658a1 7345 if (b->type == bp_longjmp_call_dummy && b->thread == tp->global_num)
e2e4d78b
JK
7346 {
7347 struct breakpoint *dummy_b = b->related_breakpoint;
7348
7349 while (dummy_b != b && dummy_b->type != bp_call_dummy)
7350 dummy_b = dummy_b->related_breakpoint;
7351 if (dummy_b->type != bp_call_dummy
7352 || frame_find_by_id (dummy_b->frame_id) != NULL)
7353 continue;
7354
00431a78 7355 dummy_frame_discard (dummy_b->frame_id, tp);
e2e4d78b
JK
7356
7357 while (b->related_breakpoint != b)
7358 {
7359 if (b_tmp == b->related_breakpoint)
7360 b_tmp = b->related_breakpoint->next;
7361 delete_breakpoint (b->related_breakpoint);
7362 }
7363 delete_breakpoint (b);
7364 }
7365}
7366
1900040c
MS
7367void
7368enable_overlay_breakpoints (void)
7369{
52f0bd74 7370 struct breakpoint *b;
1900040c
MS
7371
7372 ALL_BREAKPOINTS (b)
7373 if (b->type == bp_overlay_event)
7374 {
7375 b->enable_state = bp_enabled;
44702360 7376 update_global_location_list (UGLL_MAY_INSERT);
c02f5703 7377 overlay_events_enabled = 1;
1900040c
MS
7378 }
7379}
7380
7381void
7382disable_overlay_breakpoints (void)
7383{
52f0bd74 7384 struct breakpoint *b;
1900040c
MS
7385
7386 ALL_BREAKPOINTS (b)
7387 if (b->type == bp_overlay_event)
7388 {
7389 b->enable_state = bp_disabled;
44702360 7390 update_global_location_list (UGLL_DONT_INSERT);
c02f5703 7391 overlay_events_enabled = 0;
1900040c
MS
7392 }
7393}
7394
aa7d318d
TT
7395/* Set an active std::terminate breakpoint for each std::terminate
7396 master breakpoint. */
7397void
7398set_std_terminate_breakpoint (void)
7399{
35df4500 7400 struct breakpoint *b, *b_tmp;
aa7d318d 7401
35df4500 7402 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
7403 if (b->pspace == current_program_space
7404 && b->type == bp_std_terminate_master)
7405 {
06edf0c0 7406 momentary_breakpoint_from_master (b, bp_std_terminate,
a1aa2221 7407 &momentary_breakpoint_ops, 1);
aa7d318d
TT
7408 }
7409}
7410
7411/* Delete all the std::terminate breakpoints. */
7412void
7413delete_std_terminate_breakpoint (void)
7414{
35df4500 7415 struct breakpoint *b, *b_tmp;
aa7d318d 7416
35df4500 7417 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
7418 if (b->type == bp_std_terminate)
7419 delete_breakpoint (b);
7420}
7421
c4093a6a 7422struct breakpoint *
a6d9a66e 7423create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
c4093a6a
JM
7424{
7425 struct breakpoint *b;
c4093a6a 7426
06edf0c0
PA
7427 b = create_internal_breakpoint (gdbarch, address, bp_thread_event,
7428 &internal_breakpoint_ops);
7429
b5de0fa7 7430 b->enable_state = bp_enabled;
f00aae0f 7431 /* location has to be used or breakpoint_re_set will delete me. */
d28cd78a 7432 b->location = new_address_location (b->loc->address, NULL, 0);
c4093a6a 7433
44702360 7434 update_global_location_list_nothrow (UGLL_MAY_INSERT);
74960c60 7435
c4093a6a
JM
7436 return b;
7437}
7438
0101ce28
JJ
7439struct lang_and_radix
7440 {
7441 enum language lang;
7442 int radix;
7443 };
7444
4efc6507
DE
7445/* Create a breakpoint for JIT code registration and unregistration. */
7446
7447struct breakpoint *
7448create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7449{
2a7f3dff
PA
7450 return create_internal_breakpoint (gdbarch, address, bp_jit_event,
7451 &internal_breakpoint_ops);
4efc6507 7452}
0101ce28 7453
03673fc7
PP
7454/* Remove JIT code registration and unregistration breakpoint(s). */
7455
7456void
7457remove_jit_event_breakpoints (void)
7458{
7459 struct breakpoint *b, *b_tmp;
7460
7461 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7462 if (b->type == bp_jit_event
7463 && b->loc->pspace == current_program_space)
7464 delete_breakpoint (b);
7465}
7466
cae688ec
JJ
7467void
7468remove_solib_event_breakpoints (void)
7469{
35df4500 7470 struct breakpoint *b, *b_tmp;
cae688ec 7471
35df4500 7472 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df
PA
7473 if (b->type == bp_shlib_event
7474 && b->loc->pspace == current_program_space)
cae688ec
JJ
7475 delete_breakpoint (b);
7476}
7477
f37f681c
PA
7478/* See breakpoint.h. */
7479
7480void
7481remove_solib_event_breakpoints_at_next_stop (void)
7482{
7483 struct breakpoint *b, *b_tmp;
7484
7485 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7486 if (b->type == bp_shlib_event
7487 && b->loc->pspace == current_program_space)
7488 b->disposition = disp_del_at_next_stop;
7489}
7490
04086b45
PA
7491/* Helper for create_solib_event_breakpoint /
7492 create_and_insert_solib_event_breakpoint. Allows specifying which
7493 INSERT_MODE to pass through to update_global_location_list. */
7494
7495static struct breakpoint *
7496create_solib_event_breakpoint_1 (struct gdbarch *gdbarch, CORE_ADDR address,
7497 enum ugll_insert_mode insert_mode)
cae688ec
JJ
7498{
7499 struct breakpoint *b;
7500
06edf0c0
PA
7501 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event,
7502 &internal_breakpoint_ops);
04086b45 7503 update_global_location_list_nothrow (insert_mode);
cae688ec
JJ
7504 return b;
7505}
7506
04086b45
PA
7507struct breakpoint *
7508create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7509{
7510 return create_solib_event_breakpoint_1 (gdbarch, address, UGLL_MAY_INSERT);
7511}
7512
f37f681c
PA
7513/* See breakpoint.h. */
7514
7515struct breakpoint *
7516create_and_insert_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7517{
7518 struct breakpoint *b;
7519
04086b45
PA
7520 /* Explicitly tell update_global_location_list to insert
7521 locations. */
7522 b = create_solib_event_breakpoint_1 (gdbarch, address, UGLL_INSERT);
f37f681c
PA
7523 if (!b->loc->inserted)
7524 {
7525 delete_breakpoint (b);
7526 return NULL;
7527 }
7528 return b;
7529}
7530
cae688ec
JJ
7531/* Disable any breakpoints that are on code in shared libraries. Only
7532 apply to enabled breakpoints, disabled ones can just stay disabled. */
7533
7534void
cb851954 7535disable_breakpoints_in_shlibs (void)
cae688ec 7536{
876fa593 7537 struct bp_location *loc, **locp_tmp;
cae688ec 7538
876fa593 7539 ALL_BP_LOCATIONS (loc, locp_tmp)
cae688ec 7540 {
2bdf28a0 7541 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 7542 struct breakpoint *b = loc->owner;
2bdf28a0 7543
4a64f543
MS
7544 /* We apply the check to all breakpoints, including disabled for
7545 those with loc->duplicate set. This is so that when breakpoint
7546 becomes enabled, or the duplicate is removed, gdb will try to
7547 insert all breakpoints. If we don't set shlib_disabled here,
7548 we'll try to insert those breakpoints and fail. */
1042e4c0 7549 if (((b->type == bp_breakpoint)
508ccb1f 7550 || (b->type == bp_jit_event)
1042e4c0 7551 || (b->type == bp_hardware_breakpoint)
d77f58be 7552 || (is_tracepoint (b)))
6c95b8df 7553 && loc->pspace == current_program_space
0d381245 7554 && !loc->shlib_disabled
6c95b8df 7555 && solib_name_from_address (loc->pspace, loc->address)
a77053c2 7556 )
0d381245
VP
7557 {
7558 loc->shlib_disabled = 1;
7559 }
cae688ec
JJ
7560 }
7561}
7562
63644780
NB
7563/* Disable any breakpoints and tracepoints that are in SOLIB upon
7564 notification of unloaded_shlib. Only apply to enabled breakpoints,
7565 disabled ones can just stay disabled. */
84acb35a 7566
75149521 7567static void
84acb35a
JJ
7568disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
7569{
876fa593 7570 struct bp_location *loc, **locp_tmp;
84acb35a
JJ
7571 int disabled_shlib_breaks = 0;
7572
876fa593 7573 ALL_BP_LOCATIONS (loc, locp_tmp)
84acb35a 7574 {
2bdf28a0 7575 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 7576 struct breakpoint *b = loc->owner;
cc59ec59 7577
1e4d1764 7578 if (solib->pspace == loc->pspace
e2dd7057 7579 && !loc->shlib_disabled
1e4d1764
YQ
7580 && (((b->type == bp_breakpoint
7581 || b->type == bp_jit_event
7582 || b->type == bp_hardware_breakpoint)
7583 && (loc->loc_type == bp_loc_hardware_breakpoint
7584 || loc->loc_type == bp_loc_software_breakpoint))
7585 || is_tracepoint (b))
e2dd7057 7586 && solib_contains_address_p (solib, loc->address))
84acb35a 7587 {
e2dd7057
PP
7588 loc->shlib_disabled = 1;
7589 /* At this point, we cannot rely on remove_breakpoint
7590 succeeding so we must mark the breakpoint as not inserted
7591 to prevent future errors occurring in remove_breakpoints. */
7592 loc->inserted = 0;
8d3788bd
VP
7593
7594 /* This may cause duplicate notifications for the same breakpoint. */
76727919 7595 gdb::observers::breakpoint_modified.notify (b);
8d3788bd 7596
e2dd7057
PP
7597 if (!disabled_shlib_breaks)
7598 {
223ffa71 7599 target_terminal::ours_for_output ();
3e43a32a
MS
7600 warning (_("Temporarily disabling breakpoints "
7601 "for unloaded shared library \"%s\""),
e2dd7057 7602 solib->so_name);
84acb35a 7603 }
e2dd7057 7604 disabled_shlib_breaks = 1;
84acb35a
JJ
7605 }
7606 }
84acb35a
JJ
7607}
7608
63644780
NB
7609/* Disable any breakpoints and tracepoints in OBJFILE upon
7610 notification of free_objfile. Only apply to enabled breakpoints,
7611 disabled ones can just stay disabled. */
7612
7613static void
7614disable_breakpoints_in_freed_objfile (struct objfile *objfile)
7615{
7616 struct breakpoint *b;
7617
7618 if (objfile == NULL)
7619 return;
7620
d03de421
PA
7621 /* OBJF_SHARED|OBJF_USERLOADED objfiles are dynamic modules manually
7622 managed by the user with add-symbol-file/remove-symbol-file.
7623 Similarly to how breakpoints in shared libraries are handled in
7624 response to "nosharedlibrary", mark breakpoints in such modules
08351840
PA
7625 shlib_disabled so they end up uninserted on the next global
7626 location list update. Shared libraries not loaded by the user
7627 aren't handled here -- they're already handled in
7628 disable_breakpoints_in_unloaded_shlib, called by solib.c's
7629 solib_unloaded observer. We skip objfiles that are not
d03de421
PA
7630 OBJF_SHARED as those aren't considered dynamic objects (e.g. the
7631 main objfile). */
7632 if ((objfile->flags & OBJF_SHARED) == 0
7633 || (objfile->flags & OBJF_USERLOADED) == 0)
63644780
NB
7634 return;
7635
7636 ALL_BREAKPOINTS (b)
7637 {
7638 struct bp_location *loc;
7639 int bp_modified = 0;
7640
7641 if (!is_breakpoint (b) && !is_tracepoint (b))
7642 continue;
7643
7644 for (loc = b->loc; loc != NULL; loc = loc->next)
7645 {
7646 CORE_ADDR loc_addr = loc->address;
7647
7648 if (loc->loc_type != bp_loc_hardware_breakpoint
7649 && loc->loc_type != bp_loc_software_breakpoint)
7650 continue;
7651
7652 if (loc->shlib_disabled != 0)
7653 continue;
7654
7655 if (objfile->pspace != loc->pspace)
7656 continue;
7657
7658 if (loc->loc_type != bp_loc_hardware_breakpoint
7659 && loc->loc_type != bp_loc_software_breakpoint)
7660 continue;
7661
7662 if (is_addr_in_objfile (loc_addr, objfile))
7663 {
7664 loc->shlib_disabled = 1;
08351840
PA
7665 /* At this point, we don't know whether the object was
7666 unmapped from the inferior or not, so leave the
7667 inserted flag alone. We'll handle failure to
7668 uninsert quietly, in case the object was indeed
7669 unmapped. */
63644780
NB
7670
7671 mark_breakpoint_location_modified (loc);
7672
7673 bp_modified = 1;
7674 }
7675 }
7676
7677 if (bp_modified)
76727919 7678 gdb::observers::breakpoint_modified.notify (b);
63644780
NB
7679 }
7680}
7681
ce78b96d
JB
7682/* FORK & VFORK catchpoints. */
7683
e29a4733 7684/* An instance of this type is used to represent a fork or vfork
c1fc2657
SM
7685 catchpoint. A breakpoint is really of this type iff its ops pointer points
7686 to CATCH_FORK_BREAKPOINT_OPS. */
e29a4733 7687
c1fc2657 7688struct fork_catchpoint : public breakpoint
e29a4733 7689{
e29a4733
PA
7690 /* Process id of a child process whose forking triggered this
7691 catchpoint. This field is only valid immediately after this
7692 catchpoint has triggered. */
7693 ptid_t forked_inferior_pid;
7694};
7695
4a64f543
MS
7696/* Implement the "insert" breakpoint_ops method for fork
7697 catchpoints. */
ce78b96d 7698
77b06cd7
TJB
7699static int
7700insert_catch_fork (struct bp_location *bl)
ce78b96d 7701{
e99b03dc 7702 return target_insert_fork_catchpoint (inferior_ptid.pid ());
ce78b96d
JB
7703}
7704
4a64f543
MS
7705/* Implement the "remove" breakpoint_ops method for fork
7706 catchpoints. */
ce78b96d
JB
7707
7708static int
73971819 7709remove_catch_fork (struct bp_location *bl, enum remove_bp_reason reason)
ce78b96d 7710{
e99b03dc 7711 return target_remove_fork_catchpoint (inferior_ptid.pid ());
ce78b96d
JB
7712}
7713
7714/* Implement the "breakpoint_hit" breakpoint_ops method for fork
7715 catchpoints. */
7716
7717static int
f1310107 7718breakpoint_hit_catch_fork (const struct bp_location *bl,
bd522513 7719 const address_space *aspace, CORE_ADDR bp_addr,
09ac7c10 7720 const struct target_waitstatus *ws)
ce78b96d 7721{
e29a4733
PA
7722 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7723
f90263c1
TT
7724 if (ws->kind != TARGET_WAITKIND_FORKED)
7725 return 0;
7726
7727 c->forked_inferior_pid = ws->value.related_pid;
7728 return 1;
ce78b96d
JB
7729}
7730
4a64f543
MS
7731/* Implement the "print_it" breakpoint_ops method for fork
7732 catchpoints. */
ce78b96d
JB
7733
7734static enum print_stop_action
348d480f 7735print_it_catch_fork (bpstat bs)
ce78b96d 7736{
36dfb11c 7737 struct ui_out *uiout = current_uiout;
348d480f
PA
7738 struct breakpoint *b = bs->breakpoint_at;
7739 struct fork_catchpoint *c = (struct fork_catchpoint *) bs->breakpoint_at;
e29a4733 7740
ce78b96d 7741 annotate_catchpoint (b->number);
f303dbd6 7742 maybe_print_thread_hit_breakpoint (uiout);
36dfb11c 7743 if (b->disposition == disp_del)
112e8700 7744 uiout->text ("Temporary catchpoint ");
36dfb11c 7745 else
112e8700
SM
7746 uiout->text ("Catchpoint ");
7747 if (uiout->is_mi_like_p ())
36dfb11c 7748 {
112e8700
SM
7749 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_FORK));
7750 uiout->field_string ("disp", bpdisp_text (b->disposition));
36dfb11c 7751 }
112e8700
SM
7752 uiout->field_int ("bkptno", b->number);
7753 uiout->text (" (forked process ");
e99b03dc 7754 uiout->field_int ("newpid", c->forked_inferior_pid.pid ());
112e8700 7755 uiout->text ("), ");
ce78b96d
JB
7756 return PRINT_SRC_AND_LOC;
7757}
7758
4a64f543
MS
7759/* Implement the "print_one" breakpoint_ops method for fork
7760 catchpoints. */
ce78b96d
JB
7761
7762static void
a6d9a66e 7763print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 7764{
e29a4733 7765 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
79a45b7d 7766 struct value_print_options opts;
79a45e25 7767 struct ui_out *uiout = current_uiout;
79a45b7d
TT
7768
7769 get_user_print_options (&opts);
7770
4a64f543
MS
7771 /* Field 4, the address, is omitted (which makes the columns not
7772 line up too nicely with the headers, but the effect is relatively
7773 readable). */
79a45b7d 7774 if (opts.addressprint)
112e8700 7775 uiout->field_skip ("addr");
ce78b96d 7776 annotate_field (5);
112e8700 7777 uiout->text ("fork");
d7e15655 7778 if (c->forked_inferior_pid != null_ptid)
ce78b96d 7779 {
112e8700 7780 uiout->text (", process ");
e99b03dc 7781 uiout->field_int ("what", c->forked_inferior_pid.pid ());
112e8700 7782 uiout->spaces (1);
ce78b96d 7783 }
8ac3646f 7784
112e8700
SM
7785 if (uiout->is_mi_like_p ())
7786 uiout->field_string ("catch-type", "fork");
ce78b96d
JB
7787}
7788
7789/* Implement the "print_mention" breakpoint_ops method for fork
7790 catchpoints. */
7791
7792static void
7793print_mention_catch_fork (struct breakpoint *b)
7794{
7795 printf_filtered (_("Catchpoint %d (fork)"), b->number);
7796}
7797
6149aea9
PA
7798/* Implement the "print_recreate" breakpoint_ops method for fork
7799 catchpoints. */
7800
7801static void
7802print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
7803{
7804 fprintf_unfiltered (fp, "catch fork");
d9b3f62e 7805 print_recreate_thread (b, fp);
6149aea9
PA
7806}
7807
ce78b96d
JB
7808/* The breakpoint_ops structure to be used in fork catchpoints. */
7809
2060206e 7810static struct breakpoint_ops catch_fork_breakpoint_ops;
ce78b96d 7811
4a64f543
MS
7812/* Implement the "insert" breakpoint_ops method for vfork
7813 catchpoints. */
ce78b96d 7814
77b06cd7
TJB
7815static int
7816insert_catch_vfork (struct bp_location *bl)
ce78b96d 7817{
e99b03dc 7818 return target_insert_vfork_catchpoint (inferior_ptid.pid ());
ce78b96d
JB
7819}
7820
4a64f543
MS
7821/* Implement the "remove" breakpoint_ops method for vfork
7822 catchpoints. */
ce78b96d
JB
7823
7824static int
73971819 7825remove_catch_vfork (struct bp_location *bl, enum remove_bp_reason reason)
ce78b96d 7826{
e99b03dc 7827 return target_remove_vfork_catchpoint (inferior_ptid.pid ());
ce78b96d
JB
7828}
7829
7830/* Implement the "breakpoint_hit" breakpoint_ops method for vfork
7831 catchpoints. */
7832
7833static int
f1310107 7834breakpoint_hit_catch_vfork (const struct bp_location *bl,
bd522513 7835 const address_space *aspace, CORE_ADDR bp_addr,
09ac7c10 7836 const struct target_waitstatus *ws)
ce78b96d 7837{
e29a4733
PA
7838 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7839
f90263c1
TT
7840 if (ws->kind != TARGET_WAITKIND_VFORKED)
7841 return 0;
7842
7843 c->forked_inferior_pid = ws->value.related_pid;
7844 return 1;
ce78b96d
JB
7845}
7846
4a64f543
MS
7847/* Implement the "print_it" breakpoint_ops method for vfork
7848 catchpoints. */
ce78b96d
JB
7849
7850static enum print_stop_action
348d480f 7851print_it_catch_vfork (bpstat bs)
ce78b96d 7852{
36dfb11c 7853 struct ui_out *uiout = current_uiout;
348d480f 7854 struct breakpoint *b = bs->breakpoint_at;
e29a4733
PA
7855 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
7856
ce78b96d 7857 annotate_catchpoint (b->number);
f303dbd6 7858 maybe_print_thread_hit_breakpoint (uiout);
36dfb11c 7859 if (b->disposition == disp_del)
112e8700 7860 uiout->text ("Temporary catchpoint ");
36dfb11c 7861 else
112e8700
SM
7862 uiout->text ("Catchpoint ");
7863 if (uiout->is_mi_like_p ())
36dfb11c 7864 {
112e8700
SM
7865 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_VFORK));
7866 uiout->field_string ("disp", bpdisp_text (b->disposition));
36dfb11c 7867 }
112e8700
SM
7868 uiout->field_int ("bkptno", b->number);
7869 uiout->text (" (vforked process ");
e99b03dc 7870 uiout->field_int ("newpid", c->forked_inferior_pid.pid ());
112e8700 7871 uiout->text ("), ");
ce78b96d
JB
7872 return PRINT_SRC_AND_LOC;
7873}
7874
4a64f543
MS
7875/* Implement the "print_one" breakpoint_ops method for vfork
7876 catchpoints. */
ce78b96d
JB
7877
7878static void
a6d9a66e 7879print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 7880{
e29a4733 7881 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
79a45b7d 7882 struct value_print_options opts;
79a45e25 7883 struct ui_out *uiout = current_uiout;
79a45b7d
TT
7884
7885 get_user_print_options (&opts);
4a64f543
MS
7886 /* Field 4, the address, is omitted (which makes the columns not
7887 line up too nicely with the headers, but the effect is relatively
7888 readable). */
79a45b7d 7889 if (opts.addressprint)
112e8700 7890 uiout->field_skip ("addr");
ce78b96d 7891 annotate_field (5);
112e8700 7892 uiout->text ("vfork");
d7e15655 7893 if (c->forked_inferior_pid != null_ptid)
ce78b96d 7894 {
112e8700 7895 uiout->text (", process ");
e99b03dc 7896 uiout->field_int ("what", c->forked_inferior_pid.pid ());
112e8700 7897 uiout->spaces (1);
ce78b96d 7898 }
8ac3646f 7899
112e8700
SM
7900 if (uiout->is_mi_like_p ())
7901 uiout->field_string ("catch-type", "vfork");
ce78b96d
JB
7902}
7903
7904/* Implement the "print_mention" breakpoint_ops method for vfork
7905 catchpoints. */
7906
7907static void
7908print_mention_catch_vfork (struct breakpoint *b)
7909{
7910 printf_filtered (_("Catchpoint %d (vfork)"), b->number);
7911}
7912
6149aea9
PA
7913/* Implement the "print_recreate" breakpoint_ops method for vfork
7914 catchpoints. */
7915
7916static void
7917print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
7918{
7919 fprintf_unfiltered (fp, "catch vfork");
d9b3f62e 7920 print_recreate_thread (b, fp);
6149aea9
PA
7921}
7922
ce78b96d
JB
7923/* The breakpoint_ops structure to be used in vfork catchpoints. */
7924
2060206e 7925static struct breakpoint_ops catch_vfork_breakpoint_ops;
ce78b96d 7926
edcc5120 7927/* An instance of this type is used to represent an solib catchpoint.
c1fc2657 7928 A breakpoint is really of this type iff its ops pointer points to
edcc5120
TT
7929 CATCH_SOLIB_BREAKPOINT_OPS. */
7930
c1fc2657 7931struct solib_catchpoint : public breakpoint
edcc5120 7932{
c1fc2657 7933 ~solib_catchpoint () override;
edcc5120
TT
7934
7935 /* True for "catch load", false for "catch unload". */
7936 unsigned char is_load;
7937
7938 /* Regular expression to match, if any. COMPILED is only valid when
7939 REGEX is non-NULL. */
7940 char *regex;
2d7cc5c7 7941 std::unique_ptr<compiled_regex> compiled;
edcc5120
TT
7942};
7943
c1fc2657 7944solib_catchpoint::~solib_catchpoint ()
edcc5120 7945{
c1fc2657 7946 xfree (this->regex);
edcc5120
TT
7947}
7948
7949static int
7950insert_catch_solib (struct bp_location *ignore)
7951{
7952 return 0;
7953}
7954
7955static int
73971819 7956remove_catch_solib (struct bp_location *ignore, enum remove_bp_reason reason)
edcc5120
TT
7957{
7958 return 0;
7959}
7960
7961static int
7962breakpoint_hit_catch_solib (const struct bp_location *bl,
bd522513 7963 const address_space *aspace,
edcc5120
TT
7964 CORE_ADDR bp_addr,
7965 const struct target_waitstatus *ws)
7966{
7967 struct solib_catchpoint *self = (struct solib_catchpoint *) bl->owner;
7968 struct breakpoint *other;
7969
7970 if (ws->kind == TARGET_WAITKIND_LOADED)
7971 return 1;
7972
7973 ALL_BREAKPOINTS (other)
7974 {
7975 struct bp_location *other_bl;
7976
7977 if (other == bl->owner)
7978 continue;
7979
7980 if (other->type != bp_shlib_event)
7981 continue;
7982
c1fc2657 7983 if (self->pspace != NULL && other->pspace != self->pspace)
edcc5120
TT
7984 continue;
7985
7986 for (other_bl = other->loc; other_bl != NULL; other_bl = other_bl->next)
7987 {
7988 if (other->ops->breakpoint_hit (other_bl, aspace, bp_addr, ws))
7989 return 1;
7990 }
7991 }
7992
7993 return 0;
7994}
7995
7996static void
7997check_status_catch_solib (struct bpstats *bs)
7998{
7999 struct solib_catchpoint *self
8000 = (struct solib_catchpoint *) bs->breakpoint_at;
edcc5120
TT
8001
8002 if (self->is_load)
8003 {
52941706 8004 for (so_list *iter : current_program_space->added_solibs)
edcc5120
TT
8005 {
8006 if (!self->regex
2d7cc5c7 8007 || self->compiled->exec (iter->so_name, 0, NULL, 0) == 0)
edcc5120
TT
8008 return;
8009 }
8010 }
8011 else
8012 {
6fb16ce6 8013 for (const std::string &iter : current_program_space->deleted_solibs)
edcc5120
TT
8014 {
8015 if (!self->regex
6fb16ce6 8016 || self->compiled->exec (iter.c_str (), 0, NULL, 0) == 0)
edcc5120
TT
8017 return;
8018 }
8019 }
8020
8021 bs->stop = 0;
8022 bs->print_it = print_it_noop;
8023}
8024
8025static enum print_stop_action
8026print_it_catch_solib (bpstat bs)
8027{
8028 struct breakpoint *b = bs->breakpoint_at;
8029 struct ui_out *uiout = current_uiout;
8030
8031 annotate_catchpoint (b->number);
f303dbd6 8032 maybe_print_thread_hit_breakpoint (uiout);
edcc5120 8033 if (b->disposition == disp_del)
112e8700 8034 uiout->text ("Temporary catchpoint ");
edcc5120 8035 else
112e8700
SM
8036 uiout->text ("Catchpoint ");
8037 uiout->field_int ("bkptno", b->number);
8038 uiout->text ("\n");
8039 if (uiout->is_mi_like_p ())
8040 uiout->field_string ("disp", bpdisp_text (b->disposition));
edcc5120
TT
8041 print_solib_event (1);
8042 return PRINT_SRC_AND_LOC;
8043}
8044
8045static void
8046print_one_catch_solib (struct breakpoint *b, struct bp_location **locs)
8047{
8048 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8049 struct value_print_options opts;
8050 struct ui_out *uiout = current_uiout;
8051 char *msg;
8052
8053 get_user_print_options (&opts);
8054 /* Field 4, the address, is omitted (which makes the columns not
8055 line up too nicely with the headers, but the effect is relatively
8056 readable). */
8057 if (opts.addressprint)
8058 {
8059 annotate_field (4);
112e8700 8060 uiout->field_skip ("addr");
edcc5120
TT
8061 }
8062
8063 annotate_field (5);
8064 if (self->is_load)
8065 {
8066 if (self->regex)
8067 msg = xstrprintf (_("load of library matching %s"), self->regex);
8068 else
8069 msg = xstrdup (_("load of library"));
8070 }
8071 else
8072 {
8073 if (self->regex)
8074 msg = xstrprintf (_("unload of library matching %s"), self->regex);
8075 else
8076 msg = xstrdup (_("unload of library"));
8077 }
112e8700 8078 uiout->field_string ("what", msg);
edcc5120 8079 xfree (msg);
8ac3646f 8080
112e8700
SM
8081 if (uiout->is_mi_like_p ())
8082 uiout->field_string ("catch-type", self->is_load ? "load" : "unload");
edcc5120
TT
8083}
8084
8085static void
8086print_mention_catch_solib (struct breakpoint *b)
8087{
8088 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8089
8090 printf_filtered (_("Catchpoint %d (%s)"), b->number,
8091 self->is_load ? "load" : "unload");
8092}
8093
8094static void
8095print_recreate_catch_solib (struct breakpoint *b, struct ui_file *fp)
8096{
8097 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8098
8099 fprintf_unfiltered (fp, "%s %s",
8100 b->disposition == disp_del ? "tcatch" : "catch",
8101 self->is_load ? "load" : "unload");
8102 if (self->regex)
8103 fprintf_unfiltered (fp, " %s", self->regex);
8104 fprintf_unfiltered (fp, "\n");
8105}
8106
8107static struct breakpoint_ops catch_solib_breakpoint_ops;
8108
91985142
MG
8109/* Shared helper function (MI and CLI) for creating and installing
8110 a shared object event catchpoint. If IS_LOAD is non-zero then
8111 the events to be caught are load events, otherwise they are
8112 unload events. If IS_TEMP is non-zero the catchpoint is a
8113 temporary one. If ENABLED is non-zero the catchpoint is
8114 created in an enabled state. */
edcc5120 8115
91985142 8116void
a121b7c1 8117add_solib_catchpoint (const char *arg, int is_load, int is_temp, int enabled)
edcc5120 8118{
edcc5120 8119 struct gdbarch *gdbarch = get_current_arch ();
edcc5120 8120
edcc5120
TT
8121 if (!arg)
8122 arg = "";
f1735a53 8123 arg = skip_spaces (arg);
edcc5120 8124
36bd8eaa 8125 std::unique_ptr<solib_catchpoint> c (new solib_catchpoint ());
edcc5120
TT
8126
8127 if (*arg != '\0')
8128 {
2d7cc5c7
PA
8129 c->compiled.reset (new compiled_regex (arg, REG_NOSUB,
8130 _("Invalid regexp")));
edcc5120
TT
8131 c->regex = xstrdup (arg);
8132 }
8133
8134 c->is_load = is_load;
36bd8eaa 8135 init_catchpoint (c.get (), gdbarch, is_temp, NULL,
edcc5120
TT
8136 &catch_solib_breakpoint_ops);
8137
c1fc2657 8138 c->enable_state = enabled ? bp_enabled : bp_disabled;
91985142 8139
b270e6f9 8140 install_breakpoint (0, std::move (c), 1);
edcc5120
TT
8141}
8142
91985142
MG
8143/* A helper function that does all the work for "catch load" and
8144 "catch unload". */
8145
8146static void
eb4c3f4a 8147catch_load_or_unload (const char *arg, int from_tty, int is_load,
91985142
MG
8148 struct cmd_list_element *command)
8149{
8150 int tempflag;
8151 const int enabled = 1;
8152
8153 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
8154
8155 add_solib_catchpoint (arg, is_load, tempflag, enabled);
8156}
8157
edcc5120 8158static void
eb4c3f4a 8159catch_load_command_1 (const char *arg, int from_tty,
edcc5120
TT
8160 struct cmd_list_element *command)
8161{
8162 catch_load_or_unload (arg, from_tty, 1, command);
8163}
8164
8165static void
eb4c3f4a 8166catch_unload_command_1 (const char *arg, int from_tty,
edcc5120
TT
8167 struct cmd_list_element *command)
8168{
8169 catch_load_or_unload (arg, from_tty, 0, command);
8170}
8171
346774a9
PA
8172/* Initialize a new breakpoint of the bp_catchpoint kind. If TEMPFLAG
8173 is non-zero, then make the breakpoint temporary. If COND_STRING is
8174 not NULL, then store it in the breakpoint. OPS, if not NULL, is
8175 the breakpoint_ops structure associated to the catchpoint. */
ce78b96d 8176
ab04a2af 8177void
346774a9
PA
8178init_catchpoint (struct breakpoint *b,
8179 struct gdbarch *gdbarch, int tempflag,
63160a43 8180 const char *cond_string,
c0a91b2b 8181 const struct breakpoint_ops *ops)
c906108c 8182{
51abb421 8183 symtab_and_line sal;
6c95b8df 8184 sal.pspace = current_program_space;
c5aa993b 8185
28010a5d 8186 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
ce78b96d 8187
1b36a34b 8188 b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
b5de0fa7 8189 b->disposition = tempflag ? disp_del : disp_donttouch;
346774a9
PA
8190}
8191
28010a5d 8192void
b270e6f9 8193install_breakpoint (int internal, std::unique_ptr<breakpoint> &&arg, int update_gll)
c56053d2 8194{
b270e6f9 8195 breakpoint *b = add_to_breakpoint_chain (std::move (arg));
3a5c3e22 8196 set_breakpoint_number (internal, b);
558a9d82
YQ
8197 if (is_tracepoint (b))
8198 set_tracepoint_count (breakpoint_count);
3a5c3e22
PA
8199 if (!internal)
8200 mention (b);
76727919 8201 gdb::observers::breakpoint_created.notify (b);
3ea46bff
YQ
8202
8203 if (update_gll)
44702360 8204 update_global_location_list (UGLL_MAY_INSERT);
c56053d2
PA
8205}
8206
9b70b993 8207static void
a6d9a66e 8208create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
63160a43 8209 int tempflag, const char *cond_string,
c0a91b2b 8210 const struct breakpoint_ops *ops)
c906108c 8211{
b270e6f9 8212 std::unique_ptr<fork_catchpoint> c (new fork_catchpoint ());
ce78b96d 8213
b270e6f9 8214 init_catchpoint (c.get (), gdbarch, tempflag, cond_string, ops);
e29a4733
PA
8215
8216 c->forked_inferior_pid = null_ptid;
8217
b270e6f9 8218 install_breakpoint (0, std::move (c), 1);
c906108c
SS
8219}
8220
fe798b75
JB
8221/* Exec catchpoints. */
8222
b4d90040 8223/* An instance of this type is used to represent an exec catchpoint.
c1fc2657 8224 A breakpoint is really of this type iff its ops pointer points to
b4d90040
PA
8225 CATCH_EXEC_BREAKPOINT_OPS. */
8226
c1fc2657 8227struct exec_catchpoint : public breakpoint
b4d90040 8228{
c1fc2657 8229 ~exec_catchpoint () override;
b4d90040
PA
8230
8231 /* Filename of a program whose exec triggered this catchpoint.
8232 This field is only valid immediately after this catchpoint has
8233 triggered. */
8234 char *exec_pathname;
8235};
8236
c1fc2657 8237/* Exec catchpoint destructor. */
b4d90040 8238
c1fc2657 8239exec_catchpoint::~exec_catchpoint ()
b4d90040 8240{
c1fc2657 8241 xfree (this->exec_pathname);
b4d90040
PA
8242}
8243
77b06cd7
TJB
8244static int
8245insert_catch_exec (struct bp_location *bl)
c906108c 8246{
e99b03dc 8247 return target_insert_exec_catchpoint (inferior_ptid.pid ());
fe798b75 8248}
c906108c 8249
fe798b75 8250static int
73971819 8251remove_catch_exec (struct bp_location *bl, enum remove_bp_reason reason)
fe798b75 8252{
e99b03dc 8253 return target_remove_exec_catchpoint (inferior_ptid.pid ());
fe798b75 8254}
c906108c 8255
fe798b75 8256static int
f1310107 8257breakpoint_hit_catch_exec (const struct bp_location *bl,
bd522513 8258 const address_space *aspace, CORE_ADDR bp_addr,
09ac7c10 8259 const struct target_waitstatus *ws)
fe798b75 8260{
b4d90040
PA
8261 struct exec_catchpoint *c = (struct exec_catchpoint *) bl->owner;
8262
f90263c1
TT
8263 if (ws->kind != TARGET_WAITKIND_EXECD)
8264 return 0;
8265
8266 c->exec_pathname = xstrdup (ws->value.execd_pathname);
8267 return 1;
fe798b75 8268}
c906108c 8269
fe798b75 8270static enum print_stop_action
348d480f 8271print_it_catch_exec (bpstat bs)
fe798b75 8272{
36dfb11c 8273 struct ui_out *uiout = current_uiout;
348d480f 8274 struct breakpoint *b = bs->breakpoint_at;
b4d90040
PA
8275 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8276
fe798b75 8277 annotate_catchpoint (b->number);
f303dbd6 8278 maybe_print_thread_hit_breakpoint (uiout);
36dfb11c 8279 if (b->disposition == disp_del)
112e8700 8280 uiout->text ("Temporary catchpoint ");
36dfb11c 8281 else
112e8700
SM
8282 uiout->text ("Catchpoint ");
8283 if (uiout->is_mi_like_p ())
36dfb11c 8284 {
112e8700
SM
8285 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_EXEC));
8286 uiout->field_string ("disp", bpdisp_text (b->disposition));
36dfb11c 8287 }
112e8700
SM
8288 uiout->field_int ("bkptno", b->number);
8289 uiout->text (" (exec'd ");
8290 uiout->field_string ("new-exec", c->exec_pathname);
8291 uiout->text ("), ");
36dfb11c 8292
fe798b75 8293 return PRINT_SRC_AND_LOC;
c906108c
SS
8294}
8295
fe798b75 8296static void
a6d9a66e 8297print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
fe798b75 8298{
b4d90040 8299 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
fe798b75 8300 struct value_print_options opts;
79a45e25 8301 struct ui_out *uiout = current_uiout;
fe798b75
JB
8302
8303 get_user_print_options (&opts);
8304
8305 /* Field 4, the address, is omitted (which makes the columns
8306 not line up too nicely with the headers, but the effect
8307 is relatively readable). */
8308 if (opts.addressprint)
112e8700 8309 uiout->field_skip ("addr");
fe798b75 8310 annotate_field (5);
112e8700 8311 uiout->text ("exec");
b4d90040 8312 if (c->exec_pathname != NULL)
fe798b75 8313 {
112e8700
SM
8314 uiout->text (", program \"");
8315 uiout->field_string ("what", c->exec_pathname);
8316 uiout->text ("\" ");
fe798b75 8317 }
8ac3646f 8318
112e8700
SM
8319 if (uiout->is_mi_like_p ())
8320 uiout->field_string ("catch-type", "exec");
fe798b75
JB
8321}
8322
8323static void
8324print_mention_catch_exec (struct breakpoint *b)
8325{
8326 printf_filtered (_("Catchpoint %d (exec)"), b->number);
8327}
8328
6149aea9
PA
8329/* Implement the "print_recreate" breakpoint_ops method for exec
8330 catchpoints. */
8331
8332static void
8333print_recreate_catch_exec (struct breakpoint *b, struct ui_file *fp)
8334{
8335 fprintf_unfiltered (fp, "catch exec");
d9b3f62e 8336 print_recreate_thread (b, fp);
6149aea9
PA
8337}
8338
2060206e 8339static struct breakpoint_ops catch_exec_breakpoint_ops;
fe798b75 8340
c906108c 8341static int
fba45db2 8342hw_breakpoint_used_count (void)
c906108c 8343{
c906108c 8344 int i = 0;
f1310107
TJB
8345 struct breakpoint *b;
8346 struct bp_location *bl;
c906108c
SS
8347
8348 ALL_BREAKPOINTS (b)
c5aa993b 8349 {
d6b74ac4 8350 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
f1310107
TJB
8351 for (bl = b->loc; bl; bl = bl->next)
8352 {
8353 /* Special types of hardware breakpoints may use more than
8354 one register. */
348d480f 8355 i += b->ops->resources_needed (bl);
f1310107 8356 }
c5aa993b 8357 }
c906108c
SS
8358
8359 return i;
8360}
8361
a1398e0c
PA
8362/* Returns the resources B would use if it were a hardware
8363 watchpoint. */
8364
c906108c 8365static int
a1398e0c 8366hw_watchpoint_use_count (struct breakpoint *b)
c906108c 8367{
c906108c 8368 int i = 0;
e09342b5 8369 struct bp_location *bl;
c906108c 8370
a1398e0c
PA
8371 if (!breakpoint_enabled (b))
8372 return 0;
8373
8374 for (bl = b->loc; bl; bl = bl->next)
8375 {
8376 /* Special types of hardware watchpoints may use more than
8377 one register. */
8378 i += b->ops->resources_needed (bl);
8379 }
8380
8381 return i;
8382}
8383
8384/* Returns the sum the used resources of all hardware watchpoints of
8385 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
8386 the sum of the used resources of all hardware watchpoints of other
8387 types _not_ TYPE. */
8388
8389static int
8390hw_watchpoint_used_count_others (struct breakpoint *except,
8391 enum bptype type, int *other_type_used)
8392{
8393 int i = 0;
8394 struct breakpoint *b;
8395
c906108c
SS
8396 *other_type_used = 0;
8397 ALL_BREAKPOINTS (b)
e09342b5 8398 {
a1398e0c
PA
8399 if (b == except)
8400 continue;
e09342b5
TJB
8401 if (!breakpoint_enabled (b))
8402 continue;
8403
a1398e0c
PA
8404 if (b->type == type)
8405 i += hw_watchpoint_use_count (b);
8406 else if (is_hardware_watchpoint (b))
8407 *other_type_used = 1;
e09342b5
TJB
8408 }
8409
c906108c
SS
8410 return i;
8411}
8412
c906108c 8413void
fba45db2 8414disable_watchpoints_before_interactive_call_start (void)
c906108c 8415{
c5aa993b 8416 struct breakpoint *b;
c906108c
SS
8417
8418 ALL_BREAKPOINTS (b)
c5aa993b 8419 {
cc60f2e3 8420 if (is_watchpoint (b) && breakpoint_enabled (b))
c5aa993b 8421 {
b5de0fa7 8422 b->enable_state = bp_call_disabled;
44702360 8423 update_global_location_list (UGLL_DONT_INSERT);
c5aa993b
JM
8424 }
8425 }
c906108c
SS
8426}
8427
8428void
fba45db2 8429enable_watchpoints_after_interactive_call_stop (void)
c906108c 8430{
c5aa993b 8431 struct breakpoint *b;
c906108c
SS
8432
8433 ALL_BREAKPOINTS (b)
c5aa993b 8434 {
cc60f2e3 8435 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
c5aa993b 8436 {
b5de0fa7 8437 b->enable_state = bp_enabled;
44702360 8438 update_global_location_list (UGLL_MAY_INSERT);
c5aa993b
JM
8439 }
8440 }
c906108c
SS
8441}
8442
8bea4e01
UW
8443void
8444disable_breakpoints_before_startup (void)
8445{
6c95b8df 8446 current_program_space->executing_startup = 1;
44702360 8447 update_global_location_list (UGLL_DONT_INSERT);
8bea4e01
UW
8448}
8449
8450void
8451enable_breakpoints_after_startup (void)
8452{
6c95b8df 8453 current_program_space->executing_startup = 0;
f8eba3c6 8454 breakpoint_re_set ();
8bea4e01
UW
8455}
8456
7c16b83e
PA
8457/* Create a new single-step breakpoint for thread THREAD, with no
8458 locations. */
c906108c 8459
7c16b83e
PA
8460static struct breakpoint *
8461new_single_step_breakpoint (int thread, struct gdbarch *gdbarch)
8462{
b270e6f9 8463 std::unique_ptr<breakpoint> b (new breakpoint ());
7c16b83e 8464
b270e6f9 8465 init_raw_breakpoint_without_location (b.get (), gdbarch, bp_single_step,
7c16b83e
PA
8466 &momentary_breakpoint_ops);
8467
8468 b->disposition = disp_donttouch;
8469 b->frame_id = null_frame_id;
8470
8471 b->thread = thread;
8472 gdb_assert (b->thread != 0);
8473
b270e6f9 8474 return add_to_breakpoint_chain (std::move (b));
7c16b83e
PA
8475}
8476
8477/* Set a momentary breakpoint of type TYPE at address specified by
8478 SAL. If FRAME_ID is valid, the breakpoint is restricted to that
8479 frame. */
c906108c 8480
454dafbd 8481breakpoint_up
a6d9a66e
UW
8482set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
8483 struct frame_id frame_id, enum bptype type)
c906108c 8484{
52f0bd74 8485 struct breakpoint *b;
edb3359d 8486
193facb3
JK
8487 /* If FRAME_ID is valid, it should be a real frame, not an inlined or
8488 tail-called one. */
8489 gdb_assert (!frame_id_artificial_p (frame_id));
edb3359d 8490
06edf0c0 8491 b = set_raw_breakpoint (gdbarch, sal, type, &momentary_breakpoint_ops);
b5de0fa7
EZ
8492 b->enable_state = bp_enabled;
8493 b->disposition = disp_donttouch;
818dd999 8494 b->frame_id = frame_id;
c906108c 8495
00431a78 8496 b->thread = inferior_thread ()->global_num;
c906108c 8497
44702360 8498 update_global_location_list_nothrow (UGLL_MAY_INSERT);
74960c60 8499
454dafbd 8500 return breakpoint_up (b);
c906108c 8501}
611c83ae 8502
06edf0c0 8503/* Make a momentary breakpoint based on the master breakpoint ORIG.
a1aa2221
LM
8504 The new breakpoint will have type TYPE, use OPS as its
8505 breakpoint_ops, and will set enabled to LOC_ENABLED. */
e58b0e63 8506
06edf0c0
PA
8507static struct breakpoint *
8508momentary_breakpoint_from_master (struct breakpoint *orig,
8509 enum bptype type,
a1aa2221
LM
8510 const struct breakpoint_ops *ops,
8511 int loc_enabled)
e58b0e63
PA
8512{
8513 struct breakpoint *copy;
8514
06edf0c0 8515 copy = set_raw_breakpoint_without_location (orig->gdbarch, type, ops);
e58b0e63 8516 copy->loc = allocate_bp_location (copy);
0e30163f 8517 set_breakpoint_location_function (copy->loc, 1);
e58b0e63 8518
a6d9a66e 8519 copy->loc->gdbarch = orig->loc->gdbarch;
e58b0e63
PA
8520 copy->loc->requested_address = orig->loc->requested_address;
8521 copy->loc->address = orig->loc->address;
8522 copy->loc->section = orig->loc->section;
6c95b8df 8523 copy->loc->pspace = orig->loc->pspace;
55aa24fb 8524 copy->loc->probe = orig->loc->probe;
f8eba3c6 8525 copy->loc->line_number = orig->loc->line_number;
2f202fde 8526 copy->loc->symtab = orig->loc->symtab;
a1aa2221 8527 copy->loc->enabled = loc_enabled;
e58b0e63
PA
8528 copy->frame_id = orig->frame_id;
8529 copy->thread = orig->thread;
6c95b8df 8530 copy->pspace = orig->pspace;
e58b0e63
PA
8531
8532 copy->enable_state = bp_enabled;
8533 copy->disposition = disp_donttouch;
8534 copy->number = internal_breakpoint_number--;
8535
44702360 8536 update_global_location_list_nothrow (UGLL_DONT_INSERT);
e58b0e63
PA
8537 return copy;
8538}
8539
06edf0c0
PA
8540/* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
8541 ORIG is NULL. */
8542
8543struct breakpoint *
8544clone_momentary_breakpoint (struct breakpoint *orig)
8545{
8546 /* If there's nothing to clone, then return nothing. */
8547 if (orig == NULL)
8548 return NULL;
8549
a1aa2221 8550 return momentary_breakpoint_from_master (orig, orig->type, orig->ops, 0);
06edf0c0
PA
8551}
8552
454dafbd 8553breakpoint_up
a6d9a66e
UW
8554set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
8555 enum bptype type)
611c83ae
PA
8556{
8557 struct symtab_and_line sal;
8558
8559 sal = find_pc_line (pc, 0);
8560 sal.pc = pc;
8561 sal.section = find_pc_overlay (pc);
8562 sal.explicit_pc = 1;
8563
a6d9a66e 8564 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
611c83ae 8565}
c906108c 8566\f
c5aa993b 8567
c906108c
SS
8568/* Tell the user we have just set a breakpoint B. */
8569
8570static void
fba45db2 8571mention (struct breakpoint *b)
c906108c 8572{
348d480f 8573 b->ops->print_mention (b);
2d33446d 8574 current_uiout->text ("\n");
c906108c 8575}
c906108c 8576\f
c5aa993b 8577
1a853c52
PA
8578static int bp_loc_is_permanent (struct bp_location *loc);
8579
0d381245 8580static struct bp_location *
39d61571 8581add_location_to_breakpoint (struct breakpoint *b,
0d381245
VP
8582 const struct symtab_and_line *sal)
8583{
8584 struct bp_location *loc, **tmp;
3742cc8b
YQ
8585 CORE_ADDR adjusted_address;
8586 struct gdbarch *loc_gdbarch = get_sal_arch (*sal);
8587
8588 if (loc_gdbarch == NULL)
8589 loc_gdbarch = b->gdbarch;
8590
8591 /* Adjust the breakpoint's address prior to allocating a location.
8592 Once we call allocate_bp_location(), that mostly uninitialized
8593 location will be placed on the location chain. Adjustment of the
8594 breakpoint may cause target_read_memory() to be called and we do
8595 not want its scan of the location chain to find a breakpoint and
8596 location that's only been partially initialized. */
8597 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
8598 sal->pc, b->type);
0d381245 8599
d30113d4 8600 /* Sort the locations by their ADDRESS. */
39d61571 8601 loc = allocate_bp_location (b);
d30113d4
JK
8602 for (tmp = &(b->loc); *tmp != NULL && (*tmp)->address <= adjusted_address;
8603 tmp = &((*tmp)->next))
0d381245 8604 ;
d30113d4 8605 loc->next = *tmp;
0d381245 8606 *tmp = loc;
3742cc8b 8607
0d381245 8608 loc->requested_address = sal->pc;
3742cc8b 8609 loc->address = adjusted_address;
6c95b8df 8610 loc->pspace = sal->pspace;
935676c9 8611 loc->probe.prob = sal->prob;
729662a5 8612 loc->probe.objfile = sal->objfile;
6c95b8df 8613 gdb_assert (loc->pspace != NULL);
0d381245 8614 loc->section = sal->section;
3742cc8b 8615 loc->gdbarch = loc_gdbarch;
f8eba3c6 8616 loc->line_number = sal->line;
2f202fde 8617 loc->symtab = sal->symtab;
4a27f119 8618 loc->symbol = sal->symbol;
3467ec66
PA
8619 loc->msymbol = sal->msymbol;
8620 loc->objfile = sal->objfile;
f8eba3c6 8621
0e30163f
JK
8622 set_breakpoint_location_function (loc,
8623 sal->explicit_pc || sal->explicit_line);
1a853c52 8624
6ae88661
LM
8625 /* While by definition, permanent breakpoints are already present in the
8626 code, we don't mark the location as inserted. Normally one would expect
8627 that GDB could rely on that breakpoint instruction to stop the program,
8628 thus removing the need to insert its own breakpoint, except that executing
8629 the breakpoint instruction can kill the target instead of reporting a
8630 SIGTRAP. E.g., on SPARC, when interrupts are disabled, executing the
8631 instruction resets the CPU, so QEMU 2.0.0 for SPARC correspondingly dies
8632 with "Trap 0x02 while interrupts disabled, Error state". Letting the
8633 breakpoint be inserted normally results in QEMU knowing about the GDB
8634 breakpoint, and thus trap before the breakpoint instruction is executed.
8635 (If GDB later needs to continue execution past the permanent breakpoint,
8636 it manually increments the PC, thus avoiding executing the breakpoint
8637 instruction.) */
1a853c52 8638 if (bp_loc_is_permanent (loc))
6ae88661 8639 loc->permanent = 1;
1a853c52 8640
0d381245
VP
8641 return loc;
8642}
514f746b
AR
8643\f
8644
1cf4d951 8645/* See breakpoint.h. */
514f746b 8646
1cf4d951
PA
8647int
8648program_breakpoint_here_p (struct gdbarch *gdbarch, CORE_ADDR address)
514f746b
AR
8649{
8650 int len;
8651 CORE_ADDR addr;
1afeeb75 8652 const gdb_byte *bpoint;
514f746b
AR
8653 gdb_byte *target_mem;
8654
1cf4d951
PA
8655 addr = address;
8656 bpoint = gdbarch_breakpoint_from_pc (gdbarch, &addr, &len);
8657
8658 /* Software breakpoints unsupported? */
8659 if (bpoint == NULL)
8660 return 0;
8661
224c3ddb 8662 target_mem = (gdb_byte *) alloca (len);
1cf4d951
PA
8663
8664 /* Enable the automatic memory restoration from breakpoints while
8665 we read the memory. Otherwise we could say about our temporary
8666 breakpoints they are permanent. */
cb85b21b
TT
8667 scoped_restore restore_memory
8668 = make_scoped_restore_show_memory_breakpoints (0);
1cf4d951
PA
8669
8670 if (target_read_memory (address, target_mem, len) == 0
8671 && memcmp (target_mem, bpoint, len) == 0)
cb85b21b 8672 return 1;
1cf4d951 8673
cb85b21b 8674 return 0;
1cf4d951
PA
8675}
8676
8677/* Return 1 if LOC is pointing to a permanent breakpoint,
8678 return 0 otherwise. */
8679
8680static int
8681bp_loc_is_permanent (struct bp_location *loc)
8682{
514f746b
AR
8683 gdb_assert (loc != NULL);
8684
244558af
LM
8685 /* If we have a catchpoint or a watchpoint, just return 0. We should not
8686 attempt to read from the addresses the locations of these breakpoint types
8687 point to. program_breakpoint_here_p, below, will attempt to read
8688 memory. */
8689 if (!breakpoint_address_is_meaningful (loc->owner))
8690 return 0;
8691
5ed8105e 8692 scoped_restore_current_pspace_and_thread restore_pspace_thread;
6c95b8df 8693 switch_to_program_space_and_thread (loc->pspace);
5ed8105e 8694 return program_breakpoint_here_p (loc->gdbarch, loc->address);
514f746b
AR
8695}
8696
e7e0cddf
SS
8697/* Build a command list for the dprintf corresponding to the current
8698 settings of the dprintf style options. */
8699
8700static void
8701update_dprintf_command_list (struct breakpoint *b)
8702{
8703 char *dprintf_args = b->extra_string;
8704 char *printf_line = NULL;
8705
8706 if (!dprintf_args)
8707 return;
8708
8709 dprintf_args = skip_spaces (dprintf_args);
8710
8711 /* Allow a comma, as it may have terminated a location, but don't
8712 insist on it. */
8713 if (*dprintf_args == ',')
8714 ++dprintf_args;
8715 dprintf_args = skip_spaces (dprintf_args);
8716
8717 if (*dprintf_args != '"')
8718 error (_("Bad format string, missing '\"'."));
8719
d3ce09f5 8720 if (strcmp (dprintf_style, dprintf_style_gdb) == 0)
e7e0cddf 8721 printf_line = xstrprintf ("printf %s", dprintf_args);
d3ce09f5 8722 else if (strcmp (dprintf_style, dprintf_style_call) == 0)
e7e0cddf
SS
8723 {
8724 if (!dprintf_function)
8725 error (_("No function supplied for dprintf call"));
8726
8727 if (dprintf_channel && strlen (dprintf_channel) > 0)
8728 printf_line = xstrprintf ("call (void) %s (%s,%s)",
8729 dprintf_function,
8730 dprintf_channel,
8731 dprintf_args);
8732 else
8733 printf_line = xstrprintf ("call (void) %s (%s)",
8734 dprintf_function,
8735 dprintf_args);
8736 }
d3ce09f5
SS
8737 else if (strcmp (dprintf_style, dprintf_style_agent) == 0)
8738 {
8739 if (target_can_run_breakpoint_commands ())
8740 printf_line = xstrprintf ("agent-printf %s", dprintf_args);
8741 else
8742 {
8743 warning (_("Target cannot run dprintf commands, falling back to GDB printf"));
8744 printf_line = xstrprintf ("printf %s", dprintf_args);
8745 }
8746 }
e7e0cddf
SS
8747 else
8748 internal_error (__FILE__, __LINE__,
8749 _("Invalid dprintf style."));
8750
f28045c2 8751 gdb_assert (printf_line != NULL);
e7e0cddf 8752
12973681
TT
8753 /* Manufacture a printf sequence. */
8754 struct command_line *printf_cmd_line
8755 = new struct command_line (simple_control, printf_line);
8756 breakpoint_set_commands (b, counted_command_line (printf_cmd_line,
8757 command_lines_deleter ()));
e7e0cddf
SS
8758}
8759
8760/* Update all dprintf commands, making their command lists reflect
8761 current style settings. */
8762
8763static void
eb4c3f4a 8764update_dprintf_commands (const char *args, int from_tty,
e7e0cddf
SS
8765 struct cmd_list_element *c)
8766{
8767 struct breakpoint *b;
8768
8769 ALL_BREAKPOINTS (b)
8770 {
8771 if (b->type == bp_dprintf)
8772 update_dprintf_command_list (b);
8773 }
8774}
c3f6f71d 8775
f00aae0f
KS
8776/* Create a breakpoint with SAL as location. Use LOCATION
8777 as a description of the location, and COND_STRING
b35a8b2f
DE
8778 as condition expression. If LOCATION is NULL then create an
8779 "address location" from the address in the SAL. */
018d34a4
VP
8780
8781static void
d9b3f62e 8782init_breakpoint_sal (struct breakpoint *b, struct gdbarch *gdbarch,
6c5b2ebe 8783 gdb::array_view<const symtab_and_line> sals,
ffc2605c 8784 event_location_up &&location,
e1e01040
PA
8785 gdb::unique_xmalloc_ptr<char> filter,
8786 gdb::unique_xmalloc_ptr<char> cond_string,
8787 gdb::unique_xmalloc_ptr<char> extra_string,
d9b3f62e
PA
8788 enum bptype type, enum bpdisp disposition,
8789 int thread, int task, int ignore_count,
c0a91b2b 8790 const struct breakpoint_ops *ops, int from_tty,
44f238bb
PA
8791 int enabled, int internal, unsigned flags,
8792 int display_canonical)
018d34a4 8793{
0d381245 8794 int i;
018d34a4
VP
8795
8796 if (type == bp_hardware_breakpoint)
8797 {
fbbd034e
AS
8798 int target_resources_ok;
8799
8800 i = hw_breakpoint_used_count ();
8801 target_resources_ok =
8802 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
018d34a4
VP
8803 i + 1, 0);
8804 if (target_resources_ok == 0)
8805 error (_("No hardware breakpoint support in the target."));
8806 else if (target_resources_ok < 0)
8807 error (_("Hardware breakpoints used exceeds limit."));
8808 }
8809
6c5b2ebe 8810 gdb_assert (!sals.empty ());
6c95b8df 8811
6c5b2ebe 8812 for (const auto &sal : sals)
0d381245 8813 {
0d381245
VP
8814 struct bp_location *loc;
8815
8816 if (from_tty)
5af949e3
UW
8817 {
8818 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
8819 if (!loc_gdbarch)
8820 loc_gdbarch = gdbarch;
8821
8822 describe_other_breakpoints (loc_gdbarch,
6c95b8df 8823 sal.pspace, sal.pc, sal.section, thread);
5af949e3 8824 }
0d381245 8825
6c5b2ebe 8826 if (&sal == &sals[0])
0d381245 8827 {
d9b3f62e 8828 init_raw_breakpoint (b, gdbarch, sal, type, ops);
0d381245 8829 b->thread = thread;
4a306c9a 8830 b->task = task;
855a6e68 8831
e1e01040
PA
8832 b->cond_string = cond_string.release ();
8833 b->extra_string = extra_string.release ();
0d381245 8834 b->ignore_count = ignore_count;
41447f92 8835 b->enable_state = enabled ? bp_enabled : bp_disabled;
0d381245 8836 b->disposition = disposition;
6c95b8df 8837
44f238bb
PA
8838 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
8839 b->loc->inserted = 1;
8840
0fb4aa4b
PA
8841 if (type == bp_static_tracepoint)
8842 {
d9b3f62e 8843 struct tracepoint *t = (struct tracepoint *) b;
0fb4aa4b
PA
8844 struct static_tracepoint_marker marker;
8845
983af33b 8846 if (strace_marker_p (b))
0fb4aa4b
PA
8847 {
8848 /* We already know the marker exists, otherwise, we
8849 wouldn't see a sal for it. */
d28cd78a
TT
8850 const char *p
8851 = &event_location_to_string (b->location.get ())[3];
f00aae0f 8852 const char *endp;
0fb4aa4b 8853
f1735a53 8854 p = skip_spaces (p);
0fb4aa4b 8855
f1735a53 8856 endp = skip_to_space (p);
0fb4aa4b 8857
5d9310c4 8858 t->static_trace_marker_id.assign (p, endp - p);
0fb4aa4b 8859
3e43a32a
MS
8860 printf_filtered (_("Probed static tracepoint "
8861 "marker \"%s\"\n"),
5d9310c4 8862 t->static_trace_marker_id.c_str ());
0fb4aa4b
PA
8863 }
8864 else if (target_static_tracepoint_marker_at (sal.pc, &marker))
8865 {
5d9310c4 8866 t->static_trace_marker_id = std::move (marker.str_id);
0fb4aa4b 8867
3e43a32a
MS
8868 printf_filtered (_("Probed static tracepoint "
8869 "marker \"%s\"\n"),
5d9310c4 8870 t->static_trace_marker_id.c_str ());
0fb4aa4b
PA
8871 }
8872 else
3e43a32a
MS
8873 warning (_("Couldn't determine the static "
8874 "tracepoint marker to probe"));
0fb4aa4b
PA
8875 }
8876
0d381245
VP
8877 loc = b->loc;
8878 }
8879 else
018d34a4 8880 {
39d61571 8881 loc = add_location_to_breakpoint (b, &sal);
44f238bb
PA
8882 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
8883 loc->inserted = 1;
0d381245
VP
8884 }
8885
8886 if (b->cond_string)
8887 {
bbc13ae3
KS
8888 const char *arg = b->cond_string;
8889
1bb9788d
TT
8890 loc->cond = parse_exp_1 (&arg, loc->address,
8891 block_for_pc (loc->address), 0);
0d381245 8892 if (*arg)
588ae58c 8893 error (_("Garbage '%s' follows condition"), arg);
018d34a4 8894 }
e7e0cddf
SS
8895
8896 /* Dynamic printf requires and uses additional arguments on the
8897 command line, otherwise it's an error. */
8898 if (type == bp_dprintf)
8899 {
8900 if (b->extra_string)
8901 update_dprintf_command_list (b);
8902 else
8903 error (_("Format string required"));
8904 }
8905 else if (b->extra_string)
588ae58c 8906 error (_("Garbage '%s' at end of command"), b->extra_string);
855a6e68 8907 }
018d34a4 8908
56435ebe 8909 b->display_canonical = display_canonical;
f00aae0f 8910 if (location != NULL)
d28cd78a 8911 b->location = std::move (location);
018d34a4 8912 else
d28cd78a 8913 b->location = new_address_location (b->loc->address, NULL, 0);
e1e01040 8914 b->filter = filter.release ();
d9b3f62e 8915}
018d34a4 8916
d9b3f62e
PA
8917static void
8918create_breakpoint_sal (struct gdbarch *gdbarch,
6c5b2ebe 8919 gdb::array_view<const symtab_and_line> sals,
ffc2605c 8920 event_location_up &&location,
e1e01040
PA
8921 gdb::unique_xmalloc_ptr<char> filter,
8922 gdb::unique_xmalloc_ptr<char> cond_string,
8923 gdb::unique_xmalloc_ptr<char> extra_string,
d9b3f62e
PA
8924 enum bptype type, enum bpdisp disposition,
8925 int thread, int task, int ignore_count,
c0a91b2b 8926 const struct breakpoint_ops *ops, int from_tty,
44f238bb
PA
8927 int enabled, int internal, unsigned flags,
8928 int display_canonical)
d9b3f62e 8929{
a5e364af 8930 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (type);
d9b3f62e 8931
a5e364af 8932 init_breakpoint_sal (b.get (), gdbarch,
ffc2605c 8933 sals, std::move (location),
e1e01040
PA
8934 std::move (filter),
8935 std::move (cond_string),
8936 std::move (extra_string),
d9b3f62e
PA
8937 type, disposition,
8938 thread, task, ignore_count,
8939 ops, from_tty,
44f238bb
PA
8940 enabled, internal, flags,
8941 display_canonical);
d9b3f62e 8942
b270e6f9 8943 install_breakpoint (internal, std::move (b), 0);
018d34a4
VP
8944}
8945
8946/* Add SALS.nelts breakpoints to the breakpoint table. For each
8947 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
8948 value. COND_STRING, if not NULL, specified the condition to be
8949 used for all breakpoints. Essentially the only case where
8950 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
8951 function. In that case, it's still not possible to specify
8952 separate conditions for different overloaded functions, so
8953 we take just a single condition string.
8954
c3f6f71d 8955 NOTE: If the function succeeds, the caller is expected to cleanup
018d34a4 8956 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
c3f6f71d
JM
8957 array contents). If the function fails (error() is called), the
8958 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
4a64f543 8959 COND and SALS arrays and each of those arrays contents. */
c906108c
SS
8960
8961static void
8cdf0e15 8962create_breakpoints_sal (struct gdbarch *gdbarch,
7efd8fc2 8963 struct linespec_result *canonical,
e1e01040
PA
8964 gdb::unique_xmalloc_ptr<char> cond_string,
8965 gdb::unique_xmalloc_ptr<char> extra_string,
8cdf0e15
VP
8966 enum bptype type, enum bpdisp disposition,
8967 int thread, int task, int ignore_count,
c0a91b2b 8968 const struct breakpoint_ops *ops, int from_tty,
44f238bb 8969 int enabled, int internal, unsigned flags)
c906108c 8970{
f8eba3c6 8971 if (canonical->pre_expanded)
6c5b2ebe 8972 gdb_assert (canonical->lsals.size () == 1);
f8eba3c6 8973
6c5b2ebe 8974 for (const auto &lsal : canonical->lsals)
c3f6f71d 8975 {
f00aae0f 8976 /* Note that 'location' can be NULL in the case of a plain
f8eba3c6 8977 'break', without arguments. */
ffc2605c 8978 event_location_up location
f00aae0f 8979 = (canonical->location != NULL
8e9e35b1 8980 ? copy_event_location (canonical->location.get ()) : NULL);
e1e01040 8981 gdb::unique_xmalloc_ptr<char> filter_string
6c5b2ebe 8982 (lsal.canonical != NULL ? xstrdup (lsal.canonical) : NULL);
0d381245 8983
6c5b2ebe 8984 create_breakpoint_sal (gdbarch, lsal.sals,
ffc2605c 8985 std::move (location),
e1e01040
PA
8986 std::move (filter_string),
8987 std::move (cond_string),
8988 std::move (extra_string),
e7e0cddf 8989 type, disposition,
84f4c1fe 8990 thread, task, ignore_count, ops,
44f238bb 8991 from_tty, enabled, internal, flags,
56435ebe 8992 canonical->special_display);
c3f6f71d 8993 }
c3f6f71d 8994}
c906108c 8995
f00aae0f 8996/* Parse LOCATION which is assumed to be a SAL specification possibly
c3f6f71d 8997 followed by conditionals. On return, SALS contains an array of SAL
f00aae0f
KS
8998 addresses found. LOCATION points to the end of the SAL (for
8999 linespec locations).
9998af43
TJB
9000
9001 The array and the line spec strings are allocated on the heap, it is
9002 the caller's responsibility to free them. */
c906108c 9003
b9362cc7 9004static void
f00aae0f 9005parse_breakpoint_sals (const struct event_location *location,
58438ac1 9006 struct linespec_result *canonical)
c3f6f71d 9007{
f00aae0f
KS
9008 struct symtab_and_line cursal;
9009
9010 if (event_location_type (location) == LINESPEC_LOCATION)
9011 {
a20714ff 9012 const char *spec = get_linespec_location (location)->spec_string;
f00aae0f 9013
a20714ff 9014 if (spec == NULL)
f00aae0f
KS
9015 {
9016 /* The last displayed codepoint, if it's valid, is our default
9017 breakpoint address. */
9018 if (last_displayed_sal_is_valid ())
9019 {
f00aae0f
KS
9020 /* Set sal's pspace, pc, symtab, and line to the values
9021 corresponding to the last call to print_frame_info.
9022 Be sure to reinitialize LINE with NOTCURRENT == 0
9023 as the breakpoint line number is inappropriate otherwise.
9024 find_pc_line would adjust PC, re-set it back. */
51abb421
PA
9025 symtab_and_line sal = get_last_displayed_sal ();
9026 CORE_ADDR pc = sal.pc;
9027
f00aae0f
KS
9028 sal = find_pc_line (pc, 0);
9029
9030 /* "break" without arguments is equivalent to "break *PC"
9031 where PC is the last displayed codepoint's address. So
9032 make sure to set sal.explicit_pc to prevent GDB from
9033 trying to expand the list of sals to include all other
9034 instances with the same symtab and line. */
9035 sal.pc = pc;
9036 sal.explicit_pc = 1;
9037
6c5b2ebe
PA
9038 struct linespec_sals lsal;
9039 lsal.sals = {sal};
f00aae0f
KS
9040 lsal.canonical = NULL;
9041
6c5b2ebe 9042 canonical->lsals.push_back (std::move (lsal));
f00aae0f
KS
9043 return;
9044 }
9045 else
9046 error (_("No default breakpoint address now."));
c906108c 9047 }
c906108c 9048 }
f00aae0f
KS
9049
9050 /* Force almost all breakpoints to be in terms of the
9051 current_source_symtab (which is decode_line_1's default).
9052 This should produce the results we want almost all of the
9053 time while leaving default_breakpoint_* alone.
9054
9055 ObjC: However, don't match an Objective-C method name which
9056 may have a '+' or '-' succeeded by a '['. */
9057 cursal = get_current_source_symtab_and_line ();
9058 if (last_displayed_sal_is_valid ())
c906108c 9059 {
a20714ff 9060 const char *spec = NULL;
cc80f267 9061
f00aae0f 9062 if (event_location_type (location) == LINESPEC_LOCATION)
a20714ff 9063 spec = get_linespec_location (location)->spec_string;
cc80f267 9064
f00aae0f 9065 if (!cursal.symtab
a20714ff
PA
9066 || (spec != NULL
9067 && strchr ("+-", spec[0]) != NULL
9068 && spec[1] != '['))
f00aae0f 9069 {
c2f4122d 9070 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
f00aae0f
KS
9071 get_last_displayed_symtab (),
9072 get_last_displayed_line (),
9073 canonical, NULL, NULL);
9074 return;
9075 }
c906108c 9076 }
f00aae0f 9077
c2f4122d 9078 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
f00aae0f 9079 cursal.symtab, cursal.line, canonical, NULL, NULL);
c3f6f71d 9080}
c906108c 9081
c906108c 9082
c3f6f71d 9083/* Convert each SAL into a real PC. Verify that the PC can be
4a64f543 9084 inserted as a breakpoint. If it can't throw an error. */
c906108c 9085
b9362cc7 9086static void
6c5b2ebe 9087breakpoint_sals_to_pc (std::vector<symtab_and_line> &sals)
c3f6f71d 9088{
6c5b2ebe
PA
9089 for (auto &sal : sals)
9090 resolve_sal_pc (&sal);
c3f6f71d
JM
9091}
9092
7a697b8d
SS
9093/* Fast tracepoints may have restrictions on valid locations. For
9094 instance, a fast tracepoint using a jump instead of a trap will
9095 likely have to overwrite more bytes than a trap would, and so can
9096 only be placed where the instruction is longer than the jump, or a
9097 multi-instruction sequence does not have a jump into the middle of
9098 it, etc. */
9099
9100static void
9101check_fast_tracepoint_sals (struct gdbarch *gdbarch,
6c5b2ebe 9102 gdb::array_view<const symtab_and_line> sals)
7a697b8d 9103{
6c5b2ebe 9104 for (const auto &sal : sals)
7a697b8d 9105 {
f8eba3c6
TT
9106 struct gdbarch *sarch;
9107
6c5b2ebe 9108 sarch = get_sal_arch (sal);
f8eba3c6
TT
9109 /* We fall back to GDBARCH if there is no architecture
9110 associated with SAL. */
9111 if (sarch == NULL)
9112 sarch = gdbarch;
281d762b
TT
9113 std::string msg;
9114 if (!gdbarch_fast_tracepoint_valid_at (sarch, sal.pc, &msg))
53c3572a 9115 error (_("May not have a fast tracepoint at %s%s"),
281d762b 9116 paddress (sarch, sal.pc), msg.c_str ());
7a697b8d
SS
9117 }
9118}
9119
018d34a4
VP
9120/* Given TOK, a string specification of condition and thread, as
9121 accepted by the 'break' command, extract the condition
9122 string and thread number and set *COND_STRING and *THREAD.
4a64f543 9123 PC identifies the context at which the condition should be parsed.
018d34a4
VP
9124 If no condition is found, *COND_STRING is set to NULL.
9125 If no thread is found, *THREAD is set to -1. */
d634f2de
JB
9126
9127static void
bbc13ae3 9128find_condition_and_thread (const char *tok, CORE_ADDR pc,
e7e0cddf
SS
9129 char **cond_string, int *thread, int *task,
9130 char **rest)
018d34a4
VP
9131{
9132 *cond_string = NULL;
9133 *thread = -1;
ed1d1739
KS
9134 *task = 0;
9135 *rest = NULL;
9136
018d34a4
VP
9137 while (tok && *tok)
9138 {
bbc13ae3 9139 const char *end_tok;
018d34a4 9140 int toklen;
bbc13ae3
KS
9141 const char *cond_start = NULL;
9142 const char *cond_end = NULL;
cc59ec59 9143
f1735a53 9144 tok = skip_spaces (tok);
e7e0cddf
SS
9145
9146 if ((*tok == '"' || *tok == ',') && rest)
9147 {
9148 *rest = savestring (tok, strlen (tok));
9149 return;
9150 }
9151
f1735a53 9152 end_tok = skip_to_space (tok);
d634f2de 9153
018d34a4 9154 toklen = end_tok - tok;
d634f2de 9155
018d34a4
VP
9156 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
9157 {
9158 tok = cond_start = end_tok + 1;
4d01a485 9159 parse_exp_1 (&tok, pc, block_for_pc (pc), 0);
018d34a4 9160 cond_end = tok;
d634f2de 9161 *cond_string = savestring (cond_start, cond_end - cond_start);
018d34a4
VP
9162 }
9163 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
9164 {
5d5658a1
PA
9165 const char *tmptok;
9166 struct thread_info *thr;
d634f2de 9167
018d34a4 9168 tok = end_tok + 1;
5d5658a1 9169 thr = parse_thread_id (tok, &tmptok);
018d34a4
VP
9170 if (tok == tmptok)
9171 error (_("Junk after thread keyword."));
5d5658a1 9172 *thread = thr->global_num;
bbc13ae3 9173 tok = tmptok;
018d34a4 9174 }
4a306c9a
JB
9175 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
9176 {
9177 char *tmptok;
9178
9179 tok = end_tok + 1;
bbc13ae3 9180 *task = strtol (tok, &tmptok, 0);
4a306c9a
JB
9181 if (tok == tmptok)
9182 error (_("Junk after task keyword."));
9183 if (!valid_task_id (*task))
b6199126 9184 error (_("Unknown task %d."), *task);
bbc13ae3 9185 tok = tmptok;
4a306c9a 9186 }
e7e0cddf
SS
9187 else if (rest)
9188 {
9189 *rest = savestring (tok, strlen (tok));
ccab2054 9190 return;
e7e0cddf 9191 }
018d34a4
VP
9192 else
9193 error (_("Junk at end of arguments."));
9194 }
9195}
9196
0fb4aa4b
PA
9197/* Decode a static tracepoint marker spec. */
9198
6c5b2ebe 9199static std::vector<symtab_and_line>
f00aae0f 9200decode_static_tracepoint_spec (const char **arg_p)
0fb4aa4b 9201{
f00aae0f
KS
9202 const char *p = &(*arg_p)[3];
9203 const char *endp;
0fb4aa4b 9204
f1735a53 9205 p = skip_spaces (p);
0fb4aa4b 9206
f1735a53 9207 endp = skip_to_space (p);
0fb4aa4b 9208
81b1e71c 9209 std::string marker_str (p, endp - p);
0fb4aa4b 9210
5d9310c4
SM
9211 std::vector<static_tracepoint_marker> markers
9212 = target_static_tracepoint_markers_by_strid (marker_str.c_str ());
9213 if (markers.empty ())
81b1e71c
TT
9214 error (_("No known static tracepoint marker named %s"),
9215 marker_str.c_str ());
0fb4aa4b 9216
6c5b2ebe 9217 std::vector<symtab_and_line> sals;
5d9310c4 9218 sals.reserve (markers.size ());
0fb4aa4b 9219
5d9310c4 9220 for (const static_tracepoint_marker &marker : markers)
0fb4aa4b 9221 {
5d9310c4
SM
9222 symtab_and_line sal = find_pc_line (marker.address, 0);
9223 sal.pc = marker.address;
6c5b2ebe 9224 sals.push_back (sal);
5d9310c4 9225 }
0fb4aa4b 9226
0fb4aa4b
PA
9227 *arg_p = endp;
9228 return sals;
9229}
9230
f00aae0f 9231/* See breakpoint.h. */
0101ce28 9232
8cdf0e15
VP
9233int
9234create_breakpoint (struct gdbarch *gdbarch,
e1e01040
PA
9235 const struct event_location *location,
9236 const char *cond_string,
9237 int thread, const char *extra_string,
f00aae0f 9238 int parse_extra,
0fb4aa4b 9239 int tempflag, enum bptype type_wanted,
8cdf0e15
VP
9240 int ignore_count,
9241 enum auto_boolean pending_break_support,
c0a91b2b 9242 const struct breakpoint_ops *ops,
44f238bb
PA
9243 int from_tty, int enabled, int internal,
9244 unsigned flags)
c3f6f71d 9245{
7efd8fc2 9246 struct linespec_result canonical;
80c99de1 9247 struct cleanup *bkpt_chain = NULL;
0101ce28 9248 int pending = 0;
4a306c9a 9249 int task = 0;
86b17b60 9250 int prev_bkpt_count = breakpoint_count;
c3f6f71d 9251
348d480f
PA
9252 gdb_assert (ops != NULL);
9253
f00aae0f
KS
9254 /* If extra_string isn't useful, set it to NULL. */
9255 if (extra_string != NULL && *extra_string == '\0')
9256 extra_string = NULL;
9257
492d29ea 9258 TRY
b78a6381 9259 {
f00aae0f 9260 ops->create_sals_from_location (location, &canonical, type_wanted);
b78a6381 9261 }
492d29ea 9262 CATCH (e, RETURN_MASK_ERROR)
0101ce28 9263 {
492d29ea
PA
9264 /* If caller is interested in rc value from parse, set
9265 value. */
9266 if (e.error == NOT_FOUND_ERROR)
0101ce28 9267 {
05ff989b
AC
9268 /* If pending breakpoint support is turned off, throw
9269 error. */
fa8d40ab
JJ
9270
9271 if (pending_break_support == AUTO_BOOLEAN_FALSE)
723a2275
VP
9272 throw_exception (e);
9273
9274 exception_print (gdb_stderr, e);
fa8d40ab 9275
05ff989b
AC
9276 /* If pending breakpoint support is auto query and the user
9277 selects no, then simply return the error code. */
059fb39f 9278 if (pending_break_support == AUTO_BOOLEAN_AUTO
bfccc43c
YQ
9279 && !nquery (_("Make %s pending on future shared library load? "),
9280 bptype_string (type_wanted)))
fd9b8c24 9281 return 0;
fa8d40ab 9282
05ff989b
AC
9283 /* At this point, either the user was queried about setting
9284 a pending breakpoint and selected yes, or pending
9285 breakpoint behavior is on and thus a pending breakpoint
9286 is defaulted on behalf of the user. */
f00aae0f 9287 pending = 1;
0101ce28 9288 }
492d29ea
PA
9289 else
9290 throw_exception (e);
0101ce28 9291 }
492d29ea
PA
9292 END_CATCH
9293
6c5b2ebe 9294 if (!pending && canonical.lsals.empty ())
492d29ea 9295 return 0;
c3f6f71d 9296
c3f6f71d
JM
9297 /* ----------------------------- SNIP -----------------------------
9298 Anything added to the cleanup chain beyond this point is assumed
9299 to be part of a breakpoint. If the breakpoint create succeeds
80c99de1
PA
9300 then the memory is not reclaimed. */
9301 bkpt_chain = make_cleanup (null_cleanup, 0);
c3f6f71d 9302
c3f6f71d
JM
9303 /* Resolve all line numbers to PC's and verify that the addresses
9304 are ok for the target. */
0101ce28 9305 if (!pending)
f8eba3c6 9306 {
6c5b2ebe
PA
9307 for (auto &lsal : canonical.lsals)
9308 breakpoint_sals_to_pc (lsal.sals);
f8eba3c6 9309 }
c3f6f71d 9310
7a697b8d 9311 /* Fast tracepoints may have additional restrictions on location. */
bfccc43c 9312 if (!pending && type_wanted == bp_fast_tracepoint)
f8eba3c6 9313 {
6c5b2ebe
PA
9314 for (const auto &lsal : canonical.lsals)
9315 check_fast_tracepoint_sals (gdbarch, lsal.sals);
f8eba3c6 9316 }
7a697b8d 9317
c3f6f71d
JM
9318 /* Verify that condition can be parsed, before setting any
9319 breakpoints. Allocate a separate condition expression for each
4a64f543 9320 breakpoint. */
0101ce28 9321 if (!pending)
c3f6f71d 9322 {
e1e01040
PA
9323 gdb::unique_xmalloc_ptr<char> cond_string_copy;
9324 gdb::unique_xmalloc_ptr<char> extra_string_copy;
9325
f00aae0f 9326 if (parse_extra)
72b2ff0e 9327 {
0878d0fa 9328 char *rest;
e1e01040 9329 char *cond;
52d361e1 9330
6c5b2ebe 9331 const linespec_sals &lsal = canonical.lsals[0];
52d361e1 9332
0878d0fa
YQ
9333 /* Here we only parse 'arg' to separate condition
9334 from thread number, so parsing in context of first
9335 sal is OK. When setting the breakpoint we'll
9336 re-parse it in context of each sal. */
9337
6c5b2ebe 9338 find_condition_and_thread (extra_string, lsal.sals[0].pc,
e1e01040
PA
9339 &cond, &thread, &task, &rest);
9340 cond_string_copy.reset (cond);
9341 extra_string_copy.reset (rest);
72b2ff0e 9342 }
2f069f6f 9343 else
72b2ff0e 9344 {
f00aae0f
KS
9345 if (type_wanted != bp_dprintf
9346 && extra_string != NULL && *extra_string != '\0')
9347 error (_("Garbage '%s' at end of location"), extra_string);
0878d0fa
YQ
9348
9349 /* Create a private copy of condition string. */
9350 if (cond_string)
e1e01040 9351 cond_string_copy.reset (xstrdup (cond_string));
0878d0fa
YQ
9352 /* Create a private copy of any extra string. */
9353 if (extra_string)
e1e01040 9354 extra_string_copy.reset (xstrdup (extra_string));
72b2ff0e 9355 }
0fb4aa4b 9356
52d361e1 9357 ops->create_breakpoints_sal (gdbarch, &canonical,
e1e01040
PA
9358 std::move (cond_string_copy),
9359 std::move (extra_string_copy),
9360 type_wanted,
d9b3f62e
PA
9361 tempflag ? disp_del : disp_donttouch,
9362 thread, task, ignore_count, ops,
44f238bb 9363 from_tty, enabled, internal, flags);
c906108c 9364 }
0101ce28
JJ
9365 else
9366 {
a5e364af 9367 std::unique_ptr <breakpoint> b = new_breakpoint_from_type (type_wanted);
bfccc43c 9368
a5e364af 9369 init_raw_breakpoint_without_location (b.get (), gdbarch, type_wanted, ops);
d28cd78a 9370 b->location = copy_event_location (location);
bfccc43c 9371
f00aae0f
KS
9372 if (parse_extra)
9373 b->cond_string = NULL;
e12c7713
MK
9374 else
9375 {
9376 /* Create a private copy of condition string. */
e1e01040 9377 b->cond_string = cond_string != NULL ? xstrdup (cond_string) : NULL;
15630549 9378 b->thread = thread;
e12c7713 9379 }
f00aae0f
KS
9380
9381 /* Create a private copy of any extra string. */
e1e01040 9382 b->extra_string = extra_string != NULL ? xstrdup (extra_string) : NULL;
0101ce28 9383 b->ignore_count = ignore_count;
0101ce28 9384 b->disposition = tempflag ? disp_del : disp_donttouch;
0d381245 9385 b->condition_not_parsed = 1;
41447f92 9386 b->enable_state = enabled ? bp_enabled : bp_disabled;
cc72b2a2
KP
9387 if ((type_wanted != bp_breakpoint
9388 && type_wanted != bp_hardware_breakpoint) || thread != -1)
f8eba3c6 9389 b->pspace = current_program_space;
8bea4e01 9390
b270e6f9 9391 install_breakpoint (internal, std::move (b), 0);
0101ce28
JJ
9392 }
9393
6c5b2ebe 9394 if (canonical.lsals.size () > 1)
95a42b64 9395 {
3e43a32a
MS
9396 warning (_("Multiple breakpoints were set.\nUse the "
9397 "\"delete\" command to delete unwanted breakpoints."));
86b17b60 9398 prev_breakpoint_count = prev_bkpt_count;
95a42b64
TT
9399 }
9400
80c99de1
PA
9401 /* That's it. Discard the cleanups for data inserted into the
9402 breakpoint. */
9403 discard_cleanups (bkpt_chain);
217dc9e2 9404
80c99de1 9405 /* error call may happen here - have BKPT_CHAIN already discarded. */
44702360 9406 update_global_location_list (UGLL_MAY_INSERT);
fd9b8c24
PA
9407
9408 return 1;
c3f6f71d 9409}
c906108c 9410
348d480f 9411/* Set a breakpoint.
72b2ff0e
VP
9412 ARG is a string describing breakpoint address,
9413 condition, and thread.
9414 FLAG specifies if a breakpoint is hardware on,
9415 and if breakpoint is temporary, using BP_HARDWARE_FLAG
9416 and BP_TEMPFLAG. */
348d480f 9417
98deb0da 9418static void
f2fc3015 9419break_command_1 (const char *arg, int flag, int from_tty)
c3f6f71d 9420{
72b2ff0e 9421 int tempflag = flag & BP_TEMPFLAG;
0fb4aa4b
PA
9422 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
9423 ? bp_hardware_breakpoint
9424 : bp_breakpoint);
55aa24fb 9425 struct breakpoint_ops *ops;
f00aae0f 9426
ffc2605c 9427 event_location_up location = string_to_event_location (&arg, current_language);
55aa24fb
SDJ
9428
9429 /* Matching breakpoints on probes. */
5b56227b 9430 if (location != NULL
ffc2605c 9431 && event_location_type (location.get ()) == PROBE_LOCATION)
55aa24fb
SDJ
9432 ops = &bkpt_probe_breakpoint_ops;
9433 else
9434 ops = &bkpt_breakpoint_ops;
c3f6f71d 9435
8cdf0e15 9436 create_breakpoint (get_current_arch (),
ffc2605c 9437 location.get (),
f00aae0f 9438 NULL, 0, arg, 1 /* parse arg */,
0fb4aa4b 9439 tempflag, type_wanted,
8cdf0e15
VP
9440 0 /* Ignore count */,
9441 pending_break_support,
55aa24fb 9442 ops,
8cdf0e15 9443 from_tty,
84f4c1fe 9444 1 /* enabled */,
44f238bb
PA
9445 0 /* internal */,
9446 0);
c906108c
SS
9447}
9448
c906108c
SS
9449/* Helper function for break_command_1 and disassemble_command. */
9450
9451void
fba45db2 9452resolve_sal_pc (struct symtab_and_line *sal)
c906108c
SS
9453{
9454 CORE_ADDR pc;
9455
9456 if (sal->pc == 0 && sal->symtab != NULL)
9457 {
9458 if (!find_line_pc (sal->symtab, sal->line, &pc))
8a3fe4f8 9459 error (_("No line %d in file \"%s\"."),
05cba821 9460 sal->line, symtab_to_filename_for_display (sal->symtab));
c906108c 9461 sal->pc = pc;
6a048695 9462
4a64f543
MS
9463 /* If this SAL corresponds to a breakpoint inserted using a line
9464 number, then skip the function prologue if necessary. */
6a048695 9465 if (sal->explicit_line)
059acae7 9466 skip_prologue_sal (sal);
c906108c
SS
9467 }
9468
9469 if (sal->section == 0 && sal->symtab != NULL)
9470 {
346d1dfe 9471 const struct blockvector *bv;
3977b71f 9472 const struct block *b;
c5aa993b 9473 struct symbol *sym;
c906108c 9474
43f3e411
DE
9475 bv = blockvector_for_pc_sect (sal->pc, 0, &b,
9476 SYMTAB_COMPUNIT (sal->symtab));
c906108c
SS
9477 if (bv != NULL)
9478 {
7f0df278 9479 sym = block_linkage_function (b);
c906108c
SS
9480 if (sym != NULL)
9481 {
eb822aa6
DE
9482 fixup_symbol_section (sym, SYMTAB_OBJFILE (sal->symtab));
9483 sal->section = SYMBOL_OBJ_SECTION (SYMTAB_OBJFILE (sal->symtab),
9484 sym);
c906108c
SS
9485 }
9486 else
9487 {
4a64f543
MS
9488 /* It really is worthwhile to have the section, so we'll
9489 just have to look harder. This case can be executed
9490 if we have line numbers but no functions (as can
9491 happen in assembly source). */
c906108c 9492
5ed8105e 9493 scoped_restore_current_pspace_and_thread restore_pspace_thread;
6c95b8df 9494 switch_to_program_space_and_thread (sal->pspace);
c906108c 9495
5ed8105e 9496 bound_minimal_symbol msym = lookup_minimal_symbol_by_pc (sal->pc);
7cbd4a93 9497 if (msym.minsym)
efd66ac6 9498 sal->section = MSYMBOL_OBJ_SECTION (msym.objfile, msym.minsym);
c906108c
SS
9499 }
9500 }
9501 }
9502}
9503
9504void
0b39b52e 9505break_command (const char *arg, int from_tty)
c906108c 9506{
db107f19 9507 break_command_1 (arg, 0, from_tty);
c906108c
SS
9508}
9509
c906108c 9510void
0b39b52e 9511tbreak_command (const char *arg, int from_tty)
c906108c 9512{
db107f19 9513 break_command_1 (arg, BP_TEMPFLAG, from_tty);
c906108c
SS
9514}
9515
c906108c 9516static void
0b39b52e 9517hbreak_command (const char *arg, int from_tty)
c906108c 9518{
db107f19 9519 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
c906108c
SS
9520}
9521
9522static void
0b39b52e 9523thbreak_command (const char *arg, int from_tty)
c906108c 9524{
db107f19 9525 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
c906108c
SS
9526}
9527
9528static void
ee7ddd71 9529stop_command (const char *arg, int from_tty)
c906108c 9530{
a3f17187 9531 printf_filtered (_("Specify the type of breakpoint to set.\n\
c906108c 9532Usage: stop in <function | address>\n\
a3f17187 9533 stop at <line>\n"));
c906108c
SS
9534}
9535
9536static void
4495129a 9537stopin_command (const char *arg, int from_tty)
c906108c
SS
9538{
9539 int badInput = 0;
9540
c5aa993b 9541 if (arg == (char *) NULL)
c906108c
SS
9542 badInput = 1;
9543 else if (*arg != '*')
9544 {
4495129a 9545 const char *argptr = arg;
c906108c
SS
9546 int hasColon = 0;
9547
4a64f543 9548 /* Look for a ':'. If this is a line number specification, then
53a5351d 9549 say it is bad, otherwise, it should be an address or
4a64f543 9550 function/method name. */
c906108c 9551 while (*argptr && !hasColon)
c5aa993b
JM
9552 {
9553 hasColon = (*argptr == ':');
9554 argptr++;
9555 }
c906108c
SS
9556
9557 if (hasColon)
c5aa993b 9558 badInput = (*argptr != ':'); /* Not a class::method */
c906108c 9559 else
c5aa993b 9560 badInput = isdigit (*arg); /* a simple line number */
c906108c
SS
9561 }
9562
9563 if (badInput)
a3f17187 9564 printf_filtered (_("Usage: stop in <function | address>\n"));
c906108c 9565 else
db107f19 9566 break_command_1 (arg, 0, from_tty);
c906108c
SS
9567}
9568
9569static void
4495129a 9570stopat_command (const char *arg, int from_tty)
c906108c
SS
9571{
9572 int badInput = 0;
9573
c5aa993b 9574 if (arg == (char *) NULL || *arg == '*') /* no line number */
c906108c
SS
9575 badInput = 1;
9576 else
9577 {
4495129a 9578 const char *argptr = arg;
c906108c
SS
9579 int hasColon = 0;
9580
4a64f543
MS
9581 /* Look for a ':'. If there is a '::' then get out, otherwise
9582 it is probably a line number. */
c906108c 9583 while (*argptr && !hasColon)
c5aa993b
JM
9584 {
9585 hasColon = (*argptr == ':');
9586 argptr++;
9587 }
c906108c
SS
9588
9589 if (hasColon)
c5aa993b 9590 badInput = (*argptr == ':'); /* we have class::method */
c906108c 9591 else
c5aa993b 9592 badInput = !isdigit (*arg); /* not a line number */
c906108c
SS
9593 }
9594
9595 if (badInput)
a3f17187 9596 printf_filtered (_("Usage: stop at <line>\n"));
c906108c 9597 else
db107f19 9598 break_command_1 (arg, 0, from_tty);
c906108c
SS
9599}
9600
e7e0cddf
SS
9601/* The dynamic printf command is mostly like a regular breakpoint, but
9602 with a prewired command list consisting of a single output command,
9603 built from extra arguments supplied on the dprintf command
9604 line. */
9605
da821c7b 9606static void
0b39b52e 9607dprintf_command (const char *arg, int from_tty)
e7e0cddf 9608{
ffc2605c 9609 event_location_up location = string_to_event_location (&arg, current_language);
f00aae0f
KS
9610
9611 /* If non-NULL, ARG should have been advanced past the location;
9612 the next character must be ','. */
9613 if (arg != NULL)
9614 {
9615 if (arg[0] != ',' || arg[1] == '\0')
9616 error (_("Format string required"));
9617 else
9618 {
9619 /* Skip the comma. */
9620 ++arg;
9621 }
9622 }
9623
e7e0cddf 9624 create_breakpoint (get_current_arch (),
ffc2605c 9625 location.get (),
f00aae0f 9626 NULL, 0, arg, 1 /* parse arg */,
e7e0cddf
SS
9627 0, bp_dprintf,
9628 0 /* Ignore count */,
9629 pending_break_support,
9630 &dprintf_breakpoint_ops,
9631 from_tty,
9632 1 /* enabled */,
9633 0 /* internal */,
9634 0);
9635}
9636
d3ce09f5 9637static void
0b39b52e 9638agent_printf_command (const char *arg, int from_tty)
d3ce09f5
SS
9639{
9640 error (_("May only run agent-printf on the target"));
9641}
9642
f1310107
TJB
9643/* Implement the "breakpoint_hit" breakpoint_ops method for
9644 ranged breakpoints. */
9645
9646static int
9647breakpoint_hit_ranged_breakpoint (const struct bp_location *bl,
bd522513 9648 const address_space *aspace,
09ac7c10
TT
9649 CORE_ADDR bp_addr,
9650 const struct target_waitstatus *ws)
f1310107 9651{
09ac7c10 9652 if (ws->kind != TARGET_WAITKIND_STOPPED
a493e3e2 9653 || ws->value.sig != GDB_SIGNAL_TRAP)
09ac7c10
TT
9654 return 0;
9655
f1310107
TJB
9656 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
9657 bl->length, aspace, bp_addr);
9658}
9659
9660/* Implement the "resources_needed" breakpoint_ops method for
9661 ranged breakpoints. */
9662
9663static int
9664resources_needed_ranged_breakpoint (const struct bp_location *bl)
9665{
9666 return target_ranged_break_num_registers ();
9667}
9668
9669/* Implement the "print_it" breakpoint_ops method for
9670 ranged breakpoints. */
9671
9672static enum print_stop_action
348d480f 9673print_it_ranged_breakpoint (bpstat bs)
f1310107 9674{
348d480f 9675 struct breakpoint *b = bs->breakpoint_at;
f1310107 9676 struct bp_location *bl = b->loc;
79a45e25 9677 struct ui_out *uiout = current_uiout;
f1310107
TJB
9678
9679 gdb_assert (b->type == bp_hardware_breakpoint);
9680
9681 /* Ranged breakpoints have only one location. */
9682 gdb_assert (bl && bl->next == NULL);
9683
9684 annotate_breakpoint (b->number);
f303dbd6
PA
9685
9686 maybe_print_thread_hit_breakpoint (uiout);
9687
f1310107 9688 if (b->disposition == disp_del)
112e8700 9689 uiout->text ("Temporary ranged breakpoint ");
f1310107 9690 else
112e8700
SM
9691 uiout->text ("Ranged breakpoint ");
9692 if (uiout->is_mi_like_p ())
f1310107 9693 {
112e8700 9694 uiout->field_string ("reason",
f1310107 9695 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
112e8700 9696 uiout->field_string ("disp", bpdisp_text (b->disposition));
f1310107 9697 }
112e8700
SM
9698 uiout->field_int ("bkptno", b->number);
9699 uiout->text (", ");
f1310107
TJB
9700
9701 return PRINT_SRC_AND_LOC;
9702}
9703
9704/* Implement the "print_one" breakpoint_ops method for
9705 ranged breakpoints. */
9706
9707static void
9708print_one_ranged_breakpoint (struct breakpoint *b,
9709 struct bp_location **last_loc)
9710{
9711 struct bp_location *bl = b->loc;
9712 struct value_print_options opts;
79a45e25 9713 struct ui_out *uiout = current_uiout;
f1310107
TJB
9714
9715 /* Ranged breakpoints have only one location. */
9716 gdb_assert (bl && bl->next == NULL);
9717
9718 get_user_print_options (&opts);
9719
9720 if (opts.addressprint)
9721 /* We don't print the address range here, it will be printed later
9722 by print_one_detail_ranged_breakpoint. */
112e8700 9723 uiout->field_skip ("addr");
f1310107
TJB
9724 annotate_field (5);
9725 print_breakpoint_location (b, bl);
9726 *last_loc = bl;
9727}
9728
9729/* Implement the "print_one_detail" breakpoint_ops method for
9730 ranged breakpoints. */
9731
9732static void
9733print_one_detail_ranged_breakpoint (const struct breakpoint *b,
9734 struct ui_out *uiout)
9735{
9736 CORE_ADDR address_start, address_end;
9737 struct bp_location *bl = b->loc;
d7e74731 9738 string_file stb;
f1310107
TJB
9739
9740 gdb_assert (bl);
9741
9742 address_start = bl->address;
9743 address_end = address_start + bl->length - 1;
9744
112e8700 9745 uiout->text ("\taddress range: ");
d7e74731
PA
9746 stb.printf ("[%s, %s]",
9747 print_core_address (bl->gdbarch, address_start),
9748 print_core_address (bl->gdbarch, address_end));
112e8700
SM
9749 uiout->field_stream ("addr", stb);
9750 uiout->text ("\n");
f1310107
TJB
9751}
9752
9753/* Implement the "print_mention" breakpoint_ops method for
9754 ranged breakpoints. */
9755
9756static void
9757print_mention_ranged_breakpoint (struct breakpoint *b)
9758{
9759 struct bp_location *bl = b->loc;
79a45e25 9760 struct ui_out *uiout = current_uiout;
f1310107
TJB
9761
9762 gdb_assert (bl);
9763 gdb_assert (b->type == bp_hardware_breakpoint);
9764
2d33446d
TT
9765 uiout->message (_("Hardware assisted ranged breakpoint %d from %s to %s."),
9766 b->number, paddress (bl->gdbarch, bl->address),
9767 paddress (bl->gdbarch, bl->address + bl->length - 1));
f1310107
TJB
9768}
9769
9770/* Implement the "print_recreate" breakpoint_ops method for
9771 ranged breakpoints. */
9772
9773static void
9774print_recreate_ranged_breakpoint (struct breakpoint *b, struct ui_file *fp)
9775{
f00aae0f 9776 fprintf_unfiltered (fp, "break-range %s, %s",
d28cd78a
TT
9777 event_location_to_string (b->location.get ()),
9778 event_location_to_string (b->location_range_end.get ()));
d9b3f62e 9779 print_recreate_thread (b, fp);
f1310107
TJB
9780}
9781
9782/* The breakpoint_ops structure to be used in ranged breakpoints. */
9783
2060206e 9784static struct breakpoint_ops ranged_breakpoint_ops;
f1310107
TJB
9785
9786/* Find the address where the end of the breakpoint range should be
9787 placed, given the SAL of the end of the range. This is so that if
9788 the user provides a line number, the end of the range is set to the
9789 last instruction of the given line. */
9790
9791static CORE_ADDR
9792find_breakpoint_range_end (struct symtab_and_line sal)
9793{
9794 CORE_ADDR end;
9795
9796 /* If the user provided a PC value, use it. Otherwise,
9797 find the address of the end of the given location. */
9798 if (sal.explicit_pc)
9799 end = sal.pc;
9800 else
9801 {
9802 int ret;
9803 CORE_ADDR start;
9804
9805 ret = find_line_pc_range (sal, &start, &end);
9806 if (!ret)
9807 error (_("Could not find location of the end of the range."));
9808
9809 /* find_line_pc_range returns the start of the next line. */
9810 end--;
9811 }
9812
9813 return end;
9814}
9815
9816/* Implement the "break-range" CLI command. */
9817
9818static void
0b39b52e 9819break_range_command (const char *arg, int from_tty)
f1310107 9820{
f2fc3015 9821 const char *arg_start;
f1310107
TJB
9822 struct linespec_result canonical_start, canonical_end;
9823 int bp_count, can_use_bp, length;
9824 CORE_ADDR end;
9825 struct breakpoint *b;
f1310107
TJB
9826
9827 /* We don't support software ranged breakpoints. */
9828 if (target_ranged_break_num_registers () < 0)
9829 error (_("This target does not support hardware ranged breakpoints."));
9830
9831 bp_count = hw_breakpoint_used_count ();
9832 bp_count += target_ranged_break_num_registers ();
9833 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
9834 bp_count, 0);
9835 if (can_use_bp < 0)
9836 error (_("Hardware breakpoints used exceeds limit."));
9837
f8eba3c6 9838 arg = skip_spaces (arg);
f1310107
TJB
9839 if (arg == NULL || arg[0] == '\0')
9840 error(_("No address range specified."));
9841
f8eba3c6 9842 arg_start = arg;
ffc2605c
TT
9843 event_location_up start_location = string_to_event_location (&arg,
9844 current_language);
9845 parse_breakpoint_sals (start_location.get (), &canonical_start);
f1310107
TJB
9846
9847 if (arg[0] != ',')
9848 error (_("Too few arguments."));
6c5b2ebe 9849 else if (canonical_start.lsals.empty ())
f1310107 9850 error (_("Could not find location of the beginning of the range."));
f8eba3c6 9851
6c5b2ebe 9852 const linespec_sals &lsal_start = canonical_start.lsals[0];
f8eba3c6 9853
6c5b2ebe
PA
9854 if (canonical_start.lsals.size () > 1
9855 || lsal_start.sals.size () != 1)
f1310107
TJB
9856 error (_("Cannot create a ranged breakpoint with multiple locations."));
9857
6c5b2ebe 9858 const symtab_and_line &sal_start = lsal_start.sals[0];
81b1e71c 9859 std::string addr_string_start (arg_start, arg - arg_start);
f1310107
TJB
9860
9861 arg++; /* Skip the comma. */
f8eba3c6 9862 arg = skip_spaces (arg);
f1310107
TJB
9863
9864 /* Parse the end location. */
9865
f1310107
TJB
9866 arg_start = arg;
9867
f8eba3c6 9868 /* We call decode_line_full directly here instead of using
f1310107
TJB
9869 parse_breakpoint_sals because we need to specify the start location's
9870 symtab and line as the default symtab and line for the end of the
9871 range. This makes it possible to have ranges like "foo.c:27, +14",
9872 where +14 means 14 lines from the start location. */
ffc2605c
TT
9873 event_location_up end_location = string_to_event_location (&arg,
9874 current_language);
9875 decode_line_full (end_location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
f8eba3c6
TT
9876 sal_start.symtab, sal_start.line,
9877 &canonical_end, NULL, NULL);
9878
6c5b2ebe 9879 if (canonical_end.lsals.empty ())
f1310107 9880 error (_("Could not find location of the end of the range."));
f8eba3c6 9881
6c5b2ebe
PA
9882 const linespec_sals &lsal_end = canonical_end.lsals[0];
9883 if (canonical_end.lsals.size () > 1
9884 || lsal_end.sals.size () != 1)
f1310107
TJB
9885 error (_("Cannot create a ranged breakpoint with multiple locations."));
9886
6c5b2ebe 9887 const symtab_and_line &sal_end = lsal_end.sals[0];
f1310107
TJB
9888
9889 end = find_breakpoint_range_end (sal_end);
9890 if (sal_start.pc > end)
177b42fe 9891 error (_("Invalid address range, end precedes start."));
f1310107
TJB
9892
9893 length = end - sal_start.pc + 1;
9894 if (length < 0)
9895 /* Length overflowed. */
9896 error (_("Address range too large."));
9897 else if (length == 1)
9898 {
9899 /* This range is simple enough to be handled by
9900 the `hbreak' command. */
81b1e71c 9901 hbreak_command (&addr_string_start[0], 1);
f1310107
TJB
9902
9903 return;
9904 }
9905
9906 /* Now set up the breakpoint. */
9907 b = set_raw_breakpoint (get_current_arch (), sal_start,
348d480f 9908 bp_hardware_breakpoint, &ranged_breakpoint_ops);
f1310107
TJB
9909 set_breakpoint_count (breakpoint_count + 1);
9910 b->number = breakpoint_count;
9911 b->disposition = disp_donttouch;
d28cd78a
TT
9912 b->location = std::move (start_location);
9913 b->location_range_end = std::move (end_location);
f1310107
TJB
9914 b->loc->length = length;
9915
f1310107 9916 mention (b);
76727919 9917 gdb::observers::breakpoint_created.notify (b);
44702360 9918 update_global_location_list (UGLL_MAY_INSERT);
f1310107
TJB
9919}
9920
4a64f543
MS
9921/* Return non-zero if EXP is verified as constant. Returned zero
9922 means EXP is variable. Also the constant detection may fail for
9923 some constant expressions and in such case still falsely return
9924 zero. */
2e6e3d9c 9925
65d79d4b
SDJ
9926static int
9927watchpoint_exp_is_const (const struct expression *exp)
9928{
9929 int i = exp->nelts;
9930
9931 while (i > 0)
9932 {
9933 int oplenp, argsp;
9934
9935 /* We are only interested in the descriptor of each element. */
9936 operator_length (exp, i, &oplenp, &argsp);
9937 i -= oplenp;
9938
9939 switch (exp->elts[i].opcode)
9940 {
9941 case BINOP_ADD:
9942 case BINOP_SUB:
9943 case BINOP_MUL:
9944 case BINOP_DIV:
9945 case BINOP_REM:
9946 case BINOP_MOD:
9947 case BINOP_LSH:
9948 case BINOP_RSH:
9949 case BINOP_LOGICAL_AND:
9950 case BINOP_LOGICAL_OR:
9951 case BINOP_BITWISE_AND:
9952 case BINOP_BITWISE_IOR:
9953 case BINOP_BITWISE_XOR:
9954 case BINOP_EQUAL:
9955 case BINOP_NOTEQUAL:
9956 case BINOP_LESS:
9957 case BINOP_GTR:
9958 case BINOP_LEQ:
9959 case BINOP_GEQ:
9960 case BINOP_REPEAT:
9961 case BINOP_COMMA:
9962 case BINOP_EXP:
9963 case BINOP_MIN:
9964 case BINOP_MAX:
9965 case BINOP_INTDIV:
9966 case BINOP_CONCAT:
65d79d4b
SDJ
9967 case TERNOP_COND:
9968 case TERNOP_SLICE:
65d79d4b
SDJ
9969
9970 case OP_LONG:
edd079d9 9971 case OP_FLOAT:
65d79d4b
SDJ
9972 case OP_LAST:
9973 case OP_COMPLEX:
9974 case OP_STRING:
65d79d4b
SDJ
9975 case OP_ARRAY:
9976 case OP_TYPE:
608b4967
TT
9977 case OP_TYPEOF:
9978 case OP_DECLTYPE:
6e72ca20 9979 case OP_TYPEID:
65d79d4b
SDJ
9980 case OP_NAME:
9981 case OP_OBJC_NSSTRING:
9982
9983 case UNOP_NEG:
9984 case UNOP_LOGICAL_NOT:
9985 case UNOP_COMPLEMENT:
9986 case UNOP_ADDR:
9987 case UNOP_HIGH:
aeaa2474 9988 case UNOP_CAST:
9eaf6705
TT
9989
9990 case UNOP_CAST_TYPE:
9991 case UNOP_REINTERPRET_CAST:
9992 case UNOP_DYNAMIC_CAST:
4a64f543
MS
9993 /* Unary, binary and ternary operators: We have to check
9994 their operands. If they are constant, then so is the
9995 result of that operation. For instance, if A and B are
9996 determined to be constants, then so is "A + B".
9997
9998 UNOP_IND is one exception to the rule above, because the
9999 value of *ADDR is not necessarily a constant, even when
10000 ADDR is. */
65d79d4b
SDJ
10001 break;
10002
10003 case OP_VAR_VALUE:
10004 /* Check whether the associated symbol is a constant.
4a64f543 10005
65d79d4b 10006 We use SYMBOL_CLASS rather than TYPE_CONST because it's
4a64f543
MS
10007 possible that a buggy compiler could mark a variable as
10008 constant even when it is not, and TYPE_CONST would return
10009 true in this case, while SYMBOL_CLASS wouldn't.
10010
10011 We also have to check for function symbols because they
10012 are always constant. */
65d79d4b
SDJ
10013 {
10014 struct symbol *s = exp->elts[i + 2].symbol;
10015
10016 if (SYMBOL_CLASS (s) != LOC_BLOCK
10017 && SYMBOL_CLASS (s) != LOC_CONST
10018 && SYMBOL_CLASS (s) != LOC_CONST_BYTES)
10019 return 0;
10020 break;
10021 }
10022
10023 /* The default action is to return 0 because we are using
10024 the optimistic approach here: If we don't know something,
10025 then it is not a constant. */
10026 default:
10027 return 0;
10028 }
10029 }
10030
10031 return 1;
10032}
10033
c1fc2657 10034/* Watchpoint destructor. */
3a5c3e22 10035
c1fc2657 10036watchpoint::~watchpoint ()
3a5c3e22 10037{
c1fc2657
SM
10038 xfree (this->exp_string);
10039 xfree (this->exp_string_reparse);
3a5c3e22
PA
10040}
10041
348d480f
PA
10042/* Implement the "re_set" breakpoint_ops method for watchpoints. */
10043
10044static void
10045re_set_watchpoint (struct breakpoint *b)
10046{
3a5c3e22
PA
10047 struct watchpoint *w = (struct watchpoint *) b;
10048
348d480f
PA
10049 /* Watchpoint can be either on expression using entirely global
10050 variables, or it can be on local variables.
10051
10052 Watchpoints of the first kind are never auto-deleted, and even
10053 persist across program restarts. Since they can use variables
10054 from shared libraries, we need to reparse expression as libraries
10055 are loaded and unloaded.
10056
10057 Watchpoints on local variables can also change meaning as result
10058 of solib event. For example, if a watchpoint uses both a local
10059 and a global variables in expression, it's a local watchpoint,
10060 but unloading of a shared library will make the expression
10061 invalid. This is not a very common use case, but we still
10062 re-evaluate expression, to avoid surprises to the user.
10063
10064 Note that for local watchpoints, we re-evaluate it only if
10065 watchpoints frame id is still valid. If it's not, it means the
10066 watchpoint is out of scope and will be deleted soon. In fact,
10067 I'm not sure we'll ever be called in this case.
10068
10069 If a local watchpoint's frame id is still valid, then
3a5c3e22 10070 w->exp_valid_block is likewise valid, and we can safely use it.
348d480f 10071
3a5c3e22
PA
10072 Don't do anything about disabled watchpoints, since they will be
10073 reevaluated again when enabled. */
10074 update_watchpoint (w, 1 /* reparse */);
348d480f
PA
10075}
10076
77b06cd7
TJB
10077/* Implement the "insert" breakpoint_ops method for hardware watchpoints. */
10078
10079static int
10080insert_watchpoint (struct bp_location *bl)
10081{
3a5c3e22
PA
10082 struct watchpoint *w = (struct watchpoint *) bl->owner;
10083 int length = w->exact ? 1 : bl->length;
e09342b5
TJB
10084
10085 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
4d01a485 10086 w->cond_exp.get ());
77b06cd7
TJB
10087}
10088
10089/* Implement the "remove" breakpoint_ops method for hardware watchpoints. */
10090
10091static int
73971819 10092remove_watchpoint (struct bp_location *bl, enum remove_bp_reason reason)
77b06cd7 10093{
3a5c3e22
PA
10094 struct watchpoint *w = (struct watchpoint *) bl->owner;
10095 int length = w->exact ? 1 : bl->length;
e09342b5
TJB
10096
10097 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
4d01a485 10098 w->cond_exp.get ());
e09342b5
TJB
10099}
10100
e09342b5 10101static int
348d480f 10102breakpoint_hit_watchpoint (const struct bp_location *bl,
bd522513 10103 const address_space *aspace, CORE_ADDR bp_addr,
09ac7c10 10104 const struct target_waitstatus *ws)
e09342b5 10105{
348d480f 10106 struct breakpoint *b = bl->owner;
3a5c3e22 10107 struct watchpoint *w = (struct watchpoint *) b;
77b06cd7 10108
348d480f
PA
10109 /* Continuable hardware watchpoints are treated as non-existent if the
10110 reason we stopped wasn't a hardware watchpoint (we didn't stop on
10111 some data address). Otherwise gdb won't stop on a break instruction
10112 in the code (not from a breakpoint) when a hardware watchpoint has
10113 been defined. Also skip watchpoints which we know did not trigger
10114 (did not match the data address). */
10115 if (is_hardware_watchpoint (b)
3a5c3e22 10116 && w->watchpoint_triggered == watch_triggered_no)
348d480f 10117 return 0;
9c06b0b4 10118
348d480f 10119 return 1;
9c06b0b4
TJB
10120}
10121
348d480f
PA
10122static void
10123check_status_watchpoint (bpstat bs)
9c06b0b4 10124{
348d480f 10125 gdb_assert (is_watchpoint (bs->breakpoint_at));
9c06b0b4 10126
348d480f 10127 bpstat_check_watchpoint (bs);
9c06b0b4
TJB
10128}
10129
10130/* Implement the "resources_needed" breakpoint_ops method for
348d480f 10131 hardware watchpoints. */
9c06b0b4
TJB
10132
10133static int
348d480f 10134resources_needed_watchpoint (const struct bp_location *bl)
9c06b0b4 10135{
3a5c3e22
PA
10136 struct watchpoint *w = (struct watchpoint *) bl->owner;
10137 int length = w->exact? 1 : bl->length;
348d480f
PA
10138
10139 return target_region_ok_for_hw_watchpoint (bl->address, length);
9c06b0b4
TJB
10140}
10141
10142/* Implement the "works_in_software_mode" breakpoint_ops method for
348d480f 10143 hardware watchpoints. */
9c06b0b4
TJB
10144
10145static int
348d480f 10146works_in_software_mode_watchpoint (const struct breakpoint *b)
9c06b0b4 10147{
efa80663
PA
10148 /* Read and access watchpoints only work with hardware support. */
10149 return b->type == bp_watchpoint || b->type == bp_hardware_watchpoint;
9c06b0b4
TJB
10150}
10151
9c06b0b4 10152static enum print_stop_action
348d480f 10153print_it_watchpoint (bpstat bs)
9c06b0b4 10154{
348d480f 10155 struct breakpoint *b;
348d480f 10156 enum print_stop_action result;
3a5c3e22 10157 struct watchpoint *w;
79a45e25 10158 struct ui_out *uiout = current_uiout;
348d480f
PA
10159
10160 gdb_assert (bs->bp_location_at != NULL);
10161
348d480f 10162 b = bs->breakpoint_at;
3a5c3e22 10163 w = (struct watchpoint *) b;
348d480f 10164
f303dbd6
PA
10165 annotate_watchpoint (b->number);
10166 maybe_print_thread_hit_breakpoint (uiout);
10167
d7e74731
PA
10168 string_file stb;
10169
76f9c9cf 10170 gdb::optional<ui_out_emit_tuple> tuple_emitter;
9c06b0b4
TJB
10171 switch (b->type)
10172 {
348d480f 10173 case bp_watchpoint:
9c06b0b4 10174 case bp_hardware_watchpoint:
112e8700
SM
10175 if (uiout->is_mi_like_p ())
10176 uiout->field_string
10177 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
348d480f 10178 mention (b);
76f9c9cf 10179 tuple_emitter.emplace (uiout, "value");
112e8700 10180 uiout->text ("\nOld value = ");
850645cf 10181 watchpoint_value_print (bs->old_val.get (), &stb);
112e8700
SM
10182 uiout->field_stream ("old", stb);
10183 uiout->text ("\nNew value = ");
850645cf 10184 watchpoint_value_print (w->val.get (), &stb);
112e8700
SM
10185 uiout->field_stream ("new", stb);
10186 uiout->text ("\n");
348d480f
PA
10187 /* More than one watchpoint may have been triggered. */
10188 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10189 break;
10190
10191 case bp_read_watchpoint:
112e8700
SM
10192 if (uiout->is_mi_like_p ())
10193 uiout->field_string
10194 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
348d480f 10195 mention (b);
76f9c9cf 10196 tuple_emitter.emplace (uiout, "value");
112e8700 10197 uiout->text ("\nValue = ");
850645cf 10198 watchpoint_value_print (w->val.get (), &stb);
112e8700
SM
10199 uiout->field_stream ("value", stb);
10200 uiout->text ("\n");
348d480f 10201 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10202 break;
10203
10204 case bp_access_watchpoint:
348d480f
PA
10205 if (bs->old_val != NULL)
10206 {
112e8700
SM
10207 if (uiout->is_mi_like_p ())
10208 uiout->field_string
10209 ("reason",
348d480f
PA
10210 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10211 mention (b);
76f9c9cf 10212 tuple_emitter.emplace (uiout, "value");
112e8700 10213 uiout->text ("\nOld value = ");
850645cf 10214 watchpoint_value_print (bs->old_val.get (), &stb);
112e8700
SM
10215 uiout->field_stream ("old", stb);
10216 uiout->text ("\nNew value = ");
348d480f
PA
10217 }
10218 else
10219 {
10220 mention (b);
112e8700
SM
10221 if (uiout->is_mi_like_p ())
10222 uiout->field_string
10223 ("reason",
348d480f 10224 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
76f9c9cf 10225 tuple_emitter.emplace (uiout, "value");
112e8700 10226 uiout->text ("\nValue = ");
348d480f 10227 }
850645cf 10228 watchpoint_value_print (w->val.get (), &stb);
112e8700
SM
10229 uiout->field_stream ("new", stb);
10230 uiout->text ("\n");
348d480f 10231 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10232 break;
10233 default:
348d480f 10234 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10235 }
10236
348d480f
PA
10237 return result;
10238}
10239
10240/* Implement the "print_mention" breakpoint_ops method for hardware
10241 watchpoints. */
10242
10243static void
10244print_mention_watchpoint (struct breakpoint *b)
10245{
3a5c3e22 10246 struct watchpoint *w = (struct watchpoint *) b;
79a45e25 10247 struct ui_out *uiout = current_uiout;
46b9c129 10248 const char *tuple_name;
348d480f
PA
10249
10250 switch (b->type)
10251 {
10252 case bp_watchpoint:
112e8700 10253 uiout->text ("Watchpoint ");
46b9c129 10254 tuple_name = "wpt";
348d480f
PA
10255 break;
10256 case bp_hardware_watchpoint:
112e8700 10257 uiout->text ("Hardware watchpoint ");
46b9c129 10258 tuple_name = "wpt";
348d480f
PA
10259 break;
10260 case bp_read_watchpoint:
112e8700 10261 uiout->text ("Hardware read watchpoint ");
46b9c129 10262 tuple_name = "hw-rwpt";
348d480f
PA
10263 break;
10264 case bp_access_watchpoint:
112e8700 10265 uiout->text ("Hardware access (read/write) watchpoint ");
46b9c129 10266 tuple_name = "hw-awpt";
348d480f
PA
10267 break;
10268 default:
10269 internal_error (__FILE__, __LINE__,
10270 _("Invalid hardware watchpoint type."));
10271 }
10272
46b9c129 10273 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
112e8700
SM
10274 uiout->field_int ("number", b->number);
10275 uiout->text (": ");
10276 uiout->field_string ("exp", w->exp_string);
348d480f
PA
10277}
10278
10279/* Implement the "print_recreate" breakpoint_ops method for
10280 watchpoints. */
10281
10282static void
10283print_recreate_watchpoint (struct breakpoint *b, struct ui_file *fp)
10284{
3a5c3e22
PA
10285 struct watchpoint *w = (struct watchpoint *) b;
10286
348d480f
PA
10287 switch (b->type)
10288 {
10289 case bp_watchpoint:
10290 case bp_hardware_watchpoint:
10291 fprintf_unfiltered (fp, "watch");
10292 break;
10293 case bp_read_watchpoint:
10294 fprintf_unfiltered (fp, "rwatch");
10295 break;
10296 case bp_access_watchpoint:
10297 fprintf_unfiltered (fp, "awatch");
10298 break;
10299 default:
10300 internal_error (__FILE__, __LINE__,
10301 _("Invalid watchpoint type."));
10302 }
10303
3a5c3e22 10304 fprintf_unfiltered (fp, " %s", w->exp_string);
d9b3f62e 10305 print_recreate_thread (b, fp);
348d480f
PA
10306}
10307
427cd150
TT
10308/* Implement the "explains_signal" breakpoint_ops method for
10309 watchpoints. */
10310
47591c29 10311static int
427cd150
TT
10312explains_signal_watchpoint (struct breakpoint *b, enum gdb_signal sig)
10313{
10314 /* A software watchpoint cannot cause a signal other than
10315 GDB_SIGNAL_TRAP. */
10316 if (b->type == bp_watchpoint && sig != GDB_SIGNAL_TRAP)
47591c29 10317 return 0;
427cd150 10318
47591c29 10319 return 1;
427cd150
TT
10320}
10321
348d480f
PA
10322/* The breakpoint_ops structure to be used in hardware watchpoints. */
10323
2060206e 10324static struct breakpoint_ops watchpoint_breakpoint_ops;
348d480f
PA
10325
10326/* Implement the "insert" breakpoint_ops method for
10327 masked hardware watchpoints. */
10328
10329static int
10330insert_masked_watchpoint (struct bp_location *bl)
10331{
3a5c3e22
PA
10332 struct watchpoint *w = (struct watchpoint *) bl->owner;
10333
10334 return target_insert_mask_watchpoint (bl->address, w->hw_wp_mask,
348d480f
PA
10335 bl->watchpoint_type);
10336}
10337
10338/* Implement the "remove" breakpoint_ops method for
10339 masked hardware watchpoints. */
10340
10341static int
73971819 10342remove_masked_watchpoint (struct bp_location *bl, enum remove_bp_reason reason)
348d480f 10343{
3a5c3e22
PA
10344 struct watchpoint *w = (struct watchpoint *) bl->owner;
10345
10346 return target_remove_mask_watchpoint (bl->address, w->hw_wp_mask,
348d480f
PA
10347 bl->watchpoint_type);
10348}
10349
10350/* Implement the "resources_needed" breakpoint_ops method for
10351 masked hardware watchpoints. */
10352
10353static int
10354resources_needed_masked_watchpoint (const struct bp_location *bl)
10355{
3a5c3e22
PA
10356 struct watchpoint *w = (struct watchpoint *) bl->owner;
10357
10358 return target_masked_watch_num_registers (bl->address, w->hw_wp_mask);
348d480f
PA
10359}
10360
10361/* Implement the "works_in_software_mode" breakpoint_ops method for
10362 masked hardware watchpoints. */
10363
10364static int
10365works_in_software_mode_masked_watchpoint (const struct breakpoint *b)
10366{
10367 return 0;
10368}
10369
10370/* Implement the "print_it" breakpoint_ops method for
10371 masked hardware watchpoints. */
10372
10373static enum print_stop_action
10374print_it_masked_watchpoint (bpstat bs)
10375{
10376 struct breakpoint *b = bs->breakpoint_at;
79a45e25 10377 struct ui_out *uiout = current_uiout;
348d480f
PA
10378
10379 /* Masked watchpoints have only one location. */
10380 gdb_assert (b->loc && b->loc->next == NULL);
10381
f303dbd6
PA
10382 annotate_watchpoint (b->number);
10383 maybe_print_thread_hit_breakpoint (uiout);
10384
348d480f
PA
10385 switch (b->type)
10386 {
10387 case bp_hardware_watchpoint:
112e8700
SM
10388 if (uiout->is_mi_like_p ())
10389 uiout->field_string
10390 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
348d480f
PA
10391 break;
10392
10393 case bp_read_watchpoint:
112e8700
SM
10394 if (uiout->is_mi_like_p ())
10395 uiout->field_string
10396 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
348d480f
PA
10397 break;
10398
10399 case bp_access_watchpoint:
112e8700
SM
10400 if (uiout->is_mi_like_p ())
10401 uiout->field_string
10402 ("reason",
348d480f
PA
10403 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10404 break;
10405 default:
10406 internal_error (__FILE__, __LINE__,
10407 _("Invalid hardware watchpoint type."));
10408 }
10409
10410 mention (b);
112e8700 10411 uiout->text (_("\n\
9c06b0b4
TJB
10412Check the underlying instruction at PC for the memory\n\
10413address and value which triggered this watchpoint.\n"));
112e8700 10414 uiout->text ("\n");
9c06b0b4
TJB
10415
10416 /* More than one watchpoint may have been triggered. */
10417 return PRINT_UNKNOWN;
10418}
10419
10420/* Implement the "print_one_detail" breakpoint_ops method for
10421 masked hardware watchpoints. */
10422
10423static void
10424print_one_detail_masked_watchpoint (const struct breakpoint *b,
10425 struct ui_out *uiout)
10426{
3a5c3e22
PA
10427 struct watchpoint *w = (struct watchpoint *) b;
10428
9c06b0b4
TJB
10429 /* Masked watchpoints have only one location. */
10430 gdb_assert (b->loc && b->loc->next == NULL);
10431
112e8700
SM
10432 uiout->text ("\tmask ");
10433 uiout->field_core_addr ("mask", b->loc->gdbarch, w->hw_wp_mask);
10434 uiout->text ("\n");
9c06b0b4
TJB
10435}
10436
10437/* Implement the "print_mention" breakpoint_ops method for
10438 masked hardware watchpoints. */
10439
10440static void
10441print_mention_masked_watchpoint (struct breakpoint *b)
10442{
3a5c3e22 10443 struct watchpoint *w = (struct watchpoint *) b;
79a45e25 10444 struct ui_out *uiout = current_uiout;
46b9c129 10445 const char *tuple_name;
9c06b0b4
TJB
10446
10447 switch (b->type)
10448 {
10449 case bp_hardware_watchpoint:
112e8700 10450 uiout->text ("Masked hardware watchpoint ");
46b9c129 10451 tuple_name = "wpt";
9c06b0b4
TJB
10452 break;
10453 case bp_read_watchpoint:
112e8700 10454 uiout->text ("Masked hardware read watchpoint ");
46b9c129 10455 tuple_name = "hw-rwpt";
9c06b0b4
TJB
10456 break;
10457 case bp_access_watchpoint:
112e8700 10458 uiout->text ("Masked hardware access (read/write) watchpoint ");
46b9c129 10459 tuple_name = "hw-awpt";
9c06b0b4
TJB
10460 break;
10461 default:
10462 internal_error (__FILE__, __LINE__,
10463 _("Invalid hardware watchpoint type."));
10464 }
10465
46b9c129 10466 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
112e8700
SM
10467 uiout->field_int ("number", b->number);
10468 uiout->text (": ");
10469 uiout->field_string ("exp", w->exp_string);
9c06b0b4
TJB
10470}
10471
10472/* Implement the "print_recreate" breakpoint_ops method for
10473 masked hardware watchpoints. */
10474
10475static void
10476print_recreate_masked_watchpoint (struct breakpoint *b, struct ui_file *fp)
10477{
3a5c3e22 10478 struct watchpoint *w = (struct watchpoint *) b;
9c06b0b4
TJB
10479 char tmp[40];
10480
10481 switch (b->type)
10482 {
10483 case bp_hardware_watchpoint:
10484 fprintf_unfiltered (fp, "watch");
10485 break;
10486 case bp_read_watchpoint:
10487 fprintf_unfiltered (fp, "rwatch");
10488 break;
10489 case bp_access_watchpoint:
10490 fprintf_unfiltered (fp, "awatch");
10491 break;
10492 default:
10493 internal_error (__FILE__, __LINE__,
10494 _("Invalid hardware watchpoint type."));
10495 }
10496
3a5c3e22
PA
10497 sprintf_vma (tmp, w->hw_wp_mask);
10498 fprintf_unfiltered (fp, " %s mask 0x%s", w->exp_string, tmp);
d9b3f62e 10499 print_recreate_thread (b, fp);
9c06b0b4
TJB
10500}
10501
10502/* The breakpoint_ops structure to be used in masked hardware watchpoints. */
10503
2060206e 10504static struct breakpoint_ops masked_watchpoint_breakpoint_ops;
9c06b0b4
TJB
10505
10506/* Tell whether the given watchpoint is a masked hardware watchpoint. */
10507
10508static int
10509is_masked_watchpoint (const struct breakpoint *b)
10510{
10511 return b->ops == &masked_watchpoint_breakpoint_ops;
10512}
10513
53a5351d
JM
10514/* accessflag: hw_write: watch write,
10515 hw_read: watch read,
10516 hw_access: watch access (read or write) */
c906108c 10517static void
bbc13ae3 10518watch_command_1 (const char *arg, int accessflag, int from_tty,
84f4c1fe 10519 int just_location, int internal)
c906108c 10520{
c1fc2657 10521 struct breakpoint *scope_breakpoint = NULL;
270140bd 10522 const struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
850645cf 10523 struct value *mark, *result;
bb9d5f81 10524 int saved_bitpos = 0, saved_bitsize = 0;
bbc13ae3
KS
10525 const char *exp_start = NULL;
10526 const char *exp_end = NULL;
10527 const char *tok, *end_tok;
9c06b0b4 10528 int toklen = -1;
bbc13ae3
KS
10529 const char *cond_start = NULL;
10530 const char *cond_end = NULL;
c906108c 10531 enum bptype bp_type;
37e4754d 10532 int thread = -1;
0cf6dd15 10533 int pc = 0;
9c06b0b4
TJB
10534 /* Flag to indicate whether we are going to use masks for
10535 the hardware watchpoint. */
10536 int use_mask = 0;
10537 CORE_ADDR mask = 0;
c906108c 10538
37e4754d
LM
10539 /* Make sure that we actually have parameters to parse. */
10540 if (arg != NULL && arg[0] != '\0')
10541 {
bbc13ae3
KS
10542 const char *value_start;
10543
10544 exp_end = arg + strlen (arg);
37e4754d 10545
9c06b0b4
TJB
10546 /* Look for "parameter value" pairs at the end
10547 of the arguments string. */
bbc13ae3 10548 for (tok = exp_end - 1; tok > arg; tok--)
9c06b0b4
TJB
10549 {
10550 /* Skip whitespace at the end of the argument list. */
10551 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10552 tok--;
10553
10554 /* Find the beginning of the last token.
10555 This is the value of the parameter. */
10556 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10557 tok--;
10558 value_start = tok + 1;
10559
10560 /* Skip whitespace. */
10561 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10562 tok--;
10563
10564 end_tok = tok;
10565
10566 /* Find the beginning of the second to last token.
10567 This is the parameter itself. */
10568 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10569 tok--;
10570 tok++;
10571 toklen = end_tok - tok + 1;
10572
61012eef 10573 if (toklen == 6 && startswith (tok, "thread"))
9c06b0b4 10574 {
5d5658a1 10575 struct thread_info *thr;
9c06b0b4
TJB
10576 /* At this point we've found a "thread" token, which means
10577 the user is trying to set a watchpoint that triggers
10578 only in a specific thread. */
5d5658a1 10579 const char *endp;
37e4754d 10580
9c06b0b4
TJB
10581 if (thread != -1)
10582 error(_("You can specify only one thread."));
37e4754d 10583
9c06b0b4 10584 /* Extract the thread ID from the next token. */
5d5658a1 10585 thr = parse_thread_id (value_start, &endp);
37e4754d 10586
5d5658a1 10587 /* Check if the user provided a valid thread ID. */
9c06b0b4 10588 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
5d5658a1 10589 invalid_thread_id_error (value_start);
9c06b0b4 10590
5d5658a1 10591 thread = thr->global_num;
9c06b0b4 10592 }
61012eef 10593 else if (toklen == 4 && startswith (tok, "mask"))
9c06b0b4
TJB
10594 {
10595 /* We've found a "mask" token, which means the user wants to
10596 create a hardware watchpoint that is going to have the mask
10597 facility. */
10598 struct value *mask_value, *mark;
37e4754d 10599
9c06b0b4
TJB
10600 if (use_mask)
10601 error(_("You can specify only one mask."));
37e4754d 10602
9c06b0b4 10603 use_mask = just_location = 1;
37e4754d 10604
9c06b0b4
TJB
10605 mark = value_mark ();
10606 mask_value = parse_to_comma_and_eval (&value_start);
10607 mask = value_as_address (mask_value);
10608 value_free_to_mark (mark);
10609 }
10610 else
10611 /* We didn't recognize what we found. We should stop here. */
10612 break;
37e4754d 10613
9c06b0b4
TJB
10614 /* Truncate the string and get rid of the "parameter value" pair before
10615 the arguments string is parsed by the parse_exp_1 function. */
bbc13ae3 10616 exp_end = tok;
9c06b0b4 10617 }
37e4754d 10618 }
bbc13ae3
KS
10619 else
10620 exp_end = arg;
37e4754d 10621
bbc13ae3
KS
10622 /* Parse the rest of the arguments. From here on out, everything
10623 is in terms of a newly allocated string instead of the original
10624 ARG. */
aee1fcdf 10625 innermost_block.reset ();
81b1e71c
TT
10626 std::string expression (arg, exp_end - arg);
10627 exp_start = arg = expression.c_str ();
4d01a485 10628 expression_up exp = parse_exp_1 (&arg, 0, 0, 0);
c906108c 10629 exp_end = arg;
fa8a61dc
TT
10630 /* Remove trailing whitespace from the expression before saving it.
10631 This makes the eventual display of the expression string a bit
10632 prettier. */
10633 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
10634 --exp_end;
10635
65d79d4b 10636 /* Checking if the expression is not constant. */
4d01a485 10637 if (watchpoint_exp_is_const (exp.get ()))
65d79d4b
SDJ
10638 {
10639 int len;
10640
10641 len = exp_end - exp_start;
10642 while (len > 0 && isspace (exp_start[len - 1]))
10643 len--;
10644 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
10645 }
10646
aee1fcdf 10647 exp_valid_block = innermost_block.block ();
c906108c 10648 mark = value_mark ();
850645cf
TT
10649 struct value *val_as_value = nullptr;
10650 fetch_subexp_value (exp.get (), &pc, &val_as_value, &result, NULL,
10651 just_location);
06a64a0b 10652
850645cf 10653 if (val_as_value != NULL && just_location)
bb9d5f81 10654 {
850645cf
TT
10655 saved_bitpos = value_bitpos (val_as_value);
10656 saved_bitsize = value_bitsize (val_as_value);
bb9d5f81
PP
10657 }
10658
850645cf 10659 value_ref_ptr val;
06a64a0b
TT
10660 if (just_location)
10661 {
9c06b0b4
TJB
10662 int ret;
10663
06a64a0b 10664 exp_valid_block = NULL;
850645cf 10665 val = release_value (value_addr (result));
06a64a0b 10666 value_free_to_mark (mark);
9c06b0b4
TJB
10667
10668 if (use_mask)
10669 {
850645cf 10670 ret = target_masked_watch_num_registers (value_as_address (val.get ()),
9c06b0b4
TJB
10671 mask);
10672 if (ret == -1)
10673 error (_("This target does not support masked watchpoints."));
10674 else if (ret == -2)
10675 error (_("Invalid mask or memory region."));
10676 }
06a64a0b 10677 }
850645cf
TT
10678 else if (val_as_value != NULL)
10679 val = release_value (val_as_value);
c906108c 10680
f1735a53
TT
10681 tok = skip_spaces (arg);
10682 end_tok = skip_to_space (tok);
c906108c
SS
10683
10684 toklen = end_tok - tok;
10685 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
10686 {
aee1fcdf 10687 innermost_block.reset ();
c906108c 10688 tok = cond_start = end_tok + 1;
4d01a485 10689 parse_exp_1 (&tok, 0, 0, 0);
60e1c644
PA
10690
10691 /* The watchpoint expression may not be local, but the condition
10692 may still be. E.g.: `watch global if local > 0'. */
aee1fcdf 10693 cond_exp_valid_block = innermost_block.block ();
60e1c644 10694
c906108c
SS
10695 cond_end = tok;
10696 }
10697 if (*tok)
8a3fe4f8 10698 error (_("Junk at end of command."));
c906108c 10699
441d7c93
PA
10700 frame_info *wp_frame = block_innermost_frame (exp_valid_block);
10701
10702 /* Save this because create_internal_breakpoint below invalidates
10703 'wp_frame'. */
10704 frame_id watchpoint_frame = get_frame_id (wp_frame);
d983da9c
DJ
10705
10706 /* If the expression is "local", then set up a "watchpoint scope"
10707 breakpoint at the point where we've left the scope of the watchpoint
10708 expression. Create the scope breakpoint before the watchpoint, so
10709 that we will encounter it first in bpstat_stop_status. */
441d7c93 10710 if (exp_valid_block != NULL && wp_frame != NULL)
d983da9c 10711 {
441d7c93
PA
10712 frame_id caller_frame_id = frame_unwind_caller_id (wp_frame);
10713
10714 if (frame_id_p (caller_frame_id))
edb3359d 10715 {
441d7c93
PA
10716 gdbarch *caller_arch = frame_unwind_caller_arch (wp_frame);
10717 CORE_ADDR caller_pc = frame_unwind_caller_pc (wp_frame);
10718
edb3359d 10719 scope_breakpoint
441d7c93 10720 = create_internal_breakpoint (caller_arch, caller_pc,
06edf0c0
PA
10721 bp_watchpoint_scope,
10722 &momentary_breakpoint_ops);
d983da9c 10723
441d7c93
PA
10724 /* create_internal_breakpoint could invalidate WP_FRAME. */
10725 wp_frame = NULL;
10726
edb3359d 10727 scope_breakpoint->enable_state = bp_enabled;
d983da9c 10728
edb3359d
DJ
10729 /* Automatically delete the breakpoint when it hits. */
10730 scope_breakpoint->disposition = disp_del;
d983da9c 10731
edb3359d 10732 /* Only break in the proper frame (help with recursion). */
441d7c93 10733 scope_breakpoint->frame_id = caller_frame_id;
d983da9c 10734
edb3359d 10735 /* Set the address at which we will stop. */
441d7c93
PA
10736 scope_breakpoint->loc->gdbarch = caller_arch;
10737 scope_breakpoint->loc->requested_address = caller_pc;
edb3359d 10738 scope_breakpoint->loc->address
a6d9a66e
UW
10739 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
10740 scope_breakpoint->loc->requested_address,
edb3359d
DJ
10741 scope_breakpoint->type);
10742 }
d983da9c
DJ
10743 }
10744
e8369a73
AB
10745 /* Now set up the breakpoint. We create all watchpoints as hardware
10746 watchpoints here even if hardware watchpoints are turned off, a call
10747 to update_watchpoint later in this function will cause the type to
10748 drop back to bp_watchpoint (software watchpoint) if required. */
10749
10750 if (accessflag == hw_read)
10751 bp_type = bp_read_watchpoint;
10752 else if (accessflag == hw_access)
10753 bp_type = bp_access_watchpoint;
10754 else
10755 bp_type = bp_hardware_watchpoint;
3a5c3e22 10756
b270e6f9 10757 std::unique_ptr<watchpoint> w (new watchpoint ());
c1fc2657 10758
348d480f 10759 if (use_mask)
b270e6f9 10760 init_raw_breakpoint_without_location (w.get (), NULL, bp_type,
3a5c3e22 10761 &masked_watchpoint_breakpoint_ops);
348d480f 10762 else
b270e6f9 10763 init_raw_breakpoint_without_location (w.get (), NULL, bp_type,
3a5c3e22 10764 &watchpoint_breakpoint_ops);
c1fc2657
SM
10765 w->thread = thread;
10766 w->disposition = disp_donttouch;
10767 w->pspace = current_program_space;
b22e99fd 10768 w->exp = std::move (exp);
3a5c3e22
PA
10769 w->exp_valid_block = exp_valid_block;
10770 w->cond_exp_valid_block = cond_exp_valid_block;
06a64a0b
TT
10771 if (just_location)
10772 {
850645cf
TT
10773 struct type *t = value_type (val.get ());
10774 CORE_ADDR addr = value_as_address (val.get ());
06a64a0b 10775
43cc5389
TT
10776 w->exp_string_reparse
10777 = current_language->la_watch_location_expression (t, addr).release ();
06a64a0b 10778
3a5c3e22 10779 w->exp_string = xstrprintf ("-location %.*s",
d63d0675 10780 (int) (exp_end - exp_start), exp_start);
06a64a0b
TT
10781 }
10782 else
3a5c3e22 10783 w->exp_string = savestring (exp_start, exp_end - exp_start);
9c06b0b4
TJB
10784
10785 if (use_mask)
10786 {
3a5c3e22 10787 w->hw_wp_mask = mask;
9c06b0b4
TJB
10788 }
10789 else
10790 {
3a5c3e22 10791 w->val = val;
bb9d5f81
PP
10792 w->val_bitpos = saved_bitpos;
10793 w->val_bitsize = saved_bitsize;
3a5c3e22 10794 w->val_valid = 1;
9c06b0b4 10795 }
77b06cd7 10796
c906108c 10797 if (cond_start)
c1fc2657 10798 w->cond_string = savestring (cond_start, cond_end - cond_start);
c906108c 10799 else
c1fc2657 10800 w->cond_string = 0;
c5aa993b 10801
441d7c93 10802 if (frame_id_p (watchpoint_frame))
f6bc2008 10803 {
441d7c93 10804 w->watchpoint_frame = watchpoint_frame;
3a5c3e22 10805 w->watchpoint_thread = inferior_ptid;
f6bc2008 10806 }
c906108c 10807 else
f6bc2008 10808 {
3a5c3e22
PA
10809 w->watchpoint_frame = null_frame_id;
10810 w->watchpoint_thread = null_ptid;
f6bc2008 10811 }
c906108c 10812
d983da9c 10813 if (scope_breakpoint != NULL)
c906108c 10814 {
d983da9c
DJ
10815 /* The scope breakpoint is related to the watchpoint. We will
10816 need to act on them together. */
c1fc2657 10817 w->related_breakpoint = scope_breakpoint;
b270e6f9 10818 scope_breakpoint->related_breakpoint = w.get ();
c906108c 10819 }
d983da9c 10820
06a64a0b
TT
10821 if (!just_location)
10822 value_free_to_mark (mark);
2d134ed3 10823
b270e6f9
TT
10824 /* Finally update the new watchpoint. This creates the locations
10825 that should be inserted. */
10826 update_watchpoint (w.get (), 1);
a9634178 10827
b270e6f9 10828 install_breakpoint (internal, std::move (w), 1);
c906108c
SS
10829}
10830
e09342b5 10831/* Return count of debug registers needed to watch the given expression.
e09342b5 10832 If the watchpoint cannot be handled in hardware return zero. */
c906108c 10833
c906108c 10834static int
a6535de1 10835can_use_hardware_watchpoint (const std::vector<value_ref_ptr> &vals)
c906108c
SS
10836{
10837 int found_memory_cnt = 0;
10838
10839 /* Did the user specifically forbid us to use hardware watchpoints? */
c5aa993b 10840 if (!can_use_hw_watchpoints)
c906108c 10841 return 0;
c5aa993b 10842
a6535de1
TT
10843 gdb_assert (!vals.empty ());
10844 struct value *head = vals[0].get ();
10845
5c44784c
JM
10846 /* Make sure that the value of the expression depends only upon
10847 memory contents, and values computed from them within GDB. If we
10848 find any register references or function calls, we can't use a
10849 hardware watchpoint.
10850
10851 The idea here is that evaluating an expression generates a series
10852 of values, one holding the value of every subexpression. (The
10853 expression a*b+c has five subexpressions: a, b, a*b, c, and
10854 a*b+c.) GDB's values hold almost enough information to establish
10855 the criteria given above --- they identify memory lvalues,
10856 register lvalues, computed values, etcetera. So we can evaluate
10857 the expression, and then scan the chain of values that leaves
10858 behind to decide whether we can detect any possible change to the
10859 expression's final value using only hardware watchpoints.
10860
10861 However, I don't think that the values returned by inferior
10862 function calls are special in any way. So this function may not
10863 notice that an expression involving an inferior function call
10864 can't be watched with hardware watchpoints. FIXME. */
a6535de1 10865 for (const value_ref_ptr &iter : vals)
c906108c 10866 {
a6535de1
TT
10867 struct value *v = iter.get ();
10868
5c44784c 10869 if (VALUE_LVAL (v) == lval_memory)
c906108c 10870 {
8464be76
DJ
10871 if (v != head && value_lazy (v))
10872 /* A lazy memory lvalue in the chain is one that GDB never
10873 needed to fetch; we either just used its address (e.g.,
10874 `a' in `a.b') or we never needed it at all (e.g., `a'
10875 in `a,b'). This doesn't apply to HEAD; if that is
10876 lazy then it was not readable, but watch it anyway. */
5c44784c 10877 ;
53a5351d 10878 else
5c44784c
JM
10879 {
10880 /* Ahh, memory we actually used! Check if we can cover
10881 it with hardware watchpoints. */
df407dfe 10882 struct type *vtype = check_typedef (value_type (v));
2e70b7b9
MS
10883
10884 /* We only watch structs and arrays if user asked for it
10885 explicitly, never if they just happen to appear in a
10886 middle of some value chain. */
10887 if (v == head
10888 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
10889 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
10890 {
42ae5230 10891 CORE_ADDR vaddr = value_address (v);
e09342b5
TJB
10892 int len;
10893 int num_regs;
10894
a9634178 10895 len = (target_exact_watchpoints
e09342b5
TJB
10896 && is_scalar_type_recursive (vtype))?
10897 1 : TYPE_LENGTH (value_type (v));
2e70b7b9 10898
e09342b5
TJB
10899 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
10900 if (!num_regs)
2e70b7b9
MS
10901 return 0;
10902 else
e09342b5 10903 found_memory_cnt += num_regs;
2e70b7b9 10904 }
5c44784c 10905 }
c5aa993b 10906 }
5086187c
AC
10907 else if (VALUE_LVAL (v) != not_lval
10908 && deprecated_value_modifiable (v) == 0)
38b6c3b3 10909 return 0; /* These are values from the history (e.g., $1). */
5086187c 10910 else if (VALUE_LVAL (v) == lval_register)
38b6c3b3 10911 return 0; /* Cannot watch a register with a HW watchpoint. */
c906108c
SS
10912 }
10913
10914 /* The expression itself looks suitable for using a hardware
10915 watchpoint, but give the target machine a chance to reject it. */
10916 return found_memory_cnt;
10917}
10918
8b93c638 10919void
f2fc3015 10920watch_command_wrapper (const char *arg, int from_tty, int internal)
8b93c638 10921{
84f4c1fe 10922 watch_command_1 (arg, hw_write, from_tty, 0, internal);
06a64a0b
TT
10923}
10924
06a64a0b
TT
10925/* A helper function that looks for the "-location" argument and then
10926 calls watch_command_1. */
10927
10928static void
0b39b52e 10929watch_maybe_just_location (const char *arg, int accessflag, int from_tty)
06a64a0b
TT
10930{
10931 int just_location = 0;
10932
10933 if (arg
10934 && (check_for_argument (&arg, "-location", sizeof ("-location") - 1)
10935 || check_for_argument (&arg, "-l", sizeof ("-l") - 1)))
10936 {
e9cafbcc 10937 arg = skip_spaces (arg);
06a64a0b
TT
10938 just_location = 1;
10939 }
10940
84f4c1fe 10941 watch_command_1 (arg, accessflag, from_tty, just_location, 0);
8b93c638 10942}
8926118c 10943
c5aa993b 10944static void
0b39b52e 10945watch_command (const char *arg, int from_tty)
c906108c 10946{
06a64a0b 10947 watch_maybe_just_location (arg, hw_write, from_tty);
c906108c
SS
10948}
10949
8b93c638 10950void
f2fc3015 10951rwatch_command_wrapper (const char *arg, int from_tty, int internal)
8b93c638 10952{
84f4c1fe 10953 watch_command_1 (arg, hw_read, from_tty, 0, internal);
8b93c638 10954}
8926118c 10955
c5aa993b 10956static void
0b39b52e 10957rwatch_command (const char *arg, int from_tty)
c906108c 10958{
06a64a0b 10959 watch_maybe_just_location (arg, hw_read, from_tty);
c906108c
SS
10960}
10961
8b93c638 10962void
f2fc3015 10963awatch_command_wrapper (const char *arg, int from_tty, int internal)
8b93c638 10964{
84f4c1fe 10965 watch_command_1 (arg, hw_access, from_tty, 0, internal);
8b93c638 10966}
8926118c 10967
c5aa993b 10968static void
0b39b52e 10969awatch_command (const char *arg, int from_tty)
c906108c 10970{
06a64a0b 10971 watch_maybe_just_location (arg, hw_access, from_tty);
c906108c 10972}
c906108c 10973\f
c5aa993b 10974
cfc31633
PA
10975/* Data for the FSM that manages the until(location)/advance commands
10976 in infcmd.c. Here because it uses the mechanisms of
10977 breakpoints. */
c906108c 10978
cfc31633 10979struct until_break_fsm
bfec99b2 10980{
cfc31633
PA
10981 /* The base class. */
10982 struct thread_fsm thread_fsm;
10983
10984 /* The thread that as current when the command was executed. */
10985 int thread;
10986
10987 /* The breakpoint set at the destination location. */
10988 struct breakpoint *location_breakpoint;
10989
10990 /* Breakpoint set at the return address in the caller frame. May be
10991 NULL. */
10992 struct breakpoint *caller_breakpoint;
bfec99b2
PA
10993};
10994
8980e177
PA
10995static void until_break_fsm_clean_up (struct thread_fsm *self,
10996 struct thread_info *thread);
10997static int until_break_fsm_should_stop (struct thread_fsm *self,
10998 struct thread_info *thread);
cfc31633
PA
10999static enum async_reply_reason
11000 until_break_fsm_async_reply_reason (struct thread_fsm *self);
11001
11002/* until_break_fsm's vtable. */
11003
11004static struct thread_fsm_ops until_break_fsm_ops =
11005{
11006 NULL, /* dtor */
11007 until_break_fsm_clean_up,
11008 until_break_fsm_should_stop,
11009 NULL, /* return_value */
11010 until_break_fsm_async_reply_reason,
11011};
11012
11013/* Allocate a new until_break_command_fsm. */
11014
11015static struct until_break_fsm *
8980e177 11016new_until_break_fsm (struct interp *cmd_interp, int thread,
454dafbd
TT
11017 breakpoint_up &&location_breakpoint,
11018 breakpoint_up &&caller_breakpoint)
cfc31633
PA
11019{
11020 struct until_break_fsm *sm;
11021
11022 sm = XCNEW (struct until_break_fsm);
8980e177 11023 thread_fsm_ctor (&sm->thread_fsm, &until_break_fsm_ops, cmd_interp);
cfc31633
PA
11024
11025 sm->thread = thread;
454dafbd
TT
11026 sm->location_breakpoint = location_breakpoint.release ();
11027 sm->caller_breakpoint = caller_breakpoint.release ();
cfc31633
PA
11028
11029 return sm;
11030}
11031
11032/* Implementation of the 'should_stop' FSM method for the
11033 until(location)/advance commands. */
11034
11035static int
8980e177
PA
11036until_break_fsm_should_stop (struct thread_fsm *self,
11037 struct thread_info *tp)
cfc31633
PA
11038{
11039 struct until_break_fsm *sm = (struct until_break_fsm *) self;
cfc31633
PA
11040
11041 if (bpstat_find_breakpoint (tp->control.stop_bpstat,
11042 sm->location_breakpoint) != NULL
11043 || (sm->caller_breakpoint != NULL
11044 && bpstat_find_breakpoint (tp->control.stop_bpstat,
11045 sm->caller_breakpoint) != NULL))
11046 thread_fsm_set_finished (self);
11047
11048 return 1;
11049}
11050
11051/* Implementation of the 'clean_up' FSM method for the
11052 until(location)/advance commands. */
11053
c2c6d25f 11054static void
8980e177
PA
11055until_break_fsm_clean_up (struct thread_fsm *self,
11056 struct thread_info *thread)
43ff13b4 11057{
cfc31633 11058 struct until_break_fsm *sm = (struct until_break_fsm *) self;
bfec99b2 11059
cfc31633
PA
11060 /* Clean up our temporary breakpoints. */
11061 if (sm->location_breakpoint != NULL)
11062 {
11063 delete_breakpoint (sm->location_breakpoint);
11064 sm->location_breakpoint = NULL;
11065 }
11066 if (sm->caller_breakpoint != NULL)
11067 {
11068 delete_breakpoint (sm->caller_breakpoint);
11069 sm->caller_breakpoint = NULL;
11070 }
11071 delete_longjmp_breakpoint (sm->thread);
11072}
11073
11074/* Implementation of the 'async_reply_reason' FSM method for the
11075 until(location)/advance commands. */
11076
11077static enum async_reply_reason
11078until_break_fsm_async_reply_reason (struct thread_fsm *self)
11079{
11080 return EXEC_ASYNC_LOCATION_REACHED;
43ff13b4
JM
11081}
11082
c906108c 11083void
f2fc3015 11084until_break_command (const char *arg, int from_tty, int anywhere)
c906108c 11085{
8556afb4
PA
11086 struct frame_info *frame;
11087 struct gdbarch *frame_gdbarch;
11088 struct frame_id stack_frame_id;
11089 struct frame_id caller_frame_id;
ffc2605c 11090 struct cleanup *old_chain;
186c406b
TT
11091 int thread;
11092 struct thread_info *tp;
cfc31633 11093 struct until_break_fsm *sm;
c906108c 11094
70509625 11095 clear_proceed_status (0);
c906108c
SS
11096
11097 /* Set a breakpoint where the user wants it and at return from
4a64f543 11098 this function. */
c5aa993b 11099
ffc2605c 11100 event_location_up location = string_to_event_location (&arg, current_language);
f00aae0f 11101
6c5b2ebe
PA
11102 std::vector<symtab_and_line> sals
11103 = (last_displayed_sal_is_valid ()
11104 ? decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
11105 get_last_displayed_symtab (),
11106 get_last_displayed_line ())
11107 : decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE,
11108 NULL, (struct symtab *) NULL, 0));
c5aa993b 11109
6c5b2ebe 11110 if (sals.size () != 1)
8a3fe4f8 11111 error (_("Couldn't get information on specified line."));
c5aa993b 11112
6c5b2ebe 11113 symtab_and_line &sal = sals[0];
c5aa993b 11114
c906108c 11115 if (*arg)
8a3fe4f8 11116 error (_("Junk at end of arguments."));
c5aa993b 11117
c906108c 11118 resolve_sal_pc (&sal);
c5aa993b 11119
186c406b 11120 tp = inferior_thread ();
5d5658a1 11121 thread = tp->global_num;
186c406b 11122
883bc8d1
PA
11123 old_chain = make_cleanup (null_cleanup, NULL);
11124
8556afb4
PA
11125 /* Note linespec handling above invalidates the frame chain.
11126 Installing a breakpoint also invalidates the frame chain (as it
11127 may need to switch threads), so do any frame handling before
11128 that. */
11129
11130 frame = get_selected_frame (NULL);
11131 frame_gdbarch = get_frame_arch (frame);
11132 stack_frame_id = get_stack_frame_id (frame);
11133 caller_frame_id = frame_unwind_caller_id (frame);
883bc8d1 11134
ae66c1fc
EZ
11135 /* Keep within the current frame, or in frames called by the current
11136 one. */
edb3359d 11137
454dafbd 11138 breakpoint_up caller_breakpoint;
883bc8d1 11139 if (frame_id_p (caller_frame_id))
c906108c 11140 {
883bc8d1 11141 struct symtab_and_line sal2;
cfc31633 11142 struct gdbarch *caller_gdbarch;
883bc8d1
PA
11143
11144 sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
11145 sal2.pc = frame_unwind_caller_pc (frame);
cfc31633
PA
11146 caller_gdbarch = frame_unwind_caller_arch (frame);
11147 caller_breakpoint = set_momentary_breakpoint (caller_gdbarch,
11148 sal2,
11149 caller_frame_id,
11150 bp_until);
186c406b 11151
883bc8d1 11152 set_longjmp_breakpoint (tp, caller_frame_id);
186c406b 11153 make_cleanup (delete_longjmp_breakpoint_cleanup, &thread);
c906108c 11154 }
c5aa993b 11155
c70a6932
JK
11156 /* set_momentary_breakpoint could invalidate FRAME. */
11157 frame = NULL;
11158
454dafbd 11159 breakpoint_up location_breakpoint;
883bc8d1
PA
11160 if (anywhere)
11161 /* If the user told us to continue until a specified location,
11162 we don't specify a frame at which we need to stop. */
cfc31633
PA
11163 location_breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11164 null_frame_id, bp_until);
883bc8d1
PA
11165 else
11166 /* Otherwise, specify the selected frame, because we want to stop
11167 only at the very same frame. */
cfc31633
PA
11168 location_breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11169 stack_frame_id, bp_until);
883bc8d1 11170
8980e177 11171 sm = new_until_break_fsm (command_interp (), tp->global_num,
454dafbd
TT
11172 std::move (location_breakpoint),
11173 std::move (caller_breakpoint));
cfc31633 11174 tp->thread_fsm = &sm->thread_fsm;
f107f563 11175
cfc31633 11176 discard_cleanups (old_chain);
f107f563 11177
cfc31633 11178 proceed (-1, GDB_SIGNAL_DEFAULT);
c906108c 11179}
ae66c1fc 11180
c906108c
SS
11181/* This function attempts to parse an optional "if <cond>" clause
11182 from the arg string. If one is not found, it returns NULL.
c5aa993b 11183
c906108c
SS
11184 Else, it returns a pointer to the condition string. (It does not
11185 attempt to evaluate the string against a particular block.) And,
11186 it updates arg to point to the first character following the parsed
4a64f543 11187 if clause in the arg string. */
53a5351d 11188
63160a43
PA
11189const char *
11190ep_parse_optional_if_clause (const char **arg)
c906108c 11191{
63160a43 11192 const char *cond_string;
c5aa993b
JM
11193
11194 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
c906108c 11195 return NULL;
c5aa993b 11196
4a64f543 11197 /* Skip the "if" keyword. */
c906108c 11198 (*arg) += 2;
c5aa993b 11199
c906108c 11200 /* Skip any extra leading whitespace, and record the start of the
4a64f543 11201 condition string. */
f1735a53 11202 *arg = skip_spaces (*arg);
c906108c 11203 cond_string = *arg;
c5aa993b 11204
4a64f543
MS
11205 /* Assume that the condition occupies the remainder of the arg
11206 string. */
c906108c 11207 (*arg) += strlen (cond_string);
c5aa993b 11208
c906108c
SS
11209 return cond_string;
11210}
c5aa993b 11211
c906108c
SS
11212/* Commands to deal with catching events, such as signals, exceptions,
11213 process start/exit, etc. */
c5aa993b
JM
11214
11215typedef enum
11216{
44feb3ce
TT
11217 catch_fork_temporary, catch_vfork_temporary,
11218 catch_fork_permanent, catch_vfork_permanent
c5aa993b
JM
11219}
11220catch_fork_kind;
11221
c906108c 11222static void
eb4c3f4a 11223catch_fork_command_1 (const char *arg, int from_tty,
cc59ec59 11224 struct cmd_list_element *command)
c906108c 11225{
a6d9a66e 11226 struct gdbarch *gdbarch = get_current_arch ();
63160a43 11227 const char *cond_string = NULL;
44feb3ce
TT
11228 catch_fork_kind fork_kind;
11229 int tempflag;
11230
11231 fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
11232 tempflag = (fork_kind == catch_fork_temporary
11233 || fork_kind == catch_vfork_temporary);
c5aa993b 11234
44feb3ce
TT
11235 if (!arg)
11236 arg = "";
f1735a53 11237 arg = skip_spaces (arg);
c5aa993b 11238
c906108c 11239 /* The allowed syntax is:
c5aa993b
JM
11240 catch [v]fork
11241 catch [v]fork if <cond>
11242
4a64f543 11243 First, check if there's an if clause. */
c906108c 11244 cond_string = ep_parse_optional_if_clause (&arg);
c5aa993b 11245
c906108c 11246 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 11247 error (_("Junk at end of arguments."));
c5aa993b 11248
c906108c 11249 /* If this target supports it, create a fork or vfork catchpoint
4a64f543 11250 and enable reporting of such events. */
c5aa993b
JM
11251 switch (fork_kind)
11252 {
44feb3ce
TT
11253 case catch_fork_temporary:
11254 case catch_fork_permanent:
a6d9a66e 11255 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
ce78b96d 11256 &catch_fork_breakpoint_ops);
c906108c 11257 break;
44feb3ce
TT
11258 case catch_vfork_temporary:
11259 case catch_vfork_permanent:
a6d9a66e 11260 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
ce78b96d 11261 &catch_vfork_breakpoint_ops);
c906108c 11262 break;
c5aa993b 11263 default:
8a3fe4f8 11264 error (_("unsupported or unknown fork kind; cannot catch it"));
c906108c 11265 break;
c5aa993b 11266 }
c906108c
SS
11267}
11268
11269static void
eb4c3f4a 11270catch_exec_command_1 (const char *arg, int from_tty,
cc59ec59 11271 struct cmd_list_element *command)
c906108c 11272{
a6d9a66e 11273 struct gdbarch *gdbarch = get_current_arch ();
44feb3ce 11274 int tempflag;
63160a43 11275 const char *cond_string = NULL;
c906108c 11276
44feb3ce
TT
11277 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
11278
11279 if (!arg)
11280 arg = "";
f1735a53 11281 arg = skip_spaces (arg);
c906108c
SS
11282
11283 /* The allowed syntax is:
c5aa993b
JM
11284 catch exec
11285 catch exec if <cond>
c906108c 11286
4a64f543 11287 First, check if there's an if clause. */
c906108c
SS
11288 cond_string = ep_parse_optional_if_clause (&arg);
11289
11290 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 11291 error (_("Junk at end of arguments."));
c906108c 11292
b270e6f9
TT
11293 std::unique_ptr<exec_catchpoint> c (new exec_catchpoint ());
11294 init_catchpoint (c.get (), gdbarch, tempflag, cond_string,
b4d90040
PA
11295 &catch_exec_breakpoint_ops);
11296 c->exec_pathname = NULL;
11297
b270e6f9 11298 install_breakpoint (0, std::move (c), 1);
c906108c 11299}
c5aa993b 11300
9ac4176b 11301void
28010a5d
PA
11302init_ada_exception_breakpoint (struct breakpoint *b,
11303 struct gdbarch *gdbarch,
11304 struct symtab_and_line sal,
f2fc3015 11305 const char *addr_string,
c0a91b2b 11306 const struct breakpoint_ops *ops,
28010a5d 11307 int tempflag,
349774ef 11308 int enabled,
28010a5d 11309 int from_tty)
f7f9143b 11310{
f7f9143b
JB
11311 if (from_tty)
11312 {
5af949e3
UW
11313 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
11314 if (!loc_gdbarch)
11315 loc_gdbarch = gdbarch;
11316
6c95b8df
PA
11317 describe_other_breakpoints (loc_gdbarch,
11318 sal.pspace, sal.pc, sal.section, -1);
f7f9143b
JB
11319 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
11320 version for exception catchpoints, because two catchpoints
11321 used for different exception names will use the same address.
11322 In this case, a "breakpoint ... also set at..." warning is
4a64f543 11323 unproductive. Besides, the warning phrasing is also a bit
e5dd4106 11324 inappropriate, we should use the word catchpoint, and tell
f7f9143b
JB
11325 the user what type of catchpoint it is. The above is good
11326 enough for now, though. */
11327 }
11328
28010a5d 11329 init_raw_breakpoint (b, gdbarch, sal, bp_breakpoint, ops);
f7f9143b 11330
349774ef 11331 b->enable_state = enabled ? bp_enabled : bp_disabled;
f7f9143b 11332 b->disposition = tempflag ? disp_del : disp_donttouch;
d28cd78a
TT
11333 b->location = string_to_event_location (&addr_string,
11334 language_def (language_ada));
f7f9143b 11335 b->language = language_ada;
f7f9143b
JB
11336}
11337
c906108c 11338static void
981a3fb3 11339catch_command (const char *arg, int from_tty)
c906108c 11340{
44feb3ce 11341 error (_("Catch requires an event name."));
c906108c
SS
11342}
11343\f
11344
11345static void
981a3fb3 11346tcatch_command (const char *arg, int from_tty)
c906108c 11347{
44feb3ce 11348 error (_("Catch requires an event name."));
c906108c
SS
11349}
11350
81b1e71c 11351/* Compare two breakpoints and return a strcmp-like result. */
8a2c437b
TT
11352
11353static int
81b1e71c 11354compare_breakpoints (const breakpoint *a, const breakpoint *b)
8a2c437b 11355{
81b1e71c
TT
11356 uintptr_t ua = (uintptr_t) a;
11357 uintptr_t ub = (uintptr_t) b;
8a2c437b 11358
81b1e71c 11359 if (a->number < b->number)
8a2c437b 11360 return -1;
81b1e71c 11361 else if (a->number > b->number)
8a2c437b
TT
11362 return 1;
11363
11364 /* Now sort by address, in case we see, e..g, two breakpoints with
11365 the number 0. */
11366 if (ua < ub)
11367 return -1;
94b0e70d 11368 return ua > ub ? 1 : 0;
8a2c437b
TT
11369}
11370
80f8a6eb 11371/* Delete breakpoints by address or line. */
c906108c
SS
11372
11373static void
0b39b52e 11374clear_command (const char *arg, int from_tty)
c906108c 11375{
81b1e71c 11376 struct breakpoint *b;
c906108c 11377 int default_match;
c906108c 11378
6c5b2ebe
PA
11379 std::vector<symtab_and_line> decoded_sals;
11380 symtab_and_line last_sal;
11381 gdb::array_view<symtab_and_line> sals;
c906108c
SS
11382 if (arg)
11383 {
6c5b2ebe
PA
11384 decoded_sals
11385 = decode_line_with_current_source (arg,
11386 (DECODE_LINE_FUNFIRSTLINE
11387 | DECODE_LINE_LIST_MODE));
c906108c 11388 default_match = 0;
6c5b2ebe 11389 sals = decoded_sals;
c906108c
SS
11390 }
11391 else
11392 {
1bfeeb0f
JL
11393 /* Set sal's line, symtab, pc, and pspace to the values
11394 corresponding to the last call to print_frame_info. If the
11395 codepoint is not valid, this will set all the fields to 0. */
51abb421 11396 last_sal = get_last_displayed_sal ();
6c5b2ebe 11397 if (last_sal.symtab == 0)
8a3fe4f8 11398 error (_("No source file specified."));
c906108c 11399
c906108c 11400 default_match = 1;
6c5b2ebe 11401 sals = last_sal;
c906108c
SS
11402 }
11403
4a64f543
MS
11404 /* We don't call resolve_sal_pc here. That's not as bad as it
11405 seems, because all existing breakpoints typically have both
11406 file/line and pc set. So, if clear is given file/line, we can
11407 match this to existing breakpoint without obtaining pc at all.
ed0616c6
VP
11408
11409 We only support clearing given the address explicitly
11410 present in breakpoint table. Say, we've set breakpoint
4a64f543 11411 at file:line. There were several PC values for that file:line,
ed0616c6 11412 due to optimization, all in one block.
4a64f543
MS
11413
11414 We've picked one PC value. If "clear" is issued with another
ed0616c6
VP
11415 PC corresponding to the same file:line, the breakpoint won't
11416 be cleared. We probably can still clear the breakpoint, but
11417 since the other PC value is never presented to user, user
11418 can only find it by guessing, and it does not seem important
11419 to support that. */
11420
4a64f543
MS
11421 /* For each line spec given, delete bps which correspond to it. Do
11422 it in two passes, solely to preserve the current behavior that
11423 from_tty is forced true if we delete more than one
11424 breakpoint. */
c906108c 11425
81b1e71c 11426 std::vector<struct breakpoint *> found;
6c5b2ebe 11427 for (const auto &sal : sals)
c906108c 11428 {
05cba821
JK
11429 const char *sal_fullname;
11430
c906108c 11431 /* If exact pc given, clear bpts at that pc.
c5aa993b
JM
11432 If line given (pc == 0), clear all bpts on specified line.
11433 If defaulting, clear all bpts on default line
c906108c 11434 or at default pc.
c5aa993b
JM
11435
11436 defaulting sal.pc != 0 tests to do
11437
11438 0 1 pc
11439 1 1 pc _and_ line
11440 0 0 line
11441 1 0 <can't happen> */
c906108c 11442
05cba821
JK
11443 sal_fullname = (sal.symtab == NULL
11444 ? NULL : symtab_to_fullname (sal.symtab));
c906108c 11445
4a64f543 11446 /* Find all matching breakpoints and add them to 'found'. */
d6e956e5 11447 ALL_BREAKPOINTS (b)
c5aa993b 11448 {
0d381245 11449 int match = 0;
4a64f543 11450 /* Are we going to delete b? */
cc60f2e3 11451 if (b->type != bp_none && !is_watchpoint (b))
0d381245
VP
11452 {
11453 struct bp_location *loc = b->loc;
11454 for (; loc; loc = loc->next)
11455 {
f8eba3c6
TT
11456 /* If the user specified file:line, don't allow a PC
11457 match. This matches historical gdb behavior. */
11458 int pc_match = (!sal.explicit_line
11459 && sal.pc
11460 && (loc->pspace == sal.pspace)
11461 && (loc->address == sal.pc)
11462 && (!section_is_overlay (loc->section)
11463 || loc->section == sal.section));
4aac40c8
TT
11464 int line_match = 0;
11465
11466 if ((default_match || sal.explicit_line)
2f202fde 11467 && loc->symtab != NULL
05cba821 11468 && sal_fullname != NULL
4aac40c8 11469 && sal.pspace == loc->pspace
05cba821
JK
11470 && loc->line_number == sal.line
11471 && filename_cmp (symtab_to_fullname (loc->symtab),
11472 sal_fullname) == 0)
11473 line_match = 1;
4aac40c8 11474
0d381245
VP
11475 if (pc_match || line_match)
11476 {
11477 match = 1;
11478 break;
11479 }
11480 }
11481 }
11482
11483 if (match)
81b1e71c 11484 found.push_back (b);
c906108c 11485 }
80f8a6eb 11486 }
8a2c437b 11487
80f8a6eb 11488 /* Now go thru the 'found' chain and delete them. */
81b1e71c 11489 if (found.empty ())
80f8a6eb
MS
11490 {
11491 if (arg)
8a3fe4f8 11492 error (_("No breakpoint at %s."), arg);
80f8a6eb 11493 else
8a3fe4f8 11494 error (_("No breakpoint at this line."));
80f8a6eb 11495 }
c906108c 11496
8a2c437b 11497 /* Remove duplicates from the vec. */
81b1e71c
TT
11498 std::sort (found.begin (), found.end (),
11499 [] (const breakpoint *a, const breakpoint *b)
11500 {
11501 return compare_breakpoints (a, b) < 0;
11502 });
11503 found.erase (std::unique (found.begin (), found.end (),
11504 [] (const breakpoint *a, const breakpoint *b)
11505 {
11506 return compare_breakpoints (a, b) == 0;
11507 }),
11508 found.end ());
8a2c437b 11509
81b1e71c 11510 if (found.size () > 1)
4a64f543 11511 from_tty = 1; /* Always report if deleted more than one. */
80f8a6eb 11512 if (from_tty)
a3f17187 11513 {
81b1e71c 11514 if (found.size () == 1)
a3f17187
AC
11515 printf_unfiltered (_("Deleted breakpoint "));
11516 else
11517 printf_unfiltered (_("Deleted breakpoints "));
11518 }
d6e956e5 11519
81b1e71c 11520 for (breakpoint *iter : found)
80f8a6eb 11521 {
c5aa993b 11522 if (from_tty)
81b1e71c
TT
11523 printf_unfiltered ("%d ", iter->number);
11524 delete_breakpoint (iter);
c906108c 11525 }
80f8a6eb
MS
11526 if (from_tty)
11527 putchar_unfiltered ('\n');
c906108c
SS
11528}
11529\f
11530/* Delete breakpoint in BS if they are `delete' breakpoints and
11531 all breakpoints that are marked for deletion, whether hit or not.
11532 This is called after any breakpoint is hit, or after errors. */
11533
11534void
fba45db2 11535breakpoint_auto_delete (bpstat bs)
c906108c 11536{
35df4500 11537 struct breakpoint *b, *b_tmp;
c906108c
SS
11538
11539 for (; bs; bs = bs->next)
f431efe5
PA
11540 if (bs->breakpoint_at
11541 && bs->breakpoint_at->disposition == disp_del
c906108c 11542 && bs->stop)
f431efe5 11543 delete_breakpoint (bs->breakpoint_at);
c906108c 11544
35df4500 11545 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 11546 {
b5de0fa7 11547 if (b->disposition == disp_del_at_next_stop)
c5aa993b
JM
11548 delete_breakpoint (b);
11549 }
c906108c
SS
11550}
11551
4a64f543
MS
11552/* A comparison function for bp_location AP and BP being interfaced to
11553 qsort. Sort elements primarily by their ADDRESS (no matter what
11554 does breakpoint_address_is_meaningful say for its OWNER),
1a853c52 11555 secondarily by ordering first permanent elements and
4a64f543 11556 terciarily just ensuring the array is sorted stable way despite
e5dd4106 11557 qsort being an unstable algorithm. */
876fa593
JK
11558
11559static int
f5336ca5 11560bp_locations_compare (const void *ap, const void *bp)
876fa593 11561{
9a3c8263
SM
11562 const struct bp_location *a = *(const struct bp_location **) ap;
11563 const struct bp_location *b = *(const struct bp_location **) bp;
876fa593
JK
11564
11565 if (a->address != b->address)
11566 return (a->address > b->address) - (a->address < b->address);
11567
dea2aa5f
LM
11568 /* Sort locations at the same address by their pspace number, keeping
11569 locations of the same inferior (in a multi-inferior environment)
11570 grouped. */
11571
11572 if (a->pspace->num != b->pspace->num)
11573 return ((a->pspace->num > b->pspace->num)
11574 - (a->pspace->num < b->pspace->num));
11575
876fa593 11576 /* Sort permanent breakpoints first. */
1a853c52
PA
11577 if (a->permanent != b->permanent)
11578 return (a->permanent < b->permanent) - (a->permanent > b->permanent);
876fa593 11579
c56a97f9
JK
11580 /* Make the internal GDB representation stable across GDB runs
11581 where A and B memory inside GDB can differ. Breakpoint locations of
11582 the same type at the same address can be sorted in arbitrary order. */
876fa593
JK
11583
11584 if (a->owner->number != b->owner->number)
c56a97f9
JK
11585 return ((a->owner->number > b->owner->number)
11586 - (a->owner->number < b->owner->number));
876fa593
JK
11587
11588 return (a > b) - (a < b);
11589}
11590
f5336ca5
PA
11591/* Set bp_locations_placed_address_before_address_max and
11592 bp_locations_shadow_len_after_address_max according to the current
11593 content of the bp_locations array. */
f7545552
TT
11594
11595static void
f5336ca5 11596bp_locations_target_extensions_update (void)
f7545552 11597{
876fa593
JK
11598 struct bp_location *bl, **blp_tmp;
11599
f5336ca5
PA
11600 bp_locations_placed_address_before_address_max = 0;
11601 bp_locations_shadow_len_after_address_max = 0;
876fa593
JK
11602
11603 ALL_BP_LOCATIONS (bl, blp_tmp)
11604 {
11605 CORE_ADDR start, end, addr;
11606
11607 if (!bp_location_has_shadow (bl))
11608 continue;
11609
11610 start = bl->target_info.placed_address;
11611 end = start + bl->target_info.shadow_len;
11612
11613 gdb_assert (bl->address >= start);
11614 addr = bl->address - start;
f5336ca5
PA
11615 if (addr > bp_locations_placed_address_before_address_max)
11616 bp_locations_placed_address_before_address_max = addr;
876fa593
JK
11617
11618 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
11619
11620 gdb_assert (bl->address < end);
11621 addr = end - bl->address;
f5336ca5
PA
11622 if (addr > bp_locations_shadow_len_after_address_max)
11623 bp_locations_shadow_len_after_address_max = addr;
876fa593 11624 }
f7545552
TT
11625}
11626
1e4d1764
YQ
11627/* Download tracepoint locations if they haven't been. */
11628
11629static void
11630download_tracepoint_locations (void)
11631{
7ed2c994 11632 struct breakpoint *b;
dd2e65cc 11633 enum tribool can_download_tracepoint = TRIBOOL_UNKNOWN;
1e4d1764 11634
5ed8105e 11635 scoped_restore_current_pspace_and_thread restore_pspace_thread;
1e4d1764 11636
7ed2c994 11637 ALL_TRACEPOINTS (b)
1e4d1764 11638 {
7ed2c994 11639 struct bp_location *bl;
1e4d1764 11640 struct tracepoint *t;
f2a8bc8a 11641 int bp_location_downloaded = 0;
1e4d1764 11642
7ed2c994 11643 if ((b->type == bp_fast_tracepoint
1e4d1764
YQ
11644 ? !may_insert_fast_tracepoints
11645 : !may_insert_tracepoints))
11646 continue;
11647
dd2e65cc
YQ
11648 if (can_download_tracepoint == TRIBOOL_UNKNOWN)
11649 {
11650 if (target_can_download_tracepoint ())
11651 can_download_tracepoint = TRIBOOL_TRUE;
11652 else
11653 can_download_tracepoint = TRIBOOL_FALSE;
11654 }
11655
11656 if (can_download_tracepoint == TRIBOOL_FALSE)
11657 break;
11658
7ed2c994
YQ
11659 for (bl = b->loc; bl; bl = bl->next)
11660 {
11661 /* In tracepoint, locations are _never_ duplicated, so
11662 should_be_inserted is equivalent to
11663 unduplicated_should_be_inserted. */
11664 if (!should_be_inserted (bl) || bl->inserted)
11665 continue;
1e4d1764 11666
7ed2c994 11667 switch_to_program_space_and_thread (bl->pspace);
1e4d1764 11668
7ed2c994 11669 target_download_tracepoint (bl);
1e4d1764 11670
7ed2c994 11671 bl->inserted = 1;
f2a8bc8a 11672 bp_location_downloaded = 1;
7ed2c994
YQ
11673 }
11674 t = (struct tracepoint *) b;
11675 t->number_on_target = b->number;
f2a8bc8a 11676 if (bp_location_downloaded)
76727919 11677 gdb::observers::breakpoint_modified.notify (b);
1e4d1764 11678 }
1e4d1764
YQ
11679}
11680
934709f0
PW
11681/* Swap the insertion/duplication state between two locations. */
11682
11683static void
11684swap_insertion (struct bp_location *left, struct bp_location *right)
11685{
11686 const int left_inserted = left->inserted;
11687 const int left_duplicate = left->duplicate;
b775012e 11688 const int left_needs_update = left->needs_update;
934709f0
PW
11689 const struct bp_target_info left_target_info = left->target_info;
11690
1e4d1764
YQ
11691 /* Locations of tracepoints can never be duplicated. */
11692 if (is_tracepoint (left->owner))
11693 gdb_assert (!left->duplicate);
11694 if (is_tracepoint (right->owner))
11695 gdb_assert (!right->duplicate);
11696
934709f0
PW
11697 left->inserted = right->inserted;
11698 left->duplicate = right->duplicate;
b775012e 11699 left->needs_update = right->needs_update;
934709f0
PW
11700 left->target_info = right->target_info;
11701 right->inserted = left_inserted;
11702 right->duplicate = left_duplicate;
b775012e 11703 right->needs_update = left_needs_update;
934709f0
PW
11704 right->target_info = left_target_info;
11705}
11706
b775012e
LM
11707/* Force the re-insertion of the locations at ADDRESS. This is called
11708 once a new/deleted/modified duplicate location is found and we are evaluating
11709 conditions on the target's side. Such conditions need to be updated on
11710 the target. */
11711
11712static void
11713force_breakpoint_reinsertion (struct bp_location *bl)
11714{
11715 struct bp_location **locp = NULL, **loc2p;
11716 struct bp_location *loc;
11717 CORE_ADDR address = 0;
11718 int pspace_num;
11719
11720 address = bl->address;
11721 pspace_num = bl->pspace->num;
11722
11723 /* This is only meaningful if the target is
11724 evaluating conditions and if the user has
11725 opted for condition evaluation on the target's
11726 side. */
11727 if (gdb_evaluates_breakpoint_condition_p ()
11728 || !target_supports_evaluation_of_breakpoint_conditions ())
11729 return;
11730
11731 /* Flag all breakpoint locations with this address and
11732 the same program space as the location
11733 as "its condition has changed". We need to
11734 update the conditions on the target's side. */
11735 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, address)
11736 {
11737 loc = *loc2p;
11738
11739 if (!is_breakpoint (loc->owner)
11740 || pspace_num != loc->pspace->num)
11741 continue;
11742
11743 /* Flag the location appropriately. We use a different state to
11744 let everyone know that we already updated the set of locations
11745 with addr bl->address and program space bl->pspace. This is so
11746 we don't have to keep calling these functions just to mark locations
11747 that have already been marked. */
11748 loc->condition_changed = condition_updated;
11749
11750 /* Free the agent expression bytecode as well. We will compute
11751 it later on. */
833177a4 11752 loc->cond_bytecode.reset ();
b775012e
LM
11753 }
11754}
44702360
PA
11755/* Called whether new breakpoints are created, or existing breakpoints
11756 deleted, to update the global location list and recompute which
11757 locations are duplicate of which.
b775012e 11758
04086b45
PA
11759 The INSERT_MODE flag determines whether locations may not, may, or
11760 shall be inserted now. See 'enum ugll_insert_mode' for more
11761 info. */
b60e7edf 11762
0d381245 11763static void
44702360 11764update_global_location_list (enum ugll_insert_mode insert_mode)
0d381245 11765{
74960c60 11766 struct breakpoint *b;
876fa593 11767 struct bp_location **locp, *loc;
b775012e
LM
11768 /* Last breakpoint location address that was marked for update. */
11769 CORE_ADDR last_addr = 0;
11770 /* Last breakpoint location program space that was marked for update. */
11771 int last_pspace_num = -1;
f7545552 11772
2d134ed3
PA
11773 /* Used in the duplicates detection below. When iterating over all
11774 bp_locations, points to the first bp_location of a given address.
11775 Breakpoints and watchpoints of different types are never
11776 duplicates of each other. Keep one pointer for each type of
11777 breakpoint/watchpoint, so we only need to loop over all locations
11778 once. */
11779 struct bp_location *bp_loc_first; /* breakpoint */
11780 struct bp_location *wp_loc_first; /* hardware watchpoint */
11781 struct bp_location *awp_loc_first; /* access watchpoint */
11782 struct bp_location *rwp_loc_first; /* read watchpoint */
876fa593 11783
f5336ca5
PA
11784 /* Saved former bp_locations array which we compare against the newly
11785 built bp_locations from the current state of ALL_BREAKPOINTS. */
81b1e71c 11786 struct bp_location **old_locp;
f5336ca5 11787 unsigned old_locations_count;
81b1e71c 11788 gdb::unique_xmalloc_ptr<struct bp_location *> old_locations (bp_locations);
876fa593 11789
f5336ca5
PA
11790 old_locations_count = bp_locations_count;
11791 bp_locations = NULL;
11792 bp_locations_count = 0;
0d381245 11793
74960c60 11794 ALL_BREAKPOINTS (b)
876fa593 11795 for (loc = b->loc; loc; loc = loc->next)
f5336ca5 11796 bp_locations_count++;
876fa593 11797
f5336ca5
PA
11798 bp_locations = XNEWVEC (struct bp_location *, bp_locations_count);
11799 locp = bp_locations;
876fa593
JK
11800 ALL_BREAKPOINTS (b)
11801 for (loc = b->loc; loc; loc = loc->next)
11802 *locp++ = loc;
f5336ca5
PA
11803 qsort (bp_locations, bp_locations_count, sizeof (*bp_locations),
11804 bp_locations_compare);
876fa593 11805
f5336ca5 11806 bp_locations_target_extensions_update ();
74960c60 11807
4a64f543
MS
11808 /* Identify bp_location instances that are no longer present in the
11809 new list, and therefore should be freed. Note that it's not
11810 necessary that those locations should be removed from inferior --
11811 if there's another location at the same address (previously
11812 marked as duplicate), we don't need to remove/insert the
11813 location.
876fa593 11814
4a64f543
MS
11815 LOCP is kept in sync with OLD_LOCP, each pointing to the current
11816 and former bp_location array state respectively. */
876fa593 11817
f5336ca5 11818 locp = bp_locations;
81b1e71c
TT
11819 for (old_locp = old_locations.get ();
11820 old_locp < old_locations.get () + old_locations_count;
876fa593 11821 old_locp++)
74960c60 11822 {
876fa593 11823 struct bp_location *old_loc = *old_locp;
c7d46a38 11824 struct bp_location **loc2p;
876fa593 11825
e5dd4106 11826 /* Tells if 'old_loc' is found among the new locations. If
4a64f543 11827 not, we have to free it. */
c7d46a38 11828 int found_object = 0;
20874c92
VP
11829 /* Tells if the location should remain inserted in the target. */
11830 int keep_in_target = 0;
11831 int removed = 0;
876fa593 11832
4a64f543
MS
11833 /* Skip LOCP entries which will definitely never be needed.
11834 Stop either at or being the one matching OLD_LOC. */
f5336ca5 11835 while (locp < bp_locations + bp_locations_count
c7d46a38 11836 && (*locp)->address < old_loc->address)
876fa593 11837 locp++;
c7d46a38
PA
11838
11839 for (loc2p = locp;
f5336ca5 11840 (loc2p < bp_locations + bp_locations_count
c7d46a38
PA
11841 && (*loc2p)->address == old_loc->address);
11842 loc2p++)
11843 {
b775012e
LM
11844 /* Check if this is a new/duplicated location or a duplicated
11845 location that had its condition modified. If so, we want to send
11846 its condition to the target if evaluation of conditions is taking
11847 place there. */
11848 if ((*loc2p)->condition_changed == condition_modified
11849 && (last_addr != old_loc->address
11850 || last_pspace_num != old_loc->pspace->num))
c7d46a38 11851 {
b775012e
LM
11852 force_breakpoint_reinsertion (*loc2p);
11853 last_pspace_num = old_loc->pspace->num;
c7d46a38 11854 }
b775012e
LM
11855
11856 if (*loc2p == old_loc)
11857 found_object = 1;
c7d46a38 11858 }
74960c60 11859
b775012e
LM
11860 /* We have already handled this address, update it so that we don't
11861 have to go through updates again. */
11862 last_addr = old_loc->address;
11863
11864 /* Target-side condition evaluation: Handle deleted locations. */
11865 if (!found_object)
11866 force_breakpoint_reinsertion (old_loc);
11867
4a64f543
MS
11868 /* If this location is no longer present, and inserted, look if
11869 there's maybe a new location at the same address. If so,
11870 mark that one inserted, and don't remove this one. This is
11871 needed so that we don't have a time window where a breakpoint
11872 at certain location is not inserted. */
74960c60 11873
876fa593 11874 if (old_loc->inserted)
0d381245 11875 {
4a64f543
MS
11876 /* If the location is inserted now, we might have to remove
11877 it. */
74960c60 11878
876fa593 11879 if (found_object && should_be_inserted (old_loc))
74960c60 11880 {
4a64f543
MS
11881 /* The location is still present in the location list,
11882 and still should be inserted. Don't do anything. */
20874c92 11883 keep_in_target = 1;
74960c60
VP
11884 }
11885 else
11886 {
b775012e
LM
11887 /* This location still exists, but it won't be kept in the
11888 target since it may have been disabled. We proceed to
11889 remove its target-side condition. */
11890
4a64f543
MS
11891 /* The location is either no longer present, or got
11892 disabled. See if there's another location at the
11893 same address, in which case we don't need to remove
11894 this one from the target. */
876fa593 11895
2bdf28a0 11896 /* OLD_LOC comes from existing struct breakpoint. */
876fa593
JK
11897 if (breakpoint_address_is_meaningful (old_loc->owner))
11898 {
876fa593 11899 for (loc2p = locp;
f5336ca5 11900 (loc2p < bp_locations + bp_locations_count
c7d46a38 11901 && (*loc2p)->address == old_loc->address);
876fa593
JK
11902 loc2p++)
11903 {
11904 struct bp_location *loc2 = *loc2p;
11905
2d134ed3 11906 if (breakpoint_locations_match (loc2, old_loc))
c7d46a38 11907 {
85d721b8
PA
11908 /* Read watchpoint locations are switched to
11909 access watchpoints, if the former are not
11910 supported, but the latter are. */
11911 if (is_hardware_watchpoint (old_loc->owner))
11912 {
11913 gdb_assert (is_hardware_watchpoint (loc2->owner));
11914 loc2->watchpoint_type = old_loc->watchpoint_type;
11915 }
11916
934709f0
PW
11917 /* loc2 is a duplicated location. We need to check
11918 if it should be inserted in case it will be
11919 unduplicated. */
11920 if (loc2 != old_loc
11921 && unduplicated_should_be_inserted (loc2))
c7d46a38 11922 {
934709f0 11923 swap_insertion (old_loc, loc2);
c7d46a38
PA
11924 keep_in_target = 1;
11925 break;
11926 }
876fa593
JK
11927 }
11928 }
11929 }
74960c60
VP
11930 }
11931
20874c92
VP
11932 if (!keep_in_target)
11933 {
834c0d03 11934 if (remove_breakpoint (old_loc))
20874c92 11935 {
4a64f543
MS
11936 /* This is just about all we can do. We could keep
11937 this location on the global list, and try to
11938 remove it next time, but there's no particular
11939 reason why we will succeed next time.
20874c92 11940
4a64f543
MS
11941 Note that at this point, old_loc->owner is still
11942 valid, as delete_breakpoint frees the breakpoint
11943 only after calling us. */
3e43a32a
MS
11944 printf_filtered (_("warning: Error removing "
11945 "breakpoint %d\n"),
876fa593 11946 old_loc->owner->number);
20874c92
VP
11947 }
11948 removed = 1;
11949 }
0d381245 11950 }
74960c60
VP
11951
11952 if (!found_object)
1c5cfe86 11953 {
fbea99ea 11954 if (removed && target_is_non_stop_p ()
1cf4d951 11955 && need_moribund_for_location_type (old_loc))
20874c92 11956 {
db82e815
PA
11957 /* This location was removed from the target. In
11958 non-stop mode, a race condition is possible where
11959 we've removed a breakpoint, but stop events for that
11960 breakpoint are already queued and will arrive later.
11961 We apply an heuristic to be able to distinguish such
11962 SIGTRAPs from other random SIGTRAPs: we keep this
11963 breakpoint location for a bit, and will retire it
11964 after we see some number of events. The theory here
11965 is that reporting of events should, "on the average",
11966 be fair, so after a while we'll see events from all
11967 threads that have anything of interest, and no longer
11968 need to keep this breakpoint location around. We
11969 don't hold locations forever so to reduce chances of
11970 mistaking a non-breakpoint SIGTRAP for a breakpoint
11971 SIGTRAP.
11972
11973 The heuristic failing can be disastrous on
11974 decr_pc_after_break targets.
11975
11976 On decr_pc_after_break targets, like e.g., x86-linux,
11977 if we fail to recognize a late breakpoint SIGTRAP,
11978 because events_till_retirement has reached 0 too
11979 soon, we'll fail to do the PC adjustment, and report
11980 a random SIGTRAP to the user. When the user resumes
11981 the inferior, it will most likely immediately crash
2dec564e 11982 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
db82e815
PA
11983 corrupted, because of being resumed e.g., in the
11984 middle of a multi-byte instruction, or skipped a
11985 one-byte instruction. This was actually seen happen
11986 on native x86-linux, and should be less rare on
11987 targets that do not support new thread events, like
11988 remote, due to the heuristic depending on
11989 thread_count.
11990
11991 Mistaking a random SIGTRAP for a breakpoint trap
11992 causes similar symptoms (PC adjustment applied when
11993 it shouldn't), but then again, playing with SIGTRAPs
11994 behind the debugger's back is asking for trouble.
11995
11996 Since hardware watchpoint traps are always
11997 distinguishable from other traps, so we don't need to
11998 apply keep hardware watchpoint moribund locations
11999 around. We simply always ignore hardware watchpoint
12000 traps we can no longer explain. */
12001
876fa593
JK
12002 old_loc->events_till_retirement = 3 * (thread_count () + 1);
12003 old_loc->owner = NULL;
20874c92 12004
1123588c 12005 moribund_locations.push_back (old_loc);
1c5cfe86
PA
12006 }
12007 else
f431efe5
PA
12008 {
12009 old_loc->owner = NULL;
12010 decref_bp_location (&old_loc);
12011 }
20874c92 12012 }
74960c60 12013 }
1c5cfe86 12014
348d480f
PA
12015 /* Rescan breakpoints at the same address and section, marking the
12016 first one as "first" and any others as "duplicates". This is so
12017 that the bpt instruction is only inserted once. If we have a
12018 permanent breakpoint at the same place as BPT, make that one the
12019 official one, and the rest as duplicates. Permanent breakpoints
12020 are sorted first for the same address.
12021
12022 Do the same for hardware watchpoints, but also considering the
12023 watchpoint's type (regular/access/read) and length. */
12024
12025 bp_loc_first = NULL;
12026 wp_loc_first = NULL;
12027 awp_loc_first = NULL;
12028 rwp_loc_first = NULL;
12029 ALL_BP_LOCATIONS (loc, locp)
12030 {
12031 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
12032 non-NULL. */
348d480f 12033 struct bp_location **loc_first_p;
d3fbdd86 12034 b = loc->owner;
348d480f 12035
6f380991 12036 if (!unduplicated_should_be_inserted (loc)
348d480f 12037 || !breakpoint_address_is_meaningful (b)
1e4d1764
YQ
12038 /* Don't detect duplicate for tracepoint locations because they are
12039 never duplicated. See the comments in field `duplicate' of
12040 `struct bp_location'. */
348d480f 12041 || is_tracepoint (b))
b775012e
LM
12042 {
12043 /* Clear the condition modification flag. */
12044 loc->condition_changed = condition_unchanged;
12045 continue;
12046 }
348d480f 12047
348d480f
PA
12048 if (b->type == bp_hardware_watchpoint)
12049 loc_first_p = &wp_loc_first;
12050 else if (b->type == bp_read_watchpoint)
12051 loc_first_p = &rwp_loc_first;
12052 else if (b->type == bp_access_watchpoint)
12053 loc_first_p = &awp_loc_first;
12054 else
12055 loc_first_p = &bp_loc_first;
12056
12057 if (*loc_first_p == NULL
12058 || (overlay_debugging && loc->section != (*loc_first_p)->section)
12059 || !breakpoint_locations_match (loc, *loc_first_p))
12060 {
12061 *loc_first_p = loc;
12062 loc->duplicate = 0;
b775012e
LM
12063
12064 if (is_breakpoint (loc->owner) && loc->condition_changed)
12065 {
12066 loc->needs_update = 1;
12067 /* Clear the condition modification flag. */
12068 loc->condition_changed = condition_unchanged;
12069 }
348d480f
PA
12070 continue;
12071 }
12072
934709f0
PW
12073
12074 /* This and the above ensure the invariant that the first location
12075 is not duplicated, and is the inserted one.
12076 All following are marked as duplicated, and are not inserted. */
12077 if (loc->inserted)
12078 swap_insertion (loc, *loc_first_p);
348d480f
PA
12079 loc->duplicate = 1;
12080
b775012e
LM
12081 /* Clear the condition modification flag. */
12082 loc->condition_changed = condition_unchanged;
348d480f
PA
12083 }
12084
a25a5a45 12085 if (insert_mode == UGLL_INSERT || breakpoints_should_be_inserted_now ())
b775012e 12086 {
04086b45 12087 if (insert_mode != UGLL_DONT_INSERT)
b775012e
LM
12088 insert_breakpoint_locations ();
12089 else
12090 {
44702360
PA
12091 /* Even though the caller told us to not insert new
12092 locations, we may still need to update conditions on the
12093 target's side of breakpoints that were already inserted
12094 if the target is evaluating breakpoint conditions. We
b775012e
LM
12095 only update conditions for locations that are marked
12096 "needs_update". */
12097 update_inserted_breakpoint_locations ();
12098 }
12099 }
348d480f 12100
04086b45 12101 if (insert_mode != UGLL_DONT_INSERT)
1e4d1764 12102 download_tracepoint_locations ();
348d480f
PA
12103}
12104
12105void
12106breakpoint_retire_moribund (void)
12107{
1123588c
TT
12108 for (int ix = 0; ix < moribund_locations.size (); ++ix)
12109 {
12110 struct bp_location *loc = moribund_locations[ix];
12111 if (--(loc->events_till_retirement) == 0)
12112 {
12113 decref_bp_location (&loc);
12114 unordered_remove (moribund_locations, ix);
12115 --ix;
12116 }
12117 }
348d480f
PA
12118}
12119
12120static void
44702360 12121update_global_location_list_nothrow (enum ugll_insert_mode insert_mode)
348d480f 12122{
348d480f 12123
492d29ea
PA
12124 TRY
12125 {
12126 update_global_location_list (insert_mode);
12127 }
12128 CATCH (e, RETURN_MASK_ERROR)
12129 {
12130 }
12131 END_CATCH
348d480f
PA
12132}
12133
12134/* Clear BKP from a BPS. */
12135
12136static void
12137bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
12138{
12139 bpstat bs;
12140
12141 for (bs = bps; bs; bs = bs->next)
12142 if (bs->breakpoint_at == bpt)
12143 {
12144 bs->breakpoint_at = NULL;
12145 bs->old_val = NULL;
12146 /* bs->commands will be freed later. */
12147 }
12148}
12149
12150/* Callback for iterate_over_threads. */
12151static int
12152bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
12153{
9a3c8263 12154 struct breakpoint *bpt = (struct breakpoint *) data;
348d480f
PA
12155
12156 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
12157 return 0;
12158}
12159
12160/* Helper for breakpoint and tracepoint breakpoint_ops->mention
12161 callbacks. */
12162
12163static void
12164say_where (struct breakpoint *b)
12165{
12166 struct value_print_options opts;
12167
12168 get_user_print_options (&opts);
12169
12170 /* i18n: cagney/2005-02-11: Below needs to be merged into a
12171 single string. */
12172 if (b->loc == NULL)
12173 {
f00aae0f
KS
12174 /* For pending locations, the output differs slightly based
12175 on b->extra_string. If this is non-NULL, it contains either
12176 a condition or dprintf arguments. */
12177 if (b->extra_string == NULL)
12178 {
12179 printf_filtered (_(" (%s) pending."),
d28cd78a 12180 event_location_to_string (b->location.get ()));
f00aae0f
KS
12181 }
12182 else if (b->type == bp_dprintf)
12183 {
12184 printf_filtered (_(" (%s,%s) pending."),
d28cd78a 12185 event_location_to_string (b->location.get ()),
f00aae0f
KS
12186 b->extra_string);
12187 }
12188 else
12189 {
12190 printf_filtered (_(" (%s %s) pending."),
d28cd78a 12191 event_location_to_string (b->location.get ()),
f00aae0f
KS
12192 b->extra_string);
12193 }
348d480f
PA
12194 }
12195 else
12196 {
2f202fde 12197 if (opts.addressprint || b->loc->symtab == NULL)
348d480f
PA
12198 {
12199 printf_filtered (" at ");
12200 fputs_filtered (paddress (b->loc->gdbarch, b->loc->address),
12201 gdb_stdout);
12202 }
2f202fde 12203 if (b->loc->symtab != NULL)
f8eba3c6
TT
12204 {
12205 /* If there is a single location, we can print the location
12206 more nicely. */
12207 if (b->loc->next == NULL)
12208 printf_filtered (": file %s, line %d.",
05cba821
JK
12209 symtab_to_filename_for_display (b->loc->symtab),
12210 b->loc->line_number);
f8eba3c6
TT
12211 else
12212 /* This is not ideal, but each location may have a
12213 different file name, and this at least reflects the
12214 real situation somewhat. */
f00aae0f 12215 printf_filtered (": %s.",
d28cd78a 12216 event_location_to_string (b->location.get ()));
f8eba3c6 12217 }
348d480f
PA
12218
12219 if (b->loc->next)
12220 {
12221 struct bp_location *loc = b->loc;
12222 int n = 0;
12223 for (; loc; loc = loc->next)
12224 ++n;
12225 printf_filtered (" (%d locations)", n);
12226 }
12227 }
12228}
12229
348d480f
PA
12230/* Default bp_location_ops methods. */
12231
12232static void
12233bp_location_dtor (struct bp_location *self)
12234{
348d480f
PA
12235 xfree (self->function_name);
12236}
12237
12238static const struct bp_location_ops bp_location_ops =
12239{
12240 bp_location_dtor
12241};
12242
c1fc2657 12243/* Destructor for the breakpoint base class. */
348d480f 12244
c1fc2657 12245breakpoint::~breakpoint ()
348d480f 12246{
c1fc2657
SM
12247 xfree (this->cond_string);
12248 xfree (this->extra_string);
12249 xfree (this->filter);
348d480f
PA
12250}
12251
2060206e
PA
12252static struct bp_location *
12253base_breakpoint_allocate_location (struct breakpoint *self)
348d480f 12254{
5625a286 12255 return new bp_location (&bp_location_ops, self);
348d480f
PA
12256}
12257
2060206e
PA
12258static void
12259base_breakpoint_re_set (struct breakpoint *b)
12260{
12261 /* Nothing to re-set. */
12262}
12263
12264#define internal_error_pure_virtual_called() \
12265 gdb_assert_not_reached ("pure virtual function called")
12266
12267static int
12268base_breakpoint_insert_location (struct bp_location *bl)
12269{
12270 internal_error_pure_virtual_called ();
12271}
12272
12273static int
73971819
PA
12274base_breakpoint_remove_location (struct bp_location *bl,
12275 enum remove_bp_reason reason)
2060206e
PA
12276{
12277 internal_error_pure_virtual_called ();
12278}
12279
12280static int
12281base_breakpoint_breakpoint_hit (const struct bp_location *bl,
bd522513 12282 const address_space *aspace,
09ac7c10
TT
12283 CORE_ADDR bp_addr,
12284 const struct target_waitstatus *ws)
2060206e
PA
12285{
12286 internal_error_pure_virtual_called ();
12287}
12288
12289static void
12290base_breakpoint_check_status (bpstat bs)
12291{
12292 /* Always stop. */
12293}
12294
12295/* A "works_in_software_mode" breakpoint_ops method that just internal
12296 errors. */
12297
12298static int
12299base_breakpoint_works_in_software_mode (const struct breakpoint *b)
12300{
12301 internal_error_pure_virtual_called ();
12302}
12303
12304/* A "resources_needed" breakpoint_ops method that just internal
12305 errors. */
12306
12307static int
12308base_breakpoint_resources_needed (const struct bp_location *bl)
12309{
12310 internal_error_pure_virtual_called ();
12311}
12312
12313static enum print_stop_action
12314base_breakpoint_print_it (bpstat bs)
12315{
12316 internal_error_pure_virtual_called ();
12317}
12318
12319static void
12320base_breakpoint_print_one_detail (const struct breakpoint *self,
12321 struct ui_out *uiout)
12322{
12323 /* nothing */
12324}
12325
12326static void
12327base_breakpoint_print_mention (struct breakpoint *b)
12328{
12329 internal_error_pure_virtual_called ();
12330}
12331
12332static void
12333base_breakpoint_print_recreate (struct breakpoint *b, struct ui_file *fp)
12334{
12335 internal_error_pure_virtual_called ();
12336}
12337
983af33b 12338static void
f00aae0f
KS
12339base_breakpoint_create_sals_from_location
12340 (const struct event_location *location,
12341 struct linespec_result *canonical,
12342 enum bptype type_wanted)
983af33b
SDJ
12343{
12344 internal_error_pure_virtual_called ();
12345}
12346
12347static void
12348base_breakpoint_create_breakpoints_sal (struct gdbarch *gdbarch,
12349 struct linespec_result *c,
e1e01040
PA
12350 gdb::unique_xmalloc_ptr<char> cond_string,
12351 gdb::unique_xmalloc_ptr<char> extra_string,
983af33b
SDJ
12352 enum bptype type_wanted,
12353 enum bpdisp disposition,
12354 int thread,
12355 int task, int ignore_count,
12356 const struct breakpoint_ops *o,
12357 int from_tty, int enabled,
44f238bb 12358 int internal, unsigned flags)
983af33b
SDJ
12359{
12360 internal_error_pure_virtual_called ();
12361}
12362
6c5b2ebe 12363static std::vector<symtab_and_line>
f00aae0f
KS
12364base_breakpoint_decode_location (struct breakpoint *b,
12365 const struct event_location *location,
6c5b2ebe 12366 struct program_space *search_pspace)
983af33b
SDJ
12367{
12368 internal_error_pure_virtual_called ();
12369}
12370
ab04a2af
TT
12371/* The default 'explains_signal' method. */
12372
47591c29 12373static int
427cd150 12374base_breakpoint_explains_signal (struct breakpoint *b, enum gdb_signal sig)
ab04a2af 12375{
47591c29 12376 return 1;
ab04a2af
TT
12377}
12378
9d6e6e84
HZ
12379/* The default "after_condition_true" method. */
12380
12381static void
12382base_breakpoint_after_condition_true (struct bpstats *bs)
12383{
12384 /* Nothing to do. */
12385}
12386
ab04a2af 12387struct breakpoint_ops base_breakpoint_ops =
2060206e 12388{
2060206e
PA
12389 base_breakpoint_allocate_location,
12390 base_breakpoint_re_set,
12391 base_breakpoint_insert_location,
12392 base_breakpoint_remove_location,
12393 base_breakpoint_breakpoint_hit,
12394 base_breakpoint_check_status,
12395 base_breakpoint_resources_needed,
12396 base_breakpoint_works_in_software_mode,
12397 base_breakpoint_print_it,
12398 NULL,
12399 base_breakpoint_print_one_detail,
12400 base_breakpoint_print_mention,
983af33b 12401 base_breakpoint_print_recreate,
5f700d83 12402 base_breakpoint_create_sals_from_location,
983af33b 12403 base_breakpoint_create_breakpoints_sal,
5f700d83 12404 base_breakpoint_decode_location,
9d6e6e84
HZ
12405 base_breakpoint_explains_signal,
12406 base_breakpoint_after_condition_true,
2060206e
PA
12407};
12408
12409/* Default breakpoint_ops methods. */
12410
12411static void
348d480f
PA
12412bkpt_re_set (struct breakpoint *b)
12413{
06edf0c0 12414 /* FIXME: is this still reachable? */
9ef9e6a6 12415 if (breakpoint_event_location_empty_p (b))
06edf0c0 12416 {
f00aae0f 12417 /* Anything without a location can't be re-set. */
348d480f 12418 delete_breakpoint (b);
06edf0c0 12419 return;
348d480f 12420 }
06edf0c0
PA
12421
12422 breakpoint_re_set_default (b);
348d480f
PA
12423}
12424
2060206e 12425static int
348d480f
PA
12426bkpt_insert_location (struct bp_location *bl)
12427{
cd6c3b4f
YQ
12428 CORE_ADDR addr = bl->target_info.reqstd_address;
12429
579c6ad9 12430 bl->target_info.kind = breakpoint_kind (bl, &addr);
cd6c3b4f
YQ
12431 bl->target_info.placed_address = addr;
12432
348d480f 12433 if (bl->loc_type == bp_loc_hardware_breakpoint)
7c16b83e 12434 return target_insert_hw_breakpoint (bl->gdbarch, &bl->target_info);
348d480f 12435 else
7c16b83e 12436 return target_insert_breakpoint (bl->gdbarch, &bl->target_info);
348d480f
PA
12437}
12438
2060206e 12439static int
73971819 12440bkpt_remove_location (struct bp_location *bl, enum remove_bp_reason reason)
348d480f
PA
12441{
12442 if (bl->loc_type == bp_loc_hardware_breakpoint)
12443 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
12444 else
73971819 12445 return target_remove_breakpoint (bl->gdbarch, &bl->target_info, reason);
348d480f
PA
12446}
12447
2060206e 12448static int
348d480f 12449bkpt_breakpoint_hit (const struct bp_location *bl,
bd522513 12450 const address_space *aspace, CORE_ADDR bp_addr,
09ac7c10 12451 const struct target_waitstatus *ws)
348d480f 12452{
09ac7c10 12453 if (ws->kind != TARGET_WAITKIND_STOPPED
a493e3e2 12454 || ws->value.sig != GDB_SIGNAL_TRAP)
09ac7c10
TT
12455 return 0;
12456
348d480f
PA
12457 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
12458 aspace, bp_addr))
12459 return 0;
12460
12461 if (overlay_debugging /* unmapped overlay section */
12462 && section_is_overlay (bl->section)
12463 && !section_is_mapped (bl->section))
12464 return 0;
12465
12466 return 1;
12467}
12468
cd1608cc
PA
12469static int
12470dprintf_breakpoint_hit (const struct bp_location *bl,
bd522513 12471 const address_space *aspace, CORE_ADDR bp_addr,
cd1608cc
PA
12472 const struct target_waitstatus *ws)
12473{
12474 if (dprintf_style == dprintf_style_agent
12475 && target_can_run_breakpoint_commands ())
12476 {
12477 /* An agent-style dprintf never causes a stop. If we see a trap
12478 for this address it must be for a breakpoint that happens to
12479 be set at the same address. */
12480 return 0;
12481 }
12482
12483 return bkpt_breakpoint_hit (bl, aspace, bp_addr, ws);
12484}
12485
2060206e 12486static int
348d480f
PA
12487bkpt_resources_needed (const struct bp_location *bl)
12488{
12489 gdb_assert (bl->owner->type == bp_hardware_breakpoint);
12490
12491 return 1;
12492}
12493
2060206e 12494static enum print_stop_action
348d480f
PA
12495bkpt_print_it (bpstat bs)
12496{
348d480f
PA
12497 struct breakpoint *b;
12498 const struct bp_location *bl;
001c8c33 12499 int bp_temp;
79a45e25 12500 struct ui_out *uiout = current_uiout;
348d480f
PA
12501
12502 gdb_assert (bs->bp_location_at != NULL);
12503
12504 bl = bs->bp_location_at;
12505 b = bs->breakpoint_at;
12506
001c8c33
PA
12507 bp_temp = b->disposition == disp_del;
12508 if (bl->address != bl->requested_address)
12509 breakpoint_adjustment_warning (bl->requested_address,
12510 bl->address,
12511 b->number, 1);
12512 annotate_breakpoint (b->number);
f303dbd6
PA
12513 maybe_print_thread_hit_breakpoint (uiout);
12514
001c8c33 12515 if (bp_temp)
112e8700 12516 uiout->text ("Temporary breakpoint ");
001c8c33 12517 else
112e8700
SM
12518 uiout->text ("Breakpoint ");
12519 if (uiout->is_mi_like_p ())
348d480f 12520 {
112e8700 12521 uiout->field_string ("reason",
001c8c33 12522 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
112e8700 12523 uiout->field_string ("disp", bpdisp_text (b->disposition));
06edf0c0 12524 }
112e8700
SM
12525 uiout->field_int ("bkptno", b->number);
12526 uiout->text (", ");
06edf0c0 12527
001c8c33 12528 return PRINT_SRC_AND_LOC;
06edf0c0
PA
12529}
12530
2060206e 12531static void
06edf0c0
PA
12532bkpt_print_mention (struct breakpoint *b)
12533{
112e8700 12534 if (current_uiout->is_mi_like_p ())
06edf0c0
PA
12535 return;
12536
12537 switch (b->type)
12538 {
12539 case bp_breakpoint:
12540 case bp_gnu_ifunc_resolver:
12541 if (b->disposition == disp_del)
12542 printf_filtered (_("Temporary breakpoint"));
12543 else
12544 printf_filtered (_("Breakpoint"));
12545 printf_filtered (_(" %d"), b->number);
12546 if (b->type == bp_gnu_ifunc_resolver)
12547 printf_filtered (_(" at gnu-indirect-function resolver"));
12548 break;
12549 case bp_hardware_breakpoint:
12550 printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
12551 break;
e7e0cddf
SS
12552 case bp_dprintf:
12553 printf_filtered (_("Dprintf %d"), b->number);
12554 break;
06edf0c0
PA
12555 }
12556
12557 say_where (b);
12558}
12559
2060206e 12560static void
06edf0c0
PA
12561bkpt_print_recreate (struct breakpoint *tp, struct ui_file *fp)
12562{
12563 if (tp->type == bp_breakpoint && tp->disposition == disp_del)
12564 fprintf_unfiltered (fp, "tbreak");
12565 else if (tp->type == bp_breakpoint)
12566 fprintf_unfiltered (fp, "break");
12567 else if (tp->type == bp_hardware_breakpoint
12568 && tp->disposition == disp_del)
12569 fprintf_unfiltered (fp, "thbreak");
12570 else if (tp->type == bp_hardware_breakpoint)
12571 fprintf_unfiltered (fp, "hbreak");
12572 else
12573 internal_error (__FILE__, __LINE__,
12574 _("unhandled breakpoint type %d"), (int) tp->type);
12575
f00aae0f 12576 fprintf_unfiltered (fp, " %s",
d28cd78a 12577 event_location_to_string (tp->location.get ()));
f00aae0f
KS
12578
12579 /* Print out extra_string if this breakpoint is pending. It might
12580 contain, for example, conditions that were set by the user. */
12581 if (tp->loc == NULL && tp->extra_string != NULL)
12582 fprintf_unfiltered (fp, " %s", tp->extra_string);
12583
dd11a36c 12584 print_recreate_thread (tp, fp);
06edf0c0
PA
12585}
12586
983af33b 12587static void
f00aae0f
KS
12588bkpt_create_sals_from_location (const struct event_location *location,
12589 struct linespec_result *canonical,
12590 enum bptype type_wanted)
983af33b 12591{
f00aae0f 12592 create_sals_from_location_default (location, canonical, type_wanted);
983af33b
SDJ
12593}
12594
12595static void
12596bkpt_create_breakpoints_sal (struct gdbarch *gdbarch,
12597 struct linespec_result *canonical,
e1e01040
PA
12598 gdb::unique_xmalloc_ptr<char> cond_string,
12599 gdb::unique_xmalloc_ptr<char> extra_string,
983af33b
SDJ
12600 enum bptype type_wanted,
12601 enum bpdisp disposition,
12602 int thread,
12603 int task, int ignore_count,
12604 const struct breakpoint_ops *ops,
12605 int from_tty, int enabled,
44f238bb 12606 int internal, unsigned flags)
983af33b 12607{
023fa29b 12608 create_breakpoints_sal_default (gdbarch, canonical,
e1e01040
PA
12609 std::move (cond_string),
12610 std::move (extra_string),
e7e0cddf 12611 type_wanted,
983af33b
SDJ
12612 disposition, thread, task,
12613 ignore_count, ops, from_tty,
44f238bb 12614 enabled, internal, flags);
983af33b
SDJ
12615}
12616
6c5b2ebe 12617static std::vector<symtab_and_line>
f00aae0f
KS
12618bkpt_decode_location (struct breakpoint *b,
12619 const struct event_location *location,
6c5b2ebe 12620 struct program_space *search_pspace)
983af33b 12621{
6c5b2ebe 12622 return decode_location_default (b, location, search_pspace);
983af33b
SDJ
12623}
12624
06edf0c0
PA
12625/* Virtual table for internal breakpoints. */
12626
12627static void
12628internal_bkpt_re_set (struct breakpoint *b)
12629{
12630 switch (b->type)
12631 {
12632 /* Delete overlay event and longjmp master breakpoints; they
12633 will be reset later by breakpoint_re_set. */
12634 case bp_overlay_event:
12635 case bp_longjmp_master:
12636 case bp_std_terminate_master:
12637 case bp_exception_master:
12638 delete_breakpoint (b);
12639 break;
12640
12641 /* This breakpoint is special, it's set up when the inferior
12642 starts and we really don't want to touch it. */
12643 case bp_shlib_event:
12644
12645 /* Like bp_shlib_event, this breakpoint type is special. Once
12646 it is set up, we do not want to touch it. */
12647 case bp_thread_event:
12648 break;
12649 }
12650}
12651
12652static void
12653internal_bkpt_check_status (bpstat bs)
12654{
a9b3a50f
PA
12655 if (bs->breakpoint_at->type == bp_shlib_event)
12656 {
12657 /* If requested, stop when the dynamic linker notifies GDB of
12658 events. This allows the user to get control and place
12659 breakpoints in initializer routines for dynamically loaded
12660 objects (among other things). */
12661 bs->stop = stop_on_solib_events;
12662 bs->print = stop_on_solib_events;
12663 }
12664 else
12665 bs->stop = 0;
06edf0c0
PA
12666}
12667
12668static enum print_stop_action
12669internal_bkpt_print_it (bpstat bs)
12670{
06edf0c0 12671 struct breakpoint *b;
06edf0c0 12672
06edf0c0
PA
12673 b = bs->breakpoint_at;
12674
06edf0c0
PA
12675 switch (b->type)
12676 {
348d480f
PA
12677 case bp_shlib_event:
12678 /* Did we stop because the user set the stop_on_solib_events
12679 variable? (If so, we report this as a generic, "Stopped due
12680 to shlib event" message.) */
edcc5120 12681 print_solib_event (0);
348d480f
PA
12682 break;
12683
12684 case bp_thread_event:
12685 /* Not sure how we will get here.
12686 GDB should not stop for these breakpoints. */
12687 printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
12688 break;
12689
12690 case bp_overlay_event:
12691 /* By analogy with the thread event, GDB should not stop for these. */
12692 printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
12693 break;
12694
12695 case bp_longjmp_master:
12696 /* These should never be enabled. */
12697 printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
348d480f
PA
12698 break;
12699
12700 case bp_std_terminate_master:
12701 /* These should never be enabled. */
12702 printf_filtered (_("std::terminate Master Breakpoint: "
12703 "gdb should not stop!\n"));
348d480f
PA
12704 break;
12705
12706 case bp_exception_master:
12707 /* These should never be enabled. */
12708 printf_filtered (_("Exception Master Breakpoint: "
12709 "gdb should not stop!\n"));
06edf0c0
PA
12710 break;
12711 }
12712
001c8c33 12713 return PRINT_NOTHING;
06edf0c0
PA
12714}
12715
12716static void
12717internal_bkpt_print_mention (struct breakpoint *b)
12718{
12719 /* Nothing to mention. These breakpoints are internal. */
12720}
12721
06edf0c0
PA
12722/* Virtual table for momentary breakpoints */
12723
12724static void
12725momentary_bkpt_re_set (struct breakpoint *b)
12726{
12727 /* Keep temporary breakpoints, which can be encountered when we step
4d1eb6b4 12728 over a dlopen call and solib_add is resetting the breakpoints.
06edf0c0
PA
12729 Otherwise these should have been blown away via the cleanup chain
12730 or by breakpoint_init_inferior when we rerun the executable. */
12731}
12732
12733static void
12734momentary_bkpt_check_status (bpstat bs)
12735{
12736 /* Nothing. The point of these breakpoints is causing a stop. */
12737}
12738
12739static enum print_stop_action
12740momentary_bkpt_print_it (bpstat bs)
12741{
001c8c33 12742 return PRINT_UNKNOWN;
348d480f
PA
12743}
12744
06edf0c0
PA
12745static void
12746momentary_bkpt_print_mention (struct breakpoint *b)
348d480f 12747{
06edf0c0 12748 /* Nothing to mention. These breakpoints are internal. */
348d480f
PA
12749}
12750
e2e4d78b
JK
12751/* Ensure INITIATING_FRAME is cleared when no such breakpoint exists.
12752
12753 It gets cleared already on the removal of the first one of such placed
12754 breakpoints. This is OK as they get all removed altogether. */
12755
c1fc2657 12756longjmp_breakpoint::~longjmp_breakpoint ()
e2e4d78b 12757{
c1fc2657 12758 thread_info *tp = find_thread_global_id (this->thread);
e2e4d78b 12759
c1fc2657 12760 if (tp != NULL)
e2e4d78b 12761 tp->initiating_frame = null_frame_id;
e2e4d78b
JK
12762}
12763
55aa24fb
SDJ
12764/* Specific methods for probe breakpoints. */
12765
12766static int
12767bkpt_probe_insert_location (struct bp_location *bl)
12768{
12769 int v = bkpt_insert_location (bl);
12770
12771 if (v == 0)
12772 {
12773 /* The insertion was successful, now let's set the probe's semaphore
12774 if needed. */
935676c9 12775 bl->probe.prob->set_semaphore (bl->probe.objfile, bl->gdbarch);
55aa24fb
SDJ
12776 }
12777
12778 return v;
12779}
12780
12781static int
73971819
PA
12782bkpt_probe_remove_location (struct bp_location *bl,
12783 enum remove_bp_reason reason)
55aa24fb
SDJ
12784{
12785 /* Let's clear the semaphore before removing the location. */
935676c9 12786 bl->probe.prob->clear_semaphore (bl->probe.objfile, bl->gdbarch);
55aa24fb 12787
73971819 12788 return bkpt_remove_location (bl, reason);
55aa24fb
SDJ
12789}
12790
12791static void
f00aae0f 12792bkpt_probe_create_sals_from_location (const struct event_location *location,
5f700d83 12793 struct linespec_result *canonical,
f00aae0f 12794 enum bptype type_wanted)
55aa24fb
SDJ
12795{
12796 struct linespec_sals lsal;
12797
c2f4122d 12798 lsal.sals = parse_probes (location, NULL, canonical);
8e9e35b1
TT
12799 lsal.canonical
12800 = xstrdup (event_location_to_string (canonical->location.get ()));
6c5b2ebe 12801 canonical->lsals.push_back (std::move (lsal));
55aa24fb
SDJ
12802}
12803
6c5b2ebe 12804static std::vector<symtab_and_line>
f00aae0f
KS
12805bkpt_probe_decode_location (struct breakpoint *b,
12806 const struct event_location *location,
6c5b2ebe 12807 struct program_space *search_pspace)
55aa24fb 12808{
6c5b2ebe
PA
12809 std::vector<symtab_and_line> sals = parse_probes (location, search_pspace, NULL);
12810 if (sals.empty ())
55aa24fb 12811 error (_("probe not found"));
6c5b2ebe 12812 return sals;
55aa24fb
SDJ
12813}
12814
348d480f 12815/* The breakpoint_ops structure to be used in tracepoints. */
876fa593 12816
348d480f
PA
12817static void
12818tracepoint_re_set (struct breakpoint *b)
12819{
12820 breakpoint_re_set_default (b);
12821}
876fa593 12822
348d480f
PA
12823static int
12824tracepoint_breakpoint_hit (const struct bp_location *bl,
bd522513 12825 const address_space *aspace, CORE_ADDR bp_addr,
09ac7c10 12826 const struct target_waitstatus *ws)
348d480f
PA
12827{
12828 /* By definition, the inferior does not report stops at
12829 tracepoints. */
12830 return 0;
74960c60
VP
12831}
12832
12833static void
348d480f
PA
12834tracepoint_print_one_detail (const struct breakpoint *self,
12835 struct ui_out *uiout)
74960c60 12836{
d9b3f62e 12837 struct tracepoint *tp = (struct tracepoint *) self;
5d9310c4 12838 if (!tp->static_trace_marker_id.empty ())
348d480f
PA
12839 {
12840 gdb_assert (self->type == bp_static_tracepoint);
cc59ec59 12841
112e8700
SM
12842 uiout->text ("\tmarker id is ");
12843 uiout->field_string ("static-tracepoint-marker-string-id",
d9b3f62e 12844 tp->static_trace_marker_id);
112e8700 12845 uiout->text ("\n");
348d480f 12846 }
0d381245
VP
12847}
12848
a474d7c2 12849static void
348d480f 12850tracepoint_print_mention (struct breakpoint *b)
a474d7c2 12851{
112e8700 12852 if (current_uiout->is_mi_like_p ())
348d480f 12853 return;
cc59ec59 12854
348d480f
PA
12855 switch (b->type)
12856 {
12857 case bp_tracepoint:
12858 printf_filtered (_("Tracepoint"));
12859 printf_filtered (_(" %d"), b->number);
12860 break;
12861 case bp_fast_tracepoint:
12862 printf_filtered (_("Fast tracepoint"));
12863 printf_filtered (_(" %d"), b->number);
12864 break;
12865 case bp_static_tracepoint:
12866 printf_filtered (_("Static tracepoint"));
12867 printf_filtered (_(" %d"), b->number);
12868 break;
12869 default:
12870 internal_error (__FILE__, __LINE__,
12871 _("unhandled tracepoint type %d"), (int) b->type);
12872 }
12873
12874 say_where (b);
a474d7c2
PA
12875}
12876
348d480f 12877static void
d9b3f62e 12878tracepoint_print_recreate (struct breakpoint *self, struct ui_file *fp)
a474d7c2 12879{
d9b3f62e
PA
12880 struct tracepoint *tp = (struct tracepoint *) self;
12881
12882 if (self->type == bp_fast_tracepoint)
348d480f 12883 fprintf_unfiltered (fp, "ftrace");
c93e8391 12884 else if (self->type == bp_static_tracepoint)
348d480f 12885 fprintf_unfiltered (fp, "strace");
d9b3f62e 12886 else if (self->type == bp_tracepoint)
348d480f
PA
12887 fprintf_unfiltered (fp, "trace");
12888 else
12889 internal_error (__FILE__, __LINE__,
d9b3f62e 12890 _("unhandled tracepoint type %d"), (int) self->type);
cc59ec59 12891
f00aae0f 12892 fprintf_unfiltered (fp, " %s",
d28cd78a 12893 event_location_to_string (self->location.get ()));
d9b3f62e
PA
12894 print_recreate_thread (self, fp);
12895
12896 if (tp->pass_count)
12897 fprintf_unfiltered (fp, " passcount %d\n", tp->pass_count);
a474d7c2
PA
12898}
12899
983af33b 12900static void
f00aae0f
KS
12901tracepoint_create_sals_from_location (const struct event_location *location,
12902 struct linespec_result *canonical,
12903 enum bptype type_wanted)
983af33b 12904{
f00aae0f 12905 create_sals_from_location_default (location, canonical, type_wanted);
983af33b
SDJ
12906}
12907
12908static void
12909tracepoint_create_breakpoints_sal (struct gdbarch *gdbarch,
12910 struct linespec_result *canonical,
e1e01040
PA
12911 gdb::unique_xmalloc_ptr<char> cond_string,
12912 gdb::unique_xmalloc_ptr<char> extra_string,
983af33b
SDJ
12913 enum bptype type_wanted,
12914 enum bpdisp disposition,
12915 int thread,
12916 int task, int ignore_count,
12917 const struct breakpoint_ops *ops,
12918 int from_tty, int enabled,
44f238bb 12919 int internal, unsigned flags)
983af33b 12920{
023fa29b 12921 create_breakpoints_sal_default (gdbarch, canonical,
e1e01040
PA
12922 std::move (cond_string),
12923 std::move (extra_string),
e7e0cddf 12924 type_wanted,
983af33b
SDJ
12925 disposition, thread, task,
12926 ignore_count, ops, from_tty,
44f238bb 12927 enabled, internal, flags);
983af33b
SDJ
12928}
12929
6c5b2ebe 12930static std::vector<symtab_and_line>
f00aae0f
KS
12931tracepoint_decode_location (struct breakpoint *b,
12932 const struct event_location *location,
6c5b2ebe 12933 struct program_space *search_pspace)
983af33b 12934{
6c5b2ebe 12935 return decode_location_default (b, location, search_pspace);
983af33b
SDJ
12936}
12937
2060206e 12938struct breakpoint_ops tracepoint_breakpoint_ops;
348d480f 12939
55aa24fb
SDJ
12940/* The breakpoint_ops structure to be use on tracepoints placed in a
12941 static probe. */
12942
12943static void
f00aae0f
KS
12944tracepoint_probe_create_sals_from_location
12945 (const struct event_location *location,
12946 struct linespec_result *canonical,
12947 enum bptype type_wanted)
55aa24fb
SDJ
12948{
12949 /* We use the same method for breakpoint on probes. */
f00aae0f 12950 bkpt_probe_create_sals_from_location (location, canonical, type_wanted);
55aa24fb
SDJ
12951}
12952
6c5b2ebe 12953static std::vector<symtab_and_line>
f00aae0f
KS
12954tracepoint_probe_decode_location (struct breakpoint *b,
12955 const struct event_location *location,
6c5b2ebe 12956 struct program_space *search_pspace)
55aa24fb
SDJ
12957{
12958 /* We use the same method for breakpoint on probes. */
6c5b2ebe 12959 return bkpt_probe_decode_location (b, location, search_pspace);
55aa24fb
SDJ
12960}
12961
12962static struct breakpoint_ops tracepoint_probe_breakpoint_ops;
12963
5c2b4418
HZ
12964/* Dprintf breakpoint_ops methods. */
12965
12966static void
12967dprintf_re_set (struct breakpoint *b)
12968{
12969 breakpoint_re_set_default (b);
12970
f00aae0f
KS
12971 /* extra_string should never be non-NULL for dprintf. */
12972 gdb_assert (b->extra_string != NULL);
5c2b4418
HZ
12973
12974 /* 1 - connect to target 1, that can run breakpoint commands.
12975 2 - create a dprintf, which resolves fine.
12976 3 - disconnect from target 1
12977 4 - connect to target 2, that can NOT run breakpoint commands.
12978
12979 After steps #3/#4, you'll want the dprintf command list to
12980 be updated, because target 1 and 2 may well return different
12981 answers for target_can_run_breakpoint_commands().
12982 Given absence of finer grained resetting, we get to do
12983 it all the time. */
12984 if (b->extra_string != NULL)
12985 update_dprintf_command_list (b);
12986}
12987
2d9442cc
HZ
12988/* Implement the "print_recreate" breakpoint_ops method for dprintf. */
12989
12990static void
12991dprintf_print_recreate (struct breakpoint *tp, struct ui_file *fp)
12992{
f00aae0f 12993 fprintf_unfiltered (fp, "dprintf %s,%s",
d28cd78a 12994 event_location_to_string (tp->location.get ()),
2d9442cc
HZ
12995 tp->extra_string);
12996 print_recreate_thread (tp, fp);
12997}
12998
9d6e6e84
HZ
12999/* Implement the "after_condition_true" breakpoint_ops method for
13000 dprintf.
13001
13002 dprintf's are implemented with regular commands in their command
13003 list, but we run the commands here instead of before presenting the
13004 stop to the user, as dprintf's don't actually cause a stop. This
13005 also makes it so that the commands of multiple dprintfs at the same
13006 address are all handled. */
13007
13008static void
13009dprintf_after_condition_true (struct bpstats *bs)
13010{
04afa70c 13011 struct bpstats tmp_bs;
9d6e6e84
HZ
13012 struct bpstats *tmp_bs_p = &tmp_bs;
13013
13014 /* dprintf's never cause a stop. This wasn't set in the
13015 check_status hook instead because that would make the dprintf's
13016 condition not be evaluated. */
13017 bs->stop = 0;
13018
13019 /* Run the command list here. Take ownership of it instead of
13020 copying. We never want these commands to run later in
13021 bpstat_do_actions, if a breakpoint that causes a stop happens to
13022 be set at same address as this dprintf, or even if running the
13023 commands here throws. */
13024 tmp_bs.commands = bs->commands;
13025 bs->commands = NULL;
9d6e6e84
HZ
13026
13027 bpstat_do_actions_1 (&tmp_bs_p);
13028
13029 /* 'tmp_bs.commands' will usually be NULL by now, but
13030 bpstat_do_actions_1 may return early without processing the whole
13031 list. */
9d6e6e84
HZ
13032}
13033
983af33b
SDJ
13034/* The breakpoint_ops structure to be used on static tracepoints with
13035 markers (`-m'). */
13036
13037static void
f00aae0f 13038strace_marker_create_sals_from_location (const struct event_location *location,
5f700d83 13039 struct linespec_result *canonical,
f00aae0f 13040 enum bptype type_wanted)
983af33b
SDJ
13041{
13042 struct linespec_sals lsal;
f00aae0f 13043 const char *arg_start, *arg;
983af33b 13044
a20714ff 13045 arg = arg_start = get_linespec_location (location)->spec_string;
f00aae0f 13046 lsal.sals = decode_static_tracepoint_spec (&arg);
983af33b 13047
f2fc3015
TT
13048 std::string str (arg_start, arg - arg_start);
13049 const char *ptr = str.c_str ();
a20714ff
PA
13050 canonical->location
13051 = new_linespec_location (&ptr, symbol_name_match_type::FULL);
983af33b 13052
8e9e35b1
TT
13053 lsal.canonical
13054 = xstrdup (event_location_to_string (canonical->location.get ()));
6c5b2ebe 13055 canonical->lsals.push_back (std::move (lsal));
983af33b
SDJ
13056}
13057
13058static void
13059strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
13060 struct linespec_result *canonical,
e1e01040
PA
13061 gdb::unique_xmalloc_ptr<char> cond_string,
13062 gdb::unique_xmalloc_ptr<char> extra_string,
983af33b
SDJ
13063 enum bptype type_wanted,
13064 enum bpdisp disposition,
13065 int thread,
13066 int task, int ignore_count,
13067 const struct breakpoint_ops *ops,
13068 int from_tty, int enabled,
44f238bb 13069 int internal, unsigned flags)
983af33b 13070{
6c5b2ebe 13071 const linespec_sals &lsal = canonical->lsals[0];
983af33b
SDJ
13072
13073 /* If the user is creating a static tracepoint by marker id
13074 (strace -m MARKER_ID), then store the sals index, so that
13075 breakpoint_re_set can try to match up which of the newly
13076 found markers corresponds to this one, and, don't try to
13077 expand multiple locations for each sal, given than SALS
13078 already should contain all sals for MARKER_ID. */
13079
6c5b2ebe 13080 for (size_t i = 0; i < lsal.sals.size (); i++)
983af33b 13081 {
6c5b2ebe
PA
13082 event_location_up location
13083 = copy_event_location (canonical->location.get ());
983af33b 13084
b270e6f9 13085 std::unique_ptr<tracepoint> tp (new tracepoint ());
6c5b2ebe 13086 init_breakpoint_sal (tp.get (), gdbarch, lsal.sals[i],
ffc2605c 13087 std::move (location), NULL,
e1e01040
PA
13088 std::move (cond_string),
13089 std::move (extra_string),
e7e0cddf 13090 type_wanted, disposition,
983af33b 13091 thread, task, ignore_count, ops,
44f238bb 13092 from_tty, enabled, internal, flags,
983af33b
SDJ
13093 canonical->special_display);
13094 /* Given that its possible to have multiple markers with
13095 the same string id, if the user is creating a static
13096 tracepoint by marker id ("strace -m MARKER_ID"), then
13097 store the sals index, so that breakpoint_re_set can
13098 try to match up which of the newly found markers
13099 corresponds to this one */
13100 tp->static_trace_marker_id_idx = i;
13101
b270e6f9 13102 install_breakpoint (internal, std::move (tp), 0);
983af33b
SDJ
13103 }
13104}
13105
6c5b2ebe 13106static std::vector<symtab_and_line>
f00aae0f
KS
13107strace_marker_decode_location (struct breakpoint *b,
13108 const struct event_location *location,
6c5b2ebe 13109 struct program_space *search_pspace)
983af33b
SDJ
13110{
13111 struct tracepoint *tp = (struct tracepoint *) b;
a20714ff 13112 const char *s = get_linespec_location (location)->spec_string;
983af33b 13113
6c5b2ebe
PA
13114 std::vector<symtab_and_line> sals = decode_static_tracepoint_spec (&s);
13115 if (sals.size () > tp->static_trace_marker_id_idx)
983af33b 13116 {
6c5b2ebe
PA
13117 sals[0] = sals[tp->static_trace_marker_id_idx];
13118 sals.resize (1);
13119 return sals;
983af33b
SDJ
13120 }
13121 else
5d9310c4 13122 error (_("marker %s not found"), tp->static_trace_marker_id.c_str ());
983af33b
SDJ
13123}
13124
13125static struct breakpoint_ops strace_marker_breakpoint_ops;
13126
13127static int
13128strace_marker_p (struct breakpoint *b)
13129{
13130 return b->ops == &strace_marker_breakpoint_ops;
13131}
13132
53a5351d 13133/* Delete a breakpoint and clean up all traces of it in the data
f431efe5 13134 structures. */
c906108c
SS
13135
13136void
fba45db2 13137delete_breakpoint (struct breakpoint *bpt)
c906108c 13138{
52f0bd74 13139 struct breakpoint *b;
c906108c 13140
8a3fe4f8 13141 gdb_assert (bpt != NULL);
c906108c 13142
4a64f543
MS
13143 /* Has this bp already been deleted? This can happen because
13144 multiple lists can hold pointers to bp's. bpstat lists are
13145 especial culprits.
13146
13147 One example of this happening is a watchpoint's scope bp. When
13148 the scope bp triggers, we notice that the watchpoint is out of
13149 scope, and delete it. We also delete its scope bp. But the
13150 scope bp is marked "auto-deleting", and is already on a bpstat.
13151 That bpstat is then checked for auto-deleting bp's, which are
13152 deleted.
13153
13154 A real solution to this problem might involve reference counts in
13155 bp's, and/or giving them pointers back to their referencing
13156 bpstat's, and teaching delete_breakpoint to only free a bp's
13157 storage when no more references were extent. A cheaper bandaid
13158 was chosen. */
c906108c
SS
13159 if (bpt->type == bp_none)
13160 return;
13161
4a64f543
MS
13162 /* At least avoid this stale reference until the reference counting
13163 of breakpoints gets resolved. */
d0fb5eae 13164 if (bpt->related_breakpoint != bpt)
e5a0a904 13165 {
d0fb5eae 13166 struct breakpoint *related;
3a5c3e22 13167 struct watchpoint *w;
d0fb5eae
JK
13168
13169 if (bpt->type == bp_watchpoint_scope)
3a5c3e22 13170 w = (struct watchpoint *) bpt->related_breakpoint;
d0fb5eae 13171 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
3a5c3e22
PA
13172 w = (struct watchpoint *) bpt;
13173 else
13174 w = NULL;
13175 if (w != NULL)
13176 watchpoint_del_at_next_stop (w);
d0fb5eae
JK
13177
13178 /* Unlink bpt from the bpt->related_breakpoint ring. */
13179 for (related = bpt; related->related_breakpoint != bpt;
13180 related = related->related_breakpoint);
13181 related->related_breakpoint = bpt->related_breakpoint;
13182 bpt->related_breakpoint = bpt;
e5a0a904
JK
13183 }
13184
a9634178
TJB
13185 /* watch_command_1 creates a watchpoint but only sets its number if
13186 update_watchpoint succeeds in creating its bp_locations. If there's
13187 a problem in that process, we'll be asked to delete the half-created
13188 watchpoint. In that case, don't announce the deletion. */
13189 if (bpt->number)
76727919 13190 gdb::observers::breakpoint_deleted.notify (bpt);
c906108c 13191
c906108c
SS
13192 if (breakpoint_chain == bpt)
13193 breakpoint_chain = bpt->next;
13194
c906108c
SS
13195 ALL_BREAKPOINTS (b)
13196 if (b->next == bpt)
c5aa993b
JM
13197 {
13198 b->next = bpt->next;
13199 break;
13200 }
c906108c 13201
f431efe5
PA
13202 /* Be sure no bpstat's are pointing at the breakpoint after it's
13203 been freed. */
13204 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
e5dd4106 13205 in all threads for now. Note that we cannot just remove bpstats
f431efe5
PA
13206 pointing at bpt from the stop_bpstat list entirely, as breakpoint
13207 commands are associated with the bpstat; if we remove it here,
13208 then the later call to bpstat_do_actions (&stop_bpstat); in
13209 event-top.c won't do anything, and temporary breakpoints with
13210 commands won't work. */
13211
13212 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
13213
4a64f543
MS
13214 /* Now that breakpoint is removed from breakpoint list, update the
13215 global location list. This will remove locations that used to
13216 belong to this breakpoint. Do this before freeing the breakpoint
13217 itself, since remove_breakpoint looks at location's owner. It
13218 might be better design to have location completely
13219 self-contained, but it's not the case now. */
44702360 13220 update_global_location_list (UGLL_DONT_INSERT);
74960c60 13221
4a64f543
MS
13222 /* On the chance that someone will soon try again to delete this
13223 same bp, we mark it as deleted before freeing its storage. */
c906108c 13224 bpt->type = bp_none;
4d01a485 13225 delete bpt;
c906108c
SS
13226}
13227
51be5b68
PA
13228/* Iterator function to call a user-provided callback function once
13229 for each of B and its related breakpoints. */
13230
13231static void
13232iterate_over_related_breakpoints (struct breakpoint *b,
48649e1b 13233 gdb::function_view<void (breakpoint *)> function)
51be5b68
PA
13234{
13235 struct breakpoint *related;
13236
13237 related = b;
13238 do
13239 {
13240 struct breakpoint *next;
13241
13242 /* FUNCTION may delete RELATED. */
13243 next = related->related_breakpoint;
13244
13245 if (next == related)
13246 {
13247 /* RELATED is the last ring entry. */
48649e1b 13248 function (related);
51be5b68
PA
13249
13250 /* FUNCTION may have deleted it, so we'd never reach back to
13251 B. There's nothing left to do anyway, so just break
13252 out. */
13253 break;
13254 }
13255 else
48649e1b 13256 function (related);
51be5b68
PA
13257
13258 related = next;
13259 }
13260 while (related != b);
13261}
95a42b64 13262
4495129a 13263static void
981a3fb3 13264delete_command (const char *arg, int from_tty)
c906108c 13265{
35df4500 13266 struct breakpoint *b, *b_tmp;
c906108c 13267
ea9365bb
TT
13268 dont_repeat ();
13269
c906108c
SS
13270 if (arg == 0)
13271 {
13272 int breaks_to_delete = 0;
13273
46c6471b
PA
13274 /* Delete all breakpoints if no argument. Do not delete
13275 internal breakpoints, these have to be deleted with an
13276 explicit breakpoint number argument. */
c5aa993b 13277 ALL_BREAKPOINTS (b)
46c6471b 13278 if (user_breakpoint_p (b))
973d738b
DJ
13279 {
13280 breaks_to_delete = 1;
13281 break;
13282 }
c906108c
SS
13283
13284 /* Ask user only if there are some breakpoints to delete. */
13285 if (!from_tty
e2e0b3e5 13286 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
c906108c 13287 {
35df4500 13288 ALL_BREAKPOINTS_SAFE (b, b_tmp)
46c6471b 13289 if (user_breakpoint_p (b))
c5aa993b 13290 delete_breakpoint (b);
c906108c
SS
13291 }
13292 }
13293 else
48649e1b
TT
13294 map_breakpoint_numbers
13295 (arg, [&] (breakpoint *b)
13296 {
13297 iterate_over_related_breakpoints (b, delete_breakpoint);
13298 });
c906108c
SS
13299}
13300
c2f4122d
PA
13301/* Return true if all locations of B bound to PSPACE are pending. If
13302 PSPACE is NULL, all locations of all program spaces are
13303 considered. */
13304
0d381245 13305static int
c2f4122d 13306all_locations_are_pending (struct breakpoint *b, struct program_space *pspace)
fe3f5fa8 13307{
c2f4122d
PA
13308 struct bp_location *loc;
13309
13310 for (loc = b->loc; loc != NULL; loc = loc->next)
13311 if ((pspace == NULL
13312 || loc->pspace == pspace)
13313 && !loc->shlib_disabled
8645ff69 13314 && !loc->pspace->executing_startup)
0d381245
VP
13315 return 0;
13316 return 1;
fe3f5fa8
VP
13317}
13318
776592bf
DE
13319/* Subroutine of update_breakpoint_locations to simplify it.
13320 Return non-zero if multiple fns in list LOC have the same name.
13321 Null names are ignored. */
13322
13323static int
13324ambiguous_names_p (struct bp_location *loc)
13325{
13326 struct bp_location *l;
459a2e4c
TT
13327 htab_t htab = htab_create_alloc (13, htab_hash_string, streq_hash, NULL,
13328 xcalloc, xfree);
776592bf
DE
13329
13330 for (l = loc; l != NULL; l = l->next)
13331 {
13332 const char **slot;
13333 const char *name = l->function_name;
13334
13335 /* Allow for some names to be NULL, ignore them. */
13336 if (name == NULL)
13337 continue;
13338
13339 slot = (const char **) htab_find_slot (htab, (const void *) name,
13340 INSERT);
4a64f543
MS
13341 /* NOTE: We can assume slot != NULL here because xcalloc never
13342 returns NULL. */
776592bf
DE
13343 if (*slot != NULL)
13344 {
13345 htab_delete (htab);
13346 return 1;
13347 }
13348 *slot = name;
13349 }
13350
13351 htab_delete (htab);
13352 return 0;
13353}
13354
0fb4aa4b
PA
13355/* When symbols change, it probably means the sources changed as well,
13356 and it might mean the static tracepoint markers are no longer at
13357 the same address or line numbers they used to be at last we
13358 checked. Losing your static tracepoints whenever you rebuild is
13359 undesirable. This function tries to resync/rematch gdb static
13360 tracepoints with the markers on the target, for static tracepoints
13361 that have not been set by marker id. Static tracepoint that have
13362 been set by marker id are reset by marker id in breakpoint_re_set.
13363 The heuristic is:
13364
13365 1) For a tracepoint set at a specific address, look for a marker at
13366 the old PC. If one is found there, assume to be the same marker.
13367 If the name / string id of the marker found is different from the
13368 previous known name, assume that means the user renamed the marker
13369 in the sources, and output a warning.
13370
13371 2) For a tracepoint set at a given line number, look for a marker
13372 at the new address of the old line number. If one is found there,
13373 assume to be the same marker. If the name / string id of the
13374 marker found is different from the previous known name, assume that
13375 means the user renamed the marker in the sources, and output a
13376 warning.
13377
13378 3) If a marker is no longer found at the same address or line, it
13379 may mean the marker no longer exists. But it may also just mean
13380 the code changed a bit. Maybe the user added a few lines of code
13381 that made the marker move up or down (in line number terms). Ask
13382 the target for info about the marker with the string id as we knew
13383 it. If found, update line number and address in the matching
13384 static tracepoint. This will get confused if there's more than one
13385 marker with the same ID (possible in UST, although unadvised
13386 precisely because it confuses tools). */
13387
13388static struct symtab_and_line
13389update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
13390{
d9b3f62e 13391 struct tracepoint *tp = (struct tracepoint *) b;
0fb4aa4b
PA
13392 struct static_tracepoint_marker marker;
13393 CORE_ADDR pc;
0fb4aa4b
PA
13394
13395 pc = sal.pc;
13396 if (sal.line)
13397 find_line_pc (sal.symtab, sal.line, &pc);
13398
13399 if (target_static_tracepoint_marker_at (pc, &marker))
13400 {
5d9310c4 13401 if (tp->static_trace_marker_id != marker.str_id)
0fb4aa4b 13402 warning (_("static tracepoint %d changed probed marker from %s to %s"),
5d9310c4
SM
13403 b->number, tp->static_trace_marker_id.c_str (),
13404 marker.str_id.c_str ());
0fb4aa4b 13405
5d9310c4 13406 tp->static_trace_marker_id = std::move (marker.str_id);
0fb4aa4b
PA
13407
13408 return sal;
13409 }
13410
13411 /* Old marker wasn't found on target at lineno. Try looking it up
13412 by string ID. */
13413 if (!sal.explicit_pc
13414 && sal.line != 0
13415 && sal.symtab != NULL
5d9310c4 13416 && !tp->static_trace_marker_id.empty ())
0fb4aa4b 13417 {
5d9310c4
SM
13418 std::vector<static_tracepoint_marker> markers
13419 = target_static_tracepoint_markers_by_strid
13420 (tp->static_trace_marker_id.c_str ());
0fb4aa4b 13421
5d9310c4 13422 if (!markers.empty ())
0fb4aa4b 13423 {
0fb4aa4b 13424 struct symbol *sym;
80e1d417 13425 struct static_tracepoint_marker *tpmarker;
79a45e25 13426 struct ui_out *uiout = current_uiout;
67994074 13427 struct explicit_location explicit_loc;
0fb4aa4b 13428
5d9310c4 13429 tpmarker = &markers[0];
0fb4aa4b 13430
5d9310c4 13431 tp->static_trace_marker_id = std::move (tpmarker->str_id);
0fb4aa4b
PA
13432
13433 warning (_("marker for static tracepoint %d (%s) not "
13434 "found at previous line number"),
5d9310c4 13435 b->number, tp->static_trace_marker_id.c_str ());
0fb4aa4b 13436
51abb421 13437 symtab_and_line sal2 = find_pc_line (tpmarker->address, 0);
80e1d417 13438 sym = find_pc_sect_function (tpmarker->address, NULL);
112e8700 13439 uiout->text ("Now in ");
0fb4aa4b
PA
13440 if (sym)
13441 {
112e8700
SM
13442 uiout->field_string ("func", SYMBOL_PRINT_NAME (sym));
13443 uiout->text (" at ");
0fb4aa4b 13444 }
112e8700 13445 uiout->field_string ("file",
05cba821 13446 symtab_to_filename_for_display (sal2.symtab));
112e8700 13447 uiout->text (":");
0fb4aa4b 13448
112e8700 13449 if (uiout->is_mi_like_p ())
0fb4aa4b 13450 {
0b0865da 13451 const char *fullname = symtab_to_fullname (sal2.symtab);
0fb4aa4b 13452
112e8700 13453 uiout->field_string ("fullname", fullname);
0fb4aa4b
PA
13454 }
13455
112e8700
SM
13456 uiout->field_int ("line", sal2.line);
13457 uiout->text ("\n");
0fb4aa4b 13458
80e1d417 13459 b->loc->line_number = sal2.line;
2f202fde 13460 b->loc->symtab = sym != NULL ? sal2.symtab : NULL;
0fb4aa4b 13461
d28cd78a 13462 b->location.reset (NULL);
67994074
KS
13463 initialize_explicit_location (&explicit_loc);
13464 explicit_loc.source_filename
00e52e53 13465 = ASTRDUP (symtab_to_filename_for_display (sal2.symtab));
67994074
KS
13466 explicit_loc.line_offset.offset = b->loc->line_number;
13467 explicit_loc.line_offset.sign = LINE_OFFSET_NONE;
d28cd78a 13468 b->location = new_explicit_location (&explicit_loc);
0fb4aa4b
PA
13469
13470 /* Might be nice to check if function changed, and warn if
13471 so. */
0fb4aa4b
PA
13472 }
13473 }
13474 return sal;
13475}
13476
8d3788bd
VP
13477/* Returns 1 iff locations A and B are sufficiently same that
13478 we don't need to report breakpoint as changed. */
13479
13480static int
13481locations_are_equal (struct bp_location *a, struct bp_location *b)
13482{
13483 while (a && b)
13484 {
13485 if (a->address != b->address)
13486 return 0;
13487
13488 if (a->shlib_disabled != b->shlib_disabled)
13489 return 0;
13490
13491 if (a->enabled != b->enabled)
13492 return 0;
13493
13494 a = a->next;
13495 b = b->next;
13496 }
13497
13498 if ((a == NULL) != (b == NULL))
13499 return 0;
13500
13501 return 1;
13502}
13503
c2f4122d
PA
13504/* Split all locations of B that are bound to PSPACE out of B's
13505 location list to a separate list and return that list's head. If
13506 PSPACE is NULL, hoist out all locations of B. */
13507
13508static struct bp_location *
13509hoist_existing_locations (struct breakpoint *b, struct program_space *pspace)
13510{
13511 struct bp_location head;
13512 struct bp_location *i = b->loc;
13513 struct bp_location **i_link = &b->loc;
13514 struct bp_location *hoisted = &head;
13515
13516 if (pspace == NULL)
13517 {
13518 i = b->loc;
13519 b->loc = NULL;
13520 return i;
13521 }
13522
13523 head.next = NULL;
13524
13525 while (i != NULL)
13526 {
13527 if (i->pspace == pspace)
13528 {
13529 *i_link = i->next;
13530 i->next = NULL;
13531 hoisted->next = i;
13532 hoisted = i;
13533 }
13534 else
13535 i_link = &i->next;
13536 i = *i_link;
13537 }
13538
13539 return head.next;
13540}
13541
13542/* Create new breakpoint locations for B (a hardware or software
13543 breakpoint) based on SALS and SALS_END. If SALS_END.NELTS is not
13544 zero, then B is a ranged breakpoint. Only recreates locations for
13545 FILTER_PSPACE. Locations of other program spaces are left
13546 untouched. */
f1310107 13547
0e30163f 13548void
0d381245 13549update_breakpoint_locations (struct breakpoint *b,
c2f4122d 13550 struct program_space *filter_pspace,
6c5b2ebe
PA
13551 gdb::array_view<const symtab_and_line> sals,
13552 gdb::array_view<const symtab_and_line> sals_end)
fe3f5fa8 13553{
c2f4122d 13554 struct bp_location *existing_locations;
0d381245 13555
6c5b2ebe 13556 if (!sals_end.empty () && (sals.size () != 1 || sals_end.size () != 1))
f8eba3c6
TT
13557 {
13558 /* Ranged breakpoints have only one start location and one end
13559 location. */
13560 b->enable_state = bp_disabled;
f8eba3c6
TT
13561 printf_unfiltered (_("Could not reset ranged breakpoint %d: "
13562 "multiple locations found\n"),
13563 b->number);
13564 return;
13565 }
f1310107 13566
4a64f543
MS
13567 /* If there's no new locations, and all existing locations are
13568 pending, don't do anything. This optimizes the common case where
13569 all locations are in the same shared library, that was unloaded.
13570 We'd like to retain the location, so that when the library is
13571 loaded again, we don't loose the enabled/disabled status of the
13572 individual locations. */
6c5b2ebe 13573 if (all_locations_are_pending (b, filter_pspace) && sals.empty ())
fe3f5fa8
VP
13574 return;
13575
c2f4122d 13576 existing_locations = hoist_existing_locations (b, filter_pspace);
fe3f5fa8 13577
6c5b2ebe 13578 for (const auto &sal : sals)
fe3f5fa8 13579 {
f8eba3c6
TT
13580 struct bp_location *new_loc;
13581
6c5b2ebe 13582 switch_to_program_space_and_thread (sal.pspace);
f8eba3c6 13583
6c5b2ebe 13584 new_loc = add_location_to_breakpoint (b, &sal);
fe3f5fa8 13585
0d381245
VP
13586 /* Reparse conditions, they might contain references to the
13587 old symtab. */
13588 if (b->cond_string != NULL)
13589 {
bbc13ae3 13590 const char *s;
fe3f5fa8 13591
0d381245 13592 s = b->cond_string;
492d29ea 13593 TRY
0d381245 13594 {
6c5b2ebe
PA
13595 new_loc->cond = parse_exp_1 (&s, sal.pc,
13596 block_for_pc (sal.pc),
0d381245
VP
13597 0);
13598 }
492d29ea 13599 CATCH (e, RETURN_MASK_ERROR)
0d381245 13600 {
3e43a32a
MS
13601 warning (_("failed to reevaluate condition "
13602 "for breakpoint %d: %s"),
0d381245
VP
13603 b->number, e.message);
13604 new_loc->enabled = 0;
13605 }
492d29ea 13606 END_CATCH
0d381245 13607 }
fe3f5fa8 13608
6c5b2ebe 13609 if (!sals_end.empty ())
f1310107 13610 {
6c5b2ebe 13611 CORE_ADDR end = find_breakpoint_range_end (sals_end[0]);
f1310107 13612
6c5b2ebe 13613 new_loc->length = end - sals[0].pc + 1;
f1310107 13614 }
0d381245 13615 }
fe3f5fa8 13616
4a64f543
MS
13617 /* If possible, carry over 'disable' status from existing
13618 breakpoints. */
0d381245
VP
13619 {
13620 struct bp_location *e = existing_locations;
776592bf
DE
13621 /* If there are multiple breakpoints with the same function name,
13622 e.g. for inline functions, comparing function names won't work.
13623 Instead compare pc addresses; this is just a heuristic as things
13624 may have moved, but in practice it gives the correct answer
13625 often enough until a better solution is found. */
13626 int have_ambiguous_names = ambiguous_names_p (b->loc);
13627
0d381245
VP
13628 for (; e; e = e->next)
13629 {
13630 if (!e->enabled && e->function_name)
13631 {
13632 struct bp_location *l = b->loc;
776592bf
DE
13633 if (have_ambiguous_names)
13634 {
13635 for (; l; l = l->next)
f1310107 13636 if (breakpoint_locations_match (e, l))
776592bf
DE
13637 {
13638 l->enabled = 0;
13639 break;
13640 }
13641 }
13642 else
13643 {
13644 for (; l; l = l->next)
13645 if (l->function_name
13646 && strcmp (e->function_name, l->function_name) == 0)
13647 {
13648 l->enabled = 0;
13649 break;
13650 }
13651 }
0d381245
VP
13652 }
13653 }
13654 }
fe3f5fa8 13655
8d3788bd 13656 if (!locations_are_equal (existing_locations, b->loc))
76727919 13657 gdb::observers::breakpoint_modified.notify (b);
fe3f5fa8
VP
13658}
13659
f00aae0f 13660/* Find the SaL locations corresponding to the given LOCATION.
ef23e705
TJB
13661 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
13662
6c5b2ebe 13663static std::vector<symtab_and_line>
f00aae0f 13664location_to_sals (struct breakpoint *b, struct event_location *location,
c2f4122d 13665 struct program_space *search_pspace, int *found)
ef23e705 13666{
492d29ea 13667 struct gdb_exception exception = exception_none;
ef23e705 13668
983af33b 13669 gdb_assert (b->ops != NULL);
ef23e705 13670
6c5b2ebe
PA
13671 std::vector<symtab_and_line> sals;
13672
492d29ea 13673 TRY
ef23e705 13674 {
6c5b2ebe 13675 sals = b->ops->decode_location (b, location, search_pspace);
ef23e705 13676 }
492d29ea 13677 CATCH (e, RETURN_MASK_ERROR)
ef23e705
TJB
13678 {
13679 int not_found_and_ok = 0;
492d29ea
PA
13680
13681 exception = e;
13682
ef23e705
TJB
13683 /* For pending breakpoints, it's expected that parsing will
13684 fail until the right shared library is loaded. User has
13685 already told to create pending breakpoints and don't need
13686 extra messages. If breakpoint is in bp_shlib_disabled
13687 state, then user already saw the message about that
13688 breakpoint being disabled, and don't want to see more
13689 errors. */
58438ac1 13690 if (e.error == NOT_FOUND_ERROR
c2f4122d
PA
13691 && (b->condition_not_parsed
13692 || (b->loc != NULL
13693 && search_pspace != NULL
13694 && b->loc->pspace != search_pspace)
ef23e705 13695 || (b->loc && b->loc->shlib_disabled)
f8eba3c6 13696 || (b->loc && b->loc->pspace->executing_startup)
ef23e705
TJB
13697 || b->enable_state == bp_disabled))
13698 not_found_and_ok = 1;
13699
13700 if (!not_found_and_ok)
13701 {
13702 /* We surely don't want to warn about the same breakpoint
13703 10 times. One solution, implemented here, is disable
13704 the breakpoint on error. Another solution would be to
13705 have separate 'warning emitted' flag. Since this
13706 happens only when a binary has changed, I don't know
13707 which approach is better. */
13708 b->enable_state = bp_disabled;
13709 throw_exception (e);
13710 }
13711 }
492d29ea 13712 END_CATCH
ef23e705 13713
492d29ea 13714 if (exception.reason == 0 || exception.error != NOT_FOUND_ERROR)
ef23e705 13715 {
6c5b2ebe
PA
13716 for (auto &sal : sals)
13717 resolve_sal_pc (&sal);
f00aae0f 13718 if (b->condition_not_parsed && b->extra_string != NULL)
ef23e705 13719 {
ed1d1739
KS
13720 char *cond_string, *extra_string;
13721 int thread, task;
ef23e705 13722
6c5b2ebe 13723 find_condition_and_thread (b->extra_string, sals[0].pc,
e7e0cddf
SS
13724 &cond_string, &thread, &task,
13725 &extra_string);
f00aae0f 13726 gdb_assert (b->cond_string == NULL);
ef23e705
TJB
13727 if (cond_string)
13728 b->cond_string = cond_string;
13729 b->thread = thread;
13730 b->task = task;
e7e0cddf 13731 if (extra_string)
f00aae0f
KS
13732 {
13733 xfree (b->extra_string);
13734 b->extra_string = extra_string;
13735 }
ef23e705
TJB
13736 b->condition_not_parsed = 0;
13737 }
13738
983af33b 13739 if (b->type == bp_static_tracepoint && !strace_marker_p (b))
6c5b2ebe 13740 sals[0] = update_static_tracepoint (b, sals[0]);
ef23e705 13741
58438ac1
TT
13742 *found = 1;
13743 }
13744 else
13745 *found = 0;
ef23e705
TJB
13746
13747 return sals;
13748}
13749
348d480f
PA
13750/* The default re_set method, for typical hardware or software
13751 breakpoints. Reevaluate the breakpoint and recreate its
13752 locations. */
13753
13754static void
28010a5d 13755breakpoint_re_set_default (struct breakpoint *b)
ef23e705 13756{
c2f4122d 13757 struct program_space *filter_pspace = current_program_space;
6c5b2ebe 13758 std::vector<symtab_and_line> expanded, expanded_end;
ef23e705 13759
6c5b2ebe
PA
13760 int found;
13761 std::vector<symtab_and_line> sals = location_to_sals (b, b->location.get (),
13762 filter_pspace, &found);
ef23e705 13763 if (found)
6c5b2ebe 13764 expanded = std::move (sals);
ef23e705 13765
f00aae0f 13766 if (b->location_range_end != NULL)
f1310107 13767 {
6c5b2ebe
PA
13768 std::vector<symtab_and_line> sals_end
13769 = location_to_sals (b, b->location_range_end.get (),
13770 filter_pspace, &found);
f1310107 13771 if (found)
6c5b2ebe 13772 expanded_end = std::move (sals_end);
f1310107
TJB
13773 }
13774
c2f4122d 13775 update_breakpoint_locations (b, filter_pspace, expanded, expanded_end);
28010a5d
PA
13776}
13777
983af33b
SDJ
13778/* Default method for creating SALs from an address string. It basically
13779 calls parse_breakpoint_sals. Return 1 for success, zero for failure. */
13780
13781static void
f00aae0f
KS
13782create_sals_from_location_default (const struct event_location *location,
13783 struct linespec_result *canonical,
13784 enum bptype type_wanted)
983af33b 13785{
f00aae0f 13786 parse_breakpoint_sals (location, canonical);
983af33b
SDJ
13787}
13788
13789/* Call create_breakpoints_sal for the given arguments. This is the default
13790 function for the `create_breakpoints_sal' method of
13791 breakpoint_ops. */
13792
13793static void
13794create_breakpoints_sal_default (struct gdbarch *gdbarch,
13795 struct linespec_result *canonical,
e1e01040
PA
13796 gdb::unique_xmalloc_ptr<char> cond_string,
13797 gdb::unique_xmalloc_ptr<char> extra_string,
983af33b
SDJ
13798 enum bptype type_wanted,
13799 enum bpdisp disposition,
13800 int thread,
13801 int task, int ignore_count,
13802 const struct breakpoint_ops *ops,
13803 int from_tty, int enabled,
44f238bb 13804 int internal, unsigned flags)
983af33b 13805{
e1e01040
PA
13806 create_breakpoints_sal (gdbarch, canonical,
13807 std::move (cond_string),
13808 std::move (extra_string),
983af33b
SDJ
13809 type_wanted, disposition,
13810 thread, task, ignore_count, ops, from_tty,
44f238bb 13811 enabled, internal, flags);
983af33b
SDJ
13812}
13813
13814/* Decode the line represented by S by calling decode_line_full. This is the
5f700d83 13815 default function for the `decode_location' method of breakpoint_ops. */
983af33b 13816
6c5b2ebe 13817static std::vector<symtab_and_line>
f00aae0f
KS
13818decode_location_default (struct breakpoint *b,
13819 const struct event_location *location,
6c5b2ebe 13820 struct program_space *search_pspace)
983af33b
SDJ
13821{
13822 struct linespec_result canonical;
13823
c2f4122d 13824 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, search_pspace,
983af33b
SDJ
13825 (struct symtab *) NULL, 0,
13826 &canonical, multiple_symbols_all,
13827 b->filter);
13828
13829 /* We should get 0 or 1 resulting SALs. */
6c5b2ebe 13830 gdb_assert (canonical.lsals.size () < 2);
983af33b 13831
6c5b2ebe 13832 if (!canonical.lsals.empty ())
983af33b 13833 {
6c5b2ebe
PA
13834 const linespec_sals &lsal = canonical.lsals[0];
13835 return std::move (lsal.sals);
983af33b 13836 }
6c5b2ebe 13837 return {};
983af33b
SDJ
13838}
13839
bf469271 13840/* Reset a breakpoint. */
c906108c 13841
bf469271
PA
13842static void
13843breakpoint_re_set_one (breakpoint *b)
c906108c 13844{
fdf44873
TT
13845 input_radix = b->input_radix;
13846 set_language (b->language);
c906108c 13847
348d480f 13848 b->ops->re_set (b);
c906108c
SS
13849}
13850
c2f4122d
PA
13851/* Re-set breakpoint locations for the current program space.
13852 Locations bound to other program spaces are left untouched. */
13853
c906108c 13854void
69de3c6a 13855breakpoint_re_set (void)
c906108c 13856{
35df4500 13857 struct breakpoint *b, *b_tmp;
2a7f3dff 13858
c5aa993b 13859 {
fdf44873
TT
13860 scoped_restore_current_language save_language;
13861 scoped_restore save_input_radix = make_scoped_restore (&input_radix);
5ed8105e 13862 scoped_restore_current_pspace_and_thread restore_pspace_thread;
e62c965a 13863
8e817061
JB
13864 /* breakpoint_re_set_one sets the current_language to the language
13865 of the breakpoint it is resetting (see prepare_re_set_context)
13866 before re-evaluating the breakpoint's location. This change can
13867 unfortunately get undone by accident if the language_mode is set
13868 to auto, and we either switch frames, or more likely in this context,
13869 we select the current frame.
13870
13871 We prevent this by temporarily turning the language_mode to
13872 language_mode_manual. We restore it once all breakpoints
13873 have been reset. */
13874 scoped_restore save_language_mode = make_scoped_restore (&language_mode);
13875 language_mode = language_mode_manual;
13876
5ed8105e
PA
13877 /* Note: we must not try to insert locations until after all
13878 breakpoints have been re-set. Otherwise, e.g., when re-setting
13879 breakpoint 1, we'd insert the locations of breakpoint 2, which
13880 hadn't been re-set yet, and thus may have stale locations. */
4efc6507 13881
5ed8105e
PA
13882 ALL_BREAKPOINTS_SAFE (b, b_tmp)
13883 {
bf469271
PA
13884 TRY
13885 {
13886 breakpoint_re_set_one (b);
13887 }
13888 CATCH (ex, RETURN_MASK_ALL)
13889 {
13890 exception_fprintf (gdb_stderr, ex,
13891 "Error in re-setting breakpoint %d: ",
13892 b->number);
13893 }
13894 END_CATCH
5ed8105e 13895 }
5ed8105e
PA
13896
13897 jit_breakpoint_re_set ();
13898 }
6c95b8df 13899
af02033e
PP
13900 create_overlay_event_breakpoint ();
13901 create_longjmp_master_breakpoint ();
13902 create_std_terminate_master_breakpoint ();
186c406b 13903 create_exception_master_breakpoint ();
2a7f3dff
PA
13904
13905 /* Now we can insert. */
13906 update_global_location_list (UGLL_MAY_INSERT);
c906108c
SS
13907}
13908\f
c906108c
SS
13909/* Reset the thread number of this breakpoint:
13910
13911 - If the breakpoint is for all threads, leave it as-is.
4a64f543 13912 - Else, reset it to the current thread for inferior_ptid. */
c906108c 13913void
fba45db2 13914breakpoint_re_set_thread (struct breakpoint *b)
c906108c
SS
13915{
13916 if (b->thread != -1)
13917 {
00431a78 13918 b->thread = inferior_thread ()->global_num;
6c95b8df
PA
13919
13920 /* We're being called after following a fork. The new fork is
13921 selected as current, and unless this was a vfork will have a
13922 different program space from the original thread. Reset that
13923 as well. */
13924 b->loc->pspace = current_program_space;
c906108c
SS
13925 }
13926}
13927
03ac34d5
MS
13928/* Set ignore-count of breakpoint number BPTNUM to COUNT.
13929 If from_tty is nonzero, it prints a message to that effect,
13930 which ends with a period (no newline). */
13931
c906108c 13932void
fba45db2 13933set_ignore_count (int bptnum, int count, int from_tty)
c906108c 13934{
52f0bd74 13935 struct breakpoint *b;
c906108c
SS
13936
13937 if (count < 0)
13938 count = 0;
13939
13940 ALL_BREAKPOINTS (b)
13941 if (b->number == bptnum)
c5aa993b 13942 {
d77f58be
SS
13943 if (is_tracepoint (b))
13944 {
13945 if (from_tty && count != 0)
13946 printf_filtered (_("Ignore count ignored for tracepoint %d."),
13947 bptnum);
13948 return;
13949 }
13950
c5aa993b 13951 b->ignore_count = count;
221ea385
KS
13952 if (from_tty)
13953 {
13954 if (count == 0)
3e43a32a
MS
13955 printf_filtered (_("Will stop next time "
13956 "breakpoint %d is reached."),
221ea385
KS
13957 bptnum);
13958 else if (count == 1)
a3f17187 13959 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
221ea385
KS
13960 bptnum);
13961 else
3e43a32a
MS
13962 printf_filtered (_("Will ignore next %d "
13963 "crossings of breakpoint %d."),
221ea385
KS
13964 count, bptnum);
13965 }
76727919 13966 gdb::observers::breakpoint_modified.notify (b);
c5aa993b
JM
13967 return;
13968 }
c906108c 13969
8a3fe4f8 13970 error (_("No breakpoint number %d."), bptnum);
c906108c
SS
13971}
13972
c906108c
SS
13973/* Command to set ignore-count of breakpoint N to COUNT. */
13974
13975static void
0b39b52e 13976ignore_command (const char *args, int from_tty)
c906108c 13977{
0b39b52e 13978 const char *p = args;
52f0bd74 13979 int num;
c906108c
SS
13980
13981 if (p == 0)
e2e0b3e5 13982 error_no_arg (_("a breakpoint number"));
c5aa993b 13983
c906108c 13984 num = get_number (&p);
5c44784c 13985 if (num == 0)
8a3fe4f8 13986 error (_("bad breakpoint number: '%s'"), args);
c906108c 13987 if (*p == 0)
8a3fe4f8 13988 error (_("Second argument (specified ignore-count) is missing."));
c906108c
SS
13989
13990 set_ignore_count (num,
13991 longest_to_int (value_as_long (parse_and_eval (p))),
13992 from_tty);
221ea385
KS
13993 if (from_tty)
13994 printf_filtered ("\n");
c906108c
SS
13995}
13996\f
d0fe4701
XR
13997
13998/* Call FUNCTION on each of the breakpoints with numbers in the range
13999 defined by BP_NUM_RANGE (an inclusive range). */
c906108c
SS
14000
14001static void
d0fe4701
XR
14002map_breakpoint_number_range (std::pair<int, int> bp_num_range,
14003 gdb::function_view<void (breakpoint *)> function)
c906108c 14004{
d0fe4701
XR
14005 if (bp_num_range.first == 0)
14006 {
14007 warning (_("bad breakpoint number at or near '%d'"),
14008 bp_num_range.first);
14009 }
14010 else
c906108c 14011 {
d0fe4701 14012 struct breakpoint *b, *tmp;
197f0a60 14013
d0fe4701 14014 for (int i = bp_num_range.first; i <= bp_num_range.second; i++)
5c44784c 14015 {
d0fe4701
XR
14016 bool match = false;
14017
5c44784c 14018 ALL_BREAKPOINTS_SAFE (b, tmp)
d0fe4701 14019 if (b->number == i)
5c44784c 14020 {
bfd28288 14021 match = true;
48649e1b 14022 function (b);
11cf8741 14023 break;
5c44784c 14024 }
bfd28288 14025 if (!match)
d0fe4701 14026 printf_unfiltered (_("No breakpoint number %d.\n"), i);
c5aa993b 14027 }
c906108c
SS
14028 }
14029}
14030
d0fe4701
XR
14031/* Call FUNCTION on each of the breakpoints whose numbers are given in
14032 ARGS. */
14033
14034static void
14035map_breakpoint_numbers (const char *args,
14036 gdb::function_view<void (breakpoint *)> function)
14037{
14038 if (args == NULL || *args == '\0')
14039 error_no_arg (_("one or more breakpoint numbers"));
14040
14041 number_or_range_parser parser (args);
14042
14043 while (!parser.finished ())
14044 {
14045 int num = parser.get_number ();
14046 map_breakpoint_number_range (std::make_pair (num, num), function);
14047 }
14048}
14049
14050/* Return the breakpoint location structure corresponding to the
14051 BP_NUM and LOC_NUM values. */
14052
0d381245 14053static struct bp_location *
d0fe4701 14054find_location_by_number (int bp_num, int loc_num)
0d381245 14055{
0d381245 14056 struct breakpoint *b;
0d381245
VP
14057
14058 ALL_BREAKPOINTS (b)
14059 if (b->number == bp_num)
14060 {
14061 break;
14062 }
14063
14064 if (!b || b->number != bp_num)
d0fe4701 14065 error (_("Bad breakpoint number '%d'"), bp_num);
0d381245 14066
0d381245 14067 if (loc_num == 0)
d0fe4701 14068 error (_("Bad breakpoint location number '%d'"), loc_num);
0d381245 14069
d0fe4701
XR
14070 int n = 0;
14071 for (bp_location *loc = b->loc; loc != NULL; loc = loc->next)
14072 if (++n == loc_num)
14073 return loc;
14074
14075 error (_("Bad breakpoint location number '%d'"), loc_num);
0d381245
VP
14076}
14077
95e95a6d
PA
14078/* Modes of operation for extract_bp_num. */
14079enum class extract_bp_kind
14080{
14081 /* Extracting a breakpoint number. */
14082 bp,
14083
14084 /* Extracting a location number. */
14085 loc,
14086};
14087
14088/* Extract a breakpoint or location number (as determined by KIND)
14089 from the string starting at START. TRAILER is a character which
14090 can be found after the number. If you don't want a trailer, use
14091 '\0'. If END_OUT is not NULL, it is set to point after the parsed
14092 string. This always returns a positive integer. */
14093
14094static int
14095extract_bp_num (extract_bp_kind kind, const char *start,
14096 int trailer, const char **end_out = NULL)
14097{
14098 const char *end = start;
14099 int num = get_number_trailer (&end, trailer);
14100 if (num < 0)
14101 error (kind == extract_bp_kind::bp
14102 ? _("Negative breakpoint number '%.*s'")
14103 : _("Negative breakpoint location number '%.*s'"),
14104 int (end - start), start);
14105 if (num == 0)
14106 error (kind == extract_bp_kind::bp
14107 ? _("Bad breakpoint number '%.*s'")
14108 : _("Bad breakpoint location number '%.*s'"),
14109 int (end - start), start);
14110
14111 if (end_out != NULL)
14112 *end_out = end;
14113 return num;
14114}
14115
14116/* Extract a breakpoint or location range (as determined by KIND) in
14117 the form NUM1-NUM2 stored at &ARG[arg_offset]. Returns a std::pair
14118 representing the (inclusive) range. The returned pair's elements
14119 are always positive integers. */
14120
14121static std::pair<int, int>
14122extract_bp_or_bp_range (extract_bp_kind kind,
14123 const std::string &arg,
14124 std::string::size_type arg_offset)
14125{
14126 std::pair<int, int> range;
14127 const char *bp_loc = &arg[arg_offset];
14128 std::string::size_type dash = arg.find ('-', arg_offset);
14129 if (dash != std::string::npos)
14130 {
14131 /* bp_loc is a range (x-z). */
14132 if (arg.length () == dash + 1)
14133 error (kind == extract_bp_kind::bp
14134 ? _("Bad breakpoint number at or near: '%s'")
14135 : _("Bad breakpoint location number at or near: '%s'"),
14136 bp_loc);
14137
14138 const char *end;
14139 const char *start_first = bp_loc;
14140 const char *start_second = &arg[dash + 1];
14141 range.first = extract_bp_num (kind, start_first, '-');
14142 range.second = extract_bp_num (kind, start_second, '\0', &end);
14143
14144 if (range.first > range.second)
14145 error (kind == extract_bp_kind::bp
14146 ? _("Inverted breakpoint range at '%.*s'")
14147 : _("Inverted breakpoint location range at '%.*s'"),
14148 int (end - start_first), start_first);
14149 }
14150 else
14151 {
14152 /* bp_loc is a single value. */
14153 range.first = extract_bp_num (kind, bp_loc, '\0');
14154 range.second = range.first;
14155 }
14156 return range;
14157}
14158
d0fe4701
XR
14159/* Extract the breakpoint/location range specified by ARG. Returns
14160 the breakpoint range in BP_NUM_RANGE, and the location range in
14161 BP_LOC_RANGE.
14162
14163 ARG may be in any of the following forms:
14164
14165 x where 'x' is a breakpoint number.
14166 x-y where 'x' and 'y' specify a breakpoint numbers range.
14167 x.y where 'x' is a breakpoint number and 'y' a location number.
14168 x.y-z where 'x' is a breakpoint number and 'y' and 'z' specify a
14169 location number range.
14170*/
14171
cc638e86 14172static void
d0fe4701
XR
14173extract_bp_number_and_location (const std::string &arg,
14174 std::pair<int, int> &bp_num_range,
14175 std::pair<int, int> &bp_loc_range)
14176{
14177 std::string::size_type dot = arg.find ('.');
14178
14179 if (dot != std::string::npos)
14180 {
14181 /* Handle 'x.y' and 'x.y-z' cases. */
14182
14183 if (arg.length () == dot + 1 || dot == 0)
95e95a6d 14184 error (_("Bad breakpoint number at or near: '%s'"), arg.c_str ());
d0fe4701 14185
95e95a6d
PA
14186 bp_num_range.first
14187 = extract_bp_num (extract_bp_kind::bp, arg.c_str (), '.');
14188 bp_num_range.second = bp_num_range.first;
d0fe4701 14189
95e95a6d
PA
14190 bp_loc_range = extract_bp_or_bp_range (extract_bp_kind::loc,
14191 arg, dot + 1);
d0fe4701
XR
14192 }
14193 else
14194 {
14195 /* Handle x and x-y cases. */
d0fe4701 14196
95e95a6d 14197 bp_num_range = extract_bp_or_bp_range (extract_bp_kind::bp, arg, 0);
d0fe4701
XR
14198 bp_loc_range.first = 0;
14199 bp_loc_range.second = 0;
14200 }
d0fe4701
XR
14201}
14202
14203/* Enable or disable a breakpoint location BP_NUM.LOC_NUM. ENABLE
14204 specifies whether to enable or disable. */
14205
14206static void
14207enable_disable_bp_num_loc (int bp_num, int loc_num, bool enable)
14208{
14209 struct bp_location *loc = find_location_by_number (bp_num, loc_num);
14210 if (loc != NULL)
14211 {
14212 if (loc->enabled != enable)
14213 {
14214 loc->enabled = enable;
14215 mark_breakpoint_location_modified (loc);
14216 }
14217 if (target_supports_enable_disable_tracepoint ()
14218 && current_trace_status ()->running && loc->owner
14219 && is_tracepoint (loc->owner))
14220 target_disable_tracepoint (loc);
14221 }
14222 update_global_location_list (UGLL_DONT_INSERT);
d7154a8d
JV
14223
14224 gdb::observers::breakpoint_modified.notify (loc->owner);
d0fe4701
XR
14225}
14226
14227/* Enable or disable a range of breakpoint locations. BP_NUM is the
14228 number of the breakpoint, and BP_LOC_RANGE specifies the
14229 (inclusive) range of location numbers of that breakpoint to
14230 enable/disable. ENABLE specifies whether to enable or disable the
14231 location. */
14232
14233static void
14234enable_disable_breakpoint_location_range (int bp_num,
14235 std::pair<int, int> &bp_loc_range,
14236 bool enable)
14237{
14238 for (int i = bp_loc_range.first; i <= bp_loc_range.second; i++)
14239 enable_disable_bp_num_loc (bp_num, i, enable);
14240}
0d381245 14241
1900040c
MS
14242/* Set ignore-count of breakpoint number BPTNUM to COUNT.
14243 If from_tty is nonzero, it prints a message to that effect,
14244 which ends with a period (no newline). */
14245
c906108c 14246void
fba45db2 14247disable_breakpoint (struct breakpoint *bpt)
c906108c
SS
14248{
14249 /* Never disable a watchpoint scope breakpoint; we want to
14250 hit them when we leave scope so we can delete both the
14251 watchpoint and its scope breakpoint at that time. */
14252 if (bpt->type == bp_watchpoint_scope)
14253 return;
14254
b5de0fa7 14255 bpt->enable_state = bp_disabled;
c906108c 14256
b775012e
LM
14257 /* Mark breakpoint locations modified. */
14258 mark_breakpoint_modified (bpt);
14259
d248b706
KY
14260 if (target_supports_enable_disable_tracepoint ()
14261 && current_trace_status ()->running && is_tracepoint (bpt))
14262 {
14263 struct bp_location *location;
14264
14265 for (location = bpt->loc; location; location = location->next)
14266 target_disable_tracepoint (location);
14267 }
14268
44702360 14269 update_global_location_list (UGLL_DONT_INSERT);
c906108c 14270
76727919 14271 gdb::observers::breakpoint_modified.notify (bpt);
c906108c
SS
14272}
14273
d0fe4701
XR
14274/* Enable or disable the breakpoint(s) or breakpoint location(s)
14275 specified in ARGS. ARGS may be in any of the formats handled by
14276 extract_bp_number_and_location. ENABLE specifies whether to enable
14277 or disable the breakpoints/locations. */
14278
c906108c 14279static void
d0fe4701 14280enable_disable_command (const char *args, int from_tty, bool enable)
c906108c 14281{
c906108c 14282 if (args == 0)
46c6471b
PA
14283 {
14284 struct breakpoint *bpt;
14285
14286 ALL_BREAKPOINTS (bpt)
14287 if (user_breakpoint_p (bpt))
d0fe4701
XR
14288 {
14289 if (enable)
14290 enable_breakpoint (bpt);
14291 else
14292 disable_breakpoint (bpt);
14293 }
46c6471b 14294 }
9eaabc75 14295 else
0d381245 14296 {
cb791d59 14297 std::string num = extract_arg (&args);
9eaabc75 14298
cb791d59 14299 while (!num.empty ())
d248b706 14300 {
d0fe4701 14301 std::pair<int, int> bp_num_range, bp_loc_range;
9eaabc75 14302
cc638e86
PA
14303 extract_bp_number_and_location (num, bp_num_range, bp_loc_range);
14304
14305 if (bp_loc_range.first == bp_loc_range.second
14306 && bp_loc_range.first == 0)
d0fe4701 14307 {
cc638e86
PA
14308 /* Handle breakpoint ids with formats 'x' or 'x-z'. */
14309 map_breakpoint_number_range (bp_num_range,
14310 enable
14311 ? enable_breakpoint
14312 : disable_breakpoint);
14313 }
14314 else
14315 {
14316 /* Handle breakpoint ids with formats 'x.y' or
14317 'x.y-z'. */
14318 enable_disable_breakpoint_location_range
14319 (bp_num_range.first, bp_loc_range, enable);
b775012e 14320 }
9eaabc75 14321 num = extract_arg (&args);
d248b706 14322 }
0d381245 14323 }
c906108c
SS
14324}
14325
d0fe4701
XR
14326/* The disable command disables the specified breakpoints/locations
14327 (or all defined breakpoints) so they're no longer effective in
14328 stopping the inferior. ARGS may be in any of the forms defined in
14329 extract_bp_number_and_location. */
14330
14331static void
14332disable_command (const char *args, int from_tty)
14333{
14334 enable_disable_command (args, from_tty, false);
14335}
14336
c906108c 14337static void
816338b5
SS
14338enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
14339 int count)
c906108c 14340{
afe38095 14341 int target_resources_ok;
c906108c
SS
14342
14343 if (bpt->type == bp_hardware_breakpoint)
14344 {
14345 int i;
c5aa993b 14346 i = hw_breakpoint_used_count ();
53a5351d 14347 target_resources_ok =
d92524f1 14348 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
53a5351d 14349 i + 1, 0);
c906108c 14350 if (target_resources_ok == 0)
8a3fe4f8 14351 error (_("No hardware breakpoint support in the target."));
c906108c 14352 else if (target_resources_ok < 0)
8a3fe4f8 14353 error (_("Hardware breakpoints used exceeds limit."));
c906108c
SS
14354 }
14355
cc60f2e3 14356 if (is_watchpoint (bpt))
c906108c 14357 {
d07205c2 14358 /* Initialize it just to avoid a GCC false warning. */
f486487f 14359 enum enable_state orig_enable_state = bp_disabled;
dde02812 14360
492d29ea 14361 TRY
c906108c 14362 {
3a5c3e22
PA
14363 struct watchpoint *w = (struct watchpoint *) bpt;
14364
1e718ff1
TJB
14365 orig_enable_state = bpt->enable_state;
14366 bpt->enable_state = bp_enabled;
3a5c3e22 14367 update_watchpoint (w, 1 /* reparse */);
c906108c 14368 }
492d29ea 14369 CATCH (e, RETURN_MASK_ALL)
c5aa993b 14370 {
1e718ff1 14371 bpt->enable_state = orig_enable_state;
dde02812
ES
14372 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
14373 bpt->number);
14374 return;
c5aa993b 14375 }
492d29ea 14376 END_CATCH
c906108c 14377 }
0101ce28 14378
b775012e
LM
14379 bpt->enable_state = bp_enabled;
14380
14381 /* Mark breakpoint locations modified. */
14382 mark_breakpoint_modified (bpt);
14383
d248b706
KY
14384 if (target_supports_enable_disable_tracepoint ()
14385 && current_trace_status ()->running && is_tracepoint (bpt))
14386 {
14387 struct bp_location *location;
14388
14389 for (location = bpt->loc; location; location = location->next)
14390 target_enable_tracepoint (location);
14391 }
14392
b4c291bb 14393 bpt->disposition = disposition;
816338b5 14394 bpt->enable_count = count;
44702360 14395 update_global_location_list (UGLL_MAY_INSERT);
9c97429f 14396
76727919 14397 gdb::observers::breakpoint_modified.notify (bpt);
c906108c
SS
14398}
14399
fe3f5fa8 14400
c906108c 14401void
fba45db2 14402enable_breakpoint (struct breakpoint *bpt)
c906108c 14403{
816338b5 14404 enable_breakpoint_disp (bpt, bpt->disposition, 0);
51be5b68
PA
14405}
14406
d0fe4701
XR
14407/* The enable command enables the specified breakpoints/locations (or
14408 all defined breakpoints) so they once again become (or continue to
14409 be) effective in stopping the inferior. ARGS may be in any of the
14410 forms defined in extract_bp_number_and_location. */
c906108c 14411
c906108c 14412static void
981a3fb3 14413enable_command (const char *args, int from_tty)
c906108c 14414{
d0fe4701 14415 enable_disable_command (args, from_tty, true);
c906108c
SS
14416}
14417
c906108c 14418static void
4495129a 14419enable_once_command (const char *args, int from_tty)
c906108c 14420{
48649e1b
TT
14421 map_breakpoint_numbers
14422 (args, [&] (breakpoint *b)
14423 {
14424 iterate_over_related_breakpoints
14425 (b, [&] (breakpoint *bpt)
14426 {
14427 enable_breakpoint_disp (bpt, disp_disable, 1);
14428 });
14429 });
816338b5
SS
14430}
14431
14432static void
4495129a 14433enable_count_command (const char *args, int from_tty)
816338b5 14434{
b9d61307
SM
14435 int count;
14436
14437 if (args == NULL)
14438 error_no_arg (_("hit count"));
14439
14440 count = get_number (&args);
816338b5 14441
48649e1b
TT
14442 map_breakpoint_numbers
14443 (args, [&] (breakpoint *b)
14444 {
14445 iterate_over_related_breakpoints
14446 (b, [&] (breakpoint *bpt)
14447 {
14448 enable_breakpoint_disp (bpt, disp_disable, count);
14449 });
14450 });
c906108c
SS
14451}
14452
c906108c 14453static void
4495129a 14454enable_delete_command (const char *args, int from_tty)
c906108c 14455{
48649e1b
TT
14456 map_breakpoint_numbers
14457 (args, [&] (breakpoint *b)
14458 {
14459 iterate_over_related_breakpoints
14460 (b, [&] (breakpoint *bpt)
14461 {
14462 enable_breakpoint_disp (bpt, disp_del, 1);
14463 });
14464 });
c906108c
SS
14465}
14466\f
fa8d40ab 14467static void
981a3fb3 14468set_breakpoint_cmd (const char *args, int from_tty)
fa8d40ab
JJ
14469{
14470}
14471
14472static void
981a3fb3 14473show_breakpoint_cmd (const char *args, int from_tty)
fa8d40ab
JJ
14474{
14475}
14476
1f3b5d1b
PP
14477/* Invalidate last known value of any hardware watchpoint if
14478 the memory which that value represents has been written to by
14479 GDB itself. */
14480
14481static void
8de0566d
YQ
14482invalidate_bp_value_on_memory_change (struct inferior *inferior,
14483 CORE_ADDR addr, ssize_t len,
1f3b5d1b
PP
14484 const bfd_byte *data)
14485{
14486 struct breakpoint *bp;
14487
14488 ALL_BREAKPOINTS (bp)
14489 if (bp->enable_state == bp_enabled
3a5c3e22 14490 && bp->type == bp_hardware_watchpoint)
1f3b5d1b 14491 {
3a5c3e22 14492 struct watchpoint *wp = (struct watchpoint *) bp;
1f3b5d1b 14493
850645cf 14494 if (wp->val_valid && wp->val != nullptr)
3a5c3e22
PA
14495 {
14496 struct bp_location *loc;
14497
14498 for (loc = bp->loc; loc != NULL; loc = loc->next)
14499 if (loc->loc_type == bp_loc_hardware_watchpoint
14500 && loc->address + loc->length > addr
14501 && addr + len > loc->address)
14502 {
3a5c3e22
PA
14503 wp->val = NULL;
14504 wp->val_valid = 0;
14505 }
14506 }
1f3b5d1b
PP
14507 }
14508}
14509
8181d85f
DJ
14510/* Create and insert a breakpoint for software single step. */
14511
14512void
6c95b8df 14513insert_single_step_breakpoint (struct gdbarch *gdbarch,
accd0bcd 14514 const address_space *aspace,
4a64f543 14515 CORE_ADDR next_pc)
8181d85f 14516{
7c16b83e
PA
14517 struct thread_info *tp = inferior_thread ();
14518 struct symtab_and_line sal;
14519 CORE_ADDR pc = next_pc;
8181d85f 14520
34b7e8a6
PA
14521 if (tp->control.single_step_breakpoints == NULL)
14522 {
14523 tp->control.single_step_breakpoints
5d5658a1 14524 = new_single_step_breakpoint (tp->global_num, gdbarch);
34b7e8a6 14525 }
8181d85f 14526
7c16b83e
PA
14527 sal = find_pc_line (pc, 0);
14528 sal.pc = pc;
14529 sal.section = find_pc_overlay (pc);
14530 sal.explicit_pc = 1;
34b7e8a6 14531 add_location_to_breakpoint (tp->control.single_step_breakpoints, &sal);
8181d85f 14532
7c16b83e 14533 update_global_location_list (UGLL_INSERT);
8181d85f
DJ
14534}
14535
93f9a11f
YQ
14536/* Insert single step breakpoints according to the current state. */
14537
14538int
14539insert_single_step_breakpoints (struct gdbarch *gdbarch)
14540{
f5ea389a 14541 struct regcache *regcache = get_current_regcache ();
a0ff9e1a 14542 std::vector<CORE_ADDR> next_pcs;
93f9a11f 14543
f5ea389a 14544 next_pcs = gdbarch_software_single_step (gdbarch, regcache);
93f9a11f 14545
a0ff9e1a 14546 if (!next_pcs.empty ())
93f9a11f 14547 {
f5ea389a 14548 struct frame_info *frame = get_current_frame ();
8b86c959 14549 const address_space *aspace = get_frame_address_space (frame);
93f9a11f 14550
a0ff9e1a 14551 for (CORE_ADDR pc : next_pcs)
93f9a11f
YQ
14552 insert_single_step_breakpoint (gdbarch, aspace, pc);
14553
93f9a11f
YQ
14554 return 1;
14555 }
14556 else
14557 return 0;
14558}
14559
34b7e8a6 14560/* See breakpoint.h. */
f02253f1
HZ
14561
14562int
7c16b83e 14563breakpoint_has_location_inserted_here (struct breakpoint *bp,
accd0bcd 14564 const address_space *aspace,
7c16b83e 14565 CORE_ADDR pc)
1aafd4da 14566{
7c16b83e 14567 struct bp_location *loc;
1aafd4da 14568
7c16b83e
PA
14569 for (loc = bp->loc; loc != NULL; loc = loc->next)
14570 if (loc->inserted
14571 && breakpoint_location_address_match (loc, aspace, pc))
14572 return 1;
1aafd4da 14573
7c16b83e 14574 return 0;
ef370185
JB
14575}
14576
14577/* Check whether a software single-step breakpoint is inserted at
14578 PC. */
14579
14580int
accd0bcd 14581single_step_breakpoint_inserted_here_p (const address_space *aspace,
ef370185
JB
14582 CORE_ADDR pc)
14583{
34b7e8a6
PA
14584 struct breakpoint *bpt;
14585
14586 ALL_BREAKPOINTS (bpt)
14587 {
14588 if (bpt->type == bp_single_step
14589 && breakpoint_has_location_inserted_here (bpt, aspace, pc))
14590 return 1;
14591 }
14592 return 0;
1aafd4da
UW
14593}
14594
1042e4c0
SS
14595/* Tracepoint-specific operations. */
14596
14597/* Set tracepoint count to NUM. */
14598static void
14599set_tracepoint_count (int num)
14600{
14601 tracepoint_count = num;
4fa62494 14602 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
1042e4c0
SS
14603}
14604
70221824 14605static void
0b39b52e 14606trace_command (const char *arg, int from_tty)
1042e4c0 14607{
55aa24fb 14608 struct breakpoint_ops *ops;
55aa24fb 14609
ffc2605c
TT
14610 event_location_up location = string_to_event_location (&arg,
14611 current_language);
5b56227b 14612 if (location != NULL
ffc2605c 14613 && event_location_type (location.get ()) == PROBE_LOCATION)
55aa24fb
SDJ
14614 ops = &tracepoint_probe_breakpoint_ops;
14615 else
14616 ops = &tracepoint_breakpoint_ops;
14617
558a9d82 14618 create_breakpoint (get_current_arch (),
ffc2605c 14619 location.get (),
f00aae0f 14620 NULL, 0, arg, 1 /* parse arg */,
558a9d82
YQ
14621 0 /* tempflag */,
14622 bp_tracepoint /* type_wanted */,
14623 0 /* Ignore count */,
14624 pending_break_support,
14625 ops,
14626 from_tty,
14627 1 /* enabled */,
14628 0 /* internal */, 0);
1042e4c0
SS
14629}
14630
70221824 14631static void
0b39b52e 14632ftrace_command (const char *arg, int from_tty)
7a697b8d 14633{
ffc2605c
TT
14634 event_location_up location = string_to_event_location (&arg,
14635 current_language);
558a9d82 14636 create_breakpoint (get_current_arch (),
ffc2605c 14637 location.get (),
f00aae0f 14638 NULL, 0, arg, 1 /* parse arg */,
558a9d82
YQ
14639 0 /* tempflag */,
14640 bp_fast_tracepoint /* type_wanted */,
14641 0 /* Ignore count */,
14642 pending_break_support,
14643 &tracepoint_breakpoint_ops,
14644 from_tty,
14645 1 /* enabled */,
14646 0 /* internal */, 0);
0fb4aa4b
PA
14647}
14648
14649/* strace command implementation. Creates a static tracepoint. */
14650
70221824 14651static void
0b39b52e 14652strace_command (const char *arg, int from_tty)
0fb4aa4b 14653{
983af33b 14654 struct breakpoint_ops *ops;
ffc2605c 14655 event_location_up location;
983af33b
SDJ
14656
14657 /* Decide if we are dealing with a static tracepoint marker (`-m'),
14658 or with a normal static tracepoint. */
61012eef 14659 if (arg && startswith (arg, "-m") && isspace (arg[2]))
f00aae0f
KS
14660 {
14661 ops = &strace_marker_breakpoint_ops;
a20714ff 14662 location = new_linespec_location (&arg, symbol_name_match_type::FULL);
f00aae0f 14663 }
983af33b 14664 else
f00aae0f
KS
14665 {
14666 ops = &tracepoint_breakpoint_ops;
14667 location = string_to_event_location (&arg, current_language);
14668 }
983af33b 14669
558a9d82 14670 create_breakpoint (get_current_arch (),
ffc2605c 14671 location.get (),
f00aae0f 14672 NULL, 0, arg, 1 /* parse arg */,
558a9d82
YQ
14673 0 /* tempflag */,
14674 bp_static_tracepoint /* type_wanted */,
14675 0 /* Ignore count */,
14676 pending_break_support,
14677 ops,
14678 from_tty,
14679 1 /* enabled */,
14680 0 /* internal */, 0);
7a697b8d
SS
14681}
14682
409873ef
SS
14683/* Set up a fake reader function that gets command lines from a linked
14684 list that was acquired during tracepoint uploading. */
14685
14686static struct uploaded_tp *this_utp;
3149d8c1 14687static int next_cmd;
409873ef
SS
14688
14689static char *
14690read_uploaded_action (void)
14691{
a18ba4e4 14692 char *rslt = nullptr;
409873ef 14693
a18ba4e4
SM
14694 if (next_cmd < this_utp->cmd_strings.size ())
14695 {
14696 rslt = this_utp->cmd_strings[next_cmd];
14697 next_cmd++;
14698 }
409873ef
SS
14699
14700 return rslt;
14701}
14702
00bf0b85
SS
14703/* Given information about a tracepoint as recorded on a target (which
14704 can be either a live system or a trace file), attempt to create an
14705 equivalent GDB tracepoint. This is not a reliable process, since
14706 the target does not necessarily have all the information used when
14707 the tracepoint was originally defined. */
14708
d9b3f62e 14709struct tracepoint *
00bf0b85 14710create_tracepoint_from_upload (struct uploaded_tp *utp)
d5551862 14711{
f2fc3015
TT
14712 const char *addr_str;
14713 char small_buf[100];
d9b3f62e 14714 struct tracepoint *tp;
fd9b8c24 14715
409873ef
SS
14716 if (utp->at_string)
14717 addr_str = utp->at_string;
14718 else
14719 {
14720 /* In the absence of a source location, fall back to raw
14721 address. Since there is no way to confirm that the address
14722 means the same thing as when the trace was started, warn the
14723 user. */
3e43a32a
MS
14724 warning (_("Uploaded tracepoint %d has no "
14725 "source location, using raw address"),
409873ef 14726 utp->number);
8c042590 14727 xsnprintf (small_buf, sizeof (small_buf), "*%s", hex_string (utp->addr));
409873ef
SS
14728 addr_str = small_buf;
14729 }
14730
14731 /* There's not much we can do with a sequence of bytecodes. */
14732 if (utp->cond && !utp->cond_string)
3e43a32a
MS
14733 warning (_("Uploaded tracepoint %d condition "
14734 "has no source form, ignoring it"),
409873ef 14735 utp->number);
d5551862 14736
ffc2605c
TT
14737 event_location_up location = string_to_event_location (&addr_str,
14738 current_language);
8cdf0e15 14739 if (!create_breakpoint (get_current_arch (),
ffc2605c 14740 location.get (),
f00aae0f 14741 utp->cond_string, -1, addr_str,
e7e0cddf 14742 0 /* parse cond/thread */,
8cdf0e15 14743 0 /* tempflag */,
0fb4aa4b 14744 utp->type /* type_wanted */,
8cdf0e15
VP
14745 0 /* Ignore count */,
14746 pending_break_support,
348d480f 14747 &tracepoint_breakpoint_ops,
8cdf0e15 14748 0 /* from_tty */,
84f4c1fe 14749 utp->enabled /* enabled */,
44f238bb
PA
14750 0 /* internal */,
14751 CREATE_BREAKPOINT_FLAGS_INSERTED))
ffc2605c 14752 return NULL;
fd9b8c24 14753
409873ef 14754 /* Get the tracepoint we just created. */
fd9b8c24
PA
14755 tp = get_tracepoint (tracepoint_count);
14756 gdb_assert (tp != NULL);
d5551862 14757
00bf0b85
SS
14758 if (utp->pass > 0)
14759 {
8c042590 14760 xsnprintf (small_buf, sizeof (small_buf), "%d %d", utp->pass,
c1fc2657 14761 tp->number);
00bf0b85 14762
409873ef 14763 trace_pass_command (small_buf, 0);
00bf0b85
SS
14764 }
14765
409873ef
SS
14766 /* If we have uploaded versions of the original commands, set up a
14767 special-purpose "reader" function and call the usual command line
14768 reader, then pass the result to the breakpoint command-setting
14769 function. */
a18ba4e4 14770 if (!utp->cmd_strings.empty ())
00bf0b85 14771 {
12973681 14772 counted_command_line cmd_list;
00bf0b85 14773
409873ef 14774 this_utp = utp;
3149d8c1 14775 next_cmd = 0;
d5551862 14776
60b3cef2 14777 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL);
409873ef 14778
c1fc2657 14779 breakpoint_set_commands (tp, std::move (cmd_list));
00bf0b85 14780 }
a18ba4e4
SM
14781 else if (!utp->actions.empty ()
14782 || !utp->step_actions.empty ())
3e43a32a
MS
14783 warning (_("Uploaded tracepoint %d actions "
14784 "have no source form, ignoring them"),
409873ef 14785 utp->number);
00bf0b85 14786
f196051f 14787 /* Copy any status information that might be available. */
c1fc2657 14788 tp->hit_count = utp->hit_count;
f196051f
SS
14789 tp->traceframe_usage = utp->traceframe_usage;
14790
00bf0b85 14791 return tp;
d9b3f62e 14792}
00bf0b85 14793
1042e4c0
SS
14794/* Print information on tracepoint number TPNUM_EXP, or all if
14795 omitted. */
14796
14797static void
1d12d88f 14798info_tracepoints_command (const char *args, int from_tty)
1042e4c0 14799{
79a45e25 14800 struct ui_out *uiout = current_uiout;
e5a67952 14801 int num_printed;
1042e4c0 14802
e5a67952 14803 num_printed = breakpoint_1 (args, 0, is_tracepoint);
d77f58be
SS
14804
14805 if (num_printed == 0)
1042e4c0 14806 {
e5a67952 14807 if (args == NULL || *args == '\0')
112e8700 14808 uiout->message ("No tracepoints.\n");
d77f58be 14809 else
112e8700 14810 uiout->message ("No tracepoint matching '%s'.\n", args);
1042e4c0 14811 }
ad443146
SS
14812
14813 default_collect_info ();
1042e4c0
SS
14814}
14815
4a64f543 14816/* The 'enable trace' command enables tracepoints.
1042e4c0
SS
14817 Not supported by all targets. */
14818static void
5fed81ff 14819enable_trace_command (const char *args, int from_tty)
1042e4c0
SS
14820{
14821 enable_command (args, from_tty);
14822}
14823
4a64f543 14824/* The 'disable trace' command disables tracepoints.
1042e4c0
SS
14825 Not supported by all targets. */
14826static void
5fed81ff 14827disable_trace_command (const char *args, int from_tty)
1042e4c0
SS
14828{
14829 disable_command (args, from_tty);
14830}
14831
4a64f543 14832/* Remove a tracepoint (or all if no argument). */
1042e4c0 14833static void
4495129a 14834delete_trace_command (const char *arg, int from_tty)
1042e4c0 14835{
35df4500 14836 struct breakpoint *b, *b_tmp;
1042e4c0
SS
14837
14838 dont_repeat ();
14839
14840 if (arg == 0)
14841 {
14842 int breaks_to_delete = 0;
14843
14844 /* Delete all breakpoints if no argument.
14845 Do not delete internal or call-dummy breakpoints, these
4a64f543
MS
14846 have to be deleted with an explicit breakpoint number
14847 argument. */
1042e4c0 14848 ALL_TRACEPOINTS (b)
46c6471b 14849 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0
SS
14850 {
14851 breaks_to_delete = 1;
14852 break;
14853 }
1042e4c0
SS
14854
14855 /* Ask user only if there are some breakpoints to delete. */
14856 if (!from_tty
14857 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
14858 {
35df4500 14859 ALL_BREAKPOINTS_SAFE (b, b_tmp)
46c6471b 14860 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0 14861 delete_breakpoint (b);
1042e4c0
SS
14862 }
14863 }
14864 else
48649e1b
TT
14865 map_breakpoint_numbers
14866 (arg, [&] (breakpoint *b)
14867 {
14868 iterate_over_related_breakpoints (b, delete_breakpoint);
14869 });
1042e4c0
SS
14870}
14871
197f0a60
TT
14872/* Helper function for trace_pass_command. */
14873
14874static void
d9b3f62e 14875trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
197f0a60 14876{
d9b3f62e 14877 tp->pass_count = count;
76727919 14878 gdb::observers::breakpoint_modified.notify (tp);
197f0a60
TT
14879 if (from_tty)
14880 printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
c1fc2657 14881 tp->number, count);
197f0a60
TT
14882}
14883
1042e4c0
SS
14884/* Set passcount for tracepoint.
14885
14886 First command argument is passcount, second is tracepoint number.
14887 If tracepoint number omitted, apply to most recently defined.
14888 Also accepts special argument "all". */
14889
14890static void
0b39b52e 14891trace_pass_command (const char *args, int from_tty)
1042e4c0 14892{
d9b3f62e 14893 struct tracepoint *t1;
0b39b52e 14894 ULONGEST count;
1042e4c0
SS
14895
14896 if (args == 0 || *args == 0)
3e43a32a
MS
14897 error (_("passcount command requires an "
14898 "argument (count + optional TP num)"));
1042e4c0 14899
0b39b52e 14900 count = strtoulst (args, &args, 10); /* Count comes first, then TP num. */
1042e4c0 14901
529480d0 14902 args = skip_spaces (args);
1042e4c0
SS
14903 if (*args && strncasecmp (args, "all", 3) == 0)
14904 {
d9b3f62e
PA
14905 struct breakpoint *b;
14906
1042e4c0 14907 args += 3; /* Skip special argument "all". */
1042e4c0
SS
14908 if (*args)
14909 error (_("Junk at end of arguments."));
1042e4c0 14910
d9b3f62e 14911 ALL_TRACEPOINTS (b)
197f0a60 14912 {
d9b3f62e 14913 t1 = (struct tracepoint *) b;
197f0a60
TT
14914 trace_pass_set_count (t1, count, from_tty);
14915 }
14916 }
14917 else if (*args == '\0')
1042e4c0 14918 {
5fa1d40e 14919 t1 = get_tracepoint_by_number (&args, NULL);
1042e4c0 14920 if (t1)
197f0a60
TT
14921 trace_pass_set_count (t1, count, from_tty);
14922 }
14923 else
14924 {
bfd28288
PA
14925 number_or_range_parser parser (args);
14926 while (!parser.finished ())
1042e4c0 14927 {
bfd28288 14928 t1 = get_tracepoint_by_number (&args, &parser);
197f0a60
TT
14929 if (t1)
14930 trace_pass_set_count (t1, count, from_tty);
1042e4c0
SS
14931 }
14932 }
1042e4c0
SS
14933}
14934
d9b3f62e 14935struct tracepoint *
1042e4c0
SS
14936get_tracepoint (int num)
14937{
14938 struct breakpoint *t;
14939
14940 ALL_TRACEPOINTS (t)
14941 if (t->number == num)
d9b3f62e 14942 return (struct tracepoint *) t;
1042e4c0
SS
14943
14944 return NULL;
14945}
14946
d5551862
SS
14947/* Find the tracepoint with the given target-side number (which may be
14948 different from the tracepoint number after disconnecting and
14949 reconnecting). */
14950
d9b3f62e 14951struct tracepoint *
d5551862
SS
14952get_tracepoint_by_number_on_target (int num)
14953{
d9b3f62e 14954 struct breakpoint *b;
d5551862 14955
d9b3f62e
PA
14956 ALL_TRACEPOINTS (b)
14957 {
14958 struct tracepoint *t = (struct tracepoint *) b;
14959
14960 if (t->number_on_target == num)
14961 return t;
14962 }
d5551862
SS
14963
14964 return NULL;
14965}
14966
1042e4c0 14967/* Utility: parse a tracepoint number and look it up in the list.
197f0a60 14968 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
5fa1d40e
YQ
14969 If the argument is missing, the most recent tracepoint
14970 (tracepoint_count) is returned. */
14971
d9b3f62e 14972struct tracepoint *
0b39b52e 14973get_tracepoint_by_number (const char **arg,
bfd28288 14974 number_or_range_parser *parser)
1042e4c0 14975{
1042e4c0
SS
14976 struct breakpoint *t;
14977 int tpnum;
0b39b52e 14978 const char *instring = arg == NULL ? NULL : *arg;
1042e4c0 14979
bfd28288 14980 if (parser != NULL)
197f0a60 14981 {
bfd28288
PA
14982 gdb_assert (!parser->finished ());
14983 tpnum = parser->get_number ();
197f0a60
TT
14984 }
14985 else if (arg == NULL || *arg == NULL || ! **arg)
5fa1d40e 14986 tpnum = tracepoint_count;
1042e4c0 14987 else
197f0a60 14988 tpnum = get_number (arg);
1042e4c0
SS
14989
14990 if (tpnum <= 0)
14991 {
14992 if (instring && *instring)
14993 printf_filtered (_("bad tracepoint number at or near '%s'\n"),
14994 instring);
14995 else
5fa1d40e 14996 printf_filtered (_("No previous tracepoint\n"));
1042e4c0
SS
14997 return NULL;
14998 }
14999
15000 ALL_TRACEPOINTS (t)
15001 if (t->number == tpnum)
15002 {
d9b3f62e 15003 return (struct tracepoint *) t;
1042e4c0
SS
15004 }
15005
1042e4c0
SS
15006 printf_unfiltered ("No tracepoint number %d.\n", tpnum);
15007 return NULL;
15008}
15009
d9b3f62e
PA
15010void
15011print_recreate_thread (struct breakpoint *b, struct ui_file *fp)
15012{
15013 if (b->thread != -1)
15014 fprintf_unfiltered (fp, " thread %d", b->thread);
15015
15016 if (b->task != 0)
15017 fprintf_unfiltered (fp, " task %d", b->task);
15018
15019 fprintf_unfiltered (fp, "\n");
15020}
15021
6149aea9
PA
15022/* Save information on user settable breakpoints (watchpoints, etc) to
15023 a new script file named FILENAME. If FILTER is non-NULL, call it
15024 on each breakpoint and only include the ones for which it returns
15025 non-zero. */
15026
1042e4c0 15027static void
4495129a 15028save_breakpoints (const char *filename, int from_tty,
6149aea9 15029 int (*filter) (const struct breakpoint *))
1042e4c0
SS
15030{
15031 struct breakpoint *tp;
6149aea9 15032 int any = 0;
6149aea9 15033 int extra_trace_bits = 0;
1042e4c0 15034
6149aea9
PA
15035 if (filename == 0 || *filename == 0)
15036 error (_("Argument required (file name in which to save)"));
1042e4c0
SS
15037
15038 /* See if we have anything to save. */
6149aea9 15039 ALL_BREAKPOINTS (tp)
1042e4c0 15040 {
6149aea9 15041 /* Skip internal and momentary breakpoints. */
09d682a4 15042 if (!user_breakpoint_p (tp))
6149aea9
PA
15043 continue;
15044
15045 /* If we have a filter, only save the breakpoints it accepts. */
15046 if (filter && !filter (tp))
15047 continue;
15048
15049 any = 1;
15050
15051 if (is_tracepoint (tp))
15052 {
15053 extra_trace_bits = 1;
15054
15055 /* We can stop searching. */
15056 break;
15057 }
1042e4c0 15058 }
6149aea9
PA
15059
15060 if (!any)
1042e4c0 15061 {
6149aea9 15062 warning (_("Nothing to save."));
1042e4c0
SS
15063 return;
15064 }
15065
ee0c3293 15066 gdb::unique_xmalloc_ptr<char> expanded_filename (tilde_expand (filename));
d7e74731
PA
15067
15068 stdio_file fp;
15069
ee0c3293 15070 if (!fp.open (expanded_filename.get (), "w"))
6149aea9 15071 error (_("Unable to open file '%s' for saving (%s)"),
ee0c3293 15072 expanded_filename.get (), safe_strerror (errno));
8bf6485c 15073
6149aea9 15074 if (extra_trace_bits)
d7e74731 15075 save_trace_state_variables (&fp);
8bf6485c 15076
6149aea9 15077 ALL_BREAKPOINTS (tp)
1042e4c0 15078 {
6149aea9 15079 /* Skip internal and momentary breakpoints. */
09d682a4 15080 if (!user_breakpoint_p (tp))
6149aea9 15081 continue;
8bf6485c 15082
6149aea9
PA
15083 /* If we have a filter, only save the breakpoints it accepts. */
15084 if (filter && !filter (tp))
15085 continue;
15086
d7e74731 15087 tp->ops->print_recreate (tp, &fp);
1042e4c0 15088
6149aea9
PA
15089 /* Note, we can't rely on tp->number for anything, as we can't
15090 assume the recreated breakpoint numbers will match. Use $bpnum
15091 instead. */
15092
15093 if (tp->cond_string)
d7e74731 15094 fp.printf (" condition $bpnum %s\n", tp->cond_string);
6149aea9
PA
15095
15096 if (tp->ignore_count)
d7e74731 15097 fp.printf (" ignore $bpnum %d\n", tp->ignore_count);
6149aea9 15098
2d9442cc 15099 if (tp->type != bp_dprintf && tp->commands)
1042e4c0 15100 {
d7e74731 15101 fp.puts (" commands\n");
a7bdde9e 15102
d7e74731 15103 current_uiout->redirect (&fp);
492d29ea 15104 TRY
1042e4c0 15105 {
d1b0a7bf 15106 print_command_lines (current_uiout, tp->commands.get (), 2);
a7bdde9e 15107 }
492d29ea
PA
15108 CATCH (ex, RETURN_MASK_ALL)
15109 {
112e8700 15110 current_uiout->redirect (NULL);
492d29ea
PA
15111 throw_exception (ex);
15112 }
15113 END_CATCH
1042e4c0 15114
112e8700 15115 current_uiout->redirect (NULL);
d7e74731 15116 fp.puts (" end\n");
1042e4c0 15117 }
6149aea9
PA
15118
15119 if (tp->enable_state == bp_disabled)
d7e74731 15120 fp.puts ("disable $bpnum\n");
6149aea9
PA
15121
15122 /* If this is a multi-location breakpoint, check if the locations
15123 should be individually disabled. Watchpoint locations are
15124 special, and not user visible. */
15125 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
15126 {
15127 struct bp_location *loc;
15128 int n = 1;
15129
15130 for (loc = tp->loc; loc != NULL; loc = loc->next, n++)
15131 if (!loc->enabled)
d7e74731 15132 fp.printf ("disable $bpnum.%d\n", n);
6149aea9 15133 }
1042e4c0 15134 }
8bf6485c 15135
6149aea9 15136 if (extra_trace_bits && *default_collect)
d7e74731 15137 fp.printf ("set default-collect %s\n", default_collect);
8bf6485c 15138
1042e4c0 15139 if (from_tty)
ee0c3293 15140 printf_filtered (_("Saved to file '%s'.\n"), expanded_filename.get ());
6149aea9
PA
15141}
15142
15143/* The `save breakpoints' command. */
15144
15145static void
4495129a 15146save_breakpoints_command (const char *args, int from_tty)
6149aea9
PA
15147{
15148 save_breakpoints (args, from_tty, NULL);
15149}
15150
15151/* The `save tracepoints' command. */
15152
15153static void
4495129a 15154save_tracepoints_command (const char *args, int from_tty)
6149aea9
PA
15155{
15156 save_breakpoints (args, from_tty, is_tracepoint);
1042e4c0
SS
15157}
15158
15159/* Create a vector of all tracepoints. */
15160
f51e0e20 15161std::vector<breakpoint *>
eeae04df 15162all_tracepoints (void)
1042e4c0 15163{
f51e0e20 15164 std::vector<breakpoint *> tp_vec;
1042e4c0
SS
15165 struct breakpoint *tp;
15166
15167 ALL_TRACEPOINTS (tp)
15168 {
f51e0e20 15169 tp_vec.push_back (tp);
1042e4c0
SS
15170 }
15171
15172 return tp_vec;
15173}
15174
c906108c 15175\f
629500fa
KS
15176/* This help string is used to consolidate all the help string for specifying
15177 locations used by several commands. */
15178
15179#define LOCATION_HELP_STRING \
15180"Linespecs are colon-separated lists of location parameters, such as\n\
15181source filename, function name, label name, and line number.\n\
15182Example: To specify the start of a label named \"the_top\" in the\n\
15183function \"fact\" in the file \"factorial.c\", use\n\
15184\"factorial.c:fact:the_top\".\n\
15185\n\
15186Address locations begin with \"*\" and specify an exact address in the\n\
15187program. Example: To specify the fourth byte past the start function\n\
15188\"main\", use \"*main + 4\".\n\
15189\n\
15190Explicit locations are similar to linespecs but use an option/argument\n\
15191syntax to specify location parameters.\n\
15192Example: To specify the start of the label named \"the_top\" in the\n\
15193function \"fact\" in the file \"factorial.c\", use \"-source factorial.c\n\
a20714ff
PA
15194-function fact -label the_top\".\n\
15195\n\
15196By default, a specified function is matched against the program's\n\
15197functions in all scopes. For C++, this means in all namespaces and\n\
15198classes. For Ada, this means in all packages. E.g., in C++,\n\
15199\"func()\" matches \"A::func()\", \"A::B::func()\", etc. The\n\
15200\"-qualified\" flag overrides this behavior, making GDB interpret the\n\
15201specified name as a complete fully-qualified name instead.\n"
629500fa 15202
4a64f543
MS
15203/* This help string is used for the break, hbreak, tbreak and thbreak
15204 commands. It is defined as a macro to prevent duplication.
15205 COMMAND should be a string constant containing the name of the
15206 command. */
629500fa 15207
31e2b00f 15208#define BREAK_ARGS_HELP(command) \
fb7b5af4
SDJ
15209command" [PROBE_MODIFIER] [LOCATION] [thread THREADNUM] [if CONDITION]\n\
15210PROBE_MODIFIER shall be present if the command is to be placed in a\n\
15211probe point. Accepted values are `-probe' (for a generic, automatically\n\
d4777acb
JM
15212guessed probe type), `-probe-stap' (for a SystemTap probe) or \n\
15213`-probe-dtrace' (for a DTrace probe).\n\
629500fa
KS
15214LOCATION may be a linespec, address, or explicit location as described\n\
15215below.\n\
15216\n\
dc10affe
PA
15217With no LOCATION, uses current execution address of the selected\n\
15218stack frame. This is useful for breaking on return to a stack frame.\n\
31e2b00f
AS
15219\n\
15220THREADNUM is the number from \"info threads\".\n\
15221CONDITION is a boolean expression.\n\
629500fa 15222\n" LOCATION_HELP_STRING "\n\
d41c0fc8
PA
15223Multiple breakpoints at one place are permitted, and useful if their\n\
15224conditions are different.\n\
31e2b00f
AS
15225\n\
15226Do \"help breakpoints\" for info on other commands dealing with breakpoints."
15227
44feb3ce
TT
15228/* List of subcommands for "catch". */
15229static struct cmd_list_element *catch_cmdlist;
15230
15231/* List of subcommands for "tcatch". */
15232static struct cmd_list_element *tcatch_cmdlist;
15233
9ac4176b 15234void
a121b7c1 15235add_catch_command (const char *name, const char *docstring,
eb4c3f4a 15236 cmd_const_sfunc_ftype *sfunc,
625e8578 15237 completer_ftype *completer,
44feb3ce
TT
15238 void *user_data_catch,
15239 void *user_data_tcatch)
15240{
15241 struct cmd_list_element *command;
15242
0450cc4c 15243 command = add_cmd (name, class_breakpoint, docstring,
44feb3ce
TT
15244 &catch_cmdlist);
15245 set_cmd_sfunc (command, sfunc);
15246 set_cmd_context (command, user_data_catch);
a96d9b2e 15247 set_cmd_completer (command, completer);
44feb3ce 15248
0450cc4c 15249 command = add_cmd (name, class_breakpoint, docstring,
44feb3ce
TT
15250 &tcatch_cmdlist);
15251 set_cmd_sfunc (command, sfunc);
15252 set_cmd_context (command, user_data_tcatch);
a96d9b2e 15253 set_cmd_completer (command, completer);
44feb3ce
TT
15254}
15255
6149aea9 15256static void
981a3fb3 15257save_command (const char *arg, int from_tty)
6149aea9 15258{
3e43a32a
MS
15259 printf_unfiltered (_("\"save\" must be followed by "
15260 "the name of a save subcommand.\n"));
635c7e8a 15261 help_list (save_cmdlist, "save ", all_commands, gdb_stdout);
6149aea9
PA
15262}
15263
84f4c1fe
PM
15264struct breakpoint *
15265iterate_over_breakpoints (int (*callback) (struct breakpoint *, void *),
15266 void *data)
15267{
35df4500 15268 struct breakpoint *b, *b_tmp;
84f4c1fe 15269
35df4500 15270 ALL_BREAKPOINTS_SAFE (b, b_tmp)
84f4c1fe
PM
15271 {
15272 if ((*callback) (b, data))
15273 return b;
15274 }
15275
15276 return NULL;
15277}
15278
0574c78f
GB
15279/* Zero if any of the breakpoint's locations could be a location where
15280 functions have been inlined, nonzero otherwise. */
15281
15282static int
15283is_non_inline_function (struct breakpoint *b)
15284{
15285 /* The shared library event breakpoint is set on the address of a
15286 non-inline function. */
15287 if (b->type == bp_shlib_event)
15288 return 1;
15289
15290 return 0;
15291}
15292
15293/* Nonzero if the specified PC cannot be a location where functions
15294 have been inlined. */
15295
15296int
accd0bcd 15297pc_at_non_inline_function (const address_space *aspace, CORE_ADDR pc,
09ac7c10 15298 const struct target_waitstatus *ws)
0574c78f
GB
15299{
15300 struct breakpoint *b;
15301 struct bp_location *bl;
15302
15303 ALL_BREAKPOINTS (b)
15304 {
15305 if (!is_non_inline_function (b))
15306 continue;
15307
15308 for (bl = b->loc; bl != NULL; bl = bl->next)
15309 {
15310 if (!bl->shlib_disabled
09ac7c10 15311 && bpstat_check_location (bl, aspace, pc, ws))
0574c78f
GB
15312 return 1;
15313 }
15314 }
15315
15316 return 0;
15317}
15318
2f202fde
JK
15319/* Remove any references to OBJFILE which is going to be freed. */
15320
15321void
15322breakpoint_free_objfile (struct objfile *objfile)
15323{
15324 struct bp_location **locp, *loc;
15325
15326 ALL_BP_LOCATIONS (loc, locp)
eb822aa6 15327 if (loc->symtab != NULL && SYMTAB_OBJFILE (loc->symtab) == objfile)
2f202fde
JK
15328 loc->symtab = NULL;
15329}
15330
2060206e
PA
15331void
15332initialize_breakpoint_ops (void)
15333{
15334 static int initialized = 0;
15335
15336 struct breakpoint_ops *ops;
15337
15338 if (initialized)
15339 return;
15340 initialized = 1;
15341
15342 /* The breakpoint_ops structure to be inherit by all kinds of
15343 breakpoints (real breakpoints, i.e., user "break" breakpoints,
15344 internal and momentary breakpoints, etc.). */
15345 ops = &bkpt_base_breakpoint_ops;
15346 *ops = base_breakpoint_ops;
15347 ops->re_set = bkpt_re_set;
15348 ops->insert_location = bkpt_insert_location;
15349 ops->remove_location = bkpt_remove_location;
15350 ops->breakpoint_hit = bkpt_breakpoint_hit;
5f700d83 15351 ops->create_sals_from_location = bkpt_create_sals_from_location;
983af33b 15352 ops->create_breakpoints_sal = bkpt_create_breakpoints_sal;
5f700d83 15353 ops->decode_location = bkpt_decode_location;
2060206e
PA
15354
15355 /* The breakpoint_ops structure to be used in regular breakpoints. */
15356 ops = &bkpt_breakpoint_ops;
15357 *ops = bkpt_base_breakpoint_ops;
15358 ops->re_set = bkpt_re_set;
15359 ops->resources_needed = bkpt_resources_needed;
15360 ops->print_it = bkpt_print_it;
15361 ops->print_mention = bkpt_print_mention;
15362 ops->print_recreate = bkpt_print_recreate;
15363
15364 /* Ranged breakpoints. */
15365 ops = &ranged_breakpoint_ops;
15366 *ops = bkpt_breakpoint_ops;
15367 ops->breakpoint_hit = breakpoint_hit_ranged_breakpoint;
15368 ops->resources_needed = resources_needed_ranged_breakpoint;
15369 ops->print_it = print_it_ranged_breakpoint;
15370 ops->print_one = print_one_ranged_breakpoint;
15371 ops->print_one_detail = print_one_detail_ranged_breakpoint;
15372 ops->print_mention = print_mention_ranged_breakpoint;
15373 ops->print_recreate = print_recreate_ranged_breakpoint;
15374
15375 /* Internal breakpoints. */
15376 ops = &internal_breakpoint_ops;
15377 *ops = bkpt_base_breakpoint_ops;
15378 ops->re_set = internal_bkpt_re_set;
15379 ops->check_status = internal_bkpt_check_status;
15380 ops->print_it = internal_bkpt_print_it;
15381 ops->print_mention = internal_bkpt_print_mention;
15382
15383 /* Momentary breakpoints. */
15384 ops = &momentary_breakpoint_ops;
15385 *ops = bkpt_base_breakpoint_ops;
15386 ops->re_set = momentary_bkpt_re_set;
15387 ops->check_status = momentary_bkpt_check_status;
15388 ops->print_it = momentary_bkpt_print_it;
15389 ops->print_mention = momentary_bkpt_print_mention;
15390
55aa24fb
SDJ
15391 /* Probe breakpoints. */
15392 ops = &bkpt_probe_breakpoint_ops;
15393 *ops = bkpt_breakpoint_ops;
15394 ops->insert_location = bkpt_probe_insert_location;
15395 ops->remove_location = bkpt_probe_remove_location;
5f700d83
KS
15396 ops->create_sals_from_location = bkpt_probe_create_sals_from_location;
15397 ops->decode_location = bkpt_probe_decode_location;
55aa24fb 15398
2060206e
PA
15399 /* Watchpoints. */
15400 ops = &watchpoint_breakpoint_ops;
15401 *ops = base_breakpoint_ops;
15402 ops->re_set = re_set_watchpoint;
15403 ops->insert_location = insert_watchpoint;
15404 ops->remove_location = remove_watchpoint;
15405 ops->breakpoint_hit = breakpoint_hit_watchpoint;
15406 ops->check_status = check_status_watchpoint;
15407 ops->resources_needed = resources_needed_watchpoint;
15408 ops->works_in_software_mode = works_in_software_mode_watchpoint;
15409 ops->print_it = print_it_watchpoint;
15410 ops->print_mention = print_mention_watchpoint;
15411 ops->print_recreate = print_recreate_watchpoint;
427cd150 15412 ops->explains_signal = explains_signal_watchpoint;
2060206e
PA
15413
15414 /* Masked watchpoints. */
15415 ops = &masked_watchpoint_breakpoint_ops;
15416 *ops = watchpoint_breakpoint_ops;
15417 ops->insert_location = insert_masked_watchpoint;
15418 ops->remove_location = remove_masked_watchpoint;
15419 ops->resources_needed = resources_needed_masked_watchpoint;
15420 ops->works_in_software_mode = works_in_software_mode_masked_watchpoint;
15421 ops->print_it = print_it_masked_watchpoint;
15422 ops->print_one_detail = print_one_detail_masked_watchpoint;
15423 ops->print_mention = print_mention_masked_watchpoint;
15424 ops->print_recreate = print_recreate_masked_watchpoint;
15425
15426 /* Tracepoints. */
15427 ops = &tracepoint_breakpoint_ops;
15428 *ops = base_breakpoint_ops;
15429 ops->re_set = tracepoint_re_set;
15430 ops->breakpoint_hit = tracepoint_breakpoint_hit;
15431 ops->print_one_detail = tracepoint_print_one_detail;
15432 ops->print_mention = tracepoint_print_mention;
15433 ops->print_recreate = tracepoint_print_recreate;
5f700d83 15434 ops->create_sals_from_location = tracepoint_create_sals_from_location;
983af33b 15435 ops->create_breakpoints_sal = tracepoint_create_breakpoints_sal;
5f700d83 15436 ops->decode_location = tracepoint_decode_location;
983af33b 15437
55aa24fb
SDJ
15438 /* Probe tracepoints. */
15439 ops = &tracepoint_probe_breakpoint_ops;
15440 *ops = tracepoint_breakpoint_ops;
5f700d83
KS
15441 ops->create_sals_from_location = tracepoint_probe_create_sals_from_location;
15442 ops->decode_location = tracepoint_probe_decode_location;
55aa24fb 15443
983af33b
SDJ
15444 /* Static tracepoints with marker (`-m'). */
15445 ops = &strace_marker_breakpoint_ops;
15446 *ops = tracepoint_breakpoint_ops;
5f700d83 15447 ops->create_sals_from_location = strace_marker_create_sals_from_location;
983af33b 15448 ops->create_breakpoints_sal = strace_marker_create_breakpoints_sal;
5f700d83 15449 ops->decode_location = strace_marker_decode_location;
2060206e
PA
15450
15451 /* Fork catchpoints. */
15452 ops = &catch_fork_breakpoint_ops;
15453 *ops = base_breakpoint_ops;
15454 ops->insert_location = insert_catch_fork;
15455 ops->remove_location = remove_catch_fork;
15456 ops->breakpoint_hit = breakpoint_hit_catch_fork;
15457 ops->print_it = print_it_catch_fork;
15458 ops->print_one = print_one_catch_fork;
15459 ops->print_mention = print_mention_catch_fork;
15460 ops->print_recreate = print_recreate_catch_fork;
15461
15462 /* Vfork catchpoints. */
15463 ops = &catch_vfork_breakpoint_ops;
15464 *ops = base_breakpoint_ops;
15465 ops->insert_location = insert_catch_vfork;
15466 ops->remove_location = remove_catch_vfork;
15467 ops->breakpoint_hit = breakpoint_hit_catch_vfork;
15468 ops->print_it = print_it_catch_vfork;
15469 ops->print_one = print_one_catch_vfork;
15470 ops->print_mention = print_mention_catch_vfork;
15471 ops->print_recreate = print_recreate_catch_vfork;
15472
15473 /* Exec catchpoints. */
15474 ops = &catch_exec_breakpoint_ops;
15475 *ops = base_breakpoint_ops;
2060206e
PA
15476 ops->insert_location = insert_catch_exec;
15477 ops->remove_location = remove_catch_exec;
15478 ops->breakpoint_hit = breakpoint_hit_catch_exec;
15479 ops->print_it = print_it_catch_exec;
15480 ops->print_one = print_one_catch_exec;
15481 ops->print_mention = print_mention_catch_exec;
15482 ops->print_recreate = print_recreate_catch_exec;
15483
edcc5120
TT
15484 /* Solib-related catchpoints. */
15485 ops = &catch_solib_breakpoint_ops;
15486 *ops = base_breakpoint_ops;
edcc5120
TT
15487 ops->insert_location = insert_catch_solib;
15488 ops->remove_location = remove_catch_solib;
15489 ops->breakpoint_hit = breakpoint_hit_catch_solib;
15490 ops->check_status = check_status_catch_solib;
15491 ops->print_it = print_it_catch_solib;
15492 ops->print_one = print_one_catch_solib;
15493 ops->print_mention = print_mention_catch_solib;
15494 ops->print_recreate = print_recreate_catch_solib;
e7e0cddf
SS
15495
15496 ops = &dprintf_breakpoint_ops;
15497 *ops = bkpt_base_breakpoint_ops;
5c2b4418 15498 ops->re_set = dprintf_re_set;
e7e0cddf
SS
15499 ops->resources_needed = bkpt_resources_needed;
15500 ops->print_it = bkpt_print_it;
15501 ops->print_mention = bkpt_print_mention;
2d9442cc 15502 ops->print_recreate = dprintf_print_recreate;
9d6e6e84 15503 ops->after_condition_true = dprintf_after_condition_true;
cd1608cc 15504 ops->breakpoint_hit = dprintf_breakpoint_hit;
2060206e
PA
15505}
15506
8bfd80db
YQ
15507/* Chain containing all defined "enable breakpoint" subcommands. */
15508
15509static struct cmd_list_element *enablebreaklist = NULL;
15510
c906108c 15511void
fba45db2 15512_initialize_breakpoint (void)
c906108c
SS
15513{
15514 struct cmd_list_element *c;
15515
2060206e
PA
15516 initialize_breakpoint_ops ();
15517
76727919
TT
15518 gdb::observers::solib_unloaded.attach (disable_breakpoints_in_unloaded_shlib);
15519 gdb::observers::free_objfile.attach (disable_breakpoints_in_freed_objfile);
15520 gdb::observers::memory_changed.attach (invalidate_bp_value_on_memory_change);
84acb35a 15521
55aa24fb 15522 breakpoint_objfile_key
43dce439 15523 = register_objfile_data_with_cleanup (NULL, free_breakpoint_objfile_data);
17450429 15524
c906108c
SS
15525 breakpoint_chain = 0;
15526 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
15527 before a breakpoint is set. */
15528 breakpoint_count = 0;
15529
1042e4c0
SS
15530 tracepoint_count = 0;
15531
1bedd215
AC
15532 add_com ("ignore", class_breakpoint, ignore_command, _("\
15533Set ignore-count of breakpoint number N to COUNT.\n\
15534Usage is `ignore N COUNT'."));
c906108c 15535
1bedd215 15536 add_com ("commands", class_breakpoint, commands_command, _("\
18da0c51
MG
15537Set commands to be executed when the given breakpoints are hit.\n\
15538Give a space-separated breakpoint list as argument after \"commands\".\n\
15539A list element can be a breakpoint number (e.g. `5') or a range of numbers\n\
15540(e.g. `5-7').\n\
c906108c
SS
15541With no argument, the targeted breakpoint is the last one set.\n\
15542The commands themselves follow starting on the next line.\n\
15543Type a line containing \"end\" to indicate the end of them.\n\
15544Give \"silent\" as the first line to make the breakpoint silent;\n\
1bedd215 15545then no output is printed when it is hit, except what the commands print."));
c906108c 15546
d55637df 15547 c = add_com ("condition", class_breakpoint, condition_command, _("\
1bedd215 15548Specify breakpoint number N to break only if COND is true.\n\
c906108c 15549Usage is `condition N COND', where N is an integer and COND is an\n\
1bedd215 15550expression to be evaluated whenever breakpoint N is reached."));
d55637df 15551 set_cmd_completer (c, condition_completer);
c906108c 15552
1bedd215 15553 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
31e2b00f 15554Set a temporary breakpoint.\n\
c906108c
SS
15555Like \"break\" except the breakpoint is only temporary,\n\
15556so it will be deleted when hit. Equivalent to \"break\" followed\n\
31e2b00f
AS
15557by using \"enable delete\" on the breakpoint number.\n\
15558\n"
15559BREAK_ARGS_HELP ("tbreak")));
5ba2abeb 15560 set_cmd_completer (c, location_completer);
c94fdfd0 15561
1bedd215 15562 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
a3be7890 15563Set a hardware assisted breakpoint.\n\
c906108c 15564Like \"break\" except the breakpoint requires hardware support,\n\
31e2b00f
AS
15565some target hardware may not have this support.\n\
15566\n"
15567BREAK_ARGS_HELP ("hbreak")));
5ba2abeb 15568 set_cmd_completer (c, location_completer);
c906108c 15569
1bedd215 15570 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
31e2b00f 15571Set a temporary hardware assisted breakpoint.\n\
c906108c 15572Like \"hbreak\" except the breakpoint is only temporary,\n\
31e2b00f
AS
15573so it will be deleted when hit.\n\
15574\n"
15575BREAK_ARGS_HELP ("thbreak")));
5ba2abeb 15576 set_cmd_completer (c, location_completer);
c906108c 15577
1bedd215
AC
15578 add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
15579Enable some breakpoints.\n\
c906108c
SS
15580Give breakpoint numbers (separated by spaces) as arguments.\n\
15581With no subcommand, breakpoints are enabled until you command otherwise.\n\
15582This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 15583With a subcommand you can enable temporarily."),
c906108c 15584 &enablelist, "enable ", 1, &cmdlist);
c906108c
SS
15585
15586 add_com_alias ("en", "enable", class_breakpoint, 1);
15587
84951ab5 15588 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
1bedd215 15589Enable some breakpoints.\n\
c906108c
SS
15590Give breakpoint numbers (separated by spaces) as arguments.\n\
15591This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 15592May be abbreviated to simply \"enable\".\n"),
c5aa993b 15593 &enablebreaklist, "enable breakpoints ", 1, &enablelist);
c906108c 15594
1a966eab
AC
15595 add_cmd ("once", no_class, enable_once_command, _("\
15596Enable breakpoints for one hit. Give breakpoint numbers.\n\
15597If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
c906108c
SS
15598 &enablebreaklist);
15599
1a966eab
AC
15600 add_cmd ("delete", no_class, enable_delete_command, _("\
15601Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
15602If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
15603 &enablebreaklist);
15604
816338b5
SS
15605 add_cmd ("count", no_class, enable_count_command, _("\
15606Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
15607If a breakpoint is hit while enabled in this fashion,\n\
15608the count is decremented; when it reaches zero, the breakpoint is disabled."),
15609 &enablebreaklist);
15610
1a966eab
AC
15611 add_cmd ("delete", no_class, enable_delete_command, _("\
15612Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
15613If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
15614 &enablelist);
15615
1a966eab
AC
15616 add_cmd ("once", no_class, enable_once_command, _("\
15617Enable breakpoints for one hit. Give breakpoint numbers.\n\
15618If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
816338b5
SS
15619 &enablelist);
15620
15621 add_cmd ("count", no_class, enable_count_command, _("\
15622Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
15623If a breakpoint is hit while enabled in this fashion,\n\
15624the count is decremented; when it reaches zero, the breakpoint is disabled."),
c906108c
SS
15625 &enablelist);
15626
1bedd215
AC
15627 add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
15628Disable some breakpoints.\n\
c906108c
SS
15629Arguments are breakpoint numbers with spaces in between.\n\
15630To disable all breakpoints, give no argument.\n\
64b9b334 15631A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
c906108c
SS
15632 &disablelist, "disable ", 1, &cmdlist);
15633 add_com_alias ("dis", "disable", class_breakpoint, 1);
15634 add_com_alias ("disa", "disable", class_breakpoint, 1);
c906108c 15635
1a966eab
AC
15636 add_cmd ("breakpoints", class_alias, disable_command, _("\
15637Disable some breakpoints.\n\
c906108c
SS
15638Arguments are breakpoint numbers with spaces in between.\n\
15639To disable all breakpoints, give no argument.\n\
64b9b334 15640A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
1a966eab 15641This command may be abbreviated \"disable\"."),
c906108c
SS
15642 &disablelist);
15643
1bedd215
AC
15644 add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
15645Delete some breakpoints or auto-display expressions.\n\
c906108c
SS
15646Arguments are breakpoint numbers with spaces in between.\n\
15647To delete all breakpoints, give no argument.\n\
15648\n\
15649Also a prefix command for deletion of other GDB objects.\n\
1bedd215 15650The \"unset\" command is also an alias for \"delete\"."),
c906108c
SS
15651 &deletelist, "delete ", 1, &cmdlist);
15652 add_com_alias ("d", "delete", class_breakpoint, 1);
7f198e01 15653 add_com_alias ("del", "delete", class_breakpoint, 1);
c906108c 15654
1a966eab
AC
15655 add_cmd ("breakpoints", class_alias, delete_command, _("\
15656Delete some breakpoints or auto-display expressions.\n\
c906108c
SS
15657Arguments are breakpoint numbers with spaces in between.\n\
15658To delete all breakpoints, give no argument.\n\
1a966eab 15659This command may be abbreviated \"delete\"."),
c906108c
SS
15660 &deletelist);
15661
1bedd215 15662 add_com ("clear", class_breakpoint, clear_command, _("\
629500fa
KS
15663Clear breakpoint at specified location.\n\
15664Argument may be a linespec, explicit, or address location as described below.\n\
1bedd215
AC
15665\n\
15666With no argument, clears all breakpoints in the line that the selected frame\n\
629500fa
KS
15667is executing in.\n"
15668"\n" LOCATION_HELP_STRING "\n\
1bedd215 15669See also the \"delete\" command which clears breakpoints by number."));
a6cc4789 15670 add_com_alias ("cl", "clear", class_breakpoint, 1);
c906108c 15671
1bedd215 15672 c = add_com ("break", class_breakpoint, break_command, _("\
629500fa 15673Set breakpoint at specified location.\n"
31e2b00f 15674BREAK_ARGS_HELP ("break")));
5ba2abeb 15675 set_cmd_completer (c, location_completer);
c94fdfd0 15676
c906108c
SS
15677 add_com_alias ("b", "break", class_run, 1);
15678 add_com_alias ("br", "break", class_run, 1);
15679 add_com_alias ("bre", "break", class_run, 1);
15680 add_com_alias ("brea", "break", class_run, 1);
15681
c906108c
SS
15682 if (dbx_commands)
15683 {
1bedd215
AC
15684 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
15685Break in function/address or break at a line in the current file."),
c5aa993b
JM
15686 &stoplist, "stop ", 1, &cmdlist);
15687 add_cmd ("in", class_breakpoint, stopin_command,
1a966eab 15688 _("Break in function or address."), &stoplist);
c5aa993b 15689 add_cmd ("at", class_breakpoint, stopat_command,
1a966eab 15690 _("Break at a line in the current file."), &stoplist);
11db9430 15691 add_com ("status", class_info, info_breakpoints_command, _("\
1bedd215 15692Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
15693The \"Type\" column indicates one of:\n\
15694\tbreakpoint - normal breakpoint\n\
15695\twatchpoint - watchpoint\n\
15696The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15697the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15698breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
15699address and file/line number respectively.\n\
15700\n\
15701Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
15702are set to the address of the last breakpoint listed unless the command\n\
15703is prefixed with \"server \".\n\n\
c906108c 15704Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 15705breakpoint set."));
c906108c
SS
15706 }
15707
11db9430 15708 add_info ("breakpoints", info_breakpoints_command, _("\
e5a67952 15709Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
c906108c
SS
15710The \"Type\" column indicates one of:\n\
15711\tbreakpoint - normal breakpoint\n\
15712\twatchpoint - watchpoint\n\
15713The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15714the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15715breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
15716address and file/line number respectively.\n\
15717\n\
15718Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
15719are set to the address of the last breakpoint listed unless the command\n\
15720is prefixed with \"server \".\n\n\
c906108c 15721Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 15722breakpoint set."));
c906108c 15723
6b04bdb7
MS
15724 add_info_alias ("b", "breakpoints", 1);
15725
1a966eab
AC
15726 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
15727Status of all breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
15728The \"Type\" column indicates one of:\n\
15729\tbreakpoint - normal breakpoint\n\
15730\twatchpoint - watchpoint\n\
15731\tlongjmp - internal breakpoint used to step through longjmp()\n\
15732\tlongjmp resume - internal breakpoint at the target of longjmp()\n\
15733\tuntil - internal breakpoint used by the \"until\" command\n\
1a966eab
AC
15734\tfinish - internal breakpoint used by the \"finish\" command\n\
15735The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
c906108c
SS
15736the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15737breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1a966eab
AC
15738address and file/line number respectively.\n\
15739\n\
15740Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
15741are set to the address of the last breakpoint listed unless the command\n\
15742is prefixed with \"server \".\n\n\
c906108c 15743Convenience variable \"$bpnum\" contains the number of the last\n\
1a966eab 15744breakpoint set."),
c906108c
SS
15745 &maintenanceinfolist);
15746
44feb3ce
TT
15747 add_prefix_cmd ("catch", class_breakpoint, catch_command, _("\
15748Set catchpoints to catch events."),
15749 &catch_cmdlist, "catch ",
15750 0/*allow-unknown*/, &cmdlist);
15751
15752 add_prefix_cmd ("tcatch", class_breakpoint, tcatch_command, _("\
15753Set temporary catchpoints to catch events."),
15754 &tcatch_cmdlist, "tcatch ",
15755 0/*allow-unknown*/, &cmdlist);
15756
44feb3ce
TT
15757 add_catch_command ("fork", _("Catch calls to fork."),
15758 catch_fork_command_1,
a96d9b2e 15759 NULL,
44feb3ce
TT
15760 (void *) (uintptr_t) catch_fork_permanent,
15761 (void *) (uintptr_t) catch_fork_temporary);
15762 add_catch_command ("vfork", _("Catch calls to vfork."),
15763 catch_fork_command_1,
a96d9b2e 15764 NULL,
44feb3ce
TT
15765 (void *) (uintptr_t) catch_vfork_permanent,
15766 (void *) (uintptr_t) catch_vfork_temporary);
15767 add_catch_command ("exec", _("Catch calls to exec."),
15768 catch_exec_command_1,
a96d9b2e
SDJ
15769 NULL,
15770 CATCH_PERMANENT,
15771 CATCH_TEMPORARY);
edcc5120
TT
15772 add_catch_command ("load", _("Catch loads of shared libraries.\n\
15773Usage: catch load [REGEX]\n\
15774If REGEX is given, only stop for libraries matching the regular expression."),
15775 catch_load_command_1,
15776 NULL,
15777 CATCH_PERMANENT,
15778 CATCH_TEMPORARY);
15779 add_catch_command ("unload", _("Catch unloads of shared libraries.\n\
15780Usage: catch unload [REGEX]\n\
15781If REGEX is given, only stop for libraries matching the regular expression."),
15782 catch_unload_command_1,
15783 NULL,
15784 CATCH_PERMANENT,
15785 CATCH_TEMPORARY);
c5aa993b 15786
1bedd215
AC
15787 c = add_com ("watch", class_breakpoint, watch_command, _("\
15788Set a watchpoint for an expression.\n\
06a64a0b 15789Usage: watch [-l|-location] EXPRESSION\n\
c906108c 15790A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
15791an expression changes.\n\
15792If -l or -location is given, this evaluates EXPRESSION and watches\n\
15793the memory to which it refers."));
65d12d83 15794 set_cmd_completer (c, expression_completer);
c906108c 15795
1bedd215
AC
15796 c = add_com ("rwatch", class_breakpoint, rwatch_command, _("\
15797Set a read watchpoint for an expression.\n\
06a64a0b 15798Usage: rwatch [-l|-location] EXPRESSION\n\
c906108c 15799A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
15800an expression is read.\n\
15801If -l or -location is given, this evaluates EXPRESSION and watches\n\
15802the memory to which it refers."));
65d12d83 15803 set_cmd_completer (c, expression_completer);
c906108c 15804
1bedd215
AC
15805 c = add_com ("awatch", class_breakpoint, awatch_command, _("\
15806Set a watchpoint for an expression.\n\
06a64a0b 15807Usage: awatch [-l|-location] EXPRESSION\n\
c906108c 15808A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
15809an expression is either read or written.\n\
15810If -l or -location is given, this evaluates EXPRESSION and watches\n\
15811the memory to which it refers."));
65d12d83 15812 set_cmd_completer (c, expression_completer);
c906108c 15813
11db9430 15814 add_info ("watchpoints", info_watchpoints_command, _("\
e5a67952 15815Status of specified watchpoints (all watchpoints if no argument)."));
c906108c 15816
920d2a44
AC
15817 /* XXX: cagney/2005-02-23: This should be a boolean, and should
15818 respond to changes - contrary to the description. */
85c07804
AC
15819 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
15820 &can_use_hw_watchpoints, _("\
15821Set debugger's willingness to use watchpoint hardware."), _("\
15822Show debugger's willingness to use watchpoint hardware."), _("\
c906108c
SS
15823If zero, gdb will not use hardware for new watchpoints, even if\n\
15824such is available. (However, any hardware watchpoints that were\n\
15825created before setting this to nonzero, will continue to use watchpoint\n\
85c07804
AC
15826hardware.)"),
15827 NULL,
920d2a44 15828 show_can_use_hw_watchpoints,
85c07804 15829 &setlist, &showlist);
c906108c
SS
15830
15831 can_use_hw_watchpoints = 1;
fa8d40ab 15832
1042e4c0
SS
15833 /* Tracepoint manipulation commands. */
15834
15835 c = add_com ("trace", class_breakpoint, trace_command, _("\
629500fa 15836Set a tracepoint at specified location.\n\
1042e4c0
SS
15837\n"
15838BREAK_ARGS_HELP ("trace") "\n\
15839Do \"help tracepoints\" for info on other tracepoint commands."));
15840 set_cmd_completer (c, location_completer);
15841
15842 add_com_alias ("tp", "trace", class_alias, 0);
15843 add_com_alias ("tr", "trace", class_alias, 1);
15844 add_com_alias ("tra", "trace", class_alias, 1);
15845 add_com_alias ("trac", "trace", class_alias, 1);
15846
7a697b8d 15847 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
629500fa 15848Set a fast tracepoint at specified location.\n\
7a697b8d
SS
15849\n"
15850BREAK_ARGS_HELP ("ftrace") "\n\
15851Do \"help tracepoints\" for info on other tracepoint commands."));
15852 set_cmd_completer (c, location_completer);
15853
0fb4aa4b 15854 c = add_com ("strace", class_breakpoint, strace_command, _("\
629500fa 15855Set a static tracepoint at location or marker.\n\
0fb4aa4b
PA
15856\n\
15857strace [LOCATION] [if CONDITION]\n\
629500fa
KS
15858LOCATION may be a linespec, explicit, or address location (described below) \n\
15859or -m MARKER_ID.\n\n\
15860If a marker id is specified, probe the marker with that name. With\n\
15861no LOCATION, uses current execution address of the selected stack frame.\n\
0fb4aa4b
PA
15862Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
15863This collects arbitrary user data passed in the probe point call to the\n\
15864tracing library. You can inspect it when analyzing the trace buffer,\n\
15865by printing the $_sdata variable like any other convenience variable.\n\
15866\n\
15867CONDITION is a boolean expression.\n\
629500fa 15868\n" LOCATION_HELP_STRING "\n\
d41c0fc8
PA
15869Multiple tracepoints at one place are permitted, and useful if their\n\
15870conditions are different.\n\
0fb4aa4b
PA
15871\n\
15872Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
15873Do \"help tracepoints\" for info on other tracepoint commands."));
15874 set_cmd_completer (c, location_completer);
15875
11db9430 15876 add_info ("tracepoints", info_tracepoints_command, _("\
e5a67952 15877Status of specified tracepoints (all tracepoints if no argument).\n\
1042e4c0
SS
15878Convenience variable \"$tpnum\" contains the number of the\n\
15879last tracepoint set."));
15880
15881 add_info_alias ("tp", "tracepoints", 1);
15882
15883 add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
15884Delete specified tracepoints.\n\
15885Arguments are tracepoint numbers, separated by spaces.\n\
15886No argument means delete all tracepoints."),
15887 &deletelist);
7e20dfcd 15888 add_alias_cmd ("tr", "tracepoints", class_trace, 1, &deletelist);
1042e4c0
SS
15889
15890 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
15891Disable specified tracepoints.\n\
15892Arguments are tracepoint numbers, separated by spaces.\n\
15893No argument means disable all tracepoints."),
15894 &disablelist);
15895 deprecate_cmd (c, "disable");
15896
15897 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
15898Enable specified tracepoints.\n\
15899Arguments are tracepoint numbers, separated by spaces.\n\
15900No argument means enable all tracepoints."),
15901 &enablelist);
15902 deprecate_cmd (c, "enable");
15903
15904 add_com ("passcount", class_trace, trace_pass_command, _("\
15905Set the passcount for a tracepoint.\n\
15906The trace will end when the tracepoint has been passed 'count' times.\n\
15907Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
15908if TPNUM is omitted, passcount refers to the last tracepoint defined."));
15909
6149aea9
PA
15910 add_prefix_cmd ("save", class_breakpoint, save_command,
15911 _("Save breakpoint definitions as a script."),
15912 &save_cmdlist, "save ",
15913 0/*allow-unknown*/, &cmdlist);
15914
15915 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
15916Save current breakpoint definitions as a script.\n\
cce7e648 15917This includes all types of breakpoints (breakpoints, watchpoints,\n\
6149aea9
PA
15918catchpoints, tracepoints). Use the 'source' command in another debug\n\
15919session to restore them."),
15920 &save_cmdlist);
15921 set_cmd_completer (c, filename_completer);
15922
15923 c = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
1042e4c0 15924Save current tracepoint definitions as a script.\n\
6149aea9
PA
15925Use the 'source' command in another debug session to restore them."),
15926 &save_cmdlist);
1042e4c0
SS
15927 set_cmd_completer (c, filename_completer);
15928
6149aea9
PA
15929 c = add_com_alias ("save-tracepoints", "save tracepoints", class_trace, 0);
15930 deprecate_cmd (c, "save tracepoints");
15931
1bedd215 15932 add_prefix_cmd ("breakpoint", class_maintenance, set_breakpoint_cmd, _("\
fa8d40ab
JJ
15933Breakpoint specific settings\n\
15934Configure various breakpoint-specific variables such as\n\
1bedd215 15935pending breakpoint behavior"),
fa8d40ab
JJ
15936 &breakpoint_set_cmdlist, "set breakpoint ",
15937 0/*allow-unknown*/, &setlist);
1bedd215 15938 add_prefix_cmd ("breakpoint", class_maintenance, show_breakpoint_cmd, _("\
fa8d40ab
JJ
15939Breakpoint specific settings\n\
15940Configure various breakpoint-specific variables such as\n\
1bedd215 15941pending breakpoint behavior"),
fa8d40ab
JJ
15942 &breakpoint_show_cmdlist, "show breakpoint ",
15943 0/*allow-unknown*/, &showlist);
15944
7915a72c
AC
15945 add_setshow_auto_boolean_cmd ("pending", no_class,
15946 &pending_break_support, _("\
15947Set debugger's behavior regarding pending breakpoints."), _("\
15948Show debugger's behavior regarding pending breakpoints."), _("\
6e1d7d6c
AC
15949If on, an unrecognized breakpoint location will cause gdb to create a\n\
15950pending breakpoint. If off, an unrecognized breakpoint location results in\n\
15951an error. If auto, an unrecognized breakpoint location results in a\n\
7915a72c 15952user-query to see if a pending breakpoint should be created."),
2c5b56ce 15953 NULL,
920d2a44 15954 show_pending_break_support,
6e1d7d6c
AC
15955 &breakpoint_set_cmdlist,
15956 &breakpoint_show_cmdlist);
fa8d40ab
JJ
15957
15958 pending_break_support = AUTO_BOOLEAN_AUTO;
765dc015
VP
15959
15960 add_setshow_boolean_cmd ("auto-hw", no_class,
15961 &automatic_hardware_breakpoints, _("\
15962Set automatic usage of hardware breakpoints."), _("\
15963Show automatic usage of hardware breakpoints."), _("\
15964If set, the debugger will automatically use hardware breakpoints for\n\
15965breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
15966a warning will be emitted for such breakpoints."),
15967 NULL,
15968 show_automatic_hardware_breakpoints,
15969 &breakpoint_set_cmdlist,
15970 &breakpoint_show_cmdlist);
74960c60 15971
a25a5a45
PA
15972 add_setshow_boolean_cmd ("always-inserted", class_support,
15973 &always_inserted_mode, _("\
74960c60
VP
15974Set mode for inserting breakpoints."), _("\
15975Show mode for inserting breakpoints."), _("\
a25a5a45
PA
15976When this mode is on, breakpoints are inserted immediately as soon as\n\
15977they're created, kept inserted even when execution stops, and removed\n\
15978only when the user deletes them. When this mode is off (the default),\n\
15979breakpoints are inserted only when execution continues, and removed\n\
15980when execution stops."),
72d0e2c5
YQ
15981 NULL,
15982 &show_always_inserted_mode,
15983 &breakpoint_set_cmdlist,
15984 &breakpoint_show_cmdlist);
f1310107 15985
b775012e
LM
15986 add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
15987 condition_evaluation_enums,
15988 &condition_evaluation_mode_1, _("\
15989Set mode of breakpoint condition evaluation."), _("\
15990Show mode of breakpoint condition evaluation."), _("\
d1cda5d9 15991When this is set to \"host\", breakpoint conditions will be\n\
b775012e
LM
15992evaluated on the host's side by GDB. When it is set to \"target\",\n\
15993breakpoint conditions will be downloaded to the target (if the target\n\
15994supports such feature) and conditions will be evaluated on the target's side.\n\
15995If this is set to \"auto\" (default), this will be automatically set to\n\
15996\"target\" if it supports condition evaluation, otherwise it will\n\
15997be set to \"gdb\""),
15998 &set_condition_evaluation_mode,
15999 &show_condition_evaluation_mode,
16000 &breakpoint_set_cmdlist,
16001 &breakpoint_show_cmdlist);
16002
f1310107
TJB
16003 add_com ("break-range", class_breakpoint, break_range_command, _("\
16004Set a breakpoint for an address range.\n\
16005break-range START-LOCATION, END-LOCATION\n\
16006where START-LOCATION and END-LOCATION can be one of the following:\n\
16007 LINENUM, for that line in the current file,\n\
16008 FILE:LINENUM, for that line in that file,\n\
16009 +OFFSET, for that number of lines after the current line\n\
16010 or the start of the range\n\
16011 FUNCTION, for the first line in that function,\n\
16012 FILE:FUNCTION, to distinguish among like-named static functions.\n\
16013 *ADDRESS, for the instruction at that address.\n\
16014\n\
16015The breakpoint will stop execution of the inferior whenever it executes\n\
16016an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
16017range (including START-LOCATION and END-LOCATION)."));
16018
e7e0cddf 16019 c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
629500fa 16020Set a dynamic printf at specified location.\n\
e7e0cddf 16021dprintf location,format string,arg1,arg2,...\n\
629500fa
KS
16022location may be a linespec, explicit, or address location.\n"
16023"\n" LOCATION_HELP_STRING));
e7e0cddf
SS
16024 set_cmd_completer (c, location_completer);
16025
16026 add_setshow_enum_cmd ("dprintf-style", class_support,
16027 dprintf_style_enums, &dprintf_style, _("\
16028Set the style of usage for dynamic printf."), _("\
16029Show the style of usage for dynamic printf."), _("\
16030This setting chooses how GDB will do a dynamic printf.\n\
16031If the value is \"gdb\", then the printing is done by GDB to its own\n\
16032console, as with the \"printf\" command.\n\
16033If the value is \"call\", the print is done by calling a function in your\n\
16034program; by default printf(), but you can choose a different function or\n\
16035output stream by setting dprintf-function and dprintf-channel."),
16036 update_dprintf_commands, NULL,
16037 &setlist, &showlist);
16038
16039 dprintf_function = xstrdup ("printf");
16040 add_setshow_string_cmd ("dprintf-function", class_support,
16041 &dprintf_function, _("\
16042Set the function to use for dynamic printf"), _("\
16043Show the function to use for dynamic printf"), NULL,
16044 update_dprintf_commands, NULL,
16045 &setlist, &showlist);
16046
16047 dprintf_channel = xstrdup ("");
16048 add_setshow_string_cmd ("dprintf-channel", class_support,
16049 &dprintf_channel, _("\
16050Set the channel to use for dynamic printf"), _("\
16051Show the channel to use for dynamic printf"), NULL,
16052 update_dprintf_commands, NULL,
16053 &setlist, &showlist);
16054
d3ce09f5
SS
16055 add_setshow_boolean_cmd ("disconnected-dprintf", no_class,
16056 &disconnected_dprintf, _("\
16057Set whether dprintf continues after GDB disconnects."), _("\
16058Show whether dprintf continues after GDB disconnects."), _("\
16059Use this to let dprintf commands continue to hit and produce output\n\
16060even if GDB disconnects or detaches from the target."),
16061 NULL,
16062 NULL,
16063 &setlist, &showlist);
16064
16065 add_com ("agent-printf", class_vars, agent_printf_command, _("\
16066agent-printf \"printf format string\", arg1, arg2, arg3, ..., argn\n\
16067(target agent only) This is useful for formatted output in user-defined commands."));
16068
765dc015 16069 automatic_hardware_breakpoints = 1;
f3b1572e 16070
76727919
TT
16071 gdb::observers::about_to_proceed.attach (breakpoint_about_to_proceed);
16072 gdb::observers::thread_exit.attach (remove_threaded_breakpoints);
c906108c 16073}