gdb/copyright.py: Convert to Python 3
[deliverable/binutils-gdb.git] / gdb / breakpoint.c
CommitLineData
c906108c 1/* Everything about breakpoints, for GDB.
8926118c 2
42a4f53d 3 Copyright (C) 1986-2019 Free Software Foundation, Inc.
c906108c 4
c5aa993b 5 This file is part of GDB.
c906108c 6
c5aa993b
JM
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
a9762ec7 9 the Free Software Foundation; either version 3 of the License, or
c5aa993b 10 (at your option) any later version.
c906108c 11
c5aa993b
JM
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
c906108c 16
c5aa993b 17 You should have received a copy of the GNU General Public License
a9762ec7 18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c
SS
19
20#include "defs.h"
d55e5aa6 21#include "arch-utils.h"
4de283e4
TT
22#include <ctype.h>
23#include "hashtab.h"
24#include "symtab.h"
25#include "frame.h"
c906108c 26#include "breakpoint.h"
4de283e4
TT
27#include "tracepoint.h"
28#include "gdbtypes.h"
c906108c 29#include "expression.h"
d55e5aa6 30#include "gdbcore.h"
4de283e4
TT
31#include "gdbcmd.h"
32#include "value.h"
33#include "command.h"
c906108c 34#include "inferior.h"
45741a9c 35#include "infrun.h"
4de283e4
TT
36#include "gdbthread.h"
37#include "target.h"
c906108c 38#include "language.h"
4de283e4
TT
39#include "gdb-demangle.h"
40#include "filenames.h"
41#include "annotate.h"
42#include "symfile.h"
d55e5aa6 43#include "objfiles.h"
4de283e4
TT
44#include "source.h"
45#include "linespec.h"
46#include "completer.h"
47#include "ui-out.h"
48#include "cli/cli-script.h"
49#include "block.h"
50#include "solib.h"
51#include "solist.h"
d55e5aa6 52#include "observable.h"
4de283e4
TT
53#include "memattr.h"
54#include "ada-lang.h"
55#include "top.h"
56#include "valprint.h"
57#include "jit.h"
65d79d4b 58#include "parser-defs.h"
4de283e4 59#include "gdb_regex.h"
55aa24fb 60#include "probe.h"
4de283e4 61#include "cli/cli-utils.h"
d55e5aa6 62#include "stack.h"
4de283e4
TT
63#include "ax-gdb.h"
64#include "dummy-frame.h"
65#include "interps.h"
268a13a5 66#include "gdbsupport/format.h"
cfc31633 67#include "thread-fsm.h"
5d5658a1 68#include "tid-parse.h"
4de283e4 69#include "cli/cli-style.h"
d3ce09f5 70
1042e4c0 71/* readline include files */
073bbbb0 72#include "readline/tilde.h"
1042e4c0
SS
73
74/* readline defines this. */
75#undef savestring
76
034dad6f 77#include "mi/mi-common.h"
6dddc817 78#include "extension.h"
325fac50 79#include <algorithm>
5ed8105e 80#include "progspace-and-thread.h"
268a13a5
TT
81#include "gdbsupport/array-view.h"
82#include "gdbsupport/gdb_optional.h"
104c1213 83
4a64f543 84/* Prototypes for local functions. */
c906108c 85
896b6bda 86static void map_breakpoint_numbers (const char *,
48649e1b 87 gdb::function_view<void (breakpoint *)>);
c906108c 88
348d480f
PA
89static void breakpoint_re_set_default (struct breakpoint *);
90
f00aae0f
KS
91static void
92 create_sals_from_location_default (const struct event_location *location,
93 struct linespec_result *canonical,
94 enum bptype type_wanted);
983af33b
SDJ
95
96static void create_breakpoints_sal_default (struct gdbarch *,
97 struct linespec_result *,
e1e01040
PA
98 gdb::unique_xmalloc_ptr<char>,
99 gdb::unique_xmalloc_ptr<char>,
100 enum bptype,
983af33b
SDJ
101 enum bpdisp, int, int,
102 int,
103 const struct breakpoint_ops *,
44f238bb 104 int, int, int, unsigned);
983af33b 105
6c5b2ebe
PA
106static std::vector<symtab_and_line> decode_location_default
107 (struct breakpoint *b, const struct event_location *location,
108 struct program_space *search_pspace);
983af33b 109
a6535de1
TT
110static int can_use_hardware_watchpoint
111 (const std::vector<value_ref_ptr> &vals);
c906108c 112
a14ed312 113static void mention (struct breakpoint *);
c906108c 114
348d480f
PA
115static struct breakpoint *set_raw_breakpoint_without_location (struct gdbarch *,
116 enum bptype,
c0a91b2b 117 const struct breakpoint_ops *);
3742cc8b
YQ
118static struct bp_location *add_location_to_breakpoint (struct breakpoint *,
119 const struct symtab_and_line *);
120
4a64f543
MS
121/* This function is used in gdbtk sources and thus can not be made
122 static. */
63c252f8 123struct breakpoint *set_raw_breakpoint (struct gdbarch *gdbarch,
348d480f 124 struct symtab_and_line,
c0a91b2b
TT
125 enum bptype,
126 const struct breakpoint_ops *);
c906108c 127
06edf0c0
PA
128static struct breakpoint *
129 momentary_breakpoint_from_master (struct breakpoint *orig,
130 enum bptype type,
a1aa2221
LM
131 const struct breakpoint_ops *ops,
132 int loc_enabled);
06edf0c0 133
76897487
KB
134static void breakpoint_adjustment_warning (CORE_ADDR, CORE_ADDR, int, int);
135
a6d9a66e
UW
136static CORE_ADDR adjust_breakpoint_address (struct gdbarch *gdbarch,
137 CORE_ADDR bpaddr,
88f7da05 138 enum bptype bptype);
76897487 139
6c95b8df
PA
140static void describe_other_breakpoints (struct gdbarch *,
141 struct program_space *, CORE_ADDR,
5af949e3 142 struct obj_section *, int);
c906108c 143
85d721b8
PA
144static int watchpoint_locations_match (struct bp_location *loc1,
145 struct bp_location *loc2);
146
f1310107 147static int breakpoint_location_address_match (struct bp_location *bl,
accd0bcd 148 const struct address_space *aspace,
f1310107
TJB
149 CORE_ADDR addr);
150
d35ae833 151static int breakpoint_location_address_range_overlap (struct bp_location *,
accd0bcd 152 const address_space *,
d35ae833
PA
153 CORE_ADDR, int);
154
834c0d03 155static int remove_breakpoint (struct bp_location *);
b2b6a7da 156static int remove_breakpoint_1 (struct bp_location *, enum remove_bp_reason);
c906108c 157
e514a9d6 158static enum print_stop_action print_bp_stop_message (bpstat bs);
c906108c 159
a14ed312 160static int hw_breakpoint_used_count (void);
c906108c 161
a1398e0c
PA
162static int hw_watchpoint_use_count (struct breakpoint *);
163
164static int hw_watchpoint_used_count_others (struct breakpoint *except,
165 enum bptype type,
166 int *other_type_used);
c906108c 167
816338b5
SS
168static void enable_breakpoint_disp (struct breakpoint *, enum bpdisp,
169 int count);
c906108c 170
fe3f5fa8 171static void free_bp_location (struct bp_location *loc);
f431efe5
PA
172static void incref_bp_location (struct bp_location *loc);
173static void decref_bp_location (struct bp_location **loc);
fe3f5fa8 174
39d61571 175static struct bp_location *allocate_bp_location (struct breakpoint *bpt);
a5606eee 176
44702360
PA
177/* update_global_location_list's modes of operation wrt to whether to
178 insert locations now. */
179enum ugll_insert_mode
180{
181 /* Don't insert any breakpoint locations into the inferior, only
182 remove already-inserted locations that no longer should be
183 inserted. Functions that delete a breakpoint or breakpoints
184 should specify this mode, so that deleting a breakpoint doesn't
185 have the side effect of inserting the locations of other
186 breakpoints that are marked not-inserted, but should_be_inserted
187 returns true on them.
188
189 This behavior is useful is situations close to tear-down -- e.g.,
190 after an exec, while the target still has execution, but
191 breakpoint shadows of the previous executable image should *NOT*
192 be restored to the new image; or before detaching, where the
193 target still has execution and wants to delete breakpoints from
194 GDB's lists, and all breakpoints had already been removed from
195 the inferior. */
196 UGLL_DONT_INSERT,
197
a25a5a45
PA
198 /* May insert breakpoints iff breakpoints_should_be_inserted_now
199 claims breakpoints should be inserted now. */
04086b45
PA
200 UGLL_MAY_INSERT,
201
a25a5a45
PA
202 /* Insert locations now, irrespective of
203 breakpoints_should_be_inserted_now. E.g., say all threads are
204 stopped right now, and the user did "continue". We need to
205 insert breakpoints _before_ resuming the target, but
206 UGLL_MAY_INSERT wouldn't insert them, because
207 breakpoints_should_be_inserted_now returns false at that point,
208 as no thread is running yet. */
04086b45 209 UGLL_INSERT
44702360
PA
210};
211
212static void update_global_location_list (enum ugll_insert_mode);
a5606eee 213
44702360 214static void update_global_location_list_nothrow (enum ugll_insert_mode);
74960c60 215
74960c60 216static void insert_breakpoint_locations (void);
a5606eee 217
0b39b52e 218static void trace_pass_command (const char *, int);
1042e4c0 219
558a9d82
YQ
220static void set_tracepoint_count (int num);
221
f2478a7e 222static bool is_masked_watchpoint (const struct breakpoint *b);
9c06b0b4 223
b775012e
LM
224static struct bp_location **get_first_locp_gte_addr (CORE_ADDR address);
225
983af33b
SDJ
226/* Return 1 if B refers to a static tracepoint set by marker ("-m"), zero
227 otherwise. */
228
229static int strace_marker_p (struct breakpoint *b);
0fb4aa4b 230
2060206e
PA
231/* The breakpoint_ops structure to be inherited by all breakpoint_ops
232 that are implemented on top of software or hardware breakpoints
233 (user breakpoints, internal and momentary breakpoints, etc.). */
234static struct breakpoint_ops bkpt_base_breakpoint_ops;
235
236/* Internal breakpoints class type. */
06edf0c0 237static struct breakpoint_ops internal_breakpoint_ops;
2060206e
PA
238
239/* Momentary breakpoints class type. */
06edf0c0
PA
240static struct breakpoint_ops momentary_breakpoint_ops;
241
2060206e
PA
242/* The breakpoint_ops structure to be used in regular user created
243 breakpoints. */
244struct breakpoint_ops bkpt_breakpoint_ops;
245
55aa24fb
SDJ
246/* Breakpoints set on probes. */
247static struct breakpoint_ops bkpt_probe_breakpoint_ops;
248
bac7c5cf
GB
249/* Tracepoints set on probes. */
250static struct breakpoint_ops tracepoint_probe_breakpoint_ops;
251
e7e0cddf 252/* Dynamic printf class type. */
c5867ab6 253struct breakpoint_ops dprintf_breakpoint_ops;
e7e0cddf 254
d3ce09f5
SS
255/* The style in which to perform a dynamic printf. This is a user
256 option because different output options have different tradeoffs;
257 if GDB does the printing, there is better error handling if there
258 is a problem with any of the arguments, but using an inferior
259 function lets you have special-purpose printers and sending of
260 output to the same place as compiled-in print functions. */
261
262static const char dprintf_style_gdb[] = "gdb";
263static const char dprintf_style_call[] = "call";
264static const char dprintf_style_agent[] = "agent";
265static const char *const dprintf_style_enums[] = {
266 dprintf_style_gdb,
267 dprintf_style_call,
268 dprintf_style_agent,
269 NULL
270};
271static const char *dprintf_style = dprintf_style_gdb;
272
273/* The function to use for dynamic printf if the preferred style is to
274 call into the inferior. The value is simply a string that is
275 copied into the command, so it can be anything that GDB can
276 evaluate to a callable address, not necessarily a function name. */
277
bde6261a 278static char *dprintf_function;
d3ce09f5
SS
279
280/* The channel to use for dynamic printf if the preferred style is to
281 call into the inferior; if a nonempty string, it will be passed to
282 the call as the first argument, with the format string as the
283 second. As with the dprintf function, this can be anything that
284 GDB knows how to evaluate, so in addition to common choices like
285 "stderr", this could be an app-specific expression like
286 "mystreams[curlogger]". */
287
bde6261a 288static char *dprintf_channel;
d3ce09f5
SS
289
290/* True if dprintf commands should continue to operate even if GDB
291 has disconnected. */
491144b5 292static bool disconnected_dprintf = true;
d3ce09f5 293
5cea2a26
PA
294struct command_line *
295breakpoint_commands (struct breakpoint *b)
296{
d1b0a7bf 297 return b->commands ? b->commands.get () : NULL;
5cea2a26 298}
3daf8fe5 299
f3b1572e
PA
300/* Flag indicating that a command has proceeded the inferior past the
301 current breakpoint. */
302
491144b5 303static bool breakpoint_proceeded;
f3b1572e 304
956a9fb9 305const char *
2cec12e5
AR
306bpdisp_text (enum bpdisp disp)
307{
4a64f543
MS
308 /* NOTE: the following values are a part of MI protocol and
309 represent values of 'disp' field returned when inferior stops at
310 a breakpoint. */
bc043ef3 311 static const char * const bpdisps[] = {"del", "dstp", "dis", "keep"};
cc59ec59 312
2cec12e5
AR
313 return bpdisps[(int) disp];
314}
c906108c 315
4a64f543 316/* Prototypes for exported functions. */
c906108c 317/* If FALSE, gdb will not use hardware support for watchpoints, even
4a64f543 318 if such is available. */
c906108c
SS
319static int can_use_hw_watchpoints;
320
920d2a44
AC
321static void
322show_can_use_hw_watchpoints (struct ui_file *file, int from_tty,
323 struct cmd_list_element *c,
324 const char *value)
325{
3e43a32a
MS
326 fprintf_filtered (file,
327 _("Debugger's willingness to use "
328 "watchpoint hardware is %s.\n"),
920d2a44
AC
329 value);
330}
331
fa8d40ab
JJ
332/* If AUTO_BOOLEAN_FALSE, gdb will not attempt to create pending breakpoints.
333 If AUTO_BOOLEAN_TRUE, gdb will automatically create pending breakpoints
4a64f543 334 for unrecognized breakpoint locations.
fa8d40ab
JJ
335 If AUTO_BOOLEAN_AUTO, gdb will query when breakpoints are unrecognized. */
336static enum auto_boolean pending_break_support;
920d2a44
AC
337static void
338show_pending_break_support (struct ui_file *file, int from_tty,
339 struct cmd_list_element *c,
340 const char *value)
341{
3e43a32a
MS
342 fprintf_filtered (file,
343 _("Debugger's behavior regarding "
344 "pending breakpoints is %s.\n"),
920d2a44
AC
345 value);
346}
fa8d40ab 347
491144b5 348/* If true, gdb will automatically use hardware breakpoints for breakpoints
4a64f543 349 set with "break" but falling in read-only memory.
491144b5 350 If false, gdb will warn about such breakpoints, but won't automatically
765dc015 351 use hardware breakpoints. */
491144b5 352static bool automatic_hardware_breakpoints;
765dc015
VP
353static void
354show_automatic_hardware_breakpoints (struct ui_file *file, int from_tty,
355 struct cmd_list_element *c,
356 const char *value)
357{
3e43a32a
MS
358 fprintf_filtered (file,
359 _("Automatic usage of hardware breakpoints is %s.\n"),
765dc015
VP
360 value);
361}
362
a25a5a45
PA
363/* If on, GDB keeps breakpoints inserted even if the inferior is
364 stopped, and immediately inserts any new breakpoints as soon as
365 they're created. If off (default), GDB keeps breakpoints off of
366 the target as long as possible. That is, it delays inserting
367 breakpoints until the next resume, and removes them again when the
368 target fully stops. This is a bit safer in case GDB crashes while
369 processing user input. */
491144b5 370static bool always_inserted_mode = false;
72d0e2c5 371
33e5cbd6 372static void
74960c60 373show_always_inserted_mode (struct ui_file *file, int from_tty,
33e5cbd6 374 struct cmd_list_element *c, const char *value)
74960c60 375{
a25a5a45
PA
376 fprintf_filtered (file, _("Always inserted breakpoint mode is %s.\n"),
377 value);
74960c60
VP
378}
379
b57bacec
PA
380/* See breakpoint.h. */
381
33e5cbd6 382int
a25a5a45 383breakpoints_should_be_inserted_now (void)
33e5cbd6 384{
a25a5a45
PA
385 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
386 {
387 /* If breakpoints are global, they should be inserted even if no
388 thread under gdb's control is running, or even if there are
389 no threads under GDB's control yet. */
390 return 1;
391 }
392 else if (target_has_execution)
393 {
a25a5a45
PA
394 if (always_inserted_mode)
395 {
396 /* The user wants breakpoints inserted even if all threads
397 are stopped. */
398 return 1;
399 }
400
b57bacec
PA
401 if (threads_are_executing ())
402 return 1;
372316f1
PA
403
404 /* Don't remove breakpoints yet if, even though all threads are
405 stopped, we still have events to process. */
08036331 406 for (thread_info *tp : all_non_exited_threads ())
372316f1
PA
407 if (tp->resumed
408 && tp->suspend.waitstatus_pending_p)
409 return 1;
a25a5a45
PA
410 }
411 return 0;
33e5cbd6 412}
765dc015 413
b775012e
LM
414static const char condition_evaluation_both[] = "host or target";
415
416/* Modes for breakpoint condition evaluation. */
417static const char condition_evaluation_auto[] = "auto";
418static const char condition_evaluation_host[] = "host";
419static const char condition_evaluation_target[] = "target";
420static const char *const condition_evaluation_enums[] = {
421 condition_evaluation_auto,
422 condition_evaluation_host,
423 condition_evaluation_target,
424 NULL
425};
426
427/* Global that holds the current mode for breakpoint condition evaluation. */
428static const char *condition_evaluation_mode_1 = condition_evaluation_auto;
429
430/* Global that we use to display information to the user (gets its value from
431 condition_evaluation_mode_1. */
432static const char *condition_evaluation_mode = condition_evaluation_auto;
433
434/* Translate a condition evaluation mode MODE into either "host"
435 or "target". This is used mostly to translate from "auto" to the
436 real setting that is being used. It returns the translated
437 evaluation mode. */
438
439static const char *
440translate_condition_evaluation_mode (const char *mode)
441{
442 if (mode == condition_evaluation_auto)
443 {
444 if (target_supports_evaluation_of_breakpoint_conditions ())
445 return condition_evaluation_target;
446 else
447 return condition_evaluation_host;
448 }
449 else
450 return mode;
451}
452
453/* Discovers what condition_evaluation_auto translates to. */
454
455static const char *
456breakpoint_condition_evaluation_mode (void)
457{
458 return translate_condition_evaluation_mode (condition_evaluation_mode);
459}
460
461/* Return true if GDB should evaluate breakpoint conditions or false
462 otherwise. */
463
464static int
465gdb_evaluates_breakpoint_condition_p (void)
466{
467 const char *mode = breakpoint_condition_evaluation_mode ();
468
469 return (mode == condition_evaluation_host);
470}
471
c906108c
SS
472/* Are we executing breakpoint commands? */
473static int executing_breakpoint_commands;
474
c02f5703
MS
475/* Are overlay event breakpoints enabled? */
476static int overlay_events_enabled;
477
e09342b5 478/* See description in breakpoint.h. */
491144b5 479bool target_exact_watchpoints = false;
e09342b5 480
c906108c 481/* Walk the following statement or block through all breakpoints.
e5dd4106 482 ALL_BREAKPOINTS_SAFE does so even if the statement deletes the
4a64f543 483 current breakpoint. */
c906108c 484
5c44784c 485#define ALL_BREAKPOINTS(B) for (B = breakpoint_chain; B; B = B->next)
c906108c 486
5c44784c
JM
487#define ALL_BREAKPOINTS_SAFE(B,TMP) \
488 for (B = breakpoint_chain; \
489 B ? (TMP=B->next, 1): 0; \
490 B = TMP)
c906108c 491
4a64f543
MS
492/* Similar iterator for the low-level breakpoints. SAFE variant is
493 not provided so update_global_location_list must not be called
494 while executing the block of ALL_BP_LOCATIONS. */
7cc221ef 495
876fa593 496#define ALL_BP_LOCATIONS(B,BP_TMP) \
f5336ca5
PA
497 for (BP_TMP = bp_locations; \
498 BP_TMP < bp_locations + bp_locations_count && (B = *BP_TMP);\
876fa593 499 BP_TMP++)
7cc221ef 500
b775012e
LM
501/* Iterates through locations with address ADDRESS for the currently selected
502 program space. BP_LOCP_TMP points to each object. BP_LOCP_START points
503 to where the loop should start from.
504 If BP_LOCP_START is a NULL pointer, the macro automatically seeks the
505 appropriate location to start with. */
506
507#define ALL_BP_LOCATIONS_AT_ADDR(BP_LOCP_TMP, BP_LOCP_START, ADDRESS) \
508 for (BP_LOCP_START = BP_LOCP_START == NULL ? get_first_locp_gte_addr (ADDRESS) : BP_LOCP_START, \
509 BP_LOCP_TMP = BP_LOCP_START; \
510 BP_LOCP_START \
f5336ca5 511 && (BP_LOCP_TMP < bp_locations + bp_locations_count \
b775012e
LM
512 && (*BP_LOCP_TMP)->address == ADDRESS); \
513 BP_LOCP_TMP++)
514
1042e4c0
SS
515/* Iterator for tracepoints only. */
516
517#define ALL_TRACEPOINTS(B) \
518 for (B = breakpoint_chain; B; B = B->next) \
d77f58be 519 if (is_tracepoint (B))
1042e4c0 520
7cc221ef 521/* Chains of all breakpoints defined. */
c906108c 522
81e6b8eb 523static struct breakpoint *breakpoint_chain;
c906108c 524
39ef2f62 525/* Array is sorted by bp_location_is_less_than - primarily by the ADDRESS. */
876fa593 526
f5336ca5 527static struct bp_location **bp_locations;
876fa593 528
f5336ca5 529/* Number of elements of BP_LOCATIONS. */
876fa593 530
f5336ca5 531static unsigned bp_locations_count;
876fa593 532
4a64f543 533/* Maximum alignment offset between bp_target_info.PLACED_ADDRESS and
f5336ca5 534 ADDRESS for the current elements of BP_LOCATIONS which get a valid
4a64f543 535 result from bp_location_has_shadow. You can use it for roughly
f5336ca5 536 limiting the subrange of BP_LOCATIONS to scan for shadow bytes for
4a64f543 537 an address you need to read. */
876fa593 538
f5336ca5 539static CORE_ADDR bp_locations_placed_address_before_address_max;
876fa593 540
4a64f543
MS
541/* Maximum offset plus alignment between bp_target_info.PLACED_ADDRESS
542 + bp_target_info.SHADOW_LEN and ADDRESS for the current elements of
f5336ca5
PA
543 BP_LOCATIONS which get a valid result from bp_location_has_shadow.
544 You can use it for roughly limiting the subrange of BP_LOCATIONS to
4a64f543 545 scan for shadow bytes for an address you need to read. */
876fa593 546
f5336ca5 547static CORE_ADDR bp_locations_shadow_len_after_address_max;
7cc221ef 548
4a64f543 549/* The locations that no longer correspond to any breakpoint, unlinked
f5336ca5
PA
550 from the bp_locations array, but for which a hit may still be
551 reported by a target. */
1123588c 552static std::vector<bp_location *> moribund_locations;
20874c92 553
c906108c
SS
554/* Number of last breakpoint made. */
555
95a42b64
TT
556static int breakpoint_count;
557
86b17b60
PA
558/* The value of `breakpoint_count' before the last command that
559 created breakpoints. If the last (break-like) command created more
560 than one breakpoint, then the difference between BREAKPOINT_COUNT
561 and PREV_BREAKPOINT_COUNT is more than one. */
562static int prev_breakpoint_count;
c906108c 563
1042e4c0
SS
564/* Number of last tracepoint made. */
565
95a42b64 566static int tracepoint_count;
1042e4c0 567
6149aea9
PA
568static struct cmd_list_element *breakpoint_set_cmdlist;
569static struct cmd_list_element *breakpoint_show_cmdlist;
9291a0cd 570struct cmd_list_element *save_cmdlist;
6149aea9 571
badd37ce
SDJ
572/* See declaration at breakpoint.h. */
573
574struct breakpoint *
575breakpoint_find_if (int (*func) (struct breakpoint *b, void *d),
576 void *user_data)
577{
578 struct breakpoint *b = NULL;
579
580 ALL_BREAKPOINTS (b)
581 {
582 if (func (b, user_data) != 0)
583 break;
584 }
585
586 return b;
587}
588
468d015d
JJ
589/* Return whether a breakpoint is an active enabled breakpoint. */
590static int
591breakpoint_enabled (struct breakpoint *b)
592{
0d381245 593 return (b->enable_state == bp_enabled);
468d015d
JJ
594}
595
c906108c
SS
596/* Set breakpoint count to NUM. */
597
95a42b64 598static void
fba45db2 599set_breakpoint_count (int num)
c906108c 600{
86b17b60 601 prev_breakpoint_count = breakpoint_count;
c906108c 602 breakpoint_count = num;
4fa62494 603 set_internalvar_integer (lookup_internalvar ("bpnum"), num);
c906108c
SS
604}
605
86b17b60
PA
606/* Used by `start_rbreak_breakpoints' below, to record the current
607 breakpoint count before "rbreak" creates any breakpoint. */
608static int rbreak_start_breakpoint_count;
609
95a42b64
TT
610/* Called at the start an "rbreak" command to record the first
611 breakpoint made. */
86b17b60 612
c80049d3 613scoped_rbreak_breakpoints::scoped_rbreak_breakpoints ()
95a42b64 614{
86b17b60 615 rbreak_start_breakpoint_count = breakpoint_count;
95a42b64
TT
616}
617
618/* Called at the end of an "rbreak" command to record the last
619 breakpoint made. */
86b17b60 620
c80049d3 621scoped_rbreak_breakpoints::~scoped_rbreak_breakpoints ()
95a42b64 622{
86b17b60 623 prev_breakpoint_count = rbreak_start_breakpoint_count;
95a42b64
TT
624}
625
4a64f543 626/* Used in run_command to zero the hit count when a new run starts. */
c906108c
SS
627
628void
fba45db2 629clear_breakpoint_hit_counts (void)
c906108c
SS
630{
631 struct breakpoint *b;
632
633 ALL_BREAKPOINTS (b)
634 b->hit_count = 0;
635}
636
c906108c 637\f
48cb2d85
VP
638/* Return the breakpoint with the specified number, or NULL
639 if the number does not refer to an existing breakpoint. */
640
641struct breakpoint *
642get_breakpoint (int num)
643{
644 struct breakpoint *b;
645
646 ALL_BREAKPOINTS (b)
647 if (b->number == num)
648 return b;
649
650 return NULL;
651}
5c44784c 652
c906108c 653\f
adc36818 654
b775012e
LM
655/* Mark locations as "conditions have changed" in case the target supports
656 evaluating conditions on its side. */
657
658static void
659mark_breakpoint_modified (struct breakpoint *b)
660{
661 struct bp_location *loc;
662
663 /* This is only meaningful if the target is
664 evaluating conditions and if the user has
665 opted for condition evaluation on the target's
666 side. */
667 if (gdb_evaluates_breakpoint_condition_p ()
668 || !target_supports_evaluation_of_breakpoint_conditions ())
669 return;
670
671 if (!is_breakpoint (b))
672 return;
673
674 for (loc = b->loc; loc; loc = loc->next)
675 loc->condition_changed = condition_modified;
676}
677
678/* Mark location as "conditions have changed" in case the target supports
679 evaluating conditions on its side. */
680
681static void
682mark_breakpoint_location_modified (struct bp_location *loc)
683{
684 /* This is only meaningful if the target is
685 evaluating conditions and if the user has
686 opted for condition evaluation on the target's
687 side. */
688 if (gdb_evaluates_breakpoint_condition_p ()
689 || !target_supports_evaluation_of_breakpoint_conditions ())
690
691 return;
692
693 if (!is_breakpoint (loc->owner))
694 return;
695
696 loc->condition_changed = condition_modified;
697}
698
699/* Sets the condition-evaluation mode using the static global
700 condition_evaluation_mode. */
701
702static void
eb4c3f4a 703set_condition_evaluation_mode (const char *args, int from_tty,
b775012e
LM
704 struct cmd_list_element *c)
705{
b775012e
LM
706 const char *old_mode, *new_mode;
707
708 if ((condition_evaluation_mode_1 == condition_evaluation_target)
709 && !target_supports_evaluation_of_breakpoint_conditions ())
710 {
711 condition_evaluation_mode_1 = condition_evaluation_mode;
712 warning (_("Target does not support breakpoint condition evaluation.\n"
713 "Using host evaluation mode instead."));
714 return;
715 }
716
717 new_mode = translate_condition_evaluation_mode (condition_evaluation_mode_1);
718 old_mode = translate_condition_evaluation_mode (condition_evaluation_mode);
719
abf1152a
JK
720 /* Flip the switch. Flip it even if OLD_MODE == NEW_MODE as one of the
721 settings was "auto". */
722 condition_evaluation_mode = condition_evaluation_mode_1;
723
b775012e
LM
724 /* Only update the mode if the user picked a different one. */
725 if (new_mode != old_mode)
726 {
727 struct bp_location *loc, **loc_tmp;
728 /* If the user switched to a different evaluation mode, we
729 need to synch the changes with the target as follows:
730
731 "host" -> "target": Send all (valid) conditions to the target.
732 "target" -> "host": Remove all the conditions from the target.
733 */
734
b775012e
LM
735 if (new_mode == condition_evaluation_target)
736 {
737 /* Mark everything modified and synch conditions with the
738 target. */
739 ALL_BP_LOCATIONS (loc, loc_tmp)
740 mark_breakpoint_location_modified (loc);
741 }
742 else
743 {
744 /* Manually mark non-duplicate locations to synch conditions
745 with the target. We do this to remove all the conditions the
746 target knows about. */
747 ALL_BP_LOCATIONS (loc, loc_tmp)
748 if (is_breakpoint (loc->owner) && loc->inserted)
749 loc->needs_update = 1;
750 }
751
752 /* Do the update. */
44702360 753 update_global_location_list (UGLL_MAY_INSERT);
b775012e
LM
754 }
755
756 return;
757}
758
759/* Shows the current mode of breakpoint condition evaluation. Explicitly shows
760 what "auto" is translating to. */
761
762static void
763show_condition_evaluation_mode (struct ui_file *file, int from_tty,
764 struct cmd_list_element *c, const char *value)
765{
766 if (condition_evaluation_mode == condition_evaluation_auto)
767 fprintf_filtered (file,
768 _("Breakpoint condition evaluation "
769 "mode is %s (currently %s).\n"),
770 value,
771 breakpoint_condition_evaluation_mode ());
772 else
773 fprintf_filtered (file, _("Breakpoint condition evaluation mode is %s.\n"),
774 value);
775}
776
777/* A comparison function for bp_location AP and BP that is used by
778 bsearch. This comparison function only cares about addresses, unlike
39ef2f62 779 the more general bp_location_is_less_than function. */
b775012e
LM
780
781static int
f5336ca5 782bp_locations_compare_addrs (const void *ap, const void *bp)
b775012e 783{
9a3c8263
SM
784 const struct bp_location *a = *(const struct bp_location **) ap;
785 const struct bp_location *b = *(const struct bp_location **) bp;
b775012e
LM
786
787 if (a->address == b->address)
788 return 0;
789 else
790 return ((a->address > b->address) - (a->address < b->address));
791}
792
793/* Helper function to skip all bp_locations with addresses
794 less than ADDRESS. It returns the first bp_location that
795 is greater than or equal to ADDRESS. If none is found, just
796 return NULL. */
797
798static struct bp_location **
799get_first_locp_gte_addr (CORE_ADDR address)
800{
801 struct bp_location dummy_loc;
802 struct bp_location *dummy_locp = &dummy_loc;
803 struct bp_location **locp_found = NULL;
804
805 /* Initialize the dummy location's address field. */
b775012e
LM
806 dummy_loc.address = address;
807
808 /* Find a close match to the first location at ADDRESS. */
9a3c8263 809 locp_found = ((struct bp_location **)
f5336ca5 810 bsearch (&dummy_locp, bp_locations, bp_locations_count,
9a3c8263 811 sizeof (struct bp_location **),
f5336ca5 812 bp_locations_compare_addrs));
b775012e
LM
813
814 /* Nothing was found, nothing left to do. */
815 if (locp_found == NULL)
816 return NULL;
817
818 /* We may have found a location that is at ADDRESS but is not the first in the
819 location's list. Go backwards (if possible) and locate the first one. */
f5336ca5 820 while ((locp_found - 1) >= bp_locations
b775012e
LM
821 && (*(locp_found - 1))->address == address)
822 locp_found--;
823
824 return locp_found;
825}
826
adc36818 827void
7a26bd4d 828set_breakpoint_condition (struct breakpoint *b, const char *exp,
adc36818
PM
829 int from_tty)
830{
3a5c3e22
PA
831 xfree (b->cond_string);
832 b->cond_string = NULL;
adc36818 833
3a5c3e22 834 if (is_watchpoint (b))
adc36818 835 {
3a5c3e22
PA
836 struct watchpoint *w = (struct watchpoint *) b;
837
4d01a485 838 w->cond_exp.reset ();
3a5c3e22
PA
839 }
840 else
841 {
842 struct bp_location *loc;
843
844 for (loc = b->loc; loc; loc = loc->next)
845 {
4d01a485 846 loc->cond.reset ();
b775012e
LM
847
848 /* No need to free the condition agent expression
849 bytecode (if we have one). We will handle this
850 when we go through update_global_location_list. */
3a5c3e22 851 }
adc36818 852 }
adc36818
PM
853
854 if (*exp == 0)
855 {
856 if (from_tty)
857 printf_filtered (_("Breakpoint %d now unconditional.\n"), b->number);
858 }
859 else
860 {
bbc13ae3 861 const char *arg = exp;
cc59ec59 862
adc36818
PM
863 /* I don't know if it matters whether this is the string the user
864 typed in or the decompiled expression. */
865 b->cond_string = xstrdup (arg);
866 b->condition_not_parsed = 0;
867
868 if (is_watchpoint (b))
869 {
3a5c3e22
PA
870 struct watchpoint *w = (struct watchpoint *) b;
871
699bd4cf 872 innermost_block_tracker tracker;
adc36818 873 arg = exp;
699bd4cf 874 w->cond_exp = parse_exp_1 (&arg, 0, 0, 0, &tracker);
adc36818
PM
875 if (*arg)
876 error (_("Junk at end of expression"));
699bd4cf 877 w->cond_exp_valid_block = tracker.block ();
adc36818
PM
878 }
879 else
880 {
3a5c3e22
PA
881 struct bp_location *loc;
882
adc36818
PM
883 for (loc = b->loc; loc; loc = loc->next)
884 {
885 arg = exp;
886 loc->cond =
1bb9788d
TT
887 parse_exp_1 (&arg, loc->address,
888 block_for_pc (loc->address), 0);
adc36818
PM
889 if (*arg)
890 error (_("Junk at end of expression"));
891 }
892 }
893 }
b775012e
LM
894 mark_breakpoint_modified (b);
895
76727919 896 gdb::observers::breakpoint_modified.notify (b);
adc36818
PM
897}
898
d55637df
TT
899/* Completion for the "condition" command. */
900
eb3ff9a5 901static void
6f937416 902condition_completer (struct cmd_list_element *cmd,
eb3ff9a5 903 completion_tracker &tracker,
6f937416 904 const char *text, const char *word)
d55637df 905{
6f937416 906 const char *space;
d55637df 907
f1735a53
TT
908 text = skip_spaces (text);
909 space = skip_to_space (text);
d55637df
TT
910 if (*space == '\0')
911 {
912 int len;
913 struct breakpoint *b;
d55637df
TT
914
915 if (text[0] == '$')
916 {
917 /* We don't support completion of history indices. */
eb3ff9a5
PA
918 if (!isdigit (text[1]))
919 complete_internalvar (tracker, &text[1]);
920 return;
d55637df
TT
921 }
922
923 /* We're completing the breakpoint number. */
924 len = strlen (text);
925
926 ALL_BREAKPOINTS (b)
58ce7251
SDJ
927 {
928 char number[50];
929
930 xsnprintf (number, sizeof (number), "%d", b->number);
931
932 if (strncmp (number, text, len) == 0)
b02f78f9 933 tracker.add_completion (make_unique_xstrdup (number));
58ce7251 934 }
d55637df 935
eb3ff9a5 936 return;
d55637df
TT
937 }
938
939 /* We're completing the expression part. */
f1735a53 940 text = skip_spaces (space);
eb3ff9a5 941 expression_completer (cmd, tracker, text, word);
d55637df
TT
942}
943
c906108c
SS
944/* condition N EXP -- set break condition of breakpoint N to EXP. */
945
946static void
0b39b52e 947condition_command (const char *arg, int from_tty)
c906108c 948{
52f0bd74 949 struct breakpoint *b;
0b39b52e 950 const char *p;
52f0bd74 951 int bnum;
c906108c
SS
952
953 if (arg == 0)
e2e0b3e5 954 error_no_arg (_("breakpoint number"));
c906108c
SS
955
956 p = arg;
957 bnum = get_number (&p);
5c44784c 958 if (bnum == 0)
8a3fe4f8 959 error (_("Bad breakpoint argument: '%s'"), arg);
c906108c
SS
960
961 ALL_BREAKPOINTS (b)
962 if (b->number == bnum)
2f069f6f 963 {
6dddc817
DE
964 /* Check if this breakpoint has a "stop" method implemented in an
965 extension language. This method and conditions entered into GDB
966 from the CLI are mutually exclusive. */
967 const struct extension_language_defn *extlang
968 = get_breakpoint_cond_ext_lang (b, EXT_LANG_NONE);
969
970 if (extlang != NULL)
971 {
972 error (_("Only one stop condition allowed. There is currently"
973 " a %s stop condition defined for this breakpoint."),
974 ext_lang_capitalized_name (extlang));
975 }
2566ad2d 976 set_breakpoint_condition (b, p, from_tty);
b775012e
LM
977
978 if (is_breakpoint (b))
44702360 979 update_global_location_list (UGLL_MAY_INSERT);
b775012e 980
2f069f6f
JB
981 return;
982 }
c906108c 983
8a3fe4f8 984 error (_("No breakpoint number %d."), bnum);
c906108c
SS
985}
986
a7bdde9e
VP
987/* Check that COMMAND do not contain commands that are suitable
988 only for tracepoints and not suitable for ordinary breakpoints.
4a64f543
MS
989 Throw if any such commands is found. */
990
a7bdde9e
VP
991static void
992check_no_tracepoint_commands (struct command_line *commands)
993{
994 struct command_line *c;
cc59ec59 995
a7bdde9e
VP
996 for (c = commands; c; c = c->next)
997 {
a7bdde9e 998 if (c->control_type == while_stepping_control)
3e43a32a
MS
999 error (_("The 'while-stepping' command can "
1000 "only be used for tracepoints"));
a7bdde9e 1001
12973681
TT
1002 check_no_tracepoint_commands (c->body_list_0.get ());
1003 check_no_tracepoint_commands (c->body_list_1.get ());
a7bdde9e
VP
1004
1005 /* Not that command parsing removes leading whitespace and comment
4a64f543 1006 lines and also empty lines. So, we only need to check for
a7bdde9e
VP
1007 command directly. */
1008 if (strstr (c->line, "collect ") == c->line)
1009 error (_("The 'collect' command can only be used for tracepoints"));
1010
51661e93
VP
1011 if (strstr (c->line, "teval ") == c->line)
1012 error (_("The 'teval' command can only be used for tracepoints"));
a7bdde9e
VP
1013 }
1014}
1015
c1fc2657 1016struct longjmp_breakpoint : public breakpoint
3b0871f4 1017{
c1fc2657 1018 ~longjmp_breakpoint () override;
3b0871f4
SM
1019};
1020
d77f58be
SS
1021/* Encapsulate tests for different types of tracepoints. */
1022
3b0871f4
SM
1023static bool
1024is_tracepoint_type (bptype type)
d9b3f62e
PA
1025{
1026 return (type == bp_tracepoint
1027 || type == bp_fast_tracepoint
1028 || type == bp_static_tracepoint);
1029}
1030
3b0871f4
SM
1031static bool
1032is_longjmp_type (bptype type)
1033{
1034 return type == bp_longjmp || type == bp_exception;
1035}
1036
f2478a7e
SM
1037/* See breakpoint.h. */
1038
1039bool
d77f58be 1040is_tracepoint (const struct breakpoint *b)
a7bdde9e 1041{
d9b3f62e 1042 return is_tracepoint_type (b->type);
a7bdde9e 1043}
d9b3f62e 1044
a5e364af
SM
1045/* Factory function to create an appropriate instance of breakpoint given
1046 TYPE. */
1047
1048static std::unique_ptr<breakpoint>
1049new_breakpoint_from_type (bptype type)
1050{
1051 breakpoint *b;
1052
1053 if (is_tracepoint_type (type))
c1fc2657 1054 b = new tracepoint ();
3b0871f4 1055 else if (is_longjmp_type (type))
c1fc2657 1056 b = new longjmp_breakpoint ();
a5e364af
SM
1057 else
1058 b = new breakpoint ();
1059
1060 return std::unique_ptr<breakpoint> (b);
1061}
1062
e5dd4106 1063/* A helper function that validates that COMMANDS are valid for a
95a42b64
TT
1064 breakpoint. This function will throw an exception if a problem is
1065 found. */
48cb2d85 1066
95a42b64
TT
1067static void
1068validate_commands_for_breakpoint (struct breakpoint *b,
1069 struct command_line *commands)
48cb2d85 1070{
d77f58be 1071 if (is_tracepoint (b))
a7bdde9e 1072 {
c9a6ce02 1073 struct tracepoint *t = (struct tracepoint *) b;
a7bdde9e
VP
1074 struct command_line *c;
1075 struct command_line *while_stepping = 0;
c9a6ce02
PA
1076
1077 /* Reset the while-stepping step count. The previous commands
1078 might have included a while-stepping action, while the new
1079 ones might not. */
1080 t->step_count = 0;
1081
1082 /* We need to verify that each top-level element of commands is
1083 valid for tracepoints, that there's at most one
1084 while-stepping element, and that the while-stepping's body
1085 has valid tracing commands excluding nested while-stepping.
1086 We also need to validate the tracepoint action line in the
1087 context of the tracepoint --- validate_actionline actually
1088 has side effects, like setting the tracepoint's
1089 while-stepping STEP_COUNT, in addition to checking if the
1090 collect/teval actions parse and make sense in the
1091 tracepoint's context. */
a7bdde9e
VP
1092 for (c = commands; c; c = c->next)
1093 {
a7bdde9e
VP
1094 if (c->control_type == while_stepping_control)
1095 {
1096 if (b->type == bp_fast_tracepoint)
3e43a32a
MS
1097 error (_("The 'while-stepping' command "
1098 "cannot be used for fast tracepoint"));
0fb4aa4b 1099 else if (b->type == bp_static_tracepoint)
3e43a32a
MS
1100 error (_("The 'while-stepping' command "
1101 "cannot be used for static tracepoint"));
a7bdde9e
VP
1102
1103 if (while_stepping)
3e43a32a
MS
1104 error (_("The 'while-stepping' command "
1105 "can be used only once"));
a7bdde9e
VP
1106 else
1107 while_stepping = c;
1108 }
c9a6ce02
PA
1109
1110 validate_actionline (c->line, b);
a7bdde9e
VP
1111 }
1112 if (while_stepping)
1113 {
1114 struct command_line *c2;
1115
12973681
TT
1116 gdb_assert (while_stepping->body_list_1 == nullptr);
1117 c2 = while_stepping->body_list_0.get ();
a7bdde9e
VP
1118 for (; c2; c2 = c2->next)
1119 {
a7bdde9e
VP
1120 if (c2->control_type == while_stepping_control)
1121 error (_("The 'while-stepping' command cannot be nested"));
1122 }
1123 }
1124 }
1125 else
1126 {
1127 check_no_tracepoint_commands (commands);
1128 }
95a42b64
TT
1129}
1130
0fb4aa4b
PA
1131/* Return a vector of all the static tracepoints set at ADDR. The
1132 caller is responsible for releasing the vector. */
1133
f51e0e20 1134std::vector<breakpoint *>
0fb4aa4b
PA
1135static_tracepoints_here (CORE_ADDR addr)
1136{
1137 struct breakpoint *b;
f51e0e20 1138 std::vector<breakpoint *> found;
0fb4aa4b
PA
1139 struct bp_location *loc;
1140
1141 ALL_BREAKPOINTS (b)
1142 if (b->type == bp_static_tracepoint)
1143 {
1144 for (loc = b->loc; loc; loc = loc->next)
1145 if (loc->address == addr)
f51e0e20 1146 found.push_back (b);
0fb4aa4b
PA
1147 }
1148
1149 return found;
1150}
1151
95a42b64 1152/* Set the command list of B to COMMANDS. If breakpoint is tracepoint,
4a64f543 1153 validate that only allowed commands are included. */
95a42b64
TT
1154
1155void
4a64f543 1156breakpoint_set_commands (struct breakpoint *b,
12973681 1157 counted_command_line &&commands)
95a42b64 1158{
93921405 1159 validate_commands_for_breakpoint (b, commands.get ());
a7bdde9e 1160
d1b0a7bf 1161 b->commands = std::move (commands);
76727919 1162 gdb::observers::breakpoint_modified.notify (b);
48cb2d85
VP
1163}
1164
45a43567
TT
1165/* Set the internal `silent' flag on the breakpoint. Note that this
1166 is not the same as the "silent" that may appear in the breakpoint's
1167 commands. */
1168
1169void
1170breakpoint_set_silent (struct breakpoint *b, int silent)
1171{
1172 int old_silent = b->silent;
1173
1174 b->silent = silent;
1175 if (old_silent != silent)
76727919 1176 gdb::observers::breakpoint_modified.notify (b);
45a43567
TT
1177}
1178
1179/* Set the thread for this breakpoint. If THREAD is -1, make the
1180 breakpoint work for any thread. */
1181
1182void
1183breakpoint_set_thread (struct breakpoint *b, int thread)
1184{
1185 int old_thread = b->thread;
1186
1187 b->thread = thread;
1188 if (old_thread != thread)
76727919 1189 gdb::observers::breakpoint_modified.notify (b);
45a43567
TT
1190}
1191
1192/* Set the task for this breakpoint. If TASK is 0, make the
1193 breakpoint work for any task. */
1194
1195void
1196breakpoint_set_task (struct breakpoint *b, int task)
1197{
1198 int old_task = b->task;
1199
1200 b->task = task;
1201 if (old_task != task)
76727919 1202 gdb::observers::breakpoint_modified.notify (b);
45a43567
TT
1203}
1204
95a42b64 1205static void
896b6bda 1206commands_command_1 (const char *arg, int from_tty,
4a64f543 1207 struct command_line *control)
95a42b64 1208{
d1b0a7bf 1209 counted_command_line cmd;
999700cd
PW
1210 /* cmd_read will be true once we have read cmd. Note that cmd might still be
1211 NULL after the call to read_command_lines if the user provides an empty
1212 list of command by just typing "end". */
1213 bool cmd_read = false;
95a42b64 1214
896b6bda
PA
1215 std::string new_arg;
1216
95a42b64
TT
1217 if (arg == NULL || !*arg)
1218 {
86b17b60 1219 if (breakpoint_count - prev_breakpoint_count > 1)
896b6bda
PA
1220 new_arg = string_printf ("%d-%d", prev_breakpoint_count + 1,
1221 breakpoint_count);
95a42b64 1222 else if (breakpoint_count > 0)
896b6bda 1223 new_arg = string_printf ("%d", breakpoint_count);
48649e1b
TT
1224 arg = new_arg.c_str ();
1225 }
1226
1227 map_breakpoint_numbers
1228 (arg, [&] (breakpoint *b)
1229 {
999700cd 1230 if (!cmd_read)
48649e1b 1231 {
999700cd 1232 gdb_assert (cmd == NULL);
48649e1b 1233 if (control != NULL)
12973681 1234 cmd = control->body_list_0;
48649e1b
TT
1235 else
1236 {
81b1e71c
TT
1237 std::string str
1238 = string_printf (_("Type commands for breakpoint(s) "
1239 "%s, one per line."),
1240 arg);
48649e1b 1241
60b3cef2
TT
1242 auto do_validate = [=] (const char *line)
1243 {
1244 validate_actionline (line, b);
1245 };
1246 gdb::function_view<void (const char *)> validator;
1247 if (is_tracepoint (b))
1248 validator = do_validate;
1249
1250 cmd = read_command_lines (str.c_str (), from_tty, 1, validator);
48649e1b 1251 }
999700cd 1252 cmd_read = true;
48649e1b
TT
1253 }
1254
1255 /* If a breakpoint was on the list more than once, we don't need to
1256 do anything. */
1257 if (b->commands != cmd)
1258 {
d1b0a7bf 1259 validate_commands_for_breakpoint (b, cmd.get ());
48649e1b 1260 b->commands = cmd;
76727919 1261 gdb::observers::breakpoint_modified.notify (b);
48649e1b
TT
1262 }
1263 });
95a42b64
TT
1264}
1265
1266static void
0b39b52e 1267commands_command (const char *arg, int from_tty)
95a42b64
TT
1268{
1269 commands_command_1 (arg, from_tty, NULL);
c906108c 1270}
40c03ae8
EZ
1271
1272/* Like commands_command, but instead of reading the commands from
1273 input stream, takes them from an already parsed command structure.
1274
1275 This is used by cli-script.c to DTRT with breakpoint commands
1276 that are part of if and while bodies. */
1277enum command_control_type
896b6bda 1278commands_from_control_command (const char *arg, struct command_line *cmd)
40c03ae8 1279{
95a42b64
TT
1280 commands_command_1 (arg, 0, cmd);
1281 return simple_control;
40c03ae8 1282}
876fa593
JK
1283
1284/* Return non-zero if BL->TARGET_INFO contains valid information. */
1285
1286static int
1287bp_location_has_shadow (struct bp_location *bl)
1288{
1289 if (bl->loc_type != bp_loc_software_breakpoint)
1290 return 0;
1291 if (!bl->inserted)
1292 return 0;
1293 if (bl->target_info.shadow_len == 0)
e5dd4106 1294 /* BL isn't valid, or doesn't shadow memory. */
876fa593
JK
1295 return 0;
1296 return 1;
1297}
1298
9d497a19
PA
1299/* Update BUF, which is LEN bytes read from the target address
1300 MEMADDR, by replacing a memory breakpoint with its shadowed
1301 contents.
1302
1303 If READBUF is not NULL, this buffer must not overlap with the of
1304 the breakpoint location's shadow_contents buffer. Otherwise, a
1305 failed assertion internal error will be raised. */
1306
1307static void
1308one_breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1309 const gdb_byte *writebuf_org,
1310 ULONGEST memaddr, LONGEST len,
1311 struct bp_target_info *target_info,
1312 struct gdbarch *gdbarch)
1313{
1314 /* Now do full processing of the found relevant range of elements. */
1315 CORE_ADDR bp_addr = 0;
1316 int bp_size = 0;
1317 int bptoffset = 0;
1318
1319 if (!breakpoint_address_match (target_info->placed_address_space, 0,
1320 current_program_space->aspace, 0))
1321 {
1322 /* The breakpoint is inserted in a different address space. */
1323 return;
1324 }
1325
1326 /* Addresses and length of the part of the breakpoint that
1327 we need to copy. */
1328 bp_addr = target_info->placed_address;
1329 bp_size = target_info->shadow_len;
1330
1331 if (bp_addr + bp_size <= memaddr)
1332 {
1333 /* The breakpoint is entirely before the chunk of memory we are
1334 reading. */
1335 return;
1336 }
1337
1338 if (bp_addr >= memaddr + len)
1339 {
1340 /* The breakpoint is entirely after the chunk of memory we are
1341 reading. */
1342 return;
1343 }
1344
1345 /* Offset within shadow_contents. */
1346 if (bp_addr < memaddr)
1347 {
1348 /* Only copy the second part of the breakpoint. */
1349 bp_size -= memaddr - bp_addr;
1350 bptoffset = memaddr - bp_addr;
1351 bp_addr = memaddr;
1352 }
1353
1354 if (bp_addr + bp_size > memaddr + len)
1355 {
1356 /* Only copy the first part of the breakpoint. */
1357 bp_size -= (bp_addr + bp_size) - (memaddr + len);
1358 }
1359
1360 if (readbuf != NULL)
1361 {
1362 /* Verify that the readbuf buffer does not overlap with the
1363 shadow_contents buffer. */
1364 gdb_assert (target_info->shadow_contents >= readbuf + len
1365 || readbuf >= (target_info->shadow_contents
1366 + target_info->shadow_len));
1367
1368 /* Update the read buffer with this inserted breakpoint's
1369 shadow. */
1370 memcpy (readbuf + bp_addr - memaddr,
1371 target_info->shadow_contents + bptoffset, bp_size);
1372 }
1373 else
1374 {
1375 const unsigned char *bp;
0d5ed153
MR
1376 CORE_ADDR addr = target_info->reqstd_address;
1377 int placed_size;
9d497a19
PA
1378
1379 /* Update the shadow with what we want to write to memory. */
1380 memcpy (target_info->shadow_contents + bptoffset,
1381 writebuf_org + bp_addr - memaddr, bp_size);
1382
1383 /* Determine appropriate breakpoint contents and size for this
1384 address. */
0d5ed153 1385 bp = gdbarch_breakpoint_from_pc (gdbarch, &addr, &placed_size);
9d497a19
PA
1386
1387 /* Update the final write buffer with this inserted
1388 breakpoint's INSN. */
1389 memcpy (writebuf + bp_addr - memaddr, bp + bptoffset, bp_size);
1390 }
1391}
1392
8defab1a 1393/* Update BUF, which is LEN bytes read from the target address MEMADDR,
876fa593
JK
1394 by replacing any memory breakpoints with their shadowed contents.
1395
35c63cd8
JB
1396 If READBUF is not NULL, this buffer must not overlap with any of
1397 the breakpoint location's shadow_contents buffers. Otherwise,
1398 a failed assertion internal error will be raised.
1399
876fa593 1400 The range of shadowed area by each bp_location is:
f5336ca5
PA
1401 bl->address - bp_locations_placed_address_before_address_max
1402 up to bl->address + bp_locations_shadow_len_after_address_max
876fa593
JK
1403 The range we were requested to resolve shadows for is:
1404 memaddr ... memaddr + len
1405 Thus the safe cutoff boundaries for performance optimization are
35df4500 1406 memaddr + len <= (bl->address
f5336ca5 1407 - bp_locations_placed_address_before_address_max)
876fa593 1408 and:
f5336ca5 1409 bl->address + bp_locations_shadow_len_after_address_max <= memaddr */
c906108c 1410
8defab1a 1411void
f0ba3972
PA
1412breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1413 const gdb_byte *writebuf_org,
1414 ULONGEST memaddr, LONGEST len)
c906108c 1415{
4a64f543
MS
1416 /* Left boundary, right boundary and median element of our binary
1417 search. */
876fa593
JK
1418 unsigned bc_l, bc_r, bc;
1419
4a64f543
MS
1420 /* Find BC_L which is a leftmost element which may affect BUF
1421 content. It is safe to report lower value but a failure to
1422 report higher one. */
876fa593
JK
1423
1424 bc_l = 0;
f5336ca5 1425 bc_r = bp_locations_count;
876fa593
JK
1426 while (bc_l + 1 < bc_r)
1427 {
35df4500 1428 struct bp_location *bl;
876fa593
JK
1429
1430 bc = (bc_l + bc_r) / 2;
f5336ca5 1431 bl = bp_locations[bc];
876fa593 1432
4a64f543
MS
1433 /* Check first BL->ADDRESS will not overflow due to the added
1434 constant. Then advance the left boundary only if we are sure
1435 the BC element can in no way affect the BUF content (MEMADDR
1436 to MEMADDR + LEN range).
876fa593 1437
f5336ca5 1438 Use the BP_LOCATIONS_SHADOW_LEN_AFTER_ADDRESS_MAX safety
4a64f543
MS
1439 offset so that we cannot miss a breakpoint with its shadow
1440 range tail still reaching MEMADDR. */
c5aa993b 1441
f5336ca5 1442 if ((bl->address + bp_locations_shadow_len_after_address_max
35df4500 1443 >= bl->address)
f5336ca5 1444 && (bl->address + bp_locations_shadow_len_after_address_max
35df4500 1445 <= memaddr))
876fa593
JK
1446 bc_l = bc;
1447 else
1448 bc_r = bc;
1449 }
1450
128070bb
PA
1451 /* Due to the binary search above, we need to make sure we pick the
1452 first location that's at BC_L's address. E.g., if there are
1453 multiple locations at the same address, BC_L may end up pointing
1454 at a duplicate location, and miss the "master"/"inserted"
1455 location. Say, given locations L1, L2 and L3 at addresses A and
1456 B:
1457
1458 L1@A, L2@A, L3@B, ...
1459
1460 BC_L could end up pointing at location L2, while the "master"
1461 location could be L1. Since the `loc->inserted' flag is only set
1462 on "master" locations, we'd forget to restore the shadow of L1
1463 and L2. */
1464 while (bc_l > 0
f5336ca5 1465 && bp_locations[bc_l]->address == bp_locations[bc_l - 1]->address)
128070bb
PA
1466 bc_l--;
1467
876fa593
JK
1468 /* Now do full processing of the found relevant range of elements. */
1469
f5336ca5 1470 for (bc = bc_l; bc < bp_locations_count; bc++)
c5aa993b 1471 {
f5336ca5 1472 struct bp_location *bl = bp_locations[bc];
876fa593 1473
35df4500
TJB
1474 /* bp_location array has BL->OWNER always non-NULL. */
1475 if (bl->owner->type == bp_none)
8a3fe4f8 1476 warning (_("reading through apparently deleted breakpoint #%d?"),
35df4500 1477 bl->owner->number);
ffce0d52 1478
e5dd4106 1479 /* Performance optimization: any further element can no longer affect BUF
876fa593
JK
1480 content. */
1481
f5336ca5
PA
1482 if (bl->address >= bp_locations_placed_address_before_address_max
1483 && memaddr + len <= (bl->address
1484 - bp_locations_placed_address_before_address_max))
876fa593
JK
1485 break;
1486
35df4500 1487 if (!bp_location_has_shadow (bl))
c5aa993b 1488 continue;
6c95b8df 1489
9d497a19
PA
1490 one_breakpoint_xfer_memory (readbuf, writebuf, writebuf_org,
1491 memaddr, len, &bl->target_info, bl->gdbarch);
1492 }
c906108c 1493}
9d497a19 1494
f2478a7e 1495/* See breakpoint.h. */
b775012e 1496
f2478a7e 1497bool
b775012e
LM
1498is_breakpoint (const struct breakpoint *bpt)
1499{
1500 return (bpt->type == bp_breakpoint
e7e0cddf
SS
1501 || bpt->type == bp_hardware_breakpoint
1502 || bpt->type == bp_dprintf);
b775012e
LM
1503}
1504
60e1c644
PA
1505/* Return true if BPT is of any hardware watchpoint kind. */
1506
f2478a7e 1507static bool
d77f58be 1508is_hardware_watchpoint (const struct breakpoint *bpt)
a5606eee
VP
1509{
1510 return (bpt->type == bp_hardware_watchpoint
1511 || bpt->type == bp_read_watchpoint
1512 || bpt->type == bp_access_watchpoint);
1513}
7270d8f2 1514
f2478a7e 1515/* See breakpoint.h. */
60e1c644 1516
f2478a7e 1517bool
d77f58be 1518is_watchpoint (const struct breakpoint *bpt)
60e1c644
PA
1519{
1520 return (is_hardware_watchpoint (bpt)
1521 || bpt->type == bp_watchpoint);
1522}
1523
3a5c3e22
PA
1524/* Returns true if the current thread and its running state are safe
1525 to evaluate or update watchpoint B. Watchpoints on local
1526 expressions need to be evaluated in the context of the thread that
1527 was current when the watchpoint was created, and, that thread needs
1528 to be stopped to be able to select the correct frame context.
1529 Watchpoints on global expressions can be evaluated on any thread,
1530 and in any state. It is presently left to the target allowing
1531 memory accesses when threads are running. */
f6bc2008
PA
1532
1533static int
3a5c3e22 1534watchpoint_in_thread_scope (struct watchpoint *b)
f6bc2008 1535{
c1fc2657 1536 return (b->pspace == current_program_space
d7e15655
TT
1537 && (b->watchpoint_thread == null_ptid
1538 || (inferior_ptid == b->watchpoint_thread
00431a78 1539 && !inferior_thread ()->executing)));
f6bc2008
PA
1540}
1541
d0fb5eae
JK
1542/* Set watchpoint B to disp_del_at_next_stop, even including its possible
1543 associated bp_watchpoint_scope breakpoint. */
1544
1545static void
3a5c3e22 1546watchpoint_del_at_next_stop (struct watchpoint *w)
d0fb5eae 1547{
c1fc2657 1548 if (w->related_breakpoint != w)
d0fb5eae 1549 {
c1fc2657
SM
1550 gdb_assert (w->related_breakpoint->type == bp_watchpoint_scope);
1551 gdb_assert (w->related_breakpoint->related_breakpoint == w);
1552 w->related_breakpoint->disposition = disp_del_at_next_stop;
1553 w->related_breakpoint->related_breakpoint = w->related_breakpoint;
1554 w->related_breakpoint = w;
d0fb5eae 1555 }
c1fc2657 1556 w->disposition = disp_del_at_next_stop;
d0fb5eae
JK
1557}
1558
bb9d5f81
PP
1559/* Extract a bitfield value from value VAL using the bit parameters contained in
1560 watchpoint W. */
1561
1562static struct value *
1563extract_bitfield_from_watchpoint_value (struct watchpoint *w, struct value *val)
1564{
1565 struct value *bit_val;
1566
1567 if (val == NULL)
1568 return NULL;
1569
1570 bit_val = allocate_value (value_type (val));
1571
1572 unpack_value_bitfield (bit_val,
1573 w->val_bitpos,
1574 w->val_bitsize,
1575 value_contents_for_printing (val),
1576 value_offset (val),
1577 val);
1578
1579 return bit_val;
1580}
1581
c6d81124
PA
1582/* Allocate a dummy location and add it to B, which must be a software
1583 watchpoint. This is required because even if a software watchpoint
1584 is not watching any memory, bpstat_stop_status requires a location
1585 to be able to report stops. */
1586
1587static void
1588software_watchpoint_add_no_memory_location (struct breakpoint *b,
1589 struct program_space *pspace)
1590{
1591 gdb_assert (b->type == bp_watchpoint && b->loc == NULL);
1592
1593 b->loc = allocate_bp_location (b);
1594 b->loc->pspace = pspace;
1595 b->loc->address = -1;
1596 b->loc->length = -1;
1597}
1598
1599/* Returns true if B is a software watchpoint that is not watching any
1600 memory (e.g., "watch $pc"). */
1601
f2478a7e 1602static bool
c6d81124
PA
1603is_no_memory_software_watchpoint (struct breakpoint *b)
1604{
1605 return (b->type == bp_watchpoint
1606 && b->loc != NULL
1607 && b->loc->next == NULL
1608 && b->loc->address == -1
1609 && b->loc->length == -1);
1610}
1611
567e1b4e
JB
1612/* Assuming that B is a watchpoint:
1613 - Reparse watchpoint expression, if REPARSE is non-zero
a5606eee 1614 - Evaluate expression and store the result in B->val
567e1b4e
JB
1615 - Evaluate the condition if there is one, and store the result
1616 in b->loc->cond.
a5606eee
VP
1617 - Update the list of values that must be watched in B->loc.
1618
4a64f543
MS
1619 If the watchpoint disposition is disp_del_at_next_stop, then do
1620 nothing. If this is local watchpoint that is out of scope, delete
1621 it.
1622
1623 Even with `set breakpoint always-inserted on' the watchpoints are
1624 removed + inserted on each stop here. Normal breakpoints must
1625 never be removed because they might be missed by a running thread
1626 when debugging in non-stop mode. On the other hand, hardware
1627 watchpoints (is_hardware_watchpoint; processed here) are specific
1628 to each LWP since they are stored in each LWP's hardware debug
1629 registers. Therefore, such LWP must be stopped first in order to
1630 be able to modify its hardware watchpoints.
1631
1632 Hardware watchpoints must be reset exactly once after being
1633 presented to the user. It cannot be done sooner, because it would
1634 reset the data used to present the watchpoint hit to the user. And
1635 it must not be done later because it could display the same single
1636 watchpoint hit during multiple GDB stops. Note that the latter is
1637 relevant only to the hardware watchpoint types bp_read_watchpoint
1638 and bp_access_watchpoint. False hit by bp_hardware_watchpoint is
1639 not user-visible - its hit is suppressed if the memory content has
1640 not changed.
1641
1642 The following constraints influence the location where we can reset
1643 hardware watchpoints:
1644
1645 * target_stopped_by_watchpoint and target_stopped_data_address are
1646 called several times when GDB stops.
1647
1648 [linux]
1649 * Multiple hardware watchpoints can be hit at the same time,
1650 causing GDB to stop. GDB only presents one hardware watchpoint
1651 hit at a time as the reason for stopping, and all the other hits
1652 are presented later, one after the other, each time the user
1653 requests the execution to be resumed. Execution is not resumed
1654 for the threads still having pending hit event stored in
1655 LWP_INFO->STATUS. While the watchpoint is already removed from
1656 the inferior on the first stop the thread hit event is kept being
1657 reported from its cached value by linux_nat_stopped_data_address
1658 until the real thread resume happens after the watchpoint gets
1659 presented and thus its LWP_INFO->STATUS gets reset.
1660
1661 Therefore the hardware watchpoint hit can get safely reset on the
1662 watchpoint removal from inferior. */
a79d3c27 1663
b40ce68a 1664static void
3a5c3e22 1665update_watchpoint (struct watchpoint *b, int reparse)
7270d8f2 1666{
a5606eee 1667 int within_current_scope;
a5606eee 1668 struct frame_id saved_frame_id;
66076460 1669 int frame_saved;
a5606eee 1670
f6bc2008
PA
1671 /* If this is a local watchpoint, we only want to check if the
1672 watchpoint frame is in scope if the current thread is the thread
1673 that was used to create the watchpoint. */
1674 if (!watchpoint_in_thread_scope (b))
1675 return;
1676
c1fc2657 1677 if (b->disposition == disp_del_at_next_stop)
a5606eee
VP
1678 return;
1679
66076460 1680 frame_saved = 0;
a5606eee
VP
1681
1682 /* Determine if the watchpoint is within scope. */
1683 if (b->exp_valid_block == NULL)
1684 within_current_scope = 1;
1685 else
1686 {
b5db5dfc
UW
1687 struct frame_info *fi = get_current_frame ();
1688 struct gdbarch *frame_arch = get_frame_arch (fi);
1689 CORE_ADDR frame_pc = get_frame_pc (fi);
1690
c9cf6e20
MG
1691 /* If we're at a point where the stack has been destroyed
1692 (e.g. in a function epilogue), unwinding may not work
1693 properly. Do not attempt to recreate locations at this
b5db5dfc 1694 point. See similar comments in watchpoint_check. */
c9cf6e20 1695 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
b5db5dfc 1696 return;
66076460
DJ
1697
1698 /* Save the current frame's ID so we can restore it after
1699 evaluating the watchpoint expression on its own frame. */
1700 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
1701 took a frame parameter, so that we didn't have to change the
1702 selected frame. */
1703 frame_saved = 1;
1704 saved_frame_id = get_frame_id (get_selected_frame (NULL));
1705
a5606eee
VP
1706 fi = frame_find_by_id (b->watchpoint_frame);
1707 within_current_scope = (fi != NULL);
1708 if (within_current_scope)
1709 select_frame (fi);
1710 }
1711
b5db5dfc
UW
1712 /* We don't free locations. They are stored in the bp_location array
1713 and update_global_location_list will eventually delete them and
1714 remove breakpoints if needed. */
c1fc2657 1715 b->loc = NULL;
b5db5dfc 1716
a5606eee
VP
1717 if (within_current_scope && reparse)
1718 {
bbc13ae3 1719 const char *s;
d63d0675 1720
4d01a485 1721 b->exp.reset ();
d63d0675 1722 s = b->exp_string_reparse ? b->exp_string_reparse : b->exp_string;
1bb9788d 1723 b->exp = parse_exp_1 (&s, 0, b->exp_valid_block, 0);
a5606eee
VP
1724 /* If the meaning of expression itself changed, the old value is
1725 no longer relevant. We don't want to report a watchpoint hit
1726 to the user when the old value and the new value may actually
1727 be completely different objects. */
fa4727a6 1728 b->val = NULL;
4c1d86d9 1729 b->val_valid = false;
60e1c644
PA
1730
1731 /* Note that unlike with breakpoints, the watchpoint's condition
1732 expression is stored in the breakpoint object, not in the
1733 locations (re)created below. */
c1fc2657 1734 if (b->cond_string != NULL)
60e1c644 1735 {
4d01a485 1736 b->cond_exp.reset ();
60e1c644 1737
c1fc2657 1738 s = b->cond_string;
1bb9788d 1739 b->cond_exp = parse_exp_1 (&s, 0, b->cond_exp_valid_block, 0);
60e1c644 1740 }
a5606eee 1741 }
a5606eee
VP
1742
1743 /* If we failed to parse the expression, for example because
1744 it refers to a global variable in a not-yet-loaded shared library,
1745 don't try to insert watchpoint. We don't automatically delete
1746 such watchpoint, though, since failure to parse expression
1747 is different from out-of-scope watchpoint. */
e8369a73 1748 if (!target_has_execution)
2d134ed3
PA
1749 {
1750 /* Without execution, memory can't change. No use to try and
1751 set watchpoint locations. The watchpoint will be reset when
1752 the target gains execution, through breakpoint_re_set. */
e8369a73
AB
1753 if (!can_use_hw_watchpoints)
1754 {
c1fc2657
SM
1755 if (b->ops->works_in_software_mode (b))
1756 b->type = bp_watchpoint;
e8369a73 1757 else
638aa5a1
AB
1758 error (_("Can't set read/access watchpoint when "
1759 "hardware watchpoints are disabled."));
e8369a73 1760 }
2d134ed3
PA
1761 }
1762 else if (within_current_scope && b->exp)
a5606eee 1763 {
0cf6dd15 1764 int pc = 0;
a6535de1 1765 std::vector<value_ref_ptr> val_chain;
8d49165d 1766 struct value *v, *result;
2d134ed3 1767 struct program_space *frame_pspace;
a5606eee 1768
4d01a485 1769 fetch_subexp_value (b->exp.get (), &pc, &v, &result, &val_chain, 0);
a5606eee 1770
a5606eee
VP
1771 /* Avoid setting b->val if it's already set. The meaning of
1772 b->val is 'the last value' user saw, and we should update
1773 it only if we reported that last value to user. As it
9c06b0b4
TJB
1774 happens, the code that reports it updates b->val directly.
1775 We don't keep track of the memory value for masked
1776 watchpoints. */
c1fc2657 1777 if (!b->val_valid && !is_masked_watchpoint (b))
fa4727a6 1778 {
bb9d5f81 1779 if (b->val_bitsize != 0)
850645cf
TT
1780 v = extract_bitfield_from_watchpoint_value (b, v);
1781 b->val = release_value (v);
4c1d86d9 1782 b->val_valid = true;
fa4727a6 1783 }
a5606eee 1784
2d134ed3
PA
1785 frame_pspace = get_frame_program_space (get_selected_frame (NULL));
1786
a5606eee 1787 /* Look at each value on the value chain. */
a6535de1
TT
1788 gdb_assert (!val_chain.empty ());
1789 for (const value_ref_ptr &iter : val_chain)
a5606eee 1790 {
a6535de1
TT
1791 v = iter.get ();
1792
a5606eee
VP
1793 /* If it's a memory location, and GDB actually needed
1794 its contents to evaluate the expression, then we
fa4727a6
DJ
1795 must watch it. If the first value returned is
1796 still lazy, that means an error occurred reading it;
1797 watch it anyway in case it becomes readable. */
a5606eee 1798 if (VALUE_LVAL (v) == lval_memory
a6535de1 1799 && (v == val_chain[0] || ! value_lazy (v)))
a5606eee
VP
1800 {
1801 struct type *vtype = check_typedef (value_type (v));
7270d8f2 1802
a5606eee
VP
1803 /* We only watch structs and arrays if user asked
1804 for it explicitly, never if they just happen to
1805 appear in the middle of some value chain. */
fa4727a6 1806 if (v == result
a5606eee
VP
1807 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
1808 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
1809 {
1810 CORE_ADDR addr;
f486487f 1811 enum target_hw_bp_type type;
a5606eee 1812 struct bp_location *loc, **tmp;
bb9d5f81
PP
1813 int bitpos = 0, bitsize = 0;
1814
1815 if (value_bitsize (v) != 0)
1816 {
1817 /* Extract the bit parameters out from the bitfield
1818 sub-expression. */
1819 bitpos = value_bitpos (v);
1820 bitsize = value_bitsize (v);
1821 }
1822 else if (v == result && b->val_bitsize != 0)
1823 {
1824 /* If VAL_BITSIZE != 0 then RESULT is actually a bitfield
1825 lvalue whose bit parameters are saved in the fields
1826 VAL_BITPOS and VAL_BITSIZE. */
1827 bitpos = b->val_bitpos;
1828 bitsize = b->val_bitsize;
1829 }
a5606eee 1830
42ae5230 1831 addr = value_address (v);
bb9d5f81
PP
1832 if (bitsize != 0)
1833 {
1834 /* Skip the bytes that don't contain the bitfield. */
1835 addr += bitpos / 8;
1836 }
1837
a5606eee 1838 type = hw_write;
c1fc2657 1839 if (b->type == bp_read_watchpoint)
a5606eee 1840 type = hw_read;
c1fc2657 1841 else if (b->type == bp_access_watchpoint)
a5606eee 1842 type = hw_access;
3a5c3e22 1843
c1fc2657
SM
1844 loc = allocate_bp_location (b);
1845 for (tmp = &(b->loc); *tmp != NULL; tmp = &((*tmp)->next))
a5606eee
VP
1846 ;
1847 *tmp = loc;
a6d9a66e 1848 loc->gdbarch = get_type_arch (value_type (v));
6c95b8df
PA
1849
1850 loc->pspace = frame_pspace;
f17d9474 1851 loc->address = address_significant (loc->gdbarch, addr);
bb9d5f81
PP
1852
1853 if (bitsize != 0)
1854 {
1855 /* Just cover the bytes that make up the bitfield. */
1856 loc->length = ((bitpos % 8) + bitsize + 7) / 8;
1857 }
1858 else
1859 loc->length = TYPE_LENGTH (value_type (v));
1860
a5606eee
VP
1861 loc->watchpoint_type = type;
1862 }
1863 }
9fa40276
TJB
1864 }
1865
1866 /* Change the type of breakpoint between hardware assisted or
1867 an ordinary watchpoint depending on the hardware support
1868 and free hardware slots. REPARSE is set when the inferior
1869 is started. */
a9634178 1870 if (reparse)
9fa40276 1871 {
e09342b5 1872 int reg_cnt;
9fa40276
TJB
1873 enum bp_loc_type loc_type;
1874 struct bp_location *bl;
a5606eee 1875
a9634178 1876 reg_cnt = can_use_hardware_watchpoint (val_chain);
e09342b5
TJB
1877
1878 if (reg_cnt)
9fa40276
TJB
1879 {
1880 int i, target_resources_ok, other_type_used;
a1398e0c 1881 enum bptype type;
9fa40276 1882
a9634178
TJB
1883 /* Use an exact watchpoint when there's only one memory region to be
1884 watched, and only one debug register is needed to watch it. */
1885 b->exact = target_exact_watchpoints && reg_cnt == 1;
1886
9fa40276 1887 /* We need to determine how many resources are already
e09342b5
TJB
1888 used for all other hardware watchpoints plus this one
1889 to see if we still have enough resources to also fit
a1398e0c
PA
1890 this watchpoint in as well. */
1891
1892 /* If this is a software watchpoint, we try to turn it
1893 to a hardware one -- count resources as if B was of
1894 hardware watchpoint type. */
c1fc2657 1895 type = b->type;
a1398e0c
PA
1896 if (type == bp_watchpoint)
1897 type = bp_hardware_watchpoint;
1898
1899 /* This watchpoint may or may not have been placed on
1900 the list yet at this point (it won't be in the list
1901 if we're trying to create it for the first time,
1902 through watch_command), so always account for it
1903 manually. */
1904
1905 /* Count resources used by all watchpoints except B. */
c1fc2657 1906 i = hw_watchpoint_used_count_others (b, type, &other_type_used);
a1398e0c
PA
1907
1908 /* Add in the resources needed for B. */
c1fc2657 1909 i += hw_watchpoint_use_count (b);
a1398e0c
PA
1910
1911 target_resources_ok
1912 = target_can_use_hardware_watchpoint (type, i, other_type_used);
e09342b5 1913 if (target_resources_ok <= 0)
a9634178 1914 {
c1fc2657 1915 int sw_mode = b->ops->works_in_software_mode (b);
9c06b0b4
TJB
1916
1917 if (target_resources_ok == 0 && !sw_mode)
a9634178
TJB
1918 error (_("Target does not support this type of "
1919 "hardware watchpoint."));
9c06b0b4
TJB
1920 else if (target_resources_ok < 0 && !sw_mode)
1921 error (_("There are not enough available hardware "
1922 "resources for this watchpoint."));
a1398e0c
PA
1923
1924 /* Downgrade to software watchpoint. */
c1fc2657 1925 b->type = bp_watchpoint;
a1398e0c
PA
1926 }
1927 else
1928 {
1929 /* If this was a software watchpoint, we've just
1930 found we have enough resources to turn it to a
1931 hardware watchpoint. Otherwise, this is a
1932 nop. */
c1fc2657 1933 b->type = type;
a9634178 1934 }
9fa40276 1935 }
c1fc2657 1936 else if (!b->ops->works_in_software_mode (b))
638aa5a1
AB
1937 {
1938 if (!can_use_hw_watchpoints)
1939 error (_("Can't set read/access watchpoint when "
1940 "hardware watchpoints are disabled."));
1941 else
1942 error (_("Expression cannot be implemented with "
1943 "read/access watchpoint."));
1944 }
9fa40276 1945 else
c1fc2657 1946 b->type = bp_watchpoint;
9fa40276 1947
c1fc2657 1948 loc_type = (b->type == bp_watchpoint? bp_loc_other
9fa40276 1949 : bp_loc_hardware_watchpoint);
c1fc2657 1950 for (bl = b->loc; bl; bl = bl->next)
9fa40276
TJB
1951 bl->loc_type = loc_type;
1952 }
1953
c7437ca6
PA
1954 /* If a software watchpoint is not watching any memory, then the
1955 above left it without any location set up. But,
1956 bpstat_stop_status requires a location to be able to report
1957 stops, so make sure there's at least a dummy one. */
c1fc2657
SM
1958 if (b->type == bp_watchpoint && b->loc == NULL)
1959 software_watchpoint_add_no_memory_location (b, frame_pspace);
a5606eee
VP
1960 }
1961 else if (!within_current_scope)
7270d8f2 1962 {
ac74f770
MS
1963 printf_filtered (_("\
1964Watchpoint %d deleted because the program has left the block\n\
1965in which its expression is valid.\n"),
c1fc2657 1966 b->number);
d0fb5eae 1967 watchpoint_del_at_next_stop (b);
7270d8f2 1968 }
a5606eee
VP
1969
1970 /* Restore the selected frame. */
66076460
DJ
1971 if (frame_saved)
1972 select_frame (frame_find_by_id (saved_frame_id));
7270d8f2
OF
1973}
1974
a5606eee 1975
74960c60 1976/* Returns 1 iff breakpoint location should be
1e4d1764
YQ
1977 inserted in the inferior. We don't differentiate the type of BL's owner
1978 (breakpoint vs. tracepoint), although insert_location in tracepoint's
1979 breakpoint_ops is not defined, because in insert_bp_location,
1980 tracepoint's insert_location will not be called. */
74960c60 1981static int
35df4500 1982should_be_inserted (struct bp_location *bl)
74960c60 1983{
35df4500 1984 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
74960c60
VP
1985 return 0;
1986
35df4500 1987 if (bl->owner->disposition == disp_del_at_next_stop)
74960c60
VP
1988 return 0;
1989
35df4500 1990 if (!bl->enabled || bl->shlib_disabled || bl->duplicate)
74960c60
VP
1991 return 0;
1992
f8eba3c6
TT
1993 if (user_breakpoint_p (bl->owner) && bl->pspace->executing_startup)
1994 return 0;
1995
56710373
PA
1996 /* This is set for example, when we're attached to the parent of a
1997 vfork, and have detached from the child. The child is running
1998 free, and we expect it to do an exec or exit, at which point the
1999 OS makes the parent schedulable again (and the target reports
2000 that the vfork is done). Until the child is done with the shared
2001 memory region, do not insert breakpoints in the parent, otherwise
2002 the child could still trip on the parent's breakpoints. Since
2003 the parent is blocked anyway, it won't miss any breakpoint. */
35df4500 2004 if (bl->pspace->breakpoints_not_allowed)
56710373
PA
2005 return 0;
2006
31e77af2 2007 /* Don't insert a breakpoint if we're trying to step past its
21edc42f
YQ
2008 location, except if the breakpoint is a single-step breakpoint,
2009 and the breakpoint's thread is the thread which is stepping past
2010 a breakpoint. */
31e77af2
PA
2011 if ((bl->loc_type == bp_loc_software_breakpoint
2012 || bl->loc_type == bp_loc_hardware_breakpoint)
2013 && stepping_past_instruction_at (bl->pspace->aspace,
21edc42f
YQ
2014 bl->address)
2015 /* The single-step breakpoint may be inserted at the location
2016 we're trying to step if the instruction branches to itself.
2017 However, the instruction won't be executed at all and it may
2018 break the semantics of the instruction, for example, the
2019 instruction is a conditional branch or updates some flags.
2020 We can't fix it unless GDB is able to emulate the instruction
2021 or switch to displaced stepping. */
2022 && !(bl->owner->type == bp_single_step
2023 && thread_is_stepping_over_breakpoint (bl->owner->thread)))
e558d7c1
PA
2024 {
2025 if (debug_infrun)
2026 {
2027 fprintf_unfiltered (gdb_stdlog,
2028 "infrun: skipping breakpoint: "
2029 "stepping past insn at: %s\n",
2030 paddress (bl->gdbarch, bl->address));
2031 }
2032 return 0;
2033 }
31e77af2 2034
963f9c80
PA
2035 /* Don't insert watchpoints if we're trying to step past the
2036 instruction that triggered one. */
2037 if ((bl->loc_type == bp_loc_hardware_watchpoint)
2038 && stepping_past_nonsteppable_watchpoint ())
2039 {
2040 if (debug_infrun)
2041 {
2042 fprintf_unfiltered (gdb_stdlog,
2043 "infrun: stepping past non-steppable watchpoint. "
2044 "skipping watchpoint at %s:%d\n",
2045 paddress (bl->gdbarch, bl->address),
2046 bl->length);
2047 }
2048 return 0;
2049 }
2050
74960c60
VP
2051 return 1;
2052}
2053
934709f0
PW
2054/* Same as should_be_inserted but does the check assuming
2055 that the location is not duplicated. */
2056
2057static int
2058unduplicated_should_be_inserted (struct bp_location *bl)
2059{
2060 int result;
2061 const int save_duplicate = bl->duplicate;
2062
2063 bl->duplicate = 0;
2064 result = should_be_inserted (bl);
2065 bl->duplicate = save_duplicate;
2066 return result;
2067}
2068
b775012e
LM
2069/* Parses a conditional described by an expression COND into an
2070 agent expression bytecode suitable for evaluation
2071 by the bytecode interpreter. Return NULL if there was
2072 any error during parsing. */
2073
833177a4 2074static agent_expr_up
b775012e
LM
2075parse_cond_to_aexpr (CORE_ADDR scope, struct expression *cond)
2076{
833177a4 2077 if (cond == NULL)
b775012e
LM
2078 return NULL;
2079
833177a4
PA
2080 agent_expr_up aexpr;
2081
b775012e
LM
2082 /* We don't want to stop processing, so catch any errors
2083 that may show up. */
a70b8144 2084 try
b775012e 2085 {
036e657b 2086 aexpr = gen_eval_for_expr (scope, cond);
b775012e
LM
2087 }
2088
230d2906 2089 catch (const gdb_exception_error &ex)
b775012e
LM
2090 {
2091 /* If we got here, it means the condition could not be parsed to a valid
2092 bytecode expression and thus can't be evaluated on the target's side.
2093 It's no use iterating through the conditions. */
b775012e
LM
2094 }
2095
2096 /* We have a valid agent expression. */
2097 return aexpr;
2098}
2099
2100/* Based on location BL, create a list of breakpoint conditions to be
2101 passed on to the target. If we have duplicated locations with different
2102 conditions, we will add such conditions to the list. The idea is that the
2103 target will evaluate the list of conditions and will only notify GDB when
2104 one of them is true. */
2105
2106static void
2107build_target_condition_list (struct bp_location *bl)
2108{
2109 struct bp_location **locp = NULL, **loc2p;
2110 int null_condition_or_parse_error = 0;
2111 int modified = bl->needs_update;
2112 struct bp_location *loc;
2113
8b4f3082 2114 /* Release conditions left over from a previous insert. */
3cde5c42 2115 bl->target_info.conditions.clear ();
8b4f3082 2116
b775012e
LM
2117 /* This is only meaningful if the target is
2118 evaluating conditions and if the user has
2119 opted for condition evaluation on the target's
2120 side. */
2121 if (gdb_evaluates_breakpoint_condition_p ()
2122 || !target_supports_evaluation_of_breakpoint_conditions ())
2123 return;
2124
2125 /* Do a first pass to check for locations with no assigned
2126 conditions or conditions that fail to parse to a valid agent expression
2127 bytecode. If any of these happen, then it's no use to send conditions
2128 to the target since this location will always trigger and generate a
2129 response back to GDB. */
2130 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2131 {
2132 loc = (*loc2p);
2133 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2134 {
2135 if (modified)
2136 {
b775012e
LM
2137 /* Re-parse the conditions since something changed. In that
2138 case we already freed the condition bytecodes (see
2139 force_breakpoint_reinsertion). We just
2140 need to parse the condition to bytecodes again. */
833177a4
PA
2141 loc->cond_bytecode = parse_cond_to_aexpr (bl->address,
2142 loc->cond.get ());
b775012e
LM
2143 }
2144
2145 /* If we have a NULL bytecode expression, it means something
2146 went wrong or we have a null condition expression. */
2147 if (!loc->cond_bytecode)
2148 {
2149 null_condition_or_parse_error = 1;
2150 break;
2151 }
2152 }
2153 }
2154
2155 /* If any of these happened, it means we will have to evaluate the conditions
2156 for the location's address on gdb's side. It is no use keeping bytecodes
2157 for all the other duplicate locations, thus we free all of them here.
2158
2159 This is so we have a finer control over which locations' conditions are
2160 being evaluated by GDB or the remote stub. */
2161 if (null_condition_or_parse_error)
2162 {
2163 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2164 {
2165 loc = (*loc2p);
2166 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2167 {
2168 /* Only go as far as the first NULL bytecode is
2169 located. */
2170 if (!loc->cond_bytecode)
2171 return;
2172
833177a4 2173 loc->cond_bytecode.reset ();
b775012e
LM
2174 }
2175 }
2176 }
2177
2178 /* No NULL conditions or failed bytecode generation. Build a condition list
2179 for this location's address. */
2180 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2181 {
2182 loc = (*loc2p);
2183 if (loc->cond
2184 && is_breakpoint (loc->owner)
2185 && loc->pspace->num == bl->pspace->num
2186 && loc->owner->enable_state == bp_enabled
2187 && loc->enabled)
3cde5c42
PA
2188 {
2189 /* Add the condition to the vector. This will be used later
2190 to send the conditions to the target. */
2191 bl->target_info.conditions.push_back (loc->cond_bytecode.get ());
2192 }
b775012e
LM
2193 }
2194
2195 return;
2196}
2197
d3ce09f5
SS
2198/* Parses a command described by string CMD into an agent expression
2199 bytecode suitable for evaluation by the bytecode interpreter.
2200 Return NULL if there was any error during parsing. */
2201
833177a4 2202static agent_expr_up
d3ce09f5
SS
2203parse_cmd_to_aexpr (CORE_ADDR scope, char *cmd)
2204{
bbc13ae3
KS
2205 const char *cmdrest;
2206 const char *format_start, *format_end;
d3ce09f5
SS
2207 struct gdbarch *gdbarch = get_current_arch ();
2208
833177a4 2209 if (cmd == NULL)
d3ce09f5
SS
2210 return NULL;
2211
2212 cmdrest = cmd;
2213
2214 if (*cmdrest == ',')
2215 ++cmdrest;
f1735a53 2216 cmdrest = skip_spaces (cmdrest);
d3ce09f5
SS
2217
2218 if (*cmdrest++ != '"')
2219 error (_("No format string following the location"));
2220
2221 format_start = cmdrest;
2222
8e481c3b 2223 format_pieces fpieces (&cmdrest);
d3ce09f5
SS
2224
2225 format_end = cmdrest;
2226
2227 if (*cmdrest++ != '"')
2228 error (_("Bad format string, non-terminated '\"'."));
2229
f1735a53 2230 cmdrest = skip_spaces (cmdrest);
d3ce09f5
SS
2231
2232 if (!(*cmdrest == ',' || *cmdrest == '\0'))
2233 error (_("Invalid argument syntax"));
2234
2235 if (*cmdrest == ',')
2236 cmdrest++;
f1735a53 2237 cmdrest = skip_spaces (cmdrest);
d3ce09f5
SS
2238
2239 /* For each argument, make an expression. */
2240
8e481c3b 2241 std::vector<struct expression *> argvec;
d3ce09f5
SS
2242 while (*cmdrest != '\0')
2243 {
bbc13ae3 2244 const char *cmd1;
d3ce09f5
SS
2245
2246 cmd1 = cmdrest;
4d01a485 2247 expression_up expr = parse_exp_1 (&cmd1, scope, block_for_pc (scope), 1);
8e481c3b 2248 argvec.push_back (expr.release ());
d3ce09f5
SS
2249 cmdrest = cmd1;
2250 if (*cmdrest == ',')
2251 ++cmdrest;
2252 }
2253
833177a4
PA
2254 agent_expr_up aexpr;
2255
d3ce09f5
SS
2256 /* We don't want to stop processing, so catch any errors
2257 that may show up. */
a70b8144 2258 try
d3ce09f5 2259 {
036e657b
JB
2260 aexpr = gen_printf (scope, gdbarch, 0, 0,
2261 format_start, format_end - format_start,
8e481c3b 2262 argvec.size (), argvec.data ());
d3ce09f5 2263 }
230d2906 2264 catch (const gdb_exception_error &ex)
d3ce09f5
SS
2265 {
2266 /* If we got here, it means the command could not be parsed to a valid
2267 bytecode expression and thus can't be evaluated on the target's side.
2268 It's no use iterating through the other commands. */
d3ce09f5 2269 }
492d29ea 2270
d3ce09f5
SS
2271 /* We have a valid agent expression, return it. */
2272 return aexpr;
2273}
2274
2275/* Based on location BL, create a list of breakpoint commands to be
2276 passed on to the target. If we have duplicated locations with
2277 different commands, we will add any such to the list. */
2278
2279static void
2280build_target_command_list (struct bp_location *bl)
2281{
2282 struct bp_location **locp = NULL, **loc2p;
2283 int null_command_or_parse_error = 0;
2284 int modified = bl->needs_update;
2285 struct bp_location *loc;
2286
3cde5c42
PA
2287 /* Clear commands left over from a previous insert. */
2288 bl->target_info.tcommands.clear ();
8b4f3082 2289
41fac0cf 2290 if (!target_can_run_breakpoint_commands ())
d3ce09f5
SS
2291 return;
2292
41fac0cf
PA
2293 /* For now, limit to agent-style dprintf breakpoints. */
2294 if (dprintf_style != dprintf_style_agent)
d3ce09f5
SS
2295 return;
2296
41fac0cf
PA
2297 /* For now, if we have any duplicate location that isn't a dprintf,
2298 don't install the target-side commands, as that would make the
2299 breakpoint not be reported to the core, and we'd lose
2300 control. */
2301 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2302 {
2303 loc = (*loc2p);
2304 if (is_breakpoint (loc->owner)
2305 && loc->pspace->num == bl->pspace->num
2306 && loc->owner->type != bp_dprintf)
2307 return;
2308 }
2309
d3ce09f5
SS
2310 /* Do a first pass to check for locations with no assigned
2311 conditions or conditions that fail to parse to a valid agent expression
2312 bytecode. If any of these happen, then it's no use to send conditions
2313 to the target since this location will always trigger and generate a
2314 response back to GDB. */
2315 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2316 {
2317 loc = (*loc2p);
2318 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2319 {
2320 if (modified)
2321 {
d3ce09f5
SS
2322 /* Re-parse the commands since something changed. In that
2323 case we already freed the command bytecodes (see
2324 force_breakpoint_reinsertion). We just
2325 need to parse the command to bytecodes again. */
833177a4
PA
2326 loc->cmd_bytecode
2327 = parse_cmd_to_aexpr (bl->address,
2328 loc->owner->extra_string);
d3ce09f5
SS
2329 }
2330
2331 /* If we have a NULL bytecode expression, it means something
2332 went wrong or we have a null command expression. */
2333 if (!loc->cmd_bytecode)
2334 {
2335 null_command_or_parse_error = 1;
2336 break;
2337 }
2338 }
2339 }
2340
2341 /* If anything failed, then we're not doing target-side commands,
2342 and so clean up. */
2343 if (null_command_or_parse_error)
2344 {
2345 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2346 {
2347 loc = (*loc2p);
2348 if (is_breakpoint (loc->owner)
2349 && loc->pspace->num == bl->pspace->num)
2350 {
2351 /* Only go as far as the first NULL bytecode is
2352 located. */
40fb6c5e 2353 if (loc->cmd_bytecode == NULL)
d3ce09f5
SS
2354 return;
2355
833177a4 2356 loc->cmd_bytecode.reset ();
d3ce09f5
SS
2357 }
2358 }
2359 }
2360
2361 /* No NULL commands or failed bytecode generation. Build a command list
2362 for this location's address. */
2363 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2364 {
2365 loc = (*loc2p);
2366 if (loc->owner->extra_string
2367 && is_breakpoint (loc->owner)
2368 && loc->pspace->num == bl->pspace->num
2369 && loc->owner->enable_state == bp_enabled
2370 && loc->enabled)
3cde5c42
PA
2371 {
2372 /* Add the command to the vector. This will be used later
2373 to send the commands to the target. */
2374 bl->target_info.tcommands.push_back (loc->cmd_bytecode.get ());
2375 }
d3ce09f5
SS
2376 }
2377
2378 bl->target_info.persist = 0;
2379 /* Maybe flag this location as persistent. */
2380 if (bl->owner->type == bp_dprintf && disconnected_dprintf)
2381 bl->target_info.persist = 1;
2382}
2383
833b7ab5
YQ
2384/* Return the kind of breakpoint on address *ADDR. Get the kind
2385 of breakpoint according to ADDR except single-step breakpoint.
2386 Get the kind of single-step breakpoint according to the current
2387 registers state. */
cd6c3b4f
YQ
2388
2389static int
2390breakpoint_kind (struct bp_location *bl, CORE_ADDR *addr)
2391{
833b7ab5
YQ
2392 if (bl->owner->type == bp_single_step)
2393 {
2394 struct thread_info *thr = find_thread_global_id (bl->owner->thread);
2395 struct regcache *regcache;
2396
00431a78 2397 regcache = get_thread_regcache (thr);
833b7ab5
YQ
2398
2399 return gdbarch_breakpoint_kind_from_current_state (bl->gdbarch,
2400 regcache, addr);
2401 }
2402 else
2403 return gdbarch_breakpoint_kind_from_pc (bl->gdbarch, addr);
cd6c3b4f
YQ
2404}
2405
35df4500
TJB
2406/* Insert a low-level "breakpoint" of some type. BL is the breakpoint
2407 location. Any error messages are printed to TMP_ERROR_STREAM; and
3fbb6ffa 2408 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
c30eee59
TJB
2409 Returns 0 for success, 1 if the bp_location type is not supported or
2410 -1 for failure.
879bfdc2 2411
4a64f543
MS
2412 NOTE drow/2003-09-09: This routine could be broken down to an
2413 object-style method for each breakpoint or catchpoint type. */
26bb91f3 2414static int
35df4500 2415insert_bp_location (struct bp_location *bl,
26bb91f3 2416 struct ui_file *tmp_error_stream,
3fbb6ffa 2417 int *disabled_breaks,
dd61ec5c
MW
2418 int *hw_breakpoint_error,
2419 int *hw_bp_error_explained_already)
879bfdc2 2420{
cc06b668 2421 gdb_exception bp_excpt;
879bfdc2 2422
b775012e 2423 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
879bfdc2
DJ
2424 return 0;
2425
35c63cd8
JB
2426 /* Note we don't initialize bl->target_info, as that wipes out
2427 the breakpoint location's shadow_contents if the breakpoint
2428 is still inserted at that location. This in turn breaks
2429 target_read_memory which depends on these buffers when
2430 a memory read is requested at the breakpoint location:
2431 Once the target_info has been wiped, we fail to see that
2432 we have a breakpoint inserted at that address and thus
2433 read the breakpoint instead of returning the data saved in
2434 the breakpoint location's shadow contents. */
0d5ed153 2435 bl->target_info.reqstd_address = bl->address;
35df4500 2436 bl->target_info.placed_address_space = bl->pspace->aspace;
f1310107 2437 bl->target_info.length = bl->length;
8181d85f 2438
b775012e
LM
2439 /* When working with target-side conditions, we must pass all the conditions
2440 for the same breakpoint address down to the target since GDB will not
2441 insert those locations. With a list of breakpoint conditions, the target
2442 can decide when to stop and notify GDB. */
2443
2444 if (is_breakpoint (bl->owner))
2445 {
2446 build_target_condition_list (bl);
d3ce09f5
SS
2447 build_target_command_list (bl);
2448 /* Reset the modification marker. */
b775012e
LM
2449 bl->needs_update = 0;
2450 }
2451
35df4500
TJB
2452 if (bl->loc_type == bp_loc_software_breakpoint
2453 || bl->loc_type == bp_loc_hardware_breakpoint)
879bfdc2 2454 {
35df4500 2455 if (bl->owner->type != bp_hardware_breakpoint)
765dc015
VP
2456 {
2457 /* If the explicitly specified breakpoint type
2458 is not hardware breakpoint, check the memory map to see
2459 if the breakpoint address is in read only memory or not.
4a64f543 2460
765dc015
VP
2461 Two important cases are:
2462 - location type is not hardware breakpoint, memory
2463 is readonly. We change the type of the location to
2464 hardware breakpoint.
4a64f543
MS
2465 - location type is hardware breakpoint, memory is
2466 read-write. This means we've previously made the
2467 location hardware one, but then the memory map changed,
2468 so we undo.
765dc015 2469
4a64f543
MS
2470 When breakpoints are removed, remove_breakpoints will use
2471 location types we've just set here, the only possible
2472 problem is that memory map has changed during running
2473 program, but it's not going to work anyway with current
2474 gdb. */
765dc015 2475 struct mem_region *mr
0d5ed153 2476 = lookup_mem_region (bl->target_info.reqstd_address);
765dc015
VP
2477
2478 if (mr)
2479 {
2480 if (automatic_hardware_breakpoints)
2481 {
765dc015
VP
2482 enum bp_loc_type new_type;
2483
2484 if (mr->attrib.mode != MEM_RW)
2485 new_type = bp_loc_hardware_breakpoint;
2486 else
2487 new_type = bp_loc_software_breakpoint;
2488
35df4500 2489 if (new_type != bl->loc_type)
765dc015
VP
2490 {
2491 static int said = 0;
cc59ec59 2492
35df4500 2493 bl->loc_type = new_type;
765dc015
VP
2494 if (!said)
2495 {
3e43a32a
MS
2496 fprintf_filtered (gdb_stdout,
2497 _("Note: automatically using "
2498 "hardware breakpoints for "
2499 "read-only addresses.\n"));
765dc015
VP
2500 said = 1;
2501 }
2502 }
2503 }
35df4500 2504 else if (bl->loc_type == bp_loc_software_breakpoint
0fec99e8
PA
2505 && mr->attrib.mode != MEM_RW)
2506 {
2507 fprintf_unfiltered (tmp_error_stream,
2508 _("Cannot insert breakpoint %d.\n"
2509 "Cannot set software breakpoint "
2510 "at read-only address %s\n"),
2511 bl->owner->number,
2512 paddress (bl->gdbarch, bl->address));
2513 return 1;
2514 }
765dc015
VP
2515 }
2516 }
2517
879bfdc2
DJ
2518 /* First check to see if we have to handle an overlay. */
2519 if (overlay_debugging == ovly_off
35df4500
TJB
2520 || bl->section == NULL
2521 || !(section_is_overlay (bl->section)))
879bfdc2
DJ
2522 {
2523 /* No overlay handling: just set the breakpoint. */
a70b8144 2524 try
dd61ec5c 2525 {
0000e5cc
PA
2526 int val;
2527
dd61ec5c 2528 val = bl->owner->ops->insert_location (bl);
0000e5cc 2529 if (val)
688fca4f 2530 bp_excpt = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
dd61ec5c 2531 }
94aeb44b 2532 catch (gdb_exception &e)
dd61ec5c 2533 {
94aeb44b 2534 bp_excpt = std::move (e);
dd61ec5c 2535 }
879bfdc2
DJ
2536 }
2537 else
2538 {
4a64f543 2539 /* This breakpoint is in an overlay section.
879bfdc2
DJ
2540 Shall we set a breakpoint at the LMA? */
2541 if (!overlay_events_enabled)
2542 {
2543 /* Yes -- overlay event support is not active,
2544 so we must try to set a breakpoint at the LMA.
2545 This will not work for a hardware breakpoint. */
35df4500 2546 if (bl->loc_type == bp_loc_hardware_breakpoint)
8a3fe4f8 2547 warning (_("hardware breakpoint %d not supported in overlay!"),
35df4500 2548 bl->owner->number);
879bfdc2
DJ
2549 else
2550 {
35df4500
TJB
2551 CORE_ADDR addr = overlay_unmapped_address (bl->address,
2552 bl->section);
879bfdc2 2553 /* Set a software (trap) breakpoint at the LMA. */
35df4500 2554 bl->overlay_target_info = bl->target_info;
0d5ed153 2555 bl->overlay_target_info.reqstd_address = addr;
0000e5cc
PA
2556
2557 /* No overlay handling: just set the breakpoint. */
a70b8144 2558 try
0000e5cc
PA
2559 {
2560 int val;
2561
579c6ad9 2562 bl->overlay_target_info.kind
cd6c3b4f
YQ
2563 = breakpoint_kind (bl, &addr);
2564 bl->overlay_target_info.placed_address = addr;
0000e5cc
PA
2565 val = target_insert_breakpoint (bl->gdbarch,
2566 &bl->overlay_target_info);
2567 if (val)
688fca4f
PA
2568 bp_excpt
2569 = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
0000e5cc 2570 }
94aeb44b 2571 catch (gdb_exception &e)
0000e5cc 2572 {
94aeb44b 2573 bp_excpt = std::move (e);
0000e5cc
PA
2574 }
2575
688fca4f 2576 if (bp_excpt.reason != 0)
99361f52 2577 fprintf_unfiltered (tmp_error_stream,
3e43a32a
MS
2578 "Overlay breakpoint %d "
2579 "failed: in ROM?\n",
35df4500 2580 bl->owner->number);
879bfdc2
DJ
2581 }
2582 }
2583 /* Shall we set a breakpoint at the VMA? */
35df4500 2584 if (section_is_mapped (bl->section))
879bfdc2
DJ
2585 {
2586 /* Yes. This overlay section is mapped into memory. */
a70b8144 2587 try
dd61ec5c 2588 {
0000e5cc
PA
2589 int val;
2590
dd61ec5c 2591 val = bl->owner->ops->insert_location (bl);
0000e5cc 2592 if (val)
688fca4f 2593 bp_excpt = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
dd61ec5c 2594 }
94aeb44b 2595 catch (gdb_exception &e)
dd61ec5c 2596 {
94aeb44b 2597 bp_excpt = std::move (e);
dd61ec5c 2598 }
879bfdc2
DJ
2599 }
2600 else
2601 {
2602 /* No. This breakpoint will not be inserted.
2603 No error, but do not mark the bp as 'inserted'. */
2604 return 0;
2605 }
2606 }
2607
688fca4f 2608 if (bp_excpt.reason != 0)
879bfdc2
DJ
2609 {
2610 /* Can't set the breakpoint. */
0000e5cc
PA
2611
2612 /* In some cases, we might not be able to insert a
2613 breakpoint in a shared library that has already been
2614 removed, but we have not yet processed the shlib unload
2615 event. Unfortunately, some targets that implement
076855f9
PA
2616 breakpoint insertion themselves can't tell why the
2617 breakpoint insertion failed (e.g., the remote target
2618 doesn't define error codes), so we must treat generic
2619 errors as memory errors. */
688fca4f
PA
2620 if (bp_excpt.reason == RETURN_ERROR
2621 && (bp_excpt.error == GENERIC_ERROR
2622 || bp_excpt.error == MEMORY_ERROR)
076855f9 2623 && bl->loc_type == bp_loc_software_breakpoint
08351840 2624 && (solib_name_from_address (bl->pspace, bl->address)
d03de421
PA
2625 || shared_objfile_contains_address_p (bl->pspace,
2626 bl->address)))
879bfdc2 2627 {
4a64f543 2628 /* See also: disable_breakpoints_in_shlibs. */
35df4500 2629 bl->shlib_disabled = 1;
76727919 2630 gdb::observers::breakpoint_modified.notify (bl->owner);
3fbb6ffa
TJB
2631 if (!*disabled_breaks)
2632 {
2633 fprintf_unfiltered (tmp_error_stream,
2634 "Cannot insert breakpoint %d.\n",
2635 bl->owner->number);
2636 fprintf_unfiltered (tmp_error_stream,
2637 "Temporarily disabling shared "
2638 "library breakpoints:\n");
2639 }
2640 *disabled_breaks = 1;
879bfdc2 2641 fprintf_unfiltered (tmp_error_stream,
35df4500 2642 "breakpoint #%d\n", bl->owner->number);
0000e5cc 2643 return 0;
879bfdc2
DJ
2644 }
2645 else
879bfdc2 2646 {
35df4500 2647 if (bl->loc_type == bp_loc_hardware_breakpoint)
879bfdc2 2648 {
0000e5cc 2649 *hw_breakpoint_error = 1;
688fca4f 2650 *hw_bp_error_explained_already = bp_excpt.message != NULL;
dd61ec5c
MW
2651 fprintf_unfiltered (tmp_error_stream,
2652 "Cannot insert hardware breakpoint %d%s",
688fca4f
PA
2653 bl->owner->number,
2654 bp_excpt.message ? ":" : ".\n");
2655 if (bp_excpt.message != NULL)
2656 fprintf_unfiltered (tmp_error_stream, "%s.\n",
3d6e9d23 2657 bp_excpt.what ());
879bfdc2
DJ
2658 }
2659 else
2660 {
688fca4f 2661 if (bp_excpt.message == NULL)
0000e5cc 2662 {
1ccbe998 2663 std::string message
0000e5cc
PA
2664 = memory_error_message (TARGET_XFER_E_IO,
2665 bl->gdbarch, bl->address);
0000e5cc
PA
2666
2667 fprintf_unfiltered (tmp_error_stream,
2668 "Cannot insert breakpoint %d.\n"
2669 "%s\n",
1ccbe998 2670 bl->owner->number, message.c_str ());
0000e5cc
PA
2671 }
2672 else
2673 {
2674 fprintf_unfiltered (tmp_error_stream,
2675 "Cannot insert breakpoint %d: %s\n",
2676 bl->owner->number,
3d6e9d23 2677 bp_excpt.what ());
0000e5cc 2678 }
879bfdc2 2679 }
0000e5cc 2680 return 1;
879bfdc2
DJ
2681
2682 }
2683 }
2684 else
35df4500 2685 bl->inserted = 1;
879bfdc2 2686
0000e5cc 2687 return 0;
879bfdc2
DJ
2688 }
2689
35df4500 2690 else if (bl->loc_type == bp_loc_hardware_watchpoint
879bfdc2 2691 /* NOTE drow/2003-09-08: This state only exists for removing
4a64f543 2692 watchpoints. It's not clear that it's necessary... */
35df4500 2693 && bl->owner->disposition != disp_del_at_next_stop)
879bfdc2 2694 {
0000e5cc
PA
2695 int val;
2696
77b06cd7
TJB
2697 gdb_assert (bl->owner->ops != NULL
2698 && bl->owner->ops->insert_location != NULL);
2699
2700 val = bl->owner->ops->insert_location (bl);
85d721b8
PA
2701
2702 /* If trying to set a read-watchpoint, and it turns out it's not
2703 supported, try emulating one with an access watchpoint. */
35df4500 2704 if (val == 1 && bl->watchpoint_type == hw_read)
85d721b8
PA
2705 {
2706 struct bp_location *loc, **loc_temp;
2707
2708 /* But don't try to insert it, if there's already another
2709 hw_access location that would be considered a duplicate
2710 of this one. */
2711 ALL_BP_LOCATIONS (loc, loc_temp)
35df4500 2712 if (loc != bl
85d721b8 2713 && loc->watchpoint_type == hw_access
35df4500 2714 && watchpoint_locations_match (bl, loc))
85d721b8 2715 {
35df4500
TJB
2716 bl->duplicate = 1;
2717 bl->inserted = 1;
2718 bl->target_info = loc->target_info;
2719 bl->watchpoint_type = hw_access;
85d721b8
PA
2720 val = 0;
2721 break;
2722 }
2723
2724 if (val == 1)
2725 {
77b06cd7
TJB
2726 bl->watchpoint_type = hw_access;
2727 val = bl->owner->ops->insert_location (bl);
2728
2729 if (val)
2730 /* Back to the original value. */
2731 bl->watchpoint_type = hw_read;
85d721b8
PA
2732 }
2733 }
2734
35df4500 2735 bl->inserted = (val == 0);
879bfdc2
DJ
2736 }
2737
35df4500 2738 else if (bl->owner->type == bp_catchpoint)
879bfdc2 2739 {
0000e5cc
PA
2740 int val;
2741
77b06cd7
TJB
2742 gdb_assert (bl->owner->ops != NULL
2743 && bl->owner->ops->insert_location != NULL);
2744
2745 val = bl->owner->ops->insert_location (bl);
2746 if (val)
2747 {
2748 bl->owner->enable_state = bp_disabled;
2749
2750 if (val == 1)
2751 warning (_("\
2752Error inserting catchpoint %d: Your system does not support this type\n\
2753of catchpoint."), bl->owner->number);
2754 else
2755 warning (_("Error inserting catchpoint %d."), bl->owner->number);
2756 }
2757
2758 bl->inserted = (val == 0);
1640b821
DJ
2759
2760 /* We've already printed an error message if there was a problem
2761 inserting this catchpoint, and we've disabled the catchpoint,
2762 so just return success. */
2763 return 0;
879bfdc2
DJ
2764 }
2765
2766 return 0;
2767}
2768
6c95b8df
PA
2769/* This function is called when program space PSPACE is about to be
2770 deleted. It takes care of updating breakpoints to not reference
2771 PSPACE anymore. */
2772
2773void
2774breakpoint_program_space_exit (struct program_space *pspace)
2775{
2776 struct breakpoint *b, *b_temp;
876fa593 2777 struct bp_location *loc, **loc_temp;
6c95b8df
PA
2778
2779 /* Remove any breakpoint that was set through this program space. */
2780 ALL_BREAKPOINTS_SAFE (b, b_temp)
2781 {
2782 if (b->pspace == pspace)
2783 delete_breakpoint (b);
2784 }
2785
2786 /* Breakpoints set through other program spaces could have locations
2787 bound to PSPACE as well. Remove those. */
876fa593 2788 ALL_BP_LOCATIONS (loc, loc_temp)
6c95b8df
PA
2789 {
2790 struct bp_location *tmp;
2791
2792 if (loc->pspace == pspace)
2793 {
2bdf28a0 2794 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
6c95b8df
PA
2795 if (loc->owner->loc == loc)
2796 loc->owner->loc = loc->next;
2797 else
2798 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
2799 if (tmp->next == loc)
2800 {
2801 tmp->next = loc->next;
2802 break;
2803 }
2804 }
2805 }
2806
2807 /* Now update the global location list to permanently delete the
2808 removed locations above. */
44702360 2809 update_global_location_list (UGLL_DONT_INSERT);
6c95b8df
PA
2810}
2811
74960c60
VP
2812/* Make sure all breakpoints are inserted in inferior.
2813 Throws exception on any error.
2814 A breakpoint that is already inserted won't be inserted
2815 again, so calling this function twice is safe. */
2816void
2817insert_breakpoints (void)
2818{
2819 struct breakpoint *bpt;
2820
2821 ALL_BREAKPOINTS (bpt)
2822 if (is_hardware_watchpoint (bpt))
3a5c3e22
PA
2823 {
2824 struct watchpoint *w = (struct watchpoint *) bpt;
2825
2826 update_watchpoint (w, 0 /* don't reparse. */);
2827 }
74960c60 2828
04086b45
PA
2829 /* Updating watchpoints creates new locations, so update the global
2830 location list. Explicitly tell ugll to insert locations and
2831 ignore breakpoints_always_inserted_mode. */
2832 update_global_location_list (UGLL_INSERT);
74960c60
VP
2833}
2834
20388dd6
YQ
2835/* Invoke CALLBACK for each of bp_location. */
2836
2837void
2838iterate_over_bp_locations (walk_bp_location_callback callback)
2839{
2840 struct bp_location *loc, **loc_tmp;
2841
2842 ALL_BP_LOCATIONS (loc, loc_tmp)
2843 {
2844 callback (loc, NULL);
2845 }
2846}
2847
b775012e
LM
2848/* This is used when we need to synch breakpoint conditions between GDB and the
2849 target. It is the case with deleting and disabling of breakpoints when using
2850 always-inserted mode. */
2851
2852static void
2853update_inserted_breakpoint_locations (void)
2854{
2855 struct bp_location *bl, **blp_tmp;
2856 int error_flag = 0;
2857 int val = 0;
2858 int disabled_breaks = 0;
2859 int hw_breakpoint_error = 0;
dd61ec5c 2860 int hw_bp_details_reported = 0;
b775012e 2861
d7e74731 2862 string_file tmp_error_stream;
b775012e
LM
2863
2864 /* Explicitly mark the warning -- this will only be printed if
2865 there was an error. */
d7e74731 2866 tmp_error_stream.puts ("Warning:\n");
b775012e 2867
5ed8105e 2868 scoped_restore_current_pspace_and_thread restore_pspace_thread;
b775012e
LM
2869
2870 ALL_BP_LOCATIONS (bl, blp_tmp)
2871 {
2872 /* We only want to update software breakpoints and hardware
2873 breakpoints. */
2874 if (!is_breakpoint (bl->owner))
2875 continue;
2876
2877 /* We only want to update locations that are already inserted
2878 and need updating. This is to avoid unwanted insertion during
2879 deletion of breakpoints. */
4daf1902 2880 if (!bl->inserted || !bl->needs_update)
b775012e
LM
2881 continue;
2882
2883 switch_to_program_space_and_thread (bl->pspace);
2884
2885 /* For targets that support global breakpoints, there's no need
2886 to select an inferior to insert breakpoint to. In fact, even
2887 if we aren't attached to any process yet, we should still
2888 insert breakpoints. */
f5656ead 2889 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
d7e15655 2890 && inferior_ptid == null_ptid)
b775012e
LM
2891 continue;
2892
d7e74731 2893 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
dd61ec5c 2894 &hw_breakpoint_error, &hw_bp_details_reported);
b775012e
LM
2895 if (val)
2896 error_flag = val;
2897 }
2898
2899 if (error_flag)
2900 {
223ffa71 2901 target_terminal::ours_for_output ();
b775012e
LM
2902 error_stream (tmp_error_stream);
2903 }
b775012e
LM
2904}
2905
c30eee59 2906/* Used when starting or continuing the program. */
c906108c 2907
74960c60
VP
2908static void
2909insert_breakpoint_locations (void)
c906108c 2910{
a5606eee 2911 struct breakpoint *bpt;
35df4500 2912 struct bp_location *bl, **blp_tmp;
eacd795a 2913 int error_flag = 0;
c906108c 2914 int val = 0;
3fbb6ffa 2915 int disabled_breaks = 0;
81d0cc19 2916 int hw_breakpoint_error = 0;
dd61ec5c 2917 int hw_bp_error_explained_already = 0;
c906108c 2918
d7e74731
PA
2919 string_file tmp_error_stream;
2920
81d0cc19
GS
2921 /* Explicitly mark the warning -- this will only be printed if
2922 there was an error. */
d7e74731 2923 tmp_error_stream.puts ("Warning:\n");
6c95b8df 2924
5ed8105e 2925 scoped_restore_current_pspace_and_thread restore_pspace_thread;
6c95b8df 2926
35df4500 2927 ALL_BP_LOCATIONS (bl, blp_tmp)
879bfdc2 2928 {
b775012e 2929 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
879bfdc2
DJ
2930 continue;
2931
4a64f543
MS
2932 /* There is no point inserting thread-specific breakpoints if
2933 the thread no longer exists. ALL_BP_LOCATIONS bp_location
2934 has BL->OWNER always non-NULL. */
35df4500 2935 if (bl->owner->thread != -1
5d5658a1 2936 && !valid_global_thread_id (bl->owner->thread))
f365de73
AS
2937 continue;
2938
35df4500 2939 switch_to_program_space_and_thread (bl->pspace);
6c95b8df
PA
2940
2941 /* For targets that support global breakpoints, there's no need
2942 to select an inferior to insert breakpoint to. In fact, even
2943 if we aren't attached to any process yet, we should still
2944 insert breakpoints. */
f5656ead 2945 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
d7e15655 2946 && inferior_ptid == null_ptid)
6c95b8df
PA
2947 continue;
2948
d7e74731 2949 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
dd61ec5c 2950 &hw_breakpoint_error, &hw_bp_error_explained_already);
879bfdc2 2951 if (val)
eacd795a 2952 error_flag = val;
879bfdc2 2953 }
c906108c 2954
4a64f543
MS
2955 /* If we failed to insert all locations of a watchpoint, remove
2956 them, as half-inserted watchpoint is of limited use. */
a5606eee
VP
2957 ALL_BREAKPOINTS (bpt)
2958 {
2959 int some_failed = 0;
2960 struct bp_location *loc;
2961
2962 if (!is_hardware_watchpoint (bpt))
2963 continue;
2964
d6b74ac4 2965 if (!breakpoint_enabled (bpt))
a5606eee 2966 continue;
74960c60
VP
2967
2968 if (bpt->disposition == disp_del_at_next_stop)
2969 continue;
a5606eee
VP
2970
2971 for (loc = bpt->loc; loc; loc = loc->next)
56710373 2972 if (!loc->inserted && should_be_inserted (loc))
a5606eee
VP
2973 {
2974 some_failed = 1;
2975 break;
2976 }
2977 if (some_failed)
2978 {
2979 for (loc = bpt->loc; loc; loc = loc->next)
2980 if (loc->inserted)
834c0d03 2981 remove_breakpoint (loc);
a5606eee
VP
2982
2983 hw_breakpoint_error = 1;
d7e74731
PA
2984 tmp_error_stream.printf ("Could not insert "
2985 "hardware watchpoint %d.\n",
2986 bpt->number);
eacd795a 2987 error_flag = -1;
a5606eee
VP
2988 }
2989 }
2990
eacd795a 2991 if (error_flag)
81d0cc19
GS
2992 {
2993 /* If a hardware breakpoint or watchpoint was inserted, add a
2994 message about possibly exhausted resources. */
dd61ec5c 2995 if (hw_breakpoint_error && !hw_bp_error_explained_already)
81d0cc19 2996 {
d7e74731 2997 tmp_error_stream.printf ("Could not insert hardware breakpoints:\n\
c6510018 2998You may have requested too many hardware breakpoints/watchpoints.\n");
81d0cc19 2999 }
223ffa71 3000 target_terminal::ours_for_output ();
81d0cc19
GS
3001 error_stream (tmp_error_stream);
3002 }
c906108c
SS
3003}
3004
c30eee59
TJB
3005/* Used when the program stops.
3006 Returns zero if successful, or non-zero if there was a problem
3007 removing a breakpoint location. */
3008
c906108c 3009int
fba45db2 3010remove_breakpoints (void)
c906108c 3011{
35df4500 3012 struct bp_location *bl, **blp_tmp;
3a1bae8e 3013 int val = 0;
c906108c 3014
35df4500 3015 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3016 {
1e4d1764 3017 if (bl->inserted && !is_tracepoint (bl->owner))
834c0d03 3018 val |= remove_breakpoint (bl);
c5aa993b 3019 }
3a1bae8e 3020 return val;
c906108c
SS
3021}
3022
49fa26b0
PA
3023/* When a thread exits, remove breakpoints that are related to
3024 that thread. */
3025
3026static void
3027remove_threaded_breakpoints (struct thread_info *tp, int silent)
3028{
3029 struct breakpoint *b, *b_tmp;
3030
3031 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3032 {
5d5658a1 3033 if (b->thread == tp->global_num && user_breakpoint_p (b))
49fa26b0
PA
3034 {
3035 b->disposition = disp_del_at_next_stop;
3036
3037 printf_filtered (_("\
43792cf0
PA
3038Thread-specific breakpoint %d deleted - thread %s no longer in the thread list.\n"),
3039 b->number, print_thread_id (tp));
49fa26b0
PA
3040
3041 /* Hide it from the user. */
3042 b->number = 0;
3043 }
3044 }
3045}
3046
f3869b1a 3047/* See breakpoint.h. */
6c95b8df 3048
f3869b1a 3049void
00431a78 3050remove_breakpoints_inf (inferior *inf)
6c95b8df 3051{
35df4500 3052 struct bp_location *bl, **blp_tmp;
6c95b8df 3053 int val;
6c95b8df 3054
35df4500 3055 ALL_BP_LOCATIONS (bl, blp_tmp)
6c95b8df 3056 {
35df4500 3057 if (bl->pspace != inf->pspace)
6c95b8df
PA
3058 continue;
3059
fc126975 3060 if (bl->inserted && !bl->target_info.persist)
6c95b8df 3061 {
834c0d03 3062 val = remove_breakpoint (bl);
6c95b8df 3063 if (val != 0)
f3869b1a 3064 return;
6c95b8df
PA
3065 }
3066 }
6c95b8df
PA
3067}
3068
e58b0e63
PA
3069static int internal_breakpoint_number = -1;
3070
84f4c1fe
PM
3071/* Set the breakpoint number of B, depending on the value of INTERNAL.
3072 If INTERNAL is non-zero, the breakpoint number will be populated
3073 from internal_breakpoint_number and that variable decremented.
e5dd4106 3074 Otherwise the breakpoint number will be populated from
84f4c1fe
PM
3075 breakpoint_count and that value incremented. Internal breakpoints
3076 do not set the internal var bpnum. */
3077static void
3078set_breakpoint_number (int internal, struct breakpoint *b)
3079{
3080 if (internal)
3081 b->number = internal_breakpoint_number--;
3082 else
3083 {
3084 set_breakpoint_count (breakpoint_count + 1);
3085 b->number = breakpoint_count;
3086 }
3087}
3088
e62c965a 3089static struct breakpoint *
a6d9a66e 3090create_internal_breakpoint (struct gdbarch *gdbarch,
06edf0c0 3091 CORE_ADDR address, enum bptype type,
c0a91b2b 3092 const struct breakpoint_ops *ops)
e62c965a 3093{
51abb421 3094 symtab_and_line sal;
e62c965a
PP
3095 sal.pc = address;
3096 sal.section = find_pc_overlay (sal.pc);
6c95b8df 3097 sal.pspace = current_program_space;
e62c965a 3098
51abb421 3099 breakpoint *b = set_raw_breakpoint (gdbarch, sal, type, ops);
e62c965a
PP
3100 b->number = internal_breakpoint_number--;
3101 b->disposition = disp_donttouch;
3102
3103 return b;
3104}
3105
17450429
PP
3106static const char *const longjmp_names[] =
3107 {
3108 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
3109 };
3110#define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
3111
3112/* Per-objfile data private to breakpoint.c. */
3113struct breakpoint_objfile_data
3114{
3115 /* Minimal symbol for "_ovly_debug_event" (if any). */
43dce439 3116 struct bound_minimal_symbol overlay_msym {};
17450429
PP
3117
3118 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
43dce439 3119 struct bound_minimal_symbol longjmp_msym[NUM_LONGJMP_NAMES] {};
17450429 3120
28106bc2 3121 /* True if we have looked for longjmp probes. */
43dce439 3122 int longjmp_searched = 0;
28106bc2 3123
45461e0d
SM
3124 /* SystemTap probe points for longjmp (if any). These are non-owning
3125 references. */
3126 std::vector<probe *> longjmp_probes;
28106bc2 3127
17450429 3128 /* Minimal symbol for "std::terminate()" (if any). */
43dce439 3129 struct bound_minimal_symbol terminate_msym {};
17450429
PP
3130
3131 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
43dce439 3132 struct bound_minimal_symbol exception_msym {};
28106bc2
SDJ
3133
3134 /* True if we have looked for exception probes. */
43dce439 3135 int exception_searched = 0;
28106bc2 3136
45461e0d
SM
3137 /* SystemTap probe points for unwinding (if any). These are non-owning
3138 references. */
3139 std::vector<probe *> exception_probes;
17450429
PP
3140};
3141
51d3063a
TT
3142static const struct objfile_key<breakpoint_objfile_data>
3143 breakpoint_objfile_key;
17450429
PP
3144
3145/* Minimal symbol not found sentinel. */
3146static struct minimal_symbol msym_not_found;
3147
3148/* Returns TRUE if MSYM point to the "not found" sentinel. */
3149
3150static int
3151msym_not_found_p (const struct minimal_symbol *msym)
3152{
3153 return msym == &msym_not_found;
3154}
3155
3156/* Return per-objfile data needed by breakpoint.c.
3157 Allocate the data if necessary. */
3158
3159static struct breakpoint_objfile_data *
3160get_breakpoint_objfile_data (struct objfile *objfile)
3161{
3162 struct breakpoint_objfile_data *bp_objfile_data;
3163
51d3063a 3164 bp_objfile_data = breakpoint_objfile_key.get (objfile);
17450429 3165 if (bp_objfile_data == NULL)
51d3063a 3166 bp_objfile_data = breakpoint_objfile_key.emplace (objfile);
17450429
PP
3167 return bp_objfile_data;
3168}
3169
e62c965a 3170static void
af02033e 3171create_overlay_event_breakpoint (void)
e62c965a 3172{
af02033e 3173 const char *const func_name = "_ovly_debug_event";
e62c965a 3174
2030c079 3175 for (objfile *objfile : current_program_space->objfiles ())
69de3c6a
PP
3176 {
3177 struct breakpoint *b;
17450429
PP
3178 struct breakpoint_objfile_data *bp_objfile_data;
3179 CORE_ADDR addr;
67994074 3180 struct explicit_location explicit_loc;
69de3c6a 3181
17450429
PP
3182 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3183
3b7344d5 3184 if (msym_not_found_p (bp_objfile_data->overlay_msym.minsym))
17450429
PP
3185 continue;
3186
3b7344d5 3187 if (bp_objfile_data->overlay_msym.minsym == NULL)
17450429 3188 {
3b7344d5 3189 struct bound_minimal_symbol m;
17450429
PP
3190
3191 m = lookup_minimal_symbol_text (func_name, objfile);
3b7344d5 3192 if (m.minsym == NULL)
17450429
PP
3193 {
3194 /* Avoid future lookups in this objfile. */
3b7344d5 3195 bp_objfile_data->overlay_msym.minsym = &msym_not_found;
17450429
PP
3196 continue;
3197 }
3198 bp_objfile_data->overlay_msym = m;
3199 }
e62c965a 3200
77e371c0 3201 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->overlay_msym);
17450429 3202 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
06edf0c0
PA
3203 bp_overlay_event,
3204 &internal_breakpoint_ops);
67994074
KS
3205 initialize_explicit_location (&explicit_loc);
3206 explicit_loc.function_name = ASTRDUP (func_name);
d28cd78a 3207 b->location = new_explicit_location (&explicit_loc);
e62c965a 3208
69de3c6a
PP
3209 if (overlay_debugging == ovly_auto)
3210 {
3211 b->enable_state = bp_enabled;
3212 overlay_events_enabled = 1;
3213 }
3214 else
3215 {
3216 b->enable_state = bp_disabled;
3217 overlay_events_enabled = 0;
3218 }
e62c965a 3219 }
e62c965a
PP
3220}
3221
0fd8e87f 3222static void
af02033e 3223create_longjmp_master_breakpoint (void)
0fd8e87f 3224{
6c95b8df 3225 struct program_space *pspace;
6c95b8df 3226
5ed8105e 3227 scoped_restore_current_program_space restore_pspace;
0fd8e87f 3228
6c95b8df 3229 ALL_PSPACES (pspace)
af02033e 3230 {
af02033e
PP
3231 set_current_program_space (pspace);
3232
2030c079 3233 for (objfile *objfile : current_program_space->objfiles ())
aed57c53
TT
3234 {
3235 int i;
3236 struct gdbarch *gdbarch;
3237 struct breakpoint_objfile_data *bp_objfile_data;
0fd8e87f 3238
aed57c53 3239 gdbarch = get_objfile_arch (objfile);
0fd8e87f 3240
aed57c53 3241 bp_objfile_data = get_breakpoint_objfile_data (objfile);
17450429 3242
aed57c53
TT
3243 if (!bp_objfile_data->longjmp_searched)
3244 {
3245 std::vector<probe *> ret
3246 = find_probes_in_objfile (objfile, "libc", "longjmp");
25f9533e 3247
aed57c53
TT
3248 if (!ret.empty ())
3249 {
3250 /* We are only interested in checking one element. */
3251 probe *p = ret[0];
3252
3253 if (!p->can_evaluate_arguments ())
3254 {
3255 /* We cannot use the probe interface here, because it does
3256 not know how to evaluate arguments. */
3257 ret.clear ();
3258 }
3259 }
3260 bp_objfile_data->longjmp_probes = ret;
3261 bp_objfile_data->longjmp_searched = 1;
3262 }
25f9533e 3263
aed57c53
TT
3264 if (!bp_objfile_data->longjmp_probes.empty ())
3265 {
3266 for (probe *p : bp_objfile_data->longjmp_probes)
3267 {
3268 struct breakpoint *b;
3269
3270 b = create_internal_breakpoint (gdbarch,
3271 p->get_relocated_address (objfile),
3272 bp_longjmp_master,
3273 &internal_breakpoint_ops);
3274 b->location = new_probe_location ("-probe-stap libc:longjmp");
3275 b->enable_state = bp_disabled;
3276 }
28106bc2 3277
aed57c53
TT
3278 continue;
3279 }
28106bc2 3280
aed57c53 3281 if (!gdbarch_get_longjmp_target_p (gdbarch))
28106bc2 3282 continue;
28106bc2 3283
aed57c53
TT
3284 for (i = 0; i < NUM_LONGJMP_NAMES; i++)
3285 {
3286 struct breakpoint *b;
3287 const char *func_name;
3288 CORE_ADDR addr;
3289 struct explicit_location explicit_loc;
0fd8e87f 3290
aed57c53
TT
3291 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i].minsym))
3292 continue;
17450429 3293
aed57c53
TT
3294 func_name = longjmp_names[i];
3295 if (bp_objfile_data->longjmp_msym[i].minsym == NULL)
3296 {
3297 struct bound_minimal_symbol m;
3298
3299 m = lookup_minimal_symbol_text (func_name, objfile);
3300 if (m.minsym == NULL)
3301 {
3302 /* Prevent future lookups in this objfile. */
3303 bp_objfile_data->longjmp_msym[i].minsym = &msym_not_found;
3304 continue;
3305 }
3306 bp_objfile_data->longjmp_msym[i] = m;
3307 }
17450429 3308
aed57c53
TT
3309 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->longjmp_msym[i]);
3310 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master,
3311 &internal_breakpoint_ops);
3312 initialize_explicit_location (&explicit_loc);
3313 explicit_loc.function_name = ASTRDUP (func_name);
3314 b->location = new_explicit_location (&explicit_loc);
3315 b->enable_state = bp_disabled;
3316 }
3317 }
af02033e 3318 }
0fd8e87f
UW
3319}
3320
af02033e 3321/* Create a master std::terminate breakpoint. */
aa7d318d 3322static void
af02033e 3323create_std_terminate_master_breakpoint (void)
aa7d318d
TT
3324{
3325 struct program_space *pspace;
af02033e 3326 const char *const func_name = "std::terminate()";
aa7d318d 3327
5ed8105e 3328 scoped_restore_current_program_space restore_pspace;
aa7d318d
TT
3329
3330 ALL_PSPACES (pspace)
17450429 3331 {
17450429
PP
3332 CORE_ADDR addr;
3333
3334 set_current_program_space (pspace);
3335
2030c079 3336 for (objfile *objfile : current_program_space->objfiles ())
aed57c53
TT
3337 {
3338 struct breakpoint *b;
3339 struct breakpoint_objfile_data *bp_objfile_data;
3340 struct explicit_location explicit_loc;
aa7d318d 3341
aed57c53 3342 bp_objfile_data = get_breakpoint_objfile_data (objfile);
aa7d318d 3343
aed57c53
TT
3344 if (msym_not_found_p (bp_objfile_data->terminate_msym.minsym))
3345 continue;
17450429 3346
aed57c53
TT
3347 if (bp_objfile_data->terminate_msym.minsym == NULL)
3348 {
3349 struct bound_minimal_symbol m;
17450429 3350
aed57c53
TT
3351 m = lookup_minimal_symbol (func_name, NULL, objfile);
3352 if (m.minsym == NULL || (MSYMBOL_TYPE (m.minsym) != mst_text
3353 && MSYMBOL_TYPE (m.minsym) != mst_file_text))
3354 {
3355 /* Prevent future lookups in this objfile. */
3356 bp_objfile_data->terminate_msym.minsym = &msym_not_found;
3357 continue;
3358 }
3359 bp_objfile_data->terminate_msym = m;
3360 }
aa7d318d 3361
aed57c53
TT
3362 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->terminate_msym);
3363 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
3364 bp_std_terminate_master,
3365 &internal_breakpoint_ops);
3366 initialize_explicit_location (&explicit_loc);
3367 explicit_loc.function_name = ASTRDUP (func_name);
3368 b->location = new_explicit_location (&explicit_loc);
3369 b->enable_state = bp_disabled;
3370 }
17450429 3371 }
aa7d318d
TT
3372}
3373
186c406b
TT
3374/* Install a master breakpoint on the unwinder's debug hook. */
3375
70221824 3376static void
186c406b
TT
3377create_exception_master_breakpoint (void)
3378{
17450429 3379 const char *const func_name = "_Unwind_DebugHook";
186c406b 3380
2030c079 3381 for (objfile *objfile : current_program_space->objfiles ())
186c406b 3382 {
17450429
PP
3383 struct breakpoint *b;
3384 struct gdbarch *gdbarch;
3385 struct breakpoint_objfile_data *bp_objfile_data;
3386 CORE_ADDR addr;
67994074 3387 struct explicit_location explicit_loc;
17450429
PP
3388
3389 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3390
28106bc2
SDJ
3391 /* We prefer the SystemTap probe point if it exists. */
3392 if (!bp_objfile_data->exception_searched)
3393 {
45461e0d
SM
3394 std::vector<probe *> ret
3395 = find_probes_in_objfile (objfile, "libgcc", "unwind");
25f9533e 3396
45461e0d 3397 if (!ret.empty ())
25f9533e
SDJ
3398 {
3399 /* We are only interested in checking one element. */
45461e0d 3400 probe *p = ret[0];
25f9533e 3401
935676c9 3402 if (!p->can_evaluate_arguments ())
25f9533e
SDJ
3403 {
3404 /* We cannot use the probe interface here, because it does
3405 not know how to evaluate arguments. */
45461e0d 3406 ret.clear ();
25f9533e
SDJ
3407 }
3408 }
3409 bp_objfile_data->exception_probes = ret;
28106bc2
SDJ
3410 bp_objfile_data->exception_searched = 1;
3411 }
3412
45461e0d 3413 if (!bp_objfile_data->exception_probes.empty ())
28106bc2 3414 {
b926417a 3415 gdbarch = get_objfile_arch (objfile);
45461e0d
SM
3416
3417 for (probe *p : bp_objfile_data->exception_probes)
28106bc2 3418 {
729662a5 3419 b = create_internal_breakpoint (gdbarch,
935676c9 3420 p->get_relocated_address (objfile),
28106bc2
SDJ
3421 bp_exception_master,
3422 &internal_breakpoint_ops);
d28cd78a 3423 b->location = new_probe_location ("-probe-stap libgcc:unwind");
28106bc2
SDJ
3424 b->enable_state = bp_disabled;
3425 }
3426
3427 continue;
3428 }
3429
3430 /* Otherwise, try the hook function. */
3431
3b7344d5 3432 if (msym_not_found_p (bp_objfile_data->exception_msym.minsym))
17450429
PP
3433 continue;
3434
3435 gdbarch = get_objfile_arch (objfile);
186c406b 3436
3b7344d5 3437 if (bp_objfile_data->exception_msym.minsym == NULL)
186c406b 3438 {
3b7344d5 3439 struct bound_minimal_symbol debug_hook;
186c406b 3440
17450429 3441 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
3b7344d5 3442 if (debug_hook.minsym == NULL)
17450429 3443 {
3b7344d5 3444 bp_objfile_data->exception_msym.minsym = &msym_not_found;
17450429
PP
3445 continue;
3446 }
3447
3448 bp_objfile_data->exception_msym = debug_hook;
186c406b 3449 }
17450429 3450
77e371c0 3451 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->exception_msym);
8b88a78e
PA
3452 addr = gdbarch_convert_from_func_ptr_addr (gdbarch, addr,
3453 current_top_target ());
06edf0c0
PA
3454 b = create_internal_breakpoint (gdbarch, addr, bp_exception_master,
3455 &internal_breakpoint_ops);
67994074
KS
3456 initialize_explicit_location (&explicit_loc);
3457 explicit_loc.function_name = ASTRDUP (func_name);
d28cd78a 3458 b->location = new_explicit_location (&explicit_loc);
17450429 3459 b->enable_state = bp_disabled;
186c406b 3460 }
186c406b
TT
3461}
3462
9ef9e6a6
KS
3463/* Does B have a location spec? */
3464
3465static int
3466breakpoint_event_location_empty_p (const struct breakpoint *b)
3467{
d28cd78a 3468 return b->location != NULL && event_location_empty_p (b->location.get ());
9ef9e6a6
KS
3469}
3470
c906108c 3471void
fba45db2 3472update_breakpoints_after_exec (void)
c906108c 3473{
35df4500 3474 struct breakpoint *b, *b_tmp;
876fa593 3475 struct bp_location *bploc, **bplocp_tmp;
c906108c 3476
25b22b0a
PA
3477 /* We're about to delete breakpoints from GDB's lists. If the
3478 INSERTED flag is true, GDB will try to lift the breakpoints by
3479 writing the breakpoints' "shadow contents" back into memory. The
3480 "shadow contents" are NOT valid after an exec, so GDB should not
3481 do that. Instead, the target is responsible from marking
3482 breakpoints out as soon as it detects an exec. We don't do that
3483 here instead, because there may be other attempts to delete
3484 breakpoints after detecting an exec and before reaching here. */
876fa593 3485 ALL_BP_LOCATIONS (bploc, bplocp_tmp)
6c95b8df
PA
3486 if (bploc->pspace == current_program_space)
3487 gdb_assert (!bploc->inserted);
c906108c 3488
35df4500 3489 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 3490 {
6c95b8df
PA
3491 if (b->pspace != current_program_space)
3492 continue;
3493
4a64f543 3494 /* Solib breakpoints must be explicitly reset after an exec(). */
c5aa993b
JM
3495 if (b->type == bp_shlib_event)
3496 {
3497 delete_breakpoint (b);
3498 continue;
3499 }
c906108c 3500
4a64f543 3501 /* JIT breakpoints must be explicitly reset after an exec(). */
4efc6507
DE
3502 if (b->type == bp_jit_event)
3503 {
3504 delete_breakpoint (b);
3505 continue;
3506 }
3507
1900040c 3508 /* Thread event breakpoints must be set anew after an exec(),
0fd8e87f
UW
3509 as must overlay event and longjmp master breakpoints. */
3510 if (b->type == bp_thread_event || b->type == bp_overlay_event
186c406b
TT
3511 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
3512 || b->type == bp_exception_master)
c4093a6a
JM
3513 {
3514 delete_breakpoint (b);
3515 continue;
3516 }
3517
4a64f543 3518 /* Step-resume breakpoints are meaningless after an exec(). */
2c03e5be 3519 if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
c5aa993b
JM
3520 {
3521 delete_breakpoint (b);
3522 continue;
3523 }
3524
7c16b83e
PA
3525 /* Just like single-step breakpoints. */
3526 if (b->type == bp_single_step)
3527 {
3528 delete_breakpoint (b);
3529 continue;
3530 }
3531
611c83ae
PA
3532 /* Longjmp and longjmp-resume breakpoints are also meaningless
3533 after an exec. */
186c406b 3534 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
e2e4d78b 3535 || b->type == bp_longjmp_call_dummy
186c406b 3536 || b->type == bp_exception || b->type == bp_exception_resume)
611c83ae
PA
3537 {
3538 delete_breakpoint (b);
3539 continue;
3540 }
3541
ce78b96d
JB
3542 if (b->type == bp_catchpoint)
3543 {
3544 /* For now, none of the bp_catchpoint breakpoints need to
3545 do anything at this point. In the future, if some of
3546 the catchpoints need to something, we will need to add
3547 a new method, and call this method from here. */
3548 continue;
3549 }
3550
c5aa993b
JM
3551 /* bp_finish is a special case. The only way we ought to be able
3552 to see one of these when an exec() has happened, is if the user
3553 caught a vfork, and then said "finish". Ordinarily a finish just
3554 carries them to the call-site of the current callee, by setting
3555 a temporary bp there and resuming. But in this case, the finish
3556 will carry them entirely through the vfork & exec.
3557
3558 We don't want to allow a bp_finish to remain inserted now. But
3559 we can't safely delete it, 'cause finish_command has a handle to
3560 the bp on a bpstat, and will later want to delete it. There's a
3561 chance (and I've seen it happen) that if we delete the bp_finish
3562 here, that its storage will get reused by the time finish_command
3563 gets 'round to deleting the "use to be a bp_finish" breakpoint.
3564 We really must allow finish_command to delete a bp_finish.
3565
e5dd4106 3566 In the absence of a general solution for the "how do we know
53a5351d
JM
3567 it's safe to delete something others may have handles to?"
3568 problem, what we'll do here is just uninsert the bp_finish, and
3569 let finish_command delete it.
3570
3571 (We know the bp_finish is "doomed" in the sense that it's
3572 momentary, and will be deleted as soon as finish_command sees
3573 the inferior stopped. So it doesn't matter that the bp's
3574 address is probably bogus in the new a.out, unlike e.g., the
3575 solib breakpoints.) */
c5aa993b 3576
c5aa993b
JM
3577 if (b->type == bp_finish)
3578 {
3579 continue;
3580 }
3581
3582 /* Without a symbolic address, we have little hope of the
3583 pre-exec() address meaning the same thing in the post-exec()
4a64f543 3584 a.out. */
9ef9e6a6 3585 if (breakpoint_event_location_empty_p (b))
c5aa993b
JM
3586 {
3587 delete_breakpoint (b);
3588 continue;
3589 }
c5aa993b 3590 }
c906108c
SS
3591}
3592
3593int
d80ee84f 3594detach_breakpoints (ptid_t ptid)
c906108c 3595{
35df4500 3596 struct bp_location *bl, **blp_tmp;
3a1bae8e 3597 int val = 0;
2989a365 3598 scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid);
6c95b8df 3599 struct inferior *inf = current_inferior ();
c5aa993b 3600
e99b03dc 3601 if (ptid.pid () == inferior_ptid.pid ())
8a3fe4f8 3602 error (_("Cannot detach breakpoints of inferior_ptid"));
c5aa993b 3603
6c95b8df 3604 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
d80ee84f 3605 inferior_ptid = ptid;
35df4500 3606 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3607 {
35df4500 3608 if (bl->pspace != inf->pspace)
6c95b8df
PA
3609 continue;
3610
bd9673a4
PW
3611 /* This function must physically remove breakpoints locations
3612 from the specified ptid, without modifying the breakpoint
3613 package's state. Locations of type bp_loc_other are only
3614 maintained at GDB side. So, there is no need to remove
3615 these bp_loc_other locations. Moreover, removing these
3616 would modify the breakpoint package's state. */
3617 if (bl->loc_type == bp_loc_other)
3618 continue;
3619
35df4500 3620 if (bl->inserted)
b2b6a7da 3621 val |= remove_breakpoint_1 (bl, DETACH_BREAKPOINT);
c5aa993b 3622 }
d03285ec 3623
3a1bae8e 3624 return val;
c906108c
SS
3625}
3626
35df4500 3627/* Remove the breakpoint location BL from the current address space.
6c95b8df
PA
3628 Note that this is used to detach breakpoints from a child fork.
3629 When we get here, the child isn't in the inferior list, and neither
3630 do we have objects to represent its address space --- we should
35df4500 3631 *not* look at bl->pspace->aspace here. */
6c95b8df 3632
c906108c 3633static int
b2b6a7da 3634remove_breakpoint_1 (struct bp_location *bl, enum remove_bp_reason reason)
c906108c
SS
3635{
3636 int val;
c5aa993b 3637
35df4500
TJB
3638 /* BL is never in moribund_locations by our callers. */
3639 gdb_assert (bl->owner != NULL);
2bdf28a0 3640
74960c60
VP
3641 /* The type of none suggests that owner is actually deleted.
3642 This should not ever happen. */
35df4500 3643 gdb_assert (bl->owner->type != bp_none);
0bde7532 3644
35df4500
TJB
3645 if (bl->loc_type == bp_loc_software_breakpoint
3646 || bl->loc_type == bp_loc_hardware_breakpoint)
c906108c 3647 {
c02f5703
MS
3648 /* "Normal" instruction breakpoint: either the standard
3649 trap-instruction bp (bp_breakpoint), or a
3650 bp_hardware_breakpoint. */
3651
3652 /* First check to see if we have to handle an overlay. */
3653 if (overlay_debugging == ovly_off
35df4500
TJB
3654 || bl->section == NULL
3655 || !(section_is_overlay (bl->section)))
c02f5703
MS
3656 {
3657 /* No overlay handling: just remove the breakpoint. */
08351840
PA
3658
3659 /* If we're trying to uninsert a memory breakpoint that we
3660 know is set in a dynamic object that is marked
3661 shlib_disabled, then either the dynamic object was
3662 removed with "remove-symbol-file" or with
3663 "nosharedlibrary". In the former case, we don't know
3664 whether another dynamic object might have loaded over the
3665 breakpoint's address -- the user might well let us know
3666 about it next with add-symbol-file (the whole point of
d03de421 3667 add-symbol-file is letting the user manually maintain a
08351840
PA
3668 list of dynamically loaded objects). If we have the
3669 breakpoint's shadow memory, that is, this is a software
3670 breakpoint managed by GDB, check whether the breakpoint
3671 is still inserted in memory, to avoid overwriting wrong
3672 code with stale saved shadow contents. Note that HW
3673 breakpoints don't have shadow memory, as they're
3674 implemented using a mechanism that is not dependent on
3675 being able to modify the target's memory, and as such
3676 they should always be removed. */
3677 if (bl->shlib_disabled
3678 && bl->target_info.shadow_len != 0
3679 && !memory_validate_breakpoint (bl->gdbarch, &bl->target_info))
3680 val = 0;
3681 else
73971819 3682 val = bl->owner->ops->remove_location (bl, reason);
c02f5703 3683 }
c906108c
SS
3684 else
3685 {
4a64f543 3686 /* This breakpoint is in an overlay section.
c02f5703
MS
3687 Did we set a breakpoint at the LMA? */
3688 if (!overlay_events_enabled)
3689 {
3690 /* Yes -- overlay event support is not active, so we
3691 should have set a breakpoint at the LMA. Remove it.
3692 */
c02f5703
MS
3693 /* Ignore any failures: if the LMA is in ROM, we will
3694 have already warned when we failed to insert it. */
35df4500
TJB
3695 if (bl->loc_type == bp_loc_hardware_breakpoint)
3696 target_remove_hw_breakpoint (bl->gdbarch,
3697 &bl->overlay_target_info);
c02f5703 3698 else
35df4500 3699 target_remove_breakpoint (bl->gdbarch,
73971819
PA
3700 &bl->overlay_target_info,
3701 reason);
c02f5703
MS
3702 }
3703 /* Did we set a breakpoint at the VMA?
3704 If so, we will have marked the breakpoint 'inserted'. */
35df4500 3705 if (bl->inserted)
c906108c 3706 {
c02f5703
MS
3707 /* Yes -- remove it. Previously we did not bother to
3708 remove the breakpoint if the section had been
3709 unmapped, but let's not rely on that being safe. We
3710 don't know what the overlay manager might do. */
aa67235e
UW
3711
3712 /* However, we should remove *software* breakpoints only
3713 if the section is still mapped, or else we overwrite
3714 wrong code with the saved shadow contents. */
348d480f
PA
3715 if (bl->loc_type == bp_loc_hardware_breakpoint
3716 || section_is_mapped (bl->section))
73971819 3717 val = bl->owner->ops->remove_location (bl, reason);
aa67235e
UW
3718 else
3719 val = 0;
c906108c 3720 }
c02f5703
MS
3721 else
3722 {
3723 /* No -- not inserted, so no need to remove. No error. */
3724 val = 0;
3725 }
c906108c 3726 }
879d1e6b 3727
08351840
PA
3728 /* In some cases, we might not be able to remove a breakpoint in
3729 a shared library that has already been removed, but we have
3730 not yet processed the shlib unload event. Similarly for an
3731 unloaded add-symbol-file object - the user might not yet have
3732 had the chance to remove-symbol-file it. shlib_disabled will
3733 be set if the library/object has already been removed, but
3734 the breakpoint hasn't been uninserted yet, e.g., after
3735 "nosharedlibrary" or "remove-symbol-file" with breakpoints
3736 always-inserted mode. */
076855f9 3737 if (val
08351840
PA
3738 && (bl->loc_type == bp_loc_software_breakpoint
3739 && (bl->shlib_disabled
3740 || solib_name_from_address (bl->pspace, bl->address)
d03de421
PA
3741 || shared_objfile_contains_address_p (bl->pspace,
3742 bl->address))))
879d1e6b
UW
3743 val = 0;
3744
c906108c
SS
3745 if (val)
3746 return val;
b2b6a7da 3747 bl->inserted = (reason == DETACH_BREAKPOINT);
c906108c 3748 }
35df4500 3749 else if (bl->loc_type == bp_loc_hardware_watchpoint)
c906108c 3750 {
77b06cd7
TJB
3751 gdb_assert (bl->owner->ops != NULL
3752 && bl->owner->ops->remove_location != NULL);
3753
b2b6a7da 3754 bl->inserted = (reason == DETACH_BREAKPOINT);
73971819 3755 bl->owner->ops->remove_location (bl, reason);
2e70b7b9 3756
c906108c 3757 /* Failure to remove any of the hardware watchpoints comes here. */
b2b6a7da 3758 if (reason == REMOVE_BREAKPOINT && bl->inserted)
8a3fe4f8 3759 warning (_("Could not remove hardware watchpoint %d."),
35df4500 3760 bl->owner->number);
c906108c 3761 }
35df4500
TJB
3762 else if (bl->owner->type == bp_catchpoint
3763 && breakpoint_enabled (bl->owner)
3764 && !bl->duplicate)
ce78b96d 3765 {
77b06cd7
TJB
3766 gdb_assert (bl->owner->ops != NULL
3767 && bl->owner->ops->remove_location != NULL);
ce78b96d 3768
73971819 3769 val = bl->owner->ops->remove_location (bl, reason);
ce78b96d
JB
3770 if (val)
3771 return val;
77b06cd7 3772
b2b6a7da 3773 bl->inserted = (reason == DETACH_BREAKPOINT);
ce78b96d 3774 }
c906108c
SS
3775
3776 return 0;
3777}
3778
6c95b8df 3779static int
834c0d03 3780remove_breakpoint (struct bp_location *bl)
6c95b8df 3781{
35df4500
TJB
3782 /* BL is never in moribund_locations by our callers. */
3783 gdb_assert (bl->owner != NULL);
2bdf28a0 3784
6c95b8df
PA
3785 /* The type of none suggests that owner is actually deleted.
3786 This should not ever happen. */
35df4500 3787 gdb_assert (bl->owner->type != bp_none);
6c95b8df 3788
5ed8105e 3789 scoped_restore_current_pspace_and_thread restore_pspace_thread;
6c95b8df 3790
35df4500 3791 switch_to_program_space_and_thread (bl->pspace);
6c95b8df 3792
5ed8105e 3793 return remove_breakpoint_1 (bl, REMOVE_BREAKPOINT);
6c95b8df
PA
3794}
3795
c906108c
SS
3796/* Clear the "inserted" flag in all breakpoints. */
3797
25b22b0a 3798void
fba45db2 3799mark_breakpoints_out (void)
c906108c 3800{
35df4500 3801 struct bp_location *bl, **blp_tmp;
c906108c 3802
35df4500 3803 ALL_BP_LOCATIONS (bl, blp_tmp)
66c4b3e8 3804 if (bl->pspace == current_program_space)
35df4500 3805 bl->inserted = 0;
c906108c
SS
3806}
3807
53a5351d
JM
3808/* Clear the "inserted" flag in all breakpoints and delete any
3809 breakpoints which should go away between runs of the program.
c906108c
SS
3810
3811 Plus other such housekeeping that has to be done for breakpoints
3812 between runs.
3813
53a5351d
JM
3814 Note: this function gets called at the end of a run (by
3815 generic_mourn_inferior) and when a run begins (by
4a64f543 3816 init_wait_for_inferior). */
c906108c
SS
3817
3818
3819
3820void
fba45db2 3821breakpoint_init_inferior (enum inf_context context)
c906108c 3822{
35df4500 3823 struct breakpoint *b, *b_tmp;
6c95b8df 3824 struct program_space *pspace = current_program_space;
c906108c 3825
50c71eaf
PA
3826 /* If breakpoint locations are shared across processes, then there's
3827 nothing to do. */
f5656ead 3828 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
50c71eaf
PA
3829 return;
3830
1a853c52 3831 mark_breakpoints_out ();
075f6582 3832
35df4500 3833 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 3834 {
6c95b8df
PA
3835 if (b->loc && b->loc->pspace != pspace)
3836 continue;
3837
c5aa993b
JM
3838 switch (b->type)
3839 {
3840 case bp_call_dummy:
e2e4d78b 3841 case bp_longjmp_call_dummy:
c906108c 3842
c5aa993b 3843 /* If the call dummy breakpoint is at the entry point it will
ab92d69b
PA
3844 cause problems when the inferior is rerun, so we better get
3845 rid of it. */
3846
3847 case bp_watchpoint_scope:
3848
3849 /* Also get rid of scope breakpoints. */
3850
3851 case bp_shlib_event:
3852
3853 /* Also remove solib event breakpoints. Their addresses may
3854 have changed since the last time we ran the program.
3855 Actually we may now be debugging against different target;
3856 and so the solib backend that installed this breakpoint may
3857 not be used in by the target. E.g.,
3858
3859 (gdb) file prog-linux
3860 (gdb) run # native linux target
3861 ...
3862 (gdb) kill
3863 (gdb) file prog-win.exe
3864 (gdb) tar rem :9999 # remote Windows gdbserver.
3865 */
c906108c 3866
f59f708a
PA
3867 case bp_step_resume:
3868
3869 /* Also remove step-resume breakpoints. */
3870
7c16b83e
PA
3871 case bp_single_step:
3872
3873 /* Also remove single-step breakpoints. */
3874
c5aa993b
JM
3875 delete_breakpoint (b);
3876 break;
c906108c 3877
c5aa993b
JM
3878 case bp_watchpoint:
3879 case bp_hardware_watchpoint:
3880 case bp_read_watchpoint:
3881 case bp_access_watchpoint:
3a5c3e22
PA
3882 {
3883 struct watchpoint *w = (struct watchpoint *) b;
c906108c 3884
3a5c3e22
PA
3885 /* Likewise for watchpoints on local expressions. */
3886 if (w->exp_valid_block != NULL)
3887 delete_breakpoint (b);
63000888 3888 else
3a5c3e22 3889 {
63000888
PA
3890 /* Get rid of existing locations, which are no longer
3891 valid. New ones will be created in
3892 update_watchpoint, when the inferior is restarted.
3893 The next update_global_location_list call will
3894 garbage collect them. */
3895 b->loc = NULL;
3896
3897 if (context == inf_starting)
3898 {
3899 /* Reset val field to force reread of starting value in
3900 insert_breakpoints. */
850645cf 3901 w->val.reset (nullptr);
4c1d86d9 3902 w->val_valid = false;
63000888
PA
3903 }
3904 }
3a5c3e22 3905 }
c5aa993b
JM
3906 break;
3907 default:
c5aa993b
JM
3908 break;
3909 }
3910 }
1c5cfe86
PA
3911
3912 /* Get rid of the moribund locations. */
1123588c 3913 for (bp_location *bl : moribund_locations)
35df4500 3914 decref_bp_location (&bl);
1123588c 3915 moribund_locations.clear ();
c906108c
SS
3916}
3917
6c95b8df
PA
3918/* These functions concern about actual breakpoints inserted in the
3919 target --- to e.g. check if we need to do decr_pc adjustment or if
3920 we need to hop over the bkpt --- so we check for address space
3921 match, not program space. */
3922
c2c6d25f
JM
3923/* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
3924 exists at PC. It returns ordinary_breakpoint_here if it's an
3925 ordinary breakpoint, or permanent_breakpoint_here if it's a
3926 permanent breakpoint.
3927 - When continuing from a location with an ordinary breakpoint, we
3928 actually single step once before calling insert_breakpoints.
e5dd4106 3929 - When continuing from a location with a permanent breakpoint, we
c2c6d25f
JM
3930 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
3931 the target, to advance the PC past the breakpoint. */
c906108c 3932
c2c6d25f 3933enum breakpoint_here
accd0bcd 3934breakpoint_here_p (const address_space *aspace, CORE_ADDR pc)
c906108c 3935{
35df4500 3936 struct bp_location *bl, **blp_tmp;
c2c6d25f 3937 int any_breakpoint_here = 0;
c906108c 3938
35df4500 3939 ALL_BP_LOCATIONS (bl, blp_tmp)
075f6582 3940 {
35df4500
TJB
3941 if (bl->loc_type != bp_loc_software_breakpoint
3942 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
3943 continue;
3944
f1310107 3945 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
35df4500 3946 if ((breakpoint_enabled (bl->owner)
1a853c52 3947 || bl->permanent)
f1310107 3948 && breakpoint_location_address_match (bl, aspace, pc))
075f6582
DJ
3949 {
3950 if (overlay_debugging
35df4500
TJB
3951 && section_is_overlay (bl->section)
3952 && !section_is_mapped (bl->section))
075f6582 3953 continue; /* unmapped overlay -- can't be a match */
1a853c52 3954 else if (bl->permanent)
075f6582
DJ
3955 return permanent_breakpoint_here;
3956 else
3957 any_breakpoint_here = 1;
3958 }
3959 }
c906108c 3960
f486487f 3961 return any_breakpoint_here ? ordinary_breakpoint_here : no_breakpoint_here;
c906108c
SS
3962}
3963
d35ae833
PA
3964/* See breakpoint.h. */
3965
3966int
accd0bcd 3967breakpoint_in_range_p (const address_space *aspace,
d35ae833
PA
3968 CORE_ADDR addr, ULONGEST len)
3969{
3970 struct bp_location *bl, **blp_tmp;
3971
3972 ALL_BP_LOCATIONS (bl, blp_tmp)
3973 {
3974 if (bl->loc_type != bp_loc_software_breakpoint
3975 && bl->loc_type != bp_loc_hardware_breakpoint)
3976 continue;
3977
3978 if ((breakpoint_enabled (bl->owner)
3979 || bl->permanent)
3980 && breakpoint_location_address_range_overlap (bl, aspace,
3981 addr, len))
3982 {
3983 if (overlay_debugging
3984 && section_is_overlay (bl->section)
3985 && !section_is_mapped (bl->section))
3986 {
3987 /* Unmapped overlay -- can't be a match. */
3988 continue;
3989 }
3990
3991 return 1;
3992 }
3993 }
3994
3995 return 0;
3996}
3997
1c5cfe86
PA
3998/* Return true if there's a moribund breakpoint at PC. */
3999
4000int
accd0bcd 4001moribund_breakpoint_here_p (const address_space *aspace, CORE_ADDR pc)
1c5cfe86 4002{
1123588c 4003 for (bp_location *loc : moribund_locations)
f1310107 4004 if (breakpoint_location_address_match (loc, aspace, pc))
1c5cfe86
PA
4005 return 1;
4006
4007 return 0;
4008}
c2c6d25f 4009
f7ce857f
PA
4010/* Returns non-zero iff BL is inserted at PC, in address space
4011 ASPACE. */
4012
4013static int
4014bp_location_inserted_here_p (struct bp_location *bl,
accd0bcd 4015 const address_space *aspace, CORE_ADDR pc)
f7ce857f
PA
4016{
4017 if (bl->inserted
4018 && breakpoint_address_match (bl->pspace->aspace, bl->address,
4019 aspace, pc))
4020 {
4021 if (overlay_debugging
4022 && section_is_overlay (bl->section)
4023 && !section_is_mapped (bl->section))
4024 return 0; /* unmapped overlay -- can't be a match */
4025 else
4026 return 1;
4027 }
4028 return 0;
4029}
4030
a1fd2fa5 4031/* Returns non-zero iff there's a breakpoint inserted at PC. */
c906108c
SS
4032
4033int
accd0bcd 4034breakpoint_inserted_here_p (const address_space *aspace, CORE_ADDR pc)
c906108c 4035{
f7ce857f 4036 struct bp_location **blp, **blp_tmp = NULL;
c906108c 4037
f7ce857f 4038 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
c5aa993b 4039 {
f7ce857f
PA
4040 struct bp_location *bl = *blp;
4041
35df4500
TJB
4042 if (bl->loc_type != bp_loc_software_breakpoint
4043 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
4044 continue;
4045
f7ce857f
PA
4046 if (bp_location_inserted_here_p (bl, aspace, pc))
4047 return 1;
c5aa993b 4048 }
c36b740a
VP
4049 return 0;
4050}
4051
a1fd2fa5
PA
4052/* This function returns non-zero iff there is a software breakpoint
4053 inserted at PC. */
c36b740a
VP
4054
4055int
accd0bcd 4056software_breakpoint_inserted_here_p (const address_space *aspace,
a1fd2fa5 4057 CORE_ADDR pc)
4fa8626c 4058{
f7ce857f 4059 struct bp_location **blp, **blp_tmp = NULL;
4fa8626c 4060
f7ce857f 4061 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4fa8626c 4062 {
f7ce857f
PA
4063 struct bp_location *bl = *blp;
4064
35df4500 4065 if (bl->loc_type != bp_loc_software_breakpoint)
4fa8626c
DJ
4066 continue;
4067
f7ce857f
PA
4068 if (bp_location_inserted_here_p (bl, aspace, pc))
4069 return 1;
4fa8626c
DJ
4070 }
4071
4072 return 0;
9c02b525
PA
4073}
4074
4075/* See breakpoint.h. */
4076
4077int
accd0bcd 4078hardware_breakpoint_inserted_here_p (const address_space *aspace,
9c02b525
PA
4079 CORE_ADDR pc)
4080{
4081 struct bp_location **blp, **blp_tmp = NULL;
9c02b525
PA
4082
4083 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4084 {
4085 struct bp_location *bl = *blp;
4086
4087 if (bl->loc_type != bp_loc_hardware_breakpoint)
4088 continue;
4089
4090 if (bp_location_inserted_here_p (bl, aspace, pc))
4091 return 1;
4092 }
4093
4094 return 0;
4fa8626c
DJ
4095}
4096
9093389c 4097int
accd0bcd 4098hardware_watchpoint_inserted_in_range (const address_space *aspace,
9093389c
PA
4099 CORE_ADDR addr, ULONGEST len)
4100{
4101 struct breakpoint *bpt;
4102
4103 ALL_BREAKPOINTS (bpt)
4104 {
4105 struct bp_location *loc;
4106
4107 if (bpt->type != bp_hardware_watchpoint
4108 && bpt->type != bp_access_watchpoint)
4109 continue;
4110
4111 if (!breakpoint_enabled (bpt))
4112 continue;
4113
4114 for (loc = bpt->loc; loc; loc = loc->next)
4115 if (loc->pspace->aspace == aspace && loc->inserted)
4116 {
4117 CORE_ADDR l, h;
4118
4119 /* Check for intersection. */
768adc05
PA
4120 l = std::max<CORE_ADDR> (loc->address, addr);
4121 h = std::min<CORE_ADDR> (loc->address + loc->length, addr + len);
9093389c
PA
4122 if (l < h)
4123 return 1;
4124 }
4125 }
4126 return 0;
4127}
c5aa993b 4128
f2478a7e 4129/* See breakpoint.h. */
c906108c 4130
f2478a7e
SM
4131bool
4132is_catchpoint (struct breakpoint *b)
c906108c 4133{
f2478a7e 4134 return (b->type == bp_catchpoint);
c906108c
SS
4135}
4136
f431efe5
PA
4137/* Frees any storage that is part of a bpstat. Does not walk the
4138 'next' chain. */
4139
04afa70c 4140bpstats::~bpstats ()
198757a8 4141{
04afa70c
TT
4142 if (bp_location_at != NULL)
4143 decref_bp_location (&bp_location_at);
198757a8
VP
4144}
4145
c906108c
SS
4146/* Clear a bpstat so that it says we are not at any breakpoint.
4147 Also free any storage that is part of a bpstat. */
4148
4149void
fba45db2 4150bpstat_clear (bpstat *bsp)
c906108c
SS
4151{
4152 bpstat p;
4153 bpstat q;
4154
4155 if (bsp == 0)
4156 return;
4157 p = *bsp;
4158 while (p != NULL)
4159 {
4160 q = p->next;
04afa70c 4161 delete p;
c906108c
SS
4162 p = q;
4163 }
4164 *bsp = NULL;
4165}
4166
04afa70c
TT
4167bpstats::bpstats (const bpstats &other)
4168 : next (NULL),
4169 bp_location_at (other.bp_location_at),
4170 breakpoint_at (other.breakpoint_at),
4171 commands (other.commands),
04afa70c
TT
4172 print (other.print),
4173 stop (other.stop),
4174 print_it (other.print_it)
4175{
850645cf
TT
4176 if (other.old_val != NULL)
4177 old_val = release_value (value_copy (other.old_val.get ()));
04afa70c 4178 incref_bp_location (bp_location_at);
04afa70c
TT
4179}
4180
c906108c
SS
4181/* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
4182 is part of the bpstat is copied as well. */
4183
4184bpstat
fba45db2 4185bpstat_copy (bpstat bs)
c906108c
SS
4186{
4187 bpstat p = NULL;
4188 bpstat tmp;
4189 bpstat retval = NULL;
4190
4191 if (bs == NULL)
4192 return bs;
4193
4194 for (; bs != NULL; bs = bs->next)
4195 {
04afa70c 4196 tmp = new bpstats (*bs);
31cc81e9 4197
c906108c
SS
4198 if (p == NULL)
4199 /* This is the first thing in the chain. */
4200 retval = tmp;
4201 else
4202 p->next = tmp;
4203 p = tmp;
4204 }
4205 p->next = NULL;
4206 return retval;
4207}
4208
4a64f543 4209/* Find the bpstat associated with this breakpoint. */
c906108c
SS
4210
4211bpstat
fba45db2 4212bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
c906108c 4213{
c5aa993b
JM
4214 if (bsp == NULL)
4215 return NULL;
c906108c 4216
c5aa993b
JM
4217 for (; bsp != NULL; bsp = bsp->next)
4218 {
f431efe5 4219 if (bsp->breakpoint_at == breakpoint)
c5aa993b
JM
4220 return bsp;
4221 }
c906108c
SS
4222 return NULL;
4223}
4224
ab04a2af
TT
4225/* See breakpoint.h. */
4226
4c462cb0 4227bool
427cd150 4228bpstat_explains_signal (bpstat bsp, enum gdb_signal sig)
ab04a2af 4229{
ab04a2af
TT
4230 for (; bsp != NULL; bsp = bsp->next)
4231 {
427cd150
TT
4232 if (bsp->breakpoint_at == NULL)
4233 {
4234 /* A moribund location can never explain a signal other than
4235 GDB_SIGNAL_TRAP. */
4236 if (sig == GDB_SIGNAL_TRAP)
4c462cb0 4237 return true;
427cd150
TT
4238 }
4239 else
47591c29
PA
4240 {
4241 if (bsp->breakpoint_at->ops->explains_signal (bsp->breakpoint_at,
4242 sig))
4c462cb0 4243 return true;
47591c29 4244 }
ab04a2af
TT
4245 }
4246
4c462cb0 4247 return false;
ab04a2af
TT
4248}
4249
4a64f543
MS
4250/* Put in *NUM the breakpoint number of the first breakpoint we are
4251 stopped at. *BSP upon return is a bpstat which points to the
4252 remaining breakpoints stopped at (but which is not guaranteed to be
4253 good for anything but further calls to bpstat_num).
4254
8671a17b
PA
4255 Return 0 if passed a bpstat which does not indicate any breakpoints.
4256 Return -1 if stopped at a breakpoint that has been deleted since
4257 we set it.
4258 Return 1 otherwise. */
c906108c
SS
4259
4260int
8671a17b 4261bpstat_num (bpstat *bsp, int *num)
c906108c
SS
4262{
4263 struct breakpoint *b;
4264
4265 if ((*bsp) == NULL)
4266 return 0; /* No more breakpoint values */
8671a17b 4267
4a64f543
MS
4268 /* We assume we'll never have several bpstats that correspond to a
4269 single breakpoint -- otherwise, this function might return the
4270 same number more than once and this will look ugly. */
f431efe5 4271 b = (*bsp)->breakpoint_at;
8671a17b
PA
4272 *bsp = (*bsp)->next;
4273 if (b == NULL)
4274 return -1; /* breakpoint that's been deleted since */
4275
4276 *num = b->number; /* We have its number */
4277 return 1;
c906108c
SS
4278}
4279
e93ca019 4280/* See breakpoint.h. */
c906108c
SS
4281
4282void
e93ca019 4283bpstat_clear_actions (void)
c906108c 4284{
e93ca019
JK
4285 bpstat bs;
4286
00431a78 4287 if (inferior_ptid == null_ptid)
e93ca019
JK
4288 return;
4289
00431a78 4290 thread_info *tp = inferior_thread ();
e93ca019 4291 for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
c906108c 4292 {
d1b0a7bf 4293 bs->commands = NULL;
850645cf 4294 bs->old_val.reset (nullptr);
c906108c
SS
4295 }
4296}
4297
f3b1572e
PA
4298/* Called when a command is about to proceed the inferior. */
4299
4300static void
4301breakpoint_about_to_proceed (void)
4302{
d7e15655 4303 if (inferior_ptid != null_ptid)
f3b1572e
PA
4304 {
4305 struct thread_info *tp = inferior_thread ();
4306
4307 /* Allow inferior function calls in breakpoint commands to not
4308 interrupt the command list. When the call finishes
4309 successfully, the inferior will be standing at the same
4310 breakpoint as if nothing happened. */
16c381f0 4311 if (tp->control.in_infcall)
f3b1572e
PA
4312 return;
4313 }
4314
4315 breakpoint_proceeded = 1;
4316}
4317
abf85f46
JK
4318/* Return non-zero iff CMD as the first line of a command sequence is `silent'
4319 or its equivalent. */
4320
4321static int
4322command_line_is_silent (struct command_line *cmd)
4323{
4f45d445 4324 return cmd && (strcmp ("silent", cmd->line) == 0);
abf85f46
JK
4325}
4326
4a64f543
MS
4327/* Execute all the commands associated with all the breakpoints at
4328 this location. Any of these commands could cause the process to
4329 proceed beyond this point, etc. We look out for such changes by
4330 checking the global "breakpoint_proceeded" after each command.
c906108c 4331
347bddb7
PA
4332 Returns true if a breakpoint command resumed the inferior. In that
4333 case, it is the caller's responsibility to recall it again with the
4334 bpstat of the current thread. */
4335
4336static int
4337bpstat_do_actions_1 (bpstat *bsp)
c906108c
SS
4338{
4339 bpstat bs;
347bddb7 4340 int again = 0;
c906108c
SS
4341
4342 /* Avoid endless recursion if a `source' command is contained
4343 in bs->commands. */
4344 if (executing_breakpoint_commands)
347bddb7 4345 return 0;
c906108c 4346
81b1e71c
TT
4347 scoped_restore save_executing
4348 = make_scoped_restore (&executing_breakpoint_commands, 1);
c906108c 4349
1ac32117 4350 scoped_restore preventer = prevent_dont_repeat ();
cf6c5ffb 4351
4a64f543 4352 /* This pointer will iterate over the list of bpstat's. */
c906108c
SS
4353 bs = *bsp;
4354
4355 breakpoint_proceeded = 0;
4356 for (; bs != NULL; bs = bs->next)
4357 {
d1b0a7bf 4358 struct command_line *cmd = NULL;
6c50ab1c
JB
4359
4360 /* Take ownership of the BSP's command tree, if it has one.
4361
4362 The command tree could legitimately contain commands like
4363 'step' and 'next', which call clear_proceed_status, which
4364 frees stop_bpstat's command tree. To make sure this doesn't
4365 free the tree we're executing out from under us, we need to
4366 take ownership of the tree ourselves. Since a given bpstat's
4367 commands are only executed once, we don't need to copy it; we
4368 can clear the pointer in the bpstat, and make sure we free
4369 the tree when we're done. */
d1b0a7bf 4370 counted_command_line ccmd = bs->commands;
9add0f1b 4371 bs->commands = NULL;
d1b0a7bf
TT
4372 if (ccmd != NULL)
4373 cmd = ccmd.get ();
abf85f46
JK
4374 if (command_line_is_silent (cmd))
4375 {
4376 /* The action has been already done by bpstat_stop_status. */
4377 cmd = cmd->next;
4378 }
6c50ab1c 4379
c906108c
SS
4380 while (cmd != NULL)
4381 {
4382 execute_control_command (cmd);
4383
4384 if (breakpoint_proceeded)
4385 break;
4386 else
4387 cmd = cmd->next;
4388 }
6c50ab1c 4389
c906108c 4390 if (breakpoint_proceeded)
32c1e744 4391 {
cb814510 4392 if (current_ui->async)
347bddb7
PA
4393 /* If we are in async mode, then the target might be still
4394 running, not stopped at any breakpoint, so nothing for
4395 us to do here -- just return to the event loop. */
4396 ;
32c1e744
VP
4397 else
4398 /* In sync mode, when execute_control_command returns
4399 we're already standing on the next breakpoint.
347bddb7
PA
4400 Breakpoint commands for that stop were not run, since
4401 execute_command does not run breakpoint commands --
4402 only command_line_handler does, but that one is not
4403 involved in execution of breakpoint commands. So, we
4404 can now execute breakpoint commands. It should be
4405 noted that making execute_command do bpstat actions is
4406 not an option -- in this case we'll have recursive
4407 invocation of bpstat for each breakpoint with a
4408 command, and can easily blow up GDB stack. Instead, we
4409 return true, which will trigger the caller to recall us
4410 with the new stop_bpstat. */
4411 again = 1;
4412 break;
32c1e744 4413 }
c906108c 4414 }
347bddb7
PA
4415 return again;
4416}
4417
00431a78
PA
4418/* Helper for bpstat_do_actions. Get the current thread, if there's
4419 one, is alive and has execution. Return NULL otherwise. */
4420
4421static thread_info *
4422get_bpstat_thread ()
4423{
4424 if (inferior_ptid == null_ptid || !target_has_execution)
4425 return NULL;
4426
4427 thread_info *tp = inferior_thread ();
4428 if (tp->state == THREAD_EXITED || tp->executing)
4429 return NULL;
4430 return tp;
4431}
4432
347bddb7
PA
4433void
4434bpstat_do_actions (void)
4435{
694c6bf5 4436 auto cleanup_if_error = make_scope_exit (bpstat_clear_actions);
00431a78 4437 thread_info *tp;
353d1d73 4438
347bddb7 4439 /* Do any commands attached to breakpoint we are stopped at. */
00431a78
PA
4440 while ((tp = get_bpstat_thread ()) != NULL)
4441 {
4442 /* Since in sync mode, bpstat_do_actions may resume the
4443 inferior, and only return when it is stopped at the next
4444 breakpoint, we keep doing breakpoint actions until it returns
4445 false to indicate the inferior was not resumed. */
4446 if (!bpstat_do_actions_1 (&tp->control.stop_bpstat))
4447 break;
4448 }
353d1d73 4449
694c6bf5 4450 cleanup_if_error.release ();
c906108c
SS
4451}
4452
fa4727a6
DJ
4453/* Print out the (old or new) value associated with a watchpoint. */
4454
4455static void
4456watchpoint_value_print (struct value *val, struct ui_file *stream)
4457{
4458 if (val == NULL)
7f6aba03 4459 fprintf_styled (stream, metadata_style.style (), _("<unreadable>"));
fa4727a6 4460 else
79a45b7d
TT
4461 {
4462 struct value_print_options opts;
4463 get_user_print_options (&opts);
4464 value_print (val, stream, &opts);
4465 }
fa4727a6
DJ
4466}
4467
f303dbd6
PA
4468/* Print the "Thread ID hit" part of "Thread ID hit Breakpoint N" if
4469 debugging multiple threads. */
4470
4471void
4472maybe_print_thread_hit_breakpoint (struct ui_out *uiout)
4473{
112e8700 4474 if (uiout->is_mi_like_p ())
f303dbd6
PA
4475 return;
4476
112e8700 4477 uiout->text ("\n");
f303dbd6
PA
4478
4479 if (show_thread_that_caused_stop ())
4480 {
4481 const char *name;
4482 struct thread_info *thr = inferior_thread ();
4483
112e8700 4484 uiout->text ("Thread ");
33eca680 4485 uiout->field_string ("thread-id", print_thread_id (thr));
f303dbd6
PA
4486
4487 name = thr->name != NULL ? thr->name : target_thread_name (thr);
4488 if (name != NULL)
4489 {
112e8700 4490 uiout->text (" \"");
33eca680 4491 uiout->field_string ("name", name);
112e8700 4492 uiout->text ("\"");
f303dbd6
PA
4493 }
4494
112e8700 4495 uiout->text (" hit ");
f303dbd6
PA
4496 }
4497}
4498
e514a9d6 4499/* Generic routine for printing messages indicating why we
4a64f543 4500 stopped. The behavior of this function depends on the value
e514a9d6
JM
4501 'print_it' in the bpstat structure. Under some circumstances we
4502 may decide not to print anything here and delegate the task to
4a64f543 4503 normal_stop(). */
e514a9d6
JM
4504
4505static enum print_stop_action
4506print_bp_stop_message (bpstat bs)
4507{
4508 switch (bs->print_it)
4509 {
4510 case print_it_noop:
4a64f543 4511 /* Nothing should be printed for this bpstat entry. */
e514a9d6
JM
4512 return PRINT_UNKNOWN;
4513 break;
4514
4515 case print_it_done:
4516 /* We still want to print the frame, but we already printed the
4a64f543 4517 relevant messages. */
e514a9d6
JM
4518 return PRINT_SRC_AND_LOC;
4519 break;
4520
4521 case print_it_normal:
4f8d1dc6 4522 {
f431efe5
PA
4523 struct breakpoint *b = bs->breakpoint_at;
4524
1a6a67de
TJB
4525 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
4526 which has since been deleted. */
4527 if (b == NULL)
4528 return PRINT_UNKNOWN;
4529
348d480f
PA
4530 /* Normal case. Call the breakpoint's print_it method. */
4531 return b->ops->print_it (bs);
4f8d1dc6 4532 }
348d480f 4533 break;
3086aeae 4534
e514a9d6 4535 default:
8e65ff28 4536 internal_error (__FILE__, __LINE__,
e2e0b3e5 4537 _("print_bp_stop_message: unrecognized enum value"));
e514a9d6 4538 break;
c906108c 4539 }
c906108c
SS
4540}
4541
edcc5120
TT
4542/* A helper function that prints a shared library stopped event. */
4543
4544static void
4545print_solib_event (int is_catchpoint)
4546{
6fb16ce6 4547 bool any_deleted = !current_program_space->deleted_solibs.empty ();
bcb430e4 4548 bool any_added = !current_program_space->added_solibs.empty ();
edcc5120
TT
4549
4550 if (!is_catchpoint)
4551 {
4552 if (any_added || any_deleted)
112e8700 4553 current_uiout->text (_("Stopped due to shared library event:\n"));
edcc5120 4554 else
112e8700
SM
4555 current_uiout->text (_("Stopped due to shared library event (no "
4556 "libraries added or removed)\n"));
edcc5120
TT
4557 }
4558
112e8700
SM
4559 if (current_uiout->is_mi_like_p ())
4560 current_uiout->field_string ("reason",
4561 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
edcc5120
TT
4562
4563 if (any_deleted)
4564 {
112e8700 4565 current_uiout->text (_(" Inferior unloaded "));
10f489e5 4566 ui_out_emit_list list_emitter (current_uiout, "removed");
6fb16ce6 4567 for (int ix = 0; ix < current_program_space->deleted_solibs.size (); ix++)
edcc5120 4568 {
6fb16ce6
SM
4569 const std::string &name = current_program_space->deleted_solibs[ix];
4570
edcc5120 4571 if (ix > 0)
112e8700
SM
4572 current_uiout->text (" ");
4573 current_uiout->field_string ("library", name);
4574 current_uiout->text ("\n");
edcc5120 4575 }
edcc5120
TT
4576 }
4577
4578 if (any_added)
4579 {
112e8700 4580 current_uiout->text (_(" Inferior loaded "));
10f489e5 4581 ui_out_emit_list list_emitter (current_uiout, "added");
bcb430e4 4582 bool first = true;
52941706 4583 for (so_list *iter : current_program_space->added_solibs)
edcc5120 4584 {
bcb430e4 4585 if (!first)
112e8700 4586 current_uiout->text (" ");
bcb430e4 4587 first = false;
112e8700
SM
4588 current_uiout->field_string ("library", iter->so_name);
4589 current_uiout->text ("\n");
edcc5120 4590 }
edcc5120
TT
4591 }
4592}
4593
e514a9d6
JM
4594/* Print a message indicating what happened. This is called from
4595 normal_stop(). The input to this routine is the head of the bpstat
36dfb11c
TT
4596 list - a list of the eventpoints that caused this stop. KIND is
4597 the target_waitkind for the stopping event. This
e514a9d6
JM
4598 routine calls the generic print routine for printing a message
4599 about reasons for stopping. This will print (for example) the
4600 "Breakpoint n," part of the output. The return value of this
4601 routine is one of:
c906108c 4602
4a64f543 4603 PRINT_UNKNOWN: Means we printed nothing.
917317f4 4604 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4a64f543 4605 code to print the location. An example is
c5aa993b
JM
4606 "Breakpoint 1, " which should be followed by
4607 the location.
917317f4 4608 PRINT_SRC_ONLY: Means we printed something, but there is no need
c5aa993b
JM
4609 to also print the location part of the message.
4610 An example is the catch/throw messages, which
4a64f543 4611 don't require a location appended to the end.
917317f4 4612 PRINT_NOTHING: We have done some printing and we don't need any
4a64f543 4613 further info to be printed. */
c906108c 4614
917317f4 4615enum print_stop_action
36dfb11c 4616bpstat_print (bpstat bs, int kind)
c906108c 4617{
f486487f 4618 enum print_stop_action val;
c5aa993b 4619
c906108c 4620 /* Maybe another breakpoint in the chain caused us to stop.
53a5351d
JM
4621 (Currently all watchpoints go on the bpstat whether hit or not.
4622 That probably could (should) be changed, provided care is taken
c906108c 4623 with respect to bpstat_explains_signal). */
e514a9d6
JM
4624 for (; bs; bs = bs->next)
4625 {
4626 val = print_bp_stop_message (bs);
4627 if (val == PRINT_SRC_ONLY
4628 || val == PRINT_SRC_AND_LOC
4629 || val == PRINT_NOTHING)
4630 return val;
4631 }
c906108c 4632
36dfb11c
TT
4633 /* If we had hit a shared library event breakpoint,
4634 print_bp_stop_message would print out this message. If we hit an
4635 OS-level shared library event, do the same thing. */
4636 if (kind == TARGET_WAITKIND_LOADED)
4637 {
edcc5120 4638 print_solib_event (0);
36dfb11c
TT
4639 return PRINT_NOTHING;
4640 }
4641
e514a9d6 4642 /* We reached the end of the chain, or we got a null BS to start
4a64f543 4643 with and nothing was printed. */
917317f4 4644 return PRINT_UNKNOWN;
c906108c
SS
4645}
4646
bf469271 4647/* Evaluate the boolean expression EXP and return the result. */
c906108c 4648
bf469271
PA
4649static bool
4650breakpoint_cond_eval (expression *exp)
c906108c 4651{
278cd55f 4652 struct value *mark = value_mark ();
bf469271 4653 bool res = value_true (evaluate_expression (exp));
cc59ec59 4654
c906108c 4655 value_free_to_mark (mark);
bf469271 4656 return res;
c906108c
SS
4657}
4658
5760d0ab 4659/* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
c906108c 4660
04afa70c
TT
4661bpstats::bpstats (struct bp_location *bl, bpstat **bs_link_pointer)
4662 : next (NULL),
4663 bp_location_at (bl),
4664 breakpoint_at (bl->owner),
4665 commands (NULL),
04afa70c
TT
4666 print (0),
4667 stop (0),
4668 print_it (print_it_normal)
c906108c 4669{
f431efe5 4670 incref_bp_location (bl);
04afa70c
TT
4671 **bs_link_pointer = this;
4672 *bs_link_pointer = &next;
4673}
4674
4675bpstats::bpstats ()
4676 : next (NULL),
4677 bp_location_at (NULL),
4678 breakpoint_at (NULL),
4679 commands (NULL),
04afa70c
TT
4680 print (0),
4681 stop (0),
4682 print_it (print_it_normal)
4683{
c906108c
SS
4684}
4685\f
d983da9c
DJ
4686/* The target has stopped with waitstatus WS. Check if any hardware
4687 watchpoints have triggered, according to the target. */
4688
4689int
4690watchpoints_triggered (struct target_waitstatus *ws)
4691{
57810aa7 4692 bool stopped_by_watchpoint = target_stopped_by_watchpoint ();
d983da9c
DJ
4693 CORE_ADDR addr;
4694 struct breakpoint *b;
4695
4696 if (!stopped_by_watchpoint)
4697 {
4698 /* We were not stopped by a watchpoint. Mark all watchpoints
4699 as not triggered. */
4700 ALL_BREAKPOINTS (b)
cc60f2e3 4701 if (is_hardware_watchpoint (b))
3a5c3e22
PA
4702 {
4703 struct watchpoint *w = (struct watchpoint *) b;
4704
4705 w->watchpoint_triggered = watch_triggered_no;
4706 }
d983da9c
DJ
4707
4708 return 0;
4709 }
4710
8b88a78e 4711 if (!target_stopped_data_address (current_top_target (), &addr))
d983da9c
DJ
4712 {
4713 /* We were stopped by a watchpoint, but we don't know where.
4714 Mark all watchpoints as unknown. */
4715 ALL_BREAKPOINTS (b)
cc60f2e3 4716 if (is_hardware_watchpoint (b))
3a5c3e22
PA
4717 {
4718 struct watchpoint *w = (struct watchpoint *) b;
4719
4720 w->watchpoint_triggered = watch_triggered_unknown;
4721 }
d983da9c 4722
3c4797ba 4723 return 1;
d983da9c
DJ
4724 }
4725
4726 /* The target could report the data address. Mark watchpoints
4727 affected by this data address as triggered, and all others as not
4728 triggered. */
4729
4730 ALL_BREAKPOINTS (b)
cc60f2e3 4731 if (is_hardware_watchpoint (b))
d983da9c 4732 {
3a5c3e22 4733 struct watchpoint *w = (struct watchpoint *) b;
a5606eee 4734 struct bp_location *loc;
d983da9c 4735
3a5c3e22 4736 w->watchpoint_triggered = watch_triggered_no;
a5606eee 4737 for (loc = b->loc; loc; loc = loc->next)
9c06b0b4 4738 {
3a5c3e22 4739 if (is_masked_watchpoint (b))
9c06b0b4 4740 {
3a5c3e22
PA
4741 CORE_ADDR newaddr = addr & w->hw_wp_mask;
4742 CORE_ADDR start = loc->address & w->hw_wp_mask;
9c06b0b4
TJB
4743
4744 if (newaddr == start)
4745 {
3a5c3e22 4746 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
4747 break;
4748 }
4749 }
4750 /* Exact match not required. Within range is sufficient. */
8b88a78e 4751 else if (target_watchpoint_addr_within_range (current_top_target (),
9c06b0b4
TJB
4752 addr, loc->address,
4753 loc->length))
4754 {
3a5c3e22 4755 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
4756 break;
4757 }
4758 }
d983da9c
DJ
4759 }
4760
4761 return 1;
4762}
4763
bf469271
PA
4764/* Possible return values for watchpoint_check. */
4765enum wp_check_result
4766 {
4767 /* The watchpoint has been deleted. */
4768 WP_DELETED = 1,
4769
4770 /* The value has changed. */
4771 WP_VALUE_CHANGED = 2,
4772
4773 /* The value has not changed. */
4774 WP_VALUE_NOT_CHANGED = 3,
4775
4776 /* Ignore this watchpoint, no matter if the value changed or not. */
4777 WP_IGNORE = 4,
4778 };
c906108c
SS
4779
4780#define BP_TEMPFLAG 1
4781#define BP_HARDWAREFLAG 2
4782
4a64f543 4783/* Evaluate watchpoint condition expression and check if its value
bf469271 4784 changed. */
553e4c11 4785
bf469271
PA
4786static wp_check_result
4787watchpoint_check (bpstat bs)
c906108c 4788{
3a5c3e22 4789 struct watchpoint *b;
c906108c
SS
4790 struct frame_info *fr;
4791 int within_current_scope;
4792
f431efe5 4793 /* BS is built from an existing struct breakpoint. */
2bdf28a0 4794 gdb_assert (bs->breakpoint_at != NULL);
3a5c3e22 4795 b = (struct watchpoint *) bs->breakpoint_at;
d0fb5eae 4796
f6bc2008
PA
4797 /* If this is a local watchpoint, we only want to check if the
4798 watchpoint frame is in scope if the current thread is the thread
4799 that was used to create the watchpoint. */
4800 if (!watchpoint_in_thread_scope (b))
60e1c644 4801 return WP_IGNORE;
f6bc2008 4802
c906108c
SS
4803 if (b->exp_valid_block == NULL)
4804 within_current_scope = 1;
4805 else
4806 {
edb3359d
DJ
4807 struct frame_info *frame = get_current_frame ();
4808 struct gdbarch *frame_arch = get_frame_arch (frame);
4809 CORE_ADDR frame_pc = get_frame_pc (frame);
4810
c9cf6e20 4811 /* stack_frame_destroyed_p() returns a non-zero value if we're
4a64f543
MS
4812 still in the function but the stack frame has already been
4813 invalidated. Since we can't rely on the values of local
4814 variables after the stack has been destroyed, we are treating
4815 the watchpoint in that state as `not changed' without further
4816 checking. Don't mark watchpoints as changed if the current
4817 frame is in an epilogue - even if they are in some other
4818 frame, our view of the stack is likely to be wrong and
4819 frame_find_by_id could error out. */
c9cf6e20 4820 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
60e1c644 4821 return WP_IGNORE;
a0f49112 4822
101dcfbe 4823 fr = frame_find_by_id (b->watchpoint_frame);
c906108c 4824 within_current_scope = (fr != NULL);
69fbadd5
DJ
4825
4826 /* If we've gotten confused in the unwinder, we might have
4827 returned a frame that can't describe this variable. */
edb3359d
DJ
4828 if (within_current_scope)
4829 {
4830 struct symbol *function;
4831
4832 function = get_frame_function (fr);
4833 if (function == NULL
4834 || !contained_in (b->exp_valid_block,
4835 SYMBOL_BLOCK_VALUE (function)))
4836 within_current_scope = 0;
4837 }
69fbadd5 4838
edb3359d 4839 if (within_current_scope)
c906108c
SS
4840 /* If we end up stopping, the current frame will get selected
4841 in normal_stop. So this call to select_frame won't affect
4842 the user. */
0f7d239c 4843 select_frame (fr);
c906108c 4844 }
c5aa993b 4845
c906108c
SS
4846 if (within_current_scope)
4847 {
4a64f543
MS
4848 /* We use value_{,free_to_}mark because it could be a *long*
4849 time before we return to the command level and call
4850 free_all_values. We can't call free_all_values because we
4851 might be in the middle of evaluating a function call. */
c906108c 4852
0cf6dd15 4853 int pc = 0;
9c06b0b4 4854 struct value *mark;
fa4727a6
DJ
4855 struct value *new_val;
4856
c1fc2657 4857 if (is_masked_watchpoint (b))
9c06b0b4
TJB
4858 /* Since we don't know the exact trigger address (from
4859 stopped_data_address), just tell the user we've triggered
4860 a mask watchpoint. */
4861 return WP_VALUE_CHANGED;
4862
4863 mark = value_mark ();
4d01a485 4864 fetch_subexp_value (b->exp.get (), &pc, &new_val, NULL, NULL, 0);
218d2fc6 4865
bb9d5f81
PP
4866 if (b->val_bitsize != 0)
4867 new_val = extract_bitfield_from_watchpoint_value (b, new_val);
4868
4a64f543
MS
4869 /* We use value_equal_contents instead of value_equal because
4870 the latter coerces an array to a pointer, thus comparing just
4871 the address of the array instead of its contents. This is
4872 not what we want. */
fa4727a6 4873 if ((b->val != NULL) != (new_val != NULL)
850645cf
TT
4874 || (b->val != NULL && !value_equal_contents (b->val.get (),
4875 new_val)))
c906108c 4876 {
c906108c 4877 bs->old_val = b->val;
850645cf 4878 b->val = release_value (new_val);
4c1d86d9 4879 b->val_valid = true;
850645cf
TT
4880 if (new_val != NULL)
4881 value_free_to_mark (mark);
c906108c
SS
4882 return WP_VALUE_CHANGED;
4883 }
4884 else
4885 {
60e1c644 4886 /* Nothing changed. */
c906108c 4887 value_free_to_mark (mark);
c906108c
SS
4888 return WP_VALUE_NOT_CHANGED;
4889 }
4890 }
4891 else
4892 {
4893 /* This seems like the only logical thing to do because
c5aa993b
JM
4894 if we temporarily ignored the watchpoint, then when
4895 we reenter the block in which it is valid it contains
4896 garbage (in the case of a function, it may have two
4897 garbage values, one before and one after the prologue).
4898 So we can't even detect the first assignment to it and
4899 watch after that (since the garbage may or may not equal
4900 the first value assigned). */
348d480f
PA
4901 /* We print all the stop information in
4902 breakpoint_ops->print_it, but in this case, by the time we
4903 call breakpoint_ops->print_it this bp will be deleted
4904 already. So we have no choice but print the information
4905 here. */
468afe6c 4906
0e454242 4907 SWITCH_THRU_ALL_UIS ()
468afe6c
PA
4908 {
4909 struct ui_out *uiout = current_uiout;
4910
112e8700
SM
4911 if (uiout->is_mi_like_p ())
4912 uiout->field_string
4913 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
6a831f06
PA
4914 uiout->message ("\nWatchpoint %pF deleted because the program has "
4915 "left the block in\n"
4916 "which its expression is valid.\n",
4917 signed_field ("wpnum", b->number));
468afe6c 4918 }
4ce44c66 4919
cdac0397 4920 /* Make sure the watchpoint's commands aren't executed. */
d1b0a7bf 4921 b->commands = NULL;
d0fb5eae 4922 watchpoint_del_at_next_stop (b);
c906108c
SS
4923
4924 return WP_DELETED;
4925 }
4926}
4927
18a18393 4928/* Return true if it looks like target has stopped due to hitting
348d480f
PA
4929 breakpoint location BL. This function does not check if we should
4930 stop, only if BL explains the stop. */
4931
18a18393 4932static int
6c95b8df 4933bpstat_check_location (const struct bp_location *bl,
accd0bcd 4934 const address_space *aspace, CORE_ADDR bp_addr,
09ac7c10 4935 const struct target_waitstatus *ws)
18a18393
VP
4936{
4937 struct breakpoint *b = bl->owner;
4938
348d480f 4939 /* BL is from an existing breakpoint. */
2bdf28a0
JK
4940 gdb_assert (b != NULL);
4941
bd522513 4942 return b->ops->breakpoint_hit (bl, aspace, bp_addr, ws);
18a18393
VP
4943}
4944
3a5c3e22
PA
4945/* Determine if the watched values have actually changed, and we
4946 should stop. If not, set BS->stop to 0. */
4947
18a18393
VP
4948static void
4949bpstat_check_watchpoint (bpstat bs)
4950{
2bdf28a0 4951 const struct bp_location *bl;
3a5c3e22 4952 struct watchpoint *b;
2bdf28a0
JK
4953
4954 /* BS is built for existing struct breakpoint. */
f431efe5 4955 bl = bs->bp_location_at;
2bdf28a0 4956 gdb_assert (bl != NULL);
3a5c3e22 4957 b = (struct watchpoint *) bs->breakpoint_at;
2bdf28a0 4958 gdb_assert (b != NULL);
18a18393 4959
18a18393 4960 {
18a18393
VP
4961 int must_check_value = 0;
4962
c1fc2657 4963 if (b->type == bp_watchpoint)
18a18393
VP
4964 /* For a software watchpoint, we must always check the
4965 watched value. */
4966 must_check_value = 1;
4967 else if (b->watchpoint_triggered == watch_triggered_yes)
4968 /* We have a hardware watchpoint (read, write, or access)
4969 and the target earlier reported an address watched by
4970 this watchpoint. */
4971 must_check_value = 1;
4972 else if (b->watchpoint_triggered == watch_triggered_unknown
c1fc2657 4973 && b->type == bp_hardware_watchpoint)
18a18393
VP
4974 /* We were stopped by a hardware watchpoint, but the target could
4975 not report the data address. We must check the watchpoint's
4976 value. Access and read watchpoints are out of luck; without
4977 a data address, we can't figure it out. */
4978 must_check_value = 1;
3a5c3e22 4979
18a18393
VP
4980 if (must_check_value)
4981 {
bf469271
PA
4982 wp_check_result e;
4983
a70b8144 4984 try
bf469271
PA
4985 {
4986 e = watchpoint_check (bs);
4987 }
230d2906 4988 catch (const gdb_exception &ex)
bf469271
PA
4989 {
4990 exception_fprintf (gdb_stderr, ex,
4991 "Error evaluating expression "
4992 "for watchpoint %d\n",
4993 b->number);
4994
4995 SWITCH_THRU_ALL_UIS ()
4996 {
4997 printf_filtered (_("Watchpoint %d deleted.\n"),
4998 b->number);
4999 }
5000 watchpoint_del_at_next_stop (b);
5001 e = WP_DELETED;
5002 }
bf469271 5003
18a18393
VP
5004 switch (e)
5005 {
5006 case WP_DELETED:
5007 /* We've already printed what needs to be printed. */
5008 bs->print_it = print_it_done;
5009 /* Stop. */
5010 break;
60e1c644
PA
5011 case WP_IGNORE:
5012 bs->print_it = print_it_noop;
5013 bs->stop = 0;
5014 break;
18a18393 5015 case WP_VALUE_CHANGED:
c1fc2657 5016 if (b->type == bp_read_watchpoint)
18a18393 5017 {
85d721b8
PA
5018 /* There are two cases to consider here:
5019
4a64f543 5020 1. We're watching the triggered memory for reads.
85d721b8
PA
5021 In that case, trust the target, and always report
5022 the watchpoint hit to the user. Even though
5023 reads don't cause value changes, the value may
5024 have changed since the last time it was read, and
5025 since we're not trapping writes, we will not see
5026 those, and as such we should ignore our notion of
5027 old value.
5028
4a64f543 5029 2. We're watching the triggered memory for both
85d721b8
PA
5030 reads and writes. There are two ways this may
5031 happen:
5032
4a64f543 5033 2.1. This is a target that can't break on data
85d721b8
PA
5034 reads only, but can break on accesses (reads or
5035 writes), such as e.g., x86. We detect this case
5036 at the time we try to insert read watchpoints.
5037
4a64f543 5038 2.2. Otherwise, the target supports read
85d721b8
PA
5039 watchpoints, but, the user set an access or write
5040 watchpoint watching the same memory as this read
5041 watchpoint.
5042
5043 If we're watching memory writes as well as reads,
5044 ignore watchpoint hits when we find that the
5045 value hasn't changed, as reads don't cause
5046 changes. This still gives false positives when
5047 the program writes the same value to memory as
5048 what there was already in memory (we will confuse
5049 it for a read), but it's much better than
5050 nothing. */
5051
5052 int other_write_watchpoint = 0;
5053
5054 if (bl->watchpoint_type == hw_read)
5055 {
5056 struct breakpoint *other_b;
5057
5058 ALL_BREAKPOINTS (other_b)
3a5c3e22
PA
5059 if (other_b->type == bp_hardware_watchpoint
5060 || other_b->type == bp_access_watchpoint)
85d721b8 5061 {
3a5c3e22
PA
5062 struct watchpoint *other_w =
5063 (struct watchpoint *) other_b;
5064
5065 if (other_w->watchpoint_triggered
5066 == watch_triggered_yes)
5067 {
5068 other_write_watchpoint = 1;
5069 break;
5070 }
85d721b8
PA
5071 }
5072 }
5073
5074 if (other_write_watchpoint
5075 || bl->watchpoint_type == hw_access)
5076 {
5077 /* We're watching the same memory for writes,
5078 and the value changed since the last time we
5079 updated it, so this trap must be for a write.
5080 Ignore it. */
5081 bs->print_it = print_it_noop;
5082 bs->stop = 0;
5083 }
18a18393
VP
5084 }
5085 break;
5086 case WP_VALUE_NOT_CHANGED:
c1fc2657
SM
5087 if (b->type == bp_hardware_watchpoint
5088 || b->type == bp_watchpoint)
18a18393
VP
5089 {
5090 /* Don't stop: write watchpoints shouldn't fire if
5091 the value hasn't changed. */
5092 bs->print_it = print_it_noop;
5093 bs->stop = 0;
5094 }
5095 /* Stop. */
5096 break;
5097 default:
5098 /* Can't happen. */
18a18393
VP
5099 break;
5100 }
5101 }
5102 else /* must_check_value == 0 */
5103 {
5104 /* This is a case where some watchpoint(s) triggered, but
5105 not at the address of this watchpoint, or else no
5106 watchpoint triggered after all. So don't print
5107 anything for this watchpoint. */
5108 bs->print_it = print_it_noop;
5109 bs->stop = 0;
5110 }
5111 }
5112}
5113
7d4df6a4
DE
5114/* For breakpoints that are currently marked as telling gdb to stop,
5115 check conditions (condition proper, frame, thread and ignore count)
18a18393
VP
5116 of breakpoint referred to by BS. If we should not stop for this
5117 breakpoint, set BS->stop to 0. */
f431efe5 5118
18a18393 5119static void
00431a78 5120bpstat_check_breakpoint_conditions (bpstat bs, thread_info *thread)
18a18393 5121{
2bdf28a0
JK
5122 const struct bp_location *bl;
5123 struct breakpoint *b;
bf469271
PA
5124 /* Assume stop. */
5125 bool condition_result = true;
7d4df6a4
DE
5126 struct expression *cond;
5127
5128 gdb_assert (bs->stop);
2bdf28a0
JK
5129
5130 /* BS is built for existing struct breakpoint. */
f431efe5 5131 bl = bs->bp_location_at;
2bdf28a0 5132 gdb_assert (bl != NULL);
f431efe5 5133 b = bs->breakpoint_at;
2bdf28a0 5134 gdb_assert (b != NULL);
18a18393 5135
b775012e
LM
5136 /* Even if the target evaluated the condition on its end and notified GDB, we
5137 need to do so again since GDB does not know if we stopped due to a
5138 breakpoint or a single step breakpoint. */
5139
18a18393 5140 if (frame_id_p (b->frame_id)
edb3359d 5141 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
18a18393 5142 {
7d4df6a4
DE
5143 bs->stop = 0;
5144 return;
5145 }
60e1c644 5146
12ab52e9
PA
5147 /* If this is a thread/task-specific breakpoint, don't waste cpu
5148 evaluating the condition if this isn't the specified
5149 thread/task. */
00431a78
PA
5150 if ((b->thread != -1 && b->thread != thread->global_num)
5151 || (b->task != 0 && b->task != ada_get_task_number (thread)))
6c1b0f7b
DE
5152 {
5153 bs->stop = 0;
5154 return;
5155 }
5156
6dddc817
DE
5157 /* Evaluate extension language breakpoints that have a "stop" method
5158 implemented. */
5159 bs->stop = breakpoint_ext_lang_cond_says_stop (b);
7371cf6d 5160
7d4df6a4
DE
5161 if (is_watchpoint (b))
5162 {
5163 struct watchpoint *w = (struct watchpoint *) b;
3a5c3e22 5164
4d01a485 5165 cond = w->cond_exp.get ();
7d4df6a4
DE
5166 }
5167 else
4d01a485 5168 cond = bl->cond.get ();
60e1c644 5169
7d4df6a4
DE
5170 if (cond && b->disposition != disp_del_at_next_stop)
5171 {
5172 int within_current_scope = 1;
5173 struct watchpoint * w;
60e1c644 5174
7d4df6a4
DE
5175 /* We use value_mark and value_free_to_mark because it could
5176 be a long time before we return to the command level and
5177 call free_all_values. We can't call free_all_values
5178 because we might be in the middle of evaluating a
5179 function call. */
5180 struct value *mark = value_mark ();
5181
5182 if (is_watchpoint (b))
5183 w = (struct watchpoint *) b;
5184 else
5185 w = NULL;
5186
5187 /* Need to select the frame, with all that implies so that
5188 the conditions will have the right context. Because we
5189 use the frame, we will not see an inlined function's
5190 variables when we arrive at a breakpoint at the start
5191 of the inlined function; the current frame will be the
5192 call site. */
5193 if (w == NULL || w->cond_exp_valid_block == NULL)
5194 select_frame (get_current_frame ());
5195 else
18a18393 5196 {
7d4df6a4
DE
5197 struct frame_info *frame;
5198
5199 /* For local watchpoint expressions, which particular
5200 instance of a local is being watched matters, so we
5201 keep track of the frame to evaluate the expression
5202 in. To evaluate the condition however, it doesn't
5203 really matter which instantiation of the function
5204 where the condition makes sense triggers the
5205 watchpoint. This allows an expression like "watch
5206 global if q > 10" set in `func', catch writes to
5207 global on all threads that call `func', or catch
5208 writes on all recursive calls of `func' by a single
5209 thread. We simply always evaluate the condition in
5210 the innermost frame that's executing where it makes
5211 sense to evaluate the condition. It seems
5212 intuitive. */
5213 frame = block_innermost_frame (w->cond_exp_valid_block);
5214 if (frame != NULL)
5215 select_frame (frame);
5216 else
5217 within_current_scope = 0;
18a18393 5218 }
7d4df6a4 5219 if (within_current_scope)
bf469271 5220 {
a70b8144 5221 try
bf469271
PA
5222 {
5223 condition_result = breakpoint_cond_eval (cond);
5224 }
230d2906 5225 catch (const gdb_exception &ex)
bf469271
PA
5226 {
5227 exception_fprintf (gdb_stderr, ex,
5228 "Error in testing breakpoint condition:\n");
5229 }
bf469271 5230 }
7d4df6a4 5231 else
18a18393 5232 {
7d4df6a4
DE
5233 warning (_("Watchpoint condition cannot be tested "
5234 "in the current scope"));
5235 /* If we failed to set the right context for this
5236 watchpoint, unconditionally report it. */
18a18393 5237 }
7d4df6a4
DE
5238 /* FIXME-someday, should give breakpoint #. */
5239 value_free_to_mark (mark);
18a18393 5240 }
7d4df6a4 5241
bf469271 5242 if (cond && !condition_result)
7d4df6a4
DE
5243 {
5244 bs->stop = 0;
5245 }
7d4df6a4
DE
5246 else if (b->ignore_count > 0)
5247 {
5248 b->ignore_count--;
5249 bs->stop = 0;
5250 /* Increase the hit count even though we don't stop. */
5251 ++(b->hit_count);
76727919 5252 gdb::observers::breakpoint_modified.notify (b);
7d4df6a4 5253 }
18a18393
VP
5254}
5255
1cf4d951
PA
5256/* Returns true if we need to track moribund locations of LOC's type
5257 on the current target. */
5258
5259static int
5260need_moribund_for_location_type (struct bp_location *loc)
5261{
5262 return ((loc->loc_type == bp_loc_software_breakpoint
5263 && !target_supports_stopped_by_sw_breakpoint ())
5264 || (loc->loc_type == bp_loc_hardware_breakpoint
5265 && !target_supports_stopped_by_hw_breakpoint ()));
5266}
5267
ddfe970e 5268/* See breakpoint.h. */
c906108c
SS
5269
5270bpstat
ddfe970e 5271build_bpstat_chain (const address_space *aspace, CORE_ADDR bp_addr,
09ac7c10 5272 const struct target_waitstatus *ws)
c906108c 5273{
ddfe970e 5274 struct breakpoint *b;
5760d0ab 5275 bpstat bs_head = NULL, *bs_link = &bs_head;
c5aa993b 5276
429374b8
JK
5277 ALL_BREAKPOINTS (b)
5278 {
1a853c52 5279 if (!breakpoint_enabled (b))
429374b8 5280 continue;
a5606eee 5281
ddfe970e 5282 for (bp_location *bl = b->loc; bl != NULL; bl = bl->next)
429374b8 5283 {
4a64f543
MS
5284 /* For hardware watchpoints, we look only at the first
5285 location. The watchpoint_check function will work on the
5286 entire expression, not the individual locations. For
5287 read watchpoints, the watchpoints_triggered function has
5288 checked all locations already. */
429374b8
JK
5289 if (b->type == bp_hardware_watchpoint && bl != b->loc)
5290 break;
18a18393 5291
f6592439 5292 if (!bl->enabled || bl->shlib_disabled)
429374b8 5293 continue;
c5aa993b 5294
09ac7c10 5295 if (!bpstat_check_location (bl, aspace, bp_addr, ws))
429374b8 5296 continue;
c5aa993b 5297
4a64f543
MS
5298 /* Come here if it's a watchpoint, or if the break address
5299 matches. */
c5aa993b 5300
ddfe970e
KS
5301 bpstat bs = new bpstats (bl, &bs_link); /* Alloc a bpstat to
5302 explain stop. */
c5aa993b 5303
f431efe5
PA
5304 /* Assume we stop. Should we find a watchpoint that is not
5305 actually triggered, or if the condition of the breakpoint
5306 evaluates as false, we'll reset 'stop' to 0. */
429374b8
JK
5307 bs->stop = 1;
5308 bs->print = 1;
d983da9c 5309
f431efe5
PA
5310 /* If this is a scope breakpoint, mark the associated
5311 watchpoint as triggered so that we will handle the
5312 out-of-scope event. We'll get to the watchpoint next
5313 iteration. */
d0fb5eae 5314 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
3a5c3e22
PA
5315 {
5316 struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
5317
5318 w->watchpoint_triggered = watch_triggered_yes;
5319 }
f431efe5
PA
5320 }
5321 }
5322
7c16b83e 5323 /* Check if a moribund breakpoint explains the stop. */
1cf4d951
PA
5324 if (!target_supports_stopped_by_sw_breakpoint ()
5325 || !target_supports_stopped_by_hw_breakpoint ())
f431efe5 5326 {
1123588c 5327 for (bp_location *loc : moribund_locations)
f431efe5 5328 {
1cf4d951
PA
5329 if (breakpoint_location_address_match (loc, aspace, bp_addr)
5330 && need_moribund_for_location_type (loc))
5331 {
ddfe970e 5332 bpstat bs = new bpstats (loc, &bs_link);
1cf4d951
PA
5333 /* For hits of moribund locations, we should just proceed. */
5334 bs->stop = 0;
5335 bs->print = 0;
5336 bs->print_it = print_it_noop;
5337 }
f431efe5
PA
5338 }
5339 }
5340
ddfe970e
KS
5341 return bs_head;
5342}
5343
5344/* See breakpoint.h. */
5345
5346bpstat
5347bpstat_stop_status (const address_space *aspace,
00431a78 5348 CORE_ADDR bp_addr, thread_info *thread,
ddfe970e
KS
5349 const struct target_waitstatus *ws,
5350 bpstat stop_chain)
5351{
5352 struct breakpoint *b = NULL;
5353 /* First item of allocated bpstat's. */
5354 bpstat bs_head = stop_chain;
5355 bpstat bs;
5356 int need_remove_insert;
5357 int removed_any;
5358
5359 /* First, build the bpstat chain with locations that explain a
5360 target stop, while being careful to not set the target running,
5361 as that may invalidate locations (in particular watchpoint
5362 locations are recreated). Resuming will happen here with
5363 breakpoint conditions or watchpoint expressions that include
5364 inferior function calls. */
5365 if (bs_head == NULL)
5366 bs_head = build_bpstat_chain (aspace, bp_addr, ws);
5367
edcc5120
TT
5368 /* A bit of special processing for shlib breakpoints. We need to
5369 process solib loading here, so that the lists of loaded and
5370 unloaded libraries are correct before we handle "catch load" and
5371 "catch unload". */
5372 for (bs = bs_head; bs != NULL; bs = bs->next)
5373 {
5d268276 5374 if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
edcc5120
TT
5375 {
5376 handle_solib_event ();
5377 break;
5378 }
5379 }
5380
f431efe5
PA
5381 /* Now go through the locations that caused the target to stop, and
5382 check whether we're interested in reporting this stop to higher
5383 layers, or whether we should resume the target transparently. */
5384
5385 removed_any = 0;
5386
5760d0ab 5387 for (bs = bs_head; bs != NULL; bs = bs->next)
f431efe5
PA
5388 {
5389 if (!bs->stop)
5390 continue;
5391
f431efe5 5392 b = bs->breakpoint_at;
348d480f
PA
5393 b->ops->check_status (bs);
5394 if (bs->stop)
28010a5d 5395 {
00431a78 5396 bpstat_check_breakpoint_conditions (bs, thread);
f431efe5 5397
429374b8
JK
5398 if (bs->stop)
5399 {
5400 ++(b->hit_count);
76727919 5401 gdb::observers::breakpoint_modified.notify (b);
c906108c 5402
4a64f543 5403 /* We will stop here. */
429374b8
JK
5404 if (b->disposition == disp_disable)
5405 {
816338b5 5406 --(b->enable_count);
1a853c52 5407 if (b->enable_count <= 0)
429374b8 5408 b->enable_state = bp_disabled;
f431efe5 5409 removed_any = 1;
429374b8
JK
5410 }
5411 if (b->silent)
5412 bs->print = 0;
5413 bs->commands = b->commands;
abf85f46 5414 if (command_line_is_silent (bs->commands
d1b0a7bf 5415 ? bs->commands.get () : NULL))
abf85f46 5416 bs->print = 0;
9d6e6e84
HZ
5417
5418 b->ops->after_condition_true (bs);
429374b8
JK
5419 }
5420
348d480f 5421 }
a9b3a50f
PA
5422
5423 /* Print nothing for this entry if we don't stop or don't
5424 print. */
5425 if (!bs->stop || !bs->print)
5426 bs->print_it = print_it_noop;
429374b8 5427 }
876fa593 5428
d983da9c
DJ
5429 /* If we aren't stopping, the value of some hardware watchpoint may
5430 not have changed, but the intermediate memory locations we are
5431 watching may have. Don't bother if we're stopping; this will get
5432 done later. */
d832cb68 5433 need_remove_insert = 0;
5760d0ab
JK
5434 if (! bpstat_causes_stop (bs_head))
5435 for (bs = bs_head; bs != NULL; bs = bs->next)
d983da9c 5436 if (!bs->stop
f431efe5
PA
5437 && bs->breakpoint_at
5438 && is_hardware_watchpoint (bs->breakpoint_at))
d983da9c 5439 {
3a5c3e22
PA
5440 struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
5441
5442 update_watchpoint (w, 0 /* don't reparse. */);
d832cb68 5443 need_remove_insert = 1;
d983da9c
DJ
5444 }
5445
d832cb68 5446 if (need_remove_insert)
44702360 5447 update_global_location_list (UGLL_MAY_INSERT);
f431efe5 5448 else if (removed_any)
44702360 5449 update_global_location_list (UGLL_DONT_INSERT);
d832cb68 5450
5760d0ab 5451 return bs_head;
c906108c 5452}
628fe4e4
JK
5453
5454static void
5455handle_jit_event (void)
5456{
5457 struct frame_info *frame;
5458 struct gdbarch *gdbarch;
5459
243a9253
PA
5460 if (debug_infrun)
5461 fprintf_unfiltered (gdb_stdlog, "handling bp_jit_event\n");
5462
628fe4e4
JK
5463 /* Switch terminal for any messages produced by
5464 breakpoint_re_set. */
223ffa71 5465 target_terminal::ours_for_output ();
628fe4e4
JK
5466
5467 frame = get_current_frame ();
5468 gdbarch = get_frame_arch (frame);
5469
5470 jit_event_handler (gdbarch);
5471
223ffa71 5472 target_terminal::inferior ();
628fe4e4
JK
5473}
5474
5475/* Prepare WHAT final decision for infrun. */
5476
5477/* Decide what infrun needs to do with this bpstat. */
5478
c906108c 5479struct bpstat_what
0e30163f 5480bpstat_what (bpstat bs_head)
c906108c 5481{
c906108c 5482 struct bpstat_what retval;
0e30163f 5483 bpstat bs;
c906108c 5484
628fe4e4 5485 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
aa7d318d 5486 retval.call_dummy = STOP_NONE;
e2d0f980 5487 retval.is_longjmp = false;
628fe4e4 5488
0e30163f 5489 for (bs = bs_head; bs != NULL; bs = bs->next)
c906108c 5490 {
628fe4e4
JK
5491 /* Extract this BS's action. After processing each BS, we check
5492 if its action overrides all we've seem so far. */
5493 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
5494 enum bptype bptype;
5495
c906108c 5496 if (bs->breakpoint_at == NULL)
628fe4e4
JK
5497 {
5498 /* I suspect this can happen if it was a momentary
5499 breakpoint which has since been deleted. */
5500 bptype = bp_none;
5501 }
20874c92 5502 else
f431efe5 5503 bptype = bs->breakpoint_at->type;
628fe4e4
JK
5504
5505 switch (bptype)
c906108c
SS
5506 {
5507 case bp_none:
628fe4e4 5508 break;
c906108c
SS
5509 case bp_breakpoint:
5510 case bp_hardware_breakpoint:
7c16b83e 5511 case bp_single_step:
c906108c
SS
5512 case bp_until:
5513 case bp_finish:
a9b3a50f 5514 case bp_shlib_event:
c906108c
SS
5515 if (bs->stop)
5516 {
5517 if (bs->print)
628fe4e4 5518 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 5519 else
628fe4e4 5520 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
5521 }
5522 else
628fe4e4 5523 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5524 break;
5525 case bp_watchpoint:
5526 case bp_hardware_watchpoint:
5527 case bp_read_watchpoint:
5528 case bp_access_watchpoint:
5529 if (bs->stop)
5530 {
5531 if (bs->print)
628fe4e4 5532 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 5533 else
628fe4e4 5534 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
5535 }
5536 else
628fe4e4
JK
5537 {
5538 /* There was a watchpoint, but we're not stopping.
5539 This requires no further action. */
5540 }
c906108c
SS
5541 break;
5542 case bp_longjmp:
e2e4d78b 5543 case bp_longjmp_call_dummy:
186c406b 5544 case bp_exception:
0a39bb32
PA
5545 if (bs->stop)
5546 {
5547 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
5548 retval.is_longjmp = bptype != bp_exception;
5549 }
5550 else
5551 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5552 break;
5553 case bp_longjmp_resume:
186c406b 5554 case bp_exception_resume:
0a39bb32
PA
5555 if (bs->stop)
5556 {
5557 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
5558 retval.is_longjmp = bptype == bp_longjmp_resume;
5559 }
5560 else
5561 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5562 break;
5563 case bp_step_resume:
5564 if (bs->stop)
628fe4e4
JK
5565 this_action = BPSTAT_WHAT_STEP_RESUME;
5566 else
c906108c 5567 {
628fe4e4
JK
5568 /* It is for the wrong frame. */
5569 this_action = BPSTAT_WHAT_SINGLE;
c906108c 5570 }
c906108c 5571 break;
2c03e5be
PA
5572 case bp_hp_step_resume:
5573 if (bs->stop)
5574 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
5575 else
5576 {
5577 /* It is for the wrong frame. */
5578 this_action = BPSTAT_WHAT_SINGLE;
5579 }
5580 break;
c906108c 5581 case bp_watchpoint_scope:
c4093a6a 5582 case bp_thread_event:
1900040c 5583 case bp_overlay_event:
0fd8e87f 5584 case bp_longjmp_master:
aa7d318d 5585 case bp_std_terminate_master:
186c406b 5586 case bp_exception_master:
628fe4e4 5587 this_action = BPSTAT_WHAT_SINGLE;
c4093a6a 5588 break;
ce78b96d 5589 case bp_catchpoint:
c5aa993b
JM
5590 if (bs->stop)
5591 {
5592 if (bs->print)
628fe4e4 5593 this_action = BPSTAT_WHAT_STOP_NOISY;
c5aa993b 5594 else
628fe4e4 5595 this_action = BPSTAT_WHAT_STOP_SILENT;
c5aa993b
JM
5596 }
5597 else
628fe4e4 5598 {
cb1e4e32
PA
5599 /* Some catchpoints are implemented with breakpoints.
5600 For those, we need to step over the breakpoint. */
5601 if (bs->bp_location_at->loc_type != bp_loc_other)
5602 this_action = BPSTAT_WHAT_SINGLE;
628fe4e4
JK
5603 }
5604 break;
628fe4e4 5605 case bp_jit_event:
628fe4e4 5606 this_action = BPSTAT_WHAT_SINGLE;
c5aa993b 5607 break;
c906108c 5608 case bp_call_dummy:
53a5351d
JM
5609 /* Make sure the action is stop (silent or noisy),
5610 so infrun.c pops the dummy frame. */
aa7d318d 5611 retval.call_dummy = STOP_STACK_DUMMY;
628fe4e4 5612 this_action = BPSTAT_WHAT_STOP_SILENT;
aa7d318d
TT
5613 break;
5614 case bp_std_terminate:
5615 /* Make sure the action is stop (silent or noisy),
5616 so infrun.c pops the dummy frame. */
aa7d318d 5617 retval.call_dummy = STOP_STD_TERMINATE;
628fe4e4 5618 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c 5619 break;
1042e4c0 5620 case bp_tracepoint:
7a697b8d 5621 case bp_fast_tracepoint:
0fb4aa4b 5622 case bp_static_tracepoint:
1042e4c0
SS
5623 /* Tracepoint hits should not be reported back to GDB, and
5624 if one got through somehow, it should have been filtered
5625 out already. */
5626 internal_error (__FILE__, __LINE__,
7a697b8d 5627 _("bpstat_what: tracepoint encountered"));
0e30163f
JK
5628 break;
5629 case bp_gnu_ifunc_resolver:
5630 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
5631 this_action = BPSTAT_WHAT_SINGLE;
5632 break;
5633 case bp_gnu_ifunc_resolver_return:
5634 /* The breakpoint will be removed, execution will restart from the
5635 PC of the former breakpoint. */
5636 this_action = BPSTAT_WHAT_KEEP_CHECKING;
5637 break;
e7e0cddf
SS
5638
5639 case bp_dprintf:
a11cfd87
HZ
5640 if (bs->stop)
5641 this_action = BPSTAT_WHAT_STOP_SILENT;
5642 else
5643 this_action = BPSTAT_WHAT_SINGLE;
e7e0cddf
SS
5644 break;
5645
628fe4e4
JK
5646 default:
5647 internal_error (__FILE__, __LINE__,
5648 _("bpstat_what: unhandled bptype %d"), (int) bptype);
c906108c 5649 }
628fe4e4 5650
325fac50 5651 retval.main_action = std::max (retval.main_action, this_action);
c906108c 5652 }
628fe4e4 5653
243a9253
PA
5654 return retval;
5655}
628fe4e4 5656
243a9253
PA
5657void
5658bpstat_run_callbacks (bpstat bs_head)
5659{
5660 bpstat bs;
628fe4e4 5661
0e30163f
JK
5662 for (bs = bs_head; bs != NULL; bs = bs->next)
5663 {
5664 struct breakpoint *b = bs->breakpoint_at;
5665
5666 if (b == NULL)
5667 continue;
5668 switch (b->type)
5669 {
243a9253
PA
5670 case bp_jit_event:
5671 handle_jit_event ();
5672 break;
0e30163f
JK
5673 case bp_gnu_ifunc_resolver:
5674 gnu_ifunc_resolver_stop (b);
5675 break;
5676 case bp_gnu_ifunc_resolver_return:
5677 gnu_ifunc_resolver_return_stop (b);
5678 break;
5679 }
5680 }
c906108c
SS
5681}
5682
4c462cb0 5683/* See breakpoint.h. */
c906108c 5684
4c462cb0
SM
5685bool
5686bpstat_should_step ()
c906108c
SS
5687{
5688 struct breakpoint *b;
cc59ec59 5689
c906108c 5690 ALL_BREAKPOINTS (b)
717a8278 5691 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
4c462cb0
SM
5692 return true;
5693 return false;
c906108c
SS
5694}
5695
4c462cb0
SM
5696/* See breakpoint.h. */
5697
5698bool
67822962
PA
5699bpstat_causes_stop (bpstat bs)
5700{
5701 for (; bs != NULL; bs = bs->next)
5702 if (bs->stop)
4c462cb0 5703 return true;
67822962 5704
4c462cb0 5705 return false;
67822962
PA
5706}
5707
c906108c 5708\f
c5aa993b 5709
170b53b2
UW
5710/* Compute a string of spaces suitable to indent the next line
5711 so it starts at the position corresponding to the table column
5712 named COL_NAME in the currently active table of UIOUT. */
5713
5714static char *
5715wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
5716{
5717 static char wrap_indent[80];
5718 int i, total_width, width, align;
c5209615 5719 const char *text;
170b53b2
UW
5720
5721 total_width = 0;
112e8700 5722 for (i = 1; uiout->query_table_field (i, &width, &align, &text); i++)
170b53b2
UW
5723 {
5724 if (strcmp (text, col_name) == 0)
5725 {
5726 gdb_assert (total_width < sizeof wrap_indent);
5727 memset (wrap_indent, ' ', total_width);
5728 wrap_indent[total_width] = 0;
5729
5730 return wrap_indent;
5731 }
5732
5733 total_width += width + 1;
5734 }
5735
5736 return NULL;
5737}
5738
b775012e
LM
5739/* Determine if the locations of this breakpoint will have their conditions
5740 evaluated by the target, host or a mix of both. Returns the following:
5741
5742 "host": Host evals condition.
5743 "host or target": Host or Target evals condition.
5744 "target": Target evals condition.
5745*/
5746
5747static const char *
5748bp_condition_evaluator (struct breakpoint *b)
5749{
5750 struct bp_location *bl;
5751 char host_evals = 0;
5752 char target_evals = 0;
5753
5754 if (!b)
5755 return NULL;
5756
5757 if (!is_breakpoint (b))
5758 return NULL;
5759
5760 if (gdb_evaluates_breakpoint_condition_p ()
5761 || !target_supports_evaluation_of_breakpoint_conditions ())
5762 return condition_evaluation_host;
5763
5764 for (bl = b->loc; bl; bl = bl->next)
5765 {
5766 if (bl->cond_bytecode)
5767 target_evals++;
5768 else
5769 host_evals++;
5770 }
5771
5772 if (host_evals && target_evals)
5773 return condition_evaluation_both;
5774 else if (target_evals)
5775 return condition_evaluation_target;
5776 else
5777 return condition_evaluation_host;
5778}
5779
5780/* Determine the breakpoint location's condition evaluator. This is
5781 similar to bp_condition_evaluator, but for locations. */
5782
5783static const char *
5784bp_location_condition_evaluator (struct bp_location *bl)
5785{
5786 if (bl && !is_breakpoint (bl->owner))
5787 return NULL;
5788
5789 if (gdb_evaluates_breakpoint_condition_p ()
5790 || !target_supports_evaluation_of_breakpoint_conditions ())
5791 return condition_evaluation_host;
5792
5793 if (bl && bl->cond_bytecode)
5794 return condition_evaluation_target;
5795 else
5796 return condition_evaluation_host;
5797}
5798
859825b8
JK
5799/* Print the LOC location out of the list of B->LOC locations. */
5800
170b53b2
UW
5801static void
5802print_breakpoint_location (struct breakpoint *b,
5803 struct bp_location *loc)
0d381245 5804{
79a45e25 5805 struct ui_out *uiout = current_uiout;
5ed8105e
PA
5806
5807 scoped_restore_current_program_space restore_pspace;
6c95b8df 5808
859825b8
JK
5809 if (loc != NULL && loc->shlib_disabled)
5810 loc = NULL;
5811
6c95b8df
PA
5812 if (loc != NULL)
5813 set_current_program_space (loc->pspace);
5814
56435ebe 5815 if (b->display_canonical)
d28cd78a 5816 uiout->field_string ("what", event_location_to_string (b->location.get ()));
2f202fde 5817 else if (loc && loc->symtab)
0d381245 5818 {
4a27f119
KS
5819 const struct symbol *sym = loc->symbol;
5820
0d381245
VP
5821 if (sym)
5822 {
112e8700 5823 uiout->text ("in ");
987012b8 5824 uiout->field_string ("func", sym->print_name (),
e43b10e1 5825 function_name_style.style ());
112e8700
SM
5826 uiout->text (" ");
5827 uiout->wrap_hint (wrap_indent_at_field (uiout, "what"));
5828 uiout->text ("at ");
0d381245 5829 }
112e8700 5830 uiout->field_string ("file",
cbe56571 5831 symtab_to_filename_for_display (loc->symtab),
e43b10e1 5832 file_name_style.style ());
112e8700 5833 uiout->text (":");
05cba821 5834
112e8700
SM
5835 if (uiout->is_mi_like_p ())
5836 uiout->field_string ("fullname", symtab_to_fullname (loc->symtab));
0d381245 5837
381befee 5838 uiout->field_signed ("line", loc->line_number);
0d381245 5839 }
859825b8 5840 else if (loc)
0d381245 5841 {
d7e74731 5842 string_file stb;
170b53b2 5843
d7e74731 5844 print_address_symbolic (loc->gdbarch, loc->address, &stb,
22e722e1 5845 demangle, "");
112e8700 5846 uiout->field_stream ("at", stb);
0d381245 5847 }
859825b8 5848 else
f00aae0f 5849 {
d28cd78a
TT
5850 uiout->field_string ("pending",
5851 event_location_to_string (b->location.get ()));
f00aae0f
KS
5852 /* If extra_string is available, it could be holding a condition
5853 or dprintf arguments. In either case, make sure it is printed,
5854 too, but only for non-MI streams. */
112e8700 5855 if (!uiout->is_mi_like_p () && b->extra_string != NULL)
f00aae0f
KS
5856 {
5857 if (b->type == bp_dprintf)
112e8700 5858 uiout->text (",");
f00aae0f 5859 else
112e8700
SM
5860 uiout->text (" ");
5861 uiout->text (b->extra_string);
f00aae0f
KS
5862 }
5863 }
6c95b8df 5864
b775012e
LM
5865 if (loc && is_breakpoint (b)
5866 && breakpoint_condition_evaluation_mode () == condition_evaluation_target
5867 && bp_condition_evaluator (b) == condition_evaluation_both)
5868 {
112e8700
SM
5869 uiout->text (" (");
5870 uiout->field_string ("evaluated-by",
b775012e 5871 bp_location_condition_evaluator (loc));
112e8700 5872 uiout->text (")");
b775012e 5873 }
0d381245
VP
5874}
5875
269b11a2
PA
5876static const char *
5877bptype_string (enum bptype type)
c906108c 5878{
c4093a6a
JM
5879 struct ep_type_description
5880 {
5881 enum bptype type;
a121b7c1 5882 const char *description;
c4093a6a
JM
5883 };
5884 static struct ep_type_description bptypes[] =
c906108c 5885 {
c5aa993b
JM
5886 {bp_none, "?deleted?"},
5887 {bp_breakpoint, "breakpoint"},
c906108c 5888 {bp_hardware_breakpoint, "hw breakpoint"},
7c16b83e 5889 {bp_single_step, "sw single-step"},
c5aa993b
JM
5890 {bp_until, "until"},
5891 {bp_finish, "finish"},
5892 {bp_watchpoint, "watchpoint"},
c906108c 5893 {bp_hardware_watchpoint, "hw watchpoint"},
c5aa993b
JM
5894 {bp_read_watchpoint, "read watchpoint"},
5895 {bp_access_watchpoint, "acc watchpoint"},
5896 {bp_longjmp, "longjmp"},
5897 {bp_longjmp_resume, "longjmp resume"},
e2e4d78b 5898 {bp_longjmp_call_dummy, "longjmp for call dummy"},
186c406b
TT
5899 {bp_exception, "exception"},
5900 {bp_exception_resume, "exception resume"},
c5aa993b 5901 {bp_step_resume, "step resume"},
2c03e5be 5902 {bp_hp_step_resume, "high-priority step resume"},
c5aa993b
JM
5903 {bp_watchpoint_scope, "watchpoint scope"},
5904 {bp_call_dummy, "call dummy"},
aa7d318d 5905 {bp_std_terminate, "std::terminate"},
c5aa993b 5906 {bp_shlib_event, "shlib events"},
c4093a6a 5907 {bp_thread_event, "thread events"},
1900040c 5908 {bp_overlay_event, "overlay events"},
0fd8e87f 5909 {bp_longjmp_master, "longjmp master"},
aa7d318d 5910 {bp_std_terminate_master, "std::terminate master"},
186c406b 5911 {bp_exception_master, "exception master"},
ce78b96d 5912 {bp_catchpoint, "catchpoint"},
1042e4c0 5913 {bp_tracepoint, "tracepoint"},
7a697b8d 5914 {bp_fast_tracepoint, "fast tracepoint"},
0fb4aa4b 5915 {bp_static_tracepoint, "static tracepoint"},
e7e0cddf 5916 {bp_dprintf, "dprintf"},
4efc6507 5917 {bp_jit_event, "jit events"},
0e30163f
JK
5918 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
5919 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
c5aa993b 5920 };
269b11a2
PA
5921
5922 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
5923 || ((int) type != bptypes[(int) type].type))
5924 internal_error (__FILE__, __LINE__,
5925 _("bptypes table does not describe type #%d."),
5926 (int) type);
5927
5928 return bptypes[(int) type].description;
5929}
5930
998580f1
MK
5931/* For MI, output a field named 'thread-groups' with a list as the value.
5932 For CLI, prefix the list with the string 'inf'. */
5933
5934static void
5935output_thread_groups (struct ui_out *uiout,
5936 const char *field_name,
5c632425 5937 const std::vector<int> &inf_nums,
998580f1
MK
5938 int mi_only)
5939{
112e8700 5940 int is_mi = uiout->is_mi_like_p ();
998580f1
MK
5941
5942 /* For backward compatibility, don't display inferiors in CLI unless
5943 there are several. Always display them for MI. */
5944 if (!is_mi && mi_only)
5945 return;
5946
10f489e5 5947 ui_out_emit_list list_emitter (uiout, field_name);
752eb8b4 5948
5c632425 5949 for (size_t i = 0; i < inf_nums.size (); i++)
998580f1
MK
5950 {
5951 if (is_mi)
5952 {
5953 char mi_group[10];
5954
5c632425 5955 xsnprintf (mi_group, sizeof (mi_group), "i%d", inf_nums[i]);
112e8700 5956 uiout->field_string (NULL, mi_group);
998580f1
MK
5957 }
5958 else
5959 {
5960 if (i == 0)
112e8700 5961 uiout->text (" inf ");
998580f1 5962 else
112e8700 5963 uiout->text (", ");
998580f1 5964
5c632425 5965 uiout->text (plongest (inf_nums[i]));
998580f1
MK
5966 }
5967 }
998580f1
MK
5968}
5969
a38118e5
PA
5970/* Print B to gdb_stdout. If RAW_LOC, print raw breakpoint locations
5971 instead of going via breakpoint_ops::print_one. This makes "maint
5972 info breakpoints" show the software breakpoint locations of
5973 catchpoints, which are considered internal implementation
5974 detail. */
269b11a2
PA
5975
5976static void
5977print_one_breakpoint_location (struct breakpoint *b,
5978 struct bp_location *loc,
5979 int loc_number,
5980 struct bp_location **last_loc,
a38118e5 5981 int allflag, bool raw_loc)
269b11a2
PA
5982{
5983 struct command_line *l;
c2c6d25f 5984 static char bpenables[] = "nynny";
c906108c 5985
79a45e25 5986 struct ui_out *uiout = current_uiout;
0d381245
VP
5987 int header_of_multiple = 0;
5988 int part_of_multiple = (loc != NULL);
79a45b7d
TT
5989 struct value_print_options opts;
5990
5991 get_user_print_options (&opts);
0d381245
VP
5992
5993 gdb_assert (!loc || loc_number != 0);
4a64f543
MS
5994 /* See comment in print_one_breakpoint concerning treatment of
5995 breakpoints with single disabled location. */
0d381245
VP
5996 if (loc == NULL
5997 && (b->loc != NULL
5998 && (b->loc->next != NULL || !b->loc->enabled)))
5999 header_of_multiple = 1;
6000 if (loc == NULL)
6001 loc = b->loc;
6002
c4093a6a
JM
6003 annotate_record ();
6004
6005 /* 1 */
6006 annotate_field (0);
0d381245 6007 if (part_of_multiple)
528e1572 6008 uiout->field_fmt ("number", "%d.%d", b->number, loc_number);
0d381245 6009 else
381befee 6010 uiout->field_signed ("number", b->number);
c4093a6a
JM
6011
6012 /* 2 */
6013 annotate_field (1);
0d381245 6014 if (part_of_multiple)
112e8700 6015 uiout->field_skip ("type");
269b11a2 6016 else
112e8700 6017 uiout->field_string ("type", bptype_string (b->type));
c4093a6a
JM
6018
6019 /* 3 */
6020 annotate_field (2);
0d381245 6021 if (part_of_multiple)
112e8700 6022 uiout->field_skip ("disp");
0d381245 6023 else
112e8700 6024 uiout->field_string ("disp", bpdisp_text (b->disposition));
0d381245 6025
c4093a6a
JM
6026 /* 4 */
6027 annotate_field (3);
0d381245 6028 if (part_of_multiple)
112e8700 6029 uiout->field_string ("enabled", loc->enabled ? "y" : "n");
0d381245 6030 else
112e8700 6031 uiout->field_fmt ("enabled", "%c", bpenables[(int) b->enable_state]);
0d381245 6032
c4093a6a 6033 /* 5 and 6 */
a38118e5 6034 if (!raw_loc && b->ops != NULL && b->ops->print_one != NULL)
b58a68fe 6035 b->ops->print_one (b, last_loc);
3086aeae 6036 else
a38118e5
PA
6037 {
6038 if (is_watchpoint (b))
3a5c3e22
PA
6039 {
6040 struct watchpoint *w = (struct watchpoint *) b;
6041
6042 /* Field 4, the address, is omitted (which makes the columns
6043 not line up too nicely with the headers, but the effect
6044 is relatively readable). */
6045 if (opts.addressprint)
112e8700 6046 uiout->field_skip ("addr");
3a5c3e22 6047 annotate_field (5);
112e8700 6048 uiout->field_string ("what", w->exp_string);
3a5c3e22 6049 }
f06f1252
TT
6050 else if (!is_catchpoint (b) || is_exception_catchpoint (b)
6051 || is_ada_exception_catchpoint (b))
a38118e5
PA
6052 {
6053 if (opts.addressprint)
6054 {
6055 annotate_field (4);
6056 if (header_of_multiple)
7f6aba03
TT
6057 uiout->field_string ("addr", "<MULTIPLE>",
6058 metadata_style.style ());
a38118e5 6059 else if (b->loc == NULL || loc->shlib_disabled)
7f6aba03
TT
6060 uiout->field_string ("addr", "<PENDING>",
6061 metadata_style.style ());
a38118e5
PA
6062 else
6063 uiout->field_core_addr ("addr",
6064 loc->gdbarch, loc->address);
6065 }
6066 annotate_field (5);
6067 if (!header_of_multiple)
6068 print_breakpoint_location (b, loc);
6069 if (b->loc)
6070 *last_loc = b->loc;
6071 }
6072 }
6c95b8df 6073
998580f1 6074 if (loc != NULL && !header_of_multiple)
6c95b8df 6075 {
5c632425 6076 std::vector<int> inf_nums;
998580f1 6077 int mi_only = 1;
6c95b8df 6078
08036331 6079 for (inferior *inf : all_inferiors ())
6c95b8df
PA
6080 {
6081 if (inf->pspace == loc->pspace)
5c632425 6082 inf_nums.push_back (inf->num);
6c95b8df 6083 }
998580f1
MK
6084
6085 /* For backward compatibility, don't display inferiors in CLI unless
6086 there are several. Always display for MI. */
6087 if (allflag
6088 || (!gdbarch_has_global_breakpoints (target_gdbarch ())
6089 && (number_of_program_spaces () > 1
6090 || number_of_inferiors () > 1)
6091 /* LOC is for existing B, it cannot be in
6092 moribund_locations and thus having NULL OWNER. */
6093 && loc->owner->type != bp_catchpoint))
6094 mi_only = 0;
5c632425 6095 output_thread_groups (uiout, "thread-groups", inf_nums, mi_only);
6c95b8df
PA
6096 }
6097
4a306c9a 6098 if (!part_of_multiple)
c4093a6a 6099 {
4a306c9a
JB
6100 if (b->thread != -1)
6101 {
6102 /* FIXME: This seems to be redundant and lost here; see the
4a64f543 6103 "stop only in" line a little further down. */
112e8700 6104 uiout->text (" thread ");
381befee 6105 uiout->field_signed ("thread", b->thread);
4a306c9a
JB
6106 }
6107 else if (b->task != 0)
6108 {
112e8700 6109 uiout->text (" task ");
381befee 6110 uiout->field_signed ("task", b->task);
4a306c9a 6111 }
c4093a6a 6112 }
f1310107 6113
112e8700 6114 uiout->text ("\n");
f1310107 6115
348d480f 6116 if (!part_of_multiple)
f1310107
TJB
6117 b->ops->print_one_detail (b, uiout);
6118
0d381245 6119 if (part_of_multiple && frame_id_p (b->frame_id))
c4093a6a
JM
6120 {
6121 annotate_field (6);
112e8700 6122 uiout->text ("\tstop only in stack frame at ");
e5dd4106 6123 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
818dd999 6124 the frame ID. */
112e8700 6125 uiout->field_core_addr ("frame",
5af949e3 6126 b->gdbarch, b->frame_id.stack_addr);
112e8700 6127 uiout->text ("\n");
c4093a6a
JM
6128 }
6129
28010a5d 6130 if (!part_of_multiple && b->cond_string)
c4093a6a
JM
6131 {
6132 annotate_field (7);
d77f58be 6133 if (is_tracepoint (b))
112e8700 6134 uiout->text ("\ttrace only if ");
1042e4c0 6135 else
112e8700
SM
6136 uiout->text ("\tstop only if ");
6137 uiout->field_string ("cond", b->cond_string);
b775012e
LM
6138
6139 /* Print whether the target is doing the breakpoint's condition
6140 evaluation. If GDB is doing the evaluation, don't print anything. */
6141 if (is_breakpoint (b)
6142 && breakpoint_condition_evaluation_mode ()
6143 == condition_evaluation_target)
6144 {
6a831f06
PA
6145 uiout->message (" (%pF evals)",
6146 string_field ("evaluated-by",
6147 bp_condition_evaluator (b)));
b775012e 6148 }
112e8700 6149 uiout->text ("\n");
0101ce28
JJ
6150 }
6151
0d381245 6152 if (!part_of_multiple && b->thread != -1)
c4093a6a 6153 {
4a64f543 6154 /* FIXME should make an annotation for this. */
112e8700
SM
6155 uiout->text ("\tstop only in thread ");
6156 if (uiout->is_mi_like_p ())
381befee 6157 uiout->field_signed ("thread", b->thread);
5d5658a1
PA
6158 else
6159 {
6160 struct thread_info *thr = find_thread_global_id (b->thread);
6161
112e8700 6162 uiout->field_string ("thread", print_thread_id (thr));
5d5658a1 6163 }
112e8700 6164 uiout->text ("\n");
c4093a6a
JM
6165 }
6166
556ec64d
YQ
6167 if (!part_of_multiple)
6168 {
6169 if (b->hit_count)
31f56a27
YQ
6170 {
6171 /* FIXME should make an annotation for this. */
6172 if (is_catchpoint (b))
112e8700 6173 uiout->text ("\tcatchpoint");
31f56a27 6174 else if (is_tracepoint (b))
112e8700 6175 uiout->text ("\ttracepoint");
31f56a27 6176 else
112e8700
SM
6177 uiout->text ("\tbreakpoint");
6178 uiout->text (" already hit ");
381befee 6179 uiout->field_signed ("times", b->hit_count);
31f56a27 6180 if (b->hit_count == 1)
112e8700 6181 uiout->text (" time\n");
31f56a27 6182 else
112e8700 6183 uiout->text (" times\n");
31f56a27 6184 }
556ec64d
YQ
6185 else
6186 {
31f56a27 6187 /* Output the count also if it is zero, but only if this is mi. */
112e8700 6188 if (uiout->is_mi_like_p ())
381befee 6189 uiout->field_signed ("times", b->hit_count);
556ec64d
YQ
6190 }
6191 }
8b93c638 6192
0d381245 6193 if (!part_of_multiple && b->ignore_count)
c4093a6a
JM
6194 {
6195 annotate_field (8);
6a831f06
PA
6196 uiout->message ("\tignore next %pF hits\n",
6197 signed_field ("ignore", b->ignore_count));
c4093a6a 6198 }
059fb39f 6199
816338b5
SS
6200 /* Note that an enable count of 1 corresponds to "enable once"
6201 behavior, which is reported by the combination of enablement and
6202 disposition, so we don't need to mention it here. */
6203 if (!part_of_multiple && b->enable_count > 1)
6204 {
6205 annotate_field (8);
112e8700 6206 uiout->text ("\tdisable after ");
816338b5
SS
6207 /* Tweak the wording to clarify that ignore and enable counts
6208 are distinct, and have additive effect. */
6209 if (b->ignore_count)
112e8700 6210 uiout->text ("additional ");
816338b5 6211 else
112e8700 6212 uiout->text ("next ");
381befee 6213 uiout->field_signed ("enable", b->enable_count);
112e8700 6214 uiout->text (" hits\n");
816338b5
SS
6215 }
6216
f196051f
SS
6217 if (!part_of_multiple && is_tracepoint (b))
6218 {
6219 struct tracepoint *tp = (struct tracepoint *) b;
6220
6221 if (tp->traceframe_usage)
6222 {
112e8700 6223 uiout->text ("\ttrace buffer usage ");
381befee 6224 uiout->field_signed ("traceframe-usage", tp->traceframe_usage);
112e8700 6225 uiout->text (" bytes\n");
f196051f
SS
6226 }
6227 }
d3ce09f5 6228
d1b0a7bf 6229 l = b->commands ? b->commands.get () : NULL;
059fb39f 6230 if (!part_of_multiple && l)
c4093a6a
JM
6231 {
6232 annotate_field (9);
2e783024 6233 ui_out_emit_tuple tuple_emitter (uiout, "script");
8b93c638 6234 print_command_lines (uiout, l, 4);
c4093a6a 6235 }
d24317b4 6236
d9b3f62e 6237 if (is_tracepoint (b))
1042e4c0 6238 {
d9b3f62e
PA
6239 struct tracepoint *t = (struct tracepoint *) b;
6240
6241 if (!part_of_multiple && t->pass_count)
6242 {
6243 annotate_field (10);
112e8700 6244 uiout->text ("\tpass count ");
381befee 6245 uiout->field_signed ("pass", t->pass_count);
112e8700 6246 uiout->text (" \n");
d9b3f62e 6247 }
f2a8bc8a
YQ
6248
6249 /* Don't display it when tracepoint or tracepoint location is
6250 pending. */
6251 if (!header_of_multiple && loc != NULL && !loc->shlib_disabled)
6252 {
6253 annotate_field (11);
6254
112e8700
SM
6255 if (uiout->is_mi_like_p ())
6256 uiout->field_string ("installed",
f2a8bc8a
YQ
6257 loc->inserted ? "y" : "n");
6258 else
6259 {
6260 if (loc->inserted)
112e8700 6261 uiout->text ("\t");
f2a8bc8a 6262 else
112e8700
SM
6263 uiout->text ("\tnot ");
6264 uiout->text ("installed on target\n");
f2a8bc8a
YQ
6265 }
6266 }
1042e4c0
SS
6267 }
6268
112e8700 6269 if (uiout->is_mi_like_p () && !part_of_multiple)
d24317b4 6270 {
3a5c3e22
PA
6271 if (is_watchpoint (b))
6272 {
6273 struct watchpoint *w = (struct watchpoint *) b;
6274
112e8700 6275 uiout->field_string ("original-location", w->exp_string);
3a5c3e22 6276 }
f00aae0f 6277 else if (b->location != NULL
d28cd78a 6278 && event_location_to_string (b->location.get ()) != NULL)
112e8700 6279 uiout->field_string ("original-location",
d28cd78a 6280 event_location_to_string (b->location.get ()));
d24317b4 6281 }
c4093a6a 6282}
c5aa993b 6283
13674803
SM
6284/* See breakpoint.h. */
6285
6286bool fix_multi_location_breakpoint_output_globally = false;
6287
0d381245
VP
6288static void
6289print_one_breakpoint (struct breakpoint *b,
4a64f543 6290 struct bp_location **last_loc,
6c95b8df 6291 int allflag)
0d381245 6292{
79a45e25 6293 struct ui_out *uiout = current_uiout;
13674803
SM
6294 bool use_fixed_output
6295 = (uiout->test_flags (fix_multi_location_breakpoint_output)
6296 || fix_multi_location_breakpoint_output_globally);
8d3788bd 6297
b4be1b06 6298 gdb::optional<ui_out_emit_tuple> bkpt_tuple_emitter (gdb::in_place, uiout, "bkpt");
a38118e5 6299 print_one_breakpoint_location (b, NULL, 0, last_loc, allflag, false);
8d3788bd 6300
b4be1b06
SM
6301 /* The mi2 broken format: the main breakpoint tuple ends here, the locations
6302 are outside. */
6303 if (!use_fixed_output)
6304 bkpt_tuple_emitter.reset ();
0d381245
VP
6305
6306 /* If this breakpoint has custom print function,
6307 it's already printed. Otherwise, print individual
6308 locations, if any. */
a38118e5
PA
6309 if (b->ops == NULL
6310 || b->ops->print_one == NULL
6311 || allflag)
0d381245 6312 {
4a64f543
MS
6313 /* If breakpoint has a single location that is disabled, we
6314 print it as if it had several locations, since otherwise it's
6315 hard to represent "breakpoint enabled, location disabled"
6316 situation.
6317
6318 Note that while hardware watchpoints have several locations
a38118e5
PA
6319 internally, that's not a property exposed to users.
6320
6321 Likewise, while catchpoints may be implemented with
6322 breakpoints (e.g., catch throw), that's not a property
6323 exposed to users. We do however display the internal
6324 breakpoint locations with "maint info breakpoints". */
6325 if (!is_hardware_watchpoint (b)
f06f1252
TT
6326 && (!is_catchpoint (b) || is_exception_catchpoint (b)
6327 || is_ada_exception_catchpoint (b))
a38118e5
PA
6328 && (allflag
6329 || (b->loc && (b->loc->next || !b->loc->enabled))))
0d381245 6330 {
b4be1b06
SM
6331 gdb::optional<ui_out_emit_list> locations_list;
6332
6333 /* For MI version <= 2, keep the behavior where GDB outputs an invalid
6334 MI record. For later versions, place breakpoint locations in a
6335 list. */
6336 if (uiout->is_mi_like_p () && use_fixed_output)
6337 locations_list.emplace (uiout, "locations");
8d3788bd 6338
b4be1b06
SM
6339 int n = 1;
6340 for (bp_location *loc = b->loc; loc != NULL; loc = loc->next, ++n)
8d3788bd 6341 {
b4be1b06 6342 ui_out_emit_tuple loc_tuple_emitter (uiout, NULL);
a38118e5
PA
6343 print_one_breakpoint_location (b, loc, n, last_loc,
6344 allflag, allflag);
8d3788bd 6345 }
0d381245
VP
6346 }
6347 }
6348}
6349
a6d9a66e
UW
6350static int
6351breakpoint_address_bits (struct breakpoint *b)
6352{
6353 int print_address_bits = 0;
6354 struct bp_location *loc;
6355
c6d81124
PA
6356 /* Software watchpoints that aren't watching memory don't have an
6357 address to print. */
6358 if (is_no_memory_software_watchpoint (b))
6359 return 0;
6360
a6d9a66e
UW
6361 for (loc = b->loc; loc; loc = loc->next)
6362 {
c7437ca6
PA
6363 int addr_bit;
6364
c7437ca6 6365 addr_bit = gdbarch_addr_bit (loc->gdbarch);
a6d9a66e
UW
6366 if (addr_bit > print_address_bits)
6367 print_address_bits = addr_bit;
6368 }
6369
6370 return print_address_bits;
6371}
0d381245 6372
65630365 6373/* See breakpoint.h. */
c5aa993b 6374
65630365
PA
6375void
6376print_breakpoint (breakpoint *b)
c4093a6a 6377{
a6d9a66e 6378 struct bp_location *dummy_loc = NULL;
65630365 6379 print_one_breakpoint (b, &dummy_loc, 0);
c4093a6a 6380}
c5aa993b 6381
09d682a4
TT
6382/* Return true if this breakpoint was set by the user, false if it is
6383 internal or momentary. */
6384
6385int
6386user_breakpoint_p (struct breakpoint *b)
6387{
46c6471b 6388 return b->number > 0;
09d682a4
TT
6389}
6390
93daf339
TT
6391/* See breakpoint.h. */
6392
6393int
6394pending_breakpoint_p (struct breakpoint *b)
6395{
6396 return b->loc == NULL;
6397}
6398
5c458ae8
SM
6399/* Print information on breakpoints (including watchpoints and tracepoints).
6400
6401 If non-NULL, BP_NUM_LIST is a list of numbers and number ranges as
6402 understood by number_or_range_parser. Only breakpoints included in this
6403 list are then printed.
6404
6405 If SHOW_INTERNAL is true, print internal breakpoints.
6406
6407 If FILTER is non-NULL, call it on each breakpoint and only include the
6408 ones for which it returns true.
6409
6410 Return the total number of breakpoints listed. */
c906108c 6411
d77f58be 6412static int
5c458ae8 6413breakpoint_1 (const char *bp_num_list, bool show_internal,
f2478a7e 6414 bool (*filter) (const struct breakpoint *))
c4093a6a 6415{
52f0bd74 6416 struct breakpoint *b;
a6d9a66e 6417 struct bp_location *last_loc = NULL;
7f3b0473 6418 int nr_printable_breakpoints;
79a45b7d 6419 struct value_print_options opts;
a6d9a66e 6420 int print_address_bits = 0;
269b11a2 6421 int print_type_col_width = 14;
79a45e25 6422 struct ui_out *uiout = current_uiout;
269b11a2 6423
79a45b7d
TT
6424 get_user_print_options (&opts);
6425
4a64f543
MS
6426 /* Compute the number of rows in the table, as well as the size
6427 required for address fields. */
7f3b0473
AC
6428 nr_printable_breakpoints = 0;
6429 ALL_BREAKPOINTS (b)
e5a67952
MS
6430 {
6431 /* If we have a filter, only list the breakpoints it accepts. */
6432 if (filter && !filter (b))
6433 continue;
6434
5c458ae8 6435 /* If we have a BP_NUM_LIST string, it is a list of breakpoints to
e5a67952 6436 accept. Skip the others. */
5c458ae8 6437 if (bp_num_list != NULL && *bp_num_list != '\0')
e5a67952 6438 {
5c458ae8 6439 if (show_internal && parse_and_eval_long (bp_num_list) != b->number)
e5a67952 6440 continue;
5c458ae8 6441 if (!show_internal && !number_is_in_list (bp_num_list, b->number))
e5a67952
MS
6442 continue;
6443 }
269b11a2 6444
5c458ae8 6445 if (show_internal || user_breakpoint_p (b))
e5a67952
MS
6446 {
6447 int addr_bit, type_len;
a6d9a66e 6448
e5a67952
MS
6449 addr_bit = breakpoint_address_bits (b);
6450 if (addr_bit > print_address_bits)
6451 print_address_bits = addr_bit;
269b11a2 6452
e5a67952
MS
6453 type_len = strlen (bptype_string (b->type));
6454 if (type_len > print_type_col_width)
6455 print_type_col_width = type_len;
6456
6457 nr_printable_breakpoints++;
6458 }
6459 }
7f3b0473 6460
4a2b031d
TT
6461 {
6462 ui_out_emit_table table_emitter (uiout,
6463 opts.addressprint ? 6 : 5,
6464 nr_printable_breakpoints,
6465 "BreakpointTable");
6466
6467 if (nr_printable_breakpoints > 0)
6468 annotate_breakpoints_headers ();
6469 if (nr_printable_breakpoints > 0)
6470 annotate_field (0);
6471 uiout->table_header (7, ui_left, "number", "Num"); /* 1 */
6472 if (nr_printable_breakpoints > 0)
6473 annotate_field (1);
6474 uiout->table_header (print_type_col_width, ui_left, "type", "Type"); /* 2 */
6475 if (nr_printable_breakpoints > 0)
6476 annotate_field (2);
6477 uiout->table_header (4, ui_left, "disp", "Disp"); /* 3 */
6478 if (nr_printable_breakpoints > 0)
6479 annotate_field (3);
6480 uiout->table_header (3, ui_left, "enabled", "Enb"); /* 4 */
6481 if (opts.addressprint)
6482 {
6483 if (nr_printable_breakpoints > 0)
6484 annotate_field (4);
6485 if (print_address_bits <= 32)
6486 uiout->table_header (10, ui_left, "addr", "Address"); /* 5 */
6487 else
6488 uiout->table_header (18, ui_left, "addr", "Address"); /* 5 */
6489 }
6490 if (nr_printable_breakpoints > 0)
6491 annotate_field (5);
6492 uiout->table_header (40, ui_noalign, "what", "What"); /* 6 */
6493 uiout->table_body ();
6494 if (nr_printable_breakpoints > 0)
6495 annotate_breakpoints_table ();
6496
6497 ALL_BREAKPOINTS (b)
6498 {
6499 QUIT;
6500 /* If we have a filter, only list the breakpoints it accepts. */
6501 if (filter && !filter (b))
6502 continue;
e5a67952 6503
5c458ae8 6504 /* If we have a BP_NUM_LIST string, it is a list of breakpoints to
4a2b031d 6505 accept. Skip the others. */
e5a67952 6506
5c458ae8 6507 if (bp_num_list != NULL && *bp_num_list != '\0')
4a2b031d 6508 {
5c458ae8 6509 if (show_internal) /* maintenance info breakpoint */
4a2b031d 6510 {
5c458ae8 6511 if (parse_and_eval_long (bp_num_list) != b->number)
4a2b031d
TT
6512 continue;
6513 }
6514 else /* all others */
6515 {
5c458ae8 6516 if (!number_is_in_list (bp_num_list, b->number))
4a2b031d
TT
6517 continue;
6518 }
6519 }
6520 /* We only print out user settable breakpoints unless the
5c458ae8
SM
6521 show_internal is set. */
6522 if (show_internal || user_breakpoint_p (b))
6523 print_one_breakpoint (b, &last_loc, show_internal);
4a2b031d
TT
6524 }
6525 }
698384cd 6526
7f3b0473 6527 if (nr_printable_breakpoints == 0)
c906108c 6528 {
4a64f543
MS
6529 /* If there's a filter, let the caller decide how to report
6530 empty list. */
d77f58be
SS
6531 if (!filter)
6532 {
5c458ae8 6533 if (bp_num_list == NULL || *bp_num_list == '\0')
112e8700 6534 uiout->message ("No breakpoints or watchpoints.\n");
d77f58be 6535 else
112e8700 6536 uiout->message ("No breakpoint or watchpoint matching '%s'.\n",
5c458ae8 6537 bp_num_list);
d77f58be 6538 }
c906108c
SS
6539 }
6540 else
c4093a6a 6541 {
a6d9a66e
UW
6542 if (last_loc && !server_command)
6543 set_next_address (last_loc->gdbarch, last_loc->address);
c4093a6a 6544 }
c906108c 6545
4a64f543 6546 /* FIXME? Should this be moved up so that it is only called when
c4093a6a 6547 there have been breakpoints? */
c906108c 6548 annotate_breakpoints_table_end ();
d77f58be
SS
6549
6550 return nr_printable_breakpoints;
c906108c
SS
6551}
6552
ad443146
SS
6553/* Display the value of default-collect in a way that is generally
6554 compatible with the breakpoint list. */
6555
6556static void
6557default_collect_info (void)
6558{
79a45e25
PA
6559 struct ui_out *uiout = current_uiout;
6560
ad443146
SS
6561 /* If it has no value (which is frequently the case), say nothing; a
6562 message like "No default-collect." gets in user's face when it's
6563 not wanted. */
6564 if (!*default_collect)
6565 return;
6566
6567 /* The following phrase lines up nicely with per-tracepoint collect
6568 actions. */
112e8700
SM
6569 uiout->text ("default collect ");
6570 uiout->field_string ("default-collect", default_collect);
6571 uiout->text (" \n");
ad443146
SS
6572}
6573
c906108c 6574static void
0b39b52e 6575info_breakpoints_command (const char *args, int from_tty)
c906108c 6576{
5c458ae8 6577 breakpoint_1 (args, false, NULL);
ad443146
SS
6578
6579 default_collect_info ();
d77f58be
SS
6580}
6581
6582static void
1d12d88f 6583info_watchpoints_command (const char *args, int from_tty)
d77f58be 6584{
5c458ae8 6585 int num_printed = breakpoint_1 (args, false, is_watchpoint);
79a45e25 6586 struct ui_out *uiout = current_uiout;
d77f58be
SS
6587
6588 if (num_printed == 0)
6589 {
e5a67952 6590 if (args == NULL || *args == '\0')
112e8700 6591 uiout->message ("No watchpoints.\n");
d77f58be 6592 else
112e8700 6593 uiout->message ("No watchpoint matching '%s'.\n", args);
d77f58be 6594 }
c906108c
SS
6595}
6596
7a292a7a 6597static void
4495129a 6598maintenance_info_breakpoints (const char *args, int from_tty)
c906108c 6599{
5c458ae8 6600 breakpoint_1 (args, true, NULL);
ad443146
SS
6601
6602 default_collect_info ();
c906108c
SS
6603}
6604
0d381245 6605static int
714835d5 6606breakpoint_has_pc (struct breakpoint *b,
6c95b8df 6607 struct program_space *pspace,
714835d5 6608 CORE_ADDR pc, struct obj_section *section)
0d381245
VP
6609{
6610 struct bp_location *bl = b->loc;
cc59ec59 6611
0d381245
VP
6612 for (; bl; bl = bl->next)
6613 {
6c95b8df
PA
6614 if (bl->pspace == pspace
6615 && bl->address == pc
0d381245
VP
6616 && (!overlay_debugging || bl->section == section))
6617 return 1;
6618 }
6619 return 0;
6620}
6621
672f9b60 6622/* Print a message describing any user-breakpoints set at PC. This
6c95b8df
PA
6623 concerns with logical breakpoints, so we match program spaces, not
6624 address spaces. */
c906108c
SS
6625
6626static void
6c95b8df
PA
6627describe_other_breakpoints (struct gdbarch *gdbarch,
6628 struct program_space *pspace, CORE_ADDR pc,
5af949e3 6629 struct obj_section *section, int thread)
c906108c 6630{
52f0bd74
AC
6631 int others = 0;
6632 struct breakpoint *b;
c906108c
SS
6633
6634 ALL_BREAKPOINTS (b)
672f9b60
KP
6635 others += (user_breakpoint_p (b)
6636 && breakpoint_has_pc (b, pspace, pc, section));
c906108c
SS
6637 if (others > 0)
6638 {
a3f17187
AC
6639 if (others == 1)
6640 printf_filtered (_("Note: breakpoint "));
6641 else /* if (others == ???) */
6642 printf_filtered (_("Note: breakpoints "));
c906108c 6643 ALL_BREAKPOINTS (b)
672f9b60 6644 if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
0d381245
VP
6645 {
6646 others--;
6647 printf_filtered ("%d", b->number);
6648 if (b->thread == -1 && thread != -1)
6649 printf_filtered (" (all threads)");
6650 else if (b->thread != -1)
6651 printf_filtered (" (thread %d)", b->thread);
6652 printf_filtered ("%s%s ",
059fb39f 6653 ((b->enable_state == bp_disabled
f8eba3c6 6654 || b->enable_state == bp_call_disabled)
0d381245 6655 ? " (disabled)"
0d381245
VP
6656 : ""),
6657 (others > 1) ? ","
6658 : ((others == 1) ? " and" : ""));
6659 }
6a831f06
PA
6660 current_uiout->message (_("also set at pc %ps.\n"),
6661 styled_string (address_style.style (),
6662 paddress (gdbarch, pc)));
c906108c
SS
6663 }
6664}
6665\f
c906108c 6666
cb1e4e32
PA
6667/* Return true iff it is meaningful to use the address member of LOC.
6668 For some breakpoint types, the locations' address members are
6669 irrelevant and it makes no sense to attempt to compare them to
6670 other addresses (or use them for any other purpose either).
2d134ed3 6671
cb1e4e32
PA
6672 More specifically, software watchpoints and catchpoints that are
6673 not backed by breakpoints always have a zero valued location
6674 address and we don't want to mark breakpoints of any of these types
6675 to be a duplicate of an actual breakpoint location at address
6676 zero. */
e4f237da 6677
cb1e4e32
PA
6678static bool
6679bl_address_is_meaningful (bp_location *loc)
e4f237da 6680{
cb1e4e32 6681 return loc->loc_type != bp_loc_other;
2d134ed3
PA
6682}
6683
6684/* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
6685 true if LOC1 and LOC2 represent the same watchpoint location. */
6686
6687static int
4a64f543
MS
6688watchpoint_locations_match (struct bp_location *loc1,
6689 struct bp_location *loc2)
2d134ed3 6690{
3a5c3e22
PA
6691 struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
6692 struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
6693
6694 /* Both of them must exist. */
6695 gdb_assert (w1 != NULL);
6696 gdb_assert (w2 != NULL);
2bdf28a0 6697
4a64f543
MS
6698 /* If the target can evaluate the condition expression in hardware,
6699 then we we need to insert both watchpoints even if they are at
6700 the same place. Otherwise the watchpoint will only trigger when
6701 the condition of whichever watchpoint was inserted evaluates to
6702 true, not giving a chance for GDB to check the condition of the
6703 other watchpoint. */
3a5c3e22 6704 if ((w1->cond_exp
4a64f543
MS
6705 && target_can_accel_watchpoint_condition (loc1->address,
6706 loc1->length,
0cf6dd15 6707 loc1->watchpoint_type,
4d01a485 6708 w1->cond_exp.get ()))
3a5c3e22 6709 || (w2->cond_exp
4a64f543
MS
6710 && target_can_accel_watchpoint_condition (loc2->address,
6711 loc2->length,
0cf6dd15 6712 loc2->watchpoint_type,
4d01a485 6713 w2->cond_exp.get ())))
0cf6dd15
TJB
6714 return 0;
6715
85d721b8
PA
6716 /* Note that this checks the owner's type, not the location's. In
6717 case the target does not support read watchpoints, but does
6718 support access watchpoints, we'll have bp_read_watchpoint
6719 watchpoints with hw_access locations. Those should be considered
6720 duplicates of hw_read locations. The hw_read locations will
6721 become hw_access locations later. */
2d134ed3
PA
6722 return (loc1->owner->type == loc2->owner->type
6723 && loc1->pspace->aspace == loc2->pspace->aspace
6724 && loc1->address == loc2->address
6725 && loc1->length == loc2->length);
e4f237da
KB
6726}
6727
31e77af2 6728/* See breakpoint.h. */
6c95b8df 6729
31e77af2 6730int
accd0bcd
YQ
6731breakpoint_address_match (const address_space *aspace1, CORE_ADDR addr1,
6732 const address_space *aspace2, CORE_ADDR addr2)
6c95b8df 6733{
f5656ead 6734 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
6c95b8df
PA
6735 || aspace1 == aspace2)
6736 && addr1 == addr2);
6737}
6738
f1310107
TJB
6739/* Returns true if {ASPACE2,ADDR2} falls within the range determined by
6740 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
6741 matches ASPACE2. On targets that have global breakpoints, the address
6742 space doesn't really matter. */
6743
6744static int
accd0bcd
YQ
6745breakpoint_address_match_range (const address_space *aspace1,
6746 CORE_ADDR addr1,
6747 int len1, const address_space *aspace2,
f1310107
TJB
6748 CORE_ADDR addr2)
6749{
f5656ead 6750 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
f1310107
TJB
6751 || aspace1 == aspace2)
6752 && addr2 >= addr1 && addr2 < addr1 + len1);
6753}
6754
6755/* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
6756 a ranged breakpoint. In most targets, a match happens only if ASPACE
6757 matches the breakpoint's address space. On targets that have global
6758 breakpoints, the address space doesn't really matter. */
6759
6760static int
6761breakpoint_location_address_match (struct bp_location *bl,
accd0bcd 6762 const address_space *aspace,
f1310107
TJB
6763 CORE_ADDR addr)
6764{
6765 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
6766 aspace, addr)
6767 || (bl->length
6768 && breakpoint_address_match_range (bl->pspace->aspace,
6769 bl->address, bl->length,
6770 aspace, addr)));
6771}
6772
d35ae833
PA
6773/* Returns true if the [ADDR,ADDR+LEN) range in ASPACE overlaps
6774 breakpoint BL. BL may be a ranged breakpoint. In most targets, a
6775 match happens only if ASPACE matches the breakpoint's address
6776 space. On targets that have global breakpoints, the address space
6777 doesn't really matter. */
6778
6779static int
6780breakpoint_location_address_range_overlap (struct bp_location *bl,
accd0bcd 6781 const address_space *aspace,
d35ae833
PA
6782 CORE_ADDR addr, int len)
6783{
6784 if (gdbarch_has_global_breakpoints (target_gdbarch ())
6785 || bl->pspace->aspace == aspace)
6786 {
6787 int bl_len = bl->length != 0 ? bl->length : 1;
6788
6789 if (mem_ranges_overlap (addr, len, bl->address, bl_len))
6790 return 1;
6791 }
6792 return 0;
6793}
6794
1e4d1764
YQ
6795/* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
6796 Then, if LOC1 and LOC2 represent the same tracepoint location, returns
6797 true, otherwise returns false. */
6798
6799static int
6800tracepoint_locations_match (struct bp_location *loc1,
6801 struct bp_location *loc2)
6802{
6803 if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
6804 /* Since tracepoint locations are never duplicated with others', tracepoint
6805 locations at the same address of different tracepoints are regarded as
6806 different locations. */
6807 return (loc1->address == loc2->address && loc1->owner == loc2->owner);
6808 else
6809 return 0;
6810}
6811
2d134ed3 6812/* Assuming LOC1 and LOC2's types' have meaningful target addresses
cb1e4e32
PA
6813 (bl_address_is_meaningful), returns true if LOC1 and LOC2 represent
6814 the same location. */
2d134ed3
PA
6815
6816static int
4a64f543
MS
6817breakpoint_locations_match (struct bp_location *loc1,
6818 struct bp_location *loc2)
2d134ed3 6819{
2bdf28a0
JK
6820 int hw_point1, hw_point2;
6821
6822 /* Both of them must not be in moribund_locations. */
6823 gdb_assert (loc1->owner != NULL);
6824 gdb_assert (loc2->owner != NULL);
6825
6826 hw_point1 = is_hardware_watchpoint (loc1->owner);
6827 hw_point2 = is_hardware_watchpoint (loc2->owner);
2d134ed3
PA
6828
6829 if (hw_point1 != hw_point2)
6830 return 0;
6831 else if (hw_point1)
6832 return watchpoint_locations_match (loc1, loc2);
1e4d1764
YQ
6833 else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
6834 return tracepoint_locations_match (loc1, loc2);
2d134ed3 6835 else
f1310107
TJB
6836 /* We compare bp_location.length in order to cover ranged breakpoints. */
6837 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
6838 loc2->pspace->aspace, loc2->address)
6839 && loc1->length == loc2->length);
2d134ed3
PA
6840}
6841
76897487
KB
6842static void
6843breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
6844 int bnum, int have_bnum)
6845{
f63fbe86
MS
6846 /* The longest string possibly returned by hex_string_custom
6847 is 50 chars. These must be at least that big for safety. */
6848 char astr1[64];
6849 char astr2[64];
76897487 6850
bb599908
PH
6851 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
6852 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
76897487 6853 if (have_bnum)
8a3fe4f8 6854 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
76897487
KB
6855 bnum, astr1, astr2);
6856 else
8a3fe4f8 6857 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
76897487
KB
6858}
6859
4a64f543
MS
6860/* Adjust a breakpoint's address to account for architectural
6861 constraints on breakpoint placement. Return the adjusted address.
6862 Note: Very few targets require this kind of adjustment. For most
6863 targets, this function is simply the identity function. */
76897487
KB
6864
6865static CORE_ADDR
a6d9a66e
UW
6866adjust_breakpoint_address (struct gdbarch *gdbarch,
6867 CORE_ADDR bpaddr, enum bptype bptype)
76897487 6868{
a0de8c21
YQ
6869 if (bptype == bp_watchpoint
6870 || bptype == bp_hardware_watchpoint
6871 || bptype == bp_read_watchpoint
6872 || bptype == bp_access_watchpoint
6873 || bptype == bp_catchpoint)
88f7da05
KB
6874 {
6875 /* Watchpoints and the various bp_catch_* eventpoints should not
6876 have their addresses modified. */
6877 return bpaddr;
6878 }
7c16b83e
PA
6879 else if (bptype == bp_single_step)
6880 {
6881 /* Single-step breakpoints should not have their addresses
6882 modified. If there's any architectural constrain that
6883 applies to this address, then it should have already been
6884 taken into account when the breakpoint was created in the
6885 first place. If we didn't do this, stepping through e.g.,
6886 Thumb-2 IT blocks would break. */
6887 return bpaddr;
6888 }
76897487
KB
6889 else
6890 {
a0de8c21
YQ
6891 CORE_ADDR adjusted_bpaddr = bpaddr;
6892
6893 if (gdbarch_adjust_breakpoint_address_p (gdbarch))
6894 {
6895 /* Some targets have architectural constraints on the placement
6896 of breakpoint instructions. Obtain the adjusted address. */
6897 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
6898 }
76897487 6899
a0de8c21 6900 adjusted_bpaddr = address_significant (gdbarch, adjusted_bpaddr);
76897487
KB
6901
6902 /* An adjusted breakpoint address can significantly alter
6903 a user's expectations. Print a warning if an adjustment
6904 is required. */
6905 if (adjusted_bpaddr != bpaddr)
6906 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
6907
6908 return adjusted_bpaddr;
6909 }
6910}
6911
cb1e4e32
PA
6912static bp_loc_type
6913bp_location_from_bp_type (bptype type)
7cc221ef 6914{
cb1e4e32 6915 switch (type)
e049a4b5
DJ
6916 {
6917 case bp_breakpoint:
7c16b83e 6918 case bp_single_step:
e049a4b5
DJ
6919 case bp_until:
6920 case bp_finish:
6921 case bp_longjmp:
6922 case bp_longjmp_resume:
e2e4d78b 6923 case bp_longjmp_call_dummy:
186c406b
TT
6924 case bp_exception:
6925 case bp_exception_resume:
e049a4b5 6926 case bp_step_resume:
2c03e5be 6927 case bp_hp_step_resume:
e049a4b5
DJ
6928 case bp_watchpoint_scope:
6929 case bp_call_dummy:
aa7d318d 6930 case bp_std_terminate:
e049a4b5
DJ
6931 case bp_shlib_event:
6932 case bp_thread_event:
6933 case bp_overlay_event:
4efc6507 6934 case bp_jit_event:
0fd8e87f 6935 case bp_longjmp_master:
aa7d318d 6936 case bp_std_terminate_master:
186c406b 6937 case bp_exception_master:
0e30163f
JK
6938 case bp_gnu_ifunc_resolver:
6939 case bp_gnu_ifunc_resolver_return:
e7e0cddf 6940 case bp_dprintf:
cb1e4e32 6941 return bp_loc_software_breakpoint;
e049a4b5 6942 case bp_hardware_breakpoint:
cb1e4e32 6943 return bp_loc_hardware_breakpoint;
e049a4b5
DJ
6944 case bp_hardware_watchpoint:
6945 case bp_read_watchpoint:
6946 case bp_access_watchpoint:
cb1e4e32 6947 return bp_loc_hardware_watchpoint;
e049a4b5 6948 case bp_watchpoint:
ce78b96d 6949 case bp_catchpoint:
15c3d785
PA
6950 case bp_tracepoint:
6951 case bp_fast_tracepoint:
0fb4aa4b 6952 case bp_static_tracepoint:
cb1e4e32 6953 return bp_loc_other;
e049a4b5 6954 default:
e2e0b3e5 6955 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
e049a4b5 6956 }
cb1e4e32
PA
6957}
6958
6959bp_location::bp_location (breakpoint *owner, bp_loc_type type)
6960{
6961 this->owner = owner;
6962 this->cond_bytecode = NULL;
6963 this->shlib_disabled = 0;
6964 this->enabled = 1;
6965
6966 this->loc_type = type;
e049a4b5 6967
cb1e4e32
PA
6968 if (this->loc_type == bp_loc_software_breakpoint
6969 || this->loc_type == bp_loc_hardware_breakpoint)
6970 mark_breakpoint_location_modified (this);
6971
6972 this->refc = 1;
6973}
6974
6975bp_location::bp_location (breakpoint *owner)
6976 : bp_location::bp_location (owner,
6977 bp_location_from_bp_type (owner->type))
6978{
28010a5d
PA
6979}
6980
6981/* Allocate a struct bp_location. */
6982
6983static struct bp_location *
6984allocate_bp_location (struct breakpoint *bpt)
6985{
348d480f
PA
6986 return bpt->ops->allocate_location (bpt);
6987}
7cc221ef 6988
f431efe5
PA
6989static void
6990free_bp_location (struct bp_location *loc)
fe3f5fa8 6991{
4d01a485 6992 delete loc;
fe3f5fa8
VP
6993}
6994
f431efe5
PA
6995/* Increment reference count. */
6996
6997static void
6998incref_bp_location (struct bp_location *bl)
6999{
7000 ++bl->refc;
7001}
7002
7003/* Decrement reference count. If the reference count reaches 0,
7004 destroy the bp_location. Sets *BLP to NULL. */
7005
7006static void
7007decref_bp_location (struct bp_location **blp)
7008{
0807b50c
PA
7009 gdb_assert ((*blp)->refc > 0);
7010
f431efe5
PA
7011 if (--(*blp)->refc == 0)
7012 free_bp_location (*blp);
7013 *blp = NULL;
7014}
7015
346774a9 7016/* Add breakpoint B at the end of the global breakpoint chain. */
c906108c 7017
b270e6f9
TT
7018static breakpoint *
7019add_to_breakpoint_chain (std::unique_ptr<breakpoint> &&b)
c906108c 7020{
346774a9 7021 struct breakpoint *b1;
b270e6f9 7022 struct breakpoint *result = b.get ();
c906108c 7023
346774a9
PA
7024 /* Add this breakpoint to the end of the chain so that a list of
7025 breakpoints will come out in order of increasing numbers. */
7026
7027 b1 = breakpoint_chain;
7028 if (b1 == 0)
b270e6f9 7029 breakpoint_chain = b.release ();
346774a9
PA
7030 else
7031 {
7032 while (b1->next)
7033 b1 = b1->next;
b270e6f9 7034 b1->next = b.release ();
346774a9 7035 }
b270e6f9
TT
7036
7037 return result;
346774a9
PA
7038}
7039
7040/* Initializes breakpoint B with type BPTYPE and no locations yet. */
7041
7042static void
7043init_raw_breakpoint_without_location (struct breakpoint *b,
7044 struct gdbarch *gdbarch,
28010a5d 7045 enum bptype bptype,
c0a91b2b 7046 const struct breakpoint_ops *ops)
346774a9 7047{
348d480f
PA
7048 gdb_assert (ops != NULL);
7049
28010a5d 7050 b->ops = ops;
4d28f7a8 7051 b->type = bptype;
a6d9a66e 7052 b->gdbarch = gdbarch;
c906108c
SS
7053 b->language = current_language->la_language;
7054 b->input_radix = input_radix;
d0fb5eae 7055 b->related_breakpoint = b;
346774a9
PA
7056}
7057
7058/* Helper to set_raw_breakpoint below. Creates a breakpoint
7059 that has type BPTYPE and has no locations as yet. */
346774a9
PA
7060
7061static struct breakpoint *
7062set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
348d480f 7063 enum bptype bptype,
c0a91b2b 7064 const struct breakpoint_ops *ops)
346774a9 7065{
3b0871f4 7066 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (bptype);
346774a9 7067
3b0871f4 7068 init_raw_breakpoint_without_location (b.get (), gdbarch, bptype, ops);
b270e6f9 7069 return add_to_breakpoint_chain (std::move (b));
0d381245
VP
7070}
7071
0ba852ab 7072/* Initialize loc->function_name. */
0e30163f 7073
0d381245 7074static void
0ba852ab 7075set_breakpoint_location_function (struct bp_location *loc)
0d381245 7076{
2bdf28a0
JK
7077 gdb_assert (loc->owner != NULL);
7078
0d381245 7079 if (loc->owner->type == bp_breakpoint
1042e4c0 7080 || loc->owner->type == bp_hardware_breakpoint
d77f58be 7081 || is_tracepoint (loc->owner))
0d381245 7082 {
2c02bd72 7083 const char *function_name;
0e30163f 7084
3467ec66 7085 if (loc->msymbol != NULL
f50776aa 7086 && (MSYMBOL_TYPE (loc->msymbol) == mst_text_gnu_ifunc
0ba852ab 7087 || MSYMBOL_TYPE (loc->msymbol) == mst_data_gnu_ifunc))
0e30163f
JK
7088 {
7089 struct breakpoint *b = loc->owner;
7090
c9d95fa3 7091 function_name = loc->msymbol->linkage_name ();
3467ec66
PA
7092
7093 if (b->type == bp_breakpoint && b->loc == loc
7094 && loc->next == NULL && b->related_breakpoint == b)
0e30163f
JK
7095 {
7096 /* Create only the whole new breakpoint of this type but do not
7097 mess more complicated breakpoints with multiple locations. */
7098 b->type = bp_gnu_ifunc_resolver;
6a3a010b
MR
7099 /* Remember the resolver's address for use by the return
7100 breakpoint. */
3467ec66 7101 loc->related_address = loc->address;
0e30163f
JK
7102 }
7103 }
3467ec66
PA
7104 else
7105 find_pc_partial_function (loc->address, &function_name, NULL, NULL);
0e30163f 7106
2c02bd72
DE
7107 if (function_name)
7108 loc->function_name = xstrdup (function_name);
0d381245
VP
7109 }
7110}
7111
a6d9a66e 7112/* Attempt to determine architecture of location identified by SAL. */
1bfeeb0f 7113struct gdbarch *
a6d9a66e
UW
7114get_sal_arch (struct symtab_and_line sal)
7115{
7116 if (sal.section)
7117 return get_objfile_arch (sal.section->objfile);
7118 if (sal.symtab)
eb822aa6 7119 return get_objfile_arch (SYMTAB_OBJFILE (sal.symtab));
a6d9a66e
UW
7120
7121 return NULL;
7122}
7123
346774a9
PA
7124/* Low level routine for partially initializing a breakpoint of type
7125 BPTYPE. The newly created breakpoint's address, section, source
c56053d2 7126 file name, and line number are provided by SAL.
0d381245
VP
7127
7128 It is expected that the caller will complete the initialization of
7129 the newly created breakpoint struct as well as output any status
c56053d2 7130 information regarding the creation of a new breakpoint. */
0d381245 7131
346774a9
PA
7132static void
7133init_raw_breakpoint (struct breakpoint *b, struct gdbarch *gdbarch,
28010a5d 7134 struct symtab_and_line sal, enum bptype bptype,
c0a91b2b 7135 const struct breakpoint_ops *ops)
0d381245 7136{
28010a5d 7137 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
346774a9 7138
3742cc8b 7139 add_location_to_breakpoint (b, &sal);
0d381245 7140
6c95b8df
PA
7141 if (bptype != bp_catchpoint)
7142 gdb_assert (sal.pspace != NULL);
7143
f8eba3c6
TT
7144 /* Store the program space that was used to set the breakpoint,
7145 except for ordinary breakpoints, which are independent of the
7146 program space. */
7147 if (bptype != bp_breakpoint && bptype != bp_hardware_breakpoint)
7148 b->pspace = sal.pspace;
346774a9 7149}
c906108c 7150
346774a9
PA
7151/* set_raw_breakpoint is a low level routine for allocating and
7152 partially initializing a breakpoint of type BPTYPE. The newly
7153 created breakpoint's address, section, source file name, and line
7154 number are provided by SAL. The newly created and partially
7155 initialized breakpoint is added to the breakpoint chain and
7156 is also returned as the value of this function.
7157
7158 It is expected that the caller will complete the initialization of
7159 the newly created breakpoint struct as well as output any status
7160 information regarding the creation of a new breakpoint. In
7161 particular, set_raw_breakpoint does NOT set the breakpoint
7162 number! Care should be taken to not allow an error to occur
7163 prior to completing the initialization of the breakpoint. If this
7164 should happen, a bogus breakpoint will be left on the chain. */
7165
7166struct breakpoint *
7167set_raw_breakpoint (struct gdbarch *gdbarch,
348d480f 7168 struct symtab_and_line sal, enum bptype bptype,
c0a91b2b 7169 const struct breakpoint_ops *ops)
346774a9 7170{
3b0871f4 7171 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (bptype);
346774a9 7172
3b0871f4 7173 init_raw_breakpoint (b.get (), gdbarch, sal, bptype, ops);
b270e6f9 7174 return add_to_breakpoint_chain (std::move (b));
c906108c
SS
7175}
7176
53a5351d 7177/* Call this routine when stepping and nexting to enable a breakpoint
186c406b
TT
7178 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
7179 initiated the operation. */
c906108c
SS
7180
7181void
186c406b 7182set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
c906108c 7183{
35df4500 7184 struct breakpoint *b, *b_tmp;
5d5658a1 7185 int thread = tp->global_num;
0fd8e87f
UW
7186
7187 /* To avoid having to rescan all objfile symbols at every step,
7188 we maintain a list of continually-inserted but always disabled
7189 longjmp "master" breakpoints. Here, we simply create momentary
7190 clones of those and enable them for the requested thread. */
35df4500 7191 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df 7192 if (b->pspace == current_program_space
186c406b
TT
7193 && (b->type == bp_longjmp_master
7194 || b->type == bp_exception_master))
0fd8e87f 7195 {
06edf0c0
PA
7196 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
7197 struct breakpoint *clone;
cc59ec59 7198
e2e4d78b
JK
7199 /* longjmp_breakpoint_ops ensures INITIATING_FRAME is cleared again
7200 after their removal. */
06edf0c0 7201 clone = momentary_breakpoint_from_master (b, type,
c1fc2657 7202 &momentary_breakpoint_ops, 1);
0fd8e87f
UW
7203 clone->thread = thread;
7204 }
186c406b
TT
7205
7206 tp->initiating_frame = frame;
c906108c
SS
7207}
7208
611c83ae 7209/* Delete all longjmp breakpoints from THREAD. */
c906108c 7210void
611c83ae 7211delete_longjmp_breakpoint (int thread)
c906108c 7212{
35df4500 7213 struct breakpoint *b, *b_tmp;
c906108c 7214
35df4500 7215 ALL_BREAKPOINTS_SAFE (b, b_tmp)
186c406b 7216 if (b->type == bp_longjmp || b->type == bp_exception)
611c83ae
PA
7217 {
7218 if (b->thread == thread)
7219 delete_breakpoint (b);
7220 }
c906108c
SS
7221}
7222
f59f708a
PA
7223void
7224delete_longjmp_breakpoint_at_next_stop (int thread)
7225{
7226 struct breakpoint *b, *b_tmp;
7227
7228 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7229 if (b->type == bp_longjmp || b->type == bp_exception)
7230 {
7231 if (b->thread == thread)
7232 b->disposition = disp_del_at_next_stop;
7233 }
7234}
7235
e2e4d78b
JK
7236/* Place breakpoints of type bp_longjmp_call_dummy to catch longjmp for
7237 INFERIOR_PTID thread. Chain them all by RELATED_BREAKPOINT and return
7238 pointer to any of them. Return NULL if this system cannot place longjmp
7239 breakpoints. */
7240
7241struct breakpoint *
7242set_longjmp_breakpoint_for_call_dummy (void)
7243{
7244 struct breakpoint *b, *retval = NULL;
7245
7246 ALL_BREAKPOINTS (b)
7247 if (b->pspace == current_program_space && b->type == bp_longjmp_master)
7248 {
7249 struct breakpoint *new_b;
7250
7251 new_b = momentary_breakpoint_from_master (b, bp_longjmp_call_dummy,
a1aa2221
LM
7252 &momentary_breakpoint_ops,
7253 1);
00431a78 7254 new_b->thread = inferior_thread ()->global_num;
e2e4d78b
JK
7255
7256 /* Link NEW_B into the chain of RETVAL breakpoints. */
7257
7258 gdb_assert (new_b->related_breakpoint == new_b);
7259 if (retval == NULL)
7260 retval = new_b;
7261 new_b->related_breakpoint = retval;
7262 while (retval->related_breakpoint != new_b->related_breakpoint)
7263 retval = retval->related_breakpoint;
7264 retval->related_breakpoint = new_b;
7265 }
7266
7267 return retval;
7268}
7269
7270/* Verify all existing dummy frames and their associated breakpoints for
b67a2c6f 7271 TP. Remove those which can no longer be found in the current frame
e2e4d78b
JK
7272 stack.
7273
7274 You should call this function only at places where it is safe to currently
7275 unwind the whole stack. Failed stack unwind would discard live dummy
7276 frames. */
7277
7278void
b67a2c6f 7279check_longjmp_breakpoint_for_call_dummy (struct thread_info *tp)
e2e4d78b
JK
7280{
7281 struct breakpoint *b, *b_tmp;
7282
7283 ALL_BREAKPOINTS_SAFE (b, b_tmp)
5d5658a1 7284 if (b->type == bp_longjmp_call_dummy && b->thread == tp->global_num)
e2e4d78b
JK
7285 {
7286 struct breakpoint *dummy_b = b->related_breakpoint;
7287
7288 while (dummy_b != b && dummy_b->type != bp_call_dummy)
7289 dummy_b = dummy_b->related_breakpoint;
7290 if (dummy_b->type != bp_call_dummy
7291 || frame_find_by_id (dummy_b->frame_id) != NULL)
7292 continue;
7293
00431a78 7294 dummy_frame_discard (dummy_b->frame_id, tp);
e2e4d78b
JK
7295
7296 while (b->related_breakpoint != b)
7297 {
7298 if (b_tmp == b->related_breakpoint)
7299 b_tmp = b->related_breakpoint->next;
7300 delete_breakpoint (b->related_breakpoint);
7301 }
7302 delete_breakpoint (b);
7303 }
7304}
7305
1900040c
MS
7306void
7307enable_overlay_breakpoints (void)
7308{
52f0bd74 7309 struct breakpoint *b;
1900040c
MS
7310
7311 ALL_BREAKPOINTS (b)
7312 if (b->type == bp_overlay_event)
7313 {
7314 b->enable_state = bp_enabled;
44702360 7315 update_global_location_list (UGLL_MAY_INSERT);
c02f5703 7316 overlay_events_enabled = 1;
1900040c
MS
7317 }
7318}
7319
7320void
7321disable_overlay_breakpoints (void)
7322{
52f0bd74 7323 struct breakpoint *b;
1900040c
MS
7324
7325 ALL_BREAKPOINTS (b)
7326 if (b->type == bp_overlay_event)
7327 {
7328 b->enable_state = bp_disabled;
44702360 7329 update_global_location_list (UGLL_DONT_INSERT);
c02f5703 7330 overlay_events_enabled = 0;
1900040c
MS
7331 }
7332}
7333
aa7d318d
TT
7334/* Set an active std::terminate breakpoint for each std::terminate
7335 master breakpoint. */
7336void
7337set_std_terminate_breakpoint (void)
7338{
35df4500 7339 struct breakpoint *b, *b_tmp;
aa7d318d 7340
35df4500 7341 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
7342 if (b->pspace == current_program_space
7343 && b->type == bp_std_terminate_master)
7344 {
06edf0c0 7345 momentary_breakpoint_from_master (b, bp_std_terminate,
a1aa2221 7346 &momentary_breakpoint_ops, 1);
aa7d318d
TT
7347 }
7348}
7349
7350/* Delete all the std::terminate breakpoints. */
7351void
7352delete_std_terminate_breakpoint (void)
7353{
35df4500 7354 struct breakpoint *b, *b_tmp;
aa7d318d 7355
35df4500 7356 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
7357 if (b->type == bp_std_terminate)
7358 delete_breakpoint (b);
7359}
7360
c4093a6a 7361struct breakpoint *
a6d9a66e 7362create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
c4093a6a
JM
7363{
7364 struct breakpoint *b;
c4093a6a 7365
06edf0c0
PA
7366 b = create_internal_breakpoint (gdbarch, address, bp_thread_event,
7367 &internal_breakpoint_ops);
7368
b5de0fa7 7369 b->enable_state = bp_enabled;
f00aae0f 7370 /* location has to be used or breakpoint_re_set will delete me. */
d28cd78a 7371 b->location = new_address_location (b->loc->address, NULL, 0);
c4093a6a 7372
44702360 7373 update_global_location_list_nothrow (UGLL_MAY_INSERT);
74960c60 7374
c4093a6a
JM
7375 return b;
7376}
7377
0101ce28
JJ
7378struct lang_and_radix
7379 {
7380 enum language lang;
7381 int radix;
7382 };
7383
4efc6507
DE
7384/* Create a breakpoint for JIT code registration and unregistration. */
7385
7386struct breakpoint *
7387create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7388{
2a7f3dff
PA
7389 return create_internal_breakpoint (gdbarch, address, bp_jit_event,
7390 &internal_breakpoint_ops);
4efc6507 7391}
0101ce28 7392
03673fc7
PP
7393/* Remove JIT code registration and unregistration breakpoint(s). */
7394
7395void
7396remove_jit_event_breakpoints (void)
7397{
7398 struct breakpoint *b, *b_tmp;
7399
7400 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7401 if (b->type == bp_jit_event
7402 && b->loc->pspace == current_program_space)
7403 delete_breakpoint (b);
7404}
7405
cae688ec
JJ
7406void
7407remove_solib_event_breakpoints (void)
7408{
35df4500 7409 struct breakpoint *b, *b_tmp;
cae688ec 7410
35df4500 7411 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df
PA
7412 if (b->type == bp_shlib_event
7413 && b->loc->pspace == current_program_space)
cae688ec
JJ
7414 delete_breakpoint (b);
7415}
7416
f37f681c
PA
7417/* See breakpoint.h. */
7418
7419void
7420remove_solib_event_breakpoints_at_next_stop (void)
7421{
7422 struct breakpoint *b, *b_tmp;
7423
7424 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7425 if (b->type == bp_shlib_event
7426 && b->loc->pspace == current_program_space)
7427 b->disposition = disp_del_at_next_stop;
7428}
7429
04086b45
PA
7430/* Helper for create_solib_event_breakpoint /
7431 create_and_insert_solib_event_breakpoint. Allows specifying which
7432 INSERT_MODE to pass through to update_global_location_list. */
7433
7434static struct breakpoint *
7435create_solib_event_breakpoint_1 (struct gdbarch *gdbarch, CORE_ADDR address,
7436 enum ugll_insert_mode insert_mode)
cae688ec
JJ
7437{
7438 struct breakpoint *b;
7439
06edf0c0
PA
7440 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event,
7441 &internal_breakpoint_ops);
04086b45 7442 update_global_location_list_nothrow (insert_mode);
cae688ec
JJ
7443 return b;
7444}
7445
04086b45
PA
7446struct breakpoint *
7447create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7448{
7449 return create_solib_event_breakpoint_1 (gdbarch, address, UGLL_MAY_INSERT);
7450}
7451
f37f681c
PA
7452/* See breakpoint.h. */
7453
7454struct breakpoint *
7455create_and_insert_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7456{
7457 struct breakpoint *b;
7458
04086b45
PA
7459 /* Explicitly tell update_global_location_list to insert
7460 locations. */
7461 b = create_solib_event_breakpoint_1 (gdbarch, address, UGLL_INSERT);
f37f681c
PA
7462 if (!b->loc->inserted)
7463 {
7464 delete_breakpoint (b);
7465 return NULL;
7466 }
7467 return b;
7468}
7469
cae688ec
JJ
7470/* Disable any breakpoints that are on code in shared libraries. Only
7471 apply to enabled breakpoints, disabled ones can just stay disabled. */
7472
7473void
cb851954 7474disable_breakpoints_in_shlibs (void)
cae688ec 7475{
876fa593 7476 struct bp_location *loc, **locp_tmp;
cae688ec 7477
876fa593 7478 ALL_BP_LOCATIONS (loc, locp_tmp)
cae688ec 7479 {
2bdf28a0 7480 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 7481 struct breakpoint *b = loc->owner;
2bdf28a0 7482
4a64f543
MS
7483 /* We apply the check to all breakpoints, including disabled for
7484 those with loc->duplicate set. This is so that when breakpoint
7485 becomes enabled, or the duplicate is removed, gdb will try to
7486 insert all breakpoints. If we don't set shlib_disabled here,
7487 we'll try to insert those breakpoints and fail. */
1042e4c0 7488 if (((b->type == bp_breakpoint)
508ccb1f 7489 || (b->type == bp_jit_event)
1042e4c0 7490 || (b->type == bp_hardware_breakpoint)
d77f58be 7491 || (is_tracepoint (b)))
6c95b8df 7492 && loc->pspace == current_program_space
0d381245 7493 && !loc->shlib_disabled
6c95b8df 7494 && solib_name_from_address (loc->pspace, loc->address)
a77053c2 7495 )
0d381245
VP
7496 {
7497 loc->shlib_disabled = 1;
7498 }
cae688ec
JJ
7499 }
7500}
7501
63644780
NB
7502/* Disable any breakpoints and tracepoints that are in SOLIB upon
7503 notification of unloaded_shlib. Only apply to enabled breakpoints,
7504 disabled ones can just stay disabled. */
84acb35a 7505
75149521 7506static void
84acb35a
JJ
7507disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
7508{
876fa593 7509 struct bp_location *loc, **locp_tmp;
84acb35a
JJ
7510 int disabled_shlib_breaks = 0;
7511
876fa593 7512 ALL_BP_LOCATIONS (loc, locp_tmp)
84acb35a 7513 {
2bdf28a0 7514 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 7515 struct breakpoint *b = loc->owner;
cc59ec59 7516
1e4d1764 7517 if (solib->pspace == loc->pspace
e2dd7057 7518 && !loc->shlib_disabled
1e4d1764
YQ
7519 && (((b->type == bp_breakpoint
7520 || b->type == bp_jit_event
7521 || b->type == bp_hardware_breakpoint)
7522 && (loc->loc_type == bp_loc_hardware_breakpoint
7523 || loc->loc_type == bp_loc_software_breakpoint))
7524 || is_tracepoint (b))
e2dd7057 7525 && solib_contains_address_p (solib, loc->address))
84acb35a 7526 {
e2dd7057
PP
7527 loc->shlib_disabled = 1;
7528 /* At this point, we cannot rely on remove_breakpoint
7529 succeeding so we must mark the breakpoint as not inserted
7530 to prevent future errors occurring in remove_breakpoints. */
7531 loc->inserted = 0;
8d3788bd
VP
7532
7533 /* This may cause duplicate notifications for the same breakpoint. */
76727919 7534 gdb::observers::breakpoint_modified.notify (b);
8d3788bd 7535
e2dd7057
PP
7536 if (!disabled_shlib_breaks)
7537 {
223ffa71 7538 target_terminal::ours_for_output ();
3e43a32a
MS
7539 warning (_("Temporarily disabling breakpoints "
7540 "for unloaded shared library \"%s\""),
e2dd7057 7541 solib->so_name);
84acb35a 7542 }
e2dd7057 7543 disabled_shlib_breaks = 1;
84acb35a
JJ
7544 }
7545 }
84acb35a
JJ
7546}
7547
63644780
NB
7548/* Disable any breakpoints and tracepoints in OBJFILE upon
7549 notification of free_objfile. Only apply to enabled breakpoints,
7550 disabled ones can just stay disabled. */
7551
7552static void
7553disable_breakpoints_in_freed_objfile (struct objfile *objfile)
7554{
7555 struct breakpoint *b;
7556
7557 if (objfile == NULL)
7558 return;
7559
d03de421
PA
7560 /* OBJF_SHARED|OBJF_USERLOADED objfiles are dynamic modules manually
7561 managed by the user with add-symbol-file/remove-symbol-file.
7562 Similarly to how breakpoints in shared libraries are handled in
7563 response to "nosharedlibrary", mark breakpoints in such modules
08351840
PA
7564 shlib_disabled so they end up uninserted on the next global
7565 location list update. Shared libraries not loaded by the user
7566 aren't handled here -- they're already handled in
7567 disable_breakpoints_in_unloaded_shlib, called by solib.c's
7568 solib_unloaded observer. We skip objfiles that are not
d03de421
PA
7569 OBJF_SHARED as those aren't considered dynamic objects (e.g. the
7570 main objfile). */
7571 if ((objfile->flags & OBJF_SHARED) == 0
7572 || (objfile->flags & OBJF_USERLOADED) == 0)
63644780
NB
7573 return;
7574
7575 ALL_BREAKPOINTS (b)
7576 {
7577 struct bp_location *loc;
7578 int bp_modified = 0;
7579
7580 if (!is_breakpoint (b) && !is_tracepoint (b))
7581 continue;
7582
7583 for (loc = b->loc; loc != NULL; loc = loc->next)
7584 {
7585 CORE_ADDR loc_addr = loc->address;
7586
7587 if (loc->loc_type != bp_loc_hardware_breakpoint
7588 && loc->loc_type != bp_loc_software_breakpoint)
7589 continue;
7590
7591 if (loc->shlib_disabled != 0)
7592 continue;
7593
7594 if (objfile->pspace != loc->pspace)
7595 continue;
7596
7597 if (loc->loc_type != bp_loc_hardware_breakpoint
7598 && loc->loc_type != bp_loc_software_breakpoint)
7599 continue;
7600
7601 if (is_addr_in_objfile (loc_addr, objfile))
7602 {
7603 loc->shlib_disabled = 1;
08351840
PA
7604 /* At this point, we don't know whether the object was
7605 unmapped from the inferior or not, so leave the
7606 inserted flag alone. We'll handle failure to
7607 uninsert quietly, in case the object was indeed
7608 unmapped. */
63644780
NB
7609
7610 mark_breakpoint_location_modified (loc);
7611
7612 bp_modified = 1;
7613 }
7614 }
7615
7616 if (bp_modified)
76727919 7617 gdb::observers::breakpoint_modified.notify (b);
63644780
NB
7618 }
7619}
7620
ce78b96d
JB
7621/* FORK & VFORK catchpoints. */
7622
e29a4733 7623/* An instance of this type is used to represent a fork or vfork
c1fc2657
SM
7624 catchpoint. A breakpoint is really of this type iff its ops pointer points
7625 to CATCH_FORK_BREAKPOINT_OPS. */
e29a4733 7626
c1fc2657 7627struct fork_catchpoint : public breakpoint
e29a4733 7628{
e29a4733
PA
7629 /* Process id of a child process whose forking triggered this
7630 catchpoint. This field is only valid immediately after this
7631 catchpoint has triggered. */
7632 ptid_t forked_inferior_pid;
7633};
7634
4a64f543
MS
7635/* Implement the "insert" breakpoint_ops method for fork
7636 catchpoints. */
ce78b96d 7637
77b06cd7
TJB
7638static int
7639insert_catch_fork (struct bp_location *bl)
ce78b96d 7640{
e99b03dc 7641 return target_insert_fork_catchpoint (inferior_ptid.pid ());
ce78b96d
JB
7642}
7643
4a64f543
MS
7644/* Implement the "remove" breakpoint_ops method for fork
7645 catchpoints. */
ce78b96d
JB
7646
7647static int
73971819 7648remove_catch_fork (struct bp_location *bl, enum remove_bp_reason reason)
ce78b96d 7649{
e99b03dc 7650 return target_remove_fork_catchpoint (inferior_ptid.pid ());
ce78b96d
JB
7651}
7652
7653/* Implement the "breakpoint_hit" breakpoint_ops method for fork
7654 catchpoints. */
7655
7656static int
f1310107 7657breakpoint_hit_catch_fork (const struct bp_location *bl,
bd522513 7658 const address_space *aspace, CORE_ADDR bp_addr,
09ac7c10 7659 const struct target_waitstatus *ws)
ce78b96d 7660{
e29a4733
PA
7661 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7662
f90263c1
TT
7663 if (ws->kind != TARGET_WAITKIND_FORKED)
7664 return 0;
7665
7666 c->forked_inferior_pid = ws->value.related_pid;
7667 return 1;
ce78b96d
JB
7668}
7669
4a64f543
MS
7670/* Implement the "print_it" breakpoint_ops method for fork
7671 catchpoints. */
ce78b96d
JB
7672
7673static enum print_stop_action
348d480f 7674print_it_catch_fork (bpstat bs)
ce78b96d 7675{
36dfb11c 7676 struct ui_out *uiout = current_uiout;
348d480f
PA
7677 struct breakpoint *b = bs->breakpoint_at;
7678 struct fork_catchpoint *c = (struct fork_catchpoint *) bs->breakpoint_at;
e29a4733 7679
ce78b96d 7680 annotate_catchpoint (b->number);
f303dbd6 7681 maybe_print_thread_hit_breakpoint (uiout);
36dfb11c 7682 if (b->disposition == disp_del)
112e8700 7683 uiout->text ("Temporary catchpoint ");
36dfb11c 7684 else
112e8700
SM
7685 uiout->text ("Catchpoint ");
7686 if (uiout->is_mi_like_p ())
36dfb11c 7687 {
112e8700
SM
7688 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_FORK));
7689 uiout->field_string ("disp", bpdisp_text (b->disposition));
36dfb11c 7690 }
381befee 7691 uiout->field_signed ("bkptno", b->number);
112e8700 7692 uiout->text (" (forked process ");
381befee 7693 uiout->field_signed ("newpid", c->forked_inferior_pid.pid ());
112e8700 7694 uiout->text ("), ");
ce78b96d
JB
7695 return PRINT_SRC_AND_LOC;
7696}
7697
4a64f543
MS
7698/* Implement the "print_one" breakpoint_ops method for fork
7699 catchpoints. */
ce78b96d
JB
7700
7701static void
a6d9a66e 7702print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 7703{
e29a4733 7704 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
79a45b7d 7705 struct value_print_options opts;
79a45e25 7706 struct ui_out *uiout = current_uiout;
79a45b7d
TT
7707
7708 get_user_print_options (&opts);
7709
4a64f543
MS
7710 /* Field 4, the address, is omitted (which makes the columns not
7711 line up too nicely with the headers, but the effect is relatively
7712 readable). */
79a45b7d 7713 if (opts.addressprint)
112e8700 7714 uiout->field_skip ("addr");
ce78b96d 7715 annotate_field (5);
112e8700 7716 uiout->text ("fork");
d7e15655 7717 if (c->forked_inferior_pid != null_ptid)
ce78b96d 7718 {
112e8700 7719 uiout->text (", process ");
381befee 7720 uiout->field_signed ("what", c->forked_inferior_pid.pid ());
112e8700 7721 uiout->spaces (1);
ce78b96d 7722 }
8ac3646f 7723
112e8700
SM
7724 if (uiout->is_mi_like_p ())
7725 uiout->field_string ("catch-type", "fork");
ce78b96d
JB
7726}
7727
7728/* Implement the "print_mention" breakpoint_ops method for fork
7729 catchpoints. */
7730
7731static void
7732print_mention_catch_fork (struct breakpoint *b)
7733{
7734 printf_filtered (_("Catchpoint %d (fork)"), b->number);
7735}
7736
6149aea9
PA
7737/* Implement the "print_recreate" breakpoint_ops method for fork
7738 catchpoints. */
7739
7740static void
7741print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
7742{
7743 fprintf_unfiltered (fp, "catch fork");
d9b3f62e 7744 print_recreate_thread (b, fp);
6149aea9
PA
7745}
7746
ce78b96d
JB
7747/* The breakpoint_ops structure to be used in fork catchpoints. */
7748
2060206e 7749static struct breakpoint_ops catch_fork_breakpoint_ops;
ce78b96d 7750
4a64f543
MS
7751/* Implement the "insert" breakpoint_ops method for vfork
7752 catchpoints. */
ce78b96d 7753
77b06cd7
TJB
7754static int
7755insert_catch_vfork (struct bp_location *bl)
ce78b96d 7756{
e99b03dc 7757 return target_insert_vfork_catchpoint (inferior_ptid.pid ());
ce78b96d
JB
7758}
7759
4a64f543
MS
7760/* Implement the "remove" breakpoint_ops method for vfork
7761 catchpoints. */
ce78b96d
JB
7762
7763static int
73971819 7764remove_catch_vfork (struct bp_location *bl, enum remove_bp_reason reason)
ce78b96d 7765{
e99b03dc 7766 return target_remove_vfork_catchpoint (inferior_ptid.pid ());
ce78b96d
JB
7767}
7768
7769/* Implement the "breakpoint_hit" breakpoint_ops method for vfork
7770 catchpoints. */
7771
7772static int
f1310107 7773breakpoint_hit_catch_vfork (const struct bp_location *bl,
bd522513 7774 const address_space *aspace, CORE_ADDR bp_addr,
09ac7c10 7775 const struct target_waitstatus *ws)
ce78b96d 7776{
e29a4733
PA
7777 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7778
f90263c1
TT
7779 if (ws->kind != TARGET_WAITKIND_VFORKED)
7780 return 0;
7781
7782 c->forked_inferior_pid = ws->value.related_pid;
7783 return 1;
ce78b96d
JB
7784}
7785
4a64f543
MS
7786/* Implement the "print_it" breakpoint_ops method for vfork
7787 catchpoints. */
ce78b96d
JB
7788
7789static enum print_stop_action
348d480f 7790print_it_catch_vfork (bpstat bs)
ce78b96d 7791{
36dfb11c 7792 struct ui_out *uiout = current_uiout;
348d480f 7793 struct breakpoint *b = bs->breakpoint_at;
e29a4733
PA
7794 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
7795
ce78b96d 7796 annotate_catchpoint (b->number);
f303dbd6 7797 maybe_print_thread_hit_breakpoint (uiout);
36dfb11c 7798 if (b->disposition == disp_del)
112e8700 7799 uiout->text ("Temporary catchpoint ");
36dfb11c 7800 else
112e8700
SM
7801 uiout->text ("Catchpoint ");
7802 if (uiout->is_mi_like_p ())
36dfb11c 7803 {
112e8700
SM
7804 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_VFORK));
7805 uiout->field_string ("disp", bpdisp_text (b->disposition));
36dfb11c 7806 }
381befee 7807 uiout->field_signed ("bkptno", b->number);
112e8700 7808 uiout->text (" (vforked process ");
381befee 7809 uiout->field_signed ("newpid", c->forked_inferior_pid.pid ());
112e8700 7810 uiout->text ("), ");
ce78b96d
JB
7811 return PRINT_SRC_AND_LOC;
7812}
7813
4a64f543
MS
7814/* Implement the "print_one" breakpoint_ops method for vfork
7815 catchpoints. */
ce78b96d
JB
7816
7817static void
a6d9a66e 7818print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 7819{
e29a4733 7820 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
79a45b7d 7821 struct value_print_options opts;
79a45e25 7822 struct ui_out *uiout = current_uiout;
79a45b7d
TT
7823
7824 get_user_print_options (&opts);
4a64f543
MS
7825 /* Field 4, the address, is omitted (which makes the columns not
7826 line up too nicely with the headers, but the effect is relatively
7827 readable). */
79a45b7d 7828 if (opts.addressprint)
112e8700 7829 uiout->field_skip ("addr");
ce78b96d 7830 annotate_field (5);
112e8700 7831 uiout->text ("vfork");
d7e15655 7832 if (c->forked_inferior_pid != null_ptid)
ce78b96d 7833 {
112e8700 7834 uiout->text (", process ");
381befee 7835 uiout->field_signed ("what", c->forked_inferior_pid.pid ());
112e8700 7836 uiout->spaces (1);
ce78b96d 7837 }
8ac3646f 7838
112e8700
SM
7839 if (uiout->is_mi_like_p ())
7840 uiout->field_string ("catch-type", "vfork");
ce78b96d
JB
7841}
7842
7843/* Implement the "print_mention" breakpoint_ops method for vfork
7844 catchpoints. */
7845
7846static void
7847print_mention_catch_vfork (struct breakpoint *b)
7848{
7849 printf_filtered (_("Catchpoint %d (vfork)"), b->number);
7850}
7851
6149aea9
PA
7852/* Implement the "print_recreate" breakpoint_ops method for vfork
7853 catchpoints. */
7854
7855static void
7856print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
7857{
7858 fprintf_unfiltered (fp, "catch vfork");
d9b3f62e 7859 print_recreate_thread (b, fp);
6149aea9
PA
7860}
7861
ce78b96d
JB
7862/* The breakpoint_ops structure to be used in vfork catchpoints. */
7863
2060206e 7864static struct breakpoint_ops catch_vfork_breakpoint_ops;
ce78b96d 7865
edcc5120 7866/* An instance of this type is used to represent an solib catchpoint.
c1fc2657 7867 A breakpoint is really of this type iff its ops pointer points to
edcc5120
TT
7868 CATCH_SOLIB_BREAKPOINT_OPS. */
7869
c1fc2657 7870struct solib_catchpoint : public breakpoint
edcc5120 7871{
c1fc2657 7872 ~solib_catchpoint () override;
edcc5120
TT
7873
7874 /* True for "catch load", false for "catch unload". */
7875 unsigned char is_load;
7876
7877 /* Regular expression to match, if any. COMPILED is only valid when
7878 REGEX is non-NULL. */
7879 char *regex;
2d7cc5c7 7880 std::unique_ptr<compiled_regex> compiled;
edcc5120
TT
7881};
7882
c1fc2657 7883solib_catchpoint::~solib_catchpoint ()
edcc5120 7884{
c1fc2657 7885 xfree (this->regex);
edcc5120
TT
7886}
7887
7888static int
7889insert_catch_solib (struct bp_location *ignore)
7890{
7891 return 0;
7892}
7893
7894static int
73971819 7895remove_catch_solib (struct bp_location *ignore, enum remove_bp_reason reason)
edcc5120
TT
7896{
7897 return 0;
7898}
7899
7900static int
7901breakpoint_hit_catch_solib (const struct bp_location *bl,
bd522513 7902 const address_space *aspace,
edcc5120
TT
7903 CORE_ADDR bp_addr,
7904 const struct target_waitstatus *ws)
7905{
7906 struct solib_catchpoint *self = (struct solib_catchpoint *) bl->owner;
7907 struct breakpoint *other;
7908
7909 if (ws->kind == TARGET_WAITKIND_LOADED)
7910 return 1;
7911
7912 ALL_BREAKPOINTS (other)
7913 {
7914 struct bp_location *other_bl;
7915
7916 if (other == bl->owner)
7917 continue;
7918
7919 if (other->type != bp_shlib_event)
7920 continue;
7921
c1fc2657 7922 if (self->pspace != NULL && other->pspace != self->pspace)
edcc5120
TT
7923 continue;
7924
7925 for (other_bl = other->loc; other_bl != NULL; other_bl = other_bl->next)
7926 {
7927 if (other->ops->breakpoint_hit (other_bl, aspace, bp_addr, ws))
7928 return 1;
7929 }
7930 }
7931
7932 return 0;
7933}
7934
7935static void
7936check_status_catch_solib (struct bpstats *bs)
7937{
7938 struct solib_catchpoint *self
7939 = (struct solib_catchpoint *) bs->breakpoint_at;
edcc5120
TT
7940
7941 if (self->is_load)
7942 {
52941706 7943 for (so_list *iter : current_program_space->added_solibs)
edcc5120
TT
7944 {
7945 if (!self->regex
2d7cc5c7 7946 || self->compiled->exec (iter->so_name, 0, NULL, 0) == 0)
edcc5120
TT
7947 return;
7948 }
7949 }
7950 else
7951 {
6fb16ce6 7952 for (const std::string &iter : current_program_space->deleted_solibs)
edcc5120
TT
7953 {
7954 if (!self->regex
6fb16ce6 7955 || self->compiled->exec (iter.c_str (), 0, NULL, 0) == 0)
edcc5120
TT
7956 return;
7957 }
7958 }
7959
7960 bs->stop = 0;
7961 bs->print_it = print_it_noop;
7962}
7963
7964static enum print_stop_action
7965print_it_catch_solib (bpstat bs)
7966{
7967 struct breakpoint *b = bs->breakpoint_at;
7968 struct ui_out *uiout = current_uiout;
7969
7970 annotate_catchpoint (b->number);
f303dbd6 7971 maybe_print_thread_hit_breakpoint (uiout);
edcc5120 7972 if (b->disposition == disp_del)
112e8700 7973 uiout->text ("Temporary catchpoint ");
edcc5120 7974 else
112e8700 7975 uiout->text ("Catchpoint ");
381befee 7976 uiout->field_signed ("bkptno", b->number);
112e8700
SM
7977 uiout->text ("\n");
7978 if (uiout->is_mi_like_p ())
7979 uiout->field_string ("disp", bpdisp_text (b->disposition));
edcc5120
TT
7980 print_solib_event (1);
7981 return PRINT_SRC_AND_LOC;
7982}
7983
7984static void
7985print_one_catch_solib (struct breakpoint *b, struct bp_location **locs)
7986{
7987 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
7988 struct value_print_options opts;
7989 struct ui_out *uiout = current_uiout;
edcc5120
TT
7990
7991 get_user_print_options (&opts);
7992 /* Field 4, the address, is omitted (which makes the columns not
7993 line up too nicely with the headers, but the effect is relatively
7994 readable). */
7995 if (opts.addressprint)
7996 {
7997 annotate_field (4);
112e8700 7998 uiout->field_skip ("addr");
edcc5120
TT
7999 }
8000
528e1572 8001 std::string msg;
edcc5120
TT
8002 annotate_field (5);
8003 if (self->is_load)
8004 {
8005 if (self->regex)
528e1572 8006 msg = string_printf (_("load of library matching %s"), self->regex);
edcc5120 8007 else
528e1572 8008 msg = _("load of library");
edcc5120
TT
8009 }
8010 else
8011 {
8012 if (self->regex)
528e1572 8013 msg = string_printf (_("unload of library matching %s"), self->regex);
edcc5120 8014 else
528e1572 8015 msg = _("unload of library");
edcc5120 8016 }
112e8700 8017 uiout->field_string ("what", msg);
8ac3646f 8018
112e8700
SM
8019 if (uiout->is_mi_like_p ())
8020 uiout->field_string ("catch-type", self->is_load ? "load" : "unload");
edcc5120
TT
8021}
8022
8023static void
8024print_mention_catch_solib (struct breakpoint *b)
8025{
8026 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8027
8028 printf_filtered (_("Catchpoint %d (%s)"), b->number,
8029 self->is_load ? "load" : "unload");
8030}
8031
8032static void
8033print_recreate_catch_solib (struct breakpoint *b, struct ui_file *fp)
8034{
8035 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8036
8037 fprintf_unfiltered (fp, "%s %s",
8038 b->disposition == disp_del ? "tcatch" : "catch",
8039 self->is_load ? "load" : "unload");
8040 if (self->regex)
8041 fprintf_unfiltered (fp, " %s", self->regex);
8042 fprintf_unfiltered (fp, "\n");
8043}
8044
8045static struct breakpoint_ops catch_solib_breakpoint_ops;
8046
91985142
MG
8047/* Shared helper function (MI and CLI) for creating and installing
8048 a shared object event catchpoint. If IS_LOAD is non-zero then
8049 the events to be caught are load events, otherwise they are
8050 unload events. If IS_TEMP is non-zero the catchpoint is a
8051 temporary one. If ENABLED is non-zero the catchpoint is
8052 created in an enabled state. */
edcc5120 8053
91985142 8054void
a121b7c1 8055add_solib_catchpoint (const char *arg, int is_load, int is_temp, int enabled)
edcc5120 8056{
edcc5120 8057 struct gdbarch *gdbarch = get_current_arch ();
edcc5120 8058
edcc5120
TT
8059 if (!arg)
8060 arg = "";
f1735a53 8061 arg = skip_spaces (arg);
edcc5120 8062
36bd8eaa 8063 std::unique_ptr<solib_catchpoint> c (new solib_catchpoint ());
edcc5120
TT
8064
8065 if (*arg != '\0')
8066 {
2d7cc5c7
PA
8067 c->compiled.reset (new compiled_regex (arg, REG_NOSUB,
8068 _("Invalid regexp")));
edcc5120
TT
8069 c->regex = xstrdup (arg);
8070 }
8071
8072 c->is_load = is_load;
36bd8eaa 8073 init_catchpoint (c.get (), gdbarch, is_temp, NULL,
edcc5120
TT
8074 &catch_solib_breakpoint_ops);
8075
c1fc2657 8076 c->enable_state = enabled ? bp_enabled : bp_disabled;
91985142 8077
b270e6f9 8078 install_breakpoint (0, std::move (c), 1);
edcc5120
TT
8079}
8080
91985142
MG
8081/* A helper function that does all the work for "catch load" and
8082 "catch unload". */
8083
8084static void
eb4c3f4a 8085catch_load_or_unload (const char *arg, int from_tty, int is_load,
91985142
MG
8086 struct cmd_list_element *command)
8087{
8088 int tempflag;
8089 const int enabled = 1;
8090
8091 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
8092
8093 add_solib_catchpoint (arg, is_load, tempflag, enabled);
8094}
8095
edcc5120 8096static void
eb4c3f4a 8097catch_load_command_1 (const char *arg, int from_tty,
edcc5120
TT
8098 struct cmd_list_element *command)
8099{
8100 catch_load_or_unload (arg, from_tty, 1, command);
8101}
8102
8103static void
eb4c3f4a 8104catch_unload_command_1 (const char *arg, int from_tty,
edcc5120
TT
8105 struct cmd_list_element *command)
8106{
8107 catch_load_or_unload (arg, from_tty, 0, command);
8108}
8109
346774a9
PA
8110/* Initialize a new breakpoint of the bp_catchpoint kind. If TEMPFLAG
8111 is non-zero, then make the breakpoint temporary. If COND_STRING is
8112 not NULL, then store it in the breakpoint. OPS, if not NULL, is
8113 the breakpoint_ops structure associated to the catchpoint. */
ce78b96d 8114
ab04a2af 8115void
346774a9
PA
8116init_catchpoint (struct breakpoint *b,
8117 struct gdbarch *gdbarch, int tempflag,
63160a43 8118 const char *cond_string,
c0a91b2b 8119 const struct breakpoint_ops *ops)
c906108c 8120{
51abb421 8121 symtab_and_line sal;
6c95b8df 8122 sal.pspace = current_program_space;
c5aa993b 8123
28010a5d 8124 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
ce78b96d 8125
1b36a34b 8126 b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
b5de0fa7 8127 b->disposition = tempflag ? disp_del : disp_donttouch;
346774a9
PA
8128}
8129
28010a5d 8130void
b270e6f9 8131install_breakpoint (int internal, std::unique_ptr<breakpoint> &&arg, int update_gll)
c56053d2 8132{
b270e6f9 8133 breakpoint *b = add_to_breakpoint_chain (std::move (arg));
3a5c3e22 8134 set_breakpoint_number (internal, b);
558a9d82
YQ
8135 if (is_tracepoint (b))
8136 set_tracepoint_count (breakpoint_count);
3a5c3e22
PA
8137 if (!internal)
8138 mention (b);
76727919 8139 gdb::observers::breakpoint_created.notify (b);
3ea46bff
YQ
8140
8141 if (update_gll)
44702360 8142 update_global_location_list (UGLL_MAY_INSERT);
c56053d2
PA
8143}
8144
9b70b993 8145static void
a6d9a66e 8146create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
63160a43 8147 int tempflag, const char *cond_string,
c0a91b2b 8148 const struct breakpoint_ops *ops)
c906108c 8149{
b270e6f9 8150 std::unique_ptr<fork_catchpoint> c (new fork_catchpoint ());
ce78b96d 8151
b270e6f9 8152 init_catchpoint (c.get (), gdbarch, tempflag, cond_string, ops);
e29a4733
PA
8153
8154 c->forked_inferior_pid = null_ptid;
8155
b270e6f9 8156 install_breakpoint (0, std::move (c), 1);
c906108c
SS
8157}
8158
fe798b75
JB
8159/* Exec catchpoints. */
8160
b4d90040 8161/* An instance of this type is used to represent an exec catchpoint.
c1fc2657 8162 A breakpoint is really of this type iff its ops pointer points to
b4d90040
PA
8163 CATCH_EXEC_BREAKPOINT_OPS. */
8164
c1fc2657 8165struct exec_catchpoint : public breakpoint
b4d90040 8166{
c1fc2657 8167 ~exec_catchpoint () override;
b4d90040
PA
8168
8169 /* Filename of a program whose exec triggered this catchpoint.
8170 This field is only valid immediately after this catchpoint has
8171 triggered. */
8172 char *exec_pathname;
8173};
8174
c1fc2657 8175/* Exec catchpoint destructor. */
b4d90040 8176
c1fc2657 8177exec_catchpoint::~exec_catchpoint ()
b4d90040 8178{
c1fc2657 8179 xfree (this->exec_pathname);
b4d90040
PA
8180}
8181
77b06cd7
TJB
8182static int
8183insert_catch_exec (struct bp_location *bl)
c906108c 8184{
e99b03dc 8185 return target_insert_exec_catchpoint (inferior_ptid.pid ());
fe798b75 8186}
c906108c 8187
fe798b75 8188static int
73971819 8189remove_catch_exec (struct bp_location *bl, enum remove_bp_reason reason)
fe798b75 8190{
e99b03dc 8191 return target_remove_exec_catchpoint (inferior_ptid.pid ());
fe798b75 8192}
c906108c 8193
fe798b75 8194static int
f1310107 8195breakpoint_hit_catch_exec (const struct bp_location *bl,
bd522513 8196 const address_space *aspace, CORE_ADDR bp_addr,
09ac7c10 8197 const struct target_waitstatus *ws)
fe798b75 8198{
b4d90040
PA
8199 struct exec_catchpoint *c = (struct exec_catchpoint *) bl->owner;
8200
f90263c1
TT
8201 if (ws->kind != TARGET_WAITKIND_EXECD)
8202 return 0;
8203
8204 c->exec_pathname = xstrdup (ws->value.execd_pathname);
8205 return 1;
fe798b75 8206}
c906108c 8207
fe798b75 8208static enum print_stop_action
348d480f 8209print_it_catch_exec (bpstat bs)
fe798b75 8210{
36dfb11c 8211 struct ui_out *uiout = current_uiout;
348d480f 8212 struct breakpoint *b = bs->breakpoint_at;
b4d90040
PA
8213 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8214
fe798b75 8215 annotate_catchpoint (b->number);
f303dbd6 8216 maybe_print_thread_hit_breakpoint (uiout);
36dfb11c 8217 if (b->disposition == disp_del)
112e8700 8218 uiout->text ("Temporary catchpoint ");
36dfb11c 8219 else
112e8700
SM
8220 uiout->text ("Catchpoint ");
8221 if (uiout->is_mi_like_p ())
36dfb11c 8222 {
112e8700
SM
8223 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_EXEC));
8224 uiout->field_string ("disp", bpdisp_text (b->disposition));
36dfb11c 8225 }
381befee 8226 uiout->field_signed ("bkptno", b->number);
112e8700
SM
8227 uiout->text (" (exec'd ");
8228 uiout->field_string ("new-exec", c->exec_pathname);
8229 uiout->text ("), ");
36dfb11c 8230
fe798b75 8231 return PRINT_SRC_AND_LOC;
c906108c
SS
8232}
8233
fe798b75 8234static void
a6d9a66e 8235print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
fe798b75 8236{
b4d90040 8237 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
fe798b75 8238 struct value_print_options opts;
79a45e25 8239 struct ui_out *uiout = current_uiout;
fe798b75
JB
8240
8241 get_user_print_options (&opts);
8242
8243 /* Field 4, the address, is omitted (which makes the columns
8244 not line up too nicely with the headers, but the effect
8245 is relatively readable). */
8246 if (opts.addressprint)
112e8700 8247 uiout->field_skip ("addr");
fe798b75 8248 annotate_field (5);
112e8700 8249 uiout->text ("exec");
b4d90040 8250 if (c->exec_pathname != NULL)
fe798b75 8251 {
112e8700
SM
8252 uiout->text (", program \"");
8253 uiout->field_string ("what", c->exec_pathname);
8254 uiout->text ("\" ");
fe798b75 8255 }
8ac3646f 8256
112e8700
SM
8257 if (uiout->is_mi_like_p ())
8258 uiout->field_string ("catch-type", "exec");
fe798b75
JB
8259}
8260
8261static void
8262print_mention_catch_exec (struct breakpoint *b)
8263{
8264 printf_filtered (_("Catchpoint %d (exec)"), b->number);
8265}
8266
6149aea9
PA
8267/* Implement the "print_recreate" breakpoint_ops method for exec
8268 catchpoints. */
8269
8270static void
8271print_recreate_catch_exec (struct breakpoint *b, struct ui_file *fp)
8272{
8273 fprintf_unfiltered (fp, "catch exec");
d9b3f62e 8274 print_recreate_thread (b, fp);
6149aea9
PA
8275}
8276
2060206e 8277static struct breakpoint_ops catch_exec_breakpoint_ops;
fe798b75 8278
c906108c 8279static int
fba45db2 8280hw_breakpoint_used_count (void)
c906108c 8281{
c906108c 8282 int i = 0;
f1310107
TJB
8283 struct breakpoint *b;
8284 struct bp_location *bl;
c906108c
SS
8285
8286 ALL_BREAKPOINTS (b)
c5aa993b 8287 {
d6b74ac4 8288 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
f1310107
TJB
8289 for (bl = b->loc; bl; bl = bl->next)
8290 {
8291 /* Special types of hardware breakpoints may use more than
8292 one register. */
348d480f 8293 i += b->ops->resources_needed (bl);
f1310107 8294 }
c5aa993b 8295 }
c906108c
SS
8296
8297 return i;
8298}
8299
a1398e0c
PA
8300/* Returns the resources B would use if it were a hardware
8301 watchpoint. */
8302
c906108c 8303static int
a1398e0c 8304hw_watchpoint_use_count (struct breakpoint *b)
c906108c 8305{
c906108c 8306 int i = 0;
e09342b5 8307 struct bp_location *bl;
c906108c 8308
a1398e0c
PA
8309 if (!breakpoint_enabled (b))
8310 return 0;
8311
8312 for (bl = b->loc; bl; bl = bl->next)
8313 {
8314 /* Special types of hardware watchpoints may use more than
8315 one register. */
8316 i += b->ops->resources_needed (bl);
8317 }
8318
8319 return i;
8320}
8321
8322/* Returns the sum the used resources of all hardware watchpoints of
8323 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
8324 the sum of the used resources of all hardware watchpoints of other
8325 types _not_ TYPE. */
8326
8327static int
8328hw_watchpoint_used_count_others (struct breakpoint *except,
8329 enum bptype type, int *other_type_used)
8330{
8331 int i = 0;
8332 struct breakpoint *b;
8333
c906108c
SS
8334 *other_type_used = 0;
8335 ALL_BREAKPOINTS (b)
e09342b5 8336 {
a1398e0c
PA
8337 if (b == except)
8338 continue;
e09342b5
TJB
8339 if (!breakpoint_enabled (b))
8340 continue;
8341
a1398e0c
PA
8342 if (b->type == type)
8343 i += hw_watchpoint_use_count (b);
8344 else if (is_hardware_watchpoint (b))
8345 *other_type_used = 1;
e09342b5
TJB
8346 }
8347
c906108c
SS
8348 return i;
8349}
8350
c906108c 8351void
fba45db2 8352disable_watchpoints_before_interactive_call_start (void)
c906108c 8353{
c5aa993b 8354 struct breakpoint *b;
c906108c
SS
8355
8356 ALL_BREAKPOINTS (b)
c5aa993b 8357 {
cc60f2e3 8358 if (is_watchpoint (b) && breakpoint_enabled (b))
c5aa993b 8359 {
b5de0fa7 8360 b->enable_state = bp_call_disabled;
44702360 8361 update_global_location_list (UGLL_DONT_INSERT);
c5aa993b
JM
8362 }
8363 }
c906108c
SS
8364}
8365
8366void
fba45db2 8367enable_watchpoints_after_interactive_call_stop (void)
c906108c 8368{
c5aa993b 8369 struct breakpoint *b;
c906108c
SS
8370
8371 ALL_BREAKPOINTS (b)
c5aa993b 8372 {
cc60f2e3 8373 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
c5aa993b 8374 {
b5de0fa7 8375 b->enable_state = bp_enabled;
44702360 8376 update_global_location_list (UGLL_MAY_INSERT);
c5aa993b
JM
8377 }
8378 }
c906108c
SS
8379}
8380
8bea4e01
UW
8381void
8382disable_breakpoints_before_startup (void)
8383{
6c95b8df 8384 current_program_space->executing_startup = 1;
44702360 8385 update_global_location_list (UGLL_DONT_INSERT);
8bea4e01
UW
8386}
8387
8388void
8389enable_breakpoints_after_startup (void)
8390{
6c95b8df 8391 current_program_space->executing_startup = 0;
f8eba3c6 8392 breakpoint_re_set ();
8bea4e01
UW
8393}
8394
7c16b83e
PA
8395/* Create a new single-step breakpoint for thread THREAD, with no
8396 locations. */
c906108c 8397
7c16b83e
PA
8398static struct breakpoint *
8399new_single_step_breakpoint (int thread, struct gdbarch *gdbarch)
8400{
b270e6f9 8401 std::unique_ptr<breakpoint> b (new breakpoint ());
7c16b83e 8402
b270e6f9 8403 init_raw_breakpoint_without_location (b.get (), gdbarch, bp_single_step,
7c16b83e
PA
8404 &momentary_breakpoint_ops);
8405
8406 b->disposition = disp_donttouch;
8407 b->frame_id = null_frame_id;
8408
8409 b->thread = thread;
8410 gdb_assert (b->thread != 0);
8411
b270e6f9 8412 return add_to_breakpoint_chain (std::move (b));
7c16b83e
PA
8413}
8414
8415/* Set a momentary breakpoint of type TYPE at address specified by
8416 SAL. If FRAME_ID is valid, the breakpoint is restricted to that
8417 frame. */
c906108c 8418
454dafbd 8419breakpoint_up
a6d9a66e
UW
8420set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
8421 struct frame_id frame_id, enum bptype type)
c906108c 8422{
52f0bd74 8423 struct breakpoint *b;
edb3359d 8424
193facb3
JK
8425 /* If FRAME_ID is valid, it should be a real frame, not an inlined or
8426 tail-called one. */
8427 gdb_assert (!frame_id_artificial_p (frame_id));
edb3359d 8428
06edf0c0 8429 b = set_raw_breakpoint (gdbarch, sal, type, &momentary_breakpoint_ops);
b5de0fa7
EZ
8430 b->enable_state = bp_enabled;
8431 b->disposition = disp_donttouch;
818dd999 8432 b->frame_id = frame_id;
c906108c 8433
00431a78 8434 b->thread = inferior_thread ()->global_num;
c906108c 8435
44702360 8436 update_global_location_list_nothrow (UGLL_MAY_INSERT);
74960c60 8437
454dafbd 8438 return breakpoint_up (b);
c906108c 8439}
611c83ae 8440
06edf0c0 8441/* Make a momentary breakpoint based on the master breakpoint ORIG.
a1aa2221
LM
8442 The new breakpoint will have type TYPE, use OPS as its
8443 breakpoint_ops, and will set enabled to LOC_ENABLED. */
e58b0e63 8444
06edf0c0
PA
8445static struct breakpoint *
8446momentary_breakpoint_from_master (struct breakpoint *orig,
8447 enum bptype type,
a1aa2221
LM
8448 const struct breakpoint_ops *ops,
8449 int loc_enabled)
e58b0e63
PA
8450{
8451 struct breakpoint *copy;
8452
06edf0c0 8453 copy = set_raw_breakpoint_without_location (orig->gdbarch, type, ops);
e58b0e63 8454 copy->loc = allocate_bp_location (copy);
0ba852ab 8455 set_breakpoint_location_function (copy->loc);
e58b0e63 8456
a6d9a66e 8457 copy->loc->gdbarch = orig->loc->gdbarch;
e58b0e63
PA
8458 copy->loc->requested_address = orig->loc->requested_address;
8459 copy->loc->address = orig->loc->address;
8460 copy->loc->section = orig->loc->section;
6c95b8df 8461 copy->loc->pspace = orig->loc->pspace;
55aa24fb 8462 copy->loc->probe = orig->loc->probe;
f8eba3c6 8463 copy->loc->line_number = orig->loc->line_number;
2f202fde 8464 copy->loc->symtab = orig->loc->symtab;
a1aa2221 8465 copy->loc->enabled = loc_enabled;
e58b0e63
PA
8466 copy->frame_id = orig->frame_id;
8467 copy->thread = orig->thread;
6c95b8df 8468 copy->pspace = orig->pspace;
e58b0e63
PA
8469
8470 copy->enable_state = bp_enabled;
8471 copy->disposition = disp_donttouch;
8472 copy->number = internal_breakpoint_number--;
8473
44702360 8474 update_global_location_list_nothrow (UGLL_DONT_INSERT);
e58b0e63
PA
8475 return copy;
8476}
8477
06edf0c0
PA
8478/* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
8479 ORIG is NULL. */
8480
8481struct breakpoint *
8482clone_momentary_breakpoint (struct breakpoint *orig)
8483{
8484 /* If there's nothing to clone, then return nothing. */
8485 if (orig == NULL)
8486 return NULL;
8487
a1aa2221 8488 return momentary_breakpoint_from_master (orig, orig->type, orig->ops, 0);
06edf0c0
PA
8489}
8490
454dafbd 8491breakpoint_up
a6d9a66e
UW
8492set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
8493 enum bptype type)
611c83ae
PA
8494{
8495 struct symtab_and_line sal;
8496
8497 sal = find_pc_line (pc, 0);
8498 sal.pc = pc;
8499 sal.section = find_pc_overlay (pc);
8500 sal.explicit_pc = 1;
8501
a6d9a66e 8502 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
611c83ae 8503}
c906108c 8504\f
c5aa993b 8505
c906108c
SS
8506/* Tell the user we have just set a breakpoint B. */
8507
8508static void
fba45db2 8509mention (struct breakpoint *b)
c906108c 8510{
348d480f 8511 b->ops->print_mention (b);
2d33446d 8512 current_uiout->text ("\n");
c906108c 8513}
c906108c 8514\f
c5aa993b 8515
1a853c52
PA
8516static int bp_loc_is_permanent (struct bp_location *loc);
8517
0d381245 8518static struct bp_location *
39d61571 8519add_location_to_breakpoint (struct breakpoint *b,
0d381245
VP
8520 const struct symtab_and_line *sal)
8521{
8522 struct bp_location *loc, **tmp;
3742cc8b
YQ
8523 CORE_ADDR adjusted_address;
8524 struct gdbarch *loc_gdbarch = get_sal_arch (*sal);
8525
8526 if (loc_gdbarch == NULL)
8527 loc_gdbarch = b->gdbarch;
8528
8529 /* Adjust the breakpoint's address prior to allocating a location.
8530 Once we call allocate_bp_location(), that mostly uninitialized
8531 location will be placed on the location chain. Adjustment of the
8532 breakpoint may cause target_read_memory() to be called and we do
8533 not want its scan of the location chain to find a breakpoint and
8534 location that's only been partially initialized. */
8535 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
8536 sal->pc, b->type);
0d381245 8537
d30113d4 8538 /* Sort the locations by their ADDRESS. */
39d61571 8539 loc = allocate_bp_location (b);
d30113d4
JK
8540 for (tmp = &(b->loc); *tmp != NULL && (*tmp)->address <= adjusted_address;
8541 tmp = &((*tmp)->next))
0d381245 8542 ;
d30113d4 8543 loc->next = *tmp;
0d381245 8544 *tmp = loc;
3742cc8b 8545
0d381245 8546 loc->requested_address = sal->pc;
3742cc8b 8547 loc->address = adjusted_address;
6c95b8df 8548 loc->pspace = sal->pspace;
935676c9 8549 loc->probe.prob = sal->prob;
729662a5 8550 loc->probe.objfile = sal->objfile;
6c95b8df 8551 gdb_assert (loc->pspace != NULL);
0d381245 8552 loc->section = sal->section;
3742cc8b 8553 loc->gdbarch = loc_gdbarch;
f8eba3c6 8554 loc->line_number = sal->line;
2f202fde 8555 loc->symtab = sal->symtab;
4a27f119 8556 loc->symbol = sal->symbol;
3467ec66
PA
8557 loc->msymbol = sal->msymbol;
8558 loc->objfile = sal->objfile;
f8eba3c6 8559
0ba852ab 8560 set_breakpoint_location_function (loc);
1a853c52 8561
6ae88661
LM
8562 /* While by definition, permanent breakpoints are already present in the
8563 code, we don't mark the location as inserted. Normally one would expect
8564 that GDB could rely on that breakpoint instruction to stop the program,
8565 thus removing the need to insert its own breakpoint, except that executing
8566 the breakpoint instruction can kill the target instead of reporting a
8567 SIGTRAP. E.g., on SPARC, when interrupts are disabled, executing the
8568 instruction resets the CPU, so QEMU 2.0.0 for SPARC correspondingly dies
8569 with "Trap 0x02 while interrupts disabled, Error state". Letting the
8570 breakpoint be inserted normally results in QEMU knowing about the GDB
8571 breakpoint, and thus trap before the breakpoint instruction is executed.
8572 (If GDB later needs to continue execution past the permanent breakpoint,
8573 it manually increments the PC, thus avoiding executing the breakpoint
8574 instruction.) */
1a853c52 8575 if (bp_loc_is_permanent (loc))
6ae88661 8576 loc->permanent = 1;
1a853c52 8577
0d381245
VP
8578 return loc;
8579}
514f746b
AR
8580\f
8581
1cf4d951 8582/* See breakpoint.h. */
514f746b 8583
1cf4d951
PA
8584int
8585program_breakpoint_here_p (struct gdbarch *gdbarch, CORE_ADDR address)
514f746b
AR
8586{
8587 int len;
8588 CORE_ADDR addr;
1afeeb75 8589 const gdb_byte *bpoint;
514f746b
AR
8590 gdb_byte *target_mem;
8591
1cf4d951
PA
8592 addr = address;
8593 bpoint = gdbarch_breakpoint_from_pc (gdbarch, &addr, &len);
8594
8595 /* Software breakpoints unsupported? */
8596 if (bpoint == NULL)
8597 return 0;
8598
224c3ddb 8599 target_mem = (gdb_byte *) alloca (len);
1cf4d951
PA
8600
8601 /* Enable the automatic memory restoration from breakpoints while
8602 we read the memory. Otherwise we could say about our temporary
8603 breakpoints they are permanent. */
cb85b21b
TT
8604 scoped_restore restore_memory
8605 = make_scoped_restore_show_memory_breakpoints (0);
1cf4d951
PA
8606
8607 if (target_read_memory (address, target_mem, len) == 0
8608 && memcmp (target_mem, bpoint, len) == 0)
cb85b21b 8609 return 1;
1cf4d951 8610
cb85b21b 8611 return 0;
1cf4d951
PA
8612}
8613
8614/* Return 1 if LOC is pointing to a permanent breakpoint,
8615 return 0 otherwise. */
8616
8617static int
8618bp_loc_is_permanent (struct bp_location *loc)
8619{
514f746b
AR
8620 gdb_assert (loc != NULL);
8621
cb1e4e32
PA
8622 /* If we have a non-breakpoint-backed catchpoint or a software
8623 watchpoint, just return 0. We should not attempt to read from
8624 the addresses the locations of these breakpoint types point to.
8625 program_breakpoint_here_p, below, will attempt to read
244558af 8626 memory. */
cb1e4e32 8627 if (!bl_address_is_meaningful (loc))
244558af
LM
8628 return 0;
8629
5ed8105e 8630 scoped_restore_current_pspace_and_thread restore_pspace_thread;
6c95b8df 8631 switch_to_program_space_and_thread (loc->pspace);
5ed8105e 8632 return program_breakpoint_here_p (loc->gdbarch, loc->address);
514f746b
AR
8633}
8634
e7e0cddf
SS
8635/* Build a command list for the dprintf corresponding to the current
8636 settings of the dprintf style options. */
8637
8638static void
8639update_dprintf_command_list (struct breakpoint *b)
8640{
8641 char *dprintf_args = b->extra_string;
8642 char *printf_line = NULL;
8643
8644 if (!dprintf_args)
8645 return;
8646
8647 dprintf_args = skip_spaces (dprintf_args);
8648
8649 /* Allow a comma, as it may have terminated a location, but don't
8650 insist on it. */
8651 if (*dprintf_args == ',')
8652 ++dprintf_args;
8653 dprintf_args = skip_spaces (dprintf_args);
8654
8655 if (*dprintf_args != '"')
8656 error (_("Bad format string, missing '\"'."));
8657
d3ce09f5 8658 if (strcmp (dprintf_style, dprintf_style_gdb) == 0)
e7e0cddf 8659 printf_line = xstrprintf ("printf %s", dprintf_args);
d3ce09f5 8660 else if (strcmp (dprintf_style, dprintf_style_call) == 0)
e7e0cddf
SS
8661 {
8662 if (!dprintf_function)
8663 error (_("No function supplied for dprintf call"));
8664
8665 if (dprintf_channel && strlen (dprintf_channel) > 0)
8666 printf_line = xstrprintf ("call (void) %s (%s,%s)",
8667 dprintf_function,
8668 dprintf_channel,
8669 dprintf_args);
8670 else
8671 printf_line = xstrprintf ("call (void) %s (%s)",
8672 dprintf_function,
8673 dprintf_args);
8674 }
d3ce09f5
SS
8675 else if (strcmp (dprintf_style, dprintf_style_agent) == 0)
8676 {
8677 if (target_can_run_breakpoint_commands ())
8678 printf_line = xstrprintf ("agent-printf %s", dprintf_args);
8679 else
8680 {
8681 warning (_("Target cannot run dprintf commands, falling back to GDB printf"));
8682 printf_line = xstrprintf ("printf %s", dprintf_args);
8683 }
8684 }
e7e0cddf
SS
8685 else
8686 internal_error (__FILE__, __LINE__,
8687 _("Invalid dprintf style."));
8688
f28045c2 8689 gdb_assert (printf_line != NULL);
e7e0cddf 8690
12973681
TT
8691 /* Manufacture a printf sequence. */
8692 struct command_line *printf_cmd_line
8693 = new struct command_line (simple_control, printf_line);
8694 breakpoint_set_commands (b, counted_command_line (printf_cmd_line,
8695 command_lines_deleter ()));
e7e0cddf
SS
8696}
8697
8698/* Update all dprintf commands, making their command lists reflect
8699 current style settings. */
8700
8701static void
eb4c3f4a 8702update_dprintf_commands (const char *args, int from_tty,
e7e0cddf
SS
8703 struct cmd_list_element *c)
8704{
8705 struct breakpoint *b;
8706
8707 ALL_BREAKPOINTS (b)
8708 {
8709 if (b->type == bp_dprintf)
8710 update_dprintf_command_list (b);
8711 }
8712}
c3f6f71d 8713
f00aae0f
KS
8714/* Create a breakpoint with SAL as location. Use LOCATION
8715 as a description of the location, and COND_STRING
b35a8b2f
DE
8716 as condition expression. If LOCATION is NULL then create an
8717 "address location" from the address in the SAL. */
018d34a4
VP
8718
8719static void
d9b3f62e 8720init_breakpoint_sal (struct breakpoint *b, struct gdbarch *gdbarch,
6c5b2ebe 8721 gdb::array_view<const symtab_and_line> sals,
ffc2605c 8722 event_location_up &&location,
e1e01040
PA
8723 gdb::unique_xmalloc_ptr<char> filter,
8724 gdb::unique_xmalloc_ptr<char> cond_string,
8725 gdb::unique_xmalloc_ptr<char> extra_string,
d9b3f62e
PA
8726 enum bptype type, enum bpdisp disposition,
8727 int thread, int task, int ignore_count,
c0a91b2b 8728 const struct breakpoint_ops *ops, int from_tty,
44f238bb
PA
8729 int enabled, int internal, unsigned flags,
8730 int display_canonical)
018d34a4 8731{
0d381245 8732 int i;
018d34a4
VP
8733
8734 if (type == bp_hardware_breakpoint)
8735 {
fbbd034e
AS
8736 int target_resources_ok;
8737
8738 i = hw_breakpoint_used_count ();
8739 target_resources_ok =
8740 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
018d34a4
VP
8741 i + 1, 0);
8742 if (target_resources_ok == 0)
8743 error (_("No hardware breakpoint support in the target."));
8744 else if (target_resources_ok < 0)
8745 error (_("Hardware breakpoints used exceeds limit."));
8746 }
8747
6c5b2ebe 8748 gdb_assert (!sals.empty ());
6c95b8df 8749
6c5b2ebe 8750 for (const auto &sal : sals)
0d381245 8751 {
0d381245
VP
8752 struct bp_location *loc;
8753
8754 if (from_tty)
5af949e3
UW
8755 {
8756 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
8757 if (!loc_gdbarch)
8758 loc_gdbarch = gdbarch;
8759
8760 describe_other_breakpoints (loc_gdbarch,
6c95b8df 8761 sal.pspace, sal.pc, sal.section, thread);
5af949e3 8762 }
0d381245 8763
6c5b2ebe 8764 if (&sal == &sals[0])
0d381245 8765 {
d9b3f62e 8766 init_raw_breakpoint (b, gdbarch, sal, type, ops);
0d381245 8767 b->thread = thread;
4a306c9a 8768 b->task = task;
855a6e68 8769
e1e01040
PA
8770 b->cond_string = cond_string.release ();
8771 b->extra_string = extra_string.release ();
0d381245 8772 b->ignore_count = ignore_count;
41447f92 8773 b->enable_state = enabled ? bp_enabled : bp_disabled;
0d381245 8774 b->disposition = disposition;
6c95b8df 8775
44f238bb
PA
8776 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
8777 b->loc->inserted = 1;
8778
0fb4aa4b
PA
8779 if (type == bp_static_tracepoint)
8780 {
d9b3f62e 8781 struct tracepoint *t = (struct tracepoint *) b;
0fb4aa4b
PA
8782 struct static_tracepoint_marker marker;
8783
983af33b 8784 if (strace_marker_p (b))
0fb4aa4b
PA
8785 {
8786 /* We already know the marker exists, otherwise, we
8787 wouldn't see a sal for it. */
d28cd78a
TT
8788 const char *p
8789 = &event_location_to_string (b->location.get ())[3];
f00aae0f 8790 const char *endp;
0fb4aa4b 8791
f1735a53 8792 p = skip_spaces (p);
0fb4aa4b 8793
f1735a53 8794 endp = skip_to_space (p);
0fb4aa4b 8795
5d9310c4 8796 t->static_trace_marker_id.assign (p, endp - p);
0fb4aa4b 8797
3e43a32a
MS
8798 printf_filtered (_("Probed static tracepoint "
8799 "marker \"%s\"\n"),
5d9310c4 8800 t->static_trace_marker_id.c_str ());
0fb4aa4b
PA
8801 }
8802 else if (target_static_tracepoint_marker_at (sal.pc, &marker))
8803 {
5d9310c4 8804 t->static_trace_marker_id = std::move (marker.str_id);
0fb4aa4b 8805
3e43a32a
MS
8806 printf_filtered (_("Probed static tracepoint "
8807 "marker \"%s\"\n"),
5d9310c4 8808 t->static_trace_marker_id.c_str ());
0fb4aa4b
PA
8809 }
8810 else
3e43a32a
MS
8811 warning (_("Couldn't determine the static "
8812 "tracepoint marker to probe"));
0fb4aa4b
PA
8813 }
8814
0d381245
VP
8815 loc = b->loc;
8816 }
8817 else
018d34a4 8818 {
39d61571 8819 loc = add_location_to_breakpoint (b, &sal);
44f238bb
PA
8820 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
8821 loc->inserted = 1;
0d381245
VP
8822 }
8823
8824 if (b->cond_string)
8825 {
bbc13ae3
KS
8826 const char *arg = b->cond_string;
8827
1bb9788d
TT
8828 loc->cond = parse_exp_1 (&arg, loc->address,
8829 block_for_pc (loc->address), 0);
0d381245 8830 if (*arg)
588ae58c 8831 error (_("Garbage '%s' follows condition"), arg);
018d34a4 8832 }
e7e0cddf
SS
8833
8834 /* Dynamic printf requires and uses additional arguments on the
8835 command line, otherwise it's an error. */
8836 if (type == bp_dprintf)
8837 {
8838 if (b->extra_string)
8839 update_dprintf_command_list (b);
8840 else
8841 error (_("Format string required"));
8842 }
8843 else if (b->extra_string)
588ae58c 8844 error (_("Garbage '%s' at end of command"), b->extra_string);
855a6e68 8845 }
018d34a4 8846
56435ebe 8847 b->display_canonical = display_canonical;
f00aae0f 8848 if (location != NULL)
d28cd78a 8849 b->location = std::move (location);
018d34a4 8850 else
d28cd78a 8851 b->location = new_address_location (b->loc->address, NULL, 0);
c0e8dcd8 8852 b->filter = std::move (filter);
d9b3f62e 8853}
018d34a4 8854
d9b3f62e
PA
8855static void
8856create_breakpoint_sal (struct gdbarch *gdbarch,
6c5b2ebe 8857 gdb::array_view<const symtab_and_line> sals,
ffc2605c 8858 event_location_up &&location,
e1e01040
PA
8859 gdb::unique_xmalloc_ptr<char> filter,
8860 gdb::unique_xmalloc_ptr<char> cond_string,
8861 gdb::unique_xmalloc_ptr<char> extra_string,
d9b3f62e
PA
8862 enum bptype type, enum bpdisp disposition,
8863 int thread, int task, int ignore_count,
c0a91b2b 8864 const struct breakpoint_ops *ops, int from_tty,
44f238bb
PA
8865 int enabled, int internal, unsigned flags,
8866 int display_canonical)
d9b3f62e 8867{
a5e364af 8868 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (type);
d9b3f62e 8869
a5e364af 8870 init_breakpoint_sal (b.get (), gdbarch,
ffc2605c 8871 sals, std::move (location),
e1e01040
PA
8872 std::move (filter),
8873 std::move (cond_string),
8874 std::move (extra_string),
d9b3f62e
PA
8875 type, disposition,
8876 thread, task, ignore_count,
8877 ops, from_tty,
44f238bb
PA
8878 enabled, internal, flags,
8879 display_canonical);
d9b3f62e 8880
b270e6f9 8881 install_breakpoint (internal, std::move (b), 0);
018d34a4
VP
8882}
8883
8884/* Add SALS.nelts breakpoints to the breakpoint table. For each
8885 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
8886 value. COND_STRING, if not NULL, specified the condition to be
8887 used for all breakpoints. Essentially the only case where
8888 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
8889 function. In that case, it's still not possible to specify
8890 separate conditions for different overloaded functions, so
8891 we take just a single condition string.
8892
c3f6f71d 8893 NOTE: If the function succeeds, the caller is expected to cleanup
018d34a4 8894 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
c3f6f71d
JM
8895 array contents). If the function fails (error() is called), the
8896 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
4a64f543 8897 COND and SALS arrays and each of those arrays contents. */
c906108c
SS
8898
8899static void
8cdf0e15 8900create_breakpoints_sal (struct gdbarch *gdbarch,
7efd8fc2 8901 struct linespec_result *canonical,
e1e01040
PA
8902 gdb::unique_xmalloc_ptr<char> cond_string,
8903 gdb::unique_xmalloc_ptr<char> extra_string,
8cdf0e15
VP
8904 enum bptype type, enum bpdisp disposition,
8905 int thread, int task, int ignore_count,
c0a91b2b 8906 const struct breakpoint_ops *ops, int from_tty,
44f238bb 8907 int enabled, int internal, unsigned flags)
c906108c 8908{
f8eba3c6 8909 if (canonical->pre_expanded)
6c5b2ebe 8910 gdb_assert (canonical->lsals.size () == 1);
f8eba3c6 8911
6c5b2ebe 8912 for (const auto &lsal : canonical->lsals)
c3f6f71d 8913 {
f00aae0f 8914 /* Note that 'location' can be NULL in the case of a plain
f8eba3c6 8915 'break', without arguments. */
ffc2605c 8916 event_location_up location
f00aae0f 8917 = (canonical->location != NULL
8e9e35b1 8918 ? copy_event_location (canonical->location.get ()) : NULL);
e1e01040 8919 gdb::unique_xmalloc_ptr<char> filter_string
6c5b2ebe 8920 (lsal.canonical != NULL ? xstrdup (lsal.canonical) : NULL);
0d381245 8921
6c5b2ebe 8922 create_breakpoint_sal (gdbarch, lsal.sals,
ffc2605c 8923 std::move (location),
e1e01040
PA
8924 std::move (filter_string),
8925 std::move (cond_string),
8926 std::move (extra_string),
e7e0cddf 8927 type, disposition,
84f4c1fe 8928 thread, task, ignore_count, ops,
44f238bb 8929 from_tty, enabled, internal, flags,
56435ebe 8930 canonical->special_display);
c3f6f71d 8931 }
c3f6f71d 8932}
c906108c 8933
f00aae0f 8934/* Parse LOCATION which is assumed to be a SAL specification possibly
c3f6f71d 8935 followed by conditionals. On return, SALS contains an array of SAL
f00aae0f
KS
8936 addresses found. LOCATION points to the end of the SAL (for
8937 linespec locations).
9998af43
TJB
8938
8939 The array and the line spec strings are allocated on the heap, it is
8940 the caller's responsibility to free them. */
c906108c 8941
b9362cc7 8942static void
f00aae0f 8943parse_breakpoint_sals (const struct event_location *location,
58438ac1 8944 struct linespec_result *canonical)
c3f6f71d 8945{
f00aae0f
KS
8946 struct symtab_and_line cursal;
8947
8948 if (event_location_type (location) == LINESPEC_LOCATION)
8949 {
a20714ff 8950 const char *spec = get_linespec_location (location)->spec_string;
f00aae0f 8951
a20714ff 8952 if (spec == NULL)
f00aae0f
KS
8953 {
8954 /* The last displayed codepoint, if it's valid, is our default
8955 breakpoint address. */
8956 if (last_displayed_sal_is_valid ())
8957 {
f00aae0f
KS
8958 /* Set sal's pspace, pc, symtab, and line to the values
8959 corresponding to the last call to print_frame_info.
8960 Be sure to reinitialize LINE with NOTCURRENT == 0
8961 as the breakpoint line number is inappropriate otherwise.
8962 find_pc_line would adjust PC, re-set it back. */
51abb421
PA
8963 symtab_and_line sal = get_last_displayed_sal ();
8964 CORE_ADDR pc = sal.pc;
8965
f00aae0f
KS
8966 sal = find_pc_line (pc, 0);
8967
8968 /* "break" without arguments is equivalent to "break *PC"
8969 where PC is the last displayed codepoint's address. So
8970 make sure to set sal.explicit_pc to prevent GDB from
8971 trying to expand the list of sals to include all other
8972 instances with the same symtab and line. */
8973 sal.pc = pc;
8974 sal.explicit_pc = 1;
8975
6c5b2ebe
PA
8976 struct linespec_sals lsal;
8977 lsal.sals = {sal};
f00aae0f
KS
8978 lsal.canonical = NULL;
8979
6c5b2ebe 8980 canonical->lsals.push_back (std::move (lsal));
f00aae0f
KS
8981 return;
8982 }
8983 else
8984 error (_("No default breakpoint address now."));
c906108c 8985 }
c906108c 8986 }
f00aae0f
KS
8987
8988 /* Force almost all breakpoints to be in terms of the
8989 current_source_symtab (which is decode_line_1's default).
8990 This should produce the results we want almost all of the
8991 time while leaving default_breakpoint_* alone.
8992
8993 ObjC: However, don't match an Objective-C method name which
8994 may have a '+' or '-' succeeded by a '['. */
8995 cursal = get_current_source_symtab_and_line ();
8996 if (last_displayed_sal_is_valid ())
c906108c 8997 {
a20714ff 8998 const char *spec = NULL;
cc80f267 8999
f00aae0f 9000 if (event_location_type (location) == LINESPEC_LOCATION)
a20714ff 9001 spec = get_linespec_location (location)->spec_string;
cc80f267 9002
f00aae0f 9003 if (!cursal.symtab
a20714ff
PA
9004 || (spec != NULL
9005 && strchr ("+-", spec[0]) != NULL
9006 && spec[1] != '['))
f00aae0f 9007 {
c2f4122d 9008 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
f00aae0f
KS
9009 get_last_displayed_symtab (),
9010 get_last_displayed_line (),
9011 canonical, NULL, NULL);
9012 return;
9013 }
c906108c 9014 }
f00aae0f 9015
c2f4122d 9016 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
f00aae0f 9017 cursal.symtab, cursal.line, canonical, NULL, NULL);
c3f6f71d 9018}
c906108c 9019
c906108c 9020
c3f6f71d 9021/* Convert each SAL into a real PC. Verify that the PC can be
4a64f543 9022 inserted as a breakpoint. If it can't throw an error. */
c906108c 9023
b9362cc7 9024static void
6c5b2ebe 9025breakpoint_sals_to_pc (std::vector<symtab_and_line> &sals)
c3f6f71d 9026{
6c5b2ebe
PA
9027 for (auto &sal : sals)
9028 resolve_sal_pc (&sal);
c3f6f71d
JM
9029}
9030
7a697b8d
SS
9031/* Fast tracepoints may have restrictions on valid locations. For
9032 instance, a fast tracepoint using a jump instead of a trap will
9033 likely have to overwrite more bytes than a trap would, and so can
9034 only be placed where the instruction is longer than the jump, or a
9035 multi-instruction sequence does not have a jump into the middle of
9036 it, etc. */
9037
9038static void
9039check_fast_tracepoint_sals (struct gdbarch *gdbarch,
6c5b2ebe 9040 gdb::array_view<const symtab_and_line> sals)
7a697b8d 9041{
6c5b2ebe 9042 for (const auto &sal : sals)
7a697b8d 9043 {
f8eba3c6
TT
9044 struct gdbarch *sarch;
9045
6c5b2ebe 9046 sarch = get_sal_arch (sal);
f8eba3c6
TT
9047 /* We fall back to GDBARCH if there is no architecture
9048 associated with SAL. */
9049 if (sarch == NULL)
9050 sarch = gdbarch;
281d762b
TT
9051 std::string msg;
9052 if (!gdbarch_fast_tracepoint_valid_at (sarch, sal.pc, &msg))
53c3572a 9053 error (_("May not have a fast tracepoint at %s%s"),
281d762b 9054 paddress (sarch, sal.pc), msg.c_str ());
7a697b8d
SS
9055 }
9056}
9057
018d34a4
VP
9058/* Given TOK, a string specification of condition and thread, as
9059 accepted by the 'break' command, extract the condition
9060 string and thread number and set *COND_STRING and *THREAD.
4a64f543 9061 PC identifies the context at which the condition should be parsed.
018d34a4
VP
9062 If no condition is found, *COND_STRING is set to NULL.
9063 If no thread is found, *THREAD is set to -1. */
d634f2de
JB
9064
9065static void
bbc13ae3 9066find_condition_and_thread (const char *tok, CORE_ADDR pc,
e7e0cddf
SS
9067 char **cond_string, int *thread, int *task,
9068 char **rest)
018d34a4
VP
9069{
9070 *cond_string = NULL;
9071 *thread = -1;
ed1d1739
KS
9072 *task = 0;
9073 *rest = NULL;
9074
018d34a4
VP
9075 while (tok && *tok)
9076 {
bbc13ae3 9077 const char *end_tok;
018d34a4 9078 int toklen;
bbc13ae3
KS
9079 const char *cond_start = NULL;
9080 const char *cond_end = NULL;
cc59ec59 9081
f1735a53 9082 tok = skip_spaces (tok);
e7e0cddf
SS
9083
9084 if ((*tok == '"' || *tok == ',') && rest)
9085 {
9086 *rest = savestring (tok, strlen (tok));
9087 return;
9088 }
9089
f1735a53 9090 end_tok = skip_to_space (tok);
d634f2de 9091
018d34a4 9092 toklen = end_tok - tok;
d634f2de 9093
018d34a4
VP
9094 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
9095 {
9096 tok = cond_start = end_tok + 1;
4d01a485 9097 parse_exp_1 (&tok, pc, block_for_pc (pc), 0);
018d34a4 9098 cond_end = tok;
d634f2de 9099 *cond_string = savestring (cond_start, cond_end - cond_start);
018d34a4
VP
9100 }
9101 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
9102 {
5d5658a1
PA
9103 const char *tmptok;
9104 struct thread_info *thr;
d634f2de 9105
018d34a4 9106 tok = end_tok + 1;
5d5658a1 9107 thr = parse_thread_id (tok, &tmptok);
018d34a4
VP
9108 if (tok == tmptok)
9109 error (_("Junk after thread keyword."));
5d5658a1 9110 *thread = thr->global_num;
bbc13ae3 9111 tok = tmptok;
018d34a4 9112 }
4a306c9a
JB
9113 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
9114 {
9115 char *tmptok;
9116
9117 tok = end_tok + 1;
bbc13ae3 9118 *task = strtol (tok, &tmptok, 0);
4a306c9a
JB
9119 if (tok == tmptok)
9120 error (_("Junk after task keyword."));
9121 if (!valid_task_id (*task))
b6199126 9122 error (_("Unknown task %d."), *task);
bbc13ae3 9123 tok = tmptok;
4a306c9a 9124 }
e7e0cddf
SS
9125 else if (rest)
9126 {
9127 *rest = savestring (tok, strlen (tok));
ccab2054 9128 return;
e7e0cddf 9129 }
018d34a4
VP
9130 else
9131 error (_("Junk at end of arguments."));
9132 }
9133}
9134
0fb4aa4b
PA
9135/* Decode a static tracepoint marker spec. */
9136
6c5b2ebe 9137static std::vector<symtab_and_line>
f00aae0f 9138decode_static_tracepoint_spec (const char **arg_p)
0fb4aa4b 9139{
f00aae0f
KS
9140 const char *p = &(*arg_p)[3];
9141 const char *endp;
0fb4aa4b 9142
f1735a53 9143 p = skip_spaces (p);
0fb4aa4b 9144
f1735a53 9145 endp = skip_to_space (p);
0fb4aa4b 9146
81b1e71c 9147 std::string marker_str (p, endp - p);
0fb4aa4b 9148
5d9310c4
SM
9149 std::vector<static_tracepoint_marker> markers
9150 = target_static_tracepoint_markers_by_strid (marker_str.c_str ());
9151 if (markers.empty ())
81b1e71c
TT
9152 error (_("No known static tracepoint marker named %s"),
9153 marker_str.c_str ());
0fb4aa4b 9154
6c5b2ebe 9155 std::vector<symtab_and_line> sals;
5d9310c4 9156 sals.reserve (markers.size ());
0fb4aa4b 9157
5d9310c4 9158 for (const static_tracepoint_marker &marker : markers)
0fb4aa4b 9159 {
5d9310c4
SM
9160 symtab_and_line sal = find_pc_line (marker.address, 0);
9161 sal.pc = marker.address;
6c5b2ebe 9162 sals.push_back (sal);
5d9310c4 9163 }
0fb4aa4b 9164
0fb4aa4b
PA
9165 *arg_p = endp;
9166 return sals;
9167}
9168
bac7c5cf
GB
9169/* Returns the breakpoint ops appropriate for use with with LOCATION_TYPE and
9170 according to IS_TRACEPOINT. */
9171
9172static const struct breakpoint_ops *
9173breakpoint_ops_for_event_location_type (enum event_location_type location_type,
9174 bool is_tracepoint)
9175{
9176 if (is_tracepoint)
9177 {
9178 if (location_type == PROBE_LOCATION)
9179 return &tracepoint_probe_breakpoint_ops;
9180 else
9181 return &tracepoint_breakpoint_ops;
9182 }
9183 else
9184 {
9185 if (location_type == PROBE_LOCATION)
9186 return &bkpt_probe_breakpoint_ops;
9187 else
9188 return &bkpt_breakpoint_ops;
9189 }
9190}
9191
9192/* See breakpoint.h. */
9193
9194const struct breakpoint_ops *
9195breakpoint_ops_for_event_location (const struct event_location *location,
9196 bool is_tracepoint)
9197{
9198 if (location != nullptr)
9199 return breakpoint_ops_for_event_location_type
9200 (event_location_type (location), is_tracepoint);
9201 return is_tracepoint ? &tracepoint_breakpoint_ops : &bkpt_breakpoint_ops;
9202}
9203
f00aae0f 9204/* See breakpoint.h. */
0101ce28 9205
8cdf0e15
VP
9206int
9207create_breakpoint (struct gdbarch *gdbarch,
e1e01040
PA
9208 const struct event_location *location,
9209 const char *cond_string,
9210 int thread, const char *extra_string,
f00aae0f 9211 int parse_extra,
0fb4aa4b 9212 int tempflag, enum bptype type_wanted,
8cdf0e15
VP
9213 int ignore_count,
9214 enum auto_boolean pending_break_support,
c0a91b2b 9215 const struct breakpoint_ops *ops,
44f238bb
PA
9216 int from_tty, int enabled, int internal,
9217 unsigned flags)
c3f6f71d 9218{
7efd8fc2 9219 struct linespec_result canonical;
0101ce28 9220 int pending = 0;
4a306c9a 9221 int task = 0;
86b17b60 9222 int prev_bkpt_count = breakpoint_count;
c3f6f71d 9223
348d480f
PA
9224 gdb_assert (ops != NULL);
9225
f00aae0f
KS
9226 /* If extra_string isn't useful, set it to NULL. */
9227 if (extra_string != NULL && *extra_string == '\0')
9228 extra_string = NULL;
9229
a70b8144 9230 try
b78a6381 9231 {
f00aae0f 9232 ops->create_sals_from_location (location, &canonical, type_wanted);
b78a6381 9233 }
230d2906 9234 catch (const gdb_exception_error &e)
0101ce28 9235 {
492d29ea
PA
9236 /* If caller is interested in rc value from parse, set
9237 value. */
9238 if (e.error == NOT_FOUND_ERROR)
0101ce28 9239 {
05ff989b
AC
9240 /* If pending breakpoint support is turned off, throw
9241 error. */
fa8d40ab
JJ
9242
9243 if (pending_break_support == AUTO_BOOLEAN_FALSE)
eedc3f4f 9244 throw;
723a2275
VP
9245
9246 exception_print (gdb_stderr, e);
fa8d40ab 9247
05ff989b
AC
9248 /* If pending breakpoint support is auto query and the user
9249 selects no, then simply return the error code. */
059fb39f 9250 if (pending_break_support == AUTO_BOOLEAN_AUTO
bfccc43c
YQ
9251 && !nquery (_("Make %s pending on future shared library load? "),
9252 bptype_string (type_wanted)))
fd9b8c24 9253 return 0;
fa8d40ab 9254
05ff989b
AC
9255 /* At this point, either the user was queried about setting
9256 a pending breakpoint and selected yes, or pending
9257 breakpoint behavior is on and thus a pending breakpoint
9258 is defaulted on behalf of the user. */
f00aae0f 9259 pending = 1;
0101ce28 9260 }
492d29ea 9261 else
eedc3f4f 9262 throw;
0101ce28 9263 }
492d29ea 9264
6c5b2ebe 9265 if (!pending && canonical.lsals.empty ())
492d29ea 9266 return 0;
c3f6f71d 9267
c3f6f71d
JM
9268 /* Resolve all line numbers to PC's and verify that the addresses
9269 are ok for the target. */
0101ce28 9270 if (!pending)
f8eba3c6 9271 {
6c5b2ebe
PA
9272 for (auto &lsal : canonical.lsals)
9273 breakpoint_sals_to_pc (lsal.sals);
f8eba3c6 9274 }
c3f6f71d 9275
7a697b8d 9276 /* Fast tracepoints may have additional restrictions on location. */
bfccc43c 9277 if (!pending && type_wanted == bp_fast_tracepoint)
f8eba3c6 9278 {
6c5b2ebe
PA
9279 for (const auto &lsal : canonical.lsals)
9280 check_fast_tracepoint_sals (gdbarch, lsal.sals);
f8eba3c6 9281 }
7a697b8d 9282
c3f6f71d
JM
9283 /* Verify that condition can be parsed, before setting any
9284 breakpoints. Allocate a separate condition expression for each
4a64f543 9285 breakpoint. */
0101ce28 9286 if (!pending)
c3f6f71d 9287 {
e1e01040
PA
9288 gdb::unique_xmalloc_ptr<char> cond_string_copy;
9289 gdb::unique_xmalloc_ptr<char> extra_string_copy;
9290
f00aae0f 9291 if (parse_extra)
72b2ff0e 9292 {
0878d0fa 9293 char *rest;
e1e01040 9294 char *cond;
52d361e1 9295
6c5b2ebe 9296 const linespec_sals &lsal = canonical.lsals[0];
52d361e1 9297
0878d0fa
YQ
9298 /* Here we only parse 'arg' to separate condition
9299 from thread number, so parsing in context of first
9300 sal is OK. When setting the breakpoint we'll
9301 re-parse it in context of each sal. */
9302
6c5b2ebe 9303 find_condition_and_thread (extra_string, lsal.sals[0].pc,
e1e01040
PA
9304 &cond, &thread, &task, &rest);
9305 cond_string_copy.reset (cond);
9306 extra_string_copy.reset (rest);
72b2ff0e 9307 }
2f069f6f 9308 else
72b2ff0e 9309 {
f00aae0f
KS
9310 if (type_wanted != bp_dprintf
9311 && extra_string != NULL && *extra_string != '\0')
9312 error (_("Garbage '%s' at end of location"), extra_string);
0878d0fa
YQ
9313
9314 /* Create a private copy of condition string. */
9315 if (cond_string)
e1e01040 9316 cond_string_copy.reset (xstrdup (cond_string));
0878d0fa
YQ
9317 /* Create a private copy of any extra string. */
9318 if (extra_string)
e1e01040 9319 extra_string_copy.reset (xstrdup (extra_string));
72b2ff0e 9320 }
0fb4aa4b 9321
52d361e1 9322 ops->create_breakpoints_sal (gdbarch, &canonical,
e1e01040
PA
9323 std::move (cond_string_copy),
9324 std::move (extra_string_copy),
9325 type_wanted,
d9b3f62e
PA
9326 tempflag ? disp_del : disp_donttouch,
9327 thread, task, ignore_count, ops,
44f238bb 9328 from_tty, enabled, internal, flags);
c906108c 9329 }
0101ce28
JJ
9330 else
9331 {
a5e364af 9332 std::unique_ptr <breakpoint> b = new_breakpoint_from_type (type_wanted);
bfccc43c 9333
a5e364af 9334 init_raw_breakpoint_without_location (b.get (), gdbarch, type_wanted, ops);
d28cd78a 9335 b->location = copy_event_location (location);
bfccc43c 9336
f00aae0f
KS
9337 if (parse_extra)
9338 b->cond_string = NULL;
e12c7713
MK
9339 else
9340 {
9341 /* Create a private copy of condition string. */
e1e01040 9342 b->cond_string = cond_string != NULL ? xstrdup (cond_string) : NULL;
15630549 9343 b->thread = thread;
e12c7713 9344 }
f00aae0f
KS
9345
9346 /* Create a private copy of any extra string. */
e1e01040 9347 b->extra_string = extra_string != NULL ? xstrdup (extra_string) : NULL;
0101ce28 9348 b->ignore_count = ignore_count;
0101ce28 9349 b->disposition = tempflag ? disp_del : disp_donttouch;
0d381245 9350 b->condition_not_parsed = 1;
41447f92 9351 b->enable_state = enabled ? bp_enabled : bp_disabled;
cc72b2a2
KP
9352 if ((type_wanted != bp_breakpoint
9353 && type_wanted != bp_hardware_breakpoint) || thread != -1)
f8eba3c6 9354 b->pspace = current_program_space;
8bea4e01 9355
b270e6f9 9356 install_breakpoint (internal, std::move (b), 0);
0101ce28
JJ
9357 }
9358
6c5b2ebe 9359 if (canonical.lsals.size () > 1)
95a42b64 9360 {
3e43a32a
MS
9361 warning (_("Multiple breakpoints were set.\nUse the "
9362 "\"delete\" command to delete unwanted breakpoints."));
86b17b60 9363 prev_breakpoint_count = prev_bkpt_count;
95a42b64
TT
9364 }
9365
44702360 9366 update_global_location_list (UGLL_MAY_INSERT);
fd9b8c24
PA
9367
9368 return 1;
c3f6f71d 9369}
c906108c 9370
348d480f 9371/* Set a breakpoint.
72b2ff0e
VP
9372 ARG is a string describing breakpoint address,
9373 condition, and thread.
9374 FLAG specifies if a breakpoint is hardware on,
9375 and if breakpoint is temporary, using BP_HARDWARE_FLAG
9376 and BP_TEMPFLAG. */
348d480f 9377
98deb0da 9378static void
f2fc3015 9379break_command_1 (const char *arg, int flag, int from_tty)
c3f6f71d 9380{
72b2ff0e 9381 int tempflag = flag & BP_TEMPFLAG;
0fb4aa4b
PA
9382 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
9383 ? bp_hardware_breakpoint
9384 : bp_breakpoint);
f00aae0f 9385
ffc2605c 9386 event_location_up location = string_to_event_location (&arg, current_language);
bac7c5cf
GB
9387 const struct breakpoint_ops *ops = breakpoint_ops_for_event_location
9388 (location.get (), false /* is_tracepoint */);
c3f6f71d 9389
8cdf0e15 9390 create_breakpoint (get_current_arch (),
ffc2605c 9391 location.get (),
f00aae0f 9392 NULL, 0, arg, 1 /* parse arg */,
0fb4aa4b 9393 tempflag, type_wanted,
8cdf0e15
VP
9394 0 /* Ignore count */,
9395 pending_break_support,
55aa24fb 9396 ops,
8cdf0e15 9397 from_tty,
84f4c1fe 9398 1 /* enabled */,
44f238bb
PA
9399 0 /* internal */,
9400 0);
c906108c
SS
9401}
9402
c906108c
SS
9403/* Helper function for break_command_1 and disassemble_command. */
9404
9405void
fba45db2 9406resolve_sal_pc (struct symtab_and_line *sal)
c906108c
SS
9407{
9408 CORE_ADDR pc;
9409
9410 if (sal->pc == 0 && sal->symtab != NULL)
9411 {
9412 if (!find_line_pc (sal->symtab, sal->line, &pc))
8a3fe4f8 9413 error (_("No line %d in file \"%s\"."),
05cba821 9414 sal->line, symtab_to_filename_for_display (sal->symtab));
c906108c 9415 sal->pc = pc;
6a048695 9416
4a64f543
MS
9417 /* If this SAL corresponds to a breakpoint inserted using a line
9418 number, then skip the function prologue if necessary. */
6a048695 9419 if (sal->explicit_line)
059acae7 9420 skip_prologue_sal (sal);
c906108c
SS
9421 }
9422
9423 if (sal->section == 0 && sal->symtab != NULL)
9424 {
346d1dfe 9425 const struct blockvector *bv;
3977b71f 9426 const struct block *b;
c5aa993b 9427 struct symbol *sym;
c906108c 9428
43f3e411
DE
9429 bv = blockvector_for_pc_sect (sal->pc, 0, &b,
9430 SYMTAB_COMPUNIT (sal->symtab));
c906108c
SS
9431 if (bv != NULL)
9432 {
7f0df278 9433 sym = block_linkage_function (b);
c906108c
SS
9434 if (sym != NULL)
9435 {
eb822aa6
DE
9436 fixup_symbol_section (sym, SYMTAB_OBJFILE (sal->symtab));
9437 sal->section = SYMBOL_OBJ_SECTION (SYMTAB_OBJFILE (sal->symtab),
9438 sym);
c906108c
SS
9439 }
9440 else
9441 {
4a64f543
MS
9442 /* It really is worthwhile to have the section, so we'll
9443 just have to look harder. This case can be executed
9444 if we have line numbers but no functions (as can
9445 happen in assembly source). */
c906108c 9446
5ed8105e 9447 scoped_restore_current_pspace_and_thread restore_pspace_thread;
6c95b8df 9448 switch_to_program_space_and_thread (sal->pspace);
c906108c 9449
5ed8105e 9450 bound_minimal_symbol msym = lookup_minimal_symbol_by_pc (sal->pc);
7cbd4a93 9451 if (msym.minsym)
efd66ac6 9452 sal->section = MSYMBOL_OBJ_SECTION (msym.objfile, msym.minsym);
c906108c
SS
9453 }
9454 }
9455 }
9456}
9457
9458void
0b39b52e 9459break_command (const char *arg, int from_tty)
c906108c 9460{
db107f19 9461 break_command_1 (arg, 0, from_tty);
c906108c
SS
9462}
9463
c906108c 9464void
0b39b52e 9465tbreak_command (const char *arg, int from_tty)
c906108c 9466{
db107f19 9467 break_command_1 (arg, BP_TEMPFLAG, from_tty);
c906108c
SS
9468}
9469
c906108c 9470static void
0b39b52e 9471hbreak_command (const char *arg, int from_tty)
c906108c 9472{
db107f19 9473 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
c906108c
SS
9474}
9475
9476static void
0b39b52e 9477thbreak_command (const char *arg, int from_tty)
c906108c 9478{
db107f19 9479 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
c906108c
SS
9480}
9481
9482static void
ee7ddd71 9483stop_command (const char *arg, int from_tty)
c906108c 9484{
a3f17187 9485 printf_filtered (_("Specify the type of breakpoint to set.\n\
c906108c 9486Usage: stop in <function | address>\n\
a3f17187 9487 stop at <line>\n"));
c906108c
SS
9488}
9489
9490static void
4495129a 9491stopin_command (const char *arg, int from_tty)
c906108c
SS
9492{
9493 int badInput = 0;
9494
cafb3438 9495 if (arg == NULL)
c906108c
SS
9496 badInput = 1;
9497 else if (*arg != '*')
9498 {
4495129a 9499 const char *argptr = arg;
c906108c
SS
9500 int hasColon = 0;
9501
4a64f543 9502 /* Look for a ':'. If this is a line number specification, then
53a5351d 9503 say it is bad, otherwise, it should be an address or
4a64f543 9504 function/method name. */
c906108c 9505 while (*argptr && !hasColon)
c5aa993b
JM
9506 {
9507 hasColon = (*argptr == ':');
9508 argptr++;
9509 }
c906108c
SS
9510
9511 if (hasColon)
c5aa993b 9512 badInput = (*argptr != ':'); /* Not a class::method */
c906108c 9513 else
c5aa993b 9514 badInput = isdigit (*arg); /* a simple line number */
c906108c
SS
9515 }
9516
9517 if (badInput)
a3f17187 9518 printf_filtered (_("Usage: stop in <function | address>\n"));
c906108c 9519 else
db107f19 9520 break_command_1 (arg, 0, from_tty);
c906108c
SS
9521}
9522
9523static void
4495129a 9524stopat_command (const char *arg, int from_tty)
c906108c
SS
9525{
9526 int badInput = 0;
9527
cafb3438 9528 if (arg == NULL || *arg == '*') /* no line number */
c906108c
SS
9529 badInput = 1;
9530 else
9531 {
4495129a 9532 const char *argptr = arg;
c906108c
SS
9533 int hasColon = 0;
9534
4a64f543
MS
9535 /* Look for a ':'. If there is a '::' then get out, otherwise
9536 it is probably a line number. */
c906108c 9537 while (*argptr && !hasColon)
c5aa993b
JM
9538 {
9539 hasColon = (*argptr == ':');
9540 argptr++;
9541 }
c906108c
SS
9542
9543 if (hasColon)
c5aa993b 9544 badInput = (*argptr == ':'); /* we have class::method */
c906108c 9545 else
c5aa993b 9546 badInput = !isdigit (*arg); /* not a line number */
c906108c
SS
9547 }
9548
9549 if (badInput)
65e65158 9550 printf_filtered (_("Usage: stop at LINE\n"));
c906108c 9551 else
db107f19 9552 break_command_1 (arg, 0, from_tty);
c906108c
SS
9553}
9554
e7e0cddf
SS
9555/* The dynamic printf command is mostly like a regular breakpoint, but
9556 with a prewired command list consisting of a single output command,
9557 built from extra arguments supplied on the dprintf command
9558 line. */
9559
da821c7b 9560static void
0b39b52e 9561dprintf_command (const char *arg, int from_tty)
e7e0cddf 9562{
ffc2605c 9563 event_location_up location = string_to_event_location (&arg, current_language);
f00aae0f
KS
9564
9565 /* If non-NULL, ARG should have been advanced past the location;
9566 the next character must be ','. */
9567 if (arg != NULL)
9568 {
9569 if (arg[0] != ',' || arg[1] == '\0')
9570 error (_("Format string required"));
9571 else
9572 {
9573 /* Skip the comma. */
9574 ++arg;
9575 }
9576 }
9577
e7e0cddf 9578 create_breakpoint (get_current_arch (),
ffc2605c 9579 location.get (),
f00aae0f 9580 NULL, 0, arg, 1 /* parse arg */,
e7e0cddf
SS
9581 0, bp_dprintf,
9582 0 /* Ignore count */,
9583 pending_break_support,
9584 &dprintf_breakpoint_ops,
9585 from_tty,
9586 1 /* enabled */,
9587 0 /* internal */,
9588 0);
9589}
9590
d3ce09f5 9591static void
0b39b52e 9592agent_printf_command (const char *arg, int from_tty)
d3ce09f5
SS
9593{
9594 error (_("May only run agent-printf on the target"));
9595}
9596
f1310107
TJB
9597/* Implement the "breakpoint_hit" breakpoint_ops method for
9598 ranged breakpoints. */
9599
9600static int
9601breakpoint_hit_ranged_breakpoint (const struct bp_location *bl,
bd522513 9602 const address_space *aspace,
09ac7c10
TT
9603 CORE_ADDR bp_addr,
9604 const struct target_waitstatus *ws)
f1310107 9605{
09ac7c10 9606 if (ws->kind != TARGET_WAITKIND_STOPPED
a493e3e2 9607 || ws->value.sig != GDB_SIGNAL_TRAP)
09ac7c10
TT
9608 return 0;
9609
f1310107
TJB
9610 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
9611 bl->length, aspace, bp_addr);
9612}
9613
9614/* Implement the "resources_needed" breakpoint_ops method for
9615 ranged breakpoints. */
9616
9617static int
9618resources_needed_ranged_breakpoint (const struct bp_location *bl)
9619{
9620 return target_ranged_break_num_registers ();
9621}
9622
9623/* Implement the "print_it" breakpoint_ops method for
9624 ranged breakpoints. */
9625
9626static enum print_stop_action
348d480f 9627print_it_ranged_breakpoint (bpstat bs)
f1310107 9628{
348d480f 9629 struct breakpoint *b = bs->breakpoint_at;
f1310107 9630 struct bp_location *bl = b->loc;
79a45e25 9631 struct ui_out *uiout = current_uiout;
f1310107
TJB
9632
9633 gdb_assert (b->type == bp_hardware_breakpoint);
9634
9635 /* Ranged breakpoints have only one location. */
9636 gdb_assert (bl && bl->next == NULL);
9637
9638 annotate_breakpoint (b->number);
f303dbd6
PA
9639
9640 maybe_print_thread_hit_breakpoint (uiout);
9641
f1310107 9642 if (b->disposition == disp_del)
112e8700 9643 uiout->text ("Temporary ranged breakpoint ");
f1310107 9644 else
112e8700
SM
9645 uiout->text ("Ranged breakpoint ");
9646 if (uiout->is_mi_like_p ())
f1310107 9647 {
112e8700 9648 uiout->field_string ("reason",
f1310107 9649 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
112e8700 9650 uiout->field_string ("disp", bpdisp_text (b->disposition));
f1310107 9651 }
381befee 9652 uiout->field_signed ("bkptno", b->number);
112e8700 9653 uiout->text (", ");
f1310107
TJB
9654
9655 return PRINT_SRC_AND_LOC;
9656}
9657
9658/* Implement the "print_one" breakpoint_ops method for
9659 ranged breakpoints. */
9660
9661static void
9662print_one_ranged_breakpoint (struct breakpoint *b,
9663 struct bp_location **last_loc)
9664{
9665 struct bp_location *bl = b->loc;
9666 struct value_print_options opts;
79a45e25 9667 struct ui_out *uiout = current_uiout;
f1310107
TJB
9668
9669 /* Ranged breakpoints have only one location. */
9670 gdb_assert (bl && bl->next == NULL);
9671
9672 get_user_print_options (&opts);
9673
9674 if (opts.addressprint)
9675 /* We don't print the address range here, it will be printed later
9676 by print_one_detail_ranged_breakpoint. */
112e8700 9677 uiout->field_skip ("addr");
f1310107
TJB
9678 annotate_field (5);
9679 print_breakpoint_location (b, bl);
9680 *last_loc = bl;
9681}
9682
9683/* Implement the "print_one_detail" breakpoint_ops method for
9684 ranged breakpoints. */
9685
9686static void
9687print_one_detail_ranged_breakpoint (const struct breakpoint *b,
9688 struct ui_out *uiout)
9689{
9690 CORE_ADDR address_start, address_end;
9691 struct bp_location *bl = b->loc;
d7e74731 9692 string_file stb;
f1310107
TJB
9693
9694 gdb_assert (bl);
9695
9696 address_start = bl->address;
9697 address_end = address_start + bl->length - 1;
9698
112e8700 9699 uiout->text ("\taddress range: ");
d7e74731
PA
9700 stb.printf ("[%s, %s]",
9701 print_core_address (bl->gdbarch, address_start),
9702 print_core_address (bl->gdbarch, address_end));
112e8700
SM
9703 uiout->field_stream ("addr", stb);
9704 uiout->text ("\n");
f1310107
TJB
9705}
9706
9707/* Implement the "print_mention" breakpoint_ops method for
9708 ranged breakpoints. */
9709
9710static void
9711print_mention_ranged_breakpoint (struct breakpoint *b)
9712{
9713 struct bp_location *bl = b->loc;
79a45e25 9714 struct ui_out *uiout = current_uiout;
f1310107
TJB
9715
9716 gdb_assert (bl);
9717 gdb_assert (b->type == bp_hardware_breakpoint);
9718
2d33446d
TT
9719 uiout->message (_("Hardware assisted ranged breakpoint %d from %s to %s."),
9720 b->number, paddress (bl->gdbarch, bl->address),
9721 paddress (bl->gdbarch, bl->address + bl->length - 1));
f1310107
TJB
9722}
9723
9724/* Implement the "print_recreate" breakpoint_ops method for
9725 ranged breakpoints. */
9726
9727static void
9728print_recreate_ranged_breakpoint (struct breakpoint *b, struct ui_file *fp)
9729{
f00aae0f 9730 fprintf_unfiltered (fp, "break-range %s, %s",
d28cd78a
TT
9731 event_location_to_string (b->location.get ()),
9732 event_location_to_string (b->location_range_end.get ()));
d9b3f62e 9733 print_recreate_thread (b, fp);
f1310107
TJB
9734}
9735
9736/* The breakpoint_ops structure to be used in ranged breakpoints. */
9737
2060206e 9738static struct breakpoint_ops ranged_breakpoint_ops;
f1310107
TJB
9739
9740/* Find the address where the end of the breakpoint range should be
9741 placed, given the SAL of the end of the range. This is so that if
9742 the user provides a line number, the end of the range is set to the
9743 last instruction of the given line. */
9744
9745static CORE_ADDR
9746find_breakpoint_range_end (struct symtab_and_line sal)
9747{
9748 CORE_ADDR end;
9749
9750 /* If the user provided a PC value, use it. Otherwise,
9751 find the address of the end of the given location. */
9752 if (sal.explicit_pc)
9753 end = sal.pc;
9754 else
9755 {
9756 int ret;
9757 CORE_ADDR start;
9758
9759 ret = find_line_pc_range (sal, &start, &end);
9760 if (!ret)
9761 error (_("Could not find location of the end of the range."));
9762
9763 /* find_line_pc_range returns the start of the next line. */
9764 end--;
9765 }
9766
9767 return end;
9768}
9769
9770/* Implement the "break-range" CLI command. */
9771
9772static void
0b39b52e 9773break_range_command (const char *arg, int from_tty)
f1310107 9774{
f2fc3015 9775 const char *arg_start;
f1310107
TJB
9776 struct linespec_result canonical_start, canonical_end;
9777 int bp_count, can_use_bp, length;
9778 CORE_ADDR end;
9779 struct breakpoint *b;
f1310107
TJB
9780
9781 /* We don't support software ranged breakpoints. */
9782 if (target_ranged_break_num_registers () < 0)
9783 error (_("This target does not support hardware ranged breakpoints."));
9784
9785 bp_count = hw_breakpoint_used_count ();
9786 bp_count += target_ranged_break_num_registers ();
9787 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
9788 bp_count, 0);
9789 if (can_use_bp < 0)
9790 error (_("Hardware breakpoints used exceeds limit."));
9791
f8eba3c6 9792 arg = skip_spaces (arg);
f1310107
TJB
9793 if (arg == NULL || arg[0] == '\0')
9794 error(_("No address range specified."));
9795
f8eba3c6 9796 arg_start = arg;
ffc2605c
TT
9797 event_location_up start_location = string_to_event_location (&arg,
9798 current_language);
9799 parse_breakpoint_sals (start_location.get (), &canonical_start);
f1310107
TJB
9800
9801 if (arg[0] != ',')
9802 error (_("Too few arguments."));
6c5b2ebe 9803 else if (canonical_start.lsals.empty ())
f1310107 9804 error (_("Could not find location of the beginning of the range."));
f8eba3c6 9805
6c5b2ebe 9806 const linespec_sals &lsal_start = canonical_start.lsals[0];
f8eba3c6 9807
6c5b2ebe
PA
9808 if (canonical_start.lsals.size () > 1
9809 || lsal_start.sals.size () != 1)
f1310107
TJB
9810 error (_("Cannot create a ranged breakpoint with multiple locations."));
9811
6c5b2ebe 9812 const symtab_and_line &sal_start = lsal_start.sals[0];
81b1e71c 9813 std::string addr_string_start (arg_start, arg - arg_start);
f1310107
TJB
9814
9815 arg++; /* Skip the comma. */
f8eba3c6 9816 arg = skip_spaces (arg);
f1310107
TJB
9817
9818 /* Parse the end location. */
9819
f1310107
TJB
9820 arg_start = arg;
9821
f8eba3c6 9822 /* We call decode_line_full directly here instead of using
f1310107
TJB
9823 parse_breakpoint_sals because we need to specify the start location's
9824 symtab and line as the default symtab and line for the end of the
9825 range. This makes it possible to have ranges like "foo.c:27, +14",
9826 where +14 means 14 lines from the start location. */
ffc2605c
TT
9827 event_location_up end_location = string_to_event_location (&arg,
9828 current_language);
9829 decode_line_full (end_location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
f8eba3c6
TT
9830 sal_start.symtab, sal_start.line,
9831 &canonical_end, NULL, NULL);
9832
6c5b2ebe 9833 if (canonical_end.lsals.empty ())
f1310107 9834 error (_("Could not find location of the end of the range."));
f8eba3c6 9835
6c5b2ebe
PA
9836 const linespec_sals &lsal_end = canonical_end.lsals[0];
9837 if (canonical_end.lsals.size () > 1
9838 || lsal_end.sals.size () != 1)
f1310107
TJB
9839 error (_("Cannot create a ranged breakpoint with multiple locations."));
9840
6c5b2ebe 9841 const symtab_and_line &sal_end = lsal_end.sals[0];
f1310107
TJB
9842
9843 end = find_breakpoint_range_end (sal_end);
9844 if (sal_start.pc > end)
177b42fe 9845 error (_("Invalid address range, end precedes start."));
f1310107
TJB
9846
9847 length = end - sal_start.pc + 1;
9848 if (length < 0)
9849 /* Length overflowed. */
9850 error (_("Address range too large."));
9851 else if (length == 1)
9852 {
9853 /* This range is simple enough to be handled by
9854 the `hbreak' command. */
81b1e71c 9855 hbreak_command (&addr_string_start[0], 1);
f1310107
TJB
9856
9857 return;
9858 }
9859
9860 /* Now set up the breakpoint. */
9861 b = set_raw_breakpoint (get_current_arch (), sal_start,
348d480f 9862 bp_hardware_breakpoint, &ranged_breakpoint_ops);
f1310107
TJB
9863 set_breakpoint_count (breakpoint_count + 1);
9864 b->number = breakpoint_count;
9865 b->disposition = disp_donttouch;
d28cd78a
TT
9866 b->location = std::move (start_location);
9867 b->location_range_end = std::move (end_location);
f1310107
TJB
9868 b->loc->length = length;
9869
f1310107 9870 mention (b);
76727919 9871 gdb::observers::breakpoint_created.notify (b);
44702360 9872 update_global_location_list (UGLL_MAY_INSERT);
f1310107
TJB
9873}
9874
4a64f543
MS
9875/* Return non-zero if EXP is verified as constant. Returned zero
9876 means EXP is variable. Also the constant detection may fail for
9877 some constant expressions and in such case still falsely return
9878 zero. */
2e6e3d9c 9879
65d79d4b
SDJ
9880static int
9881watchpoint_exp_is_const (const struct expression *exp)
9882{
9883 int i = exp->nelts;
9884
9885 while (i > 0)
9886 {
9887 int oplenp, argsp;
9888
9889 /* We are only interested in the descriptor of each element. */
9890 operator_length (exp, i, &oplenp, &argsp);
9891 i -= oplenp;
9892
9893 switch (exp->elts[i].opcode)
9894 {
9895 case BINOP_ADD:
9896 case BINOP_SUB:
9897 case BINOP_MUL:
9898 case BINOP_DIV:
9899 case BINOP_REM:
9900 case BINOP_MOD:
9901 case BINOP_LSH:
9902 case BINOP_RSH:
9903 case BINOP_LOGICAL_AND:
9904 case BINOP_LOGICAL_OR:
9905 case BINOP_BITWISE_AND:
9906 case BINOP_BITWISE_IOR:
9907 case BINOP_BITWISE_XOR:
9908 case BINOP_EQUAL:
9909 case BINOP_NOTEQUAL:
9910 case BINOP_LESS:
9911 case BINOP_GTR:
9912 case BINOP_LEQ:
9913 case BINOP_GEQ:
9914 case BINOP_REPEAT:
9915 case BINOP_COMMA:
9916 case BINOP_EXP:
9917 case BINOP_MIN:
9918 case BINOP_MAX:
9919 case BINOP_INTDIV:
9920 case BINOP_CONCAT:
65d79d4b
SDJ
9921 case TERNOP_COND:
9922 case TERNOP_SLICE:
65d79d4b
SDJ
9923
9924 case OP_LONG:
edd079d9 9925 case OP_FLOAT:
65d79d4b
SDJ
9926 case OP_LAST:
9927 case OP_COMPLEX:
9928 case OP_STRING:
65d79d4b
SDJ
9929 case OP_ARRAY:
9930 case OP_TYPE:
608b4967
TT
9931 case OP_TYPEOF:
9932 case OP_DECLTYPE:
6e72ca20 9933 case OP_TYPEID:
65d79d4b
SDJ
9934 case OP_NAME:
9935 case OP_OBJC_NSSTRING:
9936
9937 case UNOP_NEG:
9938 case UNOP_LOGICAL_NOT:
9939 case UNOP_COMPLEMENT:
9940 case UNOP_ADDR:
9941 case UNOP_HIGH:
aeaa2474 9942 case UNOP_CAST:
9eaf6705
TT
9943
9944 case UNOP_CAST_TYPE:
9945 case UNOP_REINTERPRET_CAST:
9946 case UNOP_DYNAMIC_CAST:
4a64f543
MS
9947 /* Unary, binary and ternary operators: We have to check
9948 their operands. If they are constant, then so is the
9949 result of that operation. For instance, if A and B are
9950 determined to be constants, then so is "A + B".
9951
9952 UNOP_IND is one exception to the rule above, because the
9953 value of *ADDR is not necessarily a constant, even when
9954 ADDR is. */
65d79d4b
SDJ
9955 break;
9956
9957 case OP_VAR_VALUE:
9958 /* Check whether the associated symbol is a constant.
4a64f543 9959
65d79d4b 9960 We use SYMBOL_CLASS rather than TYPE_CONST because it's
4a64f543
MS
9961 possible that a buggy compiler could mark a variable as
9962 constant even when it is not, and TYPE_CONST would return
9963 true in this case, while SYMBOL_CLASS wouldn't.
9964
9965 We also have to check for function symbols because they
9966 are always constant. */
65d79d4b
SDJ
9967 {
9968 struct symbol *s = exp->elts[i + 2].symbol;
9969
9970 if (SYMBOL_CLASS (s) != LOC_BLOCK
9971 && SYMBOL_CLASS (s) != LOC_CONST
9972 && SYMBOL_CLASS (s) != LOC_CONST_BYTES)
9973 return 0;
9974 break;
9975 }
9976
9977 /* The default action is to return 0 because we are using
9978 the optimistic approach here: If we don't know something,
9979 then it is not a constant. */
9980 default:
9981 return 0;
9982 }
9983 }
9984
9985 return 1;
9986}
9987
c1fc2657 9988/* Watchpoint destructor. */
3a5c3e22 9989
c1fc2657 9990watchpoint::~watchpoint ()
3a5c3e22 9991{
c1fc2657
SM
9992 xfree (this->exp_string);
9993 xfree (this->exp_string_reparse);
3a5c3e22
PA
9994}
9995
348d480f
PA
9996/* Implement the "re_set" breakpoint_ops method for watchpoints. */
9997
9998static void
9999re_set_watchpoint (struct breakpoint *b)
10000{
3a5c3e22
PA
10001 struct watchpoint *w = (struct watchpoint *) b;
10002
348d480f
PA
10003 /* Watchpoint can be either on expression using entirely global
10004 variables, or it can be on local variables.
10005
10006 Watchpoints of the first kind are never auto-deleted, and even
10007 persist across program restarts. Since they can use variables
10008 from shared libraries, we need to reparse expression as libraries
10009 are loaded and unloaded.
10010
10011 Watchpoints on local variables can also change meaning as result
10012 of solib event. For example, if a watchpoint uses both a local
10013 and a global variables in expression, it's a local watchpoint,
10014 but unloading of a shared library will make the expression
10015 invalid. This is not a very common use case, but we still
10016 re-evaluate expression, to avoid surprises to the user.
10017
10018 Note that for local watchpoints, we re-evaluate it only if
10019 watchpoints frame id is still valid. If it's not, it means the
10020 watchpoint is out of scope and will be deleted soon. In fact,
10021 I'm not sure we'll ever be called in this case.
10022
10023 If a local watchpoint's frame id is still valid, then
3a5c3e22 10024 w->exp_valid_block is likewise valid, and we can safely use it.
348d480f 10025
3a5c3e22
PA
10026 Don't do anything about disabled watchpoints, since they will be
10027 reevaluated again when enabled. */
10028 update_watchpoint (w, 1 /* reparse */);
348d480f
PA
10029}
10030
77b06cd7
TJB
10031/* Implement the "insert" breakpoint_ops method for hardware watchpoints. */
10032
10033static int
10034insert_watchpoint (struct bp_location *bl)
10035{
3a5c3e22
PA
10036 struct watchpoint *w = (struct watchpoint *) bl->owner;
10037 int length = w->exact ? 1 : bl->length;
e09342b5
TJB
10038
10039 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
4d01a485 10040 w->cond_exp.get ());
77b06cd7
TJB
10041}
10042
10043/* Implement the "remove" breakpoint_ops method for hardware watchpoints. */
10044
10045static int
73971819 10046remove_watchpoint (struct bp_location *bl, enum remove_bp_reason reason)
77b06cd7 10047{
3a5c3e22
PA
10048 struct watchpoint *w = (struct watchpoint *) bl->owner;
10049 int length = w->exact ? 1 : bl->length;
e09342b5
TJB
10050
10051 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
4d01a485 10052 w->cond_exp.get ());
e09342b5
TJB
10053}
10054
e09342b5 10055static int
348d480f 10056breakpoint_hit_watchpoint (const struct bp_location *bl,
bd522513 10057 const address_space *aspace, CORE_ADDR bp_addr,
09ac7c10 10058 const struct target_waitstatus *ws)
e09342b5 10059{
348d480f 10060 struct breakpoint *b = bl->owner;
3a5c3e22 10061 struct watchpoint *w = (struct watchpoint *) b;
77b06cd7 10062
348d480f
PA
10063 /* Continuable hardware watchpoints are treated as non-existent if the
10064 reason we stopped wasn't a hardware watchpoint (we didn't stop on
10065 some data address). Otherwise gdb won't stop on a break instruction
10066 in the code (not from a breakpoint) when a hardware watchpoint has
10067 been defined. Also skip watchpoints which we know did not trigger
10068 (did not match the data address). */
10069 if (is_hardware_watchpoint (b)
3a5c3e22 10070 && w->watchpoint_triggered == watch_triggered_no)
348d480f 10071 return 0;
9c06b0b4 10072
348d480f 10073 return 1;
9c06b0b4
TJB
10074}
10075
348d480f
PA
10076static void
10077check_status_watchpoint (bpstat bs)
9c06b0b4 10078{
348d480f 10079 gdb_assert (is_watchpoint (bs->breakpoint_at));
9c06b0b4 10080
348d480f 10081 bpstat_check_watchpoint (bs);
9c06b0b4
TJB
10082}
10083
10084/* Implement the "resources_needed" breakpoint_ops method for
348d480f 10085 hardware watchpoints. */
9c06b0b4
TJB
10086
10087static int
348d480f 10088resources_needed_watchpoint (const struct bp_location *bl)
9c06b0b4 10089{
3a5c3e22
PA
10090 struct watchpoint *w = (struct watchpoint *) bl->owner;
10091 int length = w->exact? 1 : bl->length;
348d480f
PA
10092
10093 return target_region_ok_for_hw_watchpoint (bl->address, length);
9c06b0b4
TJB
10094}
10095
10096/* Implement the "works_in_software_mode" breakpoint_ops method for
348d480f 10097 hardware watchpoints. */
9c06b0b4
TJB
10098
10099static int
348d480f 10100works_in_software_mode_watchpoint (const struct breakpoint *b)
9c06b0b4 10101{
efa80663
PA
10102 /* Read and access watchpoints only work with hardware support. */
10103 return b->type == bp_watchpoint || b->type == bp_hardware_watchpoint;
9c06b0b4
TJB
10104}
10105
9c06b0b4 10106static enum print_stop_action
348d480f 10107print_it_watchpoint (bpstat bs)
9c06b0b4 10108{
348d480f 10109 struct breakpoint *b;
348d480f 10110 enum print_stop_action result;
3a5c3e22 10111 struct watchpoint *w;
79a45e25 10112 struct ui_out *uiout = current_uiout;
348d480f
PA
10113
10114 gdb_assert (bs->bp_location_at != NULL);
10115
348d480f 10116 b = bs->breakpoint_at;
3a5c3e22 10117 w = (struct watchpoint *) b;
348d480f 10118
f303dbd6
PA
10119 annotate_watchpoint (b->number);
10120 maybe_print_thread_hit_breakpoint (uiout);
10121
d7e74731
PA
10122 string_file stb;
10123
76f9c9cf 10124 gdb::optional<ui_out_emit_tuple> tuple_emitter;
9c06b0b4
TJB
10125 switch (b->type)
10126 {
348d480f 10127 case bp_watchpoint:
9c06b0b4 10128 case bp_hardware_watchpoint:
112e8700
SM
10129 if (uiout->is_mi_like_p ())
10130 uiout->field_string
10131 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
348d480f 10132 mention (b);
76f9c9cf 10133 tuple_emitter.emplace (uiout, "value");
112e8700 10134 uiout->text ("\nOld value = ");
850645cf 10135 watchpoint_value_print (bs->old_val.get (), &stb);
112e8700
SM
10136 uiout->field_stream ("old", stb);
10137 uiout->text ("\nNew value = ");
850645cf 10138 watchpoint_value_print (w->val.get (), &stb);
112e8700
SM
10139 uiout->field_stream ("new", stb);
10140 uiout->text ("\n");
348d480f
PA
10141 /* More than one watchpoint may have been triggered. */
10142 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10143 break;
10144
10145 case bp_read_watchpoint:
112e8700
SM
10146 if (uiout->is_mi_like_p ())
10147 uiout->field_string
10148 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
348d480f 10149 mention (b);
76f9c9cf 10150 tuple_emitter.emplace (uiout, "value");
112e8700 10151 uiout->text ("\nValue = ");
850645cf 10152 watchpoint_value_print (w->val.get (), &stb);
112e8700
SM
10153 uiout->field_stream ("value", stb);
10154 uiout->text ("\n");
348d480f 10155 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10156 break;
10157
10158 case bp_access_watchpoint:
348d480f
PA
10159 if (bs->old_val != NULL)
10160 {
112e8700
SM
10161 if (uiout->is_mi_like_p ())
10162 uiout->field_string
10163 ("reason",
348d480f
PA
10164 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10165 mention (b);
76f9c9cf 10166 tuple_emitter.emplace (uiout, "value");
112e8700 10167 uiout->text ("\nOld value = ");
850645cf 10168 watchpoint_value_print (bs->old_val.get (), &stb);
112e8700
SM
10169 uiout->field_stream ("old", stb);
10170 uiout->text ("\nNew value = ");
348d480f
PA
10171 }
10172 else
10173 {
10174 mention (b);
112e8700
SM
10175 if (uiout->is_mi_like_p ())
10176 uiout->field_string
10177 ("reason",
348d480f 10178 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
76f9c9cf 10179 tuple_emitter.emplace (uiout, "value");
112e8700 10180 uiout->text ("\nValue = ");
348d480f 10181 }
850645cf 10182 watchpoint_value_print (w->val.get (), &stb);
112e8700
SM
10183 uiout->field_stream ("new", stb);
10184 uiout->text ("\n");
348d480f 10185 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10186 break;
10187 default:
348d480f 10188 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10189 }
10190
348d480f
PA
10191 return result;
10192}
10193
10194/* Implement the "print_mention" breakpoint_ops method for hardware
10195 watchpoints. */
10196
10197static void
10198print_mention_watchpoint (struct breakpoint *b)
10199{
3a5c3e22 10200 struct watchpoint *w = (struct watchpoint *) b;
79a45e25 10201 struct ui_out *uiout = current_uiout;
46b9c129 10202 const char *tuple_name;
348d480f
PA
10203
10204 switch (b->type)
10205 {
10206 case bp_watchpoint:
112e8700 10207 uiout->text ("Watchpoint ");
46b9c129 10208 tuple_name = "wpt";
348d480f
PA
10209 break;
10210 case bp_hardware_watchpoint:
112e8700 10211 uiout->text ("Hardware watchpoint ");
46b9c129 10212 tuple_name = "wpt";
348d480f
PA
10213 break;
10214 case bp_read_watchpoint:
112e8700 10215 uiout->text ("Hardware read watchpoint ");
46b9c129 10216 tuple_name = "hw-rwpt";
348d480f
PA
10217 break;
10218 case bp_access_watchpoint:
112e8700 10219 uiout->text ("Hardware access (read/write) watchpoint ");
46b9c129 10220 tuple_name = "hw-awpt";
348d480f
PA
10221 break;
10222 default:
10223 internal_error (__FILE__, __LINE__,
10224 _("Invalid hardware watchpoint type."));
10225 }
10226
46b9c129 10227 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
381befee 10228 uiout->field_signed ("number", b->number);
112e8700
SM
10229 uiout->text (": ");
10230 uiout->field_string ("exp", w->exp_string);
348d480f
PA
10231}
10232
10233/* Implement the "print_recreate" breakpoint_ops method for
10234 watchpoints. */
10235
10236static void
10237print_recreate_watchpoint (struct breakpoint *b, struct ui_file *fp)
10238{
3a5c3e22
PA
10239 struct watchpoint *w = (struct watchpoint *) b;
10240
348d480f
PA
10241 switch (b->type)
10242 {
10243 case bp_watchpoint:
10244 case bp_hardware_watchpoint:
10245 fprintf_unfiltered (fp, "watch");
10246 break;
10247 case bp_read_watchpoint:
10248 fprintf_unfiltered (fp, "rwatch");
10249 break;
10250 case bp_access_watchpoint:
10251 fprintf_unfiltered (fp, "awatch");
10252 break;
10253 default:
10254 internal_error (__FILE__, __LINE__,
10255 _("Invalid watchpoint type."));
10256 }
10257
3a5c3e22 10258 fprintf_unfiltered (fp, " %s", w->exp_string);
d9b3f62e 10259 print_recreate_thread (b, fp);
348d480f
PA
10260}
10261
427cd150
TT
10262/* Implement the "explains_signal" breakpoint_ops method for
10263 watchpoints. */
10264
47591c29 10265static int
427cd150
TT
10266explains_signal_watchpoint (struct breakpoint *b, enum gdb_signal sig)
10267{
10268 /* A software watchpoint cannot cause a signal other than
10269 GDB_SIGNAL_TRAP. */
10270 if (b->type == bp_watchpoint && sig != GDB_SIGNAL_TRAP)
47591c29 10271 return 0;
427cd150 10272
47591c29 10273 return 1;
427cd150
TT
10274}
10275
348d480f
PA
10276/* The breakpoint_ops structure to be used in hardware watchpoints. */
10277
2060206e 10278static struct breakpoint_ops watchpoint_breakpoint_ops;
348d480f
PA
10279
10280/* Implement the "insert" breakpoint_ops method for
10281 masked hardware watchpoints. */
10282
10283static int
10284insert_masked_watchpoint (struct bp_location *bl)
10285{
3a5c3e22
PA
10286 struct watchpoint *w = (struct watchpoint *) bl->owner;
10287
10288 return target_insert_mask_watchpoint (bl->address, w->hw_wp_mask,
348d480f
PA
10289 bl->watchpoint_type);
10290}
10291
10292/* Implement the "remove" breakpoint_ops method for
10293 masked hardware watchpoints. */
10294
10295static int
73971819 10296remove_masked_watchpoint (struct bp_location *bl, enum remove_bp_reason reason)
348d480f 10297{
3a5c3e22
PA
10298 struct watchpoint *w = (struct watchpoint *) bl->owner;
10299
10300 return target_remove_mask_watchpoint (bl->address, w->hw_wp_mask,
348d480f
PA
10301 bl->watchpoint_type);
10302}
10303
10304/* Implement the "resources_needed" breakpoint_ops method for
10305 masked hardware watchpoints. */
10306
10307static int
10308resources_needed_masked_watchpoint (const struct bp_location *bl)
10309{
3a5c3e22
PA
10310 struct watchpoint *w = (struct watchpoint *) bl->owner;
10311
10312 return target_masked_watch_num_registers (bl->address, w->hw_wp_mask);
348d480f
PA
10313}
10314
10315/* Implement the "works_in_software_mode" breakpoint_ops method for
10316 masked hardware watchpoints. */
10317
10318static int
10319works_in_software_mode_masked_watchpoint (const struct breakpoint *b)
10320{
10321 return 0;
10322}
10323
10324/* Implement the "print_it" breakpoint_ops method for
10325 masked hardware watchpoints. */
10326
10327static enum print_stop_action
10328print_it_masked_watchpoint (bpstat bs)
10329{
10330 struct breakpoint *b = bs->breakpoint_at;
79a45e25 10331 struct ui_out *uiout = current_uiout;
348d480f
PA
10332
10333 /* Masked watchpoints have only one location. */
10334 gdb_assert (b->loc && b->loc->next == NULL);
10335
f303dbd6
PA
10336 annotate_watchpoint (b->number);
10337 maybe_print_thread_hit_breakpoint (uiout);
10338
348d480f
PA
10339 switch (b->type)
10340 {
10341 case bp_hardware_watchpoint:
112e8700
SM
10342 if (uiout->is_mi_like_p ())
10343 uiout->field_string
10344 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
348d480f
PA
10345 break;
10346
10347 case bp_read_watchpoint:
112e8700
SM
10348 if (uiout->is_mi_like_p ())
10349 uiout->field_string
10350 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
348d480f
PA
10351 break;
10352
10353 case bp_access_watchpoint:
112e8700
SM
10354 if (uiout->is_mi_like_p ())
10355 uiout->field_string
10356 ("reason",
348d480f
PA
10357 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10358 break;
10359 default:
10360 internal_error (__FILE__, __LINE__,
10361 _("Invalid hardware watchpoint type."));
10362 }
10363
10364 mention (b);
112e8700 10365 uiout->text (_("\n\
9c06b0b4
TJB
10366Check the underlying instruction at PC for the memory\n\
10367address and value which triggered this watchpoint.\n"));
112e8700 10368 uiout->text ("\n");
9c06b0b4
TJB
10369
10370 /* More than one watchpoint may have been triggered. */
10371 return PRINT_UNKNOWN;
10372}
10373
10374/* Implement the "print_one_detail" breakpoint_ops method for
10375 masked hardware watchpoints. */
10376
10377static void
10378print_one_detail_masked_watchpoint (const struct breakpoint *b,
10379 struct ui_out *uiout)
10380{
3a5c3e22
PA
10381 struct watchpoint *w = (struct watchpoint *) b;
10382
9c06b0b4
TJB
10383 /* Masked watchpoints have only one location. */
10384 gdb_assert (b->loc && b->loc->next == NULL);
10385
112e8700
SM
10386 uiout->text ("\tmask ");
10387 uiout->field_core_addr ("mask", b->loc->gdbarch, w->hw_wp_mask);
10388 uiout->text ("\n");
9c06b0b4
TJB
10389}
10390
10391/* Implement the "print_mention" breakpoint_ops method for
10392 masked hardware watchpoints. */
10393
10394static void
10395print_mention_masked_watchpoint (struct breakpoint *b)
10396{
3a5c3e22 10397 struct watchpoint *w = (struct watchpoint *) b;
79a45e25 10398 struct ui_out *uiout = current_uiout;
46b9c129 10399 const char *tuple_name;
9c06b0b4
TJB
10400
10401 switch (b->type)
10402 {
10403 case bp_hardware_watchpoint:
112e8700 10404 uiout->text ("Masked hardware watchpoint ");
46b9c129 10405 tuple_name = "wpt";
9c06b0b4
TJB
10406 break;
10407 case bp_read_watchpoint:
112e8700 10408 uiout->text ("Masked hardware read watchpoint ");
46b9c129 10409 tuple_name = "hw-rwpt";
9c06b0b4
TJB
10410 break;
10411 case bp_access_watchpoint:
112e8700 10412 uiout->text ("Masked hardware access (read/write) watchpoint ");
46b9c129 10413 tuple_name = "hw-awpt";
9c06b0b4
TJB
10414 break;
10415 default:
10416 internal_error (__FILE__, __LINE__,
10417 _("Invalid hardware watchpoint type."));
10418 }
10419
46b9c129 10420 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
381befee 10421 uiout->field_signed ("number", b->number);
112e8700
SM
10422 uiout->text (": ");
10423 uiout->field_string ("exp", w->exp_string);
9c06b0b4
TJB
10424}
10425
10426/* Implement the "print_recreate" breakpoint_ops method for
10427 masked hardware watchpoints. */
10428
10429static void
10430print_recreate_masked_watchpoint (struct breakpoint *b, struct ui_file *fp)
10431{
3a5c3e22 10432 struct watchpoint *w = (struct watchpoint *) b;
9c06b0b4
TJB
10433 char tmp[40];
10434
10435 switch (b->type)
10436 {
10437 case bp_hardware_watchpoint:
10438 fprintf_unfiltered (fp, "watch");
10439 break;
10440 case bp_read_watchpoint:
10441 fprintf_unfiltered (fp, "rwatch");
10442 break;
10443 case bp_access_watchpoint:
10444 fprintf_unfiltered (fp, "awatch");
10445 break;
10446 default:
10447 internal_error (__FILE__, __LINE__,
10448 _("Invalid hardware watchpoint type."));
10449 }
10450
3a5c3e22
PA
10451 sprintf_vma (tmp, w->hw_wp_mask);
10452 fprintf_unfiltered (fp, " %s mask 0x%s", w->exp_string, tmp);
d9b3f62e 10453 print_recreate_thread (b, fp);
9c06b0b4
TJB
10454}
10455
10456/* The breakpoint_ops structure to be used in masked hardware watchpoints. */
10457
2060206e 10458static struct breakpoint_ops masked_watchpoint_breakpoint_ops;
9c06b0b4
TJB
10459
10460/* Tell whether the given watchpoint is a masked hardware watchpoint. */
10461
f2478a7e 10462static bool
9c06b0b4
TJB
10463is_masked_watchpoint (const struct breakpoint *b)
10464{
10465 return b->ops == &masked_watchpoint_breakpoint_ops;
10466}
10467
53a5351d
JM
10468/* accessflag: hw_write: watch write,
10469 hw_read: watch read,
10470 hw_access: watch access (read or write) */
c906108c 10471static void
bbc13ae3 10472watch_command_1 (const char *arg, int accessflag, int from_tty,
84f4c1fe 10473 int just_location, int internal)
c906108c 10474{
c1fc2657 10475 struct breakpoint *scope_breakpoint = NULL;
270140bd 10476 const struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
b926417a 10477 struct value *result;
bb9d5f81 10478 int saved_bitpos = 0, saved_bitsize = 0;
bbc13ae3
KS
10479 const char *exp_start = NULL;
10480 const char *exp_end = NULL;
10481 const char *tok, *end_tok;
9c06b0b4 10482 int toklen = -1;
bbc13ae3
KS
10483 const char *cond_start = NULL;
10484 const char *cond_end = NULL;
c906108c 10485 enum bptype bp_type;
37e4754d 10486 int thread = -1;
0cf6dd15 10487 int pc = 0;
9c06b0b4
TJB
10488 /* Flag to indicate whether we are going to use masks for
10489 the hardware watchpoint. */
10490 int use_mask = 0;
10491 CORE_ADDR mask = 0;
c906108c 10492
37e4754d
LM
10493 /* Make sure that we actually have parameters to parse. */
10494 if (arg != NULL && arg[0] != '\0')
10495 {
bbc13ae3
KS
10496 const char *value_start;
10497
10498 exp_end = arg + strlen (arg);
37e4754d 10499
9c06b0b4
TJB
10500 /* Look for "parameter value" pairs at the end
10501 of the arguments string. */
bbc13ae3 10502 for (tok = exp_end - 1; tok > arg; tok--)
9c06b0b4
TJB
10503 {
10504 /* Skip whitespace at the end of the argument list. */
10505 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10506 tok--;
10507
10508 /* Find the beginning of the last token.
10509 This is the value of the parameter. */
10510 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10511 tok--;
10512 value_start = tok + 1;
10513
10514 /* Skip whitespace. */
10515 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10516 tok--;
10517
10518 end_tok = tok;
10519
10520 /* Find the beginning of the second to last token.
10521 This is the parameter itself. */
10522 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10523 tok--;
10524 tok++;
10525 toklen = end_tok - tok + 1;
10526
61012eef 10527 if (toklen == 6 && startswith (tok, "thread"))
9c06b0b4 10528 {
5d5658a1 10529 struct thread_info *thr;
9c06b0b4
TJB
10530 /* At this point we've found a "thread" token, which means
10531 the user is trying to set a watchpoint that triggers
10532 only in a specific thread. */
5d5658a1 10533 const char *endp;
37e4754d 10534
9c06b0b4
TJB
10535 if (thread != -1)
10536 error(_("You can specify only one thread."));
37e4754d 10537
9c06b0b4 10538 /* Extract the thread ID from the next token. */
5d5658a1 10539 thr = parse_thread_id (value_start, &endp);
37e4754d 10540
5d5658a1 10541 /* Check if the user provided a valid thread ID. */
9c06b0b4 10542 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
5d5658a1 10543 invalid_thread_id_error (value_start);
9c06b0b4 10544
5d5658a1 10545 thread = thr->global_num;
9c06b0b4 10546 }
61012eef 10547 else if (toklen == 4 && startswith (tok, "mask"))
9c06b0b4
TJB
10548 {
10549 /* We've found a "mask" token, which means the user wants to
10550 create a hardware watchpoint that is going to have the mask
10551 facility. */
10552 struct value *mask_value, *mark;
37e4754d 10553
9c06b0b4
TJB
10554 if (use_mask)
10555 error(_("You can specify only one mask."));
37e4754d 10556
9c06b0b4 10557 use_mask = just_location = 1;
37e4754d 10558
9c06b0b4
TJB
10559 mark = value_mark ();
10560 mask_value = parse_to_comma_and_eval (&value_start);
10561 mask = value_as_address (mask_value);
10562 value_free_to_mark (mark);
10563 }
10564 else
10565 /* We didn't recognize what we found. We should stop here. */
10566 break;
37e4754d 10567
9c06b0b4
TJB
10568 /* Truncate the string and get rid of the "parameter value" pair before
10569 the arguments string is parsed by the parse_exp_1 function. */
bbc13ae3 10570 exp_end = tok;
9c06b0b4 10571 }
37e4754d 10572 }
bbc13ae3
KS
10573 else
10574 exp_end = arg;
37e4754d 10575
bbc13ae3
KS
10576 /* Parse the rest of the arguments. From here on out, everything
10577 is in terms of a newly allocated string instead of the original
10578 ARG. */
81b1e71c
TT
10579 std::string expression (arg, exp_end - arg);
10580 exp_start = arg = expression.c_str ();
699bd4cf
TT
10581 innermost_block_tracker tracker;
10582 expression_up exp = parse_exp_1 (&arg, 0, 0, 0, &tracker);
c906108c 10583 exp_end = arg;
fa8a61dc
TT
10584 /* Remove trailing whitespace from the expression before saving it.
10585 This makes the eventual display of the expression string a bit
10586 prettier. */
10587 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
10588 --exp_end;
10589
65d79d4b 10590 /* Checking if the expression is not constant. */
4d01a485 10591 if (watchpoint_exp_is_const (exp.get ()))
65d79d4b
SDJ
10592 {
10593 int len;
10594
10595 len = exp_end - exp_start;
10596 while (len > 0 && isspace (exp_start[len - 1]))
10597 len--;
10598 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
10599 }
10600
699bd4cf 10601 exp_valid_block = tracker.block ();
b926417a 10602 struct value *mark = value_mark ();
850645cf
TT
10603 struct value *val_as_value = nullptr;
10604 fetch_subexp_value (exp.get (), &pc, &val_as_value, &result, NULL,
10605 just_location);
06a64a0b 10606
850645cf 10607 if (val_as_value != NULL && just_location)
bb9d5f81 10608 {
850645cf
TT
10609 saved_bitpos = value_bitpos (val_as_value);
10610 saved_bitsize = value_bitsize (val_as_value);
bb9d5f81
PP
10611 }
10612
850645cf 10613 value_ref_ptr val;
06a64a0b
TT
10614 if (just_location)
10615 {
9c06b0b4
TJB
10616 int ret;
10617
06a64a0b 10618 exp_valid_block = NULL;
850645cf 10619 val = release_value (value_addr (result));
06a64a0b 10620 value_free_to_mark (mark);
9c06b0b4
TJB
10621
10622 if (use_mask)
10623 {
850645cf 10624 ret = target_masked_watch_num_registers (value_as_address (val.get ()),
9c06b0b4
TJB
10625 mask);
10626 if (ret == -1)
10627 error (_("This target does not support masked watchpoints."));
10628 else if (ret == -2)
10629 error (_("Invalid mask or memory region."));
10630 }
06a64a0b 10631 }
850645cf
TT
10632 else if (val_as_value != NULL)
10633 val = release_value (val_as_value);
c906108c 10634
f1735a53
TT
10635 tok = skip_spaces (arg);
10636 end_tok = skip_to_space (tok);
c906108c
SS
10637
10638 toklen = end_tok - tok;
10639 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
10640 {
10641 tok = cond_start = end_tok + 1;
699bd4cf
TT
10642 innermost_block_tracker if_tracker;
10643 parse_exp_1 (&tok, 0, 0, 0, &if_tracker);
60e1c644
PA
10644
10645 /* The watchpoint expression may not be local, but the condition
10646 may still be. E.g.: `watch global if local > 0'. */
699bd4cf 10647 cond_exp_valid_block = if_tracker.block ();
60e1c644 10648
c906108c
SS
10649 cond_end = tok;
10650 }
10651 if (*tok)
8a3fe4f8 10652 error (_("Junk at end of command."));
c906108c 10653
441d7c93
PA
10654 frame_info *wp_frame = block_innermost_frame (exp_valid_block);
10655
10656 /* Save this because create_internal_breakpoint below invalidates
10657 'wp_frame'. */
10658 frame_id watchpoint_frame = get_frame_id (wp_frame);
d983da9c
DJ
10659
10660 /* If the expression is "local", then set up a "watchpoint scope"
10661 breakpoint at the point where we've left the scope of the watchpoint
10662 expression. Create the scope breakpoint before the watchpoint, so
10663 that we will encounter it first in bpstat_stop_status. */
441d7c93 10664 if (exp_valid_block != NULL && wp_frame != NULL)
d983da9c 10665 {
441d7c93
PA
10666 frame_id caller_frame_id = frame_unwind_caller_id (wp_frame);
10667
10668 if (frame_id_p (caller_frame_id))
edb3359d 10669 {
441d7c93
PA
10670 gdbarch *caller_arch = frame_unwind_caller_arch (wp_frame);
10671 CORE_ADDR caller_pc = frame_unwind_caller_pc (wp_frame);
10672
edb3359d 10673 scope_breakpoint
441d7c93 10674 = create_internal_breakpoint (caller_arch, caller_pc,
06edf0c0
PA
10675 bp_watchpoint_scope,
10676 &momentary_breakpoint_ops);
d983da9c 10677
441d7c93
PA
10678 /* create_internal_breakpoint could invalidate WP_FRAME. */
10679 wp_frame = NULL;
10680
edb3359d 10681 scope_breakpoint->enable_state = bp_enabled;
d983da9c 10682
edb3359d
DJ
10683 /* Automatically delete the breakpoint when it hits. */
10684 scope_breakpoint->disposition = disp_del;
d983da9c 10685
edb3359d 10686 /* Only break in the proper frame (help with recursion). */
441d7c93 10687 scope_breakpoint->frame_id = caller_frame_id;
d983da9c 10688
edb3359d 10689 /* Set the address at which we will stop. */
441d7c93
PA
10690 scope_breakpoint->loc->gdbarch = caller_arch;
10691 scope_breakpoint->loc->requested_address = caller_pc;
edb3359d 10692 scope_breakpoint->loc->address
a6d9a66e
UW
10693 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
10694 scope_breakpoint->loc->requested_address,
edb3359d
DJ
10695 scope_breakpoint->type);
10696 }
d983da9c
DJ
10697 }
10698
e8369a73
AB
10699 /* Now set up the breakpoint. We create all watchpoints as hardware
10700 watchpoints here even if hardware watchpoints are turned off, a call
10701 to update_watchpoint later in this function will cause the type to
10702 drop back to bp_watchpoint (software watchpoint) if required. */
10703
10704 if (accessflag == hw_read)
10705 bp_type = bp_read_watchpoint;
10706 else if (accessflag == hw_access)
10707 bp_type = bp_access_watchpoint;
10708 else
10709 bp_type = bp_hardware_watchpoint;
3a5c3e22 10710
b270e6f9 10711 std::unique_ptr<watchpoint> w (new watchpoint ());
c1fc2657 10712
348d480f 10713 if (use_mask)
b270e6f9 10714 init_raw_breakpoint_without_location (w.get (), NULL, bp_type,
3a5c3e22 10715 &masked_watchpoint_breakpoint_ops);
348d480f 10716 else
b270e6f9 10717 init_raw_breakpoint_without_location (w.get (), NULL, bp_type,
3a5c3e22 10718 &watchpoint_breakpoint_ops);
c1fc2657
SM
10719 w->thread = thread;
10720 w->disposition = disp_donttouch;
10721 w->pspace = current_program_space;
b22e99fd 10722 w->exp = std::move (exp);
3a5c3e22
PA
10723 w->exp_valid_block = exp_valid_block;
10724 w->cond_exp_valid_block = cond_exp_valid_block;
06a64a0b
TT
10725 if (just_location)
10726 {
850645cf
TT
10727 struct type *t = value_type (val.get ());
10728 CORE_ADDR addr = value_as_address (val.get ());
06a64a0b 10729
43cc5389
TT
10730 w->exp_string_reparse
10731 = current_language->la_watch_location_expression (t, addr).release ();
06a64a0b 10732
3a5c3e22 10733 w->exp_string = xstrprintf ("-location %.*s",
d63d0675 10734 (int) (exp_end - exp_start), exp_start);
06a64a0b
TT
10735 }
10736 else
3a5c3e22 10737 w->exp_string = savestring (exp_start, exp_end - exp_start);
9c06b0b4
TJB
10738
10739 if (use_mask)
10740 {
3a5c3e22 10741 w->hw_wp_mask = mask;
9c06b0b4
TJB
10742 }
10743 else
10744 {
3a5c3e22 10745 w->val = val;
bb9d5f81
PP
10746 w->val_bitpos = saved_bitpos;
10747 w->val_bitsize = saved_bitsize;
4c1d86d9 10748 w->val_valid = true;
9c06b0b4 10749 }
77b06cd7 10750
c906108c 10751 if (cond_start)
c1fc2657 10752 w->cond_string = savestring (cond_start, cond_end - cond_start);
c906108c 10753 else
c1fc2657 10754 w->cond_string = 0;
c5aa993b 10755
441d7c93 10756 if (frame_id_p (watchpoint_frame))
f6bc2008 10757 {
441d7c93 10758 w->watchpoint_frame = watchpoint_frame;
3a5c3e22 10759 w->watchpoint_thread = inferior_ptid;
f6bc2008 10760 }
c906108c 10761 else
f6bc2008 10762 {
3a5c3e22
PA
10763 w->watchpoint_frame = null_frame_id;
10764 w->watchpoint_thread = null_ptid;
f6bc2008 10765 }
c906108c 10766
d983da9c 10767 if (scope_breakpoint != NULL)
c906108c 10768 {
d983da9c
DJ
10769 /* The scope breakpoint is related to the watchpoint. We will
10770 need to act on them together. */
c1fc2657 10771 w->related_breakpoint = scope_breakpoint;
b270e6f9 10772 scope_breakpoint->related_breakpoint = w.get ();
c906108c 10773 }
d983da9c 10774
06a64a0b
TT
10775 if (!just_location)
10776 value_free_to_mark (mark);
2d134ed3 10777
b270e6f9
TT
10778 /* Finally update the new watchpoint. This creates the locations
10779 that should be inserted. */
10780 update_watchpoint (w.get (), 1);
a9634178 10781
b270e6f9 10782 install_breakpoint (internal, std::move (w), 1);
c906108c
SS
10783}
10784
e09342b5 10785/* Return count of debug registers needed to watch the given expression.
e09342b5 10786 If the watchpoint cannot be handled in hardware return zero. */
c906108c 10787
c906108c 10788static int
a6535de1 10789can_use_hardware_watchpoint (const std::vector<value_ref_ptr> &vals)
c906108c
SS
10790{
10791 int found_memory_cnt = 0;
10792
10793 /* Did the user specifically forbid us to use hardware watchpoints? */
c5aa993b 10794 if (!can_use_hw_watchpoints)
c906108c 10795 return 0;
c5aa993b 10796
a6535de1
TT
10797 gdb_assert (!vals.empty ());
10798 struct value *head = vals[0].get ();
10799
5c44784c
JM
10800 /* Make sure that the value of the expression depends only upon
10801 memory contents, and values computed from them within GDB. If we
10802 find any register references or function calls, we can't use a
10803 hardware watchpoint.
10804
10805 The idea here is that evaluating an expression generates a series
10806 of values, one holding the value of every subexpression. (The
10807 expression a*b+c has five subexpressions: a, b, a*b, c, and
10808 a*b+c.) GDB's values hold almost enough information to establish
10809 the criteria given above --- they identify memory lvalues,
10810 register lvalues, computed values, etcetera. So we can evaluate
10811 the expression, and then scan the chain of values that leaves
10812 behind to decide whether we can detect any possible change to the
10813 expression's final value using only hardware watchpoints.
10814
10815 However, I don't think that the values returned by inferior
10816 function calls are special in any way. So this function may not
10817 notice that an expression involving an inferior function call
10818 can't be watched with hardware watchpoints. FIXME. */
a6535de1 10819 for (const value_ref_ptr &iter : vals)
c906108c 10820 {
a6535de1
TT
10821 struct value *v = iter.get ();
10822
5c44784c 10823 if (VALUE_LVAL (v) == lval_memory)
c906108c 10824 {
8464be76
DJ
10825 if (v != head && value_lazy (v))
10826 /* A lazy memory lvalue in the chain is one that GDB never
10827 needed to fetch; we either just used its address (e.g.,
10828 `a' in `a.b') or we never needed it at all (e.g., `a'
10829 in `a,b'). This doesn't apply to HEAD; if that is
10830 lazy then it was not readable, but watch it anyway. */
5c44784c 10831 ;
53a5351d 10832 else
5c44784c
JM
10833 {
10834 /* Ahh, memory we actually used! Check if we can cover
10835 it with hardware watchpoints. */
df407dfe 10836 struct type *vtype = check_typedef (value_type (v));
2e70b7b9
MS
10837
10838 /* We only watch structs and arrays if user asked for it
10839 explicitly, never if they just happen to appear in a
10840 middle of some value chain. */
10841 if (v == head
10842 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
10843 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
10844 {
42ae5230 10845 CORE_ADDR vaddr = value_address (v);
e09342b5
TJB
10846 int len;
10847 int num_regs;
10848
a9634178 10849 len = (target_exact_watchpoints
e09342b5
TJB
10850 && is_scalar_type_recursive (vtype))?
10851 1 : TYPE_LENGTH (value_type (v));
2e70b7b9 10852
e09342b5
TJB
10853 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
10854 if (!num_regs)
2e70b7b9
MS
10855 return 0;
10856 else
e09342b5 10857 found_memory_cnt += num_regs;
2e70b7b9 10858 }
5c44784c 10859 }
c5aa993b 10860 }
5086187c
AC
10861 else if (VALUE_LVAL (v) != not_lval
10862 && deprecated_value_modifiable (v) == 0)
38b6c3b3 10863 return 0; /* These are values from the history (e.g., $1). */
5086187c 10864 else if (VALUE_LVAL (v) == lval_register)
38b6c3b3 10865 return 0; /* Cannot watch a register with a HW watchpoint. */
c906108c
SS
10866 }
10867
10868 /* The expression itself looks suitable for using a hardware
10869 watchpoint, but give the target machine a chance to reject it. */
10870 return found_memory_cnt;
10871}
10872
8b93c638 10873void
f2fc3015 10874watch_command_wrapper (const char *arg, int from_tty, int internal)
8b93c638 10875{
84f4c1fe 10876 watch_command_1 (arg, hw_write, from_tty, 0, internal);
06a64a0b
TT
10877}
10878
06a64a0b
TT
10879/* A helper function that looks for the "-location" argument and then
10880 calls watch_command_1. */
10881
10882static void
0b39b52e 10883watch_maybe_just_location (const char *arg, int accessflag, int from_tty)
06a64a0b
TT
10884{
10885 int just_location = 0;
10886
10887 if (arg
10888 && (check_for_argument (&arg, "-location", sizeof ("-location") - 1)
10889 || check_for_argument (&arg, "-l", sizeof ("-l") - 1)))
cbba3ecd 10890 just_location = 1;
06a64a0b 10891
84f4c1fe 10892 watch_command_1 (arg, accessflag, from_tty, just_location, 0);
8b93c638 10893}
8926118c 10894
c5aa993b 10895static void
0b39b52e 10896watch_command (const char *arg, int from_tty)
c906108c 10897{
06a64a0b 10898 watch_maybe_just_location (arg, hw_write, from_tty);
c906108c
SS
10899}
10900
8b93c638 10901void
f2fc3015 10902rwatch_command_wrapper (const char *arg, int from_tty, int internal)
8b93c638 10903{
84f4c1fe 10904 watch_command_1 (arg, hw_read, from_tty, 0, internal);
8b93c638 10905}
8926118c 10906
c5aa993b 10907static void
0b39b52e 10908rwatch_command (const char *arg, int from_tty)
c906108c 10909{
06a64a0b 10910 watch_maybe_just_location (arg, hw_read, from_tty);
c906108c
SS
10911}
10912
8b93c638 10913void
f2fc3015 10914awatch_command_wrapper (const char *arg, int from_tty, int internal)
8b93c638 10915{
84f4c1fe 10916 watch_command_1 (arg, hw_access, from_tty, 0, internal);
8b93c638 10917}
8926118c 10918
c5aa993b 10919static void
0b39b52e 10920awatch_command (const char *arg, int from_tty)
c906108c 10921{
06a64a0b 10922 watch_maybe_just_location (arg, hw_access, from_tty);
c906108c 10923}
c906108c 10924\f
c5aa993b 10925
cfc31633
PA
10926/* Data for the FSM that manages the until(location)/advance commands
10927 in infcmd.c. Here because it uses the mechanisms of
10928 breakpoints. */
c906108c 10929
46e3ed7f 10930struct until_break_fsm : public thread_fsm
bfec99b2 10931{
46e3ed7f 10932 /* The thread that was current when the command was executed. */
cfc31633
PA
10933 int thread;
10934
10935 /* The breakpoint set at the destination location. */
46e3ed7f 10936 breakpoint_up location_breakpoint;
cfc31633
PA
10937
10938 /* Breakpoint set at the return address in the caller frame. May be
10939 NULL. */
46e3ed7f 10940 breakpoint_up caller_breakpoint;
cfc31633 10941
46e3ed7f
TT
10942 until_break_fsm (struct interp *cmd_interp, int thread,
10943 breakpoint_up &&location_breakpoint,
10944 breakpoint_up &&caller_breakpoint)
10945 : thread_fsm (cmd_interp),
10946 thread (thread),
10947 location_breakpoint (std::move (location_breakpoint)),
10948 caller_breakpoint (std::move (caller_breakpoint))
10949 {
10950 }
cfc31633 10951
46e3ed7f
TT
10952 void clean_up (struct thread_info *thread) override;
10953 bool should_stop (struct thread_info *thread) override;
10954 enum async_reply_reason do_async_reply_reason () override;
cfc31633
PA
10955};
10956
cfc31633
PA
10957/* Implementation of the 'should_stop' FSM method for the
10958 until(location)/advance commands. */
10959
46e3ed7f
TT
10960bool
10961until_break_fsm::should_stop (struct thread_info *tp)
cfc31633 10962{
cfc31633 10963 if (bpstat_find_breakpoint (tp->control.stop_bpstat,
46e3ed7f
TT
10964 location_breakpoint.get ()) != NULL
10965 || (caller_breakpoint != NULL
cfc31633 10966 && bpstat_find_breakpoint (tp->control.stop_bpstat,
46e3ed7f
TT
10967 caller_breakpoint.get ()) != NULL))
10968 set_finished ();
cfc31633 10969
46e3ed7f 10970 return true;
cfc31633
PA
10971}
10972
10973/* Implementation of the 'clean_up' FSM method for the
10974 until(location)/advance commands. */
10975
46e3ed7f
TT
10976void
10977until_break_fsm::clean_up (struct thread_info *)
43ff13b4 10978{
cfc31633 10979 /* Clean up our temporary breakpoints. */
46e3ed7f
TT
10980 location_breakpoint.reset ();
10981 caller_breakpoint.reset ();
10982 delete_longjmp_breakpoint (thread);
cfc31633
PA
10983}
10984
10985/* Implementation of the 'async_reply_reason' FSM method for the
10986 until(location)/advance commands. */
10987
46e3ed7f
TT
10988enum async_reply_reason
10989until_break_fsm::do_async_reply_reason ()
cfc31633
PA
10990{
10991 return EXEC_ASYNC_LOCATION_REACHED;
43ff13b4
JM
10992}
10993
c906108c 10994void
f2fc3015 10995until_break_command (const char *arg, int from_tty, int anywhere)
c906108c 10996{
8556afb4
PA
10997 struct frame_info *frame;
10998 struct gdbarch *frame_gdbarch;
10999 struct frame_id stack_frame_id;
11000 struct frame_id caller_frame_id;
186c406b
TT
11001 int thread;
11002 struct thread_info *tp;
c906108c 11003
70509625 11004 clear_proceed_status (0);
c906108c
SS
11005
11006 /* Set a breakpoint where the user wants it and at return from
4a64f543 11007 this function. */
c5aa993b 11008
ffc2605c 11009 event_location_up location = string_to_event_location (&arg, current_language);
f00aae0f 11010
6c5b2ebe
PA
11011 std::vector<symtab_and_line> sals
11012 = (last_displayed_sal_is_valid ()
11013 ? decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
11014 get_last_displayed_symtab (),
11015 get_last_displayed_line ())
11016 : decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE,
cafb3438 11017 NULL, NULL, 0));
c5aa993b 11018
6c5b2ebe 11019 if (sals.size () != 1)
8a3fe4f8 11020 error (_("Couldn't get information on specified line."));
c5aa993b 11021
6c5b2ebe 11022 symtab_and_line &sal = sals[0];
c5aa993b 11023
c906108c 11024 if (*arg)
8a3fe4f8 11025 error (_("Junk at end of arguments."));
c5aa993b 11026
c906108c 11027 resolve_sal_pc (&sal);
c5aa993b 11028
186c406b 11029 tp = inferior_thread ();
5d5658a1 11030 thread = tp->global_num;
186c406b 11031
8556afb4
PA
11032 /* Note linespec handling above invalidates the frame chain.
11033 Installing a breakpoint also invalidates the frame chain (as it
11034 may need to switch threads), so do any frame handling before
11035 that. */
11036
11037 frame = get_selected_frame (NULL);
11038 frame_gdbarch = get_frame_arch (frame);
11039 stack_frame_id = get_stack_frame_id (frame);
11040 caller_frame_id = frame_unwind_caller_id (frame);
883bc8d1 11041
ae66c1fc
EZ
11042 /* Keep within the current frame, or in frames called by the current
11043 one. */
edb3359d 11044
454dafbd 11045 breakpoint_up caller_breakpoint;
5419bdae
TT
11046
11047 gdb::optional<delete_longjmp_breakpoint_cleanup> lj_deleter;
11048
883bc8d1 11049 if (frame_id_p (caller_frame_id))
c906108c 11050 {
883bc8d1 11051 struct symtab_and_line sal2;
cfc31633 11052 struct gdbarch *caller_gdbarch;
883bc8d1
PA
11053
11054 sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
11055 sal2.pc = frame_unwind_caller_pc (frame);
cfc31633
PA
11056 caller_gdbarch = frame_unwind_caller_arch (frame);
11057 caller_breakpoint = set_momentary_breakpoint (caller_gdbarch,
11058 sal2,
11059 caller_frame_id,
11060 bp_until);
186c406b 11061
883bc8d1 11062 set_longjmp_breakpoint (tp, caller_frame_id);
5419bdae 11063 lj_deleter.emplace (thread);
c906108c 11064 }
c5aa993b 11065
c70a6932
JK
11066 /* set_momentary_breakpoint could invalidate FRAME. */
11067 frame = NULL;
11068
454dafbd 11069 breakpoint_up location_breakpoint;
883bc8d1
PA
11070 if (anywhere)
11071 /* If the user told us to continue until a specified location,
11072 we don't specify a frame at which we need to stop. */
cfc31633
PA
11073 location_breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11074 null_frame_id, bp_until);
883bc8d1
PA
11075 else
11076 /* Otherwise, specify the selected frame, because we want to stop
11077 only at the very same frame. */
cfc31633
PA
11078 location_breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11079 stack_frame_id, bp_until);
883bc8d1 11080
46e3ed7f
TT
11081 tp->thread_fsm = new until_break_fsm (command_interp (), tp->global_num,
11082 std::move (location_breakpoint),
11083 std::move (caller_breakpoint));
f107f563 11084
5419bdae
TT
11085 if (lj_deleter)
11086 lj_deleter->release ();
f107f563 11087
cfc31633 11088 proceed (-1, GDB_SIGNAL_DEFAULT);
c906108c 11089}
ae66c1fc 11090
c906108c
SS
11091/* This function attempts to parse an optional "if <cond>" clause
11092 from the arg string. If one is not found, it returns NULL.
c5aa993b 11093
c906108c
SS
11094 Else, it returns a pointer to the condition string. (It does not
11095 attempt to evaluate the string against a particular block.) And,
11096 it updates arg to point to the first character following the parsed
4a64f543 11097 if clause in the arg string. */
53a5351d 11098
63160a43
PA
11099const char *
11100ep_parse_optional_if_clause (const char **arg)
c906108c 11101{
63160a43 11102 const char *cond_string;
c5aa993b
JM
11103
11104 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
c906108c 11105 return NULL;
c5aa993b 11106
4a64f543 11107 /* Skip the "if" keyword. */
c906108c 11108 (*arg) += 2;
c5aa993b 11109
c906108c 11110 /* Skip any extra leading whitespace, and record the start of the
4a64f543 11111 condition string. */
f1735a53 11112 *arg = skip_spaces (*arg);
c906108c 11113 cond_string = *arg;
c5aa993b 11114
4a64f543
MS
11115 /* Assume that the condition occupies the remainder of the arg
11116 string. */
c906108c 11117 (*arg) += strlen (cond_string);
c5aa993b 11118
c906108c
SS
11119 return cond_string;
11120}
c5aa993b 11121
c906108c
SS
11122/* Commands to deal with catching events, such as signals, exceptions,
11123 process start/exit, etc. */
c5aa993b
JM
11124
11125typedef enum
11126{
44feb3ce
TT
11127 catch_fork_temporary, catch_vfork_temporary,
11128 catch_fork_permanent, catch_vfork_permanent
c5aa993b
JM
11129}
11130catch_fork_kind;
11131
c906108c 11132static void
eb4c3f4a 11133catch_fork_command_1 (const char *arg, int from_tty,
cc59ec59 11134 struct cmd_list_element *command)
c906108c 11135{
a6d9a66e 11136 struct gdbarch *gdbarch = get_current_arch ();
63160a43 11137 const char *cond_string = NULL;
44feb3ce
TT
11138 catch_fork_kind fork_kind;
11139 int tempflag;
11140
11141 fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
11142 tempflag = (fork_kind == catch_fork_temporary
11143 || fork_kind == catch_vfork_temporary);
c5aa993b 11144
44feb3ce
TT
11145 if (!arg)
11146 arg = "";
f1735a53 11147 arg = skip_spaces (arg);
c5aa993b 11148
c906108c 11149 /* The allowed syntax is:
c5aa993b
JM
11150 catch [v]fork
11151 catch [v]fork if <cond>
11152
4a64f543 11153 First, check if there's an if clause. */
c906108c 11154 cond_string = ep_parse_optional_if_clause (&arg);
c5aa993b 11155
c906108c 11156 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 11157 error (_("Junk at end of arguments."));
c5aa993b 11158
c906108c 11159 /* If this target supports it, create a fork or vfork catchpoint
4a64f543 11160 and enable reporting of such events. */
c5aa993b
JM
11161 switch (fork_kind)
11162 {
44feb3ce
TT
11163 case catch_fork_temporary:
11164 case catch_fork_permanent:
a6d9a66e 11165 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
ce78b96d 11166 &catch_fork_breakpoint_ops);
c906108c 11167 break;
44feb3ce
TT
11168 case catch_vfork_temporary:
11169 case catch_vfork_permanent:
a6d9a66e 11170 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
ce78b96d 11171 &catch_vfork_breakpoint_ops);
c906108c 11172 break;
c5aa993b 11173 default:
8a3fe4f8 11174 error (_("unsupported or unknown fork kind; cannot catch it"));
c906108c 11175 break;
c5aa993b 11176 }
c906108c
SS
11177}
11178
11179static void
eb4c3f4a 11180catch_exec_command_1 (const char *arg, int from_tty,
cc59ec59 11181 struct cmd_list_element *command)
c906108c 11182{
a6d9a66e 11183 struct gdbarch *gdbarch = get_current_arch ();
44feb3ce 11184 int tempflag;
63160a43 11185 const char *cond_string = NULL;
c906108c 11186
44feb3ce
TT
11187 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
11188
11189 if (!arg)
11190 arg = "";
f1735a53 11191 arg = skip_spaces (arg);
c906108c
SS
11192
11193 /* The allowed syntax is:
c5aa993b
JM
11194 catch exec
11195 catch exec if <cond>
c906108c 11196
4a64f543 11197 First, check if there's an if clause. */
c906108c
SS
11198 cond_string = ep_parse_optional_if_clause (&arg);
11199
11200 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 11201 error (_("Junk at end of arguments."));
c906108c 11202
b270e6f9
TT
11203 std::unique_ptr<exec_catchpoint> c (new exec_catchpoint ());
11204 init_catchpoint (c.get (), gdbarch, tempflag, cond_string,
b4d90040
PA
11205 &catch_exec_breakpoint_ops);
11206 c->exec_pathname = NULL;
11207
b270e6f9 11208 install_breakpoint (0, std::move (c), 1);
c906108c 11209}
c5aa993b 11210
9ac4176b 11211void
28010a5d
PA
11212init_ada_exception_breakpoint (struct breakpoint *b,
11213 struct gdbarch *gdbarch,
11214 struct symtab_and_line sal,
f2fc3015 11215 const char *addr_string,
c0a91b2b 11216 const struct breakpoint_ops *ops,
28010a5d 11217 int tempflag,
349774ef 11218 int enabled,
28010a5d 11219 int from_tty)
f7f9143b 11220{
f7f9143b
JB
11221 if (from_tty)
11222 {
5af949e3
UW
11223 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
11224 if (!loc_gdbarch)
11225 loc_gdbarch = gdbarch;
11226
6c95b8df
PA
11227 describe_other_breakpoints (loc_gdbarch,
11228 sal.pspace, sal.pc, sal.section, -1);
f7f9143b
JB
11229 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
11230 version for exception catchpoints, because two catchpoints
11231 used for different exception names will use the same address.
11232 In this case, a "breakpoint ... also set at..." warning is
4a64f543 11233 unproductive. Besides, the warning phrasing is also a bit
e5dd4106 11234 inappropriate, we should use the word catchpoint, and tell
f7f9143b
JB
11235 the user what type of catchpoint it is. The above is good
11236 enough for now, though. */
11237 }
11238
f06f1252 11239 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
f7f9143b 11240
349774ef 11241 b->enable_state = enabled ? bp_enabled : bp_disabled;
f7f9143b 11242 b->disposition = tempflag ? disp_del : disp_donttouch;
d28cd78a
TT
11243 b->location = string_to_event_location (&addr_string,
11244 language_def (language_ada));
f7f9143b 11245 b->language = language_ada;
f7f9143b
JB
11246}
11247
c906108c 11248static void
981a3fb3 11249catch_command (const char *arg, int from_tty)
c906108c 11250{
44feb3ce 11251 error (_("Catch requires an event name."));
c906108c
SS
11252}
11253\f
11254
11255static void
981a3fb3 11256tcatch_command (const char *arg, int from_tty)
c906108c 11257{
44feb3ce 11258 error (_("Catch requires an event name."));
c906108c
SS
11259}
11260
81b1e71c 11261/* Compare two breakpoints and return a strcmp-like result. */
8a2c437b
TT
11262
11263static int
81b1e71c 11264compare_breakpoints (const breakpoint *a, const breakpoint *b)
8a2c437b 11265{
81b1e71c
TT
11266 uintptr_t ua = (uintptr_t) a;
11267 uintptr_t ub = (uintptr_t) b;
8a2c437b 11268
81b1e71c 11269 if (a->number < b->number)
8a2c437b 11270 return -1;
81b1e71c 11271 else if (a->number > b->number)
8a2c437b
TT
11272 return 1;
11273
11274 /* Now sort by address, in case we see, e..g, two breakpoints with
11275 the number 0. */
11276 if (ua < ub)
11277 return -1;
94b0e70d 11278 return ua > ub ? 1 : 0;
8a2c437b
TT
11279}
11280
80f8a6eb 11281/* Delete breakpoints by address or line. */
c906108c
SS
11282
11283static void
0b39b52e 11284clear_command (const char *arg, int from_tty)
c906108c 11285{
81b1e71c 11286 struct breakpoint *b;
c906108c 11287 int default_match;
c906108c 11288
6c5b2ebe
PA
11289 std::vector<symtab_and_line> decoded_sals;
11290 symtab_and_line last_sal;
11291 gdb::array_view<symtab_and_line> sals;
c906108c
SS
11292 if (arg)
11293 {
6c5b2ebe
PA
11294 decoded_sals
11295 = decode_line_with_current_source (arg,
11296 (DECODE_LINE_FUNFIRSTLINE
11297 | DECODE_LINE_LIST_MODE));
c906108c 11298 default_match = 0;
6c5b2ebe 11299 sals = decoded_sals;
c906108c
SS
11300 }
11301 else
11302 {
1bfeeb0f
JL
11303 /* Set sal's line, symtab, pc, and pspace to the values
11304 corresponding to the last call to print_frame_info. If the
11305 codepoint is not valid, this will set all the fields to 0. */
51abb421 11306 last_sal = get_last_displayed_sal ();
6c5b2ebe 11307 if (last_sal.symtab == 0)
8a3fe4f8 11308 error (_("No source file specified."));
c906108c 11309
c906108c 11310 default_match = 1;
6c5b2ebe 11311 sals = last_sal;
c906108c
SS
11312 }
11313
4a64f543
MS
11314 /* We don't call resolve_sal_pc here. That's not as bad as it
11315 seems, because all existing breakpoints typically have both
11316 file/line and pc set. So, if clear is given file/line, we can
11317 match this to existing breakpoint without obtaining pc at all.
ed0616c6
VP
11318
11319 We only support clearing given the address explicitly
11320 present in breakpoint table. Say, we've set breakpoint
4a64f543 11321 at file:line. There were several PC values for that file:line,
ed0616c6 11322 due to optimization, all in one block.
4a64f543
MS
11323
11324 We've picked one PC value. If "clear" is issued with another
ed0616c6
VP
11325 PC corresponding to the same file:line, the breakpoint won't
11326 be cleared. We probably can still clear the breakpoint, but
11327 since the other PC value is never presented to user, user
11328 can only find it by guessing, and it does not seem important
11329 to support that. */
11330
4a64f543
MS
11331 /* For each line spec given, delete bps which correspond to it. Do
11332 it in two passes, solely to preserve the current behavior that
11333 from_tty is forced true if we delete more than one
11334 breakpoint. */
c906108c 11335
81b1e71c 11336 std::vector<struct breakpoint *> found;
6c5b2ebe 11337 for (const auto &sal : sals)
c906108c 11338 {
05cba821
JK
11339 const char *sal_fullname;
11340
c906108c 11341 /* If exact pc given, clear bpts at that pc.
c5aa993b
JM
11342 If line given (pc == 0), clear all bpts on specified line.
11343 If defaulting, clear all bpts on default line
c906108c 11344 or at default pc.
c5aa993b
JM
11345
11346 defaulting sal.pc != 0 tests to do
11347
11348 0 1 pc
11349 1 1 pc _and_ line
11350 0 0 line
11351 1 0 <can't happen> */
c906108c 11352
05cba821
JK
11353 sal_fullname = (sal.symtab == NULL
11354 ? NULL : symtab_to_fullname (sal.symtab));
c906108c 11355
4a64f543 11356 /* Find all matching breakpoints and add them to 'found'. */
d6e956e5 11357 ALL_BREAKPOINTS (b)
c5aa993b 11358 {
0d381245 11359 int match = 0;
4a64f543 11360 /* Are we going to delete b? */
cc60f2e3 11361 if (b->type != bp_none && !is_watchpoint (b))
0d381245
VP
11362 {
11363 struct bp_location *loc = b->loc;
11364 for (; loc; loc = loc->next)
11365 {
f8eba3c6
TT
11366 /* If the user specified file:line, don't allow a PC
11367 match. This matches historical gdb behavior. */
11368 int pc_match = (!sal.explicit_line
11369 && sal.pc
11370 && (loc->pspace == sal.pspace)
11371 && (loc->address == sal.pc)
11372 && (!section_is_overlay (loc->section)
11373 || loc->section == sal.section));
4aac40c8
TT
11374 int line_match = 0;
11375
11376 if ((default_match || sal.explicit_line)
2f202fde 11377 && loc->symtab != NULL
05cba821 11378 && sal_fullname != NULL
4aac40c8 11379 && sal.pspace == loc->pspace
05cba821
JK
11380 && loc->line_number == sal.line
11381 && filename_cmp (symtab_to_fullname (loc->symtab),
11382 sal_fullname) == 0)
11383 line_match = 1;
4aac40c8 11384
0d381245
VP
11385 if (pc_match || line_match)
11386 {
11387 match = 1;
11388 break;
11389 }
11390 }
11391 }
11392
11393 if (match)
81b1e71c 11394 found.push_back (b);
c906108c 11395 }
80f8a6eb 11396 }
8a2c437b 11397
80f8a6eb 11398 /* Now go thru the 'found' chain and delete them. */
81b1e71c 11399 if (found.empty ())
80f8a6eb
MS
11400 {
11401 if (arg)
8a3fe4f8 11402 error (_("No breakpoint at %s."), arg);
80f8a6eb 11403 else
8a3fe4f8 11404 error (_("No breakpoint at this line."));
80f8a6eb 11405 }
c906108c 11406
8a2c437b 11407 /* Remove duplicates from the vec. */
81b1e71c 11408 std::sort (found.begin (), found.end (),
b926417a 11409 [] (const breakpoint *bp_a, const breakpoint *bp_b)
81b1e71c 11410 {
b926417a 11411 return compare_breakpoints (bp_a, bp_b) < 0;
81b1e71c
TT
11412 });
11413 found.erase (std::unique (found.begin (), found.end (),
b926417a 11414 [] (const breakpoint *bp_a, const breakpoint *bp_b)
81b1e71c 11415 {
b926417a 11416 return compare_breakpoints (bp_a, bp_b) == 0;
81b1e71c
TT
11417 }),
11418 found.end ());
8a2c437b 11419
81b1e71c 11420 if (found.size () > 1)
4a64f543 11421 from_tty = 1; /* Always report if deleted more than one. */
80f8a6eb 11422 if (from_tty)
a3f17187 11423 {
81b1e71c 11424 if (found.size () == 1)
a3f17187
AC
11425 printf_unfiltered (_("Deleted breakpoint "));
11426 else
11427 printf_unfiltered (_("Deleted breakpoints "));
11428 }
d6e956e5 11429
81b1e71c 11430 for (breakpoint *iter : found)
80f8a6eb 11431 {
c5aa993b 11432 if (from_tty)
81b1e71c
TT
11433 printf_unfiltered ("%d ", iter->number);
11434 delete_breakpoint (iter);
c906108c 11435 }
80f8a6eb
MS
11436 if (from_tty)
11437 putchar_unfiltered ('\n');
c906108c
SS
11438}
11439\f
11440/* Delete breakpoint in BS if they are `delete' breakpoints and
11441 all breakpoints that are marked for deletion, whether hit or not.
11442 This is called after any breakpoint is hit, or after errors. */
11443
11444void
fba45db2 11445breakpoint_auto_delete (bpstat bs)
c906108c 11446{
35df4500 11447 struct breakpoint *b, *b_tmp;
c906108c
SS
11448
11449 for (; bs; bs = bs->next)
f431efe5
PA
11450 if (bs->breakpoint_at
11451 && bs->breakpoint_at->disposition == disp_del
c906108c 11452 && bs->stop)
f431efe5 11453 delete_breakpoint (bs->breakpoint_at);
c906108c 11454
35df4500 11455 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 11456 {
b5de0fa7 11457 if (b->disposition == disp_del_at_next_stop)
c5aa993b
JM
11458 delete_breakpoint (b);
11459 }
c906108c
SS
11460}
11461
4a64f543 11462/* A comparison function for bp_location AP and BP being interfaced to
39ef2f62 11463 std::sort. Sort elements primarily by their ADDRESS (no matter what
cb1e4e32
PA
11464 bl_address_is_meaningful says), secondarily by ordering first
11465 permanent elements and terciarily just ensuring the array is sorted
39ef2f62 11466 stable way despite std::sort being an unstable algorithm. */
876fa593
JK
11467
11468static int
39ef2f62 11469bp_location_is_less_than (const bp_location *a, const bp_location *b)
876fa593 11470{
876fa593 11471 if (a->address != b->address)
39ef2f62 11472 return a->address < b->address;
876fa593 11473
dea2aa5f
LM
11474 /* Sort locations at the same address by their pspace number, keeping
11475 locations of the same inferior (in a multi-inferior environment)
11476 grouped. */
11477
11478 if (a->pspace->num != b->pspace->num)
39ef2f62 11479 return a->pspace->num < b->pspace->num;
dea2aa5f 11480
876fa593 11481 /* Sort permanent breakpoints first. */
1a853c52 11482 if (a->permanent != b->permanent)
39ef2f62 11483 return a->permanent > b->permanent;
876fa593 11484
c56a97f9
JK
11485 /* Make the internal GDB representation stable across GDB runs
11486 where A and B memory inside GDB can differ. Breakpoint locations of
11487 the same type at the same address can be sorted in arbitrary order. */
876fa593
JK
11488
11489 if (a->owner->number != b->owner->number)
39ef2f62 11490 return a->owner->number < b->owner->number;
876fa593 11491
39ef2f62 11492 return a < b;
876fa593
JK
11493}
11494
f5336ca5
PA
11495/* Set bp_locations_placed_address_before_address_max and
11496 bp_locations_shadow_len_after_address_max according to the current
11497 content of the bp_locations array. */
f7545552
TT
11498
11499static void
f5336ca5 11500bp_locations_target_extensions_update (void)
f7545552 11501{
876fa593
JK
11502 struct bp_location *bl, **blp_tmp;
11503
f5336ca5
PA
11504 bp_locations_placed_address_before_address_max = 0;
11505 bp_locations_shadow_len_after_address_max = 0;
876fa593
JK
11506
11507 ALL_BP_LOCATIONS (bl, blp_tmp)
11508 {
11509 CORE_ADDR start, end, addr;
11510
11511 if (!bp_location_has_shadow (bl))
11512 continue;
11513
11514 start = bl->target_info.placed_address;
11515 end = start + bl->target_info.shadow_len;
11516
11517 gdb_assert (bl->address >= start);
11518 addr = bl->address - start;
f5336ca5
PA
11519 if (addr > bp_locations_placed_address_before_address_max)
11520 bp_locations_placed_address_before_address_max = addr;
876fa593
JK
11521
11522 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
11523
11524 gdb_assert (bl->address < end);
11525 addr = end - bl->address;
f5336ca5
PA
11526 if (addr > bp_locations_shadow_len_after_address_max)
11527 bp_locations_shadow_len_after_address_max = addr;
876fa593 11528 }
f7545552
TT
11529}
11530
1e4d1764
YQ
11531/* Download tracepoint locations if they haven't been. */
11532
11533static void
11534download_tracepoint_locations (void)
11535{
7ed2c994 11536 struct breakpoint *b;
dd2e65cc 11537 enum tribool can_download_tracepoint = TRIBOOL_UNKNOWN;
1e4d1764 11538
5ed8105e 11539 scoped_restore_current_pspace_and_thread restore_pspace_thread;
1e4d1764 11540
7ed2c994 11541 ALL_TRACEPOINTS (b)
1e4d1764 11542 {
7ed2c994 11543 struct bp_location *bl;
1e4d1764 11544 struct tracepoint *t;
f2a8bc8a 11545 int bp_location_downloaded = 0;
1e4d1764 11546
7ed2c994 11547 if ((b->type == bp_fast_tracepoint
1e4d1764
YQ
11548 ? !may_insert_fast_tracepoints
11549 : !may_insert_tracepoints))
11550 continue;
11551
dd2e65cc
YQ
11552 if (can_download_tracepoint == TRIBOOL_UNKNOWN)
11553 {
11554 if (target_can_download_tracepoint ())
11555 can_download_tracepoint = TRIBOOL_TRUE;
11556 else
11557 can_download_tracepoint = TRIBOOL_FALSE;
11558 }
11559
11560 if (can_download_tracepoint == TRIBOOL_FALSE)
11561 break;
11562
7ed2c994
YQ
11563 for (bl = b->loc; bl; bl = bl->next)
11564 {
11565 /* In tracepoint, locations are _never_ duplicated, so
11566 should_be_inserted is equivalent to
11567 unduplicated_should_be_inserted. */
11568 if (!should_be_inserted (bl) || bl->inserted)
11569 continue;
1e4d1764 11570
7ed2c994 11571 switch_to_program_space_and_thread (bl->pspace);
1e4d1764 11572
7ed2c994 11573 target_download_tracepoint (bl);
1e4d1764 11574
7ed2c994 11575 bl->inserted = 1;
f2a8bc8a 11576 bp_location_downloaded = 1;
7ed2c994
YQ
11577 }
11578 t = (struct tracepoint *) b;
11579 t->number_on_target = b->number;
f2a8bc8a 11580 if (bp_location_downloaded)
76727919 11581 gdb::observers::breakpoint_modified.notify (b);
1e4d1764 11582 }
1e4d1764
YQ
11583}
11584
934709f0
PW
11585/* Swap the insertion/duplication state between two locations. */
11586
11587static void
11588swap_insertion (struct bp_location *left, struct bp_location *right)
11589{
11590 const int left_inserted = left->inserted;
11591 const int left_duplicate = left->duplicate;
b775012e 11592 const int left_needs_update = left->needs_update;
934709f0
PW
11593 const struct bp_target_info left_target_info = left->target_info;
11594
1e4d1764
YQ
11595 /* Locations of tracepoints can never be duplicated. */
11596 if (is_tracepoint (left->owner))
11597 gdb_assert (!left->duplicate);
11598 if (is_tracepoint (right->owner))
11599 gdb_assert (!right->duplicate);
11600
934709f0
PW
11601 left->inserted = right->inserted;
11602 left->duplicate = right->duplicate;
b775012e 11603 left->needs_update = right->needs_update;
934709f0
PW
11604 left->target_info = right->target_info;
11605 right->inserted = left_inserted;
11606 right->duplicate = left_duplicate;
b775012e 11607 right->needs_update = left_needs_update;
934709f0
PW
11608 right->target_info = left_target_info;
11609}
11610
b775012e
LM
11611/* Force the re-insertion of the locations at ADDRESS. This is called
11612 once a new/deleted/modified duplicate location is found and we are evaluating
11613 conditions on the target's side. Such conditions need to be updated on
11614 the target. */
11615
11616static void
11617force_breakpoint_reinsertion (struct bp_location *bl)
11618{
11619 struct bp_location **locp = NULL, **loc2p;
11620 struct bp_location *loc;
11621 CORE_ADDR address = 0;
11622 int pspace_num;
11623
11624 address = bl->address;
11625 pspace_num = bl->pspace->num;
11626
11627 /* This is only meaningful if the target is
11628 evaluating conditions and if the user has
11629 opted for condition evaluation on the target's
11630 side. */
11631 if (gdb_evaluates_breakpoint_condition_p ()
11632 || !target_supports_evaluation_of_breakpoint_conditions ())
11633 return;
11634
11635 /* Flag all breakpoint locations with this address and
11636 the same program space as the location
11637 as "its condition has changed". We need to
11638 update the conditions on the target's side. */
11639 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, address)
11640 {
11641 loc = *loc2p;
11642
11643 if (!is_breakpoint (loc->owner)
11644 || pspace_num != loc->pspace->num)
11645 continue;
11646
11647 /* Flag the location appropriately. We use a different state to
11648 let everyone know that we already updated the set of locations
11649 with addr bl->address and program space bl->pspace. This is so
11650 we don't have to keep calling these functions just to mark locations
11651 that have already been marked. */
11652 loc->condition_changed = condition_updated;
11653
11654 /* Free the agent expression bytecode as well. We will compute
11655 it later on. */
833177a4 11656 loc->cond_bytecode.reset ();
b775012e
LM
11657 }
11658}
44702360
PA
11659/* Called whether new breakpoints are created, or existing breakpoints
11660 deleted, to update the global location list and recompute which
11661 locations are duplicate of which.
b775012e 11662
04086b45
PA
11663 The INSERT_MODE flag determines whether locations may not, may, or
11664 shall be inserted now. See 'enum ugll_insert_mode' for more
11665 info. */
b60e7edf 11666
0d381245 11667static void
44702360 11668update_global_location_list (enum ugll_insert_mode insert_mode)
0d381245 11669{
74960c60 11670 struct breakpoint *b;
876fa593 11671 struct bp_location **locp, *loc;
b775012e
LM
11672 /* Last breakpoint location address that was marked for update. */
11673 CORE_ADDR last_addr = 0;
11674 /* Last breakpoint location program space that was marked for update. */
11675 int last_pspace_num = -1;
f7545552 11676
2d134ed3
PA
11677 /* Used in the duplicates detection below. When iterating over all
11678 bp_locations, points to the first bp_location of a given address.
11679 Breakpoints and watchpoints of different types are never
11680 duplicates of each other. Keep one pointer for each type of
11681 breakpoint/watchpoint, so we only need to loop over all locations
11682 once. */
11683 struct bp_location *bp_loc_first; /* breakpoint */
11684 struct bp_location *wp_loc_first; /* hardware watchpoint */
11685 struct bp_location *awp_loc_first; /* access watchpoint */
11686 struct bp_location *rwp_loc_first; /* read watchpoint */
876fa593 11687
f5336ca5
PA
11688 /* Saved former bp_locations array which we compare against the newly
11689 built bp_locations from the current state of ALL_BREAKPOINTS. */
81b1e71c 11690 struct bp_location **old_locp;
f5336ca5 11691 unsigned old_locations_count;
81b1e71c 11692 gdb::unique_xmalloc_ptr<struct bp_location *> old_locations (bp_locations);
876fa593 11693
f5336ca5
PA
11694 old_locations_count = bp_locations_count;
11695 bp_locations = NULL;
11696 bp_locations_count = 0;
0d381245 11697
74960c60 11698 ALL_BREAKPOINTS (b)
876fa593 11699 for (loc = b->loc; loc; loc = loc->next)
f5336ca5 11700 bp_locations_count++;
876fa593 11701
f5336ca5
PA
11702 bp_locations = XNEWVEC (struct bp_location *, bp_locations_count);
11703 locp = bp_locations;
876fa593
JK
11704 ALL_BREAKPOINTS (b)
11705 for (loc = b->loc; loc; loc = loc->next)
11706 *locp++ = loc;
39ef2f62
CB
11707 std::sort (bp_locations, bp_locations + bp_locations_count,
11708 bp_location_is_less_than);
876fa593 11709
f5336ca5 11710 bp_locations_target_extensions_update ();
74960c60 11711
4a64f543
MS
11712 /* Identify bp_location instances that are no longer present in the
11713 new list, and therefore should be freed. Note that it's not
11714 necessary that those locations should be removed from inferior --
11715 if there's another location at the same address (previously
11716 marked as duplicate), we don't need to remove/insert the
11717 location.
876fa593 11718
4a64f543
MS
11719 LOCP is kept in sync with OLD_LOCP, each pointing to the current
11720 and former bp_location array state respectively. */
876fa593 11721
f5336ca5 11722 locp = bp_locations;
81b1e71c
TT
11723 for (old_locp = old_locations.get ();
11724 old_locp < old_locations.get () + old_locations_count;
876fa593 11725 old_locp++)
74960c60 11726 {
876fa593 11727 struct bp_location *old_loc = *old_locp;
c7d46a38 11728 struct bp_location **loc2p;
876fa593 11729
e5dd4106 11730 /* Tells if 'old_loc' is found among the new locations. If
4a64f543 11731 not, we have to free it. */
c7d46a38 11732 int found_object = 0;
20874c92
VP
11733 /* Tells if the location should remain inserted in the target. */
11734 int keep_in_target = 0;
11735 int removed = 0;
876fa593 11736
4a64f543
MS
11737 /* Skip LOCP entries which will definitely never be needed.
11738 Stop either at or being the one matching OLD_LOC. */
f5336ca5 11739 while (locp < bp_locations + bp_locations_count
c7d46a38 11740 && (*locp)->address < old_loc->address)
876fa593 11741 locp++;
c7d46a38
PA
11742
11743 for (loc2p = locp;
f5336ca5 11744 (loc2p < bp_locations + bp_locations_count
c7d46a38
PA
11745 && (*loc2p)->address == old_loc->address);
11746 loc2p++)
11747 {
b775012e
LM
11748 /* Check if this is a new/duplicated location or a duplicated
11749 location that had its condition modified. If so, we want to send
11750 its condition to the target if evaluation of conditions is taking
11751 place there. */
11752 if ((*loc2p)->condition_changed == condition_modified
11753 && (last_addr != old_loc->address
11754 || last_pspace_num != old_loc->pspace->num))
c7d46a38 11755 {
b775012e
LM
11756 force_breakpoint_reinsertion (*loc2p);
11757 last_pspace_num = old_loc->pspace->num;
c7d46a38 11758 }
b775012e
LM
11759
11760 if (*loc2p == old_loc)
11761 found_object = 1;
c7d46a38 11762 }
74960c60 11763
b775012e
LM
11764 /* We have already handled this address, update it so that we don't
11765 have to go through updates again. */
11766 last_addr = old_loc->address;
11767
11768 /* Target-side condition evaluation: Handle deleted locations. */
11769 if (!found_object)
11770 force_breakpoint_reinsertion (old_loc);
11771
4a64f543
MS
11772 /* If this location is no longer present, and inserted, look if
11773 there's maybe a new location at the same address. If so,
11774 mark that one inserted, and don't remove this one. This is
11775 needed so that we don't have a time window where a breakpoint
11776 at certain location is not inserted. */
74960c60 11777
876fa593 11778 if (old_loc->inserted)
0d381245 11779 {
4a64f543
MS
11780 /* If the location is inserted now, we might have to remove
11781 it. */
74960c60 11782
876fa593 11783 if (found_object && should_be_inserted (old_loc))
74960c60 11784 {
4a64f543
MS
11785 /* The location is still present in the location list,
11786 and still should be inserted. Don't do anything. */
20874c92 11787 keep_in_target = 1;
74960c60
VP
11788 }
11789 else
11790 {
b775012e
LM
11791 /* This location still exists, but it won't be kept in the
11792 target since it may have been disabled. We proceed to
11793 remove its target-side condition. */
11794
4a64f543
MS
11795 /* The location is either no longer present, or got
11796 disabled. See if there's another location at the
11797 same address, in which case we don't need to remove
11798 this one from the target. */
876fa593 11799
2bdf28a0 11800 /* OLD_LOC comes from existing struct breakpoint. */
cb1e4e32 11801 if (bl_address_is_meaningful (old_loc))
876fa593 11802 {
876fa593 11803 for (loc2p = locp;
f5336ca5 11804 (loc2p < bp_locations + bp_locations_count
c7d46a38 11805 && (*loc2p)->address == old_loc->address);
876fa593
JK
11806 loc2p++)
11807 {
11808 struct bp_location *loc2 = *loc2p;
11809
2d134ed3 11810 if (breakpoint_locations_match (loc2, old_loc))
c7d46a38 11811 {
85d721b8
PA
11812 /* Read watchpoint locations are switched to
11813 access watchpoints, if the former are not
11814 supported, but the latter are. */
11815 if (is_hardware_watchpoint (old_loc->owner))
11816 {
11817 gdb_assert (is_hardware_watchpoint (loc2->owner));
11818 loc2->watchpoint_type = old_loc->watchpoint_type;
11819 }
11820
934709f0
PW
11821 /* loc2 is a duplicated location. We need to check
11822 if it should be inserted in case it will be
11823 unduplicated. */
11824 if (loc2 != old_loc
11825 && unduplicated_should_be_inserted (loc2))
c7d46a38 11826 {
934709f0 11827 swap_insertion (old_loc, loc2);
c7d46a38
PA
11828 keep_in_target = 1;
11829 break;
11830 }
876fa593
JK
11831 }
11832 }
11833 }
74960c60
VP
11834 }
11835
20874c92
VP
11836 if (!keep_in_target)
11837 {
834c0d03 11838 if (remove_breakpoint (old_loc))
20874c92 11839 {
4a64f543
MS
11840 /* This is just about all we can do. We could keep
11841 this location on the global list, and try to
11842 remove it next time, but there's no particular
11843 reason why we will succeed next time.
20874c92 11844
4a64f543
MS
11845 Note that at this point, old_loc->owner is still
11846 valid, as delete_breakpoint frees the breakpoint
11847 only after calling us. */
3e43a32a
MS
11848 printf_filtered (_("warning: Error removing "
11849 "breakpoint %d\n"),
876fa593 11850 old_loc->owner->number);
20874c92
VP
11851 }
11852 removed = 1;
11853 }
0d381245 11854 }
74960c60
VP
11855
11856 if (!found_object)
1c5cfe86 11857 {
fbea99ea 11858 if (removed && target_is_non_stop_p ()
1cf4d951 11859 && need_moribund_for_location_type (old_loc))
20874c92 11860 {
db82e815
PA
11861 /* This location was removed from the target. In
11862 non-stop mode, a race condition is possible where
11863 we've removed a breakpoint, but stop events for that
11864 breakpoint are already queued and will arrive later.
11865 We apply an heuristic to be able to distinguish such
11866 SIGTRAPs from other random SIGTRAPs: we keep this
11867 breakpoint location for a bit, and will retire it
11868 after we see some number of events. The theory here
11869 is that reporting of events should, "on the average",
11870 be fair, so after a while we'll see events from all
11871 threads that have anything of interest, and no longer
11872 need to keep this breakpoint location around. We
11873 don't hold locations forever so to reduce chances of
11874 mistaking a non-breakpoint SIGTRAP for a breakpoint
11875 SIGTRAP.
11876
11877 The heuristic failing can be disastrous on
11878 decr_pc_after_break targets.
11879
11880 On decr_pc_after_break targets, like e.g., x86-linux,
11881 if we fail to recognize a late breakpoint SIGTRAP,
11882 because events_till_retirement has reached 0 too
11883 soon, we'll fail to do the PC adjustment, and report
11884 a random SIGTRAP to the user. When the user resumes
11885 the inferior, it will most likely immediately crash
2dec564e 11886 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
db82e815
PA
11887 corrupted, because of being resumed e.g., in the
11888 middle of a multi-byte instruction, or skipped a
11889 one-byte instruction. This was actually seen happen
11890 on native x86-linux, and should be less rare on
11891 targets that do not support new thread events, like
11892 remote, due to the heuristic depending on
11893 thread_count.
11894
11895 Mistaking a random SIGTRAP for a breakpoint trap
11896 causes similar symptoms (PC adjustment applied when
11897 it shouldn't), but then again, playing with SIGTRAPs
11898 behind the debugger's back is asking for trouble.
11899
11900 Since hardware watchpoint traps are always
11901 distinguishable from other traps, so we don't need to
11902 apply keep hardware watchpoint moribund locations
11903 around. We simply always ignore hardware watchpoint
11904 traps we can no longer explain. */
11905
876fa593
JK
11906 old_loc->events_till_retirement = 3 * (thread_count () + 1);
11907 old_loc->owner = NULL;
20874c92 11908
1123588c 11909 moribund_locations.push_back (old_loc);
1c5cfe86
PA
11910 }
11911 else
f431efe5
PA
11912 {
11913 old_loc->owner = NULL;
11914 decref_bp_location (&old_loc);
11915 }
20874c92 11916 }
74960c60 11917 }
1c5cfe86 11918
348d480f
PA
11919 /* Rescan breakpoints at the same address and section, marking the
11920 first one as "first" and any others as "duplicates". This is so
11921 that the bpt instruction is only inserted once. If we have a
11922 permanent breakpoint at the same place as BPT, make that one the
11923 official one, and the rest as duplicates. Permanent breakpoints
11924 are sorted first for the same address.
11925
11926 Do the same for hardware watchpoints, but also considering the
11927 watchpoint's type (regular/access/read) and length. */
11928
11929 bp_loc_first = NULL;
11930 wp_loc_first = NULL;
11931 awp_loc_first = NULL;
11932 rwp_loc_first = NULL;
11933 ALL_BP_LOCATIONS (loc, locp)
11934 {
11935 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
11936 non-NULL. */
348d480f 11937 struct bp_location **loc_first_p;
d3fbdd86 11938 b = loc->owner;
348d480f 11939
6f380991 11940 if (!unduplicated_should_be_inserted (loc)
cb1e4e32 11941 || !bl_address_is_meaningful (loc)
1e4d1764
YQ
11942 /* Don't detect duplicate for tracepoint locations because they are
11943 never duplicated. See the comments in field `duplicate' of
11944 `struct bp_location'. */
348d480f 11945 || is_tracepoint (b))
b775012e
LM
11946 {
11947 /* Clear the condition modification flag. */
11948 loc->condition_changed = condition_unchanged;
11949 continue;
11950 }
348d480f 11951
348d480f
PA
11952 if (b->type == bp_hardware_watchpoint)
11953 loc_first_p = &wp_loc_first;
11954 else if (b->type == bp_read_watchpoint)
11955 loc_first_p = &rwp_loc_first;
11956 else if (b->type == bp_access_watchpoint)
11957 loc_first_p = &awp_loc_first;
11958 else
11959 loc_first_p = &bp_loc_first;
11960
11961 if (*loc_first_p == NULL
11962 || (overlay_debugging && loc->section != (*loc_first_p)->section)
11963 || !breakpoint_locations_match (loc, *loc_first_p))
11964 {
11965 *loc_first_p = loc;
11966 loc->duplicate = 0;
b775012e
LM
11967
11968 if (is_breakpoint (loc->owner) && loc->condition_changed)
11969 {
11970 loc->needs_update = 1;
11971 /* Clear the condition modification flag. */
11972 loc->condition_changed = condition_unchanged;
11973 }
348d480f
PA
11974 continue;
11975 }
11976
934709f0
PW
11977
11978 /* This and the above ensure the invariant that the first location
11979 is not duplicated, and is the inserted one.
11980 All following are marked as duplicated, and are not inserted. */
11981 if (loc->inserted)
11982 swap_insertion (loc, *loc_first_p);
348d480f
PA
11983 loc->duplicate = 1;
11984
b775012e
LM
11985 /* Clear the condition modification flag. */
11986 loc->condition_changed = condition_unchanged;
348d480f
PA
11987 }
11988
a25a5a45 11989 if (insert_mode == UGLL_INSERT || breakpoints_should_be_inserted_now ())
b775012e 11990 {
04086b45 11991 if (insert_mode != UGLL_DONT_INSERT)
b775012e
LM
11992 insert_breakpoint_locations ();
11993 else
11994 {
44702360
PA
11995 /* Even though the caller told us to not insert new
11996 locations, we may still need to update conditions on the
11997 target's side of breakpoints that were already inserted
11998 if the target is evaluating breakpoint conditions. We
b775012e
LM
11999 only update conditions for locations that are marked
12000 "needs_update". */
12001 update_inserted_breakpoint_locations ();
12002 }
12003 }
348d480f 12004
04086b45 12005 if (insert_mode != UGLL_DONT_INSERT)
1e4d1764 12006 download_tracepoint_locations ();
348d480f
PA
12007}
12008
12009void
12010breakpoint_retire_moribund (void)
12011{
1123588c
TT
12012 for (int ix = 0; ix < moribund_locations.size (); ++ix)
12013 {
12014 struct bp_location *loc = moribund_locations[ix];
12015 if (--(loc->events_till_retirement) == 0)
12016 {
12017 decref_bp_location (&loc);
12018 unordered_remove (moribund_locations, ix);
12019 --ix;
12020 }
12021 }
348d480f
PA
12022}
12023
12024static void
44702360 12025update_global_location_list_nothrow (enum ugll_insert_mode insert_mode)
348d480f 12026{
348d480f 12027
a70b8144 12028 try
492d29ea
PA
12029 {
12030 update_global_location_list (insert_mode);
12031 }
230d2906 12032 catch (const gdb_exception_error &e)
492d29ea
PA
12033 {
12034 }
348d480f
PA
12035}
12036
12037/* Clear BKP from a BPS. */
12038
12039static void
12040bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
12041{
12042 bpstat bs;
12043
12044 for (bs = bps; bs; bs = bs->next)
12045 if (bs->breakpoint_at == bpt)
12046 {
12047 bs->breakpoint_at = NULL;
12048 bs->old_val = NULL;
12049 /* bs->commands will be freed later. */
12050 }
12051}
12052
12053/* Callback for iterate_over_threads. */
12054static int
12055bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
12056{
9a3c8263 12057 struct breakpoint *bpt = (struct breakpoint *) data;
348d480f
PA
12058
12059 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
12060 return 0;
12061}
12062
12063/* Helper for breakpoint and tracepoint breakpoint_ops->mention
12064 callbacks. */
12065
12066static void
12067say_where (struct breakpoint *b)
12068{
12069 struct value_print_options opts;
12070
12071 get_user_print_options (&opts);
12072
12073 /* i18n: cagney/2005-02-11: Below needs to be merged into a
12074 single string. */
12075 if (b->loc == NULL)
12076 {
f00aae0f
KS
12077 /* For pending locations, the output differs slightly based
12078 on b->extra_string. If this is non-NULL, it contains either
12079 a condition or dprintf arguments. */
12080 if (b->extra_string == NULL)
12081 {
12082 printf_filtered (_(" (%s) pending."),
d28cd78a 12083 event_location_to_string (b->location.get ()));
f00aae0f
KS
12084 }
12085 else if (b->type == bp_dprintf)
12086 {
12087 printf_filtered (_(" (%s,%s) pending."),
d28cd78a 12088 event_location_to_string (b->location.get ()),
f00aae0f
KS
12089 b->extra_string);
12090 }
12091 else
12092 {
12093 printf_filtered (_(" (%s %s) pending."),
d28cd78a 12094 event_location_to_string (b->location.get ()),
f00aae0f
KS
12095 b->extra_string);
12096 }
348d480f
PA
12097 }
12098 else
12099 {
2f202fde 12100 if (opts.addressprint || b->loc->symtab == NULL)
6a831f06
PA
12101 printf_filtered (" at %ps",
12102 styled_string (address_style.style (),
12103 paddress (b->loc->gdbarch,
12104 b->loc->address)));
2f202fde 12105 if (b->loc->symtab != NULL)
f8eba3c6
TT
12106 {
12107 /* If there is a single location, we can print the location
12108 more nicely. */
12109 if (b->loc->next == NULL)
0bb296cb 12110 {
6a831f06
PA
12111 const char *filename
12112 = symtab_to_filename_for_display (b->loc->symtab);
12113 printf_filtered (": file %ps, line %d.",
12114 styled_string (file_name_style.style (),
12115 filename),
0bb296cb
TT
12116 b->loc->line_number);
12117 }
f8eba3c6
TT
12118 else
12119 /* This is not ideal, but each location may have a
12120 different file name, and this at least reflects the
12121 real situation somewhat. */
f00aae0f 12122 printf_filtered (": %s.",
d28cd78a 12123 event_location_to_string (b->location.get ()));
f8eba3c6 12124 }
348d480f
PA
12125
12126 if (b->loc->next)
12127 {
12128 struct bp_location *loc = b->loc;
12129 int n = 0;
12130 for (; loc; loc = loc->next)
12131 ++n;
12132 printf_filtered (" (%d locations)", n);
12133 }
12134 }
12135}
12136
5f486660 12137bp_location::~bp_location ()
348d480f 12138{
5f486660 12139 xfree (function_name);
348d480f
PA
12140}
12141
c1fc2657 12142/* Destructor for the breakpoint base class. */
348d480f 12143
c1fc2657 12144breakpoint::~breakpoint ()
348d480f 12145{
c1fc2657
SM
12146 xfree (this->cond_string);
12147 xfree (this->extra_string);
348d480f
PA
12148}
12149
2060206e
PA
12150static struct bp_location *
12151base_breakpoint_allocate_location (struct breakpoint *self)
348d480f 12152{
5f486660 12153 return new bp_location (self);
348d480f
PA
12154}
12155
2060206e
PA
12156static void
12157base_breakpoint_re_set (struct breakpoint *b)
12158{
12159 /* Nothing to re-set. */
12160}
12161
12162#define internal_error_pure_virtual_called() \
12163 gdb_assert_not_reached ("pure virtual function called")
12164
12165static int
12166base_breakpoint_insert_location (struct bp_location *bl)
12167{
12168 internal_error_pure_virtual_called ();
12169}
12170
12171static int
73971819
PA
12172base_breakpoint_remove_location (struct bp_location *bl,
12173 enum remove_bp_reason reason)
2060206e
PA
12174{
12175 internal_error_pure_virtual_called ();
12176}
12177
12178static int
12179base_breakpoint_breakpoint_hit (const struct bp_location *bl,
bd522513 12180 const address_space *aspace,
09ac7c10
TT
12181 CORE_ADDR bp_addr,
12182 const struct target_waitstatus *ws)
2060206e
PA
12183{
12184 internal_error_pure_virtual_called ();
12185}
12186
12187static void
12188base_breakpoint_check_status (bpstat bs)
12189{
12190 /* Always stop. */
12191}
12192
12193/* A "works_in_software_mode" breakpoint_ops method that just internal
12194 errors. */
12195
12196static int
12197base_breakpoint_works_in_software_mode (const struct breakpoint *b)
12198{
12199 internal_error_pure_virtual_called ();
12200}
12201
12202/* A "resources_needed" breakpoint_ops method that just internal
12203 errors. */
12204
12205static int
12206base_breakpoint_resources_needed (const struct bp_location *bl)
12207{
12208 internal_error_pure_virtual_called ();
12209}
12210
12211static enum print_stop_action
12212base_breakpoint_print_it (bpstat bs)
12213{
12214 internal_error_pure_virtual_called ();
12215}
12216
12217static void
12218base_breakpoint_print_one_detail (const struct breakpoint *self,
12219 struct ui_out *uiout)
12220{
12221 /* nothing */
12222}
12223
12224static void
12225base_breakpoint_print_mention (struct breakpoint *b)
12226{
12227 internal_error_pure_virtual_called ();
12228}
12229
12230static void
12231base_breakpoint_print_recreate (struct breakpoint *b, struct ui_file *fp)
12232{
12233 internal_error_pure_virtual_called ();
12234}
12235
983af33b 12236static void
f00aae0f
KS
12237base_breakpoint_create_sals_from_location
12238 (const struct event_location *location,
12239 struct linespec_result *canonical,
12240 enum bptype type_wanted)
983af33b
SDJ
12241{
12242 internal_error_pure_virtual_called ();
12243}
12244
12245static void
12246base_breakpoint_create_breakpoints_sal (struct gdbarch *gdbarch,
12247 struct linespec_result *c,
e1e01040
PA
12248 gdb::unique_xmalloc_ptr<char> cond_string,
12249 gdb::unique_xmalloc_ptr<char> extra_string,
983af33b
SDJ
12250 enum bptype type_wanted,
12251 enum bpdisp disposition,
12252 int thread,
12253 int task, int ignore_count,
12254 const struct breakpoint_ops *o,
12255 int from_tty, int enabled,
44f238bb 12256 int internal, unsigned flags)
983af33b
SDJ
12257{
12258 internal_error_pure_virtual_called ();
12259}
12260
6c5b2ebe 12261static std::vector<symtab_and_line>
f00aae0f
KS
12262base_breakpoint_decode_location (struct breakpoint *b,
12263 const struct event_location *location,
6c5b2ebe 12264 struct program_space *search_pspace)
983af33b
SDJ
12265{
12266 internal_error_pure_virtual_called ();
12267}
12268
ab04a2af
TT
12269/* The default 'explains_signal' method. */
12270
47591c29 12271static int
427cd150 12272base_breakpoint_explains_signal (struct breakpoint *b, enum gdb_signal sig)
ab04a2af 12273{
47591c29 12274 return 1;
ab04a2af
TT
12275}
12276
9d6e6e84
HZ
12277/* The default "after_condition_true" method. */
12278
12279static void
12280base_breakpoint_after_condition_true (struct bpstats *bs)
12281{
12282 /* Nothing to do. */
12283}
12284
ab04a2af 12285struct breakpoint_ops base_breakpoint_ops =
2060206e 12286{
2060206e
PA
12287 base_breakpoint_allocate_location,
12288 base_breakpoint_re_set,
12289 base_breakpoint_insert_location,
12290 base_breakpoint_remove_location,
12291 base_breakpoint_breakpoint_hit,
12292 base_breakpoint_check_status,
12293 base_breakpoint_resources_needed,
12294 base_breakpoint_works_in_software_mode,
12295 base_breakpoint_print_it,
12296 NULL,
12297 base_breakpoint_print_one_detail,
12298 base_breakpoint_print_mention,
983af33b 12299 base_breakpoint_print_recreate,
5f700d83 12300 base_breakpoint_create_sals_from_location,
983af33b 12301 base_breakpoint_create_breakpoints_sal,
5f700d83 12302 base_breakpoint_decode_location,
9d6e6e84
HZ
12303 base_breakpoint_explains_signal,
12304 base_breakpoint_after_condition_true,
2060206e
PA
12305};
12306
12307/* Default breakpoint_ops methods. */
12308
12309static void
348d480f
PA
12310bkpt_re_set (struct breakpoint *b)
12311{
06edf0c0 12312 /* FIXME: is this still reachable? */
9ef9e6a6 12313 if (breakpoint_event_location_empty_p (b))
06edf0c0 12314 {
f00aae0f 12315 /* Anything without a location can't be re-set. */
348d480f 12316 delete_breakpoint (b);
06edf0c0 12317 return;
348d480f 12318 }
06edf0c0
PA
12319
12320 breakpoint_re_set_default (b);
348d480f
PA
12321}
12322
2060206e 12323static int
348d480f
PA
12324bkpt_insert_location (struct bp_location *bl)
12325{
cd6c3b4f
YQ
12326 CORE_ADDR addr = bl->target_info.reqstd_address;
12327
579c6ad9 12328 bl->target_info.kind = breakpoint_kind (bl, &addr);
cd6c3b4f
YQ
12329 bl->target_info.placed_address = addr;
12330
348d480f 12331 if (bl->loc_type == bp_loc_hardware_breakpoint)
7c16b83e 12332 return target_insert_hw_breakpoint (bl->gdbarch, &bl->target_info);
348d480f 12333 else
7c16b83e 12334 return target_insert_breakpoint (bl->gdbarch, &bl->target_info);
348d480f
PA
12335}
12336
2060206e 12337static int
73971819 12338bkpt_remove_location (struct bp_location *bl, enum remove_bp_reason reason)
348d480f
PA
12339{
12340 if (bl->loc_type == bp_loc_hardware_breakpoint)
12341 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
12342 else
73971819 12343 return target_remove_breakpoint (bl->gdbarch, &bl->target_info, reason);
348d480f
PA
12344}
12345
2060206e 12346static int
348d480f 12347bkpt_breakpoint_hit (const struct bp_location *bl,
bd522513 12348 const address_space *aspace, CORE_ADDR bp_addr,
09ac7c10 12349 const struct target_waitstatus *ws)
348d480f 12350{
09ac7c10 12351 if (ws->kind != TARGET_WAITKIND_STOPPED
a493e3e2 12352 || ws->value.sig != GDB_SIGNAL_TRAP)
09ac7c10
TT
12353 return 0;
12354
348d480f
PA
12355 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
12356 aspace, bp_addr))
12357 return 0;
12358
12359 if (overlay_debugging /* unmapped overlay section */
12360 && section_is_overlay (bl->section)
12361 && !section_is_mapped (bl->section))
12362 return 0;
12363
12364 return 1;
12365}
12366
cd1608cc
PA
12367static int
12368dprintf_breakpoint_hit (const struct bp_location *bl,
bd522513 12369 const address_space *aspace, CORE_ADDR bp_addr,
cd1608cc
PA
12370 const struct target_waitstatus *ws)
12371{
12372 if (dprintf_style == dprintf_style_agent
12373 && target_can_run_breakpoint_commands ())
12374 {
12375 /* An agent-style dprintf never causes a stop. If we see a trap
12376 for this address it must be for a breakpoint that happens to
12377 be set at the same address. */
12378 return 0;
12379 }
12380
12381 return bkpt_breakpoint_hit (bl, aspace, bp_addr, ws);
12382}
12383
2060206e 12384static int
348d480f
PA
12385bkpt_resources_needed (const struct bp_location *bl)
12386{
12387 gdb_assert (bl->owner->type == bp_hardware_breakpoint);
12388
12389 return 1;
12390}
12391
2060206e 12392static enum print_stop_action
348d480f
PA
12393bkpt_print_it (bpstat bs)
12394{
348d480f
PA
12395 struct breakpoint *b;
12396 const struct bp_location *bl;
001c8c33 12397 int bp_temp;
79a45e25 12398 struct ui_out *uiout = current_uiout;
348d480f
PA
12399
12400 gdb_assert (bs->bp_location_at != NULL);
12401
12402 bl = bs->bp_location_at;
12403 b = bs->breakpoint_at;
12404
001c8c33
PA
12405 bp_temp = b->disposition == disp_del;
12406 if (bl->address != bl->requested_address)
12407 breakpoint_adjustment_warning (bl->requested_address,
12408 bl->address,
12409 b->number, 1);
12410 annotate_breakpoint (b->number);
f303dbd6
PA
12411 maybe_print_thread_hit_breakpoint (uiout);
12412
112e8700 12413 if (uiout->is_mi_like_p ())
348d480f 12414 {
112e8700 12415 uiout->field_string ("reason",
001c8c33 12416 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
112e8700 12417 uiout->field_string ("disp", bpdisp_text (b->disposition));
06edf0c0 12418 }
6a831f06
PA
12419 if (bp_temp)
12420 uiout->message ("Temporary breakpoint %pF, ",
12421 signed_field ("bkptno", b->number));
12422 else
12423 uiout->message ("Breakpoint %pF, ",
12424 signed_field ("bkptno", b->number));
06edf0c0 12425
001c8c33 12426 return PRINT_SRC_AND_LOC;
06edf0c0
PA
12427}
12428
2060206e 12429static void
06edf0c0
PA
12430bkpt_print_mention (struct breakpoint *b)
12431{
112e8700 12432 if (current_uiout->is_mi_like_p ())
06edf0c0
PA
12433 return;
12434
12435 switch (b->type)
12436 {
12437 case bp_breakpoint:
12438 case bp_gnu_ifunc_resolver:
12439 if (b->disposition == disp_del)
12440 printf_filtered (_("Temporary breakpoint"));
12441 else
12442 printf_filtered (_("Breakpoint"));
12443 printf_filtered (_(" %d"), b->number);
12444 if (b->type == bp_gnu_ifunc_resolver)
12445 printf_filtered (_(" at gnu-indirect-function resolver"));
12446 break;
12447 case bp_hardware_breakpoint:
12448 printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
12449 break;
e7e0cddf
SS
12450 case bp_dprintf:
12451 printf_filtered (_("Dprintf %d"), b->number);
12452 break;
06edf0c0
PA
12453 }
12454
12455 say_where (b);
12456}
12457
2060206e 12458static void
06edf0c0
PA
12459bkpt_print_recreate (struct breakpoint *tp, struct ui_file *fp)
12460{
12461 if (tp->type == bp_breakpoint && tp->disposition == disp_del)
12462 fprintf_unfiltered (fp, "tbreak");
12463 else if (tp->type == bp_breakpoint)
12464 fprintf_unfiltered (fp, "break");
12465 else if (tp->type == bp_hardware_breakpoint
12466 && tp->disposition == disp_del)
12467 fprintf_unfiltered (fp, "thbreak");
12468 else if (tp->type == bp_hardware_breakpoint)
12469 fprintf_unfiltered (fp, "hbreak");
12470 else
12471 internal_error (__FILE__, __LINE__,
12472 _("unhandled breakpoint type %d"), (int) tp->type);
12473
f00aae0f 12474 fprintf_unfiltered (fp, " %s",
d28cd78a 12475 event_location_to_string (tp->location.get ()));
f00aae0f
KS
12476
12477 /* Print out extra_string if this breakpoint is pending. It might
12478 contain, for example, conditions that were set by the user. */
12479 if (tp->loc == NULL && tp->extra_string != NULL)
12480 fprintf_unfiltered (fp, " %s", tp->extra_string);
12481
dd11a36c 12482 print_recreate_thread (tp, fp);
06edf0c0
PA
12483}
12484
983af33b 12485static void
f00aae0f
KS
12486bkpt_create_sals_from_location (const struct event_location *location,
12487 struct linespec_result *canonical,
12488 enum bptype type_wanted)
983af33b 12489{
f00aae0f 12490 create_sals_from_location_default (location, canonical, type_wanted);
983af33b
SDJ
12491}
12492
12493static void
12494bkpt_create_breakpoints_sal (struct gdbarch *gdbarch,
12495 struct linespec_result *canonical,
e1e01040
PA
12496 gdb::unique_xmalloc_ptr<char> cond_string,
12497 gdb::unique_xmalloc_ptr<char> extra_string,
983af33b
SDJ
12498 enum bptype type_wanted,
12499 enum bpdisp disposition,
12500 int thread,
12501 int task, int ignore_count,
12502 const struct breakpoint_ops *ops,
12503 int from_tty, int enabled,
44f238bb 12504 int internal, unsigned flags)
983af33b 12505{
023fa29b 12506 create_breakpoints_sal_default (gdbarch, canonical,
e1e01040
PA
12507 std::move (cond_string),
12508 std::move (extra_string),
e7e0cddf 12509 type_wanted,
983af33b
SDJ
12510 disposition, thread, task,
12511 ignore_count, ops, from_tty,
44f238bb 12512 enabled, internal, flags);
983af33b
SDJ
12513}
12514
6c5b2ebe 12515static std::vector<symtab_and_line>
f00aae0f
KS
12516bkpt_decode_location (struct breakpoint *b,
12517 const struct event_location *location,
6c5b2ebe 12518 struct program_space *search_pspace)
983af33b 12519{
6c5b2ebe 12520 return decode_location_default (b, location, search_pspace);
983af33b
SDJ
12521}
12522
06edf0c0
PA
12523/* Virtual table for internal breakpoints. */
12524
12525static void
12526internal_bkpt_re_set (struct breakpoint *b)
12527{
12528 switch (b->type)
12529 {
12530 /* Delete overlay event and longjmp master breakpoints; they
12531 will be reset later by breakpoint_re_set. */
12532 case bp_overlay_event:
12533 case bp_longjmp_master:
12534 case bp_std_terminate_master:
12535 case bp_exception_master:
12536 delete_breakpoint (b);
12537 break;
12538
12539 /* This breakpoint is special, it's set up when the inferior
12540 starts and we really don't want to touch it. */
12541 case bp_shlib_event:
12542
12543 /* Like bp_shlib_event, this breakpoint type is special. Once
12544 it is set up, we do not want to touch it. */
12545 case bp_thread_event:
12546 break;
12547 }
12548}
12549
12550static void
12551internal_bkpt_check_status (bpstat bs)
12552{
a9b3a50f
PA
12553 if (bs->breakpoint_at->type == bp_shlib_event)
12554 {
12555 /* If requested, stop when the dynamic linker notifies GDB of
12556 events. This allows the user to get control and place
12557 breakpoints in initializer routines for dynamically loaded
12558 objects (among other things). */
12559 bs->stop = stop_on_solib_events;
12560 bs->print = stop_on_solib_events;
12561 }
12562 else
12563 bs->stop = 0;
06edf0c0
PA
12564}
12565
12566static enum print_stop_action
12567internal_bkpt_print_it (bpstat bs)
12568{
06edf0c0 12569 struct breakpoint *b;
06edf0c0 12570
06edf0c0
PA
12571 b = bs->breakpoint_at;
12572
06edf0c0
PA
12573 switch (b->type)
12574 {
348d480f
PA
12575 case bp_shlib_event:
12576 /* Did we stop because the user set the stop_on_solib_events
12577 variable? (If so, we report this as a generic, "Stopped due
12578 to shlib event" message.) */
edcc5120 12579 print_solib_event (0);
348d480f
PA
12580 break;
12581
12582 case bp_thread_event:
12583 /* Not sure how we will get here.
12584 GDB should not stop for these breakpoints. */
12585 printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
12586 break;
12587
12588 case bp_overlay_event:
12589 /* By analogy with the thread event, GDB should not stop for these. */
12590 printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
12591 break;
12592
12593 case bp_longjmp_master:
12594 /* These should never be enabled. */
12595 printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
348d480f
PA
12596 break;
12597
12598 case bp_std_terminate_master:
12599 /* These should never be enabled. */
12600 printf_filtered (_("std::terminate Master Breakpoint: "
12601 "gdb should not stop!\n"));
348d480f
PA
12602 break;
12603
12604 case bp_exception_master:
12605 /* These should never be enabled. */
12606 printf_filtered (_("Exception Master Breakpoint: "
12607 "gdb should not stop!\n"));
06edf0c0
PA
12608 break;
12609 }
12610
001c8c33 12611 return PRINT_NOTHING;
06edf0c0
PA
12612}
12613
12614static void
12615internal_bkpt_print_mention (struct breakpoint *b)
12616{
12617 /* Nothing to mention. These breakpoints are internal. */
12618}
12619
06edf0c0
PA
12620/* Virtual table for momentary breakpoints */
12621
12622static void
12623momentary_bkpt_re_set (struct breakpoint *b)
12624{
12625 /* Keep temporary breakpoints, which can be encountered when we step
4d1eb6b4 12626 over a dlopen call and solib_add is resetting the breakpoints.
06edf0c0
PA
12627 Otherwise these should have been blown away via the cleanup chain
12628 or by breakpoint_init_inferior when we rerun the executable. */
12629}
12630
12631static void
12632momentary_bkpt_check_status (bpstat bs)
12633{
12634 /* Nothing. The point of these breakpoints is causing a stop. */
12635}
12636
12637static enum print_stop_action
12638momentary_bkpt_print_it (bpstat bs)
12639{
001c8c33 12640 return PRINT_UNKNOWN;
348d480f
PA
12641}
12642
06edf0c0
PA
12643static void
12644momentary_bkpt_print_mention (struct breakpoint *b)
348d480f 12645{
06edf0c0 12646 /* Nothing to mention. These breakpoints are internal. */
348d480f
PA
12647}
12648
e2e4d78b
JK
12649/* Ensure INITIATING_FRAME is cleared when no such breakpoint exists.
12650
12651 It gets cleared already on the removal of the first one of such placed
12652 breakpoints. This is OK as they get all removed altogether. */
12653
c1fc2657 12654longjmp_breakpoint::~longjmp_breakpoint ()
e2e4d78b 12655{
c1fc2657 12656 thread_info *tp = find_thread_global_id (this->thread);
e2e4d78b 12657
c1fc2657 12658 if (tp != NULL)
e2e4d78b 12659 tp->initiating_frame = null_frame_id;
e2e4d78b
JK
12660}
12661
55aa24fb
SDJ
12662/* Specific methods for probe breakpoints. */
12663
12664static int
12665bkpt_probe_insert_location (struct bp_location *bl)
12666{
12667 int v = bkpt_insert_location (bl);
12668
12669 if (v == 0)
12670 {
12671 /* The insertion was successful, now let's set the probe's semaphore
12672 if needed. */
935676c9 12673 bl->probe.prob->set_semaphore (bl->probe.objfile, bl->gdbarch);
55aa24fb
SDJ
12674 }
12675
12676 return v;
12677}
12678
12679static int
73971819
PA
12680bkpt_probe_remove_location (struct bp_location *bl,
12681 enum remove_bp_reason reason)
55aa24fb
SDJ
12682{
12683 /* Let's clear the semaphore before removing the location. */
935676c9 12684 bl->probe.prob->clear_semaphore (bl->probe.objfile, bl->gdbarch);
55aa24fb 12685
73971819 12686 return bkpt_remove_location (bl, reason);
55aa24fb
SDJ
12687}
12688
12689static void
f00aae0f 12690bkpt_probe_create_sals_from_location (const struct event_location *location,
5f700d83 12691 struct linespec_result *canonical,
f00aae0f 12692 enum bptype type_wanted)
55aa24fb
SDJ
12693{
12694 struct linespec_sals lsal;
12695
c2f4122d 12696 lsal.sals = parse_probes (location, NULL, canonical);
8e9e35b1
TT
12697 lsal.canonical
12698 = xstrdup (event_location_to_string (canonical->location.get ()));
6c5b2ebe 12699 canonical->lsals.push_back (std::move (lsal));
55aa24fb
SDJ
12700}
12701
6c5b2ebe 12702static std::vector<symtab_and_line>
f00aae0f
KS
12703bkpt_probe_decode_location (struct breakpoint *b,
12704 const struct event_location *location,
6c5b2ebe 12705 struct program_space *search_pspace)
55aa24fb 12706{
6c5b2ebe
PA
12707 std::vector<symtab_and_line> sals = parse_probes (location, search_pspace, NULL);
12708 if (sals.empty ())
55aa24fb 12709 error (_("probe not found"));
6c5b2ebe 12710 return sals;
55aa24fb
SDJ
12711}
12712
348d480f 12713/* The breakpoint_ops structure to be used in tracepoints. */
876fa593 12714
348d480f
PA
12715static void
12716tracepoint_re_set (struct breakpoint *b)
12717{
12718 breakpoint_re_set_default (b);
12719}
876fa593 12720
348d480f
PA
12721static int
12722tracepoint_breakpoint_hit (const struct bp_location *bl,
bd522513 12723 const address_space *aspace, CORE_ADDR bp_addr,
09ac7c10 12724 const struct target_waitstatus *ws)
348d480f
PA
12725{
12726 /* By definition, the inferior does not report stops at
12727 tracepoints. */
12728 return 0;
74960c60
VP
12729}
12730
12731static void
348d480f
PA
12732tracepoint_print_one_detail (const struct breakpoint *self,
12733 struct ui_out *uiout)
74960c60 12734{
d9b3f62e 12735 struct tracepoint *tp = (struct tracepoint *) self;
5d9310c4 12736 if (!tp->static_trace_marker_id.empty ())
348d480f
PA
12737 {
12738 gdb_assert (self->type == bp_static_tracepoint);
cc59ec59 12739
6a831f06
PA
12740 uiout->message ("\tmarker id is %pF\n",
12741 string_field ("static-tracepoint-marker-string-id",
12742 tp->static_trace_marker_id.c_str ()));
348d480f 12743 }
0d381245
VP
12744}
12745
a474d7c2 12746static void
348d480f 12747tracepoint_print_mention (struct breakpoint *b)
a474d7c2 12748{
112e8700 12749 if (current_uiout->is_mi_like_p ())
348d480f 12750 return;
cc59ec59 12751
348d480f
PA
12752 switch (b->type)
12753 {
12754 case bp_tracepoint:
12755 printf_filtered (_("Tracepoint"));
12756 printf_filtered (_(" %d"), b->number);
12757 break;
12758 case bp_fast_tracepoint:
12759 printf_filtered (_("Fast tracepoint"));
12760 printf_filtered (_(" %d"), b->number);
12761 break;
12762 case bp_static_tracepoint:
12763 printf_filtered (_("Static tracepoint"));
12764 printf_filtered (_(" %d"), b->number);
12765 break;
12766 default:
12767 internal_error (__FILE__, __LINE__,
12768 _("unhandled tracepoint type %d"), (int) b->type);
12769 }
12770
12771 say_where (b);
a474d7c2
PA
12772}
12773
348d480f 12774static void
d9b3f62e 12775tracepoint_print_recreate (struct breakpoint *self, struct ui_file *fp)
a474d7c2 12776{
d9b3f62e
PA
12777 struct tracepoint *tp = (struct tracepoint *) self;
12778
12779 if (self->type == bp_fast_tracepoint)
348d480f 12780 fprintf_unfiltered (fp, "ftrace");
c93e8391 12781 else if (self->type == bp_static_tracepoint)
348d480f 12782 fprintf_unfiltered (fp, "strace");
d9b3f62e 12783 else if (self->type == bp_tracepoint)
348d480f
PA
12784 fprintf_unfiltered (fp, "trace");
12785 else
12786 internal_error (__FILE__, __LINE__,
d9b3f62e 12787 _("unhandled tracepoint type %d"), (int) self->type);
cc59ec59 12788
f00aae0f 12789 fprintf_unfiltered (fp, " %s",
d28cd78a 12790 event_location_to_string (self->location.get ()));
d9b3f62e
PA
12791 print_recreate_thread (self, fp);
12792
12793 if (tp->pass_count)
12794 fprintf_unfiltered (fp, " passcount %d\n", tp->pass_count);
a474d7c2
PA
12795}
12796
983af33b 12797static void
f00aae0f
KS
12798tracepoint_create_sals_from_location (const struct event_location *location,
12799 struct linespec_result *canonical,
12800 enum bptype type_wanted)
983af33b 12801{
f00aae0f 12802 create_sals_from_location_default (location, canonical, type_wanted);
983af33b
SDJ
12803}
12804
12805static void
12806tracepoint_create_breakpoints_sal (struct gdbarch *gdbarch,
12807 struct linespec_result *canonical,
e1e01040
PA
12808 gdb::unique_xmalloc_ptr<char> cond_string,
12809 gdb::unique_xmalloc_ptr<char> extra_string,
983af33b
SDJ
12810 enum bptype type_wanted,
12811 enum bpdisp disposition,
12812 int thread,
12813 int task, int ignore_count,
12814 const struct breakpoint_ops *ops,
12815 int from_tty, int enabled,
44f238bb 12816 int internal, unsigned flags)
983af33b 12817{
023fa29b 12818 create_breakpoints_sal_default (gdbarch, canonical,
e1e01040
PA
12819 std::move (cond_string),
12820 std::move (extra_string),
e7e0cddf 12821 type_wanted,
983af33b
SDJ
12822 disposition, thread, task,
12823 ignore_count, ops, from_tty,
44f238bb 12824 enabled, internal, flags);
983af33b
SDJ
12825}
12826
6c5b2ebe 12827static std::vector<symtab_and_line>
f00aae0f
KS
12828tracepoint_decode_location (struct breakpoint *b,
12829 const struct event_location *location,
6c5b2ebe 12830 struct program_space *search_pspace)
983af33b 12831{
6c5b2ebe 12832 return decode_location_default (b, location, search_pspace);
983af33b
SDJ
12833}
12834
2060206e 12835struct breakpoint_ops tracepoint_breakpoint_ops;
348d480f 12836
bac7c5cf 12837/* Virtual table for tracepoints on static probes. */
55aa24fb
SDJ
12838
12839static void
f00aae0f
KS
12840tracepoint_probe_create_sals_from_location
12841 (const struct event_location *location,
12842 struct linespec_result *canonical,
12843 enum bptype type_wanted)
55aa24fb
SDJ
12844{
12845 /* We use the same method for breakpoint on probes. */
f00aae0f 12846 bkpt_probe_create_sals_from_location (location, canonical, type_wanted);
55aa24fb
SDJ
12847}
12848
6c5b2ebe 12849static std::vector<symtab_and_line>
f00aae0f
KS
12850tracepoint_probe_decode_location (struct breakpoint *b,
12851 const struct event_location *location,
6c5b2ebe 12852 struct program_space *search_pspace)
55aa24fb
SDJ
12853{
12854 /* We use the same method for breakpoint on probes. */
6c5b2ebe 12855 return bkpt_probe_decode_location (b, location, search_pspace);
55aa24fb
SDJ
12856}
12857
5c2b4418
HZ
12858/* Dprintf breakpoint_ops methods. */
12859
12860static void
12861dprintf_re_set (struct breakpoint *b)
12862{
12863 breakpoint_re_set_default (b);
12864
f00aae0f
KS
12865 /* extra_string should never be non-NULL for dprintf. */
12866 gdb_assert (b->extra_string != NULL);
5c2b4418
HZ
12867
12868 /* 1 - connect to target 1, that can run breakpoint commands.
12869 2 - create a dprintf, which resolves fine.
12870 3 - disconnect from target 1
12871 4 - connect to target 2, that can NOT run breakpoint commands.
12872
12873 After steps #3/#4, you'll want the dprintf command list to
12874 be updated, because target 1 and 2 may well return different
12875 answers for target_can_run_breakpoint_commands().
12876 Given absence of finer grained resetting, we get to do
12877 it all the time. */
12878 if (b->extra_string != NULL)
12879 update_dprintf_command_list (b);
12880}
12881
2d9442cc
HZ
12882/* Implement the "print_recreate" breakpoint_ops method for dprintf. */
12883
12884static void
12885dprintf_print_recreate (struct breakpoint *tp, struct ui_file *fp)
12886{
f00aae0f 12887 fprintf_unfiltered (fp, "dprintf %s,%s",
d28cd78a 12888 event_location_to_string (tp->location.get ()),
2d9442cc
HZ
12889 tp->extra_string);
12890 print_recreate_thread (tp, fp);
12891}
12892
9d6e6e84
HZ
12893/* Implement the "after_condition_true" breakpoint_ops method for
12894 dprintf.
12895
12896 dprintf's are implemented with regular commands in their command
12897 list, but we run the commands here instead of before presenting the
12898 stop to the user, as dprintf's don't actually cause a stop. This
12899 also makes it so that the commands of multiple dprintfs at the same
12900 address are all handled. */
12901
12902static void
12903dprintf_after_condition_true (struct bpstats *bs)
12904{
04afa70c 12905 struct bpstats tmp_bs;
9d6e6e84
HZ
12906 struct bpstats *tmp_bs_p = &tmp_bs;
12907
12908 /* dprintf's never cause a stop. This wasn't set in the
12909 check_status hook instead because that would make the dprintf's
12910 condition not be evaluated. */
12911 bs->stop = 0;
12912
12913 /* Run the command list here. Take ownership of it instead of
12914 copying. We never want these commands to run later in
12915 bpstat_do_actions, if a breakpoint that causes a stop happens to
12916 be set at same address as this dprintf, or even if running the
12917 commands here throws. */
12918 tmp_bs.commands = bs->commands;
12919 bs->commands = NULL;
9d6e6e84
HZ
12920
12921 bpstat_do_actions_1 (&tmp_bs_p);
12922
12923 /* 'tmp_bs.commands' will usually be NULL by now, but
12924 bpstat_do_actions_1 may return early without processing the whole
12925 list. */
9d6e6e84
HZ
12926}
12927
983af33b
SDJ
12928/* The breakpoint_ops structure to be used on static tracepoints with
12929 markers (`-m'). */
12930
12931static void
f00aae0f 12932strace_marker_create_sals_from_location (const struct event_location *location,
5f700d83 12933 struct linespec_result *canonical,
f00aae0f 12934 enum bptype type_wanted)
983af33b
SDJ
12935{
12936 struct linespec_sals lsal;
f00aae0f 12937 const char *arg_start, *arg;
983af33b 12938
a20714ff 12939 arg = arg_start = get_linespec_location (location)->spec_string;
f00aae0f 12940 lsal.sals = decode_static_tracepoint_spec (&arg);
983af33b 12941
f2fc3015
TT
12942 std::string str (arg_start, arg - arg_start);
12943 const char *ptr = str.c_str ();
a20714ff
PA
12944 canonical->location
12945 = new_linespec_location (&ptr, symbol_name_match_type::FULL);
983af33b 12946
8e9e35b1
TT
12947 lsal.canonical
12948 = xstrdup (event_location_to_string (canonical->location.get ()));
6c5b2ebe 12949 canonical->lsals.push_back (std::move (lsal));
983af33b
SDJ
12950}
12951
12952static void
12953strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
12954 struct linespec_result *canonical,
e1e01040
PA
12955 gdb::unique_xmalloc_ptr<char> cond_string,
12956 gdb::unique_xmalloc_ptr<char> extra_string,
983af33b
SDJ
12957 enum bptype type_wanted,
12958 enum bpdisp disposition,
12959 int thread,
12960 int task, int ignore_count,
12961 const struct breakpoint_ops *ops,
12962 int from_tty, int enabled,
44f238bb 12963 int internal, unsigned flags)
983af33b 12964{
6c5b2ebe 12965 const linespec_sals &lsal = canonical->lsals[0];
983af33b
SDJ
12966
12967 /* If the user is creating a static tracepoint by marker id
12968 (strace -m MARKER_ID), then store the sals index, so that
12969 breakpoint_re_set can try to match up which of the newly
12970 found markers corresponds to this one, and, don't try to
12971 expand multiple locations for each sal, given than SALS
12972 already should contain all sals for MARKER_ID. */
12973
6c5b2ebe 12974 for (size_t i = 0; i < lsal.sals.size (); i++)
983af33b 12975 {
6c5b2ebe
PA
12976 event_location_up location
12977 = copy_event_location (canonical->location.get ());
983af33b 12978
b270e6f9 12979 std::unique_ptr<tracepoint> tp (new tracepoint ());
6c5b2ebe 12980 init_breakpoint_sal (tp.get (), gdbarch, lsal.sals[i],
ffc2605c 12981 std::move (location), NULL,
e1e01040
PA
12982 std::move (cond_string),
12983 std::move (extra_string),
e7e0cddf 12984 type_wanted, disposition,
983af33b 12985 thread, task, ignore_count, ops,
44f238bb 12986 from_tty, enabled, internal, flags,
983af33b
SDJ
12987 canonical->special_display);
12988 /* Given that its possible to have multiple markers with
12989 the same string id, if the user is creating a static
12990 tracepoint by marker id ("strace -m MARKER_ID"), then
12991 store the sals index, so that breakpoint_re_set can
12992 try to match up which of the newly found markers
12993 corresponds to this one */
12994 tp->static_trace_marker_id_idx = i;
12995
b270e6f9 12996 install_breakpoint (internal, std::move (tp), 0);
983af33b
SDJ
12997 }
12998}
12999
6c5b2ebe 13000static std::vector<symtab_and_line>
f00aae0f
KS
13001strace_marker_decode_location (struct breakpoint *b,
13002 const struct event_location *location,
6c5b2ebe 13003 struct program_space *search_pspace)
983af33b
SDJ
13004{
13005 struct tracepoint *tp = (struct tracepoint *) b;
a20714ff 13006 const char *s = get_linespec_location (location)->spec_string;
983af33b 13007
6c5b2ebe
PA
13008 std::vector<symtab_and_line> sals = decode_static_tracepoint_spec (&s);
13009 if (sals.size () > tp->static_trace_marker_id_idx)
983af33b 13010 {
6c5b2ebe
PA
13011 sals[0] = sals[tp->static_trace_marker_id_idx];
13012 sals.resize (1);
13013 return sals;
983af33b
SDJ
13014 }
13015 else
5d9310c4 13016 error (_("marker %s not found"), tp->static_trace_marker_id.c_str ());
983af33b
SDJ
13017}
13018
13019static struct breakpoint_ops strace_marker_breakpoint_ops;
13020
13021static int
13022strace_marker_p (struct breakpoint *b)
13023{
13024 return b->ops == &strace_marker_breakpoint_ops;
13025}
13026
53a5351d 13027/* Delete a breakpoint and clean up all traces of it in the data
f431efe5 13028 structures. */
c906108c
SS
13029
13030void
fba45db2 13031delete_breakpoint (struct breakpoint *bpt)
c906108c 13032{
52f0bd74 13033 struct breakpoint *b;
c906108c 13034
8a3fe4f8 13035 gdb_assert (bpt != NULL);
c906108c 13036
4a64f543
MS
13037 /* Has this bp already been deleted? This can happen because
13038 multiple lists can hold pointers to bp's. bpstat lists are
13039 especial culprits.
13040
13041 One example of this happening is a watchpoint's scope bp. When
13042 the scope bp triggers, we notice that the watchpoint is out of
13043 scope, and delete it. We also delete its scope bp. But the
13044 scope bp is marked "auto-deleting", and is already on a bpstat.
13045 That bpstat is then checked for auto-deleting bp's, which are
13046 deleted.
13047
13048 A real solution to this problem might involve reference counts in
13049 bp's, and/or giving them pointers back to their referencing
13050 bpstat's, and teaching delete_breakpoint to only free a bp's
13051 storage when no more references were extent. A cheaper bandaid
13052 was chosen. */
c906108c
SS
13053 if (bpt->type == bp_none)
13054 return;
13055
4a64f543
MS
13056 /* At least avoid this stale reference until the reference counting
13057 of breakpoints gets resolved. */
d0fb5eae 13058 if (bpt->related_breakpoint != bpt)
e5a0a904 13059 {
d0fb5eae 13060 struct breakpoint *related;
3a5c3e22 13061 struct watchpoint *w;
d0fb5eae
JK
13062
13063 if (bpt->type == bp_watchpoint_scope)
3a5c3e22 13064 w = (struct watchpoint *) bpt->related_breakpoint;
d0fb5eae 13065 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
3a5c3e22
PA
13066 w = (struct watchpoint *) bpt;
13067 else
13068 w = NULL;
13069 if (w != NULL)
13070 watchpoint_del_at_next_stop (w);
d0fb5eae
JK
13071
13072 /* Unlink bpt from the bpt->related_breakpoint ring. */
13073 for (related = bpt; related->related_breakpoint != bpt;
13074 related = related->related_breakpoint);
13075 related->related_breakpoint = bpt->related_breakpoint;
13076 bpt->related_breakpoint = bpt;
e5a0a904
JK
13077 }
13078
a9634178
TJB
13079 /* watch_command_1 creates a watchpoint but only sets its number if
13080 update_watchpoint succeeds in creating its bp_locations. If there's
13081 a problem in that process, we'll be asked to delete the half-created
13082 watchpoint. In that case, don't announce the deletion. */
13083 if (bpt->number)
76727919 13084 gdb::observers::breakpoint_deleted.notify (bpt);
c906108c 13085
c906108c
SS
13086 if (breakpoint_chain == bpt)
13087 breakpoint_chain = bpt->next;
13088
c906108c
SS
13089 ALL_BREAKPOINTS (b)
13090 if (b->next == bpt)
c5aa993b
JM
13091 {
13092 b->next = bpt->next;
13093 break;
13094 }
c906108c 13095
f431efe5
PA
13096 /* Be sure no bpstat's are pointing at the breakpoint after it's
13097 been freed. */
13098 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
e5dd4106 13099 in all threads for now. Note that we cannot just remove bpstats
f431efe5
PA
13100 pointing at bpt from the stop_bpstat list entirely, as breakpoint
13101 commands are associated with the bpstat; if we remove it here,
13102 then the later call to bpstat_do_actions (&stop_bpstat); in
13103 event-top.c won't do anything, and temporary breakpoints with
13104 commands won't work. */
13105
13106 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
13107
4a64f543
MS
13108 /* Now that breakpoint is removed from breakpoint list, update the
13109 global location list. This will remove locations that used to
13110 belong to this breakpoint. Do this before freeing the breakpoint
13111 itself, since remove_breakpoint looks at location's owner. It
13112 might be better design to have location completely
13113 self-contained, but it's not the case now. */
44702360 13114 update_global_location_list (UGLL_DONT_INSERT);
74960c60 13115
4a64f543
MS
13116 /* On the chance that someone will soon try again to delete this
13117 same bp, we mark it as deleted before freeing its storage. */
c906108c 13118 bpt->type = bp_none;
4d01a485 13119 delete bpt;
c906108c
SS
13120}
13121
51be5b68
PA
13122/* Iterator function to call a user-provided callback function once
13123 for each of B and its related breakpoints. */
13124
13125static void
13126iterate_over_related_breakpoints (struct breakpoint *b,
48649e1b 13127 gdb::function_view<void (breakpoint *)> function)
51be5b68
PA
13128{
13129 struct breakpoint *related;
13130
13131 related = b;
13132 do
13133 {
13134 struct breakpoint *next;
13135
13136 /* FUNCTION may delete RELATED. */
13137 next = related->related_breakpoint;
13138
13139 if (next == related)
13140 {
13141 /* RELATED is the last ring entry. */
48649e1b 13142 function (related);
51be5b68
PA
13143
13144 /* FUNCTION may have deleted it, so we'd never reach back to
13145 B. There's nothing left to do anyway, so just break
13146 out. */
13147 break;
13148 }
13149 else
48649e1b 13150 function (related);
51be5b68
PA
13151
13152 related = next;
13153 }
13154 while (related != b);
13155}
95a42b64 13156
4495129a 13157static void
981a3fb3 13158delete_command (const char *arg, int from_tty)
c906108c 13159{
35df4500 13160 struct breakpoint *b, *b_tmp;
c906108c 13161
ea9365bb
TT
13162 dont_repeat ();
13163
c906108c
SS
13164 if (arg == 0)
13165 {
13166 int breaks_to_delete = 0;
13167
46c6471b
PA
13168 /* Delete all breakpoints if no argument. Do not delete
13169 internal breakpoints, these have to be deleted with an
13170 explicit breakpoint number argument. */
c5aa993b 13171 ALL_BREAKPOINTS (b)
46c6471b 13172 if (user_breakpoint_p (b))
973d738b
DJ
13173 {
13174 breaks_to_delete = 1;
13175 break;
13176 }
c906108c
SS
13177
13178 /* Ask user only if there are some breakpoints to delete. */
13179 if (!from_tty
e2e0b3e5 13180 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
c906108c 13181 {
35df4500 13182 ALL_BREAKPOINTS_SAFE (b, b_tmp)
46c6471b 13183 if (user_breakpoint_p (b))
c5aa993b 13184 delete_breakpoint (b);
c906108c
SS
13185 }
13186 }
13187 else
48649e1b 13188 map_breakpoint_numbers
b926417a 13189 (arg, [&] (breakpoint *br)
48649e1b 13190 {
b926417a 13191 iterate_over_related_breakpoints (br, delete_breakpoint);
48649e1b 13192 });
c906108c
SS
13193}
13194
c2f4122d
PA
13195/* Return true if all locations of B bound to PSPACE are pending. If
13196 PSPACE is NULL, all locations of all program spaces are
13197 considered. */
13198
0d381245 13199static int
c2f4122d 13200all_locations_are_pending (struct breakpoint *b, struct program_space *pspace)
fe3f5fa8 13201{
c2f4122d
PA
13202 struct bp_location *loc;
13203
13204 for (loc = b->loc; loc != NULL; loc = loc->next)
13205 if ((pspace == NULL
13206 || loc->pspace == pspace)
13207 && !loc->shlib_disabled
8645ff69 13208 && !loc->pspace->executing_startup)
0d381245
VP
13209 return 0;
13210 return 1;
fe3f5fa8
VP
13211}
13212
776592bf
DE
13213/* Subroutine of update_breakpoint_locations to simplify it.
13214 Return non-zero if multiple fns in list LOC have the same name.
13215 Null names are ignored. */
13216
13217static int
13218ambiguous_names_p (struct bp_location *loc)
13219{
13220 struct bp_location *l;
459a2e4c
TT
13221 htab_t htab = htab_create_alloc (13, htab_hash_string, streq_hash, NULL,
13222 xcalloc, xfree);
776592bf
DE
13223
13224 for (l = loc; l != NULL; l = l->next)
13225 {
13226 const char **slot;
13227 const char *name = l->function_name;
13228
13229 /* Allow for some names to be NULL, ignore them. */
13230 if (name == NULL)
13231 continue;
13232
13233 slot = (const char **) htab_find_slot (htab, (const void *) name,
13234 INSERT);
4a64f543
MS
13235 /* NOTE: We can assume slot != NULL here because xcalloc never
13236 returns NULL. */
776592bf
DE
13237 if (*slot != NULL)
13238 {
13239 htab_delete (htab);
13240 return 1;
13241 }
13242 *slot = name;
13243 }
13244
13245 htab_delete (htab);
13246 return 0;
13247}
13248
0fb4aa4b
PA
13249/* When symbols change, it probably means the sources changed as well,
13250 and it might mean the static tracepoint markers are no longer at
13251 the same address or line numbers they used to be at last we
13252 checked. Losing your static tracepoints whenever you rebuild is
13253 undesirable. This function tries to resync/rematch gdb static
13254 tracepoints with the markers on the target, for static tracepoints
13255 that have not been set by marker id. Static tracepoint that have
13256 been set by marker id are reset by marker id in breakpoint_re_set.
13257 The heuristic is:
13258
13259 1) For a tracepoint set at a specific address, look for a marker at
13260 the old PC. If one is found there, assume to be the same marker.
13261 If the name / string id of the marker found is different from the
13262 previous known name, assume that means the user renamed the marker
13263 in the sources, and output a warning.
13264
13265 2) For a tracepoint set at a given line number, look for a marker
13266 at the new address of the old line number. If one is found there,
13267 assume to be the same marker. If the name / string id of the
13268 marker found is different from the previous known name, assume that
13269 means the user renamed the marker in the sources, and output a
13270 warning.
13271
13272 3) If a marker is no longer found at the same address or line, it
13273 may mean the marker no longer exists. But it may also just mean
13274 the code changed a bit. Maybe the user added a few lines of code
13275 that made the marker move up or down (in line number terms). Ask
13276 the target for info about the marker with the string id as we knew
13277 it. If found, update line number and address in the matching
13278 static tracepoint. This will get confused if there's more than one
13279 marker with the same ID (possible in UST, although unadvised
13280 precisely because it confuses tools). */
13281
13282static struct symtab_and_line
13283update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
13284{
d9b3f62e 13285 struct tracepoint *tp = (struct tracepoint *) b;
0fb4aa4b
PA
13286 struct static_tracepoint_marker marker;
13287 CORE_ADDR pc;
0fb4aa4b
PA
13288
13289 pc = sal.pc;
13290 if (sal.line)
13291 find_line_pc (sal.symtab, sal.line, &pc);
13292
13293 if (target_static_tracepoint_marker_at (pc, &marker))
13294 {
5d9310c4 13295 if (tp->static_trace_marker_id != marker.str_id)
0fb4aa4b 13296 warning (_("static tracepoint %d changed probed marker from %s to %s"),
5d9310c4
SM
13297 b->number, tp->static_trace_marker_id.c_str (),
13298 marker.str_id.c_str ());
0fb4aa4b 13299
5d9310c4 13300 tp->static_trace_marker_id = std::move (marker.str_id);
0fb4aa4b
PA
13301
13302 return sal;
13303 }
13304
13305 /* Old marker wasn't found on target at lineno. Try looking it up
13306 by string ID. */
13307 if (!sal.explicit_pc
13308 && sal.line != 0
13309 && sal.symtab != NULL
5d9310c4 13310 && !tp->static_trace_marker_id.empty ())
0fb4aa4b 13311 {
5d9310c4
SM
13312 std::vector<static_tracepoint_marker> markers
13313 = target_static_tracepoint_markers_by_strid
13314 (tp->static_trace_marker_id.c_str ());
0fb4aa4b 13315
5d9310c4 13316 if (!markers.empty ())
0fb4aa4b 13317 {
0fb4aa4b 13318 struct symbol *sym;
80e1d417 13319 struct static_tracepoint_marker *tpmarker;
79a45e25 13320 struct ui_out *uiout = current_uiout;
67994074 13321 struct explicit_location explicit_loc;
0fb4aa4b 13322
5d9310c4 13323 tpmarker = &markers[0];
0fb4aa4b 13324
5d9310c4 13325 tp->static_trace_marker_id = std::move (tpmarker->str_id);
0fb4aa4b
PA
13326
13327 warning (_("marker for static tracepoint %d (%s) not "
13328 "found at previous line number"),
5d9310c4 13329 b->number, tp->static_trace_marker_id.c_str ());
0fb4aa4b 13330
51abb421 13331 symtab_and_line sal2 = find_pc_line (tpmarker->address, 0);
80e1d417 13332 sym = find_pc_sect_function (tpmarker->address, NULL);
112e8700 13333 uiout->text ("Now in ");
0fb4aa4b
PA
13334 if (sym)
13335 {
987012b8 13336 uiout->field_string ("func", sym->print_name (),
e43b10e1 13337 function_name_style.style ());
112e8700 13338 uiout->text (" at ");
0fb4aa4b 13339 }
112e8700 13340 uiout->field_string ("file",
cbe56571 13341 symtab_to_filename_for_display (sal2.symtab),
e43b10e1 13342 file_name_style.style ());
112e8700 13343 uiout->text (":");
0fb4aa4b 13344
112e8700 13345 if (uiout->is_mi_like_p ())
0fb4aa4b 13346 {
0b0865da 13347 const char *fullname = symtab_to_fullname (sal2.symtab);
0fb4aa4b 13348
112e8700 13349 uiout->field_string ("fullname", fullname);
0fb4aa4b
PA
13350 }
13351
381befee 13352 uiout->field_signed ("line", sal2.line);
112e8700 13353 uiout->text ("\n");
0fb4aa4b 13354
80e1d417 13355 b->loc->line_number = sal2.line;
2f202fde 13356 b->loc->symtab = sym != NULL ? sal2.symtab : NULL;
0fb4aa4b 13357
d28cd78a 13358 b->location.reset (NULL);
67994074
KS
13359 initialize_explicit_location (&explicit_loc);
13360 explicit_loc.source_filename
00e52e53 13361 = ASTRDUP (symtab_to_filename_for_display (sal2.symtab));
67994074
KS
13362 explicit_loc.line_offset.offset = b->loc->line_number;
13363 explicit_loc.line_offset.sign = LINE_OFFSET_NONE;
d28cd78a 13364 b->location = new_explicit_location (&explicit_loc);
0fb4aa4b
PA
13365
13366 /* Might be nice to check if function changed, and warn if
13367 so. */
0fb4aa4b
PA
13368 }
13369 }
13370 return sal;
13371}
13372
8d3788bd
VP
13373/* Returns 1 iff locations A and B are sufficiently same that
13374 we don't need to report breakpoint as changed. */
13375
13376static int
13377locations_are_equal (struct bp_location *a, struct bp_location *b)
13378{
13379 while (a && b)
13380 {
13381 if (a->address != b->address)
13382 return 0;
13383
13384 if (a->shlib_disabled != b->shlib_disabled)
13385 return 0;
13386
13387 if (a->enabled != b->enabled)
13388 return 0;
13389
13390 a = a->next;
13391 b = b->next;
13392 }
13393
13394 if ((a == NULL) != (b == NULL))
13395 return 0;
13396
13397 return 1;
13398}
13399
c2f4122d
PA
13400/* Split all locations of B that are bound to PSPACE out of B's
13401 location list to a separate list and return that list's head. If
13402 PSPACE is NULL, hoist out all locations of B. */
13403
13404static struct bp_location *
13405hoist_existing_locations (struct breakpoint *b, struct program_space *pspace)
13406{
13407 struct bp_location head;
13408 struct bp_location *i = b->loc;
13409 struct bp_location **i_link = &b->loc;
13410 struct bp_location *hoisted = &head;
13411
13412 if (pspace == NULL)
13413 {
13414 i = b->loc;
13415 b->loc = NULL;
13416 return i;
13417 }
13418
13419 head.next = NULL;
13420
13421 while (i != NULL)
13422 {
13423 if (i->pspace == pspace)
13424 {
13425 *i_link = i->next;
13426 i->next = NULL;
13427 hoisted->next = i;
13428 hoisted = i;
13429 }
13430 else
13431 i_link = &i->next;
13432 i = *i_link;
13433 }
13434
13435 return head.next;
13436}
13437
13438/* Create new breakpoint locations for B (a hardware or software
13439 breakpoint) based on SALS and SALS_END. If SALS_END.NELTS is not
13440 zero, then B is a ranged breakpoint. Only recreates locations for
13441 FILTER_PSPACE. Locations of other program spaces are left
13442 untouched. */
f1310107 13443
0e30163f 13444void
0d381245 13445update_breakpoint_locations (struct breakpoint *b,
c2f4122d 13446 struct program_space *filter_pspace,
6c5b2ebe
PA
13447 gdb::array_view<const symtab_and_line> sals,
13448 gdb::array_view<const symtab_and_line> sals_end)
fe3f5fa8 13449{
c2f4122d 13450 struct bp_location *existing_locations;
0d381245 13451
6c5b2ebe 13452 if (!sals_end.empty () && (sals.size () != 1 || sals_end.size () != 1))
f8eba3c6
TT
13453 {
13454 /* Ranged breakpoints have only one start location and one end
13455 location. */
13456 b->enable_state = bp_disabled;
f8eba3c6
TT
13457 printf_unfiltered (_("Could not reset ranged breakpoint %d: "
13458 "multiple locations found\n"),
13459 b->number);
13460 return;
13461 }
f1310107 13462
4a64f543
MS
13463 /* If there's no new locations, and all existing locations are
13464 pending, don't do anything. This optimizes the common case where
13465 all locations are in the same shared library, that was unloaded.
13466 We'd like to retain the location, so that when the library is
13467 loaded again, we don't loose the enabled/disabled status of the
13468 individual locations. */
6c5b2ebe 13469 if (all_locations_are_pending (b, filter_pspace) && sals.empty ())
fe3f5fa8
VP
13470 return;
13471
c2f4122d 13472 existing_locations = hoist_existing_locations (b, filter_pspace);
fe3f5fa8 13473
6c5b2ebe 13474 for (const auto &sal : sals)
fe3f5fa8 13475 {
f8eba3c6
TT
13476 struct bp_location *new_loc;
13477
6c5b2ebe 13478 switch_to_program_space_and_thread (sal.pspace);
f8eba3c6 13479
6c5b2ebe 13480 new_loc = add_location_to_breakpoint (b, &sal);
fe3f5fa8 13481
0d381245
VP
13482 /* Reparse conditions, they might contain references to the
13483 old symtab. */
13484 if (b->cond_string != NULL)
13485 {
bbc13ae3 13486 const char *s;
fe3f5fa8 13487
0d381245 13488 s = b->cond_string;
a70b8144 13489 try
0d381245 13490 {
6c5b2ebe
PA
13491 new_loc->cond = parse_exp_1 (&s, sal.pc,
13492 block_for_pc (sal.pc),
0d381245
VP
13493 0);
13494 }
230d2906 13495 catch (const gdb_exception_error &e)
0d381245 13496 {
3e43a32a
MS
13497 warning (_("failed to reevaluate condition "
13498 "for breakpoint %d: %s"),
3d6e9d23 13499 b->number, e.what ());
0d381245
VP
13500 new_loc->enabled = 0;
13501 }
13502 }
fe3f5fa8 13503
6c5b2ebe 13504 if (!sals_end.empty ())
f1310107 13505 {
6c5b2ebe 13506 CORE_ADDR end = find_breakpoint_range_end (sals_end[0]);
f1310107 13507
6c5b2ebe 13508 new_loc->length = end - sals[0].pc + 1;
f1310107 13509 }
0d381245 13510 }
fe3f5fa8 13511
4a64f543
MS
13512 /* If possible, carry over 'disable' status from existing
13513 breakpoints. */
0d381245
VP
13514 {
13515 struct bp_location *e = existing_locations;
776592bf
DE
13516 /* If there are multiple breakpoints with the same function name,
13517 e.g. for inline functions, comparing function names won't work.
13518 Instead compare pc addresses; this is just a heuristic as things
13519 may have moved, but in practice it gives the correct answer
13520 often enough until a better solution is found. */
13521 int have_ambiguous_names = ambiguous_names_p (b->loc);
13522
0d381245
VP
13523 for (; e; e = e->next)
13524 {
13525 if (!e->enabled && e->function_name)
13526 {
13527 struct bp_location *l = b->loc;
776592bf
DE
13528 if (have_ambiguous_names)
13529 {
13530 for (; l; l = l->next)
f1310107 13531 if (breakpoint_locations_match (e, l))
776592bf
DE
13532 {
13533 l->enabled = 0;
13534 break;
13535 }
13536 }
13537 else
13538 {
13539 for (; l; l = l->next)
13540 if (l->function_name
13541 && strcmp (e->function_name, l->function_name) == 0)
13542 {
13543 l->enabled = 0;
13544 break;
13545 }
13546 }
0d381245
VP
13547 }
13548 }
13549 }
fe3f5fa8 13550
8d3788bd 13551 if (!locations_are_equal (existing_locations, b->loc))
76727919 13552 gdb::observers::breakpoint_modified.notify (b);
fe3f5fa8
VP
13553}
13554
f00aae0f 13555/* Find the SaL locations corresponding to the given LOCATION.
ef23e705
TJB
13556 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
13557
6c5b2ebe 13558static std::vector<symtab_and_line>
f00aae0f 13559location_to_sals (struct breakpoint *b, struct event_location *location,
c2f4122d 13560 struct program_space *search_pspace, int *found)
ef23e705 13561{
cc06b668 13562 struct gdb_exception exception;
ef23e705 13563
983af33b 13564 gdb_assert (b->ops != NULL);
ef23e705 13565
6c5b2ebe
PA
13566 std::vector<symtab_and_line> sals;
13567
a70b8144 13568 try
ef23e705 13569 {
6c5b2ebe 13570 sals = b->ops->decode_location (b, location, search_pspace);
ef23e705 13571 }
94aeb44b 13572 catch (gdb_exception_error &e)
ef23e705
TJB
13573 {
13574 int not_found_and_ok = 0;
492d29ea 13575
ef23e705
TJB
13576 /* For pending breakpoints, it's expected that parsing will
13577 fail until the right shared library is loaded. User has
13578 already told to create pending breakpoints and don't need
13579 extra messages. If breakpoint is in bp_shlib_disabled
13580 state, then user already saw the message about that
13581 breakpoint being disabled, and don't want to see more
13582 errors. */
58438ac1 13583 if (e.error == NOT_FOUND_ERROR
c2f4122d
PA
13584 && (b->condition_not_parsed
13585 || (b->loc != NULL
13586 && search_pspace != NULL
13587 && b->loc->pspace != search_pspace)
ef23e705 13588 || (b->loc && b->loc->shlib_disabled)
f8eba3c6 13589 || (b->loc && b->loc->pspace->executing_startup)
ef23e705
TJB
13590 || b->enable_state == bp_disabled))
13591 not_found_and_ok = 1;
13592
13593 if (!not_found_and_ok)
13594 {
13595 /* We surely don't want to warn about the same breakpoint
13596 10 times. One solution, implemented here, is disable
13597 the breakpoint on error. Another solution would be to
13598 have separate 'warning emitted' flag. Since this
13599 happens only when a binary has changed, I don't know
13600 which approach is better. */
13601 b->enable_state = bp_disabled;
eedc3f4f 13602 throw;
ef23e705 13603 }
94aeb44b
TT
13604
13605 exception = std::move (e);
ef23e705
TJB
13606 }
13607
492d29ea 13608 if (exception.reason == 0 || exception.error != NOT_FOUND_ERROR)
ef23e705 13609 {
6c5b2ebe
PA
13610 for (auto &sal : sals)
13611 resolve_sal_pc (&sal);
f00aae0f 13612 if (b->condition_not_parsed && b->extra_string != NULL)
ef23e705 13613 {
ed1d1739
KS
13614 char *cond_string, *extra_string;
13615 int thread, task;
ef23e705 13616
6c5b2ebe 13617 find_condition_and_thread (b->extra_string, sals[0].pc,
e7e0cddf
SS
13618 &cond_string, &thread, &task,
13619 &extra_string);
f00aae0f 13620 gdb_assert (b->cond_string == NULL);
ef23e705
TJB
13621 if (cond_string)
13622 b->cond_string = cond_string;
13623 b->thread = thread;
13624 b->task = task;
e7e0cddf 13625 if (extra_string)
f00aae0f
KS
13626 {
13627 xfree (b->extra_string);
13628 b->extra_string = extra_string;
13629 }
ef23e705
TJB
13630 b->condition_not_parsed = 0;
13631 }
13632
983af33b 13633 if (b->type == bp_static_tracepoint && !strace_marker_p (b))
6c5b2ebe 13634 sals[0] = update_static_tracepoint (b, sals[0]);
ef23e705 13635
58438ac1
TT
13636 *found = 1;
13637 }
13638 else
13639 *found = 0;
ef23e705
TJB
13640
13641 return sals;
13642}
13643
348d480f
PA
13644/* The default re_set method, for typical hardware or software
13645 breakpoints. Reevaluate the breakpoint and recreate its
13646 locations. */
13647
13648static void
28010a5d 13649breakpoint_re_set_default (struct breakpoint *b)
ef23e705 13650{
c2f4122d 13651 struct program_space *filter_pspace = current_program_space;
6c5b2ebe 13652 std::vector<symtab_and_line> expanded, expanded_end;
ef23e705 13653
6c5b2ebe
PA
13654 int found;
13655 std::vector<symtab_and_line> sals = location_to_sals (b, b->location.get (),
13656 filter_pspace, &found);
ef23e705 13657 if (found)
6c5b2ebe 13658 expanded = std::move (sals);
ef23e705 13659
f00aae0f 13660 if (b->location_range_end != NULL)
f1310107 13661 {
6c5b2ebe
PA
13662 std::vector<symtab_and_line> sals_end
13663 = location_to_sals (b, b->location_range_end.get (),
13664 filter_pspace, &found);
f1310107 13665 if (found)
6c5b2ebe 13666 expanded_end = std::move (sals_end);
f1310107
TJB
13667 }
13668
c2f4122d 13669 update_breakpoint_locations (b, filter_pspace, expanded, expanded_end);
28010a5d
PA
13670}
13671
983af33b
SDJ
13672/* Default method for creating SALs from an address string. It basically
13673 calls parse_breakpoint_sals. Return 1 for success, zero for failure. */
13674
13675static void
f00aae0f
KS
13676create_sals_from_location_default (const struct event_location *location,
13677 struct linespec_result *canonical,
13678 enum bptype type_wanted)
983af33b 13679{
f00aae0f 13680 parse_breakpoint_sals (location, canonical);
983af33b
SDJ
13681}
13682
13683/* Call create_breakpoints_sal for the given arguments. This is the default
13684 function for the `create_breakpoints_sal' method of
13685 breakpoint_ops. */
13686
13687static void
13688create_breakpoints_sal_default (struct gdbarch *gdbarch,
13689 struct linespec_result *canonical,
e1e01040
PA
13690 gdb::unique_xmalloc_ptr<char> cond_string,
13691 gdb::unique_xmalloc_ptr<char> extra_string,
983af33b
SDJ
13692 enum bptype type_wanted,
13693 enum bpdisp disposition,
13694 int thread,
13695 int task, int ignore_count,
13696 const struct breakpoint_ops *ops,
13697 int from_tty, int enabled,
44f238bb 13698 int internal, unsigned flags)
983af33b 13699{
e1e01040
PA
13700 create_breakpoints_sal (gdbarch, canonical,
13701 std::move (cond_string),
13702 std::move (extra_string),
983af33b
SDJ
13703 type_wanted, disposition,
13704 thread, task, ignore_count, ops, from_tty,
44f238bb 13705 enabled, internal, flags);
983af33b
SDJ
13706}
13707
13708/* Decode the line represented by S by calling decode_line_full. This is the
5f700d83 13709 default function for the `decode_location' method of breakpoint_ops. */
983af33b 13710
6c5b2ebe 13711static std::vector<symtab_and_line>
f00aae0f
KS
13712decode_location_default (struct breakpoint *b,
13713 const struct event_location *location,
6c5b2ebe 13714 struct program_space *search_pspace)
983af33b
SDJ
13715{
13716 struct linespec_result canonical;
13717
c2f4122d 13718 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, search_pspace,
cafb3438 13719 NULL, 0, &canonical, multiple_symbols_all,
c0e8dcd8 13720 b->filter.get ());
983af33b
SDJ
13721
13722 /* We should get 0 or 1 resulting SALs. */
6c5b2ebe 13723 gdb_assert (canonical.lsals.size () < 2);
983af33b 13724
6c5b2ebe 13725 if (!canonical.lsals.empty ())
983af33b 13726 {
6c5b2ebe
PA
13727 const linespec_sals &lsal = canonical.lsals[0];
13728 return std::move (lsal.sals);
983af33b 13729 }
6c5b2ebe 13730 return {};
983af33b
SDJ
13731}
13732
bf469271 13733/* Reset a breakpoint. */
c906108c 13734
bf469271
PA
13735static void
13736breakpoint_re_set_one (breakpoint *b)
c906108c 13737{
fdf44873
TT
13738 input_radix = b->input_radix;
13739 set_language (b->language);
c906108c 13740
348d480f 13741 b->ops->re_set (b);
c906108c
SS
13742}
13743
c2f4122d
PA
13744/* Re-set breakpoint locations for the current program space.
13745 Locations bound to other program spaces are left untouched. */
13746
c906108c 13747void
69de3c6a 13748breakpoint_re_set (void)
c906108c 13749{
35df4500 13750 struct breakpoint *b, *b_tmp;
2a7f3dff 13751
c5aa993b 13752 {
fdf44873
TT
13753 scoped_restore_current_language save_language;
13754 scoped_restore save_input_radix = make_scoped_restore (&input_radix);
5ed8105e 13755 scoped_restore_current_pspace_and_thread restore_pspace_thread;
e62c965a 13756
8e817061
JB
13757 /* breakpoint_re_set_one sets the current_language to the language
13758 of the breakpoint it is resetting (see prepare_re_set_context)
13759 before re-evaluating the breakpoint's location. This change can
13760 unfortunately get undone by accident if the language_mode is set
13761 to auto, and we either switch frames, or more likely in this context,
13762 we select the current frame.
13763
13764 We prevent this by temporarily turning the language_mode to
13765 language_mode_manual. We restore it once all breakpoints
13766 have been reset. */
13767 scoped_restore save_language_mode = make_scoped_restore (&language_mode);
13768 language_mode = language_mode_manual;
13769
5ed8105e
PA
13770 /* Note: we must not try to insert locations until after all
13771 breakpoints have been re-set. Otherwise, e.g., when re-setting
13772 breakpoint 1, we'd insert the locations of breakpoint 2, which
13773 hadn't been re-set yet, and thus may have stale locations. */
4efc6507 13774
5ed8105e
PA
13775 ALL_BREAKPOINTS_SAFE (b, b_tmp)
13776 {
a70b8144 13777 try
bf469271
PA
13778 {
13779 breakpoint_re_set_one (b);
13780 }
230d2906 13781 catch (const gdb_exception &ex)
bf469271
PA
13782 {
13783 exception_fprintf (gdb_stderr, ex,
13784 "Error in re-setting breakpoint %d: ",
13785 b->number);
13786 }
5ed8105e 13787 }
5ed8105e
PA
13788
13789 jit_breakpoint_re_set ();
13790 }
6c95b8df 13791
af02033e
PP
13792 create_overlay_event_breakpoint ();
13793 create_longjmp_master_breakpoint ();
13794 create_std_terminate_master_breakpoint ();
186c406b 13795 create_exception_master_breakpoint ();
2a7f3dff
PA
13796
13797 /* Now we can insert. */
13798 update_global_location_list (UGLL_MAY_INSERT);
c906108c
SS
13799}
13800\f
c906108c
SS
13801/* Reset the thread number of this breakpoint:
13802
13803 - If the breakpoint is for all threads, leave it as-is.
4a64f543 13804 - Else, reset it to the current thread for inferior_ptid. */
c906108c 13805void
fba45db2 13806breakpoint_re_set_thread (struct breakpoint *b)
c906108c
SS
13807{
13808 if (b->thread != -1)
13809 {
00431a78 13810 b->thread = inferior_thread ()->global_num;
6c95b8df
PA
13811
13812 /* We're being called after following a fork. The new fork is
13813 selected as current, and unless this was a vfork will have a
13814 different program space from the original thread. Reset that
13815 as well. */
13816 b->loc->pspace = current_program_space;
c906108c
SS
13817 }
13818}
13819
03ac34d5
MS
13820/* Set ignore-count of breakpoint number BPTNUM to COUNT.
13821 If from_tty is nonzero, it prints a message to that effect,
13822 which ends with a period (no newline). */
13823
c906108c 13824void
fba45db2 13825set_ignore_count (int bptnum, int count, int from_tty)
c906108c 13826{
52f0bd74 13827 struct breakpoint *b;
c906108c
SS
13828
13829 if (count < 0)
13830 count = 0;
13831
13832 ALL_BREAKPOINTS (b)
13833 if (b->number == bptnum)
c5aa993b 13834 {
d77f58be
SS
13835 if (is_tracepoint (b))
13836 {
13837 if (from_tty && count != 0)
13838 printf_filtered (_("Ignore count ignored for tracepoint %d."),
13839 bptnum);
13840 return;
13841 }
13842
c5aa993b 13843 b->ignore_count = count;
221ea385
KS
13844 if (from_tty)
13845 {
13846 if (count == 0)
3e43a32a
MS
13847 printf_filtered (_("Will stop next time "
13848 "breakpoint %d is reached."),
221ea385
KS
13849 bptnum);
13850 else if (count == 1)
a3f17187 13851 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
221ea385
KS
13852 bptnum);
13853 else
3e43a32a
MS
13854 printf_filtered (_("Will ignore next %d "
13855 "crossings of breakpoint %d."),
221ea385
KS
13856 count, bptnum);
13857 }
76727919 13858 gdb::observers::breakpoint_modified.notify (b);
c5aa993b
JM
13859 return;
13860 }
c906108c 13861
8a3fe4f8 13862 error (_("No breakpoint number %d."), bptnum);
c906108c
SS
13863}
13864
c906108c
SS
13865/* Command to set ignore-count of breakpoint N to COUNT. */
13866
13867static void
0b39b52e 13868ignore_command (const char *args, int from_tty)
c906108c 13869{
0b39b52e 13870 const char *p = args;
52f0bd74 13871 int num;
c906108c
SS
13872
13873 if (p == 0)
e2e0b3e5 13874 error_no_arg (_("a breakpoint number"));
c5aa993b 13875
c906108c 13876 num = get_number (&p);
5c44784c 13877 if (num == 0)
8a3fe4f8 13878 error (_("bad breakpoint number: '%s'"), args);
c906108c 13879 if (*p == 0)
8a3fe4f8 13880 error (_("Second argument (specified ignore-count) is missing."));
c906108c
SS
13881
13882 set_ignore_count (num,
13883 longest_to_int (value_as_long (parse_and_eval (p))),
13884 from_tty);
221ea385
KS
13885 if (from_tty)
13886 printf_filtered ("\n");
c906108c
SS
13887}
13888\f
d0fe4701
XR
13889
13890/* Call FUNCTION on each of the breakpoints with numbers in the range
13891 defined by BP_NUM_RANGE (an inclusive range). */
c906108c
SS
13892
13893static void
d0fe4701
XR
13894map_breakpoint_number_range (std::pair<int, int> bp_num_range,
13895 gdb::function_view<void (breakpoint *)> function)
c906108c 13896{
d0fe4701
XR
13897 if (bp_num_range.first == 0)
13898 {
13899 warning (_("bad breakpoint number at or near '%d'"),
13900 bp_num_range.first);
13901 }
13902 else
c906108c 13903 {
d0fe4701 13904 struct breakpoint *b, *tmp;
197f0a60 13905
d0fe4701 13906 for (int i = bp_num_range.first; i <= bp_num_range.second; i++)
5c44784c 13907 {
d0fe4701
XR
13908 bool match = false;
13909
5c44784c 13910 ALL_BREAKPOINTS_SAFE (b, tmp)
d0fe4701 13911 if (b->number == i)
5c44784c 13912 {
bfd28288 13913 match = true;
48649e1b 13914 function (b);
11cf8741 13915 break;
5c44784c 13916 }
bfd28288 13917 if (!match)
d0fe4701 13918 printf_unfiltered (_("No breakpoint number %d.\n"), i);
c5aa993b 13919 }
c906108c
SS
13920 }
13921}
13922
d0fe4701
XR
13923/* Call FUNCTION on each of the breakpoints whose numbers are given in
13924 ARGS. */
13925
13926static void
13927map_breakpoint_numbers (const char *args,
13928 gdb::function_view<void (breakpoint *)> function)
13929{
13930 if (args == NULL || *args == '\0')
13931 error_no_arg (_("one or more breakpoint numbers"));
13932
13933 number_or_range_parser parser (args);
13934
13935 while (!parser.finished ())
13936 {
13937 int num = parser.get_number ();
13938 map_breakpoint_number_range (std::make_pair (num, num), function);
13939 }
13940}
13941
13942/* Return the breakpoint location structure corresponding to the
13943 BP_NUM and LOC_NUM values. */
13944
0d381245 13945static struct bp_location *
d0fe4701 13946find_location_by_number (int bp_num, int loc_num)
0d381245 13947{
0d381245 13948 struct breakpoint *b;
0d381245
VP
13949
13950 ALL_BREAKPOINTS (b)
13951 if (b->number == bp_num)
13952 {
13953 break;
13954 }
13955
13956 if (!b || b->number != bp_num)
d0fe4701 13957 error (_("Bad breakpoint number '%d'"), bp_num);
0d381245 13958
0d381245 13959 if (loc_num == 0)
d0fe4701 13960 error (_("Bad breakpoint location number '%d'"), loc_num);
0d381245 13961
d0fe4701
XR
13962 int n = 0;
13963 for (bp_location *loc = b->loc; loc != NULL; loc = loc->next)
13964 if (++n == loc_num)
13965 return loc;
13966
13967 error (_("Bad breakpoint location number '%d'"), loc_num);
0d381245
VP
13968}
13969
95e95a6d
PA
13970/* Modes of operation for extract_bp_num. */
13971enum class extract_bp_kind
13972{
13973 /* Extracting a breakpoint number. */
13974 bp,
13975
13976 /* Extracting a location number. */
13977 loc,
13978};
13979
13980/* Extract a breakpoint or location number (as determined by KIND)
13981 from the string starting at START. TRAILER is a character which
13982 can be found after the number. If you don't want a trailer, use
13983 '\0'. If END_OUT is not NULL, it is set to point after the parsed
13984 string. This always returns a positive integer. */
13985
13986static int
13987extract_bp_num (extract_bp_kind kind, const char *start,
13988 int trailer, const char **end_out = NULL)
13989{
13990 const char *end = start;
13991 int num = get_number_trailer (&end, trailer);
13992 if (num < 0)
13993 error (kind == extract_bp_kind::bp
13994 ? _("Negative breakpoint number '%.*s'")
13995 : _("Negative breakpoint location number '%.*s'"),
13996 int (end - start), start);
13997 if (num == 0)
13998 error (kind == extract_bp_kind::bp
13999 ? _("Bad breakpoint number '%.*s'")
14000 : _("Bad breakpoint location number '%.*s'"),
14001 int (end - start), start);
14002
14003 if (end_out != NULL)
14004 *end_out = end;
14005 return num;
14006}
14007
14008/* Extract a breakpoint or location range (as determined by KIND) in
14009 the form NUM1-NUM2 stored at &ARG[arg_offset]. Returns a std::pair
14010 representing the (inclusive) range. The returned pair's elements
14011 are always positive integers. */
14012
14013static std::pair<int, int>
14014extract_bp_or_bp_range (extract_bp_kind kind,
14015 const std::string &arg,
14016 std::string::size_type arg_offset)
14017{
14018 std::pair<int, int> range;
14019 const char *bp_loc = &arg[arg_offset];
14020 std::string::size_type dash = arg.find ('-', arg_offset);
14021 if (dash != std::string::npos)
14022 {
14023 /* bp_loc is a range (x-z). */
14024 if (arg.length () == dash + 1)
14025 error (kind == extract_bp_kind::bp
14026 ? _("Bad breakpoint number at or near: '%s'")
14027 : _("Bad breakpoint location number at or near: '%s'"),
14028 bp_loc);
14029
14030 const char *end;
14031 const char *start_first = bp_loc;
14032 const char *start_second = &arg[dash + 1];
14033 range.first = extract_bp_num (kind, start_first, '-');
14034 range.second = extract_bp_num (kind, start_second, '\0', &end);
14035
14036 if (range.first > range.second)
14037 error (kind == extract_bp_kind::bp
14038 ? _("Inverted breakpoint range at '%.*s'")
14039 : _("Inverted breakpoint location range at '%.*s'"),
14040 int (end - start_first), start_first);
14041 }
14042 else
14043 {
14044 /* bp_loc is a single value. */
14045 range.first = extract_bp_num (kind, bp_loc, '\0');
14046 range.second = range.first;
14047 }
14048 return range;
14049}
14050
d0fe4701
XR
14051/* Extract the breakpoint/location range specified by ARG. Returns
14052 the breakpoint range in BP_NUM_RANGE, and the location range in
14053 BP_LOC_RANGE.
14054
14055 ARG may be in any of the following forms:
14056
14057 x where 'x' is a breakpoint number.
14058 x-y where 'x' and 'y' specify a breakpoint numbers range.
14059 x.y where 'x' is a breakpoint number and 'y' a location number.
14060 x.y-z where 'x' is a breakpoint number and 'y' and 'z' specify a
14061 location number range.
14062*/
14063
cc638e86 14064static void
d0fe4701
XR
14065extract_bp_number_and_location (const std::string &arg,
14066 std::pair<int, int> &bp_num_range,
14067 std::pair<int, int> &bp_loc_range)
14068{
14069 std::string::size_type dot = arg.find ('.');
14070
14071 if (dot != std::string::npos)
14072 {
14073 /* Handle 'x.y' and 'x.y-z' cases. */
14074
14075 if (arg.length () == dot + 1 || dot == 0)
95e95a6d 14076 error (_("Bad breakpoint number at or near: '%s'"), arg.c_str ());
d0fe4701 14077
95e95a6d
PA
14078 bp_num_range.first
14079 = extract_bp_num (extract_bp_kind::bp, arg.c_str (), '.');
14080 bp_num_range.second = bp_num_range.first;
d0fe4701 14081
95e95a6d
PA
14082 bp_loc_range = extract_bp_or_bp_range (extract_bp_kind::loc,
14083 arg, dot + 1);
d0fe4701
XR
14084 }
14085 else
14086 {
14087 /* Handle x and x-y cases. */
d0fe4701 14088
95e95a6d 14089 bp_num_range = extract_bp_or_bp_range (extract_bp_kind::bp, arg, 0);
d0fe4701
XR
14090 bp_loc_range.first = 0;
14091 bp_loc_range.second = 0;
14092 }
d0fe4701
XR
14093}
14094
14095/* Enable or disable a breakpoint location BP_NUM.LOC_NUM. ENABLE
14096 specifies whether to enable or disable. */
14097
14098static void
14099enable_disable_bp_num_loc (int bp_num, int loc_num, bool enable)
14100{
14101 struct bp_location *loc = find_location_by_number (bp_num, loc_num);
14102 if (loc != NULL)
14103 {
14104 if (loc->enabled != enable)
14105 {
14106 loc->enabled = enable;
14107 mark_breakpoint_location_modified (loc);
14108 }
14109 if (target_supports_enable_disable_tracepoint ()
14110 && current_trace_status ()->running && loc->owner
14111 && is_tracepoint (loc->owner))
14112 target_disable_tracepoint (loc);
14113 }
14114 update_global_location_list (UGLL_DONT_INSERT);
d7154a8d
JV
14115
14116 gdb::observers::breakpoint_modified.notify (loc->owner);
d0fe4701
XR
14117}
14118
14119/* Enable or disable a range of breakpoint locations. BP_NUM is the
14120 number of the breakpoint, and BP_LOC_RANGE specifies the
14121 (inclusive) range of location numbers of that breakpoint to
14122 enable/disable. ENABLE specifies whether to enable or disable the
14123 location. */
14124
14125static void
14126enable_disable_breakpoint_location_range (int bp_num,
14127 std::pair<int, int> &bp_loc_range,
14128 bool enable)
14129{
14130 for (int i = bp_loc_range.first; i <= bp_loc_range.second; i++)
14131 enable_disable_bp_num_loc (bp_num, i, enable);
14132}
0d381245 14133
1900040c
MS
14134/* Set ignore-count of breakpoint number BPTNUM to COUNT.
14135 If from_tty is nonzero, it prints a message to that effect,
14136 which ends with a period (no newline). */
14137
c906108c 14138void
fba45db2 14139disable_breakpoint (struct breakpoint *bpt)
c906108c
SS
14140{
14141 /* Never disable a watchpoint scope breakpoint; we want to
14142 hit them when we leave scope so we can delete both the
14143 watchpoint and its scope breakpoint at that time. */
14144 if (bpt->type == bp_watchpoint_scope)
14145 return;
14146
b5de0fa7 14147 bpt->enable_state = bp_disabled;
c906108c 14148
b775012e
LM
14149 /* Mark breakpoint locations modified. */
14150 mark_breakpoint_modified (bpt);
14151
d248b706
KY
14152 if (target_supports_enable_disable_tracepoint ()
14153 && current_trace_status ()->running && is_tracepoint (bpt))
14154 {
14155 struct bp_location *location;
14156
14157 for (location = bpt->loc; location; location = location->next)
14158 target_disable_tracepoint (location);
14159 }
14160
44702360 14161 update_global_location_list (UGLL_DONT_INSERT);
c906108c 14162
76727919 14163 gdb::observers::breakpoint_modified.notify (bpt);
c906108c
SS
14164}
14165
d0fe4701
XR
14166/* Enable or disable the breakpoint(s) or breakpoint location(s)
14167 specified in ARGS. ARGS may be in any of the formats handled by
14168 extract_bp_number_and_location. ENABLE specifies whether to enable
14169 or disable the breakpoints/locations. */
14170
c906108c 14171static void
d0fe4701 14172enable_disable_command (const char *args, int from_tty, bool enable)
c906108c 14173{
c906108c 14174 if (args == 0)
46c6471b
PA
14175 {
14176 struct breakpoint *bpt;
14177
14178 ALL_BREAKPOINTS (bpt)
14179 if (user_breakpoint_p (bpt))
d0fe4701
XR
14180 {
14181 if (enable)
14182 enable_breakpoint (bpt);
14183 else
14184 disable_breakpoint (bpt);
14185 }
46c6471b 14186 }
9eaabc75 14187 else
0d381245 14188 {
cb791d59 14189 std::string num = extract_arg (&args);
9eaabc75 14190
cb791d59 14191 while (!num.empty ())
d248b706 14192 {
d0fe4701 14193 std::pair<int, int> bp_num_range, bp_loc_range;
9eaabc75 14194
cc638e86
PA
14195 extract_bp_number_and_location (num, bp_num_range, bp_loc_range);
14196
14197 if (bp_loc_range.first == bp_loc_range.second
14198 && bp_loc_range.first == 0)
d0fe4701 14199 {
cc638e86
PA
14200 /* Handle breakpoint ids with formats 'x' or 'x-z'. */
14201 map_breakpoint_number_range (bp_num_range,
14202 enable
14203 ? enable_breakpoint
14204 : disable_breakpoint);
14205 }
14206 else
14207 {
14208 /* Handle breakpoint ids with formats 'x.y' or
14209 'x.y-z'. */
14210 enable_disable_breakpoint_location_range
14211 (bp_num_range.first, bp_loc_range, enable);
b775012e 14212 }
9eaabc75 14213 num = extract_arg (&args);
d248b706 14214 }
0d381245 14215 }
c906108c
SS
14216}
14217
d0fe4701
XR
14218/* The disable command disables the specified breakpoints/locations
14219 (or all defined breakpoints) so they're no longer effective in
14220 stopping the inferior. ARGS may be in any of the forms defined in
14221 extract_bp_number_and_location. */
14222
14223static void
14224disable_command (const char *args, int from_tty)
14225{
14226 enable_disable_command (args, from_tty, false);
14227}
14228
c906108c 14229static void
816338b5
SS
14230enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
14231 int count)
c906108c 14232{
afe38095 14233 int target_resources_ok;
c906108c
SS
14234
14235 if (bpt->type == bp_hardware_breakpoint)
14236 {
14237 int i;
c5aa993b 14238 i = hw_breakpoint_used_count ();
53a5351d 14239 target_resources_ok =
d92524f1 14240 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
53a5351d 14241 i + 1, 0);
c906108c 14242 if (target_resources_ok == 0)
8a3fe4f8 14243 error (_("No hardware breakpoint support in the target."));
c906108c 14244 else if (target_resources_ok < 0)
8a3fe4f8 14245 error (_("Hardware breakpoints used exceeds limit."));
c906108c
SS
14246 }
14247
cc60f2e3 14248 if (is_watchpoint (bpt))
c906108c 14249 {
d07205c2 14250 /* Initialize it just to avoid a GCC false warning. */
f486487f 14251 enum enable_state orig_enable_state = bp_disabled;
dde02812 14252
a70b8144 14253 try
c906108c 14254 {
3a5c3e22
PA
14255 struct watchpoint *w = (struct watchpoint *) bpt;
14256
1e718ff1
TJB
14257 orig_enable_state = bpt->enable_state;
14258 bpt->enable_state = bp_enabled;
3a5c3e22 14259 update_watchpoint (w, 1 /* reparse */);
c906108c 14260 }
230d2906 14261 catch (const gdb_exception &e)
c5aa993b 14262 {
1e718ff1 14263 bpt->enable_state = orig_enable_state;
dde02812
ES
14264 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
14265 bpt->number);
14266 return;
c5aa993b 14267 }
c906108c 14268 }
0101ce28 14269
b775012e
LM
14270 bpt->enable_state = bp_enabled;
14271
14272 /* Mark breakpoint locations modified. */
14273 mark_breakpoint_modified (bpt);
14274
d248b706
KY
14275 if (target_supports_enable_disable_tracepoint ()
14276 && current_trace_status ()->running && is_tracepoint (bpt))
14277 {
14278 struct bp_location *location;
14279
14280 for (location = bpt->loc; location; location = location->next)
14281 target_enable_tracepoint (location);
14282 }
14283
b4c291bb 14284 bpt->disposition = disposition;
816338b5 14285 bpt->enable_count = count;
44702360 14286 update_global_location_list (UGLL_MAY_INSERT);
9c97429f 14287
76727919 14288 gdb::observers::breakpoint_modified.notify (bpt);
c906108c
SS
14289}
14290
fe3f5fa8 14291
c906108c 14292void
fba45db2 14293enable_breakpoint (struct breakpoint *bpt)
c906108c 14294{
816338b5 14295 enable_breakpoint_disp (bpt, bpt->disposition, 0);
51be5b68
PA
14296}
14297
d0fe4701
XR
14298/* The enable command enables the specified breakpoints/locations (or
14299 all defined breakpoints) so they once again become (or continue to
14300 be) effective in stopping the inferior. ARGS may be in any of the
14301 forms defined in extract_bp_number_and_location. */
c906108c 14302
c906108c 14303static void
981a3fb3 14304enable_command (const char *args, int from_tty)
c906108c 14305{
d0fe4701 14306 enable_disable_command (args, from_tty, true);
c906108c
SS
14307}
14308
c906108c 14309static void
4495129a 14310enable_once_command (const char *args, int from_tty)
c906108c 14311{
48649e1b
TT
14312 map_breakpoint_numbers
14313 (args, [&] (breakpoint *b)
14314 {
14315 iterate_over_related_breakpoints
14316 (b, [&] (breakpoint *bpt)
14317 {
14318 enable_breakpoint_disp (bpt, disp_disable, 1);
14319 });
14320 });
816338b5
SS
14321}
14322
14323static void
4495129a 14324enable_count_command (const char *args, int from_tty)
816338b5 14325{
b9d61307
SM
14326 int count;
14327
14328 if (args == NULL)
14329 error_no_arg (_("hit count"));
14330
14331 count = get_number (&args);
816338b5 14332
48649e1b
TT
14333 map_breakpoint_numbers
14334 (args, [&] (breakpoint *b)
14335 {
14336 iterate_over_related_breakpoints
14337 (b, [&] (breakpoint *bpt)
14338 {
14339 enable_breakpoint_disp (bpt, disp_disable, count);
14340 });
14341 });
c906108c
SS
14342}
14343
c906108c 14344static void
4495129a 14345enable_delete_command (const char *args, int from_tty)
c906108c 14346{
48649e1b
TT
14347 map_breakpoint_numbers
14348 (args, [&] (breakpoint *b)
14349 {
14350 iterate_over_related_breakpoints
14351 (b, [&] (breakpoint *bpt)
14352 {
14353 enable_breakpoint_disp (bpt, disp_del, 1);
14354 });
14355 });
c906108c
SS
14356}
14357\f
fa8d40ab 14358static void
981a3fb3 14359set_breakpoint_cmd (const char *args, int from_tty)
fa8d40ab
JJ
14360{
14361}
14362
14363static void
981a3fb3 14364show_breakpoint_cmd (const char *args, int from_tty)
fa8d40ab
JJ
14365{
14366}
14367
1f3b5d1b
PP
14368/* Invalidate last known value of any hardware watchpoint if
14369 the memory which that value represents has been written to by
14370 GDB itself. */
14371
14372static void
8de0566d
YQ
14373invalidate_bp_value_on_memory_change (struct inferior *inferior,
14374 CORE_ADDR addr, ssize_t len,
1f3b5d1b
PP
14375 const bfd_byte *data)
14376{
14377 struct breakpoint *bp;
14378
14379 ALL_BREAKPOINTS (bp)
14380 if (bp->enable_state == bp_enabled
3a5c3e22 14381 && bp->type == bp_hardware_watchpoint)
1f3b5d1b 14382 {
3a5c3e22 14383 struct watchpoint *wp = (struct watchpoint *) bp;
1f3b5d1b 14384
850645cf 14385 if (wp->val_valid && wp->val != nullptr)
3a5c3e22
PA
14386 {
14387 struct bp_location *loc;
14388
14389 for (loc = bp->loc; loc != NULL; loc = loc->next)
14390 if (loc->loc_type == bp_loc_hardware_watchpoint
14391 && loc->address + loc->length > addr
14392 && addr + len > loc->address)
14393 {
3a5c3e22 14394 wp->val = NULL;
4c1d86d9 14395 wp->val_valid = false;
3a5c3e22
PA
14396 }
14397 }
1f3b5d1b
PP
14398 }
14399}
14400
8181d85f
DJ
14401/* Create and insert a breakpoint for software single step. */
14402
14403void
6c95b8df 14404insert_single_step_breakpoint (struct gdbarch *gdbarch,
accd0bcd 14405 const address_space *aspace,
4a64f543 14406 CORE_ADDR next_pc)
8181d85f 14407{
7c16b83e
PA
14408 struct thread_info *tp = inferior_thread ();
14409 struct symtab_and_line sal;
14410 CORE_ADDR pc = next_pc;
8181d85f 14411
34b7e8a6
PA
14412 if (tp->control.single_step_breakpoints == NULL)
14413 {
14414 tp->control.single_step_breakpoints
5d5658a1 14415 = new_single_step_breakpoint (tp->global_num, gdbarch);
34b7e8a6 14416 }
8181d85f 14417
7c16b83e
PA
14418 sal = find_pc_line (pc, 0);
14419 sal.pc = pc;
14420 sal.section = find_pc_overlay (pc);
14421 sal.explicit_pc = 1;
34b7e8a6 14422 add_location_to_breakpoint (tp->control.single_step_breakpoints, &sal);
8181d85f 14423
7c16b83e 14424 update_global_location_list (UGLL_INSERT);
8181d85f
DJ
14425}
14426
93f9a11f
YQ
14427/* Insert single step breakpoints according to the current state. */
14428
14429int
14430insert_single_step_breakpoints (struct gdbarch *gdbarch)
14431{
f5ea389a 14432 struct regcache *regcache = get_current_regcache ();
a0ff9e1a 14433 std::vector<CORE_ADDR> next_pcs;
93f9a11f 14434
f5ea389a 14435 next_pcs = gdbarch_software_single_step (gdbarch, regcache);
93f9a11f 14436
a0ff9e1a 14437 if (!next_pcs.empty ())
93f9a11f 14438 {
f5ea389a 14439 struct frame_info *frame = get_current_frame ();
8b86c959 14440 const address_space *aspace = get_frame_address_space (frame);
93f9a11f 14441
a0ff9e1a 14442 for (CORE_ADDR pc : next_pcs)
93f9a11f
YQ
14443 insert_single_step_breakpoint (gdbarch, aspace, pc);
14444
93f9a11f
YQ
14445 return 1;
14446 }
14447 else
14448 return 0;
14449}
14450
34b7e8a6 14451/* See breakpoint.h. */
f02253f1
HZ
14452
14453int
7c16b83e 14454breakpoint_has_location_inserted_here (struct breakpoint *bp,
accd0bcd 14455 const address_space *aspace,
7c16b83e 14456 CORE_ADDR pc)
1aafd4da 14457{
7c16b83e 14458 struct bp_location *loc;
1aafd4da 14459
7c16b83e
PA
14460 for (loc = bp->loc; loc != NULL; loc = loc->next)
14461 if (loc->inserted
14462 && breakpoint_location_address_match (loc, aspace, pc))
14463 return 1;
1aafd4da 14464
7c16b83e 14465 return 0;
ef370185
JB
14466}
14467
14468/* Check whether a software single-step breakpoint is inserted at
14469 PC. */
14470
14471int
accd0bcd 14472single_step_breakpoint_inserted_here_p (const address_space *aspace,
ef370185
JB
14473 CORE_ADDR pc)
14474{
34b7e8a6
PA
14475 struct breakpoint *bpt;
14476
14477 ALL_BREAKPOINTS (bpt)
14478 {
14479 if (bpt->type == bp_single_step
14480 && breakpoint_has_location_inserted_here (bpt, aspace, pc))
14481 return 1;
14482 }
14483 return 0;
1aafd4da
UW
14484}
14485
1042e4c0
SS
14486/* Tracepoint-specific operations. */
14487
14488/* Set tracepoint count to NUM. */
14489static void
14490set_tracepoint_count (int num)
14491{
14492 tracepoint_count = num;
4fa62494 14493 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
1042e4c0
SS
14494}
14495
70221824 14496static void
0b39b52e 14497trace_command (const char *arg, int from_tty)
1042e4c0 14498{
ffc2605c
TT
14499 event_location_up location = string_to_event_location (&arg,
14500 current_language);
bac7c5cf
GB
14501 const struct breakpoint_ops *ops = breakpoint_ops_for_event_location
14502 (location.get (), true /* is_tracepoint */);
55aa24fb 14503
558a9d82 14504 create_breakpoint (get_current_arch (),
ffc2605c 14505 location.get (),
f00aae0f 14506 NULL, 0, arg, 1 /* parse arg */,
558a9d82
YQ
14507 0 /* tempflag */,
14508 bp_tracepoint /* type_wanted */,
14509 0 /* Ignore count */,
14510 pending_break_support,
14511 ops,
14512 from_tty,
14513 1 /* enabled */,
14514 0 /* internal */, 0);
1042e4c0
SS
14515}
14516
70221824 14517static void
0b39b52e 14518ftrace_command (const char *arg, int from_tty)
7a697b8d 14519{
ffc2605c
TT
14520 event_location_up location = string_to_event_location (&arg,
14521 current_language);
558a9d82 14522 create_breakpoint (get_current_arch (),
ffc2605c 14523 location.get (),
f00aae0f 14524 NULL, 0, arg, 1 /* parse arg */,
558a9d82
YQ
14525 0 /* tempflag */,
14526 bp_fast_tracepoint /* type_wanted */,
14527 0 /* Ignore count */,
14528 pending_break_support,
14529 &tracepoint_breakpoint_ops,
14530 from_tty,
14531 1 /* enabled */,
14532 0 /* internal */, 0);
0fb4aa4b
PA
14533}
14534
14535/* strace command implementation. Creates a static tracepoint. */
14536
70221824 14537static void
0b39b52e 14538strace_command (const char *arg, int from_tty)
0fb4aa4b 14539{
983af33b 14540 struct breakpoint_ops *ops;
ffc2605c 14541 event_location_up location;
983af33b
SDJ
14542
14543 /* Decide if we are dealing with a static tracepoint marker (`-m'),
14544 or with a normal static tracepoint. */
61012eef 14545 if (arg && startswith (arg, "-m") && isspace (arg[2]))
f00aae0f
KS
14546 {
14547 ops = &strace_marker_breakpoint_ops;
a20714ff 14548 location = new_linespec_location (&arg, symbol_name_match_type::FULL);
f00aae0f 14549 }
983af33b 14550 else
f00aae0f
KS
14551 {
14552 ops = &tracepoint_breakpoint_ops;
14553 location = string_to_event_location (&arg, current_language);
14554 }
983af33b 14555
558a9d82 14556 create_breakpoint (get_current_arch (),
ffc2605c 14557 location.get (),
f00aae0f 14558 NULL, 0, arg, 1 /* parse arg */,
558a9d82
YQ
14559 0 /* tempflag */,
14560 bp_static_tracepoint /* type_wanted */,
14561 0 /* Ignore count */,
14562 pending_break_support,
14563 ops,
14564 from_tty,
14565 1 /* enabled */,
14566 0 /* internal */, 0);
7a697b8d
SS
14567}
14568
409873ef
SS
14569/* Set up a fake reader function that gets command lines from a linked
14570 list that was acquired during tracepoint uploading. */
14571
14572static struct uploaded_tp *this_utp;
3149d8c1 14573static int next_cmd;
409873ef
SS
14574
14575static char *
14576read_uploaded_action (void)
14577{
a18ba4e4 14578 char *rslt = nullptr;
409873ef 14579
a18ba4e4
SM
14580 if (next_cmd < this_utp->cmd_strings.size ())
14581 {
67aa1f3c 14582 rslt = this_utp->cmd_strings[next_cmd].get ();
a18ba4e4
SM
14583 next_cmd++;
14584 }
409873ef
SS
14585
14586 return rslt;
14587}
14588
00bf0b85
SS
14589/* Given information about a tracepoint as recorded on a target (which
14590 can be either a live system or a trace file), attempt to create an
14591 equivalent GDB tracepoint. This is not a reliable process, since
14592 the target does not necessarily have all the information used when
14593 the tracepoint was originally defined. */
14594
d9b3f62e 14595struct tracepoint *
00bf0b85 14596create_tracepoint_from_upload (struct uploaded_tp *utp)
d5551862 14597{
f2fc3015
TT
14598 const char *addr_str;
14599 char small_buf[100];
d9b3f62e 14600 struct tracepoint *tp;
fd9b8c24 14601
409873ef 14602 if (utp->at_string)
67aa1f3c 14603 addr_str = utp->at_string.get ();
409873ef
SS
14604 else
14605 {
14606 /* In the absence of a source location, fall back to raw
14607 address. Since there is no way to confirm that the address
14608 means the same thing as when the trace was started, warn the
14609 user. */
3e43a32a
MS
14610 warning (_("Uploaded tracepoint %d has no "
14611 "source location, using raw address"),
409873ef 14612 utp->number);
8c042590 14613 xsnprintf (small_buf, sizeof (small_buf), "*%s", hex_string (utp->addr));
409873ef
SS
14614 addr_str = small_buf;
14615 }
14616
14617 /* There's not much we can do with a sequence of bytecodes. */
14618 if (utp->cond && !utp->cond_string)
3e43a32a
MS
14619 warning (_("Uploaded tracepoint %d condition "
14620 "has no source form, ignoring it"),
409873ef 14621 utp->number);
d5551862 14622
ffc2605c
TT
14623 event_location_up location = string_to_event_location (&addr_str,
14624 current_language);
8cdf0e15 14625 if (!create_breakpoint (get_current_arch (),
ffc2605c 14626 location.get (),
67aa1f3c 14627 utp->cond_string.get (), -1, addr_str,
e7e0cddf 14628 0 /* parse cond/thread */,
8cdf0e15 14629 0 /* tempflag */,
0fb4aa4b 14630 utp->type /* type_wanted */,
8cdf0e15
VP
14631 0 /* Ignore count */,
14632 pending_break_support,
348d480f 14633 &tracepoint_breakpoint_ops,
8cdf0e15 14634 0 /* from_tty */,
84f4c1fe 14635 utp->enabled /* enabled */,
44f238bb
PA
14636 0 /* internal */,
14637 CREATE_BREAKPOINT_FLAGS_INSERTED))
ffc2605c 14638 return NULL;
fd9b8c24 14639
409873ef 14640 /* Get the tracepoint we just created. */
fd9b8c24
PA
14641 tp = get_tracepoint (tracepoint_count);
14642 gdb_assert (tp != NULL);
d5551862 14643
00bf0b85
SS
14644 if (utp->pass > 0)
14645 {
8c042590 14646 xsnprintf (small_buf, sizeof (small_buf), "%d %d", utp->pass,
c1fc2657 14647 tp->number);
00bf0b85 14648
409873ef 14649 trace_pass_command (small_buf, 0);
00bf0b85
SS
14650 }
14651
409873ef
SS
14652 /* If we have uploaded versions of the original commands, set up a
14653 special-purpose "reader" function and call the usual command line
14654 reader, then pass the result to the breakpoint command-setting
14655 function. */
a18ba4e4 14656 if (!utp->cmd_strings.empty ())
00bf0b85 14657 {
12973681 14658 counted_command_line cmd_list;
00bf0b85 14659
409873ef 14660 this_utp = utp;
3149d8c1 14661 next_cmd = 0;
d5551862 14662
60b3cef2 14663 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL);
409873ef 14664
c1fc2657 14665 breakpoint_set_commands (tp, std::move (cmd_list));
00bf0b85 14666 }
a18ba4e4
SM
14667 else if (!utp->actions.empty ()
14668 || !utp->step_actions.empty ())
3e43a32a
MS
14669 warning (_("Uploaded tracepoint %d actions "
14670 "have no source form, ignoring them"),
409873ef 14671 utp->number);
00bf0b85 14672
f196051f 14673 /* Copy any status information that might be available. */
c1fc2657 14674 tp->hit_count = utp->hit_count;
f196051f
SS
14675 tp->traceframe_usage = utp->traceframe_usage;
14676
00bf0b85 14677 return tp;
d9b3f62e 14678}
00bf0b85 14679
1042e4c0
SS
14680/* Print information on tracepoint number TPNUM_EXP, or all if
14681 omitted. */
14682
14683static void
1d12d88f 14684info_tracepoints_command (const char *args, int from_tty)
1042e4c0 14685{
79a45e25 14686 struct ui_out *uiout = current_uiout;
e5a67952 14687 int num_printed;
1042e4c0 14688
5c458ae8 14689 num_printed = breakpoint_1 (args, false, is_tracepoint);
d77f58be
SS
14690
14691 if (num_printed == 0)
1042e4c0 14692 {
e5a67952 14693 if (args == NULL || *args == '\0')
112e8700 14694 uiout->message ("No tracepoints.\n");
d77f58be 14695 else
112e8700 14696 uiout->message ("No tracepoint matching '%s'.\n", args);
1042e4c0 14697 }
ad443146
SS
14698
14699 default_collect_info ();
1042e4c0
SS
14700}
14701
4a64f543 14702/* The 'enable trace' command enables tracepoints.
1042e4c0
SS
14703 Not supported by all targets. */
14704static void
5fed81ff 14705enable_trace_command (const char *args, int from_tty)
1042e4c0
SS
14706{
14707 enable_command (args, from_tty);
14708}
14709
4a64f543 14710/* The 'disable trace' command disables tracepoints.
1042e4c0
SS
14711 Not supported by all targets. */
14712static void
5fed81ff 14713disable_trace_command (const char *args, int from_tty)
1042e4c0
SS
14714{
14715 disable_command (args, from_tty);
14716}
14717
4a64f543 14718/* Remove a tracepoint (or all if no argument). */
1042e4c0 14719static void
4495129a 14720delete_trace_command (const char *arg, int from_tty)
1042e4c0 14721{
35df4500 14722 struct breakpoint *b, *b_tmp;
1042e4c0
SS
14723
14724 dont_repeat ();
14725
14726 if (arg == 0)
14727 {
14728 int breaks_to_delete = 0;
14729
14730 /* Delete all breakpoints if no argument.
14731 Do not delete internal or call-dummy breakpoints, these
4a64f543
MS
14732 have to be deleted with an explicit breakpoint number
14733 argument. */
1042e4c0 14734 ALL_TRACEPOINTS (b)
46c6471b 14735 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0
SS
14736 {
14737 breaks_to_delete = 1;
14738 break;
14739 }
1042e4c0
SS
14740
14741 /* Ask user only if there are some breakpoints to delete. */
14742 if (!from_tty
14743 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
14744 {
35df4500 14745 ALL_BREAKPOINTS_SAFE (b, b_tmp)
46c6471b 14746 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0 14747 delete_breakpoint (b);
1042e4c0
SS
14748 }
14749 }
14750 else
48649e1b 14751 map_breakpoint_numbers
b926417a 14752 (arg, [&] (breakpoint *br)
48649e1b 14753 {
b926417a 14754 iterate_over_related_breakpoints (br, delete_breakpoint);
48649e1b 14755 });
1042e4c0
SS
14756}
14757
197f0a60
TT
14758/* Helper function for trace_pass_command. */
14759
14760static void
d9b3f62e 14761trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
197f0a60 14762{
d9b3f62e 14763 tp->pass_count = count;
76727919 14764 gdb::observers::breakpoint_modified.notify (tp);
197f0a60
TT
14765 if (from_tty)
14766 printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
c1fc2657 14767 tp->number, count);
197f0a60
TT
14768}
14769
1042e4c0
SS
14770/* Set passcount for tracepoint.
14771
14772 First command argument is passcount, second is tracepoint number.
14773 If tracepoint number omitted, apply to most recently defined.
14774 Also accepts special argument "all". */
14775
14776static void
0b39b52e 14777trace_pass_command (const char *args, int from_tty)
1042e4c0 14778{
d9b3f62e 14779 struct tracepoint *t1;
0b39b52e 14780 ULONGEST count;
1042e4c0
SS
14781
14782 if (args == 0 || *args == 0)
3e43a32a
MS
14783 error (_("passcount command requires an "
14784 "argument (count + optional TP num)"));
1042e4c0 14785
0b39b52e 14786 count = strtoulst (args, &args, 10); /* Count comes first, then TP num. */
1042e4c0 14787
529480d0 14788 args = skip_spaces (args);
1042e4c0
SS
14789 if (*args && strncasecmp (args, "all", 3) == 0)
14790 {
d9b3f62e
PA
14791 struct breakpoint *b;
14792
1042e4c0 14793 args += 3; /* Skip special argument "all". */
1042e4c0
SS
14794 if (*args)
14795 error (_("Junk at end of arguments."));
1042e4c0 14796
d9b3f62e 14797 ALL_TRACEPOINTS (b)
197f0a60 14798 {
d9b3f62e 14799 t1 = (struct tracepoint *) b;
197f0a60
TT
14800 trace_pass_set_count (t1, count, from_tty);
14801 }
14802 }
14803 else if (*args == '\0')
1042e4c0 14804 {
5fa1d40e 14805 t1 = get_tracepoint_by_number (&args, NULL);
1042e4c0 14806 if (t1)
197f0a60
TT
14807 trace_pass_set_count (t1, count, from_tty);
14808 }
14809 else
14810 {
bfd28288
PA
14811 number_or_range_parser parser (args);
14812 while (!parser.finished ())
1042e4c0 14813 {
bfd28288 14814 t1 = get_tracepoint_by_number (&args, &parser);
197f0a60
TT
14815 if (t1)
14816 trace_pass_set_count (t1, count, from_tty);
1042e4c0
SS
14817 }
14818 }
1042e4c0
SS
14819}
14820
d9b3f62e 14821struct tracepoint *
1042e4c0
SS
14822get_tracepoint (int num)
14823{
14824 struct breakpoint *t;
14825
14826 ALL_TRACEPOINTS (t)
14827 if (t->number == num)
d9b3f62e 14828 return (struct tracepoint *) t;
1042e4c0
SS
14829
14830 return NULL;
14831}
14832
d5551862
SS
14833/* Find the tracepoint with the given target-side number (which may be
14834 different from the tracepoint number after disconnecting and
14835 reconnecting). */
14836
d9b3f62e 14837struct tracepoint *
d5551862
SS
14838get_tracepoint_by_number_on_target (int num)
14839{
d9b3f62e 14840 struct breakpoint *b;
d5551862 14841
d9b3f62e
PA
14842 ALL_TRACEPOINTS (b)
14843 {
14844 struct tracepoint *t = (struct tracepoint *) b;
14845
14846 if (t->number_on_target == num)
14847 return t;
14848 }
d5551862
SS
14849
14850 return NULL;
14851}
14852
1042e4c0 14853/* Utility: parse a tracepoint number and look it up in the list.
197f0a60 14854 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
5fa1d40e
YQ
14855 If the argument is missing, the most recent tracepoint
14856 (tracepoint_count) is returned. */
14857
d9b3f62e 14858struct tracepoint *
0b39b52e 14859get_tracepoint_by_number (const char **arg,
bfd28288 14860 number_or_range_parser *parser)
1042e4c0 14861{
1042e4c0
SS
14862 struct breakpoint *t;
14863 int tpnum;
0b39b52e 14864 const char *instring = arg == NULL ? NULL : *arg;
1042e4c0 14865
bfd28288 14866 if (parser != NULL)
197f0a60 14867 {
bfd28288
PA
14868 gdb_assert (!parser->finished ());
14869 tpnum = parser->get_number ();
197f0a60
TT
14870 }
14871 else if (arg == NULL || *arg == NULL || ! **arg)
5fa1d40e 14872 tpnum = tracepoint_count;
1042e4c0 14873 else
197f0a60 14874 tpnum = get_number (arg);
1042e4c0
SS
14875
14876 if (tpnum <= 0)
14877 {
14878 if (instring && *instring)
14879 printf_filtered (_("bad tracepoint number at or near '%s'\n"),
14880 instring);
14881 else
5fa1d40e 14882 printf_filtered (_("No previous tracepoint\n"));
1042e4c0
SS
14883 return NULL;
14884 }
14885
14886 ALL_TRACEPOINTS (t)
14887 if (t->number == tpnum)
14888 {
d9b3f62e 14889 return (struct tracepoint *) t;
1042e4c0
SS
14890 }
14891
1042e4c0
SS
14892 printf_unfiltered ("No tracepoint number %d.\n", tpnum);
14893 return NULL;
14894}
14895
d9b3f62e
PA
14896void
14897print_recreate_thread (struct breakpoint *b, struct ui_file *fp)
14898{
14899 if (b->thread != -1)
14900 fprintf_unfiltered (fp, " thread %d", b->thread);
14901
14902 if (b->task != 0)
14903 fprintf_unfiltered (fp, " task %d", b->task);
14904
14905 fprintf_unfiltered (fp, "\n");
14906}
14907
6149aea9
PA
14908/* Save information on user settable breakpoints (watchpoints, etc) to
14909 a new script file named FILENAME. If FILTER is non-NULL, call it
14910 on each breakpoint and only include the ones for which it returns
f2478a7e 14911 true. */
6149aea9 14912
1042e4c0 14913static void
4495129a 14914save_breakpoints (const char *filename, int from_tty,
f2478a7e 14915 bool (*filter) (const struct breakpoint *))
1042e4c0
SS
14916{
14917 struct breakpoint *tp;
6149aea9 14918 int any = 0;
6149aea9 14919 int extra_trace_bits = 0;
1042e4c0 14920
6149aea9
PA
14921 if (filename == 0 || *filename == 0)
14922 error (_("Argument required (file name in which to save)"));
1042e4c0
SS
14923
14924 /* See if we have anything to save. */
6149aea9 14925 ALL_BREAKPOINTS (tp)
1042e4c0 14926 {
6149aea9 14927 /* Skip internal and momentary breakpoints. */
09d682a4 14928 if (!user_breakpoint_p (tp))
6149aea9
PA
14929 continue;
14930
14931 /* If we have a filter, only save the breakpoints it accepts. */
14932 if (filter && !filter (tp))
14933 continue;
14934
14935 any = 1;
14936
14937 if (is_tracepoint (tp))
14938 {
14939 extra_trace_bits = 1;
14940
14941 /* We can stop searching. */
14942 break;
14943 }
1042e4c0 14944 }
6149aea9
PA
14945
14946 if (!any)
1042e4c0 14947 {
6149aea9 14948 warning (_("Nothing to save."));
1042e4c0
SS
14949 return;
14950 }
14951
ee0c3293 14952 gdb::unique_xmalloc_ptr<char> expanded_filename (tilde_expand (filename));
d7e74731
PA
14953
14954 stdio_file fp;
14955
ee0c3293 14956 if (!fp.open (expanded_filename.get (), "w"))
6149aea9 14957 error (_("Unable to open file '%s' for saving (%s)"),
ee0c3293 14958 expanded_filename.get (), safe_strerror (errno));
8bf6485c 14959
6149aea9 14960 if (extra_trace_bits)
d7e74731 14961 save_trace_state_variables (&fp);
8bf6485c 14962
6149aea9 14963 ALL_BREAKPOINTS (tp)
1042e4c0 14964 {
6149aea9 14965 /* Skip internal and momentary breakpoints. */
09d682a4 14966 if (!user_breakpoint_p (tp))
6149aea9 14967 continue;
8bf6485c 14968
6149aea9
PA
14969 /* If we have a filter, only save the breakpoints it accepts. */
14970 if (filter && !filter (tp))
14971 continue;
14972
d7e74731 14973 tp->ops->print_recreate (tp, &fp);
1042e4c0 14974
6149aea9
PA
14975 /* Note, we can't rely on tp->number for anything, as we can't
14976 assume the recreated breakpoint numbers will match. Use $bpnum
14977 instead. */
14978
14979 if (tp->cond_string)
d7e74731 14980 fp.printf (" condition $bpnum %s\n", tp->cond_string);
6149aea9
PA
14981
14982 if (tp->ignore_count)
d7e74731 14983 fp.printf (" ignore $bpnum %d\n", tp->ignore_count);
6149aea9 14984
2d9442cc 14985 if (tp->type != bp_dprintf && tp->commands)
1042e4c0 14986 {
d7e74731 14987 fp.puts (" commands\n");
a7bdde9e 14988
d7e74731 14989 current_uiout->redirect (&fp);
a70b8144 14990 try
1042e4c0 14991 {
d1b0a7bf 14992 print_command_lines (current_uiout, tp->commands.get (), 2);
a7bdde9e 14993 }
230d2906 14994 catch (const gdb_exception &ex)
492d29ea 14995 {
112e8700 14996 current_uiout->redirect (NULL);
eedc3f4f 14997 throw;
492d29ea 14998 }
1042e4c0 14999
112e8700 15000 current_uiout->redirect (NULL);
d7e74731 15001 fp.puts (" end\n");
1042e4c0 15002 }
6149aea9
PA
15003
15004 if (tp->enable_state == bp_disabled)
d7e74731 15005 fp.puts ("disable $bpnum\n");
6149aea9
PA
15006
15007 /* If this is a multi-location breakpoint, check if the locations
15008 should be individually disabled. Watchpoint locations are
15009 special, and not user visible. */
15010 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
15011 {
15012 struct bp_location *loc;
15013 int n = 1;
15014
15015 for (loc = tp->loc; loc != NULL; loc = loc->next, n++)
15016 if (!loc->enabled)
d7e74731 15017 fp.printf ("disable $bpnum.%d\n", n);
6149aea9 15018 }
1042e4c0 15019 }
8bf6485c 15020
6149aea9 15021 if (extra_trace_bits && *default_collect)
d7e74731 15022 fp.printf ("set default-collect %s\n", default_collect);
8bf6485c 15023
1042e4c0 15024 if (from_tty)
ee0c3293 15025 printf_filtered (_("Saved to file '%s'.\n"), expanded_filename.get ());
6149aea9
PA
15026}
15027
15028/* The `save breakpoints' command. */
15029
15030static void
4495129a 15031save_breakpoints_command (const char *args, int from_tty)
6149aea9
PA
15032{
15033 save_breakpoints (args, from_tty, NULL);
15034}
15035
15036/* The `save tracepoints' command. */
15037
15038static void
4495129a 15039save_tracepoints_command (const char *args, int from_tty)
6149aea9
PA
15040{
15041 save_breakpoints (args, from_tty, is_tracepoint);
1042e4c0
SS
15042}
15043
15044/* Create a vector of all tracepoints. */
15045
f51e0e20 15046std::vector<breakpoint *>
eeae04df 15047all_tracepoints (void)
1042e4c0 15048{
f51e0e20 15049 std::vector<breakpoint *> tp_vec;
1042e4c0
SS
15050 struct breakpoint *tp;
15051
15052 ALL_TRACEPOINTS (tp)
15053 {
f51e0e20 15054 tp_vec.push_back (tp);
1042e4c0
SS
15055 }
15056
15057 return tp_vec;
15058}
15059
c906108c 15060\f
629500fa
KS
15061/* This help string is used to consolidate all the help string for specifying
15062 locations used by several commands. */
15063
15064#define LOCATION_HELP_STRING \
15065"Linespecs are colon-separated lists of location parameters, such as\n\
15066source filename, function name, label name, and line number.\n\
15067Example: To specify the start of a label named \"the_top\" in the\n\
15068function \"fact\" in the file \"factorial.c\", use\n\
15069\"factorial.c:fact:the_top\".\n\
15070\n\
15071Address locations begin with \"*\" and specify an exact address in the\n\
15072program. Example: To specify the fourth byte past the start function\n\
15073\"main\", use \"*main + 4\".\n\
15074\n\
15075Explicit locations are similar to linespecs but use an option/argument\n\
15076syntax to specify location parameters.\n\
15077Example: To specify the start of the label named \"the_top\" in the\n\
15078function \"fact\" in the file \"factorial.c\", use \"-source factorial.c\n\
a20714ff
PA
15079-function fact -label the_top\".\n\
15080\n\
15081By default, a specified function is matched against the program's\n\
15082functions in all scopes. For C++, this means in all namespaces and\n\
15083classes. For Ada, this means in all packages. E.g., in C++,\n\
15084\"func()\" matches \"A::func()\", \"A::B::func()\", etc. The\n\
15085\"-qualified\" flag overrides this behavior, making GDB interpret the\n\
89549d7f 15086specified name as a complete fully-qualified name instead."
629500fa 15087
4a64f543
MS
15088/* This help string is used for the break, hbreak, tbreak and thbreak
15089 commands. It is defined as a macro to prevent duplication.
15090 COMMAND should be a string constant containing the name of the
15091 command. */
629500fa 15092
31e2b00f 15093#define BREAK_ARGS_HELP(command) \
fb7b5af4
SDJ
15094command" [PROBE_MODIFIER] [LOCATION] [thread THREADNUM] [if CONDITION]\n\
15095PROBE_MODIFIER shall be present if the command is to be placed in a\n\
15096probe point. Accepted values are `-probe' (for a generic, automatically\n\
d4777acb
JM
15097guessed probe type), `-probe-stap' (for a SystemTap probe) or \n\
15098`-probe-dtrace' (for a DTrace probe).\n\
629500fa
KS
15099LOCATION may be a linespec, address, or explicit location as described\n\
15100below.\n\
15101\n\
dc10affe
PA
15102With no LOCATION, uses current execution address of the selected\n\
15103stack frame. This is useful for breaking on return to a stack frame.\n\
31e2b00f
AS
15104\n\
15105THREADNUM is the number from \"info threads\".\n\
15106CONDITION is a boolean expression.\n\
89549d7f 15107\n" LOCATION_HELP_STRING "\n\n\
d41c0fc8
PA
15108Multiple breakpoints at one place are permitted, and useful if their\n\
15109conditions are different.\n\
31e2b00f
AS
15110\n\
15111Do \"help breakpoints\" for info on other commands dealing with breakpoints."
15112
44feb3ce
TT
15113/* List of subcommands for "catch". */
15114static struct cmd_list_element *catch_cmdlist;
15115
15116/* List of subcommands for "tcatch". */
15117static struct cmd_list_element *tcatch_cmdlist;
15118
9ac4176b 15119void
a121b7c1 15120add_catch_command (const char *name, const char *docstring,
eb4c3f4a 15121 cmd_const_sfunc_ftype *sfunc,
625e8578 15122 completer_ftype *completer,
44feb3ce
TT
15123 void *user_data_catch,
15124 void *user_data_tcatch)
15125{
15126 struct cmd_list_element *command;
15127
0450cc4c 15128 command = add_cmd (name, class_breakpoint, docstring,
44feb3ce
TT
15129 &catch_cmdlist);
15130 set_cmd_sfunc (command, sfunc);
15131 set_cmd_context (command, user_data_catch);
a96d9b2e 15132 set_cmd_completer (command, completer);
44feb3ce 15133
0450cc4c 15134 command = add_cmd (name, class_breakpoint, docstring,
44feb3ce
TT
15135 &tcatch_cmdlist);
15136 set_cmd_sfunc (command, sfunc);
15137 set_cmd_context (command, user_data_tcatch);
a96d9b2e 15138 set_cmd_completer (command, completer);
44feb3ce
TT
15139}
15140
6149aea9 15141static void
981a3fb3 15142save_command (const char *arg, int from_tty)
6149aea9 15143{
3e43a32a
MS
15144 printf_unfiltered (_("\"save\" must be followed by "
15145 "the name of a save subcommand.\n"));
635c7e8a 15146 help_list (save_cmdlist, "save ", all_commands, gdb_stdout);
6149aea9
PA
15147}
15148
84f4c1fe 15149struct breakpoint *
95da600f 15150iterate_over_breakpoints (gdb::function_view<bool (breakpoint *)> callback)
84f4c1fe 15151{
35df4500 15152 struct breakpoint *b, *b_tmp;
84f4c1fe 15153
35df4500 15154 ALL_BREAKPOINTS_SAFE (b, b_tmp)
84f4c1fe 15155 {
95da600f 15156 if (callback (b))
84f4c1fe
PM
15157 return b;
15158 }
15159
15160 return NULL;
15161}
15162
0574c78f
GB
15163/* Zero if any of the breakpoint's locations could be a location where
15164 functions have been inlined, nonzero otherwise. */
15165
15166static int
15167is_non_inline_function (struct breakpoint *b)
15168{
15169 /* The shared library event breakpoint is set on the address of a
15170 non-inline function. */
15171 if (b->type == bp_shlib_event)
15172 return 1;
15173
15174 return 0;
15175}
15176
15177/* Nonzero if the specified PC cannot be a location where functions
15178 have been inlined. */
15179
15180int
accd0bcd 15181pc_at_non_inline_function (const address_space *aspace, CORE_ADDR pc,
09ac7c10 15182 const struct target_waitstatus *ws)
0574c78f
GB
15183{
15184 struct breakpoint *b;
15185 struct bp_location *bl;
15186
15187 ALL_BREAKPOINTS (b)
15188 {
15189 if (!is_non_inline_function (b))
15190 continue;
15191
15192 for (bl = b->loc; bl != NULL; bl = bl->next)
15193 {
15194 if (!bl->shlib_disabled
09ac7c10 15195 && bpstat_check_location (bl, aspace, pc, ws))
0574c78f
GB
15196 return 1;
15197 }
15198 }
15199
15200 return 0;
15201}
15202
2f202fde
JK
15203/* Remove any references to OBJFILE which is going to be freed. */
15204
15205void
15206breakpoint_free_objfile (struct objfile *objfile)
15207{
15208 struct bp_location **locp, *loc;
15209
15210 ALL_BP_LOCATIONS (loc, locp)
eb822aa6 15211 if (loc->symtab != NULL && SYMTAB_OBJFILE (loc->symtab) == objfile)
2f202fde
JK
15212 loc->symtab = NULL;
15213}
15214
2060206e
PA
15215void
15216initialize_breakpoint_ops (void)
15217{
15218 static int initialized = 0;
15219
15220 struct breakpoint_ops *ops;
15221
15222 if (initialized)
15223 return;
15224 initialized = 1;
15225
15226 /* The breakpoint_ops structure to be inherit by all kinds of
15227 breakpoints (real breakpoints, i.e., user "break" breakpoints,
15228 internal and momentary breakpoints, etc.). */
15229 ops = &bkpt_base_breakpoint_ops;
15230 *ops = base_breakpoint_ops;
15231 ops->re_set = bkpt_re_set;
15232 ops->insert_location = bkpt_insert_location;
15233 ops->remove_location = bkpt_remove_location;
15234 ops->breakpoint_hit = bkpt_breakpoint_hit;
5f700d83 15235 ops->create_sals_from_location = bkpt_create_sals_from_location;
983af33b 15236 ops->create_breakpoints_sal = bkpt_create_breakpoints_sal;
5f700d83 15237 ops->decode_location = bkpt_decode_location;
2060206e
PA
15238
15239 /* The breakpoint_ops structure to be used in regular breakpoints. */
15240 ops = &bkpt_breakpoint_ops;
15241 *ops = bkpt_base_breakpoint_ops;
15242 ops->re_set = bkpt_re_set;
15243 ops->resources_needed = bkpt_resources_needed;
15244 ops->print_it = bkpt_print_it;
15245 ops->print_mention = bkpt_print_mention;
15246 ops->print_recreate = bkpt_print_recreate;
15247
15248 /* Ranged breakpoints. */
15249 ops = &ranged_breakpoint_ops;
15250 *ops = bkpt_breakpoint_ops;
15251 ops->breakpoint_hit = breakpoint_hit_ranged_breakpoint;
15252 ops->resources_needed = resources_needed_ranged_breakpoint;
15253 ops->print_it = print_it_ranged_breakpoint;
15254 ops->print_one = print_one_ranged_breakpoint;
15255 ops->print_one_detail = print_one_detail_ranged_breakpoint;
15256 ops->print_mention = print_mention_ranged_breakpoint;
15257 ops->print_recreate = print_recreate_ranged_breakpoint;
15258
15259 /* Internal breakpoints. */
15260 ops = &internal_breakpoint_ops;
15261 *ops = bkpt_base_breakpoint_ops;
15262 ops->re_set = internal_bkpt_re_set;
15263 ops->check_status = internal_bkpt_check_status;
15264 ops->print_it = internal_bkpt_print_it;
15265 ops->print_mention = internal_bkpt_print_mention;
15266
15267 /* Momentary breakpoints. */
15268 ops = &momentary_breakpoint_ops;
15269 *ops = bkpt_base_breakpoint_ops;
15270 ops->re_set = momentary_bkpt_re_set;
15271 ops->check_status = momentary_bkpt_check_status;
15272 ops->print_it = momentary_bkpt_print_it;
15273 ops->print_mention = momentary_bkpt_print_mention;
15274
55aa24fb
SDJ
15275 /* Probe breakpoints. */
15276 ops = &bkpt_probe_breakpoint_ops;
15277 *ops = bkpt_breakpoint_ops;
15278 ops->insert_location = bkpt_probe_insert_location;
15279 ops->remove_location = bkpt_probe_remove_location;
5f700d83
KS
15280 ops->create_sals_from_location = bkpt_probe_create_sals_from_location;
15281 ops->decode_location = bkpt_probe_decode_location;
55aa24fb 15282
2060206e
PA
15283 /* Watchpoints. */
15284 ops = &watchpoint_breakpoint_ops;
15285 *ops = base_breakpoint_ops;
15286 ops->re_set = re_set_watchpoint;
15287 ops->insert_location = insert_watchpoint;
15288 ops->remove_location = remove_watchpoint;
15289 ops->breakpoint_hit = breakpoint_hit_watchpoint;
15290 ops->check_status = check_status_watchpoint;
15291 ops->resources_needed = resources_needed_watchpoint;
15292 ops->works_in_software_mode = works_in_software_mode_watchpoint;
15293 ops->print_it = print_it_watchpoint;
15294 ops->print_mention = print_mention_watchpoint;
15295 ops->print_recreate = print_recreate_watchpoint;
427cd150 15296 ops->explains_signal = explains_signal_watchpoint;
2060206e
PA
15297
15298 /* Masked watchpoints. */
15299 ops = &masked_watchpoint_breakpoint_ops;
15300 *ops = watchpoint_breakpoint_ops;
15301 ops->insert_location = insert_masked_watchpoint;
15302 ops->remove_location = remove_masked_watchpoint;
15303 ops->resources_needed = resources_needed_masked_watchpoint;
15304 ops->works_in_software_mode = works_in_software_mode_masked_watchpoint;
15305 ops->print_it = print_it_masked_watchpoint;
15306 ops->print_one_detail = print_one_detail_masked_watchpoint;
15307 ops->print_mention = print_mention_masked_watchpoint;
15308 ops->print_recreate = print_recreate_masked_watchpoint;
15309
15310 /* Tracepoints. */
15311 ops = &tracepoint_breakpoint_ops;
15312 *ops = base_breakpoint_ops;
15313 ops->re_set = tracepoint_re_set;
15314 ops->breakpoint_hit = tracepoint_breakpoint_hit;
15315 ops->print_one_detail = tracepoint_print_one_detail;
15316 ops->print_mention = tracepoint_print_mention;
15317 ops->print_recreate = tracepoint_print_recreate;
5f700d83 15318 ops->create_sals_from_location = tracepoint_create_sals_from_location;
983af33b 15319 ops->create_breakpoints_sal = tracepoint_create_breakpoints_sal;
5f700d83 15320 ops->decode_location = tracepoint_decode_location;
983af33b 15321
55aa24fb
SDJ
15322 /* Probe tracepoints. */
15323 ops = &tracepoint_probe_breakpoint_ops;
15324 *ops = tracepoint_breakpoint_ops;
5f700d83
KS
15325 ops->create_sals_from_location = tracepoint_probe_create_sals_from_location;
15326 ops->decode_location = tracepoint_probe_decode_location;
55aa24fb 15327
983af33b
SDJ
15328 /* Static tracepoints with marker (`-m'). */
15329 ops = &strace_marker_breakpoint_ops;
15330 *ops = tracepoint_breakpoint_ops;
5f700d83 15331 ops->create_sals_from_location = strace_marker_create_sals_from_location;
983af33b 15332 ops->create_breakpoints_sal = strace_marker_create_breakpoints_sal;
5f700d83 15333 ops->decode_location = strace_marker_decode_location;
2060206e
PA
15334
15335 /* Fork catchpoints. */
15336 ops = &catch_fork_breakpoint_ops;
15337 *ops = base_breakpoint_ops;
15338 ops->insert_location = insert_catch_fork;
15339 ops->remove_location = remove_catch_fork;
15340 ops->breakpoint_hit = breakpoint_hit_catch_fork;
15341 ops->print_it = print_it_catch_fork;
15342 ops->print_one = print_one_catch_fork;
15343 ops->print_mention = print_mention_catch_fork;
15344 ops->print_recreate = print_recreate_catch_fork;
15345
15346 /* Vfork catchpoints. */
15347 ops = &catch_vfork_breakpoint_ops;
15348 *ops = base_breakpoint_ops;
15349 ops->insert_location = insert_catch_vfork;
15350 ops->remove_location = remove_catch_vfork;
15351 ops->breakpoint_hit = breakpoint_hit_catch_vfork;
15352 ops->print_it = print_it_catch_vfork;
15353 ops->print_one = print_one_catch_vfork;
15354 ops->print_mention = print_mention_catch_vfork;
15355 ops->print_recreate = print_recreate_catch_vfork;
15356
15357 /* Exec catchpoints. */
15358 ops = &catch_exec_breakpoint_ops;
15359 *ops = base_breakpoint_ops;
2060206e
PA
15360 ops->insert_location = insert_catch_exec;
15361 ops->remove_location = remove_catch_exec;
15362 ops->breakpoint_hit = breakpoint_hit_catch_exec;
15363 ops->print_it = print_it_catch_exec;
15364 ops->print_one = print_one_catch_exec;
15365 ops->print_mention = print_mention_catch_exec;
15366 ops->print_recreate = print_recreate_catch_exec;
15367
edcc5120
TT
15368 /* Solib-related catchpoints. */
15369 ops = &catch_solib_breakpoint_ops;
15370 *ops = base_breakpoint_ops;
edcc5120
TT
15371 ops->insert_location = insert_catch_solib;
15372 ops->remove_location = remove_catch_solib;
15373 ops->breakpoint_hit = breakpoint_hit_catch_solib;
15374 ops->check_status = check_status_catch_solib;
15375 ops->print_it = print_it_catch_solib;
15376 ops->print_one = print_one_catch_solib;
15377 ops->print_mention = print_mention_catch_solib;
15378 ops->print_recreate = print_recreate_catch_solib;
e7e0cddf
SS
15379
15380 ops = &dprintf_breakpoint_ops;
15381 *ops = bkpt_base_breakpoint_ops;
5c2b4418 15382 ops->re_set = dprintf_re_set;
e7e0cddf
SS
15383 ops->resources_needed = bkpt_resources_needed;
15384 ops->print_it = bkpt_print_it;
15385 ops->print_mention = bkpt_print_mention;
2d9442cc 15386 ops->print_recreate = dprintf_print_recreate;
9d6e6e84 15387 ops->after_condition_true = dprintf_after_condition_true;
cd1608cc 15388 ops->breakpoint_hit = dprintf_breakpoint_hit;
2060206e
PA
15389}
15390
8bfd80db
YQ
15391/* Chain containing all defined "enable breakpoint" subcommands. */
15392
15393static struct cmd_list_element *enablebreaklist = NULL;
15394
8588b356
SM
15395/* See breakpoint.h. */
15396
15397cmd_list_element *commands_cmd_element = nullptr;
15398
c906108c 15399void
fba45db2 15400_initialize_breakpoint (void)
c906108c
SS
15401{
15402 struct cmd_list_element *c;
15403
2060206e
PA
15404 initialize_breakpoint_ops ();
15405
76727919
TT
15406 gdb::observers::solib_unloaded.attach (disable_breakpoints_in_unloaded_shlib);
15407 gdb::observers::free_objfile.attach (disable_breakpoints_in_freed_objfile);
15408 gdb::observers::memory_changed.attach (invalidate_bp_value_on_memory_change);
84acb35a 15409
c906108c
SS
15410 breakpoint_chain = 0;
15411 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
15412 before a breakpoint is set. */
15413 breakpoint_count = 0;
15414
1042e4c0
SS
15415 tracepoint_count = 0;
15416
1bedd215
AC
15417 add_com ("ignore", class_breakpoint, ignore_command, _("\
15418Set ignore-count of breakpoint number N to COUNT.\n\
15419Usage is `ignore N COUNT'."));
c906108c 15420
8588b356
SM
15421 commands_cmd_element = add_com ("commands", class_breakpoint,
15422 commands_command, _("\
18da0c51
MG
15423Set commands to be executed when the given breakpoints are hit.\n\
15424Give a space-separated breakpoint list as argument after \"commands\".\n\
15425A list element can be a breakpoint number (e.g. `5') or a range of numbers\n\
15426(e.g. `5-7').\n\
c906108c
SS
15427With no argument, the targeted breakpoint is the last one set.\n\
15428The commands themselves follow starting on the next line.\n\
15429Type a line containing \"end\" to indicate the end of them.\n\
15430Give \"silent\" as the first line to make the breakpoint silent;\n\
1bedd215 15431then no output is printed when it is hit, except what the commands print."));
c906108c 15432
d55637df 15433 c = add_com ("condition", class_breakpoint, condition_command, _("\
1bedd215 15434Specify breakpoint number N to break only if COND is true.\n\
c906108c 15435Usage is `condition N COND', where N is an integer and COND is an\n\
1bedd215 15436expression to be evaluated whenever breakpoint N is reached."));
d55637df 15437 set_cmd_completer (c, condition_completer);
c906108c 15438
1bedd215 15439 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
31e2b00f 15440Set a temporary breakpoint.\n\
c906108c
SS
15441Like \"break\" except the breakpoint is only temporary,\n\
15442so it will be deleted when hit. Equivalent to \"break\" followed\n\
31e2b00f
AS
15443by using \"enable delete\" on the breakpoint number.\n\
15444\n"
15445BREAK_ARGS_HELP ("tbreak")));
5ba2abeb 15446 set_cmd_completer (c, location_completer);
c94fdfd0 15447
1bedd215 15448 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
a3be7890 15449Set a hardware assisted breakpoint.\n\
c906108c 15450Like \"break\" except the breakpoint requires hardware support,\n\
31e2b00f
AS
15451some target hardware may not have this support.\n\
15452\n"
15453BREAK_ARGS_HELP ("hbreak")));
5ba2abeb 15454 set_cmd_completer (c, location_completer);
c906108c 15455
1bedd215 15456 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
31e2b00f 15457Set a temporary hardware assisted breakpoint.\n\
c906108c 15458Like \"hbreak\" except the breakpoint is only temporary,\n\
31e2b00f
AS
15459so it will be deleted when hit.\n\
15460\n"
15461BREAK_ARGS_HELP ("thbreak")));
5ba2abeb 15462 set_cmd_completer (c, location_completer);
c906108c 15463
1bedd215 15464 add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
590042fc
PW
15465Enable all or some breakpoints.\n\
15466Usage: enable [BREAKPOINTNUM]...\n\
c906108c
SS
15467Give breakpoint numbers (separated by spaces) as arguments.\n\
15468With no subcommand, breakpoints are enabled until you command otherwise.\n\
15469This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 15470With a subcommand you can enable temporarily."),
c906108c 15471 &enablelist, "enable ", 1, &cmdlist);
c906108c
SS
15472
15473 add_com_alias ("en", "enable", class_breakpoint, 1);
15474
84951ab5 15475 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
590042fc
PW
15476Enable all or some breakpoints.\n\
15477Usage: enable breakpoints [BREAKPOINTNUM]...\n\
c906108c
SS
15478Give breakpoint numbers (separated by spaces) as arguments.\n\
15479This is used to cancel the effect of the \"disable\" command.\n\
89549d7f 15480May be abbreviated to simply \"enable\"."),
c5aa993b 15481 &enablebreaklist, "enable breakpoints ", 1, &enablelist);
c906108c 15482
1a966eab 15483 add_cmd ("once", no_class, enable_once_command, _("\
590042fc
PW
15484Enable some breakpoints for one hit.\n\
15485Usage: enable breakpoints once BREAKPOINTNUM...\n\
1a966eab 15486If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
c906108c
SS
15487 &enablebreaklist);
15488
1a966eab 15489 add_cmd ("delete", no_class, enable_delete_command, _("\
590042fc
PW
15490Enable some breakpoints and delete when hit.\n\
15491Usage: enable breakpoints delete BREAKPOINTNUM...\n\
1a966eab 15492If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
15493 &enablebreaklist);
15494
816338b5 15495 add_cmd ("count", no_class, enable_count_command, _("\
590042fc
PW
15496Enable some breakpoints for COUNT hits.\n\
15497Usage: enable breakpoints count COUNT BREAKPOINTNUM...\n\
816338b5
SS
15498If a breakpoint is hit while enabled in this fashion,\n\
15499the count is decremented; when it reaches zero, the breakpoint is disabled."),
15500 &enablebreaklist);
15501
1a966eab 15502 add_cmd ("delete", no_class, enable_delete_command, _("\
590042fc
PW
15503Enable some breakpoints and delete when hit.\n\
15504Usage: enable delete BREAKPOINTNUM...\n\
1a966eab 15505If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
15506 &enablelist);
15507
1a966eab 15508 add_cmd ("once", no_class, enable_once_command, _("\
590042fc
PW
15509Enable some breakpoints for one hit.\n\
15510Usage: enable once BREAKPOINTNUM...\n\
1a966eab 15511If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
816338b5
SS
15512 &enablelist);
15513
15514 add_cmd ("count", no_class, enable_count_command, _("\
590042fc
PW
15515Enable some breakpoints for COUNT hits.\n\
15516Usage: enable count COUNT BREAKPOINTNUM...\n\
816338b5
SS
15517If a breakpoint is hit while enabled in this fashion,\n\
15518the count is decremented; when it reaches zero, the breakpoint is disabled."),
c906108c
SS
15519 &enablelist);
15520
1bedd215 15521 add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
590042fc
PW
15522Disable all or some breakpoints.\n\
15523Usage: disable [BREAKPOINTNUM]...\n\
c906108c
SS
15524Arguments are breakpoint numbers with spaces in between.\n\
15525To disable all breakpoints, give no argument.\n\
64b9b334 15526A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
c906108c
SS
15527 &disablelist, "disable ", 1, &cmdlist);
15528 add_com_alias ("dis", "disable", class_breakpoint, 1);
15529 add_com_alias ("disa", "disable", class_breakpoint, 1);
c906108c 15530
1a966eab 15531 add_cmd ("breakpoints", class_alias, disable_command, _("\
590042fc
PW
15532Disable all or some breakpoints.\n\
15533Usage: disable breakpoints [BREAKPOINTNUM]...\n\
c906108c
SS
15534Arguments are breakpoint numbers with spaces in between.\n\
15535To disable all breakpoints, give no argument.\n\
64b9b334 15536A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
1a966eab 15537This command may be abbreviated \"disable\"."),
c906108c
SS
15538 &disablelist);
15539
1bedd215 15540 add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
590042fc
PW
15541Delete all or some breakpoints.\n\
15542Usage: delete [BREAKPOINTNUM]...\n\
c906108c
SS
15543Arguments are breakpoint numbers with spaces in between.\n\
15544To delete all breakpoints, give no argument.\n\
15545\n\
590042fc 15546Also a prefix command for deletion of other GDB objects."),
c906108c
SS
15547 &deletelist, "delete ", 1, &cmdlist);
15548 add_com_alias ("d", "delete", class_breakpoint, 1);
7f198e01 15549 add_com_alias ("del", "delete", class_breakpoint, 1);
c906108c 15550
1a966eab 15551 add_cmd ("breakpoints", class_alias, delete_command, _("\
590042fc
PW
15552Delete all or some breakpoints or auto-display expressions.\n\
15553Usage: delete breakpoints [BREAKPOINTNUM]...\n\
c906108c
SS
15554Arguments are breakpoint numbers with spaces in between.\n\
15555To delete all breakpoints, give no argument.\n\
1a966eab 15556This command may be abbreviated \"delete\"."),
c906108c
SS
15557 &deletelist);
15558
1bedd215 15559 add_com ("clear", class_breakpoint, clear_command, _("\
629500fa
KS
15560Clear breakpoint at specified location.\n\
15561Argument may be a linespec, explicit, or address location as described below.\n\
1bedd215
AC
15562\n\
15563With no argument, clears all breakpoints in the line that the selected frame\n\
629500fa 15564is executing in.\n"
89549d7f 15565"\n" LOCATION_HELP_STRING "\n\n\
1bedd215 15566See also the \"delete\" command which clears breakpoints by number."));
a6cc4789 15567 add_com_alias ("cl", "clear", class_breakpoint, 1);
c906108c 15568
1bedd215 15569 c = add_com ("break", class_breakpoint, break_command, _("\
629500fa 15570Set breakpoint at specified location.\n"
31e2b00f 15571BREAK_ARGS_HELP ("break")));
5ba2abeb 15572 set_cmd_completer (c, location_completer);
c94fdfd0 15573
c906108c
SS
15574 add_com_alias ("b", "break", class_run, 1);
15575 add_com_alias ("br", "break", class_run, 1);
15576 add_com_alias ("bre", "break", class_run, 1);
15577 add_com_alias ("brea", "break", class_run, 1);
15578
c906108c
SS
15579 if (dbx_commands)
15580 {
1bedd215
AC
15581 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
15582Break in function/address or break at a line in the current file."),
c5aa993b
JM
15583 &stoplist, "stop ", 1, &cmdlist);
15584 add_cmd ("in", class_breakpoint, stopin_command,
1a966eab 15585 _("Break in function or address."), &stoplist);
c5aa993b 15586 add_cmd ("at", class_breakpoint, stopat_command,
1a966eab 15587 _("Break at a line in the current file."), &stoplist);
11db9430 15588 add_com ("status", class_info, info_breakpoints_command, _("\
1bedd215 15589Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
15590The \"Type\" column indicates one of:\n\
15591\tbreakpoint - normal breakpoint\n\
15592\twatchpoint - watchpoint\n\
15593The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15594the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15595breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
15596address and file/line number respectively.\n\
15597\n\
15598Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
15599are set to the address of the last breakpoint listed unless the command\n\
15600is prefixed with \"server \".\n\n\
c906108c 15601Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 15602breakpoint set."));
c906108c
SS
15603 }
15604
11db9430 15605 add_info ("breakpoints", info_breakpoints_command, _("\
e5a67952 15606Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
c906108c
SS
15607The \"Type\" column indicates one of:\n\
15608\tbreakpoint - normal breakpoint\n\
15609\twatchpoint - watchpoint\n\
15610The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15611the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15612breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
15613address and file/line number respectively.\n\
15614\n\
15615Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
15616are set to the address of the last breakpoint listed unless the command\n\
15617is prefixed with \"server \".\n\n\
c906108c 15618Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 15619breakpoint set."));
c906108c 15620
6b04bdb7
MS
15621 add_info_alias ("b", "breakpoints", 1);
15622
1a966eab
AC
15623 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
15624Status of all breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
15625The \"Type\" column indicates one of:\n\
15626\tbreakpoint - normal breakpoint\n\
15627\twatchpoint - watchpoint\n\
15628\tlongjmp - internal breakpoint used to step through longjmp()\n\
15629\tlongjmp resume - internal breakpoint at the target of longjmp()\n\
15630\tuntil - internal breakpoint used by the \"until\" command\n\
1a966eab
AC
15631\tfinish - internal breakpoint used by the \"finish\" command\n\
15632The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
c906108c
SS
15633the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15634breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1a966eab
AC
15635address and file/line number respectively.\n\
15636\n\
15637Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
15638are set to the address of the last breakpoint listed unless the command\n\
15639is prefixed with \"server \".\n\n\
c906108c 15640Convenience variable \"$bpnum\" contains the number of the last\n\
1a966eab 15641breakpoint set."),
c906108c
SS
15642 &maintenanceinfolist);
15643
44feb3ce
TT
15644 add_prefix_cmd ("catch", class_breakpoint, catch_command, _("\
15645Set catchpoints to catch events."),
15646 &catch_cmdlist, "catch ",
15647 0/*allow-unknown*/, &cmdlist);
15648
15649 add_prefix_cmd ("tcatch", class_breakpoint, tcatch_command, _("\
15650Set temporary catchpoints to catch events."),
15651 &tcatch_cmdlist, "tcatch ",
15652 0/*allow-unknown*/, &cmdlist);
15653
44feb3ce
TT
15654 add_catch_command ("fork", _("Catch calls to fork."),
15655 catch_fork_command_1,
a96d9b2e 15656 NULL,
44feb3ce
TT
15657 (void *) (uintptr_t) catch_fork_permanent,
15658 (void *) (uintptr_t) catch_fork_temporary);
15659 add_catch_command ("vfork", _("Catch calls to vfork."),
15660 catch_fork_command_1,
a96d9b2e 15661 NULL,
44feb3ce
TT
15662 (void *) (uintptr_t) catch_vfork_permanent,
15663 (void *) (uintptr_t) catch_vfork_temporary);
15664 add_catch_command ("exec", _("Catch calls to exec."),
15665 catch_exec_command_1,
a96d9b2e
SDJ
15666 NULL,
15667 CATCH_PERMANENT,
15668 CATCH_TEMPORARY);
edcc5120
TT
15669 add_catch_command ("load", _("Catch loads of shared libraries.\n\
15670Usage: catch load [REGEX]\n\
15671If REGEX is given, only stop for libraries matching the regular expression."),
15672 catch_load_command_1,
15673 NULL,
15674 CATCH_PERMANENT,
15675 CATCH_TEMPORARY);
15676 add_catch_command ("unload", _("Catch unloads of shared libraries.\n\
15677Usage: catch unload [REGEX]\n\
15678If REGEX is given, only stop for libraries matching the regular expression."),
15679 catch_unload_command_1,
15680 NULL,
15681 CATCH_PERMANENT,
15682 CATCH_TEMPORARY);
c5aa993b 15683
1bedd215
AC
15684 c = add_com ("watch", class_breakpoint, watch_command, _("\
15685Set a watchpoint for an expression.\n\
06a64a0b 15686Usage: watch [-l|-location] EXPRESSION\n\
c906108c 15687A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
15688an expression changes.\n\
15689If -l or -location is given, this evaluates EXPRESSION and watches\n\
15690the memory to which it refers."));
65d12d83 15691 set_cmd_completer (c, expression_completer);
c906108c 15692
1bedd215
AC
15693 c = add_com ("rwatch", class_breakpoint, rwatch_command, _("\
15694Set a read watchpoint for an expression.\n\
06a64a0b 15695Usage: rwatch [-l|-location] EXPRESSION\n\
c906108c 15696A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
15697an expression is read.\n\
15698If -l or -location is given, this evaluates EXPRESSION and watches\n\
15699the memory to which it refers."));
65d12d83 15700 set_cmd_completer (c, expression_completer);
c906108c 15701
1bedd215
AC
15702 c = add_com ("awatch", class_breakpoint, awatch_command, _("\
15703Set a watchpoint for an expression.\n\
06a64a0b 15704Usage: awatch [-l|-location] EXPRESSION\n\
c906108c 15705A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
15706an expression is either read or written.\n\
15707If -l or -location is given, this evaluates EXPRESSION and watches\n\
15708the memory to which it refers."));
65d12d83 15709 set_cmd_completer (c, expression_completer);
c906108c 15710
11db9430 15711 add_info ("watchpoints", info_watchpoints_command, _("\
e5a67952 15712Status of specified watchpoints (all watchpoints if no argument)."));
c906108c 15713
920d2a44
AC
15714 /* XXX: cagney/2005-02-23: This should be a boolean, and should
15715 respond to changes - contrary to the description. */
85c07804
AC
15716 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
15717 &can_use_hw_watchpoints, _("\
15718Set debugger's willingness to use watchpoint hardware."), _("\
15719Show debugger's willingness to use watchpoint hardware."), _("\
c906108c
SS
15720If zero, gdb will not use hardware for new watchpoints, even if\n\
15721such is available. (However, any hardware watchpoints that were\n\
15722created before setting this to nonzero, will continue to use watchpoint\n\
85c07804
AC
15723hardware.)"),
15724 NULL,
920d2a44 15725 show_can_use_hw_watchpoints,
85c07804 15726 &setlist, &showlist);
c906108c
SS
15727
15728 can_use_hw_watchpoints = 1;
fa8d40ab 15729
1042e4c0
SS
15730 /* Tracepoint manipulation commands. */
15731
15732 c = add_com ("trace", class_breakpoint, trace_command, _("\
629500fa 15733Set a tracepoint at specified location.\n\
1042e4c0
SS
15734\n"
15735BREAK_ARGS_HELP ("trace") "\n\
15736Do \"help tracepoints\" for info on other tracepoint commands."));
15737 set_cmd_completer (c, location_completer);
15738
15739 add_com_alias ("tp", "trace", class_alias, 0);
15740 add_com_alias ("tr", "trace", class_alias, 1);
15741 add_com_alias ("tra", "trace", class_alias, 1);
15742 add_com_alias ("trac", "trace", class_alias, 1);
15743
7a697b8d 15744 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
629500fa 15745Set a fast tracepoint at specified location.\n\
7a697b8d
SS
15746\n"
15747BREAK_ARGS_HELP ("ftrace") "\n\
15748Do \"help tracepoints\" for info on other tracepoint commands."));
15749 set_cmd_completer (c, location_completer);
15750
0fb4aa4b 15751 c = add_com ("strace", class_breakpoint, strace_command, _("\
629500fa 15752Set a static tracepoint at location or marker.\n\
0fb4aa4b
PA
15753\n\
15754strace [LOCATION] [if CONDITION]\n\
629500fa
KS
15755LOCATION may be a linespec, explicit, or address location (described below) \n\
15756or -m MARKER_ID.\n\n\
15757If a marker id is specified, probe the marker with that name. With\n\
15758no LOCATION, uses current execution address of the selected stack frame.\n\
0fb4aa4b
PA
15759Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
15760This collects arbitrary user data passed in the probe point call to the\n\
15761tracing library. You can inspect it when analyzing the trace buffer,\n\
15762by printing the $_sdata variable like any other convenience variable.\n\
15763\n\
15764CONDITION is a boolean expression.\n\
89549d7f 15765\n" LOCATION_HELP_STRING "\n\n\
d41c0fc8
PA
15766Multiple tracepoints at one place are permitted, and useful if their\n\
15767conditions are different.\n\
0fb4aa4b
PA
15768\n\
15769Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
15770Do \"help tracepoints\" for info on other tracepoint commands."));
15771 set_cmd_completer (c, location_completer);
15772
11db9430 15773 add_info ("tracepoints", info_tracepoints_command, _("\
e5a67952 15774Status of specified tracepoints (all tracepoints if no argument).\n\
1042e4c0
SS
15775Convenience variable \"$tpnum\" contains the number of the\n\
15776last tracepoint set."));
15777
15778 add_info_alias ("tp", "tracepoints", 1);
15779
15780 add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
15781Delete specified tracepoints.\n\
15782Arguments are tracepoint numbers, separated by spaces.\n\
15783No argument means delete all tracepoints."),
15784 &deletelist);
7e20dfcd 15785 add_alias_cmd ("tr", "tracepoints", class_trace, 1, &deletelist);
1042e4c0
SS
15786
15787 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
15788Disable specified tracepoints.\n\
15789Arguments are tracepoint numbers, separated by spaces.\n\
15790No argument means disable all tracepoints."),
15791 &disablelist);
15792 deprecate_cmd (c, "disable");
15793
15794 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
15795Enable specified tracepoints.\n\
15796Arguments are tracepoint numbers, separated by spaces.\n\
15797No argument means enable all tracepoints."),
15798 &enablelist);
15799 deprecate_cmd (c, "enable");
15800
15801 add_com ("passcount", class_trace, trace_pass_command, _("\
15802Set the passcount for a tracepoint.\n\
15803The trace will end when the tracepoint has been passed 'count' times.\n\
15804Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
15805if TPNUM is omitted, passcount refers to the last tracepoint defined."));
15806
6149aea9
PA
15807 add_prefix_cmd ("save", class_breakpoint, save_command,
15808 _("Save breakpoint definitions as a script."),
15809 &save_cmdlist, "save ",
15810 0/*allow-unknown*/, &cmdlist);
15811
15812 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
15813Save current breakpoint definitions as a script.\n\
cce7e648 15814This includes all types of breakpoints (breakpoints, watchpoints,\n\
6149aea9
PA
15815catchpoints, tracepoints). Use the 'source' command in another debug\n\
15816session to restore them."),
15817 &save_cmdlist);
15818 set_cmd_completer (c, filename_completer);
15819
15820 c = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
1042e4c0 15821Save current tracepoint definitions as a script.\n\
6149aea9
PA
15822Use the 'source' command in another debug session to restore them."),
15823 &save_cmdlist);
1042e4c0
SS
15824 set_cmd_completer (c, filename_completer);
15825
6149aea9
PA
15826 c = add_com_alias ("save-tracepoints", "save tracepoints", class_trace, 0);
15827 deprecate_cmd (c, "save tracepoints");
15828
1bedd215 15829 add_prefix_cmd ("breakpoint", class_maintenance, set_breakpoint_cmd, _("\
590042fc 15830Breakpoint specific settings.\n\
fa8d40ab 15831Configure various breakpoint-specific variables such as\n\
590042fc 15832pending breakpoint behavior."),
fa8d40ab
JJ
15833 &breakpoint_set_cmdlist, "set breakpoint ",
15834 0/*allow-unknown*/, &setlist);
1bedd215 15835 add_prefix_cmd ("breakpoint", class_maintenance, show_breakpoint_cmd, _("\
590042fc 15836Breakpoint specific settings.\n\
fa8d40ab 15837Configure various breakpoint-specific variables such as\n\
590042fc 15838pending breakpoint behavior."),
fa8d40ab
JJ
15839 &breakpoint_show_cmdlist, "show breakpoint ",
15840 0/*allow-unknown*/, &showlist);
15841
7915a72c
AC
15842 add_setshow_auto_boolean_cmd ("pending", no_class,
15843 &pending_break_support, _("\
15844Set debugger's behavior regarding pending breakpoints."), _("\
15845Show debugger's behavior regarding pending breakpoints."), _("\
6e1d7d6c
AC
15846If on, an unrecognized breakpoint location will cause gdb to create a\n\
15847pending breakpoint. If off, an unrecognized breakpoint location results in\n\
15848an error. If auto, an unrecognized breakpoint location results in a\n\
7915a72c 15849user-query to see if a pending breakpoint should be created."),
2c5b56ce 15850 NULL,
920d2a44 15851 show_pending_break_support,
6e1d7d6c
AC
15852 &breakpoint_set_cmdlist,
15853 &breakpoint_show_cmdlist);
fa8d40ab
JJ
15854
15855 pending_break_support = AUTO_BOOLEAN_AUTO;
765dc015
VP
15856
15857 add_setshow_boolean_cmd ("auto-hw", no_class,
15858 &automatic_hardware_breakpoints, _("\
15859Set automatic usage of hardware breakpoints."), _("\
15860Show automatic usage of hardware breakpoints."), _("\
15861If set, the debugger will automatically use hardware breakpoints for\n\
15862breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
15863a warning will be emitted for such breakpoints."),
15864 NULL,
15865 show_automatic_hardware_breakpoints,
15866 &breakpoint_set_cmdlist,
15867 &breakpoint_show_cmdlist);
74960c60 15868
a25a5a45
PA
15869 add_setshow_boolean_cmd ("always-inserted", class_support,
15870 &always_inserted_mode, _("\
74960c60
VP
15871Set mode for inserting breakpoints."), _("\
15872Show mode for inserting breakpoints."), _("\
a25a5a45
PA
15873When this mode is on, breakpoints are inserted immediately as soon as\n\
15874they're created, kept inserted even when execution stops, and removed\n\
15875only when the user deletes them. When this mode is off (the default),\n\
15876breakpoints are inserted only when execution continues, and removed\n\
15877when execution stops."),
72d0e2c5
YQ
15878 NULL,
15879 &show_always_inserted_mode,
15880 &breakpoint_set_cmdlist,
15881 &breakpoint_show_cmdlist);
f1310107 15882
b775012e
LM
15883 add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
15884 condition_evaluation_enums,
15885 &condition_evaluation_mode_1, _("\
15886Set mode of breakpoint condition evaluation."), _("\
15887Show mode of breakpoint condition evaluation."), _("\
d1cda5d9 15888When this is set to \"host\", breakpoint conditions will be\n\
b775012e
LM
15889evaluated on the host's side by GDB. When it is set to \"target\",\n\
15890breakpoint conditions will be downloaded to the target (if the target\n\
15891supports such feature) and conditions will be evaluated on the target's side.\n\
15892If this is set to \"auto\" (default), this will be automatically set to\n\
15893\"target\" if it supports condition evaluation, otherwise it will\n\
15894be set to \"gdb\""),
15895 &set_condition_evaluation_mode,
15896 &show_condition_evaluation_mode,
15897 &breakpoint_set_cmdlist,
15898 &breakpoint_show_cmdlist);
15899
f1310107
TJB
15900 add_com ("break-range", class_breakpoint, break_range_command, _("\
15901Set a breakpoint for an address range.\n\
15902break-range START-LOCATION, END-LOCATION\n\
15903where START-LOCATION and END-LOCATION can be one of the following:\n\
15904 LINENUM, for that line in the current file,\n\
15905 FILE:LINENUM, for that line in that file,\n\
15906 +OFFSET, for that number of lines after the current line\n\
15907 or the start of the range\n\
15908 FUNCTION, for the first line in that function,\n\
15909 FILE:FUNCTION, to distinguish among like-named static functions.\n\
15910 *ADDRESS, for the instruction at that address.\n\
15911\n\
15912The breakpoint will stop execution of the inferior whenever it executes\n\
15913an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
15914range (including START-LOCATION and END-LOCATION)."));
15915
e7e0cddf 15916 c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
629500fa 15917Set a dynamic printf at specified location.\n\
e7e0cddf 15918dprintf location,format string,arg1,arg2,...\n\
629500fa
KS
15919location may be a linespec, explicit, or address location.\n"
15920"\n" LOCATION_HELP_STRING));
e7e0cddf
SS
15921 set_cmd_completer (c, location_completer);
15922
15923 add_setshow_enum_cmd ("dprintf-style", class_support,
15924 dprintf_style_enums, &dprintf_style, _("\
15925Set the style of usage for dynamic printf."), _("\
15926Show the style of usage for dynamic printf."), _("\
15927This setting chooses how GDB will do a dynamic printf.\n\
15928If the value is \"gdb\", then the printing is done by GDB to its own\n\
15929console, as with the \"printf\" command.\n\
15930If the value is \"call\", the print is done by calling a function in your\n\
15931program; by default printf(), but you can choose a different function or\n\
15932output stream by setting dprintf-function and dprintf-channel."),
15933 update_dprintf_commands, NULL,
15934 &setlist, &showlist);
15935
15936 dprintf_function = xstrdup ("printf");
15937 add_setshow_string_cmd ("dprintf-function", class_support,
15938 &dprintf_function, _("\
590042fc
PW
15939Set the function to use for dynamic printf."), _("\
15940Show the function to use for dynamic printf."), NULL,
e7e0cddf
SS
15941 update_dprintf_commands, NULL,
15942 &setlist, &showlist);
15943
15944 dprintf_channel = xstrdup ("");
15945 add_setshow_string_cmd ("dprintf-channel", class_support,
15946 &dprintf_channel, _("\
590042fc
PW
15947Set the channel to use for dynamic printf."), _("\
15948Show the channel to use for dynamic printf."), NULL,
e7e0cddf
SS
15949 update_dprintf_commands, NULL,
15950 &setlist, &showlist);
15951
d3ce09f5
SS
15952 add_setshow_boolean_cmd ("disconnected-dprintf", no_class,
15953 &disconnected_dprintf, _("\
15954Set whether dprintf continues after GDB disconnects."), _("\
15955Show whether dprintf continues after GDB disconnects."), _("\
15956Use this to let dprintf commands continue to hit and produce output\n\
15957even if GDB disconnects or detaches from the target."),
15958 NULL,
15959 NULL,
15960 &setlist, &showlist);
15961
15962 add_com ("agent-printf", class_vars, agent_printf_command, _("\
590042fc
PW
15963Target agent only formatted printing, like the C \"printf\" function.\n\
15964Usage: agent-printf \"format string\", ARG1, ARG2, ARG3, ..., ARGN\n\
15965This supports most C printf format specifications, like %s, %d, etc.\n\
15966This is useful for formatted output in user-defined commands."));
d3ce09f5 15967
491144b5 15968 automatic_hardware_breakpoints = true;
f3b1572e 15969
76727919
TT
15970 gdb::observers::about_to_proceed.attach (breakpoint_about_to_proceed);
15971 gdb::observers::thread_exit.attach (remove_threaded_breakpoints);
c906108c 15972}
This page took 4.161993 seconds and 4 git commands to generate.